Skip to content

Commit

Permalink
Merge pull request #10 from birth-software/backend-improvement
Browse files Browse the repository at this point in the history
Backend improvement
  • Loading branch information
davidgmbb authored Nov 10, 2023
2 parents f63fcbc + aee9770 commit d025291
Show file tree
Hide file tree
Showing 26 changed files with 8,952 additions and 1,787 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
matrix:
os: [
ubuntu-latest,
windows-latest,
macos-latest
]
runs-on: ${{ matrix.os }}
timeout-minutes: 15
Expand All @@ -39,4 +37,7 @@ jobs:
- name: Zig environment variables
run: zig env
- name: Test
run: zig build test -Dall --summary all
run: |
zig build test -Dall --summary all
zig build run -- test/first/main.nat
nat/first
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
"type": "lldb",
"request": "launch",
"name": "Launch",
"program": "${workspaceFolder}/zig-out/bin/compiler",
"args": [],
"program": "${workspaceFolder}/zig-out/bin/nativity",
"args": [
"test/hello_world/main.nat"
],
"cwd": "${workspaceFolder}",
"preLaunchTask": "zig build"
},
Expand Down
99 changes: 26 additions & 73 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,116 +1,69 @@
const std = @import("std");
var all: bool = false;

fn everythingForTargetAndOptimization(b: *std.Build, target: std.zig.CrossTarget, optimization: std.builtin.OptimizeMode, unit_tests: []const []const u8, test_step: *std.Build.Step) !void {
const name = if (all) try std.mem.concat(b.allocator, u8, &.{ "nativity_", @tagName(optimization) }) else "nativity";
pub fn build(b: *std.Build) !void {
all = b.option(bool, "all", "All") orelse false;
const target = b.standardTargetOptions(.{});
const optimization = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = name,
.name = "nativity",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimization,
.use_llvm = true,
.use_lld = false,
});

b.installArtifact(exe);
b.installDirectory(.{
.source_dir = std.Build.LazyPath.relative("lib"),
.install_dir = .bin,
.install_subdir = "lib",
});

const run_cmd = b.addRunArtifact(exe);

run_cmd.step.dependOn(b.getInstallStep());

if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "run_", @tagName(optimization) }) else "run", "Run the app");
run_step.dependOn(&run_cmd.step);

const debug_command = addDebugCommand(b, exe);
const debug_step = b.step(if (all) try std.mem.concat(b.allocator, u8, &.{ "debug_", @tagName(optimization) }) else "debug", "Debug the app");
debug_step.dependOn(&debug_command.step);

const zig_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimization,
});

const run_zig_tests = b.addRunArtifact(zig_tests);
run_zig_tests.has_side_effects = true;
test_step.dependOn(&run_zig_tests.step);
const run_command = b.addRunArtifact(exe);

for (unit_tests) |unit_test_main_source_file| {
const unit_test = b.addRunArtifact(exe);
unit_test.has_side_effects = true;
unit_test.addArg(unit_test_main_source_file);
test_step.dependOn(&unit_test.step);
}
}

