@@ -652,90 +652,9 @@ Compiles to (ownership removed):
652652
653653== 8. Codegen Module Environment
654654
655- This section describes how the WebAssembly code generator (`lib/codegen.ml`)
656- builds its name environment. It is implementation documentation aimed at
657- contributors; the language semantics are fully specified in §2–4.
658-
659- === 8.1 Name Environment (`func_indices`)
660-
661- The codegen context maintains a single association list
662-
663- [source,ocaml]
664- ----
665- func_indices : (string * int) list
666- ----
667-
668- that maps every top-level name visible at later declaration sites to an
669- integer key. Two distinct kinds of binding share this table:
670-
671- [cols="2,2,3", options="header"]
672- |===
673- | Source declaration | Key value | Meaning
674-
675- | `fn f(…) { … }`
676- | `k ≥ 0`
677- | WebAssembly function index (imports + defined functions, combined)
678-
679- | `const C: T = e`
680- | `-(g + 1)`, where `g` is the global's index in the Wasm `globals` vector
681- | Negative sentinel reserved for constants
682- |===
683-
684- Sign-based partitioning is deliberate: `k ≥ 0` decodes directly as a Wasm
685- `funcidx`, and `k < 0` recovers the global index as `g = -(k + 1)`. A
686- single integer per name keeps the lookup uniform across both kinds of binding.
687-
688- *Population.* Top-level declarations are visited in source order by
689- `gen_decl`, which is folded over `prog.prog_decls` from `generate_module`.
690- The relevant cases are:
691-
692- - `TopFn fd` with `fd.fd_body <> FnExtern` — picks the next Wasm function
693- index (`import_func_count ctx + List.length ctx.funcs`), registers
694- `(fd.fd_name.name, func_idx)` in `func_indices` _before_ generating the
695- body so the body may recursively refer to its own name, then appends the
696- emitted function to `ctx.funcs`.
697- - `TopFn fd` with `fd.fd_body = FnExtern` — emits a Wasm import (module
698- `"env"`, name `fd.fd_name.name`) and registers
699- `(fd.fd_name.name, import_func_idx)` in `func_indices`, where
700- `import_func_idx` is the number of imports before adding this one. No
701- function body is generated. See §8.2.
702- - `TopConst tc` — generates the global initializer, appends the global to
703- `ctx.globals`, then registers `(tc.tc_name.name, -(global_idx + 1))` in
704- `func_indices`.
705-
706- Because population is strictly single-pass and in declaration order,
707- forward references (to either functions or constants declared later in the
708- file) are not supported by the current backend.
709-
710- *Call-site lookup.* The `ExprApp (ExprVar id, _)` branch of `gen_expr`
711- consults `func_indices` to translate a direct call into a Wasm `call k`
712- instruction. Decoding the negative sentinel back to a `global.get` —
713- needed to make a bare `const` identifier usable inside another top-level
714- declaration's body — is tracked as a known gap in issue #73. The encoding
715- documented in this section is the data layout the fix relies on; the
716- call-site decode path will land alongside that fix.
717-
718- === 8.2 Extern Bindings
719-
720- An `extern fn name(…) -> Ret;` declaration produces a `TopFn` with
721- `fd_body = FnExtern`. Codegen lowers it to a Wasm import:
722-
723- [source]
724- ----
725- (import "env" "<name>" (func (param …) (result …)))
726- ----
727-
728- The resulting import function index is positive (it counts among the
729- combined "imports + defined functions" view used by every other call
730- site), so the name is registered in `func_indices` with `k ≥ 0` and call
731- sites resolve through `call k` indistinguishably from a locally-defined
732- function. The Wasm module name is currently hard-coded to `"env"`,
733- matching the convention adopted by the Node-CJS host shim.
734-
735- An `extern type Name;` declaration produces a `TopType` with
736- `td_body = TyExtern`. It generates no Wasm artifact — opaque types are
737- purely a typechecker concern — and the codegen `TopType TyExtern` case
738- returns the unchanged context.
655+ See link:codegen-environment.adoc[`docs/specs/codegen-environment.adoc`] for the
656+ full codegen module environment reference, including the `func_indices`
657+ dual-use encoding, population order, and extern binding rules.
739658
740659== Appendix: Grammar Reference
741660
0 commit comments