Skip to content

Commit 90ab8ea

Browse files
authored
Sync Aro sources (#19199)
ref: 02353ad9f17f659e173f68975a442fcec3dd2c94
1 parent 1e67f50 commit 90ab8ea

26 files changed

+851
-275
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ lib/libcxx/** linguist-vendored
1212
lib/libcxxabi/** linguist-vendored
1313
lib/libunwind/** linguist-vendored
1414
lib/tsan/** linguist-vendored
15-
deps/** linguist-vendored
15+
lib/compiler/aro/** linguist-vendored

lib/compiler/aro/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ int main(void) {
2020
printf("Hello, world!\n");
2121
return 0;
2222
}
23-
$ zig build run -- hello.c -o hello
23+
$ zig build && ./zig-out/bin/arocc hello.c -o hello
2424
$ ./hello
2525
Hello, world!
26-
$
2726
```

lib/compiler/aro/aro/Attribute/names.zig

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Autogenerated by GenerateDef from deps/aro/aro/Attribute/names.def, do not edit
1+
//! Autogenerated by GenerateDef from src/aro/Attribute/names.def, do not edit
22
// zig fmt: off
33

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

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

175165
const dafsa = [_]Node{

lib/compiler/aro/aro/Builtins.zig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ fn createType(desc: TypeDescription, it: *TypeDescription.TypeIterator, comp: *c
9999
}
100100
},
101101
.h => builder.combine(undefined, .fp16, 0) catch unreachable,
102-
.x => {
103-
// Todo: _Float16
104-
return .{ .specifier = .invalid };
105-
},
102+
.x => builder.combine(undefined, .float16, 0) catch unreachable,
106103
.y => {
107104
// Todo: __bf16
108105
return .{ .specifier = .invalid };

lib/compiler/aro/aro/Builtins/Builtin.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Autogenerated by GenerateDef from deps/aro/aro/Builtins/Builtin.def, do not edit
1+
//! Autogenerated by GenerateDef from src/aro/Builtins/Builtin.def, do not edit
22
// zig fmt: off
33

44
const std = @import("std");

lib/compiler/aro/aro/Compilation.zig

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ pub const SystemDefinesMode = enum {
241241
fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
242242
const ptr_width = comp.target.ptrBitWidth();
243243

244+
if (comp.langopts.gnuc_version > 0) {
245+
try w.print("#define __GNUC__ {d}\n", .{comp.langopts.gnuc_version / 10_000});
246+
try w.print("#define __GNUC_MINOR__ {d}\n", .{comp.langopts.gnuc_version / 100 % 100});
247+
try w.print("#define __GNUC_PATCHLEVEL__ {d}\n", .{comp.langopts.gnuc_version % 100});
248+
}
249+
244250
// os macros
245251
switch (comp.target.os.tag) {
246252
.linux => try w.writeAll(
@@ -419,6 +425,25 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
419425
\\
420426
);
421427

428+
// TODO: Set these to target-specific constants depending on backend capabilities
429+
// For now they are just set to the "may be lock-free" value
430+
try w.writeAll(
431+
\\#define __ATOMIC_BOOL_LOCK_FREE 1
432+
\\#define __ATOMIC_CHAR_LOCK_FREE 1
433+
\\#define __ATOMIC_CHAR16_T_LOCK_FREE 1
434+
\\#define __ATOMIC_CHAR32_T_LOCK_FREE 1
435+
\\#define __ATOMIC_WCHAR_T_LOCK_FREE 1
436+
\\#define __ATOMIC_SHORT_LOCK_FREE 1
437+
\\#define __ATOMIC_INT_LOCK_FREE 1
438+
\\#define __ATOMIC_LONG_LOCK_FREE 1
439+
\\#define __ATOMIC_LLONG_LOCK_FREE 1
440+
\\#define __ATOMIC_POINTER_LOCK_FREE 1
441+
\\
442+
);
443+
if (comp.langopts.hasChar8_T()) {
444+
try w.writeAll("#define __ATOMIC_CHAR8_T_LOCK_FREE 1\n");
445+
}
446+
422447
// types
423448
if (comp.getCharSignedness() == .unsigned) try w.writeAll("#define __CHAR_UNSIGNED__ 1\n");
424449
try w.writeAll("#define __CHAR_BIT__ 8\n");
@@ -438,6 +463,7 @@ fn generateSystemDefines(comp: *Compilation, w: anytype) !void {
438463
try comp.generateIntMaxAndWidth(w, "PTRDIFF", comp.types.ptrdiff);
439464
try comp.generateIntMaxAndWidth(w, "INTPTR", comp.types.intptr);
440465
try comp.generateIntMaxAndWidth(w, "UINTPTR", comp.types.intptr.makeIntegerUnsigned());
466+
try comp.generateIntMaxAndWidth(w, "SIG_ATOMIC", target_util.sigAtomicType(comp.target));
441467

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

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

519547
// standard macros
520548
try buf.appendSlice(
521-
\\#define __STDC_NO_ATOMICS__ 1
522549
\\#define __STDC_NO_COMPLEX__ 1
523550
\\#define __STDC_NO_THREADS__ 1
524551
\\#define __STDC_NO_VLA__ 1
@@ -1030,9 +1057,8 @@ pub fn getCharSignedness(comp: *const Compilation) std.builtin.Signedness {
10301057
return comp.langopts.char_signedness_override orelse comp.target.charSignedness();
10311058
}
10321059

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

1048-
if (comp.target.os.tag == .linux) {
1049-
const triple_str = try comp.target.linuxTriple(allocator);
1050-
defer allocator.free(triple_str);
1051-
1052-
const multiarch_path = try std.fs.path.join(allocator, &.{ "/usr/include", triple_str });
1053-
defer allocator.free(multiarch_path);
1054-
1055-
if (!std.meta.isError(std.fs.accessAbsolute(multiarch_path, .{}))) {
1056-
const duped = try comp.gpa.dupe(u8, multiarch_path);
1057-
errdefer comp.gpa.free(duped);
1058-
try comp.system_include_dirs.append(comp.gpa, duped);
1059-
}
1060-
}
1061-
const usr_include = try comp.gpa.dupe(u8, "/usr/include");
1062-
errdefer comp.gpa.free(usr_include);
1063-
try comp.system_include_dirs.append(comp.gpa, usr_include);
1075+
pub fn addSystemIncludeDir(comp: *Compilation, path: []const u8) !void {
1076+
const duped = try comp.gpa.dupe(u8, path);
1077+
errdefer comp.gpa.free(duped);
1078+
try comp.system_include_dirs.append(comp.gpa, duped);
10641079
}
10651080

10661081
pub fn getSource(comp: *const Compilation, id: Source.Id) Source {
@@ -1331,6 +1346,10 @@ pub fn hasInclude(
13311346
/// __has_include vs __has_include_next
13321347
which: WhichInclude,
13331348
) !bool {
1349+
if (mem.indexOfScalar(u8, filename, 0) != null) {
1350+
return false;
1351+
}
1352+
13341353
const cwd = std.fs.cwd();
13351354
if (std.fs.path.isAbsolute(filename)) {
13361355
if (which == .next) return false;

lib/compiler/aro/aro/Diagnostics.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ pub const Options = struct {
208208
@"unsupported-embed-param": Kind = .default,
209209
@"unused-result": Kind = .default,
210210
normalized: Kind = .default,
211+
@"shift-count-negative": Kind = .default,
212+
@"shift-count-overflow": Kind = .default,
211213
};
212214

213215
const Diagnostics = @This();
@@ -291,7 +293,7 @@ pub fn addExtra(
291293
.kind = .note,
292294
.extra = .{ .unsigned = expansion_locs.len - d.macro_backtrace_limit },
293295
});
294-
i = half - 1;
296+
i = half -| 1;
295297
while (i > 0) {
296298
i -= 1;
297299
d.list.appendAssumeCapacity(.{

lib/compiler/aro/aro/Diagnostics/messages.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Autogenerated by GenerateDef from deps/aro/aro/Diagnostics/messages.def, do not edit
1+
//! Autogenerated by GenerateDef from src/aro/Diagnostics/messages.def, do not edit
22
// zig fmt: off
33

44
const std = @import("std");
@@ -504,6 +504,11 @@ pub const Tag = enum {
504504
c23_auto_single_declarator,
505505
c32_auto_requires_initializer,
506506
c23_auto_scalar_init,
507+
negative_shift_count,
508+
too_big_shift_count,
509+
complex_conj,
510+
overflow_builtin_requires_int,
511+
overflow_result_requires_ptr,
507512

508513
pub fn property(tag: Tag) Properties {
509514
return named_data[@intFromEnum(tag)];
@@ -1005,6 +1010,11 @@ pub const Tag = enum {
10051010
.{ .msg = "'auto' can only be used with a single declarator", .kind = .@"error" },
10061011
.{ .msg = "'auto' requires an initializer", .kind = .@"error" },
10071012
.{ .msg = "'auto' requires a scalar initializer", .kind = .@"error" },
1013+
.{ .msg = "shift count is negative", .opt = W("shift-count-negative"), .kind = .warning, .all = true },
1014+
.{ .msg = "shift count >= width of type", .opt = W("shift-count-overflow"), .kind = .warning, .all = true },
1015+
.{ .msg = "ISO C does not support '~' for complex conjugation of '{s}'", .opt = W("pedantic"), .extra = .str, .kind = .off },
1016+
.{ .msg = "operand argument to overflow builtin must be an integer ('{s}' invalid)", .extra = .str, .kind = .@"error" },
1017+
.{ .msg = "result argument to overflow builtin must be a pointer to a non-const integer ('{s}' invalid)", .extra = .str, .kind = .@"error" },
10081018
};
10091019
};
10101020
};

lib/compiler/aro/aro/Driver.zig

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Preprocessor = @import("Preprocessor.zig");
1212
const Source = @import("Source.zig");
1313
const Toolchain = @import("Toolchain.zig");
1414
const target_util = @import("target.zig");
15+
const GCCVersion = @import("Driver/GCCVersion.zig");
1516

1617
pub const Linker = enum {
1718
ld,
@@ -43,6 +44,9 @@ verbose_pp: bool = false,
4344
verbose_ir: bool = false,
4445
verbose_linker_args: bool = false,
4546
color: ?bool = null,
47+
nobuiltininc: bool = false,
48+
nostdinc: bool = false,
49+
nostdlibinc: bool = false,
4650

4751
/// Full path to the aro executable
4852
aro_name: []const u8 = "",
@@ -95,6 +99,7 @@ pub const usage =
9599
\\ -fcolor-diagnostics Enable colors in diagnostics
96100
\\ -fno-color-diagnostics Disable colors in diagnostics
97101
\\ -fdeclspec Enable support for __declspec attributes
102+
\\ -fgnuc-version=<value> Controls value of __GNUC__ and related macros. Set to 0 or empty to disable them.
98103
\\ -fno-declspec Disable support for __declspec attributes
99104
\\ -ffp-eval-method=[source|double|extended]
100105
\\ Evaluation method to use for floating-point arithmetic
@@ -127,6 +132,10 @@ pub const usage =
127132
\\ -isystem Add directory to SYSTEM include search path
128133
\\ --emulate=[clang|gcc|msvc]
129134
\\ Select which C compiler to emulate (default clang)
135+
\\ -nobuiltininc Do not search the compiler's builtin directory for include files
136+
\\ -nostdinc, --no-standard-includes
137+
\\ Do not search the standard system directories or compiler builtin directories for include files.
138+
\\ -nostdlibinc Do not search the standard system directories for include files, but do search compiler builtin include directories
130139
\\ -o <file> Write output to <file>
131140
\\ -P, --no-line-commands Disable linemarker output in -E mode
132141
\\ -pedantic Warn on language extensions
@@ -180,6 +189,7 @@ pub fn parseArgs(
180189
var i: usize = 1;
181190
var comment_arg: []const u8 = "";
182191
var hosted: ?bool = null;
192+
var gnuc_version: []const u8 = "4.2.1"; // default value set by clang
183193
while (i < args.len) : (i += 1) {
184194
const arg = args[i];
185195
if (mem.startsWith(u8, arg, "-") and arg.len > 1) {
@@ -303,6 +313,10 @@ pub fn parseArgs(
303313
d.only_syntax = true;
304314
} else if (mem.startsWith(u8, arg, "-fno-syntax-only")) {
305315
d.only_syntax = false;
316+
} else if (mem.eql(u8, arg, "-fgnuc-version=")) {
317+
gnuc_version = "0";
318+
} else if (option(arg, "-fgnuc-version=")) |version| {
319+
gnuc_version = version;
306320
} else if (mem.startsWith(u8, arg, "-isystem")) {
307321
var path = arg["-isystem".len..];
308322
if (path.len == 0) {
@@ -421,6 +435,12 @@ pub fn parseArgs(
421435
d.nodefaultlibs = true;
422436
} else if (mem.eql(u8, arg, "-nolibc")) {
423437
d.nolibc = true;
438+
} else if (mem.eql(u8, arg, "-nobuiltininc")) {
439+
d.nobuiltininc = true;
440+
} else if (mem.eql(u8, arg, "-nostdinc") or mem.eql(u8, arg, "--no-standard-includes")) {
441+
d.nostdinc = true;
442+
} else if (mem.eql(u8, arg, "-nostdlibinc")) {
443+
d.nostdlibinc = true;
424444
} else if (mem.eql(u8, arg, "-nostdlib")) {
425445
d.nostdlib = true;
426446
} else if (mem.eql(u8, arg, "-nostartfiles")) {
@@ -459,6 +479,11 @@ pub fn parseArgs(
459479
d.comp.target.os.tag = .freestanding;
460480
}
461481
}
482+
const version = GCCVersion.parse(gnuc_version);
483+
if (version.major == -1) {
484+
return d.fatal("invalid value '{0s}' in '-fgnuc-version={0s}'", .{gnuc_version});
485+
}
486+
d.comp.langopts.gnuc_version = version.toUnsigned();
462487
return false;
463488
}
464489

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

561-
d.comp.defineSystemIncludes(d.aro_name) catch |er| switch (er) {
586+
try tc.discover();
587+
tc.defineSystemIncludes() catch |er| switch (er) {
562588
error.OutOfMemory => return error.OutOfMemory,
563589
error.AroIncludeNotFound => return d.fatal("unable to find Aro builtin headers", .{}),
564590
};
@@ -763,8 +789,6 @@ fn dumpLinkerArgs(items: []const []const u8) !void {
763789
/// The entry point of the Aro compiler.
764790
/// **MAY call `exit` if `fast_exit` is set.**
765791
pub fn invokeLinker(d: *Driver, tc: *Toolchain, comptime fast_exit: bool) !void {
766-
try tc.discover();
767-
768792
var argv = std.ArrayList([]const u8).init(d.comp.gpa);
769793
defer argv.deinit();
770794

lib/compiler/aro/aro/Driver/GCCVersion.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ pub fn order(a: GCCVersion, b: GCCVersion) Order {
9898
return .eq;
9999
}
100100

101+
/// Used for determining __GNUC__ macro values
102+
/// This matches clang's logic for overflowing values
103+
pub fn toUnsigned(self: GCCVersion) u32 {
104+
var result: u32 = 0;
105+
if (self.major > 0) result = @as(u32, @intCast(self.major)) *% 10_000;
106+
if (self.minor > 0) result +%= @as(u32, @intCast(self.minor)) *% 100;
107+
if (self.patch > 0) result +%= @as(u32, @intCast(self.patch));
108+
return result;
109+
}
110+
101111
test parse {
102112
const versions = [10]GCCVersion{
103113
parse("5"),

0 commit comments

Comments
 (0)