#257

Feature: Implement std.codec.hexEncode and std.codec.utf8Valid in direct backend and runtime

Open555aadityaopened 5mo ago

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:

  1. The compiler IR lowering stage (native/zero-c/src/ir.c) does not capture or lower these calls.
  2. The direct emitters do not compile them or emit runtime relocation patches for them.
  3. 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 packed Maybe<String> (packed structure format).
  • Add zero_utf8_valid(ZeroByteView bytes) returning a boolean (1 or 0).

2. Compiler IR Lowering (native/zero-c/src/ir.h & ir.c)

  • Define IR value kinds: IR_VALUE_CODEC_HEX_ENCODE and IR_VALUE_CODEC_UTF8_VALID.
  • Lower the calls std.codec.hexEncode and std.codec.utf8Valid to their respective IR value kinds in ir.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_encode and zero_utf8_valid in 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.0 compiles and runs successfully using bin/zero run examples/std-data-formats.0
  • Ensure all conformance tests pass: pnpm run conformance:local

Activity

0

No activity yet.