FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/src/org/jruby/ir/IRClosure.java at parallel_boot · MSNexploder/jruby · GitHub
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
/
IRClosure.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
314 lines (260 loc) · 12 KB
Breadcrumbs
jruby
/
src
/
org
/
jruby
/
ir
/
IRClosure.java
Copy path
File metadata and controls
314 lines (260 loc) · 12 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
package
org
.
jruby
.
ir
;
import
java
.
util
.
List
;
import
java
.
util
.
ArrayList
;
// Closures are contexts/scopes for the purpose of IR building. They are self-contained and accumulate instructions
// that don't merge into the flow of the containing scope. They are manipulated as an unit.
// Their parents are always execution scopes.
import
org
.
jruby
.
ir
.
operands
.
Label
;
import
org
.
jruby
.
ir
.
operands
.
Operand
;
import
org
.
jruby
.
ir
.
operands
.
Splat
;
import
org
.
jruby
.
ir
.
operands
.
ClosureLocalVariable
;
import
org
.
jruby
.
ir
.
operands
.
LocalVariable
;
import
org
.
jruby
.
ir
.
operands
.
TemporaryVariable
;
import
org
.
jruby
.
ir
.
operands
.
TemporaryClosureVariable
;
import
org
.
jruby
.
ir
.
operands
.
Variable
;
import
org
.
jruby
.
ir
.
instructions
.
Instr
;
import
org
.
jruby
.
ir
.
instructions
.
ReceiveArgBase
;
import
org
.
jruby
.
ir
.
instructions
.
ReceiveExceptionInstr
;
import
org
.
jruby
.
ir
.
instructions
.
ReceiveRestArgBase
;
import
org
.
jruby
.
ir
.
instructions
.
RuntimeHelperCall
;
import
org
.
jruby
.
ir
.
representations
.
BasicBlock
;
import
org
.
jruby
.
ir
.
representations
.
CFG
;
import
org
.
jruby
.
ir
.
transformations
.
inlining
.
InlinerInfo
;
import
org
.
jruby
.
parser
.
StaticScope
;
import
org
.
jruby
.
parser
.
IRStaticScope
;
import
org
.
jruby
.
runtime
.
Arity
;
import
org
.
jruby
.
runtime
.
BlockBody
;
import
org
.
jruby
.
runtime
.
InterpretedIRBlockBody
;
import
org
.
jruby
.
runtime
.
InterpretedIRBlockBody19
;
public
class
IRClosure
extends
IRScope
{
public
final
Label
startLabel
;
// Label for the start of the closure (used to implement redo)
public
final
Label
endLabel
;
// Label for the end of the closure (used to implement retry)
public
final
int
closureId
;
// Unique id for this closure within the nearest ancestor method.
private
int
nestingDepth
;
// How many nesting levels within a method is this closure nested in?
private
BlockBody
body
;
// for-loop body closures are special in that they dont really define a new variable scope.
// They just silently reuse the parent scope. This changes how variables are allocated (see IRMethod.java).
private
boolean
isForLoopBody
;
// Block parameters
private
List
<
Operand
>
blockArgs
;
/** The parameter names, for Proc#parameters */
private
String
[]
parameterList
;
public
boolean
addedGEBForUncaughtBreaks
;
/** Used by cloning code */
private
IRClosure
(
IRClosure
c
,
IRScope
lexicalParent
) {
super
(
c
,
lexicalParent
);
this
.
closureId
=
lexicalParent
.
getNextClosureId
();
setName
(
"_CLOSURE_CLONE_"
+
closureId
);
this
.
startLabel
=
getNewLabel
(
getName
() +
"_START"
);
this
.
endLabel
=
getNewLabel
(
getName
() +
"_END"
);
this
.
body
= (
c
.
body
instanceof
InterpretedIRBlockBody19
) ?
new
InterpretedIRBlockBody19
(
this
,
c
.
body
.
arity
(),
c
.
body
.
getArgumentType
())
:
new
InterpretedIRBlockBody
(
this
,
c
.
body
.
arity
(),
c
.
body
.
getArgumentType
());
this
.
addedGEBForUncaughtBreaks
=
false
;
}
public
IRClosure
(
IRManager
manager
,
IRScope
lexicalParent
,
boolean
isForLoopBody
,
int
lineNumber
,
StaticScope
staticScope
,
Arity
arity
,
int
argumentType
,
boolean
is1_9
) {
this
(
manager
,
lexicalParent
,
lexicalParent
.
getFileName
(),
lineNumber
,
staticScope
,
isForLoopBody
?
"_FOR_LOOP_"
:
"_CLOSURE_"
);
this
.
isForLoopBody
=
isForLoopBody
;
this
.
blockArgs
=
new
ArrayList
<
Operand
>();
if
(
getManager
().
isDryRun
()) {
this
.
body
=
null
;
}
else
{
this
.
body
=
is1_9
?
new
InterpretedIRBlockBody19
(
this
,
arity
,
argumentType
)
:
new
InterpretedIRBlockBody
(
this
,
arity
,
argumentType
);
if
((
staticScope
!=
null
) && !
isForLoopBody
) ((
IRStaticScope
)
staticScope
).
setIRScope
(
this
);
}
// set nesting depth -- after isForLoopBody value is set
int
n
=
0
;
IRScope
s
=
this
;
while
(
s
instanceof
IRClosure
) {
if
(!
s
.
isForLoopBody
())
n
++;
s
=
s
.
getLexicalParent
();
}
this
.
nestingDepth
=
n
;
}
// Used by IREvalScript
protected
IRClosure
(
IRManager
manager
,
IRScope
lexicalParent
,
String
fileName
,
int
lineNumber
,
StaticScope
staticScope
,
String
prefix
) {
super
(
manager
,
lexicalParent
,
null
,
fileName
,
lineNumber
,
staticScope
);
this
.
isForLoopBody
=
false
;
this
.
startLabel
=
getNewLabel
(
prefix
+
"START"
);
this
.
endLabel
=
getNewLabel
(
prefix
+
"END"
);
this
.
closureId
=
lexicalParent
.
getNextClosureId
();
setName
(
prefix
+
closureId
);
this
.
body
=
null
;
this
.
parameterList
=
new
String
[] {};
// set nesting depth
int
n
=
0
;
IRScope
s
=
this
;
while
(
s
instanceof
IRClosure
) {
if
(!
s
.
isForLoopBody
())
n
++;
s
=
s
.
getLexicalParent
();
}
this
.
nestingDepth
=
n
;
}
public
void
setParameterList
(
String
[]
parameterList
) {
this
.
parameterList
=
parameterList
;
}
public
String
[]
getParameterList
() {
return
this
.
parameterList
;
}
@
Override
public
int
getNextClosureId
() {
return
getLexicalParent
().
getNextClosureId
();
}
@
Override
public
LocalVariable
getNewFlipStateVariable
() {
throw
new
RuntimeException
(
"Cannot get flip variables from closures."
);
}
@
Override
public
TemporaryVariable
getNewTemporaryVariable
() {
temporaryVariableIndex
++;
return
new
TemporaryClosureVariable
(
closureId
,
temporaryVariableIndex
);
}
public
TemporaryVariable
getNewTemporaryVariable
(
String
name
) {
temporaryVariableIndex
++;
return
new
TemporaryClosureVariable
(
name
,
temporaryVariableIndex
);
}
@
Override
public
Label
getNewLabel
() {
return
getNewLabel
(
"CL"
+
closureId
+
"_LBL"
);
}
public
String
getScopeName
() {
return
"Closure"
;
}
@
Override
public
boolean
isForLoopBody
() {
return
isForLoopBody
;
}
@
Override
public
boolean
isTopLocalVariableScope
() {
return
false
;
}
@
Override
public
boolean
isFlipScope
() {
return
false
;
}
@
Override
public
void
addInstr
(
Instr
i
) {
// Accumulate block arguments
if
(
i
instanceof
ReceiveRestArgBase
)
blockArgs
.
add
(
new
Splat
(((
ReceiveRestArgBase
)
i
).
getResult
()));
else
if
(
i
instanceof
ReceiveArgBase
)
blockArgs
.
add
(((
ReceiveArgBase
)
i
).
getResult
());
super
.
addInstr
(
i
);
}
public
Operand
[]
getBlockArgs
() {
return
blockArgs
.
toArray
(
new
Operand
[
blockArgs
.
size
()]);
}
public
String
toStringBody
() {
StringBuilder
buf
=
new
StringBuilder
();
buf
.
append
(
getName
()).
append
(
" = {
\n
"
);
CFG
c
=
getCFG
();
if
(
c
!=
null
) {
buf
.
append
(
"
\n
CFG:
\n
"
).
append
(
c
.
toStringGraph
()).
append
(
"
\n
Instructions:
\n
"
).
append
(
c
.
toStringInstrs
());
}
else
{
buf
.
append
(
toStringInstrs
());
}
buf
.
append
(
"
\n
}
\n
\n
"
);
return
buf
.
toString
();
}
public
BlockBody
getBlockBody
() {
return
body
;
}
@
Override
public
LocalVariable
findExistingLocalVariable
(
String
name
,
int
scopeDepth
) {
LocalVariable
lvar
=
localVars
.
getVariable
(
name
);
if
(
lvar
!=
null
)
return
lvar
;
int
newDepth
=
isForLoopBody
?
scopeDepth
:
scopeDepth
-
1
;
return
newDepth
>=
0
?
getLexicalParent
().
findExistingLocalVariable
(
name
,
newDepth
) :
null
;
}
public
LocalVariable
getNewLocalVariable
(
String
name
,
int
depth
) {
if
(
isForLoopBody
)
return
getLexicalParent
().
getNewLocalVariable
(
name
,
depth
);
if
(
depth
==
0
) {
LocalVariable
lvar
=
new
ClosureLocalVariable
(
this
,
name
,
0
,
localVars
.
nextSlot
);
localVars
.
putVariable
(
name
,
lvar
);
return
lvar
;
}
else
{
return
getLexicalParent
().
getNewLocalVariable
(
name
,
depth
-
1
);
}
}
@
Override
public
LocalVariable
getLocalVariable
(
String
name
,
int
scopeDepth
) {
if
(
isForLoopBody
)
return
getLexicalParent
().
getLocalVariable
(
name
,
scopeDepth
);
LocalVariable
lvar
=
findExistingLocalVariable
(
name
,
scopeDepth
);
if
(
lvar
==
null
)
lvar
=
getNewLocalVariable
(
name
,
scopeDepth
);
// Create a copy of the variable usable at the right depth
if
(
lvar
.
getScopeDepth
() !=
scopeDepth
)
lvar
=
lvar
.
cloneForDepth
(
scopeDepth
);
return
lvar
;
}
public
int
getNestingDepth
() {
return
nestingDepth
;
}
public
LocalVariable
getImplicitBlockArg
() {
// SSS: FIXME: Ugly! We cannot use 'getLocalVariable(Variable.BLOCK, getNestingDepth())' because
// of scenario 3. below. Can we clean up this code?
//
// 1. If the variable has previously been defined, return a copy usable at the closure's nesting depth.
// 2. If not, and if the closure is ultimately nested within a method, build a local variable that will
// be defined in that method.
// 3. If not, and if the closure is not nested within a method, the closure can never receive a block.
// So, we could return 'null', but it creates problems for IR generation. So, for this scenario,
// we simply create a dummy var at depth 0 (meaning, it is local to the closure itself) and return it.
LocalVariable
blockVar
=
findExistingLocalVariable
(
Variable
.
BLOCK
,
getNestingDepth
());
if
(
blockVar
!=
null
) {
// Create a copy of the variable usable at the right depth
if
(
blockVar
.
getScopeDepth
() !=
getNestingDepth
())
blockVar
=
blockVar
.
cloneForDepth
(
getNestingDepth
());
}
else
{
IRScope
s
=
this
;
while
(
s
instanceof
IRClosure
)
s
=
s
.
getLexicalParent
();
if
(
s
instanceof
IRMethod
) {
blockVar
=
s
.
getNewLocalVariable
(
Variable
.
BLOCK
,
0
);
// Create a copy of the variable usable at the right depth
if
(
getNestingDepth
() !=
0
)
blockVar
=
blockVar
.
cloneForDepth
(
getNestingDepth
());
}
else
{
// Dummy var
blockVar
=
getNewLocalVariable
(
Variable
.
BLOCK
,
0
);
}
}
return
blockVar
;
}
public
IRClosure
cloneForClonedInstr
(
InlinerInfo
ii
) {
IRClosure
clonedClosure
=
new
IRClosure
(
this
,
ii
.
getNewLexicalParentForClosure
());
clonedClosure
.
isForLoopBody
=
this
.
isForLoopBody
;
clonedClosure
.
nestingDepth
=
this
.
nestingDepth
;
clonedClosure
.
parameterList
=
this
.
parameterList
;
// Create a new inliner info object
ii
=
ii
.
cloneForCloningClosure
(
clonedClosure
);
// clone the cfg, and all instructions
clonedClosure
.
setCFG
(
getCFG
().
cloneForCloningClosure
(
clonedClosure
,
ii
));
return
clonedClosure
;
}
// Add a global-ensure-block to catch uncaught breaks
// This is usually required only if this closure is being
// used as a lambda, but it is safe to add this for any closure
protected
boolean
addGEBForUncaughtBreaks
() {
// Nothing to do if already done
if
(
addedGEBForUncaughtBreaks
) {
return
false
;
}
CFG
cfg
=
cfg
();
BasicBlock
geb
=
cfg
.
getGlobalEnsureBB
();
if
(
geb
==
null
) {
geb
=
new
BasicBlock
(
cfg
,
new
Label
(
"_GLOBAL_ENSURE_BLOCK"
));
Variable
exc
=
getNewTemporaryVariable
();
geb
.
addInstr
(
new
ReceiveExceptionInstr
(
exc
,
false
));
// No need to check type since it is not used before rethrowing
// Handle uncaught break using runtime helper
// --> IRRuntimeHelpers.catchUncaughtBreakInLambdas(context, scope, bj, blockType)
geb
.
addInstr
(
new
RuntimeHelperCall
(
null
,
"catchUncaughtBreakInLambdas"
,
new
Operand
[]{
exc
} ));
cfg
.
addGlobalEnsureBB
(
geb
);
}
else
{
// SSS FIXME: Assumptions:
//
// First instr is a 'ReceiveExceptionInstr'
// Last instr is a 'ThrowExceptionInstr'
List
<
Instr
>
instrs
=
geb
.
getInstrs
();
Variable
exc
= ((
ReceiveExceptionInstr
)
instrs
.
get
(
0
)).
getResult
();
instrs
.
set
(
instrs
.
size
(),
new
RuntimeHelperCall
(
null
,
"catchUncaughtBreakInLambdas"
,
new
Operand
[]{
exc
} ));
}
// Update scope
addedGEBForUncaughtBreaks
=
true
;
return
true
;
}
}
Back
|
FazBrowse Home
|
New Git URL