authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 05:38:32+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-26 13:48:07+00:00
loga61def10c66abc871f92c84d9cef85b6b7752cbf
tree6d8dfdbad1cbaf827d52ec61c33fff0d6ddae86d
parentb8d114a29e341a0cbd4c22cf02cb1588b0154446
signaturelock-open Commit is signed but in an unrecognized format.

compiler: eliminate most usages of TypedValue


24 files changed, 307 insertions(+), 487 deletions(-)

src/Module.zig+16-15
......@@ -3678,20 +3678,21 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
36783678 const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
36793679 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
36803680 const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
3681 const decl_tv = try sema.resolveFinalDeclValue(&block_scope, init_src, result_ref);
3681 const decl_val = try sema.resolveFinalDeclValue(&block_scope, init_src, result_ref);
3682 const decl_ty = decl_val.typeOf(mod);
36823683
36833684 // Note this resolves the type of the Decl, not the value; if this Decl
36843685 // is a struct, for example, this resolves `type` (which needs no resolution),
36853686 // not the struct itself.
3686 try sema.resolveTypeLayout(decl_tv.ty);
3687 try sema.resolveTypeLayout(decl_ty);
36873688
36883689 if (decl.kind == .@"usingnamespace") {
3689 if (!decl_tv.ty.eql(Type.type, mod)) {
3690 if (!decl_ty.eql(Type.type, mod)) {
36903691 return sema.fail(&block_scope, ty_src, "expected type, found {}", .{
3691 decl_tv.ty.fmt(mod),
3692 decl_ty.fmt(mod),
36923693 });
36933694 }
3694 const ty = decl_tv.val.toType();
3695 const ty = decl_val.toType();
36953696 if (ty.getNamespace(mod) == null) {
36963697 return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(mod)});
36973698 }
......@@ -3713,10 +3714,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37133714 var queue_linker_work = true;
37143715 var is_func = false;
37153716 var is_inline = false;
3716 switch (decl_tv.val.toIntern()) {
3717 switch (decl_val.toIntern()) {
37173718 .generic_poison => unreachable,
37183719 .unreachable_value => unreachable,
3719 else => switch (ip.indexToKey(decl_tv.val.toIntern())) {
3720 else => switch (ip.indexToKey(decl_val.toIntern())) {
37203721 .variable => |variable| {
37213722 decl.owns_tv = variable.decl == decl_index;
37223723 queue_linker_work = decl.owns_tv;
......@@ -3731,7 +3732,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37313732 .func => |func| {
37323733 decl.owns_tv = func.owner_decl == decl_index;
37333734 queue_linker_work = false;
3734 is_inline = decl.owns_tv and decl_tv.ty.fnCallingConvention(mod) == .Inline;
3735 is_inline = decl.owns_tv and decl_ty.fnCallingConvention(mod) == .Inline;
37353736 is_func = decl.owns_tv;
37363737 },
37373738
......@@ -3739,7 +3740,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37393740 },
37403741 }
37413742
3742 decl.val = decl_tv.val;
3743 decl.val = decl_val;
37433744 // Function linksection, align, and addrspace were already set by Sema
37443745 if (!is_func) {
37453746 decl.alignment = blk: {
......@@ -3762,7 +3763,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37623763 break :blk section.toOptional();
37633764 };
37643765 decl.@"addrspace" = blk: {
3765 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_tv.val.toIntern())) {
3766 const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_val.toIntern())) {
37663767 .variable => .variable,
37673768 .extern_func, .func => .function,
37683769 else => .constant,
......@@ -3784,10 +3785,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37843785 decl.analysis = .complete;
37853786
37863787 const result: SemaDeclResult = if (old_has_tv) .{
3787 .invalidate_decl_val = !decl_tv.ty.eql(old_ty, mod) or
3788 !decl.val.eql(old_val, decl_tv.ty, mod) or
3788 .invalidate_decl_val = !decl_ty.eql(old_ty, mod) or
3789 !decl.val.eql(old_val, decl_ty, mod) or
37893790 is_inline != old_is_inline,
3790 .invalidate_decl_ref = !decl_tv.ty.eql(old_ty, mod) or
3791 .invalidate_decl_ref = !decl_ty.eql(old_ty, mod) or
37913792 decl.alignment != old_align or
37923793 decl.@"linksection" != old_linksection or
37933794 decl.@"addrspace" != old_addrspace or
......@@ -3797,11 +3798,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
37973798 .invalidate_decl_ref = true,
37983799 };
37993800
3800 const has_runtime_bits = queue_linker_work and (is_func or try sema.typeHasRuntimeBits(decl_tv.ty));
3801 const has_runtime_bits = queue_linker_work and (is_func or try sema.typeHasRuntimeBits(decl_ty));
38013802 if (has_runtime_bits) {
38023803 // Needed for codegen_decl which will call updateDecl and then the
38033804 // codegen backend wants full access to the Decl Type.
3804 try sema.resolveTypeFully(decl_tv.ty);
3805 try sema.resolveTypeFully(decl_ty);
38053806
38063807 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
38073808
src/Sema.zig+10-19
......@@ -175,7 +175,6 @@ const Sema = @This();
175175const Value = @import("Value.zig");
176176const MutableValue = @import("mutable_value.zig").MutableValue;
177177const Type = @import("type.zig").Type;
178const TypedValue = @import("TypedValue.zig");
179178const Air = @import("Air.zig");
180179const Zir = std.zig.Zir;
181180const Module = @import("Module.zig");
......@@ -1708,7 +1707,7 @@ fn analyzeBodyInner(
17081707 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
17091708 .block_comptime_reason = block.comptime_reason,
17101709 });
1711 const inline_body = if (cond.val.toBool()) then_body else else_body;
1710 const inline_body = if (cond.toBool()) then_body else else_body;
17121711
17131712 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
17141713
......@@ -1728,7 +1727,7 @@ fn analyzeBodyInner(
17281727 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
17291728 .block_comptime_reason = block.comptime_reason,
17301729 });
1731 const inline_body = if (cond.val.toBool()) then_body else else_body;
1730 const inline_body = if (cond.toBool()) then_body else else_body;
17321731
17331732 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
17341733 const old_runtime_index = block.runtime_index;
......@@ -2179,13 +2178,9 @@ fn resolveInstConst(
21792178 src: LazySrcLoc,
21802179 zir_ref: Zir.Inst.Ref,
21812180 reason: NeededComptimeReason,
2182) CompileError!TypedValue {
2181) CompileError!Value {
21832182 const air_ref = try sema.resolveInst(zir_ref);
2184 const val = try sema.resolveConstDefinedValue(block, src, air_ref, reason);
2185 return .{
2186 .ty = sema.typeOf(air_ref),
2187 .val = val,
2188 };
2183 return sema.resolveConstDefinedValue(block, src, air_ref, reason);
21892184}
21902185
21912186/// Value Tag may be `undef` or `variable`.
......@@ -2194,7 +2189,7 @@ pub fn resolveFinalDeclValue(
21942189 block: *Block,
21952190 src: LazySrcLoc,
21962191 air_ref: Air.Inst.Ref,
2197) CompileError!TypedValue {
2192) CompileError!Value {
21982193 const val = try sema.resolveValueAllowVariables(air_ref) orelse {
21992194 return sema.failWithNeededComptime(block, src, .{
22002195 .needed_comptime_reason = "global variable initializer must be comptime-known",
......@@ -2204,10 +2199,7 @@ pub fn resolveFinalDeclValue(
22042199 if (val.canMutateComptimeVarState(sema.mod)) {
22052200 return sema.fail(block, src, "global variable contains reference to comptime var", .{});
22062201 }
2207 return .{
2208 .ty = sema.typeOf(air_ref),
2209 .val = val,
2210 };
2202 return val;
22112203}
22122204
22132205fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError {
......@@ -6414,7 +6406,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
64146406 const options = try sema.resolveExportOptions(block, options_src, extra.options);
64156407 if (options.linkage == .internal)
64166408 return;
6417 if (operand.val.getFunction(mod)) |function| {
6409 if (operand.getFunction(mod)) |function| {
64186410 const decl_index = function.owner_decl;
64196411 return sema.analyzeExport(block, src, options, decl_index);
64206412 }
......@@ -6424,7 +6416,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
64246416 .src = src,
64256417 .owner_decl = sema.owner_decl_index,
64266418 .src_decl = block.src_decl,
6427 .exported = .{ .value = operand.val.toIntern() },
6419 .exported = .{ .value = operand.toIntern() },
64286420 .status = .in_progress,
64296421 });
64306422}
......@@ -25831,7 +25823,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2583125823 } else if (extra.data.bits.has_ret_ty_ref) blk: {
2583225824 const ret_ty_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
2583325825 extra_index += 1;
25834 const ret_ty_tv = sema.resolveInstConst(block, ret_src, ret_ty_ref, .{
25826 const ret_ty_val = sema.resolveInstConst(block, ret_src, ret_ty_ref, .{
2583525827 .needed_comptime_reason = "return type must be comptime-known",
2583625828 }) catch |err| switch (err) {
2583725829 error.GenericPoison => {
......@@ -25839,8 +25831,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
2583925831 },
2584025832 else => |e| return e,
2584125833 };
25842 const ty = ret_ty_tv.val.toType();
25843 break :blk ty;
25834 break :blk ret_ty_val.toType();
2584425835 } else Type.void;
2584525836
2584625837 const noalias_bits: u32 = if (extra.data.bits.has_any_noalias) blk: {
src/TypedValue.zig+3-37
......@@ -1,3 +1,6 @@
1//! This type exists only for legacy purposes, and will be removed in the future.
2//! It is a thin wrapper around a `Value` which also, redundantly, stores its `Type`.
3
14const std = @import("std");
25const Type = @import("type.zig").Type;
36const Value = @import("Value.zig");
......@@ -12,42 +15,6 @@ const Target = std.Target;
1215ty: Type,
1316val: Value,
1417
15/// Memory management for TypedValue. The main purpose of this type
16/// is to be small and have a deinit() function to free associated resources.
17pub const Managed = struct {
18 /// If the tag value is less than Tag.no_payload_count, then no pointer
19 /// dereference is needed.
20 typed_value: TypedValue,
21 /// If this is `null` then there is no memory management needed.
22 arena: ?*std.heap.ArenaAllocator.State = null,
23
24 pub fn deinit(self: *Managed, allocator: Allocator) void {
25 if (self.arena) |a| a.promote(allocator).deinit();
26 self.* = undefined;
27 }
28};
29
30/// Assumes arena allocation. Does a recursive copy.
31pub fn copy(self: TypedValue, arena: Allocator) error{OutOfMemory}!TypedValue {
32 return TypedValue{
33 .ty = self.ty,
34 .val = try self.val.copy(arena),
35 };
36}
37
38pub fn eql(a: TypedValue, b: TypedValue, mod: *Module) bool {
39 if (a.ty.toIntern() != b.ty.toIntern()) return false;
40 return a.val.eql(b.val, a.ty, mod);
41}
42
43pub fn hash(tv: TypedValue, hasher: *std.hash.Wyhash, mod: *Module) void {
44 return tv.val.hash(tv.ty, hasher, mod);
45}
46
47pub fn intFromEnum(tv: TypedValue, mod: *Module) Allocator.Error!Value {
48 return tv.val.intFromEnum(tv.ty, mod);
49}
50
5118const max_aggregate_items = 100;
5219const max_string_len = 256;
5320
......@@ -72,7 +39,6 @@ pub fn format(
7239 };
7340}
7441
75/// Prints the Value according to the Type, not according to the Value Tag.
7642pub fn print(
7743 tv: TypedValue,
7844 writer: anytype,
src/Value.zig+3-3
......@@ -249,12 +249,12 @@ pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, opt_sema: ?*Sema) !?u64
249249 .int => |int| Value.fromInterned(int).getUnsignedIntAdvanced(mod, opt_sema),
250250 .elem => |elem| {
251251 const base_addr = (try Value.fromInterned(elem.base).getUnsignedIntAdvanced(mod, opt_sema)) orelse return null;
252 const elem_ty = Type.fromInterned(mod.intern_pool.typeOf(elem.base)).elemType2(mod);
252 const elem_ty = Value.fromInterned(elem.base).typeOf(mod).elemType2(mod);
253253 return base_addr + elem.index * elem_ty.abiSize(mod);
254254 },
255255 .field => |field| {
256256 const base_addr = (try Value.fromInterned(field.base).getUnsignedIntAdvanced(mod, opt_sema)) orelse return null;
257 const struct_ty = Type.fromInterned(mod.intern_pool.typeOf(field.base)).childType(mod);
257 const struct_ty = Value.fromInterned(field.base).typeOf(mod).childType(mod);
258258 if (opt_sema) |sema| try sema.resolveTypeLayout(struct_ty);
259259 return base_addr + struct_ty.structFieldOffset(@as(usize, @intCast(field.index)), mod);
260260 },
......@@ -1390,7 +1390,7 @@ pub fn elemPtr(
13901390 };
13911391 switch (mod.intern_pool.indexToKey(ptr_val.toIntern())) {
13921392 .ptr => |ptr| switch (ptr.addr) {
1393 .elem => |elem| if (Type.fromInterned(mod.intern_pool.typeOf(elem.base)).elemType2(mod).eql(elem_ty, mod))
1393 .elem => |elem| if (Value.fromInterned(elem.base).typeOf(mod).elemType2(mod).eql(elem_ty, mod))
13941394 return Value.fromInterned((try mod.intern(.{ .ptr = .{
13951395 .ty = elem_ptr_ty.toIntern(),
13961396 .addr = .{ .elem = .{
src/arch/aarch64/CodeGen.zig+3-7
......@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");
1010const Liveness = @import("../../Liveness.zig");
1111const Type = @import("../../type.zig").Type;
1212const Value = @import("../../Value.zig");
13const TypedValue = @import("../../TypedValue.zig");
1413const link = @import("../../link.zig");
1514const Module = @import("../../Module.zig");
1615const InternPool = @import("../../InternPool.zig");
......@@ -6143,10 +6142,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
61436142 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))
61446143 return MCValue{ .none = {} };
61456144
6146 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{
6147 .ty = inst_ty,
6148 .val = (try self.air.value(inst, mod)).?,
6149 });
6145 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
61506146
61516147 return self.getResolvedInstValue(inst_index);
61526148}
......@@ -6163,11 +6159,11 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
61636159 }
61646160}
61656161
6166fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {
6162fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
61676163 const mcv: MCValue = switch (try codegen.genTypedValue(
61686164 self.bin_file,
61696165 self.src_loc,
6170 arg_tv,
6166 val,
61716167 self.owner_decl,
61726168 )) {
61736169 .mcv => |mcv| switch (mcv) {
src/arch/arm/CodeGen.zig+3-7
......@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");
1010const Liveness = @import("../../Liveness.zig");
1111const Type = @import("../../type.zig").Type;
1212const Value = @import("../../Value.zig");
13const TypedValue = @import("../../TypedValue.zig");
1413const link = @import("../../link.zig");
1514const Module = @import("../../Module.zig");
1615const InternPool = @import("../../InternPool.zig");
......@@ -6097,10 +6096,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
60976096 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))
60986097 return MCValue{ .none = {} };
60996098
6100 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{
6101 .ty = inst_ty,
6102 .val = (try self.air.value(inst, mod)).?,
6103 });
6099 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
61046100
61056101 return self.getResolvedInstValue(inst_index);
61066102}
......@@ -6117,12 +6113,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
61176113 }
61186114}
61196115
6120fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {
6116fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
61216117 const mod = self.bin_file.comp.module.?;
61226118 const mcv: MCValue = switch (try codegen.genTypedValue(
61236119 self.bin_file,
61246120 self.src_loc,
6125 arg_tv,
6121 val,
61266122 mod.funcOwnerDeclIndex(self.func_index),
61276123 )) {
61286124 .mcv => |mcv| switch (mcv) {
src/arch/riscv64/CodeGen.zig+3-7
......@@ -9,7 +9,6 @@ const Emit = @import("Emit.zig");
99const Liveness = @import("../../Liveness.zig");
1010const Type = @import("../../type.zig").Type;
1111const Value = @import("../../Value.zig");
12const TypedValue = @import("../../TypedValue.zig");
1312const link = @import("../../link.zig");
1413const Module = @import("../../Module.zig");
1514const InternPool = @import("../../InternPool.zig");
......@@ -2552,10 +2551,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
25522551 if (!inst_ty.hasRuntimeBits(mod))
25532552 return MCValue{ .none = {} };
25542553
2555 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{
2556 .ty = inst_ty,
2557 .val = (try self.air.value(inst, mod)).?,
2558 });
2554 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
25592555
25602556 return self.getResolvedInstValue(inst_index);
25612557}
......@@ -2572,12 +2568,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
25722568 }
25732569}
25742570
2575fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
2571fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
25762572 const mod = self.bin_file.comp.module.?;
25772573 const mcv: MCValue = switch (try codegen.genTypedValue(
25782574 self.bin_file,
25792575 self.src_loc,
2580 typed_value,
2576 val,
25812577 mod.funcOwnerDeclIndex(self.func_index),
25822578 )) {
25832579 .mcv => |mcv| switch (mcv) {
src/arch/sparc64/CodeGen.zig+4-7
......@@ -12,7 +12,7 @@ const builtin = @import("builtin");
1212const link = @import("../../link.zig");
1313const Module = @import("../../Module.zig");
1414const InternPool = @import("../../InternPool.zig");
15const TypedValue = @import("../../TypedValue.zig");
15const Value = @import("../../Value.zig");
1616const ErrorMsg = Module.ErrorMsg;
1717const codegen = @import("../../codegen.zig");
1818const Air = @import("../../Air.zig");
......@@ -4118,12 +4118,12 @@ fn genStoreASI(self: *Self, value_reg: Register, addr_reg: Register, off_reg: Re
41184118 }
41194119}
41204120
4121fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
4121fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
41224122 const mod = self.bin_file.comp.module.?;
41234123 const mcv: MCValue = switch (try codegen.genTypedValue(
41244124 self.bin_file,
41254125 self.src_loc,
4126 typed_value,
4126 val,
41274127 mod.funcOwnerDeclIndex(self.func_index),
41284128 )) {
41294129 .mcv => |mcv| switch (mcv) {
......@@ -4546,10 +4546,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
45464546 return self.getResolvedInstValue(inst);
45474547 }
45484548
4549 return self.genTypedValue(.{
4550 .ty = ty,
4551 .val = (try self.air.value(ref, mod)).?,
4552 });
4549 return self.genTypedValue((try self.air.value(ref, mod)).?);
45534550}
45544551
45554552fn ret(self: *Self, mcv: MCValue) !void {
src/arch/wasm/CodeGen.zig+14-18
......@@ -18,7 +18,6 @@ const Value = @import("../../Value.zig");
1818const Compilation = @import("../../Compilation.zig");
1919const LazySrcLoc = std.zig.LazySrcLoc;
2020const link = @import("../../link.zig");
21const TypedValue = @import("../../TypedValue.zig");
2221const Air = @import("../../Air.zig");
2322const Liveness = @import("../../Liveness.zig");
2423const target_util = @import("../../target.zig");
......@@ -805,7 +804,7 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
805804 // In the other cases, we will simply lower the constant to a value that fits
806805 // into a single local (such as a pointer, integer, bool, etc).
807806 const result = if (isByRef(ty, mod)) blk: {
808 const sym_index = try func.bin_file.lowerUnnamedConst(.{ .ty = ty, .val = val }, func.decl_index);
807 const sym_index = try func.bin_file.lowerUnnamedConst(val, func.decl_index);
809808 break :blk WValue{ .memory = sym_index };
810809 } else try func.lowerConstant(val, ty);
811810
......@@ -3119,10 +3118,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, offset: u32) InnerError!WValue
31193118}
31203119
31213120fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {
3122 const mod = func.bin_file.base.comp.module.?;
3123 const decl = mod.declPtr(decl_index);
3124 const ptr_ty = try mod.singleMutPtrType(decl.typeOf(mod));
3125 return func.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index, offset);
3121 return func.lowerDeclRefValue(ptr_val, decl_index, offset);
31263122}
31273123
31283124fn lowerAnonDeclRef(
......@@ -3157,7 +3153,7 @@ fn lowerAnonDeclRef(
31573153 } else return WValue{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } };
31583154}
31593155
3160fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {
3156fn lowerDeclRefValue(func: *CodeGen, val: Value, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {
31613157 const mod = func.bin_file.base.comp.module.?;
31623158
31633159 const decl = mod.declPtr(decl_index);
......@@ -3165,11 +3161,11 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.Decl
31653161 // want to lower the actual decl, rather than the alias itself.
31663162 if (decl.val.getFunction(mod)) |func_val| {
31673163 if (func_val.owner_decl != decl_index) {
3168 return func.lowerDeclRefValue(tv, func_val.owner_decl, offset);
3164 return func.lowerDeclRefValue(val, func_val.owner_decl, offset);
31693165 }
31703166 } else if (decl.val.getExternFunc(mod)) |func_val| {
31713167 if (func_val.decl != decl_index) {
3172 return func.lowerDeclRefValue(tv, func_val.decl, offset);
3168 return func.lowerDeclRefValue(val, func_val.decl, offset);
31733169 }
31743170 }
31753171 const decl_ty = decl.typeOf(mod);
......@@ -3280,23 +3276,23 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
32803276 },
32813277 .error_union => |error_union| {
32823278 const err_int_ty = try mod.errorIntType();
3283 const err_tv: TypedValue = switch (error_union.val) {
3279 const err_ty, const err_val = switch (error_union.val) {
32843280 .err_name => |err_name| .{
3285 .ty = ty.errorUnionSet(mod),
3286 .val = Value.fromInterned((try mod.intern(.{ .err = .{
3281 ty.errorUnionSet(mod),
3282 Value.fromInterned((try mod.intern(.{ .err = .{
32873283 .ty = ty.errorUnionSet(mod).toIntern(),
32883284 .name = err_name,
32893285 } }))),
32903286 },
32913287 .payload => .{
3292 .ty = err_int_ty,
3293 .val = try mod.intValue(err_int_ty, 0),
3288 err_int_ty,
3289 try mod.intValue(err_int_ty, 0),
32943290 },
32953291 };
32963292 const payload_type = ty.errorUnionPayload(mod);
32973293 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
32983294 // We use the error type directly as the type.
3299 return func.lowerConstant(err_tv.val, err_tv.ty);
3295 return func.lowerConstant(err_val, err_ty);
33003296 }
33013297
33023298 return func.fail("Wasm TODO: lowerConstant error union with non-zero-bit payload type", .{});
......@@ -3320,10 +3316,10 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
33203316 .elem, .field => |base_index| ptr = ip.indexToKey(base_index.base).ptr,
33213317 .comptime_field, .comptime_alloc => unreachable,
33223318 };
3323 return .{ .memory = try func.bin_file.lowerUnnamedConst(.{ .ty = ty, .val = val }, owner_decl) };
3319 return .{ .memory = try func.bin_file.lowerUnnamedConst(val, owner_decl) };
33243320 },
33253321 .ptr => |ptr| switch (ptr.addr) {
3326 .decl => |decl| return func.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl, 0),
3322 .decl => |decl| return func.lowerDeclRefValue(val, decl, 0),
33273323 .int => |int| return func.lowerConstant(Value.fromInterned(int), Type.fromInterned(ip.typeOf(int))),
33283324 .opt_payload, .elem, .field => return func.lowerParentPtr(val, 0),
33293325 .anon_decl => |ad| return func.lowerAnonDeclRef(ad, 0),
......@@ -7285,7 +7281,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 {
72857281 .storage = .{ .bytes = tag_name },
72867282 } });
72877283 const tag_sym_index = try func.bin_file.lowerUnnamedConst(
7288 .{ .ty = name_ty, .val = Value.fromInterned(name_val) },
7284 Value.fromInterned(name_val),
72897285 enum_decl_index,
72907286 );
72917287
src/arch/x86_64/CodeGen.zig+38-63
......@@ -32,7 +32,6 @@ const InternPool = @import("../../InternPool.zig");
3232const Alignment = InternPool.Alignment;
3333const Target = std.Target;
3434const Type = @import("../../type.zig").Type;
35const TypedValue = @import("../../TypedValue.zig");
3635const Value = @import("../../Value.zig");
3736const Instruction = @import("encoder.zig").Instruction;
3837
......@@ -2250,7 +2249,7 @@ fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void {
22502249 for (exitlude_jump_relocs, 0..) |*exitlude_jump_reloc, tag_index| {
22512250 const tag_name_len = ip.stringToSlice(tag_names.get(ip)[tag_index]).len;
22522251 const tag_val = try mod.enumValueFieldIndex(enum_ty, @intCast(tag_index));
2253 const tag_mcv = try self.genTypedValue(.{ .ty = enum_ty, .val = tag_val });
2252 const tag_mcv = try self.genTypedValue(tag_val);
22542253 try self.genBinOpMir(.{ ._, .cmp }, enum_ty, enum_mcv, tag_mcv);
22552254 const skip_reloc = try self.asmJccReloc(.ne, undefined);
22562255
......@@ -3323,7 +3322,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
33233322 .storage = .{ .repeated_elem = mask_val.ip_index },
33243323 } });
33253324
3326 const splat_mcv = try self.genTypedValue(.{ .ty = splat_ty, .val = Value.fromInterned(splat_val) });
3325 const splat_mcv = try self.genTypedValue(Value.fromInterned(splat_val));
33273326 const splat_addr_mcv: MCValue = switch (splat_mcv) {
33283327 .memory, .indirect, .load_frame => splat_mcv.address(),
33293328 else => .{ .register = try self.copyToTmpRegister(Type.usize, splat_mcv.address()) },
......@@ -4992,17 +4991,14 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void {
49924991 defer self.register_manager.unlockReg(shift_lock);
49934992
49944993 const mask_ty = try mod.vectorType(.{ .len = 16, .child = .u8_type });
4995 const mask_mcv = try self.genTypedValue(.{
4996 .ty = mask_ty,
4997 .val = Value.fromInterned((try mod.intern(.{ .aggregate = .{
4998 .ty = mask_ty.toIntern(),
4999 .storage = .{ .elems = &([1]InternPool.Index{
5000 (try rhs_ty.childType(mod).maxIntScalar(mod, Type.u8)).toIntern(),
5001 } ++ [1]InternPool.Index{
5002 (try mod.intValue(Type.u8, 0)).toIntern(),
5003 } ** 15) },
5004 } }))),
5005 });
4994 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
4995 .ty = mask_ty.toIntern(),
4996 .storage = .{ .elems = &([1]InternPool.Index{
4997 (try rhs_ty.childType(mod).maxIntScalar(mod, Type.u8)).toIntern(),
4998 } ++ [1]InternPool.Index{
4999 (try mod.intValue(Type.u8, 0)).toIntern(),
5000 } ** 15) },
5001 } })));
50065002 const mask_addr_reg =
50075003 try self.copyToTmpRegister(Type.usize, mask_mcv.address());
50085004 const mask_addr_lock = self.register_manager.lockRegAssumeUnused(mask_addr_reg);
......@@ -6860,11 +6856,11 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type)
68606856 .child = (try mod.intType(.signed, scalar_bits)).ip_index,
68616857 });
68626858
6863 const sign_mcv = try self.genTypedValue(.{ .ty = vec_ty, .val = switch (tag) {
6859 const sign_mcv = try self.genTypedValue(switch (tag) {
68646860 .neg => try vec_ty.minInt(mod, vec_ty),
68656861 .abs => try vec_ty.maxInt(mod, vec_ty),
68666862 else => unreachable,
6867 } });
6863 });
68686864 const sign_mem: Memory = if (sign_mcv.isMemory())
68696865 try sign_mcv.mem(self, Memory.Size.fromSize(abi_size))
68706866 else
......@@ -11130,10 +11126,7 @@ fn genBinOp(
1113011126 .cmp_neq,
1113111127 => {
1113211128 const unsigned_ty = try lhs_ty.toUnsigned(mod);
11133 const not_mcv = try self.genTypedValue(.{
11134 .ty = lhs_ty,
11135 .val = try unsigned_ty.maxInt(mod, unsigned_ty),
11136 });
11129 const not_mcv = try self.genTypedValue(try unsigned_ty.maxInt(mod, unsigned_ty));
1113711130 const not_mem: Memory = if (not_mcv.isMemory())
1113811131 try not_mcv.mem(self, Memory.Size.fromSize(abi_size))
1113911132 else
......@@ -14692,10 +14685,7 @@ fn genSetReg(
1469214685 ),
1469314686 else => unreachable,
1469414687 },
14695 .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(.{
14696 .ty = ty,
14697 .val = try mod.undefValue(ty),
14698 }), opts),
14688 .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(try mod.undefValue(ty)), opts),
1469914689 },
1470014690 .eflags => |cc| try self.asmSetccRegister(cc, dst_reg.to8()),
1470114691 .immediate => |imm| {
......@@ -16893,13 +16883,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {
1689316883 .ty = mask_elem_ty.toIntern(),
1689416884 .storage = .{ .u64 = bit / elem_bits },
1689516885 } });
16896 const mask_mcv = try self.genTypedValue(.{
16897 .ty = mask_ty,
16898 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{
16899 .ty = mask_ty.toIntern(),
16900 .storage = .{ .elems = mask_elems[0..vec_len] },
16901 } })),
16902 });
16886 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
16887 .ty = mask_ty.toIntern(),
16888 .storage = .{ .elems = mask_elems[0..vec_len] },
16889 } })));
1690316890 const mask_mem: Memory = .{
1690416891 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },
1690516892 .mod = .{ .rm = .{ .size = self.memSize(ty) } },
......@@ -16921,13 +16908,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {
1692116908 .ty = mask_elem_ty.toIntern(),
1692216909 .storage = .{ .u64 = @as(u32, 1) << @intCast(bit & (elem_bits - 1)) },
1692316910 } });
16924 const mask_mcv = try self.genTypedValue(.{
16925 .ty = mask_ty,
16926 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{
16927 .ty = mask_ty.toIntern(),
16928 .storage = .{ .elems = mask_elems[0..vec_len] },
16929 } })),
16930 });
16911 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
16912 .ty = mask_ty.toIntern(),
16913 .storage = .{ .elems = mask_elems[0..vec_len] },
16914 } })));
1693116915 const mask_mem: Memory = .{
1693216916 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },
1693316917 .mod = .{ .rm = .{ .size = self.memSize(ty) } },
......@@ -17658,13 +17642,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
1765817642 else
1765917643 try select_mask_elem_ty.minIntScalar(mod, select_mask_elem_ty)).toIntern();
1766017644 }
17661 const select_mask_mcv = try self.genTypedValue(.{
17662 .ty = select_mask_ty,
17663 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{
17664 .ty = select_mask_ty.toIntern(),
17665 .storage = .{ .elems = select_mask_elems[0..mask_elems.len] },
17666 } })),
17667 });
17645 const select_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17646 .ty = select_mask_ty.toIntern(),
17647 .storage = .{ .elems = select_mask_elems[0..mask_elems.len] },
17648 } })));
1766817649
1766917650 if (self.hasFeature(.sse4_1)) {
1767017651 const mir_tag: Mir.Inst.FixedTag = .{
......@@ -17809,13 +17790,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
1780917790 } });
1781017791 }
1781117792 const lhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });
17812 const lhs_mask_mcv = try self.genTypedValue(.{
17813 .ty = lhs_mask_ty,
17814 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{
17815 .ty = lhs_mask_ty.toIntern(),
17816 .storage = .{ .elems = lhs_mask_elems[0..max_abi_size] },
17817 } })),
17818 });
17793 const lhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17794 .ty = lhs_mask_ty.toIntern(),
17795 .storage = .{ .elems = lhs_mask_elems[0..max_abi_size] },
17796 } })));
1781917797 const lhs_mask_mem: Memory = .{
1782017798 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, lhs_mask_mcv.address()) },
1782117799 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },
......@@ -17846,13 +17824,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
1784617824 } });
1784717825 }
1784817826 const rhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });
17849 const rhs_mask_mcv = try self.genTypedValue(.{
17850 .ty = rhs_mask_ty,
17851 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{
17852 .ty = rhs_mask_ty.toIntern(),
17853 .storage = .{ .elems = rhs_mask_elems[0..max_abi_size] },
17854 } })),
17855 });
17827 const rhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17828 .ty = rhs_mask_ty.toIntern(),
17829 .storage = .{ .elems = rhs_mask_elems[0..max_abi_size] },
17830 } })));
1785617831 const rhs_mask_mem: Memory = .{
1785717832 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, rhs_mask_mcv.address()) },
1785817833 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },
......@@ -18138,7 +18113,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
1813818113 .{ .frame = frame_index },
1813918114 @intCast(elem_size * elements.len),
1814018115 elem_ty,
18141 try self.genTypedValue(.{ .ty = elem_ty, .val = sentinel }),
18116 try self.genTypedValue(sentinel),
1814218117 .{},
1814318118 );
1814418119 break :result .{ .load_frame = .{ .index = frame_index } };
......@@ -18662,7 +18637,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
1866218637 const ip_index = ref.toInterned().?;
1866318638 const gop = try self.const_tracking.getOrPut(self.gpa, ip_index);
1866418639 if (!gop.found_existing) gop.value_ptr.* = InstTracking.init(init: {
18665 const const_mcv = try self.genTypedValue(.{ .ty = ty, .val = Value.fromInterned(ip_index) });
18640 const const_mcv = try self.genTypedValue(Value.fromInterned(ip_index));
1866618641 switch (const_mcv) {
1866718642 .lea_tlv => |tlv_sym| switch (self.bin_file.tag) {
1866818643 .elf, .macho => {
......@@ -18727,9 +18702,9 @@ fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCV
1872718702 return mcv;
1872818703}
1872918704
18730fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {
18705fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
1873118706 const mod = self.bin_file.comp.module.?;
18732 return switch (try codegen.genTypedValue(self.bin_file, self.src_loc, arg_tv, self.owner.getDecl(mod))) {
18707 return switch (try codegen.genTypedValue(self.bin_file, self.src_loc, val, self.owner.getDecl(mod))) {
1873318708 .mcv => |mcv| switch (mcv) {
1873418709 .none => .none,
1873518710 .undef => .undef,
src/codegen.zig+120-168
......@@ -19,7 +19,6 @@ const Liveness = @import("Liveness.zig");
1919const Module = @import("Module.zig");
2020const Target = std.Target;
2121const Type = @import("type.zig").Type;
22const TypedValue = @import("TypedValue.zig");
2322const Value = @import("Value.zig");
2423const Zir = std.zig.Zir;
2524const Alignment = InternPool.Alignment;
......@@ -171,7 +170,7 @@ pub fn generateLazySymbol(
171170pub fn generateSymbol(
172171 bin_file: *link.File,
173172 src_loc: Module.SrcLoc,
174 arg_tv: TypedValue,
173 val: Value,
175174 code: *std.ArrayList(u8),
176175 debug_output: DebugInfoOutput,
177176 reloc_info: RelocInfo,
......@@ -181,23 +180,22 @@ pub fn generateSymbol(
181180
182181 const mod = bin_file.comp.module.?;
183182 const ip = &mod.intern_pool;
184 const typed_value = arg_tv;
183 const ty = val.typeOf(mod);
185184
186185 const target = mod.getTarget();
187186 const endian = target.cpu.arch.endian();
188187
189 log.debug("generateSymbol: ty = {}, val = {}", .{
190 typed_value.ty.fmt(mod),
191 typed_value.val.fmtValue(typed_value.ty, mod),
188 log.debug("generateSymbol: val = {}", .{
189 val.fmtValue(ty, mod),
192190 });
193191
194 if (typed_value.val.isUndefDeep(mod)) {
195 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;
192 if (val.isUndefDeep(mod)) {
193 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
196194 try code.appendNTimes(0xaa, abi_size);
197195 return .ok;
198196 }
199197
200 switch (ip.indexToKey(typed_value.val.toIntern())) {
198 switch (ip.indexToKey(val.toIntern())) {
201199 .int_type,
202200 .ptr_type,
203201 .array_type,
......@@ -238,17 +236,17 @@ pub fn generateSymbol(
238236 .empty_enum_value,
239237 => unreachable, // non-runtime values
240238 .int => {
241 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;
239 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
242240 var space: Value.BigIntSpace = undefined;
243 const val = typed_value.val.toBigInt(&space, mod);
244 val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian);
241 const int_val = val.toBigInt(&space, mod);
242 int_val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian);
245243 },
246244 .err => |err| {
247245 const int = try mod.getErrorValue(err.name);
248246 try code.writer().writeInt(u16, @as(u16, @intCast(int)), endian);
249247 },
250248 .error_union => |error_union| {
251 const payload_ty = typed_value.ty.errorUnionPayload(mod);
249 const payload_ty = ty.errorUnionPayload(mod);
252250 const err_val = switch (error_union.val) {
253251 .err_name => |err_name| @as(u16, @intCast(try mod.getErrorValue(err_name))),
254252 .payload => @as(u16, 0),
......@@ -261,7 +259,7 @@ pub fn generateSymbol(
261259
262260 const payload_align = payload_ty.abiAlignment(mod);
263261 const error_align = Type.anyerror.abiAlignment(mod);
264 const abi_align = typed_value.ty.abiAlignment(mod);
262 const abi_align = ty.abiAlignment(mod);
265263
266264 // error value first when its type is larger than the error union's payload
267265 if (error_align.order(payload_align) == .gt) {
......@@ -271,13 +269,10 @@ pub fn generateSymbol(
271269 // emit payload part of the error union
272270 {
273271 const begin = code.items.len;
274 switch (try generateSymbol(bin_file, src_loc, .{
275 .ty = payload_ty,
276 .val = Value.fromInterned(switch (error_union.val) {
277 .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }),
278 .payload => |payload| payload,
279 }),
280 }, code, debug_output, reloc_info)) {
272 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (error_union.val) {
273 .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }),
274 .payload => |payload| payload,
275 }), code, debug_output, reloc_info)) {
281276 .ok => {},
282277 .fail => |em| return .{ .fail = em },
283278 }
......@@ -304,11 +299,8 @@ pub fn generateSymbol(
304299 }
305300 },
306301 .enum_tag => |enum_tag| {
307 const int_tag_ty = typed_value.ty.intTagType(mod);
308 switch (try generateSymbol(bin_file, src_loc, .{
309 .ty = int_tag_ty,
310 .val = try mod.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty),
311 }, code, debug_output, reloc_info)) {
302 const int_tag_ty = ty.intTagType(mod);
303 switch (try generateSymbol(bin_file, src_loc, try mod.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, debug_output, reloc_info)) {
312304 .ok => {},
313305 .fail => |em| return .{ .fail = em },
314306 }
......@@ -319,42 +311,33 @@ pub fn generateSymbol(
319311 .f64 => |f64_val| writeFloat(f64, f64_val, target, endian, try code.addManyAsArray(8)),
320312 .f80 => |f80_val| {
321313 writeFloat(f80, f80_val, target, endian, try code.addManyAsArray(10));
322 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;
314 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
323315 try code.appendNTimes(0, abi_size - 10);
324316 },
325317 .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)),
326318 },
327 .ptr => switch (try lowerParentPtr(bin_file, src_loc, typed_value.val.toIntern(), code, debug_output, reloc_info)) {
319 .ptr => switch (try lowerParentPtr(bin_file, src_loc, val.toIntern(), code, debug_output, reloc_info)) {
328320 .ok => {},
329321 .fail => |em| return .{ .fail = em },
330322 },
331323 .slice => |slice| {
332 switch (try generateSymbol(bin_file, src_loc, .{
333 .ty = typed_value.ty.slicePtrFieldType(mod),
334 .val = Value.fromInterned(slice.ptr),
335 }, code, debug_output, reloc_info)) {
324 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.ptr), code, debug_output, reloc_info)) {
336325 .ok => {},
337326 .fail => |em| return .{ .fail = em },
338327 }
339 switch (try generateSymbol(bin_file, src_loc, .{
340 .ty = Type.usize,
341 .val = Value.fromInterned(slice.len),
342 }, code, debug_output, reloc_info)) {
328 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.len), code, debug_output, reloc_info)) {
343329 .ok => {},
344330 .fail => |em| return .{ .fail = em },
345331 }
346332 },
347333 .opt => {
348 const payload_type = typed_value.ty.optionalChild(mod);
349 const payload_val = typed_value.val.optionalValue(mod);
350 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;
334 const payload_type = ty.optionalChild(mod);
335 const payload_val = val.optionalValue(mod);
336 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
351337
352 if (typed_value.ty.optionalReprIsPayload(mod)) {
338 if (ty.optionalReprIsPayload(mod)) {
353339 if (payload_val) |value| {
354 switch (try generateSymbol(bin_file, src_loc, .{
355 .ty = payload_type,
356 .val = value,
357 }, code, debug_output, reloc_info)) {
340 switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) {
358341 .ok => {},
359342 .fail => |em| return Result{ .fail = em },
360343 }
......@@ -365,10 +348,7 @@ pub fn generateSymbol(
365348 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;
366349 if (payload_type.hasRuntimeBits(mod)) {
367350 const value = payload_val orelse Value.fromInterned((try mod.intern(.{ .undef = payload_type.toIntern() })));
368 switch (try generateSymbol(bin_file, src_loc, .{
369 .ty = payload_type,
370 .val = value,
371 }, code, debug_output, reloc_info)) {
351 switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) {
372352 .ok => {},
373353 .fail => |em| return Result{ .fail = em },
374354 }
......@@ -377,7 +357,7 @@ pub fn generateSymbol(
377357 try code.appendNTimes(0, padding);
378358 }
379359 },
380 .aggregate => |aggregate| switch (ip.indexToKey(typed_value.ty.toIntern())) {
360 .aggregate => |aggregate| switch (ip.indexToKey(ty.toIntern())) {
381361 .array_type => |array_type| switch (aggregate.storage) {
382362 .bytes => |bytes| try code.appendSlice(bytes),
383363 .elems, .repeated_elem => {
......@@ -385,17 +365,14 @@ pub fn generateSymbol(
385365 const len_including_sentinel =
386366 array_type.len + @intFromBool(array_type.sentinel != .none);
387367 while (index < len_including_sentinel) : (index += 1) {
388 switch (try generateSymbol(bin_file, src_loc, .{
389 .ty = Type.fromInterned(array_type.child),
390 .val = Value.fromInterned(switch (aggregate.storage) {
391 .bytes => unreachable,
392 .elems => |elems| elems[@as(usize, @intCast(index))],
393 .repeated_elem => |elem| if (index < array_type.len)
394 elem
395 else
396 array_type.sentinel,
397 }),
398 }, code, debug_output, reloc_info)) {
368 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) {
369 .bytes => unreachable,
370 .elems => |elems| elems[@as(usize, @intCast(index))],
371 .repeated_elem => |elem| if (index < array_type.len)
372 elem
373 else
374 array_type.sentinel,
375 }), code, debug_output, reloc_info)) {
399376 .ok => {},
400377 .fail => |em| return .{ .fail = em },
401378 }
......@@ -403,7 +380,7 @@ pub fn generateSymbol(
403380 },
404381 },
405382 .vector_type => |vector_type| {
406 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse
383 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse
407384 return error.Overflow;
408385 if (vector_type.child == .bool_type) {
409386 const bytes = try code.addManyAsSlice(abi_size);
......@@ -449,16 +426,13 @@ pub fn generateSymbol(
449426 .elems, .repeated_elem => {
450427 var index: u64 = 0;
451428 while (index < vector_type.len) : (index += 1) {
452 switch (try generateSymbol(bin_file, src_loc, .{
453 .ty = Type.fromInterned(vector_type.child),
454 .val = Value.fromInterned(switch (aggregate.storage) {
455 .bytes => unreachable,
456 .elems => |elems| elems[
457 math.cast(usize, index) orelse return error.Overflow
458 ],
459 .repeated_elem => |elem| elem,
460 }),
461 }, code, debug_output, reloc_info)) {
429 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) {
430 .bytes => unreachable,
431 .elems => |elems| elems[
432 math.cast(usize, index) orelse return error.Overflow
433 ],
434 .repeated_elem => |elem| elem,
435 }), code, debug_output, reloc_info)) {
462436 .ok => {},
463437 .fail => |em| return .{ .fail = em },
464438 }
......@@ -491,17 +465,14 @@ pub fn generateSymbol(
491465 .repeated_elem => |elem| elem,
492466 };
493467
494 switch (try generateSymbol(bin_file, src_loc, .{
495 .ty = Type.fromInterned(field_ty),
496 .val = Value.fromInterned(field_val),
497 }, code, debug_output, reloc_info)) {
468 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
498469 .ok => {},
499470 .fail => |em| return Result{ .fail = em },
500471 }
501472 const unpadded_field_end = code.items.len - struct_begin;
502473
503474 // Pad struct members if required
504 const padded_field_end = typed_value.ty.structFieldOffset(index + 1, mod);
475 const padded_field_end = ty.structFieldOffset(index + 1, mod);
505476 const padding = math.cast(usize, padded_field_end - unpadded_field_end) orelse
506477 return error.Overflow;
507478
......@@ -511,10 +482,10 @@ pub fn generateSymbol(
511482 }
512483 },
513484 .struct_type => {
514 const struct_type = ip.loadStructType(typed_value.ty.toIntern());
485 const struct_type = ip.loadStructType(ty.toIntern());
515486 switch (struct_type.layout) {
516487 .@"packed" => {
517 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse
488 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse
518489 return error.Overflow;
519490 const current_pos = code.items.len;
520491 try code.resize(current_pos + abi_size);
......@@ -537,10 +508,7 @@ pub fn generateSymbol(
537508 return error.Overflow;
538509 var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size);
539510 defer tmp_list.deinit();
540 switch (try generateSymbol(bin_file, src_loc, .{
541 .ty = Type.fromInterned(field_ty),
542 .val = Value.fromInterned(field_val),
543 }, &tmp_list, debug_output, reloc_info)) {
511 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), &tmp_list, debug_output, reloc_info)) {
544512 .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items),
545513 .fail => |em| return Result{ .fail = em },
546514 }
......@@ -560,7 +528,7 @@ pub fn generateSymbol(
560528 const field_ty = field_types[field_index];
561529 if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue;
562530
563 const field_val = switch (ip.indexToKey(typed_value.val.toIntern()).aggregate.storage) {
531 const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) {
564532 .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{
565533 .ty = field_ty,
566534 .storage = .{ .u64 = bytes[field_index] },
......@@ -575,10 +543,7 @@ pub fn generateSymbol(
575543 ) orelse return error.Overflow;
576544 if (padding > 0) try code.appendNTimes(0, padding);
577545
578 switch (try generateSymbol(bin_file, src_loc, .{
579 .ty = Type.fromInterned(field_ty),
580 .val = Value.fromInterned(field_val),
581 }, code, debug_output, reloc_info)) {
546 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
582547 .ok => {},
583548 .fail => |em| return Result{ .fail = em },
584549 }
......@@ -599,37 +564,28 @@ pub fn generateSymbol(
599564 else => unreachable,
600565 },
601566 .un => |un| {
602 const layout = typed_value.ty.unionGetLayout(mod);
567 const layout = ty.unionGetLayout(mod);
603568
604569 if (layout.payload_size == 0) {
605 return generateSymbol(bin_file, src_loc, .{
606 .ty = typed_value.ty.unionTagTypeSafety(mod).?,
607 .val = Value.fromInterned(un.tag),
608 }, code, debug_output, reloc_info);
570 return generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info);
609571 }
610572
611573 // Check if we should store the tag first.
612574 if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) {
613 switch (try generateSymbol(bin_file, src_loc, .{
614 .ty = typed_value.ty.unionTagTypeSafety(mod).?,
615 .val = Value.fromInterned(un.tag),
616 }, code, debug_output, reloc_info)) {
575 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
617576 .ok => {},
618577 .fail => |em| return Result{ .fail = em },
619578 }
620579 }
621580
622 const union_obj = mod.typeToUnion(typed_value.ty).?;
581 const union_obj = mod.typeToUnion(ty).?;
623582 if (un.tag != .none) {
624 const field_index = typed_value.ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?;
583 const field_index = ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?;
625584 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
626585 if (!field_ty.hasRuntimeBits(mod)) {
627586 try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);
628587 } else {
629 switch (try generateSymbol(bin_file, src_loc, .{
630 .ty = field_ty,
631 .val = Value.fromInterned(un.val),
632 }, code, debug_output, reloc_info)) {
588 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
633589 .ok => {},
634590 .fail => |em| return Result{ .fail = em },
635591 }
......@@ -640,20 +596,14 @@ pub fn generateSymbol(
640596 }
641597 }
642598 } else {
643 switch (try generateSymbol(bin_file, src_loc, .{
644 .ty = Type.fromInterned(ip.typeOf(un.val)),
645 .val = Value.fromInterned(un.val),
646 }, code, debug_output, reloc_info)) {
599 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
647600 .ok => {},
648601 .fail => |em| return Result{ .fail = em },
649602 }
650603 }
651604
652605 if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) {
653 switch (try generateSymbol(bin_file, src_loc, .{
654 .ty = Type.fromInterned(union_obj.enum_tag_ty),
655 .val = Value.fromInterned(un.tag),
656 }, code, debug_output, reloc_info)) {
606 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
657607 .ok => {},
658608 .fail => |em| return Result{ .fail = em },
659609 }
......@@ -681,10 +631,7 @@ fn lowerParentPtr(
681631 return switch (ptr.addr) {
682632 .decl => |decl| try lowerDeclRef(bin_file, src_loc, decl, code, debug_output, reloc_info),
683633 .anon_decl => |ad| try lowerAnonDeclRef(bin_file, src_loc, ad, code, debug_output, reloc_info),
684 .int => |int| try generateSymbol(bin_file, src_loc, .{
685 .ty = Type.usize,
686 .val = Value.fromInterned(int),
687 }, code, debug_output, reloc_info),
634 .int => |int| try generateSymbol(bin_file, src_loc, Value.fromInterned(int), code, debug_output, reloc_info),
688635 .eu_payload => |eu_payload| try lowerParentPtr(
689636 bin_file,
690637 src_loc,
......@@ -910,11 +857,12 @@ pub const GenResult = union(enum) {
910857fn genDeclRef(
911858 lf: *link.File,
912859 src_loc: Module.SrcLoc,
913 tv: TypedValue,
860 val: Value,
914861 ptr_decl_index: InternPool.DeclIndex,
915862) CodeGenError!GenResult {
916863 const zcu = lf.comp.module.?;
917 log.debug("genDeclRef: ty = {}, val = {}", .{ tv.ty.fmt(zcu), tv.val.fmtValue(tv.ty, zcu) });
864 const ty = val.typeOf(zcu);
865 log.debug("genDeclRef: val = {}", .{val.fmtValue(ty, zcu)});
918866
919867 const ptr_decl = zcu.declPtr(ptr_decl_index);
920868 const namespace = zcu.namespacePtr(ptr_decl.src_namespace);
......@@ -945,12 +893,12 @@ fn genDeclRef(
945893 const gpa = comp.gpa;
946894
947895 // TODO this feels clunky. Perhaps we should check for it in `genTypedValue`?
948 if (tv.ty.castPtrToFn(zcu)) |fn_ty| {
896 if (ty.castPtrToFn(zcu)) |fn_ty| {
949897 if (zcu.typeToFunc(fn_ty).?.is_generic) {
950898 return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(zcu).toByteUnitsOptional().? });
951899 }
952 } else if (tv.ty.zigTypeTag(zcu) == .Pointer) {
953 const elem_ty = tv.ty.elemType2(zcu);
900 } else if (ty.zigTypeTag(zcu) == .Pointer) {
901 const elem_ty = ty.elemType2(zcu);
954902 if (!elem_ty.hasRuntimeBits(zcu)) {
955903 return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(zcu).toByteUnitsOptional().? });
956904 }
......@@ -958,7 +906,7 @@ fn genDeclRef(
958906
959907 const decl_namespace = zcu.namespacePtr(decl.src_namespace);
960908 const single_threaded = decl_namespace.file_scope.mod.single_threaded;
961 const is_threadlocal = tv.val.isPtrToThreadLocal(zcu) and !single_threaded;
909 const is_threadlocal = val.isPtrToThreadLocal(zcu) and !single_threaded;
962910 const is_extern = decl.isExtern(zcu);
963911
964912 if (lf.cast(link.File.Elf)) |elf_file| {
......@@ -1023,14 +971,14 @@ fn genDeclRef(
1023971fn genUnnamedConst(
1024972 lf: *link.File,
1025973 src_loc: Module.SrcLoc,
1026 tv: TypedValue,
974 val: Value,
1027975 owner_decl_index: InternPool.DeclIndex,
1028976) CodeGenError!GenResult {
1029977 const zcu = lf.comp.module.?;
1030978 const gpa = lf.comp.gpa;
1031 log.debug("genUnnamedConst: ty = {}, val = {}", .{ tv.ty.fmt(zcu), tv.val.fmtValue(tv.ty, zcu) });
979 log.debug("genUnnamedConst: val = {}", .{val.fmtValue(val.typeOf(zcu), zcu)});
1032980
1033 const local_sym_index = lf.lowerUnnamedConst(tv, owner_decl_index) catch |err| {
981 const local_sym_index = lf.lowerUnnamedConst(val, owner_decl_index) catch |err| {
1034982 return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
1035983 };
1036984 switch (lf.tag) {
......@@ -1062,18 +1010,15 @@ fn genUnnamedConst(
10621010pub fn genTypedValue(
10631011 lf: *link.File,
10641012 src_loc: Module.SrcLoc,
1065 arg_tv: TypedValue,
1013 val: Value,
10661014 owner_decl_index: InternPool.DeclIndex,
10671015) CodeGenError!GenResult {
10681016 const zcu = lf.comp.module.?;
1069 const typed_value = arg_tv;
1017 const ty = val.typeOf(zcu);
10701018
1071 log.debug("genTypedValue: ty = {}, val = {}", .{
1072 typed_value.ty.fmt(zcu),
1073 typed_value.val.fmtValue(typed_value.ty, zcu),
1074 });
1019 log.debug("genTypedValue: val = {}", .{val.fmtValue(ty, zcu)});
10751020
1076 if (typed_value.val.isUndef(zcu))
1021 if (val.isUndef(zcu))
10771022 return GenResult.mcv(.undef);
10781023
10791024 const owner_decl = zcu.declPtr(owner_decl_index);
......@@ -1081,85 +1026,92 @@ pub fn genTypedValue(
10811026 const target = namespace.file_scope.mod.resolved_target.result;
10821027 const ptr_bits = target.ptrBitWidth();
10831028
1084 if (!typed_value.ty.isSlice(zcu)) switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) {
1029 if (!ty.isSlice(zcu)) switch (zcu.intern_pool.indexToKey(val.toIntern())) {
10851030 .ptr => |ptr| switch (ptr.addr) {
1086 .decl => |decl| return genDeclRef(lf, src_loc, typed_value, decl),
1031 .decl => |decl| return genDeclRef(lf, src_loc, val, decl),
10871032 else => {},
10881033 },
10891034 else => {},
10901035 };
10911036
1092 switch (typed_value.ty.zigTypeTag(zcu)) {
1037 switch (ty.zigTypeTag(zcu)) {
10931038 .Void => return GenResult.mcv(.none),
1094 .Pointer => switch (typed_value.ty.ptrSize(zcu)) {
1039 .Pointer => switch (ty.ptrSize(zcu)) {
10951040 .Slice => {},
1096 else => switch (typed_value.val.toIntern()) {
1041 else => switch (val.toIntern()) {
10971042 .null_value => {
10981043 return GenResult.mcv(.{ .immediate = 0 });
10991044 },
11001045 .none => {},
1101 else => switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) {
1046 else => switch (zcu.intern_pool.indexToKey(val.toIntern())) {
11021047 .int => {
1103 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(zcu) });
1048 return GenResult.mcv(.{ .immediate = val.toUnsignedInt(zcu) });
11041049 },
11051050 else => {},
11061051 },
11071052 },
11081053 },
11091054 .Int => {
1110 const info = typed_value.ty.intInfo(zcu);
1055 const info = ty.intInfo(zcu);
11111056 if (info.bits <= ptr_bits) {
11121057 const unsigned = switch (info.signedness) {
1113 .signed => @as(u64, @bitCast(typed_value.val.toSignedInt(zcu))),
1114 .unsigned => typed_value.val.toUnsignedInt(zcu),
1058 .signed => @as(u64, @bitCast(val.toSignedInt(zcu))),
1059 .unsigned => val.toUnsignedInt(zcu),
11151060 };
11161061 return GenResult.mcv(.{ .immediate = unsigned });
11171062 }
11181063 },
11191064 .Bool => {
1120 return GenResult.mcv(.{ .immediate = @intFromBool(typed_value.val.toBool()) });
1065 return GenResult.mcv(.{ .immediate = @intFromBool(val.toBool()) });
11211066 },
11221067 .Optional => {
1123 if (typed_value.ty.isPtrLikeOptional(zcu)) {
1124 return genTypedValue(lf, src_loc, .{
1125 .ty = typed_value.ty.optionalChild(zcu),
1126 .val = typed_value.val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }),
1127 }, owner_decl_index);
1128 } else if (typed_value.ty.abiSize(zcu) == 1) {
1129 return GenResult.mcv(.{ .immediate = @intFromBool(!typed_value.val.isNull(zcu)) });
1068 if (ty.isPtrLikeOptional(zcu)) {
1069 return genTypedValue(
1070 lf,
1071 src_loc,
1072 val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }),
1073 owner_decl_index,
1074 );
1075 } else if (ty.abiSize(zcu) == 1) {
1076 return GenResult.mcv(.{ .immediate = @intFromBool(!val.isNull(zcu)) });
11301077 }
11311078 },
11321079 .Enum => {
1133 const enum_tag = zcu.intern_pool.indexToKey(typed_value.val.toIntern()).enum_tag;
1134 const int_tag_ty = zcu.intern_pool.typeOf(enum_tag.int);
1135 return genTypedValue(lf, src_loc, .{
1136 .ty = Type.fromInterned(int_tag_ty),
1137 .val = Value.fromInterned(enum_tag.int),
1138 }, owner_decl_index);
1080 const enum_tag = zcu.intern_pool.indexToKey(val.toIntern()).enum_tag;
1081 return genTypedValue(
1082 lf,
1083 src_loc,
1084 Value.fromInterned(enum_tag.int),
1085 owner_decl_index,
1086 );
11391087 },
11401088 .ErrorSet => {
1141 const err_name = zcu.intern_pool.indexToKey(typed_value.val.toIntern()).err.name;
1089 const err_name = zcu.intern_pool.indexToKey(val.toIntern()).err.name;
11421090 const error_index = zcu.global_error_set.getIndex(err_name).?;
11431091 return GenResult.mcv(.{ .immediate = error_index });
11441092 },
11451093 .ErrorUnion => {
1146 const err_type = typed_value.ty.errorUnionSet(zcu);
1147 const payload_type = typed_value.ty.errorUnionPayload(zcu);
1094 const err_type = ty.errorUnionSet(zcu);
1095 const payload_type = ty.errorUnionPayload(zcu);
11481096 if (!payload_type.hasRuntimeBitsIgnoreComptime(zcu)) {
11491097 // We use the error type directly as the type.
11501098 const err_int_ty = try zcu.errorIntType();
1151 switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern()).error_union.val) {
1152 .err_name => |err_name| return genTypedValue(lf, src_loc, .{
1153 .ty = err_type,
1154 .val = Value.fromInterned((try zcu.intern(.{ .err = .{
1099 switch (zcu.intern_pool.indexToKey(val.toIntern()).error_union.val) {
1100 .err_name => |err_name| return genTypedValue(
1101 lf,
1102 src_loc,
1103 Value.fromInterned(try zcu.intern(.{ .err = .{
11551104 .ty = err_type.toIntern(),
11561105 .name = err_name,
1157 } }))),
1158 }, owner_decl_index),
1159 .payload => return genTypedValue(lf, src_loc, .{
1160 .ty = err_int_ty,
1161 .val = try zcu.intValue(err_int_ty, 0),
1162 }, owner_decl_index),
1106 } })),
1107 owner_decl_index,
1108 ),
1109 .payload => return genTypedValue(
1110 lf,
1111 src_loc,
1112 try zcu.intValue(err_int_ty, 0),
1113 owner_decl_index,
1114 ),
11631115 }
11641116 }
11651117 },
......@@ -1176,7 +1128,7 @@ pub fn genTypedValue(
11761128 else => {},
11771129 }
11781130
1179 return genUnnamedConst(lf, src_loc, typed_value, owner_decl_index);
1131 return genUnnamedConst(lf, src_loc, val, owner_decl_index);
11801132}
11811133
11821134pub fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u64 {
src/codegen/c.zig+28-35
......@@ -9,7 +9,6 @@ const Module = @import("../Module.zig");
99const Compilation = @import("../Compilation.zig");
1010const Value = @import("../Value.zig");
1111const Type = @import("../type.zig").Type;
12const TypedValue = @import("../TypedValue.zig");
1312const C = link.File.C;
1413const Decl = Module.Decl;
1514const trace = @import("../tracy.zig").trace;
......@@ -1877,9 +1876,9 @@ pub const DeclGen = struct {
18771876 try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{});
18781877 }
18791878
1880 fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool {
1879 fn declIsGlobal(dg: *DeclGen, val: Value) bool {
18811880 const mod = dg.module;
1882 return switch (mod.intern_pool.indexToKey(tv.val.ip_index)) {
1881 return switch (mod.intern_pool.indexToKey(val.ip_index)) {
18831882 .variable => |variable| mod.decl_exports.contains(variable.decl),
18841883 .extern_func => true,
18851884 .func => |func| mod.decl_exports.contains(func.owner_decl),
......@@ -1972,7 +1971,7 @@ pub const DeclGen = struct {
19721971 ) !void {
19731972 const decl = dg.module.declPtr(decl_index);
19741973 const fwd = dg.fwdDeclWriter();
1975 const is_global = variable.is_extern or dg.declIsGlobal(.{ .ty = decl.typeOf(dg.module), .val = decl.val });
1974 const is_global = variable.is_extern or dg.declIsGlobal(decl.val);
19761975 try fwd.writeAll(if (is_global) "zig_extern " else "static ");
19771976 const maybe_exports = dg.module.decl_exports.get(decl_index);
19781977 const export_weak_linkage = if (maybe_exports) |exports|
......@@ -2656,13 +2655,12 @@ fn genExports(o: *Object) !void {
26562655 .anon, .flush => return,
26572656 };
26582657 const decl = mod.declPtr(decl_index);
2659 const tv: TypedValue = .{ .ty = decl.typeOf(mod), .val = decl.val };
26602658 const fwd = o.dg.fwdDeclWriter();
26612659
26622660 const exports = mod.decl_exports.get(decl_index) orelse return;
26632661 if (exports.items.len < 2) return;
26642662
2665 const is_variable_const = switch (ip.indexToKey(tv.val.toIntern())) {
2663 const is_variable_const = switch (ip.indexToKey(decl.val.toIntern())) {
26662664 .func => return for (exports.items[1..], 1..) |@"export", i| {
26672665 try fwd.writeAll("zig_extern ");
26682666 if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage_fn ");
......@@ -2805,15 +2803,11 @@ pub fn genFunc(f: *Function) !void {
28052803 const gpa = o.dg.gpa;
28062804 const decl_index = o.dg.pass.decl;
28072805 const decl = mod.declPtr(decl_index);
2808 const tv: TypedValue = .{
2809 .ty = decl.typeOf(mod),
2810 .val = decl.val,
2811 };
28122806
28132807 o.code_header = std.ArrayList(u8).init(gpa);
28142808 defer o.code_header.deinit();
28152809
2816 const is_global = o.dg.declIsGlobal(tv);
2810 const is_global = o.dg.declIsGlobal(decl.val);
28172811 const fwd_decl_writer = o.dg.fwdDeclWriter();
28182812 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
28192813
......@@ -2893,22 +2887,23 @@ pub fn genDecl(o: *Object) !void {
28932887 const mod = o.dg.module;
28942888 const decl_index = o.dg.pass.decl;
28952889 const decl = mod.declPtr(decl_index);
2896 const tv: TypedValue = .{ .ty = decl.typeOf(mod), .val = decl.val };
2890 const decl_val = decl.val;
2891 const decl_ty = decl_val.typeOf(mod);
28972892
2898 if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
2899 if (tv.val.getExternFunc(mod)) |_| {
2893 if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
2894 if (decl_val.getExternFunc(mod)) |_| {
29002895 const fwd_decl_writer = o.dg.fwdDeclWriter();
29012896 try fwd_decl_writer.writeAll("zig_extern ");
29022897 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });
29032898 try fwd_decl_writer.writeAll(";\n");
29042899 try genExports(o);
2905 } else if (tv.val.getVariable(mod)) |variable| {
2900 } else if (decl_val.getVariable(mod)) |variable| {
29062901 try o.dg.renderFwdDecl(decl_index, variable, .final);
29072902 try genExports(o);
29082903
29092904 if (variable.is_extern) return;
29102905
2911 const is_global = variable.is_extern or o.dg.declIsGlobal(tv);
2906 const is_global = variable.is_extern or o.dg.declIsGlobal(decl_val);
29122907 const w = o.writer();
29132908 if (!is_global) try w.writeAll("static ");
29142909 if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");
......@@ -2916,22 +2911,22 @@ pub fn genDecl(o: *Object) !void {
29162911 if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s|
29172912 try w.print("zig_linksection(\"{s}\", ", .{s});
29182913 const decl_c_value = .{ .decl = decl_index };
2919 try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, .{}, decl.alignment, .complete);
2914 try o.dg.renderTypeAndName(w, decl_ty, decl_c_value, .{}, decl.alignment, .complete);
29202915 if (decl.@"linksection" != .none) try w.writeAll(", read, write)");
29212916 try w.writeAll(" = ");
2922 try o.dg.renderValue(w, tv.ty, Value.fromInterned(variable.init), .StaticInitializer);
2917 try o.dg.renderValue(w, decl_ty, Value.fromInterned(variable.init), .StaticInitializer);
29232918 try w.writeByte(';');
29242919 try o.indent_writer.insertNewline();
29252920 } else {
29262921 const is_global = o.dg.module.decl_exports.contains(decl_index);
29272922 const decl_c_value = .{ .decl = decl_index };
2928 try genDeclValue(o, tv, is_global, decl_c_value, decl.alignment, decl.@"linksection");
2923 try genDeclValue(o, decl_val, is_global, decl_c_value, decl.alignment, decl.@"linksection");
29292924 }
29302925}
29312926
29322927pub fn genDeclValue(
29332928 o: *Object,
2934 tv: TypedValue,
2929 val: Value,
29352930 is_global: bool,
29362931 decl_c_value: CValue,
29372932 alignment: Alignment,
......@@ -2940,8 +2935,10 @@ pub fn genDeclValue(
29402935 const mod = o.dg.module;
29412936 const fwd_decl_writer = o.dg.fwdDeclWriter();
29422937
2938 const ty = val.typeOf(mod);
2939
29432940 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
2944 try o.dg.renderTypeAndName(fwd_decl_writer, tv.ty, decl_c_value, Const, alignment, .complete);
2941 try o.dg.renderTypeAndName(fwd_decl_writer, ty, decl_c_value, Const, alignment, .complete);
29452942 switch (o.dg.pass) {
29462943 .decl => |decl_index| {
29472944 if (mod.decl_exports.get(decl_index)) |exports| {
......@@ -2964,10 +2961,10 @@ pub fn genDeclValue(
29642961
29652962 if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|
29662963 try w.print("zig_linksection(\"{s}\", ", .{s});
2967 try o.dg.renderTypeAndName(w, tv.ty, decl_c_value, Const, alignment, .complete);
2964 try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete);
29682965 if (link_section != .none) try w.writeAll(", read)");
29692966 try w.writeAll(" = ");
2970 try o.dg.renderValue(w, tv.ty, tv.val, .StaticInitializer);
2967 try o.dg.renderValue(w, ty, val, .StaticInitializer);
29712968 try w.writeAll(";\n");
29722969}
29732970
......@@ -2978,14 +2975,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
29782975 const mod = dg.module;
29792976 const decl_index = dg.pass.decl;
29802977 const decl = mod.declPtr(decl_index);
2981 const tv: TypedValue = .{
2982 .ty = decl.typeOf(mod),
2983 .val = decl.val,
2984 };
29852978 const writer = dg.fwdDeclWriter();
29862979
2987 switch (tv.ty.zigTypeTag(mod)) {
2988 .Fn => if (dg.declIsGlobal(tv)) {
2980 switch (decl.val.typeOf(mod).zigTypeTag(mod)) {
2981 .Fn => if (dg.declIsGlobal(decl.val)) {
29892982 try writer.writeAll("zig_extern ");
29902983 try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });
29912984 try dg.fwd_decl.appendSlice(";\n");
......@@ -5304,25 +5297,25 @@ fn airIsNull(
53045297 const err_int_ty = try mod.errorIntType();
53055298
53065299 const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod))
5307 TypedValue{ .ty = Type.bool, .val = Value.true }
5300 Value.true
53085301 else if (optional_ty.isPtrLikeOptional(mod))
53095302 // operand is a regular pointer, test `operand !=/== NULL`
5310 TypedValue{ .ty = optional_ty, .val = try mod.getCoerced(Value.null, optional_ty) }
5303 try mod.getCoerced(Value.null, optional_ty)
53115304 else if (payload_ty.zigTypeTag(mod) == .ErrorSet)
5312 TypedValue{ .ty = err_int_ty, .val = try mod.intValue(err_int_ty, 0) }
5305 try mod.intValue(err_int_ty, 0)
53135306 else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: {
53145307 try writer.writeAll(".ptr");
53155308 const slice_ptr_ty = payload_ty.slicePtrFieldType(mod);
53165309 const opt_slice_ptr_ty = try mod.optionalType(slice_ptr_ty.toIntern());
5317 break :rhs TypedValue{ .ty = opt_slice_ptr_ty, .val = try mod.nullValue(opt_slice_ptr_ty) };
5310 break :rhs try mod.nullValue(opt_slice_ptr_ty);
53185311 } else rhs: {
53195312 try writer.writeAll(".is_null");
5320 break :rhs TypedValue{ .ty = Type.bool, .val = Value.true };
5313 break :rhs Value.true;
53215314 };
53225315 try writer.writeByte(' ');
53235316 try writer.writeAll(operator);
53245317 try writer.writeByte(' ');
5325 try f.object.dg.renderValue(writer, rhs.ty, rhs.val, .Other);
5318 try f.object.dg.renderValue(writer, rhs.typeOf(mod), rhs, .Other);
53265319 try writer.writeAll(";\n");
53275320 return local;
53285321}
src/codegen/llvm.zig+11-18
......@@ -18,7 +18,6 @@ const Module = @import("../Module.zig");
1818const Zcu = Module;
1919const InternPool = @import("../InternPool.zig");
2020const Package = @import("../Package.zig");
21const TypedValue = @import("../TypedValue.zig");
2221const Air = @import("../Air.zig");
2322const Liveness = @import("../Liveness.zig");
2423const Value = @import("../Value.zig");
......@@ -4823,19 +4822,17 @@ pub const FuncGen = struct {
48234822
48244823 const o = self.dg.object;
48254824 const mod = o.module;
4826 const llvm_val = try self.resolveValue(.{
4827 .ty = self.typeOf(inst),
4828 .val = (try self.air.value(inst, mod)).?,
4829 });
4825 const llvm_val = try self.resolveValue((try self.air.value(inst, mod)).?);
48304826 gop.value_ptr.* = llvm_val.toValue();
48314827 return llvm_val.toValue();
48324828 }
48334829
4834 fn resolveValue(self: *FuncGen, tv: TypedValue) Error!Builder.Constant {
4830 fn resolveValue(self: *FuncGen, val: Value) Error!Builder.Constant {
48354831 const o = self.dg.object;
48364832 const mod = o.module;
4837 const llvm_val = try o.lowerValue(tv.val.toIntern());
4838 if (!isByRef(tv.ty, mod)) return llvm_val;
4833 const ty = val.typeOf(mod);
4834 const llvm_val = try o.lowerValue(val.toIntern());
4835 if (!isByRef(ty, mod)) return llvm_val;
48394836
48404837 // We have an LLVM value but we need to create a global constant and
48414838 // set the value as its initializer, and then return a pointer to the global.
......@@ -4849,7 +4846,7 @@ pub const FuncGen = struct {
48494846 variable_index.setLinkage(.private, &o.builder);
48504847 variable_index.setMutability(.constant, &o.builder);
48514848 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);
4852 variable_index.setAlignment(tv.ty.abiAlignment(mod).toLlvm(), &o.builder);
4849 variable_index.setAlignment(ty.abiAlignment(mod).toLlvm(), &o.builder);
48534850 return o.builder.convConst(
48544851 .unneeded,
48554852 variable_index.toConst(&o.builder),
......@@ -4861,11 +4858,10 @@ pub const FuncGen = struct {
48614858 const o = self.dg.object;
48624859 const mod = o.module;
48634860 if (o.null_opt_usize == .no_init) {
4864 const ty = try mod.intern(.{ .opt_type = .usize_type });
4865 o.null_opt_usize = try self.resolveValue(.{
4866 .ty = Type.fromInterned(ty),
4867 .val = Value.fromInterned((try mod.intern(.{ .opt = .{ .ty = ty, .val = .none } }))),
4868 });
4861 o.null_opt_usize = try self.resolveValue(Value.fromInterned(try mod.intern(.{ .opt = .{
4862 .ty = try mod.intern(.{ .opt_type = .usize_type }),
4863 .val = .none,
4864 } })));
48694865 }
48704866 return o.null_opt_usize;
48714867 }
......@@ -10061,10 +10057,7 @@ pub const FuncGen = struct {
1006110057 const elem_ptr = try self.wip.gep(.inbounds, llvm_result_ty, alloca_inst, &.{
1006210058 usize_zero, try o.builder.intValue(llvm_usize, array_info.len),
1006310059 }, "");
10064 const llvm_elem = try self.resolveValue(.{
10065 .ty = array_info.elem_type,
10066 .val = sent_val,
10067 });
10060 const llvm_elem = try self.resolveValue(sent_val);
1006810061 try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none);
1006910062 }
1007010063
src/link.zig+3-3
......@@ -17,7 +17,7 @@ const Liveness = @import("Liveness.zig");
1717const Module = @import("Module.zig");
1818const InternPool = @import("InternPool.zig");
1919const Type = @import("type.zig").Type;
20const TypedValue = @import("TypedValue.zig");
20const Value = @import("Value.zig");
2121const LlvmObject = @import("codegen/llvm.zig").Object;
2222
2323/// When adding a new field, remember to update `hashAddSystemLibs`.
......@@ -376,14 +376,14 @@ pub const File = struct {
376376 /// Called from within the CodeGen to lower a local variable instantion as an unnamed
377377 /// constant. Returns the symbol index of the lowered constant in the read-only section
378378 /// of the final binary.
379 pub fn lowerUnnamedConst(base: *File, tv: TypedValue, decl_index: InternPool.DeclIndex) UpdateDeclError!u32 {
379 pub fn lowerUnnamedConst(base: *File, val: Value, decl_index: InternPool.DeclIndex) UpdateDeclError!u32 {
380380 if (build_options.only_c) @compileError("unreachable");
381381 switch (base.tag) {
382382 .spirv => unreachable,
383383 .c => unreachable,
384384 .nvptx => unreachable,
385385 inline else => |t| {
386 return @fieldParentPtr(t.Type(), "base", base).lowerUnnamedConst(tv, decl_index);
386 return @fieldParentPtr(t.Type(), "base", base).lowerUnnamedConst(val, decl_index);
387387 },
388388 }
389389 }
src/link/C.zig+1-5
......@@ -283,13 +283,9 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void {
283283 code.* = object.code.moveToUnmanaged();
284284 }
285285
286 const tv: @import("../TypedValue.zig") = .{
287 .ty = Type.fromInterned(module.intern_pool.typeOf(anon_decl)),
288 .val = Value.fromInterned(anon_decl),
289 };
290286 const c_value: codegen.CValue = .{ .constant = anon_decl };
291287 const alignment: Alignment = self.aligned_anon_decls.get(anon_decl) orelse .none;
292 codegen.genDeclValue(&object, tv, false, c_value, alignment, .none) catch |err| switch (err) {
288 codegen.genDeclValue(&object, Value.fromInterned(anon_decl), false, c_value, alignment, .none) catch |err| switch (err) {
293289 error.AnalysisFail => {
294290 @panic("TODO: C backend AnalysisFail on anonymous decl");
295291 //try module.failed_decls.put(gpa, decl_index, object.dg.error_msg.?);
src/link/Coff.zig+7-11
......@@ -1167,7 +1167,7 @@ pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air:
11671167 return self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));
11681168}
11691169
1170pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
1170pub fn lowerUnnamedConst(self: *Coff, val: Value, decl_index: InternPool.DeclIndex) !u32 {
11711171 const gpa = self.base.comp.gpa;
11721172 const mod = self.base.comp.module.?;
11731173 const decl = mod.declPtr(decl_index);
......@@ -1180,7 +1180,8 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: InternPool.Dec
11801180 const index = unnamed_consts.items.len;
11811181 const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
11821182 defer gpa.free(sym_name);
1183 const atom_index = switch (try self.lowerConst(sym_name, tv, tv.ty.abiAlignment(mod), self.rdata_section_index.?, decl.srcLoc(mod))) {
1183 const ty = val.typeOf(mod);
1184 const atom_index = switch (try self.lowerConst(sym_name, val, ty.abiAlignment(mod), self.rdata_section_index.?, decl.srcLoc(mod))) {
11841185 .ok => |atom_index| atom_index,
11851186 .fail => |em| {
11861187 decl.analysis = .codegen_failure;
......@@ -1198,7 +1199,7 @@ const LowerConstResult = union(enum) {
11981199 fail: *Module.ErrorMsg,
11991200};
12001201
1201fn lowerConst(self: *Coff, name: []const u8, tv: TypedValue, required_alignment: InternPool.Alignment, sect_id: u16, src_loc: Module.SrcLoc) !LowerConstResult {
1202fn lowerConst(self: *Coff, name: []const u8, val: Value, required_alignment: InternPool.Alignment, sect_id: u16, src_loc: Module.SrcLoc) !LowerConstResult {
12021203 const gpa = self.base.comp.gpa;
12031204
12041205 var code_buffer = std.ArrayList(u8).init(gpa);
......@@ -1209,7 +1210,7 @@ fn lowerConst(self: *Coff, name: []const u8, tv: TypedValue, required_alignment:
12091210 try self.setSymbolName(sym, name);
12101211 sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_id + 1));
12111212
1212 const res = try codegen.generateSymbol(&self.base, src_loc, tv, &code_buffer, .none, .{
1213 const res = try codegen.generateSymbol(&self.base, src_loc, val, &code_buffer, .none, .{
12131214 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
12141215 });
12151216 const code = switch (res) {
......@@ -1271,10 +1272,7 @@ pub fn updateDecl(
12711272 defer code_buffer.deinit();
12721273
12731274 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
1274 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
1275 .ty = decl.typeOf(mod),
1276 .val = decl_val,
1277 }, &code_buffer, .none, .{
1275 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
12781276 .parent_atom_index = atom.getSymbolIndex().?,
12791277 });
12801278 const code = switch (res) {
......@@ -1887,14 +1885,13 @@ pub fn lowerAnonDecl(
18871885 }
18881886
18891887 const val = Value.fromInterned(decl_val);
1890 const tv = TypedValue{ .ty = ty, .val = val };
18911888 var name_buf: [32]u8 = undefined;
18921889 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
18931890 @intFromEnum(decl_val),
18941891 }) catch unreachable;
18951892 const res = self.lowerConst(
18961893 name,
1897 tv,
1894 val,
18981895 decl_alignment,
18991896 self.rdata_section_index.?,
19001897 src_loc,
......@@ -2754,7 +2751,6 @@ const TableSection = @import("table_section.zig").TableSection;
27542751const StringTable = @import("StringTable.zig");
27552752const Type = @import("../type.zig").Type;
27562753const Value = @import("../Value.zig");
2757const TypedValue = @import("../TypedValue.zig");
27582754
27592755pub const base_tag: link.File.Tag = .coff;
27602756
src/link/Elf.zig+3-3
......@@ -3039,8 +3039,8 @@ pub fn updateDecl(
30393039 return self.zigObjectPtr().?.updateDecl(self, mod, decl_index);
30403040}
30413041
3042pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
3043 return self.zigObjectPtr().?.lowerUnnamedConst(self, typed_value, decl_index);
3042pub fn lowerUnnamedConst(self: *Elf, val: Value, decl_index: InternPool.DeclIndex) !u32 {
3043 return self.zigObjectPtr().?.lowerUnnamedConst(self, val, decl_index);
30443044}
30453045
30463046pub fn updateExports(
......@@ -6260,7 +6260,7 @@ const SharedObject = @import("Elf/SharedObject.zig");
62606260const Symbol = @import("Elf/Symbol.zig");
62616261const StringTable = @import("StringTable.zig");
62626262const Thunk = thunks.Thunk;
6263const TypedValue = @import("../TypedValue.zig");
6263const Value = @import("../Value.zig");
62646264const VerneedSection = synthetic_sections.VerneedSection;
62656265const ZigGotSection = synthetic_sections.ZigGotSection;
62666266const ZigObject = @import("Elf/ZigObject.zig");
src/link/Elf/ZigObject.zig+9-16
......@@ -702,7 +702,6 @@ pub fn lowerAnonDecl(
702702 }
703703
704704 const val = Value.fromInterned(decl_val);
705 const tv = TypedValue{ .ty = ty, .val = val };
706705 var name_buf: [32]u8 = undefined;
707706 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
708707 @intFromEnum(decl_val),
......@@ -710,7 +709,7 @@ pub fn lowerAnonDecl(
710709 const res = self.lowerConst(
711710 elf_file,
712711 name,
713 tv,
712 val,
714713 decl_alignment,
715714 elf_file.zig_data_rel_ro_section_index.?,
716715 src_loc,
......@@ -1157,19 +1156,13 @@ pub fn updateDecl(
11571156 // TODO implement .debug_info for global variables
11581157 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
11591158 const res = if (decl_state) |*ds|
1160 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), .{
1161 .ty = decl.typeOf(mod),
1162 .val = decl_val,
1163 }, &code_buffer, .{
1159 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .{
11641160 .dwarf = ds,
11651161 }, .{
11661162 .parent_atom_index = sym_index,
11671163 })
11681164 else
1169 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), .{
1170 .ty = decl.typeOf(mod),
1171 .val = decl_val,
1172 }, &code_buffer, .none, .{
1165 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
11731166 .parent_atom_index = sym_index,
11741167 });
11751168
......@@ -1289,7 +1282,7 @@ fn updateLazySymbol(
12891282pub fn lowerUnnamedConst(
12901283 self: *ZigObject,
12911284 elf_file: *Elf,
1292 typed_value: TypedValue,
1285 val: Value,
12931286 decl_index: InternPool.DeclIndex,
12941287) !u32 {
12951288 const gpa = elf_file.base.comp.gpa;
......@@ -1304,11 +1297,12 @@ pub fn lowerUnnamedConst(
13041297 const index = unnamed_consts.items.len;
13051298 const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
13061299 defer gpa.free(name);
1300 const ty = val.typeOf(mod);
13071301 const sym_index = switch (try self.lowerConst(
13081302 elf_file,
13091303 name,
1310 typed_value,
1311 typed_value.ty.abiAlignment(mod),
1304 val,
1305 ty.abiAlignment(mod),
13121306 elf_file.zig_data_rel_ro_section_index.?,
13131307 decl.srcLoc(mod),
13141308 )) {
......@@ -1334,7 +1328,7 @@ fn lowerConst(
13341328 self: *ZigObject,
13351329 elf_file: *Elf,
13361330 name: []const u8,
1337 tv: TypedValue,
1331 val: Value,
13381332 required_alignment: InternPool.Alignment,
13391333 output_section_index: u32,
13401334 src_loc: Module.SrcLoc,
......@@ -1346,7 +1340,7 @@ fn lowerConst(
13461340
13471341 const sym_index = try self.addAtom(elf_file);
13481342
1349 const res = try codegen.generateSymbol(&elf_file.base, src_loc, tv, &code_buffer, .{
1343 const res = try codegen.generateSymbol(&elf_file.base, src_loc, val, &code_buffer, .{
13501344 .none = {},
13511345 }, .{
13521346 .parent_atom_index = sym_index,
......@@ -1657,5 +1651,4 @@ const Symbol = @import("Symbol.zig");
16571651const StringTable = @import("../StringTable.zig");
16581652const Type = @import("../../type.zig").Type;
16591653const Value = @import("../../Value.zig");
1660const TypedValue = @import("../../TypedValue.zig");
16611654const ZigObject = @This();
src/link/MachO.zig+3-3
......@@ -3127,8 +3127,8 @@ pub fn updateFunc(self: *MachO, mod: *Module, func_index: InternPool.Index, air:
31273127 return self.getZigObject().?.updateFunc(self, mod, func_index, air, liveness);
31283128}
31293129
3130pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
3131 return self.getZigObject().?.lowerUnnamedConst(self, typed_value, decl_index);
3130pub fn lowerUnnamedConst(self: *MachO, val: Value, decl_index: InternPool.DeclIndex) !u32 {
3131 return self.getZigObject().?.lowerUnnamedConst(self, val, decl_index);
31323132}
31333133
31343134pub fn updateDecl(self: *MachO, mod: *Module, decl_index: InternPool.DeclIndex) !void {
......@@ -4689,7 +4689,7 @@ const StubsHelperSection = synthetic.StubsHelperSection;
46894689const Symbol = @import("MachO/Symbol.zig");
46904690const Thunk = thunks.Thunk;
46914691const TlvPtrSection = synthetic.TlvPtrSection;
4692const TypedValue = @import("../TypedValue.zig");
4692const Value = @import("../Value.zig");
46934693const UnwindInfo = @import("MachO/UnwindInfo.zig");
46944694const WeakBindSection = synthetic.WeakBindSection;
46954695const ZigGotSection = synthetic.ZigGotSection;
src/link/MachO/ZigObject.zig+7-14
......@@ -567,8 +567,6 @@ pub fn lowerAnonDecl(
567567 return .ok;
568568 }
569569
570 const val = Value.fromInterned(decl_val);
571 const tv = TypedValue{ .ty = ty, .val = val };
572570 var name_buf: [32]u8 = undefined;
573571 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
574572 @intFromEnum(decl_val),
......@@ -576,7 +574,7 @@ pub fn lowerAnonDecl(
576574 const res = self.lowerConst(
577575 macho_file,
578576 name,
579 tv,
577 Value.fromInterned(decl_val),
580578 decl_alignment,
581579 macho_file.zig_const_sect_index.?,
582580 src_loc,
......@@ -738,11 +736,7 @@ pub fn updateDecl(
738736
739737 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
740738 const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none;
741 const res =
742 try codegen.generateSymbol(&macho_file.base, decl.srcLoc(mod), .{
743 .ty = decl.typeOf(mod),
744 .val = decl_val,
745 }, &code_buffer, dio, .{
739 const res = try codegen.generateSymbol(&macho_file.base, decl.srcLoc(mod), decl_val, &code_buffer, dio, .{
746740 .parent_atom_index = sym_index,
747741 });
748742
......@@ -1068,7 +1062,7 @@ fn getDeclOutputSection(
10681062pub fn lowerUnnamedConst(
10691063 self: *ZigObject,
10701064 macho_file: *MachO,
1071 typed_value: TypedValue,
1065 val: Value,
10721066 decl_index: InternPool.DeclIndex,
10731067) !u32 {
10741068 const gpa = macho_file.base.comp.gpa;
......@@ -1086,8 +1080,8 @@ pub fn lowerUnnamedConst(
10861080 const sym_index = switch (try self.lowerConst(
10871081 macho_file,
10881082 name,
1089 typed_value,
1090 typed_value.ty.abiAlignment(mod),
1083 val,
1084 val.typeOf(mod).abiAlignment(mod),
10911085 macho_file.zig_const_sect_index.?,
10921086 decl.srcLoc(mod),
10931087 )) {
......@@ -1113,7 +1107,7 @@ fn lowerConst(
11131107 self: *ZigObject,
11141108 macho_file: *MachO,
11151109 name: []const u8,
1116 tv: TypedValue,
1110 val: Value,
11171111 required_alignment: Atom.Alignment,
11181112 output_section_index: u8,
11191113 src_loc: Module.SrcLoc,
......@@ -1125,7 +1119,7 @@ fn lowerConst(
11251119
11261120 const sym_index = try self.addAtom(macho_file);
11271121
1128 const res = try codegen.generateSymbol(&macho_file.base, src_loc, tv, &code_buffer, .{
1122 const res = try codegen.generateSymbol(&macho_file.base, src_loc, val, &code_buffer, .{
11291123 .none = {},
11301124 }, .{
11311125 .parent_atom_index = sym_index,
......@@ -1580,5 +1574,4 @@ const Symbol = @import("Symbol.zig");
15801574const StringTable = @import("../StringTable.zig");
15811575const Type = @import("../../type.zig").Type;
15821576const Value = @import("../../Value.zig");
1583const TypedValue = @import("../../TypedValue.zig");
15841577const ZigObject = @This();
src/link/Plan9.zig+4-11
......@@ -15,7 +15,6 @@ const Air = @import("../Air.zig");
1515const Liveness = @import("../Liveness.zig");
1616const Type = @import("../type.zig").Type;
1717const Value = @import("../Value.zig");
18const TypedValue = @import("../TypedValue.zig");
1918
2019const std = @import("std");
2120const builtin = @import("builtin");
......@@ -463,7 +462,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:
463462 return self.updateFinish(decl_index);
464463}
465464
466pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
465pub fn lowerUnnamedConst(self: *Plan9, val: Value, decl_index: InternPool.DeclIndex) !u32 {
467466 const gpa = self.base.comp.gpa;
468467 _ = try self.seeDecl(decl_index);
469468 var code_buffer = std.ArrayList(u8).init(gpa);
......@@ -500,7 +499,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: InternPool.De
500499 };
501500 self.syms.items[info.sym_index.?] = sym;
502501
503 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), tv, &code_buffer, .{
502 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), val, &code_buffer, .{
504503 .none = {},
505504 }, .{
506505 .parent_atom_index = new_atom_idx,
......@@ -539,10 +538,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex)
539538 defer code_buffer.deinit();
540539 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
541540 // TODO we need the symbol index for symbol in the table of locals for the containing atom
542 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), .{
543 .ty = decl.typeOf(mod),
544 .val = decl_val,
545 }, &code_buffer, .{ .none = {} }, .{
541 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .{ .none = {} }, .{
546542 .parent_atom_index = @as(Atom.Index, @intCast(atom_idx)),
547543 });
548544 const code = switch (res) {
......@@ -1545,11 +1541,8 @@ pub fn lowerAnonDecl(
15451541 // ...
15461542 const gpa = self.base.comp.gpa;
15471543 const gop = try self.anon_decls.getOrPut(gpa, decl_val);
1548 const mod = self.base.comp.module.?;
15491544 if (!gop.found_existing) {
1550 const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
15511545 const val = Value.fromInterned(decl_val);
1552 const tv = TypedValue{ .ty = ty, .val = val };
15531546 const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)});
15541547
15551548 const index = try self.createAtom();
......@@ -1557,7 +1550,7 @@ pub fn lowerAnonDecl(
15571550 gop.value_ptr.* = index;
15581551 // we need to free name latex
15591552 var code_buffer = std.ArrayList(u8).init(gpa);
1560 const res = try codegen.generateSymbol(&self.base, src_loc, tv, &code_buffer, .{ .none = {} }, .{ .parent_atom_index = index });
1553 const res = try codegen.generateSymbol(&self.base, src_loc, val, &code_buffer, .{ .none = {} }, .{ .parent_atom_index = index });
15611554 const code = switch (res) {
15621555 .ok => code_buffer.items,
15631556 .fail => |em| return .{ .fail = em },
src/link/Wasm.zig+3-3
......@@ -32,7 +32,7 @@ const Module = @import("../Module.zig");
3232const Object = @import("Wasm/Object.zig");
3333const Symbol = @import("Wasm/Symbol.zig");
3434const Type = @import("../type.zig").Type;
35const TypedValue = @import("../TypedValue.zig");
35const Value = @import("../Value.zig");
3636const ZigObject = @import("Wasm/ZigObject.zig");
3737
3838pub const Atom = @import("Wasm/Atom.zig");
......@@ -1504,8 +1504,8 @@ fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type {
15041504/// Lowers a constant typed value to a local symbol and atom.
15051505/// Returns the symbol index of the local
15061506/// The given `decl` is the parent decl whom owns the constant.
1507pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
1508 return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, tv, decl_index);
1507pub fn lowerUnnamedConst(wasm: *Wasm, val: Value, decl_index: InternPool.DeclIndex) !u32 {
1508 return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, val, decl_index);
15091509}
15101510
15111511/// Returns the symbol index from a symbol of which its flag is set global,
src/link/Wasm/ZigObject.zig+11-13
......@@ -270,7 +270,7 @@ pub fn updateDecl(
270270 const res = try codegen.generateSymbol(
271271 &wasm_file.base,
272272 decl.srcLoc(mod),
273 .{ .ty = decl.typeOf(mod), .val = val },
273 val,
274274 &code_writer,
275275 .none,
276276 .{ .parent_atom_index = @intFromEnum(atom.sym_index) },
......@@ -444,15 +444,12 @@ pub fn lowerAnonDecl(
444444 const gpa = wasm_file.base.comp.gpa;
445445 const gop = try zig_object.anon_decls.getOrPut(gpa, decl_val);
446446 if (!gop.found_existing) {
447 const mod = wasm_file.base.comp.module.?;
448 const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
449 const tv: TypedValue = .{ .ty = ty, .val = Value.fromInterned(decl_val) };
450447 var name_buf: [32]u8 = undefined;
451448 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
452449 @intFromEnum(decl_val),
453450 }) catch unreachable;
454451
455 switch (try zig_object.lowerConst(wasm_file, name, tv, src_loc)) {
452 switch (try zig_object.lowerConst(wasm_file, name, Value.fromInterned(decl_val), src_loc)) {
456453 .ok => |atom_index| zig_object.anon_decls.values()[gop.index] = atom_index,
457454 .fail => |em| return .{ .fail = em },
458455 }
......@@ -472,10 +469,10 @@ pub fn lowerAnonDecl(
472469/// Lowers a constant typed value to a local symbol and atom.
473470/// Returns the symbol index of the local
474471/// The given `decl` is the parent decl whom owns the constant.
475pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {
472pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, val: Value, decl_index: InternPool.DeclIndex) !u32 {
476473 const gpa = wasm_file.base.comp.gpa;
477474 const mod = wasm_file.base.comp.module.?;
478 std.debug.assert(tv.ty.zigTypeTag(mod) != .Fn); // cannot create local symbols for functions
475 std.debug.assert(val.typeOf(mod).zigTypeTag(mod) != .Fn); // cannot create local symbols for functions
479476 const decl = mod.declPtr(decl_index);
480477
481478 const parent_atom_index = try zig_object.getOrCreateAtomForDecl(wasm_file, decl_index);
......@@ -487,7 +484,7 @@ pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, tv: TypedValu
487484 });
488485 defer gpa.free(name);
489486
490 switch (try zig_object.lowerConst(wasm_file, name, tv, decl.srcLoc(mod))) {
487 switch (try zig_object.lowerConst(wasm_file, name, val, decl.srcLoc(mod))) {
491488 .ok => |atom_index| {
492489 try wasm_file.getAtomPtr(parent_atom_index).locals.append(gpa, atom_index);
493490 return @intFromEnum(wasm_file.getAtom(atom_index).sym_index);
......@@ -505,10 +502,12 @@ const LowerConstResult = union(enum) {
505502 fail: *Module.ErrorMsg,
506503};
507504
508fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: TypedValue, src_loc: Module.SrcLoc) !LowerConstResult {
505fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, val: Value, src_loc: Module.SrcLoc) !LowerConstResult {
509506 const gpa = wasm_file.base.comp.gpa;
510507 const mod = wasm_file.base.comp.module.?;
511508
509 const ty = val.typeOf(mod);
510
512511 // Create and initialize a new local symbol and atom
513512 const sym_index = try zig_object.allocateSymbol(gpa);
514513 const atom_index = try wasm_file.createAtom(sym_index, zig_object.index);
......@@ -517,7 +516,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
517516
518517 const code = code: {
519518 const atom = wasm_file.getAtomPtr(atom_index);
520 atom.alignment = tv.ty.abiAlignment(mod);
519 atom.alignment = ty.abiAlignment(mod);
521520 const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name });
522521 errdefer gpa.free(segment_name);
523522 zig_object.symbol(sym_index).* = .{
......@@ -527,7 +526,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
527526 .index = try zig_object.createDataSegment(
528527 gpa,
529528 segment_name,
530 tv.ty.abiAlignment(mod),
529 ty.abiAlignment(mod),
531530 ),
532531 .virtual_address = undefined,
533532 };
......@@ -535,7 +534,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
535534 const result = try codegen.generateSymbol(
536535 &wasm_file.base,
537536 src_loc,
538 tv,
537 val,
539538 &value_bytes,
540539 .none,
541540 .{
......@@ -1242,7 +1241,6 @@ const Module = @import("../../Module.zig");
12421241const StringTable = @import("../StringTable.zig");
12431242const Symbol = @import("Symbol.zig");
12441243const Type = @import("../../type.zig").Type;
1245const TypedValue = @import("../../TypedValue.zig");
12461244const Value = @import("../../Value.zig");
12471245const Wasm = @import("../Wasm.zig");
12481246const ZigObject = @This();
src/type.zig-1
......@@ -7,7 +7,6 @@ const Module = @import("Module.zig");
77const Zcu = Module;
88const log = std.log.scoped(.Type);
99const target_util = @import("target.zig");
10const TypedValue = @import("TypedValue.zig");
1110const Sema = @import("Sema.zig");
1211const InternPool = @import("InternPool.zig");
1312const Alignment = InternPool.Alignment;