tcg-target.c.inc 83 KB

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