compiler 0.0.2-preview.1

The Zena self-hosted compiler

Readme Files Dependencies Versions

compiler#

The Zena compiler, written in Zena. It parses, type-checks, and compiles Zena source code to WebAssembly (Wasm GC) and WebAssembly Text (WAT).

Usage#

Parsing a Single File#

Use compiler/parser to parse Zena source text into an AST (Module):

import { parse } from 'compiler/parser';

let ast = parse('export let answer = 42;', 'example.zena');

Loading and Checking a Program#

Use compiler (createCompiler and checkCompilation) with a CompilerHost to resolve imports across a module graph and type-check a program:

import {
  createCompiler, checkCompilation, buildStdlibConfig
} from 'compiler';
import { FsCompilerHost } from 'compiler/fs-host';

let stdlibRoot = '/stdlib';
let host = new FsCompilerHost(stdlibRoot);
let packages = buildStdlibConfig(stdlibRoot, 'host');
let compiler = createCompiler(host, {
  stdlibRoot,
  packages,
});

let result = await compiler.compile({
  entry: '/workspace/main.zena',
  target: 'host',
});
let checked = checkCompilation(result);

Compiling a File to WebAssembly#

Use compiler/compile-file to compile an entry file on disk to a .wasm or .wat output file:

import { compileFile } from 'compiler/compile-file';

let ok = await compileFile({
  entry: 'src/main.zena',
  outPath: 'out/main.wasm',
  stdlibRoot: '/stdlib',
});

Libraries#

  • compiler / compiler/compiler — compiler entry point (Compiler, createCompiler, checkCompilation, ProgramCheckResult)
  • compiler/compile-file — end-to-end file compilation (compileFile, CompileFileOptions)
  • compiler/tokenizer — lexer and token definitions (tokenize, Token, TokenType)
  • compiler/parser — recursive-descent parser (parse, Parser)
  • compiler/ast — AST node definitions (Node, Module, Statement, Expression, TypeAnnotation)
  • compiler/visitor — AST traversal helpers (walk, Visitor)
  • compiler/scope — lexical scope and symbol resolution (ScopeBuilder, ScopeResult, Symbol)
  • compiler/checker — static type checker (checkModule, CheckResult)
  • compiler/semantic-model — semantic queries over a checked module (SemanticModel)
  • compiler/types — compiler type representations (Type, ClassType, FunctionType, typeToString)
  • compiler/diagnostics — compiler diagnostics and source spans (Diagnostic, DiagnosticCode, DiagnosticSeverity, SourceSpan)
  • compiler/package-manifest — zena-package.json manifest and workspace loader (loadWorkspace, loadPackageMap)
  • compiler/module-resolver — package and relative module specifier resolution (ModuleResolver, PackageConfig)
  • compiler/library-loader — module graph loader (LibraryLoader, CompilerHost, SourceFile)
  • compiler/codegen/binary-generator — WebAssembly GC binary emitter (BinaryGenerator)
  • compiler/codegen/wat-generator — WebAssembly Text (WAT) emitter (WatGenerator)

License#

MIT

47.5 KiB Raw
import { ValTypeRef, ValTypeRefNull, ValTypeRefExtern } from './wasm.zena';
import { ByteBuffer, Encoding } from 'zena:core';
import { WasmEmitter, ImportItem } from './wasm-emitter.zena';
import {
  WasmSignature, WasmFunction, WasmStruct, WasmArray,
} from './wasm-module.zena';
import {
  SectionId, ValType, Opcode, SatOpcode, GcOpcode, HeapType, valTypeToOpcode, heapTypeToSigned,
  ImportDesc, FuncImport, GlobalImport, MemoryImport,
} from './wasm.zena';

export function emitValType(buffer: ByteBuffer, type: ValType) {
  let ignored = match (type) {
    case ValTypeRef as ref: {
      if (ref.target.index < 0) {
         let typeName = if (ref.target is WasmStruct) {
           let ws = ref.target as WasmStruct;
           "WasmStruct " + (if (ws.name != null) (ws.name as String) else "unnamed")
         } else if (ref.target is WasmArray) {
           "WasmArray"
         } else if (ref.target is WasmSignature) {
           "WasmSignature"
         } else {
           "WasmType"
         };
         throw new Error("Invalid WasmType index < 0 for ValTypeRef: target type is " + typeName);
      }
      buffer.writeByte(0x64);
      buffer.writeSLEB128(ref.target.index);
    }
    case ValTypeRefNull as ref: {
      if (ref.target.index < 0) {
         let typeName = if (ref.target is WasmStruct) {
           let ws = ref.target as WasmStruct;
           var fieldDesc = "";
           for (let f in ws.fields) {
             fieldDesc = fieldDesc + (f.name ?? "?") + ":" +
                 i32ToString(valTypeToOpcode(f.type)) + " ";
           }
           "WasmStruct " + (if (ws.name != null) (ws.name as String) else "unnamed") +
               " [" + fieldDesc + "]"
         } else if (ref.target is WasmArray) {
           "WasmArray"
         } else if (ref.target is WasmSignature) {
           "WasmSignature"
         } else {
           "WasmType"
         };
         throw new Error("Invalid WasmType index < 0 for ValTypeRefNull: target type is " + typeName);
      }
      buffer.writeByte(0x63);
      buffer.writeSLEB128(ref.target.index);
    }
    case ValTypeRefExtern: {
      buffer.writeByte(0x64);
      buffer.writeByte(0x6F);
    }
    case _: {
      buffer.writeByte(valTypeToOpcode(type));
    }
  };
}

