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

59.6 KiB Raw
import { collectValueExports } from './lookup.zena';
import {
  ValTypeI32, ValTypeI64, ValTypeF32, ValTypeF64, ValTypeV128, ValTypeRefNull, ValTypeStructRef,
  ValTypeRef, ValTypeI8, ValTypeI16, ValTypeFuncRef, ValTypeExternRef, ValTypeAnyRef,
  ValTypeEqRef, ValTypeOptRef, ValTypeVoid, ValTypeFunc, ValTypeNullRef,
  heapTypeToSigned, HeapType, isRefValType,
} from './wasm.zena';
import { simdInstruction } from '../simd.zena';
import {
  WasmModule, WasmFunction, WasmSignature, WasmExport, WasmGlobal, WasmStruct, VTableInit, WasmArray, StringLiteralLocation,
} from './wasm-module.zena';
import { ByteBuffer, growable } from 'zena:core';
import { ReachabilityAnalysis } from './reachability/analysis.zena';
import { rowInstantiationCounts, rowInstantiationReport } from './row-stats.zena';
import { prepareGenerators } from './ir/generators.zena';
import { prepareAsyncFunctions } from './ir/async.zena';
import { explodeRecordParameters } from './ir/explode.zena';
import {
  prepareComponentEntry, prepareComponentExports, prepareSyncExports,
  prepareResourceDestructors
} from './ir/component-adapters.zena';
import { getClassKey, getInterfaceKey, sortStringsByLengthDescending, parseFloatLiteral, parseI64OrU64Literal, parseI32WithHex } from './codegen-utils.zena';
import { ClassType, TypeInterner, markTypes } from '../types.zena';
import { Program } from '../program.zena';
import {
  NumberLiteral, StringLiteral, BooleanLiteral, EnumDeclaration, ImportDeclaration, Module,
  VariableDeclaration, IdentifierPattern, FunctionDeclaration,
} from '../ast.zena';
import { isWebUiProtocolLibrary } from '../stdlib-manifest.zena';
import { Symbol } from '../scope/symbol.zena';
import {
  ValType, resetWasmTypeUids, FuncImport, GlobalImport, MemoryImport,
} from './wasm.zena';
import {
  SingleImport, ImportGroup, ImportTypeGroup, planImports,
} from './import-plan.zena';
import { parseF64, parseI64, i32ToString } from 'zena:core';

import { WasmEmitter, ImportItem } from './wasm-emitter.zena';
import { performance, nowMs } from '../time.zena';
import { TemplateStats } from './template-stats.zena';
import { DiscoveryCounts, censusReport } from './ir/census.zena';
import { propagationReport, runPropagationDevirt } from './ir/propagate.zena';
import { dirName, normalizePath } from '../module-resolver.zena';
import { lowerFunction, lowerWithTemplates } from './ir/lowering.zena';
import { TemplateFamilies } from './ir/template.zena';
import { IrBody, IrScratch } from './ir/ir.zena';
import { verifyIr } from './ir/verifier.zena';
import { runGvn } from './ir/gvn.zena';
import { runSimplify } from './ir/simplify.zena';
import { runBlockCleanup } from './ir/blockmerge.zena';
import { runDce } from './ir/dce.zena';
import { runInline } from './ir/inline.zena';
import { DevirtContext, runDevirt } from './ir/devirt.zena';
import { EscapeSummaries } from './ir/escape.zena';
import { pruneUnreached } from './ir/harvest.zena';
import { runLicm } from './ir/licm.zena';
import { runSccp } from './ir/sccp.zena';
import { runSroa } from './ir/sroa.zena';
import { runJumpThreading } from './ir/thread.zena';
import { runSpecialize } from './ir/specialize.zena';
import { pruneTypes } from './ir/typeprune.zena';
import { eliminateDeadFields, removeEmptyCalls } from './ir/deadfields.zena';
import { eliminateDeadSlots } from './ir/deadslots.zena';
import { printIr } from './ir/printer.zena';
import { IrCfg } from './ir/cfg.zena';
import { emitZirFunction } from './ir/emit.zena';
import {
  ComponentShape, ImportTypeEncoder, componentEntryExport,
  componentCallbackExport, componentReallocExport,
  componentPostReturnExport, componentMemoryExport,
  componentRuntimeNamespace
} from './component-emitter.zena';
import { buildComponentShape } from './component-shape.zena';
import {
  defaultImportedStringConstants, targetEmitsComponent, targetUsesJsStrings
} from '../targets.zena';

export final class ModuleGenerator {
  program: Program;
  emitter: WasmEmitter;
  target: String;
  /** ZENA_ZIR_STATS=1: report the per-module lowered-function count. */
  zirStats: boolean;
  /**
   * What a component would wrap this module in — its WIT imports and
   * lifted exports — collected on a target that emits one, and null
   * otherwise. `BinaryGenerator` reads it after `compile()`; it is
   * derived from the same `WasmModule` the module was emitted from, so
   * the two cannot disagree about what exists.
   */
  var componentShape: ComponentShape | null = null;
  /**
   * The WIT import encoder, injected by the driver on the component
   * target (`wit-imports.zena` builds it). Null elsewhere — and in
   * hosts like the language service that never emit a component and
   * deliberately do not link the WIT parser.
   */
  var importEncoder: ImportTypeEncoder | null = null;
  /**
   * Emit the wide-arithmetic proposal's instructions natively rather
   * than the sequences they replace (`WasmModule.wideArithmetic`).
   */
  var wideArithmetic: boolean = false;

  /** `WasmModule.debugContracts`. */
  var debugContracts: boolean = false;
  /** `WasmModule.importedStringConstants`. */
  var importedStringConstants = defaultImportedStringConstants;
  /**
   * Group imports in the encodings of the compact import section
   * proposal (`planImports`), reordering them to do so. Off by default:
   * V8 reads them only behind `--experimental-wasm-compact-imports`,
   * and no engine ships them.
   */
  var compactImports = false;

  /**
   * Make the bodies of a generic class's methods from one template per
   * family (ir/template.zena, docs/design/template-zir.md). The emitted
   * module is the same either way; `ZENA_TEMPLATES=0` turns it off to
   * measure it.
   */
  var templates = true;

  /**
   * Lower every body made from a template directly as well, and fail
   * the compile on any difference (`ZENA_TEMPLATE_CHECK=1`).
   */
  var templateCheck = false;

  /** After `compile()`: the templates the module's bodies were made from, for tests. */
  var templateFamilies: TemplateFamilies | null = null;

  /**
   * At `-O2`, make the `call_ref`s whose callee interprocedural type
   * propagation (ir/propagate.zena) narrows to one function direct
   * (docs/design/type-propagation.md). `ZENA_PROPAGATE=0` turns it off,
   * to measure it.
   */
  var propagate = true;

  /**
   * The checker's interned types, for the module to build unions
   * through; null when the generator is used without a compile.
   */
  var interner: TypeInterner | null = null;

  /**
   * Optimization level (docs/design/optimization-pipeline.md). 0 =
   * lower + GVN + emit; 1 (the default) adds the per-function cleanup
   * passes; 2 is reserved for the module loop and currently equals 1.
   */
  optLevel: i32;

  new(this.program, this.emitter, this.target = "core", this.zirStats = false,
      this.optLevel = 1);

