authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-20 00:35:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-20 00:36:44-07:00
log0576086395774389a9f38d960f9ed5102a813bdb
tree43558a9b825a3312865ec76d2da7f6980d0b1a70
parent3ef34feaeb3a18926bead1981e5ce577382da38e

stage2: remove Value.Tag.abi_align_default

and make Decl alignment & linksection, and struct & union field alignment be scalar values, not Value values. YAGNI

5 files changed, 79 insertions(+), 110 deletions(-)

src/Module.zig+37-33
...@@ -349,12 +349,13 @@ pub const Decl = struct {...@@ -349,12 +349,13 @@ pub const Decl = struct {
349 /// Populated when `has_tv`.349 /// Populated when `has_tv`.
350 val: Value,350 val: Value,
351 /// Populated when `has_tv`.351 /// Populated when `has_tv`.
352 align_val: Value,352 /// Points to memory inside value_arena.
353 @"linksection": ?[*:0]const u8,
353 /// Populated when `has_tv`.354 /// Populated when `has_tv`.
354 linksection_val: Value,355 @"align": u32,
355 /// Populated when `has_tv`.356 /// Populated when `has_tv`.
356 @"addrspace": std.builtin.AddressSpace,357 @"addrspace": std.builtin.AddressSpace,
357 /// The memory for ty, val, align_val, linksection_val, and captures.358 /// The memory for ty, val, align, linksection, and captures.
358 /// If this is `null` then there is no memory management needed.359 /// If this is `null` then there is no memory management needed.
359 value_arena: ?*std.heap.ArenaAllocator.State = null,360 value_arena: ?*std.heap.ArenaAllocator.State = null,
360 /// The direct parent namespace of the Decl.361 /// The direct parent namespace of the Decl.
...@@ -423,7 +424,7 @@ pub const Decl = struct {...@@ -423,7 +424,7 @@ pub const Decl = struct {
423 /// to require re-analysis.424 /// to require re-analysis.
424 outdated,425 outdated,
425 },426 },
426 /// Whether `typed_value`, `align_val`, `linksection_val` and `addrspace` are populated.427 /// Whether `typed_value`, `align`, `linksection` and `addrspace` are populated.
427 has_tv: bool,428 has_tv: bool,
428 /// If `true` it means the `Decl` is the resource owner of the type/value associated429 /// If `true` it means the `Decl` is the resource owner of the type/value associated
429 /// with it. That means when `Decl` is destroyed, the cleanup code should additionally430 /// with it. That means when `Decl` is destroyed, the cleanup code should additionally
...@@ -794,9 +795,9 @@ pub const Decl = struct {...@@ -794,9 +795,9 @@ pub const Decl = struct {
794795
795 pub fn getAlignment(decl: Decl, target: Target) u32 {796 pub fn getAlignment(decl: Decl, target: Target) u32 {
796 assert(decl.has_tv);797 assert(decl.has_tv);
797 if (decl.align_val.tag() != .null_value) {798 if (decl.@"align" != 0) {
798 // Explicit alignment.799 // Explicit alignment.
799 return @intCast(u32, decl.align_val.toUnsignedInt());800 return decl.@"align";
800 } else {801 } else {
801 // Natural alignment.802 // Natural alignment.
802 return decl.ty.abiAlignment(target);803 return decl.ty.abiAlignment(target);
...@@ -893,9 +894,10 @@ pub const Struct = struct {...@@ -893,9 +894,10 @@ pub const Struct = struct {
893 /// Uses `noreturn` to indicate `anytype`.894 /// Uses `noreturn` to indicate `anytype`.
894 /// undefined until `status` is `have_field_types` or `have_layout`.895 /// undefined until `status` is `have_field_types` or `have_layout`.
895 ty: Type,896 ty: Type,
896 abi_align: Value,
897 /// Uses `unreachable_value` to indicate no default.897 /// Uses `unreachable_value` to indicate no default.
898 default_val: Value,898 default_val: Value,
899 /// Zero means to use the ABI alignment of the type.
900 abi_align: u32,
899 /// undefined until `status` is `have_layout`.901 /// undefined until `status` is `have_layout`.
900 offset: u32,902 offset: u32,
901 /// If true then `default_val` is the comptime field value.903 /// If true then `default_val` is the comptime field value.
...@@ -903,10 +905,10 @@ pub const Struct = struct {...@@ -903,10 +905,10 @@ pub const Struct = struct {
903905
904 /// Returns the field alignment, assuming the struct is not packed.906 /// Returns the field alignment, assuming the struct is not packed.
905 pub fn normalAlignment(field: Field, target: Target) u32 {907 pub fn normalAlignment(field: Field, target: Target) u32 {
906 if (field.abi_align.tag() == .abi_align_default) {908 if (field.abi_align == 0) {
907 return field.ty.abiAlignment(target);909 return field.ty.abiAlignment(target);
908 } else {910 } else {
909 return @intCast(u32, field.abi_align.toUnsignedInt());911 return field.abi_align;
910 }912 }
911 }913 }
912 };914 };
...@@ -1139,16 +1141,17 @@ pub const Union = struct {...@@ -1139,16 +1141,17 @@ pub const Union = struct {
1139 pub const Field = struct {1141 pub const Field = struct {
1140 /// undefined until `status` is `have_field_types` or `have_layout`.1142 /// undefined until `status` is `have_field_types` or `have_layout`.
1141 ty: Type,1143 ty: Type,
1142 abi_align: Value,1144 /// 0 means the ABI alignment of the type.
1145 abi_align: u32,
11431146
1144 /// Returns the field alignment, assuming the union is not packed.1147 /// Returns the field alignment, assuming the union is not packed.
1145 /// Keep implementation in sync with `Sema.unionFieldAlignment`.1148 /// Keep implementation in sync with `Sema.unionFieldAlignment`.
1146 /// Prefer to call that function instead of this one during Sema.1149 /// Prefer to call that function instead of this one during Sema.
1147 pub fn normalAlignment(field: Field, target: Target) u32 {1150 pub fn normalAlignment(field: Field, target: Target) u32 {
1148 if (field.abi_align.tag() == .abi_align_default) {1151 if (field.abi_align == 0) {
1149 return field.ty.abiAlignment(target);1152 return field.ty.abiAlignment(target);
1150 } else {1153 } else {
1151 return @intCast(u32, field.abi_align.toUnsignedInt());1154 return field.abi_align;
1152 }1155 }
1153 }1156 }
1154 };1157 };
...@@ -1224,7 +1227,7 @@ pub const Union = struct {...@@ -1224,7 +1227,7 @@ pub const Union = struct {
1224 if (!field.ty.hasRuntimeBits()) continue;1227 if (!field.ty.hasRuntimeBits()) continue;
12251228
1226 const field_align = a: {1229 const field_align = a: {
1227 if (field.abi_align.tag() == .abi_align_default) {1230 if (field.abi_align == 0) {
1228 break :a field.ty.abiAlignment(target);1231 break :a field.ty.abiAlignment(target);
1229 } else {1232 } else {
1230 break :a @intCast(u32, field.abi_align.toUnsignedInt());1233 break :a @intCast(u32, field.abi_align.toUnsignedInt());
...@@ -1246,10 +1249,10 @@ pub const Union = struct {...@@ -1246,10 +1249,10 @@ pub const Union = struct {
1246 if (!field.ty.hasRuntimeBits()) continue;1249 if (!field.ty.hasRuntimeBits()) continue;
12471250
1248 const field_align = a: {1251 const field_align = a: {
1249 if (field.abi_align.tag() == .abi_align_default) {1252 if (field.abi_align == 0) {
1250 break :a field.ty.abiAlignment(target);1253 break :a field.ty.abiAlignment(target);
1251 } else {1254 } else {
1252 break :a @intCast(u32, field.abi_align.toUnsignedInt());1255 break :a field.abi_align;
1253 }1256 }
1254 };1257 };
1255 max_align = @maximum(max_align, field_align);1258 max_align = @maximum(max_align, field_align);
...@@ -1300,10 +1303,10 @@ pub const Union = struct {...@@ -1300,10 +1303,10 @@ pub const Union = struct {
1300 if (!field.ty.hasRuntimeBitsIgnoreComptime()) continue;1303 if (!field.ty.hasRuntimeBitsIgnoreComptime()) continue;
13011304
1302 const field_align = a: {1305 const field_align = a: {
1303 if (field.abi_align.tag() == .abi_align_default) {1306 if (field.abi_align == 0) {
1304 break :a field.ty.abiAlignment(target);1307 break :a field.ty.abiAlignment(target);
1305 } else {1308 } else {
1306 break :a @intCast(u32, field.abi_align.toUnsignedInt());1309 break :a field.abi_align;
1307 }1310 }
1308 };1311 };
1309 const field_size = field.ty.abiSize(target);1312 const field_size = field.ty.abiSize(target);
...@@ -3863,15 +3866,16 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3863,15 +3866,16 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
3863 try wip_captures.finalize();3866 try wip_captures.finalize();
3864 const src: LazySrcLoc = .{ .node_offset = 0 };3867 const src: LazySrcLoc = .{ .node_offset = 0 };
3865 const decl_tv = try sema.resolveInstValue(&block_scope, src, result_ref);3868 const decl_tv = try sema.resolveInstValue(&block_scope, src, result_ref);
3866 const align_val = blk: {3869 const decl_align: u16 = blk: {
3867 const align_ref = decl.zirAlignRef();3870 const align_ref = decl.zirAlignRef();
3868 if (align_ref == .none) break :blk Value.initTag(.null_value);3871 if (align_ref == .none) break :blk 0;
3869 break :blk (try sema.resolveInstConst(&block_scope, src, align_ref)).val;3872 break :blk try sema.resolveAlign(&block_scope, src, align_ref);
3870 };3873 };
3871 const linksection_val = blk: {3874 const decl_linksection: ?[*:0]const u8 = blk: {
3872 const linksection_ref = decl.zirLinksectionRef();3875 const linksection_ref = decl.zirLinksectionRef();
3873 if (linksection_ref == .none) break :blk Value.initTag(.null_value);3876 if (linksection_ref == .none) break :blk null;
3874 break :blk (try sema.resolveInstConst(&block_scope, src, linksection_ref)).val;3877 const bytes = try sema.resolveConstString(&block_scope, src, linksection_ref);
3878 break :blk (try decl_arena_allocator.dupeZ(u8, bytes)).ptr;
3875 };3879 };
3876 const address_space = blk: {3880 const address_space = blk: {
3877 const addrspace_ctx: Sema.AddressSpaceContext = switch (decl_tv.val.tag()) {3881 const addrspace_ctx: Sema.AddressSpaceContext = switch (decl_tv.val.tag()) {
...@@ -3911,8 +3915,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3911,8 +3915,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
39113915
3912 decl.ty = ty_ty;3916 decl.ty = ty_ty;
3913 decl.val = try Value.Tag.ty.create(decl_arena_allocator, ty);3917 decl.val = try Value.Tag.ty.create(decl_arena_allocator, ty);
3914 decl.align_val = Value.initTag(.null_value);3918 decl.@"align" = 0;
3915 decl.linksection_val = Value.initTag(.null_value);3919 decl.@"linksection" = null;
3916 decl.has_tv = true;3920 decl.has_tv = true;
3917 decl.owns_tv = false;3921 decl.owns_tv = false;
3918 decl_arena_state.* = decl_arena.state;3922 decl_arena_state.* = decl_arena.state;
...@@ -3942,8 +3946,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -3942,8 +3946,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
39423946
3943 decl.ty = try decl_tv.ty.copy(decl_arena_allocator);3947 decl.ty = try decl_tv.ty.copy(decl_arena_allocator);
3944 decl.val = try decl_tv.val.copy(decl_arena_allocator);3948 decl.val = try decl_tv.val.copy(decl_arena_allocator);
3945 decl.align_val = try align_val.copy(decl_arena_allocator);3949 decl.@"align" = decl_align;
3946 decl.linksection_val = try linksection_val.copy(decl_arena_allocator);3950 decl.@"linksection" = decl_linksection;
3947 decl.@"addrspace" = address_space;3951 decl.@"addrspace" = address_space;
3948 decl.has_tv = true;3952 decl.has_tv = true;
3949 decl.owns_tv = owns_tv;3953 decl.owns_tv = owns_tv;
...@@ -4022,8 +4026,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {...@@ -4022,8 +4026,8 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
40224026
4023 decl.ty = try decl_tv.ty.copy(decl_arena_allocator);4027 decl.ty = try decl_tv.ty.copy(decl_arena_allocator);
4024 decl.val = try decl_tv.val.copy(decl_arena_allocator);4028 decl.val = try decl_tv.val.copy(decl_arena_allocator);
4025 decl.align_val = try align_val.copy(decl_arena_allocator);4029 decl.@"align" = decl_align;
4026 decl.linksection_val = try linksection_val.copy(decl_arena_allocator);4030 decl.@"linksection" = decl_linksection;
4027 decl.@"addrspace" = address_space;4031 decl.@"addrspace" = address_space;
4028 decl.has_tv = true;4032 decl.has_tv = true;
4029 decl_arena_state.* = decl_arena.state;4033 decl_arena_state.* = decl_arena.state;
...@@ -4889,8 +4893,8 @@ pub fn allocateNewDecl(...@@ -4889,8 +4893,8 @@ pub fn allocateNewDecl(
4889 .owns_tv = false,4893 .owns_tv = false,
4890 .ty = undefined,4894 .ty = undefined,
4891 .val = undefined,4895 .val = undefined,
4892 .align_val = undefined,4896 .@"align" = undefined,
4893 .linksection_val = undefined,4897 .@"linksection" = undefined,
4894 .@"addrspace" = .generic,4898 .@"addrspace" = .generic,
4895 .analysis = .unreferenced,4899 .analysis = .unreferenced,
4896 .deletion_flag = false,4900 .deletion_flag = false,
...@@ -4995,8 +4999,8 @@ pub fn createAnonymousDeclFromDeclNamed(...@@ -4995,8 +4999,8 @@ pub fn createAnonymousDeclFromDeclNamed(
4995 new_decl.src_line = src_decl.src_line;4999 new_decl.src_line = src_decl.src_line;
4996 new_decl.ty = typed_value.ty;5000 new_decl.ty = typed_value.ty;
4997 new_decl.val = typed_value.val;5001 new_decl.val = typed_value.val;
4998 new_decl.align_val = Value.@"null";5002 new_decl.@"align" = 0;
4999 new_decl.linksection_val = Value.@"null";5003 new_decl.@"linksection" = null;
5000 new_decl.has_tv = true;5004 new_decl.has_tv = true;
5001 new_decl.analysis = .complete;5005 new_decl.analysis = .complete;
5002 new_decl.generation = mod.generation;5006 new_decl.generation = mod.generation;
src/Sema.zig+21-36
...@@ -486,19 +486,17 @@ pub const Block = struct {...@@ -486,19 +486,17 @@ pub const Block = struct {
486 wad.* = undefined;486 wad.* = undefined;
487 }487 }
488488
489 /// `alignment` value of 0 means to use ABI alignment.
489 pub fn finish(wad: *WipAnonDecl, ty: Type, val: Value, alignment: u32) !*Decl {490 pub fn finish(wad: *WipAnonDecl, ty: Type, val: Value, alignment: u32) !*Decl {
490 const sema = wad.block.sema;491 const sema = wad.block.sema;
491 // Do this ahead of time because `createAnonymousDecl` depends on calling492 // Do this ahead of time because `createAnonymousDecl` depends on calling
492 // `type.hasRuntimeBits()`.493 // `type.hasRuntimeBits()`.
493 _ = try sema.typeHasRuntimeBits(wad.block, wad.src, ty);494 _ = try sema.typeHasRuntimeBits(wad.block, wad.src, ty);
494 const align_val = if (alignment != 0) blk: {
495 break :blk try Value.Tag.int_u64.create(wad.arena(), alignment);
496 } else Value.@"null";
497 const new_decl = try sema.mod.createAnonymousDecl(wad.block, .{495 const new_decl = try sema.mod.createAnonymousDecl(wad.block, .{
498 .ty = ty,496 .ty = ty,
499 .val = val,497 .val = val,
500 });498 });
501 new_decl.align_val = align_val;499 new_decl.@"align" = alignment;
502 errdefer sema.mod.abortAnonDecl(new_decl);500 errdefer sema.mod.abortAnonDecl(new_decl);
503 try new_decl.finalizeNewArena(&wad.new_decl_arena);501 try new_decl.finalizeNewArena(&wad.new_decl_arena);
504 wad.finished = true;502 wad.finished = true;
...@@ -1281,7 +1279,7 @@ fn resolveConstBool(...@@ -1281,7 +1279,7 @@ fn resolveConstBool(
1281 return val.toBool();1279 return val.toBool();
1282}1280}
12831281
1284fn resolveConstString(1282pub fn resolveConstString(
1285 sema: *Sema,1283 sema: *Sema,
1286 block: *Block,1284 block: *Block,
1287 src: LazySrcLoc,1285 src: LazySrcLoc,
...@@ -1539,7 +1537,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, block: *Block, err_msg: *Module.ErrorMsg)...@@ -1539,7 +1537,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, block: *Block, err_msg: *Module.ErrorMsg)
1539 return error.AnalysisFail;1537 return error.AnalysisFail;
1540}1538}
15411539
1542fn resolveAlign(1540pub fn resolveAlign(
1543 sema: *Sema,1541 sema: *Sema,
1544 block: *Block,1542 block: *Block,
1545 src: LazySrcLoc,1543 src: LazySrcLoc,
...@@ -13061,7 +13059,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I...@@ -13061,7 +13059,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I
13061 var buffer: Value.ToTypeBuffer = undefined;13059 var buffer: Value.ToTypeBuffer = undefined;
13062 gop.value_ptr.* = .{13060 gop.value_ptr.* = .{
13063 .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator),13061 .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator),
13064 .abi_align = try alignment_val.copy(new_decl_arena_allocator),13062 .abi_align = @intCast(u32, alignment_val.toUnsignedInt()),
13065 };13063 };
13066 }13064 }
13067 }13065 }
...@@ -13230,7 +13228,7 @@ fn reifyStruct(...@@ -13230,7 +13228,7 @@ fn reifyStruct(
13230 var buffer: Value.ToTypeBuffer = undefined;13228 var buffer: Value.ToTypeBuffer = undefined;
13231 gop.value_ptr.* = .{13229 gop.value_ptr.* = .{
13232 .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator),13230 .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator),
13233 .abi_align = try alignment_val.copy(new_decl_arena_allocator),13231 .abi_align = @intCast(u32, alignment_val.toUnsignedInt()),
13234 .default_val = default_val,13232 .default_val = default_val,
13235 .is_comptime = is_comptime_val.toBool(),13233 .is_comptime = is_comptime_val.toBool(),
13236 .offset = undefined,13234 .offset = undefined,
...@@ -14949,9 +14947,9 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -14949,9 +14947,9 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr
14949 };14947 };
1495014948
14951 if (struct_obj.layout == .Packed) {14949 if (struct_obj.layout == .Packed) {
14952 // TODO handle packed structs14950 return sema.fail(block, src, "TODO handle packed structs with @fieldParentPtr", .{});
14953 } else if (field.abi_align.tag() != .abi_align_default) {14951 } else {
14954 ptr_ty_data.@"align" = @intCast(u32, field.abi_align.toUnsignedInt());14952 ptr_ty_data.@"align" = field.abi_align;
14955 }14953 }
1495614954
14957 const target = sema.mod.getTarget();14955 const target = sema.mod.getTarget();
...@@ -15552,8 +15550,8 @@ fn zirBuiltinExtern(...@@ -15552,8 +15550,8 @@ fn zirBuiltinExtern(
15552 new_decl.src_line = sema.owner_decl.src_line;15550 new_decl.src_line = sema.owner_decl.src_line;
15553 new_decl.ty = try ty.copy(new_decl_arena_allocator);15551 new_decl.ty = try ty.copy(new_decl_arena_allocator);
15554 new_decl.val = try Value.Tag.variable.create(new_decl_arena_allocator, new_var);15552 new_decl.val = try Value.Tag.variable.create(new_decl_arena_allocator, new_var);
15555 new_decl.align_val = Value.@"null";15553 new_decl.@"align" = 0;
15556 new_decl.linksection_val = Value.@"null";15554 new_decl.@"linksection" = null;
15557 new_decl.has_tv = true;15555 new_decl.has_tv = true;
15558 new_decl.analysis = .complete;15556 new_decl.analysis = .complete;
15559 new_decl.generation = sema.mod.generation;15557 new_decl.generation = sema.mod.generation;
...@@ -16543,9 +16541,7 @@ fn structFieldPtrByIndex(...@@ -16543,9 +16541,7 @@ fn structFieldPtrByIndex(
16543 ptr_ty_data.bit_offset += struct_ptr_ty_info.bit_offset;16541 ptr_ty_data.bit_offset += struct_ptr_ty_info.bit_offset;
16544 }16542 }
16545 } else {16543 } else {
16546 if (field.abi_align.tag() != .abi_align_default) {16544 ptr_ty_data.@"align" = field.abi_align;
16547 ptr_ty_data.@"align" = @intCast(u32, field.abi_align.toUnsignedInt());
16548 }
16549 }16545 }
1655016546
16551 const target = sema.mod.getTarget();16547 const target = sema.mod.getTarget();
...@@ -19168,15 +19164,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {...@@ -19168,15 +19164,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
19168 const decl_tv = try decl.typedValue();19164 const decl_tv = try decl.typedValue();
19169 if (decl_tv.val.castTag(.variable)) |payload| {19165 if (decl_tv.val.castTag(.variable)) |payload| {
19170 const variable = payload.data;19166 const variable = payload.data;
19171 const alignment: u32 = if (decl.align_val.tag() == .null_value)
19172 0
19173 else
19174 @intCast(u32, decl.align_val.toUnsignedInt());
19175 const ty = try Type.ptr(sema.arena, target, .{19167 const ty = try Type.ptr(sema.arena, target, .{
19176 .pointee_type = decl_tv.ty,19168 .pointee_type = decl_tv.ty,
19177 .mutable = variable.is_mutable,19169 .mutable = variable.is_mutable,
19178 .@"addrspace" = decl.@"addrspace",19170 .@"addrspace" = decl.@"addrspace",
19179 .@"align" = alignment,19171 .@"align" = decl.@"align",
19180 });19172 });
19181 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));19173 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));
19182 }19174 }
...@@ -20848,7 +20840,7 @@ fn semaStructFields(...@@ -20848,7 +20840,7 @@ fn semaStructFields(
20848 assert(!gop.found_existing);20840 assert(!gop.found_existing);
20849 gop.value_ptr.* = .{20841 gop.value_ptr.* = .{
20850 .ty = try field_ty.copy(decl_arena_allocator),20842 .ty = try field_ty.copy(decl_arena_allocator),
20851 .abi_align = Value.initTag(.abi_align_default),20843 .abi_align = 0,
20852 .default_val = Value.initTag(.unreachable_value),20844 .default_val = Value.initTag(.unreachable_value),
20853 .is_comptime = is_comptime,20845 .is_comptime = is_comptime,
20854 .offset = undefined,20846 .offset = undefined,
...@@ -20860,8 +20852,7 @@ fn semaStructFields(...@@ -20860,8 +20852,7 @@ fn semaStructFields(
20860 // TODO: if we need to report an error here, use a source location20852 // TODO: if we need to report an error here, use a source location
20861 // that points to this alignment expression rather than the struct.20853 // that points to this alignment expression rather than the struct.
20862 // But only resolve the source location if we need to emit a compile error.20854 // But only resolve the source location if we need to emit a compile error.
20863 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;20855 gop.value_ptr.abi_align = try sema.resolveAlign(&block_scope, src, align_ref);
20864 gop.value_ptr.abi_align = try abi_align_val.copy(decl_arena_allocator);
20865 }20856 }
20866 if (has_default) {20857 if (has_default) {
20867 const default_ref = @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);20858 const default_ref = @intToEnum(Zir.Inst.Ref, zir.extra[extra_index]);
...@@ -21088,17 +21079,16 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {...@@ -21088,17 +21079,16 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void {
21088 assert(!gop.found_existing);21079 assert(!gop.found_existing);
21089 gop.value_ptr.* = .{21080 gop.value_ptr.* = .{
21090 .ty = try field_ty.copy(decl_arena_allocator),21081 .ty = try field_ty.copy(decl_arena_allocator),
21091 .abi_align = Value.initTag(.abi_align_default),21082 .abi_align = 0,
21092 };21083 };
2109321084
21094 if (align_ref != .none) {21085 if (align_ref != .none) {
21095 // TODO: if we need to report an error here, use a source location21086 // TODO: if we need to report an error here, use a source location
21096 // that points to this alignment expression rather than the struct.21087 // that points to this alignment expression rather than the struct.
21097 // But only resolve the source location if we need to emit a compile error.21088 // But only resolve the source location if we need to emit a compile error.
21098 const abi_align_val = (try sema.resolveInstConst(&block_scope, src, align_ref)).val;21089 gop.value_ptr.abi_align = try sema.resolveAlign(&block_scope, src, align_ref);
21099 gop.value_ptr.abi_align = try abi_align_val.copy(decl_arena_allocator);
21100 } else {21090 } else {
21101 gop.value_ptr.abi_align = Value.initTag(.abi_align_default);21091 gop.value_ptr.abi_align = 0;
21102 }21092 }
21103 }21093 }
21104}21094}
...@@ -21638,11 +21628,6 @@ fn analyzeComptimeAlloc(...@@ -21638,11 +21628,6 @@ fn analyzeComptimeAlloc(
21638 var anon_decl = try block.startAnonDecl(src);21628 var anon_decl = try block.startAnonDecl(src);
21639 defer anon_decl.deinit();21629 defer anon_decl.deinit();
2164021630
21641 const align_val = if (alignment == 0)
21642 Value.@"null"
21643 else
21644 try Value.Tag.int_u64.create(anon_decl.arena(), alignment);
21645
21646 const decl = try anon_decl.finish(21631 const decl = try anon_decl.finish(
21647 try var_type.copy(anon_decl.arena()),21632 try var_type.copy(anon_decl.arena()),
21648 // There will be stores before the first load, but they may be to sub-elements or21633 // There will be stores before the first load, but they may be to sub-elements or
...@@ -21651,7 +21636,7 @@ fn analyzeComptimeAlloc(...@@ -21651,7 +21636,7 @@ fn analyzeComptimeAlloc(
21651 Value.undef,21636 Value.undef,
21652 alignment,21637 alignment,
21653 );21638 );
21654 decl.align_val = align_val;21639 decl.@"align" = alignment;
2165521640
21656 try sema.mod.declareDeclDependency(sema.owner_decl, decl);21641 try sema.mod.declareDeclDependency(sema.owner_decl, decl);
21657 return sema.addConstant(ptr_type, try Value.Tag.decl_ref_mut.create(sema.arena, .{21642 return sema.addConstant(ptr_type, try Value.Tag.decl_ref_mut.create(sema.arena, .{
...@@ -22066,10 +22051,10 @@ fn unionFieldAlignment(...@@ -22066,10 +22051,10 @@ fn unionFieldAlignment(
22066 src: LazySrcLoc,22051 src: LazySrcLoc,
22067 field: Module.Union.Field,22052 field: Module.Union.Field,
22068) !u32 {22053) !u32 {
22069 if (field.abi_align.tag() == .abi_align_default) {22054 if (field.abi_align == 0) {
22070 return sema.typeAbiAlignment(block, src, field.ty);22055 return sema.typeAbiAlignment(block, src, field.ty);
22071 } else {22056 } else {
22072 return @intCast(u32, field.abi_align.toUnsignedInt());22057 return field.abi_align;
22073 }22058 }
22074}22059}
2207522060
src/arch/x86_64/abi.zig+4-6
...@@ -179,9 +179,8 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {...@@ -179,9 +179,8 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
179 var byte_i: usize = 0; // out of 8179 var byte_i: usize = 0; // out of 8
180 const fields = ty.structFields();180 const fields = ty.structFields();
181 for (fields.values()) |field| {181 for (fields.values()) |field| {
182 if (field.abi_align.tag() != .abi_align_default) {182 if (field.abi_align != 0) {
183 const field_alignment = field.abi_align.toUnsignedInt();183 if (field.abi_align < field.ty.abiAlignment(target)) {
184 if (field_alignment < field.ty.abiAlignment(target)) {
185 return memory_class;184 return memory_class;
186 }185 }
187 }186 }
...@@ -288,9 +287,8 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {...@@ -288,9 +287,8 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class {
288287
289 const fields = ty.unionFields();288 const fields = ty.unionFields();
290 for (fields.values()) |field| {289 for (fields.values()) |field| {
291 if (field.abi_align.tag() != .abi_align_default) {290 if (field.abi_align != 0) {
292 const field_alignment = field.abi_align.toUnsignedInt();291 if (field.abi_align < field.ty.abiAlignment(target)) {
293 if (field_alignment < field.ty.abiAlignment(target)) {
294 return memory_class;292 return memory_class;
295 }293 }
296 }294 }
src/codegen/c.zig+17-31
...@@ -245,7 +245,7 @@ pub const Function = struct {...@@ -245,7 +245,7 @@ pub const Function = struct {
245 ty,245 ty,
246 decl_c_value,246 decl_c_value,
247 .Const,247 .Const,
248 Value.initTag(.abi_align_default),248 0,
249 );249 );
250 try writer.writeAll(" = ");250 try writer.writeAll(" = ");
251 try f.object.dg.renderValue(writer, ty, val);251 try f.object.dg.renderValue(writer, ty, val);
...@@ -267,10 +267,10 @@ pub const Function = struct {...@@ -267,10 +267,10 @@ pub const Function = struct {
267 }267 }
268268
269 fn allocLocal(f: *Function, ty: Type, mutability: Mutability) !CValue {269 fn allocLocal(f: *Function, ty: Type, mutability: Mutability) !CValue {
270 return f.allocAlignedLocal(ty, mutability, Value.initTag(.abi_align_default));270 return f.allocAlignedLocal(ty, mutability, 0);
271 }271 }
272272
273 fn allocAlignedLocal(f: *Function, ty: Type, mutability: Mutability, alignment: Value) !CValue {273 fn allocAlignedLocal(f: *Function, ty: Type, mutability: Mutability, alignment: u32) !CValue {
274 const local_value = f.allocLocalValue();274 const local_value = f.allocLocalValue();
275 try f.object.dg.renderTypeAndName(275 try f.object.dg.renderTypeAndName(
276 f.object.writer(),276 f.object.writer(),
...@@ -854,8 +854,7 @@ pub const DeclGen = struct {...@@ -854,8 +854,7 @@ pub const DeclGen = struct {
854 try w.writeAll(", ");854 try w.writeAll(", ");
855 }855 }
856 const name = CValue{ .arg = index };856 const name = CValue{ .arg = index };
857 const alignment = Value.initTag(.abi_align_default);857 try dg.renderTypeAndName(w, dg.decl.ty.fnParamType(index), name, .Mut, 0);
858 try dg.renderTypeAndName(w, dg.decl.ty.fnParamType(index), name, .Mut, alignment);
859 params_written += 1;858 params_written += 1;
860 }859 }
861860
...@@ -928,8 +927,7 @@ pub const DeclGen = struct {...@@ -928,8 +927,7 @@ pub const DeclGen = struct {
928 var ptr_type_buf: Type.SlicePtrFieldTypeBuffer = undefined;927 var ptr_type_buf: Type.SlicePtrFieldTypeBuffer = undefined;
929 const ptr_type = t.slicePtrFieldType(&ptr_type_buf);928 const ptr_type = t.slicePtrFieldType(&ptr_type_buf);
930 const ptr_name = CValue{ .bytes = "ptr" };929 const ptr_name = CValue{ .bytes = "ptr" };
931 const ptr_alignment = Value.initTag(.abi_align_default);930 try dg.renderTypeAndName(bw, ptr_type, ptr_name, .Mut, 0);
932 try dg.renderTypeAndName(bw, ptr_type, ptr_name, .Mut, ptr_alignment);
933931
934 const ptr_sentinel = ptr_type.ptrInfo().data.sentinel;932 const ptr_sentinel = ptr_type.ptrInfo().data.sentinel;
935 const child_type = t.childType();933 const child_type = t.childType();
...@@ -1018,7 +1016,7 @@ pub const DeclGen = struct {...@@ -1018,7 +1016,7 @@ pub const DeclGen = struct {
1018 try name.writer().print("field_{d}", .{i});1016 try name.writer().print("field_{d}", .{i});
10191017
1020 try buffer.append(' ');1018 try buffer.append(' ');
1021 try dg.renderTypeAndName(writer, field_ty, .{ .bytes = name.items }, .Mut, Value.initTag(.abi_align_default));1019 try dg.renderTypeAndName(writer, field_ty, .{ .bytes = name.items }, .Mut, 0);
1022 try buffer.appendSlice(";\n");1020 try buffer.appendSlice(";\n");
1023 }1021 }
1024 }1022 }
...@@ -1056,7 +1054,7 @@ pub const DeclGen = struct {...@@ -1056,7 +1054,7 @@ pub const DeclGen = struct {
1056 const name: CValue = .{ .bytes = "tag" };1054 const name: CValue = .{ .bytes = "tag" };
1057 try buffer.appendSlice("struct {\n ");1055 try buffer.appendSlice("struct {\n ");
1058 if (layout.tag_size != 0) {1056 if (layout.tag_size != 0) {
1059 try dg.renderTypeAndName(buffer.writer(), tag_ty, name, .Mut, Value.initTag(.abi_align_default));1057 try dg.renderTypeAndName(buffer.writer(), tag_ty, name, .Mut, 0);
1060 try buffer.appendSlice(";\n");1058 try buffer.appendSlice(";\n");
1061 }1059 }
1062 }1060 }
...@@ -1106,8 +1104,7 @@ pub const DeclGen = struct {...@@ -1106,8 +1104,7 @@ pub const DeclGen = struct {
11061104
1107 try bw.writeAll("typedef struct { ");1105 try bw.writeAll("typedef struct { ");
1108 const payload_name = CValue{ .bytes = "payload" };1106 const payload_name = CValue{ .bytes = "payload" };
1109 const alignment = Value.initTag(.abi_align_default);1107 try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, 0);
1110 try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, alignment);
1111 try bw.writeAll("; uint16_t error; } ");1108 try bw.writeAll("; uint16_t error; } ");
1112 const name_index = buffer.items.len;1109 const name_index = buffer.items.len;
1113 if (err_set_type.castTag(.error_set_inferred)) |inf_err_set_payload| {1110 if (err_set_type.castTag(.error_set_inferred)) |inf_err_set_payload| {
...@@ -1172,8 +1169,7 @@ pub const DeclGen = struct {...@@ -1172,8 +1169,7 @@ pub const DeclGen = struct {
11721169
1173 try bw.writeAll("typedef struct { ");1170 try bw.writeAll("typedef struct { ");
1174 const payload_name = CValue{ .bytes = "payload" };1171 const payload_name = CValue{ .bytes = "payload" };
1175 const alignment = Value.initTag(.abi_align_default);1172 try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, 0);
1176 try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, alignment);
1177 try bw.writeAll("; bool is_null; } ");1173 try bw.writeAll("; bool is_null; } ");
1178 const name_index = buffer.items.len;1174 const name_index = buffer.items.len;
1179 try bw.print("zig_Q_{s};\n", .{typeToCIdentifier(child_type)});1175 try bw.print("zig_Q_{s};\n", .{typeToCIdentifier(child_type)});
...@@ -1375,12 +1371,9 @@ pub const DeclGen = struct {...@@ -1375,12 +1371,9 @@ pub const DeclGen = struct {
1375 dg: *DeclGen,1371 dg: *DeclGen,
1376 w: anytype,1372 w: anytype,
1377 ty: Type,1373 ty: Type,
1378 //mutability: Mutability,
1379 //alignment: Value,
1380 ) error{ OutOfMemory, AnalysisFail }!void {1374 ) error{ OutOfMemory, AnalysisFail }!void {
1381 const name = CValue{ .bytes = "" };1375 const name = CValue{ .bytes = "" };
1382 const alignment = Value.initTag(.abi_align_default);1376 return renderTypeAndName(dg, w, ty, name, .Mut, 0);
1383 return renderTypeAndName(dg, w, ty, name, .Mut, alignment);
1384 }1377 }
13851378
1386 /// Renders a type and name in field declaration/definition format.1379 /// Renders a type and name in field declaration/definition format.
...@@ -1398,7 +1391,7 @@ pub const DeclGen = struct {...@@ -1398,7 +1391,7 @@ pub const DeclGen = struct {
1398 ty: Type,1391 ty: Type,
1399 name: CValue,1392 name: CValue,
1400 mutability: Mutability,1393 mutability: Mutability,
1401 alignment: Value,1394 alignment: u32,
1402 ) error{ OutOfMemory, AnalysisFail }!void {1395 ) error{ OutOfMemory, AnalysisFail }!void {
1403 var suffix = std.ArrayList(u8).init(dg.gpa);1396 var suffix = std.ArrayList(u8).init(dg.gpa);
1404 defer suffix.deinit();1397 defer suffix.deinit();
...@@ -1413,8 +1406,8 @@ pub const DeclGen = struct {...@@ -1413,8 +1406,8 @@ pub const DeclGen = struct {
1413 render_ty = render_ty.elemType();1406 render_ty = render_ty.elemType();
1414 }1407 }
14151408
1416 if (alignment.tag() != .abi_align_default and alignment.tag() != .null_value)1409 if (alignment != 0)
1417 try w.print("ZIG_ALIGN({}) ", .{alignment.toUnsignedInt()});1410 try w.print("ZIG_ALIGN({}) ", .{alignment});
1418 try dg.renderType(w, render_ty);1411 try dg.renderType(w, render_ty);
14191412
1420 const const_prefix = switch (mutability) {1413 const const_prefix = switch (mutability) {
...@@ -1570,7 +1563,7 @@ pub fn genDecl(o: *Object) !void {...@@ -1570,7 +1563,7 @@ pub fn genDecl(o: *Object) !void {
15701563
1571 const decl_c_value: CValue = if (is_global) .{ .bytes = mem.span(o.dg.decl.name) } else .{ .decl = o.dg.decl };1564 const decl_c_value: CValue = if (is_global) .{ .bytes = mem.span(o.dg.decl.name) } else .{ .decl = o.dg.decl };
15721565
1573 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.align_val);1566 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");
1574 try fwd_decl_writer.writeAll(";\n");1567 try fwd_decl_writer.writeAll(";\n");
15751568
1576 if (variable.init.isUndefDeep()) {1569 if (variable.init.isUndefDeep()) {
...@@ -1579,7 +1572,7 @@ pub fn genDecl(o: *Object) !void {...@@ -1579,7 +1572,7 @@ pub fn genDecl(o: *Object) !void {
15791572
1580 try o.indent_writer.insertNewline();1573 try o.indent_writer.insertNewline();
1581 const w = o.writer();1574 const w = o.writer();
1582 try o.dg.renderTypeAndName(w, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.align_val);1575 try o.dg.renderTypeAndName(w, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align");
1583 try w.writeAll(" = ");1576 try w.writeAll(" = ");
1584 if (variable.init.tag() != .unreachable_value) {1577 if (variable.init.tag() != .unreachable_value) {
1585 try o.dg.renderValue(w, tv.ty, variable.init);1578 try o.dg.renderValue(w, tv.ty, variable.init);
...@@ -1594,7 +1587,7 @@ pub fn genDecl(o: *Object) !void {...@@ -1594,7 +1587,7 @@ pub fn genDecl(o: *Object) !void {
1594 // https://github.com/ziglang/zig/issues/75821587 // https://github.com/ziglang/zig/issues/7582
15951588
1596 const decl_c_value: CValue = .{ .decl = o.dg.decl };1589 const decl_c_value: CValue = .{ .decl = o.dg.decl };
1597 try o.dg.renderTypeAndName(writer, tv.ty, decl_c_value, .Mut, o.dg.decl.align_val);1590 try o.dg.renderTypeAndName(writer, tv.ty, decl_c_value, .Mut, o.dg.decl.@"align");
15981591
1599 try writer.writeAll(" = ");1592 try writer.writeAll(" = ");
1600 try o.dg.renderValue(writer, tv.ty, tv.val);1593 try o.dg.renderValue(writer, tv.ty, tv.val);
...@@ -1993,15 +1986,8 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {...@@ -1993,15 +1986,8 @@ fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue {
1993 }1986 }
19941987
1995 const target = f.object.dg.module.getTarget();1988 const target = f.object.dg.module.getTarget();
1996 const alignment = inst_ty.ptrAlignment(target);
1997 var payload = Value.Payload.U64{
1998 .base = .{ .tag = .int_u64 },
1999 .data = alignment,
2000 };
2001 const alignment_value = Value.initPayload(&payload.base);
2002
2003 // First line: the variable used as data storage.1989 // First line: the variable used as data storage.
2004 const local = try f.allocAlignedLocal(elem_type, mutability, alignment_value);1990 const local = try f.allocAlignedLocal(elem_type, mutability, inst_ty.ptrAlignment(target));
2005 try writer.writeAll(";\n");1991 try writer.writeAll(";\n");
20061992
2007 return CValue{ .local_ref = local.local };1993 return CValue{ .local_ref = local.local };
src/value.zig-4
...@@ -98,7 +98,6 @@ pub const Value = extern union {...@@ -98,7 +98,6 @@ pub const Value = extern union {
98 bool_false,98 bool_false,
99 generic_poison,99 generic_poison,
100100
101 abi_align_default,
102 empty_struct_value,101 empty_struct_value,
103 empty_array, // See last_no_payload_tag below.102 empty_array, // See last_no_payload_tag below.
104 // After this, the tag requires a payload.103 // After this, the tag requires a payload.
...@@ -241,7 +240,6 @@ pub const Value = extern union {...@@ -241,7 +240,6 @@ pub const Value = extern union {
241 .null_value,240 .null_value,
242 .bool_true,241 .bool_true,
243 .bool_false,242 .bool_false,
244 .abi_align_default,
245 .manyptr_u8_type,243 .manyptr_u8_type,
246 .manyptr_const_u8_type,244 .manyptr_const_u8_type,
247 .manyptr_const_u8_sentinel_0_type,245 .manyptr_const_u8_sentinel_0_type,
...@@ -437,7 +435,6 @@ pub const Value = extern union {...@@ -437,7 +435,6 @@ pub const Value = extern union {
437 .bool_true,435 .bool_true,
438 .bool_false,436 .bool_false,
439 .empty_struct_value,437 .empty_struct_value,
440 .abi_align_default,
441 .manyptr_u8_type,438 .manyptr_u8_type,
442 .manyptr_const_u8_type,439 .manyptr_const_u8_type,
443 .manyptr_const_u8_sentinel_0_type,440 .manyptr_const_u8_sentinel_0_type,
...@@ -677,7 +674,6 @@ pub const Value = extern union {...@@ -677,7 +674,6 @@ pub const Value = extern union {
677 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),674 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
678 .extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),675 .extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),
679 .type_info_type => return out_stream.writeAll("std.builtin.Type"),676 .type_info_type => return out_stream.writeAll("std.builtin.Type"),
680 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),
681677
682 .empty_struct_value => return out_stream.writeAll("struct {}{}"),678 .empty_struct_value => return out_stream.writeAll("struct {}{}"),
683 .aggregate => {679 .aggregate => {