/
IRBuilder.java
3538 lines (3054 loc) · 161 KB
/
IRBuilder.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.ir;
import org.jruby.EvalType;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.ast.*;
import org.jruby.ast.types.INameNode;
import org.jruby.compiler.NotCompilableException;
import org.jruby.ir.instructions.*;
import org.jruby.ir.instructions.defined.GetErrorInfoInstr;
import org.jruby.ir.instructions.defined.RestoreErrorInfoInstr;
import org.jruby.ir.listeners.IRScopeListener;
import org.jruby.ir.operands.*;
import org.jruby.ir.operands.Boolean;
import org.jruby.ir.operands.Float;
import org.jruby.ir.transformations.inlining.SimpleCloneInfo;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.Arity;
import org.jruby.runtime.CallType;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.RubyEvent;
import org.jruby.util.ByteList;
import org.jruby.util.KeyValuePair;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import static org.jruby.ir.instructions.RuntimeHelperCall.Methods.*;
// 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 manager.getNil()
// ----------------------------
// - We should be returning null from the build methods where it is a normal "error" condition
// - We should be returning manager.getNil() where the actual return value of a build is the ruby nil operand
// Look in buildIf for an example of this
//
// 3. Temporary variable reuse
// ---------------------------
// I am reusing variables a lot in places in this code. Should I instead always get a new variable when I need it
// This introduces artificial data dependencies, but fewer variables. But, if we are going to implement SSA pass
// this is not a big deal. Think this through!
public class IRBuilder {
static final Operand[] NO_ARGS = new Operand[]{};
static final UnexecutableNil U_NIL = UnexecutableNil.U_NIL;
public static IRBuilder createIRBuilder(Ruby runtime, IRManager manager) {
return new IRBuilder(manager);
}
public static Node buildAST(boolean isCommandLineScript, String arg) {
Ruby ruby = Ruby.getGlobalRuntime();
// inline script
if (isCommandLineScript) return ruby.parse(ByteList.create(arg), "-e", null, 0, false);
// 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 ignored) { }
}
}
private static class IRLoop {
public final IRScope container;
public final IRLoop parentLoop;
public final Label loopStartLabel;
public final Label loopEndLabel;
public final Label iterStartLabel;
public final Label iterEndLabel;
public final Variable loopResult;
public IRLoop(IRScope s, IRLoop outerLoop) {
container = s;
parentLoop = outerLoop;
loopStartLabel = s.getNewLabel("_LOOP_BEGIN");
loopEndLabel = s.getNewLabel("_LOOP_END");
iterStartLabel = s.getNewLabel("_ITER_BEGIN");
iterEndLabel = s.getNewLabel("_ITER_END");
loopResult = s.createTemporaryVariable();
s.setHasLoopsFlag();
}
}
private static class RescueBlockInfo {
RescueNode rescueNode; // Rescue node for which we are tracking info
Label entryLabel; // Entry of the rescue block
Variable savedExceptionVariable; // Variable that contains the saved $! variable
IRLoop innermostLoop; // Innermost loop within which this rescue block is nested, if any
public RescueBlockInfo(RescueNode n, Label l, Variable v, IRLoop loop) {
rescueNode = n;
entryLabel = l;
savedExceptionVariable = v;
innermostLoop = loop;
}
public void restoreException(IRBuilder b, IRScope s, IRLoop currLoop) {
if (currLoop == innermostLoop) b.addInstr(s, new PutGlobalVarInstr("$!", savedExceptionVariable));
}
}
/* -----------------------------------------------------------------------------------
* Every ensure block has a start label and end label
*
* This ruby code will translate to the IR shown below
* -----------------
* begin
* ... protected body ...
* ensure
* ... ensure block to run
* end
* -----------------
* L_region_start
* IR instructions for the protected body
* .. copy of ensure block IR ..
* L_dummy_rescue:
* e = recv_exc
* L_start:
* .. ensure block IR ..
* throw e
* L_end:
* -----------------
*
* If N is a node in the protected body that might exit this scope (exception rethrows
* and returns), N has to first run the ensure block before exiting.
*
* 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
* run the stack of ensure blocks in the right order.
* ----------------------------------------------------------------------------------- */
private static class EnsureBlockInfo {
Label regionStart;
Label start;
Label end;
Label dummyRescueBlockLabel;
Variable savedGlobalException;
// Label of block that will rescue exceptions raised by ensure code
Label bodyRescuer;
// Innermost loop within which this ensure block is nested, if any
IRLoop innermostLoop;
// AST node for any associated rescue node in the case of begin-rescue-ensure-end block
// Will be null in the case of begin-ensure-end block
RescueNode matchingRescueNode;
// This ensure block's instructions
List<Instr> instrs;
public EnsureBlockInfo(IRScope s, RescueNode n, IRLoop l, Label bodyRescuer) {
regionStart = s.getNewLabel();
start = s.getNewLabel();
end = s.getNewLabel();
dummyRescueBlockLabel = s.getNewLabel();
instrs = new ArrayList<Instr>();
savedGlobalException = null;
innermostLoop = l;
matchingRescueNode = n;
this.bodyRescuer = bodyRescuer;
}
public void addInstr(Instr i) {
instrs.add(i);
}
public void addInstrAtBeginning(Instr i) {
instrs.add(0, i);
}
public void emitBody(IRBuilder b, IRScope s) {
b.addInstr(s, new LabelInstr(start));
for (Instr i: instrs) {
b.addInstr(s, i);
}
}
public void cloneIntoHostScope(IRBuilder b, IRScope s) {
SimpleCloneInfo ii = new SimpleCloneInfo(s, true);
// Clone required labels.
// During normal cloning below, labels not found in the rename map
// are not cloned.
ii.renameLabel(start);
for (Instr i: instrs) {
if (i instanceof LabelInstr) {
ii.renameLabel(((LabelInstr)i).label);
}
}
// Clone instructions now
b.addInstr(s, new LabelInstr(ii.getRenamedLabel(start)));
b.addInstr(s, new ExceptionRegionStartMarkerInstr(bodyRescuer));
for (Instr i: instrs) {
Instr clonedInstr = i.clone(ii);
if (clonedInstr instanceof CallBase) {
CallBase call = (CallBase)clonedInstr;
Operand block = call.getClosureArg(null);
if (block instanceof WrappedIRClosure) s.addClosure(((WrappedIRClosure)block).getClosure());
}
b.addInstr(s, clonedInstr);
}
b.addInstr(s, new ExceptionRegionEndMarkerInstr());
}
}
// Stack of nested rescue blocks -- this just tracks the start label of the blocks
private Stack<RescueBlockInfo> activeRescueBlockStack = new Stack<RescueBlockInfo>();
// Stack of ensure blocks that are currently active
private Stack<EnsureBlockInfo> activeEnsureBlockStack = new Stack<EnsureBlockInfo>();
// Stack of ensure blocks whose bodies are being constructed
private Stack<EnsureBlockInfo> ensureBodyBuildStack = new Stack<EnsureBlockInfo>();
// Combined stack of active rescue/ensure nestings -- required to properly set up
// rescuers for ensure block bodies cloned into other regions -- those bodies are
// rescued by the active rescuers at the point of definition rather than the point
// of cloning.
private Stack<Label> activeRescuers = new Stack<Label>();
private int _lastProcessedLineNum = -1;
// Since we are processing ASTs, loop bodies are processed in depth-first manner
// with outer loops encountered before inner loops, and inner loops finished before outer ones.
//
// So, we can keep track of loops in a loop stack which keeps track of loops as they are encountered.
// This lets us implement next/redo/break/retry easily for the non-closure cases
private Stack<IRLoop> loopStack = new Stack<IRLoop>();
public IRLoop getCurrentLoop() {
return loopStack.isEmpty() ? null : loopStack.peek();
}
protected IRManager manager;
public IRBuilder(IRManager manager) {
this.manager = manager;
this.activeRescuers.push(Label.UNRESCUED_REGION_LABEL);
}
public void addInstr(IRScope s, Instr i) {
// If we are building an ensure body, stash the instruction
// in the ensure body's list. If not, add it to the scope directly.
if (ensureBodyBuildStack.empty()) {
s.addInstr(i);
} else {
ensureBodyBuildStack.peek().addInstr(i);
}
}
public void addInstrAtBeginning(IRScope s, Instr i) {
// If we are building an ensure body, stash the instruction
// in the ensure body's list. If not, add it to the scope directly.
if (ensureBodyBuildStack.empty()) {
s.addInstrAtBeginning(i);
} else {
ensureBodyBuildStack.peek().addInstrAtBeginning(i);
}
}
private Operand getImplicitBlockArg(IRScope s) {
int n = 0;
while (s != null && s instanceof IRClosure) {
// We have this oddity of an extra inserted scope for instance/class/module evals
if (s instanceof IREvalScript && ((IREvalScript)s).isModuleOrInstanceEval()) {
n++;
}
n++;
s = s.getLexicalParent();
}
if (s != null) {
LocalVariable v = null;
if (s instanceof IRMethod || s instanceof IRMetaClassBody) {
v = s.getLocalVariable(Variable.BLOCK, 0);
}
if (v != null) {
return n == 0 ? v : v.cloneForDepth(n);
}
}
return manager.getNil();
}
// Emit cloned ensure bodies by walking up the ensure block stack.
// If we have been passed a loop value, only emit bodies that are nested within that loop.
private void emitEnsureBlocks(IRScope s, IRLoop loop) {
int n = activeEnsureBlockStack.size();
EnsureBlockInfo[] ebArray = activeEnsureBlockStack.toArray(new EnsureBlockInfo[n]);
for (int i = n-1; i >= 0; i--) {
EnsureBlockInfo ebi = ebArray[i];
// For "break" and "next" instructions, we only want to run
// ensure blocks from the loops they are present in.
if (loop != null && ebi.innermostLoop != loop) break;
// SSS FIXME: Should $! be restored before or after the ensure block is run?
if (ebi.savedGlobalException != null) {
addInstr(s, new PutGlobalVarInstr("$!", ebi.savedGlobalException));
}
// Clone into host scope
ebi.cloneIntoHostScope(this, s);
}
}
private Operand buildOperand(Node node, IRScope s) throws NotCompilableException {
switch (node.getNodeType()) {
case ALIASNODE: return buildAlias((AliasNode) node, s);
case ANDNODE: return buildAnd((AndNode) node, s);
case ARGSCATNODE: return buildArgsCat((ArgsCatNode) node, s);
case ARGSPUSHNODE: return buildArgsPush((ArgsPushNode) node, s);
case ARRAYNODE: return buildArray(node, s);
case ATTRASSIGNNODE: return buildAttrAssign((AttrAssignNode) node, s);
case BACKREFNODE: return buildBackref((BackRefNode) node, s);
case BEGINNODE: return buildBegin((BeginNode) node, s);
case BIGNUMNODE: return buildBignum((BignumNode) node, s);
case BLOCKNODE: return buildBlock((BlockNode) node, s);
case BREAKNODE: return buildBreak((BreakNode) node, s);
case CALLNODE: return buildCall((CallNode) node, s);
case CASENODE: return buildCase((CaseNode) node, s);
case CLASSNODE: return buildClass((ClassNode) node, s);
case CLASSVARNODE: return buildClassVar((ClassVarNode) node, s);
case CLASSVARASGNNODE: return buildClassVarAsgn((ClassVarAsgnNode) node, s);
case CLASSVARDECLNODE: return buildClassVarDecl((ClassVarDeclNode) node, s);
case COLON2NODE: return buildColon2((Colon2Node) node, s);
case COLON3NODE: return buildColon3((Colon3Node) node, s);
case CONSTDECLNODE: return buildConstDecl((ConstDeclNode) node, s);
case CONSTNODE: return searchConst(s, ((ConstNode) node).getName());
case DASGNNODE: return buildDAsgn((DAsgnNode) node, s);
case DEFINEDNODE: return buildGetDefinition(((DefinedNode) node).getExpressionNode(), s);
case DEFNNODE: return buildDefn((MethodDefNode) node, s);
case DEFSNODE: return buildDefs((DefsNode) node, s);
case DOTNODE: return buildDot((DotNode) node, s);
case DREGEXPNODE: return buildDRegexp((DRegexpNode) node, s);
case DSTRNODE: return buildDStr((DStrNode) node, s);
case DSYMBOLNODE: return buildDSymbol((DSymbolNode) node, s);
case DVARNODE: return buildDVar((DVarNode) node, s);
case DXSTRNODE: return buildDXStr((DXStrNode) node, s);
case ENCODINGNODE: return buildEncoding((EncodingNode)node, s);
case ENSURENODE: return buildEnsureNode((EnsureNode) node, s);
case EVSTRNODE: return buildEvStr((EvStrNode) node, s);
case FALSENODE: return buildFalse(node, s);
case FCALLNODE: return buildFCall((FCallNode) node, s);
case FIXNUMNODE: return buildFixnum((FixnumNode) node, s);
case FLIPNODE: return buildFlip((FlipNode) node, s);
case FLOATNODE: return buildFloat((FloatNode) node, s);
case FORNODE: return buildFor((ForNode) node, s);
case GLOBALASGNNODE: return buildGlobalAsgn((GlobalAsgnNode) node, s);
case GLOBALVARNODE: return buildGlobalVar((GlobalVarNode) node, s);
case HASHNODE: return buildHash((HashNode) node, s);
case IFNODE: return buildIf((IfNode) node, s);
case INSTASGNNODE: return buildInstAsgn((InstAsgnNode) node, s);
case INSTVARNODE: return buildInstVar((InstVarNode) node, s);
case ITERNODE: return buildIter((IterNode) node, s);
case LAMBDANODE: return buildLambda((LambdaNode)node, s);
case LITERALNODE: return buildLiteral((LiteralNode) node, s);
case LOCALASGNNODE: return buildLocalAsgn((LocalAsgnNode) node, s);
case LOCALVARNODE: return buildLocalVar((LocalVarNode) node, s);
case MATCH2NODE: return buildMatch2((Match2Node) node, s);
case MATCH3NODE: return buildMatch3((Match3Node) node, s);
case MATCHNODE: return buildMatch((MatchNode) node, s);
case MODULENODE: return buildModule((ModuleNode) node, s);
case MULTIPLEASGNNODE: return buildMultipleAsgn((MultipleAsgnNode) node, s); // Only for 1.8
case MULTIPLEASGN19NODE: return buildMultipleAsgn19((MultipleAsgn19Node) node, s);
case NEWLINENODE: return buildNewline((NewlineNode) node, s);
case NEXTNODE: return buildNext((NextNode) node, s);
case NTHREFNODE: return buildNthRef((NthRefNode) node, s);
case NILNODE: return buildNil(node, s);
case OPASGNANDNODE: return buildOpAsgnAnd((OpAsgnAndNode) node, s);
case OPASGNNODE: return buildOpAsgn((OpAsgnNode) node, s);
case OPASGNORNODE: return buildOpAsgnOr((OpAsgnOrNode) node, s);
case OPELEMENTASGNNODE: return buildOpElementAsgn((OpElementAsgnNode) node, s);
case ORNODE: return buildOr((OrNode) node, s);
case PREEXENODE: return buildPreExe((PreExeNode) node, s);
case POSTEXENODE: return buildPostExe((PostExeNode) node, s);
case REDONODE: return buildRedo(node, s);
case REGEXPNODE: return buildRegexp((RegexpNode) node, s);
case RESCUEBODYNODE:
throw new NotCompilableException("rescue body is handled by rescue compilation at: " + node.getPosition());
case RESCUENODE: return buildRescue((RescueNode) node, s);
case RETRYNODE: return buildRetry(node, s);
case RETURNNODE: return buildReturn((ReturnNode) node, s);
case ROOTNODE:
throw new NotCompilableException("Use buildRoot(); Root node at: " + node.getPosition());
case SCLASSNODE: return buildSClass((SClassNode) node, s);
case SELFNODE: return buildSelf(s);
case SPLATNODE: return buildSplat((SplatNode) node, s);
case STRNODE: return buildStr((StrNode) node, s);
case SUPERNODE: return buildSuper((SuperNode) node, s);
case SVALUENODE: return buildSValue((SValueNode) node, s);
case SYMBOLNODE: return buildSymbol((SymbolNode) node, s);
case TRUENODE: return buildTrue(node, s);
case UNDEFNODE: return buildUndef(node, s);
case UNTILNODE: return buildUntil((UntilNode) node, s);
case VALIASNODE: return buildVAlias((VAliasNode) node, s);
case VCALLNODE: return buildVCall((VCallNode) node, s);
case WHILENODE: return buildWhile((WhileNode) node, s);
case WHENNODE: assert false : "When nodes are handled by case node compilation."; return null;
case XSTRNODE: return buildXStr((XStrNode) node, s);
case YIELDNODE: return buildYield((YieldNode) node, s);
case ZARRAYNODE: return buildZArray(node, s);
case ZSUPERNODE: return buildZSuper((ZSuperNode) node, s);
default: throw new NotCompilableException("Unknown node encountered in builder: " + node.getClass());
}
}
private boolean hasListener() {
return manager.getIRScopeListener() != null;
}
public IRBuilder newIRBuilder(IRManager manager) {
return new IRBuilder(manager);
}
public Node skipOverNewlines(IRScope 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) {
if (RubyInstanceConfig.FULL_TRACE_ENABLED) {
addInstr(s, new TraceInstr(RubyEvent.LINE, methodNameFor(s), s.getFileName(), currLineNum));
}
addInstr(s, new LineNumberInstr(s, currLineNum));
_lastProcessedLineNum = currLineNum;
}
}
while (n.getNodeType() == NodeType.NEWLINENODE)
n = ((NewlineNode)n).getNextNode();
return n;
}
public Operand build(Node node, IRScope s) {
if (node == null) return null;
if (s == null) {
System.out.println("Got a null scope!");
throw new NotCompilableException("Unknown node encountered in builder: " + node);
}
if (hasListener()) {
IRScopeListener listener = manager.getIRScopeListener();
listener.startBuildOperand(node, s);
}
Operand operand = buildOperand(node, s);
if (hasListener()) {
IRScopeListener listener = manager.getIRScopeListener();
listener.endBuildOperand(node, s, operand);
}
return operand;
}
public Operand buildLambda(LambdaNode node, IRScope s) {
IRClosure closure = new IRClosure(manager, s, node.getPosition().getStartLine(), node.getScope(), Arity.procArityOf(node.getArgs()), node.getArgumentType());
// Create a new nested builder to ensure this gets its own IR builder state
// like the ensure block stack
IRBuilder closureBuilder = newIRBuilder(manager);
// Receive self
closureBuilder.addInstr(closure, new ReceiveSelfInstr(closure.getSelf()));
// args
closureBuilder.receiveBlockArgs(node, closure);
Operand closureRetVal = node.getBody() == null ? manager.getNil() : closureBuilder.build(node.getBody(), closure);
// can be U_NIL if the node is an if node with returns in both branches.
if (closureRetVal != U_NIL) closureBuilder.addInstr(closure, new ReturnInstr(closureRetVal));
handleBreakAndReturnsInLambdas(closure);
Variable lambda = s.createTemporaryVariable();
// SSS FIXME: Is this the right self here?
WrappedIRClosure lambdaBody = new WrappedIRClosure(s.getSelf(), closure);
addInstr(s, new BuildLambdaInstr(lambda, lambdaBody, node.getPosition()));
return lambda;
}
public Operand buildEncoding(EncodingNode node, IRScope s) {
Variable ret = s.createTemporaryVariable();
addInstr(s, new GetEncodingInstr(ret, node.getEncoding()));
return ret;
}
// Non-arg masgn
public Operand buildMultipleAsgn19(MultipleAsgn19Node multipleAsgnNode, IRScope s) {
Operand values = build(multipleAsgnNode.getValueNode(), s);
Variable ret = getValueInTemporaryVariable(s, values);
Variable tmp = s.createTemporaryVariable();
addInstr(s, new ToAryInstr(tmp, ret));
buildMultipleAsgn19Assignment(multipleAsgnNode, s, null, tmp);
return ret;
}
protected Variable copyAndReturnValue(IRScope s, Operand val) {
return addResultInstr(s, new CopyInstr(s.createTemporaryVariable(), val));
}
protected Variable getValueInTemporaryVariable(IRScope s, Operand val) {
if (val != null && val instanceof TemporaryVariable) return (Variable) val;
return copyAndReturnValue(s, val);
}
// Return the last argument in the list -- AttrAssign needs it
protected Operand buildCallArgs(List<Operand> argsList, Node args, IRScope s) {
// unwrap newline nodes to get their actual type
args = skipOverNewlines(s, args);
switch (args.getNodeType()) {
case ARGSCATNODE: {
ArgsCatNode argsCatNode = (ArgsCatNode)args;
Operand v1 = build(argsCatNode.getFirstNode(), s);
Operand v2 = build(argsCatNode.getSecondNode(), s);
Variable res = s.createTemporaryVariable();
addInstr(s, new BuildCompoundArrayInstr(res, v1, v2, false));
argsList.add(new Splat(res, true));
return v2;
}
case ARGSPUSHNODE: {
ArgsPushNode argsPushNode = (ArgsPushNode)args;
Operand v1 = build(argsPushNode.getFirstNode(), s);
Operand v2 = build(argsPushNode.getSecondNode(), s);
Variable res = s.createTemporaryVariable();
addInstr(s, new BuildCompoundArrayInstr(res, v1, v2, true));
argsList.add(new Splat(res, true));
return v2;
}
case ARRAYNODE: {
ArrayNode arrayNode = (ArrayNode)args;
if (arrayNode.isLightweight()) {
List<Node> children = arrayNode.childNodes();
// explode array, it's an internal "args" array
for (Node n: children) {
argsList.add(build(n, s));
}
} else {
// use array as-is, it's a literal array
argsList.add(build(arrayNode, s));
}
break;
}
case SPLATNODE: {
Splat splat = new Splat(build(((SplatNode)args).getValue(), s), true);
argsList.add(splat);
break;
}
default: {
argsList.add(build(args, s));
break;
}
}
return argsList.isEmpty() ? manager.getNil() : argsList.get(argsList.size() - 1);
}
public List<Operand> setupCallArgs(Node args, IRScope s) {
List<Operand> argsList = new ArrayList<Operand>();
if (args != null) buildCallArgs(argsList, args, s);
return argsList;
}
// Non-arg masgn (actually a nested masgn)
public void buildVersionSpecificAssignment(Node node, IRScope s, Variable v) {
switch (node.getNodeType()) {
case MULTIPLEASGN19NODE: {
Variable tmp = s.createTemporaryVariable();
addInstr(s, new ToAryInstr(tmp, v));
buildMultipleAsgn19Assignment((MultipleAsgn19Node)node, s, null, tmp);
break;
}
default:
throw new NotCompilableException("Can't build assignment node: " + node);
}
}
// This method is called to build assignments for a multiple-assignment instruction
public void buildAssignment(Node node, IRScope s, Variable rhsVal) {
switch (node.getNodeType()) {
case ATTRASSIGNNODE:
buildAttrAssignAssignment(node, s, rhsVal);
break;
case CLASSVARASGNNODE:
addInstr(s, new PutClassVariableInstr(classVarDefinitionContainer(s), ((ClassVarAsgnNode)node).getName(), rhsVal));
break;
case CLASSVARDECLNODE:
addInstr(s, new PutClassVariableInstr(classVarDeclarationContainer(s), ((ClassVarDeclNode)node).getName(), rhsVal));
break;
case CONSTDECLNODE:
buildConstDeclAssignment((ConstDeclNode) node, s, rhsVal);
break;
case DASGNNODE: {
DAsgnNode variable = (DAsgnNode) node;
int depth = variable.getDepth();
addInstr(s, new CopyInstr(s.getLocalVariable(variable.getName(), depth), rhsVal));
break;
}
case GLOBALASGNNODE:
addInstr(s, new PutGlobalVarInstr(((GlobalAsgnNode)node).getName(), rhsVal));
break;
case INSTASGNNODE:
// NOTE: if 's' happens to the a class, this is effectively an assignment of a class instance variable
addInstr(s, new PutFieldInstr(s.getSelf(), ((InstAsgnNode)node).getName(), rhsVal));
break;
case LOCALASGNNODE: {
LocalAsgnNode localVariable = (LocalAsgnNode) node;
int depth = localVariable.getDepth();
addInstr(s, new CopyInstr(s.getLocalVariable(localVariable.getName(), depth), rhsVal));
break;
}
case ZEROARGNODE:
throw new NotCompilableException("Shouldn't get here; zeroarg does not do assignment: " + node);
default:
buildVersionSpecificAssignment(node, s, rhsVal);
}
}
protected LocalVariable getBlockArgVariable(IRScope s, String name, int depth) {
if (!(s instanceof IRFor)) throw new NotCompilableException("Cannot ask for block-arg variable in 1.9 mode");
return s.getLocalVariable(name, depth);
}
protected void receiveBlockArg(IRScope s, Variable v, Operand argsArray, int argIndex, boolean isSplat) {
if (argsArray != null) {
// We are in a nested receive situation -- when we are not at the root of a masgn tree
// Ex: We are trying to receive (b,c) in this example: "|a, (b,c), d| = ..."
if (isSplat) addInstr(s, new RestArgMultipleAsgnInstr(v, argsArray, argIndex));
else addInstr(s, new ReqdArgMultipleAsgnInstr(v, argsArray, argIndex));
} else {
// argsArray can be null when the first node in the args-node-ast is a multiple-assignment
// For example, for-nodes
addInstr(s, isSplat ? new ReceiveRestArgInstr(v, argIndex, argIndex) : new ReceivePreReqdArgInstr(v, argIndex));
}
}
public void buildVersionSpecificBlockArgsAssignment(Node node, IRScope s) {
if (!(s instanceof IRFor)) throw new NotCompilableException("Should not have come here for block args assignment in 1.9 mode: " + node);
// Argh! For-loop bodies and regular iterators are different in terms of block-args!
switch (node.getNodeType()) {
case MULTIPLEASGN19NODE: {
ListNode sourceArray = ((MultipleAsgn19Node) node).getPre();
int i = 0;
for (Node an: sourceArray.childNodes()) {
// Use 1.8 mode version for this
buildBlockArgsAssignment(an, s, null, i, false);
i++;
}
break;
}
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, IRScope s, Operand argsArray, int argIndex, boolean isSplat) {
Variable v;
switch (node.getNodeType()) {
case ATTRASSIGNNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
buildAttrAssignAssignment(node, s, v);
break;
case DASGNNODE: {
DAsgnNode dynamicAsgn = (DAsgnNode) node;
v = getBlockArgVariable(s, dynamicAsgn.getName(), dynamicAsgn.getDepth());
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
break;
}
case CLASSVARASGNNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
addInstr(s, new PutClassVariableInstr(classVarDefinitionContainer(s), ((ClassVarAsgnNode)node).getName(), v));
break;
case CLASSVARDECLNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
addInstr(s, new PutClassVariableInstr(classVarDeclarationContainer(s), ((ClassVarDeclNode)node).getName(), v));
break;
case CONSTDECLNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
buildConstDeclAssignment((ConstDeclNode) node, s, v);
break;
case GLOBALASGNNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
addInstr(s, new PutGlobalVarInstr(((GlobalAsgnNode)node).getName(), v));
break;
case INSTASGNNODE:
v = s.createTemporaryVariable();
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
// NOTE: if 's' happens to the a class, this is effectively an assignment of a class instance variable
addInstr(s, new PutFieldInstr(s.getSelf(), ((InstAsgnNode)node).getName(), v));
break;
case LOCALASGNNODE: {
LocalAsgnNode localVariable = (LocalAsgnNode) node;
int depth = localVariable.getDepth();
v = getBlockArgVariable(s, localVariable.getName(), depth);
receiveBlockArg(s, v, argsArray, argIndex, isSplat);
break;
}
case ZEROARGNODE:
throw new NotCompilableException("Shouldn't get here; zeroarg does not do assignment: " + node);
default:
buildVersionSpecificBlockArgsAssignment(node, s);
}
}
public Operand buildAlias(final AliasNode alias, IRScope s) {
Operand newName = build(alias.getNewName(), s);
Operand oldName = build(alias.getOldName(), s);
addInstr(s, new AliasInstr(newName, oldName));
return manager.getNil();
}
// 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 = v1
// beq(v1, false, L)
// v2 = -- build(b) --
// ret = v2
// L:
//
public Operand buildAnd(final AndNode andNode, IRScope s) {
if (andNode.getFirstNode().getNodeType().alwaysTrue()) {
// build first node (and ignore its result) and then second node
build(andNode.getFirstNode(), s);
return build(andNode.getSecondNode(), s);
} else if (andNode.getFirstNode().getNodeType().alwaysFalse()) {
// build first node only and return its value
return build(andNode.getFirstNode(), s);
} else {
Label l = s.getNewLabel();
Operand v1 = build(andNode.getFirstNode(), s);
Variable ret = getValueInTemporaryVariable(s, v1);
addInstr(s, BEQInstr.create(v1, manager.getFalse(), l));
Operand v2 = build(andNode.getSecondNode(), s);
addInstr(s, new CopyInstr(ret, v2));
addInstr(s, new LabelInstr(l));
return ret;
}
}
public Operand buildArray(Node node, IRScope s) {
List<Operand> elts = new ArrayList<Operand>();
for (Node e: node.childNodes())
elts.add(build(e, s));
return copyAndReturnValue(s, new Array(elts));
}
public Operand buildArgsCat(final ArgsCatNode argsCatNode, IRScope s) {
Operand v1 = build(argsCatNode.getFirstNode(), s);
Operand v2 = build(argsCatNode.getSecondNode(), s);
Variable res = s.createTemporaryVariable();
addInstr(s, new BuildCompoundArrayInstr(res, v1, v2, false));
return res;
}
public Operand buildArgsPush(final ArgsPushNode node, IRScope s) {
Operand v1 = build(node.getFirstNode(), s);
Operand v2 = build(node.getSecondNode(), s);
Variable res = s.createTemporaryVariable();
addInstr(s, new BuildCompoundArrayInstr(res, v1, v2, true));
return res;
}
private Operand buildAttrAssign(final AttrAssignNode attrAssignNode, IRScope s) {
Operand obj = build(attrAssignNode.getReceiverNode(), s);
List<Operand> args = new ArrayList<Operand>();
Node argsNode = attrAssignNode.getArgsNode();
Operand lastArg = (argsNode == null) ? manager.getNil() : buildCallArgs(args, argsNode, s);
addInstr(s, new AttrAssignInstr(obj, new MethAddr(attrAssignNode.getName()), args.toArray(new Operand[args.size()])));
return lastArg;
}
public Operand buildAttrAssignAssignment(Node node, IRScope s, Operand value) {
final AttrAssignNode attrAssignNode = (AttrAssignNode) node;
Operand obj = build(attrAssignNode.getReceiverNode(), s);
List<Operand> args = setupCallArgs(attrAssignNode.getArgsNode(), s);
args.add(value);
addInstr(s, new AttrAssignInstr(obj, new MethAddr(attrAssignNode.getName()), args.toArray(new Operand[args.size()])));
return value;
}
public Operand buildBackref(BackRefNode node, IRScope s) {
// SSS FIXME: Required? Verify with Tom/Charlie
return copyAndReturnValue(s, new Backref(node.getType()));
}
public Operand buildBegin(BeginNode beginNode, IRScope s) {
return build(beginNode.getBodyNode(), s);
}
public Operand buildBignum(BignumNode node, IRScope s) {
// SSS: Since bignum literals are effectively interned objects, no need to copyAndReturnValue(...)
// Or is this a premature optimization?
return new Bignum(node.getValue());
}
public Operand buildBlock(BlockNode node, IRScope 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, IRScope s) {
IRLoop currLoop = getCurrentLoop();
Operand rv = build(breakNode.getValueNode(), s);
// If we have ensure blocks, have to run those first!
if (!activeEnsureBlockStack.empty()) emitEnsureBlocks(s, currLoop);
else if (!activeRescueBlockStack.empty()) activeRescueBlockStack.peek().restoreException(this, s, currLoop);
if (currLoop != null) {
addInstr(s, new CopyInstr(currLoop.loopResult, rv));
addInstr(s, new JumpInstr(currLoop.loopEndLabel));
} else {
if (s instanceof IRClosure) {
// This lexical scope value is only used (and valid) in regular block contexts.
// If this instruction is executed in a Proc or Lambda context, the lexical scope value is useless.
IRScope returnScope = s.getLexicalParent();
// In 1.9 and later modes, no breaks from evals
if (s instanceof IREvalScript || returnScope == null) addInstr(s, new ThrowExceptionInstr(IRException.BREAK_LocalJumpError));
else addInstr(s, new BreakInstr(rv, returnScope.getName()));
} else {
// We are not in a closure or a loop => bad break instr!
addInstr(s, new ThrowExceptionInstr(IRException.BREAK_LocalJumpError));
}
}
// Once the break instruction executes, control exits this scope
return U_NIL;
}
private void handleNonlocalReturnInMethod(IRScope s) {
Label rBeginLabel = s.getNewLabel();
Label rEndLabel = s.getNewLabel();
Label gebLabel = s.getNewLabel();
// Protect the entire body as it exists now with the global ensure block
//
// Add label and marker instruction in reverse order to the beginning
// so that the label ends up being the first instr.
addInstrAtBeginning(s, new ExceptionRegionStartMarkerInstr(gebLabel));
addInstrAtBeginning(s, new LabelInstr(rBeginLabel));
addInstr(s, new ExceptionRegionEndMarkerInstr());
// Receive exceptions (could be anything, but the handler only processes IRReturnJumps)
addInstr(s, new LabelInstr(gebLabel));
Variable exc = s.createTemporaryVariable();
addInstr(s, new ReceiveJRubyExceptionInstr(exc));
if (RubyInstanceConfig.FULL_TRACE_ENABLED) {
addInstr(s, new TraceInstr(RubyEvent.RETURN, s.getName(), s.getFileName(), -1));
}
// Handle break using runtime helper
// --> IRRuntimeHelpers.handleNonlocalReturn(scope, bj, blockType)
Variable ret = s.createTemporaryVariable();
addInstr(s, new RuntimeHelperCall(ret, HANDLE_NONLOCAL_RETURN, new Operand[]{exc} ));
addInstr(s, new ReturnInstr(ret));
// End
addInstr(s, new LabelInstr(rEndLabel));
}
private Operand receiveBreakException(IRScope s, Operand block, CodeBlock codeBlock, int linenumber) {
// Check if we have to handle a break
if (block == null ||
!(block instanceof WrappedIRClosure) ||
!(((WrappedIRClosure)block).getClosure()).flags.contains(IRFlags.HAS_BREAK_INSTRS)) {
// No protection needed -- add the call and return
return codeBlock.run();
}
Label rBeginLabel = s.getNewLabel();
Label rEndLabel = s.getNewLabel();
Label rescueLabel = s.getNewLabel();
// Protected region
addInstr(s, new LabelInstr(rBeginLabel));
addInstr(s, new ExceptionRegionStartMarkerInstr(rescueLabel));
Variable callResult = (Variable)codeBlock.run();
addInstr(s, new JumpInstr(rEndLabel));
addInstr(s, new ExceptionRegionEndMarkerInstr());
// Receive exceptions (could be anything, but the handler only processes IRBreakJumps)
addInstr(s, new LabelInstr(rescueLabel));
Variable exc = s.createTemporaryVariable();
addInstr(s, new ReceiveJRubyExceptionInstr(exc));
// Handle break using runtime helper
// --> IRRuntimeHelpers.handlePropagatedBreak(context, scope, bj, blockType)
addInstr(s, new RuntimeHelperCall(callResult, HANDLE_PROPAGATE_BREAK, new Operand[]{exc} ));
// End
addInstr(s, new LabelInstr(rEndLabel));
return callResult;
}
// Wrap call in a rescue handler that catches the IRBreakJump
private void receiveBreakException(final IRScope s, Operand block, final CallInstr callInstr, int linenumber) {
receiveBreakException(s, block, new CodeBlock() { public Operand run() { addInstr(s, callInstr); return callInstr.getResult(); } }, linenumber);
}
public Operand buildCall(CallNode callNode, IRScope s) {
Node callArgsNode = callNode.getArgsNode();
Node receiverNode = callNode.getReceiverNode();
// Though you might be tempted to move this build into the CallInstr as:
// new Callinstr( ... , build(receiverNode, s), ...)
// that is incorrect IR because the receiver has to be built *before* call arguments are built
// to preserve expected code execution order
Operand receiver = build(receiverNode, s);
List<Operand> args = setupCallArgs(callArgsNode, s);
Operand block = setupCallClosure(callNode.getIterNode(), s);
Variable callResult = s.createTemporaryVariable();
CallInstr callInstr = CallInstr.create(callResult, new MethAddr(callNode.getName()), receiver, args.toArray(new Operand[args.size()]), block);
receiveBreakException(s, block, callInstr, callNode.getPosition().getStartLine());
return callResult;
}
public Operand buildCase(CaseNode caseNode, IRScope s) {
// get the incoming case value
Operand value = build(caseNode.getCaseNode(), s);
// This is for handling case statements without a value (see example below)
// case
// when true <blah>
// when false <blah>
// end
if (value == null) value = UndefinedValue.UNDEFINED;
Label endLabel = s.getNewLabel();
boolean hasElse = (caseNode.getElseNode() != null);
Label elseLabel = s.getNewLabel();
Variable result = s.createTemporaryVariable();