| ... | ... | @@ -4459,6 +4459,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) Error!void { |
| 4459 | 4459 | |
| 4460 | 4460 | .optional_payload => try cg.airUnwrapOptional(inst), |
| 4461 | 4461 | .optional_payload_ptr => try cg.airUnwrapOptionalPtr(inst), |
| 4462 | .optional_payload_ptr_set => try cg.airSetOptionalPtr(inst), |
| 4462 | 4463 | .wrap_optional => try cg.airWrapOptional(inst), |
| 4463 | 4464 | |
| 4464 | 4465 | .assembly => try cg.airAssembly(inst), |
| ... | ... | @@ -8017,6 +8018,39 @@ fn airUnwrapOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 8017 | 8018 | return try cg.accessChain(result_ty_id, operand_id, &.{0}); |
| 8018 | 8019 | } |
| 8019 | 8020 | |
| 8021 | fn airSetOptionalPtr(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 8022 | const zcu = cg.zcu; |
| 8023 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 8024 | |
| 8025 | const ptr_ty = cg.typeOf(ty_op.operand); |
| 8026 | const ptr_id = try cg.resolve(ty_op.operand); |
| 8027 | |
| 8028 | const optional_ty = ptr_ty.childType(zcu); |
| 8029 | const payload_ty = optional_ty.optionalChild(zcu); |
| 8030 | const result_ty = cg.typeOfIndex(inst); |
| 8031 | |
| 8032 | if (optional_ty.optionalReprIsPayload(zcu)) { |
| 8033 | return try cg.bitCast(result_ty, ptr_ty, ptr_id); |
| 8034 | } |
| 8035 | |
| 8036 | const storage_class = cg.storageClass(ptr_ty.ptrAddressSpace(zcu)); |
| 8037 | const bool_indirect_ty_id = try cg.resolveType(.bool, .indirect); |
| 8038 | const bool_ptr_ty_id = try cg.ptrType(bool_indirect_ty_id, storage_class); |
| 8039 | const result_ty_id = try cg.resolveType(result_ty, .direct); |
| 8040 | |
| 8041 | const bool_ptr_id, const ret = switch (payload_ty.hasRuntimeBits(zcu)) { |
| 8042 | true => .{ |
| 8043 | try cg.accessChain(bool_ptr_ty_id, ptr_id, &.{1}), |
| 8044 | try cg.accessChain(result_ty_id, ptr_id, &.{0}), |
| 8045 | }, |
| 8046 | false => .{ ptr_id, try cg.bitCast(result_ty, ptr_ty, ptr_id) }, |
| 8047 | }; |
| 8048 | |
| 8049 | try cg.store(.bool, bool_ptr_id, try cg.constBool(true, .direct), .{}); |
| 8050 | |
| 8051 | return ret; |
| 8052 | } |
| 8053 | |
| 8020 | 8054 | fn airWrapOptional(cg: *CodeGen, inst: Air.Inst.Index) !?Id { |
| 8021 | 8055 | const zcu = cg.zcu; |
| 8022 | 8056 | const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |