Files
ziran/std/bundle_host.zi
T
waoziandClaude Opus 5.5 9ad5f481ef Name the tools by what they turn into what, and run verified ZX natively
The tools are renamed after their input and output: zi2zx lowers source
or ZIR to executable ZX, zx2native turns ZX into a native ELF, zx2wasm
into a wasm32 module, and zi2c, zi2go and zi2zir keep their roles under
cmd/ folders of the same names. zi2asm and the cmd/zir-* folders are
gone, and tests/tool_names.sh keeps the old names out. zx2native keeps
the direct encoder as its default; the machine pipeline replaces it once
it reaches parity.

The embedding API can run a verified bundle as x86-64 machine code in
memory. Code is written to RW pages, relocated, then switched to RX,
never RWX, and each instance gets its own data
(cmd/zir/zir_native_memory.c, zir_host_native.c). Backend selection is
BundleBackendPolicy: Engine and Native are strict, and Auto chooses by
platform only, never by whether one program fits. A native gap is a
named error, never a switch to the engine. Desktop x86-64 Auto stays on
the engine until the native backend is complete.

cmd/compiler_machine_image.zi adds the target-neutral machine image
contract. The machine verifiers reject unknown relocation kinds,
out-of-range symbols and addends, non-host external calls, native-width
mismatches and missing stack or fuel facts. The machine pipeline lowers
and encodes more of LIR. Docs, site pages, Kryon's build references and
the benchmarks use the new names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-14 22:07:42 -03:00

452 lines
15 KiB
Ziran

