Class JavaMethodWasmCodeBuilder
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.WasmCodeBuilder
-
- de.inetsoftware.jwebassembly.module.JavaMethodWasmCodeBuilder
-
class JavaMethodWasmCodeBuilder extends WasmCodeBuilder
Convert Java Byte Code to a list of WasmInstruction.
-
-
Field Summary
Fields Modifier and Type Field Description private BranchManager
branchManager
-
Fields inherited from class de.inetsoftware.jwebassembly.module.WasmCodeBuilder
CLASS_INIT, CONSTRUCTOR
-
-
Constructor Summary
Constructors Constructor Description JavaMethodWasmCodeBuilder(WasmCodeBuilder codeBuilder)
Create an instance
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
buildCode(Code code, MethodInfo method)
Build the wasm instructionsprivate static int
findNext(int current, int[] values)
Find the next higher value.(package private) void
init(WasmOptions options, ClassFileLoader classFileLoader)
Initialize the code builder;private void
opCompare(ValueType valueType, CodeInputStream byteCode, int codePos, int lineNumber)
Handle the different compare operator.private void
opIfCompareCondition(NumericOperator compareOp, CodeInputStream byteCode, int codePos, int lineNumber)
Handle the ifof the Java byte code. private void
opIfCondition(NumericOperator compareOp, CodeInputStream byteCode, int codePos, int lineNumber)
Handle the ifof the Java byte code. private void
patchTypeOfNullConst()
NULL const has no type in Java.private void
writeCode(CodeInputStream byteCode, ConstantPool constantPool, ClassFile classFile, AnyType returnType)
Write the byte code of a method.private void
writeSwitchCode(CodeInputStream byteCode, boolean isLookupSwitch)
Write the both switch operation codes-
Methods inherited from class de.inetsoftware.jwebassembly.module.WasmCodeBuilder
addArrayInstruction, addBlockInstruction, addCallInstruction, addCallInterfaceInstruction, addCallVirtualInstruction, addConstInstruction, addConstInstruction, addConvertInstruction, addDupInstruction, addDupX1Instruction, addDupX2Instruction, addGlobalInstruction, addGlobalInstruction, addInvokeDynamic, addJumpPlaceholder, addLoadStoreInstruction, addLocalInstruction, addMemoryInstruction, addMultiNewArrayInstruction, addMultiNewArrayInstruction, addNopInstruction, addNumericInstruction, addStructInstruction, addTableInstruction, calculateVariables, findArrayTypeFromStack, findBlockStartCodePosition, findValueTypeFromStack, getInstructions, getLocalName, getLocalTypes, getLocalVariables, getOptions, getTempVariable, getTypeManager, isEndsWithReturn, reset
-
-
-
-
Field Detail
-
branchManager
private BranchManager branchManager
-
-
Constructor Detail
-
JavaMethodWasmCodeBuilder
JavaMethodWasmCodeBuilder(@Nonnull WasmCodeBuilder codeBuilder)
Create an instance- Parameters:
codeBuilder
- other code builder for shred references
-
-
Method Detail
-
init
void init(WasmOptions options, ClassFileLoader classFileLoader)
Initialize the code builder;- Overrides:
init
in classWasmCodeBuilder
- Parameters:
options
- compiler propertiesclassFileLoader
- for loading the class files
-
buildCode
void buildCode(@Nonnull Code code, MethodInfo method)
Build the wasm instructions- Parameters:
code
- the Java method codemethod
- the method with signature as fallback for a missing variable table- Throws:
WasmException
- if some Java code can't converted
-
writeCode
private void writeCode(CodeInputStream byteCode, ConstantPool constantPool, ClassFile classFile, AnyType returnType) throws WasmException
Write the byte code of a method.- Parameters:
byteCode
- a stream of byte codeconstantPool
- the constant pool of the the current classclassFile
- the declaring class filereturnType
- the return type of the method- Throws:
WasmException
- if some Java code can't converted
-
writeSwitchCode
private void writeSwitchCode(CodeInputStream byteCode, boolean isLookupSwitch) throws java.io.IOException
Write the both switch operation codes- Parameters:
byteCode
- the current stream with a position after the operation codeisLookupSwitch
- true, if the operation was a loopupswitch; false, if the operation was a tableswitch- Throws:
java.io.IOException
- if any I/O error occur
-
findNext
private static int findNext(int current, int[] values)
Find the next higher value.- Parameters:
current
- the current valuevalues
- the unordered list of values- Returns:
- the next value or current value if not found.
-
opIfCondition
private void opIfCondition(NumericOperator compareOp, CodeInputStream byteCode, int codePos, int lineNumber) throws java.io.IOException
Handle the ifof the Java byte code. This Java instruction compare the first stack value with value 0. Important: In the Java IF expression the condition for the jump to the else block is saved. In WebAssembler we need to use condition for the if block. The caller of the method must already negate this - Parameters:
compareOp
- The condition for the continue of a loop.byteCode
- current byte code stream to read the target offset.codePos
- the code position/offset in the Java methodlineNumber
- the line number in the Java source code- Throws:
java.io.IOException
- if any I/O errors occur.
-
opIfCompareCondition
private void opIfCompareCondition(NumericOperator compareOp, CodeInputStream byteCode, int codePos, int lineNumber) throws java.io.IOException
Handle the ifof the Java byte code. This Java instruction compare 2 values from stack. Important: In the Java IF expression the condition for the jump to the else block is saved. In WebAssembler we need to use condition for the if block. The caller of the method must already negate this. - Parameters:
compareOp
- The condition for the continue of a loop.byteCode
- current byte code stream to read the target offset.codePos
- the code position/offset in the Java methodlineNumber
- the line number in the Java source code- Throws:
java.io.IOException
- if any I/O errors occur.
-
opCompare
private void opCompare(ValueType valueType, CodeInputStream byteCode, int codePos, int lineNumber) throws java.io.IOException
Handle the different compare operator. The compare operator returns the integer values -1, 0 or 1. There is no equivalent in WebAssembly. That we need to read the next operation to find an equivalent.- Parameters:
valueType
- the value type of the comparedbyteCode
- current byte code stream to read the next operation.codePos
- the code position/offset in the Java methodlineNumber
- the line number in the Java source code- Throws:
java.io.IOException
- if any I/O errors occur.
-
patchTypeOfNullConst
private void patchTypeOfNullConst()
NULL const has no type in Java. In WebAssembly currently.
-
-