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
2019-07-24 16:52:26 +02:00

167 lines
2.8 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)
{
ulong v;
if (ACC_FMT_IS_MEM(p1->type))
v = readmemsx(ctx, p1->addr, p1->mlen);
else v = p1->val;
COMPARE_SUB(v, p2->val);
if (eval_cond(ctx, ctx->cond))
R(RIP) = p3->val;
return 0;
}
IMPL_START(cmp)
{
ulong v;
if (ACC_FMT_IS_MEM(p1->type))
v = readmemsx(ctx, p1->addr, p1->mlen);
else v = p1->val;
COMPARE_SUB(v, p2->val);
return 0;
}
//----------------------------------------------------------------------------//
IMPL_START(lea) { *r1 = p2->addr; return 1; }
IMPL_START(mov)
{
if (ACC_FMT_IS_MEM(p2->type))
*r1 = readmemsx(ctx, p2->addr, p2->mlen);
else *r1 = p2->val;
return 1;
}
IMPL_START(movzx)
{
if (ACC_FMT_IS_MEM(p2->type))
{
if (__builtin_expect(p2->mlen == 8, 0))
_except(ctx, E_ILL, "MOVZX with qword memory source");
*r1 = readmemzx(ctx, p2->addr, p2->mlen);
}
else *r1 = p2->val;
return 1;
}
IMPL_START(movsxb) { *r1 = (ulong)(long)(char)(p2->val & 0xFF); return 1; }
IMPL_START(movsxw) { *r1 = (ulong)(long)(short)(p2->val & 0xFFFF); return 1; }
IMPL_START(movsxd) { *r1 = (ulong)(long)(int)(p2->val & 0xFFFFFFFF); return 1; }
IMPL_START(xchg)
{
*r2 = p1->val;
if (ACC_FMT_IS_MEM(p2->type))
*r1 = readmemsx(ctx, p2->addr, p2->mlen);
else *r1 = p2->val;
return 2;
}
//----------------------------------------------------------------------------//
//
// Stack manipulation instructions
//
IMPL_START(push)
{
ulong v;
if (ACC_FMT_IS_MEM(p1->type))
v = readmemzx(ctx, p1->addr, p1->mlen);
else v = p1->val;
R(RSP) -= 8;
writemem(ctx, v, R(RSP), 8);
return 0;
}
IMPL_START(pop)
{
*r1 = readmemzx(ctx, R(RSP), 8);
R(RSP) += 8;
return 1;
}
IMPL_START(call)
{
R(RSP) -= 8;
writemem(ctx, R(RIP), R(RSP), 8);
R(RIP) = p1->val;
if (p2)
{
R(AX0) = p2->val;
if (p3)
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;
}
//----------------------------------------------------------------------------//