authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-23 14:49:11+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-28 16:46:59+00:00
logc0f3a238314315a1e4a578b23cc66237eac90689
treec1ce31a04892cea98f61ef903444861cd85682ab
parentbd8088bb9826b40be281d5cca89eac7ff7b76241
signaturelock-open Commit is signed but in an unrecognized format.

llvm: get rid of a bunch of `PerThread` usages

Also, notably, remove `Air.value`! The `onePossibleValue` check was actually dead code, because it is a bug if Sema ever emits code which considers a value of OPV type to be runtime-known---and at that point `Air.value` is just a thin wrapper around `Air.Ref.toInterned`.

14 files changed, 620 insertions(+), 706 deletions(-)

src/Air.zig-9
...@@ -1843,15 +1843,6 @@ pub fn internedToRef(ip_index: InternPool.Index) Inst.Ref {...@@ -1843,15 +1843,6 @@ pub fn internedToRef(ip_index: InternPool.Index) Inst.Ref {
1843 return .fromIntern(ip_index);1843 return .fromIntern(ip_index);
1844}1844}
18451845
1846/// Returns `null` if runtime-known.
1847pub fn value(air: Air, inst: Inst.Ref, pt: Zcu.PerThread) !?Value {
1848 if (inst.toInterned()) |ip_index| {
1849 return .fromInterned(ip_index);
1850 }
1851 const index = inst.toIndex().?;
1852 return air.typeOfIndex(index, &pt.zcu.intern_pool).onePossibleValue(pt);
1853}
1854
1855pub const NullTerminatedString = enum(u32) {1846pub const NullTerminatedString = enum(u32) {
1856 none = std.math.maxInt(u32),1847 none = std.math.maxInt(u32),
1857 _,1848 _,
src/Sema.zig+1-1
...@@ -18896,7 +18896,7 @@ fn finishStructInit(...@@ -18896,7 +18896,7 @@ fn finishStructInit(
18896 var bit_offset: u16 = 0;18896 var bit_offset: u16 = 0;
18897 for (field_inits) |field_init| {18897 for (field_inits) |field_init| {
18898 const field_val = sema.resolveValue(field_init).?;18898 const field_val = sema.resolveValue(field_init).?;
18899 field_val.writeToPackedMemory(pt, buf, bit_offset) catch |err| switch (err) {18899 field_val.writeToPackedMemory(zcu, buf, bit_offset) catch |err| switch (err) {
18900 error.ReinterpretDeclRef => unreachable, // bitpack fields cannot be pointers18900 error.ReinterpretDeclRef => unreachable, // bitpack fields cannot be pointers
18901 error.OutOfMemory => |e| return e,18901 error.OutOfMemory => |e| return e,
18902 };18902 };
src/Sema/bitcast.zig+2-2
...@@ -443,7 +443,7 @@ const UnpackValueBits = struct {...@@ -443,7 +443,7 @@ const UnpackValueBits = struct {
443 // This @intCast is okay because no primitive can exceed the size of a u16.443 // This @intCast is okay because no primitive can exceed the size of a u16.
444 const int_ty = try unpack.pt.intType(.unsigned, @intCast(bit_count));444 const int_ty = try unpack.pt.intType(.unsigned, @intCast(bit_count));
445 const buf = try unpack.arena.alloc(u8, @intCast((val_bits + 7) / 8));445 const buf = try unpack.arena.alloc(u8, @intCast((val_bits + 7) / 8));
446 try val.writeToPackedMemory(unpack.pt, buf, 0);446 try val.writeToPackedMemory(zcu, buf, 0);
447 const sub_val = try Value.readFromPackedMemory(int_ty, unpack.pt, buf, @intCast(bit_offset), unpack.arena);447 const sub_val = try Value.readFromPackedMemory(int_ty, unpack.pt, buf, @intCast(bit_offset), unpack.arena);
448 try unpack.primitive(sub_val);448 try unpack.primitive(sub_val);
449 },449 },
...@@ -722,7 +722,7 @@ const PackValueBits = struct {...@@ -722,7 +722,7 @@ const PackValueBits = struct {
722 const val = Value.fromInterned(ip_val);722 const val = Value.fromInterned(ip_val);
723 const ty = val.typeOf(zcu);723 const ty = val.typeOf(zcu);
724 if (!val.isUndef(zcu)) {724 if (!val.isUndef(zcu)) {
725 try val.writeToPackedMemory(pt, buf, cur_bit_off);725 try val.writeToPackedMemory(zcu, buf, cur_bit_off);
726 }726 }
727 cur_bit_off += @intCast(ty.bitSize(zcu));727 cur_bit_off += @intCast(ty.bitSize(zcu));
728 }728 }
src/Value.zig+32-36
...@@ -245,13 +245,12 @@ pub fn toBool(val: Value) bool {...@@ -245,13 +245,12 @@ pub fn toBool(val: Value) bool {
245///245///
246/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past246/// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past
247/// the end of the value in memory.247/// the end of the value in memory.
248pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{248pub fn writeToMemory(val: Value, zcu: *const Zcu, buffer: []u8) error{
249 ReinterpretDeclRef,249 ReinterpretDeclRef,
250 IllDefinedMemoryLayout,250 IllDefinedMemoryLayout,
251 Unimplemented,251 Unimplemented,
252 OutOfMemory,252 OutOfMemory,
253}!void {253}!void {
254 const zcu = pt.zcu;
255 const target = zcu.getTarget();254 const target = zcu.getTarget();
256 const endian = target.cpu.arch.endian();255 const endian = target.cpu.arch.endian();
257 const ip = &zcu.intern_pool;256 const ip = &zcu.intern_pool;
...@@ -289,14 +288,18 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{...@@ -289,14 +288,18 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
289 else => unreachable,288 else => unreachable,
290 },289 },
291 .array => {290 .array => {
291 const aggregate = ip.indexToKey(val.toIntern()).aggregate;
292 const len = ty.arrayLen(zcu);292 const len = ty.arrayLen(zcu);
293 const elem_ty = ty.childType(zcu);293 const elem_ty = ty.childType(zcu);
294 const elem_size: usize = @intCast(elem_ty.abiSize(zcu));294 const elem_size: usize = @intCast(elem_ty.abiSize(zcu));
295 var elem_i: usize = 0;295 var elem_i: usize = 0;
296 var buf_off: usize = 0;296 var buf_off: usize = 0;
297 while (elem_i < len) : (elem_i += 1) {297 while (elem_i < len) : (elem_i += 1) {
298 const elem_val = try val.elemValue(pt, elem_i);298 switch (aggregate.storage) {
299 try elem_val.writeToMemory(pt, buffer[buf_off..]);299 .bytes => |bytes| buffer[buf_off] = bytes.at(elem_i, ip),
300 .elems => |elems| try Value.fromInterned(elems[elem_i]).writeToMemory(zcu, buffer[buf_off..]),
301 .repeated_elem => |elem| try Value.fromInterned(elem).writeToMemory(zcu, buffer[buf_off..]),
302 }
300 buf_off += elem_size;303 buf_off += elem_size;
301 }304 }
302 },305 },
...@@ -304,7 +307,7 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{...@@ -304,7 +307,7 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
304 // We use byte_count instead of abi_size here, so that any padding bytes307 // We use byte_count instead of abi_size here, so that any padding bytes
305 // follow the data bytes, on both big- and little-endian systems.308 // follow the data bytes, on both big- and little-endian systems.
306 const byte_count = (@as(usize, @intCast(ty.bitSize(zcu))) + 7) / 8;309 const byte_count = (@as(usize, @intCast(ty.bitSize(zcu))) + 7) / 8;
307 return writeToPackedMemory(val, pt, buffer[0..byte_count], 0);310 return writeToPackedMemory(val, zcu, buffer[0..byte_count], 0);
308 },311 },
309 .@"struct" => {312 .@"struct" => {
310 const struct_type = zcu.typeToStruct(ty) orelse return error.IllDefinedMemoryLayout;313 const struct_type = zcu.typeToStruct(ty) orelse return error.IllDefinedMemoryLayout;
...@@ -320,42 +323,33 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{...@@ -320,42 +323,33 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
320 .elems => |elems| elems[field_index],323 .elems => |elems| elems[field_index],
321 .repeated_elem => |elem| elem,324 .repeated_elem => |elem| elem,
322 });325 });
323 try writeToMemory(field_val, pt, buffer[off..]);326 try writeToMemory(field_val, zcu, buffer[off..]);
324 },327 },
325 .@"packed" => {328 .@"packed" => {
326 const int_index = ip.indexToKey(val.toIntern()).bitpack.backing_int_val;329 const int_index = ip.indexToKey(val.toIntern()).bitpack.backing_int_val;
327 return Value.fromInterned(int_index).writeToMemory(pt, buffer);330 return Value.fromInterned(int_index).writeToMemory(zcu, buffer);
328 },331 },
329 }332 }
330 },333 },
331 .@"union" => switch (ty.containerLayout(zcu)) {334 .@"union" => switch (ty.containerLayout(zcu)) {
332 .auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already335 .auto => return error.IllDefinedMemoryLayout, // Sema is supposed to have emitted a compile error already
333 .@"extern" => {336 .@"extern" => {
334 if (val.unionTag(zcu)) |union_tag| {337 const payload_val = val.unionPayload(zcu);
335 const union_obj = zcu.typeToUnion(ty).?;338 return writeToMemory(payload_val, zcu, buffer);
336 const field_index = zcu.unionTagFieldIndex(union_obj, union_tag).?;
337 const field_type = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
338 const field_val = try val.fieldValue(pt, field_index);
339 const byte_count: usize = @intCast(field_type.abiSize(zcu));
340 return writeToMemory(field_val, pt, buffer[0..byte_count]);
341 } else {
342 const backing_ty = try ty.externUnionBackingType(pt);
343 const byte_count: usize = @intCast(backing_ty.abiSize(zcu));
344 return writeToMemory(val.unionPayload(zcu), pt, buffer[0..byte_count]);
345 }
346 },339 },
347 .@"packed" => {340 .@"packed" => {
348 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);341 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);
349 return writeToMemory(int_val, pt, buffer);342 return writeToMemory(int_val, zcu, buffer);
350 },343 },
351 },344 },
352 .optional => {345 .optional => {
353 if (!ty.isPtrLikeOptional(zcu)) return error.IllDefinedMemoryLayout;346 if (!ty.isPtrLikeOptional(zcu)) return error.IllDefinedMemoryLayout;
354 const opt_val = val.optionalValue(zcu);347 const opt_val = val.optionalValue(zcu);
355 if (opt_val) |some| {348 if (opt_val) |some| {
356 return some.writeToMemory(pt, buffer);349 return some.writeToMemory(zcu, buffer);
357 } else {350 } else {
358 return writeToMemory(try pt.intValue(Type.usize, 0), pt, buffer);351 const byte_count = Type.usize.abiSize(zcu);
352 @memset(buffer[0..@intCast(byte_count)], 0); // null pointer
359 }353 }
360 },354 },
361 else => return error.Unimplemented,355 else => return error.Unimplemented,
...@@ -368,11 +362,10 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{...@@ -368,11 +362,10 @@ pub fn writeToMemory(val: Value, pt: Zcu.PerThread, buffer: []u8) error{
368/// big-endian packed memory layouts start at the end of the buffer.362/// big-endian packed memory layouts start at the end of the buffer.
369pub fn writeToPackedMemory(363pub fn writeToPackedMemory(
370 val: Value,364 val: Value,
371 pt: Zcu.PerThread,365 zcu: *const Zcu,
372 buffer: []u8,366 buffer: []u8,
373 bit_offset: usize,367 bit_offset: usize,
374) error{ ReinterpretDeclRef, OutOfMemory }!void {368) error{ ReinterpretDeclRef, OutOfMemory }!void {
375 const zcu = pt.zcu;
376 const ip = &zcu.intern_pool;369 const ip = &zcu.intern_pool;
377 const target = zcu.getTarget();370 const target = zcu.getTarget();
378 const endian = target.cpu.arch.endian();371 const endian = target.cpu.arch.endian();
...@@ -399,7 +392,7 @@ pub fn writeToPackedMemory(...@@ -399,7 +392,7 @@ pub fn writeToPackedMemory(
399 },392 },
400 .@"enum" => {393 .@"enum" => {
401 const int_val = val.intFromEnum(zcu);394 const int_val = val.intFromEnum(zcu);
402 return int_val.writeToPackedMemory(pt, buffer, bit_offset);395 return int_val.writeToPackedMemory(zcu, buffer, bit_offset);
403 },396 },
404 .pointer => {397 .pointer => {
405 assert(!ty.isSlice(zcu)); // No well defined layout.398 assert(!ty.isSlice(zcu)); // No well defined layout.
...@@ -430,25 +423,29 @@ pub fn writeToPackedMemory(...@@ -430,25 +423,29 @@ pub fn writeToPackedMemory(
430423
431 var bits: u16 = 0;424 var bits: u16 = 0;
432 var elem_i: usize = 0;425 var elem_i: usize = 0;
426 const aggregate = ip.indexToKey(val.toIntern()).aggregate;
433 while (elem_i < len) : (elem_i += 1) {427 while (elem_i < len) : (elem_i += 1) {
434 // On big-endian systems, LLVM reverses the element order of vectors by default428 // On big-endian systems, LLVM reverses the element order of vectors by default
435 const tgt_elem_i = if (endian == .big) len - elem_i - 1 else elem_i;429 const tgt_elem_i = if (endian == .big) len - elem_i - 1 else elem_i;
436 const elem_val = try val.elemValue(pt, tgt_elem_i);430 switch (aggregate.storage) {
437 try elem_val.writeToPackedMemory(pt, buffer, bit_offset + bits);431 .bytes => |bytes| std.mem.writePackedInt(u8, buffer, bit_offset + bits, bytes.at(tgt_elem_i, ip), endian),
432 .elems => |elems| try Value.fromInterned(elems[tgt_elem_i]).writeToPackedMemory(zcu, buffer, bit_offset + bits),
433 .repeated_elem => |elem| try Value.fromInterned(elem).writeToPackedMemory(zcu, buffer, bit_offset + bits),
434 }
438 bits += elem_bit_size;435 bits += elem_bit_size;
439 }436 }
440 },437 },
441 .@"struct", .@"union" => {438 .@"struct", .@"union" => {
442 assert(ty.containerLayout(zcu) == .@"packed");439 assert(ty.containerLayout(zcu) == .@"packed");
443 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);440 const int_val: Value = .fromInterned(ip.indexToKey(val.toIntern()).bitpack.backing_int_val);
444 return int_val.writeToPackedMemory(pt, buffer, bit_offset);441 return int_val.writeToPackedMemory(zcu, buffer, bit_offset);
445 },442 },
446 .optional => {443 .optional => {
447 assert(ty.isPtrLikeOptional(zcu));444 assert(ty.isPtrLikeOptional(zcu));
448 if (val.optionalValue(zcu)) |ptr_val| {445 if (val.optionalValue(zcu)) |ptr_val| {
449 return ptr_val.writeToPackedMemory(pt, buffer, bit_offset);446 return ptr_val.writeToPackedMemory(zcu, buffer, bit_offset);
450 } else {447 } else {
451 return Value.zero_usize.writeToPackedMemory(pt, buffer, bit_offset);448 return Value.zero_usize.writeToPackedMemory(zcu, buffer, bit_offset);
452 }449 }
453 },450 },
454 else => @panic("TODO implement writeToPackedMemory for more types"),451 else => @panic("TODO implement writeToPackedMemory for more types"),
...@@ -889,7 +886,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {...@@ -889,7 +886,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {
889 const sfba = sfba_state.get();886 const sfba = sfba_state.get();
890 const buf = try sfba.alloc(u8, @intCast((ty.bitSize(zcu) + 7) / 8));887 const buf = try sfba.alloc(u8, @intCast((ty.bitSize(zcu) + 7) / 8));
891 defer sfba.free(buf);888 defer sfba.free(buf);
892 int_val.writeToPackedMemory(pt, buf, 0) catch |err| switch (err) {889 int_val.writeToPackedMemory(zcu, buf, 0) catch |err| switch (err) {
893 error.ReinterpretDeclRef => unreachable, // it's an integer890 error.ReinterpretDeclRef => unreachable, // it's an integer
894 error.OutOfMemory => |e| return e,891 error.OutOfMemory => |e| return e,
895 };892 };
...@@ -902,7 +899,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {...@@ -902,7 +899,7 @@ pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value {
902 };899 };
903}900}
904901
905pub fn unionTag(val: Value, zcu: *Zcu) ?Value {902pub fn unionTag(val: Value, zcu: *const Zcu) ?Value {
906 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {903 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {
907 .undef, .enum_tag => val,904 .undef, .enum_tag => val,
908 .un => |un| if (un.tag != .none) Value.fromInterned(un.tag) else return null,905 .un => |un| if (un.tag != .none) Value.fromInterned(un.tag) else return null,
...@@ -910,7 +907,7 @@ pub fn unionTag(val: Value, zcu: *Zcu) ?Value {...@@ -910,7 +907,7 @@ pub fn unionTag(val: Value, zcu: *Zcu) ?Value {
910 };907 };
911}908}
912909
913pub fn unionPayload(val: Value, zcu: *Zcu) Value {910pub fn unionPayload(val: Value, zcu: *const Zcu) Value {
914 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {911 return switch (zcu.intern_pool.indexToKey(val.toIntern())) {
915 .un => |un| Value.fromInterned(un.val),912 .un => |un| Value.fromInterned(un.val),
916 else => unreachable,913 else => unreachable,
...@@ -1605,15 +1602,14 @@ pub fn mulAddScalar(...@@ -1605,15 +1602,14 @@ pub fn mulAddScalar(
16051602
1606/// If the value is represented in-memory as a series of bytes that all1603/// If the value is represented in-memory as a series of bytes that all
1607/// have the same value, return that byte value, otherwise null.1604/// have the same value, return that byte value, otherwise null.
1608pub fn hasRepeatedByteRepr(val: Value, pt: Zcu.PerThread) !?u8 {1605pub fn hasRepeatedByteRepr(val: Value, zcu: *const Zcu) !?u8 {
1609 const zcu = pt.zcu;
1610 const ty = val.typeOf(zcu);1606 const ty = val.typeOf(zcu);
1611 const abi_size = std.math.cast(usize, ty.abiSize(zcu)) orelse return null;1607 const abi_size = std.math.cast(usize, ty.abiSize(zcu)) orelse return null;
1612 assert(abi_size >= 1);1608 assert(abi_size >= 1);
1613 const byte_buffer = try zcu.gpa.alloc(u8, abi_size);1609 const byte_buffer = try zcu.gpa.alloc(u8, abi_size);
1614 defer zcu.gpa.free(byte_buffer);1610 defer zcu.gpa.free(byte_buffer);
16151611
1616 writeToMemory(val, pt, byte_buffer) catch |err| switch (err) {1612 writeToMemory(val, zcu, byte_buffer) catch |err| switch (err) {
1617 error.OutOfMemory => return error.OutOfMemory,1613 error.OutOfMemory => return error.OutOfMemory,
1618 error.ReinterpretDeclRef => return null,1614 error.ReinterpretDeclRef => return null,
1619 // TODO: The writeToMemory function was originally created for the purpose1615 // TODO: The writeToMemory function was originally created for the purpose
src/Zcu/PerThread.zig+1-1
...@@ -3751,7 +3751,7 @@ fn processExportsInner(...@@ -3751,7 +3751,7 @@ fn processExportsInner(
3751 if (skip_linker_work) return;3751 if (skip_linker_work) return;
37523752
3753 if (zcu.llvm_object) |llvm_object| {3753 if (zcu.llvm_object) |llvm_object| {
3754 try zcu.handleUpdateExports(export_indices, llvm_object.updateExports(pt, exported, export_indices));3754 try zcu.handleUpdateExports(export_indices, llvm_object.updateExports(exported, export_indices));
3755 } else if (zcu.comp.bin_file) |lf| {3755 } else if (zcu.comp.bin_file) |lf| {
3756 try zcu.handleUpdateExports(export_indices, lf.updateExports(pt, exported, export_indices));3756 try zcu.handleUpdateExports(export_indices, lf.updateExports(pt, exported, export_indices));
3757 }3757 }
src/codegen/aarch64/Select.zig+1-1
...@@ -11364,7 +11364,7 @@ fn writeToMemory(isel: *Select, constant: Constant, buffer: []u8) error{OutOfMem...@@ -11364,7 +11364,7 @@ fn writeToMemory(isel: *Select, constant: Constant, buffer: []u8) error{OutOfMem
11364 const zcu = isel.pt.zcu;11364 const zcu = isel.pt.zcu;
11365 const ip = &zcu.intern_pool;11365 const ip = &zcu.intern_pool;
11366 if (try isel.writeKeyToMemory(ip.indexToKey(constant.toIntern()), buffer)) return true;11366 if (try isel.writeKeyToMemory(ip.indexToKey(constant.toIntern()), buffer)) return true;
11367 constant.writeToMemory(isel.pt, buffer) catch |err| switch (err) {11367 constant.writeToMemory(zcu, buffer) catch |err| switch (err) {
11368 error.OutOfMemory => return error.OutOfMemory,11368 error.OutOfMemory => return error.OutOfMemory,
11369 error.ReinterpretDeclRef, error.Unimplemented, error.IllDefinedMemoryLayout => return false,11369 error.ReinterpretDeclRef, error.Unimplemented, error.IllDefinedMemoryLayout => return false,
11370 };11370 };
src/codegen/c.zig+12-12
...@@ -435,8 +435,7 @@ pub const Function = struct {...@@ -435,8 +435,7 @@ pub const Function = struct {
435 fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue {435 fn resolveInst(f: *Function, ref: Air.Inst.Ref) !CValue {
436 const gop = try f.value_map.getOrPut(ref);436 const gop = try f.value_map.getOrPut(ref);
437 if (!gop.found_existing) {437 if (!gop.found_existing) {
438 const val = try f.air.value(ref, f.dg.pt);438 gop.value_ptr.* = .{ .constant = .fromInterned(ref.toInterned().?) };
439 gop.value_ptr.* = .{ .constant = val.? };
440 }439 }
441 return gop.value_ptr.*;440 return gop.value_ptr.*;
442 }441 }
...@@ -3389,7 +3388,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -3389,7 +3388,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
3389 const ptr_val = try f.resolveInst(bin_op.lhs);3388 const ptr_val = try f.resolveInst(bin_op.lhs);
3390 const src_ty = f.typeOf(bin_op.rhs);3389 const src_ty = f.typeOf(bin_op.rhs);
33913390
3392 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndef(zcu) else false;3391 const val_is_undef = if (bin_op.rhs.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
33933392
3394 const w = &f.code.writer;3393 const w = &f.code.writer;
3395 if (val_is_undef) {3394 if (val_is_undef) {
...@@ -3922,8 +3921,8 @@ fn airCall(...@@ -3922,8 +3921,8 @@ fn airCall(
39223921
3923 callee: {3922 callee: {
3924 known: {3923 known: {
3925 const callee_val = (try f.air.value(call.callee, pt)) orelse break :known;3924 const callee_ip_index = call.callee.toInterned() orelse break :known;
3926 const fn_nav, const need_cast = switch (ip.indexToKey(callee_val.toIntern())) {3925 const fn_nav, const need_cast = switch (ip.indexToKey(callee_ip_index)) {
3927 .@"extern" => |@"extern"| .{ @"extern".owner_nav, false },3926 .@"extern" => |@"extern"| .{ @"extern".owner_nav, false },
3928 .func => |func| .{ func.owner_nav, Type.fromInterned(func.ty).fnCallingConvention(zcu) != .naked and3927 .func => |func| .{ func.owner_nav, Type.fromInterned(func.ty).fnCallingConvention(zcu) != .naked and
3929 Type.fromInterned(func.uncoerced_ty).fnCallingConvention(zcu) == .naked },3928 Type.fromInterned(func.uncoerced_ty).fnCallingConvention(zcu) == .naked },
...@@ -4027,7 +4026,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -4027,7 +4026,7 @@ fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue {
4027 const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)];4026 const tag = f.air.instructions.items(.tag)[@intFromEnum(inst)];
4028 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;4027 const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4029 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);4028 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
4030 const operand_is_undef = if (try f.air.value(pl_op.operand, pt)) |v| v.isUndef(zcu) else false;4029 const operand_is_undef = if (pl_op.operand.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
4031 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);4030 if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand);
40324031
4033 try reap(f, inst, &.{pl_op.operand});4032 try reap(f, inst, &.{pl_op.operand});
...@@ -4204,7 +4203,8 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {...@@ -4204,7 +4203,8 @@ fn airSwitchDispatch(f: *Function, inst: Air.Inst.Index) !void {
4204 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;4203 const br = f.air.instructions.items(.data)[@intFromEnum(inst)].br;
4205 const w = &f.code.writer;4204 const w = &f.code.writer;
42064205
4207 if (try f.air.value(br.operand, pt)) |cond_val| {4206 if (br.operand.toInterned()) |cond_ip_index| {
4207 const cond_val: Value = .fromInterned(cond_ip_index);
4208 // Comptime-known dispatch. Iterate the cases to find the correct4208 // Comptime-known dispatch. Iterate the cases to find the correct
4209 // one, and branch directly to the corresponding case.4209 // one, and branch directly to the corresponding case.
4210 const switch_br = f.air.unwrapSwitch(br.block_inst);4210 const switch_br = f.air.unwrapSwitch(br.block_inst);
...@@ -4539,12 +4539,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void...@@ -4539,12 +4539,12 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
4539 try f.writeCValue(w, cond_val, .other);4539 try f.writeCValue(w, cond_val, .other);
4540 try w.writeAll(", ");4540 try w.writeAll(", ");
4541 }4541 }
4542 const item_value = try f.air.value(item, pt);4542 const item_value: Value = .fromInterned(item.toInterned().?);
4543 // If `item_value` is a pointer with a known integer address, print the address4543 // If `item_value` is a pointer with a known integer address, print the address
4544 // with no cast to avoid a warning.4544 // with no cast to avoid a warning.
4545 write_val: {4545 write_val: {
4546 if (cond_ty.zigTypeTag(zcu) == .pointer) {4546 if (cond_ty.zigTypeTag(zcu) == .pointer) {
4547 if (item_value.?.getUnsignedInt(zcu)) |item_int| {4547 if (item_value.getUnsignedInt(zcu)) |item_int| {
4548 try w.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_cond_ty, item_int))});4548 try w.print("{f}", .{try f.fmtIntLiteralDec(try pt.intValue(lowered_cond_ty, item_int))});
4549 break :write_val;4549 break :write_val;
4550 }4550 }
...@@ -4552,7 +4552,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void...@@ -4552,7 +4552,7 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
4552 try f.renderType(w, .usize);4552 try f.renderType(w, .usize);
4553 try w.writeByte(')');4553 try w.writeByte(')');
4554 }4554 }
4555 try f.dg.renderValue(w, (try f.air.value(item, pt)).?, .other);4555 try f.dg.renderValue(w, .fromInterned(item.toInterned().?), .other);
4556 }4556 }
4557 switch (cond_cint) {4557 switch (cond_cint) {
4558 .zig_u128, .zig_i128 => try w.writeByte(')'),4558 .zig_u128, .zig_i128 => try w.writeByte(')'),
...@@ -4710,7 +4710,7 @@ fn lowerSwitchCmp(...@@ -4710,7 +4710,7 @@ fn lowerSwitchCmp(
4710 try f.writeCValue(w, cond_val, .other);4710 try f.writeCValue(w, cond_val, .other);
4711 try w.writeAll(if (use_builtin) ", " else compareOperatorC(operator));4711 try w.writeAll(if (use_builtin) ", " else compareOperatorC(operator));
4712 if (class == .big) try w.writeByte('&');4712 if (class == .big) try w.writeByte('&');
4713 try f.dg.renderValue(w, (try f.air.value(case_inst, pt)).?, .other);4713 try f.dg.renderValue(w, .fromInterned(case_inst.toInterned().?), .other);
4714 if (use_builtin) {4714 if (use_builtin) {
4715 try f.dg.renderBuiltinInfo(w, ty, if (class == .big) .bits else .none);4715 try f.dg.renderBuiltinInfo(w, ty, if (class == .big) .bits else .none);
4716 try w.writeByte(')');4716 try w.writeByte(')');
...@@ -6100,7 +6100,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {...@@ -6100,7 +6100,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue {
6100 const value = try f.resolveInst(bin_op.rhs);6100 const value = try f.resolveInst(bin_op.rhs);
6101 const elem_ty = f.typeOf(bin_op.rhs);6101 const elem_ty = f.typeOf(bin_op.rhs);
6102 const elem_abi_size = elem_ty.abiSize(zcu);6102 const elem_abi_size = elem_ty.abiSize(zcu);
6103 const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false;6103 const val_is_undef = if (bin_op.rhs.toInterned()) |ip_index| Value.fromInterned(ip_index).isUndef(zcu) else false;
6104 const w = &f.code.writer;6104 const w = &f.code.writer;
61056105
6106 if (val_is_undef) {6106 if (val_is_undef) {
src/codegen/llvm.zig+207-222
...@@ -696,11 +696,11 @@ pub const Object = struct {...@@ -696,11 +696,11 @@ pub const Object = struct {
696 self.* = undefined;696 self.* = undefined;
697 }697 }
698698
699 fn genErrorNameTable(o: *Object, pt: Zcu.PerThread) Allocator.Error!void {699 fn genErrorNameTable(o: *Object) Allocator.Error!void {
700 // If o.error_name_table is null, then it was not referenced by any instructions.700 // If o.error_name_table is null, then it was not referenced by any instructions.
701 if (o.error_name_table == .none) return;701 if (o.error_name_table == .none) return;
702702
703 const zcu = pt.zcu;703 const zcu = o.zcu;
704 const ip = &zcu.intern_pool;704 const ip = &zcu.intern_pool;
705705
706 const error_name_list = ip.global_error_set.getNamesFromMainThread();706 const error_name_list = ip.global_error_set.getNamesFromMainThread();
...@@ -709,8 +709,8 @@ pub const Object = struct {...@@ -709,8 +709,8 @@ pub const Object = struct {
709709
710 // TODO: Address space710 // TODO: Address space
711 const slice_ty = Type.slice_const_u8_sentinel_0;711 const slice_ty = Type.slice_const_u8_sentinel_0;
712 const llvm_usize_ty = try o.lowerType(pt, Type.usize);712 const llvm_usize_ty = try o.lowerType(.usize);
713 const llvm_slice_ty = try o.lowerType(pt, slice_ty);713 const llvm_slice_ty = try o.lowerType(slice_ty);
714 const llvm_table_ty = try o.builder.arrayType(1 + error_name_list.len, llvm_slice_ty);714 const llvm_table_ty = try o.builder.arrayType(1 + error_name_list.len, llvm_slice_ty);
715715
716 llvm_errors[0] = try o.builder.undefConst(llvm_slice_ty);716 llvm_errors[0] = try o.builder.undefConst(llvm_slice_ty);
...@@ -768,7 +768,7 @@ pub const Object = struct {...@@ -768,7 +768,7 @@ pub const Object = struct {
768 };768 };
769769
770 pub fn emit(o: *Object, pt: Zcu.PerThread, options: EmitOptions) error{ LinkFailure, OutOfMemory }!void {770 pub fn emit(o: *Object, pt: Zcu.PerThread, options: EmitOptions) error{ LinkFailure, OutOfMemory }!void {
771 const zcu = pt.zcu;771 const zcu = o.zcu;
772 const comp = zcu.comp;772 const comp = zcu.comp;
773 const io = comp.io;773 const io = comp.io;
774 const diags = &comp.link_diags;774 const diags = &comp.link_diags;
...@@ -779,7 +779,7 @@ pub const Object = struct {...@@ -779,7 +779,7 @@ pub const Object = struct {
779 const init_val = try o.builder.intConst(try o.errorIntType(), errors_len);779 const init_val = try o.builder.intConst(try o.errorIntType(), errors_len);
780 try o.errors_len_variable.setInitializer(init_val, &o.builder);780 try o.errors_len_variable.setInitializer(init_val, &o.builder);
781 }781 }
782 try o.genErrorNameTable(pt);782 try o.genErrorNameTable();
783 try o.genModuleLevelAssembly();783 try o.genModuleLevelAssembly();
784784
785 if (o.used.items.len > 0) {785 if (o.used.items.len > 0) {
...@@ -1139,7 +1139,7 @@ pub const Object = struct {...@@ -1139,7 +1139,7 @@ pub const Object = struct {
1139 air: *const Air,1139 air: *const Air,
1140 liveness: *const ?Air.Liveness,1140 liveness: *const ?Air.Liveness,
1141 ) !void {1141 ) !void {
1142 const zcu = pt.zcu;1142 const zcu = o.zcu;
1143 const comp = zcu.comp;1143 const comp = zcu.comp;
1144 const ip = &zcu.intern_pool;1144 const ip = &zcu.intern_pool;
1145 const func = zcu.funcInfo(func_index);1145 const func = zcu.funcInfo(func_index);
...@@ -1150,7 +1150,7 @@ pub const Object = struct {...@@ -1150,7 +1150,7 @@ pub const Object = struct {
1150 const fn_info = zcu.typeToFunc(fn_ty).?;1150 const fn_info = zcu.typeToFunc(fn_ty).?;
1151 const target = &owner_mod.resolved_target.result;1151 const target = &owner_mod.resolved_target.result;
11521152
1153 const function_index = try o.resolveLlvmFunction(pt, func.owner_nav);1153 const function_index = try o.resolveLlvmFunction(func.owner_nav);
11541154
1155 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);1155 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);
1156 defer attributes.deinit(&o.builder);1156 defer attributes.deinit(&o.builder);
...@@ -1262,7 +1262,7 @@ pub const Object = struct {...@@ -1262,7 +1262,7 @@ pub const Object = struct {
1262 defer args.deinit(gpa);1262 defer args.deinit(gpa);
12631263
1264 {1264 {
1265 var it = iterateParamTypes(o, pt, fn_info);1265 var it = iterateParamTypes(o, fn_info);
1266 while (try it.next()) |lowering| {1266 while (try it.next()) |lowering| {
1267 try args.ensureUnusedCapacity(gpa, 1);1267 try args.ensureUnusedCapacity(gpa, 1);
12681268
...@@ -1289,7 +1289,7 @@ pub const Object = struct {...@@ -1289,7 +1289,7 @@ pub const Object = struct {
1289 },1289 },
1290 .byref => {1290 .byref => {
1291 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1291 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1292 const param_llvm_ty = try o.lowerType(pt, param_ty);1292 const param_llvm_ty = try o.lowerType(param_ty);
1293 const param = wip.arg(llvm_arg_i);1293 const param = wip.arg(llvm_arg_i);
1294 const alignment = param_ty.abiAlignment(zcu).toLlvm();1294 const alignment = param_ty.abiAlignment(zcu).toLlvm();
12951295
...@@ -1304,7 +1304,7 @@ pub const Object = struct {...@@ -1304,7 +1304,7 @@ pub const Object = struct {
1304 },1304 },
1305 .byref_mut => {1305 .byref_mut => {
1306 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1306 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1307 const param_llvm_ty = try o.lowerType(pt, param_ty);1307 const param_llvm_ty = try o.lowerType(param_ty);
1308 const param = wip.arg(llvm_arg_i);1308 const param = wip.arg(llvm_arg_i);
1309 const alignment = param_ty.abiAlignment(zcu).toLlvm();1309 const alignment = param_ty.abiAlignment(zcu).toLlvm();
13101310
...@@ -1323,7 +1323,7 @@ pub const Object = struct {...@@ -1323,7 +1323,7 @@ pub const Object = struct {
1323 const param = wip.arg(llvm_arg_i);1323 const param = wip.arg(llvm_arg_i);
1324 llvm_arg_i += 1;1324 llvm_arg_i += 1;
13251325
1326 const param_llvm_ty = try o.lowerType(pt, param_ty);1326 const param_llvm_ty = try o.lowerType(param_ty);
1327 const alignment = param_ty.abiAlignment(zcu).toLlvm();1327 const alignment = param_ty.abiAlignment(zcu).toLlvm();
1328 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target);1328 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target);
1329 _ = try wip.store(.normal, param, arg_ptr, alignment);1329 _ = try wip.store(.normal, param, arg_ptr, alignment);
...@@ -1362,7 +1362,7 @@ pub const Object = struct {...@@ -1362,7 +1362,7 @@ pub const Object = struct {
1362 const len_param = wip.arg(llvm_arg_i);1362 const len_param = wip.arg(llvm_arg_i);
1363 llvm_arg_i += 1;1363 llvm_arg_i += 1;
13641364
1365 const slice_llvm_ty = try o.lowerType(pt, param_ty);1365 const slice_llvm_ty = try o.lowerType(param_ty);
1366 args.appendAssumeCapacity(1366 args.appendAssumeCapacity(
1367 try wip.buildAggregate(slice_llvm_ty, &.{ ptr_param, len_param }, ""),1367 try wip.buildAggregate(slice_llvm_ty, &.{ ptr_param, len_param }, ""),
1368 );1368 );
...@@ -1371,7 +1371,7 @@ pub const Object = struct {...@@ -1371,7 +1371,7 @@ pub const Object = struct {
1371 assert(!it.byval_attr);1371 assert(!it.byval_attr);
1372 const field_types = it.types_buffer[0..it.types_len];1372 const field_types = it.types_buffer[0..it.types_len];
1373 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1373 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1374 const param_llvm_ty = try o.lowerType(pt, param_ty);1374 const param_llvm_ty = try o.lowerType(param_ty);
1375 const param_alignment = param_ty.abiAlignment(zcu).toLlvm();1375 const param_alignment = param_ty.abiAlignment(zcu).toLlvm();
1376 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, param_alignment, target);1376 const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, param_alignment, target);
1377 const llvm_ty = try o.builder.structType(.normal, field_types);1377 const llvm_ty = try o.builder.structType(.normal, field_types);
...@@ -1391,7 +1391,7 @@ pub const Object = struct {...@@ -1391,7 +1391,7 @@ pub const Object = struct {
1391 },1391 },
1392 .float_array => {1392 .float_array => {
1393 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1393 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1394 const param_llvm_ty = try o.lowerType(pt, param_ty);1394 const param_llvm_ty = try o.lowerType(param_ty);
1395 const param = wip.arg(llvm_arg_i);1395 const param = wip.arg(llvm_arg_i);
1396 llvm_arg_i += 1;1396 llvm_arg_i += 1;
13971397
...@@ -1406,7 +1406,7 @@ pub const Object = struct {...@@ -1406,7 +1406,7 @@ pub const Object = struct {
1406 },1406 },
1407 .i32_array, .i64_array => {1407 .i32_array, .i64_array => {
1408 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1408 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1409 const param_llvm_ty = try o.lowerType(pt, param_ty);1409 const param_llvm_ty = try o.lowerType(param_ty);
1410 const param = wip.arg(llvm_arg_i);1410 const param = wip.arg(llvm_arg_i);
1411 llvm_arg_i += 1;1411 llvm_arg_i += 1;
14121412
...@@ -1560,7 +1560,7 @@ pub const Object = struct {...@@ -1560,7 +1560,7 @@ pub const Object = struct {
1560 }1560 }
15611561
1562 pub fn updateNav(o: *Object, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void {1562 pub fn updateNav(o: *Object, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !void {
1563 const zcu = pt.zcu;1563 const zcu = o.zcu;
1564 const ip = &zcu.intern_pool;1564 const ip = &zcu.intern_pool;
15651565
1566 const nav = ip.getNav(nav_index);1566 const nav = ip.getNav(nav_index);
...@@ -1573,12 +1573,12 @@ pub const Object = struct {...@@ -1573,12 +1573,12 @@ pub const Object = struct {
1573 const ty: Type = .fromInterned(nav.resolved.?.type);1573 const ty: Type = .fromInterned(nav.resolved.?.type);
15741574
1575 if (linkage != .internal and ip.isFunctionType(ty.toIntern())) {1575 if (linkage != .internal and ip.isFunctionType(ty.toIntern())) {
1576 const function_index = try o.resolveLlvmFunction(pt, owner_nav);1576 const function_index = try o.resolveLlvmFunction(owner_nav);
1577 // Add parameter attributes which weren't set by `resolveLlvmFunction`1577 // Add parameter attributes which weren't set by `resolveLlvmFunction`
1578 const fn_info = zcu.typeToFunc(ty).?;1578 const fn_info = zcu.typeToFunc(ty).?;
1579 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);1579 var attributes = try function_index.ptrConst(&o.builder).attributes.toWip(&o.builder);
1580 defer attributes.deinit(&o.builder);1580 defer attributes.deinit(&o.builder);
1581 var it = iterateParamTypes(o, pt, fn_info);1581 var it = iterateParamTypes(o, fn_info);
1582 if (firstParamSRet(fn_info, zcu, zcu.getTarget())) it.llvm_index += 1;1582 if (firstParamSRet(fn_info, zcu, zcu.getTarget())) it.llvm_index += 1;
1583 if (fn_info.cc == .auto and zcu.comp.config.any_error_tracing) it.llvm_index += 1;1583 if (fn_info.cc == .auto and zcu.comp.config.any_error_tracing) it.llvm_index += 1;
1584 while (try it.next()) |lowering| switch (lowering) {1584 while (try it.next()) |lowering| switch (lowering) {
...@@ -1591,7 +1591,7 @@ pub const Object = struct {...@@ -1591,7 +1591,7 @@ pub const Object = struct {
1591 },1591 },
1592 .byref => {1592 .byref => {
1593 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);1593 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
1594 const param_llvm_ty = try o.lowerType(pt, param_ty);1594 const param_llvm_ty = try o.lowerType(param_ty);
1595 const alignment = param_ty.abiAlignment(zcu);1595 const alignment = param_ty.abiAlignment(zcu);
1596 try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment.toLlvm(), it.byval_attr, param_llvm_ty);1596 try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment.toLlvm(), it.byval_attr, param_llvm_ty);
1597 },1597 },
...@@ -1609,14 +1609,14 @@ pub const Object = struct {...@@ -1609,14 +1609,14 @@ pub const Object = struct {
1609 };1609 };
1610 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);1610 function_index.setAttributes(try attributes.finish(&o.builder), &o.builder);
1611 } else {1611 } else {
1612 const variable_index = try o.resolveGlobalNav(pt, nav_index);1612 const variable_index = try o.resolveGlobalNav(nav_index);
1613 variable_index.setAlignment(zcu.navAlignment(nav_index).toLlvm(), &o.builder);1613 variable_index.setAlignment(zcu.navAlignment(nav_index).toLlvm(), &o.builder);
1614 if (resolved.@"linksection".toSlice(ip)) |section|1614 if (resolved.@"linksection".toSlice(ip)) |section|
1615 variable_index.setSection(try o.builder.string(section), &o.builder);1615 variable_index.setSection(try o.builder.string(section), &o.builder);
1616 if (resolved.@"const") variable_index.setMutability(.constant, &o.builder);1616 if (resolved.@"const") variable_index.setMutability(.constant, &o.builder);
1617 try variable_index.setInitializer(switch (init_val) {1617 try variable_index.setInitializer(switch (init_val) {
1618 .none => .no_init,1618 .none => .no_init,
1619 else => try o.lowerValue(pt, init_val),1619 else => try o.lowerValue(init_val),
1620 }, &o.builder);1620 }, &o.builder);
1621 variable_index.setVisibility(visibility, &o.builder);1621 variable_index.setVisibility(visibility, &o.builder);
16221622
...@@ -1703,14 +1703,13 @@ pub const Object = struct {...@@ -1703,14 +1703,13 @@ pub const Object = struct {
17031703
1704 pub fn updateExports(1704 pub fn updateExports(
1705 self: *Object,1705 self: *Object,
1706 pt: Zcu.PerThread,
1707 exported: Zcu.Exported,1706 exported: Zcu.Exported,
1708 export_indices: []const Zcu.Export.Index,1707 export_indices: []const Zcu.Export.Index,
1709 ) link.File.UpdateExportsError!void {1708 ) link.File.UpdateExportsError!void {
1710 const zcu = pt.zcu;1709 const zcu = self.zcu;
1711 const nav_index = switch (exported) {1710 const nav_index = switch (exported) {
1712 .nav => |nav| nav,1711 .nav => |nav| nav,
1713 .uav => |uav| return updateExportedValue(self, pt, uav, export_indices),1712 .uav => |uav| return updateExportedValue(self, uav, export_indices),
1714 };1713 };
1715 const ip = &zcu.intern_pool;1714 const ip = &zcu.intern_pool;
1716 const global_index = self.nav_map.get(nav_index).?;1715 const global_index = self.nav_map.get(nav_index).?;
...@@ -1751,11 +1750,10 @@ pub const Object = struct {...@@ -1751,11 +1750,10 @@ pub const Object = struct {
17511750
1752 fn updateExportedValue(1751 fn updateExportedValue(
1753 o: *Object,1752 o: *Object,
1754 pt: Zcu.PerThread,
1755 exported_value: InternPool.Index,1753 exported_value: InternPool.Index,
1756 export_indices: []const Zcu.Export.Index,1754 export_indices: []const Zcu.Export.Index,
1757 ) link.File.UpdateExportsError!void {1755 ) link.File.UpdateExportsError!void {
1758 const zcu = pt.zcu;1756 const zcu = o.zcu;
1759 const gpa = zcu.gpa;1757 const gpa = zcu.gpa;
1760 const ip = &zcu.intern_pool;1758 const ip = &zcu.intern_pool;
1761 const main_exp_name = try o.builder.strtabString(export_indices[0].ptr(zcu).opts.name.toSlice(ip));1759 const main_exp_name = try o.builder.strtabString(export_indices[0].ptr(zcu).opts.name.toSlice(ip));
...@@ -1769,13 +1767,13 @@ pub const Object = struct {...@@ -1769,13 +1767,13 @@ pub const Object = struct {
1769 const llvm_addr_space = toLlvmAddressSpace(.generic, zcu.getTarget());1767 const llvm_addr_space = toLlvmAddressSpace(.generic, zcu.getTarget());
1770 const variable_index = try o.builder.addVariable(1768 const variable_index = try o.builder.addVariable(
1771 main_exp_name,1769 main_exp_name,
1772 try o.lowerType(pt, Type.fromInterned(ip.typeOf(exported_value))),1770 try o.lowerType(.fromInterned(ip.typeOf(exported_value))),
1773 llvm_addr_space,1771 llvm_addr_space,
1774 );1772 );
1775 const global_index = variable_index.ptrConst(&o.builder).global;1773 const global_index = variable_index.ptrConst(&o.builder).global;
1776 gop.value_ptr.* = global_index;1774 gop.value_ptr.* = global_index;
1777 // This line invalidates `gop`.1775 // This line invalidates `gop`.
1778 const init_val = try o.lowerValue(pt, exported_value);1776 const init_val = try o.lowerValue(exported_value);
1779 try variable_index.setInitializer(init_val, &o.builder);1777 try variable_index.setInitializer(init_val, &o.builder);
1780 break :i global_index;1778 break :i global_index;
1781 };1779 };
...@@ -1894,7 +1892,7 @@ pub const Object = struct {...@@ -1894,7 +1892,7 @@ pub const Object = struct {
1894 pub fn updateContainerType(o: *Object, pt: Zcu.PerThread, ty: InternPool.Index, success: bool) Allocator.Error!void {1892 pub fn updateContainerType(o: *Object, pt: Zcu.PerThread, ty: InternPool.Index, success: bool) Allocator.Error!void {
1895 try o.type_pool.updateContainerType(pt, .{ .llvm = o }, ty, success);1893 try o.type_pool.updateContainerType(pt, .{ .llvm = o }, ty, success);
1896 if (o.named_enum_map.get(ty)) |function_index| {1894 if (o.named_enum_map.get(ty)) |function_index| {
1897 try o.updateIsNamedEnumValueFunction(pt, .fromInterned(ty), function_index);1895 try o.updateIsNamedEnumValueFunction(.fromInterned(ty), function_index);
1898 }1896 }
1899 }1897 }
19001898
...@@ -1902,7 +1900,8 @@ pub const Object = struct {...@@ -1902,7 +1900,8 @@ pub const Object = struct {
1902 ///1900 ///
1903 /// `val` is always a type because `o.type_pool` only contains types.1901 /// `val` is always a type because `o.type_pool` only contains types.
1904 pub fn addConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {1902 pub fn addConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1905 const zcu = pt.zcu;1903 _ = pt;
1904 const zcu = o.zcu;
1906 const gpa = zcu.comp.gpa;1905 const gpa = zcu.comp.gpa;
1907 assert(zcu.intern_pool.typeOf(val) == .type_type);1906 assert(zcu.intern_pool.typeOf(val) == .type_type);
19081907
...@@ -1928,7 +1927,7 @@ pub const Object = struct {...@@ -1928,7 +1927,7 @@ pub const Object = struct {
1928 ///1927 ///
1929 /// `val` is always a type because `o.type_pool` only contains types.1928 /// `val` is always a type because `o.type_pool` only contains types.
1930 pub fn updateConstIncomplete(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {1929 pub fn updateConstIncomplete(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1931 const zcu = pt.zcu;1930 const zcu = o.zcu;
1932 assert(zcu.intern_pool.typeOf(val) == .type_type);1931 assert(zcu.intern_pool.typeOf(val) == .type_type);
19331932
1934 const ty: Type = .fromInterned(val);1933 const ty: Type = .fromInterned(val);
...@@ -1950,7 +1949,7 @@ pub const Object = struct {...@@ -1950,7 +1949,7 @@ pub const Object = struct {
1950 ///1949 ///
1951 /// `val` is always a type because `o.type_pool` only contains types.1950 /// `val` is always a type because `o.type_pool` only contains types.
1952 pub fn updateConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {1951 pub fn updateConst(o: *Object, pt: Zcu.PerThread, index: link.ConstPool.Index, val: InternPool.Index) Allocator.Error!void {
1953 const zcu = pt.zcu;1952 const zcu = o.zcu;
1954 assert(zcu.intern_pool.typeOf(val) == .type_type);1953 assert(zcu.intern_pool.typeOf(val) == .type_type);
19551954
1956 const ty: Type = .fromInterned(val);1955 const ty: Type = .fromInterned(val);
...@@ -2005,7 +2004,7 @@ pub const Object = struct {...@@ -2005,7 +2004,7 @@ pub const Object = struct {
2005 assert(!o.builder.strip);2004 assert(!o.builder.strip);
20062005
2007 const gpa = o.gpa;2006 const gpa = o.gpa;
2008 const zcu = pt.zcu;2007 const zcu = o.zcu;
2009 const target = zcu.getTarget();2008 const target = zcu.getTarget();
2010 const ip = &zcu.intern_pool;2009 const ip = &zcu.intern_pool;
20112010
...@@ -2712,30 +2711,20 @@ pub const Object = struct {...@@ -2712,30 +2711,20 @@ pub const Object = struct {
2712 }2711 }
27132712
2714 fn namespaceToDebugScope(o: *Object, pt: Zcu.PerThread, namespace_index: InternPool.NamespaceIndex) !Builder.Metadata {2713 fn namespaceToDebugScope(o: *Object, pt: Zcu.PerThread, namespace_index: InternPool.NamespaceIndex) !Builder.Metadata {
2715 const zcu = pt.zcu;2714 const zcu = o.zcu;
2716 const namespace = zcu.namespacePtr(namespace_index);2715 const namespace = zcu.namespacePtr(namespace_index);
2717 if (namespace.parent == .none) return try o.getDebugFile(namespace.file_scope);2716 if (namespace.parent == .none) return try o.getDebugFile(namespace.file_scope);
2718 return o.getDebugType(pt, .fromInterned(namespace.owner_type));2717 return o.getDebugType(pt, .fromInterned(namespace.owner_type));
2719 }2718 }
27202719
2721 fn allocTypeName(o: *Object, pt: Zcu.PerThread, ty: Type) Allocator.Error![:0]const u8 {
2722 var aw: Io.Writer.Allocating = .init(o.gpa);
2723 defer aw.deinit();
2724 ty.print(&aw.writer, pt, null) catch |err| switch (err) {
2725 error.WriteFailed => return error.OutOfMemory,
2726 };
2727 return aw.toOwnedSliceSentinel(0);
2728 }
2729
2730 /// If the llvm function does not exist, create it.2720 /// If the llvm function does not exist, create it.
2731 /// Note that this can be called before the function's semantic analysis has2721 /// Note that this can be called before the function's semantic analysis has
2732 /// completed, so if any attributes rely on that, they must be done in updateFunc, not here.2722 /// completed, so if any attributes rely on that, they must be done in updateFunc, not here.
2733 pub fn resolveLlvmFunction(2723 pub fn resolveLlvmFunction(
2734 o: *Object,2724 o: *Object,
2735 pt: Zcu.PerThread,
2736 nav_index: InternPool.Nav.Index,2725 nav_index: InternPool.Nav.Index,
2737 ) Allocator.Error!Builder.Function.Index {2726 ) Allocator.Error!Builder.Function.Index {
2738 const zcu = pt.zcu;2727 const zcu = o.zcu;
2739 const ip = &zcu.intern_pool;2728 const ip = &zcu.intern_pool;
2740 const gpa = o.gpa;2729 const gpa = o.gpa;
2741 const nav = ip.getNav(nav_index);2730 const nav = ip.getNav(nav_index);
...@@ -2752,7 +2741,7 @@ pub const Object = struct {...@@ -2752,7 +2741,7 @@ pub const Object = struct {
2752 else2741 else
2753 .{ false, .none };2742 .{ false, .none };
2754 const function_index = try o.builder.addFunction(2743 const function_index = try o.builder.addFunction(
2755 try o.lowerType(pt, ty),2744 try o.lowerType(ty),
2756 try o.builder.strtabString((if (is_extern) nav.name else nav.fqn).toSlice(ip)),2745 try o.builder.strtabString((if (is_extern) nav.name else nav.fqn).toSlice(ip)),
2757 toLlvmAddressSpace(nav.resolved.?.@"addrspace", target),2746 toLlvmAddressSpace(nav.resolved.?.@"addrspace", target),
2758 );2747 );
...@@ -2785,7 +2774,7 @@ pub const Object = struct {...@@ -2785,7 +2774,7 @@ pub const Object = struct {
2785 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);2774 try attributes.addParamAttr(llvm_arg_i, .nonnull, &o.builder);
2786 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);2775 try attributes.addParamAttr(llvm_arg_i, .@"noalias", &o.builder);
27872776
2788 const raw_llvm_ret_ty = try o.lowerType(pt, Type.fromInterned(fn_info.return_type));2777 const raw_llvm_ret_ty = try o.lowerType(.fromInterned(fn_info.return_type));
2789 try attributes.addParamAttr(llvm_arg_i, .{ .sret = raw_llvm_ret_ty }, &o.builder);2778 try attributes.addParamAttr(llvm_arg_i, .{ .sret = raw_llvm_ret_ty }, &o.builder);
27902779
2791 llvm_arg_i += 1;2780 llvm_arg_i += 1;
...@@ -2965,7 +2954,6 @@ pub const Object = struct {...@@ -2965,7 +2954,6 @@ pub const Object = struct {
29652954
2966 fn resolveGlobalUav(2955 fn resolveGlobalUav(
2967 o: *Object,2956 o: *Object,
2968 pt: Zcu.PerThread,
2969 uav: InternPool.Index,2957 uav: InternPool.Index,
2970 llvm_addr_space: Builder.AddrSpace,2958 llvm_addr_space: Builder.AddrSpace,
2971 alignment: InternPool.Alignment,2959 alignment: InternPool.Alignment,
...@@ -2983,17 +2971,17 @@ pub const Object = struct {...@@ -2983,17 +2971,17 @@ pub const Object = struct {
2983 }2971 }
2984 errdefer assert(o.uav_map.remove(uav));2972 errdefer assert(o.uav_map.remove(uav));
29852973
2986 const zcu = pt.zcu;2974 const zcu = o.zcu;
2987 const decl_ty = zcu.intern_pool.typeOf(uav);2975 const decl_ty = zcu.intern_pool.typeOf(uav);
29882976
2989 const variable_index = try o.builder.addVariable(2977 const variable_index = try o.builder.addVariable(
2990 try o.builder.strtabStringFmt("__anon_{d}", .{@intFromEnum(uav)}),2978 try o.builder.strtabStringFmt("__anon_{d}", .{@intFromEnum(uav)}),
2991 try o.lowerType(pt, Type.fromInterned(decl_ty)),2979 try o.lowerType(.fromInterned(decl_ty)),
2992 llvm_addr_space,2980 llvm_addr_space,
2993 );2981 );
2994 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;2982 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;
29952983
2996 try variable_index.setInitializer(try o.lowerValue(pt, uav), &o.builder);2984 try variable_index.setInitializer(try o.lowerValue(uav), &o.builder);
2997 variable_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);2985 variable_index.setLinkage(if (o.builder.strip) .private else .internal, &o.builder);
2998 variable_index.setMutability(.constant, &o.builder);2986 variable_index.setMutability(.constant, &o.builder);
2999 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);2987 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
...@@ -3003,14 +2991,13 @@ pub const Object = struct {...@@ -3003,14 +2991,13 @@ pub const Object = struct {
30032991
3004 fn resolveGlobalNav(2992 fn resolveGlobalNav(
3005 o: *Object,2993 o: *Object,
3006 pt: Zcu.PerThread,
3007 nav_index: InternPool.Nav.Index,2994 nav_index: InternPool.Nav.Index,
3008 ) Allocator.Error!Builder.Variable.Index {2995 ) Allocator.Error!Builder.Variable.Index {
3009 const gop = try o.nav_map.getOrPut(o.gpa, nav_index);2996 const gop = try o.nav_map.getOrPut(o.gpa, nav_index);
3010 if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable;2997 if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable;
3011 errdefer assert(o.nav_map.remove(nav_index));2998 errdefer assert(o.nav_map.remove(nav_index));
30122999
3013 const zcu = pt.zcu;3000 const zcu = o.zcu;
3014 const ip = &zcu.intern_pool;3001 const ip = &zcu.intern_pool;
3015 const nav = ip.getNav(nav_index);3002 const nav = ip.getNav(nav_index);
3016 const linkage: std.builtin.GlobalLinkage, const visibility: Builder.Visibility, const is_dll_import: bool = switch (nav.resolved.?.value) {3003 const linkage: std.builtin.GlobalLinkage, const visibility: Builder.Visibility, const is_dll_import: bool = switch (nav.resolved.?.value) {
...@@ -3027,7 +3014,7 @@ pub const Object = struct {...@@ -3027,7 +3014,7 @@ pub const Object = struct {
3027 .strong, .weak => nav.name,3014 .strong, .weak => nav.name,
3028 .link_once => unreachable,3015 .link_once => unreachable,
3029 }.toSlice(ip)),3016 }.toSlice(ip)),
3030 try o.lowerType(pt, .fromInterned(nav.resolved.?.type)),3017 try o.lowerType(.fromInterned(nav.resolved.?.type)),
3031 toLlvmGlobalAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget()),3018 toLlvmGlobalAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget()),
3032 );3019 );
3033 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;3020 gop.value_ptr.* = variable_index.ptrConst(&o.builder).global;
...@@ -3060,8 +3047,8 @@ pub const Object = struct {...@@ -3060,8 +3047,8 @@ pub const Object = struct {
3060 return o.builder.intType(o.zcu.errorSetBits());3047 return o.builder.intType(o.zcu.errorSetBits());
3061 }3048 }
30623049
3063 pub fn lowerType(o: *Object, pt: Zcu.PerThread, t: Type) Allocator.Error!Builder.Type {3050 pub fn lowerType(o: *Object, t: Type) Allocator.Error!Builder.Type {
3064 const zcu = pt.zcu;3051 const zcu = o.zcu;
3065 const target = zcu.getTarget();3052 const target = zcu.getTarget();
3066 const ip = &zcu.intern_pool;3053 const ip = &zcu.intern_pool;
3067 return switch (t.toIntern()) {3054 return switch (t.toIntern()) {
...@@ -3134,7 +3121,7 @@ pub const Object = struct {...@@ -3134,7 +3121,7 @@ pub const Object = struct {
3134 => .ptr,3121 => .ptr,
3135 .slice_const_u8_type,3122 .slice_const_u8_type,
3136 .slice_const_u8_sentinel_0_type,3123 .slice_const_u8_sentinel_0_type,
3137 => try o.builder.structType(.normal, &.{ .ptr, try o.lowerType(pt, Type.usize) }),3124 => try o.builder.structType(.normal, &.{ .ptr, try o.lowerType(.usize) }),
3138 .optional_noreturn_type => unreachable,3125 .optional_noreturn_type => unreachable,
3139 .anyerror_void_error_union_type,3126 .anyerror_void_error_union_type,
3140 .adhoc_inferred_error_set_type,3127 .adhoc_inferred_error_set_type,
...@@ -3175,24 +3162,24 @@ pub const Object = struct {...@@ -3175,24 +3162,24 @@ pub const Object = struct {
3175 .one, .many, .c => ptr_ty,3162 .one, .many, .c => ptr_ty,
3176 .slice => try o.builder.structType(.normal, &.{3163 .slice => try o.builder.structType(.normal, &.{
3177 ptr_ty,3164 ptr_ty,
3178 try o.lowerType(pt, Type.usize),3165 try o.lowerType(.usize),
3179 }),3166 }),
3180 };3167 };
3181 },3168 },
3182 .array_type => |array_type| o.builder.arrayType(3169 .array_type => |array_type| o.builder.arrayType(
3183 array_type.lenIncludingSentinel(),3170 array_type.lenIncludingSentinel(),
3184 try o.lowerType(pt, Type.fromInterned(array_type.child)),3171 try o.lowerType(.fromInterned(array_type.child)),
3185 ),3172 ),
3186 .vector_type => |vector_type| o.builder.vectorType(3173 .vector_type => |vector_type| o.builder.vectorType(
3187 .normal,3174 .normal,
3188 vector_type.len,3175 vector_type.len,
3189 try o.lowerType(pt, Type.fromInterned(vector_type.child)),3176 try o.lowerType(.fromInterned(vector_type.child)),
3190 ),3177 ),
3191 .opt_type => |child_ty| {3178 .opt_type => |child_ty| {
3192 // Must stay in sync with `opt_payload` logic in `lowerPtr`.3179 // Must stay in sync with `opt_payload` logic in `lowerPtr`.
3193 if (!Type.fromInterned(child_ty).hasRuntimeBits(zcu)) return .i8;3180 if (!Type.fromInterned(child_ty).hasRuntimeBits(zcu)) return .i8;
31943181
3195 const payload_ty = try o.lowerType(pt, Type.fromInterned(child_ty));3182 const payload_ty = try o.lowerType(.fromInterned(child_ty));
3196 if (t.optionalReprIsPayload(zcu)) return payload_ty;3183 if (t.optionalReprIsPayload(zcu)) return payload_ty;
31973184
3198 comptime assert(optional_layout_version == 3);3185 comptime assert(optional_layout_version == 3);
...@@ -3214,7 +3201,7 @@ pub const Object = struct {...@@ -3214,7 +3201,7 @@ pub const Object = struct {
3214 const error_type = try o.errorIntType();3201 const error_type = try o.errorIntType();
3215 if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBits(zcu))3202 if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBits(zcu))
3216 return error_type;3203 return error_type;
3217 const payload_type = try o.lowerType(pt, Type.fromInterned(error_union_type.payload_type));3204 const payload_type = try o.lowerType(.fromInterned(error_union_type.payload_type));
32183205
3219 const payload_align = Type.fromInterned(error_union_type.payload_type).abiAlignment(zcu);3206 const payload_align = Type.fromInterned(error_union_type.payload_type).abiAlignment(zcu);
3220 const error_align: InternPool.Alignment = .fromByteUnits(std.zig.target.intAlignment(target, zcu.errorSetBits()));3207 const error_align: InternPool.Alignment = .fromByteUnits(std.zig.target.intAlignment(target, zcu.errorSetBits()));
...@@ -3254,7 +3241,7 @@ pub const Object = struct {...@@ -3254,7 +3241,7 @@ pub const Object = struct {
3254 const struct_type = ip.loadStructType(t.toIntern());3241 const struct_type = ip.loadStructType(t.toIntern());
32553242
3256 if (struct_type.layout == .@"packed") {3243 if (struct_type.layout == .@"packed") {
3257 const int_ty = try o.lowerType(pt, .fromInterned(struct_type.packed_backing_int_type));3244 const int_ty = try o.lowerType(.fromInterned(struct_type.packed_backing_int_type));
3258 try o.type_map.put(o.gpa, t.toIntern(), int_ty);3245 try o.type_map.put(o.gpa, t.toIntern(), int_ty);
3259 return int_ty;3246 return int_ty;
3260 }3247 }
...@@ -3290,7 +3277,7 @@ pub const Object = struct {...@@ -3290,7 +3277,7 @@ pub const Object = struct {
32903277
3291 if (!field_ty.hasRuntimeBits(zcu)) continue;3278 if (!field_ty.hasRuntimeBits(zcu)) continue;
32923279
3293 try llvm_field_types.append(o.gpa, try o.lowerType(pt, field_ty));3280 try llvm_field_types.append(o.gpa, try o.lowerType(field_ty));
32943281
3295 offset += field_ty.abiSize(zcu);3282 offset += field_ty.abiSize(zcu);
3296 }3283 }
...@@ -3346,7 +3333,7 @@ pub const Object = struct {...@@ -3346,7 +3333,7 @@ pub const Object = struct {
3346 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) {3333 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) {
3347 continue;3334 continue;
3348 }3335 }
3349 try llvm_field_types.append(o.gpa, try o.lowerType(pt, Type.fromInterned(field_ty)));3336 try llvm_field_types.append(o.gpa, try o.lowerType(.fromInterned(field_ty)));
33503337
3351 offset += Type.fromInterned(field_ty).abiSize(zcu);3338 offset += Type.fromInterned(field_ty).abiSize(zcu);
3352 }3339 }
...@@ -3367,7 +3354,7 @@ pub const Object = struct {...@@ -3367,7 +3354,7 @@ pub const Object = struct {
3367 const union_obj = ip.loadUnionType(t.toIntern());3354 const union_obj = ip.loadUnionType(t.toIntern());
33683355
3369 if (union_obj.layout == .@"packed") {3356 if (union_obj.layout == .@"packed") {
3370 const int_ty = try o.lowerType(pt, .fromInterned(union_obj.packed_backing_int_type));3357 const int_ty = try o.lowerType(.fromInterned(union_obj.packed_backing_int_type));
3371 try o.type_map.put(o.gpa, t.toIntern(), int_ty);3358 try o.type_map.put(o.gpa, t.toIntern(), int_ty);
3372 return int_ty;3359 return int_ty;
3373 }3360 }
...@@ -3375,13 +3362,13 @@ pub const Object = struct {...@@ -3375,13 +3362,13 @@ pub const Object = struct {
3375 const layout = Type.getUnionLayout(union_obj, zcu);3362 const layout = Type.getUnionLayout(union_obj, zcu);
33763363
3377 if (layout.payload_size == 0) {3364 if (layout.payload_size == 0) {
3378 const enum_tag_ty = try o.lowerType(pt, .fromInterned(union_obj.enum_tag_type));3365 const enum_tag_ty = try o.lowerType(.fromInterned(union_obj.enum_tag_type));
3379 try o.type_map.put(o.gpa, t.toIntern(), enum_tag_ty);3366 try o.type_map.put(o.gpa, t.toIntern(), enum_tag_ty);
3380 return enum_tag_ty;3367 return enum_tag_ty;
3381 }3368 }
33823369
3383 const aligned_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[layout.most_aligned_field]);3370 const aligned_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[layout.most_aligned_field]);
3384 const aligned_field_llvm_ty = try o.lowerType(pt, aligned_field_ty);3371 const aligned_field_llvm_ty = try o.lowerType(aligned_field_ty);
33853372
3386 const payload_ty = ty: {3373 const payload_ty = ty: {
3387 if (layout.most_aligned_field_size == layout.payload_size) {3374 if (layout.most_aligned_field_size == layout.payload_size) {
...@@ -3407,7 +3394,7 @@ pub const Object = struct {...@@ -3407,7 +3394,7 @@ pub const Object = struct {
3407 );3394 );
3408 return ty;3395 return ty;
3409 }3396 }
3410 const enum_tag_ty = try o.lowerType(pt, .fromInterned(union_obj.enum_tag_type));3397 const enum_tag_ty = try o.lowerType(.fromInterned(union_obj.enum_tag_type));
34113398
3412 // Put the tag before or after the payload depending on which one's3399 // Put the tag before or after the payload depending on which one's
3413 // alignment is greater.3400 // alignment is greater.
...@@ -3442,8 +3429,8 @@ pub const Object = struct {...@@ -3442,8 +3429,8 @@ pub const Object = struct {
3442 }3429 }
3443 return gop.value_ptr.*;3430 return gop.value_ptr.*;
3444 },3431 },
3445 .enum_type => try o.lowerType(pt, t.intTagType(zcu)),3432 .enum_type => try o.lowerType(t.intTagType(zcu)),
3446 .func_type => |func_type| try o.lowerFnType(pt, func_type),3433 .func_type => |func_type| try o.lowerFnType(func_type),
3447 .error_set_type, .inferred_error_set_type => try o.errorIntType(),3434 .error_set_type, .inferred_error_set_type => try o.errorIntType(),
3448 // values, not types3435 // values, not types
3449 .undef,3436 .undef,
...@@ -3469,11 +3456,11 @@ pub const Object = struct {...@@ -3469,11 +3456,11 @@ pub const Object = struct {
3469 };3456 };
3470 }3457 }
34713458
3472 fn lowerFnType(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {3459 fn lowerFnType(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
3473 const zcu = pt.zcu;3460 const zcu = o.zcu;
3474 const ip = &zcu.intern_pool;3461 const ip = &zcu.intern_pool;
3475 const target = zcu.getTarget();3462 const target = zcu.getTarget();
3476 const ret_ty = try lowerFnRetTy(o, pt, fn_info);3463 const ret_ty = try lowerFnRetTy(o, fn_info);
34773464
3478 var llvm_params: std.ArrayList(Builder.Type) = .empty;3465 var llvm_params: std.ArrayList(Builder.Type) = .empty;
3479 defer llvm_params.deinit(o.gpa);3466 defer llvm_params.deinit(o.gpa);
...@@ -3488,12 +3475,12 @@ pub const Object = struct {...@@ -3488,12 +3475,12 @@ pub const Object = struct {
3488 try llvm_params.append(o.gpa, llvm_ptr_ty);3475 try llvm_params.append(o.gpa, llvm_ptr_ty);
3489 }3476 }
34903477
3491 var it = iterateParamTypes(o, pt, fn_info);3478 var it = iterateParamTypes(o, fn_info);
3492 while (try it.next()) |lowering| switch (lowering) {3479 while (try it.next()) |lowering| switch (lowering) {
3493 .no_bits => continue,3480 .no_bits => continue,
3494 .byval => {3481 .byval => {
3495 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);3482 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
3496 try llvm_params.append(o.gpa, try o.lowerType(pt, param_ty));3483 try llvm_params.append(o.gpa, try o.lowerType(param_ty));
3497 },3484 },
3498 .byref, .byref_mut => {3485 .byref, .byref_mut => {
3499 try llvm_params.append(o.gpa, .ptr);3486 try llvm_params.append(o.gpa, .ptr);
...@@ -3508,7 +3495,7 @@ pub const Object = struct {...@@ -3508,7 +3495,7 @@ pub const Object = struct {
3508 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);3495 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
3509 try llvm_params.appendSlice(o.gpa, &.{3496 try llvm_params.appendSlice(o.gpa, &.{
3510 try o.builder.ptrType(toLlvmAddressSpace(param_ty.ptrAddressSpace(zcu), target)),3497 try o.builder.ptrType(toLlvmAddressSpace(param_ty.ptrAddressSpace(zcu), target)),
3511 try o.lowerType(pt, Type.usize),3498 try o.lowerType(.usize),
3512 });3499 });
3513 },3500 },
3514 .multiple_llvm_types => {3501 .multiple_llvm_types => {
...@@ -3516,7 +3503,7 @@ pub const Object = struct {...@@ -3516,7 +3503,7 @@ pub const Object = struct {
3516 },3503 },
3517 .float_array => |count| {3504 .float_array => |count| {
3518 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);3505 const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]);
3519 const float_ty = try o.lowerType(pt, aarch64_c_abi.getFloatArrayType(param_ty, zcu).?);3506 const float_ty = try o.lowerType(aarch64_c_abi.getFloatArrayType(param_ty, zcu).?);
3520 try llvm_params.append(o.gpa, try o.builder.arrayType(count, float_ty));3507 try llvm_params.append(o.gpa, try o.builder.arrayType(count, float_ty));
3521 },3508 },
3522 .i32_array, .i64_array => |arr_len| {3509 .i32_array, .i64_array => |arr_len| {
...@@ -3535,8 +3522,8 @@ pub const Object = struct {...@@ -3535,8 +3522,8 @@ pub const Object = struct {
3535 );3522 );
3536 }3523 }
35373524
3538 pub fn lowerValue(o: *Object, pt: Zcu.PerThread, arg_val: InternPool.Index) Allocator.Error!Builder.Constant {3525 pub fn lowerValue(o: *Object, arg_val: InternPool.Index) Allocator.Error!Builder.Constant {
3539 const zcu = pt.zcu;3526 const zcu = o.zcu;
3540 const ip = &zcu.intern_pool;3527 const ip = &zcu.intern_pool;
3541 const target = zcu.getTarget();3528 const target = zcu.getTarget();
35423529
...@@ -3544,7 +3531,7 @@ pub const Object = struct {...@@ -3544,7 +3531,7 @@ pub const Object = struct {
3544 const val_key = ip.indexToKey(val.toIntern());3531 const val_key = ip.indexToKey(val.toIntern());
35453532
3546 if (val.isUndef(zcu)) {3533 if (val.isUndef(zcu)) {
3547 return o.builder.undefConst(try o.lowerType(pt, Type.fromInterned(val_key.typeOf())));3534 return o.builder.undefConst(try o.lowerType(.fromInterned(val_key.typeOf())));
3548 }3535 }
35493536
3550 const ty: Type = .fromInterned(val_key.typeOf());3537 const ty: Type = .fromInterned(val_key.typeOf());
...@@ -3580,45 +3567,45 @@ pub const Object = struct {...@@ -3580,45 +3567,45 @@ pub const Object = struct {
3580 },3567 },
3581 .enum_literal => unreachable, // non-runtime value3568 .enum_literal => unreachable, // non-runtime value
3582 .@"extern" => |@"extern"| {3569 .@"extern" => |@"extern"| {
3583 const function_index = try o.resolveLlvmFunction(pt, @"extern".owner_nav);3570 const function_index = try o.resolveLlvmFunction(@"extern".owner_nav);
3584 return function_index.ptrConst(&o.builder).global.toConst();3571 return function_index.ptrConst(&o.builder).global.toConst();
3585 },3572 },
3586 .func => |func| {3573 .func => |func| {
3587 const function_index = try o.resolveLlvmFunction(pt, func.owner_nav);3574 const function_index = try o.resolveLlvmFunction(func.owner_nav);
3588 return function_index.ptrConst(&o.builder).global.toConst();3575 return function_index.ptrConst(&o.builder).global.toConst();
3589 },3576 },
3590 .int => {3577 .int => {
3591 var bigint_space: Value.BigIntSpace = undefined;3578 var bigint_space: Value.BigIntSpace = undefined;
3592 const bigint = val.toBigInt(&bigint_space, zcu);3579 const bigint = val.toBigInt(&bigint_space, zcu);
3593 return lowerBigInt(o, pt, ty, bigint);3580 const llvm_int_ty = try o.builder.intType(ty.intInfo(zcu).bits);
3581 return o.builder.bigIntConst(llvm_int_ty, bigint);
3594 },3582 },
3595 .err => |err| {3583 .err => |err| {
3596 const int = try pt.getErrorValue(err.name);3584 const int = zcu.intern_pool.getErrorValueIfExists(err.name).?;
3597 const llvm_int = try o.builder.intConst(try o.errorIntType(), int);3585 return o.builder.intConst(try o.errorIntType(), int);
3598 return llvm_int;
3599 },3586 },
3600 .error_union => |error_union| {3587 .error_union => |error_union| {
3601 const err_val = switch (error_union.val) {3588 const llvm_error_ty = try o.errorIntType();
3602 .err_name => |err_name| try pt.intern(.{ .err = .{3589 const llvm_error_value = switch (error_union.val) {
3603 .ty = ty.errorUnionSet(zcu).toIntern(),3590 .err_name => |name| try o.builder.intConst(
3604 .name = err_name,3591 llvm_error_ty,
3605 } }),3592 zcu.intern_pool.getErrorValueIfExists(name).?,
3606 .payload => (try pt.intValue(try pt.errorIntType(), 0)).toIntern(),3593 ),
3594 .payload => try o.builder.intConst(llvm_error_ty, 0),
3607 };3595 };
3608 const err_int_ty = try pt.errorIntType();3596
3609 const payload_type = ty.errorUnionPayload(zcu);3597 const payload_type = ty.errorUnionPayload(zcu);
3610 if (!payload_type.hasRuntimeBits(zcu)) {3598 if (!payload_type.hasRuntimeBits(zcu)) {
3611 // We use the error type directly as the type.3599 // We use the error type directly as the type.
3612 return o.lowerValue(pt, err_val);3600 return llvm_error_value;
3613 }3601 }
36143602
3615 const payload_align = payload_type.abiAlignment(zcu);3603 const payload_align = payload_type.abiAlignment(zcu);
3616 const error_align = err_int_ty.abiAlignment(zcu);3604 const error_align = Type.errorAbiAlignment(zcu);
3617 const llvm_error_value = try o.lowerValue(pt, err_val);3605 const llvm_payload_value = switch (error_union.val) {
3618 const llvm_payload_value = try o.lowerValue(pt, switch (error_union.val) {3606 .err_name => try o.builder.undefConst(try o.lowerType(payload_type)),
3619 .err_name => try pt.intern(.{ .undef = payload_type.toIntern() }),3607 .payload => |payload| try o.lowerValue(payload),
3620 .payload => |payload| payload,3608 };
3621 });
36223609
3623 var fields: [3]Builder.Type = undefined;3610 var fields: [3]Builder.Type = undefined;
3624 var vals: [3]Builder.Constant = undefined;3611 var vals: [3]Builder.Constant = undefined;
...@@ -3632,7 +3619,7 @@ pub const Object = struct {...@@ -3632,7 +3619,7 @@ pub const Object = struct {
3632 fields[0] = vals[0].typeOf(&o.builder);3619 fields[0] = vals[0].typeOf(&o.builder);
3633 fields[1] = vals[1].typeOf(&o.builder);3620 fields[1] = vals[1].typeOf(&o.builder);
36343621
3635 const llvm_ty = try o.lowerType(pt, ty);3622 const llvm_ty = try o.lowerType(ty);
3636 const llvm_ty_fields = llvm_ty.structFields(&o.builder);3623 const llvm_ty_fields = llvm_ty.structFields(&o.builder);
3637 if (llvm_ty_fields.len > 2) {3624 if (llvm_ty_fields.len > 2) {
3638 assert(llvm_ty_fields.len == 3);3625 assert(llvm_ty_fields.len == 3);
...@@ -3644,7 +3631,7 @@ pub const Object = struct {...@@ -3644,7 +3631,7 @@ pub const Object = struct {
3644 fields[0..llvm_ty_fields.len],3631 fields[0..llvm_ty_fields.len],
3645 ), vals[0..llvm_ty_fields.len]);3632 ), vals[0..llvm_ty_fields.len]);
3646 },3633 },
3647 .enum_tag => |enum_tag| o.lowerValue(pt, enum_tag.int),3634 .enum_tag => |enum_tag| o.lowerValue(enum_tag.int),
3648 .float => switch (ty.floatBits(target)) {3635 .float => switch (ty.floatBits(target)) {
3649 16 => if (backendSupportsF16(target))3636 16 => if (backendSupportsF16(target))
3650 try o.builder.halfConst(val.toFloat(f16, zcu))3637 try o.builder.halfConst(val.toFloat(f16, zcu))
...@@ -3659,10 +3646,10 @@ pub const Object = struct {...@@ -3659,10 +3646,10 @@ pub const Object = struct {
3659 128 => try o.builder.fp128Const(val.toFloat(f128, zcu)),3646 128 => try o.builder.fp128Const(val.toFloat(f128, zcu)),
3660 else => unreachable,3647 else => unreachable,
3661 },3648 },
3662 .ptr => try o.lowerPtr(pt, arg_val, 0),3649 .ptr => try o.lowerPtr(arg_val, 0),
3663 .slice => |slice| return o.builder.structConst(try o.lowerType(pt, ty), &.{3650 .slice => |slice| return o.builder.structConst(try o.lowerType(ty), &.{
3664 try o.lowerValue(pt, slice.ptr),3651 try o.lowerValue(slice.ptr),
3665 try o.lowerValue(pt, slice.len),3652 try o.lowerValue(slice.len),
3666 }),3653 }),
3667 .opt => |opt| {3654 .opt => |opt| {
3668 comptime assert(optional_layout_version == 3);3655 comptime assert(optional_layout_version == 3);
...@@ -3672,7 +3659,7 @@ pub const Object = struct {...@@ -3672,7 +3659,7 @@ pub const Object = struct {
3672 if (!payload_ty.hasRuntimeBits(zcu)) {3659 if (!payload_ty.hasRuntimeBits(zcu)) {
3673 return non_null_bit;3660 return non_null_bit;
3674 }3661 }
3675 const llvm_ty = try o.lowerType(pt, ty);3662 const llvm_ty = try o.lowerType(ty);
3676 if (ty.optionalReprIsPayload(zcu)) return switch (opt.val) {3663 if (ty.optionalReprIsPayload(zcu)) return switch (opt.val) {
3677 .none => switch (llvm_ty.tag(&o.builder)) {3664 .none => switch (llvm_ty.tag(&o.builder)) {
3678 .integer => try o.builder.intConst(llvm_ty, 0),3665 .integer => try o.builder.intConst(llvm_ty, 0),
...@@ -3680,16 +3667,16 @@ pub const Object = struct {...@@ -3680,16 +3667,16 @@ pub const Object = struct {
3680 .structure => try o.builder.zeroInitConst(llvm_ty),3667 .structure => try o.builder.zeroInitConst(llvm_ty),
3681 else => unreachable,3668 else => unreachable,
3682 },3669 },
3683 else => |payload| try o.lowerValue(pt, payload),3670 else => |payload| try o.lowerValue(payload),
3684 };3671 };
3685 assert(payload_ty.zigTypeTag(zcu) != .@"fn");3672 assert(payload_ty.zigTypeTag(zcu) != .@"fn");
36863673
3687 var fields: [3]Builder.Type = undefined;3674 var fields: [3]Builder.Type = undefined;
3688 var vals: [3]Builder.Constant = undefined;3675 var vals: [3]Builder.Constant = undefined;
3689 vals[0] = try o.lowerValue(pt, switch (opt.val) {3676 vals[0] = switch (opt.val) {
3690 .none => try pt.intern(.{ .undef = payload_ty.toIntern() }),3677 .none => try o.builder.undefConst(try o.lowerType(payload_ty)),
3691 else => |payload| payload,3678 else => |payload| try o.lowerValue(payload),
3692 });3679 };
3693 vals[1] = non_null_bit;3680 vals[1] = non_null_bit;
3694 fields[0] = vals[0].typeOf(&o.builder);3681 fields[0] = vals[0].typeOf(&o.builder);
3695 fields[1] = vals[1].typeOf(&o.builder);3682 fields[1] = vals[1].typeOf(&o.builder);
...@@ -3705,14 +3692,14 @@ pub const Object = struct {...@@ -3705,14 +3692,14 @@ pub const Object = struct {
3705 fields[0..llvm_ty_fields.len],3692 fields[0..llvm_ty_fields.len],
3706 ), vals[0..llvm_ty_fields.len]);3693 ), vals[0..llvm_ty_fields.len]);
3707 },3694 },
3708 .bitpack => |bitpack| return o.lowerValue(pt, bitpack.backing_int_val),3695 .bitpack => |bitpack| return o.lowerValue(bitpack.backing_int_val),
3709 .aggregate => |aggregate| switch (ip.indexToKey(ty.toIntern())) {3696 .aggregate => |aggregate| switch (ip.indexToKey(ty.toIntern())) {
3710 .array_type => |array_type| switch (aggregate.storage) {3697 .array_type => |array_type| switch (aggregate.storage) {
3711 .bytes => |bytes| try o.builder.stringConst(try o.builder.string(3698 .bytes => |bytes| try o.builder.stringConst(try o.builder.string(
3712 bytes.toSlice(array_type.lenIncludingSentinel(), ip),3699 bytes.toSlice(array_type.lenIncludingSentinel(), ip),
3713 )),3700 )),
3714 .elems => |elems| {3701 .elems => |elems| {
3715 const array_ty = try o.lowerType(pt, ty);3702 const array_ty = try o.lowerType(ty);
3716 const elem_ty = array_ty.childType(&o.builder);3703 const elem_ty = array_ty.childType(&o.builder);
3717 assert(elems.len == array_ty.aggregateLen(&o.builder));3704 assert(elems.len == array_ty.aggregateLen(&o.builder));
37183705
...@@ -3732,7 +3719,7 @@ pub const Object = struct {...@@ -3732,7 +3719,7 @@ pub const Object = struct {
37323719
3733 var need_unnamed = false;3720 var need_unnamed = false;
3734 for (vals, fields, elems) |*result_val, *result_field, elem| {3721 for (vals, fields, elems) |*result_val, *result_field, elem| {
3735 result_val.* = try o.lowerValue(pt, elem);3722 result_val.* = try o.lowerValue(elem);
3736 result_field.* = result_val.typeOf(&o.builder);3723 result_field.* = result_val.typeOf(&o.builder);
3737 if (result_field.* != elem_ty) need_unnamed = true;3724 if (result_field.* != elem_ty) need_unnamed = true;
3738 }3725 }
...@@ -3744,7 +3731,7 @@ pub const Object = struct {...@@ -3744,7 +3731,7 @@ pub const Object = struct {
3744 .repeated_elem => |elem| {3731 .repeated_elem => |elem| {
3745 const len: usize = @intCast(array_type.len);3732 const len: usize = @intCast(array_type.len);
3746 const len_including_sentinel: usize = @intCast(array_type.lenIncludingSentinel());3733 const len_including_sentinel: usize = @intCast(array_type.lenIncludingSentinel());
3747 const array_ty = try o.lowerType(pt, ty);3734 const array_ty = try o.lowerType(ty);
3748 const elem_ty = array_ty.childType(&o.builder);3735 const elem_ty = array_ty.childType(&o.builder);
37493736
3750 const ExpectedContents = extern struct {3737 const ExpectedContents = extern struct {
...@@ -3762,12 +3749,12 @@ pub const Object = struct {...@@ -3762,12 +3749,12 @@ pub const Object = struct {
3762 defer allocator.free(fields);3749 defer allocator.free(fields);
37633750
3764 var need_unnamed = false;3751 var need_unnamed = false;
3765 @memset(vals[0..len], try o.lowerValue(pt, elem));3752 @memset(vals[0..len], try o.lowerValue(elem));
3766 @memset(fields[0..len], vals[0].typeOf(&o.builder));3753 @memset(fields[0..len], vals[0].typeOf(&o.builder));
3767 if (fields[0] != elem_ty) need_unnamed = true;3754 if (fields[0] != elem_ty) need_unnamed = true;
37683755
3769 if (array_type.sentinel != .none) {3756 if (array_type.sentinel != .none) {
3770 vals[len] = try o.lowerValue(pt, array_type.sentinel);3757 vals[len] = try o.lowerValue(array_type.sentinel);
3771 fields[len] = vals[len].typeOf(&o.builder);3758 fields[len] = vals[len].typeOf(&o.builder);
3772 if (fields[len] != elem_ty) need_unnamed = true;3759 if (fields[len] != elem_ty) need_unnamed = true;
3773 }3760 }
...@@ -3779,7 +3766,7 @@ pub const Object = struct {...@@ -3779,7 +3766,7 @@ pub const Object = struct {
3779 },3766 },
3780 },3767 },
3781 .vector_type => |vector_type| {3768 .vector_type => |vector_type| {
3782 const vector_ty = try o.lowerType(pt, ty);3769 const vector_ty = try o.lowerType(ty);
3783 switch (aggregate.storage) {3770 switch (aggregate.storage) {
3784 .bytes, .elems => {3771 .bytes, .elems => {
3785 const ExpectedContents = [Builder.expected_fields_len]Builder.Constant;3772 const ExpectedContents = [Builder.expected_fields_len]Builder.Constant;
...@@ -3796,7 +3783,7 @@ pub const Object = struct {...@@ -3796,7 +3783,7 @@ pub const Object = struct {
3796 result_val.* = try o.builder.intConst(.i8, byte);3783 result_val.* = try o.builder.intConst(.i8, byte);
3797 },3784 },
3798 .elems => |elems| for (vals, elems) |*result_val, elem| {3785 .elems => |elems| for (vals, elems) |*result_val, elem| {
3799 result_val.* = try o.lowerValue(pt, elem);3786 result_val.* = try o.lowerValue(elem);
3800 },3787 },
3801 .repeated_elem => unreachable,3788 .repeated_elem => unreachable,
3802 }3789 }
...@@ -3804,12 +3791,12 @@ pub const Object = struct {...@@ -3804,12 +3791,12 @@ pub const Object = struct {
3804 },3791 },
3805 .repeated_elem => |elem| return o.builder.splatConst(3792 .repeated_elem => |elem| return o.builder.splatConst(
3806 vector_ty,3793 vector_ty,
3807 try o.lowerValue(pt, elem),3794 try o.lowerValue(elem),
3808 ),3795 ),
3809 }3796 }
3810 },3797 },
3811 .tuple_type => |tuple| {3798 .tuple_type => |tuple| {
3812 const struct_ty = try o.lowerType(pt, ty);3799 const struct_ty = try o.lowerType(ty);
3813 const llvm_len = struct_ty.aggregateLen(&o.builder);3800 const llvm_len = struct_ty.aggregateLen(&o.builder);
38143801
3815 const ExpectedContents = extern struct {3802 const ExpectedContents = extern struct {
...@@ -3835,8 +3822,8 @@ pub const Object = struct {...@@ -3835,8 +3822,8 @@ pub const Object = struct {
3835 tuple.types.get(ip),3822 tuple.types.get(ip),
3836 tuple.values.get(ip),3823 tuple.values.get(ip),
3837 0..,3824 0..,
3838 ) |field_ty, field_val, field_index| {3825 ) |field_ty, field_comptime_val, field_index| {
3839 if (field_val != .none) continue;3826 if (field_comptime_val != .none) continue;
3840 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue;3827 if (!Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue;
38413828
3842 const field_align = Type.fromInterned(field_ty).abiAlignment(zcu);3829 const field_align = Type.fromInterned(field_ty).abiAlignment(zcu);
...@@ -3854,8 +3841,11 @@ pub const Object = struct {...@@ -3854,8 +3841,11 @@ pub const Object = struct {
3854 llvm_index += 1;3841 llvm_index += 1;
3855 }3842 }
38563843
3857 vals[llvm_index] =3844 vals[llvm_index] = switch (aggregate.storage) {
3858 try o.lowerValue(pt, (try val.fieldValue(pt, field_index)).toIntern());3845 .bytes => |bytes| try o.builder.intConst(.i8, bytes.at(field_index, ip)),
3846 .elems => |elems| try o.lowerValue(elems[field_index]),
3847 .repeated_elem => |elem| try o.lowerValue(elem),
3848 };
3859 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);3849 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);
3860 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])3850 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])
3861 need_unnamed = true;3851 need_unnamed = true;
...@@ -3883,7 +3873,7 @@ pub const Object = struct {...@@ -3883,7 +3873,7 @@ pub const Object = struct {
3883 },3873 },
3884 .struct_type => {3874 .struct_type => {
3885 const struct_type = ip.loadStructType(ty.toIntern());3875 const struct_type = ip.loadStructType(ty.toIntern());
3886 const struct_ty = try o.lowerType(pt, ty);3876 const struct_ty = try o.lowerType(ty);
3887 assert(struct_type.layout != .@"packed");3877 assert(struct_type.layout != .@"packed");
3888 const llvm_len = struct_ty.aggregateLen(&o.builder);3878 const llvm_len = struct_ty.aggregateLen(&o.builder);
38893879
...@@ -3927,10 +3917,11 @@ pub const Object = struct {...@@ -3927,10 +3917,11 @@ pub const Object = struct {
3927 continue;3917 continue;
3928 }3918 }
39293919
3930 vals[llvm_index] = try o.lowerValue(3920 vals[llvm_index] = switch (aggregate.storage) {
3931 pt,3921 .bytes => |bytes| try o.builder.intConst(.i8, bytes.at(field_index, ip)),
3932 (try val.fieldValue(pt, field_index)).toIntern(),3922 .elems => |elems| try o.lowerValue(elems[field_index]),
3933 );3923 .repeated_elem => |elem| try o.lowerValue(elem),
3924 };
3934 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);3925 fields[llvm_index] = vals[llvm_index].typeOf(&o.builder);
3935 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])3926 if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index])
3936 need_unnamed = true;3927 need_unnamed = true;
...@@ -3959,9 +3950,9 @@ pub const Object = struct {...@@ -3959,9 +3950,9 @@ pub const Object = struct {
3959 else => unreachable,3950 else => unreachable,
3960 },3951 },
3961 .un => |un| {3952 .un => |un| {
3962 const union_ty = try o.lowerType(pt, ty);3953 const union_ty = try o.lowerType(ty);
3963 const layout = ty.unionGetLayout(zcu);3954 const layout = ty.unionGetLayout(zcu);
3964 if (layout.payload_size == 0) return o.lowerValue(pt, un.tag);3955 if (layout.payload_size == 0) return o.lowerValue(un.tag);
39653956
3966 const union_obj = zcu.typeToUnion(ty).?;3957 const union_obj = zcu.typeToUnion(ty).?;
3967 const container_layout = union_obj.layout;3958 const container_layout = union_obj.layout;
...@@ -3982,7 +3973,7 @@ pub const Object = struct {...@@ -3982,7 +3973,7 @@ pub const Object = struct {
3982 const padding_len = layout.payload_size;3973 const padding_len = layout.payload_size;
3983 break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8));3974 break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8));
3984 }3975 }
3985 const payload = try o.lowerValue(pt, un.val);3976 const payload = try o.lowerValue(un.val);
3986 const payload_ty = payload.typeOf(&o.builder);3977 const payload_ty = payload.typeOf(&o.builder);
3987 if (payload_ty != union_ty.structFields(&o.builder)[3978 if (payload_ty != union_ty.structFields(&o.builder)[
3988 @intFromBool(layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align))3979 @intFromBool(layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align))
...@@ -3997,7 +3988,7 @@ pub const Object = struct {...@@ -3997,7 +3988,7 @@ pub const Object = struct {
3997 );3988 );
3998 } else p: {3989 } else p: {
3999 assert(layout.tag_size == 0);3990 assert(layout.tag_size == 0);
4000 const union_val = try o.lowerValue(pt, un.val);3991 const union_val = try o.lowerValue(un.val);
4001 need_unnamed = true;3992 need_unnamed = true;
4002 break :p union_val;3993 break :p union_val;
4003 };3994 };
...@@ -4007,7 +3998,7 @@ pub const Object = struct {...@@ -4007,7 +3998,7 @@ pub const Object = struct {
4007 try o.builder.structType(union_ty.structKind(&o.builder), &.{payload_ty})3998 try o.builder.structType(union_ty.structKind(&o.builder), &.{payload_ty})
4008 else3999 else
4009 union_ty, &.{payload});4000 union_ty, &.{payload});
4010 const tag = try o.lowerValue(pt, un.tag);4001 const tag = try o.lowerValue(un.tag);
4011 const tag_ty = tag.typeOf(&o.builder);4002 const tag_ty = tag.typeOf(&o.builder);
4012 var fields: [3]Builder.Type = undefined;4003 var fields: [3]Builder.Type = undefined;
4013 var vals: [3]Builder.Constant = undefined;4004 var vals: [3]Builder.Constant = undefined;
...@@ -4033,52 +4024,45 @@ pub const Object = struct {...@@ -4033,52 +4024,45 @@ pub const Object = struct {
4033 };4024 };
4034 }4025 }
40354026
4036 fn lowerBigInt(
4037 o: *Object,
4038 pt: Zcu.PerThread,
4039 ty: Type,
4040 bigint: std.math.big.int.Const,
4041 ) Allocator.Error!Builder.Constant {
4042 const zcu = pt.zcu;
4043 return o.builder.bigIntConst(try o.builder.intType(ty.intInfo(zcu).bits), bigint);
4044 }
4045
4046 fn lowerPtr(4027 fn lowerPtr(
4047 o: *Object,4028 o: *Object,
4048 pt: Zcu.PerThread,
4049 ptr_val: InternPool.Index,4029 ptr_val: InternPool.Index,
4050 prev_offset: u64,4030 prev_offset: u64,
4051 ) Allocator.Error!Builder.Constant {4031 ) Allocator.Error!Builder.Constant {
4052 const zcu = pt.zcu;4032 const zcu = o.zcu;
4053 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;4033 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;
4054 const offset: u64 = prev_offset + ptr.byte_offset;4034 const offset: u64 = prev_offset + ptr.byte_offset;
4055 return switch (ptr.base_addr) {4035 return switch (ptr.base_addr) {
4056 .nav => |nav| {4036 .nav => |nav| {
4057 const base_ptr = try o.lowerNavRefValue(pt, nav);4037 const base_ptr = try o.lowerNavRefValue(nav);
4058 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{4038 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{
4059 try o.builder.intConst(.i64, offset),4039 try o.builder.intConst(.i64, offset),
4060 });4040 });
4061 },4041 },
4062 .uav => |uav| {4042 .uav => |uav| {
4063 const base_ptr = try o.lowerUavRef(pt, uav);4043 const orig_ptr_ty: Type = .fromInterned(uav.orig_ty);
4044 const base_ptr = try o.lowerUavRef(
4045 uav.val,
4046 orig_ptr_ty.ptrAlignment(zcu),
4047 orig_ptr_ty.ptrAddressSpace(zcu),
4048 );
4064 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{4049 return o.builder.gepConst(.inbounds, .i8, base_ptr, null, &.{
4065 try o.builder.intConst(.i64, offset),4050 try o.builder.intConst(.i64, offset),
4066 });4051 });
4067 },4052 },
4068 .int => try o.builder.castConst(4053 .int => try o.builder.castConst(
4069 .inttoptr,4054 .inttoptr,
4070 try o.builder.intConst(try o.lowerType(pt, Type.usize), offset),4055 try o.builder.intConst(try o.lowerType(.usize), offset),
4071 try o.lowerType(pt, Type.fromInterned(ptr.ty)),4056 try o.lowerType(.fromInterned(ptr.ty)),
4072 ),4057 ),
4073 .eu_payload => |eu_ptr| try o.lowerPtr(4058 .eu_payload => |eu_ptr| try o.lowerPtr(
4074 pt,
4075 eu_ptr,4059 eu_ptr,
4076 offset + codegen.errUnionPayloadOffset(4060 offset + codegen.errUnionPayloadOffset(
4077 Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu),4061 Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu),
4078 zcu,4062 zcu,
4079 ),4063 ),
4080 ),4064 ),
4081 .opt_payload => |opt_ptr| try o.lowerPtr(pt, opt_ptr, offset),4065 .opt_payload => |opt_ptr| try o.lowerPtr(opt_ptr, offset),
4082 .field => |field| {4066 .field => |field| {
4083 const agg_ty = Value.fromInterned(field.base).typeOf(zcu).childType(zcu);4067 const agg_ty = Value.fromInterned(field.base).typeOf(zcu).childType(zcu);
4084 const field_off: u64 = switch (agg_ty.zigTypeTag(zcu)) {4068 const field_off: u64 = switch (agg_ty.zigTypeTag(zcu)) {
...@@ -4096,13 +4080,13 @@ pub const Object = struct {...@@ -4096,13 +4080,13 @@ pub const Object = struct {
4096 },4080 },
4097 else => unreachable,4081 else => unreachable,
4098 };4082 };
4099 return o.lowerPtr(pt, field.base, offset + field_off);4083 return o.lowerPtr(field.base, offset + field_off);
4100 },4084 },
4101 .arr_elem => |arr_elem| {4085 .arr_elem => |arr_elem| {
4102 const base_ptr_ty = Value.fromInterned(arr_elem.base).typeOf(zcu);4086 const base_ptr_ty = Value.fromInterned(arr_elem.base).typeOf(zcu);
4103 assert(base_ptr_ty.ptrSize(zcu) == .many);4087 assert(base_ptr_ty.ptrSize(zcu) == .many);
4104 const elem_size = base_ptr_ty.childType(zcu).abiSize(zcu);4088 const elem_size = base_ptr_ty.childType(zcu).abiSize(zcu);
4105 return o.lowerPtr(pt, arr_elem.base, offset + elem_size * arr_elem.index);4089 return o.lowerPtr(arr_elem.base, offset + elem_size * arr_elem.index);
4106 },4090 },
4107 .comptime_field => unreachable,4091 .comptime_field => unreachable,
4108 .comptime_alloc => unreachable,4092 .comptime_alloc => unreachable,
...@@ -4113,88 +4097,84 @@ pub const Object = struct {...@@ -4113,88 +4097,84 @@ pub const Object = struct {
4113 /// Maybe the logic could be unified.4097 /// Maybe the logic could be unified.
4114 pub fn lowerUavRef(4098 pub fn lowerUavRef(
4115 o: *Object,4099 o: *Object,
4116 pt: Zcu.PerThread,4100 uav_val: InternPool.Index,
4117 uav: InternPool.Key.Ptr.BaseAddr.Uav,4101 /// Must not be `.none`.
4102 @"align": InternPool.Alignment,
4103 @"addrspace": std.builtin.AddressSpace,
4118 ) Allocator.Error!Builder.Constant {4104 ) Allocator.Error!Builder.Constant {
4119 const zcu = pt.zcu;4105 assert(@"align" != .none);
4106
4107 const zcu = o.zcu;
4120 const ip = &zcu.intern_pool;4108 const ip = &zcu.intern_pool;
4121 const uav_val = uav.val;4109 const uav_ty: Type = .fromInterned(ip.typeOf(uav_val));
4122 const uav_ty = Type.fromInterned(ip.typeOf(uav_val));
4123 const target = zcu.getTarget();
41244110
4125 switch (ip.indexToKey(uav_val)) {4111 switch (ip.indexToKey(uav_val)) {
4126 .func => @panic("TODO"),4112 .func => unreachable, // should be using a Nav ref
4127 .@"extern" => @panic("TODO"),4113 .@"extern" => unreachable, // should be using a Nav ref
4128 else => {},4114 else => {},
4129 }4115 }
41304116
4131 const ptr_ty = Type.fromInterned(uav.orig_ty);4117 if (!uav_ty.hasRuntimeBits(zcu)) {
41324118 return o.lowerPtrToVoid(@"align", @"addrspace");
4133 if (!uav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) {
4134 return o.lowerPtrToVoid(pt, ptr_ty);
4135 }4119 }
41364120
4137 assert(uav_ty.zigTypeTag(zcu) != .@"fn"); // should be using a Nav ref4121 const llvm_addrspace = toLlvmAddressSpace(@"addrspace", zcu.getTarget());
41384122 const llvm_global = (try o.resolveGlobalUav(uav_val, llvm_addrspace, @"align")).ptrConst(&o.builder).global;
4139 const llvm_addr_space = toLlvmAddressSpace(ptr_ty.ptrAddressSpace(zcu), target);
4140 const alignment = ptr_ty.ptrAlignment(zcu);
4141 const llvm_global = (try o.resolveGlobalUav(pt, uav.val, llvm_addr_space, alignment)).ptrConst(&o.builder).global;
41424123
4143 const llvm_val = try o.builder.convConst(4124 return o.builder.convConst(
4144 llvm_global.toConst(),4125 llvm_global.toConst(),
4145 try o.builder.ptrType(llvm_addr_space),4126 try o.builder.ptrType(llvm_addrspace),
4146 );4127 );
4147
4148 return o.builder.convConst(llvm_val, try o.lowerType(pt, ptr_ty));
4149 }4128 }
41504129
4151 pub fn lowerNavRefValue(o: *Object, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) Allocator.Error!Builder.Constant {4130 pub fn lowerNavRefValue(o: *Object, nav_index: InternPool.Nav.Index) Allocator.Error!Builder.Constant {
4152 const zcu = pt.zcu;4131 const zcu = o.zcu;
4153 const ip = &zcu.intern_pool;4132 const ip = &zcu.intern_pool;
41544133
4155 const nav = ip.getNav(nav_index);4134 const nav = ip.getNav(nav_index);
41564135
4157 const nav_ty: Type = .fromInterned(nav.resolved.?.type);4136 const nav_ty: Type = .fromInterned(nav.resolved.?.type);
4158 const ptr_ty = try pt.navPtrType(nav_index);
41594137
4160 if (nav.getExtern(ip) == null and !nav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) {4138 if (nav.getExtern(ip) == null and !nav_ty.isRuntimeFnOrHasRuntimeBits(zcu)) {
4161 return o.lowerPtrToVoid(pt, ptr_ty);4139 return o.lowerPtrToVoid(nav.resolved.?.@"align", nav.resolved.?.@"addrspace");
4162 }4140 }
41634141
4164 const llvm_global = if (nav_ty.zigTypeTag(zcu) == .@"fn")4142 const llvm_global = if (nav_ty.zigTypeTag(zcu) == .@"fn")
4165 (try o.resolveLlvmFunction(pt, nav_index)).ptrConst(&o.builder).global4143 (try o.resolveLlvmFunction(nav_index)).ptrConst(&o.builder).global
4166 else4144 else
4167 (try o.resolveGlobalNav(pt, nav_index)).ptrConst(&o.builder).global;4145 (try o.resolveGlobalNav(nav_index)).ptrConst(&o.builder).global;
41684146
4169 const llvm_val = try o.builder.convConst(4147 return try o.builder.convConst(
4170 llvm_global.toConst(),4148 llvm_global.toConst(),
4171 try o.builder.ptrType(toLlvmAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget())),4149 try o.builder.ptrType(toLlvmAddressSpace(nav.resolved.?.@"addrspace", zcu.getTarget())),
4172 );4150 );
4173
4174 return o.builder.convConst(llvm_val, try o.lowerType(pt, ptr_ty));
4175 }4151 }
41764152
4177 pub fn lowerPtrToVoid(o: *Object, pt: Zcu.PerThread, ptr_ty: Type) Allocator.Error!Builder.Constant {4153 pub fn lowerPtrToVoid(
4178 const zcu = pt.zcu;4154 o: *Object,
4155 @"align": InternPool.Alignment,
4156 @"addrspace": std.builtin.AddressSpace,
4157 ) Allocator.Error!Builder.Constant {
4158 const target = o.zcu.getTarget();
4179 // Even though we are pointing at something which has zero bits (e.g. `void`),4159 // Even though we are pointing at something which has zero bits (e.g. `void`),
4180 // Pointers are defined to have bits. So we must return something here.4160 // Pointers are defined to have bits. So we must return something here.
4181 // The value cannot be undefined, because we use the `nonnull` annotation4161 // The value cannot be undefined, because we use the `nonnull` annotation
4182 // for non-optional pointers. We also need to respect the alignment, even though4162 // for non-optional pointers. We also need to respect the alignment, even though
4183 // the address will never be dereferenced.4163 // the address will never be dereferenced.
4184 const int: u64 = ptr_ty.ptrInfo(zcu).flags.alignment.toByteUnits() orelse4164 const int: u64 = @"align".toByteUnits() orelse
4185 // Note that these 0xaa values are appropriate even in release-optimized builds4165 // Note that these 0xaa values are appropriate even in release-optimized builds
4186 // because we need a well-defined value that is not null, and LLVM does not4166 // because we need a well-defined value that is not null, and LLVM does not
4187 // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR4167 // have an "undef_but_not_null" attribute. As an example, if this `alloc` AIR
4188 // instruction is followed by a `wrap_optional`, it will return this value4168 // instruction is followed by a `wrap_optional`, it will return this value
4189 // verbatim, and the result should test as non-null.4169 // verbatim, and the result should test as non-null.
4190 switch (zcu.getTarget().ptrBitWidth()) {4170 switch (target.ptrBitWidth()) {
4191 16 => 0xaaaa,4171 16 => 0xaaaa,
4192 32 => 0xaaaaaaaa,4172 32 => 0xaaaaaaaa,
4193 64 => 0xaaaaaaaa_aaaaaaaa,4173 64 => 0xaaaaaaaa_aaaaaaaa,
4194 else => unreachable,4174 else => unreachable,
4195 };4175 };
4196 const llvm_usize = try o.lowerType(pt, Type.usize);4176 const llvm_usize = try o.lowerType(.usize);
4197 const llvm_ptr_ty = try o.lowerType(pt, ptr_ty);4177 const llvm_ptr_ty = try o.builder.ptrType(toLlvmAddressSpace(@"addrspace", target));
4198 return o.builder.castConst(.inttoptr, try o.builder.intConst(llvm_usize, int), llvm_ptr_ty);4178 return o.builder.castConst(.inttoptr, try o.builder.intConst(llvm_usize, int), llvm_ptr_ty);
4199 }4179 }
42004180
...@@ -4207,7 +4187,7 @@ pub const Object = struct {...@@ -4207,7 +4187,7 @@ pub const Object = struct {
4207 fn_info: InternPool.Key.FuncType,4187 fn_info: InternPool.Key.FuncType,
4208 llvm_arg_i: u32,4188 llvm_arg_i: u32,
4209 ) Allocator.Error!void {4189 ) Allocator.Error!void {
4210 const zcu = pt.zcu;4190 const zcu = o.zcu;
4211 if (param_ty.isPtrAtRuntime(zcu)) {4191 if (param_ty.isPtrAtRuntime(zcu)) {
4212 const ptr_info = param_ty.ptrInfo(zcu);4192 const ptr_info = param_ty.ptrInfo(zcu);
4213 if (std.math.cast(u5, param_index)) |i| {4193 if (std.math.cast(u5, param_index)) |i| {
...@@ -4226,7 +4206,7 @@ pub const Object = struct {...@@ -4226,7 +4206,7 @@ pub const Object = struct {
4226 .x86_64_interrupt,4206 .x86_64_interrupt,
4227 .x86_interrupt,4207 .x86_interrupt,
4228 => {4208 => {
4229 const child_type = try lowerType(o, pt, Type.fromInterned(ptr_info.child));4209 const child_type = try lowerType(o, Type.fromInterned(ptr_info.child));
4230 try attributes.addParamAttr(llvm_arg_i, .{ .byval = child_type }, &o.builder);4210 try attributes.addParamAttr(llvm_arg_i, .{ .byval = child_type }, &o.builder);
4231 },4211 },
4232 }4212 }
...@@ -4292,8 +4272,8 @@ pub const Object = struct {...@@ -4292,8 +4272,8 @@ pub const Object = struct {
4292 }4272 }
42934273
4294 /// MLUGG TODO: this also needs incremental updates dumbass4274 /// MLUGG TODO: this also needs incremental updates dumbass
4295 pub fn getEnumTagNameFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {4275 pub fn getEnumTagNameFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4296 const zcu = pt.zcu;4276 const zcu = o.zcu;
4297 const ip = &zcu.intern_pool;4277 const ip = &zcu.intern_pool;
4298 const enum_type = ip.loadEnumType(enum_ty.toIntern());4278 const enum_type = ip.loadEnumType(enum_ty.toIntern());
42994279
...@@ -4301,11 +4281,12 @@ pub const Object = struct {...@@ -4301,11 +4281,12 @@ pub const Object = struct {
4301 if (gop.found_existing) return gop.value_ptr.ptrConst(&o.builder).kind.function;4281 if (gop.found_existing) return gop.value_ptr.ptrConst(&o.builder).kind.function;
4302 errdefer assert(o.enum_tag_name_map.remove(enum_ty.toIntern()));4282 errdefer assert(o.enum_tag_name_map.remove(enum_ty.toIntern()));
43034283
4304 const usize_ty = try o.lowerType(pt, Type.usize);4284 const usize_ty = try o.lowerType(.usize);
4305 const ret_ty = try o.lowerType(pt, Type.slice_const_u8_sentinel_0);4285 const ret_ty = try o.lowerType(.slice_const_u8_sentinel_0);
4286 const llvm_int_ty = try o.lowerType(.fromInterned(enum_type.int_tag_type));
4306 const target = &zcu.root_mod.resolved_target.result;4287 const target = &zcu.root_mod.resolved_target.result;
4307 const function_index = try o.builder.addFunction(4288 const function_index = try o.builder.addFunction(
4308 try o.builder.fnType(ret_ty, &.{try o.lowerType(pt, Type.fromInterned(enum_type.int_tag_type))}, .normal),4289 try o.builder.fnType(ret_ty, &.{llvm_int_ty}, .normal),
4309 try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}),4290 try o.builder.strtabStringFmt("__zig_tag_name_{f}", .{enum_type.name.fmt(ip)}),
4310 toLlvmAddressSpace(.generic, target),4291 toLlvmAddressSpace(.generic, target),
4311 );4292 );
...@@ -4353,11 +4334,11 @@ pub const Object = struct {...@@ -4353,11 +4334,11 @@ pub const Object = struct {
4353 });4334 });
43544335
4355 const return_block = try wip.block(1, "Name");4336 const return_block = try wip.block(1, "Name");
4356 const this_tag_int_value = try o.lowerValue(4337 const llvm_tag_val = switch (enum_type.field_values.getOrNone(ip, field_index)) {
4357 pt,4338 .none => try o.builder.intConst(llvm_int_ty, field_index), // auto-numbered
4358 (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(),4339 else => |tag_val_ip| try o.lowerValue(tag_val_ip),
4359 );4340 };
4360 try wip_switch.addCase(this_tag_int_value, return_block, &wip);4341 try wip_switch.addCase(llvm_tag_val, return_block, &wip);
43614342
4362 wip.cursor = .{ .block = return_block };4343 wip.cursor = .{ .block = return_block };
4363 _ = try wip.ret(name_val);4344 _ = try wip.ret(name_val);
...@@ -4375,8 +4356,8 @@ pub const Object = struct {...@@ -4375,8 +4356,8 @@ pub const Object = struct {
4375 return o.lazy_abi_aligns.items[@intFromEnum(index)];4356 return o.lazy_abi_aligns.items[@intFromEnum(index)];
4376 }4357 }
43774358
4378 pub fn getIsNamedEnumValueFunction(o: *Object, pt: Zcu.PerThread, enum_ty: Type) !Builder.Function.Index {4359 pub fn getIsNamedEnumValueFunction(o: *Object, enum_ty: Type) !Builder.Function.Index {
4379 const zcu = pt.zcu;4360 const zcu = o.zcu;
4380 const ip = &zcu.intern_pool;4361 const ip = &zcu.intern_pool;
43814362
4382 const gop = try o.named_enum_map.getOrPut(o.gpa, enum_ty.toIntern());4363 const gop = try o.named_enum_map.getOrPut(o.gpa, enum_ty.toIntern());
...@@ -4390,23 +4371,21 @@ pub const Object = struct {...@@ -4390,23 +4371,21 @@ pub const Object = struct {
4390 toLlvmAddressSpace(.generic, zcu.getTarget()),4371 toLlvmAddressSpace(.generic, zcu.getTarget()),
4391 );4372 );
4392 gop.value_ptr.* = function_index;4373 gop.value_ptr.* = function_index;
4393 try o.updateIsNamedEnumValueFunction(pt, enum_ty, function_index);4374 try o.updateIsNamedEnumValueFunction(enum_ty, function_index);
4394 return function_index;4375 return function_index;
4395 }4376 }
4396 fn updateIsNamedEnumValueFunction(4377 fn updateIsNamedEnumValueFunction(
4397 o: *Object,4378 o: *Object,
4398 pt: Zcu.PerThread,
4399 enum_ty: Type,4379 enum_ty: Type,
4400 function_index: Builder.Function.Index,4380 function_index: Builder.Function.Index,
4401 ) Allocator.Error!void {4381 ) Allocator.Error!void {
4402 const zcu = pt.zcu;4382 const zcu = o.zcu;
4403 const builder = &o.builder;4383 const builder = &o.builder;
4404 const loaded_enum = zcu.intern_pool.loadEnumType(enum_ty.toIntern());4384 const loaded_enum = zcu.intern_pool.loadEnumType(enum_ty.toIntern());
4405 function_index.ptrConst(builder).global.ptr(builder).type = try builder.fnType(4385
4406 .i1,4386 const llvm_int_ty = try o.lowerType(.fromInterned(loaded_enum.int_tag_type));
4407 &.{try o.lowerType(pt, .fromInterned(loaded_enum.int_tag_type))},4387 function_index.ptrConst(builder).global.ptr(builder).type =
4408 .normal,4388 try builder.fnType(.i1, &.{llvm_int_ty}, .normal);
4409 );
44104389
4411 var attributes: Builder.FunctionAttributes.Wip = .{};4390 var attributes: Builder.FunctionAttributes.Wip = .{};
4412 defer attributes.deinit(builder);4391 defer attributes.deinit(builder);
...@@ -4429,13 +4408,19 @@ pub const Object = struct {...@@ -4429,13 +4408,19 @@ pub const Object = struct {
4429 var wip_switch = try wip.@"switch"(tag_int_value, unnamed_block, @intCast(loaded_enum.field_names.len), .none);4408 var wip_switch = try wip.@"switch"(tag_int_value, unnamed_block, @intCast(loaded_enum.field_names.len), .none);
4430 defer wip_switch.finish(&wip);4409 defer wip_switch.finish(&wip);
44314410
4432 for (0..loaded_enum.field_names.len) |field_index| {4411 if (loaded_enum.field_values.len > 0) {
4433 const this_tag_int_value = try o.lowerValue(4412 for (loaded_enum.field_values.get(&zcu.intern_pool)) |tag_val_ip| {
4434 pt,4413 const llvm_tag_val = try o.lowerValue(tag_val_ip);
4435 (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(),4414 try wip_switch.addCase(llvm_tag_val, named_block, &wip);
4436 );4415 }
4437 try wip_switch.addCase(this_tag_int_value, named_block, &wip);4416 } else {
4417 // Auto-numbered.
4418 for (0..loaded_enum.field_names.len) |field_index| {
4419 const llvm_tag_val = try o.builder.intConst(llvm_int_ty, field_index);
4420 try wip_switch.addCase(llvm_tag_val, named_block, &wip);
4421 }
4438 }4422 }
4423
4439 wip.cursor = .{ .block = named_block };4424 wip.cursor = .{ .block = named_block };
4440 _ = try wip.ret(.true);4425 _ = try wip.ret(.true);
44414426
src/codegen/llvm/FuncGen.zig+348-403
...@@ -73,7 +73,7 @@ const TodoError = Zcu.CodegenFailError;...@@ -73,7 +73,7 @@ const TodoError = Zcu.CodegenFailError;
73/// Avoid introducing new calls to this function---see documentation comment on `TodoError`.73/// Avoid introducing new calls to this function---see documentation comment on `TodoError`.
74fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {74fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {
75 @branchHint(.cold);75 @branchHint(.cold);
76 return fg.pt.zcu.codegenFail(76 return fg.object.zcu.codegenFail(
77 fg.nav_index,77 fg.nav_index,
78 "TODO (LLVM): " ++ format,78 "TODO (LLVM): " ++ format,
79 args,79 args,
...@@ -81,7 +81,7 @@ fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {...@@ -81,7 +81,7 @@ fn todo(fg: *FuncGen, comptime format: []const u8, args: anytype) TodoError {
81}81}
8282
83fn ownerModule(fg: *const FuncGen) *Package.Module {83fn ownerModule(fg: *const FuncGen) *Package.Module {
84 return fg.pt.zcu.navFileScope(fg.nav_index).mod.?;84 return fg.object.zcu.navFileScope(fg.nav_index).mod.?;
85}85}
8686
87fn maybeMarkAllowZeroAccess(self: *FuncGen, info: InternPool.Key.PtrType) void {87fn maybeMarkAllowZeroAccess(self: *FuncGen, info: InternPool.Key.PtrType) void {
...@@ -154,34 +154,35 @@ fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) Allocator.Error!Builder.Value...@@ -154,34 +154,35 @@ fn resolveInst(self: *FuncGen, inst: Air.Inst.Ref) Allocator.Error!Builder.Value
154 const gop = try self.func_inst_table.getOrPut(gpa, inst);154 const gop = try self.func_inst_table.getOrPut(gpa, inst);
155 if (gop.found_existing) return gop.value_ptr.*;155 if (gop.found_existing) return gop.value_ptr.*;
156156
157 const llvm_val = try self.resolveValue((try self.air.value(inst, self.pt)).?);157 const llvm_val = try self.resolveValue(.fromInterned(inst.toInterned().?));
158 gop.value_ptr.* = llvm_val.toValue();158 gop.value_ptr.* = llvm_val.toValue();
159 return llvm_val.toValue();159 return llvm_val.toValue();
160}160}
161161
162fn resolveValue(self: *FuncGen, val: Value) Allocator.Error!Builder.Constant {162fn resolveValue(self: *FuncGen, val: Value) Allocator.Error!Builder.Constant {
163 const o = self.object;163 const o = self.object;
164 const pt = self.pt;164 const zcu = o.zcu;
165 const zcu = pt.zcu;
166 const ty = val.typeOf(zcu);165 const ty = val.typeOf(zcu);
167 if (!isByRef(ty, zcu)) {166 if (!isByRef(ty, zcu)) {
168 return o.lowerValue(pt, val.toIntern());167 return o.lowerValue(val.toIntern());
169 } else {168 } else {
170 // We need a pointer to a global constant, i.e. a UAV.169 // We need a pointer to a global constant, i.e. a UAV.
171 return o.lowerUavRef(pt, .{170 return o.lowerUavRef(
172 .val = val.toIntern(),171 val.toIntern(),
173 .orig_ty = (try pt.singleConstPtrType(ty)).toIntern(),172 ty.abiAlignment(zcu),
174 });173 target_util.defaultAddressSpace(zcu.getTarget(), .global_constant),
174 );
175 }175 }
176}176}
177177
178/// MLUGG TODO okay yeah prolly delete this again
178fn lowerType(fg: *const FuncGen, ty: Type) Allocator.Error!Builder.Type {179fn lowerType(fg: *const FuncGen, ty: Type) Allocator.Error!Builder.Type {
179 return fg.object.lowerType(fg.pt, ty);180 return fg.object.lowerType(ty);
180}181}
181182
182pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) TodoError!void {183pub fn genBody(self: *FuncGen, body: []const Air.Inst.Index, coverage_point: Air.CoveragePoint) TodoError!void {
183 const o = self.object;184 const o = self.object;
184 const zcu = self.pt.zcu;185 const zcu = self.object.zcu;
185 const ip = &zcu.intern_pool;186 const ip = &zcu.intern_pool;
186 const air_tags = self.air.instructions.items(.tag);187 const air_tags = self.air.instructions.items(.tag);
187 switch (coverage_point) {188 switch (coverage_point) {
...@@ -514,8 +515,7 @@ fn genBodyDebugScope(...@@ -514,8 +515,7 @@ fn genBodyDebugScope(
514 defer self.scope = old_scope;515 defer self.scope = old_scope;
515516
516 if (maybe_inline_func) |inline_func| {517 if (maybe_inline_func) |inline_func| {
517 const pt = self.pt;518 const zcu = o.zcu;
518 const zcu = pt.zcu;
519 const ip = &zcu.intern_pool;519 const ip = &zcu.intern_pool;
520520
521 const func = zcu.funcInfo(inline_func);521 const func = zcu.funcInfo(inline_func);
...@@ -529,18 +529,13 @@ fn genBodyDebugScope(...@@ -529,18 +529,13 @@ fn genBodyDebugScope(
529 const line_number = self.base_line + 1;529 const line_number = self.base_line + 1;
530 self.inlined_at = try self.wip.debug_location.toMetadata(&o.builder);530 self.inlined_at = try self.wip.debug_location.toMetadata(&o.builder);
531531
532 const fn_ty = try pt.funcType(.{
533 .param_types = &.{},
534 .return_type = .void_type,
535 });
536
537 self.scope = try o.builder.debugSubprogram(532 self.scope = try o.builder.debugSubprogram(
538 self.file,533 self.file,
539 try o.builder.metadataString(nav.name.toSlice(&zcu.intern_pool)),534 try o.builder.metadataString(nav.name.toSlice(&zcu.intern_pool)),
540 try o.builder.metadataString(nav.fqn.toSlice(&zcu.intern_pool)),535 try o.builder.metadataString(nav.fqn.toSlice(&zcu.intern_pool)),
541 line_number,536 line_number,
542 line_number + func.lbrace_line,537 line_number + func.lbrace_line,
543 try o.getDebugType(pt, fn_ty),538 try o.builder.debugSubroutineType(null),
544 .{539 .{
545 .di_flags = .{ .StaticMember = true },540 .di_flags = .{ .StaticMember = true },
546 .sp_flags = .{541 .sp_flags = .{
...@@ -582,7 +577,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -582,7 +577,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
582 const args = air_call.args;577 const args = air_call.args;
583 const o = self.object;578 const o = self.object;
584 const pt = self.pt;579 const pt = self.pt;
585 const zcu = pt.zcu;580 const zcu = o.zcu;
586 const ip = &zcu.intern_pool;581 const ip = &zcu.intern_pool;
587 const callee_ty = self.typeOf(air_call.callee);582 const callee_ty = self.typeOf(air_call.callee);
588 const zig_fn_ty = switch (callee_ty.zigTypeTag(zcu)) {583 const zig_fn_ty = switch (callee_ty.zigTypeTag(zcu)) {
...@@ -628,7 +623,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -628,7 +623,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
628 try llvm_args.append(self.err_ret_trace);623 try llvm_args.append(self.err_ret_trace);
629 }624 }
630625
631 var it = iterateParamTypes(o, pt, fn_info);626 var it = iterateParamTypes(o, fn_info);
632 while (try it.nextCall(self, args)) |lowering| switch (lowering) {627 while (try it.nextCall(self, args)) |lowering| switch (lowering) {
633 .no_bits => continue,628 .no_bits => continue,
634 .byval => {629 .byval => {
...@@ -761,7 +756,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -761,7 +756,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
761756
762 {757 {
763 // Add argument attributes.758 // Add argument attributes.
764 it = iterateParamTypes(o, pt, fn_info);759 it = iterateParamTypes(o, fn_info);
765 it.llvm_index += @intFromBool(sret);760 it.llvm_index += @intFromBool(sret);
766 it.llvm_index += @intFromBool(err_return_tracing);761 it.llvm_index += @intFromBool(err_return_tracing);
767 while (try it.next()) |lowering| switch (lowering) {762 while (try it.next()) |lowering| switch (lowering) {
...@@ -852,7 +847,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -852,7 +847,7 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
852 }847 }
853 }848 }
854849
855 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);850 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
856851
857 if (abi_ret_ty != llvm_ret_ty) {852 if (abi_ret_ty != llvm_ret_ty) {
858 // In this case the function return type is honoring the calling convention by having853 // In this case the function return type is honoring the calling convention by having
...@@ -881,12 +876,11 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif...@@ -881,12 +876,11 @@ fn airCall(self: *FuncGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif
881876
882fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!void {877fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!void {
883 const o = fg.object;878 const o = fg.object;
884 const pt = fg.pt;879 const zcu = o.zcu;
885 const zcu = pt.zcu;
886 const target = zcu.getTarget();880 const target = zcu.getTarget();
887 const panic_func = zcu.funcInfo(zcu.builtin_decl_values.get(panic_id.toBuiltin()));881 const panic_func = zcu.funcInfo(zcu.builtin_decl_values.get(panic_id.toBuiltin()));
888 const fn_info = zcu.typeToFunc(.fromInterned(panic_func.ty)).?;882 const fn_info = zcu.typeToFunc(.fromInterned(panic_func.ty)).?;
889 const panic_global = try o.resolveLlvmFunction(pt, panic_func.owner_nav);883 const panic_global = try o.resolveLlvmFunction(panic_func.owner_nav);
890884
891 const has_err_trace = zcu.comp.config.any_error_tracing and fn_info.cc == .auto;885 const has_err_trace = zcu.comp.config.any_error_tracing and fn_info.cc == .auto;
892 if (has_err_trace) assert(fg.err_ret_trace != .none);886 if (has_err_trace) assert(fg.err_ret_trace != .none);
...@@ -905,30 +899,19 @@ fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!v...@@ -905,30 +899,19 @@ fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.SimplePanicId) Allocator.Error!v
905899
906fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!void {900fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!void {
907 const o = self.object;901 const o = self.object;
908 const pt = self.pt;902 const zcu = o.zcu;
909 const zcu = pt.zcu;
910 const ip = &zcu.intern_pool;903 const ip = &zcu.intern_pool;
911 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;904 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
912 const ret_ty = self.typeOf(un_op);905 const ret_ty = self.typeOf(un_op);
913906
914 if (self.ret_ptr != .none) {907 if (self.ret_ptr != .none) {
915 const ptr_ty = try pt.singleMutPtrType(ret_ty);
916
917 const operand = try self.resolveInst(un_op);908 const operand = try self.resolveInst(un_op);
918 const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndef(zcu) else false;909 const val_is_undef = if (un_op.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
919 if (val_is_undef and safety) undef: {910 if (val_is_undef and safety) {
920 const ptr_info = ptr_ty.ptrInfo(zcu);
921 const needs_bitmask = (ptr_info.packed_offset.host_size != 0);
922 if (needs_bitmask) {
923 // TODO: only some bits are to be undef, we cannot write with a simple memset.
924 // meanwhile, ignore the write rather than stomping over valid bits.
925 // https://github.com/ziglang/zig/issues/15337
926 break :undef;
927 }
928 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));911 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
929 _ = try self.wip.callMemSet(912 _ = try self.wip.callMemSet(
930 self.ret_ptr,913 self.ret_ptr,
931 ptr_ty.ptrAlignment(zcu).toLlvm(),914 ret_ty.abiAlignment(zcu).toLlvm(),
932 try o.builder.intValue(.i8, 0xaa),915 try o.builder.intValue(.i8, 0xaa),
933 len,916 len,
934 .normal,917 .normal,
...@@ -951,7 +934,12 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo...@@ -951,7 +934,12 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
951 return;934 return;
952 }935 }
953936
954 try self.store(self.ret_ptr, ptr_ty, operand, .none);937 try self.store(
938 self.ret_ptr,
939 .none,
940 operand,
941 ret_ty,
942 );
955 _ = try self.wip.retVoid();943 _ = try self.wip.retVoid();
956 return;944 return;
957 }945 }
...@@ -968,15 +956,15 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo...@@ -968,15 +956,15 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
968 return;956 return;
969 }957 }
970958
971 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);959 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
972 const operand = try self.resolveInst(un_op);960 const operand = try self.resolveInst(un_op);
973 const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndef(zcu) else false;961 const val_is_undef = if (un_op.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
974 const alignment = ret_ty.abiAlignment(zcu).toLlvm();962 const alignment = ret_ty.abiAlignment(zcu).toLlvm();
975963
976 if (val_is_undef and safety) {964 if (val_is_undef and safety) {
977 const llvm_ret_ty = operand.typeOfWip(&self.wip);965 const llvm_ret_ty = operand.typeOfWip(&self.wip);
978 const rp = try self.buildAlloca(llvm_ret_ty, alignment);966 const rp = try self.buildAlloca(llvm_ret_ty, alignment);
979 const len = try o.builder.intValue(try self.lowerType(Type.usize), ret_ty.abiSize(zcu));967 const len = try o.builder.intValue(try self.lowerType(.usize), ret_ty.abiSize(zcu));
980 _ = try self.wip.callMemSet(968 _ = try self.wip.callMemSet(
981 rp,969 rp,
982 alignment,970 alignment,
...@@ -1014,8 +1002,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo...@@ -1014,8 +1002,7 @@ fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!vo
10141002
1015fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {1003fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
1016 const o = self.object;1004 const o = self.object;
1017 const pt = self.pt;1005 const zcu = o.zcu;
1018 const zcu = pt.zcu;
1019 const ip = &zcu.intern_pool;1006 const ip = &zcu.intern_pool;
1020 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;1007 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
1021 const ptr_ty = self.typeOf(un_op);1008 const ptr_ty = self.typeOf(un_op);
...@@ -1037,7 +1024,7 @@ fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {...@@ -1037,7 +1024,7 @@ fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
1037 return;1024 return;
1038 }1025 }
1039 const ptr = try self.resolveInst(un_op);1026 const ptr = try self.resolveInst(un_op);
1040 const abi_ret_ty = try lowerFnRetTy(o, pt, fn_info);1027 const abi_ret_ty = try lowerFnRetTy(o, fn_info);
1041 const alignment = ret_ty.abiAlignment(zcu).toLlvm();1028 const alignment = ret_ty.abiAlignment(zcu).toLlvm();
1042 _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, ptr, alignment, ""));1029 _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, ptr, alignment, ""));
1043 return;1030 return;
...@@ -1053,14 +1040,13 @@ fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -1053,14 +1040,13 @@ fn airCVaArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
1053}1040}
10541041
1055fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {1042fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1056 const pt = self.pt;1043 const zcu = self.object.zcu;
1057 const zcu = pt.zcu;
1058 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;1044 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1059 const src_list = try self.resolveInst(ty_op.operand);1045 const src_list = try self.resolveInst(ty_op.operand);
1060 const va_list_ty = ty_op.ty.toType();1046 const va_list_ty = ty_op.ty.toType();
1061 const llvm_va_list_ty = try self.lowerType(va_list_ty);1047 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10621048
1063 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();1049 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
1064 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);1050 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
10651051
1066 _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{dest_list.typeOfWip(&self.wip)}, &.{ dest_list, src_list }, "");1052 _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{dest_list.typeOfWip(&self.wip)}, &.{ dest_list, src_list }, "");
...@@ -1079,12 +1065,11 @@ fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -1079,12 +1065,11 @@ fn airCVaEnd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
1079}1065}
10801066
1081fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {1067fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1082 const pt = self.pt;1068 const zcu = self.object.zcu;
1083 const zcu = pt.zcu;
1084 const va_list_ty = self.typeOfIndex(inst);1069 const va_list_ty = self.typeOfIndex(inst);
1085 const llvm_va_list_ty = try self.lowerType(va_list_ty);1070 const llvm_va_list_ty = try self.lowerType(va_list_ty);
10861071
1087 const result_alignment = va_list_ty.abiAlignment(pt.zcu).toLlvm();1072 const result_alignment = va_list_ty.abiAlignment(zcu).toLlvm();
1088 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);1073 const dest_list = try self.buildAlloca(llvm_va_list_ty, result_alignment);
10891074
1090 _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{dest_list.typeOfWip(&self.wip)}, &.{dest_list}, "");1075 _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{dest_list.typeOfWip(&self.wip)}, &.{dest_list}, "");
...@@ -1145,8 +1130,7 @@ fn cmp(...@@ -1145,8 +1130,7 @@ fn cmp(
1145 rhs: Builder.Value,1130 rhs: Builder.Value,
1146) Allocator.Error!Builder.Value {1131) Allocator.Error!Builder.Value {
1147 const o = self.object;1132 const o = self.object;
1148 const pt = self.pt;1133 const zcu = o.zcu;
1149 const zcu = pt.zcu;
1150 const scalar_ty = operand_ty.scalarType(zcu);1134 const scalar_ty = operand_ty.scalarType(zcu);
1151 const int_ty = switch (scalar_ty.zigTypeTag(zcu)) {1135 const int_ty = switch (scalar_ty.zigTypeTag(zcu)) {
1152 .@"enum" => scalar_ty.intTagType(zcu),1136 .@"enum" => scalar_ty.intTagType(zcu),
...@@ -1244,8 +1228,7 @@ fn lowerBlock(...@@ -1244,8 +1228,7 @@ fn lowerBlock(
1244 maybe_inline_func: ?InternPool.Index,1228 maybe_inline_func: ?InternPool.Index,
1245 body: []const Air.Inst.Index,1229 body: []const Air.Inst.Index,
1246) TodoError!Builder.Value {1230) TodoError!Builder.Value {
1247 const pt = self.pt;1231 const zcu = self.object.zcu;
1248 const zcu = pt.zcu;
1249 const inst_ty = self.typeOfIndex(inst);1232 const inst_ty = self.typeOfIndex(inst);
12501233
1251 if (inst_ty.isNoReturn(zcu)) {1234 if (inst_ty.isNoReturn(zcu)) {
...@@ -1294,7 +1277,7 @@ fn lowerBlock(...@@ -1294,7 +1277,7 @@ fn lowerBlock(
1294}1277}
12951278
1296fn airBr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {1279fn airBr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
1297 const zcu = self.pt.zcu;1280 const zcu = self.object.zcu;
1298 const branch = self.air.instructions.items(.data)[@intFromEnum(inst)].br;1281 const branch = self.air.instructions.items(.data)[@intFromEnum(inst)].br;
1299 const block = self.blocks.get(branch.block_inst).?;1282 const block = self.blocks.get(branch.block_inst).?;
13001283
...@@ -1324,12 +1307,12 @@ fn lowerSwitchDispatch(...@@ -1324,12 +1307,12 @@ fn lowerSwitchDispatch(
1324 dispatch_info: SwitchDispatchInfo,1307 dispatch_info: SwitchDispatchInfo,
1325) Allocator.Error!void {1308) Allocator.Error!void {
1326 const o = self.object;1309 const o = self.object;
1327 const pt = self.pt;1310 const zcu = o.zcu;
1328 const zcu = pt.zcu;
1329 const cond_ty = self.typeOf(cond_ref);1311 const cond_ty = self.typeOf(cond_ref);
1330 const switch_br = self.air.unwrapSwitch(switch_inst);1312 const switch_br = self.air.unwrapSwitch(switch_inst);
13311313
1332 if (try self.air.value(cond_ref, pt)) |cond_val| {1314 if (cond_ref.toInterned()) |cond_ip_index| {
1315 const cond_val: Value = .fromInterned(cond_ip_index);
1333 // Comptime-known dispatch. Iterate the cases to find the correct1316 // Comptime-known dispatch. Iterate the cases to find the correct
1334 // one, and branch to the corresponding element of `case_blocks`.1317 // one, and branch to the corresponding element of `case_blocks`.
1335 var it = switch_br.iterateCases();1318 var it = switch_br.iterateCases();
...@@ -1413,7 +1396,7 @@ fn lowerSwitchDispatch(...@@ -1413,7 +1396,7 @@ fn lowerSwitchDispatch(
1413 // The switch prongs will correspond to our scalar cases. Ranges will1396 // The switch prongs will correspond to our scalar cases. Ranges will
1414 // be handled by conditional branches in the `else` prong.1397 // be handled by conditional branches in the `else` prong.
14151398
1416 const llvm_usize = try self.lowerType(Type.usize);1399 const llvm_usize = try self.lowerType(.usize);
1417 const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder))1400 const cond_int = if (cond.typeOfWip(&self.wip).isPointer(&o.builder))
1418 try self.wip.cast(.ptrtoint, cond, llvm_usize, "")1401 try self.wip.cast(.ptrtoint, cond, llvm_usize, "")
1419 else1402 else
...@@ -1581,7 +1564,7 @@ fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builde...@@ -1581,7 +1564,7 @@ fn airTry(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builde
1581}1564}
15821565
1583fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builder.Value {1566fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index, err_cold: bool) TodoError!Builder.Value {
1584 const zcu = self.pt.zcu;1567 const zcu = self.object.zcu;
1585 const unwrapped_try = self.air.unwrapTryPtr(inst);1568 const unwrapped_try = self.air.unwrapTryPtr(inst);
1586 const err_union_ptr = try self.resolveInst(unwrapped_try.error_union_ptr);1569 const err_union_ptr = try self.resolveInst(unwrapped_try.error_union_ptr);
1587 const body = unwrapped_try.else_body;1570 const body = unwrapped_try.else_body;
...@@ -1605,8 +1588,7 @@ fn lowerTry(...@@ -1605,8 +1588,7 @@ fn lowerTry(
1605 err_cold: bool,1588 err_cold: bool,
1606) TodoError!Builder.Value {1589) TodoError!Builder.Value {
1607 const o = fg.object;1590 const o = fg.object;
1608 const pt = fg.pt;1591 const zcu = o.zcu;
1609 const zcu = pt.zcu;
1610 const payload_ty = err_union_ty.errorUnionPayload(zcu);1592 const payload_ty = err_union_ty.errorUnionPayload(zcu);
1611 const payload_has_bits = payload_ty.hasRuntimeBits(zcu);1593 const payload_has_bits = payload_ty.hasRuntimeBits(zcu);
1612 const error_type = try o.errorIntType();1594 const error_type = try o.errorIntType();
...@@ -1667,8 +1649,7 @@ fn lowerTry(...@@ -1667,8 +1649,7 @@ fn lowerTry(
16671649
1668fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) TodoError!void {1650fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) TodoError!void {
1669 const o = self.object;1651 const o = self.object;
1670 const pt = self.pt;1652 const zcu = o.zcu;
1671 const zcu = pt.zcu;
16721653
1673 const switch_br = self.air.unwrapSwitch(inst);1654 const switch_br = self.air.unwrapSwitch(inst);
16741655
...@@ -1773,8 +1754,8 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod...@@ -1773,8 +1754,8 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod
1773 const table_includes_else = item_count != table_len;1754 const table_includes_else = item_count != table_len;
17741755
1775 break :jmp_table .{1756 break :jmp_table .{
1776 .min = try o.lowerValue(pt, min.toIntern()),1757 .min = try o.lowerValue(min.toIntern()),
1777 .max = try o.lowerValue(pt, max.toIntern()),1758 .max = try o.lowerValue(max.toIntern()),
1778 .in_bounds_hint = if (table_includes_else) .none else switch (switch_br.getElseHint()) {1759 .in_bounds_hint = if (table_includes_else) .none else switch (switch_br.getElseHint()) {
1779 .none, .cold => .none,1760 .none, .cold => .none,
1780 .unpredictable => .unpredictable,1761 .unpredictable => .unpredictable,
...@@ -1883,7 +1864,7 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod...@@ -1883,7 +1864,7 @@ fn airSwitchBr(self: *FuncGen, inst: Air.Inst.Index, is_dispatch_loop: bool) Tod
1883}1864}
18841865
1885fn switchCaseItemRange(self: *FuncGen, switch_br: Air.UnwrappedSwitch) ?[2]Value {1866fn switchCaseItemRange(self: *FuncGen, switch_br: Air.UnwrappedSwitch) ?[2]Value {
1886 const zcu = self.pt.zcu;1867 const zcu = self.object.zcu;
1887 var it = switch_br.iterateCases();1868 var it = switch_br.iterateCases();
1888 var min: ?Value = null;1869 var min: ?Value = null;
1889 var max: ?Value = null;1870 var max: ?Value = null;
...@@ -1928,8 +1909,7 @@ fn airLoop(self: *FuncGen, inst: Air.Inst.Index) TodoError!void {...@@ -1928,8 +1909,7 @@ fn airLoop(self: *FuncGen, inst: Air.Inst.Index) TodoError!void {
19281909
1929fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {1910fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
1930 const o = self.object;1911 const o = self.object;
1931 const pt = self.pt;1912 const zcu = o.zcu;
1932 const zcu = pt.zcu;
1933 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;1913 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
1934 const operand_ty = self.typeOf(ty_op.operand);1914 const operand_ty = self.typeOf(ty_op.operand);
1935 const array_ty = operand_ty.childType(zcu);1915 const array_ty = operand_ty.childType(zcu);
...@@ -1942,8 +1922,7 @@ fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder...@@ -1942,8 +1922,7 @@ fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
19421922
1943fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {1923fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
1944 const o = self.object;1924 const o = self.object;
1945 const pt = self.pt;1925 const zcu = o.zcu;
1946 const zcu = pt.zcu;
1947 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;1926 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
19481927
1949 const operand = try self.resolveInst(ty_op.operand);1928 const operand = try self.resolveInst(ty_op.operand);
...@@ -1964,7 +1943,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value...@@ -1964,7 +1943,7 @@ fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value
1964 );1943 );
19651944
1966 const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(zcu))) orelse {1945 const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(zcu))) orelse {
1967 return self.todo("float_from_int from '{f}' without intrinsics", .{operand_scalar_ty.fmt(pt)});1946 return self.todo("float_from_int on {d} bit integer", .{operand_scalar_ty.bitSize(zcu)});
1968 };1947 };
1969 const rt_int_ty = try o.builder.intType(rt_int_bits);1948 const rt_int_ty = try o.builder.intType(rt_int_bits);
1970 var extended = try self.wip.conv(1949 var extended = try self.wip.conv(
...@@ -2011,8 +1990,7 @@ fn airIntFromFloat(...@@ -2011,8 +1990,7 @@ fn airIntFromFloat(
2011 _ = fast;1990 _ = fast;
20121991
2013 const o = self.object;1992 const o = self.object;
2014 const pt = self.pt;1993 const zcu = o.zcu;
2015 const zcu = pt.zcu;
2016 const target = zcu.getTarget();1994 const target = zcu.getTarget();
2017 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;1995 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
20181996
...@@ -2035,7 +2013,7 @@ fn airIntFromFloat(...@@ -2035,7 +2013,7 @@ fn airIntFromFloat(
2035 }2013 }
20362014
2037 const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(zcu))) orelse {2015 const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(zcu))) orelse {
2038 return self.todo("int_from_float to '{f}' without intrinsics", .{dest_scalar_ty.fmt(pt)});2016 return self.todo("int_from_float to {d} bit integer", .{dest_scalar_ty.bitSize(zcu)});
2039 };2017 };
2040 const ret_ty = try o.builder.intType(rt_int_bits);2018 const ret_ty = try o.builder.intType(rt_int_bits);
2041 const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: {2019 const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: {
...@@ -2074,14 +2052,13 @@ fn airIntFromFloat(...@@ -2074,14 +2052,13 @@ fn airIntFromFloat(
2074}2052}
20752053
2076fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {2054fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {
2077 const zcu = fg.pt.zcu;2055 const zcu = fg.object.zcu;
2078 return if (ty.isSlice(zcu)) fg.wip.extractValue(ptr, &.{0}, "") else ptr;2056 return if (ty.isSlice(zcu)) fg.wip.extractValue(ptr, &.{0}, "") else ptr;
2079}2057}
20802058
2081fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {2059fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value {
2082 const o = fg.object;2060 const o = fg.object;
2083 const pt = fg.pt;2061 const zcu = o.zcu;
2084 const zcu = pt.zcu;
2085 const llvm_usize = try fg.lowerType(.usize);2062 const llvm_usize = try fg.lowerType(.usize);
2086 switch (ty.ptrSize(zcu)) {2063 switch (ty.ptrSize(zcu)) {
2087 .slice => {2064 .slice => {
...@@ -2109,15 +2086,14 @@ fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) Allocator.Err...@@ -2109,15 +2086,14 @@ fn airSliceField(self: *FuncGen, inst: Air.Inst.Index, index: u32) Allocator.Err
2109}2086}
21102087
2111fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: u1) Allocator.Error!Builder.Value {2088fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: u1) Allocator.Error!Builder.Value {
2112 const zcu = self.pt.zcu;2089 const zcu = self.object.zcu;
2113 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;2090 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2114 const slice_ptr = try self.resolveInst(ty_op.operand);2091 const slice_ptr = try self.resolveInst(ty_op.operand);
2115 return self.ptraddConst(slice_ptr, index * Type.usize.abiSize(zcu));2092 return self.ptraddConst(slice_ptr, index * Type.usize.abiSize(zcu));
2116}2093}
21172094
2118fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2095fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2119 const pt = self.pt;2096 const zcu = self.object.zcu;
2120 const zcu = pt.zcu;
2121 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;2097 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2122 const slice_ty = self.typeOf(bin_op.lhs);2098 const slice_ty = self.typeOf(bin_op.lhs);
2123 const slice = try self.resolveInst(bin_op.lhs);2099 const slice = try self.resolveInst(bin_op.lhs);
...@@ -2138,8 +2114,7 @@ fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder...@@ -2138,8 +2114,7 @@ fn airSliceElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
2138}2114}
21392115
2140fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2116fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2141 const pt = self.pt;2117 const zcu = self.object.zcu;
2142 const zcu = pt.zcu;
2143 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;2118 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2144 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;2119 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2145 const slice_ty = self.typeOf(bin_op.lhs);2120 const slice_ty = self.typeOf(bin_op.lhs);
...@@ -2151,8 +2126,7 @@ fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder...@@ -2151,8 +2126,7 @@ fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
2151}2126}
21522127
2153fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2128fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2154 const pt = self.pt;2129 const zcu = self.object.zcu;
2155 const zcu = pt.zcu;
21562130
2157 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;2131 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2158 const array_ty = self.typeOf(bin_op.lhs);2132 const array_ty = self.typeOf(bin_op.lhs);
...@@ -2174,28 +2148,25 @@ fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder...@@ -2174,28 +2148,25 @@ fn airArrayElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
2174}2148}
21752149
2176fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2150fn airPtrElemVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2177 const pt = self.pt;2151 const zcu = self.object.zcu;
2178 const zcu = pt.zcu;
2179 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;2152 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2180 const ptr_ty = self.typeOf(bin_op.lhs);2153 const ptr_ty = self.typeOf(bin_op.lhs);
2181 const elem_ty = ptr_ty.indexableElem(zcu);2154 const elem_ty = ptr_ty.indexableElem(zcu);
2182 const base_ptr = try self.resolveInst(bin_op.lhs);2155 const base_ptr = try self.resolveInst(bin_op.lhs);
2183 const rhs = try self.resolveInst(bin_op.rhs);2156 const rhs = try self.resolveInst(bin_op.rhs);
2184 const ptr = try self.ptraddScaled(base_ptr, rhs, elem_ty.abiSize(zcu));
2185 if (isByRef(elem_ty, zcu)) {
2186 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
2187 const ptr_align = (ptr_ty.ptrAlignment(zcu).min(elem_ty.abiAlignment(zcu))).toLlvm();
2188 return self.loadByRef(ptr, elem_ty, ptr_align, if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal);
2189 }
21902157
2191 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));2158 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
21922159
2193 return self.load(ptr, ptr_ty);2160 return self.load(
2161 try self.ptraddScaled(base_ptr, rhs, elem_ty.abiSize(zcu)),
2162 elem_ty,
2163 ptr_ty.ptrAlignment(zcu).min(elem_ty.abiAlignment(zcu)).toLlvm(),
2164 if (ptr_ty.isVolatilePtr(zcu)) .@"volatile" else .normal,
2165 );
2194}2166}
21952167
2196fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2168fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2197 const pt = self.pt;2169 const zcu = self.object.zcu;
2198 const zcu = pt.zcu;
2199 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;2170 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2200 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;2171 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2201 const ptr_ty = self.typeOf(bin_op.lhs);2172 const ptr_ty = self.typeOf(bin_op.lhs);
...@@ -2232,8 +2203,7 @@ fn airStructFieldPtrIndex(...@@ -2232,8 +2203,7 @@ fn airStructFieldPtrIndex(
22322203
2233fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2204fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2234 const o = self.object;2205 const o = self.object;
2235 const pt = self.pt;2206 const zcu = o.zcu;
2236 const zcu = pt.zcu;
2237 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;2207 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2238 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;2208 const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data;
2239 const struct_ty = self.typeOf(struct_field.struct_operand);2209 const struct_ty = self.typeOf(struct_field.struct_operand);
...@@ -2298,8 +2268,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build...@@ -2298,8 +2268,7 @@ fn airStructFieldVal(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
22982268
2299fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2269fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2300 const o = self.object;2270 const o = self.object;
2301 const pt = self.pt;2271 const zcu = o.zcu;
2302 const zcu = pt.zcu;
2303 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;2272 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
2304 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;2273 const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
23052274
...@@ -2310,7 +2279,7 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build...@@ -2310,7 +2279,7 @@ fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
2310 if (field_offset == 0) return field_ptr;2279 if (field_offset == 0) return field_ptr;
23112280
2312 const res_ty = try self.lowerType(ty_pl.ty.toType());2281 const res_ty = try self.lowerType(ty_pl.ty.toType());
2313 const llvm_usize = try self.lowerType(Type.usize);2282 const llvm_usize = try self.lowerType(.usize);
23142283
2315 const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, "");2284 const field_ptr_int = try self.wip.cast(.ptrtoint, field_ptr, llvm_usize, "");
2316 const base_ptr_int = try self.wip.bin(2285 const base_ptr_int = try self.wip.bin(
...@@ -2358,7 +2327,7 @@ fn airDbgEmptyStmt(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder...@@ -2358,7 +2327,7 @@ fn airDbgEmptyStmt(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder
2358fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2327fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
2359 const o = self.object;2328 const o = self.object;
2360 const pt = self.pt;2329 const pt = self.pt;
2361 const zcu = pt.zcu;2330 const zcu = o.zcu;
2362 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;2331 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
2363 const operand = try self.resolveInst(pl_op.operand);2332 const operand = try self.resolveInst(pl_op.operand);
2364 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);2333 const name: Air.NullTerminatedString = @enumFromInt(pl_op.payload);
...@@ -2415,7 +2384,7 @@ fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) Allocator.Er...@@ -2415,7 +2384,7 @@ fn airDbgVarVal(self: *FuncGen, inst: Air.Inst.Index, is_arg: bool) Allocator.Er
2415 try o.getDebugType(pt, operand_ty),2384 try o.getDebugType(pt, operand_ty),
2416 );2385 );
24172386
2418 const zcu = pt.zcu;2387 const zcu = o.zcu;
2419 const owner_mod = self.ownerModule();2388 const owner_mod = self.ownerModule();
2420 if (isByRef(operand_ty, zcu)) {2389 if (isByRef(operand_ty, zcu)) {
2421 _ = try self.wip.callIntrinsic(2390 _ = try self.wip.callIntrinsic(
...@@ -2501,8 +2470,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {...@@ -2501,8 +2470,7 @@ fn airAssembly(self: *FuncGen, inst: Air.Inst.Index) TodoError!Builder.Value {
2501 // This stores whether we need to add an elementtype attribute and2470 // This stores whether we need to add an elementtype attribute and
2502 // if so, the element type itself.2471 // if so, the element type itself.
2503 const llvm_param_attrs = try arena.alloc(Builder.Type, max_param_count);2472 const llvm_param_attrs = try arena.alloc(Builder.Type, max_param_count);
2504 const pt = self.pt;2473 const zcu = o.zcu;
2505 const zcu = pt.zcu;
2506 const ip = &zcu.intern_pool;2474 const ip = &zcu.intern_pool;
2507 const target = zcu.getTarget();2475 const target = zcu.getTarget();
25082476
...@@ -2855,8 +2823,7 @@ fn airIsNonNull(...@@ -2855,8 +2823,7 @@ fn airIsNonNull(
2855 cond: Builder.IntegerCondition,2823 cond: Builder.IntegerCondition,
2856) Allocator.Error!Builder.Value {2824) Allocator.Error!Builder.Value {
2857 const o = self.object;2825 const o = self.object;
2858 const pt = self.pt;2826 const zcu = o.zcu;
2859 const zcu = pt.zcu;
2860 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;2827 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
2861 const operand = try self.resolveInst(un_op);2828 const operand = try self.resolveInst(un_op);
2862 const operand_ty = self.typeOf(un_op);2829 const operand_ty = self.typeOf(un_op);
...@@ -2905,8 +2872,7 @@ fn airIsErr(...@@ -2905,8 +2872,7 @@ fn airIsErr(
2905 operand_is_ptr: bool,2872 operand_is_ptr: bool,
2906) Allocator.Error!Builder.Value {2873) Allocator.Error!Builder.Value {
2907 const o = self.object;2874 const o = self.object;
2908 const pt = self.pt;2875 const zcu = o.zcu;
2909 const zcu = pt.zcu;
2910 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;2876 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
2911 const operand = try self.resolveInst(un_op);2877 const operand = try self.resolveInst(un_op);
2912 const operand_ty = self.typeOf(un_op);2878 const operand_ty = self.typeOf(un_op);
...@@ -2959,8 +2925,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro...@@ -2959,8 +2925,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro
2959 comptime assert(optional_layout_version == 3);2925 comptime assert(optional_layout_version == 3);
29602926
2961 const o = self.object;2927 const o = self.object;
2962 const pt = self.pt;2928 const zcu = o.zcu;
2963 const zcu = pt.zcu;
2964 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;2929 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
2965 const operand = try self.resolveInst(ty_op.operand);2930 const operand = try self.resolveInst(ty_op.operand);
2966 const optional_ptr_ty = self.typeOf(ty_op.operand);2931 const optional_ptr_ty = self.typeOf(ty_op.operand);
...@@ -3001,8 +2966,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro...@@ -3001,8 +2966,7 @@ fn airOptionalPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Erro
3001}2966}
30022967
3003fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {2968fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3004 const pt = self.pt;2969 const zcu = self.object.zcu;
3005 const zcu = pt.zcu;
3006 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;2970 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3007 const operand = try self.resolveInst(ty_op.operand);2971 const operand = try self.resolveInst(ty_op.operand);
3008 const optional_ty = self.typeOf(ty_op.operand);2972 const optional_ty = self.typeOf(ty_op.operand);
...@@ -3018,8 +2982,7 @@ fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Buil...@@ -3018,8 +2982,7 @@ fn airOptionalPayload(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Buil
3018}2982}
30192983
3020fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) Allocator.Error!Builder.Value {2984fn airErrUnionPayload(self: *FuncGen, inst: Air.Inst.Index, operand_is_ptr: bool) Allocator.Error!Builder.Value {
3021 const pt = self.pt;2985 const zcu = self.object.zcu;
3022 const zcu = pt.zcu;
3023 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;2986 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3024 const operand = try self.resolveInst(ty_op.operand);2987 const operand = try self.resolveInst(ty_op.operand);
3025 const operand_ty = self.typeOf(ty_op.operand);2988 const operand_ty = self.typeOf(ty_op.operand);
...@@ -3050,8 +3013,7 @@ fn airErrUnionErr(...@@ -3050,8 +3013,7 @@ fn airErrUnionErr(
3050 operand_is_ptr: bool,3013 operand_is_ptr: bool,
3051) Allocator.Error!Builder.Value {3014) Allocator.Error!Builder.Value {
3052 const o = self.object;3015 const o = self.object;
3053 const pt = self.pt;3016 const zcu = o.zcu;
3054 const zcu = pt.zcu;
3055 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;3017 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3056 const operand = try self.resolveInst(ty_op.operand);3018 const operand = try self.resolveInst(ty_op.operand);
3057 const operand_ty = self.typeOf(ty_op.operand);3019 const operand_ty = self.typeOf(ty_op.operand);
...@@ -3093,8 +3055,7 @@ fn airErrUnionErr(...@@ -3093,8 +3055,7 @@ fn airErrUnionErr(
30933055
3094fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3056fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3095 const o = self.object;3057 const o = self.object;
3096 const pt = self.pt;3058 const zcu = o.zcu;
3097 const zcu = pt.zcu;
3098 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;3059 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3099 const operand = try self.resolveInst(ty_op.operand);3060 const operand = try self.resolveInst(ty_op.operand);
3100 const err_union_ptr_ty = self.typeOf(ty_op.operand);3061 const err_union_ptr_ty = self.typeOf(ty_op.operand);
...@@ -3133,8 +3094,7 @@ fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bu...@@ -3133,8 +3094,7 @@ fn airSetErrReturnTrace(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bu
3133}3094}
31343095
3135fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3096fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3136 const pt = self.pt;3097 const zcu = self.object.zcu;
3137 const zcu = pt.zcu;
31383098
3139 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;3099 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3140 const struct_ty = ty_pl.ty.toType();3100 const struct_ty = ty_pl.ty.toType();
...@@ -3150,12 +3110,7 @@ fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Er...@@ -3150,12 +3110,7 @@ fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) Allocator.Er
3150 };3110 };
31513111
3152 const field_ptr = try self.ptraddConst(self.err_ret_trace, field_offset);3112 const field_ptr = try self.ptraddConst(self.err_ret_trace, field_offset);
31533113 return self.load(field_ptr, field_ty, field_align.toLlvm(), .normal);
3154 const field_ptr_ty = try pt.ptrType(.{
3155 .child = field_ty.toIntern(),
3156 .flags = .{ .alignment = field_align },
3157 });
3158 return self.load(field_ptr, field_ptr_ty);
3159}3114}
31603115
3161/// As an optimization, we want to avoid unnecessary copies of3116/// As an optimization, we want to avoid unnecessary copies of
...@@ -3183,8 +3138,7 @@ fn isNextRet(...@@ -3183,8 +3138,7 @@ fn isNextRet(
31833138
3184fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {3139fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
3185 const o = self.object;3140 const o = self.object;
3186 const pt = self.pt;3141 const zcu = o.zcu;
3187 const zcu = pt.zcu;
3188 const inst = body_tail[0];3142 const inst = body_tail[0];
3189 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;3143 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3190 const payload_ty = self.typeOf(ty_op.operand);3144 const payload_ty = self.typeOf(ty_op.operand);
...@@ -3205,8 +3159,12 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator....@@ -3205,8 +3159,12 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.
3205 };3159 };
32063160
3207 const payload_ptr = optional_ptr; // payload always at offset 03161 const payload_ptr = optional_ptr; // payload always at offset 0
3208 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);3162 try self.store(
3209 try self.store(payload_ptr, payload_ptr_ty, operand, .none);3163 payload_ptr,
3164 .none,
3165 operand,
3166 payload_ty,
3167 );
3210 // Non-null bit immediately after payload (no padding because the bit has alignment 1).3168 // Non-null bit immediately after payload (no padding because the bit has alignment 1).
3211 const non_null_ptr = try self.ptraddConst(optional_ptr, payload_ty.abiSize(zcu));3169 const non_null_ptr = try self.ptraddConst(optional_ptr, payload_ty.abiSize(zcu));
3212 _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default);3170 _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default);
...@@ -3215,8 +3173,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator....@@ -3215,8 +3173,7 @@ fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.
32153173
3216fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {3174fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
3217 const o = self.object;3175 const o = self.object;
3218 const pt = self.pt;3176 const zcu = o.zcu;
3219 const zcu = pt.zcu;
3220 const inst = body_tail[0];3177 const inst = body_tail[0];
3221 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;3178 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3222 const err_un_ty = self.typeOfIndex(inst);3179 const err_un_ty = self.typeOfIndex(inst);
...@@ -3230,23 +3187,26 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) All...@@ -3230,23 +3187,26 @@ fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) All
3230 const result_ptr = if (self.isNextRet(body_tail))3187 const result_ptr = if (self.isNextRet(body_tail))
3231 self.ret_ptr3188 self.ret_ptr
3232 else brk: {3189 else brk: {
3233 const alignment = err_un_ty.abiAlignment(pt.zcu).toLlvm();3190 const alignment = err_un_ty.abiAlignment(o.zcu).toLlvm();
3234 const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment);3191 const result_ptr = try self.buildAlloca(err_un_llvm_ty, alignment);
3235 break :brk result_ptr;3192 break :brk result_ptr;
3236 };3193 };
32373194
3238 const err_ptr = try self.ptraddConst(result_ptr, codegen.errUnionErrorOffset(payload_ty, zcu));3195 const err_ptr = try self.ptraddConst(result_ptr, codegen.errUnionErrorOffset(payload_ty, zcu));
3239 const error_alignment = Type.anyerror.abiAlignment(pt.zcu).toLlvm();3196 const error_alignment = Type.anyerror.abiAlignment(o.zcu).toLlvm();
3240 _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment);3197 _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment);
3241 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));3198 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));
3242 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);3199 try self.store(
3243 try self.store(payload_ptr, payload_ptr_ty, operand, .none);3200 payload_ptr,
3201 .none,
3202 operand,
3203 payload_ty,
3204 );
3244 return result_ptr;3205 return result_ptr;
3245}3206}
32463207
3247fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {3208fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocator.Error!Builder.Value {
3248 const pt = self.pt;3209 const zcu = self.object.zcu;
3249 const zcu = pt.zcu;
3250 const inst = body_tail[0];3210 const inst = body_tail[0];
3251 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;3211 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
3252 const err_un_ty = self.typeOfIndex(inst);3212 const err_un_ty = self.typeOfIndex(inst);
...@@ -3268,10 +3228,8 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocat...@@ -3268,10 +3228,8 @@ fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) Allocat
3268 const error_alignment = Type.anyerror.abiAlignment(zcu).toLlvm();3228 const error_alignment = Type.anyerror.abiAlignment(zcu).toLlvm();
3269 _ = try self.wip.store(.normal, operand, err_ptr, error_alignment);3229 _ = try self.wip.store(.normal, operand, err_ptr, error_alignment);
3270 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));3230 const payload_ptr = try self.ptraddConst(result_ptr, codegen.errUnionPayloadOffset(payload_ty, zcu));
3271 const payload_ptr_ty = try pt.singleMutPtrType(payload_ty);
3272 // TODO store undef to payload_ptr3231 // TODO store undef to payload_ptr
3273 _ = payload_ptr;3232 _ = payload_ptr;
3274 _ = payload_ptr_ty;
3275 return result_ptr;3233 return result_ptr;
3276}3234}
32773235
...@@ -3279,7 +3237,7 @@ fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build...@@ -3279,7 +3237,7 @@ fn airWasmMemorySize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
3279 const o = self.object;3237 const o = self.object;
3280 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;3238 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
3281 const index = pl_op.payload;3239 const index = pl_op.payload;
3282 const llvm_usize = try self.lowerType(Type.usize);3240 const llvm_usize = try self.lowerType(.usize);
3283 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{3241 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.size", &.{llvm_usize}, &.{
3284 try o.builder.intValue(.i32, index),3242 try o.builder.intValue(.i32, index),
3285 }, "");3243 }, "");
...@@ -3289,7 +3247,7 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build...@@ -3289,7 +3247,7 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
3289 const o = self.object;3247 const o = self.object;
3290 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;3248 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
3291 const index = pl_op.payload;3249 const index = pl_op.payload;
3292 const llvm_isize = try self.lowerType(Type.isize);3250 const llvm_isize = try self.lowerType(.isize);
3293 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{3251 return self.wip.callIntrinsic(.normal, .none, .@"wasm.memory.grow", &.{llvm_isize}, &.{
3294 try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),3252 try o.builder.intValue(.i32, index), try self.resolveInst(pl_op.operand),
3295 }, "");3253 }, "");
...@@ -3297,15 +3255,13 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build...@@ -3297,15 +3255,13 @@ fn airWasmMemoryGrow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Build
32973255
3298fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3256fn airRuntimeNavPtr(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3299 const o = fg.object;3257 const o = fg.object;
3300 const pt = fg.pt;
3301 const ty_nav = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;3258 const ty_nav = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav;
3302 const llvm_ptr_const = try o.lowerNavRefValue(pt, ty_nav.nav);3259 const llvm_ptr_const = try o.lowerNavRefValue(ty_nav.nav);
3303 return llvm_ptr_const.toValue();3260 return llvm_ptr_const.toValue();
3304}3261}
33053262
3306fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3263fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3307 const pt = self.pt;3264 const zcu = self.object.zcu;
3308 const zcu = pt.zcu;
3309 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3265 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3310 const lhs = try self.resolveInst(bin_op.lhs);3266 const lhs = try self.resolveInst(bin_op.lhs);
3311 const rhs = try self.resolveInst(bin_op.rhs);3267 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3324,8 +3280,7 @@ fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {...@@ -3324,8 +3280,7 @@ fn airMin(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3324}3280}
33253281
3326fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3282fn airMax(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3327 const pt = self.pt;3283 const zcu = self.object.zcu;
3328 const zcu = pt.zcu;
3329 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3284 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3330 const lhs = try self.resolveInst(bin_op.lhs);3285 const lhs = try self.resolveInst(bin_op.lhs);
3331 const rhs = try self.resolveInst(bin_op.rhs);3286 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3353,7 +3308,7 @@ fn airSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -3353,7 +3308,7 @@ fn airSlice(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
3353}3308}
33543309
3355fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3310fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3356 const zcu = self.pt.zcu;3311 const zcu = self.object.zcu;
3357 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3312 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3358 const lhs = try self.resolveInst(bin_op.lhs);3313 const lhs = try self.resolveInst(bin_op.lhs);
3359 const rhs = try self.resolveInst(bin_op.rhs);3314 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3371,8 +3326,7 @@ fn airSafeArithmetic(...@@ -3371,8 +3326,7 @@ fn airSafeArithmetic(
3371 unsigned_intrinsic: Builder.Intrinsic,3326 unsigned_intrinsic: Builder.Intrinsic,
3372) Allocator.Error!Builder.Value {3327) Allocator.Error!Builder.Value {
3373 const o = fg.object;3328 const o = fg.object;
3374 const pt = fg.pt;3329 const zcu = o.zcu;
3375 const zcu = pt.zcu;
33763330
3377 const bin_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3331 const bin_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3378 const lhs = try fg.resolveInst(bin_op.lhs);3332 const lhs = try fg.resolveInst(bin_op.lhs);
...@@ -3419,8 +3373,7 @@ fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -3419,8 +3373,7 @@ fn airAddWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
3419}3373}
34203374
3421fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3375fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3422 const pt = self.pt;3376 const zcu = self.object.zcu;
3423 const zcu = pt.zcu;
3424 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3377 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3425 const lhs = try self.resolveInst(bin_op.lhs);3378 const lhs = try self.resolveInst(bin_op.lhs);
3426 const rhs = try self.resolveInst(bin_op.rhs);3379 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3438,7 +3391,7 @@ fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -3438,7 +3391,7 @@ fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
3438}3391}
34393392
3440fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3393fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3441 const zcu = self.pt.zcu;3394 const zcu = self.object.zcu;
3442 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3395 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3443 const lhs = try self.resolveInst(bin_op.lhs);3396 const lhs = try self.resolveInst(bin_op.lhs);
3444 const rhs = try self.resolveInst(bin_op.rhs);3397 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3458,8 +3411,7 @@ fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -3458,8 +3411,7 @@ fn airSubWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
3458}3411}
34593412
3460fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3413fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3461 const pt = self.pt;3414 const zcu = self.object.zcu;
3462 const zcu = pt.zcu;
3463 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3415 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3464 const lhs = try self.resolveInst(bin_op.lhs);3416 const lhs = try self.resolveInst(bin_op.lhs);
3465 const rhs = try self.resolveInst(bin_op.rhs);3417 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3477,7 +3429,7 @@ fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -3477,7 +3429,7 @@ fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
3477}3429}
34783430
3479fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3431fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3480 const zcu = self.pt.zcu;3432 const zcu = self.object.zcu;
3481 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3433 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3482 const lhs = try self.resolveInst(bin_op.lhs);3434 const lhs = try self.resolveInst(bin_op.lhs);
3483 const rhs = try self.resolveInst(bin_op.rhs);3435 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3497,8 +3449,7 @@ fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -3497,8 +3449,7 @@ fn airMulWrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
3497}3449}
34983450
3499fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3451fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3500 const pt = self.pt;3452 const zcu = self.object.zcu;
3501 const zcu = pt.zcu;
3502 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3453 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3503 const lhs = try self.resolveInst(bin_op.lhs);3454 const lhs = try self.resolveInst(bin_op.lhs);
3504 const rhs = try self.resolveInst(bin_op.rhs);3455 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3525,7 +3476,7 @@ fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)...@@ -3525,7 +3476,7 @@ fn airDivFloat(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
3525}3476}
35263477
3527fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3478fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3528 const zcu = self.pt.zcu;3479 const zcu = self.object.zcu;
3529 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3480 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3530 const lhs = try self.resolveInst(bin_op.lhs);3481 const lhs = try self.resolveInst(bin_op.lhs);
3531 const rhs = try self.resolveInst(bin_op.rhs);3482 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3541,8 +3492,7 @@ fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)...@@ -3541,8 +3492,7 @@ fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
35413492
3542fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3493fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3543 const o = self.object;3494 const o = self.object;
3544 const pt = self.pt;3495 const zcu = o.zcu;
3545 const zcu = pt.zcu;
3546 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3496 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3547 const lhs = try self.resolveInst(bin_op.lhs);3497 const lhs = try self.resolveInst(bin_op.lhs);
3548 const rhs = try self.resolveInst(bin_op.rhs);3498 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3591,7 +3541,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)...@@ -3591,7 +3541,7 @@ fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
3591}3541}
35923542
3593fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3543fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3594 const zcu = self.pt.zcu;3544 const zcu = self.object.zcu;
3595 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3545 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3596 const lhs = try self.resolveInst(bin_op.lhs);3546 const lhs = try self.resolveInst(bin_op.lhs);
3597 const rhs = try self.resolveInst(bin_op.rhs);3547 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3608,7 +3558,7 @@ fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)...@@ -3608,7 +3558,7 @@ fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind)
3608}3558}
36093559
3610fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3560fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3611 const zcu = self.pt.zcu;3561 const zcu = self.object.zcu;
3612 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3562 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3613 const lhs = try self.resolveInst(bin_op.lhs);3563 const lhs = try self.resolveInst(bin_op.lhs);
3614 const rhs = try self.resolveInst(bin_op.rhs);3564 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3625,8 +3575,7 @@ fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo...@@ -3625,8 +3575,7 @@ fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
36253575
3626fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {3576fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
3627 const o = self.object;3577 const o = self.object;
3628 const pt = self.pt;3578 const zcu = o.zcu;
3629 const zcu = pt.zcu;
3630 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;3579 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
3631 const lhs = try self.resolveInst(bin_op.lhs);3580 const lhs = try self.resolveInst(bin_op.lhs);
3632 const rhs = try self.resolveInst(bin_op.rhs);3581 const rhs = try self.resolveInst(bin_op.rhs);
...@@ -3678,7 +3627,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo...@@ -3678,7 +3627,7 @@ fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allo
3678}3627}
36793628
3680fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3629fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3681 const zcu = self.pt.zcu;3630 const zcu = self.object.zcu;
3682 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;3631 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3683 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;3632 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3684 const ptr_or_slice = try self.resolveInst(bin_op.lhs);3633 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
...@@ -3694,8 +3643,7 @@ fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -3694,8 +3643,7 @@ fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
36943643
3695fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3644fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
3696 const o = self.object;3645 const o = self.object;
3697 const pt = self.pt;3646 const zcu = o.zcu;
3698 const zcu = pt.zcu;
3699 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;3647 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3700 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;3648 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
3701 const ptr_or_slice = try self.resolveInst(bin_op.lhs);3649 const ptr_or_slice = try self.resolveInst(bin_op.lhs);
...@@ -3718,8 +3666,7 @@ fn airOverflow(...@@ -3718,8 +3666,7 @@ fn airOverflow(
3718 signed_intrinsic: Builder.Intrinsic,3666 signed_intrinsic: Builder.Intrinsic,
3719 unsigned_intrinsic: Builder.Intrinsic,3667 unsigned_intrinsic: Builder.Intrinsic,
3720) Allocator.Error!Builder.Value {3668) Allocator.Error!Builder.Value {
3721 const pt = self.pt;3669 const zcu = self.object.zcu;
3722 const zcu = pt.zcu;
3723 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;3670 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3724 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;3671 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
37253672
...@@ -3801,8 +3748,7 @@ fn buildFloatCmp(...@@ -3801,8 +3748,7 @@ fn buildFloatCmp(
3801 params: [2]Builder.Value,3748 params: [2]Builder.Value,
3802) Allocator.Error!Builder.Value {3749) Allocator.Error!Builder.Value {
3803 const o = self.object;3750 const o = self.object;
3804 const pt = self.pt;3751 const zcu = o.zcu;
3805 const zcu = pt.zcu;
3806 const target = zcu.getTarget();3752 const target = zcu.getTarget();
3807 const scalar_ty = ty.scalarType(zcu);3753 const scalar_ty = ty.scalarType(zcu);
3808 const scalar_llvm_ty = try self.lowerType(scalar_ty);3754 const scalar_llvm_ty = try self.lowerType(scalar_ty);
...@@ -3908,8 +3854,7 @@ fn buildFloatOp(...@@ -3908,8 +3854,7 @@ fn buildFloatOp(
3908 params: [params_len]Builder.Value,3854 params: [params_len]Builder.Value,
3909) Allocator.Error!Builder.Value {3855) Allocator.Error!Builder.Value {
3910 const o = self.object;3856 const o = self.object;
3911 const pt = self.pt;3857 const zcu = o.zcu;
3912 const zcu = pt.zcu;
3913 const target = zcu.getTarget();3858 const target = zcu.getTarget();
3914 const scalar_ty = ty.scalarType(zcu);3859 const scalar_ty = ty.scalarType(zcu);
3915 const llvm_ty = try self.lowerType(ty);3860 const llvm_ty = try self.lowerType(ty);
...@@ -4049,8 +3994,7 @@ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -4049,8 +3994,7 @@ fn airMulAdd(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
4049}3994}
40503995
4051fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {3996fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4052 const pt = self.pt;3997 const zcu = self.object.zcu;
4053 const zcu = pt.zcu;
4054 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;3998 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4055 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;3999 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
40564000
...@@ -4120,8 +4064,7 @@ fn airXor(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {...@@ -4120,8 +4064,7 @@ fn airXor(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4120}4064}
41214065
4122fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4066fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4123 const pt = self.pt;4067 const zcu = self.object.zcu;
4124 const zcu = pt.zcu;
4125 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;4068 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41264069
4127 const lhs = try self.resolveInst(bin_op.lhs);4070 const lhs = try self.resolveInst(bin_op.lhs);
...@@ -4143,8 +4086,7 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val...@@ -4143,8 +4086,7 @@ fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
4143}4086}
41444087
4145fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4088fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4146 const pt = self.pt;4089 const zcu = self.object.zcu;
4147 const zcu = pt.zcu;
4148 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;4090 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41494091
4150 const lhs = try self.resolveInst(bin_op.lhs);4092 const lhs = try self.resolveInst(bin_op.lhs);
...@@ -4162,8 +4104,7 @@ fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {...@@ -4162,8 +4104,7 @@ fn airShl(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
41624104
4163fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4105fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4164 const o = self.object;4106 const o = self.object;
4165 const pt = self.pt;4107 const zcu = o.zcu;
4166 const zcu = pt.zcu;
4167 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;4108 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
41684109
4169 const lhs = try self.resolveInst(bin_op.lhs);4110 const lhs = try self.resolveInst(bin_op.lhs);
...@@ -4244,8 +4185,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -4244,8 +4185,7 @@ fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
4244}4185}
42454186
4246fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!Builder.Value {4187fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!Builder.Value {
4247 const pt = self.pt;4188 const zcu = self.object.zcu;
4248 const zcu = pt.zcu;
4249 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;4189 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
42504190
4251 const lhs = try self.resolveInst(bin_op.lhs);4191 const lhs = try self.resolveInst(bin_op.lhs);
...@@ -4269,8 +4209,7 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!...@@ -4269,8 +4209,7 @@ fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) Allocator.Error!
42694209
4270fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4210fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4271 const o = self.object;4211 const o = self.object;
4272 const pt = self.pt;4212 const zcu = o.zcu;
4273 const zcu = pt.zcu;
4274 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4213 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
4275 const operand = try self.resolveInst(ty_op.operand);4214 const operand = try self.resolveInst(ty_op.operand);
4276 const operand_ty = self.typeOf(ty_op.operand);4215 const operand_ty = self.typeOf(ty_op.operand);
...@@ -4292,8 +4231,7 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {...@@ -4292,8 +4231,7 @@ fn airAbs(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
42924231
4293fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {4232fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
4294 const o = fg.object;4233 const o = fg.object;
4295 const pt = fg.pt;4234 const zcu = o.zcu;
4296 const zcu = pt.zcu;
4297 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4235 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
4298 const dest_ty = fg.typeOfIndex(inst);4236 const dest_ty = fg.typeOfIndex(inst);
4299 const dest_llvm_ty = try fg.lowerType(dest_ty);4237 const dest_llvm_ty = try fg.lowerType(dest_ty);
...@@ -4379,7 +4317,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!...@@ -4379,7 +4317,7 @@ fn airIntCast(fg: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
4379 }, operand, dest_llvm_ty, "");4317 }, operand, dest_llvm_ty, "");
43804318
4381 if (safety and dest_is_enum and !dest_ty.isNonexhaustiveEnum(zcu)) {4319 if (safety and dest_is_enum and !dest_ty.isNonexhaustiveEnum(zcu)) {
4382 const llvm_fn = try o.getIsNamedEnumValueFunction(pt, dest_ty);4320 const llvm_fn = try o.getIsNamedEnumValueFunction(dest_ty);
4383 const is_valid_enum_val = try fg.wip.call(4321 const is_valid_enum_val = try fg.wip.call(
4384 .normal,4322 .normal,
4385 .fastcc,4323 .fastcc,
...@@ -4409,8 +4347,7 @@ fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -4409,8 +4347,7 @@ fn airTrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
44094347
4410fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4348fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4411 const o = self.object;4349 const o = self.object;
4412 const pt = self.pt;4350 const zcu = o.zcu;
4413 const zcu = pt.zcu;
4414 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4351 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
4415 const operand = try self.resolveInst(ty_op.operand);4352 const operand = try self.resolveInst(ty_op.operand);
4416 const operand_ty = self.typeOf(ty_op.operand);4353 const operand_ty = self.typeOf(ty_op.operand);
...@@ -4444,8 +4381,7 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -4444,8 +4381,7 @@ fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
44444381
4445fn airFpext(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4382fn airFpext(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4446 const o = self.object;4383 const o = self.object;
4447 const pt = self.pt;4384 const zcu = o.zcu;
4448 const zcu = pt.zcu;
4449 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4385 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
4450 const operand = try self.resolveInst(ty_op.operand);4386 const operand = try self.resolveInst(ty_op.operand);
4451 const operand_ty = self.typeOf(ty_op.operand);4387 const operand_ty = self.typeOf(ty_op.operand);
...@@ -4493,8 +4429,7 @@ fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -4493,8 +4429,7 @@ fn airBitCast(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
44934429
4494fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) Allocator.Error!Builder.Value {4430fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) Allocator.Error!Builder.Value {
4495 const o = self.object;4431 const o = self.object;
4496 const pt = self.pt;4432 const zcu = o.zcu;
4497 const zcu = pt.zcu;
4498 const operand_is_ref = isByRef(operand_ty, zcu);4433 const operand_is_ref = isByRef(operand_ty, zcu);
4499 const result_is_ref = isByRef(inst_ty, zcu);4434 const result_is_ref = isByRef(inst_ty, zcu);
4500 const llvm_dest_ty = try self.lowerType(inst_ty);4435 const llvm_dest_ty = try self.lowerType(inst_ty);
...@@ -4599,7 +4534,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty...@@ -4599,7 +4534,7 @@ fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Ty
4599fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4534fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4600 const o = self.object;4535 const o = self.object;
4601 const pt = self.pt;4536 const pt = self.pt;
4602 const zcu = pt.zcu;4537 const zcu = o.zcu;
4603 const arg_val = self.args[self.arg_index];4538 const arg_val = self.args[self.arg_index];
4604 self.arg_index += 1;4539 self.arg_index += 1;
46054540
...@@ -4688,13 +4623,13 @@ fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {...@@ -4688,13 +4623,13 @@ fn airArg(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
46884623
4689fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4624fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4690 const o = self.object;4625 const o = self.object;
4691 const pt = self.pt;4626 const zcu = o.zcu;
4692 const zcu = pt.zcu;
4693 const ptr_ty = self.typeOfIndex(inst);4627 const ptr_ty = self.typeOfIndex(inst);
4694 const pointee_type = ptr_ty.childType(zcu);4628 const pointee_type = ptr_ty.childType(zcu);
4695 if (!pointee_type.hasRuntimeBits(zcu))4629 if (!pointee_type.hasRuntimeBits(zcu)) {
4696 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();4630 const ptr_info = ptr_ty.ptrInfo(zcu);
46974631 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
4632 }
4698 const pointee_llvm_ty = try self.lowerType(pointee_type);4633 const pointee_llvm_ty = try self.lowerType(pointee_type);
4699 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();4634 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
4700 return self.buildAlloca(pointee_llvm_ty, alignment);4635 return self.buildAlloca(pointee_llvm_ty, alignment);
...@@ -4702,12 +4637,13 @@ fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -4702,12 +4637,13 @@ fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
47024637
4703fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4638fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4704 const o = self.object;4639 const o = self.object;
4705 const pt = self.pt;4640 const zcu = o.zcu;
4706 const zcu = pt.zcu;
4707 const ptr_ty = self.typeOfIndex(inst);4641 const ptr_ty = self.typeOfIndex(inst);
4708 const ret_ty = ptr_ty.childType(zcu);4642 const ret_ty = ptr_ty.childType(zcu);
4709 if (!ret_ty.hasRuntimeBits(zcu))4643 if (!ret_ty.hasRuntimeBits(zcu)) {
4710 return (try o.lowerPtrToVoid(pt, ptr_ty)).toValue();4644 const ptr_info = ptr_ty.ptrInfo(zcu);
4645 return (try o.lowerPtrToVoid(ptr_info.flags.alignment, ptr_info.flags.address_space)).toValue();
4646 }
4711 if (self.ret_ptr != .none) return self.ret_ptr;4647 if (self.ret_ptr != .none) return self.ret_ptr;
4712 const ret_llvm_ty = try self.lowerType(ret_ty);4648 const ret_llvm_ty = try self.lowerType(ret_ty);
4713 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();4649 const alignment = ptr_ty.ptrAlignment(zcu).toLlvm();
...@@ -4721,20 +4657,19 @@ fn buildAlloca(...@@ -4721,20 +4657,19 @@ fn buildAlloca(
4721 llvm_ty: Builder.Type,4657 llvm_ty: Builder.Type,
4722 alignment: Builder.Alignment,4658 alignment: Builder.Alignment,
4723) Allocator.Error!Builder.Value {4659) Allocator.Error!Builder.Value {
4724 const target = self.pt.zcu.getTarget();4660 const target = self.object.zcu.getTarget();
4725 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);4661 return buildAllocaInner(&self.wip, llvm_ty, alignment, target);
4726}4662}
47274663
4728fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {4664fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
4729 const o = self.object;4665 const o = self.object;
4730 const pt = self.pt;4666 const zcu = o.zcu;
4731 const zcu = pt.zcu;
4732 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;4667 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
4733 const dest_ptr = try self.resolveInst(bin_op.lhs);4668 const dest_ptr = try self.resolveInst(bin_op.lhs);
4734 const ptr_ty = self.typeOf(bin_op.lhs);4669 const ptr_ty = self.typeOf(bin_op.lhs);
4735 const operand_ty = ptr_ty.childType(zcu);4670 const operand_ty = ptr_ty.childType(zcu);
47364671
4737 const val_is_undef = if (try self.air.value(bin_op.rhs, pt)) |val| val.isUndef(zcu) else false;4672 const val_is_undef = if (bin_op.rhs.toInterned()) |i| Value.fromInterned(i).isUndef(zcu) else false;
4738 if (val_is_undef) {4673 if (val_is_undef) {
4739 const owner_mod = self.ownerModule();4674 const owner_mod = self.ownerModule();
47404675
...@@ -4762,7 +4697,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!...@@ -4762,7 +4697,7 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
47624697
4763 self.maybeMarkAllowZeroAccess(ptr_info);4698 self.maybeMarkAllowZeroAccess(ptr_info);
47644699
4765 const len = try o.builder.intValue(try self.lowerType(Type.usize), operand_ty.abiSize(zcu));4700 const len = try o.builder.intValue(try self.lowerType(.usize), operand_ty.abiSize(zcu));
4766 _ = try self.wip.callMemSet(4701 _ = try self.wip.callMemSet(
4767 dest_ptr,4702 dest_ptr,
4768 ptr_ty.ptrAlignment(zcu).toLlvm(),4703 ptr_ty.ptrAlignment(zcu).toLlvm(),
...@@ -4780,24 +4715,75 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!...@@ -4780,24 +4715,75 @@ fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!
4780 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));4715 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
47814716
4782 const src_operand = try self.resolveInst(bin_op.rhs);4717 const src_operand = try self.resolveInst(bin_op.rhs);
4783 try self.store(dest_ptr, ptr_ty, src_operand, .none);4718 try self.storeFull(dest_ptr, ptr_ty, src_operand, .none);
4784 return .none;4719 return .none;
4785}4720}
47864721
4787fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4722fn airLoad(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4788 const pt = fg.pt;4723 const o = fg.object;
4789 const zcu = pt.zcu;4724 const zcu = o.zcu;
4790 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;4725 const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
4791 const ptr_ty = fg.typeOf(ty_op.operand);4726 const ptr_ty = fg.typeOf(ty_op.operand);
4792 const ptr_info = ptr_ty.ptrInfo(zcu);4727 const ptr_info = ptr_ty.ptrInfo(zcu);
4793 const ptr = try fg.resolveInst(ty_op.operand);4728 const ptr = try fg.resolveInst(ty_op.operand);
4729 const elem_ty = ptr_ty.childType(zcu);
4730 const llvm_ptr_align = ptr_ty.ptrAlignment(zcu).toLlvm();
4731
4794 fg.maybeMarkAllowZeroAccess(ptr_info);4732 fg.maybeMarkAllowZeroAccess(ptr_info);
4795 return fg.load(ptr, ptr_ty);4733
4734 const access_kind: Builder.MemoryAccessKind =
4735 if (ptr_info.flags.is_volatile) .@"volatile" else .normal;
4736
4737 if (ptr_info.flags.vector_index != .none) {
4738 const index_u32 = try o.builder.intValue(.i32, ptr_info.flags.vector_index);
4739 const vec_elem_ty = try fg.lowerType(elem_ty);
4740 const vec_ty = try o.builder.vectorType(.normal, ptr_info.packed_offset.host_size, vec_elem_ty);
4741
4742 const loaded_vector = try fg.wip.load(access_kind, vec_ty, ptr, llvm_ptr_align, "");
4743 return fg.wip.extractElement(loaded_vector, index_u32, "");
4744 }
4745
4746 if (ptr_info.packed_offset.host_size == 0) {
4747 return fg.load(ptr, elem_ty, llvm_ptr_align, access_kind);
4748 }
4749
4750 const containing_int_ty = try o.builder.intType(@intCast(ptr_info.packed_offset.host_size * 8));
4751 const containing_int =
4752 try fg.wip.load(access_kind, containing_int_ty, ptr, llvm_ptr_align, "");
4753
4754 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
4755 const shift_amt = try o.builder.intValue(containing_int_ty, ptr_info.packed_offset.bit_offset);
4756 const shifted_value = try fg.wip.bin(.lshr, containing_int, shift_amt, "");
4757 const elem_llvm_ty = try fg.lowerType(elem_ty);
4758
4759 if (isByRef(elem_ty, zcu)) {
4760 const result_align = elem_ty.abiAlignment(zcu).toLlvm();
4761 const result_ptr = try fg.buildAlloca(elem_llvm_ty, result_align);
4762
4763 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4764 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4765 _ = try fg.wip.store(.normal, truncated_int, result_ptr, result_align);
4766 return result_ptr;
4767 }
4768
4769 if (elem_ty.zigTypeTag(zcu) == .float or elem_ty.zigTypeTag(zcu) == .vector) {
4770 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4771 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4772 return fg.wip.cast(.bitcast, truncated_int, elem_llvm_ty, "");
4773 }
4774
4775 if (elem_ty.isPtrAtRuntime(zcu)) {
4776 const same_size_int = try o.builder.intType(@intCast(elem_bits));
4777 const truncated_int = try fg.wip.cast(.trunc, shifted_value, same_size_int, "");
4778 return fg.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, "");
4779 }
4780
4781 return fg.wip.cast(.trunc, shifted_value, elem_llvm_ty, "");
4796}4782}
47974783
4798fn airTrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {4784fn airTrap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!void {
4799 _ = inst;4785 _ = inst;
4800 const target = self.pt.zcu.getTarget();4786 const target = self.object.zcu.getTarget();
4801 if ((target.cpu.arch == .mips or target.cpu.arch == .mipsel) and4787 if ((target.cpu.arch == .mips or target.cpu.arch == .mipsel) and
4802 target.cpu.has(.mips, .notraps))4788 target.cpu.has(.mips, .notraps))
4803 {4789 {
...@@ -4828,8 +4814,8 @@ fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V...@@ -4828,8 +4814,8 @@ fn airBreakpoint(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
4828fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4814fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4829 _ = inst;4815 _ = inst;
4830 const o = self.object;4816 const o = self.object;
4831 const llvm_usize = try self.lowerType(Type.usize);4817 const llvm_usize = try self.lowerType(.usize);
4832 if (!target_util.supportsReturnAddress(self.pt.zcu.getTarget(), self.ownerModule().optimize_mode)) {4818 if (!target_util.supportsReturnAddress(self.object.zcu.getTarget(), self.ownerModule().optimize_mode)) {
4833 // https://github.com/ziglang/zig/issues/119464819 // https://github.com/ziglang/zig/issues/11946
4834 return o.builder.intValue(llvm_usize, 0);4820 return o.builder.intValue(llvm_usize, 0);
4835 }4821 }
...@@ -4840,7 +4826,7 @@ fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -4840,7 +4826,7 @@ fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
4840fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4826fn airFrameAddress(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4841 _ = inst;4827 _ = inst;
4842 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");4828 const result = try self.wip.callIntrinsic(.normal, .none, .frameaddress, &.{.ptr}, &.{.@"0"}, "");
4843 return self.wip.cast(.ptrtoint, result, try self.lowerType(Type.usize), "");4829 return self.wip.cast(.ptrtoint, result, try self.lowerType(.usize), "");
4844}4830}
48454831
4846fn airCmpxchg(4832fn airCmpxchg(
...@@ -4849,8 +4835,7 @@ fn airCmpxchg(...@@ -4849,8 +4835,7 @@ fn airCmpxchg(
4849 kind: Builder.Function.Instruction.CmpXchg.Kind,4835 kind: Builder.Function.Instruction.CmpXchg.Kind,
4850) Allocator.Error!Builder.Value {4836) Allocator.Error!Builder.Value {
4851 const o = self.object;4837 const o = self.object;
4852 const pt = self.pt;4838 const zcu = o.zcu;
4853 const zcu = pt.zcu;
4854 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;4839 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
4855 const extra = self.air.extraData(Air.Cmpxchg, ty_pl.payload).data;4840 const extra = self.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
4856 const ptr = try self.resolveInst(extra.ptr);4841 const ptr = try self.resolveInst(extra.ptr);
...@@ -4915,8 +4900,7 @@ fn airCmpxchg(...@@ -4915,8 +4900,7 @@ fn airCmpxchg(
49154900
4916fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4901fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4917 const o = self.object;4902 const o = self.object;
4918 const pt = self.pt;4903 const zcu = o.zcu;
4919 const zcu = pt.zcu;
4920 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;4904 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
4921 const extra = self.air.extraData(Air.AtomicRmw, pl_op.payload).data;4905 const extra = self.air.extraData(Air.AtomicRmw, pl_op.payload).data;
4922 const ptr = try self.resolveInst(pl_op.operand);4906 const ptr = try self.resolveInst(pl_op.operand);
...@@ -4971,7 +4955,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va...@@ -4971,7 +4955,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
4971 access_kind,4955 access_kind,
4972 op,4956 op,
4973 ptr,4957 ptr,
4974 try self.wip.cast(.ptrtoint, operand, try self.lowerType(Type.usize), ""),4958 try self.wip.cast(.ptrtoint, operand, try self.lowerType(.usize), ""),
4975 self.sync_scope,4959 self.sync_scope,
4976 ordering,4960 ordering,
4977 ptr_alignment,4961 ptr_alignment,
...@@ -4980,8 +4964,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va...@@ -4980,8 +4964,7 @@ fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
4980}4964}
49814965
4982fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {4966fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
4983 const pt = self.pt;4967 const zcu = self.object.zcu;
4984 const zcu = pt.zcu;
4985 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;4968 const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
4986 const ptr = try self.resolveInst(atomic_load.ptr);4969 const ptr = try self.resolveInst(atomic_load.ptr);
4987 const ptr_ty = self.typeOf(atomic_load.ptr);4970 const ptr_ty = self.typeOf(atomic_load.ptr);
...@@ -5029,8 +5012,7 @@ fn airAtomicStore(...@@ -5029,8 +5012,7 @@ fn airAtomicStore(
5029 inst: Air.Inst.Index,5012 inst: Air.Inst.Index,
5030 ordering: Builder.AtomicOrdering,5013 ordering: Builder.AtomicOrdering,
5031) Allocator.Error!Builder.Value {5014) Allocator.Error!Builder.Value {
5032 const pt = self.pt;5015 const zcu = self.object.zcu;
5033 const zcu = pt.zcu;
5034 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;5016 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5035 const ptr_ty = self.typeOf(bin_op.lhs);5017 const ptr_ty = self.typeOf(bin_op.lhs);
5036 const operand_ty = ptr_ty.childType(zcu);5018 const operand_ty = ptr_ty.childType(zcu);
...@@ -5051,14 +5033,13 @@ fn airAtomicStore(...@@ -5051,14 +5033,13 @@ fn airAtomicStore(
50515033
5052 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));5034 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
50535035
5054 try self.store(ptr, ptr_ty, element, ordering);5036 try self.storeFull(ptr, ptr_ty, element, ordering);
5055 return .none;5037 return .none;
5056}5038}
50575039
5058fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {5040fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error!Builder.Value {
5059 const o = self.object;5041 const o = self.object;
5060 const pt = self.pt;5042 const zcu = o.zcu;
5061 const zcu = pt.zcu;
5062 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;5043 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5063 const dest_slice = try self.resolveInst(bin_op.lhs);5044 const dest_slice = try self.resolveInst(bin_op.lhs);
5064 const ptr_ty = self.typeOf(bin_op.lhs);5045 const ptr_ty = self.typeOf(bin_op.lhs);
...@@ -5070,7 +5051,8 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error...@@ -5070,7 +5051,8 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
50705051
5071 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));5052 self.maybeMarkAllowZeroAccess(ptr_ty.ptrInfo(zcu));
50725053
5073 if (try self.air.value(bin_op.rhs, pt)) |elem_val| {5054 if (bin_op.rhs.toInterned()) |elem_ip_index| {
5055 const elem_val: Value = .fromInterned(elem_ip_index);
5074 if (elem_val.isUndef(zcu)) {5056 if (elem_val.isUndef(zcu)) {
5075 // Even if safety is disabled, we still emit a memset to undefined since it conveys5057 // Even if safety is disabled, we still emit a memset to undefined since it conveys
5076 // extra information to LLVM. However, safety makes the difference between using5058 // extra information to LLVM. However, safety makes the difference between using
...@@ -5099,7 +5081,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error...@@ -5099,7 +5081,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
5099 // repeating byte pattern, for example, `@as(u64, 0)` has a5081 // repeating byte pattern, for example, `@as(u64, 0)` has a
5100 // repeating byte pattern of 0 bytes. In such case, the memset5082 // repeating byte pattern of 0 bytes. In such case, the memset
5101 // intrinsic can be used.5083 // intrinsic can be used.
5102 if (try elem_val.hasRepeatedByteRepr(pt)) |byte_val| {5084 if (try elem_val.hasRepeatedByteRepr(zcu)) |byte_val| {
5103 const fill_byte = try o.builder.intValue(.i8, byte_val);5085 const fill_byte = try o.builder.intValue(.i8, byte_val);
5104 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);5086 const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty);
5105 _ = try self.wip.callMemSet(5087 _ = try self.wip.callMemSet(
...@@ -5154,7 +5136,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error...@@ -5154,7 +5136,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
5154 const body_block = try self.wip.block(1, "InlineMemsetBody");5136 const body_block = try self.wip.block(1, "InlineMemsetBody");
5155 const end_block = try self.wip.block(1, "InlineMemsetEnd");5137 const end_block = try self.wip.block(1, "InlineMemsetEnd");
51565138
5157 const llvm_usize_ty = try self.lowerType(Type.usize);5139 const llvm_usize_ty = try self.lowerType(.usize);
5158 const end_ptr = switch (ptr_ty.ptrSize(zcu)) {5140 const end_ptr = switch (ptr_ty.ptrSize(zcu)) {
5159 .slice => try self.ptraddScaled(5141 .slice => try self.ptraddScaled(
5160 dest_ptr,5142 dest_ptr,
...@@ -5194,8 +5176,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error...@@ -5194,8 +5176,7 @@ fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) Allocator.Error
5194}5176}
51955177
5196fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5178fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5197 const pt = self.pt;5179 const zcu = self.object.zcu;
5198 const zcu = pt.zcu;
5199 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;5180 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5200 const dest_slice = try self.resolveInst(bin_op.lhs);5181 const dest_slice = try self.resolveInst(bin_op.lhs);
5201 const dest_ptr_ty = self.typeOf(bin_op.lhs);5182 const dest_ptr_ty = self.typeOf(bin_op.lhs);
...@@ -5223,8 +5204,7 @@ fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -5223,8 +5204,7 @@ fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
5223}5204}
52245205
5225fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5206fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5226 const pt = self.pt;5207 const zcu = self.object.zcu;
5227 const zcu = pt.zcu;
5228 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;5208 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5229 const dest_slice = try self.resolveInst(bin_op.lhs);5209 const dest_slice = try self.resolveInst(bin_op.lhs);
5230 const dest_ptr_ty = self.typeOf(bin_op.lhs);5210 const dest_ptr_ty = self.typeOf(bin_op.lhs);
...@@ -5248,8 +5228,7 @@ fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -5248,8 +5228,7 @@ fn airMemmove(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
5248}5228}
52495229
5250fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5230fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5251 const pt = self.pt;5231 const zcu = self.object.zcu;
5252 const zcu = pt.zcu;
5253 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;5232 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
5254 const un_ptr_ty = self.typeOf(bin_op.lhs);5233 const un_ptr_ty = self.typeOf(bin_op.lhs);
5255 const un_ty = un_ptr_ty.childType(zcu);5234 const un_ty = un_ptr_ty.childType(zcu);
...@@ -5280,8 +5259,7 @@ fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder....@@ -5280,8 +5259,7 @@ fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.
52805259
5281fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5260fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5282 const o = self.object;5261 const o = self.object;
5283 const pt = self.pt;5262 const zcu = o.zcu;
5284 const zcu = pt.zcu;
5285 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;5263 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5286 const un_ty = self.typeOf(ty_op.operand);5264 const un_ty = self.typeOf(ty_op.operand);
5287 const layout = un_ty.unionGetLayout(zcu);5265 const layout = un_ty.unionGetLayout(zcu);
...@@ -5354,8 +5332,7 @@ fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)...@@ -5354,8 +5332,7 @@ fn airBitOp(self: *FuncGen, inst: Air.Inst.Index, intrinsic: Builder.Intrinsic)
53545332
5355fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5333fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5356 const o = self.object;5334 const o = self.object;
5357 const pt = self.pt;5335 const zcu = o.zcu;
5358 const zcu = pt.zcu;
5359 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;5336 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5360 const operand_ty = self.typeOf(ty_op.operand);5337 const operand_ty = self.typeOf(ty_op.operand);
5361 var bits = operand_ty.intInfo(zcu).bits;5338 var bits = operand_ty.intInfo(zcu).bits;
...@@ -5389,8 +5366,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val...@@ -5389,8 +5366,7 @@ fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
53895366
5390fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5367fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5391 const o = self.object;5368 const o = self.object;
5392 const pt = self.pt;5369 const zcu = o.zcu;
5393 const zcu = pt.zcu;
5394 const ip = &zcu.intern_pool;5370 const ip = &zcu.intern_pool;
5395 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;5371 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
5396 const operand = try self.resolveInst(ty_op.operand);5372 const operand = try self.resolveInst(ty_op.operand);
...@@ -5426,7 +5402,7 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui...@@ -5426,7 +5402,7 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui
5426 const operand = try self.resolveInst(un_op);5402 const operand = try self.resolveInst(un_op);
5427 const enum_ty = self.typeOf(un_op);5403 const enum_ty = self.typeOf(un_op);
54285404
5429 const llvm_fn = try o.getIsNamedEnumValueFunction(self.pt, enum_ty);5405 const llvm_fn = try o.getIsNamedEnumValueFunction(enum_ty);
5430 return self.wip.call(5406 return self.wip.call(
5431 .normal,5407 .normal,
5432 .fastcc,5408 .fastcc,
...@@ -5440,12 +5416,11 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui...@@ -5440,12 +5416,11 @@ fn airIsNamedEnumValue(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Bui
54405416
5441fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5417fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5442 const o = self.object;5418 const o = self.object;
5443 const pt = self.pt;
5444 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;5419 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5445 const operand = try self.resolveInst(un_op);5420 const operand = try self.resolveInst(un_op);
5446 const enum_ty = self.typeOf(un_op);5421 const enum_ty = self.typeOf(un_op);
54475422
5448 const llvm_fn = try o.getEnumTagNameFunction(pt, enum_ty);5423 const llvm_fn = try o.getEnumTagNameFunction(enum_ty);
5449 return self.wip.call(5424 return self.wip.call(
5450 .normal,5425 .normal,
5451 .fastcc,5426 .fastcc,
...@@ -5459,8 +5434,7 @@ fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu...@@ -5459,8 +5434,7 @@ fn airTagName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Valu
54595434
5460fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5435fn airErrorName(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5461 const o = self.object;5436 const o = self.object;
5462 const pt = self.pt;5437 const zcu = o.zcu;
5463 const zcu = pt.zcu;
5464 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;5438 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
5465 const operand = try self.resolveInst(un_op);5439 const operand = try self.resolveInst(un_op);
5466 const slice_ty = self.typeOfIndex(inst);5440 const slice_ty = self.typeOfIndex(inst);
...@@ -5493,8 +5467,7 @@ fn airSelect(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value...@@ -5493,8 +5467,7 @@ fn airSelect(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value
54935467
5494fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5468fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5495 const o = fg.object;5469 const o = fg.object;
5496 const pt = fg.pt;5470 const zcu = o.zcu;
5497 const zcu = pt.zcu;
5498 const gpa = zcu.gpa;5471 const gpa = zcu.gpa;
54995472
5500 const unwrapped = fg.air.unwrapShuffleOne(zcu, inst);5473 const unwrapped = fg.air.unwrapShuffleOne(zcu, inst);
...@@ -5534,7 +5507,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val...@@ -5534,7 +5507,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
5534 .elem => llvm_poison_elem,5507 .elem => llvm_poison_elem,
5535 .value => |val| if (!Value.fromInterned(val).isUndef(zcu)) elem: {5508 .value => |val| if (!Value.fromInterned(val).isUndef(zcu)) elem: {
5536 any_defined_comptime_value = true;5509 any_defined_comptime_value = true;
5537 break :elem try o.lowerValue(pt, val);5510 break :elem try o.lowerValue(val);
5538 } else llvm_poison_elem,5511 } else llvm_poison_elem,
5539 };5512 };
5540 }5513 }
...@@ -5600,8 +5573,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val...@@ -5600,8 +5573,7 @@ fn airShuffleOne(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
56005573
5601fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5574fn airShuffleTwo(fg: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5602 const o = fg.object;5575 const o = fg.object;
5603 const pt = fg.pt;5576 const zcu = o.zcu;
5604 const zcu = pt.zcu;
5605 const gpa = zcu.gpa;5577 const gpa = zcu.gpa;
56065578
5607 const unwrapped = fg.air.unwrapShuffleTwo(zcu, inst);5579 const unwrapped = fg.air.unwrapShuffleTwo(zcu, inst);
...@@ -5699,7 +5671,7 @@ fn buildReducedCall(...@@ -5699,7 +5671,7 @@ fn buildReducedCall(
5699 accum_init: Builder.Value,5671 accum_init: Builder.Value,
5700) Allocator.Error!Builder.Value {5672) Allocator.Error!Builder.Value {
5701 const o = self.object;5673 const o = self.object;
5702 const usize_ty = try self.lowerType(Type.usize);5674 const usize_ty = try self.lowerType(.usize);
5703 const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len);5675 const llvm_vector_len = try o.builder.intValue(usize_ty, vector_len);
5704 const llvm_result_ty = accum_init.typeOfWip(&self.wip);5676 const llvm_result_ty = accum_init.typeOfWip(&self.wip);
57055677
...@@ -5753,8 +5725,7 @@ fn buildReducedCall(...@@ -5753,8 +5725,7 @@ fn buildReducedCall(
57535725
5754fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {5726fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) Allocator.Error!Builder.Value {
5755 const o = self.object;5727 const o = self.object;
5756 const pt = self.pt;5728 const zcu = o.zcu;
5757 const zcu = pt.zcu;
5758 const target = zcu.getTarget();5729 const target = zcu.getTarget();
57595730
5760 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;5731 const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce;
...@@ -5863,8 +5834,7 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) A...@@ -5863,8 +5834,7 @@ fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) A
58635834
5864fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5835fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5865 const o = self.object;5836 const o = self.object;
5866 const pt = self.pt;5837 const zcu = o.zcu;
5867 const zcu = pt.zcu;
5868 const ip = &zcu.intern_pool;5838 const ip = &zcu.intern_pool;
5869 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;5839 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5870 const result_ty = self.typeOfIndex(inst);5840 const result_ty = self.typeOfIndex(inst);
...@@ -5960,19 +5930,18 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde...@@ -5960,19 +5930,18 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
5960 const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment);5930 const alloca_inst = try self.buildAlloca(llvm_result_ty, alignment);
59615931
5962 const array_info = result_ty.arrayInfo(zcu);5932 const array_info = result_ty.arrayInfo(zcu);
5963 const elem_ptr_ty = try pt.singleConstPtrType(array_info.elem_type);
59645933
5965 const elem_size = array_info.elem_type.abiSize(zcu);5934 const elem_size = array_info.elem_type.abiSize(zcu);
59665935
5967 for (elements, 0..) |elem, i| {5936 for (elements, 0..) |elem, i| {
5968 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * i);5937 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * i);
5969 const llvm_elem = try self.resolveInst(elem);5938 const llvm_elem = try self.resolveInst(elem);
5970 try self.store(elem_ptr, elem_ptr_ty, llvm_elem, .none);5939 try self.store(elem_ptr, .none, llvm_elem, array_info.elem_type);
5971 }5940 }
5972 if (array_info.sentinel) |sent_val| {5941 if (array_info.sentinel) |sent_val| {
5973 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * array_info.len);5942 const elem_ptr = try self.ptraddConst(alloca_inst, elem_size * array_info.len);
5974 const llvm_elem = try self.resolveValue(sent_val);5943 const llvm_elem = try self.resolveValue(sent_val);
5975 try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none);5944 try self.store(elem_ptr, .none, llvm_elem.toValue(), array_info.elem_type);
5976 }5945 }
59775946
5978 return alloca_inst;5947 return alloca_inst;
...@@ -5983,8 +5952,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde...@@ -5983,8 +5952,7 @@ fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
59835952
5984fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {5953fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
5985 const o = self.object;5954 const o = self.object;
5986 const pt = self.pt;5955 const zcu = o.zcu;
5987 const zcu = pt.zcu;
5988 const ip = &zcu.intern_pool;5956 const ip = &zcu.intern_pool;
5989 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;5957 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
5990 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;5958 const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data;
...@@ -6006,18 +5974,19 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va...@@ -6006,18 +5974,19 @@ fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Va
6006 assert(field_ty.hasRuntimeBits(zcu));5974 assert(field_ty.hasRuntimeBits(zcu));
60075975
6008 {5976 {
6009 const payload_ptr_ty = try pt.ptrType(.{
6010 .child = field_ty.toIntern(),
6011 .flags = .{ .alignment = layout.payload_align },
6012 });
6013 const payload_ptr = try self.ptraddConst(result_ptr, layout.payloadOffset());5977 const payload_ptr = try self.ptraddConst(result_ptr, layout.payloadOffset());
6014 try self.store(payload_ptr, payload_ptr_ty, llvm_payload, .none);5978 try self.store(payload_ptr, layout.payload_align, llvm_payload, field_ty);
6015 }5979 }
60165980
6017 if (layout.tag_size != 0) {5981 if (layout.tag_size != 0) {
6018 const tag_ty: Type = .fromInterned(union_obj.enum_tag_type);5982 const loaded_enum = ip.loadEnumType(union_obj.enum_tag_type);
6019 const tag_val = try pt.enumValueFieldIndex(tag_ty, extra.field_index);5983 const llvm_tag_val = switch (loaded_enum.field_values.getOrNone(ip, extra.field_index)) {
6020 const llvm_tag_val = try o.lowerValue(pt, tag_val.toIntern());5984 .none => try o.builder.intConst(
5985 try o.lowerType(.fromInterned(union_obj.enum_tag_type)),
5986 extra.field_index, // auto-numbered
5987 ),
5988 else => |tag_val_ip| try o.lowerValue(tag_val_ip),
5989 };
6021 const tag_ptr = try self.ptraddConst(result_ptr, layout.tagOffset());5990 const tag_ptr = try self.ptraddConst(result_ptr, layout.tagOffset());
6022 _ = try self.wip.store(.normal, llvm_tag_val.toValue(), tag_ptr, layout.tag_align.toLlvm());5991 _ = try self.wip.store(.normal, llvm_tag_val.toValue(), tag_ptr, layout.tag_align.toLlvm());
6023 }5992 }
...@@ -6043,7 +6012,7 @@ fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val...@@ -6043,7 +6012,7 @@ fn airPrefetch(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Val
6043 // by the target.6012 // by the target.
6044 // To work around this, don't emit llvm.prefetch in this case.6013 // To work around this, don't emit llvm.prefetch in this case.
6045 // See https://bugs.llvm.org/show_bug.cgi?id=210376014 // See https://bugs.llvm.org/show_bug.cgi?id=21037
6046 const zcu = self.pt.zcu;6015 const zcu = self.object.zcu;
6047 const target = zcu.getTarget();6016 const target = zcu.getTarget();
6048 switch (prefetch.cache) {6017 switch (prefetch.cache) {
6049 .instruction => switch (target.cpu.arch) {6018 .instruction => switch (target.cpu.arch) {
...@@ -6097,7 +6066,7 @@ fn workIntrinsic(...@@ -6097,7 +6066,7 @@ fn workIntrinsic(
6097}6066}
60986067
6099fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {6068fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6100 const target = self.pt.zcu.getTarget();6069 const target = self.object.zcu.getTarget();
61016070
6102 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;6071 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
6103 const dimension = pl_op.payload;6072 const dimension = pl_op.payload;
...@@ -6110,8 +6079,7 @@ fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V...@@ -6110,8 +6079,7 @@ fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.V
6110}6079}
61116080
6112fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {6081fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6113 const pt = self.pt;6082 const target = self.object.zcu.getTarget();
6114 const target = pt.zcu.getTarget();
61156083
6116 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;6084 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
6117 const dimension = pl_op.payload;6085 const dimension = pl_op.payload;
...@@ -6138,7 +6106,7 @@ fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde...@@ -6138,7 +6106,7 @@ fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builde
6138}6106}
61396107
6140fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {6108fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) Allocator.Error!Builder.Value {
6141 const target = self.pt.zcu.getTarget();6109 const target = self.object.zcu.getTarget();
61426110
6143 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;6111 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
6144 const dimension = pl_op.payload;6112 const dimension = pl_op.payload;
...@@ -6158,7 +6126,7 @@ fn optCmpNull(...@@ -6158,7 +6126,7 @@ fn optCmpNull(
6158 opt_ptr: Builder.Value,6126 opt_ptr: Builder.Value,
6159 access_kind: Builder.MemoryAccessKind,6127 access_kind: Builder.MemoryAccessKind,
6160) Allocator.Error!Builder.Value {6128) Allocator.Error!Builder.Value {
6161 const zcu = self.pt.zcu;6129 const zcu = self.object.zcu;
6162 assert(isByRef(opt_ty, zcu));6130 assert(isByRef(opt_ty, zcu));
6163 comptime assert(optional_layout_version == 3);6131 comptime assert(optional_layout_version == 3);
6164 // Non-null bit is always after the payload, with no padding because it has alignment 1.6132 // Non-null bit is always after the payload, with no padding because it has alignment 1.
...@@ -6174,8 +6142,7 @@ fn optPayloadHandle(...@@ -6174,8 +6142,7 @@ fn optPayloadHandle(
6174 opt_ty: Type,6142 opt_ty: Type,
6175 can_elide_load: bool,6143 can_elide_load: bool,
6176) Allocator.Error!Builder.Value {6144) Allocator.Error!Builder.Value {
6177 const pt = fg.pt;6145 const zcu = fg.object.zcu;
6178 const zcu = pt.zcu;
6179 assert(isByRef(opt_ty, zcu));6146 assert(isByRef(opt_ty, zcu));
6180 const payload_ty = opt_ty.optionalChild(zcu);6147 const payload_ty = opt_ty.optionalChild(zcu);
61816148
...@@ -6197,8 +6164,7 @@ fn fieldPtr(...@@ -6197,8 +6164,7 @@ fn fieldPtr(
6197 aggregate_ptr_ty: Type,6164 aggregate_ptr_ty: Type,
6198 field_index: u32,6165 field_index: u32,
6199) Allocator.Error!Builder.Value {6166) Allocator.Error!Builder.Value {
6200 const pt = self.pt;6167 const zcu = self.object.zcu;
6201 const zcu = pt.zcu;
6202 const aggregate_ty = aggregate_ptr_ty.childType(zcu);6168 const aggregate_ty = aggregate_ptr_ty.childType(zcu);
6203 if (aggregate_ty.containerLayout(zcu) == .@"packed") {6169 if (aggregate_ty.containerLayout(zcu) == .@"packed") {
6204 // A pointer to a bitpack field is equivalent to a pointer to the whole bitpack; the6170 // A pointer to a bitpack field is equivalent to a pointer to the whole bitpack; the
...@@ -6226,8 +6192,7 @@ fn loadTruncate(...@@ -6226,8 +6192,7 @@ fn loadTruncate(
6226 // => so load the byte aligned value and trunc the unwanted bits.6192 // => so load the byte aligned value and trunc the unwanted bits.
62276193
6228 const o = fg.object;6194 const o = fg.object;
6229 const pt = fg.pt;6195 const zcu = o.zcu;
6230 const zcu = pt.zcu;
6231 const payload_llvm_ty = try fg.lowerType(payload_ty);6196 const payload_llvm_ty = try fg.lowerType(payload_ty);
6232 const abi_size = payload_ty.abiSize(zcu);6197 const abi_size = payload_ty.abiSize(zcu);
62336198
...@@ -6256,12 +6221,11 @@ fn loadByRef(...@@ -6256,12 +6221,11 @@ fn loadByRef(
6256 access_kind: Builder.MemoryAccessKind,6221 access_kind: Builder.MemoryAccessKind,
6257) Allocator.Error!Builder.Value {6222) Allocator.Error!Builder.Value {
6258 const o = fg.object;6223 const o = fg.object;
6259 const pt = fg.pt;
6260 const pointee_llvm_ty = try fg.lowerType(pointee_type);6224 const pointee_llvm_ty = try fg.lowerType(pointee_type);
6261 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)6225 const result_align = InternPool.Alignment.fromLlvm(ptr_alignment)
6262 .max(pointee_type.abiAlignment(pt.zcu)).toLlvm();6226 .max(pointee_type.abiAlignment(o.zcu)).toLlvm();
6263 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);6227 const result_ptr = try fg.buildAlloca(pointee_llvm_ty, result_align);
6264 const size_bytes = pointee_type.abiSize(pt.zcu);6228 const size_bytes = pointee_type.abiSize(o.zcu);
6265 _ = try fg.wip.callMemCpy(6229 _ = try fg.wip.callMemCpy(
6266 result_ptr,6230 result_ptr,
6267 result_align,6231 result_align,
...@@ -6274,76 +6238,24 @@ fn loadByRef(...@@ -6274,76 +6238,24 @@ fn loadByRef(
6274 return result_ptr;6238 return result_ptr;
6275}6239}
62766240
6277/// This function always performs a copy. For isByRef=true types, it creates a new6241/// If `isByRef` returns `true` for `elem_ty`, this still performs a copy by memcpy'ing the value
6278/// alloca and copies the value into it, then returns the alloca instruction.6242/// into a new alloca.
6279/// For isByRef=false types, it creates a load instruction and returns it.6243fn load(
6280fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) Allocator.Error!Builder.Value {6244 fg: *FuncGen,
6281 const o = self.object;6245 ptr: Builder.Value,
6282 const pt = self.pt;6246 elem_ty: Type,
6283 const zcu = pt.zcu;6247 ptr_alignment: Builder.Alignment,
6284 const info = ptr_ty.ptrInfo(zcu);6248 access_kind: Builder.MemoryAccessKind,
6285 const elem_ty = Type.fromInterned(info.child);6249) Allocator.Error!Builder.Value {
6286 if (!elem_ty.hasRuntimeBits(zcu)) return .none;6250 const zcu = fg.object.zcu;
6287
6288 const ptr_alignment = (if (info.flags.alignment != .none)
6289 @as(InternPool.Alignment, info.flags.alignment)
6290 else
6291 elem_ty.abiAlignment(zcu)).toLlvm();
6292
6293 const access_kind: Builder.MemoryAccessKind =
6294 if (info.flags.is_volatile) .@"volatile" else .normal;
6295
6296 if (info.flags.vector_index != .none) {
6297 const index_u32 = try o.builder.intValue(.i32, info.flags.vector_index);
6298 const vec_elem_ty = try self.lowerType(elem_ty);
6299 const vec_ty = try o.builder.vectorType(.normal, info.packed_offset.host_size, vec_elem_ty);
6300
6301 const loaded_vector = try self.wip.load(access_kind, vec_ty, ptr, ptr_alignment, "");
6302 return self.wip.extractElement(loaded_vector, index_u32, "");
6303 }
6304
6305 if (info.packed_offset.host_size == 0) {
6306 if (isByRef(elem_ty, zcu)) {
6307 return self.loadByRef(ptr, elem_ty, ptr_alignment, access_kind);
6308 }
6309 return self.loadTruncate(access_kind, elem_ty, ptr, ptr_alignment);
6310 }
6311
6312 const containing_int_ty = try o.builder.intType(@intCast(info.packed_offset.host_size * 8));
6313 const containing_int =
6314 try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, "");
6315
6316 const elem_bits = ptr_ty.childType(zcu).bitSize(zcu);
6317 const shift_amt = try o.builder.intValue(containing_int_ty, info.packed_offset.bit_offset);
6318 const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, "");
6319 const elem_llvm_ty = try self.lowerType(elem_ty);
6320
6321 if (isByRef(elem_ty, zcu)) {6251 if (isByRef(elem_ty, zcu)) {
6322 const result_align = elem_ty.abiAlignment(zcu).toLlvm();6252 return fg.loadByRef(ptr, elem_ty, ptr_alignment, access_kind);
6323 const result_ptr = try self.buildAlloca(elem_llvm_ty, result_align);6253 } else {
63246254 return fg.loadTruncate(access_kind, elem_ty, ptr, ptr_alignment);
6325 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6326 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6327 _ = try self.wip.store(.normal, truncated_int, result_ptr, result_align);
6328 return result_ptr;
6329 }
6330
6331 if (elem_ty.zigTypeTag(zcu) == .float or elem_ty.zigTypeTag(zcu) == .vector) {
6332 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6333 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6334 return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, "");
6335 }
6336
6337 if (elem_ty.isPtrAtRuntime(zcu)) {
6338 const same_size_int = try o.builder.intType(@intCast(elem_bits));
6339 const truncated_int = try self.wip.cast(.trunc, shifted_value, same_size_int, "");
6340 return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, "");
6341 }6255 }
6342
6343 return self.wip.cast(.trunc, shifted_value, elem_llvm_ty, "");
6344}6256}
63456257
6346fn store(6258fn storeFull(
6347 self: *FuncGen,6259 self: *FuncGen,
6348 ptr: Builder.Value,6260 ptr: Builder.Value,
6349 ptr_ty: Type,6261 ptr_ty: Type,
...@@ -6351,8 +6263,7 @@ fn store(...@@ -6351,8 +6263,7 @@ fn store(
6351 ordering: Builder.AtomicOrdering,6263 ordering: Builder.AtomicOrdering,
6352) Allocator.Error!void {6264) Allocator.Error!void {
6353 const o = self.object;6265 const o = self.object;
6354 const pt = self.pt;6266 const zcu = o.zcu;
6355 const zcu = pt.zcu;
6356 const info = ptr_ty.ptrInfo(zcu);6267 const info = ptr_ty.ptrInfo(zcu);
6357 const elem_ty = Type.fromInterned(info.child);6268 const elem_ty = Type.fromInterned(info.child);
6358 if (!elem_ty.hasRuntimeBits(zcu)) {6269 if (!elem_ty.hasRuntimeBits(zcu)) {
...@@ -6433,12 +6344,51 @@ fn store(...@@ -6433,12 +6344,51 @@ fn store(
6433 ptr_alignment,6344 ptr_alignment,
6434 elem,6345 elem,
6435 elem_ty.abiAlignment(zcu).toLlvm(),6346 elem_ty.abiAlignment(zcu).toLlvm(),
6436 try o.builder.intValue(try self.lowerType(Type.usize), elem_ty.abiSize(zcu)),6347 try o.builder.intValue(try self.lowerType(.usize), elem_ty.abiSize(zcu)),
6437 access_kind,6348 access_kind,
6438 self.disable_intrinsics,6349 self.disable_intrinsics,
6439 );6350 );
6440}6351}
64416352
6353/// Non-atomic, non-volatile, non-packed store.
6354fn store(
6355 fg: *FuncGen,
6356 ptr: Builder.Value,
6357 ptr_align: InternPool.Alignment,
6358 elem: Builder.Value,
6359 elem_ty: Type,
6360) Allocator.Error!void {
6361 const o = fg.object;
6362 const zcu = o.zcu;
6363 const llvm_ptr_align = switch (ptr_align) {
6364 .none => elem_ty.abiAlignment(zcu).toLlvm(),
6365 else => ptr_align.toLlvm(),
6366 };
6367 if (isByRef(elem_ty, zcu)) {
6368 _ = try fg.wip.callMemCpy(
6369 ptr,
6370 llvm_ptr_align,
6371 elem,
6372 elem_ty.abiAlignment(zcu).toLlvm(),
6373 try o.builder.intValue(
6374 try fg.lowerType(.usize),
6375 elem_ty.abiSize(zcu),
6376 ),
6377 .normal,
6378 fg.disable_intrinsics,
6379 );
6380 } else {
6381 _ = try fg.wip.storeAtomic(
6382 .normal,
6383 elem,
6384 ptr,
6385 fg.sync_scope,
6386 .none,
6387 llvm_ptr_align,
6388 );
6389 }
6390}
6391
6442fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void {6392fn valgrindMarkUndef(fg: *FuncGen, ptr: Builder.Value, len: Builder.Value) Allocator.Error!void {
6443 const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;6393 const VG_USERREQ__MAKE_MEM_UNDEFINED = 1296236545;
6444 const o = fg.object;6394 const o = fg.object;
...@@ -6460,8 +6410,7 @@ fn valgrindClientRequest(...@@ -6460,8 +6410,7 @@ fn valgrindClientRequest(
6460 a5: Builder.Value,6410 a5: Builder.Value,
6461) Allocator.Error!Builder.Value {6411) Allocator.Error!Builder.Value {
6462 const o = fg.object;6412 const o = fg.object;
6463 const pt = fg.pt;6413 const zcu = o.zcu;
6464 const zcu = pt.zcu;
6465 const target = zcu.getTarget();6414 const target = zcu.getTarget();
6466 if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;6415 if (!target_util.hasValgrindSupport(target, .stage2_llvm)) return default_value;
64676416
...@@ -6588,18 +6537,17 @@ fn valgrindClientRequest(...@@ -6588,18 +6537,17 @@ fn valgrindClientRequest(
6588}6537}
65896538
6590fn typeOf(fg: *FuncGen, inst: Air.Inst.Ref) Type {6539fn typeOf(fg: *FuncGen, inst: Air.Inst.Ref) Type {
6591 const zcu = fg.pt.zcu;6540 const zcu = fg.object.zcu;
6592 return fg.air.typeOf(inst, &zcu.intern_pool);6541 return fg.air.typeOf(inst, &zcu.intern_pool);
6593}6542}
65946543
6595fn typeOfIndex(fg: *FuncGen, inst: Air.Inst.Index) Type {6544fn typeOfIndex(fg: *FuncGen, inst: Air.Inst.Index) Type {
6596 const zcu = fg.pt.zcu;6545 const zcu = fg.object.zcu;
6597 return fg.air.typeOfIndex(inst, &zcu.intern_pool);6546 return fg.air.typeOfIndex(inst, &zcu.intern_pool);
6598}6547}
65996548
6600const ParamTypeIterator = struct {6549const ParamTypeIterator = struct {
6601 object: *Object,6550 object: *Object,
6602 pt: Zcu.PerThread,
6603 fn_info: InternPool.Key.FuncType,6551 fn_info: InternPool.Key.FuncType,
6604 zig_index: u32,6552 zig_index: u32,
6605 llvm_index: u32,6553 llvm_index: u32,
...@@ -6622,7 +6570,7 @@ const ParamTypeIterator = struct {...@@ -6622,7 +6570,7 @@ const ParamTypeIterator = struct {
66226570
6623 pub fn next(it: *ParamTypeIterator) Allocator.Error!?Lowering {6571 pub fn next(it: *ParamTypeIterator) Allocator.Error!?Lowering {
6624 if (it.zig_index >= it.fn_info.param_types.len) return null;6572 if (it.zig_index >= it.fn_info.param_types.len) return null;
6625 const ip = &it.pt.zcu.intern_pool;6573 const ip = &it.object.zcu.intern_pool;
6626 const ty = it.fn_info.param_types.get(ip)[it.zig_index];6574 const ty = it.fn_info.param_types.get(ip)[it.zig_index];
6627 it.byval_attr = false;6575 it.byval_attr = false;
6628 return nextInner(it, Type.fromInterned(ty));6576 return nextInner(it, Type.fromInterned(ty));
...@@ -6630,8 +6578,7 @@ const ParamTypeIterator = struct {...@@ -6630,8 +6578,7 @@ const ParamTypeIterator = struct {
66306578
6631 /// `airCall` uses this instead of `next` so that it can take into account variadic functions.6579 /// `airCall` uses this instead of `next` so that it can take into account variadic functions.
6632 fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) Allocator.Error!?Lowering {6580 fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) Allocator.Error!?Lowering {
6633 assert(std.meta.eql(it.pt, fg.pt));6581 const ip = &it.object.zcu.intern_pool;
6634 const ip = &it.pt.zcu.intern_pool;
6635 if (it.zig_index >= it.fn_info.param_types.len) {6582 if (it.zig_index >= it.fn_info.param_types.len) {
6636 if (it.zig_index >= args.len) {6583 if (it.zig_index >= args.len) {
6637 return null;6584 return null;
...@@ -6644,8 +6591,7 @@ const ParamTypeIterator = struct {...@@ -6644,8 +6591,7 @@ const ParamTypeIterator = struct {
6644 }6591 }
66456592
6646 fn nextInner(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {6593 fn nextInner(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {
6647 const pt = it.pt;6594 const zcu = it.object.zcu;
6648 const zcu = pt.zcu;
6649 const target = zcu.getTarget();6595 const target = zcu.getTarget();
66506596
6651 if (!ty.hasRuntimeBits(zcu)) {6597 if (!ty.hasRuntimeBits(zcu)) {
...@@ -6744,7 +6690,7 @@ const ParamTypeIterator = struct {...@@ -6744,7 +6690,7 @@ const ParamTypeIterator = struct {
6744 for (0..ty.structFieldCount(zcu)) |field_index| {6690 for (0..ty.structFieldCount(zcu)) |field_index| {
6745 const field_ty = ty.fieldType(field_index, zcu);6691 const field_ty = ty.fieldType(field_index, zcu);
6746 if (!field_ty.hasRuntimeBits(zcu)) continue;6692 if (!field_ty.hasRuntimeBits(zcu)) continue;
6747 it.types_buffer[it.types_len] = try it.object.lowerType(pt, field_ty);6693 it.types_buffer[it.types_len] = try it.object.lowerType(field_ty);
6748 it.types_len += 1;6694 it.types_len += 1;
6749 }6695 }
6750 it.llvm_index += it.types_len - 1;6696 it.llvm_index += it.types_len - 1;
...@@ -6760,7 +6706,7 @@ const ParamTypeIterator = struct {...@@ -6760,7 +6706,7 @@ const ParamTypeIterator = struct {
6760 return .byval;6706 return .byval;
6761 } else {6707 } else {
6762 var types_buffer: [8]Builder.Type = undefined;6708 var types_buffer: [8]Builder.Type = undefined;
6763 types_buffer[0] = try it.object.lowerType(pt, scalar_ty);6709 types_buffer[0] = try it.object.lowerType(scalar_ty);
6764 it.types_buffer = types_buffer;6710 it.types_buffer = types_buffer;
6765 it.types_len = 1;6711 it.types_len = 1;
6766 it.llvm_index += 1;6712 it.llvm_index += 1;
...@@ -6785,7 +6731,7 @@ const ParamTypeIterator = struct {...@@ -6785,7 +6731,7 @@ const ParamTypeIterator = struct {
6785 }6731 }
67866732
6787 fn nextWin64(it: *ParamTypeIterator, ty: Type) ?Lowering {6733 fn nextWin64(it: *ParamTypeIterator, ty: Type) ?Lowering {
6788 const zcu = it.pt.zcu;6734 const zcu = it.object.zcu;
6789 switch (x86_64_abi.classifyWindows(ty, zcu, zcu.getTarget(), .arg)) {6735 switch (x86_64_abi.classifyWindows(ty, zcu, zcu.getTarget(), .arg)) {
6790 .integer => {6736 .integer => {
6791 if (isScalar(zcu, ty)) {6737 if (isScalar(zcu, ty)) {
...@@ -6818,7 +6764,7 @@ const ParamTypeIterator = struct {...@@ -6818,7 +6764,7 @@ const ParamTypeIterator = struct {
6818 }6764 }
68196765
6820 fn nextSystemV(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {6766 fn nextSystemV(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering {
6821 const zcu = it.pt.zcu;6767 const zcu = it.object.zcu;
6822 const ip = &zcu.intern_pool;6768 const ip = &zcu.intern_pool;
6823 ty.assertHasLayout(zcu);6769 ty.assertHasLayout(zcu);
6824 const classes = x86_64_abi.classifySystemV(ty, zcu, zcu.getTarget(), .arg);6770 const classes = x86_64_abi.classifySystemV(ty, zcu, zcu.getTarget(), .arg);
...@@ -6909,10 +6855,9 @@ const ParamTypeIterator = struct {...@@ -6909,10 +6855,9 @@ const ParamTypeIterator = struct {
6909 return .multiple_llvm_types;6855 return .multiple_llvm_types;
6910 }6856 }
6911};6857};
6912pub fn iterateParamTypes(object: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) ParamTypeIterator {6858pub fn iterateParamTypes(object: *Object, fn_info: InternPool.Key.FuncType) ParamTypeIterator {
6913 return .{6859 return .{
6914 .object = object,6860 .object = object,
6915 .pt = pt,
6916 .fn_info = fn_info,6861 .fn_info = fn_info,
6917 .zig_index = 0,6862 .zig_index = 0,
6918 .llvm_index = 0,6863 .llvm_index = 0,
...@@ -6976,8 +6921,8 @@ fn firstParamSRetSystemV(ty: Type, zcu: *Zcu, target: *const std.Target) bool {...@@ -6976,8 +6921,8 @@ fn firstParamSRetSystemV(ty: Type, zcu: *Zcu, target: *const std.Target) bool {
6976/// In order to support the C calling convention, some return types need to be lowered6921/// In order to support the C calling convention, some return types need to be lowered
6977/// completely differently in the function prototype to honor the C ABI, and then6922/// completely differently in the function prototype to honor the C ABI, and then
6978/// be effectively bitcasted to the actual return type.6923/// be effectively bitcasted to the actual return type.
6979pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {6924pub fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
6980 const zcu = pt.zcu;6925 const zcu = o.zcu;
6981 const return_type = Type.fromInterned(fn_info.return_type);6926 const return_type = Type.fromInterned(fn_info.return_type);
6982 if (!return_type.hasRuntimeBits(zcu)) {6927 if (!return_type.hasRuntimeBits(zcu)) {
6983 assert(!return_type.isError(zcu));6928 assert(!return_type.isError(zcu));
...@@ -6986,27 +6931,27 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT...@@ -6986,27 +6931,27 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT
6986 const target = zcu.getTarget();6931 const target = zcu.getTarget();
6987 switch (fn_info.cc) {6932 switch (fn_info.cc) {
6988 .@"inline" => unreachable,6933 .@"inline" => unreachable,
6989 .auto => return if (returnTypeByRef(zcu, target, return_type)) .void else o.lowerType(pt, return_type),6934 .auto => return if (returnTypeByRef(zcu, target, return_type)) .void else o.lowerType(return_type),
69906935
6991 .x86_64_sysv => return lowerSystemVFnRetTy(o, pt, fn_info),6936 .x86_64_sysv => return lowerSystemVFnRetTy(o, fn_info),
6992 .x86_64_win => return lowerWin64FnRetTy(o, pt, fn_info),6937 .x86_64_win => return lowerWin64FnRetTy(o, fn_info),
6993 .x86_stdcall => return if (isScalar(zcu, return_type)) o.lowerType(pt, return_type) else .void,6938 .x86_stdcall => return if (isScalar(zcu, return_type)) o.lowerType(return_type) else .void,
6994 .x86_sysv, .x86_win => return if (isByRef(return_type, zcu)) .void else o.lowerType(pt, return_type),6939 .x86_sysv, .x86_win => return if (isByRef(return_type, zcu)) .void else o.lowerType(return_type),
6995 .aarch64_aapcs, .aarch64_aapcs_darwin, .aarch64_aapcs_win => switch (aarch64_c_abi.classifyType(return_type, zcu)) {6940 .aarch64_aapcs, .aarch64_aapcs_darwin, .aarch64_aapcs_win => switch (aarch64_c_abi.classifyType(return_type, zcu)) {
6996 .memory => return .void,6941 .memory => return .void,
6997 .float_array => return o.lowerType(pt, return_type),6942 .float_array => return o.lowerType(return_type),
6998 .byval => return o.lowerType(pt, return_type),6943 .byval => return o.lowerType(return_type),
6999 .integer => return o.builder.intType(@intCast(return_type.bitSize(zcu))),6944 .integer => return o.builder.intType(@intCast(return_type.bitSize(zcu))),
7000 .double_integer => return o.builder.arrayType(2, .i64),6945 .double_integer => return o.builder.arrayType(2, .i64),
7001 },6946 },
7002 .arm_aapcs, .arm_aapcs_vfp => switch (arm_c_abi.classifyType(return_type, zcu, .ret)) {6947 .arm_aapcs, .arm_aapcs_vfp => switch (arm_c_abi.classifyType(return_type, zcu, .ret)) {
7003 .memory, .i64_array => return .void,6948 .memory, .i64_array => return .void,
7004 .i32_array => |len| return if (len == 1) .i32 else .void,6949 .i32_array => |len| return if (len == 1) .i32 else .void,
7005 .byval => return o.lowerType(pt, return_type),6950 .byval => return o.lowerType(return_type),
7006 },6951 },
7007 .mips_o32 => switch (mips_c_abi.classifyType(return_type, zcu, .ret)) {6952 .mips_o32 => switch (mips_c_abi.classifyType(return_type, zcu, .ret)) {
7008 .memory, .i32_array => return .void,6953 .memory, .i32_array => return .void,
7009 .byval => return o.lowerType(pt, return_type),6954 .byval => return o.lowerType(return_type),
7010 },6955 },
7011 .riscv64_lp64, .riscv32_ilp32 => switch (riscv_c_abi.classifyType(return_type, zcu)) {6956 .riscv64_lp64, .riscv32_ilp32 => switch (riscv_c_abi.classifyType(return_type, zcu)) {
7012 .memory => return .void,6957 .memory => return .void,
...@@ -7019,53 +6964,53 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT...@@ -7019,53 +6964,53 @@ pub fn lowerFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncT
7019 };6964 };
7020 return o.builder.structType(.normal, &.{ integer, integer });6965 return o.builder.structType(.normal, &.{ integer, integer });
7021 },6966 },
7022 .byval => return o.lowerType(pt, return_type),6967 .byval => return o.lowerType(return_type),
7023 .fields => {6968 .fields => {
7024 var types_len: usize = 0;6969 var types_len: usize = 0;
7025 var types: [8]Builder.Type = undefined;6970 var types: [8]Builder.Type = undefined;
7026 for (0..return_type.structFieldCount(zcu)) |field_index| {6971 for (0..return_type.structFieldCount(zcu)) |field_index| {
7027 const field_ty = return_type.fieldType(field_index, zcu);6972 const field_ty = return_type.fieldType(field_index, zcu);
7028 if (!field_ty.hasRuntimeBits(zcu)) continue;6973 if (!field_ty.hasRuntimeBits(zcu)) continue;
7029 types[types_len] = try o.lowerType(pt, field_ty);6974 types[types_len] = try o.lowerType(field_ty);
7030 types_len += 1;6975 types_len += 1;
7031 }6976 }
7032 return o.builder.structType(.normal, types[0..types_len]);6977 return o.builder.structType(.normal, types[0..types_len]);
7033 },6978 },
7034 },6979 },
7035 .wasm_mvp => switch (wasm_c_abi.classifyType(return_type, zcu)) {6980 .wasm_mvp => switch (wasm_c_abi.classifyType(return_type, zcu)) {
7036 .direct => |scalar_ty| return o.lowerType(pt, scalar_ty),6981 .direct => |scalar_ty| return o.lowerType(scalar_ty),
7037 .indirect => return .void,6982 .indirect => return .void,
7038 },6983 },
7039 // TODO investigate other callconvs6984 // TODO investigate other callconvs
7040 else => return o.lowerType(pt, return_type),6985 else => return o.lowerType(return_type),
7041 }6986 }
7042}6987}
70436988
7044fn lowerWin64FnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {6989fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
7045 const zcu = pt.zcu;6990 const zcu = o.zcu;
7046 const return_type = Type.fromInterned(fn_info.return_type);6991 const return_type = Type.fromInterned(fn_info.return_type);
7047 switch (x86_64_abi.classifyWindows(return_type, zcu, zcu.getTarget(), .ret)) {6992 switch (x86_64_abi.classifyWindows(return_type, zcu, zcu.getTarget(), .ret)) {
7048 .integer => {6993 .integer => {
7049 if (isScalar(zcu, return_type)) {6994 if (isScalar(zcu, return_type)) {
7050 return o.lowerType(pt, return_type);6995 return o.lowerType(return_type);
7051 } else {6996 } else {
7052 return o.builder.intType(@intCast(return_type.abiSize(zcu) * 8));6997 return o.builder.intType(@intCast(return_type.abiSize(zcu) * 8));
7053 }6998 }
7054 },6999 },
7055 .win_i128 => return o.builder.vectorType(.normal, 2, .i64),7000 .win_i128 => return o.builder.vectorType(.normal, 2, .i64),
7056 .memory => return .void,7001 .memory => return .void,
7057 .sse => return o.lowerType(pt, return_type),7002 .sse => return o.lowerType(return_type),
7058 else => unreachable,7003 else => unreachable,
7059 }7004 }
7060}7005}
70617006
7062fn lowerSystemVFnRetTy(o: *Object, pt: Zcu.PerThread, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {7007fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type {
7063 const zcu = pt.zcu;7008 const zcu = o.zcu;
7064 const ip = &zcu.intern_pool;7009 const ip = &zcu.intern_pool;
7065 const return_type = Type.fromInterned(fn_info.return_type);7010 const return_type = Type.fromInterned(fn_info.return_type);
7066 return_type.assertHasLayout(zcu);7011 return_type.assertHasLayout(zcu);
7067 if (isScalar(zcu, return_type)) {7012 if (isScalar(zcu, return_type)) {
7068 return o.lowerType(pt, return_type);7013 return o.lowerType(return_type);
7069 }7014 }
7070 const classes = x86_64_abi.classifySystemV(return_type, zcu, zcu.getTarget(), .ret);7015 const classes = x86_64_abi.classifySystemV(return_type, zcu, zcu.getTarget(), .ret);
7071 var types_index: u32 = 0;7016 var types_index: u32 = 0;
...@@ -7297,7 +7242,7 @@ pub fn isByRef(ty: Type, zcu: *const Zcu) bool {...@@ -7297,7 +7242,7 @@ pub fn isByRef(ty: Type, zcu: *const Zcu) bool {
7297/// RMW exchange of floating-point values is bitcasted to same-sized integer7242/// RMW exchange of floating-point values is bitcasted to same-sized integer
7298/// types to work around a LLVM deficiency when targeting ARM/AArch64.7243/// types to work around a LLVM deficiency when targeting ARM/AArch64.
7299fn getAtomicAbiType(fg: *const FuncGen, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type {7244fn getAtomicAbiType(fg: *const FuncGen, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type {
7300 const zcu = fg.pt.zcu;7245 const zcu = fg.object.zcu;
7301 switch (ty.zigTypeTag(zcu)) {7246 switch (ty.zigTypeTag(zcu)) {
7302 .int, .@"enum", .@"struct", .@"union" => {},7247 .int, .@"enum", .@"struct", .@"union" => {},
7303 .float => {7248 .float => {
src/codegen/riscv64/CodeGen.zig+2-2
...@@ -4956,8 +4956,8 @@ fn genCall(...@@ -4956,8 +4956,8 @@ fn genCall(
4956 // on linking.4956 // on linking.
4957 switch (info) {4957 switch (info) {
4958 .air => |callee| {4958 .air => |callee| {
4959 if (try func.air.value(callee, pt)) |func_value| {4959 if (callee.toInterned()) |func_ip_index| {
4960 const func_key = zcu.intern_pool.indexToKey(func_value.ip_index);4960 const func_key = zcu.intern_pool.indexToKey(func_ip_index);
4961 switch (switch (func_key) {4961 switch (switch (func_key) {
4962 else => func_key,4962 else => func_key,
4963 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {4963 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {
src/codegen/sparc64/CodeGen.zig+2-2
...@@ -1310,7 +1310,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier...@@ -1310,7 +1310,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier
13101310
1311 // Due to incremental compilation, how function calls are generated depends1311 // Due to incremental compilation, how function calls are generated depends
1312 // on linking.1312 // on linking.
1313 if (try self.air.value(call.callee, pt)) |func_value| switch (ip.indexToKey(func_value.toIntern())) {1313 if (call.callee.toInterned()) |func_ip_index| switch (ip.indexToKey(func_ip_index)) {
1314 .func => {1314 .func => {
1315 return self.fail("TODO implement calling functions", .{});1315 return self.fail("TODO implement calling functions", .{});
1316 },1316 },
...@@ -4487,7 +4487,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {...@@ -4487,7 +4487,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
4487 return self.getResolvedInstValue(inst);4487 return self.getResolvedInstValue(inst);
4488 }4488 }
44894489
4490 return self.genTypedValue((try self.air.value(ref, pt)).?);4490 return self.genTypedValue(.fromInterned(ref.toInterned().?));
4491}4491}
44924492
4493fn ret(self: *Self, mcv: MCValue) !void {4493fn ret(self: *Self, mcv: MCValue) !void {
src/codegen/spirv/CodeGen.zig+7-10
...@@ -387,13 +387,12 @@ fn importExtendedSet(cg: *CodeGen) !Id {...@@ -387,13 +387,12 @@ fn importExtendedSet(cg: *CodeGen) !Id {
387387
388/// Fetch the result-id for a previously generated instruction or constant.388/// Fetch the result-id for a previously generated instruction or constant.
389fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {389fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {
390 const pt = cg.pt;
391 const zcu = cg.module.zcu;390 const zcu = cg.module.zcu;
392 const ip = &zcu.intern_pool;391 const ip = &zcu.intern_pool;
393 if (try cg.air.value(inst, pt)) |val| {392 if (inst.toInterned()) |val_ip_index| {
394 const ty = cg.typeOf(inst);393 const ty = cg.typeOf(inst);
395 if (ty.zigTypeTag(zcu) == .@"fn") {394 if (ty.zigTypeTag(zcu) == .@"fn") {
396 const fn_nav = switch (zcu.intern_pool.indexToKey(val.ip_index)) {395 const fn_nav = switch (zcu.intern_pool.indexToKey(val_ip_index)) {
397 .@"extern" => |@"extern"| @"extern".owner_nav,396 .@"extern" => |@"extern"| @"extern".owner_nav,
398 .func => |func| func.owner_nav,397 .func => |func| func.owner_nav,
399 else => unreachable,398 else => unreachable,
...@@ -403,7 +402,7 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {...@@ -403,7 +402,7 @@ fn resolve(cg: *CodeGen, inst: Air.Inst.Ref) !Id {
403 return cg.module.declPtr(spv_decl_index).result_id;402 return cg.module.declPtr(spv_decl_index).result_id;
404 }403 }
405404
406 return try cg.constant(ty, val, .direct);405 return try cg.constant(ty, .fromInterned(val_ip_index), .direct);
407 }406 }
408 const index = inst.toIndex().?;407 const index = inst.toIndex().?;
409 return cg.inst_results.get(index).?; // Assertion means instruction does not dominate usage.408 return cg.inst_results.get(index).?; // Assertion means instruction does not dominate usage.
...@@ -5657,7 +5656,6 @@ fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id {...@@ -5657,7 +5656,6 @@ fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
56575656
5658fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {5657fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {
5659 const gpa = cg.module.gpa;5658 const gpa = cg.module.gpa;
5660 const pt = cg.pt;
5661 const zcu = cg.module.zcu;5659 const zcu = cg.module.zcu;
5662 const target = cg.module.zcu.getTarget();5660 const target = cg.module.zcu.getTarget();
5663 const switch_br = cg.air.unwrapSwitch(inst);5661 const switch_br = cg.air.unwrapSwitch(inst);
...@@ -5732,7 +5730,7 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {...@@ -5732,7 +5730,7 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) !void {
5732 const label = case_labels.at(case.idx);5730 const label = case_labels.at(case.idx);
57335731
5734 for (case.items) |item| {5732 for (case.items) |item| {
5735 const value = (try cg.air.value(item, pt)) orelse unreachable;5733 const value: Value = .fromInterned(item.toInterned().?);
5736 const int_val: u64 = switch (cond_ty.zigTypeTag(zcu)) {5734 const int_val: u64 = switch (cond_ty.zigTypeTag(zcu)) {
5737 .bool, .int => if (cond_ty.isSignedInt(zcu)) @bitCast(value.toSignedInt(zcu)) else value.toUnsignedInt(zcu),5735 .bool, .int => if (cond_ty.isSignedInt(zcu)) @bitCast(value.toSignedInt(zcu)) else value.toUnsignedInt(zcu),
5738 .@"enum" => blk: {5736 .@"enum" => blk: {
...@@ -5875,9 +5873,9 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {...@@ -5875,9 +5873,9 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
58755873
5876 if (std.mem.eql(u8, in.constraint, "c")) {5874 if (std.mem.eql(u8, in.constraint, "c")) {
5877 // constant5875 // constant
5878 const val = (try cg.air.value(in.operand, cg.pt)) orelse {5876 const val: Value = .fromInterned(in.operand.toInterned() orelse {
5879 return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{});5877 return cg.fail("assembly inputs with 'c' constraint have to be compile-time known", .{});
5880 };5878 });
58815879
5882 // TODO: This entire function should be handled a bit better...5880 // TODO: This entire function should be handled a bit better...
5883 const ip = &zcu.intern_pool;5881 const ip = &zcu.intern_pool;
...@@ -5911,8 +5909,7 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {...@@ -5911,8 +5909,7 @@ fn airAssembly(cg: *CodeGen, inst: Air.Inst.Index) !?Id {
5911 if (input_ty.zigTypeTag(zcu) == .type) {5909 if (input_ty.zigTypeTag(zcu) == .type) {
5912 // This assembly input is a type instead of a value.5910 // This assembly input is a type instead of a value.
5913 // That's fine for now, just make sure to resolve it as such.5911 // That's fine for now, just make sure to resolve it as such.
5914 const val = (try cg.air.value(in.operand, cg.pt)).?;5912 const ty_id = try cg.resolveType(in.operand.toType(), .direct);
5915 const ty_id = try cg.resolveType(val.toType(), .direct);
5916 try ass.value_map.put(gpa, in.name, .{ .ty = ty_id });5913 try ass.value_map.put(gpa, in.name, .{ .ty = ty_id });
5917 } else {5914 } else {
5918 const ty_id = try cg.resolveType(input_ty, .direct);5915 const ty_id = try cg.resolveType(input_ty, .direct);
src/codegen/wasm/CodeGen.zig+3-3
...@@ -303,7 +303,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {...@@ -303,7 +303,7 @@ fn resolveInst(cg: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
303303
304 const pt = cg.pt;304 const pt = cg.pt;
305 const zcu = pt.zcu;305 const zcu = pt.zcu;
306 const val = (try cg.air.value(ref, pt)).?;306 const val: Value = .fromInterned(ref.toInterned().?);
307 const ty = cg.typeOf(ref);307 const ty = cg.typeOf(ref);
308 if (!ty.hasRuntimeBits(zcu) and !ty.isInt(zcu) and !ty.isError(zcu)) {308 if (!ty.hasRuntimeBits(zcu) and !ty.isInt(zcu) and !ty.isError(zcu)) {
309 gop.value_ptr.* = .none;309 gop.value_ptr.* = .none;
...@@ -2006,7 +2006,7 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifie...@@ -2006,7 +2006,7 @@ fn airCall(cg: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifie
2006 const first_param_sret = firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), zcu, cg.target);2006 const first_param_sret = firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), zcu, cg.target);
20072007
2008 const callee: ?InternPool.Nav.Index = blk: {2008 const callee: ?InternPool.Nav.Index = blk: {
2009 const func_val = (try cg.air.value(call.callee, pt)) orelse break :blk null;2009 const func_val: Value = .fromInterned(call.callee.toInterned() orelse break :blk null);
20102010
2011 switch (ip.indexToKey(func_val.toIntern())) {2011 switch (ip.indexToKey(func_val.toIntern())) {
2012 inline .func, .@"extern" => |x| break :blk x.owner_nav,2012 inline .func, .@"extern" => |x| break :blk x.owner_nav,
...@@ -4464,7 +4464,7 @@ fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {...@@ -4464,7 +4464,7 @@ fn lowerConstant(cg: *CodeGen, val: Value) InnerError!WValue {
4464 .vector_type => {4464 .vector_type => {
4465 assert(determineSimdStoreStrategy(ty, zcu, cg.target) == .direct);4465 assert(determineSimdStoreStrategy(ty, zcu, cg.target) == .direct);
4466 var buf: [16]u8 = undefined;4466 var buf: [16]u8 = undefined;
4467 val.writeToMemory(pt, &buf) catch unreachable;4467 val.writeToMemory(zcu, &buf) catch unreachable;
4468 return cg.storeSimdImmd(buf);4468 return cg.storeSimdImmd(buf);
4469 },4469 },
4470 .struct_type => unreachable, // packed structs use `bitpack`4470 .struct_type => unreachable, // packed structs use `bitpack`
src/codegen/x86_64/CodeGen.zig+2-2
...@@ -176185,8 +176185,8 @@ fn genCall(self: *CodeGen, info: union(enum) {...@@ -176185,8 +176185,8 @@ fn genCall(self: *CodeGen, info: union(enum) {
176185 // Due to incremental compilation, how function calls are generated depends176185 // Due to incremental compilation, how function calls are generated depends
176186 // on linking.176186 // on linking.
176187 switch (info) {176187 switch (info) {
176188 .air => |callee| if (try self.air.value(callee, pt)) |func_value| {176188 .air => |callee| if (callee.toInterned()) |func_ip_index| {
176189 const func_key = ip.indexToKey(func_value.ip_index);176189 const func_key = ip.indexToKey(func_ip_index);
176190 switch (switch (func_key) {176190 switch (switch (func_key) {
176191 else => func_key,176191 else => func_key,
176192 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {176192 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {