mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-08 11:38:21 +02:00
Fixed some alignment differences between LLVM and BeefBE
This commit is contained in:
parent
0ae14f5a5d
commit
1ca01864bb
12 changed files with 73 additions and 21 deletions
|
@ -1829,12 +1829,16 @@ void BeCOFFObject::InitSect(BeCOFFSection& sect, const StringImpl& name, int cha
|
||||||
MarkSectionUsed(sect, makeSectSymbol);
|
MarkSectionUsed(sect, makeSectSymbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
|
void BeCOFFObject::AlignConst(BeCOFFSection& sect, BeConstant* constVal)
|
||||||
{
|
{
|
||||||
auto beType = constVal->GetType();
|
auto beType = constVal->GetType();
|
||||||
sect.mAlign = BF_MAX(sect.mAlign, beType->mAlign);
|
sect.mAlign = BF_MAX(sect.mAlign, beType->mAlign);
|
||||||
sect.mData.Align(beType->mAlign);
|
sect.mData.Align(beType->mAlign);
|
||||||
|
}
|
||||||
|
|
||||||
|
void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
|
||||||
|
{
|
||||||
|
auto beType = constVal->GetType();
|
||||||
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal))
|
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constVal))
|
||||||
{
|
{
|
||||||
auto sym = GetSymbol(globalVar);
|
auto sym = GetSymbol(globalVar);
|
||||||
|
@ -1882,13 +1886,6 @@ void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
|
||||||
else
|
else
|
||||||
BF_FATAL("Invalid StructConst type");
|
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))
|
else if (auto constStr = BeValueDynCast<BeStringConstant>(constVal))
|
||||||
{
|
{
|
||||||
sect.mData.Write((void*)constStr->mString.c_str(), (int)constStr->mString.length() + 1);
|
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);
|
BF_ASSERT(globalVar->mAlign != -1);
|
||||||
|
|
||||||
|
if (globalVar->mName == "sBfTypeData._J")
|
||||||
|
{
|
||||||
|
NOP;
|
||||||
|
}
|
||||||
|
|
||||||
if (globalVar->mIsConstant)
|
if (globalVar->mIsConstant)
|
||||||
{
|
{
|
||||||
auto constVal = BeValueDynCast<BeConstant>(globalVar->mInitializer);
|
auto constVal = BeValueDynCast<BeConstant>(globalVar->mInitializer);
|
||||||
|
@ -2021,9 +2023,9 @@ void BeCOFFObject::Generate(BeModule* module)
|
||||||
sym->mSectionNum = mRDataSect.mSectionIdx + 1;
|
sym->mSectionNum = mRDataSect.mSectionIdx + 1;
|
||||||
mRDataSect.mData.Align(globalVar->mAlign);
|
mRDataSect.mData.Align(globalVar->mAlign);
|
||||||
mRDataSect.mAlign = BF_MAX(mRDataSect.mAlign, 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);
|
WriteConst(mRDataSect, constVal);
|
||||||
}
|
}
|
||||||
else if (globalVar->mIsTLS)
|
else if (globalVar->mIsTLS)
|
||||||
|
|
|
@ -272,6 +272,7 @@ public:
|
||||||
void DbgEndLineBlock(BeDbgFunction* dbgFunc, const Array<BeDbgCodeEmission>& emissions, int blockStartPos, int emissionStartIdx, int lineCount);
|
void DbgEndLineBlock(BeDbgFunction* dbgFunc, const Array<BeDbgCodeEmission>& emissions, int blockStartPos, int emissionStartIdx, int lineCount);
|
||||||
void DbgGenerateModuleInfo();
|
void DbgGenerateModuleInfo();
|
||||||
void InitSect(BeCOFFSection& sect, const StringImpl& name, int characteristics, bool addNow, bool makeSectSymbol);
|
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 WriteConst(BeCOFFSection& sect, BeConstant* constVal);
|
||||||
|
|
||||||
void Generate(BeModule* module);
|
void Generate(BeModule* module);
|
||||||
|
|
|
@ -108,6 +108,9 @@ void BeContext::SetStructBody(BeStructType* structType, const SizedArrayImpl<BeT
|
||||||
member.mType = beType;
|
member.mType = beType;
|
||||||
member.mByteOffset = dataPos;
|
member.mByteOffset = dataPos;
|
||||||
dataPos += beType->mSize;
|
dataPos += beType->mSize;
|
||||||
|
if (packed)
|
||||||
|
structType->mAlign = 1;
|
||||||
|
else
|
||||||
structType->mAlign = std::max(structType->mAlign, beType->mAlign);
|
structType->mAlign = std::max(structType->mAlign, beType->mAlign);
|
||||||
structType->mMembers.push_back(member);
|
structType->mMembers.push_back(member);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2540,6 +2540,7 @@ void BfIRBuilder::CreateDbgTypeDefinition(BfType* type)
|
||||||
if (wasMadeAddr)
|
if (wasMadeAddr)
|
||||||
useType = mModule->CreatePointerType(useType);
|
useType = mModule->CreatePointerType(useType);
|
||||||
staticValue = CreateGlobalVariable(mModule->mBfIRBuilder->MapType(useType), true, BfIRLinkageType_Internal, staticValue, staticVarName);
|
staticValue = CreateGlobalVariable(mModule->mBfIRBuilder->MapType(useType), true, BfIRLinkageType_Internal, staticValue, staticVarName);
|
||||||
|
GlobalVar_SetAlignment(staticValue, useType->mAlign);
|
||||||
}
|
}
|
||||||
|
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
|
@ -715,6 +715,7 @@ struct BfGlobalVar
|
||||||
int mStreamId;
|
int mStreamId;
|
||||||
BfIRValue mInitializer;
|
BfIRValue mInitializer;
|
||||||
bool mIsTLS;
|
bool mIsTLS;
|
||||||
|
int mAlignment;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct BfGlobalVar_TypeInst
|
struct BfGlobalVar_TypeInst
|
||||||
|
|
|
@ -3153,6 +3153,7 @@ void BfModule::CreateStaticField(BfFieldInstance* fieldInstance, bool isThreadLo
|
||||||
initValue,
|
initValue,
|
||||||
staticVarName,
|
staticVarName,
|
||||||
isThreadLocal);
|
isThreadLocal);
|
||||||
|
mBfIRBuilder->GlobalVar_SetAlignment(globalVar, fieldType->mAlign);
|
||||||
|
|
||||||
BF_ASSERT(globalVar);
|
BF_ASSERT(globalVar);
|
||||||
mStaticFieldRefs[fieldInstance] = 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);
|
auto pointerTypeData = mBfIRBuilder->CreateConstStruct(mBfIRBuilder->MapTypeInst(reflectPointerType, BfIRPopulateType_Full), pointerTypeDataParms);
|
||||||
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(reflectPointerType), true,
|
typeDataVar = mBfIRBuilder->CreateGlobalVariable(mBfIRBuilder->MapTypeInst(reflectPointerType), true,
|
||||||
BfIRLinkageType_External, pointerTypeData, typeDataName);
|
BfIRLinkageType_External, pointerTypeData, typeDataName);
|
||||||
|
|
||||||
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
|
typeDataVar = mBfIRBuilder->CreateBitCast(typeDataVar, mBfIRBuilder->MapType(mContext->mBfTypeType));
|
||||||
}
|
}
|
||||||
else if (type->IsSizedArray())
|
else if (type->IsSizedArray())
|
||||||
|
@ -4487,6 +4489,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
|
||||||
else
|
else
|
||||||
typeDataVar = mBfIRBuilder->CreateConstNull(mBfIRBuilder->MapType(mContext->mBfTypeType));
|
typeDataVar = mBfIRBuilder->CreateConstNull(mBfIRBuilder->MapType(mContext->mBfTypeType));
|
||||||
|
|
||||||
|
mBfIRBuilder->GlobalVar_SetAlignment(typeDataVar, mSystem->mPtrSize);
|
||||||
mTypeDataRefs[typeInstance] = typeDataVar;
|
mTypeDataRefs[typeInstance] = typeDataVar;
|
||||||
|
|
||||||
return typeDataVar;
|
return typeDataVar;
|
||||||
|
|
|
@ -4629,6 +4629,7 @@ void DbgExprEvaluator::Visit(BfIndexerExpression* indexerExpr)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DbgTypedValue indexArgument = indexerValues[0];
|
DbgTypedValue indexArgument = indexerValues[0];
|
||||||
|
indexArgument.mType = indexArgument.mType->RemoveModifiers();
|
||||||
if (!indexArgument.mType->IsInteger())
|
if (!indexArgument.mType->IsInteger())
|
||||||
{
|
{
|
||||||
mResult = DbgTypedValue();
|
mResult = DbgTypedValue();
|
||||||
|
|
31
IDEHelper/Tests/src/Arrays.bf
Normal file
31
IDEHelper/Tests/src/Arrays.bf
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -97,6 +97,15 @@ namespace Tests
|
||||||
public float mD = 4;
|
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]
|
[Test]
|
||||||
static void TestA()
|
static void TestA()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue