Skip to content

Commit cf37d2e

Browse files
committed
aro: update
This is f5fb720a5399ee98e45f36337b2f68a4d23a783c plus ehaas's nonnull attribute pull request currently at 4b26cb3ac610a0a070fc43e43da8b4cdf0e9101b with zig patches intact.
1 parent ec60095 commit cf37d2e

31 files changed

+1482
-1239
lines changed

lib/compiler/aro/aro/Attribute.zig

Lines changed: 59 additions & 44 deletions
Large diffs are not rendered by default.

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

Lines changed: 498 additions & 495 deletions
Large diffs are not rendered by default.

lib/compiler/aro/aro/Builtins.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,13 @@ pub const Iterator = struct {
312312
};
313313

314314
test Iterator {
315+
const gpa = std.testing.allocator;
315316
var it = Iterator{};
316317

317-
var seen = std.StringHashMap(Builtin).init(std.testing.allocator);
318-
defer seen.deinit();
318+
var seen: std.StringHashMapUnmanaged(Builtin) = .empty;
319+
defer seen.deinit(gpa);
319320

320-
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
321+
var arena_state = std.heap.ArenaAllocator.init(gpa);
321322
defer arena_state.deinit();
322323
const arena = arena_state.allocator();
323324

@@ -333,7 +334,7 @@ test Iterator {
333334
std.debug.print("previous data: {}\n", .{seen.get(entry.name).?});
334335
return error.TestExpectedUniqueEntries;
335336
}
336-
try seen.put(try arena.dupe(u8, entry.name), entry.builtin);
337+
try seen.put(gpa, try arena.dupe(u8, entry.name), entry.builtin);
337338
}
338339
try std.testing.expectEqual(@as(usize, Builtin.data.len), seen.count());
339340
}

lib/compiler/aro/aro/CodeGen.zig

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ tree: *const Tree,
4040
comp: *Compilation,
4141
builder: Builder,
4242
wip_switch: *WipSwitch = undefined,
43-
symbols: std.ArrayListUnmanaged(Symbol) = .{},
44-
ret_nodes: std.ArrayListUnmanaged(Ir.Inst.Phi.Input) = .{},
45-
phi_nodes: std.ArrayListUnmanaged(Ir.Inst.Phi.Input) = .{},
46-
record_elem_buf: std.ArrayListUnmanaged(Interner.Ref) = .{},
47-
record_cache: std.AutoHashMapUnmanaged(QualType, Interner.Ref) = .{},
43+
symbols: std.ArrayList(Symbol) = .empty,
44+
ret_nodes: std.ArrayList(Ir.Inst.Phi.Input) = .empty,
45+
phi_nodes: std.ArrayList(Ir.Inst.Phi.Input) = .empty,
46+
record_elem_buf: std.ArrayList(Interner.Ref) = .empty,
47+
record_cache: std.AutoHashMapUnmanaged(QualType, Interner.Ref) = .empty,
4848
cond_dummy_ty: ?Interner.Ref = null,
4949
bool_invert: bool = false,
5050
bool_end_label: Ir.Ref = .none,
@@ -56,10 +56,11 @@ compound_assign_dummy: ?Ir.Ref = null,
5656

5757
fn fail(c: *CodeGen, comptime fmt: []const u8, args: anytype) error{ FatalError, OutOfMemory } {
5858
var sf = std.heap.stackFallback(1024, c.comp.gpa);
59-
var buf = std.ArrayList(u8).init(sf.get());
60-
defer buf.deinit();
59+
const allocator = sf.get();
60+
var buf: std.ArrayList(u8) = .empty;
61+
defer buf.deinit(allocator);
6162

62-
try buf.print(fmt, args);
63+
try buf.print(allocator, fmt, args);
6364
try c.comp.diagnostics.add(.{ .text = buf.items, .kind = .@"fatal error", .location = null });
6465
return error.FatalError;
6566
}

lib/compiler/aro/aro/Compilation.zig

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const EpochSeconds = std.time.epoch.EpochSeconds;
44
const mem = std.mem;
55
const Allocator = mem.Allocator;
66

7-
const Interner = @import("../backend.zig").Interner;
8-
const CodeGenOptions = @import("../backend.zig").CodeGenOptions;
7+
const backend = @import("../backend.zig");
8+
const Interner = backend.Interner;
9+
const CodeGenOptions = backend.CodeGenOptions;
910

1011
const Builtins = @import("Builtins.zig");
1112
const Builtin = Builtins.Builtin;
@@ -127,24 +128,24 @@ diagnostics: *Diagnostics,
127128

