/
InvokeDynamicCacheCompiler.java
502 lines (441 loc) · 17.8 KB
/
InvokeDynamicCacheCompiler.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
/*
***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.compiler.impl;
import java.math.BigInteger;
import org.jcodings.Encoding;
import org.jruby.RubyBoolean;
import org.jruby.RubyEncoding;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.ast.NodeType;
import org.jruby.ast.executable.AbstractScript;
import org.jruby.compiler.ASTInspector;
import org.jruby.compiler.CompilerCallback;
import org.jruby.runtime.Helpers;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.BlockBody;
import org.jruby.runtime.CallSite;
import org.jruby.runtime.CallType;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.invokedynamic.InvokeDynamicSupport;
import org.jruby.util.ByteList;
import static org.jruby.util.CodegenUtils.*;
import org.jruby.util.JavaNameMangler;
import org.jruby.util.cli.Options;
/**
* A CacheCompiler that uses invokedynamic as a lazy thunk for literals and other
* invokedynamic features like SwitchPoint to produce fast (nearly free)
* invalidatable caches for things like constant lookup.
*/
public class InvokeDynamicCacheCompiler extends InheritedCacheCompiler {
public InvokeDynamicCacheCompiler(StandardASMCompiler scriptCompiler) {
super(scriptCompiler);
}
/**
* Cache a constant reference using invokedynamic.
*
* This cache uses a java.lang.invoke.SwitchPoint as the invalidation
* mechanism in order to avoid the cost of constantly pinging a constant
* generation in org.jruby.Ruby. This allows a nearly free constant cache.
*
* @param method the method compiler with which bytecode is emitted
* @param constantName the name of the constant to look up
*/
@Override
public void cacheConstant(BaseBodyCompiler method, String constantName) {
if (!Options.INVOKEDYNAMIC_CACHE_CONSTANTS.load()) {
super.cacheConstant(method, constantName);
return;
}
method.loadThis();
method.loadThreadContext();
method.method.invokedynamic(
constantName,
sig(IRubyObject.class, AbstractScript.class, ThreadContext.class),
InvokeDynamicSupport.getConstantHandle(),
method.getScopeIndex());
}
/**
* Cache a constant boolean using invokedynamic.
*
* This cache uses a java.lang.invoke.SwitchPoint as the invalidation
* mechanism in order to avoid the cost of constantly pinging a constant
* generation in org.jruby.Ruby. This allows a nearly free constant cache.
*
* @param method the method compiler with which bytecode is emitted
* @param constantName the name of the constant to look up
*/
@Override
public void cacheConstantBoolean(BaseBodyCompiler method, String constantName) {
if (!Options.INVOKEDYNAMIC_CACHE_CONSTANTS.load()) {
super.cacheConstantBoolean(method, constantName);
return;
}
method.loadThis();
method.loadThreadContext();
method.method.invokedynamic(
constantName,
sig(boolean.class, AbstractScript.class, ThreadContext.class),
InvokeDynamicSupport.getConstantBooleanHandle(),
method.getScopeIndex());
}
/**
* This doesn't get used, since it's only used from cacheRegexp in superclass,
* and that is completely bound via invokedynamic now. Implemented here and in
* InvokeDynamicSupport for consistency.
*
* @param method the method compiler with which bytecode is emitted
* @param contents the contents of the bytelist to cache
*/
@Override
public void cacheByteList(BaseBodyCompiler method, ByteList contents) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheByteList(method, contents);
return;
}
String asString = Helpers.rawBytesToString(contents.bytes());
String encodingName = new String(contents.getEncoding().getName());
method.method.invokedynamic(
"getByteList",
sig(ByteList.class),
InvokeDynamicSupport.getByteListHandle(),
asString,
encodingName);
}
/**
* Cache the __ENCODING__ keyword using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param encoding the encoding for this script
*/
@Override
public void cacheRubyEncoding(BaseBodyCompiler method, Encoding encoding) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheRubyEncoding(method, encoding);
return;
}
String encodingName = new String(encoding.getName());
method.loadThreadContext();
method.method.invokedynamic(
"getEncoding",
sig(RubyEncoding.class, ThreadContext.class),
InvokeDynamicSupport.getEncodingHandle(),
encodingName);
}
/**
* Cache a closure body (BlockBody) using invokedynamic.
*/
@Override
public int cacheClosure(BaseBodyCompiler method, String closureMethod, int arity, StaticScope scope, String file, int line, boolean hasMultipleArgsHead, NodeType argsNodeId, ASTInspector inspector) {
String descriptor = Helpers.buildBlockDescriptor(
closureMethod,
arity,
file,
line,
hasMultipleArgsHead,
argsNodeId,
inspector);
method.loadThis();
method.loadThreadContext();
int scopeIndex = cacheStaticScope(method, scope);
method.method.invokedynamic(
"getBlockBody",
sig(BlockBody.class, Object.class, ThreadContext.class, StaticScope.class),
InvokeDynamicSupport.getBlockBodyHandle(),
descriptor);
return scopeIndex;
}
/**
* Cache a closure body (BlockBody) for 1.9 mode using invokedynamic.
*/
@Override
public int cacheClosure19(BaseBodyCompiler method, String closureMethod, int arity, StaticScope scope, String file, int line, boolean hasMultipleArgsHead, NodeType argsNodeId, String parameterList, ASTInspector inspector) {
String descriptor = Helpers.buildBlockDescriptor19(
closureMethod,
arity,
file,
line,
hasMultipleArgsHead,
argsNodeId,
parameterList,
inspector);
method.loadThis();
method.loadThreadContext();
int scopeIndex = cacheStaticScope(method, scope);
method.method.invokedynamic(
"getBlockBody19",
sig(BlockBody.class, Object.class, ThreadContext.class, StaticScope.class),
InvokeDynamicSupport.getBlockBody19Handle(),
descriptor);
return scopeIndex;
}
/**
* Cache a Regexp literal using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param pattern the contents of the bytelist for the regexp pattern
* @param options the regexp options
*/
@Override
public void cacheRegexp(BaseBodyCompiler method, ByteList pattern, int options) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheRegexp(method, pattern, options);
return;
}
String asString = Helpers.rawBytesToString(pattern.bytes());
String encodingName = new String(pattern.getEncoding().getName());
method.loadThreadContext();
method.method.invokedynamic(
"getRegexp",
sig(RubyRegexp.class, ThreadContext.class),
InvokeDynamicSupport.getRegexpHandle(),
asString,
encodingName,
options);
}
/**
* Cache a Fixnum literal using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param value the value of the Fixnum
*/
@Override
public void cacheFixnum(BaseBodyCompiler method, long value) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheFixnum(method, value);
return;
}
method.loadThreadContext();
method.method.invokedynamic(
"getFixnum",
sig(RubyFixnum.class, ThreadContext.class),
InvokeDynamicSupport.getFixnumHandle(),
value);
}
/**
* Cache a Float literal using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param value the value of the Float
*/
@Override
public void cacheFloat(BaseBodyCompiler method, double value) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheFloat(method, value);
return;
}
method.loadThreadContext();
method.method.invokedynamic(
"getFloat",
sig(RubyFloat.class, ThreadContext.class),
InvokeDynamicSupport.getFloatHandle(),
value);
}
/**
* Cache a StaticScope using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param scope the original scope to base the new one on
*/
@Override
public int cacheStaticScope(BaseBodyCompiler method, StaticScope scope) {
String scopeString = Helpers.encodeScope(scope);
int index = scopeCount;
scopeCount++;
method.loadThis();
method.loadThreadContext();
method.loadStaticScope();
method.method.invokedynamic(
"getStaticScope",
sig(StaticScope.class, AbstractScript.class, ThreadContext.class, StaticScope.class),
InvokeDynamicSupport.getStaticScopeHandle(),
scopeString,
index);
return index;
}
public void loadStaticScope(BaseBodyCompiler method, int index) {
method.loadThis();
method.method.invokedynamic(
"getStaticScope",
sig(StaticScope.class, AbstractScript.class),
InvokeDynamicSupport.getLoadStaticScopeHandle(),
index);
}
/**
* Cache a CallSite object using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param name the method name the call site invokes, or null for "super"
* @param callType the type of call
*/
@Override
public void cacheCallSite(BaseBodyCompiler method, String name, CallType callType) {
char callTypeChar = 0;
switch (callType) {
case NORMAL:
callTypeChar = 'N';
break;
case FUNCTIONAL:
callTypeChar = 'F';
break;
case VARIABLE:
callTypeChar = 'V';
break;
case SUPER:
callTypeChar = 'S';
break;
}
if (name == null) name = "super";
method.method.invokedynamic(
"getCallSite",
sig(CallSite.class),
InvokeDynamicSupport.getCallSiteHandle(),
name,
callTypeChar);
}
/**
* Cache a String literal using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param contents the contents of the bytelist for the String
* @param codeRange the code range for the String
*/
@Override
public void cacheString(BaseBodyCompiler method, ByteList contents, int codeRange) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheString(method, contents, codeRange);
return;
}
String asString = Helpers.rawBytesToString(contents.bytes());
String encodingName = new String(contents.getEncoding().getName());
method.loadThreadContext();
method.method.invokedynamic(
"getString",
sig(RubyString.class, ThreadContext.class),
InvokeDynamicSupport.getStringHandle(),
asString,
encodingName,
codeRange);
}
/**
* Cache a BigInteger using invokedynamic. Used for Bignum construction
*
* @param method the method compiler with which bytecode is emitted
* @param bigint the BigInteger to cache
*/
@Override
public void cacheBigInteger(BaseBodyCompiler method, BigInteger bigint) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheBigInteger(method, bigint);
return;
}
String asString = bigint.toString(16);
method.method.invokedynamic(
"getBigInteger",
sig(BigInteger.class),
InvokeDynamicSupport.getBigIntegerHandle(),
asString);
}
/**
* Cache the symbol for the given string using invokedynamic.
*
* @param method the method compiler with which bytecode is emitted
* @param symbol the string of the Symbol to cache
*/
public void cacheSymbol(BaseBodyCompiler method, String symbol, Encoding encoding) {
if (!Options.INVOKEDYNAMIC_CACHE_LITERALS.load()) {
super.cacheSymbol(method, symbol, encoding);
return;
}
method.loadThreadContext();
method.method.invokedynamic(
"getSymbol",
sig(RubySymbol.class, ThreadContext.class),
InvokeDynamicSupport.getSymbolHandle(),
symbol,
encoding.toString());
}
public void cachedGetVariable(BaseBodyCompiler method, String name) {
if (!Options.INVOKEDYNAMIC_CACHE_IVARS.load()) {
super.cachedGetVariable(method, name);
return;
}
method.loadSelf();
method.method.invokedynamic(
"get:" + name,
sig(IRubyObject.class, IRubyObject.class),
InvokeDynamicSupport.getVariableHandle(),
method.getScriptCompiler().getSourcename(),
method.getLastLine() + 1
);
}
public void cachedSetVariable(BaseBodyCompiler method, String name, CompilerCallback valueCallback) {
if (!Options.INVOKEDYNAMIC_CACHE_IVARS.load()) {
super.cachedSetVariable(method, name, valueCallback);
return;
}
method.loadSelf();
valueCallback.call(method);
method.method.invokedynamic(
"set:" + name,
sig(IRubyObject.class, IRubyObject.class, IRubyObject.class),
InvokeDynamicSupport.getVariableHandle(),
method.getScriptCompiler().getSourcename(),
method.getLastLine() + 1
);
}
public void cacheGlobal(BaseBodyCompiler method, String globalName) {
method.loadThreadContext();
method.method.invokedynamic(
"get:" + JavaNameMangler.mangleMethodName(globalName),
sig(IRubyObject.class, ThreadContext.class),
InvokeDynamicSupport.getGlobalHandle(),
method.getScriptCompiler().getSourcename(),
method.getLastLine() + 1);
}
public void cacheGlobalBoolean(BaseBodyCompiler method, String globalName) {
method.loadThreadContext();
method.method.invokedynamic(
"getBoolean:" + JavaNameMangler.mangleMethodName(globalName),
sig(boolean.class, ThreadContext.class),
InvokeDynamicSupport.getGlobalBooleanHandle(),
method.getScriptCompiler().getSourcename(),
method.getLastLine() + 1);
}
public void cacheBoolean(BaseBodyCompiler method, boolean tru) {
method.loadThreadContext();
method.method.invokedynamic(
"loadBoolean:" + tru,
sig(RubyBoolean.class, ThreadContext.class),
InvokeDynamicSupport.getLoadBooleanHandle());
}
}