lang_registry.c (2132B)
1 /* Language frontend registry. 2 * 3 * This file is the *only* place in libkit that checks 4 * KIT_LANG_*_ENABLED. It runs during compiler construction and wires 5 * each compiled-in frontend's vtable into c->frontends[] so the public 6 * compile/pipeline paths can dispatch by KitLanguage without any 7 * host-side bootstrap. 8 * 9 * The asm frontend lives inside the codegen substrate, but automatic 10 * registration is gated by KIT_LANG_ASM_ENABLED. 11 * 12 * Third parties may still call kit_register_frontend() to install or 13 * override any slot after construction; that public API is unchanged. 14 */ 15 16 #include "api/lang_registry.h" 17 18 #include <kit/compile.h> 19 20 #include "kit/config.h" 21 22 /* Defined in src/api/compile.c, alongside the asm frontend's 23 * new/compile/free functions. Treated as part of the codegen substrate 24 * (no per-frontend lang/ directory), so its declaration lives here 25 * rather than in a public header. */ 26 #if KIT_LANG_ASM_ENABLED 27 extern const KitFrontendVTable kit_asm_frontend_vtable; 28 #endif 29 30 #if KIT_LANG_C_ENABLED 31 #include "c/c.h" 32 #endif 33 #if KIT_LANG_TOY_ENABLED 34 #include "toy/toy.h" 35 #endif 36 #if KIT_LANG_WASM_ENABLED 37 #include "wasm/wasm.h" 38 #endif 39 40 void lang_registry_init(KitCompiler* c) { 41 #if KIT_LANG_ASM_ENABLED 42 (void)kit_register_frontend(c, KIT_LANG_ASM, &kit_asm_frontend_vtable); 43 #endif 44 #if KIT_LANG_C_ENABLED 45 (void)kit_register_frontend(c, KIT_LANG_C, &kit_c_frontend_vtable); 46 #endif 47 #if KIT_LANG_TOY_ENABLED 48 (void)kit_register_frontend(c, KIT_LANG_TOY, &kit_toy_frontend_vtable); 49 #endif 50 #if KIT_LANG_WASM_ENABLED 51 (void)kit_register_frontend(c, KIT_LANG_WASM, &kit_wasm_frontend_vtable); 52 #endif 53 } 54 55 const KitFrontendVTable* lang_registry_vtable(KitLanguage lang) { 56 switch (lang) { 57 #if KIT_LANG_ASM_ENABLED 58 case KIT_LANG_ASM: 59 return &kit_asm_frontend_vtable; 60 #endif 61 #if KIT_LANG_C_ENABLED 62 case KIT_LANG_C: 63 return &kit_c_frontend_vtable; 64 #endif 65 #if KIT_LANG_TOY_ENABLED 66 case KIT_LANG_TOY: 67 return &kit_toy_frontend_vtable; 68 #endif 69 #if KIT_LANG_WASM_ENABLED 70 case KIT_LANG_WASM: 71 return &kit_wasm_frontend_vtable; 72 #endif 73 default: 74 break; 75 } 76 return NULL; 77 }