Opnd.cpp 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. namespace IR
  7. {
  8. ///----------------------------------------------------------------------------
  9. ///
  10. /// Opnd::UseWithNewType
  11. ///
  12. /// Creates a Use (a copy if already in use or returns the same)
  13. /// and sets it type
  14. ///
  15. ///----------------------------------------------------------------------------
  16. Opnd*
  17. Opnd::UseWithNewType(IRType type, Func * func)
  18. {
  19. Opnd * res = this->Use(func);
  20. res->SetType(type);
  21. StackSym* sym = res->GetStackSym();
  22. if (sym)
  23. {
  24. if (TySize[sym->GetType()] < TySize[type])
  25. {
  26. Assert(!sym->IsAllocated());
  27. sym->m_type = type;
  28. }
  29. }
  30. return res;
  31. }
  32. bool
  33. Opnd::IsTaggedInt() const
  34. {
  35. return GetValueType().IsTaggedInt();
  36. }
  37. bool
  38. Opnd::IsTaggedValue() const
  39. {
  40. CompileAssert(!FLOATVAR || INT32VAR);
  41. #if FLOATVAR
  42. return GetValueType().IsNumber();
  43. #else
  44. return IsTaggedInt();
  45. #endif
  46. }
  47. bool
  48. Opnd::IsNotNumber() const
  49. {
  50. if (this->GetValueType().IsNotNumber())
  51. {
  52. return true;
  53. }
  54. if (this->IsRegOpnd())
  55. {
  56. const IR::RegOpnd* regOpnd = this->AsRegOpnd();
  57. if (regOpnd->m_sym == nullptr)
  58. {
  59. return true;
  60. }
  61. if (regOpnd->m_sym->m_isNotInt)
  62. {
  63. // m_isNotInt actually means "is not number". It should not be set to true for definitely-float values.
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. bool
  70. Opnd::IsNotInt() const
  71. {
  72. return IsNotNumber() || IsFloat();
  73. }
  74. bool
  75. Opnd::IsNotTaggedValue() const
  76. {
  77. if (!PHASE_OFF1(Js::OptTagChecksPhase) && this->GetValueType().IsNotTaggedValue())
  78. {
  79. return true;
  80. }
  81. return this->IsNotNumber();
  82. }
  83. bool
  84. Opnd::IsWriteBarrierTriggerableValue()
  85. {
  86. // Determines whether if an operand is used as a source in a store instruction, whether the store needs a write barrier
  87. // If it's a tagged value, we don't need a write barrier
  88. if (this->IsTaggedValue())
  89. {
  90. return false;
  91. }
  92. // If this operand is known address, then it doesn't need a write barrier, the address is either not a GC address or is pinned
  93. if (this->IsAddrOpnd() && static_cast<AddrOpndKind>(this->AsAddrOpnd()->GetKind()) == AddrOpndKindDynamicVar)
  94. {
  95. return false;
  96. }
  97. if (TySize[this->GetType()] != sizeof(void*))
  98. {
  99. return false;
  100. }
  101. #if DBG
  102. if (CONFIG_FLAG(ForceSoftwareWriteBarrier) && CONFIG_FLAG(VerifyBarrierBit))
  103. {
  104. return true; // No further optimization if we are in verification
  105. }
  106. #endif
  107. // If its null/boolean/undefined, we don't need a write barrier since the javascript library will keep those guys alive
  108. return !(this->GetValueType().IsBoolean() || this->GetValueType().IsNull() || this->GetValueType().IsUndefined());
  109. }
  110. /*
  111. * This is a devirtualized functions See the note above Opnd:Copy()
  112. */
  113. OpndKind Opnd::GetKind() const
  114. {
  115. return this->m_kind;
  116. }
  117. /*
  118. * This is a devirtualized functions See the note above Opnd:Copy()
  119. */
  120. Opnd *
  121. Opnd::CloneDef(Func *func)
  122. {
  123. switch (this->m_kind)
  124. {
  125. case OpndKindSym:
  126. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  127. {
  128. return static_cast<PropertySymOpnd*>(this)->CloneDefInternalSub(func);
  129. }
  130. return static_cast<SymOpnd*>(this)->CloneDefInternal(func);
  131. case OpndKindReg:
  132. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  133. {
  134. return static_cast<ArrayRegOpnd*>(this)->CloneDefInternalSub(func);
  135. }
  136. return static_cast<RegOpnd*>(this)->CloneDefInternal(func);
  137. case OpndKindIndir:
  138. return static_cast<IndirOpnd*>(this)->CloneDefInternal(func);
  139. default:
  140. return this->Copy(func);
  141. };
  142. }
  143. /*
  144. * This is a devirtualized functions See the note above Opnd:Copy()
  145. */
  146. Opnd *
  147. Opnd::CloneUse(Func *func)
  148. {
  149. switch (this->m_kind)
  150. {
  151. case OpndKindSym:
  152. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  153. {
  154. return static_cast<PropertySymOpnd*>(this)->CloneUseInternalSub(func);
  155. }
  156. return static_cast<SymOpnd*>(this)->CloneUseInternal(func);
  157. case OpndKindReg:
  158. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  159. {
  160. return static_cast<ArrayRegOpnd*>(this)->CloneUseInternalSub(func);
  161. }
  162. return static_cast<RegOpnd*>(this)->CloneUseInternal(func);
  163. case OpndKindIndir:
  164. return static_cast<IndirOpnd*>(this)->CloneUseInternal(func);
  165. default:
  166. return this->Copy(func);
  167. };
  168. }
  169. /*
  170. * This is a devirtualized functions See the note above Opnd:Copy()
  171. */
  172. void Opnd::Free(Func *func)
  173. {
  174. switch (this->m_kind)
  175. {
  176. case OpndKindIntConst:
  177. //NOTE: use to be Sealed do not do sub class checks like in CloneUse
  178. static_cast<IntConstOpnd*>(this)->FreeInternal(func);
  179. break;
  180. case OpndKindInt64Const:
  181. return static_cast<Int64ConstOpnd*>(this)->FreeInternal(func);
  182. case OpndKindSimd128Const:
  183. static_cast<Simd128ConstOpnd*>(this)->FreeInternal(func);
  184. break;
  185. case OpndKindFloatConst:
  186. static_cast<FloatConstOpnd*>(this)->FreeInternal(func);
  187. break;
  188. case OpndKindHelperCall:
  189. static_cast<HelperCallOpnd*>(this)->FreeInternal(func);
  190. break;
  191. case OpndKindSym:
  192. static_cast<SymOpnd*>(this)->FreeInternal(func);
  193. break;
  194. case OpndKindReg:
  195. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  196. {
  197. static_cast<ArrayRegOpnd*>(this)->FreeInternalSub(func);
  198. break;
  199. }
  200. static_cast<RegOpnd*>(this)->FreeInternal(func);
  201. break;
  202. case OpndKindAddr:
  203. static_cast<AddrOpnd*>(this)->FreeInternal(func);
  204. break;
  205. case OpndKindIndir:
  206. static_cast<IndirOpnd*>(this)->FreeInternal(func);
  207. break;
  208. case OpndKindMemRef:
  209. static_cast<MemRefOpnd*>(this)->FreeInternal(func);
  210. break;
  211. case OpndKindLabel:
  212. static_cast<LabelOpnd*>(this)->FreeInternal(func);
  213. break;
  214. case OpndKindRegBV:
  215. static_cast<RegBVOpnd*>(this)->FreeInternal(func);
  216. break;
  217. default:
  218. Assert(UNREACHED);
  219. __assume(UNREACHED);
  220. };
  221. #if DBG
  222. if (func->m_alloc->HasDelayFreeList())
  223. {
  224. this->isDeleted = true;
  225. }
  226. #endif
  227. }
  228. /*
  229. * This is a devirtualized functions See the note above Opnd:Copy()
  230. */
  231. bool Opnd::IsEqual(Opnd *opnd)
  232. {
  233. switch (this->m_kind)
  234. {
  235. case OpndKindIntConst:
  236. return static_cast<IntConstOpnd*>(this)->IsEqualInternal(opnd);
  237. case OpndKindInt64Const:
  238. return static_cast<Int64ConstOpnd*>(this)->IsEqualInternal(opnd);
  239. case OpndKindFloatConst:
  240. return static_cast<FloatConstOpnd*>(this)->IsEqualInternal(opnd);
  241. case OpndKindHelperCall:
  242. if ((*static_cast<HelperCallOpnd*>(this)).IsDiagHelperCallOpnd())
  243. {
  244. return static_cast<DiagHelperCallOpnd*>(this)->IsEqualInternalSub(opnd);
  245. }
  246. return static_cast<HelperCallOpnd*>(this)->IsEqualInternal(opnd);
  247. case OpndKindSym:
  248. //NOTE: use to be Sealed do not do sub class checks like in CloneUse
  249. return static_cast<SymOpnd*>(this)->IsEqualInternal(opnd);
  250. case OpndKindReg:
  251. //NOTE: not sealed but ArrayRegOpnd::isEqual function does not exist, default to RegOpnd only
  252. return static_cast<RegOpnd*>(this)->IsEqualInternal(opnd);
  253. case OpndKindAddr:
  254. return static_cast<AddrOpnd*>(this)->IsEqualInternal(opnd);
  255. case OpndKindIndir:
  256. return static_cast<IndirOpnd*>(this)->IsEqualInternal(opnd);
  257. case OpndKindMemRef:
  258. return static_cast<MemRefOpnd*>(this)->IsEqualInternal(opnd);
  259. case OpndKindLabel:
  260. return static_cast<LabelOpnd*>(this)->IsEqualInternal(opnd);
  261. case OpndKindRegBV:
  262. return static_cast<RegBVOpnd*>(this)->IsEqualInternal(opnd);
  263. default:
  264. Assert(UNREACHED);
  265. __assume(UNREACHED);
  266. };
  267. }
  268. /*
  269. * This is a devirtualized functions If you inherit from any of the child classes of Opnd
  270. * And would like to override the default method behavior you must add an
  271. * Is<your new Opnd Type>Opnd() call and check for it like in examples
  272. * HelperCallOpnd, PropertySymOpnd, & RegOpnd
  273. */
  274. Opnd * Opnd::Copy(Func *func)
  275. {
  276. switch (this->m_kind)
  277. {
  278. case OpndKindIntConst:
  279. return static_cast<IntConstOpnd*>(this)->CopyInternal(func);
  280. case OpndKindInt64Const:
  281. return static_cast<Int64ConstOpnd*>(this)->CopyInternal(func);
  282. case OpndKindFloatConst:
  283. return static_cast<FloatConstOpnd*>(this)->CopyInternal(func);
  284. case OpndKindHelperCall:
  285. if ((*static_cast<HelperCallOpnd*>(this)).IsDiagHelperCallOpnd())
  286. {
  287. return static_cast<DiagHelperCallOpnd*>(this)->CopyInternalSub(func);
  288. }
  289. return static_cast<HelperCallOpnd*>(this)->CopyInternal(func);
  290. case OpndKindSym:
  291. if ((*static_cast<SymOpnd*>(this)).IsPropertySymOpnd())
  292. {
  293. return static_cast<PropertySymOpnd*>(this)->CopyInternalSub(func);
  294. }
  295. return static_cast<SymOpnd*>(this)->CopyInternal(func);
  296. case OpndKindReg:
  297. if ((*static_cast<RegOpnd*>(this)).IsArrayRegOpnd())
  298. {
  299. return static_cast<ArrayRegOpnd*>(this)->CopyInternalSub(func);
  300. }
  301. return static_cast<RegOpnd*>(this)->CopyInternal(func);
  302. case OpndKindAddr:
  303. return static_cast<AddrOpnd*>(this)->CopyInternal(func);
  304. case OpndKindIndir:
  305. return static_cast<IndirOpnd*>(this)->CopyInternal(func);
  306. case OpndKindMemRef:
  307. return static_cast<MemRefOpnd*>(this)->CopyInternal(func);
  308. case OpndKindLabel:
  309. return static_cast<LabelOpnd*>(this)->CopyInternal(func);
  310. case OpndKindRegBV:
  311. return static_cast<RegBVOpnd*>(this)->CopyInternal(func);
  312. default:
  313. Assert(UNREACHED);
  314. __assume(UNREACHED);
  315. };
  316. }
  317. StackSym *
  318. Opnd::GetStackSym() const
  319. {
  320. switch (this->GetKind())
  321. {
  322. case OpndKindSym:
  323. return static_cast<SymOpnd const *>(this)->GetStackSymInternal();
  324. case OpndKindReg:
  325. return static_cast<RegOpnd const *>(this)->GetStackSymInternal();
  326. default:
  327. return nullptr;
  328. }
  329. }
  330. Sym*
  331. Opnd::GetSym() const
  332. {
  333. switch (this->GetKind())
  334. {
  335. case OpndKindSym:
  336. return static_cast<SymOpnd const *>(this)->m_sym;
  337. case OpndKindReg:
  338. return static_cast<RegOpnd const *>(this)->m_sym;
  339. default:
  340. return nullptr;
  341. }
  342. }
  343. int64
  344. Opnd::GetImmediateValue(Func* func)
  345. {
  346. switch (this->GetKind())
  347. {
  348. case OpndKindIntConst:
  349. return this->AsIntConstOpnd()->GetValue();
  350. case OpndKindInt64Const:
  351. return this->AsInt64ConstOpnd()->GetValue();
  352. case OpndKindAddr:
  353. return (intptr_t)this->AsAddrOpnd()->m_address;
  354. case OpndKindHelperCall:
  355. return (intptr_t)IR::GetMethodAddress(func->GetThreadContextInfo(), this->AsHelperCallOpnd());
  356. default:
  357. AssertMsg(UNREACHED, "Unexpected immediate opnd kind");
  358. return 0;
  359. }
  360. }
  361. #if TARGET_32 && !defined(_M_IX86)
  362. int32
  363. Opnd::GetImmediateValueAsInt32(Func * func)
  364. {
  365. Assert(!IRType_IsInt64(this->GetType()));
  366. Assert(this->GetKind() != OpndKindInt64Const);
  367. return (int32)this->GetImmediateValue(func);
  368. }
  369. #endif
  370. BailoutConstantValue Opnd::GetConstValue()
  371. {
  372. BailoutConstantValue value;
  373. if (this->IsIntConstOpnd())
  374. {
  375. value.InitIntConstValue(this->AsIntConstOpnd()->GetValue(), this->m_type);
  376. }
  377. else if (this->IsFloatConstOpnd())
  378. {
  379. value.InitFloatConstValue(this->AsFloatConstOpnd()->m_value);
  380. }
  381. else
  382. {
  383. AssertMsg(this->IsAddrOpnd(), "Unexpected const sym");
  384. value.InitVarConstValue(this->AsAddrOpnd()->m_address);
  385. }
  386. return value;
  387. }
  388. void Opnd::SetValueType(const ValueType valueType)
  389. {
  390. if(m_isValueTypeFixed)
  391. {
  392. return;
  393. }
  394. // ArrayRegOpnd has information specific to the array type, so make sure that doesn't change
  395. Assert(
  396. !IsRegOpnd() ||
  397. !AsRegOpnd()->IsArrayRegOpnd() ||
  398. valueType.IsObject() && valueType.GetObjectType() == m_valueType.GetObjectType());
  399. m_valueType = valueType;
  400. }
  401. bool Opnd::IsScopeObjOpnd(Func * func)
  402. {
  403. if (IsRegOpnd())
  404. {
  405. return this->GetStackSym() == func->GetScopeObjSym();
  406. }
  407. else if(IsSymOpnd() && AsSymOpnd()->m_sym->IsPropertySym())
  408. {
  409. return this->AsSymOpnd()->m_sym->AsPropertySym()->m_stackSym == func->GetScopeObjSym();
  410. }
  411. return false;
  412. }
  413. ValueType Opnd::FindProfiledValueType()
  414. {
  415. if (!this->GetValueType().IsUninitialized())
  416. {
  417. return this->GetValueType();
  418. }
  419. // could be expanded to cover additional opnd kinds as well.
  420. if (this->IsRegOpnd() && this->AsRegOpnd()->m_sym->IsSingleDef())
  421. {
  422. IR::Instr * defInstr = this->AsRegOpnd()->m_sym->GetInstrDef();
  423. IR::Opnd * src1 = defInstr->GetSrc1();
  424. while(defInstr->m_opcode == Js::OpCode::Ld_A)
  425. {
  426. if (!src1->IsRegOpnd() || !src1->AsRegOpnd()->m_sym->IsSingleDef())
  427. {
  428. return ValueType::Uninitialized;
  429. }
  430. defInstr = src1->AsRegOpnd()->m_sym->GetInstrDef();
  431. src1 = defInstr->GetSrc1();
  432. }
  433. if (defInstr->GetDst()->GetValueType().IsAnyArray())
  434. {
  435. return defInstr->GetDst()->GetValueType().ToLikely();
  436. }
  437. else
  438. {
  439. return defInstr->GetDst()->GetValueType();
  440. }
  441. }
  442. return ValueType::Uninitialized;
  443. }
  444. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  445. void Opnd::DumpValueType()
  446. {
  447. if(m_valueType.IsUninitialized())
  448. {
  449. return;
  450. }
  451. if(!CONFIG_FLAG(Verbose))
  452. {
  453. // Skip printing the value type when it's obvious since verbose mode is off
  454. switch(this->GetKind())
  455. {
  456. case OpndKindIntConst:
  457. case OpndKindInt64Const:
  458. case OpndKindFloatConst:
  459. return;
  460. case OpndKindReg:
  461. {
  462. StackSym *const sym = this->AsRegOpnd()->m_sym;
  463. if(sym && (
  464. sym->IsInt32() ||
  465. sym->IsFloat32() ||
  466. sym->IsFloat64() ||
  467. sym->IsInt64() ||
  468. sym->IsUint64()
  469. ))
  470. {
  471. return;
  472. }
  473. break;
  474. }
  475. case OpndKindAddr:
  476. if(this->AsAddrOpnd()->m_address && this->AsAddrOpnd()->IsVar())
  477. {
  478. IR::AddrOpnd *addrOpnd = this->AsAddrOpnd();
  479. Js::Var address = addrOpnd->decodedValue ? addrOpnd->decodedValue : addrOpnd->m_address;
  480. // Tagged int might be encoded here, so check the type
  481. if (addrOpnd->GetAddrOpndKind() == AddrOpndKindConstantVar
  482. || Js::TaggedInt::Is(address) || (
  483. #if !FLOATVAR
  484. !JITManager::GetJITManager()->IsOOPJITEnabled() &&
  485. #endif
  486. Js::JavascriptNumber::Is_NoTaggedIntCheck(address)))
  487. {
  488. return;
  489. }
  490. }
  491. break;
  492. }
  493. }
  494. DumpValueType(m_valueType);
  495. }
  496. void Opnd::DumpValueType(const ValueType valueType)
  497. {
  498. if(valueType.IsUninitialized())
  499. {
  500. return;
  501. }
  502. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  503. valueType.ToString(valueTypeStr);
  504. Output::Print(_u("[%S]"), valueTypeStr);
  505. }
  506. #endif
  507. IntConstOpnd *Opnd::CreateUint32Opnd(const uint i, Func *const func)
  508. {
  509. return IntConstOpnd::New(i, TyUint32, func, true);
  510. }
  511. IntConstOpnd *Opnd::CreateProfileIdOpnd(const Js::ProfileId profileId, Func *const func)
  512. {
  513. CompileAssert(sizeof(profileId) == sizeof(uint16));
  514. return IntConstOpnd::New(profileId, TyUint16, func, true);
  515. }
  516. IntConstOpnd *Opnd::CreateInlineCacheIndexOpnd(const Js::InlineCacheIndex inlineCacheIndex, Func *const func)
  517. {
  518. CompileAssert(sizeof(inlineCacheIndex) == sizeof(uint));
  519. return CreateUint32Opnd(inlineCacheIndex, func);
  520. }
  521. RegOpnd *Opnd::CreateFramePointerOpnd(Func *const func)
  522. {
  523. return RegOpnd::New(nullptr, LowererMD::GetRegFramePointer(), TyMachPtr, func);
  524. }
  525. ///----------------------------------------------------------------------------
  526. ///
  527. /// SymOpnd::New
  528. ///
  529. /// Creates a new SymOpnd.
  530. ///
  531. ///----------------------------------------------------------------------------
  532. SymOpnd *
  533. SymOpnd::New(Sym *sym, IRType type, Func *func)
  534. {
  535. return SymOpnd::New(sym, 0, type, func);
  536. }
  537. SymOpnd *
  538. SymOpnd::New(Sym *sym, uint32 offset, IRType type, Func *func)
  539. {
  540. SymOpnd * symOpnd;
  541. AssertMsg(sym, "A SymOpnd needs a valid symbol.");
  542. symOpnd = JitAnew(func->m_alloc, IR::SymOpnd);
  543. symOpnd->m_sym = sym;
  544. symOpnd->m_offset = offset;
  545. symOpnd->m_type = type;
  546. symOpnd->SetIsJITOptimizedReg(false);
  547. symOpnd->m_kind = OpndKindSym;
  548. return symOpnd;
  549. }
  550. ///----------------------------------------------------------------------------
  551. ///
  552. /// SymOpnd::Copy
  553. ///
  554. /// Returns a copy of this opnd.
  555. ///
  556. ///----------------------------------------------------------------------------
  557. SymOpnd *
  558. SymOpnd::CopyInternal(Func *func)
  559. {
  560. Assert(m_kind == OpndKindSym);
  561. SymOpnd * newOpnd;
  562. newOpnd = SymOpnd::New(m_sym, m_offset, m_type, func);
  563. newOpnd->m_valueType = m_valueType;
  564. newOpnd->canStoreTemp = this->canStoreTemp;
  565. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  566. return newOpnd;
  567. }
  568. SymOpnd *
  569. SymOpnd::CloneDefInternal(Func *func)
  570. {
  571. Assert(m_kind == OpndKindSym);
  572. Sym *sym = this->m_sym;
  573. if (sym->IsStackSym() && sym->AsStackSym()->m_isSingleDef)
  574. {
  575. StackSym * oldSym = sym->AsStackSym();
  576. StackSym * newSym = oldSym->CloneDef(func)->AsStackSym();
  577. if (func->GetCloner()->clonedInstrGetOrigArgSlotSym && oldSym->IsArgSlotSym())
  578. {
  579. Assert(newSym != oldSym);
  580. this->m_sym = newSym;
  581. newSym->m_instrDef = oldSym->m_instrDef;
  582. oldSym->m_instrDef = nullptr;
  583. sym = oldSym;
  584. }
  585. else
  586. {
  587. sym = newSym;
  588. }
  589. }
  590. SymOpnd * newOpnd = SymOpnd::New(sym, m_offset, m_type, func);
  591. return newOpnd;
  592. }
  593. SymOpnd *
  594. SymOpnd::CloneUseInternal(Func *func)
  595. {
  596. Assert(m_kind == OpndKindSym);
  597. Sym *sym = this->m_sym;
  598. if (sym->IsStackSym() && sym->AsStackSym()->m_isSingleDef)
  599. {
  600. StackSym * oldSym = sym->AsStackSym();
  601. StackSym * newSym = oldSym->CloneUse(func)->AsStackSym();
  602. if (func->GetCloner()->clonedInstrGetOrigArgSlotSym && oldSym->IsArgSlotSym())
  603. {
  604. Assert(newSym != oldSym);
  605. this->m_sym = newSym;
  606. sym = oldSym;
  607. }
  608. else
  609. {
  610. sym = newSym;
  611. }
  612. }
  613. SymOpnd * newOpnd = SymOpnd::New(sym, m_offset, m_type, func);
  614. return newOpnd;
  615. }
  616. StackSym *
  617. SymOpnd::GetStackSymInternal() const
  618. {
  619. return (this->m_sym && this->m_sym->IsStackSym()) ? this->m_sym->AsStackSym() : nullptr;
  620. }
  621. ///----------------------------------------------------------------------------
  622. ///
  623. /// SymOpnd::IsEqual
  624. /// The SymOpnd's offset is 0 if it is called before regalloc. For Stack symopnd,
  625. /// compare the type and symbol's offsets only when the symbol's isAllocated is true.
  626. /// For other cases, compare the type, syms and offsets.
  627. /// For example, following two instructions after RegAlloc phase:
  628. /// iarg65535(s534)<0>.i32 = MOV (NULL).var
  629. /// iarg65535(s533)<0>.i32 = MOV (NULL).var
  630. /// are actually same instructions after encoding: mov dword ptr[ebp-0x1c], 0x0
  631. /// Here for dst stack symOpnd, m_sym are different: s534 vs. s533, but offsets and
  632. /// types are the same. So this function will report true if isAllocated is true.
  633. /// Note: for property symopnd, still compare type, offset and sym.
  634. ///
  635. ///----------------------------------------------------------------------------
  636. bool
  637. SymOpnd::IsEqualInternal(Opnd *opnd)
  638. {
  639. Assert(m_kind == OpndKindSym);
  640. Assert(opnd);
  641. if (!opnd->IsSymOpnd() || this->GetType() != opnd->GetType())
  642. {
  643. return false;
  644. }
  645. SymOpnd *opndSym = opnd->AsSymOpnd();
  646. Assert(opndSym);
  647. StackSym *thisStackSym = this->GetStackSymInternal();
  648. StackSym *opndStackSym = opndSym->GetStackSymInternal();
  649. if (thisStackSym && opndStackSym && thisStackSym->IsAllocated() && opndStackSym->IsAllocated())
  650. {
  651. return thisStackSym->m_offset == opndStackSym->m_offset;
  652. }
  653. else
  654. {
  655. return m_sym == opndSym->m_sym && m_offset == opndSym->m_offset;
  656. }
  657. }
  658. void
  659. SymOpnd::FreeInternal(Func *func)
  660. {
  661. Assert(m_kind == OpndKindSym);
  662. JitAdelete(func->m_alloc, this);
  663. }
  664. RegOpnd *SymOpnd::CreatePropertyOwnerOpnd(Func *const func) const
  665. {
  666. Assert(m_sym->IsPropertySym());
  667. Assert(func);
  668. StackSym *const propertyOwnerSym = m_sym->AsPropertySym()->m_stackSym;
  669. RegOpnd *const propertyOwnerOpnd = RegOpnd::New(propertyOwnerSym, propertyOwnerSym->GetType(), func);
  670. propertyOwnerOpnd->SetValueType(GetPropertyOwnerValueType());
  671. return propertyOwnerOpnd;
  672. }
  673. PropertySymOpnd *
  674. PropertySymOpnd::New(PropertySym *propertySym, uint inlineCacheIndex, IRType type, Func *func)
  675. {
  676. PropertySymOpnd *newOpnd = IR::PropertySymOpnd::New(propertySym, type, func);
  677. newOpnd->Init(inlineCacheIndex, func);
  678. return newOpnd;
  679. }
  680. void
  681. PropertySymOpnd::Init(uint inlineCacheIndex, Func *func)
  682. {
  683. this->Init(inlineCacheIndex,
  684. inlineCacheIndex != -1 ? func->GetRuntimeInlineCache(inlineCacheIndex) : 0,
  685. inlineCacheIndex != -1 ? func->GetRuntimePolymorphicInlineCache(inlineCacheIndex) : nullptr,
  686. inlineCacheIndex != -1 ? func->GetObjTypeSpecFldInfo(inlineCacheIndex) : nullptr,
  687. inlineCacheIndex != -1 ? func->GetPolyCacheUtilToInitialize(inlineCacheIndex) : PolymorphicInlineCacheUtilizationMinValue);
  688. }
  689. PropertySymOpnd *
  690. PropertySymOpnd::New(PropertySym *propertySym, IRType type, Func *func)
  691. {
  692. PropertySymOpnd *newOpnd = JitAnew(func->m_alloc, IR::PropertySymOpnd);
  693. newOpnd->m_sym = propertySym;
  694. newOpnd->m_offset = 0;
  695. newOpnd->m_type = type;
  696. newOpnd->SetObjTypeSpecFldInfo(nullptr);
  697. newOpnd->finalType = JITTypeHolder(nullptr);
  698. newOpnd->monoGuardType = JITTypeHolder(nullptr);
  699. newOpnd->guardedPropOps = nullptr;
  700. newOpnd->writeGuards = nullptr;
  701. newOpnd->objTypeSpecFlags = 0;
  702. newOpnd->isPropertySymOpnd = true;
  703. newOpnd->checkedTypeSetIndex = (uint16)-1;
  704. newOpnd->m_kind = OpndKindSym;
  705. return newOpnd;
  706. }
  707. void
  708. PropertySymOpnd::Init(uint inlineCacheIndex, intptr_t runtimeInlineCache, JITTimePolymorphicInlineCache * runtimePolymorphicInlineCache, JITObjTypeSpecFldInfo* objTypeSpecFldInfo, byte polyCacheUtil)
  709. {
  710. this->m_inlineCacheIndex = inlineCacheIndex;
  711. this->m_runtimeInlineCache = runtimeInlineCache;
  712. this->m_runtimePolymorphicInlineCache = runtimePolymorphicInlineCache;
  713. this->m_polyCacheUtil = polyCacheUtil;
  714. this->SetObjTypeSpecFldInfo(objTypeSpecFldInfo);
  715. this->SetIsJITOptimizedReg(false);
  716. }
  717. PropertySymOpnd *
  718. PropertySymOpnd::CopyCommon(Func *func)
  719. {
  720. PropertySymOpnd *newOpnd = PropertySymOpnd::New(this->m_sym->AsPropertySym(), this->m_type, func);
  721. newOpnd->m_valueType = this->m_valueType;
  722. newOpnd->m_inlineCacheIndex = this->m_inlineCacheIndex;
  723. newOpnd->m_runtimeInlineCache = this->m_runtimeInlineCache;
  724. newOpnd->m_runtimePolymorphicInlineCache = this->m_runtimePolymorphicInlineCache;
  725. newOpnd->canStoreTemp = this->canStoreTemp;
  726. return newOpnd;
  727. }
  728. PropertySymOpnd *
  729. PropertySymOpnd::CopyWithoutFlowSensitiveInfo(Func *func)
  730. {
  731. PropertySymOpnd *newOpnd = CopyCommon(func);
  732. newOpnd->SetObjTypeSpecFldInfo(this->objTypeSpecFldInfo);
  733. // This field is not flow sensitive. It is only on if the instruction is CheckFixedMethodFld. If we ever
  734. // hoist CheckFixedMethodFld (or otherwise copy it), we must make sure not to change the opcode.
  735. newOpnd->usesFixedValue = this->usesFixedValue;
  736. // Note that the following fields are flow sensitive. If we're cloning this operand in order to attach it to
  737. // an instruction elsewhere in the flow (e.g. field hoisting or copy propagation), these fields cannot be copied.
  738. // If the caller knows some of them can be safely copied, the caller must do so manually.
  739. Assert(newOpnd->typeCheckSeqFlags == 0);
  740. Assert(newOpnd->finalType == nullptr);
  741. Assert(newOpnd->guardedPropOps == nullptr);
  742. Assert(newOpnd->writeGuards == nullptr);
  743. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  744. return newOpnd;
  745. }
  746. PropertySymOpnd *
  747. PropertySymOpnd::CopyInternalSub(Func *func)
  748. {
  749. Assert(m_kind == OpndKindSym && this->IsPropertySymOpnd());
  750. PropertySymOpnd *newOpnd = CopyCommon(func);
  751. newOpnd->objTypeSpecFldInfo = this->objTypeSpecFldInfo;
  752. newOpnd->usesAuxSlot = usesAuxSlot;
  753. newOpnd->slotIndex = slotIndex;
  754. newOpnd->checkedTypeSetIndex = checkedTypeSetIndex;
  755. newOpnd->objTypeSpecFlags = this->objTypeSpecFlags;
  756. newOpnd->finalType = this->finalType;
  757. newOpnd->guardedPropOps = this->guardedPropOps != nullptr ? this->guardedPropOps->CopyNew() : nullptr;
  758. newOpnd->writeGuards = this->writeGuards != nullptr ? this->writeGuards->CopyNew() : nullptr;
  759. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  760. return newOpnd;
  761. }
  762. bool
  763. PropertySymOpnd::IsObjectHeaderInlined() const
  764. {
  765. JITTypeHolder type(nullptr);
  766. if (this->IsMono())
  767. {
  768. type = this->GetType();
  769. }
  770. else if (this->HasEquivalentTypeSet())
  771. {
  772. type = this->GetFirstEquivalentType();
  773. }
  774. if (type != nullptr && Js::DynamicType::Is(type->GetTypeId()))
  775. {
  776. return type->GetTypeHandler()->IsObjectHeaderInlinedTypeHandler();
  777. }
  778. return false;
  779. }
  780. bool
  781. PropertySymOpnd::ChangesObjectLayout() const
  782. {
  783. JITTypeHolder cachedType = this->IsMono() ? this->GetType() : this->GetFirstEquivalentType();
  784. JITTypeHolder finalType = this->GetFinalType();
  785. if (finalType != nullptr && Js::DynamicType::Is(finalType->GetTypeId()))
  786. {
  787. // This is the case where final type opt may cause pro-active type transition to take place.
  788. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  789. return cachedType->GetTypeHandler()->GetInlineSlotCapacity() != finalType->GetTypeHandler()->GetInlineSlotCapacity() ||
  790. cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() != finalType->GetTypeHandler()->GetOffsetOfInlineSlots();
  791. }
  792. if (!this->HasInitialType())
  793. {
  794. return false;
  795. }
  796. JITTypeHolder initialType = this->GetInitialType();
  797. if (initialType != nullptr && Js::DynamicType::Is(initialType->GetTypeId()))
  798. {
  799. // This is the case where the type transition actually occurs. (This is the only case that's detectable
  800. // during the loop pre-pass, since final types are not in place yet.)
  801. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  802. const JITTypeHandler * cachedTypeHandler = cachedType->GetTypeHandler();
  803. const JITTypeHandler * initialTypeHandler = initialType->GetTypeHandler();
  804. return cachedTypeHandler->GetInlineSlotCapacity() != initialTypeHandler->GetInlineSlotCapacity() ||
  805. cachedTypeHandler->GetOffsetOfInlineSlots() != initialTypeHandler->GetOffsetOfInlineSlots();
  806. }
  807. return false;
  808. }
  809. void
  810. PropertySymOpnd::UpdateSlotForFinalType()
  811. {
  812. JITTypeHolder finalType = this->GetFinalType();
  813. Assert(this->IsMono() || this->checkedTypeSetIndex != (uint16)-1);
  814. JITTypeHolder cachedType =
  815. this->IsMono() ? this->GetType() : this->GetEquivalentTypeSet()->GetType(checkedTypeSetIndex);
  816. Assert(finalType != nullptr && Js::DynamicType::Is(finalType->GetTypeId()));
  817. Assert(cachedType != nullptr && Js::DynamicType::Is(cachedType->GetTypeId()));
  818. if (finalType == cachedType)
  819. {
  820. return;
  821. }
  822. // TODO: OOP JIT: should assert about runtime type handler addr
  823. Assert(cachedType->GetTypeHandler() != finalType->GetTypeHandler());
  824. if (cachedType->GetTypeHandler()->GetInlineSlotCapacity() == finalType->GetTypeHandler()->GetInlineSlotCapacity() &&
  825. cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() == finalType->GetTypeHandler()->GetOffsetOfInlineSlots())
  826. {
  827. // Nothing can change, since the variables aren't changing.
  828. return;
  829. }
  830. // Get the slot index and figure out the property index
  831. uint16 index = this->GetSlotIndex();
  832. if (this->UsesAuxSlot())
  833. {
  834. index += cachedType->GetTypeHandler()->GetInlineSlotCapacity();
  835. }
  836. else
  837. {
  838. index -= cachedType->GetTypeHandler()->GetOffsetOfInlineSlots() / sizeof(Js::Var);
  839. }
  840. // Figure out the slot index and aux-ness from the property index
  841. if (index >= finalType->GetTypeHandler()->GetInlineSlotCapacity())
  842. {
  843. this->SetUsesAuxSlot(true);
  844. index -= finalType->GetTypeHandler()->GetInlineSlotCapacity();
  845. }
  846. else
  847. {
  848. this->SetUsesAuxSlot(false);
  849. index += finalType->GetTypeHandler()->GetOffsetOfInlineSlots() / sizeof(Js::Var);
  850. }
  851. this->SetSlotIndex(index);
  852. }
  853. bool PropertySymOpnd::HasFinalType() const
  854. {
  855. return this->finalType != nullptr;
  856. }
  857. PropertySymOpnd *
  858. PropertySymOpnd::CloneDefInternalSub(Func *func)
  859. {
  860. return this->CopyInternalSub(func);
  861. }
  862. PropertySymOpnd *
  863. PropertySymOpnd::CloneUseInternalSub(Func *func)
  864. {
  865. return this->CopyInternalSub(func);
  866. }
  867. RegOpnd::RegOpnd(StackSym *sym, RegNum reg, IRType type)
  868. {
  869. Initialize(sym, reg, type);
  870. }
  871. RegOpnd::RegOpnd(const RegOpnd &other, StackSym *const sym)
  872. {
  873. Initialize(sym, other.m_reg, other.m_type);
  874. m_valueType = other.m_valueType;
  875. SetIsJITOptimizedReg(other.GetIsJITOptimizedReg());
  876. m_dontDeadStore = other.m_dontDeadStore;
  877. m_wasNegativeZeroPreventedByBailout = other.m_wasNegativeZeroPreventedByBailout;
  878. #if DBG
  879. m_symValueFrozen = other.m_symValueFrozen;
  880. #endif
  881. }
  882. void RegOpnd::Initialize(StackSym *sym, RegNum reg, IRType type)
  883. {
  884. AssertMsg(sym || reg != RegNOREG, "A RegOpnd needs a valid symbol or register.");
  885. Assert(!sym || sym->GetType() != TyMisc);
  886. m_kind = OpndKindReg;
  887. m_sym = sym;
  888. SetReg(reg);
  889. m_type = type;
  890. m_isTempLastUse = false;
  891. m_isCallArg = false;
  892. SetIsJITOptimizedReg(false);
  893. m_dontDeadStore = false;
  894. m_fgPeepTmp = false;
  895. m_wasNegativeZeroPreventedByBailout = false;
  896. m_isArrayRegOpnd = false;
  897. #if DBG
  898. m_symValueFrozen = false;
  899. #endif
  900. }
  901. ///----------------------------------------------------------------------------
  902. ///
  903. /// RegOpnd::New
  904. ///
  905. /// Creates a new RegOpnd.
  906. ///
  907. ///----------------------------------------------------------------------------
  908. RegOpnd *
  909. RegOpnd::New(IRType type, Func *func)
  910. {
  911. return RegOpnd::New(StackSym::New(type, func), RegNOREG, type, func);
  912. }
  913. RegOpnd *
  914. RegOpnd::New(StackSym *sym, IRType type, Func *func)
  915. {
  916. return RegOpnd::New(sym, RegNOREG, type, func);
  917. }
  918. ///----------------------------------------------------------------------------
  919. ///
  920. /// RegOpnd::New
  921. ///
  922. /// Creates a new RegOpnd.
  923. ///
  924. ///----------------------------------------------------------------------------
  925. RegOpnd *
  926. RegOpnd::New(StackSym *sym, RegNum reg, IRType type, Func *func)
  927. {
  928. return JitAnew(func->m_alloc, IR::RegOpnd, sym, reg, type);
  929. }
  930. ///----------------------------------------------------------------------------
  931. ///
  932. /// RegOpnd::Copy
  933. ///
  934. /// Returns a copy of this opnd.
  935. ///
  936. ///----------------------------------------------------------------------------
  937. RegOpnd *
  938. RegOpnd::CopyInternal(StackSym * sym, Func *func)
  939. {
  940. Assert(m_kind == OpndKindReg);
  941. return JitAnew(func->m_alloc, IR::RegOpnd, *this, sym);
  942. }
  943. RegOpnd *
  944. RegOpnd::CopyInternal(Func *func)
  945. {
  946. return CopyInternal(m_sym, func);
  947. }
  948. RegOpnd *
  949. RegOpnd::CloneDefInternal(Func *func)
  950. {
  951. StackSym * sym = m_sym ? m_sym->CloneDef(func) : nullptr;
  952. return CopyInternal(sym, func);
  953. }
  954. RegOpnd *
  955. RegOpnd::CloneUseInternal(Func *func)
  956. {
  957. StackSym * sym = m_sym ? m_sym->CloneUse(func) : nullptr;
  958. return CopyInternal(sym, func);
  959. }
  960. StackSym *
  961. RegOpnd::GetStackSymInternal() const
  962. {
  963. return this->m_sym;
  964. }
  965. StackSym *
  966. RegOpnd::TryGetStackSym(Opnd *const opnd)
  967. {
  968. return opnd && opnd->IsRegOpnd() ? opnd->AsRegOpnd()->m_sym : nullptr;
  969. }
  970. ///----------------------------------------------------------------------------
  971. ///
  972. /// RegOpnd::IsEqual
  973. ///
  974. ///----------------------------------------------------------------------------
  975. bool
  976. RegOpnd::IsEqualInternal(Opnd *opnd)
  977. {
  978. Assert(m_kind == OpndKindReg);
  979. return IsSameRegUntyped(opnd) && (this->GetType() == opnd->GetType());
  980. }
  981. void
  982. RegOpnd::FreeInternal(Func *func)
  983. {
  984. Assert(m_kind == OpndKindReg);
  985. JitAdelete(func->m_alloc, this);
  986. }
  987. ///----------------------------------------------------------------------------
  988. ///
  989. /// RegOpnd::IsSameReg
  990. ///
  991. /// Same as IsEqual except the type only need to be equal size
  992. ///
  993. ///----------------------------------------------------------------------------
  994. bool
  995. RegOpnd::IsSameReg(Opnd *opnd)
  996. {
  997. return IsSameRegUntyped(opnd) && (TySize[this->GetType()] == TySize[opnd->GetType()]);
  998. }
  999. ///----------------------------------------------------------------------------
  1000. ///
  1001. /// RegOpnd::IsSameRegUntyped
  1002. ///
  1003. /// Same as IsEqual but without any types comparison
  1004. ///
  1005. ///----------------------------------------------------------------------------
  1006. bool
  1007. RegOpnd::IsSameRegUntyped(Opnd *opnd)
  1008. {
  1009. if (!opnd->IsRegOpnd())
  1010. {
  1011. return false;
  1012. }
  1013. RegOpnd *regOpnd = opnd->AsRegOpnd();
  1014. if (m_reg != RegNOREG)
  1015. {
  1016. return m_reg == regOpnd->m_reg;
  1017. }
  1018. return m_sym == regOpnd->m_sym && regOpnd->m_reg == RegNOREG;
  1019. }
  1020. ///----------------------------------------------------------------------------
  1021. ///
  1022. /// ArrayRegOpnd
  1023. ///
  1024. ///----------------------------------------------------------------------------
  1025. ArrayRegOpnd::ArrayRegOpnd(
  1026. StackSym *const arraySym,
  1027. const ValueType valueType,
  1028. StackSym *const headSegmentSym,
  1029. StackSym *const headSegmentLengthSym,
  1030. StackSym *const lengthSym,
  1031. const bool eliminatedLowerBoundCheck,
  1032. const bool eliminatedUpperBoundCheck)
  1033. : RegOpnd(arraySym, RegNOREG, TyVar),
  1034. headSegmentSym(headSegmentSym),
  1035. headSegmentLengthSym(headSegmentLengthSym),
  1036. lengthSym(lengthSym),
  1037. eliminatedLowerBoundCheck(eliminatedLowerBoundCheck),
  1038. eliminatedUpperBoundCheck(eliminatedUpperBoundCheck)
  1039. {
  1040. Assert(valueType.IsAnyOptimizedArray());
  1041. m_valueType = valueType;
  1042. m_isArrayRegOpnd = true;
  1043. }
  1044. ArrayRegOpnd::ArrayRegOpnd(
  1045. const RegOpnd &other,
  1046. StackSym *const arraySym,
  1047. const ValueType valueType,
  1048. StackSym *const headSegmentSym,
  1049. StackSym *const headSegmentLengthSym,
  1050. StackSym *const lengthSym,
  1051. const bool eliminatedLowerBoundCheck,
  1052. const bool eliminatedUpperBoundCheck)
  1053. : RegOpnd(other, arraySym),
  1054. headSegmentSym(headSegmentSym),
  1055. headSegmentLengthSym(headSegmentLengthSym),
  1056. lengthSym(lengthSym),
  1057. eliminatedLowerBoundCheck(eliminatedLowerBoundCheck),
  1058. eliminatedUpperBoundCheck(eliminatedUpperBoundCheck)
  1059. {
  1060. Assert(valueType.IsAnyOptimizedArray());
  1061. m_valueType = valueType;
  1062. m_isArrayRegOpnd = true;
  1063. }
  1064. ArrayRegOpnd *ArrayRegOpnd::New(
  1065. StackSym *const arraySym,
  1066. const ValueType valueType,
  1067. StackSym *const headSegmentSym,
  1068. StackSym *const headSegmentLengthSym,
  1069. StackSym *const lengthSym,
  1070. const bool eliminatedLowerBoundCheck,
  1071. const bool eliminatedUpperBoundCheck,
  1072. Func *const func)
  1073. {
  1074. Assert(func);
  1075. return
  1076. JitAnew(
  1077. func->m_alloc,
  1078. ArrayRegOpnd,
  1079. arraySym,
  1080. valueType,
  1081. headSegmentSym,
  1082. headSegmentLengthSym,
  1083. lengthSym,
  1084. eliminatedLowerBoundCheck,
  1085. eliminatedUpperBoundCheck);
  1086. }
  1087. ArrayRegOpnd *ArrayRegOpnd::New(
  1088. const RegOpnd *const other,
  1089. const ValueType valueType,
  1090. StackSym *const headSegmentSym,
  1091. StackSym *const headSegmentLengthSym,
  1092. StackSym *const lengthSym,
  1093. const bool eliminatedLowerBoundCheck,
  1094. const bool eliminatedUpperBoundCheck,
  1095. Func *const func)
  1096. {
  1097. Assert(func);
  1098. return
  1099. JitAnew(
  1100. func->m_alloc,
  1101. ArrayRegOpnd,
  1102. *other,
  1103. other->m_sym,
  1104. valueType,
  1105. headSegmentSym,
  1106. headSegmentLengthSym,
  1107. lengthSym,
  1108. eliminatedLowerBoundCheck,
  1109. eliminatedUpperBoundCheck);
  1110. }
  1111. RegOpnd *ArrayRegOpnd::CopyAsRegOpnd(Func *func)
  1112. {
  1113. RegOpnd *const regOpndCopy = RegOpnd::CopyInternal(func);
  1114. Assert(!regOpndCopy->IsArrayRegOpnd());
  1115. return regOpndCopy;
  1116. }
  1117. ArrayRegOpnd *ArrayRegOpnd::CopyInternalSub(Func *func)
  1118. {
  1119. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1120. return Clone(m_sym, headSegmentSym, headSegmentLengthSym, lengthSym, func);
  1121. }
  1122. ArrayRegOpnd *ArrayRegOpnd::CloneDefInternalSub(Func *func)
  1123. {
  1124. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1125. return
  1126. Clone(
  1127. m_sym ? m_sym->CloneDef(func) : nullptr,
  1128. headSegmentSym ? headSegmentSym->CloneUse(func) : nullptr,
  1129. headSegmentLengthSym ? headSegmentLengthSym->CloneUse(func) : nullptr,
  1130. lengthSym ? lengthSym->CloneUse(func) : nullptr,
  1131. func);
  1132. }
  1133. ArrayRegOpnd *ArrayRegOpnd::CloneUseInternalSub(Func *func)
  1134. {
  1135. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1136. return
  1137. Clone(
  1138. m_sym ? m_sym->CloneUse(func) : nullptr,
  1139. headSegmentSym ? headSegmentSym->CloneUse(func) : nullptr,
  1140. headSegmentLengthSym ? headSegmentLengthSym->CloneUse(func) : nullptr,
  1141. lengthSym ? lengthSym->CloneUse(func) : nullptr,
  1142. func);
  1143. }
  1144. ArrayRegOpnd *ArrayRegOpnd::Clone(
  1145. StackSym *const arraySym,
  1146. StackSym *const headSegmentSym,
  1147. StackSym *const headSegmentLengthSym,
  1148. StackSym *const lengthSym,
  1149. Func *const func) const
  1150. {
  1151. Assert(func);
  1152. // Careful how clones are used. Only GlobOpt knows when it's valid to use the information in this class, so ideally cloning
  1153. // should be done only at lowering time.
  1154. return
  1155. JitAnew(
  1156. func->m_alloc,
  1157. ArrayRegOpnd,
  1158. *this,
  1159. arraySym,
  1160. m_valueType,
  1161. headSegmentSym,
  1162. headSegmentLengthSym,
  1163. lengthSym,
  1164. eliminatedLowerBoundCheck,
  1165. eliminatedUpperBoundCheck);
  1166. }
  1167. void ArrayRegOpnd::FreeInternalSub(Func *func)
  1168. {
  1169. Assert(m_kind == OpndKindReg && this->IsArrayRegOpnd());
  1170. JitAdelete(func->m_alloc, this);
  1171. }
  1172. ///----------------------------------------------------------------------------
  1173. ///
  1174. /// IntConstOpnd::New
  1175. ///
  1176. /// Creates a new IntConstOpnd.
  1177. ///
  1178. ///----------------------------------------------------------------------------
  1179. IntConstOpnd *
  1180. IntConstOpnd::New(IntConstType value, IRType type, Func *func, bool dontEncode)
  1181. {
  1182. IntConstOpnd * intConstOpnd;
  1183. Assert(TySize[type] <= sizeof(IntConstType));
  1184. intConstOpnd = JitAnew(func->m_alloc, IR::IntConstOpnd);
  1185. intConstOpnd->m_type = type;
  1186. intConstOpnd->m_kind = OpndKindIntConst;
  1187. intConstOpnd->m_dontEncode = dontEncode;
  1188. intConstOpnd->SetValue(value);
  1189. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1190. intConstOpnd->decodedValue = 0;
  1191. intConstOpnd->name = nullptr;
  1192. #endif
  1193. return intConstOpnd;
  1194. }
  1195. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1196. IntConstOpnd *
  1197. IntConstOpnd::New(IntConstType value, IRType type, const char16 * name, Func *func, bool dontEncode)
  1198. {
  1199. IntConstOpnd * intConstOpnd = IntConstOpnd::New(value, type, func, dontEncode);
  1200. intConstOpnd->name = name;
  1201. return intConstOpnd;
  1202. }
  1203. #endif
  1204. ///----------------------------------------------------------------------------
  1205. ///
  1206. /// IntConstOpnd::CreateIntConstOpndFromType
  1207. ///
  1208. /// Create an IntConstOpnd or Int64ConstOpnd depending on the IRType.
  1209. ///
  1210. ///----------------------------------------------------------------------------
  1211. IR::Opnd* IntConstOpnd::NewFromType(int64 value, IRType type, Func* func)
  1212. {
  1213. if (IRType_IsInt64(type))
  1214. {
  1215. return Int64ConstOpnd::New(value, type, func);
  1216. }
  1217. Assert(value < (int64)UINT_MAX);
  1218. return IntConstOpnd::New((IntConstType)value, type, func);
  1219. }
  1220. ///----------------------------------------------------------------------------
  1221. ///
  1222. /// IntConstOpnd::Copy
  1223. ///
  1224. /// Returns a copy of this opnd.
  1225. ///
  1226. ///----------------------------------------------------------------------------
  1227. IntConstOpnd *
  1228. IntConstOpnd::CopyInternal(Func *func)
  1229. {
  1230. Assert(m_kind == OpndKindIntConst);
  1231. IntConstOpnd * newOpnd;
  1232. newOpnd = IntConstOpnd::New(m_value, m_type, func, m_dontEncode);
  1233. newOpnd->m_valueType = m_valueType;
  1234. return newOpnd;
  1235. }
  1236. ///----------------------------------------------------------------------------
  1237. ///
  1238. /// IntConstOpnd::IsEqual
  1239. ///
  1240. ///----------------------------------------------------------------------------
  1241. bool
  1242. IntConstOpnd::IsEqualInternal(Opnd *opnd)
  1243. {
  1244. Assert(m_kind == OpndKindIntConst);
  1245. if (!opnd->IsIntConstOpnd() || this->GetType() != opnd->GetType())
  1246. {
  1247. return false;
  1248. }
  1249. return m_value == opnd->AsIntConstOpnd()->m_value;
  1250. }
  1251. void
  1252. IntConstOpnd::FreeInternal(Func *func)
  1253. {
  1254. Assert(m_kind == OpndKindIntConst);
  1255. JitAdelete(func->m_alloc, this);
  1256. }
  1257. ///----------------------------------------------------------------------------
  1258. ///
  1259. /// IntConstOpnd::SetValue
  1260. ///
  1261. /// Modifies the value of the IntConstOpnd
  1262. ///
  1263. ///----------------------------------------------------------------------------
  1264. void
  1265. IntConstOpnd::SetValue(IntConstType value)
  1266. {
  1267. if (sizeof(IntConstType) > sizeof(int32))
  1268. {
  1269. Assert(m_type != TyInt32 || (value >= INT32_MIN && value <= INT32_MAX));
  1270. Assert(m_type != TyUint32 || (value >= 0 && value <= UINT32_MAX));
  1271. }
  1272. // TODO: These should be uncommented, unfortunately, Lowerer::UseWithNewType
  1273. // can change m_type (by calling SetType) in such a way that it violates these constraints.
  1274. // If CopyInternal is later called on the IntConstOpnd, these will fail.
  1275. // Assert(m_type != TyInt16 || (value >= INT16_MIN && value <= INT16_MAX));
  1276. // Assert(m_type != TyUint16 || (value >= 0 && value <= UINT16_MAX));
  1277. // Assert(m_type != TyInt8 || (value >= INT8_MIN && value <= INT8_MAX));
  1278. // Assert(m_type != TyUint8 || (value >= 0 && value <= UINT8_MAX));
  1279. m_value = value;
  1280. }
  1281. ///----------------------------------------------------------------------------
  1282. ///
  1283. /// IntConstOpnd::AsInt32
  1284. ///
  1285. /// Retrieves the value of the int const opnd as a signed 32-bit integer.
  1286. ///
  1287. ///----------------------------------------------------------------------------
  1288. int32
  1289. IntConstOpnd::AsInt32()
  1290. {
  1291. // TODO: Currently, there are cases where we construct IntConstOpnd with TyInt32
  1292. // and retrieve value out as uint32 (or vice versa). Because of these, we allow
  1293. // AsInt32/AsUint32 to cast between int32/uint32 in a lossy manner for now.
  1294. // In the future, we should tighten up usage of IntConstOpnd to avoid these casts
  1295. if (sizeof(IntConstType) == sizeof(int32))
  1296. {
  1297. return (int32)m_value;
  1298. }
  1299. if (m_type == TyUint32)
  1300. {
  1301. Assert(m_value >= 0 && m_value <= UINT32_MAX);
  1302. return (int32)(uint32)m_value;
  1303. }
  1304. Assert(Math::FitsInDWord(m_value));
  1305. return (int32)m_value;
  1306. }
  1307. ///----------------------------------------------------------------------------
  1308. ///
  1309. /// IntConstOpnd::AsUint32
  1310. ///
  1311. /// Retrieves the value of the int const opnd as an unsigned 32-bit integer.
  1312. ///
  1313. ///----------------------------------------------------------------------------
  1314. uint32
  1315. IntConstOpnd::AsUint32()
  1316. {
  1317. // TODO: See comment in AsInt32() regarding casts from int32 to uint32
  1318. if (sizeof(uint32) == sizeof(IntConstType))
  1319. {
  1320. return (uint32)m_value;
  1321. }
  1322. Assert(sizeof(uint32) < sizeof(IntConstType));
  1323. Assert(m_value >= 0 && m_value <= UINT32_MAX);
  1324. return (uint32)m_value;
  1325. }
  1326. ///----------------------------------------------------------------------------
  1327. ///
  1328. /// Int64ConstOpnd Methods
  1329. ///
  1330. ///----------------------------------------------------------------------------
  1331. IR::Int64ConstOpnd* Int64ConstOpnd::New(int64 value, IRType type, Func *func)
  1332. {
  1333. AssertMsg(func->GetJITFunctionBody()->IsWasmFunction(), "Only WebAssembly functions should have int64 const operands. Use IntConstOpnd for size_t type");
  1334. Int64ConstOpnd * intConstOpnd;
  1335. Assert(TySize[type] == sizeof(int64));
  1336. intConstOpnd = JitAnew(func->m_alloc, IR::Int64ConstOpnd);
  1337. intConstOpnd->m_type = type;
  1338. intConstOpnd->m_kind = OpndKindInt64Const;
  1339. intConstOpnd->m_value = value;
  1340. return intConstOpnd;
  1341. }
  1342. IR::Int64ConstOpnd* Int64ConstOpnd::CopyInternal(Func *func)
  1343. {
  1344. Assert(m_kind == OpndKindInt64Const);
  1345. Int64ConstOpnd * newOpnd;
  1346. newOpnd = Int64ConstOpnd::New(m_value, m_type, func);
  1347. newOpnd->m_valueType = m_valueType;
  1348. return newOpnd;
  1349. }
  1350. bool Int64ConstOpnd::IsEqualInternal(Opnd *opnd)
  1351. {
  1352. Assert(m_kind == OpndKindInt64Const);
  1353. if (!opnd->IsInt64ConstOpnd() || this->GetType() != opnd->GetType())
  1354. {
  1355. return false;
  1356. }
  1357. return m_value == opnd->AsInt64ConstOpnd()->m_value;
  1358. }
  1359. void Int64ConstOpnd::FreeInternal(Func * func)
  1360. {
  1361. Assert(m_kind == OpndKindInt64Const);
  1362. JitAdelete(func->m_alloc, this);
  1363. }
  1364. int64 Int64ConstOpnd::GetValue()
  1365. {
  1366. Assert(m_type == TyInt64);
  1367. return m_value;
  1368. }
  1369. ///----------------------------------------------------------------------------
  1370. ///
  1371. /// RegBVOpnd::New
  1372. ///
  1373. /// Creates a new IntConstOpnd.
  1374. ///
  1375. ///----------------------------------------------------------------------------
  1376. RegBVOpnd *
  1377. RegBVOpnd::New(BVUnit32 value, IRType type, Func *func)
  1378. {
  1379. RegBVOpnd * regBVOpnd;
  1380. regBVOpnd = JitAnew(func->m_alloc, IR::RegBVOpnd);
  1381. regBVOpnd->m_value.Copy(value);
  1382. regBVOpnd->m_type = type;
  1383. regBVOpnd->m_kind = OpndKindRegBV;
  1384. return regBVOpnd;
  1385. }
  1386. ///----------------------------------------------------------------------------
  1387. ///
  1388. /// RegBVOpnd::Copy
  1389. ///
  1390. /// Returns a copy of this opnd.
  1391. ///
  1392. ///----------------------------------------------------------------------------
  1393. RegBVOpnd *
  1394. RegBVOpnd::CopyInternal(Func *func)
  1395. {
  1396. Assert(m_kind == OpndKindRegBV);
  1397. RegBVOpnd * newOpnd;
  1398. newOpnd = RegBVOpnd::New(m_value, m_type, func);
  1399. newOpnd->m_valueType = m_valueType;
  1400. return newOpnd;
  1401. }
  1402. ///----------------------------------------------------------------------------
  1403. ///
  1404. /// RegBVOpnd::IsEqual
  1405. ///
  1406. ///----------------------------------------------------------------------------
  1407. bool
  1408. RegBVOpnd::IsEqualInternal(Opnd *opnd)
  1409. {
  1410. Assert(m_kind == OpndKindRegBV);
  1411. if (!opnd->IsRegBVOpnd() || this->GetType() != opnd->GetType())
  1412. {
  1413. return false;
  1414. }
  1415. return m_value.Equal(opnd->AsRegBVOpnd()->m_value);
  1416. }
  1417. void
  1418. RegBVOpnd::FreeInternal(Func *func)
  1419. {
  1420. Assert(m_kind == OpndKindRegBV);
  1421. JitAdelete(func->m_alloc, this);
  1422. }
  1423. ///----------------------------------------------------------------------------
  1424. ///
  1425. /// FloatConstOpnd::New
  1426. ///
  1427. /// Creates a new FloatConstOpnd.
  1428. ///
  1429. ///----------------------------------------------------------------------------
  1430. FloatConstOpnd *
  1431. FloatConstOpnd::New(FloatConstType value, IRType type, Func *func)
  1432. {
  1433. FloatConstOpnd * floatConstOpnd;
  1434. floatConstOpnd = JitAnew(func->m_alloc, IR::FloatConstOpnd);
  1435. floatConstOpnd->m_value = value;
  1436. floatConstOpnd->m_type = type;
  1437. #if !FLOATVAR
  1438. floatConstOpnd->m_number = nullptr;
  1439. #endif
  1440. floatConstOpnd->m_kind = OpndKindFloatConst;
  1441. return floatConstOpnd;
  1442. }
  1443. FloatConstOpnd *
  1444. FloatConstOpnd::New(Js::Var floatVar, IRType type, Func *func, Js::Var varLocal /*= nullptr*/)
  1445. {
  1446. Assert((varLocal && Js::JavascriptNumber::Is(varLocal)) || Js::JavascriptNumber::Is(floatVar));
  1447. FloatConstType value = Js::JavascriptNumber::GetValue(varLocal ? varLocal : floatVar);
  1448. FloatConstOpnd * floatConstOpnd = FloatConstOpnd::New(value, type, func);
  1449. #if !FLOATVAR
  1450. floatConstOpnd->m_number = floatVar;
  1451. floatConstOpnd->m_numberCopy = (Js::JavascriptNumber*)varLocal;
  1452. #endif
  1453. return floatConstOpnd;
  1454. }
  1455. AddrOpnd *
  1456. FloatConstOpnd::GetAddrOpnd(Func *func, bool dontEncode)
  1457. {
  1458. #if !FLOATVAR
  1459. if (this->m_number)
  1460. {
  1461. return AddrOpnd::New(this->m_number, (Js::TaggedNumber::Is(this->m_number) ? AddrOpndKindConstantVar : AddrOpndKindDynamicVar), func, dontEncode, this->m_numberCopy);
  1462. }
  1463. #endif
  1464. IR::AddrOpnd *opnd = AddrOpnd::NewFromNumber(this->m_value, func, dontEncode);
  1465. #if !FLOATVAR
  1466. this->m_number = opnd->m_address;
  1467. #endif
  1468. return opnd;
  1469. }
  1470. ///----------------------------------------------------------------------------
  1471. ///
  1472. /// FloatConstOpnd::Copy
  1473. ///
  1474. /// Returns a copy of this opnd.
  1475. ///
  1476. ///----------------------------------------------------------------------------
  1477. FloatConstOpnd *
  1478. FloatConstOpnd::CopyInternal(Func *func)
  1479. {
  1480. Assert(m_kind == OpndKindFloatConst);
  1481. FloatConstOpnd * newOpnd;
  1482. newOpnd = FloatConstOpnd::New(m_value, m_type, func);
  1483. newOpnd->m_valueType = m_valueType;
  1484. return newOpnd;
  1485. }
  1486. ///----------------------------------------------------------------------------
  1487. ///
  1488. /// FloatConstOpnd::IsEqual
  1489. ///
  1490. ///----------------------------------------------------------------------------
  1491. bool
  1492. FloatConstOpnd::IsEqualInternal(Opnd *opnd)
  1493. {
  1494. Assert(m_kind == OpndKindFloatConst);
  1495. if (!opnd->IsFloatConstOpnd() || this->GetType() != opnd->GetType())
  1496. {
  1497. return false;
  1498. }
  1499. return m_value == opnd->AsFloatConstOpnd()->m_value;
  1500. }
  1501. void
  1502. FloatConstOpnd::FreeInternal(Func *func)
  1503. {
  1504. Assert(m_kind == OpndKindFloatConst);
  1505. JitAdelete(func->m_alloc, this);
  1506. }
  1507. ///----------------------------------------------------------------------------
  1508. ///
  1509. /// Simd128ConstOpnd::New
  1510. ///
  1511. /// Creates a new FloatConstOpnd.
  1512. ///
  1513. ///----------------------------------------------------------------------------
  1514. Simd128ConstOpnd *
  1515. Simd128ConstOpnd::New(AsmJsSIMDValue value, IRType type, Func *func)
  1516. {
  1517. Simd128ConstOpnd * simd128ConstOpnd;
  1518. simd128ConstOpnd = JitAnew(func->m_alloc, IR::Simd128ConstOpnd);
  1519. simd128ConstOpnd->m_value = value;
  1520. simd128ConstOpnd->m_type = type;
  1521. simd128ConstOpnd->m_kind = OpndKindSimd128Const;
  1522. return simd128ConstOpnd;
  1523. }
  1524. ///----------------------------------------------------------------------------
  1525. ///
  1526. /// Simd128ConstOpnd::Copy
  1527. ///
  1528. /// Returns a copy of this opnd.
  1529. ///
  1530. ///----------------------------------------------------------------------------
  1531. Simd128ConstOpnd *
  1532. Simd128ConstOpnd::CopyInternal(Func *func)
  1533. {
  1534. Assert(m_kind == OpndKindSimd128Const);
  1535. Simd128ConstOpnd * newOpnd;
  1536. newOpnd = Simd128ConstOpnd::New(m_value, m_type, func);
  1537. newOpnd->m_valueType = m_valueType;
  1538. return newOpnd;
  1539. }
  1540. ///----------------------------------------------------------------------------
  1541. ///
  1542. /// Simd128ConstOpnd::IsEqual
  1543. ///
  1544. ///----------------------------------------------------------------------------
  1545. bool
  1546. Simd128ConstOpnd::IsEqualInternal(Opnd *opnd)
  1547. {
  1548. Assert(m_kind == OpndKindSimd128Const);
  1549. if (!opnd->IsSimd128ConstOpnd() || this->GetType() != opnd->GetType())
  1550. {
  1551. return false;
  1552. }
  1553. return m_value == opnd->AsSimd128ConstOpnd()->m_value;
  1554. }
  1555. void
  1556. Simd128ConstOpnd::FreeInternal(Func *func)
  1557. {
  1558. Assert(m_kind == OpndKindSimd128Const);
  1559. JitAdelete(func->m_alloc, this);
  1560. }
  1561. ///----------------------------------------------------------------------------
  1562. ///
  1563. /// HelperCallOpnd::New
  1564. ///
  1565. /// Creates a new HelperCallOpnd.
  1566. ///
  1567. ///----------------------------------------------------------------------------
  1568. HelperCallOpnd *
  1569. HelperCallOpnd::New(JnHelperMethod fnHelper, Func *func)
  1570. {
  1571. HelperCallOpnd *helperCallOpnd = JitAnew(func->m_alloc, IR::HelperCallOpnd);
  1572. helperCallOpnd->Init(fnHelper);
  1573. return helperCallOpnd;
  1574. }
  1575. void
  1576. HelperCallOpnd::Init(JnHelperMethod fnHelper)
  1577. {
  1578. this->m_fnHelper = fnHelper;
  1579. this->m_type = TyMachPtr;
  1580. this->m_kind = OpndKindHelperCall;
  1581. }
  1582. ///----------------------------------------------------------------------------
  1583. ///
  1584. /// HelperCallOpnd::Copy
  1585. ///
  1586. /// Returns a copy of this opnd.
  1587. ///
  1588. ///----------------------------------------------------------------------------
  1589. HelperCallOpnd *
  1590. HelperCallOpnd::CopyInternal(Func *func)
  1591. {
  1592. Assert(m_kind == OpndKindHelperCall);
  1593. HelperCallOpnd *const newOpnd = HelperCallOpnd::New(m_fnHelper, func);
  1594. newOpnd->m_valueType = m_valueType;
  1595. return newOpnd;
  1596. }
  1597. ///----------------------------------------------------------------------------
  1598. ///
  1599. /// HelperCallOpnd::IsEqual
  1600. ///
  1601. ///----------------------------------------------------------------------------
  1602. bool
  1603. HelperCallOpnd::IsEqualInternal(Opnd *opnd)
  1604. {
  1605. Assert(m_kind == OpndKindHelperCall);
  1606. if (!opnd->IsHelperCallOpnd())
  1607. {
  1608. return false;
  1609. }
  1610. return m_fnHelper == opnd->AsHelperCallOpnd()->m_fnHelper;
  1611. }
  1612. void
  1613. HelperCallOpnd::FreeInternal(Func *func)
  1614. {
  1615. Assert(m_kind == OpndKindHelperCall);
  1616. JitAdelete(func->m_alloc, this);
  1617. }
  1618. DiagHelperCallOpnd *
  1619. DiagHelperCallOpnd::New(JnHelperMethod fnHelper, Func *func, int argCount)
  1620. {
  1621. DiagHelperCallOpnd *helperCallOpnd = JitAnew(func->m_alloc, IR::DiagHelperCallOpnd);
  1622. helperCallOpnd->Init(fnHelper);
  1623. helperCallOpnd->m_argCount = argCount;
  1624. helperCallOpnd->isDiagHelperCallOpnd = true;
  1625. return helperCallOpnd;
  1626. }
  1627. DiagHelperCallOpnd *
  1628. DiagHelperCallOpnd::CopyInternalSub(Func *func)
  1629. {
  1630. Assert(m_kind == OpndKindHelperCall && this->IsDiagHelperCallOpnd());
  1631. DiagHelperCallOpnd *const newOpnd = DiagHelperCallOpnd::New(m_fnHelper, func, m_argCount);
  1632. newOpnd->m_valueType = m_valueType;
  1633. return newOpnd;
  1634. }
  1635. bool
  1636. DiagHelperCallOpnd::IsEqualInternalSub(Opnd *opnd)
  1637. {
  1638. Assert(m_kind == OpndKindHelperCall && this->IsDiagHelperCallOpnd());
  1639. if (!opnd->IsHelperCallOpnd() || !opnd->AsHelperCallOpnd()->IsDiagHelperCallOpnd())
  1640. {
  1641. return false;
  1642. }
  1643. return
  1644. m_fnHelper == opnd->AsHelperCallOpnd()->m_fnHelper &&
  1645. m_argCount == static_cast<DiagHelperCallOpnd*>(opnd)->m_argCount;
  1646. }
  1647. ///----------------------------------------------------------------------------
  1648. ///
  1649. /// AddrOpnd::New
  1650. ///
  1651. /// Creates a new AddrOpnd.
  1652. ///
  1653. ///----------------------------------------------------------------------------
  1654. AddrOpnd *
  1655. AddrOpnd::New(intptr_t address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode /* = false */, Js::Var varLocal /* = nullptr*/)
  1656. {
  1657. AddrOpnd * addrOpnd;
  1658. addrOpnd = JitAnew(func->m_alloc, IR::AddrOpnd);
  1659. // TODO (michhol): OOP JIT, use intptr_t instead of Js::Var by default so people don't try to dereference
  1660. addrOpnd->m_address = (Js::Var)address;
  1661. addrOpnd->m_localAddress = func->IsOOPJIT() ? varLocal : (Js::Var)address;
  1662. addrOpnd->addrOpndKind = addrOpndKind;
  1663. addrOpnd->m_type = addrOpnd->IsVar() ? TyVar : TyMachPtr;
  1664. addrOpnd->m_dontEncode = dontEncode;
  1665. addrOpnd->m_isFunction = false;
  1666. if (address && addrOpnd->IsVar())
  1667. {
  1668. if (Js::TaggedInt::Is(address))
  1669. {
  1670. addrOpnd->m_valueType = ValueType::GetTaggedInt();
  1671. addrOpnd->SetValueTypeFixed();
  1672. }
  1673. else if (
  1674. #if !FLOATVAR
  1675. !func->IsOOPJIT() && CONFIG_FLAG(OOPJITMissingOpts) &&
  1676. #endif
  1677. Js::JavascriptNumber::Is_NoTaggedIntCheck(addrOpnd->m_address))
  1678. {
  1679. addrOpnd->m_valueType =
  1680. Js::JavascriptNumber::IsInt32_NoChecks(addrOpnd->m_address)
  1681. ? ValueType::GetInt(false)
  1682. : ValueType::Float;
  1683. addrOpnd->SetValueTypeFixed();
  1684. }
  1685. }
  1686. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1687. addrOpnd->decodedValue = 0;
  1688. addrOpnd->wasVar = addrOpnd->IsVar();
  1689. #endif
  1690. addrOpnd->m_kind = OpndKindAddr;
  1691. return addrOpnd;
  1692. }
  1693. AddrOpnd *
  1694. AddrOpnd::New(Js::Var address, AddrOpndKind addrOpndKind, Func *func, bool dontEncode /* = false */, Js::Var varLocal /* = nullptr*/)
  1695. {
  1696. AddrOpnd * addrOpnd;
  1697. addrOpnd = JitAnew(func->m_alloc, IR::AddrOpnd);
  1698. addrOpnd->m_address = address;
  1699. addrOpnd->m_localAddress = func->IsOOPJIT() ? varLocal : address;
  1700. addrOpnd->addrOpndKind = addrOpndKind;
  1701. addrOpnd->m_type = addrOpnd->IsVar()? TyVar : TyMachPtr;
  1702. addrOpnd->m_dontEncode = dontEncode;
  1703. addrOpnd->m_isFunction = false;
  1704. addrOpnd->m_metadata = nullptr;
  1705. if(address && addrOpnd->IsVar())
  1706. {
  1707. if(Js::TaggedInt::Is(address))
  1708. {
  1709. addrOpnd->m_valueType = ValueType::GetTaggedInt();
  1710. addrOpnd->SetValueTypeFixed();
  1711. }
  1712. else
  1713. {
  1714. Js::Var var = varLocal ? varLocal : address;
  1715. if (
  1716. #if !FLOATVAR
  1717. varLocal || (!func->IsOOPJIT() && CONFIG_FLAG(OOPJITMissingOpts)) &&
  1718. #endif
  1719. Js::JavascriptNumber::Is_NoTaggedIntCheck(var))
  1720. {
  1721. addrOpnd->m_valueType =
  1722. Js::JavascriptNumber::IsInt32_NoChecks(var)
  1723. ? ValueType::GetInt(false)
  1724. : ValueType::Float;
  1725. addrOpnd->SetValueTypeFixed();
  1726. }
  1727. }
  1728. }
  1729. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1730. addrOpnd->decodedValue = 0;
  1731. addrOpnd->wasVar = addrOpnd->IsVar();
  1732. #endif
  1733. addrOpnd->m_kind = OpndKindAddr;
  1734. return addrOpnd;
  1735. }
  1736. AddrOpnd *
  1737. AddrOpnd::NewFromNumber(int32 value, Func *func, bool dontEncode /* = false */)
  1738. {
  1739. if (!Js::TaggedInt::IsOverflow(value))
  1740. {
  1741. return New(Js::TaggedInt::ToVarUnchecked(value), AddrOpndKindConstantVar, func, dontEncode);
  1742. }
  1743. else
  1744. {
  1745. return NewFromNumberVar(value, func, dontEncode);
  1746. }
  1747. }
  1748. AddrOpnd *
  1749. AddrOpnd::NewFromNumber(int64 value, Func *func, bool dontEncode /* = false */)
  1750. {
  1751. if (!Js::TaggedInt::IsOverflow(value))
  1752. {
  1753. return New(Js::TaggedInt::ToVarUnchecked((int)value), AddrOpndKindConstantVar, func, dontEncode);
  1754. }
  1755. else
  1756. {
  1757. return NewFromNumberVar((double)value, func, dontEncode);
  1758. }
  1759. }
  1760. AddrOpnd *
  1761. AddrOpnd::NewFromNumber(double value, Func *func, bool dontEncode /* = false */)
  1762. {
  1763. //
  1764. // Check if a well-known value:
  1765. // - This significantly cuts down on the below floating-point to integer conversions.
  1766. //
  1767. if (Js::JavascriptNumber::IsNegZero(value))
  1768. {
  1769. return New(func->GetScriptContextInfo()->GetNegativeZeroAddr(), AddrOpndKindDynamicVar, func, dontEncode);
  1770. }
  1771. if (value == +0.0)
  1772. {
  1773. return New(Js::TaggedInt::ToVarUnchecked(0), AddrOpndKindConstantVar, func, dontEncode);
  1774. }
  1775. if (value == 1.0)
  1776. {
  1777. return New(Js::TaggedInt::ToVarUnchecked(1), AddrOpndKindConstantVar, func, dontEncode);
  1778. }
  1779. //
  1780. // Check if number can be reduced back into a TaggedInt:
  1781. // - This avoids extra GC.
  1782. //
  1783. int nValue = (int) value;
  1784. double dblCheck = (double) nValue;
  1785. if ((dblCheck == value) && (!Js::TaggedInt::IsOverflow(nValue)))
  1786. {
  1787. return New(Js::TaggedInt::ToVarUnchecked(nValue), AddrOpndKindConstantVar, func, dontEncode);
  1788. }
  1789. return NewFromNumberVar(value, func, dontEncode);
  1790. }
  1791. AddrOpnd *
  1792. AddrOpnd::NewFromNumberVar(double value, Func *func, bool dontEncode /* = false */)
  1793. {
  1794. Js::Var var = func->AllocateNumber((double)value);
  1795. AddrOpnd* addrOpnd = New((intptr_t)var, AddrOpndKindDynamicVar, func, dontEncode);
  1796. addrOpnd->m_valueType =
  1797. Js::JavascriptNumber::IsInt32(value)
  1798. ? ValueType::GetInt(false)
  1799. : ValueType::Float;
  1800. addrOpnd->SetValueTypeFixed();
  1801. return addrOpnd;
  1802. }
  1803. AddrOpnd *
  1804. AddrOpnd::NewNull(Func *func)
  1805. {
  1806. return AddrOpnd::New((Js::Var)0, AddrOpndKindConstantAddress, func, true);
  1807. }
  1808. ///----------------------------------------------------------------------------
  1809. ///
  1810. /// AddrOpnd::Copy
  1811. ///
  1812. /// Returns a copy of this opnd.
  1813. ///
  1814. ///----------------------------------------------------------------------------
  1815. AddrOpnd *
  1816. AddrOpnd::CopyInternal(Func *func)
  1817. {
  1818. Assert(m_kind == OpndKindAddr);
  1819. AddrOpnd * newOpnd;
  1820. newOpnd = AddrOpnd::New(nullptr, addrOpndKind, func, m_dontEncode);
  1821. // Constructor evaluates address for type, but this is invalid if the address has been encoded, so we wait to set it
  1822. newOpnd->m_address = m_address;
  1823. newOpnd->m_valueType = m_valueType;
  1824. newOpnd->m_isFunction = m_isFunction;
  1825. newOpnd->m_metadata = m_metadata;
  1826. newOpnd->SetType(m_type);
  1827. if (IsValueTypeFixed())
  1828. {
  1829. newOpnd->SetValueTypeFixed();
  1830. }
  1831. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1832. newOpnd->decodedValue = this->decodedValue;
  1833. newOpnd->wasVar = this->wasVar;
  1834. #endif
  1835. return newOpnd;
  1836. }
  1837. ///----------------------------------------------------------------------------
  1838. ///
  1839. /// AddrOpnd::IsEqual
  1840. ///
  1841. ///----------------------------------------------------------------------------
  1842. bool
  1843. AddrOpnd::IsEqualInternal(Opnd *opnd)
  1844. {
  1845. Assert(m_kind == OpndKindAddr);
  1846. if (!opnd->IsAddrOpnd())
  1847. {
  1848. return false;
  1849. }
  1850. return m_address == opnd->AsAddrOpnd()->m_address;
  1851. }
  1852. void
  1853. AddrOpnd::FreeInternal(Func *func)
  1854. {
  1855. Assert(m_kind == OpndKindAddr);
  1856. JitAdelete(func->m_alloc, this);
  1857. }
  1858. void
  1859. AddrOpnd::SetEncodedValue(Js::Var address, AddrOpndKind addrOpndKind)
  1860. {
  1861. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1862. this->decodedValue = this->m_address;
  1863. #endif
  1864. this->SetAddress(address, addrOpndKind);
  1865. }
  1866. void
  1867. AddrOpnd::SetAddress(Js::Var address, AddrOpndKind addrOpndKind)
  1868. {
  1869. this->m_address = address;
  1870. this->addrOpndKind = addrOpndKind;
  1871. }
  1872. ///----------------------------------------------------------------------------
  1873. ///
  1874. /// IndirOpnd::New
  1875. ///
  1876. /// Creates a new IndirOpnd.
  1877. ///
  1878. ///----------------------------------------------------------------------------
  1879. IndirOpnd *
  1880. IndirOpnd::New(RegOpnd *baseOpnd, RegOpnd *indexOpnd, IRType type, Func *func)
  1881. {
  1882. IndirOpnd * indirOpnd;
  1883. AssertMsg(baseOpnd, "An IndirOpnd needs a valid baseOpnd.");
  1884. Assert(baseOpnd->GetSize() == TySize[TyMachReg]);
  1885. indirOpnd = JitAnew(func->m_alloc, IR::IndirOpnd);
  1886. indirOpnd->m_func = func;
  1887. indirOpnd->SetBaseOpnd(baseOpnd);
  1888. indirOpnd->SetIndexOpnd(indexOpnd);
  1889. indirOpnd->m_type = type;
  1890. indirOpnd->SetIsJITOptimizedReg(false);
  1891. indirOpnd->m_kind = OpndKindIndir;
  1892. return indirOpnd;
  1893. }
  1894. ///----------------------------------------------------------------------------
  1895. ///
  1896. /// IndirOpnd::New
  1897. ///
  1898. /// Creates a new IndirOpnd.
  1899. ///
  1900. ///----------------------------------------------------------------------------
  1901. IndirOpnd *
  1902. IndirOpnd::New(RegOpnd *baseOpnd, RegOpnd *indexOpnd, byte scale, IRType type, Func *func)
  1903. {
  1904. IndirOpnd * indirOpnd = IndirOpnd::New(baseOpnd, indexOpnd, type, func);
  1905. indirOpnd->m_scale = scale;
  1906. return indirOpnd;
  1907. }
  1908. ///----------------------------------------------------------------------------
  1909. ///
  1910. /// IndirOpnd::New
  1911. ///
  1912. /// Creates a new IndirOpnd.
  1913. ///
  1914. ///----------------------------------------------------------------------------
  1915. IndirOpnd *
  1916. IndirOpnd::New(RegOpnd *baseOpnd, int32 offset, IRType type, Func *func, bool dontEncode /* = false */)
  1917. {
  1918. IndirOpnd * indirOpnd;
  1919. indirOpnd = JitAnew(func->m_alloc, IR::IndirOpnd);
  1920. indirOpnd->m_func = func;
  1921. indirOpnd->SetBaseOpnd(baseOpnd);
  1922. indirOpnd->SetOffset(offset, dontEncode);
  1923. indirOpnd->m_type = type;
  1924. indirOpnd->SetIsJITOptimizedReg(false);
  1925. indirOpnd->m_kind = OpndKindIndir;
  1926. return indirOpnd;
  1927. }
  1928. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  1929. ///----------------------------------------------------------------------------
  1930. ///
  1931. /// IndirOpnd::New
  1932. ///
  1933. /// Creates a new IndirOpnd.
  1934. ///
  1935. ///----------------------------------------------------------------------------
  1936. IndirOpnd *
  1937. IndirOpnd::New(RegOpnd *baseOpnd, int32 offset, IRType type, const char16 *desc, Func *func, bool dontEncode /* = false */)
  1938. {
  1939. IndirOpnd * indirOpnd = IndirOpnd::New(baseOpnd, offset, type, func);
  1940. indirOpnd->m_desc = desc;
  1941. indirOpnd->m_dontEncode = dontEncode;
  1942. return indirOpnd;
  1943. }
  1944. #endif
  1945. IndirOpnd::~IndirOpnd()
  1946. {
  1947. if (m_baseOpnd != nullptr)
  1948. {
  1949. m_baseOpnd->Free(m_func);
  1950. }
  1951. if (m_indexOpnd != nullptr)
  1952. {
  1953. m_indexOpnd->Free(m_func);
  1954. }
  1955. }
  1956. ///----------------------------------------------------------------------------
  1957. ///
  1958. /// IndirOpnd::Copy
  1959. ///
  1960. /// Returns a copy of this opnd.
  1961. ///
  1962. ///----------------------------------------------------------------------------
  1963. IndirOpnd *
  1964. IndirOpnd::CopyInternal(Func *func)
  1965. {
  1966. Assert(m_kind == OpndKindIndir);
  1967. IndirOpnd * newOpnd;
  1968. newOpnd = IndirOpnd::New(m_baseOpnd, m_indexOpnd, m_scale, m_type, func);
  1969. newOpnd->m_valueType = m_valueType;
  1970. newOpnd->canStoreTemp = this->canStoreTemp;
  1971. newOpnd->SetOffset(m_offset, m_dontEncode);
  1972. newOpnd->SetIsJITOptimizedReg(this->GetIsJITOptimizedReg());
  1973. #if DBG_DUMP
  1974. newOpnd->m_addrKind = m_addrKind;
  1975. newOpnd->m_originalAddress = m_originalAddress;
  1976. #endif
  1977. return newOpnd;
  1978. }
  1979. IndirOpnd *
  1980. IndirOpnd::CloneDefInternal(Func *func)
  1981. {
  1982. Assert(m_kind == OpndKindIndir);
  1983. IndirOpnd * newOpnd;
  1984. // The components of an IndirOpnd are always uses, even if the IndirOpnd itself is a def.
  1985. RegOpnd * newBaseOpnd = m_baseOpnd ? m_baseOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  1986. RegOpnd * newIndexOpnd = m_indexOpnd ? m_indexOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  1987. newOpnd = IndirOpnd::New(newBaseOpnd, newIndexOpnd, m_scale, m_type, func);
  1988. newOpnd->SetOffset(m_offset, m_dontEncode);
  1989. #if DBG_DUMP
  1990. newOpnd->m_addrKind = m_addrKind;
  1991. newOpnd->m_originalAddress = m_originalAddress;
  1992. #endif
  1993. return newOpnd;
  1994. }
  1995. IndirOpnd *
  1996. IndirOpnd::CloneUseInternal(Func *func)
  1997. {
  1998. Assert(m_kind == OpndKindIndir);
  1999. IndirOpnd * newOpnd;
  2000. RegOpnd * newBaseOpnd = m_baseOpnd ? m_baseOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2001. RegOpnd * newIndexOpnd = m_indexOpnd ? m_indexOpnd->CloneUse(func)->AsRegOpnd() : nullptr;
  2002. newOpnd = IndirOpnd::New(newBaseOpnd, newIndexOpnd, m_scale, m_type, func);
  2003. newOpnd->SetOffset(m_offset, m_dontEncode);
  2004. #if DBG_DUMP
  2005. newOpnd->m_addrKind = m_addrKind;
  2006. newOpnd->m_originalAddress = m_originalAddress;
  2007. #endif
  2008. return newOpnd;
  2009. }
  2010. bool
  2011. IndirOpnd::TryGetIntConstIndexValue(bool trySym, IntConstType *pValue, bool * pIsNotInt)
  2012. {
  2013. *pIsNotInt = false;
  2014. IR::RegOpnd * indexOpnd = this->GetIndexOpnd();
  2015. if (!indexOpnd)
  2016. {
  2017. *pValue = (IntConstType)this->GetOffset();
  2018. return true;
  2019. }
  2020. if (!trySym)
  2021. {
  2022. return false;
  2023. }
  2024. StackSym * indexSym = indexOpnd->m_sym;
  2025. *pIsNotInt = indexOpnd->IsNotInt();
  2026. // Const flags for type-specialized syms are not accurate during the forward pass, so the forward pass cannot use that info
  2027. // while the lowerer can. Additionally, due to value transfers being conservative in a loop prepass, the const flags can
  2028. // show that a sym has a constant value even though the value during the forward pass did not. Skip checking const flags for
  2029. // type-specialized index syms and instead, expect that once the above issues are fixed, that the forward pass would fold a
  2030. // constant index into the indir's offset.
  2031. if (!*pIsNotInt && !indexSym->IsTypeSpec() && indexSym->IsIntConst())
  2032. {
  2033. *pValue = indexSym->GetIntConstValue();
  2034. return true;
  2035. }
  2036. return false;
  2037. }
  2038. ///----------------------------------------------------------------------------
  2039. ///
  2040. /// IndirOpnd::IsEqual
  2041. ///
  2042. ///----------------------------------------------------------------------------
  2043. bool
  2044. IndirOpnd::IsEqualInternal(Opnd *opnd)
  2045. {
  2046. Assert(m_kind == OpndKindIndir);
  2047. Assert(opnd);
  2048. if (!opnd->IsIndirOpnd() || this->GetType() != opnd->GetType())
  2049. {
  2050. return false;
  2051. }
  2052. IndirOpnd *indirOpnd = opnd->AsIndirOpnd();
  2053. return m_offset == indirOpnd->m_offset && m_baseOpnd->IsEqual(indirOpnd->m_baseOpnd)
  2054. && ((m_indexOpnd == nullptr && indirOpnd->m_indexOpnd == nullptr) || (m_indexOpnd && indirOpnd->m_indexOpnd && m_indexOpnd->IsEqual(indirOpnd->m_indexOpnd)));
  2055. }
  2056. void
  2057. IndirOpnd::FreeInternal(Func *func)
  2058. {
  2059. Assert(m_kind == OpndKindIndir);
  2060. JitAdelete(func->m_alloc, this);
  2061. }
  2062. ///----------------------------------------------------------------------------
  2063. ///
  2064. /// IndirOpnd::SetBaseOpnd
  2065. ///
  2066. ///----------------------------------------------------------------------------
  2067. void
  2068. IndirOpnd::SetBaseOpnd(RegOpnd *baseOpnd)
  2069. {
  2070. if (m_baseOpnd)
  2071. {
  2072. m_baseOpnd->UnUse();
  2073. }
  2074. if (baseOpnd)
  2075. {
  2076. baseOpnd = baseOpnd->Use(m_func)->AsRegOpnd();
  2077. }
  2078. m_baseOpnd = baseOpnd;
  2079. }
  2080. ///----------------------------------------------------------------------------
  2081. ///
  2082. /// IndirOpnd::UnlinkBaseOpnd
  2083. ///
  2084. ///----------------------------------------------------------------------------
  2085. RegOpnd *
  2086. IndirOpnd::UnlinkBaseOpnd()
  2087. {
  2088. RegOpnd * baseOpnd = this->m_baseOpnd;
  2089. // This will also call UnUse()...
  2090. this->SetBaseOpnd(nullptr);
  2091. return baseOpnd;
  2092. }
  2093. void
  2094. IndirOpnd::ReplaceBaseOpnd(RegOpnd *newBase)
  2095. {
  2096. RegOpnd * baseOpnd = this->m_baseOpnd;
  2097. this->UnlinkBaseOpnd();
  2098. baseOpnd->Free(this->m_func);
  2099. this->SetBaseOpnd(newBase);
  2100. }
  2101. ///----------------------------------------------------------------------------
  2102. ///
  2103. /// IndirOpnd::SetIndexOpnd
  2104. ///
  2105. ///----------------------------------------------------------------------------
  2106. void
  2107. IndirOpnd::SetIndexOpnd(RegOpnd *indexOpnd)
  2108. {
  2109. if (m_indexOpnd)
  2110. {
  2111. m_indexOpnd->UnUse();
  2112. }
  2113. if (indexOpnd)
  2114. {
  2115. indexOpnd = indexOpnd->Use(m_func)->AsRegOpnd();
  2116. }
  2117. m_indexOpnd = indexOpnd;
  2118. }
  2119. ///----------------------------------------------------------------------------
  2120. ///
  2121. /// IndirOpnd::UnlinkIndexOpnd
  2122. ///
  2123. ///----------------------------------------------------------------------------
  2124. RegOpnd *
  2125. IndirOpnd::UnlinkIndexOpnd()
  2126. {
  2127. RegOpnd * indexOpnd = this->m_indexOpnd;
  2128. // This will also call UnUse()...
  2129. this->SetIndexOpnd(nullptr);
  2130. return indexOpnd;
  2131. }
  2132. void
  2133. IndirOpnd::ReplaceIndexOpnd(RegOpnd *newIndex)
  2134. {
  2135. RegOpnd * indexOpnd = this->m_indexOpnd;
  2136. this->UnlinkIndexOpnd();
  2137. indexOpnd->Free(this->m_func);
  2138. this->SetIndexOpnd(newIndex);
  2139. }
  2140. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  2141. const char16 *
  2142. IndirOpnd::GetDescription()
  2143. {
  2144. return this->m_desc;
  2145. }
  2146. bool
  2147. IndirOpnd::HasAddrKind() const
  2148. {
  2149. #if DBG_DUMP
  2150. return m_addrKind != (IR::AddrOpndKind) - 1;
  2151. #else
  2152. return false;
  2153. #endif
  2154. }
  2155. IR::AddrOpndKind
  2156. IndirOpnd::GetAddrKind() const
  2157. {
  2158. Assert(HasAddrKind());
  2159. #if DBG_DUMP
  2160. return m_addrKind;
  2161. #else
  2162. return IR::AddrOpndKindDynamicMisc;
  2163. #endif
  2164. }
  2165. void *
  2166. IndirOpnd::GetOriginalAddress() const
  2167. {
  2168. Assert(HasAddrKind());
  2169. #if DBG_DUMP
  2170. Assert(m_originalAddress != nullptr);
  2171. return m_originalAddress;
  2172. #else
  2173. return nullptr;
  2174. #endif
  2175. }
  2176. #endif
  2177. #if DBG_DUMP
  2178. void
  2179. IndirOpnd::SetAddrKind(IR::AddrOpndKind kind, void * originalAddress)
  2180. {
  2181. Assert(originalAddress != nullptr);
  2182. this->m_addrKind = kind;
  2183. this->m_originalAddress = originalAddress;
  2184. }
  2185. #endif
  2186. ///----------------------------------------------------------------------------
  2187. ///
  2188. /// MemRefOpnd::New
  2189. ///
  2190. /// Creates a new MemRefOpnd.
  2191. ///
  2192. ///----------------------------------------------------------------------------
  2193. MemRefOpnd *
  2194. MemRefOpnd::New(intptr_t pMemLoc, IRType type, Func *func, AddrOpndKind addrOpndKind)
  2195. {
  2196. MemRefOpnd * memRefOpnd = JitAnew(func->m_alloc, IR::MemRefOpnd);
  2197. memRefOpnd->m_memLoc = pMemLoc;
  2198. memRefOpnd->m_type = type;
  2199. memRefOpnd->m_kind = OpndKindMemRef;
  2200. #if DBG_DUMP
  2201. memRefOpnd->m_addrKind = addrOpndKind;
  2202. #endif
  2203. return memRefOpnd;
  2204. }
  2205. // TODO: michhol OOP JIT, remove this signature
  2206. MemRefOpnd *
  2207. MemRefOpnd::New(void * pMemLoc, IRType type, Func *func, AddrOpndKind addrOpndKind)
  2208. {
  2209. MemRefOpnd * memRefOpnd = JitAnew(func->m_alloc, IR::MemRefOpnd);
  2210. memRefOpnd->m_memLoc = (intptr_t)pMemLoc;
  2211. memRefOpnd->m_type = type;
  2212. memRefOpnd->m_kind = OpndKindMemRef;
  2213. #if DBG_DUMP
  2214. memRefOpnd->m_addrKind = addrOpndKind;
  2215. #endif
  2216. return memRefOpnd;
  2217. }
  2218. IR::AddrOpndKind
  2219. MemRefOpnd::GetAddrKind() const
  2220. {
  2221. #if DBG_DUMP
  2222. return this->m_addrKind;
  2223. #else
  2224. return AddrOpndKindDynamicMisc;
  2225. #endif
  2226. }
  2227. ///----------------------------------------------------------------------------
  2228. ///
  2229. /// MemRefOpnd::Copy
  2230. ///
  2231. /// Returns a copy of this opnd.
  2232. ///
  2233. ///----------------------------------------------------------------------------
  2234. MemRefOpnd *
  2235. MemRefOpnd::CopyInternal(Func *func)
  2236. {
  2237. Assert(m_kind == OpndKindMemRef);
  2238. MemRefOpnd * newOpnd;
  2239. newOpnd = MemRefOpnd::New(m_memLoc, m_type, func);
  2240. newOpnd->m_valueType = m_valueType;
  2241. newOpnd->m_memLoc = m_memLoc;
  2242. #if DBG_DUMP
  2243. newOpnd->m_addrKind = m_addrKind;
  2244. #endif
  2245. return newOpnd;
  2246. }
  2247. ///----------------------------------------------------------------------------
  2248. ///
  2249. /// MemRefOpnd::IsEqual
  2250. ///
  2251. ///----------------------------------------------------------------------------
  2252. bool
  2253. MemRefOpnd::IsEqualInternal(Opnd *opnd)
  2254. {
  2255. Assert(m_kind == OpndKindMemRef);
  2256. if (!opnd->IsMemRefOpnd() || this->GetType() != opnd->GetType())
  2257. {
  2258. return false;
  2259. }
  2260. MemRefOpnd *memRefOpnd = opnd->AsMemRefOpnd();
  2261. return m_memLoc == memRefOpnd->m_memLoc;
  2262. }
  2263. void
  2264. MemRefOpnd::FreeInternal(Func *func)
  2265. {
  2266. Assert(m_kind == OpndKindMemRef);
  2267. JitAdelete(func->m_alloc, this);
  2268. }
  2269. LabelOpnd *
  2270. LabelOpnd::New(LabelInstr * labelInstr, Func * func)
  2271. {
  2272. LabelOpnd * labelOpnd = JitAnew(func->m_alloc, IR::LabelOpnd);
  2273. labelOpnd->m_label = labelInstr;
  2274. labelOpnd->m_type = TyMachPtr;
  2275. labelInstr->m_hasNonBranchRef = true;
  2276. labelOpnd->m_kind = OpndKindLabel;
  2277. return labelOpnd;
  2278. }
  2279. LabelOpnd *
  2280. LabelOpnd::CopyInternal(Func * func)
  2281. {
  2282. Assert(m_kind == OpndKindLabel);
  2283. LabelOpnd * newOpnd;
  2284. newOpnd = LabelOpnd::New(m_label, func);
  2285. newOpnd->m_valueType = m_valueType;
  2286. return newOpnd;
  2287. }
  2288. bool
  2289. LabelOpnd::IsEqualInternal(Opnd * opnd)
  2290. {
  2291. Assert(m_kind == OpndKindLabel);
  2292. if (!opnd->IsLabelOpnd())
  2293. {
  2294. return false;
  2295. }
  2296. LabelOpnd * newOpnd = opnd->AsLabelOpnd();
  2297. return m_label == newOpnd->GetLabel();
  2298. }
  2299. void
  2300. LabelOpnd::FreeInternal(Func *func)
  2301. {
  2302. Assert(m_kind == OpndKindLabel);
  2303. JitAdelete(func->m_alloc, this);
  2304. }
  2305. IR::RegOpnd *
  2306. Opnd::FindRegUse(IR::RegOpnd *regOpnd)
  2307. {
  2308. StackSym *regSym = regOpnd->m_sym;
  2309. if (this->IsRegOpnd())
  2310. {
  2311. if (this->AsRegOpnd()->m_sym == regSym)
  2312. {
  2313. return this->AsRegOpnd();
  2314. }
  2315. }
  2316. else if (this->IsIndirOpnd())
  2317. {
  2318. IndirOpnd *indirOpnd = this->AsIndirOpnd();
  2319. if (indirOpnd->GetBaseOpnd() && indirOpnd->GetBaseOpnd()->m_sym == regSym)
  2320. {
  2321. return indirOpnd->GetBaseOpnd();
  2322. }
  2323. if (indirOpnd->GetIndexOpnd() && indirOpnd->GetIndexOpnd()->m_sym == regSym)
  2324. {
  2325. return indirOpnd->GetIndexOpnd();
  2326. }
  2327. }
  2328. return nullptr;
  2329. }
  2330. bool
  2331. Opnd::IsArgumentsObject()
  2332. {
  2333. // returns "false" if the sym is not single def (happens when the parent function has formals); the opnd can still be the arguments object.
  2334. // Since we need this information in the inliner where we don't track arguments object sym, going with single def is the best option.
  2335. StackSym * sym = this->GetStackSym();
  2336. return sym && sym->IsSingleDef() && sym->GetInstrDef()->HasAnyLoadHeapArgsOpCode();
  2337. }
  2338. #if DBG_DUMP || defined(ENABLE_IR_VIEWER)
  2339. void
  2340. Opnd::DumpAddress(void *address, bool printToConsole, bool skipMaskedAddress)
  2341. {
  2342. if (!printToConsole)
  2343. {
  2344. return;
  2345. }
  2346. if (!Js::Configuration::Global.flags.DumpIRAddresses)
  2347. {
  2348. if (skipMaskedAddress)
  2349. {
  2350. return;
  2351. }
  2352. Output::Print(_u("0xXXXXXXXX"));
  2353. }
  2354. else
  2355. {
  2356. #ifdef _M_X64
  2357. Output::Print(_u("0x%012I64X"), address);
  2358. #else
  2359. Output::Print(_u("0x%08X"), address);
  2360. #endif
  2361. }
  2362. }
  2363. void
  2364. Opnd::DumpFunctionInfo(_Outptr_result_buffer_(*count) char16 ** buffer, size_t * count, Js::FunctionInfo * info, bool printToConsole, _In_opt_z_ char16 const * type)
  2365. {
  2366. char16 debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  2367. if (info->HasBody())
  2368. {
  2369. if (type == nullptr)
  2370. {
  2371. type = _u("FunctionBody");
  2372. }
  2373. Js::FunctionProxy * proxy = info->GetFunctionProxy();
  2374. WriteToBuffer(buffer, count, _u(" (%s [%s%s])"), type, proxy->GetDisplayName(), proxy->GetDebugNumberSet(debugStringBuffer));
  2375. }
  2376. else
  2377. {
  2378. if (type == nullptr)
  2379. {
  2380. type = _u("FunctionInfo");
  2381. }
  2382. WriteToBuffer(buffer, count, _u(" (%s)"), type);
  2383. }
  2384. }
  2385. ///----------------------------------------------------------------------------
  2386. ///
  2387. /// Opnd::Dump
  2388. ///
  2389. /// Dump this opnd.
  2390. ///
  2391. ///----------------------------------------------------------------------------
  2392. void
  2393. Opnd::Dump(IRDumpFlags flags, Func *func)
  2394. {
  2395. bool const AsmDumpMode = flags & IRDumpFlags_AsmDumpMode;
  2396. bool const SimpleForm = !!(flags & IRDumpFlags_SimpleForm);
  2397. FloatConstType floatValue;
  2398. SymOpnd * symOpnd;
  2399. RegOpnd * regOpnd;
  2400. JnHelperMethod helperMethod;
  2401. bool dumpValueType = !SimpleForm;
  2402. switch(GetKind())
  2403. {
  2404. case OpndKindSym:
  2405. symOpnd = this->AsSymOpnd();
  2406. if(symOpnd->m_sym->IsPropertySym() && !SimpleForm)
  2407. {
  2408. symOpnd->m_sym->Dump(flags, symOpnd->GetPropertyOwnerValueType());
  2409. }
  2410. else
  2411. {
  2412. symOpnd->m_sym->Dump(flags);
  2413. }
  2414. if (symOpnd->m_sym->IsStackSym())
  2415. {
  2416. StackSym * stackSym = symOpnd->m_sym->AsStackSym();
  2417. bool hasOffset = stackSym->IsArgSlotSym()?
  2418. ((stackSym->m_offset != -1) || !stackSym->m_isInlinedArgSlot) :
  2419. (stackSym->m_offset != 0);
  2420. if (hasOffset)
  2421. {
  2422. int offset = stackSym->m_offset;
  2423. if(symOpnd->m_offset != 0)
  2424. {
  2425. Assert(static_cast<int>(offset + symOpnd->m_offset) >= offset);
  2426. offset += symOpnd->m_offset;
  2427. }
  2428. Output::Print(_u("<%d>"), offset);
  2429. }
  2430. }
  2431. else if (symOpnd->IsPropertySymOpnd() && !SimpleForm)
  2432. {
  2433. PropertySymOpnd *propertySymOpnd = symOpnd->AsPropertySymOpnd();
  2434. Output::Print(_u("<"));
  2435. if (propertySymOpnd->HasObjTypeSpecFldInfo())
  2436. {
  2437. Output::Print(_u("%u,%s%s%s%s,"), propertySymOpnd->GetObjTypeSpecFldId(), propertySymOpnd->IsPoly() ? _u("p") : _u("m"),
  2438. propertySymOpnd->IsLoadedFromProto() ? _u("~") : _u(""), propertySymOpnd->UsesFixedValue() ? _u("=") : _u(""),
  2439. propertySymOpnd->IsBeingAdded() ? _u("+") : _u(""));
  2440. }
  2441. else
  2442. {
  2443. Output::Print(_u("?,,"));
  2444. }
  2445. Output::Print(_u("%s%s,"), propertySymOpnd->MayNeedTypeCheckProtection() ?
  2446. propertySymOpnd->IsMono() ? _u("+") : _u("=") :
  2447. propertySymOpnd->IsRootObjectNonConfigurableFieldLoad() ? _u("~") : _u("-"),
  2448. propertySymOpnd->IsTypeCheckSeqCandidate() ? _u("+") : _u("-"));
  2449. if (propertySymOpnd->HasObjectTypeSym())
  2450. {
  2451. Output::Print(_u("s%d"), propertySymOpnd->GetObjectTypeSym()->m_id);
  2452. if (propertySymOpnd->IsTypeChecked())
  2453. {
  2454. Output::Print(_u("+%s"), propertySymOpnd->IsMono() ? _u("m") : _u("p"));
  2455. }
  2456. else if (propertySymOpnd->IsTypeAvailable())
  2457. {
  2458. Output::Print(_u("*"));
  2459. }
  2460. if (propertySymOpnd->IsTypeDead())
  2461. {
  2462. Output::Print(_u("!"));
  2463. }
  2464. }
  2465. else
  2466. {
  2467. Output::Print(_u("s?"));
  2468. }
  2469. if (propertySymOpnd->m_sym->AsPropertySym()->m_writeGuardSym != nullptr)
  2470. {
  2471. Output::Print(_u(",s%d"), propertySymOpnd->m_sym->AsPropertySym()->m_writeGuardSym->m_id);
  2472. if (propertySymOpnd->IsWriteGuardChecked())
  2473. {
  2474. Output::Print(_u("+"));
  2475. }
  2476. }
  2477. else
  2478. {
  2479. Output::Print(_u(",s?"));
  2480. }
  2481. if (propertySymOpnd->HasFinalType())
  2482. {
  2483. Output::Print(_u(",final:"));
  2484. this->DumpAddress((void*)propertySymOpnd->GetFinalType()->GetAddr(), /* printToConsole */ true, /* skipMaskedAddress */ false);
  2485. }
  2486. if (propertySymOpnd->GetGuardedPropOps() != nullptr)
  2487. {
  2488. Output::Print(_u(",{"));
  2489. if (func != nullptr)
  2490. {
  2491. int i = 0;
  2492. auto guardedPropOps = propertySymOpnd->GetGuardedPropOps();
  2493. FOREACH_BITSET_IN_SPARSEBV(propertyOpId, guardedPropOps)
  2494. {
  2495. if (i++ > 0)
  2496. {
  2497. Output::Print(_u(","));
  2498. }
  2499. const JITObjTypeSpecFldInfo* propertyOpInfo = func->GetTopFunc()->GetGlobalObjTypeSpecFldInfo(propertyOpId);
  2500. if (!JITManager::GetJITManager()->IsOOPJITEnabled())
  2501. {
  2502. Output::Print(_u("%s"), func->GetInProcThreadContext()->GetPropertyRecord(propertyOpInfo->GetPropertyId())->GetBuffer(), propertyOpId);
  2503. }
  2504. Output::Print(_u("(%u)"), propertyOpId);
  2505. if (propertyOpInfo->IsLoadedFromProto())
  2506. {
  2507. Output::Print(_u("~"));
  2508. }
  2509. if (propertyOpInfo->HasFixedValue())
  2510. {
  2511. Output::Print(_u("="));
  2512. }
  2513. if (propertyOpInfo->IsBeingAdded())
  2514. {
  2515. Output::Print(_u("+"));
  2516. }
  2517. }
  2518. NEXT_BITSET_IN_SPARSEBV;
  2519. }
  2520. else
  2521. {
  2522. Output::Print(_u("(no func)"));
  2523. }
  2524. Output::Print(_u("}"));
  2525. }
  2526. if (propertySymOpnd->GetWriteGuards() != nullptr)
  2527. {
  2528. Output::Print(_u(",{"));
  2529. int i = 0;
  2530. auto writeGuards = propertySymOpnd->GetWriteGuards();
  2531. FOREACH_BITSET_IN_SPARSEBV(writeGuardSymId, writeGuards)
  2532. {
  2533. if (i++ > 0)
  2534. {
  2535. Output::Print(_u(","));
  2536. }
  2537. Output::Print(_u("s%d"), writeGuardSymId);
  2538. }
  2539. NEXT_BITSET_IN_SPARSEBV;
  2540. Output::Print(_u("}"));
  2541. }
  2542. if (propertySymOpnd->canStoreTemp)
  2543. {
  2544. Output::Print(_u(",t"));
  2545. }
  2546. Output::Print(_u(">"));
  2547. }
  2548. break;
  2549. case OpndKindReg:
  2550. regOpnd = this->AsRegOpnd();
  2551. if (regOpnd->m_sym)
  2552. {
  2553. regOpnd->m_sym->Dump(flags);
  2554. }
  2555. if(AsmDumpMode)
  2556. {
  2557. //
  2558. // Print no brackets
  2559. //
  2560. Output::Print(_u("%S"), RegNames[regOpnd->GetReg()]);
  2561. }
  2562. else
  2563. {
  2564. if (regOpnd->GetReg() != RegNOREG)
  2565. {
  2566. Output::Print(_u("(%S)"), RegNames[regOpnd->GetReg()]);
  2567. }
  2568. if (regOpnd->m_isTempLastUse)
  2569. {
  2570. Output::Print(_u("[isTempLastUse]"));
  2571. }
  2572. StackSym *sym = regOpnd->GetStackSym();
  2573. if (sym && func)
  2574. {
  2575. if (sym == func->GetScriptContextSym())
  2576. {
  2577. Output::Print(_u("[ScriptContext]"));
  2578. }
  2579. else if (sym == func->GetFuncObjSym())
  2580. {
  2581. Output::Print(_u("[FuncObj]"));
  2582. }
  2583. else if (sym == func->GetFunctionBodySym())
  2584. {
  2585. Output::Print(_u("[FunctionBody]"));
  2586. }
  2587. }
  2588. if(regOpnd->IsArrayRegOpnd())
  2589. {
  2590. if(dumpValueType)
  2591. {
  2592. // Dump the array value type before the associated syms
  2593. DumpValueType();
  2594. dumpValueType = false;
  2595. }
  2596. const ArrayRegOpnd *const arrayRegOpnd = regOpnd->AsArrayRegOpnd();
  2597. if(arrayRegOpnd->HeadSegmentSym())
  2598. {
  2599. Output::Print(_u("[seg: "));
  2600. arrayRegOpnd->HeadSegmentSym()->Dump();
  2601. Output::Print(_u("]"));
  2602. }
  2603. if(arrayRegOpnd->HeadSegmentLengthSym())
  2604. {
  2605. Output::Print(_u("[segLen: "));
  2606. arrayRegOpnd->HeadSegmentLengthSym()->Dump();
  2607. Output::Print(_u("]"));
  2608. }
  2609. if(arrayRegOpnd->LengthSym() && arrayRegOpnd->LengthSym() != arrayRegOpnd->HeadSegmentLengthSym())
  2610. {
  2611. Output::Print(_u("[len: "));
  2612. arrayRegOpnd->LengthSym()->Dump();
  2613. Output::Print(_u("]"));
  2614. }
  2615. if(arrayRegOpnd->EliminatedLowerBoundCheck() || arrayRegOpnd->EliminatedUpperBoundCheck())
  2616. {
  2617. Output::Print(_u("["));
  2618. if(arrayRegOpnd->EliminatedLowerBoundCheck())
  2619. {
  2620. Output::Print(_u(">"));
  2621. }
  2622. if(arrayRegOpnd->EliminatedUpperBoundCheck())
  2623. {
  2624. Output::Print(_u("<"));
  2625. }
  2626. Output::Print(_u("]"));
  2627. }
  2628. }
  2629. }
  2630. break;
  2631. case OpndKindInt64Const:
  2632. {
  2633. Int64ConstOpnd * intConstOpnd = this->AsInt64ConstOpnd();
  2634. int64 intValue = intConstOpnd->GetValue();
  2635. Output::Print(_u("%lld (0x%llX)"), intValue, intValue);
  2636. break;
  2637. }
  2638. case OpndKindIntConst:
  2639. {
  2640. IntConstOpnd * intConstOpnd = this->AsIntConstOpnd();
  2641. if (intConstOpnd->name != nullptr)
  2642. {
  2643. if (!Js::Configuration::Global.flags.DumpIRAddresses)
  2644. {
  2645. Output::Print(_u("<%s>"), intConstOpnd->name);
  2646. }
  2647. else
  2648. {
  2649. Output::Print(_u("<%s> (value: 0x%X)"), intConstOpnd->name, intConstOpnd->GetValue());
  2650. }
  2651. }
  2652. else
  2653. {
  2654. IntConstType intValue;
  2655. if (intConstOpnd->decodedValue != 0)
  2656. {
  2657. intValue = intConstOpnd->decodedValue;
  2658. Output::Print(_u("%d (0x%X)"), intValue, intValue);
  2659. if (!Js::Configuration::Global.flags.DumpIRAddresses)
  2660. {
  2661. Output::Print(_u(" [encoded]"));
  2662. }
  2663. else
  2664. {
  2665. Output::Print(_u(" [encoded: 0x%X]"), intConstOpnd->GetValue());
  2666. }
  2667. }
  2668. else
  2669. {
  2670. intValue = intConstOpnd->GetValue();
  2671. Output::Print(_u("%d (0x%X)"), intValue, intValue);
  2672. }
  2673. }
  2674. break;
  2675. }
  2676. case OpndKindRegBV:
  2677. {
  2678. RegBVOpnd * regBVOpnd = this->AsRegBVOpnd();
  2679. regBVOpnd->m_value.Dump();
  2680. break;
  2681. }
  2682. case OpndKindHelperCall:
  2683. helperMethod = this->AsHelperCallOpnd()->m_fnHelper;
  2684. Output::Print(_u("%s"), IR::GetMethodName(helperMethod));
  2685. break;
  2686. case OpndKindFloatConst:
  2687. floatValue = this->AsFloatConstOpnd()->m_value;
  2688. Output::Print(_u("%G"), floatValue);
  2689. break;
  2690. case OpndKindAddr:
  2691. DumpOpndKindAddr(AsmDumpMode, func);
  2692. break;
  2693. case OpndKindIndir:
  2694. {
  2695. IndirOpnd *indirOpnd = this->AsIndirOpnd();
  2696. Output::Print(_u("["));
  2697. indirOpnd->GetBaseOpnd()->Dump(flags, func);
  2698. if (indirOpnd->GetIndexOpnd())
  2699. {
  2700. Output::Print(_u("+"));
  2701. indirOpnd->GetIndexOpnd()->Dump(flags, func);
  2702. if (indirOpnd->GetScale() > 0)
  2703. {
  2704. Output::Print(_u("*%d"), 1 << indirOpnd->GetScale());
  2705. }
  2706. }
  2707. if (indirOpnd->GetOffset())
  2708. {
  2709. if (!Js::Configuration::Global.flags.DumpIRAddresses && indirOpnd->HasAddrKind())
  2710. {
  2711. Output::Print(_u("+XX"));
  2712. }
  2713. else
  2714. {
  2715. const auto sign = indirOpnd->GetOffset() >= 0 ? _u("+") : _u("");
  2716. if (AsmDumpMode)
  2717. {
  2718. Output::Print(_u("%sXXXX%04d"), sign, indirOpnd->GetOffset() & 0xffff);
  2719. }
  2720. else
  2721. {
  2722. Output::Print(_u("%s%d"), sign, indirOpnd->GetOffset());
  2723. }
  2724. }
  2725. }
  2726. if (indirOpnd->GetDescription())
  2727. {
  2728. Output::Print(_u(" <%s>"), indirOpnd->GetDescription());
  2729. }
  2730. if (indirOpnd->HasAddrKind())
  2731. {
  2732. INT_PTR address = (INT_PTR)indirOpnd->GetOriginalAddress();
  2733. Output::Print(_u(" <"));
  2734. const size_t BUFFER_LEN = 128;
  2735. char16 buffer[BUFFER_LEN];
  2736. GetAddrDescription(buffer, BUFFER_LEN, (void *)address, indirOpnd->GetAddrKind(), AsmDumpMode, /*printToConsole */ true, func, /* skipMaskedAddress */true);
  2737. Output::Print(_u("%s"), buffer);
  2738. Output::Print(_u(">"));
  2739. }
  2740. Output::Print(_u("]"));
  2741. break;
  2742. }
  2743. case OpndKindMemRef:
  2744. {
  2745. DumpOpndKindMemRef(AsmDumpMode, func);
  2746. break;
  2747. }
  2748. case OpndKindLabel:
  2749. {
  2750. LabelOpnd * labelOpnd = this->AsLabelOpnd();
  2751. LabelInstr * labelInstr = labelOpnd->GetLabel();
  2752. if (labelInstr == nullptr)
  2753. {
  2754. Output::Print(_u("??"));
  2755. }
  2756. else
  2757. {
  2758. Output::Print(_u("&$L%d"), labelInstr->m_id);
  2759. }
  2760. break;
  2761. }
  2762. }
  2763. if(!AsmDumpMode && dumpValueType)
  2764. {
  2765. DumpValueType();
  2766. }
  2767. if (!SimpleForm || this->GetType() != TyVar)
  2768. {
  2769. Output::Print(_u("."));
  2770. IRType_Dump(this->GetType());
  2771. }
  2772. if (this->m_isDead && !SimpleForm)
  2773. {
  2774. Output::Print(_u("!"));
  2775. }
  2776. }
  2777. ///----------------------------------------------------------------------------
  2778. ///
  2779. /// Opnd::DumpOpndKindAddr
  2780. ///
  2781. /// Dump this opnd as an address.
  2782. ///
  2783. ///----------------------------------------------------------------------------
  2784. void
  2785. Opnd::DumpOpndKindAddr(bool AsmDumpMode, Func *func)
  2786. {
  2787. const size_t BUFFER_LEN = 128;
  2788. char16 buffer[BUFFER_LEN];
  2789. GetAddrDescription(buffer, BUFFER_LEN, AsmDumpMode, true, func);
  2790. Output::Print(_u("%s"), buffer);
  2791. }
  2792. void
  2793. Opnd::DumpOpndKindMemRef(bool AsmDumpMode, Func *func)
  2794. {
  2795. MemRefOpnd *memRefOpnd = this->AsMemRefOpnd();
  2796. Output::Print(_u("["));
  2797. const size_t BUFFER_LEN = 128;
  2798. char16 buffer[BUFFER_LEN];
  2799. // TODO: michhol, make this intptr_t
  2800. GetAddrDescription(buffer, BUFFER_LEN, (void*)memRefOpnd->GetMemLoc(), memRefOpnd->GetAddrKind(), AsmDumpMode, true, func);
  2801. Output::Print(_u("%s"), buffer);
  2802. Output::Print(_u("]"));
  2803. }
  2804. /**
  2805. WriteToBuffer
  2806. Write <fmt> with applicable replacements into <buffer>.
  2807. Subtract the number of characters written from <count>, and increment the address
  2808. <buffer> so that subsequent calls to this function will continue writing at the point
  2809. in the buffer where this function left off and will respect the maximum length specified
  2810. by count.
  2811. @param buffer
  2812. A pointer to a buffer which will hold the result.
  2813. @param count
  2814. The maximum number of characters that should be returned in <buffer>.
  2815. @param fmt
  2816. A format string.
  2817. @param ...
  2818. Additional parameters to be passed to the formatter.
  2819. */
  2820. void
  2821. Opnd::WriteToBuffer(_Outptr_result_buffer_(*count) char16 **buffer, size_t *count, const char16 *fmt, ...)
  2822. {
  2823. va_list argptr;
  2824. va_start(argptr, fmt);
  2825. int len = _vsnwprintf_s(*buffer, *count, _TRUNCATE, fmt, argptr);
  2826. *count -= len;
  2827. *buffer += len;
  2828. va_end(argptr);
  2829. }
  2830. void
  2831. Opnd::GetAddrDescription(__out_ecount(count) char16 *const description, const size_t count,
  2832. void * address, IR::AddrOpndKind addressKind, bool AsmDumpMode, bool printToConsole, Func *func, bool skipMaskedAddress)
  2833. {
  2834. char16 *buffer = description;
  2835. size_t n = count;
  2836. if (address)
  2837. {
  2838. switch (addressKind)
  2839. {
  2840. case IR::AddrOpndKindConstantAddress:
  2841. {
  2842. #ifdef _M_X64_OR_ARM64
  2843. char16 const * format = _u("0x%012I64X");
  2844. #else
  2845. char16 const * format = _u("0x%08X");
  2846. #endif
  2847. WriteToBuffer(&buffer, &n, format, address);
  2848. }
  2849. break;
  2850. case IR::AddrOpndKindDynamicVar:
  2851. if (Js::TaggedInt::Is(address))
  2852. {
  2853. #ifdef _M_X64_OR_ARM64
  2854. char16 const * format = _u("0x%012I64X (value: %d)");
  2855. #else
  2856. char16 const * format = _u("0x%08X (value: %d)");
  2857. #endif
  2858. WriteToBuffer(&buffer, &n, format, address, Js::TaggedInt::ToInt32(address));
  2859. }
  2860. #if FLOATVAR
  2861. else if (Js::JavascriptNumber::Is_NoTaggedIntCheck(address))
  2862. #else
  2863. else if (!func->IsOOPJIT() && Js::JavascriptNumber::Is_NoTaggedIntCheck(address))
  2864. #endif
  2865. {
  2866. WriteToBuffer(&buffer, &n, _u(" (value: %f)"), Js::JavascriptNumber::GetValue(address));
  2867. }
  2868. else
  2869. {
  2870. DumpAddress(address, printToConsole, skipMaskedAddress);
  2871. // TODO: michhol OOP JIT, fix dumping these
  2872. if (func->IsOOPJIT())
  2873. {
  2874. WriteToBuffer(&buffer, &n, _u(" (unknown)"));
  2875. }
  2876. else
  2877. {
  2878. switch (Js::RecyclableObject::FromVar(address)->GetTypeId())
  2879. {
  2880. case Js::TypeIds_Boolean:
  2881. WriteToBuffer(&buffer, &n, Js::JavascriptBoolean::FromVar(address)->GetValue() ? _u(" (true)") : _u(" (false)"));
  2882. break;
  2883. case Js::TypeIds_String:
  2884. WriteToBuffer(&buffer, &n, _u(" (\"%s\")"), Js::JavascriptString::FromVar(address)->GetSz());
  2885. break;
  2886. case Js::TypeIds_Number:
  2887. WriteToBuffer(&buffer, &n, _u(" (value: %f)"), Js::JavascriptNumber::GetValue(address));
  2888. break;
  2889. case Js::TypeIds_Undefined:
  2890. WriteToBuffer(&buffer, &n, _u(" (undefined)"));
  2891. break;
  2892. case Js::TypeIds_Null:
  2893. WriteToBuffer(&buffer, &n, _u(" (null)"));
  2894. break;
  2895. case Js::TypeIds_GlobalObject:
  2896. WriteToBuffer(&buffer, &n, _u(" (GlobalObject)"));
  2897. break;
  2898. case Js::TypeIds_UndeclBlockVar:
  2899. WriteToBuffer(&buffer, &n, _u(" (UndeclBlockVar)"));
  2900. break;
  2901. case Js::TypeIds_Function:
  2902. DumpFunctionInfo(&buffer, &n, ((Js::JavascriptFunction *)address)->GetFunctionInfo(), printToConsole, _u("FunctionObject"));
  2903. break;
  2904. default:
  2905. WriteToBuffer(&buffer, &n, _u(" (DynamicObject)"));
  2906. break;
  2907. }
  2908. }
  2909. }
  2910. break;
  2911. case IR::AddrOpndKindConstantVar:
  2912. {
  2913. #ifdef _M_X64_OR_ARM64
  2914. char16 const * format = _u("0x%012I64X%s");
  2915. #else
  2916. char16 const * format = _u("0x%08X%s");
  2917. #endif
  2918. char16 const * addressName = _u("");
  2919. if (address == Js::JavascriptArray::MissingItem)
  2920. {
  2921. addressName = _u(" (MissingItem)");
  2922. }
  2923. #if FLOATVAR
  2924. else if (address == (Js::Var)Js::FloatTag_Value)
  2925. {
  2926. addressName = _u(" (FloatTag)");
  2927. }
  2928. #endif
  2929. WriteToBuffer(&buffer, &n, format, address, addressName);
  2930. break;
  2931. }
  2932. case IR::AddrOpndKindDynamicScriptContext:
  2933. Assert(func == nullptr || (intptr_t)address == func->GetScriptContextInfo()->GetAddr());
  2934. // The script context pointer is unstable allocated from the CRT
  2935. DumpAddress(address, printToConsole, skipMaskedAddress);
  2936. WriteToBuffer(&buffer, &n, _u(" (ScriptContext)"));
  2937. break;
  2938. case IR::AddrOpndKindDynamicCharStringCache:
  2939. Assert(func == nullptr || (intptr_t)address == func->GetScriptContextInfo()->GetCharStringCacheAddr());
  2940. DumpAddress(address, printToConsole, skipMaskedAddress);
  2941. WriteToBuffer(&buffer, &n, _u(" (CharStringCache)"));
  2942. break;
  2943. case IR::AddrOpndKindDynamicBailOutRecord:
  2944. DumpAddress(address, printToConsole, skipMaskedAddress);
  2945. WriteToBuffer(&buffer, &n, _u(" (BailOutRecord)"));
  2946. break;
  2947. case IR::AddrOpndKindDynamicInlineCache:
  2948. DumpAddress(address, printToConsole, skipMaskedAddress);
  2949. WriteToBuffer(&buffer, &n, _u(" (InlineCache)"));
  2950. break;
  2951. case IR::AddrOpndKindDynamicIsInstInlineCacheFunctionRef:
  2952. DumpAddress(address, printToConsole, skipMaskedAddress);
  2953. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.function)"));
  2954. break;
  2955. case IR::AddrOpndKindDynamicIsInstInlineCacheTypeRef:
  2956. DumpAddress(address, printToConsole, skipMaskedAddress);
  2957. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.type)"));
  2958. break;
  2959. case IR::AddrOpndKindDynamicIsInstInlineCacheResultRef:
  2960. DumpAddress(address, printToConsole, skipMaskedAddress);
  2961. WriteToBuffer(&buffer, &n, _u(" (&IsInstInlineCache.result)"));
  2962. break;
  2963. case AddrOpndKindDynamicGuardValueRef:
  2964. DumpAddress(address, printToConsole, skipMaskedAddress);
  2965. WriteToBuffer(&buffer, &n, _u(" (&GuardValue)"));
  2966. break;
  2967. case AddrOpndKindDynamicAuxSlotArrayRef:
  2968. DumpAddress(address, printToConsole, skipMaskedAddress);
  2969. WriteToBuffer(&buffer, &n, _u(" (&AuxSlotArray)"));
  2970. break;
  2971. case AddrOpndKindDynamicPropertySlotRef:
  2972. DumpAddress(address, printToConsole, skipMaskedAddress);
  2973. WriteToBuffer(&buffer, &n, _u(" (&PropertySlot)"));
  2974. break;
  2975. case AddrOpndKindDynamicBailOutKindRef:
  2976. DumpAddress(address, printToConsole, skipMaskedAddress);
  2977. WriteToBuffer(&buffer, &n, _u(" (&BailOutKind)"));
  2978. break;
  2979. case AddrOpndKindDynamicArrayCallSiteInfo:
  2980. DumpAddress(address, printToConsole, skipMaskedAddress);
  2981. WriteToBuffer(&buffer, &n, _u(" (ArrayCallSiteInfo)"));
  2982. break;
  2983. case AddrOpndKindDynamicTypeCheckGuard:
  2984. DumpAddress(address, printToConsole, skipMaskedAddress);
  2985. WriteToBuffer(&buffer, &n, _u(" (TypeCheckGuard)"));
  2986. break;
  2987. case AddrOpndKindDynamicRecyclerAllocatorEndAddressRef:
  2988. DumpAddress(address, printToConsole, skipMaskedAddress);
  2989. WriteToBuffer(&buffer, &n, _u(" (&RecyclerAllocatorEndAddress)"));
  2990. break;
  2991. case AddrOpndKindDynamicAuxBufferRef:
  2992. DumpAddress(address, printToConsole, skipMaskedAddress);
  2993. WriteToBuffer(&buffer, &n, _u(" (AuxBufferRef)"));
  2994. break;
  2995. case AddrOpndKindDynamicRecyclerAllocatorFreeListRef:
  2996. DumpAddress(address, printToConsole, skipMaskedAddress);
  2997. WriteToBuffer(&buffer, &n, _u(" (&RecyclerAllocatorFreeList)"));
  2998. break;
  2999. case IR::AddrOpndKindDynamicFunctionInfo:
  3000. DumpAddress(address, printToConsole, skipMaskedAddress);
  3001. if (func->IsOOPJIT())
  3002. {
  3003. // TODO: OOP JIT, dump more info
  3004. WriteToBuffer(&buffer, &n, _u(" (FunctionInfo)"));
  3005. }
  3006. else
  3007. {
  3008. DumpFunctionInfo(&buffer, &n, (Js::FunctionInfo *)address, printToConsole);
  3009. }
  3010. break;
  3011. case IR::AddrOpndKindDynamicFunctionBody:
  3012. DumpAddress(address, printToConsole, skipMaskedAddress);
  3013. if (func->IsOOPJIT())
  3014. {
  3015. // TODO: OOP JIT, dump more info
  3016. WriteToBuffer(&buffer, &n, _u(" (FunctionBody)"));
  3017. }
  3018. else
  3019. {
  3020. DumpFunctionInfo(&buffer, &n, ((Js::FunctionBody *)address)->GetFunctionInfo(), printToConsole);
  3021. }
  3022. break;
  3023. case IR::AddrOpndKindDynamicFunctionBodyWeakRef:
  3024. DumpAddress(address, printToConsole, skipMaskedAddress);
  3025. if (func->IsOOPJIT())
  3026. {
  3027. // TODO: OOP JIT, dump more info
  3028. WriteToBuffer(&buffer, &n, _u(" (FunctionBodyWeakRef)"));
  3029. }
  3030. else
  3031. {
  3032. DumpFunctionInfo(&buffer, &n, ((RecyclerWeakReference<Js::FunctionBody> *)address)->FastGet()->GetFunctionInfo(), printToConsole, _u("FunctionBodyWeakRef"));
  3033. }
  3034. break;
  3035. case IR::AddrOpndKindDynamicFunctionEnvironmentRef:
  3036. DumpAddress(address, printToConsole, skipMaskedAddress);
  3037. DumpFunctionInfo(&buffer, &n, ((Js::ScriptFunction *)((intptr_t)address - Js::ScriptFunction::GetOffsetOfEnvironment()))->GetFunctionInfo(),
  3038. printToConsole, _u("ScriptFunctionEnvironmentRef"));
  3039. break;
  3040. case IR::AddrOpndKindDynamicVtable:
  3041. if ((INT_PTR)address == Js::ScriptContextOptimizationOverrideInfo::InvalidVtable)
  3042. {
  3043. WriteToBuffer(&buffer, &n, _u("%d (Invalid Vtable)"), Js::ScriptContextOptimizationOverrideInfo::InvalidVtable);
  3044. }
  3045. else
  3046. {
  3047. DumpAddress(address, printToConsole, skipMaskedAddress);
  3048. WriteToBuffer(&buffer, &n, _u(" (%S Vtable)"), func->GetVtableName((INT_PTR)address));
  3049. }
  3050. break;
  3051. case IR::AddrOpndKindDynamicTypeHandler:
  3052. DumpAddress(address, printToConsole, skipMaskedAddress);
  3053. WriteToBuffer(&buffer, &n, _u(" (TypeHandler)"));
  3054. break;
  3055. case IR::AddrOpndKindDynamicObjectTypeRef:
  3056. DumpAddress(address, printToConsole, skipMaskedAddress);
  3057. {
  3058. Js::RecyclableObject * dynamicObject = (Js::RecyclableObject *)((intptr_t)address - Js::RecyclableObject::GetOffsetOfType());
  3059. if (!func->IsOOPJIT() && Js::JavascriptFunction::Is(dynamicObject))
  3060. {
  3061. DumpFunctionInfo(&buffer, &n, Js::JavascriptFunction::FromVar((void *)((intptr_t)address - Js::RecyclableObject::GetOffsetOfType()))->GetFunctionInfo(),
  3062. printToConsole, _u("FunctionObjectTypeRef"));
  3063. }
  3064. else
  3065. {
  3066. // TODO: OOP JIT, dump more info
  3067. WriteToBuffer(&buffer, &n, _u(" (ObjectTypeRef)"));
  3068. }
  3069. }
  3070. break;
  3071. case IR::AddrOpndKindDynamicType:
  3072. DumpAddress(address, printToConsole, skipMaskedAddress);
  3073. // TODO: OOP JIT, dump more info
  3074. if(!func->IsOOPJIT())
  3075. {
  3076. Js::TypeId typeId = ((Js::Type*)address)->GetTypeId();
  3077. switch (typeId)
  3078. {
  3079. case Js::TypeIds_Number:
  3080. WriteToBuffer(&buffer, &n, _u(" (Type: StaticNumber)"));
  3081. break;
  3082. case Js::TypeIds_String:
  3083. WriteToBuffer(&buffer, &n, _u(" (Type: StaticString)"));
  3084. break;
  3085. case Js::TypeIds_Object:
  3086. WriteToBuffer(&buffer, &n, _u(" (Type: Object)"));
  3087. break;
  3088. case Js::TypeIds_RegEx:
  3089. WriteToBuffer(&buffer, &n, _u(" (Type: Regex)"));
  3090. break;
  3091. case Js::TypeIds_Array:
  3092. WriteToBuffer(&buffer, &n, _u(" (Type: Array)"));
  3093. break;
  3094. case Js::TypeIds_NativeIntArray:
  3095. WriteToBuffer(&buffer, &n, _u(" (Type: NativeIntArray)"));
  3096. break;
  3097. case Js::TypeIds_NativeFloatArray:
  3098. WriteToBuffer(&buffer, &n, _u(" (Type: NativeFltArray)"));
  3099. break;
  3100. default:
  3101. WriteToBuffer(&buffer, &n, _u(" (Type: Id %d)"), typeId);
  3102. break;
  3103. }
  3104. }
  3105. break;
  3106. case AddrOpndKindDynamicFrameDisplay:
  3107. {
  3108. Js::FrameDisplay * frameDisplay = (Js::FrameDisplay *)address;
  3109. WriteToBuffer(&buffer, &n, (frameDisplay->GetStrictMode() ? _u(" (StrictFrameDisplay len %d)") : _u(" (FrameDisplay len %d)")),
  3110. frameDisplay->GetLength());
  3111. }
  3112. break;
  3113. case AddrOpndKindSz:
  3114. WriteToBuffer(&buffer, &n, wcslen((char16 const *)address) > 30 ? _u("\"%.30s...\"") : _u("\"%.30s\""), address);
  3115. break;
  3116. case AddrOpndKindDynamicFloatRef:
  3117. DumpAddress(address, printToConsole, skipMaskedAddress);
  3118. WriteToBuffer(&buffer, &n, _u(" (&(float)%f)"), *(float *)address);
  3119. break;
  3120. case AddrOpndKindDynamicDoubleRef:
  3121. DumpAddress(address, printToConsole, skipMaskedAddress);
  3122. WriteToBuffer(&buffer, &n, _u(" (&(double)%f)"), *(double *)address);
  3123. break;
  3124. case AddrOpndKindForInCache:
  3125. DumpAddress(address, printToConsole, skipMaskedAddress);
  3126. WriteToBuffer(&buffer, &n, _u(" (ForInCache)"));
  3127. break;
  3128. case AddrOpndKindForInCacheType:
  3129. DumpAddress(address, printToConsole, skipMaskedAddress);
  3130. WriteToBuffer(&buffer, &n, _u(" (&ForInCache->type)"));
  3131. break;
  3132. case AddrOpndKindForInCacheData:
  3133. DumpAddress(address, printToConsole, skipMaskedAddress);
  3134. WriteToBuffer(&buffer, &n, _u(" (&ForInCache->data)"));
  3135. break;
  3136. case AddrOpndKindDynamicNativeCodeDataRef:
  3137. DumpAddress(address, printToConsole, skipMaskedAddress);
  3138. WriteToBuffer(&buffer, &n, _u(" (&NativeCodeData)"));
  3139. break;
  3140. default:
  3141. DumpAddress(address, printToConsole, skipMaskedAddress);
  3142. if ((intptr_t)address == func->GetThreadContextInfo()->GetNullFrameDisplayAddr())
  3143. {
  3144. WriteToBuffer(&buffer, &n, _u(" (NullFrameDisplay)"));
  3145. }
  3146. else if ((intptr_t)address == func->GetThreadContextInfo()->GetStrictNullFrameDisplayAddr())
  3147. {
  3148. WriteToBuffer(&buffer, &n, _u(" (StrictNullFrameDisplay)"));
  3149. }
  3150. else if ((intptr_t)address == func->GetScriptContextInfo()->GetNumberAllocatorAddr())
  3151. {
  3152. WriteToBuffer(&buffer, &n, _u(" (NumberAllocator)"));
  3153. }
  3154. else if ((intptr_t)address == func->GetScriptContextInfo()->GetRecyclerAddr())
  3155. {
  3156. WriteToBuffer(&buffer, &n, _u(" (Recycler)"));
  3157. }
  3158. else if (func->GetWorkItem()->Type() == JsFunctionType && (intptr_t)address == func->GetWorkItem()->GetCallsCountAddress())
  3159. {
  3160. WriteToBuffer(&buffer, &n, _u(" (&CallCount)"));
  3161. }
  3162. else if ((intptr_t)address == func->GetThreadContextInfo()->GetImplicitCallFlagsAddr())
  3163. {
  3164. WriteToBuffer(&buffer, &n, _u(" (&ImplicitCallFlags)"));
  3165. }
  3166. else if ((intptr_t)address == func->GetThreadContextInfo()->GetDisableImplicitFlagsAddr())
  3167. {
  3168. WriteToBuffer(&buffer, &n, _u(" (&DisableImplicitCallFlags)"));
  3169. }
  3170. else if ((intptr_t)address == func->GetThreadContextInfo()->GetThreadStackLimitAddr())
  3171. {
  3172. WriteToBuffer(&buffer, &n, _u(" (&StackLimit)"));
  3173. }
  3174. else if (func->CanAllocInPreReservedHeapPageSegment() &&
  3175. #if ENABLE_OOP_NATIVE_CODEGEN
  3176. (func->IsOOPJIT()
  3177. ? func->GetOOPThreadContext()->GetPreReservedSectionAllocator()->IsPreReservedEndAddress(address)
  3178. : func->GetInProcThreadContext()->GetPreReservedVirtualAllocator()->IsPreReservedEndAddress(address)
  3179. )
  3180. #else
  3181. func->GetInProcThreadContext()->GetPreReservedVirtualAllocator()->IsPreReservedEndAddress(address)
  3182. #endif
  3183. )
  3184. {
  3185. WriteToBuffer(&buffer, &n, _u(" (PreReservedCodeSegmentEnd)"));
  3186. }
  3187. else if ((intptr_t)address == func->GetScriptContextInfo()->GetSideEffectsAddr())
  3188. {
  3189. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_SideEffects)"));
  3190. }
  3191. else if ((intptr_t)address == func->GetScriptContextInfo()->GetArraySetElementFastPathVtableAddr())
  3192. {
  3193. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_ArraySetElementFastPathVtable)"));
  3194. }
  3195. else if ((intptr_t)address == func->GetScriptContextInfo()->GetIntArraySetElementFastPathVtableAddr())
  3196. {
  3197. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_IntArraySetElementFastPathVtable)"));
  3198. }
  3199. else if ((intptr_t)address == func->GetScriptContextInfo()->GetFloatArraySetElementFastPathVtableAddr())
  3200. {
  3201. WriteToBuffer(&buffer, &n, _u(" (&OptimizationOverrides_FloatArraySetElementFastPathVtable)"));
  3202. }
  3203. else
  3204. {
  3205. WriteToBuffer(&buffer, &n, _u(" (Unknown)"));
  3206. }
  3207. }
  3208. }
  3209. else
  3210. {
  3211. WriteToBuffer(&buffer, &n, _u("(NULL)"));
  3212. }
  3213. }
  3214. /**
  3215. GetAddrDescription
  3216. Determine the type of the address and place at most <count> wide chars of the
  3217. description into <description>.
  3218. Force null termination of <description>.
  3219. @param description
  3220. A buffer which will hold the description.
  3221. @param count
  3222. The maximum number of characters that should be returned in <description>.
  3223. @param AsmDumpMode
  3224. @param func
  3225. */
  3226. void
  3227. Opnd::GetAddrDescription(__out_ecount(count) char16 *const description, const size_t count, bool AsmDumpMode,
  3228. bool printToConsole, Func *func)
  3229. {
  3230. char16 *buffer = description;
  3231. size_t n = count;
  3232. IR::AddrOpnd * addrOpnd = this->AsAddrOpnd();
  3233. Js::Var address;
  3234. bool isEncoded = false;
  3235. if (addrOpnd->decodedValue != 0)
  3236. {
  3237. address = addrOpnd->decodedValue;
  3238. isEncoded = true;
  3239. }
  3240. else
  3241. {
  3242. address = addrOpnd->m_address;
  3243. }
  3244. GetAddrDescription(description, count, address, addrOpnd->GetAddrOpndKind(), AsmDumpMode, printToConsole, func);
  3245. if (isEncoded)
  3246. {
  3247. if (AsmDumpMode)
  3248. {
  3249. WriteToBuffer(&buffer, &n, _u(" [encoded]"));
  3250. }
  3251. else
  3252. {
  3253. WriteToBuffer(&buffer, &n, _u(" [encoded: 0x%08X"), addrOpnd->m_address);
  3254. }
  3255. }
  3256. description[count-1] = 0; // force null termination
  3257. }
  3258. void
  3259. Opnd::Dump()
  3260. {
  3261. this->Dump(IRDumpFlags_None, nullptr);
  3262. }
  3263. #endif
  3264. } // namespace IR