1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Fixed issues with global var addresses in const arrays

This commit is contained in:
Brian Fiete 2020-07-13 08:51:02 -07:00
parent 52af512b4f
commit b30a72719c
13 changed files with 376 additions and 36 deletions

View file

@ -680,7 +680,8 @@ void BeIRCodeGen::Read(BeValue*& beValue)
globalVariable->mIsTLS = isTLS;
globalVariable->mAlign = varType->mAlign;
globalVariable->mUnnamedAddr = false;
BF_ASSERT(varType->mAlign > 0);
if (initializer != NULL)
BF_ASSERT(varType->mAlign > 0);
SetResult(streamId, globalVariable);
beValue = globalVariable;
@ -721,6 +722,19 @@ void BeIRCodeGen::Read(BeValue*& beValue)
BE_MEM_END("ParamType_Const_GEP32_2");
return;
}
else if (constType == BfConstType_ExtractValue)
{
CMD_PARAM(BeConstant*, target);
CMD_PARAM(int, idx0);
auto gepConstant = mBeModule->mAlloc.Alloc<BeExtractValueConstant>();
gepConstant->mTarget = target;
gepConstant->mIdx0 = idx0;
beValue = gepConstant;
BE_MEM_END("ParamType_Const_ExtractValue");
return;
}
else if (constType == BfConstType_PtrToInt)
{
CMD_PARAM(BeConstant*, target);

View file

@ -2258,9 +2258,9 @@ BeMCOperand BeMCContext::GetOperand(BeValue* value, bool allowMetaResult, bool a
}
case BeGEPConstant::TypeId:
{
auto gepConstant = (BeGEPConstant*)value;
auto gepConstant = (BeGEPConstant*)value;
auto mcVal = GetOperand(gepConstant->mTarget);
auto mcVal = GetOperand(gepConstant->mTarget);
BePointerType* ptrType = (BePointerType*)GetType(mcVal);
BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
@ -2297,6 +2297,21 @@ BeMCOperand BeMCContext::GetOperand(BeValue* value, bool allowMetaResult, bool a
return result;
}
break;
case BeExtractValueConstant::TypeId:
{
// Note: this only handles zero-aggregates
auto extractConstant = (BeExtractValueConstant*)value;
auto elementType = extractConstant->GetType();
auto mcVal = GetOperand(extractConstant->mTarget);
auto valType = GetType(mcVal);
BeConstant beConstant;
beConstant.mType = elementType;
beConstant.mUInt64 = 0;
return GetOperand(&beConstant);
}
break;
case BeFunction::TypeId:
{
auto sym = mCOFFObject->GetSymbol(value);
@ -9887,7 +9902,7 @@ bool BeMCContext::DoLegalization()
}
if (arg0Type->IsComposite())
{
{
if (arg1.mKind == BeMCOperandKind_Immediate_i64)
{
// This is just a "zero initializer" marker
@ -12185,8 +12200,10 @@ void BeMCContext::EmitAggMov(const BeMCOperand& dest, const BeMCOperand& src)
BF_ASSERT(src.mKind == BeMCOperandKind_ConstAgg);
auto aggConstant = src.mConstant;
Array<uint8> dataVec;
aggConstant->GetData(dataVec);
BeConstData constData;
aggConstant->GetData(constData);
Array<uint8>& dataVec = constData.mData;
int memSize = dataVec.size();
int curOfs = 0;
@ -12254,7 +12271,7 @@ void BeMCContext::EmitAggMov(const BeMCOperand& dest, const BeMCOperand& src)
}
else
{
// movabs r11, val32
// movabs r11, val64
Emit(0x49); Emit(0xBB);
mOut.Write((int64)val);
}
@ -12362,6 +12379,30 @@ void BeMCContext::EmitAggMov(const BeMCOperand& dest, const BeMCOperand& src)
Emit(0x89 - 1);
EmitModRMRel(EncodeRegNum(X64Reg_R11B), rmInfo.mRegA, rmInfo.mRegB, 1, rmInfo.mDisp + curOfs);
}
for (auto constVal : constData.mConsts)
{
BeMCRelocation reloc;
// movabs r11, val64
Emit(0x49); Emit(0xBB);
reloc.mKind = BeMCRelocationKind_ADDR64;
reloc.mOffset = mOut.GetPos();
auto operand = GetOperand(constVal.mConstant);
reloc.mSymTableIdx = operand.mSymbolIdx;
mCOFFObject->mTextSect.mRelocs.push_back(reloc);
mTextRelocs.push_back((int)mCOFFObject->mTextSect.mRelocs.size() - 1);
mOut.Write((int64)0);
// mov <dest+curOfs>, R11
EmitREX(BeMCOperand::FromReg(X64Reg_R11), dest, true);
Emit(0x89);
EmitModRMRel(EncodeRegNum(X64Reg_R11), rmInfo.mRegA, rmInfo.mRegB, 1, rmInfo.mDisp + constVal.mIdx);
}
}
void BeMCContext::DoCodeEmission()
@ -13166,6 +13207,11 @@ void BeMCContext::DoCodeEmission()
{
if (inst->mArg1.mKind == BeMCOperandKind_ConstAgg)
{
if (mDebugging)
{
NOP;
}
EmitAggMov(inst->mArg0, inst->mArg1);
break;
}
@ -15487,7 +15533,7 @@ void BeMCContext::Generate(BeFunction* function)
mDbgPreferredRegs[32] = X64Reg_R8;*/
//mDbgPreferredRegs[8] = X64Reg_RAX;
//mDebugging = (function->mName == "?Main@Program@bf@@SAXPEAV?$Array1@PEAVString@System@bf@@@System@2@@Z");
mDebugging = (function->mName == "?Hey@Blurg@bf@@SAXXZ");
// || (function->mName == "?__BfStaticCtor@roboto_font@Drawing@ClassicUO_assistant@bf@@SAXXZ")
// || (function->mName == "?Hey@Blurg@bf@@SAXXZ")
// ;

View file

@ -445,25 +445,25 @@ BeType* BeConstant::GetType()
return mType;
}
void BeConstant::GetData(Array<uint8>& data)
void BeConstant::GetData(BeConstData& data)
{
auto type = GetType();
while ((((int)data.size()) % type->mAlign) != 0)
data.push_back(0);
while ((((int)data.mData.size()) % type->mAlign) != 0)
data.mData.push_back(0);
if (type->IsComposite())
{
for (int i = 0; i < type->mSize; i++)
data.push_back(0); // Aggregate
data.mData.push_back(0); // Aggregate
}
else if (type->mTypeCode == BeTypeCode_Float)
{
float f = mDouble;
data.Insert(data.mSize, (uint8*)&f, sizeof(float));
data.mData.Insert(data.mData.mSize, (uint8*)&f, sizeof(float));
}
else
{
data.Insert(data.mSize, &mUInt8, type->mSize);
data.mData.Insert(data.mData.mSize, &mUInt8, type->mSize);
}
}
@ -491,7 +491,7 @@ void BeConstant::HashContent(BeHashContext& hashCtx)
BF_FATAL("NotImpl");
}
void BeStructConstant::GetData(Array<uint8>& data)
void BeStructConstant::GetData(BeConstData& data)
{
for (auto val : mMemberValues)
val->GetData(data);
@ -519,6 +519,27 @@ BeType* BeGEPConstant::GetType()
}
}
BeType* BeExtractValueConstant::GetType()
{
BeType* type = mTarget->GetType();
if (type->mTypeCode == BeTypeCode_SizedArray)
{
BeSizedArrayType* arrayType = (BeSizedArrayType*)type;
BF_ASSERT(arrayType->mTypeCode == BeTypeCode_SizedArray);
return arrayType->mContext->GetPointerTo(arrayType->mElementType);
}
/*else if (ptrType->mElementType->IsPointer())
{
return ptrType->mElementType;
}*/
else
{
BeStructType* structType = (BeStructType*)type;
BF_ASSERT(structType->mTypeCode == BeTypeCode_Struct);
return structType->mContext->GetPointerTo(structType->mMembers[mIdx0].mType);
}
}
BeType* BeGlobalVariable::GetType()
{
//if (mIsConstant)
@ -1240,6 +1261,14 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto constantExtract = BeValueDynCast<BeExtractValueConstant>(value))
{
str += "ConstExtract ";
ToString(str, constantExtract->mTarget);
str += StrFormat(" %d", constantExtract->mIdx0);
return;
}
if (auto arg = BeValueDynCast<BeArgument>(value))
{
auto activeFunction = arg->mModule->mActiveFunction;
@ -1313,6 +1342,15 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto constant = BeValueDynCast<BeExtractValueConstant>(value))
{
ToString(str, constant->GetType());
str += " extract (";
ToString(str, constant->mTarget);
str += StrFormat(", %d)", constant->mIdx0);
return;
}
if (auto constant = BeValueDynCast<BeStructConstant>(value))
{
ToString(str, constant->GetType());

View file

@ -271,6 +271,20 @@ class BeDbgLoc;
class BeMDNode;
class BeGlobalVariable;
class BeConstant;
struct BeConstData
{
struct ConstantEntry
{
int mIdx;
BeConstant* mConstant;
};
Array<uint8> mData;
Array<ConstantEntry> mConsts;
};
class BeConstant : public BeValue
{
public:
@ -304,7 +318,7 @@ public:
}
virtual BeType* GetType();
virtual void GetData(Array<uint8>& data);
virtual void GetData(BeConstData& data);
virtual void HashContent(BeHashContext& hashCtx) override;
};
@ -319,6 +333,11 @@ public:
mType->HashReference(hashCtx);
mTarget->HashReference(hashCtx);
}
virtual void GetData(BeConstData& data) override
{
mTarget->GetData(data);
}
};
class BeGEPConstant : public BeConstant
@ -339,6 +358,22 @@ public:
}
};
class BeExtractValueConstant : public BeConstant
{
public:
BE_VALUE_TYPE(BeExtractValueConstant, BeConstant);
int mIdx0;
virtual BeType* GetType();
virtual void HashContent(BeHashContext& hashCtx) override
{
hashCtx.Mixin(TypeId);
mTarget->HashReference(hashCtx);
hashCtx.Mixin(mIdx0);
}
};
class BeStructConstant : public BeConstant
{
public:
@ -346,7 +381,7 @@ public:
SizedArray<BeConstant*, 4> mMemberValues;
virtual void GetData(Array<uint8>& data) override;
virtual void GetData(BeConstData& data) override;
virtual void HashContent(BeHashContext& hashCtx) override
{
@ -399,6 +434,12 @@ public:
hashCtx.Mixin(mAlign);
hashCtx.Mixin(mUnnamedAddr);
}
virtual void GetData(BeConstData& data) override
{
data.mConsts.Add({ (int)data.mData.size(), this });
data.mData.Insert(data.mData.size(), (uint8)0, 8);
}
};
class BeFunctionParam
@ -679,7 +720,7 @@ public:
hashCtx.Mixin(TypeId);
mValue->HashReference(hashCtx);
mToType->HashReference(hashCtx);
}
}
};
class BeNegInst : public BeInst