core.h (5501B)
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; 86 u8 pad[3]; 87 } SourceInclude; 88 89 typedef struct SourceExpansion { 90 SrcLoc spelling_loc; 91 SrcLoc expansion_loc; 92 Sym macro_name; 93 } SourceExpansion; 94 95 typedef struct SourceDepIter SourceDepIter; 96 97 SourceManager* source_new(Compiler*); 98 void source_free(SourceManager*); 99 100 KitStatus source_add_file(SourceManager*, const char* path, int system_header, 101 u32* id_out); 102 KitStatus source_add_memory(SourceManager*, KitSlice name, u32* id_out); 103 KitStatus source_add_builtin(SourceManager*, KitSlice name, u32* id_out); 104 KitStatus source_add_include(SourceManager*, u32 includer_file_id, 105 u32 included_file_id, SrcLoc include_loc, 106 int system); 107 KitStatus source_add_macro_expansion(SourceManager*, Sym macro_name, 108 SrcLoc spelling_loc, SrcLoc expansion_loc, 109 u32* id_out); 110 111 const SourceFile* source_file(SourceManager*, u32 file_id); 112 const SourceExpansion* source_expansion(SourceManager*, u32 expansion_file_id); 113 SrcLoc source_spelling_loc(SourceManager*, SrcLoc); 114 SrcLoc source_expansion_loc(SourceManager*, SrcLoc); 115 116 SourceDepIter* source_depiter_new(SourceManager*); 117 const SourceInclude* source_depiter_next(SourceDepIter*); 118 void source_depiter_free(SourceDepIter*); 119 120 typedef struct CompilerCleanup CompilerCleanup; 121 typedef struct PanicFrame PanicFrame; 122 123 struct PanicFrame { 124 jmp_buf env; 125 PanicFrame* prev; 126 }; 127 128 struct KitCompiler { 129 const KitContext* ctx; 130 Pool* global; 131 Arena* tu; 132 Arena* scratch; 133 SourceManager* sources; 134 TargetABI* abi; 135 const KitTarget* target_ref; 136 Target target; 137 CompilerCleanup* cleanup; 138 const KitFrontendVTable* frontends[KIT_LANG_COUNT]; 139 void* cg_api; 140 void (*cg_api_free)(Compiler*); 141 /* Wasm frontend host-import configuration. Stashed by 142 * kit_wasm_set_host_imports; consumed by runners that call 143 * kit_wasm_bind_host_imports after the link image is produced. The 144 * pointers are borrowed: callers own the lifetime. */ 145 const void* wasm_host_imports; /* KitWasmHostImport array */ 146 size_t wasm_host_nimports; 147 void* wasm_host_resolve; /* KitWasmResolveFn */ 148 void* wasm_host_user; 149 /* Optional InterpProgram sink (struct InterpProgram*). When non-NULL, the 150 * optimizer additionally lowers each function through opt_run_o1_interp into 151 * this program for the threaded interpreter. Set by 152 * kit_interp_program_attach; borrowed (the caller owns the program). */ 153 void* interp_sink; 154 PanicFrame* panic_frame; 155 /* Keep jmp_buf last: its size comes from the including C environment 156 * (host libc for some tests, rt/include for libkit), and must not shift 157 * the offsets of the fields above across those builds. 158 * 159 * New nested panic boundaries should use PanicFrame, not this legacy slot: 160 * copying jmp_buf bytes is not a portable way to save nested handlers and 161 * can corrupt sanitizer-managed jump state. The slot stays for older 162 * internal tests/tools that install one direct setjmp on a compiler. */ 163 jmp_buf panic; 164 }; 165 166 KitStatus compiler_init(Compiler*, const KitTarget*, const KitContext*); 167 void compiler_fini(Compiler*); 168 169 CompilerCleanup* compiler_defer(Compiler*, void (*fn)(void*), void* arg); 170 void compiler_undefer(Compiler*, CompilerCleanup*); 171 void compiler_run_cleanups(Compiler*); 172 173 _Noreturn void compiler_panic(Compiler*, SrcLoc, const char* fmt, ...); 174 _Noreturn void compiler_panicv(Compiler*, SrcLoc, const char* fmt, va_list); 175 176 void compiler_panic_push(Compiler*, PanicFrame*); 177 void compiler_panic_pop(Compiler*, PanicFrame*); 178 179 #endif