diff options
Diffstat (limited to 'src/compile.zig')
-rw-r--r-- | src/compile.zig | 94 |
1 files changed, 51 insertions, 43 deletions
diff --git a/src/compile.zig b/src/compile.zig index d1cac8c..69ac270 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -5,6 +5,8 @@ const Token = root.Lexer.Token; const parse = root.parse; const Location = root.Lexer.Location; +const log = std.log.scoped(.compile); + /// Virtual register pub const VReg = enum(u32) { _ }; pub const BlockRef = enum(u32) { _ }; @@ -101,7 +103,7 @@ pub const BasicBlock = struct { for (self.instrs.items, 0..) |instr, i| { const is_last = i == self.instrs.items.len - 1; if (instr.ends_block() != is_last) { - std.log.debug("Instruction at {s}last position must {s}end block. Found instruction {}. Block is: {}", .{ + log.debug("Instruction at {s}last position must {s}end block. Found instruction {}. Block is: {}", .{ if (is_last) "" else "not ", if (is_last) "" else "not ", instr, @@ -263,8 +265,7 @@ pub const Instr = struct { pub fn dest(self: *const Instr) ?VReg { return switch (self.type) { - inline .constant, .bin_op, .call, .get_local => |s| s.dest, - .branch, .jump, .ret, .assign_local => null, + inline else => |instr| if (@hasField(@TypeOf(instr), "dest")) instr.dest else null, }; } @@ -398,18 +399,11 @@ fn compileProcedure( try pctx.locals.putNoClobber(ctx.allocator, lvar, {}); break :blk lvar; } else null; - try pctx.compileBlock(proc.body); - if (!pctx.currentBlockEnded()) { - const proc_res = pctx.vreg_ctr.get(); - try pctx.addInstr(.{ - .loc = .{ .start = 0, .end = 0 }, - .type = .{ .constant = .{ .dest = proc_res, .value = 0 } }, - }); - try pctx.addInstr(.{ - .loc = .{ .start = 0, .end = 0 }, - .type = .{ .ret = .{ .val = proc_res } }, - }); - } + const body_res = try pctx.compileExpr(proc.body); + try pctx.addInstrUnlessEnded(.{ + .loc = .{ .start = 0, .end = 0 }, + .type = .{ .ret = .{ .val = body_res } }, + }); var blocks = try ctx.allocator.alloc(BasicBlock, pctx.blocks.count()); var it = pctx.blocks.iterator(); var i: usize = 0; @@ -417,6 +411,9 @@ fn compileProcedure( std.debug.assert(kv.key_ptr.* == kv.value_ptr.ref); blocks[i] = kv.value_ptr.*; } + for (blocks) |block| { + log.debug("{s}\n{}", .{ name.getIdent(ctx.source), block }); + } return try .init( ctx.allocator, name.getIdent(ctx.source), @@ -438,31 +435,13 @@ const ProcedureContext = struct { blocks: std.AutoArrayHashMapUnmanaged(BlockRef, BasicBlock), current_block: BlockRef, - fn compileBlock(self: *ProcedureContext, block: parse.Block) !void { - var parent = self.scope; - self.scope = .{ - .locals = .empty, - .parent = &parent, - }; - - for (block.stmts) |stmt| { - try self.compileStmt(stmt); - } - - self.scope.locals.deinit(self.ctx.allocator); - self.scope = parent; - } - fn compileStmt(self: *ProcedureContext, stmt: parse.Stmt) CompileError!void { switch (stmt.type) { .expr => |expr| _ = try self.compileExpr(expr), - .block => |block| { - try self.compileBlock(block); - }, .assign_var => |assign_var| { const local = if (assign_var.is_decl) blk: { - const local = self.lvar_ctr.get(); const name = assign_var.ident.getIdent(self.ctx.source); + const local = self.lvar_ctr.get(); try self.scope.locals.put(self.ctx.allocator, name, local); try self.locals.put(self.ctx.allocator, local, {}); break :blk local; @@ -472,7 +451,7 @@ const ProcedureContext = struct { if (s.locals.get(assign_var.ident.getIdent(self.ctx.source))) |local| break :blk local; } else { - std.log.debug("{s}", .{assign_var.ident.getIdent(self.ctx.source)}); + log.debug("{s}", .{assign_var.ident.getIdent(self.ctx.source)}); return error.UnknownVariable; } }; @@ -507,7 +486,7 @@ const ProcedureContext = struct { } }, }); self.current_block = do; - try self.compileBlock(@"while".do); + _ = try self.compileExpr(@"while".do); try self.addInstrUnlessEnded(.{ .loc = stmt.loc, .type = .{ .jump = .{ .to = cond_block } }, @@ -572,7 +551,7 @@ const ProcedureContext = struct { break :blk local; } } - std.log.debug("{s}", .{expr.loc.getIdent(self.ctx.source)}); + log.debug("{s}", .{expr.loc.getIdent(self.ctx.source)}); return error.UnknownVariable; }; try self.addInstr(.{ @@ -589,8 +568,14 @@ const ProcedureContext = struct { const after = try self.switchToNewBlock(); + const phony = self.lvar_ctr.get(); + try self.locals.put(self.ctx.allocator, phony, {}); const t = try self.switchToNewBlock(); - try self.compileBlock(@"if".then); + const t_val = try self.compileExpr(@"if".then); + try self.addInstrUnlessEnded(.{ + .loc = expr.loc, + .type = .{ .assign_local = .{ .local = phony, .val = t_val } }, + }); try self.addInstrUnlessEnded(.{ .loc = expr.loc, .type = .{ .jump = .{ .to = after } }, @@ -598,7 +583,11 @@ const ProcedureContext = struct { const f = if (@"if".@"else") |@"else"| blk: { const f = try self.switchToNewBlock(); - try self.compileBlock(@"else"); + const f_val = try self.compileExpr(@"else"); + try self.addInstrUnlessEnded(.{ + .loc = expr.loc, + .type = .{ .assign_local = .{ .local = phony, .val = f_val } }, + }); try self.addInstrUnlessEnded(.{ .loc = expr.loc, .type = .{ .jump = .{ .to = after } }, @@ -617,11 +606,12 @@ const ProcedureContext = struct { }); self.current_block = after; + try self.addInstr(.{ + .loc = expr.loc, + .type = .{ .get_local = .{ .dest = dest, .local = phony } }, + }); }, - .proc => |proc| { - _ = proc; - return error.CannotDefineProcedureHere; - }, + .proc => return error.CannotDefineProcedureHere, .@"return" => |ret| { const val = try self.compileExpr(ret.value); try self.addInstr(.{ @@ -629,6 +619,24 @@ const ProcedureContext = struct { .type = .{ .ret = .{ .val = val } }, }); }, + .block => |block| { + var parent = self.scope; + defer self.scope = parent; + self.scope = .{ + .locals = .empty, + .parent = &parent, + }; + defer self.scope.locals.deinit(self.ctx.allocator); + + for (block.stmts) |stmt| { + try self.compileStmt(stmt); + } + + try self.addInstrUnlessEnded(.{ + .loc = expr.loc, + .type = .{ .constant = .{ .dest = dest, .value = 0 } }, + }); + }, } return dest; } |