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 {...@@ -3678,20 +3678,21 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3678 const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };3678 const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
3679 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };3679 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
3680 const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };3680 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
3683 // Note this resolves the type of the Decl, not the value; if this Decl3684 // Note this resolves the type of the Decl, not the value; if this Decl
3684 // is a struct, for example, this resolves `type` (which needs no resolution),3685 // is a struct, for example, this resolves `type` (which needs no resolution),
3685 // not the struct itself.3686 // not the struct itself.
3686 try sema.resolveTypeLayout(decl_tv.ty);3687 try sema.resolveTypeLayout(decl_ty);
36873688
3688 if (decl.kind == .@"usingnamespace") {3689 if (decl.kind == .@"usingnamespace") {
3689 if (!decl_tv.ty.eql(Type.type, mod)) {3690 if (!decl_ty.eql(Type.type, mod)) {
3690 return sema.fail(&block_scope, ty_src, "expected type, found {}", .{3691 return sema.fail(&block_scope, ty_src, "expected type, found {}", .{
3691 decl_tv.ty.fmt(mod),3692 decl_ty.fmt(mod),
3692 });3693 });
3693 }3694 }
3694 const ty = decl_tv.val.toType();3695 const ty = decl_val.toType();
3695 if (ty.getNamespace(mod) == null) {3696 if (ty.getNamespace(mod) == null) {
3696 return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(mod)});3697 return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(mod)});
3697 }3698 }
...@@ -3713,10 +3714,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3713,10 +3714,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3713 var queue_linker_work = true;3714 var queue_linker_work = true;
3714 var is_func = false;3715 var is_func = false;
3715 var is_inline = false;3716 var is_inline = false;
3716 switch (decl_tv.val.toIntern()) {3717 switch (decl_val.toIntern()) {
3717 .generic_poison => unreachable,3718 .generic_poison => unreachable,
3718 .unreachable_value => unreachable,3719 .unreachable_value => unreachable,
3719 else => switch (ip.indexToKey(decl_tv.val.toIntern())) {3720 else => switch (ip.indexToKey(decl_val.toIntern())) {
3720 .variable => |variable| {3721 .variable => |variable| {
3721 decl.owns_tv = variable.decl == decl_index;3722 decl.owns_tv = variable.decl == decl_index;
3722 queue_linker_work = decl.owns_tv;3723 queue_linker_work = decl.owns_tv;
...@@ -3731,7 +3732,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3731,7 +3732,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3731 .func => |func| {3732 .func => |func| {
3732 decl.owns_tv = func.owner_decl == decl_index;3733 decl.owns_tv = func.owner_decl == decl_index;
3733 queue_linker_work = false;3734 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;
3735 is_func = decl.owns_tv;3736 is_func = decl.owns_tv;
3736 },3737 },
37373738
...@@ -3739,7 +3740,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3739,7 +3740,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3739 },3740 },
3740 }3741 }
37413742
3742 decl.val = decl_tv.val;3743 decl.val = decl_val;
3743 // Function linksection, align, and addrspace were already set by Sema3744 // Function linksection, align, and addrspace were already set by Sema
3744 if (!is_func) {3745 if (!is_func) {
3745 decl.alignment = blk: {3746 decl.alignment = blk: {
...@@ -3762,7 +3763,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3762,7 +3763,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3762 break :blk section.toOptional();3763 break :blk section.toOptional();
3763 };3764 };
3764 decl.@"addrspace" = blk: {3765 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())) {
3766 .variable => .variable,3767 .variable => .variable,
3767 .extern_func, .func => .function,3768 .extern_func, .func => .function,
3768 else => .constant,3769 else => .constant,
...@@ -3784,10 +3785,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3784,10 +3785,10 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3784 decl.analysis = .complete;3785 decl.analysis = .complete;
37853786
3786 const result: SemaDeclResult = if (old_has_tv) .{3787 const result: SemaDeclResult = if (old_has_tv) .{
3787 .invalidate_decl_val = !decl_tv.ty.eql(old_ty, mod) or3788 .invalidate_decl_val = !decl_ty.eql(old_ty, mod) or
3788 !decl.val.eql(old_val, decl_tv.ty, mod) or3789 !decl.val.eql(old_val, decl_ty, mod) or
3789 is_inline != old_is_inline,3790 is_inline != old_is_inline,
3790 .invalidate_decl_ref = !decl_tv.ty.eql(old_ty, mod) or3791 .invalidate_decl_ref = !decl_ty.eql(old_ty, mod) or
3791 decl.alignment != old_align or3792 decl.alignment != old_align or
3792 decl.@"linksection" != old_linksection or3793 decl.@"linksection" != old_linksection or
3793 decl.@"addrspace" != old_addrspace or3794 decl.@"addrspace" != old_addrspace or
...@@ -3797,11 +3798,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {...@@ -3797,11 +3798,11 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !SemaDeclResult {
3797 .invalidate_decl_ref = true,3798 .invalidate_decl_ref = true,
3798 };3799 };
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));
3801 if (has_runtime_bits) {3802 if (has_runtime_bits) {
3802 // Needed for codegen_decl which will call updateDecl and then the3803 // Needed for codegen_decl which will call updateDecl and then the
3803 // codegen backend wants full access to the Decl Type.3804 // codegen backend wants full access to the Decl Type.
3804 try sema.resolveTypeFully(decl_tv.ty);3805 try sema.resolveTypeFully(decl_ty);
38053806
3806 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });3807 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
38073808
src/Sema.zig+10-19
...@@ -175,7 +175,6 @@ const Sema = @This();...@@ -175,7 +175,6 @@ const Sema = @This();
175const Value = @import("Value.zig");175const Value = @import("Value.zig");
176const MutableValue = @import("mutable_value.zig").MutableValue;176const MutableValue = @import("mutable_value.zig").MutableValue;
177const Type = @import("type.zig").Type;177const Type = @import("type.zig").Type;
178const TypedValue = @import("TypedValue.zig");
179const Air = @import("Air.zig");178const Air = @import("Air.zig");
180const Zir = std.zig.Zir;179const Zir = std.zig.Zir;
181const Module = @import("Module.zig");180const Module = @import("Module.zig");
...@@ -1708,7 +1707,7 @@ fn analyzeBodyInner(...@@ -1708,7 +1707,7 @@ fn analyzeBodyInner(
1708 .needed_comptime_reason = "condition in comptime branch must be comptime-known",1707 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
1709 .block_comptime_reason = block.comptime_reason,1708 .block_comptime_reason = block.comptime_reason,
1710 });1709 });
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
1713 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);1712 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
17141713
...@@ -1728,7 +1727,7 @@ fn analyzeBodyInner(...@@ -1728,7 +1727,7 @@ fn analyzeBodyInner(
1728 .needed_comptime_reason = "condition in comptime branch must be comptime-known",1727 .needed_comptime_reason = "condition in comptime branch must be comptime-known",
1729 .block_comptime_reason = block.comptime_reason,1728 .block_comptime_reason = block.comptime_reason,
1730 });1729 });
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
1733 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);1732 try sema.maybeErrorUnwrapCondbr(block, inline_body, extra.data.condition, cond_src);
1734 const old_runtime_index = block.runtime_index;1733 const old_runtime_index = block.runtime_index;
...@@ -2179,13 +2178,9 @@ fn resolveInstConst(...@@ -2179,13 +2178,9 @@ fn resolveInstConst(
2179 src: LazySrcLoc,2178 src: LazySrcLoc,
2180 zir_ref: Zir.Inst.Ref,2179 zir_ref: Zir.Inst.Ref,
2181 reason: NeededComptimeReason,2180 reason: NeededComptimeReason,
2182) CompileError!TypedValue {2181) CompileError!Value {
2183 const air_ref = try sema.resolveInst(zir_ref);2182 const air_ref = try sema.resolveInst(zir_ref);
2184 const val = try sema.resolveConstDefinedValue(block, src, air_ref, reason);2183 return sema.resolveConstDefinedValue(block, src, air_ref, reason);
2185 return .{
2186 .ty = sema.typeOf(air_ref),
2187 .val = val,
2188 };
2189}2184}
21902185
2191/// Value Tag may be `undef` or `variable`.2186/// Value Tag may be `undef` or `variable`.
...@@ -2194,7 +2189,7 @@ pub fn resolveFinalDeclValue(...@@ -2194,7 +2189,7 @@ pub fn resolveFinalDeclValue(
2194 block: *Block,2189 block: *Block,
2195 src: LazySrcLoc,2190 src: LazySrcLoc,
2196 air_ref: Air.Inst.Ref,2191 air_ref: Air.Inst.Ref,
2197) CompileError!TypedValue {2192) CompileError!Value {
2198 const val = try sema.resolveValueAllowVariables(air_ref) orelse {2193 const val = try sema.resolveValueAllowVariables(air_ref) orelse {
2199 return sema.failWithNeededComptime(block, src, .{2194 return sema.failWithNeededComptime(block, src, .{
2200 .needed_comptime_reason = "global variable initializer must be comptime-known",2195 .needed_comptime_reason = "global variable initializer must be comptime-known",
...@@ -2204,10 +2199,7 @@ pub fn resolveFinalDeclValue(...@@ -2204,10 +2199,7 @@ pub fn resolveFinalDeclValue(
2204 if (val.canMutateComptimeVarState(sema.mod)) {2199 if (val.canMutateComptimeVarState(sema.mod)) {
2205 return sema.fail(block, src, "global variable contains reference to comptime var", .{});2200 return sema.fail(block, src, "global variable contains reference to comptime var", .{});
2206 }2201 }
2207 return .{2202 return val;
2208 .ty = sema.typeOf(air_ref),
2209 .val = val,
2210 };
2211}2203}
22122204
2213fn failWithNeededComptime(sema: *Sema, block: *Block, src: LazySrcLoc, reason: NeededComptimeReason) CompileError {2205fn 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...@@ -6414,7 +6406,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
6414 const options = try sema.resolveExportOptions(block, options_src, extra.options);6406 const options = try sema.resolveExportOptions(block, options_src, extra.options);
6415 if (options.linkage == .internal)6407 if (options.linkage == .internal)
6416 return;6408 return;
6417 if (operand.val.getFunction(mod)) |function| {6409 if (operand.getFunction(mod)) |function| {
6418 const decl_index = function.owner_decl;6410 const decl_index = function.owner_decl;
6419 return sema.analyzeExport(block, src, options, decl_index);6411 return sema.analyzeExport(block, src, options, decl_index);
6420 }6412 }
...@@ -6424,7 +6416,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError...@@ -6424,7 +6416,7 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
6424 .src = src,6416 .src = src,
6425 .owner_decl = sema.owner_decl_index,6417 .owner_decl = sema.owner_decl_index,
6426 .src_decl = block.src_decl,6418 .src_decl = block.src_decl,
6427 .exported = .{ .value = operand.val.toIntern() },6419 .exported = .{ .value = operand.toIntern() },
6428 .status = .in_progress,6420 .status = .in_progress,
6429 });6421 });
6430}6422}
...@@ -25831,7 +25823,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25831,7 +25823,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25831 } else if (extra.data.bits.has_ret_ty_ref) blk: {25823 } else if (extra.data.bits.has_ret_ty_ref) blk: {
25832 const ret_ty_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);25824 const ret_ty_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
25833 extra_index += 1;25825 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, .{
25835 .needed_comptime_reason = "return type must be comptime-known",25827 .needed_comptime_reason = "return type must be comptime-known",
25836 }) catch |err| switch (err) {25828 }) catch |err| switch (err) {
25837 error.GenericPoison => {25829 error.GenericPoison => {
...@@ -25839,8 +25831,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A...@@ -25839,8 +25831,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
25839 },25831 },
25840 else => |e| return e,25832 else => |e| return e,
25841 };25833 };
25842 const ty = ret_ty_tv.val.toType();25834 break :blk ret_ty_val.toType();
25843 break :blk ty;
25844 } else Type.void;25835 } else Type.void;
2584525836
25846 const noalias_bits: u32 = if (extra.data.bits.has_any_noalias) blk: {25837 const noalias_bits: u32 = if (extra.data.bits.has_any_noalias) blk: {
src/TypedValue.zig+3-37
...@@ -1,3 +1,6 @@...@@ -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
1const std = @import("std");4const std = @import("std");
2const Type = @import("type.zig").Type;5const Type = @import("type.zig").Type;
3const Value = @import("Value.zig");6const Value = @import("Value.zig");
...@@ -12,42 +15,6 @@ const Target = std.Target;...@@ -12,42 +15,6 @@ const Target = std.Target;
12ty: Type,15ty: Type,
13val: Value,16val: 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
51const max_aggregate_items = 100;18const max_aggregate_items = 100;
52const max_string_len = 256;19const max_string_len = 256;
5320
...@@ -72,7 +39,6 @@ pub fn format(...@@ -72,7 +39,6 @@ pub fn format(
72 };39 };
73}40}
7441
75/// Prints the Value according to the Type, not according to the Value Tag.
76pub fn print(42pub fn print(
77 tv: TypedValue,43 tv: TypedValue,
78 writer: anytype,44 writer: anytype,
src/Value.zig+3-3
...@@ -249,12 +249,12 @@ pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, opt_sema: ?*Sema) !?u64...@@ -249,12 +249,12 @@ pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, opt_sema: ?*Sema) !?u64
249 .int => |int| Value.fromInterned(int).getUnsignedIntAdvanced(mod, opt_sema),249 .int => |int| Value.fromInterned(int).getUnsignedIntAdvanced(mod, opt_sema),
250 .elem => |elem| {250 .elem => |elem| {
251 const base_addr = (try Value.fromInterned(elem.base).getUnsignedIntAdvanced(mod, opt_sema)) orelse return null;251 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);
253 return base_addr + elem.index * elem_ty.abiSize(mod);253 return base_addr + elem.index * elem_ty.abiSize(mod);
254 },254 },
255 .field => |field| {255 .field => |field| {
256 const base_addr = (try Value.fromInterned(field.base).getUnsignedIntAdvanced(mod, opt_sema)) orelse return null;256 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);
258 if (opt_sema) |sema| try sema.resolveTypeLayout(struct_ty);258 if (opt_sema) |sema| try sema.resolveTypeLayout(struct_ty);
259 return base_addr + struct_ty.structFieldOffset(@as(usize, @intCast(field.index)), mod);259 return base_addr + struct_ty.structFieldOffset(@as(usize, @intCast(field.index)), mod);
260 },260 },
...@@ -1390,7 +1390,7 @@ pub fn elemPtr(...@@ -1390,7 +1390,7 @@ pub fn elemPtr(
1390 };1390 };
1391 switch (mod.intern_pool.indexToKey(ptr_val.toIntern())) {1391 switch (mod.intern_pool.indexToKey(ptr_val.toIntern())) {
1392 .ptr => |ptr| switch (ptr.addr) {1392 .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))
1394 return Value.fromInterned((try mod.intern(.{ .ptr = .{1394 return Value.fromInterned((try mod.intern(.{ .ptr = .{
1395 .ty = elem_ptr_ty.toIntern(),1395 .ty = elem_ptr_ty.toIntern(),
1396 .addr = .{ .elem = .{1396 .addr = .{ .elem = .{
src/arch/aarch64/CodeGen.zig+3-7
...@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");...@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");
10const Liveness = @import("../../Liveness.zig");10const Liveness = @import("../../Liveness.zig");
11const Type = @import("../../type.zig").Type;11const Type = @import("../../type.zig").Type;
12const Value = @import("../../Value.zig");12const Value = @import("../../Value.zig");
13const TypedValue = @import("../../TypedValue.zig");
14const link = @import("../../link.zig");13const link = @import("../../link.zig");
15const Module = @import("../../Module.zig");14const Module = @import("../../Module.zig");
16const InternPool = @import("../../InternPool.zig");15const InternPool = @import("../../InternPool.zig");
...@@ -6143,10 +6142,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {...@@ -6143,10 +6142,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
6143 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))6142 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))
6144 return MCValue{ .none = {} };6143 return MCValue{ .none = {} };
61456144
6146 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{6145 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
6147 .ty = inst_ty,
6148 .val = (try self.air.value(inst, mod)).?,
6149 });
61506146
6151 return self.getResolvedInstValue(inst_index);6147 return self.getResolvedInstValue(inst_index);
6152}6148}
...@@ -6163,11 +6159,11 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {...@@ -6163,11 +6159,11 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
6163 }6159 }
6164}6160}
61656161
6166fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {6162fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
6167 const mcv: MCValue = switch (try codegen.genTypedValue(6163 const mcv: MCValue = switch (try codegen.genTypedValue(
6168 self.bin_file,6164 self.bin_file,
6169 self.src_loc,6165 self.src_loc,
6170 arg_tv,6166 val,
6171 self.owner_decl,6167 self.owner_decl,
6172 )) {6168 )) {
6173 .mcv => |mcv| switch (mcv) {6169 .mcv => |mcv| switch (mcv) {
src/arch/arm/CodeGen.zig+3-7
...@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");...@@ -10,7 +10,6 @@ const Emit = @import("Emit.zig");
10const Liveness = @import("../../Liveness.zig");10const Liveness = @import("../../Liveness.zig");
11const Type = @import("../../type.zig").Type;11const Type = @import("../../type.zig").Type;
12const Value = @import("../../Value.zig");12const Value = @import("../../Value.zig");
13const TypedValue = @import("../../TypedValue.zig");
14const link = @import("../../link.zig");13const link = @import("../../link.zig");
15const Module = @import("../../Module.zig");14const Module = @import("../../Module.zig");
16const InternPool = @import("../../InternPool.zig");15const InternPool = @import("../../InternPool.zig");
...@@ -6097,10 +6096,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {...@@ -6097,10 +6096,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
6097 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))6096 if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod))
6098 return MCValue{ .none = {} };6097 return MCValue{ .none = {} };
60996098
6100 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{6099 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
6101 .ty = inst_ty,
6102 .val = (try self.air.value(inst, mod)).?,
6103 });
61046100
6105 return self.getResolvedInstValue(inst_index);6101 return self.getResolvedInstValue(inst_index);
6106}6102}
...@@ -6117,12 +6113,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {...@@ -6117,12 +6113,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
6117 }6113 }
6118}6114}
61196115
6120fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {6116fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
6121 const mod = self.bin_file.comp.module.?;6117 const mod = self.bin_file.comp.module.?;
6122 const mcv: MCValue = switch (try codegen.genTypedValue(6118 const mcv: MCValue = switch (try codegen.genTypedValue(
6123 self.bin_file,6119 self.bin_file,
6124 self.src_loc,6120 self.src_loc,
6125 arg_tv,6121 val,
6126 mod.funcOwnerDeclIndex(self.func_index),6122 mod.funcOwnerDeclIndex(self.func_index),
6127 )) {6123 )) {
6128 .mcv => |mcv| switch (mcv) {6124 .mcv => |mcv| switch (mcv) {
src/arch/riscv64/CodeGen.zig+3-7
...@@ -9,7 +9,6 @@ const Emit = @import("Emit.zig");...@@ -9,7 +9,6 @@ const Emit = @import("Emit.zig");
9const Liveness = @import("../../Liveness.zig");9const Liveness = @import("../../Liveness.zig");
10const Type = @import("../../type.zig").Type;10const Type = @import("../../type.zig").Type;
11const Value = @import("../../Value.zig");11const Value = @import("../../Value.zig");
12const TypedValue = @import("../../TypedValue.zig");
13const link = @import("../../link.zig");12const link = @import("../../link.zig");
14const Module = @import("../../Module.zig");13const Module = @import("../../Module.zig");
15const InternPool = @import("../../InternPool.zig");14const InternPool = @import("../../InternPool.zig");
...@@ -2552,10 +2551,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {...@@ -2552,10 +2551,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
2552 if (!inst_ty.hasRuntimeBits(mod))2551 if (!inst_ty.hasRuntimeBits(mod))
2553 return MCValue{ .none = {} };2552 return MCValue{ .none = {} };
25542553
2555 const inst_index = inst.toIndex() orelse return self.genTypedValue(.{2554 const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?);
2556 .ty = inst_ty,
2557 .val = (try self.air.value(inst, mod)).?,
2558 });
25592555
2560 return self.getResolvedInstValue(inst_index);2556 return self.getResolvedInstValue(inst_index);
2561}2557}
...@@ -2572,12 +2568,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {...@@ -2572,12 +2568,12 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue {
2572 }2568 }
2573}2569}
25742570
2575fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {2571fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
2576 const mod = self.bin_file.comp.module.?;2572 const mod = self.bin_file.comp.module.?;
2577 const mcv: MCValue = switch (try codegen.genTypedValue(2573 const mcv: MCValue = switch (try codegen.genTypedValue(
2578 self.bin_file,2574 self.bin_file,
2579 self.src_loc,2575 self.src_loc,
2580 typed_value,2576 val,
2581 mod.funcOwnerDeclIndex(self.func_index),2577 mod.funcOwnerDeclIndex(self.func_index),
2582 )) {2578 )) {
2583 .mcv => |mcv| switch (mcv) {2579 .mcv => |mcv| switch (mcv) {
src/arch/sparc64/CodeGen.zig+4-7
...@@ -12,7 +12,7 @@ const builtin = @import("builtin");...@@ -12,7 +12,7 @@ const builtin = @import("builtin");
12const link = @import("../../link.zig");12const link = @import("../../link.zig");
13const Module = @import("../../Module.zig");13const Module = @import("../../Module.zig");
14const InternPool = @import("../../InternPool.zig");14const InternPool = @import("../../InternPool.zig");
15const TypedValue = @import("../../TypedValue.zig");15const Value = @import("../../Value.zig");
16const ErrorMsg = Module.ErrorMsg;16const ErrorMsg = Module.ErrorMsg;
17const codegen = @import("../../codegen.zig");17const codegen = @import("../../codegen.zig");
18const Air = @import("../../Air.zig");18const Air = @import("../../Air.zig");
...@@ -4118,12 +4118,12 @@ fn genStoreASI(self: *Self, value_reg: Register, addr_reg: Register, off_reg: Re...@@ -4118,12 +4118,12 @@ fn genStoreASI(self: *Self, value_reg: Register, addr_reg: Register, off_reg: Re
4118 }4118 }
4119}4119}
41204120
4121fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {4121fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
4122 const mod = self.bin_file.comp.module.?;4122 const mod = self.bin_file.comp.module.?;
4123 const mcv: MCValue = switch (try codegen.genTypedValue(4123 const mcv: MCValue = switch (try codegen.genTypedValue(
4124 self.bin_file,4124 self.bin_file,
4125 self.src_loc,4125 self.src_loc,
4126 typed_value,4126 val,
4127 mod.funcOwnerDeclIndex(self.func_index),4127 mod.funcOwnerDeclIndex(self.func_index),
4128 )) {4128 )) {
4129 .mcv => |mcv| switch (mcv) {4129 .mcv => |mcv| switch (mcv) {
...@@ -4546,10 +4546,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {...@@ -4546,10 +4546,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
4546 return self.getResolvedInstValue(inst);4546 return self.getResolvedInstValue(inst);
4547 }4547 }
45484548
4549 return self.genTypedValue(.{4549 return self.genTypedValue((try self.air.value(ref, mod)).?);
4550 .ty = ty,
4551 .val = (try self.air.value(ref, mod)).?,
4552 });
4553}4550}
45544551
4555fn ret(self: *Self, mcv: MCValue) !void {4552fn ret(self: *Self, mcv: MCValue) !void {
src/arch/wasm/CodeGen.zig+14-18
...@@ -18,7 +18,6 @@ const Value = @import("../../Value.zig");...@@ -18,7 +18,6 @@ const Value = @import("../../Value.zig");
18const Compilation = @import("../../Compilation.zig");18const Compilation = @import("../../Compilation.zig");
19const LazySrcLoc = std.zig.LazySrcLoc;19const LazySrcLoc = std.zig.LazySrcLoc;
20const link = @import("../../link.zig");20const link = @import("../../link.zig");
21const TypedValue = @import("../../TypedValue.zig");
22const Air = @import("../../Air.zig");21const Air = @import("../../Air.zig");
23const Liveness = @import("../../Liveness.zig");22const Liveness = @import("../../Liveness.zig");
24const target_util = @import("../../target.zig");23const target_util = @import("../../target.zig");
...@@ -805,7 +804,7 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {...@@ -805,7 +804,7 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue {
805 // In the other cases, we will simply lower the constant to a value that fits804 // In the other cases, we will simply lower the constant to a value that fits
806 // into a single local (such as a pointer, integer, bool, etc).805 // into a single local (such as a pointer, integer, bool, etc).
807 const result = if (isByRef(ty, mod)) blk: {806 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);
809 break :blk WValue{ .memory = sym_index };808 break :blk WValue{ .memory = sym_index };
810 } else try func.lowerConstant(val, ty);809 } else try func.lowerConstant(val, ty);
811810
...@@ -3119,10 +3118,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, offset: u32) InnerError!WValue...@@ -3119,10 +3118,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, offset: u32) InnerError!WValue
3119}3118}
31203119
3121fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {3120fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue {
3122 const mod = func.bin_file.base.comp.module.?;3121 return func.lowerDeclRefValue(ptr_val, decl_index, offset);
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);
3126}3122}
31273123
3128fn lowerAnonDeclRef(3124fn lowerAnonDeclRef(
...@@ -3157,7 +3153,7 @@ fn lowerAnonDeclRef(...@@ -3157,7 +3153,7 @@ fn lowerAnonDeclRef(
3157 } else return WValue{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } };3153 } else return WValue{ .memory_offset = .{ .pointer = target_sym_index, .offset = offset } };
3158}3154}
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 {
3161 const mod = func.bin_file.base.comp.module.?;3157 const mod = func.bin_file.base.comp.module.?;
31623158
3163 const decl = mod.declPtr(decl_index);3159 const decl = mod.declPtr(decl_index);
...@@ -3165,11 +3161,11 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.Decl...@@ -3165,11 +3161,11 @@ fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: InternPool.Decl
3165 // want to lower the actual decl, rather than the alias itself.3161 // want to lower the actual decl, rather than the alias itself.
3166 if (decl.val.getFunction(mod)) |func_val| {3162 if (decl.val.getFunction(mod)) |func_val| {
3167 if (func_val.owner_decl != decl_index) {3163 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);
3169 }3165 }
3170 } else if (decl.val.getExternFunc(mod)) |func_val| {3166 } else if (decl.val.getExternFunc(mod)) |func_val| {
3171 if (func_val.decl != decl_index) {3167 if (func_val.decl != decl_index) {
3172 return func.lowerDeclRefValue(tv, func_val.decl, offset);3168 return func.lowerDeclRefValue(val, func_val.decl, offset);
3173 }3169 }
3174 }3170 }
3175 const decl_ty = decl.typeOf(mod);3171 const decl_ty = decl.typeOf(mod);
...@@ -3280,23 +3276,23 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {...@@ -3280,23 +3276,23 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
3280 },3276 },
3281 .error_union => |error_union| {3277 .error_union => |error_union| {
3282 const err_int_ty = try mod.errorIntType();3278 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) {
3284 .err_name => |err_name| .{3280 .err_name => |err_name| .{
3285 .ty = ty.errorUnionSet(mod),3281 ty.errorUnionSet(mod),
3286 .val = Value.fromInterned((try mod.intern(.{ .err = .{3282 Value.fromInterned((try mod.intern(.{ .err = .{
3287 .ty = ty.errorUnionSet(mod).toIntern(),3283 .ty = ty.errorUnionSet(mod).toIntern(),
3288 .name = err_name,3284 .name = err_name,
3289 } }))),3285 } }))),
3290 },3286 },
3291 .payload => .{3287 .payload => .{
3292 .ty = err_int_ty,3288 err_int_ty,
3293 .val = try mod.intValue(err_int_ty, 0),3289 try mod.intValue(err_int_ty, 0),
3294 },3290 },
3295 };3291 };
3296 const payload_type = ty.errorUnionPayload(mod);3292 const payload_type = ty.errorUnionPayload(mod);
3297 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {3293 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
3298 // We use the error type directly as the type.3294 // 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);
3300 }3296 }
33013297
3302 return func.fail("Wasm TODO: lowerConstant error union with non-zero-bit payload type", .{});3298 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 {...@@ -3320,10 +3316,10 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue {
3320 .elem, .field => |base_index| ptr = ip.indexToKey(base_index.base).ptr,3316 .elem, .field => |base_index| ptr = ip.indexToKey(base_index.base).ptr,
3321 .comptime_field, .comptime_alloc => unreachable,3317 .comptime_field, .comptime_alloc => unreachable,
3322 };3318 };
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) };
3324 },3320 },
3325 .ptr => |ptr| switch (ptr.addr) {3321 .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),
3327 .int => |int| return func.lowerConstant(Value.fromInterned(int), Type.fromInterned(ip.typeOf(int))),3323 .int => |int| return func.lowerConstant(Value.fromInterned(int), Type.fromInterned(ip.typeOf(int))),
3328 .opt_payload, .elem, .field => return func.lowerParentPtr(val, 0),3324 .opt_payload, .elem, .field => return func.lowerParentPtr(val, 0),
3329 .anon_decl => |ad| return func.lowerAnonDeclRef(ad, 0),3325 .anon_decl => |ad| return func.lowerAnonDeclRef(ad, 0),
...@@ -7285,7 +7281,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 {...@@ -7285,7 +7281,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 {
7285 .storage = .{ .bytes = tag_name },7281 .storage = .{ .bytes = tag_name },
7286 } });7282 } });
7287 const tag_sym_index = try func.bin_file.lowerUnnamedConst(7283 const tag_sym_index = try func.bin_file.lowerUnnamedConst(
7288 .{ .ty = name_ty, .val = Value.fromInterned(name_val) },7284 Value.fromInterned(name_val),
7289 enum_decl_index,7285 enum_decl_index,
7290 );7286 );
72917287
src/arch/x86_64/CodeGen.zig+38-63
...@@ -32,7 +32,6 @@ const InternPool = @import("../../InternPool.zig");...@@ -32,7 +32,6 @@ const InternPool = @import("../../InternPool.zig");
32const Alignment = InternPool.Alignment;32const Alignment = InternPool.Alignment;
33const Target = std.Target;33const Target = std.Target;
34const Type = @import("../../type.zig").Type;34const Type = @import("../../type.zig").Type;
35const TypedValue = @import("../../TypedValue.zig");
36const Value = @import("../../Value.zig");35const Value = @import("../../Value.zig");
37const Instruction = @import("encoder.zig").Instruction;36const Instruction = @import("encoder.zig").Instruction;
3837
...@@ -2250,7 +2249,7 @@ fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void {...@@ -2250,7 +2249,7 @@ fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void {
2250 for (exitlude_jump_relocs, 0..) |*exitlude_jump_reloc, tag_index| {2249 for (exitlude_jump_relocs, 0..) |*exitlude_jump_reloc, tag_index| {
2251 const tag_name_len = ip.stringToSlice(tag_names.get(ip)[tag_index]).len;2250 const tag_name_len = ip.stringToSlice(tag_names.get(ip)[tag_index]).len;
2252 const tag_val = try mod.enumValueFieldIndex(enum_ty, @intCast(tag_index));2251 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);
2254 try self.genBinOpMir(.{ ._, .cmp }, enum_ty, enum_mcv, tag_mcv);2253 try self.genBinOpMir(.{ ._, .cmp }, enum_ty, enum_mcv, tag_mcv);
2255 const skip_reloc = try self.asmJccReloc(.ne, undefined);2254 const skip_reloc = try self.asmJccReloc(.ne, undefined);
22562255
...@@ -3323,7 +3322,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {...@@ -3323,7 +3322,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void {
3323 .storage = .{ .repeated_elem = mask_val.ip_index },3322 .storage = .{ .repeated_elem = mask_val.ip_index },
3324 } });3323 } });
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));
3327 const splat_addr_mcv: MCValue = switch (splat_mcv) {3326 const splat_addr_mcv: MCValue = switch (splat_mcv) {
3328 .memory, .indirect, .load_frame => splat_mcv.address(),3327 .memory, .indirect, .load_frame => splat_mcv.address(),
3329 else => .{ .register = try self.copyToTmpRegister(Type.usize, splat_mcv.address()) },3328 else => .{ .register = try self.copyToTmpRegister(Type.usize, splat_mcv.address()) },
...@@ -4992,17 +4991,14 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void {...@@ -4992,17 +4991,14 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void {
4992 defer self.register_manager.unlockReg(shift_lock);4991 defer self.register_manager.unlockReg(shift_lock);
49934992
4994 const mask_ty = try mod.vectorType(.{ .len = 16, .child = .u8_type });4993 const mask_ty = try mod.vectorType(.{ .len = 16, .child = .u8_type });
4995 const mask_mcv = try self.genTypedValue(.{4994 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
4996 .ty = mask_ty,4995 .ty = mask_ty.toIntern(),
4997 .val = Value.fromInterned((try mod.intern(.{ .aggregate = .{4996 .storage = .{ .elems = &([1]InternPool.Index{
4998 .ty = mask_ty.toIntern(),4997 (try rhs_ty.childType(mod).maxIntScalar(mod, Type.u8)).toIntern(),
4999 .storage = .{ .elems = &([1]InternPool.Index{4998 } ++ [1]InternPool.Index{
5000 (try rhs_ty.childType(mod).maxIntScalar(mod, Type.u8)).toIntern(),4999 (try mod.intValue(Type.u8, 0)).toIntern(),
5001 } ++ [1]InternPool.Index{5000 } ** 15) },
5002 (try mod.intValue(Type.u8, 0)).toIntern(),5001 } })));
5003 } ** 15) },
5004 } }))),
5005 });
5006 const mask_addr_reg =5002 const mask_addr_reg =
5007 try self.copyToTmpRegister(Type.usize, mask_mcv.address());5003 try self.copyToTmpRegister(Type.usize, mask_mcv.address());
5008 const mask_addr_lock = self.register_manager.lockRegAssumeUnused(mask_addr_reg);5004 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)...@@ -6860,11 +6856,11 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type)
6860 .child = (try mod.intType(.signed, scalar_bits)).ip_index,6856 .child = (try mod.intType(.signed, scalar_bits)).ip_index,
6861 });6857 });
68626858
6863 const sign_mcv = try self.genTypedValue(.{ .ty = vec_ty, .val = switch (tag) {6859 const sign_mcv = try self.genTypedValue(switch (tag) {
6864 .neg => try vec_ty.minInt(mod, vec_ty),6860 .neg => try vec_ty.minInt(mod, vec_ty),
6865 .abs => try vec_ty.maxInt(mod, vec_ty),6861 .abs => try vec_ty.maxInt(mod, vec_ty),
6866 else => unreachable,6862 else => unreachable,
6867 } });6863 });
6868 const sign_mem: Memory = if (sign_mcv.isMemory())6864 const sign_mem: Memory = if (sign_mcv.isMemory())
6869 try sign_mcv.mem(self, Memory.Size.fromSize(abi_size))6865 try sign_mcv.mem(self, Memory.Size.fromSize(abi_size))
6870 else6866 else
...@@ -11130,10 +11126,7 @@ fn genBinOp(...@@ -11130,10 +11126,7 @@ fn genBinOp(
11130 .cmp_neq,11126 .cmp_neq,
11131 => {11127 => {
11132 const unsigned_ty = try lhs_ty.toUnsigned(mod);11128 const unsigned_ty = try lhs_ty.toUnsigned(mod);
11133 const not_mcv = try self.genTypedValue(.{11129 const not_mcv = try self.genTypedValue(try unsigned_ty.maxInt(mod, unsigned_ty));
11134 .ty = lhs_ty,
11135 .val = try unsigned_ty.maxInt(mod, unsigned_ty),
11136 });
11137 const not_mem: Memory = if (not_mcv.isMemory())11130 const not_mem: Memory = if (not_mcv.isMemory())
11138 try not_mcv.mem(self, Memory.Size.fromSize(abi_size))11131 try not_mcv.mem(self, Memory.Size.fromSize(abi_size))
11139 else11132 else
...@@ -14692,10 +14685,7 @@ fn genSetReg(...@@ -14692,10 +14685,7 @@ fn genSetReg(
14692 ),14685 ),
14693 else => unreachable,14686 else => unreachable,
14694 },14687 },
14695 .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(.{14688 .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(try mod.undefValue(ty)), opts),
14696 .ty = ty,
14697 .val = try mod.undefValue(ty),
14698 }), opts),
14699 },14689 },
14700 .eflags => |cc| try self.asmSetccRegister(cc, dst_reg.to8()),14690 .eflags => |cc| try self.asmSetccRegister(cc, dst_reg.to8()),
14701 .immediate => |imm| {14691 .immediate => |imm| {
...@@ -16893,13 +16883,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {...@@ -16893,13 +16883,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {
16893 .ty = mask_elem_ty.toIntern(),16883 .ty = mask_elem_ty.toIntern(),
16894 .storage = .{ .u64 = bit / elem_bits },16884 .storage = .{ .u64 = bit / elem_bits },
16895 } });16885 } });
16896 const mask_mcv = try self.genTypedValue(.{16886 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
16897 .ty = mask_ty,16887 .ty = mask_ty.toIntern(),
16898 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{16888 .storage = .{ .elems = mask_elems[0..vec_len] },
16899 .ty = mask_ty.toIntern(),16889 } })));
16900 .storage = .{ .elems = mask_elems[0..vec_len] },
16901 } })),
16902 });
16903 const mask_mem: Memory = .{16890 const mask_mem: Memory = .{
16904 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },16891 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },
16905 .mod = .{ .rm = .{ .size = self.memSize(ty) } },16892 .mod = .{ .rm = .{ .size = self.memSize(ty) } },
...@@ -16921,13 +16908,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {...@@ -16921,13 +16908,10 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void {
16921 .ty = mask_elem_ty.toIntern(),16908 .ty = mask_elem_ty.toIntern(),
16922 .storage = .{ .u64 = @as(u32, 1) << @intCast(bit & (elem_bits - 1)) },16909 .storage = .{ .u64 = @as(u32, 1) << @intCast(bit & (elem_bits - 1)) },
16923 } });16910 } });
16924 const mask_mcv = try self.genTypedValue(.{16911 const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
16925 .ty = mask_ty,16912 .ty = mask_ty.toIntern(),
16926 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{16913 .storage = .{ .elems = mask_elems[0..vec_len] },
16927 .ty = mask_ty.toIntern(),16914 } })));
16928 .storage = .{ .elems = mask_elems[0..vec_len] },
16929 } })),
16930 });
16931 const mask_mem: Memory = .{16915 const mask_mem: Memory = .{
16932 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },16916 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, mask_mcv.address()) },
16933 .mod = .{ .rm = .{ .size = self.memSize(ty) } },16917 .mod = .{ .rm = .{ .size = self.memSize(ty) } },
...@@ -17658,13 +17642,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {...@@ -17658,13 +17642,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
17658 else17642 else
17659 try select_mask_elem_ty.minIntScalar(mod, select_mask_elem_ty)).toIntern();17643 try select_mask_elem_ty.minIntScalar(mod, select_mask_elem_ty)).toIntern();
17660 }17644 }
17661 const select_mask_mcv = try self.genTypedValue(.{17645 const select_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17662 .ty = select_mask_ty,17646 .ty = select_mask_ty.toIntern(),
17663 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{17647 .storage = .{ .elems = select_mask_elems[0..mask_elems.len] },
17664 .ty = select_mask_ty.toIntern(),17648 } })));
17665 .storage = .{ .elems = select_mask_elems[0..mask_elems.len] },
17666 } })),
17667 });
1766817649
17669 if (self.hasFeature(.sse4_1)) {17650 if (self.hasFeature(.sse4_1)) {
17670 const mir_tag: Mir.Inst.FixedTag = .{17651 const mir_tag: Mir.Inst.FixedTag = .{
...@@ -17809,13 +17790,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {...@@ -17809,13 +17790,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
17809 } });17790 } });
17810 }17791 }
17811 const lhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });17792 const lhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });
17812 const lhs_mask_mcv = try self.genTypedValue(.{17793 const lhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17813 .ty = lhs_mask_ty,17794 .ty = lhs_mask_ty.toIntern(),
17814 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{17795 .storage = .{ .elems = lhs_mask_elems[0..max_abi_size] },
17815 .ty = lhs_mask_ty.toIntern(),17796 } })));
17816 .storage = .{ .elems = lhs_mask_elems[0..max_abi_size] },
17817 } })),
17818 });
17819 const lhs_mask_mem: Memory = .{17797 const lhs_mask_mem: Memory = .{
17820 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, lhs_mask_mcv.address()) },17798 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, lhs_mask_mcv.address()) },
17821 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },17799 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },
...@@ -17846,13 +17824,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {...@@ -17846,13 +17824,10 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void {
17846 } });17824 } });
17847 }17825 }
17848 const rhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });17826 const rhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type });
17849 const rhs_mask_mcv = try self.genTypedValue(.{17827 const rhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{
17850 .ty = rhs_mask_ty,17828 .ty = rhs_mask_ty.toIntern(),
17851 .val = Value.fromInterned(try mod.intern(.{ .aggregate = .{17829 .storage = .{ .elems = rhs_mask_elems[0..max_abi_size] },
17852 .ty = rhs_mask_ty.toIntern(),17830 } })));
17853 .storage = .{ .elems = rhs_mask_elems[0..max_abi_size] },
17854 } })),
17855 });
17856 const rhs_mask_mem: Memory = .{17831 const rhs_mask_mem: Memory = .{
17857 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, rhs_mask_mcv.address()) },17832 .base = .{ .reg = try self.copyToTmpRegister(Type.usize, rhs_mask_mcv.address()) },
17858 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },17833 .mod = .{ .rm = .{ .size = Memory.Size.fromSize(@max(max_abi_size, 16)) } },
...@@ -18138,7 +18113,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {...@@ -18138,7 +18113,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
18138 .{ .frame = frame_index },18113 .{ .frame = frame_index },
18139 @intCast(elem_size * elements.len),18114 @intCast(elem_size * elements.len),
18140 elem_ty,18115 elem_ty,
18141 try self.genTypedValue(.{ .ty = elem_ty, .val = sentinel }),18116 try self.genTypedValue(sentinel),
18142 .{},18117 .{},
18143 );18118 );
18144 break :result .{ .load_frame = .{ .index = frame_index } };18119 break :result .{ .load_frame = .{ .index = frame_index } };
...@@ -18662,7 +18637,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {...@@ -18662,7 +18637,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue {
18662 const ip_index = ref.toInterned().?;18637 const ip_index = ref.toInterned().?;
18663 const gop = try self.const_tracking.getOrPut(self.gpa, ip_index);18638 const gop = try self.const_tracking.getOrPut(self.gpa, ip_index);
18664 if (!gop.found_existing) gop.value_ptr.* = InstTracking.init(init: {18639 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));
18666 switch (const_mcv) {18641 switch (const_mcv) {
18667 .lea_tlv => |tlv_sym| switch (self.bin_file.tag) {18642 .lea_tlv => |tlv_sym| switch (self.bin_file.tag) {
18668 .elf, .macho => {18643 .elf, .macho => {
...@@ -18727,9 +18702,9 @@ fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCV...@@ -18727,9 +18702,9 @@ fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCV
18727 return mcv;18702 return mcv;
18728}18703}
1872918704
18730fn genTypedValue(self: *Self, arg_tv: TypedValue) InnerError!MCValue {18705fn genTypedValue(self: *Self, val: Value) InnerError!MCValue {
18731 const mod = self.bin_file.comp.module.?;18706 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))) {
18733 .mcv => |mcv| switch (mcv) {18708 .mcv => |mcv| switch (mcv) {
18734 .none => .none,18709 .none => .none,
18735 .undef => .undef,18710 .undef => .undef,
src/codegen.zig+120-168
...@@ -19,7 +19,6 @@ const Liveness = @import("Liveness.zig");...@@ -19,7 +19,6 @@ const Liveness = @import("Liveness.zig");
19const Module = @import("Module.zig");19const Module = @import("Module.zig");
20const Target = std.Target;20const Target = std.Target;
21const Type = @import("type.zig").Type;21const Type = @import("type.zig").Type;
22const TypedValue = @import("TypedValue.zig");
23const Value = @import("Value.zig");22const Value = @import("Value.zig");
24const Zir = std.zig.Zir;23const Zir = std.zig.Zir;
25const Alignment = InternPool.Alignment;24const Alignment = InternPool.Alignment;
...@@ -171,7 +170,7 @@ pub fn generateLazySymbol(...@@ -171,7 +170,7 @@ pub fn generateLazySymbol(
171pub fn generateSymbol(170pub fn generateSymbol(
172 bin_file: *link.File,171 bin_file: *link.File,
173 src_loc: Module.SrcLoc,172 src_loc: Module.SrcLoc,
174 arg_tv: TypedValue,173 val: Value,
175 code: *std.ArrayList(u8),174 code: *std.ArrayList(u8),
176 debug_output: DebugInfoOutput,175 debug_output: DebugInfoOutput,
177 reloc_info: RelocInfo,176 reloc_info: RelocInfo,
...@@ -181,23 +180,22 @@ pub fn generateSymbol(...@@ -181,23 +180,22 @@ pub fn generateSymbol(
181180
182 const mod = bin_file.comp.module.?;181 const mod = bin_file.comp.module.?;
183 const ip = &mod.intern_pool;182 const ip = &mod.intern_pool;
184 const typed_value = arg_tv;183 const ty = val.typeOf(mod);
185184
186 const target = mod.getTarget();185 const target = mod.getTarget();
187 const endian = target.cpu.arch.endian();186 const endian = target.cpu.arch.endian();
188187
189 log.debug("generateSymbol: ty = {}, val = {}", .{188 log.debug("generateSymbol: val = {}", .{
190 typed_value.ty.fmt(mod),189 val.fmtValue(ty, mod),
191 typed_value.val.fmtValue(typed_value.ty, mod),
192 });190 });
193191
194 if (typed_value.val.isUndefDeep(mod)) {192 if (val.isUndefDeep(mod)) {
195 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;193 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow;
196 try code.appendNTimes(0xaa, abi_size);194 try code.appendNTimes(0xaa, abi_size);
197 return .ok;195 return .ok;
198 }196 }
199197
200 switch (ip.indexToKey(typed_value.val.toIntern())) {198 switch (ip.indexToKey(val.toIntern())) {
201 .int_type,199 .int_type,
202 .ptr_type,200 .ptr_type,
203 .array_type,201 .array_type,
...@@ -238,17 +236,17 @@ pub fn generateSymbol(...@@ -238,17 +236,17 @@ pub fn generateSymbol(
238 .empty_enum_value,236 .empty_enum_value,
239 => unreachable, // non-runtime values237 => unreachable, // non-runtime values
240 .int => {238 .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;
242 var space: Value.BigIntSpace = undefined;240 var space: Value.BigIntSpace = undefined;
243 const val = typed_value.val.toBigInt(&space, mod);241 const int_val = val.toBigInt(&space, mod);
244 val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian);242 int_val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian);
245 },243 },
246 .err => |err| {244 .err => |err| {
247 const int = try mod.getErrorValue(err.name);245 const int = try mod.getErrorValue(err.name);
248 try code.writer().writeInt(u16, @as(u16, @intCast(int)), endian);246 try code.writer().writeInt(u16, @as(u16, @intCast(int)), endian);
249 },247 },
250 .error_union => |error_union| {248 .error_union => |error_union| {
251 const payload_ty = typed_value.ty.errorUnionPayload(mod);249 const payload_ty = ty.errorUnionPayload(mod);
252 const err_val = switch (error_union.val) {250 const err_val = switch (error_union.val) {
253 .err_name => |err_name| @as(u16, @intCast(try mod.getErrorValue(err_name))),251 .err_name => |err_name| @as(u16, @intCast(try mod.getErrorValue(err_name))),
254 .payload => @as(u16, 0),252 .payload => @as(u16, 0),
...@@ -261,7 +259,7 @@ pub fn generateSymbol(...@@ -261,7 +259,7 @@ pub fn generateSymbol(
261259
262 const payload_align = payload_ty.abiAlignment(mod);260 const payload_align = payload_ty.abiAlignment(mod);
263 const error_align = Type.anyerror.abiAlignment(mod);261 const error_align = Type.anyerror.abiAlignment(mod);
264 const abi_align = typed_value.ty.abiAlignment(mod);262 const abi_align = ty.abiAlignment(mod);
265263
266 // error value first when its type is larger than the error union's payload264 // error value first when its type is larger than the error union's payload
267 if (error_align.order(payload_align) == .gt) {265 if (error_align.order(payload_align) == .gt) {
...@@ -271,13 +269,10 @@ pub fn generateSymbol(...@@ -271,13 +269,10 @@ pub fn generateSymbol(
271 // emit payload part of the error union269 // emit payload part of the error union
272 {270 {
273 const begin = code.items.len;271 const begin = code.items.len;
274 switch (try generateSymbol(bin_file, src_loc, .{272 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (error_union.val) {
275 .ty = payload_ty,273 .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }),
276 .val = Value.fromInterned(switch (error_union.val) {274 .payload => |payload| payload,
277 .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }),275 }), code, debug_output, reloc_info)) {
278 .payload => |payload| payload,
279 }),
280 }, code, debug_output, reloc_info)) {
281 .ok => {},276 .ok => {},
282 .fail => |em| return .{ .fail = em },277 .fail => |em| return .{ .fail = em },
283 }278 }
...@@ -304,11 +299,8 @@ pub fn generateSymbol(...@@ -304,11 +299,8 @@ pub fn generateSymbol(
304 }299 }
305 },300 },
306 .enum_tag => |enum_tag| {301 .enum_tag => |enum_tag| {
307 const int_tag_ty = typed_value.ty.intTagType(mod);302 const int_tag_ty = ty.intTagType(mod);
308 switch (try generateSymbol(bin_file, src_loc, .{303 switch (try generateSymbol(bin_file, src_loc, try mod.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, debug_output, reloc_info)) {
309 .ty = int_tag_ty,
310 .val = try mod.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty),
311 }, code, debug_output, reloc_info)) {
312 .ok => {},304 .ok => {},
313 .fail => |em| return .{ .fail = em },305 .fail => |em| return .{ .fail = em },
314 }306 }
...@@ -319,42 +311,33 @@ pub fn generateSymbol(...@@ -319,42 +311,33 @@ pub fn generateSymbol(
319 .f64 => |f64_val| writeFloat(f64, f64_val, target, endian, try code.addManyAsArray(8)),311 .f64 => |f64_val| writeFloat(f64, f64_val, target, endian, try code.addManyAsArray(8)),
320 .f80 => |f80_val| {312 .f80 => |f80_val| {
321 writeFloat(f80, f80_val, target, endian, try code.addManyAsArray(10));313 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;
323 try code.appendNTimes(0, abi_size - 10);315 try code.appendNTimes(0, abi_size - 10);
324 },316 },
325 .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)),317 .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)),
326 },318 },
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)) {
328 .ok => {},320 .ok => {},
329 .fail => |em| return .{ .fail = em },321 .fail => |em| return .{ .fail = em },
330 },322 },
331 .slice => |slice| {323 .slice => |slice| {
332 switch (try generateSymbol(bin_file, src_loc, .{324 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.ptr), code, debug_output, reloc_info)) {
333 .ty = typed_value.ty.slicePtrFieldType(mod),
334 .val = Value.fromInterned(slice.ptr),
335 }, code, debug_output, reloc_info)) {
336 .ok => {},325 .ok => {},
337 .fail => |em| return .{ .fail = em },326 .fail => |em| return .{ .fail = em },
338 }327 }
339 switch (try generateSymbol(bin_file, src_loc, .{328 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.len), code, debug_output, reloc_info)) {
340 .ty = Type.usize,
341 .val = Value.fromInterned(slice.len),
342 }, code, debug_output, reloc_info)) {
343 .ok => {},329 .ok => {},
344 .fail => |em| return .{ .fail = em },330 .fail => |em| return .{ .fail = em },
345 }331 }
346 },332 },
347 .opt => {333 .opt => {
348 const payload_type = typed_value.ty.optionalChild(mod);334 const payload_type = ty.optionalChild(mod);
349 const payload_val = typed_value.val.optionalValue(mod);335 const payload_val = val.optionalValue(mod);
350 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse return error.Overflow;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)) {
353 if (payload_val) |value| {339 if (payload_val) |value| {
354 switch (try generateSymbol(bin_file, src_loc, .{340 switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) {
355 .ty = payload_type,
356 .val = value,
357 }, code, debug_output, reloc_info)) {
358 .ok => {},341 .ok => {},
359 .fail => |em| return Result{ .fail = em },342 .fail => |em| return Result{ .fail = em },
360 }343 }
...@@ -365,10 +348,7 @@ pub fn generateSymbol(...@@ -365,10 +348,7 @@ pub fn generateSymbol(
365 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;348 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;
366 if (payload_type.hasRuntimeBits(mod)) {349 if (payload_type.hasRuntimeBits(mod)) {
367 const value = payload_val orelse Value.fromInterned((try mod.intern(.{ .undef = payload_type.toIntern() })));350 const value = payload_val orelse Value.fromInterned((try mod.intern(.{ .undef = payload_type.toIntern() })));
368 switch (try generateSymbol(bin_file, src_loc, .{351 switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) {
369 .ty = payload_type,
370 .val = value,
371 }, code, debug_output, reloc_info)) {
372 .ok => {},352 .ok => {},
373 .fail => |em| return Result{ .fail = em },353 .fail => |em| return Result{ .fail = em },
374 }354 }
...@@ -377,7 +357,7 @@ pub fn generateSymbol(...@@ -377,7 +357,7 @@ pub fn generateSymbol(
377 try code.appendNTimes(0, padding);357 try code.appendNTimes(0, padding);
378 }358 }
379 },359 },
380 .aggregate => |aggregate| switch (ip.indexToKey(typed_value.ty.toIntern())) {360 .aggregate => |aggregate| switch (ip.indexToKey(ty.toIntern())) {
381 .array_type => |array_type| switch (aggregate.storage) {361 .array_type => |array_type| switch (aggregate.storage) {
382 .bytes => |bytes| try code.appendSlice(bytes),362 .bytes => |bytes| try code.appendSlice(bytes),
383 .elems, .repeated_elem => {363 .elems, .repeated_elem => {
...@@ -385,17 +365,14 @@ pub fn generateSymbol(...@@ -385,17 +365,14 @@ pub fn generateSymbol(
385 const len_including_sentinel =365 const len_including_sentinel =
386 array_type.len + @intFromBool(array_type.sentinel != .none);366 array_type.len + @intFromBool(array_type.sentinel != .none);
387 while (index < len_including_sentinel) : (index += 1) {367 while (index < len_including_sentinel) : (index += 1) {
388 switch (try generateSymbol(bin_file, src_loc, .{368 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) {
389 .ty = Type.fromInterned(array_type.child),369 .bytes => unreachable,
390 .val = Value.fromInterned(switch (aggregate.storage) {370 .elems => |elems| elems[@as(usize, @intCast(index))],
391 .bytes => unreachable,371 .repeated_elem => |elem| if (index < array_type.len)
392 .elems => |elems| elems[@as(usize, @intCast(index))],372 elem
393 .repeated_elem => |elem| if (index < array_type.len)373 else
394 elem374 array_type.sentinel,
395 else375 }), code, debug_output, reloc_info)) {
396 array_type.sentinel,
397 }),
398 }, code, debug_output, reloc_info)) {
399 .ok => {},376 .ok => {},
400 .fail => |em| return .{ .fail = em },377 .fail => |em| return .{ .fail = em },
401 }378 }
...@@ -403,7 +380,7 @@ pub fn generateSymbol(...@@ -403,7 +380,7 @@ pub fn generateSymbol(
403 },380 },
404 },381 },
405 .vector_type => |vector_type| {382 .vector_type => |vector_type| {
406 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse383 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse
407 return error.Overflow;384 return error.Overflow;
408 if (vector_type.child == .bool_type) {385 if (vector_type.child == .bool_type) {
409 const bytes = try code.addManyAsSlice(abi_size);386 const bytes = try code.addManyAsSlice(abi_size);
...@@ -449,16 +426,13 @@ pub fn generateSymbol(...@@ -449,16 +426,13 @@ pub fn generateSymbol(
449 .elems, .repeated_elem => {426 .elems, .repeated_elem => {
450 var index: u64 = 0;427 var index: u64 = 0;
451 while (index < vector_type.len) : (index += 1) {428 while (index < vector_type.len) : (index += 1) {
452 switch (try generateSymbol(bin_file, src_loc, .{429 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) {
453 .ty = Type.fromInterned(vector_type.child),430 .bytes => unreachable,
454 .val = Value.fromInterned(switch (aggregate.storage) {431 .elems => |elems| elems[
455 .bytes => unreachable,432 math.cast(usize, index) orelse return error.Overflow
456 .elems => |elems| elems[433 ],
457 math.cast(usize, index) orelse return error.Overflow434 .repeated_elem => |elem| elem,
458 ],435 }), code, debug_output, reloc_info)) {
459 .repeated_elem => |elem| elem,
460 }),
461 }, code, debug_output, reloc_info)) {
462 .ok => {},436 .ok => {},
463 .fail => |em| return .{ .fail = em },437 .fail => |em| return .{ .fail = em },
464 }438 }
...@@ -491,17 +465,14 @@ pub fn generateSymbol(...@@ -491,17 +465,14 @@ pub fn generateSymbol(
491 .repeated_elem => |elem| elem,465 .repeated_elem => |elem| elem,
492 };466 };
493467
494 switch (try generateSymbol(bin_file, src_loc, .{468 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
495 .ty = Type.fromInterned(field_ty),
496 .val = Value.fromInterned(field_val),
497 }, code, debug_output, reloc_info)) {
498 .ok => {},469 .ok => {},
499 .fail => |em| return Result{ .fail = em },470 .fail => |em| return Result{ .fail = em },
500 }471 }
501 const unpadded_field_end = code.items.len - struct_begin;472 const unpadded_field_end = code.items.len - struct_begin;
502473
503 // Pad struct members if required474 // 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);
505 const padding = math.cast(usize, padded_field_end - unpadded_field_end) orelse476 const padding = math.cast(usize, padded_field_end - unpadded_field_end) orelse
506 return error.Overflow;477 return error.Overflow;
507478
...@@ -511,10 +482,10 @@ pub fn generateSymbol(...@@ -511,10 +482,10 @@ pub fn generateSymbol(
511 }482 }
512 },483 },
513 .struct_type => {484 .struct_type => {
514 const struct_type = ip.loadStructType(typed_value.ty.toIntern());485 const struct_type = ip.loadStructType(ty.toIntern());
515 switch (struct_type.layout) {486 switch (struct_type.layout) {
516 .@"packed" => {487 .@"packed" => {
517 const abi_size = math.cast(usize, typed_value.ty.abiSize(mod)) orelse488 const abi_size = math.cast(usize, ty.abiSize(mod)) orelse
518 return error.Overflow;489 return error.Overflow;
519 const current_pos = code.items.len;490 const current_pos = code.items.len;
520 try code.resize(current_pos + abi_size);491 try code.resize(current_pos + abi_size);
...@@ -537,10 +508,7 @@ pub fn generateSymbol(...@@ -537,10 +508,7 @@ pub fn generateSymbol(
537 return error.Overflow;508 return error.Overflow;
538 var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size);509 var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size);
539 defer tmp_list.deinit();510 defer tmp_list.deinit();
540 switch (try generateSymbol(bin_file, src_loc, .{511 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), &tmp_list, debug_output, reloc_info)) {
541 .ty = Type.fromInterned(field_ty),
542 .val = Value.fromInterned(field_val),
543 }, &tmp_list, debug_output, reloc_info)) {
544 .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items),512 .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items),
545 .fail => |em| return Result{ .fail = em },513 .fail => |em| return Result{ .fail = em },
546 }514 }
...@@ -560,7 +528,7 @@ pub fn generateSymbol(...@@ -560,7 +528,7 @@ pub fn generateSymbol(
560 const field_ty = field_types[field_index];528 const field_ty = field_types[field_index];
561 if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue;529 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) {
564 .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{532 .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{
565 .ty = field_ty,533 .ty = field_ty,
566 .storage = .{ .u64 = bytes[field_index] },534 .storage = .{ .u64 = bytes[field_index] },
...@@ -575,10 +543,7 @@ pub fn generateSymbol(...@@ -575,10 +543,7 @@ pub fn generateSymbol(
575 ) orelse return error.Overflow;543 ) orelse return error.Overflow;
576 if (padding > 0) try code.appendNTimes(0, padding);544 if (padding > 0) try code.appendNTimes(0, padding);
577545
578 switch (try generateSymbol(bin_file, src_loc, .{546 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) {
579 .ty = Type.fromInterned(field_ty),
580 .val = Value.fromInterned(field_val),
581 }, code, debug_output, reloc_info)) {
582 .ok => {},547 .ok => {},
583 .fail => |em| return Result{ .fail = em },548 .fail => |em| return Result{ .fail = em },
584 }549 }
...@@ -599,37 +564,28 @@ pub fn generateSymbol(...@@ -599,37 +564,28 @@ pub fn generateSymbol(
599 else => unreachable,564 else => unreachable,
600 },565 },
601 .un => |un| {566 .un => |un| {
602 const layout = typed_value.ty.unionGetLayout(mod);567 const layout = ty.unionGetLayout(mod);
603568
604 if (layout.payload_size == 0) {569 if (layout.payload_size == 0) {
605 return generateSymbol(bin_file, src_loc, .{570 return generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info);
606 .ty = typed_value.ty.unionTagTypeSafety(mod).?,
607 .val = Value.fromInterned(un.tag),
608 }, code, debug_output, reloc_info);
609 }571 }
610572
611 // Check if we should store the tag first.573 // Check if we should store the tag first.
612 if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) {574 if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) {
613 switch (try generateSymbol(bin_file, src_loc, .{575 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
614 .ty = typed_value.ty.unionTagTypeSafety(mod).?,
615 .val = Value.fromInterned(un.tag),
616 }, code, debug_output, reloc_info)) {
617 .ok => {},576 .ok => {},
618 .fail => |em| return Result{ .fail = em },577 .fail => |em| return Result{ .fail = em },
619 }578 }
620 }579 }
621580
622 const union_obj = mod.typeToUnion(typed_value.ty).?;581 const union_obj = mod.typeToUnion(ty).?;
623 if (un.tag != .none) {582 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).?;
625 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);584 const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]);
626 if (!field_ty.hasRuntimeBits(mod)) {585 if (!field_ty.hasRuntimeBits(mod)) {
627 try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);586 try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);
628 } else {587 } else {
629 switch (try generateSymbol(bin_file, src_loc, .{588 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
630 .ty = field_ty,
631 .val = Value.fromInterned(un.val),
632 }, code, debug_output, reloc_info)) {
633 .ok => {},589 .ok => {},
634 .fail => |em| return Result{ .fail = em },590 .fail => |em| return Result{ .fail = em },
635 }591 }
...@@ -640,20 +596,14 @@ pub fn generateSymbol(...@@ -640,20 +596,14 @@ pub fn generateSymbol(
640 }596 }
641 }597 }
642 } else {598 } else {
643 switch (try generateSymbol(bin_file, src_loc, .{599 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) {
644 .ty = Type.fromInterned(ip.typeOf(un.val)),
645 .val = Value.fromInterned(un.val),
646 }, code, debug_output, reloc_info)) {
647 .ok => {},600 .ok => {},
648 .fail => |em| return Result{ .fail = em },601 .fail => |em| return Result{ .fail = em },
649 }602 }
650 }603 }
651604
652 if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) {605 if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) {
653 switch (try generateSymbol(bin_file, src_loc, .{606 switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) {
654 .ty = Type.fromInterned(union_obj.enum_tag_ty),
655 .val = Value.fromInterned(un.tag),
656 }, code, debug_output, reloc_info)) {
657 .ok => {},607 .ok => {},
658 .fail => |em| return Result{ .fail = em },608 .fail => |em| return Result{ .fail = em },
659 }609 }
...@@ -681,10 +631,7 @@ fn lowerParentPtr(...@@ -681,10 +631,7 @@ fn lowerParentPtr(
681 return switch (ptr.addr) {631 return switch (ptr.addr) {
682 .decl => |decl| try lowerDeclRef(bin_file, src_loc, decl, code, debug_output, reloc_info),632 .decl => |decl| try lowerDeclRef(bin_file, src_loc, decl, code, debug_output, reloc_info),
683 .anon_decl => |ad| try lowerAnonDeclRef(bin_file, src_loc, ad, code, debug_output, reloc_info),633 .anon_decl => |ad| try lowerAnonDeclRef(bin_file, src_loc, ad, code, debug_output, reloc_info),
684 .int => |int| try generateSymbol(bin_file, src_loc, .{634 .int => |int| try generateSymbol(bin_file, src_loc, Value.fromInterned(int), code, debug_output, reloc_info),
685 .ty = Type.usize,
686 .val = Value.fromInterned(int),
687 }, code, debug_output, reloc_info),
688 .eu_payload => |eu_payload| try lowerParentPtr(635 .eu_payload => |eu_payload| try lowerParentPtr(
689 bin_file,636 bin_file,
690 src_loc,637 src_loc,
...@@ -910,11 +857,12 @@ pub const GenResult = union(enum) {...@@ -910,11 +857,12 @@ pub const GenResult = union(enum) {
910fn genDeclRef(857fn genDeclRef(
911 lf: *link.File,858 lf: *link.File,
912 src_loc: Module.SrcLoc,859 src_loc: Module.SrcLoc,
913 tv: TypedValue,860 val: Value,
914 ptr_decl_index: InternPool.DeclIndex,861 ptr_decl_index: InternPool.DeclIndex,
915) CodeGenError!GenResult {862) CodeGenError!GenResult {
916 const zcu = lf.comp.module.?;863 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
919 const ptr_decl = zcu.declPtr(ptr_decl_index);867 const ptr_decl = zcu.declPtr(ptr_decl_index);
920 const namespace = zcu.namespacePtr(ptr_decl.src_namespace);868 const namespace = zcu.namespacePtr(ptr_decl.src_namespace);
...@@ -945,12 +893,12 @@ fn genDeclRef(...@@ -945,12 +893,12 @@ fn genDeclRef(
945 const gpa = comp.gpa;893 const gpa = comp.gpa;
946894
947 // TODO this feels clunky. Perhaps we should check for it in `genTypedValue`?895 // 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| {
949 if (zcu.typeToFunc(fn_ty).?.is_generic) {897 if (zcu.typeToFunc(fn_ty).?.is_generic) {
950 return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(zcu).toByteUnitsOptional().? });898 return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(zcu).toByteUnitsOptional().? });
951 }899 }
952 } else if (tv.ty.zigTypeTag(zcu) == .Pointer) {900 } else if (ty.zigTypeTag(zcu) == .Pointer) {
953 const elem_ty = tv.ty.elemType2(zcu);901 const elem_ty = ty.elemType2(zcu);
954 if (!elem_ty.hasRuntimeBits(zcu)) {902 if (!elem_ty.hasRuntimeBits(zcu)) {
955 return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(zcu).toByteUnitsOptional().? });903 return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(zcu).toByteUnitsOptional().? });
956 }904 }
...@@ -958,7 +906,7 @@ fn genDeclRef(...@@ -958,7 +906,7 @@ fn genDeclRef(
958906
959 const decl_namespace = zcu.namespacePtr(decl.src_namespace);907 const decl_namespace = zcu.namespacePtr(decl.src_namespace);
960 const single_threaded = decl_namespace.file_scope.mod.single_threaded;908 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;
962 const is_extern = decl.isExtern(zcu);910 const is_extern = decl.isExtern(zcu);
963911
964 if (lf.cast(link.File.Elf)) |elf_file| {912 if (lf.cast(link.File.Elf)) |elf_file| {
...@@ -1023,14 +971,14 @@ fn genDeclRef(...@@ -1023,14 +971,14 @@ fn genDeclRef(
1023fn genUnnamedConst(971fn genUnnamedConst(
1024 lf: *link.File,972 lf: *link.File,
1025 src_loc: Module.SrcLoc,973 src_loc: Module.SrcLoc,
1026 tv: TypedValue,974 val: Value,
1027 owner_decl_index: InternPool.DeclIndex,975 owner_decl_index: InternPool.DeclIndex,
1028) CodeGenError!GenResult {976) CodeGenError!GenResult {
1029 const zcu = lf.comp.module.?;977 const zcu = lf.comp.module.?;
1030 const gpa = lf.comp.gpa;978 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| {
1034 return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});982 return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
1035 };983 };
1036 switch (lf.tag) {984 switch (lf.tag) {
...@@ -1062,18 +1010,15 @@ fn genUnnamedConst(...@@ -1062,18 +1010,15 @@ fn genUnnamedConst(
1062pub fn genTypedValue(1010pub fn genTypedValue(
1063 lf: *link.File,1011 lf: *link.File,
1064 src_loc: Module.SrcLoc,1012 src_loc: Module.SrcLoc,
1065 arg_tv: TypedValue,1013 val: Value,
1066 owner_decl_index: InternPool.DeclIndex,1014 owner_decl_index: InternPool.DeclIndex,
1067) CodeGenError!GenResult {1015) CodeGenError!GenResult {
1068 const zcu = lf.comp.module.?;1016 const zcu = lf.comp.module.?;
1069 const typed_value = arg_tv;1017 const ty = val.typeOf(zcu);
10701018
1071 log.debug("genTypedValue: ty = {}, val = {}", .{1019 log.debug("genTypedValue: val = {}", .{val.fmtValue(ty, zcu)});
1072 typed_value.ty.fmt(zcu),
1073 typed_value.val.fmtValue(typed_value.ty, zcu),
1074 });
10751020
1076 if (typed_value.val.isUndef(zcu))1021 if (val.isUndef(zcu))
1077 return GenResult.mcv(.undef);1022 return GenResult.mcv(.undef);
10781023
1079 const owner_decl = zcu.declPtr(owner_decl_index);1024 const owner_decl = zcu.declPtr(owner_decl_index);
...@@ -1081,85 +1026,92 @@ pub fn genTypedValue(...@@ -1081,85 +1026,92 @@ pub fn genTypedValue(
1081 const target = namespace.file_scope.mod.resolved_target.result;1026 const target = namespace.file_scope.mod.resolved_target.result;
1082 const ptr_bits = target.ptrBitWidth();1027 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())) {
1085 .ptr => |ptr| switch (ptr.addr) {1030 .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),
1087 else => {},1032 else => {},
1088 },1033 },
1089 else => {},1034 else => {},
1090 };1035 };
10911036
1092 switch (typed_value.ty.zigTypeTag(zcu)) {1037 switch (ty.zigTypeTag(zcu)) {
1093 .Void => return GenResult.mcv(.none),1038 .Void => return GenResult.mcv(.none),
1094 .Pointer => switch (typed_value.ty.ptrSize(zcu)) {1039 .Pointer => switch (ty.ptrSize(zcu)) {
1095 .Slice => {},1040 .Slice => {},
1096 else => switch (typed_value.val.toIntern()) {1041 else => switch (val.toIntern()) {
1097 .null_value => {1042 .null_value => {
1098 return GenResult.mcv(.{ .immediate = 0 });1043 return GenResult.mcv(.{ .immediate = 0 });
1099 },1044 },
1100 .none => {},1045 .none => {},
1101 else => switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) {1046 else => switch (zcu.intern_pool.indexToKey(val.toIntern())) {
1102 .int => {1047 .int => {
1103 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(zcu) });1048 return GenResult.mcv(.{ .immediate = val.toUnsignedInt(zcu) });
1104 },1049 },
1105 else => {},1050 else => {},
1106 },1051 },
1107 },1052 },
1108 },1053 },
1109 .Int => {1054 .Int => {
1110 const info = typed_value.ty.intInfo(zcu);1055 const info = ty.intInfo(zcu);
1111 if (info.bits <= ptr_bits) {1056 if (info.bits <= ptr_bits) {
1112 const unsigned = switch (info.signedness) {1057 const unsigned = switch (info.signedness) {
1113 .signed => @as(u64, @bitCast(typed_value.val.toSignedInt(zcu))),1058 .signed => @as(u64, @bitCast(val.toSignedInt(zcu))),
1114 .unsigned => typed_value.val.toUnsignedInt(zcu),1059 .unsigned => val.toUnsignedInt(zcu),
1115 };1060 };
1116 return GenResult.mcv(.{ .immediate = unsigned });1061 return GenResult.mcv(.{ .immediate = unsigned });
1117 }1062 }
1118 },1063 },
1119 .Bool => {1064 .Bool => {
1120 return GenResult.mcv(.{ .immediate = @intFromBool(typed_value.val.toBool()) });1065 return GenResult.mcv(.{ .immediate = @intFromBool(val.toBool()) });
1121 },1066 },
1122 .Optional => {1067 .Optional => {
1123 if (typed_value.ty.isPtrLikeOptional(zcu)) {1068 if (ty.isPtrLikeOptional(zcu)) {
1124 return genTypedValue(lf, src_loc, .{1069 return genTypedValue(
1125 .ty = typed_value.ty.optionalChild(zcu),1070 lf,
1126 .val = typed_value.val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }),1071 src_loc,
1127 }, owner_decl_index);1072 val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }),
1128 } else if (typed_value.ty.abiSize(zcu) == 1) {1073 owner_decl_index,
1129 return GenResult.mcv(.{ .immediate = @intFromBool(!typed_value.val.isNull(zcu)) });1074 );
1075 } else if (ty.abiSize(zcu) == 1) {
1076 return GenResult.mcv(.{ .immediate = @intFromBool(!val.isNull(zcu)) });
1130 }1077 }
1131 },1078 },
1132 .Enum => {1079 .Enum => {
1133 const enum_tag = zcu.intern_pool.indexToKey(typed_value.val.toIntern()).enum_tag;1080 const enum_tag = zcu.intern_pool.indexToKey(val.toIntern()).enum_tag;
1134 const int_tag_ty = zcu.intern_pool.typeOf(enum_tag.int);1081 return genTypedValue(
1135 return genTypedValue(lf, src_loc, .{1082 lf,
1136 .ty = Type.fromInterned(int_tag_ty),1083 src_loc,
1137 .val = Value.fromInterned(enum_tag.int),1084 Value.fromInterned(enum_tag.int),
1138 }, owner_decl_index);1085 owner_decl_index,
1086 );
1139 },1087 },
1140 .ErrorSet => {1088 .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;
1142 const error_index = zcu.global_error_set.getIndex(err_name).?;1090 const error_index = zcu.global_error_set.getIndex(err_name).?;
1143 return GenResult.mcv(.{ .immediate = error_index });1091 return GenResult.mcv(.{ .immediate = error_index });
1144 },1092 },
1145 .ErrorUnion => {1093 .ErrorUnion => {
1146 const err_type = typed_value.ty.errorUnionSet(zcu);1094 const err_type = ty.errorUnionSet(zcu);
1147 const payload_type = typed_value.ty.errorUnionPayload(zcu);1095 const payload_type = ty.errorUnionPayload(zcu);
1148 if (!payload_type.hasRuntimeBitsIgnoreComptime(zcu)) {1096 if (!payload_type.hasRuntimeBitsIgnoreComptime(zcu)) {
1149 // We use the error type directly as the type.1097 // We use the error type directly as the type.
1150 const err_int_ty = try zcu.errorIntType();1098 const err_int_ty = try zcu.errorIntType();
1151 switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern()).error_union.val) {1099 switch (zcu.intern_pool.indexToKey(val.toIntern()).error_union.val) {
1152 .err_name => |err_name| return genTypedValue(lf, src_loc, .{1100 .err_name => |err_name| return genTypedValue(
1153 .ty = err_type,1101 lf,
1154 .val = Value.fromInterned((try zcu.intern(.{ .err = .{1102 src_loc,
1103 Value.fromInterned(try zcu.intern(.{ .err = .{
1155 .ty = err_type.toIntern(),1104 .ty = err_type.toIntern(),
1156 .name = err_name,1105 .name = err_name,
1157 } }))),1106 } })),
1158 }, owner_decl_index),1107 owner_decl_index,
1159 .payload => return genTypedValue(lf, src_loc, .{1108 ),
1160 .ty = err_int_ty,1109 .payload => return genTypedValue(
1161 .val = try zcu.intValue(err_int_ty, 0),1110 lf,
1162 }, owner_decl_index),1111 src_loc,
1112 try zcu.intValue(err_int_ty, 0),
1113 owner_decl_index,
1114 ),
1163 }1115 }
1164 }1116 }
1165 },1117 },
...@@ -1176,7 +1128,7 @@ pub fn genTypedValue(...@@ -1176,7 +1128,7 @@ pub fn genTypedValue(
1176 else => {},1128 else => {},
1177 }1129 }
11781130
1179 return genUnnamedConst(lf, src_loc, typed_value, owner_decl_index);1131 return genUnnamedConst(lf, src_loc, val, owner_decl_index);
1180}1132}
11811133
1182pub fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u64 {1134pub fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u64 {
src/codegen/c.zig+28-35
...@@ -9,7 +9,6 @@ const Module = @import("../Module.zig");...@@ -9,7 +9,6 @@ const Module = @import("../Module.zig");
9const Compilation = @import("../Compilation.zig");9const Compilation = @import("../Compilation.zig");
10const Value = @import("../Value.zig");10const Value = @import("../Value.zig");
11const Type = @import("../type.zig").Type;11const Type = @import("../type.zig").Type;
12const TypedValue = @import("../TypedValue.zig");
13const C = link.File.C;12const C = link.File.C;
14const Decl = Module.Decl;13const Decl = Module.Decl;
15const trace = @import("../tracy.zig").trace;14const trace = @import("../tracy.zig").trace;
...@@ -1877,9 +1876,9 @@ pub const DeclGen = struct {...@@ -1877,9 +1876,9 @@ pub const DeclGen = struct {
1877 try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{});1876 try renderTypeSuffix(dg.pass, store.*, mod, w, cty_idx, .suffix, .{});
1878 }1877 }
18791878
1880 fn declIsGlobal(dg: *DeclGen, tv: TypedValue) bool {1879 fn declIsGlobal(dg: *DeclGen, val: Value) bool {
1881 const mod = dg.module;1880 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)) {
1883 .variable => |variable| mod.decl_exports.contains(variable.decl),1882 .variable => |variable| mod.decl_exports.contains(variable.decl),
1884 .extern_func => true,1883 .extern_func => true,
1885 .func => |func| mod.decl_exports.contains(func.owner_decl),1884 .func => |func| mod.decl_exports.contains(func.owner_decl),
...@@ -1972,7 +1971,7 @@ pub const DeclGen = struct {...@@ -1972,7 +1971,7 @@ pub const DeclGen = struct {
1972 ) !void {1971 ) !void {
1973 const decl = dg.module.declPtr(decl_index);1972 const decl = dg.module.declPtr(decl_index);
1974 const fwd = dg.fwdDeclWriter();1973 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);
1976 try fwd.writeAll(if (is_global) "zig_extern " else "static ");1975 try fwd.writeAll(if (is_global) "zig_extern " else "static ");
1977 const maybe_exports = dg.module.decl_exports.get(decl_index);1976 const maybe_exports = dg.module.decl_exports.get(decl_index);
1978 const export_weak_linkage = if (maybe_exports) |exports|1977 const export_weak_linkage = if (maybe_exports) |exports|
...@@ -2656,13 +2655,12 @@ fn genExports(o: *Object) !void {...@@ -2656,13 +2655,12 @@ fn genExports(o: *Object) !void {
2656 .anon, .flush => return,2655 .anon, .flush => return,
2657 };2656 };
2658 const decl = mod.declPtr(decl_index);2657 const decl = mod.declPtr(decl_index);
2659 const tv: TypedValue = .{ .ty = decl.typeOf(mod), .val = decl.val };
2660 const fwd = o.dg.fwdDeclWriter();2658 const fwd = o.dg.fwdDeclWriter();
26612659
2662 const exports = mod.decl_exports.get(decl_index) orelse return;2660 const exports = mod.decl_exports.get(decl_index) orelse return;
2663 if (exports.items.len < 2) return;2661 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())) {
2666 .func => return for (exports.items[1..], 1..) |@"export", i| {2664 .func => return for (exports.items[1..], 1..) |@"export", i| {
2667 try fwd.writeAll("zig_extern ");2665 try fwd.writeAll("zig_extern ");
2668 if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage_fn ");2666 if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage_fn ");
...@@ -2805,15 +2803,11 @@ pub fn genFunc(f: *Function) !void {...@@ -2805,15 +2803,11 @@ pub fn genFunc(f: *Function) !void {
2805 const gpa = o.dg.gpa;2803 const gpa = o.dg.gpa;
2806 const decl_index = o.dg.pass.decl;2804 const decl_index = o.dg.pass.decl;
2807 const decl = mod.declPtr(decl_index);2805 const decl = mod.declPtr(decl_index);
2808 const tv: TypedValue = .{
2809 .ty = decl.typeOf(mod),
2810 .val = decl.val,
2811 };
28122806
2813 o.code_header = std.ArrayList(u8).init(gpa);2807 o.code_header = std.ArrayList(u8).init(gpa);
2814 defer o.code_header.deinit();2808 defer o.code_header.deinit();
28152809
2816 const is_global = o.dg.declIsGlobal(tv);2810 const is_global = o.dg.declIsGlobal(decl.val);
2817 const fwd_decl_writer = o.dg.fwdDeclWriter();2811 const fwd_decl_writer = o.dg.fwdDeclWriter();
2818 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");2812 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
28192813
...@@ -2893,22 +2887,23 @@ pub fn genDecl(o: *Object) !void {...@@ -2893,22 +2887,23 @@ pub fn genDecl(o: *Object) !void {
2893 const mod = o.dg.module;2887 const mod = o.dg.module;
2894 const decl_index = o.dg.pass.decl;2888 const decl_index = o.dg.pass.decl;
2895 const decl = mod.declPtr(decl_index);2889 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;2893 if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return;
2899 if (tv.val.getExternFunc(mod)) |_| {2894 if (decl_val.getExternFunc(mod)) |_| {
2900 const fwd_decl_writer = o.dg.fwdDeclWriter();2895 const fwd_decl_writer = o.dg.fwdDeclWriter();
2901 try fwd_decl_writer.writeAll("zig_extern ");2896 try fwd_decl_writer.writeAll("zig_extern ");
2902 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });2897 try o.dg.renderFunctionSignature(fwd_decl_writer, decl_index, .forward, .{ .export_index = 0 });
2903 try fwd_decl_writer.writeAll(";\n");2898 try fwd_decl_writer.writeAll(";\n");
2904 try genExports(o);2899 try genExports(o);
2905 } else if (tv.val.getVariable(mod)) |variable| {2900 } else if (decl_val.getVariable(mod)) |variable| {
2906 try o.dg.renderFwdDecl(decl_index, variable, .final);2901 try o.dg.renderFwdDecl(decl_index, variable, .final);
2907 try genExports(o);2902 try genExports(o);
29082903
2909 if (variable.is_extern) return;2904 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);
2912 const w = o.writer();2907 const w = o.writer();
2913 if (!is_global) try w.writeAll("static ");2908 if (!is_global) try w.writeAll("static ");
2914 if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");2909 if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage ");
...@@ -2916,22 +2911,22 @@ pub fn genDecl(o: *Object) !void {...@@ -2916,22 +2911,22 @@ pub fn genDecl(o: *Object) !void {
2916 if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s|2911 if (mod.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s|
2917 try w.print("zig_linksection(\"{s}\", ", .{s});2912 try w.print("zig_linksection(\"{s}\", ", .{s});
2918 const decl_c_value = .{ .decl = decl_index };2913 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);
2920 if (decl.@"linksection" != .none) try w.writeAll(", read, write)");2915 if (decl.@"linksection" != .none) try w.writeAll(", read, write)");
2921 try w.writeAll(" = ");2916 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);
2923 try w.writeByte(';');2918 try w.writeByte(';');
2924 try o.indent_writer.insertNewline();2919 try o.indent_writer.insertNewline();
2925 } else {2920 } else {
2926 const is_global = o.dg.module.decl_exports.contains(decl_index);2921 const is_global = o.dg.module.decl_exports.contains(decl_index);
2927 const decl_c_value = .{ .decl = decl_index };2922 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");
2929 }2924 }
2930}2925}
29312926
2932pub fn genDeclValue(2927pub fn genDeclValue(
2933 o: *Object,2928 o: *Object,
2934 tv: TypedValue,2929 val: Value,
2935 is_global: bool,2930 is_global: bool,
2936 decl_c_value: CValue,2931 decl_c_value: CValue,
2937 alignment: Alignment,2932 alignment: Alignment,
...@@ -2940,8 +2935,10 @@ pub fn genDeclValue(...@@ -2940,8 +2935,10 @@ pub fn genDeclValue(
2940 const mod = o.dg.module;2935 const mod = o.dg.module;
2941 const fwd_decl_writer = o.dg.fwdDeclWriter();2936 const fwd_decl_writer = o.dg.fwdDeclWriter();
29422937
2938 const ty = val.typeOf(mod);
2939
2943 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");2940 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);
2945 switch (o.dg.pass) {2942 switch (o.dg.pass) {
2946 .decl => |decl_index| {2943 .decl => |decl_index| {
2947 if (mod.decl_exports.get(decl_index)) |exports| {2944 if (mod.decl_exports.get(decl_index)) |exports| {
...@@ -2964,10 +2961,10 @@ pub fn genDeclValue(...@@ -2964,10 +2961,10 @@ pub fn genDeclValue(
29642961
2965 if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|2962 if (mod.intern_pool.stringToSliceUnwrap(link_section)) |s|
2966 try w.print("zig_linksection(\"{s}\", ", .{s});2963 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);
2968 if (link_section != .none) try w.writeAll(", read)");2965 if (link_section != .none) try w.writeAll(", read)");
2969 try w.writeAll(" = ");2966 try w.writeAll(" = ");
2970 try o.dg.renderValue(w, tv.ty, tv.val, .StaticInitializer);2967 try o.dg.renderValue(w, ty, val, .StaticInitializer);
2971 try w.writeAll(";\n");2968 try w.writeAll(";\n");
2972}2969}
29732970
...@@ -2978,14 +2975,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {...@@ -2978,14 +2975,10 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
2978 const mod = dg.module;2975 const mod = dg.module;
2979 const decl_index = dg.pass.decl;2976 const decl_index = dg.pass.decl;
2980 const decl = mod.declPtr(decl_index);2977 const decl = mod.declPtr(decl_index);
2981 const tv: TypedValue = .{
2982 .ty = decl.typeOf(mod),
2983 .val = decl.val,
2984 };
2985 const writer = dg.fwdDeclWriter();2978 const writer = dg.fwdDeclWriter();
29862979
2987 switch (tv.ty.zigTypeTag(mod)) {2980 switch (decl.val.typeOf(mod).zigTypeTag(mod)) {
2988 .Fn => if (dg.declIsGlobal(tv)) {2981 .Fn => if (dg.declIsGlobal(decl.val)) {
2989 try writer.writeAll("zig_extern ");2982 try writer.writeAll("zig_extern ");
2990 try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });2983 try dg.renderFunctionSignature(writer, dg.pass.decl, .complete, .{ .export_index = 0 });
2991 try dg.fwd_decl.appendSlice(";\n");2984 try dg.fwd_decl.appendSlice(";\n");
...@@ -5304,25 +5297,25 @@ fn airIsNull(...@@ -5304,25 +5297,25 @@ fn airIsNull(
5304 const err_int_ty = try mod.errorIntType();5297 const err_int_ty = try mod.errorIntType();
53055298
5306 const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod))5299 const rhs = if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod))
5307 TypedValue{ .ty = Type.bool, .val = Value.true }5300 Value.true
5308 else if (optional_ty.isPtrLikeOptional(mod))5301 else if (optional_ty.isPtrLikeOptional(mod))
5309 // operand is a regular pointer, test `operand !=/== NULL`5302 // 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)
5311 else if (payload_ty.zigTypeTag(mod) == .ErrorSet)5304 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)
5313 else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: {5306 else if (payload_ty.isSlice(mod) and optional_ty.optionalReprIsPayload(mod)) rhs: {
5314 try writer.writeAll(".ptr");5307 try writer.writeAll(".ptr");
5315 const slice_ptr_ty = payload_ty.slicePtrFieldType(mod);5308 const slice_ptr_ty = payload_ty.slicePtrFieldType(mod);
5316 const opt_slice_ptr_ty = try mod.optionalType(slice_ptr_ty.toIntern());5309 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);
5318 } else rhs: {5311 } else rhs: {
5319 try writer.writeAll(".is_null");5312 try writer.writeAll(".is_null");
5320 break :rhs TypedValue{ .ty = Type.bool, .val = Value.true };5313 break :rhs Value.true;
5321 };5314 };
5322 try writer.writeByte(' ');5315 try writer.writeByte(' ');
5323 try writer.writeAll(operator);5316 try writer.writeAll(operator);
5324 try writer.writeByte(' ');5317 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);
5326 try writer.writeAll(";\n");5319 try writer.writeAll(";\n");
5327 return local;5320 return local;
5328}5321}
src/codegen/llvm.zig+11-18
...@@ -18,7 +18,6 @@ const Module = @import("../Module.zig");...@@ -18,7 +18,6 @@ const Module = @import("../Module.zig");
18const Zcu = Module;18const Zcu = Module;
19const InternPool = @import("../InternPool.zig");19const InternPool = @import("../InternPool.zig");
20const Package = @import("../Package.zig");20const Package = @import("../Package.zig");
21const TypedValue = @import("../TypedValue.zig");
22const Air = @import("../Air.zig");21const Air = @import("../Air.zig");
23const Liveness = @import("../Liveness.zig");22const Liveness = @import("../Liveness.zig");
24const Value = @import("../Value.zig");23const Value = @import("../Value.zig");
...@@ -4823,19 +4822,17 @@ pub const FuncGen = struct {...@@ -4823,19 +4822,17 @@ pub const FuncGen = struct {
48234822
4824 const o = self.dg.object;4823 const o = self.dg.object;
4825 const mod = o.module;4824 const mod = o.module;
4826 const llvm_val = try self.resolveValue(.{4825 const llvm_val = try self.resolveValue((try self.air.value(inst, mod)).?);
4827 .ty = self.typeOf(inst),
4828 .val = (try self.air.value(inst, mod)).?,
4829 });
4830 gop.value_ptr.* = llvm_val.toValue();4826 gop.value_ptr.* = llvm_val.toValue();
4831 return llvm_val.toValue();4827 return llvm_val.toValue();
4832 }4828 }
48334829
4834 fn resolveValue(self: *FuncGen, tv: TypedValue) Error!Builder.Constant {4830 fn resolveValue(self: *FuncGen, val: Value) Error!Builder.Constant {
4835 const o = self.dg.object;4831 const o = self.dg.object;
4836 const mod = o.module;4832 const mod = o.module;
4837 const llvm_val = try o.lowerValue(tv.val.toIntern());4833 const ty = val.typeOf(mod);
4838 if (!isByRef(tv.ty, mod)) return llvm_val;4834 const llvm_val = try o.lowerValue(val.toIntern());
4835 if (!isByRef(ty, mod)) return llvm_val;
48394836
4840 // We have an LLVM value but we need to create a global constant and4837 // We have an LLVM value but we need to create a global constant and
4841 // set the value as its initializer, and then return a pointer to the global.4838 // set the value as its initializer, and then return a pointer to the global.
...@@ -4849,7 +4846,7 @@ pub const FuncGen = struct {...@@ -4849,7 +4846,7 @@ pub const FuncGen = struct {
4849 variable_index.setLinkage(.private, &o.builder);4846 variable_index.setLinkage(.private, &o.builder);
4850 variable_index.setMutability(.constant, &o.builder);4847 variable_index.setMutability(.constant, &o.builder);
4851 variable_index.setUnnamedAddr(.unnamed_addr, &o.builder);4848 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);
4853 return o.builder.convConst(4850 return o.builder.convConst(
4854 .unneeded,4851 .unneeded,
4855 variable_index.toConst(&o.builder),4852 variable_index.toConst(&o.builder),
...@@ -4861,11 +4858,10 @@ pub const FuncGen = struct {...@@ -4861,11 +4858,10 @@ pub const FuncGen = struct {
4861 const o = self.dg.object;4858 const o = self.dg.object;
4862 const mod = o.module;4859 const mod = o.module;
4863 if (o.null_opt_usize == .no_init) {4860 if (o.null_opt_usize == .no_init) {
4864 const ty = try mod.intern(.{ .opt_type = .usize_type });4861 o.null_opt_usize = try self.resolveValue(Value.fromInterned(try mod.intern(.{ .opt = .{
4865 o.null_opt_usize = try self.resolveValue(.{4862 .ty = try mod.intern(.{ .opt_type = .usize_type }),
4866 .ty = Type.fromInterned(ty),4863 .val = .none,
4867 .val = Value.fromInterned((try mod.intern(.{ .opt = .{ .ty = ty, .val = .none } }))),4864 } })));
4868 });
4869 }4865 }
4870 return o.null_opt_usize;4866 return o.null_opt_usize;
4871 }4867 }
...@@ -10061,10 +10057,7 @@ pub const FuncGen = struct {...@@ -10061,10 +10057,7 @@ pub const FuncGen = struct {
10061 const elem_ptr = try self.wip.gep(.inbounds, llvm_result_ty, alloca_inst, &.{10057 const elem_ptr = try self.wip.gep(.inbounds, llvm_result_ty, alloca_inst, &.{
10062 usize_zero, try o.builder.intValue(llvm_usize, array_info.len),10058 usize_zero, try o.builder.intValue(llvm_usize, array_info.len),
10063 }, "");10059 }, "");
10064 const llvm_elem = try self.resolveValue(.{10060 const llvm_elem = try self.resolveValue(sent_val);
10065 .ty = array_info.elem_type,
10066 .val = sent_val,
10067 });
10068 try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none);10061 try self.store(elem_ptr, elem_ptr_ty, llvm_elem.toValue(), .none);
10069 }10062 }
1007010063
src/link.zig+3-3
...@@ -17,7 +17,7 @@ const Liveness = @import("Liveness.zig");...@@ -17,7 +17,7 @@ const Liveness = @import("Liveness.zig");
17const Module = @import("Module.zig");17const Module = @import("Module.zig");
18const InternPool = @import("InternPool.zig");18const InternPool = @import("InternPool.zig");
19const Type = @import("type.zig").Type;19const Type = @import("type.zig").Type;
20const TypedValue = @import("TypedValue.zig");20const Value = @import("Value.zig");
21const LlvmObject = @import("codegen/llvm.zig").Object;21const LlvmObject = @import("codegen/llvm.zig").Object;
2222
23/// When adding a new field, remember to update `hashAddSystemLibs`.23/// When adding a new field, remember to update `hashAddSystemLibs`.
...@@ -376,14 +376,14 @@ pub const File = struct {...@@ -376,14 +376,14 @@ pub const File = struct {
376 /// Called from within the CodeGen to lower a local variable instantion as an unnamed376 /// Called from within the CodeGen to lower a local variable instantion as an unnamed
377 /// constant. Returns the symbol index of the lowered constant in the read-only section377 /// constant. Returns the symbol index of the lowered constant in the read-only section
378 /// of the final binary.378 /// 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 {
380 if (build_options.only_c) @compileError("unreachable");380 if (build_options.only_c) @compileError("unreachable");
381 switch (base.tag) {381 switch (base.tag) {
382 .spirv => unreachable,382 .spirv => unreachable,
383 .c => unreachable,383 .c => unreachable,
384 .nvptx => unreachable,384 .nvptx => unreachable,
385 inline else => |t| {385 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);
387 },387 },
388 }388 }
389 }389 }
src/link/C.zig+1-5
...@@ -283,13 +283,9 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void {...@@ -283,13 +283,9 @@ fn updateAnonDecl(self: *C, module: *Module, i: usize) !void {
283 code.* = object.code.moveToUnmanaged();283 code.* = object.code.moveToUnmanaged();
284 }284 }
285285
286 const tv: @import("../TypedValue.zig") = .{
287 .ty = Type.fromInterned(module.intern_pool.typeOf(anon_decl)),
288 .val = Value.fromInterned(anon_decl),
289 };
290 const c_value: codegen.CValue = .{ .constant = anon_decl };286 const c_value: codegen.CValue = .{ .constant = anon_decl };
291 const alignment: Alignment = self.aligned_anon_decls.get(anon_decl) orelse .none;287 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) {
293 error.AnalysisFail => {289 error.AnalysisFail => {
294 @panic("TODO: C backend AnalysisFail on anonymous decl");290 @panic("TODO: C backend AnalysisFail on anonymous decl");
295 //try module.failed_decls.put(gpa, decl_index, object.dg.error_msg.?);291 //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:...@@ -1167,7 +1167,7 @@ pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air:
1167 return self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));1167 return self.updateExports(mod, .{ .decl_index = decl_index }, mod.getDeclExports(decl_index));
1168}1168}
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 {
1171 const gpa = self.base.comp.gpa;1171 const gpa = self.base.comp.gpa;
1172 const mod = self.base.comp.module.?;1172 const mod = self.base.comp.module.?;
1173 const decl = mod.declPtr(decl_index);1173 const decl = mod.declPtr(decl_index);
...@@ -1180,7 +1180,8 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: InternPool.Dec...@@ -1180,7 +1180,8 @@ pub fn lowerUnnamedConst(self: *Coff, tv: TypedValue, decl_index: InternPool.Dec
1180 const index = unnamed_consts.items.len;1180 const index = unnamed_consts.items.len;
1181 const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });1181 const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
1182 defer gpa.free(sym_name);1182 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))) {
1184 .ok => |atom_index| atom_index,1185 .ok => |atom_index| atom_index,
1185 .fail => |em| {1186 .fail => |em| {
1186 decl.analysis = .codegen_failure;1187 decl.analysis = .codegen_failure;
...@@ -1198,7 +1199,7 @@ const LowerConstResult = union(enum) {...@@ -1198,7 +1199,7 @@ const LowerConstResult = union(enum) {
1198 fail: *Module.ErrorMsg,1199 fail: *Module.ErrorMsg,
1199};1200};
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 {
1202 const gpa = self.base.comp.gpa;1203 const gpa = self.base.comp.gpa;
12031204
1204 var code_buffer = std.ArrayList(u8).init(gpa);1205 var code_buffer = std.ArrayList(u8).init(gpa);
...@@ -1209,7 +1210,7 @@ fn lowerConst(self: *Coff, name: []const u8, tv: TypedValue, required_alignment:...@@ -1209,7 +1210,7 @@ fn lowerConst(self: *Coff, name: []const u8, tv: TypedValue, required_alignment:
1209 try self.setSymbolName(sym, name);1210 try self.setSymbolName(sym, name);
1210 sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_id + 1));1211 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, .{
1213 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,1214 .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?,
1214 });1215 });
1215 const code = switch (res) {1216 const code = switch (res) {
...@@ -1271,10 +1272,7 @@ pub fn updateDecl(...@@ -1271,10 +1272,7 @@ pub fn updateDecl(
1271 defer code_buffer.deinit();1272 defer code_buffer.deinit();
12721273
1273 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;1274 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 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
1275 .ty = decl.typeOf(mod),
1276 .val = decl_val,
1277 }, &code_buffer, .none, .{
1278 .parent_atom_index = atom.getSymbolIndex().?,1276 .parent_atom_index = atom.getSymbolIndex().?,
1279 });1277 });
1280 const code = switch (res) {1278 const code = switch (res) {
...@@ -1887,14 +1885,13 @@ pub fn lowerAnonDecl(...@@ -1887,14 +1885,13 @@ pub fn lowerAnonDecl(
1887 }1885 }
18881886
1889 const val = Value.fromInterned(decl_val);1887 const val = Value.fromInterned(decl_val);
1890 const tv = TypedValue{ .ty = ty, .val = val };
1891 var name_buf: [32]u8 = undefined;1888 var name_buf: [32]u8 = undefined;
1892 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{1889 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
1893 @intFromEnum(decl_val),1890 @intFromEnum(decl_val),
1894 }) catch unreachable;1891 }) catch unreachable;
1895 const res = self.lowerConst(1892 const res = self.lowerConst(
1896 name,1893 name,
1897 tv,1894 val,
1898 decl_alignment,1895 decl_alignment,
1899 self.rdata_section_index.?,1896 self.rdata_section_index.?,
1900 src_loc,1897 src_loc,
...@@ -2754,7 +2751,6 @@ const TableSection = @import("table_section.zig").TableSection;...@@ -2754,7 +2751,6 @@ const TableSection = @import("table_section.zig").TableSection;
2754const StringTable = @import("StringTable.zig");2751const StringTable = @import("StringTable.zig");
2755const Type = @import("../type.zig").Type;2752const Type = @import("../type.zig").Type;
2756const Value = @import("../Value.zig");2753const Value = @import("../Value.zig");
2757const TypedValue = @import("../TypedValue.zig");
27582754
2759pub const base_tag: link.File.Tag = .coff;2755pub const base_tag: link.File.Tag = .coff;
27602756
src/link/Elf.zig+3-3
...@@ -3039,8 +3039,8 @@ pub fn updateDecl(...@@ -3039,8 +3039,8 @@ pub fn updateDecl(
3039 return self.zigObjectPtr().?.updateDecl(self, mod, decl_index);3039 return self.zigObjectPtr().?.updateDecl(self, mod, decl_index);
3040}3040}
30413041
3042pub fn lowerUnnamedConst(self: *Elf, typed_value: TypedValue, decl_index: InternPool.DeclIndex) !u32 {3042pub fn lowerUnnamedConst(self: *Elf, val: Value, decl_index: InternPool.DeclIndex) !u32 {
3043 return self.zigObjectPtr().?.lowerUnnamedConst(self, typed_value, decl_index);3043 return self.zigObjectPtr().?.lowerUnnamedConst(self, val, decl_index);
3044}3044}
30453045
3046pub fn updateExports(3046pub fn updateExports(
...@@ -6260,7 +6260,7 @@ const SharedObject = @import("Elf/SharedObject.zig");...@@ -6260,7 +6260,7 @@ const SharedObject = @import("Elf/SharedObject.zig");
6260const Symbol = @import("Elf/Symbol.zig");6260const Symbol = @import("Elf/Symbol.zig");
6261const StringTable = @import("StringTable.zig");6261const StringTable = @import("StringTable.zig");
6262const Thunk = thunks.Thunk;6262const Thunk = thunks.Thunk;
6263const TypedValue = @import("../TypedValue.zig");6263const Value = @import("../Value.zig");
6264const VerneedSection = synthetic_sections.VerneedSection;6264const VerneedSection = synthetic_sections.VerneedSection;
6265const ZigGotSection = synthetic_sections.ZigGotSection;6265const ZigGotSection = synthetic_sections.ZigGotSection;
6266const ZigObject = @import("Elf/ZigObject.zig");6266const ZigObject = @import("Elf/ZigObject.zig");
src/link/Elf/ZigObject.zig+9-16
...@@ -702,7 +702,6 @@ pub fn lowerAnonDecl(...@@ -702,7 +702,6 @@ pub fn lowerAnonDecl(
702 }702 }
703703
704 const val = Value.fromInterned(decl_val);704 const val = Value.fromInterned(decl_val);
705 const tv = TypedValue{ .ty = ty, .val = val };
706 var name_buf: [32]u8 = undefined;705 var name_buf: [32]u8 = undefined;
707 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{706 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
708 @intFromEnum(decl_val),707 @intFromEnum(decl_val),
...@@ -710,7 +709,7 @@ pub fn lowerAnonDecl(...@@ -710,7 +709,7 @@ pub fn lowerAnonDecl(
710 const res = self.lowerConst(709 const res = self.lowerConst(
711 elf_file,710 elf_file,
712 name,711 name,
713 tv,712 val,
714 decl_alignment,713 decl_alignment,
715 elf_file.zig_data_rel_ro_section_index.?,714 elf_file.zig_data_rel_ro_section_index.?,
716 src_loc,715 src_loc,
...@@ -1157,19 +1156,13 @@ pub fn updateDecl(...@@ -1157,19 +1156,13 @@ pub fn updateDecl(
1157 // TODO implement .debug_info for global variables1156 // TODO implement .debug_info for global variables
1158 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;1157 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
1159 const res = if (decl_state) |*ds|1158 const res = if (decl_state) |*ds|
1160 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), .{1159 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .{
1161 .ty = decl.typeOf(mod),
1162 .val = decl_val,
1163 }, &code_buffer, .{
1164 .dwarf = ds,1160 .dwarf = ds,
1165 }, .{1161 }, .{
1166 .parent_atom_index = sym_index,1162 .parent_atom_index = sym_index,
1167 })1163 })
1168 else1164 else
1169 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), .{1165 try codegen.generateSymbol(&elf_file.base, decl.srcLoc(mod), decl_val, &code_buffer, .none, .{
1170 .ty = decl.typeOf(mod),
1171 .val = decl_val,
1172 }, &code_buffer, .none, .{
1173 .parent_atom_index = sym_index,1166 .parent_atom_index = sym_index,
1174 });1167 });
11751168
...@@ -1289,7 +1282,7 @@ fn updateLazySymbol(...@@ -1289,7 +1282,7 @@ fn updateLazySymbol(
1289pub fn lowerUnnamedConst(1282pub fn lowerUnnamedConst(
1290 self: *ZigObject,1283 self: *ZigObject,
1291 elf_file: *Elf,1284 elf_file: *Elf,
1292 typed_value: TypedValue,1285 val: Value,
1293 decl_index: InternPool.DeclIndex,1286 decl_index: InternPool.DeclIndex,
1294) !u32 {1287) !u32 {
1295 const gpa = elf_file.base.comp.gpa;1288 const gpa = elf_file.base.comp.gpa;
...@@ -1304,11 +1297,12 @@ pub fn lowerUnnamedConst(...@@ -1304,11 +1297,12 @@ pub fn lowerUnnamedConst(
1304 const index = unnamed_consts.items.len;1297 const index = unnamed_consts.items.len;
1305 const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });1298 const name = try std.fmt.allocPrint(gpa, "__unnamed_{s}_{d}", .{ decl_name, index });
1306 defer gpa.free(name);1299 defer gpa.free(name);
1300 const ty = val.typeOf(mod);
1307 const sym_index = switch (try self.lowerConst(1301 const sym_index = switch (try self.lowerConst(
1308 elf_file,1302 elf_file,
1309 name,1303 name,
1310 typed_value,1304 val,
1311 typed_value.ty.abiAlignment(mod),1305 ty.abiAlignment(mod),
1312 elf_file.zig_data_rel_ro_section_index.?,1306 elf_file.zig_data_rel_ro_section_index.?,
1313 decl.srcLoc(mod),1307 decl.srcLoc(mod),
1314 )) {1308 )) {
...@@ -1334,7 +1328,7 @@ fn lowerConst(...@@ -1334,7 +1328,7 @@ fn lowerConst(
1334 self: *ZigObject,1328 self: *ZigObject,
1335 elf_file: *Elf,1329 elf_file: *Elf,
1336 name: []const u8,1330 name: []const u8,
1337 tv: TypedValue,1331 val: Value,
1338 required_alignment: InternPool.Alignment,1332 required_alignment: InternPool.Alignment,
1339 output_section_index: u32,1333 output_section_index: u32,
1340 src_loc: Module.SrcLoc,1334 src_loc: Module.SrcLoc,
...@@ -1346,7 +1340,7 @@ fn lowerConst(...@@ -1346,7 +1340,7 @@ fn lowerConst(
13461340
1347 const sym_index = try self.addAtom(elf_file);1341 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, .{
1350 .none = {},1344 .none = {},
1351 }, .{1345 }, .{
1352 .parent_atom_index = sym_index,1346 .parent_atom_index = sym_index,
...@@ -1657,5 +1651,4 @@ const Symbol = @import("Symbol.zig");...@@ -1657,5 +1651,4 @@ const Symbol = @import("Symbol.zig");
1657const StringTable = @import("../StringTable.zig");1651const StringTable = @import("../StringTable.zig");
1658const Type = @import("../../type.zig").Type;1652const Type = @import("../../type.zig").Type;
1659const Value = @import("../../Value.zig");1653const Value = @import("../../Value.zig");
1660const TypedValue = @import("../../TypedValue.zig");
1661const ZigObject = @This();1654const ZigObject = @This();
src/link/MachO.zig+3-3
...@@ -3127,8 +3127,8 @@ pub fn updateFunc(self: *MachO, mod: *Module, func_index: InternPool.Index, air:...@@ -3127,8 +3127,8 @@ pub fn updateFunc(self: *MachO, mod: *Module, func_index: InternPool.Index, air:
3127 return self.getZigObject().?.updateFunc(self, mod, func_index, air, liveness);3127 return self.getZigObject().?.updateFunc(self, mod, func_index, air, liveness);
3128}3128}
31293129
3130pub fn lowerUnnamedConst(self: *MachO, typed_value: TypedValue, decl_index: InternPool.DeclIndex) !u32 {3130pub fn lowerUnnamedConst(self: *MachO, val: Value, decl_index: InternPool.DeclIndex) !u32 {
3131 return self.getZigObject().?.lowerUnnamedConst(self, typed_value, decl_index);3131 return self.getZigObject().?.lowerUnnamedConst(self, val, decl_index);
3132}3132}
31333133
3134pub fn updateDecl(self: *MachO, mod: *Module, decl_index: InternPool.DeclIndex) !void {3134pub fn updateDecl(self: *MachO, mod: *Module, decl_index: InternPool.DeclIndex) !void {
...@@ -4689,7 +4689,7 @@ const StubsHelperSection = synthetic.StubsHelperSection;...@@ -4689,7 +4689,7 @@ const StubsHelperSection = synthetic.StubsHelperSection;
4689const Symbol = @import("MachO/Symbol.zig");4689const Symbol = @import("MachO/Symbol.zig");
4690const Thunk = thunks.Thunk;4690const Thunk = thunks.Thunk;
4691const TlvPtrSection = synthetic.TlvPtrSection;4691const TlvPtrSection = synthetic.TlvPtrSection;
4692const TypedValue = @import("../TypedValue.zig");4692const Value = @import("../Value.zig");
4693const UnwindInfo = @import("MachO/UnwindInfo.zig");4693const UnwindInfo = @import("MachO/UnwindInfo.zig");
4694const WeakBindSection = synthetic.WeakBindSection;4694const WeakBindSection = synthetic.WeakBindSection;
4695const ZigGotSection = synthetic.ZigGotSection;4695const ZigGotSection = synthetic.ZigGotSection;
src/link/MachO/ZigObject.zig+7-14
...@@ -567,8 +567,6 @@ pub fn lowerAnonDecl(...@@ -567,8 +567,6 @@ pub fn lowerAnonDecl(
567 return .ok;567 return .ok;
568 }568 }
569569
570 const val = Value.fromInterned(decl_val);
571 const tv = TypedValue{ .ty = ty, .val = val };
572 var name_buf: [32]u8 = undefined;570 var name_buf: [32]u8 = undefined;
573 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{571 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
574 @intFromEnum(decl_val),572 @intFromEnum(decl_val),
...@@ -576,7 +574,7 @@ pub fn lowerAnonDecl(...@@ -576,7 +574,7 @@ pub fn lowerAnonDecl(
576 const res = self.lowerConst(574 const res = self.lowerConst(
577 macho_file,575 macho_file,
578 name,576 name,
579 tv,577 Value.fromInterned(decl_val),
580 decl_alignment,578 decl_alignment,
581 macho_file.zig_const_sect_index.?,579 macho_file.zig_const_sect_index.?,
582 src_loc,580 src_loc,
...@@ -738,11 +736,7 @@ pub fn updateDecl(...@@ -738,11 +736,7 @@ pub fn updateDecl(
738736
739 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;737 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
740 const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none;738 const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none;
741 const res =739 const res = try codegen.generateSymbol(&macho_file.base, decl.srcLoc(mod), decl_val, &code_buffer, dio, .{
742 try codegen.generateSymbol(&macho_file.base, decl.srcLoc(mod), .{
743 .ty = decl.typeOf(mod),
744 .val = decl_val,
745 }, &code_buffer, dio, .{
746 .parent_atom_index = sym_index,740 .parent_atom_index = sym_index,
747 });741 });
748742
...@@ -1068,7 +1062,7 @@ fn getDeclOutputSection(...@@ -1068,7 +1062,7 @@ fn getDeclOutputSection(
1068pub fn lowerUnnamedConst(1062pub fn lowerUnnamedConst(
1069 self: *ZigObject,1063 self: *ZigObject,
1070 macho_file: *MachO,1064 macho_file: *MachO,
1071 typed_value: TypedValue,1065 val: Value,
1072 decl_index: InternPool.DeclIndex,1066 decl_index: InternPool.DeclIndex,
1073) !u32 {1067) !u32 {
1074 const gpa = macho_file.base.comp.gpa;1068 const gpa = macho_file.base.comp.gpa;
...@@ -1086,8 +1080,8 @@ pub fn lowerUnnamedConst(...@@ -1086,8 +1080,8 @@ pub fn lowerUnnamedConst(
1086 const sym_index = switch (try self.lowerConst(1080 const sym_index = switch (try self.lowerConst(
1087 macho_file,1081 macho_file,
1088 name,1082 name,
1089 typed_value,1083 val,
1090 typed_value.ty.abiAlignment(mod),1084 val.typeOf(mod).abiAlignment(mod),
1091 macho_file.zig_const_sect_index.?,1085 macho_file.zig_const_sect_index.?,
1092 decl.srcLoc(mod),1086 decl.srcLoc(mod),
1093 )) {1087 )) {
...@@ -1113,7 +1107,7 @@ fn lowerConst(...@@ -1113,7 +1107,7 @@ fn lowerConst(
1113 self: *ZigObject,1107 self: *ZigObject,
1114 macho_file: *MachO,1108 macho_file: *MachO,
1115 name: []const u8,1109 name: []const u8,
1116 tv: TypedValue,1110 val: Value,
1117 required_alignment: Atom.Alignment,1111 required_alignment: Atom.Alignment,
1118 output_section_index: u8,1112 output_section_index: u8,
1119 src_loc: Module.SrcLoc,1113 src_loc: Module.SrcLoc,
...@@ -1125,7 +1119,7 @@ fn lowerConst(...@@ -1125,7 +1119,7 @@ fn lowerConst(
11251119
1126 const sym_index = try self.addAtom(macho_file);1120 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, .{
1129 .none = {},1123 .none = {},
1130 }, .{1124 }, .{
1131 .parent_atom_index = sym_index,1125 .parent_atom_index = sym_index,
...@@ -1580,5 +1574,4 @@ const Symbol = @import("Symbol.zig");...@@ -1580,5 +1574,4 @@ const Symbol = @import("Symbol.zig");
1580const StringTable = @import("../StringTable.zig");1574const StringTable = @import("../StringTable.zig");
1581const Type = @import("../../type.zig").Type;1575const Type = @import("../../type.zig").Type;
1582const Value = @import("../../Value.zig");1576const Value = @import("../../Value.zig");
1583const TypedValue = @import("../../TypedValue.zig");
1584const ZigObject = @This();1577const ZigObject = @This();
src/link/Plan9.zig+4-11
...@@ -15,7 +15,6 @@ const Air = @import("../Air.zig");...@@ -15,7 +15,6 @@ const Air = @import("../Air.zig");
15const Liveness = @import("../Liveness.zig");15const Liveness = @import("../Liveness.zig");
16const Type = @import("../type.zig").Type;16const Type = @import("../type.zig").Type;
17const Value = @import("../Value.zig");17const Value = @import("../Value.zig");
18const TypedValue = @import("../TypedValue.zig");
1918
20const std = @import("std");19const std = @import("std");
21const builtin = @import("builtin");20const builtin = @import("builtin");
...@@ -463,7 +462,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:...@@ -463,7 +462,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air:
463 return self.updateFinish(decl_index);462 return self.updateFinish(decl_index);
464}463}
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 {
467 const gpa = self.base.comp.gpa;466 const gpa = self.base.comp.gpa;
468 _ = try self.seeDecl(decl_index);467 _ = try self.seeDecl(decl_index);
469 var code_buffer = std.ArrayList(u8).init(gpa);468 var code_buffer = std.ArrayList(u8).init(gpa);
...@@ -500,7 +499,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: InternPool.De...@@ -500,7 +499,7 @@ pub fn lowerUnnamedConst(self: *Plan9, tv: TypedValue, decl_index: InternPool.De
500 };499 };
501 self.syms.items[info.sym_index.?] = sym;500 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, .{
504 .none = {},503 .none = {},
505 }, .{504 }, .{
506 .parent_atom_index = new_atom_idx,505 .parent_atom_index = new_atom_idx,
...@@ -539,10 +538,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex)...@@ -539,10 +538,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex)
539 defer code_buffer.deinit();538 defer code_buffer.deinit();
540 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;539 const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val;
541 // TODO we need the symbol index for symbol in the table of locals for the containing atom540 // 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), .{541 const res = try codegen.generateSymbol(&self.base, decl.srcLoc(mod), decl_val, &code_buffer, .{ .none = {} }, .{
543 .ty = decl.typeOf(mod),
544 .val = decl_val,
545 }, &code_buffer, .{ .none = {} }, .{
546 .parent_atom_index = @as(Atom.Index, @intCast(atom_idx)),542 .parent_atom_index = @as(Atom.Index, @intCast(atom_idx)),
547 });543 });
548 const code = switch (res) {544 const code = switch (res) {
...@@ -1545,11 +1541,8 @@ pub fn lowerAnonDecl(...@@ -1545,11 +1541,8 @@ pub fn lowerAnonDecl(
1545 // ...1541 // ...
1546 const gpa = self.base.comp.gpa;1542 const gpa = self.base.comp.gpa;
1547 const gop = try self.anon_decls.getOrPut(gpa, decl_val);1543 const gop = try self.anon_decls.getOrPut(gpa, decl_val);
1548 const mod = self.base.comp.module.?;
1549 if (!gop.found_existing) {1544 if (!gop.found_existing) {
1550 const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
1551 const val = Value.fromInterned(decl_val);1545 const val = Value.fromInterned(decl_val);
1552 const tv = TypedValue{ .ty = ty, .val = val };
1553 const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)});1546 const name = try std.fmt.allocPrint(gpa, "__anon_{d}", .{@intFromEnum(decl_val)});
15541547
1555 const index = try self.createAtom();1548 const index = try self.createAtom();
...@@ -1557,7 +1550,7 @@ pub fn lowerAnonDecl(...@@ -1557,7 +1550,7 @@ pub fn lowerAnonDecl(
1557 gop.value_ptr.* = index;1550 gop.value_ptr.* = index;
1558 // we need to free name latex1551 // we need to free name latex
1559 var code_buffer = std.ArrayList(u8).init(gpa);1552 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 });
1561 const code = switch (res) {1554 const code = switch (res) {
1562 .ok => code_buffer.items,1555 .ok => code_buffer.items,
1563 .fail => |em| return .{ .fail = em },1556 .fail => |em| return .{ .fail = em },
src/link/Wasm.zig+3-3
...@@ -32,7 +32,7 @@ const Module = @import("../Module.zig");...@@ -32,7 +32,7 @@ const Module = @import("../Module.zig");
32const Object = @import("Wasm/Object.zig");32const Object = @import("Wasm/Object.zig");
33const Symbol = @import("Wasm/Symbol.zig");33const Symbol = @import("Wasm/Symbol.zig");
34const Type = @import("../type.zig").Type;34const Type = @import("../type.zig").Type;
35const TypedValue = @import("../TypedValue.zig");35const Value = @import("../Value.zig");
36const ZigObject = @import("Wasm/ZigObject.zig");36const ZigObject = @import("Wasm/ZigObject.zig");
3737
38pub const Atom = @import("Wasm/Atom.zig");38pub const Atom = @import("Wasm/Atom.zig");
...@@ -1504,8 +1504,8 @@ fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type {...@@ -1504,8 +1504,8 @@ fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type {
1504/// Lowers a constant typed value to a local symbol and atom.1504/// Lowers a constant typed value to a local symbol and atom.
1505/// Returns the symbol index of the local1505/// Returns the symbol index of the local
1506/// The given `decl` is the parent decl whom owns the constant.1506/// The given `decl` is the parent decl whom owns the constant.
1507pub fn lowerUnnamedConst(wasm: *Wasm, tv: TypedValue, decl_index: InternPool.DeclIndex) !u32 {1507pub fn lowerUnnamedConst(wasm: *Wasm, val: Value, decl_index: InternPool.DeclIndex) !u32 {
1508 return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, tv, decl_index);1508 return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, val, decl_index);
1509}1509}
15101510
1511/// Returns the symbol index from a symbol of which its flag is set global,1511/// 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(...@@ -270,7 +270,7 @@ pub fn updateDecl(
270 const res = try codegen.generateSymbol(270 const res = try codegen.generateSymbol(
271 &wasm_file.base,271 &wasm_file.base,
272 decl.srcLoc(mod),272 decl.srcLoc(mod),
273 .{ .ty = decl.typeOf(mod), .val = val },273 val,
274 &code_writer,274 &code_writer,
275 .none,275 .none,
276 .{ .parent_atom_index = @intFromEnum(atom.sym_index) },276 .{ .parent_atom_index = @intFromEnum(atom.sym_index) },
...@@ -444,15 +444,12 @@ pub fn lowerAnonDecl(...@@ -444,15 +444,12 @@ pub fn lowerAnonDecl(
444 const gpa = wasm_file.base.comp.gpa;444 const gpa = wasm_file.base.comp.gpa;
445 const gop = try zig_object.anon_decls.getOrPut(gpa, decl_val);445 const gop = try zig_object.anon_decls.getOrPut(gpa, decl_val);
446 if (!gop.found_existing) {446 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) };
450 var name_buf: [32]u8 = undefined;447 var name_buf: [32]u8 = undefined;
451 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{448 const name = std.fmt.bufPrint(&name_buf, "__anon_{d}", .{
452 @intFromEnum(decl_val),449 @intFromEnum(decl_val),
453 }) catch unreachable;450 }) 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)) {
456 .ok => |atom_index| zig_object.anon_decls.values()[gop.index] = atom_index,453 .ok => |atom_index| zig_object.anon_decls.values()[gop.index] = atom_index,
457 .fail => |em| return .{ .fail = em },454 .fail => |em| return .{ .fail = em },
458 }455 }
...@@ -472,10 +469,10 @@ pub fn lowerAnonDecl(...@@ -472,10 +469,10 @@ pub fn lowerAnonDecl(
472/// Lowers a constant typed value to a local symbol and atom.469/// Lowers a constant typed value to a local symbol and atom.
473/// Returns the symbol index of the local470/// Returns the symbol index of the local
474/// The given `decl` is the parent decl whom owns the constant.471/// 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 {
476 const gpa = wasm_file.base.comp.gpa;473 const gpa = wasm_file.base.comp.gpa;
477 const mod = wasm_file.base.comp.module.?;474 const mod = wasm_file.base.comp.module.?;
478 std.debug.assert(tv.ty.zigTypeTag(mod) != .Fn); // cannot create local symbols for functions475 std.debug.assert(val.typeOf(mod).zigTypeTag(mod) != .Fn); // cannot create local symbols for functions
479 const decl = mod.declPtr(decl_index);476 const decl = mod.declPtr(decl_index);
480477
481 const parent_atom_index = try zig_object.getOrCreateAtomForDecl(wasm_file, decl_index);478 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...@@ -487,7 +484,7 @@ pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, tv: TypedValu
487 });484 });
488 defer gpa.free(name);485 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))) {
491 .ok => |atom_index| {488 .ok => |atom_index| {
492 try wasm_file.getAtomPtr(parent_atom_index).locals.append(gpa, atom_index);489 try wasm_file.getAtomPtr(parent_atom_index).locals.append(gpa, atom_index);
493 return @intFromEnum(wasm_file.getAtom(atom_index).sym_index);490 return @intFromEnum(wasm_file.getAtom(atom_index).sym_index);
...@@ -505,10 +502,12 @@ const LowerConstResult = union(enum) {...@@ -505,10 +502,12 @@ const LowerConstResult = union(enum) {
505 fail: *Module.ErrorMsg,502 fail: *Module.ErrorMsg,
506};503};
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 {
509 const gpa = wasm_file.base.comp.gpa;506 const gpa = wasm_file.base.comp.gpa;
510 const mod = wasm_file.base.comp.module.?;507 const mod = wasm_file.base.comp.module.?;
511508
509 const ty = val.typeOf(mod);
510
512 // Create and initialize a new local symbol and atom511 // Create and initialize a new local symbol and atom
513 const sym_index = try zig_object.allocateSymbol(gpa);512 const sym_index = try zig_object.allocateSymbol(gpa);
514 const atom_index = try wasm_file.createAtom(sym_index, zig_object.index);513 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...@@ -517,7 +516,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
517516
518 const code = code: {517 const code = code: {
519 const atom = wasm_file.getAtomPtr(atom_index);518 const atom = wasm_file.getAtomPtr(atom_index);
520 atom.alignment = tv.ty.abiAlignment(mod);519 atom.alignment = ty.abiAlignment(mod);
521 const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name });520 const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name });
522 errdefer gpa.free(segment_name);521 errdefer gpa.free(segment_name);
523 zig_object.symbol(sym_index).* = .{522 zig_object.symbol(sym_index).* = .{
...@@ -527,7 +526,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty...@@ -527,7 +526,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
527 .index = try zig_object.createDataSegment(526 .index = try zig_object.createDataSegment(
528 gpa,527 gpa,
529 segment_name,528 segment_name,
530 tv.ty.abiAlignment(mod),529 ty.abiAlignment(mod),
531 ),530 ),
532 .virtual_address = undefined,531 .virtual_address = undefined,
533 };532 };
...@@ -535,7 +534,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty...@@ -535,7 +534,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, tv: Ty
535 const result = try codegen.generateSymbol(534 const result = try codegen.generateSymbol(
536 &wasm_file.base,535 &wasm_file.base,
537 src_loc,536 src_loc,
538 tv,537 val,
539 &value_bytes,538 &value_bytes,
540 .none,539 .none,
541 .{540 .{
...@@ -1242,7 +1241,6 @@ const Module = @import("../../Module.zig");...@@ -1242,7 +1241,6 @@ const Module = @import("../../Module.zig");
1242const StringTable = @import("../StringTable.zig");1241const StringTable = @import("../StringTable.zig");
1243const Symbol = @import("Symbol.zig");1242const Symbol = @import("Symbol.zig");
1244const Type = @import("../../type.zig").Type;1243const Type = @import("../../type.zig").Type;
1245const TypedValue = @import("../../TypedValue.zig");
1246const Value = @import("../../Value.zig");1244const Value = @import("../../Value.zig");
1247const Wasm = @import("../Wasm.zig");1245const Wasm = @import("../Wasm.zig");
1248const ZigObject = @This();1246const ZigObject = @This();
src/type.zig-1
...@@ -7,7 +7,6 @@ const Module = @import("Module.zig");...@@ -7,7 +7,6 @@ const Module = @import("Module.zig");
7const Zcu = Module;7const Zcu = Module;
8const log = std.log.scoped(.Type);8const log = std.log.scoped(.Type);
9const target_util = @import("target.zig");9const target_util = @import("target.zig");
10const TypedValue = @import("TypedValue.zig");
11const Sema = @import("Sema.zig");10const Sema = @import("Sema.zig");
12const InternPool = @import("InternPool.zig");11const InternPool = @import("InternPool.zig");
13const Alignment = InternPool.Alignment;12const Alignment = InternPool.Alignment;