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

2.5 KiB Raw
import { Set, HashSet } from 'zena:collections';
import { simdInstruction } from './simd.zena';

let validIntrinsics = new Set<String>();
let intrinsics = [
    'array.len',
    'array.get',
    'array.get_u',
    'array.set',
    'array.new',
    'array.new_default',
    'array.new_fixed',
    'array.new_data',
    'array.copy',
    'array.fill',
    'array.init_data',
    'array.init_elem',
    'memory.size',
    'memory.grow',
    'i32.load',
    'i32.load8_u',
    'i32.load8_s',
    'i32.store',
    'i32.store8',
    'i64.load',
    'i64.store',
    'f32.load',
    'f32.store',
    'f64.load',
    'f64.store',
    'eq',
    'hash',
    'to_string',
    'default_value',
    'is_first_class',
    'release_owned',
    'share_reference',
    'cancel.raise',
    'unreachable',
    'f32.abs',
    'f32.ceil',
    'f32.copysign',
    'f32.floor',
    'f32.max',
    'f32.min',
    'f32.nearest',
    'f32.neg',
    'f32.reinterpret_i32',
    'f32.sqrt',
    'f32.trunc',
    'f64.abs',
    'f64.ceil',
    'f64.copysign',
    'f64.floor',
    'f64.max',
    'f64.min',
    'f64.nearest',
    'f64.neg',
    'f64.reinterpret_i64',
    'f64.sqrt',
    'f64.trunc',
    'i32.clz',
    'i32.ctz',
    'i32.div_s',
    'i32.div_u',
    'i32.popcnt',
    'i32.reinterpret_f32',
    'i32.trunc_sat_f32_s',
    'i32.trunc_sat_f32_u',
    'i32.trunc_sat_f64_s',
    'i32.trunc_sat_f64_u',
    'i64.clz',
    'i64.ctz',
    'i64.div_s',
    'i64.div_u',
    'i64.popcnt',
    'i64.reinterpret_f64',
    'i64.trunc_sat_f32_s',
    'i64.trunc_sat_f32_u',
    'i64.trunc_sat_f64_s',
    'i64.trunc_sat_f64_u',
    // The wide-arithmetic proposal. Each of these produces TWO i64s,
    // so the declarations in zena:math return `inline (u64, u64)` —
    // low half first, matching the proposal's result order. Whether
    // they lower to the real instructions or to the equivalent
    // 32-bit-halves sequences is `WasmModule.wideArithmetic`.
    'i64.add128',
    'i64.sub128',
    'i64.mul_wide_s',
    'i64.mul_wide_u',
];
var initialized = false;

export function isValidIntrinsic(name: String): boolean {
  // TODO: We need a way to make a set from an array
  if (!initialized) {
    initialized = true;
    for (let intrinsic in intrinsics) {
      validIntrinsics.add(intrinsic);
    }
  }
  if (validIntrinsics.has(name)) {
    return true;
  }
  // The SIMD instructions are not listed above: their names, opcodes and
  // operand shapes come from one table in simd.zena, which the ZIR
  // lowering router reads too. Listing them here as well would be a
  // second place for the set to drift out of.
  return simdInstruction(name) != null;
}
VersionPublishedDependenciesApproved by
0.0.2-preview.1 2026-11-19