kit

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

commit 704b5dbd0eebb2d0abc2455c313c494e1a901f34
parent 805d7438efb536c528cd923294e4aa8fe7b7ef1f
Author: Ryan Sepassi <rsepassi@gmail.com>
Date:   Mon, 25 May 2026 13:08:55 -0700

build: make optimizer sources optional

Diffstat:
MMakefile | 11+++++++++++
Minclude/cfree/config.h | 7++++++-
Minclude/cfree/core.h | 2+-
Mmk/config.mk | 2++
Msrc/arch/aa64/ops.c | 3+++
Msrc/arch/rv64/ops.c | 3+++
Msrc/arch/x64/ops.c | 3+++
Msrc/cg/internal.h | 1-
Msrc/cg/session.c | 15+++++++++++++++
Msrc/core/config_assert.c | 3+++
10 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile @@ -48,17 +48,25 @@ LIB_SRCS_NONARCH = $(shell find src -name '*.c' \ -not -path 'src/obj/elf/*' \ -not -path 'src/obj/macho/*' \ -not -path 'src/obj/coff/*' \ + -not -path 'src/opt/*' \ -not -path 'src/abi/abi_*.c') LIB_SRCS_ARCH_AA64 = $(shell find src/arch/aa64 -name '*.c' 2>/dev/null) LIB_SRCS_ARCH_X64 = $(shell find src/arch/x64 -name '*.c' 2>/dev/null) LIB_SRCS_ARCH_RV64 = $(shell find src/arch/rv64 -name '*.c' 2>/dev/null) LIB_SRCS_ARCH_C_TARGET = $(shell find src/arch/c_target -name '*.c' 2>/dev/null) +ifneq ($(CFREE_OPT_ENABLED),1) +LIB_SRCS_ARCH_AA64 := $(filter-out %/opt_coord.c,$(LIB_SRCS_ARCH_AA64)) +LIB_SRCS_ARCH_X64 := $(filter-out %/opt_coord.c,$(LIB_SRCS_ARCH_X64)) +LIB_SRCS_ARCH_RV64 := $(filter-out %/opt_coord.c,$(LIB_SRCS_ARCH_RV64)) +endif LIB_SRCS_OBJ_ELF = $(shell find src/obj/elf -name '*.c' 2>/dev/null) LIB_SRCS_OBJ_MACHO = $(shell find src/obj/macho -name '*.c' 2>/dev/null) LIB_SRCS_OBJ_COFF = $(shell find src/obj/coff -name '*.c' 2>/dev/null) +LIB_SRCS_OPT = $(shell find src/opt -name '*.c' 2>/dev/null) + LIB_SRC_ABI_AAPCS64 = src/abi/abi_aapcs64.c LIB_SRC_ABI_APPLE_ARM64 = src/abi/abi_apple_arm64.c LIB_SRC_ABI_AAPCS64_WINDOWS = src/abi/abi_aapcs64_windows.c @@ -68,6 +76,9 @@ LIB_SRC_ABI_WIN64_X64 = src/abi/abi_win64_x64.c LIB_SRC_ABI_RV64 = src/abi/abi_rv64.c LIB_SRCS = $(LIB_SRCS_NONARCH) +ifeq ($(CFREE_OPT_ENABLED),1) +LIB_SRCS += $(LIB_SRCS_OPT) +endif ifeq ($(CFREE_ARCH_AA64_ENABLED),1) LIB_SRCS += $(LIB_SRCS_ARCH_AA64) endif diff --git a/include/cfree/config.h b/include/cfree/config.h @@ -3,10 +3,11 @@ /* Build-time component configuration for libcfree and the cfree driver. * - * Three independent axes are gated here: + * Four independent axes are gated here: * - Backend architectures * - Object/image formats * - Language frontends + * - Optional optimizer pipeline * * ABIs are derived from (arch x obj-format) and are pulled in automatically * when both sides are enabled (see src/abi/). @@ -44,4 +45,8 @@ #define CFREE_LANG_TOY_ENABLED 1 #define CFREE_LANG_WASM_ENABLED 1 +/* Optimizer pipeline. -O0/direct codegen is always available; -O1 and above + * require this flag and the matching src/opt sources. */ +#define CFREE_OPT_ENABLED 1 + #endif /* CFREE_CONFIG_H */ diff --git a/include/cfree/core.h b/include/cfree/core.h @@ -193,7 +193,7 @@ typedef struct CfreePathPrefixMap { } CfreePathPrefixMap; typedef struct CfreeCodeOptions { - int opt_level; /* 0 direct, 1 minimal, 2 full */ + int opt_level; /* 0 direct; 1+ require CFREE_OPT_ENABLED */ bool debug_info; /* emit source/debug records when supported */ /* When set, CG emits portable C source instead of machine-code bytes. * The TU is still target-locked: the emitted source uses the configured diff --git a/mk/config.mk b/mk/config.mk @@ -18,3 +18,5 @@ CFREE_LANG_CPP_ENABLED := $(call cfg_flag,CFREE_LANG_CPP_ENABLED) CFREE_LANG_C_ENABLED := $(call cfg_flag,CFREE_LANG_C_ENABLED) CFREE_LANG_TOY_ENABLED := $(call cfg_flag,CFREE_LANG_TOY_ENABLED) CFREE_LANG_WASM_ENABLED := $(call cfg_flag,CFREE_LANG_WASM_ENABLED) + +CFREE_OPT_ENABLED := $(call cfg_flag,CFREE_OPT_ENABLED) diff --git a/src/arch/aa64/ops.c b/src/arch/aa64/ops.c @@ -2,6 +2,7 @@ * intrinsics, asm_block, set_loc, finalize/destroy, vtable constructor. */ #include "arch/aa64/internal.h" +#include "cfree/config.h" #include "core/slice.h" /* ============================================================ @@ -2803,7 +2804,9 @@ CGTarget* aa64_cgtarget_new(Compiler* c, ObjBuilder* o, MCEmitter* m) { /* alloc/label/scope vtable entries */ aa_alloc_vtable_init(t); +#if CFREE_OPT_ENABLED aa_coord_vtable_init(t); +#endif /* Suppress unused warning. */ (void)type_is_signed; diff --git a/src/arch/rv64/ops.c b/src/arch/rv64/ops.c @@ -4,6 +4,7 @@ #include "arch/rv64/asm.h" #include "arch/rv64/regs.h" +#include "cfree/config.h" #include "core/pool.h" #include "core/slice.h" @@ -2451,7 +2452,9 @@ CGTarget* rv64_cgtarget_new(Compiler* c, ObjBuilder* o, MCEmitter* m) { t->finalize = rv_finalize; t->destroy = rv_destroy; +#if CFREE_OPT_ENABLED rv_coord_vtable_init(t); +#endif (void)type_is_signed; compiler_defer(c, cgt_cleanup, t); diff --git a/src/arch/x64/ops.c b/src/arch/x64/ops.c @@ -14,6 +14,7 @@ #include "arch/arch.h" #include "arch/x64/asm.h" +#include "cfree/config.h" #include "arch/x64/internal.h" #include "arch/x64/isa.h" #include "arch/x64/x64.h" @@ -2904,7 +2905,9 @@ CGTarget* x64_cgtarget_new(Compiler* c, ObjBuilder* o, MCEmitter* m) { t->finalize = x_finalize; t->destroy = x_destroy; +#if CFREE_OPT_ENABLED x_coord_vtable_init(t); +#endif compiler_defer(c, cgt_cleanup, t); return t; diff --git a/src/cg/internal.h b/src/cg/internal.h @@ -21,7 +21,6 @@ #include "core/strbuf.h" #include "debug/debug.h" #include "obj/obj.h" -#include "opt/opt.h" typedef struct CGTarget CGTarget; typedef struct MCEmitter MCEmitter; diff --git a/src/cg/session.c b/src/cg/session.c @@ -1,5 +1,11 @@ +#include <cfree/config.h> + #include "cg/internal.h" +#if CFREE_OPT_ENABLED +#include "opt/opt.h" +#endif + static void cg_free_obj_state(CfreeCg* g) { Heap* h; u32 i; @@ -94,14 +100,23 @@ CfreeStatus cfree_cg_begin_obj(CfreeCg* g, CfreeObjBuilder* out, compiler_panic((Compiler*)c, api_no_loc(), "CfreeCg: unsupported opt_level %d", opt_level); } +#if !CFREE_OPT_ENABLED + if (opt_level > 0) { + compiler_panic((Compiler*)c, api_no_loc(), + "CfreeCg: opt_level %d requires CFREE_OPT_ENABLED", + opt_level); + } +#endif backend = cg_backend_for_session((Compiler*)c, opts); if (!backend) return CFREE_UNSUPPORTED; target = backend->make((Compiler*)c, (ObjBuilder*)out, opts); if (!target) return CFREE_UNSUPPORTED; +#if CFREE_OPT_ENABLED if (opt_level > 0) { target = opt_cgtarget_new((Compiler*)c, target, opt_level); if (!target) return CFREE_UNSUPPORTED; } +#endif g->obj = (ObjBuilder*)out; g->target = target; g->mc = target->mc; diff --git a/src/core/config_assert.c b/src/core/config_assert.c @@ -13,3 +13,6 @@ _Static_assert(CFREE_OBJ_ELF_ENABLED + CFREE_OBJ_MACHO_ENABLED + CFREE_OBJ_COFF_ENABLED >= 1, "at least one object/image format must be enabled"); + +_Static_assert(CFREE_OPT_ENABLED == 0 || CFREE_OPT_ENABLED == 1, + "CFREE_OPT_ENABLED must be 0 or 1");