tcg-target.c.inc 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. /*
  2. * Tiny Code Generator for QEMU
  3. *
  4. * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org>
  5. * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net>
  6. * Based on i386/tcg-target.c - Copyright (c) 2008 Fabrice Bellard
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "../tcg-ldst.c.inc"
  27. #if HOST_BIG_ENDIAN
  28. # define MIPS_BE 1
  29. #else
  30. # define MIPS_BE 0
  31. #endif
  32. #if TCG_TARGET_REG_BITS == 32
  33. # define LO_OFF (MIPS_BE * 4)
  34. # define HI_OFF (4 - LO_OFF)
  35. #else
  36. /* To assert at compile-time that these values are never used
  37. for TCG_TARGET_REG_BITS == 64. */
  38. int link_error(void);
  39. # define LO_OFF link_error()
  40. # define HI_OFF link_error()
  41. #endif
  42. #ifdef CONFIG_DEBUG_TCG
  43. static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
  44. "zero",
  45. "at",
  46. "v0",
  47. "v1",
  48. "a0",
  49. "a1",
  50. "a2",
  51. "a3",
  52. "t0",
  53. "t1",
  54. "t2",
  55. "t3",
  56. "t4",
  57. "t5",
  58. "t6",
  59. "t7",
  60. "s0",
  61. "s1",
  62. "s2",
  63. "s3",
  64. "s4",
  65. "s5",
  66. "s6",
  67. "s7",
  68. "t8",
  69. "t9",
  70. "k0",
  71. "k1",
  72. "gp",
  73. "sp",
  74. "s8",
  75. "ra",
  76. };
  77. #endif
  78. #define TCG_TMP0 TCG_REG_AT
  79. #define TCG_TMP1 TCG_REG_T9
  80. #define TCG_TMP2 TCG_REG_T8
  81. #define TCG_TMP3 TCG_REG_T7
  82. #ifndef CONFIG_SOFTMMU
  83. #define TCG_GUEST_BASE_REG TCG_REG_S1
  84. #endif
  85. /* check if we really need so many registers :P */
  86. static const int tcg_target_reg_alloc_order[] = {
  87. /* Call saved registers. */
  88. TCG_REG_S0,
  89. TCG_REG_S1,
  90. TCG_REG_S2,
  91. TCG_REG_S3,
  92. TCG_REG_S4,
  93. TCG_REG_S5,
  94. TCG_REG_S6,
  95. TCG_REG_S7,
  96. TCG_REG_S8,
  97. /* Call clobbered registers. */
  98. TCG_REG_T4,
  99. TCG_REG_T5,
  100. TCG_REG_T6,
  101. TCG_REG_T7,
  102. TCG_REG_T8,
  103. TCG_REG_T9,
  104. TCG_REG_V1,
  105. TCG_REG_V0,
  106. /* Argument registers, opposite order of allocation. */
  107. TCG_REG_T3,
  108. TCG_REG_T2,
  109. TCG_REG_T1,
  110. TCG_REG_T0,
  111. TCG_REG_A3,
  112. TCG_REG_A2,
  113. TCG_REG_A1,
  114. TCG_REG_A0,
  115. };
  116. static const TCGReg tcg_target_call_iarg_regs[] = {
  117. TCG_REG_A0,
  118. TCG_REG_A1,
  119. TCG_REG_A2,
  120. TCG_REG_A3,
  121. #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
  122. TCG_REG_T0,
  123. TCG_REG_T1,
  124. TCG_REG_T2,
  125. TCG_REG_T3,
  126. #endif
  127. };
  128. static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
  129. {
  130. tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
  131. tcg_debug_assert(slot >= 0 && slot <= 1);
  132. return TCG_REG_V0 + slot;
  133. }
  134. static const tcg_insn_unit *tb_ret_addr;
  135. static const tcg_insn_unit *bswap32_addr;
  136. static const tcg_insn_unit *bswap32u_addr;
  137. static const tcg_insn_unit *bswap64_addr;
  138. static bool reloc_pc16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
  139. {
  140. /* Let the compiler perform the right-shift as part of the arithmetic. */
  141. const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
  142. ptrdiff_t disp = target - (src_rx + 1);
  143. if (disp == (int16_t)disp) {
  144. *src_rw = deposit32(*src_rw, 0, 16, disp);
  145. return true;
  146. }
  147. return false;
  148. }
  149. static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
  150. intptr_t value, intptr_t addend)
  151. {
  152. tcg_debug_assert(type == R_MIPS_PC16);
  153. tcg_debug_assert(addend == 0);
  154. return reloc_pc16(code_ptr, (const tcg_insn_unit *)value);
  155. }
  156. #define TCG_CT_CONST_ZERO 0x100
  157. #define TCG_CT_CONST_U16 0x200 /* Unsigned 16-bit: 0 - 0xffff. */
  158. #define TCG_CT_CONST_S16 0x400 /* Signed 16-bit: -32768 - 32767 */
  159. #define TCG_CT_CONST_P2M1 0x800 /* Power of 2 minus 1. */
  160. #define TCG_CT_CONST_N16 0x1000 /* "Negatable" 16-bit: -32767 - 32767 */
  161. #define TCG_CT_CONST_WSZ 0x2000 /* word size */
  162. #define ALL_GENERAL_REGS 0xffffffffu
  163. #define NOA0_REGS (ALL_GENERAL_REGS & ~(1 << TCG_REG_A0))
  164. #ifdef CONFIG_SOFTMMU
  165. #define ALL_QLOAD_REGS \
  166. (NOA0_REGS & ~((TCG_TARGET_REG_BITS < TARGET_LONG_BITS) << TCG_REG_A2))
  167. #define ALL_QSTORE_REGS \
  168. (NOA0_REGS & ~(TCG_TARGET_REG_BITS < TARGET_LONG_BITS \
  169. ? (1 << TCG_REG_A2) | (1 << TCG_REG_A3) \
  170. : (1 << TCG_REG_A1)))
  171. #else
  172. #define ALL_QLOAD_REGS NOA0_REGS
  173. #define ALL_QSTORE_REGS NOA0_REGS
  174. #endif
  175. static bool is_p2m1(tcg_target_long val)
  176. {
  177. return val && ((val + 1) & val) == 0;
  178. }
  179. /* test if a constant matches the constraint */
  180. static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
  181. {
  182. if (ct & TCG_CT_CONST) {
  183. return 1;
  184. } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
  185. return 1;
  186. } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
  187. return 1;
  188. } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
  189. return 1;
  190. } else if ((ct & TCG_CT_CONST_N16) && val >= -32767 && val <= 32767) {
  191. return 1;
  192. } else if ((ct & TCG_CT_CONST_P2M1)
  193. && use_mips32r2_instructions && is_p2m1(val)) {
  194. return 1;
  195. } else if ((ct & TCG_CT_CONST_WSZ)
  196. && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. /* instruction opcodes */
  202. typedef enum {
  203. OPC_J = 002 << 26,
  204. OPC_JAL = 003 << 26,
  205. OPC_BEQ = 004 << 26,
  206. OPC_BNE = 005 << 26,
  207. OPC_BLEZ = 006 << 26,
  208. OPC_BGTZ = 007 << 26,
  209. OPC_ADDIU = 011 << 26,
  210. OPC_SLTI = 012 << 26,
  211. OPC_SLTIU = 013 << 26,
  212. OPC_ANDI = 014 << 26,
  213. OPC_ORI = 015 << 26,
  214. OPC_XORI = 016 << 26,
  215. OPC_LUI = 017 << 26,
  216. OPC_BNEL = 025 << 26,
  217. OPC_BNEZALC_R6 = 030 << 26,
  218. OPC_DADDIU = 031 << 26,
  219. OPC_LDL = 032 << 26,
  220. OPC_LDR = 033 << 26,
  221. OPC_LB = 040 << 26,
  222. OPC_LH = 041 << 26,
  223. OPC_LWL = 042 << 26,
  224. OPC_LW = 043 << 26,
  225. OPC_LBU = 044 << 26,
  226. OPC_LHU = 045 << 26,
  227. OPC_LWR = 046 << 26,
  228. OPC_LWU = 047 << 26,
  229. OPC_SB = 050 << 26,
  230. OPC_SH = 051 << 26,
  231. OPC_SWL = 052 << 26,
  232. OPC_SW = 053 << 26,
  233. OPC_SDL = 054 << 26,
  234. OPC_SDR = 055 << 26,
  235. OPC_SWR = 056 << 26,
  236. OPC_LD = 067 << 26,
  237. OPC_SD = 077 << 26,
  238. OPC_SPECIAL = 000 << 26,
  239. OPC_SLL = OPC_SPECIAL | 000,
  240. OPC_SRL = OPC_SPECIAL | 002,
  241. OPC_ROTR = OPC_SPECIAL | 002 | (1 << 21),
  242. OPC_SRA = OPC_SPECIAL | 003,
  243. OPC_SLLV = OPC_SPECIAL | 004,
  244. OPC_SRLV = OPC_SPECIAL | 006,
  245. OPC_ROTRV = OPC_SPECIAL | 006 | 0100,
  246. OPC_SRAV = OPC_SPECIAL | 007,
  247. OPC_JR_R5 = OPC_SPECIAL | 010,
  248. OPC_JALR = OPC_SPECIAL | 011,
  249. OPC_MOVZ = OPC_SPECIAL | 012,
  250. OPC_MOVN = OPC_SPECIAL | 013,
  251. OPC_SYNC = OPC_SPECIAL | 017,
  252. OPC_MFHI = OPC_SPECIAL | 020,
  253. OPC_MFLO = OPC_SPECIAL | 022,
  254. OPC_DSLLV = OPC_SPECIAL | 024,
  255. OPC_DSRLV = OPC_SPECIAL | 026,
  256. OPC_DROTRV = OPC_SPECIAL | 026 | 0100,
  257. OPC_DSRAV = OPC_SPECIAL | 027,
  258. OPC_MULT = OPC_SPECIAL | 030,
  259. OPC_MUL_R6 = OPC_SPECIAL | 030 | 0200,
  260. OPC_MUH = OPC_SPECIAL | 030 | 0300,
  261. OPC_MULTU = OPC_SPECIAL | 031,
  262. OPC_MULU = OPC_SPECIAL | 031 | 0200,
  263. OPC_MUHU = OPC_SPECIAL | 031 | 0300,
  264. OPC_DIV = OPC_SPECIAL | 032,
  265. OPC_DIV_R6 = OPC_SPECIAL | 032 | 0200,
  266. OPC_MOD = OPC_SPECIAL | 032 | 0300,
  267. OPC_DIVU = OPC_SPECIAL | 033,
  268. OPC_DIVU_R6 = OPC_SPECIAL | 033 | 0200,
  269. OPC_MODU = OPC_SPECIAL | 033 | 0300,
  270. OPC_DMULT = OPC_SPECIAL | 034,
  271. OPC_DMUL = OPC_SPECIAL | 034 | 0200,
  272. OPC_DMUH = OPC_SPECIAL | 034 | 0300,
  273. OPC_DMULTU = OPC_SPECIAL | 035,
  274. OPC_DMULU = OPC_SPECIAL | 035 | 0200,
  275. OPC_DMUHU = OPC_SPECIAL | 035 | 0300,
  276. OPC_DDIV = OPC_SPECIAL | 036,
  277. OPC_DDIV_R6 = OPC_SPECIAL | 036 | 0200,
  278. OPC_DMOD = OPC_SPECIAL | 036 | 0300,
  279. OPC_DDIVU = OPC_SPECIAL | 037,
  280. OPC_DDIVU_R6 = OPC_SPECIAL | 037 | 0200,
  281. OPC_DMODU = OPC_SPECIAL | 037 | 0300,
  282. OPC_ADDU = OPC_SPECIAL | 041,
  283. OPC_SUBU = OPC_SPECIAL | 043,
  284. OPC_AND = OPC_SPECIAL | 044,
  285. OPC_OR = OPC_SPECIAL | 045,
  286. OPC_XOR = OPC_SPECIAL | 046,
  287. OPC_NOR = OPC_SPECIAL | 047,
  288. OPC_SLT = OPC_SPECIAL | 052,
  289. OPC_SLTU = OPC_SPECIAL | 053,
  290. OPC_DADDU = OPC_SPECIAL | 055,
  291. OPC_DSUBU = OPC_SPECIAL | 057,
  292. OPC_SELEQZ = OPC_SPECIAL | 065,
  293. OPC_SELNEZ = OPC_SPECIAL | 067,
  294. OPC_DSLL = OPC_SPECIAL | 070,
  295. OPC_DSRL = OPC_SPECIAL | 072,
  296. OPC_DROTR = OPC_SPECIAL | 072 | (1 << 21),
  297. OPC_DSRA = OPC_SPECIAL | 073,
  298. OPC_DSLL32 = OPC_SPECIAL | 074,
  299. OPC_DSRL32 = OPC_SPECIAL | 076,
  300. OPC_DROTR32 = OPC_SPECIAL | 076 | (1 << 21),
  301. OPC_DSRA32 = OPC_SPECIAL | 077,
  302. OPC_CLZ_R6 = OPC_SPECIAL | 0120,
  303. OPC_DCLZ_R6 = OPC_SPECIAL | 0122,
  304. OPC_REGIMM = 001 << 26,
  305. OPC_BLTZ = OPC_REGIMM | (000 << 16),
  306. OPC_BGEZ = OPC_REGIMM | (001 << 16),
  307. OPC_SPECIAL2 = 034 << 26,
  308. OPC_MUL_R5 = OPC_SPECIAL2 | 002,
  309. OPC_CLZ = OPC_SPECIAL2 | 040,
  310. OPC_DCLZ = OPC_SPECIAL2 | 044,
  311. OPC_SPECIAL3 = 037 << 26,
  312. OPC_EXT = OPC_SPECIAL3 | 000,
  313. OPC_DEXTM = OPC_SPECIAL3 | 001,
  314. OPC_DEXTU = OPC_SPECIAL3 | 002,
  315. OPC_DEXT = OPC_SPECIAL3 | 003,
  316. OPC_INS = OPC_SPECIAL3 | 004,
  317. OPC_DINSM = OPC_SPECIAL3 | 005,
  318. OPC_DINSU = OPC_SPECIAL3 | 006,
  319. OPC_DINS = OPC_SPECIAL3 | 007,
  320. OPC_WSBH = OPC_SPECIAL3 | 00240,
  321. OPC_DSBH = OPC_SPECIAL3 | 00244,
  322. OPC_DSHD = OPC_SPECIAL3 | 00544,
  323. OPC_SEB = OPC_SPECIAL3 | 02040,
  324. OPC_SEH = OPC_SPECIAL3 | 03040,
  325. /* MIPS r6 doesn't have JR, JALR should be used instead */
  326. OPC_JR = use_mips32r6_instructions ? OPC_JALR : OPC_JR_R5,
  327. /*
  328. * MIPS r6 replaces MUL with an alternative encoding which is
  329. * backwards-compatible at the assembly level.
  330. */
  331. OPC_MUL = use_mips32r6_instructions ? OPC_MUL_R6 : OPC_MUL_R5,
  332. /* MIPS r6 introduced names for weaker variants of SYNC. These are
  333. backward compatible to previous architecture revisions. */
  334. OPC_SYNC_WMB = OPC_SYNC | 0x04 << 6,
  335. OPC_SYNC_MB = OPC_SYNC | 0x10 << 6,
  336. OPC_SYNC_ACQUIRE = OPC_SYNC | 0x11 << 6,
  337. OPC_SYNC_RELEASE = OPC_SYNC | 0x12 << 6,
  338. OPC_SYNC_RMB = OPC_SYNC | 0x13 << 6,
  339. /* Aliases for convenience. */
  340. ALIAS_PADD = sizeof(void *) == 4 ? OPC_ADDU : OPC_DADDU,
  341. ALIAS_PADDI = sizeof(void *) == 4 ? OPC_ADDIU : OPC_DADDIU,
  342. ALIAS_TSRL = TARGET_LONG_BITS == 32 || TCG_TARGET_REG_BITS == 32
  343. ? OPC_SRL : OPC_DSRL,
  344. } MIPSInsn;
  345. /*
  346. * Type reg
  347. */
  348. static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc,
  349. TCGReg rd, TCGReg rs, TCGReg rt)
  350. {
  351. int32_t inst;
  352. inst = opc;
  353. inst |= (rs & 0x1F) << 21;
  354. inst |= (rt & 0x1F) << 16;
  355. inst |= (rd & 0x1F) << 11;
  356. tcg_out32(s, inst);
  357. }
  358. /*
  359. * Type immediate
  360. */
  361. static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc,
  362. TCGReg rt, TCGReg rs, TCGArg imm)
  363. {
  364. int32_t inst;
  365. inst = opc;
  366. inst |= (rs & 0x1F) << 21;
  367. inst |= (rt & 0x1F) << 16;
  368. inst |= (imm & 0xffff);
  369. tcg_out32(s, inst);
  370. }
  371. /*
  372. * Type bitfield
  373. */
  374. static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt,
  375. TCGReg rs, int msb, int lsb)
  376. {
  377. int32_t inst;
  378. inst = opc;
  379. inst |= (rs & 0x1F) << 21;
  380. inst |= (rt & 0x1F) << 16;
  381. inst |= (msb & 0x1F) << 11;
  382. inst |= (lsb & 0x1F) << 6;
  383. tcg_out32(s, inst);
  384. }
  385. static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm,
  386. MIPSInsn oph, TCGReg rt, TCGReg rs,
  387. int msb, int lsb)
  388. {
  389. if (lsb >= 32) {
  390. opc = oph;
  391. msb -= 32;
  392. lsb -= 32;
  393. } else if (msb >= 32) {
  394. opc = opm;
  395. msb -= 32;
  396. }
  397. tcg_out_opc_bf(s, opc, rt, rs, msb, lsb);
  398. }
  399. /*
  400. * Type branch
  401. */
  402. static void tcg_out_opc_br(TCGContext *s, MIPSInsn opc, TCGReg rt, TCGReg rs)
  403. {
  404. tcg_out_opc_imm(s, opc, rt, rs, 0);
  405. }
  406. /*
  407. * Type sa
  408. */
  409. static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc,
  410. TCGReg rd, TCGReg rt, TCGArg sa)
  411. {
  412. int32_t inst;
  413. inst = opc;
  414. inst |= (rt & 0x1F) << 16;
  415. inst |= (rd & 0x1F) << 11;
  416. inst |= (sa & 0x1F) << 6;
  417. tcg_out32(s, inst);
  418. }
  419. static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2,
  420. TCGReg rd, TCGReg rt, TCGArg sa)
  421. {
  422. int32_t inst;
  423. inst = (sa & 32 ? opc2 : opc1);
  424. inst |= (rt & 0x1F) << 16;
  425. inst |= (rd & 0x1F) << 11;
  426. inst |= (sa & 0x1F) << 6;
  427. tcg_out32(s, inst);
  428. }
  429. /*
  430. * Type jump.
  431. * Returns true if the branch was in range and the insn was emitted.
  432. */
  433. static bool tcg_out_opc_jmp(TCGContext *s, MIPSInsn opc, const void *target)
  434. {
  435. uintptr_t dest = (uintptr_t)target;
  436. uintptr_t from = (uintptr_t)tcg_splitwx_to_rx(s->code_ptr) + 4;
  437. int32_t inst;
  438. /* The pc-region branch happens within the 256MB region of
  439. the delay slot (thus the +4). */
  440. if ((from ^ dest) & -(1 << 28)) {
  441. return false;
  442. }
  443. tcg_debug_assert((dest & 3) == 0);
  444. inst = opc;
  445. inst |= (dest >> 2) & 0x3ffffff;
  446. tcg_out32(s, inst);
  447. return true;
  448. }
  449. static void tcg_out_nop(TCGContext *s)
  450. {
  451. tcg_out32(s, 0);
  452. }
  453. static void tcg_out_dsll(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
  454. {
  455. tcg_out_opc_sa64(s, OPC_DSLL, OPC_DSLL32, rd, rt, sa);
  456. }
  457. static void tcg_out_dsrl(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
  458. {
  459. tcg_out_opc_sa64(s, OPC_DSRL, OPC_DSRL32, rd, rt, sa);
  460. }
  461. static void tcg_out_dsra(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
  462. {
  463. tcg_out_opc_sa64(s, OPC_DSRA, OPC_DSRA32, rd, rt, sa);
  464. }
  465. static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
  466. {
  467. /* Simple reg-reg move, optimising out the 'do nothing' case */
  468. if (ret != arg) {
  469. tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO);
  470. }
  471. return true;
  472. }
  473. static void tcg_out_movi(TCGContext *s, TCGType type,
  474. TCGReg ret, tcg_target_long arg)
  475. {
  476. if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
  477. arg = (int32_t)arg;
  478. }
  479. if (arg == (int16_t)arg) {
  480. tcg_out_opc_imm(s, OPC_ADDIU, ret, TCG_REG_ZERO, arg);
  481. return;
  482. }
  483. if (arg == (uint16_t)arg) {
  484. tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg);
  485. return;
  486. }
  487. if (TCG_TARGET_REG_BITS == 32 || arg == (int32_t)arg) {
  488. tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
  489. } else {
  490. tcg_out_movi(s, TCG_TYPE_I32, ret, arg >> 31 >> 1);
  491. if (arg & 0xffff0000ull) {
  492. tcg_out_dsll(s, ret, ret, 16);
  493. tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg >> 16);
  494. tcg_out_dsll(s, ret, ret, 16);
  495. } else {
  496. tcg_out_dsll(s, ret, ret, 32);
  497. }
  498. }
  499. if (arg & 0xffff) {
  500. tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff);
  501. }
  502. }
  503. static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
  504. tcg_target_long imm)
  505. {
  506. /* This function is only used for passing structs by reference. */
  507. g_assert_not_reached();
  508. }
  509. static void tcg_out_bswap16(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
  510. {
  511. /* ret and arg can't be register tmp0 */
  512. tcg_debug_assert(ret != TCG_TMP0);
  513. tcg_debug_assert(arg != TCG_TMP0);
  514. /* With arg = abcd: */
  515. if (use_mips32r2_instructions) {
  516. tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg); /* badc */
  517. if (flags & TCG_BSWAP_OS) {
  518. tcg_out_opc_reg(s, OPC_SEH, ret, 0, ret); /* ssdc */
  519. } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
  520. tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xffff); /* 00dc */
  521. }
  522. return;
  523. }
  524. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, arg, 8); /* 0abc */
  525. if (!(flags & TCG_BSWAP_IZ)) {
  526. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, TCG_TMP0, 0x00ff); /* 000c */
  527. }
  528. if (flags & TCG_BSWAP_OS) {
  529. tcg_out_opc_sa(s, OPC_SLL, ret, arg, 24); /* d000 */
  530. tcg_out_opc_sa(s, OPC_SRA, ret, ret, 16); /* ssd0 */
  531. } else {
  532. tcg_out_opc_sa(s, OPC_SLL, ret, arg, 8); /* bcd0 */
  533. if (flags & TCG_BSWAP_OZ) {
  534. tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xff00); /* 00d0 */
  535. }
  536. }
  537. tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP0); /* ssdc */
  538. }
  539. static void tcg_out_bswap_subr(TCGContext *s, const tcg_insn_unit *sub)
  540. {
  541. if (!tcg_out_opc_jmp(s, OPC_JAL, sub)) {
  542. tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP1, (uintptr_t)sub);
  543. tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_TMP1, 0);
  544. }
  545. }
  546. static void tcg_out_bswap32(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
  547. {
  548. if (use_mips32r2_instructions) {
  549. tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg);
  550. tcg_out_opc_sa(s, OPC_ROTR, ret, ret, 16);
  551. if (flags & TCG_BSWAP_OZ) {
  552. tcg_out_opc_bf(s, OPC_DEXT, ret, ret, 31, 0);
  553. }
  554. } else {
  555. if (flags & TCG_BSWAP_OZ) {
  556. tcg_out_bswap_subr(s, bswap32u_addr);
  557. } else {
  558. tcg_out_bswap_subr(s, bswap32_addr);
  559. }
  560. /* delay slot -- never omit the insn, like tcg_out_mov might. */
  561. tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
  562. tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
  563. }
  564. }
  565. static void tcg_out_bswap64(TCGContext *s, TCGReg ret, TCGReg arg)
  566. {
  567. if (use_mips32r2_instructions) {
  568. tcg_out_opc_reg(s, OPC_DSBH, ret, 0, arg);
  569. tcg_out_opc_reg(s, OPC_DSHD, ret, 0, ret);
  570. } else {
  571. tcg_out_bswap_subr(s, bswap64_addr);
  572. /* delay slot -- never omit the insn, like tcg_out_mov might. */
  573. tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
  574. tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
  575. }
  576. }
  577. static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
  578. {
  579. if (use_mips32r2_instructions) {
  580. tcg_out_opc_bf(s, OPC_DEXT, ret, arg, 31, 0);
  581. } else {
  582. tcg_out_dsll(s, ret, arg, 32);
  583. tcg_out_dsrl(s, ret, ret, 32);
  584. }
  585. }
  586. static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data,
  587. TCGReg addr, intptr_t ofs)
  588. {
  589. int16_t lo = ofs;
  590. if (ofs != lo) {
  591. tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - lo);
  592. if (addr != TCG_REG_ZERO) {
  593. tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP0, TCG_TMP0, addr);
  594. }
  595. addr = TCG_TMP0;
  596. }
  597. tcg_out_opc_imm(s, opc, data, addr, lo);
  598. }
  599. static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
  600. TCGReg arg1, intptr_t arg2)
  601. {
  602. MIPSInsn opc = OPC_LD;
  603. if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
  604. opc = OPC_LW;
  605. }
  606. tcg_out_ldst(s, opc, arg, arg1, arg2);
  607. }
  608. static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
  609. TCGReg arg1, intptr_t arg2)
  610. {
  611. MIPSInsn opc = OPC_SD;
  612. if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
  613. opc = OPC_SW;
  614. }
  615. tcg_out_ldst(s, opc, arg, arg1, arg2);
  616. }
  617. static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
  618. TCGReg base, intptr_t ofs)
  619. {
  620. if (val == 0) {
  621. tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
  622. return true;
  623. }
  624. return false;
  625. }
  626. static void tcg_out_addsub2(TCGContext *s, TCGReg rl, TCGReg rh, TCGReg al,
  627. TCGReg ah, TCGArg bl, TCGArg bh, bool cbl,
  628. bool cbh, bool is_sub)
  629. {
  630. TCGReg th = TCG_TMP1;
  631. /* If we have a negative constant such that negating it would
  632. make the high part zero, we can (usually) eliminate one insn. */
  633. if (cbl && cbh && bh == -1 && bl != 0) {
  634. bl = -bl;
  635. bh = 0;
  636. is_sub = !is_sub;
  637. }
  638. /* By operating on the high part first, we get to use the final
  639. carry operation to move back from the temporary. */
  640. if (!cbh) {
  641. tcg_out_opc_reg(s, (is_sub ? OPC_SUBU : OPC_ADDU), th, ah, bh);
  642. } else if (bh != 0 || ah == rl) {
  643. tcg_out_opc_imm(s, OPC_ADDIU, th, ah, (is_sub ? -bh : bh));
  644. } else {
  645. th = ah;
  646. }
  647. /* Note that tcg optimization should eliminate the bl == 0 case. */
  648. if (is_sub) {
  649. if (cbl) {
  650. tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, al, bl);
  651. tcg_out_opc_imm(s, OPC_ADDIU, rl, al, -bl);
  652. } else {
  653. tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, al, bl);
  654. tcg_out_opc_reg(s, OPC_SUBU, rl, al, bl);
  655. }
  656. tcg_out_opc_reg(s, OPC_SUBU, rh, th, TCG_TMP0);
  657. } else {
  658. if (cbl) {
  659. tcg_out_opc_imm(s, OPC_ADDIU, rl, al, bl);
  660. tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, rl, bl);
  661. } else if (rl == al && rl == bl) {
  662. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, al, TCG_TARGET_REG_BITS - 1);
  663. tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
  664. } else {
  665. tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
  666. tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, rl, (rl == bl ? al : bl));
  667. }
  668. tcg_out_opc_reg(s, OPC_ADDU, rh, th, TCG_TMP0);
  669. }
  670. }
  671. /* Bit 0 set if inversion required; bit 1 set if swapping required. */
  672. #define MIPS_CMP_INV 1
  673. #define MIPS_CMP_SWAP 2
  674. static const uint8_t mips_cmp_map[16] = {
  675. [TCG_COND_LT] = 0,
  676. [TCG_COND_LTU] = 0,
  677. [TCG_COND_GE] = MIPS_CMP_INV,
  678. [TCG_COND_GEU] = MIPS_CMP_INV,
  679. [TCG_COND_LE] = MIPS_CMP_INV | MIPS_CMP_SWAP,
  680. [TCG_COND_LEU] = MIPS_CMP_INV | MIPS_CMP_SWAP,
  681. [TCG_COND_GT] = MIPS_CMP_SWAP,
  682. [TCG_COND_GTU] = MIPS_CMP_SWAP,
  683. };
  684. static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
  685. TCGReg arg1, TCGReg arg2)
  686. {
  687. MIPSInsn s_opc = OPC_SLTU;
  688. int cmp_map;
  689. switch (cond) {
  690. case TCG_COND_EQ:
  691. if (arg2 != 0) {
  692. tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
  693. arg1 = ret;
  694. }
  695. tcg_out_opc_imm(s, OPC_SLTIU, ret, arg1, 1);
  696. break;
  697. case TCG_COND_NE:
  698. if (arg2 != 0) {
  699. tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
  700. arg1 = ret;
  701. }
  702. tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, arg1);
  703. break;
  704. case TCG_COND_LT:
  705. case TCG_COND_GE:
  706. case TCG_COND_LE:
  707. case TCG_COND_GT:
  708. s_opc = OPC_SLT;
  709. /* FALLTHRU */
  710. case TCG_COND_LTU:
  711. case TCG_COND_GEU:
  712. case TCG_COND_LEU:
  713. case TCG_COND_GTU:
  714. cmp_map = mips_cmp_map[cond];
  715. if (cmp_map & MIPS_CMP_SWAP) {
  716. TCGReg t = arg1;
  717. arg1 = arg2;
  718. arg2 = t;
  719. }
  720. tcg_out_opc_reg(s, s_opc, ret, arg1, arg2);
  721. if (cmp_map & MIPS_CMP_INV) {
  722. tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
  723. }
  724. break;
  725. default:
  726. tcg_abort();
  727. break;
  728. }
  729. }
  730. static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
  731. TCGReg arg2, TCGLabel *l)
  732. {
  733. static const MIPSInsn b_zero[16] = {
  734. [TCG_COND_LT] = OPC_BLTZ,
  735. [TCG_COND_GT] = OPC_BGTZ,
  736. [TCG_COND_LE] = OPC_BLEZ,
  737. [TCG_COND_GE] = OPC_BGEZ,
  738. };
  739. MIPSInsn s_opc = OPC_SLTU;
  740. MIPSInsn b_opc;
  741. int cmp_map;
  742. switch (cond) {
  743. case TCG_COND_EQ:
  744. b_opc = OPC_BEQ;
  745. break;
  746. case TCG_COND_NE:
  747. b_opc = OPC_BNE;
  748. break;
  749. case TCG_COND_LT:
  750. case TCG_COND_GT:
  751. case TCG_COND_LE:
  752. case TCG_COND_GE:
  753. if (arg2 == 0) {
  754. b_opc = b_zero[cond];
  755. arg2 = arg1;
  756. arg1 = 0;
  757. break;
  758. }
  759. s_opc = OPC_SLT;
  760. /* FALLTHRU */
  761. case TCG_COND_LTU:
  762. case TCG_COND_GTU:
  763. case TCG_COND_LEU:
  764. case TCG_COND_GEU:
  765. cmp_map = mips_cmp_map[cond];
  766. if (cmp_map & MIPS_CMP_SWAP) {
  767. TCGReg t = arg1;
  768. arg1 = arg2;
  769. arg2 = t;
  770. }
  771. tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);
  772. b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);
  773. arg1 = TCG_TMP0;
  774. arg2 = TCG_REG_ZERO;
  775. break;
  776. default:
  777. tcg_abort();
  778. break;
  779. }
  780. tcg_out_opc_br(s, b_opc, arg1, arg2);
  781. tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, l, 0);
  782. tcg_out_nop(s);
  783. }
  784. static TCGReg tcg_out_reduce_eq2(TCGContext *s, TCGReg tmp0, TCGReg tmp1,
  785. TCGReg al, TCGReg ah,
  786. TCGReg bl, TCGReg bh)
  787. {
  788. /* Merge highpart comparison into AH. */
  789. if (bh != 0) {
  790. if (ah != 0) {
  791. tcg_out_opc_reg(s, OPC_XOR, tmp0, ah, bh);
  792. ah = tmp0;
  793. } else {
  794. ah = bh;
  795. }
  796. }
  797. /* Merge lowpart comparison into AL. */
  798. if (bl != 0) {
  799. if (al != 0) {
  800. tcg_out_opc_reg(s, OPC_XOR, tmp1, al, bl);
  801. al = tmp1;
  802. } else {
  803. al = bl;
  804. }
  805. }
  806. /* Merge high and low part comparisons into AL. */
  807. if (ah != 0) {
  808. if (al != 0) {
  809. tcg_out_opc_reg(s, OPC_OR, tmp0, ah, al);
  810. al = tmp0;
  811. } else {
  812. al = ah;
  813. }
  814. }
  815. return al;
  816. }
  817. static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
  818. TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
  819. {
  820. TCGReg tmp0 = TCG_TMP0;
  821. TCGReg tmp1 = ret;
  822. tcg_debug_assert(ret != TCG_TMP0);
  823. if (ret == ah || ret == bh) {
  824. tcg_debug_assert(ret != TCG_TMP1);
  825. tmp1 = TCG_TMP1;
  826. }
  827. switch (cond) {
  828. case TCG_COND_EQ:
  829. case TCG_COND_NE:
  830. tmp1 = tcg_out_reduce_eq2(s, tmp0, tmp1, al, ah, bl, bh);
  831. tcg_out_setcond(s, cond, ret, tmp1, TCG_REG_ZERO);
  832. break;
  833. default:
  834. tcg_out_setcond(s, TCG_COND_EQ, tmp0, ah, bh);
  835. tcg_out_setcond(s, tcg_unsigned_cond(cond), tmp1, al, bl);
  836. tcg_out_opc_reg(s, OPC_AND, tmp1, tmp1, tmp0);
  837. tcg_out_setcond(s, tcg_high_cond(cond), tmp0, ah, bh);
  838. tcg_out_opc_reg(s, OPC_OR, ret, tmp1, tmp0);
  839. break;
  840. }
  841. }
  842. static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
  843. TCGReg bl, TCGReg bh, TCGLabel *l)
  844. {
  845. TCGCond b_cond = TCG_COND_NE;
  846. TCGReg tmp = TCG_TMP1;
  847. /* With branches, we emit between 4 and 9 insns with 2 or 3 branches.
  848. With setcond, we emit between 3 and 10 insns and only 1 branch,
  849. which ought to get better branch prediction. */
  850. switch (cond) {
  851. case TCG_COND_EQ:
  852. case TCG_COND_NE:
  853. b_cond = cond;
  854. tmp = tcg_out_reduce_eq2(s, TCG_TMP0, TCG_TMP1, al, ah, bl, bh);
  855. break;
  856. default:
  857. /* Minimize code size by preferring a compare not requiring INV. */
  858. if (mips_cmp_map[cond] & MIPS_CMP_INV) {
  859. cond = tcg_invert_cond(cond);
  860. b_cond = TCG_COND_EQ;
  861. }
  862. tcg_out_setcond2(s, cond, tmp, al, ah, bl, bh);
  863. break;
  864. }
  865. tcg_out_brcond(s, b_cond, tmp, TCG_REG_ZERO, l);
  866. }
  867. static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
  868. TCGReg c1, TCGReg c2, TCGReg v1, TCGReg v2)
  869. {
  870. bool eqz = false;
  871. /* If one of the values is zero, put it last to match SEL*Z instructions */
  872. if (use_mips32r6_instructions && v1 == 0) {
  873. v1 = v2;
  874. v2 = 0;
  875. cond = tcg_invert_cond(cond);
  876. }
  877. switch (cond) {
  878. case TCG_COND_EQ:
  879. eqz = true;
  880. /* FALLTHRU */
  881. case TCG_COND_NE:
  882. if (c2 != 0) {
  883. tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, c1, c2);
  884. c1 = TCG_TMP0;
  885. }
  886. break;
  887. default:
  888. /* Minimize code size by preferring a compare not requiring INV. */
  889. if (mips_cmp_map[cond] & MIPS_CMP_INV) {
  890. cond = tcg_invert_cond(cond);
  891. eqz = true;
  892. }
  893. tcg_out_setcond(s, cond, TCG_TMP0, c1, c2);
  894. c1 = TCG_TMP0;
  895. break;
  896. }
  897. if (use_mips32r6_instructions) {
  898. MIPSInsn m_opc_t = eqz ? OPC_SELEQZ : OPC_SELNEZ;
  899. MIPSInsn m_opc_f = eqz ? OPC_SELNEZ : OPC_SELEQZ;
  900. if (v2 != 0) {
  901. tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1);
  902. }
  903. tcg_out_opc_reg(s, m_opc_t, ret, v1, c1);
  904. if (v2 != 0) {
  905. tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1);
  906. }
  907. } else {
  908. MIPSInsn m_opc = eqz ? OPC_MOVZ : OPC_MOVN;
  909. tcg_out_opc_reg(s, m_opc, ret, v1, c1);
  910. /* This should be guaranteed via constraints */
  911. tcg_debug_assert(v2 == ret);
  912. }
  913. }
  914. static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
  915. {
  916. /* Note that the ABI requires the called function's address to be
  917. loaded into T9, even if a direct branch is in range. */
  918. tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T9, (uintptr_t)arg);
  919. /* But do try a direct branch, allowing the cpu better insn prefetch. */
  920. if (tail) {
  921. if (!tcg_out_opc_jmp(s, OPC_J, arg)) {
  922. tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0);
  923. }
  924. } else {
  925. if (!tcg_out_opc_jmp(s, OPC_JAL, arg)) {
  926. tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_REG_T9, 0);
  927. }
  928. }
  929. }
  930. static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
  931. const TCGHelperInfo *info)
  932. {
  933. tcg_out_call_int(s, arg, false);
  934. tcg_out_nop(s);
  935. }
  936. #if defined(CONFIG_SOFTMMU)
  937. static void * const qemu_ld_helpers[(MO_SSIZE | MO_BSWAP) + 1] = {
  938. [MO_UB] = helper_ret_ldub_mmu,
  939. [MO_SB] = helper_ret_ldsb_mmu,
  940. [MO_LEUW] = helper_le_lduw_mmu,
  941. [MO_LESW] = helper_le_ldsw_mmu,
  942. [MO_LEUL] = helper_le_ldul_mmu,
  943. [MO_LEUQ] = helper_le_ldq_mmu,
  944. [MO_BEUW] = helper_be_lduw_mmu,
  945. [MO_BESW] = helper_be_ldsw_mmu,
  946. [MO_BEUL] = helper_be_ldul_mmu,
  947. [MO_BEUQ] = helper_be_ldq_mmu,
  948. #if TCG_TARGET_REG_BITS == 64
  949. [MO_LESL] = helper_le_ldsl_mmu,
  950. [MO_BESL] = helper_be_ldsl_mmu,
  951. #endif
  952. };
  953. static void * const qemu_st_helpers[(MO_SIZE | MO_BSWAP) + 1] = {
  954. [MO_UB] = helper_ret_stb_mmu,
  955. [MO_LEUW] = helper_le_stw_mmu,
  956. [MO_LEUL] = helper_le_stl_mmu,
  957. [MO_LEUQ] = helper_le_stq_mmu,
  958. [MO_BEUW] = helper_be_stw_mmu,
  959. [MO_BEUL] = helper_be_stl_mmu,
  960. [MO_BEUQ] = helper_be_stq_mmu,
  961. };
  962. /* Helper routines for marshalling helper function arguments into
  963. * the correct registers and stack.
  964. * I is where we want to put this argument, and is updated and returned
  965. * for the next call. ARG is the argument itself.
  966. *
  967. * We provide routines for arguments which are: immediate, 32 bit
  968. * value in register, 16 and 8 bit values in register (which must be zero
  969. * extended before use) and 64 bit value in a lo:hi register pair.
  970. */
  971. static int tcg_out_call_iarg_reg(TCGContext *s, int i, TCGReg arg)
  972. {
  973. if (i < ARRAY_SIZE(tcg_target_call_iarg_regs)) {
  974. tcg_out_mov(s, TCG_TYPE_REG, tcg_target_call_iarg_regs[i], arg);
  975. } else {
  976. /* For N32 and N64, the initial offset is different. But there
  977. we also have 8 argument register so we don't run out here. */
  978. tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
  979. tcg_out_st(s, TCG_TYPE_REG, arg, TCG_REG_SP, 4 * i);
  980. }
  981. return i + 1;
  982. }
  983. static int tcg_out_call_iarg_reg8(TCGContext *s, int i, TCGReg arg)
  984. {
  985. TCGReg tmp = TCG_TMP0;
  986. if (i < ARRAY_SIZE(tcg_target_call_iarg_regs)) {
  987. tmp = tcg_target_call_iarg_regs[i];
  988. }
  989. tcg_out_opc_imm(s, OPC_ANDI, tmp, arg, 0xff);
  990. return tcg_out_call_iarg_reg(s, i, tmp);
  991. }
  992. static int tcg_out_call_iarg_reg16(TCGContext *s, int i, TCGReg arg)
  993. {
  994. TCGReg tmp = TCG_TMP0;
  995. if (i < ARRAY_SIZE(tcg_target_call_iarg_regs)) {
  996. tmp = tcg_target_call_iarg_regs[i];
  997. }
  998. tcg_out_opc_imm(s, OPC_ANDI, tmp, arg, 0xffff);
  999. return tcg_out_call_iarg_reg(s, i, tmp);
  1000. }
  1001. static int tcg_out_call_iarg_imm(TCGContext *s, int i, TCGArg arg)
  1002. {
  1003. TCGReg tmp = TCG_TMP0;
  1004. if (arg == 0) {
  1005. tmp = TCG_REG_ZERO;
  1006. } else {
  1007. if (i < ARRAY_SIZE(tcg_target_call_iarg_regs)) {
  1008. tmp = tcg_target_call_iarg_regs[i];
  1009. }
  1010. tcg_out_movi(s, TCG_TYPE_REG, tmp, arg);
  1011. }
  1012. return tcg_out_call_iarg_reg(s, i, tmp);
  1013. }
  1014. static int tcg_out_call_iarg_reg2(TCGContext *s, int i, TCGReg al, TCGReg ah)
  1015. {
  1016. tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
  1017. i = (i + 1) & ~1;
  1018. i = tcg_out_call_iarg_reg(s, i, (MIPS_BE ? ah : al));
  1019. i = tcg_out_call_iarg_reg(s, i, (MIPS_BE ? al : ah));
  1020. return i;
  1021. }
  1022. /* We expect to use a 16-bit negative offset from ENV. */
  1023. QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
  1024. QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -32768);
  1025. /*
  1026. * Perform the tlb comparison operation.
  1027. * The complete host address is placed in BASE.
  1028. * Clobbers TMP0, TMP1, TMP2, TMP3.
  1029. */
  1030. static void tcg_out_tlb_load(TCGContext *s, TCGReg base, TCGReg addrl,
  1031. TCGReg addrh, MemOpIdx oi,
  1032. tcg_insn_unit *label_ptr[2], bool is_load)
  1033. {
  1034. MemOp opc = get_memop(oi);
  1035. unsigned a_bits = get_alignment_bits(opc);
  1036. unsigned s_bits = opc & MO_SIZE;
  1037. unsigned a_mask = (1 << a_bits) - 1;
  1038. unsigned s_mask = (1 << s_bits) - 1;
  1039. int mem_index = get_mmuidx(oi);
  1040. int fast_off = TLB_MASK_TABLE_OFS(mem_index);
  1041. int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
  1042. int table_off = fast_off + offsetof(CPUTLBDescFast, table);
  1043. int add_off = offsetof(CPUTLBEntry, addend);
  1044. int cmp_off = (is_load ? offsetof(CPUTLBEntry, addr_read)
  1045. : offsetof(CPUTLBEntry, addr_write));
  1046. target_ulong tlb_mask;
  1047. /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx]. */
  1048. tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_AREG0, mask_off);
  1049. tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP1, TCG_AREG0, table_off);
  1050. /* Extract the TLB index from the address into TMP3. */
  1051. tcg_out_opc_sa(s, ALIAS_TSRL, TCG_TMP3, addrl,
  1052. TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
  1053. tcg_out_opc_reg(s, OPC_AND, TCG_TMP3, TCG_TMP3, TCG_TMP0);
  1054. /* Add the tlb_table pointer, creating the CPUTLBEntry address in TMP3. */
  1055. tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  1056. /* Load the (low-half) tlb comparator. */
  1057. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1058. tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + LO_OFF);
  1059. } else {
  1060. tcg_out_ldst(s, (TARGET_LONG_BITS == 64 ? OPC_LD
  1061. : TCG_TARGET_REG_BITS == 64 ? OPC_LWU : OPC_LW),
  1062. TCG_TMP0, TCG_TMP3, cmp_off);
  1063. }
  1064. /* Zero extend a 32-bit guest address for a 64-bit host. */
  1065. if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
  1066. tcg_out_ext32u(s, base, addrl);
  1067. addrl = base;
  1068. }
  1069. /*
  1070. * Mask the page bits, keeping the alignment bits to compare against.
  1071. * For unaligned accesses, compare against the end of the access to
  1072. * verify that it does not cross a page boundary.
  1073. */
  1074. tlb_mask = (target_ulong)TARGET_PAGE_MASK | a_mask;
  1075. tcg_out_movi(s, TCG_TYPE_I32, TCG_TMP1, tlb_mask);
  1076. if (a_mask >= s_mask) {
  1077. tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addrl);
  1078. } else {
  1079. tcg_out_opc_imm(s, ALIAS_PADDI, TCG_TMP2, addrl, s_mask - a_mask);
  1080. tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2);
  1081. }
  1082. if (TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
  1083. /* Load the tlb addend for the fast path. */
  1084. tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP2, TCG_TMP3, add_off);
  1085. }
  1086. label_ptr[0] = s->code_ptr;
  1087. tcg_out_opc_br(s, OPC_BNE, TCG_TMP1, TCG_TMP0);
  1088. /* Load and test the high half tlb comparator. */
  1089. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1090. /* delay slot */
  1091. tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + HI_OFF);
  1092. /* Load the tlb addend for the fast path. */
  1093. tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP2, TCG_TMP3, add_off);
  1094. label_ptr[1] = s->code_ptr;
  1095. tcg_out_opc_br(s, OPC_BNE, addrh, TCG_TMP0);
  1096. }
  1097. /* delay slot */
  1098. tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP2, addrl);
  1099. }
  1100. static void add_qemu_ldst_label(TCGContext *s, int is_ld, MemOpIdx oi,
  1101. TCGType ext,
  1102. TCGReg datalo, TCGReg datahi,
  1103. TCGReg addrlo, TCGReg addrhi,
  1104. void *raddr, tcg_insn_unit *label_ptr[2])
  1105. {
  1106. TCGLabelQemuLdst *label = new_ldst_label(s);
  1107. label->is_ld = is_ld;
  1108. label->oi = oi;
  1109. label->type = ext;
  1110. label->datalo_reg = datalo;
  1111. label->datahi_reg = datahi;
  1112. label->addrlo_reg = addrlo;
  1113. label->addrhi_reg = addrhi;
  1114. label->raddr = tcg_splitwx_to_rx(raddr);
  1115. label->label_ptr[0] = label_ptr[0];
  1116. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1117. label->label_ptr[1] = label_ptr[1];
  1118. }
  1119. }
  1120. static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
  1121. {
  1122. const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
  1123. MemOpIdx oi = l->oi;
  1124. MemOp opc = get_memop(oi);
  1125. TCGReg v0;
  1126. int i;
  1127. /* resolve label address */
  1128. if (!reloc_pc16(l->label_ptr[0], tgt_rx)
  1129. || (TCG_TARGET_REG_BITS < TARGET_LONG_BITS
  1130. && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
  1131. return false;
  1132. }
  1133. i = 1;
  1134. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1135. i = tcg_out_call_iarg_reg2(s, i, l->addrlo_reg, l->addrhi_reg);
  1136. } else {
  1137. i = tcg_out_call_iarg_reg(s, i, l->addrlo_reg);
  1138. }
  1139. i = tcg_out_call_iarg_imm(s, i, oi);
  1140. i = tcg_out_call_iarg_imm(s, i, (intptr_t)l->raddr);
  1141. tcg_out_call_int(s, qemu_ld_helpers[opc & (MO_BSWAP | MO_SSIZE)], false);
  1142. /* delay slot */
  1143. tcg_out_mov(s, TCG_TYPE_PTR, tcg_target_call_iarg_regs[0], TCG_AREG0);
  1144. v0 = l->datalo_reg;
  1145. if (TCG_TARGET_REG_BITS == 32 && (opc & MO_SIZE) == MO_64) {
  1146. /* We eliminated V0 from the possible output registers, so it
  1147. cannot be clobbered here. So we must move V1 first. */
  1148. if (MIPS_BE) {
  1149. tcg_out_mov(s, TCG_TYPE_I32, v0, TCG_REG_V1);
  1150. v0 = l->datahi_reg;
  1151. } else {
  1152. tcg_out_mov(s, TCG_TYPE_I32, l->datahi_reg, TCG_REG_V1);
  1153. }
  1154. }
  1155. tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
  1156. if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
  1157. return false;
  1158. }
  1159. /* delay slot */
  1160. if (TCG_TARGET_REG_BITS == 64 && l->type == TCG_TYPE_I32) {
  1161. /* we always sign-extend 32-bit loads */
  1162. tcg_out_opc_sa(s, OPC_SLL, v0, TCG_REG_V0, 0);
  1163. } else {
  1164. tcg_out_opc_reg(s, OPC_OR, v0, TCG_REG_V0, TCG_REG_ZERO);
  1165. }
  1166. return true;
  1167. }
  1168. static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
  1169. {
  1170. const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
  1171. MemOpIdx oi = l->oi;
  1172. MemOp opc = get_memop(oi);
  1173. MemOp s_bits = opc & MO_SIZE;
  1174. int i;
  1175. /* resolve label address */
  1176. if (!reloc_pc16(l->label_ptr[0], tgt_rx)
  1177. || (TCG_TARGET_REG_BITS < TARGET_LONG_BITS
  1178. && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
  1179. return false;
  1180. }
  1181. i = 1;
  1182. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1183. i = tcg_out_call_iarg_reg2(s, i, l->addrlo_reg, l->addrhi_reg);
  1184. } else {
  1185. i = tcg_out_call_iarg_reg(s, i, l->addrlo_reg);
  1186. }
  1187. switch (s_bits) {
  1188. case MO_8:
  1189. i = tcg_out_call_iarg_reg8(s, i, l->datalo_reg);
  1190. break;
  1191. case MO_16:
  1192. i = tcg_out_call_iarg_reg16(s, i, l->datalo_reg);
  1193. break;
  1194. case MO_32:
  1195. i = tcg_out_call_iarg_reg(s, i, l->datalo_reg);
  1196. break;
  1197. case MO_64:
  1198. if (TCG_TARGET_REG_BITS == 32) {
  1199. i = tcg_out_call_iarg_reg2(s, i, l->datalo_reg, l->datahi_reg);
  1200. } else {
  1201. i = tcg_out_call_iarg_reg(s, i, l->datalo_reg);
  1202. }
  1203. break;
  1204. default:
  1205. tcg_abort();
  1206. }
  1207. i = tcg_out_call_iarg_imm(s, i, oi);
  1208. /* Tail call to the store helper. Thus force the return address
  1209. computation to take place in the return address register. */
  1210. tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RA, (intptr_t)l->raddr);
  1211. i = tcg_out_call_iarg_reg(s, i, TCG_REG_RA);
  1212. tcg_out_call_int(s, qemu_st_helpers[opc & (MO_BSWAP | MO_SIZE)], true);
  1213. /* delay slot */
  1214. tcg_out_mov(s, TCG_TYPE_PTR, tcg_target_call_iarg_regs[0], TCG_AREG0);
  1215. return true;
  1216. }
  1217. #else
  1218. static void tcg_out_test_alignment(TCGContext *s, bool is_ld, TCGReg addrlo,
  1219. TCGReg addrhi, unsigned a_bits)
  1220. {
  1221. unsigned a_mask = (1 << a_bits) - 1;
  1222. TCGLabelQemuLdst *l = new_ldst_label(s);
  1223. l->is_ld = is_ld;
  1224. l->addrlo_reg = addrlo;
  1225. l->addrhi_reg = addrhi;
  1226. /* We are expecting a_bits to max out at 7, much lower than ANDI. */
  1227. tcg_debug_assert(a_bits < 16);
  1228. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, addrlo, a_mask);
  1229. l->label_ptr[0] = s->code_ptr;
  1230. if (use_mips32r6_instructions) {
  1231. tcg_out_opc_br(s, OPC_BNEZALC_R6, TCG_REG_ZERO, TCG_TMP0);
  1232. } else {
  1233. tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO);
  1234. tcg_out_nop(s);
  1235. }
  1236. l->raddr = tcg_splitwx_to_rx(s->code_ptr);
  1237. }
  1238. static bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
  1239. {
  1240. void *target;
  1241. if (!reloc_pc16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
  1242. return false;
  1243. }
  1244. if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
  1245. /* A0 is env, A1 is skipped, A2:A3 is the uint64_t address. */
  1246. TCGReg a2 = MIPS_BE ? l->addrhi_reg : l->addrlo_reg;
  1247. TCGReg a3 = MIPS_BE ? l->addrlo_reg : l->addrhi_reg;
  1248. if (a3 != TCG_REG_A2) {
  1249. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, a2);
  1250. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, a3);
  1251. } else if (a2 != TCG_REG_A3) {
  1252. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, a3);
  1253. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, a2);
  1254. } else {
  1255. tcg_out_mov(s, TCG_TYPE_I32, TCG_TMP0, TCG_REG_A2);
  1256. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, TCG_REG_A3);
  1257. tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, TCG_TMP0);
  1258. }
  1259. } else {
  1260. tcg_out_mov(s, TCG_TYPE_TL, TCG_REG_A1, l->addrlo_reg);
  1261. }
  1262. tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
  1263. /*
  1264. * Tail call to the helper, with the return address back inline.
  1265. * We have arrived here via BNEL, so $31 is already set.
  1266. */
  1267. target = (l->is_ld ? helper_unaligned_ld : helper_unaligned_st);
  1268. tcg_out_call_int(s, target, true);
  1269. return true;
  1270. }
  1271. static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
  1272. {
  1273. return tcg_out_fail_alignment(s, l);
  1274. }
  1275. static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
  1276. {
  1277. return tcg_out_fail_alignment(s, l);
  1278. }
  1279. #endif /* SOFTMMU */
  1280. static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
  1281. TCGReg base, MemOp opc, bool is_64)
  1282. {
  1283. switch (opc & (MO_SSIZE | MO_BSWAP)) {
  1284. case MO_UB:
  1285. tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
  1286. break;
  1287. case MO_SB:
  1288. tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
  1289. break;
  1290. case MO_UW | MO_BSWAP:
  1291. tcg_out_opc_imm(s, OPC_LHU, TCG_TMP1, base, 0);
  1292. tcg_out_bswap16(s, lo, TCG_TMP1, TCG_BSWAP_IZ | TCG_BSWAP_OZ);
  1293. break;
  1294. case MO_UW:
  1295. tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
  1296. break;
  1297. case MO_SW | MO_BSWAP:
  1298. tcg_out_opc_imm(s, OPC_LHU, TCG_TMP1, base, 0);
  1299. tcg_out_bswap16(s, lo, TCG_TMP1, TCG_BSWAP_IZ | TCG_BSWAP_OS);
  1300. break;
  1301. case MO_SW:
  1302. tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
  1303. break;
  1304. case MO_UL | MO_BSWAP:
  1305. if (TCG_TARGET_REG_BITS == 64 && is_64) {
  1306. if (use_mips32r2_instructions) {
  1307. tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
  1308. tcg_out_bswap32(s, lo, lo, TCG_BSWAP_IZ | TCG_BSWAP_OZ);
  1309. } else {
  1310. tcg_out_bswap_subr(s, bswap32u_addr);
  1311. /* delay slot */
  1312. tcg_out_opc_imm(s, OPC_LWU, TCG_TMP0, base, 0);
  1313. tcg_out_mov(s, TCG_TYPE_I64, lo, TCG_TMP3);
  1314. }
  1315. break;
  1316. }
  1317. /* FALLTHRU */
  1318. case MO_SL | MO_BSWAP:
  1319. if (use_mips32r2_instructions) {
  1320. tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
  1321. tcg_out_bswap32(s, lo, lo, 0);
  1322. } else {
  1323. tcg_out_bswap_subr(s, bswap32_addr);
  1324. /* delay slot */
  1325. tcg_out_opc_imm(s, OPC_LW, TCG_TMP0, base, 0);
  1326. tcg_out_mov(s, TCG_TYPE_I32, lo, TCG_TMP3);
  1327. }
  1328. break;
  1329. case MO_UL:
  1330. if (TCG_TARGET_REG_BITS == 64 && is_64) {
  1331. tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
  1332. break;
  1333. }
  1334. /* FALLTHRU */
  1335. case MO_SL:
  1336. tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
  1337. break;
  1338. case MO_UQ | MO_BSWAP:
  1339. if (TCG_TARGET_REG_BITS == 64) {
  1340. if (use_mips32r2_instructions) {
  1341. tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
  1342. tcg_out_bswap64(s, lo, lo);
  1343. } else {
  1344. tcg_out_bswap_subr(s, bswap64_addr);
  1345. /* delay slot */
  1346. tcg_out_opc_imm(s, OPC_LD, TCG_TMP0, base, 0);
  1347. tcg_out_mov(s, TCG_TYPE_I64, lo, TCG_TMP3);
  1348. }
  1349. } else if (use_mips32r2_instructions) {
  1350. tcg_out_opc_imm(s, OPC_LW, TCG_TMP0, base, 0);
  1351. tcg_out_opc_imm(s, OPC_LW, TCG_TMP1, base, 4);
  1352. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP0, 0, TCG_TMP0);
  1353. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP1, 0, TCG_TMP1);
  1354. tcg_out_opc_sa(s, OPC_ROTR, MIPS_BE ? lo : hi, TCG_TMP0, 16);
  1355. tcg_out_opc_sa(s, OPC_ROTR, MIPS_BE ? hi : lo, TCG_TMP1, 16);
  1356. } else {
  1357. tcg_out_bswap_subr(s, bswap32_addr);
  1358. /* delay slot */
  1359. tcg_out_opc_imm(s, OPC_LW, TCG_TMP0, base, 0);
  1360. tcg_out_opc_imm(s, OPC_LW, TCG_TMP0, base, 4);
  1361. tcg_out_bswap_subr(s, bswap32_addr);
  1362. /* delay slot */
  1363. tcg_out_mov(s, TCG_TYPE_I32, MIPS_BE ? lo : hi, TCG_TMP3);
  1364. tcg_out_mov(s, TCG_TYPE_I32, MIPS_BE ? hi : lo, TCG_TMP3);
  1365. }
  1366. break;
  1367. case MO_UQ:
  1368. /* Prefer to load from offset 0 first, but allow for overlap. */
  1369. if (TCG_TARGET_REG_BITS == 64) {
  1370. tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
  1371. } else if (MIPS_BE ? hi != base : lo == base) {
  1372. tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
  1373. tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
  1374. } else {
  1375. tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
  1376. tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
  1377. }
  1378. break;
  1379. default:
  1380. tcg_abort();
  1381. }
  1382. }
  1383. static void tcg_out_qemu_ld_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
  1384. TCGReg base, MemOp opc, bool is_64)
  1385. {
  1386. const MIPSInsn lw1 = MIPS_BE ? OPC_LWL : OPC_LWR;
  1387. const MIPSInsn lw2 = MIPS_BE ? OPC_LWR : OPC_LWL;
  1388. const MIPSInsn ld1 = MIPS_BE ? OPC_LDL : OPC_LDR;
  1389. const MIPSInsn ld2 = MIPS_BE ? OPC_LDR : OPC_LDL;
  1390. bool sgn = (opc & MO_SIGN);
  1391. switch (opc & (MO_SSIZE | MO_BSWAP)) {
  1392. case MO_SW | MO_BE:
  1393. case MO_UW | MO_BE:
  1394. tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 0);
  1395. tcg_out_opc_imm(s, OPC_LBU, lo, base, 1);
  1396. if (use_mips32r2_instructions) {
  1397. tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
  1398. } else {
  1399. tcg_out_opc_sa(s, OPC_SLL, TCG_TMP0, TCG_TMP0, 8);
  1400. tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1);
  1401. }
  1402. break;
  1403. case MO_SW | MO_LE:
  1404. case MO_UW | MO_LE:
  1405. if (use_mips32r2_instructions && lo != base) {
  1406. tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
  1407. tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 1);
  1408. tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
  1409. } else {
  1410. tcg_out_opc_imm(s, OPC_LBU, TCG_TMP0, base, 0);
  1411. tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP1, base, 1);
  1412. tcg_out_opc_sa(s, OPC_SLL, TCG_TMP1, TCG_TMP1, 8);
  1413. tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1);
  1414. }
  1415. break;
  1416. case MO_SL:
  1417. case MO_UL:
  1418. tcg_out_opc_imm(s, lw1, lo, base, 0);
  1419. tcg_out_opc_imm(s, lw2, lo, base, 3);
  1420. if (TCG_TARGET_REG_BITS == 64 && is_64 && !sgn) {
  1421. tcg_out_ext32u(s, lo, lo);
  1422. }
  1423. break;
  1424. case MO_UL | MO_BSWAP:
  1425. case MO_SL | MO_BSWAP:
  1426. if (use_mips32r2_instructions) {
  1427. tcg_out_opc_imm(s, lw1, lo, base, 0);
  1428. tcg_out_opc_imm(s, lw2, lo, base, 3);
  1429. tcg_out_bswap32(s, lo, lo,
  1430. TCG_TARGET_REG_BITS == 64 && is_64
  1431. ? (sgn ? TCG_BSWAP_OS : TCG_BSWAP_OZ) : 0);
  1432. } else {
  1433. const tcg_insn_unit *subr =
  1434. (TCG_TARGET_REG_BITS == 64 && is_64 && !sgn
  1435. ? bswap32u_addr : bswap32_addr);
  1436. tcg_out_opc_imm(s, lw1, TCG_TMP0, base, 0);
  1437. tcg_out_bswap_subr(s, subr);
  1438. /* delay slot */
  1439. tcg_out_opc_imm(s, lw2, TCG_TMP0, base, 3);
  1440. tcg_out_mov(s, is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32, lo, TCG_TMP3);
  1441. }
  1442. break;
  1443. case MO_UQ:
  1444. if (TCG_TARGET_REG_BITS == 64) {
  1445. tcg_out_opc_imm(s, ld1, lo, base, 0);
  1446. tcg_out_opc_imm(s, ld2, lo, base, 7);
  1447. } else {
  1448. tcg_out_opc_imm(s, lw1, MIPS_BE ? hi : lo, base, 0 + 0);
  1449. tcg_out_opc_imm(s, lw2, MIPS_BE ? hi : lo, base, 0 + 3);
  1450. tcg_out_opc_imm(s, lw1, MIPS_BE ? lo : hi, base, 4 + 0);
  1451. tcg_out_opc_imm(s, lw2, MIPS_BE ? lo : hi, base, 4 + 3);
  1452. }
  1453. break;
  1454. case MO_UQ | MO_BSWAP:
  1455. if (TCG_TARGET_REG_BITS == 64) {
  1456. if (use_mips32r2_instructions) {
  1457. tcg_out_opc_imm(s, ld1, lo, base, 0);
  1458. tcg_out_opc_imm(s, ld2, lo, base, 7);
  1459. tcg_out_bswap64(s, lo, lo);
  1460. } else {
  1461. tcg_out_opc_imm(s, ld1, TCG_TMP0, base, 0);
  1462. tcg_out_bswap_subr(s, bswap64_addr);
  1463. /* delay slot */
  1464. tcg_out_opc_imm(s, ld2, TCG_TMP0, base, 7);
  1465. tcg_out_mov(s, TCG_TYPE_I64, lo, TCG_TMP3);
  1466. }
  1467. } else if (use_mips32r2_instructions) {
  1468. tcg_out_opc_imm(s, lw1, TCG_TMP0, base, 0 + 0);
  1469. tcg_out_opc_imm(s, lw2, TCG_TMP0, base, 0 + 3);
  1470. tcg_out_opc_imm(s, lw1, TCG_TMP1, base, 4 + 0);
  1471. tcg_out_opc_imm(s, lw2, TCG_TMP1, base, 4 + 3);
  1472. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP0, 0, TCG_TMP0);
  1473. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP1, 0, TCG_TMP1);
  1474. tcg_out_opc_sa(s, OPC_ROTR, MIPS_BE ? lo : hi, TCG_TMP0, 16);
  1475. tcg_out_opc_sa(s, OPC_ROTR, MIPS_BE ? hi : lo, TCG_TMP1, 16);
  1476. } else {
  1477. tcg_out_opc_imm(s, lw1, TCG_TMP0, base, 0 + 0);
  1478. tcg_out_bswap_subr(s, bswap32_addr);
  1479. /* delay slot */
  1480. tcg_out_opc_imm(s, lw2, TCG_TMP0, base, 0 + 3);
  1481. tcg_out_opc_imm(s, lw1, TCG_TMP0, base, 4 + 0);
  1482. tcg_out_mov(s, TCG_TYPE_I32, MIPS_BE ? lo : hi, TCG_TMP3);
  1483. tcg_out_bswap_subr(s, bswap32_addr);
  1484. /* delay slot */
  1485. tcg_out_opc_imm(s, lw2, TCG_TMP0, base, 4 + 3);
  1486. tcg_out_mov(s, TCG_TYPE_I32, MIPS_BE ? hi : lo, TCG_TMP3);
  1487. }
  1488. break;
  1489. default:
  1490. g_assert_not_reached();
  1491. }
  1492. }
  1493. static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is_64)
  1494. {
  1495. TCGReg addr_regl, addr_regh __attribute__((unused));
  1496. TCGReg data_regl, data_regh;
  1497. MemOpIdx oi;
  1498. MemOp opc;
  1499. #if defined(CONFIG_SOFTMMU)
  1500. tcg_insn_unit *label_ptr[2];
  1501. #else
  1502. #endif
  1503. unsigned a_bits, s_bits;
  1504. TCGReg base = TCG_REG_A0;
  1505. data_regl = *args++;
  1506. data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
  1507. addr_regl = *args++;
  1508. addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
  1509. oi = *args++;
  1510. opc = get_memop(oi);
  1511. a_bits = get_alignment_bits(opc);
  1512. s_bits = opc & MO_SIZE;
  1513. /*
  1514. * R6 removes the left/right instructions but requires the
  1515. * system to support misaligned memory accesses.
  1516. */
  1517. #if defined(CONFIG_SOFTMMU)
  1518. tcg_out_tlb_load(s, base, addr_regl, addr_regh, oi, label_ptr, 1);
  1519. if (use_mips32r6_instructions || a_bits >= s_bits) {
  1520. tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
  1521. } else {
  1522. tcg_out_qemu_ld_unalign(s, data_regl, data_regh, base, opc, is_64);
  1523. }
  1524. add_qemu_ldst_label(s, 1, oi,
  1525. (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
  1526. data_regl, data_regh, addr_regl, addr_regh,
  1527. s->code_ptr, label_ptr);
  1528. #else
  1529. if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
  1530. tcg_out_ext32u(s, base, addr_regl);
  1531. addr_regl = base;
  1532. }
  1533. if (guest_base == 0 && data_regl != addr_regl) {
  1534. base = addr_regl;
  1535. } else if (guest_base == (int16_t)guest_base) {
  1536. tcg_out_opc_imm(s, ALIAS_PADDI, base, addr_regl, guest_base);
  1537. } else {
  1538. tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_GUEST_BASE_REG, addr_regl);
  1539. }
  1540. if (use_mips32r6_instructions) {
  1541. if (a_bits) {
  1542. tcg_out_test_alignment(s, true, addr_regl, addr_regh, a_bits);
  1543. }
  1544. tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
  1545. } else {
  1546. if (a_bits && a_bits != s_bits) {
  1547. tcg_out_test_alignment(s, true, addr_regl, addr_regh, a_bits);
  1548. }
  1549. if (a_bits >= s_bits) {
  1550. tcg_out_qemu_ld_direct(s, data_regl, data_regh, base, opc, is_64);
  1551. } else {
  1552. tcg_out_qemu_ld_unalign(s, data_regl, data_regh, base, opc, is_64);
  1553. }
  1554. }
  1555. #endif
  1556. }
  1557. static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
  1558. TCGReg base, MemOp opc)
  1559. {
  1560. /* Don't clutter the code below with checks to avoid bswapping ZERO. */
  1561. if ((lo | hi) == 0) {
  1562. opc &= ~MO_BSWAP;
  1563. }
  1564. switch (opc & (MO_SIZE | MO_BSWAP)) {
  1565. case MO_8:
  1566. tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
  1567. break;
  1568. case MO_16 | MO_BSWAP:
  1569. tcg_out_bswap16(s, TCG_TMP1, lo, 0);
  1570. lo = TCG_TMP1;
  1571. /* FALLTHRU */
  1572. case MO_16:
  1573. tcg_out_opc_imm(s, OPC_SH, lo, base, 0);
  1574. break;
  1575. case MO_32 | MO_BSWAP:
  1576. tcg_out_bswap32(s, TCG_TMP3, lo, 0);
  1577. lo = TCG_TMP3;
  1578. /* FALLTHRU */
  1579. case MO_32:
  1580. tcg_out_opc_imm(s, OPC_SW, lo, base, 0);
  1581. break;
  1582. case MO_64 | MO_BSWAP:
  1583. if (TCG_TARGET_REG_BITS == 64) {
  1584. tcg_out_bswap64(s, TCG_TMP3, lo);
  1585. tcg_out_opc_imm(s, OPC_SD, TCG_TMP3, base, 0);
  1586. } else if (use_mips32r2_instructions) {
  1587. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP0, 0, MIPS_BE ? lo : hi);
  1588. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP1, 0, MIPS_BE ? hi : lo);
  1589. tcg_out_opc_sa(s, OPC_ROTR, TCG_TMP0, TCG_TMP0, 16);
  1590. tcg_out_opc_sa(s, OPC_ROTR, TCG_TMP1, TCG_TMP1, 16);
  1591. tcg_out_opc_imm(s, OPC_SW, TCG_TMP0, base, 0);
  1592. tcg_out_opc_imm(s, OPC_SW, TCG_TMP1, base, 4);
  1593. } else {
  1594. tcg_out_bswap32(s, TCG_TMP3, MIPS_BE ? lo : hi, 0);
  1595. tcg_out_opc_imm(s, OPC_SW, TCG_TMP3, base, 0);
  1596. tcg_out_bswap32(s, TCG_TMP3, MIPS_BE ? hi : lo, 0);
  1597. tcg_out_opc_imm(s, OPC_SW, TCG_TMP3, base, 4);
  1598. }
  1599. break;
  1600. case MO_64:
  1601. if (TCG_TARGET_REG_BITS == 64) {
  1602. tcg_out_opc_imm(s, OPC_SD, lo, base, 0);
  1603. } else {
  1604. tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? hi : lo, base, 0);
  1605. tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? lo : hi, base, 4);
  1606. }
  1607. break;
  1608. default:
  1609. tcg_abort();
  1610. }
  1611. }
  1612. static void tcg_out_qemu_st_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
  1613. TCGReg base, MemOp opc)
  1614. {
  1615. const MIPSInsn sw1 = MIPS_BE ? OPC_SWL : OPC_SWR;
  1616. const MIPSInsn sw2 = MIPS_BE ? OPC_SWR : OPC_SWL;
  1617. const MIPSInsn sd1 = MIPS_BE ? OPC_SDL : OPC_SDR;
  1618. const MIPSInsn sd2 = MIPS_BE ? OPC_SDR : OPC_SDL;
  1619. /* Don't clutter the code below with checks to avoid bswapping ZERO. */
  1620. if ((lo | hi) == 0) {
  1621. opc &= ~MO_BSWAP;
  1622. }
  1623. switch (opc & (MO_SIZE | MO_BSWAP)) {
  1624. case MO_16 | MO_BE:
  1625. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8);
  1626. tcg_out_opc_imm(s, OPC_SB, TCG_TMP0, base, 0);
  1627. tcg_out_opc_imm(s, OPC_SB, lo, base, 1);
  1628. break;
  1629. case MO_16 | MO_LE:
  1630. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8);
  1631. tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
  1632. tcg_out_opc_imm(s, OPC_SB, TCG_TMP0, base, 1);
  1633. break;
  1634. case MO_32 | MO_BSWAP:
  1635. tcg_out_bswap32(s, TCG_TMP3, lo, 0);
  1636. lo = TCG_TMP3;
  1637. /* fall through */
  1638. case MO_32:
  1639. tcg_out_opc_imm(s, sw1, lo, base, 0);
  1640. tcg_out_opc_imm(s, sw2, lo, base, 3);
  1641. break;
  1642. case MO_64 | MO_BSWAP:
  1643. if (TCG_TARGET_REG_BITS == 64) {
  1644. tcg_out_bswap64(s, TCG_TMP3, lo);
  1645. lo = TCG_TMP3;
  1646. } else if (use_mips32r2_instructions) {
  1647. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP0, 0, MIPS_BE ? hi : lo);
  1648. tcg_out_opc_reg(s, OPC_WSBH, TCG_TMP1, 0, MIPS_BE ? lo : hi);
  1649. tcg_out_opc_sa(s, OPC_ROTR, TCG_TMP0, TCG_TMP0, 16);
  1650. tcg_out_opc_sa(s, OPC_ROTR, TCG_TMP1, TCG_TMP1, 16);
  1651. hi = MIPS_BE ? TCG_TMP0 : TCG_TMP1;
  1652. lo = MIPS_BE ? TCG_TMP1 : TCG_TMP0;
  1653. } else {
  1654. tcg_out_bswap32(s, TCG_TMP3, MIPS_BE ? lo : hi, 0);
  1655. tcg_out_opc_imm(s, sw1, TCG_TMP3, base, 0 + 0);
  1656. tcg_out_opc_imm(s, sw2, TCG_TMP3, base, 0 + 3);
  1657. tcg_out_bswap32(s, TCG_TMP3, MIPS_BE ? hi : lo, 0);
  1658. tcg_out_opc_imm(s, sw1, TCG_TMP3, base, 4 + 0);
  1659. tcg_out_opc_imm(s, sw2, TCG_TMP3, base, 4 + 3);
  1660. break;
  1661. }
  1662. /* fall through */
  1663. case MO_64:
  1664. if (TCG_TARGET_REG_BITS == 64) {
  1665. tcg_out_opc_imm(s, sd1, lo, base, 0);
  1666. tcg_out_opc_imm(s, sd2, lo, base, 7);
  1667. } else {
  1668. tcg_out_opc_imm(s, sw1, MIPS_BE ? hi : lo, base, 0 + 0);
  1669. tcg_out_opc_imm(s, sw2, MIPS_BE ? hi : lo, base, 0 + 3);
  1670. tcg_out_opc_imm(s, sw1, MIPS_BE ? lo : hi, base, 4 + 0);
  1671. tcg_out_opc_imm(s, sw2, MIPS_BE ? lo : hi, base, 4 + 3);
  1672. }
  1673. break;
  1674. default:
  1675. tcg_abort();
  1676. }
  1677. }
  1678. static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is_64)
  1679. {
  1680. TCGReg addr_regl, addr_regh __attribute__((unused));
  1681. TCGReg data_regl, data_regh;
  1682. MemOpIdx oi;
  1683. MemOp opc;
  1684. #if defined(CONFIG_SOFTMMU)
  1685. tcg_insn_unit *label_ptr[2];
  1686. #endif
  1687. unsigned a_bits, s_bits;
  1688. TCGReg base = TCG_REG_A0;
  1689. data_regl = *args++;
  1690. data_regh = (TCG_TARGET_REG_BITS == 32 && is_64 ? *args++ : 0);
  1691. addr_regl = *args++;
  1692. addr_regh = (TCG_TARGET_REG_BITS < TARGET_LONG_BITS ? *args++ : 0);
  1693. oi = *args++;
  1694. opc = get_memop(oi);
  1695. a_bits = get_alignment_bits(opc);
  1696. s_bits = opc & MO_SIZE;
  1697. /*
  1698. * R6 removes the left/right instructions but requires the
  1699. * system to support misaligned memory accesses.
  1700. */
  1701. #if defined(CONFIG_SOFTMMU)
  1702. tcg_out_tlb_load(s, base, addr_regl, addr_regh, oi, label_ptr, 0);
  1703. if (use_mips32r6_instructions || a_bits >= s_bits) {
  1704. tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
  1705. } else {
  1706. tcg_out_qemu_st_unalign(s, data_regl, data_regh, base, opc);
  1707. }
  1708. add_qemu_ldst_label(s, 0, oi,
  1709. (is_64 ? TCG_TYPE_I64 : TCG_TYPE_I32),
  1710. data_regl, data_regh, addr_regl, addr_regh,
  1711. s->code_ptr, label_ptr);
  1712. #else
  1713. if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
  1714. tcg_out_ext32u(s, base, addr_regl);
  1715. addr_regl = base;
  1716. }
  1717. if (guest_base == 0) {
  1718. base = addr_regl;
  1719. } else if (guest_base == (int16_t)guest_base) {
  1720. tcg_out_opc_imm(s, ALIAS_PADDI, base, addr_regl, guest_base);
  1721. } else {
  1722. tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_GUEST_BASE_REG, addr_regl);
  1723. }
  1724. if (use_mips32r6_instructions) {
  1725. if (a_bits) {
  1726. tcg_out_test_alignment(s, true, addr_regl, addr_regh, a_bits);
  1727. }
  1728. tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
  1729. } else {
  1730. if (a_bits && a_bits != s_bits) {
  1731. tcg_out_test_alignment(s, true, addr_regl, addr_regh, a_bits);
  1732. }
  1733. if (a_bits >= s_bits) {
  1734. tcg_out_qemu_st_direct(s, data_regl, data_regh, base, opc);
  1735. } else {
  1736. tcg_out_qemu_st_unalign(s, data_regl, data_regh, base, opc);
  1737. }
  1738. }
  1739. #endif
  1740. }
  1741. static void tcg_out_mb(TCGContext *s, TCGArg a0)
  1742. {
  1743. static const MIPSInsn sync[] = {
  1744. /* Note that SYNC_MB is a slightly weaker than SYNC 0,
  1745. as the former is an ordering barrier and the latter
  1746. is a completion barrier. */
  1747. [0 ... TCG_MO_ALL] = OPC_SYNC_MB,
  1748. [TCG_MO_LD_LD] = OPC_SYNC_RMB,
  1749. [TCG_MO_ST_ST] = OPC_SYNC_WMB,
  1750. [TCG_MO_LD_ST] = OPC_SYNC_RELEASE,
  1751. [TCG_MO_LD_ST | TCG_MO_ST_ST] = OPC_SYNC_RELEASE,
  1752. [TCG_MO_LD_ST | TCG_MO_LD_LD] = OPC_SYNC_ACQUIRE,
  1753. };
  1754. tcg_out32(s, sync[a0 & TCG_MO_ALL]);
  1755. }
  1756. static void tcg_out_clz(TCGContext *s, MIPSInsn opcv2, MIPSInsn opcv6,
  1757. int width, TCGReg a0, TCGReg a1, TCGArg a2)
  1758. {
  1759. if (use_mips32r6_instructions) {
  1760. if (a2 == width) {
  1761. tcg_out_opc_reg(s, opcv6, a0, a1, 0);
  1762. } else {
  1763. tcg_out_opc_reg(s, opcv6, TCG_TMP0, a1, 0);
  1764. tcg_out_movcond(s, TCG_COND_EQ, a0, a1, 0, a2, TCG_TMP0);
  1765. }
  1766. } else {
  1767. if (a2 == width) {
  1768. tcg_out_opc_reg(s, opcv2, a0, a1, a1);
  1769. } else if (a0 == a2) {
  1770. tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
  1771. tcg_out_opc_reg(s, OPC_MOVN, a0, TCG_TMP0, a1);
  1772. } else if (a0 != a1) {
  1773. tcg_out_opc_reg(s, opcv2, a0, a1, a1);
  1774. tcg_out_opc_reg(s, OPC_MOVZ, a0, a2, a1);
  1775. } else {
  1776. tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
  1777. tcg_out_opc_reg(s, OPC_MOVZ, TCG_TMP0, a2, a1);
  1778. tcg_out_mov(s, TCG_TYPE_REG, a0, TCG_TMP0);
  1779. }
  1780. }
  1781. }
  1782. static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
  1783. {
  1784. TCGReg b0 = TCG_REG_ZERO;
  1785. if (a0 & ~0xffff) {
  1786. tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_V0, a0 & ~0xffff);
  1787. b0 = TCG_REG_V0;
  1788. }
  1789. if (!tcg_out_opc_jmp(s, OPC_J, tb_ret_addr)) {
  1790. tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)tb_ret_addr);
  1791. tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
  1792. }
  1793. tcg_out_opc_imm(s, OPC_ORI, TCG_REG_V0, b0, a0 & 0xffff);
  1794. }
  1795. static void tcg_out_goto_tb(TCGContext *s, int which)
  1796. {
  1797. /* indirect jump method */
  1798. tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_REG_ZERO,
  1799. get_jmp_target_addr(s, which));
  1800. tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
  1801. tcg_out_nop(s);
  1802. set_jmp_reset_offset(s, which);
  1803. }
  1804. void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
  1805. uintptr_t jmp_rx, uintptr_t jmp_rw)
  1806. {
  1807. /* Always indirect, nothing to do */
  1808. }
  1809. static void tcg_out_op(TCGContext *s, TCGOpcode opc,
  1810. const TCGArg args[TCG_MAX_OP_ARGS],
  1811. const int const_args[TCG_MAX_OP_ARGS])
  1812. {
  1813. MIPSInsn i1, i2;
  1814. TCGArg a0, a1, a2;
  1815. int c2;
  1816. /*
  1817. * Note that many operands use the constraint set "rZ".
  1818. * We make use of the fact that 0 is the ZERO register,
  1819. * and hence such cases need not check for const_args.
  1820. */
  1821. a0 = args[0];
  1822. a1 = args[1];
  1823. a2 = args[2];
  1824. c2 = const_args[2];
  1825. switch (opc) {
  1826. case INDEX_op_goto_ptr:
  1827. /* jmp to the given host address (could be epilogue) */
  1828. tcg_out_opc_reg(s, OPC_JR, 0, a0, 0);
  1829. tcg_out_nop(s);
  1830. break;
  1831. case INDEX_op_br:
  1832. tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
  1833. arg_label(a0));
  1834. break;
  1835. case INDEX_op_ld8u_i32:
  1836. case INDEX_op_ld8u_i64:
  1837. i1 = OPC_LBU;
  1838. goto do_ldst;
  1839. case INDEX_op_ld8s_i32:
  1840. case INDEX_op_ld8s_i64:
  1841. i1 = OPC_LB;
  1842. goto do_ldst;
  1843. case INDEX_op_ld16u_i32:
  1844. case INDEX_op_ld16u_i64:
  1845. i1 = OPC_LHU;
  1846. goto do_ldst;
  1847. case INDEX_op_ld16s_i32:
  1848. case INDEX_op_ld16s_i64:
  1849. i1 = OPC_LH;
  1850. goto do_ldst;
  1851. case INDEX_op_ld_i32:
  1852. case INDEX_op_ld32s_i64:
  1853. i1 = OPC_LW;
  1854. goto do_ldst;
  1855. case INDEX_op_ld32u_i64:
  1856. i1 = OPC_LWU;
  1857. goto do_ldst;
  1858. case INDEX_op_ld_i64:
  1859. i1 = OPC_LD;
  1860. goto do_ldst;
  1861. case INDEX_op_st8_i32:
  1862. case INDEX_op_st8_i64:
  1863. i1 = OPC_SB;
  1864. goto do_ldst;
  1865. case INDEX_op_st16_i32:
  1866. case INDEX_op_st16_i64:
  1867. i1 = OPC_SH;
  1868. goto do_ldst;
  1869. case INDEX_op_st_i32:
  1870. case INDEX_op_st32_i64:
  1871. i1 = OPC_SW;
  1872. goto do_ldst;
  1873. case INDEX_op_st_i64:
  1874. i1 = OPC_SD;
  1875. do_ldst:
  1876. tcg_out_ldst(s, i1, a0, a1, a2);
  1877. break;
  1878. case INDEX_op_add_i32:
  1879. i1 = OPC_ADDU, i2 = OPC_ADDIU;
  1880. goto do_binary;
  1881. case INDEX_op_add_i64:
  1882. i1 = OPC_DADDU, i2 = OPC_DADDIU;
  1883. goto do_binary;
  1884. case INDEX_op_or_i32:
  1885. case INDEX_op_or_i64:
  1886. i1 = OPC_OR, i2 = OPC_ORI;
  1887. goto do_binary;
  1888. case INDEX_op_xor_i32:
  1889. case INDEX_op_xor_i64:
  1890. i1 = OPC_XOR, i2 = OPC_XORI;
  1891. do_binary:
  1892. if (c2) {
  1893. tcg_out_opc_imm(s, i2, a0, a1, a2);
  1894. break;
  1895. }
  1896. do_binaryv:
  1897. tcg_out_opc_reg(s, i1, a0, a1, a2);
  1898. break;
  1899. case INDEX_op_sub_i32:
  1900. i1 = OPC_SUBU, i2 = OPC_ADDIU;
  1901. goto do_subtract;
  1902. case INDEX_op_sub_i64:
  1903. i1 = OPC_DSUBU, i2 = OPC_DADDIU;
  1904. do_subtract:
  1905. if (c2) {
  1906. tcg_out_opc_imm(s, i2, a0, a1, -a2);
  1907. break;
  1908. }
  1909. goto do_binaryv;
  1910. case INDEX_op_and_i32:
  1911. if (c2 && a2 != (uint16_t)a2) {
  1912. int msb = ctz32(~a2) - 1;
  1913. tcg_debug_assert(use_mips32r2_instructions);
  1914. tcg_debug_assert(is_p2m1(a2));
  1915. tcg_out_opc_bf(s, OPC_EXT, a0, a1, msb, 0);
  1916. break;
  1917. }
  1918. i1 = OPC_AND, i2 = OPC_ANDI;
  1919. goto do_binary;
  1920. case INDEX_op_and_i64:
  1921. if (c2 && a2 != (uint16_t)a2) {
  1922. int msb = ctz64(~a2) - 1;
  1923. tcg_debug_assert(use_mips32r2_instructions);
  1924. tcg_debug_assert(is_p2m1(a2));
  1925. tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1, msb, 0);
  1926. break;
  1927. }
  1928. i1 = OPC_AND, i2 = OPC_ANDI;
  1929. goto do_binary;
  1930. case INDEX_op_nor_i32:
  1931. case INDEX_op_nor_i64:
  1932. i1 = OPC_NOR;
  1933. goto do_binaryv;
  1934. case INDEX_op_mul_i32:
  1935. if (use_mips32_instructions) {
  1936. tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
  1937. break;
  1938. }
  1939. i1 = OPC_MULT, i2 = OPC_MFLO;
  1940. goto do_hilo1;
  1941. case INDEX_op_mulsh_i32:
  1942. if (use_mips32r6_instructions) {
  1943. tcg_out_opc_reg(s, OPC_MUH, a0, a1, a2);
  1944. break;
  1945. }
  1946. i1 = OPC_MULT, i2 = OPC_MFHI;
  1947. goto do_hilo1;
  1948. case INDEX_op_muluh_i32:
  1949. if (use_mips32r6_instructions) {
  1950. tcg_out_opc_reg(s, OPC_MUHU, a0, a1, a2);
  1951. break;
  1952. }
  1953. i1 = OPC_MULTU, i2 = OPC_MFHI;
  1954. goto do_hilo1;
  1955. case INDEX_op_div_i32:
  1956. if (use_mips32r6_instructions) {
  1957. tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2);
  1958. break;
  1959. }
  1960. i1 = OPC_DIV, i2 = OPC_MFLO;
  1961. goto do_hilo1;
  1962. case INDEX_op_divu_i32:
  1963. if (use_mips32r6_instructions) {
  1964. tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2);
  1965. break;
  1966. }
  1967. i1 = OPC_DIVU, i2 = OPC_MFLO;
  1968. goto do_hilo1;
  1969. case INDEX_op_rem_i32:
  1970. if (use_mips32r6_instructions) {
  1971. tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2);
  1972. break;
  1973. }
  1974. i1 = OPC_DIV, i2 = OPC_MFHI;
  1975. goto do_hilo1;
  1976. case INDEX_op_remu_i32:
  1977. if (use_mips32r6_instructions) {
  1978. tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2);
  1979. break;
  1980. }
  1981. i1 = OPC_DIVU, i2 = OPC_MFHI;
  1982. goto do_hilo1;
  1983. case INDEX_op_mul_i64:
  1984. if (use_mips32r6_instructions) {
  1985. tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2);
  1986. break;
  1987. }
  1988. i1 = OPC_DMULT, i2 = OPC_MFLO;
  1989. goto do_hilo1;
  1990. case INDEX_op_mulsh_i64:
  1991. if (use_mips32r6_instructions) {
  1992. tcg_out_opc_reg(s, OPC_DMUH, a0, a1, a2);
  1993. break;
  1994. }
  1995. i1 = OPC_DMULT, i2 = OPC_MFHI;
  1996. goto do_hilo1;
  1997. case INDEX_op_muluh_i64:
  1998. if (use_mips32r6_instructions) {
  1999. tcg_out_opc_reg(s, OPC_DMUHU, a0, a1, a2);
  2000. break;
  2001. }
  2002. i1 = OPC_DMULTU, i2 = OPC_MFHI;
  2003. goto do_hilo1;
  2004. case INDEX_op_div_i64:
  2005. if (use_mips32r6_instructions) {
  2006. tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2);
  2007. break;
  2008. }
  2009. i1 = OPC_DDIV, i2 = OPC_MFLO;
  2010. goto do_hilo1;
  2011. case INDEX_op_divu_i64:
  2012. if (use_mips32r6_instructions) {
  2013. tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2);
  2014. break;
  2015. }
  2016. i1 = OPC_DDIVU, i2 = OPC_MFLO;
  2017. goto do_hilo1;
  2018. case INDEX_op_rem_i64:
  2019. if (use_mips32r6_instructions) {
  2020. tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2);
  2021. break;
  2022. }
  2023. i1 = OPC_DDIV, i2 = OPC_MFHI;
  2024. goto do_hilo1;
  2025. case INDEX_op_remu_i64:
  2026. if (use_mips32r6_instructions) {
  2027. tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2);
  2028. break;
  2029. }
  2030. i1 = OPC_DDIVU, i2 = OPC_MFHI;
  2031. do_hilo1:
  2032. tcg_out_opc_reg(s, i1, 0, a1, a2);
  2033. tcg_out_opc_reg(s, i2, a0, 0, 0);
  2034. break;
  2035. case INDEX_op_muls2_i32:
  2036. i1 = OPC_MULT;
  2037. goto do_hilo2;
  2038. case INDEX_op_mulu2_i32:
  2039. i1 = OPC_MULTU;
  2040. goto do_hilo2;
  2041. case INDEX_op_muls2_i64:
  2042. i1 = OPC_DMULT;
  2043. goto do_hilo2;
  2044. case INDEX_op_mulu2_i64:
  2045. i1 = OPC_DMULTU;
  2046. do_hilo2:
  2047. tcg_out_opc_reg(s, i1, 0, a2, args[3]);
  2048. tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0);
  2049. tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0);
  2050. break;
  2051. case INDEX_op_not_i32:
  2052. case INDEX_op_not_i64:
  2053. i1 = OPC_NOR;
  2054. goto do_unary;
  2055. case INDEX_op_ext8s_i32:
  2056. case INDEX_op_ext8s_i64:
  2057. i1 = OPC_SEB;
  2058. goto do_unary;
  2059. case INDEX_op_ext16s_i32:
  2060. case INDEX_op_ext16s_i64:
  2061. i1 = OPC_SEH;
  2062. do_unary:
  2063. tcg_out_opc_reg(s, i1, a0, TCG_REG_ZERO, a1);
  2064. break;
  2065. case INDEX_op_bswap16_i32:
  2066. case INDEX_op_bswap16_i64:
  2067. tcg_out_bswap16(s, a0, a1, a2);
  2068. break;
  2069. case INDEX_op_bswap32_i32:
  2070. tcg_out_bswap32(s, a0, a1, 0);
  2071. break;
  2072. case INDEX_op_bswap32_i64:
  2073. tcg_out_bswap32(s, a0, a1, a2);
  2074. break;
  2075. case INDEX_op_bswap64_i64:
  2076. tcg_out_bswap64(s, a0, a1);
  2077. break;
  2078. case INDEX_op_extrh_i64_i32:
  2079. tcg_out_dsra(s, a0, a1, 32);
  2080. break;
  2081. case INDEX_op_ext32s_i64:
  2082. case INDEX_op_ext_i32_i64:
  2083. case INDEX_op_extrl_i64_i32:
  2084. tcg_out_opc_sa(s, OPC_SLL, a0, a1, 0);
  2085. break;
  2086. case INDEX_op_ext32u_i64:
  2087. case INDEX_op_extu_i32_i64:
  2088. tcg_out_ext32u(s, a0, a1);
  2089. break;
  2090. case INDEX_op_sar_i32:
  2091. i1 = OPC_SRAV, i2 = OPC_SRA;
  2092. goto do_shift;
  2093. case INDEX_op_shl_i32:
  2094. i1 = OPC_SLLV, i2 = OPC_SLL;
  2095. goto do_shift;
  2096. case INDEX_op_shr_i32:
  2097. i1 = OPC_SRLV, i2 = OPC_SRL;
  2098. goto do_shift;
  2099. case INDEX_op_rotr_i32:
  2100. i1 = OPC_ROTRV, i2 = OPC_ROTR;
  2101. do_shift:
  2102. if (c2) {
  2103. tcg_out_opc_sa(s, i2, a0, a1, a2);
  2104. break;
  2105. }
  2106. do_shiftv:
  2107. tcg_out_opc_reg(s, i1, a0, a2, a1);
  2108. break;
  2109. case INDEX_op_rotl_i32:
  2110. if (c2) {
  2111. tcg_out_opc_sa(s, OPC_ROTR, a0, a1, 32 - a2);
  2112. } else {
  2113. tcg_out_opc_reg(s, OPC_SUBU, TCG_TMP0, TCG_REG_ZERO, a2);
  2114. tcg_out_opc_reg(s, OPC_ROTRV, a0, TCG_TMP0, a1);
  2115. }
  2116. break;
  2117. case INDEX_op_sar_i64:
  2118. if (c2) {
  2119. tcg_out_dsra(s, a0, a1, a2);
  2120. break;
  2121. }
  2122. i1 = OPC_DSRAV;
  2123. goto do_shiftv;
  2124. case INDEX_op_shl_i64:
  2125. if (c2) {
  2126. tcg_out_dsll(s, a0, a1, a2);
  2127. break;
  2128. }
  2129. i1 = OPC_DSLLV;
  2130. goto do_shiftv;
  2131. case INDEX_op_shr_i64:
  2132. if (c2) {
  2133. tcg_out_dsrl(s, a0, a1, a2);
  2134. break;
  2135. }
  2136. i1 = OPC_DSRLV;
  2137. goto do_shiftv;
  2138. case INDEX_op_rotr_i64:
  2139. if (c2) {
  2140. tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2);
  2141. break;
  2142. }
  2143. i1 = OPC_DROTRV;
  2144. goto do_shiftv;
  2145. case INDEX_op_rotl_i64:
  2146. if (c2) {
  2147. tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, 64 - a2);
  2148. } else {
  2149. tcg_out_opc_reg(s, OPC_DSUBU, TCG_TMP0, TCG_REG_ZERO, a2);
  2150. tcg_out_opc_reg(s, OPC_DROTRV, a0, TCG_TMP0, a1);
  2151. }
  2152. break;
  2153. case INDEX_op_clz_i32:
  2154. tcg_out_clz(s, OPC_CLZ, OPC_CLZ_R6, 32, a0, a1, a2);
  2155. break;
  2156. case INDEX_op_clz_i64:
  2157. tcg_out_clz(s, OPC_DCLZ, OPC_DCLZ_R6, 64, a0, a1, a2);
  2158. break;
  2159. case INDEX_op_deposit_i32:
  2160. tcg_out_opc_bf(s, OPC_INS, a0, a2, args[3] + args[4] - 1, args[3]);
  2161. break;
  2162. case INDEX_op_deposit_i64:
  2163. tcg_out_opc_bf64(s, OPC_DINS, OPC_DINSM, OPC_DINSU, a0, a2,
  2164. args[3] + args[4] - 1, args[3]);
  2165. break;
  2166. case INDEX_op_extract_i32:
  2167. tcg_out_opc_bf(s, OPC_EXT, a0, a1, args[3] - 1, a2);
  2168. break;
  2169. case INDEX_op_extract_i64:
  2170. tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1,
  2171. args[3] - 1, a2);
  2172. break;
  2173. case INDEX_op_brcond_i32:
  2174. case INDEX_op_brcond_i64:
  2175. tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
  2176. break;
  2177. case INDEX_op_brcond2_i32:
  2178. tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
  2179. break;
  2180. case INDEX_op_movcond_i32:
  2181. case INDEX_op_movcond_i64:
  2182. tcg_out_movcond(s, args[5], a0, a1, a2, args[3], args[4]);
  2183. break;
  2184. case INDEX_op_setcond_i32:
  2185. case INDEX_op_setcond_i64:
  2186. tcg_out_setcond(s, args[3], a0, a1, a2);
  2187. break;
  2188. case INDEX_op_setcond2_i32:
  2189. tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
  2190. break;
  2191. case INDEX_op_qemu_ld_i32:
  2192. tcg_out_qemu_ld(s, args, false);
  2193. break;
  2194. case INDEX_op_qemu_ld_i64:
  2195. tcg_out_qemu_ld(s, args, true);
  2196. break;
  2197. case INDEX_op_qemu_st_i32:
  2198. tcg_out_qemu_st(s, args, false);
  2199. break;
  2200. case INDEX_op_qemu_st_i64:
  2201. tcg_out_qemu_st(s, args, true);
  2202. break;
  2203. case INDEX_op_add2_i32:
  2204. tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
  2205. const_args[4], const_args[5], false);
  2206. break;
  2207. case INDEX_op_sub2_i32:
  2208. tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
  2209. const_args[4], const_args[5], true);
  2210. break;
  2211. case INDEX_op_mb:
  2212. tcg_out_mb(s, a0);
  2213. break;
  2214. case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
  2215. case INDEX_op_mov_i64:
  2216. case INDEX_op_call: /* Always emitted via tcg_out_call. */
  2217. case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
  2218. case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
  2219. default:
  2220. tcg_abort();
  2221. }
  2222. }
  2223. static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
  2224. {
  2225. switch (op) {
  2226. case INDEX_op_goto_ptr:
  2227. return C_O0_I1(r);
  2228. case INDEX_op_ld8u_i32:
  2229. case INDEX_op_ld8s_i32:
  2230. case INDEX_op_ld16u_i32:
  2231. case INDEX_op_ld16s_i32:
  2232. case INDEX_op_ld_i32:
  2233. case INDEX_op_not_i32:
  2234. case INDEX_op_bswap16_i32:
  2235. case INDEX_op_bswap32_i32:
  2236. case INDEX_op_ext8s_i32:
  2237. case INDEX_op_ext16s_i32:
  2238. case INDEX_op_extract_i32:
  2239. case INDEX_op_ld8u_i64:
  2240. case INDEX_op_ld8s_i64:
  2241. case INDEX_op_ld16u_i64:
  2242. case INDEX_op_ld16s_i64:
  2243. case INDEX_op_ld32s_i64:
  2244. case INDEX_op_ld32u_i64:
  2245. case INDEX_op_ld_i64:
  2246. case INDEX_op_not_i64:
  2247. case INDEX_op_bswap16_i64:
  2248. case INDEX_op_bswap32_i64:
  2249. case INDEX_op_bswap64_i64:
  2250. case INDEX_op_ext8s_i64:
  2251. case INDEX_op_ext16s_i64:
  2252. case INDEX_op_ext32s_i64:
  2253. case INDEX_op_ext32u_i64:
  2254. case INDEX_op_ext_i32_i64:
  2255. case INDEX_op_extu_i32_i64:
  2256. case INDEX_op_extrl_i64_i32:
  2257. case INDEX_op_extrh_i64_i32:
  2258. case INDEX_op_extract_i64:
  2259. return C_O1_I1(r, r);
  2260. case INDEX_op_st8_i32:
  2261. case INDEX_op_st16_i32:
  2262. case INDEX_op_st_i32:
  2263. case INDEX_op_st8_i64:
  2264. case INDEX_op_st16_i64:
  2265. case INDEX_op_st32_i64:
  2266. case INDEX_op_st_i64:
  2267. return C_O0_I2(rZ, r);
  2268. case INDEX_op_add_i32:
  2269. case INDEX_op_add_i64:
  2270. return C_O1_I2(r, r, rJ);
  2271. case INDEX_op_sub_i32:
  2272. case INDEX_op_sub_i64:
  2273. return C_O1_I2(r, rZ, rN);
  2274. case INDEX_op_mul_i32:
  2275. case INDEX_op_mulsh_i32:
  2276. case INDEX_op_muluh_i32:
  2277. case INDEX_op_div_i32:
  2278. case INDEX_op_divu_i32:
  2279. case INDEX_op_rem_i32:
  2280. case INDEX_op_remu_i32:
  2281. case INDEX_op_nor_i32:
  2282. case INDEX_op_setcond_i32:
  2283. case INDEX_op_mul_i64:
  2284. case INDEX_op_mulsh_i64:
  2285. case INDEX_op_muluh_i64:
  2286. case INDEX_op_div_i64:
  2287. case INDEX_op_divu_i64:
  2288. case INDEX_op_rem_i64:
  2289. case INDEX_op_remu_i64:
  2290. case INDEX_op_nor_i64:
  2291. case INDEX_op_setcond_i64:
  2292. return C_O1_I2(r, rZ, rZ);
  2293. case INDEX_op_muls2_i32:
  2294. case INDEX_op_mulu2_i32:
  2295. case INDEX_op_muls2_i64:
  2296. case INDEX_op_mulu2_i64:
  2297. return C_O2_I2(r, r, r, r);
  2298. case INDEX_op_and_i32:
  2299. case INDEX_op_and_i64:
  2300. return C_O1_I2(r, r, rIK);
  2301. case INDEX_op_or_i32:
  2302. case INDEX_op_xor_i32:
  2303. case INDEX_op_or_i64:
  2304. case INDEX_op_xor_i64:
  2305. return C_O1_I2(r, r, rI);
  2306. case INDEX_op_shl_i32:
  2307. case INDEX_op_shr_i32:
  2308. case INDEX_op_sar_i32:
  2309. case INDEX_op_rotr_i32:
  2310. case INDEX_op_rotl_i32:
  2311. case INDEX_op_shl_i64:
  2312. case INDEX_op_shr_i64:
  2313. case INDEX_op_sar_i64:
  2314. case INDEX_op_rotr_i64:
  2315. case INDEX_op_rotl_i64:
  2316. return C_O1_I2(r, r, ri);
  2317. case INDEX_op_clz_i32:
  2318. case INDEX_op_clz_i64:
  2319. return C_O1_I2(r, r, rWZ);
  2320. case INDEX_op_deposit_i32:
  2321. case INDEX_op_deposit_i64:
  2322. return C_O1_I2(r, 0, rZ);
  2323. case INDEX_op_brcond_i32:
  2324. case INDEX_op_brcond_i64:
  2325. return C_O0_I2(rZ, rZ);
  2326. case INDEX_op_movcond_i32:
  2327. case INDEX_op_movcond_i64:
  2328. return (use_mips32r6_instructions
  2329. ? C_O1_I4(r, rZ, rZ, rZ, rZ)
  2330. : C_O1_I4(r, rZ, rZ, rZ, 0));
  2331. case INDEX_op_add2_i32:
  2332. case INDEX_op_sub2_i32:
  2333. return C_O2_I4(r, r, rZ, rZ, rN, rN);
  2334. case INDEX_op_setcond2_i32:
  2335. return C_O1_I4(r, rZ, rZ, rZ, rZ);
  2336. case INDEX_op_brcond2_i32:
  2337. return C_O0_I4(rZ, rZ, rZ, rZ);
  2338. case INDEX_op_qemu_ld_i32:
  2339. return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
  2340. ? C_O1_I1(r, L) : C_O1_I2(r, L, L));
  2341. case INDEX_op_qemu_st_i32:
  2342. return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
  2343. ? C_O0_I2(SZ, S) : C_O0_I3(SZ, S, S));
  2344. case INDEX_op_qemu_ld_i64:
  2345. return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, L)
  2346. : TARGET_LONG_BITS == 32 ? C_O2_I1(r, r, L)
  2347. : C_O2_I2(r, r, L, L));
  2348. case INDEX_op_qemu_st_i64:
  2349. return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(SZ, S)
  2350. : TARGET_LONG_BITS == 32 ? C_O0_I3(SZ, SZ, S)
  2351. : C_O0_I4(SZ, SZ, S, S));
  2352. default:
  2353. g_assert_not_reached();
  2354. }
  2355. }
  2356. static const int tcg_target_callee_save_regs[] = {
  2357. TCG_REG_S0, /* used for the global env (TCG_AREG0) */
  2358. TCG_REG_S1,
  2359. TCG_REG_S2,
  2360. TCG_REG_S3,
  2361. TCG_REG_S4,
  2362. TCG_REG_S5,
  2363. TCG_REG_S6,
  2364. TCG_REG_S7,
  2365. TCG_REG_S8,
  2366. TCG_REG_RA, /* should be last for ABI compliance */
  2367. };
  2368. /* The Linux kernel doesn't provide any information about the available
  2369. instruction set. Probe it using a signal handler. */
  2370. #ifndef use_movnz_instructions
  2371. bool use_movnz_instructions = false;
  2372. #endif
  2373. #ifndef use_mips32_instructions
  2374. bool use_mips32_instructions = false;
  2375. #endif
  2376. #ifndef use_mips32r2_instructions
  2377. bool use_mips32r2_instructions = false;
  2378. #endif
  2379. static volatile sig_atomic_t got_sigill;
  2380. static void sigill_handler(int signo, siginfo_t *si, void *data)
  2381. {
  2382. /* Skip the faulty instruction */
  2383. ucontext_t *uc = (ucontext_t *)data;
  2384. uc->uc_mcontext.pc += 4;
  2385. got_sigill = 1;
  2386. }
  2387. static void tcg_target_detect_isa(void)
  2388. {
  2389. struct sigaction sa_old, sa_new;
  2390. memset(&sa_new, 0, sizeof(sa_new));
  2391. sa_new.sa_flags = SA_SIGINFO;
  2392. sa_new.sa_sigaction = sigill_handler;
  2393. sigaction(SIGILL, &sa_new, &sa_old);
  2394. /* Probe for movn/movz, necessary to implement movcond. */
  2395. #ifndef use_movnz_instructions
  2396. got_sigill = 0;
  2397. asm volatile(".set push\n"
  2398. ".set mips32\n"
  2399. "movn $zero, $zero, $zero\n"
  2400. "movz $zero, $zero, $zero\n"
  2401. ".set pop\n"
  2402. : : : );
  2403. use_movnz_instructions = !got_sigill;
  2404. #endif
  2405. /* Probe for MIPS32 instructions. As no subsetting is allowed
  2406. by the specification, it is only necessary to probe for one
  2407. of the instructions. */
  2408. #ifndef use_mips32_instructions
  2409. got_sigill = 0;
  2410. asm volatile(".set push\n"
  2411. ".set mips32\n"
  2412. "mul $zero, $zero\n"
  2413. ".set pop\n"
  2414. : : : );
  2415. use_mips32_instructions = !got_sigill;
  2416. #endif
  2417. /* Probe for MIPS32r2 instructions if MIPS32 instructions are
  2418. available. As no subsetting is allowed by the specification,
  2419. it is only necessary to probe for one of the instructions. */
  2420. #ifndef use_mips32r2_instructions
  2421. if (use_mips32_instructions) {
  2422. got_sigill = 0;
  2423. asm volatile(".set push\n"
  2424. ".set mips32r2\n"
  2425. "seb $zero, $zero\n"
  2426. ".set pop\n"
  2427. : : : );
  2428. use_mips32r2_instructions = !got_sigill;
  2429. }
  2430. #endif
  2431. sigaction(SIGILL, &sa_old, NULL);
  2432. }
  2433. static tcg_insn_unit *align_code_ptr(TCGContext *s)
  2434. {
  2435. uintptr_t p = (uintptr_t)s->code_ptr;
  2436. if (p & 15) {
  2437. p = (p + 15) & -16;
  2438. s->code_ptr = (void *)p;
  2439. }
  2440. return s->code_ptr;
  2441. }
  2442. /* Stack frame parameters. */
  2443. #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
  2444. #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
  2445. #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
  2446. #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
  2447. + TCG_TARGET_STACK_ALIGN - 1) \
  2448. & -TCG_TARGET_STACK_ALIGN)
  2449. #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
  2450. /* We're expecting to be able to use an immediate for frame allocation. */
  2451. QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7fff);
  2452. /* Generate global QEMU prologue and epilogue code */
  2453. static void tcg_target_qemu_prologue(TCGContext *s)
  2454. {
  2455. int i;
  2456. tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
  2457. /* TB prologue */
  2458. tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
  2459. for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
  2460. tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
  2461. TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
  2462. }
  2463. #ifndef CONFIG_SOFTMMU
  2464. if (guest_base) {
  2465. tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
  2466. tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
  2467. }
  2468. #endif
  2469. /* Call generated code */
  2470. tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0);
  2471. /* delay slot */
  2472. tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
  2473. /*
  2474. * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
  2475. * and fall through to the rest of the epilogue.
  2476. */
  2477. tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
  2478. tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_V0, TCG_REG_ZERO);
  2479. /* TB epilogue */
  2480. tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
  2481. for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
  2482. tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
  2483. TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
  2484. }
  2485. tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
  2486. /* delay slot */
  2487. tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
  2488. if (use_mips32r2_instructions) {
  2489. return;
  2490. }
  2491. /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3;
  2492. clobbers TCG_TMP1, TCG_TMP2. */
  2493. /*
  2494. * bswap32 -- 32-bit swap (signed result for mips64). a0 = abcd.
  2495. */
  2496. bswap32_addr = tcg_splitwx_to_rx(align_code_ptr(s));
  2497. /* t3 = (ssss)d000 */
  2498. tcg_out_opc_sa(s, OPC_SLL, TCG_TMP3, TCG_TMP0, 24);
  2499. /* t1 = 000a */
  2500. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24);
  2501. /* t2 = 00c0 */
  2502. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
  2503. /* t3 = d00a */
  2504. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2505. /* t1 = 0abc */
  2506. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
  2507. /* t2 = 0c00 */
  2508. tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
  2509. /* t1 = 00b0 */
  2510. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
  2511. /* t3 = dc0a */
  2512. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
  2513. tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
  2514. /* t3 = dcba -- delay slot */
  2515. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2516. if (TCG_TARGET_REG_BITS == 32) {
  2517. return;
  2518. }
  2519. /*
  2520. * bswap32u -- unsigned 32-bit swap. a0 = ....abcd.
  2521. */
  2522. bswap32u_addr = tcg_splitwx_to_rx(align_code_ptr(s));
  2523. /* t1 = (0000)000d */
  2524. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP0, 0xff);
  2525. /* t3 = 000a */
  2526. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, TCG_TMP0, 24);
  2527. /* t1 = (0000)d000 */
  2528. tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
  2529. /* t2 = 00c0 */
  2530. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
  2531. /* t3 = d00a */
  2532. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2533. /* t1 = 0abc */
  2534. tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
  2535. /* t2 = 0c00 */
  2536. tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
  2537. /* t1 = 00b0 */
  2538. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
  2539. /* t3 = dc0a */
  2540. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
  2541. tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
  2542. /* t3 = dcba -- delay slot */
  2543. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2544. /*
  2545. * bswap64 -- 64-bit swap. a0 = abcdefgh
  2546. */
  2547. bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s));
  2548. /* t3 = h0000000 */
  2549. tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56);
  2550. /* t1 = 0000000a */
  2551. tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56);
  2552. /* t2 = 000000g0 */
  2553. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
  2554. /* t3 = h000000a */
  2555. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2556. /* t1 = 00000abc */
  2557. tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40);
  2558. /* t2 = 0g000000 */
  2559. tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
  2560. /* t1 = 000000b0 */
  2561. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
  2562. /* t3 = hg00000a */
  2563. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
  2564. /* t2 = 0000abcd */
  2565. tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32);
  2566. /* t3 = hg0000ba */
  2567. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2568. /* t1 = 000000c0 */
  2569. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00);
  2570. /* t2 = 0000000d */
  2571. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff);
  2572. /* t1 = 00000c00 */
  2573. tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8);
  2574. /* t2 = 0000d000 */
  2575. tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24);
  2576. /* t3 = hg000cba */
  2577. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2578. /* t1 = 00abcdef */
  2579. tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16);
  2580. /* t3 = hg00dcba */
  2581. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
  2582. /* t2 = 0000000f */
  2583. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff);
  2584. /* t1 = 000000e0 */
  2585. tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
  2586. /* t2 = 00f00000 */
  2587. tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
  2588. /* t1 = 000e0000 */
  2589. tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
  2590. /* t3 = hgf0dcba */
  2591. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
  2592. tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
  2593. /* t3 = hgfedcba -- delay slot */
  2594. tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
  2595. }
  2596. static void tcg_target_init(TCGContext *s)
  2597. {
  2598. tcg_target_detect_isa();
  2599. tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
  2600. if (TCG_TARGET_REG_BITS == 64) {
  2601. tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
  2602. }
  2603. tcg_target_call_clobber_regs = 0;
  2604. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
  2605. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
  2606. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A0);
  2607. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A1);
  2608. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A2);
  2609. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A3);
  2610. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T0);
  2611. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T1);
  2612. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T2);
  2613. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T3);
  2614. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T4);
  2615. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T5);
  2616. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T6);
  2617. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T7);
  2618. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T8);
  2619. tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T9);
  2620. s->reserved_regs = 0;
  2621. tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); /* zero register */
  2622. tcg_regset_set_reg(s->reserved_regs, TCG_REG_K0); /* kernel use only */
  2623. tcg_regset_set_reg(s->reserved_regs, TCG_REG_K1); /* kernel use only */
  2624. tcg_regset_set_reg(s->reserved_regs, TCG_TMP0); /* internal use */
  2625. tcg_regset_set_reg(s->reserved_regs, TCG_TMP1); /* internal use */
  2626. tcg_regset_set_reg(s->reserved_regs, TCG_TMP2); /* internal use */
  2627. tcg_regset_set_reg(s->reserved_regs, TCG_TMP3); /* internal use */
  2628. tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA); /* return address */
  2629. tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); /* stack pointer */
  2630. tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); /* global pointer */
  2631. }
  2632. typedef struct {
  2633. DebugFrameHeader h;
  2634. uint8_t fde_def_cfa[4];
  2635. uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
  2636. } DebugFrame;
  2637. #define ELF_HOST_MACHINE EM_MIPS
  2638. /* GDB doesn't appear to require proper setting of ELF_HOST_FLAGS,
  2639. which is good because they're really quite complicated for MIPS. */
  2640. static const DebugFrame debug_frame = {
  2641. .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
  2642. .h.cie.id = -1,
  2643. .h.cie.version = 1,
  2644. .h.cie.code_align = 1,
  2645. .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
  2646. .h.cie.return_column = TCG_REG_RA,
  2647. /* Total FDE size does not include the "len" member. */
  2648. .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
  2649. .fde_def_cfa = {
  2650. 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
  2651. (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
  2652. (FRAME_SIZE >> 7)
  2653. },
  2654. .fde_reg_ofs = {
  2655. 0x80 + 16, 9, /* DW_CFA_offset, s0, -72 */
  2656. 0x80 + 17, 8, /* DW_CFA_offset, s2, -64 */
  2657. 0x80 + 18, 7, /* DW_CFA_offset, s3, -56 */
  2658. 0x80 + 19, 6, /* DW_CFA_offset, s4, -48 */
  2659. 0x80 + 20, 5, /* DW_CFA_offset, s5, -40 */
  2660. 0x80 + 21, 4, /* DW_CFA_offset, s6, -32 */
  2661. 0x80 + 22, 3, /* DW_CFA_offset, s7, -24 */
  2662. 0x80 + 30, 2, /* DW_CFA_offset, s8, -16 */
  2663. 0x80 + 31, 1, /* DW_CFA_offset, ra, -8 */
  2664. }
  2665. };
  2666. void tcg_register_jit(const void *buf, size_t buf_size)
  2667. {
  2668. tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
  2669. }