Moggi documentation

Architecture and invariants

The compiler is a pipeline of phases. Each phase owns one representation; a fact that leaks into another phase is the defect this page exists to prevent.

The pipeline

source
  → lexer                    tokens
  → parser                   raw AST
  → checking                 typed AST (kinds, schemes, evidence, exhaustiveness)
  → IO/ST validation         checked effect surface
  → normalization            elaborated AST (monad elaboration, explicit sequencing)
  → IR lowering              backend-neutral IR
  → optimization             optimized IR
  → code generation          target source / bytecode
  → packaging                .phar / .jar / .dll

Kind inference is part of checking (src/semantics/kinds.php), not a separate pass. Moggi\Pipeline\run (src/pipeline/) drives one module; its stopAt parameter supplies --ast, --typed-ast, --ir and --opt-ir.

The project layer (src/modules/modules.php) runs before that:

source closure → fixity → dependency order → import/export resolution
→ per-module pipeline (cached) → cross-module specialization
→ reachability/stripping → per-module codegen → packaging

Instances are global: every module can see them without an import, which is why cross-module specialization can rewrite a call. An executable keeps only what main, exports and foreign bindings can reach; a library build keeps everything.

Who owns what

Phase Owns Produces Must not know
Lexing (src/syntax/lexer.php) tokens, lexical diagnostics token stream parsing, modules, types
Parsing (src/syntax/parser.php, parse_*.php) surface grammar, fixity, layout, purely syntactic sugar raw AST types, evidence, backend names
Modules (src/modules/) closure, import/export selection, fixity collection, project preparation import contexts, checked core target syntax
Kinds (src/semantics/kinds.php) kind inference and unification kinded constructors values, codegen
Types (src/semantics/types/) declaration registration, inference, classes/evidence, records, exhaustiveness typed AST PHP names, IR-only control flow
IO boundary / normalization (io_boundary.php, strict_io_normalize.php) pure/effect split and explicit sequencing elaborated AST backend emission
IR (src/ir/) backend-neutral operations, types, lowering IR source-only syntax, unresolved evidence
Optimizer (src/optimize/) IR-to-IR rewrites and their order optimized IR resolver state
Backends (src/backend/) names, layouts, emission, runtime, packaging target code, artifact type-class semantics, name resolution

Type checking is the largest phase. Keep type core, declaration registry, class/evidence elaboration, pattern/exhaustiveness, records and entry-point validation separable.

Representation contracts

Representation Contains Must not contain
Raw AST source syntax, spans, module/import headers inferred types, evidence, backend names
Typed AST inferred/declared types, resolved symbols, exhaustiveness facts, evidence PHP require/use lines, output paths
Elaborated AST evidence insertion, monad elaboration, strict IO sequencing raw do or unresolved evidence
IR functions, blocks, explicit calls/matches/operands source syntax, unresolved types
Optimized IR IR plus documented pass metadata frontend state
Target source the backend’s own code compiler-internal metadata

Invariants

Backends

Every target implements the same Backend contract and uses the same emit_* role set, with EmitEnv $env first and IR payloads following in the same order. tests/backend/emit_surface.php enforces parity over every file a backend directory holds, whatever its extension and nesting: the set must be the shared files plus that host’s own, each shared role must be declared and called, and each IR-walking signature must start with the same prefix. A host-specific file or parameter is a documented exception, not a silent difference. PHP’s host-specific support comprises host.php (probing the PHP that verifies foreign php), store.php (runtime and extension manifests) and portable.php (preparing a copyable runtime).

The managed backends share statement traversal and ordering in src/backend/managed/stmt.php; each EmitEnv implements StmtTarget for the host instructions. Intrinsics dispatch by family (emitIntrinsicPrimop, emitIntrinsicCompare, emitIntrinsicState, emitIntrinsicValue), so adding an intrinsic lands in its family. The stack backends decide source locations once in src/backend/stack/source_lines.php; PHP is expression-valued while JVM and .NET are stack-based.

The managed runtimes’ shared protocol is declared once in src/backend/managed/abi_facts.php and checked against both rendered runtimes by tests/backend/managed_abi_parity.php, so a protocol type cannot be renamed on one target alone. A module’s artifact path is the target’s artifactRelativePath(); the project layer asks rather than substituting an extension, and src/backend/dotnet/inspect/Program.cs is the IL inspector the .NET verifier compiles.

Changing the compiler

  1. Find the owner in the table above; merge duplicate owners first.
  2. Add the smallest fixture that fails for the old behaviour, under the phase’s group in testing.md.
  3. Keep the representation boundary: put a semantic decision in the earliest phase that can make it.
  4. For a new pass, add a checkpoint, document its effects, and place it in the order in optimizations.md.
  5. Run the affected group, then the full suite; change a golden only when the new output is intended.

src/cli/main.php dispatches; src/cli/commands/*.php parse argv and call libraries. No library reads argv.


Moggi compiles to PHP, the JVM and .NET. This site is generated from docs/ in the moggi-lang/moggi repository; the same pages are readable there as Markdown.