Class BinaryModuleWriter
java.lang.Object
de.inetsoftware.jwebassembly.module.ModuleWriter
de.inetsoftware.jwebassembly.binary.BinaryModuleWriter
- All Implemented Interfaces:
InstructionOpcodes, Closeable, AutoCloseable
Module Writer for binary format. http://webassembly.org/docs/binary-encoding/
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate TypeManager.StructTypeprivate WasmOutputStreamprivate final booleanprivate intprivate List<ExportEntry> private Functionprivate FunctionTypeEntryprivate Map<String, ImportFunction> private Stringprivate FunctionNameprivate TypeManager.StructTypeprivate WasmTargetprivate WasmOutputStreamprivate static final byte[]private static final intFields inherited from class ModuleWriter
dataStream, optionsFields inherited from interface 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
ConstructorsConstructorDescriptionBinaryModuleWriter(WasmTarget target, WasmOptions options) Create new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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 voidFinish the prepare after all classes/methods are prepare.protected voidprepareImport(FunctionName name, String importModule, 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, Object data) Write a block/branch codeprotected intWrite a block type.protected voidwriteCast(ValueTypeConvertion cast) Cast a value from one type to anotherprivate voidWrite the code section to the output.protected voidwriteConst(Number value, ValueType valueType) Write a constant number valueprivate voidWrite the data sectionprivate voidwriteDebugFunctionNames(Set<? extends Map.Entry<String, ? extends Function>> entries, WasmOutputStream section) Write function names to the custom "name" section.private voidWrite optional the debug names into the custom "name" section.private voidwriteDebugParameternNames(Set<? extends Map.Entry<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 voidWrite element section.protected voidMark to write exceptionsprotected voidwriteExport(FunctionName name, String exportName) Write an export directiveprotected voidwriteFunctionCall(FunctionName name, 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 voidWrite the memory section.protected voidComplete the methodprotected voidwriteMethodParam(String kind, AnyType valueType, String name) Write a method parameter.protected voidFinish the function parameter.protected voidwriteMethodParamStart(FunctionName name, FunctionType funcType) Write the method header.protected voidwriteMethodStart(FunctionName name, String sourceFile) Start the writing of method/function code.protected voidwriteNumericOperator(NumericOperator numOp, ValueType valueType) Write a add operatorprivate voidWrite producer information to wasmprivate voidwriteSection(SectionType type, Collection<? extends SectionEntry> entries) Write a section with list format to the output.private voidWrite the source mapping urlprivate voidWrite a start section.protected voidwriteStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx) Write a struct operationprotected intWrite a type/struct.protected voidwriteTable(boolean load, int idx) Write a table operation.private voidWrite the table section.private voidWrite the tag section if needed.protected voidwriteVirtualFunctionCall(FunctionName name, AnyType type) Write a function call to an instance function.
-
Field Details
-
WASM_BINARY_MAGIC
private static final byte[] WASM_BINARY_MAGIC -
WASM_BINARY_VERSION
private static final int WASM_BINARY_VERSION- See Also:
-
target
-
wasm
-
createSourceMap
private final boolean createSourceMap -
codeStream
-
functionTypes
-
functions
-
locals
-
globals
-
exports
-
imports
-
abstracts
-
function
-
functionType
-
exceptionSignatureIndex
private int exceptionSignatureIndex -
javaSourceFile
-
callIndirect
private boolean callIndirect -
startFunction
-
stringType
-
classType
-
-
Constructor Details
-
BinaryModuleWriter
Create new instance.- Parameters:
target- the target for the module data.options- compiler properties- Throws:
IOException- if any I/O error occur
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
writeSection
private void writeSection(SectionType type, Collection<? extends SectionEntry> entries) throws IOException Write a section with list format to the output.- Parameters:
type- the type of the sectionentries- the entries of the section- Throws:
IOException- if any I/O error occur
-
writeTableSection
Write the table section. It declare the space for the element section.- Throws:
IOException- if any I/O error occur
-
writeMemorySection
Write the memory section.- Throws:
IOException- if any I/O error occur
-
writeTagSection
Write the tag section if needed.- Throws:
IOException- if any I/O error occur
-
writeStartSection
Write a start section. The id of the function that should be automatically executed.- Throws:
IOException- if any I/O error occur
-
writeElementSection
Write element section. This section create a matching between direct and indirect function call IDs.- Throws:
IOException- if any I/O error occur
-
writeCodeSection
Write the code section to the output. This section contains the byte code.- Throws:
IOException- if any I/O error occur
-
writeDataSection
Write the data section- Throws:
IOException- if any I/O error occur
-
writeDebugNames
Write optional the debug names into the custom "name" section.- Throws:
IOException- if any I/O error occur
-
writeDebugFunctionNames
private void writeDebugFunctionNames(Set<? extends Map.Entry<String, ? extends Function>> entries, WasmOutputStream section) throws IOException Write function names to the custom "name" section.- Parameters:
entries- the functionssection- the target- Throws:
IOException- if any I/O error occur
-
writeDebugParameternNames
private void writeDebugParameternNames(Set<? extends Map.Entry<String, ? extends Function>> entries, WasmOutputStream section) throws IOException Write parameter names to the custom "name" section.- Parameters:
entries- the functionssection- the target- Throws:
IOException- if any I/O error occur
-
writeSourceMappingUrl
Write the source mapping url- Throws:
IOException- if any I/O error occur
-
writeProducersSection
Write producer information to wasm- Throws:
IOException- if any I/O error occur
-
writeStructType
Write a type/struct.- Specified by:
writeStructTypein 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:
writeBlockTypein classModuleWriter- Parameters:
type- the type- Returns:
- type ID
- Throws:
IOException- if any I/O error occur
-
writeException
Mark to write exceptions- Specified by:
writeExceptionin classModuleWriter- Throws:
IOException- if any I/O error occur
-
prepareImport
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
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:
IOException- if any I/O error occur
-
writeMethodParamStart
Write the method header.- Specified by:
writeMethodParamStartin 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:
writeMethodParamin 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
-
writeMethodParamFinish
Finish the function parameter.- Specified by:
writeMethodParamFinishin classModuleWriter- Parameters:
name- the function name- Throws:
IOException- if any I/O error occur
-
writeMethodStart
Start the writing of method/function code.- Specified by:
writeMethodStartin 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:
markSourceLinein classModuleWriter- Parameters:
javaSourceLine- the line number in the Java code
-
writeMethodFinish
Complete the method- Specified by:
writeMethodFinishin classModuleWriter- Throws:
IOException- if any I/O error occur
-
writeConst
Write a constant number value- Specified by:
writeConstin 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:
writeLocalin 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:
writeGlobalAccessin 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:
writeTablein 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:
writeDefaultValuein classModuleWriter- Parameters:
type- 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:
writeNumericOperatorin 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:
writeCastin classModuleWriter- Parameters:
cast- the operator- Throws:
IOException- if any I/O error occur
-
writeFunctionCall
Write a call to a function.- Specified by:
writeFunctionCallin classModuleWriter- Parameters:
name- the function namecomments- 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:
writeVirtualFunctionCallin classModuleWriter- Parameters:
name- the function nametype- the base type that should be called- Throws:
IOException- if any I/O error occur
-
getFunction
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 Object data) throws IOException Write a block/branch code- Specified by:
writeBlockCodein classModuleWriter- Parameters:
op- the operationdata- extra data depending of the operator- Throws:
IOException- if any I/O error occur
-
writeArrayOperator
Write an array operation.- Specified by:
writeArrayOperatorin 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:
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:
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:
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:
IOException- if any I/O error occur
-