BeModule.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. #pragma once
  2. #include "BeefySysLib/Common.h"
  3. #include "BeefySysLib/util/HashSet.h"
  4. #include "../BumpList.h"
  5. #include "../Compiler/BfIRBuilder.h"
  6. #include "BeContext.h"
  7. #include "../X64.h"
  8. NS_BF_BEGIN
  9. class BeContext;
  10. class BeValue;
  11. class BeCallInst;
  12. class BeValue;
  13. class BeBlock;
  14. class BeArgument;
  15. class BeInst;
  16. class BeNopInst;
  17. class BeUnreachableInst;
  18. class BeEnsureInstructionAtInst;
  19. class BeUndefValueInst;
  20. class BeExtractValueInst;
  21. class BeInsertValueInst;
  22. class BeNumericCastInst;
  23. class BeBitCastInst;
  24. class BeNegInst;
  25. class BeNotInst;
  26. class BeBinaryOpInst;
  27. class BeCmpInst;
  28. class BeObjectAccessCheckInst;
  29. class BeAllocaInst;
  30. class BeLifetimeExtendInst;
  31. class BeAliasValueInst;
  32. class BeLifetimeStartInst;
  33. class BeLifetimeEndInst;
  34. class BeLifetimeSoftEndInst;
  35. class BeLifetimeFenceInst;
  36. class BeValueScopeStartInst;
  37. class BeValueScopeRetainInst;
  38. class BeValueScopeEndInst;
  39. class BeLoadInst;
  40. class BeStoreInst;
  41. class BeSetCanMergeInst;
  42. class BeMemSetInst;
  43. class BeFenceInst;
  44. class BeStackSaveInst;
  45. class BeStackRestoreInst;
  46. class BeGEPInst;
  47. class BeBrInst;
  48. class BeCondBrInst;
  49. class BePhiIncoming;
  50. class BePhiInst;
  51. class BeSwitchInst;
  52. class BeRetInst;
  53. class BeCallInst;
  54. class CeMachine;
  55. class BeDbgVariable;
  56. class BeDbgDeclareInst;
  57. class BeValueVisitor
  58. {
  59. public:
  60. void VisitChild(BeValue* value);
  61. virtual void Visit(BeValue* beValue) {}
  62. virtual void Visit(BeBlock* beBlock) {}
  63. virtual void Visit(BeArgument* beArgument) {}
  64. virtual void Visit(BeInst* beInst) {}
  65. virtual void Visit(BeNopInst* nopInst) {}
  66. virtual void Visit(BeUnreachableInst* unreachableInst) {}
  67. virtual void Visit(BeEnsureInstructionAtInst* ensureCodeAtInst) {}
  68. virtual void Visit(BeUndefValueInst* undefValue) {}
  69. virtual void Visit(BeExtractValueInst* extractValue) {}
  70. virtual void Visit(BeInsertValueInst* insertValue) {}
  71. virtual void Visit(BeNumericCastInst* castInst) {}
  72. virtual void Visit(BeBitCastInst* castInst) {}
  73. virtual void Visit(BeNegInst* negInst) {}
  74. virtual void Visit(BeNotInst* notInst) {}
  75. virtual void Visit(BeBinaryOpInst* binaryOpInst) {}
  76. virtual void Visit(BeFenceInst* fenceInst) {}
  77. virtual void Visit(BeStackSaveInst* stackSaveInst) {}
  78. virtual void Visit(BeStackRestoreInst* stackRestoreInst) {}
  79. virtual void Visit(BeCmpInst* cmpInst) {}
  80. virtual void Visit(BeObjectAccessCheckInst* objectAccessCheckInst) {}
  81. virtual void Visit(BeAllocaInst* allocaInst) {}
  82. virtual void Visit(BeAliasValueInst* aliasValueInst) {}
  83. virtual void Visit(BeLifetimeExtendInst* lifetimeExtendInst) {}
  84. virtual void Visit(BeLifetimeStartInst* lifetimeStartInst) {}
  85. virtual void Visit(BeLifetimeEndInst* lifetimeEndInst) {}
  86. virtual void Visit(BeLifetimeSoftEndInst* lifetimeEndInst) {}
  87. virtual void Visit(BeLifetimeFenceInst* lifetimeFenceInst) {}
  88. virtual void Visit(BeValueScopeStartInst* valueScopeStartInst) {}
  89. virtual void Visit(BeValueScopeRetainInst* valueScopeRetainInst) {}
  90. virtual void Visit(BeValueScopeEndInst* valueScopeEndInst) {}
  91. virtual void Visit(BeLoadInst* allocaInst) {}
  92. virtual void Visit(BeStoreInst* storeInst) {}
  93. virtual void Visit(BeSetCanMergeInst* setCanMergeInst) {}
  94. virtual void Visit(BeMemSetInst* memSetInst) {}
  95. virtual void Visit(BeGEPInst* gepInst) {}
  96. virtual void Visit(BeBrInst* brInst) {}
  97. virtual void Visit(BeCondBrInst* condBrInst) {}
  98. virtual void Visit(BePhiIncoming* phiIncomingInst) {}
  99. virtual void Visit(BePhiInst* phiInst) {}
  100. virtual void Visit(BeSwitchInst* switchInst) {}
  101. virtual void Visit(BeRetInst* retInst) {}
  102. virtual void Visit(BeCallInst* callInst) {}
  103. //virtual void Visit(BeDbgVariable* dbgVariable) {}
  104. virtual void Visit(BeDbgDeclareInst* dbgDeclareInst) {}
  105. };
  106. class BeModule;
  107. class BeFunction;
  108. class BeDbgLoc;
  109. class BeInliner : public BeValueVisitor
  110. {
  111. public:
  112. BumpAllocator* mAlloc;
  113. OwnedVector<BeValue>* mOwnedValueVec;
  114. Dictionary<BeValue*, BeValue*> mValueMap;
  115. Dictionary<BeDbgLoc*, BeDbgLoc*> mInlinedAtMap;
  116. BeModule* mModule;
  117. BeFunction* mSrcFunc;
  118. BeFunction* mDestFunc;
  119. BeCallInst* mCallInst;
  120. BeBlock* mDestBlock;
  121. BeDbgLoc* mSrcDbgLoc;
  122. BeDbgLoc* mDestDbgLoc;
  123. public:
  124. BeInliner()
  125. {
  126. mSrcDbgLoc = NULL;
  127. mDestDbgLoc = NULL;
  128. }
  129. BeValue* Remap(BeValue* srcValue);
  130. BeDbgLoc* ExtendInlineDbgLoc(BeDbgLoc* srcInlineAt);
  131. void AddInst(BeInst* destInst, BeInst* srcInst);
  132. template <typename T>
  133. T* AllocInst(T* srcInst)
  134. {
  135. auto inst = mAlloc->Alloc<T>();
  136. AddInst(inst, srcInst);
  137. return inst;
  138. }
  139. template <typename T>
  140. T* AllocInstOwned(T* srcInst)
  141. {
  142. auto inst = mOwnedValueVec->Alloc<T>();
  143. AddInst(inst, srcInst);
  144. return inst;
  145. }
  146. virtual void Visit(BeValue* beValue) override;
  147. virtual void Visit(BeBlock* beBlock) override;
  148. virtual void Visit(BeArgument* beArgument) override;
  149. virtual void Visit(BeInst* beInst) override;
  150. virtual void Visit(BeNopInst* nopInst) override;
  151. virtual void Visit(BeUnreachableInst* unreachableInst) override;
  152. virtual void Visit(BeEnsureInstructionAtInst* ensureCodeAtInst) override;
  153. virtual void Visit(BeUndefValueInst* undefValue) override;
  154. virtual void Visit(BeExtractValueInst* extractValue) override;
  155. virtual void Visit(BeInsertValueInst* insertValue) override;
  156. virtual void Visit(BeNumericCastInst* castInst) override;
  157. virtual void Visit(BeBitCastInst* castInst) override;
  158. virtual void Visit(BeNegInst* negInst) override;
  159. virtual void Visit(BeNotInst* notInst) override;
  160. virtual void Visit(BeBinaryOpInst* binaryOpInst) override;
  161. virtual void Visit(BeCmpInst* cmpInst) override;
  162. virtual void Visit(BeFenceInst* fenceInst) override;
  163. virtual void Visit(BeStackSaveInst* stackSaveInst) override;
  164. virtual void Visit(BeStackRestoreInst* stackRestoreInst) override;
  165. virtual void Visit(BeObjectAccessCheckInst* objectAccessCheckInst) override;
  166. virtual void Visit(BeAllocaInst* allocaInst) override;
  167. virtual void Visit(BeAliasValueInst* aliasValueInst) override;
  168. virtual void Visit(BeLifetimeStartInst* lifetimeStartInst) override;
  169. virtual void Visit(BeLifetimeExtendInst* lifetimeExtendInst) override;
  170. virtual void Visit(BeLifetimeEndInst* lifetimeEndInst) override;
  171. virtual void Visit(BeLifetimeSoftEndInst* lifetimeEndInst) override;
  172. virtual void Visit(BeLifetimeFenceInst* lifetimeFenceInst) override;
  173. virtual void Visit(BeValueScopeStartInst* valueScopeStartInst) override;
  174. virtual void Visit(BeValueScopeRetainInst* valueScopeRetainInst) override;
  175. virtual void Visit(BeValueScopeEndInst* valueScopeEndInst) override;
  176. virtual void Visit(BeLoadInst* allocaInst) override;
  177. virtual void Visit(BeStoreInst* storeInst) override;
  178. virtual void Visit(BeSetCanMergeInst* setCanMergeInst) override;
  179. virtual void Visit(BeMemSetInst* memSetInst) override;
  180. virtual void Visit(BeGEPInst* gepInst) override;
  181. virtual void Visit(BeBrInst* brInst) override;
  182. virtual void Visit(BeCondBrInst* condBrInst) override;
  183. virtual void Visit(BePhiIncoming* phiIncomingInst) override;
  184. virtual void Visit(BePhiInst* phiInst) override;
  185. virtual void Visit(BeSwitchInst* switchInst) override;
  186. virtual void Visit(BeRetInst* retInst) override;
  187. virtual void Visit(BeCallInst* callInst) override;
  188. virtual void Visit(BeDbgDeclareInst* dbgDeclareInst) override;
  189. };
  190. class BeValue : public BeHashble
  191. {
  192. public:
  193. int mRefCount;
  194. #ifdef _DEBUG
  195. bool mLifetimeEnded;
  196. bool mWasRemoved;
  197. BeValue()
  198. {
  199. mLifetimeEnded = false;
  200. mWasRemoved = false;
  201. mRefCount = 0;
  202. }
  203. #else
  204. BeValue()
  205. {
  206. mRefCount = 0;
  207. }
  208. #endif
  209. virtual ~BeValue()
  210. {
  211. }
  212. static const int TypeId = 0;
  213. virtual void Accept(BeValueVisitor* beVisitor) = 0;
  214. virtual bool TypeIdIsA(int typeId) = 0;
  215. virtual BeValue* DynCast(int typeId)
  216. {
  217. if (TypeIdIsA(typeId))
  218. return this;
  219. return NULL;
  220. }
  221. static bool ClassIsA(int typeId)
  222. {
  223. return typeId == 0;
  224. }
  225. virtual int GetTypeId() { return TypeId; }
  226. public:
  227. virtual BeType* GetType()
  228. {
  229. return NULL;
  230. }
  231. virtual void SetName(const StringImpl& name)
  232. {
  233. }
  234. };
  235. #define BE_VALUE_TYPE(name, TBase) static const int TypeId = __LINE__; \
  236. virtual void Accept(BeValueVisitor* beVisitor) override { beVisitor->Visit(this); } \
  237. static bool ClassIsA(int typeId) { return (typeId == TypeId) || TBase::ClassIsA(typeId); } \
  238. virtual bool TypeIdIsA(int typeId) override { return ClassIsA(typeId); } \
  239. virtual int GetTypeId() override { return TypeId; } \
  240. TBase* ToBase() { return (TBase*)this; }
  241. template <typename T>
  242. T* BeValueDynCast(BeValue* value)
  243. {
  244. if (value == NULL)
  245. return NULL;
  246. BeValue* result = value->DynCast(T::TypeId);
  247. return (T*)result;
  248. }
  249. template <typename T>
  250. T* BeValueDynCastExact(BeValue* value)
  251. {
  252. if (value == NULL)
  253. return NULL;
  254. if (value->GetTypeId() != T::TypeId)
  255. return NULL;
  256. return (T*)value;
  257. }
  258. class BeBlock;
  259. class BeInst;
  260. class BeModule;
  261. class BeDbgLoc;
  262. class BeMDNode;
  263. class BeGlobalVariable;
  264. class BeConstant;
  265. struct BeConstData
  266. {
  267. struct ConstantEntry
  268. {
  269. int mIdx;
  270. BeConstant* mConstant;
  271. };
  272. Array<uint8> mData;
  273. Array<ConstantEntry> mConsts;
  274. };
  275. class BeConstant : public BeValue
  276. {
  277. public:
  278. BE_VALUE_TYPE(BeConstant, BeValue);
  279. BeType* mType;
  280. union
  281. {
  282. bool mBool;
  283. int64 mInt64;
  284. int32 mInt32;
  285. int16 mInt16;
  286. int8 mInt8;
  287. uint64 mUInt64;
  288. uint32 mUInt32;
  289. uint16 mUInt16;
  290. uint8 mUInt8;
  291. uint8 mChar;
  292. uint32 mChar32;
  293. double mDouble;
  294. //BeType* mTypeParam;
  295. //BeGlobalVariable* mGlobalVar;
  296. BeConstant* mTarget;
  297. };
  298. bool IsNull()
  299. {
  300. if (mType->mTypeCode == BeTypeCode_NullPtr)
  301. return true;
  302. return false;
  303. }
  304. virtual BeType* GetType();
  305. virtual void GetData(BeConstData& data);
  306. virtual void HashContent(BeHashContext& hashCtx) override;
  307. };
  308. class BeCastConstant : public BeConstant
  309. {
  310. public:
  311. BE_VALUE_TYPE(BeCastConstant, BeConstant);
  312. virtual void HashContent(BeHashContext& hashCtx) override
  313. {
  314. hashCtx.Mixin(TypeId);
  315. mType->HashReference(hashCtx);
  316. mTarget->HashReference(hashCtx);
  317. }
  318. virtual void GetData(BeConstData& data) override
  319. {
  320. mTarget->GetData(data);
  321. }
  322. };
  323. class BeGEP1Constant : public BeConstant
  324. {
  325. public:
  326. BE_VALUE_TYPE(BeGEP1Constant, BeConstant);
  327. int mIdx0;
  328. virtual BeType* GetType();
  329. virtual void HashContent(BeHashContext& hashCtx) override
  330. {
  331. hashCtx.Mixin(TypeId);
  332. mTarget->HashReference(hashCtx);
  333. hashCtx.Mixin(mIdx0);
  334. }
  335. };
  336. class BeGEP2Constant : public BeConstant
  337. {
  338. public:
  339. BE_VALUE_TYPE(BeGEP2Constant, BeConstant);
  340. int mIdx0;
  341. int mIdx1;
  342. virtual BeType* GetType();
  343. virtual void HashContent(BeHashContext& hashCtx) override
  344. {
  345. hashCtx.Mixin(TypeId);
  346. mTarget->HashReference(hashCtx);
  347. hashCtx.Mixin(mIdx0);
  348. hashCtx.Mixin(mIdx1);
  349. }
  350. };
  351. class BeExtractValueConstant : public BeConstant
  352. {
  353. public:
  354. BE_VALUE_TYPE(BeExtractValueConstant, BeConstant);
  355. int mIdx0;
  356. virtual BeType* GetType();
  357. virtual void HashContent(BeHashContext& hashCtx) override
  358. {
  359. hashCtx.Mixin(TypeId);
  360. mTarget->HashReference(hashCtx);
  361. hashCtx.Mixin(mIdx0);
  362. }
  363. };
  364. class BeStructConstant : public BeConstant
  365. {
  366. public:
  367. BE_VALUE_TYPE(BeStructConstant, BeConstant);
  368. SizedArray<BeConstant*, 4> mMemberValues;
  369. virtual void GetData(BeConstData& data) override;
  370. virtual void HashContent(BeHashContext& hashCtx) override
  371. {
  372. hashCtx.Mixin(TypeId);
  373. hashCtx.Mixin(mType);
  374. hashCtx.Mixin(mMemberValues.size());
  375. for (auto member : mMemberValues)
  376. member->HashReference(hashCtx);
  377. }
  378. };
  379. class BeUndefConstant : public BeConstant
  380. {
  381. public:
  382. BE_VALUE_TYPE(BeUndefConstant, BeConstant);
  383. virtual void HashContent(BeHashContext& hashCtx) override
  384. {
  385. hashCtx.Mixin(mType);
  386. hashCtx.Mixin(TypeId);
  387. }
  388. };
  389. class BeTypeOfConstant : public BeConstant
  390. {
  391. public:
  392. BE_VALUE_TYPE(BeTypeOfConstant, BeConstant);
  393. int mBfTypeId;
  394. virtual void HashContent(BeHashContext& hashCtx) override
  395. {
  396. hashCtx.Mixin(TypeId);
  397. hashCtx.Mixin(mBfTypeId);
  398. }
  399. };
  400. class BeStringConstant : public BeConstant
  401. {
  402. public:
  403. BE_VALUE_TYPE(BeStringConstant, BeConstant);
  404. String mString;
  405. virtual void HashContent(BeHashContext& hashCtx) override
  406. {
  407. hashCtx.Mixin(TypeId);
  408. hashCtx.MixinStr(mString);
  409. }
  410. };
  411. class BeGlobalVariable : public BeConstant
  412. {
  413. public:
  414. BE_VALUE_TYPE(BeGlobalVariable, BeConstant);
  415. BeModule* mModule;
  416. String mName;
  417. BeConstant* mInitializer;
  418. BfIRLinkageType mLinkageType;
  419. BfIRStorageKind mStorageKind;
  420. bool mIsConstant;
  421. bool mIsTLS;
  422. int mAlign;
  423. bool mUnnamedAddr;
  424. virtual BeType* GetType();
  425. virtual void HashContent(BeHashContext& hashCtx) override
  426. {
  427. hashCtx.Mixin(TypeId);
  428. hashCtx.MixinStr(mName);
  429. if (mInitializer != NULL)
  430. mInitializer->HashReference(hashCtx);
  431. hashCtx.Mixin(mLinkageType);
  432. hashCtx.Mixin(mStorageKind);
  433. hashCtx.Mixin(mIsConstant);
  434. hashCtx.Mixin(mIsTLS);
  435. hashCtx.Mixin(mAlign);
  436. hashCtx.Mixin(mUnnamedAddr);
  437. }
  438. virtual void GetData(BeConstData& data) override
  439. {
  440. data.mConsts.Add({ (int)data.mData.size(), this });
  441. data.mData.Insert(data.mData.size(), (uint8)0, 8);
  442. }
  443. };
  444. class BeFunctionParam
  445. {
  446. public:
  447. String mName;
  448. bool mStructRet;
  449. bool mNoAlias;
  450. bool mNoCapture;
  451. bool mZExt;
  452. int mDereferenceableSize;
  453. int mByValSize;
  454. BeFunctionParam()
  455. {
  456. mStructRet = false;
  457. mNoAlias = false;
  458. mNoCapture = false;
  459. mZExt = false;
  460. mDereferenceableSize = -1;
  461. mByValSize = -1;
  462. }
  463. };
  464. class BeDbgFunction;
  465. class BeIntrinsic : public BeValue
  466. {
  467. public:
  468. BE_VALUE_TYPE(BeIntrinsic, BeValue);
  469. String mName;
  470. BfIRIntrinsic mKind;
  471. BeType* mReturnType;
  472. BeIntrinsic()
  473. {
  474. mReturnType = NULL;
  475. }
  476. virtual void HashContent(BeHashContext& hashCtx) override
  477. {
  478. hashCtx.Mixin(TypeId);
  479. hashCtx.Mixin(mKind);
  480. }
  481. };
  482. class BeFunction : public BeConstant
  483. {
  484. public:
  485. BE_VALUE_TYPE(BeFunction, BeConstant);
  486. BeModule* mModule;
  487. #ifdef _DEBUG
  488. StringT<256> mName;
  489. #else
  490. String mName;
  491. #endif
  492. BfIRLinkageType mLinkageType;
  493. bool mIsVarReturn;
  494. bool mAlwaysInline;
  495. bool mNoUnwind;
  496. bool mUWTable;
  497. bool mNoReturn;
  498. bool mDidInlinePass;
  499. bool mNoFramePointerElim;
  500. bool mIsDLLExport;
  501. bool mIsDLLImport;
  502. BfIRCallingConv mCallingConv;
  503. Array<BeBlock*> mBlocks;
  504. Array<BeFunctionParam> mParams;
  505. BeDbgFunction* mDbgFunction;
  506. BeGlobalVariable* mRemapBindVar;
  507. public:
  508. BeFunction()
  509. {
  510. mCallingConv = BfIRCallingConv_CDecl;
  511. mLinkageType = BfIRLinkageType_External;
  512. mModule = NULL;
  513. mDbgFunction = NULL;
  514. mIsVarReturn = false;
  515. mAlwaysInline = false;
  516. mDidInlinePass = false;
  517. mNoUnwind = false;
  518. mUWTable = false;
  519. mNoReturn = false;
  520. mNoFramePointerElim = false;
  521. mIsDLLExport = false;
  522. mIsDLLImport = false;
  523. mRemapBindVar = NULL;
  524. }
  525. BeFunctionType* GetFuncType()
  526. {
  527. BF_ASSERT(mType->IsPointer());
  528. return (BeFunctionType*)(((BePointerType*)mType)->mElementType);
  529. }
  530. bool IsDecl()
  531. {
  532. return mBlocks.size() == 0;
  533. }
  534. bool HasStructRet()
  535. {
  536. return (!mParams.IsEmpty()) && (mParams[0].mStructRet);
  537. }
  538. virtual void HashContent(BeHashContext& hashCtx) override;
  539. };
  540. class BeBlock : public BeValue
  541. {
  542. public:
  543. BE_VALUE_TYPE(BeBlock, BeValue);
  544. String mName;
  545. Array<BeInst*> mInstructions;
  546. BeFunction* mFunction;
  547. public:
  548. bool IsEmpty();
  549. virtual void HashContent(BeHashContext& hashCtx) override;
  550. };
  551. //////////////////////////////////////////////////////////////////////////
  552. class BeInst : public BeValue
  553. {
  554. public:
  555. BE_VALUE_TYPE(BeInst, BeValue);
  556. BeBlock* mParentBlock;
  557. const char* mName;
  558. BeDbgLoc* mDbgLoc;
  559. public:
  560. BeContext* GetContext();
  561. BeModule* GetModule();
  562. virtual bool CanBeReferenced()
  563. {
  564. return GetType() != NULL;
  565. }
  566. virtual void SetName(const StringImpl& name) override;
  567. BeInst()
  568. {
  569. mParentBlock = NULL;
  570. mName = NULL;
  571. mDbgLoc = NULL;
  572. }
  573. virtual void HashInst(BeHashContext& hashCtx) = 0;
  574. virtual void HashContent(BeHashContext& hashCtx) override;
  575. };
  576. class BeNopInst : public BeInst
  577. {
  578. public:
  579. BE_VALUE_TYPE(BeNopInst, BeInst);
  580. virtual void HashInst(BeHashContext& hashCtx) override
  581. {
  582. hashCtx.Mixin(TypeId);
  583. }
  584. };
  585. class BeUnreachableInst : public BeInst
  586. {
  587. public:
  588. BE_VALUE_TYPE(BeUnreachableInst, BeInst);
  589. virtual void HashInst(BeHashContext& hashCtx) override
  590. {
  591. hashCtx.Mixin(TypeId);
  592. }
  593. };
  594. class BeEnsureInstructionAtInst : public BeInst
  595. {
  596. public:
  597. BE_VALUE_TYPE(BeEnsureInstructionAtInst, BeInst);
  598. virtual void HashInst(BeHashContext& hashCtx) override
  599. {
  600. hashCtx.Mixin(TypeId);
  601. }
  602. };
  603. class BeUndefValueInst : public BeInst
  604. {
  605. public:
  606. BE_VALUE_TYPE(BeUndefValueInst, BeInst);
  607. BeType* mType;
  608. virtual BeType* GetType() override;
  609. virtual void HashInst(BeHashContext& hashCtx) override
  610. {
  611. hashCtx.Mixin(TypeId);
  612. mType->HashReference(hashCtx);
  613. }
  614. };
  615. class BeExtractValueInst : public BeInst
  616. {
  617. public:
  618. BE_VALUE_TYPE(BeExtractValueInst, BeInst);
  619. BeValue* mAggVal;
  620. int mIdx;
  621. virtual BeType* GetType() override;
  622. virtual void HashInst(BeHashContext& hashCtx) override
  623. {
  624. hashCtx.Mixin(TypeId);
  625. mAggVal->HashReference(hashCtx);
  626. hashCtx.Mixin(mIdx);
  627. }
  628. };
  629. class BeInsertValueInst : public BeInst
  630. {
  631. public:
  632. BE_VALUE_TYPE(BeInsertValueInst, BeInst);
  633. BeValue* mAggVal;
  634. BeValue* mMemberVal;
  635. int mIdx;
  636. virtual BeType* GetType() override;
  637. virtual void HashInst(BeHashContext& hashCtx) override
  638. {
  639. hashCtx.Mixin(TypeId);
  640. mAggVal->HashReference(hashCtx);
  641. mMemberVal->HashReference(hashCtx);
  642. hashCtx.Mixin(mIdx);
  643. }
  644. };
  645. class BeNumericCastInst : public BeInst
  646. {
  647. public:
  648. BE_VALUE_TYPE(BeNumericCastInst, BeInst);
  649. BeValue* mValue;
  650. BeType* mToType;
  651. bool mValSigned;
  652. bool mToSigned;
  653. virtual BeType* GetType() override;
  654. virtual void HashInst(BeHashContext& hashCtx) override
  655. {
  656. hashCtx.Mixin(TypeId);
  657. mValue->HashReference(hashCtx);
  658. mToType->HashReference(hashCtx);
  659. hashCtx.Mixin(mValSigned);
  660. hashCtx.Mixin(mToSigned);
  661. }
  662. };
  663. class BeBitCastInst : public BeInst
  664. {
  665. public:
  666. BE_VALUE_TYPE(BeBitCastInst, BeInst);
  667. BeValue* mValue;
  668. BeType* mToType;
  669. virtual BeType* GetType() override;
  670. virtual void HashInst(BeHashContext& hashCtx) override
  671. {
  672. hashCtx.Mixin(TypeId);
  673. mValue->HashReference(hashCtx);
  674. mToType->HashReference(hashCtx);
  675. }
  676. };
  677. class BeNegInst : public BeInst
  678. {
  679. public:
  680. BE_VALUE_TYPE(BeNegInst, BeInst);
  681. BeValue* mValue;
  682. virtual BeType* GetType() override;
  683. virtual void HashInst(BeHashContext& hashCtx) override
  684. {
  685. hashCtx.Mixin(TypeId);
  686. mValue->HashReference(hashCtx);
  687. }
  688. };
  689. class BeNotInst : public BeInst
  690. {
  691. public:
  692. BE_VALUE_TYPE(BeNotInst, BeInst);
  693. BeValue* mValue;
  694. virtual BeType* GetType() override;
  695. virtual void HashInst(BeHashContext& hashCtx) override
  696. {
  697. hashCtx.Mixin(TypeId);
  698. mValue->HashReference(hashCtx);
  699. }
  700. };
  701. enum BeBinaryOpKind
  702. {
  703. BeBinaryOpKind_None,
  704. BeBinaryOpKind_Add,
  705. BeBinaryOpKind_Subtract,
  706. BeBinaryOpKind_Multiply,
  707. BeBinaryOpKind_SDivide,
  708. BeBinaryOpKind_UDivide,
  709. BeBinaryOpKind_SModulus,
  710. BeBinaryOpKind_UModulus,
  711. BeBinaryOpKind_BitwiseAnd,
  712. BeBinaryOpKind_BitwiseOr,
  713. BeBinaryOpKind_ExclusiveOr,
  714. BeBinaryOpKind_LeftShift,
  715. BeBinaryOpKind_RightShift,
  716. BeBinaryOpKind_ARightShift,
  717. BeBinaryOpKind_Equality,
  718. BeBinaryOpKind_InEquality,
  719. BeBinaryOpKind_GreaterThan,
  720. BeBinaryOpKind_LessThan,
  721. BeBinaryOpKind_GreaterThanOrEqual,
  722. BeBinaryOpKind_LessThanOrEqual,
  723. };
  724. class BeBinaryOpInst : public BeInst
  725. {
  726. public:
  727. BE_VALUE_TYPE(BeBinaryOpInst, BeInst);
  728. BeBinaryOpKind mOpKind;
  729. BfOverflowCheckKind mOverflowCheckKind;
  730. BeValue* mLHS;
  731. BeValue* mRHS;
  732. virtual BeType* GetType() override;
  733. virtual void HashInst(BeHashContext& hashCtx) override
  734. {
  735. hashCtx.Mixin(TypeId);
  736. hashCtx.Mixin(mOpKind);
  737. hashCtx.Mixin(mOverflowCheckKind);
  738. mLHS->HashReference(hashCtx);
  739. mRHS->HashReference(hashCtx);
  740. }
  741. };
  742. enum BeCmpKind
  743. {
  744. BeCmpKind_None,
  745. BeCmpKind_SLT,
  746. BeCmpKind_ULT,
  747. BeCmpKind_OLT,
  748. BeCmpKind_SLE,
  749. BeCmpKind_ULE,
  750. BeCmpKind_OLE,
  751. BeCmpKind_EQ,
  752. BeCmpKind_OEQ,
  753. BeCmpKind_NE,
  754. BeCmpKind_UNE,
  755. BeCmpKind_SGT,
  756. BeCmpKind_UGT,
  757. BeCmpKind_OGT,
  758. BeCmpKind_SGE,
  759. BeCmpKind_UGE,
  760. BeCmpKind_OGE,
  761. BeCmpKind_NB,
  762. BeCmpKind_NO,
  763. BeCmpKind_Sign,
  764. };
  765. class BeCmpInst : public BeInst
  766. {
  767. public:
  768. BE_VALUE_TYPE(BeCmpInst, BeInst);
  769. BeValue* mLHS;
  770. BeValue* mRHS;
  771. BeCmpKind mCmpKind;
  772. virtual BeType* GetType() override;
  773. virtual void HashInst(BeHashContext& hashCtx) override
  774. {
  775. hashCtx.Mixin(TypeId);
  776. mLHS->HashReference(hashCtx);
  777. mRHS->HashReference(hashCtx);
  778. hashCtx.Mixin(mCmpKind);
  779. }
  780. };
  781. class BeObjectAccessCheckInst : public BeInst
  782. {
  783. public:
  784. BE_VALUE_TYPE(BeObjectAccessCheckInst, BeInst);
  785. BeValue* mValue;
  786. virtual void HashInst(BeHashContext& hashCtx) override
  787. {
  788. hashCtx.Mixin(TypeId);
  789. mValue->HashReference(hashCtx);
  790. }
  791. };
  792. class BeAllocaInst : public BeInst
  793. {
  794. public:
  795. BE_VALUE_TYPE(BeAllocaInst, BeInst);
  796. BeType* mType;
  797. BeValue* mArraySize;
  798. int mAlign;
  799. bool mNoChkStk;
  800. bool mForceMem;
  801. public:
  802. virtual BeType* GetType() override;
  803. virtual void HashInst(BeHashContext& hashCtx) override
  804. {
  805. hashCtx.Mixin(TypeId);
  806. mType->HashReference(hashCtx);
  807. if (mArraySize != NULL)
  808. mArraySize->HashReference(hashCtx);
  809. hashCtx.Mixin(mAlign);
  810. hashCtx.Mixin(mNoChkStk);
  811. hashCtx.Mixin(mForceMem);
  812. }
  813. };
  814. class BeAliasValueInst : public BeInst
  815. {
  816. public:
  817. BE_VALUE_TYPE(BeAliasValueInst, BeInst);
  818. BeValue* mPtr;
  819. virtual void HashInst(BeHashContext& hashCtx) override
  820. {
  821. hashCtx.Mixin(TypeId);
  822. mPtr->HashReference(hashCtx);
  823. }
  824. virtual BeType* GetType() override
  825. {
  826. return mPtr->GetType();
  827. }
  828. };
  829. class BeLifetimeStartInst : public BeInst
  830. {
  831. public:
  832. BE_VALUE_TYPE(BeLifetimeStartInst, BeInst);
  833. BeValue* mPtr;
  834. virtual void HashInst(BeHashContext& hashCtx) override
  835. {
  836. hashCtx.Mixin(TypeId);
  837. mPtr->HashReference(hashCtx);
  838. }
  839. };
  840. class BeLifetimeEndInst : public BeInst
  841. {
  842. public:
  843. BE_VALUE_TYPE(BeLifetimeEndInst, BeInst);
  844. BeValue* mPtr;
  845. virtual void HashInst(BeHashContext& hashCtx) override
  846. {
  847. hashCtx.Mixin(TypeId);
  848. mPtr->HashReference(hashCtx);
  849. }
  850. };
  851. class BeLifetimeSoftEndInst : public BeInst
  852. {
  853. public:
  854. BE_VALUE_TYPE(BeLifetimeSoftEndInst, BeInst);
  855. BeValue* mPtr;
  856. virtual void HashInst(BeHashContext& hashCtx) override
  857. {
  858. hashCtx.Mixin(TypeId);
  859. mPtr->HashReference(hashCtx);
  860. }
  861. };
  862. class BeLifetimeFenceInst : public BeInst
  863. {
  864. public:
  865. BE_VALUE_TYPE(BeLifetimeFenceInst, BeInst);
  866. BeBlock* mFenceBlock; // Lifetime is blocked from extending into the end of this block
  867. BeValue* mPtr;
  868. virtual void HashInst(BeHashContext& hashCtx) override
  869. {
  870. hashCtx.Mixin(TypeId);
  871. mFenceBlock->HashReference(hashCtx);
  872. mPtr->HashReference(hashCtx);
  873. }
  874. };
  875. class BeLifetimeExtendInst : public BeInst
  876. {
  877. public:
  878. BE_VALUE_TYPE(BeLifetimeExtendInst, BeInst);
  879. BeValue* mPtr;
  880. virtual void HashInst(BeHashContext& hashCtx) override
  881. {
  882. hashCtx.Mixin(TypeId);
  883. mPtr->HashReference(hashCtx);
  884. }
  885. };
  886. class BeValueScopeStartInst : public BeInst
  887. {
  888. public:
  889. BE_VALUE_TYPE(BeValueScopeStartInst, BeInst);
  890. virtual BeType* GetType() override;
  891. virtual void HashInst(BeHashContext& hashCtx) override
  892. {
  893. hashCtx.Mixin(TypeId);
  894. }
  895. };
  896. class BeValueScopeRetainInst : public BeInst
  897. {
  898. public:
  899. BE_VALUE_TYPE(BeValueScopeRetainInst, BeInst);
  900. BeValue* mValue;
  901. virtual void HashInst(BeHashContext& hashCtx) override
  902. {
  903. hashCtx.Mixin(TypeId);
  904. mValue->HashReference(hashCtx);
  905. }
  906. };
  907. class BeValueScopeEndInst : public BeInst
  908. {
  909. public:
  910. BE_VALUE_TYPE(BeValueScopeEndInst, BeInst);
  911. BeValueScopeStartInst* mScopeStart;
  912. bool mIsSoft;
  913. virtual void HashInst(BeHashContext& hashCtx) override
  914. {
  915. hashCtx.Mixin(TypeId);
  916. mScopeStart->HashReference(hashCtx);
  917. hashCtx.Mixin(mIsSoft);
  918. }
  919. };
  920. class BeLoadInst : public BeInst
  921. {
  922. public:
  923. BE_VALUE_TYPE(BeLoadInst, BeInst);
  924. BeValue* mTarget;
  925. bool mIsVolatile;
  926. public:
  927. virtual BeType* GetType() override;
  928. virtual void HashInst(BeHashContext& hashCtx) override
  929. {
  930. hashCtx.Mixin(TypeId);
  931. mTarget->HashReference(hashCtx);
  932. }
  933. };
  934. class BeStoreInst : public BeInst
  935. {
  936. public:
  937. BE_VALUE_TYPE(BeStoreInst, BeInst);
  938. BeValue* mVal;
  939. BeValue* mPtr;
  940. bool mIsVolatile;
  941. virtual void HashInst(BeHashContext& hashCtx) override
  942. {
  943. hashCtx.Mixin(TypeId);
  944. mVal->HashReference(hashCtx);
  945. mPtr->HashReference(hashCtx);
  946. }
  947. };
  948. class BeSetCanMergeInst : public BeInst
  949. {
  950. public:
  951. BE_VALUE_TYPE(BeSetCanMergeInst, BeInst);
  952. BeValue* mVal;
  953. virtual void HashInst(BeHashContext& hashCtx) override
  954. {
  955. hashCtx.Mixin(TypeId);
  956. mVal->HashReference(hashCtx);
  957. }
  958. };
  959. class BeMemSetInst : public BeInst
  960. {
  961. public:
  962. BE_VALUE_TYPE(BeGEPInst, BeInst);
  963. BeValue* mAddr;
  964. BeValue* mVal;
  965. BeValue* mSize;
  966. int mAlignment;
  967. virtual void HashInst(BeHashContext& hashCtx) override
  968. {
  969. hashCtx.Mixin(TypeId);
  970. mAddr->HashReference(hashCtx);
  971. mVal->HashReference(hashCtx);
  972. mSize->HashReference(hashCtx);
  973. hashCtx.Mixin(mAlignment);
  974. }
  975. };
  976. class BeFenceInst : public BeInst
  977. {
  978. public:
  979. BE_VALUE_TYPE(BeFenceInst, BeInst);
  980. virtual void HashInst(BeHashContext& hashCtx) override
  981. {
  982. hashCtx.Mixin(TypeId);
  983. }
  984. };
  985. class BeStackSaveInst : public BeInst
  986. {
  987. public:
  988. BE_VALUE_TYPE(BeStackSaveInst, BeInst);
  989. virtual void HashInst(BeHashContext& hashCtx) override
  990. {
  991. hashCtx.Mixin(TypeId);
  992. }
  993. virtual BeType* GetType() override
  994. {
  995. return GetContext()->GetPrimitiveType(BeTypeCode_NullPtr);
  996. }
  997. };
  998. class BeStackRestoreInst : public BeInst
  999. {
  1000. public:
  1001. BE_VALUE_TYPE(BeStackRestoreInst, BeInst);
  1002. BeValue* mStackVal;
  1003. virtual void HashInst(BeHashContext& hashCtx) override
  1004. {
  1005. hashCtx.Mixin(TypeId);
  1006. mStackVal->HashReference(hashCtx);
  1007. }
  1008. };
  1009. class BeGEPInst : public BeInst
  1010. {
  1011. public:
  1012. BE_VALUE_TYPE(BeGEPInst, BeInst);
  1013. BeValue* mPtr;
  1014. BeValue* mIdx0;
  1015. BeValue* mIdx1;
  1016. virtual BeType* GetType() override;
  1017. virtual void HashInst(BeHashContext& hashCtx) override
  1018. {
  1019. hashCtx.Mixin(TypeId);
  1020. mPtr->HashReference(hashCtx);
  1021. mIdx0->HashReference(hashCtx);
  1022. if (mIdx1 != NULL)
  1023. mIdx1->HashReference(hashCtx);
  1024. }
  1025. };
  1026. class BeBrInst : public BeInst
  1027. {
  1028. public:
  1029. BE_VALUE_TYPE(BeBrInst, BeInst);
  1030. BeBlock* mTargetBlock;
  1031. bool mNoCollapse;
  1032. bool mIsFake;
  1033. virtual void HashInst(BeHashContext& hashCtx) override
  1034. {
  1035. hashCtx.Mixin(TypeId);
  1036. mTargetBlock->HashReference(hashCtx);
  1037. hashCtx.Mixin(mNoCollapse);
  1038. hashCtx.Mixin(mIsFake);
  1039. }
  1040. };
  1041. class BeCondBrInst : public BeInst
  1042. {
  1043. public:
  1044. BE_VALUE_TYPE(BeCondBrInst, BeInst);
  1045. BeValue* mCond;
  1046. BeBlock* mTrueBlock;
  1047. BeBlock* mFalseBlock;
  1048. virtual void HashInst(BeHashContext& hashCtx) override
  1049. {
  1050. hashCtx.Mixin(TypeId);
  1051. mCond->HashReference(hashCtx);
  1052. mTrueBlock->HashReference(hashCtx);
  1053. mFalseBlock->HashReference(hashCtx);
  1054. }
  1055. };
  1056. class BePhiIncoming : public BeValue
  1057. {
  1058. public:
  1059. BE_VALUE_TYPE(BePhiIncoming, BeValue);
  1060. BeBlock* mBlock;
  1061. BeValue* mValue;
  1062. virtual void HashContent(BeHashContext& hashCtx) override
  1063. {
  1064. hashCtx.Mixin(TypeId);
  1065. mBlock->HashReference(hashCtx);
  1066. mValue->HashReference(hashCtx);
  1067. }
  1068. };
  1069. class BePhiInst : public BeInst
  1070. {
  1071. public:
  1072. BE_VALUE_TYPE(BePhiInst, BeInst);
  1073. BeType* mType;
  1074. SizedArray<BePhiIncoming*, 4> mIncoming;
  1075. virtual BeType* GetType() override;
  1076. virtual void HashInst(BeHashContext& hashCtx) override
  1077. {
  1078. hashCtx.Mixin(TypeId);
  1079. mType->HashReference(hashCtx);
  1080. hashCtx.Mixin(mIncoming.size());
  1081. for (auto incoming : mIncoming)
  1082. {
  1083. incoming->mBlock->HashReference(hashCtx);
  1084. incoming->mValue->HashReference(hashCtx);
  1085. }
  1086. }
  1087. };
  1088. class BeSwitchCase
  1089. {
  1090. public:
  1091. BeConstant* mValue;
  1092. BeBlock* mBlock;
  1093. };
  1094. class BeSwitchInst : public BeInst
  1095. {
  1096. public:
  1097. BE_VALUE_TYPE(BeSwitchInst, BeInst);
  1098. BeValue* mValue;
  1099. BeBlock* mDefaultBlock;
  1100. Array<BeSwitchCase> mCases;
  1101. virtual void HashInst(BeHashContext& hashCtx) override
  1102. {
  1103. hashCtx.Mixin(TypeId);
  1104. mValue->HashReference(hashCtx);
  1105. mDefaultBlock->HashReference(hashCtx);
  1106. for (auto& caseVal : mCases)
  1107. {
  1108. caseVal.mValue->HashReference(hashCtx);
  1109. caseVal.mBlock->HashReference(hashCtx);
  1110. }
  1111. }
  1112. };
  1113. class BeRetInst : public BeInst
  1114. {
  1115. public:
  1116. BE_VALUE_TYPE(BeRetInst, BeInst);
  1117. BeValue* mRetValue;
  1118. virtual void HashInst(BeHashContext& hashCtx) override
  1119. {
  1120. hashCtx.Mixin(TypeId);
  1121. if (mRetValue != NULL)
  1122. mRetValue->HashReference(hashCtx);
  1123. }
  1124. };
  1125. class BeSetRetInst : public BeRetInst
  1126. {
  1127. public:
  1128. BE_VALUE_TYPE(BeSetRetInst, BeRetInst);
  1129. public:
  1130. int32 mReturnTypeId;
  1131. };
  1132. class BeCallInst : public BeInst
  1133. {
  1134. public:
  1135. struct Arg
  1136. {
  1137. BeValue* mValue;
  1138. int mDereferenceableSize;
  1139. int mByRefSize;
  1140. bool mStructRet;
  1141. bool mZExt;
  1142. bool mNoAlias;
  1143. bool mNoCapture;
  1144. Arg()
  1145. {
  1146. mValue = NULL;
  1147. mStructRet = false;
  1148. mZExt = false;
  1149. mNoAlias = false;
  1150. mNoCapture = false;
  1151. mDereferenceableSize = -1;
  1152. mByRefSize = -1;
  1153. }
  1154. };
  1155. public:
  1156. BE_VALUE_TYPE(BeCallInst, BeInst);
  1157. BeValue* mInlineResult;
  1158. BeValue* mFunc;
  1159. SizedArray<Arg, 4> mArgs;
  1160. BfIRCallingConv mCallingConv;
  1161. bool mNoReturn;
  1162. bool mTailCall;
  1163. virtual BeType* GetType() override;
  1164. BeCallInst()
  1165. {
  1166. mInlineResult = NULL;
  1167. mFunc = NULL;
  1168. mCallingConv = BfIRCallingConv_CDecl;
  1169. mNoReturn = false;
  1170. mTailCall = false;
  1171. }
  1172. virtual void HashInst(BeHashContext& hashCtx) override
  1173. {
  1174. hashCtx.Mixin(TypeId);
  1175. if (mInlineResult != NULL)
  1176. mInlineResult->HashReference(hashCtx);
  1177. mFunc->HashReference(hashCtx);
  1178. for (auto& arg : mArgs)
  1179. {
  1180. arg.mValue->HashReference(hashCtx);
  1181. hashCtx.Mixin(arg.mStructRet);
  1182. hashCtx.Mixin(arg.mZExt);
  1183. hashCtx.Mixin(arg.mDereferenceableSize);
  1184. hashCtx.Mixin(arg.mByRefSize);
  1185. }
  1186. hashCtx.Mixin(mCallingConv);
  1187. hashCtx.Mixin(mNoReturn);
  1188. hashCtx.Mixin(mTailCall);
  1189. }
  1190. bool HasStructRet()
  1191. {
  1192. return (!mArgs.IsEmpty()) && (mArgs[0].mStructRet);
  1193. }
  1194. };
  1195. //////////////////////////////////////////////////////////////////////////
  1196. class BeComptimeError : public BeInst
  1197. {
  1198. public:
  1199. BE_VALUE_TYPE(BeComptimeError, BeInst);
  1200. public:
  1201. int mError;
  1202. public:
  1203. virtual void HashInst(BeHashContext& hashCtx) override
  1204. {
  1205. hashCtx.Mixin(TypeId);
  1206. hashCtx.Mixin(mError);
  1207. }
  1208. };
  1209. class BeComptimeGetType : public BeInst
  1210. {
  1211. public:
  1212. BE_VALUE_TYPE(BeComptimeGetType, BeInst);
  1213. public:
  1214. int mTypeId;
  1215. BeType* mResultType;
  1216. public:
  1217. virtual BeType* GetType() override
  1218. {
  1219. return mResultType;
  1220. }
  1221. virtual void HashInst(BeHashContext& hashCtx) override
  1222. {
  1223. hashCtx.Mixin(TypeId);
  1224. hashCtx.Mixin(mTypeId);
  1225. }
  1226. };
  1227. class BeComptimeGetReflectType : public BeInst
  1228. {
  1229. public:
  1230. BE_VALUE_TYPE(BeComptimeGetReflectType, BeInst);
  1231. public:
  1232. int mTypeId;
  1233. BeType* mResultType;
  1234. public:
  1235. virtual BeType* GetType() override
  1236. {
  1237. return mResultType;
  1238. }
  1239. virtual void HashInst(BeHashContext& hashCtx) override
  1240. {
  1241. hashCtx.Mixin(TypeId);
  1242. hashCtx.Mixin(mTypeId);
  1243. }
  1244. };
  1245. class BeComptimeDynamicCastCheck : public BeInst
  1246. {
  1247. public:
  1248. BE_VALUE_TYPE(BeComptimeDynamicCastCheck, BeInst);
  1249. public:
  1250. BeValue* mValue;
  1251. int mTypeId;
  1252. BeType* mResultType;
  1253. public:
  1254. virtual BeType* GetType() override
  1255. {
  1256. return mResultType;
  1257. }
  1258. virtual void HashInst(BeHashContext& hashCtx) override
  1259. {
  1260. hashCtx.Mixin(TypeId);
  1261. mValue->HashReference(hashCtx);
  1262. hashCtx.Mixin(mTypeId);
  1263. }
  1264. };
  1265. class BeComptimeGetVirtualFunc : public BeInst
  1266. {
  1267. public:
  1268. BE_VALUE_TYPE(BeComptimeGetVirtualFunc, BeInst);
  1269. public:
  1270. BeValue* mValue;
  1271. int mVirtualTableIdx;
  1272. BeType* mResultType;
  1273. public:
  1274. virtual BeType* GetType() override
  1275. {
  1276. return mResultType;
  1277. }
  1278. virtual void HashInst(BeHashContext& hashCtx) override
  1279. {
  1280. hashCtx.Mixin(TypeId);
  1281. mValue->HashReference(hashCtx);
  1282. hashCtx.Mixin(mVirtualTableIdx);
  1283. }
  1284. };
  1285. class BeComptimeGetInterfaceFunc : public BeInst
  1286. {
  1287. public:
  1288. BE_VALUE_TYPE(BeComptimeGetInterfaceFunc, BeInst);
  1289. public:
  1290. BeValue* mValue;
  1291. int mIFaceTypeId;
  1292. int mMethodIdx;
  1293. BeType* mResultType;
  1294. public:
  1295. virtual BeType* GetType() override
  1296. {
  1297. return mResultType;
  1298. }
  1299. virtual void HashInst(BeHashContext& hashCtx) override
  1300. {
  1301. hashCtx.Mixin(TypeId);
  1302. mValue->HashReference(hashCtx);
  1303. hashCtx.Mixin(mIFaceTypeId);
  1304. hashCtx.Mixin(mMethodIdx);
  1305. }
  1306. };
  1307. //////////////////////////////////////////////////////////////////////////
  1308. class BeArgument : public BeValue
  1309. {
  1310. public:
  1311. BE_VALUE_TYPE(BeArgument, BeValue);
  1312. BeModule* mModule;
  1313. int mArgIdx;
  1314. virtual BeType* GetType() override;
  1315. virtual void HashContent(BeHashContext& hashCtx) override
  1316. {
  1317. hashCtx.Mixin(TypeId);
  1318. hashCtx.Mixin(mArgIdx);
  1319. }
  1320. };
  1321. struct BeDumpContext
  1322. {
  1323. public:
  1324. Dictionary<BeValue*, String> mValueNameMap;
  1325. Dictionary<String, int> mSeenNames;
  1326. void ToString(StringImpl& str, BeValue* value, bool showType = true, bool mdDrillDown = false, bool checkStack = false);
  1327. void ToString(StringImpl& str, BeType* type);
  1328. void ToString(StringImpl& str, BeDbgFunction* dbgFunction, bool showScope);
  1329. static void ToString(StringImpl& str, int val);
  1330. static void ToString(StringImpl& str, BeCmpKind cmpKind);
  1331. static void ToString(StringImpl& str, BeBinaryOpKind opKind);
  1332. String ToString(BeValue* value, bool showType = true, bool mdDrillDown = false);
  1333. String ToString(BeType* type);
  1334. String ToString(BeDbgFunction* dbgFunction);
  1335. static String ToString(int val);
  1336. static String ToString(BeCmpKind cmpKind);
  1337. static String ToString(BeBinaryOpKind opKind);
  1338. };
  1339. //////////////////////////////////////////////////////////////////////////
  1340. class BeDbgVariable;
  1341. class BeDbgDeclareInst : public BeInst
  1342. {
  1343. public:
  1344. BE_VALUE_TYPE(BeDbgDeclareInst, BeInst);
  1345. public:
  1346. BeDbgVariable* mDbgVar;
  1347. BeValue* mValue;
  1348. bool mIsValue;
  1349. virtual void HashInst(BeHashContext& hashCtx) override;
  1350. };
  1351. class BeMDNode : public BeValue
  1352. {
  1353. public:
  1354. BE_VALUE_TYPE(BeMDNode, BeValue);
  1355. public:
  1356. virtual ~BeMDNode()
  1357. {
  1358. }
  1359. virtual void HashContent(BeHashContext& hashCtx) override
  1360. {
  1361. hashCtx.Mixin(TypeId);
  1362. }
  1363. };
  1364. class BeDbgFile;
  1365. class BeDbgLoc : public BeMDNode
  1366. {
  1367. public:
  1368. BE_VALUE_TYPE(BeDbgLoc, BeMDNode);
  1369. public:
  1370. int mLine;
  1371. int mColumn;
  1372. BeMDNode* mDbgScope;
  1373. BeDbgLoc* mDbgInlinedAt;
  1374. int mIdx;
  1375. bool mHadInline;
  1376. public:
  1377. BeDbgLoc()
  1378. {
  1379. }
  1380. int GetInlineDepth();
  1381. int GetInlineMatchDepth(BeDbgLoc* other);
  1382. BeDbgLoc* GetInlinedAt(int idx = 0);
  1383. BeDbgLoc* GetRoot();
  1384. BeDbgFunction* GetDbgFunc();
  1385. BeDbgFile* GetDbgFile();
  1386. virtual void HashContent(BeHashContext& hashCtx) override
  1387. {
  1388. hashCtx.Mixin(TypeId);
  1389. hashCtx.Mixin(mLine);
  1390. hashCtx.Mixin(mColumn);
  1391. if (mDbgScope != NULL)
  1392. mDbgScope->HashReference(hashCtx);
  1393. else
  1394. hashCtx.Mixin(-1);
  1395. if (mDbgInlinedAt != NULL)
  1396. mDbgInlinedAt->HashReference(hashCtx);
  1397. }
  1398. };
  1399. class BeDbgLexicalBlock : public BeMDNode
  1400. {
  1401. public:
  1402. BE_VALUE_TYPE(BeDbgLexicalBlock, BeMDNode);
  1403. public:
  1404. BeDbgFile* mFile;
  1405. BeMDNode* mScope;
  1406. BeBlock* mLastBeBlock;
  1407. int mId;
  1408. virtual void HashContent(BeHashContext& hashCtx) override;
  1409. };
  1410. class BeDbgNamespace : public BeMDNode
  1411. {
  1412. public:
  1413. BE_VALUE_TYPE(BeDbgNamespace, BeMDNode);
  1414. public:
  1415. BeMDNode* mScope;
  1416. String mName;
  1417. virtual void HashContent(BeHashContext& hashCtx) override
  1418. {
  1419. hashCtx.Mixin(TypeId);
  1420. mScope->HashReference(hashCtx);
  1421. hashCtx.MixinStr(mName);
  1422. }
  1423. };
  1424. class BeDbgTypeId : public BeMDNode
  1425. {
  1426. public:
  1427. BE_VALUE_TYPE(BeDbgTypeId, BeMDNode);
  1428. public:
  1429. int mTypeId;
  1430. BeDbgTypeId()
  1431. {
  1432. mTypeId = -1;
  1433. }
  1434. virtual void HashContent(BeHashContext& hashCtx) override
  1435. {
  1436. hashCtx.Mixin(TypeId);
  1437. hashCtx.Mixin(mTypeId);
  1438. }
  1439. };
  1440. class BeDbgType : public BeMDNode
  1441. {
  1442. public:
  1443. BE_VALUE_TYPE(BeDbgType, BeMDNode);
  1444. public:
  1445. int mSize;
  1446. int mAlign;
  1447. int mCvDeclTypeId;
  1448. int mCvDefTypeId;
  1449. BumpList<BeDbgType*> mDerivedTypes;
  1450. BeDbgType()
  1451. {
  1452. mSize = -1;
  1453. mAlign = -1;
  1454. mCvDeclTypeId = -1;
  1455. mCvDefTypeId = -1;
  1456. }
  1457. BeDbgType* FindDerivedType(int typeId)
  1458. {
  1459. for (auto derivedType : mDerivedTypes)
  1460. {
  1461. if (derivedType->GetTypeId() == typeId)
  1462. return derivedType;
  1463. }
  1464. return NULL;
  1465. }
  1466. virtual void HashContent(BeHashContext& hashCtx) override
  1467. {
  1468. hashCtx.Mixin(TypeId);
  1469. hashCtx.Mixin(mSize);
  1470. hashCtx.Mixin(mAlign);
  1471. }
  1472. };
  1473. class BeDbgBasicType : public BeDbgType
  1474. {
  1475. public:
  1476. BE_VALUE_TYPE(BeDbgBasicType, BeDbgType);
  1477. public:
  1478. String mName;
  1479. int mEncoding;
  1480. virtual void HashContent(BeHashContext& hashCtx) override
  1481. {
  1482. hashCtx.Mixin(TypeId);
  1483. hashCtx.Mixin(mSize);
  1484. hashCtx.Mixin(mAlign);
  1485. hashCtx.MixinStr(mName);
  1486. hashCtx.Mixin(mEncoding);
  1487. }
  1488. };
  1489. class BeDbgArrayType : public BeDbgType
  1490. {
  1491. public:
  1492. BE_VALUE_TYPE(BeDbgArrayType, BeDbgType);
  1493. public:
  1494. BeDbgType* mElement;
  1495. int mNumElements;
  1496. virtual void HashContent(BeHashContext& hashCtx) override
  1497. {
  1498. hashCtx.Mixin(TypeId);
  1499. hashCtx.Mixin(mSize);
  1500. hashCtx.Mixin(mAlign);
  1501. hashCtx.Mixin(mNumElements);
  1502. mElement->HashReference(hashCtx);
  1503. }
  1504. };
  1505. class BeDbgArtificialType : public BeDbgType
  1506. {
  1507. public:
  1508. BE_VALUE_TYPE(BeDbgArtificialType, BeDbgType);
  1509. public:
  1510. BeDbgType* mElement;
  1511. virtual void HashContent(BeHashContext& hashCtx) override
  1512. {
  1513. hashCtx.Mixin(TypeId);
  1514. mElement->HashReference(hashCtx);
  1515. }
  1516. };
  1517. class BeDbgConstType : public BeDbgType
  1518. {
  1519. public:
  1520. BE_VALUE_TYPE(BeDbgConstType, BeDbgType);
  1521. public:
  1522. BeMDNode* mElement;
  1523. virtual void HashContent(BeHashContext& hashCtx) override
  1524. {
  1525. hashCtx.Mixin(TypeId);
  1526. mElement->HashReference(hashCtx);
  1527. }
  1528. };
  1529. class BeDbgReferenceType : public BeDbgType
  1530. {
  1531. public:
  1532. BE_VALUE_TYPE(BeDbgReferenceType, BeDbgType);
  1533. public:
  1534. BeDbgType* mElement;
  1535. virtual void HashContent(BeHashContext& hashCtx) override
  1536. {
  1537. hashCtx.Mixin(TypeId);
  1538. mElement->HashReference(hashCtx);
  1539. }
  1540. };
  1541. class BeDbgPointerType : public BeDbgType
  1542. {
  1543. public:
  1544. BE_VALUE_TYPE(BeDbgPointerType, BeDbgType);
  1545. public:
  1546. BeDbgType* mElement;
  1547. virtual void HashContent(BeHashContext& hashCtx) override
  1548. {
  1549. hashCtx.Mixin(TypeId);
  1550. mElement->HashReference(hashCtx);
  1551. }
  1552. };
  1553. class BeDbgInheritance : public BeMDNode
  1554. {
  1555. public:
  1556. BE_VALUE_TYPE(BeDbgInheritance, BeMDNode);
  1557. public:
  1558. BeDbgType* mBaseType;
  1559. virtual void HashContent(BeHashContext& hashCtx) override
  1560. {
  1561. hashCtx.Mixin(TypeId);
  1562. mBaseType->HashReference(hashCtx);
  1563. }
  1564. };
  1565. class BeDbgStructMember : public BeMDNode
  1566. {
  1567. public:
  1568. BE_VALUE_TYPE(BeDbgStructMember, BeMDNode);
  1569. public:
  1570. String mName;
  1571. BeDbgType* mType;
  1572. int mFlags;
  1573. int mOffset;
  1574. bool mIsStatic;
  1575. BeValue* mStaticValue;
  1576. public:
  1577. BeDbgStructMember()
  1578. {
  1579. mType = NULL;
  1580. mFlags = 0;
  1581. mOffset = -1;
  1582. mIsStatic = false;
  1583. mStaticValue = NULL;
  1584. }
  1585. virtual void HashContent(BeHashContext& hashCtx) override
  1586. {
  1587. hashCtx.Mixin(TypeId);
  1588. hashCtx.MixinStr(mName);
  1589. mType->HashReference(hashCtx);
  1590. hashCtx.Mixin(mFlags);
  1591. hashCtx.Mixin(mOffset);
  1592. hashCtx.Mixin(mIsStatic);
  1593. if (mStaticValue != NULL)
  1594. mStaticValue->HashReference(hashCtx);
  1595. }
  1596. };
  1597. class BeDbgFunctionType : public BeMDNode
  1598. {
  1599. public:
  1600. BE_VALUE_TYPE(BeDbgFunctionType, BeMDNode);
  1601. public:
  1602. BeDbgType* mReturnType;
  1603. Array<BeDbgType*> mParams;
  1604. public:
  1605. virtual void HashContent(BeHashContext& hashCtx) override
  1606. {
  1607. hashCtx.Mixin(TypeId);
  1608. if (mReturnType != NULL)
  1609. mReturnType->HashReference(hashCtx);
  1610. hashCtx.Mixin(mParams.size());
  1611. for (auto param : mParams)
  1612. param->HashReference(hashCtx);
  1613. }
  1614. };
  1615. /*class BeDbgVariableRange
  1616. {
  1617. public:
  1618. enum Kind
  1619. {
  1620. Kind_None,
  1621. Kind_Reg, // Direct reg usage
  1622. Kind_Indexed // [RBP+8] type usage (spilled)
  1623. };
  1624. public:
  1625. int mCodeStartOfs;
  1626. int mCodeRange;
  1627. Kind mKind;
  1628. X64CPURegister mReg;
  1629. int mOfs;
  1630. public:
  1631. BeDbgVariableRange()
  1632. {
  1633. mCodeStartOfs = -1;
  1634. mCodeRange = -1;
  1635. mKind = Kind_None;
  1636. mReg = X64Reg_None;
  1637. mOfs = 0;
  1638. }
  1639. };*/
  1640. class BeDbgVariableRange
  1641. {
  1642. public:
  1643. int mOffset;
  1644. int mLength;
  1645. };
  1646. class BeDbgVariableLoc
  1647. {
  1648. public:
  1649. enum Kind
  1650. {
  1651. Kind_None,
  1652. Kind_Reg, // Direct reg usage
  1653. Kind_Indexed, // [RBP+8] type usage (spilled)
  1654. Kind_SymbolAddr
  1655. };
  1656. Kind mKind;
  1657. X64CPURegister mReg;
  1658. int mOfs;
  1659. public:
  1660. BeDbgVariableLoc()
  1661. {
  1662. mKind = Kind_None;
  1663. mReg = X64Reg_None;
  1664. mOfs = 0;
  1665. }
  1666. };
  1667. class BeDbgVariable : public BeMDNode
  1668. {
  1669. public:
  1670. BE_VALUE_TYPE(BeDbgVariable, BeMDNode);
  1671. public:
  1672. String mName;
  1673. BeMDNode* mType;
  1674. BeValue* mValue;
  1675. int mParamNum;
  1676. BfIRInitType mInitType;
  1677. BfIRInitType mPendingInitType;
  1678. bool mPendingInitDef;
  1679. BeMDNode* mScope;
  1680. BeDbgLoc* mDeclDbgLoc;
  1681. BeDbgVariableLoc mPrimaryLoc;
  1682. BeDbgVariableLoc mSavedLoc;
  1683. int mDeclStart;
  1684. int mDeclEnd;
  1685. int mDeclMCBlockId;
  1686. bool mDeclLifetimeExtend;
  1687. bool mDbgLifeEnded;
  1688. bool mIsValue; // Value vs Addr
  1689. Array<BeDbgVariableRange> mSavedRanges;
  1690. Array<BeDbgVariableRange> mGaps;
  1691. public:
  1692. BeDbgVariable()
  1693. {
  1694. mType = NULL;
  1695. mValue = NULL;
  1696. mParamNum = -1;
  1697. mInitType = BfIRInitType_NotSet;
  1698. mPendingInitType = BfIRInitType_NotNeeded;
  1699. mPendingInitDef = false;
  1700. mScope = NULL;
  1701. mDeclDbgLoc = NULL;
  1702. mDeclStart = -1;
  1703. mDeclEnd = -1;
  1704. mDeclMCBlockId = -1;
  1705. mIsValue = false;
  1706. mDbgLifeEnded = false;
  1707. mDeclLifetimeExtend = false;
  1708. }
  1709. virtual void HashContent(BeHashContext& hashCtx) override
  1710. {
  1711. hashCtx.Mixin(TypeId);
  1712. hashCtx.MixinStr(mName);
  1713. mType->HashReference(hashCtx);
  1714. if (mValue != NULL)
  1715. mValue->HashReference(hashCtx);
  1716. hashCtx.Mixin(mParamNum);
  1717. hashCtx.Mixin(mInitType);
  1718. hashCtx.Mixin(mPendingInitType);
  1719. if (mScope != NULL)
  1720. mScope->HashReference(hashCtx);
  1721. if (mDeclDbgLoc != NULL)
  1722. mDeclDbgLoc->HashReference(hashCtx);
  1723. // The others only get filled in after generation -- not part of hash
  1724. }
  1725. };
  1726. class BeDbgFile;
  1727. class BeDbgCodeEmission
  1728. {
  1729. public:
  1730. BeDbgLoc* mDbgLoc;
  1731. int mPos;
  1732. };
  1733. class BeDbgFunction : public BeMDNode
  1734. {
  1735. public:
  1736. BE_VALUE_TYPE(BeDbgFunction, BeMDNode);
  1737. public:
  1738. int mIdx;
  1739. BeMDNode* mScope;
  1740. BeDbgFile* mFile;
  1741. int mLine;
  1742. String mName;
  1743. String mLinkageName;
  1744. BeDbgFunctionType* mType;
  1745. //Array<BeDbgVariable*> mParams;
  1746. Array<BeDbgType*> mGenericArgs;
  1747. Array<BeConstant*> mGenericConstValueArgs;
  1748. BeFunction* mValue;
  1749. bool mIsLocalToUnit;
  1750. bool mIsStaticMethod;
  1751. bool mIncludedAsMember;
  1752. int mFlags;
  1753. int mVK;
  1754. int mVIndex;
  1755. Array<BeDbgVariable*> mVariables;
  1756. int mPrologSize;
  1757. int mCodeLen;
  1758. Array<BeDbgCodeEmission> mEmissions;
  1759. int mCvTypeId;
  1760. int mCvFuncId;
  1761. int mCvArgListId;
  1762. public:
  1763. BeDbgFunction()
  1764. {
  1765. mIdx = -1;
  1766. mScope = NULL;
  1767. mFile = NULL;
  1768. mLine = -1;
  1769. mType = NULL;
  1770. mValue = NULL;
  1771. mFlags = 0;
  1772. mIsLocalToUnit = false;
  1773. mVK = -1;
  1774. mVIndex = -1;
  1775. mIsStaticMethod = true;
  1776. mIncludedAsMember = false;
  1777. mPrologSize = 0;
  1778. mCodeLen = -1;
  1779. mCvTypeId = -1;
  1780. mCvFuncId = -1;
  1781. mCvArgListId = -1;
  1782. }
  1783. BeMDNode* GetParamType(int paramIdx)
  1784. {
  1785. /*if (!mParams.empty())
  1786. return mParams[paramIdx]->mType;*/
  1787. if (paramIdx < (int)mVariables.size())
  1788. {
  1789. auto param = mVariables[paramIdx];
  1790. if (param->mParamNum == paramIdx)
  1791. return param->mType;
  1792. }
  1793. return mType->mParams[paramIdx];
  1794. }
  1795. bool HasThis()
  1796. {
  1797. return !mIsStaticMethod;
  1798. /*bool matchLLVM = false;
  1799. // This matches the LLVM CV emitter
  1800. if (matchLLVM)
  1801. {
  1802. return (BeValueDynCast<BeDbgType>(mScope) != NULL) &&
  1803. (mType->mParams.size() > 0);
  1804. }
  1805. else
  1806. {
  1807. return ((mVariables.size() > 0) && (mVariables[0]->mName == "this"));
  1808. }*/
  1809. }
  1810. virtual void HashContent(BeHashContext& hashCtx) override;
  1811. };
  1812. class BeDbgInlinedScope : public BeMDNode
  1813. {
  1814. public:
  1815. BE_VALUE_TYPE(BeDbgInlinedScope, BeMDNode);
  1816. BeMDNode* mScope;
  1817. virtual void HashContent(BeHashContext& hashCtx) override
  1818. {
  1819. hashCtx.Mixin(TypeId);
  1820. mScope->HashReference(hashCtx);
  1821. }
  1822. };
  1823. class BeDbgFile;
  1824. class BeDbgStructType : public BeDbgType
  1825. {
  1826. public:
  1827. BE_VALUE_TYPE(BeDbgStructType, BeDbgType);
  1828. public:
  1829. BeMDNode* mScope;
  1830. String mName;
  1831. BeDbgType* mDerivedFrom;
  1832. Array<BeDbgStructMember*> mMembers;
  1833. Array<BeDbgFunction*> mMethods;
  1834. bool mIsFullyDefined;
  1835. bool mIsStatic;
  1836. BeDbgFile* mDefFile;
  1837. int mDefLine;
  1838. public:
  1839. BeDbgStructType()
  1840. {
  1841. mScope = NULL;
  1842. mDerivedFrom = NULL;
  1843. mIsStatic = false;
  1844. mIsFullyDefined = false;
  1845. mDefFile = NULL;
  1846. mDefLine = 0;
  1847. }
  1848. void SetMembers(SizedArrayImpl<BeMDNode*>& members);
  1849. virtual void HashContent(BeHashContext& hashCtx) override;
  1850. };
  1851. class BeDbgEnumMember : public BeMDNode
  1852. {
  1853. public:
  1854. BE_VALUE_TYPE(BeDbgEnumMember, BeMDNode);
  1855. public:
  1856. String mName;
  1857. int64 mValue;
  1858. virtual void HashContent(BeHashContext& hashCtx) override
  1859. {
  1860. hashCtx.Mixin(TypeId);
  1861. hashCtx.MixinStr(mName);
  1862. hashCtx.Mixin(mValue);
  1863. }
  1864. };
  1865. class BeDbgEnumType : public BeDbgType
  1866. {
  1867. public:
  1868. BE_VALUE_TYPE(BeDbgEnumType, BeDbgType);
  1869. public:
  1870. BeMDNode* mScope;
  1871. String mName;
  1872. BeDbgType* mElementType;
  1873. bool mIsFullyDefined;
  1874. Array<BeDbgEnumMember*> mMembers;
  1875. public:
  1876. BeDbgEnumType()
  1877. {
  1878. mScope = NULL;
  1879. mElementType = NULL;
  1880. mIsFullyDefined = false;
  1881. }
  1882. void SetMembers(SizedArrayImpl<BeMDNode*>& members);
  1883. virtual void HashContent(BeHashContext& hashCtx) override
  1884. {
  1885. mScope->HashReference(hashCtx);
  1886. hashCtx.MixinStr(mName);
  1887. if (mElementType != NULL)
  1888. mElementType->HashReference(hashCtx);
  1889. hashCtx.Mixin(mIsFullyDefined);
  1890. for (auto member : mMembers)
  1891. member->HashReference(hashCtx);
  1892. }
  1893. };
  1894. class BeDbgFile : public BeMDNode
  1895. {
  1896. public:
  1897. BE_VALUE_TYPE(BeDbgFile, BeMDNode);
  1898. public:
  1899. String mFileName;
  1900. String mDirectory;
  1901. Val128 mMD5Hash;
  1902. int mIdx;
  1903. void ToString(String& str);
  1904. void GetFilePath(String& outStr);
  1905. virtual void HashContent(BeHashContext& hashCtx) override
  1906. {
  1907. hashCtx.Mixin(TypeId);
  1908. hashCtx.MixinStr(mFileName);
  1909. hashCtx.Mixin(mMD5Hash);
  1910. hashCtx.MixinStr(mDirectory);
  1911. }
  1912. };
  1913. class BeDbgGlobalVariable : public BeMDNode
  1914. {
  1915. public:
  1916. BE_VALUE_TYPE(BeDbgGlobalVariable, BeMDNode);
  1917. BeMDNode* mContext;
  1918. String mName;
  1919. String mLinkageName;
  1920. BeDbgFile* mFile;
  1921. int mLineNum;
  1922. BeMDNode* mType;
  1923. bool mIsLocalToUnit;
  1924. BeConstant* mValue;
  1925. BeMDNode* mDecl;
  1926. virtual void HashContent(BeHashContext& hashCtx) override
  1927. {
  1928. hashCtx.Mixin(TypeId);
  1929. hashCtx.MixinStr(mName);
  1930. hashCtx.MixinStr(mLinkageName);
  1931. if (mFile != NULL)
  1932. mFile->HashReference(hashCtx);
  1933. hashCtx.Mixin(mLineNum);
  1934. mType->HashReference(hashCtx);
  1935. hashCtx.Mixin(mIsLocalToUnit);
  1936. if (mValue != NULL)
  1937. mValue->HashReference(hashCtx);
  1938. if (mDecl != NULL)
  1939. mDecl->HashReference(hashCtx);
  1940. }
  1941. };
  1942. class BeDbgModule : public BeMDNode
  1943. {
  1944. public:
  1945. BE_VALUE_TYPE(BeDbgModule, BeMDNode);
  1946. public:
  1947. BeModule* mBeModule;
  1948. String mFileName;
  1949. String mDirectory;
  1950. String mProducer;
  1951. OwnedVector<BeDbgFile> mFiles;
  1952. OwnedVector<BeDbgNamespace> mNamespaces;
  1953. OwnedVector<BeDbgGlobalVariable> mGlobalVariables;
  1954. OwnedVector<BeMDNode> mTypes;
  1955. Array<BeDbgFunction*> mFuncs; // Does not include methods in structs
  1956. virtual void HashContent(BeHashContext& hashCtx) override;
  1957. BeDbgReferenceType* CreateReferenceType(BeDbgType* dbgType);
  1958. };
  1959. //////////////////////////////////////////////////////////////////////////
  1960. class BeDbgModule;
  1961. class BeModule
  1962. {
  1963. public:
  1964. BeIRCodeGen* mBeIRCodeGen;
  1965. Array<BeConstant*> mConfigConsts32;
  1966. Array<BeConstant*> mConfigConsts64;
  1967. BeFunction* mActiveFunction;
  1968. BumpAllocator mAlloc;
  1969. OwnedVector<BeValue> mOwnedValues; // Those that need dtors
  1970. OwnedVector<BeGlobalVariable> mGlobalVariables;
  1971. BeContext* mContext;
  1972. String mModuleName;
  1973. String mTargetTriple;
  1974. String mTargetCPU;
  1975. BeBlock* mActiveBlock;
  1976. int mInsertPos;
  1977. BeDbgLoc* mCurDbgLoc;
  1978. BeDbgLoc* mPrevDbgLocInline;
  1979. BeDbgLoc* mLastDbgLoc;
  1980. Array<BeArgument*> mArgs;
  1981. Array<BeFunction*> mFunctions;
  1982. Dictionary<String, BeFunction*> mFunctionMap;
  1983. int mCurDbgLocIdx;
  1984. int mCurLexBlockId;
  1985. BeDbgModule* mDbgModule;
  1986. CeMachine* mCeMachine;
  1987. public:
  1988. void AddInst(BeInst* inst);
  1989. static void ToString(StringImpl& str, BeType* type);
  1990. static void StructToString(StringImpl& str, BeStructType* type);
  1991. template <typename T>
  1992. T* AllocInst()
  1993. {
  1994. T* newInst = mAlloc.Alloc<T>();
  1995. AddInst(newInst);
  1996. return newInst;
  1997. }
  1998. template <typename T>
  1999. T* AllocInstOwned()
  2000. {
  2001. T* newInst = mOwnedValues.Alloc<T>();
  2002. AddInst(newInst);
  2003. return newInst;
  2004. }
  2005. public:
  2006. BeModule(const StringImpl& moduleName, BeContext* context);
  2007. ~BeModule();
  2008. void Hash(BeHashContext& hashCtx);
  2009. String ToString(BeFunction* func = NULL);
  2010. void Print();
  2011. void Print(BeFunction* func);
  2012. void PrintValue(BeValue* val);
  2013. void DoInlining(BeFunction* func);
  2014. void DoInlining();
  2015. static BeCmpKind InvertCmp(BeCmpKind cmpKind);
  2016. static BeCmpKind SwapCmpSides(BeCmpKind cmpKind);
  2017. static bool IsCmpOrdered(BeCmpKind cmpKind);
  2018. void SetActiveFunction(BeFunction* function);
  2019. BeArgument* GetArgument(int arg);
  2020. BeBlock* CreateBlock(const StringImpl& name);
  2021. void AddBlock(BeFunction* function, BeBlock* block);
  2022. void RemoveBlock(BeFunction* function, BeBlock* block);
  2023. BeBlock* GetInsertBlock();
  2024. void SetInsertPoint(BeBlock* block);
  2025. void SetInsertPointAtStart(BeBlock* block);
  2026. BeFunction* CreateFunction(BeFunctionType* funcType, BfIRLinkageType linkageType, const StringImpl& name);
  2027. BeDbgLoc* GetCurrentDebugLocation();
  2028. void SetCurrentDebugLocation(BeDbgLoc* dbgLoc);
  2029. void SetCurrentDebugLocation(int line, int column, BeMDNode* diScope, BeDbgLoc* diInlinedAt);
  2030. BeDbgLoc* DupDebugLocation(BeDbgLoc* dbgLoc);
  2031. void DupCurrentDebugLocation();
  2032. ///
  2033. BeNopInst* CreateNop();
  2034. BeUndefValueInst* CreateUndefValue(BeType* type);
  2035. BeNumericCastInst* CreateNumericCast(BeValue* value, BeType* toType, bool valSigned, bool toSigned);
  2036. BeBitCastInst* CreateBitCast(BeValue* value, BeType* toType);;
  2037. BeCmpInst* CreateCmp(BeCmpKind cmpKind, BeValue* lhs, BeValue* rhs);
  2038. BeBinaryOpInst* CreateBinaryOp(BeBinaryOpKind opKind, BeValue* lhs, BeValue* rhs, BfOverflowCheckKind overflowCheckKind = BfOverflowCheckKind_None);
  2039. BeAllocaInst* CreateAlloca(BeType* type);
  2040. BeLoadInst* CreateLoad(BeValue* value, bool isVolatile);
  2041. BeLoadInst* CreateAlignedLoad(BeValue* value, int alignment, bool isVolatile);
  2042. BeStoreInst* CreateStore(BeValue* val, BeValue* ptr, bool isVolatile);
  2043. BeStoreInst* CreateAlignedStore(BeValue* val, BeValue* ptr, int alignment, bool isVolatile);
  2044. BeGEPInst* CreateGEP(BeValue* ptr, BeValue* idx0, BeValue* idx1);
  2045. BeBrInst* CreateBr(BeBlock* block);
  2046. BeCondBrInst* CreateCondBr(BeValue* cond, BeBlock* trueBlock, BeBlock* falseBlock);
  2047. BeRetInst* CreateRetVoid();
  2048. BeRetInst* CreateRet(BeValue* value);
  2049. BeSetRetInst* CreateSetRet(BeValue* value, int returnTypeId);
  2050. BeCallInst* CreateCall(BeValue* func, const SizedArrayImpl<BeValue*>& args);
  2051. BeConstant* GetConstant(BeType* type, double floatVal);
  2052. BeConstant* GetConstant(BeType* type, int64 intVal);
  2053. BeConstant* GetConstant(BeType* type, bool boolVal);
  2054. BeConstant* GetConstantNull(BePointerType* type);
  2055. };
  2056. NS_BF_END