-
Notifications
You must be signed in to change notification settings - Fork 41
/
compile.c
1333 lines (1249 loc) · 48.9 KB
/
compile.c
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <memory.h>
#include "ngs.h"
#include "ast.h"
#include "compile.h"
#include "obj.h"
// TODO: disabllow break/continue outside of loops
// TODO: abstract UINT16
//
// TODO: Use exceptions instead of asserts
#define OPCODE(buf, x) { (buf)[*idx]=x; (*idx)++; }
#define L8_STR(buf, x) { int l = strlen(x); assert(l<256); OPCODE(buf, l); memcpy((buf)+(*idx), x, l); (*idx) += l; }
#define DATA(buf, x) { memcpy((buf)+(*idx), &(x), sizeof(x)); (*idx) += sizeof(x); }
#define DATA_INT32(buf, x) { *(int32_t *)&(buf)[*idx] = x; (*idx)+=sizeof(int32_t); }
#define DATA_INT64(buf, x) { *(int64_t *)&(buf)[*idx] = x; (*idx)+=sizeof(int64_t); }
#define DATA_UINT16(buf, x) { *(uint16_t *)&(buf)[*idx] = x; (*idx)+=sizeof(uint16_t); }
#define DATA_UINT32(buf, x) { *(uint32_t *)&(buf)[*idx] = x; (*idx)+=sizeof(uint32_t); }
#define DATA_JUMP_OFFSET(buf, x) { *(JUMP_OFFSET *)&(buf)[*idx] = x; (*idx)+=sizeof(JUMP_OFFSET); }
#define DATA_JUMP_OFFSET_PLACEHOLDER(buf) DATA_JUMP_OFFSET(buf, 1024)
#define DATA_PATCH_OFFSET(buf, x) { *(PATCH_OFFSET *)&(buf)[*idx] = x; (*idx)+=sizeof(PATCH_OFFSET); }
#define DATA_N_LOCAL_VARS(buf, x) { *(LOCAL_VAR_INDEX *)&(buf)[*idx] = x; (*idx)+=sizeof(LOCAL_VAR_INDEX); }
#define DATA_N_GLOBAL_VARS(buf, x) { *(GLOBAL_VAR_INDEX *)&(buf)[*idx] = x; (*idx)+=sizeof(GLOBAL_VAR_INDEX); }
#define DATA_N_UPVAR_INDEX(buf, x) { *(UPVAR_INDEX *)&(buf)[*idx] = x; (*idx)+=sizeof(UPVAR_INDEX); }
// Symbol table:
// symbol -> [offset1, offset2, ..., offsetN]
#define POP_IF_DONT_NEED_RESULT(buf) if(!need_result) OPCODE(buf, OP_POP);
#define DUP_IF_NEED_RESULT(buf) if(need_result) OPCODE(buf, OP_DUP);
#define DONT_NEED_RESULT (0)
#define NEED_RESULT (1)
#define IF_NOT_SWITCH_COND if(node->number != SWITCH_NODE_COND && node->number != SWITCH_NODE_ECOND)
#define LOCALS (ctx->locals[ctx->locals_ptr-1])
#define IDENTIFIERS_SCOPES (ctx->identifiers_scopes[ctx->locals_ptr-1])
#define N_LOCALS (ctx->n_locals[ctx->locals_ptr-1])
#define N_UPLEVELS (ctx->n_uplevels[ctx->locals_ptr-1])
#define NS (ctx->ns[ctx->locals_ptr])
#define STACK_DEPTH (ctx->stack_depth[ctx->locals_ptr])
// #define IN_FUNCTION (ctx->locals_ptr)
#define BREAK_ADDR (ctx->fill_in_break_addrs[ctx->fill_in_break_addrs_ptr-1])
#define CONTINUE_ADDR (ctx->fill_in_continue_addrs[ctx->fill_in_continue_addrs_ptr-1])
#define SETUP_ADDRESS_FILLING() \
old_break_addrs_ptr = ctx->fill_in_break_addrs_ptr; \
old_continue_addrs_ptr = ctx->fill_in_continue_addrs_ptr;
#define HANDLE_ADDRESS_FILLING() \
while(ctx->fill_in_break_addrs_ptr > old_break_addrs_ptr) { \
*(JUMP_OFFSET *)&(*buf)[BREAK_ADDR] = *idx - (BREAK_ADDR + sizeof(JUMP_OFFSET)) ; \
ctx->fill_in_break_addrs_ptr--; \
} \
while(ctx->fill_in_continue_addrs_ptr > old_continue_addrs_ptr) { \
*(JUMP_OFFSET *)&(*buf)[CONTINUE_ADDR] = continue_target_idx - (CONTINUE_ADDR + sizeof(JUMP_OFFSET)); \
ctx->fill_in_continue_addrs_ptr--; \
}
void compile_main_section(COMPILATION_CONTEXT *ctx, ast_node *node, char **buf, size_t *idx, size_t *allocated, int need_result);
/* static here makes `clang` compiler happy and not "undefined reference to `ensure_room'"*/
static inline void ensure_room(char **buf, const size_t cur_len, size_t *allocated, size_t room) {
size_t new_size;
// DEBUG_COMPILER("entering ensure_room() buf=%p, cur_len=%zu, allocated=%zu, room=%zu\n", *buf, cur_len, *allocated, room);
assert(*allocated);
if(*allocated-cur_len >= room) {
// DEBUG_COMPILER("leaving ensure_room() status=enough_room buf=%p, cur_len=%zu, allocated=%zu, room=%zu\n", *buf, cur_len, *allocated, room);
return;
}
for(new_size = *allocated; new_size-cur_len < room; new_size <<= 1) ;
*buf = NGS_REALLOC(*buf, new_size);
assert(buf);
*allocated = new_size;
DEBUG_COMPILER("leaving ensure_room() status=realloc_done buf=%p, cur_len=%zu, allocated=%zu, room=%zu\n", *buf, cur_len, *allocated, room);
}
SYMBOL *get_symbol_table_entry(VALUE st, char *name, int create_if_not_exists, int *created) {
SYMBOL *s;
*created = 0;
HASH_OBJECT_ENTRY *entry = get_hash_key(st, make_string(name));
if(entry) {
return (SYMBOL *)entry->val.ptr;
}
if(!create_if_not_exists) {
return NULL;
}
s = NGS_MALLOC(sizeof(*s));
s->is_predefined_global = 0;
set_hash_key(st, make_string(name), (VALUE) {.ptr = s});
*created = 1;
return s;
}
GLOBAL_VAR_INDEX get_global_var_index(COMPILATION_CONTEXT *ctx, char *name, const size_t *idx) {
SYMBOL *s;
int created;
s = get_symbol_table_entry(ctx->globals, name, 1, &created);
if(created) {
int global_found;
s->index = check_global_index(&ctx->vm, name, strlen(name), &global_found);
DEBUG_COMPILER("New global symbol name=%s is_predefined_global=%d\n", name, global_found);
// global_found = 0; // XXX: test
s->is_predefined_global = global_found;
if(!s->is_predefined_global) {
s->offsets = make_array(0);
}
}
if(s->is_predefined_global) {
return (GLOBAL_VAR_INDEX) s->index;
}
array_push(s->offsets, MAKE_INT(*idx));
DEBUG_COMPILER("Global %s to be patched at offset %zu\n", name, *idx);
return 0;
}
IDENTIFIER_INFO resolve_identifier(COMPILATION_CONTEXT *ctx, const char *name) {
IDENTIFIER_INFO ret;
HASH_OBJECT_ENTRY *e;
int locals_idx;
VALUE name_val = make_string(name);
if(ctx->locals_ptr) {
e = get_hash_key(LOCALS, name_val);
if(e) {
// printf("xxx-1-1 %s\n", name);
ret.type = LOCAL_IDENTIFIER;
ret.index = ((SYMBOL *)e->val.ptr)->index;
goto exit;
}
// printf("xxx-1-2 %s\n", name);
for(locals_idx = ctx->locals_ptr-1; locals_idx > 0; locals_idx--) {
e = get_hash_key(ctx->locals[locals_idx-1], name_val);
if(e) {
// printf("xxx-2 %s\n", name);
UPVAR_INDEX u = locals_idx;
ret.type = UPVAR_IDENTIFIER;
ret.index = ((SYMBOL *)e->val.ptr)->index;
ret.uplevel = ctx->locals_ptr - locals_idx - 1; // -1 is to make it zero based
// F level1(x) { -> uplevels=0
// F level2() { -> uplevels=1
// F level3() { -> uplevels=2
// dump(x) -> uplevels=2 (we are here, maximize also uplevels for level3, level2 and up)
// }
// }
// }
for(locals_idx = ctx->locals_ptr; locals_idx > 0; locals_idx--) {
if(ctx->n_uplevels[locals_idx-1] < locals_idx - u) {
ctx->n_uplevels[locals_idx-1] = locals_idx - u;
}
// XXX: else break? all upper levels should be up to date in this case
}
goto exit;
}
}
}
e = get_hash_key(ctx->globals, name_val);
if(e) {
ret.type = GLOBAL_IDENTIFIER;
ret.index = ((SYMBOL *)e->val.ptr)->index;
goto exit;
}
// Identifier not found
ret.type = NO_IDENTIFIER;
exit:
return ret;
}
#define REGISTER_IDENTIFIER(t) \
SYMBOL *s; \
s = NGS_MALLOC(sizeof(*s)); \
s->is_predefined_global = 0; \
s->index = t; \
set_hash_key(IDENTIFIERS_SCOPES, make_string(name), (VALUE) {.ptr = s}); \
void register_local_identifier(COMPILATION_CONTEXT *ctx, char *name) { REGISTER_IDENTIFIER(LOCAL_IDENTIFIER); }
void register_upvar_identifier(COMPILATION_CONTEXT *ctx, char *name) { REGISTER_IDENTIFIER(UPVAR_IDENTIFIER); }
void register_global_identifier(COMPILATION_CONTEXT *ctx, char *name) { REGISTER_IDENTIFIER(GLOBAL_IDENTIFIER); }
void register_local_var(COMPILATION_CONTEXT *ctx, char *name) {
SYMBOL *s;
VALUE name_val = make_string(name);
HASH_OBJECT_ENTRY *e = get_hash_key(LOCALS, name_val);
if(e) {
return;
}
assert(N_LOCALS < MAX_LOCALS);
s = NGS_MALLOC(sizeof(*s));
s->index = N_LOCALS++;
set_hash_key(LOCALS, name_val, (VALUE) {.ptr = s});
register_local_identifier(ctx, name);
}
// TODO: maybe do it at parse time? That might be more complex but probably faster
// TODO: refactor for code deduplication
void register_local_vars(COMPILATION_CONTEXT *ctx, ast_node *node) {
SYMBOL *s;
HASH_OBJECT_ENTRY *e;
ast_node *ptr, *ptr2;
switch(node->type) {
case FUNC_NODE:
if(node->first_child->next_sibling->next_sibling->next_sibling) {
// Function has a name
e = get_hash_key(IDENTIFIERS_SCOPES, make_string(node->first_child->next_sibling->next_sibling->next_sibling->name));
if(!e) {
register_local_var(ctx, node->first_child->next_sibling->next_sibling->next_sibling->name);
}
}
return;
case LOCAL_NODE:
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
switch(ptr->type) {
case IDENTIFIER_NODE:
register_local_var(ctx, ptr->name);
break;
case ASSIGNMENT_NODE:
assert(ptr->first_child->type == IDENTIFIER_NODE);
register_local_var(ctx, ptr->first_child->name);
for(ptr2=ptr->first_child->next_sibling; ptr2; ptr2=ptr2->next_sibling) {
register_local_vars(ctx, ptr2);
}
break;
default:
assert(0 == "Unexpected node type under LOCAL_NODE");
}
}
break;
case UPVAR_NODE:
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
switch(ptr->type) {
case IDENTIFIER_NODE:
register_upvar_identifier(ctx, ptr->name);
break;
case ASSIGNMENT_NODE:
assert(ptr->first_child->type == IDENTIFIER_NODE);
register_upvar_identifier(ctx, ptr->first_child->name);
for(ptr2=ptr->first_child->next_sibling; ptr2; ptr2=ptr2->next_sibling) {
register_local_vars(ctx, ptr2);
}
break;
default:
assert(0 == "Unexpected node type under UPVAR_NODE");
}
}
break;
case GLOBAL_NODE:
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
switch(ptr->type) {
case IDENTIFIER_NODE:
// printf("REG GLOB %s\n", ptr->name);
register_global_identifier(ctx, ptr->name);
break;
case ASSIGNMENT_NODE:
assert(ptr->first_child->type == IDENTIFIER_NODE);
register_global_identifier(ctx, ptr->first_child->name);
for(ptr2=ptr->first_child->next_sibling; ptr2; ptr2=ptr2->next_sibling) {
register_local_vars(ctx, ptr2);
}
break;
default:
assert(0 == "Unexpected node type under GLOBAL_NODE");
}
}
break;
case ASSIGNMENT_NODE:
if(node->first_child->type == IDENTIFIER_NODE) {
e = get_hash_key(IDENTIFIERS_SCOPES, make_string(node->first_child->name));
if(!e) {
IDENTIFIER_INFO identifier_info;
identifier_info = resolve_identifier(ctx, node->first_child->name);
if(identifier_info.type == UPVAR_IDENTIFIER) {
s = NGS_MALLOC(sizeof(*s));
s->is_predefined_global = 0;
s->index = UPVAR_IDENTIFIER;
set_hash_key(IDENTIFIERS_SCOPES, make_string(node->first_child->name), (VALUE) {.ptr=s});
} else {
register_local_var(ctx, node->first_child->name);
}
}
}
for(ptr=node->first_child->next_sibling; ptr; ptr=ptr->next_sibling) {
register_local_vars(ctx, ptr);
}
break;
default:
break;
}
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
register_local_vars(ctx, ptr);
}
}
void compile_identifier(COMPILATION_CONTEXT *ctx, char **buf, size_t *idx, char *name, int opcode_local, int opcode_upvar, int opcode_global) {
IDENTIFIER_INFO identifier_info;
GLOBAL_VAR_INDEX gvi;
identifier_info = resolve_identifier(ctx, name);
// printf("NAME %s\n", name);
switch(identifier_info.type) {
case LOCAL_IDENTIFIER:
// printf(" LOCAL\n");
OPCODE(*buf, opcode_local);
DATA_N_LOCAL_VARS(*buf, identifier_info.index);
break;
case UPVAR_IDENTIFIER:
// printf(" UPVAR\n");
OPCODE(*buf, opcode_upvar);
DATA_N_UPVAR_INDEX(*buf, identifier_info.uplevel);
DATA_N_LOCAL_VARS(*buf, identifier_info.index);
break;
case NO_IDENTIFIER:
case GLOBAL_IDENTIFIER:
// printf(" GLOBAL\n");
OPCODE(*buf, opcode_global);
gvi = get_global_var_index(ctx, name, idx);
DATA_N_GLOBAL_VARS(*buf, gvi);
break;
}
}
void compile_field_name(COMPILATION_CONTEXT *ctx, ast_node *node, char **buf, size_t *idx, size_t *allocated, int need_result) {
if(!need_result) {
return;
}
if(node->type != IDENTIFIER_NODE) {
compile_main_section(ctx, node, buf, idx, allocated, NEED_RESULT);
return;
}
OPCODE(*buf, OP_PUSH_L8_STR);
L8_STR(*buf, node->name);
}
void compile_main_section(COMPILATION_CONTEXT *ctx, ast_node *node, char **buf, size_t *idx, size_t *allocated, int need_result) {
ast_node *ptr, *callable;
int argc, have_arr_splat, have_hash_splat, params_flags;
int doing_named_args = 0;
LOCAL_VAR_INDEX n_locals, n_params_required, n_params_optional;
UPVAR_INDEX n_uplevels;
size_t loop_beg_idx, cond_jump, continue_target_idx, func_jump, end_of_func_idx, if_jump, while_jump;
int old_break_addrs_ptr, old_continue_addrs_ptr, i, saved_stack_depth;
HASH_OBJECT_ENTRY *e;
// Can probably be overwhelmed by number of local variables with lengthy names
ensure_room(buf, *idx, allocated, 1024); // XXX - magic number
// printf("compile_main_section() node=%p type=%s last_child=%p need_result=%d\n", node, NGS_AST_NODE_TYPES_NAMES[node->type], node->last_child, need_result);
if(node->location.first_line) {
source_tracking_entry *ste = NULL;
// printf("LOC: ip=%lu\n %d:%d %d:%d\n", *idx, node->location.first_line, node->location.first_column, node->location.last_line, node->location.last_column);
if(ctx->source_tracking_entries_count) {
if(ctx->source_tracking_entries[ctx->source_tracking_entries_count-1].ip == *idx) {
// Override because deeper ast nodes have more precise location
ste = &ctx->source_tracking_entries[ctx->source_tracking_entries_count-1];
} else {
ste = NULL;
}
}
if(!ste) {
if (ctx->source_tracking_entries_count == ctx->source_tracking_entries_allocated) {
ctx->source_tracking_entries_allocated *= 2;
ctx->source_tracking_entries = NGS_REALLOC(ctx->source_tracking_entries, ctx->source_tracking_entries_allocated * sizeof(source_tracking_entry));
}
ste = &ctx->source_tracking_entries[ctx->source_tracking_entries_count++];
}
ste->source_file_name_idx = 0; // XXX: currently only one source file per compile() is supported
ste->ip = *idx;
ste->source_location[0] = node->location.first_line;
ste->source_location[1] = node->location.first_column;
ste->source_location[2] = node->location.last_line;
ste->source_location[3] = node->location.last_column;
}
switch(node->type) {
case CALL_NODE:
DEBUG_COMPILER("COMPILER: %s %zu\n", "CALL NODE", *idx);
OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
saved_stack_depth = STACK_DEPTH;
STACK_DEPTH++;
// print_ast(node, 0);
assert(node->first_child->next_sibling->type == ARGS_NODE);
for(ptr=node->first_child->next_sibling->first_child, have_arr_splat=0, have_hash_splat=0; ptr; ptr=ptr->next_sibling) {
assert(ptr->type == ARG_NODE);
if(ptr->first_child->type == ARR_SPLAT_NODE) {
have_arr_splat = 1;
}
if(ptr->first_child->type == HASH_SPLAT_NODE) {
have_hash_splat = 1;
}
}
if(have_arr_splat) {
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0);
OPCODE(*buf, OP_MAKE_ARR);
STACK_DEPTH++;
}
argc = 0;
if(node->first_child->type == FIELD_NODE) {
compile_main_section(ctx, node->first_child->first_child, buf, idx, allocated, NEED_RESULT);
if(have_arr_splat) {
OPCODE(*buf, OP_ARR_APPEND);
}
argc++;
}
for(ptr=node->first_child->next_sibling->first_child; ptr; ptr=ptr->next_sibling) {
assert(ptr->type == ARG_NODE);
if(ptr->first_child->next_sibling) {
continue;
}
if(ptr->first_child->type == ARR_SPLAT_NODE) {
compile_main_section(ctx, ptr->first_child->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_ARR);
OPCODE(*buf, OP_ARR_CONCAT);
continue;
}
if(ptr->first_child->type == HASH_SPLAT_NODE) {
continue;
}
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
STACK_DEPTH++;
argc++;
if(have_arr_splat) {
OPCODE(*buf, ptr->first_child->type == ARR_SPLAT_NODE ? OP_ARR_CONCAT : OP_ARR_APPEND);
}
}
doing_named_args = 0;
for(ptr=node->first_child->next_sibling->first_child; ptr; ptr=ptr->next_sibling) {
assert(ptr->type == ARG_NODE);
if(ptr->first_child->next_sibling) {
// Got named argument
if(!doing_named_args) {
// Setup named arguments
doing_named_args = 1;
// TODO: maybe special opcode for creating an empty hash?
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0);
OPCODE(*buf, OP_MAKE_HASH);
STACK_DEPTH++;
argc++;
}
// argument name
compile_main_section(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
// argument value
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_HASH_SET);
continue;
}
if(ptr->first_child->type == HASH_SPLAT_NODE) {
if(!doing_named_args) {
// Setup named arguments
doing_named_args = 1;
// TODO: maybe special opcode for creating an empty hash?
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0);
OPCODE(*buf, OP_MAKE_HASH);
// XXX needed? /// STACK_DEPTH++;
argc++;
}
compile_main_section(ctx, ptr->first_child->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_HASH_UPDATE);
continue;
}
}
if(doing_named_args) {
// Marker at the end
OPCODE(*buf, OP_PUSH_KWARGS_MARKER);
argc++;
if(have_arr_splat) {
OPCODE(*buf, OP_ARR_APPEND2);
}
}
if(!have_arr_splat) {
assert(argc <= MAX_ARGS); // TODO: Exception
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, argc);
STACK_DEPTH++;
}
if(node->first_child->type == FIELD_NODE) {
callable = node->first_child->first_child->next_sibling;
} else {
callable = node->first_child;
}
compile_main_section(ctx, callable, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, have_arr_splat ? OP_CALL_ARR : OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);
STACK_DEPTH = saved_stack_depth;
break;
case INDEX_NODE:
case FIELD_NODE:
case NS_NODE:
DEBUG_COMPILER("COMPILER: %s %zu\n", "INDEX NODE", *idx);
OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
saved_stack_depth = STACK_DEPTH;
STACK_DEPTH++;
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
STACK_DEPTH++;
if(node->type == FIELD_NODE || node->type == NS_NODE) {
compile_field_name(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
} else {
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
}
STACK_DEPTH++;
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 2);
STACK_DEPTH++;
compile_identifier(ctx, buf, idx, node->type == INDEX_NODE ? "[]" : (node->type == NS_NODE ? "::" : "."), OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);
STACK_DEPTH = saved_stack_depth;
break;
case INT_NODE:
/*printf("Compiling tNUMBER @ %d\n", *idx);*/
if(need_result) {
// (>> TAG_BITS) is workaround for highest bits of OP_PUSH_INT32 get lost
if(node->number < (INT32_MIN >> TAG_BITS) || node->number > (INT32_MAX >> TAG_BITS)) {
OPCODE(*buf, OP_PUSH_INT64); DATA_INT64(*buf, node->number);
} else {
int n = (int) node->number;
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, n);
}
}
break;
case REAL_NODE:
if(need_result) {
OPCODE(*buf, OP_PUSH_REAL); DATA(*buf, *(NGS_REAL *)node->data);
}
break;
case IDENTIFIER_NODE:
compile_identifier(ctx, buf, idx, node->name, OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
POP_IF_DONT_NEED_RESULT(*buf);
break;
case ASSIGNMENT_NODE:
ptr = node->first_child;
switch(ptr->type) {
case IDENTIFIER_NODE:
DEBUG_COMPILER("COMPILER: %s %zu\n", "identifier <- expression", *idx);
compile_main_section(ctx, ptr->next_sibling, buf, idx, allocated, NEED_RESULT);
DUP_IF_NEED_RESULT(*buf);
compile_identifier(ctx, buf, idx, ptr->name, OP_STORE_LOCAL, OP_STORE_UPVAR, OP_STORE_GLOBAL);
break;
case INDEX_NODE:
OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
compile_main_section(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 3);
compile_identifier(ctx, buf, idx, "[]=", OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);
break;
case FIELD_NODE:
case NS_NODE:
OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
compile_field_name(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 3);
compile_identifier(ctx, buf, idx, ptr->type == FIELD_NODE ? ".=" : "::=", OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
POP_IF_DONT_NEED_RESULT(*buf);
break;
default:
assert(0=="compile_main_section(): assignment to unknown node type");
}
break;
case EXPRESSIONS_NODE:
if(!node->first_child && need_result) {
OPCODE(*buf, OP_PUSH_NULL);
break;
}
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
// printf("EXPRESSIONS_NODE ptr=%p type=%s need_result=%d will_do_result=%d\n", ptr, NGS_AST_NODE_TYPES_NAMES[ptr->type], need_result, (ptr == node->last_child) && need_result);
compile_main_section(ctx, ptr, buf, idx, allocated, (!ptr->next_sibling) && need_result);
}
break;
case FOR_NODE:
// setup
compile_main_section(ctx, node->first_child, buf, idx, allocated, DONT_NEED_RESULT);
// condition
loop_beg_idx = *idx;
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_BOOL);
OPCODE(*buf, OP_JMP_FALSE);
cond_jump = *idx;
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
// body
SETUP_ADDRESS_FILLING();
compile_main_section(ctx, node->first_child->next_sibling->next_sibling->next_sibling, buf, idx, allocated, DONT_NEED_RESULT);
// increment
continue_target_idx = *idx;
compile_main_section(ctx, node->first_child->next_sibling->next_sibling, buf, idx, allocated, DONT_NEED_RESULT);
// jump to condition
OPCODE(*buf, OP_JMP);
assert(*idx - cond_jump < 0x7FFF);
*(JUMP_OFFSET *)&(*buf)[cond_jump] = *idx - cond_jump;
assert((*idx - loop_beg_idx) < 0x7FFF);
DATA_JUMP_OFFSET(*buf, -(*idx - loop_beg_idx + sizeof(JUMP_OFFSET)));
HANDLE_ADDRESS_FILLING();
if(need_result) OPCODE(*buf, OP_PUSH_NULL);
break;
case EMPTY_NODE:
break;
case ARR_LIT_NODE:
DEBUG_COMPILER("COMPILER: %s %zu\n", "ARRAY NODE", *idx);
for(ptr=node->first_child, have_arr_splat=0; ptr; ptr=ptr->next_sibling) {
if(ptr->type == ARR_SPLAT_NODE) {
have_arr_splat = 1;
break;
}
}
if(have_arr_splat) {
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0);
OPCODE(*buf, OP_MAKE_ARR);
}
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
if(ptr->type == ARR_SPLAT_NODE) {
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_ARR);
} else {
compile_main_section(ctx, ptr, buf, idx, allocated, NEED_RESULT);
}
if(have_arr_splat) {
OPCODE(*buf, ptr->type == ARR_SPLAT_NODE ? OP_ARR_CONCAT : OP_ARR_APPEND);
}
}
if(!have_arr_splat) {
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, argc);
OPCODE(*buf, OP_MAKE_ARR);
}
POP_IF_DONT_NEED_RESULT(*buf);
break;
case FUNC_NODE:
// FUNC_NODE children: arguments, body
DEBUG_COMPILER("COMPILER: %s %zu\n", "FUNC NODE", *idx);
OPCODE(*buf, OP_JMP);
func_jump = *idx;
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
ctx->locals_ptr++;
assert(ctx->locals_ptr < COMPILE_MAX_FUNC_DEPTH);
LOCALS = make_hash(4);
IDENTIFIERS_SCOPES = make_hash(4);
N_LOCALS = 0;
N_UPLEVELS = 0;
STACK_DEPTH = 0;
NS = NULL;
params_flags = 0;
// Arguments
for(ptr=node->first_child->first_child; ptr; ptr=ptr->next_sibling) {
// ptr children: identifier, type, (default value | splat indicator)
register_local_var(ctx, ptr->first_child->name);
}
// Body
register_local_vars(ctx, node->first_child->next_sibling);
// if(N_UPLEVELS) printf("UPLEVELS %d\n", N_UPLEVELS);
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_RET);
end_of_func_idx = *idx;
n_locals = N_LOCALS;
n_uplevels = N_UPLEVELS;
// Local variables names
// TODO: something more efficient
for(e=HASH_HEAD(LOCALS); e; e=e->insertion_order_next) {
OPCODE(*buf, OP_PUSH_L8_STR);
L8_STR(*buf, obj_to_cstring(e->key));
}
ctx->locals_ptr--;
// Arguments' types and default values
for(ptr=node->first_child->first_child, n_params_required=0, n_params_optional=0; ptr; ptr=ptr->next_sibling) {
// ptr children: identifier, type, (default value | splat indicator)
OPCODE(*buf, OP_PUSH_L8_STR);
L8_STR(*buf, ptr->first_child->name);
// printf("PT 0 %s\n", ptr->first_child->name);
compile_main_section(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
if(ptr->first_child->next_sibling->next_sibling) {
// Either array/hash splat or default value
if(ptr->first_child->next_sibling->next_sibling->type == ARR_SPLAT_NODE) {
if(ptr->next_sibling && (ptr->next_sibling->first_child->next_sibling->next_sibling->type != HASH_SPLAT_NODE)) {
assert(0 == "splat function parameter must be the last one or followed by keyword splat only");
}
params_flags |= PARAMS_FLAG_ARR_SPLAT;
continue;
}
if(ptr->first_child->next_sibling->next_sibling->type == HASH_SPLAT_NODE) {
if(ptr->next_sibling) {
assert(0 == "keyword splat function parameter must be the last one");
}
params_flags |= PARAMS_FLAG_HASH_SPLAT;
continue;
}
// Splat's handled, we have default value
compile_main_section(ctx, ptr->first_child->next_sibling->next_sibling, buf, idx, allocated, NEED_RESULT);
n_params_optional++;
continue;
}
// Optional parameters can not be followed by required parameters
assert(n_params_optional == 0);
n_params_required++;
}
*(JUMP_OFFSET *)&(*buf)[func_jump] = (end_of_func_idx - func_jump - sizeof(JUMP_OFFSET));
OPCODE(*buf, OP_MAKE_CLOSURE);
DATA_JUMP_OFFSET(*buf, -(*idx - func_jump + 3*sizeof(LOCAL_VAR_INDEX) + sizeof(UPVAR_INDEX) + sizeof(int)));
DATA_N_LOCAL_VARS(*buf, n_params_required);
DATA_N_LOCAL_VARS(*buf, n_params_optional);
DATA_N_LOCAL_VARS(*buf, n_locals);
DATA_N_UPVAR_INDEX(*buf, n_uplevels);
DATA_INT32(*buf, params_flags);
// Doc
compile_main_section(ctx, node->first_child->next_sibling->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_SET_CLOSURE_DOC);
// Namespace attribute
if(NS) {
compile_main_section(ctx, NS, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_SET_CLOSURE_NS);
}
// Name
if(node->first_child->next_sibling->next_sibling->next_sibling) {
// Function has a name
compile_identifier(ctx, buf, idx, node->first_child->next_sibling->next_sibling->next_sibling->name, OP_DEF_LOCAL_FUNC, OP_DEF_UPVAR_FUNC, OP_DEF_GLOBAL_FUNC);
OPCODE(*buf, OP_SET_CLOSURE_NAME);
L8_STR(*buf, node->first_child->next_sibling->next_sibling->next_sibling->name);
}
POP_IF_DONT_NEED_RESULT(*buf);
break;
case SET_NS_NODE:
NS = node->first_child;
break;
case GET_NS_NODE:
if(NS) {
compile_main_section(ctx, NS, buf, idx, allocated, need_result);
} else {
if(need_result) {
OPCODE(*buf, OP_PUSH_NULL);
}
}
break;
case STR_COMPS_NODE: {
int have_splat = 0;
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
if(ptr->type == STR_COMP_SPLAT_EXPANSION_NODE) {
have_splat = 1;
break;
}
}
if(have_splat) {
OPCODE(*buf, OP_PUSH_NULL);
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
compile_main_section(ctx, ptr, buf, idx, allocated, NEED_RESULT);
switch(ptr->type) {
case STR_COMP_IMM_NODE: OPCODE(*buf, OP_MAKE_STR_IMM); break;
case STR_COMP_EXPANSION_NODE: OPCODE(*buf, OP_MAKE_STR_EXP); break;
case STR_COMP_SPLAT_EXPANSION_NODE: OPCODE(*buf, OP_MAKE_STR_SPLAT_EXP); break;
default: break;
}
}
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, argc);
OPCODE(*buf, OP_MAKE_ARR);
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 1);
// OPCODE(*buf, OP_MAKE_SPLAT_STR);
compile_identifier(ctx, buf, idx, "\"$*\"", OP_FETCH_LOCAL, OP_FETCH_UPVAR, OP_FETCH_GLOBAL);
OPCODE(*buf, OP_CALL);
} else {
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
compile_main_section(ctx, ptr, buf, idx, allocated, NEED_RESULT);
if(ptr->type != STR_COMP_IMM_NODE) {
OPCODE(*buf, OP_TO_STR);
}
}
switch(argc) {
case 0:
OPCODE(*buf, OP_PUSH_EMPTY_STR);
break;
case 1:
break;
default:
OPCODE(*buf, OP_PUSH_INT32);
DATA_INT32(*buf, argc);
OPCODE(*buf, OP_MAKE_STR);
}
}
POP_IF_DONT_NEED_RESULT(*buf);
break;
}
case STR_COMP_IMM_NODE: {
size_t l = strlen(node->name);
if(l < 256) {
OPCODE(*buf, OP_PUSH_L8_STR);
L8_STR(*buf, node->name);
} else {
OPCODE(*buf, OP_PUSH_L32_STR);
DATA_UINT32(*buf, l);
ensure_room(buf, *idx, allocated, l);
memcpy((*buf)+(*idx), node->name, l);
(*idx) += l;
}
break;
}
case STR_COMP_EXPANSION_NODE:
case STR_COMP_SPLAT_EXPANSION_NODE:
compile_main_section(ctx, node->first_child, buf, idx, allocated, need_result); break;
case NULL_NODE: if(need_result) { OPCODE(*buf, OP_PUSH_NULL); } break;
case TRUE_NODE: if(need_result) { OPCODE(*buf, OP_PUSH_TRUE); } break;
case FALSE_NODE: if(need_result) { OPCODE(*buf, OP_PUSH_FALSE); } break;
case IF_NODE:
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_BOOL);
OPCODE(*buf, OP_JMP_FALSE);
if_jump = *idx;
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, need_result);
OPCODE(*buf, OP_JMP);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
*(JUMP_OFFSET *)&(*buf)[if_jump] = *idx - if_jump - sizeof(JUMP_OFFSET); // Jump is OP_JMP_FALSE JUMP_OFFSET shorter
if_jump = *idx - sizeof(JUMP_OFFSET);
compile_main_section(ctx, node->first_child->next_sibling->next_sibling, buf, idx, allocated, need_result);
*(JUMP_OFFSET *)&(*buf)[if_jump] = *idx - if_jump - sizeof(JUMP_OFFSET);
break;
case WHILE_NODE:
loop_beg_idx = *idx;
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_BOOL);
OPCODE(*buf, OP_JMP_FALSE);
while_jump = *idx;
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
SETUP_ADDRESS_FILLING();
continue_target_idx = loop_beg_idx; // For HANDLE_ADDRESS_FILLING
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, DONT_NEED_RESULT);
OPCODE(*buf, OP_JMP);
DATA_JUMP_OFFSET(*buf, -(*idx - loop_beg_idx + sizeof(JUMP_OFFSET)));
*(JUMP_OFFSET *)&(*buf)[while_jump] = *idx - while_jump - sizeof(JUMP_OFFSET);
HANDLE_ADDRESS_FILLING();
if(need_result) { OPCODE(*buf, OP_PUSH_NULL); }
break;
case LOCAL_NODE:
case UPVAR_NODE:
case GLOBAL_NODE:
for(ptr=node->first_child; ptr; ptr=ptr->next_sibling) {
if(ptr->type != IDENTIFIER_NODE) {
compile_main_section(ctx, ptr, buf, idx, allocated, DONT_NEED_RESULT);
}
}
if(need_result) { OPCODE(*buf, OP_PUSH_NULL); }
break;
case HASH_LIT_NODE:
DEBUG_COMPILER("COMPILER: %s %zu\n", "HASH NODE", *idx);
for(ptr=node->first_child, have_hash_splat=0; ptr; ptr=ptr->next_sibling) {
if(ptr->type == HASH_SPLAT_NODE) {
have_hash_splat = 1;
break;
}
}
if(have_hash_splat) {
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 0);
OPCODE(*buf, OP_MAKE_HASH);
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
if(ptr->type == HASH_SPLAT_NODE) {
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_TO_HASH);
OPCODE(*buf, OP_HASH_UPDATE);
} else {
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
compile_main_section(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_HASH_SET);
}
}
} else {
for(argc=0, ptr=node->first_child; ptr; argc++, ptr=ptr->next_sibling) {
compile_main_section(ctx, ptr->first_child, buf, idx, allocated, NEED_RESULT);
compile_main_section(ctx, ptr->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
}
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, argc);
OPCODE(*buf, OP_MAKE_HASH);
}
POP_IF_DONT_NEED_RESULT(*buf);
break;
case RETURN_NODE:
for(i=0; i<STACK_DEPTH; i++) {
OPCODE(*buf, OP_POP);
}
if(node->first_child) {
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
} else {
OPCODE(*buf, OP_PUSH_NULL);
}
OPCODE(*buf, OP_RET);
break;
case AND_NODE:
case OR_NODE:
// TODO: optimize more for DONT_NEED_RESULT case
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_DUP);
OPCODE(*buf, OP_TO_BOOL);
if_jump = *idx;
OPCODE(*buf, node->type == AND_NODE ? OP_JMP_FALSE : OP_JMP_TRUE);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf);
OPCODE(*buf, OP_POP);
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
*(JUMP_OFFSET *)&(*buf)[if_jump+1] = *idx - if_jump - 1 - sizeof(JUMP_OFFSET); // Jump is OP_JMP_FALSE JUMP_OFFSET shorter
if_jump = *idx - 1 - sizeof(JUMP_OFFSET);
POP_IF_DONT_NEED_RESULT(*buf);
break;
case TAND_NODE:
if_jump = *idx;
OPCODE(*buf, OP_TRY_START);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Set handler code location
compile_main_section(ctx, node->first_child, buf, idx, allocated, DONT_NEED_RESULT);
end_of_func_idx = *idx;
OPCODE(*buf, OP_TRY_END);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Jump over handler code
*(JUMP_OFFSET *)&(*buf)[if_jump+1] = *idx - if_jump - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
// Exception handler - start
OPCODE(*buf, OP_THROW); // Rethrow
// Exception handler - end
*(JUMP_OFFSET *)&(*buf)[end_of_func_idx+1] = *idx - end_of_func_idx - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, need_result);
break;
case TOR_NODE:
if_jump = *idx;
OPCODE(*buf, OP_TRY_START);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Set handler code location
compile_main_section(ctx, node->first_child, buf, idx, allocated, need_result);
end_of_func_idx = *idx;
OPCODE(*buf, OP_TRY_END);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Jump over handler code
*(JUMP_OFFSET *)&(*buf)[if_jump+1] = *idx - if_jump - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
// Exception handler - start
OPCODE(*buf, OP_POP); // Ignore the exception value
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, need_result);
// Exception handler - end
*(JUMP_OFFSET *)&(*buf)[end_of_func_idx+1] = *idx - end_of_func_idx - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
break;
case GUARD_NODE:
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_DUP); // guard evaluates to the argument
OPCODE(*buf, OP_TO_BOOL);
OPCODE(*buf, OP_GUARD);
POP_IF_DONT_NEED_RESULT(*buf);
break;
case TRY_CATCH_NODE:
if_jump = *idx;
OPCODE(*buf, OP_TRY_START);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Set handler code location
compile_main_section(ctx, node->first_child, buf, idx, allocated, need_result);
end_of_func_idx = *idx;
OPCODE(*buf, OP_TRY_END);
DATA_JUMP_OFFSET_PLACEHOLDER(*buf); // Jump over handler code
*(JUMP_OFFSET *)&(*buf)[if_jump+1] = *idx - if_jump - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
if(node->first_child->next_sibling) {
// Room for return value
OPCODE(*buf, OP_PUSH_NULL);
OPCODE(*buf, OP_XCHG);
OPCODE(*buf, OP_PUSH_INT32); DATA_INT32(*buf, 1); // One argument for the call of handler function(s)
OPCODE(*buf, OP_MAKE_MULTIMETHOD);
for(ptr=node->first_child->next_sibling; ptr; ptr=ptr->next_sibling) {
compile_main_section(ctx, ptr, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_MULTIMETHOD_APPEND);
}
OPCODE(*buf, OP_MULTIMETHOD_REVERSE);
OPCODE(*buf, OP_CALL_EXC);
POP_IF_DONT_NEED_RESULT(*buf);
} else {
// No handlers, return null
OPCODE(*buf, OP_POP); // Ignore the exception value
if(need_result) {
OPCODE(*buf, OP_PUSH_NULL);
}
}
*(JUMP_OFFSET *)&(*buf)[end_of_func_idx+1] = *idx - end_of_func_idx - 1 - sizeof(JUMP_OFFSET); // Jump is OP_TRY_START JUMP_OFFSET shorter
break;
case THROW_NODE:
compile_main_section(ctx, node->first_child, buf, idx, allocated, NEED_RESULT);
OPCODE(*buf, OP_THROW);
break;
case COMMANDS_PIPELINE_NODE:
OPCODE(*buf, OP_PUSH_NULL); // Placeholder for return value
// commands
compile_main_section(ctx, node->first_child->next_sibling, buf, idx, allocated, NEED_RESULT);
// pipes
compile_main_section(ctx, node->first_child->next_sibling->next_sibling, buf, idx, allocated, NEED_RESULT);
// options