Class WasmCodeBuilder

java.lang.Object
de.inetsoftware.jwebassembly.module.WasmCodeBuilder
Direct Known Subclasses:
WatParser

public abstract class WasmCodeBuilder extends Object
Base class for Code Building.
Author:
Volker Berlin
  • Constructor Details

    • WasmCodeBuilder

      protected WasmCodeBuilder()
      Create a new instance of CodeBuilder
  • Method Details

    • getTypeManager

      protected TypeManager getTypeManager()
      Get the type manager.
      Returns:
      the type manager
    • 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 type
      load - true: if load
      javaIdx - the memory/slot index of the variable in Java byte code
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 operation
      wasmIdx - the index of the variable
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • addDupInstruction

      protected void addDupInstruction(boolean dup2, int javaCodePos, int lineNumber)
      Create a WasmDupInstruction.
      Parameters:
      dup2 - call from dup2 instruction
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 method
      lineNumber - 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 method
      lineNumber - 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 load
      ref - reference to a static field
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 load
      name - reference to a static field
      type - the type of the static field
      clinit - a reference to the class/static constructor which should executed before access a static field
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 load
      idx - the index of the table
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • addConstInstruction

      protected void addConstInstruction(Number value, ValueType valueType, int javaCodePos, int lineNumber)
      Add a constant instruction.
      Parameters:
      value - the value
      valueType - the value type
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • addConstInstruction

      protected void addConstInstruction(Object value, int javaCodePos, int lineNumber)
      Add a constant instruction with unknown value type.
      Parameters:
      value - the value
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • addNumericInstruction

      protected de.inetsoftware.jwebassembly.module.WasmNumericInstruction addNumericInstruction(@Nullable NumericOperator numOp, @Nullable ValueType valueType, int javaCodePos, int lineNumber)
      Add a numeric operation instruction
      Parameters:
      numOp - the operation
      valueType - the value type
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 conversion
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 called
      needThisParameter - true, if the hidden THIS parameter is needed, If it is an instance method call.
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • addCallVirtualInstruction

      protected void addCallVirtualInstruction(FunctionName name, int javaCodePos, int lineNumber)
      Add a virtual/method function call.
      Parameters:
      name - the function name that should be called
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 called
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 operation
      data - extra data for some operations
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 method
      lineNumber - 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 jump
      popCount - the the count of values that are removed from the stack.
      pushValueType - optional type of a push value
      javaCodePos - the code position/offset in the Java method
      lineNumber - the line number in the Java source code
    • 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 operation
      type - the array/component type of the array
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 >= 2
      typeName - the full type name
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 >= 2
      type - the full type
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 operation
      typeName - the type name like "java/lang/Object"
      fieldName - the name of field if needed for the operation
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 executed
      factorySignature - Get the signature of the factory method. For example "()Ljava.lang.Runnable;" for the lamba expression Runnable run = () -> foo();
      interfaceMethodName - The simple name of the generated method of the single function interface.
      javaCodePos - the code position/offset in the Java method
      lineNumber - 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 operation
      type - the type of the static field
      offset - the base offset which will be added to the offset value on the stack
      alignment - 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 method
      lineNumber - the line number in the Java source code