kvisc/pc/instrs/instrs.h

103 lines
4.8 KiB
C
Raw Normal View History

2019-05-30 12:44:56 +02:00
// The OS/K Team licenses 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-30 20:23:27 +02:00
{
2019-05-15 22:08:37 +02:00
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) \
{ \
2019-06-02 16:33:28 +02:00
ulong v1 = (p1->type == A_REG ? R(p1->val) : p1->val); \
2019-05-30 20:23:27 +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) \
{ \
2019-06-02 16:33:28 +02:00
ulong v1 = (p1->type == A_REG ? R(p1->val) : p1->val); \
ulong v2 = (p2->type == A_REG ? R(p2->val) : p2->val); \
2019-05-29 16:57:22 +02:00
if (p1->mem) v1 = readmem(ctx, v1 + p1->off, p1->mlen); \
2019-05-30 20:23:27 +02:00
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) \
{ \
2019-06-02 16:33:28 +02:00
ulong v2 = (p2->type == A_REG ? R(p2->val) : p2->val); \
2019-05-30 20:23:27 +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) { \
2019-06-02 16:33:28 +02:00
ulong addr = p1->type == A_REG ? R(p1->val) : p1->val; \
2019-05-30 19:33:22 +02:00
writemem(ctx, v1, addr + p1->off, p1->mlen); \
2019-05-16 16:48:45 +02:00
} \
2019-06-02 16:33:28 +02:00
else R(p1->val) = v1; \
2019-05-30 20:23:27 +02:00
}
2019-05-15 21:47:08 +02:00
#define IMPL_END \
}
2019-05-16 16:48:45 +02:00
2019-05-30 20:23:27 +02:00
//
// c...z instructions easy implementation
//
#define IMPL_CxxxZ(name) \
IMPL_START_0(c##name##z) \
{ \
2019-06-02 16:33:28 +02:00
if (flg & ZF) { \
2019-05-30 20:23:27 +02:00
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_CxxxNZ(name) \
IMPL_START_0(c##name##nz) \
{ \
2019-06-02 16:33:28 +02:00
if (!(flg & ZF)) { \
2019-05-30 20:23:27 +02:00
i_##name(ctx, p1, p2); \
} \
} \
IMPL_END
#define IMPL_COND(name) \
IMPL_CxxxZ(name); \
IMPL_CxxxNZ(name)
//
// Consistency checks
//
2019-05-16 16:48:45 +02:00
#define CHK_SUPERV() \
do { \
2019-06-02 16:33:28 +02:00
if ((cr0 & 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) \
2019-06-02 16:33:28 +02:00
if (rsp % 8 > 0 || rbp % 8 > 0) { \
2019-05-29 16:57:22 +02:00
_except(ctx, E_STK, "Misaligned stack REGS"); \
} \
2019-06-02 16:33:28 +02:00
if (rbp op rsp) { \
2019-05-29 16:57:22 +02:00
_except(ctx, E_STK, "RSP above RBP"); \
}
2019-05-30 20:23:27 +02:00
//
// Common operations
//
2019-05-29 16:57:22 +02:00
#define PUSH(v) \
2019-06-02 16:33:28 +02:00
writemem64(ctx, v, rsp); \
rsp -= 8;
2019-05-29 16:57:22 +02:00
#define POP(v) \
2019-06-02 16:33:28 +02:00
rsp += 8; \
v = readmem64(ctx, rsp);
2019-05-29 16:57:22 +02:00
#define JUMP(v) \
2019-06-02 16:33:28 +02:00
rip = v + cr1
2019-05-29 16:57:22 +02:00