1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 03:28:20 +02:00

Fixed some alignment differences between LLVM and BeefBE

This commit is contained in:
Brian Fiete 2020-04-03 10:34:26 -07:00
parent 0ae14f5a5d
commit 1ca01864bb
12 changed files with 73 additions and 21 deletions

View file

@ -1829,12 +1829,16 @@ void BeCOFFObject::InitSect(BeCOFFSection& sect, const StringImpl& name, int cha
MarkSectionUsed(sect, makeSectSymbol);
}
void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
void BeCOFFObject::AlignConst(BeCOFFSection& sect, BeConstant* constVal)
{
auto beType = constVal->GetType();
sect.mAlign = BF_MAX(sect.mAlign, beType->mAlign);
sect.mData.Align(beType->mAlign);
}
void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
{
auto beType = constVal->GetType();
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal))
{
auto sym = GetSymbol(globalVar);
@ -1882,13 +1886,6 @@ void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
else
BF_FATAL("Invalid StructConst type");
}
else if (auto constArr = BeValueDynCast<BeStructConstant>(constVal))
{
for (auto member : constArr->mMemberValues)
{
WriteConst(sect, member);
}
}
else if (auto constStr = BeValueDynCast<BeStringConstant>(constVal))
{
sect.mData.Write((void*)constStr->mString.c_str(), (int)constStr->mString.length() + 1);
@ -2013,6 +2010,11 @@ void BeCOFFObject::Generate(BeModule* module)
BF_ASSERT(globalVar->mAlign != -1);
if (globalVar->mName == "sBfTypeData._J")
{
NOP;
}
if (globalVar->mIsConstant)
{
auto constVal = BeValueDynCast<BeConstant>(globalVar->mInitializer);
@ -2021,9 +2023,9 @@ void BeCOFFObject::Generate(BeModule* module)
sym->mSectionNum = mRDataSect.mSectionIdx + 1;
mRDataSect.mData.Align(globalVar->mAlign);
mRDataSect.mAlign = BF_MAX(mRDataSect.mAlign, globalVar->mAlign);
sym->mValue = mRDataSect.mData.GetSize();
//mRDataSect.mSizeOverride += globalVar->mType->mSize;
AlignConst(mRDataSect, constVal);
sym->mValue = mRDataSect.mData.GetSize();
WriteConst(mRDataSect, constVal);
}
else if (globalVar->mIsTLS)

View file

@ -272,6 +272,7 @@ public:
void DbgEndLineBlock(BeDbgFunction* dbgFunc, const Array<BeDbgCodeEmission>& emissions, int blockStartPos, int emissionStartIdx, int lineCount);
void DbgGenerateModuleInfo();
void InitSect(BeCOFFSection& sect, const StringImpl& name, int characteristics, bool addNow, bool makeSectSymbol);
void AlignConst(BeCOFFSection& sect, BeConstant* constVal);
void WriteConst(BeCOFFSection& sect, BeConstant* constVal);
void Generate(BeModule* module);

View file

@ -108,6 +108,9 @@ void BeContext::SetStructBody(BeStructType* structType, const SizedArrayImpl<BeT
member.mType = beType;
member.mByteOffset = dataPos;
dataPos += beType->mSize;
if (packed)
structType->mAlign = 1;
else
structType->mAlign = std::max(structType->mAlign, beType->mAlign);
structType->mMembers.push_back(member);
}

View file

@ -2540,6 +2540,7 @@ void BfIRBuilder::CreateDbgTypeDefinition(BfType* type)
if (wasMadeAddr)
useType = mModule->CreatePointerType(useType);
staticValue = CreateGlobalVariable(mModule->mBfIRBuilder->MapType(useType), true, BfIRLinkageType_Internal, staticValue, staticVarName);
GlobalVar_SetAlignment(staticValue, useType->mAlign);
}
int flags = 0;

View file

@ -715,6 +715,7 @@ struct BfGlobalVar
int mStreamId;
BfIRValue mInitializer;
bool mIsTLS;
int mAlignment;
};
struct BfGlobalVar_TypeInst

View file

@ -3153,6 +3153,7 @@ void BfModule::CreateStaticField(BfFieldInstance* fieldInstance, bool isThreadLo
initValue,
staticVarName,
isThreadLocal);
mBfIRBuilder->GlobalVar_SetAlignment(globalVar, fieldType->mAlign);
BF_ASSERT(globalVar);
mStaticFieldRefs[fieldInstance] = globalVar;
@ -4460,6 +4461,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
auto pointerTypeData = mBfIRBuilder->CreateConstStruct(mBfIRBuilder->MapTypeInst(reflectPointerType, BfIRPopulateType_Full), pointerTypeDataParms);
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(reflectPointerType), true,
BfIRLinkageType_External, pointerTypeData, typeDataName);
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
}
else if (type->IsSizedArray())
@ -4487,6 +4489,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
else
typeDataVar = mBfIRBuilder->CreateConstNull(mBfIRBuilder->MapType(mContext->mBfTypeType));
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
mTypeDataRefs[typeInstance] = typeDataVar;
return typeDataVar;

View file

@ -4629,6 +4629,7 @@ void DbgExprEvaluator::Visit(BfIndexerExpression* indexerExpr)
return;
}
DbgTypedValue indexArgument = indexerValues[0];
indexArgument.mType = indexArgument.mType->RemoveModifiers();
if (!indexArgument.mType->IsInteger())
{
mResult = DbgTypedValue();

View file

@ -0,0 +1,31 @@
using System;
namespace Tests
{
class Arrays
{
struct StructA
{
public int16 mA = 11;
public int16 mB = 22;
public int16 mC = 33;
}
[Test]
public static void TestPacking()
{
StructA[] arr = scope .[3](.(), );
ref StructA sa = ref arr[0];
Test.Assert(sa.mA == 11);
Test.Assert(sa.mB == 22);
Test.Assert(sa.mC == 33);
#if BF_64_BIT
/*int a = (int)(void*)&sa - (int)Internal.UnsafeCastToPtr(arr);
int b = typeof(System.Array).InstanceSize;
Test.Assert((int)(void*)&sa - (int)Internal.UnsafeCastToPtr(arr) == sizeof(System.Array));*/
#endif
}
}
}

View file

@ -97,6 +97,15 @@ namespace Tests
public float mD = 4;
}
[Test]
static void TestTypes()
{
Type t = typeof(int32);
Test.Assert(t.InstanceSize == 4);
t = typeof(int64);
Test.Assert(t.InstanceSize == 8);
}
[Test]
static void TestA()
{