Class BinaryModuleWriter
- java.lang.Object
-
- de.inetsoftware.jwebassembly.module.ModuleWriter
-
- de.inetsoftware.jwebassembly.binary.BinaryModuleWriter
-
- All Implemented Interfaces:
InstructionOpcodes,java.io.Closeable,java.lang.AutoCloseable
public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcodes
Module Writer for binary format. http://webassembly.org/docs/binary-encoding/
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,Function>abstractsprivate booleancallIndirectprivate TypeManager.StructTypeclassTypeprivate WasmOutputStreamcodeStreamprivate booleancreateSourceMapprivate intexceptionSignatureIndexprivate java.util.List<ExportEntry>exportsprivate Functionfunctionprivate java.util.Map<java.lang.String,Function>functionsprivate FunctionTypeEntryfunctionTypeprivate java.util.List<TypeEntry>functionTypesprivate java.util.Map<java.lang.String,Global>globalsprivate java.util.Map<java.lang.String,ImportFunction>importsprivate java.lang.StringjavaSourceFileprivate java.util.List<AnyType>localsprivate FunctionNamestartFunctionprivate TypeManager.StructTypestringTypeprivate WasmTargettargetprivate WasmOutputStreamwasmprivate static byte[]WASM_BINARY_MAGICprivate static intWASM_BINARY_VERSION-
Fields inherited from class de.inetsoftware.jwebassembly.module.ModuleWriter
dataStream, options
-
Fields inherited from interface de.inetsoftware.jwebassembly.binary.InstructionOpcodes
ARRAY_GET, ARRAY_GET_S, ARRAY_GET_U, ARRAY_LEN, ARRAY_NEW, ARRAY_NEW_DEFAULT, ARRAY_SET, BLOCK, BR, BR_IF, BR_ON_EXN, BR_ON_NULL, BR_TABLE, CALL, CALL_INDIRECT, CALL_REF, CATCH, DROP, ELSE, END, F32_ABS, F32_ADD, F32_CEIL, F32_CONST, F32_CONVERT_I32_S, F32_CONVERT_I32_U, F32_CONVERT_I64_S, F32_CONVERT_I64_U, F32_COPYSIGN, F32_DEMOTE_F64, F32_DIV, F32_EQ, F32_FLOOR, F32_GE, F32_GT, F32_LE, F32_LOAD, F32_LT, F32_MAX, F32_MIN, F32_MUL, F32_NE, F32_NEAREST, F32_NEG, F32_REINTERPRET_I32, F32_SQRT, F32_STORE, F32_SUB, F32_TRUNC, F64_ABS, F64_ADD, F64_CEIL, F64_CONST, F64_CONVERT_I32_S, F64_CONVERT_I32_U, F64_CONVERT_I64_S, F64_CONVERT_I64_U, F64_COPYSIGN, F64_DIV, F64_EQ, F64_FLOOR, F64_GE, F64_GT, F64_LE, F64_LOAD, F64_LT, F64_MAX, F64_MIN, F64_MUL, F64_NE, F64_NEAREST, F64_NEG, F64_PROMOTE_F32, F64_REINTERPRET_I64, F64_SQRT, F64_STORE, F64_SUB, F64_TRUNC, FUNC_BIND, GLOBAL_GET, GLOBAL_SET, I32_ADD, I32_AND, I32_CLZ, I32_CONST, I32_CTZ, I32_DIV_S, I32_DIV_U, I32_EQ, I32_EQZ, I32_EXTEND16_S, I32_EXTEND8_S, I32_GE_S, I32_GE_U, I32_GT_S, I32_GT_U, I32_LE_S, I32_LE_U, I32_LOAD, I32_LOAD16_S, I32_LOAD16_U, I32_LOAD8_S, I32_LOAD8_U, I32_LT_S, I32_LT_U, I32_MUL, I32_NE, I32_OR, I32_POPCNT, I32_REINTERPRET_F32, I32_REM_S, I32_REM_U, I32_ROTL, I32_ROTR, I32_SHL, I32_SHR_S, I32_SHR_U, I32_STORE, I32_STORE16, I32_STORE8, I32_SUB, I32_TRUNC_F32_S, I32_TRUNC_F32_U, I32_TRUNC_F64_S, I32_TRUNC_F64_U, I32_TRUNC_SAT_F32_S, I32_TRUNC_SAT_F32_U, I32_TRUNC_SAT_F64_S, I32_TRUNC_SAT_F64_U, I32_WRAP_I64, I32_XOR, I54_STORE16, I54_STORE32, I54_STORE8, I64_ADD, I64_AND, I64_CLZ, I64_CONST, I64_CTZ, I64_DIV_S, I64_DIV_U, I64_EQ, I64_EQZ, I64_EXTEND_I32_S, I64_EXTEND_I32_U, I64_EXTEND16_S, I64_EXTEND32_S, I64_EXTEND8_S, I64_GE_S, I64_GE_U, I64_GT_S, I64_GT_U, I64_LE_S, I64_LE_U, I64_LOAD, I64_LOAD16_S, I64_LOAD16_U, I64_LOAD32_S, I64_LOAD32_U, I64_LOAD8_S, I64_LOAD8_U, I64_LT_S, I64_LT_U, I64_MUL, I64_NE, I64_OR, I64_POPCNT, I64_REINTERPRET_F64, I64_REM_S, I64_REM_U, I64_ROTL, I64_ROTR, I64_SHL, I64_SHR_S, I64_SHR_U, I64_STORE, I64_SUB, I64_TRUNC_F32_S, I64_TRUNC_F32_U, I64_TRUNC_F64_S, I64_TRUNC_F64_U, I64_TRUNC_SAT_F32_S, I64_TRUNC_SAT_F32_U, I64_TRUNC_SAT_F64_S, I64_TRUNC_SAT_F64_U, I64_XOR, IF, LET, LOCAL_GET, LOCAL_SET, LOCAL_TEE, LOOP, MEMORY_GROW, MEMORY_SIZE, NOP, REF_AS_NON_NULL, REF_CAST, REF_EQ, REF_ISNULL, REF_NULL, RETHROW, RETURN, RETURN_CALL, RETURN_CALL_INDIRECT, RETURN_CALL_REF, RTT_CANON, SELECT, STRUCT_GET, STRUCT_NEW, STRUCT_NEW_DEFAULT, STRUCT_SET, TABLE_FILL, TABLE_GET, TABLE_GROW, TABLE_SET, TABLE_SIZE, THROW, TRY, UNREACHABLE
-
-
Constructor Summary
Constructors Constructor Description BinaryModuleWriter(WasmTarget target, WasmOptions options)Create new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()private FunctiongetFunction(FunctionName name)Get the function object for the name.protected voidmarkSourceLine(int javaSourceLine)Mark the current output position with Java code position for crating of a source map.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 anotherprivate voidwriteCodeSection()Write the code section to the output.protected voidwriteConst(java.lang.Number value, ValueType valueType)Write a constant number valueprivate voidwriteDataSection()Write the data sectionprivate voidwriteDebugFunctionNames(java.util.Set<? extends java.util.Map.Entry<java.lang.String,? extends Function>> entries, WasmOutputStream section)Write function names to the custom "name" section.private voidwriteDebugNames()Write optional the debug names into the custom "name" section.private voidwriteDebugParameternNames(java.util.Set<? extends java.util.Map.Entry<java.lang.String,? extends Function>> entries, WasmOutputStream section)Write parameter names to the custom "name" section.protected voidwriteDefaultValue(AnyType type)Write the default/initial value for a type.private voidwriteElementSection()Write element section.protected voidwriteException()Mark to write exceptionsprotected voidwriteExport(FunctionName name, java.lang.String exportName)Write an export directiveprotected voidwriteFunctionCall(FunctionName name, java.lang.String comments)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.private voidwriteMemorySection()Write the memory section.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 voidwriteProducersSection()Write producer information to wasmprivate voidwriteSection(SectionType type, java.util.Collection<? extends SectionEntry> entries)Write a section with list format to the output.private voidwriteSourceMappingUrl()Write the source mapping urlprivate voidwriteStartSection()Write a start section.protected 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 voidwriteTableSection()Write the table section.private voidwriteTagSection()Write the tag section if needed.protected voidwriteVirtualFunctionCall(FunctionName name, AnyType type)Write a function call to an instance function.
-
-
-
Field Detail
-
WASM_BINARY_MAGIC
private static final byte[] WASM_BINARY_MAGIC
-
WASM_BINARY_VERSION
private static final int WASM_BINARY_VERSION
- See Also:
- Constant Field Values
-
target
private WasmTarget target
-
wasm
private WasmOutputStream wasm
-
createSourceMap
private final boolean createSourceMap
-
codeStream
private WasmOutputStream codeStream
-
functionTypes
private java.util.List<TypeEntry> functionTypes
-
functions
private java.util.Map<java.lang.String,Function> functions
-
locals
private java.util.List<AnyType> locals
-
globals
private java.util.Map<java.lang.String,Global> globals
-
exports
private java.util.List<ExportEntry> exports
-
imports
private java.util.Map<java.lang.String,ImportFunction> imports
-
abstracts
private java.util.Map<java.lang.String,Function> abstracts
-
function
private Function function
-
functionType
private FunctionTypeEntry functionType
-
exceptionSignatureIndex
private int exceptionSignatureIndex
-
javaSourceFile
private java.lang.String javaSourceFile
-
callIndirect
private boolean callIndirect
-
startFunction
private FunctionName startFunction
-
stringType
private TypeManager.StructType stringType
-
classType
private TypeManager.StructType classType
-
-
Constructor Detail
-
BinaryModuleWriter
public BinaryModuleWriter(WasmTarget target, WasmOptions options) throws java.io.IOException
Create new instance.- Parameters:
target- the target for the module data.options- compiler properties- Throws:
java.io.IOException- if any I/O error occur
-
-
Method Detail
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
writeSection
private void writeSection(SectionType type, java.util.Collection<? extends SectionEntry> entries) throws java.io.IOException
Write a section with list format to the output.- Parameters:
type- the type of the sectionentries- the entries of the section- Throws:
java.io.IOException- if any I/O error occur
-
writeTableSection
private void writeTableSection() throws java.io.IOExceptionWrite the table section. It declare the space for the element section.- Throws:
java.io.IOException- if any I/O error occur
-
writeMemorySection
private void writeMemorySection() throws java.io.IOExceptionWrite the memory section.- Throws:
java.io.IOException- if any I/O error occur
-
writeTagSection
private void writeTagSection() throws java.io.IOExceptionWrite the tag section if needed.- Throws:
java.io.IOException- if any I/O error occur
-
writeStartSection
private void writeStartSection() throws java.io.IOExceptionWrite a start section. The id of the function that should be automatically executed.- Throws:
java.io.IOException- if any I/O error occur
-
writeElementSection
private void writeElementSection() throws java.io.IOExceptionWrite element section. This section create a matching between direct and indirect function call IDs.- Throws:
java.io.IOException- if any I/O error occur
-
writeCodeSection
private void writeCodeSection() throws java.io.IOExceptionWrite the code section to the output. This section contains the byte code.- Throws:
java.io.IOException- if any I/O error occur
-
writeDataSection
private void writeDataSection() throws java.io.IOExceptionWrite the data section- Throws:
java.io.IOException- if any I/O error occur
-
writeDebugNames
private void writeDebugNames() throws java.io.IOExceptionWrite optional the debug names into the custom "name" section.- Throws:
java.io.IOException- if any I/O error occur
-
writeDebugFunctionNames
private void writeDebugFunctionNames(java.util.Set<? extends java.util.Map.Entry<java.lang.String,? extends Function>> entries, WasmOutputStream section) throws java.io.IOException
Write function names to the custom "name" section.- Parameters:
entries- the functionssection- the target- Throws:
java.io.IOException- if any I/O error occur
-
writeDebugParameternNames
private void writeDebugParameternNames(java.util.Set<? extends java.util.Map.Entry<java.lang.String,? extends Function>> entries, WasmOutputStream section) throws java.io.IOException
Write parameter names to the custom "name" section.- Parameters:
entries- the functionssection- the target- Throws:
java.io.IOException- if any I/O error occur
-
writeSourceMappingUrl
private void writeSourceMappingUrl() throws java.io.IOExceptionWrite the source mapping url- Throws:
java.io.IOException- if any I/O error occur
-
writeProducersSection
private void writeProducersSection() throws java.io.IOExceptionWrite producer information to wasm- Throws:
java.io.IOException- if any I/O error occur
-
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
-
prepareImport
protected void prepareImport(FunctionName name, java.lang.String importModule, java.lang.String importName)
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
-
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
-
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
-
writeMethodParamStart
protected void writeMethodParamStart(FunctionName name, FunctionType funcType) throws java.io.IOException
Write 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
-
writeMethodParamFinish
protected void writeMethodParamFinish(FunctionName name) throws java.io.IOException
Finish the function parameter.- Specified by:
writeMethodParamFinishin classModuleWriter- 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:
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.
-
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
-
writeFunctionCall
protected void writeFunctionCall(FunctionName name, java.lang.String comments) throws java.io.IOException
Write a call to a function.- Specified by:
writeFunctionCallin classModuleWriter- Parameters:
name- the function namecomments- 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
-
getFunction
@Nonnull private Function getFunction(FunctionName name)
Get the function object for the name. If not exists then it will be created.- Parameters:
name- the function name- Returns:
- the function object
-
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
-
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
-
-