forked from jruby/jruby
/
IR_Builder.java
2894 lines (2580 loc) · 121 KB
/
IR_Builder.java
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
package org.jruby.compiler.ir;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
import java.util.Map;
import java.util.Date;
import org.jruby.Ruby;
import org.jruby.ast.AliasNode;
import org.jruby.ast.AndNode;
import org.jruby.ast.ArgsNode;
import org.jruby.ast.ArgumentNode;
import org.jruby.ast.ArrayNode;
import org.jruby.ast.AttrAssignNode;
import org.jruby.ast.BackRefNode;
import org.jruby.ast.BeginNode;
import org.jruby.ast.BignumNode;
import org.jruby.ast.BlockNode;
import org.jruby.ast.BreakNode;
import org.jruby.ast.CallNode;
import org.jruby.ast.ClassVarAsgnNode;
import org.jruby.ast.ClassVarNode;
import org.jruby.ast.Colon2Node;
import org.jruby.ast.Colon3Node;
import org.jruby.ast.ConstDeclNode;
import org.jruby.ast.ConstNode;
import org.jruby.ast.DAsgnNode;
import org.jruby.ast.DStrNode;
import org.jruby.ast.DVarNode;
import org.jruby.ast.DotNode;
import org.jruby.ast.EvStrNode;
import org.jruby.ast.EnsureNode;
import org.jruby.ast.FCallNode;
import org.jruby.ast.FixnumNode;
import org.jruby.ast.FloatNode;
import org.jruby.ast.GlobalAsgnNode;
import org.jruby.ast.GlobalVarNode;
import org.jruby.ast.HashNode;
import org.jruby.ast.IfNode;
import org.jruby.ast.InstAsgnNode;
import org.jruby.ast.InstVarNode;
import org.jruby.ast.IterNode;
import org.jruby.ast.ListNode;
import org.jruby.ast.LocalAsgnNode;
import org.jruby.ast.LocalVarNode;
import org.jruby.ast.Match2Node;
import org.jruby.ast.Match3Node;
import org.jruby.ast.MatchNode;
import org.jruby.ast.MethodDefNode;
import org.jruby.ast.NewlineNode;
import org.jruby.ast.NextNode;
import org.jruby.ast.Node;
import org.jruby.ast.NodeType;
import org.jruby.ast.NotNode;
import org.jruby.ast.NthRefNode;
import org.jruby.ast.OpAsgnAndNode;
import org.jruby.ast.OpAsgnOrNode;
import org.jruby.ast.OrNode;
import org.jruby.ast.RegexpNode;
import org.jruby.ast.ReturnNode;
import org.jruby.ast.RootNode;
import org.jruby.ast.SClassNode;
import org.jruby.ast.SValueNode;
import org.jruby.ast.SplatNode;
import org.jruby.ast.StrNode;
import org.jruby.ast.SuperNode;
import org.jruby.ast.SymbolNode;
import org.jruby.ast.VCallNode;
import org.jruby.ast.WhileNode;
import org.jruby.ast.YieldNode;
import org.jruby.ast.ArgsCatNode;
import org.jruby.ast.ArgsPushNode;
import org.jruby.ast.BlockPassNode;
import org.jruby.ast.CaseNode;
import org.jruby.ast.ClassNode;
import org.jruby.ast.ClassVarDeclNode;
import org.jruby.ast.Colon2ConstNode;
import org.jruby.ast.Colon2MethodNode;
import org.jruby.ast.DRegexpNode;
import org.jruby.ast.RescueNode;
import org.jruby.ast.RescueBodyNode;
import org.jruby.ast.DXStrNode;
import org.jruby.ast.ForNode;
import org.jruby.ast.ModuleNode;
import org.jruby.ast.MultipleAsgnNode;
import org.jruby.ast.OpAsgnNode;
import org.jruby.ast.OpElementAsgnNode;
import org.jruby.ast.SelfNode;
import org.jruby.ast.StarNode;
import org.jruby.ast.ToAryNode;
import org.jruby.ast.TypedArgumentNode;
import org.jruby.ast.UntilNode;
import org.jruby.ast.WhenNode;
import org.jruby.ast.XStrNode;
import org.jruby.ast.ZSuperNode;
import org.jruby.compiler.NotCompilableException;
import org.jruby.compiler.ir.instructions.ATTR_ASSIGN_Instr;
import org.jruby.compiler.ir.instructions.BEQ_Instr;
import org.jruby.compiler.ir.instructions.BREAK_Instr;
import org.jruby.compiler.ir.instructions.CallInstruction;
import org.jruby.compiler.ir.instructions.CASE_Instr;
import org.jruby.compiler.ir.instructions.CLOSURE_RETURN_Instr;
import org.jruby.compiler.ir.instructions.COPY_Instr;
import org.jruby.compiler.ir.instructions.DECLARE_LOCAL_TYPE_Instr;
import org.jruby.compiler.ir.instructions.EQQ_Instr;
import org.jruby.compiler.ir.instructions.FilenameInstruction;
import org.jruby.compiler.ir.instructions.GET_ARRAY_Instr;
import org.jruby.compiler.ir.instructions.GET_CONST_Instr;
import org.jruby.compiler.ir.instructions.GET_CVAR_Instr;
import org.jruby.compiler.ir.instructions.GET_FIELD_Instr;
import org.jruby.compiler.ir.instructions.GET_GLOBAL_VAR_Instr;
import org.jruby.compiler.ir.instructions.IR_Instr;
import org.jruby.compiler.ir.instructions.IS_TRUE_Instr;
import org.jruby.compiler.ir.instructions.JRUBY_IMPL_CALL_Instr;
import org.jruby.compiler.ir.instructions.JUMP_Instr;
import org.jruby.compiler.ir.instructions.JUMP_INDIRECT_Instr;
import org.jruby.compiler.ir.instructions.LABEL_Instr;
import org.jruby.compiler.ir.instructions.LINE_NUM_Instr;
import org.jruby.compiler.ir.instructions.NOT_Instr;
import org.jruby.compiler.ir.instructions.PUT_CONST_Instr;
import org.jruby.compiler.ir.instructions.PUT_CVAR_Instr;
import org.jruby.compiler.ir.instructions.PUT_FIELD_Instr;
import org.jruby.compiler.ir.instructions.PUT_GLOBAL_VAR_Instr;
import org.jruby.compiler.ir.instructions.ReceiveArgumentInstruction;
import org.jruby.compiler.ir.instructions.RECV_CLOSURE_ARG_Instr;
import org.jruby.compiler.ir.instructions.RECV_CLOSURE_Instr;
import org.jruby.compiler.ir.instructions.RECV_EXCEPTION_Instr;
import org.jruby.compiler.ir.instructions.RECV_OPT_ARG_Instr;
import org.jruby.compiler.ir.instructions.RESCUED_BODY_START_MARKER_Instr;
import org.jruby.compiler.ir.instructions.RESCUED_BODY_END_MARKER_Instr;
import org.jruby.compiler.ir.instructions.RETURN_Instr;
import org.jruby.compiler.ir.instructions.RUBY_INTERNALS_CALL_Instr;
import org.jruby.compiler.ir.instructions.SET_RETADDR_Instr;
import org.jruby.compiler.ir.instructions.THREAD_POLL_Instr;
import org.jruby.compiler.ir.instructions.THROW_EXCEPTION_Instr;
import org.jruby.compiler.ir.instructions.YIELD_Instr;
import org.jruby.compiler.ir.operands.Array;
import org.jruby.compiler.ir.operands.Backref;
import org.jruby.compiler.ir.operands.BacktickString;
import org.jruby.compiler.ir.operands.BooleanLiteral;
import org.jruby.compiler.ir.operands.BreakResult;
import org.jruby.compiler.ir.operands.CompoundArray;
import org.jruby.compiler.ir.operands.CompoundString;
import org.jruby.compiler.ir.operands.DynamicSymbol;
import org.jruby.compiler.ir.operands.Fixnum;
import org.jruby.compiler.ir.operands.Float;
import org.jruby.compiler.ir.operands.Hash;
import org.jruby.compiler.ir.operands.KeyValuePair;
import org.jruby.compiler.ir.operands.Label;
import org.jruby.compiler.ir.operands.LocalVariable;
import org.jruby.compiler.ir.operands.MetaObject;
import org.jruby.compiler.ir.operands.MethAddr;
import org.jruby.compiler.ir.operands.Nil;
import org.jruby.compiler.ir.operands.NthRef;
import org.jruby.compiler.ir.operands.Operand;
import org.jruby.compiler.ir.operands.Range;
import org.jruby.compiler.ir.operands.Regexp;
import org.jruby.compiler.ir.operands.SValue;
import org.jruby.compiler.ir.operands.Splat;
import org.jruby.compiler.ir.operands.StringLiteral;
import org.jruby.compiler.ir.operands.Symbol;
import org.jruby.compiler.ir.operands.Variable;
import org.jruby.runtime.BlockBody;
import org.jruby.util.ByteList;
// This class converts an AST into a bunch of IR instructions
// IR Building Notes
// -----------------
//
// 1. More copy instructions added than necessary
// ----------------------------------------------
// Note that in general, there will be lots of a = b kind of copies
// introduced in the IR because the translation is entirely single-node focused.
// An example will make this clear
//
// RUBY:
// v = @f
// will translate to
//
// AST:
// LocalAsgnNode v
// InstrVarNode f
// will translate to
//
// IR:
// tmp = self.f [ GET_FIELD(tmp,self,f) ]
// v = tmp [ COPY(v, tmp) ]
//
// instead of
// v = self.f [ GET_FIELD(v, self, f) ]
//
// We could get smarter and pass in the variable into which this expression is going to get evaluated
// and use that to store the value of the expression (or not build the expression if the variable is null).
//
// But, that makes the code more complicated, and in any case, all this will get fixed in a single pass of
// copy propagation and dead-code elimination.
//
// Something to pay attention to and if this extra pass becomes a concern (not convinced that it is yet),
// this smart can be built in here. Right now, the goal is to do something simple and straightforward that is going to be correct.
//
// 2. Returning null vs Nil.NIL
// ----------------------------
// - We should be returning null from the build methods where it is a normal "error" condition
// - We should be returning Nil.NIL where the actual return value of a build is the ruby nil operand
// Look in buildIf for an example of this
public class IR_Builder
{
public static void main(String[] args) {
boolean isDebug = args.length > 0 && args[0].equals("-debug");
int i = isDebug ? 1 : 0;
String methName = null;
if (args.length > i && args[i].equals("-inline")) {
methName = args[i+1];
i += 2;
}
boolean isCommandLineScript = args.length > i && args[i].equals("-e");
i += (isCommandLineScript ? 1 : 0);
while (i < args.length) {
long t1 = new Date().getTime();
Node ast = buildAST(isCommandLineScript, args[i]);
long t2 = new Date().getTime();
IR_Scope scope = new IR_Builder().buildRoot(ast);
long t3 = new Date().getTime();
if (isDebug) {
System.out.println("################## Before local optimization pass ##################");
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.IR_Printer());
}
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.opts.LocalOptimizationPass());
long t4 = new Date().getTime();
if (isDebug) {
System.out.println("################## After local optimization pass ##################");
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.IR_Printer());
}
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.CFG_Builder());
long t5 = new Date().getTime();
// scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.DominatorTreeBuilder());
long t6 = new Date().getTime();
if (methName != null) {
System.out.println("################## After inline pass ##################");
System.out.println("Asked to inline " + methName);
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.InlineTest(methName));
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.opts.LocalOptimizationPass());
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.IR_Printer());
}
if (isDebug) {
System.out.println("################## After dead code elimination pass ##################");
}
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.LiveVariableAnalysis());
long t7 = new Date().getTime();
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.opts.DeadCodeElimination());
long t8 = new Date().getTime();
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.AddFrameInstructions());
long t9 = new Date().getTime();
if (isDebug) {
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.IR_Printer());
}
if (isDebug) {
System.out.println("################## After cfg linearization pass ##################");
}
scope.runCompilerPass(new org.jruby.compiler.ir.compiler_pass.LinearizeCFG());
System.out.println("Time to build AST : " + (t2 - t1));
System.out.println("Time to build IR : " + (t3 - t2));
System.out.println("Time to run local opts : " + (t4 - t3));
System.out.println("Time to run build cfg : " + (t5 - t4));
System.out.println("Time to run build domtree : " + (t6 - t5));
System.out.println("Time to run lva : " + (t7 - t6));
System.out.println("Time to run dead code elim: " + (t8 - t7));
System.out.println("Time to add frame instrs : " + (t9 - t8));
i++;
}
}
/* -----------------------------------------------------------------------------------
* Every ensure block has a start label and end label, and at the end, it will execute
* an jump to an address stored in a return address variable.
*
* This ruby code will translate to the IR shown below
* -----------------
* begin
* ... protected body ...
* ensure
* ... ensure block to run
* end
* -----------------
* IR instructions for the protected body
* L_start:
* .. ensure block IR ...
* jump %ret_addr
* L_end:
* -----------------
*
* If N is a node in the protected body that might exit this scope (exception rethrows
* and returns), N has to first jump to the ensure block and let the ensure block run.
* In addition, N has to set up a return address label in the return address var of
* this ensure block so that the ensure block can transfer control block to N.
*
* Since we can have a nesting of ensure blocks, we are maintaining a stack of these
* well-nested ensure blocks. Every node N that will exit this scope will have to
* co-ordinate the jumps in-and-out of the ensure blocks in the top-to-bottom stacked
* order.
* ----------------------------------------------------------------------------------- */
private static class EnsureBlockInfo
{
Label start;
Label end;
Variable returnAddr;
// Flag set if the protected body has a return or if it rethrows exception
// -- basically anytime there is a non-fallthru xfer of control flow.
boolean noFallThru;
// The end label for an ensure block is not always needed.
boolean endLabelNeeded;
public EnsureBlockInfo(IR_Scope m)
{
returnAddr = m.getNewTemporaryVariable();
start = m.getNewLabel();
end = m.getNewLabel();
noFallThru = false;
endLabelNeeded = false;
}
public static void emitJumpChain(IR_Scope m, Stack<EnsureBlockInfo> ebStack)
{
int n = ebStack.size();
EnsureBlockInfo[] ebArray = ebStack.toArray(new EnsureBlockInfo[n]);
for (int i = n-1; i >= 0; i--) {
Label retLabel = m.getNewLabel();
m.addInstr(new SET_RETADDR_Instr(ebArray[i].returnAddr, retLabel));
m.addInstr(new JUMP_Instr(ebArray[i].start));
m.addInstr(new LABEL_Instr(retLabel));
ebArray[i].noFallThru = true;
}
}
}
private int _lastProcessedLineNum = -1;
private Stack<EnsureBlockInfo> _ensureBlockStack = new Stack<EnsureBlockInfo>();
public static Node buildAST(boolean isCommandLineScript, String arg) {
Ruby ruby = Ruby.getGlobalRuntime();
if (isCommandLineScript) {
// inline script
return ruby.parse(ByteList.create(arg), "-e", null, 0, false);
} else {
// from file
FileInputStream fis = null;
try {
File file = new File(arg);
fis = new FileInputStream(file);
long size = file.length();
byte[] bytes = new byte[(int)size];
fis.read(bytes);
System.out.println("-- processing " + arg + " --");
return ruby.parse(new ByteList(bytes), arg, null, 0, false);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} finally {
try { if (fis != null) fis.close(); } catch(Exception e) { }
}
}
}
public Node skipOverNewlines(IR_Scope s, Node n) {
if (n.getNodeType() == NodeType.NEWLINENODE) {
// Do not emit multiple line number instrs for the same line
int currLineNum = n.getPosition().getStartLine();
if (currLineNum != _lastProcessedLineNum) {
s.addInstr(new LINE_NUM_Instr(s, currLineNum));
_lastProcessedLineNum = currLineNum;
}
}
while (n.getNodeType() == NodeType.NEWLINENODE)
n = ((NewlineNode)n).getNextNode();
return n;
}
public Operand build(Node node, IR_Scope m) {
if (node == null) {
return null;
}
if (m == null) {
System.out.println("Got a null scope!");
throw new NotCompilableException("Unknown node encountered in builder: " + node);
}
switch (node.getNodeType()) {
case ALIASNODE: return buildAlias((AliasNode) node, m); // done
case ANDNODE: return buildAnd((AndNode) node, m); // done
case ARGSCATNODE: return buildArgsCat((ArgsCatNode) node, m); // done
case ARGSPUSHNODE: return buildArgsPush((ArgsPushNode) node, m); // Nothing to do for 1.8
case ARRAYNODE: return buildArray(node, m); // done
case ATTRASSIGNNODE: return buildAttrAssign((AttrAssignNode) node, m); // done
case BACKREFNODE: return buildBackref((BackRefNode) node, m); // done
case BEGINNODE: return buildBegin((BeginNode) node, m); // done
case BIGNUMNODE: return buildBignum((BignumNode) node, m); // done
case BLOCKNODE: return buildBlock((BlockNode) node, m); // done
case BREAKNODE: return buildBreak((BreakNode) node, (IR_ExecutionScope)m); // done?
case CALLNODE: return buildCall((CallNode) node, m); // done
case CASENODE: return buildCase((CaseNode) node, m); // done
case CLASSNODE: return buildClass((ClassNode) node, m); // done
case CLASSVARNODE: return buildClassVar((ClassVarNode) node, m); // done
case CLASSVARASGNNODE: return buildClassVarAsgn((ClassVarAsgnNode) node, m); // done
case CLASSVARDECLNODE: return buildClassVarDecl((ClassVarDeclNode) node, m); // done
case COLON2NODE: return buildColon2((Colon2Node) node, m); // done
case COLON3NODE: return buildColon3((Colon3Node) node, m); // done
case CONSTDECLNODE: return buildConstDecl((ConstDeclNode) node, m); // done
case CONSTNODE: return buildConst((ConstNode) node, m); // done
case DASGNNODE: return buildDAsgn((DAsgnNode) node, m); // done
// case DEFINEDNODE: return buildDefined(node, m); // Incomplete
case DEFNNODE: return buildDefn((MethodDefNode) node, m); // done
case DEFSNODE: return buildDefs((MethodDefNode) node, m); // done
case DOTNODE: return buildDot((DotNode) node, m); // done
case DREGEXPNODE: return buildDRegexp((DRegexpNode) node, m); // done
case DSTRNODE: return buildDStr((DStrNode) node, m); // done
case DSYMBOLNODE: return buildDSymbol(node, m); // done
case DVARNODE: return buildDVar((DVarNode) node, m); // done
case DXSTRNODE: return buildDXStr((DXStrNode) node, m); // done
case ENSURENODE: return buildEnsureNode(node, m); // done
case EVSTRNODE: return buildEvStr((EvStrNode) node, m); // done
case FALSENODE: return buildFalse(node, m); // done
case FCALLNODE: return buildFCall((FCallNode) node, m); // done
case FIXNUMNODE: return buildFixnum((FixnumNode) node, m); // done
// case FLIPNODE: return buildFlip(node, m); // SSS FIXME: What code generates this AST?
case FLOATNODE: return buildFloat((FloatNode) node, m); // done
case FORNODE: return buildFor((ForNode) node, (IR_ExecutionScope)m); // done
case GLOBALASGNNODE: return buildGlobalAsgn((GlobalAsgnNode) node, m); // done
case GLOBALVARNODE: return buildGlobalVar((GlobalVarNode) node, m); // done
case HASHNODE: return buildHash((HashNode) node, m); // done
case IFNODE: return buildIf((IfNode) node, m); // done
case INSTASGNNODE: return buildInstAsgn((InstAsgnNode) node, m); // done
case INSTVARNODE: return buildInstVar((InstVarNode) node, m); // done
case ITERNODE: return buildIter((IterNode) node, (IR_ExecutionScope)m); // done
case LOCALASGNNODE: return buildLocalAsgn((LocalAsgnNode) node, m); // done
case LOCALVARNODE: return buildLocalVar((LocalVarNode) node, m); // done
case MATCH2NODE: return buildMatch2((Match2Node) node, m); // done
case MATCH3NODE: return buildMatch3((Match3Node) node, m); // done
case MATCHNODE: return buildMatch((MatchNode) node, m); // done
case MODULENODE: return buildModule((ModuleNode) node, m); // done
case MULTIPLEASGNNODE: return buildMultipleAsgn((MultipleAsgnNode) node, m); // done
case NEWLINENODE: return buildNewline((NewlineNode) node, m); // done
case NEXTNODE: return buildNext((NextNode) node, (IR_ExecutionScope)m); // done?
case NTHREFNODE: return buildNthRef((NthRefNode) node, m); // done
case NILNODE: return buildNil(node, m); // done
case NOTNODE: return buildNot((NotNode) node, m); // done
case OPASGNANDNODE: return buildOpAsgnAnd((OpAsgnAndNode) node, m); // done
case OPASGNNODE: return buildOpAsgn((OpAsgnNode) node, m); // done
case OPASGNORNODE: return buildOpAsgnOr((OpAsgnOrNode) node, m); // done -- partially
case OPELEMENTASGNNODE: return buildOpElementAsgn(node, m); // done
case ORNODE: return buildOr((OrNode) node, m); // done
// case POSTEXENODE: return buildPostExe(node, m); // DEFERRED
// case PREEXENODE: return buildPreExe(node, m); // DEFERRED
case REDONODE: return buildRedo(node, (IR_ExecutionScope)m); // done??
case REGEXPNODE: return buildRegexp((RegexpNode) node, m); // done
case RESCUEBODYNODE:
throw new NotCompilableException("rescue body is handled by rescue compilation at: " + node.getPosition());
case RESCUENODE: return buildRescue(node, m); // done
// case RETRYNODE: return buildRetry(node, m); // DEFERRED
case RETURNNODE: return buildReturn((ReturnNode) node, m); // done
case ROOTNODE:
throw new NotCompilableException("Use buildRoot(); Root node at: " + node.getPosition());
case SCLASSNODE: return buildSClass(node, m); // done
case SELFNODE: return buildSelf((SelfNode) node, m); // done
case SPLATNODE: return buildSplat((SplatNode) node, m); // done
case STRNODE: return buildStr((StrNode) node, m); // done
case SUPERNODE: return buildSuper((SuperNode) node, m); // done
case SVALUENODE: return buildSValue((SValueNode) node, m); // done
case SYMBOLNODE: return buildSymbol((SymbolNode) node, m); // done
case TOARYNODE: return buildToAry((ToAryNode) node, m); // done
case TRUENODE: return buildTrue(node, m); // done
// case UNDEFNODE: return buildUndef(node, m); // DEFERRED
case UNTILNODE: return buildUntil((UntilNode) node, (IR_ExecutionScope)m); // done
// case VALIASNODE: return buildVAlias(node, m); // DEFERRED
case VCALLNODE: return buildVCall((VCallNode) node, m); // done
case WHILENODE: return buildWhile((WhileNode) node, (IR_ExecutionScope)m); // done
case WHENNODE: assert false : "When nodes are handled by case node compilation."; return null;
case XSTRNODE: return buildXStr((XStrNode) node, m); // done
case YIELDNODE: return buildYield((YieldNode) node, m); // done
case ZARRAYNODE: return buildZArray(node, m); // done
case ZSUPERNODE: return buildZSuper((ZSuperNode) node, m); // done
default: throw new NotCompilableException("Unknown node encountered in builder: " + node);
}
}
public void buildArguments(List<Operand> args, Node node, IR_Scope s) {
switch (node.getNodeType()) {
case ARGSCATNODE: buildArgsCatArguments(args, (ArgsCatNode) node, s); break;
case ARGSPUSHNODE: buildArgsPushArguments(args, (ArgsPushNode) node, s); break;
case ARRAYNODE: buildArrayArguments(args, node, s); break;
case SPLATNODE: buildSplatArguments(args, (SplatNode) node, s); break;
default:
Operand retVal = build(node, s);
if (retVal != null) // SSS FIXME: Can this ever be null?
args.add(retVal);
}
}
public void buildVariableArityArguments(List<Operand> args, Node node, IR_Scope s) {
buildArguments(args, node, s);
}
public void buildSpecificArityArguments (List<Operand> args, Node node, IR_Scope s) {
if (node.getNodeType() == NodeType.ARRAYNODE) {
ArrayNode arrayNode = (ArrayNode)node;
if (arrayNode.isLightweight()) {
// explode array, it's an internal "args" array
for (Node n : arrayNode.childNodes())
args.add(build(n, s));
} else {
// use array as-is, it's a literal array
args.add(build(arrayNode, s));
}
} else {
args.add(build(node, s));
}
}
public List<Operand> setupCallArgs(Node receiver, Node args, IR_Scope s) {
List<Operand> argsList = new ArrayList<Operand>();
argsList.add(build(receiver, s)); // SSS FIXME: Is this correct?
if (args != null) {
// unwrap newline nodes to get their actual type
args = skipOverNewlines(s, args);
buildArgs(argsList, args, s);
}
return argsList;
}
public List<Operand> setupCallArgs(Node args, IR_Scope s) {
List<Operand> argsList = new ArrayList<Operand>();
argsList.add(s.getSelf()); // SSS FIXME: Is this correct?
if (args != null) {
// unwrap newline nodes to get their actual type
args = skipOverNewlines(s, args);
buildArgs(argsList, args, s);
}
return argsList;
}
public void buildArgs(List<Operand> argsList, Node args, IR_Scope s) {
switch (args.getNodeType()) {
case ARGSCATNODE:
case ARGSPUSHNODE:
case SPLATNODE:
buildVariableArityArguments(argsList, args, s);
break;
case ARRAYNODE:
ArrayNode arrayNode = (ArrayNode)args;
if (arrayNode.size() > 3)
buildVariableArityArguments(argsList, arrayNode, s);
else if (arrayNode.size() > 0)
buildSpecificArityArguments(argsList, arrayNode, s);
break;
default:
buildSpecificArityArguments(argsList, args, s);
break;
}
}
// This method is called to build assignments for a multiple-assignment instruction
public void buildAssignment(Node node, IR_Scope s, Operand values, int argIndex, boolean isSplat) {
Variable v = s.getNewTemporaryVariable();
s.addInstr(new GET_ARRAY_Instr(v, values, argIndex, isSplat));
switch (node.getNodeType()) {
case ATTRASSIGNNODE:
buildAttrAssignAssignment(node, s, v);
break;
// SSS FIXME: What is the difference between ClassVarAsgnNode & ClassVarDeclNode
case CLASSVARASGNNODE:
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), ((ClassVarAsgnNode)node).getName(), v));
break;
case CLASSVARDECLNODE:
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), ((ClassVarDeclNode)node).getName(), v));
break;
case CONSTDECLNODE:
buildConstDeclAssignment((ConstDeclNode) node, s, v);
break;
case GLOBALASGNNODE:
s.addInstr(new PUT_GLOBAL_VAR_Instr(((GlobalAsgnNode)node).getName(), v));
break;
case INSTASGNNODE:
// NOTE: if 's' happens to the a class, this is effectively an assignment of a class instance variable
s.addInstr(new PUT_FIELD_Instr(s.getSelf(), ((InstAsgnNode)node).getName(), v));
break;
case LOCALASGNNODE:
s.addInstr(new COPY_Instr(new LocalVariable(((LocalAsgnNode)node).getName()), v));
break;
case MULTIPLEASGNNODE:
buildMultipleAsgnAssignment((MultipleAsgnNode) node, s, v);
break;
case ZEROARGNODE:
throw new NotCompilableException("Shouldn't get here; zeroarg does not do assignment: " + node);
default:
throw new NotCompilableException("Can't build assignment node: " + node);
}
}
// This method is called to build arguments for a block!
public void buildBlockArgsAssignment(Node node, IR_Scope s, int argIndex, boolean isSplat) {
Variable v;
switch (node.getNodeType()) {
case ATTRASSIGNNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
buildAttrAssignAssignment(node, s, v);
break;
// SSS FIXME:
//
// There are also differences in variable scoping between 1.8 and 1.9
// Ruby 1.8 is the buggy semantics if I understand correctly.
//
// The semantics of how this shadows other variables outside the block needs
// to be figured out during live var analysis.
case DASGNNODE:
v = new LocalVariable(((DAsgnNode)node).getName());
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
break;
// SSS FIXME: What is the difference between ClassVarAsgnNode & ClassVarDeclNode
case CLASSVARASGNNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), ((ClassVarAsgnNode)node).getName(), v));
break;
case CLASSVARDECLNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), ((ClassVarDeclNode)node).getName(), v));
break;
case CONSTDECLNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
buildConstDeclAssignment((ConstDeclNode) node, s, v);
break;
case GLOBALASGNNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
s.addInstr(new PUT_GLOBAL_VAR_Instr(((GlobalAsgnNode)node).getName(), v));
break;
case INSTASGNNODE:
v = s.getNewTemporaryVariable();
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
// NOTE: if 's' happens to the a class, this is effectively an assignment of a class instance variable
s.addInstr(new PUT_FIELD_Instr(s.getSelf(), ((InstAsgnNode)node).getName(), v));
break;
case LOCALASGNNODE:
v = new LocalVariable(((LocalAsgnNode)node).getName());
s.addInstr(new RECV_CLOSURE_ARG_Instr(v, argIndex, isSplat));
break;
case MULTIPLEASGNNODE:
// SSS FIXME: Are we guaranteed that we splats dont head to multiple-assignment nodes! i.e. |*(a,b)|?
buildMultipleAsgnAssignment((MultipleAsgnNode) node, s, null);
break;
case ZEROARGNODE:
throw new NotCompilableException("Shouldn't get here; zeroarg does not do assignment: " + node);
default:
throw new NotCompilableException("Can't build assignment node: " + node);
}
}
public Operand buildAlias(final AliasNode alias, IR_Scope s) {
String newName = "";//TODO: FIX breakage....alias.getNewName();
String oldName = "";//TODO: FIX breakage....alias.getOldName();
Operand[] args = new Operand[] { new MetaObject(s), new MethAddr(newName), new MethAddr(oldName) };
s.recordMethodAlias(newName, oldName);
s.addInstr(new RUBY_INTERNALS_CALL_Instr(null, MethAddr.DEFINE_ALIAS, args));
// SSS FIXME: Can this return anything other than nil?
return Nil.NIL;
}
// Translate "ret = (a && b)" --> "ret = (a ? b : false)" -->
//
// v1 = -- build(a) --
// OPT: ret can be set to v1, but effectively v1 is false if we take the branch to L.
// while this info can be inferred by using attributes, why bother if we can do this?
// ret = false
// beq(v1, false, L)
// v2 = -- build(b) --
// ret = v2
// L:
//
public Operand buildAnd(final AndNode andNode, IR_Scope m) {
if (andNode.getFirstNode().getNodeType().alwaysTrue()) {
// build first node (and ignore its result) and then second node
build(andNode.getFirstNode(), m);
return build(andNode.getSecondNode(), m);
} else if (andNode.getFirstNode().getNodeType().alwaysFalse()) {
// build first node only and return false
build(andNode.getFirstNode(), m);
return BooleanLiteral.FALSE;
} else {
Variable ret = m.getNewTemporaryVariable();
Label l = m.getNewLabel();
Operand v1 = build(andNode.getFirstNode(), m);
m.addInstr(new COPY_Instr(ret, BooleanLiteral.FALSE));
m.addInstr(new BEQ_Instr(v1, BooleanLiteral.FALSE, l));
Operand v2 = build(andNode.getSecondNode(), m);
m.addInstr(new COPY_Instr(ret, v2));
m.addInstr(new LABEL_Instr(l));
return ret;
}
}
public Operand buildArray(Node node, IR_Scope m) {
List<Operand> elts = new ArrayList<Operand>();
for (Node e: node.childNodes())
elts.add(build(e, m));
return new Array(elts);
}
public Operand buildArgsCat(final ArgsCatNode argsCatNode, IR_Scope s) {
Operand v1 = build(argsCatNode.getFirstNode(), s);
Operand v2 = build(argsCatNode.getSecondNode(), s);
return new CompoundArray(v1, v2);
}
public Operand buildArgsPush(final ArgsPushNode node, IR_Scope m) {
throw new NotCompilableException("ArgsPush should never be encountered bare in 1.8");
}
private Operand buildAttrAssign(final AttrAssignNode attrAssignNode, IR_Scope s) {
List<Operand> args = setupCallArgs(attrAssignNode.getArgsNode(), s);
Operand obj = build(attrAssignNode.getReceiverNode(), s);
s.addInstr(new ATTR_ASSIGN_Instr(obj, new StringLiteral(attrAssignNode.getName()), args.get(1)));
return args.get(0);
}
public Operand buildAttrAssignAssignment(Node node, IR_Scope s, Operand value) {
final AttrAssignNode attrAssignNode = (AttrAssignNode) node;
List<Operand> args = setupCallArgs(attrAssignNode.getArgsNode(), s);
Operand obj = build(attrAssignNode.getReceiverNode(), s);
s.addInstr(new ATTR_ASSIGN_Instr(obj, new StringLiteral(attrAssignNode.getName()), value));
return value;
}
public Operand buildBackref(BackRefNode node, IR_Scope m) {
return new Backref(node.getType());
}
public Operand buildBegin(BeginNode beginNode, IR_Scope s) {
return build(beginNode.getBodyNode(), s);
}
public Operand buildBignum(BignumNode node, IR_Scope s) {
return new Fixnum(node.getValue());
}
public Operand buildBlock(BlockNode node, IR_Scope s) {
Operand retVal = null;
for (Node child : node.childNodes()) {
retVal = build(child, s);
}
// Value of the last expression in the block
return retVal;
}
public Operand buildBreak(BreakNode breakNode, IR_ExecutionScope s) {
Operand rv = build(breakNode.getValueNode(), s);
if (s instanceof IR_Closure) {
s.addInstr(new BREAK_Instr(rv));
return rv;
}
else {
// If this is not a closure, the break is equivalent to jumping to the loop end label
// But, since break can return a result even in loops, we need to pass back both
// the return value as well as the jump target -- so, create a special-purpose operand just for that purpose!
return new BreakResult(rv, s.getCurrentLoop()._loopEndLabel);
}
}
public Operand buildCall(CallNode callNode, IR_Scope s) {
Node callArgsNode = callNode.getArgsNode();
Node receiverNode = callNode.getReceiverNode();
List<Operand> args = setupCallArgs(receiverNode, callArgsNode, s);
Operand block = setupCallClosure(callNode.getIterNode(), s);
Variable callResult = s.getNewTemporaryVariable();
IR_Instr callInstr = new CallInstruction(callResult, new MethAddr(callNode.getName()), args.toArray(new Operand[args.size()]), block);
s.addInstr(callInstr);
return callResult;
}
public Operand buildCase(CaseNode caseNode, IR_Scope m) {
// get the incoming case value
Operand value = build(caseNode.getCaseNode(), m);
// the CASE instruction
Label endLabel = m.getNewLabel();
Variable result = m.getNewTemporaryVariable();
CASE_Instr caseInstr = new CASE_Instr(result, value, endLabel);
m.addInstr(caseInstr);
// lists to aggregate variables and bodies for whens
List<Operand> variables = new ArrayList<Operand>();
List<Label> labels = new ArrayList<Label>();
Map<Label, Node> bodies = new HashMap<Label, Node>();
// build each "when"
for (Node aCase : caseNode.getCases().childNodes()) {
WhenNode whenNode = (WhenNode)aCase;
Label bodyLabel = m.getNewLabel();
if (whenNode.getExpressionNodes() instanceof ListNode) {
// multiple conditions for when
for (Node expression : ((ListNode)whenNode.getExpressionNodes()).childNodes()) {
Variable eqqResult = m.getNewTemporaryVariable();
variables.add(eqqResult);
labels.add(bodyLabel);
m.addInstr(new EQQ_Instr(eqqResult, build(expression, m), value));
m.addInstr(new BEQ_Instr(eqqResult, BooleanLiteral.TRUE, bodyLabel));
}
} else {
Variable eqqResult = m.getNewTemporaryVariable();
variables.add(eqqResult);
labels.add(bodyLabel);
m.addInstr(new EQQ_Instr(eqqResult, build(whenNode.getExpressionNodes(), m), value));
m.addInstr(new BEQ_Instr(eqqResult, BooleanLiteral.TRUE, bodyLabel));
}
// SSS FIXME: This doesn't preserve original order of when clauses. We could consider
// preserving the order (or maybe not, since we would have to sort the constants first
// in any case) for outputing jump tables in certain situations.
//
// add body to map for emitting later
bodies.put(bodyLabel, whenNode.getBodyNode());
}
// build "else" if it exists
if (caseNode.getElseNode() != null) {
Label elseLbl = m.getNewLabel();
caseInstr.setElse(elseLbl);
bodies.put(elseLbl, caseNode.getElseNode());
}
// now emit bodies
for (Map.Entry<Label, Node> entry : bodies.entrySet()) {
m.addInstr(new LABEL_Instr(entry.getKey()));
Operand bodyValue = build(entry.getValue(), m);
// Local optimization of break results (followed by a copy & jump) to short-circuit the jump right away
// rather than wait to do it during an optimization pass when a dead jump needs to be removed.
Label tgt = endLabel;
if (bodyValue instanceof BreakResult) {
BreakResult br = (BreakResult)bodyValue;
bodyValue = br._result;
tgt = br._jumpTarget;
}
m.addInstr(new COPY_Instr(result, bodyValue));
m.addInstr(new JUMP_Instr(tgt));
}
// close it out
m.addInstr(new LABEL_Instr(endLabel));
caseInstr.setLabels(labels);
caseInstr.setVariables(variables);
// CON FIXME: I don't know how to make case be an expression...does that
// logic need to go here?
return result;
}
public Operand buildClass(ClassNode classNode, IR_Scope s) {
final Node superNode = classNode.getSuperNode();
final Colon3Node cpathNode = classNode.getCPath();
Operand superClass = null;
if (superNode != null)
superClass = build(superNode, s);
// By default, the container for this class is 's'
Operand container = null;
// Do we have a dynamic container?
if (cpathNode instanceof Colon2Node) {
Node leftNode = ((Colon2Node) cpathNode).getLeftNode();
if (leftNode != null)
container = build(leftNode, s);
} else if (cpathNode instanceof Colon3Node) {
container = new MetaObject(IR_Class.getCoreClass("Object"));
}
// Build a new class and add it to the current scope (could be a script / module / class)
String className = cpathNode.getName();
IR_Class c = new IR_Class(s, container, superClass, className);
s.addClass(c);
if (container != null)
s.addInstr(new PUT_CONST_Instr(container, className, new MetaObject(c)));
// Build the class body!
if (classNode.getBodyNode() != null)
build(classNode.getBodyNode(), c.getRootMethod());
return null;
}
public Operand buildSClass(Node node, IR_Scope s) {
final SClassNode sclassNode = (SClassNode) node;
// class Foo
// ...
// class << self
// ...
// end
// ...
// end
//
// Here, the class << self declaration is in Foo's root method.
// Foo is the class in whose context this is being defined.
Operand receiver = build(sclassNode.getReceiverNode(), s);
IR_Class mc = new IR_MetaClass(s, receiver);
// Record the new class as being lexically defined in scope s
s.addClass(mc);
if (sclassNode.getBodyNode() != null)
build(sclassNode.getBodyNode(), mc.getRootMethod());
return null;
}
public Operand buildClassVar(ClassVarNode node, IR_Scope s) {
Variable ret = s.getNewTemporaryVariable();
s.addInstr(new GET_CVAR_Instr(ret, new MetaObject(s), node.getName()));
return ret;
}
// SSS FIXME: Where is this set up? How is this diff from ClassVarDeclNode??
public Operand buildClassVarAsgn(final ClassVarAsgnNode classVarAsgnNode, IR_Scope s) {
Operand val = build(classVarAsgnNode.getValueNode(), s);
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), classVarAsgnNode.getName(), val));
return val;
}
public Operand buildClassVarDecl(final ClassVarDeclNode classVarDeclNode, IR_Scope s) {
Operand val = build(classVarDeclNode.getValueNode(), s);
s.addInstr(new PUT_CVAR_Instr(new MetaObject(s), classVarDeclNode.getName(), val));
return val;
}
public Operand buildConstDecl(ConstDeclNode node, IR_Scope s) {
Operand val = build(node.getValueNode(), s);
return buildConstDeclAssignment(node, s, val);
}
public Operand buildConstDeclAssignment(ConstDeclNode constDeclNode, IR_Scope s, Operand val) {
Node constNode = constDeclNode.getConstNode();
if (constNode == null) {
s.setConstantValue(constDeclNode.getName(), val);
} else if (constNode.getNodeType() == NodeType.COLON2NODE) {
Operand module = build(((Colon2Node) constNode).getLeftNode(), s);
s.addInstr(new PUT_CONST_Instr(module, constDeclNode.getName(), val));
} else { // colon3, assign in Object
s.addInstr(new PUT_CONST_Instr(s.getSelf(), constDeclNode.getName(), val));
}
return val;
}
private Operand loadConst(IR_Scope s, IR_Scope currScope, String name)
{
Operand cv = s.getConstantValue(name);
if (cv == null) {
Variable v = currScope.getNewTemporaryVariable();
currScope.addInstr(new GET_CONST_Instr(v, s, name));
cv = v;
}
return cv;
}
public Operand buildConst(ConstNode node, IR_Scope s) {
return loadConst(s, s, node.getName());
}
public Operand buildColon2(final Colon2Node iVisited, IR_Scope s) {
Node leftNode = iVisited.getLeftNode();
final String name = iVisited.getName();
if (leftNode == null) {