1
0
mirror of https://gitlab.os-k.eu/os-k-team/kvisc.git synced 2023-08-25 14:05:46 +02:00
kvisc/pc/instrs/instrs.h

71 lines
3.8 KiB
C
Raw Normal View History

2019-05-15 21:47:08 +02:00
// The OS/K Team licences this file to you under the MIT license.
2019-05-15 21:56:42 +02:00
// See the LICENSE file in the project root for more information.
2019-05-15 21:47:08 +02:00
2019-05-29 16:57:22 +02:00
#include "../arch.h"
2019-05-15 21:47:08 +02:00
2019-05-15 22:08:37 +02:00
#define IMPL_START_0(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
2019-05-15 21:47:08 +02:00
#define IMPL_START_1(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
ulong v1 = (p1->type == A_REG ? ctx->r[p1->val].val : p1->val); \
2019-05-29 16:57:22 +02:00
if (p1->mem) v1 = readmem(ctx, v1 + p1->off, p1->mlen); \
2019-05-15 21:47:08 +02:00
#define IMPL_START_2(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
ulong v1 = (p1->type == A_REG ? ctx->r[p1->val].val : p1->val); \
ulong v2 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val); \
2019-05-29 16:57:22 +02:00
if (p1->mem) v1 = readmem(ctx, v1 + p1->off, p1->mlen); \
if (p2->mem) v2 = readmem(ctx, v2 + p2->off, p2->mlen); \
2019-05-16 16:48:45 +02:00
#define IMPL_START_3(name) \
void i_##name(ctx_t *ctx, acc_t *p1, acc_t *p2) \
{ \
ulong v2 = (p2->type == A_REG ? ctx->r[p2->val].val : p2->val); \
2019-05-29 16:57:22 +02:00
if (p2->mem) v2 = readmem(ctx, v2 + p2->off, p2->mlen); \
2019-05-16 16:48:45 +02:00
2019-05-15 21:47:08 +02:00
#define IMPL_OUT \
2019-05-16 16:48:45 +02:00
assert(p1->type == A_REG || p1->mem); \
if (p1->mem) { \
ulong addr = p1->type == A_REG ? ctx->r[p1->val].val : p1->val; \
2019-05-29 16:57:22 +02:00
writemem(ctx, v1, addr, p1->mlen); \
2019-05-16 16:48:45 +02:00
} \
else ctx->r[p1->val].val = v1; \
2019-05-15 21:47:08 +02:00
} \
#define IMPL_END \
}
2019-05-16 16:48:45 +02:00
#define CHK_SUPERV() \
do { \
2019-05-29 19:00:13 +02:00
if ((ctx->r[CR0].val & UF) == 1) { \
2019-05-16 16:48:45 +02:00
_except(ctx, E_SYS, "Supervisor-only INSTR"); \
} \
} while (0)
2019-05-15 21:47:08 +02:00
2019-05-29 16:57:22 +02:00
#define CHK_STACK(op) \
if (ctx->r[RSP].val % 8 > 0 || ctx->r[RBP].val % 8 > 0) { \
_except(ctx, E_STK, "Misaligned stack REGS"); \
} \
if (ctx->r[RBP].val op ctx->r[RSP].val) { \
_except(ctx, E_STK, "RSP above RBP"); \
}
#define PUSH(v) \
writemem64(ctx, v, ctx->r[RSP].val); \
ctx->r[RSP].val -= 8; \
#define POP(v) \
ctx->r[RSP].val += 8; \
v = readmem64(ctx, ctx->r[RSP].val);
#define JUMP(v) \
ctx->r[RIP].val = v + ctx->r[CR1].val