authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-09-08 21:26:24-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-10-06 15:09:56-04:00
log9ef6c0a035820b586cc2d3a051eef4ae21ec244b
tree6f8f67745acb240e06729d604ab449184ee9af41
parent9979719a9d0902ffde692ef06e7facf4c1921b99
signaturelock-open Commit is signed but in an unrecognized format.

CBE: utilize per-function arena allocator


1 files changed, 14 insertions(+), 11 deletions(-)

src/codegen/c.zig+14-11
......@@ -54,8 +54,10 @@ fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Va
5454fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
5555 const tv = decl.typed_value.most_recent.typed_value;
5656 try renderType(ctx, writer, tv.ty.fnReturnType());
57 const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name));
58 defer ctx.file.base.allocator.free(name);
57 // Use the child allocator directly, as we know the name can be freed before
58 // the rest of the arena.
59 const name = try map(ctx.arena.child_allocator, mem.spanZ(decl.name));
60 defer ctx.arena.child_allocator.free(name);
5961 try writer.print(" {}(", .{name});
6062 var param_len = tv.ty.fnParamLen();
6163 if (param_len == 0)
......@@ -102,22 +104,18 @@ fn genArray(file: *C, decl: *Decl) !void {
102104const Context = struct {
103105 file: *C,
104106 decl: *Decl,
105 inst_map: std.AutoHashMap(*Inst, []u8),
107 inst_map: *std.AutoHashMap(*Inst, []u8),
108 arena: *std.heap.ArenaAllocator,
106109 argdex: usize = 0,
107110 unnamed_index: usize = 0,
108111
109112 fn name(self: *Context) ![]u8 {
110 const val = try std.fmt.allocPrint(self.file.base.allocator, "__temp_{}", .{self.unnamed_index});
113 const val = try std.fmt.allocPrint(&self.arena.allocator, "__temp_{}", .{self.unnamed_index});
111114 self.unnamed_index += 1;
112115 return val;
113116 }
114117
115118 fn deinit(self: *Context) void {
116 var it = self.inst_map.iterator();
117 while (it.next()) |kv| {
118 self.file.base.allocator.free(kv.value);
119 }
120 self.inst_map.deinit();
121119 self.* = undefined;
122120 }
123121};
......@@ -126,10 +124,15 @@ fn genFn(file: *C, decl: *Decl) !void {
126124 const writer = file.main.writer();
127125 const tv = decl.typed_value.most_recent.typed_value;
128126
127 var arena = std.heap.ArenaAllocator.init(file.base.allocator);
128 defer arena.deinit();
129 var inst_map = std.AutoHashMap(*Inst, []u8).init(&arena.allocator);
130 defer inst_map.deinit();
129131 var ctx = Context{
130132 .file = file,
131133 .decl = decl,
132 .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator),
134 .arena = &arena,
135 .inst_map = &inst_map,
133136 };
134137 defer ctx.deinit();
135138
......@@ -163,7 +166,7 @@ fn genFn(file: *C, decl: *Decl) !void {
163166}
164167
165168fn genArg(ctx: *Context) !?[]u8 {
166 const name = try std.fmt.allocPrint(ctx.file.base.allocator, "arg{}", .{ctx.argdex});
169 const name = try std.fmt.allocPrint(&ctx.arena.allocator, "arg{}", .{ctx.argdex});
167170 ctx.argdex += 1;
168171 return name;
169172}