authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-21 19:39:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-21 19:43:08-07:00
log6afcaf4a08b6fb1cce0cdb2393fc1d4cd041509c
treebf4a17c51d53b720077e8315c5d0a38f20eba219
parent96e5f661bd34d98bba89bcb70c9db059aaf38641

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

11 files changed, 163 insertions(+), 85 deletions(-)

src/Module.zig+4-2
......@@ -3661,7 +3661,8 @@ pub fn embedFile(mod: *Module, cur_file: *File, rel_file_path: []const u8) !*Emb
36613661 defer file.close();
36623662
36633663 const stat = try file.stat();
3664 const bytes = try file.readToEndAllocOptions(gpa, std.math.maxInt(u32), stat.size, 1, 0);
3664 const size_usize = try std.math.cast(usize, stat.size);
3665 const bytes = try file.readToEndAllocOptions(gpa, std.math.maxInt(u32), size_usize, 1, 0);
36653666
36663667 log.debug("new embedFile. resolved_root_path={s}, resolved_path={s}, sub_file_path={s}, rel_file_path={s}", .{
36673668 resolved_root_path, resolved_path, sub_file_path, rel_file_path,
......@@ -3694,7 +3695,8 @@ pub fn detectEmbedFileUpdate(mod: *Module, embed_file: *EmbedFile) !void {
36943695 if (unchanged_metadata) return;
36953696
36963697 const gpa = mod.gpa;
3697 const bytes = try file.readToEndAllocOptions(gpa, std.math.maxInt(u32), stat.size, 1, 0);
3698 const size_usize = try std.math.cast(usize, stat.size);
3699 const bytes = try file.readToEndAllocOptions(gpa, std.math.maxInt(u32), size_usize, 1, 0);
36983700 gpa.free(embed_file.bytes);
36993701 embed_file.bytes = bytes;
37003702 embed_file.stat_size = stat.size;
src/Sema.zig+63-27
......@@ -7020,7 +7020,7 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
70207020 if (val.isUndef()) {
70217021 return sema.addConstUndef(scalar_type);
70227022 } else if (operand_type.zigTypeTag() == .Vector) {
7023 const vec_len = operand_type.arrayLen();
7023 const vec_len = try sema.usizeCast(block, operand_src, operand_type.arrayLen());
70247024 var elem_val_buf: Value.ElemValueBuffer = undefined;
70257025 const elems = try sema.arena.alloc(Value, vec_len);
70267026 for (elems) |*elem, i| {
......@@ -7073,7 +7073,9 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
70737073
70747074 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {
70757075 if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| {
7076 const final_len = lhs_info.len + rhs_info.len;
7076 const lhs_len = try sema.usizeCast(block, lhs_src, lhs_info.len);
7077 const rhs_len = try sema.usizeCast(block, lhs_src, rhs_info.len);
7078 const final_len = lhs_len + rhs_len;
70777079 const final_len_including_sent = final_len + @boolToInt(res_sent != null);
70787080 const is_pointer = lhs_ty.zigTypeTag() == .Pointer;
70797081 const lhs_sub_val = if (is_pointer) (try sema.pointerDeref(block, lhs_src, lhs_val, lhs_ty)).? else lhs_val;
......@@ -7083,17 +7085,17 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
70837085
70847086 const buf = try anon_decl.arena().alloc(Value, final_len_including_sent);
70857087 {
7086 var i: u64 = 0;
7087 while (i < lhs_info.len) : (i += 1) {
7088 var i: usize = 0;
7089 while (i < lhs_len) : (i += 1) {
70887090 const val = try lhs_sub_val.elemValue(sema.arena, i);
70897091 buf[i] = try val.copy(anon_decl.arena());
70907092 }
70917093 }
70927094 {
7093 var i: u64 = 0;
7094 while (i < rhs_info.len) : (i += 1) {
7095 var i: usize = 0;
7096 while (i < rhs_len) : (i += 1) {
70957097 const val = try rhs_sub_val.elemValue(sema.arena, i);
7096 buf[lhs_info.len + i] = try val.copy(anon_decl.arena());
7098 buf[lhs_len + i] = try val.copy(anon_decl.arena());
70977099 }
70987100 }
70997101 const ty = if (res_sent) |rs| ty: {
......@@ -7143,6 +7145,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
71437145 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
71447146 const lhs = sema.resolveInst(extra.lhs);
71457147 const lhs_ty = sema.typeOf(lhs);
7148 const src: LazySrcLoc = inst_data.src();
71467149 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
71477150 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
71487151
......@@ -7151,11 +7154,14 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
71517154 const mulinfo = getArrayCatInfo(lhs_ty) orelse
71527155 return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty});
71537156
7154 const final_len = std.math.mul(u64, mulinfo.len, factor) catch
7157 const final_len_u64 = std.math.mul(u64, mulinfo.len, factor) catch
71557158 return sema.fail(block, rhs_src, "operation results in overflow", .{});
7156 const final_len_including_sent = final_len + @boolToInt(mulinfo.sentinel != null);
71577159
71587160 if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| {
7161 const final_len = try sema.usizeCast(block, src, final_len_u64);
7162 const final_len_including_sent = final_len + @boolToInt(mulinfo.sentinel != null);
7163 const lhs_len = try sema.usizeCast(block, lhs_src, mulinfo.len);
7164
71597165 const lhs_sub_val = if (lhs_ty.zigTypeTag() == .Pointer) (try sema.pointerDeref(block, lhs_src, lhs_val, lhs_ty)).? else lhs_val;
71607166
71617167 var anon_decl = try block.startAnonDecl();
......@@ -7176,18 +7182,18 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
71767182
71777183 // Optimization for the common pattern of a single element repeated N times, such
71787184 // as zero-filling a byte array.
7179 const val = if (mulinfo.len == 1) blk: {
7185 const val = if (lhs_len == 1) blk: {
71807186 const elem_val = try lhs_sub_val.elemValue(sema.arena, 0);
71817187 const copied_val = try elem_val.copy(anon_decl.arena());
71827188 break :blk try Value.Tag.repeated.create(anon_decl.arena(), copied_val);
71837189 } else blk: {
71847190 // the actual loop
7185 var i: u64 = 0;
7191 var i: usize = 0;
71867192 while (i < factor) : (i += 1) {
7187 var j: u64 = 0;
7188 while (j < mulinfo.len) : (j += 1) {
7193 var j: usize = 0;
7194 while (j < lhs_len) : (j += 1) {
71897195 const val = try lhs_sub_val.elemValue(sema.arena, j);
7190 buf[mulinfo.len * i + j] = try val.copy(anon_decl.arena());
7196 buf[lhs_len * i + j] = try val.copy(anon_decl.arena());
71917197 }
71927198 }
71937199 if (mulinfo.sentinel) |sent| {
......@@ -8122,7 +8128,7 @@ fn analyzePtrArithmetic(
81228128 return sema.addConstUndef(new_ptr_ty);
81238129 }
81248130
8125 const offset_int = offset_val.toUnsignedInt();
8131 const offset_int = try sema.usizeCast(block, offset_src, offset_val.toUnsignedInt());
81268132 if (ptr_val.getUnsignedInt()) |addr| {
81278133 const target = sema.mod.getTarget();
81288134 const ptr_child_ty = ptr_ty.childType();
......@@ -10204,7 +10210,7 @@ fn checkComptimeVarStore(
1020410210}
1020510211
1020610212const SimdBinOp = struct {
10207 len: ?u64,
10213 len: ?usize,
1020810214 /// Coerced to `result_ty`.
1020910215 lhs: Air.Inst.Ref,
1021010216 /// Coerced to `result_ty`.
......@@ -10230,7 +10236,7 @@ fn checkSimdBinOp(
1023010236 const lhs_zig_ty_tag = try lhs_ty.zigTypeTagOrPoison();
1023110237 const rhs_zig_ty_tag = try rhs_ty.zigTypeTagOrPoison();
1023210238
10233 var vec_len: ?u64 = null;
10239 var vec_len: ?usize = null;
1023410240 if (lhs_zig_ty_tag == .Vector and rhs_zig_ty_tag == .Vector) {
1023510241 const lhs_len = lhs_ty.arrayLen();
1023610242 const rhs_len = rhs_ty.arrayLen();
......@@ -10244,7 +10250,7 @@ fn checkSimdBinOp(
1024410250 };
1024510251 return sema.failWithOwnedErrorMsg(msg);
1024610252 }
10247 vec_len = lhs_len;
10253 vec_len = try sema.usizeCast(block, lhs_src, lhs_len);
1024810254 } else if (lhs_zig_ty_tag == .Vector or rhs_zig_ty_tag == .Vector) {
1024910255 const msg = msg: {
1025010256 const msg = try sema.errMsg(block, src, "mixed scalar and vector operands: {} and {}", .{
......@@ -12671,8 +12677,7 @@ fn storePtrVal(
1267112677 var kit = try beginComptimePtrMutation(sema, block, src, ptr_val);
1267212678 try sema.checkComptimeVarStore(block, src, kit.decl_ref_mut);
1267312679
12674 const target = sema.mod.getTarget();
12675 const bitcasted_val = try operand_val.bitCast(operand_ty, kit.ty, target, sema.gpa, sema.arena);
12680 const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty);
1267612681
1267712682 const arena = kit.beginArena(sema.gpa);
1267812683 defer kit.finishArena();
......@@ -12724,7 +12729,9 @@ fn beginComptimePtrMutation(
1272412729 const arena = parent.beginArena(sema.gpa);
1272512730 defer parent.finishArena();
1272612731
12727 const elems = try arena.alloc(Value, parent.ty.arrayLenIncludingSentinel());
12732 const array_len_including_sentinel =
12733 try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel());
12734 const elems = try arena.alloc(Value, array_len_including_sentinel);
1272812735 mem.set(Value, elems, Value.undef);
1272912736
1273012737 parent.val.* = try Value.Tag.array.create(arena, elems);
......@@ -12771,7 +12778,9 @@ fn beginComptimePtrMutation(
1277112778 defer parent.finishArena();
1277212779
1277312780 const repeated_val = try parent.val.castTag(.repeated).?.data.copy(arena);
12774 const elems = try arena.alloc(Value, parent.ty.arrayLenIncludingSentinel());
12781 const array_len_including_sentinel =
12782 try sema.usizeCast(block, src, parent.ty.arrayLenIncludingSentinel());
12783 const elems = try arena.alloc(Value, array_len_including_sentinel);
1277512784 mem.set(Value, elems, repeated_val);
1277612785
1277712786 parent.val.* = try Value.Tag.array.create(arena, elems);
......@@ -12925,7 +12934,7 @@ fn beginComptimePtrLoad(
1292512934 .root_val = parent.root_val,
1292612935 .val = try parent.val.elemValue(sema.arena, elem_ptr.index),
1292712936 .ty = elem_ty,
12928 .byte_offset = parent.byte_offset + elem_size * elem_ptr.index,
12937 .byte_offset = try sema.usizeCast(block, src, parent.byte_offset + elem_size * elem_ptr.index),
1292912938 .is_mutable = parent.is_mutable,
1293012939 };
1293112940 },
......@@ -12939,7 +12948,7 @@ fn beginComptimePtrLoad(
1293912948 .root_val = parent.root_val,
1294012949 .val = try parent.val.fieldValue(sema.arena, field_index),
1294112950 .ty = parent.ty.structFieldType(field_index),
12942 .byte_offset = parent.byte_offset + field_offset,
12951 .byte_offset = try sema.usizeCast(block, src, parent.byte_offset + field_offset),
1294312952 .is_mutable = parent.is_mutable,
1294412953 };
1294512954 },
......@@ -12990,15 +12999,34 @@ fn bitCast(
1299012999) CompileError!Air.Inst.Ref {
1299113000 // TODO validate the type size and other compile errors
1299213001 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
12993 const target = sema.mod.getTarget();
1299413002 const old_ty = sema.typeOf(inst);
12995 const result_val = try val.bitCast(old_ty, dest_ty, target, sema.gpa, sema.arena);
13003 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty);
1299613004 return sema.addConstant(dest_ty, result_val);
1299713005 }
1299813006 try sema.requireRuntimeBlock(block, inst_src);
1299913007 return block.addBitCast(dest_ty, inst);
1300013008}
1300113009
13010pub fn bitCastVal(
13011 sema: *Sema,
13012 block: *Block,
13013 src: LazySrcLoc,
13014 val: Value,
13015 old_ty: Type,
13016 new_ty: Type,
13017) !Value {
13018 if (old_ty.eql(new_ty)) return val;
13019
13020 // For types with well-defined memory layouts, we serialize them a byte buffer,
13021 // then deserialize to the new type.
13022 const target = sema.mod.getTarget();
13023 const abi_size = try sema.usizeCast(block, src, old_ty.abiSize(target));
13024 const buffer = try sema.gpa.alloc(u8, abi_size);
13025 defer sema.gpa.free(buffer);
13026 val.writeToMemory(old_ty, target, buffer);
13027 return Value.readFromMemory(new_ty, target, buffer, sema.arena);
13028}
13029
1300213030fn coerceArrayPtrToSlice(
1300313031 sema: *Sema,
1300413032 block: *Block,
......@@ -15103,7 +15131,7 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr
1510315131 // The Type it is stored as in the compiler has an ABI size greater or equal to
1510415132 // the ABI size of `load_ty`. We may perform the bitcast based on
1510515133 // `parent.val` alone (more efficient).
15106 return try parent.val.bitCast(parent.ty, load_ty, target, sema.gpa, sema.arena);
15134 return try sema.bitCastVal(block, src, parent.val, parent.ty, load_ty);
1510715135 }
1510815136
1510915137 // The Type it is stored as in the compiler has an ABI size less than the ABI size
......@@ -15111,3 +15139,11 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr
1511115139 // and reinterpreted starting at `parent.byte_offset`.
1511215140 return sema.fail(block, src, "TODO: implement bitcast with index offset", .{});
1511315141}
15142
15143/// Used to convert a u64 value to a usize value, emitting a compile error if the number
15144/// is too big to fit.
15145fn usizeCast(sema: *Sema, block: *Block, src: LazySrcLoc, int: u64) CompileError!usize {
15146 return std.math.cast(usize, int) catch |err| switch (err) {
15147 error.Overflow => return sema.fail(block, src, "expression produces integer value {d} which is too big for this compiler implementation to handle", .{int}),
15148 };
15149}
src/arch/wasm/CodeGen.zig+4-1
......@@ -538,6 +538,8 @@ const InnerError = error{
538538 AnalysisFail,
539539 /// Failed to emit MIR instructions to binary/textual representation.
540540 EmitFail,
541 /// Compiler implementation could not handle a large integer.
542 Overflow,
541543};
542544
543545pub fn deinit(self: *Self) void {
......@@ -877,7 +879,8 @@ pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {
877879 },
878880 .Struct => {
879881 // TODO write the fields for real
880 try self.code.writer().writeByteNTimes(0xaa, ty.abiSize(self.target));
882 const abi_size = try std.math.cast(usize, ty.abiSize(self.target));
883 try self.code.writer().writeByteNTimes(0xaa, abi_size);
881884 return Result{ .appended = {} };
882885 },
883886 else => |tag| return self.fail("TODO: Implement zig type codegen for type: '{s}'", .{tag}),
src/arch/x86_64/Emit.zig+3-1
......@@ -42,6 +42,7 @@ relocs: std.ArrayListUnmanaged(Reloc) = .{},
4242
4343const InnerError = error{
4444 OutOfMemory,
45 Overflow,
4546 EmitFail,
4647};
4748
......@@ -174,10 +175,11 @@ fn fixupRelocs(emit: *Emit) InnerError!void {
174175 // possible resolution, i.e., 8bit, and iteratively converge on the minimum required resolution
175176 // until the entire decl is correctly emitted with all JMP/CALL instructions within range.
176177 for (emit.relocs.items) |reloc| {
178 const offset = try math.cast(usize, reloc.offset);
177179 const target = emit.code_offset_mapping.get(reloc.target) orelse
178180 return emit.fail("JMP/CALL relocation target not found!", .{});
179181 const disp = @intCast(i32, @intCast(i64, target) - @intCast(i64, reloc.source + reloc.length));
180 mem.writeIntLittle(i32, emit.code.items[reloc.offset..][0..4], disp);
182 mem.writeIntLittle(i32, emit.code.items[offset..][0..4], disp);
181183 }
182184}
183185
src/arch/x86_64/abi.zig+2-2
......@@ -207,7 +207,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
207207 // "Otherwise class SSE is used."
208208 result[result_i] = .sse;
209209 }
210 byte_i += field_size;
210 byte_i += @intCast(usize, field_size);
211211 if (byte_i == 8) {
212212 byte_i = 0;
213213 result_i += 1;
......@@ -222,7 +222,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
222222 result_i += field_class.len;
223223 // If there are any bytes leftover, we have to try to combine
224224 // the next field with them.
225 byte_i = field_size % 8;
225 byte_i = @intCast(usize, field_size % 8);
226226 if (byte_i != 0) result_i -= 1;
227227 }
228228 }
src/codegen.zig+3-1
......@@ -37,6 +37,7 @@ pub const Result = union(enum) {
3737
3838pub const GenerateSymbolError = error{
3939 OutOfMemory,
40 Overflow,
4041 /// A Decl that this symbol depends on had a semantic analysis failure.
4142 AnalysisFail,
4243};
......@@ -289,7 +290,8 @@ pub fn generateSymbol(
289290 const field_vals = typed_value.val.castTag(.@"struct").?.data;
290291 _ = field_vals; // TODO write the fields for real
291292 const target = bin_file.options.target;
292 try code.writer().writeByteNTimes(0xaa, typed_value.ty.abiSize(target));
293 const abi_size = try math.cast(usize, typed_value.ty.abiSize(target));
294 try code.writer().writeByteNTimes(0xaa, abi_size);
293295 return Result{ .appended = {} };
294296 },
295297 else => |t| {
src/codegen/llvm.zig+28-12
......@@ -1006,10 +1006,18 @@ pub const DeclGen = struct {
10061006 const int_info = tv.ty.intInfo(target);
10071007 const llvm_type = self.context.intType(int_info.bits);
10081008
1009 const unsigned_val = if (bigint.limbs.len == 1)
1010 llvm_type.constInt(bigint.limbs[0], .False)
1011 else
1012 llvm_type.constIntOfArbitraryPrecision(@intCast(c_uint, bigint.limbs.len), bigint.limbs.ptr);
1009 const unsigned_val = v: {
1010 if (bigint.limbs.len == 1) {
1011 break :v llvm_type.constInt(bigint.limbs[0], .False);
1012 }
1013 if (@sizeOf(usize) == @sizeOf(u64)) {
1014 break :v llvm_type.constIntOfArbitraryPrecision(
1015 @intCast(c_uint, bigint.limbs.len),
1016 bigint.limbs.ptr,
1017 );
1018 }
1019 @panic("TODO implement bigint to llvm int for 32-bit compiler builds");
1020 };
10131021 if (!bigint.positive) {
10141022 return llvm.constNeg(unsigned_val);
10151023 }
......@@ -1026,10 +1034,18 @@ pub const DeclGen = struct {
10261034 const int_info = tv.ty.intInfo(target);
10271035 const llvm_type = self.context.intType(int_info.bits);
10281036
1029 const unsigned_val = if (bigint.limbs.len == 1)
1030 llvm_type.constInt(bigint.limbs[0], .False)
1031 else
1032 llvm_type.constIntOfArbitraryPrecision(@intCast(c_uint, bigint.limbs.len), bigint.limbs.ptr);
1037 const unsigned_val = v: {
1038 if (bigint.limbs.len == 1) {
1039 break :v llvm_type.constInt(bigint.limbs[0], .False);
1040 }
1041 if (@sizeOf(usize) == @sizeOf(u64)) {
1042 break :v llvm_type.constIntOfArbitraryPrecision(
1043 @intCast(c_uint, bigint.limbs.len),
1044 bigint.limbs.ptr,
1045 );
1046 }
1047 @panic("TODO implement bigint to llvm int for 32-bit compiler builds");
1048 };
10331049 if (!bigint.positive) {
10341050 return llvm.constNeg(unsigned_val);
10351051 }
......@@ -1144,7 +1160,7 @@ pub const DeclGen = struct {
11441160 const val = tv.val.castTag(.repeated).?.data;
11451161 const elem_ty = tv.ty.elemType();
11461162 const sentinel = tv.ty.sentinel();
1147 const len = tv.ty.arrayLen();
1163 const len = @intCast(usize, tv.ty.arrayLen());
11481164 const len_including_sent = len + @boolToInt(sentinel != null);
11491165 const gpa = self.gpa;
11501166 const llvm_elems = try gpa.alloc(*const llvm.Value, len_including_sent);
......@@ -1317,7 +1333,7 @@ pub const DeclGen = struct {
13171333 .bytes => {
13181334 // Note, sentinel is not stored even if the type has a sentinel.
13191335 const bytes = tv.val.castTag(.bytes).?.data;
1320 const vector_len = tv.ty.arrayLen();
1336 const vector_len = @intCast(usize, tv.ty.arrayLen());
13211337 assert(vector_len == bytes.len or vector_len + 1 == bytes.len);
13221338
13231339 const elem_ty = tv.ty.elemType();
......@@ -1343,7 +1359,7 @@ pub const DeclGen = struct {
13431359 // Note, sentinel is not stored even if the type has a sentinel.
13441360 // The value includes the sentinel in those cases.
13451361 const elem_vals = tv.val.castTag(.array).?.data;
1346 const vector_len = tv.ty.arrayLen();
1362 const vector_len = @intCast(usize, tv.ty.arrayLen());
13471363 assert(vector_len == elem_vals.len or vector_len + 1 == elem_vals.len);
13481364 const elem_ty = tv.ty.elemType();
13491365 const llvm_elems = try self.gpa.alloc(*const llvm.Value, vector_len);
......@@ -1360,7 +1376,7 @@ pub const DeclGen = struct {
13601376 // Note, sentinel is not stored even if the type has a sentinel.
13611377 const val = tv.val.castTag(.repeated).?.data;
13621378 const elem_ty = tv.ty.elemType();
1363 const len = tv.ty.arrayLen();
1379 const len = @intCast(usize, tv.ty.arrayLen());
13641380 const llvm_elems = try self.gpa.alloc(*const llvm.Value, len);
13651381 defer self.gpa.free(llvm_elems);
13661382 for (llvm_elems) |*elem| {
src/link.zig+40-5
......@@ -350,9 +350,39 @@ pub const File = struct {
350350 }
351351 }
352352
353 pub const UpdateDeclError = error{
354 OutOfMemory,
355 Overflow,
356 Underflow,
357 FileTooBig,
358 InputOutput,
359 FilesOpenedWithWrongFlags,
360 IsDir,
361 NoSpaceLeft,
362 Unseekable,
363 PermissionDenied,
364 FileBusy,
365 SystemResources,
366 OperationAborted,
367 BrokenPipe,
368 ConnectionResetByPeer,
369 ConnectionTimedOut,
370 NotOpenForReading,
371 WouldBlock,
372 AccessDenied,
373 Unexpected,
374 DiskQuota,
375 NotOpenForWriting,
376 AnalysisFail,
377 CodegenFail,
378 EmitFail,
379 NameTooLong,
380 CurrentWorkingDirectoryUnlinked,
381 };
382
353383 /// May be called before or after updateDeclExports but must be called
354384 /// after allocateDeclIndexes for any given Decl.
355 pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) !void {
385 pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) UpdateDeclError!void {
356386 log.debug("updateDecl {*} ({s}), type={}", .{ decl, decl.name, decl.ty });
357387 assert(decl.has_tv);
358388 switch (base.tag) {
......@@ -370,7 +400,7 @@ pub const File = struct {
370400
371401 /// May be called before or after updateDeclExports but must be called
372402 /// after allocateDeclIndexes for any given Decl.
373 pub fn updateFunc(base: *File, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) !void {
403 pub fn updateFunc(base: *File, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) UpdateDeclError!void {
374404 log.debug("updateFunc {*} ({s}), type={}", .{
375405 func.owner_decl, func.owner_decl.name, func.owner_decl.ty,
376406 });
......@@ -387,7 +417,7 @@ pub const File = struct {
387417 }
388418 }
389419
390 pub fn updateDeclLineNumber(base: *File, module: *Module, decl: *Module.Decl) !void {
420 pub fn updateDeclLineNumber(base: *File, module: *Module, decl: *Module.Decl) UpdateDeclError!void {
391421 log.debug("updateDeclLineNumber {*} ({s}), line={}", .{
392422 decl, decl.name, decl.src_line + 1,
393423 });
......@@ -407,12 +437,17 @@ pub const File = struct {
407437 /// TODO we're transitioning to deleting this function and instead having
408438 /// each linker backend notice the first time updateDecl or updateFunc is called, or
409439 /// a callee referenced from AIR.
410 pub fn allocateDeclIndexes(base: *File, decl: *Module.Decl) !void {
440 pub fn allocateDeclIndexes(base: *File, decl: *Module.Decl) error{OutOfMemory}!void {
411441 log.debug("allocateDeclIndexes {*} ({s})", .{ decl, decl.name });
412442 switch (base.tag) {
413443 .coff => return @fieldParentPtr(Coff, "base", base).allocateDeclIndexes(decl),
414444 .elf => return @fieldParentPtr(Elf, "base", base).allocateDeclIndexes(decl),
415 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl),
445 .macho => return @fieldParentPtr(MachO, "base", base).allocateDeclIndexes(decl) catch |err| switch (err) {
446 // remap this error code because we are transitioning away from
447 // `allocateDeclIndexes`.
448 error.Overflow => return error.OutOfMemory,
449 error.OutOfMemory => return error.OutOfMemory,
450 },
416451 .wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl),
417452 .plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl),
418453 .c, .spirv => {},
src/link/MachO.zig+8-8
......@@ -1788,19 +1788,18 @@ pub fn getMatchingSection(self: *MachO, sect: macho.section_64) !?MatchingSectio
17881788}
17891789
17901790pub fn createEmptyAtom(self: *MachO, local_sym_index: u32, size: u64, alignment: u32) !*Atom {
1791 const code = try self.base.allocator.alloc(u8, size);
1792 defer self.base.allocator.free(code);
1793 mem.set(u8, code, 0);
1794
1791 const size_usize = try math.cast(usize, size);
17951792 const atom = try self.base.allocator.create(Atom);
17961793 errdefer self.base.allocator.destroy(atom);
17971794 atom.* = Atom.empty;
17981795 atom.local_sym_index = local_sym_index;
17991796 atom.size = size;
18001797 atom.alignment = alignment;
1801 try atom.code.appendSlice(self.base.allocator, code);
1802 try self.managed_atoms.append(self.base.allocator, atom);
18031798
1799 try atom.code.resize(self.base.allocator, size_usize);
1800 mem.set(u8, atom.code.items, 0);
1801
1802 try self.managed_atoms.append(self.base.allocator, atom);
18041803 return atom;
18051804}
18061805
......@@ -1872,9 +1871,10 @@ fn writeAtoms(self: *MachO) !void {
18721871 while (true) {
18731872 if (atom.dirty or self.invalidate_relocs) {
18741873 const atom_sym = self.locals.items[atom.local_sym_index];
1875 const padding_size: u64 = if (atom.next) |next| blk: {
1874 const padding_size: usize = if (atom.next) |next| blk: {
18761875 const next_sym = self.locals.items[next.local_sym_index];
1877 break :blk next_sym.n_value - (atom_sym.n_value + atom.size);
1876 const size = next_sym.n_value - (atom_sym.n_value + atom.size);
1877 break :blk try math.cast(usize, size);
18781878 } else 0;
18791879
18801880 log.debug(" (adding atom {s} to buffer: {})", .{ self.getString(atom_sym.n_strx), atom_sym });
src/link/Plan9.zig+4-4
......@@ -71,9 +71,9 @@ entry_val: ?u64 = null,
7171got_len: usize = 0,
7272// A list of all the free got indexes, so when making a new decl
7373// don't make a new one, just use one from here.
74got_index_free_list: std.ArrayListUnmanaged(u64) = .{},
74got_index_free_list: std.ArrayListUnmanaged(usize) = .{},
7575
76syms_index_free_list: std.ArrayListUnmanaged(u64) = .{},
76syms_index_free_list: std.ArrayListUnmanaged(usize) = .{},
7777
7878const Bases = struct {
7979 text: u64,
......@@ -356,8 +356,8 @@ pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void {
356356 }
357357}
358358
359fn declCount(self: *Plan9) u64 {
360 var fn_decl_count: u64 = 0;
359fn declCount(self: *Plan9) usize {
360 var fn_decl_count: usize = 0;
361361 var itf_files = self.fn_decl_table.iterator();
362362 while (itf_files.next()) |ent| {
363363 // get the submap
src/value.zig+4-22
......@@ -995,24 +995,6 @@ pub const Value = extern union {
995995 };
996996 }
997997
998 pub fn bitCast(
999 val: Value,
1000 old_ty: Type,
1001 new_ty: Type,
1002 target: Target,
1003 gpa: *Allocator,
1004 arena: *Allocator,
1005 ) !Value {
1006 if (old_ty.eql(new_ty)) return val;
1007
1008 // For types with well-defined memory layouts, we serialize them a byte buffer,
1009 // then deserialize to the new type.
1010 const buffer = try gpa.alloc(u8, old_ty.abiSize(target));
1011 defer gpa.free(buffer);
1012 val.writeToMemory(old_ty, target, buffer);
1013 return Value.readFromMemory(new_ty, target, buffer, arena);
1014 }
1015
1016998 pub fn writeToMemory(val: Value, ty: Type, target: Target, buffer: []u8) void {
1017999 switch (ty.zigTypeTag()) {
10181000 .Int => {
......@@ -1039,7 +1021,7 @@ pub const Value = extern union {
10391021 .Array, .Vector => {
10401022 const len = ty.arrayLen();
10411023 const elem_ty = ty.childType();
1042 const elem_size = elem_ty.abiSize(target);
1024 const elem_size = @intCast(usize, elem_ty.abiSize(target));
10431025 var elem_i: usize = 0;
10441026 var elem_value_buf: ElemValueBuffer = undefined;
10451027 var buf_off: usize = 0;
......@@ -2494,7 +2476,7 @@ pub const Value = extern union {
24942476 // resorting to BigInt first.
24952477 var lhs_space: Value.BigIntSpace = undefined;
24962478 const lhs_bigint = lhs.toBigInt(&lhs_space);
2497 const shift = rhs.toUnsignedInt();
2479 const shift = @intCast(usize, rhs.toUnsignedInt());
24982480 const limbs = try allocator.alloc(
24992481 std.math.big.Limb,
25002482 lhs_bigint.limbs.len + (shift / (@sizeOf(std.math.big.Limb) * 8)) + 1,
......@@ -2521,7 +2503,7 @@ pub const Value = extern union {
25212503
25222504 var lhs_space: Value.BigIntSpace = undefined;
25232505 const lhs_bigint = lhs.toBigInt(&lhs_space);
2524 const shift = rhs.toUnsignedInt();
2506 const shift = @intCast(usize, rhs.toUnsignedInt());
25252507 const limbs = try arena.alloc(
25262508 std.math.big.Limb,
25272509 std.math.big.int.calcTwosCompLimbCount(info.bits),
......@@ -2540,7 +2522,7 @@ pub const Value = extern union {
25402522 // resorting to BigInt first.
25412523 var lhs_space: Value.BigIntSpace = undefined;
25422524 const lhs_bigint = lhs.toBigInt(&lhs_space);
2543 const shift = rhs.toUnsignedInt();
2525 const shift = @intCast(usize, rhs.toUnsignedInt());
25442526 const limbs = try allocator.alloc(
25452527 std.math.big.Limb,
25462528 lhs_bigint.limbs.len - (shift / (@sizeOf(std.math.big.Limb) * 8)),