master
1const std = @import("std");
2const Allocator = mem.Allocator;
3const mem = std.mem;
4const process = std.process;
5const aro = @import("aro");
6const Compilation = aro.Compilation;
7const Diagnostics = aro.Diagnostics;
8const Driver = aro.Driver;
9const Toolchain = aro.Toolchain;
10const assembly_backend = @import("assembly_backend");
11
12var debug_allocator: std.heap.DebugAllocator(.{
13 .stack_trace_frames = 0,
14 // A unique value so that when a default-constructed
15 // GeneralPurposeAllocator is incorrectly passed to testing allocator, or
16 // vice versa, panic occurs.
17 .canary = @truncate(0xc647026dc6875134),
18}) = .{};
19
20pub fn main() u8 {
21 const gpa = if (@import("builtin").link_libc)
22 std.heap.raw_c_allocator
23 else
24 debug_allocator.allocator();
25 defer if (!@import("builtin").link_libc) {
26 _ = debug_allocator.deinit();
27 };
28
29 var arena_instance = std.heap.ArenaAllocator.init(gpa);
30 defer arena_instance.deinit();
31 const arena = arena_instance.allocator();
32
33 var threaded: std.Io.Threaded = .init(gpa);
34 defer threaded.deinit();
35 const io = threaded.io();
36
37 const fast_exit = @import("builtin").mode != .Debug;
38
39 const args = process.argsAlloc(arena) catch {
40 std.debug.print("out of memory\n", .{});
41 if (fast_exit) process.exit(1);
42 return 1;
43 };
44
45 const aro_name = std.fs.selfExePathAlloc(gpa) catch {
46 std.debug.print("unable to find Aro executable path\n", .{});
47 if (fast_exit) process.exit(1);
48 return 1;
49 };
50 defer gpa.free(aro_name);
51
52 var stderr_buf: [1024]u8 = undefined;
53 var stderr = std.fs.File.stderr().writer(&stderr_buf);
54 var diagnostics: Diagnostics = .{
55 .output = .{ .to_writer = .{
56 .color = .detect(stderr.file),
57 .writer = &stderr.interface,
58 } },
59 };
60
61 var comp = Compilation.initDefault(gpa, arena, io, &diagnostics, std.fs.cwd()) catch |er| switch (er) {
62 error.OutOfMemory => {
63 std.debug.print("out of memory\n", .{});
64 if (fast_exit) process.exit(1);
65 return 1;
66 },
67 };
68 defer comp.deinit();
69
70 var driver: Driver = .{ .comp = &comp, .aro_name = aro_name, .diagnostics = &diagnostics };
71 defer driver.deinit();
72
73 var toolchain: Toolchain = .{ .driver = &driver };
74 defer toolchain.deinit();
75
76 driver.main(&toolchain, args, fast_exit, assembly_backend.genAsm) catch |er| switch (er) {
77 error.OutOfMemory => {
78 std.debug.print("out of memory\n", .{});
79 if (fast_exit) process.exit(1);
80 return 1;
81 },
82 error.FatalError => {
83 driver.printDiagnosticsStats();
84 if (fast_exit) process.exit(1);
85 return 1;
86 },
87 };
88 if (fast_exit) process.exit(@intFromBool(comp.diagnostics.errors != 0));
89 return @intFromBool(diagnostics.errors != 0);
90}