#257
Feature: Implement std.codec.hexEncode and std.codec.utf8Valid in direct backend and runtime
Describe the Feature
Currently, standard library signatures for std.codec.hexEncode and std.codec.utf8Valid are defined in native/zero-c/src/std_sig.c and validated in checker.c. Because of this, type-checking files that call these functions (such as examples/std-data-formats.0) succeeds under zero check.
However, building or running these programs fails under the direct codegen backend (e.g. AArch64 Mach-O, x86 ELF, COFF) with a BLD004 (non-identifier callee) error. This is because:
- The compiler IR lowering stage (
native/zero-c/src/ir.c) does not capture or lower these calls. - The direct emitters do not compile them or emit runtime relocation patches for them.
- The precompiled C runtime (
native/zero-c/runtime/zero_runtime.c) does not implement them.
This issue proposes implementing both std.codec.hexEncode and std.codec.utf8Valid natively in the C runtime and the compiler's direct codegen pipeline.
Proposed Architectural Solution
1. Runtime Implementation (native/zero-c/runtime/zero_runtime.c)
- Add
zero_hex_encode(ZeroByteView destination, ZeroByteView source)returning a packedMaybe<String>(packed structure format). - Add
zero_utf8_valid(ZeroByteView bytes)returning a boolean (1or0).
2. Compiler IR Lowering (native/zero-c/src/ir.h & ir.c)
- Define IR value kinds:
IR_VALUE_CODEC_HEX_ENCODEandIR_VALUE_CODEC_UTF8_VALID. - Lower the calls
std.codec.hexEncodeandstd.codec.utf8Validto their respective IR value kinds inir.c.
3. Emitters & Relocation Patches (elf_emit_state.c / macho_emit_state.c / coff_emit_state.c & direct emitters)
- Declare runtime helper symbols
zero_hex_encodeandzero_utf8_validin the patch matrices. - Wire the direct emitters (e.g.
emit_elf64.c,emit_macho64.c,emit_coff.c) to generate the correct symbol relocation patches.
Verification Plan
- Compile the compiler:
make -C native/zero-c - Verify
examples/std-data-formats.0compiles and runs successfully usingbin/zero run examples/std-data-formats.0 - Ensure all conformance tests pass:
pnpm run conformance:local
Activity
0No activity yet.