diff options
| author | Daniele Cocca <daniele.cocca@gmail.com> | 2022-03-13 00:43:51 +0000 |
|---|---|---|
| committer | Daniele Cocca <daniele.cocca@gmail.com> | 2022-03-13 09:59:15 +0000 |
| commit | d0277a3d17f738036a951a44f367839597dcaee4 (patch) | |
| tree | 5415e8fcfc445b567fb2c0ba2e2f1b187f46d021 /src/codegen | |
| parent | 87744a7ea9a2449764a110da4210d7750e3938ee (diff) | |
| download | zig-d0277a3d17f738036a951a44f367839597dcaee4.tar.gz zig-d0277a3d17f738036a951a44f367839597dcaee4.zip | |
CBE: implement popCount, byteSwap, bitReverse for ints <= 128 bits
This folds the airCountZeroes() code from
226fcd7c709ec664c5d883042cf7beb3026f66cb back into airBuiltinCall(),
since most of these builtins happen to require the same arguments and
can be unified under a common function signature.
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/c.zig | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 39b84dde3c..cee42b0603 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1711,8 +1711,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 airCountZeroes(f, inst, "clz"), - .ctz => try airCountZeroes(f, inst, "ctz"), + .clz => try airBuiltinCall(f, inst, "clz"), + .ctz => try airBuiltinCall(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"), @@ -3354,35 +3354,22 @@ fn airBuiltinCall(f: *Function, inst: Air.Inst.Index, fn_name: [*:0]const u8) !C const inst_ty = f.air.typeOfIndex(inst); const local = try f.allocLocal(inst_ty, .Const); - const ty_op = f.air.instructions.items(.data)[inst].ty_op; - const writer = f.object.writer(); - const operand = try f.resolveInst(ty_op.operand); - - // TODO implement the function in zig.h and call it here - - try writer.print(" = {s}(", .{fn_name}); - try f.writeCValue(writer, operand); - try writer.writeAll(");\n"); - 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 + const int_info = operand_ty.intInfo(target); + _ = toCIntBits(int_info.bits) orelse return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{}); + const signed_type = switch (int_info.signedness) { + .signed => "true", + .unsigned => "false", + }; try writer.print(" = zig_{s}(", .{fn_name}); try f.writeCValue(writer, try f.resolveInst(operand)); - try writer.print(", {d});\n", .{zig_bits}); + try writer.print(", {d}, {s});\n", .{ int_info.bits, signed_type }); return local; } |
