authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-19 15:31:50-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-19 15:31:50-07:00
log0357cd86537f708d915976398a38837feb1a5528
tree244f9334803dbbda47169bd94e1a2c5fec282a81
parent81a935aef81cac9e8c20ad6351290b8a56a7cf65

Sema: allocate inst_map with arena where appropriate


2 files changed, 70 insertions(+), 73 deletions(-)

src/Module.zig+11-21
......@@ -1826,7 +1826,7 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
18261826
18271827 const code = try gen_scope.finish();
18281828 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
1829 zir.dumpZir(mod.gpa, "comptime_block", decl.name, code) catch {};
1829 code.dump(mod.gpa, "comptime_block", decl.name) catch {};
18301830 }
18311831 break :blk code;
18321832 };
......@@ -1836,13 +1836,11 @@ fn astgenAndSemaDecl(mod: *Module, decl: *Decl) !bool {
18361836 .gpa = mod.gpa,
18371837 .arena = &analysis_arena.allocator,
18381838 .code = code,
1839 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
1839 .inst_map = try analysis_arena.allocator.alloc(*ir.Inst, code.instructions.len),
18401840 .owner_decl = decl,
18411841 .func = null,
18421842 .param_inst_list = &.{},
18431843 };
1844 defer mod.gpa.free(sema.inst_map);
1845
18461844 var block_scope: Scope.Block = .{
18471845 .parent = null,
18481846 .sema = &sema,
......@@ -2049,7 +2047,7 @@ fn astgenAndSemaFn(
20492047
20502048 const fn_type_code = try fn_type_scope.finish();
20512049 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2052 zir.dumpZir(mod.gpa, "fn_type", decl.name, fn_type_code) catch {};
2050 fn_type_code.dump(mod.gpa, "fn_type", decl.name) catch {};
20532051 }
20542052
20552053 var fn_type_sema: Sema = .{
......@@ -2057,13 +2055,11 @@ fn astgenAndSemaFn(
20572055 .gpa = mod.gpa,
20582056 .arena = &decl_arena.allocator,
20592057 .code = fn_type_code,
2060 .inst_map = try mod.gpa.alloc(*ir.Inst, fn_type_code.instructions.len),
2058 .inst_map = try fn_type_scope_arena.allocator.alloc(*ir.Inst, fn_type_code.instructions.len),
20612059 .owner_decl = decl,
20622060 .func = null,
20632061 .param_inst_list = &.{},
20642062 };
2065 defer mod.gpa.free(fn_type_sema.inst_map);
2066
20672063 var block_scope: Scope.Block = .{
20682064 .parent = null,
20692065 .sema = &fn_type_sema,
......@@ -2174,7 +2170,7 @@ fn astgenAndSemaFn(
21742170
21752171 const code = try gen_scope.finish();
21762172 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2177 zir.dumpZir(mod.gpa, "fn_body", decl.name, code) catch {};
2173 code.dump(mod.gpa, "fn_body", decl.name) catch {};
21782174 }
21792175
21802176 break :blk code;
......@@ -2351,7 +2347,7 @@ fn astgenAndSemaVarDecl(
23512347 );
23522348 const code = try gen_scope.finish();
23532349 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2354 zir.dumpZir(mod.gpa, "var_init", decl.name, code) catch {};
2350 code.dump(mod.gpa, "var_init", decl.name) catch {};
23552351 }
23562352
23572353 var sema: Sema = .{
......@@ -2359,13 +2355,11 @@ fn astgenAndSemaVarDecl(
23592355 .gpa = mod.gpa,
23602356 .arena = &gen_scope_arena.allocator,
23612357 .code = code,
2362 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
2358 .inst_map = try gen_scope_arena.allocator.alloc(*ir.Inst, code.instructions.len),
23632359 .owner_decl = decl,
23642360 .func = null,
23652361 .param_inst_list = &.{},
23662362 };
2367 defer mod.gpa.free(sema.inst_map);
2368
23692363 var block_scope: Scope.Block = .{
23702364 .parent = null,
23712365 .sema = &sema,
......@@ -2415,7 +2409,7 @@ fn astgenAndSemaVarDecl(
24152409 const var_type = try astgen.typeExpr(mod, &type_scope.base, var_decl.ast.type_node);
24162410 const code = try type_scope.finish();
24172411 if (std.builtin.mode == .Debug and mod.comp.verbose_ir) {
2418 zir.dumpZir(mod.gpa, "var_type", decl.name, code) catch {};
2412 code.dump(mod.gpa, "var_type", decl.name) catch {};
24192413 }
24202414
24212415 var sema: Sema = .{
......@@ -2423,13 +2417,11 @@ fn astgenAndSemaVarDecl(
24232417 .gpa = mod.gpa,
24242418 .arena = &type_scope_arena.allocator,
24252419 .code = code,
2426 .inst_map = try mod.gpa.alloc(*ir.Inst, code.instructions.len),
2420 .inst_map = try type_scope_arena.allocator.alloc(*ir.Inst, code.instructions.len),
24272421 .owner_decl = decl,
24282422 .func = null,
24292423 .param_inst_list = &.{},
24302424 };
2431 defer mod.gpa.free(sema.inst_map);
2432
24332425 var block_scope: Scope.Block = .{
24342426 .parent = null,
24352427 .sema = &sema,
......@@ -2985,9 +2977,6 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void {
29852977 var arena = decl.typed_value.most_recent.arena.?.promote(mod.gpa);
29862978 defer decl.typed_value.most_recent.arena.?.* = arena.state;
29872979
2988 const inst_map = try mod.gpa.alloc(*ir.Inst, func.zir.instructions.len);
2989 defer mod.gpa.free(inst_map);
2990
29912980 const fn_ty = decl.typed_value.most_recent.typed_value.ty;
29922981 const param_inst_list = try mod.gpa.alloc(*ir.Inst, fn_ty.fnParamLen());
29932982 defer mod.gpa.free(param_inst_list);
......@@ -3012,11 +3001,12 @@ pub fn analyzeFnBody(mod: *Module, decl: *Decl, func: *Fn) !void {
30123001 .gpa = mod.gpa,
30133002 .arena = &arena.allocator,
30143003 .code = func.zir,
3015 .inst_map = inst_map,
3004 .inst_map = try mod.gpa.alloc(*ir.Inst, func.zir.instructions.len),
30163005 .owner_decl = decl,
30173006 .func = func,
30183007 .param_inst_list = param_inst_list,
30193008 };
3009 defer mod.gpa.free(sema.inst_map);
30203010
30213011 var inner_block: Scope.Block = .{
30223012 .parent = null,
src/zir.zig+59-52
......@@ -1,4 +1,5 @@
1//! This file has to do with parsing and rendering the ZIR text format.
1//! Zig Intermediate Representation. astgen.zig converts AST nodes to these
2//! untyped IR instructions. Next, Sema.zig processes these into TZIR.
23
34const std = @import("std");
45const mem = std.mem;
......@@ -6,12 +7,13 @@ const Allocator = std.mem.Allocator;
67const assert = std.debug.assert;
78const BigIntConst = std.math.big.int.Const;
89const BigIntMutable = std.math.big.int.Mutable;
10const ast = std.zig.ast;
11
912const Type = @import("type.zig").Type;
1013const Value = @import("value.zig").Value;
1114const TypedValue = @import("TypedValue.zig");
1215const ir = @import("ir.zig");
1316const Module = @import("Module.zig");
14const ast = std.zig.ast;
1517const LazySrcLoc = Module.LazySrcLoc;
1618
1719/// The minimum amount of information needed to represent a list of ZIR instructions.
......@@ -64,6 +66,61 @@ pub const Code = struct {
6466 }
6567 return code.string_bytes[index..end :0];
6668 }
69
70 /// For debugging purposes, like dumpFn but for unanalyzed zir blocks
71 pub fn dump(code: Code, gpa: *Allocator, kind: []const u8, decl_name: [*:0]const u8) !void {
72 var arena = std.heap.ArenaAllocator.init(gpa);
73 defer arena.deinit();
74
75 if (true) @panic("TODO fix this function for zir-memory-layout branch");
76
77 var writer: Writer = .{
78 .gpa = gpa,
79 .arena = &arena.allocator,
80 .code = code,
81 .inst_map = try arena.allocator.alloc(*ir.Inst, code.instructions.len),
82 .owner_decl = decl,
83 .func = null,
84 .param_inst_list = &.{},
85 };
86 var write = Writer{
87 .inst_table = InstPtrTable.init(gpa),
88 .block_table = std.AutoHashMap(*Inst.Block, []const u8).init(gpa),
89 .loop_table = std.AutoHashMap(*Inst.Loop, []const u8).init(gpa),
90 .arena = std.heap.ArenaAllocator.init(gpa),
91 .indent = 4,
92 .next_instr_index = 0,
93 };
94 defer write.arena.deinit();
95 defer write.inst_table.deinit();
96 defer write.block_table.deinit();
97 defer write.loop_table.deinit();
98
99 try write.inst_table.ensureCapacity(@intCast(u32, instructions.len));
100
101 const stderr = std.io.getStdErr().writer();
102 try stderr.print("{s} {s} {{ // unanalyzed\n", .{ kind, decl_name });
103
104 for (instructions) |inst| {
105 const my_i = write.next_instr_index;
106 write.next_instr_index += 1;
107
108 if (inst.cast(Inst.Block)) |block| {
109 const name = try std.fmt.allocPrint(&write.arena.allocator, "label_{d}", .{my_i});
110 try write.block_table.put(block, name);
111 } else if (inst.cast(Inst.Loop)) |loop| {
112 const name = try std.fmt.allocPrint(&write.arena.allocator, "loop_{d}", .{my_i});
113 try write.loop_table.put(loop, name);
114 }
115
116 try write.inst_table.putNoClobber(inst, .{ .inst = inst, .index = my_i, .name = "inst" });
117 try stderr.print(" %{d} ", .{my_i});
118 try write.writeInstToStream(stderr, inst);
119 try stderr.writeByte('\n');
120 }
121
122 try stderr.print("}} // {s} {s}\n\n", .{ kind, decl_name });
123 }
67124};
68125
69126/// These correspond to the first N tags of Value.
......@@ -1209,53 +1266,3 @@ pub const Inst = struct {
12091266 field_name: Ref,
12101267 };
12111268};
1212
1213/// For debugging purposes, like dumpFn but for unanalyzed zir blocks
1214pub fn dumpZir(gpa: *Allocator, kind: []const u8, decl_name: [*:0]const u8, code: Code) !void {
1215 if (true) @panic("TODO fix this function for zir-memory-layout branch");
1216 var fib = std.heap.FixedBufferAllocator.init(&[_]u8{});
1217 var module = Module{
1218 .decls = &[_]*Module.Decl{},
1219 .arena = std.heap.ArenaAllocator.init(&fib.allocator),
1220 .metadata = std.AutoHashMap(*Inst, Module.MetaData).init(&fib.allocator),
1221 .body_metadata = std.AutoHashMap(*Body, Module.BodyMetaData).init(&fib.allocator),
1222 };
1223 var write = Writer{
1224 .module = &module,
1225 .inst_table = InstPtrTable.init(gpa),
1226 .block_table = std.AutoHashMap(*Inst.Block, []const u8).init(gpa),
1227 .loop_table = std.AutoHashMap(*Inst.Loop, []const u8).init(gpa),
1228 .arena = std.heap.ArenaAllocator.init(gpa),
1229 .indent = 4,
1230 .next_instr_index = 0,
1231 };
1232 defer write.arena.deinit();
1233 defer write.inst_table.deinit();
1234 defer write.block_table.deinit();
1235 defer write.loop_table.deinit();
1236
1237 try write.inst_table.ensureCapacity(@intCast(u32, instructions.len));
1238
1239 const stderr = std.io.getStdErr().writer();
1240 try stderr.print("{s} {s} {{ // unanalyzed\n", .{ kind, decl_name });
1241
1242 for (instructions) |inst| {
1243 const my_i = write.next_instr_index;
1244 write.next_instr_index += 1;
1245
1246 if (inst.cast(Inst.Block)) |block| {
1247 const name = try std.fmt.allocPrint(&write.arena.allocator, "label_{d}", .{my_i});
1248 try write.block_table.put(block, name);
1249 } else if (inst.cast(Inst.Loop)) |loop| {
1250 const name = try std.fmt.allocPrint(&write.arena.allocator, "loop_{d}", .{my_i});
1251 try write.loop_table.put(loop, name);
1252 }
1253
1254 try write.inst_table.putNoClobber(inst, .{ .inst = inst, .index = my_i, .name = "inst" });
1255 try stderr.print(" %{d} ", .{my_i});
1256 try write.writeInstToStream(stderr, inst);
1257 try stderr.writeByte('\n');
1258 }
1259
1260 try stderr.print("}} // {s} {s}\n\n", .{ kind, decl_name });
1261}