/** The bytes `writeWasmString` writes for `s` after the length prefix. */
export function utf8ByteLength(s: String): i32 {
  if (s.encoding != Encoding.WTF16) {
    return s.length;
  }
  let sLen = s.length;
  var utf8Len = 0;
  var i = 0;
  while (i < sLen) {
    let cp = s.codePointAt(i);
    if (cp < 0x80) {
      utf8Len += 1;
    } else if (cp < 0x800) {
      utf8Len += 2;
    } else if (cp < 0x10000) {
      utf8Len += 3;
    } else {
      utf8Len += 4;
    }
    if (cp >= 0x10000) {
      i += 2;
    } else {
      i += 1;
    }
  }
  return utf8Len;
}

export function writeWasmString(buffer: ByteBuffer, s: String) {
  if (s.encoding == Encoding.WTF16) {
    let sLen = s.length;
    buffer.writeULEB128(utf8ByteLength(s));
    var i = 0;
    while (i < sLen) {
      let cp = s.codePointAt(i);
      if (cp < 0x80) {
        buffer.writeByte(cp);
      } else if (cp < 0x800) {
        buffer.writeByte(0xC0 | (cp >> 6));
        buffer.writeByte(0x80 | (cp & 0x3F));
      } else if (cp < 0x10000) {
        buffer.writeByte(0xE0 | (cp >> 12));
        buffer.writeByte(0x80 | ((cp >> 6) & 0x3F));
        buffer.writeByte(0x80 | (cp & 0x3F));
      } else {
        buffer.writeByte(0xF0 | (cp >> 18));
        buffer.writeByte(0x80 | ((cp >> 12) & 0x3F));
        buffer.writeByte(0x80 | ((cp >> 6) & 0x3F));
        buffer.writeByte(0x80 | (cp & 0x3F));
      }
      if (cp >= 0x10000) {
        i += 2;
      } else {
        i += 1;
      }
    }
  } else {
    let len = s.length;
    buffer.writeULEB128(len);
    for (var j = 0; j < len; j += 1) {
      buffer.writeByte(s.getByteAt(j));
    }
  }
}

export function writeImportDesc(buffer: ByteBuffer, desc: ImportDesc) {
  match (desc) {
    case FuncImport {typeIndex}: {
      buffer.writeByte(0x00);
      buffer.writeULEB128(typeIndex);
    }
    case GlobalImport {type, isMutable}: {
      buffer.writeByte(0x03);
      emitValType(buffer, type);
      buffer.writeByte(if (isMutable) 1 else 0);
    }
    case MemoryImport {minPages}: {
      buffer.writeByte(0x02);
      buffer.writeByte(0x00); // limits: min only
      buffer.writeULEB128(minPages);
    }
  }
}

/**
 * Implements the WasmEmitter interface by writing WebAssembly binary
 * formatted bytes to a ByteBuffer.
 */
export final class BinaryEmitter implements WasmEmitter {
  mainBuffer: ByteBuffer;
  
  var currentSection: ByteBuffer | null = null;
  var currentBuffer: ByteBuffer | null = null;
  var currentLocals: GrowableArray<ValType> | null = null;
  /**
   * Byte offsets (into currentBuffer) and locals of the removable
   * ref.as_non_null asserts in the current function, in emission
   * order. emitFunctionCodeEnd skips the offsets whose local
   * graduated (per #graduatedLocals) when copying the body out.
   */
  #removableOffsets = new Array<i32>();
  #removableLocals = new Array<i32>();
  var #graduatedLocals: GrowableArray<boolean> | null = null;
  functionsList = new Array<WasmFunction>();
  // Struct type names for the name section's type subsection, recorded
  // as the type section is written. A heap profiler (V8's snapshots)
  // labels wasm GC objects by their type's name, or by a bare index
  // without one.
  #typeNameIndices = new Array<i32>();
  #typeNames = new Array<String>();
  /**
   * Emit the `name` custom section. Debug metadata only, and by far the
   * largest section of a small module — so it is opt-in (`-g`) rather
   * than unconditional.
   */
  debugNames: boolean;

  new(this.mainBuffer, this.debugNames = false);

  // Module lifecycle
  emitModuleStart(): void {
    // Magic: \0asm
    this.mainBuffer.writeU32(0x6D736100);
    // Version: 1
    this.mainBuffer.writeU32(0x00000001);
  }
  
