Skip to content

Sync Aro sources #19199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ lib/libcxx/** linguist-vendored
lib/libcxxabi/** linguist-vendored
lib/libunwind/** linguist-vendored
lib/tsan/** linguist-vendored
deps/** linguist-vendored
lib/compiler/aro/** linguist-vendored
3 changes: 1 addition & 2 deletions lib/compiler/aro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ int main(void) {
printf("Hello, world!\n");
return 0;
}
$ zig build run -- hello.c -o hello
$ zig build && ./zig-out/bin/arocc hello.c -o hello
$ ./hello
Hello, world!
$
```
22 changes: 6 additions & 16 deletions lib/compiler/aro/aro/Attribute/names.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Autogenerated by GenerateDef from deps/aro/aro/Attribute/names.def, do not edit
//! Autogenerated by GenerateDef from src/aro/Attribute/names.def, do not edit
// zig fmt: off

const std = @import("std");
Expand Down Expand Up @@ -142,34 +142,24 @@ pub fn nameFromUniqueIndex(index: u16, buf: []u8) []u8 {
return fbs.getWritten();
}

/// We're 1 bit shy of being able to fit this in a u32:
/// - char only contains 0-9, a-z, A-Z, and _, so it could use a enum(u6) with a way to convert <-> u8
/// (note: this would have a performance cost that may make the u32 not worth it)
/// - number has a max value of > 2047 and < 4095 (the first _ node has the largest number),
/// so it could fit into a u12
/// - child_index currently has a max of > 4095 and < 8191, so it could fit into a u13
///
/// with the end_of_word/end_of_list 2 bools, that makes 33 bits total
const Node = packed struct(u64) {
const Node = packed struct(u32) {
char: u8,
/// Nodes are numbered with "an integer which gives the number of words that
/// would be accepted by the automaton starting from that state." This numbering
/// allows calculating "a one-to-one correspondence between the integers 1 to L
/// (L is the number of words accepted by the automaton) and the words themselves."
///
/// Essentially, this allows us to have a minimal perfect hashing scheme such that
/// it's possible to store & lookup the properties of each builtin using a separate array.
number: u16,
/// If true, this node is the end of a valid builtin.
/// it's possible to store & lookup the properties of each name using a separate array.
number: u8,
/// If true, this node is the end of a valid name.
/// Note: This does not necessarily mean that this node does not have child nodes.
end_of_word: bool,
/// If true, this node is the end of a sibling list.
/// If false, then (index + 1) will contain the next sibling.
end_of_list: bool,
/// Padding bits to get to u64, unsure if there's some way to use these to improve something.
_extra: u22 = 0,
/// Index of the first child of this node.
child_index: u16,
child_index: u14,
};

const dafsa = [_]Node{
Expand Down
5 changes: 1 addition & 4 deletions lib/compiler/aro/aro/Builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ fn createType(desc: TypeDescription, it: *TypeDescription.TypeIterator, comp: *c
}
},
.h => builder.combine(undefined, .fp16, 0) catch unreachable,
.x => {
// Todo: _Float16
return .{ .specifier = .invalid };
},
.x => builder.combine(undefined, .float16, 0) catch unreachable,
.y => {
// Todo: __bf16
return .{ .specifier = .invalid };
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/Builtins/Builtin.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Autogenerated by GenerateDef from deps/aro/aro/Builtins/Builtin.def, do not edit
//! Autogenerated by GenerateDef from src/aro/Builtins/Builtin.def, do not edit
// zig fmt: off

const std = @import("std");
Expand Down
59 changes: 39 additions & 20 deletions lib/compiler/aro/aro/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ pub const SystemDefinesMode = enum {
fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
const ptr_width = comp.target.ptrBitWidth();

if (comp.langopts.gnuc_version > 0) {
try w.print("#define __GNUC__ {d}\n", .{comp.langopts.gnuc_version / 10_000});
try w.print("#define __GNUC_MINOR__ {d}\n", .{comp.langopts.gnuc_version / 100 % 100});
try w.print("#define __GNUC_PATCHLEVEL__ {d}\n", .{comp.langopts.gnuc_version % 100});
}

// os macros
switch (comp.target.os.tag) {
.linux => try w.writeAll(
Expand Down Expand Up @@ -419,6 +425,25 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
\\
);

// TODO: Set these to target-specific constants depending on backend capabilities
// For now they are just set to the "may be lock-free" value
try w.writeAll(
\\#define __ATOMIC_BOOL_LOCK_FREE 1
\\#define __ATOMIC_CHAR_LOCK_FREE 1
\\#define __ATOMIC_CHAR16_T_LOCK_FREE 1
\\#define __ATOMIC_CHAR32_T_LOCK_FREE 1
\\#define __ATOMIC_WCHAR_T_LOCK_FREE 1
\\#define __ATOMIC_SHORT_LOCK_FREE 1
\\#define __ATOMIC_INT_LOCK_FREE 1
\\#define __ATOMIC_LONG_LOCK_FREE 1
\\#define __ATOMIC_LLONG_LOCK_FREE 1
\\#define __ATOMIC_POINTER_LOCK_FREE 1
\\
);
if (comp.langopts.hasChar8_T()) {
try w.writeAll("#define __ATOMIC_CHAR8_T_LOCK_FREE 1\n");
}

// types
if (comp.getCharSignedness() == .unsigned) try w.writeAll("#define __CHAR_UNSIGNED__ 1\n");
try w.writeAll("#define __CHAR_BIT__ 8\n");
Expand All @@ -438,6 +463,7 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
try comp.generateIntMaxAndWidth(w, "PTRDIFF", comp.types.ptrdiff);
try comp.generateIntMaxAndWidth(w, "INTPTR", comp.types.intptr);
try comp.generateIntMaxAndWidth(w, "UINTPTR", comp.types.intptr.makeIntegerUnsigned());
try comp.generateIntMaxAndWidth(w, "SIG_ATOMIC", target_util.sigAtomicType(comp.target));

// int widths
try w.print("#define __BITINT_MAXWIDTH__ {d}\n", .{bit_int_max_bits});
Expand Down Expand Up @@ -474,6 +500,8 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
try generateTypeMacro(w, mapper, "__PTRDIFF_TYPE__", comp.types.ptrdiff, comp.langopts);
try generateTypeMacro(w, mapper, "__SIZE_TYPE__", comp.types.size, comp.langopts);
try generateTypeMacro(w, mapper, "__WCHAR_TYPE__", comp.types.wchar, comp.langopts);
try generateTypeMacro(w, mapper, "__CHAR16_TYPE__", comp.types.uint_least16_t, comp.langopts);
try generateTypeMacro(w, mapper, "__CHAR32_TYPE__", comp.types.uint_least32_t, comp.langopts);

try comp.generateExactWidthTypes(w, mapper);
try comp.generateFastAndLeastWidthTypes(w, mapper);
Expand Down Expand Up @@ -518,7 +546,6 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi

// standard macros
try buf.appendSlice(
\\#define __STDC_NO_ATOMICS__ 1
\\#define __STDC_NO_COMPLEX__ 1
\\#define __STDC_NO_THREADS__ 1
\\#define __STDC_NO_VLA__ 1
Expand Down Expand Up @@ -1030,9 +1057,8 @@ pub fn getCharSignedness(comp: *const Compilation) std.builtin.Signedness {
return comp.langopts.char_signedness_override orelse comp.target.charSignedness();
}

pub fn defineSystemIncludes(comp: *Compilation, aro_dir: []const u8) !void {
var stack_fallback = std.heap.stackFallback(path_buf_stack_limit, comp.gpa);
const allocator = stack_fallback.get();
/// Add built-in aro headers directory to system include paths
pub fn addBuiltinIncludeDir(comp: *Compilation, aro_dir: []const u8) !void {
var search_path = aro_dir;
while (std.fs.path.dirname(search_path)) |dirname| : (search_path = dirname) {
var base_dir = std.fs.cwd().openDir(dirname, .{}) catch continue;
Expand All @@ -1044,23 +1070,12 @@ pub fn defineSystemIncludes(comp: *Compilation, aro_dir: []const u8) !void {
try comp.system_include_dirs.append(comp.gpa, path);
break;
} else return error.AroIncludeNotFound;
}

if (comp.target.os.tag == .linux) {
const triple_str = try comp.target.linuxTriple(allocator);
defer allocator.free(triple_str);

const multiarch_path = try std.fs.path.join(allocator, &.{ "/usr/include", triple_str });
defer allocator.free(multiarch_path);

if (!std.meta.isError(std.fs.accessAbsolute(multiarch_path, .{}))) {
const duped = try comp.gpa.dupe(u8, multiarch_path);
errdefer comp.gpa.free(duped);
try comp.system_include_dirs.append(comp.gpa, duped);
}
}
const usr_include = try comp.gpa.dupe(u8, "/usr/include");
errdefer comp.gpa.free(usr_include);
try comp.system_include_dirs.append(comp.gpa, usr_include);
pub fn addSystemIncludeDir(comp: *Compilation, path: []const u8) !void {
const duped = try comp.gpa.dupe(u8, path);
errdefer comp.gpa.free(duped);
try comp.system_include_dirs.append(comp.gpa, duped);
}

pub fn getSource(comp: *const Compilation, id: Source.Id) Source {
Expand Down Expand Up @@ -1331,6 +1346,10 @@ pub fn hasInclude(
/// __has_include vs __has_include_next
which: WhichInclude,
) !bool {
if (mem.indexOfScalar(u8, filename, 0) != null) {
return false;
}

const cwd = std.fs.cwd();
if (std.fs.path.isAbsolute(filename)) {
if (which == .next) return false;
Expand Down
4 changes: 3 additions & 1 deletion lib/compiler/aro/aro/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ pub const Options = struct {
@"unsupported-embed-param": Kind = .default,
@"unused-result": Kind = .default,
normalized: Kind = .default,
@"shift-count-negative": Kind = .default,
@"shift-count-overflow": Kind = .default,
};

const Diagnostics = @This();
Expand Down Expand Up @@ -291,7 +293,7 @@ pub fn addExtra(
.kind = .note,
.extra = .{ .unsigned = expansion_locs.len - d.macro_backtrace_limit },
});
i = half - 1;
i = half -| 1;
while (i > 0) {
i -= 1;
d.list.appendAssumeCapacity(.{
Expand Down
12 changes: 11 additions & 1 deletion lib/compiler/aro/aro/Diagnostics/messages.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Autogenerated by GenerateDef from deps/aro/aro/Diagnostics/messages.def, do not edit
//! Autogenerated by GenerateDef from src/aro/Diagnostics/messages.def, do not edit
// zig fmt: off

const std = @import("std");
Expand Down Expand Up @@ -504,6 +504,11 @@ pub const Tag = enum {
c23_auto_single_declarator,
c32_auto_requires_initializer,
c23_auto_scalar_init,
negative_shift_count,
too_big_shift_count,
complex_conj,
overflow_builtin_requires_int,
overflow_result_requires_ptr,

pub fn property(tag: Tag) Properties {
return named_data[@intFromEnum(tag)];
Expand Down Expand Up @@ -1005,6 +1010,11 @@ pub const Tag = enum {
.{ .msg = "'auto' can only be used with a single declarator", .kind = .@"error" },
.{ .msg = "'auto' requires an initializer", .kind = .@"error" },
.{ .msg = "'auto' requires a scalar initializer", .kind = .@"error" },
.{ .msg = "shift count is negative", .opt = W("shift-count-negative"), .kind = .warning, .all = true },
.{ .msg = "shift count >= width of type", .opt = W("shift-count-overflow"), .kind = .warning, .all = true },
.{ .msg = "ISO C does not support '~' for complex conjugation of '{s}'", .opt = W("pedantic"), .extra = .str, .kind = .off },
.{ .msg = "operand argument to overflow builtin must be an integer ('{s}' invalid)", .extra = .str, .kind = .@"error" },
.{ .msg = "result argument to overflow builtin must be a pointer to a non-const integer ('{s}' invalid)", .extra = .str, .kind = .@"error" },
};
};
};
Expand Down
30 changes: 27 additions & 3 deletions lib/compiler/aro/aro/Driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Preprocessor = @import("Preprocessor.zig");
const Source = @import("Source.zig");
const Toolchain = @import("Toolchain.zig");
const target_util = @import("target.zig");
const GCCVersion = @import("Driver/GCCVersion.zig");

pub const Linker = enum {
ld,
Expand Down Expand Up @@ -43,6 +44,9 @@ verbose_pp: bool = false,
verbose_ir: bool = false,
verbose_linker_args: bool = false,
color: ?bool = null,
nobuiltininc: bool = false,
nostdinc: bool = false,
nostdlibinc: bool = false,

/// Full path to the aro executable
aro_name: []const u8 = "",
Expand Down Expand Up @@ -95,6 +99,7 @@ pub const usage =
\\ -fcolor-diagnostics Enable colors in diagnostics
\\ -fno-color-diagnostics Disable colors in diagnostics
\\ -fdeclspec Enable support for __declspec attributes
\\ -fgnuc-version=<value> Controls value of __GNUC__ and related macros. Set to 0 or empty to disable them.
\\ -fno-declspec Disable support for __declspec attributes
\\ -ffp-eval-method=[source|double|extended]
\\ Evaluation method to use for floating-point arithmetic
Expand Down Expand Up @@ -127,6 +132,10 @@ pub const usage =
\\ -isystem Add directory to SYSTEM include search path
\\ --emulate=[clang|gcc|msvc]
\\ Select which C compiler to emulate (default clang)
\\ -nobuiltininc Do not search the compiler's builtin directory for include files
\\ -nostdinc, --no-standard-includes
\\ Do not search the standard system directories or compiler builtin directories for include files.
\\ -nostdlibinc Do not search the standard system directories for include files, but do search compiler builtin include directories
\\ -o <file> Write output to <file>
\\ -P, --no-line-commands Disable linemarker output in -E mode
\\ -pedantic Warn on language extensions
Expand Down Expand Up @@ -180,6 +189,7 @@ pub fn parseArgs(
var i: usize = 1;
var comment_arg: []const u8 = "";
var hosted: ?bool = null;
var gnuc_version: []const u8 = "4.2.1"; // default value set by clang
while (i < args.len) : (i += 1) {
const arg = args[i];
if (mem.startsWith(u8, arg, "-") and arg.len > 1) {
Expand Down Expand Up @@ -303,6 +313,10 @@ pub fn parseArgs(
d.only_syntax = true;
} else if (mem.startsWith(u8, arg, "-fno-syntax-only")) {
d.only_syntax = false;
} else if (mem.eql(u8, arg, "-fgnuc-version=")) {
gnuc_version = "0";
} else if (option(arg, "-fgnuc-version=")) |version| {
gnuc_version = version;
} else if (mem.startsWith(u8, arg, "-isystem")) {
var path = arg["-isystem".len..];
if (path.len == 0) {
Expand Down Expand Up @@ -421,6 +435,12 @@ pub fn parseArgs(
d.nodefaultlibs = true;
} else if (mem.eql(u8, arg, "-nolibc")) {
d.nolibc = true;
} else if (mem.eql(u8, arg, "-nobuiltininc")) {
d.nobuiltininc = true;
} else if (mem.eql(u8, arg, "-nostdinc") or mem.eql(u8, arg, "--no-standard-includes")) {
d.nostdinc = true;
} else if (mem.eql(u8, arg, "-nostdlibinc")) {
d.nostdlibinc = true;
} else if (mem.eql(u8, arg, "-nostdlib")) {
d.nostdlib = true;
} else if (mem.eql(u8, arg, "-nostartfiles")) {
Expand Down Expand Up @@ -459,6 +479,11 @@ pub fn parseArgs(
d.comp.target.os.tag = .freestanding;
}
}
const version = GCCVersion.parse(gnuc_version);
if (version.major == -1) {
return d.fatal("invalid value '{0s}' in '-fgnuc-version={0s}'", .{gnuc_version});
}
d.comp.langopts.gnuc_version = version.toUnsigned();
return false;
}

Expand Down Expand Up @@ -558,7 +583,8 @@ pub fn main(d: *Driver, tc: *Toolchain, args: []const []const u8, comptime fast_
try d.comp.addDiagnostic(.{ .tag = .cli_unused_link_object, .extra = .{ .str = obj } }, &.{});
};

d.comp.defineSystemIncludes(d.aro_name) catch |er| switch (er) {
try tc.discover();
tc.defineSystemIncludes() catch |er| switch (er) {
error.OutOfMemory => return error.OutOfMemory,
error.AroIncludeNotFound => return d.fatal("unable to find Aro builtin headers", .{}),
};
Expand Down Expand Up @@ -763,8 +789,6 @@ fn dumpLinkerArgs(items: []const []const u8) !void {
/// The entry point of the Aro compiler.
/// **MAY call `exit` if `fast_exit` is set.**
pub fn invokeLinker(d: *Driver, tc: *Toolchain, comptime fast_exit: bool) !void {
try tc.discover();

var argv = std.ArrayList([]const u8).init(d.comp.gpa);
defer argv.deinit();

Expand Down
10 changes: 10 additions & 0 deletions lib/compiler/aro/aro/Driver/GCCVersion.zig
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ pub fn order(a: GCCVersion, b: GCCVersion) Order {
return .eq;
}

/// Used for determining __GNUC__ macro values
/// This matches clang's logic for overflowing values
pub fn toUnsigned(self: GCCVersion) u32 {
var result: u32 = 0;
if (self.major > 0) result = @as(u32, @intCast(self.major)) *% 10_000;
if (self.minor > 0) result +%= @as(u32, @intCast(self.minor)) *% 100;
if (self.patch > 0) result +%= @as(u32, @intCast(self.patch));
return result;
}

test parse {
const versions = [10]GCCVersion{
parse("5"),
Expand Down
Loading