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>
abstracts
private boolean
callIndirect
private TypeManager.StructType
classType
private WasmOutputStream
codeStream
private boolean
createSourceMap
private int
exceptionSignatureIndex
private java.util.List<ExportEntry>
exports
private Function
function
private java.util.Map<java.lang.String,Function>
functions
private FunctionTypeEntry
functionType
private java.util.List<TypeEntry>
functionTypes
private java.util.Map<java.lang.String,Global>
globals
private java.util.Map<java.lang.String,ImportFunction>
imports
private java.lang.String
javaSourceFile
private java.util.List<AnyType>
locals
private FunctionName
startFunction
private TypeManager.StructType
stringType
private WasmTarget
target
private WasmOutputStream
wasm
private static byte[]
WASM_BINARY_MAGIC
private static int
WASM_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 void
close()
private Function
getFunction(FunctionName name)
Get the function object for the name.protected void
markSourceLine(int javaSourceLine)
Mark the current output position with Java code position for crating of a source map.protected void
prepareFinish()
Finish the prepare after all classes/methods are prepare.protected void
prepareImport(FunctionName name, java.lang.String importModule, java.lang.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, java.lang.Object data)
Write a block/branch codeprotected int
writeBlockType(TypeManager.BlockType type)
Write a block type.protected void
writeCast(ValueTypeConvertion cast)
Cast a value from one type to anotherprivate void
writeCodeSection()
Write the code section to the output.protected void
writeConst(java.lang.Number value, ValueType valueType)
Write a constant number valueprivate void
writeDataSection()
Write the data sectionprivate void
writeDebugFunctionNames(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 void
writeDebugNames()
Write optional the debug names into the custom "name" section.private void
writeDebugParameternNames(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 void
writeDefaultValue(AnyType type)
Write the default/initial value for a type.private void
writeElementSection()
Write element section.protected void
writeException()
Mark to write exceptionsprotected void
writeExport(FunctionName name, java.lang.String exportName)
Write an export directiveprotected void
writeFunctionCall(FunctionName name, java.lang.String comments)
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.private void
writeMemorySection()
Write the memory section.protected void
writeMethodFinish()
Complete the methodprotected void
writeMethodParam(java.lang.String kind, AnyType valueType, java.lang.String name)
Write a method parameter.protected void
writeMethodParamFinish(FunctionName name)
Finish the function parameter.protected void
writeMethodParamStart(FunctionName name, FunctionType funcType)
Write the method header.protected void
writeMethodStart(FunctionName name, java.lang.String sourceFile)
Start the writing of method/function code.protected void
writeNumericOperator(NumericOperator numOp, ValueType valueType)
Write a add operatorprivate void
writeProducersSection()
Write producer information to wasmprivate void
writeSection(SectionType type, java.util.Collection<? extends SectionEntry> entries)
Write a section with list format to the output.private void
writeSourceMappingUrl()
Write the source mapping urlprivate void
writeStartSection()
Write a start section.protected void
writeStructOperator(StructOperator op, AnyType type, NamedStorageType fieldName, int idx)
Write a struct operationprotected int
writeStructType(TypeManager.StructType type)
Write a type/struct.protected void
writeTable(boolean load, int idx)
Write a table operation.private void
writeTableSection()
Write the table section.private void
writeTagSection()
Write the tag section if needed.protected void
writeVirtualFunctionCall(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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in 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.IOException
Write 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.IOException
Write the memory section.- Throws:
java.io.IOException
- if any I/O error occur
-
writeTagSection
private void writeTagSection() throws java.io.IOException
Write the tag section if needed.- Throws:
java.io.IOException
- if any I/O error occur
-
writeStartSection
private void writeStartSection() throws java.io.IOException
Write 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.IOException
Write 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.IOException
Write 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.IOException
Write the data section- Throws:
java.io.IOException
- if any I/O error occur
-
writeDebugNames
private void writeDebugNames() throws java.io.IOException
Write 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.IOException
Write the source mapping url- Throws:
java.io.IOException
- if any I/O error occur
-
writeProducersSection
private void writeProducersSection() throws java.io.IOException
Write 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:
writeStructType
in 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:
writeBlockType
in 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.IOException
Mark to write exceptions- Specified by:
writeException
in 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:
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
-
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
-
writeExport
protected void writeExport(FunctionName name, java.lang.String exportName) throws java.io.IOException
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:
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:
writeMethodParamStart
in 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.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:
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:
writeMethodParamFinish
in 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:
writeMethodStart
in 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:
markSourceLine
in classModuleWriter
- Parameters:
javaSourceLine
- the line number in the Java code
-
writeMethodFinish
protected void writeMethodFinish() throws java.io.IOException
Complete the method- Specified by:
writeMethodFinish
in classModuleWriter
- Throws:
java.io.IOException
- if any I/O error occur
-
writeConst
protected void writeConst(java.lang.Number value, ValueType valueType) throws java.io.IOException
Write a constant number value- Specified by:
writeConst
in 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:
writeLocal
in 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.IOException
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:
java.io.IOException
- if any I/O error occur
-
writeTable
protected void writeTable(boolean load, @Nonnegative int idx) throws java.io.IOException
Write a table operation.- Specified by:
writeTable
in 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:
writeDefaultValue
in 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:
writeNumericOperator
in 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:
writeCast
in 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:
writeFunctionCall
in 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:
writeVirtualFunctionCall
in 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.IOException
Write a block/branch code- Specified by:
writeBlockCode
in 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.IOException
Write an array operation.- Specified by:
writeArrayOperator
in 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:
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:
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:
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:
java.io.IOException
- if any I/O error occur
-
-