-
Notifications
You must be signed in to change notification settings - Fork 41
/
vm.h
371 lines (315 loc) · 7.9 KB
/
vm.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#ifndef VM_H
#define VM_H
#include "ngs.h"
#include "obj.h"
#define NGS_UNUSED __attribute__((unused))
// --- BYTECODE ---------------------------------
// * bytecode format
//
// "NGS BYTECODE" (String of length 12, not zero-terminated)
//
// 0x0102030405060708 - 8 bytes for byte ordering check
//
// uint16 - number of sections
//
// section:
// uint16 - type
// uint32 - data len
// data
//
// section type 1 data: code
// section type 2 data: globals patching
// uint16 - number of items
// item:
// Lstr
// uint16 - number of locations
// uint32[] - locations
// section type 0x101 data: source tracking
// * Files names
// uint16 - N files
// Lstring - file name
// * IP positions
// uint32 - N entries
// N entries:
// uint32 - ip
// uint32 x 4 - location
// uint16 - file number
typedef uint64_t BYTECODE_ORDER_CHECK;
typedef uint16_t BYTECODE_SECTIONS_COUNT;
typedef uint16_t BYTECODE_SECTION_TYPE;
typedef uint32_t BYTECODE_SECTION_LEN;
typedef uint16_t BYTECODE_GLOBALS_COUNT;
typedef uint16_t BYTECODE_GLOBALS_LOC_COUNT;
typedef uint32_t BYTECODE_GLOBALS_OFFSET;
extern char BYTECODE_SIGNATURE[];
#define BYTECODE_SECTION_TYPE_CODE (1)
#define BYTECODE_SECTION_TYPE_GLOBALS (2)
#define BYTECODE_SECTION_TYPE_SRCLOC (0x101)
#define BYTECODE_ORDER_CHECK_VAL (0x0102030405060708)
typedef struct {
char *data;
size_t len;
BYTECODE_SECTIONS_COUNT sections_count, next_section_num;
char *next_section_ptr;
} BYTECODE_HANDLE;
// --- VM ---------------------------------------
#define MAX_STACK (2048)
#define MAX_FRAMES (1024)
#define MAX_TRIES_PER_FRAME (8)
#define MAX_ARGS (16)
// TODO: range check
// TODO: smaller types for shorter jumps?
typedef uint32_t PATCH_OFFSET;
typedef int32_t JUMP_OFFSET;
typedef size_t IP;
typedef struct {
IP catch_ip;
size_t saved_stack_ptr;
} TRY_INFO;
typedef struct {
// TODO: smarter allocation of locals when can't be captured by clousre,
// probably on stack.
VALUE *locals;
VALUE closure;
const VALUE *arr_callable;
int *arr_callable_idx;
TRY_INFO try_info[MAX_TRIES_PER_FRAME];
int try_info_ptr;
// Enable/disable using method_not_found_handler
int do_call_method_not_found_handler;
int do_call_call;
// For stack trace
IP last_ip;
} FRAME;
// Plan: have exactly one context per thread.
typedef struct context {
VALUE stack[MAX_STACK];
size_t stack_ptr;
FRAME frames[MAX_FRAMES];
size_t frame_ptr;
} CTX;
typedef struct {
uint32_t ip;
uint32_t source_location[4]; // start line+col, end line+col
uint16_t source_file_name_idx;
} source_tracking_entry;
typedef struct {
size_t start_ip, len;
char **files_names;
size_t files_names_allocated, files_names_len;
size_t source_tracking_entries_count;
source_tracking_entry *source_tracking_entries;
} VM_REGION;
struct VM {
char *bytecode;
size_t bytecode_len;
VALUE *globals;
size_t globals_len;
VALUE globals_indexes;
char **globals_names;
VM_REGION *regions;
size_t regions_len, regions_allocated;
VALUE last_doc_hash;
VALUE *type_by_value_tag[MAX_VALUE_TAG_VALUE+1];
VALUE *type_by_t_obj_type_id[MAX_T_OBJ_TYPE_ID+1]; // XXX
VALUE Null;
VALUE Bool;
VALUE Int;
VALUE Real;
VALUE Str;
VALUE Arr;
VALUE Fun;
VALUE UserDefinedMethod;
VALUE NativeMethod;
VALUE MultiMethod;
VALUE Any;
VALUE BasicTypeInstance;
VALUE NormalTypeInstance;
VALUE Seq;
VALUE Type;
VALUE BasicType;
VALUE NormalType;
VALUE Hash;
VALUE Namespace;
VALUE LLHashEntry;
VALUE CLib;
VALUE CSym;
VALUE RegExp;
VALUE NormalTypeConstructor;
// Not sure about naming convention
VALUE c_pthread_t;
VALUE c_pthread_attr_t;
VALUE c_pthread_mutex_t;
VALUE c_pthread_mutexattr_t;
VALUE c_pthread_cond_t;
// TODO: VALUE c_pthread_mutex_tAttr;
VALUE c_ffi_type;
VALUE c_ffi_cif;
VALUE C_DIR;
// *** Add new VALUE MyType above this line ***
VALUE Exception;
VALUE Error;
VALUE InternalError;
VALUE LookupFail;
VALUE KeyNotFound;
VALUE IndexNotFound;
VALUE EmptyArrayFail;
VALUE FieldNotFound;
VALUE GlobalNotFound;
VALUE UndefinedLocalVar;
VALUE UndefinedUpVar;
VALUE InvalidArgument;
VALUE DivisionByZero;
VALUE CompileFail;
VALUE RegExpCompileFail;
VALUE CallFail;
VALUE DontKnowHowToCall;
VALUE MethodNotFound;
VALUE StackDepthFail;
VALUE ArgsMismatch;
VALUE SwitchFail;
VALUE DlopenFail;
VALUE DecodeFail;
VALUE JsonDecodeFail;
VALUE StackOverflow;
VALUE Backtrace;
VALUE CommandsPipeline;
VALUE CommandsPipe;
VALUE Command;
VALUE CommandRedir;
VALUE NumRange;
VALUE Stat;
VALUE c_tm;
VALUE NgsStrComp;
VALUE NgsStrCompImm;
VALUE NgsStrCompExp;
VALUE NgsStrCompSplatExp;
VALUE method_not_found_handler;
VALUE global_not_found_handler;
VALUE init;
VALUE call;
VALUE eqeq;
// awk -F '[ ;]' '$1 == "FFI_EXTERN" {print "VALUE c_" $3 ";"}' /usr/include/x86_64-linux-gnu/ffi.h
VALUE c_ffi_type_void;
VALUE c_ffi_type_uint8;
VALUE c_ffi_type_sint8;
VALUE c_ffi_type_uint16;
VALUE c_ffi_type_sint16;
VALUE c_ffi_type_uint32;
VALUE c_ffi_type_sint32;
VALUE c_ffi_type_uint64;
VALUE c_ffi_type_sint64;
VALUE c_ffi_type_float;
VALUE c_ffi_type_double;
VALUE c_ffi_type_pointer;
VALUE c_ffi_type_longdouble;
VALUE c_ffi_type_complex_float;
VALUE c_ffi_type_complex_double;
VALUE c_ffi_type_complex_longdouble;
VALUE c_ffi_type_string;
// networking
VALUE c_sockaddr;
VALUE c_sockaddr_un;
VALUE c_sockaddr_in;
};
typedef struct {
VM *vm;
VALUE f;
VALUE arg;
} NGS_PTHREAD_INIT_INFO;
enum opcodes {
OP_HALT,
OP_PUSH_NULL,
OP_PUSH_FALSE,
OP_PUSH_TRUE,
OP_PUSH_INT32,
OP_PUSH_INT64,
OP_PUSH_REAL,
OP_PUSH_L8_STR,
OP_PUSH_L32_STR,
OP_DUP,
OP_POP,
OP_XCHG,
OP_RESOLVE_GLOBAL,
OP_PATCH,
OP_FETCH_GLOBAL,
OP_STORE_GLOBAL,
OP_FETCH_LOCAL,
OP_STORE_LOCAL,
OP_CALL,
OP_CALL_EXC,
OP_CALL_ARR,
OP_RET,
OP_JMP,
OP_JMP_TRUE,
OP_JMP_FALSE,
OP_MAKE_ARR,
OP_MAKE_CLOSURE,
OP_TO_STR,
OP_MAKE_STR,
OP_MAKE_STR_IMM,
OP_MAKE_STR_EXP,
OP_MAKE_STR_SPLAT_EXP,
OP_PUSH_EMPTY_STR,
OP_DEF_GLOBAL_FUNC,
OP_DEF_LOCAL_FUNC,
OP_FETCH_UPVAR,
OP_STORE_UPVAR,
OP_DEF_UPVAR_FUNC,
OP_MAKE_HASH,
OP_TO_BOOL,
OP_TO_ARR,
OP_TO_HASH,
OP_ARR_APPEND,
OP_ARR_APPEND2,
OP_ARR_CONCAT,
OP_GUARD,
OP_TRY_START,
OP_TRY_END,
OP_ARR_REVERSE,
OP_THROW,
OP_MAKE_CMDS_PIPELINE,
OP_MAKE_CMDS_PIPE,
OP_MAKE_CMD,
OP_SET_CLOSURE_NAME,
OP_SET_CLOSURE_DOC,
OP_SET_CLOSURE_NS,
OP_HASH_SET,
OP_HASH_UPDATE,
OP_PUSH_KWARGS_MARKER,
OP_MAKE_REDIR,
OP_SUPER,
OP_MAKE_MULTIMETHOD,
OP_MULTIMETHOD_APPEND,
OP_MULTIMETHOD_REVERSE,
NUMBER_OF_OPCODES,
};
extern char *opcodes_names[NUMBER_OF_OPCODES];
enum range_field {
RANGE_FIELD_START = 0U,
RANGE_FIELD_END = 1U,
RANGE_FIELD_INCLUDE_START = 2U,
RANGE_FIELD_INCLUDE_END = 3U,
RANGE_FIELD_STEP = 4U,
};
typedef METHOD_RESULT (*VM_FUNC)(const VALUE *argv, VALUE *result);
typedef METHOD_RESULT (*VM_EXT_FUNC)(VM *vm, CTX *ctx, const VALUE *argv, VALUE *result);
void vm_init(VM *vm, int argc, char **argv);
size_t vm_load_bytecode(VM *vm, char *bc);
void ctx_init(CTX *ctx);
GLOBAL_VAR_INDEX check_global_index(VM *vm, const char *name, size_t name_len, int *found);
GLOBAL_VAR_INDEX get_global_index(VM *vm, const char *name, size_t name_len);
// typedef int VM_INT;
void set_global(VM *vm, const char *name, VALUE v);
METHOD_RESULT vm_run(VM *vm, CTX *ctx, IP ip, VALUE *result);
METHOD_RESULT vm_call(VM *vm, CTX *ctx, VALUE *result, const VALUE callable, int argc, const VALUE *argv);
BYTECODE_HANDLE *ngs_create_bytecode();
void ngs_add_bytecode_section(BYTECODE_HANDLE *h, BYTECODE_SECTION_TYPE type, BYTECODE_SECTION_LEN len, char *data);
BYTECODE_HANDLE *ngs_start_unserializing_bytecode(char *data);
void ngs_fetch_bytecode_section(BYTECODE_HANDLE *h, BYTECODE_SECTION_TYPE *type, BYTECODE_SECTION_LEN *len, char **data);
// In obj.c
VALUE make_backtrace(VM *vm, CTX *ctx);
VALUE resolve_instruction_pointer(VM *vm, IP ip);
VALUE value_type(VM *vm, VALUE val);
int obj_is_of_type(VM *vm, VALUE obj, VALUE t);
#endif