Class ModuleWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    BinaryModuleWriter, TextModuleWriter

    public abstract class ModuleWriter
    extends java.lang.Object
    implements java.io.Closeable
    Module Writer base class.
    • Field Detail

      • options

        protected final WasmOptions options
        The compiler options.
      • dataStream

        protected final java.io.ByteArrayOutputStream dataStream
        The stream of the data section for constant data like strings and vtables
    • Constructor Detail

      • ModuleWriter

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

      • 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 java.io.IOException
        Write a type/struct.
        Parameters:
        type - the type to declare/write
        Returns:
        type ID
        Throws:
        java.io.IOException - if any I/O error occur
      • writeBlockType

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

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

        protected abstract void prepareImport​(FunctionName name,
                                              java.lang.String importModule,
                                              java.lang.String importName)
                                       throws java.io.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:
        java.io.IOException - if any I/O error occur
      • writeExport

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

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

        protected abstract void writeMethodParam​(java.lang.String kind,
                                                 AnyType valueType,
                                                 @Nullable
                                                 java.lang.String name)
                                          throws java.io.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:
        java.io.IOException - if any I/O error occur
      • writeMethodParamFinish

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

        protected abstract void writeMethodStart​(FunctionName name,
                                                 java.lang.String sourceFile)
                                          throws java.io.IOException
        Start the writing of method/function code.
        Parameters:
        name - the function name
        sourceFile - the name of the source file
        Throws:
        java.io.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 java.io.IOException
        Complete the method
        Throws:
        java.io.IOException - if any I/O error occur
      • writeConst

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

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

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

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

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

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

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

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

        protected abstract void writeVirtualFunctionCall​(FunctionName name,
                                                         AnyType type)
                                                  throws java.io.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:
        java.io.IOException - if any I/O error occur
      • writeBlockCode

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

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

        protected abstract void writeStructOperator​(StructOperator op,
                                                    AnyType type,
                                                    NamedStorageType fieldName,
                                                    int idx)
                                             throws java.io.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:
        java.io.IOException - if any I/O error occur
      • writeMemoryOperator

        protected abstract void writeMemoryOperator​(MemoryOperator memOp,
                                                    ValueType valueType,
                                                    int offset,
                                                    int alignment)
                                             throws java.io.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:
        java.io.IOException - if any I/O error occur