From 9bf1681990fe87a6b2e5fc644a89f1aece304579 Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 18:28:44 -0800 Subject: C backend: basic big ints, fix airPtrToInt, array references, pointer arithmetic UB with NULL, implement airPtrElemPtr/Val, fix redundant indirection/references with arrays -add additional test cases that were found to be passing -add basic int128 test cases which previously did not pass but weren't covered -most test cases in cast.zig now pass -i128/u128 or smaller int constants can now be rendered -unsigned int constants are now always suffixed with 'u' to prevent random compile errors -pointers with a val tag of 'zero' now just emit a 0 constant which coerces to the pointer type and fixes some warnings with ordered comparisons -pointers with a val tag of 'one' are now casted back to the pointer type -support pointers with a u64 val -fix bug where rendering an array's type will emit more indirection than is needed -render uint128_t/int128_t manually when needed -implement ptr_add/sub AIR handlers manually so they manually cast to int types which avoids UB if the result or ptr operand is NULL -implement airPtrElemVal/Ptr -airAlloc for arrays will not allocate a ref as the local for the array is already a reference/pointer to the array itself -fix airPtrToInt by casting to the int type --- src/codegen/c.zig | 166 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 138 insertions(+), 28 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index e95a5a77ec..44271d0657 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -226,6 +226,36 @@ pub const DeclGen = struct { try dg.renderDeclName(decl, writer); } + /// Assumes that int_val is an int greater than maxInt(u64) and has > 64 and <= 128 bits. + fn renderBigInt( + writer: anytype, + int_val: anytype, + ) error{ OutOfMemory, AnalysisFail }!void { + const int_info = @typeInfo(@TypeOf(int_val)).Int; + const is_signed = int_info.signedness == .signed; + const is_neg = int_val < 0; + comptime assert(int_info.bits > 64 and int_info.bits <= 128); + + // Clang and GCC don't support 128-bit integer constants but will hopefully unfold them + // if we construct one manually. + const magnitude = std.math.absCast(int_val); + + const high = @truncate(u64, magnitude >> 64); + const low = @truncate(u64, magnitude); + + // (int128_t)/<->( ( (uint128_t)( val_high << 64 )u ) + (uint128_t)val_low/u ) + if (is_signed) try writer.writeAll("(int128_t)"); + if (is_neg) try writer.writeByte('-'); + + assert(high > 0); + try writer.print("(((uint128_t)0x{x}u<<64)", .{ high }); + + if (low > 0) + try writer.print("+(uint128_t)0x{x}u", .{ low }); + + return writer.writeByte(')'); + } + fn renderValue( dg: *DeclGen, writer: anytype, @@ -240,18 +270,18 @@ pub const DeclGen = struct { const c_bits = toCIntBits(ty.intInfo(dg.module.getTarget()).bits) orelse return dg.fail("TODO: C backend: implement integer types larger than 128 bits", .{}); switch (c_bits) { - 8 => return writer.writeAll("0xaaU"), - 16 => return writer.writeAll("0xaaaaU"), - 32 => return writer.writeAll("0xaaaaaaaaU"), - 64 => return writer.writeAll("0xaaaaaaaaaaaaaaaaUL"), - 128 => return writer.writeAll("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaULL"), + 8 => return writer.writeAll("0xaau"), + 16 => return writer.writeAll("0xaaaau"), + 32 => return writer.writeAll("0xaaaaaaaau"), + 64 => return writer.writeAll("0xaaaaaaaaaaaaaaaau"), + 128 => return renderBigInt(writer, @as(u128, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)), else => unreachable, } }, .Float => { switch (ty.floatBits(dg.module.getTarget())) { - 32 => return writer.writeAll("zig_bitcast_f32_u32(0xaaaaaaaa)"), - 64 => return writer.writeAll("zig_bitcast_f64_u64(0xaaaaaaaaaaaaaaaa)"), + 32 => return writer.writeAll("zig_bitcast_f32_u32(0xaaaaaaaau)"), + 64 => return writer.writeAll("zig_bitcast_f64_u64(0xaaaaaaaaaaaaaaaau)"), else => return dg.fail("TODO float types > 64 bits are not support in renderValue() as of now", .{}), } }, @@ -265,10 +295,18 @@ pub const DeclGen = struct { } } switch (ty.zigTypeTag()) { - .Int => { - if (ty.isSignedInt()) - return writer.print("{d}", .{val.toSignedInt()}); - return writer.print("{d}", .{val.toUnsignedInt()}); + .Int => switch (val.tag()) { + .int_big_positive => try renderBigInt(writer, val.castTag(.int_big_positive).?.asBigInt().to(u128) catch { + return dg.fail("TODO implement integer constants larger than 128 bits", .{}); + }), + .int_big_negative => try renderBigInt(writer, val.castTag(.int_big_negative).?.asBigInt().to(i128) catch { + return dg.fail("TODO implement integer constants larger than 128 bits", .{}); + }), + else => { + if (ty.isSignedInt()) + return writer.print("{d}", .{val.toSignedInt()}); + return writer.print("{d}u", .{val.toUnsignedInt()}); + } }, .Float => { if (ty.floatBits(dg.module.getTarget()) <= 64) { @@ -286,8 +324,17 @@ pub const DeclGen = struct { return dg.fail("TODO: C backend: implement lowering large float values", .{}); }, .Pointer => switch (val.tag()) { - .null_value, .zero => try writer.writeAll("NULL"), - .one => try writer.writeAll("1"), + .null_value => try writer.writeAll("NULL"), + // Technically this should produce NULL but the integer literal 0 will always coerce + // to the assigned pointer type. Note this is just a hack to fix warnings from ordered comparisons (<, >, etc) + // between pointers and 0, which is an extension to begin with. + .zero => try writer.writeByte('0'), + .one => { + // int constants like 1 will not cast to the pointer however. + try writer.writeAll("(("); + try dg.renderType(writer, ty); + return writer.writeAll(")1)"); + }, .decl_ref => { const decl = val.castTag(.decl_ref).?.data; return dg.renderDeclValue(writer, ty, val, decl); @@ -316,6 +363,11 @@ pub const DeclGen = struct { const decl = val.castTag(.extern_fn).?.data; try dg.renderDeclName(decl, writer); }, + .int_u64 => { + try writer.writeAll("(("); + try dg.renderType(writer, ty); + try writer.print(")0x{x}u)", .{val.toUnsignedInt()}); + }, else => unreachable, }, .Array => { @@ -728,6 +780,8 @@ pub const DeclGen = struct { .i32 => try w.writeAll("int32_t"), .u64 => try w.writeAll("uint64_t"), .i64 => try w.writeAll("int64_t"), + .u128 => try w.writeAll("uint128_t"), + .i128 => try w.writeAll("int128_t"), .usize => try w.writeAll("uintptr_t"), .isize => try w.writeAll("intptr_t"), .c_short => try w.writeAll("short"), @@ -787,8 +841,9 @@ pub const DeclGen = struct { }, .Array => { // We are referencing the array so it will decay to a C pointer. - try dg.renderType(w, t.elemType()); - return w.writeAll(" *"); + // NB: arrays are not really types in C so they are either specified in the declaration + // or are already pointed to; our only job is to render the element's type. + return dg.renderType(w, t.elemType()); }, .Optional => { var opt_buf: Type.Payload.ElemType = undefined; @@ -1068,12 +1123,15 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .unreach => try airUnreach(f), .fence => try airFence(f, inst), + .ptr_add => try airPtrAddSub (f, inst, " + "), + .ptr_sub => try airPtrAddSub (f, inst, " - "), + // TODO use a different strategy for add that communicates to the optimizer // that wrapping is UB. - .add, .ptr_add => try airBinOp (f, inst, " + "), + .add => try airBinOp (f, inst, " + "), // TODO use a different strategy for sub that communicates to the optimizer // that wrapping is UB. - .sub, .ptr_sub => try airBinOp (f, inst, " - "), + .sub => try airBinOp (f, inst, " - "), // TODO use a different strategy for mul that communicates to the optimizer // that wrapping is UB. .mul => try airBinOp (f, inst, " * "), @@ -1187,7 +1245,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .ptr_slice_len_ptr => try airPtrSliceFieldPtr(f, inst, ".len;\n"), .ptr_slice_ptr_ptr => try airPtrSliceFieldPtr(f, inst, ".ptr;\n"), - .ptr_elem_val => try airPtrElemVal(f, inst, "["), + .ptr_elem_val => try airPtrElemVal(f, inst), .ptr_elem_ptr => try airPtrElemPtr(f, inst), .slice_elem_val => try airSliceElemVal(f, inst), .slice_elem_ptr => try airSliceElemPtr(f, inst), @@ -1240,20 +1298,39 @@ fn airPtrSliceFieldPtr(f: *Function, inst: Air.Inst.Index, suffix: []const u8) ! return f.fail("TODO: C backend: airPtrSliceFieldPtr", .{}); } -fn airPtrElemVal(f: *Function, inst: Air.Inst.Index, prefix: []const u8) !CValue { - const is_volatile = false; // TODO - if (!is_volatile and f.liveness.isUnused(inst)) - return CValue.none; +fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { + const bin_op = f.air.instructions.items(.data)[inst].bin_op; + const slice_ty = f.air.typeOf(bin_op.lhs); + if (!slice_ty.isVolatilePtr() and f.liveness.isUnused(inst)) return CValue.none; - _ = prefix; - return f.fail("TODO: C backend: airPtrElemVal", .{}); + const arr = try f.resolveInst(bin_op.lhs); + const index = try f.resolveInst(bin_op.rhs); + const writer = f.object.writer(); + const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); + try writer.writeAll(" = "); + try f.writeCValue(writer, arr); + try writer.writeByte('['); + try f.writeCValue(writer, index); + try writer.writeAll("];\n"); + return local; } fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { - if (f.liveness.isUnused(inst)) - return CValue.none; + if (f.liveness.isUnused(inst)) return CValue.none; - return f.fail("TODO: C backend: airPtrElemPtr", .{}); + const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; + const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; + + const arr = try f.resolveInst(bin_op.lhs); + const index = try f.resolveInst(bin_op.rhs); + const writer = f.object.writer(); + const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); + try writer.writeAll(" = &"); + try f.writeCValue(writer, arr); + try writer.writeByte('['); + try f.writeCValue(writer, index); + try writer.writeAll("];\n"); + return local; } fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { @@ -1317,6 +1394,10 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { const local = try f.allocLocal(elem_type, mutability); try writer.writeAll(";\n"); + // Arrays are already pointers so they don't need to be referenced. + if (elem_type.zigTypeTag() == .Array) + return CValue{ .local = local.local }; + return CValue{ .local_ref = local.local }; } @@ -1810,6 +1891,33 @@ fn airBinOp(f: *Function, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue return local; } +fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue { + if (f.liveness.isUnused(inst)) + return CValue.none; + + const bin_op = f.air.instructions.items(.data)[inst].bin_op; + const lhs = try f.resolveInst(bin_op.lhs); + const rhs = try f.resolveInst(bin_op.rhs); + + const writer = f.object.writer(); + const inst_ty = f.air.typeOfIndex(inst); + const local = try f.allocLocal(inst_ty, .Const); + + // We must convert to and from integer types to prevent UB if the operation results in a NULL pointer, + // or if LHS is NULL. The operation is only UB if the result is NULL and then dereferenced. + try writer.writeAll(" = ("); + try f.renderType(writer, inst_ty); + try writer.writeAll(")(((uintptr_t)"); + try f.writeCValue(writer, lhs); + try writer.print("){s}(", .{operator}); + try f.writeCValue(writer, rhs); + try writer.writeAll("*sizeof("); + try f.renderType(writer, inst_ty.childType()); + try writer.print(")));\n", .{}); + + return local; +} + fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue { if (f.liveness.isUnused(inst)) return CValue.none; @@ -2529,7 +2637,9 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue { const writer = f.object.writer(); const operand = try f.resolveInst(un_op); - try writer.writeAll(" = "); + try writer.writeAll(" = ("); + try f.renderType(writer, inst_ty); + try writer.writeAll(")"); try f.writeCValue(writer, operand); try writer.writeAll(";\n"); return local; -- cgit v1.2.3 From 34684725aa728e392368a5743e1a2405328d5788 Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 18:31:31 -0800 Subject: fmt --- src/codegen/c.zig | 6 +++--- test/behavior/int128.zig | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 44271d0657..7dabf3bf9e 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -248,10 +248,10 @@ pub const DeclGen = struct { if (is_neg) try writer.writeByte('-'); assert(high > 0); - try writer.print("(((uint128_t)0x{x}u<<64)", .{ high }); + try writer.print("(((uint128_t)0x{x}u<<64)", .{high}); if (low > 0) - try writer.print("+(uint128_t)0x{x}u", .{ low }); + try writer.print("+(uint128_t)0x{x}u", .{low}); return writer.writeByte(')'); } @@ -306,7 +306,7 @@ pub const DeclGen = struct { if (ty.isSignedInt()) return writer.print("{d}", .{val.toSignedInt()}); return writer.print("{d}u", .{val.toUnsignedInt()}); - } + }, }, .Float => { if (ty.floatBits(dg.module.getTarget()) <= 64) { diff --git a/test/behavior/int128.zig b/test/behavior/int128.zig index 444096ff18..76b12ef1d9 100644 --- a/test/behavior/int128.zig +++ b/test/behavior/int128.zig @@ -40,4 +40,4 @@ test "int128" { test "truncate int128" { var buff: u128 = maxInt(u128); try expect(@truncate(u64, buff) == maxInt(u64)); -} \ No newline at end of file +} -- cgit v1.2.3 From ad4627ea3b645e4dd51397fe4a2d92878c2bd9bf Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 19:08:14 -0800 Subject: small changes + align tests obviously shouldn't have passed --- src/codegen/c.zig | 2 +- test/behavior.zig | 2 +- test/behavior/int128.zig | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 7dabf3bf9e..554a60ad65 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -842,7 +842,7 @@ pub const DeclGen = struct { .Array => { // We are referencing the array so it will decay to a C pointer. // NB: arrays are not really types in C so they are either specified in the declaration - // or are already pointed to; our only job is to render the element's type. + // or are already pointed to; our only job is to render the element type. return dg.renderType(w, t.elemType()); }, .Optional => { diff --git a/test/behavior.zig b/test/behavior.zig index 0abc15035b..739aeabb94 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -2,7 +2,6 @@ const builtin = @import("builtin"); test { // Tests that pass for stage1, stage2, and the C backend. - _ = @import("behavior/align.zig"); _ = @import("behavior/basic.zig"); _ = @import("behavior/bitcast.zig"); _ = @import("behavior/bool.zig"); @@ -42,6 +41,7 @@ test { if (builtin.object_format != .c) { // Tests that pass for stage1 and stage2 but not the C backend. + _ = @import("behavior/align.zig"); _ = @import("behavior/array.zig"); _ = @import("behavior/atomics.zig"); _ = @import("behavior/basic_llvm.zig"); diff --git a/test/behavior/int128.zig b/test/behavior/int128.zig index 76b12ef1d9..4b0232feb8 100644 --- a/test/behavior/int128.zig +++ b/test/behavior/int128.zig @@ -32,9 +32,6 @@ test "int128" { buff = minInt(i128); try expect(buff < 0); - - // This should be uncommented once wrapping arithmetic is implemented for 128 bit ints: - // try expect(buff < 0 and (buff -% 1) > 0) } test "truncate int128" { -- cgit v1.2.3 From dffa6dcaf93621ab8bbfdb8effad8bde6d73c20a Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 19:26:12 -0800 Subject: make it more clear we should do UB wrapping optimizations for ptr arithmetic --- src/codegen/c.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 554a60ad65..5fdc2b1c2a 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1123,15 +1123,14 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .unreach => try airUnreach(f), .fence => try airFence(f, inst), - .ptr_add => try airPtrAddSub (f, inst, " + "), - .ptr_sub => try airPtrAddSub (f, inst, " - "), - // TODO use a different strategy for add that communicates to the optimizer // that wrapping is UB. .add => try airBinOp (f, inst, " + "), + .ptr_add => try airPtrAddSub (f, inst, " + "), // TODO use a different strategy for sub that communicates to the optimizer // that wrapping is UB. .sub => try airBinOp (f, inst, " - "), + .ptr_sub => try airPtrAddSub (f, inst, " - "), // TODO use a different strategy for mul that communicates to the optimizer // that wrapping is UB. .mul => try airBinOp (f, inst, " * "), -- cgit v1.2.3 From 0300ec4ef70f524718d01f0f351d41349465387c Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 19:51:36 -0800 Subject: fix assumption where all positive big ints are unsigned --- src/codegen/c.zig | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 5fdc2b1c2a..a82fb42625 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -19,6 +19,7 @@ const Zir = @import("../Zir.zig"); const Liveness = @import("../Liveness.zig"); const Mutability = enum { Const, Mut }; +const BigIntConst = std.math.big.int.Const; pub const CValue = union(enum) { none: void, @@ -226,8 +227,7 @@ pub const DeclGen = struct { try dg.renderDeclName(decl, writer); } - /// Assumes that int_val is an int greater than maxInt(u64) and has > 64 and <= 128 bits. - fn renderBigInt( + fn renderInt128( writer: anytype, int_val: anytype, ) error{ OutOfMemory, AnalysisFail }!void { @@ -256,6 +256,23 @@ pub const DeclGen = struct { return writer.writeByte(')'); } + fn renderBigIntConst( + dg: *DeclGen, + writer: anytype, + val: BigIntConst, + signed: bool, + ) error{ OutOfMemory, AnalysisFail }!void { + if (signed) { + try renderInt128(writer, val.to(i128) catch { + return dg.fail("TODO implement integer constants larger than 128 bits", .{}); + }); + } else { + try renderInt128(writer, val.to(u128) catch { + return dg.fail("TODO implement integer constants larger than 128 bits", .{}); + }); + } + } + fn renderValue( dg: *DeclGen, writer: anytype, @@ -274,7 +291,7 @@ pub const DeclGen = struct { 16 => return writer.writeAll("0xaaaau"), 32 => return writer.writeAll("0xaaaaaaaau"), 64 => return writer.writeAll("0xaaaaaaaaaaaaaaaau"), - 128 => return renderBigInt(writer, @as(u128, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)), + 128 => return renderInt128(writer, @as(u128, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)), else => unreachable, } }, @@ -296,12 +313,8 @@ pub const DeclGen = struct { } switch (ty.zigTypeTag()) { .Int => switch (val.tag()) { - .int_big_positive => try renderBigInt(writer, val.castTag(.int_big_positive).?.asBigInt().to(u128) catch { - return dg.fail("TODO implement integer constants larger than 128 bits", .{}); - }), - .int_big_negative => try renderBigInt(writer, val.castTag(.int_big_negative).?.asBigInt().to(i128) catch { - return dg.fail("TODO implement integer constants larger than 128 bits", .{}); - }), + .int_big_positive => try dg.renderBigIntConst(writer, val.castTag(.int_big_positive).?.asBigInt(), ty.isSignedInt()), + .int_big_negative => try dg.renderBigIntConst(writer, val.castTag(.int_big_negative).?.asBigInt(), true), else => { if (ty.isSignedInt()) return writer.print("{d}", .{val.toSignedInt()}); -- cgit v1.2.3 From 3896de307888d598cca1baba8e559debc81d2080 Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 22:06:12 -0800 Subject: simplify things --- src/codegen/c.zig | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index a82fb42625..49cf48cfb0 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -342,12 +342,6 @@ pub const DeclGen = struct { // to the assigned pointer type. Note this is just a hack to fix warnings from ordered comparisons (<, >, etc) // between pointers and 0, which is an extension to begin with. .zero => try writer.writeByte('0'), - .one => { - // int constants like 1 will not cast to the pointer however. - try writer.writeAll("(("); - try dg.renderType(writer, ty); - return writer.writeAll(")1)"); - }, .decl_ref => { const decl = val.castTag(.decl_ref).?.data; return dg.renderDeclValue(writer, ty, val, decl); @@ -376,7 +370,7 @@ pub const DeclGen = struct { const decl = val.castTag(.extern_fn).?.data; try dg.renderDeclName(decl, writer); }, - .int_u64 => { + .int_u64, .one => { try writer.writeAll("(("); try dg.renderType(writer, ty); try writer.print(")0x{x}u)", .{val.toUnsignedInt()}); -- cgit v1.2.3 From cf99afc52568ab121db2db8aa3ba94b97109f396 Mon Sep 17 00:00:00 2001 From: drew Date: Sun, 14 Nov 2021 23:27:13 -0800 Subject: add generics behavior test -airLoad and airStore now properly report an error if they are used with an array, instead of having the C compiler emit a vague error -airStoreUndefined now works with array types -structFieldPtr now works with array types, allowing generics' tests to pass --- src/codegen/c.zig | 21 ++++++++++++++++----- test/behavior.zig | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 49cf48cfb0..1dfcab5d61 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1431,6 +1431,8 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { if (!is_volatile and f.liveness.isUnused(inst)) return CValue.none; const inst_ty = f.air.typeOfIndex(inst); + if (inst_ty.zigTypeTag() == .Array) + return f.fail("TODO: C backend: implement airLoad for arrays", .{}); const operand = try f.resolveInst(ty_op.operand); const writer = f.object.writer(); const local = try f.allocLocal(inst_ty, .Const); @@ -1557,7 +1559,7 @@ fn airBoolToInt(f: *Function, inst: Air.Inst.Index) !CValue { return local; } -fn airStoreUndefined(f: *Function, dest_ptr: CValue) !CValue { +fn airStoreUndefined(f: *Function, dest_ptr: CValue, dest_type: Type) !CValue { const is_debug_build = f.object.dg.module.optimizeMode() == .Debug; if (!is_debug_build) return CValue.none; @@ -1581,9 +1583,11 @@ fn airStoreUndefined(f: *Function, dest_ptr: CValue) !CValue { try writer.writeAll("));\n"); }, else => { + const indirection = if (dest_type.zigTypeTag() == .Array) "" else "*"; + try writer.writeAll("memset("); try f.writeCValue(writer, dest_ptr); - try writer.writeAll(", 0xaa, sizeof(*"); + try writer.print(", 0xaa, sizeof({s}", .{indirection}); try f.writeCValue(writer, dest_ptr); try writer.writeAll("));\n"); }, @@ -1596,11 +1600,16 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { const bin_op = f.air.instructions.items(.data)[inst].bin_op; const dest_ptr = try f.resolveInst(bin_op.lhs); const src_val = try f.resolveInst(bin_op.rhs); + const lhs_type = f.air.typeOf(bin_op.lhs); const src_val_is_undefined = if (f.air.value(bin_op.rhs)) |v| v.isUndef() else false; if (src_val_is_undefined) - return try airStoreUndefined(f, dest_ptr); + return try airStoreUndefined(f, dest_ptr, lhs_type); + + // Don't check this for airStoreUndefined as that will work for arrays already + if (lhs_type.zigTypeTag() == .Array) + return f.fail("TODO: C backend: implement airStore for arrays", .{}); const writer = f.object.writer(); switch (dest_ptr) { @@ -2420,15 +2429,17 @@ fn structFieldPtr(f: *Function, inst: Air.Inst.Index, struct_ptr_ty: Type, struc const writer = f.object.writer(); const struct_obj = struct_ptr_ty.elemType().castTag(.@"struct").?.data; const field_name = struct_obj.fields.keys()[index]; + const field_val = struct_obj.fields.values()[index]; + const addrof = if (field_val.ty.zigTypeTag() == .Array) "" else "&"; const inst_ty = f.air.typeOfIndex(inst); const local = try f.allocLocal(inst_ty, .Const); switch (struct_ptr) { .local_ref => |i| { - try writer.print(" = &t{d}.{};\n", .{ i, fmtIdent(field_name) }); + try writer.print(" = {s}t{d}.{};\n", .{ addrof, i, fmtIdent(field_name) }); }, else => { - try writer.writeAll(" = &"); + try writer.print(" = {s}", .{addrof}); try f.writeCValue(writer, struct_ptr); try writer.print("->{};\n", .{fmtIdent(field_name)}); }, diff --git a/test/behavior.zig b/test/behavior.zig index 739aeabb94..db6002e3f2 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -38,6 +38,7 @@ test { _ = @import("behavior/this.zig"); _ = @import("behavior/member_func.zig"); _ = @import("behavior/translate_c_macros.zig"); + _ = @import("behavior/generics.zig"); if (builtin.object_format != .c) { // Tests that pass for stage1 and stage2 but not the C backend. @@ -57,7 +58,6 @@ test { _ = @import("behavior/floatop.zig"); _ = @import("behavior/fn.zig"); _ = @import("behavior/for.zig"); - _ = @import("behavior/generics.zig"); _ = @import("behavior/math.zig"); _ = @import("behavior/maximum_minimum.zig"); _ = @import("behavior/null_llvm.zig"); -- cgit v1.2.3 From f33af8f071a1782d222067e48b68eaed5d1a05a8 Mon Sep 17 00:00:00 2001 From: drew Date: Mon, 15 Nov 2021 00:08:57 -0800 Subject: fix array airStoreUndefined for arrays --- src/codegen/c.zig | 4 ++-- test/behavior/cast_c.zig | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 1dfcab5d61..92d973fb4f 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1583,7 +1583,7 @@ fn airStoreUndefined(f: *Function, dest_ptr: CValue, dest_type: Type) !CValue { try writer.writeAll("));\n"); }, else => { - const indirection = if (dest_type.zigTypeTag() == .Array) "" else "*"; + const indirection = if (dest_type.childType().zigTypeTag() == .Array) "" else "*"; try writer.writeAll("memset("); try f.writeCValue(writer, dest_ptr); @@ -1608,7 +1608,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { return try airStoreUndefined(f, dest_ptr, lhs_type); // Don't check this for airStoreUndefined as that will work for arrays already - if (lhs_type.zigTypeTag() == .Array) + if (lhs_type.childType().zigTypeTag() == .Array) return f.fail("TODO: C backend: implement airStore for arrays", .{}); const writer = f.object.writer(); diff --git a/test/behavior/cast_c.zig b/test/behavior/cast_c.zig index e634103d42..0e74cd0b58 100644 --- a/test/behavior/cast_c.zig +++ b/test/behavior/cast_c.zig @@ -247,3 +247,14 @@ test "*const ?[*]const T to [*c]const [*c]const T" { try expect(b.*[0] == 'o'); try expect(b[0][1] == 'k'); } + +test "array coersion to undefined at runtime" { + @setRuntimeSafety(true); + + var array = [4]u8{ 3, 4, 5, 6 }; + var undefined_val = [4]u8{ 0xAA, 0xAA, 0xAA, 0xAA }; + + try expect(std.mem.eql(u8, &array, &array)); + array = undefined; + try expect(std.mem.eql(u8, &array, &undefined_val)); +} -- cgit v1.2.3 From a1d760416293b20b6a6a72f39a5957fbf22187cc Mon Sep 17 00:00:00 2001 From: drew Date: Mon, 15 Nov 2021 18:07:48 -0800 Subject: correct misnamed variables caused by copy-paste --- src/codegen/c.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 92d973fb4f..908b85e701 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1306,15 +1306,15 @@ fn airPtrSliceFieldPtr(f: *Function, inst: Air.Inst.Index, suffix: []const u8) ! fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { const bin_op = f.air.instructions.items(.data)[inst].bin_op; - const slice_ty = f.air.typeOf(bin_op.lhs); - if (!slice_ty.isVolatilePtr() and f.liveness.isUnused(inst)) return CValue.none; + const ptr_ty = f.air.typeOf(bin_op.lhs); + if (!ptr_ty.isVolatilePtr() and f.liveness.isUnused(inst)) return CValue.none; - const arr = try f.resolveInst(bin_op.lhs); + const ptr = try f.resolveInst(bin_op.lhs); const index = try f.resolveInst(bin_op.rhs); const writer = f.object.writer(); const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); try writer.writeAll(" = "); - try f.writeCValue(writer, arr); + try f.writeCValue(writer, ptr); try writer.writeByte('['); try f.writeCValue(writer, index); try writer.writeAll("];\n"); @@ -1327,12 +1327,12 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { const ty_pl = f.air.instructions.items(.data)[inst].ty_pl; const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; - const arr = try f.resolveInst(bin_op.lhs); + const ptr = try f.resolveInst(bin_op.lhs); const index = try f.resolveInst(bin_op.rhs); const writer = f.object.writer(); const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const); try writer.writeAll(" = &"); - try f.writeCValue(writer, arr); + try f.writeCValue(writer, ptr); try writer.writeByte('['); try f.writeCValue(writer, index); try writer.writeAll("];\n"); -- cgit v1.2.3 From 09588c795c08064971f61ee147d06972f0add94e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 16 Nov 2021 17:46:39 -0700 Subject: stage2: LLVM backend: memset to 0xaa for undefined stores Also support `one` and `int_big_positive` tags for const pointers. --- src/codegen/c.zig | 8 +++++--- src/codegen/llvm.zig | 30 ++++++++++++++++++++++++++---- src/value.zig | 7 +++++++ test/behavior/cast.zig | 9 +++++++-- test/behavior/int128.zig | 9 +++++++-- 5 files changed, 52 insertions(+), 11 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 908b85e701..3aef5a8f92 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -279,7 +279,7 @@ pub const DeclGen = struct { ty: Type, val: Value, ) error{ OutOfMemory, AnalysisFail }!void { - if (val.isUndef()) { + if (val.isUndefDeep()) { switch (ty.zigTypeTag()) { // Using '{}' for integer and floats seemed to error C compilers (both GCC and Clang) // with 'error: expected expression' (including when built with 'zig cc') @@ -1049,7 +1049,7 @@ pub fn genDecl(o: *Object) !void { } try fwd_decl_writer.writeAll(";\n"); - if (variable.init.isUndef()) { + if (variable.init.isUndefDeep()) { return; } @@ -1602,8 +1602,10 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { const src_val = try f.resolveInst(bin_op.rhs); const lhs_type = f.air.typeOf(bin_op.lhs); + // TODO Sema should emit a different instruction when the store should + // possibly do the safety 0xaa bytes for undefined. const src_val_is_undefined = - if (f.air.value(bin_op.rhs)) |v| v.isUndef() else false; + if (f.air.value(bin_op.rhs)) |v| v.isUndefDeep() else false; if (src_val_is_undefined) return try airStoreUndefined(f, dest_ptr, lhs_type); diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index d0f6d62ad7..306a3df83c 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1078,7 +1078,7 @@ pub const DeclGen = struct { }; return self.context.constStruct(&fields, fields.len, .False); }, - .int_u64 => { + .int_u64, .one, .int_big_positive => { const llvm_usize = try self.llvmType(Type.usize); const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(), .False); return llvm_int.constIntToPtr(try self.llvmType(tv.ty)); @@ -3464,8 +3464,30 @@ pub const FuncGen = struct { const bin_op = self.air.instructions.items(.data)[inst].bin_op; const dest_ptr = try self.resolveInst(bin_op.lhs); const ptr_ty = self.air.typeOf(bin_op.lhs); - const src_operand = try self.resolveInst(bin_op.rhs); - self.store(dest_ptr, ptr_ty, src_operand, .NotAtomic); + + // TODO Sema should emit a different instruction when the store should + // possibly do the safety 0xaa bytes for undefined. + const val_is_undef = if (self.air.value(bin_op.rhs)) |val| val.isUndefDeep() else false; + if (val_is_undef) { + const elem_ty = ptr_ty.childType(); + const target = self.dg.module.getTarget(); + const elem_size = elem_ty.abiSize(target); + const u8_llvm_ty = self.context.intType(8); + const ptr_u8_llvm_ty = u8_llvm_ty.pointerType(0); + const dest_ptr_u8 = self.builder.buildBitCast(dest_ptr, ptr_u8_llvm_ty, ""); + const fill_char = u8_llvm_ty.constInt(0xaa, .False); + const dest_ptr_align = ptr_ty.ptrAlignment(target); + const usize_llvm_ty = try self.dg.llvmType(Type.usize); + const len = usize_llvm_ty.constInt(elem_size, .False); + _ = self.builder.buildMemSet(dest_ptr_u8, fill_char, len, dest_ptr_align, ptr_ty.isVolatilePtr()); + if (self.dg.module.comp.bin_file.options.valgrind) { + // TODO generate valgrind client request to mark byte range as undefined + // see gen_valgrind_undef() in codegen.cpp + } + } else { + const src_operand = try self.resolveInst(bin_op.rhs); + self.store(dest_ptr, ptr_ty, src_operand, .NotAtomic); + } return null; } @@ -3651,7 +3673,7 @@ pub const FuncGen = struct { const dest_ptr = try self.resolveInst(pl_op.operand); const ptr_ty = self.air.typeOf(pl_op.operand); const value = try self.resolveInst(extra.lhs); - const val_is_undef = if (self.air.value(extra.lhs)) |val| val.isUndef() else false; + const val_is_undef = if (self.air.value(extra.lhs)) |val| val.isUndefDeep() else false; const len = try self.resolveInst(extra.rhs); const u8_llvm_ty = self.context.intType(8); const ptr_u8_llvm_ty = u8_llvm_ty.pointerType(0); diff --git a/src/value.zig b/src/value.zig index 4b571891f4..4eaa865149 100644 --- a/src/value.zig +++ b/src/value.zig @@ -1802,6 +1802,13 @@ pub const Value = extern union { return self.tag() == .undef; } + /// TODO: check for cases such as array that is not marked undef but all the element + /// values are marked undef, or struct that is not marked undef but all fields are marked + /// undef, etc. + pub fn isUndefDeep(self: Value) bool { + return self.isUndef(); + } + /// Asserts the value is not undefined and not unreachable. /// Integer value 0 is considered null because of C pointers. pub fn isNull(self: Value) bool { diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 5350b09534..607df6a8e8 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -251,8 +251,13 @@ test "*const ?[*]const T to [*c]const [*c]const T" { test "array coersion to undefined at runtime" { @setRuntimeSafety(true); - // setRuntimeSafety isn't recognized on stage2 - if (@import("builtin").zig_is_stage2 and @import("builtin").mode != .Debug and @import("builtin").mode != .ReleaseSafe) return error.SkipZigTest; + // TODO implement @setRuntimeSafety in stage2 + if (@import("builtin").zig_is_stage2 and + @import("builtin").mode != .Debug and + @import("builtin").mode != .ReleaseSafe) + { + return error.SkipZigTest; + } var array = [4]u8{ 3, 4, 5, 6 }; var undefined_val = [4]u8{ 0xAA, 0xAA, 0xAA, 0xAA }; diff --git a/test/behavior/int128.zig b/test/behavior/int128.zig index 6b4e2de895..12367b2e9c 100644 --- a/test/behavior/int128.zig +++ b/test/behavior/int128.zig @@ -20,8 +20,13 @@ test "uint128" { test "undefined 128 bit int" { @setRuntimeSafety(true); - // setRuntimeSafety isn't recognized on stage2 - if (@import("builtin").zig_is_stage2 and @import("builtin").mode != .Debug and @import("builtin").mode != .ReleaseSafe) return error.SkipZigTest; + // TODO implement @setRuntimeSafety in stage2 + if (@import("builtin").zig_is_stage2 and + @import("builtin").mode != .Debug and + @import("builtin").mode != .ReleaseSafe) + { + return error.SkipZigTest; + } var undef: u128 = undefined; var undef_signed: i128 = undefined; -- cgit v1.2.3