1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/vm/in/mem.c

209 lines
3.5 KiB
C
Raw Normal View History

2019-07-24 16:52:26 +02:00
// The OS/K Team licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <in/instrs.h>
//----------------------------------------------------------------------------//
2019-08-08 18:39:12 +02:00
IMPL_START(jmp, 1) { R(RIP) = p1->val; return 0; }
2019-07-24 16:52:26 +02:00
2019-08-08 18:39:12 +02:00
IMPL_START(loop, 1) {
2019-07-24 16:52:26 +02:00
if (R(RCX) > 0) {
R(RCX)--;
R(RIP) = p1->val;
}
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(bch, 3)
2019-07-24 16:52:26 +02:00
{
2019-08-03 17:41:44 +02:00
SRCP(p1);
SRCP(p2);
2019-07-24 16:52:26 +02:00
2019-08-03 17:41:44 +02:00
COMPARE_SUB(p1->val, p2->val);
2019-07-24 16:52:26 +02:00
2019-08-03 19:01:12 +02:00
if (eval_cond(ctx->cond))
2019-07-24 16:52:26 +02:00
R(RIP) = p3->val;
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(cmp, 2)
2019-07-24 16:52:26 +02:00
{
2019-08-03 17:41:44 +02:00
SRCP(p1);
2019-07-24 16:52:26 +02:00
2019-08-03 17:41:44 +02:00
COMPARE_SUB(p1->val, p2->val);
2019-07-24 16:52:26 +02:00
return 0;
}
//----------------------------------------------------------------------------//
2019-08-08 18:39:12 +02:00
IMPL_START(lea, 2) { *r1 = p2->addr; return 1; }
IMPL_START(mov, 2) { XSRCP(*r1, p2, sx); return 1; }
IMPL_START(movzx, 2) { XSRCP(*r1, p2, zx); return 1; }
2019-07-24 16:52:26 +02:00
2019-08-08 18:39:12 +02:00
IMPL_START(movsxb, 2) { SRCP(p2); *r1 = (ulong)(long)(char)p2->val; return 1; }
IMPL_START(movsxw, 2) { SRCP(p2); *r1 = (ulong)(long)(short)p2->val; return 1; }
IMPL_START(movsxd, 2) { SRCP(p2); *r1 = (ulong)(long)(int)p2->val; return 1; }
2019-07-24 16:52:26 +02:00
2019-08-08 18:39:12 +02:00
IMPL_START(xchg, 2)
2019-07-24 16:52:26 +02:00
{
2019-08-03 17:41:44 +02:00
SRCP(p1);
SRCP(p2);
2019-07-24 16:52:26 +02:00
2019-08-03 17:41:44 +02:00
*r2 = p1->val;
*r1 = p2->val;
2019-07-24 16:52:26 +02:00
return 2;
}
//----------------------------------------------------------------------------//
2019-08-08 18:39:12 +02:00
IMPL_START(push, 1)
2019-07-24 16:52:26 +02:00
{
2019-08-03 17:41:44 +02:00
XSRCP(p1->val, p1, zx);
2019-07-24 16:52:26 +02:00
R(RSP) -= 8;
2019-08-03 19:01:12 +02:00
writemem(p1->val, R(RSP), 8);
2019-07-24 16:52:26 +02:00
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(push, 2)
{
XSRCP(p1->val, p1, zx);
XSRCP(p2->val, p2, zx);
R(RSP) -= 16;
writemem(p1->val, R(RSP) + 8, 8);
writemem(p2->val, R(RSP) + 0, 8);
return 0;
}
IMPL_START(push, 3)
{
XSRCP(p1->val, p1, zx);
XSRCP(p2->val, p2, zx);
XSRCP(p3->val, p3, zx);
R(RSP) -= 24;
writemem(p1->val, R(RSP) + 16, 8);
writemem(p2->val, R(RSP) + 8, 8);
writemem(p3->val, R(RSP) + 0, 8);
return 0;
}
//----------------------------------------------------------------------------//
IMPL_START(pop, 1)
2019-07-24 16:52:26 +02:00
{
2019-08-03 19:01:12 +02:00
*r1 = readmemzx(R(RSP), 8);
2019-07-24 16:52:26 +02:00
R(RSP) += 8;
return 1;
}
2019-08-08 18:39:12 +02:00
IMPL_START(pop, 2)
{
*r1 = readmemzx(R(RSP) + 0, 8);
*r2 = readmemzx(R(RSP) + 8, 8);
R(RSP) += 16;
return 2;
}
IMPL_START(pop, 3)
{
*r1 = readmemzx(R(RSP) + 0, 8);
*r2 = readmemzx(R(RSP) + 8, 8);
*r3 = readmemzx(R(RSP) + 16, 8);
R(RSP) += 24;
return 3;
}
//----------------------------------------------------------------------------//
IMPL_START(call, 1)
2019-07-24 16:52:26 +02:00
{
2019-08-03 17:41:44 +02:00
SRCP(p1);
2019-07-24 16:52:26 +02:00
R(RSP) -= 8;
2019-08-03 19:01:12 +02:00
writemem(R(RIP), R(RSP), 8);
2019-07-24 16:52:26 +02:00
R(RIP) = p1->val;
2019-08-03 17:41:44 +02:00
return 0;
}
2019-07-24 16:52:26 +02:00
2019-08-08 18:39:12 +02:00
IMPL_START(call, 2)
2019-08-03 17:41:44 +02:00
{
SRCP(p1);
SRCP(p2);
R(RSP) -= 8;
2019-08-03 19:01:12 +02:00
writemem(R(RIP), R(RSP), 8);
2019-08-03 17:41:44 +02:00
R(RIP) = p1->val;
R(AX0) = p2->val;
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(call, 3)
2019-08-03 17:41:44 +02:00
{
SRCP(p1);
SRCP(p2);
SRCP(p3);
R(RSP) -= 8;
2019-08-03 19:01:12 +02:00
writemem(R(RIP), R(RSP), 8);
2019-08-03 17:41:44 +02:00
R(RIP) = p1->val;
R(AX0) = p2->val;
R(AX1) = p3->val;
2019-07-24 16:52:26 +02:00
return 0;
}
2019-08-08 18:39:12 +02:00
//----------------------------------------------------------------------------//
IMPL_START(ret, 0)
2019-07-24 16:52:26 +02:00
{
2019-08-03 19:01:12 +02:00
R(RIP) = readmemzx(R(RSP), 8);
2019-07-24 16:52:26 +02:00
R(RSP) += 8;
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(enter, 0)
{
R(RSP) -= 8;
writemem(R(RBP), R(RSP), 8);
R(RBP) = R(RSP);
return 0;
}
IMPL_START(enter, 1)
2019-07-24 16:52:26 +02:00
{
2019-08-03 19:01:12 +02:00
writemem(R(RBP), R(RSP) - 8, 8);
2019-07-24 16:52:26 +02:00
R(RBP) = R(RSP) - 8;
R(RSP) -= (p1->val + 1) * 8;
return 0;
}
2019-08-08 18:39:12 +02:00
IMPL_START(leave, 0)
2019-07-24 16:52:26 +02:00
{
R(RSP) = R(RBP) + 8;
2019-08-03 19:01:12 +02:00
R(RBP) = readmemzx(R(RBP), 8);
2019-07-24 16:52:26 +02:00
return 0;
}
//----------------------------------------------------------------------------//