| ... | ... | @@ -1974,8 +1974,6 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 1974 | 1974 | defer trash_block.instructions.deinit(sema.gpa); |
| 1975 | 1975 | const operand = try trash_block.addBitCast(pointee_ty, .void_value); |
| 1976 | 1976 | |
| 1977 | | try inferred_alloc.stored_inst_list.append(sema.arena, operand); |
| 1978 | | |
| 1979 | 1977 | try sema.requireRuntimeBlock(block, src); |
| 1980 | 1978 | const ptr_ty = try Type.ptr(sema.arena, sema.mod, .{ |
| 1981 | 1979 | .pointee_type = pointee_ty, |
| ... | ... | @@ -1983,6 +1981,12 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 1983 | 1981 | .@"addrspace" = addr_space, |
| 1984 | 1982 | }); |
| 1985 | 1983 | const bitcasted_ptr = try block.addBitCast(ptr_ty, ptr); |
| 1984 | |
| 1985 | try inferred_alloc.prongs.append(sema.arena, .{ |
| 1986 | .stored_inst = operand, |
| 1987 | .placeholder = Air.refToIndex(bitcasted_ptr).?, |
| 1988 | }); |
| 1989 | |
| 1986 | 1990 | return bitcasted_ptr; |
| 1987 | 1991 | }, |
| 1988 | 1992 | .inferred_alloc_comptime => { |
| ... | ... | @@ -2026,8 +2030,23 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 2026 | 2030 | defer trash_block.instructions.deinit(sema.gpa); |
| 2027 | 2031 | |
| 2028 | 2032 | const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr)); |
| 2033 | return coerceResultPtr(sema, block, src, ptr, dummy_ptr, pointee_ty, &trash_block); |
| 2034 | } |
| 2035 | |
| 2036 | fn coerceResultPtr( |
| 2037 | sema: *Sema, |
| 2038 | block: *Block, |
| 2039 | src: LazySrcLoc, |
| 2040 | ptr: Air.Inst.Ref, |
| 2041 | dummy_ptr: Air.Inst.Ref, |
| 2042 | pointee_ty: Type, |
| 2043 | trash_block: *Block, |
| 2044 | ) CompileError!Air.Inst.Ref { |
| 2045 | const target = sema.mod.getTarget(); |
| 2046 | const addr_space = target_util.defaultAddressSpace(target, .local); |
| 2047 | |
| 2029 | 2048 | const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value); |
| 2030 | | try sema.storePtr2(&trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast); |
| 2049 | try sema.storePtr2(trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast); |
| 2031 | 2050 | |
| 2032 | 2051 | { |
| 2033 | 2052 | const air_tags = sema.air_instructions.items(.tag); |
| ... | ... | @@ -2066,6 +2085,12 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 2066 | 2085 | if (try sema.resolveDefinedValue(block, src, new_ptr)) |ptr_val| { |
| 2067 | 2086 | return sema.addConstant(ptr_ty, ptr_val); |
| 2068 | 2087 | } |
| 2088 | if (pointee_ty.eql(Type.@"null", sema.mod)) { |
| 2089 | const opt_ty = sema.typeOf(new_ptr).childType(); |
| 2090 | const null_inst = try sema.addConstant(opt_ty, Value.@"null"); |
| 2091 | _ = try block.addBinOp(.store, new_ptr, null_inst); |
| 2092 | return Air.Inst.Ref.void_value; |
| 2093 | } |
| 2069 | 2094 | return sema.bitCast(block, ptr_ty, new_ptr, src); |
| 2070 | 2095 | } |
| 2071 | 2096 | const ty_op = air_datas[trash_inst].ty_op; |
| ... | ... | @@ -3141,7 +3166,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 3141 | 3166 | }, |
| 3142 | 3167 | .inferred_alloc => { |
| 3143 | 3168 | const inferred_alloc = ptr_val.castTag(.inferred_alloc).?; |
| 3144 | | const peer_inst_list = inferred_alloc.data.stored_inst_list.items; |
| 3169 | const peer_inst_list = inferred_alloc.data.prongs.items(.stored_inst); |
| 3145 | 3170 | const final_elem_ty = try sema.resolvePeerTypes(block, ty_src, peer_inst_list, .none); |
| 3146 | 3171 | |
| 3147 | 3172 | const final_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{ |
| ... | ... | @@ -3250,6 +3275,71 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 3250 | 3275 | .tag = .alloc, |
| 3251 | 3276 | .data = .{ .ty = final_ptr_ty }, |
| 3252 | 3277 | }); |
| 3278 | |
| 3279 | // Now we need to go back over all the coerce_result_ptr instructions, which |
| 3280 | // previously inserted a bitcast as a placeholder, and do the logic as if |
| 3281 | // the new result ptr type was available. |
| 3282 | const placeholders = inferred_alloc.data.prongs.items(.placeholder); |
| 3283 | const gpa = sema.gpa; |
| 3284 | |
| 3285 | var trash_block = block.makeSubBlock(); |
| 3286 | trash_block.is_comptime = false; |
| 3287 | trash_block.is_coerce_result_ptr = true; |
| 3288 | defer trash_block.instructions.deinit(gpa); |
| 3289 | |
| 3290 | const mut_final_ptr_ty = try Type.ptr(sema.arena, sema.mod, .{ |
| 3291 | .pointee_type = final_elem_ty, |
| 3292 | .mutable = true, |
| 3293 | .@"align" = inferred_alloc.data.alignment, |
| 3294 | .@"addrspace" = target_util.defaultAddressSpace(target, .local), |
| 3295 | }); |
| 3296 | const dummy_ptr = try trash_block.addTy(.alloc, mut_final_ptr_ty); |
| 3297 | const empty_trash_count = trash_block.instructions.items.len; |
| 3298 | |
| 3299 | for (placeholders) |bitcast_inst, i| { |
| 3300 | const sub_ptr_ty = sema.typeOf(Air.indexToRef(bitcast_inst)); |
| 3301 | |
| 3302 | if (mut_final_ptr_ty.eql(sub_ptr_ty, sema.mod)) { |
| 3303 | // New result location type is the same as the old one; nothing |
| 3304 | // to do here. |
| 3305 | continue; |
| 3306 | } |
| 3307 | |
| 3308 | var bitcast_block = block.makeSubBlock(); |
| 3309 | defer bitcast_block.instructions.deinit(gpa); |
| 3310 | |
| 3311 | trash_block.instructions.shrinkRetainingCapacity(empty_trash_count); |
| 3312 | const pointee_ty = sema.typeOf(peer_inst_list[i]); |
| 3313 | const sub_ptr = try coerceResultPtr(sema, &bitcast_block, src, ptr, dummy_ptr, pointee_ty, &trash_block); |
| 3314 | |
| 3315 | assert(bitcast_block.instructions.items.len > 0); |
| 3316 | // If only one instruction is produced then we can replace the bitcast |
| 3317 | // placeholder instruction with this instruction; no need for an entire block. |
| 3318 | if (bitcast_block.instructions.items.len == 1) { |
| 3319 | const only_inst = bitcast_block.instructions.items[0]; |
| 3320 | sema.air_instructions.set(bitcast_inst, sema.air_instructions.get(only_inst)); |
| 3321 | continue; |
| 3322 | } |
| 3323 | |
| 3324 | // Here we replace the placeholder bitcast instruction with a block |
| 3325 | // that does the coerce_result_ptr logic. |
| 3326 | _ = try bitcast_block.addBr(bitcast_inst, sub_ptr); |
| 3327 | const ty_inst = sema.air_instructions.items(.data)[bitcast_inst].ty_op.ty; |
| 3328 | try sema.air_extra.ensureUnusedCapacity( |
| 3329 | gpa, |
| 3330 | @typeInfo(Air.Block).Struct.fields.len + bitcast_block.instructions.items.len, |
| 3331 | ); |
| 3332 | sema.air_instructions.set(bitcast_inst, .{ |
| 3333 | .tag = .block, |
| 3334 | .data = .{ .ty_pl = .{ |
| 3335 | .ty = ty_inst, |
| 3336 | .payload = sema.addExtraAssumeCapacity(Air.Block{ |
| 3337 | .body_len = @intCast(u32, bitcast_block.instructions.items.len), |
| 3338 | }), |
| 3339 | } }, |
| 3340 | }); |
| 3341 | sema.air_extra.appendSliceAssumeCapacity(bitcast_block.instructions.items); |
| 3342 | } |
| 3253 | 3343 | }, |
| 3254 | 3344 | else => unreachable, |
| 3255 | 3345 | } |
| ... | ... | @@ -4086,9 +4176,6 @@ fn storeToInferredAlloc( |
| 4086 | 4176 | inferred_alloc: *Value.Payload.InferredAlloc, |
| 4087 | 4177 | ) CompileError!void { |
| 4088 | 4178 | const operand_ty = sema.typeOf(operand); |
| 4089 | | // Add the stored instruction to the set we will use to resolve peer types |
| 4090 | | // for the inferred allocation. |
| 4091 | | try inferred_alloc.data.stored_inst_list.append(sema.arena, operand); |
| 4092 | 4179 | // Create a runtime bitcast instruction with exactly the type the pointer wants. |
| 4093 | 4180 | const target = sema.mod.getTarget(); |
| 4094 | 4181 | const ptr_ty = try Type.ptr(sema.arena, sema.mod, .{ |
| ... | ... | @@ -4097,6 +4184,12 @@ fn storeToInferredAlloc( |
| 4097 | 4184 | .@"addrspace" = target_util.defaultAddressSpace(target, .local), |
| 4098 | 4185 | }); |
| 4099 | 4186 | const bitcasted_ptr = try block.addBitCast(ptr_ty, ptr); |
| 4187 | // Add the stored instruction to the set we will use to resolve peer types |
| 4188 | // for the inferred allocation. |
| 4189 | try inferred_alloc.data.prongs.append(sema.arena, .{ |
| 4190 | .stored_inst = operand, |
| 4191 | .placeholder = Air.refToIndex(bitcasted_ptr).?, |
| 4192 | }); |
| 4100 | 4193 | return sema.storePtr(block, src, bitcasted_ptr, operand); |
| 4101 | 4194 | } |
| 4102 | 4195 | |