use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.
the class JSSSAWriter method print.
private void print(InvokeVirtualMethodExpression aValue) {
String theMethodName = aValue.getMethodName();
BytecodeMethodSignature theSignature = aValue.getSignature();
List<Value> theIncomingData = aValue.incomingDataFlows();
Value theTarget = theIncomingData.get(0);
List<Value> theArguments = theIncomingData.subList(1, theIncomingData.size());
BytecodeVirtualMethodIdentifier theMethodIdentifier = linkerContext.getMethodCollection().identifierFor(theMethodName, theSignature);
if (Objects.equals(aValue.getMethodName(), "invokeWithMagicBehindTheScenes")) {
print("(");
} else {
print(theTarget);
print(".");
print(JSWriterUtils.toMethodName(theMethodName, theSignature));
print("(");
}
print(theTarget);
for (Value theArgument : theArguments) {
print(",");
print(theArgument);
}
print(")");
}
use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.
the class JSSSAWriter method print.
private void print(GetFieldExpression aValue) {
Value theTarget = aValue.incomingDataFlows().get(0);
BytecodeFieldRefConstant theField = aValue.getField();
print(theTarget);
printInstanceFieldReference(theField);
}
use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.
the class JSSSAWriter method writeExpressions.
private void writeExpressions(ExpressionList aExpressions) {
for (Expression theExpression : aExpressions.toList()) {
if (options.isDebugOutput()) {
String theComment = theExpression.getComment();
if (theComment != null && !theComment.isEmpty()) {
print("// ");
println(theComment);
}
}
if (theExpression instanceof ReturnExpression) {
ReturnExpression theE = (ReturnExpression) theExpression;
print("return");
println(";");
} else if (theExpression instanceof VariableAssignmentExpression) {
VariableAssignmentExpression theE = (VariableAssignmentExpression) theExpression;
Variable theVariable = theE.getVariable();
Value theValue = theE.getValue();
if (theValue instanceof ComputedMemoryLocationWriteExpression) {
continue;
}
if (!program.isGlobalVariable(theVariable)) {
print("var ");
}
print(theVariable.getName());
print(" = ");
print(theValue);
print("; // type is ");
println(theVariable.resolveType().resolve().name() + " value type is " + theValue.resolveType());
} else if (theExpression instanceof PutStaticExpression) {
PutStaticExpression theE = (PutStaticExpression) theExpression;
BytecodeFieldRefConstant theField = theE.getField();
Value theValue = theE.incomingDataFlows().get(0);
printStaticFieldReference(theField);
print(" = ");
print(theValue);
println(";");
} else if (theExpression instanceof ReturnValueExpression) {
ReturnValueExpression theE = (ReturnValueExpression) theExpression;
Value theValue = theE.incomingDataFlows().get(0);
print("return ");
print(theValue);
println(";");
} else if (theExpression instanceof ThrowExpression) {
ThrowExpression theE = (ThrowExpression) theExpression;
Value theValue = theE.incomingDataFlows().get(0);
print("throw ");
print(theValue);
println(";");
} else if (theExpression instanceof InvokeVirtualMethodExpression) {
InvokeVirtualMethodExpression theE = (InvokeVirtualMethodExpression) theExpression;
print(theE);
println(";");
} else if (theExpression instanceof DirectInvokeMethodExpression) {
DirectInvokeMethodExpression theE = (DirectInvokeMethodExpression) theExpression;
print(theE);
println(";");
} else if (theExpression instanceof InvokeStaticMethodExpression) {
InvokeStaticMethodExpression theE = (InvokeStaticMethodExpression) theExpression;
print(theE);
println(";");
} else if (theExpression instanceof PutFieldExpression) {
PutFieldExpression theE = (PutFieldExpression) theExpression;
List<Value> theIncomingData = theE.incomingDataFlows();
Value theTarget = theIncomingData.get(0);
BytecodeFieldRefConstant theField = theE.getField();
Value thevalue = theIncomingData.get(1);
print(theTarget);
printInstanceFieldReference(theField);
print(" = ");
print(thevalue);
println(";");
} else if (theExpression instanceof IFExpression) {
IFExpression theE = (IFExpression) theExpression;
print("if (");
print(theE.incomingDataFlows().get(0));
println(") {");
withDeeperIndent().writeExpressions(theE.getExpressions());
println("}");
} else if (theExpression instanceof GotoExpression) {
GotoExpression theE = (GotoExpression) theExpression;
println(generateJumpCodeFor(theE.getJumpTarget()));
} else if (theExpression instanceof ArrayStoreExpression) {
ArrayStoreExpression theE = (ArrayStoreExpression) theExpression;
List<Value> theIncomingData = theE.incomingDataFlows();
Value theArray = theIncomingData.get(0);
Value theIndex = theIncomingData.get(1);
Value theValue = theIncomingData.get(2);
print(theArray);
printArrayIndexReference(theIndex);
print(" = ");
print(theValue);
println(";");
} else if (theExpression instanceof CheckCastExpression) {
CheckCastExpression theE = (CheckCastExpression) theExpression;
// Completely ignored
} else if (theExpression instanceof TableSwitchExpression) {
TableSwitchExpression theE = (TableSwitchExpression) theExpression;
Value theValue = theE.incomingDataFlows().get(0);
print("if (");
print(theValue);
print(" < ");
print(theE.getLowValue());
print(" || ");
print(theValue);
print(" > ");
print(theE.getHighValue());
println(") {");
print(" ");
writeExpressions(theE.getDefaultExpressions());
println("}");
print("switch(");
print(theValue);
print(" - ");
print(theE.getLowValue());
println(") {");
for (Map.Entry<Long, ExpressionList> theEntry : theE.getOffsets().entrySet()) {
print(" case ");
print(theEntry.getKey());
println(":");
print(" ");
writeExpressions(theEntry.getValue());
}
println("}");
println("throw 'Illegal jump target!';");
} else if (theExpression instanceof LookupSwitchExpression) {
LookupSwitchExpression theE = (LookupSwitchExpression) theExpression;
print("switch(");
print(theE.incomingDataFlows().get(0));
println(") {");
for (Map.Entry<Long, ExpressionList> theEntry : theE.getPairs().entrySet()) {
print(" case ");
print(theEntry.getKey());
println(":");
print(" ");
writeExpressions(theEntry.getValue());
}
println("}");
writeExpressions(theE.getDefaultExpressions());
} else if (theExpression instanceof SetMemoryLocationExpression) {
SetMemoryLocationExpression theE = (SetMemoryLocationExpression) theExpression;
List<Value> theIncomingData = theE.incomingDataFlows();
print("bytecoderGlobalMemory[");
ComputedMemoryLocationWriteExpression theValue = (ComputedMemoryLocationWriteExpression) theIncomingData.get(0);
print(theValue);
print("] = ");
print(theIncomingData.get(1));
println(";");
} else if (theExpression instanceof UnreachableExpression) {
println("throw 'Unreachable';");
} else if (theExpression instanceof BreakExpression) {
BreakExpression theBreak = (BreakExpression) theExpression;
if (theBreak.isSetLabelRequired()) {
print("__label__ = ");
print(theBreak.jumpTarget().getAddress());
println(";");
}
if (!theBreak.isSilent()) {
print("break $");
print(theBreak.blockToBreak().name());
println(";");
}
} else if (theExpression instanceof ContinueExpression) {
ContinueExpression theContinue = (ContinueExpression) theExpression;
print("__label__ = ");
print(theContinue.jumpTarget().getAddress());
println(";");
print("continue $");
print(theContinue.labelToReturnTo().name());
println(";");
} else {
throw new IllegalStateException("Not implemented : " + theExpression);
}
}
}
use of de.mirkosertic.bytecoder.ssa.Value in project Bytecoder by mirkosertic.
the class JSSSAWriter method print.
private void print(InstanceOfExpression aValue) {
Value theValue = aValue.incomingDataFlows().get(0);
print("(");
print(theValue);
print(" == null ? false : ");
print(theValue);
print(".instanceOf(");
BytecodeUtf8Constant theConstant = aValue.getType().getConstant();
if (!theConstant.stringValue().startsWith("[")) {
BytecodeLinkedClass theLinkedClass = linkerContext.isLinkedOrNull(aValue.getType().getConstant());
print(JSWriterUtils.toClassName(theLinkedClass.getClassName()));
} else {
BytecodeLinkedClass theLinkedClass = linkerContext.resolveClass(BytecodeObjectTypeRef.fromRuntimeClass(Array.class));
print(JSWriterUtils.toClassName(theLinkedClass.getClassName()));
}
print(")");
print(")");
}
Aggregations