Add extern fun support for calling external C functions#70
Open
ihasq wants to merge 1 commit into
Open
Conversation
Implements extern fun syntax to enable Zero programs to call external C functions through ELF linking. This allows interop with C libraries (libc, webgpu.h, SQLite, etc.) without requiring compiler builtin implementations. Key changes: - Parser: recognize extern [c] fun syntax, skip body parsing - Checker: add diagnostic 3031 for C ABI validation - IR: add extern_symbol_name field to distinguish extern calls - ELF emitter: generate undefined symbols and R_X86_64_PLT32 relocations - Tests: 4 conformance tests + integration test script Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@ihasq is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
| char **extern_symbol_names = NULL; | ||
| uint32_t *extern_symbol_strtab_offsets = NULL; | ||
| if (ctx.extern_call_site_len > 0) { | ||
| extern_symbol_names = calloc(ctx.extern_call_site_len, sizeof(char *)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add extern fun support for calling external C functions
Summary
Implements
extern funsyntax to enable Zero programs to call external C functions. This allows Zero to interoperate with C libraries through proper ELF linking without requiring compiler builtin implementations.Motivation
Zero currently lacks a mechanism for users to call external C functions. While std.* modules provide core functionality through compiler builtins, there is no way to link against arbitrary C libraries (libc, libm, custom libraries, etc.) from Zero code. This PR addresses that gap by implementing extern function declarations with proper ELF undefined symbol generation and PLT relocations.
Use cases enabled:
This capability significantly expands Zero's ecosystem reach. With extern fun, Zero programs can now interface with established C libraries such as webgpu.h (GPU compute and graphics), libcurl (HTTP clients), SQLite (embedded databases), libpng/libjpeg (image processing), and countless domain-specific libraries. For AI agent tooling—Zero's core target—this opens access to ML inference runtimes (ONNX Runtime, TensorFlow Lite C API), vector databases, and specialized hardware accelerators. Rather than reimplementing these battle-tested libraries as compiler builtins, extern fun enables Zero to leverage decades of C ecosystem development while maintaining Zero's safety and expressiveness at the application layer.
Changes
1. Parser (native/zero-c/src/parser.c)
Added extern function syntax:
extern fun name(...) -> Type(no function body)extern c funvariant for consistency with existingextern c "header.h"Function.extern_c = trueflag in ASTKey modifications:
externkeyword with optionalcqualifierextern c "header.h"(header import) fromextern c fun(function declaration)2. Checker (native/zero-c/src/checker.c)
Added ABI validation:
raises(C ABI incompatible)Key modifications:
validate_extern_c_function()enforces ABI constraints3. IR Layer (native/zero-c/src/ir.c)
Added extern call representation:
IrValue.extern_symbol_namefor extern function callsextern_symbol_name != NULLindicates external linkageextern_symbol_name == NULLindicates internal call (usescallee_index)Key modifications:
direct_functions(no IR lowering needed)extern_symbol_namein cleanup4. ELF64 Emitter (native/zero-c/src/emit_elf64.c)
Added undefined symbol generation and PLT relocations:
Key modifications:
ExternCallSitestructure and tracking arraysELF structure changes:
5. AST Definition (native/zero-c/include/zero.h)
Added extern tracking fields:
Function.extern_cflagIrValue.extern_symbol_namepointerUsage
Basic usage (libc functions)
Compile and link:
Custom C libraries
C helper (helpers.c):
Zero code:
Build:
Testing
Conformance tests
Four conformance test cases added in
conformance/native/pass/:abs()function call (baseline)Integration test script
tests/extern-fun-test.shperforms full compile-link-execute validation:ELF validation
Generated object files verified with objdump:
0000000000000000 *UND* absR_X86_64_PLT32 abs-0x4Limitations
1. C ABI type restrictions (enforced by checker diagnostic 3031)
Only C-compatible types allowed:
2. Manual linking required
This PR does NOT include automatic linking driver. Users must:
bin/zero build --emit obj file.0 --out file.occ file.o [other.o...] -o executableAutomatic linking (detecting required libraries, invoking linker) is deferred to future work.
3. x86_64 Linux only
Implementation uses:
Other platforms (aarch64, Windows, macOS) not supported in this PR.
Future work
ccautomaticallyextern fun printf(format: Ptr[u8], ...) -> i32(requires parser + IR changes)Conformance tests: Located in
conformance/native/pass/extern-fun-*.0. Run./tests/extern-fun-test.shto verify full linking and execution.