Skip to content

Commit a57a0ae

Browse files
authored
Remove Painless Type from MethodWriter in favor of Java Class. (#28346)
1 parent 090ac3c commit a57a0ae

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public final class MethodWriter extends GeneratorAdapter {
8181
private final BitSet statements;
8282
private final CompilerSettings settings;
8383

84-
private final Deque<List<org.objectweb.asm.Type>> stringConcatArgs =
84+
private final Deque<List<Type>> stringConcatArgs =
8585
(INDY_STRING_CONCAT_BOOTSTRAP_HANDLE == null) ? null : new ArrayDeque<>();
8686

8787
public MethodWriter(int access, Method method, ClassVisitor cw, BitSet statements, CompilerSettings settings) {
@@ -200,7 +200,7 @@ private void writeCast(Class<?> from, Class<?> to) {
200200
* Proxy the box method to use valueOf instead to ensure that the modern boxing methods are used.
201201
*/
202202
@Override
203-
public void box(org.objectweb.asm.Type type) {
203+
public void box(Type type) {
204204
valueOf(type);
205205
}
206206

@@ -252,10 +252,10 @@ public int writeNewStrings() {
252252
}
253253
}
254254

255-
public void writeAppendStrings(final Definition.Type type) {
255+
public void writeAppendStrings(Class<?> clazz) {
256256
if (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE != null) {
257257
// Java 9+: record type information
258-
stringConcatArgs.peek().add(type.type);
258+
stringConcatArgs.peek().add(getType(clazz));
259259
// prevent too many concat args.
260260
// If there are too many, do the actual concat:
261261
if (stringConcatArgs.peek().size() >= MAX_INDY_STRING_CONCAT_ARGS) {
@@ -266,24 +266,24 @@ public void writeAppendStrings(final Definition.Type type) {
266266
}
267267
} else {
268268
// Java 8: push a StringBuilder append
269-
if (type.clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN);
270-
else if (type.clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR);
271-
else if (type.clazz == byte.class ||
272-
type.clazz == short.class ||
273-
type.clazz == int.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_INT);
274-
else if (type.clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG);
275-
else if (type.clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT);
276-
else if (type.clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE);
277-
else if (type.clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING);
278-
else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT);
269+
if (clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN);
270+
else if (clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR);
271+
else if (clazz == byte.class ||
272+
clazz == short.class ||
273+
clazz == int.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_INT);
274+
else if (clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG);
275+
else if (clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT);
276+
else if (clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE);
277+
else if (clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING);
278+
else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT);
279279
}
280280
}
281281

282282
public void writeToStrings() {
283283
if (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE != null) {
284284
// Java 9+: use type information and push invokeDynamic
285-
final String desc = org.objectweb.asm.Type.getMethodDescriptor(STRING_TYPE,
286-
stringConcatArgs.pop().stream().toArray(org.objectweb.asm.Type[]::new));
285+
final String desc = Type.getMethodDescriptor(STRING_TYPE,
286+
stringConcatArgs.pop().stream().toArray(Type[]::new));
287287
invokeDynamic("concat", desc, INDY_STRING_CONCAT_BOOTSTRAP_HANDLE);
288288
} else {
289289
// Java 8: call toString() on StringBuilder
@@ -292,9 +292,9 @@ public void writeToStrings() {
292292
}
293293

294294
/** Writes a dynamic binary instruction: returnType, lhs, and rhs can be different */
295-
public void writeDynamicBinaryInstruction(Location location, Definition.Type returnType, Definition.Type lhs, Definition.Type rhs,
295+
public void writeDynamicBinaryInstruction(Location location, Class<?> returnType, Class<?> lhs, Class<?> rhs,
296296
Operation operation, int flags) {
297-
org.objectweb.asm.Type methodType = org.objectweb.asm.Type.getMethodType(returnType.type, lhs.type, rhs.type);
297+
Type methodType = Type.getMethodType(getType(returnType), getType(lhs), getType(rhs));
298298

299299
switch (operation) {
300300
case MUL:
@@ -310,7 +310,7 @@ public void writeDynamicBinaryInstruction(Location location, Definition.Type ret
310310
// if either side is primitive, then the + operator should always throw NPE on null,
311311
// so we don't need a special NPE guard.
312312
// otherwise, we need to allow nulls for possible string concatenation.
313-
boolean hasPrimitiveArg = lhs.clazz.isPrimitive() || rhs.clazz.isPrimitive();
313+
boolean hasPrimitiveArg = lhs.isPrimitive() || rhs.isPrimitive();
314314
if (!hasPrimitiveArg) {
315315
flags |= DefBootstrap.OPERATOR_ALLOWS_NULL;
316316
}
@@ -343,26 +343,26 @@ public void writeDynamicBinaryInstruction(Location location, Definition.Type ret
343343
}
344344

345345
/** Writes a static binary instruction */
346-
public void writeBinaryInstruction(Location location, Definition.Type type, Operation operation) {
347-
if ((type.clazz == float.class || type.clazz == double.class) &&
346+
public void writeBinaryInstruction(Location location, Class<?> clazz, Operation operation) {
347+
if ( (clazz == float.class || clazz == double.class) &&
348348
(operation == Operation.LSH || operation == Operation.USH ||
349349
operation == Operation.RSH || operation == Operation.BWAND ||
350350
operation == Operation.XOR || operation == Operation.BWOR)) {
351351
throw location.createError(new IllegalStateException("Illegal tree structure."));
352352
}
353353

354354
switch (operation) {
355-
case MUL: math(GeneratorAdapter.MUL, type.type); break;
356-
case DIV: math(GeneratorAdapter.DIV, type.type); break;
357-
case REM: math(GeneratorAdapter.REM, type.type); break;
358-
case ADD: math(GeneratorAdapter.ADD, type.type); break;
359-
case SUB: math(GeneratorAdapter.SUB, type.type); break;
360-
case LSH: math(GeneratorAdapter.SHL, type.type); break;
361-
case USH: math(GeneratorAdapter.USHR, type.type); break;
362-
case RSH: math(GeneratorAdapter.SHR, type.type); break;
363-
case BWAND: math(GeneratorAdapter.AND, type.type); break;
364-
case XOR: math(GeneratorAdapter.XOR, type.type); break;
365-
case BWOR: math(GeneratorAdapter.OR, type.type); break;
355+
case MUL: math(GeneratorAdapter.MUL, getType(clazz)); break;
356+
case DIV: math(GeneratorAdapter.DIV, getType(clazz)); break;
357+
case REM: math(GeneratorAdapter.REM, getType(clazz)); break;
358+
case ADD: math(GeneratorAdapter.ADD, getType(clazz)); break;
359+
case SUB: math(GeneratorAdapter.SUB, getType(clazz)); break;
360+
case LSH: math(GeneratorAdapter.SHL, getType(clazz)); break;
361+
case USH: math(GeneratorAdapter.USHR, getType(clazz)); break;
362+
case RSH: math(GeneratorAdapter.SHR, getType(clazz)); break;
363+
case BWAND: math(GeneratorAdapter.AND, getType(clazz)); break;
364+
case XOR: math(GeneratorAdapter.XOR, getType(clazz)); break;
365+
case BWOR: math(GeneratorAdapter.OR, getType(clazz)); break;
366366
default:
367367
throw location.createError(new IllegalStateException("Illegal tree structure."));
368368
}
@@ -416,7 +416,7 @@ public void visitEnd() {
416416
* @param flavor type of call
417417
* @param params flavor-specific parameters
418418
*/
419-
public void invokeDefCall(String name, org.objectweb.asm.Type methodType, int flavor, Object... params) {
419+
public void invokeDefCall(String name, Type methodType, int flavor, Object... params) {
420420
Object[] args = new Object[params.length + 2];
421421
args[0] = settings.getInitialCallSiteDepth();
422422
args[1] = flavor;

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EAssignment.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.painless.Definition;
2626
import org.elasticsearch.painless.Definition.Cast;
2727
import org.elasticsearch.painless.Definition.Type;
28+
import org.elasticsearch.painless.Definition.def;
2829
import org.elasticsearch.painless.Globals;
2930
import org.elasticsearch.painless.Locals;
3031
import org.elasticsearch.painless.Location;
@@ -274,12 +275,12 @@ void write(MethodWriter writer, Globals globals) {
274275
writer.writeDup(lhs.accessElementCount(), catElementStackSize); // dup the top element and insert it
275276
// before concat helper on stack
276277
lhs.load(writer, globals); // read the current lhs's value
277-
writer.writeAppendStrings(lhs.actual); // append the lhs's value using the StringBuilder
278+
writer.writeAppendStrings(Definition.TypeToClass(lhs.actual)); // append the lhs's value using the StringBuilder
278279

279280
rhs.write(writer, globals); // write the bytecode for the rhs
280281

281-
if (!(rhs instanceof EBinary) || !((EBinary)rhs).cat) { // check to see if the rhs has already done a concatenation
282-
writer.writeAppendStrings(rhs.actual); // append the rhs's value since it's hasn't already
282+
if (!(rhs instanceof EBinary) || !((EBinary)rhs).cat) { // check to see if the rhs has already done a concatenation
283+
writer.writeAppendStrings(Definition.TypeToClass(rhs.actual)); // append the rhs's value since it's hasn't already
283284
}
284285

285286
writer.writeToStrings(); // put the value for string concat onto the stack
@@ -313,9 +314,9 @@ void write(MethodWriter writer, Globals globals) {
313314
// write the operation instruction for compound assignment
314315
if (promote.dynamic) {
315316
writer.writeDynamicBinaryInstruction(
316-
location, promote, DefType, DefType, operation, DefBootstrap.OPERATOR_COMPOUND_ASSIGNMENT);
317+
location, Definition.TypeToClass(promote), def.class, def.class, operation, DefBootstrap.OPERATOR_COMPOUND_ASSIGNMENT);
317318
} else {
318-
writer.writeBinaryInstruction(location, promote, operation);
319+
writer.writeBinaryInstruction(location, Definition.TypeToClass(promote), operation);
319320
}
320321

321322
writer.writeCast(back); // if necessary cast the promotion type value back to the lhs's type

modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EBinary.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,13 @@ void write(MethodWriter writer, Globals globals) {
649649
left.write(writer, globals);
650650

651651
if (!(left instanceof EBinary) || !((EBinary)left).cat) {
652-
writer.writeAppendStrings(left.actual);
652+
writer.writeAppendStrings(Definition.TypeToClass(left.actual));
653653
}
654654

655655
right.write(writer, globals);
656656

657657
if (!(right instanceof EBinary) || !((EBinary)right).cat) {
658-
writer.writeAppendStrings(right.actual);
658+
writer.writeAppendStrings(Definition.TypeToClass(right.actual));
659659
}
660660

661661
if (!cat) {
@@ -684,9 +684,10 @@ void write(MethodWriter writer, Globals globals) {
684684
if (originallyExplicit) {
685685
flags |= DefBootstrap.OPERATOR_EXPLICIT_CAST;
686686
}
687-
writer.writeDynamicBinaryInstruction(location, actual, left.actual, right.actual, operation, flags);
687+
writer.writeDynamicBinaryInstruction(location, Definition.TypeToClass(actual),
688+
Definition.TypeToClass(left.actual), Definition.TypeToClass(right.actual), operation, flags);
688689
} else {
689-
writer.writeBinaryInstruction(location, actual, operation);
690+
writer.writeBinaryInstruction(location, Definition.TypeToClass(actual), operation);
690691
}
691692
}
692693
}

0 commit comments

Comments
 (0)