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 {...@@ -353,7 +353,7 @@ pub const Decl = struct {
353 /// to require re-analysis.353 /// to require re-analysis.
354 outdated,354 outdated,
355 },355 },
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.
357 has_tv: bool,357 has_tv: bool,
358 /// If `true` it means the `Decl` is the resource owner of the type/value associated358 /// If `true` it means the `Decl` is the resource owner of the type/value associated
359 /// with it. That means when `Decl` is destroyed, the cleanup code should additionally359 /// with it. That means when `Decl` is destroyed, the cleanup code should additionally
...@@ -4401,6 +4401,34 @@ pub fn ptrType(...@@ -4401,6 +4401,34 @@ pub fn ptrType(
4401 });4401 });
4402}4402}
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
4404pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {4432pub fn optionalType(arena: *Allocator, child_type: Type) Allocator.Error!Type {
4405 switch (child_type.tag()) {4433 switch (child_type.tag()) {
4406 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(4434 .single_const_pointer => return Type.Tag.optional_single_const_pointer.create(
src/Sema.zig+16-4
...@@ -3658,7 +3658,13 @@ fn zirOptionalPayloadPtr(...@@ -3658,7 +3658,13 @@ fn zirOptionalPayloadPtr(
3658 }3658 }
36593659
3660 const child_type = try opt_type.optionalChildAlloc(sema.arena);3660 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
3663 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {3669 if (try sema.resolveDefinedValue(block, src, optional_ptr)) |pointer_val| {
3664 if (try pointer_val.pointerDeref(sema.arena)) |val| {3670 if (try pointer_val.pointerDeref(sema.arena)) |val| {
...@@ -3773,7 +3779,13 @@ fn zirErrUnionPayloadPtr(...@@ -3773,7 +3779,13 @@ fn zirErrUnionPayloadPtr(
3773 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});3779 return sema.mod.fail(&block.base, src, "expected error union type, found {}", .{operand_ty.elemType()});
37743780
3775 const payload_ty = operand_ty.elemType().errorUnionPayload();3781 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
3778 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {3790 if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| {
3779 if (try pointer_val.pointerDeref(sema.arena)) |val| {3791 if (try pointer_val.pointerDeref(sema.arena)) |val| {
...@@ -9525,11 +9537,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {...@@ -9525,11 +9537,11 @@ fn analyzeDeclRef(sema: *Sema, decl: *Decl) CompileError!Air.Inst.Ref {
9525 const decl_tv = try decl.typedValue();9537 const decl_tv = try decl.typedValue();
9526 if (decl_tv.val.castTag(.variable)) |payload| {9538 if (decl_tv.val.castTag(.variable)) |payload| {
9527 const variable = payload.data;9539 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");
9529 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));9541 return sema.addConstant(ty, try Value.Tag.decl_ref.create(sema.arena, decl));
9530 }9542 }
9531 return sema.addConstant(9543 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"),
9533 try Value.Tag.decl_ref.create(sema.arena, decl),9545 try Value.Tag.decl_ref.create(sema.arena, decl),
9534 );9546 );
9535}9547}
src/type.zig+24
...@@ -1526,6 +1526,30 @@ pub const Type = extern union {...@@ -1526,6 +1526,30 @@ pub const Type = extern union {
1526 }1526 }
1527 }1527 }
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
1529 /// Asserts that hasCodeGenBits() is true.1553 /// Asserts that hasCodeGenBits() is true.
1530 pub fn abiAlignment(self: Type, target: Target) u32 {1554 pub fn abiAlignment(self: Type, target: Target) u32 {
1531 return switch (self.tag()) {1555 return switch (self.tag()) {