-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.jai
More file actions
582 lines (457 loc) · 25.7 KB
/
execute.jai
File metadata and controls
582 lines (457 loc) · 25.7 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
TODO: we should differentiate execute and evaluate procs
the procs here should be labelled execute, since they work primarily through side effects
i.e., they push/pop things on the preallocated stack and rely on typecheck phase to have been done correctly
by contrast, I will probably reintroduce some evaluate procs that basically do things the old way of dynamically typechecking and returning Any's
these may be useful in the case of scripts that only need to be run once, e.g. evaluating expresions in gon files or seomthing
as it stands, I think the execute procs will probably have decent enough performance characteristics that going to a bytecode will probabyl not be worth it for a good while
it will be much easier to debug things using the old ast walking method, and because we are typechecked, it's really more like a fat ass bytecode with horrible cache misses
but we would still have bad cache misses in bytecode with procedure calls anyhow because of the type info navigation that's required
*/
execute_script :: (script: *Script) -> bool {
if !(script.flags & .PARSED) {
set_execution_error(script, "Cannot execute script before AST has been constructed.\n", node = null);
return false;
}
if !(script.flags & .TYPECHECKED) {
set_execution_error(script, "Cannot execute script before AST has been typechecked.\n", node = null);
return false;
}
script.stack_ptr = script.stack_base;
statements := script.ast_root.statements;
for statements {
dprint("executing statement #%\n", it_index);
if !execute_node(script, it) return false;
}
assert(script.stack_ptr == script.stack_base);
return true;
}
/*
Even though the script has already been typechecked, I use stack_push_any and stack_pop_any
so that it's easier to get the type info when needed for debugging.
Using these procedures also just makes the code here a bit easier to read and captures the
type-specific rules about additional indirections automatically.
When moving to bytecode though, we should be just use stack_push and stack_pop,
which are probably slightly faster and don't require passing type info.
The explicit_call param is now required for signalling that we are indeed wanting to run a named block.
Maybe this is sort of a back hack though...
*/
execute_node :: (script: *Script, node: *Node, explicit_call := false) -> bool {
dprint("executing node %: %\n", node, node.node_type);
dprint_push_indent();
dprint("stack_pointer offset: % (%)\n", script.stack_ptr - script.stack_base, script.stack_ptr);
if node.flags & .IS_LVALUE dprint("expecting lvalue\n");
if !node.value_type {
dprint("Warning: node has no set value type!\n");
}
if !node.flags & .TYPECHECKED {
set_execution_error(script, "Cannot execute a node that has not been typechecked.", node = node);
return false;
}
if node.node_type == {
case Node_Directive;
directive := node.(*Node_Directive);
if directive.runtime_node {
return execute_node(script, directive.runtime_node);
}
return true;
case Node_Literal;
literal := node.(*Node_Literal);
if literal.flags & .IS_LVALUE {
assert(literal.flags & .IS_MALLEABLE == .IS_MALLEABLE || literal.literal_type == .ANY);
}
if #complete literal.literal_type == {
case .ANY;
if !stack_push_any(script, literal.any, literal.flags & .IS_LVALUE != 0) return false;
dprint("returning literal with value %\n", literal.any);
return true;
case .STRING;
if !stack_push_any(script, literal.text, literal.flags & .IS_LVALUE != 0) return false;
dprint("returning literal with value %\n", literal.text);
return true;
case .BOOLEAN; #through;
case .NUMBER;
if !stack_push_any(script, to_any(*literal.number), literal.flags & .IS_LVALUE != 0) return false;
dprint("returning literal with value %\n", to_any(*literal.number));
return true;
case .STRUCT;
if !stack_push_any(script, Any.{ literal.value_type, literal.aggr.value_pointer }, literal.flags & .IS_LVALUE != 0) return false;
write_ptr := literal.aggr.value_pointer;
// TODO: if we enhance typechecking to pass value pointer for these values,
// then we don't need to do any pushing or popping from stack here.
// will require changes to almost all execute cases though...
// if node.dst_provided memcpy directly else push stack
for literal.aggr.expressions {
if !execute_node(script, it) return false;
memcpy(write_ptr, stack_pop_any(script, it.value_type).value_pointer, it.value_type.runtime_size);
write_ptr += it.value_type.runtime_size;
}
return true;
}
return false;
case Node_Identifier;
identifier := node.(*Node_Identifier);
if identifier.identifier_type == {
case .DECLARATION;
declaration := identifier.declaration;
if declaration.flags & .MACRO {
// return execute_node(script, declaration.init_expression);
}
variable_any := Any.{ declaration.value_type, declaration.value_pointer };
dprint("returning internal variable with value % at %\n", variable_any, variable_any.value_pointer);
if !stack_push_any(script, variable_any, identifier.flags & .IS_LVALUE != 0) return false;
return true;
case .EXTERNAL_VARIABLE;
variable := *script.variables[identifier.index];
if variable.value_pointer == null {
set_execution_error(script, "Unable to get value of external variable '%'.", variable.name, node = node);
return false;
}
value := get_value(variable);
if variable.type.runtime_size > 8
then dprint("returning external variable at %\n", variable.value_pointer);
else dprint("returning external variable with value % at %\n", value, variable.value_pointer);
if !stack_push_any(script, value, identifier.flags & .IS_LVALUE != 0) return false;
return true;
case .EXTERNAL_PROCEDURE;
procedure := *script.procedures[identifier.index];
dprint("returning external procedure with type % at %\n", as_type(procedure.proc_info), procedure.pointer);
assert(identifier.flags & .IS_LVALUE == 0);
if !stack_push_any(script, procedure.pointer) return false;
return true;
case .LITERAL;
return execute_node(script, identifier.literal);
case;
set_execution_error(script, "Unhandled case in execute_node for identifier type: %", identifier.identifier_type, node = node);
return false;
}
return false;
case Node_Operation;
operation := node.(*Node_Operation);
if operation.flags & .OVERLOAD {
return execute_node(script, operation.overload_procedure);
}
_operator := get_operator(script, operation);
if _operator.kind != .ASSIGNMENT {
if is_aggr(operation.value_type) {
if !stack_push_any(script, Any.{ operation.value_type, operation.return_ptr }) return false;
} else {
if !stack_push_zeroes(script, 8) return false;
}
}
if !execute_node(script, operation.left) return false;
if !is_unary(_operator.kind) {
if !execute_node(script, operation.right) return false;
}
// special case: operator = is always a simple memcpy
if operation.name == "=" {
assert(operation.left.flags & .IS_LVALUE != 0);
r_value := stack_pop_any(script, operation.right.value_type);
l_value := stack_pop_any(script, operation.left.value_type, true);
memcpy(l_value.value_pointer, r_value.value_pointer, r_value.type.runtime_size);
return true;
}
// TODO: maybe use the simpler version of execute_builtin_operation, but with this more dynamic stack popping
// this is probably a little bit slower, but it may be worth it to reduce code bloat
// the other benefit(?) of going this way is that we could do the swap args swizzle here, rather than swap them at the ast level
// op := BUILTIN_OPERATIONS[operation.operator_index];
// right := null;
// if !is_unary right = stack_pop (script, is_aggr(op.right));
// left := stack_pop (script, is_aggr(op.left ));
// ret := stack_peek(script, is_aggr(op.ret ));
// if !execute_builtin_operation(operation.operator_index, left, right, ret) return false;
// dprint("operation: %\n", op);
execute_builtin_operation(script, operation.builtin_operation_index);
result_any := Any.{
operation.value_type,
ifx is_aggr(operation.value_type) then operation.return_ptr else script.stack_ptr
};
dprint("returning result of binary operation: % %\n", as_type(operation.value_type), result_any);
return true;
case Node_Procedure_Call;
procedure_call := node.(*Node_Procedure_Call);
// TODO: this will have to be refactored later for bytecode (to push result ptr after popping args)
if !execute_node(script, procedure_call.procedure_expression) return false;
proc_info := procedure_call.procedure_expression.value_type.(*Type_Info_Procedure);
assert(proc_info.type == .PROCEDURE);
dprint("proc_info: %\n", as_type(proc_info));
procedure := stack_pop_any(script, proc_info); // TODO: really no need to get an any here. but we need to fix the older stack_pop proc
// we have to grab the actual procedure pointer value now before overwriting with another stack push below
proc_ptr := procedure.value_pointer.(**void).*;
return_ptr := script.stack_ptr;
if is_aggr(procedure_call.value_type) {
if !stack_push_any(script, Any.{ procedure_call.value_type, procedure_call.return_ptr }) return false;
return_ptr = procedure_call.return_ptr;
} else {
stack_push_zeroes(script, 8);
}
arg_ptr_start := script.stack_ptr;
for procedure_call.arguments if !execute_node(script, it) return false;
arguments := NewArray(proc_info.argument_types.count, Any,, temp);
arg_ptr := arg_ptr_start;
for proc_info.argument_types {
value_pointer := arg_ptr;
if is_aggr(it) value_pointer = value_pointer.(**void).*;
arguments[it_index] = Any.{ it, value_pointer };
arg_ptr += size_of(*void);
}
result := Any.{ procedure_call.value_type, return_ptr };
result_array: [] Any;
if result.type != xx void then result_array = .[ result ];
#if USING_DYNCALL {
if !do_dyncall(script.dyncall_vm, Any.{ proc_info, *proc_ptr }, arguments, result_array) {
set_general_error(script, "Failed while trying to make dyncall.");
return false;
}
} else {
if !try_calling_procedure_with_wrapper(proc_info, proc_ptr, arguments, result_array) {
log("Unable to find wrapper for procedure of type: %", as_type(proc_info));
return false;
}
}
script.stack_ptr = arg_ptr_start;
dprint("returning result of procedure call: %\n", Any.{ procedure_call.value_type, return_ptr });
if procedure_call.flags & .DISCARD_VALUE {
stack_pop_any(script, procedure_call.value_type);
}
return true;
case Node_Cast;
node_cast := node.(*Node_Cast);
// TODO: right now this assumes we will only ever do casts between trivial types (we should actually check this!)
dst := Any.{ node.value_type, script.stack_ptr };
if !stack_push_zeroes(script, 8) return false;
if !execute_node(script, node_cast.value) return false;
src := stack_pop_any(script, node_cast.value.value_type);
if !Convert.any_to_any(dst, src) return false;
return true;
case Node_Dot;
dot := node.(*Node_Dot);
if dot.flags & .IS_ARROW {
// right_ident := dot.right.(*Node_Identifier);
// declaration := right_ident.declaration;
// variable_any := Any.{ right_ident.value_type, declaration.value_pointer };
// if !stack_push_any(script, variable_any, (dot.flags & .IS_LVALUE) == .IS_LVALUE) return false;
// return true;
return execute_node(script, dot.right);
}
if dot.left == null {
assert(node.value_type.type == .ENUM || node.value_type.type == .STRUCT);
return execute_node(script, dot.right);
}
left_type := dot.left.value_type;
if left_type.type == {
case .POINTER;
pointer_info := left_type.(*Type_Info_Pointer);
assert(pointer_info.pointer_to.type == .STRUCT);
// override left type for struct case so that we we pop the correct type
dprint("override left_type: % -> %\n", as_type(left_type), as_type(pointer_info.pointer_to));
left_type = pointer_info.pointer_to;
if !execute_node(script, dot.left) return false;
struct_any, did_dereference := dereference_any_pointer(stack_pop_any(script, pointer_info, false));
assert(did_dereference);
member_any := Any.{ dot.value_type, struct_any.value_pointer + dot.right.(*Node_Identifier).member.offset_in_bytes };
dprint("struct value ptr: %\n", struct_any.value_pointer);
dprint("member value ptr: %\n", member_any.value_pointer);
if !stack_push_any(script, member_any, (dot.flags & .IS_LVALUE) == .IS_LVALUE) return false;
return true;
case .STRUCT;
if !execute_node(script, dot.left) return false;
struct_any := stack_pop_any(script, left_type, true);
member_any := Any.{ dot.value_type, struct_any.value_pointer + dot.right.(*Node_Identifier).member.offset_in_bytes };
dprint("struct value ptr: %\n", struct_any.value_pointer);
dprint("member value ptr: %\n", member_any.value_pointer);
if !stack_push_any(script, member_any, (dot.flags & .IS_LVALUE) == .IS_LVALUE) return false;
return true;
case .TYPE;
// type expression on left side is evaluated during typechecking
return execute_node(script, dot.right);
}
assert(false);
case Node_Subscript;
subscript := node.(*Node_Subscript);
if !execute_node(script, subscript.base_expression) return false;
if !execute_node(script, subscript.indexing_expression) return false;
index_any := stack_pop_any(script, subscript.indexing_expression.value_type);
base_any := stack_pop_any(script, subscript.base_expression.value_type, true);
ti_array := subscript.base_expression.value_type.(*Type_Info_Array);
indexing_type := subscript.indexing_expression.value_type;
// get index as s64
// TODO: maybe improve this
// for bytecode, could do an instruction for inplace type cast
index_as_s64: s64;
assert(Convert.any_to_int(index_as_s64, index_any));
array_count, array_data := get_array_count_and_data(base_any.value_pointer, xx base_any.type);
if index_as_s64 < 0 || index_as_s64 >= array_count {
set_execution_error(script, "Array index % was out of bounds! Max index is %.", index_as_s64, array_count-1, node = node);
return false;
}
element := Any.{ node.value_type, array_data + index_as_s64 * node.value_type.runtime_size };
if !stack_push_any(script, element, node.flags & .IS_LVALUE == .IS_LVALUE) return false;
// dprint("returning result of indexing operation: %\n", element);
if element.type.runtime_size > 8
then dprint("returning result of indexing operation at %\n", element.value_pointer);
else dprint("returning result of indexing operation with value % at %\n", element, element.value_pointer);
return true;
case Node_Declaration;
declaration := node.(*Node_Declaration);
// early return on macro for now so that we don't overwrite value of malleable literal
// the initial value is evaluated in typechecking phase, which is fine since for now we only allow simple literals in a macro decl
if declaration.flags & .MACRO return true;
/*
When executng a virtual member declaration, we have to use the get_virtual_member callback to get the actual Any to the virtual member.
We then assert that the type returned matches what was determined during typechecking, and overwrite the declaration's value pointer.
The virtual member lookup only needs to occur when the declaration is executed, not at each location where the virtual member is used as an rvalue.
*/
if is_virtual_member_declaration(declaration) {
if script.get_virtual_member == null {
set_execution_error(script, "Script's get_virtual_member procedure was not provided by user!", node = node);
return false;
}
arrow := declaration.left.(*Node_Dot);
if !execute_node(script, arrow.left) return false;
owner := stack_pop_any(script, arrow.left.value_type, true);
// log("owner: % (%)", owner, as_type(owner.type));
if is_on_stack(script, owner.value_pointer) {
log("owner is on stack");
}
member_identifier := get_identifier_name(script, xx arrow.right);
ok, member_value := script.get_virtual_member(script, owner, member_identifier, declaration.value_type);
if !ok {
set_execution_error(script, "get_virtual_member failed with owning type: %", as_type(owner.type), node = node);
return false;
}
if member_value.type != declaration.value_type {
set_execution_error(script, "get_virtual_member returned unexpected type: %. The expected type was: %", as_type(member_value.type), as_type(declaration.value_type), node = node);
return false;
}
declaration.value_pointer = member_value.value_pointer;
}
l_value := Any.{ declaration.value_type, declaration.value_pointer };
if declaration.init_expression {
if !execute_node(script, declaration.init_expression) return false;
r_value := stack_pop_any(script, declaration.init_expression.value_type);
dprint("l_value.value_pointer: %\n", l_value.value_pointer);
dprint("r_value.value_pointer: %\n", r_value.value_pointer);
dprint("l_value: % % at %\n", as_type(l_value.type), l_value, l_value.value_pointer);
dprint("r_value: % % at %\n", as_type(r_value.type), r_value, r_value.value_pointer);
memcpy(l_value.value_pointer, r_value.value_pointer, r_value.type.runtime_size);
} else if !is_virtual_member_declaration(declaration) {
memset(l_value.value_pointer, 0, l_value.type.runtime_size);
}
return true;
case Node_Block;
block := node.(*Node_Block);
if block.name && !explicit_call return true; // we don't execute named blocks unless explicitly called
for block.statements {
dprint("executing statement #%\n", it_index);
if !execute_node(script, it) return false;
}
return true;
case Node_If_Statement;
if_statement := node.(*Node_If_Statement);
if !execute_node(script, if_statement.condition) {
dprint("failed to evaluate left side of assignment statement!\n");
return false;
}
condition := stack_pop_any(script, if_statement.condition.value_type);
dprint("condition: % %\n", as_type(condition.type), condition);
condition_as_bool: bool;
Convert.any_to_bool(condition_as_bool, condition);
if condition_as_bool {
if !execute_node(script, if_statement.statement) {
dprint("failed to evaluate left side of assignment statement!\n");
return false;
}
}
return true;
case Node_While_Loop;
while_loop := node.(*Node_While_Loop);
while loop := true {
if !execute_node(script, while_loop.condition) {
dprint("failed to evaluate left side of assignment statement!\n");
return false;
}
condition := stack_pop_any(script, while_loop.condition.value_type);
dprint("condition: % %\n", as_type(condition.type), condition);
condition_as_bool: bool;
Convert.any_to_bool(condition_as_bool, condition);
if !condition_as_bool break loop;
if !execute_node(script, while_loop.statement) {
dprint("failed to evaluate left side of assignment statement!\n");
return false;
}
}
return true;
case Node_For_Loop;
for_loop := node.(*Node_For_Loop);
if for_loop.control_type == {
case .ARRAY;
if !execute_node(script, for_loop.array_expression) {
dprint("failed to evaluate for loop control node!\n");
return false;
}
ti_array := for_loop.array_expression.value_type.(*Type_Info_Array);
array := stack_pop_any(script, ti_array, true); // TODO: maybe not use lvalue later?
array_count, array_data := get_array_count_and_data(array.value_pointer, xx array.type);
// these will be accessed by iterator and iterator_index nodes during iteration
it_index: int;
for_loop.it_decl.value_pointer = array_data;
for_loop.it_index_decl.value_pointer = *it_index;
while it_index < array_count {
defer {
for_loop.it_decl.value_pointer += ti_array.element_type.runtime_size;
it_index += 1;
}
if !execute_node(script, for_loop.statement) {
dprint("failed to evaluate for loop statement!\n");
return false;
}
}
return true;
case .RANGE;
if !execute_node(script, for_loop.range.lower) {
dprint("failed to evaluate for_loop.range.lower!\n");
return false;
}
if !execute_node(script, for_loop.range.upper) {
dprint("failed to evaluate for_loop.range.upper!\n");
return false;
}
control_type := for_loop.range.lower.value_type;
upper := stack_pop_any(script, control_type);
lower := stack_pop_any(script, control_type);
upper_as_s64: int;
lower_as_s64: int;
Convert.any_to_int(upper_as_s64, upper);
Convert.any_to_int(lower_as_s64, lower);
for lower_as_s64..upper_as_s64 {
_it: u64 = 0; // just need 8 bytes of zeros. we will just use this space as dst for dynamically typed int value for iterator
Convert.any_to_any(Any.{ control_type, *_it }, it);
for_loop.it_decl.value_pointer = *_it;
// TODO: for now we just don't set it_index.
// we should probably prevent using it_index for range-based loops in typecheck
if !execute_node(script, for_loop.statement) {
dprint("failed to evaluate for loop statement!\n");
return false;
}
}
return true;
case .LIST;
_it_index: int;
for_loop.it_index_decl.value_pointer = *_it_index;
for for_loop.list {
_it_index = it_index;
if !execute_node(script, it) return false;
for_loop.it_decl.value_pointer = stack_pop_any(script, for_loop.it_decl.value_type, true).value_pointer;
if !execute_node(script, for_loop.statement) {
dprint("failed to evaluate for loop statement!\n");
return false;
}
}
return true;
}
return false;
}
assert(false);
return false;
}