Class WasmCodeBuilder

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

public abstract class WasmCodeBuilder extends Object
Base class for Code Building.
  • Field Details

  • 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

      void init(WasmOptions options, ClassFileLoader classFileLoader)
      Initialize the code builder;
      Parameters:
      options - compiler properties
      classFileLoader - for loading the class files
    • getInstructions

      @Nonnull 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

      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

      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 variable
      startCodePosition - the start of the Java code position
      endCodePosition - 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, 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
    • 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 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
    • isAscii

      private static boolean isAscii(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 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
    • 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 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
    • getNonGC

      private FunctionName getNonGC(String name, int lineNumber)
      Get a non GC polyfill function.
      Parameters:
      name - the function name
      lineNumber - 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 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