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/instrs.h
julianb0 81b739a739
mem
2019-06-06 14:57:34 +02:00

162 lines
8.3 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 <pc/arch.h>
#include <in/arch_i.h>
#define DECV(p, v) \
ulong v = (p->type == A_REG ? R(p->val) : p->val); \
if (p->mem) { \
v = readmem(ctx, v + p->off + R(p->offreg), p1->mlen); \
}
#define IMPL_START_0(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{
#define IMPL_START_1(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
DECV(p1, v1);
#define IMPL_START_2(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
DECV(p1, v1); \
DECV(p2, v2);
#define IMPL_START_3(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
DECV(p2, v2);
#define IMPL_OUT \
assert(p1->type == A_REG || p1->mem); \
if (p1->mem) { \
ulong addr = p1->type == A_REG ? R(p1->val) : p1->val; \
writemem(ctx, v1, addr + p1->off + R(p1->offreg), p1->mlen); \
} \
else R(p1->val) = v1; \
}
#define IMPL_OUT_2 \
assert(p1->type == A_REG || p1->mem); \
if (p1->mem) { \
ulong addr = p1->type == A_REG ? R(p1->val) : p1->val; \
writemem(ctx, v1, addr + p1->off + R(p1->offreg), p1->mlen); \
} \
else R(p1->val) = v1; \
\
assert(p2->type == A_REG || p2->mem); \
if (p2->mem) { \
ulong addr = p2->type == A_REG ? R(p2->val) : p2->val; \
writemem(ctx, v2, addr + p2->off + R(p2->offreg), p2->mlen); \
} \
else R(p2->val) = v2; \
}
#define IMPL_END \
}
//
// c...z instructions easy implementation
//
#define IMPL_CxxxZ(name) \
IMPL_START_0(c##name##z) \
{ \
if (flg & ZF) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxNZ(name) \
IMPL_START_0(c##name##nz) \
{ \
if (!(flg & ZF)) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxA(name) \
IMPL_START_0(c##name##a) \
{ \
if (!(flg & ZF) && !(flg & CF)) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxAE(name) \
IMPL_START_0(c##name##ae) \
{ \
if (!(flg & CF)) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxB(name) \
IMPL_START_0(c##name##b) \
{ \
if (!(flg & ZF) && (flg & CF)) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxBE(name) \
IMPL_START_0(c##name##be) \
{ \
if (flg & CF) { \
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_COND(name) \
IMPL_CxxxZ(name); \
IMPL_CxxxA(name); \
IMPL_CxxxB(name); \
IMPL_CxxxNZ(name); \
IMPL_CxxxAE(name); \
IMPL_CxxxBE(name)
//
// Consistency checks
//
#define CHK_SUPERV() \
do { \
if ((cr0 & UF) == 1) { \
_except(ctx, E_SYS, "Supervisor-only INSTR"); \
} \
} while (0)
#define CHK_STACK(op) \
if (rsp % 8 > 0 || rbp % 8 > 0) { \
_except(ctx, E_STK, "Misaligned stack REGS"); \
} \
if (rbp op rsp) { \
_except(ctx, E_STK, "RSP above RBP"); \
}
//
// Common operations
//
#define PUSH(v) \
writemem64(ctx, v, rsp); \
rsp -= 8;
#define POP(v) \
rsp += 8; \
v = readmem64(ctx, rsp);
#define JUMP(v) \
rip = v + cr1