CBE: addition and subtraction

This commit is contained in:
Noam Preil
2020-09-08 14:39:47 -04:00
parent e06ba9e86e
commit ea7b2750c8
2 changed files with 63 additions and 0 deletions

View File

@@ -160,6 +160,8 @@ fn genFn(file: *C, decl: *Decl) !void {
if (switch (inst.tag) {
.assembly => try genAsm(&ctx, inst.castTag(.assembly).?),
.call => try genCall(&ctx, inst.castTag(.call).?),
.add => try genBinOp(&ctx, inst.cast(Inst.BinOp).?, "+"),
.sub => try genBinOp(&ctx, inst.cast(Inst.BinOp).?, "-"),
.ret => try genRet(&ctx, inst.castTag(.ret).?),
.retvoid => try genRetVoid(&ctx),
.arg => try genArg(&ctx),
@@ -207,6 +209,19 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
return name;
}
fn genBinOp(ctx: *Context, inst: *Inst.BinOp, comptime operator: []const u8) !?[]u8 {
if (inst.base.isUnused())
return null;
const lhs = ctx.resolveInst(inst.lhs);
const rhs = ctx.resolveInst(inst.rhs);
const writer = ctx.file.main.writer();
const name = try ctx.name();
try writer.writeAll(indentation ++ "const ");
try renderType(ctx, writer, inst.base.ty);
try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs });
return name;
}
fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
const writer = ctx.file.main.writer();
const header = ctx.file.header.writer();