Skip to content

Commit

Permalink
Remove Painless Type from MethodWriter in favor of Java Class. (#28346)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdconrad committed Jan 24, 2018
1 parent c7e69ca commit 0522218
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public final class MethodWriter extends GeneratorAdapter {
private final BitSet statements;
private final CompilerSettings settings;

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

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

Expand Down Expand Up @@ -252,10 +252,10 @@ public int writeNewStrings() {
}
}

public void writeAppendStrings(final Definition.Type type) {
public void writeAppendStrings(Class<?> clazz) {
if (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE != null) {
// Java 9+: record type information
stringConcatArgs.peek().add(type.type);
stringConcatArgs.peek().add(getType(clazz));
// prevent too many concat args.
// If there are too many, do the actual concat:
if (stringConcatArgs.peek().size() >= MAX_INDY_STRING_CONCAT_ARGS) {
Expand All @@ -266,24 +266,24 @@ public void writeAppendStrings(final Definition.Type type) {
}
} else {
// Java 8: push a StringBuilder append
if (type.clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN);
else if (type.clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR);
else if (type.clazz == byte.class ||
type.clazz == short.class ||
type.clazz == int.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_INT);
else if (type.clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG);
else if (type.clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT);
else if (type.clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE);
else if (type.clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING);
else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT);
if (clazz == boolean.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_BOOLEAN);
else if (clazz == char.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_CHAR);
else if (clazz == byte.class ||
clazz == short.class ||
clazz == int.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_INT);
else if (clazz == long.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_LONG);
else if (clazz == float.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_FLOAT);
else if (clazz == double.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_DOUBLE);
else if (clazz == String.class) invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_STRING);
else invokeVirtual(STRINGBUILDER_TYPE, STRINGBUILDER_APPEND_OBJECT);
}
}

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

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

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

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

switch (operation) {
case MUL: math(GeneratorAdapter.MUL, type.type); break;
case DIV: math(GeneratorAdapter.DIV, type.type); break;
case REM: math(GeneratorAdapter.REM, type.type); break;
case ADD: math(GeneratorAdapter.ADD, type.type); break;
case SUB: math(GeneratorAdapter.SUB, type.type); break;
case LSH: math(GeneratorAdapter.SHL, type.type); break;
case USH: math(GeneratorAdapter.USHR, type.type); break;
case RSH: math(GeneratorAdapter.SHR, type.type); break;
case BWAND: math(GeneratorAdapter.AND, type.type); break;
case XOR: math(GeneratorAdapter.XOR, type.type); break;
case BWOR: math(GeneratorAdapter.OR, type.type); break;
case MUL: math(GeneratorAdapter.MUL, getType(clazz)); break;
case DIV: math(GeneratorAdapter.DIV, getType(clazz)); break;
case REM: math(GeneratorAdapter.REM, getType(clazz)); break;
case ADD: math(GeneratorAdapter.ADD, getType(clazz)); break;
case SUB: math(GeneratorAdapter.SUB, getType(clazz)); break;
case LSH: math(GeneratorAdapter.SHL, getType(clazz)); break;
case USH: math(GeneratorAdapter.USHR, getType(clazz)); break;
case RSH: math(GeneratorAdapter.SHR, getType(clazz)); break;
case BWAND: math(GeneratorAdapter.AND, getType(clazz)); break;
case XOR: math(GeneratorAdapter.XOR, getType(clazz)); break;
case BWOR: math(GeneratorAdapter.OR, getType(clazz)); break;
default:
throw location.createError(new IllegalStateException("Illegal tree structure."));
}
Expand Down Expand Up @@ -416,7 +416,7 @@ public void visitEnd() {
* @param flavor type of call
* @param params flavor-specific parameters
*/
public void invokeDefCall(String name, org.objectweb.asm.Type methodType, int flavor, Object... params) {
public void invokeDefCall(String name, Type methodType, int flavor, Object... params) {
Object[] args = new Object[params.length + 2];
args[0] = settings.getInitialCallSiteDepth();
args[1] = flavor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.painless.Definition;
import org.elasticsearch.painless.Definition.Cast;
import org.elasticsearch.painless.Definition.Type;
import org.elasticsearch.painless.Definition.def;
import org.elasticsearch.painless.Globals;
import org.elasticsearch.painless.Locals;
import org.elasticsearch.painless.Location;
Expand Down Expand Up @@ -274,12 +275,12 @@ void write(MethodWriter writer, Globals globals) {
writer.writeDup(lhs.accessElementCount(), catElementStackSize); // dup the top element and insert it
// before concat helper on stack
lhs.load(writer, globals); // read the current lhs's value
writer.writeAppendStrings(lhs.actual); // append the lhs's value using the StringBuilder
writer.writeAppendStrings(Definition.TypeToClass(lhs.actual)); // append the lhs's value using the StringBuilder

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

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

writer.writeToStrings(); // put the value for string concat onto the stack
Expand Down Expand Up @@ -313,9 +314,9 @@ void write(MethodWriter writer, Globals globals) {
// write the operation instruction for compound assignment
if (promote.dynamic) {
writer.writeDynamicBinaryInstruction(
location, promote, DefType, DefType, operation, DefBootstrap.OPERATOR_COMPOUND_ASSIGNMENT);
location, Definition.TypeToClass(promote), def.class, def.class, operation, DefBootstrap.OPERATOR_COMPOUND_ASSIGNMENT);
} else {
writer.writeBinaryInstruction(location, promote, operation);
writer.writeBinaryInstruction(location, Definition.TypeToClass(promote), operation);
}

writer.writeCast(back); // if necessary cast the promotion type value back to the lhs's type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -649,13 +649,13 @@ void write(MethodWriter writer, Globals globals) {
left.write(writer, globals);

if (!(left instanceof EBinary) || !((EBinary)left).cat) {
writer.writeAppendStrings(left.actual);
writer.writeAppendStrings(Definition.TypeToClass(left.actual));
}

right.write(writer, globals);

if (!(right instanceof EBinary) || !((EBinary)right).cat) {
writer.writeAppendStrings(right.actual);
writer.writeAppendStrings(Definition.TypeToClass(right.actual));
}

if (!cat) {
Expand Down Expand Up @@ -684,9 +684,10 @@ void write(MethodWriter writer, Globals globals) {
if (originallyExplicit) {
flags |= DefBootstrap.OPERATOR_EXPLICIT_CAST;
}
writer.writeDynamicBinaryInstruction(location, actual, left.actual, right.actual, operation, flags);
writer.writeDynamicBinaryInstruction(location, Definition.TypeToClass(actual),
Definition.TypeToClass(left.actual), Definition.TypeToClass(right.actual), operation, flags);
} else {
writer.writeBinaryInstruction(location, actual, operation);
writer.writeBinaryInstruction(location, Definition.TypeToClass(actual), operation);
}
}
}
Expand Down

0 comments on commit 0522218

Please sign in to comment.