-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
prism_compile.c
4281 lines (3563 loc) · 163 KB
/
prism_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 "prism.h"
#define OLD_ISEQ NEW_ISEQ
#undef NEW_ISEQ
#define NEW_ISEQ(node, name, type, line_no) \
pm_new_child_iseq(iseq, (node), parser, rb_fstring(name), 0, (type), (line_no))
#define OLD_CHILD_ISEQ NEW_CHILD_ISEQ
#undef NEW_CHILD_ISEQ
#define NEW_CHILD_ISEQ(node, name, type, line_no) \
pm_new_child_iseq(iseq, (node), parser, rb_fstring(name), iseq, (type), (line_no))
#define PM_COMPILE(node) \
pm_compile_node(iseq, (node), ret, src, popped, scope_node)
#define PM_COMPILE_INTO_ANCHOR(_ret, node) \
pm_compile_node(iseq, (node), _ret, src, popped, scope_node)
#define PM_COMPILE_POPPED(node) \
pm_compile_node(iseq, (node), ret, src, true, scope_node)
#define PM_COMPILE_NOT_POPPED(node) \
pm_compile_node(iseq, (node), ret, src, false, scope_node)
#define PM_POP \
ADD_INSN(ret, &dummy_line_node, pop);
#define PM_POP_IF_POPPED \
if (popped) PM_POP;
#define PM_POP_UNLESS_POPPED \
if (!popped) PM_POP;
#define PM_DUP \
ADD_INSN(ret, &dummy_line_node, dup);
#define PM_DUP_UNLESS_POPPED \
if (!popped) PM_DUP;
#define PM_PUTSELF \
ADD_INSN(ret, &dummy_line_node, putself);
#define PM_PUTNIL \
ADD_INSN(ret, &dummy_line_node, putnil);
#define PM_PUTNIL_UNLESS_POPPED \
if (!popped) PM_PUTNIL;
#define PM_SWAP \
ADD_INSN(ret, &dummy_line_node, swap);
#define PM_SWAP_UNLESS_POPPED \
if (!popped) PM_SWAP;
/**
* We're using the top most bit of a pm_constant_id_t as a tag to represent an
* anonymous local. When a child iseq is created and needs access to a value
* that has yet to be defined, or is defined by the parent node's iseq. This can
* be added to it's local table and then handled accordingly when compiling the
* scope node associated with the child iseq.
*
* See the compilation process for PM_FOR_NODE: as an example, where the
* variable referenced inside the StatementsNode is defined as part of the top
* level ForLoop node.
*/
#define TEMP_CONSTANT_IDENTIFIER ((pm_constant_id_t)(1 << 31))
rb_iseq_t *
pm_iseq_new_with_opt(pm_scope_node_t *scope_node, pm_parser_t *parser, VALUE name, VALUE path, VALUE realpath,
int first_lineno, const rb_iseq_t *parent, int isolated_depth,
enum rb_iseq_type type, const rb_compile_option_t *option);
static VALUE
parse_integer(const pm_integer_node_t *node)
{
const char *start = (const char *) node->base.location.start;
const char *end = (const char *) node->base.location.end;
size_t length = end - start;
int base = -10;
switch (node->base.flags & (PM_INTEGER_BASE_FLAGS_BINARY | PM_INTEGER_BASE_FLAGS_DECIMAL | PM_INTEGER_BASE_FLAGS_OCTAL | PM_INTEGER_BASE_FLAGS_HEXADECIMAL)) {
case PM_INTEGER_BASE_FLAGS_BINARY:
base = 2;
break;
case PM_INTEGER_BASE_FLAGS_DECIMAL:
base = 10;
break;
case PM_INTEGER_BASE_FLAGS_OCTAL:
base = 8;
break;
case PM_INTEGER_BASE_FLAGS_HEXADECIMAL:
base = 16;
break;
default:
rb_bug("Unexpected integer base");
}
return rb_int_parse_cstr(start, length, NULL, NULL, base, RB_INT_PARSE_DEFAULT);
}
static VALUE
parse_float(const pm_node_t *node)
{
const uint8_t *start = node->location.start;
const uint8_t *end = node->location.end;
size_t length = end - start;
char *buffer = malloc(length + 1);
memcpy(buffer, start, length);
buffer[length] = '\0';
VALUE number = DBL2NUM(rb_cstr_to_dbl(buffer, 0));
free(buffer);
return number;
}
static VALUE
parse_rational(const pm_node_t *node)
{
const uint8_t *start = node->location.start;
const uint8_t *end = node->location.end - 1;
size_t length = end - start;
VALUE res;
if (PM_NODE_TYPE_P(((pm_rational_node_t *)node)->numeric, PM_FLOAT_NODE)) {
char *buffer = malloc(length + 1);
memcpy(buffer, start, length);
buffer[length] = '\0';
char *decimal = memchr(buffer, '.', length);
RUBY_ASSERT(decimal);
size_t seen_decimal = decimal - buffer;
size_t fraclen = length - seen_decimal - 1;
memmove(decimal, decimal + 1, fraclen + 1);
VALUE v = rb_cstr_to_inum(buffer, 10, false);
res = rb_rational_new(v, rb_int_positive_pow(10, fraclen));
free(buffer);
}
else {
RUBY_ASSERT(PM_NODE_TYPE_P(((pm_rational_node_t *)node)->numeric, PM_INTEGER_NODE));
VALUE number = rb_int_parse_cstr((const char *)start, length, NULL, NULL, -10, RB_INT_PARSE_DEFAULT);
res = rb_rational_raw(number, INT2FIX(1));
}
return res;
}
static VALUE
parse_imaginary(pm_imaginary_node_t *node)
{
VALUE imaginary_part;
switch (PM_NODE_TYPE(node->numeric)) {
case PM_FLOAT_NODE: {
imaginary_part = parse_float(node->numeric);
break;
}
case PM_INTEGER_NODE: {
imaginary_part = parse_integer((pm_integer_node_t *) node->numeric);
break;
}
case PM_RATIONAL_NODE: {
imaginary_part = parse_rational(node->numeric);
break;
}
default:
rb_bug("Unexpected numeric type on imaginary number");
}
return rb_complex_raw(INT2FIX(0), imaginary_part);
}
static inline VALUE
parse_string(pm_string_t *string, const pm_parser_t *parser)
{
rb_encoding *enc = rb_enc_from_index(rb_enc_find_index(parser->encoding.name));
return rb_enc_str_new((const char *) pm_string_source(string), pm_string_length(string), enc);
}
static inline ID
parse_symbol(const uint8_t *start, const uint8_t *end, pm_parser_t *parser)
{
rb_encoding *enc = rb_enc_from_index(rb_enc_find_index(parser->encoding.name));
return rb_intern3((const char *) start, end - start, enc);
}
static inline ID
parse_string_symbol(pm_string_t *string, pm_parser_t *parser)
{
const uint8_t *start = pm_string_source(string);
return parse_symbol(start, start + pm_string_length(string), parser);
}
static inline ID
parse_location_symbol(pm_location_t *location, pm_parser_t *parser)
{
return parse_symbol(location->start, location->end, parser);
}
static int
pm_optimizable_range_item_p(pm_node_t *node)
{
return (!node || PM_NODE_TYPE_P(node, PM_INTEGER_NODE) || PM_NODE_TYPE_P(node, PM_NIL_NODE));
}
#define RE_OPTION_ENCODING_SHIFT 8
/**
* Check the prism flags of a regular expression-like node and return the flags
* that are expected by the CRuby VM.
*/
static int
pm_reg_flags(const pm_node_t *node) {
int flags = 0;
int dummy = 0;
// Check "no encoding" first so that flags don't get clobbered
// We're calling `rb_char_to_option_kcode` in this case so that
// we don't need to have access to `ARG_ENCODING_NONE`
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT) {
rb_char_to_option_kcode('n', &flags, &dummy);
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_EUC_JP) {
rb_char_to_option_kcode('e', &flags, &dummy);
flags |= ('e' << RE_OPTION_ENCODING_SHIFT);
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J) {
rb_char_to_option_kcode('s', &flags, &dummy);
flags |= ('s' << RE_OPTION_ENCODING_SHIFT);
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_UTF_8) {
rb_char_to_option_kcode('u', &flags, &dummy);
flags |= ('u' << RE_OPTION_ENCODING_SHIFT);
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_IGNORE_CASE) {
flags |= ONIG_OPTION_IGNORECASE;
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_MULTI_LINE) {
flags |= ONIG_OPTION_MULTILINE;
}
if (node->flags & PM_REGULAR_EXPRESSION_FLAGS_EXTENDED) {
flags |= ONIG_OPTION_EXTEND;
}
return flags;
}
static rb_encoding *
pm_reg_enc(const pm_regular_expression_node_t *node, const pm_parser_t *parser) {
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_ASCII_8BIT) {
return rb_ascii8bit_encoding();
}
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_EUC_JP) {
return rb_enc_get_from_index(ENCINDEX_EUC_JP);
}
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_WINDOWS_31J) {
return rb_enc_get_from_index(ENCINDEX_Windows_31J);
}
if (node->base.flags & PM_REGULAR_EXPRESSION_FLAGS_UTF_8) {
return rb_utf8_encoding();
}
return rb_enc_from_index(rb_enc_find_index(parser->encoding.name));
}
/**
* Certain nodes can be compiled literally, which can lead to further
* optimizations. These nodes will all have the PM_NODE_FLAG_STATIC_LITERAL flag
* set.
*/
static inline bool
pm_static_literal_p(const pm_node_t *node)
{
return node->flags & PM_NODE_FLAG_STATIC_LITERAL;
}
static VALUE
pm_new_regex(pm_regular_expression_node_t * cast, const pm_parser_t * parser) {
VALUE regex_str = parse_string(&cast->unescaped, parser);
rb_encoding * enc = pm_reg_enc(cast, parser);
return rb_enc_reg_new(RSTRING_PTR(regex_str), RSTRING_LEN(regex_str), enc, pm_reg_flags((const pm_node_t *)cast));
}
/**
* Certain nodes can be compiled literally. This function returns the literal
* value described by the given node. For example, an array node with all static
* literal values can be compiled into a literal array.
*/
static inline VALUE
pm_static_literal_value(const pm_node_t *node, pm_scope_node_t *scope_node, pm_parser_t *parser)
{
// Every node that comes into this function should already be marked as
// static literal. If it's not, then we have a bug somewhere.
assert(pm_static_literal_p(node));
switch (PM_NODE_TYPE(node)) {
case PM_ARRAY_NODE: {
pm_array_node_t *cast = (pm_array_node_t *) node;
pm_node_list_t *elements = &cast->elements;
VALUE value = rb_ary_hidden_new(elements->size);
for (size_t index = 0; index < elements->size; index++) {
rb_ary_push(value, pm_static_literal_value(elements->nodes[index], scope_node, parser));
}
OBJ_FREEZE(value);
return value;
}
case PM_FALSE_NODE:
return Qfalse;
case PM_FLOAT_NODE:
return parse_float(node);
case PM_HASH_NODE: {
pm_hash_node_t *cast = (pm_hash_node_t *) node;
pm_node_list_t *elements = &cast->elements;
VALUE array = rb_ary_hidden_new(elements->size * 2);
for (size_t index = 0; index < elements->size; index++) {
assert(PM_NODE_TYPE_P(elements->nodes[index], PM_ASSOC_NODE));
pm_assoc_node_t *cast = (pm_assoc_node_t *) elements->nodes[index];
VALUE pair[2] = { pm_static_literal_value(cast->key, scope_node, parser), pm_static_literal_value(cast->value, scope_node, parser) };
rb_ary_cat(array, pair, 2);
}
VALUE value = rb_hash_new_with_size(elements->size);
rb_hash_bulk_insert(RARRAY_LEN(array), RARRAY_CONST_PTR(array), value);
value = rb_obj_hide(value);
OBJ_FREEZE(value);
return value;
}
case PM_IMAGINARY_NODE:
return parse_imaginary((pm_imaginary_node_t *) node);
case PM_INTEGER_NODE:
return parse_integer((pm_integer_node_t *) node);
case PM_NIL_NODE:
return Qnil;
case PM_RATIONAL_NODE:
return parse_rational(node);
case PM_REGULAR_EXPRESSION_NODE: {
pm_regular_expression_node_t *cast = (pm_regular_expression_node_t *) node;
return pm_new_regex(cast, parser);
}
case PM_SOURCE_ENCODING_NODE: {
rb_encoding *encoding = rb_find_encoding(rb_str_new_cstr(scope_node->parser->encoding.name));
if (!encoding) rb_bug("Encoding not found!");
return rb_enc_from_encoding(encoding);
}
case PM_SOURCE_FILE_NODE: {
pm_source_file_node_t *cast = (pm_source_file_node_t *)node;
return cast->filepath.length ? parse_string(&cast->filepath, parser) : rb_fstring_lit("<compiled>");
}
case PM_SOURCE_LINE_NODE: {
int source_line = (int) pm_newline_list_line_column(&scope_node->parser->newline_list, node->location.start).line;
// Ruby treats file lines as 1-indexed
// TODO: Incorporate options which allow for passing a line number
source_line += 1;
return INT2FIX(source_line);
}
case PM_STRING_NODE:
return parse_string(&((pm_string_node_t *) node)->unescaped, parser);
case PM_SYMBOL_NODE:
return ID2SYM(parse_string_symbol(&((pm_symbol_node_t *) node)->unescaped, parser));
case PM_TRUE_NODE:
return Qtrue;
default:
rb_raise(rb_eArgError, "Don't have a literal value for this type");
return Qfalse;
}
}
static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
static void
pm_compile_logical(rb_iseq_t *iseq, LINK_ANCHOR *const ret, pm_node_t *cond,
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
{
pm_parser_t *parser = scope_node->parser;
pm_newline_list_t newline_list = parser->newline_list;
int lineno = (int)pm_newline_list_line_column(&newline_list, cond->location.start).line;
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
DECL_ANCHOR(seq);
INIT_ANCHOR(seq);
LABEL *label = NEW_LABEL(lineno);
if (!then_label) then_label = label;
else if (!else_label) else_label = label;
pm_compile_branch_condition(iseq, seq, cond, then_label, else_label, src, popped, scope_node);
if (LIST_INSN_SIZE_ONE(seq)) {
INSN *insn = (INSN *)ELEM_FIRST_INSN(FIRST_ELEMENT(seq));
if (insn->insn_id == BIN(jump) && (LABEL *)(insn->operands[0]) == label)
return;
}
if (!label->refcnt) {
PM_PUTNIL;
}
else {
ADD_LABEL(seq, label);
}
ADD_SEQ(ret, seq);
return;
}
static void pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node);
static void
pm_compile_flip_flop(pm_flip_flop_node_t *flip_flop_node, LABEL *else_label, LABEL *then_label, rb_iseq_t *iseq, const int lineno, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
{
NODE dummy_line_node = generate_dummy_line_node(ISEQ_BODY(iseq)->location.first_lineno, -1);
LABEL *lend = NEW_LABEL(lineno);
int again = !(flip_flop_node->base.flags & PM_RANGE_FLAGS_EXCLUDE_END);
rb_num_t count = ISEQ_FLIP_CNT_INCREMENT(ISEQ_BODY(iseq)->local_iseq) + VM_SVAR_FLIPFLOP_START;
VALUE key = INT2FIX(count);
ADD_INSN2(ret, &dummy_line_node, getspecial, key, INT2FIX(0));
ADD_INSNL(ret, &dummy_line_node, branchif, lend);
if (flip_flop_node->left) {
PM_COMPILE(flip_flop_node->left);
}
else {
PM_PUTNIL;
}
ADD_INSNL(ret, &dummy_line_node, branchunless, else_label);
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
if (!again) {
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
}
ADD_LABEL(ret, lend);
if (flip_flop_node->right) {
PM_COMPILE(flip_flop_node->right);
}
else {
PM_PUTNIL;
}
ADD_INSNL(ret, &dummy_line_node, branchunless, then_label);
ADD_INSN1(ret, &dummy_line_node, putobject, Qfalse);
ADD_INSN1(ret, &dummy_line_node, setspecial, key);
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
}
void pm_compile_defined_expr(rb_iseq_t *iseq, const pm_node_t *defined_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, int lineno, bool in_condition);
static void
pm_compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const pm_node_t *cond,
LABEL *then_label, LABEL *else_label, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
{
pm_parser_t *parser = scope_node->parser;
pm_newline_list_t newline_list = parser->newline_list;
int lineno = (int) pm_newline_list_line_column(&newline_list, cond->location.start).line;
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
again:
switch (PM_NODE_TYPE(cond)) {
case PM_AND_NODE: {
pm_and_node_t *and_node = (pm_and_node_t *)cond;
pm_compile_logical(iseq, ret, and_node->left, NULL, else_label, src, popped, scope_node);
cond = and_node->right;
goto again;
}
case PM_OR_NODE: {
pm_or_node_t *or_node = (pm_or_node_t *)cond;
pm_compile_logical(iseq, ret, or_node->left, then_label, NULL, src, popped, scope_node);
cond = or_node->right;
goto again;
}
case PM_FALSE_NODE:
case PM_NIL_NODE:
ADD_INSNL(ret, &dummy_line_node, jump, else_label);
return;
case PM_FLOAT_NODE:
case PM_IMAGINARY_NODE:
case PM_INTEGER_NODE:
case PM_LAMBDA_NODE:
case PM_RATIONAL_NODE:
case PM_REGULAR_EXPRESSION_NODE:
case PM_STRING_NODE:
case PM_SYMBOL_NODE:
case PM_TRUE_NODE:
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
return;
case PM_FLIP_FLOP_NODE:
pm_compile_flip_flop((pm_flip_flop_node_t *)cond, else_label, then_label, iseq, lineno, ret, src, popped, scope_node);
return;
// TODO: Several more nodes in this case statement
case PM_DEFINED_NODE: {
pm_defined_node_t *defined_node = (pm_defined_node_t *)cond;
pm_compile_defined_expr(iseq, defined_node->value, ret, src, popped, scope_node, dummy_line_node, lineno, true);
break;
}
default: {
DECL_ANCHOR(cond_seq);
INIT_ANCHOR(cond_seq);
pm_compile_node(iseq, cond, cond_seq, src, false, scope_node);
ADD_SEQ(ret, cond_seq);
break;
}
}
ADD_INSNL(ret, &dummy_line_node, branchunless, else_label);
ADD_INSNL(ret, &dummy_line_node, jump, then_label);
return;
}
static void
pm_compile_if(rb_iseq_t *iseq, const int line, pm_statements_node_t *node_body, pm_node_t *node_else, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
{
NODE dummy_line_node = generate_dummy_line_node(line, line);
DECL_ANCHOR(cond_seq);
LABEL *then_label, *else_label, *end_label;
INIT_ANCHOR(cond_seq);
then_label = NEW_LABEL(line);
else_label = NEW_LABEL(line);
end_label = 0;
pm_compile_branch_condition(iseq, cond_seq, predicate, then_label, else_label, src, false, scope_node);
ADD_SEQ(ret, cond_seq);
if (then_label->refcnt) {
ADD_LABEL(ret, then_label);
DECL_ANCHOR(then_seq);
INIT_ANCHOR(then_seq);
if (node_body) {
pm_compile_node(iseq, (pm_node_t *)node_body, then_seq, src, popped, scope_node);
} else {
PM_PUTNIL_UNLESS_POPPED;
}
if (else_label->refcnt) {
end_label = NEW_LABEL(line);
ADD_INSNL(then_seq, &dummy_line_node, jump, end_label);
if (!popped) {
ADD_INSN(then_seq, &dummy_line_node, pop);
}
}
ADD_SEQ(ret, then_seq);
}
if (else_label->refcnt) {
ADD_LABEL(ret, else_label);
DECL_ANCHOR(else_seq);
INIT_ANCHOR(else_seq);
if (node_else) {
pm_compile_node(iseq, (pm_node_t *)node_else, else_seq, src, popped, scope_node);
}
else {
PM_PUTNIL_UNLESS_POPPED;
}
ADD_SEQ(ret, else_seq);
}
if (end_label) {
ADD_LABEL(ret, end_label);
}
return;
}
static void
pm_compile_while(rb_iseq_t *iseq, int lineno, pm_node_flags_t flags, enum pm_node_type type, pm_statements_node_t *statements, pm_node_t *predicate, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node)
{
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
LABEL *prev_start_label = ISEQ_COMPILE_DATA(iseq)->start_label;
LABEL *prev_end_label = ISEQ_COMPILE_DATA(iseq)->end_label;
LABEL *prev_redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label;
// TODO: Deal with ensures in here
LABEL *next_label = ISEQ_COMPILE_DATA(iseq)->start_label = NEW_LABEL(lineno); /* next */
LABEL *redo_label = ISEQ_COMPILE_DATA(iseq)->redo_label = NEW_LABEL(lineno); /* redo */
LABEL *break_label = ISEQ_COMPILE_DATA(iseq)->end_label = NEW_LABEL(lineno); /* break */
LABEL *end_label = NEW_LABEL(lineno);
LABEL *adjust_label = NEW_LABEL(lineno);
LABEL *next_catch_label = NEW_LABEL(lineno);
LABEL *tmp_label = NULL;
// begin; end while true
if (flags & PM_LOOP_FLAGS_BEGIN_MODIFIER) {
tmp_label = NEW_LABEL(lineno);
ADD_INSNL(ret, &dummy_line_node, jump, tmp_label);
}
else {
// while true; end
ADD_INSNL(ret, &dummy_line_node, jump, next_label);
}
ADD_LABEL(ret, adjust_label);
PM_PUTNIL;
ADD_LABEL(ret, next_catch_label);
PM_POP;
ADD_INSNL(ret, &dummy_line_node, jump, next_label);
if (tmp_label) ADD_LABEL(ret, tmp_label);
ADD_LABEL(ret, redo_label);
if (statements) {
PM_COMPILE_POPPED((pm_node_t *)statements);
}
ADD_LABEL(ret, next_label);
if (type == PM_WHILE_NODE) {
pm_compile_branch_condition(iseq, ret, predicate, redo_label, end_label, src, popped, scope_node);
} else if (type == PM_UNTIL_NODE) {
pm_compile_branch_condition(iseq, ret, predicate, end_label, redo_label, src, popped, scope_node);
}
ADD_LABEL(ret, end_label);
ADD_ADJUST_RESTORE(ret, adjust_label);
PM_PUTNIL;
ADD_LABEL(ret, break_label);
PM_POP_IF_POPPED;
ADD_CATCH_ENTRY(CATCH_TYPE_BREAK, redo_label, break_label, NULL,
break_label);
ADD_CATCH_ENTRY(CATCH_TYPE_NEXT, redo_label, break_label, NULL,
next_catch_label);
ADD_CATCH_ENTRY(CATCH_TYPE_REDO, redo_label, break_label, NULL,
ISEQ_COMPILE_DATA(iseq)->redo_label);
ISEQ_COMPILE_DATA(iseq)->start_label = prev_start_label;
ISEQ_COMPILE_DATA(iseq)->end_label = prev_end_label;
ISEQ_COMPILE_DATA(iseq)->redo_label = prev_redo_label;
return;
}
static void
pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_line_node, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, pm_parser_t *parser)
{
size_t parts_size = parts->size;
if (parts_size > 0) {
for (size_t index = 0; index < parts_size; index++) {
pm_node_t *part = parts->nodes[index];
if (PM_NODE_TYPE_P(part, PM_STRING_NODE)) {
pm_string_node_t *string_node = (pm_string_node_t *) part;
ADD_INSN1(ret, &dummy_line_node, putobject, parse_string(&string_node->unescaped, parser));
}
else {
PM_COMPILE_NOT_POPPED(part);
PM_DUP;
ADD_INSN1(ret, &dummy_line_node, objtostring, new_callinfo(iseq, idTo_s, 0, VM_CALL_FCALL | VM_CALL_ARGS_SIMPLE , NULL, FALSE));
ADD_INSN(ret, &dummy_line_node, anytostring);
}
}
}
else {
PM_PUTNIL;
}
}
// This recurses through scopes and finds the local index at any scope level
// It also takes a pointer to depth, and increments depth appropriately
// according to the depth of the local
static int
pm_lookup_local_index_any_scope(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
{
if (!scope_node) {
// We have recursed up all scope nodes
// and have not found the local yet
rb_bug("This local does not exist");
}
st_data_t local_index;
if (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
// Local does not exist at this level, continue recursing up
return pm_lookup_local_index_any_scope(iseq, scope_node->previous, constant_id);
}
return (int)scope_node->index_lookup_table->num_entries - (int)local_index;
}
static int
pm_lookup_local_index(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
{
st_data_t local_index;
int locals_size = (int) scope_node->locals.size;
if (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
rb_bug("This local does not exist");
}
return locals_size - (int)local_index;
}
static int
pm_lookup_local_index_with_depth(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, uint32_t depth)
{
for(uint32_t i = 0; i < depth; i++) {
scope_node = scope_node->previous;
iseq = (rb_iseq_t *)ISEQ_BODY(iseq)->parent_iseq;
}
return pm_lookup_local_index_any_scope(iseq, scope_node, constant_id);
}
// This returns the CRuby ID which maps to the pm_constant_id_t
//
// Constant_ids in prism are indexes of the constants in prism's constant pool.
// We add a constants mapping on the scope_node which is a mapping from
// these constant_id indexes to the CRuby IDs that they represent.
// This helper method allows easy access to those IDs
static ID
pm_constant_id_lookup(pm_scope_node_t *scope_node, pm_constant_id_t constant_id)
{
if (constant_id < 1 || constant_id > scope_node->parser->constant_pool.size) {
rb_raise(rb_eArgError, "[PRISM] constant_id out of range: %u", (unsigned int)constant_id);
}
return scope_node->constants[constant_id - 1];
}
static rb_iseq_t *
pm_new_child_iseq(rb_iseq_t *iseq, pm_scope_node_t node, pm_parser_t *parser,
VALUE name, const rb_iseq_t *parent, enum rb_iseq_type type, int line_no)
{
debugs("[new_child_iseq]> ---------------------------------------\n");
int isolated_depth = ISEQ_COMPILE_DATA(iseq)->isolated_depth;
rb_iseq_t * ret_iseq = pm_iseq_new_with_opt(&node, parser, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
line_no, parent,
isolated_depth ? isolated_depth + 1 : 0,
type, ISEQ_COMPILE_DATA(iseq)->option);
debugs("[new_child_iseq]< ---------------------------------------\n");
return ret_iseq;
}
static int
pm_compile_class_path(LINK_ANCHOR *const ret, rb_iseq_t *iseq, const pm_node_t *constant_path_node, const NODE *line_node, const uint8_t * src, bool popped, pm_scope_node_t *scope_node)
{
if (PM_NODE_TYPE_P(constant_path_node, PM_CONSTANT_PATH_NODE)) {
pm_node_t *parent = ((pm_constant_path_node_t *)constant_path_node)->parent;
if (parent) {
/* Bar::Foo */
PM_COMPILE(parent);
return VM_DEFINECLASS_FLAG_SCOPED;
}
else {
/* toplevel class ::Foo */
ADD_INSN1(ret, line_node, putobject, rb_cObject);
return VM_DEFINECLASS_FLAG_SCOPED;
}
}
else {
/* class at cbase Foo */
ADD_INSN1(ret, line_node, putspecialobject,
INT2FIX(VM_SPECIAL_OBJECT_CONST_BASE));
return 0;
}
}
static void
pm_compile_call_and_or_write_node(bool and_node, pm_node_t *receiver, pm_node_t *value, pm_constant_id_t write_name, pm_constant_id_t read_name, LINK_ANCHOR *const ret, rb_iseq_t *iseq, int lineno, const uint8_t * src, bool popped, pm_scope_node_t *scope_node)
{
LABEL *call_end_label = NEW_LABEL(lineno);
LABEL *end_label = NEW_LABEL(lineno);
NODE dummy_line_node = generate_dummy_line_node(lineno, lineno);
int flag = 0;
if (PM_NODE_TYPE_P(receiver, PM_SELF_NODE)) {
flag = VM_CALL_FCALL;
}
PM_COMPILE_NOT_POPPED(receiver);
ID write_name_id = pm_constant_id_lookup(scope_node, write_name);
ID read_name_id = pm_constant_id_lookup(scope_node, read_name);
PM_DUP;
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, read_name_id, INT2FIX(0), INT2FIX(flag));
PM_DUP_UNLESS_POPPED;
if (and_node) {
ADD_INSNL(ret, &dummy_line_node, branchunless, call_end_label);
}
else {
// or_node
ADD_INSNL(ret, &dummy_line_node, branchif, call_end_label);
}
PM_POP_UNLESS_POPPED;
PM_COMPILE_NOT_POPPED(value);
if (!popped) {
PM_SWAP;
ADD_INSN1(ret, &dummy_line_node, topn, INT2FIX(1));
}
ID aid = rb_id_attrset(write_name_id);
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, aid, INT2FIX(1), INT2FIX(flag));
ADD_INSNL(ret, &dummy_line_node, jump, end_label);
ADD_LABEL(ret, call_end_label);
if (!popped) {
PM_SWAP;
}
ADD_LABEL(ret, end_label);
PM_POP;
return;
}
static void
pm_compile_index_write_nodes_add_send(bool popped, LINK_ANCHOR *const ret, rb_iseq_t *iseq, NODE dummy_line_node, VALUE argc, int flag, int block_offset)
{
if (!popped) {
ADD_INSN1(ret, &dummy_line_node, setn, FIXNUM_INC(argc, 2 + block_offset));
}
if (flag & VM_CALL_ARGS_SPLAT) {
ADD_INSN1(ret, &dummy_line_node, newarray, INT2FIX(1));
if (block_offset > 0) {
ADD_INSN1(ret, &dummy_line_node, dupn, INT2FIX(3));
PM_SWAP;
PM_POP;
}
ADD_INSN(ret, &dummy_line_node, concatarray);
if (block_offset > 0) {
ADD_INSN1(ret, &dummy_line_node, setn, INT2FIX(3));
PM_POP;
}
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idASET, argc, INT2FIX(flag));
}
else {
if (block_offset > 0) {
PM_SWAP;
}
ADD_SEND_WITH_FLAG(ret, &dummy_line_node, idASET, FIXNUM_INC(argc, 1), INT2FIX(flag));
}
PM_POP;
return;
}
static int
pm_setup_args(pm_arguments_node_t *arguments_node, int *flags, struct rb_callinfo_kwarg **kw_arg, rb_iseq_t *iseq, LINK_ANCHOR *const ret, const uint8_t *src, bool popped, pm_scope_node_t *scope_node, NODE dummy_line_node, pm_parser_t *parser)
{
int orig_argc = 0;
if (arguments_node == NULL) {
if (*flags & VM_CALL_FCALL) {
*flags |= VM_CALL_VCALL;
}
}
else {
pm_node_list_t arguments_node_list = arguments_node->arguments;
bool has_keyword_splat = (arguments_node->base.flags & PM_ARGUMENTS_NODE_FLAGS_CONTAINS_KEYWORD_SPLAT);
bool has_splat = false;
// We count the number of elements post the splat node that are not keyword elements to
// eventually pass as an argument to newarray
int post_splat_counter = 0;
for (size_t index = 0; index < arguments_node_list.size; index++) {
pm_node_t *argument = arguments_node_list.nodes[index];
switch (PM_NODE_TYPE(argument)) {
// A keyword hash node contains all keyword arguments as AssocNodes and AssocSplatNodes
case PM_KEYWORD_HASH_NODE: {
pm_keyword_hash_node_t *keyword_arg = (pm_keyword_hash_node_t *)argument;
size_t len = keyword_arg->elements.size;
if (has_keyword_splat) {
int cur_hash_size = 0;
orig_argc++;
bool new_hash_emitted = false;
for (size_t i = 0; i < len; i++) {
pm_node_t *cur_node = keyword_arg->elements.nodes[i];
pm_node_type_t cur_type = PM_NODE_TYPE(cur_node);
switch (PM_NODE_TYPE(cur_node)) {
case PM_ASSOC_NODE: {
pm_assoc_node_t *assoc = (pm_assoc_node_t *)cur_node;
PM_COMPILE_NOT_POPPED(assoc->key);
PM_COMPILE_NOT_POPPED(assoc->value);
cur_hash_size++;
// If we're at the last keyword arg, or the last assoc node of this "set",
// then we want to either construct a newhash or merge onto previous hashes
if (i == (len - 1) || !PM_NODE_TYPE_P(keyword_arg->elements.nodes[i + 1], cur_type)) {
if (new_hash_emitted) {
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_ptr, INT2FIX(cur_hash_size * 2 + 1));
}
else {
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(cur_hash_size * 2));
cur_hash_size = 0;
new_hash_emitted = true;
}
}
break;
}
case PM_ASSOC_SPLAT_NODE: {
if (len > 1) {
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
if (i == 0) {
ADD_INSN1(ret, &dummy_line_node, newhash, INT2FIX(0));
new_hash_emitted = true;
}
else {
PM_SWAP;
}
*flags |= VM_CALL_KW_SPLAT_MUT;
}
pm_assoc_splat_node_t *assoc_splat = (pm_assoc_splat_node_t *)cur_node;
PM_COMPILE_NOT_POPPED(assoc_splat->value);
*flags |= VM_CALL_KW_SPLAT;
if (len > 1) {
ADD_SEND(ret, &dummy_line_node, id_core_hash_merge_kwd, INT2FIX(2));
}
if ((i < len - 1) && !PM_NODE_TYPE_P(keyword_arg->elements.nodes[i + 1], cur_type)) {
ADD_INSN1(ret, &dummy_line_node, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
PM_SWAP;
}
cur_hash_size = 0;
break;
}
default: {
rb_bug("Unknown type");
}
}
}
break;
}
else {
*kw_arg = rb_xmalloc_mul_add(len, sizeof(VALUE), sizeof(struct rb_callinfo_kwarg));
*flags = VM_CALL_KWARG;
(*kw_arg)->keyword_len = (int) len;
// TODO: Method callers like `foo(a => b)`
for (size_t i = 0; i < len; i++) {
pm_assoc_node_t *assoc = (pm_assoc_node_t *)keyword_arg->elements.nodes[i];
(*kw_arg)->keywords[i] = pm_static_literal_value(assoc->key, scope_node, parser);
PM_COMPILE_NOT_POPPED(assoc->value);
}
}
break;
}
case PM_SPLAT_NODE: {
*flags |= VM_CALL_ARGS_SPLAT;
pm_splat_node_t *splat_node = (pm_splat_node_t *)argument;
if (splat_node->expression) {
orig_argc++;
PM_COMPILE_NOT_POPPED(splat_node->expression);
}
ADD_INSN1(ret, &dummy_line_node, splatarray, popped ? Qfalse : Qtrue);
has_splat = true;
post_splat_counter = 0;
break;
}