fn addDebugCommand(b: *std.Build, artifact: *std.Build.Step.Compile) *std.Build.Step.Run {
return switch (@import("builtin").os.tag) {
const debug_command = switch (@import("builtin").os.tag) {
.linux => blk: {
const result = b.addSystemCommand(&.{"gf2"});
result.addArtifactArg(artifact);

if (artifact.kind == .@"test") {
result.addArgs(&.{ "-ex", "r" });
}

result.addArg("--args");
result.addArtifactArg(exe);
break :blk result;
},
.windows => blk: {
const result = b.addSystemCommand(&.{"remedybg"});
result.addArg("-g");
result.addArtifactArg(artifact);
result.addArtifactArg(exe);

break :blk result;
},
.macos => blk: {
// not tested
const result = b.addSystemCommand(&.{"gdb"});
result.addArtifactArg(artifact);
const result = b.addSystemCommand(&.{"lldb"});
result.addArg("--");
result.addArtifactArg(exe);
break :blk result;
},
else => @compileError("Operating system not supported"),
else => @compileError("OS not supported"),
};
}

pub fn build(b: *std.Build) !void {
all = b.option(bool, "all", "All") orelse false;

var unit_test_list = std.ArrayList([]const u8).init(b.allocator);
var test_dir = try std.fs.cwd().openIterableDir("test", .{ .access_sub_paths = true });
defer test_dir.close();
const test_command = b.addRunArtifact(zig_tests);

var test_dir_iterator = test_dir.iterate();

while (try test_dir_iterator.next()) |entry| {
switch (entry.kind) {
.directory => {
const dir_name = entry.name;
const main_unit_test_source_file = try std.mem.concat(b.allocator, u8, &.{ "test/", dir_name, "/main.nat" });
try unit_test_list.append(main_unit_test_source_file);
},
.file => {},
else => @panic("Don't put crap on test directory"),
}
if (b.args) |args| {
run_command.addArgs(args);
test_command.addArgs(args);
debug_command.addArgs(args);
}

const target = b.standardTargetOptions(.{});
const unit_tests = unit_test_list.items;
const run_step = b.step("run", "Test the Nativity compiler");
run_step.dependOn(&run_command.step);
const test_step = b.step("test", "Test the Nativity compiler");

if (all) {
inline for (@typeInfo(std.builtin.OptimizeMode).Enum.fields) |enum_field| {
const optimization = @field(std.builtin.OptimizeMode, enum_field.name);
try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step);
}
} else {
const optimization = b.standardOptimizeOption(.{});
_ = try everythingForTargetAndOptimization(b, target, optimization, unit_tests, test_step);
}
test_step.dependOn(&test_command.step);
const debug_step = b.step("debug", "Debug the Nativity compiler");
debug_step.dependOn(&debug_command.step);
}
16 changes: 16 additions & 0 deletions lib/std/builtin.nat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Os = enum{
linux,
macos,
windows,
};

const Cpu = enum{
aarch64,
x86_64,
};

const Abi = enum{
none,
gnu,
msvc,
};
46 changes: 46 additions & 0 deletions lib/std/os.nat
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const current = #import("builtin").os;
const system = switch (current) {
.linux => linux,
.macos => macos,
.windows => windows,
};

const write = fn (file_descriptor: FileDescriptor, bytes_ptr: [@]const u8, bytes_len: usize) ssize {
switch (current) {
.linux => return #syscall(1, file_descriptor, bytes_ptr, bytes_len),
.macos => return macos.write(file_descriptor, bytes_ptr, bytes_len),
.windows => {
var written_bytes: u32 = 0;
if (windows.WriteFile(file_descriptor, bytes_ptr, bytes_len, @written_bytes, false) != 0) {
return written_bytes;
} else {
unreachable;
}
},
}
}

const FileDescriptor = system.FileDescriptor;

const print = fn(bytes_ptr: [@]const u8, bytes_len: usize) void {
const file_descriptor = switch (current) {
.linux, .macos => 2,
.windows => windows.GetStdHandle(windows.STD_OUTPUT_HANDLE),
};

_ = write(file_descriptor, bytes_ptr, bytes_len);
}

const exit = fn(exit_code: s32) noreturn {
switch (current) {
.linux => _ = #syscall(231, exit_code),
.macos => macos.exit(exit_code),
.windows => windows.ExitProcess(exit_code),
}

unreachable;
}

const linux = #import("os/linux.nat");
const macos = #import("os/macos.nat");
const windows = #import("os/windows.nat");
1 change: 1 addition & 0 deletions lib/std/os/linux.nat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const FileDescriptor = s32;
3 changes: 3 additions & 0 deletions lib/std/os/macos.nat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const FileDescriptor = s32;
const write = fn (file_descriptor: FileDescriptor, bytes_ptr: [@]const u8, bytes_len: usize) ssize extern;
const exit = fn (exit_code: u32) noreturn extern;
3 changes: 3 additions & 0 deletions lib/std/os/windows.nat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const HANDLE = u64;
const FileDescriptor = HANDLE;
const GetStdHandle = fn(handle_descriptor: u32) HANDLE extern;
6 changes: 3 additions & 3 deletions lib/std/start.nat
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const std = #import("std");
comptime {
_ = _start;
}

const _start = fn () noreturn {
const result = #import("main").main();
_ = #syscall(231, result);
unreachable;
};
std.os.exit(result);
}
3 changes: 3 additions & 0 deletions lib/std/std.nat
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ comptime {
_ = start;
}

const builtin = #import("builtin.nat");
const os = #import("os.nat");
const print = os.print;
const start = #import("start.nat");
Loading

0 comments on commit d025291

Please sign in to comment.