BfCompiler.h 14 KB

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