BfCompiler.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #pragma once
  2. #pragma warning(push)
  3. #pragma warning(disable:4141)
  4. #pragma warning(disable:4146)
  5. #pragma warning(disable:4291)
  6. #pragma warning(disable:4244)
  7. #pragma warning(disable:4267)
  8. #pragma warning(disable:4624)
  9. #pragma warning(disable:4800)
  10. #include "BeefySysLib/Common.h"
  11. #include "BeefySysLib/util/CritSect.h"
  12. #include "BeefySysLib/util/PerfTimer.h"
  13. #include "BeefySysLib/util/String.h"
  14. #include "BfAst.h"
  15. #include "BfSystem.h"
  16. #include "llvm/Support/Compiler.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Type.h"
  19. #include "llvm/IR/DIBuilder.h"
  20. #include "llvm/IR/DebugInfo.h"
  21. #include "llvm/IR/Argument.h"
  22. #include "llvm/IR/Constants.h"
  23. #include "BfResolvedTypeUtils.h"
  24. #include <unordered_set>
  25. #include "BfContext.h"
  26. #include "BfCodeGen.h"
  27. #include "BfMangler.h"
  28. #pragma warning(pop)
  29. NS_BF_BEGIN
  30. class BfType;
  31. class BfResolvedType;
  32. class BfTypeInstance;
  33. class BfModule;
  34. class BfFileInstance;
  35. class BfAutoComplete;
  36. class BfMethodInstance;
  37. class BfSourceClassifier;
  38. class BfResolvePassData;
  39. enum BfCompileOnDemandKind
  40. {
  41. BfCompileOnDemandKind_AlwaysInclude,
  42. BfCompileOnDemandKind_ResolveUnused,
  43. BfCompileOnDemandKind_SkipUnused
  44. };
  45. class BfCompiler
  46. {
  47. public:
  48. enum CompileState
  49. {
  50. CompileState_None,
  51. CompileState_Normal,
  52. CompileState_Unreified,
  53. CompileState_VData
  54. };
  55. struct Stats
  56. {
  57. int mTotalTypes;
  58. int mMethodDeclarations;
  59. int mTypesPopulated;
  60. int mMethodsProcessed;
  61. int mUnreifiedMethodsProcessed;
  62. int mQueuedTypesProcessed;
  63. int mTypesQueued;
  64. int mTypesDeleted;
  65. int mMethodsQueued;
  66. int mModulesStarted;
  67. int mModulesFinished;
  68. int mModulesReified;
  69. int mModulesUnreified;
  70. int mReifiedModuleCount;
  71. int mIRBytes;
  72. int mConstBytes;
  73. };
  74. Stats mStats;
  75. struct Options
  76. {
  77. BfProject* mHotProject;
  78. int mHotCompileIdx;
  79. int32 mForceRebuildIdx;
  80. BfCompileOnDemandKind mCompileOnDemandKind;
  81. String mTargetTriple;
  82. BfMachineType mMachineType;
  83. BfToolsetType mToolsetType;
  84. BfSIMDSetting mSIMDSetting;
  85. int mMaxWorkerThreads;
  86. String mMallocLinkName;
  87. String mFreeLinkName;
  88. bool mIncrementalBuild;
  89. bool mEmitDebugInfo;
  90. bool mEmitLineInfo;
  91. bool mNoFramePointerElim;
  92. bool mInitLocalVariables;
  93. bool mRuntimeChecks;
  94. bool mAllowStructByVal;
  95. bool mEmitDynamicCastCheck;
  96. bool mAllowHotSwapping;
  97. bool mObjectHasDebugFlags;
  98. bool mEnableRealtimeLeakCheck;
  99. bool mEmitObjectAccessCheck; // Only valid with mObjectHasDebugFlags
  100. bool mEnableCustodian;
  101. bool mEnableSideStack;
  102. bool mHasVDataExtender;
  103. bool mDebugAlloc;
  104. bool mOmitDebugHelpers;
  105. bool mUseDebugBackingParams;
  106. bool mWriteIR;
  107. bool mGenerateObj;
  108. int mAllocStackCount;
  109. bool mExtraResolveChecks;
  110. int mMaxSplatRegs;
  111. String mErrorString;
  112. Options()
  113. {
  114. mMallocLinkName = "malloc";
  115. mFreeLinkName = "free";
  116. mHotCompileIdx = 0;
  117. mForceRebuildIdx = 0;
  118. mCompileOnDemandKind = BfCompileOnDemandKind_AlwaysInclude;
  119. mMachineType = BfMachineType_x86;
  120. mToolsetType = BfToolsetType_Microsoft;
  121. mSIMDSetting = BfSIMDSetting_None;
  122. mHotProject = NULL;
  123. mDebugAlloc = false;
  124. mOmitDebugHelpers = false;
  125. mIncrementalBuild = true;
  126. mEmitDebugInfo = false;
  127. mEmitLineInfo = false;
  128. mNoFramePointerElim = true;
  129. mInitLocalVariables = false;
  130. mRuntimeChecks = true;
  131. mAllowStructByVal = false;
  132. mEmitDynamicCastCheck = true;
  133. mAllowHotSwapping = false;
  134. mEmitObjectAccessCheck = false;
  135. mObjectHasDebugFlags = false;
  136. mEnableRealtimeLeakCheck = false;
  137. mWriteIR = false;
  138. mGenerateObj = true;
  139. mEnableCustodian = false;
  140. mEnableSideStack = false;
  141. mHasVDataExtender = false;
  142. mUseDebugBackingParams = true;
  143. mAllocStackCount = 1;
  144. mExtraResolveChecks = false;
  145. #ifdef _DEBUG
  146. //mExtraResolveChecks = true;
  147. #endif
  148. mMaxSplatRegs = 4;
  149. }
  150. bool IsCodeView()
  151. {
  152. #ifdef BF_PLATFORM_WINDOWS
  153. return mToolsetType != BfToolsetType_GNU;
  154. #else
  155. return false;
  156. #endif
  157. }
  158. };
  159. Options mOptions;
  160. enum HotTypeFlags
  161. {
  162. HotTypeFlag_None = 0,
  163. HotTypeFlag_UserNotUsed = 1,
  164. HotTypeFlag_UserUsed = 2,
  165. HotTypeFlag_Heap = 4,
  166. HotTypeFlag_ActiveFunction = 8, // Only set for a type version mismatch
  167. HotTypeFlag_Delegate = 0x10, // Only set for a type version mismatch
  168. HotTypeFlag_FuncPtr = 0x20, // Only set for a type version mismatch
  169. HotTypeFlag_CanAllocate = 0x40
  170. };
  171. enum HotResolveFlags
  172. {
  173. HotResolveFlag_None = 0,
  174. HotResolveFlag_HadDataChanges = 1
  175. };
  176. struct HotReachableData
  177. {
  178. HotTypeFlags mTypeFlags;
  179. bool mHadNonDevirtualizedCall;
  180. HotReachableData()
  181. {
  182. mTypeFlags = HotTypeFlag_None;
  183. mHadNonDevirtualizedCall = false;
  184. }
  185. };
  186. class HotResolveData
  187. {
  188. public:
  189. HotResolveFlags mFlags;
  190. Dictionary<BfHotMethod*, HotReachableData> mReachableMethods;
  191. HashSet<BfHotMethod*> mActiveMethods;
  192. Dictionary<BfHotTypeVersion*, HotTypeFlags> mHotTypeFlags;
  193. Array<HotTypeFlags> mHotTypeIdFlags;
  194. Array<BfHotDepData*> mReasons;
  195. HashSet<BfHotMethod*> mDeferredThisCheckMethods;
  196. ~HotResolveData();
  197. };
  198. class HotData
  199. {
  200. public:
  201. BfCompiler* mCompiler;
  202. Dictionary<String, BfHotMethod*> mMethodMap;
  203. Dictionary<BfHotTypeVersion*, BfHotThisType*> mThisType;
  204. Dictionary<BfHotTypeVersion*, BfHotAllocation*> mAllocation;
  205. Dictionary<BfHotMethod*, BfHotDevirtualizedMethod*> mDevirtualizedMethods;
  206. Dictionary<BfHotMethod*, BfHotFunctionReference*> mFuncPtrs;
  207. Dictionary<BfHotMethod*, BfHotVirtualDeclaration*> mVirtualDecls;
  208. Dictionary<BfHotMethod*, BfHotInnerMethod*> mInnerMethods;
  209. public:
  210. ~HotData();
  211. void ClearUnused(bool isHotCompile);
  212. BfHotThisType* GetThisType(BfHotTypeVersion* hotVersion);
  213. BfHotAllocation* GetAllocation(BfHotTypeVersion* hotVersion);
  214. BfHotDevirtualizedMethod* GetDevirtualizedMethod(BfHotMethod* hotMethod);
  215. BfHotFunctionReference* GetFunctionReference(BfHotMethod* hotMethod);
  216. BfHotVirtualDeclaration* GetVirtualDeclaration(BfHotMethod* hotMethod);
  217. BfHotInnerMethod* GetInnerMethod(BfHotMethod* hotMethod);
  218. };
  219. class HotState
  220. {
  221. public:
  222. BfProject* mHotProject;
  223. int mLastStringId;
  224. int mCommittedHotCompileIdx;
  225. bool mHasNewTypes;
  226. bool mHasNewInterfaceTypes;
  227. Array<BfCodeGenFileEntry> mQueuedOutFiles; // Queues up when we have failed hot compiles
  228. HashSet<int> mSlotDefineTypeIds;
  229. HashSet<int> mNewlySlottedTypeIds;
  230. HashSet<int> mPendingDataChanges;
  231. HashSet<int> mPendingFailedSlottings;
  232. Dictionary<String, int> mDeletedTypeNameMap;
  233. Val128 mVDataHashEx;
  234. public:
  235. HotState()
  236. {
  237. mHotProject = NULL;
  238. mLastStringId = -1;
  239. mCommittedHotCompileIdx = 0;
  240. mHasNewTypes = false;
  241. mHasNewInterfaceTypes = false;
  242. }
  243. ~HotState();
  244. bool HasPendingChanges(BfTypeInstance* type);
  245. void RemovePendingChanges(BfTypeInstance* type);
  246. };
  247. HotData* mHotData;
  248. HotState* mHotState;
  249. HotResolveData* mHotResolveData;
  250. struct StringValueEntry
  251. {
  252. int mId;
  253. BfIRValue mStringVal;
  254. };
  255. struct TestMethod
  256. {
  257. String mName;
  258. BfMethodInstance* mMethodInstance;
  259. };
  260. public:
  261. BfPassInstance* mPassInstance;
  262. FILE* mCompileLogFP;
  263. BfSystem* mSystem;
  264. bool mIsResolveOnly;
  265. BfResolvePassData* mResolvePassData;
  266. Dictionary<String, Array<int>> mAttributeTypeOptionMap;
  267. int mRevision;
  268. bool mLastRevisionAborted;
  269. BfContext* mContext;
  270. BfCodeGen mCodeGen;
  271. String mOutputDirectory;
  272. bool mCanceling;
  273. bool mHadCancel;
  274. bool mWantsDeferMethodDecls;
  275. bool mInInvalidState;
  276. float mCompletionPct;
  277. int mHSPreserveIdx;
  278. BfModule* mLastAutocompleteModule;
  279. CompileState mCompileState;
  280. Array<BfVDataModule*> mVDataModules;
  281. BfTypeDef* mArray1TypeDef;
  282. BfTypeDef* mArray2TypeDef;
  283. BfTypeDef* mArray3TypeDef;
  284. BfTypeDef* mArray4TypeDef;
  285. BfTypeDef* mSpanTypeDef;
  286. BfTypeDef* mBfObjectTypeDef;
  287. BfTypeDef* mClassVDataTypeDef;
  288. BfTypeDef* mDbgRawAllocDataTypeDef;
  289. BfTypeDef* mDeferredCallTypeDef;
  290. BfTypeDef* mDelegateTypeDef;
  291. BfTypeDef* mEnumTypeDef;
  292. BfTypeDef* mStringTypeDef;
  293. BfTypeDef* mTypeTypeDef;
  294. BfTypeDef* mValueTypeTypeDef;
  295. BfTypeDef* mFunctionTypeDef;
  296. BfTypeDef* mGCTypeDef;
  297. BfTypeDef* mGenericIEnumerableTypeDef;
  298. BfTypeDef* mGenericIEnumeratorTypeDef;
  299. BfTypeDef* mGenericIRefEnumeratorTypeDef;
  300. BfTypeDef* mInternalTypeDef;
  301. BfTypeDef* mIPrintableTypeDef;
  302. BfTypeDef* mIHashableTypeDef;
  303. BfTypeDef* mMethodRefTypeDef;
  304. BfTypeDef* mNullableTypeDef;
  305. BfTypeDef* mPointerTTypeDef;
  306. BfTypeDef* mPointerTypeDef;
  307. BfTypeDef* mReflectArrayType;
  308. BfTypeDef* mReflectFieldDataDef;
  309. BfTypeDef* mReflectFieldSplatDataDef;
  310. BfTypeDef* mReflectMethodDataDef;
  311. BfTypeDef* mReflectParamDataDef;
  312. BfTypeDef* mReflectPointerType;
  313. BfTypeDef* mReflectSizedArrayType;
  314. BfTypeDef* mReflectSpecializedGenericType;
  315. BfTypeDef* mReflectTypeInstanceTypeDef;
  316. BfTypeDef* mReflectUnspecializedGenericType;
  317. BfTypeDef* mSizedArrayTypeDef;
  318. BfTypeDef* mAttributeTypeDef;
  319. BfTypeDef* mAttributeUsageAttributeTypeDef;
  320. BfTypeDef* mLinkNameAttributeTypeDef;
  321. BfTypeDef* mOrderedAttributeTypeDef;
  322. BfTypeDef* mInlineAttributeTypeDef;
  323. BfTypeDef* mCLinkAttributeTypeDef;
  324. BfTypeDef* mCReprAttributeTypeDef;
  325. BfTypeDef* mNoDiscardAttributeTypeDef;
  326. BfTypeDef* mDisableObjectAccessChecksAttributeTypeDef;
  327. BfTypeDef* mFriendAttributeTypeDef;
  328. BfTypeDef* mCheckedAttributeTypeDef;
  329. BfTypeDef* mUncheckedAttributeTypeDef;
  330. BfTypeDef* mSkipAccessCheckAttributeTypeDef;
  331. BfTypeDef* mStaticInitAfterAttributeTypeDef;
  332. BfTypeDef* mStaticInitPriorityAttributeTypeDef;
  333. BfTypeDef* mTestAttributeTypeDef;
  334. BfTypeDef* mThreadStaticAttributeTypeDef;
  335. BfTypeDef* mUnboundAttributeTypeDef;
  336. BfTypeDef* mObsoleteAttributeTypeDef;
  337. BfTypeDef* mErrorAttributeTypeDef;
  338. BfTypeDef* mWarnAttributeTypeDef;
  339. int mCurTypeId;
  340. int mTypeInitCount;
  341. String mOutputPath;
  342. Array<BfType*> mGenericInstancePurgatory;
  343. Array<int> mTypeIdFreeList;
  344. int mMaxInterfaceSlots;
  345. bool mInterfaceSlotCountChanged;
  346. public:
  347. bool IsTypeAccessible(BfType* checkType, BfProject* curProject);
  348. bool IsTypeUsed(BfType* checkType, BfProject* curProject);
  349. bool IsModuleAccessible(BfModule* module, BfProject* curProject);
  350. void FixVDataHash(BfModule* bfModule);
  351. void CheckModuleStringRefs(BfModule* module, BfVDataModule* vdataModule, int lastModuleRevision, HashSet<int>& foundStringIds, HashSet<int>& dllNameSet, Array<BfMethodInstance*>& dllMethods, Array<BfCompiler::StringValueEntry>& stringValueEntries);
  352. void HashModuleVData(BfModule* module, HashContext& hash);
  353. BfIRFunction CreateLoadSharedLibraries(BfVDataModule* bfModule, Array<BfMethodInstance*>& dllMethods);
  354. void GetTestMethods(BfVDataModule* bfModule, Array<TestMethod>& testMethods, HashContext& vdataHashCtx);
  355. void EmitTestMethod(BfVDataModule* bfModule, Array<TestMethod>& testMethods, BfIRValue& retValue);
  356. void CreateVData(BfVDataModule* bfModule);
  357. void UpdateDependencyMap(bool deleteUnusued, bool& didWork);
  358. void ProcessPurgatory(bool reifiedOnly);
  359. bool VerifySlotNums();
  360. bool QuickGenerateSlotNums();
  361. bool SlowGenerateSlotNums();
  362. void GenerateSlotNums();
  363. void GenerateDynCastData();
  364. void UpdateRevisedTypes();
  365. void VisitAutocompleteExteriorIdentifiers();
  366. void VisitSourceExteriorNodes();
  367. void UpdateCompletion();
  368. bool DoWorkLoop(bool onlyReifiedTypes = false, bool onlyReifiedMethods = false);
  369. BfMangler::MangleKind GetMangleKind();
  370. BfTypeDef* GetArrayTypeDef(int dimensions);
  371. void GenerateAutocompleteInfo();
  372. void MarkStringPool(BfModule* module);
  373. void ClearUnusedStringPoolEntries();
  374. void ClearBuildCache();
  375. int GetDynCastVDataCount();
  376. bool IsAutocomplete();
  377. BfAutoComplete* GetAutoComplete();
  378. bool IsHotCompile();
  379. bool IsSkippingExtraResolveChecks();
  380. int GetVTableMethodOffset();
  381. BfType* CheckSymbolReferenceTypeRef(BfModule* module, BfTypeReference* typeRef);
  382. void AddDepsToRebuildTypeList(BfTypeInstance* typeInst, HashSet<BfTypeInstance*>& rebuildTypeInstList);
  383. void CompileReified();
  384. void PopulateReified();
  385. void HotCommit();
  386. void HotResolve_Start(HotResolveFlags flags);
  387. bool HotResolve_AddReachableMethod(BfHotMethod* hotMethod, HotTypeFlags flags, bool devirtualized, bool forceProcess = false);
  388. void HotResolve_AddReachableMethod(const StringImpl& methodName);
  389. void HotResolve_AddActiveMethod(BfHotMethod* hotMethod);
  390. void HotResolve_AddActiveMethod(const StringImpl& methodName);
  391. void HotResolve_AddDelegateMethod(const StringImpl& methodName);
  392. void HotResolve_ReportType(BfHotTypeVersion* hotTypeVersion, HotTypeFlags flags, BfHotDepData* reason);
  393. void HotResolve_ReportType(int typeId, HotTypeFlags flags);
  394. String HotResolve_Finish();
  395. void ClearOldHotData();
  396. public:
  397. BfCompiler(BfSystem* bfSystem, bool isResolveOnly);
  398. ~BfCompiler();
  399. bool Compile(const StringImpl& outputPath);
  400. bool DoCompile(const StringImpl& outputPath);
  401. void ClearResults();
  402. void ProcessAutocompleteTempType();
  403. void GetSymbolReferences();
  404. void Cancel();
  405. String GetTypeDefList();
  406. String GetTypeDefMatches(const StringImpl& searchSrc);
  407. String GetTypeDefInfo(const StringImpl& typeName);
  408. void CompileLog(const char* fmt ...);
  409. void ReportMemory(MemReporter* memReporter);
  410. };
  411. NS_BF_END