1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-07 19:18:19 +02:00
Beef/IDEHelper/Compiler/BfIRCodeGen.h

311 lines
8.2 KiB
C
Raw Normal View History

2019-08-23 11:56:54 -07:00
#pragma once
#include "BfIRBuilder.h"
#include "BfSystem.h"
#include "BfTargetTriple.h"
2019-08-23 11:56:54 -07:00
2020-09-14 06:52:19 -07:00
namespace llvm
{
class Constant;
class Value;
class Type;
class BasicBlock;
class Function;
class FunctionType;
class MDNode;
class InlineAsm;
class DIType;
class DIBuilder;
class DICompileUnit;
class AttributeList;
class Module;
class LLVMContext;
2022-07-26 13:27:03 -04:00
class TargetMachine;
2025-03-20 09:25:46 -04:00
class Triple;
2020-09-14 06:52:19 -07:00
};
2019-08-23 11:56:54 -07:00
NS_BF_BEGIN
enum BfIRCodeGenEntryKind
{
BfIRCodeGenEntryKind_None,
BfIRCodeGenEntryKind_LLVMValue,
BfIRCodeGenEntryKind_LLVMValue_Aligned,
2019-08-23 11:56:54 -07:00
BfIRCodeGenEntryKind_LLVMType,
2024-05-01 06:26:14 -04:00
BfIRCodeGenEntryKind_TypedValue,
BfIRCodeGenEntryKind_TypedValue_Aligned,
BfIRCodeGenEntryKind_TypeEx,
2019-08-23 11:56:54 -07:00
BfIRCodeGenEntryKind_LLVMBasicBlock,
BfIRCodeGenEntryKind_LLVMMetadata,
2020-02-19 13:16:33 -08:00
BfIRCodeGenEntryKind_IntrinsicData,
};
2024-05-01 06:26:14 -04:00
class BfIRTypeEx;
2020-02-19 13:16:33 -08:00
class BfIRIntrinsicData
{
public:
2020-08-23 05:42:42 -07:00
String mName;
2020-02-19 13:16:33 -08:00
BfIRIntrinsic mIntrinsic;
2024-05-01 06:26:14 -04:00
BfIRTypeEx* mReturnType;
};
class BfIRTypeEx
{
public:
llvm::Type* mLLVMType;
SizedArray<BfIRTypeEx*, 1> mMembers;
BfIRTypeEx()
{
mLLVMType = NULL;
}
};
struct BfIRTypedValue
{
llvm::Value* mValue;
BfIRTypeEx* mTypeEx;
2019-08-23 11:56:54 -07:00
};
struct BfIRCodeGenEntry
{
BfIRCodeGenEntryKind mKind;
union
{
llvm::Value* mLLVMValue;
llvm::Type* mLLVMType;
2024-05-01 06:26:14 -04:00
BfIRTypeEx* mTypeEx;
2019-08-23 11:56:54 -07:00
llvm::BasicBlock* mLLVMBlock;
2022-07-26 13:27:03 -04:00
llvm::MDNode* mLLVMMetadata;
2020-02-19 13:16:33 -08:00
BfIRIntrinsicData* mIntrinsicData;
2024-05-01 06:26:14 -04:00
BfIRTypedValue mTypedValue;
2019-08-23 11:56:54 -07:00
};
};
class BfIRTypeEntry
{
public:
int mTypeId;
2021-01-18 14:09:16 -08:00
int mSize;
int mAlign;
2019-08-23 11:56:54 -07:00
llvm::DIType* mDIType;
llvm::DIType* mInstDIType;
2024-05-01 06:26:14 -04:00
BfIRTypeEx* mType;
BfIRTypeEx* mAlignType;
BfIRTypeEx* mInstType;
2019-08-23 11:56:54 -07:00
public:
BfIRTypeEntry()
{
mTypeId = -1;
2021-01-18 14:09:16 -08:00
mSize = -1;
mAlign = -1;
2019-08-23 11:56:54 -07:00
mDIType = NULL;
mInstDIType = NULL;
2024-05-01 06:26:14 -04:00
mType = NULL;
mAlignType = NULL;
mInstType = NULL;
2019-08-23 11:56:54 -07:00
}
};
enum BfIRSizeAlignKind
{
BfIRSizeAlignKind_NoTransform,
BfIRSizeAlignKind_Original,
2022-07-26 13:27:03 -04:00
BfIRSizeAlignKind_Aligned,
};
enum BfIRSimdType
{
BfIRSimdType_None,
BfIRSimdType_SSE,
BfIRSimdType_SSE2,
BfIRSimdType_AVX,
BfIRSimdType_AVX2,
BfIRSimdType_AVX512
};
2019-08-23 11:56:54 -07:00
class BfIRCodeGen : public BfIRCodeGenBase
{
2022-07-26 13:27:03 -04:00
public:
BfIRBuilder* mBfIRBuilder;
2020-02-19 13:16:33 -08:00
BumpAllocator mAlloc;
BfTargetTriple mTargetTriple;
2022-01-25 07:04:54 -05:00
String mTargetCPU;
2019-08-23 11:56:54 -07:00
String mModuleName;
llvm::LLVMContext* mLLVMContext;
llvm::Module* mLLVMModule;
llvm::Function* mActiveFunction;
2024-05-01 06:26:14 -04:00
BfIRTypeEx* mActiveFunctionType;
2019-08-23 11:56:54 -07:00
llvm::IRBuilder<>* mIRBuilder;
llvm::AttributeList* mAttrSet;
llvm::DIBuilder* mDIBuilder;
llvm::DICompileUnit* mDICompileUnit;
2020-09-14 06:52:19 -07:00
llvm::TargetMachine* mLLVMTargetMachine;
2019-08-23 11:56:54 -07:00
Array<llvm::DebugLoc> mSavedDebugLocs;
llvm::InlineAsm* mNopInlineAsm;
2022-01-11 08:17:09 -05:00
llvm::InlineAsm* mObjectCheckAsm;
llvm::InlineAsm* mOverflowCheckAsm;
2019-08-23 11:56:54 -07:00
llvm::DebugLoc mDebugLoc;
2020-09-14 06:52:19 -07:00
BfCodeGenOptions mCodeGenOptions;
2022-07-26 13:27:03 -04:00
bool mHasDebugLoc;
2020-06-13 08:38:13 -07:00
bool mIsCodeView;
bool mHadDLLExport;
2020-08-12 11:42:15 -07:00
int mConstValIdx;
2019-08-23 11:56:54 -07:00
int mCmdCount;
2024-05-01 06:26:14 -04:00
int mCurLine;
2019-08-23 11:56:54 -07:00
Dictionary<int, BfIRCodeGenEntry> mResults;
Dictionary<int, BfIRTypeEntry> mTypes;
Dictionary<int, llvm::Function*> mIntrinsicMap;
2024-05-01 06:26:14 -04:00
Dictionary<BfTypeCode, BfIRTypeEx*> mTypeCodeTypeExMap;
Dictionary<llvm::Type*, BfIRTypeEx*> mLLVMTypeExMap;
Dictionary<BfIRTypeEx*, BfIRTypeEx*> mPointerTypeExMap;
2022-07-26 13:27:03 -04:00
Dictionary<llvm::Function*, int> mIntrinsicReverseMap;
2019-08-23 11:56:54 -07:00
Array<llvm::Constant*> mConfigConsts32;
Array<llvm::Constant*> mConfigConsts64;
2024-05-01 06:26:14 -04:00
Dictionary<BfIRTypeEx*, BfIRTypedValue> mReflectDataMap;
Dictionary<BfIRTypeEx*, BfIRTypeEx*> mAlignedTypeToNormalType;
Dictionary<BfIRTypeEx*, int> mTypeToTypeIdMap;
2021-11-15 16:44:28 -08:00
HashSet<llvm::BasicBlock*> mLockedBlocks;
2022-03-18 18:06:14 -07:00
OwnedArray<BfIRIntrinsicData> mIntrinsicData;
Dictionary<llvm::Function*, BfIRSimdType> mFunctionsUsingSimd;
2024-05-01 06:26:14 -04:00
Array<BfIRTypeEx*> mIRTypeExs;
BfIRTypedValue mLastFuncCalled;
2019-08-23 11:56:54 -07:00
2022-07-26 13:27:03 -04:00
public:
2020-09-14 06:52:19 -07:00
void InitTarget();
void FixValues(llvm::StructType* structType, llvm::SmallVector<llvm::Value*, 8>& values);
void FixIndexer(llvm::Value*& val);
2024-05-01 06:26:14 -04:00
void FixTypedValue(BfIRTypedValue& typedValue);
2019-08-23 11:56:54 -07:00
BfTypeCode GetTypeCode(llvm::Type* type, bool isSigned);
llvm::Type* GetLLVMType(BfTypeCode typeCode, bool& isSigned);
2024-05-01 06:26:14 -04:00
BfIRTypeEx* GetTypeEx(llvm::Type* llvmType);
BfIRTypeEx* CreateTypeEx(llvm::Type* llvmType);
BfIRTypeEx* GetTypeEx(BfTypeCode typeCode, bool& isSigned);
BfIRTypeEx* GetPointerTypeEx(BfIRTypeEx* typeEx);
BfIRTypeEx* GetTypeMember(BfIRTypeEx* typeEx, int idx);
2019-08-23 11:56:54 -07:00
BfIRTypeEntry& GetTypeEntry(int typeId);
2024-05-01 06:26:14 -04:00
BfIRTypeEntry* GetTypeEntry(BfIRTypeEx* type);
2019-08-23 11:56:54 -07:00
void SetResult(int id, llvm::Value* value);
2024-05-01 06:26:14 -04:00
void SetResult(int id, const BfIRTypedValue& value);
void SetResultAligned(int id, llvm::Value* value);
2024-05-01 06:26:14 -04:00
void SetResultAligned(int id, const BfIRTypedValue& value);
2022-07-26 13:27:03 -04:00
void SetResult(int id, llvm::Type* value);
2024-05-01 06:26:14 -04:00
void SetResult(int id, BfIRTypeEx* typeEx);
2019-08-23 11:56:54 -07:00
void SetResult(int id, llvm::BasicBlock* value);
2025-03-20 09:25:46 -04:00
void SetResult(int id, llvm::MDNode* value);
2019-08-23 11:56:54 -07:00
void CreateMemSet(llvm::Value* addr, llvm::Value* val, llvm::Value* size, int alignment, bool isVolatile = false);
void AddNop();
2024-05-01 06:26:14 -04:00
llvm::Value* TryToVector(const BfIRTypedValue& value);
bool TryMemCpy(const BfIRTypedValue& ptr, llvm::Value* val);
bool TryVectorCpy(const BfIRTypedValue& ptr, llvm::Value* val);
llvm::Type* GetLLVMPointerElementType(BfIRTypeEx* typeEx);
BfIRTypeEx* GetSizeAlignedType(BfIRTypeEntry* typeEntry);
BfIRTypedValue GetAlignedPtr(const BfIRTypedValue& val);
2022-01-11 08:17:09 -05:00
llvm::Value* DoCheckedIntrinsic(llvm::Intrinsic::ID intrin, llvm::Value* lhs, llvm::Value* rhs, bool useAsm);
2019-08-23 11:56:54 -07:00
2024-05-05 12:26:21 -04:00
void RunOptimizationPipeline(const llvm::Triple& targetTriple);
2019-08-23 11:56:54 -07:00
public:
BfIRCodeGen();
~BfIRCodeGen();
2020-07-13 09:55:16 -07:00
void FatalError(const StringImpl& str);
2019-08-23 11:56:54 -07:00
virtual void Fail(const StringImpl& error) override;
void ProcessBfIRData(const BfSizedArray<uint8>& buffer) override;
void PrintModule();
void PrintFunction();
int64 ReadSLEB128();
2022-07-26 13:27:03 -04:00
void Read(StringImpl& str);
2019-08-23 11:56:54 -07:00
void Read(int& i);
void Read(int64& i);
2020-03-23 12:07:05 -07:00
void Read(Val128& i);
2019-08-23 11:56:54 -07:00
void Read(bool& val);
2022-01-11 08:17:09 -05:00
void Read(int8& val);
2019-08-23 11:56:54 -07:00
void Read(BfIRTypeEntry*& type);
2024-05-01 06:26:14 -04:00
void Read(BfIRTypeEx*& typeEx, BfIRTypeEntry** outTypeEntry = NULL);
2021-01-18 14:09:16 -08:00
void Read(llvm::Type*& llvmType, BfIRTypeEntry** outTypeEntry = NULL);
2019-08-23 11:56:54 -07:00
void Read(llvm::FunctionType*& llvmType);
2024-05-01 06:26:14 -04:00
void ReadFunctionType(BfIRTypeEx*& typeEx);
void Read(BfIRTypedValue& llvmValue, BfIRCodeGenEntry** codeGenEntry = NULL, BfIRSizeAlignKind sizeAlignKind = BfIRSizeAlignKind_Original);
void Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry = NULL, BfIRSizeAlignKind sizeAlignKind = BfIRSizeAlignKind_Original);
2024-05-01 06:26:14 -04:00
void Read(llvm::Constant*& llvmConstant, BfIRSizeAlignKind sizeAlignKind = BfIRSizeAlignKind_Original);
2019-08-23 11:56:54 -07:00
void Read(llvm::Function*& llvmFunc);
2024-05-01 06:26:14 -04:00
void ReadFunction(BfIRTypedValue& typeEx);
2019-08-23 11:56:54 -07:00
void Read(llvm::BasicBlock*& llvmBlock);
void Read(llvm::MDNode*& llvmMD);
void Read(llvm::Metadata*& llvmMD);
template <typename T>
void Read(llvm::SmallVectorImpl<T>& vec)
{
int len = (int)ReadSLEB128();
for (int i = 0; i < len; i++)
{
T result;
Read(result);
vec.push_back(result);
}
}
void Read(llvm::SmallVectorImpl<llvm::Value*>& vec, BfIRSizeAlignKind sizeAlignKind = BfIRSizeAlignKind_Original)
2021-01-18 14:09:16 -08:00
{
int len = (int)ReadSLEB128();
for (int i = 0; i < len; i++)
{
llvm::Value* result;
Read(result, NULL, sizeAlignKind);
2021-01-18 14:09:16 -08:00
vec.push_back(result);
}
}
void Read(llvm::SmallVectorImpl<llvm::Constant*>& vec, BfIRSizeAlignKind sizeAlignKind = BfIRSizeAlignKind_Original)
2021-01-18 14:09:16 -08:00
{
int len = (int)ReadSLEB128();
for (int i = 0; i < len; i++)
{
llvm::Constant* result;
Read(result, sizeAlignKind);
2021-01-18 14:09:16 -08:00
vec.push_back(result);
}
}
2019-08-23 11:56:54 -07:00
void HandleNextCmd() override;
2020-09-14 06:52:19 -07:00
void SetCodeGenOptions(BfCodeGenOptions codeGenOptions);
2019-08-23 11:56:54 -07:00
void SetConfigConst(int idx, int value) override;
void SetActiveFunctionSimdType(BfIRSimdType type);
String GetSimdTypeString(BfIRSimdType type);
BfIRSimdType GetSimdTypeFromFunction(llvm::Function* function);
2024-05-01 06:26:14 -04:00
BfIRTypedValue GetTypedValue(int streamId);
2019-08-23 11:56:54 -07:00
llvm::Value* GetLLVMValue(int streamId);
llvm::Type* GetLLVMType(int streamId);
llvm::BasicBlock* GetLLVMBlock(int streamId);
llvm::MDNode* GetLLVMMetadata(int streamId);
2022-07-26 13:27:03 -04:00
llvm::Type* GetLLVMTypeById(int id);
2019-08-23 11:56:54 -07:00
///
2020-09-14 06:52:19 -07:00
bool WriteObjectFile(const StringImpl& outFileName);
2019-08-23 11:56:54 -07:00
bool WriteIR(const StringImpl& outFileName, StringImpl& error);
void ApplySimdFeatures();
2022-07-26 13:27:03 -04:00
static int GetIntrinsicId(const StringImpl& name);
2019-08-23 11:56:54 -07:00
static const char* GetIntrinsicName(int intrinId);
static void SetAsmKind(BfAsmKind asmKind);
2019-10-16 13:07:37 -07:00
static void StaticInit();
2019-08-23 11:56:54 -07:00
};
NS_BF_END