Class WasmCodeBuilder
java.lang.Object
de.inetsoftware.jwebassembly.module.WasmCodeBuilder
- Direct Known Subclasses:
JavaMethodWasmCodeBuilder, WatParser
Base class for Code Building.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final StringJava method name of static constructor or initialization methodprivate ClassFileLoader(package private) static final StringJava method name of of constructorprivate FunctionManagerprivate final List<WasmInstruction> private final LocaleVariableManagerprivate WasmOptionsprivate StringManagerprivate TypeManager -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreate a new instance of CodeBuilder(package private)WasmCodeBuilder(WasmCodeBuilder codeBuilder) Create a new instance with shared resources -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddArrayInstruction(ArrayOperator op, AnyType type, int javaCodePos, int lineNumber) Add an array operation to the instruction list as marker on the code position.protected voidaddBlockInstruction(WasmBlockOperator op, Object data, int javaCodePos, int lineNumber) Add a block operation.private voidaddCallIndirectInstruction(WasmCallIndirectInstruction indirectCall) Add indirect call to the instruction.protected voidaddCallInstruction(FunctionName name, boolean needThisParameter, int javaCodePos, int lineNumber) Add a static function call.protected voidaddCallInterfaceInstruction(FunctionName name, int javaCodePos, int lineNumber) Add interface function callprotected voidaddCallVirtualInstruction(FunctionName name, int javaCodePos, int lineNumber) Add a virtual/method function call.protected voidaddConstInstruction(Number value, ValueType valueType, int javaCodePos, int lineNumber) Add a constant instruction.protected voidaddConstInstruction(Object value, int javaCodePos, int lineNumber) Add a constant instruction with unknown value type.protected voidaddConvertInstruction(ValueTypeConvertion conversion, int javaCodePos, int lineNumber) Add a value convert/cast instruction.protected voidaddDupInstruction(boolean dup2, int javaCodePos, int lineNumber) Create a WasmDupInstruction.protected voidaddDupX1Instruction(int javaCodePos, int lineNumber) Simulate the dup_x1 Java byte code instruction.protected voidaddDupX2Instruction(int javaCodePos, int lineNumber) Simulate the dup_x2 Java byte code instruction.protected voidaddGlobalInstruction(boolean load, Member ref, int javaCodePos, int lineNumber) Add a global instructionprotected voidaddGlobalInstruction(boolean load, FunctionName name, AnyType type, FunctionName clinit, int javaCodePos, int lineNumber) Add a global instructionprotected voidaddInvokeDynamic(BootstrapMethod method, String factorySignature, String interfaceMethodName, int javaCodePos, int lineNumber) Add invoke dynamic operation.protected voidaddJumpPlaceholder(int jumpPos, int popCount, AnyType pushValueType, int javaCodePos, int lineNumber) Add a Jump instruction for later stack inspectionprotected voidaddLoadStoreInstruction(AnyType valueType, boolean load, int javaIdx, int javaCodePos, int lineNumber) Create a WasmLoadStoreInstruction.protected voidaddLocalInstruction(VariableOperator op, int wasmIdx, int javaCodePos, int lineNumber) Create a WasmLoadStoreInstruction local.get/local.set.protected voidaddMemoryInstruction(MemoryOperator op, ValueType type, int offset, int alignment, int javaCodePos, int lineNumber) Create an instance of a load/store to the linear memory instructionprotected voidaddMultiNewArrayInstruction(int dim, ArrayType type, int javaCodePos, int lineNumber) Add a new multi dimensional array instructionprotected voidaddMultiNewArrayInstruction(int dim, String typeName, int javaCodePos, int lineNumber) Add a new multi dimensional array instructionprotected voidaddNopInstruction(int javaCodePos, int lineNumber) Add a no operation to the instruction list as marker on the code position.protected WasmNumericInstructionaddNumericInstruction(NumericOperator numOp, ValueType valueType, int javaCodePos, int lineNumber) Add a numeric operation instructionprotected voidaddStructInstruction(StructOperator op, String typeName, NamedStorageType fieldName, int javaCodePos, int lineNumber) Add a struct/object operation to the instruction list.protected voidaddTableInstruction(boolean load, int idx, int javaCodePos, int lineNumber) Add a WasmTableInstruction table.get/table.set.protected voidCalculate the index of the variables(package private) AnyTypefindArrayTypeFromStack(int count, int javaCodePos) Find the array component type from stack.private intfindBlockStart(int count, boolean codePosition) We need one value from the stack inside of a block.(package private) intfindBlockStartCodePosition(int count) We need one value from the stack inside of a block.private WasmInstructionfindInstructionThatPushValue(int count, int javaCodePos) Find the instruction that push the x-th value to the stack.(package private) AnyTypefindValueTypeFromStack(int count, int javaCodePos) We need the value type from the stack.(package private) List<WasmInstruction> Get the list of instructions(package private) StringgetLocalName(int idx) Get the name of the variable or null if no name availablegetLocalTypes(int paramCount) Get the data types of the local variables.(package private) LocaleVariableManagerGet the manager of local variablesprivate FunctionNameGet a non GC polyfill function.(package private) WasmOptionsGet the compiler settingsprivate static intgetPossibleSlot(WasmInstruction instr) Get a possible slot from the instruction(package private) intgetTempVariable(AnyType valueType, int startCodePosition, int endCodePosition) Get the slot of the temporary variable.protected TypeManagerGet the type manager.(package private) voidinit(WasmOptions options, ClassFileLoader classFileLoader) Initialize the code builder;private static booleanif the string contains only ASCCI characters(package private) booleanCheck if the last instruction is a return instructionprotected voidreset(LocalVariableTable variableTable, MethodInfo method, Iterator<AnyType> signature) Reset the code builder.
-
Field Details
-
CONSTRUCTOR
-
CLASS_INIT
Java method name of static constructor or initialization method- See Also:
-
localVariables
-
instructions
-
types
-
functions
-
options
-
strings
-
classFileLoader
-
-
Constructor Details
-
WasmCodeBuilder
protected WasmCodeBuilder()Create a new instance of CodeBuilder -
WasmCodeBuilder
WasmCodeBuilder(@Nonnull WasmCodeBuilder codeBuilder) Create a new instance with shared resources- Parameters:
codeBuilder- other instance of CodeBuilder
-
-
Method Details
-
init
Initialize the code builder;- Parameters:
options- compiler propertiesclassFileLoader- for loading the class files
-
getInstructions
-
getLocalVariables
Get the manager of local variables- Returns:
- the manager
-
getOptions
-
isEndsWithReturn
boolean isEndsWithReturn()Check if the last instruction is a return instruction- Returns:
- true, if a return
-
findBlockStartCodePosition
int findBlockStartCodePosition(int count) We need one value from the stack inside of a block. We need to find the WasmInstruction on which the block can start. If this a function call or numeric expression this can be complex to find the right point.- Parameters:
count- the count of values on the stack back. 1 means the last value. 2 means the penultimate value.- Returns:
- the code position that push the last instruction
-
findBlockStart
private int findBlockStart(int count, boolean codePosition) We need one value from the stack inside of a block. We need to find the WasmInstruction on which the block can start. If this a function call or numeric expression this can be complex to find the right point.- Parameters:
count- the count of values on the stack back. 1 means the last value. 2 means the penultimate value.codePosition- true, get the code position; false, get the index in the instructions- Returns:
- the code position that push the last instruction
-
findValueTypeFromStack
We need the value type from the stack.- Parameters:
count- the count of values on the stack back. 1 means the last value. 2 means the penultimate value.javaCodePos- current code position for which the stack is inspected- Returns:
- the type of the last push value
-
findArrayTypeFromStack
Find the array component type from stack.- Parameters:
count- the count of values on the stack back. 1 means the last value. 2 means the penultimate value.javaCodePos- current code position for which the stack is inspected- Returns:
-
findInstructionThatPushValue
Find the instruction that push the x-th value to the stack.- Parameters:
count- the count of values on the stack back. 1 means the last value. 2 means the penultimate value.javaCodePos- current code position for which the stack is inspected- Returns:
- the instruction
-
getLocalTypes
-
getLocalName
Get the name of the variable or null if no name available- Parameters:
idx- the wasm variable index- Returns:
- the name
-
getTempVariable
Get the slot of the temporary variable.- Parameters:
valueType- the valueType for the variablestartCodePosition- the start of the Java code positionendCodePosition- the end of the Java code position- Returns:
- the slot
-
getTypeManager
-
reset
protected void reset(LocalVariableTable variableTable, MethodInfo method, Iterator<AnyType> signature) Reset the code builder.- Parameters:
variableTable- variable table of the Java method.method- the method with signature as fallback for a missing variable table. If null signature is used and the method must be static.signature- alternative for method signature, can be null if method is set
-
calculateVariables
protected void calculateVariables()Calculate the index of the variables -
addLoadStoreInstruction
protected void addLoadStoreInstruction(AnyType valueType, boolean load, @Nonnegative int javaIdx, int javaCodePos, int lineNumber) Create a WasmLoadStoreInstruction.- Parameters:
valueType- the value typeload- true: if loadjavaIdx- the memory/slot index of the variable in Java byte codejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addLocalInstruction
protected void addLocalInstruction(VariableOperator op, @Nonnegative int wasmIdx, int javaCodePos, int lineNumber) Create a WasmLoadStoreInstruction local.get/local.set.- Parameters:
op- the operationwasmIdx- the index of the variablejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
getPossibleSlot
Get a possible slot from the instruction- Parameters:
instr- the instruction- Returns:
- the slot or -1 if there no slot
-
addDupInstruction
protected void addDupInstruction(boolean dup2, int javaCodePos, int lineNumber) Create a WasmDupInstruction.- Parameters:
dup2- call from dup2 instructionjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addDupX1Instruction
protected void addDupX1Instruction(int javaCodePos, int lineNumber) Simulate the dup_x1 Java byte code instruction...., value2, value1 → ..., value1, value2, value1
- Parameters:
javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addDupX2Instruction
protected void addDupX2Instruction(int javaCodePos, int lineNumber) Simulate the dup_x2 Java byte code instruction...., value3, value2, value1 → ..., value1, value3, value2, value1
- Parameters:
javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addGlobalInstruction
Add a global instruction- Parameters:
load- true: if loadref- reference to a static fieldjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addGlobalInstruction
protected void addGlobalInstruction(boolean load, FunctionName name, AnyType type, FunctionName clinit, int javaCodePos, int lineNumber) Add a global instruction- Parameters:
load- true: if loadname- reference to a static fieldtype- the type of the static fieldclinit- a reference to the class/static constructor which should executed before access a static fieldjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addTableInstruction
protected void addTableInstruction(boolean load, @Nonnegative int idx, int javaCodePos, int lineNumber) Add a WasmTableInstruction table.get/table.set.- Parameters:
load- true: if loadidx- the index of the tablejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addConstInstruction
-
addConstInstruction
Add a constant instruction with unknown value type.- Parameters:
value- the valuejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
isAscii
if the string contains only ASCCI characters- Parameters:
str- the staring- Returns:
- true, if only ASCII
-
addNumericInstruction
protected WasmNumericInstruction addNumericInstruction(@Nullable NumericOperator numOp, @Nullable ValueType valueType, int javaCodePos, int lineNumber) Add a numeric operation instruction- Parameters:
numOp- the operationvalueType- the value typejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code- Returns:
- the added instruction
-
addConvertInstruction
protected void addConvertInstruction(ValueTypeConvertion conversion, int javaCodePos, int lineNumber) Add a value convert/cast instruction.- Parameters:
conversion- the conversionjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addCallInstruction
protected void addCallInstruction(FunctionName name, boolean needThisParameter, int javaCodePos, int lineNumber) Add a static function call.- Parameters:
name- the function name that should be calledneedThisParameter- true, if the hidden THIS parameter is needed, If it is an instance method call.javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addCallIndirectInstruction
Add indirect call to the instruction.- Parameters:
indirectCall- the instruction
-
addCallVirtualInstruction
Add a virtual/method function call.- Parameters:
name- the function name that should be calledjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addCallInterfaceInstruction
Add interface function call- Parameters:
name- the function name that should be calledjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addBlockInstruction
protected void addBlockInstruction(WasmBlockOperator op, @Nullable Object data, int javaCodePos, int lineNumber) Add a block operation.- Parameters:
op- the operationdata- extra data for some operationsjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addNopInstruction
protected void addNopInstruction(int javaCodePos, int lineNumber) Add a no operation to the instruction list as marker on the code position. This instruction will not be write to the output.- Parameters:
javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addJumpPlaceholder
protected void addJumpPlaceholder(int jumpPos, int popCount, AnyType pushValueType, int javaCodePos, int lineNumber) Add a Jump instruction for later stack inspection- Parameters:
jumpPos- the position of the jumppopCount- the the count of values that are removed from the stack.pushValueType- optional type of a push valuejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
getNonGC
Get a non GC polyfill function.- Parameters:
name- the function namelineNumber- the line number for a possible error- Returns:
- the function name
-
addArrayInstruction
protected void addArrayInstruction(@Nonnull ArrayOperator op, @Nonnull AnyType type, int javaCodePos, int lineNumber) Add an array operation to the instruction list as marker on the code position.- Parameters:
op- the operationtype- the array/component type of the arrayjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addMultiNewArrayInstruction
protected void addMultiNewArrayInstruction(int dim, String typeName, int javaCodePos, int lineNumber) Add a new multi dimensional array instruction- Parameters:
dim- the dimension of the array >= 2typeName- the full type namejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addMultiNewArrayInstruction
protected void addMultiNewArrayInstruction(int dim, ArrayType type, int javaCodePos, int lineNumber) Add a new multi dimensional array instruction- Parameters:
dim- the dimension of the array >= 2type- the full typejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addStructInstruction
protected void addStructInstruction(StructOperator op, @Nonnull String typeName, @Nullable NamedStorageType fieldName, int javaCodePos, int lineNumber) Add a struct/object operation to the instruction list.- Parameters:
op- the operationtypeName- the type name like "java/lang/Object"fieldName- the name of field if needed for the operationjavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addInvokeDynamic
protected void addInvokeDynamic(BootstrapMethod method, String factorySignature, String interfaceMethodName, int javaCodePos, int lineNumber) Add invoke dynamic operation. (Creating of a lambda expression)- Parameters:
method- the BootstrapMethod, described the method that should be executedfactorySignature- Get the signature of the factory method. For example "()Ljava.lang.Runnable;" for the lamba expressionRunnable run = () -> foo();interfaceMethodName- The simple name of the generated method of the single function interface.javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addMemoryInstruction
protected void addMemoryInstruction(MemoryOperator op, ValueType type, int offset, int alignment, int javaCodePos, int lineNumber) Create an instance of a load/store to the linear memory instruction- Parameters:
op- the operationtype- the type of the static fieldoffset- the base offset which will be added to the offset value on the stackalignment- the alignment of the value on the linear memory (0: 8 Bit; 1: 16 Bit; 2: 32 Bit)javaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-