BeIRCodeGen.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include "../Beef/BfCommon.h"
  3. #include "../Compiler/BfIRBuilder.h"
  4. #include "BeContext.h"
  5. #include "BeModule.h"
  6. NS_BF_BEGIN
  7. class BeModule;
  8. class BeDbgType;
  9. class BfIRBuilder;
  10. class ChunkedDataBuffer;
  11. enum BeIRCodeGenEntryKind
  12. {
  13. BeIRCodeGenEntryKind_None,
  14. BeIRCodeGenEntryKind_Value,
  15. BeIRCodeGenEntryKind_Type,
  16. BeIRCodeGenEntryKind_Block,
  17. BeIRCodeGenEntryKind_Metadata
  18. };
  19. struct BeIRCodeGenEntry
  20. {
  21. BeIRCodeGenEntryKind mKind;
  22. union
  23. {
  24. BeValue* mBeValue;
  25. BeType* mBeType;
  26. BeBlock* mBeBlock;
  27. BeMDNode* mBeMetadata;
  28. };
  29. };
  30. class BeIRTypeEntry
  31. {
  32. public:
  33. int mTypeId;
  34. BeDbgType* mDIType;
  35. BeDbgType* mInstDIType;
  36. BeType* mBeType;
  37. BeType* mInstBeType;
  38. public:
  39. BeIRTypeEntry()
  40. {
  41. mTypeId = -1;
  42. mDIType = NULL;
  43. mInstDIType = NULL;
  44. mBeType = NULL;
  45. mInstBeType = NULL;
  46. }
  47. };
  48. class BeState
  49. {
  50. public:
  51. BeFunction* mActiveFunction;
  52. Array<BeDbgLoc*> mSavedDebugLocs;
  53. bool mHasDebugLoc;
  54. BeBlock* mActiveBlock;
  55. int mInsertPos;
  56. BeDbgLoc* mCurDbgLoc;
  57. BeDbgLoc* mPrevDbgLocInline;
  58. BeDbgLoc* mLastDbgLoc;
  59. };
  60. template <typename T>
  61. class CmdParamVec : public SizedArray<T, 8>
  62. {};
  63. class BeIRCodeGen : public BfIRCodeGenBase
  64. {
  65. public:
  66. bool mDebugging;
  67. BfIRBuilder* mBfIRBuilder;
  68. BeFunction* mActiveFunction;
  69. BeContext* mBeContext;
  70. BeModule* mBeModule;
  71. Array<BeDbgLoc*> mSavedDebugLocs;
  72. bool mHasDebugLoc;
  73. int mCmdCount;
  74. Dictionary<int, BeIRCodeGenEntry> mResults;
  75. Dictionary<int, BeIRTypeEntry> mTypes;
  76. Dictionary<BeType*, BeDbgType*> mOnDemandTypeMap;
  77. Dictionary<BeType*, BeValue*> mReflectDataMap;
  78. Array<int> mConfigConsts;
  79. public:
  80. void FatalError(const StringImpl& str);
  81. void NotImpl();
  82. BfTypeCode GetTypeCode(BeType* type, bool isSigned);
  83. void SetResult(int id, BeValue* value);
  84. void SetResult(int id, BeType* type);
  85. void SetResult(int id, BeBlock* value);
  86. void SetResult(int id, BeMDNode* md);
  87. BeType* GetBeType(BfTypeCode typeCode, bool& isSigned);
  88. BeIRTypeEntry& GetTypeEntry(int typeId);
  89. void FixValues(BeStructType* structType, CmdParamVec<BeValue*>& values);
  90. public:
  91. BeIRCodeGen();
  92. ~BeIRCodeGen();
  93. void Hash(BeHashContext& hashCtx);
  94. bool IsModuleEmpty();
  95. int64 ReadSLEB128();
  96. void Read(StringImpl& str);
  97. void Read(int& i);
  98. void Read(int64& i);
  99. void Read(Val128& i);
  100. void Read(bool& val);
  101. void Read(int8& val);
  102. void Read(BeIRTypeEntry*& type);
  103. void Read(BeType*& beType);
  104. void Read(BeFunctionType*& beType);
  105. void Read(BeValue*& beValue);
  106. void Read(BeConstant*& beConstant);
  107. void Read(BeFunction*& beFunc);
  108. void Read(BeBlock*& beBlock);
  109. void Read(BeMDNode*& beMD);
  110. template <typename T>
  111. void Read(SizedArrayImpl<T>& vec)
  112. {
  113. int len = (int)ReadSLEB128();
  114. for (int i = 0; i < len; i++)
  115. {
  116. T result;
  117. Read(result);
  118. vec.push_back(result);
  119. }
  120. }
  121. void Init(const BfSizedArray<uint8>& buffer);
  122. void Process();
  123. virtual void ProcessBfIRData(const BfSizedArray<uint8>& buffer) override;
  124. virtual void HandleNextCmd() override;
  125. virtual void SetConfigConst(int idx, int value) override;
  126. BeValue* GetBeValue(int streamId);
  127. BeType* GetBeType(int streamId);
  128. BeBlock* GetBeBlock(int streamId);
  129. BeMDNode* GetBeMetadata(int streamId);
  130. BeType* GetBeTypeById(int id);
  131. BeState GetState();
  132. void SetState(const BeState& state);
  133. };
  134. NS_BF_END