128129
code_gen_options: CodeGenOptions = .default,
129130
environment: Environment = .{},
130-
sources: std.StringArrayHashMapUnmanaged(Source) = .{},
131+
sources: std.StringArrayHashMapUnmanaged(Source) = .empty,
131132
/// Allocated into `gpa`, but keys are externally managed.
132-
include_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
133+
include_dirs: std.ArrayList([]const u8) = .empty,
133134
/// Allocated into `gpa`, but keys are externally managed.
134-
system_include_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
135+
system_include_dirs: std.ArrayList([]const u8) = .empty,
135136
/// Allocated into `gpa`, but keys are externally managed.
136-
after_include_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
137+
after_include_dirs: std.ArrayList([]const u8) = .empty,
137138
/// Allocated into `gpa`, but keys are externally managed.
138-
framework_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
139+
framework_dirs: std.ArrayList([]const u8) = .empty,
139140
/// Allocated into `gpa`, but keys are externally managed.
140-
system_framework_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
141+
system_framework_dirs: std.ArrayList([]const u8) = .empty,
141142
/// Allocated into `gpa`, but keys are externally managed.
142-
embed_dirs: std.ArrayListUnmanaged([]const u8) = .empty,
143+
embed_dirs: std.ArrayList([]const u8) = .empty,
143144
target: std.Target = @import("builtin").target,
144145
cmodel: std.builtin.CodeModel = .default,
145-
pragma_handlers: std.StringArrayHashMapUnmanaged(*Pragma) = .{},
146+
pragma_handlers: std.StringArrayHashMapUnmanaged(*Pragma) = .empty,
146147
langopts: LangOpts = .{},
147-
generated_buf: std.ArrayListUnmanaged(u8) = .{},
148+
generated_buf: std.ArrayList(u8) = .empty,
148149
builtins: Builtins = .{},
149150
string_interner: StringInterner = .{},
150151
interner: Interner = .{},
@@ -245,6 +246,13 @@ fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {
245246
try w.print("#define __GNUC_PATCHLEVEL__ {d}\n", .{comp.langopts.gnuc_version % 100});
246247
}
247248

