summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--Makefile63
-rw-r--r--build.zig61
-rw-r--r--main.c1
4 files changed, 73 insertions, 58 deletions
diff --git a/.gitignore b/.gitignore
index afd08d7..6d548b5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,3 @@
-*.o
+zig-cache/
+zig-out/
 qbe
-config.h
-.comfile
-*.out
diff --git a/Makefile b/Makefile
index f5e8a76..71d5065 100644
--- a/Makefile
+++ b/Makefile
@@ -4,56 +4,16 @@
 PREFIX = /usr/local
 BINDIR = $(PREFIX)/bin
 
-COMMOBJ  = main.o util.o parse.o abi.o cfg.o mem.o ssa.o alias.o load.o \
-           copy.o fold.o simpl.o live.o spill.o rega.o emit.o
-AMD64OBJ = amd64/targ.o amd64/sysv.o amd64/isel.o amd64/emit.o
-ARM64OBJ = arm64/targ.o arm64/abi.o arm64/isel.o arm64/emit.o
-RV64OBJ  = rv64/targ.o rv64/abi.o rv64/isel.o rv64/emit.o
-OBJ      = $(COMMOBJ) $(AMD64OBJ) $(ARM64OBJ) $(RV64OBJ)
+COMMSRC  = main.c util.c parse.c abi.c cfg.c mem.c ssa.c alias.c load.c \
+           copy.c fold.c simpl.c live.c spill.c rega.c emit.c
+AMD64SRC = amd64/targ.c amd64/sysv.c amd64/isel.c amd64/emit.c
+ARM64SRC = arm64/targ.c arm64/abi.c arm64/isel.c arm64/emit.c
+RV64SRC  = rv64/targ.c rv64/abi.c rv64/isel.c rv64/emit.c
+SRCALL   = $(COMMSRC) $(AMD64SRC) $(ARM64SRC) $(RV64SRC)
 
-SRCALL   = $(OBJ:.o=.c)
-
-CC       = cc
-CFLAGS   = -std=c99 -g -Wall -Wextra -Wpedantic
-
-qbe: $(OBJ)
-	$(CC) $(LDFLAGS) $(OBJ) -o $@
-
-.c.o:
-	$(CC) $(CFLAGS) -c $< -o $@
-
-$(OBJ): all.h ops.h
-$(AMD64OBJ): amd64/all.h
-$(ARM64OBJ): arm64/all.h
-$(RV64OBJ): rv64/all.h
-main.o: config.h
-
-config.h:
-	@case `uname` in                               \
-	*Darwin*)                                      \
-		case `uname -m` in                     \
-		*arm64*)                               \
-			echo "#define Deftgt T_arm64_apple";\
-			;;                             \
-		*)                                     \
-			echo "#define Deftgt T_amd64_apple";\
-			;;                             \
-		esac                                   \
-		;;                                     \
-	*)                                             \
-		case `uname -m` in                     \
-		*aarch64*|*arm64*)                     \
-			echo "#define Deftgt T_arm64"; \
-			;;                             \
-		*riscv64*)                             \
-			echo "#define Deftgt T_rv64";  \
-			;;                             \
-		*)                                     \
-			echo "#define Deftgt T_amd64_sysv";\
-			;;                             \
-		esac                                   \
-		;;                                     \
-	esac > $@
+qbe: $(SRCALL) all.h ops.h amd64/all.h arm64/all.h rv64/all.h
+	zig build
+	ln -fs zig-out/bin/qbe qbe
 
 install: qbe
 	mkdir -p "$(DESTDIR)$(BINDIR)"
@@ -63,10 +23,7 @@ uninstall:
 	rm -f "$(DESTDIR)$(BINDIR)/qbe"
 
 clean:
-	rm -f *.o */*.o qbe
-
-clean-gen: clean
-	rm -f config.h
+	rm -fr qbe zig-cache zig-out
 
 check: qbe
 	tools/test.sh all
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..14373c9
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,61 @@
+//! Build recipe
+const builtin = @import("builtin");
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const target = b.standardTargetOptions(.{});
+    const arch = target.cpu_arch orelse builtin.target.cpu.arch;
+    const optimize = b.standardOptimizeOption(.{});
+
+    const bin = b.addExecutable(.{
+        .name = "roux",
+        .target = target,
+        .optimize = optimize,
+        .link_libc = true,
+    });
+    const cflags = .{
+        "-std=c99", "-g", "-Wall", "-Wextra", "-Wpedantic",
+        switch (target.os_tag orelse builtin.target.os.tag) {
+            .macos => switch (arch) {
+                .aarch64 => "-DDeftgt=T_arm64_apple",
+                .x86_64 => "-DDeftgt=T_amd64_apple",
+                else => unreachable,
+            },
+            else => switch (arch) {
+                .aarch64 => "-DDeftgt=T_arm64",
+                .riscv64 => "-DDeftgt=T_rv64",
+                .x86_64 => "-DDeftgt=T_amd64_sysv",
+                else => unreachable,
+            },
+        },
+        // https://lists.sr.ht/~mpu/qbe/<CYB4FWK7MACC.2IF4DEL4C9BF1@loang.net>
+        "-fno-sanitize=undefined",
+    };
+    bin.addIncludePath(.{ .path = "." });
+    bin.addCSourceFiles(&.{
+        "main.c", "util.c", "parse.c", "abi.c", "cfg.c",
+        "mem.c", "ssa.c", "alias.c", "load.c", "copy.c", "fold.c",
+        "simpl.c", "live.c", "spill.c", "rega.c", "emit.c",
+    }, &cflags);
+    bin.addCSourceFiles(&.{
+        "amd64/targ.c", "amd64/sysv.c", "amd64/isel.c", "amd64/emit.c",
+        "arm64/targ.c", "arm64/abi.c", "arm64/isel.c", "arm64/emit.c",
+        "rv64/targ.c", "rv64/abi.c", "rv64/isel.c", "rv64/emit.c",
+    }, &cflags);
+    b.installArtifact(bin);
+    const run_cmd = b.addRunArtifact(bin);
+    run_cmd.step.dependOn(b.getInstallStep());
+    if (b.args) |args|
+        run_cmd.addArgs(args);
+    b.step("run", "Run the app").dependOn(&run_cmd.step);
+
+    const unit_tests = b.addTest(.{
+        .root_source_file = .{ .path = "src/main.zig" },
+        .target = target,
+        .optimize = optimize,
+    });
+
+    const run_unit_tests = b.addRunArtifact(unit_tests);
+    const test_step = b.step("test", "Run unit tests");
+    test_step.dependOn(&run_unit_tests.step);
+}
diff --git a/main.c b/main.c
index 5ecb4d0..fa60d3b 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,4 @@
 #include "all.h"
-#include "config.h"
 #include <ctype.h>
 #include <getopt.h>