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
julianb0 89ed8fdc0d
vm
2019-08-03 17:41:44 +02:00

153 lines
2.4 KiB
C

// 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>
//----------------------------------------------------------------------------//
IMPL_START(jmp) { R(RIP) = p1->val; return 0; }
IMPL_START(loop) {
if (R(RCX) > 0) {
R(RCX)--;
R(RIP) = p1->val;
}
return 0;
}
IMPL_START(b)
{
SRCP(p1);
SRCP(p2);
COMPARE_SUB(p1->val, p2->val);
if (eval_cond(ctx, ctx->cond))
R(RIP) = p3->val;
return 0;
}
IMPL_START(cmp)
{
SRCP(p1);
COMPARE_SUB(p1->val, p2->val);
return 0;
}
//----------------------------------------------------------------------------//
IMPL_START(lea) { *r1 = p2->addr; return 1; }
IMPL_START(mov) { XSRCP(*r1, p2, sx); return 1; }
IMPL_START(movzx) { XSRCP(*r1, p2, zx); return 1; }
IMPL_START(movsxb) { SRCP(p2); *r1 = (ulong)(long)(char)p2->val; return 1; }
IMPL_START(movsxw) { SRCP(p2); *r1 = (ulong)(long)(short)p2->val; return 1; }
IMPL_START(movsxd) { SRCP(p2); *r1 = (ulong)(long)(int)p2->val; return 1; }
IMPL_START(xchg)
{
SRCP(p1);
SRCP(p2);
*r2 = p1->val;
*r1 = p2->val;
return 2;
}
//----------------------------------------------------------------------------//
//
// Stack manipulation instructions
//
IMPL_START(push)
{
XSRCP(p1->val, p1, zx);
R(RSP) -= 8;
writemem(ctx, p1->val, R(RSP), 8);
return 0;
}
IMPL_START(pop)
{
*r1 = readmemzx(ctx, R(RSP), 8);
R(RSP) += 8;
return 1;
}
IMPL_START(call)
{
SRCP(p1);
R(RSP) -= 8;
writemem(ctx, R(RIP), R(RSP), 8);
R(RIP) = p1->val;
return 0;
}
IMPL_START(xcall2)
{
SRCP(p1);
SRCP(p2);
R(RSP) -= 8;
writemem(ctx, R(RIP), R(RSP), 8);
R(RIP) = p1->val;
R(AX0) = p2->val;
return 0;
}
IMPL_START(xcall3)
{
SRCP(p1);
SRCP(p2);
SRCP(p3);
R(RSP) -= 8;
writemem(ctx, R(RIP), R(RSP), 8);
R(RIP) = p1->val;
R(AX0) = p2->val;
R(AX1) = p3->val;
return 0;
}
IMPL_START(ret)
{
R(RIP) = readmemzx(ctx, R(RSP), 8);
R(RSP) += 8;
return 0;
}
IMPL_START(enter)
{
writemem(ctx, R(RBP), R(RSP) - 8, 8);
R(RBP) = R(RSP) - 8;
R(RSP) -= (p1->val + 1) * 8;
return 0;
}
IMPL_START(leave)
{
R(RSP) = R(RBP) + 8;
R(RBP) = readmemzx(ctx, R(RBP), 8);
return 0;
}
//----------------------------------------------------------------------------//