authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-01-27 12:22:38+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-02-01 08:48:24+02:00
log3ec5c9a3bcae09c01cbe4f0505e6ab03834bbb98
tree2558bfafc885f48c78b8035b3b0431a4cb7545d7
parent106520329e5adc6cf5ef83595da6c9d5dd3c4b35
signaturelock-open Commit is signed but in an unrecognized format.

stage2 cbe: implement not and some bitwise ops


2 files changed, 29 insertions(+), 2 deletions(-)

src/codegen/c.zig+22-2
...@@ -293,6 +293,7 @@ pub const DeclGen = struct {...@@ -293,6 +293,7 @@ pub const DeclGen = struct {
293 try dg.renderType(w, t.elemType());293 try dg.renderType(w, t.elemType());
294 try w.writeAll(" *");294 try w.writeAll(" *");
295 },295 },
296 .Null, .Undefined => unreachable, // must be const or comptime
296 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{297 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{
297 @tagName(e),298 @tagName(e),
298 }),299 }),
...@@ -387,6 +388,7 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi...@@ -387,6 +388,7 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
387388
388 for (body.instructions) |inst| {389 for (body.instructions) |inst| {
389 const result_value = switch (inst.tag) {390 const result_value = switch (inst.tag) {
391 .constant => unreachable, // excluded from function bodies
390 .add => try genBinOp(o, inst.castTag(.add).?, " + "),392 .add => try genBinOp(o, inst.castTag(.add).?, " + "),
391 .alloc => try genAlloc(o, inst.castTag(.alloc).?),393 .alloc => try genAlloc(o, inst.castTag(.alloc).?),
392 .arg => genArg(o),394 .arg => genArg(o),
...@@ -415,8 +417,10 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi...@@ -415,8 +417,10 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
415 .brvoid => try genBrVoid(o, inst.castTag(.brvoid).?.block),417 .brvoid => try genBrVoid(o, inst.castTag(.brvoid).?.block),
416 .switchbr => try genSwitchBr(o, inst.castTag(.switchbr).?),418 .switchbr => try genSwitchBr(o, inst.castTag(.switchbr).?),
417 // booland and boolor are non-short-circuit operations419 // booland and boolor are non-short-circuit operations
418 .booland => try genBinOp(o, inst.castTag(.booland).?, " & "),420 .booland, .bitand => try genBinOp(o, inst.castTag(.booland).?, " & "),
419 .boolor => try genBinOp(o, inst.castTag(.boolor).?, " | "),421 .boolor, .bitor => try genBinOp(o, inst.castTag(.boolor).?, " | "),
422 .xor => try genBinOp(o, inst.castTag(.xor).?, " ^ "),
423 .not => try genUnOp(o, inst.castTag(.not).?, "!"),
420 else => |e| return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement codegen for {}", .{e}),424 else => |e| return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement codegen for {}", .{e}),
421 };425 };
422 switch (result_value) {426 switch (result_value) {
...@@ -541,6 +545,22 @@ fn genBinOp(o: *Object, inst: *Inst.BinOp, operator: []const u8) !CValue {...@@ -541,6 +545,22 @@ fn genBinOp(o: *Object, inst: *Inst.BinOp, operator: []const u8) !CValue {
541 return local;545 return local;
542}546}
543547
548fn genUnOp(o: *Object, inst: *Inst.UnOp, operator: []const u8) !CValue {
549 if (inst.base.isUnused())
550 return CValue.none;
551
552 const operand = try o.resolveInst(inst.operand);
553
554 const writer = o.writer();
555 const local = try o.allocLocal(inst.base.ty, .Const);
556
557 try writer.print(" = {s}", .{operator});
558 try o.writeCValue(writer, operand);
559 try writer.writeAll(";\n");
560
561 return local;
562}
563
544fn genCall(o: *Object, inst: *Inst.Call) !CValue {564fn genCall(o: *Object, inst: *Inst.Call) !CValue {
545 if (inst.func.castTag(.constant)) |func_inst| {565 if (inst.func.castTag(.constant)) |func_inst| {
546 const fn_decl = if (func_inst.val.castTag(.extern_fn)) |extern_fn|566 const fn_decl = if (func_inst.val.castTag(.extern_fn)) |extern_fn|
test/stage2/cbe.zig+7
...@@ -196,6 +196,13 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -196,6 +196,13 @@ pub fn addCases(ctx: *TestContext) !void {
196 \\ return a - 5;196 \\ return a - 5;
197 \\}197 \\}
198 , "");198 , "");
199 case.addCompareOutput(
200 \\export fn main() c_int {
201 \\ var a = true;
202 \\ while (!a) {}
203 \\ return 0;
204 \\}
205 , "");
199206
200 // If expression207 // If expression
201 case.addCompareOutput(208 case.addCompareOutput(