249+
if (comp.code_gen_options.optimization_level.hasAnyOptimizations()) {
250+
try define(w, "__OPTIMIZE__");
251+
}
252+
if (comp.code_gen_options.optimization_level.isSizeOptimized()) {
253+
try define(w, "__OPTIMIZE_SIZE__");
254+
}
255+
248256
// os macros
249257
switch (comp.target.os.tag) {
250258
.linux => try defineStd(w, "linux", is_gnu),
@@ -1384,8 +1392,8 @@ pub fn addSourceFromOwnedBuffer(comp: *Compilation, path: []const u8, buf: []u8,
13841392
const duped_path = try comp.gpa.dupe(u8, path);
13851393
errdefer comp.gpa.free(duped_path);
13861394

1387-
var splice_list = std.array_list.Managed(u32).init(comp.gpa);
1388-
defer splice_list.deinit();
1395+
var splice_list: std.ArrayList(u32) = .empty;
1396+
defer splice_list.deinit(comp.gpa);
13891397

13901398
const source_id: Source.Id = @enumFromInt(comp.sources.count() + 2);
13911399

@@ -1418,9 +1426,9 @@ pub fn addSourceFromOwnedBuffer(comp: *Compilation, path: []const u8, buf: []u8,
14181426
},
14191427
.back_slash, .trailing_ws, .back_slash_cr => {
14201428
i = backslash_loc;
1421-
try splice_list.append(i);
1429+
try splice_list.append(comp.gpa, i);
14221430
if (state == .trailing_ws) {
1423-
try comp.addNewlineEscapeError(path, buf, splice_list.items, i, line);
1431+
try comp.addNewlineEscapeError(path, buf, splice_list.items, i, line, kind);
14241432
}
14251433
state = if (state == .back_slash_cr) .cr else .back_slash_cr;
14261434
},
@@ -1438,10 +1446,10 @@ pub fn addSourceFromOwnedBuffer(comp: *Compilation, path: []const u8, buf: []u8,
14381446
.back_slash, .trailing_ws => {
14391447
i = backslash_loc;
14401448
if (state == .back_slash or state == .trailing_ws) {
1441-
try splice_list.append(i);
1449+
try splice_list.append(comp.gpa, i);
14421450
}
14431451
if (state == .trailing_ws) {
1444-
try comp.addNewlineEscapeError(path, buf, splice_list.items, i, line);
1452+
try comp.addNewlineEscapeError(path, buf, splice_list.items, i, line, kind);
14451453
}
14461454
},
14471455
.bom1, .bom2 => break,
@@ -1491,11 +1499,11 @@ pub fn addSourceFromOwnedBuffer(comp: *Compilation, path: []const u8, buf: []u8,
14911499
}
14921500
}
14931501

1494-
const splice_locs = try splice_list.toOwnedSlice();
1502+
const splice_locs = try splice_list.toOwnedSlice(comp.gpa);
14951503
errdefer comp.gpa.free(splice_locs);
14961504

14971505
if (i != contents.len) {
1498-
var list: std.ArrayListUnmanaged(u8) = .{
1506+
var list: std.ArrayList(u8) = .{
14991507
.items = contents[0..i],
15001508
.capacity = contents.len,
15011509
};
@@ -1515,13 +1523,21 @@ pub fn addSourceFromOwnedBuffer(comp: *Compilation, path: []const u8, buf: []u8,
15151523
return source;
15161524
}
15171525

1518-
fn addNewlineEscapeError(comp: *Compilation, path: []const u8, buf: []const u8, splice_locs: []const u32, byte_offset: u32, line: u32) !void {
1526+
fn addNewlineEscapeError(
1527+
comp: *Compilation,
1528+
path: []const u8,
1529+
buf: []const u8,
1530+
splice_locs: []const u32,
1531+
byte_offset: u32,
1532+
line: u32,
1533+
kind: Source.Kind,
1534+
) !void {
15191535
// Temporary source for getting the location for errors.
15201536
var tmp_source: Source = .{
15211537
.path = path,
15221538
.buf = buf,
15231539
.id = undefined,
1524-
.kind = undefined,
1540+
.kind = kind,
15251541
.splice_locs = splice_locs,
15261542
};
15271543

@@ -1571,17 +1587,7 @@ fn addSourceFromPathExtra(comp: *Compilation, path: []const u8, kind: Source.Kin
15711587
}
15721588

15731589
pub fn addSourceFromFile(comp: *Compilation, file: std.fs.File, path: []const u8, kind: Source.Kind) !Source {
1574-
var file_buf: [4096]u8 = undefined;
1575-
var file_reader = file.reader(&file_buf);
1576-
if (try file_reader.getSize() > std.math.maxInt(u32)) return error.FileTooBig;
1577-
1578-
var allocating: std.Io.Writer.Allocating = .init(comp.gpa);
1579-
_ = allocating.writer.sendFileAll(&file_reader, .limited(std.math.maxInt(u32))) catch |e| switch (e) {
1580-
error.WriteFailed => return error.OutOfMemory,
1581-
error.ReadFailed => return file_reader.err.?,
1582-
};
1583-
1584-
const contents = try allocating.toOwnedSlice();
1590+
const contents = try comp.getFileContents(file, .unlimited);
15851591
errdefer comp.gpa.free(contents);
15861592
return comp.addSourceFromOwnedBuffer(path, contents, kind);
15871593
}
@@ -1676,7 +1682,7 @@ const FindInclude = struct {
16761682
if (try find.checkFrameworkDir(dir, .system)) |res| return res;
16771683
}
16781684
for (comp.after_include_dirs.items) |dir| {
1679-
if (try find.checkIncludeDir(dir, .user)) |res| return res;
1685+
if (try find.checkIncludeDir(dir, .system)) |res| return res;
16801686
}
16811687
if (comp.ms_cwd_source_id) |source_id| {
16821688
if (try find.checkMsCwdIncludeDir(source_id)) |res| return res;
@@ -1771,26 +1777,38 @@ pub const IncludeType = enum {
17711777
angle_brackets,
17721778
};
17731779

1774-
fn getFileContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![]const u8 {
1780+
fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![]u8 {
17751781
if (mem.indexOfScalar(u8, path, 0) != null) {
17761782
return error.FileNotFound;
17771783
}
17781784

17791785
const file = try comp.cwd.openFile(path, .{});
17801786
defer file.close();
1787+
return comp.getFileContents(file, limit);
1788+
}
17811789

1782-
var allocating: std.Io.Writer.Allocating = .init(comp.gpa);
1783-
defer allocating.deinit();
1784-
1790+
fn getFileContents(comp: *Compilation, file: std.fs.File, limit: std.Io.Limit) ![]u8 {
17851791
var file_buf: [4096]u8 = undefined;
17861792
var file_reader = file.reader(&file_buf);
1787-
if (limit.minInt64(try file_reader.getSize()) > std.math.maxInt(u32)) return error.FileTooBig;
1788-
1789-
_ = allocating.writer.sendFileAll(&file_reader, limit) catch |err| switch (err) {
1790-
error.WriteFailed => return error.OutOfMemory,
1791-
error.ReadFailed => return file_reader.err.?,
1792-
};
17931793

1794+
var allocating: std.Io.Writer.Allocating = .init(comp.gpa);
1795+
defer allocating.deinit();
1796+
if (file_reader.getSize()) |size| {
1797+
const limited_size = limit.minInt64(size);
1798+
if (limited_size > std.math.maxInt(u32)) return error.FileTooBig;
1799+
try allocating.ensureUnusedCapacity(limited_size);
1800+
} else |_| {}
1801+
1802+
var remaining = limit.min(.limited(std.math.maxInt(u32)));
1803+
while (remaining.nonzero()) {
1804+
const n = file_reader.interface.stream(&allocating.writer, remaining) catch |err| switch (err) {
1805+
error.EndOfStream => return allocating.toOwnedSlice(),
1806+
error.WriteFailed => return error.OutOfMemory,
1807+
error.ReadFailed => return file_reader.err.?,
1808+
};
1809+
remaining = remaining.subtract(n).?;
1810+
}
1811+
if (limit == .unlimited) return error.FileTooBig;
17941812
return allocating.toOwnedSlice();
17951813
}
17961814

@@ -1802,9 +1820,10 @@ pub fn findEmbed(
18021820
include_type: IncludeType,
18031821
limit: std.Io.Limit,
18041822
opt_dep_file: ?*DepFile,
1805-
) !?[]const u8 {
1823+
) !?[]u8 {
18061824
if (std.fs.path.isAbsolute(filename)) {
1807-
if (comp.getFileContents(filename, limit)) |some| {
1825+
if (comp.getPathContents(filename, limit)) |some| {
1826+
errdefer comp.gpa.free(some);
18081827
if (opt_dep_file) |dep_file| try dep_file.addDependencyDupe(comp.gpa, comp.arena, filename);
18091828
return some;
18101829
} else |err| switch (err) {
@@ -1824,7 +1843,8 @@ pub fn findEmbed(
18241843
if (comp.langopts.ms_extensions) {
18251844
std.mem.replaceScalar(u8, path, '\\', '/');
18261845
}
1827-
if (comp.getFileContents(path, limit)) |some| {
1846+
if (comp.getPathContents(path, limit)) |some| {
1847+
errdefer comp.gpa.free(some);
18281848
if (opt_dep_file) |dep_file| try dep_file.addDependencyDupe(comp.gpa, comp.arena, filename);
18291849
return some;
18301850
} else |err| switch (err) {
@@ -1840,7 +1860,8 @@ pub fn findEmbed(
18401860
if (comp.langopts.ms_extensions) {
18411861
std.mem.replaceScalar(u8, path, '\\', '/');
18421862
}
1843-
if (comp.getFileContents(path, limit)) |some| {
1863+
if (comp.getPathContents(path, limit)) |some| {
1864+
errdefer comp.gpa.free(some);
18441865
if (opt_dep_file) |dep_file| try dep_file.addDependencyDupe(comp.gpa, comp.arena, filename);
18451866
return some;
18461867
} else |err| switch (err) {

lib/compiler/aro/aro/DepFile.zig

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn write(d: *const DepFile, w: *std.Io.Writer) std.Io.Writer.Error!void {
2828
const max_columns = 75;
2929
var columns: usize = 0;
3030

31-
try w.writeAll(d.target);
31+
try writeTarget(d.target, w);
3232
columns += d.target.len;
3333
try w.writeByte(':');
3434
columns += 1;
@@ -48,6 +48,26 @@ pub fn write(d: *const DepFile, w: *std.Io.Writer) std.Io.Writer.Error!void {
4848
try w.flush();
4949
}
5050

51+
fn writeTarget(path: []const u8, w: *std.Io.Writer) !void {
52+
for (path, 0..) |c, i| {
53+
switch (c) {
54+
' ', '\t' => {
55+
try w.writeByte('\\');
56+
var j = i;
57+
while (j != 0) {
58+
j -= 1;
59+
if (path[j] != '\\') break;
60+
try w.writeByte('\\');
61+
}
62+
},
63+
'$' => try w.writeByte('$'),
64+
'#' => try w.writeByte('\\'),
65+
else => {},
66+
}
67+
try w.writeByte(c);
68+
}
69+
}
70+
5171
fn writePath(d: *const DepFile, path: []const u8, w: *std.Io.Writer) !void {
5272
switch (d.format) {
5373
.nmake => {
@@ -58,18 +78,19 @@ fn writePath(d: *const DepFile, path: []const u8, w: *std.Io.Writer) !void {
5878
},
5979
.make => {
6080
for (path, 0..) |c, i| {
61-
if (c == '#') {
62-
try w.writeByte('\\');
63-
} else if (c == '$') {
64-
try w.writeByte('$');
65-
} else if (c == ' ') {
66-
try w.writeByte('\\');
67-
var j = i;
68-
while (j != 0) {
69-
j -= 1;
70-
if (path[j] != '\\') break;
81+
switch (c) {
82+
' ' => {
7183
try w.writeByte('\\');
72-
}
84+
var j = i;
85+
while (j != 0) {
86+
j -= 1;
87+
if (path[j] != '\\') break;
88+
try w.writeByte('\\');
89+
}
90+
},
91+
'$' => try w.writeByte('$'),
92+
'#' => try w.writeByte('\\'),
93+
else => {},
7394
}
7495
try w.writeByte(c);
7596
}

0 commit comments

Comments
 (0)