tcg-op.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706
  1. /*
  2. * Tiny Code Generator for QEMU
  3. *
  4. * Copyright (c) 2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "exec/exec-all.h"
  26. #include "tcg/tcg.h"
  27. #include "tcg/tcg-temp-internal.h"
  28. #include "tcg/tcg-op.h"
  29. #include "tcg/tcg-mo.h"
  30. #include "exec/plugin-gen.h"
  31. #include "tcg-internal.h"
  32. void tcg_gen_op1(TCGOpcode opc, TCGArg a1)
  33. {
  34. TCGOp *op = tcg_emit_op(opc, 1);
  35. op->args[0] = a1;
  36. }
  37. void tcg_gen_op2(TCGOpcode opc, TCGArg a1, TCGArg a2)
  38. {
  39. TCGOp *op = tcg_emit_op(opc, 2);
  40. op->args[0] = a1;
  41. op->args[1] = a2;
  42. }
  43. void tcg_gen_op3(TCGOpcode opc, TCGArg a1, TCGArg a2, TCGArg a3)
  44. {
  45. TCGOp *op = tcg_emit_op(opc, 3);
  46. op->args[0] = a1;
  47. op->args[1] = a2;
  48. op->args[2] = a3;
  49. }
  50. void tcg_gen_op4(TCGOpcode opc, TCGArg a1, TCGArg a2, TCGArg a3, TCGArg a4)
  51. {
  52. TCGOp *op = tcg_emit_op(opc, 4);
  53. op->args[0] = a1;
  54. op->args[1] = a2;
  55. op->args[2] = a3;
  56. op->args[3] = a4;
  57. }
  58. void tcg_gen_op5(TCGOpcode opc, TCGArg a1, TCGArg a2, TCGArg a3,
  59. TCGArg a4, TCGArg a5)
  60. {
  61. TCGOp *op = tcg_emit_op(opc, 5);
  62. op->args[0] = a1;
  63. op->args[1] = a2;
  64. op->args[2] = a3;
  65. op->args[3] = a4;
  66. op->args[4] = a5;
  67. }
  68. void tcg_gen_op6(TCGOpcode opc, TCGArg a1, TCGArg a2, TCGArg a3,
  69. TCGArg a4, TCGArg a5, TCGArg a6)
  70. {
  71. TCGOp *op = tcg_emit_op(opc, 6);
  72. op->args[0] = a1;
  73. op->args[1] = a2;
  74. op->args[2] = a3;
  75. op->args[3] = a4;
  76. op->args[4] = a5;
  77. op->args[5] = a6;
  78. }
  79. /* Generic ops. */
  80. static void add_last_as_label_use(TCGLabel *l)
  81. {
  82. TCGLabelUse *u = tcg_malloc(sizeof(TCGLabelUse));
  83. u->op = tcg_last_op();
  84. QSIMPLEQ_INSERT_TAIL(&l->branches, u, next);
  85. }
  86. void tcg_gen_br(TCGLabel *l)
  87. {
  88. tcg_gen_op1(INDEX_op_br, label_arg(l));
  89. add_last_as_label_use(l);
  90. }
  91. void tcg_gen_mb(TCGBar mb_type)
  92. {
  93. if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) {
  94. tcg_gen_op1(INDEX_op_mb, mb_type);
  95. }
  96. }
  97. /* 32 bit ops */
  98. void tcg_gen_movi_i32(TCGv_i32 ret, int32_t arg)
  99. {
  100. tcg_gen_mov_i32(ret, tcg_constant_i32(arg));
  101. }
  102. void tcg_gen_addi_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  103. {
  104. /* some cases can be optimized here */
  105. if (arg2 == 0) {
  106. tcg_gen_mov_i32(ret, arg1);
  107. } else {
  108. tcg_gen_add_i32(ret, arg1, tcg_constant_i32(arg2));
  109. }
  110. }
  111. void tcg_gen_subfi_i32(TCGv_i32 ret, int32_t arg1, TCGv_i32 arg2)
  112. {
  113. if (arg1 == 0 && TCG_TARGET_HAS_neg_i32) {
  114. /* Don't recurse with tcg_gen_neg_i32. */
  115. tcg_gen_op2_i32(INDEX_op_neg_i32, ret, arg2);
  116. } else {
  117. tcg_gen_sub_i32(ret, tcg_constant_i32(arg1), arg2);
  118. }
  119. }
  120. void tcg_gen_subi_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  121. {
  122. /* some cases can be optimized here */
  123. if (arg2 == 0) {
  124. tcg_gen_mov_i32(ret, arg1);
  125. } else {
  126. tcg_gen_sub_i32(ret, arg1, tcg_constant_i32(arg2));
  127. }
  128. }
  129. void tcg_gen_andi_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  130. {
  131. /* Some cases can be optimized here. */
  132. switch (arg2) {
  133. case 0:
  134. tcg_gen_movi_i32(ret, 0);
  135. return;
  136. case -1:
  137. tcg_gen_mov_i32(ret, arg1);
  138. return;
  139. case 0xff:
  140. /* Don't recurse with tcg_gen_ext8u_i32. */
  141. if (TCG_TARGET_HAS_ext8u_i32) {
  142. tcg_gen_op2_i32(INDEX_op_ext8u_i32, ret, arg1);
  143. return;
  144. }
  145. break;
  146. case 0xffff:
  147. if (TCG_TARGET_HAS_ext16u_i32) {
  148. tcg_gen_op2_i32(INDEX_op_ext16u_i32, ret, arg1);
  149. return;
  150. }
  151. break;
  152. }
  153. tcg_gen_and_i32(ret, arg1, tcg_constant_i32(arg2));
  154. }
  155. void tcg_gen_ori_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  156. {
  157. /* Some cases can be optimized here. */
  158. if (arg2 == -1) {
  159. tcg_gen_movi_i32(ret, -1);
  160. } else if (arg2 == 0) {
  161. tcg_gen_mov_i32(ret, arg1);
  162. } else {
  163. tcg_gen_or_i32(ret, arg1, tcg_constant_i32(arg2));
  164. }
  165. }
  166. void tcg_gen_xori_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  167. {
  168. /* Some cases can be optimized here. */
  169. if (arg2 == 0) {
  170. tcg_gen_mov_i32(ret, arg1);
  171. } else if (arg2 == -1 && TCG_TARGET_HAS_not_i32) {
  172. /* Don't recurse with tcg_gen_not_i32. */
  173. tcg_gen_op2_i32(INDEX_op_not_i32, ret, arg1);
  174. } else {
  175. tcg_gen_xor_i32(ret, arg1, tcg_constant_i32(arg2));
  176. }
  177. }
  178. void tcg_gen_shli_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  179. {
  180. tcg_debug_assert(arg2 >= 0 && arg2 < 32);
  181. if (arg2 == 0) {
  182. tcg_gen_mov_i32(ret, arg1);
  183. } else {
  184. tcg_gen_shl_i32(ret, arg1, tcg_constant_i32(arg2));
  185. }
  186. }
  187. void tcg_gen_shri_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  188. {
  189. tcg_debug_assert(arg2 >= 0 && arg2 < 32);
  190. if (arg2 == 0) {
  191. tcg_gen_mov_i32(ret, arg1);
  192. } else {
  193. tcg_gen_shr_i32(ret, arg1, tcg_constant_i32(arg2));
  194. }
  195. }
  196. void tcg_gen_sari_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  197. {
  198. tcg_debug_assert(arg2 >= 0 && arg2 < 32);
  199. if (arg2 == 0) {
  200. tcg_gen_mov_i32(ret, arg1);
  201. } else {
  202. tcg_gen_sar_i32(ret, arg1, tcg_constant_i32(arg2));
  203. }
  204. }
  205. void tcg_gen_brcond_i32(TCGCond cond, TCGv_i32 arg1, TCGv_i32 arg2, TCGLabel *l)
  206. {
  207. if (cond == TCG_COND_ALWAYS) {
  208. tcg_gen_br(l);
  209. } else if (cond != TCG_COND_NEVER) {
  210. tcg_gen_op4ii_i32(INDEX_op_brcond_i32, arg1, arg2, cond, label_arg(l));
  211. add_last_as_label_use(l);
  212. }
  213. }
  214. void tcg_gen_brcondi_i32(TCGCond cond, TCGv_i32 arg1, int32_t arg2, TCGLabel *l)
  215. {
  216. if (cond == TCG_COND_ALWAYS) {
  217. tcg_gen_br(l);
  218. } else if (cond != TCG_COND_NEVER) {
  219. tcg_gen_brcond_i32(cond, arg1, tcg_constant_i32(arg2), l);
  220. }
  221. }
  222. void tcg_gen_setcond_i32(TCGCond cond, TCGv_i32 ret,
  223. TCGv_i32 arg1, TCGv_i32 arg2)
  224. {
  225. if (cond == TCG_COND_ALWAYS) {
  226. tcg_gen_movi_i32(ret, 1);
  227. } else if (cond == TCG_COND_NEVER) {
  228. tcg_gen_movi_i32(ret, 0);
  229. } else {
  230. tcg_gen_op4i_i32(INDEX_op_setcond_i32, ret, arg1, arg2, cond);
  231. }
  232. }
  233. void tcg_gen_setcondi_i32(TCGCond cond, TCGv_i32 ret,
  234. TCGv_i32 arg1, int32_t arg2)
  235. {
  236. tcg_gen_setcond_i32(cond, ret, arg1, tcg_constant_i32(arg2));
  237. }
  238. void tcg_gen_muli_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  239. {
  240. if (arg2 == 0) {
  241. tcg_gen_movi_i32(ret, 0);
  242. } else if (is_power_of_2(arg2)) {
  243. tcg_gen_shli_i32(ret, arg1, ctz32(arg2));
  244. } else {
  245. tcg_gen_mul_i32(ret, arg1, tcg_constant_i32(arg2));
  246. }
  247. }
  248. void tcg_gen_div_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  249. {
  250. if (TCG_TARGET_HAS_div_i32) {
  251. tcg_gen_op3_i32(INDEX_op_div_i32, ret, arg1, arg2);
  252. } else if (TCG_TARGET_HAS_div2_i32) {
  253. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  254. tcg_gen_sari_i32(t0, arg1, 31);
  255. tcg_gen_op5_i32(INDEX_op_div2_i32, ret, t0, arg1, t0, arg2);
  256. tcg_temp_free_i32(t0);
  257. } else {
  258. gen_helper_div_i32(ret, arg1, arg2);
  259. }
  260. }
  261. void tcg_gen_rem_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  262. {
  263. if (TCG_TARGET_HAS_rem_i32) {
  264. tcg_gen_op3_i32(INDEX_op_rem_i32, ret, arg1, arg2);
  265. } else if (TCG_TARGET_HAS_div_i32) {
  266. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  267. tcg_gen_op3_i32(INDEX_op_div_i32, t0, arg1, arg2);
  268. tcg_gen_mul_i32(t0, t0, arg2);
  269. tcg_gen_sub_i32(ret, arg1, t0);
  270. tcg_temp_free_i32(t0);
  271. } else if (TCG_TARGET_HAS_div2_i32) {
  272. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  273. tcg_gen_sari_i32(t0, arg1, 31);
  274. tcg_gen_op5_i32(INDEX_op_div2_i32, t0, ret, arg1, t0, arg2);
  275. tcg_temp_free_i32(t0);
  276. } else {
  277. gen_helper_rem_i32(ret, arg1, arg2);
  278. }
  279. }
  280. void tcg_gen_divu_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  281. {
  282. if (TCG_TARGET_HAS_div_i32) {
  283. tcg_gen_op3_i32(INDEX_op_divu_i32, ret, arg1, arg2);
  284. } else if (TCG_TARGET_HAS_div2_i32) {
  285. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  286. tcg_gen_movi_i32(t0, 0);
  287. tcg_gen_op5_i32(INDEX_op_divu2_i32, ret, t0, arg1, t0, arg2);
  288. tcg_temp_free_i32(t0);
  289. } else {
  290. gen_helper_divu_i32(ret, arg1, arg2);
  291. }
  292. }
  293. void tcg_gen_remu_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  294. {
  295. if (TCG_TARGET_HAS_rem_i32) {
  296. tcg_gen_op3_i32(INDEX_op_remu_i32, ret, arg1, arg2);
  297. } else if (TCG_TARGET_HAS_div_i32) {
  298. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  299. tcg_gen_op3_i32(INDEX_op_divu_i32, t0, arg1, arg2);
  300. tcg_gen_mul_i32(t0, t0, arg2);
  301. tcg_gen_sub_i32(ret, arg1, t0);
  302. tcg_temp_free_i32(t0);
  303. } else if (TCG_TARGET_HAS_div2_i32) {
  304. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  305. tcg_gen_movi_i32(t0, 0);
  306. tcg_gen_op5_i32(INDEX_op_divu2_i32, t0, ret, arg1, t0, arg2);
  307. tcg_temp_free_i32(t0);
  308. } else {
  309. gen_helper_remu_i32(ret, arg1, arg2);
  310. }
  311. }
  312. void tcg_gen_andc_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  313. {
  314. if (TCG_TARGET_HAS_andc_i32) {
  315. tcg_gen_op3_i32(INDEX_op_andc_i32, ret, arg1, arg2);
  316. } else {
  317. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  318. tcg_gen_not_i32(t0, arg2);
  319. tcg_gen_and_i32(ret, arg1, t0);
  320. tcg_temp_free_i32(t0);
  321. }
  322. }
  323. void tcg_gen_eqv_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  324. {
  325. if (TCG_TARGET_HAS_eqv_i32) {
  326. tcg_gen_op3_i32(INDEX_op_eqv_i32, ret, arg1, arg2);
  327. } else {
  328. tcg_gen_xor_i32(ret, arg1, arg2);
  329. tcg_gen_not_i32(ret, ret);
  330. }
  331. }
  332. void tcg_gen_nand_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  333. {
  334. if (TCG_TARGET_HAS_nand_i32) {
  335. tcg_gen_op3_i32(INDEX_op_nand_i32, ret, arg1, arg2);
  336. } else {
  337. tcg_gen_and_i32(ret, arg1, arg2);
  338. tcg_gen_not_i32(ret, ret);
  339. }
  340. }
  341. void tcg_gen_nor_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  342. {
  343. if (TCG_TARGET_HAS_nor_i32) {
  344. tcg_gen_op3_i32(INDEX_op_nor_i32, ret, arg1, arg2);
  345. } else {
  346. tcg_gen_or_i32(ret, arg1, arg2);
  347. tcg_gen_not_i32(ret, ret);
  348. }
  349. }
  350. void tcg_gen_orc_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  351. {
  352. if (TCG_TARGET_HAS_orc_i32) {
  353. tcg_gen_op3_i32(INDEX_op_orc_i32, ret, arg1, arg2);
  354. } else {
  355. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  356. tcg_gen_not_i32(t0, arg2);
  357. tcg_gen_or_i32(ret, arg1, t0);
  358. tcg_temp_free_i32(t0);
  359. }
  360. }
  361. void tcg_gen_clz_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  362. {
  363. if (TCG_TARGET_HAS_clz_i32) {
  364. tcg_gen_op3_i32(INDEX_op_clz_i32, ret, arg1, arg2);
  365. } else if (TCG_TARGET_HAS_clz_i64) {
  366. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  367. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  368. tcg_gen_extu_i32_i64(t1, arg1);
  369. tcg_gen_extu_i32_i64(t2, arg2);
  370. tcg_gen_addi_i64(t2, t2, 32);
  371. tcg_gen_clz_i64(t1, t1, t2);
  372. tcg_gen_extrl_i64_i32(ret, t1);
  373. tcg_temp_free_i64(t1);
  374. tcg_temp_free_i64(t2);
  375. tcg_gen_subi_i32(ret, ret, 32);
  376. } else {
  377. gen_helper_clz_i32(ret, arg1, arg2);
  378. }
  379. }
  380. void tcg_gen_clzi_i32(TCGv_i32 ret, TCGv_i32 arg1, uint32_t arg2)
  381. {
  382. tcg_gen_clz_i32(ret, arg1, tcg_constant_i32(arg2));
  383. }
  384. void tcg_gen_ctz_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  385. {
  386. if (TCG_TARGET_HAS_ctz_i32) {
  387. tcg_gen_op3_i32(INDEX_op_ctz_i32, ret, arg1, arg2);
  388. } else if (TCG_TARGET_HAS_ctz_i64) {
  389. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  390. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  391. tcg_gen_extu_i32_i64(t1, arg1);
  392. tcg_gen_extu_i32_i64(t2, arg2);
  393. tcg_gen_ctz_i64(t1, t1, t2);
  394. tcg_gen_extrl_i64_i32(ret, t1);
  395. tcg_temp_free_i64(t1);
  396. tcg_temp_free_i64(t2);
  397. } else if (TCG_TARGET_HAS_ctpop_i32
  398. || TCG_TARGET_HAS_ctpop_i64
  399. || TCG_TARGET_HAS_clz_i32
  400. || TCG_TARGET_HAS_clz_i64) {
  401. TCGv_i32 z, t = tcg_temp_ebb_new_i32();
  402. if (TCG_TARGET_HAS_ctpop_i32 || TCG_TARGET_HAS_ctpop_i64) {
  403. tcg_gen_subi_i32(t, arg1, 1);
  404. tcg_gen_andc_i32(t, t, arg1);
  405. tcg_gen_ctpop_i32(t, t);
  406. } else {
  407. /* Since all non-x86 hosts have clz(0) == 32, don't fight it. */
  408. tcg_gen_neg_i32(t, arg1);
  409. tcg_gen_and_i32(t, t, arg1);
  410. tcg_gen_clzi_i32(t, t, 32);
  411. tcg_gen_xori_i32(t, t, 31);
  412. }
  413. z = tcg_constant_i32(0);
  414. tcg_gen_movcond_i32(TCG_COND_EQ, ret, arg1, z, arg2, t);
  415. tcg_temp_free_i32(t);
  416. } else {
  417. gen_helper_ctz_i32(ret, arg1, arg2);
  418. }
  419. }
  420. void tcg_gen_ctzi_i32(TCGv_i32 ret, TCGv_i32 arg1, uint32_t arg2)
  421. {
  422. if (!TCG_TARGET_HAS_ctz_i32 && TCG_TARGET_HAS_ctpop_i32 && arg2 == 32) {
  423. /* This equivalence has the advantage of not requiring a fixup. */
  424. TCGv_i32 t = tcg_temp_ebb_new_i32();
  425. tcg_gen_subi_i32(t, arg1, 1);
  426. tcg_gen_andc_i32(t, t, arg1);
  427. tcg_gen_ctpop_i32(ret, t);
  428. tcg_temp_free_i32(t);
  429. } else {
  430. tcg_gen_ctz_i32(ret, arg1, tcg_constant_i32(arg2));
  431. }
  432. }
  433. void tcg_gen_clrsb_i32(TCGv_i32 ret, TCGv_i32 arg)
  434. {
  435. if (TCG_TARGET_HAS_clz_i32) {
  436. TCGv_i32 t = tcg_temp_ebb_new_i32();
  437. tcg_gen_sari_i32(t, arg, 31);
  438. tcg_gen_xor_i32(t, t, arg);
  439. tcg_gen_clzi_i32(t, t, 32);
  440. tcg_gen_subi_i32(ret, t, 1);
  441. tcg_temp_free_i32(t);
  442. } else {
  443. gen_helper_clrsb_i32(ret, arg);
  444. }
  445. }
  446. void tcg_gen_ctpop_i32(TCGv_i32 ret, TCGv_i32 arg1)
  447. {
  448. if (TCG_TARGET_HAS_ctpop_i32) {
  449. tcg_gen_op2_i32(INDEX_op_ctpop_i32, ret, arg1);
  450. } else if (TCG_TARGET_HAS_ctpop_i64) {
  451. TCGv_i64 t = tcg_temp_ebb_new_i64();
  452. tcg_gen_extu_i32_i64(t, arg1);
  453. tcg_gen_ctpop_i64(t, t);
  454. tcg_gen_extrl_i64_i32(ret, t);
  455. tcg_temp_free_i64(t);
  456. } else {
  457. gen_helper_ctpop_i32(ret, arg1);
  458. }
  459. }
  460. void tcg_gen_rotl_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  461. {
  462. if (TCG_TARGET_HAS_rot_i32) {
  463. tcg_gen_op3_i32(INDEX_op_rotl_i32, ret, arg1, arg2);
  464. } else {
  465. TCGv_i32 t0, t1;
  466. t0 = tcg_temp_ebb_new_i32();
  467. t1 = tcg_temp_ebb_new_i32();
  468. tcg_gen_shl_i32(t0, arg1, arg2);
  469. tcg_gen_subfi_i32(t1, 32, arg2);
  470. tcg_gen_shr_i32(t1, arg1, t1);
  471. tcg_gen_or_i32(ret, t0, t1);
  472. tcg_temp_free_i32(t0);
  473. tcg_temp_free_i32(t1);
  474. }
  475. }
  476. void tcg_gen_rotli_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  477. {
  478. tcg_debug_assert(arg2 >= 0 && arg2 < 32);
  479. /* some cases can be optimized here */
  480. if (arg2 == 0) {
  481. tcg_gen_mov_i32(ret, arg1);
  482. } else if (TCG_TARGET_HAS_rot_i32) {
  483. tcg_gen_rotl_i32(ret, arg1, tcg_constant_i32(arg2));
  484. } else {
  485. TCGv_i32 t0, t1;
  486. t0 = tcg_temp_ebb_new_i32();
  487. t1 = tcg_temp_ebb_new_i32();
  488. tcg_gen_shli_i32(t0, arg1, arg2);
  489. tcg_gen_shri_i32(t1, arg1, 32 - arg2);
  490. tcg_gen_or_i32(ret, t0, t1);
  491. tcg_temp_free_i32(t0);
  492. tcg_temp_free_i32(t1);
  493. }
  494. }
  495. void tcg_gen_rotr_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2)
  496. {
  497. if (TCG_TARGET_HAS_rot_i32) {
  498. tcg_gen_op3_i32(INDEX_op_rotr_i32, ret, arg1, arg2);
  499. } else {
  500. TCGv_i32 t0, t1;
  501. t0 = tcg_temp_ebb_new_i32();
  502. t1 = tcg_temp_ebb_new_i32();
  503. tcg_gen_shr_i32(t0, arg1, arg2);
  504. tcg_gen_subfi_i32(t1, 32, arg2);
  505. tcg_gen_shl_i32(t1, arg1, t1);
  506. tcg_gen_or_i32(ret, t0, t1);
  507. tcg_temp_free_i32(t0);
  508. tcg_temp_free_i32(t1);
  509. }
  510. }
  511. void tcg_gen_rotri_i32(TCGv_i32 ret, TCGv_i32 arg1, int32_t arg2)
  512. {
  513. tcg_debug_assert(arg2 >= 0 && arg2 < 32);
  514. /* some cases can be optimized here */
  515. if (arg2 == 0) {
  516. tcg_gen_mov_i32(ret, arg1);
  517. } else {
  518. tcg_gen_rotli_i32(ret, arg1, 32 - arg2);
  519. }
  520. }
  521. void tcg_gen_deposit_i32(TCGv_i32 ret, TCGv_i32 arg1, TCGv_i32 arg2,
  522. unsigned int ofs, unsigned int len)
  523. {
  524. uint32_t mask;
  525. TCGv_i32 t1;
  526. tcg_debug_assert(ofs < 32);
  527. tcg_debug_assert(len > 0);
  528. tcg_debug_assert(len <= 32);
  529. tcg_debug_assert(ofs + len <= 32);
  530. if (len == 32) {
  531. tcg_gen_mov_i32(ret, arg2);
  532. return;
  533. }
  534. if (TCG_TARGET_HAS_deposit_i32 && TCG_TARGET_deposit_i32_valid(ofs, len)) {
  535. tcg_gen_op5ii_i32(INDEX_op_deposit_i32, ret, arg1, arg2, ofs, len);
  536. return;
  537. }
  538. t1 = tcg_temp_ebb_new_i32();
  539. if (TCG_TARGET_HAS_extract2_i32) {
  540. if (ofs + len == 32) {
  541. tcg_gen_shli_i32(t1, arg1, len);
  542. tcg_gen_extract2_i32(ret, t1, arg2, len);
  543. goto done;
  544. }
  545. if (ofs == 0) {
  546. tcg_gen_extract2_i32(ret, arg1, arg2, len);
  547. tcg_gen_rotli_i32(ret, ret, len);
  548. goto done;
  549. }
  550. }
  551. mask = (1u << len) - 1;
  552. if (ofs + len < 32) {
  553. tcg_gen_andi_i32(t1, arg2, mask);
  554. tcg_gen_shli_i32(t1, t1, ofs);
  555. } else {
  556. tcg_gen_shli_i32(t1, arg2, ofs);
  557. }
  558. tcg_gen_andi_i32(ret, arg1, ~(mask << ofs));
  559. tcg_gen_or_i32(ret, ret, t1);
  560. done:
  561. tcg_temp_free_i32(t1);
  562. }
  563. void tcg_gen_deposit_z_i32(TCGv_i32 ret, TCGv_i32 arg,
  564. unsigned int ofs, unsigned int len)
  565. {
  566. tcg_debug_assert(ofs < 32);
  567. tcg_debug_assert(len > 0);
  568. tcg_debug_assert(len <= 32);
  569. tcg_debug_assert(ofs + len <= 32);
  570. if (ofs + len == 32) {
  571. tcg_gen_shli_i32(ret, arg, ofs);
  572. } else if (ofs == 0) {
  573. tcg_gen_andi_i32(ret, arg, (1u << len) - 1);
  574. } else if (TCG_TARGET_HAS_deposit_i32
  575. && TCG_TARGET_deposit_i32_valid(ofs, len)) {
  576. TCGv_i32 zero = tcg_constant_i32(0);
  577. tcg_gen_op5ii_i32(INDEX_op_deposit_i32, ret, zero, arg, ofs, len);
  578. } else {
  579. /* To help two-operand hosts we prefer to zero-extend first,
  580. which allows ARG to stay live. */
  581. switch (len) {
  582. case 16:
  583. if (TCG_TARGET_HAS_ext16u_i32) {
  584. tcg_gen_ext16u_i32(ret, arg);
  585. tcg_gen_shli_i32(ret, ret, ofs);
  586. return;
  587. }
  588. break;
  589. case 8:
  590. if (TCG_TARGET_HAS_ext8u_i32) {
  591. tcg_gen_ext8u_i32(ret, arg);
  592. tcg_gen_shli_i32(ret, ret, ofs);
  593. return;
  594. }
  595. break;
  596. }
  597. /* Otherwise prefer zero-extension over AND for code size. */
  598. switch (ofs + len) {
  599. case 16:
  600. if (TCG_TARGET_HAS_ext16u_i32) {
  601. tcg_gen_shli_i32(ret, arg, ofs);
  602. tcg_gen_ext16u_i32(ret, ret);
  603. return;
  604. }
  605. break;
  606. case 8:
  607. if (TCG_TARGET_HAS_ext8u_i32) {
  608. tcg_gen_shli_i32(ret, arg, ofs);
  609. tcg_gen_ext8u_i32(ret, ret);
  610. return;
  611. }
  612. break;
  613. }
  614. tcg_gen_andi_i32(ret, arg, (1u << len) - 1);
  615. tcg_gen_shli_i32(ret, ret, ofs);
  616. }
  617. }
  618. void tcg_gen_extract_i32(TCGv_i32 ret, TCGv_i32 arg,
  619. unsigned int ofs, unsigned int len)
  620. {
  621. tcg_debug_assert(ofs < 32);
  622. tcg_debug_assert(len > 0);
  623. tcg_debug_assert(len <= 32);
  624. tcg_debug_assert(ofs + len <= 32);
  625. /* Canonicalize certain special cases, even if extract is supported. */
  626. if (ofs + len == 32) {
  627. tcg_gen_shri_i32(ret, arg, 32 - len);
  628. return;
  629. }
  630. if (ofs == 0) {
  631. tcg_gen_andi_i32(ret, arg, (1u << len) - 1);
  632. return;
  633. }
  634. if (TCG_TARGET_HAS_extract_i32
  635. && TCG_TARGET_extract_i32_valid(ofs, len)) {
  636. tcg_gen_op4ii_i32(INDEX_op_extract_i32, ret, arg, ofs, len);
  637. return;
  638. }
  639. /* Assume that zero-extension, if available, is cheaper than a shift. */
  640. switch (ofs + len) {
  641. case 16:
  642. if (TCG_TARGET_HAS_ext16u_i32) {
  643. tcg_gen_ext16u_i32(ret, arg);
  644. tcg_gen_shri_i32(ret, ret, ofs);
  645. return;
  646. }
  647. break;
  648. case 8:
  649. if (TCG_TARGET_HAS_ext8u_i32) {
  650. tcg_gen_ext8u_i32(ret, arg);
  651. tcg_gen_shri_i32(ret, ret, ofs);
  652. return;
  653. }
  654. break;
  655. }
  656. /* ??? Ideally we'd know what values are available for immediate AND.
  657. Assume that 8 bits are available, plus the special case of 16,
  658. so that we get ext8u, ext16u. */
  659. switch (len) {
  660. case 1 ... 8: case 16:
  661. tcg_gen_shri_i32(ret, arg, ofs);
  662. tcg_gen_andi_i32(ret, ret, (1u << len) - 1);
  663. break;
  664. default:
  665. tcg_gen_shli_i32(ret, arg, 32 - len - ofs);
  666. tcg_gen_shri_i32(ret, ret, 32 - len);
  667. break;
  668. }
  669. }
  670. void tcg_gen_sextract_i32(TCGv_i32 ret, TCGv_i32 arg,
  671. unsigned int ofs, unsigned int len)
  672. {
  673. tcg_debug_assert(ofs < 32);
  674. tcg_debug_assert(len > 0);
  675. tcg_debug_assert(len <= 32);
  676. tcg_debug_assert(ofs + len <= 32);
  677. /* Canonicalize certain special cases, even if extract is supported. */
  678. if (ofs + len == 32) {
  679. tcg_gen_sari_i32(ret, arg, 32 - len);
  680. return;
  681. }
  682. if (ofs == 0) {
  683. switch (len) {
  684. case 16:
  685. tcg_gen_ext16s_i32(ret, arg);
  686. return;
  687. case 8:
  688. tcg_gen_ext8s_i32(ret, arg);
  689. return;
  690. }
  691. }
  692. if (TCG_TARGET_HAS_sextract_i32
  693. && TCG_TARGET_extract_i32_valid(ofs, len)) {
  694. tcg_gen_op4ii_i32(INDEX_op_sextract_i32, ret, arg, ofs, len);
  695. return;
  696. }
  697. /* Assume that sign-extension, if available, is cheaper than a shift. */
  698. switch (ofs + len) {
  699. case 16:
  700. if (TCG_TARGET_HAS_ext16s_i32) {
  701. tcg_gen_ext16s_i32(ret, arg);
  702. tcg_gen_sari_i32(ret, ret, ofs);
  703. return;
  704. }
  705. break;
  706. case 8:
  707. if (TCG_TARGET_HAS_ext8s_i32) {
  708. tcg_gen_ext8s_i32(ret, arg);
  709. tcg_gen_sari_i32(ret, ret, ofs);
  710. return;
  711. }
  712. break;
  713. }
  714. switch (len) {
  715. case 16:
  716. if (TCG_TARGET_HAS_ext16s_i32) {
  717. tcg_gen_shri_i32(ret, arg, ofs);
  718. tcg_gen_ext16s_i32(ret, ret);
  719. return;
  720. }
  721. break;
  722. case 8:
  723. if (TCG_TARGET_HAS_ext8s_i32) {
  724. tcg_gen_shri_i32(ret, arg, ofs);
  725. tcg_gen_ext8s_i32(ret, ret);
  726. return;
  727. }
  728. break;
  729. }
  730. tcg_gen_shli_i32(ret, arg, 32 - len - ofs);
  731. tcg_gen_sari_i32(ret, ret, 32 - len);
  732. }
  733. /*
  734. * Extract 32-bits from a 64-bit input, ah:al, starting from ofs.
  735. * Unlike tcg_gen_extract_i32 above, len is fixed at 32.
  736. */
  737. void tcg_gen_extract2_i32(TCGv_i32 ret, TCGv_i32 al, TCGv_i32 ah,
  738. unsigned int ofs)
  739. {
  740. tcg_debug_assert(ofs <= 32);
  741. if (ofs == 0) {
  742. tcg_gen_mov_i32(ret, al);
  743. } else if (ofs == 32) {
  744. tcg_gen_mov_i32(ret, ah);
  745. } else if (al == ah) {
  746. tcg_gen_rotri_i32(ret, al, ofs);
  747. } else if (TCG_TARGET_HAS_extract2_i32) {
  748. tcg_gen_op4i_i32(INDEX_op_extract2_i32, ret, al, ah, ofs);
  749. } else {
  750. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  751. tcg_gen_shri_i32(t0, al, ofs);
  752. tcg_gen_deposit_i32(ret, t0, ah, 32 - ofs, ofs);
  753. tcg_temp_free_i32(t0);
  754. }
  755. }
  756. void tcg_gen_movcond_i32(TCGCond cond, TCGv_i32 ret, TCGv_i32 c1,
  757. TCGv_i32 c2, TCGv_i32 v1, TCGv_i32 v2)
  758. {
  759. if (cond == TCG_COND_ALWAYS) {
  760. tcg_gen_mov_i32(ret, v1);
  761. } else if (cond == TCG_COND_NEVER) {
  762. tcg_gen_mov_i32(ret, v2);
  763. } else if (TCG_TARGET_HAS_movcond_i32) {
  764. tcg_gen_op6i_i32(INDEX_op_movcond_i32, ret, c1, c2, v1, v2, cond);
  765. } else {
  766. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  767. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  768. tcg_gen_setcond_i32(cond, t0, c1, c2);
  769. tcg_gen_neg_i32(t0, t0);
  770. tcg_gen_and_i32(t1, v1, t0);
  771. tcg_gen_andc_i32(ret, v2, t0);
  772. tcg_gen_or_i32(ret, ret, t1);
  773. tcg_temp_free_i32(t0);
  774. tcg_temp_free_i32(t1);
  775. }
  776. }
  777. void tcg_gen_add2_i32(TCGv_i32 rl, TCGv_i32 rh, TCGv_i32 al,
  778. TCGv_i32 ah, TCGv_i32 bl, TCGv_i32 bh)
  779. {
  780. if (TCG_TARGET_HAS_add2_i32) {
  781. tcg_gen_op6_i32(INDEX_op_add2_i32, rl, rh, al, ah, bl, bh);
  782. } else {
  783. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  784. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  785. tcg_gen_concat_i32_i64(t0, al, ah);
  786. tcg_gen_concat_i32_i64(t1, bl, bh);
  787. tcg_gen_add_i64(t0, t0, t1);
  788. tcg_gen_extr_i64_i32(rl, rh, t0);
  789. tcg_temp_free_i64(t0);
  790. tcg_temp_free_i64(t1);
  791. }
  792. }
  793. void tcg_gen_sub2_i32(TCGv_i32 rl, TCGv_i32 rh, TCGv_i32 al,
  794. TCGv_i32 ah, TCGv_i32 bl, TCGv_i32 bh)
  795. {
  796. if (TCG_TARGET_HAS_sub2_i32) {
  797. tcg_gen_op6_i32(INDEX_op_sub2_i32, rl, rh, al, ah, bl, bh);
  798. } else {
  799. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  800. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  801. tcg_gen_concat_i32_i64(t0, al, ah);
  802. tcg_gen_concat_i32_i64(t1, bl, bh);
  803. tcg_gen_sub_i64(t0, t0, t1);
  804. tcg_gen_extr_i64_i32(rl, rh, t0);
  805. tcg_temp_free_i64(t0);
  806. tcg_temp_free_i64(t1);
  807. }
  808. }
  809. void tcg_gen_mulu2_i32(TCGv_i32 rl, TCGv_i32 rh, TCGv_i32 arg1, TCGv_i32 arg2)
  810. {
  811. if (TCG_TARGET_HAS_mulu2_i32) {
  812. tcg_gen_op4_i32(INDEX_op_mulu2_i32, rl, rh, arg1, arg2);
  813. } else if (TCG_TARGET_HAS_muluh_i32) {
  814. TCGv_i32 t = tcg_temp_ebb_new_i32();
  815. tcg_gen_op3_i32(INDEX_op_mul_i32, t, arg1, arg2);
  816. tcg_gen_op3_i32(INDEX_op_muluh_i32, rh, arg1, arg2);
  817. tcg_gen_mov_i32(rl, t);
  818. tcg_temp_free_i32(t);
  819. } else if (TCG_TARGET_REG_BITS == 64) {
  820. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  821. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  822. tcg_gen_extu_i32_i64(t0, arg1);
  823. tcg_gen_extu_i32_i64(t1, arg2);
  824. tcg_gen_mul_i64(t0, t0, t1);
  825. tcg_gen_extr_i64_i32(rl, rh, t0);
  826. tcg_temp_free_i64(t0);
  827. tcg_temp_free_i64(t1);
  828. } else {
  829. qemu_build_not_reached();
  830. }
  831. }
  832. void tcg_gen_muls2_i32(TCGv_i32 rl, TCGv_i32 rh, TCGv_i32 arg1, TCGv_i32 arg2)
  833. {
  834. if (TCG_TARGET_HAS_muls2_i32) {
  835. tcg_gen_op4_i32(INDEX_op_muls2_i32, rl, rh, arg1, arg2);
  836. } else if (TCG_TARGET_HAS_mulsh_i32) {
  837. TCGv_i32 t = tcg_temp_ebb_new_i32();
  838. tcg_gen_op3_i32(INDEX_op_mul_i32, t, arg1, arg2);
  839. tcg_gen_op3_i32(INDEX_op_mulsh_i32, rh, arg1, arg2);
  840. tcg_gen_mov_i32(rl, t);
  841. tcg_temp_free_i32(t);
  842. } else if (TCG_TARGET_REG_BITS == 32) {
  843. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  844. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  845. TCGv_i32 t2 = tcg_temp_ebb_new_i32();
  846. TCGv_i32 t3 = tcg_temp_ebb_new_i32();
  847. tcg_gen_mulu2_i32(t0, t1, arg1, arg2);
  848. /* Adjust for negative inputs. */
  849. tcg_gen_sari_i32(t2, arg1, 31);
  850. tcg_gen_sari_i32(t3, arg2, 31);
  851. tcg_gen_and_i32(t2, t2, arg2);
  852. tcg_gen_and_i32(t3, t3, arg1);
  853. tcg_gen_sub_i32(rh, t1, t2);
  854. tcg_gen_sub_i32(rh, rh, t3);
  855. tcg_gen_mov_i32(rl, t0);
  856. tcg_temp_free_i32(t0);
  857. tcg_temp_free_i32(t1);
  858. tcg_temp_free_i32(t2);
  859. tcg_temp_free_i32(t3);
  860. } else {
  861. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  862. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  863. tcg_gen_ext_i32_i64(t0, arg1);
  864. tcg_gen_ext_i32_i64(t1, arg2);
  865. tcg_gen_mul_i64(t0, t0, t1);
  866. tcg_gen_extr_i64_i32(rl, rh, t0);
  867. tcg_temp_free_i64(t0);
  868. tcg_temp_free_i64(t1);
  869. }
  870. }
  871. void tcg_gen_mulsu2_i32(TCGv_i32 rl, TCGv_i32 rh, TCGv_i32 arg1, TCGv_i32 arg2)
  872. {
  873. if (TCG_TARGET_REG_BITS == 32) {
  874. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  875. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  876. TCGv_i32 t2 = tcg_temp_ebb_new_i32();
  877. tcg_gen_mulu2_i32(t0, t1, arg1, arg2);
  878. /* Adjust for negative input for the signed arg1. */
  879. tcg_gen_sari_i32(t2, arg1, 31);
  880. tcg_gen_and_i32(t2, t2, arg2);
  881. tcg_gen_sub_i32(rh, t1, t2);
  882. tcg_gen_mov_i32(rl, t0);
  883. tcg_temp_free_i32(t0);
  884. tcg_temp_free_i32(t1);
  885. tcg_temp_free_i32(t2);
  886. } else {
  887. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  888. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  889. tcg_gen_ext_i32_i64(t0, arg1);
  890. tcg_gen_extu_i32_i64(t1, arg2);
  891. tcg_gen_mul_i64(t0, t0, t1);
  892. tcg_gen_extr_i64_i32(rl, rh, t0);
  893. tcg_temp_free_i64(t0);
  894. tcg_temp_free_i64(t1);
  895. }
  896. }
  897. void tcg_gen_ext8s_i32(TCGv_i32 ret, TCGv_i32 arg)
  898. {
  899. if (TCG_TARGET_HAS_ext8s_i32) {
  900. tcg_gen_op2_i32(INDEX_op_ext8s_i32, ret, arg);
  901. } else {
  902. tcg_gen_shli_i32(ret, arg, 24);
  903. tcg_gen_sari_i32(ret, ret, 24);
  904. }
  905. }
  906. void tcg_gen_ext16s_i32(TCGv_i32 ret, TCGv_i32 arg)
  907. {
  908. if (TCG_TARGET_HAS_ext16s_i32) {
  909. tcg_gen_op2_i32(INDEX_op_ext16s_i32, ret, arg);
  910. } else {
  911. tcg_gen_shli_i32(ret, arg, 16);
  912. tcg_gen_sari_i32(ret, ret, 16);
  913. }
  914. }
  915. void tcg_gen_ext8u_i32(TCGv_i32 ret, TCGv_i32 arg)
  916. {
  917. if (TCG_TARGET_HAS_ext8u_i32) {
  918. tcg_gen_op2_i32(INDEX_op_ext8u_i32, ret, arg);
  919. } else {
  920. tcg_gen_andi_i32(ret, arg, 0xffu);
  921. }
  922. }
  923. void tcg_gen_ext16u_i32(TCGv_i32 ret, TCGv_i32 arg)
  924. {
  925. if (TCG_TARGET_HAS_ext16u_i32) {
  926. tcg_gen_op2_i32(INDEX_op_ext16u_i32, ret, arg);
  927. } else {
  928. tcg_gen_andi_i32(ret, arg, 0xffffu);
  929. }
  930. }
  931. void tcg_gen_bswap16_i32(TCGv_i32 ret, TCGv_i32 arg, int flags)
  932. {
  933. /* Only one extension flag may be present. */
  934. tcg_debug_assert(!(flags & TCG_BSWAP_OS) || !(flags & TCG_BSWAP_OZ));
  935. if (TCG_TARGET_HAS_bswap16_i32) {
  936. tcg_gen_op3i_i32(INDEX_op_bswap16_i32, ret, arg, flags);
  937. } else {
  938. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  939. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  940. tcg_gen_shri_i32(t0, arg, 8);
  941. if (!(flags & TCG_BSWAP_IZ)) {
  942. tcg_gen_ext8u_i32(t0, t0);
  943. }
  944. if (flags & TCG_BSWAP_OS) {
  945. tcg_gen_shli_i32(t1, arg, 24);
  946. tcg_gen_sari_i32(t1, t1, 16);
  947. } else if (flags & TCG_BSWAP_OZ) {
  948. tcg_gen_ext8u_i32(t1, arg);
  949. tcg_gen_shli_i32(t1, t1, 8);
  950. } else {
  951. tcg_gen_shli_i32(t1, arg, 8);
  952. }
  953. tcg_gen_or_i32(ret, t0, t1);
  954. tcg_temp_free_i32(t0);
  955. tcg_temp_free_i32(t1);
  956. }
  957. }
  958. void tcg_gen_bswap32_i32(TCGv_i32 ret, TCGv_i32 arg)
  959. {
  960. if (TCG_TARGET_HAS_bswap32_i32) {
  961. tcg_gen_op3i_i32(INDEX_op_bswap32_i32, ret, arg, 0);
  962. } else {
  963. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  964. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  965. TCGv_i32 t2 = tcg_constant_i32(0x00ff00ff);
  966. /* arg = abcd */
  967. tcg_gen_shri_i32(t0, arg, 8); /* t0 = .abc */
  968. tcg_gen_and_i32(t1, arg, t2); /* t1 = .b.d */
  969. tcg_gen_and_i32(t0, t0, t2); /* t0 = .a.c */
  970. tcg_gen_shli_i32(t1, t1, 8); /* t1 = b.d. */
  971. tcg_gen_or_i32(ret, t0, t1); /* ret = badc */
  972. tcg_gen_shri_i32(t0, ret, 16); /* t0 = ..ba */
  973. tcg_gen_shli_i32(t1, ret, 16); /* t1 = dc.. */
  974. tcg_gen_or_i32(ret, t0, t1); /* ret = dcba */
  975. tcg_temp_free_i32(t0);
  976. tcg_temp_free_i32(t1);
  977. }
  978. }
  979. void tcg_gen_hswap_i32(TCGv_i32 ret, TCGv_i32 arg)
  980. {
  981. /* Swapping 2 16-bit elements is a rotate. */
  982. tcg_gen_rotli_i32(ret, arg, 16);
  983. }
  984. void tcg_gen_smin_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
  985. {
  986. tcg_gen_movcond_i32(TCG_COND_LT, ret, a, b, a, b);
  987. }
  988. void tcg_gen_umin_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
  989. {
  990. tcg_gen_movcond_i32(TCG_COND_LTU, ret, a, b, a, b);
  991. }
  992. void tcg_gen_smax_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
  993. {
  994. tcg_gen_movcond_i32(TCG_COND_LT, ret, a, b, b, a);
  995. }
  996. void tcg_gen_umax_i32(TCGv_i32 ret, TCGv_i32 a, TCGv_i32 b)
  997. {
  998. tcg_gen_movcond_i32(TCG_COND_LTU, ret, a, b, b, a);
  999. }
  1000. void tcg_gen_abs_i32(TCGv_i32 ret, TCGv_i32 a)
  1001. {
  1002. TCGv_i32 t = tcg_temp_ebb_new_i32();
  1003. tcg_gen_sari_i32(t, a, 31);
  1004. tcg_gen_xor_i32(ret, a, t);
  1005. tcg_gen_sub_i32(ret, ret, t);
  1006. tcg_temp_free_i32(t);
  1007. }
  1008. /* 64-bit ops */
  1009. #if TCG_TARGET_REG_BITS == 32
  1010. /* These are all inline for TCG_TARGET_REG_BITS == 64. */
  1011. void tcg_gen_discard_i64(TCGv_i64 arg)
  1012. {
  1013. tcg_gen_discard_i32(TCGV_LOW(arg));
  1014. tcg_gen_discard_i32(TCGV_HIGH(arg));
  1015. }
  1016. void tcg_gen_mov_i64(TCGv_i64 ret, TCGv_i64 arg)
  1017. {
  1018. TCGTemp *ts = tcgv_i64_temp(arg);
  1019. /* Canonicalize TCGv_i64 TEMP_CONST into TCGv_i32 TEMP_CONST. */
  1020. if (ts->kind == TEMP_CONST) {
  1021. tcg_gen_movi_i64(ret, ts->val);
  1022. } else {
  1023. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1024. tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg));
  1025. }
  1026. }
  1027. void tcg_gen_movi_i64(TCGv_i64 ret, int64_t arg)
  1028. {
  1029. tcg_gen_movi_i32(TCGV_LOW(ret), arg);
  1030. tcg_gen_movi_i32(TCGV_HIGH(ret), arg >> 32);
  1031. }
  1032. void tcg_gen_ld8u_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1033. {
  1034. tcg_gen_ld8u_i32(TCGV_LOW(ret), arg2, offset);
  1035. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1036. }
  1037. void tcg_gen_ld8s_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1038. {
  1039. tcg_gen_ld8s_i32(TCGV_LOW(ret), arg2, offset);
  1040. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1041. }
  1042. void tcg_gen_ld16u_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1043. {
  1044. tcg_gen_ld16u_i32(TCGV_LOW(ret), arg2, offset);
  1045. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1046. }
  1047. void tcg_gen_ld16s_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1048. {
  1049. tcg_gen_ld16s_i32(TCGV_LOW(ret), arg2, offset);
  1050. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1051. }
  1052. void tcg_gen_ld32u_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1053. {
  1054. tcg_gen_ld_i32(TCGV_LOW(ret), arg2, offset);
  1055. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1056. }
  1057. void tcg_gen_ld32s_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1058. {
  1059. tcg_gen_ld_i32(TCGV_LOW(ret), arg2, offset);
  1060. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1061. }
  1062. void tcg_gen_ld_i64(TCGv_i64 ret, TCGv_ptr arg2, tcg_target_long offset)
  1063. {
  1064. /* Since arg2 and ret have different types,
  1065. they cannot be the same temporary */
  1066. #if HOST_BIG_ENDIAN
  1067. tcg_gen_ld_i32(TCGV_HIGH(ret), arg2, offset);
  1068. tcg_gen_ld_i32(TCGV_LOW(ret), arg2, offset + 4);
  1069. #else
  1070. tcg_gen_ld_i32(TCGV_LOW(ret), arg2, offset);
  1071. tcg_gen_ld_i32(TCGV_HIGH(ret), arg2, offset + 4);
  1072. #endif
  1073. }
  1074. void tcg_gen_st8_i64(TCGv_i64 arg1, TCGv_ptr arg2, tcg_target_long offset)
  1075. {
  1076. tcg_gen_st8_i32(TCGV_LOW(arg1), arg2, offset);
  1077. }
  1078. void tcg_gen_st16_i64(TCGv_i64 arg1, TCGv_ptr arg2, tcg_target_long offset)
  1079. {
  1080. tcg_gen_st16_i32(TCGV_LOW(arg1), arg2, offset);
  1081. }
  1082. void tcg_gen_st32_i64(TCGv_i64 arg1, TCGv_ptr arg2, tcg_target_long offset)
  1083. {
  1084. tcg_gen_st_i32(TCGV_LOW(arg1), arg2, offset);
  1085. }
  1086. void tcg_gen_st_i64(TCGv_i64 arg1, TCGv_ptr arg2, tcg_target_long offset)
  1087. {
  1088. #if HOST_BIG_ENDIAN
  1089. tcg_gen_st_i32(TCGV_HIGH(arg1), arg2, offset);
  1090. tcg_gen_st_i32(TCGV_LOW(arg1), arg2, offset + 4);
  1091. #else
  1092. tcg_gen_st_i32(TCGV_LOW(arg1), arg2, offset);
  1093. tcg_gen_st_i32(TCGV_HIGH(arg1), arg2, offset + 4);
  1094. #endif
  1095. }
  1096. void tcg_gen_add_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1097. {
  1098. tcg_gen_add2_i32(TCGV_LOW(ret), TCGV_HIGH(ret), TCGV_LOW(arg1),
  1099. TCGV_HIGH(arg1), TCGV_LOW(arg2), TCGV_HIGH(arg2));
  1100. }
  1101. void tcg_gen_sub_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1102. {
  1103. tcg_gen_sub2_i32(TCGV_LOW(ret), TCGV_HIGH(ret), TCGV_LOW(arg1),
  1104. TCGV_HIGH(arg1), TCGV_LOW(arg2), TCGV_HIGH(arg2));
  1105. }
  1106. void tcg_gen_and_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1107. {
  1108. tcg_gen_and_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1109. tcg_gen_and_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1110. }
  1111. void tcg_gen_or_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1112. {
  1113. tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1114. tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1115. }
  1116. void tcg_gen_xor_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1117. {
  1118. tcg_gen_xor_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1119. tcg_gen_xor_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1120. }
  1121. void tcg_gen_shl_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1122. {
  1123. gen_helper_shl_i64(ret, arg1, arg2);
  1124. }
  1125. void tcg_gen_shr_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1126. {
  1127. gen_helper_shr_i64(ret, arg1, arg2);
  1128. }
  1129. void tcg_gen_sar_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1130. {
  1131. gen_helper_sar_i64(ret, arg1, arg2);
  1132. }
  1133. void tcg_gen_mul_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1134. {
  1135. TCGv_i64 t0;
  1136. TCGv_i32 t1;
  1137. t0 = tcg_temp_ebb_new_i64();
  1138. t1 = tcg_temp_ebb_new_i32();
  1139. tcg_gen_mulu2_i32(TCGV_LOW(t0), TCGV_HIGH(t0),
  1140. TCGV_LOW(arg1), TCGV_LOW(arg2));
  1141. tcg_gen_mul_i32(t1, TCGV_LOW(arg1), TCGV_HIGH(arg2));
  1142. tcg_gen_add_i32(TCGV_HIGH(t0), TCGV_HIGH(t0), t1);
  1143. tcg_gen_mul_i32(t1, TCGV_HIGH(arg1), TCGV_LOW(arg2));
  1144. tcg_gen_add_i32(TCGV_HIGH(t0), TCGV_HIGH(t0), t1);
  1145. tcg_gen_mov_i64(ret, t0);
  1146. tcg_temp_free_i64(t0);
  1147. tcg_temp_free_i32(t1);
  1148. }
  1149. #else
  1150. void tcg_gen_movi_i64(TCGv_i64 ret, int64_t arg)
  1151. {
  1152. tcg_gen_mov_i64(ret, tcg_constant_i64(arg));
  1153. }
  1154. #endif /* TCG_TARGET_REG_SIZE == 32 */
  1155. void tcg_gen_addi_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1156. {
  1157. /* some cases can be optimized here */
  1158. if (arg2 == 0) {
  1159. tcg_gen_mov_i64(ret, arg1);
  1160. } else if (TCG_TARGET_REG_BITS == 64) {
  1161. tcg_gen_add_i64(ret, arg1, tcg_constant_i64(arg2));
  1162. } else {
  1163. tcg_gen_add2_i32(TCGV_LOW(ret), TCGV_HIGH(ret),
  1164. TCGV_LOW(arg1), TCGV_HIGH(arg1),
  1165. tcg_constant_i32(arg2), tcg_constant_i32(arg2 >> 32));
  1166. }
  1167. }
  1168. void tcg_gen_subfi_i64(TCGv_i64 ret, int64_t arg1, TCGv_i64 arg2)
  1169. {
  1170. if (arg1 == 0 && TCG_TARGET_HAS_neg_i64) {
  1171. /* Don't recurse with tcg_gen_neg_i64. */
  1172. tcg_gen_op2_i64(INDEX_op_neg_i64, ret, arg2);
  1173. } else if (TCG_TARGET_REG_BITS == 64) {
  1174. tcg_gen_sub_i64(ret, tcg_constant_i64(arg1), arg2);
  1175. } else {
  1176. tcg_gen_sub2_i32(TCGV_LOW(ret), TCGV_HIGH(ret),
  1177. tcg_constant_i32(arg1), tcg_constant_i32(arg1 >> 32),
  1178. TCGV_LOW(arg2), TCGV_HIGH(arg2));
  1179. }
  1180. }
  1181. void tcg_gen_subi_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1182. {
  1183. /* some cases can be optimized here */
  1184. if (arg2 == 0) {
  1185. tcg_gen_mov_i64(ret, arg1);
  1186. } else if (TCG_TARGET_REG_BITS == 64) {
  1187. tcg_gen_sub_i64(ret, arg1, tcg_constant_i64(arg2));
  1188. } else {
  1189. tcg_gen_sub2_i32(TCGV_LOW(ret), TCGV_HIGH(ret),
  1190. TCGV_LOW(arg1), TCGV_HIGH(arg1),
  1191. tcg_constant_i32(arg2), tcg_constant_i32(arg2 >> 32));
  1192. }
  1193. }
  1194. void tcg_gen_andi_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1195. {
  1196. if (TCG_TARGET_REG_BITS == 32) {
  1197. tcg_gen_andi_i32(TCGV_LOW(ret), TCGV_LOW(arg1), arg2);
  1198. tcg_gen_andi_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), arg2 >> 32);
  1199. return;
  1200. }
  1201. /* Some cases can be optimized here. */
  1202. switch (arg2) {
  1203. case 0:
  1204. tcg_gen_movi_i64(ret, 0);
  1205. return;
  1206. case -1:
  1207. tcg_gen_mov_i64(ret, arg1);
  1208. return;
  1209. case 0xff:
  1210. /* Don't recurse with tcg_gen_ext8u_i64. */
  1211. if (TCG_TARGET_HAS_ext8u_i64) {
  1212. tcg_gen_op2_i64(INDEX_op_ext8u_i64, ret, arg1);
  1213. return;
  1214. }
  1215. break;
  1216. case 0xffff:
  1217. if (TCG_TARGET_HAS_ext16u_i64) {
  1218. tcg_gen_op2_i64(INDEX_op_ext16u_i64, ret, arg1);
  1219. return;
  1220. }
  1221. break;
  1222. case 0xffffffffu:
  1223. if (TCG_TARGET_HAS_ext32u_i64) {
  1224. tcg_gen_op2_i64(INDEX_op_ext32u_i64, ret, arg1);
  1225. return;
  1226. }
  1227. break;
  1228. }
  1229. tcg_gen_and_i64(ret, arg1, tcg_constant_i64(arg2));
  1230. }
  1231. void tcg_gen_ori_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1232. {
  1233. if (TCG_TARGET_REG_BITS == 32) {
  1234. tcg_gen_ori_i32(TCGV_LOW(ret), TCGV_LOW(arg1), arg2);
  1235. tcg_gen_ori_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), arg2 >> 32);
  1236. return;
  1237. }
  1238. /* Some cases can be optimized here. */
  1239. if (arg2 == -1) {
  1240. tcg_gen_movi_i64(ret, -1);
  1241. } else if (arg2 == 0) {
  1242. tcg_gen_mov_i64(ret, arg1);
  1243. } else {
  1244. tcg_gen_or_i64(ret, arg1, tcg_constant_i64(arg2));
  1245. }
  1246. }
  1247. void tcg_gen_xori_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1248. {
  1249. if (TCG_TARGET_REG_BITS == 32) {
  1250. tcg_gen_xori_i32(TCGV_LOW(ret), TCGV_LOW(arg1), arg2);
  1251. tcg_gen_xori_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), arg2 >> 32);
  1252. return;
  1253. }
  1254. /* Some cases can be optimized here. */
  1255. if (arg2 == 0) {
  1256. tcg_gen_mov_i64(ret, arg1);
  1257. } else if (arg2 == -1 && TCG_TARGET_HAS_not_i64) {
  1258. /* Don't recurse with tcg_gen_not_i64. */
  1259. tcg_gen_op2_i64(INDEX_op_not_i64, ret, arg1);
  1260. } else {
  1261. tcg_gen_xor_i64(ret, arg1, tcg_constant_i64(arg2));
  1262. }
  1263. }
  1264. static inline void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
  1265. unsigned c, bool right, bool arith)
  1266. {
  1267. tcg_debug_assert(c < 64);
  1268. if (c == 0) {
  1269. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1));
  1270. tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
  1271. } else if (c >= 32) {
  1272. c -= 32;
  1273. if (right) {
  1274. if (arith) {
  1275. tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
  1276. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31);
  1277. } else {
  1278. tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
  1279. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1280. }
  1281. } else {
  1282. tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_LOW(arg1), c);
  1283. tcg_gen_movi_i32(TCGV_LOW(ret), 0);
  1284. }
  1285. } else if (right) {
  1286. if (TCG_TARGET_HAS_extract2_i32) {
  1287. tcg_gen_extract2_i32(TCGV_LOW(ret),
  1288. TCGV_LOW(arg1), TCGV_HIGH(arg1), c);
  1289. } else {
  1290. tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c);
  1291. tcg_gen_deposit_i32(TCGV_LOW(ret), TCGV_LOW(ret),
  1292. TCGV_HIGH(arg1), 32 - c, c);
  1293. }
  1294. if (arith) {
  1295. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
  1296. } else {
  1297. tcg_gen_shri_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
  1298. }
  1299. } else {
  1300. if (TCG_TARGET_HAS_extract2_i32) {
  1301. tcg_gen_extract2_i32(TCGV_HIGH(ret),
  1302. TCGV_LOW(arg1), TCGV_HIGH(arg1), 32 - c);
  1303. } else {
  1304. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  1305. tcg_gen_shri_i32(t0, TCGV_LOW(arg1), 32 - c);
  1306. tcg_gen_deposit_i32(TCGV_HIGH(ret), t0,
  1307. TCGV_HIGH(arg1), c, 32 - c);
  1308. tcg_temp_free_i32(t0);
  1309. }
  1310. tcg_gen_shli_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c);
  1311. }
  1312. }
  1313. void tcg_gen_shli_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1314. {
  1315. tcg_debug_assert(arg2 >= 0 && arg2 < 64);
  1316. if (TCG_TARGET_REG_BITS == 32) {
  1317. tcg_gen_shifti_i64(ret, arg1, arg2, 0, 0);
  1318. } else if (arg2 == 0) {
  1319. tcg_gen_mov_i64(ret, arg1);
  1320. } else {
  1321. tcg_gen_shl_i64(ret, arg1, tcg_constant_i64(arg2));
  1322. }
  1323. }
  1324. void tcg_gen_shri_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1325. {
  1326. tcg_debug_assert(arg2 >= 0 && arg2 < 64);
  1327. if (TCG_TARGET_REG_BITS == 32) {
  1328. tcg_gen_shifti_i64(ret, arg1, arg2, 1, 0);
  1329. } else if (arg2 == 0) {
  1330. tcg_gen_mov_i64(ret, arg1);
  1331. } else {
  1332. tcg_gen_shr_i64(ret, arg1, tcg_constant_i64(arg2));
  1333. }
  1334. }
  1335. void tcg_gen_sari_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1336. {
  1337. tcg_debug_assert(arg2 >= 0 && arg2 < 64);
  1338. if (TCG_TARGET_REG_BITS == 32) {
  1339. tcg_gen_shifti_i64(ret, arg1, arg2, 1, 1);
  1340. } else if (arg2 == 0) {
  1341. tcg_gen_mov_i64(ret, arg1);
  1342. } else {
  1343. tcg_gen_sar_i64(ret, arg1, tcg_constant_i64(arg2));
  1344. }
  1345. }
  1346. void tcg_gen_brcond_i64(TCGCond cond, TCGv_i64 arg1, TCGv_i64 arg2, TCGLabel *l)
  1347. {
  1348. if (cond == TCG_COND_ALWAYS) {
  1349. tcg_gen_br(l);
  1350. } else if (cond != TCG_COND_NEVER) {
  1351. if (TCG_TARGET_REG_BITS == 32) {
  1352. tcg_gen_op6ii_i32(INDEX_op_brcond2_i32, TCGV_LOW(arg1),
  1353. TCGV_HIGH(arg1), TCGV_LOW(arg2),
  1354. TCGV_HIGH(arg2), cond, label_arg(l));
  1355. } else {
  1356. tcg_gen_op4ii_i64(INDEX_op_brcond_i64, arg1, arg2, cond,
  1357. label_arg(l));
  1358. }
  1359. add_last_as_label_use(l);
  1360. }
  1361. }
  1362. void tcg_gen_brcondi_i64(TCGCond cond, TCGv_i64 arg1, int64_t arg2, TCGLabel *l)
  1363. {
  1364. if (TCG_TARGET_REG_BITS == 64) {
  1365. tcg_gen_brcond_i64(cond, arg1, tcg_constant_i64(arg2), l);
  1366. } else if (cond == TCG_COND_ALWAYS) {
  1367. tcg_gen_br(l);
  1368. } else if (cond != TCG_COND_NEVER) {
  1369. tcg_gen_op6ii_i32(INDEX_op_brcond2_i32,
  1370. TCGV_LOW(arg1), TCGV_HIGH(arg1),
  1371. tcg_constant_i32(arg2),
  1372. tcg_constant_i32(arg2 >> 32),
  1373. cond, label_arg(l));
  1374. add_last_as_label_use(l);
  1375. }
  1376. }
  1377. void tcg_gen_setcond_i64(TCGCond cond, TCGv_i64 ret,
  1378. TCGv_i64 arg1, TCGv_i64 arg2)
  1379. {
  1380. if (cond == TCG_COND_ALWAYS) {
  1381. tcg_gen_movi_i64(ret, 1);
  1382. } else if (cond == TCG_COND_NEVER) {
  1383. tcg_gen_movi_i64(ret, 0);
  1384. } else {
  1385. if (TCG_TARGET_REG_BITS == 32) {
  1386. tcg_gen_op6i_i32(INDEX_op_setcond2_i32, TCGV_LOW(ret),
  1387. TCGV_LOW(arg1), TCGV_HIGH(arg1),
  1388. TCGV_LOW(arg2), TCGV_HIGH(arg2), cond);
  1389. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1390. } else {
  1391. tcg_gen_op4i_i64(INDEX_op_setcond_i64, ret, arg1, arg2, cond);
  1392. }
  1393. }
  1394. }
  1395. void tcg_gen_setcondi_i64(TCGCond cond, TCGv_i64 ret,
  1396. TCGv_i64 arg1, int64_t arg2)
  1397. {
  1398. if (TCG_TARGET_REG_BITS == 64) {
  1399. tcg_gen_setcond_i64(cond, ret, arg1, tcg_constant_i64(arg2));
  1400. } else if (cond == TCG_COND_ALWAYS) {
  1401. tcg_gen_movi_i64(ret, 1);
  1402. } else if (cond == TCG_COND_NEVER) {
  1403. tcg_gen_movi_i64(ret, 0);
  1404. } else {
  1405. tcg_gen_op6i_i32(INDEX_op_setcond2_i32, TCGV_LOW(ret),
  1406. TCGV_LOW(arg1), TCGV_HIGH(arg1),
  1407. tcg_constant_i32(arg2),
  1408. tcg_constant_i32(arg2 >> 32), cond);
  1409. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1410. }
  1411. }
  1412. void tcg_gen_muli_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1413. {
  1414. if (arg2 == 0) {
  1415. tcg_gen_movi_i64(ret, 0);
  1416. } else if (is_power_of_2(arg2)) {
  1417. tcg_gen_shli_i64(ret, arg1, ctz64(arg2));
  1418. } else {
  1419. tcg_gen_mul_i64(ret, arg1, tcg_constant_i64(arg2));
  1420. }
  1421. }
  1422. void tcg_gen_div_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1423. {
  1424. if (TCG_TARGET_HAS_div_i64) {
  1425. tcg_gen_op3_i64(INDEX_op_div_i64, ret, arg1, arg2);
  1426. } else if (TCG_TARGET_HAS_div2_i64) {
  1427. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1428. tcg_gen_sari_i64(t0, arg1, 63);
  1429. tcg_gen_op5_i64(INDEX_op_div2_i64, ret, t0, arg1, t0, arg2);
  1430. tcg_temp_free_i64(t0);
  1431. } else {
  1432. gen_helper_div_i64(ret, arg1, arg2);
  1433. }
  1434. }
  1435. void tcg_gen_rem_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1436. {
  1437. if (TCG_TARGET_HAS_rem_i64) {
  1438. tcg_gen_op3_i64(INDEX_op_rem_i64, ret, arg1, arg2);
  1439. } else if (TCG_TARGET_HAS_div_i64) {
  1440. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1441. tcg_gen_op3_i64(INDEX_op_div_i64, t0, arg1, arg2);
  1442. tcg_gen_mul_i64(t0, t0, arg2);
  1443. tcg_gen_sub_i64(ret, arg1, t0);
  1444. tcg_temp_free_i64(t0);
  1445. } else if (TCG_TARGET_HAS_div2_i64) {
  1446. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1447. tcg_gen_sari_i64(t0, arg1, 63);
  1448. tcg_gen_op5_i64(INDEX_op_div2_i64, t0, ret, arg1, t0, arg2);
  1449. tcg_temp_free_i64(t0);
  1450. } else {
  1451. gen_helper_rem_i64(ret, arg1, arg2);
  1452. }
  1453. }
  1454. void tcg_gen_divu_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1455. {
  1456. if (TCG_TARGET_HAS_div_i64) {
  1457. tcg_gen_op3_i64(INDEX_op_divu_i64, ret, arg1, arg2);
  1458. } else if (TCG_TARGET_HAS_div2_i64) {
  1459. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1460. tcg_gen_movi_i64(t0, 0);
  1461. tcg_gen_op5_i64(INDEX_op_divu2_i64, ret, t0, arg1, t0, arg2);
  1462. tcg_temp_free_i64(t0);
  1463. } else {
  1464. gen_helper_divu_i64(ret, arg1, arg2);
  1465. }
  1466. }
  1467. void tcg_gen_remu_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1468. {
  1469. if (TCG_TARGET_HAS_rem_i64) {
  1470. tcg_gen_op3_i64(INDEX_op_remu_i64, ret, arg1, arg2);
  1471. } else if (TCG_TARGET_HAS_div_i64) {
  1472. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1473. tcg_gen_op3_i64(INDEX_op_divu_i64, t0, arg1, arg2);
  1474. tcg_gen_mul_i64(t0, t0, arg2);
  1475. tcg_gen_sub_i64(ret, arg1, t0);
  1476. tcg_temp_free_i64(t0);
  1477. } else if (TCG_TARGET_HAS_div2_i64) {
  1478. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1479. tcg_gen_movi_i64(t0, 0);
  1480. tcg_gen_op5_i64(INDEX_op_divu2_i64, t0, ret, arg1, t0, arg2);
  1481. tcg_temp_free_i64(t0);
  1482. } else {
  1483. gen_helper_remu_i64(ret, arg1, arg2);
  1484. }
  1485. }
  1486. void tcg_gen_ext8s_i64(TCGv_i64 ret, TCGv_i64 arg)
  1487. {
  1488. if (TCG_TARGET_REG_BITS == 32) {
  1489. tcg_gen_ext8s_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1490. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1491. } else if (TCG_TARGET_HAS_ext8s_i64) {
  1492. tcg_gen_op2_i64(INDEX_op_ext8s_i64, ret, arg);
  1493. } else {
  1494. tcg_gen_shli_i64(ret, arg, 56);
  1495. tcg_gen_sari_i64(ret, ret, 56);
  1496. }
  1497. }
  1498. void tcg_gen_ext16s_i64(TCGv_i64 ret, TCGv_i64 arg)
  1499. {
  1500. if (TCG_TARGET_REG_BITS == 32) {
  1501. tcg_gen_ext16s_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1502. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1503. } else if (TCG_TARGET_HAS_ext16s_i64) {
  1504. tcg_gen_op2_i64(INDEX_op_ext16s_i64, ret, arg);
  1505. } else {
  1506. tcg_gen_shli_i64(ret, arg, 48);
  1507. tcg_gen_sari_i64(ret, ret, 48);
  1508. }
  1509. }
  1510. void tcg_gen_ext32s_i64(TCGv_i64 ret, TCGv_i64 arg)
  1511. {
  1512. if (TCG_TARGET_REG_BITS == 32) {
  1513. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1514. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1515. } else if (TCG_TARGET_HAS_ext32s_i64) {
  1516. tcg_gen_op2_i64(INDEX_op_ext32s_i64, ret, arg);
  1517. } else {
  1518. tcg_gen_shli_i64(ret, arg, 32);
  1519. tcg_gen_sari_i64(ret, ret, 32);
  1520. }
  1521. }
  1522. void tcg_gen_ext8u_i64(TCGv_i64 ret, TCGv_i64 arg)
  1523. {
  1524. if (TCG_TARGET_REG_BITS == 32) {
  1525. tcg_gen_ext8u_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1526. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1527. } else if (TCG_TARGET_HAS_ext8u_i64) {
  1528. tcg_gen_op2_i64(INDEX_op_ext8u_i64, ret, arg);
  1529. } else {
  1530. tcg_gen_andi_i64(ret, arg, 0xffu);
  1531. }
  1532. }
  1533. void tcg_gen_ext16u_i64(TCGv_i64 ret, TCGv_i64 arg)
  1534. {
  1535. if (TCG_TARGET_REG_BITS == 32) {
  1536. tcg_gen_ext16u_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1537. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1538. } else if (TCG_TARGET_HAS_ext16u_i64) {
  1539. tcg_gen_op2_i64(INDEX_op_ext16u_i64, ret, arg);
  1540. } else {
  1541. tcg_gen_andi_i64(ret, arg, 0xffffu);
  1542. }
  1543. }
  1544. void tcg_gen_ext32u_i64(TCGv_i64 ret, TCGv_i64 arg)
  1545. {
  1546. if (TCG_TARGET_REG_BITS == 32) {
  1547. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1548. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1549. } else if (TCG_TARGET_HAS_ext32u_i64) {
  1550. tcg_gen_op2_i64(INDEX_op_ext32u_i64, ret, arg);
  1551. } else {
  1552. tcg_gen_andi_i64(ret, arg, 0xffffffffu);
  1553. }
  1554. }
  1555. void tcg_gen_bswap16_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
  1556. {
  1557. /* Only one extension flag may be present. */
  1558. tcg_debug_assert(!(flags & TCG_BSWAP_OS) || !(flags & TCG_BSWAP_OZ));
  1559. if (TCG_TARGET_REG_BITS == 32) {
  1560. tcg_gen_bswap16_i32(TCGV_LOW(ret), TCGV_LOW(arg), flags);
  1561. if (flags & TCG_BSWAP_OS) {
  1562. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1563. } else {
  1564. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1565. }
  1566. } else if (TCG_TARGET_HAS_bswap16_i64) {
  1567. tcg_gen_op3i_i64(INDEX_op_bswap16_i64, ret, arg, flags);
  1568. } else {
  1569. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1570. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  1571. tcg_gen_shri_i64(t0, arg, 8);
  1572. if (!(flags & TCG_BSWAP_IZ)) {
  1573. tcg_gen_ext8u_i64(t0, t0);
  1574. }
  1575. if (flags & TCG_BSWAP_OS) {
  1576. tcg_gen_shli_i64(t1, arg, 56);
  1577. tcg_gen_sari_i64(t1, t1, 48);
  1578. } else if (flags & TCG_BSWAP_OZ) {
  1579. tcg_gen_ext8u_i64(t1, arg);
  1580. tcg_gen_shli_i64(t1, t1, 8);
  1581. } else {
  1582. tcg_gen_shli_i64(t1, arg, 8);
  1583. }
  1584. tcg_gen_or_i64(ret, t0, t1);
  1585. tcg_temp_free_i64(t0);
  1586. tcg_temp_free_i64(t1);
  1587. }
  1588. }
  1589. void tcg_gen_bswap32_i64(TCGv_i64 ret, TCGv_i64 arg, int flags)
  1590. {
  1591. /* Only one extension flag may be present. */
  1592. tcg_debug_assert(!(flags & TCG_BSWAP_OS) || !(flags & TCG_BSWAP_OZ));
  1593. if (TCG_TARGET_REG_BITS == 32) {
  1594. tcg_gen_bswap32_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1595. if (flags & TCG_BSWAP_OS) {
  1596. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  1597. } else {
  1598. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1599. }
  1600. } else if (TCG_TARGET_HAS_bswap32_i64) {
  1601. tcg_gen_op3i_i64(INDEX_op_bswap32_i64, ret, arg, flags);
  1602. } else {
  1603. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1604. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  1605. TCGv_i64 t2 = tcg_constant_i64(0x00ff00ff);
  1606. /* arg = xxxxabcd */
  1607. tcg_gen_shri_i64(t0, arg, 8); /* t0 = .xxxxabc */
  1608. tcg_gen_and_i64(t1, arg, t2); /* t1 = .....b.d */
  1609. tcg_gen_and_i64(t0, t0, t2); /* t0 = .....a.c */
  1610. tcg_gen_shli_i64(t1, t1, 8); /* t1 = ....b.d. */
  1611. tcg_gen_or_i64(ret, t0, t1); /* ret = ....badc */
  1612. tcg_gen_shli_i64(t1, ret, 48); /* t1 = dc...... */
  1613. tcg_gen_shri_i64(t0, ret, 16); /* t0 = ......ba */
  1614. if (flags & TCG_BSWAP_OS) {
  1615. tcg_gen_sari_i64(t1, t1, 32); /* t1 = ssssdc.. */
  1616. } else {
  1617. tcg_gen_shri_i64(t1, t1, 32); /* t1 = ....dc.. */
  1618. }
  1619. tcg_gen_or_i64(ret, t0, t1); /* ret = ssssdcba */
  1620. tcg_temp_free_i64(t0);
  1621. tcg_temp_free_i64(t1);
  1622. }
  1623. }
  1624. void tcg_gen_bswap64_i64(TCGv_i64 ret, TCGv_i64 arg)
  1625. {
  1626. if (TCG_TARGET_REG_BITS == 32) {
  1627. TCGv_i32 t0, t1;
  1628. t0 = tcg_temp_ebb_new_i32();
  1629. t1 = tcg_temp_ebb_new_i32();
  1630. tcg_gen_bswap32_i32(t0, TCGV_LOW(arg));
  1631. tcg_gen_bswap32_i32(t1, TCGV_HIGH(arg));
  1632. tcg_gen_mov_i32(TCGV_LOW(ret), t1);
  1633. tcg_gen_mov_i32(TCGV_HIGH(ret), t0);
  1634. tcg_temp_free_i32(t0);
  1635. tcg_temp_free_i32(t1);
  1636. } else if (TCG_TARGET_HAS_bswap64_i64) {
  1637. tcg_gen_op3i_i64(INDEX_op_bswap64_i64, ret, arg, 0);
  1638. } else {
  1639. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1640. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  1641. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  1642. /* arg = abcdefgh */
  1643. tcg_gen_movi_i64(t2, 0x00ff00ff00ff00ffull);
  1644. tcg_gen_shri_i64(t0, arg, 8); /* t0 = .abcdefg */
  1645. tcg_gen_and_i64(t1, arg, t2); /* t1 = .b.d.f.h */
  1646. tcg_gen_and_i64(t0, t0, t2); /* t0 = .a.c.e.g */
  1647. tcg_gen_shli_i64(t1, t1, 8); /* t1 = b.d.f.h. */
  1648. tcg_gen_or_i64(ret, t0, t1); /* ret = badcfehg */
  1649. tcg_gen_movi_i64(t2, 0x0000ffff0000ffffull);
  1650. tcg_gen_shri_i64(t0, ret, 16); /* t0 = ..badcfe */
  1651. tcg_gen_and_i64(t1, ret, t2); /* t1 = ..dc..hg */
  1652. tcg_gen_and_i64(t0, t0, t2); /* t0 = ..ba..fe */
  1653. tcg_gen_shli_i64(t1, t1, 16); /* t1 = dc..hg.. */
  1654. tcg_gen_or_i64(ret, t0, t1); /* ret = dcbahgfe */
  1655. tcg_gen_shri_i64(t0, ret, 32); /* t0 = ....dcba */
  1656. tcg_gen_shli_i64(t1, ret, 32); /* t1 = hgfe.... */
  1657. tcg_gen_or_i64(ret, t0, t1); /* ret = hgfedcba */
  1658. tcg_temp_free_i64(t0);
  1659. tcg_temp_free_i64(t1);
  1660. tcg_temp_free_i64(t2);
  1661. }
  1662. }
  1663. void tcg_gen_hswap_i64(TCGv_i64 ret, TCGv_i64 arg)
  1664. {
  1665. uint64_t m = 0x0000ffff0000ffffull;
  1666. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1667. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  1668. /* See include/qemu/bitops.h, hswap64. */
  1669. tcg_gen_rotli_i64(t1, arg, 32);
  1670. tcg_gen_andi_i64(t0, t1, m);
  1671. tcg_gen_shli_i64(t0, t0, 16);
  1672. tcg_gen_shri_i64(t1, t1, 16);
  1673. tcg_gen_andi_i64(t1, t1, m);
  1674. tcg_gen_or_i64(ret, t0, t1);
  1675. tcg_temp_free_i64(t0);
  1676. tcg_temp_free_i64(t1);
  1677. }
  1678. void tcg_gen_wswap_i64(TCGv_i64 ret, TCGv_i64 arg)
  1679. {
  1680. /* Swapping 2 32-bit elements is a rotate. */
  1681. tcg_gen_rotli_i64(ret, arg, 32);
  1682. }
  1683. void tcg_gen_not_i64(TCGv_i64 ret, TCGv_i64 arg)
  1684. {
  1685. if (TCG_TARGET_REG_BITS == 32) {
  1686. tcg_gen_not_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  1687. tcg_gen_not_i32(TCGV_HIGH(ret), TCGV_HIGH(arg));
  1688. } else if (TCG_TARGET_HAS_not_i64) {
  1689. tcg_gen_op2_i64(INDEX_op_not_i64, ret, arg);
  1690. } else {
  1691. tcg_gen_xori_i64(ret, arg, -1);
  1692. }
  1693. }
  1694. void tcg_gen_andc_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1695. {
  1696. if (TCG_TARGET_REG_BITS == 32) {
  1697. tcg_gen_andc_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1698. tcg_gen_andc_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1699. } else if (TCG_TARGET_HAS_andc_i64) {
  1700. tcg_gen_op3_i64(INDEX_op_andc_i64, ret, arg1, arg2);
  1701. } else {
  1702. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1703. tcg_gen_not_i64(t0, arg2);
  1704. tcg_gen_and_i64(ret, arg1, t0);
  1705. tcg_temp_free_i64(t0);
  1706. }
  1707. }
  1708. void tcg_gen_eqv_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1709. {
  1710. if (TCG_TARGET_REG_BITS == 32) {
  1711. tcg_gen_eqv_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1712. tcg_gen_eqv_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1713. } else if (TCG_TARGET_HAS_eqv_i64) {
  1714. tcg_gen_op3_i64(INDEX_op_eqv_i64, ret, arg1, arg2);
  1715. } else {
  1716. tcg_gen_xor_i64(ret, arg1, arg2);
  1717. tcg_gen_not_i64(ret, ret);
  1718. }
  1719. }
  1720. void tcg_gen_nand_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1721. {
  1722. if (TCG_TARGET_REG_BITS == 32) {
  1723. tcg_gen_nand_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1724. tcg_gen_nand_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1725. } else if (TCG_TARGET_HAS_nand_i64) {
  1726. tcg_gen_op3_i64(INDEX_op_nand_i64, ret, arg1, arg2);
  1727. } else {
  1728. tcg_gen_and_i64(ret, arg1, arg2);
  1729. tcg_gen_not_i64(ret, ret);
  1730. }
  1731. }
  1732. void tcg_gen_nor_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1733. {
  1734. if (TCG_TARGET_REG_BITS == 32) {
  1735. tcg_gen_nor_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1736. tcg_gen_nor_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1737. } else if (TCG_TARGET_HAS_nor_i64) {
  1738. tcg_gen_op3_i64(INDEX_op_nor_i64, ret, arg1, arg2);
  1739. } else {
  1740. tcg_gen_or_i64(ret, arg1, arg2);
  1741. tcg_gen_not_i64(ret, ret);
  1742. }
  1743. }
  1744. void tcg_gen_orc_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1745. {
  1746. if (TCG_TARGET_REG_BITS == 32) {
  1747. tcg_gen_orc_i32(TCGV_LOW(ret), TCGV_LOW(arg1), TCGV_LOW(arg2));
  1748. tcg_gen_orc_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), TCGV_HIGH(arg2));
  1749. } else if (TCG_TARGET_HAS_orc_i64) {
  1750. tcg_gen_op3_i64(INDEX_op_orc_i64, ret, arg1, arg2);
  1751. } else {
  1752. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  1753. tcg_gen_not_i64(t0, arg2);
  1754. tcg_gen_or_i64(ret, arg1, t0);
  1755. tcg_temp_free_i64(t0);
  1756. }
  1757. }
  1758. void tcg_gen_clz_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1759. {
  1760. if (TCG_TARGET_HAS_clz_i64) {
  1761. tcg_gen_op3_i64(INDEX_op_clz_i64, ret, arg1, arg2);
  1762. } else {
  1763. gen_helper_clz_i64(ret, arg1, arg2);
  1764. }
  1765. }
  1766. void tcg_gen_clzi_i64(TCGv_i64 ret, TCGv_i64 arg1, uint64_t arg2)
  1767. {
  1768. if (TCG_TARGET_REG_BITS == 32
  1769. && TCG_TARGET_HAS_clz_i32
  1770. && arg2 <= 0xffffffffu) {
  1771. TCGv_i32 t = tcg_temp_ebb_new_i32();
  1772. tcg_gen_clzi_i32(t, TCGV_LOW(arg1), arg2 - 32);
  1773. tcg_gen_addi_i32(t, t, 32);
  1774. tcg_gen_clz_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), t);
  1775. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1776. tcg_temp_free_i32(t);
  1777. } else {
  1778. tcg_gen_clz_i64(ret, arg1, tcg_constant_i64(arg2));
  1779. }
  1780. }
  1781. void tcg_gen_ctz_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1782. {
  1783. if (TCG_TARGET_HAS_ctz_i64) {
  1784. tcg_gen_op3_i64(INDEX_op_ctz_i64, ret, arg1, arg2);
  1785. } else if (TCG_TARGET_HAS_ctpop_i64 || TCG_TARGET_HAS_clz_i64) {
  1786. TCGv_i64 z, t = tcg_temp_ebb_new_i64();
  1787. if (TCG_TARGET_HAS_ctpop_i64) {
  1788. tcg_gen_subi_i64(t, arg1, 1);
  1789. tcg_gen_andc_i64(t, t, arg1);
  1790. tcg_gen_ctpop_i64(t, t);
  1791. } else {
  1792. /* Since all non-x86 hosts have clz(0) == 64, don't fight it. */
  1793. tcg_gen_neg_i64(t, arg1);
  1794. tcg_gen_and_i64(t, t, arg1);
  1795. tcg_gen_clzi_i64(t, t, 64);
  1796. tcg_gen_xori_i64(t, t, 63);
  1797. }
  1798. z = tcg_constant_i64(0);
  1799. tcg_gen_movcond_i64(TCG_COND_EQ, ret, arg1, z, arg2, t);
  1800. tcg_temp_free_i64(t);
  1801. tcg_temp_free_i64(z);
  1802. } else {
  1803. gen_helper_ctz_i64(ret, arg1, arg2);
  1804. }
  1805. }
  1806. void tcg_gen_ctzi_i64(TCGv_i64 ret, TCGv_i64 arg1, uint64_t arg2)
  1807. {
  1808. if (TCG_TARGET_REG_BITS == 32
  1809. && TCG_TARGET_HAS_ctz_i32
  1810. && arg2 <= 0xffffffffu) {
  1811. TCGv_i32 t32 = tcg_temp_ebb_new_i32();
  1812. tcg_gen_ctzi_i32(t32, TCGV_HIGH(arg1), arg2 - 32);
  1813. tcg_gen_addi_i32(t32, t32, 32);
  1814. tcg_gen_ctz_i32(TCGV_LOW(ret), TCGV_LOW(arg1), t32);
  1815. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1816. tcg_temp_free_i32(t32);
  1817. } else if (!TCG_TARGET_HAS_ctz_i64
  1818. && TCG_TARGET_HAS_ctpop_i64
  1819. && arg2 == 64) {
  1820. /* This equivalence has the advantage of not requiring a fixup. */
  1821. TCGv_i64 t = tcg_temp_ebb_new_i64();
  1822. tcg_gen_subi_i64(t, arg1, 1);
  1823. tcg_gen_andc_i64(t, t, arg1);
  1824. tcg_gen_ctpop_i64(ret, t);
  1825. tcg_temp_free_i64(t);
  1826. } else {
  1827. tcg_gen_ctz_i64(ret, arg1, tcg_constant_i64(arg2));
  1828. }
  1829. }
  1830. void tcg_gen_clrsb_i64(TCGv_i64 ret, TCGv_i64 arg)
  1831. {
  1832. if (TCG_TARGET_HAS_clz_i64 || TCG_TARGET_HAS_clz_i32) {
  1833. TCGv_i64 t = tcg_temp_ebb_new_i64();
  1834. tcg_gen_sari_i64(t, arg, 63);
  1835. tcg_gen_xor_i64(t, t, arg);
  1836. tcg_gen_clzi_i64(t, t, 64);
  1837. tcg_gen_subi_i64(ret, t, 1);
  1838. tcg_temp_free_i64(t);
  1839. } else {
  1840. gen_helper_clrsb_i64(ret, arg);
  1841. }
  1842. }
  1843. void tcg_gen_ctpop_i64(TCGv_i64 ret, TCGv_i64 arg1)
  1844. {
  1845. if (TCG_TARGET_HAS_ctpop_i64) {
  1846. tcg_gen_op2_i64(INDEX_op_ctpop_i64, ret, arg1);
  1847. } else if (TCG_TARGET_REG_BITS == 32 && TCG_TARGET_HAS_ctpop_i32) {
  1848. tcg_gen_ctpop_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
  1849. tcg_gen_ctpop_i32(TCGV_LOW(ret), TCGV_LOW(arg1));
  1850. tcg_gen_add_i32(TCGV_LOW(ret), TCGV_LOW(ret), TCGV_HIGH(ret));
  1851. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1852. } else {
  1853. gen_helper_ctpop_i64(ret, arg1);
  1854. }
  1855. }
  1856. void tcg_gen_rotl_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1857. {
  1858. if (TCG_TARGET_HAS_rot_i64) {
  1859. tcg_gen_op3_i64(INDEX_op_rotl_i64, ret, arg1, arg2);
  1860. } else {
  1861. TCGv_i64 t0, t1;
  1862. t0 = tcg_temp_ebb_new_i64();
  1863. t1 = tcg_temp_ebb_new_i64();
  1864. tcg_gen_shl_i64(t0, arg1, arg2);
  1865. tcg_gen_subfi_i64(t1, 64, arg2);
  1866. tcg_gen_shr_i64(t1, arg1, t1);
  1867. tcg_gen_or_i64(ret, t0, t1);
  1868. tcg_temp_free_i64(t0);
  1869. tcg_temp_free_i64(t1);
  1870. }
  1871. }
  1872. void tcg_gen_rotli_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1873. {
  1874. tcg_debug_assert(arg2 >= 0 && arg2 < 64);
  1875. /* some cases can be optimized here */
  1876. if (arg2 == 0) {
  1877. tcg_gen_mov_i64(ret, arg1);
  1878. } else if (TCG_TARGET_HAS_rot_i64) {
  1879. tcg_gen_rotl_i64(ret, arg1, tcg_constant_i64(arg2));
  1880. } else {
  1881. TCGv_i64 t0, t1;
  1882. t0 = tcg_temp_ebb_new_i64();
  1883. t1 = tcg_temp_ebb_new_i64();
  1884. tcg_gen_shli_i64(t0, arg1, arg2);
  1885. tcg_gen_shri_i64(t1, arg1, 64 - arg2);
  1886. tcg_gen_or_i64(ret, t0, t1);
  1887. tcg_temp_free_i64(t0);
  1888. tcg_temp_free_i64(t1);
  1889. }
  1890. }
  1891. void tcg_gen_rotr_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2)
  1892. {
  1893. if (TCG_TARGET_HAS_rot_i64) {
  1894. tcg_gen_op3_i64(INDEX_op_rotr_i64, ret, arg1, arg2);
  1895. } else {
  1896. TCGv_i64 t0, t1;
  1897. t0 = tcg_temp_ebb_new_i64();
  1898. t1 = tcg_temp_ebb_new_i64();
  1899. tcg_gen_shr_i64(t0, arg1, arg2);
  1900. tcg_gen_subfi_i64(t1, 64, arg2);
  1901. tcg_gen_shl_i64(t1, arg1, t1);
  1902. tcg_gen_or_i64(ret, t0, t1);
  1903. tcg_temp_free_i64(t0);
  1904. tcg_temp_free_i64(t1);
  1905. }
  1906. }
  1907. void tcg_gen_rotri_i64(TCGv_i64 ret, TCGv_i64 arg1, int64_t arg2)
  1908. {
  1909. tcg_debug_assert(arg2 >= 0 && arg2 < 64);
  1910. /* some cases can be optimized here */
  1911. if (arg2 == 0) {
  1912. tcg_gen_mov_i64(ret, arg1);
  1913. } else {
  1914. tcg_gen_rotli_i64(ret, arg1, 64 - arg2);
  1915. }
  1916. }
  1917. void tcg_gen_deposit_i64(TCGv_i64 ret, TCGv_i64 arg1, TCGv_i64 arg2,
  1918. unsigned int ofs, unsigned int len)
  1919. {
  1920. uint64_t mask;
  1921. TCGv_i64 t1;
  1922. tcg_debug_assert(ofs < 64);
  1923. tcg_debug_assert(len > 0);
  1924. tcg_debug_assert(len <= 64);
  1925. tcg_debug_assert(ofs + len <= 64);
  1926. if (len == 64) {
  1927. tcg_gen_mov_i64(ret, arg2);
  1928. return;
  1929. }
  1930. if (TCG_TARGET_HAS_deposit_i64 && TCG_TARGET_deposit_i64_valid(ofs, len)) {
  1931. tcg_gen_op5ii_i64(INDEX_op_deposit_i64, ret, arg1, arg2, ofs, len);
  1932. return;
  1933. }
  1934. if (TCG_TARGET_REG_BITS == 32) {
  1935. if (ofs >= 32) {
  1936. tcg_gen_deposit_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1),
  1937. TCGV_LOW(arg2), ofs - 32, len);
  1938. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1));
  1939. return;
  1940. }
  1941. if (ofs + len <= 32) {
  1942. tcg_gen_deposit_i32(TCGV_LOW(ret), TCGV_LOW(arg1),
  1943. TCGV_LOW(arg2), ofs, len);
  1944. tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
  1945. return;
  1946. }
  1947. }
  1948. t1 = tcg_temp_ebb_new_i64();
  1949. if (TCG_TARGET_HAS_extract2_i64) {
  1950. if (ofs + len == 64) {
  1951. tcg_gen_shli_i64(t1, arg1, len);
  1952. tcg_gen_extract2_i64(ret, t1, arg2, len);
  1953. goto done;
  1954. }
  1955. if (ofs == 0) {
  1956. tcg_gen_extract2_i64(ret, arg1, arg2, len);
  1957. tcg_gen_rotli_i64(ret, ret, len);
  1958. goto done;
  1959. }
  1960. }
  1961. mask = (1ull << len) - 1;
  1962. if (ofs + len < 64) {
  1963. tcg_gen_andi_i64(t1, arg2, mask);
  1964. tcg_gen_shli_i64(t1, t1, ofs);
  1965. } else {
  1966. tcg_gen_shli_i64(t1, arg2, ofs);
  1967. }
  1968. tcg_gen_andi_i64(ret, arg1, ~(mask << ofs));
  1969. tcg_gen_or_i64(ret, ret, t1);
  1970. done:
  1971. tcg_temp_free_i64(t1);
  1972. }
  1973. void tcg_gen_deposit_z_i64(TCGv_i64 ret, TCGv_i64 arg,
  1974. unsigned int ofs, unsigned int len)
  1975. {
  1976. tcg_debug_assert(ofs < 64);
  1977. tcg_debug_assert(len > 0);
  1978. tcg_debug_assert(len <= 64);
  1979. tcg_debug_assert(ofs + len <= 64);
  1980. if (ofs + len == 64) {
  1981. tcg_gen_shli_i64(ret, arg, ofs);
  1982. } else if (ofs == 0) {
  1983. tcg_gen_andi_i64(ret, arg, (1ull << len) - 1);
  1984. } else if (TCG_TARGET_HAS_deposit_i64
  1985. && TCG_TARGET_deposit_i64_valid(ofs, len)) {
  1986. TCGv_i64 zero = tcg_constant_i64(0);
  1987. tcg_gen_op5ii_i64(INDEX_op_deposit_i64, ret, zero, arg, ofs, len);
  1988. } else {
  1989. if (TCG_TARGET_REG_BITS == 32) {
  1990. if (ofs >= 32) {
  1991. tcg_gen_deposit_z_i32(TCGV_HIGH(ret), TCGV_LOW(arg),
  1992. ofs - 32, len);
  1993. tcg_gen_movi_i32(TCGV_LOW(ret), 0);
  1994. return;
  1995. }
  1996. if (ofs + len <= 32) {
  1997. tcg_gen_deposit_z_i32(TCGV_LOW(ret), TCGV_LOW(arg), ofs, len);
  1998. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  1999. return;
  2000. }
  2001. }
  2002. /* To help two-operand hosts we prefer to zero-extend first,
  2003. which allows ARG to stay live. */
  2004. switch (len) {
  2005. case 32:
  2006. if (TCG_TARGET_HAS_ext32u_i64) {
  2007. tcg_gen_ext32u_i64(ret, arg);
  2008. tcg_gen_shli_i64(ret, ret, ofs);
  2009. return;
  2010. }
  2011. break;
  2012. case 16:
  2013. if (TCG_TARGET_HAS_ext16u_i64) {
  2014. tcg_gen_ext16u_i64(ret, arg);
  2015. tcg_gen_shli_i64(ret, ret, ofs);
  2016. return;
  2017. }
  2018. break;
  2019. case 8:
  2020. if (TCG_TARGET_HAS_ext8u_i64) {
  2021. tcg_gen_ext8u_i64(ret, arg);
  2022. tcg_gen_shli_i64(ret, ret, ofs);
  2023. return;
  2024. }
  2025. break;
  2026. }
  2027. /* Otherwise prefer zero-extension over AND for code size. */
  2028. switch (ofs + len) {
  2029. case 32:
  2030. if (TCG_TARGET_HAS_ext32u_i64) {
  2031. tcg_gen_shli_i64(ret, arg, ofs);
  2032. tcg_gen_ext32u_i64(ret, ret);
  2033. return;
  2034. }
  2035. break;
  2036. case 16:
  2037. if (TCG_TARGET_HAS_ext16u_i64) {
  2038. tcg_gen_shli_i64(ret, arg, ofs);
  2039. tcg_gen_ext16u_i64(ret, ret);
  2040. return;
  2041. }
  2042. break;
  2043. case 8:
  2044. if (TCG_TARGET_HAS_ext8u_i64) {
  2045. tcg_gen_shli_i64(ret, arg, ofs);
  2046. tcg_gen_ext8u_i64(ret, ret);
  2047. return;
  2048. }
  2049. break;
  2050. }
  2051. tcg_gen_andi_i64(ret, arg, (1ull << len) - 1);
  2052. tcg_gen_shli_i64(ret, ret, ofs);
  2053. }
  2054. }
  2055. void tcg_gen_extract_i64(TCGv_i64 ret, TCGv_i64 arg,
  2056. unsigned int ofs, unsigned int len)
  2057. {
  2058. tcg_debug_assert(ofs < 64);
  2059. tcg_debug_assert(len > 0);
  2060. tcg_debug_assert(len <= 64);
  2061. tcg_debug_assert(ofs + len <= 64);
  2062. /* Canonicalize certain special cases, even if extract is supported. */
  2063. if (ofs + len == 64) {
  2064. tcg_gen_shri_i64(ret, arg, 64 - len);
  2065. return;
  2066. }
  2067. if (ofs == 0) {
  2068. tcg_gen_andi_i64(ret, arg, (1ull << len) - 1);
  2069. return;
  2070. }
  2071. if (TCG_TARGET_REG_BITS == 32) {
  2072. /* Look for a 32-bit extract within one of the two words. */
  2073. if (ofs >= 32) {
  2074. tcg_gen_extract_i32(TCGV_LOW(ret), TCGV_HIGH(arg), ofs - 32, len);
  2075. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  2076. return;
  2077. }
  2078. if (ofs + len <= 32) {
  2079. tcg_gen_extract_i32(TCGV_LOW(ret), TCGV_LOW(arg), ofs, len);
  2080. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  2081. return;
  2082. }
  2083. /* The field is split across two words. One double-word
  2084. shift is better than two double-word shifts. */
  2085. goto do_shift_and;
  2086. }
  2087. if (TCG_TARGET_HAS_extract_i64
  2088. && TCG_TARGET_extract_i64_valid(ofs, len)) {
  2089. tcg_gen_op4ii_i64(INDEX_op_extract_i64, ret, arg, ofs, len);
  2090. return;
  2091. }
  2092. /* Assume that zero-extension, if available, is cheaper than a shift. */
  2093. switch (ofs + len) {
  2094. case 32:
  2095. if (TCG_TARGET_HAS_ext32u_i64) {
  2096. tcg_gen_ext32u_i64(ret, arg);
  2097. tcg_gen_shri_i64(ret, ret, ofs);
  2098. return;
  2099. }
  2100. break;
  2101. case 16:
  2102. if (TCG_TARGET_HAS_ext16u_i64) {
  2103. tcg_gen_ext16u_i64(ret, arg);
  2104. tcg_gen_shri_i64(ret, ret, ofs);
  2105. return;
  2106. }
  2107. break;
  2108. case 8:
  2109. if (TCG_TARGET_HAS_ext8u_i64) {
  2110. tcg_gen_ext8u_i64(ret, arg);
  2111. tcg_gen_shri_i64(ret, ret, ofs);
  2112. return;
  2113. }
  2114. break;
  2115. }
  2116. /* ??? Ideally we'd know what values are available for immediate AND.
  2117. Assume that 8 bits are available, plus the special cases of 16 and 32,
  2118. so that we get ext8u, ext16u, and ext32u. */
  2119. switch (len) {
  2120. case 1 ... 8: case 16: case 32:
  2121. do_shift_and:
  2122. tcg_gen_shri_i64(ret, arg, ofs);
  2123. tcg_gen_andi_i64(ret, ret, (1ull << len) - 1);
  2124. break;
  2125. default:
  2126. tcg_gen_shli_i64(ret, arg, 64 - len - ofs);
  2127. tcg_gen_shri_i64(ret, ret, 64 - len);
  2128. break;
  2129. }
  2130. }
  2131. void tcg_gen_sextract_i64(TCGv_i64 ret, TCGv_i64 arg,
  2132. unsigned int ofs, unsigned int len)
  2133. {
  2134. tcg_debug_assert(ofs < 64);
  2135. tcg_debug_assert(len > 0);
  2136. tcg_debug_assert(len <= 64);
  2137. tcg_debug_assert(ofs + len <= 64);
  2138. /* Canonicalize certain special cases, even if sextract is supported. */
  2139. if (ofs + len == 64) {
  2140. tcg_gen_sari_i64(ret, arg, 64 - len);
  2141. return;
  2142. }
  2143. if (ofs == 0) {
  2144. switch (len) {
  2145. case 32:
  2146. tcg_gen_ext32s_i64(ret, arg);
  2147. return;
  2148. case 16:
  2149. tcg_gen_ext16s_i64(ret, arg);
  2150. return;
  2151. case 8:
  2152. tcg_gen_ext8s_i64(ret, arg);
  2153. return;
  2154. }
  2155. }
  2156. if (TCG_TARGET_REG_BITS == 32) {
  2157. /* Look for a 32-bit extract within one of the two words. */
  2158. if (ofs >= 32) {
  2159. tcg_gen_sextract_i32(TCGV_LOW(ret), TCGV_HIGH(arg), ofs - 32, len);
  2160. } else if (ofs + len <= 32) {
  2161. tcg_gen_sextract_i32(TCGV_LOW(ret), TCGV_LOW(arg), ofs, len);
  2162. } else if (ofs == 0) {
  2163. tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg));
  2164. tcg_gen_sextract_i32(TCGV_HIGH(ret), TCGV_HIGH(arg), 0, len - 32);
  2165. return;
  2166. } else if (len > 32) {
  2167. TCGv_i32 t = tcg_temp_ebb_new_i32();
  2168. /* Extract the bits for the high word normally. */
  2169. tcg_gen_sextract_i32(t, TCGV_HIGH(arg), ofs + 32, len - 32);
  2170. /* Shift the field down for the low part. */
  2171. tcg_gen_shri_i64(ret, arg, ofs);
  2172. /* Overwrite the shift into the high part. */
  2173. tcg_gen_mov_i32(TCGV_HIGH(ret), t);
  2174. tcg_temp_free_i32(t);
  2175. return;
  2176. } else {
  2177. /* Shift the field down for the low part, such that the
  2178. field sits at the MSB. */
  2179. tcg_gen_shri_i64(ret, arg, ofs + len - 32);
  2180. /* Shift the field down from the MSB, sign extending. */
  2181. tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_LOW(ret), 32 - len);
  2182. }
  2183. /* Sign-extend the field from 32 bits. */
  2184. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  2185. return;
  2186. }
  2187. if (TCG_TARGET_HAS_sextract_i64
  2188. && TCG_TARGET_extract_i64_valid(ofs, len)) {
  2189. tcg_gen_op4ii_i64(INDEX_op_sextract_i64, ret, arg, ofs, len);
  2190. return;
  2191. }
  2192. /* Assume that sign-extension, if available, is cheaper than a shift. */
  2193. switch (ofs + len) {
  2194. case 32:
  2195. if (TCG_TARGET_HAS_ext32s_i64) {
  2196. tcg_gen_ext32s_i64(ret, arg);
  2197. tcg_gen_sari_i64(ret, ret, ofs);
  2198. return;
  2199. }
  2200. break;
  2201. case 16:
  2202. if (TCG_TARGET_HAS_ext16s_i64) {
  2203. tcg_gen_ext16s_i64(ret, arg);
  2204. tcg_gen_sari_i64(ret, ret, ofs);
  2205. return;
  2206. }
  2207. break;
  2208. case 8:
  2209. if (TCG_TARGET_HAS_ext8s_i64) {
  2210. tcg_gen_ext8s_i64(ret, arg);
  2211. tcg_gen_sari_i64(ret, ret, ofs);
  2212. return;
  2213. }
  2214. break;
  2215. }
  2216. switch (len) {
  2217. case 32:
  2218. if (TCG_TARGET_HAS_ext32s_i64) {
  2219. tcg_gen_shri_i64(ret, arg, ofs);
  2220. tcg_gen_ext32s_i64(ret, ret);
  2221. return;
  2222. }
  2223. break;
  2224. case 16:
  2225. if (TCG_TARGET_HAS_ext16s_i64) {
  2226. tcg_gen_shri_i64(ret, arg, ofs);
  2227. tcg_gen_ext16s_i64(ret, ret);
  2228. return;
  2229. }
  2230. break;
  2231. case 8:
  2232. if (TCG_TARGET_HAS_ext8s_i64) {
  2233. tcg_gen_shri_i64(ret, arg, ofs);
  2234. tcg_gen_ext8s_i64(ret, ret);
  2235. return;
  2236. }
  2237. break;
  2238. }
  2239. tcg_gen_shli_i64(ret, arg, 64 - len - ofs);
  2240. tcg_gen_sari_i64(ret, ret, 64 - len);
  2241. }
  2242. /*
  2243. * Extract 64 bits from a 128-bit input, ah:al, starting from ofs.
  2244. * Unlike tcg_gen_extract_i64 above, len is fixed at 64.
  2245. */
  2246. void tcg_gen_extract2_i64(TCGv_i64 ret, TCGv_i64 al, TCGv_i64 ah,
  2247. unsigned int ofs)
  2248. {
  2249. tcg_debug_assert(ofs <= 64);
  2250. if (ofs == 0) {
  2251. tcg_gen_mov_i64(ret, al);
  2252. } else if (ofs == 64) {
  2253. tcg_gen_mov_i64(ret, ah);
  2254. } else if (al == ah) {
  2255. tcg_gen_rotri_i64(ret, al, ofs);
  2256. } else if (TCG_TARGET_HAS_extract2_i64) {
  2257. tcg_gen_op4i_i64(INDEX_op_extract2_i64, ret, al, ah, ofs);
  2258. } else {
  2259. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2260. tcg_gen_shri_i64(t0, al, ofs);
  2261. tcg_gen_deposit_i64(ret, t0, ah, 64 - ofs, ofs);
  2262. tcg_temp_free_i64(t0);
  2263. }
  2264. }
  2265. void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, TCGv_i64 c1,
  2266. TCGv_i64 c2, TCGv_i64 v1, TCGv_i64 v2)
  2267. {
  2268. if (cond == TCG_COND_ALWAYS) {
  2269. tcg_gen_mov_i64(ret, v1);
  2270. } else if (cond == TCG_COND_NEVER) {
  2271. tcg_gen_mov_i64(ret, v2);
  2272. } else if (TCG_TARGET_REG_BITS == 32) {
  2273. TCGv_i32 t0 = tcg_temp_ebb_new_i32();
  2274. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  2275. tcg_gen_op6i_i32(INDEX_op_setcond2_i32, t0,
  2276. TCGV_LOW(c1), TCGV_HIGH(c1),
  2277. TCGV_LOW(c2), TCGV_HIGH(c2), cond);
  2278. if (TCG_TARGET_HAS_movcond_i32) {
  2279. tcg_gen_movi_i32(t1, 0);
  2280. tcg_gen_movcond_i32(TCG_COND_NE, TCGV_LOW(ret), t0, t1,
  2281. TCGV_LOW(v1), TCGV_LOW(v2));
  2282. tcg_gen_movcond_i32(TCG_COND_NE, TCGV_HIGH(ret), t0, t1,
  2283. TCGV_HIGH(v1), TCGV_HIGH(v2));
  2284. } else {
  2285. tcg_gen_neg_i32(t0, t0);
  2286. tcg_gen_and_i32(t1, TCGV_LOW(v1), t0);
  2287. tcg_gen_andc_i32(TCGV_LOW(ret), TCGV_LOW(v2), t0);
  2288. tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(ret), t1);
  2289. tcg_gen_and_i32(t1, TCGV_HIGH(v1), t0);
  2290. tcg_gen_andc_i32(TCGV_HIGH(ret), TCGV_HIGH(v2), t0);
  2291. tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t1);
  2292. }
  2293. tcg_temp_free_i32(t0);
  2294. tcg_temp_free_i32(t1);
  2295. } else if (TCG_TARGET_HAS_movcond_i64) {
  2296. tcg_gen_op6i_i64(INDEX_op_movcond_i64, ret, c1, c2, v1, v2, cond);
  2297. } else {
  2298. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2299. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  2300. tcg_gen_setcond_i64(cond, t0, c1, c2);
  2301. tcg_gen_neg_i64(t0, t0);
  2302. tcg_gen_and_i64(t1, v1, t0);
  2303. tcg_gen_andc_i64(ret, v2, t0);
  2304. tcg_gen_or_i64(ret, ret, t1);
  2305. tcg_temp_free_i64(t0);
  2306. tcg_temp_free_i64(t1);
  2307. }
  2308. }
  2309. void tcg_gen_add2_i64(TCGv_i64 rl, TCGv_i64 rh, TCGv_i64 al,
  2310. TCGv_i64 ah, TCGv_i64 bl, TCGv_i64 bh)
  2311. {
  2312. if (TCG_TARGET_HAS_add2_i64) {
  2313. tcg_gen_op6_i64(INDEX_op_add2_i64, rl, rh, al, ah, bl, bh);
  2314. } else {
  2315. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2316. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  2317. tcg_gen_add_i64(t0, al, bl);
  2318. tcg_gen_setcond_i64(TCG_COND_LTU, t1, t0, al);
  2319. tcg_gen_add_i64(rh, ah, bh);
  2320. tcg_gen_add_i64(rh, rh, t1);
  2321. tcg_gen_mov_i64(rl, t0);
  2322. tcg_temp_free_i64(t0);
  2323. tcg_temp_free_i64(t1);
  2324. }
  2325. }
  2326. void tcg_gen_sub2_i64(TCGv_i64 rl, TCGv_i64 rh, TCGv_i64 al,
  2327. TCGv_i64 ah, TCGv_i64 bl, TCGv_i64 bh)
  2328. {
  2329. if (TCG_TARGET_HAS_sub2_i64) {
  2330. tcg_gen_op6_i64(INDEX_op_sub2_i64, rl, rh, al, ah, bl, bh);
  2331. } else {
  2332. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2333. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  2334. tcg_gen_sub_i64(t0, al, bl);
  2335. tcg_gen_setcond_i64(TCG_COND_LTU, t1, al, bl);
  2336. tcg_gen_sub_i64(rh, ah, bh);
  2337. tcg_gen_sub_i64(rh, rh, t1);
  2338. tcg_gen_mov_i64(rl, t0);
  2339. tcg_temp_free_i64(t0);
  2340. tcg_temp_free_i64(t1);
  2341. }
  2342. }
  2343. void tcg_gen_mulu2_i64(TCGv_i64 rl, TCGv_i64 rh, TCGv_i64 arg1, TCGv_i64 arg2)
  2344. {
  2345. if (TCG_TARGET_HAS_mulu2_i64) {
  2346. tcg_gen_op4_i64(INDEX_op_mulu2_i64, rl, rh, arg1, arg2);
  2347. } else if (TCG_TARGET_HAS_muluh_i64) {
  2348. TCGv_i64 t = tcg_temp_ebb_new_i64();
  2349. tcg_gen_op3_i64(INDEX_op_mul_i64, t, arg1, arg2);
  2350. tcg_gen_op3_i64(INDEX_op_muluh_i64, rh, arg1, arg2);
  2351. tcg_gen_mov_i64(rl, t);
  2352. tcg_temp_free_i64(t);
  2353. } else {
  2354. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2355. tcg_gen_mul_i64(t0, arg1, arg2);
  2356. gen_helper_muluh_i64(rh, arg1, arg2);
  2357. tcg_gen_mov_i64(rl, t0);
  2358. tcg_temp_free_i64(t0);
  2359. }
  2360. }
  2361. void tcg_gen_muls2_i64(TCGv_i64 rl, TCGv_i64 rh, TCGv_i64 arg1, TCGv_i64 arg2)
  2362. {
  2363. if (TCG_TARGET_HAS_muls2_i64) {
  2364. tcg_gen_op4_i64(INDEX_op_muls2_i64, rl, rh, arg1, arg2);
  2365. } else if (TCG_TARGET_HAS_mulsh_i64) {
  2366. TCGv_i64 t = tcg_temp_ebb_new_i64();
  2367. tcg_gen_op3_i64(INDEX_op_mul_i64, t, arg1, arg2);
  2368. tcg_gen_op3_i64(INDEX_op_mulsh_i64, rh, arg1, arg2);
  2369. tcg_gen_mov_i64(rl, t);
  2370. tcg_temp_free_i64(t);
  2371. } else if (TCG_TARGET_HAS_mulu2_i64 || TCG_TARGET_HAS_muluh_i64) {
  2372. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2373. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  2374. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  2375. TCGv_i64 t3 = tcg_temp_ebb_new_i64();
  2376. tcg_gen_mulu2_i64(t0, t1, arg1, arg2);
  2377. /* Adjust for negative inputs. */
  2378. tcg_gen_sari_i64(t2, arg1, 63);
  2379. tcg_gen_sari_i64(t3, arg2, 63);
  2380. tcg_gen_and_i64(t2, t2, arg2);
  2381. tcg_gen_and_i64(t3, t3, arg1);
  2382. tcg_gen_sub_i64(rh, t1, t2);
  2383. tcg_gen_sub_i64(rh, rh, t3);
  2384. tcg_gen_mov_i64(rl, t0);
  2385. tcg_temp_free_i64(t0);
  2386. tcg_temp_free_i64(t1);
  2387. tcg_temp_free_i64(t2);
  2388. tcg_temp_free_i64(t3);
  2389. } else {
  2390. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2391. tcg_gen_mul_i64(t0, arg1, arg2);
  2392. gen_helper_mulsh_i64(rh, arg1, arg2);
  2393. tcg_gen_mov_i64(rl, t0);
  2394. tcg_temp_free_i64(t0);
  2395. }
  2396. }
  2397. void tcg_gen_mulsu2_i64(TCGv_i64 rl, TCGv_i64 rh, TCGv_i64 arg1, TCGv_i64 arg2)
  2398. {
  2399. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  2400. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  2401. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  2402. tcg_gen_mulu2_i64(t0, t1, arg1, arg2);
  2403. /* Adjust for negative input for the signed arg1. */
  2404. tcg_gen_sari_i64(t2, arg1, 63);
  2405. tcg_gen_and_i64(t2, t2, arg2);
  2406. tcg_gen_sub_i64(rh, t1, t2);
  2407. tcg_gen_mov_i64(rl, t0);
  2408. tcg_temp_free_i64(t0);
  2409. tcg_temp_free_i64(t1);
  2410. tcg_temp_free_i64(t2);
  2411. }
  2412. void tcg_gen_smin_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
  2413. {
  2414. tcg_gen_movcond_i64(TCG_COND_LT, ret, a, b, a, b);
  2415. }
  2416. void tcg_gen_umin_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
  2417. {
  2418. tcg_gen_movcond_i64(TCG_COND_LTU, ret, a, b, a, b);
  2419. }
  2420. void tcg_gen_smax_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
  2421. {
  2422. tcg_gen_movcond_i64(TCG_COND_LT, ret, a, b, b, a);
  2423. }
  2424. void tcg_gen_umax_i64(TCGv_i64 ret, TCGv_i64 a, TCGv_i64 b)
  2425. {
  2426. tcg_gen_movcond_i64(TCG_COND_LTU, ret, a, b, b, a);
  2427. }
  2428. void tcg_gen_abs_i64(TCGv_i64 ret, TCGv_i64 a)
  2429. {
  2430. TCGv_i64 t = tcg_temp_ebb_new_i64();
  2431. tcg_gen_sari_i64(t, a, 63);
  2432. tcg_gen_xor_i64(ret, a, t);
  2433. tcg_gen_sub_i64(ret, ret, t);
  2434. tcg_temp_free_i64(t);
  2435. }
  2436. /* Size changing operations. */
  2437. void tcg_gen_extrl_i64_i32(TCGv_i32 ret, TCGv_i64 arg)
  2438. {
  2439. if (TCG_TARGET_REG_BITS == 32) {
  2440. tcg_gen_mov_i32(ret, TCGV_LOW(arg));
  2441. } else if (TCG_TARGET_HAS_extrl_i64_i32) {
  2442. tcg_gen_op2(INDEX_op_extrl_i64_i32,
  2443. tcgv_i32_arg(ret), tcgv_i64_arg(arg));
  2444. } else {
  2445. tcg_gen_mov_i32(ret, (TCGv_i32)arg);
  2446. }
  2447. }
  2448. void tcg_gen_extrh_i64_i32(TCGv_i32 ret, TCGv_i64 arg)
  2449. {
  2450. if (TCG_TARGET_REG_BITS == 32) {
  2451. tcg_gen_mov_i32(ret, TCGV_HIGH(arg));
  2452. } else if (TCG_TARGET_HAS_extrh_i64_i32) {
  2453. tcg_gen_op2(INDEX_op_extrh_i64_i32,
  2454. tcgv_i32_arg(ret), tcgv_i64_arg(arg));
  2455. } else {
  2456. TCGv_i64 t = tcg_temp_ebb_new_i64();
  2457. tcg_gen_shri_i64(t, arg, 32);
  2458. tcg_gen_mov_i32(ret, (TCGv_i32)t);
  2459. tcg_temp_free_i64(t);
  2460. }
  2461. }
  2462. void tcg_gen_extu_i32_i64(TCGv_i64 ret, TCGv_i32 arg)
  2463. {
  2464. if (TCG_TARGET_REG_BITS == 32) {
  2465. tcg_gen_mov_i32(TCGV_LOW(ret), arg);
  2466. tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
  2467. } else {
  2468. tcg_gen_op2(INDEX_op_extu_i32_i64,
  2469. tcgv_i64_arg(ret), tcgv_i32_arg(arg));
  2470. }
  2471. }
  2472. void tcg_gen_ext_i32_i64(TCGv_i64 ret, TCGv_i32 arg)
  2473. {
  2474. if (TCG_TARGET_REG_BITS == 32) {
  2475. tcg_gen_mov_i32(TCGV_LOW(ret), arg);
  2476. tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);
  2477. } else {
  2478. tcg_gen_op2(INDEX_op_ext_i32_i64,
  2479. tcgv_i64_arg(ret), tcgv_i32_arg(arg));
  2480. }
  2481. }
  2482. void tcg_gen_concat_i32_i64(TCGv_i64 dest, TCGv_i32 low, TCGv_i32 high)
  2483. {
  2484. TCGv_i64 tmp;
  2485. if (TCG_TARGET_REG_BITS == 32) {
  2486. tcg_gen_mov_i32(TCGV_LOW(dest), low);
  2487. tcg_gen_mov_i32(TCGV_HIGH(dest), high);
  2488. return;
  2489. }
  2490. tmp = tcg_temp_ebb_new_i64();
  2491. /* These extensions are only needed for type correctness.
  2492. We may be able to do better given target specific information. */
  2493. tcg_gen_extu_i32_i64(tmp, high);
  2494. tcg_gen_extu_i32_i64(dest, low);
  2495. /* If deposit is available, use it. Otherwise use the extra
  2496. knowledge that we have of the zero-extensions above. */
  2497. if (TCG_TARGET_HAS_deposit_i64 && TCG_TARGET_deposit_i64_valid(32, 32)) {
  2498. tcg_gen_deposit_i64(dest, dest, tmp, 32, 32);
  2499. } else {
  2500. tcg_gen_shli_i64(tmp, tmp, 32);
  2501. tcg_gen_or_i64(dest, dest, tmp);
  2502. }
  2503. tcg_temp_free_i64(tmp);
  2504. }
  2505. void tcg_gen_extr_i64_i32(TCGv_i32 lo, TCGv_i32 hi, TCGv_i64 arg)
  2506. {
  2507. if (TCG_TARGET_REG_BITS == 32) {
  2508. tcg_gen_mov_i32(lo, TCGV_LOW(arg));
  2509. tcg_gen_mov_i32(hi, TCGV_HIGH(arg));
  2510. } else {
  2511. tcg_gen_extrl_i64_i32(lo, arg);
  2512. tcg_gen_extrh_i64_i32(hi, arg);
  2513. }
  2514. }
  2515. void tcg_gen_extr32_i64(TCGv_i64 lo, TCGv_i64 hi, TCGv_i64 arg)
  2516. {
  2517. tcg_gen_ext32u_i64(lo, arg);
  2518. tcg_gen_shri_i64(hi, arg, 32);
  2519. }
  2520. void tcg_gen_extr_i128_i64(TCGv_i64 lo, TCGv_i64 hi, TCGv_i128 arg)
  2521. {
  2522. tcg_gen_mov_i64(lo, TCGV128_LOW(arg));
  2523. tcg_gen_mov_i64(hi, TCGV128_HIGH(arg));
  2524. }
  2525. void tcg_gen_concat_i64_i128(TCGv_i128 ret, TCGv_i64 lo, TCGv_i64 hi)
  2526. {
  2527. tcg_gen_mov_i64(TCGV128_LOW(ret), lo);
  2528. tcg_gen_mov_i64(TCGV128_HIGH(ret), hi);
  2529. }
  2530. void tcg_gen_mov_i128(TCGv_i128 dst, TCGv_i128 src)
  2531. {
  2532. if (dst != src) {
  2533. tcg_gen_mov_i64(TCGV128_LOW(dst), TCGV128_LOW(src));
  2534. tcg_gen_mov_i64(TCGV128_HIGH(dst), TCGV128_HIGH(src));
  2535. }
  2536. }
  2537. /* QEMU specific operations. */
  2538. void tcg_gen_exit_tb(const TranslationBlock *tb, unsigned idx)
  2539. {
  2540. /*
  2541. * Let the jit code return the read-only version of the
  2542. * TranslationBlock, so that we minimize the pc-relative
  2543. * distance of the address of the exit_tb code to TB.
  2544. * This will improve utilization of pc-relative address loads.
  2545. *
  2546. * TODO: Move this to translator_loop, so that all const
  2547. * TranslationBlock pointers refer to read-only memory.
  2548. * This requires coordination with targets that do not use
  2549. * the translator_loop.
  2550. */
  2551. uintptr_t val = (uintptr_t)tcg_splitwx_to_rx((void *)tb) + idx;
  2552. if (tb == NULL) {
  2553. tcg_debug_assert(idx == 0);
  2554. } else if (idx <= TB_EXIT_IDXMAX) {
  2555. #ifdef CONFIG_DEBUG_TCG
  2556. /* This is an exit following a goto_tb. Verify that we have
  2557. seen this numbered exit before, via tcg_gen_goto_tb. */
  2558. tcg_debug_assert(tcg_ctx->goto_tb_issue_mask & (1 << idx));
  2559. #endif
  2560. } else {
  2561. /* This is an exit via the exitreq label. */
  2562. tcg_debug_assert(idx == TB_EXIT_REQUESTED);
  2563. }
  2564. tcg_gen_op1i(INDEX_op_exit_tb, val);
  2565. }
  2566. void tcg_gen_goto_tb(unsigned idx)
  2567. {
  2568. /* We tested CF_NO_GOTO_TB in translator_use_goto_tb. */
  2569. tcg_debug_assert(!(tcg_ctx->gen_tb->cflags & CF_NO_GOTO_TB));
  2570. /* We only support two chained exits. */
  2571. tcg_debug_assert(idx <= TB_EXIT_IDXMAX);
  2572. #ifdef CONFIG_DEBUG_TCG
  2573. /* Verify that we haven't seen this numbered exit before. */
  2574. tcg_debug_assert((tcg_ctx->goto_tb_issue_mask & (1 << idx)) == 0);
  2575. tcg_ctx->goto_tb_issue_mask |= 1 << idx;
  2576. #endif
  2577. plugin_gen_disable_mem_helpers();
  2578. tcg_gen_op1i(INDEX_op_goto_tb, idx);
  2579. }
  2580. void tcg_gen_lookup_and_goto_ptr(void)
  2581. {
  2582. TCGv_ptr ptr;
  2583. if (tcg_ctx->gen_tb->cflags & CF_NO_GOTO_PTR) {
  2584. tcg_gen_exit_tb(NULL, 0);
  2585. return;
  2586. }
  2587. plugin_gen_disable_mem_helpers();
  2588. ptr = tcg_temp_ebb_new_ptr();
  2589. gen_helper_lookup_tb_ptr(ptr, cpu_env);
  2590. tcg_gen_op1i(INDEX_op_goto_ptr, tcgv_ptr_arg(ptr));
  2591. tcg_temp_free_ptr(ptr);
  2592. }
  2593. static inline MemOp tcg_canonicalize_memop(MemOp op, bool is64, bool st)
  2594. {
  2595. /* Trigger the asserts within as early as possible. */
  2596. unsigned a_bits = get_alignment_bits(op);
  2597. /* Prefer MO_ALIGN+MO_XX over MO_ALIGN_XX+MO_XX */
  2598. if (a_bits == (op & MO_SIZE)) {
  2599. op = (op & ~MO_AMASK) | MO_ALIGN;
  2600. }
  2601. switch (op & MO_SIZE) {
  2602. case MO_8:
  2603. op &= ~MO_BSWAP;
  2604. break;
  2605. case MO_16:
  2606. break;
  2607. case MO_32:
  2608. if (!is64) {
  2609. op &= ~MO_SIGN;
  2610. }
  2611. break;
  2612. case MO_64:
  2613. if (is64) {
  2614. op &= ~MO_SIGN;
  2615. break;
  2616. }
  2617. /* fall through */
  2618. default:
  2619. g_assert_not_reached();
  2620. }
  2621. if (st) {
  2622. op &= ~MO_SIGN;
  2623. }
  2624. return op;
  2625. }
  2626. static void gen_ldst_i32(TCGOpcode opc, TCGv_i32 val, TCGv addr,
  2627. MemOp memop, TCGArg idx)
  2628. {
  2629. MemOpIdx oi = make_memop_idx(memop, idx);
  2630. #if TARGET_LONG_BITS == 32
  2631. tcg_gen_op3i_i32(opc, val, addr, oi);
  2632. #else
  2633. if (TCG_TARGET_REG_BITS == 32) {
  2634. tcg_gen_op4i_i32(opc, val, TCGV_LOW(addr), TCGV_HIGH(addr), oi);
  2635. } else {
  2636. tcg_gen_op3(opc, tcgv_i32_arg(val), tcgv_i64_arg(addr), oi);
  2637. }
  2638. #endif
  2639. }
  2640. static void gen_ldst_i64(TCGOpcode opc, TCGv_i64 val, TCGv addr,
  2641. MemOp memop, TCGArg idx)
  2642. {
  2643. MemOpIdx oi = make_memop_idx(memop, idx);
  2644. #if TARGET_LONG_BITS == 32
  2645. if (TCG_TARGET_REG_BITS == 32) {
  2646. tcg_gen_op4i_i32(opc, TCGV_LOW(val), TCGV_HIGH(val), addr, oi);
  2647. } else {
  2648. tcg_gen_op3(opc, tcgv_i64_arg(val), tcgv_i32_arg(addr), oi);
  2649. }
  2650. #else
  2651. if (TCG_TARGET_REG_BITS == 32) {
  2652. tcg_gen_op5i_i32(opc, TCGV_LOW(val), TCGV_HIGH(val),
  2653. TCGV_LOW(addr), TCGV_HIGH(addr), oi);
  2654. } else {
  2655. tcg_gen_op3i_i64(opc, val, addr, oi);
  2656. }
  2657. #endif
  2658. }
  2659. static void tcg_gen_req_mo(TCGBar type)
  2660. {
  2661. #ifdef TCG_GUEST_DEFAULT_MO
  2662. type &= TCG_GUEST_DEFAULT_MO;
  2663. #endif
  2664. type &= ~TCG_TARGET_DEFAULT_MO;
  2665. if (type) {
  2666. tcg_gen_mb(type | TCG_BAR_SC);
  2667. }
  2668. }
  2669. static inline TCGv plugin_prep_mem_callbacks(TCGv vaddr)
  2670. {
  2671. #ifdef CONFIG_PLUGIN
  2672. if (tcg_ctx->plugin_insn != NULL) {
  2673. /* Save a copy of the vaddr for use after a load. */
  2674. TCGv temp = tcg_temp_new();
  2675. tcg_gen_mov_tl(temp, vaddr);
  2676. return temp;
  2677. }
  2678. #endif
  2679. return vaddr;
  2680. }
  2681. static void plugin_gen_mem_callbacks(TCGv vaddr, MemOpIdx oi,
  2682. enum qemu_plugin_mem_rw rw)
  2683. {
  2684. #ifdef CONFIG_PLUGIN
  2685. if (tcg_ctx->plugin_insn != NULL) {
  2686. qemu_plugin_meminfo_t info = make_plugin_meminfo(oi, rw);
  2687. plugin_gen_empty_mem_callback(vaddr, info);
  2688. tcg_temp_free(vaddr);
  2689. }
  2690. #endif
  2691. }
  2692. void tcg_gen_qemu_ld_i32(TCGv_i32 val, TCGv addr, TCGArg idx, MemOp memop)
  2693. {
  2694. MemOp orig_memop;
  2695. MemOpIdx oi;
  2696. tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
  2697. memop = tcg_canonicalize_memop(memop, 0, 0);
  2698. oi = make_memop_idx(memop, idx);
  2699. orig_memop = memop;
  2700. if (!TCG_TARGET_HAS_MEMORY_BSWAP && (memop & MO_BSWAP)) {
  2701. memop &= ~MO_BSWAP;
  2702. /* The bswap primitive benefits from zero-extended input. */
  2703. if ((memop & MO_SSIZE) == MO_SW) {
  2704. memop &= ~MO_SIGN;
  2705. }
  2706. }
  2707. addr = plugin_prep_mem_callbacks(addr);
  2708. gen_ldst_i32(INDEX_op_qemu_ld_i32, val, addr, memop, idx);
  2709. plugin_gen_mem_callbacks(addr, oi, QEMU_PLUGIN_MEM_R);
  2710. if ((orig_memop ^ memop) & MO_BSWAP) {
  2711. switch (orig_memop & MO_SIZE) {
  2712. case MO_16:
  2713. tcg_gen_bswap16_i32(val, val, (orig_memop & MO_SIGN
  2714. ? TCG_BSWAP_IZ | TCG_BSWAP_OS
  2715. : TCG_BSWAP_IZ | TCG_BSWAP_OZ));
  2716. break;
  2717. case MO_32:
  2718. tcg_gen_bswap32_i32(val, val);
  2719. break;
  2720. default:
  2721. g_assert_not_reached();
  2722. }
  2723. }
  2724. }
  2725. void tcg_gen_qemu_st_i32(TCGv_i32 val, TCGv addr, TCGArg idx, MemOp memop)
  2726. {
  2727. TCGv_i32 swap = NULL;
  2728. MemOpIdx oi;
  2729. tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
  2730. memop = tcg_canonicalize_memop(memop, 0, 1);
  2731. oi = make_memop_idx(memop, idx);
  2732. if (!TCG_TARGET_HAS_MEMORY_BSWAP && (memop & MO_BSWAP)) {
  2733. swap = tcg_temp_ebb_new_i32();
  2734. switch (memop & MO_SIZE) {
  2735. case MO_16:
  2736. tcg_gen_bswap16_i32(swap, val, 0);
  2737. break;
  2738. case MO_32:
  2739. tcg_gen_bswap32_i32(swap, val);
  2740. break;
  2741. default:
  2742. g_assert_not_reached();
  2743. }
  2744. val = swap;
  2745. memop &= ~MO_BSWAP;
  2746. }
  2747. addr = plugin_prep_mem_callbacks(addr);
  2748. if (TCG_TARGET_HAS_qemu_st8_i32 && (memop & MO_SIZE) == MO_8) {
  2749. gen_ldst_i32(INDEX_op_qemu_st8_i32, val, addr, memop, idx);
  2750. } else {
  2751. gen_ldst_i32(INDEX_op_qemu_st_i32, val, addr, memop, idx);
  2752. }
  2753. plugin_gen_mem_callbacks(addr, oi, QEMU_PLUGIN_MEM_W);
  2754. if (swap) {
  2755. tcg_temp_free_i32(swap);
  2756. }
  2757. }
  2758. void tcg_gen_qemu_ld_i64(TCGv_i64 val, TCGv addr, TCGArg idx, MemOp memop)
  2759. {
  2760. MemOp orig_memop;
  2761. MemOpIdx oi;
  2762. if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
  2763. tcg_gen_qemu_ld_i32(TCGV_LOW(val), addr, idx, memop);
  2764. if (memop & MO_SIGN) {
  2765. tcg_gen_sari_i32(TCGV_HIGH(val), TCGV_LOW(val), 31);
  2766. } else {
  2767. tcg_gen_movi_i32(TCGV_HIGH(val), 0);
  2768. }
  2769. return;
  2770. }
  2771. tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
  2772. memop = tcg_canonicalize_memop(memop, 1, 0);
  2773. oi = make_memop_idx(memop, idx);
  2774. orig_memop = memop;
  2775. if (!TCG_TARGET_HAS_MEMORY_BSWAP && (memop & MO_BSWAP)) {
  2776. memop &= ~MO_BSWAP;
  2777. /* The bswap primitive benefits from zero-extended input. */
  2778. if ((memop & MO_SIGN) && (memop & MO_SIZE) < MO_64) {
  2779. memop &= ~MO_SIGN;
  2780. }
  2781. }
  2782. addr = plugin_prep_mem_callbacks(addr);
  2783. gen_ldst_i64(INDEX_op_qemu_ld_i64, val, addr, memop, idx);
  2784. plugin_gen_mem_callbacks(addr, oi, QEMU_PLUGIN_MEM_R);
  2785. if ((orig_memop ^ memop) & MO_BSWAP) {
  2786. int flags = (orig_memop & MO_SIGN
  2787. ? TCG_BSWAP_IZ | TCG_BSWAP_OS
  2788. : TCG_BSWAP_IZ | TCG_BSWAP_OZ);
  2789. switch (orig_memop & MO_SIZE) {
  2790. case MO_16:
  2791. tcg_gen_bswap16_i64(val, val, flags);
  2792. break;
  2793. case MO_32:
  2794. tcg_gen_bswap32_i64(val, val, flags);
  2795. break;
  2796. case MO_64:
  2797. tcg_gen_bswap64_i64(val, val);
  2798. break;
  2799. default:
  2800. g_assert_not_reached();
  2801. }
  2802. }
  2803. }
  2804. void tcg_gen_qemu_st_i64(TCGv_i64 val, TCGv addr, TCGArg idx, MemOp memop)
  2805. {
  2806. TCGv_i64 swap = NULL;
  2807. MemOpIdx oi;
  2808. if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
  2809. tcg_gen_qemu_st_i32(TCGV_LOW(val), addr, idx, memop);
  2810. return;
  2811. }
  2812. tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
  2813. memop = tcg_canonicalize_memop(memop, 1, 1);
  2814. oi = make_memop_idx(memop, idx);
  2815. if (!TCG_TARGET_HAS_MEMORY_BSWAP && (memop & MO_BSWAP)) {
  2816. swap = tcg_temp_ebb_new_i64();
  2817. switch (memop & MO_SIZE) {
  2818. case MO_16:
  2819. tcg_gen_bswap16_i64(swap, val, 0);
  2820. break;
  2821. case MO_32:
  2822. tcg_gen_bswap32_i64(swap, val, 0);
  2823. break;
  2824. case MO_64:
  2825. tcg_gen_bswap64_i64(swap, val);
  2826. break;
  2827. default:
  2828. g_assert_not_reached();
  2829. }
  2830. val = swap;
  2831. memop &= ~MO_BSWAP;
  2832. }
  2833. addr = plugin_prep_mem_callbacks(addr);
  2834. gen_ldst_i64(INDEX_op_qemu_st_i64, val, addr, memop, idx);
  2835. plugin_gen_mem_callbacks(addr, oi, QEMU_PLUGIN_MEM_W);
  2836. if (swap) {
  2837. tcg_temp_free_i64(swap);
  2838. }
  2839. }
  2840. static void canonicalize_memop_i128_as_i64(MemOp ret[2], MemOp orig)
  2841. {
  2842. MemOp mop_1 = orig, mop_2;
  2843. tcg_debug_assert((orig & MO_SIZE) == MO_128);
  2844. tcg_debug_assert((orig & MO_SIGN) == 0);
  2845. /* Use a memory ordering implemented by the host. */
  2846. if (!TCG_TARGET_HAS_MEMORY_BSWAP && (orig & MO_BSWAP)) {
  2847. mop_1 &= ~MO_BSWAP;
  2848. }
  2849. /* Reduce the size to 64-bit. */
  2850. mop_1 = (mop_1 & ~MO_SIZE) | MO_64;
  2851. /* Retain the alignment constraints of the original. */
  2852. switch (orig & MO_AMASK) {
  2853. case MO_UNALN:
  2854. case MO_ALIGN_2:
  2855. case MO_ALIGN_4:
  2856. mop_2 = mop_1;
  2857. break;
  2858. case MO_ALIGN_8:
  2859. /* Prefer MO_ALIGN+MO_64 to MO_ALIGN_8+MO_64. */
  2860. mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
  2861. mop_2 = mop_1;
  2862. break;
  2863. case MO_ALIGN:
  2864. /* Second has 8-byte alignment; first has 16-byte alignment. */
  2865. mop_2 = mop_1;
  2866. mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN_16;
  2867. break;
  2868. case MO_ALIGN_16:
  2869. case MO_ALIGN_32:
  2870. case MO_ALIGN_64:
  2871. /* Second has 8-byte alignment; first retains original. */
  2872. mop_2 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
  2873. break;
  2874. default:
  2875. g_assert_not_reached();
  2876. }
  2877. ret[0] = mop_1;
  2878. ret[1] = mop_2;
  2879. }
  2880. void tcg_gen_qemu_ld_i128(TCGv_i128 val, TCGv addr, TCGArg idx, MemOp memop)
  2881. {
  2882. MemOp mop[2];
  2883. TCGv addr_p8;
  2884. TCGv_i64 x, y;
  2885. canonicalize_memop_i128_as_i64(mop, memop);
  2886. tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
  2887. addr = plugin_prep_mem_callbacks(addr);
  2888. /* TODO: respect atomicity of the operation. */
  2889. /* TODO: allow the tcg backend to see the whole operation. */
  2890. /*
  2891. * Since there are no global TCGv_i128, there is no visible state
  2892. * changed if the second load faults. Load directly into the two
  2893. * subwords.
  2894. */
  2895. if ((memop & MO_BSWAP) == MO_LE) {
  2896. x = TCGV128_LOW(val);
  2897. y = TCGV128_HIGH(val);
  2898. } else {
  2899. x = TCGV128_HIGH(val);
  2900. y = TCGV128_LOW(val);
  2901. }
  2902. gen_ldst_i64(INDEX_op_qemu_ld_i64, x, addr, mop[0], idx);
  2903. if ((mop[0] ^ memop) & MO_BSWAP) {
  2904. tcg_gen_bswap64_i64(x, x);
  2905. }
  2906. addr_p8 = tcg_temp_new();
  2907. tcg_gen_addi_tl(addr_p8, addr, 8);
  2908. gen_ldst_i64(INDEX_op_qemu_ld_i64, y, addr_p8, mop[1], idx);
  2909. tcg_temp_free(addr_p8);
  2910. if ((mop[0] ^ memop) & MO_BSWAP) {
  2911. tcg_gen_bswap64_i64(y, y);
  2912. }
  2913. plugin_gen_mem_callbacks(addr, make_memop_idx(memop, idx),
  2914. QEMU_PLUGIN_MEM_R);
  2915. }
  2916. void tcg_gen_qemu_st_i128(TCGv_i128 val, TCGv addr, TCGArg idx, MemOp memop)
  2917. {
  2918. MemOp mop[2];
  2919. TCGv addr_p8;
  2920. TCGv_i64 x, y;
  2921. canonicalize_memop_i128_as_i64(mop, memop);
  2922. tcg_gen_req_mo(TCG_MO_ST_LD | TCG_MO_ST_ST);
  2923. addr = plugin_prep_mem_callbacks(addr);
  2924. /* TODO: respect atomicity of the operation. */
  2925. /* TODO: allow the tcg backend to see the whole operation. */
  2926. if ((memop & MO_BSWAP) == MO_LE) {
  2927. x = TCGV128_LOW(val);
  2928. y = TCGV128_HIGH(val);
  2929. } else {
  2930. x = TCGV128_HIGH(val);
  2931. y = TCGV128_LOW(val);
  2932. }
  2933. addr_p8 = tcg_temp_new();
  2934. if ((mop[0] ^ memop) & MO_BSWAP) {
  2935. TCGv_i64 t = tcg_temp_ebb_new_i64();
  2936. tcg_gen_bswap64_i64(t, x);
  2937. gen_ldst_i64(INDEX_op_qemu_st_i64, t, addr, mop[0], idx);
  2938. tcg_gen_bswap64_i64(t, y);
  2939. tcg_gen_addi_tl(addr_p8, addr, 8);
  2940. gen_ldst_i64(INDEX_op_qemu_st_i64, t, addr_p8, mop[1], idx);
  2941. tcg_temp_free_i64(t);
  2942. } else {
  2943. gen_ldst_i64(INDEX_op_qemu_st_i64, x, addr, mop[0], idx);
  2944. tcg_gen_addi_tl(addr_p8, addr, 8);
  2945. gen_ldst_i64(INDEX_op_qemu_st_i64, y, addr_p8, mop[1], idx);
  2946. }
  2947. tcg_temp_free(addr_p8);
  2948. plugin_gen_mem_callbacks(addr, make_memop_idx(memop, idx),
  2949. QEMU_PLUGIN_MEM_W);
  2950. }
  2951. static void tcg_gen_ext_i32(TCGv_i32 ret, TCGv_i32 val, MemOp opc)
  2952. {
  2953. switch (opc & MO_SSIZE) {
  2954. case MO_SB:
  2955. tcg_gen_ext8s_i32(ret, val);
  2956. break;
  2957. case MO_UB:
  2958. tcg_gen_ext8u_i32(ret, val);
  2959. break;
  2960. case MO_SW:
  2961. tcg_gen_ext16s_i32(ret, val);
  2962. break;
  2963. case MO_UW:
  2964. tcg_gen_ext16u_i32(ret, val);
  2965. break;
  2966. default:
  2967. tcg_gen_mov_i32(ret, val);
  2968. break;
  2969. }
  2970. }
  2971. static void tcg_gen_ext_i64(TCGv_i64 ret, TCGv_i64 val, MemOp opc)
  2972. {
  2973. switch (opc & MO_SSIZE) {
  2974. case MO_SB:
  2975. tcg_gen_ext8s_i64(ret, val);
  2976. break;
  2977. case MO_UB:
  2978. tcg_gen_ext8u_i64(ret, val);
  2979. break;
  2980. case MO_SW:
  2981. tcg_gen_ext16s_i64(ret, val);
  2982. break;
  2983. case MO_UW:
  2984. tcg_gen_ext16u_i64(ret, val);
  2985. break;
  2986. case MO_SL:
  2987. tcg_gen_ext32s_i64(ret, val);
  2988. break;
  2989. case MO_UL:
  2990. tcg_gen_ext32u_i64(ret, val);
  2991. break;
  2992. default:
  2993. tcg_gen_mov_i64(ret, val);
  2994. break;
  2995. }
  2996. }
  2997. typedef void (*gen_atomic_cx_i32)(TCGv_i32, TCGv_env, TCGv,
  2998. TCGv_i32, TCGv_i32, TCGv_i32);
  2999. typedef void (*gen_atomic_cx_i64)(TCGv_i64, TCGv_env, TCGv,
  3000. TCGv_i64, TCGv_i64, TCGv_i32);
  3001. typedef void (*gen_atomic_cx_i128)(TCGv_i128, TCGv_env, TCGv,
  3002. TCGv_i128, TCGv_i128, TCGv_i32);
  3003. typedef void (*gen_atomic_op_i32)(TCGv_i32, TCGv_env, TCGv,
  3004. TCGv_i32, TCGv_i32);
  3005. typedef void (*gen_atomic_op_i64)(TCGv_i64, TCGv_env, TCGv,
  3006. TCGv_i64, TCGv_i32);
  3007. #ifdef CONFIG_ATOMIC64
  3008. # define WITH_ATOMIC64(X) X,
  3009. #else
  3010. # define WITH_ATOMIC64(X)
  3011. #endif
  3012. #ifdef CONFIG_CMPXCHG128
  3013. # define WITH_ATOMIC128(X) X,
  3014. #else
  3015. # define WITH_ATOMIC128(X)
  3016. #endif
  3017. static void * const table_cmpxchg[(MO_SIZE | MO_BSWAP) + 1] = {
  3018. [MO_8] = gen_helper_atomic_cmpxchgb,
  3019. [MO_16 | MO_LE] = gen_helper_atomic_cmpxchgw_le,
  3020. [MO_16 | MO_BE] = gen_helper_atomic_cmpxchgw_be,
  3021. [MO_32 | MO_LE] = gen_helper_atomic_cmpxchgl_le,
  3022. [MO_32 | MO_BE] = gen_helper_atomic_cmpxchgl_be,
  3023. WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_cmpxchgq_le)
  3024. WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_cmpxchgq_be)
  3025. WITH_ATOMIC128([MO_128 | MO_LE] = gen_helper_atomic_cmpxchgo_le)
  3026. WITH_ATOMIC128([MO_128 | MO_BE] = gen_helper_atomic_cmpxchgo_be)
  3027. };
  3028. void tcg_gen_nonatomic_cmpxchg_i32(TCGv_i32 retv, TCGv addr, TCGv_i32 cmpv,
  3029. TCGv_i32 newv, TCGArg idx, MemOp memop)
  3030. {
  3031. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  3032. TCGv_i32 t2 = tcg_temp_ebb_new_i32();
  3033. tcg_gen_ext_i32(t2, cmpv, memop & MO_SIZE);
  3034. tcg_gen_qemu_ld_i32(t1, addr, idx, memop & ~MO_SIGN);
  3035. tcg_gen_movcond_i32(TCG_COND_EQ, t2, t1, t2, newv, t1);
  3036. tcg_gen_qemu_st_i32(t2, addr, idx, memop);
  3037. tcg_temp_free_i32(t2);
  3038. if (memop & MO_SIGN) {
  3039. tcg_gen_ext_i32(retv, t1, memop);
  3040. } else {
  3041. tcg_gen_mov_i32(retv, t1);
  3042. }
  3043. tcg_temp_free_i32(t1);
  3044. }
  3045. void tcg_gen_atomic_cmpxchg_i32(TCGv_i32 retv, TCGv addr, TCGv_i32 cmpv,
  3046. TCGv_i32 newv, TCGArg idx, MemOp memop)
  3047. {
  3048. gen_atomic_cx_i32 gen;
  3049. MemOpIdx oi;
  3050. if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
  3051. tcg_gen_nonatomic_cmpxchg_i32(retv, addr, cmpv, newv, idx, memop);
  3052. return;
  3053. }
  3054. memop = tcg_canonicalize_memop(memop, 0, 0);
  3055. gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
  3056. tcg_debug_assert(gen != NULL);
  3057. oi = make_memop_idx(memop & ~MO_SIGN, idx);
  3058. gen(retv, cpu_env, addr, cmpv, newv, tcg_constant_i32(oi));
  3059. if (memop & MO_SIGN) {
  3060. tcg_gen_ext_i32(retv, retv, memop);
  3061. }
  3062. }
  3063. void tcg_gen_nonatomic_cmpxchg_i64(TCGv_i64 retv, TCGv addr, TCGv_i64 cmpv,
  3064. TCGv_i64 newv, TCGArg idx, MemOp memop)
  3065. {
  3066. TCGv_i64 t1, t2;
  3067. if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
  3068. tcg_gen_nonatomic_cmpxchg_i32(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
  3069. TCGV_LOW(newv), idx, memop);
  3070. if (memop & MO_SIGN) {
  3071. tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
  3072. } else {
  3073. tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
  3074. }
  3075. return;
  3076. }
  3077. t1 = tcg_temp_ebb_new_i64();
  3078. t2 = tcg_temp_ebb_new_i64();
  3079. tcg_gen_ext_i64(t2, cmpv, memop & MO_SIZE);
  3080. tcg_gen_qemu_ld_i64(t1, addr, idx, memop & ~MO_SIGN);
  3081. tcg_gen_movcond_i64(TCG_COND_EQ, t2, t1, t2, newv, t1);
  3082. tcg_gen_qemu_st_i64(t2, addr, idx, memop);
  3083. tcg_temp_free_i64(t2);
  3084. if (memop & MO_SIGN) {
  3085. tcg_gen_ext_i64(retv, t1, memop);
  3086. } else {
  3087. tcg_gen_mov_i64(retv, t1);
  3088. }
  3089. tcg_temp_free_i64(t1);
  3090. }
  3091. void tcg_gen_atomic_cmpxchg_i64(TCGv_i64 retv, TCGv addr, TCGv_i64 cmpv,
  3092. TCGv_i64 newv, TCGArg idx, MemOp memop)
  3093. {
  3094. if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
  3095. tcg_gen_nonatomic_cmpxchg_i64(retv, addr, cmpv, newv, idx, memop);
  3096. return;
  3097. }
  3098. if ((memop & MO_SIZE) == MO_64) {
  3099. gen_atomic_cx_i64 gen;
  3100. memop = tcg_canonicalize_memop(memop, 1, 0);
  3101. gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
  3102. if (gen) {
  3103. MemOpIdx oi = make_memop_idx(memop, idx);
  3104. gen(retv, cpu_env, addr, cmpv, newv, tcg_constant_i32(oi));
  3105. return;
  3106. }
  3107. gen_helper_exit_atomic(cpu_env);
  3108. /*
  3109. * Produce a result for a well-formed opcode stream. This satisfies
  3110. * liveness for set before used, which happens before this dead code
  3111. * is removed.
  3112. */
  3113. tcg_gen_movi_i64(retv, 0);
  3114. return;
  3115. }
  3116. if (TCG_TARGET_REG_BITS == 32) {
  3117. tcg_gen_atomic_cmpxchg_i32(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
  3118. TCGV_LOW(newv), idx, memop);
  3119. if (memop & MO_SIGN) {
  3120. tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
  3121. } else {
  3122. tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
  3123. }
  3124. } else {
  3125. TCGv_i32 c32 = tcg_temp_ebb_new_i32();
  3126. TCGv_i32 n32 = tcg_temp_ebb_new_i32();
  3127. TCGv_i32 r32 = tcg_temp_ebb_new_i32();
  3128. tcg_gen_extrl_i64_i32(c32, cmpv);
  3129. tcg_gen_extrl_i64_i32(n32, newv);
  3130. tcg_gen_atomic_cmpxchg_i32(r32, addr, c32, n32, idx, memop & ~MO_SIGN);
  3131. tcg_temp_free_i32(c32);
  3132. tcg_temp_free_i32(n32);
  3133. tcg_gen_extu_i32_i64(retv, r32);
  3134. tcg_temp_free_i32(r32);
  3135. if (memop & MO_SIGN) {
  3136. tcg_gen_ext_i64(retv, retv, memop);
  3137. }
  3138. }
  3139. }
  3140. void tcg_gen_nonatomic_cmpxchg_i128(TCGv_i128 retv, TCGv addr, TCGv_i128 cmpv,
  3141. TCGv_i128 newv, TCGArg idx, MemOp memop)
  3142. {
  3143. if (TCG_TARGET_REG_BITS == 32) {
  3144. /* Inline expansion below is simply too large for 32-bit hosts. */
  3145. gen_atomic_cx_i128 gen = ((memop & MO_BSWAP) == MO_LE
  3146. ? gen_helper_nonatomic_cmpxchgo_le
  3147. : gen_helper_nonatomic_cmpxchgo_be);
  3148. MemOpIdx oi = make_memop_idx(memop, idx);
  3149. tcg_debug_assert((memop & MO_SIZE) == MO_128);
  3150. tcg_debug_assert((memop & MO_SIGN) == 0);
  3151. gen(retv, cpu_env, addr, cmpv, newv, tcg_constant_i32(oi));
  3152. } else {
  3153. TCGv_i128 oldv = tcg_temp_ebb_new_i128();
  3154. TCGv_i128 tmpv = tcg_temp_ebb_new_i128();
  3155. TCGv_i64 t0 = tcg_temp_ebb_new_i64();
  3156. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  3157. TCGv_i64 z = tcg_constant_i64(0);
  3158. tcg_gen_qemu_ld_i128(oldv, addr, idx, memop);
  3159. /* Compare i128 */
  3160. tcg_gen_xor_i64(t0, TCGV128_LOW(oldv), TCGV128_LOW(cmpv));
  3161. tcg_gen_xor_i64(t1, TCGV128_HIGH(oldv), TCGV128_HIGH(cmpv));
  3162. tcg_gen_or_i64(t0, t0, t1);
  3163. /* tmpv = equal ? newv : oldv */
  3164. tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_LOW(tmpv), t0, z,
  3165. TCGV128_LOW(newv), TCGV128_LOW(oldv));
  3166. tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_HIGH(tmpv), t0, z,
  3167. TCGV128_HIGH(newv), TCGV128_HIGH(oldv));
  3168. /* Unconditional writeback. */
  3169. tcg_gen_qemu_st_i128(tmpv, addr, idx, memop);
  3170. tcg_gen_mov_i128(retv, oldv);
  3171. tcg_temp_free_i64(t0);
  3172. tcg_temp_free_i64(t1);
  3173. tcg_temp_free_i128(tmpv);
  3174. tcg_temp_free_i128(oldv);
  3175. }
  3176. }
  3177. void tcg_gen_atomic_cmpxchg_i128(TCGv_i128 retv, TCGv addr, TCGv_i128 cmpv,
  3178. TCGv_i128 newv, TCGArg idx, MemOp memop)
  3179. {
  3180. gen_atomic_cx_i128 gen;
  3181. if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
  3182. tcg_gen_nonatomic_cmpxchg_i128(retv, addr, cmpv, newv, idx, memop);
  3183. return;
  3184. }
  3185. tcg_debug_assert((memop & MO_SIZE) == MO_128);
  3186. tcg_debug_assert((memop & MO_SIGN) == 0);
  3187. gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
  3188. if (gen) {
  3189. MemOpIdx oi = make_memop_idx(memop, idx);
  3190. gen(retv, cpu_env, addr, cmpv, newv, tcg_constant_i32(oi));
  3191. return;
  3192. }
  3193. gen_helper_exit_atomic(cpu_env);
  3194. /*
  3195. * Produce a result for a well-formed opcode stream. This satisfies
  3196. * liveness for set before used, which happens before this dead code
  3197. * is removed.
  3198. */
  3199. tcg_gen_movi_i64(TCGV128_LOW(retv), 0);
  3200. tcg_gen_movi_i64(TCGV128_HIGH(retv), 0);
  3201. }
  3202. static void do_nonatomic_op_i32(TCGv_i32 ret, TCGv addr, TCGv_i32 val,
  3203. TCGArg idx, MemOp memop, bool new_val,
  3204. void (*gen)(TCGv_i32, TCGv_i32, TCGv_i32))
  3205. {
  3206. TCGv_i32 t1 = tcg_temp_ebb_new_i32();
  3207. TCGv_i32 t2 = tcg_temp_ebb_new_i32();
  3208. memop = tcg_canonicalize_memop(memop, 0, 0);
  3209. tcg_gen_qemu_ld_i32(t1, addr, idx, memop);
  3210. tcg_gen_ext_i32(t2, val, memop);
  3211. gen(t2, t1, t2);
  3212. tcg_gen_qemu_st_i32(t2, addr, idx, memop);
  3213. tcg_gen_ext_i32(ret, (new_val ? t2 : t1), memop);
  3214. tcg_temp_free_i32(t1);
  3215. tcg_temp_free_i32(t2);
  3216. }
  3217. static void do_atomic_op_i32(TCGv_i32 ret, TCGv addr, TCGv_i32 val,
  3218. TCGArg idx, MemOp memop, void * const table[])
  3219. {
  3220. gen_atomic_op_i32 gen;
  3221. MemOpIdx oi;
  3222. memop = tcg_canonicalize_memop(memop, 0, 0);
  3223. gen = table[memop & (MO_SIZE | MO_BSWAP)];
  3224. tcg_debug_assert(gen != NULL);
  3225. oi = make_memop_idx(memop & ~MO_SIGN, idx);
  3226. gen(ret, cpu_env, addr, val, tcg_constant_i32(oi));
  3227. if (memop & MO_SIGN) {
  3228. tcg_gen_ext_i32(ret, ret, memop);
  3229. }
  3230. }
  3231. static void do_nonatomic_op_i64(TCGv_i64 ret, TCGv addr, TCGv_i64 val,
  3232. TCGArg idx, MemOp memop, bool new_val,
  3233. void (*gen)(TCGv_i64, TCGv_i64, TCGv_i64))
  3234. {
  3235. TCGv_i64 t1 = tcg_temp_ebb_new_i64();
  3236. TCGv_i64 t2 = tcg_temp_ebb_new_i64();
  3237. memop = tcg_canonicalize_memop(memop, 1, 0);
  3238. tcg_gen_qemu_ld_i64(t1, addr, idx, memop);
  3239. tcg_gen_ext_i64(t2, val, memop);
  3240. gen(t2, t1, t2);
  3241. tcg_gen_qemu_st_i64(t2, addr, idx, memop);
  3242. tcg_gen_ext_i64(ret, (new_val ? t2 : t1), memop);
  3243. tcg_temp_free_i64(t1);
  3244. tcg_temp_free_i64(t2);
  3245. }
  3246. static void do_atomic_op_i64(TCGv_i64 ret, TCGv addr, TCGv_i64 val,
  3247. TCGArg idx, MemOp memop, void * const table[])
  3248. {
  3249. memop = tcg_canonicalize_memop(memop, 1, 0);
  3250. if ((memop & MO_SIZE) == MO_64) {
  3251. #ifdef CONFIG_ATOMIC64
  3252. gen_atomic_op_i64 gen;
  3253. MemOpIdx oi;
  3254. gen = table[memop & (MO_SIZE | MO_BSWAP)];
  3255. tcg_debug_assert(gen != NULL);
  3256. oi = make_memop_idx(memop & ~MO_SIGN, idx);
  3257. gen(ret, cpu_env, addr, val, tcg_constant_i32(oi));
  3258. #else
  3259. gen_helper_exit_atomic(cpu_env);
  3260. /* Produce a result, so that we have a well-formed opcode stream
  3261. with respect to uses of the result in the (dead) code following. */
  3262. tcg_gen_movi_i64(ret, 0);
  3263. #endif /* CONFIG_ATOMIC64 */
  3264. } else {
  3265. TCGv_i32 v32 = tcg_temp_ebb_new_i32();
  3266. TCGv_i32 r32 = tcg_temp_ebb_new_i32();
  3267. tcg_gen_extrl_i64_i32(v32, val);
  3268. do_atomic_op_i32(r32, addr, v32, idx, memop & ~MO_SIGN, table);
  3269. tcg_temp_free_i32(v32);
  3270. tcg_gen_extu_i32_i64(ret, r32);
  3271. tcg_temp_free_i32(r32);
  3272. if (memop & MO_SIGN) {
  3273. tcg_gen_ext_i64(ret, ret, memop);
  3274. }
  3275. }
  3276. }
  3277. #define GEN_ATOMIC_HELPER(NAME, OP, NEW) \
  3278. static void * const table_##NAME[(MO_SIZE | MO_BSWAP) + 1] = { \
  3279. [MO_8] = gen_helper_atomic_##NAME##b, \
  3280. [MO_16 | MO_LE] = gen_helper_atomic_##NAME##w_le, \
  3281. [MO_16 | MO_BE] = gen_helper_atomic_##NAME##w_be, \
  3282. [MO_32 | MO_LE] = gen_helper_atomic_##NAME##l_le, \
  3283. [MO_32 | MO_BE] = gen_helper_atomic_##NAME##l_be, \
  3284. WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_##NAME##q_le) \
  3285. WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_##NAME##q_be) \
  3286. }; \
  3287. void tcg_gen_atomic_##NAME##_i32 \
  3288. (TCGv_i32 ret, TCGv addr, TCGv_i32 val, TCGArg idx, MemOp memop) \
  3289. { \
  3290. if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) { \
  3291. do_atomic_op_i32(ret, addr, val, idx, memop, table_##NAME); \
  3292. } else { \
  3293. do_nonatomic_op_i32(ret, addr, val, idx, memop, NEW, \
  3294. tcg_gen_##OP##_i32); \
  3295. } \
  3296. } \
  3297. void tcg_gen_atomic_##NAME##_i64 \
  3298. (TCGv_i64 ret, TCGv addr, TCGv_i64 val, TCGArg idx, MemOp memop) \
  3299. { \
  3300. if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) { \
  3301. do_atomic_op_i64(ret, addr, val, idx, memop, table_##NAME); \
  3302. } else { \
  3303. do_nonatomic_op_i64(ret, addr, val, idx, memop, NEW, \
  3304. tcg_gen_##OP##_i64); \
  3305. } \
  3306. }
  3307. GEN_ATOMIC_HELPER(fetch_add, add, 0)
  3308. GEN_ATOMIC_HELPER(fetch_and, and, 0)
  3309. GEN_ATOMIC_HELPER(fetch_or, or, 0)
  3310. GEN_ATOMIC_HELPER(fetch_xor, xor, 0)
  3311. GEN_ATOMIC_HELPER(fetch_smin, smin, 0)
  3312. GEN_ATOMIC_HELPER(fetch_umin, umin, 0)
  3313. GEN_ATOMIC_HELPER(fetch_smax, smax, 0)
  3314. GEN_ATOMIC_HELPER(fetch_umax, umax, 0)
  3315. GEN_ATOMIC_HELPER(add_fetch, add, 1)
  3316. GEN_ATOMIC_HELPER(and_fetch, and, 1)
  3317. GEN_ATOMIC_HELPER(or_fetch, or, 1)
  3318. GEN_ATOMIC_HELPER(xor_fetch, xor, 1)
  3319. GEN_ATOMIC_HELPER(smin_fetch, smin, 1)
  3320. GEN_ATOMIC_HELPER(umin_fetch, umin, 1)
  3321. GEN_ATOMIC_HELPER(smax_fetch, smax, 1)
  3322. GEN_ATOMIC_HELPER(umax_fetch, umax, 1)
  3323. static void tcg_gen_mov2_i32(TCGv_i32 r, TCGv_i32 a, TCGv_i32 b)
  3324. {
  3325. tcg_gen_mov_i32(r, b);
  3326. }
  3327. static void tcg_gen_mov2_i64(TCGv_i64 r, TCGv_i64 a, TCGv_i64 b)
  3328. {
  3329. tcg_gen_mov_i64(r, b);
  3330. }
  3331. GEN_ATOMIC_HELPER(xchg, mov2, 0)
  3332. #undef GEN_ATOMIC_HELPER