Commit 414b144257

Jacob G-W <jacoblevgw@gmail.com>
2021-07-18 03:16:41
cbe: fix not (it is a ty_op, not un_op)
1 parent 4a0f38b
Changed files (1)
src
codegen
src/codegen/c.zig
@@ -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),
@@ -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;