authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-08-20 02:53:29+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-09-20 02:29:03+02:00
logcd9f6001af407a6961281cbe9c658cfe94b81ecb
tree348a4612ec8bcc0b89368955932615cecc6f4634
parent60231086508d26689d53b8bc545e8fe98cad966d

Address Spaces: decl_ref, *?T => *T, and *(E!T) -> *T


3 files changed, 69 insertions(+), 5 deletions(-)

src/Module.zig+29-1
......@@ -353,7 +353,7 @@ pub const Decl = struct {
353353 /// to require re-analysis.
354354 outdated,
355355 },
356 /// Whether `typed_value`, `align_val`, `linksection_val` and `has_addrspace` are populated.
356 /// Whether `typed_value`, `align_val`, `linksection_val` and `addrspace` are populated.
357357 has_tv: bool,
358358 /// If `true` it means the `Decl` is the resource owner of the type/value associated
359359 /// with it. That means when `Decl` is destroyed, the cleanup code should additionally
......@@ -4401,6 +4401,34 @@ pub fn ptrType(
44014401 });
44024402}
44034403
4404/// Create a pointer type with an explicit address space. This function might return results
4405/// of either simplePtrType or ptrType, depending on the address space.
4406/// TODO(Snektron) unify ptrType functions.
4407pub fn simplePtrTypeWithAddressSpace(
4408 arena: *Allocator,
4409 elem_ty: Type,
4410 mutable: bool,
4411 size: std.builtin.TypeInfo.Pointer.Size,
4412 address_space: std.builtin.AddressSpace,
4413) Allocator.Error!Type {
4414 switch (address_space) {
4415 .generic => return simplePtrType(arena, elem_ty, mutable, size),
4416 else => return ptrType(
4417 arena,
4418 elem_ty,
4419 null,
4420 0,
4421 address_space,
4422 0,
4423 0,
4424 mutable,
4425 false,
4426 false,
4427 size,
4428 ),
4429 }
4430}
4431
44044432pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {
44054433 switch (child_type.tag()) {
44064434 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(
src/Sema.zig+16-4
......@@ -3658,7 +3658,13 @@ fn zirOptionalPayloadPtr(
36583658 }
36593659
36603660 const child_type = try opt_type.optionalChildAlloc(sema.arena);
3661 const child_pointer = try Module.simplePtrType(sema.arena, child_type, !optional_ptr_ty.isConstPtr(), .One);
3661 const child_pointer = try Module.simplePtrTypeWithAddressSpace(
3662 sema.arena,
3663 child_type,
3664 !optional_ptr_ty.isConstPtr(),
3665 .One,
3666 optional_ptr_ty.ptrAddressSpace(),
3667 );
36623668
36633669 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {
36643670 if (try pointer_val.pointerDeref(sema.arena)) |val| {
......@@ -3773,7 +3779,13 @@ fn zirErrUnionPayloadPtr(
37733779 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});
37743780
37753781 const payload_ty = operand_ty.elemType().errorUnionPayload();
3776 const operand_pointer_ty = try Module.simplePtrType(sema.arena, payload_ty, !operand_ty.isConstPtr(), .One);
3782 const operand_pointer_ty = try Module.simplePtrTypeWithAddressSpace(
3783 sema.arena,
3784 payload_ty,
3785 !operand_ty.isConstPtr(),
3786 .One,
3787 operand_ty.ptrAddressSpace(),
3788 );
37773789
37783790 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
37793791 if (try pointer_val.pointerDeref(sema.arena)) |val| {
......@@ -9525,11 +9537,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
95259537 const decl_tv = try decl.typedValue();
95269538 if (decl_tv.val.castTag(.variable)) |payload| {
95279539 const variable = payload.data;
9528 const ty = try Module.simplePtrType(sema.arena, decl_tv.ty, variable.is_mutable, .One);
9540 const ty = try Module.simplePtrTypeWithAddressSpace(sema.arena, decl_tv.ty, variable.is_mutable, .One, decl.@"addrspace");
95299541 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));
95309542 }
95319543 return sema.addConstant(
9532 try Module.simplePtrType(sema.arena, decl_tv.ty, false, .One),
9544 try Module.simplePtrTypeWithAddressSpace(sema.arena, decl_tv.ty, false, .One, decl.@"addrspace"),
95339545 try Value.Tag.decl_ref.create(sema.arena, decl),
95349546 );
95359547}
src/type.zig+24
......@@ -1526,6 +1526,30 @@ pub const Type = extern union {
15261526 }
15271527 }
15281528
1529 pub fn ptrAddressSpace(self: Type) std.builtin.AddressSpace {
1530 return switch (self.tag()) {
1531 .single_const_pointer_to_comptime_int,
1532 .const_slice_u8,
1533 .single_const_pointer,
1534 .single_mut_pointer,
1535 .many_const_pointer,
1536 .many_mut_pointer,
1537 .c_const_pointer,
1538 .c_mut_pointer,
1539 .const_slice,
1540 .mut_slice,
1541 .inferred_alloc_const,
1542 .inferred_alloc_mut,
1543 .manyptr_u8,
1544 .manyptr_const_u8,
1545 => .generic,
1546
1547 .pointer => self.castTag(.pointer).?.data.@"addrspace",
1548
1549 else => unreachable,
1550 };
1551 }
1552
15291553 /// Asserts that hasCodeGenBits() is true.
15301554 pub fn abiAlignment(self: Type, target: Target) u32 {
15311555 return switch (self.tag()) {