runtime_abi.h (4927B)
1 #ifndef KIT_LANG_WASM_RUNTIME_ABI_H 2 #define KIT_LANG_WASM_RUNTIME_ABI_H 3 4 #include <stdint.h> 5 6 /* The public header is the source of truth for the shared emitter<->reader 7 * types: the KIT_WASM_MEMORY_* flags, KitWasmImportKind, KitWasmMemoryLayout, 8 * KitWasmRuntimeLayout, and the KitWasmInstance forward declaration. The 9 * internal-only kit-instance ABI types live below. */ 10 #include <kit/wasm.h> 11 12 typedef struct KitWasmMemory { 13 uint8_t* data; 14 uint64_t pages; 15 uint64_t max_pages; 16 uint32_t flags; 17 } KitWasmMemory; 18 19 typedef struct KitWasmFuncImport { 20 void* fn; 21 } KitWasmFuncImport; 22 23 typedef struct KitWasmGlobalImport { 24 void* addr; 25 } KitWasmGlobalImport; 26 27 typedef struct KitWasmTableEntry { 28 void* fn; 29 uint32_t typeidx; 30 } KitWasmTableEntry; 31 32 typedef struct KitWasmTable { 33 KitWasmTableEntry* entries; 34 uint32_t len; 35 uint32_t max; 36 } KitWasmTable; 37 38 /* Bulk-memory passive data segment slot. Populated by __kit_wasm_init from 39 * the module's WASM_SEG_PASSIVE data segments. `base` points to a const data 40 * symbol interned by codegen; `len` is the segment's byte length. data.drop 41 * sets `len = 0`. memory.init copies from this region into linear memory. */ 42 typedef struct KitWasmPassiveDataSegment { 43 const uint8_t* base; 44 uint64_t len; 45 } KitWasmPassiveDataSegment; 46 47 /* Bulk-memory passive elem segment slot. Populated by __kit_wasm_init from 48 * the module's WASM_SEG_PASSIVE element segments. `entries` points to a const 49 * array of {fn, typeidx} table-entry records; `length` is the entry count. 50 * elem.drop sets `length = 0`. table.init copies entries into a table. */ 51 typedef struct KitWasmPassiveElemSegment { 52 const KitWasmTableEntry* entries; 53 uint32_t length; 54 } KitWasmPassiveElemSegment; 55 56 /* KitWasmInstance is forward-declared in kit/wasm.h. The compiler emits a 57 * module-specific instance layout with KitWasmMemory first, followed by import 58 * slots, runtime tables, and lowered global slots. Table entry storage is 59 * instance-owned for defined tables; embedders fill import slots before calling 60 * the init hook. */ 61 typedef void (*KitWasmInitFn)(KitWasmInstance*); 62 63 /* ---- Host-import metadata (subagent C) ---- 64 * 65 * Each module that the wasm frontend lowers emits readonly symbols alongside 66 * __kit_wasm_init so a runtime can resolve imports by (module, field) without 67 * needing the source WasmModule struct: 68 * 69 * __kit_wasm_imports : KitWasmImportDesc[] (one per declared 70 * import; if the module has 71 * none, the symbol is 72 * absent) 73 * __kit_wasm_nimports : uint32_t (array length) 74 * __kit_wasm_nfunc_import_types : uint32_t (unique function import 75 * signature count) 76 * __kit_wasm_types : KitWasmTypeDesc[] (unique WasmFuncType 77 * signatures referenced by 78 * imported functions; 79 * absent when no functions 80 * are imported) 81 * __kit_wasm_memory_import_types : KitWasmMemoryImportDesc[] 82 * __kit_wasm_table_import_types : KitWasmTableImportDesc[] 83 * __kit_wasm_global_import_types : KitWasmGlobalImportDesc[] 84 * 85 * The wire format below is the C ABI the JIT/AOT image exposes. Param/result 86 * bytes use the raw wasm WasmValType encoding (I32=0x7f, I64=0x7e, F32=0x7d, 87 * F64=0x7c, FUNCREF=0x70, EXTERNREF=0x6f) — the binder translates these to 88 * the public KitWasmValType enum when invoking host resolvers. */ 89 90 typedef struct KitWasmImportDesc { 91 const char* module; /* image-owned, NUL-terminated */ 92 const char* field; /* image-owned, NUL-terminated */ 93 uint32_t kind; /* KitWasmImportKind */ 94 uint32_t desc_index; /* index into the kind-specific descriptor array */ 95 uint32_t slot_offset; /* byte offset of the instance slot for this import */ 96 uint32_t reserved; 97 } KitWasmImportDesc; 98 99 typedef struct KitWasmTypeDesc { 100 const uint8_t* params; /* nparams WasmValType bytes */ 101 uint32_t nparams; 102 const uint8_t* results; /* nresults WasmValType bytes */ 103 uint32_t nresults; 104 } KitWasmTypeDesc; 105 106 typedef struct KitWasmMemoryImportDesc { 107 uint64_t min_pages; 108 uint64_t max_pages; 109 uint32_t flags; /* KIT_WASM_MEMORY_* */ 110 uint32_t has_max; /* nonzero when the import declared an explicit maximum */ 111 } KitWasmMemoryImportDesc; 112 113 typedef struct KitWasmTableImportDesc { 114 uint32_t elem_type; /* raw WasmValType byte */ 115 uint32_t min; 116 uint32_t max; 117 uint32_t has_max; /* nonzero when the import declared an explicit maximum */ 118 } KitWasmTableImportDesc; 119 120 typedef struct KitWasmGlobalImportDesc { 121 uint32_t type; /* raw WasmValType byte */ 122 uint32_t mutable_; 123 } KitWasmGlobalImportDesc; 124 125 #endif