diff --git a/IDEHelper/Compiler/BfIRBuilder.cpp b/IDEHelper/Compiler/BfIRBuilder.cpp index b524fdd9..8e1ace33 100644 --- a/IDEHelper/Compiler/BfIRBuilder.cpp +++ b/IDEHelper/Compiler/BfIRBuilder.cpp @@ -857,6 +857,26 @@ BfIRValue BfIRConstHolder::CreateConstArrayZero(int count) return irValue; } +BfIRValue BfIRConstHolder::CreateConstBitCast(BfIRValue val, BfIRType type) +{ + auto constVal = GetConstant(val); + + auto bitCast = mTempAlloc.Alloc(); + if ((constVal == NULL) || (constVal->IsNull())) + bitCast->mConstType = BfConstType_BitCastNull; + else + bitCast->mConstType = BfConstType_BitCast; + bitCast->mTarget = val.mId; + bitCast->mToType = type; + + BfIRValue castedVal(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(bitCast)); +#ifdef CHECK_CONSTHOLDER + castedVal.mHolder = this; +#endif + BF_ASSERT((void*)GetConstant(castedVal) == (void*)bitCast); + return castedVal; +} + BfIRValue BfIRConstHolder::CreateTypeOf(BfType* type) { BfTypeOf_Const* typeOf = mTempAlloc.Alloc(); @@ -4414,25 +4434,7 @@ BfIRValue BfIRBuilder::CreateNot(BfIRValue val) BfIRValue BfIRBuilder::CreateBitCast(BfIRValue val, BfIRType type) { if (val.IsConst()) - { - auto constVal = GetConstant(val); - - auto bitCast = mTempAlloc.Alloc(); - if (constVal->IsNull()) - bitCast->mConstType = BfConstType_BitCastNull; - else - bitCast->mConstType = BfConstType_BitCast; - bitCast->mTarget = val.mId; - bitCast->mToType = type; - - BfIRValue castedVal(BfIRValueFlags_Const, mTempAlloc.GetChunkedId(bitCast)); -#ifdef CHECK_CONSTHOLDER - castedVal.mHolder = this; -#endif - BF_ASSERT((void*)GetConstant(castedVal) == (void*)bitCast); - return castedVal; - } - + return CreateConstBitCast(val, type); auto retVal = WriteCmd(BfIRCmd_BitCast, val, type); NEW_CMD_INSERTED_IRVALUE; return retVal; diff --git a/IDEHelper/Compiler/BfIRBuilder.h b/IDEHelper/Compiler/BfIRBuilder.h index 3d045deb..64c0f55d 100644 --- a/IDEHelper/Compiler/BfIRBuilder.h +++ b/IDEHelper/Compiler/BfIRBuilder.h @@ -934,6 +934,7 @@ public: BfIRValue CreateConstAggCE(BfIRType type, addr_ce ptr); BfIRValue CreateConstArrayZero(BfIRType type, int count); BfIRValue CreateConstArrayZero(int count); + BfIRValue CreateConstBitCast(BfIRValue val, BfIRType type); BfIRValue CreateTypeOf(BfType* type); BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData); BfIRValue GetUndefConstValue(BfIRType type); diff --git a/IDEHelper/Compiler/BfModule.cpp b/IDEHelper/Compiler/BfModule.cpp index da894da9..ba13dae6 100644 --- a/IDEHelper/Compiler/BfModule.cpp +++ b/IDEHelper/Compiler/BfModule.cpp @@ -10873,8 +10873,15 @@ void BfModule::CurrentAddToConstHolder(BfIRValue& irVal) auto origConst = irVal; if ((constant->mConstType == BfConstType_BitCast) || (constant->mConstType == BfConstType_BitCastNull)) { - auto bitcast = (BfConstantBitCast*)constant; - constant = mBfIRBuilder->GetConstantById(bitcast->mTarget); + auto bitcast = (BfConstantBitCast*)constant; + BfIRValue newVal; + if (bitcast->mTarget) + { + newVal = BfIRValue(BfIRValueFlags_Const, bitcast->mTarget); + CurrentAddToConstHolder(newVal); + } + irVal = mCurTypeInstance->GetOrCreateConstHolder()->CreateConstBitCast(newVal, bitcast->mToType); + return; } irVal = mCurTypeInstance->CreateConst(constant, mBfIRBuilder); @@ -10999,7 +11006,7 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con wantType = mContext->mTypes[constant->mIRType.mId]; if (wantType == NULL) - return constHolder->CreateConstNull(); + return mBfIRBuilder->CreateConstNull(); return GetDefaultValue(wantType); } @@ -11047,8 +11054,21 @@ BfIRValue BfModule::ConstantToCurrent(BfConstant* constant, BfIRConstHolder* con return mBfIRBuilder->CreateIntToPtr(ConstantToCurrent(fromTarget, constHolder, NULL), toIRType); } + if ((constant->mConstType == BfConstType_BitCast) || (constant->mConstType == BfConstType_BitCastNull)) + { + auto bitcast = (BfConstantBitCast*)constant; + auto fromTarget = constHolder->GetConstantById(bitcast->mTarget); + BfIRType toIRType = bitcast->mToType; + if (toIRType.mKind == BfIRTypeData::TypeKind_TypeId) + { + auto toType = mContext->mTypes[toIRType.mId]; + toIRType = mBfIRBuilder->MapType(toType); + } + return mBfIRBuilder->CreateBitCast(ConstantToCurrent(fromTarget, constHolder, NULL), toIRType); + } + if (constant->mConstType == BfConstType_Agg) - { + { auto constArray = (BfConstantAgg*)constant; if ((wantType == NULL) && (constArray->mType.mKind == BfIRTypeData::TypeKind_TypeId)) diff --git a/IDEHelper/Compiler/BfModuleTypeUtils.cpp b/IDEHelper/Compiler/BfModuleTypeUtils.cpp index 6ee0e55f..c134dc85 100644 --- a/IDEHelper/Compiler/BfModuleTypeUtils.cpp +++ b/IDEHelper/Compiler/BfModuleTypeUtils.cpp @@ -11811,7 +11811,7 @@ BfIRValue BfModule::CastToValue(BfAstNode* srcNode, BfTypedValue typedVal, BfTyp if (allowCast) { - if (ignoreWrites) + if ((ignoreWrites) && (!typedVal.mValue.IsConst())) return mBfIRBuilder->GetFakeVal(); return mBfIRBuilder->CreateBitCast(typedVal.mValue, mBfIRBuilder->MapType(toType)); }