BfCompiler.h 13 KB

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