C backend: ahead-of-time compilation to a native binary
- new cat/backend/c.{h,c}: lower a compiled CAT_BYTECODE_Function to a
self-contained C translation unit that rebuilds the bytecode and runs
it on the catscript runtime. The tree is emitted in pre-order with
forward declarations; constants (int/float/text/bool) are recreated by
the matching constructors; jumps keep their absolute targets.
- Makefile: also build libcatscript.a (all objects but main.o) so an AOT
program links the runtime without clashing on `main`.
- main.c: `--emit-c <file>` prints the generated C; `--build <file> <out>`
emits it, invokes the system `cc`, and produces the executable.
- tests/aot.cat + suite: the native binary's stdout must equal the
interpreter's (arithmetic, recursion, objects, interfaces, `is`,
concat, comparisons). Suite 151 -> 152.
The backend is faithful by construction: it lowers the very bytecode the
interpreter runs, so native output matches the interpreter exactly
(including current language quirks, e.g. upvalues and `try`/`catch`).
This commit is contained in:
1 parent
70728093ef
commit
33a1152958
6 files changed
+473
-2
No files matched your search
@@ -21,6 +21,7 @@ LDLIBS ?= -lm -lffi -ldl
|
||||
|
||||
BUILD := build/catscript
|
||||
BIN := $(BUILD)/catscript
|
||||
LIB := $(BUILD)/libcatscript.a
|
||||
|
||||
SRCS := \
|
||||
src/main.c \
|
||||
@@ -31,6 +32,7 @@ SRCS := \
|
||||
src/cat/compiler.c \
|
||||
src/cat/vm.c \
|
||||
src/cat/module.c \
|
||||
src/cat/backend/c.c \
|
||||
src/cat/types/empty.c \
|
||||
src/cat/types/type.c \
|
||||
src/cat/types/bool.c \
|
||||
@@ -57,6 +59,11 @@ SRCS := \
|
||||
|
||||
OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS))
|
||||
|
||||
# The runtime archive is every object EXCEPT the CLI entry point, so an
|
||||
# ahead-of-time compiled program can link the runtime without clashing
|
||||
# on `main`.
|
||||
LIBOBJS := $(filter-out $(BUILD)/main.o,$(OBJS))
|
||||
|
||||
.PHONY: help all test clean examples
|
||||
|
||||
# Default goal: print the help.
|
||||
@@ -67,12 +74,16 @@ help:
|
||||
@echo " make clean - remove build artifacts"
|
||||
@echo " make help - show this help (default goal)"
|
||||
|
||||
all: $(BIN)
|
||||
all: $(BIN) $(LIB)
|
||||
|
||||
$(BIN): $(OBJS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
|
||||
|
||||
$(LIB): $(LIBOBJS)
|
||||
@mkdir -p $(dir $@)
|
||||
$(AR) rcs $@ $(LIBOBJS)
|
||||
|
||||
$(BUILD)/%.o: src/%.c
|
||||
@mkdir -p $(dir $@)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
|
||||
|
||||
Reference in new issue
Block a user