BfIRCodeGen.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #include "BfIRBuilder.h"
  3. #include "BfSystem.h"
  4. #include "BfTargetTriple.h"
  5. NS_BF_BEGIN
  6. enum BfIRCodeGenEntryKind
  7. {
  8. BfIRCodeGenEntryKind_None,
  9. BfIRCodeGenEntryKind_LLVMValue,
  10. BfIRCodeGenEntryKind_LLVMType,
  11. BfIRCodeGenEntryKind_LLVMBasicBlock,
  12. BfIRCodeGenEntryKind_LLVMMetadata,
  13. BfIRCodeGenEntryKind_FakeIntrinsic,
  14. };
  15. struct BfIRCodeGenEntry
  16. {
  17. BfIRCodeGenEntryKind mKind;
  18. union
  19. {
  20. llvm::Value* mLLVMValue;
  21. llvm::Type* mLLVMType;
  22. llvm::BasicBlock* mLLVMBlock;
  23. llvm::MDNode* mLLVMMetadata;
  24. BfIRIntrinsic mIntrinsic;
  25. };
  26. };
  27. class BfIRTypeEntry
  28. {
  29. public:
  30. int mTypeId;
  31. llvm::DIType* mDIType;
  32. llvm::DIType* mInstDIType;
  33. llvm::Type* mLLVMType;
  34. llvm::Type* mInstLLVMType;
  35. public:
  36. BfIRTypeEntry()
  37. {
  38. mTypeId = -1;
  39. mDIType = NULL;
  40. mInstDIType = NULL;
  41. mLLVMType = NULL;
  42. mInstLLVMType = NULL;
  43. }
  44. };
  45. class BfIRCodeGen : public BfIRCodeGenBase
  46. {
  47. public:
  48. BfIRBuilder* mBfIRBuilder;
  49. BfTargetTriple mTargetTriple;
  50. String mModuleName;
  51. llvm::LLVMContext* mLLVMContext;
  52. llvm::Module* mLLVMModule;
  53. llvm::Function* mActiveFunction;
  54. llvm::IRBuilder<>* mIRBuilder;
  55. llvm::AttributeList* mAttrSet;
  56. llvm::DIBuilder* mDIBuilder;
  57. llvm::DICompileUnit* mDICompileUnit;
  58. Array<llvm::DebugLoc> mSavedDebugLocs;
  59. llvm::InlineAsm* mNopInlineAsm;
  60. llvm::InlineAsm* mAsmObjectCheckAsm;
  61. llvm::DebugLoc mDebugLoc;
  62. bool mHasDebugLoc;
  63. bool mIsCodeView;
  64. int mCmdCount;
  65. Dictionary<int, BfIRCodeGenEntry> mResults;
  66. Dictionary<int, BfIRTypeEntry> mTypes;
  67. Dictionary<int, llvm::Function*> mIntrinsicMap;
  68. Dictionary<llvm::Function*, int> mIntrinsicReverseMap;
  69. Array<llvm::Constant*> mConfigConsts32;
  70. Array<llvm::Constant*> mConfigConsts64;
  71. public:
  72. BfTypeCode GetTypeCode(llvm::Type* type, bool isSigned);
  73. llvm::Type* GetLLVMType(BfTypeCode typeCode, bool& isSigned);
  74. BfIRTypeEntry& GetTypeEntry(int typeId);
  75. void SetResult(int id, llvm::Value* value);
  76. void SetResult(int id, llvm::Type* value);
  77. void SetResult(int id, llvm::BasicBlock* value);
  78. void SetResult(int id, llvm::MDNode* value);
  79. void CreateMemSet(llvm::Value* addr, llvm::Value* val, llvm::Value* size, int alignment, bool isVolatile = false);
  80. void AddNop();
  81. public:
  82. BfIRCodeGen();
  83. ~BfIRCodeGen();
  84. virtual void Fail(const StringImpl& error) override;
  85. void ProcessBfIRData(const BfSizedArray<uint8>& buffer) override;
  86. void PrintModule();
  87. void PrintFunction();
  88. int64 ReadSLEB128();
  89. void Read(StringImpl& str);
  90. void Read(int& i);
  91. void Read(int64& i);
  92. void Read(bool& val);
  93. void Read(BfIRTypeEntry*& type);
  94. void Read(llvm::Type*& llvmType);
  95. void Read(llvm::FunctionType*& llvmType);
  96. void Read(llvm::Value*& llvmValue, BfIRCodeGenEntry** codeGenEntry = NULL);
  97. void Read(llvm::Constant*& llvmConstant);
  98. void Read(llvm::Function*& llvmFunc);
  99. void Read(llvm::BasicBlock*& llvmBlock);
  100. void Read(llvm::MDNode*& llvmMD);
  101. void Read(llvm::Metadata*& llvmMD);
  102. template <typename T>
  103. void Read(llvm::SmallVectorImpl<T>& vec)
  104. {
  105. int len = (int)ReadSLEB128();
  106. for (int i = 0; i < len; i++)
  107. {
  108. T result;
  109. Read(result);
  110. vec.push_back(result);
  111. }
  112. }
  113. void HandleNextCmd() override;
  114. void SetConfigConst(int idx, int value) override;
  115. llvm::Value* GetLLVMValue(int streamId);
  116. llvm::Type* GetLLVMType(int streamId);
  117. llvm::BasicBlock* GetLLVMBlock(int streamId);
  118. llvm::MDNode* GetLLVMMetadata(int streamId);
  119. llvm::Type* GetLLVMTypeById(int id);
  120. ///
  121. bool WriteObjectFile(const StringImpl& outFileName, const BfCodeGenOptions& codeGenOptions);
  122. bool WriteIR(const StringImpl& outFileName, StringImpl& error);
  123. static int GetIntrinsicId(const StringImpl& name);
  124. static const char* GetIntrinsicName(int intrinId);
  125. static void SetAsmKind(BfAsmKind asmKind);
  126. static void StaticInit();
  127. };
  128. NS_BF_END