Skip to content

Commit b8e531b

Browse files
hyperpolymathclaude
andcommitted
fix(codegen): wire string_length lowering — sibling to #362's interp binding
The interp binding for string_length landed in #362 (already on main), but the wasm-backend codegen still does not know it — `codegen.ml`'s `ExprApp` dispatch fell through to the "Function or variable not found" error path. The codegen-test fixtures env_at.affine / arg_at.affine / env_count_and_at.affine all call `string_length(env_at(0))` etc., so the dune-runtest codegen-fixture loop fails at code-generation time even after the resolver / typechecker (#332) and interp (#362) layers were wired. AS string layout is `[len: i32][bytes...]` at the pointer the arg evaluates to — reading the length is one `i32.load` at offset 0. Same shape as the existing tuple-index / record-field load patterns. Three lines of effective code in the handler. Closes the gap for ADR-015 S5 (env_at/arg_at) too: their codegen emits a length-prefixed AS string, and the fixture tests then need string_length to read that length back. Both pieces (env_at/arg_at wiring + string_length codegen lowering) are required for the fixtures to compile end-to-end. Refs #332 (STDLIB-04e originating ticket), Refs #361 (queue-clearance incident), follow-on to #362 (interp side) and #364's env_at/arg_at wiring earlier in this PR. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 7186e06 commit b8e531b

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

lib/codegen.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,17 @@ let rec gen_expr (ctx : context) (expr : expr) : (context * instr list) result =
908908
in
909909
Ok (ctx_with_heap, code)
910910

911+
| ExprVar id when id.name = "string_length" && List.length args = 1 ->
912+
(* STDLIB-04e (#332) wasm-backend lowering. AS string layout is
913+
`[len: i32][bytes...]` at the pointer the arg evaluates to —
914+
reading the length is one i32.load at offset 0. The interp
915+
binding (lib/interp.ml) was wired in #362; this handler is
916+
the codegen sibling so tests/codegen/*.affine fixtures that
917+
call string_length (env_at / arg_at / env_count_and_at) can
918+
compile end-to-end. *)
919+
let* (ctx_with_arg, arg_code) = gen_expr ctx (List.hd args) in
920+
Ok (ctx_with_arg, arg_code @ [I32Load (2, 0)])
921+
911922
| ExprVar id when (id.name = "env_at" || id.name = "arg_at")
912923
&& List.length args = 1 ->
913924
(* ADR-015 S5 (#180): env_at(i) / arg_at(i) — fetch the i-th

0 commit comments

Comments
 (0)