Class WasmCodeBuilder
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.WasmCodeBuilder
-
- Direct Known Subclasses:
JavaMethodWasmCodeBuilder,WatParser
public abstract class WasmCodeBuilder extends java.lang.ObjectBase class for Code Building.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.StringCLASS_INITJava method name of static constructor or initialization methodprivate ClassFileLoaderclassFileLoader(package private) static java.lang.StringCONSTRUCTORJava method name of of constructorprivate FunctionManagerfunctionsprivate java.util.List<WasmInstruction>instructionsprivate LocaleVariableManagerlocalVariablesprivate WasmOptionsoptionsprivate StringManagerstringsprivate TypeManagertypes
-
Constructor Summary
Constructors Modifier Constructor Description protectedWasmCodeBuilder()Create a new instance of CodeBuilder(package private)WasmCodeBuilder(WasmCodeBuilder codeBuilder)Create a new instance with shared resources
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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, java.lang.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(java.lang.Number value, ValueType valueType, int javaCodePos, int lineNumber)Add a constant instruction.protected voidaddConstInstruction(java.lang.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, java.lang.String factorySignature, java.lang.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, java.lang.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, java.lang.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 voidcalculateVariables()Calculate 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) java.util.List<WasmInstruction>getInstructions()Get the list of instructions(package private) java.lang.StringgetLocalName(int idx)Get the name of the variable or null if no name available(package private) java.util.List<AnyType>getLocalTypes(int paramCount)Get the data types of the local variables.(package private) LocaleVariableManagergetLocalVariables()Get the manager of local variablesprivate FunctionNamegetNonGC(java.lang.String name, int lineNumber)Get a non GC polyfill function.(package private) WasmOptionsgetOptions()Get 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 TypeManagergetTypeManager()Get the type manager.(package private) voidinit(WasmOptions options, ClassFileLoader classFileLoader)Initialize the code builder;private static booleanisAscii(java.lang.String str)if the string contains only ASCCI characters(package private) booleanisEndsWithReturn()Check if the last instruction is a return instructionprotected voidreset(LocalVariableTable variableTable, MethodInfo method, java.util.Iterator<AnyType> signature)Reset the code builder.
-
-
-
Field Detail
-
CONSTRUCTOR
static final java.lang.String CONSTRUCTOR
Java method name of of constructor- See Also:
- Constant Field Values
-
CLASS_INIT
static final java.lang.String CLASS_INIT
Java method name of static constructor or initialization method- See Also:
- Constant Field Values
-
localVariables
private final LocaleVariableManager localVariables
-
instructions
private final java.util.List<WasmInstruction> instructions
-
types
private TypeManager types
-
functions
private FunctionManager functions
-
options
private WasmOptions options
-
strings
private StringManager strings
-
classFileLoader
private ClassFileLoader classFileLoader
-
-
Constructor Detail
-
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 Detail
-
init
void init(WasmOptions options, ClassFileLoader classFileLoader)
Initialize the code builder;- Parameters:
options- compiler propertiesclassFileLoader- for loading the class files
-
getInstructions
@Nonnull java.util.List<WasmInstruction> getInstructions()
Get the list of instructions- Returns:
- the list
-
getLocalVariables
@Nonnull LocaleVariableManager getLocalVariables()
Get the manager of local variables- Returns:
- the manager
-
getOptions
@Nonnull WasmOptions getOptions()
Get the compiler settings- Returns:
- the settings
-
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
@Nonnull AnyType findValueTypeFromStack(int count, int javaCodePos)
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
@Nonnull AnyType findArrayTypeFromStack(int count, int javaCodePos)
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
@Nonnull private WasmInstruction findInstructionThatPushValue(int count, int javaCodePos)
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
java.util.List<AnyType> getLocalTypes(int paramCount)
Get the data types of the local variables. The value is only valid until the next call.- Parameters:
paramCount- the count of method parameter which should be exclude- Returns:
- the reused list with fresh values
-
getLocalName
java.lang.String getLocalName(int idx)
Get the name of the variable or null if no name available- Parameters:
idx- the wasm variable index- Returns:
- the name
-
getTempVariable
int getTempVariable(AnyType valueType, int startCodePosition, int endCodePosition)
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
protected TypeManager getTypeManager()
Get the type manager.- Returns:
- the type manager
-
reset
protected void reset(LocalVariableTable variableTable, MethodInfo method, java.util.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
private static int getPossibleSlot(@Nonnull WasmInstruction instr)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
protected void addGlobalInstruction(boolean load, Member ref, int javaCodePos, int lineNumber)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
protected void addConstInstruction(java.lang.Number value, ValueType valueType, int javaCodePos, int lineNumber)Add a constant instruction.- Parameters:
value- the valuevalueType- the value typejavaCodePos- the code position/offset in the Java methodlineNumber- the line number in the Java source code
-
addConstInstruction
protected void addConstInstruction(java.lang.Object value, int javaCodePos, int lineNumber)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
private static boolean isAscii(java.lang.String str)
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
private void addCallIndirectInstruction(WasmCallIndirectInstruction indirectCall)
Add indirect call to the instruction.- Parameters:
indirectCall- the instruction
-
addCallVirtualInstruction
protected void addCallVirtualInstruction(FunctionName name, int javaCodePos, int lineNumber)
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
protected void addCallInterfaceInstruction(FunctionName name, int javaCodePos, int lineNumber)
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 java.lang.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
private FunctionName getNonGC(java.lang.String name, int lineNumber)
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, java.lang.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 java.lang.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, java.lang.String factorySignature, java.lang.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
-
-