Class BinaryModuleWriter

java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleWriter
de.inetsoftware.jwebassembly.binary.BinaryModuleWriter
All Implemented Interfaces:
InstructionOpcodes, Closeable, AutoCloseable

public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcodes
Module Writer for binary format. http://webassembly.org/docs/binary-encoding/
  • Field Details

  • Constructor Details

    • BinaryModuleWriter

      public BinaryModuleWriter(WasmTarget target, WasmOptions options) throws IOException
      Create new instance.
      Parameters:
      target - the target for the module data.
      options - compiler properties
      Throws:
      IOException - if any I/O error occur
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • writeSection

      private void writeSection(SectionType type, Collection<? extends SectionEntry> entries) throws IOException
      Write a section with list format to the output.
      Parameters:
      type - the type of the section
      entries - the entries of the section
      Throws:
      IOException - if any I/O error occur
    • writeTableSection

      private void writeTableSection() throws IOException
      Write the table section. It declare the space for the element section.
      Throws:
      IOException - if any I/O error occur
    • writeMemorySection

      private void writeMemorySection() throws IOException
      Write the memory section.
      Throws:
      IOException - if any I/O error occur
    • writeTagSection

      private void writeTagSection() throws IOException
      Write the tag section if needed.
      Throws:
      IOException - if any I/O error occur
    • writeStartSection

      private void writeStartSection() throws IOException
      Write a start section. The id of the function that should be automatically executed.
      Throws:
      IOException - if any I/O error occur
    • writeElementSection

      private void writeElementSection() throws IOException
      Write element section. This section create a matching between direct and indirect function call IDs.
      Throws:
      IOException - if any I/O error occur
    • writeCodeSection

      private void writeCodeSection() throws IOException
      Write the code section to the output. This section contains the byte code.
      Throws:
      IOException - if any I/O error occur
    • writeDataSection

      private void writeDataSection() throws IOException
      Write the data section
      Throws:
      IOException - if any I/O error occur
    • writeDebugNames

      private void writeDebugNames() throws IOException
      Write optional the debug names into the custom "name" section.
      Throws:
      IOException - if any I/O error occur
    • writeDebugFunctionNames

      private void writeDebugFunctionNames(Set<? extends Map.Entry<String,? extends Function>> entries, WasmOutputStream section) throws IOException
      Write function names to the custom "name" section.
      Parameters:
      entries - the functions
      section - the target
      Throws:
      IOException - if any I/O error occur
    • writeDebugParameternNames

      private void writeDebugParameternNames(Set<? extends Map.Entry<String,? extends Function>> entries, WasmOutputStream section) throws IOException
      Write parameter names to the custom "name" section.
      Parameters:
      entries - the functions
      section - the target
      Throws:
      IOException - if any I/O error occur
    • writeSourceMappingUrl

      private void writeSourceMappingUrl() throws IOException
      Write the source mapping url
      Throws:
      IOException - if any I/O error occur
    • writeProducersSection

      private void writeProducersSection() throws IOException
      Write producer information to wasm
      Throws:
      IOException - if any I/O error occur
    • writeStructType

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

      protected int writeBlockType(TypeManager.BlockType type) throws IOException
      Write a block type.
      Specified by:
      writeBlockType in class ModuleWriter
      Parameters:
      type - the type
      Returns:
      type ID
      Throws:
      IOException - if any I/O error occur
    • writeException

      protected void writeException() throws IOException
      Mark to write exceptions
      Specified by:
      writeException in class ModuleWriter
      Throws:
      IOException - if any I/O error occur
    • prepareImport

      protected void prepareImport(FunctionName name, String importModule, String importName)
      Prepare a imported single function in the prepare phase.
      Specified by:
      prepareImport in class ModuleWriter
      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
    • prepareFinish

      protected void prepareFinish()
      Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the first method.
      Specified by:
      prepareFinish in class ModuleWriter
    • writeExport

      protected void writeExport(FunctionName name, String exportName) throws IOException
      Write an export directive
      Specified by:
      writeExport in class ModuleWriter
      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 void writeMethodParamStart(FunctionName name, FunctionType funcType) throws IOException
      Write the method header.
      Specified by:
      writeMethodParamStart in class ModuleWriter
      Parameters:
      name - the function name
      funcType - the type of function
      Throws:
      IOException - if any I/O error occur
    • writeMethodParam

      protected void writeMethodParam(String kind, AnyType valueType, @Nullable String name) throws IOException
      Write a method parameter.
      Specified by:
      writeMethodParam in class ModuleWriter
      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 void writeMethodParamFinish(FunctionName name) throws IOException
      Finish the function parameter.
      Specified by:
      writeMethodParamFinish in class ModuleWriter
      Parameters:
      name - the function name
      Throws:
      IOException - if any I/O error occur
    • writeMethodStart

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

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

      protected void writeMethodFinish() throws IOException
      Complete the method
      Specified by:
      writeMethodFinish in class ModuleWriter
      Throws:
      IOException - if any I/O error occur
    • writeConst

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

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

      protected void writeGlobalAccess(boolean load, FunctionName name, AnyType type) throws IOException
      Write a global variable operation
      Specified by:
      writeGlobalAccess in class ModuleWriter
      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 void writeTable(boolean load, @Nonnegative int idx) throws IOException
      Write a table operation.
      Specified by:
      writeTable in class ModuleWriter
      Parameters:
      load - true: if "get" else "set"
      idx - the index of the table
      Throws:
      IOException - if any I/O error occur
    • writeDefaultValue

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

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

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

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

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

      @Nonnull private Function getFunction(FunctionName name)
      Get the function object for the name. If not exists then it will be created.
      Parameters:
      name - the function name
      Returns:
      the function object
    • writeBlockCode

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

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

      protected void writeStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx) throws IOException
      Write a struct operation
      Specified by:
      writeStructOperator in class ModuleWriter
      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 void writeMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment) throws IOException
      Write a memory operation for the linear memory.
      Specified by:
      writeMemoryOperator in class ModuleWriter
      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