Class ModuleWriter
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.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 Summary
Fields Modifier and Type Field Description protected java.io.ByteArrayOutputStream
dataStream
The stream of the data section for constant data like strings and vtablesprotected WasmOptions
options
The compiler options.
-
Constructor Summary
Constructors Modifier Constructor Description protected
ModuleWriter(WasmOptions options)
Create a instance with its options.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description protected abstract void
markSourceLine(int javaSourceLine)
Mark the current output position with Java code position for crating of a source map.protected abstract void
prepareFinish()
Finish the prepare after all classes/methods are prepare.protected abstract void
prepareImport(FunctionName name, java.lang.String importModule, java.lang.String importName)
Prepare a imported single function in the prepare phase.protected abstract void
writeArrayOperator(ArrayOperator op, ArrayType type)
Write an array operation.protected abstract void
writeBlockCode(WasmBlockOperator op, java.lang.Object data)
Write a block/branch codeprotected abstract int
writeBlockType(TypeManager.BlockType type)
Write a block type.protected abstract void
writeCast(ValueTypeConvertion cast)
Cast a value from one type to anotherprotected abstract void
writeConst(java.lang.Number value, ValueType valueType)
Write a constant number valueprotected abstract void
writeDefaultValue(AnyType type)
Write the default/initial value for a type.protected abstract void
writeException()
Mark to write exceptionsprotected abstract void
writeExport(FunctionName name, java.lang.String exportName)
Write an export directiveprotected abstract void
writeFunctionCall(FunctionName name, java.lang.String comment)
Write a call to a function.protected abstract void
writeGlobalAccess(boolean load, FunctionName name, AnyType type)
Write a global variable operationprotected abstract void
writeLocal(VariableOperator op, int idx)
Write a local variable operation.protected abstract void
writeMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment)
Write a memory operation for the linear memory.protected abstract void
writeMethodFinish()
Complete the methodprotected abstract void
writeMethodParam(java.lang.String kind, AnyType valueType, java.lang.String name)
Write a method parameter.protected abstract void
writeMethodParamFinish(FunctionName name)
Finish the function parameter.protected abstract void
writeMethodParamStart(FunctionName name, FunctionType funcType)
Write the method header.protected abstract void
writeMethodStart(FunctionName name, java.lang.String sourceFile)
Start the writing of method/function code.protected abstract void
writeNumericOperator(NumericOperator numOp, ValueType valueType)
Write a add operatorprotected abstract void
writeStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx)
Write a struct operationprotected abstract int
writeStructType(TypeManager.StructType type)
Write a type/struct.protected abstract void
writeTable(boolean load, int idx)
Write a table operation.protected abstract void
writeVirtualFunctionCall(FunctionName name, AnyType type)
Write a function call to an instance function.
-
-
-
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 nameimportModule
- the import module name if it is a import functionimportName
- 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 nameexportName
- 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 namefuncType
- 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 parametername
- 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 namesourceFile
- 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 valuevalueType
- 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 operationidx
- 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 GETname
- the variable nametype
- 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 operationvalueType
- 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 namecomment
- 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 nametype
- 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 operationdata
- 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 operationtype
- 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 operationtype
- the type of the structfieldName
- the fieldName if the operation is per fieldidx
- 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 operationvalueType
- the value type of the stack valueoffset
- 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
-
-