import { ValTypeVoid, ValTypeFunc, ValTypeStructRef, ValTypeNullRef, ValTypeI32, ValTypeF32, ValTypeF64, ValTypeI64, ValTypeI8, ValTypeI16, ValTypeV128, ValTypeOptRef, ValTypeAnyRef, ValTypeFuncRef, ValTypeExternRef, ValTypeRefExtern, ValTypeEqRef, ValTypeRef, ValTypeRefNull } from './wasm.zena'; import { StringBuilder, ByteBuffer } from 'zena:core'; import { WasmEmitter, ImportItem } from './wasm-emitter.zena'; import { ValType, ImportDesc, FuncImport, GlobalImport, MemoryImport } from "./wasm.zena"; import { WasmFunction } from "./wasm-module.zena"; import { simdName } from '../simd.zena'; import { wideArithName } from './wasm.zena'; export final class WatEmitter implements WasmEmitter { /** * The active output. Function code is assembled out-of-line — the * signature and body go to per-function builders, joined at * emitFunctionCodeEnd — so locals print with their FINAL types and * dropped removable asserts never appear: the text must not show a * `ref.as_non_null` the binary does not pay for. */ var sb: StringBuilder; var indentDepth: i32 = 1; var emittedRecGroup: boolean = false; /** Whether the struct being emitted opened a `(sub ...)` clause. */ var #wroteSubClause: boolean = false; var currentFunctionName: String = ""; var #outerSb: StringBuilder | null = null; var #funcHeadSb: StringBuilder | null = null; var #funcBodySb: StringBuilder | null = null; var #currentLocals: GrowableArray | null = null; /** Byte ranges of removable asserts in the body builder, per local. */ #removableStarts = new Array(); #removableEnds = new Array(); #removableLocals = new Array(); var #graduatedLocals: GrowableArray | null = null; new(this.sb); indent(): void { for (var i = 0; i < this.indentDepth; i += 1) { this.sb.append(' '); } } // Module lifecycle emitModuleStart(): void { this.sb.append('(module\n'); } emitTypeSectionStart(count: i32): void {} emitRecGroupStart(count: i32): void { if (count > 1) { this.indent(); this.sb.append('(rec\n'); this.indentDepth += 1; this.emittedRecGroup = true; } else { this.emittedRecGroup = false; } } emitRecGroupEnd(): void { if (this.emittedRecGroup) { this.indentDepth -= 1; this.indent(); this.sb.append(')\n'); this.emittedRecGroup = false; } } emitFunctionTypeStart(index: i32): void { this.indent(); this.sb.append('(type (;').append(i32ToString(index)).append(';) (func'); } emitFunctionTypeParamsStart(count: i32): void {} emitFunctionTypeParam(type: ValType): void { this.sb.append(' (param ').append(getTypeName(type)).append(')'); } emitFunctionTypeResultsStart(count: i32): void {} emitFunctionTypeResult(type: ValType): void { this.sb.append(' (result ').append(getTypeName(type)).append(')'); } emitFunctionTypeEnd(): void { this.sb.append('))\n'); } emitStructTypeStart(index: i32, name: String | null, superTypeIndex: i32, isFinal: boolean): void { this.indent(); if (name != null) { this.sb.append('(type $').append(name).append(' (;').append(i32ToString(index)).append(';) '); } else { this.sb.append('(type (;').append(i32ToString(index)).append(';) '); } // A bare `(struct ...)` already means `sub final` with no // supertype, so the clause is written only when it says something // the bare form does not: that the struct is open, or what it // extends. Printing nothing for an OPEN struct was the bug — it // read as final while the binary emitter declared every struct // open (struct-finality.md). this.#wroteSubClause = !isFinal || superTypeIndex >= 0; if (this.#wroteSubClause) { this.sb.append('(sub '); if (isFinal) { this.sb.append('final '); } if (superTypeIndex >= 0) { this.sb.append(i32ToString(superTypeIndex)).append(' '); } } this.sb.append('(struct'); } emitStructTypeFieldsStart(count: i32): void {} emitStructTypeField(type: ValType, isMutable: boolean): void { this.sb.append(' (field '); if (isMutable) { this.sb.append('(mut '); } this.sb.append(getTypeName(type)); if (isMutable) { this.sb.append(')'); } this.sb.append(')'); } emitStructTypeEnd(): void { // Closes the `(struct`, the `(sub` if one was opened, and the `(type`. if (this.#wroteSubClause) { this.sb.append(')))\n'); } else { this.sb.append('))\n'); } this.#wroteSubClause = false; } emitTypeSectionEnd(): void {} emitFunctionSectionStart(count: i32): void {} emitFunctionDefinition(typeIndex: i32): void {} emitFunctionSectionEnd(): void {} emitImportSectionStart(count: i32): void {} emitImport(item: ImportItem): void { this.indent(); this.sb.append('(import "').append(item.module).append('" "').append(item.name).append('" '); this.#importDesc(item.desc); this.sb.append(')\n'); } emitImportGroup(module: String, items: GrowableArray): void { this.indent(); this.sb.append('(import "').append(module).append('"'); for (let {name, desc} in items) { this.sb.append('\n'); this.indent(); this.sb.append(' (item "').append(name).append('" '); this.#importDesc(desc); this.sb.append(')'); } this.sb.append(')\n'); } emitImportTypeGroup(module: String, desc: ImportDesc, items: GrowableArray): void { this.indent(); this.sb.append('(import "').append(module).append('"'); for (let {name} in items) { this.sb.append('\n'); this.indent(); this.sb.append(' (item "').append(name).append('")'); } this.sb.append('\n'); this.indent(); this.sb.append(' '); this.#importDesc(desc); this.sb.append(')\n'); } #importDesc(desc: ImportDesc): void { match (desc) { case FuncImport {typeIndex}: { this.sb.append('(func (type ').append(i32ToString(typeIndex)).append('))'); } case GlobalImport {type, isMutable}: { if (isMutable) { this.sb.append('(global (mut ').append(getTypeName(type)).append('))'); } else { this.sb.append('(global ').append(getTypeName(type)).append(')'); } } case MemoryImport {minPages}: { this.sb.append('(memory ').append(i32ToString(minPages)).append(')'); } } } emitImportSectionEnd(): void {} emitGlobalSectionStart(count: i32): void {} emitGlobal(type: ValType, isMutable: boolean): void { this.indent(); this.sb.append('(global '); if (isMutable) { this.sb.append('(mut ').append(getTypeName(type)).append(')'); } else { this.sb.append(getTypeName(type)); } this.sb.append('\n'); this.indentDepth += 1; } emitGlobalEnd(): void { this.indentDepth -= 1; this.indent(); this.sb.append(')\n'); } emitGlobalSectionEnd(): void {} emitExportSectionStart(count: i32): void {} emitExport(name: String, func: WasmFunction): void { this.indent(); this.sb .append('(export "') .append(name) .append('" (func $') .append(getFunctionName(func)) .append('))\n'); } emitExportGlobal(name: String, globalIndex: i32): void { this.indent(); this.sb .append('(export "') .append(name) .append('" (global ') .append(i32ToString(globalIndex)) .append('))\n'); } emitExportTag(name: String, tagIndex: i32): void { this.indent(); this.sb .append('(export "') .append(name) .append('" (tag ') .append(i32ToString(tagIndex)) .append('))\n'); } emitExportSectionEnd(): void {} emitTagSectionStart(count: i32): void {} emitTagDefinition(index: i32, typeIndex: i32): void { this.indent(); this.sb .append('(tag (;') .append(i32ToString(index)) .append(';) (type ') .append(i32ToString(typeIndex)) .append('))\n'); } emitTagSectionEnd(): void {} emitElementSection(indices: GrowableArray): void { if (indices.length == 0) return; this.indent(); this.sb.append('(elem declare func'); for (let idx in indices) { this.sb.append(' ').append(i32ToString(idx)); } this.sb.append(')\n'); } emitStartSection(funcIndex: i32): void { this.indent(); this.sb.append('(start ').append(i32ToString(funcIndex)).append(')\n'); } emitCodeSectionStart(count: i32): void {} emitFunctionCodeStart(func: WasmFunction): void { this.#outerSb = this.sb; let head = new StringBuilder(); this.#funcHeadSb = head; this.sb = head; while (this.#removableStarts.length > 0) { this.#removableStarts.pop(); this.#removableEnds.pop(); this.#removableLocals.pop(); } this.#graduatedLocals = null; } emitFunctionCodeEnd(func: WasmFunction): void { let outer = this.#outerSb as StringBuilder; this.sb = outer; let head = this.#funcHeadSb as StringBuilder; outer.append(head.toString()); let locals = this.#currentLocals; if (locals != null && locals.length > 0) { outer.append('\n'); this.indentDepth += 1; for (let l in locals) { this.indent(); outer.append('(local ').append(getTypeName(l)).append(')\n'); } this.indentDepth -= 1; } outer.append('\n'); // Body text, minus the removable asserts of graduated locals. let bodyText = (this.#funcBodySb as StringBuilder).toString(); let graduated = this.#graduatedLocals; var pos = 0; if (graduated != null) { var r = 0; while (r < this.#removableStarts.length) { let l = this.#removableLocals[r]; if (l < graduated.length && graduated[l]) { outer.append(bodyText.sliceBytes(pos, this.#removableStarts[r])); pos = this.#removableEnds[r]; } r += 1; } } outer.append(bodyText.sliceBytes(pos, bodyText.length)); this.#outerSb = null; this.#funcHeadSb = null; this.#funcBodySb = null; this.#currentLocals = null; } emitCodeSectionEnd(): void {} emitModuleEnd(): void { this.sb.append(')\n'); } // Instruction lifecycle emitFunctionSignatureStart(name: String, typeIndex: i32): void { this.currentFunctionName = name; this.indent(); this.sb.append('(func $').append(name).append(' (type ').append(i32ToString(typeIndex)).append(')'); } emitFunctionLocalsStart(locals: GrowableArray): void { // Printing is deferred to emitFunctionCodeEnd: the ZIR emitter may // still graduate entries of this array to non-null after the body. this.#currentLocals = locals; let body = new StringBuilder(); this.#funcBodySb = body; this.sb = body; this.indentDepth += 1; } emitFunctionEnd(): void { this.indentDepth -= 1; this.indent(); this.sb.append(')\n'); } // Control flow emitUnreachable(): void { this.indent(); this.sb.append('unreachable\n'); } emitBlock(resultType: ValType): void { this.indent(); if (resultType is ValTypeVoid ) { this.sb.append('block\n'); } else { this.sb.append('block (result ').append(getTypeName(resultType)).append(')\n'); } this.indentDepth += 1; } emitLoop(resultType: ValType): void { this.indent(); if (resultType is ValTypeVoid ) { this.sb.append('loop\n'); } else { this.sb.append('loop (result ').append(getTypeName(resultType)).append(')\n'); } this.indentDepth += 1; } emitIf(resultType: ValType): void { this.indent(); if (resultType is ValTypeVoid ) { this.sb.append('if\n'); } else { this.sb.append('if (result ').append(getTypeName(resultType)).append(')\n'); } this.indentDepth += 1; } emitElse(): void { this.indentDepth -= 1; this.indent(); this.sb.append('else\n'); this.indentDepth += 1; } emitEnd(): void { this.indentDepth -= 1; this.indent(); this.sb.append('end\n'); } emitBr(depth: i32): void { this.indent(); this.sb.append('br ').append(i32ToString(depth)).append('\n'); } emitBrIf(depth: i32): void { this.indent(); this.sb.append('br_if ').append(i32ToString(depth)).append('\n'); } emitThrow(tagIndex: i32): void { this.indent(); this.sb.append('throw ').append(i32ToString(tagIndex)).append('\n'); } emitTryTableCatch(resultType: ValType, tagIndex: i32, destDepth: i32): void { this.indent(); this.sb.append('try_table'); if (!(resultType is ValTypeVoid)) { this.sb.append(' (result ').append(getTypeName(resultType)).append(')'); } this.sb .append(' (catch ') .append(i32ToString(tagIndex)) .append(' ') .append(i32ToString(destDepth)) .append(')\n'); this.indentDepth += 1; } emitReturn(): void { this.indent(); this.sb.append('return\n'); } emitCall(target: WasmFunction): void { this.indent(); this.sb.append('call $').append(getFunctionName(target)).append('\n'); } emitCallRef(typeIndex: i32): void { this.indent(); this.sb.append('call_ref ').append(i32ToString(typeIndex)).append('\n'); } emitReturnCall(target: WasmFunction): void { this.indent(); this.sb.append('return_call $').append(getFunctionName(target)).append('\n'); } emitReturnCallRef(typeIndex: i32): void { this.indent(); this.sb.append('return_call_ref ').append(i32ToString(typeIndex)).append('\n'); } emitRefEq(): void { this.indent(); this.sb.append('ref.eq\n'); } emitRefIsNull(): void { this.indent(); this.sb.append('ref.is_null\n'); } emitRefAsNonNull(): void { this.indent(); this.sb.append('ref.as_non_null\n'); } emitRemovableRefAsNonNull(local: i32): void { this.#removableStarts.push(this.sb.length); this.#removableLocals.push(local); this.emitRefAsNonNull(); this.#removableEnds.push(this.sb.length); } dropAssertsForLocals(graduated: GrowableArray): void { this.#graduatedLocals = graduated; } emitRefFunc(index: i32): void { this.indent(); this.sb.append('ref.func ').append(i32ToString(index)).append('\n'); } emitRefNull(typeIndex: i32): void { this.indent(); // A negative index is an abstract heap type: its reference type's // code minus 0x80, which is how the binary emitter writes it. let typeStr = if (typeIndex == 0x71 || typeIndex == -15) { "none" } else if (typeIndex == -16) { "func" } else if (typeIndex == -17) { "extern" } else if (typeIndex == 0x6e || typeIndex == -18) { "any" } else if (typeIndex == 0x6d || typeIndex == -19) { "eq" } else if (typeIndex == -20) { "i31" } else if (typeIndex == -21) { "struct" } else { i32ToString(typeIndex) }; this.sb.append('ref.null ').append(typeStr).append('\n'); } emitAnyConvertExtern(): void { this.indent(); this.sb.append('any.convert_extern\n'); } emitExternConvertAny(): void { this.indent(); this.sb.append('extern.convert_any\n'); } // GC operations emitStructNew(typeIndex: i32): void { this.indent(); this.sb.append('struct.new ').append(i32ToString(typeIndex)).append('\n'); } emitStructNewDefault(typeIndex: i32): void { this.indent(); this.sb.append('struct.new_default ').append(i32ToString(typeIndex)).append('\n'); } emitStructGet(typeIndex: i32, fieldIndex: i32): void { this.indent(); this.sb.append('struct.get ').append(i32ToString(typeIndex)).append(' ').append(i32ToString(fieldIndex)).append('\n'); } emitStructSet(typeIndex: i32, fieldIndex: i32): void { this.indent(); this.sb.append('struct.set ').append(i32ToString(typeIndex)).append(' ').append(i32ToString(fieldIndex)).append('\n'); } emitRefCastEq(): void { this.indent(); this.sb.append('ref.cast eq\n'); } emitRefCastEqNull(): void { this.indent(); this.sb.append('ref.cast (ref null eq)\n'); } emitRefCastStructNull(): void { this.indent(); this.sb.append('ref.cast (ref null struct)\n'); } emitRefCastNullNone(): void { this.indent(); this.sb.append('ref.cast (ref null none)\n'); } emitRefCast(typeIndex: i32): void { this.indent(); this.sb.append('ref.cast ').append(i32ToString(typeIndex)).append('\n'); } emitRefCastNull(typeIndex: i32): void { this.indent(); this.sb.append('ref.cast (ref null ').append(i32ToString(typeIndex)).append(')\n'); } emitRefTest(typeIndex: i32): void { this.indent(); this.sb.append('ref.test ').append(i32ToString(typeIndex)).append('\n'); } emitRefTestNull(typeIndex: i32): void { this.indent(); this.sb.append('ref.test_null ').append(i32ToString(typeIndex)).append('\n'); } emitI32Const(value: i32): void { this.indent(); this.sb.append('i32.const ').append(i32ToString(value)).append('\n'); } emitF64Const(value: f64): void { this.indent(); this.sb.append('f64.const ').append(f64ToString(value)).append('\n'); } emitF32Const(value: f64): void { this.indent(); this.sb.append('f32.const ').append(f64ToString(value)).append('\n'); } emitBooleanConst(value: boolean): void { this.indent(); let s = if (value) '1' else '0'; this.sb.append('i32.const ').append(s).append('\n'); } emitStringConst(value: String): void { this.indent(); this.sb.append(';; string "').append(value).append('"\n'); } emitDrop(): void { this.indent(); this.sb.append('drop\n'); } // Variables emitLocalGet(index: i32): void { this.indent(); this.sb.append('local.get ').append(i32ToString(index)).append('\n'); } emitLocalSet(index: i32): void { this.indent(); this.sb.append('local.set ').append(i32ToString(index)).append('\n'); } emitLocalTee(index: i32): void { this.indent(); this.sb.append('local.tee ').append(i32ToString(index)).append('\n'); } emitGlobalGet(index: i32): void { this.indent(); this.sb.append('global.get ').append(i32ToString(index)).append('\n'); } emitGlobalSet(index: i32): void { this.indent(); this.sb.append('global.set ').append(i32ToString(index)).append('\n'); } // Binary operations emitI32Add(): void { this.indent(); this.sb.append('i32.add\n'); } emitI32Sub(): void { this.indent(); this.sb.append('i32.sub\n'); } emitI32Mul(): void { this.indent(); this.sb.append('i32.mul\n'); } emitI32DivS(): void { this.indent(); this.sb.append('i32.div_s\n'); } emitI32DivU(): void { this.indent(); this.sb.append('i32.div_u\n'); } emitI32Eqz(): void { this.indent(); this.sb.append('i32.eqz\n'); } emitI32Eq(): void { this.indent(); this.sb.append('i32.eq\n'); } emitI32Ne(): void { this.indent(); this.sb.append('i32.ne\n'); } emitI32LtS(): void { this.indent(); this.sb.append('i32.lt_s\n'); } emitI32LtU(): void { this.indent(); this.sb.append('i32.lt_u\n'); } emitI32LeS(): void { this.indent(); this.sb.append('i32.le_s\n'); } emitI32LeU(): void { this.indent(); this.sb.append('i32.le_u\n'); } emitI32GtS(): void { this.indent(); this.sb.append('i32.gt_s\n'); } emitI32GtU(): void { this.indent(); this.sb.append('i32.gt_u\n'); } emitI32GeS(): void { this.indent(); this.sb.append('i32.ge_s\n'); } emitI32GeU(): void { this.indent(); this.sb.append('i32.ge_u\n'); } emitI32RemS(): void { this.indent(); this.sb.append('i32.rem_s\n'); } emitI32RemU(): void { this.indent(); this.sb.append('i32.rem_u\n'); } emitI32And(): void { this.indent(); this.sb.append('i32.and\n'); } emitI32Or(): void { this.indent(); this.sb.append('i32.or\n'); } emitI32Xor(): void { this.indent(); this.sb.append('i32.xor\n'); } emitI32Shl(): void { this.indent(); this.sb.append('i32.shl\n'); } emitI32ShrS(): void { this.indent(); this.sb.append('i32.shr_s\n'); } emitI32ShrU(): void { this.indent(); this.sb.append('i32.shr_u\n'); } emitI32Clz(): void { this.indent(); this.sb.append('i32.clz\n'); } emitI32Ctz(): void { this.indent(); this.sb.append('i32.ctz\n'); } emitI32Popcnt(): void { this.indent(); this.sb.append('i32.popcnt\n'); } emitF64Add(): void { this.indent(); this.sb.append('f64.add\n'); } emitF64Sub(): void { this.indent(); this.sb.append('f64.sub\n'); } emitF64Mul(): void { this.indent(); this.sb.append('f64.mul\n'); } emitF64Div(): void { this.indent(); this.sb.append('f64.div\n'); } emitF64Eq(): void { this.indent(); this.sb.append('f64.eq\n'); } emitF64Ne(): void { this.indent(); this.sb.append('f64.ne\n'); } emitF64Lt(): void { this.indent(); this.sb.append('f64.lt\n'); } emitF64Gt(): void { this.indent(); this.sb.append('f64.gt\n'); } emitF64Le(): void { this.indent(); this.sb.append('f64.le\n'); } emitF64Ge(): void { this.indent(); this.sb.append('f64.ge\n'); } emitF64ConvertI32S(): void { this.indent(); this.sb.append('f64.convert_i32_s\n'); } emitF32Add(): void { this.indent(); this.sb.append('f32.add\n'); } emitF32Sub(): void { this.indent(); this.sb.append('f32.sub\n'); } emitF32Mul(): void { this.indent(); this.sb.append('f32.mul\n'); } emitF32Div(): void { this.indent(); this.sb.append('f32.div\n'); } emitF32Eq(): void { this.indent(); this.sb.append('f32.eq\n'); } emitF32Ne(): void { this.indent(); this.sb.append('f32.ne\n'); } emitF32Lt(): void { this.indent(); this.sb.append('f32.lt\n'); } emitF32Le(): void { this.indent(); this.sb.append('f32.le\n'); } emitF32Gt(): void { this.indent(); this.sb.append('f32.gt\n'); } emitF32Ge(): void { this.indent(); this.sb.append('f32.ge\n'); } emitF32ConvertI32S(): void { this.indent(); this.sb.append('f32.convert_i32_s\n'); } emitMemorySize(): void { this.indent(); this.sb.append('memory.size\n'); } emitMemoryGrow(): void { this.indent(); this.sb.append('memory.grow\n'); } emitI32Load(align: i32, offset: i32): void { this.indent(); this.sb.append(`i32.load align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitI32Store(align: i32, offset: i32): void { this.indent(); this.sb.append(`i32.store align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitI32Load8U(align: i32, offset: i32): void { this.indent(); this.sb.append(`i32.load8_u align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitI32Store8(align: i32, offset: i32): void { this.indent(); this.sb.append(`i32.store8 align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitI64Load(align: i32, offset: i32): void { this.indent(); this.sb.append(`i64.load align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitI64Store(align: i32, offset: i32): void { this.indent(); this.sb.append(`i64.store align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitF32Load(align: i32, offset: i32): void { this.indent(); this.sb.append(`f32.load align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitF32Store(align: i32, offset: i32): void { this.indent(); this.sb.append(`f32.store align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitF64Load(align: i32, offset: i32): void { this.indent(); this.sb.append(`f64.load align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitF64Store(align: i32, offset: i32): void { this.indent(); this.sb.append(`f64.store align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitSimd(opcode: i32): void { this.indent(); this.sb.append(simdName(opcode) + '\n'); } emitWideArith(opcode: i32): void { this.indent(); this.sb.append(wideArithName(opcode) + '\n'); } emitSimdLane(opcode: i32, lane: i32): void { this.indent(); this.sb.append(`${simdName(opcode)} ${i32ToString(lane)}\n`); } emitSimdMem(opcode: i32, align: i32, offset: i32): void { this.indent(); this.sb.append(`${simdName(opcode)} align=${i32ToString(align)} offset=${i32ToString(offset)}\n`); } emitSimdMemLane(opcode: i32, align: i32, offset: i32, lane: i32): void { this.indent(); this.sb.append(`${simdName(opcode)} align=${i32ToString(align)} offset=${i32ToString(offset)} ${i32ToString(lane)}\n`); } /** * `v128.const` prints in its `i8x16` form because the bytes are what * the instruction actually carries; `i8x16.shuffle`'s bytes are lane * selectors and take no shape prefix. */ emitSimdBytes(opcode: i32, bytes: GrowableArray): void { this.indent(); let name = simdName(opcode); this.sb.append(name); if (name == 'v128.const') { this.sb.append(' i8x16'); } for (let byte in bytes) { this.sb.append(' ' + i32ToString(byte)); } this.sb.append('\n'); } emitArrayType(index: i32, elementType: ValType, isMutable: boolean, superTypeIndex: i32, isFinal: boolean): void { this.indent(); this.sb.append('(type (;').append(i32ToString(index)).append(';) '); // As for the struct form above: the bare `(array ...)` is already // `sub final` with no supertype, so an array of values prints // bare. let wroteSubClause = !isFinal || superTypeIndex >= 0; if (wroteSubClause) { this.sb.append('(sub '); if (isFinal) { this.sb.append('final '); } if (superTypeIndex >= 0) { this.sb.append(i32ToString(superTypeIndex)).append(' '); } } this.sb.append('(array '); if (isMutable) { this.sb.append('(mut '); } this.sb.append(getTypeName(elementType)); if (isMutable) { this.sb.append(')'); } this.sb.append(')'); if (wroteSubClause) { this.sb.append(')'); } this.sb.append(')\n'); } emitMemorySection(minPages: i32): void { this.indent(); this.sb.append('(memory (;0;) ').append(i32ToString(minPages)).append(')\n'); } emitExportMemory(name: String, memIndex: i32): void { this.indent(); this.sb.append('(export "').append(name).append('" (memory ').append(i32ToString(memIndex)).append('))\n'); } emitDataCountSection(count: i32): void {} emitDataSection(datas: GrowableArray, rawDatas: GrowableArray): void { var i = 0; for (let data in rawDatas) { this.indent(); this.sb.append('(data (;').append(i32ToString(i)).append(';) "').append(escapeBytesForWat(data)).append('")\n'); i += 1; } } emitArrayNew(typeIndex: i32): void { this.indent(); this.sb.append('array.new ').append(i32ToString(typeIndex)).append('\n'); } emitArrayNewDefault(typeIndex: i32): void { this.indent(); this.sb.append('array.new_default ').append(i32ToString(typeIndex)).append('\n'); } emitArrayNewFixed(typeIndex: i32, count: i32): void { this.indent(); this.sb.append('array.new_fixed ').append(i32ToString(typeIndex)).append(' ').append(i32ToString(count)).append('\n'); } emitArrayNewData(typeIndex: i32, dataIndex: i32): void { this.indent(); this.sb.append('array.new_data ').append(i32ToString(typeIndex)).append(' ').append(i32ToString(dataIndex)).append('\n'); } emitArrayGet(typeIndex: i32): void { this.indent(); this.sb.append('array.get ').append(i32ToString(typeIndex)).append('\n'); } emitArrayGetU(typeIndex: i32): void { this.indent(); this.sb.append('array.get_u ').append(i32ToString(typeIndex)).append('\n'); } emitArrayGetS(typeIndex: i32): void { this.indent(); this.sb.append('array.get_s ').append(i32ToString(typeIndex)).append('\n'); } emitArraySet(typeIndex: i32): void { this.indent(); this.sb.append('array.set ').append(i32ToString(typeIndex)).append('\n'); } emitArrayLen(): void { this.indent(); this.sb.append('array.len\n'); } emitArrayCopy(destTypeIndex: i32, srcTypeIndex: i32): void { this.indent(); this.sb.append('array.copy ').append(i32ToString(destTypeIndex)).append(' ').append(i32ToString(srcTypeIndex)).append('\n'); } emitI64Const(value: i64): void { this.indent(); this.sb.append('i64.const ').append(i64ToString(value)).append('\n'); } emitI64Add(): void { this.indent(); this.sb.append('i64.add\n'); } emitI64Sub(): void { this.indent(); this.sb.append('i64.sub\n'); } emitI64Mul(): void { this.indent(); this.sb.append('i64.mul\n'); } emitI64DivS(): void { this.indent(); this.sb.append('i64.div_s\n'); } emitI64DivU(): void { this.indent(); this.sb.append('i64.div_u\n'); } emitI64RemS(): void { this.indent(); this.sb.append('i64.rem_s\n'); } emitI64RemU(): void { this.indent(); this.sb.append('i64.rem_u\n'); } emitI64And(): void { this.indent(); this.sb.append('i64.and\n'); } emitI64Or(): void { this.indent(); this.sb.append('i64.or\n'); } emitI64Xor(): void { this.indent(); this.sb.append('i64.xor\n'); } emitI64Shl(): void { this.indent(); this.sb.append('i64.shl\n'); } emitI64ShrS(): void { this.indent(); this.sb.append('i64.shr_s\n'); } emitI64ShrU(): void { this.indent(); this.sb.append('i64.shr_u\n'); } emitI64Eqz(): void { this.indent(); this.sb.append('i64.eqz\n'); } emitI64Eq(): void { this.indent(); this.sb.append('i64.eq\n'); } emitI64Ne(): void { this.indent(); this.sb.append('i64.ne\n'); } emitI64LtS(): void { this.indent(); this.sb.append('i64.lt_s\n'); } emitI64LtU(): void { this.indent(); this.sb.append('i64.lt_u\n'); } emitI64LeS(): void { this.indent(); this.sb.append('i64.le_s\n'); } emitI64LeU(): void { this.indent(); this.sb.append('i64.le_u\n'); } emitI64GtS(): void { this.indent(); this.sb.append('i64.gt_s\n'); } emitI64GtU(): void { this.indent(); this.sb.append('i64.gt_u\n'); } emitI64GeS(): void { this.indent(); this.sb.append('i64.ge_s\n'); } emitI64GeU(): void { this.indent(); this.sb.append('i64.ge_u\n'); } emitI64Clz(): void { this.indent(); this.sb.append('i64.clz\n'); } emitI64Ctz(): void { this.indent(); this.sb.append('i64.ctz\n'); } emitI64Popcnt(): void { this.indent(); this.sb.append('i64.popcnt\n'); } emitF32Neg(): void { this.indent(); this.sb.append('f32.neg\n'); } emitF64Neg(): void { this.indent(); this.sb.append('f64.neg\n'); } emitF32Ceil(): void { this.indent(); this.sb.append('f32.ceil\n'); } emitF64Ceil(): void { this.indent(); this.sb.append('f64.ceil\n'); } emitF32Floor(): void { this.indent(); this.sb.append('f32.floor\n'); } emitF64Floor(): void { this.indent(); this.sb.append('f64.floor\n'); } emitF32Trunc(): void { this.indent(); this.sb.append('f32.trunc\n'); } emitF64Trunc(): void { this.indent(); this.sb.append('f64.trunc\n'); } emitF32Sqrt(): void { this.indent(); this.sb.append('f32.sqrt\n'); } emitF64Sqrt(): void { this.indent(); this.sb.append('f64.sqrt\n'); } emitF32Min(): void { this.indent(); this.sb.append('f32.min\n'); } emitF64Min(): void { this.indent(); this.sb.append('f64.min\n'); } emitF32Max(): void { this.indent(); this.sb.append('f32.max\n'); } emitF64Max(): void { this.indent(); this.sb.append('f64.max\n'); } emitF32Copysign(): void { this.indent(); this.sb.append('f32.copysign\n'); } emitF64Copysign(): void { this.indent(); this.sb.append('f64.copysign\n'); } emitF32Abs(): void { this.indent(); this.sb.append('f32.abs\n'); } emitF64Abs(): void { this.indent(); this.sb.append('f64.abs\n'); } emitF32Nearest(): void { this.indent(); this.sb.append('f32.nearest\n'); } emitF64Nearest(): void { this.indent(); this.sb.append('f64.nearest\n'); } emitI64TruncSatF32S(): void { this.indent(); this.sb.append('i64.trunc_sat_f32_s\n'); } emitI64TruncSatF32U(): void { this.indent(); this.sb.append('i64.trunc_sat_f32_u\n'); } emitI64TruncSatF64S(): void { this.indent(); this.sb.append('i64.trunc_sat_f64_s\n'); } emitI64TruncSatF64U(): void { this.indent(); this.sb.append('i64.trunc_sat_f64_u\n'); } emitI32TruncSatF32S(): void { this.indent(); this.sb.append('i32.trunc_sat_f32_s\n'); } emitI32TruncSatF32U(): void { this.indent(); this.sb.append('i32.trunc_sat_f32_u\n'); } emitI32TruncSatF64S(): void { this.indent(); this.sb.append('i32.trunc_sat_f64_s\n'); } emitI32TruncSatF64U(): void { this.indent(); this.sb.append('i32.trunc_sat_f64_u\n'); } emitI64ReinterpretF64(): void { this.indent(); this.sb.append('i64.reinterpret_f64\n'); } emitF64ReinterpretI64(): void { this.indent(); this.sb.append('f64.reinterpret_i64\n'); } emitI32ReinterpretF32(): void { this.indent(); this.sb.append('i32.reinterpret_f32\n'); } emitF32ReinterpretI32(): void { this.indent(); this.sb.append('f32.reinterpret_i32\n'); } emitI64ExtendI32S(): void { this.indent(); this.sb.append('i64.extend_i32_s\n'); } emitI64ExtendI32U(): void { this.indent(); this.sb.append('i64.extend_i32_u\n'); } emitI32WrapI64(): void { this.indent(); this.sb.append('i32.wrap_i64\n'); } emitF32ConvertI64S(): void { this.indent(); this.sb.append('f32.convert_i64_s\n'); } emitF64ConvertI64S(): void { this.indent(); this.sb.append('f64.convert_i64_s\n'); } emitF32ConvertI32U(): void { this.indent(); this.sb.append('f32.convert_i32_u\n'); } emitF64ConvertI32U(): void { this.indent(); this.sb.append('f64.convert_i32_u\n'); } emitF32ConvertI64U(): void { this.indent(); this.sb.append('f32.convert_i64_u\n'); } emitF64ConvertI64U(): void { this.indent(); this.sb.append('f64.convert_i64_u\n'); } emitF64PromoteF32(): void { this.indent(); this.sb.append('f64.promote_f32\n'); } emitF32DemoteF64(): void { this.indent(); this.sb.append('f32.demote_f64\n'); } } function escapeBytesForWat(bytes: ByteBuffer): String { let sb = new StringBuilder(); let len = bytes.length; for (var i = 0; i < len; i += 1) { let b = bytes.getByte(i); sb.append('\\'); let hexChars = "0123456789abcdef"; let high = (b >> 4) & 0x0F; let low = b & 0x0F; sb.append(hexChars.sliceBytes(high, high + 1)); sb.append(hexChars.sliceBytes(low, low + 1)); } return sb.toString(); } function getFunctionName(func: WasmFunction): String { return func.name ?? `func${func.index}`; } function getTypeName(t: ValType): String { return match (t) { case ValTypeVoid: 'void' case ValTypeFunc: 'func' case ValTypeI32: 'i32' case ValTypeI64: 'i64' case ValTypeF32: 'f32' case ValTypeF64: 'f64' case ValTypeV128: 'v128' case ValTypeFuncRef: 'funcref' case ValTypeExternRef: 'externref' case ValTypeRefExtern: '(ref extern)' case ValTypeAnyRef: 'anyref' case ValTypeEqRef: 'eqref' case ValTypeStructRef: 'structref' case ValTypeNullRef: 'nullref' case ValTypeOptRef: 'optref' case ValTypeRef as r: `(ref ${i32ToString(r.target.index)})` case ValTypeRefNull as r: `(ref null ${i32ToString(r.target.index)})` case ValTypeI8: 'i8' case ValTypeI16: 'i16' }; }