Skip to content

Commit

Permalink
cbe: fix not (it is a ty_op, not un_op)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-w1 authored and andrewrk committed Jul 20, 2021
1 parent 4a0f38b commit 414b144
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/codegen/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,8 @@ fn genBody(o: *Object, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfM
.bit_and => try airBinOp(o, inst, " & "),
.bit_or => try airBinOp(o, inst, " | "),
.xor => try airBinOp(o, inst, " ^ "),
.not => try airUnOp( o, inst, "!"),

.not => try airNot( o, inst),

.optional_payload => try airOptionalPayload(o, inst),
.optional_payload_ptr => try airOptionalPayload(o, inst),
Expand Down Expand Up @@ -1181,40 +1182,44 @@ fn airWrapOp(
return ret;
}

fn airBinOp(o: *Object, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue {
fn airNot(o: *Object, inst: Air.Inst.Index) !CValue {
if (o.liveness.isUnused(inst))
return CValue.none;

const bin_op = o.air.instructions.items(.data)[inst].bin_op;
const lhs = try o.resolveInst(bin_op.lhs);
const rhs = try o.resolveInst(bin_op.rhs);
const ty_op = o.air.instructions.items(.data)[inst].ty_op;
const op = try o.resolveInst(ty_op.operand);

const writer = o.writer();
const inst_ty = o.air.typeOfIndex(inst);
const local = try o.allocLocal(inst_ty, .Const);

try writer.writeAll(" = ");
try o.writeCValue(writer, lhs);
try writer.print("{s}", .{operator});
try o.writeCValue(writer, rhs);
if (inst_ty.zigTypeTag() == .Bool)
try writer.writeAll("!")
else
try writer.writeAll("~");
try o.writeCValue(writer, op);
try writer.writeAll(";\n");

return local;
}

fn airUnOp(o: *Object, inst: Air.Inst.Index, operator: []const u8) !CValue {
fn airBinOp(o: *Object, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue {
if (o.liveness.isUnused(inst))
return CValue.none;

const un_op = o.air.instructions.items(.data)[inst].un_op;
const operand = try o.resolveInst(un_op);
const bin_op = o.air.instructions.items(.data)[inst].bin_op;
const lhs = try o.resolveInst(bin_op.lhs);
const rhs = try o.resolveInst(bin_op.rhs);

const writer = o.writer();
const inst_ty = o.air.typeOfIndex(inst);
const local = try o.allocLocal(inst_ty, .Const);

try writer.print(" = {s}", .{operator});
try o.writeCValue(writer, operand);
try writer.writeAll(" = ");
try o.writeCValue(writer, lhs);
try writer.print("{s}", .{operator});
try o.writeCValue(writer, rhs);
try writer.writeAll(";\n");

return local;
Expand Down

0 comments on commit 414b144

Please sign in to comment.