// Native C ABI for embedding a portable Ziran bundle. The runtime owns the
// bundle and instance; callers own binding contexts and borrowed result data.
#import "byte_text_linux"
#scope_export
runtime :: #system_library "ziran";
HostValueKind :: enum {
HostVoid :: 0
HostInteger :: 1
HostUnsigned :: 2
HostReal :: 3
HostString :: 4
HostRecord :: 5
HostSlice :: 6
HostPointer :: 7
HostArray :: 8
}
HostValue :: struct {
kind: s32
type_name: *u8
integer: s64
bits: u64
real: float64
data: *u8
length: usize
fields: *HostField
field_count: usize
elements: *HostValue
pointer: *void
}
HostField :: struct {
name: *u8
value: HostValue
}
HostCall :: #type (context: *void, module: *u8, function: *u8,
arguments: *HostValue, count: s32, result: *HostValue) -> s32 #c_call;
HostBinding :: struct {
module: *u8
function: *u8
call: HostCall
context: *void
}
// Auto is a platform choice, never a program-fit fallback: browsers use Wasm,
// CPUs whose native backend has passed complete parity use Native, and all
// other CPUs use Engine.
BundleBackendPolicy :: enum s32 { Auto; Engine; Native }
BundleExecutionBackend :: enum s32 {
None; Engine; Wasm; NativeX86_64; NativeAArch64; NativeArmV7
}
BundleInstanceOptions :: struct {
backend: BundleBackendPolicy
max_steps: s32
}
BundleStateValue :: struct {
module: *u8
name: *u8
type: *u8
value: HostValue
}
BundleStateMigrate :: #type (context: *void, old_state: *void,
new_state: *void) -> s32 #c_call;
BundleComponentVerify :: #type (context: *void,
instance: *void) -> s32 #c_call;
BundleCapabilityAbi :: struct {
module: *u8
function: *u8
signature: *u8
arguments: *u8
return_type: *u8
variable_arguments: s32
reads_arguments: s32
}
#scope_file
RawBundleOpen :: (path: *u8) -> *void #foreign runtime "BundleOpen";
RawBundleOpenBytes :: (data: *u8, size: usize) -> *void #foreign runtime "BundleOpenBytes";
RawBundleClose :: (bundle: *void) #foreign runtime "BundleClose";
RawBundleCapabilityCount :: (bundle: *void) -> usize #foreign runtime "BundleCapabilityCount";
RawBundleCapabilityModule :: (bundle: *void, index: usize) -> *u8 #foreign runtime "BundleCapabilityModule";
RawBundleCapabilityFunction :: (bundle: *void, index: usize) -> *u8 #foreign runtime "BundleCapabilityFunction";
RawBundleCapabilityAbiAt :: (bundle: *void, index: usize,
abi: *BundleCapabilityAbi) -> s32 #foreign runtime "BundleCapabilityAbiAt";
RawBundleAbiCompatible :: (left: *void, right: *void) -> s32 #foreign runtime "BundleAbiCompatible";
RawBundleEntryModuleName :: (bundle: *void) -> *u8 #foreign runtime "BundleEntryModuleName";
RawBundleEntryFunctionName :: (bundle: *void) -> *u8 #foreign runtime "BundleEntryFunctionName";
RawBundleCodeIdentity :: (bundle: *void, identity: *u8) #foreign runtime "BundleCodeIdentity";
RawBundleAssetCount :: (bundle: *void) -> usize #foreign runtime "BundleAssetCount";
RawBundleAssetName :: (bundle: *void, index: usize) -> *u8 #foreign runtime "BundleAssetName";
RawBundleAssetData :: (bundle: *void, index: usize) -> *u8 #foreign runtime "BundleAssetData";
RawBundleAssetSize :: (bundle: *void, index: usize) -> usize #foreign runtime "BundleAssetSize";
RawBundleInstantiate :: (bundle: *void, bindings: *HostBinding,
count: usize) -> *void #foreign runtime "BundleInstantiate";
RawBundleInstantiateLimited :: (bundle: *void, bindings: *HostBinding,
count: usize, max_steps: s32) -> *void #foreign runtime "BundleInstantiateLimited";
RawBundleInstantiateWithOptions :: (bundle: *void, bindings: *HostBinding,
count: usize, options: *BundleInstanceOptions) -> *void #foreign runtime "BundleInstantiateWithOptions";
RawBundleInstanceBackend :: (instance: *void) -> BundleExecutionBackend #foreign runtime "BundleInstanceBackend";
RawBundleNativeProblem :: (bundle: *void) -> *u8 #foreign runtime "BundleNativeProblem";
RawBundleInstanceRun :: (instance: *void, result: *s64,
has_result: *s32) -> s32 #foreign runtime "BundleInstanceRun";
RawBundleInstanceClose :: (instance: *void) #foreign runtime "BundleInstanceClose";
RawBundleInstanceLimitSteps :: (instance: *void, max_steps: s32) #foreign runtime "BundleInstanceLimitSteps";
RawBundleInstanceMatchFieldsByName :: (instance: *void, enabled: s32) #foreign runtime "BundleInstanceMatchFieldsByName";
RawBundleInstanceExportState :: (instance: *void,
state: **void) -> s32 #foreign runtime "BundleInstanceExportState";
RawBundleInstanceImportState :: (instance: *void,
state: *void) -> s32 #foreign runtime "BundleInstanceImportState";
RawBundleStateCount :: (state: *void) -> usize #foreign runtime "BundleStateCount";
RawBundleStateValueBytes :: (state: *void) -> usize #foreign runtime "BundleStateValueBytes";
RawBundleStateValueAt :: (state: *void, index: usize,
value: *BundleStateValue) -> s32 #foreign runtime "BundleStateValueAt";
RawBundleStateValueRef :: (state: *void, index: usize) -> *BundleStateValue #foreign runtime "BundleStateValueRef";
RawBundleStateFind :: (state: *void, module: *u8, name: *u8,
index: *usize) -> s32 #foreign runtime "BundleStateFind";
RawBundleStateSetInteger :: (state: *void, index: usize,
value: s64) -> s32 #foreign runtime "BundleStateSetInteger";
RawBundleStateSetUnsigned :: (state: *void, index: usize,
value: u64) -> s32 #foreign runtime "BundleStateSetUnsigned";
RawBundleStateSetReal :: (state: *void, index: usize,
value: float64) -> s32 #foreign runtime "BundleStateSetReal";
RawBundleStateSetString :: (state: *void, index: usize, data: *u8,
length: usize) -> s32 #foreign runtime "BundleStateSetString";
RawBundleStateClose :: (state: *void) #foreign runtime "BundleStateClose";
RawBundleComponentOpen :: (bundle: *void, bindings: *HostBinding,
count: usize, max_steps: s32) -> *void #foreign runtime "BundleComponentOpen";
RawBundleComponentAcquire :: (component: *void) -> *void #foreign runtime "BundleComponentAcquire";
RawBundleComponentHandleValid :: (handle: *void) -> s32 #foreign runtime "BundleComponentHandleValid";
RawBundleComponentRun :: (handle: *void, result: *s64,
has_result: *s32) -> s32 #foreign runtime "BundleComponentRun";
RawBundleComponentReplace :: (component: *void, replacement: *void,
migrate: BundleStateMigrate, verify: BundleComponentVerify, context: *void,
new_handle: **void) -> s32 #foreign runtime "BundleComponentReplace";
RawBundleComponentLiveValueBytes :: (component: *void) -> usize #foreign runtime "BundleComponentLiveValueBytes";
RawBundleComponentClose :: (component: *void) #foreign runtime "BundleComponentClose";
RawBundleComponentHandleClose :: (handle: *void) #foreign runtime "BundleComponentHandleClose";
#scope_export
#program_export
OpenBundle :: (path: *u8) -> *void { return RawBundleOpen(path) }
#program_export
OpenBundleBytes :: (data: *u8, size: u64) -> *void {
narrowed := cast(usize)size
if cast(u64)narrowed != size {
return null
}
return RawBundleOpenBytes(data, narrowed)
}
#program_export
CloseBundle :: (bundle: *void) { RawBundleClose(bundle) }
// Borrowed names remain valid until CloseBundle. A host can bind the exact
// package-qualified names in a downloaded program without knowing its lock.
#program_export
BundleCapabilities :: (bundle: *void) -> u64 {
return cast(u64)RawBundleCapabilityCount(bundle)
}
#program_export
BundleCapabilityModuleName :: (bundle: *void, index: u64) -> *u8 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return null
}
return RawBundleCapabilityModule(bundle, narrowed)
}
#program_export
BundleCapabilityFunctionName :: (bundle: *void, index: u64) -> *u8 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return null
}
return RawBundleCapabilityFunction(bundle, narrowed)
}
#program_export
ReadBundleCapabilityAbi :: (bundle: *void, index: u64,
abi: *BundleCapabilityAbi) -> s32 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return 0
}
return RawBundleCapabilityAbiAt(bundle, narrowed, abi)
}
#program_export
BundlesAbiCompatible :: (left: *void, right: *void) -> bool {
return RawBundleAbiCompatible(left, right) != 0
}
#program_export
BundleEntryModule :: (bundle: *void) -> *u8 {
return RawBundleEntryModuleName(bundle)
}
#program_export
BundleEntryFunction :: (bundle: *void) -> *u8 {
return RawBundleEntryFunctionName(bundle)
}
#program_export
BundleCodeDigest :: (bundle: *void, identity: *u8) {
RawBundleCodeIdentity(bundle, identity)
}
#program_export
BundleAssets :: (bundle: *void) -> u64 { return cast(u64)RawBundleAssetCount(bundle) }
#program_export
BundleAssetPath :: (bundle: *void, index: u64) -> *u8 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return null
}
return RawBundleAssetName(bundle, narrowed)
}
#program_export
BundleAssetBytes :: (bundle: *void, index: u64) -> *u8 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return null
}
return RawBundleAssetData(bundle, narrowed)
}
#program_export
BundleAssetLength :: (bundle: *void, index: u64) -> u64 {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return 0
}
return cast(u64)RawBundleAssetSize(bundle, narrowed)
}
#program_export
InstantiateBundle :: (bundle: *void, bindings: *HostBinding,
count: u64) -> *void {
narrowed := cast(usize)count
if cast(u64)narrowed != count {
return null
}
return RawBundleInstantiate(bundle, bindings, narrowed)
}
// One positive step budget covering global initialization and all runs.
#program_export
InstantiateBundleLimited :: (bundle: *void, bindings: *HostBinding,
count: u64, max_steps: s32) -> *void {
narrowed := cast(usize)count
if cast(u64)narrowed != count || max_steps <= 0 {
return null
}
return RawBundleInstantiateLimited(bundle, bindings, narrowed, max_steps)
}
#program_export
InstantiateBundleWithOptions :: (bundle: *void, bindings: *HostBinding,
count: u64, options: *BundleInstanceOptions) -> *void {
narrowed := cast(usize)count
if cast(u64)narrowed != count || options == null || options.max_steps < 0 {
return null
}
return RawBundleInstantiateWithOptions(bundle, bindings, narrowed, options)
}
#program_export
GetBundleInstanceBackend :: (instance: *void) -> BundleExecutionBackend {
return RawBundleInstanceBackend(instance)
}
#program_export
GetBundleNativeProblem :: (bundle: *void) -> string {
return TextFromCString(RawBundleNativeProblem(bundle))
}
#program_export
RunBundleInstance :: (instance: *void, result: *s64,
has_result: *s32) -> s32 {
return RawBundleInstanceRun(instance, result, has_result)
}
#program_export
CloseBundleInstance :: (instance: *void) { RawBundleInstanceClose(instance) }
#program_export
LimitBundleSteps :: (instance: *void, max_steps: s32) {
RawBundleInstanceLimitSteps(instance, max_steps)
}
// Records returned to the instance match fields by name, so a bundle built
// against an older or newer record layout still runs.
#program_export
MatchBundleFieldsByName :: (instance: *void, enabled: bool) {
RawBundleInstanceMatchFieldsByName(instance, ifx enabled then 1 else 0)
}
#program_export
ExportBundleState :: (instance: *void, state: **void) -> bool {
return RawBundleInstanceExportState(instance, state) != 0
}
#program_export
ImportBundleState :: (instance: *void, state: *void) -> bool {
return RawBundleInstanceImportState(instance, state) != 0
}
#program_export
BundleStateValueCount :: (state: *void) -> u64 {
return cast(u64)RawBundleStateCount(state)
}
#program_export
BundleStateBytes :: (state: *void) -> u64 {
return cast(u64)RawBundleStateValueBytes(state)
}
#program_export
ReadBundleStateValue :: (state: *void, index: u64,
value: *BundleStateValue) -> bool {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return false
}
return RawBundleStateValueAt(state, narrowed, value) != 0
}
#program_export
BundleStateValueReference :: (state: *void, index: u64) -> *BundleStateValue {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return null
}
return RawBundleStateValueRef(state, narrowed)
}
#program_export
FindBundleStateValue :: (state: *void, module: *u8, name: *u8,
index: *u64) -> bool {
narrowed := cast(usize)0
if RawBundleStateFind(state, module, name, *narrowed) == 0 {
return false
}
<<index = cast(u64)narrowed
return true
}
#program_export
SetBundleStateInteger :: (state: *void, index: u64, value: s64) -> bool {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return false
}
return RawBundleStateSetInteger(state, narrowed, value) != 0
}
#program_export
SetBundleStateUnsigned :: (state: *void, index: u64, value: u64) -> bool {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return false
}
return RawBundleStateSetUnsigned(state, narrowed, value) != 0
}
#program_export
SetBundleStateReal :: (state: *void, index: u64, value: float64) -> bool {
narrowed := cast(usize)index
if cast(u64)narrowed != index {
return false
}
return RawBundleStateSetReal(state, narrowed, value) != 0
}
#program_export
SetBundleStateString :: (state: *void, index: u64, data: *u8,
length: u64) -> bool {
narrowed_index := cast(usize)index
narrowed_length := cast(usize)length
if cast(u64)narrowed_index != index ||
cast(u64)narrowed_length != length {
return false
}
return RawBundleStateSetString(state, narrowed_index, data,
narrowed_length) != 0
}
#program_export
CloseBundleState :: (state: *void) { RawBundleStateClose(state) }
#program_export
OpenBundleComponent :: (bundle: *void, bindings: *HostBinding,
count: u64, max_steps: s32) -> *void {
narrowed := cast(usize)count
if cast(u64)narrowed != count || max_steps <= 0 {
return null
}
return RawBundleComponentOpen(bundle, bindings, narrowed, max_steps)
}
#program_export
AcquireBundleComponent :: (component: *void) -> *void {
return RawBundleComponentAcquire(component)
}
#program_export
BundleComponentHandleIsValid :: (handle: *void) -> bool {
return RawBundleComponentHandleValid(handle) != 0
}
#program_export
RunBundleComponent :: (handle: *void, result: *s64,
has_result: *s32) -> s32 {
return RawBundleComponentRun(handle, result, has_result)
}
#program_export
ReplaceBundleComponent :: (component: *void, replacement: *void,
migrate: BundleStateMigrate, verify: BundleComponentVerify,
context: *void, new_handle: **void) -> bool {
return RawBundleComponentReplace(component, replacement, migrate, verify,
context, new_handle) != 0
}
#program_export
BundleComponentLiveBytes :: (component: *void) -> u64 {
return cast(u64)RawBundleComponentLiveValueBytes(component)
}
#program_export
CloseBundleComponent :: (component: *void) {
RawBundleComponentClose(component)
}
#program_export
CloseBundleComponentHandle :: (handle: *void) {
RawBundleComponentHandleClose(handle)
}