core.h (6372B)
1 #ifndef KIT_INTERNAL_CORE_H 2 #define KIT_INTERNAL_CORE_H 3 4 #include <kit/compile.h> 5 #include <kit/core.h> 6 #include <kit/object.h> 7 #include <kit/source.h> 8 #include <setjmp.h> 9 #include <stdarg.h> 10 #include <stddef.h> 11 #include <stdint.h> 12 13 /* Short integer aliases used throughout libkit's internal headers. */ 14 typedef int8_t i8; 15 typedef int16_t i16; 16 typedef int32_t i32; 17 typedef int64_t i64; 18 typedef uint8_t u8; 19 typedef uint16_t u16; 20 typedef uint32_t u32; 21 typedef uint64_t u64; 22 23 /* Internal aliases for types that also have a public Kit-prefixed name. */ 24 typedef KitCompiler Compiler; 25 typedef KitContext Context; 26 typedef KitHeap Heap; 27 typedef KitDiagSink DiagSink; 28 typedef KitWriter Writer; 29 typedef KitTargetSpec Target; 30 typedef KitObjBuilder ObjBuilder; 31 typedef enum KitArchKind ArchKind; 32 typedef enum KitOSKind OSKind; 33 typedef enum KitObjFmt ObjFmt; 34 35 /* Internal-only forward declarations. */ 36 typedef struct Arena Arena; 37 typedef struct Pool Pool; 38 typedef struct TargetABI TargetABI; 39 typedef struct SourceManager SourceManager; 40 41 typedef u32 Sym; 42 43 typedef u32 BytesId; 44 #define BYTES_NONE 0u 45 46 typedef KitSrcLoc SrcLoc; 47 48 // Canonical "no source location" — single source of truth. Replaces the 49 // per-TU `static SrcLoc no_loc(void)` helpers formerly duplicated across the 50 // obj/ and link/ writers (used mostly as the location arg to compiler_panic). 51 #define SRCLOC_NONE ((SrcLoc){0, 0, 0}) 52 53 typedef struct SrcRange { 54 SrcLoc begin; 55 SrcLoc end; 56 } SrcRange; 57 58 struct KitTarget { 59 const KitContext* ctx; 60 Target spec; 61 u64* feature_words; 62 u32 nfeature_words; 63 }; 64 65 typedef enum SourceFileKind { 66 SRC_FILE_REAL, 67 SRC_FILE_MEMORY, 68 SRC_FILE_BUILTIN, 69 SRC_FILE_MACRO, 70 } SourceFileKind; 71 72 typedef struct SourceFile { 73 u32 id; 74 Sym name; 75 Sym path; 76 u8 kind; 77 u8 system_header; 78 u16 pad; 79 } SourceFile; 80 81 typedef struct SourceInclude { 82 u32 includer_file_id; 83 u32 included_file_id; 84 SrcLoc include_loc; 85 u8 system; /* spelling form: <...> (1) vs "..." (0) */ 86 u8 resolved_system; /* resolved via a system (-isystem) include dir */ 87 u8 pad[2]; 88 } SourceInclude; 89 90 typedef struct SourceExpansion { 91 SrcLoc spelling_loc; 92 SrcLoc expansion_loc; 93 Sym macro_name; 94 } SourceExpansion; 95 96 typedef struct SourceDepIter SourceDepIter; 97 98 SourceManager* source_new(Compiler*); 99 void source_free(SourceManager*); 100 101 KitStatus source_add_file(SourceManager*, const char* path, int system_header, 102 u32* id_out); 103 KitStatus source_add_memory(SourceManager*, KitSlice name, u32* id_out); 104 /* Register an in-memory file from a pre-interned name Sym (skips the per-call 105 * intern + strlen; the file_id sequence is identical to source_add_memory). */ 106 KitStatus source_add_memory_sym(SourceManager*, Sym name, u32* id_out); 107 KitStatus source_add_builtin(SourceManager*, KitSlice name, u32* id_out); 108 KitStatus source_add_include(SourceManager*, u32 includer_file_id, 109 u32 included_file_id, SrcLoc include_loc, 110 int system, int resolved_system); 111 KitStatus source_add_macro_expansion(SourceManager*, Sym macro_name, 112 SrcLoc spelling_loc, SrcLoc expansion_loc, 113 u32* id_out); 114 115 const SourceFile* source_file(SourceManager*, u32 file_id); 116 const SourceExpansion* source_expansion(SourceManager*, u32 expansion_file_id); 117 SrcLoc source_spelling_loc(SourceManager*, SrcLoc); 118 SrcLoc source_expansion_loc(SourceManager*, SrcLoc); 119 120 SourceDepIter* source_depiter_new(SourceManager*); 121 const SourceInclude* source_depiter_next(SourceDepIter*); 122 void source_depiter_free(SourceDepIter*); 123 124 typedef struct CompilerCleanup CompilerCleanup; 125 typedef struct PanicFrame PanicFrame; 126 127 struct PanicFrame { 128 jmp_buf env; 129 PanicFrame* prev; 130 }; 131 132 struct KitCompiler { 133 const KitContext* ctx; 134 Pool* global; 135 Arena* tu; 136 Arena* scratch; 137 SourceManager* sources; 138 TargetABI* abi; 139 const KitTarget* target_ref; 140 Target target; 141 CompilerCleanup* cleanup; 142 const KitFrontendVTable** frontends; 143 u32 nfrontends; 144 u32 frontends_cap; 145 void* cg_api; 146 void (*cg_api_free)(Compiler*); 147 /* Borrowed pointer to the per-compiler builtin 148 * ABITypeInfo[KIT_CG_BUILTIN_COUNT] layout table inside cg_api state: set 149 * when builtins initialize, NULLed on cg_api teardown. Lets abi_cg_type_info 150 * resolve a builtin's size/align/scalar profile with one inline indexed load 151 * instead of a cross-TU layout-cache call. Opaque here (const void*) so 152 * core.h need not pull abi.h; abi.c casts it to const ABITypeInfo*. */ 153 const void* cg_builtin_layout; 154 /* Wasm frontend host-import configuration. Stashed by 155 * kit_wasm_set_host_imports; consumed by runners that call 156 * kit_wasm_bind_host_imports after the link image is produced. The 157 * pointers are borrowed: callers own the lifetime. */ 158 const void* wasm_host_imports; /* KitWasmHostImport array */ 159 size_t wasm_host_nimports; 160 void* wasm_host_resolve; /* KitWasmResolveFn */ 161 void* wasm_host_user; 162 /* Optional InterpProgram sink (struct InterpProgram*). When non-NULL, the 163 * optimizer additionally lowers each function through opt_run_o1_interp into 164 * this program for the threaded interpreter. Set by 165 * kit_interp_program_attach; borrowed (the caller owns the program). */ 166 void* interp_sink; 167 PanicFrame* panic_frame; 168 /* Keep jmp_buf last: its size comes from the including C environment 169 * (host libc for some tests, rt/include for libkit), and must not shift 170 * the offsets of the fields above across those builds. 171 * 172 * New nested panic boundaries should use PanicFrame, not this legacy slot: 173 * copying jmp_buf bytes is not a portable way to save nested handlers and 174 * can corrupt sanitizer-managed jump state. The slot stays for older 175 * internal tests/tools that install one direct setjmp on a compiler. */ 176 jmp_buf panic; 177 }; 178 179 KitStatus compiler_init(Compiler*, const KitTarget*, const KitContext*); 180 void compiler_fini(Compiler*); 181 182 CompilerCleanup* compiler_defer(Compiler*, void (*fn)(void*), void* arg); 183 void compiler_undefer(Compiler*, CompilerCleanup*); 184 void compiler_run_cleanups(Compiler*); 185 186 _Noreturn void compiler_panic(Compiler*, SrcLoc, const char* fmt, ...); 187 _Noreturn void compiler_panicv(Compiler*, SrcLoc, const char* fmt, va_list); 188 189 void compiler_panic_push(Compiler*, PanicFrame*); 190 void compiler_panic_pop(Compiler*, PanicFrame*); 191 192 #endif