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 {
293293 try dg.renderType(w, t.elemType());
294294 try w.writeAll(" *");
295295 },
296 .Null, .Undefined => unreachable, // must be const or comptime
296297 else => |e| return dg.fail(dg.decl.src(), "TODO: C backend: implement type {s}", .{
297298 @tagName(e),
298299 }),
......@@ -387,6 +388,7 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
387388
388389 for (body.instructions) |inst| {
389390 const result_value = switch (inst.tag) {
391 .constant => unreachable, // excluded from function bodies
390392 .add => try genBinOp(o, inst.castTag(.add).?, " + "),
391393 .alloc => try genAlloc(o, inst.castTag(.alloc).?),
392394 .arg => genArg(o),
......@@ -415,8 +417,10 @@ pub fn genBody(o: *Object, body: ir.Body) error{ AnalysisFail, OutOfMemory }!voi
415417 .brvoid => try genBrVoid(o, inst.castTag(.brvoid).?.block),
416418 .switchbr => try genSwitchBr(o, inst.castTag(.switchbr).?),
417419 // booland and boolor are non-short-circuit operations
418 .booland => try genBinOp(o, inst.castTag(.booland).?, " & "),
419 .boolor => try genBinOp(o, inst.castTag(.boolor).?, " | "),
420 .booland, .bitand => try genBinOp(o, inst.castTag(.booland).?, " & "),
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).?, "!"),
420424 else => |e| return o.dg.fail(o.dg.decl.src(), "TODO: C backend: implement codegen for {}", .{e}),
421425 };
422426 switch (result_value) {
......@@ -541,6 +545,22 @@ fn genBinOp(o: *Object, inst: *Inst.BinOp, operator: []const u8) !CValue {
541545 return local;
542546}
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
544564fn genCall(o: *Object, inst: *Inst.Call) !CValue {
545565 if (inst.func.castTag(.constant)) |func_inst| {
546566 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 {
196196 \\ return a - 5;
197197 \\}
198198 , "");
199 case.addCompareOutput(
200 \\export fn main() c_int {
201 \\ var a = true;
202 \\ while (!a) {}
203 \\ return 0;
204 \\}
205 , "");
199206
200207 // If expression
201208 case.addCompareOutput(