BfCompiler.h 13 KB

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