authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-06 00:29:04+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-20 02:29:04+02:00
log95e83afa98c5af89c6e5f40d559eb54c6c31a54e
treef1616f0a6a546a0ff7434b56b6efd1d0a3213c0f
parent5a142dfa5651ec21792de211a6e9341c31ab4dbb

Address Spaces: Yeet address space on function prototypes

This is a property which solely belongs to pointers to functions, not to the functions themselves. This cannot be properly represented by stage 2 at the moment, as type with zigTypeTag() == .Fn is overloaded for for function pointers and function prototypes.

10 files changed, 179 insertions(+), 208 deletions(-)

lib/std/builtin.zig-1
......@@ -365,7 +365,6 @@ pub const TypeInfo = union(enum) {
365365 pub const Fn = struct {
366366 calling_convention: CallingConvention,
367367 alignment: comptime_int,
368 address_space: AddressSpace,
369368 is_generic: bool,
370369 is_var_args: bool,
371370 return_type: ?type,
src/AstGen.zig+4-14
......@@ -1117,9 +1117,9 @@ fn fnProtoExpr(
11171117 break :inst try expr(gz, scope, align_rl, fn_proto.ast.align_expr);
11181118 };
11191119
1120 const addrspace_inst: Zir.Inst.Ref = if (fn_proto.ast.addrspace_expr == 0) .none else inst: {
1121 break :inst try expr(gz, scope, .{ .ty = .address_space_type }, fn_proto.ast.addrspace_expr);
1122 };
1120 if (fn_proto.ast.addrspace_expr != 0) {
1121 return astgen.failNode(fn_proto.ast.addrspace_expr, "addrspace not allowed on function prototypes", .{});
1122 }
11231123
11241124 if (fn_proto.ast.section_expr != 0) {
11251125 return astgen.failNode(fn_proto.ast.section_expr, "linksection not allowed on function prototypes", .{});
......@@ -1153,7 +1153,6 @@ fn fnProtoExpr(
11531153 .body = &[0]Zir.Inst.Index{},
11541154 .cc = cc,
11551155 .align_inst = align_inst,
1156 .addrspace_inst = addrspace_inst,
11571156 .lib_name = 0,
11581157 .is_var_args = is_var_args,
11591158 .is_inferred_error = false,
......@@ -3089,7 +3088,6 @@ fn fnDecl(
30893088 .body = &[0]Zir.Inst.Index{},
30903089 .cc = cc,
30913090 .align_inst = .none, // passed in the per-decl data
3092 .addrspace_inst = .none, // passed in the per-decl data
30933091 .lib_name = lib_name,
30943092 .is_var_args = is_var_args,
30953093 .is_inferred_error = false,
......@@ -3129,7 +3127,6 @@ fn fnDecl(
31293127 .body = fn_gz.instructions.items,
31303128 .cc = cc,
31313129 .align_inst = .none, // passed in the per-decl data
3132 .addrspace_inst = .none, // passed in the per-decl data
31333130 .lib_name = lib_name,
31343131 .is_var_args = is_var_args,
31353132 .is_inferred_error = is_inferred_error,
......@@ -3481,7 +3478,6 @@ fn testDecl(
34813478 .body = fn_block.instructions.items,
34823479 .cc = .none,
34833480 .align_inst = .none,
3484 .addrspace_inst = .none,
34853481 .lib_name = 0,
34863482 .is_var_args = false,
34873483 .is_inferred_error = true,
......@@ -9217,7 +9213,6 @@ const GenZir = struct {
92179213 ret_br: Zir.Inst.Index,
92189214 cc: Zir.Inst.Ref,
92199215 align_inst: Zir.Inst.Ref,
9220 addrspace_inst: Zir.Inst.Ref,
92219216 lib_name: u32,
92229217 is_var_args: bool,
92239218 is_inferred_error: bool,
......@@ -9261,7 +9256,7 @@ const GenZir = struct {
92619256
92629257 if (args.cc != .none or args.lib_name != 0 or
92639258 args.is_var_args or args.is_test or args.align_inst != .none or
9264 args.addrspace_inst != .none or args.is_extern)
9259 args.is_extern)
92659260 {
92669261 try astgen.extra.ensureUnusedCapacity(
92679262 gpa,
......@@ -9269,7 +9264,6 @@ const GenZir = struct {
92699264 args.ret_ty.len + args.body.len + src_locs.len +
92709265 @boolToInt(args.lib_name != 0) +
92719266 @boolToInt(args.align_inst != .none) +
9272 @boolToInt(args.addrspace_inst != .none) +
92739267 @boolToInt(args.cc != .none),
92749268 );
92759269 const payload_index = astgen.addExtraAssumeCapacity(Zir.Inst.ExtendedFunc{
......@@ -9287,9 +9281,6 @@ const GenZir = struct {
92879281 if (args.align_inst != .none) {
92889282 astgen.extra.appendAssumeCapacity(@enumToInt(args.align_inst));
92899283 }
9290 if (args.addrspace_inst != .none) {
9291 astgen.extra.appendAssumeCapacity(@enumToInt(args.addrspace_inst));
9292 }
92939284 astgen.extra.appendSliceAssumeCapacity(args.ret_ty);
92949285 astgen.extra.appendSliceAssumeCapacity(args.body);
92959286 astgen.extra.appendSliceAssumeCapacity(src_locs);
......@@ -9308,7 +9299,6 @@ const GenZir = struct {
93089299 .has_lib_name = args.lib_name != 0,
93099300 .has_cc = args.cc != .none,
93109301 .has_align = args.align_inst != .none,
9311 .has_addrspace = args.addrspace_inst != .none,
93129302 .is_test = args.is_test,
93139303 .is_extern = args.is_extern,
93149304 }),
src/Module.zig+55-59
......@@ -3220,7 +3220,12 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
32203220 };
32213221
32223222 break :blk switch (decl.zirAddrspaceRef()) {
3223 .none => .generic,
3223 .none => switch (addrspace_ctx) {
3224 .function => target_util.defaultAddressSpace(sema.mod.getTarget(), .function),
3225 .variable => target_util.defaultAddressSpace(sema.mod.getTarget(), .global_mutable),
3226 .constant => target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant),
3227 else => unreachable,
3228 },
32243229 else => |addrspace_ref| try sema.analyzeAddrspace(&block_scope, src, addrspace_ref, addrspace_ctx),
32253230 };
32263231 };
......@@ -4359,26 +4364,21 @@ pub fn simplePtrType(
43594364 elem_ty: Type,
43604365 mutable: bool,
43614366 size: std.builtin.TypeInfo.Pointer.Size,
4367 @"addrspace": std.builtin.AddressSpace,
43624368) Allocator.Error!Type {
4363 if (!mutable and size == .Slice and elem_ty.eql(Type.initTag(.u8))) {
4364 return Type.initTag(.const_slice_u8);
4365 }
4366 // TODO stage1 type inference bug
4367 const T = Type.Tag;
4368
4369 const type_payload = try arena.create(Type.Payload.ElemType);
4370 type_payload.* = .{
4371 .base = .{
4372 .tag = switch (size) {
4373 .One => if (mutable) T.single_mut_pointer else T.single_const_pointer,
4374 .Many => if (mutable) T.many_mut_pointer else T.many_const_pointer,
4375 .C => if (mutable) T.c_mut_pointer else T.c_const_pointer,
4376 .Slice => if (mutable) T.mut_slice else T.const_slice,
4377 },
4378 },
4379 .data = elem_ty,
4380 };
4381 return Type.initPayload(&type_payload.base);
4369 return ptrType(
4370 arena,
4371 elem_ty,
4372 null,
4373 0,
4374 @"addrspace",
4375 0,
4376 0,
4377 mutable,
4378 false,
4379 false,
4380 size,
4381 );
43824382}
43834383
43844384pub fn ptrType(
......@@ -4396,47 +4396,43 @@ pub fn ptrType(
43964396) Allocator.Error!Type {
43974397 assert(host_size == 0 or bit_offset < host_size * 8);
43984398
4399 // TODO check if type can be represented by simplePtrType
4400 return Type.Tag.pointer.create(arena, .{
4401 .pointee_type = elem_ty,
4402 .sentinel = sentinel,
4403 .@"align" = @"align",
4404 .@"addrspace" = @"addrspace",
4405 .bit_offset = bit_offset,
4406 .host_size = host_size,
4407 .@"allowzero" = @"allowzero",
4408 .mutable = mutable,
4409 .@"volatile" = @"volatile",
4410 .size = size,
4411 });
4412}
4399 if (sentinel != null or @"align" != 0 or @"addrspace" != .generic or
4400 bit_offset != 0 or host_size != 0 or @"allowzero" or @"volatile")
4401 {
4402 return Type.Tag.pointer.create(arena, .{
4403 .pointee_type = elem_ty,
4404 .sentinel = sentinel,
4405 .@"align" = @"align",
4406 .@"addrspace" = @"addrspace",
4407 .bit_offset = bit_offset,
4408 .host_size = host_size,
4409 .@"allowzero" = @"allowzero",
4410 .mutable = mutable,
4411 .@"volatile" = @"volatile",
4412 .size = size,
4413 });
4414 }
44134415
4414/// Create a pointer type with an explicit address space. This function might return results
4415/// of either simplePtrType or ptrType, depending on the address space.
4416/// TODO(Snektron) unify ptrType functions.
4417pub fn simplePtrTypeWithAddressSpace(
4418 arena: *Allocator,
4419 elem_ty: Type,
4420 mutable: bool,
4421 size: std.builtin.TypeInfo.Pointer.Size,
4422 address_space: std.builtin.AddressSpace,
4423) Allocator.Error!Type {
4424 switch (address_space) {
4425 .generic => return simplePtrType(arena, elem_ty, mutable, size),
4426 else => return ptrType(
4427 arena,
4428 elem_ty,
4429 null,
4430 0,
4431 address_space,
4432 0,
4433 0,
4434 mutable,
4435 false,
4436 false,
4437 size,
4438 ),
4416 if (!mutable and size == .Slice and elem_ty.eql(Type.initTag(.u8))) {
4417 return Type.initTag(.const_slice_u8);
44394418 }
4419
4420 // TODO stage1 type inference bug
4421 const T = Type.Tag;
4422
4423 const type_payload = try arena.create(Type.Payload.ElemType);
4424 type_payload.* = .{
4425 .base = .{
4426 .tag = switch (size) {
4427 .One => if (mutable) T.single_mut_pointer else T.single_const_pointer,
4428 .Many => if (mutable) T.many_mut_pointer else T.many_const_pointer,
4429 .C => if (mutable) T.c_mut_pointer else T.c_const_pointer,
4430 .Slice => if (mutable) T.mut_slice else T.const_slice,
4431 },
4432 },
4433 .data = elem_ty,
4434 };
4435 return Type.initPayload(&type_payload.base);
44404436}
44414437
44424438pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {
src/Sema.zig+81-40
......@@ -1373,7 +1373,13 @@ fn zirRetPtr(
13731373 return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty);
13741374 }
13751375
1376 const ptr_type = try Module.simplePtrType(sema.arena, sema.fn_ret_ty, true, .One);
1376 const ptr_type = try Module.simplePtrType(
1377 sema.arena,
1378 sema.fn_ret_ty,
1379 true,
1380 .One,
1381 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1382 );
13771383 return block.addTy(.alloc, ptr_type);
13781384}
13791385
......@@ -1521,7 +1527,13 @@ fn zirAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileError
15211527 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = inst_data.src_node };
15221528 const var_decl_src = inst_data.src();
15231529 const var_type = try sema.resolveType(block, ty_src, inst_data.operand);
1524 const ptr_type = try Module.simplePtrType(sema.arena, var_type, true, .One);
1530 const ptr_type = try Module.simplePtrType(
1531 sema.arena,
1532 var_type,
1533 true,
1534 .One,
1535 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1536 );
15251537 try sema.requireRuntimeBlock(block, var_decl_src);
15261538 return block.addTy(.alloc, ptr_type);
15271539}
......@@ -1538,7 +1550,13 @@ fn zirAllocMut(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
15381550 return sema.analyzeComptimeAlloc(block, var_type);
15391551 }
15401552 try sema.validateVarType(block, ty_src, var_type);
1541 const ptr_type = try Module.simplePtrType(sema.arena, var_type, true, .One);
1553 const ptr_type = try Module.simplePtrType(
1554 sema.arena,
1555 var_type,
1556 true,
1557 .One,
1558 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1559 );
15421560 try sema.requireRuntimeBlock(block, var_decl_src);
15431561 return block.addTy(.alloc, ptr_type);
15441562}
......@@ -1598,7 +1616,13 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
15981616 try sema.mod.declareDeclDependency(sema.owner_decl, decl);
15991617
16001618 const final_elem_ty = try decl.ty.copy(sema.arena);
1601 const final_ptr_ty = try Module.simplePtrType(sema.arena, final_elem_ty, true, .One);
1619 const final_ptr_ty = try Module.simplePtrType(
1620 sema.arena,
1621 final_elem_ty,
1622 true,
1623 .One,
1624 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1625 );
16021626 const final_ptr_ty_inst = try sema.addType(final_ptr_ty);
16031627 sema.air_instructions.items(.data)[ptr_inst].ty_pl.ty = final_ptr_ty_inst;
16041628
......@@ -1620,7 +1644,13 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Inde
16201644 try sema.validateVarType(block, ty_src, final_elem_ty);
16211645 }
16221646 // Change it to a normal alloc.
1623 const final_ptr_ty = try Module.simplePtrType(sema.arena, final_elem_ty, true, .One);
1647 const final_ptr_ty = try Module.simplePtrType(
1648 sema.arena,
1649 final_elem_ty,
1650 true,
1651 .One,
1652 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1653 );
16241654 sema.air_instructions.set(ptr_inst, .{
16251655 .tag = .alloc,
16261656 .data = .{ .ty = final_ptr_ty },
......@@ -1774,7 +1804,14 @@ fn zirStoreToBlockPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) Co
17741804 }
17751805 const ptr = sema.resolveInst(bin_inst.lhs);
17761806 const value = sema.resolveInst(bin_inst.rhs);
1777 const ptr_ty = try Module.simplePtrType(sema.arena, sema.typeOf(value), true, .One);
1807 const ptr_ty = try Module.simplePtrType(
1808 sema.arena,
1809 sema.typeOf(value),
1810 true,
1811 .One,
1812 // TODO figure out which address space is appropriate here
1813 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1814 );
17781815 // TODO detect when this store should be done at compile-time. For example,
17791816 // if expressions should force it when the condition is compile-time known.
17801817 const src: LazySrcLoc = .unneeded;
......@@ -1821,7 +1858,14 @@ fn zirStoreToInferredPtr(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index)
18211858 // for the inferred allocation.
18221859 try inferred_alloc.data.stored_inst_list.append(sema.arena, operand);
18231860 // Create a runtime bitcast instruction with exactly the type the pointer wants.
1824 const ptr_ty = try Module.simplePtrType(sema.arena, operand_ty, true, .One);
1861 const ptr_ty = try Module.simplePtrType(
1862 sema.arena,
1863 operand_ty,
1864 true,
1865 .One,
1866 // TODO figure out which address space is appropriate here
1867 target_util.defaultAddressSpace(sema.mod.getTarget(), .local),
1868 );
18251869 const bitcasted_ptr = try block.addTyOp(.bitcast, ptr_ty, ptr);
18261870 return sema.storePtr(block, src, bitcasted_ptr, operand);
18271871 }
......@@ -3658,7 +3702,7 @@ fn zirOptionalPayloadPtr(
36583702 }
36593703
36603704 const child_type = try opt_type.optionalChildAlloc(sema.arena);
3661 const child_pointer = try Module.simplePtrTypeWithAddressSpace(
3705 const child_pointer = try Module.simplePtrType(
36623706 sema.arena,
36633707 child_type,
36643708 !optional_ptr_ty.isConstPtr(),
......@@ -3779,7 +3823,7 @@ fn zirErrUnionPayloadPtr(
37793823 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});
37803824
37813825 const payload_ty = operand_ty.elemType().errorUnionPayload();
3782 const operand_pointer_ty = try Module.simplePtrTypeWithAddressSpace(
3826 const operand_pointer_ty = try Module.simplePtrType(
37833827 sema.arena,
37843828 payload_ty,
37853829 !operand_ty.isConstPtr(),
......@@ -3907,7 +3951,6 @@ fn zirFunc(
39073951 ret_ty_body,
39083952 cc,
39093953 Value.initTag(.null_value),
3910 .generic,
39113954 false,
39123955 inferred_error_set,
39133956 false,
......@@ -3924,7 +3967,6 @@ fn funcCommon(
39243967 ret_ty_body: []const Zir.Inst.Index,
39253968 cc: std.builtin.CallingConvention,
39263969 align_val: Value,
3927 address_space: std.builtin.AddressSpace,
39283970 var_args: bool,
39293971 inferred_error_set: bool,
39303972 is_extern: bool,
......@@ -3982,7 +4024,7 @@ fn funcCommon(
39824024 // Hot path for some common function types.
39834025 // TODO can we eliminate some of these Type tag values? seems unnecessarily complicated.
39844026 if (!is_generic and block.params.items.len == 0 and !var_args and
3985 align_val.tag() == .null_value and !inferred_error_set and address_space == .generic)
4027 align_val.tag() == .null_value and !inferred_error_set)
39864028 {
39874029 if (bare_return_type.zigTypeTag() == .NoReturn and cc == .Unspecified) {
39884030 break :fn_ty Type.initTag(.fn_noreturn_no_args);
......@@ -4034,7 +4076,6 @@ fn funcCommon(
40344076 .comptime_params = comptime_params.ptr,
40354077 .return_type = return_type,
40364078 .cc = cc,
4037 .@"addrspace" = address_space,
40384079 .is_var_args = var_args,
40394080 .is_generic = is_generic,
40404081 });
......@@ -6413,7 +6454,7 @@ fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
64136454
64146455 switch (ty.zigTypeTag()) {
64156456 .Fn => {
6416 const field_values = try sema.arena.alloc(Value, 7);
6457 const field_values = try sema.arena.alloc(Value, 6);
64176458 // calling_convention: CallingConvention,
64186459 field_values[0] = try Value.Tag.enum_field_index.create(
64196460 sema.arena,
......@@ -6421,19 +6462,14 @@ fn zirTypeInfo(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) CompileEr
64216462 );
64226463 // alignment: comptime_int,
64236464 field_values[1] = try Value.Tag.int_u64.create(sema.arena, ty.abiAlignment(target));
6424 // address_space: AddressSpace,
6425 field_values[2] = try Value.Tag.enum_field_index.create(
6426 sema.arena,
6427 @enumToInt(ty.fnAddressSpace()),
6428 );
64296465 // is_generic: bool,
6430 field_values[3] = Value.initTag(.bool_false); // TODO
6466 field_values[2] = Value.initTag(.bool_false); // TODO
64316467 // is_var_args: bool,
6432 field_values[4] = Value.initTag(.bool_false); // TODO
6468 field_values[3] = Value.initTag(.bool_false); // TODO
64336469 // return_type: ?type,
6434 field_values[5] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
6470 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
64356471 // args: []const FnArg,
6436 field_values[6] = Value.initTag(.null_value); // TODO
6472 field_values[5] = Value.initTag(.null_value); // TODO
64376473
64386474 return sema.addConstant(
64396475 type_info_ty,
......@@ -8063,7 +8099,6 @@ fn zirFuncExtended(
80638099 const src: LazySrcLoc = .{ .node_offset = extra.data.src_node };
80648100 const cc_src: LazySrcLoc = .{ .node_offset_fn_type_cc = extra.data.src_node };
80658101 const align_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at align
8066 const addrspace_src: LazySrcLoc = src; // TODO(Snektron) add a LazySrcLoc that points at addrspace
80678102 const small = @bitCast(Zir.Inst.ExtendedFunc.Small, extended.small);
80688103
80698104 var extra_index: usize = extra.end;
......@@ -8088,12 +8123,6 @@ fn zirFuncExtended(
80888123 break :blk align_tv.val;
80898124 } else Value.initTag(.null_value);
80908125
8091 const address_space: std.builtin.AddressSpace = if (small.has_addrspace) blk: {
8092 const addrspace_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
8093 extra_index += 1;
8094 break :blk try sema.analyzeAddrspace(block, addrspace_src, addrspace_ref, .function);
8095 } else .generic;
8096
80978126 const ret_ty_body = sema.code.extra[extra_index..][0..extra.data.ret_body_len];
80988127 extra_index += ret_ty_body.len;
80998128
......@@ -8116,7 +8145,6 @@ fn zirFuncExtended(
81168145 ret_ty_body,
81178146 cc,
81188147 align_val,
8119 address_space,
81208148 is_var_args,
81218149 is_inferred_error,
81228150 is_extern,
......@@ -8309,7 +8337,13 @@ fn panicWithMsg(
83098337 const panic_fn = try sema.getBuiltin(block, src, "panic");
83108338 const unresolved_stack_trace_ty = try sema.getBuiltinType(block, src, "StackTrace");
83118339 const stack_trace_ty = try sema.resolveTypeFields(block, src, unresolved_stack_trace_ty);
8312 const ptr_stack_trace_ty = try Module.simplePtrType(arena, stack_trace_ty, true, .One);
8340 const ptr_stack_trace_ty = try Module.simplePtrType(
8341 arena,
8342 stack_trace_ty,
8343 true,
8344 .One,
8345 target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant), // TODO might need a place that is more dynamic
8346 );
83138347 const null_stack_trace = try sema.addConstant(
83148348 try Module.optionalType(arena, ptr_stack_trace_ty),
83158349 Value.initTag(.null_value),
......@@ -8788,7 +8822,7 @@ fn structFieldPtr(
87888822 const field_index = struct_obj.fields.getIndex(field_name) orelse
87898823 return sema.failWithBadFieldAccess(block, struct_obj, field_name_src, field_name);
87908824 const field = struct_obj.fields.values()[field_index];
8791 const ptr_field_ty = try Module.simplePtrTypeWithAddressSpace(
8825 const ptr_field_ty = try Module.simplePtrType(
87928826 arena,
87938827 field.ty,
87948828 struct_ptr_ty.ptrIsMutable(),
......@@ -8893,7 +8927,7 @@ fn unionFieldPtr(
88938927 return sema.failWithBadUnionFieldAccess(block, union_obj, field_name_src, field_name);
88948928
88958929 const field = union_obj.fields.values()[field_index];
8896 const ptr_field_ty = try Module.simplePtrTypeWithAddressSpace(
8930 const ptr_field_ty = try Module.simplePtrType(
88978931 arena,
88988932 field.ty,
88998933 union_ptr_ty.ptrIsMutable(),
......@@ -9075,7 +9109,7 @@ fn elemPtrArray(
90759109) CompileError!Air.Inst.Ref {
90769110 const array_ptr_ty = sema.typeOf(array_ptr);
90779111 const pointee_type = array_ptr_ty.elemType().elemType();
9078 const result_ty = try Module.simplePtrTypeWithAddressSpace(
9112 const result_ty = try Module.simplePtrType(
90799113 sema.arena,
90809114 pointee_type,
90819115 array_ptr_ty.ptrIsMutable(),
......@@ -9581,11 +9615,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
95819615 const decl_tv = try decl.typedValue();
95829616 if (decl_tv.val.castTag(.variable)) |payload| {
95839617 const variable = payload.data;
9584 const ty = try Module.simplePtrTypeWithAddressSpace(sema.arena, decl_tv.ty, variable.is_mutable, .One, decl.@"addrspace");
9618 const ty = try Module.simplePtrType(sema.arena, decl_tv.ty, variable.is_mutable, .One, decl.@"addrspace");
95859619 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));
95869620 }
95879621 return sema.addConstant(
9588 try Module.simplePtrTypeWithAddressSpace(sema.arena, decl_tv.ty, false, .One, decl.@"addrspace"),
9622 try Module.simplePtrType(sema.arena, decl_tv.ty, false, .One, decl.@"addrspace"),
95899623 try Value.Tag.decl_ref.create(sema.arena, decl),
95909624 );
95919625}
......@@ -9608,8 +9642,9 @@ fn analyzeRef(
96089642 }
96099643
96109644 try sema.requireRuntimeBlock(block, src);
9611 const ptr_type = try Module.simplePtrType(sema.arena, operand_ty, false, .One);
9612 const mut_ptr_type = try Module.simplePtrType(sema.arena, operand_ty, true, .One);
9645 const address_space = target_util.defaultAddressSpace(sema.mod.getTarget(), .local);
9646 const ptr_type = try Module.simplePtrType(sema.arena, operand_ty, false, .One, address_space);
9647 const mut_ptr_type = try Module.simplePtrType(sema.arena, operand_ty, true, .One, address_space);
96139648 const alloc = try block.addTy(.alloc, mut_ptr_type);
96149649 try sema.storePtr(block, src, alloc, operand);
96159650
......@@ -10955,7 +10990,13 @@ fn analyzeComptimeAlloc(
1095510990 block: *Scope.Block,
1095610991 var_type: Type,
1095710992) CompileError!Air.Inst.Ref {
10958 const ptr_type = try Module.simplePtrType(sema.arena, var_type, true, .One);
10993 const ptr_type = try Module.simplePtrType(
10994 sema.arena,
10995 var_type,
10996 true,
10997 .One,
10998 target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant),
10999 );
1095911000
1096011001 var anon_decl = try block.startAnonDecl();
1096111002 defer anon_decl.deinit();
src/Zir.zig+1-14
......@@ -2309,7 +2309,6 @@ pub const Inst = struct {
23092309 /// 0. lib_name: u32, // null terminated string index, if has_lib_name is set
23102310 /// 1. cc: Ref, // if has_cc is set
23112311 /// 2. align: Ref, // if has_align is set
2312 /// 3. addrspace: Ref, // if has_addrspace is set
23132312 /// 3. return_type: Index // for each ret_body_len
23142313 /// 4. body: Index // for each body_len
23152314 /// 5. src_locs: Func.SrcLocs // if body_len != 0
......@@ -2327,10 +2326,9 @@ pub const Inst = struct {
23272326 has_lib_name: bool,
23282327 has_cc: bool,
23292328 has_align: bool,
2330 has_addrspace: bool,
23312329 is_test: bool,
23322330 is_extern: bool,
2333 _: u8 = undefined,
2331 _: u9 = undefined,
23342332 };
23352333 };
23362334
......@@ -4483,7 +4481,6 @@ const Writer = struct {
44834481 false,
44844482 .none,
44854483 .none,
4486 .none,
44874484 body,
44884485 src,
44894486 src_locs,
......@@ -4512,11 +4509,6 @@ const Writer = struct {
45124509 extra_index += 1;
45134510 break :blk align_inst;
45144511 };
4515 const addrspace_inst: Inst.Ref = if (!small.has_addrspace) .none else blk: {
4516 const addrspace_inst = @intToEnum(Zir.Inst.Ref, self.code.extra[extra_index]);
4517 extra_index += 1;
4518 break :blk addrspace_inst;
4519 };
45204512
45214513 const ret_ty_body = self.code.extra[extra_index..][0..extra.data.ret_body_len];
45224514 extra_index += ret_ty_body.len;
......@@ -4536,7 +4528,6 @@ const Writer = struct {
45364528 small.is_extern,
45374529 cc,
45384530 align_inst,
4539 addrspace_inst,
45404531 body,
45414532 src,
45424533 src_locs,
......@@ -4619,7 +4610,6 @@ const Writer = struct {
46194610 is_extern: bool,
46204611 cc: Inst.Ref,
46214612 align_inst: Inst.Ref,
4622 addrspace_inst: Inst.Ref,
46234613 body: []const Inst.Index,
46244614 src: LazySrcLoc,
46254615 src_locs: Zir.Inst.Func.SrcLocs,
......@@ -4637,7 +4627,6 @@ const Writer = struct {
46374627
46384628 try self.writeOptionalInstRef(stream, ", cc=", cc);
46394629 try self.writeOptionalInstRef(stream, ", align=", align_inst);
4640 try self.writeOptionalInstRef(stream, ", addrspace=", addrspace_inst);
46414630 try self.writeFlag(stream, ", vargs", var_args);
46424631 try self.writeFlag(stream, ", extern", is_extern);
46434632 try self.writeFlag(stream, ", inferror", inferred_error_set);
......@@ -4915,7 +4904,6 @@ fn findDeclsInner(
49154904 extra_index += @boolToInt(small.has_lib_name);
49164905 extra_index += @boolToInt(small.has_cc);
49174906 extra_index += @boolToInt(small.has_align);
4918 extra_index += @boolToInt(small.has_addrspace);
49194907 const body = zir.extra[extra_index..][0..extra.data.body_len];
49204908 return zir.findDeclsBody(list, body);
49214909 },
......@@ -5119,7 +5107,6 @@ pub fn getFnInfo(zir: Zir, fn_inst: Inst.Index) FnInfo {
51195107 extra_index += @boolToInt(small.has_lib_name);
51205108 extra_index += @boolToInt(small.has_cc);
51215109 extra_index += @boolToInt(small.has_align);
5122 extra_index += @boolToInt(small.has_addrspace);
51235110 const ret_ty_body = zir.extra[extra_index..][0..extra.data.ret_body_len];
51245111 extra_index += ret_ty_body.len;
51255112 const body = zir.extra[extra_index..][0..extra.data.body_len];
src/codegen/llvm.zig+2-1
......@@ -700,7 +700,8 @@ pub const DeclGen = struct {
700700 @intCast(c_uint, llvm_params.len),
701701 llvm.Bool.fromBool(is_var_args),
702702 );
703 const llvm_addrspace = self.llvmAddressSpace(t.fnAddressSpace());
703 // TODO make .Fn not both a pointer type and a prototype
704 const llvm_addrspace = self.llvmAddressSpace(.generic);
704705 return llvm_fn_ty.pointerType(llvm_addrspace);
705706 },
706707 .ComptimeInt => unreachable,
src/stage1/ir.cpp+18-36
......@@ -18483,35 +18483,30 @@ static Error ir_make_type_info_value(IrAnalyze *ira, Scope *scope, AstNode *sour
1848318483 fields[1]->special = ConstValSpecialStatic;
1848418484 fields[1]->type = ira->codegen->builtin_types.entry_num_lit_int;
1848518485 bigint_init_unsigned(&fields[1]->data.x_bigint, get_ptr_align(ira->codegen, type_entry));
18486 // address_space: AddressSpace
18487 ensure_field_index(result->type, "address_space", 2);
18488 fields[2]->special = ConstValSpecialStatic;
18489 fields[2]->type = get_builtin_type(ira->codegen, "AddressSpace");
18490 bigint_init_unsigned(&fields[2]->data.x_enum_tag, AddressSpaceGeneric);
1849118486 // is_generic: bool
18492 ensure_field_index(result->type, "is_generic", 3);
18487 ensure_field_index(result->type, "is_generic", 2);
1849318488 bool is_generic = type_entry->data.fn.is_generic;
18494 fields[3]->special = ConstValSpecialStatic;
18495 fields[3]->type = ira->codegen->builtin_types.entry_bool;
18496 fields[3]->data.x_bool = is_generic;
18489 fields[2]->special = ConstValSpecialStatic;
18490 fields[2]->type = ira->codegen->builtin_types.entry_bool;
18491 fields[2]->data.x_bool = is_generic;
1849718492 // is_varargs: bool
18498 ensure_field_index(result->type, "is_var_args", 4);
18493 ensure_field_index(result->type, "is_var_args", 3);
1849918494 bool is_varargs = type_entry->data.fn.fn_type_id.is_var_args;
18500 fields[4]->special = ConstValSpecialStatic;
18501 fields[4]->type = ira->codegen->builtin_types.entry_bool;
18502 fields[4]->data.x_bool = is_varargs;
18495 fields[3]->special = ConstValSpecialStatic;
18496 fields[3]->type = ira->codegen->builtin_types.entry_bool;
18497 fields[3]->data.x_bool = is_varargs;
1850318498 // return_type: ?type
18504 ensure_field_index(result->type, "return_type", 5);
18505 fields[5]->special = ConstValSpecialStatic;
18506 fields[5]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_type);
18499 ensure_field_index(result->type, "return_type", 4);
18500 fields[4]->special = ConstValSpecialStatic;
18501 fields[4]->type = get_optional_type(ira->codegen, ira->codegen->builtin_types.entry_type);
1850718502 if (type_entry->data.fn.fn_type_id.return_type == nullptr)
18508 fields[5]->data.x_optional = nullptr;
18503 fields[4]->data.x_optional = nullptr;
1850918504 else {
1851018505 ZigValue *return_type = ira->codegen->pass1_arena->create<ZigValue>();
1851118506 return_type->special = ConstValSpecialStatic;
1851218507 return_type->type = ira->codegen->builtin_types.entry_type;
1851318508 return_type->data.x_type = type_entry->data.fn.fn_type_id.return_type;
18514 fields[5]->data.x_optional = return_type;
18509 fields[4]->data.x_optional = return_type;
1851518510 }
1851618511 // args: []TypeInfo.FnArg
1851718512 ZigType *type_info_fn_arg_type = ir_type_info_get_type(ira, "FnArg", nullptr);
......@@ -18526,7 +18521,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, Scope *scope, AstNode *sour
1852618521 fn_arg_array->data.x_array.special = ConstArraySpecialNone;
1852718522 fn_arg_array->data.x_array.data.s_none.elements = ira->codegen->pass1_arena->allocate<ZigValue>(fn_arg_count);
1852818523
18529 init_const_slice(ira->codegen, fields[6], fn_arg_array, 0, fn_arg_count, false, nullptr);
18524 init_const_slice(ira->codegen, fields[5], fn_arg_array, 0, fn_arg_count, false, nullptr);
1853018525
1853118526 for (size_t fn_arg_index = 0; fn_arg_index < fn_arg_count; fn_arg_index++) {
1853218527 FnTypeParamInfo *fn_param_info = &type_entry->data.fn.fn_type_id.param_info[fn_arg_index];
......@@ -19330,22 +19325,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
1933019325 if (alignment == nullptr)
1933119326 return ira->codegen->invalid_inst_gen->value->type;
1933219327
19333 ZigValue *as_value = get_const_field(ira, source_node, payload, "address_space", 2);
19334 if (as_value == nullptr)
19335 return ira->codegen->invalid_inst_gen->value->type;
19336 assert(as_value->special == ConstValSpecialStatic);
19337 assert(as_value->type == get_builtin_type(ira->codegen, "AddressSpace"));
19338 AddressSpace as = (AddressSpace)bigint_as_u32(&as_value->data.x_enum_tag);
19339 if (as != AddressSpaceGeneric) {
19340 ir_add_error_node(ira, source_node, buf_sprintf(
19341 "address space '%s' not available in stage 1 compiler, must be .generic",
19342 address_space_name(as)));
19343 return ira->codegen->invalid_inst_gen->value->type;
19344 }
19345
1934619328 Error err;
1934719329 bool is_generic;
19348 if ((err = get_const_field_bool(ira, source_node, payload, "is_generic", 3, &is_generic)))
19330 if ((err = get_const_field_bool(ira, source_node, payload, "is_generic", 2, &is_generic)))
1934919331 return ira->codegen->invalid_inst_gen->value->type;
1935019332 if (is_generic) {
1935119333 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.Fn.is_generic must be false for @Type"));
......@@ -19353,20 +19335,20 @@ static ZigType *type_info_to_type(IrAnalyze *ira, Scope *scope, AstNode *source_
1935319335 }
1935419336
1935519337 bool is_var_args;
19356 if ((err = get_const_field_bool(ira, source_node, payload, "is_var_args", 4, &is_var_args)))
19338 if ((err = get_const_field_bool(ira, source_node, payload, "is_var_args", 3, &is_var_args)))
1935719339 return ira->codegen->invalid_inst_gen->value->type;
1935819340 if (is_var_args && cc != CallingConventionC) {
1935919341 ir_add_error_node(ira, source_node, buf_sprintf("varargs functions must have C calling convention"));
1936019342 return ira->codegen->invalid_inst_gen->value->type;
1936119343 }
1936219344
19363 ZigType *return_type = get_const_field_meta_type_optional(ira, source_node, payload, "return_type", 5);
19345 ZigType *return_type = get_const_field_meta_type_optional(ira, source_node, payload, "return_type", 4);
1936419346 if (return_type == nullptr) {
1936519347 ir_add_error_node(ira, source_node, buf_sprintf("TypeInfo.Fn.return_type must be non-null for @Type"));
1936619348 return ira->codegen->invalid_inst_gen->value->type;
1936719349 }
1936819350
19369 ZigValue *args_value = get_const_field(ira, source_node, payload, "args", 6);
19351 ZigValue *args_value = get_const_field(ira, source_node, payload, "args", 5);
1937019352 if (args_value == nullptr)
1937119353 return ira->codegen->invalid_inst_gen->value->type;
1937219354 assert(args_value->special == ConstValSpecialStatic);
src/target.zig+18
......@@ -544,3 +544,21 @@ pub fn largestAtomicBits(target: std.Target) u32 {
544544 .x86_64 => 128,
545545 };
546546}
547
548pub fn defaultAddressSpace(
549 target: std.Target,
550 context: enum {
551 /// Query the default address space for global constant values.
552 global_constant,
553 /// Query the default address space for global mutable values.
554 global_mutable,
555 /// Query the default address space for function-local values.
556 local,
557 /// Query the default address space for functions themselves.
558 function,
559 },
560) std.builtin.AddressSpace {
561 _ = target;
562 _ = context;
563 return .generic;
564}
src/type.zig-23
......@@ -530,8 +530,6 @@ pub const Type = extern union {
530530 return false;
531531 if (a.fnCallingConvention() != b.fnCallingConvention())
532532 return false;
533 if (a.fnAddressSpace() != b.fnAddressSpace())
534 return false;
535533 const a_param_len = a.fnParamLen();
536534 const b_param_len = b.fnParamLen();
537535 if (a_param_len != b_param_len)
......@@ -838,7 +836,6 @@ pub const Type = extern union {
838836 .return_type = try payload.return_type.copy(allocator),
839837 .param_types = param_types,
840838 .cc = payload.cc,
841 .@"addrspace" = payload.@"addrspace",
842839 .is_var_args = payload.is_var_args,
843840 .is_generic = payload.is_generic,
844841 .comptime_params = comptime_params.ptr,
......@@ -1001,9 +998,6 @@ pub const Type = extern union {
1001998 try writer.writeAll(") callconv(.");
1002999 try writer.writeAll(@tagName(payload.cc));
10031000 try writer.writeAll(") ");
1004 if (payload.@"addrspace" != .generic) {
1005 try writer.print("addrspace(.{s}) ", .{@tagName(payload.@"addrspace")});
1006 }
10071001 ty = payload.return_type;
10081002 continue;
10091003 },
......@@ -2730,18 +2724,6 @@ pub const Type = extern union {
27302724 };
27312725 }
27322726
2733 pub fn fnAddressSpace(self: Type) std.builtin.AddressSpace {
2734 return switch (self.tag()) {
2735 .fn_noreturn_no_args => .generic,
2736 .fn_void_no_args => .generic,
2737 .fn_naked_noreturn_no_args => .generic,
2738 .fn_ccc_void_no_args => .generic,
2739 .function => self.castTag(.function).?.data.@"addrspace",
2740
2741 else => unreachable,
2742 };
2743 }
2744
27452727 pub fn fnInfo(ty: Type) Payload.Function.Data {
27462728 return switch (ty.tag()) {
27472729 .fn_noreturn_no_args => .{
......@@ -2749,7 +2731,6 @@ pub const Type = extern union {
27492731 .comptime_params = undefined,
27502732 .return_type = initTag(.noreturn),
27512733 .cc = .Unspecified,
2752 .@"addrspace" = .generic,
27532734 .is_var_args = false,
27542735 .is_generic = false,
27552736 },
......@@ -2758,7 +2739,6 @@ pub const Type = extern union {
27582739 .comptime_params = undefined,
27592740 .return_type = initTag(.void),
27602741 .cc = .Unspecified,
2761 .@"addrspace" = .generic,
27622742 .is_var_args = false,
27632743 .is_generic = false,
27642744 },
......@@ -2767,7 +2747,6 @@ pub const Type = extern union {
27672747 .comptime_params = undefined,
27682748 .return_type = initTag(.noreturn),
27692749 .cc = .Naked,
2770 .@"addrspace" = .generic,
27712750 .is_var_args = false,
27722751 .is_generic = false,
27732752 },
......@@ -2776,7 +2755,6 @@ pub const Type = extern union {
27762755 .comptime_params = undefined,
27772756 .return_type = initTag(.void),
27782757 .cc = .C,
2779 .@"addrspace" = .generic,
27802758 .is_var_args = false,
27812759 .is_generic = false,
27822760 },
......@@ -3648,7 +3626,6 @@ pub const Type = extern union {
36483626 comptime_params: [*]bool,
36493627 return_type: Type,
36503628 cc: std.builtin.CallingConvention,
3651 @"addrspace": std.builtin.AddressSpace,
36523629 is_var_args: bool,
36533630 is_generic: bool,
36543631
test/compile_errors.zig-20
......@@ -410,7 +410,6 @@ pub fn addCases(ctx: *TestContext) !void {
410410 \\ .Fn = .{
411411 \\ .calling_convention = .Unspecified,
412412 \\ .alignment = 0,
413 \\ .address_space = .generic,
414413 \\ .is_generic = true,
415414 \\ .is_var_args = false,
416415 \\ .return_type = u0,
......@@ -427,7 +426,6 @@ pub fn addCases(ctx: *TestContext) !void {
427426 \\ .Fn = .{
428427 \\ .calling_convention = .Unspecified,
429428 \\ .alignment = 0,
430 \\ .address_space = .generic,
431429 \\ .is_generic = false,
432430 \\ .is_var_args = true,
433431 \\ .return_type = u0,
......@@ -444,7 +442,6 @@ pub fn addCases(ctx: *TestContext) !void {
444442 \\ .Fn = .{
445443 \\ .calling_convention = .Unspecified,
446444 \\ .alignment = 0,
447 \\ .address_space = .generic,
448445 \\ .is_generic = false,
449446 \\ .is_var_args = false,
450447 \\ .return_type = null,
......@@ -456,23 +453,6 @@ pub fn addCases(ctx: *TestContext) !void {
456453 "tmp.zig:1:20: error: TypeInfo.Fn.return_type must be non-null for @Type",
457454 });
458455
459 ctx.objErrStage1("@Type(.Fn) with invalid address space ",
460 \\const Foo = @Type(.{
461 \\ .Fn = .{
462 \\ .calling_convention = .Unspecified,
463 \\ .alignment = 0,
464 \\ .address_space = .fs,
465 \\ .is_generic = false,
466 \\ .is_var_args = false,
467 \\ .return_type = u0,
468 \\ .args = &[_]@import("std").builtin.TypeInfo.FnArg{},
469 \\ },
470 \\});
471 \\comptime { _ = Foo; }
472 , &[_][]const u8{
473 "tmp.zig:1:20: error: address space 'fs' not available in stage 1 compiler, must be .generic",
474 });
475
476456 ctx.objErrStage1("@Type for union with opaque field",
477457 \\const TypeInfo = @import("std").builtin.TypeInfo;
478458 \\const Untagged = @Type(.{