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

Comptime debugging

This commit is contained in:
Brian Fiete 2022-03-08 06:27:06 -08:00
parent bbb97d1490
commit ff2e40e3bf
40 changed files with 6213 additions and 443 deletions

View file

@ -475,9 +475,9 @@ void BeCOFFObject::DbgMakeFuncType(BeDbgFunction* dbgFunc)
if (hasThis)
{
if ((dbgFunc->mVariables.size() > 0) && (dbgFunc->mVariables[0] == NULL))
outT.Write(DbgGetTypeId(dbgFunc->GetParamType(1))); // 0 is sret, 1 = this
outT.Write(DbgGetTypeId(BeValueDynCast<BeDbgType>(dbgFunc->GetParamType(1)))); // 0 is sret, 1 = this
else
outT.Write(DbgGetTypeId(dbgFunc->GetParamType(0))); // 0 = this
outT.Write(DbgGetTypeId(BeValueDynCast<BeDbgType>(dbgFunc->GetParamType(0)))); // 0 = this
}
else
outT.Write((int32)T_VOID);
@ -941,7 +941,7 @@ int BeCOFFObject::DbgGetTypeId(BeDbgType* dbgType, bool doDefine)
{
CV_modifier_t attr = { 0 };
attr.MOD_const = 1;
int32 elementId = DbgGetTypeId(constType->mElement);
int32 elementId = DbgGetTypeId(BeValueDynCast<BeDbgType>(constType->mElement));
DbgTStartTag();
outT.Write((int16)LF_MODIFIER);
@ -985,10 +985,10 @@ void BeCOFFObject::DbgGenerateTypeInfo()
auto& outT = mDebugTSect.mData;
outT.Write((int)CV_SIGNATURE_C13);
for (auto dbgType : mBeModule->mDbgModule->mTypes)
for (auto mdNode : mBeModule->mDbgModule->mTypes)
{
bool defineType = true;
if (auto dbgStructType = BeValueDynCast<BeDbgStructType>(dbgType))
if (auto dbgStructType = BeValueDynCast<BeDbgStructType>(mdNode))
{
if (!dbgStructType->mIsFullyDefined)
defineType = false;
@ -996,7 +996,8 @@ void BeCOFFObject::DbgGenerateTypeInfo()
if (defineType)
{
DbgGetTypeId(dbgType, true);
if (auto dbgType = BeValueDynCast<BeDbgType>(mdNode))
DbgGetTypeId(dbgType, true);
}
}
@ -1032,6 +1033,8 @@ void BeCOFFObject::DbgStartVarDefRange(BeDbgFunction* dbgFunc, BeDbgVariable* db
auto funcSym = GetSymbol(dbgFunc->mValue);
auto varType = BeValueDynCast<BeDbgType>(dbgVar->mType);
auto& outS = mDebugSSect.mData;
if (varLoc.mKind == BeDbgVariableLoc::Kind_SymbolAddr)
{
@ -1042,14 +1045,14 @@ void BeCOFFObject::DbgStartVarDefRange(BeDbgFunction* dbgFunc, BeDbgVariable* db
if (varLoc.mOfs == 0)
{
outS.Write((int16)S_DEFRANGE_REGISTER);
outS.Write((int16)GetCVRegNum(varLoc.mReg, dbgVar->mType->mSize * 8));
outS.Write((int16)GetCVRegNum(varLoc.mReg, varType->mSize * 8));
CV_RANGEATTR rangeAttr = { 0 };
outS.Write(*(int16*)&rangeAttr); // offset to register
}
else
{
outS.Write((int16)S_DEFRANGE_REGISTER_REL);
outS.Write((int16)GetCVRegNum(varLoc.mReg, dbgVar->mType->mSize * 8));
outS.Write((int16)GetCVRegNum(varLoc.mReg, varType->mSize * 8));
outS.Write((int16)0);
outS.Write((int32)varLoc.mOfs);
// CV_RANGEATTR rangeAttr = { 0 };
@ -1135,6 +1138,8 @@ void BeCOFFObject::DbgSEndTag()
void BeCOFFObject::DbgOutputLocalVar(BeDbgFunction* dbgFunc, BeDbgVariable* dbgVar)
{
auto varType = BeValueDynCast<BeDbgType>(dbgVar->mType);
// CodeView only allows 16-bit lengths, so we need to split ranges for very long spans
if (dbgVar->mDeclEnd - dbgVar->mDeclStart > 0xFFFF)
{
@ -1215,7 +1220,7 @@ void BeCOFFObject::DbgOutputLocalVar(BeDbgFunction* dbgFunc, BeDbgVariable* dbgV
DbgSStartTag();
outS.Write((int16)S_LOCAL);
outS.Write(DbgGetTypeId(dbgVar->mType));
outS.Write(DbgGetTypeId(varType));
CV_LVARFLAGS flags = { 0 };
if (dbgVar->mParamNum != -1)
@ -1732,7 +1737,7 @@ void BeCOFFObject::DbgGenerateModuleInfo()
else
outS.Write(dbgGlobalVar->mIsLocalToUnit ? (int16)S_LDATA32 : (int16)S_GDATA32);
outS.Write(DbgGetTypeId(dbgGlobalVar->mType));
outS.Write(DbgGetTypeId(BeValueDynCast<BeDbgType>(dbgGlobalVar->mType)));
BF_ASSERT(dbgGlobalVar->mValue != NULL);
@ -1896,7 +1901,7 @@ void BeCOFFObject::WriteConst(BeCOFFSection& sect, BeConstant* constVal)
{
WriteConst(sect, constCast->mTarget);
}
else if (auto constGep = BeValueDynCast<BeGEPConstant>(constVal))
else if (auto constGep = BeValueDynCast<BeGEP2Constant>(constVal))
{
if (auto globalVar = BeValueDynCast<BeGlobalVariable>(constGep->mTarget))
{

View file

@ -5,6 +5,9 @@
#include "BeefySysLib/util/AllocDebug.h"
#include "BeefySysLib/util/Hash.h"
#include "BeModule.h"
#include "BeContext.h"
#include "..\Compiler\CeMachine.h"
#ifdef _DEBUG
#define BE_EXTRA_CHECKS
@ -228,7 +231,7 @@ BeIRCodeGen::BeIRCodeGen()
mBeContext = NULL;
mBeModule = NULL;
mHasDebugLoc = false;
mDebugging = false;
mDebugging = false;
mCmdCount = 0;
}
@ -736,6 +739,20 @@ void BeIRCodeGen::Read(BeValue*& beValue)
BE_MEM_END("ParamType_Const_BitCast");
return;
}
else if (constType == BfConstType_GEP32_1)
{
CMD_PARAM(BeConstant*, target);
CMD_PARAM(int, idx0);
BF_ASSERT(target->GetType()->IsPointer());
auto gepConstant = mBeModule->mAlloc.Alloc<BeGEP1Constant>();
gepConstant->mTarget = target;
gepConstant->mIdx0 = idx0;
beValue = gepConstant;
BE_MEM_END("ParamType_Const_GEP32_1");
return;
}
else if (constType == BfConstType_GEP32_2)
{
CMD_PARAM(BeConstant*, target);
@ -743,7 +760,7 @@ void BeIRCodeGen::Read(BeValue*& beValue)
CMD_PARAM(int, idx1);
BF_ASSERT(target->GetType()->IsPointer());
auto gepConstant = mBeModule->mAlloc.Alloc<BeGEPConstant>();
auto gepConstant = mBeModule->mAlloc.Alloc<BeGEP2Constant>();
gepConstant->mTarget = target;
gepConstant->mIdx0 = idx0;
gepConstant->mIdx1 = idx1;
@ -2852,7 +2869,15 @@ void BeIRCodeGen::HandleNextCmd()
case BfIRCmd_DbgGetType:
{
CMD_PARAM(int, typeId);
SetResult(curId, GetTypeEntry(typeId).mDIType);
if (mBeModule->mCeMachine != NULL)
{
auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgTypeId>();
dbgType->mTypeId = typeId;
SetResult(curId, dbgType);
}
else
SetResult(curId, GetTypeEntry(typeId).mDIType);
}
break;
case BfIRCmd_DbgGetTypeInst:
@ -2974,7 +2999,21 @@ void BeIRCodeGen::HandleNextCmd()
{
CMD_PARAM(BeMDNode*, elementTypeNode);
BeDbgType* elementType = (BeDbgType*)elementTypeNode;
BeDbgType* elementType = BeValueDynCast<BeDbgType>(elementTypeNode);
if (elementType == NULL)
{
if (auto dbgTypeId = BeValueDynCast<BeDbgTypeId>(elementTypeNode))
{
auto bfElementType = mBeModule->mCeMachine->mCeModule->mContext->mTypes[dbgTypeId->mTypeId];
auto bfPtrType = mBeModule->mCeMachine->mCeModule->CreatePointerType(bfElementType);
auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgTypeId>();
dbgType->mTypeId = bfPtrType->mTypeId;
SetResult(curId, dbgType);
break;
}
}
BeDbgType* useType = elementType->FindDerivedType(BeDbgPointerType::TypeId);
if (useType == NULL)
{
@ -2992,7 +3031,23 @@ void BeIRCodeGen::HandleNextCmd()
case BfIRCmd_DbgCreateReferenceType:
{
CMD_PARAM(BeMDNode*, elementTypeNode);
auto useType = mBeModule->mDbgModule->CreateReferenceType((BeDbgType*)elementTypeNode);
BeDbgType* elementType = BeValueDynCast<BeDbgType>(elementTypeNode);
if (elementType == NULL)
{
if (auto dbgTypeId = BeValueDynCast<BeDbgTypeId>(elementTypeNode))
{
auto bfElementType = mBeModule->mCeMachine->mCeModule->mContext->mTypes[dbgTypeId->mTypeId];
auto bfPtrType = mBeModule->mCeMachine->mCeModule->CreateRefType(bfElementType);
auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgTypeId>();
dbgType->mTypeId = bfPtrType->mTypeId;
SetResult(curId, dbgType);
break;
}
}
auto useType = mBeModule->mDbgModule->CreateReferenceType(elementType);
SetResult(curId, useType);
}
break;
@ -3000,7 +3055,15 @@ void BeIRCodeGen::HandleNextCmd()
{
CMD_PARAM(BeMDNode*, elementTypeNode);
BeDbgType* elementType = (BeDbgType*)elementTypeNode;
BeDbgType* elementType = BeValueDynCast<BeDbgType>(elementTypeNode);
if (elementType == NULL)
{
auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgConstType>();
dbgType->mElement = elementTypeNode;
SetResult(curId, dbgType);
break;
}
BeDbgType* useType = elementType->FindDerivedType(BeDbgConstType::TypeId);
if (useType == NULL)
{
@ -3363,7 +3426,7 @@ void BeIRCodeGen::HandleNextCmd()
auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
dbgVar->mName = name;
dbgVar->mType = (BeDbgType*)type;
dbgVar->mType = type;
dbgVar->mParamNum = argNo - 1;
int argIdx = argNo - 1;
@ -3411,7 +3474,7 @@ void BeIRCodeGen::HandleNextCmd()
auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
dbgVar->mName = name;
dbgVar->mType = (BeDbgType*)type;
dbgVar->mType = type;
dbgVar->mScope = scope;
dbgVar->mInitType = (BfIRInitType)initType;
mActiveFunction->mDbgFunction->mVariables.push_back(dbgVar);
@ -3490,7 +3553,7 @@ void BeIRCodeGen::HandleNextCmd()
dbgGlobalVariable->mLinkageName = linkageName;
dbgGlobalVariable->mFile = (BeDbgFile*)file;
dbgGlobalVariable->mLineNum = lineNum;
dbgGlobalVariable->mType = (BeDbgType*)type;
dbgGlobalVariable->mType = type;
dbgGlobalVariable->mIsLocalToUnit = isLocalToUnit;
dbgGlobalVariable->mValue = val;
dbgGlobalVariable->mDecl = decl;

View file

@ -82,7 +82,7 @@ public:
BeContext* mBeContext;
BeModule* mBeModule;
Array<BeDbgLoc*> mSavedDebugLocs;
bool mHasDebugLoc;
bool mHasDebugLoc;
int mCmdCount;
Dictionary<int, BeIRCodeGenEntry> mResults;

View file

@ -2279,9 +2279,34 @@ BeMCOperand BeMCContext::GetOperand(BeValue* value, bool allowMetaResult, bool a
return mcOperand;
}
case BeGEPConstant::TypeId:
case BeGEP1Constant::TypeId:
{
auto gepConstant = (BeGEPConstant*)value;
auto gepConstant = (BeGEP1Constant*)value;
auto mcVal = GetOperand(gepConstant->mTarget);
BePointerType* ptrType = (BePointerType*)GetType(mcVal);
BEMC_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
auto result = mcVal;
// We assume we never do both an idx0 and idx1 at once. Fix if we change that.
int byteOffset = 0;
BeType* elementType = ptrType->mElementType;
byteOffset += gepConstant->mIdx0 * ptrType->mElementType->GetStride();
result = AllocRelativeVirtualReg(ptrType, result, GetImmediate(byteOffset), 1);
// The def is primary to create a single 'master location' for the GEP vreg to become legalized before use
auto vregInfo = GetVRegInfo(result);
vregInfo->mDefOnFirstUse = true;
result.mKind = BeMCOperandKind_VReg;
return result;
}
break;
case BeGEP2Constant::TypeId:
{
auto gepConstant = (BeGEP2Constant*)value;
auto mcVal = GetOperand(gepConstant->mTarget);
@ -2887,10 +2912,14 @@ static bool NeedsDecompose(BeConstant* constant)
if (auto targetConstant = BeValueDynCast<BeConstant>(castConst->mValue))
return NeedsDecompose(targetConstant);
}
else if (auto castConst = BeValueDynCast<BeGEPConstant>(constant))
else if (auto castConst = BeValueDynCast<BeGEP1Constant>(constant))
{
return NeedsDecompose(castConst->mTarget);
}
else if (auto castConst = BeValueDynCast<BeGEP2Constant>(constant))
{
return NeedsDecompose(castConst->mTarget);
}
return false;
}
@ -8681,7 +8710,7 @@ void BeMCContext::DoActualization()
else
{
inst->mArg0.mKind = BeMCOperandKind_VRegAddr;
vregInfo->mDbgVariable->mType = mModule->mDbgModule->CreateReferenceType(vregInfo->mDbgVariable->mType);
vregInfo->mDbgVariable->mType = mModule->mDbgModule->CreateReferenceType(BeValueDynCast<BeDbgType>(vregInfo->mDbgVariable->mType));
}
}
vregInfo->mWantsExprActualize = false;

View file

@ -497,7 +497,13 @@ void BeStructConstant::GetData(BeConstData& data)
val->GetData(data);
}
BeType* BeGEPConstant::GetType()
BeType* BeGEP1Constant::GetType()
{
BePointerType* ptrType = (BePointerType*)mTarget->GetType();
return ptrType;
}
BeType* BeGEP2Constant::GetType()
{
BePointerType* ptrType = (BePointerType*)mTarget->GetType();
BF_ASSERT(ptrType->mTypeCode == BeTypeCode_Pointer);
@ -1040,6 +1046,12 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto dbgType = BeValueDynCast<BeDbgTypeId>(mdNode))
{
str += StrFormat("DbgTypeId: %d", dbgType->mTypeId);
return;
}
if (auto dbgVar = BeValueDynCast<BeDbgVariable>(mdNode))
{
ToString(str, dbgVar->mType);
@ -1281,9 +1293,17 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto constantGEP = BeValueDynCast<BeGEPConstant>(value))
if (auto constantGEP = BeValueDynCast<BeGEP1Constant>(value))
{
str += "ConstGEP1 ";
ToString(str, constantGEP->mTarget);
str += StrFormat(" %d %d", constantGEP->mIdx0);
return;
}
if (auto constantGEP = BeValueDynCast<BeGEP2Constant>(value))
{
str += "ConstGep ";
str += "ConstGEP2 ";
ToString(str, constantGEP->mTarget);
str += StrFormat(" %d %d", constantGEP->mIdx0, constantGEP->mIdx1);
return;
@ -1373,7 +1393,16 @@ void BeDumpContext::ToString(StringImpl& str, BeValue* value, bool showType, boo
return;
}
if (auto constant = BeValueDynCast<BeGEPConstant>(value))
if (auto constant = BeValueDynCast<BeGEP1Constant>(value))
{
ToString(str, constant->GetType());
str += " gep (";
ToString(str, constant->mTarget);
str += StrFormat(", %d)", constant->mIdx0);
return;
}
if (auto constant = BeValueDynCast<BeGEP2Constant>(value))
{
ToString(str, constant->GetType());
str += " gep (";
@ -1762,6 +1791,7 @@ BeModule::BeModule(const StringImpl& moduleName, BeContext* context)
mLastDbgLoc = NULL;
mActiveFunction = NULL;
mDbgModule = NULL;
mCeMachine = NULL;
mPrevDbgLocInline = NULL;
mCurDbgLocIdx = 0;
mCurLexBlockId = 0;

View file

@ -55,6 +55,7 @@ class BePhiInst;
class BeSwitchInst;
class BeRetInst;
class BeCallInst;
class CeMachine;
class BeDbgVariable;
class BeDbgDeclareInst;
@ -357,10 +358,26 @@ public:
}
};
class BeGEPConstant : public BeConstant
class BeGEP1Constant : public BeConstant
{
public:
BE_VALUE_TYPE(BeGEPConstant, BeConstant);
BE_VALUE_TYPE(BeGEP1Constant, BeConstant);
int mIdx0;
virtual BeType* GetType();
virtual void HashContent(BeHashContext& hashCtx) override
{
hashCtx.Mixin(TypeId);
mTarget->HashReference(hashCtx);
hashCtx.Mixin(mIdx0);
}
};
class BeGEP2Constant : public BeConstant
{
public:
BE_VALUE_TYPE(BeGEP2Constant, BeConstant);
int mIdx0;
int mIdx1;
@ -1646,6 +1663,26 @@ public:
}
};
class BeDbgTypeId : public BeMDNode
{
public:
BE_VALUE_TYPE(BeDbgTypeId, BeMDNode);
public:
int mTypeId;
BeDbgTypeId()
{
mTypeId = -1;
}
virtual void HashContent(BeHashContext& hashCtx) override
{
hashCtx.Mixin(TypeId);
hashCtx.Mixin(mTypeId);
}
};
class BeDbgType : public BeMDNode
{
public:
@ -1743,7 +1780,7 @@ public:
BE_VALUE_TYPE(BeDbgConstType, BeDbgType);
public:
BeDbgType* mElement;
BeMDNode* mElement;
virtual void HashContent(BeHashContext& hashCtx) override
{
@ -1920,7 +1957,7 @@ public:
public:
String mName;
BeDbgType* mType;
BeMDNode* mType;
BeValue* mValue;
int mParamNum;
BfIRInitType mInitType;
@ -2044,7 +2081,7 @@ public:
mCvArgListId = -1;
}
BeDbgType* GetParamType(int paramIdx)
BeMDNode* GetParamType(int paramIdx)
{
/*if (!mParams.empty())
return mParams[paramIdx]->mType;*/
@ -2209,7 +2246,7 @@ public:
String mLinkageName;
BeDbgFile* mFile;
int mLineNum;
BeDbgType* mType;
BeMDNode* mType;
bool mIsLocalToUnit;
BeConstant* mValue;
BeMDNode* mDecl;
@ -2246,7 +2283,7 @@ public:
OwnedVector<BeDbgNamespace> mNamespaces;
OwnedVector<BeDbgGlobalVariable> mGlobalVariables;
OwnedVector<BeDbgType> mTypes;
OwnedVector<BeMDNode> mTypes;
Array<BeDbgFunction*> mFuncs; // Does not include methods in structs
virtual void HashContent(BeHashContext& hashCtx) override;
@ -2284,6 +2321,7 @@ public:
int mCurLexBlockId;
BeDbgModule* mDbgModule;
CeMachine* mCeMachine;
public:
void AddInst(BeInst* inst);