  writeSection(id: i32, payload: ByteBuffer): void {
    this.mainBuffer.writeByte(id);
    this.mainBuffer.writeULEB128(payload.length);
    this.mainBuffer.writeBuffer(payload);
  }

  emitTypeSectionStart(count: i32): void {
    if (count == 0) return;
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitRecGroupStart(count: i32): void {
    if (this.currentSection == null) return;
    if (count > 1) {
      (this.currentSection as ByteBuffer).writeByte(0x4E); // 0x4E = rec form
      (this.currentSection as ByteBuffer).writeULEB128(count);
    }
  }

  emitRecGroupEnd(): void {}
  
  emitFunctionTypeStart(index: i32): void {
    if (this.currentSection == null) return;
      (this.currentSection as ByteBuffer).writeByte(0x60);
    }

  emitFunctionTypeParamsStart(count: i32): void {
    if (this.currentSection == null) return;
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }
  
  emitFunctionTypeParam(type: ValType): void {
    if (this.currentSection == null) return;
    emitValType(this.currentSection as ByteBuffer, type);
  }
  
  emitFunctionTypeResultsStart(count: i32): void {
    if (this.currentSection == null) return;
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }
  
  emitFunctionTypeResult(type: ValType): void {
    if (this.currentSection == null) return;
    emitValType(this.currentSection as ByteBuffer, type);
  }
  
  emitFunctionTypeEnd(): void {}

  emitStructTypeStart(index: i32, name: String | null, superTypeIndex: i32,
      isFinal: boolean): void {
    if (this.currentSection == null) {
      return;
    }
    if (this.debugNames && name != null) {
      this.#typeNameIndices.push(index);
      this.#typeNames.push(name);
    }
    // 0x4F is `sub final`, 0x50 the open `sub`. Final is the default
    // here because almost every struct is a leaf, and it is what keeps
    // a cast against one inline in wasmtime (struct-finality.md).
    (this.currentSection as ByteBuffer).writeByte(if (isFinal) 0x4F else 0x50);
    if (superTypeIndex >= 0) {
      (this.currentSection as ByteBuffer).writeULEB128(1); // 1 supertype
      (this.currentSection as ByteBuffer).writeULEB128(superTypeIndex); // index of supertype
    } else {
      (this.currentSection as ByteBuffer).writeULEB128(0); // 0 supertypes
    }
    // 0x5F is the type form for struct in Wasm GC
    (this.currentSection as ByteBuffer).writeByte(0x5F);
  }

  emitStructTypeFieldsStart(count: i32): void {
    if (this.currentSection == null) {
      return;
    }
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitStructTypeField(type: ValType, isMutable: boolean): void {
    if (this.currentSection == null) {
      return;
    }
    emitValType(this.currentSection as ByteBuffer, type);
    (this.currentSection as ByteBuffer).writeByte(if (isMutable) 1 else 0);
  }

  emitStructTypeEnd(): void {}
  
  emitTypeSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Type as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
    }
  }

  emitImportSectionStart(count: i32): void {
    if (count == 0) return;
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }
  emitImport(item: ImportItem): void {
    let section = this.currentSection;
    if (section != null) {
      writeWasmString(section, item.module);
      writeWasmString(section, item.name);
      writeImportDesc(section, item.desc);
    }
  }
  emitImportGroup(module: String, items: GrowableArray<ImportItem>): void {
    let section = this.currentSection;
    if (section != null) {
      writeWasmString(section, module);
      section.writeByte(0x00);
      section.writeByte(0x7F);
      section.writeULEB128(items.length);
      for (let {name, desc} in items) {
        writeWasmString(section, name);
        writeImportDesc(section, desc);
      }
    }
  }
  emitImportTypeGroup(module: String, desc: ImportDesc, items: GrowableArray<ImportItem>): void {
    let section = this.currentSection;
    if (section != null) {
      writeWasmString(section, module);
      section.writeByte(0x00);
      section.writeByte(0x7E);
      writeImportDesc(section, desc);
      section.writeULEB128(items.length);
      for (let {name} in items) {
        writeWasmString(section, name);
      }
    }
  }
  emitImportSectionEnd(): void {
    let section = this.currentSection;
    if (section != null) {
      this.writeSection(SectionId.Import as i32, section);
      this.currentSection = null;
    }
  }

