Class ModuleWriter
java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleWriter
- All Implemented Interfaces:
Closeable, AutoCloseable
- Direct Known Subclasses:
BinaryModuleWriter, TextModuleWriter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ByteArrayOutputStreamThe stream of the data section for constant data like strings and vtablesprotected final WasmOptionsThe compiler options. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedModuleWriter(WasmOptions options) Create a instance with its options. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidmarkSourceLine(int javaSourceLine) Mark the current output position with Java code position for crating of a source map.protected abstract voidFinish the prepare after all classes/methods are prepare.protected abstract voidprepareImport(FunctionName name, String importModule, String importName) Prepare a imported single function in the prepare phase.protected abstract voidwriteArrayOperator(ArrayOperator op, ArrayType type) Write an array operation.protected abstract voidwriteBlockCode(WasmBlockOperator op, Object data) Write a block/branch codeprotected abstract intWrite a block type.protected abstract voidwriteCast(ValueTypeConvertion cast) Cast a value from one type to anotherprotected abstract voidwriteConst(Number value, ValueType valueType) Write a constant number valueprotected abstract voidwriteDefaultValue(AnyType type) Write the default/initial value for a type.protected abstract voidMark to write exceptionsprotected abstract voidwriteExport(FunctionName name, String exportName) Write an export directiveprotected abstract voidwriteFunctionCall(FunctionName name, String comment) Write a call to a function.protected abstract voidwriteGlobalAccess(boolean load, FunctionName name, AnyType type) Write a global variable operationprotected abstract voidwriteLocal(VariableOperator op, int idx) Write a local variable operation.protected abstract voidwriteMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment) Write a memory operation for the linear memory.protected abstract voidComplete the methodprotected abstract voidwriteMethodParam(String kind, AnyType valueType, String name) Write a method parameter.protected abstract voidFinish the function parameter.protected abstract voidwriteMethodParamStart(FunctionName name, FunctionType funcType) Write the method header.protected abstract voidwriteMethodStart(FunctionName name, String sourceFile) Start the writing of method/function code.protected abstract voidwriteNumericOperator(NumericOperator numOp, ValueType valueType) Write a add operatorprotected abstract voidwriteStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx) Write a struct operationprotected abstract intWrite a type/struct.protected abstract voidwriteTable(boolean load, int idx) Write a table operation.protected abstract voidwriteVirtualFunctionCall(FunctionName name, AnyType type) Write a function call to an instance function.
-
Field Details
-
options
The compiler options. -
dataStream
The stream of the data section for constant data like strings and vtables
-
-
Constructor Details
-
ModuleWriter
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
Write a type/struct.- Parameters:
type- the type to declare/write- Returns:
- type ID
- Throws:
IOException- if any I/O error occur
-
writeBlockType
Write a block type.- Parameters:
type- the type- Returns:
- type ID
- Throws:
IOException- if any I/O error occur
-
writeException
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 nameimportModule- the import module name if it is a import functionimportName- the import name if it is a import function- Throws:
IOException- if any I/O error occur
-
writeExport
Write an export directive- Parameters:
name- the function nameexportName- 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 namefuncType- 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 parametername- optional name of the parameter- Throws:
IOException- if any I/O error occur
-
writeMethodParamFinish
Finish the function parameter.- Parameters:
name- the function name- Throws:
IOException- if any I/O error occur
-
writeMethodStart
Start the writing of method/function code.- Parameters:
name- the function namesourceFile- 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
Complete the method- Throws:
IOException- if any I/O error occur
-
writeConst
Write a constant number value- Parameters:
value- the valuevalueType- the data type of the number- Throws:
IOException- if any I/O error occur
-
writeLocal
Write a local variable operation.- Parameters:
op- the operationidx- 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 GETname- the variable nametype- the type of the variable- Throws:
IOException- if any I/O error occur
-
writeTable
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
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 operationvalueType- the type of the parameters- Throws:
IOException- if any I/O error occur
-
writeCast
Cast a value from one type to another- Parameters:
cast- the operator- Throws:
IOException- if any I/O error occur
-
writeFunctionCall
Write a call to a function.- Parameters:
name- the function namecomment- 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 nametype- 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 operationdata- 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 operationtype- 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 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:
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 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:
IOException- if any I/O error occur
-