Newer
Older
#include <stdio.h>
#include <inttypes.h>
#include <assert.h>
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
#define OPERAND_STACK_INIT_CAPACITY 1024
void ostack_init ( OperandStack * stack ) {
stack -> elements = (Value**) malloc ( OPERAND_STACK_INIT_CAPACITY * sizeof (Value*) );
stack -> size = 0;
stack -> capacity = OPERAND_STACK_INIT_CAPACITY;
}
void ostack_pop ( OperandStack * stack ) {
assert ( stack -> size );
stack -> size--;
}
Value * ostack_top ( OperandStack * stack ) {
return stack -> elements [ stack -> size - 1 ];
}
void ostack_push ( OperandStack * stack , Value * value ) {
if ( stack -> size == stack -> capacity ) {
Value ** tmp = (Value**) malloc ( 2 * stack -> capacity * sizeof (Value*) );
memcpy ( tmp, stack -> elements, stack -> size * sizeof (Value*) );
free ( stack -> elements );
stack -> elements = tmp;
}
stack -> elements [ stack -> size++ ] = value;
}
void ostack_destroy ( OperandStack * stack ) {
free ( stack -> elements );
}
void vm_init ( VM * vm, size_t heap_size ) {
heap_init ( & vm -> heap, heap_size );
make_null ( & vm -> heap );
ostack_init ( & vm -> ostack );
}
void vm_destroy ( VM * vm ) {
heap_destroy ( & vm -> heap );
ostack_destroy ( & vm -> ostack );
}
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
// void * heap_alloc_alligned ( Heap * heap, size_t len, size_t align ) {
// size_t pos = (size_t) heap -> next;
// size_t rem = pos % align;
// if ( rem )
// heap -> next = heap -> next + align - rem;
// if ( heap -> next + len >= heap -> end )
// return NULL;
// void * ret = heap -> next;
// heap -> next += len;
// return ret;
// }
//
// void * heap_alloc ( Heap * heap, size_t len ) {
// size_t pos = (size_t) heap -> next;
// size_t rem = pos % 8;
// if ( rem )
// heap -> next = heap -> next + 8 - rem;
// if ( heap -> next + len >= heap -> end )
// return NULL;
// void * ret = heap -> next;
// heap -> next += len;
// return ret;
// }
//
// void heap_init ( Heap * heap, size_t heap_size ) {
// heap -> begin = (u8*) malloc ( heap_size );
// heap -> next = heap -> begin;
// heap -> end = heap -> begin + heap_size;
// }
//
// void heap_destroy ( Heap * heap ) {
// free ( heap -> begin );
// }
//Value * make_null ( ASTInterpreterState * state ) {
// NullValue * value = heap_alloc ( state -> heap, sizeof (NullValue) );
// *value = (NullValue) { .kind = (Value) { .kind = VALUE_NULL } };
// return (Value*) value;
//}
//
//Value * make_int ( ASTInterpreterState * state, i32 val ) {
// IntValue * value = heap_alloc ( state -> heap, sizeof (IntValue) );
// *value = (IntValue) { .kind = (Value) { .kind = VALUE_INTEGER }, .integer = val };
// return (Value*) value;
//}
//
//Value * make_bool ( ASTInterpreterState * state, bool val ) {
// BoolValue * value = heap_alloc ( state -> heap, sizeof (BoolValue) );
// *value = (BoolValue) { .kind = (Value) { .kind = VALUE_BOOLEAN }, .boolean = val };
// return (Value*) value;
//}
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
Value * alloc_constant ( VM * vm, u16 index ) {
assert ( vm -> program . constant_count > index );
Constant constant = vm -> program . constant_pool [ index ];
switch ( constant . kind ) {
case CONSTANT_INTEGER:
return make_int ( & vm -> heap, constant . integer );
case CONSTANT_NULL:
return make_null ( & vm -> heap );
case CONSTANT_BOOLEAN:
return make_bool ( & vm -> heap, constant . boolean );
default:
assert ( false );
}
return NULL;
}
void print_operand_value ( Value * value ) {
switch ( value -> kind ) {
case VALUE_INTEGER:
printf ( "%" PRIi32, ((IntValue*) value ) -> integer );
break;
case VALUE_BOOLEAN:
if ( ((BoolValue*) value ) -> boolean )
printf ( "true" );
else
printf ( "false" );
break;
case VALUE_NULL:
printf ( "null" );
break;
case VALUE_FUNCTION:
printf ( "function" );
break;
/*case VALUE_ARRAY:
putchar ( '[' );
ArrayValue * arr = (ArrayValue*) value;
for ( i32 i = 0; i < arr -> length; ++i ) {
if ( i != 0 )
printf ( ", " );
print_operand_value ( arr -> elements [ i ] );
}
putchar ( ']' );
break;
case VALUE_OBJECT:
printf ( "object(");
ObjectValue * obj = (ObjectValue*) value;
bool parent = false;
if ( obj -> extends -> kind != VALUE_NULL ) {
parent = true;
printf ( "..=" );
print_operand_value ( obj -> extends );
}
if ( obj -> member_cnt )
qsort ( obj -> members, obj -> member_cnt, sizeof (SimpleEntry), compare_entry );
for ( size_t i = 0; i < obj -> member_cnt; ++i ) {
if ( i != 0 || parent )
printf ( ", " );
printf ( "%.*s=", (int) obj -> members [ i ] . name . len, obj -> members [ i ] . name . str );
print_operand_value ( obj -> members [ i ] . value );
}
putchar ( ')' );
break;*/
default:
assert ( false );
}
}
void print_call ( VM * vm, u16 index, u8 arguments ) {
assert ( vm -> program . constant_count > index );
Constant constant = vm -> program . constant_pool [ index ];
assert ( constant . kind == CONSTANT_STRING );
assert ( arguments <= vm -> ostack . size );
Value ** args = (Value**) malloc ( arguments * sizeof (Value*) );
for ( size_t i = arguments; i != 0; --i ) {
args [ i - 1 ] = ostack_top ( & vm -> ostack );
ostack_pop ( & vm -> ostack );
}
size_t arg = 0;
Str format = constant . string;
u8 c;
for ( size_t i = 0; i < format . len; ++i ) {
c = format . str [ i ];
if ( c == '\\' ) {
++i;
c = format . str [ i ];
switch ( c ) {
case '~':
putchar ( '~' );
break;
case 'n':
putchar ( '\n' );
break;
case '"':
putchar ( '"' );
break;
case 'r':
putchar ( '\r' );
break;
case 't':
putchar ( '\t' );
break;
case '\\':
putchar ( '\\' );
break;
default:
assert ( false );
}
} else if ( c == '~' ) {
assert ( arg != arguments );
print_operand_value ( args [ arg++ ] );
} else
putchar ( c );
}
assert ( arg == arguments );
}
static inline u8 read_byte ( Str str, size_t * pos ) {
return str . str [ (*pos)++ ];
}
static inline u16 read_u16 ( Str str, size_t * pos ) {
u8 bytes [ 2 ] = { read_byte ( str, pos ), read_byte ( str, pos ) };
return (uint16_t) (255 & bytes [ 1 ]) << 8 | (255 & bytes [ 0 ]);
}
static inline i32 read_i32 ( Str str, size_t * pos ) {
u8 bytes [4];
for ( size_t i = 0; i < 4; ++i )
bytes [ i ] = read_byte ( str, pos );
return bytes [ 3 ] << 24 | bytes [ 2 ] << 16 | bytes [ 1 ] << 8 | bytes [ 0 ];
}
void read_header ( Str input, size_t * pos ) {
u8 bytes [4];
bytes [ i ] = read_byte ( input, pos );
Str header;
header . str = bytes;
header . len = 4;
assert ( str_eq ( header, STR ( "FML\n" ) ) );
}
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
void read_constant_pool ( Program * program, Str input, size_t * pos ) {
// printf ( "%u, %u\n", count_bytes [ 0 ], count_bytes [ 1 ] );
program -> constant_count = read_u16 ( input, pos );
// printf ( "%x, %x\n", count_bytes [ 0 ], count_bytes [ 1 ] );
u8 tag;
//printf ( "size is %u\n", program -> constant_count );
Constant * constant_pool = (Constant*) malloc ( program -> constant_count * sizeof (Constant) );
for ( size_t i = 0; i < program -> constant_count; ++i ) {
tag = read_byte ( input, pos );
switch ( tag ) {
case 0x00:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_INTEGER, .integer = read_i32 ( input, pos ) };
// printf ( "%d\n", constant_pool [ i ] . integer );
break;
case 0x01:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_NULL };
// printf ( "null\n" );
break;
case 0x02: {
u32 len = (u32) read_i32 ( input, pos );
const u8 * start = input . str + *pos;
*pos += len;
constant_pool [ i ] = (Constant) { .kind = CONSTANT_STRING, .string = (Str) { .len = len, .str = start } };
// printf ( "%.*s\n", (int) constant_pool [ i ] . string . len, constant_pool [ i ] . string . str );
break;
}
case 0x03: {
u8 parameters = read_byte ( input, pos );
u16 locals = read_u16 ( input, pos );
u32 len = (u32) read_i32 ( input, pos );
const u8 * start = input . str + *pos;
*pos += len;
constant_pool [ i ] = (Constant) { .kind = CONSTANT_FUNCTION, .function = (ConstantFunction) { .parameters = parameters, .locals = locals, .len = len, .start = start } };
// printf ( "func: p=%u, l=%u, len=%u, start=%lu\n", parameters, locals, len, *pos - len );
break;
}
case 0x04:
constant_pool [ i ] = (Constant) { .kind = CONSTANT_BOOLEAN, .boolean = read_byte ( input, pos ) };
// printf ( "%s\n", constant_pool [ i ] . boolean == true ? "true" : "false" );
break;
default:
// printf ( "unsupported type\n" );
break;
}
}
program -> constant_pool = constant_pool;
}
void read_globals ( Str input, size_t * pos ) {
u16 elements = read_u16 ( input, pos );
*pos += elements;
void parse_program ( Program * program, Str input ) {
// read + check header
size_t pos = 0;
read_header ( input, &pos );
// read constant pool
read_constant_pool ( program, input, &pos );
read_globals ( input, &pos );
program -> entry_point = read_u16 ( input, &pos );
//printf ("EP: %u\n", program -> entry_point );
}
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
int bc_interpret ( VM * vm, Str input ) {
parse_program ( & vm -> program, input );
Constant main = vm -> program . constant_pool [ vm -> program . entry_point ];
assert ( main . kind == CONSTANT_FUNCTION );
ConstantFunction f = main . function;
u8 opcode;
size_t addr = f . start - input . str;
ostack_push ( & vm -> ostack, (Value*) vm -> heap . begin );
for ( size_t pos = addr; pos < addr + f . len; ) {
opcode = read_byte ( input, &pos );
switch ( opcode ) {
case 0x00:
//printf ("drop\n");
ostack_pop ( & vm -> ostack );
break;
case 0x01: {
u16 index = read_u16 ( input, &pos );
//printf ("constant %u\n", index);
ostack_push ( & vm -> ostack, alloc_constant ( vm, index ) );
break;
}
case 0x02: {
//printf ("print\n");
u16 index = read_u16 ( input, &pos );
u8 arguments = read_byte ( input, &pos );
print_call ( vm, index, arguments );
ostack_push ( & vm -> ostack, (Value*) vm -> heap . begin );
break;
}
case 0x0F:
//printf ("Job well done!\n");
break;
default:
//printf ( "%d\n", opcode );
assert ( false );
}
}
return 0;