Class ModuleWriter

java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleWriter
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
BinaryModuleWriter, TextModuleWriter

public abstract class ModuleWriter extends Object implements Closeable
Module Writer base class.
  • Field Details

    • options

      protected final WasmOptions options
      The compiler options.
    • dataStream

      protected final ByteArrayOutputStream dataStream
      The stream of the data section for constant data like strings and vtables
  • Constructor Details

    • ModuleWriter

      protected ModuleWriter(WasmOptions options)
      Create a instance with its options.
      Parameters:
      options - the compiler options
  • Method Details

    • prepareFinish

      protected abstract void prepareFinish()
      Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the first method.
    • writeStructType

      protected abstract int writeStructType(@Nonnull TypeManager.StructType type) throws IOException
      Write a type/struct.
      Parameters:
      type - the type to declare/write
      Returns:
      type ID
      Throws:
      IOException - if any I/O error occur
    • writeBlockType

      protected abstract int writeBlockType(@Nonnull TypeManager.BlockType type) throws IOException
      Write a block type.
      Parameters:
      type - the type
      Returns:
      type ID
      Throws:
      IOException - if any I/O error occur
    • writeException

      protected abstract void writeException() throws IOException
      Mark to write exceptions
      Throws:
      IOException - if any I/O error occur
    • prepareImport

      protected abstract void prepareImport(FunctionName name, String importModule, String importName) throws IOException
      Prepare a imported single function in the prepare phase.
      Parameters:
      name - the function name
      importModule - the import module name if it is a import function
      importName - the import name if it is a import function
      Throws:
      IOException - if any I/O error occur
    • writeExport

      protected abstract void writeExport(FunctionName name, String exportName) throws IOException
      Write an export directive
      Parameters:
      name - the function name
      exportName - the export name, if null then the same like the method name
      Throws:
      IOException - if any I/O error occur
    • writeMethodParamStart

      protected abstract void writeMethodParamStart(@Nonnull FunctionName name, @Nonnull FunctionType funcType) throws IOException
      Write the method header.
      Parameters:
      name - the function name
      funcType - the type of function
      Throws:
      IOException - if any I/O error occur
    • writeMethodParam

      protected abstract void writeMethodParam(String kind, AnyType valueType, @Nullable String name) throws IOException
      Write a method parameter.
      Parameters:
      kind - "param", "result" or "local"
      valueType - the data type of the parameter
      name - optional name of the parameter
      Throws:
      IOException - if any I/O error occur
    • writeMethodParamFinish

      protected abstract void writeMethodParamFinish(@Nonnull FunctionName name) throws IOException
      Finish the function parameter.
      Parameters:
      name - the function name
      Throws:
      IOException - if any I/O error occur
    • writeMethodStart

      protected abstract void writeMethodStart(FunctionName name, String sourceFile) throws IOException
      Start the writing of method/function code.
      Parameters:
      name - the function name
      sourceFile - the name of the source file
      Throws:
      IOException - if any I/O error occur
    • markSourceLine

      protected abstract void markSourceLine(int javaSourceLine)
      Mark the current output position with Java code position for crating of a source map.
      Parameters:
      javaSourceLine - the line number in the Java code
    • writeMethodFinish

      protected abstract void writeMethodFinish() throws IOException
      Complete the method
      Throws:
      IOException - if any I/O error occur
    • writeConst

      protected abstract void writeConst(Number value, ValueType valueType) throws IOException
      Write a constant number value
      Parameters:
      value - the value
      valueType - the data type of the number
      Throws:
      IOException - if any I/O error occur
    • writeLocal

      protected abstract void writeLocal(VariableOperator op, int idx) throws IOException
      Write a local variable operation.
      Parameters:
      op - the operation
      idx - the index of the parameter variable
      Throws:
      IOException - if any I/O error occur
    • writeGlobalAccess

      protected abstract void writeGlobalAccess(boolean load, FunctionName name, AnyType type) throws IOException
      Write a global variable operation
      Parameters:
      load - true: if load or GET
      name - the variable name
      type - the type of the variable
      Throws:
      IOException - if any I/O error occur
    • writeTable

      protected abstract void writeTable(boolean load, @Nonnegative int idx) throws IOException
      Write a table operation.
      Parameters:
      load - true: if "get" else "set"
      idx - the index of the table
      Throws:
      IOException - if any I/O error occur
    • writeDefaultValue

      protected abstract void writeDefaultValue(AnyType type) throws IOException
      Write the default/initial value for a type.
      Parameters:
      type - the type
      Throws:
      IOException - if an I/O error occurs.
    • writeNumericOperator

      protected abstract void writeNumericOperator(NumericOperator numOp, @Nullable ValueType valueType) throws IOException
      Write a add operator
      Parameters:
      numOp - the numeric operation
      valueType - the type of the parameters
      Throws:
      IOException - if any I/O error occur
    • writeCast

      protected abstract void writeCast(ValueTypeConvertion cast) throws IOException
      Cast a value from one type to another
      Parameters:
      cast - the operator
      Throws:
      IOException - if any I/O error occur
    • writeFunctionCall

      protected abstract void writeFunctionCall(FunctionName name, String comment) throws IOException
      Write a call to a function.
      Parameters:
      name - the function name
      comment - optional comment for the text format
      Throws:
      IOException - if any I/O error occur
    • writeVirtualFunctionCall

      protected abstract void writeVirtualFunctionCall(FunctionName name, AnyType type) throws IOException
      Write a function call to an instance function. On the stack there must be the object.
      Parameters:
      name - the function name
      type - the base type that should be called
      Throws:
      IOException - if any I/O error occur
    • writeBlockCode

      protected abstract void writeBlockCode(@Nonnull WasmBlockOperator op, @Nullable Object data) throws IOException
      Write a block/branch code
      Parameters:
      op - the operation
      data - extra data depending of the operator
      Throws:
      IOException - if any I/O error occur
    • writeArrayOperator

      protected abstract void writeArrayOperator(@Nonnull ArrayOperator op, ArrayType type) throws IOException
      Write an array operation.
      Parameters:
      op - the operation
      type - the type of the array
      Throws:
      IOException - if any I/O error occur
    • writeStructOperator

      protected abstract void writeStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx) throws IOException
      Write a struct operation
      Parameters:
      op - the operation
      type - the type of the struct
      fieldName - the fieldName if the operation is per field
      idx - the index of the field if the operation is per field
      Throws:
      IOException - if any I/O error occur
    • writeMemoryOperator

      protected abstract void writeMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment) throws IOException
      Write a memory operation for the linear memory.
      Parameters:
      memOp - the memory operation
      valueType - the value type of the stack value
      offset - the offset into the memory. Should be ideally a factor of 4.
      alignment - the alignment of the value on the linear memory (0: 8 Bit; 1: 16 Bit; 2: 32 Bit)
      Throws:
      IOException - if any I/O error occur