| 10615 | } |
| 10616 | |
| 10617 | BfIRValue BfModule::AppendAllocFromType(BfType* type, BfIRValue appendSizeValue, int appendAllocAlign, BfIRValue arraySize, int arrayDim, bool isRawArrayAlloc, bool zeroMemory) |
| 10618 | { |
| 10619 | auto localVar = mCurMethodState->GetRootMethodState()->mLocals[1]; |
| 10620 | BF_ASSERT(localVar->mName == "__appendIdx"); |
| 10621 | BfTypedValue appendIdxVal(localVar->mValue, localVar->mResolvedType, true); |
| 10622 | |
| 10623 | BfIRValue retValue; |
| 10624 | BfTypeInstance* retTypeInstance = NULL; |
| 10625 | auto intPtrType = GetPrimitiveType(BfTypeCode_IntPtr); |
| 10626 | auto voidPtrType = GetPrimitiveType(BfTypeCode_NullPtr); |
| 10627 | |
| 10628 | if (arraySize) |
| 10629 | { |
| 10630 | if (isRawArrayAlloc) |
| 10631 | { |
| 10632 | EmitAppendAlign(type->mAlign); |
| 10633 | BfPointerType* ptrType = CreatePointerType(type); |
| 10634 | BfIRValue sizeValue = mBfIRBuilder->CreateMul(GetConstValue(type->mSize), arraySize); |
| 10635 | auto curIdxVal = mBfIRBuilder->CreateLoad(appendIdxVal.mValue); |
| 10636 | if (mSystem->mPtrSize != 4) |
| 10637 | sizeValue = mBfIRBuilder->CreateNumericCast(sizeValue, true, (intPtrType->mSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64); |
| 10638 | auto newIdxVal = mBfIRBuilder->CreateAdd(curIdxVal, sizeValue); |
| 10639 | mBfIRBuilder->CreateStore(newIdxVal, appendIdxVal.mValue); |
| 10640 | |
| 10641 | if (zeroMemory) |
| 10642 | { |
| 10643 | auto ptr = mBfIRBuilder->CreateIntToPtr(curIdxVal, mBfIRBuilder->MapType(voidPtrType)); |
| 10644 | mBfIRBuilder->CreateMemSet(ptr, GetConstValue8(0), sizeValue, type->mAlign); |
| 10645 | } |
| 10646 | |
| 10647 | return mBfIRBuilder->CreateIntToPtr(curIdxVal, mBfIRBuilder->MapType(ptrType)); |
| 10648 | } |
| 10649 | else |
| 10650 | { |
| 10651 | BfArrayType* arrayType = CreateArrayType(type, arrayDim); |
| 10652 | EmitAppendAlign(arrayType->mAlign, type->mSize); |
| 10653 | |
| 10654 | auto firstElementField = &arrayType->mFieldInstances.back(); |
| 10655 | int arrayClassSize = arrayType->mInstSize - firstElementField->mDataSize; |
| 10656 | |
| 10657 | BfIRValue sizeValue = GetConstValue(arrayClassSize); |
| 10658 | BfIRValue elementDataSize = mBfIRBuilder->CreateMul(GetConstValue(type->mSize), arraySize); |
| 10659 | sizeValue = mBfIRBuilder->CreateAdd(sizeValue, elementDataSize); |
| 10660 | |
| 10661 | auto curIdxVal = mBfIRBuilder->CreateLoad(appendIdxVal.mValue); |
| 10662 | if (mSystem->mPtrSize != 4) |
| 10663 | sizeValue = mBfIRBuilder->CreateNumericCast(sizeValue, true, (intPtrType->mSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64); |
| 10664 | auto newIdxVal = mBfIRBuilder->CreateAdd(curIdxVal, sizeValue); |
| 10665 | mBfIRBuilder->CreateStore(newIdxVal, appendIdxVal.mValue); |
| 10666 | |
| 10667 | if (zeroMemory) |
| 10668 | { |
| 10669 | // Only zero out the actual elements |
| 10670 | int dataOffset = arrayType->mFieldInstances.back().mDataOffset; |
| 10671 | auto newOffset = mBfIRBuilder->CreateAdd(curIdxVal, GetConstValue(dataOffset)); |
| 10672 | auto newSize = mBfIRBuilder->CreateSub(sizeValue, GetConstValue(dataOffset)); |
| 10673 | auto ptr = mBfIRBuilder->CreateIntToPtr(newOffset, mBfIRBuilder->MapType(voidPtrType)); |
| 10674 | mBfIRBuilder->CreateMemSet(ptr, GetConstValue8(0), newSize, type->mAlign); |
no test coverage detected