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...@@ -54,8 +54,10 @@ fn renderValue(ctx: *Context, writer: std.ArrayList(u8).Writer, T: Type, val: Va
54fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {54fn renderFunctionSignature(ctx: *Context, writer: std.ArrayList(u8).Writer, decl: *Decl) !void {
55 const tv = decl.typed_value.most_recent.typed_value;55 const tv = decl.typed_value.most_recent.typed_value;
56 try renderType(ctx, writer, tv.ty.fnReturnType());56 try renderType(ctx, writer, tv.ty.fnReturnType());
57 const name = try map(ctx.file.base.allocator, mem.spanZ(decl.name));57 // Use the child allocator directly, as we know the name can be freed before
58 defer ctx.file.base.allocator.free(name);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);
59 try writer.print(" {}(", .{name});61 try writer.print(" {}(", .{name});
60 var param_len = tv.ty.fnParamLen();62 var param_len = tv.ty.fnParamLen();
61 if (param_len == 0)63 if (param_len == 0)
...@@ -102,22 +104,18 @@ fn genArray(file: *C, decl: *Decl) !void {...@@ -102,22 +104,18 @@ fn genArray(file: *C, decl: *Decl) !void {
102const Context = struct {104const Context = struct {
103 file: *C,105 file: *C,
104 decl: *Decl,106 decl: *Decl,
105 inst_map: std.AutoHashMap(*Inst, []u8),107 inst_map: *std.AutoHashMap(*Inst, []u8),
108 arena: *std.heap.ArenaAllocator,
106 argdex: usize = 0,109 argdex: usize = 0,
107 unnamed_index: usize = 0,110 unnamed_index: usize = 0,
108111
109 fn name(self: *Context) ![]u8 {112 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});
111 self.unnamed_index += 1;114 self.unnamed_index += 1;
112 return val;115 return val;
113 }116 }
114117
115 fn deinit(self: *Context) void {118 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();
121 self.* = undefined;119 self.* = undefined;
122 }120 }
123};121};
...@@ -126,10 +124,15 @@ fn genFn(file: *C, decl: *Decl) !void {...@@ -126,10 +124,15 @@ fn genFn(file: *C, decl: *Decl) !void {
126 const writer = file.main.writer();124 const writer = file.main.writer();
127 const tv = decl.typed_value.most_recent.typed_value;125 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();
129 var ctx = Context{131 var ctx = Context{
130 .file = file,132 .file = file,
131 .decl = decl,133 .decl = decl,
132 .inst_map = std.AutoHashMap(*Inst, []u8).init(file.base.allocator),134 .arena = &arena,
135 .inst_map = &inst_map,
133 };136 };
134 defer ctx.deinit();137 defer ctx.deinit();
135138
...@@ -163,7 +166,7 @@ fn genFn(file: *C, decl: *Decl) !void {...@@ -163,7 +166,7 @@ fn genFn(file: *C, decl: *Decl) !void {
163}166}
164167
165fn genArg(ctx: *Context) !?[]u8 {168fn 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});
167 ctx.argdex += 1;170 ctx.argdex += 1;
168 return name;171 return name;
169}172}