Class TextModuleWriter
java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleWriter
de.inetsoftware.jwebassembly.text.TextModuleWriter
- All Implemented Interfaces:
Closeable
,AutoCloseable
Module Writer for text format with S-expressions.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private final StringBuilder
private int
private boolean
private StringBuilder
private final StringBuilder
private final WasmTarget
private final StringBuilder
private boolean
private boolean
private boolean
Fields inherited from class de.inetsoftware.jwebassembly.module.ModuleWriter
dataStream, options
-
Constructor Summary
ConstructorsConstructorDescriptionTextModuleWriter
(WasmTarget target, WasmOptions options) Create a new instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate CharSequence
blockWithResult
(String blockName, AnyType result) Create a the result type for a block instructionvoid
close()
private Function
getFunction
(FunctionName name) protected void
markSourceLine
(int javaSourceLine) Mark the current output position with Java code position for crating of a source map.private void
newline
(Appendable output) Add a newline with the insets.private String
normalizeName
(FunctionName name) Normalize the function name for the text formatprivate String
normalizeName
(String name) Normalize the function name for the text format of IDs.protected void
Finish the prepare after all classes/methods are prepare.protected void
prepareImport
(FunctionName name, String importModule, String importName) Prepare a imported single function in the prepare phase.protected void
writeArrayOperator
(ArrayOperator op, ArrayType type) Write an array operation.protected void
writeBlockCode
(WasmBlockOperator op, Object data) Write a block/branch codeprotected int
Write a block type.protected void
writeCast
(ValueTypeConvertion cast) Cast a value from one type to anotherprotected void
writeConst
(Number value, ValueType valueType) Write a constant number valueprotected void
writeDefaultValue
(AnyType type) Write the default/initial value for a type.private void
writeDefaultValue
(Appendable output, AnyType type) Write the default/initial value for type.protected void
Mark to write exceptionsprotected void
writeExport
(FunctionName name, String exportName) Write an export directiveprotected void
writeFunctionCall
(FunctionName name, String comment) Write a call to a function.protected void
writeGlobalAccess
(boolean load, FunctionName name, AnyType type) Write a global variable operationprotected void
writeLocal
(VariableOperator op, int idx) Write a local variable operation.protected void
writeMemoryOperator
(MemoryOperator memOp, ValueType valueType, int offset, int alignment) Write a memory operation for the linear memory.protected void
Complete the methodprotected void
writeMethodParam
(String kind, AnyType valueType, String name) Write a method parameter.protected void
Finish the function parameter.protected void
writeMethodParamStart
(FunctionName name, FunctionType funcType) Write the method header.protected void
writeMethodStart
(FunctionName name, String sourceFile) Start the writing of method/function code.protected void
writeNumericOperator
(NumericOperator numOp, ValueType valueType) Write a add operatorprivate void
writeParam
(StringBuilder output, String kind, AnyType valueType, String name) Write a parameter to the given outputprotected void
writeStructOperator
(StructOperator op, AnyType type, NamedStorageType fieldName, int idx) Write a struct operationprotected int
Write a type/struct.protected void
writeTable
(boolean load, int idx) Write a table operation.private void
writeTypeName
(Appendable output, AnyType type) Write the name of a type.protected void
writeVirtualFunctionCall
(FunctionName name, AnyType type) Write a function call to an instance function.
-
Field Details
-
target
-
output
-
methodParamNames
-
typeOutput
-
types
-
methodOutput
-
imports
-
functions
-
abstracts
-
functionNames
-
inset
private int inset -
isImport
private boolean isImport -
globals
-
useExceptions
private boolean useExceptions -
callIndirect
private boolean callIndirect -
useTypeString
private boolean useTypeString -
useTypeClass
private boolean useTypeClass
-
-
Constructor Details
-
TextModuleWriter
Create a new instance.- Parameters:
target
- target for the resultoptions
- compiler properties- Throws:
IOException
- if any I/O error occur
-
-
Method Details
-
close
- Throws:
IOException
-
writeStructType
Write a type/struct.- Specified by:
writeStructType
in classModuleWriter
- Parameters:
type
- the type to declare/write- Returns:
- type ID
- Throws:
IOException
- if any I/O error occur
-
writeBlockType
Write a block type.- Specified by:
writeBlockType
in classModuleWriter
- Parameters:
type
- the type- Returns:
- type ID
- Throws:
IOException
- if any I/O error occur
-
writeException
Mark to write exceptions- Specified by:
writeException
in classModuleWriter
- Throws:
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 classModuleWriter
-
prepareImport
protected void prepareImport(FunctionName name, String importModule, String importName) throws IOException Prepare a imported single function in the prepare phase.- Specified by:
prepareImport
in classModuleWriter
- 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
-
normalizeName
Normalize the function name for the text format- Parameters:
name
- the name- Returns:
- the normalized name
-
normalizeName
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
Write an export directive- Specified by:
writeExport
in classModuleWriter
- 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
-
writeTypeName
Write the name of a type.- Parameters:
output
- the targettype
- the type- Throws:
IOException
- if any I/O error occur
-
writeMethodParamStart
protected void writeMethodParamStart(@Nonnull FunctionName name, FunctionType funcType) throws IOException Write the method header.- Specified by:
writeMethodParamStart
in classModuleWriter
- Parameters:
name
- the function namefuncType
- 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 classModuleWriter
- 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
-
writeParam
private void writeParam(StringBuilder output, String kind, AnyType valueType, @Nullable String name) throws IOException Write a parameter to the given output- Parameters:
output
- the tragetkind
- "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.- Specified by:
writeMethodParamFinish
in classModuleWriter
- Parameters:
name
- the function name- Throws:
IOException
- if any I/O error occur
-
getFunction
-
writeMethodStart
Start the writing of method/function code.- Specified by:
writeMethodStart
in classModuleWriter
- Parameters:
name
- the function namesourceFile
- 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 classModuleWriter
- Parameters:
javaSourceLine
- the line number in the Java code
-
writeMethodFinish
Complete the method- Specified by:
writeMethodFinish
in classModuleWriter
- Throws:
IOException
- if any I/O error occur
-
writeConst
Write a constant number value- Specified by:
writeConst
in classModuleWriter
- Parameters:
value
- the valuevalueType
- the data type of the number- Throws:
IOException
- if any I/O error occur
-
writeLocal
Write a local variable operation.- Specified by:
writeLocal
in classModuleWriter
- Parameters:
op
- the operationidx
- the index of the parameter variable- Throws:
IOException
- if any I/O error occur
-
writeGlobalAccess
Write a global variable operation- Specified by:
writeGlobalAccess
in classModuleWriter
- 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.- Specified by:
writeTable
in classModuleWriter
- 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.- Specified by:
writeDefaultValue
in classModuleWriter
- Parameters:
type
- the type- Throws:
IOException
- if an I/O error occurs.
-
writeDefaultValue
Write the default/initial value for type.- Parameters:
output
- the targettype
- 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 classModuleWriter
- 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- Specified by:
writeCast
in classModuleWriter
- Parameters:
cast
- the operator- Throws:
IOException
- if any I/O error occur
-
newline
Add a newline with the insets.- Parameters:
output
- the target- Throws:
IOException
- if any I/O error occur
-
writeFunctionCall
Write a call to a function.- Specified by:
writeFunctionCall
in classModuleWriter
- Parameters:
name
- the function namecomment
- optional comment for the text format- Throws:
IOException
- if any I/O error occur
-
writeVirtualFunctionCall
Write a function call to an instance function. On the stack there must be the object.- Specified by:
writeVirtualFunctionCall
in classModuleWriter
- Parameters:
name
- the function nametype
- the base type that should be called- Throws:
IOException
- if any I/O error occur
-
writeBlockCode
protected void writeBlockCode(@Nonnull WasmBlockOperator op, @Nullable Object data) throws IOException Write a block/branch code- Specified by:
writeBlockCode
in classModuleWriter
- Parameters:
op
- the operationdata
- extra data depending of the operator- Throws:
IOException
- if any I/O error occur
-
blockWithResult
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:
IOException
- if any I/O error occur
-
writeArrayOperator
Write an array operation.- Specified by:
writeArrayOperator
in classModuleWriter
- Parameters:
op
- the operationtype
- 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 classModuleWriter
- 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 void writeMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment) throws IOException Write a memory operation for the linear memory.- Specified by:
writeMemoryOperator
in classModuleWriter
- 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
-