-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.h
150 lines (134 loc) · 2.37 KB
/
instruction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#pragma once
#include <stdint.h>
#include "hash_trie.h"
#define OPCODES(X) \
X(PUSH) \
X(POP) \
X(UNDEF) \
X(NOP) \
X(LOAD) \
X(STORE) \
X(REF) \
X(LOAD_FIELD) \
X(FIELD_REF) \
X(BINOP) \
X(RET) \
X(CALL) \
X(CALL_PTR) \
X(TEST) \
X(JMP) \
X(JZ) \
X(JNZ) \
X(CONST_0) \
X(CONST_1) \
X(TABLE) \
X(WAIT) \
X(UNARY) \
X(VECTOR) \
X(PRINT_EXPR) \
X(GLOBAL)
// X(SELF)
typedef enum
{
OP_INVALID,
#define OPCODE_ENUM(NAME) OP_##NAME,
OPCODES(OPCODE_ENUM) OP_MAX
} Opcode;
static const char *opcode_names[] = {
"invalid",
#define OPCODE_ENUM_STR(NAME) #NAME,
OPCODES(OPCODE_ENUM_STR) NULL,
};
#define VM_CALL_FLAG_NONE (0)
#define VM_CALL_FLAG_THREADED (1)
#define VM_CALL_FLAG_METHOD (2)
// typedef enum
// {
// OP_PUSH,
// // OP_PUSHF,
// // ...
// OP_POP,
// OP_NOP,
// OP_LOAD,
// OP_STORE,
// OP_REF,
// OP_LOAD_FIELD,
// OP_FIELD_REF,
// // OP_LOAD_REF,
// // OP_STORE_REF,
// // OP_LOAD_OBJECT_FIELD_REF,
// OP_BINOP,
// // OP_ADD,
// // OP_SUB,
// // OP_MUL,
// // OP_DIV,
// // OP_MOD,
// OP_RET,
// OP_CALL,
// // OP_CALL_EXTERNAL,
// OP_TEST,
// OP_JMP,
// OP_JZ,
// OP_JNZ,
// OP_CONST_0,
// OP_CONST_1,
// OP_WAIT,
// // OP_LABEL
// } Opcode;
typedef enum
{
OPERAND_TYPE_NONE,
OPERAND_TYPE_INT,
OPERAND_TYPE_FLOAT,
OPERAND_TYPE_INDEXED_STRING
} OperandType;
static const char *operand_type_names[] = { "NONE", "INT", "FLOAT", "STRING", NULL };
typedef struct
{
OperandType type;
union
{
int64_t integer;
float number;
unsigned int string_index;
} value;
} Operand;
#define MAX_OPERANDS (4)
typedef struct Instruction Instruction;
struct Instruction
{
int32_t offset;
uint8_t opcode;
Operand operands[MAX_OPERANDS];
int line;
};
enum
{
sizeof_Instruction = sizeof(Instruction)
};
#define MAX_INSTRUCTIONS (1 << 17) // 80 bytes * (1 << 17) = 10MB
enum
{
COMPILE_STATE_NOT_STARTED,
COMPILE_STATE_FAILED,
COMPILE_STATE_DONE
};
typedef struct
{
const char *name;
HashTrie functions;
HashTrie includes;
HashTrie file_references;
int state;
} CompiledFile;
typedef struct
{
const char *name;
CompiledFile *file;
Instruction *instructions;
int instruction_count;
size_t parameter_count;
size_t local_count;
char **variable_names;
int line;
} CompiledFunction;