89 lines
3.5 KiB
C
89 lines
3.5 KiB
C
#ifndef ZRE_OPCODES_H
|
|
#define ZRE_OPCODES_H
|
|
|
|
#include "common.h"
|
|
|
|
typedef union {
|
|
int32_t i; /* Integers */
|
|
float f; /* Float */
|
|
uint32_t u; /* Unsigned integers, also used for pointer address */
|
|
char c[4]; /* 4 Byte char array for string packing */
|
|
} Value;
|
|
|
|
typedef union {
|
|
uint32_t length;
|
|
char *string;
|
|
Value *array;
|
|
Value *code;
|
|
} Object;
|
|
|
|
#define MAX_REGS 256
|
|
typedef struct {
|
|
Value registers[MAX_REGS]; /* R0-R255 */
|
|
Object *locals; /* Short lived object */
|
|
} Frame;
|
|
|
|
#define STACK_SIZE 128
|
|
typedef struct {
|
|
Value stack[STACK_SIZE];
|
|
uint32_t stack_size;
|
|
uint32_t pc; /* Program counter */
|
|
Value *memory;
|
|
uint32_t memory_size;
|
|
Frame *frame;
|
|
} VM;
|
|
|
|
typedef enum {
|
|
OP_HALT, /* terminate execution */
|
|
OP_LOADI, /* dest = next memory location as int */
|
|
OP_LOADU, /* dest = next memory location as uint */
|
|
OP_LOADF, /* dest = next memory location as float */
|
|
OP_STOREI, /* next memory location = src1 as int */
|
|
OP_STOREU, /* next memory location = src1 as uint */
|
|
OP_STOREF, /* next memory location = src1 as float */
|
|
OP_ADD_INT, /* dest = src1 + src2 */
|
|
OP_SUB_INT, /* dest = src1 - src2 */
|
|
OP_MUL_INT, /* dest = src1 * src2 */
|
|
OP_DIV_INT, /* dest = src1 / src2 */
|
|
OP_JEQ_INT, /* jump to address dest if src1 as int == src2 as int */
|
|
OP_JGT_INT, /* jump to address dest if src1 as int > src2 as int*/
|
|
OP_JLT_INT, /* jump to address dest if src1 as int < src2 as int */
|
|
OP_JLE_INT, /* jump to address dest if src1 as int <= src2 as int */
|
|
OP_JGE_INT, /* jump to address dest if src1 as int >= src2 as int*/
|
|
OP_INT_TO_REAL, /* dest = src1 as f32 */
|
|
OP_ADD_UINT, /* dest = src1 + src2 */
|
|
OP_SUB_UINT, /* dest = src1 - src2 */
|
|
OP_MUL_UINT, /* dest = src1 * src2 */
|
|
OP_DIV_UINT, /* dest = src1 / src2 */
|
|
OP_JEQ_UINT, /* jump to address dest if src1 as int == src2 as uint */
|
|
OP_JGT_UINT, /* jump to address dest if src1 as int > src2 as uint*/
|
|
OP_JLT_UINT, /* jump to address dest if src1 as int < src2 as uint */
|
|
OP_JLE_UINT, /* jump to address dest if src1 as int <= src2 as uint */
|
|
OP_JGE_UINT, /* jump to address dest if src1 as int >= src2 as uint*/
|
|
OP_UINT_TO_REAL, /* dest = src1 as f32 */
|
|
OP_ADD_REAL, /* dest = src1 + src2 */
|
|
OP_SUB_REAL, /* dest = src1 - src2 */
|
|
OP_MUL_REAL, /* dest = src1 * src2 */
|
|
OP_DIV_REAL, /* dest = src1 / src2 */
|
|
OP_JEQ_REAL, /* jump to address dest if src1 as real == src2 as real */
|
|
OP_JGE_REAL, /* jump to address dest if src1 as real >= src2 as real */
|
|
OP_JGT_REAL, /* jump to address dest if src1 as real > src2 as real */
|
|
OP_JLT_REAL, /* jump to address dest if src1 as real < src2 as real */
|
|
OP_JLE_REAL, /* jump to address dest if src1 as real <= src2 as real */
|
|
OP_REAL_TO_INT, /* dest = src1 as int */
|
|
OP_REAL_TO_UINT, /* dest = src1 as uint */
|
|
OP_MOV, /* dest = src1 */
|
|
OP_JMP, /* jump to address src1 unconditionally */
|
|
OP_INT_TO_STRING, /* dest = src1 as str */
|
|
OP_UINT_TO_STRING, /* dest = src1 as str */
|
|
OP_REAL_TO_STRING, /* dest = src1 as str */
|
|
OP_READ_STRING, /* dest = read as str */
|
|
OP_PRINT_STRING, /* write src1 to stdout */
|
|
OP_CMP_STRING, /* dest = (str == src2) as bool */
|
|
} Opcode;
|
|
|
|
/* defines a uint32 opcode */
|
|
#define OP(opcode, a, b, c) ((opcode << 24) | (a << 16) | (b << 8) | c)
|
|
|
|
#endif
|