  /**
   * The zero of a value type: 0 for a number, a null for a reference. A
   * null names its heap type, and for an abstract one that is the
   * reference type's own code as a negative number (`eqref`, 0x6D, is
   * `ref.null -19`), so each abstract type gets its own null.
   */
  emitDefaultValue(type: ValType): void {
    match (type) {
      // Packed field types are written as i32s.
      case ValTypeI32: { this.emitter.emitI32Const(0); }
      case ValTypeI8: { this.emitter.emitI32Const(0); }
      case ValTypeI16: { this.emitter.emitI32Const(0); }
      case ValTypeI64: { this.emitter.emitI64Const(0 as i64); }
      case ValTypeF64: { this.emitter.emitF64Const(0.0); }
      case ValTypeF32: { this.emitter.emitF32Const(0.0); }
      case ValTypeV128: { this.emitZeroV128(); }
      case ValTypeRefNull as refNull: { this.emitter.emitRefNull(refNull.target.index); }
      case ValTypeRef as ref: { this.emitter.emitRefNull(ref.target.index); }
      case ValTypeFuncRef: { this.emitter.emitRefNull(-16); }
      case ValTypeExternRef: { this.emitter.emitRefNull(-17); }
      case ValTypeAnyRef: { this.emitter.emitRefNull(-18); }
      case ValTypeEqRef: { this.emitter.emitRefNull(-19); }
      case ValTypeOptRef: { this.emitter.emitRefNull(-20); }
      case ValTypeStructRef: { this.emitter.emitRefNull(-21); }
      case ValTypeNullRef: { this.emitter.emitRefNull(heapTypeToSigned(HeapType.none)); }
      case _: {
        throw new Error('a value of this type has no default: it is not a value type');
      }
    }
  }

  /**
   * An all-zero vector, the only constant expression a `v128` global or
   * field can be initialized with — there is no `v128` equivalent of
   * `i32.const`, only the 16-byte `v128.const`.
   */
  emitZeroV128(): void {
    let zeros = new Array<i32>();
    var i = 0;
    while (i < 16) {
      zeros.push(0);
      i += 1;
    }
    let instr = simdInstruction('v128.const');
    if (instr == null) {
      throw new Error('v128.const is missing from the SIMD table');
    }
    this.emitter.emitSimdBytes(instr.opcode, zeros);
  }

  /**
   * The module's function exports: the user's, the runtime's string
   * helpers, the async host entry points, and the component
   * machinery. Built before the optimization loop because harvest
   * roots liveness in this list, and the export section emits from
   * the same list afterwards.
   */
  #collectFunctionExports(wasm: WasmModule): GrowableArray<WasmExport> {
    let memoryImported =
        targetEmitsComponent(this.target) && wasm.hasMemory;
    let exports = new Array<WasmExport>();
    for (let k in wasm.exportMap.keys()) {
      let f = wasm.exportMap[k];
      exports.push(new WasmExport(k as String, f));
    }

    // Export synthesized runtime string helpers if present
    if (targetUsesJsStrings(this.target)) {
      let strCreate = wasm.getStdlibFunc('core', '$stringCreate');
      if (strCreate != null) {
        exports.push(new WasmExport("$stringCreate", strCreate));
      }
      let strGetHandle = wasm.getStdlibFunc('core', '$stringGetHandle');
      if (strGetHandle != null) {
        exports.push(new WasmExport("$stringGetHandle", strGetHandle));
      }
      let strGetLength = wasm.getStdlibFunc('core', '$stringGetLength');
      if (strGetLength != null) {
        exports.push(new WasmExport("$stringGetLength", strGetLength));
      }
    } else {
      let {stringCreateFunc} = wasm;
      if (stringCreateFunc != null) {
        exports.push(new WasmExport("$stringCreate", stringCreateFunc));
      }
      let {stringSetByteFunc} = wasm;
      if (stringSetByteFunc != null) {
        exports.push(new WasmExport("$stringSetByte", stringSetByteFunc));
      }
      let {stringGetByteFunc} = wasm;
      if (stringGetByteFunc != null) {
        exports.push(new WasmExport("$stringGetByte", stringGetByteFunc));
      }
      let {stringGetLengthFunc} = wasm;
      if (stringGetLengthFunc != null) {
        exports.push(new WasmExport("$stringGetLength", stringGetLengthFunc));
      }
    }

    // __zena_drain — the re-entry point for a host that cannot wait
    // synchronously (async.md §4). On such a host `drainMicrotasks()`
    // unwinds when the only remaining work is a future deadline, and
    // the host pings this to resume once the deadline arrives. Exported
    // on core-module targets whenever zena:async is reached.
    let asyncDrain = wasm.getStdlibFunc('async', 'drainMicrotasks');
    if (asyncDrain != null && !targetEmitsComponent(this.target)) {
      exports.push(new WasmExport("__zena_drain", asyncDrain));
    }

