Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#162 Replace enumValues with Intrinsic #168

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ public JSCompileResult generateCodeFor(final CompileOptions aOptions, final Byte
theWriter.text("};").newLine();

final String theGetNameMethodName = theMinifier.toMethodName("getName", new BytecodeMethodSignature(BytecodeObjectTypeRef.fromRuntimeClass(String.class), new BytecodeTypeRef[0]));
final String theGetEnumConstantsMethodName = theMinifier.toMethodName("getEnumConstants", new BytecodeMethodSignature(new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(Object.class),1), new BytecodeTypeRef[0]));

final ConstantPool thePool = new ConstantPool();

Expand Down Expand Up @@ -390,10 +389,6 @@ public JSCompileResult generateCodeFor(final CompileOptions aOptions, final Byte
theWriter.tab(2).text("return _tr.").text(theGetNameMethodName).text("();").newLine();
}
theWriter.tab().text("},").newLine();

theWriter.tab().text(theGetEnumConstantsMethodName).colon().text("function(aClazz)").space().text("{").newLine();
theWriter.tab(2).text("return aClazz.").symbol("$VALUES", null).text(";").newLine();
theWriter.tab().text("},").newLine();
}

theMethods.stream().forEach(aEntry -> {
Expand Down Expand Up @@ -549,7 +544,8 @@ public JSCompileResult generateCodeFor(final CompileOptions aOptions, final Byte
!theMethod.isConstructor() &&
!theMethod.isClassInitializer() &&
!"desiredAssertionStatus".equals(theMethod.getName().stringValue()) &&
!"getClass".equals(theMethod.getName().stringValue())) {
!"getClass".equals(theMethod.getName().stringValue()) &&
!"getEnumConstants".equals(theMethod.getName().stringValue())) {

if (theVisitedMethods.add(theMethodName)) {
theWriter.tab(2).text("p.").text(theMethodName).assign().text(theMinifier.toClassName(aEntry.getProvidingClass().getClassName())).text(".").text(theMethodName).text(";").newLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ private void print(final Value aValue) {
print((FloatingPointFloorExpression) aValue);
} else if (aValue instanceof FloatingPointCeilExpression) {
print((FloatingPointCeilExpression) aValue);
} else if (aValue instanceof EnumConstantsExpression) {
print((EnumConstantsExpression) aValue);
} else {
throw new IllegalStateException("Not implemented : " + aValue);
}
}

private void print(final EnumConstantsExpression aValue) {
print(aValue.incomingDataFlows().get(0));
writer.text(".");
writer.text(minifier.toSymbol("$VALUES"));
}

private void print(final MaxExpression aValue) {
writer.text("Math.max(");
print(aValue.incomingDataFlows().get(0));
Expand Down Expand Up @@ -1126,6 +1134,15 @@ public void writeExpressions(final ExpressionList aExpressions) {
startLine().text("__l").assign().text("" + theContinue.jumpTarget().getAddress()).text(";").newLine();
}
startLine().text("continue $").text(theContinue.labelToReturnTo().name()).text(";").newLine();
} else if (theExpression instanceof SetEnumConstantsExpression) {
final SetEnumConstantsExpression theSet = (SetEnumConstantsExpression) theExpression;
startLine();

print(theSet.incomingDataFlows().get(0));
writer.print(".").text(minifier.toSymbol("$VALUES")).assign();
print(theSet.incomingDataFlows().get(1));

writer.text(";").newLine();
} else {
throw new IllegalStateException("Not implemented : " + theExpression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ public WASMCompileResult generateCodeFor(
final ExportableFunction lambdaResolvevtableindex = module.getFunctions().newFunction("LAMBDA" + WASMSSAASTWriter.VTABLEFUNCTIONSUFFIX, Arrays.asList(param("thisRef", PrimitiveType.i32), param("methodId", PrimitiveType.i32)), PrimitiveType.i32).toTable();
lambdaResolvevtableindex.flow.ret(i32.load(8, getLocal(lambdaResolvevtableindex.localByLabel("thisRef"), null), null), null);

final ExportableFunction classGetEnumConstants = module.getFunctions().newFunction("jlClass_A1jlObjectgetEnumConstants", Collections.singletonList(param("thisRef", PrimitiveType.i32)), PrimitiveType.i32).toTable();
classGetEnumConstants.flow.ret(i32.load(0, i32.load(12, getLocal(classGetEnumConstants.localByLabel("thisRef"), null), null), null), null);

final ExportableFunction classGetName = module.getFunctions().newFunction("jlClass_jlStringgetName", Collections.singletonList(param("thisRef", PrimitiveType.i32)), PrimitiveType.i32).toTable();
{
final List<WASMValue> theGetArguments = new ArrayList<>();
Expand Down Expand Up @@ -285,8 +282,6 @@ public Function resolveCallsiteBootstrapFor(final BytecodeClass owningClass, fin
block.flow.unreachable(null);
} else if (Objects.equals("hashCode", theMethod.getName().stringValue())) {
block.flow.unreachable(null);
} else if (Objects.equals("getEnumConstants", theMethod.getName().stringValue())) {
block.flow.ret(i32.c(module.getTables().funcTable().indexOf(classGetEnumConstants), null), null);
} else {
block.flow.unreachable(null);
}
Expand Down Expand Up @@ -408,8 +403,8 @@ public Function resolveCallsiteBootstrapFor(final BytecodeClass owningClass, fin
if (!theMethod.getAccessFlags().isStatic() &&
!theMethod.isConstructor() &&
!theMethod.getAccessFlags().isAbstract() &&
(theMethod != BytecodeLinkedClass.GET_CLASS_PLACEHOLDER) &&
!"desiredAssertionStatus".equals(theMethod.getName().stringValue())) {
!"desiredAssertionStatus".equals(theMethod.getName().stringValue()) &&
!"getEnumConstants".equals(theMethod.getName().stringValue())) {

final BytecodeVirtualMethodIdentifier theMethodIdentifier = aLinkerContext.getMethodCollection()
.identifierFor(theMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,72 +64,7 @@
import de.mirkosertic.bytecoder.core.BytecodeTypeRef;
import de.mirkosertic.bytecoder.core.BytecodeVirtualMethodIdentifier;
import de.mirkosertic.bytecoder.relooper.Relooper;
import de.mirkosertic.bytecoder.ssa.ArrayEntryExpression;
import de.mirkosertic.bytecoder.ssa.ArrayLengthExpression;
import de.mirkosertic.bytecoder.ssa.ArrayStoreExpression;
import de.mirkosertic.bytecoder.ssa.BinaryExpression;
import de.mirkosertic.bytecoder.ssa.BreakExpression;
import de.mirkosertic.bytecoder.ssa.ByteValue;
import de.mirkosertic.bytecoder.ssa.CheckCastExpression;
import de.mirkosertic.bytecoder.ssa.ClassReferenceValue;
import de.mirkosertic.bytecoder.ssa.CompareExpression;
import de.mirkosertic.bytecoder.ssa.ComputedMemoryLocationReadExpression;
import de.mirkosertic.bytecoder.ssa.ComputedMemoryLocationWriteExpression;
import de.mirkosertic.bytecoder.ssa.ContinueExpression;
import de.mirkosertic.bytecoder.ssa.CurrentExceptionExpression;
import de.mirkosertic.bytecoder.ssa.DirectInvokeMethodExpression;
import de.mirkosertic.bytecoder.ssa.DoubleValue;
import de.mirkosertic.bytecoder.ssa.Expression;
import de.mirkosertic.bytecoder.ssa.ExpressionList;
import de.mirkosertic.bytecoder.ssa.FixedBinaryExpression;
import de.mirkosertic.bytecoder.ssa.FloatValue;
import de.mirkosertic.bytecoder.ssa.FloatingPointCeilExpression;
import de.mirkosertic.bytecoder.ssa.FloatingPointFloorExpression;
import de.mirkosertic.bytecoder.ssa.FloorExpression;
import de.mirkosertic.bytecoder.ssa.GetFieldExpression;
import de.mirkosertic.bytecoder.ssa.GetStaticExpression;
import de.mirkosertic.bytecoder.ssa.GotoExpression;
import de.mirkosertic.bytecoder.ssa.IFExpression;
import de.mirkosertic.bytecoder.ssa.InstanceOfExpression;
import de.mirkosertic.bytecoder.ssa.IntegerValue;
import de.mirkosertic.bytecoder.ssa.InvokeStaticMethodExpression;
import de.mirkosertic.bytecoder.ssa.InvokeVirtualMethodExpression;
import de.mirkosertic.bytecoder.ssa.LongValue;
import de.mirkosertic.bytecoder.ssa.LookupSwitchExpression;
import de.mirkosertic.bytecoder.ssa.MaxExpression;
import de.mirkosertic.bytecoder.ssa.MemorySizeExpression;
import de.mirkosertic.bytecoder.ssa.MethodHandlesGeneratedLookupExpression;
import de.mirkosertic.bytecoder.ssa.MethodRefExpression;
import de.mirkosertic.bytecoder.ssa.MethodTypeExpression;
import de.mirkosertic.bytecoder.ssa.MinExpression;
import de.mirkosertic.bytecoder.ssa.NegatedExpression;
import de.mirkosertic.bytecoder.ssa.NewArrayExpression;
import de.mirkosertic.bytecoder.ssa.NewMultiArrayExpression;
import de.mirkosertic.bytecoder.ssa.NewObjectExpression;
import de.mirkosertic.bytecoder.ssa.NullValue;
import de.mirkosertic.bytecoder.ssa.PHIExpression;
import de.mirkosertic.bytecoder.ssa.Program;
import de.mirkosertic.bytecoder.ssa.PutFieldExpression;
import de.mirkosertic.bytecoder.ssa.PutStaticExpression;
import de.mirkosertic.bytecoder.ssa.RegionNode;
import de.mirkosertic.bytecoder.ssa.ResolveCallsiteObjectExpression;
import de.mirkosertic.bytecoder.ssa.ReturnExpression;
import de.mirkosertic.bytecoder.ssa.ReturnValueExpression;
import de.mirkosertic.bytecoder.ssa.RuntimeGeneratedTypeExpression;
import de.mirkosertic.bytecoder.ssa.SetMemoryLocationExpression;
import de.mirkosertic.bytecoder.ssa.ShortValue;
import de.mirkosertic.bytecoder.ssa.SqrtExpression;
import de.mirkosertic.bytecoder.ssa.StackTopExpression;
import de.mirkosertic.bytecoder.ssa.StringValue;
import de.mirkosertic.bytecoder.ssa.TableSwitchExpression;
import de.mirkosertic.bytecoder.ssa.ThrowExpression;
import de.mirkosertic.bytecoder.ssa.TypeConversionExpression;
import de.mirkosertic.bytecoder.ssa.TypeOfExpression;
import de.mirkosertic.bytecoder.ssa.TypeRef;
import de.mirkosertic.bytecoder.ssa.UnreachableExpression;
import de.mirkosertic.bytecoder.ssa.Value;
import de.mirkosertic.bytecoder.ssa.Variable;
import de.mirkosertic.bytecoder.ssa.VariableAssignmentExpression;
import de.mirkosertic.bytecoder.ssa.*;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -377,6 +312,11 @@ private void generateExpressions(final Expression aExpression) {
flow.branch(target, aExpression);
return;
}
if (aExpression instanceof SetEnumConstantsExpression) {
final SetEnumConstantsExpression theSetEnum = (SetEnumConstantsExpression) aExpression;
flow.i32.store(0, i32.load(12, toValue(theSetEnum.incomingDataFlows().get(0)), null), toValue(theSetEnum.incomingDataFlows().get(1)), null);
return;
}

throw new IllegalStateException("Not supported : " + aExpression);
}
Expand Down Expand Up @@ -733,9 +673,16 @@ private WASMValue toValue(final Value aValue) {
if (aValue instanceof FloatingPointCeilExpression) {
return floatingPointCeil((FloatingPointCeilExpression) aValue);
}
if (aValue instanceof EnumConstantsExpression) {
return enumConstants((EnumConstantsExpression) aValue);
}
throw new IllegalStateException("Not supported : " + aValue);
}

private WASMValue enumConstants(final EnumConstantsExpression aValue) {
return i32.load(0, i32.load(12, toValue(aValue.incomingDataFlows().get(0)), null), null);
}

private WASMValue floatingPointCeil(final FloatingPointCeilExpression aValue) {
final List<Value> theArguments = aValue.incomingDataFlows();
return f32.ceil(toValue(theArguments.get(0)), aValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,7 @@ public class BytecodeLinkedClass extends Node {

public static final BytecodeMethodSignature GET_CLASS_SIGNATURE = new BytecodeMethodSignature(BytecodeObjectTypeRef.fromRuntimeClass(Class.class), new BytecodeTypeRef[0]);
public static final BytecodeMethodSignature DESIRED_ASSERTION_STATUS_SIGNATURE = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.BOOLEAN, new BytecodeTypeRef[0]);
public static final BytecodeMethod GET_CLASS_PLACEHOLDER = new BytecodeMethod(new BytecodeAccessFlags(0x0001), null, null, null) {
@Override
public BytecodeUtf8Constant getName() {
return new BytecodeUtf8Constant("getClass");
}

@Override
public BytecodeMethodSignature getSignature() {
return GET_CLASS_SIGNATURE;
}
};
public static final BytecodeMethodSignature GET_ENUM_CONSTANTS_SIGNATURE = new BytecodeMethodSignature(new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(Object.class), 1), new BytecodeTypeRef[0]);

private final int uniqueId;
private final BytecodeObjectTypeRef className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public Intrinsics() {
intrinsics.add(new JavaLangMathIntrinsic());
intrinsics.add(new RuntimeClassIntrinsic());
intrinsics.add(new ObjectConstructorCallIntrinsic());
}

public void add(final Intrinsic aIntrinsic) {
intrinsics.add(aIntrinsic);
intrinsics.add(new JavaLangEnumIntrinsic());
}

public boolean intrinsify(final Program aProgram, final BytecodeInstructionINVOKESTATIC aInstruction, final List<Value> aArguments,
Expand Down Expand Up @@ -96,4 +93,4 @@ public boolean intrinsify(final Program aProgram, final BytecodeInstructionPUTST
}
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2019 Mirko Sertic
*
* Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.mirkosertic.bytecoder.intrinsics;

import de.mirkosertic.bytecoder.core.*;
import de.mirkosertic.bytecoder.ssa.*;

import java.util.List;

public class JavaLangEnumIntrinsic extends Intrinsic {

@Override
public boolean intrinsify(final Program aProgram, final BytecodeInstructionINVOKEVIRTUAL aInstruction, final String aMethodName, final List<Value> aArguments, final Value aTarget, final RegionNode aTargetBlock, final ParsingHelper aHelper) {
final BytecodeMethodSignature theSignature = aInstruction.getMethodReference().getNameAndTypeIndex().getNameAndType().getDescriptorIndex().methodSignature();
if ("getEnumConstants".equals(aMethodName) && theSignature.matchesExactlyTo(BytecodeLinkedClass.GET_ENUM_CONSTANTS_SIGNATURE)) {
final Value theValue = new EnumConstantsExpression(aProgram, aInstruction.getOpcodeAddress(), aTarget);
final Variable theNewVariable = aTargetBlock.newVariable(aInstruction.getOpcodeAddress(), TypeRef.toType(theSignature.getReturnType()), theValue);
aHelper.push(theNewVariable);
return true;
}
return false;
}

@Override
public boolean intrinsify(final Program aProgram, final BytecodeInstructionGETSTATIC aInstruction, final String aFieldName, final BytecodeObjectTypeRef aTtargetType, final RegionNode aTargetBlock, final ParsingHelper aHelper) {
if ("$VALUES".equals(aFieldName)) {
final Value theValue = new EnumConstantsExpression(aProgram, aInstruction.getOpcodeAddress(), new ClassReferenceValue(aTtargetType));
aHelper.push(theValue);
return true;
}
return false;
}

@Override
public boolean intrinsify(final Program aProgram, final BytecodeInstructionPUTSTATIC aInstruction, final String aFieldName, final BytecodeObjectTypeRef aTtargetType, final Value aValue, final RegionNode aTargetBlock, final ParsingHelper aHelper) {
if ("$VALUES".equals(aFieldName)) {
aTargetBlock.getExpressions().add(new SetEnumConstantsExpression(aProgram, aInstruction.getOpcodeAddress(), new ClassReferenceValue(aTtargetType), aValue));
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class JavaLangMathIntrinsic extends Intrinsic {
public boolean intrinsify(final Program aProgram, final BytecodeInstructionINVOKESTATIC aInstruction, final String aMethodName, final List<Value> aArguments, final BytecodeObjectTypeRef aTargetClass, final RegionNode aTargetBlock, final ParsingHelper aHelper) {
if (Math.class.getName().equals(aTargetClass.name())) {
final BytecodeMethodSignature theSignature = aInstruction.getMethodReference().getNameAndTypeIndex().getNameAndType().getDescriptorIndex().methodSignature();
if ("sqrt".equals(aInstruction.getMethodReference().getNameAndTypeIndex().getNameAndType().getNameIndex().getName().stringValue())) {
if ("sqrt".equals(aMethodName)) {
final Value theValue = new SqrtExpression(aProgram, aInstruction.getOpcodeAddress(), TypeRef.toType(theSignature.getReturnType()),
aArguments.get(0));
final Variable theNewVariable = aTargetBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public boolean intrinsify(final Program aProgram, final BytecodeInstructionINVOK
@Override
public boolean intrinsify(final Program aProgram, final BytecodeInstructionINVOKEVIRTUAL aInstruction, final String aMethodName, final List<Value> aArguments, final Value aTarget, final RegionNode aTargetBlock, final ParsingHelper aHelper) {
final BytecodeMethodSignature theSignature = aInstruction.getMethodReference().getNameAndTypeIndex().getNameAndType().getDescriptorIndex().methodSignature();
if (theSignature.matchesExactlyTo(BytecodeLinkedClass.GET_CLASS_SIGNATURE) && "getClass".equals(aMethodName)) {
if ("getClass".equals(aMethodName) && theSignature.matchesExactlyTo(BytecodeLinkedClass.GET_CLASS_SIGNATURE)) {
final Value theValue = new TypeOfExpression(aProgram, aInstruction.getOpcodeAddress(), aTarget);
final Variable theNewVariable = aTargetBlock.newVariable(aInstruction.getOpcodeAddress(), TypeRef.toType(theSignature.getReturnType()), theValue);
aHelper.push(theNewVariable);
return true;
}
if (theSignature.matchesExactlyTo(BytecodeLinkedClass.DESIRED_ASSERTION_STATUS_SIGNATURE) && "desiredAssertionStatus".equals(aMethodName)) {
if ("desiredAssertionStatus".equals(aMethodName) && theSignature.matchesExactlyTo(BytecodeLinkedClass.DESIRED_ASSERTION_STATUS_SIGNATURE)) {
// Status is always false
aHelper.push(new IntegerValue(0));
return true;
Expand Down
Loading