Class TextModuleWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class TextModuleWriter
    extends ModuleWriter
    Module Writer for text format with S-expressions.
    • Field Detail

      • output

        private final java.lang.StringBuilder output
      • methodParamNames

        private final java.util.ArrayList<java.lang.String> methodParamNames
      • typeOutput

        private final java.lang.StringBuilder typeOutput
      • types

        private final java.util.ArrayList<java.lang.String> types
      • methodOutput

        private java.lang.StringBuilder methodOutput
      • imports

        private final java.lang.StringBuilder imports
      • functions

        private final java.util.Map<java.lang.String,​Function> functions
      • abstracts

        private final java.util.Map<java.lang.String,​Function> abstracts
      • functionNames

        private final java.util.Set<java.lang.String> functionNames
      • inset

        private int inset
      • isImport

        private boolean isImport
      • globals

        private final java.util.HashMap<java.lang.String,​AnyType> globals
      • useExceptions

        private boolean useExceptions
      • callIndirect

        private boolean callIndirect
      • useTypeString

        private boolean useTypeString
      • useTypeClass

        private boolean useTypeClass
    • Constructor Detail

      • TextModuleWriter

        public TextModuleWriter​(WasmTarget target,
                                WasmOptions options)
                         throws java.io.IOException
        Create a new instance.
        Parameters:
        target - target for the result
        options - compiler properties
        Throws:
        java.io.IOException - if any I/O error occur
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • writeStructType

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

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

        protected void writeException()
                               throws java.io.IOException
        Mark to write exceptions
        Specified by:
        writeException in class ModuleWriter
        Throws:
        java.io.IOException - if any I/O error occur
      • 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
      • prepareImport

        protected 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.
        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
        Throws:
        java.io.IOException - if any I/O error occur
      • normalizeName

        @Nonnull
        private java.lang.String normalizeName​(FunctionName name)
        Normalize the function name for the text format
        Parameters:
        name - the name
        Returns:
        the normalized name
      • normalizeName

        @Nonnull
        private java.lang.String normalizeName​(java.lang.String name)
        Normalize the function name for the text format of IDs. https://webassembly.github.io/spec/core/text/values.html#text-id
        Parameters:
        name - the name
        Returns:
        the normalized name
      • writeExport

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

        private void writeTypeName​(java.lang.Appendable output,
                                   AnyType type)
                            throws java.io.IOException
        Write the name of a type.
        Parameters:
        output - the target
        type - the type
        Throws:
        java.io.IOException - if any I/O error occur
      • writeMethodParamStart

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

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

        private void writeParam​(java.lang.StringBuilder output,
                                java.lang.String kind,
                                AnyType valueType,
                                @Nullable
                                java.lang.String name)
                         throws java.io.IOException
        Write a parameter to the given output
        Parameters:
        output - the traget
        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 void writeMethodParamFinish​(@Nonnull
                                              FunctionName name)
                                       throws java.io.IOException
        Finish the function parameter.
        Specified by:
        writeMethodParamFinish in class ModuleWriter
        Parameters:
        name - the function name
        Throws:
        java.io.IOException - if any I/O error occur
      • writeMethodStart

        protected void writeMethodStart​(FunctionName name,
                                        java.lang.String sourceFile)
                                 throws java.io.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:
        java.io.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 java.io.IOException
        Complete the method
        Specified by:
        writeMethodFinish in class ModuleWriter
        Throws:
        java.io.IOException - if any I/O error occur
      • writeConst

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

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

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

        protected void writeTable​(boolean load,
                                  @Nonnegative
                                  int idx)
                           throws java.io.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:
        java.io.IOException - if any I/O error occur
      • writeDefaultValue

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

        private void writeDefaultValue​(java.lang.Appendable output,
                                       AnyType type)
                                throws java.io.IOException
        Write the default/initial value for type.
        Parameters:
        output - the target
        type - the type
        Throws:
        java.io.IOException - if an I/O error occurs.
      • writeNumericOperator

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

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

        private void newline​(java.lang.Appendable output)
                      throws java.io.IOException
        Add a newline with the insets.
        Parameters:
        output - the target
        Throws:
        java.io.IOException - if any I/O error occur
      • writeFunctionCall

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

        protected 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.
        Specified by:
        writeVirtualFunctionCall in class ModuleWriter
        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 void writeBlockCode​(@Nonnull
                                      WasmBlockOperator op,
                                      @Nullable
                                      java.lang.Object data)
                               throws java.io.IOException
        Write a block/branch code
        Specified by:
        writeBlockCode in class ModuleWriter
        Parameters:
        op - the operation
        data - extra data depending of the operator
        Throws:
        java.io.IOException - if any I/O error occur
      • blockWithResult

        @Nonnull
        private java.lang.CharSequence blockWithResult​(java.lang.String blockName,
                                                       AnyType result)
                                                throws java.io.IOException
        Create a the result type for a block instruction
        Parameters:
        blockName - the name of the block for example "if" or "block"
        result - the result type of the block
        Returns:
        the block with result type
        Throws:
        java.io.IOException - if any I/O error occur
      • writeArrayOperator

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

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

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