kit

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

commit 9f443e5fdf3521ad8fa83ca1f149b640d4580d3a
parent 201c8f0da021884364993e891a9bb09ec10c05ad
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Thu, 14 May 2026 10:50:12 -0700

Wire opt level into CG target creation

Diffstat:
Minclude/cfree/cg.h | 3++-
Mlang/toy/toy.c | 3+--
Msrc/api/cg.c | 20+++++++++++++++++++-
Msrc/emu/emu.c | 5++++-
Msrc/opt/opt.c | 9+++++++++
5 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/include/cfree/cg.h b/include/cfree/cg.h @@ -358,7 +358,8 @@ CfreeSym cfree_cg_c_linkage_name(CfreeCompiler*, CfreeSym source_name); * Lifecycle and Source Locations * ============================================================ */ -CfreeCg* cfree_cg_new(CfreeCompiler*, CfreeObjBuilder* out); +CfreeCg* cfree_cg_new(CfreeCompiler*, CfreeObjBuilder* out, + const CfreeCompileOptions*); void cfree_cg_free(CfreeCg*); /* Sticky source location. Function, scope, local, param, instruction, and diff --git a/lang/toy/toy.c b/lang/toy/toy.c @@ -2824,11 +2824,10 @@ int cfree_toy_compile(CfreeCompiler* c, const CfreeCompileOptions* opts, CfreeCg* cg; const uint8_t* source; - (void)opts; if (!c || !input || !out) return 1; source = input->data ? input->data : (const uint8_t*)""; - cg = cfree_cg_new(c, out); + cg = cfree_cg_new(c, out, opts); if (!cg) return 1; toy_parser_init(&p, c, cg, source, input->len); diff --git a/src/api/cg.c b/src/api/cg.c @@ -13,6 +13,7 @@ #include "core/pool.h" #include "core/segvec.h" #include "obj/obj.h" +#include "opt/opt.h" typedef enum CgApiTypeKind { CG_API_TYPE_PTR, @@ -1656,12 +1657,26 @@ static RelocKind api_data_reloc_kind(int pcrel, uint32_t width) { * Public API: CfreeCg lifecycle * ============================================================ */ -CfreeCg* cfree_cg_new(CfreeCompiler* c, CfreeObjBuilder* out) { +static SrcLoc api_no_loc(void) { + SrcLoc loc; + loc.file_id = 0; + loc.line = 0; + loc.col = 0; + return loc; +} + +CfreeCg* cfree_cg_new(CfreeCompiler* c, CfreeObjBuilder* out, + const CfreeCompileOptions* opts) { Heap* h; CfreeCg* g; MCEmitter* mc; CGTarget* target; + int opt_level = opts ? opts->opt_level : 0; if (!c || !out) return NULL; + if (opt_level < 0 || opt_level > 2) { + compiler_panic((Compiler*)c, api_no_loc(), + "CfreeCg: unsupported opt_level %d", opt_level); + } h = (Heap*)c->env->heap; mc = mc_new((Compiler*)c, (ObjBuilder*)out); if (!mc) return NULL; @@ -1670,6 +1685,9 @@ CfreeCg* cfree_cg_new(CfreeCompiler* c, CfreeObjBuilder* out) { mc_free(mc); return NULL; } + if (opt_level > 0) { + target = opt_cgtarget_new((Compiler*)c, target, opt_level); + } g = (CfreeCg*)h->alloc(h, sizeof(CfreeCg), _Alignof(CfreeCg)); if (!g) { cgtarget_free(target); diff --git a/src/emu/emu.c b/src/emu/emu.c @@ -139,6 +139,7 @@ static void* translate_block(CfreeEmu* e, u64 guest_pc) { u32 ninsts; ObjBuilder* ob; CfreeCg* cg; + CfreeCompileOptions copts; Sym block_name; ObjSymId block_sym; EmuLiftCtx ctx; @@ -171,7 +172,9 @@ static void* translate_block(CfreeEmu* e, u64 guest_pc) { /* Per-block ObjBuilder + public CG pipeline. The block lands as a single * host function once per-ISA lifters start emitting real code. */ ob = obj_new(e->c); - cg = cfree_cg_new(e->c, ob); + memset(&copts, 0, sizeof(copts)); + copts.opt_level = e->opt_level; + cg = cfree_cg_new(e->c, ob, &copts); if (!cg) compiler_panic(e->c, no_loc(), "emu: cfree_cg_new failed"); block_name = emu_block_sym_name(e->c, guest_pc); diff --git a/src/opt/opt.c b/src/opt/opt.c @@ -193,6 +193,14 @@ static void w_reserve_hard_regs(CGTarget* t, RegClass cls, wr->reserve_hard_regs(wr, cls, regs, n); } +static int w_resolve_reg_name(CGTarget* t, Sym name, Reg* out, + RegClass* cls_out) { + CGTarget* wr = impl_of(t)->target; + if (wr->resolve_reg_name) + return wr->resolve_reg_name(wr, name, out, cls_out); + return 1; +} + /* ---- labels and control flow ---- */ static Label w_label_new(CGTarget* t) { @@ -1376,6 +1384,7 @@ CGTarget* opt_cgtarget_new(Compiler* c, CGTarget* target, int level) { t->intrinsic = w_intrinsic; t->asm_block = w_asm_block; + t->resolve_reg_name = w_resolve_reg_name; t->set_loc = w_set_loc; t->finalize = w_finalize;