FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/src/org/jruby/ir/IRBuilder.java at parallel_boot · MSNexploder/jruby · GitHub
jruby/src/org/jruby/ir/IRBuilder.java at parallel_boot · MSNexploder/jruby · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
ir
/
IRBuilder.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
3202 lines (2803 loc) · 147 KB
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
ir
/
IRBuilder.java
Copy path
File metadata and controls
3202 lines (2803 loc) · 147 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
.
Ruby
;
import
org
.
jruby
.
RubyInstanceConfig
.
CompileMode
;
import
org
.
jruby
.
ast
.*;
import
org
.
jruby
.
compiler
.
NotCompilableException
;
import
org
.
jruby
.
ir
.
instructions
.*;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
BackrefIsMatchDataInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
ClassVarIsDefinedInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
GetDefinedConstantOrMethodInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
GetErrorInfoInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
GlobalIsDefinedInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
HasInstanceVarInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
IsMethodBoundInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
MethodDefinedInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
MethodIsPublicInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
RestoreErrorInfoInstr
;
import
org
.
jruby
.
ir
.
instructions
.
defined
.
SuperMethodBoundInstr
;
import
org
.
jruby
.
ir
.
instructions
.
ruby18
.
ReceiveOptArgInstr18
;
import
org
.
jruby
.
ir
.
instructions
.
ruby18
.
ReceiveRestArgInstr18
;
import
org
.
jruby
.
ir
.
operands
.
Array
;
import
org
.
jruby
.
ir
.
operands
.
AsString
;
import
org
.
jruby
.
ir
.
operands
.
Backref
;
import
org
.
jruby
.
ir
.
operands
.
BacktickString
;
import
org
.
jruby
.
ir
.
operands
.
Bignum
;
import
org
.
jruby
.
ir
.
operands
.
CompoundArray
;
import
org
.
jruby
.
ir
.
operands
.
CompoundString
;
import
org
.
jruby
.
ir
.
operands
.
ScopeModule
;
import
org
.
jruby
.
ir
.
operands
.
CurrentScope
;
import
org
.
jruby
.
ir
.
operands
.
DynamicSymbol
;
import
org
.
jruby
.
ir
.
operands
.
Fixnum
;
import
org
.
jruby
.
ir
.
operands
.
Float
;
import
org
.
jruby
.
ir
.
operands
.
Hash
;
import
org
.
jruby
.
ir
.
operands
.
IRException
;
import
org
.
jruby
.
ir
.
operands
.
KeyValuePair
;
import
org
.
jruby
.
ir
.
operands
.
Label
;
import
org
.
jruby
.
ir
.
operands
.
LocalVariable
;
import
org
.
jruby
.
ir
.
operands
.
MethAddr
;
import
org
.
jruby
.
ir
.
operands
.
NthRef
;
import
org
.
jruby
.
ir
.
operands
.
ObjectClass
;
import
org
.
jruby
.
ir
.
operands
.
Operand
;
import
org
.
jruby
.
ir
.
operands
.
Range
;
import
org
.
jruby
.
ir
.
operands
.
Regexp
;
import
org
.
jruby
.
ir
.
operands
.
SValue
;
import
org
.
jruby
.
ir
.
operands
.
Splat
;
import
org
.
jruby
.
ir
.
operands
.
StringLiteral
;
import
org
.
jruby
.
ir
.
operands
.
Symbol
;
import
org
.
jruby
.
ir
.
operands
.
TemporaryVariable
;
import
org
.
jruby
.
ir
.
operands
.
UndefinedValue
;
import
org
.
jruby
.
ir
.
operands
.
UnexecutableNil
;
import
org
.
jruby
.
ir
.
operands
.
Variable
;
import
org
.
jruby
.
ir
.
operands
.
WrappedIRClosure
;
import
org
.
jruby
.
parser
.
StaticScope
;
import
org
.
jruby
.
runtime
.
Arity
;
import
org
.
jruby
.
runtime
.
BlockBody
;
import
org
.
jruby
.
runtime
.
CallType
;
import
org
.
jruby
.
util
.
ByteList
;
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
.
Map
;
import
java
.
util
.
Stack
;
// 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
{
protected
static
final
Operand
[]
NO_ARGS
=
new
Operand
[]{};
protected
static
final
UnexecutableNil
U_NIL
=
UnexecutableNil
.
U_NIL
;
private
static
String
rubyVersion
=
"1.8"
;
// default is 1.8
public
static
void
setRubyVersion
(
String
rubyVersion
) {
IRBuilder
.
rubyVersion
=
rubyVersion
;
}
public
boolean
is1_9
() {
return
false
;
}
/* -----------------------------------------------------------------------------------
* Every ensure block has a start label and end label, and at the end, it will 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
* -----------------
* L_region_start
* 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
regionStart
;
Label
start
;
Label
end
;
Label
dummyRescueBlockLabel
;
Variable
returnAddr
;
Variable
savedGlobalException
;
// 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
;
public
EnsureBlockInfo
(
IRScope
s
,
RescueNode
n
,
IRLoop
l
) {
regionStart
=
s
.
getNewLabel
();
start
=
s
.
getNewLabel
();
end
=
s
.
getNewLabel
();
returnAddr
=
s
.
getNewTemporaryVariable
();
dummyRescueBlockLabel
=
s
.
getNewLabel
();
savedGlobalException
=
null
;
innermostLoop
=
l
;
matchingRescueNode
=
n
;
}
// Emit jump chain by walking up the ensure block stack
// If we have been passed a loop value, then emit values that are nested within that loop
public
static
void
emitJumpChain
(
IRScope
s
,
Stack
<
EnsureBlockInfo
>
ebStack
,
IRLoop
loop
) {
// SSS: There are 2 ways of encoding this:
// 1. Jump to ensure block 1, return back here, jump ensure block 2, return back here, ...
// Generates 3*n instrs. where n is the # of ensure blocks to execute
// 2. Jump to ensure block 1, then to block 2, then to 3, ...
// Generates n+1 instrs. where n is the # of ensure blocks to execute
// Doesn't really matter all that much since we shouldn't have deep nesting of ensure blocks often
// but is there a reason to go with technique 1 at all??
int
n
=
ebStack
.
size
();
EnsureBlockInfo
[]
ebArray
=
ebStack
.
toArray
(
new
EnsureBlockInfo
[
n
]);
for
(
int
i
=
n
-
1
;
i
>=
0
;
i
--) {
EnsureBlockInfo
ebi
=
ebArray
[
i
];
//
if
(
ebi
.
innermostLoop
!=
loop
)
break
;
Label
retLabel
=
s
.
getNewLabel
();
if
(
ebi
.
savedGlobalException
!=
null
) {
s
.
addInstr
(
new
PutGlobalVarInstr
(
"$!"
,
ebi
.
savedGlobalException
));
}
s
.
addInstr
(
new
SetReturnAddressInstr
(
ebi
.
returnAddr
,
retLabel
));
s
.
addInstr
(
new
JumpInstr
(
ebi
.
start
));
s
.
addInstr
(
new
LabelInstr
(
retLabel
));
}
}
}
// Stack encoding nested ensure blocks
private
Stack
<
EnsureBlockInfo
>
_ensureBlockStack
=
new
Stack
<
EnsureBlockInfo
>();
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 ensure 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
(
IRScope
s
,
IRLoop
currLoop
) {
if
(
currLoop
==
innermostLoop
)
s
.
addInstr
(
new
PutGlobalVarInstr
(
"$!"
,
savedExceptionVariable
));
}
}
// Stack encoding nested rescue blocks -- this just tracks the start label of the blocks
private
Stack
<
RescueBlockInfo
>
_rescueBlockStack
=
new
Stack
<
RescueBlockInfo
>();
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
;
}
public
static
Node
buildAST
(
boolean
isCommandLineScript
,
String
arg
) {
Ruby
ruby
=
Ruby
.
getGlobalRuntime
();
// set to IR mode, since we use different scopes, etc for IR
ruby
.
getInstanceConfig
().
setCompileMode
(
CompileMode
.
OFFIR
);
// 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
e
) { }
}
}
public
static
IRBuilder
createIRBuilder
(
IRManager
manager
,
boolean
is19
) {
return
is19
?
new
IRBuilder19
(
manager
) :
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
) {
s
.
addInstr
(
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
);
}
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
,
s
, ((
ConstNode
)
node
).
getName
());
case
DASGNNODE
:
return
buildDAsgn
((
DAsgnNode
)
node
,
s
);
case
DEFINEDNODE
:
return
buildGetDefinitionBase
(((
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
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
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
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
NOTNODE
:
return
buildNot
((
NotNode
)
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
((
SelfNode
)
node
,
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
TOARYNODE
:
return
buildToAry
((
ToAryNode
)
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
(
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
:
return
buildVersionSpecificNodes
(
node
,
s
);
}
}
protected
Operand
buildVersionSpecificNodes
(
Node
node
,
IRScope
s
) {
throw
new
NotCompilableException
(
"Unknown node encountered in builder: "
+
node
.
getClass
());
}
protected
Variable
getSelf
(
IRScope
s
) {
return
s
.
getSelf
();
}
protected
Variable
copyAndReturnValue
(
IRScope
s
,
Operand
val
) {
Variable
v
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
CopyInstr
(
v
,
val
));
return
v
;
}
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
: {
CompoundArray
a
= (
CompoundArray
)
build
(
args
,
s
);
argsList
.
add
(
new
Splat
(
a
));
return
a
.
getAppendedArg
();
}
case
ARGSPUSHNODE
: {
ArgsPushNode
ap
= (
ArgsPushNode
)
args
;
Operand
v1
=
build
(
ap
.
getFirstNode
(),
s
);
Operand
v2
=
build
(
ap
.
getSecondNode
(),
s
);
argsList
.
add
(
new
Splat
(
new
CompoundArray
(
v1
,
v2
,
true
)));
return
v2
;
}
case
ARRAYNODE
: {
ArrayNode
arrayNode
= (
ArrayNode
)
args
;
if
(
arrayNode
.
isLightweight
()) {
List
<
Node
>
children
=
arrayNode
.
childNodes
();
if
(
children
.
size
() ==
1
) {
// skipOverNewlines is required because the parser inserts a NewLineNode in between!
Node
child
=
skipOverNewlines
(
s
,
children
.
get
(
0
));
if
(
child
instanceof
SplatNode
) {
// SSS: If the only child is a splat, the splat is supposed to get through
// as an array without being expanded into the call arg list.
//
// The AST for the foo([*1]) is: ArrayNode(Splat19Node(..))
// The AST for the foo(*1) is: Splat19Node(..)
//
// Since a lone splat in call args is always expanded, we convert the splat
// into a compound array: *n --> args-cat([], *n)
SplatNode
splat
= (
SplatNode
)
child
;
Variable
splatArray
=
getValueInTemporaryVariable
(
s
,
build
(
splat
.
getValue
(),
s
));
argsList
.
add
(
new
CompoundArray
(
new
Array
(),
splatArray
));
return
new
Splat
(
splatArray
);
}
else
{
Operand
childOperand
=
build
(
child
,
s
);
argsList
.
add
(
childOperand
);
return
childOperand
;
}
}
else
{
// 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
;
}
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
;
}
public
void
buildVersionSpecificAssignment
(
Node
node
,
IRScope
s
,
Variable
v
) {
switch
(
node
.
getNodeType
()) {
case
MULTIPLEASGNNODE
: {
Operand
valuesArg
;
MultipleAsgnNode
childNode
= (
MultipleAsgnNode
)
node
;
if
(
childNode
.
getHeadNode
() !=
null
&& ((
ListNode
)
childNode
.
getHeadNode
()).
childNodes
().
size
() >
0
) {
// Invoke to_ary on the operand only if it is not an array already
Variable
result
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
ToAryInstr
(
result
,
v
,
manager
.
getTrue
()));
valuesArg
=
result
;
}
else
{
s
.
addInstr
(
new
EnsureRubyArrayInstr
(
v
,
v
));
valuesArg
=
v
;
}
buildMultipleAsgnAssignment
(
childNode
,
s
,
null
,
valuesArg
);
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
:
s
.
addInstr
(
new
PutClassVariableInstr
(
classVarDefinitionContainer
(
s
), ((
ClassVarAsgnNode
)
node
).
getName
(),
rhsVal
));
break
;
case
CLASSVARDECLNODE
:
s
.
addInstr
(
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
();
s
.
addInstr
(
new
CopyInstr
(
s
.
getLocalVariable
(
variable
.
getName
(),
depth
),
rhsVal
));
break
;
}
case
GLOBALASGNNODE
:
s
.
addInstr
(
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
s
.
addInstr
(
new
PutFieldInstr
(
getSelf
(
s
), ((
InstAsgnNode
)
node
).
getName
(),
rhsVal
));
break
;
case
LOCALASGNNODE
: {
LocalAsgnNode
localVariable
= (
LocalAsgnNode
)
node
;
int
depth
=
localVariable
.
getDepth
();
s
.
addInstr
(
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
cl
,
String
name
,
int
depth
) {
return
cl
.
getLocalVariable
(
name
,
depth
);
}
protected
void
receiveBlockArg
(
IRScope
s
,
Variable
v
,
Operand
argsArray
,
int
argIndex
,
boolean
isClosureArg
,
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
)
s
.
addInstr
(
new
RestArgMultipleAsgnInstr
(
v
,
argsArray
,
argIndex
));
else
s
.
addInstr
(
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
s
.
addInstr
(
isClosureArg
?
new
ReceiveClosureInstr
(
v
) : (
isSplat
?
new
ReceiveRestArgInstr18
(
v
,
argIndex
) :
new
ReceivePreReqdArgInstr
(
v
,
argIndex
)));
}
}
public
void
buildVersionSpecificBlockArgsAssignment
(
Node
node
,
IRScope
s
,
Operand
argsArray
,
int
argIndex
,
boolean
isMasgnRoot
,
boolean
isClosureArg
,
boolean
isSplat
) {
switch
(
node
.
getNodeType
()) {
case
MULTIPLEASGNNODE
: {
Variable
oldArgs
=
null
;
MultipleAsgnNode
childNode
= (
MultipleAsgnNode
)
node
;
if
(!
isMasgnRoot
) {
// Vars used to receive args should always be local-variables because
// these arg values may need to be accessed by some zsuper instruction.
// During interpretation, only local-vars are accessible (at least right now)
// outside the scope they are defined in.
Variable
v
=
s
.
getLocalVariable
(
"%_masgn_arg_"
+
argIndex
,
0
);
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
boolean
runToAry
=
childNode
.
getHeadNode
() !=
null
&& (((
ListNode
)
childNode
.
getHeadNode
()).
childNodes
().
size
() >
0
);
if
(
runToAry
) {
s
.
addInstr
(
new
ToAryInstr
(
v
,
v
,
manager
.
getFalse
()));
}
else
{
s
.
addInstr
(
new
EnsureRubyArrayInstr
(
v
,
v
));
}
argsArray
=
v
;
// SSS FIXME: Are we guaranteed that splats dont head to multiple-assignment nodes! i.e. |*(a,b)|?
}
// Build
buildMultipleAsgnAssignment
(
childNode
,
s
,
argsArray
,
null
);
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
isMasgnRoot
,
boolean
isClosureArg
,
boolean
isSplat
) {
Variable
v
;
switch
(
node
.
getNodeType
()) {
case
ATTRASSIGNNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
buildAttrAssignAssignment
(
node
,
s
,
v
);
break
;
case
DASGNNODE
: {
DAsgnNode
dynamicAsgn
= (
DAsgnNode
)
node
;
v
=
getBlockArgVariable
((
IRClosure
)
s
,
dynamicAsgn
.
getName
(),
dynamicAsgn
.
getDepth
());
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
break
;
}
case
CLASSVARASGNNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
s
.
addInstr
(
new
PutClassVariableInstr
(
classVarDefinitionContainer
(
s
), ((
ClassVarAsgnNode
)
node
).
getName
(),
v
));
break
;
case
CLASSVARDECLNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
s
.
addInstr
(
new
PutClassVariableInstr
(
classVarDeclarationContainer
(
s
), ((
ClassVarDeclNode
)
node
).
getName
(),
v
));
break
;
case
CONSTDECLNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
buildConstDeclAssignment
((
ConstDeclNode
)
node
,
s
,
v
);
break
;
case
GLOBALASGNNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
s
.
addInstr
(
new
PutGlobalVarInstr
(((
GlobalAsgnNode
)
node
).
getName
(),
v
));
break
;
case
INSTASGNNODE
:
v
=
s
.
getNewTemporaryVariable
();
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
// NOTE: if 's' happens to the a class, this is effectively an assignment of a class instance variable
s
.
addInstr
(
new
PutFieldInstr
(
getSelf
(
s
), ((
InstAsgnNode
)
node
).
getName
(),
v
));
break
;
case
LOCALASGNNODE
: {
LocalAsgnNode
localVariable
= (
LocalAsgnNode
)
node
;
int
depth
=
localVariable
.
getDepth
();
v
=
getBlockArgVariable
((
IRClosure
)
s
,
localVariable
.
getName
(),
depth
);
receiveBlockArg
(
s
,
v
,
argsArray
,
argIndex
,
isClosureArg
,
isSplat
);
break
;
}
case
ZEROARGNODE
:
throw
new
NotCompilableException
(
"Shouldn't get here; zeroarg does not do assignment: "
+
node
);
default
:
buildVersionSpecificBlockArgsAssignment
(
node
,
s
,
argsArray
,
argIndex
,
isMasgnRoot
,
isClosureArg
,
isSplat
);
}
}
public
Operand
buildAlias
(
final
AliasNode
alias
,
IRScope
s
) {
Operand
newName
=
build
(
alias
.
getNewName
(),
s
);
Operand
oldName
=
build
(
alias
.
getOldName
(),
s
);
s
.
addInstr
(
new
AliasInstr
(
getSelf
(
s
),
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
);
s
.
addInstr
(
BEQInstr
.
create
(
v1
,
manager
.
getFalse
(),
l
));
Operand
v2
=
build
(
andNode
.
getSecondNode
(),
s
);
s
.
addInstr
(
new
CopyInstr
(
ret
,
v2
));
s
.
addInstr
(
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
);
return
new
CompoundArray
(
v1
,
v2
);
}
public
Operand
buildArgsPush
(
final
ArgsPushNode
node
,
IRScope
s
) {
throw
new
NotCompilableException
(
"ArgsPush should never be encountered bare in 1.8"
+
node
);
}
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
);
s
.
addInstr
(
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
);
s
.
addInstr
(
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
(!
_ensureBlockStack
.
empty
())
EnsureBlockInfo
.
emitJumpChain
(
s
,
_ensureBlockStack
,
currLoop
);
else
if
(!
_rescueBlockStack
.
empty
())
_rescueBlockStack
.
peek
().
restoreException
(
s
,
currLoop
);
if
(
currLoop
!=
null
) {
s
.
addInstr
(
new
CopyInstr
(
currLoop
.
loopResult
,
rv
));
s
.
addInstr
(
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
();
if
(
is1_9
()) {
// In 1.9 mode, no breaks from evals
if
(
s
instanceof
IREvalScript
)
s
.
addInstr
(
new
ThrowExceptionInstr
(
IRException
.
BREAK_LocalJumpError
));
else
s
.
addInstr
(
new
BreakInstr
(
rv
,
returnScope
));
}
else
{
// In pre-1.9 mode, breaks from evals are legitimate!
if
(
s
instanceof
IREvalScript
)
returnScope
=
returnScope
.
getLexicalParent
();
s
.
addInstr
(
new
BreakInstr
(
rv
,
returnScope
));
}
}
else
{
// We are not in a closure or a loop => bad break instr!
s
.
addInstr
(
new
ThrowExceptionInstr
(
IRException
.
BREAK_LocalJumpError
));
}
}
// Once the break instruction executes, control exits this scope
return
UnexecutableNil
.
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
s
.
addInstrAtBeginning
(
new
ExceptionRegionStartMarkerInstr
(
rBeginLabel
,
rEndLabel
,
gebLabel
,
gebLabel
));
s
.
addInstr
(
new
ExceptionRegionEndMarkerInstr
());
// Receive exceptions (could be anything, but the handler only processes IRReturnJumps)
s
.
addInstr
(
new
LabelInstr
(
gebLabel
));
Variable
exc
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
ReceiveExceptionInstr
(
exc
,
false
));
// no type-checking
// Handle break using runtime helper
// --> IRRuntimeHelpers.handleNonlocalReturn(scope, bj, blockType)
Variable
ret
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
RuntimeHelperCall
(
ret
,
"handleNonlocalReturn"
,
new
Operand
[]{
exc
} ));
s
.
addInstr
(
new
ReturnInstr
(
ret
));
// End
s
.
addInstr
(
new
LabelInstr
(
rEndLabel
));
}
// Wrap call in a rescue handler that catches the IRBreakJump
private
void
receiveBreakException
(
IRScope
s
,
Operand
block
,
CallInstr
callInstr
) {
// Check if we have to handle a break
if
(
block
!=
null
&&
block
instanceof
WrappedIRClosure
) {
IRClosure
closure
= ((
WrappedIRClosure
)
block
).
getClosure
();
if
(!
closure
.
hasBreakInstrs
) {
// No protection needed -- add the call and return
s
.
addInstr
(
callInstr
);
return
;
}
}
else
{
// No protection needed -- add the call and return
s
.
addInstr
((
Instr
)
callInstr
);
return
;
}
Label
rBeginLabel
=
s
.
getNewLabel
();
Label
rEndLabel
=
s
.
getNewLabel
();
Label
rescueLabel
=
s
.
getNewLabel
();
// Protected region
s
.
addInstr
(
new
ExceptionRegionStartMarkerInstr
(
rBeginLabel
,
rEndLabel
,
null
,
rescueLabel
));
s
.
addInstr
(
callInstr
);
s
.
addInstr
(
new
JumpInstr
(
rEndLabel
));
s
.
addInstr
(
new
ExceptionRegionEndMarkerInstr
());
// Receive exceptions (could be anything, but the handler only processes IRBreakJumps)
s
.
addInstr
(
new
LabelInstr
(
rescueLabel
));
Variable
exc
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
ReceiveExceptionInstr
(
exc
));
// Handle break using runtime helper
// --> IRRuntimeHelpers.handlePropagatedBreak(context, scope, bj, blockType)
s
.
addInstr
(
new
RuntimeHelperCall
(
callInstr
.
getResult
(),
"handlePropagatedBreak"
,
new
Operand
[]{
exc
} ));
// End
s
.
addInstr
(
new
LabelInstr
(
rEndLabel
));
}
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
.
getNewTemporaryVariable
();
CallInstr
callInstr
=
CallInstr
.
create
(
callResult
,
new
MethAddr
(
callNode
.
getName
()),
receiver
,
args
.
toArray
(
new
Operand
[
args
.
size
()]),
block
);
receiveBreakException
(
s
,
block
,
callInstr
);
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
.
getNewTemporaryVariable
();
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
=
s
.
getNewLabel
();
Variable
eqqResult
=
s
.
getNewTemporaryVariable
();
labels
.
add
(
bodyLabel
);
Operand
v1
,
v2
;
if
(
whenNode
.
getExpressionNodes
()
instanceof
ListNode
) {
// SSS FIXME: Note about refactoring:
// - BEQInstr has a quick implementation when the second operand is a boolean literal
// If it can be fixed to do this even on the first operand, we can switch around
// v1 and v2 in the UndefinedValue scenario and DRY out this code.
// - Even with this asymmetric implementation of BEQInstr, you might be tempted to
// switch around v1 and v2 in the else case. But, that is equivalent to this Ruby code change:
// (v1 == value) instead of (value == v1)
// It seems that they should be identical, but the first one is v1.==(value) and the second one is
// value.==(v1). This is just fine *if* the Ruby programmer has implemented an algebraically
// symmetric "==" method on those objects. If not, then, the results might be unexpected where the
// code (intentionally or otherwise) relies on this asymmetry of "==". While it could be argued
// that this a Ruby code bug, we will just try to preserve the order of the == check as it appears
// in the Ruby code.
if
(
value
==
UndefinedValue
.
UNDEFINED
) {
v1
=
build
(
whenNode
.
getExpressionNodes
(),
s
);
v2
=
manager
.
getTrue
();
}
else
{
v1
=
value
;
v2
=
build
(
whenNode
.
getExpressionNodes
(),
s
);
}
}
else
{
s
.
addInstr
(
new
EQQInstr
(
eqqResult
,
build
(
whenNode
.
getExpressionNodes
(),
s
),
value
));
v1
=
eqqResult
;
v2
=
manager
.
getTrue
();
}
s
.
addInstr
(
BEQInstr
.
create
(
v1
,
v2
,
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
());
}
// Jump to else in case nothing matches!
s
.
addInstr
(
new
JumpInstr
(
elseLabel
));
// build "else" if it exists
if
(
hasElse
) {
labels
.
add
(
elseLabel
);
bodies
.
put
(
elseLabel
,
caseNode
.
getElseNode
());
}
// now emit bodies while preserving when clauses order
for
(
Label
whenLabel
:
labels
) {
s
.
addInstr
(
new
LabelInstr
(
whenLabel
));
Operand
bodyValue
=
build
(
bodies
.
get
(
whenLabel
),
s
);
// bodyValue can be null if the body ends with a return!
if
(
bodyValue
!=
null
) {
// SSS FIXME: Do 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. For this, you have
// to look at what the last generated instruction was.
Label
tgt
=
endLabel
;
s
.
addInstr
(
new
CopyInstr
(
result
,
bodyValue
));
s
.
addInstr
(
new
JumpInstr
(
tgt
));
}
}
if
(!
hasElse
) {
s
.
addInstr
(
new
LabelInstr
(
elseLabel
));
s
.
addInstr
(
new
CopyInstr
(
result
,
manager
.
getNil
()));
s
.
addInstr
(
new
JumpInstr
(
endLabel
));
}
// close it out
s
.
addInstr
(
new
LabelInstr
(
endLabel
));
// SSS: Got rid of the marker case label instruction
return
result
;
}
/**
* Build a new class and add it to the current scope (s).
*/
public
Operand
buildClass
(
ClassNode
classNode
,
IRScope
s
) {
Node
superNode
=
classNode
.
getSuperNode
();
Colon3Node
cpath
=
classNode
.
getCPath
();
Operand
superClass
= (
superNode
==
null
) ?
null
:
build
(
superNode
,
s
);
String
className
=
cpath
.
getName
();
Operand
container
=
getContainerFromCPath
(
cpath
,
s
);
IRClassBody
c
=
new
IRClassBody
(
manager
,
s
,
className
,
classNode
.
getPosition
().
getLine
(),
classNode
.
getScope
());
Variable
classBody
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
DefineClassInstr
(
classBody
,
c
,
container
,
superClass
));
Variable
ret
=
s
.
getNewTemporaryVariable
();
s
.
addInstr
(
new
ProcessModuleBodyInstr
(
ret
,
classBody
));
c
.
addInstr
(
new
ReceiveSelfInstr
(
c
.
getSelf
()));
// Set %current_scope = <c>
// Set %current_module = module<c>
c
.
addInstr
(
new
CopyInstr
(
c
.
getCurrentScopeVariable
(),
new
CurrentScope
(
c
)));
c
.
addInstr
(
new
CopyInstr
(
c
.
getCurrentModuleVariable
(),
new
ScopeModule
(
c
)));
// Create a new nested builder to ensure this gets its own IR builder state
Operand
rv
=
createIRBuilder
(
manager
,
is1_9
()).
build
(
classNode
.
getBodyNode
(),
c
);
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL