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
@@ -0,0 +1,32 @@
|
||||
-- tests/aot.cat -- the C backend (ahead-of-time compilation).
|
||||
--
|
||||
-- Built with `catscript --build tests/aot.cat /tmp/aot_native`, this must
|
||||
-- produce byte-for-byte the same stdout as the interpreter. It exercises
|
||||
-- arithmetic, calls, recursion, objects, interfaces and structural `is`.
|
||||
|
||||
function add(a, b) return a + b end
|
||||
print("add:", add(2, 3))
|
||||
|
||||
function fact(n)
|
||||
if n <= 1 then return 1 end
|
||||
return n * fact(n - 1)
|
||||
end
|
||||
print("fact:", fact(5))
|
||||
|
||||
local p = { x = 1, y = 2 }
|
||||
p.x = p.x + 10
|
||||
print("point:", p.x, p.y)
|
||||
|
||||
interface Shape
|
||||
function area(self) end
|
||||
end
|
||||
|
||||
class Square implements Shape
|
||||
function init(self, side) self.side = side end
|
||||
function area(self) return self.side * self.side end
|
||||
end
|
||||
|
||||
local s = Square(4)
|
||||
print("area:", s.area(), s is Shape)
|
||||
|
||||
print("text:", "ca" .. "tscript", 3 < 4, 5 == 5, not (2 == 3))
|
||||
@@ -263,6 +263,20 @@ check "trait-robot" "robot is Greeter: true" "`echo "$out" | sed -n 6p`"
|
||||
check "trait-sized" "sized: true" "`echo "$out" | sed -n 7p`"
|
||||
check "trait-sized-fn" "implements Sized: true" "`echo "$out" | sed -n 8p`"
|
||||
|
||||
out=`$BIN tests/aot.cat`
|
||||
exp=`$BIN tests/aot.cat`
|
||||
$BIN --build tests/aot.cat build/tests/aot_native >/dev/null 2>&1
|
||||
if [ -x build/tests/aot_native ]; then
|
||||
got=`build/tests/aot_native`
|
||||
if [ "$exp" = "$got" ]; then
|
||||
check "aot-native" "interpreter == native" "interpreter == native"
|
||||
else
|
||||
check "aot-native" "interpreter == native" "diverged: $got"
|
||||
fi
|
||||
else
|
||||
check "aot-native" "native binary built" "missing"
|
||||
fi
|
||||
|
||||
out=`$BIN tests/comptime.cat`
|
||||
check "comptime-expr" "ct-expr: 42" "`echo "$out" | sed -n 1p`"
|
||||
check "comptime-text" "ct-text: hi there" "`echo "$out" | sed -n 2p`"
|
||||
|
||||
Reference in new issue
Block a user