aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-03-11 20:15:13 -0500
committerGitHub <noreply@github.com>2022-03-11 20:15:13 -0500
commit6f986298c6cc58d2d86e9790b11bad5ce36085ad (patch)
tree0db86846775295f41d1159b9b19a2649cef82565 /src/codegen
parentfca51c81bc5fd83a3d4a87092696df80790003e9 (diff)
parent226fcd7c709ec664c5d883042cf7beb3026f66cb (diff)
downloadzig-6f986298c6cc58d2d86e9790b11bad5ce36085ad.tar.gz
zig-6f986298c6cc58d2d86e9790b11bad5ce36085ad.zip
Merge pull request #11125 from jmc-88/cbe
CBE: promote an already passing test, and add implementation for clz, ctz for integers
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index c179b65046..f1da1fdc0c 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -1708,8 +1708,8 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
.memcpy => try airMemcpy(f, inst),
.set_union_tag => try airSetUnionTag(f, inst),
.get_union_tag => try airGetUnionTag(f, inst),
- .clz => try airBuiltinCall(f, inst, "clz"),
- .ctz => try airBuiltinCall(f, inst, "ctz"),
+ .clz => try airCountZeroes(f, inst, "clz"),
+ .ctz => try airCountZeroes(f, inst, "ctz"),
.popcount => try airBuiltinCall(f, inst, "popcount"),
.byte_swap => try airBuiltinCall(f, inst, "byte_swap"),
.bit_reverse => try airBuiltinCall(f, inst, "bit_reverse"),
@@ -3349,6 +3349,26 @@ fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !C
return local;
}
+fn airCountZeroes(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !CValue {
+ if (f.liveness.isUnused(inst)) return CValue.none;
+
+ const inst_ty = f.air.typeOfIndex(inst);
+ const local = try f.allocLocal(inst_ty, .Const);
+ const operand = f.air.instructions.items(.data)[inst].ty_op.operand;
+ const operand_ty = f.air.typeOf(operand);
+ const target = f.object.dg.module.getTarget();
+ const writer = f.object.writer();
+
+ const zig_bits = operand_ty.intInfo(target).bits;
+ _ = toCIntBits(zig_bits) orelse
+ return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
+
+ try writer.print(" = zig_{s}(", .{fn_name});
+ try f.writeCValue(writer, try f.resolveInst(operand));
+ try writer.print(", {d});\n", .{zig_bits});
+ return local;
+}
+
fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue {
const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data;