From 6afcaf4a08b6fb1cce0cdb2393fc1d4cd041509c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 21 Nov 2021 19:39:32 -0700 Subject: stage2: fix the build for 32-bit architectures * Introduce a mechanism into Sema for emitting a compile error when an integer is too big and we need it to fit into a usize. * Add `@intCast` where necessary * link/MachO: fix an unnecessary allocation when all that was happening was appending zeroes to an ArrayList. * Add `error.Overflow` as a possible error to some codepaths, allowing usage of `math.intCast`. closes #9710 --- src/codegen/llvm.zig | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 306a3df83c..b75cc52856 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1006,10 +1006,18 @@ pub const DeclGen = struct { const int_info = tv.ty.intInfo(target); const llvm_type = self.context.intType(int_info.bits); - const unsigned_val = if (bigint.limbs.len == 1) - llvm_type.constInt(bigint.limbs[0], .False) - else - llvm_type.constIntOfArbitraryPrecision(@intCast(c_uint, bigint.limbs.len), bigint.limbs.ptr); + const unsigned_val = v: { + if (bigint.limbs.len == 1) { + break :v llvm_type.constInt(bigint.limbs[0], .False); + } + if (@sizeOf(usize) == @sizeOf(u64)) { + break :v llvm_type.constIntOfArbitraryPrecision( + @intCast(c_uint, bigint.limbs.len), + bigint.limbs.ptr, + ); + } + @panic("TODO implement bigint to llvm int for 32-bit compiler builds"); + }; if (!bigint.positive) { return llvm.constNeg(unsigned_val); } @@ -1026,10 +1034,18 @@ pub const DeclGen = struct { const int_info = tv.ty.intInfo(target); const llvm_type = self.context.intType(int_info.bits); - const unsigned_val = if (bigint.limbs.len == 1) - llvm_type.constInt(bigint.limbs[0], .False) - else - llvm_type.constIntOfArbitraryPrecision(@intCast(c_uint, bigint.limbs.len), bigint.limbs.ptr); + const unsigned_val = v: { + if (bigint.limbs.len == 1) { + break :v llvm_type.constInt(bigint.limbs[0], .False); + } + if (@sizeOf(usize) == @sizeOf(u64)) { + break :v llvm_type.constIntOfArbitraryPrecision( + @intCast(c_uint, bigint.limbs.len), + bigint.limbs.ptr, + ); + } + @panic("TODO implement bigint to llvm int for 32-bit compiler builds"); + }; if (!bigint.positive) { return llvm.constNeg(unsigned_val); } @@ -1144,7 +1160,7 @@ pub const DeclGen = struct { const val = tv.val.castTag(.repeated).?.data; const elem_ty = tv.ty.elemType(); const sentinel = tv.ty.sentinel(); - const len = tv.ty.arrayLen(); + const len = @intCast(usize, tv.ty.arrayLen()); const len_including_sent = len + @boolToInt(sentinel != null); const gpa = self.gpa; const llvm_elems = try gpa.alloc(*const llvm.Value, len_including_sent); @@ -1317,7 +1333,7 @@ pub const DeclGen = struct { .bytes => { // Note, sentinel is not stored even if the type has a sentinel. const bytes = tv.val.castTag(.bytes).?.data; - const vector_len = tv.ty.arrayLen(); + const vector_len = @intCast(usize, tv.ty.arrayLen()); assert(vector_len == bytes.len or vector_len + 1 == bytes.len); const elem_ty = tv.ty.elemType(); @@ -1343,7 +1359,7 @@ pub const DeclGen = struct { // Note, sentinel is not stored even if the type has a sentinel. // The value includes the sentinel in those cases. const elem_vals = tv.val.castTag(.array).?.data; - const vector_len = tv.ty.arrayLen(); + const vector_len = @intCast(usize, tv.ty.arrayLen()); assert(vector_len == elem_vals.len or vector_len + 1 == elem_vals.len); const elem_ty = tv.ty.elemType(); const llvm_elems = try self.gpa.alloc(*const llvm.Value, vector_len); @@ -1360,7 +1376,7 @@ pub const DeclGen = struct { // Note, sentinel is not stored even if the type has a sentinel. const val = tv.val.castTag(.repeated).?.data; const elem_ty = tv.ty.elemType(); - const len = tv.ty.arrayLen(); + const len = @intCast(usize, tv.ty.arrayLen()); const llvm_elems = try self.gpa.alloc(*const llvm.Value, len); defer self.gpa.free(llvm_elems); for (llvm_elems) |*elem| { -- cgit v1.2.3