diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-11-21 19:39:32 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-11-21 19:43:08 -0700 |
| commit | 6afcaf4a08b6fb1cce0cdb2393fc1d4cd041509c (patch) | |
| tree | bf4a17c51d53b720077e8315c5d0a38f20eba219 /src/codegen | |
| parent | 96e5f661bd34d98bba89bcb70c9db059aaf38641 (diff) | |
| download | zig-6afcaf4a08b6fb1cce0cdb2393fc1d4cd041509c.tar.gz zig-6afcaf4a08b6fb1cce0cdb2393fc1d4cd041509c.zip | |
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
Diffstat (limited to 'src/codegen')
| -rw-r--r-- | src/codegen/llvm.zig | 40 |
1 files changed, 28 insertions, 12 deletions
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| { |
