/// Specialized emitter for Builtin.reinterpretCast. static ManagedValue emitBuiltinReinterpretCast(SILGenFunction &SGF, SILLocation loc, SubstitutionMap substitutions, ArrayRef<ManagedValue> args, SGFContext C) { assert(args.size() == 1 && "reinterpretCast should be given one argument"); assert(substitutions.getReplacementTypes().size() == 2 && "reinterpretCast should have two subs"); auto &fromTL = SGF.getTypeLowering(substitutions.getReplacementTypes()[0]); auto &toTL = SGF.getTypeLowering(substitutions.getReplacementTypes()[1]); // If casting between address types, cast the address. if (fromTL.isAddress() || toTL.isAddress()) { SILValue fromAddr; // If the from value is not an address, move it to a buffer. if (!fromTL.isAddress()) { fromAddr = SGF.emitTemporaryAllocation(loc, args[0].getValue()->getType()); fromTL.emitStore(SGF.B, loc, args[0].getValue(), fromAddr, StoreOwnershipQualifier::Init); } else { fromAddr = args[0].getValue(); } auto toAddr = SGF.B.createUncheckedAddrCast(loc, fromAddr, toTL.getLoweredType().getAddressType()); // Load and retain the destination value if it's loadable. Leave the cleanup // on the original value since we don't know anything about it's type. if (!toTL.isAddress()) { return SGF.emitManagedLoadCopy(loc, toAddr, toTL); } // Leave the cleanup on the original value. if (toTL.isTrivial()) return ManagedValue::forUnmanaged(toAddr); // Initialize the +1 result buffer without taking the incoming value. The // source and destination cleanups will be independent. return SGF.B.bufferForExpr( loc, toTL.getLoweredType(), toTL, C, [&](SILValue bufferAddr) { SGF.B.createCopyAddr(loc, toAddr, bufferAddr, IsNotTake, IsInitialization); }); } // Create the appropriate bitcast based on the source and dest types. ManagedValue in = args[0]; SILType resultTy = toTL.getLoweredType(); if (resultTy.isTrivial(SGF.F)) return SGF.B.createUncheckedTrivialBitCast(loc, in, resultTy); // If we can perform a ref cast, just return. if (auto refCast = SGF.B.tryCreateUncheckedRefCast(loc, in, resultTy)) return refCast; // Otherwise leave the original cleanup and retain the cast value. SILValue out = SGF.B.createUncheckedBitwiseCast(loc, in.getValue(), resultTy); return SGF.emitManagedRetain(loc, out, toTL); }
/// Specialized emitter for Builtin.reinterpretCast. static ManagedValue emitBuiltinReinterpretCast(SILGenFunction &gen, SILLocation loc, SubstitutionList substitutions, ArrayRef<ManagedValue> args, CanFunctionType formalApplyType, SGFContext C) { assert(args.size() == 1 && "reinterpretCast should be given one argument"); assert(substitutions.size() == 2 && "reinterpretCast should have two subs"); auto &fromTL = gen.getTypeLowering(substitutions[0].getReplacement()); auto &toTL = gen.getTypeLowering(substitutions[1].getReplacement()); // If casting between address-only types, cast the address. if (!fromTL.isLoadable() || !toTL.isLoadable()) { SILValue fromAddr; // If the from value is loadable, move it to a buffer. if (fromTL.isLoadable()) { fromAddr = gen.emitTemporaryAllocation(loc, args[0].getValue()->getType()); fromTL.emitStore(gen.B, loc, args[0].getValue(), fromAddr, StoreOwnershipQualifier::Init); } else { fromAddr = args[0].getValue(); } auto toAddr = gen.B.createUncheckedAddrCast(loc, fromAddr, toTL.getLoweredType().getAddressType()); // Load and retain the destination value if it's loadable. Leave the cleanup // on the original value since we don't know anything about it's type. if (toTL.isLoadable()) { return gen.emitManagedLoadCopy(loc, toAddr, toTL); } // Leave the cleanup on the original value. if (toTL.isTrivial()) return ManagedValue::forUnmanaged(toAddr); // Initialize the +1 result buffer without taking the incoming value. The // source and destination cleanups will be independent. return gen.B.bufferForExpr( loc, toTL.getLoweredType(), toTL, C, [&](SILValue bufferAddr) { gen.B.createCopyAddr(loc, toAddr, bufferAddr, IsNotTake, IsInitialization); }); } // Create the appropriate bitcast based on the source and dest types. auto &in = args[0]; SILValue out = gen.B.createUncheckedBitCast(loc, in.getValue(), toTL.getLoweredType()); // If the cast reduces to unchecked_ref_cast, then the source and dest // have identical cleanup, so just forward the cleanup as an optimization. if (isa<UncheckedRefCastInst>(out)) return ManagedValue(out, in.getCleanup()); // Otherwise leave the original cleanup and retain the cast value. return gen.emitManagedRetain(loc, out, toTL); }