    // __zena_complete_* — how a host settles a future it was handed a
    // handle for (async.md §4, Level 2). Present exactly when the
    // program imports zena:js, whose exports RTA roots for this
    // reason; absent, and costing nothing, otherwise.
    let completeEntries = [
      ("__zena_complete_void", "completeVoid"),
      ("__zena_complete_i32", "completeI32"),
      ("__zena_complete_f64", "completeF64"),
      ("__zena_complete_string", "completeString"),
      ("__zena_complete_extern", "completeExtern"),
      ("__zena_complete_error", "completeError"),
    ];
    for (let entry in completeEntries) {
      let (exportName, zenaName) = entry;
      let f = wasm.getStdlibFunc('js', zenaName);
      if (f != null) {
        exports.push(new WasmExport(exportName, f));
      }
    }
    // The Wasm Web UI protocol (docs/design/wasm-web-ui.md): a
    // program that imports zena:web-templates or zena:web-elements exports their
    // `webui_*` functions under their own names, and the host calls them
    // to read results and drive elements. Present exactly when the
    // program imports the library, like the completion entries above,
    // and only on the js target: a server has no host to call them.
    for (let unit in this.program.units) {
      if (!targetUsesJsStrings(this.target) || !isWebUiProtocolLibrary(unit.ast.path)) {
        continue;
      }
      for (let stmt in unit.ast.body) {
        // An exported `function` or an exported `let` of an arrow:
        // reachability roots both (`webui_*` in analysis.zena).
        var name = '';
        if (stmt is FunctionDeclaration) {
          if (stmt.exported) {
            name = stmt.name.name;
          }
        } else if (stmt is VariableDeclaration) {
          if (stmt.exported && stmt.pattern is IdentifierPattern) {
            name = (stmt.pattern as IdentifierPattern).name;
          }
        }
        if (!name.startsWith('webui_')) {
          continue;
        }
        let f = wasm.getUnitFunc(unit.ast.path, name);
        if (f != null) {
          exports.push(new WasmExport(name, f));
        }
      }
    }
    let {componentEntryFunc} = wasm;
    if (componentEntryFunc != null) {
      exports.push(new WasmExport(componentEntryExport,
          componentEntryFunc));
    }
    let {componentCallbackFunc} = wasm;
    if (componentCallbackFunc != null) {
      exports.push(new WasmExport(componentCallbackExport,
          componentCallbackFunc));
    }
    // The async export adapters, by their own names: the component
    // aliases each out to lift it with the callback.
    for (let asyncExport in wasm.componentAsyncExports) {
      let adapterName = asyncExport.adapter.name;
      if (adapterName != null) {
        exports.push(new WasmExport(adapterName as String,
            asyncExport.adapter));
      }
    }
    // The sync export wrappers, likewise: lifted directly under the
    // WIT's type.
    for (let syncExport in wasm.componentSyncExports) {
      exports.push(new WasmExport(syncExport.coreName, syncExport.func));
    }
    // The resource destructors: core exports the component's destructor
    // shim is filled from, never lifted.
    for (let dtor in wasm.componentResourceDtors) {
      exports.push(new WasmExport(dtor.coreName, dtor.func));
    }
    // The wrappers, and the two functions the canonical options name.
    // They are core-module exports and not component ones: the component
    // aliases them back out to build its lifts, and nothing outside sees
    // them.
    // Walked through `exportMap` rather than over the wrapper map's own
    // keys, so the order is the one the user exports are already in: a
    // hash map's iteration order is stable for a given set of keys, but
    // tying the two together is what makes it obviously so.
    for (let wrapperKey in wasm.exportMap.keys()) {
      if (let (true, wrapper) = wasm.componentExportWrappers.get(wrapperKey)) {
        let wrapperName = wrapper.name;
        if (wrapperName == null) {
          throw new Error('a component export wrapper has no name');
        }
        exports.push(new WasmExport(wrapperName, wrapper));
      }
    }
    // `cabi_realloc` is not exported when the memory is imported: the
    // canonical options name the runtime memory module's own realloc,
    // which is the allocator this module already delegates to.
    let {componentReallocFunc} = wasm;
    if (componentReallocFunc != null && !memoryImported) {
      exports.push(new WasmExport(componentReallocExport,
          componentReallocFunc));
    }
    let {componentPostReturnFunc} = wasm;
    if (componentPostReturnFunc != null) {
      exports.push(new WasmExport(componentPostReturnExport,
          componentPostReturnFunc));
    }
    let {asyncMainStartFunc} = wasm;
    if (asyncMainStartFunc != null) {
      exports.push(new WasmExport("__zena_main_start", asyncMainStartFunc));
    }
    let {asyncMainResultFunc} = wasm;
    if (asyncMainResultFunc != null) {
      exports.push(new WasmExport("__zena_main_result", asyncMainResultFunc));
    }
    return exports;
  }

  compile(): void {
    // Uids are hashCode for WasmType and WasmFunction, so they order
    // every hash container keyed by one, and that order reaches the
    // emitted bytes. Restart the sequence per module so a module is
    // emitted the same whether or not others were compiled first.
    resetWasmTypeUids();

    let wasm = new WasmModule();
    wasm.target = this.target;
    wasm.program = this.program;
    wasm.wideArithmetic = this.wideArithmetic;
    wasm.debugContracts = this.debugContracts;
    wasm.importedStringConstants = this.importedStringConstants;
    let interner = this.interner;
    if (interner != null) {
      wasm.interner = interner;
    }


    let tDiscovery = performance.start("codegen-discovery");
    // The tuple types the checked code makes meet share a struct; joined
    // before discovery maps any of them (#1137).
    wasm.noteTupleJoins(this.program.units);
    let pass = new ReachabilityAnalysis(this.program, wasm, this.target);
    pass.run();
    if (this.zirStats) {
      // Before explosion splits functions, so each counts once.
      for (let line in rowInstantiationReport(rowInstantiationCounts(wasm))) {
        console.log(line);
      }
    }
    // Class structs now have their supertypes; immutable arrays of
    // them can declare theirs (lowering's conformance checks read it).
    wasm.linkArraySupertypes();
    // Every tuple type the module uses is registered, so each tuple
    // group's class fields can take their final types: the lowest
    // common ancestor of what its members hold there. First the joins
    // of the bodies discovery checked on demand.
    wasm.noteTupleJoins(this.program.units);
    wasm.settleTupleSlots();
    performance.end(tDiscovery);



    // Pre-discover all string literals inside reached function bodies so that they are registered in wasm.datas BEFORE layouts and sections are emitted
    if (!targetUsesJsStrings(this.target)) {
      for (let u in this.program.units) {
        let moduleNode = u.ast;
        for (let stmt in moduleNode.body) {
          if (stmt is EnumDeclaration) {
            for (let member in stmt.members) {
              let initializer = member.initializer;
              if (initializer is StringLiteral) {
                wasm.registerStringLiteral(initializer.value);
              }
            }
          }
        }
      }
    }


    markTypes("discovery");
    let tLayout = performance.start("codegen-layout");
    let {stringClass} = wasm;
    if (stringClass != null && !targetUsesJsStrings(this.target)) {
      if (wasm.stringLiterals.size > 0) {
        wasm.registerStringLiteral(""); // Ensure empty string is in stringLiterals
        let stringKey = getClassKey(stringClass);
        var stringStruct: WasmStruct | null = null;
        if (let (true, stringInfo) = wasm.classInfos.get(stringKey)) {
          stringStruct = stringInfo.structType;
        }
        if (stringStruct == null) {
          throw new Error("String class struct not found during module compilation!");
        }
        new ValTypeRefNull(stringStruct);

        // Sort stringLiterals by length descending
        let sortedStrings = new Array<String>();
        let litKeys = wasm.stringLiterals.keys();
        for (let val in litKeys) {
          sortedStrings.push(val);
        }
        sortStringsByLengthDescending(sortedStrings);

        // Subsequence optimization: place each literal at the first offset
        // where its bytes already occur in the accumulated segment,
        // appending it only when absent. Scanning the whole segment per
        // literal is quadratic in program size, so index every 4-byte
        // window of the segment and only verify offsets that share the
        // literal's first four bytes; the first verified candidate is the
        // same offset the full scan would have found.
        let accumulatedBytes = new ByteBuffer();
        let windowIndex = new Map<i32, GrowableArray<i32>>();
        // Window starts below this offset are indexed; it trails the end of
        // the buffer by up to 3 bytes so windows spanning an append
        // boundary are indexed once the bytes after them exist.
        var indexedUpTo = 0;
        for (let s in sortedStrings) {
          let sLen = s.length;

          var foundOffset = -1;
          if (sLen == 0) {
            foundOffset = 0;
          } else if (sLen < 4) {
            let accumLen = accumulatedBytes.length;
            for (var i = 0; i <= accumLen - sLen; i += 1) {
              var match = true;
              for (var j = 0; j < sLen; j += 1) {
                if (accumulatedBytes.getByte(i + j) != s.getByteAt(j)) {
                  match = false;
                  break;
                }
              }
              if (match) {
                foundOffset = i;
                break;
              }
            }
          } else {
            let key = s.getByteAt(0) | (s.getByteAt(1) << 8) | (s.getByteAt(2) << 16) | (s.getByteAt(3) << 24);
            if (let (true, positions) = windowIndex.get(key)) {
              let accumLen = accumulatedBytes.length;
              for (let pos in positions) {
                if (pos + sLen <= accumLen) {
                  var match = true;
                  for (var j = 4; j < sLen; j += 1) {
                    if (accumulatedBytes.getByte(pos + j) != s.getByteAt(j)) {
                      match = false;
                      break;
                    }
                  }
                  if (match) {
                    foundOffset = pos;
                    break;
                  }
                }
              }
            }
          }

          if (foundOffset != -1) {
            wasm.stringLiteralLocations[s] = new StringLiteralLocation(foundOffset, sLen);
          } else {
            let offset = accumulatedBytes.length;
            for (var i = 0; i < sLen; i += 1) {
              accumulatedBytes.writeByte(s.getByteAt(i));
            }
            let newLen = accumulatedBytes.length;
            while (indexedUpTo + 4 <= newLen) {
              let w = indexedUpTo;
              let wKey = accumulatedBytes.getByte(w) | (accumulatedBytes.getByte(w + 1) << 8) | (accumulatedBytes.getByte(w + 2) << 16) | (accumulatedBytes.getByte(w + 3) << 24);
              if (let (true, list) = windowIndex.get(wKey)) {
                list.push(w);
              } else {
                let list = growable<i32>([w]);
                windowIndex[wKey] = list;
              }
              indexedUpTo += 1;
            }
            wasm.stringLiteralLocations[s] = new StringLiteralLocation(offset, sLen);
          }
        }

        // Add single concatenated data segment
        let dataIndex = wasm.addData(accumulatedBytes);
        wasm.sharedStringsLength = accumulatedBytes.length;
        wasm.sharedStringsDataIdx = dataIndex;

        // Assign sequential IDs to string literals
        let astLitKeys = wasm.stringLiterals.keys();
        var litIdx = 0;
        for (let val in astLitKeys) {
          wasm.stringLiteralIds[val] = litIdx;
          litIdx += 1;
        }

        wasm.ensureStringLiteralHelpers(this.program.units[0].model);
        let {stringLiteralsArrayGlobal} = wasm;
        if (stringLiteralsArrayGlobal != null) {
          stringLiteralsArrayGlobal.initArraySize = wasm.stringLiterals.size;
        }
      }
    }
    
    var needsStartFunction = (stringClass != null && wasm.stringLiterals.size > 0 && !targetUsesJsStrings(this.target)) ||
        (targetUsesJsStrings(this.target) && wasm.importedStringGlobals.size > 0);
    for (let g in wasm.globals) {
      // A namespace-import object is built in __start (one closure
      // pair per exported function, exported globals by value), but it
      // carries no initExpr to say so. It got a start function for
      // free while some stdlib global always had a non-literal
      // initializer; once a library unit is only walked when something
      // reaches it, that stopped being true and the object stayed
      // null.
      if (g.namespaceImport != null) {
        needsStartFunction = true;
      }
      let {initExpr} = g;
      if (initExpr != null) {
        let isLiteral = match (initExpr) {
          case NumberLiteral: !isRefValType(g.type)
          case BooleanLiteral: true
          case _: false
        };
        if (!isLiteral) {
          needsStartFunction = true;
        }
      }
    }
    
    if (needsStartFunction) {
      let startSig = wasm.getSignature(new Array<ValType>(), new Array<ValType>());
      let firstModel = this.program.units[0].model;
      let startFunc = new WasmFunction(startSig, null, firstModel);
      startFunc.name = "__start";
      wasm.addFunction(startFunc);
      wasm.startFunction = startFunc;
    }

    if (targetUsesJsStrings(this.target)) {
      let firstModel = this.program.units[0].model;
      for (let strVal in wasm.importedStringGlobals.keys()) {
        wasm.getImportedStringWrapperGlobal(strVal, firstModel);
      }
    }

    // Follows RTA's instantiation verdict, NOT declaration: the
    // helpers' bodies are synthesized against String's struct, and
    // "the struct has fields" is no signal either — layout populates
    // every declared class's struct whether or not a value can exist.
    if (wasm.stringClassIsInstantiated && !targetUsesJsStrings(this.target)) {
      wasm.ensureStringHelpers(this.program.units[0].model);
    }
    // Pre-discover functions whose references are taken by namespace imports
    for (let g in wasm.globals) {
      let {namespaceImport} = g;
      if (namespaceImport != null) {
        let spec = namespaceImport;
        let moduleModel = g.model;
        let parentNode = moduleModel.getParent(spec);
        if (parentNode is ImportDeclaration) {
          let specifier = parentNode.moduleSpecifier.value;
          var canonicalPath = specifier;
          if (specifier.startsWith("./") || specifier.startsWith("../")) {
            let referrer = moduleModel.getParent(parentNode);
            if (referrer is Module) {
              let dir = dirName(referrer.path);
              canonicalPath = normalizePath(`${dir}/${specifier}`);
            }
          }
          if (this.program.unitsByPath.has(canonicalPath)) {
            let targetUnit = this.program.unitsByPath[canonicalPath];
            let exports = new Map<String, Symbol>();
            collectValueExports(this.program, targetUnit, exports);
            let expKeys = exports.keys();
            for (let k in expKeys) {
              let sym = exports[k];
              if (wasm.functionMap.has(sym.id)) {
                let f = wasm.functionMap[sym.id];
                f.isReferenceTaken = true;
                if (f.closureTrampoline == null) {
                  let tParams = growable<ValType>([new ValTypeStructRef()]);
                  for (let p in f.signature.params) {
                    tParams.push(p);
                  }
                  let tSig = wasm.getSignature(tParams, f.signature.returns);
                  let trampoline = new WasmFunction(tSig, null, f.model);
                  let fName = if (f.name != null) (f.name as String) else "anonymous";
                  trampoline.name = fName + "$trampoline";
                  trampoline.isClosureTrampoline = true;
                  trampoline.closureTrampolineTarget = f;
                  trampoline.isReferenceTaken = true;
                  f.closureTrampoline = trampoline;
                  wasm.addFunction(trampoline);
                }
              }
            }
          }
        }
      }
    }

    // Argument explosion (ir/explode.zena): a core beside each function
    // whose record parameter some call passes unpacked, or whose record
    // result some call only reads the fields of. Before the splits,
    // which lower every generator and async body: those bodies' calls
    // then see the cores, and a suspending function's core is split
    // like any other (its frame holds the fields) while its forwarder
    // stays a plain function. Before layout() locks types, because it
    // interns signatures. It reads only reachability's results, so the
    // callers the passes below synthesize, which call forwarders by
    // their unchanged signatures, need not exist yet.
    explodeRecordParameters(wasm, pass);

    // Generator split pass (ir/generators.zena): presplit-lowers every
    // generator body (which needs the string-literal registry above)
    // and synthesizes everything a generator needs — frame structs,
    // next() functions, Iterator<T> vtable globals. Must run before
    // layout() locks indices.
    let tGenerators = performance.start("codegen-generators");
    prepareGenerators(wasm, pass);
    performance.end(tGenerators);

    // Async split pass (ir/async.zena): the same treatment for every
    // `async` body — frame structs, step() functions, Resumable
    // vtable globals. Also before layout().
    let tAsync = performance.start("codegen-async");
    prepareAsyncFunctions(wasm, pass);
    performance.end(tAsync);

    // The component target's adapters: the async entry, synthesized only
    // where the program links a driver that needs one, and a wrapper per
    // export carrying a string. Before layout(), like the passes above,
    // because they add functions. The entry comes first — it decides
    // whether `main` is still an export to wrap.
    if (targetEmitsComponent(this.target)) {
      prepareComponentEntry(wasm, pass);
      prepareSyncExports(wasm, pass);
      prepareResourceDestructors(wasm, pass);
      prepareComponentExports(wasm, pass);
    }

    wasm.layout();
    performance.end(tLayout);

    // Every body is lowered, optimized, and verified before any
    // section is emitted: the module loop needs all bodies at once,
    // and harvest deletes functions, which the function section, the
    // vtable initializers, the exports, and the element segment all
    // name by index. The export list is built now for the same reason
    // — harvest roots liveness in it — and emitted later unchanged.
    let functionExports = this.#collectFunctionExports(wasm);
    markTypes("layout, generators, async");
    let tLower = performance.start("codegen-lower-optimize");
    var zirLowered = 0;
    var zirGraduated = 0;
    var zirDemoted = 0;
    var zirSimplified = 0;
    // Reusable per-body tables for every pass visit below.
    let scratch = new IrScratch();
    var zirBlocksRemoved = 0;
    var zirDceRemoved = 0;
    let bodies = new Array<IrBody>();
    let templateStats = if (this.zirStats) new TemplateStats() else null;
    let discovered = if (this.zirStats) new DiscoveryCounts(wasm) else null;
    let families = if (this.templates) new TemplateFamilies(wasm, this.templateCheck) else null;
    this.templateFamilies = families;
    for (let wasmFunc in wasm.functions) {
      let tLowerStart = if (templateStats != null) nowMs() else 0.0;
      // Lowering failures throw: a bail is a compiler bug, not a
      // coverage gap.
      let body = if (families != null) {
        lowerWithTemplates(wasm, families, wasmFunc)
      } else {
        lowerFunction(wasm, wasmFunc)
      };
      let tLowered = if (templateStats != null) nowMs() else 0.0;
      if (templateStats != null) {
        templateStats.compare(wasmFunc, body);
      }
      let tCleanupStart = if (templateStats != null) nowMs() else 0.0;
      // Cleanup order: simplify folds (and may strand blocks via
      // branch folding), block cleanup removes what it stranded,
      // GVN deduplicates the folded constants, DCE sweeps whatever
      // ended up unused. GVN runs at every level — emission quality
      // and the narrowing cast-dedup contract depend on it.
      if (this.optLevel >= 1) {
        zirSimplified += runSimplify(body, scratch);
        zirBlocksRemoved += runBlockCleanup(body, scratch);
      }
      runGvn(body, scratch);
      if (this.optLevel >= 1) {
        zirDceRemoved += runDce(body, scratch);
      }
      // Loud on verifier failure: a broken lowering must never limp
      // into emission.
      let verifyErrors = verifyIr(body, scratch);
      if (verifyErrors.length > 0) {
        let fname = if (wasmFunc.name != null) wasmFunc.name as String else "<unnamed>";
        var message = `zir verify failed for ${fname}:`;
        for (let e in verifyErrors) {
          message = `${message}\n  ${e}`;
        }
        if (this.zirStats) {
          // The body as the passes left it: the ids the errors name
          // are meaningless without it.
          message = `${message}\n${printIr(body, fname)}`;
        }
        throw new Error(message);
      }
      if (templateStats != null) {
        templateStats.record(wasmFunc, tLowered - tLowerStart,
            nowMs() - tCleanupStart, body.instCount());
      }
      bodies.push(body);
      zirLowered += 1;
    }
    performance.end(tLower);
    if (templateStats != null) {
      for (let line in templateStats.report()) {
        console.log(line);
      }
      if (families != null) {
        for (let line in families.report()) {
          console.log(line);
        }
      }
    }

    // The module loop (-O2): inline sweeps to a fixpoint with a round
    // cap, re-running the cleanup passes on every caller a sweep
    // changed. Chains (an adapter calling an accessor) resolve across
    // rounds rather than by a topological order.
    var zirInlined = 0;
    var zirDevirtualized = 0;
    var zirPropagationDevirtualized = 0;
    var zirHarvestedFunctions = 0;
    var zirHarvestedGlobals = 0;
    var zirHoisted = 0;
    var zirPropagated = 0;
    var zirScalarized = 0;
    var zirThreaded = 0;
    var zirSpecialized = 0;
    // Harvest roots: the exported functions (exported globals and the
    // start function are found inside the pass).
    let roots = new Array<WasmFunction>();
    for (let e in functionExports) {
      roots.push(e.func);
    }
    if (this.optLevel >= 2) {
      let tLoop = performance.start("codegen-optimize-loop");
      let devirtCx = new DevirtContext(wasm);
      let changed = new Array<boolean>();
      for (var i = 0; i < bodies.length; i += 1) {
        changed.push(false);
      }
      // Re-runs the cleanup passes on every body a sweep changed, and
      // clears the flags.
      let cleanupChanged = (what: String) => {
        for (var i = 0; i < bodies.length; i += 1) {
          if (!changed[i]) {
            continue;
          }
          changed[i] = false;
          let body = bodies[i];
          // Block cleanup runs before simplify as well as after:
          // deleting a continuation's parameter is what lets the
          // forwarding rules see the allocation behind it.
          zirBlocksRemoved += runBlockCleanup(body, scratch);
          zirSimplified += runSimplify(body, scratch);
          zirBlocksRemoved += runBlockCleanup(body, scratch);
          runGvn(body, scratch);
          zirDceRemoved += runDce(body, scratch);
          let errors = verifyIr(body, scratch);
          if (errors.length > 0) {
            let f = wasm.functions[i];
            var message = "zir verify failed after " + what + " into " +
                (if (f.name != null) f.name as String else "<unnamed>") + ":";
            for (let e in errors) {
              message = `${message}\n  ${e}`;
            }
            throw new Error(message);
          }
        }
      };
      // Each round: inline, clean up, devirtualize, clean up. The
      // cleanup between the two matters: forwarding a closure's
      // function field off its allocation is what lets devirtualization
      // see the function, and the direct call that produces is what
      // the next round's inline sweep consumes. Escape summaries are
      // recomputed per round, since a parameter can stop escaping once
      // the call it was passed to has been inlined and simplified.
      let runRounds = (maxRounds: i32, devirt: DevirtContext) => {
      var round = 0;
      while (round < maxRounds) {
        let escapes = new EscapeSummaries(wasm, bodies, scratch);
        let inlined = runInline(wasm, bodies, changed, escapes, scratch);
        cleanupChanged("inlining");
        var devirtualized = 0;
        for (var i = 0; i < bodies.length; i += 1) {
          let d = runDevirt(devirt, bodies[i]);
          if (d > 0) {
            changed[i] = true;
            devirtualized += d;
          }
        }
        // Scalar replacement dissolves the objects inlining localized,
        // jump threading sends each arm of an inlined body past the
        // continuation's branch on the flag it returned, then constant
        // propagation folds the branches the sweeps made constant:
        // every body in the first round, after that only the bodies a
        // sweep touched.
        var scalarized = 0;
        var threaded = 0;
        var propagated = 0;
        for (var i = 0; i < bodies.length; i += 1) {
          if (round == 0 || changed[i]) {
            let s = runSroa(bodies[i], scratch);
            if (s > 0) {
              changed[i] = true;
              scalarized += s;
            }
            let t = runJumpThreading(bodies[i], scratch);
            if (t > 0) {
              changed[i] = true;
              threaded += t;
            }
            let n = runSccp(bodies[i], scratch);
            if (n > 0) {
              changed[i] = true;
              propagated += n;
            }
          }
        }
        cleanupChanged("devirtualization");
        // A function every caller hands the same class of fat pointer
        // gets a clone taking the instance; the clone's re-pack is
        // what the next cleanup forwards and the next round's sweeps
        // devirtualize and inline through.
        let specialized = runSpecialize(wasm, bodies, changed);
        cleanupChanged("specialization");
        zirInlined += inlined;
        zirDevirtualized += devirtualized;
        zirScalarized += scalarized;
        zirThreaded += threaded;
        zirPropagated += propagated;
        zirSpecialized += specialized;
        if (inlined == 0 && devirtualized == 0 && specialized == 0) {
          break;
        }
        round += 1;
      }
      };
      runRounds(6, devirtCx);
      // Interprocedural type propagation: once the rounds have done
      // what they can site by site, the call_refs whose callee only one
      // function can reach become direct calls, and two more rounds
      // inline through them.
      if (this.propagate) {
        let tProp = performance.start("codegen-propagate");
        zirPropagationDevirtualized = runPropagationDevirt(wasm, bodies, roots, changed);
        performance.end(tProp);
        if (zirPropagationDevirtualized > 0) {
          cleanupChanged("propagation");
          runRounds(2, devirtCx);
        }
      }
      // Loop-invariant code motion, once the rounds have made the
      // loops what they will be; GVN then merges what hoisting made
      // adjacent.
      for (var i = 0; i < bodies.length; i += 1) {
        let hoisted = runLicm(bodies[i], scratch);
        if (hoisted > 0) {
          zirHoisted += hoisted;
          changed[i] = true;
        }
      }
      cleanupChanged("hoisting");
      // Harvest with feedback: deleting the vtable globals nothing
      // reads any more shrinks the candidate set devirtualization
      // consults, so a further round can make more calls direct — and
      // that can strand more globals. Stops when a harvest deletes no
      // global, or after a few passes.
      var pass = 0;
      while (pass < 3) {
        let harvested = pruneUnreached(wasm, bodies, roots);
        zirHarvestedFunctions += harvested.functions;
        zirHarvestedGlobals += harvested.globals;
        if (harvested.globals == 0) {
          break;
        }
        runRounds(2, new DevirtContext(wasm));
        pass += 1;
      }
      performance.end(tLoop);
    } else if (this.optLevel >= 1) {
      let harvested = pruneUnreached(wasm, bodies, roots);
      zirHarvestedFunctions = harvested.functions;
      zirHarvestedGlobals = harvested.globals;
    }

    // Vtable slots no surviving body reads go (ir/deadslots.zena): the
    // module loop made their calls direct, and only the vtable globals
    // still name the functions behind them, which the harvest after
    // removes.
    var zirSlotsRemoved = 0;
    if (this.optLevel >= 1) {
      let slotsChanged = new Array<boolean>();
      for (var i = 0; i < bodies.length; i += 1) {
        slotsChanged.push(false);
      }
      zirSlotsRemoved = eliminateDeadSlots(wasm, bodies, slotsChanged, scratch);
      if (zirSlotsRemoved > 0) {
        for (var i = 0; i < bodies.length; i += 1) {
          if (slotsChanged[i]) {
            zirDceRemoved += runDce(bodies[i], scratch);
          }
        }
        let harvested = pruneUnreached(wasm, bodies, roots);
        zirHarvestedFunctions += harvested.functions;
        zirHarvestedGlobals += harvested.globals;
      }
    }

    // Class fields no surviving body reads go, with their writes
    // (ir/deadfields.zena). An allocation that loses its `<vtable>`
    // operand leaves the vtable global's read dead; DCE deletes it and
    // a second harvest the global.
    var zirFieldsRemoved = 0;
    var zirEmptyCallsRemoved = 0;
    if (this.optLevel >= 1) {
      let fieldsChanged = new Array<boolean>();
      for (var i = 0; i < bodies.length; i += 1) {
        fieldsChanged.push(false);
      }
      zirFieldsRemoved = eliminateDeadFields(wasm, bodies, fieldsChanged);
      if (zirFieldsRemoved > 0) {
        for (var i = 0; i < bodies.length; i += 1) {
          if (fieldsChanged[i]) {
            zirDceRemoved += runDce(bodies[i], scratch);
          }
        }
        // A setter whose field went is now empty; its calls go too.
        zirEmptyCallsRemoved = removeEmptyCalls(wasm, bodies, fieldsChanged);
        let harvested = pruneUnreached(wasm, bodies, roots);
        zirHarvestedFunctions += harvested.functions;
        zirHarvestedGlobals += harvested.globals;
      }
    }

    // With the dead functions and globals gone, the types only they
    // named go too. Emission reads type indices off the objects, so
    // this has to run before the type section is emitted. The groups
    // are then formed again: removed fields and types can leave two
    // groups structurally identical, and wasm would merge them into
    // one type.
    var zirTypesRemoved = 0;
    if (this.optLevel >= 1) {
      zirTypesRemoved = pruneTypes(wasm, bodies);
      wasm.layoutTypes();
    }

    markTypes("lower, optimize");
    let tEmitOther = performance.start("codegen-emit-other");
    this.emitter.emitModuleStart();
    // Rec groups come from layout()'s SCC pass: one group per
    // strongly-connected component of the type-reference graph, in
    // dependency order. A type-section entry is a GROUP (a singleton
    // emits in the bare, header-free form), so the section count is
    // the group count.
    let recGroupSizes = wasm.typeRecGroupSizes;
    var groupedTypeCount = 0;
    for (let gs in recGroupSizes) {
      groupedTypeCount += gs;
    }
    if (groupedTypeCount != wasm.types.length) {
      throw new Error('rec group sizes cover ' +
          i32ToString(groupedTypeCount) + ' types but the layout has ' +
          i32ToString(wasm.types.length));
    }

    // Which laid-out types some other type declares as a supertype.
    // Everything else is emitted `final`: subtyping in wasm is by
    // declaration, so a type no declaration names cannot have a
    // subtype, and a `ref.test`/`ref.cast` against a final type is an
    // inline type-index comparison in wasmtime instead of a call into
    // the runtime's `is_subtype` (docs/design/struct-finality.md).
    // Computed here rather than in layout() because pruneTypes above
    // can delete the last subtype a type had.
    let isExtended = new Array<boolean>();
    for (var i = 0; i < wasm.types.length; i += 1) {
      isExtended.push(false);
    }
    for (let t in wasm.types) {
      var superIndex = -1;
      if (t is WasmStruct) {
        let sup = (t as WasmStruct).superStruct;
        if (sup != null) {
          superIndex = (sup as WasmStruct).index;
        }
      } else if (t is WasmArray) {
        let sup = (t as WasmArray).superArray;
        if (sup != null) {
          superIndex = (sup as WasmArray).index;
        }
      }
      if (superIndex >= 0) {
        isExtended[superIndex] = true;
      }
    }

    this.emitter.emitTypeSectionStart(recGroupSizes.length);

    let emitTypeAtIndex = (idx: i32) => {
      let t = wasm.types[idx];
      if (t is WasmSignature) {
        this.emitter.emitFunctionTypeStart(idx);
        
        this.emitter.emitFunctionTypeParamsStart(t.params.length);
        for (let param in t.params) {
          this.emitter.emitFunctionTypeParam(param);
        }
        
        this.emitter.emitFunctionTypeResultsStart(t.returns.length);
        for (let ret in t.returns) {
          this.emitter.emitFunctionTypeResult(ret);
        }
        
        this.emitter.emitFunctionTypeEnd();
      } else if (t is WasmStruct) {
        let superIdx =
          if (t.superStruct != null) (t.superStruct as WasmStruct).index 
          else -1;
        this.emitter.emitStructTypeStart(idx, t.name, superIdx,
            !isExtended[idx]);
        
        this.emitter.emitStructTypeFieldsStart(t.fields.length);
        for (let field in t.fields) {
          this.emitter.emitStructTypeField(field.type, field.isMutable);
        }
        
        this.emitter.emitStructTypeEnd();
      } else if (t is WasmArray) {
        let arraySuperIdx =
          if (t.superArray != null) (t.superArray as WasmArray).index
          else -1;
        this.emitter.emitArrayType(idx, t.elementType, t.isMutable,
            arraySuperIdx, !isExtended[idx]);
      }
    };

    var nextTypeIdx = 0;
    for (let gs in recGroupSizes) {
      this.emitter.emitRecGroupStart(gs);
      var inGroup = 0;
      while (inGroup < gs) {
        emitTypeAtIndex(nextTypeIdx);
        nextTypeIdx += 1;
        inGroup += 1;
      }
      this.emitter.emitRecGroupEnd();
    }

    this.emitter.emitTypeSectionEnd();

    // On the component target the program does not own its memory: the
    // runtime memory module does, instantiated before this one, so that
    // a lowered import's canonical options have a memory that already
    // exists (docs/design/component-emission.md, 1.3). Everywhere else
    // the module defines its memory as before.
    let memoryImported =
        targetEmitsComponent(this.target) && wasm.hasMemory;

    let imports = new Array<ImportItem>();
    for (let f in wasm.functionImports) {
      let {importModule, importName} = f;
      if (importModule == null || importName == null) {
        throw new Error(`Function import ${f.name ?? "<unnamed>"} has no module or name`);
      }
      if (f.signature.index < 0) {
        throw new Error(`Function import ${f.name ?? "<unnamed>"} has unassigned signature index (-1)!`);
      }
      imports.push(new ImportItem(importModule, importName, new FuncImport(f.signature.index), f));
    }
    for (let g in wasm.globalImports) {
      imports.push(new ImportItem(g.importModule as String, g.importName as String,
          new GlobalImport(g.type, g.isMutable), g));
    }
    if (memoryImported) {
      imports.push(new ImportItem(componentRuntimeNamespace, componentMemoryExport,
          new MemoryImport(1), null));
    }
    let importEntries = planImports(imports, this.compactImports);
    if (this.compactImports) {
      // Bodies name functions and globals by object, and nothing has
      // been emitted by index yet, so the plan's order can become the
      // index order.
      let funcs = new Array<WasmFunction>();
      let globals = new Array<WasmGlobal>();
      let take = (item: ImportItem) => {
        match (item.target) {
          case WasmFunction as f: { funcs.push(f); }
          case WasmGlobal as g: { globals.push(g); }
          case null: {}
        }
      };
      for (let entry in importEntries) {
        match (entry) {
          case SingleImport {item}: { take(item); }
          case ImportGroup {items}: { for (let item in items) { take(item); } }
          case ImportTypeGroup {items}: { for (let item in items) { take(item); } }
        }
      }
      wasm.reorderImports(funcs, globals);
    }

    this.emitter.emitImportSectionStart(importEntries.length);
    for (let entry in importEntries) {
      match (entry) {
        case SingleImport {item}: { this.emitter.emitImport(item); }
        case ImportGroup {module, items}: { this.emitter.emitImportGroup(module, items); }
        case ImportTypeGroup {module, desc, items}: {
          this.emitter.emitImportTypeGroup(module, desc, items);
        }
      }
    }
    this.emitter.emitImportSectionEnd();

    this.emitter.emitFunctionSectionStart(wasm.functions.length);
    for (let f in wasm.functions) {
      if (f.signature.index < 0) {
        throw new Error(`Function ${if (f.name != null) f.name as String else "<unnamed>"} has unassigned signature index (-1)!`);
      }
      this.emitter.emitFunctionDefinition(f.signature.index);
    }
    this.emitter.emitFunctionSectionEnd();

    if (wasm.hasMemory && !memoryImported) {
      this.emitter.emitMemorySection(1);
    }

    this.emitter.emitTagSectionStart(wasm.tags.length);
    for (let tag in wasm.tags) {
      if (tag.signature.index < 0) {
        throw new Error("Tag has unassigned signature index (-1)!");
      }
      this.emitter.emitTagDefinition(tag.index, tag.signature.index);
    }
    this.emitter.emitTagSectionEnd();

    this.emitter.emitGlobalSectionStart(wasm.globals.length);
    for (let g in wasm.globals) {
      this.emitter.emitGlobal(g.type, g.isMutable);
      
      var emitted = false;
      let {initExpr} = g;
      if (initExpr != null) {
        match (initExpr) {
          case NumberLiteral {raw}: {
             let {type} = g;
             if (!isRefValType(type)) {
               // A literal initializer is baked into the global's own const
               // expression, so it is re-parsed from the raw source text here.
               // That text can be hex, and a parser that does not know it
               // would quietly hand back 0 — the constant would simply be
               // wrong at runtime with nothing failing to compile.
               if (type is ValTypeI32 ) {
                  let (ok, val) = parseI32WithHex(raw);
                  if (!ok) {
                    throw new Error('global initializer is not a valid i32 literal: ' + raw);
                  }
                  this.emitter.emitI32Const(val);
               } else if (type is ValTypeI64 ) {
                  let (ok, val) = parseI64OrU64Literal(raw);
                  if (!ok) {
                    throw new Error('global initializer is not a valid i64 literal: ' + raw);
                  }
                  this.emitter.emitI64Const(val);
               } else if (type is ValTypeF64 ) {
                  this.emitter.emitF64Const(parseFloatLiteral(raw));
               } else if (type is ValTypeF32 ) {
                  this.emitter.emitF32Const(parseFloatLiteral(raw)); // f32 requires casting
               } else {
                  this.emitter.emitI32Const(0);
               }
               emitted = true;
             }
          }
          case BooleanLiteral {value}: {
            this.emitter.emitBooleanConst(value);
            emitted = true;
          }
          case _: {}
        }
      } else if (g.initVTable != null) {
        let vtableInit = g.initVTable as VTableInit;
        let slots = vtableInit.slotGlobals;
        if (slots != null) {
          for (var si = 0; si < slots.length; si += 1) {
            let slot = slots[si];
            if (slot == null) {
              this.emitDefaultValue(vtableInit.structType.fields[si].type);
            } else {
              if (slot.index == -1) {
                throw new Error(`vtable initializer reads global ${slot.name} before it is declared`);
              }
              this.emitter.emitGlobalGet(slot.index);
            }
          }
        }
        for (let func in vtableInit.funcs) {
          if (func.index == -1) {
            throw new Error(`DIAGNOSTIC VTABLE INIT ERROR: func.name=${if (func.name == null) "null" else func.name as String} has index -1`);
          }
          this.emitter.emitRefFunc(func.index);
        }
        if (vtableInit.shapeTag >= 0) {
          this.emitter.emitI32Const(vtableInit.shapeTag);
        }
        let stored = (if (slots == null) 0 else slots.length) +
            vtableInit.funcs.length + (if (vtableInit.shapeTag >= 0) 1 else 0);
        if (stored != vtableInit.structType.fields.length) {
          throw new Error(`vtable initializer of ${g.name} stores ${i32ToString(stored)} values for ${i32ToString(vtableInit.structType.fields.length)} fields`);
        }
        this.emitter.emitStructNew(vtableInit.structType.index);
        emitted = true;
      } else if (g.initSingletonStruct != null) {
        let cType = g.initSingletonStruct as ClassType;
        let cKey = getClassKey(cType);
        var structTypeOpt: WasmStruct | null = null;
        var vtableGlobalOpt: WasmGlobal | null = null;
        if (let (true, info) = wasm.classInfos.get(cKey)) {
          structTypeOpt = info.structType;
          vtableGlobalOpt = info.vtableGlobal;
        }
        if (structTypeOpt == null) {
          throw new Error("Singleton struct not found for class: " + cType.name);
        }
        let structType = structTypeOpt;
        for (let field in structType.fields) {
          let {name} = field;
          if (name != null && name == "<vtable>") {
            if (vtableGlobalOpt == null) {
              throw new Error("Singleton vtable not found for class: " + cType.name);
            }
            this.emitter.emitGlobalGet(vtableGlobalOpt.index);
          } else {
            this.emitDefaultValue(field.type);
          }
        }
        this.emitter.emitStructNew(structType.index);
        emitted = true;
      } else if (g.initArraySize >= 0) {
        this.emitter.emitI32Const(g.initArraySize);
        let targetIndex = match (g.type) {
          case ValTypeRef {target}: target.index
          case ValTypeRefNull {target}: target.index
          case _: throw new Error("Expected ValTypeRef or ValTypeRefNull for array global initialization")
        };
        this.emitter.emitArrayNewDefault(targetIndex);
        emitted = true;
      }
      
      // A global the start function initializes starts out as its type's
      // zero.
      if (!emitted) {
        this.emitDefaultValue(g.type);
      }

      this.emitter.emitGlobalEnd();
    }
    this.emitter.emitGlobalSectionEnd();
    
    let exports = functionExports;


    // Compute total exports = functions + globals + tags
    var totalExports = exports.length;
    let globalExports = new Array<WasmGlobal>();
    for (let gName in wasm.exportGlobalMap.keys()) {
      globalExports.push(wasm.exportGlobalMap[gName]);
    }
    totalExports += globalExports.length;
    
    let tagExports = new Array<String>();
    let tagKeys = wasm.exportTagMap.keys();
    for (let k in tagKeys) {
      tagExports.push(k);
    }
    totalExports += tagExports.length;

    // An imported memory is the runtime module's to export; re-exporting
    // it here would only offer the component a second name for it.
    if (wasm.hasMemory && !memoryImported) {
      totalExports += 1;
    }

    this.emitter.emitExportSectionStart(totalExports);
    for (let exp in exports) {
      this.emitter.emitExport(exp.name, exp.func);
    }
    for (let g in globalExports) {
      this.emitter.emitExportGlobal(g.zenaName, g.index);
    }
    for (let tName in tagExports) {
      let tag = wasm.exportTagMap[tName];
      this.emitter.emitExportTag(tName, tag.index);
    }
    if (wasm.hasMemory && !memoryImported) {
      this.emitter.emitExportMemory("memory", 0);
    }
    this.emitter.emitExportSectionEnd();

    let {startFunction} = wasm;
    if (startFunction != null) {
      let sf = startFunction;
      this.emitter.emitStartSection(sf.index);
    }

    let elemIndices = new Array<i32>();
    for (let f in wasm.functions) {
      if (f.isReferenceTaken) {
        elemIndices.push(f.index);
      }
    }
    if (elemIndices.length > 0) {
      this.emitter.emitElementSection(elemIndices);
    }
    
    performance.end(tEmitOther);

    let tEmitCode = performance.start("codegen-emit-code");
    this.emitter.emitCodeSectionStart(wasm.functions.length);
    var bodyIndex = 0;
    for (let wasmFunc in wasm.functions) {
      var graduated = 0;
      var demoted = 0;
      try {
        let (g, d) = emitZirFunction(wasm, this.emitter, wasmFunc, bodies[bodyIndex], scratch);
        graduated = g;
        demoted = d;
      } catch (e) {
        if (this.zirStats && e is Error) {
          // The body as emission saw it: a schedule violation names
          // value ids that mean nothing without it.
          let fname = if (wasmFunc.name != null) wasmFunc.name as String else "<unnamed>";
          throw new Error(`${e.message}\n${printIr(bodies[bodyIndex], fname)}`);
        }
        throw e;
      }
      bodyIndex += 1;
      zirGraduated += graduated;
      zirDemoted += demoted;
    }
    if (this.zirStats) {
      console.log(`zir backend: ${i32ToString(zirLowered)} lowered`);
      console.log("zir simplify: " + i32ToString(zirSimplified) +
          " instructions rewritten");
      console.log("zir cleanup: " + i32ToString(zirBlocksRemoved) +
          " blocks removed, " + i32ToString(zirDceRemoved) +
          " dead instructions removed");
      console.log(`zir inline: ${i32ToString(zirInlined)} call sites inlined`);
      console.log("zir devirt: " + i32ToString(zirDevirtualized) +
          " call_ref sites made direct, " + i32ToString(zirPropagationDevirtualized) +
          " more by type propagation");
      console.log("zir licm: " + i32ToString(zirHoisted) +
          " loop-invariant instructions hoisted");
      console.log("zir sccp: " + i32ToString(zirPropagated) +
          " constants, parameters and branches folded");
      console.log("zir sroa: " + i32ToString(zirScalarized) +
          " local objects replaced by their fields");
      console.log("zir thread: " + i32ToString(zirThreaded) +
          " branches on a returned flag threaded");
      console.log("zir specialize: " + i32ToString(zirSpecialized) +
          " functions cloned for a fat-pointer parameter");
      console.log("zir harvest: " + i32ToString(zirHarvestedFunctions) +
          " unreachable functions and " + i32ToString(zirHarvestedGlobals) +
          " globals removed");
      console.log(`zir slots: ${i32ToString(zirSlotsRemoved)} vtable slots removed`);
      console.log("zir fields: " + i32ToString(zirFieldsRemoved) + " removed, " +
          i32ToString(zirEmptyCallsRemoved) + " calls to emptied functions removed");
      console.log("zir types: " + i32ToString(zirTypesRemoved) +
          " unreferenced types removed");
      console.log("zir locals: " + i32ToString(zirGraduated) +
          " ref locals non-null, " + i32ToString(zirDemoted) +
          " kept nullable (init rollback)");
      if (discovered != null) {
        for (let line in censusReport(wasm, bodies, discovered)) {
          console.log(line);
        }
      }
      let tProp = nowMs();
      for (let line in propagationReport(wasm, bodies, roots)) {
        console.log(line);
      }
      console.log(`zir propagation: ${i32ToString((nowMs() - tProp) as i32)} ms`);
    }
    if (wasm.datas.length > 0) {
      this.emitter.emitDataCountSection(wasm.datas.length);
    }
    this.emitter.emitCodeSectionEnd();
    performance.end(tEmitCode);

    let tEmitOther2 = performance.start("codegen-emit-other");
    if (wasm.datas.length > 0) {
      this.emitter.emitDataSection(wasm.datas, wasm.rawDatas);
    }

    this.emitter.emitModuleEnd();
    performance.end(tEmitOther2);

    if (targetEmitsComponent(this.target)) {
      this.componentShape = buildComponentShape(wasm, this.importEncoder);
    }
  }
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19