kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

runtime_abi.h (5450B)


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