  emitGlobalSectionStart(count: i32): void {
    if (count == 0) {
      return;
    }
    this.currentSection = new ByteBuffer();
    this.currentBuffer = this.currentSection;
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitGlobal(type: ValType, isMutable: boolean): void {
    let section = this.currentSection;
    if (section != null) {
      emitValType(section, type);
      section.writeByte(if (isMutable) 1 else 0);
    }
  }

  emitGlobalEnd(): void {
    let section = this.currentSection;
    if (section != null) {
      section.writeByte(Opcode.end as i32);
    }
  }

  emitGlobalSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Global as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
      this.currentBuffer = null;
    }
  }

  emitFunctionSectionStart(count: i32): void {
    if (count == 0) return;
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitFunctionDefinition(typeIndex: i32): void {
    let {currentSection} = this;
    if (currentSection != null) {
      currentSection.writeULEB128(typeIndex);
    }
  }

  emitFunctionSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Function as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
    }
  }
  
  emitExportSectionStart(count: i32): void {
    if (count == 0) {
      return;
    }
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitExport(name: String, func: WasmFunction): void {
    let section = this.currentSection;

    if (section == null) {
      return;
    }
    
    writeWasmString(section, name);
    section.writeByte(0); // func
    section.writeULEB128(func.index);
  }

  emitExportGlobal(name: String, globalIndex: i32): void {
    let section = this.currentSection;
    if (section == null) {
      return;
    }
    
    writeWasmString(section, name);
    section.writeByte(3); // global
    section.writeULEB128(globalIndex);
  }

  emitExportTag(name: String, tagIndex: i32): void {
    let section = this.currentSection;
    if (section == null) {
      return;
    }
    
    writeWasmString(section, name);
    section.writeByte(4); // tag
    section.writeULEB128(tagIndex);
  }

  emitExportSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Export as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
    }
  }
  
  emitElementSection(indices: GrowableArray<i32>): void {
    if (indices.length == 0) return;
    let section = new ByteBuffer();
    section.writeULEB128(1); // 1 segment
    section.writeULEB128(3); // flags: declarative (3)
    section.writeULEB128(0); // elemkind: func (0)
    section.writeULEB128(indices.length);
    for (let idx in indices) {
      section.writeULEB128(idx);
    }
    this.writeSection(SectionId.Element as i32, section);
  }

  emitTagSectionStart(count: i32): void {
    if (count == 0) return;
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }

  emitTagDefinition(index: i32, typeIndex: i32): void {
    let {currentSection} = this;
    if (currentSection != null) {
      currentSection.writeByte(0); // attribute: 0 = exception
      currentSection.writeULEB128(typeIndex);
    }
  }

  emitTagSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Tag as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
    }
  }
  
  emitStartSection(funcIndex: i32): void {
    let payload = new ByteBuffer();
    payload.writeULEB128(funcIndex);
    this.writeSection(SectionId.Start as i32, payload);
  }

  emitCodeSectionStart(count: i32): void {
    if (count == 0) return;
    this.currentSection = new ByteBuffer();
    (this.currentSection as ByteBuffer).writeULEB128(count);
  }
  
  emitFunctionCodeStart(func: WasmFunction): void {
    this.currentBuffer = new ByteBuffer();
    this.functionsList.push(func);
    while (this.#removableOffsets.length > 0) {
      this.#removableOffsets.pop();
      this.#removableLocals.pop();
    }
    this.#graduatedLocals = null;
  }
  
  emitFunctionCodeEnd(func: WasmFunction): void {
    let funcBody = new ByteBuffer();
    let locals = this.currentLocals;
    if (locals != null && locals.length > 0) {
      funcBody.writeULEB128(locals.length);
      for (let l in locals) {
        funcBody.writeULEB128(1); // count 1
        // Emitted EXACTLY as given: the ZIR emitter widens ref locals
        // to nullable itself and graduates provably-initialized ones
        // back to non-null (#graduateNonNullLocals) — a blanket
        // widening here would silently disagree with the re-assert
        // decisions made against localTypes.
        emitValType(funcBody, l);
      }
    } else {
      funcBody.writeULEB128(0); // local count 0
    }
    
    let currFunc = this.currentBuffer as ByteBuffer;
    // Copy the body, skipping the one-byte removable ref.as_non_null
    // asserts whose local graduated non-null (dropAssertsForLocals).
    let graduated = this.#graduatedLocals;
    if (graduated == null) {
      funcBody.writeBuffer(currFunc);
    } else {
      var pos = 0;
      var r = 0;
      while (r < this.#removableOffsets.length) {
        let l = this.#removableLocals[r];
        if (l < graduated.length && graduated[l]) {
          let off = this.#removableOffsets[r];
          funcBody.writeBufferRange(currFunc, pos, off);
          pos = off + 1;
        }
        r += 1;
      }
      funcBody.writeBufferRange(currFunc, pos, currFunc.length);
    }

    let section = this.currentSection as ByteBuffer;
    section.writeULEB128(funcBody.length);
    section.writeBuffer(funcBody);
    
    this.currentBuffer = null;
    this.currentLocals = null;
  }
  
  emitCodeSectionEnd(): void {
    if (this.currentSection != null) {
      this.writeSection(SectionId.Code as i32, this.currentSection as ByteBuffer);
      this.currentSection = null;
    }
  }

  emitModuleEnd(): void {
    if (!this.debugNames) {
      return;
    }
    let namedFuncs = new Array<WasmFunction>();
    for (let f in this.functionsList) {
      var name = f.name;
      if (name == null && f.methodName != null) {
        name = f.methodName;
      }
      if (name != null) {
        namedFuncs.push(f);
      }
    }

    if (namedFuncs.length > 0) {
      let payload = new ByteBuffer();
      // Write section name: "name"
      payload.writeULEB128(4);
      payload.writeByte(110); // 'n'
      payload.writeByte(97);  // 'a'
      payload.writeByte(109); // 'm'
      payload.writeByte(101); // 'e'

      // Subsection 1: Function names
      let subPayload = new ByteBuffer();
      subPayload.writeULEB128(namedFuncs.length);
      for (let f in namedFuncs) {
        subPayload.writeULEB128(f.index);
        
        var name = f.name;
        if (name == null && f.methodName != null) {
          name = f.methodName;
        }
        let nameStr = name as String;
        writeWasmString(subPayload, nameStr);
      }

      payload.writeByte(1); // subsection id for function names
      payload.writeULEB128(subPayload.length);
      payload.writeBuffer(subPayload);

      // Subsection 4: type names. Subsections go in ascending id order.
      if (this.#typeNames.length > 0) {
        let typePayload = new ByteBuffer();
        typePayload.writeULEB128(this.#typeNames.length);
        for (var t = 0; t < this.#typeNames.length; t += 1) {
          typePayload.writeULEB128(this.#typeNameIndices[t]);
          writeWasmString(typePayload, this.#typeNames[t]);
        }
        payload.writeByte(4);
        payload.writeULEB128(typePayload.length);
        payload.writeBuffer(typePayload);
      }

      this.writeSection(0, payload); // section id 0 for custom sections
    }
  }

  // Function lifecycle
  emitFunctionSignatureStart(name: String, typeIndex: i32): void {}
  emitFunctionLocalsStart(locals: GrowableArray<ValType>): void {
    this.currentLocals = locals;
  }
  
  emitFunctionEnd(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.end as i32);
  }
    // Control flow
  emitUnreachable(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.unreachable as i32);
  }

  emitBlock(resultType: ValType): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.block as i32);
    emitValType(b, resultType);
  }

  emitLoop(resultType: ValType): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.loop as i32);
    emitValType(b, resultType);
  }

  emitIf(resultType: ValType): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.if_ as i32);
    emitValType(b, resultType);
  }

  emitElse(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.else_ as i32);
  }

  emitEnd(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.end as i32);
  }

  emitBr(depth: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.br as i32);
    b.writeULEB128(depth);
  }

  emitBrIf(depth: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.br_if as i32);
    b.writeULEB128(depth);
  }

  emitThrow(tagIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.throw as i32);
    b.writeULEB128(tagIndex);
  }

  emitTryTableCatch(resultType: ValType, tagIndex: i32, destDepth: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.try_table as i32);
    emitValType(b, resultType);
    b.writeULEB128(1); // catch count
    b.writeByte(0);    // catch kind: 0 = catch
    b.writeULEB128(tagIndex);
    b.writeULEB128(destDepth);
  }

  emitReturn(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.return_ as i32);
  }
  
  emitCall(target: WasmFunction): void {
    if (target.index < 0) {
      throw new Error(`Invalid function call: function index < 0 for: ${if (target.name != null) (target.name as String) else "unnamed"} isReached=${if (target.isReached) "true" else "false"}`);
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.call as i32);
    b.writeULEB128(target.index);
  }

  emitReturnCall(target: WasmFunction): void {
    if (target.index < 0) {
      throw new Error(`Invalid tail call: function index < 0 for: ${if (target.name != null) (target.name as String) else "unnamed"} isReached=${if (target.isReached) "true" else "false"}`);
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.return_call as i32);
    b.writeULEB128(target.index);
  }
  
  emitI32Const(value: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.i32_const as i32);
    b.writeSLEB128(value);
  }
  
  emitF64Const(value: f64): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.f64_const as i32);
    b.writeF64(value);
  }

  emitF32Const(value: f64): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.f32_const as i32);
    b.writeF32(value as f32);
  }

  emitBooleanConst(value: boolean): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.i32_const as i32);
    let v = if (value) 1 else 0;    
    b.writeSLEB128(v);
  }
  
  emitStringConst(value: String): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.i32_const as i32);
    b.writeSLEB128(0);
  }

  emitDrop(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.drop as i32);
  }

  // GC operations
  emitStructNew(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.struct_new as i32);
    b.writeULEB128(typeIndex);
  }

  emitStructNewDefault(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.struct_new_default as i32);
    b.writeULEB128(typeIndex);
  }

  emitStructGet(typeIndex: i32, fieldIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.struct_get as i32);
    b.writeULEB128(typeIndex);
    b.writeULEB128(fieldIndex);
  }

  emitStructSet(typeIndex: i32, fieldIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.struct_set as i32);
    b.writeULEB128(typeIndex);
    b.writeULEB128(fieldIndex);
  }

  emitRefCastEq(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast as i32);
    b.writeSLEB128(heapTypeToSigned(HeapType.eq));
  }

  emitRefCastEqNull(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast_null as i32);
    b.writeSLEB128(heapTypeToSigned(HeapType.eq));
  }

  emitRefCastStructNull(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast_null as i32);
    b.writeSLEB128(heapTypeToSigned(HeapType.struct));
  }

  emitRefCastNullNone(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast_null as i32);
    b.writeSLEB128(heapTypeToSigned(HeapType.none));
  }

  emitRefCast(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast as i32);
    b.writeSLEB128(typeIndex);
  }

  emitRefCastNull(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_cast_null as i32);
    b.writeSLEB128(typeIndex);
  }

  emitRefTest(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_test as i32);
    b.writeSLEB128(typeIndex);
  }

  emitRefTestNull(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.ref_test_null as i32);
    b.writeSLEB128(typeIndex);
  }

  emitRefNull(heapType: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.ref_null as i32);
    b.writeSLEB128(heapType);
  }

  emitAnyConvertExtern(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.any_convert_extern as i32);
  }

  emitExternConvertAny(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.extern_convert_any as i32);
  }

  // Variables
  emitLocalGet(index: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.local_get as i32);
    b.writeULEB128(index);
  }

  emitLocalSet(index: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.local_set as i32);
    b.writeULEB128(index);
  }
  
  emitLocalTee(index: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.local_tee as i32);
    b.writeULEB128(index);
  }

  emitGlobalGet(index: i32): void {
    if (index < 0) {
      throw new Error("Invalid global index < 0 for emitGlobalGet");
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.global_get as i32);
    b.writeULEB128(index);
  }

  emitCallRef(typeIndex: i32): void {
    if (typeIndex < 0) {
      throw new Error("Invalid call_ref: typeIndex < 0");
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.call_ref as i32);
    b.writeULEB128(typeIndex);
  }

  emitReturnCallRef(typeIndex: i32): void {
    if (typeIndex < 0) {
      throw new Error("Invalid return_call_ref: typeIndex < 0");
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.return_call_ref as i32);
    b.writeULEB128(typeIndex);
  }

  emitRefFunc(index: i32): void {
    if (index < 0) {
      throw new Error("Invalid ref_func: index < 0");
    }
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.ref_func as i32);
    b.writeULEB128(index);
  }

  emitRefEq(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.ref_eq as i32);
  }

  emitRefIsNull(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.ref_is_null as i32);
  }

  emitRefAsNonNull(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.ref_as_non_null as i32);
  }

  emitRemovableRefAsNonNull(local: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    this.#removableOffsets.push(b.length);
    this.#removableLocals.push(local);
    b.writeByte(Opcode.ref_as_non_null as i32);
  }

  dropAssertsForLocals(graduated: GrowableArray<boolean>): void {
    this.#graduatedLocals = graduated;
  }

  emitGlobalSet(index: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.global_set as i32);
    b.writeULEB128(index);
  }

  emitI32Add(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_add as i32); }
  emitI32Sub(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_sub as i32); }
  emitI32Mul(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_mul as i32); }
  emitI32DivS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_div_s as i32); }
  emitI32DivU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_div_u as i32); }
  emitI32Eq(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_eq as i32); }
  emitI32Eqz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_eqz as i32); }
  emitI32Ne(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_ne as i32); }
  emitI32LtS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_lt_s as i32); }
  emitI32LtU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_lt_u as i32); }
  emitI32LeS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_le_s as i32); }
  emitI32LeU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_le_u as i32); }
  emitI32GtS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_gt_s as i32); }
  emitI32GtU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_gt_u as i32); }
  emitI32GeS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_ge_s as i32); }
  emitI32GeU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_ge_u as i32); }
  emitI32RemS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_rem_s as i32); }
  emitI32RemU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_rem_u as i32); }
  emitI32And(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_and as i32); }
  emitI32Or(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_or as i32); }
  emitI32Xor(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_xor as i32); }
  emitI32Shl(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_shl as i32); }
  emitI32ShrS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_shr_s as i32); }
  emitI32ShrU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_shr_u as i32); }
  emitI32Clz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_clz as i32); }
  emitI32Ctz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_ctz as i32); }
  emitI32Popcnt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_popcnt as i32); }

  emitF64Add(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_add as i32); }
  emitF64Sub(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_sub as i32); }
  emitF64Mul(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_mul as i32); }
  emitF64Div(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_div as i32); }
  emitF64Eq(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_eq as i32); }
  emitF64Ne(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_ne as i32); }
  emitF64Lt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_lt as i32); }
  emitF64Gt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_gt as i32); }
  emitF64Le(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_le as i32); }
  emitF64Ge(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_ge as i32); }
  emitF64ConvertI32S(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_convert_i32_s as i32); }

  emitF32Add(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_add as i32); }
  emitF32Sub(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_sub as i32); }
  emitF32Mul(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_mul as i32); }
  emitF32Div(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_div as i32); }
  emitF32Eq(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_eq as i32); }
  emitF32Ne(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_ne as i32); }
  emitF32Lt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_lt as i32); }
  emitF32Gt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_gt as i32); }
  emitF32Le(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_le as i32); }
  emitF32Ge(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_ge as i32); }
  emitF32ConvertI32S(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_convert_i32_s as i32); }

  emitMemorySize(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.memory_size as i32);
    (this.currentBuffer as ByteBuffer).writeByte(0);
  }
  emitMemoryGrow(): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.memory_grow as i32);
    (this.currentBuffer as ByteBuffer).writeByte(0);
  }
  emitI32Load(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_load as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitI32Store(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_store as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitI32Load8U(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_load8_u as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitI32Store8(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_store8 as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitI64Load(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_load as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitI64Store(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_store as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitF32Load(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_load as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitF32Store(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_store as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitF64Load(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_load as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }
  emitF64Store(align: i32, offset: i32): void {
    (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_store as i32);
    (this.currentBuffer as ByteBuffer).writeULEB128(align);
    (this.currentBuffer as ByteBuffer).writeULEB128(offset);
  }

  /**
   * The `0xFD` prefix and the ULEB128 opcode every vector instruction
   * starts with. Half the set has an opcode above 127, so writing it as
   * a single byte would truncate `i32x4.add` (0xAE) to a different
   * instruction rather than fail — hence ULEB128, always.
   */
  #simdPrefix(opcode: i32): ByteBuffer {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.simd_prefix as i32);
    b.writeULEB128(opcode);
    return b;
  }

  emitSimd(opcode: i32): void {
    this.#simdPrefix(opcode);
  }

  emitWideArith(opcode: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeULEB128(opcode);
  }

  emitSimdLane(opcode: i32, lane: i32): void {
    let b = this.#simdPrefix(opcode);
    b.writeByte(lane);
  }

  emitSimdMem(opcode: i32, align: i32, offset: i32): void {
    let b = this.#simdPrefix(opcode);
    b.writeULEB128(align);
    b.writeULEB128(offset);
  }

  emitSimdMemLane(opcode: i32, align: i32, offset: i32, lane: i32): void {
    let b = this.#simdPrefix(opcode);
    b.writeULEB128(align);
    b.writeULEB128(offset);
    b.writeByte(lane);
  }

  emitSimdBytes(opcode: i32, bytes: GrowableArray<i32>): void {
    let b = this.#simdPrefix(opcode);
    for (let byte in bytes) {
      b.writeByte(byte);
    }
  }

  emitArrayType(index: i32, elementType: ValType, isMutable: boolean,
      superTypeIndex: i32, isFinal: boolean): void {
    let b = this.currentSection;
    if (b == null) {
      return;
    }
    // Only an immutable array of references declares a supertype — the
    // array of its element's supertype, which is what makes an
    // immutable array of a subclass a subtype of its superclass's array
    // (WasmArray.superArray). Declaring one and being one are separate:
    // the array is open only when another type in this module names it
    // as a supertype.
    b.writeByte(if (isFinal) 0x4F else 0x50);
    if (superTypeIndex >= 0) {
      b.writeULEB128(1);
      b.writeULEB128(superTypeIndex);
    } else {
      b.writeULEB128(0);
    }
    b.writeByte(0x5E); // array form
    emitValType(b, elementType);
    b.writeByte(if (isMutable) 1 else 0);
  }

  emitMemorySection(minPages: i32): void {
    let payload = new ByteBuffer();
    payload.writeULEB128(1); // 1 memory
    payload.writeByte(0); // limits: min only
    payload.writeULEB128(minPages);
    this.writeSection(SectionId.Memory as i32, payload);
  }

  emitExportMemory(name: String, memIndex: i32): void {
    let section = this.currentSection;
    if (section == null) {
      return;
    }
    writeWasmString(section, name);
    section.writeByte(2); // memory export kind
    section.writeULEB128(memIndex);
  }

  emitDataCountSection(count: i32): void {
    if (count == 0) return;
    let payload = new ByteBuffer();
    payload.writeULEB128(count);
    this.writeSection(SectionId.DataCount as i32, payload);
  }

  emitDataSection(datas: GrowableArray<ByteBuffer>, rawDatas: GrowableArray<ByteBuffer>): void {
    if (datas.length == 0) return;
    let payload = new ByteBuffer();
    payload.writeULEB128(datas.length);
    for (let data in datas) {
      payload.writeBuffer(data);
    }
    this.writeSection(SectionId.Data as i32, payload);
  }

  emitArrayNew(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_new as i32);
    b.writeULEB128(typeIndex);
  }

  emitArrayNewDefault(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_new_default as i32);
    b.writeULEB128(typeIndex);
  }

  emitArrayNewFixed(typeIndex: i32, count: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_new_fixed as i32);
    b.writeULEB128(typeIndex);
    b.writeULEB128(count);
  }

  emitArrayNewData(typeIndex: i32, dataIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_new_data as i32);
    b.writeULEB128(typeIndex);
    b.writeULEB128(dataIndex);
  }

  emitArrayGet(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_get as i32);
    b.writeULEB128(typeIndex);
  }

  emitArrayGetU(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_get_u as i32);
    b.writeULEB128(typeIndex);
  }

  emitArrayGetS(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_get_s as i32);
    b.writeULEB128(typeIndex);
  }

  emitArraySet(typeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_set as i32);
    b.writeULEB128(typeIndex);
  }

  emitArrayLen(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_len as i32);
  }

  emitArrayCopy(destTypeIndex: i32, srcTypeIndex: i32): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.gc_prefix as i32);
    b.writeULEB128(GcOpcode.array_copy as i32);
    b.writeULEB128(destTypeIndex);
    b.writeULEB128(srcTypeIndex);
  }

  emitI64Const(value: i64): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.i64_const as i32);
    b.writeSLEB128_64(value);
  }

  emitI64Add(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_add as i32); }
  emitI64Sub(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_sub as i32); }
  emitI64Mul(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_mul as i32); }
  emitI64DivS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_div_s as i32); }
  emitI64DivU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_div_u as i32); }
  emitI64RemS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_rem_s as i32); }
  emitI64RemU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_rem_u as i32); }
  emitI64And(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_and as i32); }
  emitI64Or(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_or as i32); }
  emitI64Xor(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_xor as i32); }
  emitI64Shl(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_shl as i32); }
  emitI64ShrS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_shr_s as i32); }
  emitI64ShrU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_shr_u as i32); }

  emitI64Eqz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_eqz as i32); }
  emitI64Eq(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_eq as i32); }
  emitI64Ne(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_ne as i32); }
  emitI64LtS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_lt_s as i32); }
  emitI64LtU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_lt_u as i32); }
  emitI64LeS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_le_s as i32); }
  emitI64LeU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_le_u as i32); }
  emitI64GtS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_gt_s as i32); }
  emitI64GtU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_gt_u as i32); }
  emitI64GeS(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_ge_s as i32); }
  emitI64GeU(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_ge_u as i32); }
  emitI64Clz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_clz as i32); }
  emitI64Ctz(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_ctz as i32); }
  emitI64Popcnt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_popcnt as i32); }
  emitF32Neg(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_neg as i32); }
  emitF64Neg(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_neg as i32); }
  emitF32Ceil(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_ceil as i32); }
  emitF64Ceil(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_ceil as i32); }
  emitF32Floor(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_floor as i32); }
  emitF64Floor(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_floor as i32); }
  emitF32Trunc(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_trunc as i32); }
  emitF64Trunc(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_trunc as i32); }
  emitF32Sqrt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_sqrt as i32); }
  emitF64Sqrt(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_sqrt as i32); }
  emitF32Min(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_min as i32); }
  emitF64Min(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_min as i32); }
  emitF32Max(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_max as i32); }
  emitF64Max(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_max as i32); }
  emitF32Copysign(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_copysign as i32); }
  emitF64Copysign(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_copysign as i32); }
  emitF32Abs(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_abs as i32); }
  emitF64Abs(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_abs as i32); }
  emitF32Nearest(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_nearest as i32); }
  emitF64Nearest(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_nearest as i32); }

  emitI64TruncSatF32S(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i64_trunc_sat_f32_s as i32);
  }
  emitI64TruncSatF32U(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i64_trunc_sat_f32_u as i32);
  }
  emitI64TruncSatF64S(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i64_trunc_sat_f64_s as i32);
  }
  emitI64TruncSatF64U(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i64_trunc_sat_f64_u as i32);
  }

  emitI32TruncSatF32S(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i32_trunc_sat_f32_s as i32);
  }
  emitI32TruncSatF32U(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i32_trunc_sat_f32_u as i32);
  }
  emitI32TruncSatF64S(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i32_trunc_sat_f64_s as i32);
  }
  emitI32TruncSatF64U(): void {
    let b = this.currentBuffer as ByteBuffer;
    b.writeByte(Opcode.sat_prefix as i32);
    b.writeByte(SatOpcode.i32_trunc_sat_f64_u as i32);
  }

  emitI64ReinterpretF64(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_reinterpret_f64 as i32); }
  emitF64ReinterpretI64(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_reinterpret_i64 as i32); }
  emitI32ReinterpretF32(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_reinterpret_f32 as i32); }
  emitF32ReinterpretI32(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_reinterpret_i32 as i32); }

  emitI64ExtendI32S(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_extend_i32_s as i32); }
  emitI64ExtendI32U(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i64_extend_i32_u as i32); }
  emitI32WrapI64(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.i32_wrap_i64 as i32); }
  emitF32ConvertI64S(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_convert_i64_s as i32); }
  emitF64ConvertI64S(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_convert_i64_s as i32); }
  emitF32ConvertI32U(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_convert_i32_u as i32); }
  emitF64ConvertI32U(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_convert_i32_u as i32); }
  emitF32ConvertI64U(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_convert_i64_u as i32); }
  emitF64ConvertI64U(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_convert_i64_u as i32); }
  emitF64PromoteF32(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f64_promote_f32 as i32); }
  emitF32DemoteF64(): void { (this.currentBuffer as ByteBuffer).writeByte(Opcode.f32_demote_f64 as i32); }
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19