Skip to content

Commit

Permalink
fix airSatOp lost changes
Browse files Browse the repository at this point in the history
  • Loading branch information
travisstaloch committed Sep 29, 2021
1 parent 6785552 commit 9761f93
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/codegen/c.zig
Expand Up @@ -885,17 +885,15 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
// that wrapping is UB.
.add, .ptr_add => try airBinOp( f, inst, " + "),
.addwrap => try airWrapOp(f, inst, " + ", "addw_"),
.addsat => return f.fail("TODO: C backend: implement codegen for addsat", .{}),

// TODO use a different strategy for sub that communicates to the optimizer
// that wrapping is UB.
.sub, .ptr_sub => try airBinOp( f, inst, " - "),
.subwrap => try airWrapOp(f, inst, " - ", "subw_"),
.subsat => return f.fail("TODO: C backend: implement codegen for subsat", .{}),
// TODO use a different strategy for mul that communicates to the optimizer
// that wrapping is UB.
.mul => try airBinOp( f, inst, " * "),
.mulwrap => try airWrapOp(f, inst, " * ", "mulw_"),
.mulsat => return f.fail("TODO: C backend: implement codegen for mulsat", .{}),
// TODO use a different strategy for div that communicates to the optimizer
// that wrapping is UB.
.div => try airBinOp( f, inst, " / "),
Expand All @@ -917,7 +915,11 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO

.shr => try airBinOp(f, inst, " >> "),
.shl => try airBinOp(f, inst, " << "),
.shl_sat => return f.fail("TODO: C backend: implement codegen for mulsat", .{}),

.addsat=> try airSatOp(f, inst, "adds_"),
.subsat=> try airSatOp(f, inst, "subs_"),
.mulsat=> try airSatOp(f, inst, "muls_"),
.shl_sat=> try airSatOp(f, inst, "shls_"),


.not => try airNot( f, inst),
Expand Down Expand Up @@ -1313,26 +1315,27 @@ fn airWrapOp(
}

fn airSatOp(
o: *Object,
f: *Function,
inst: Air.Inst.Index,
fn_op: [*:0]const u8,
) !CValue {
if (o.liveness.isUnused(inst))
if (f.liveness.isUnused(inst))
return CValue.none;

const bin_op = o.air.instructions.items(.data)[inst].bin_op;
const inst_ty = o.air.typeOfIndex(inst);
const int_info = inst_ty.intInfo(o.dg.module.getTarget());
const bin_op = f.air.instructions.items(.data)[inst].bin_op;
const inst_ty = f.air.typeOfIndex(inst);

const int_info = inst_ty.intInfo(f.object.dg.module.getTarget());
const bits = int_info.bits;

switch (bits) {
8, 16, 32, 64, 128 => {},
else => return o.dg.fail("TODO: C backend: airSatOp for non power of 2 integers", .{}),
else => return f.object.dg.fail("TODO: C backend: airSatOp for non power of 2 integers", .{}),
}

// if it's an unsigned int with non-arbitrary bit size then we can just add
if (bits > 64) {
return o.dg.fail("TODO: C backend: airSatOp for large integers", .{});
return f.object.dg.fail("TODO: C backend: airSatOp for large integers", .{});
}

var min_buf: [80]u8 = undefined;
Expand Down Expand Up @@ -1380,11 +1383,11 @@ fn airSatOp(
},
};

const lhs = try o.resolveInst(bin_op.lhs);
const rhs = try o.resolveInst(bin_op.rhs);
const w = o.writer();
const lhs = try f.resolveInst(bin_op.lhs);
const rhs = try f.resolveInst(bin_op.rhs);
const w = f.object.writer();

const ret = try o.allocLocal(inst_ty, .Mut);
const ret = try f.allocLocal(inst_ty, .Mut);
try w.print(" = zig_{s}", .{fn_op});

switch (inst_ty.tag()) {
Expand All @@ -1410,16 +1413,16 @@ fn airSatOp(
}

try w.writeByte('(');
try o.writeCValue(w, lhs);
try f.writeCValue(w, lhs);
try w.writeAll(", ");
try o.writeCValue(w, rhs);
try f.writeCValue(w, rhs);

if (int_info.signedness == .signed) {
try w.print(", {s}", .{min});
}

try w.print(", {s});", .{max});
try o.indent_writer.insertNewline();
try f.object.indent_writer.insertNewline();

return ret;
}
Expand Down

0 comments on commit 9761f93

Please sign in to comment.