Class TextModuleWriter
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.ModuleWriter
-
- de.inetsoftware.jwebassembly.text.TextModuleWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class TextModuleWriter extends ModuleWriter
Module Writer for text format with S-expressions.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,Function>abstractsprivate booleancallIndirectprivate java.util.Set<java.lang.String>functionNamesprivate java.util.Map<java.lang.String,Function>functionsprivate java.util.HashMap<java.lang.String,AnyType>globalsprivate java.lang.StringBuilderimportsprivate intinsetprivate booleanisImportprivate java.lang.StringBuildermethodOutputprivate java.util.ArrayList<java.lang.String>methodParamNamesprivate java.lang.StringBuilderoutputprivate WasmTargettargetprivate java.lang.StringBuildertypeOutputprivate java.util.ArrayList<java.lang.String>typesprivate booleanuseExceptionsprivate booleanuseTypeClassprivate booleanuseTypeString-
Fields inherited from class de.inetsoftware.jwebassembly.module.ModuleWriter
dataStream, options
-
-
Constructor Summary
Constructors Constructor Description TextModuleWriter(WasmTarget target, WasmOptions options)Create a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private java.lang.CharSequenceblockWithResult(java.lang.String blockName, AnyType result)Create a the result type for a block instructionvoidclose()private FunctiongetFunction(FunctionName name)protected voidmarkSourceLine(int javaSourceLine)Mark the current output position with Java code position for crating of a source map.private voidnewline(java.lang.Appendable output)Add a newline with the insets.private java.lang.StringnormalizeName(FunctionName name)Normalize the function name for the text formatprivate java.lang.StringnormalizeName(java.lang.String name)Normalize the function name for the text format of IDs.protected voidprepareFinish()Finish the prepare after all classes/methods are prepare.protected voidprepareImport(FunctionName name, java.lang.String importModule, java.lang.String importName)Prepare a imported single function in the prepare phase.protected voidwriteArrayOperator(ArrayOperator op, ArrayType type)Write an array operation.protected voidwriteBlockCode(WasmBlockOperator op, java.lang.Object data)Write a block/branch codeprotected intwriteBlockType(TypeManager.BlockType type)Write a block type.protected voidwriteCast(ValueTypeConvertion cast)Cast a value from one type to anotherprotected voidwriteConst(java.lang.Number value, ValueType valueType)Write a constant number valueprotected voidwriteDefaultValue(AnyType type)Write the default/initial value for a type.private voidwriteDefaultValue(java.lang.Appendable output, AnyType type)Write the default/initial value for type.protected voidwriteException()Mark to write exceptionsprotected voidwriteExport(FunctionName name, java.lang.String exportName)Write an export directiveprotected voidwriteFunctionCall(FunctionName name, java.lang.String comment)Write a call to a function.protected voidwriteGlobalAccess(boolean load, FunctionName name, AnyType type)Write a global variable operationprotected voidwriteLocal(VariableOperator op, int idx)Write a local variable operation.protected voidwriteMemoryOperator(MemoryOperator memOp, ValueType valueType, int offset, int alignment)Write a memory operation for the linear memory.protected voidwriteMethodFinish()Complete the methodprotected voidwriteMethodParam(java.lang.String kind, AnyType valueType, java.lang.String name)Write a method parameter.protected voidwriteMethodParamFinish(FunctionName name)Finish the function parameter.protected voidwriteMethodParamStart(FunctionName name, FunctionType funcType)Write the method header.protected voidwriteMethodStart(FunctionName name, java.lang.String sourceFile)Start the writing of method/function code.protected voidwriteNumericOperator(NumericOperator numOp, ValueType valueType)Write a add operatorprivate voidwriteParam(java.lang.StringBuilder output, java.lang.String kind, AnyType valueType, java.lang.String name)Write a parameter to the given outputprotected voidwriteStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx)Write a struct operationprotected intwriteStructType(TypeManager.StructType type)Write a type/struct.protected voidwriteTable(boolean load, int idx)Write a table operation.private voidwriteTypeName(java.lang.Appendable output, AnyType type)Write the name of a type.protected voidwriteVirtualFunctionCall(FunctionName name, AnyType type)Write a function call to an instance function.
-
-
-
Field Detail
-
target
private final WasmTarget target
-
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 resultoptions- 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:
writeStructTypein classModuleWriter- 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:
writeBlockTypein classModuleWriter- Parameters:
type- the type- Returns:
- type ID
- Throws:
java.io.IOException- if any I/O error occur
-
writeException
protected void writeException() throws java.io.IOExceptionMark to write exceptions- Specified by:
writeExceptionin classModuleWriter- 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:
prepareFinishin classModuleWriter
-
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:
prepareImportin 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:
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:
writeExportin classModuleWriter- 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
-
writeTypeName
private void writeTypeName(java.lang.Appendable output, AnyType type) throws java.io.IOExceptionWrite the name of a type.- Parameters:
output- the targettype- the type- Throws:
java.io.IOException- if any I/O error occur
-
writeMethodParamStart
protected void writeMethodParamStart(@Nonnull FunctionName name, FunctionType funcType) throws java.io.IOExceptionWrite the method header.- Specified by:
writeMethodParamStartin classModuleWriter- Parameters:
name- the function namefuncType- 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.IOExceptionWrite a method parameter.- Specified by:
writeMethodParamin classModuleWriter- 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
-
writeParam
private void writeParam(java.lang.StringBuilder output, java.lang.String kind, AnyType valueType, @Nullable java.lang.String name) throws java.io.IOExceptionWrite 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:
java.io.IOException- if any I/O error occur
-
writeMethodParamFinish
protected void writeMethodParamFinish(@Nonnull FunctionName name) throws java.io.IOExceptionFinish the function parameter.- Specified by:
writeMethodParamFinishin classModuleWriter- Parameters:
name- the function name- Throws:
java.io.IOException- if any I/O error occur
-
getFunction
private Function getFunction(FunctionName name)
-
writeMethodStart
protected void writeMethodStart(FunctionName name, java.lang.String sourceFile) throws java.io.IOException
Start the writing of method/function code.- Specified by:
writeMethodStartin classModuleWriter- Parameters:
name- the function namesourceFile- 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:
markSourceLinein classModuleWriter- Parameters:
javaSourceLine- the line number in the Java code
-
writeMethodFinish
protected void writeMethodFinish() throws java.io.IOExceptionComplete the method- Specified by:
writeMethodFinishin classModuleWriter- Throws:
java.io.IOException- if any I/O error occur
-
writeConst
protected void writeConst(java.lang.Number value, ValueType valueType) throws java.io.IOExceptionWrite a constant number value- Specified by:
writeConstin classModuleWriter- Parameters:
value- the valuevalueType- 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:
writeLocalin classModuleWriter- Parameters:
op- the operationidx- 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.IOExceptionWrite a global variable operation- Specified by:
writeGlobalAccessin classModuleWriter- 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 void writeTable(boolean load, @Nonnegative int idx) throws java.io.IOExceptionWrite a table operation.- Specified by:
writeTablein classModuleWriter- 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:
writeDefaultValuein classModuleWriter- 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.IOExceptionWrite the default/initial value for type.- Parameters:
output- the targettype- 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:
writeNumericOperatorin classModuleWriter- Parameters:
numOp- the numeric operationvalueType- 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:
writeCastin classModuleWriter- 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.IOExceptionAdd 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:
writeFunctionCallin classModuleWriter- Parameters:
name- the function namecomment- 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:
writeVirtualFunctionCallin classModuleWriter- Parameters:
name- the function nametype- 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.IOExceptionWrite a block/branch code- Specified by:
writeBlockCodein classModuleWriter- Parameters:
op- the operationdata- 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.IOExceptionCreate 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.IOExceptionWrite an array operation.- Specified by:
writeArrayOperatorin classModuleWriter- Parameters:
op- the operationtype- 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:
writeStructOperatorin 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:
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:
writeMemoryOperatorin 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:
java.io.IOException- if any I/O error occur
-
-