/
compiler.c
1393 lines (1237 loc) · 54.5 KB
/
compiler.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 "moar.h"
#include "nodes_moar.h"
#include <stdint.h>
/* Some constants. */
#define HEADER_SIZE 92
#define BYTECODE_VERSION 2
#define FRAME_HEADER_SIZE (9 * 4 + 2 * 2)
#define FRAME_HANDLER_SIZE (4 * 4 + 2 * 2)
#define SC_DEP_SIZE 4
#define EXTOP_SIZE (4 + 8)
#define SCDEP_HEADER_OFFSET 12
#define EXTOP_HEADER_OFFSET 20
#define FRAME_HEADER_OFFSET 28
#define CALLSITE_HEADER_OFFSET 36
#define STRING_HEADER_OFFSET 44
#define SCDATA_HEADER_OFFSET 52
#define BYTECODE_HEADER_OFFSET 60
#define ANNOTATION_HEADER_OFFSET 68
#define HLL_NAME_HEADER_OFFSET 76
#define SPECIAL_FRAME_HEADER_OFFSET 80
#define EXTOP_BASE 1024
/* Frame flags. */
#define FRAME_FLAG_EXIT_HANDLER 1
#define FRAME_FLAG_IS_THUNK 2
typedef struct {
/* callsite ID */
unsigned short callsite_id;
/* the uthash hash handle. */
UT_hash_handle hash_handle;
} CallsiteReuseEntry;
/* Information about a handler. */
typedef struct {
/* Offset of start of protected region from frame start. */
unsigned int start_offset;
/* Offset of end of protected region, exclusive, from frame start. */
unsigned int end_offset;
/* Exception categry mask. */
unsigned int category_mask;
/* Handler action. */
unsigned short action;
/* Local holding block to invoke, if invokey handler. */
unsigned short local;
/* Label, which will need resolving. */
MASTNode *label;
/* Local holding a label in case we have a labeled loop. */
unsigned short label_reg;
} FrameHandler;
/* Handler actions. */
#define HANDLER_UNWIND_GOTO 0
#define HANDLER_UNWIND_GOTO_OBJ 1
#define HANDLER_INVOKE 2
/* Describes the state for the frame we're currently compiling. */
typedef struct {
/* Position of start of bytecode. */
unsigned int bytecode_start;
/* Position of start of frame entry. */
unsigned int frame_start;
/* Types of locals, along with the number of them we have. */
unsigned short *local_types;
unsigned int num_locals;
/* Types of lexicals, along with the number of them we have. */
unsigned short *lexical_types;
unsigned int num_lexicals;
/* Number of annotations. */
unsigned int num_annotations;
/* Number of handlers */
unsigned int num_handlers;
/* Labels that we have seen and know the address of. Hash of name to
* index. */
MASTNode *known_labels;
/* Labels that are currently unresolved, that we need to fix up. Hash
* of name to a list of positions needing a fixup. */
MASTNode *labels_to_resolve;
/* Handlers list. */
FrameHandler *handlers;
MASTNode *frame_mast;
} FrameState;
/* Describes the current writer state for the compilation unit as a whole. */
typedef struct {
/* The set of node types. */
MASTNodeTypes *types;
/* The current frame and frame count. */
FrameState *cur_frame;
unsigned int num_frames;
/* String heap and seen hash mapping known strings to indexes. */
MASTNode *strings;
MASTNode *seen_strings;
/* The SC dependencies segment; we know the size up front. */
char *scdep_seg;
unsigned int scdep_bytes;
/* The extension ops segment; we know the size ahead of time. */
char *extops_seg;
unsigned int extops_bytes;
unsigned int num_extops;
/* The frame segment. */
char *frame_seg;
unsigned int frame_pos;
unsigned int frame_alloc;
/* The callsite segment and number of callsites. */
char *callsite_seg;
unsigned int callsite_pos;
unsigned int callsite_alloc;
unsigned int num_callsites;
/* The bytecode segment. */
char *bytecode_seg;
unsigned int bytecode_pos;
unsigned int bytecode_alloc;
/* The annotation segment. */
char *annotation_seg;
unsigned int annotation_pos;
unsigned int annotation_alloc;
/* Current instruction info */
MVMOpInfo *current_op_info;
/* Zero-based index of current frame */
unsigned int current_frame_idx;
/* Zero-based index of MAST instructions */
unsigned int current_ins_idx;
/* Zero-based index of current operand */
unsigned int current_operand_idx;
/* The compilation unit we're compiling. */
MAST_CompUnit *cu;
/* Hash for callsite descriptor strings to callsite IDs */
CallsiteReuseEntry *callsite_reuse_head;
/* Last Annotated node, for error reporting */
MAST_Annotated *last_annotated;
} WriterState;
static unsigned int umax(unsigned int a, unsigned int b);
static void memcpy_endian(char *dest, void *src, size_t size);
static void write_int64(char *buffer, size_t offset, unsigned long long value);
static void write_int32(char *buffer, size_t offset, unsigned int value);
static void write_int16(char *buffer, size_t offset, unsigned short value);
static void write_int8(char *buffer, size_t offset, unsigned char value);
static void write_double(char *buffer, size_t offset, double value);
void ensure_space(VM, char **buffer, unsigned int *alloc, unsigned int pos, unsigned int need);
void cleanup_frame(VM, FrameState *fs);
void cleanup_all(VM, WriterState *ws);
unsigned int get_string_heap_index(VM, WriterState *ws, VMSTR *strval);
unsigned short get_frame_index(VM, WriterState *ws, MASTNode *frame);
unsigned short type_to_local_type(VM, WriterState *ws, MASTNode *type);
void compile_operand(VM, WriterState *ws, unsigned char op_flags, MASTNode *operand);
unsigned short get_callsite_id(VM, WriterState *ws, MASTNode *flags);
void compile_instruction(VM, WriterState *ws, MASTNode *node);
void compile_frame(VM, WriterState *ws, MASTNode *node, unsigned short idx);
char * form_string_heap(VM, WriterState *ws, unsigned int *string_heap_size);
char * form_bytecode_output(VM, WriterState *ws, unsigned int *bytecode_size);
char * MVM_mast_compile(VM, MASTNode *node, MASTNodeTypes *types, unsigned int *size);
static unsigned int umax(unsigned int a, unsigned int b) {
return a > b ? a : b;
}
/* copies memory dependent on endianness */
static void memcpy_endian(char *dest, void *src, size_t size) {
#ifdef MVM_BIGENDIAN
size_t i;
char *srcbytes = (char *)src;
for (i = 0; i < size; i++)
dest[size - i - 1] = srcbytes[i];
#else
memcpy(dest, src, size);
#endif
}
/* Writes an int64 into a buffer. */
static void write_int64(char *buffer, size_t offset, unsigned long long value) {
memcpy_endian(buffer + offset, &value, 8);
}
/* Writes an int32 into a buffer. */
static void write_int32(char *buffer, size_t offset, unsigned int value) {
memcpy_endian(buffer + offset, &value, 4);
}
/* Writes an int16 into a buffer. */
static void write_int16(char *buffer, size_t offset, unsigned short value) {
memcpy_endian(buffer + offset, &value, 2);
}
/* Writes an int8 into a buffer. */
static void write_int8(char *buffer, size_t offset, unsigned char value) {
memcpy(buffer + offset, &value, 1);
}
/* Writes an double into a buffer. */
static void write_double(char *buffer, size_t offset, double value) {
memcpy_endian(buffer + offset, &value, 8);
}
/* Ensures the specified buffer has enough space and expands it if so. */
void ensure_space(VM, char **buffer, unsigned int *alloc, unsigned int pos, unsigned int need) {
if (pos + need > *alloc) {
do { *alloc = *alloc * 2; } while (pos + need > *alloc);
*buffer = (char *)realloc(*buffer, *alloc);
}
}
/* Cleans up all allocated memory related to a frame. */
void cleanup_frame(VM, FrameState *fs) {
if (fs->local_types)
free(fs->local_types);
if (fs->lexical_types)
free(fs->lexical_types);
if (fs->handlers)
free(fs->handlers);
free(fs);
}
/* Cleans up all allocated memory related to this compilation. */
void cleanup_all(VM, WriterState *ws) {
if (ws->cur_frame)
cleanup_frame(vm, ws->cur_frame);
if (ws->scdep_seg)
free(ws->scdep_seg);
if (ws->extops_seg)
free(ws->extops_seg);
if (ws->frame_seg)
free(ws->frame_seg);
if (ws->callsite_seg)
free(ws->callsite_seg);
if (ws->bytecode_seg)
free(ws->bytecode_seg);
if (ws->annotation_seg)
free(ws->annotation_seg);
MVM_HASH_DESTROY(hash_handle, CallsiteReuseEntry, ws->callsite_reuse_head);
free(ws);
}
/* Gets the index of a string already in the string heap, or
* adds it to the heap if it's not already there. */
unsigned int get_string_heap_index(VM, WriterState *ws, VMSTR *strval) {
if (EXISTSKEY(vm, ws->seen_strings, strval)) {
return (unsigned int)ATKEY_I(vm, ws->seen_strings, strval);
}
else {
unsigned int index = (unsigned int)ELEMS(vm, ws->strings);
/* XXX Overflow check */
BINDPOS_S(vm, ws->strings, index, strval);
BINDKEY_I(vm, ws->seen_strings, strval, index);
return index;
}
}
/* Locates the index of a frame. */
unsigned short get_frame_index(VM, WriterState *ws, MASTNode *frame) {
int num_frames = ELEMS(vm, ws->cu->frames);
unsigned short i;
for (i = 0; i < num_frames; i++)
if (ATPOS(vm, ws->cu->frames, i) == frame)
return i;
cleanup_all(vm, ws);
DIE(vm, "MAST::Frame passed for code ref not found in compilation unit");
}
/* Takes a 6model object type and turns it into a local/lexical type flag. */
unsigned short type_to_local_type(VM, WriterState *ws, MASTNode *type) {
MVMStorageSpec ss;
if (VM_OBJ_IS_NULL(type))
return MVM_reg_obj;
ss = REPR(type)->get_storage_spec(vm, STABLE(type));
if (ss.inlineable) {
switch (ss.boxed_primitive) {
case MVM_STORAGE_SPEC_BP_INT:
switch (ss.bits) {
case 8:
return MVM_reg_int8;
case 16:
return MVM_reg_int16;
case 32:
#ifdef PARROT_OPS_BUILD
/* XXX Parrot specific hack... */
return sizeof(INTVAL) == 4 ? MVM_reg_int64 : MVM_reg_int32;
#else
return MVM_reg_int32;
#endif
case 64:
return MVM_reg_int64;
default:
cleanup_all(vm, ws);
DIE(vm, "Invalid int size for local/lexical");
}
break;
case MVM_STORAGE_SPEC_BP_NUM:
switch (ss.bits) {
case 32:
return MVM_reg_num32;
case 64:
return MVM_reg_num64;
default:
cleanup_all(vm, ws);
DIE(vm, "Invalid num size for local/lexical");
}
break;
case MVM_STORAGE_SPEC_BP_STR:
return MVM_reg_str;
break;
default:
cleanup_all(vm, ws);
DIE(vm, "Type used for local/lexical has invalid boxed primitive in storage spec");
}
}
else {
return MVM_reg_obj;
}
}
/* Compiles the operand to an instruction; this involves checking
* that we have a node of the correct type for it and writing out
* the appropriate thing to the bytecode stream. */
void compile_operand(VM, WriterState *ws, unsigned char op_flags, MASTNode *operand) {
unsigned char op_rw = op_flags & MVM_operand_rw_mask;
unsigned char op_type = op_flags & MVM_operand_type_mask;
unsigned short int local_type;
if (op_rw == MVM_operand_literal) {
/* Literal; go by type. */
switch (op_type) {
case MVM_operand_int64: {
if (ISTYPE(vm, operand, ws->types->IVal)) {
MAST_IVal *iv = GET_IVal(operand);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 8);
write_int64(ws->bytecode_seg, ws->bytecode_pos, iv->value);
ws->bytecode_pos += 8;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::IVal, but didn't get one");
}
break;
}
case MVM_operand_int16: {
if (ISTYPE(vm, operand, ws->types->IVal)) {
MAST_IVal *iv = GET_IVal(operand);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
if (iv->value > 32767 || iv->value < -32768) {
cleanup_all(vm, ws);
DIE(vm, "Value outside range of 16-bit MAST::IVal");
}
write_int16(ws->bytecode_seg, ws->bytecode_pos, (short)iv->value);
ws->bytecode_pos += 2;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::IVal, but didn't get one");
}
break;
}
case MVM_operand_num64: {
if (ISTYPE(vm, operand, ws->types->NVal)) {
MAST_NVal *nv = GET_NVal(operand);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 8);
write_double(ws->bytecode_seg, ws->bytecode_pos, nv->value);
ws->bytecode_pos += 8;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::NVal, but didn't get one");
}
break;
}
case MVM_operand_str: {
if (ISTYPE(vm, operand, ws->types->SVal)) {
MAST_SVal *sv = GET_SVal(operand);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 4);
write_int32(ws->bytecode_seg, ws->bytecode_pos,
get_string_heap_index(vm, ws, sv->value));
ws->bytecode_pos += 4;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::SVal, but didn't get one");
}
break;
}
case MVM_operand_ins: {
if (ISTYPE(vm, operand, ws->types->Label)) {
MAST_Label *l = GET_Label(operand);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 4);
if (EXISTSKEY(vm, ws->cur_frame->known_labels, l->name)) {
/* Label offset already known; just write it. */
write_int32(ws->bytecode_seg, ws->bytecode_pos,
(unsigned int)ATKEY_I(vm, ws->cur_frame->known_labels, l->name));
}
else {
/* Add this as a position to fix up. */
MASTNode *fixup_list;
if (EXISTSKEY(vm, ws->cur_frame->labels_to_resolve, l->name)) {
fixup_list = ATKEY(vm, ws->cur_frame->labels_to_resolve, l->name);
}
else {
fixup_list = NEWLIST_I(vm);
BINDKEY(vm, ws->cur_frame->labels_to_resolve, l->name, fixup_list);
}
BINDPOS_I(vm, fixup_list, ELEMS(vm, fixup_list), ws->bytecode_pos);
write_int32(ws->bytecode_seg, ws->bytecode_pos, 0);
}
ws->bytecode_pos += 4;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::Label, but didn't get one");
}
break;
}
case MVM_operand_coderef: {
if (ISTYPE(vm, operand, ws->types->Frame)) {
/* Find the frame index in the compilation unit. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
write_int16(ws->bytecode_seg, ws->bytecode_pos,
get_frame_index(vm, ws, operand));
ws->bytecode_pos += 2;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::Frame, but didn't get one");
}
break;
}
default:
cleanup_all(vm, ws);
DIE(vm, "Unhandled literal type in MAST compiler");
}
}
else if (op_rw == MVM_operand_read_reg || op_rw == MVM_operand_write_reg) {
/* The operand node had best be a MAST::Local. */
if (ISTYPE(vm, operand, ws->types->Local)) {
MAST_Local *l = GET_Local(operand);
/* Ensure it's within the set of known locals. */
if (l->index >= ws->cur_frame->num_locals) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local index out of range");
}
/* Check the type matches. */
local_type = ws->cur_frame->local_types[l->index];
if (op_type != local_type << 3 && op_type != MVM_operand_type_var) {
unsigned int current_frame_idx = ws->current_frame_idx;
unsigned int current_ins_idx = ws->current_ins_idx;
const char *name = ws->current_op_info->name;
unsigned int current_operand_idx = ws->current_operand_idx;
cleanup_all(vm, ws);
DIE(vm, "At Frame %u, Instruction %u, op '%s', operand %u, "
"MAST::Local of wrong type (%u) specified; expected %u",
current_frame_idx, current_ins_idx,
name, current_operand_idx,
local_type, (op_type >> 3));
}
/* Write the operand type. */
if (l->index < 0 || l->index > 32768)
DIE(vm, "Frame %u local access out of range", ws->current_frame_idx);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
write_int16(ws->bytecode_seg, ws->bytecode_pos, (unsigned short)l->index);
ws->bytecode_pos += 2;
}
else {
unsigned int current_frame_idx = ws->current_frame_idx;
unsigned int current_ins_idx = ws->current_ins_idx;
const char *name = ws->current_op_info->name;
unsigned int current_operand_idx = ws->current_operand_idx;
cleanup_all(vm, ws);
DIE(vm, "At Frame %u, Instruction %u, op '%s', operand %u, expected MAST::Local, but didn't get one",
current_frame_idx, current_ins_idx, name, current_operand_idx);
}
}
else if (op_rw == MVM_operand_read_lex || op_rw == MVM_operand_write_lex) {
/* The operand node should be a MAST::Lexical. */
if (ISTYPE(vm, operand, ws->types->Lexical)) {
MAST_Lexical *l = GET_Lexical(operand);
/* Write the index, then the frame count. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 4);
write_int16(ws->bytecode_seg, ws->bytecode_pos, (unsigned short)l->index);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, (unsigned short)l->frames_out);
ws->bytecode_pos += 2;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Expected MAST::Lexical, but didn't get one");
}
}
else {
cleanup_all(vm, ws);
DIE(vm, "Unknown operand type cannot be compiled");
}
ws->current_operand_idx++;
}
/* Takes a set of flags describing a callsite. Writes out a callsite
* descriptor and returns the index of it. */
unsigned short get_callsite_id(VM, WriterState *ws, MASTNode *flags) {
/* Work out callsite size. */
unsigned short elems = (unsigned short)ELEMS(vm, flags);
unsigned short align = elems % 2;
unsigned short i;
CallsiteReuseEntry *entry = NULL;
unsigned char *identifier = (unsigned char *)malloc(elems);
for (i = 0; i < elems; i++)
identifier[i] = (unsigned char)ATPOS_I_C(vm, flags, i);
HASH_FIND(hash_handle, ws->callsite_reuse_head, identifier, elems, entry);
if (entry) {
free(identifier);
return entry->callsite_id;
}
entry = (CallsiteReuseEntry *)malloc(sizeof(CallsiteReuseEntry));
entry->callsite_id = (unsigned short)ws->num_callsites;
HASH_ADD_KEYPTR(hash_handle, ws->callsite_reuse_head, identifier, elems, entry);
/* Emit callsite; be sure to pad if there's uneven number of flags. */
ensure_space(vm, &ws->callsite_seg, &ws->callsite_alloc, ws->callsite_pos,
2 + elems + align);
write_int16(ws->callsite_seg, ws->callsite_pos, elems);
ws->callsite_pos += 2;
for (i = 0; i < elems; i++)
write_int8(ws->callsite_seg, ws->callsite_pos++,
(unsigned char)ATPOS_I_C(vm, flags, i));
if (align)
write_int8(ws->callsite_seg, ws->callsite_pos++, 0);
return (unsigned short)ws->num_callsites++;
}
#define OVERRIDE_WITH_32 1
#define OVERRIDE_WITH_16 2
/* Compiles an instruction (which may actaully be any of the
* nodes valid directly in a Frame's instruction list, which
* means labels are valid too). */
void compile_instruction(VM, WriterState *ws, MASTNode *node) {
if (ISTYPE(vm, node, ws->types->Op)) {
MAST_Op *o = GET_Op(node);
MVMOpInfo *info;
int i;
unsigned char override_second_argument = 0;
/* Look up opcode and get argument info. */
unsigned short op = o->op;
info = MVM_op_get_op(op);
if (!info)
DIE(vm, "Invalid op specified in instruction %d", op);
ws->current_op_info = info;
ws->current_operand_idx = 0;
/* Ensure argument count matches up. */
if (info->num_operands != 0 && ELEMS(vm, o->operands) != info->num_operands) {
unsigned int current_frame_idx = ws->current_frame_idx;
unsigned int current_ins_idx = ws->current_ins_idx;
const char *name = ws->current_op_info->name;
cleanup_all(vm, ws);
DIE(vm, "At Frame %u, Instruction %u, op '%s' has invalid number (%u) of operands; needs %u.",
current_frame_idx, current_ins_idx, name,
ELEMS(vm, o->operands), info->num_operands);
}
/* If we're outputting a const_i64 instruction, we may want to */
/* turn it into a const_i64_32 or const_i64_16 instead if it fits */
if (op == MVM_OP_const_i64) {
MASTNode *operand = ATPOS(vm, o->operands, 1);
MAST_IVal *iv = GET_IVal(operand);
if (INT16_MIN <= iv->value && iv->value <= INT16_MAX) {
override_second_argument = OVERRIDE_WITH_16;
} else if (INT32_MIN <= iv->value && iv->value <= INT32_MAX) {
override_second_argument = OVERRIDE_WITH_32;
}
}
/* Write opcode. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
if (override_second_argument == 0)
write_int16(ws->bytecode_seg, ws->bytecode_pos, op);
else if (override_second_argument == OVERRIDE_WITH_16)
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_const_i64_16);
else if (override_second_argument == OVERRIDE_WITH_32)
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_const_i64_32);
ws->bytecode_pos += 2;
/* Write operands. */
for (i = 0; i < info->num_operands; i++) {
if (i == 1 && override_second_argument) {
MASTNode *operand = ATPOS(vm, o->operands, 1);
MAST_IVal *iv = GET_IVal(operand);
if (override_second_argument == OVERRIDE_WITH_32) {
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 4);
write_int32(ws->bytecode_seg, ws->bytecode_pos, (MVMint32)iv->value);
ws->bytecode_pos += 4;
} else {
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
write_int16(ws->bytecode_seg, ws->bytecode_pos, (MVMint16)iv->value);
ws->bytecode_pos += 2;
}
} else {
compile_operand(vm, ws, info->operands[i], ATPOS(vm, o->operands, i));
}
}
}
else if (ISTYPE(vm, node, ws->types->ExtOp)) {
MAST_ExtOp *o = GET_ExtOp(node);
MASTNode *operands;
int i, num_operands;
/* Look up opcode and get argument info. */
unsigned short op = o->op;
if (op < EXTOP_BASE || (op - EXTOP_BASE) >= ELEMS(vm, ws->cu->extop_sigs))
DIE(vm, "Invalid extension op %d specified", op);
operands = ATPOS(vm, ws->cu->extop_sigs, op - EXTOP_BASE);
if (VM_OBJ_IS_NULL(operands))
DIE(vm, "Missing extension op operand array for instruction %d", op);
ws->current_op_info = NULL;
ws->current_operand_idx = 0;
/* Ensure argument count matches up. */
num_operands = ELEMS(vm, operands);
if (ELEMS(vm, o->operands) != num_operands) {
unsigned int current_frame_idx = ws->current_frame_idx;
unsigned int current_ins_idx = ws->current_ins_idx;
cleanup_all(vm, ws);
DIE(vm, "At Frame %u, Instruction %u, op '%s' has invalid number (%u) of operands; needs %u.",
current_frame_idx, current_ins_idx,
VM_STRING_TO_C_STRING(vm, o->name),
ELEMS(vm, o->operands), num_operands);
}
/* Write opcode. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 2);
write_int16(ws->bytecode_seg, ws->bytecode_pos, op);
ws->bytecode_pos += 2;
/* Write operands. */
for (i = 0; i < num_operands; i++)
compile_operand(vm, ws, ATPOS_I(vm, operands, i), ATPOS(vm, o->operands, i));
}
else if (ISTYPE(vm, node, ws->types->Label)) {
/* Duplicate check, then insert. */
MAST_Label *l = GET_Label(node);
unsigned int offset = ws->bytecode_pos - ws->cur_frame->bytecode_start;
if (EXISTSKEY(vm, ws->cur_frame->known_labels, l->name)) {
cleanup_all(vm, ws);
DIE(vm, "Duplicate label");
}
BINDKEY_I(vm, ws->cur_frame->known_labels, l->name, offset);
/* Resolve any existing usages. */
if (EXISTSKEY(vm, ws->cur_frame->labels_to_resolve, l->name)) {
MASTNode *res_list = ATKEY(vm, ws->cur_frame->labels_to_resolve, l->name);
unsigned int num_res = ELEMS(vm, res_list);
unsigned int i;
for (i = 0; i < num_res; i++) {
unsigned int res_pos = (unsigned int)ATPOS_I(vm, res_list, i);
write_int32(ws->bytecode_seg, res_pos, offset);
}
DELETEKEY(vm, ws->cur_frame->labels_to_resolve, l->name);
}
}
else if (ISTYPE(vm, node, ws->types->Call)) {
MAST_Call *c = GET_Call(node);
unsigned char call_op = MVM_OP_invoke_v;
unsigned char res_type = 0;
unsigned short num_flags, num_args, flag_pos, arg_pos;
/* Emit callsite (may re-use existing one) and emit loading of it. */
unsigned short callsite_id = get_callsite_id(vm, ws, c->flags);
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 4);
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_prepargs);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, callsite_id);
ws->bytecode_pos += 2;
/* for errors */
ws->current_op_info = MVM_op_get_op(MVM_OP_prepargs);
ws->current_operand_idx = 0;
/* Set up args. */
num_flags = (unsigned short)ELEMS(vm, c->flags);
num_args = (unsigned short)ELEMS(vm, c->args);
arg_pos = 0;
for (flag_pos = 0; flag_pos < num_flags; flag_pos++) {
/* Handle any special flags. */
unsigned char flag = (unsigned char)ATPOS_I_C(vm, c->flags, flag_pos);
if (flag & MVM_CALLSITE_ARG_NAMED) {
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 6);
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_argconst_s);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, arg_pos);
ws->bytecode_pos += 2;
compile_operand(vm, ws, MVM_operand_str, ATPOS(vm, c->args, arg_pos));
arg_pos++;
}
else if (flag & MVM_CALLSITE_ARG_FLAT) {
/* don't need to do anything special */
}
/* Now go by flag type. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 6);
if (flag & MVM_CALLSITE_ARG_OBJ) {
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_arg_o);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, arg_pos);
ws->bytecode_pos += 2;
compile_operand(vm, ws, MVM_operand_read_reg | MVM_operand_obj,
ATPOS(vm, c->args, arg_pos));
}
else if (flag & MVM_CALLSITE_ARG_STR) {
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_arg_s);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, arg_pos);
ws->bytecode_pos += 2;
compile_operand(vm, ws, MVM_operand_read_reg | MVM_operand_str,
ATPOS(vm, c->args, arg_pos));
}
else if (flag & MVM_CALLSITE_ARG_INT) {
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_arg_i);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, arg_pos);
ws->bytecode_pos += 2;
compile_operand(vm, ws, MVM_operand_read_reg | MVM_operand_int64,
ATPOS(vm, c->args, arg_pos));
}
else if (flag & MVM_CALLSITE_ARG_NUM) {
write_int16(ws->bytecode_seg, ws->bytecode_pos, MVM_OP_arg_n);
ws->bytecode_pos += 2;
write_int16(ws->bytecode_seg, ws->bytecode_pos, arg_pos);
ws->bytecode_pos += 2;
compile_operand(vm, ws, MVM_operand_read_reg | MVM_operand_num64,
ATPOS(vm, c->args, arg_pos));
}
else {
unsigned int current_frame_idx = ws->current_frame_idx;
unsigned int current_ins_idx = ws->current_ins_idx;
const char *name = ws->current_op_info->name;
cleanup_all(vm, ws);
/*
DIE(vm, "At Frame %u, Instruction %u, op '%s', "
"file %s, line %u, unhandled arg type %u.",
current_frame_idx, current_ins_idx, name,
ws->last_annotated ? VM_STRING_TO_C_STRING(vm, ws->last_annotated->file) : "",
ws->last_annotated ? ws->last_annotated->line : 0,
flag);
*/
DIE(vm, "At Frame %u, Instruction %u, op '%s', unhandled arg type %u.",
current_frame_idx, current_ins_idx, name, flag);
}
arg_pos++;
}
/* Select operation based on return type. */
if (ISTYPE(vm, c->result, ws->types->Local)) {
MAST_Local *l = GET_Local(c->result);
/* Ensure it's within the set of known locals. */
if (l->index >= ws->cur_frame->num_locals) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local index out of range");
}
/* Go by type. */
switch (ws->cur_frame->local_types[l->index]) {
case MVM_reg_int64:
call_op = MVM_OP_invoke_i;
res_type = MVM_operand_int64;
break;
case MVM_reg_num64:
call_op = MVM_OP_invoke_n;
res_type = MVM_operand_num64;
break;
case MVM_reg_str:
call_op = MVM_OP_invoke_s;
res_type = MVM_operand_str;
break;
case MVM_reg_obj:
call_op = MVM_OP_invoke_o;
res_type = MVM_operand_obj;
break;
default:
cleanup_all(vm, ws);
DIE(vm, "Invalid MAST::Local type for return value");
}
}
/* Emit the invocation op. */
ensure_space(vm, &ws->bytecode_seg, &ws->bytecode_alloc, ws->bytecode_pos, 6);
write_int16(ws->bytecode_seg, ws->bytecode_pos, call_op);
ws->bytecode_pos += 2;
if (call_op != MVM_OP_invoke_v)
compile_operand(vm, ws, MVM_operand_read_reg | res_type, c->result);
compile_operand(vm, ws, MVM_operand_read_reg | MVM_operand_obj, c->target);
}
else if (ISTYPE(vm, node, ws->types->Annotated)) {
MAST_Annotated *a = GET_Annotated(node);
unsigned int i;
unsigned int num_ins = ELEMS(vm, a->instructions);
unsigned int offset = ws->bytecode_pos - ws->cur_frame->bytecode_start;
ws->last_annotated = a;
ensure_space(vm, &ws->annotation_seg, &ws->annotation_alloc, ws->annotation_pos, 12);
write_int32(ws->annotation_seg, ws->annotation_pos, offset);
write_int32(ws->annotation_seg, ws->annotation_pos + 4, get_string_heap_index(vm, ws, a->file));
write_int32(ws->annotation_seg, ws->annotation_pos + 8, (unsigned int)a->line);
ws->annotation_pos += 12;
ws->cur_frame->num_annotations++;
for (i = 0; i < num_ins; i++)
compile_instruction(vm, ws, ATPOS(vm, a->instructions, i));
}
else if (ISTYPE(vm, node, ws->types->HandlerScope)) {
MAST_HandlerScope *hs = GET_HandlerScope(node);
unsigned int i;
unsigned int num_ins = ELEMS(vm, hs->instructions);
unsigned int start = ws->bytecode_pos - ws->cur_frame->bytecode_start;
unsigned int end;
for (i = 0; i < num_ins; i++)
compile_instruction(vm, ws, ATPOS(vm, hs->instructions, i));
end = ws->bytecode_pos - ws->cur_frame->bytecode_start;
ws->cur_frame->num_handlers++;
if (ws->cur_frame->handlers)
ws->cur_frame->handlers = (FrameHandler *)realloc(ws->cur_frame->handlers,
ws->cur_frame->num_handlers * sizeof(FrameHandler));
else
ws->cur_frame->handlers = (FrameHandler *)malloc(
ws->cur_frame->num_handlers * sizeof(FrameHandler));
i = ws->cur_frame->num_handlers - 1;
ws->cur_frame->handlers[i].start_offset = start;
ws->cur_frame->handlers[i].end_offset = end;
ws->cur_frame->handlers[i].category_mask = (unsigned int)hs->category_mask;
ws->cur_frame->handlers[i].action = (unsigned short)hs->action;
if (ws->cur_frame->handlers[i].category_mask & MVM_EX_CAT_LABELED) {
if (ISTYPE(vm, hs->label_local, ws->types->Local)) {
MAST_Local *l = GET_Local(hs->label_local);
/* Ensure it's within the set of known locals and an object. */
if (l->index >= ws->cur_frame->num_locals) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local index out of range in HandlerScope");
}
if (ws->cur_frame->local_types[l->index] != MVM_reg_obj) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local for HandlerScope must be an object");
}
/* Stash local index. */
ws->cur_frame->handlers[i].label_reg = (unsigned short)l->index;
}
else {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local required for HandlerScope with loop label");
}
}
/* Ensure we have a label. */
if (ISTYPE(vm, hs->goto_label, ws->types->Label)) {
ws->cur_frame->handlers[i].label = hs->goto_label;
}
else {
cleanup_all(vm, ws);
DIE(vm, "MAST::Label required for HandlerScope goto");
}
/* May need a block also. */
if (hs->action == HANDLER_INVOKE) {
if (ISTYPE(vm, hs->block_local, ws->types->Local)) {
MAST_Local *l = GET_Local(hs->block_local);
/* Ensure it's within the set of known locals and an object. */
if (l->index >= ws->cur_frame->num_locals) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local index out of range in HandlerScope");
}
if (ws->cur_frame->local_types[l->index] != MVM_reg_obj) {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local for HandlerScope must be an object");
}
/* Stash local index. */
ws->cur_frame->handlers[i].local = (unsigned short)l->index;
}
else {
cleanup_all(vm, ws);
DIE(vm, "MAST::Local required for HandlerScope invoke action");
}
}
else if (hs->action == HANDLER_UNWIND_GOTO || hs->action == HANDLER_UNWIND_GOTO_OBJ) {
ws->cur_frame->handlers[i].local = 0;
}
else {
cleanup_all(vm, ws);
DIE(vm, "Invalid action code for handler scope");
}
}
else {
cleanup_all(vm, ws);
DIE(vm, "Invalid MAST node in instruction list (must be Op, ExtOp, Call, Label, or Annotated)");
}
ws->current_ins_idx++;
}
/* Compiles a frame. */
void compile_frame(VM, WriterState *ws, MASTNode *node, unsigned short idx) {
MAST_Frame *f;
FrameState *fs;
unsigned int i, num_ins, instructions_start;
MASTNode *last_inst = NULL;
/* Ensure we have a node of the right type. */
if (!ISTYPE(vm, node, ws->types->Frame)) {
cleanup_all(vm, ws);
DIE(vm, "Child of CompUnit must be a Frame");
}
f = GET_Frame(node);
/* Allocate frame state. */
fs = ws->cur_frame = (FrameState *)malloc(sizeof(FrameState));
fs->bytecode_start = ws->bytecode_pos;
fs->frame_start = ws->frame_pos;
fs->known_labels = NEWHASH(vm);
fs->labels_to_resolve = NEWHASH(vm);
/* Count locals and lexicals. */
fs->num_locals = ELEMS(vm, f->local_types);
fs->num_lexicals = ELEMS(vm, f->lexical_types);
if (fs->num_locals > (1 << 16)) {
cleanup_all(vm, ws);
DIE(vm, "Too many locals in this frame.");
}
if (ELEMS(vm, f->lexical_names) != fs->num_lexicals) {
cleanup_all(vm, ws);
DIE(vm, "Lexical types list and lexical names list have unequal length");
}
/* initialize number of annotation */
fs->num_annotations = 0;
/* initialize number of handlers and handlers pointer */
fs->num_handlers = 0;
fs->handlers = NULL;
/* Ensure space is available to write frame entry, and write the
* header, apart from the bytecode length, which we'll fill in
* later. */
ensure_space(vm, &ws->frame_seg, &ws->frame_alloc, ws->frame_pos,
FRAME_HEADER_SIZE + fs->num_locals * 2 + fs->num_lexicals * 6);
write_int32(ws->frame_seg, ws->frame_pos, fs->bytecode_start);
write_int32(ws->frame_seg, ws->frame_pos + 4, 0); /* Filled in later. */
write_int32(ws->frame_seg, ws->frame_pos + 8, fs->num_locals);
write_int32(ws->frame_seg, ws->frame_pos + 12, fs->num_lexicals);
write_int32(ws->frame_seg, ws->frame_pos + 16,
get_string_heap_index(vm, ws, f->cuuid));
write_int32(ws->frame_seg, ws->frame_pos + 20,
get_string_heap_index(vm, ws, f->name));
/* Handle outer. The current index means "no outer". */
if (ISTYPE(vm, f->outer, ws->types->Frame)) {
unsigned short i, found, num_frames;
found = 0;
num_frames = (unsigned short)ELEMS(vm, ws->cu->frames);