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

153 lines
2.4 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>
//----------------------------------------------------------------------------//
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)
{
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;
}
IMPL_START(cmp)
{
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;
}
//----------------------------------------------------------------------------//
IMPL_START(lea) { *r1 = p2->addr; return 1; }
2019-08-03 17:41:44 +02:00
IMPL_START(mov) { XSRCP(*r1, p2, sx); return 1; }
IMPL_START(movzx) { XSRCP(*r1, p2, zx); return 1; }
2019-07-24 16:52:26 +02:00
2019-08-03 17:41:44 +02:00
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; }
2019-07-24 16:52:26 +02:00
IMPL_START(xchg)
{
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;
}
//----------------------------------------------------------------------------//
//
// Stack manipulation instructions
//
IMPL_START(push)
{
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;
}
IMPL_START(pop)
{
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;
}
IMPL_START(call)
{
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-03 17:41:44 +02:00
IMPL_START(xcall2)
{
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;
}
IMPL_START(xcall3)
{
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;
}
IMPL_START(ret)
{
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;
}
IMPL_START(enter)
{
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;
}
IMPL_START(leave)
{
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;
}
//----------------------------------------------------------------------------//