authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-03 21:56:21-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-03 21:56:21-07:00
logedfede575c3113c3611b14a868e8d1e956ca9ef5
tree76e5e77bccee9103c2fe659191ebe438818e4cf6
parenta33efc74ed303517aa458109acfcd5c40dc97703

self-hosted: add build option for log scopes

Now you can enable a set of log scopes by passing -Dlog=<scope>

4 files changed, 59 insertions(+), 49 deletions(-)

build.zig+3
......@@ -77,6 +77,9 @@ pub fn build(b: *Builder) !void {
7777 const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse false;
7878 if (link_libc) exe.linkLibC();
7979
80 const log_scopes = b.option([]const []const u8, "log", "Which log scopes to enable") orelse &[0][]const u8{};
81
82 exe.addBuildOption([]const []const u8, "log_scopes", log_scopes);
8083 exe.addBuildOption(bool, "enable_tracy", tracy != null);
8184 if (tracy) |tracy_path| {
8285 const client_cpp = fs.path.join(
lib/std/build.zig+31-19
......@@ -430,9 +430,9 @@ pub const Builder = struct {
430430 const entry = self.user_input_options.getEntry(name) orelse return null;
431431 entry.value.used = true;
432432 switch (type_id) {
433 TypeId.Bool => switch (entry.value.value) {
434 UserValue.Flag => return true,
435 UserValue.Scalar => |s| {
433 .Bool => switch (entry.value.value) {
434 .Flag => return true,
435 .Scalar => |s| {
436436 if (mem.eql(u8, s, "true")) {
437437 return true;
438438 } else if (mem.eql(u8, s, "false")) {
......@@ -443,21 +443,21 @@ pub const Builder = struct {
443443 return null;
444444 }
445445 },
446 UserValue.List => {
446 .List => {
447447 warn("Expected -D{} to be a boolean, but received a list.\n", .{name});
448448 self.markInvalidUserInput();
449449 return null;
450450 },
451451 },
452 TypeId.Int => panic("TODO integer options to build script", .{}),
453 TypeId.Float => panic("TODO float options to build script", .{}),
454 TypeId.Enum => switch (entry.value.value) {
455 UserValue.Flag => {
452 .Int => panic("TODO integer options to build script", .{}),
453 .Float => panic("TODO float options to build script", .{}),
454 .Enum => switch (entry.value.value) {
455 .Flag => {
456456 warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
457457 self.markInvalidUserInput();
458458 return null;
459459 },
460 UserValue.Scalar => |s| {
460 .Scalar => |s| {
461461 if (std.meta.stringToEnum(T, s)) |enum_lit| {
462462 return enum_lit;
463463 } else {
......@@ -466,33 +466,35 @@ pub const Builder = struct {
466466 return null;
467467 }
468468 },
469 UserValue.List => {
469 .List => {
470470 warn("Expected -D{} to be a string, but received a list.\n", .{name});
471471 self.markInvalidUserInput();
472472 return null;
473473 },
474474 },
475 TypeId.String => switch (entry.value.value) {
476 UserValue.Flag => {
475 .String => switch (entry.value.value) {
476 .Flag => {
477477 warn("Expected -D{} to be a string, but received a boolean.\n", .{name});
478478 self.markInvalidUserInput();
479479 return null;
480480 },
481 UserValue.List => {
481 .List => {
482482 warn("Expected -D{} to be a string, but received a list.\n", .{name});
483483 self.markInvalidUserInput();
484484 return null;
485485 },
486 UserValue.Scalar => |s| return s,
486 .Scalar => |s| return s,
487487 },
488 TypeId.List => switch (entry.value.value) {
489 UserValue.Flag => {
488 .List => switch (entry.value.value) {
489 .Flag => {
490490 warn("Expected -D{} to be a list, but received a boolean.\n", .{name});
491491 self.markInvalidUserInput();
492492 return null;
493493 },
494 UserValue.Scalar => |s| return &[_][]const u8{s},
495 UserValue.List => |lst| return lst.span(),
494 .Scalar => |s| {
495 return self.allocator.dupe([]const u8, &[_][]const u8{s}) catch unreachable;
496 },
497 .List => |lst| return lst.span(),
496498 },
497499 }
498500 }
......@@ -1706,9 +1708,19 @@ pub const LibExeObjStep = struct {
17061708
17071709 pub fn addBuildOption(self: *LibExeObjStep, comptime T: type, name: []const u8, value: T) void {
17081710 const out = self.build_options_contents.outStream();
1711 if (T == []const []const u8) {
1712 out.print("pub const {}: []const []const u8 = &[_][]const u8{{\n", .{name}) catch unreachable;
1713 for (value) |slice| {
1714 out.writeAll(" ") catch unreachable;
1715 std.zig.renderStringLiteral(slice, out) catch unreachable;
1716 out.writeAll(",\n") catch unreachable;
1717 }
1718 out.writeAll("};\n") catch unreachable;
1719 return;
1720 }
17091721 switch (@typeInfo(T)) {
17101722 .Enum => |enum_info| {
1711 out.print("const {} = enum {{\n", .{@typeName(T)}) catch unreachable;
1723 out.print("pub const {} = enum {{\n", .{@typeName(T)}) catch unreachable;
17121724 inline for (enum_info.fields) |field| {
17131725 out.print(" {},\n", .{field.name}) catch unreachable;
17141726 }
src-self-hosted/Module.zig+16-17
......@@ -6,6 +6,7 @@ const Value = @import("value.zig").Value;
66const Type = @import("type.zig").Type;
77const TypedValue = @import("TypedValue.zig");
88const assert = std.debug.assert;
9const log = std.log;
910const BigIntConst = std.math.big.int.Const;
1011const BigIntMutable = std.math.big.int.Mutable;
1112const Target = std.Target;
......@@ -235,7 +236,7 @@ pub const Decl = struct {
235236
236237 pub fn dump(self: *Decl) void {
237238 const loc = std.zig.findLineColumn(self.scope.source.bytes, self.src);
238 std.debug.warn("{}:{}:{} name={} status={}", .{
239 std.debug.print("{}:{}:{} name={} status={}", .{
239240 self.scope.sub_file_path,
240241 loc.line + 1,
241242 loc.column + 1,
......@@ -243,9 +244,9 @@ pub const Decl = struct {
243244 @tagName(self.analysis),
244245 });
245246 if (self.typedValueManaged()) |tvm| {
246 std.debug.warn(" ty={} val={}", .{ tvm.typed_value.ty, tvm.typed_value.val });
247 std.debug.print(" ty={} val={}", .{ tvm.typed_value.ty, tvm.typed_value.val });
247248 }
248 std.debug.warn("\n", .{});
249 std.debug.print("\n", .{});
249250 }
250251
251252 pub fn typedValueManaged(self: *Decl) ?*TypedValue.Managed {
......@@ -544,7 +545,7 @@ pub const Scope = struct {
544545
545546 pub fn dumpSrc(self: *File, src: usize) void {
546547 const loc = std.zig.findLineColumn(self.source.bytes, src);
547 std.debug.warn("{}:{}:{}\n", .{ self.sub_file_path, loc.line + 1, loc.column + 1 });
548 std.debug.print("{}:{}:{}\n", .{ self.sub_file_path, loc.line + 1, loc.column + 1 });
548549 }
549550
550551 pub fn getSource(self: *File, module: *Module) ![:0]const u8 {
......@@ -646,7 +647,7 @@ pub const Scope = struct {
646647
647648 pub fn dumpSrc(self: *ZIRModule, src: usize) void {
648649 const loc = std.zig.findLineColumn(self.source.bytes, src);
649 std.debug.warn("{}:{}:{}\n", .{ self.sub_file_path, loc.line + 1, loc.column + 1 });
650 std.debug.print("{}:{}:{}\n", .{ self.sub_file_path, loc.line + 1, loc.column + 1 });
650651 }
651652
652653 pub fn getSource(self: *ZIRModule, module: *Module) ![:0]const u8 {
......@@ -946,7 +947,6 @@ pub fn update(self: *Module) !void {
946947 }
947948
948949 self.link_error_flags = self.bin_file.errorFlags();
949 std.log.debug(.module, "link_error_flags: {}\n", .{self.link_error_flags});
950950
951951 // If there are any errors, we anticipate the source files being loaded
952952 // to report error messages. Otherwise we unload all source files to save memory.
......@@ -1109,7 +1109,7 @@ pub fn ensureDeclAnalyzed(self: *Module, decl: *Decl) InnerError!void {
11091109 assert(decl.analysis == .complete);
11101110 return;
11111111 }
1112 //std.debug.warn("re-analyzing {}\n", .{decl.name});
1112 log.debug(.module, "re-analyzing {}\n", .{decl.name});
11131113
11141114 // The exports this Decl performs will be re-discovered, so we remove them here
11151115 // prior to re-analysis.
......@@ -1546,7 +1546,7 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
15461546 // Handle explicitly deleted decls from the source code. Not to be confused
15471547 // with when we delete decls because they are no longer referenced.
15481548 for (deleted_decls.items()) |entry| {
1549 //std.debug.warn("noticed '{}' deleted from source\n", .{entry.key.name});
1549 log.debug(.module, "noticed '{}' deleted from source\n", .{entry.key.name});
15501550 try self.deleteDecl(entry.key);
15511551 }
15521552}
......@@ -1575,7 +1575,6 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
15751575 const name_hash = root_scope.fullyQualifiedNameHash(src_decl.name);
15761576 if (self.decl_table.get(name_hash)) |decl| {
15771577 deleted_decls.removeAssertDiscard(decl);
1578 //std.debug.warn("'{}' contents: '{}'\n", .{ src_decl.name, src_decl.contents });
15791578 if (!srcHashEql(src_decl.contents_hash, decl.contents_hash)) {
15801579 try self.markOutdatedDecl(decl);
15811580 decl.contents_hash = src_decl.contents_hash;
......@@ -1600,7 +1599,7 @@ fn analyzeRootZIRModule(self: *Module, root_scope: *Scope.ZIRModule) !void {
16001599 // Handle explicitly deleted decls from the source code. Not to be confused
16011600 // with when we delete decls because they are no longer referenced.
16021601 for (deleted_decls.items()) |entry| {
1603 //std.debug.warn("noticed '{}' deleted from source\n", .{entry.key.name});
1602 log.debug(.module, "noticed '{}' deleted from source\n", .{entry.key.name});
16041603 try self.deleteDecl(entry.key);
16051604 }
16061605}
......@@ -1612,7 +1611,7 @@ fn deleteDecl(self: *Module, decl: *Decl) !void {
16121611 // not be present in the set, and this does nothing.
16131612 decl.scope.removeDecl(decl);
16141613
1615 //std.debug.warn("deleting decl '{}'\n", .{decl.name});
1614 log.debug(.module, "deleting decl '{}'\n", .{decl.name});
16161615 const name_hash = decl.fullyQualifiedNameHash();
16171616 self.decl_table.removeAssertDiscard(name_hash);
16181617 // Remove itself from its dependencies, because we are about to destroy the decl pointer.
......@@ -1698,17 +1697,17 @@ fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
16981697 const fn_zir = func.analysis.queued;
16991698 defer fn_zir.arena.promote(self.gpa).deinit();
17001699 func.analysis = .{ .in_progress = {} };
1701 //std.debug.warn("set {} to in_progress\n", .{decl.name});
1700 log.debug(.module, "set {} to in_progress\n", .{decl.name});
17021701
17031702 try zir_sema.analyzeBody(self, &inner_block.base, fn_zir.body);
17041703
17051704 const instructions = try arena.allocator.dupe(*Inst, inner_block.instructions.items);
17061705 func.analysis = .{ .success = .{ .instructions = instructions } };
1707 //std.debug.warn("set {} to success\n", .{decl.name});
1706 log.debug(.module, "set {} to success\n", .{decl.name});
17081707}
17091708
17101709fn markOutdatedDecl(self: *Module, decl: *Decl) !void {
1711 //std.debug.warn("mark {} outdated\n", .{decl.name});
1710 log.debug(.module, "mark {} outdated\n", .{decl.name});
17121711 try self.work_queue.writeItem(.{ .analyze_decl = decl });
17131712 if (self.failed_decls.remove(decl)) |entry| {
17141713 entry.value.destroy(self.gpa);
......@@ -2817,7 +2816,7 @@ pub fn dumpInst(self: *Module, scope: *Scope, inst: *Inst) void {
28172816 const source = zir_module.getSource(self) catch @panic("dumpInst failed to get source");
28182817 const loc = std.zig.findLineColumn(source, inst.src);
28192818 if (inst.tag == .constant) {
2820 std.debug.warn("constant ty={} val={} src={}:{}:{}\n", .{
2819 std.debug.print("constant ty={} val={} src={}:{}:{}\n", .{
28212820 inst.ty,
28222821 inst.castTag(.constant).?.val,
28232822 zir_module.subFilePath(),
......@@ -2825,7 +2824,7 @@ pub fn dumpInst(self: *Module, scope: *Scope, inst: *Inst) void {
28252824 loc.column + 1,
28262825 });
28272826 } else if (inst.deaths == 0) {
2828 std.debug.warn("{} ty={} src={}:{}:{}\n", .{
2827 std.debug.print("{} ty={} src={}:{}:{}\n", .{
28292828 @tagName(inst.tag),
28302829 inst.ty,
28312830 zir_module.subFilePath(),
......@@ -2833,7 +2832,7 @@ pub fn dumpInst(self: *Module, scope: *Scope, inst: *Inst) void {
28332832 loc.column + 1,
28342833 });
28352834 } else {
2836 std.debug.warn("{} ty={} deaths={b} src={}:{}:{}\n", .{
2835 std.debug.print("{} ty={} deaths={b} src={}:{}:{}\n", .{
28372836 @tagName(inst.tag),
28382837 inst.ty,
28392838 inst.deaths,
src-self-hosted/main.zig+9-13
......@@ -10,9 +10,7 @@ const Module = @import("Module.zig");
1010const link = @import("link.zig");
1111const Package = @import("Package.zig");
1212const zir = @import("zir.zig");
13
14// TODO Improve async I/O enough that we feel comfortable doing this.
15//pub const io_mode = .evented;
13const build_options = @import("build_options");
1614
1715pub const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
1816
......@@ -47,18 +45,16 @@ pub fn log(
4745 if (@enumToInt(level) > @enumToInt(std.log.level))
4846 return;
4947
50 const scope_prefix = "(" ++ switch (scope) {
51 // Uncomment to hide logs
52 //.compiler,
53 .module,
54 .liveness,
55 .link,
56 => return,
48 const scope_name = @tagName(scope);
49 const ok = comptime for (build_options.log_scopes) |log_scope| {
50 if (mem.eql(u8, log_scope, scope_name))
51 break true;
52 } else false;
5753
58 else => @tagName(scope),
59 } ++ "): ";
54 if (!ok)
55 return;
6056
61 const prefix = "[" ++ @tagName(level) ++ "] " ++ scope_prefix;
57 const prefix = "[" ++ @tagName(level) ++ "] " ++ "(" ++ @tagName(scope) ++ "): ";
6258
6359 // Print the message to stderr, silently ignoring any errors
6460 std.debug.print(prefix ++ format, args);