kit

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

target.c (1946B)


      1 /* C-source backend registration.
      2  *
      3  * The public backend path records CG into semantic IR, then emits C from that
      4  * IR. The legacy CgTarget adapter lives in cg_emit.c only so it can be deleted
      5  * whole once all callers use the IR path directly. */
      6 
      7 #include <string.h>
      8 
      9 #include "arch/c_target/c_emit.h"
     10 #include "arch/c_target/ir_emit.h"
     11 #include "cg/ir_recorder.h"
     12 #include "core/core.h"
     13 
     14 static void c_ir_finalize(void* user, const CgIrModule* module) {
     15   CTarget* t = (CTarget*)user;
     16   c_emit_ir_module(t, module);
     17   c_emit_finalize(t);
     18 }
     19 
     20 static void c_ir_destroy(void* user) {
     21   CTarget* t = (CTarget*)user;
     22   if (t) c_emit_destroy(t);
     23 }
     24 
     25 static int c_ir_local_static_data_begin(void* user,
     26                                         const CGLocalStaticDataDesc* desc) {
     27   return c_emit_can_local_static_data((CTarget*)user, desc);
     28 }
     29 
     30 static const char* c_ir_tail_call_unrealizable_reason(void* user,
     31                                                       const CGFuncDesc* caller,
     32                                                       const CGCallDesc* call) {
     33   return c_emit_tail_call_unrealizable_reason_for((CTarget*)user, caller, call);
     34 }
     35 
     36 static CgTarget* c_target_backend_make(Compiler* c, ObjBuilder* o,
     37                                        const KitCodeOptions* opts) {
     38   if (!opts || !opts->c_source_writer) {
     39     compiler_panic(c, (SrcLoc){0, 0, 0},
     40                    "c_target backend: emit_c_source requires c_source_writer");
     41   }
     42 
     43   CTarget* ctarget = c_emit_target_new(c, o, opts->c_source_writer);
     44   CgIrRecorderConfig cfg;
     45   memset(&cfg, 0, sizeof cfg);
     46   cfg.finalize = c_ir_finalize;
     47   cfg.destroy = c_ir_destroy;
     48   cfg.local_static_data_begin = c_ir_local_static_data_begin;
     49   cfg.tail_call_unrealizable_reason = c_ir_tail_call_unrealizable_reason;
     50   cfg.user = ctarget;
     51   return cg_ir_recorder_new(c, o, &cfg);
     52 }
     53 
     54 const CGBackend cg_backend_c_target = {
     55     .name = "c_target",
     56     .make = c_target_backend_make,
     57 };