sljitNativeMIPS_common.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  1. /*
  2. * Stack-less Just-In-Time compiler
  3. *
  4. * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are
  7. * permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  13. * of conditions and the following disclaimer in the documentation and/or other materials
  14. * provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  21. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  22. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* Latest MIPS architecture. */
  27. /* Automatically detect SLJIT_MIPS_R1 */
  28. #if (defined __mips_isa_rev) && (__mips_isa_rev >= 6)
  29. #define SLJIT_MIPS_R6 1
  30. #endif
  31. SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
  32. {
  33. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  34. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  35. return "MIPS32-R6" SLJIT_CPUINFO;
  36. #else /* !SLJIT_CONFIG_MIPS_32 */
  37. return "MIPS64-R6" SLJIT_CPUINFO;
  38. #endif /* SLJIT_CONFIG_MIPS_32 */
  39. #elif (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  40. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  41. return "MIPS32-R1" SLJIT_CPUINFO;
  42. #else /* !SLJIT_CONFIG_MIPS_32 */
  43. return "MIPS64-R1" SLJIT_CPUINFO;
  44. #endif /* SLJIT_CONFIG_MIPS_32 */
  45. #else /* SLJIT_MIPS_R1 */
  46. return "MIPS III" SLJIT_CPUINFO;
  47. #endif /* SLJIT_MIPS_R6 */
  48. }
  49. /* Length of an instruction word
  50. Both for mips-32 and mips-64 */
  51. typedef sljit_u32 sljit_ins;
  52. #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
  53. #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
  54. #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
  55. /* For position independent code, t9 must contain the function address. */
  56. #define PIC_ADDR_REG TMP_REG2
  57. /* Floating point status register. */
  58. #define FCSR_REG 31
  59. /* Return address register. */
  60. #define RETURN_ADDR_REG 31
  61. /* Flags are kept in volatile registers. */
  62. #define EQUAL_FLAG 3
  63. #define OTHER_FLAG 1
  64. #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
  65. #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
  66. #define TMP_FREG3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3)
  67. static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
  68. 0, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 24, 23, 22, 21, 20, 19, 18, 17, 16, 29, 4, 25, 31
  69. };
  70. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  71. static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 4] = {
  72. 0, 0, 14, 2, 4, 6, 8, 12, 10, 16
  73. };
  74. #else
  75. static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 4] = {
  76. 0, 0, 13, 14, 15, 16, 17, 12, 18, 10
  77. };
  78. #endif
  79. /* --------------------------------------------------------------------- */
  80. /* Instrucion forms */
  81. /* --------------------------------------------------------------------- */
  82. #define S(s) (reg_map[s] << 21)
  83. #define T(t) (reg_map[t] << 16)
  84. #define D(d) (reg_map[d] << 11)
  85. #define FT(t) (freg_map[t] << 16)
  86. #define FS(s) (freg_map[s] << 11)
  87. #define FD(d) (freg_map[d] << 6)
  88. /* Absolute registers. */
  89. #define SA(s) ((s) << 21)
  90. #define TA(t) ((t) << 16)
  91. #define DA(d) ((d) << 11)
  92. #define IMM(imm) ((imm) & 0xffff)
  93. #define SH_IMM(imm) ((imm) << 6)
  94. #define DR(dr) (reg_map[dr])
  95. #define FR(dr) (freg_map[dr])
  96. #define HI(opcode) ((opcode) << 26)
  97. #define LO(opcode) (opcode)
  98. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  99. /* CMP.cond.fmt */
  100. /* S = (20 << 21) D = (21 << 21) */
  101. #define CMP_FMT_S (20 << 21)
  102. #endif /* SLJIT_MIPS_R6 */
  103. /* S = (16 << 21) D = (17 << 21) */
  104. #define FMT_S (16 << 21)
  105. #define FMT_D (17 << 21)
  106. #define ABS_S (HI(17) | FMT_S | LO(5))
  107. #define ADD_S (HI(17) | FMT_S | LO(0))
  108. #define ADDIU (HI(9))
  109. #define ADDU (HI(0) | LO(33))
  110. #define AND (HI(0) | LO(36))
  111. #define ANDI (HI(12))
  112. #define B (HI(4))
  113. #define BAL (HI(1) | (17 << 16))
  114. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  115. #define BC1EQZ (HI(17) | (9 << 21) | FT(TMP_FREG3))
  116. #define BC1NEZ (HI(17) | (13 << 21) | FT(TMP_FREG3))
  117. #else /* !SLJIT_MIPS_R6 */
  118. #define BC1F (HI(17) | (8 << 21))
  119. #define BC1T (HI(17) | (8 << 21) | (1 << 16))
  120. #endif /* SLJIT_MIPS_R6 */
  121. #define BEQ (HI(4))
  122. #define BGEZ (HI(1) | (1 << 16))
  123. #define BGTZ (HI(7))
  124. #define BLEZ (HI(6))
  125. #define BLTZ (HI(1) | (0 << 16))
  126. #define BNE (HI(5))
  127. #define BREAK (HI(0) | LO(13))
  128. #define CFC1 (HI(17) | (2 << 21))
  129. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  130. #define C_UEQ_S (HI(17) | CMP_FMT_S | LO(3))
  131. #define C_ULE_S (HI(17) | CMP_FMT_S | LO(7))
  132. #define C_ULT_S (HI(17) | CMP_FMT_S | LO(5))
  133. #define C_UN_S (HI(17) | CMP_FMT_S | LO(1))
  134. #define C_FD (FD(TMP_FREG3))
  135. #else /* !SLJIT_MIPS_R6 */
  136. #define C_UEQ_S (HI(17) | FMT_S | LO(51))
  137. #define C_ULE_S (HI(17) | FMT_S | LO(55))
  138. #define C_ULT_S (HI(17) | FMT_S | LO(53))
  139. #define C_UN_S (HI(17) | FMT_S | LO(49))
  140. #define C_FD (0)
  141. #endif /* SLJIT_MIPS_R6 */
  142. #define CVT_S_S (HI(17) | FMT_S | LO(32))
  143. #define DADDIU (HI(25))
  144. #define DADDU (HI(0) | LO(45))
  145. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  146. #define DDIV (HI(0) | (2 << 6) | LO(30))
  147. #define DDIVU (HI(0) | (2 << 6) | LO(31))
  148. #define DMOD (HI(0) | (3 << 6) | LO(30))
  149. #define DMODU (HI(0) | (3 << 6) | LO(31))
  150. #define DIV (HI(0) | (2 << 6) | LO(26))
  151. #define DIVU (HI(0) | (2 << 6) | LO(27))
  152. #define DMUH (HI(0) | (3 << 6) | LO(28))
  153. #define DMUHU (HI(0) | (3 << 6) | LO(29))
  154. #define DMUL (HI(0) | (2 << 6) | LO(28))
  155. #define DMULU (HI(0) | (2 << 6) | LO(29))
  156. #else /* !SLJIT_MIPS_R6 */
  157. #define DDIV (HI(0) | LO(30))
  158. #define DDIVU (HI(0) | LO(31))
  159. #define DIV (HI(0) | LO(26))
  160. #define DIVU (HI(0) | LO(27))
  161. #define DMULT (HI(0) | LO(28))
  162. #define DMULTU (HI(0) | LO(29))
  163. #endif /* SLJIT_MIPS_R6 */
  164. #define DIV_S (HI(17) | FMT_S | LO(3))
  165. #define DSLL (HI(0) | LO(56))
  166. #define DSLL32 (HI(0) | LO(60))
  167. #define DSLLV (HI(0) | LO(20))
  168. #define DSRA (HI(0) | LO(59))
  169. #define DSRA32 (HI(0) | LO(63))
  170. #define DSRAV (HI(0) | LO(23))
  171. #define DSRL (HI(0) | LO(58))
  172. #define DSRL32 (HI(0) | LO(62))
  173. #define DSRLV (HI(0) | LO(22))
  174. #define DSUBU (HI(0) | LO(47))
  175. #define J (HI(2))
  176. #define JAL (HI(3))
  177. #define JALR (HI(0) | LO(9))
  178. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  179. #define JR (HI(0) | LO(9))
  180. #else /* !SLJIT_MIPS_R6 */
  181. #define JR (HI(0) | LO(8))
  182. #endif /* SLJIT_MIPS_R6 */
  183. #define LD (HI(55))
  184. #define LUI (HI(15))
  185. #define LW (HI(35))
  186. #define MFC1 (HI(17))
  187. #if !(defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  188. #define MFHI (HI(0) | LO(16))
  189. #define MFLO (HI(0) | LO(18))
  190. #else /* SLJIT_MIPS_R6 */
  191. #define MOD (HI(0) | (3 << 6) | LO(26))
  192. #define MODU (HI(0) | (3 << 6) | LO(27))
  193. #endif /* !SLJIT_MIPS_R6 */
  194. #define MOV_S (HI(17) | FMT_S | LO(6))
  195. #define MTC1 (HI(17) | (4 << 21))
  196. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  197. #define MUH (HI(0) | (3 << 6) | LO(24))
  198. #define MUHU (HI(0) | (3 << 6) | LO(25))
  199. #define MUL (HI(0) | (2 << 6) | LO(24))
  200. #define MULU (HI(0) | (2 << 6) | LO(25))
  201. #else /* !SLJIT_MIPS_R6 */
  202. #define MULT (HI(0) | LO(24))
  203. #define MULTU (HI(0) | LO(25))
  204. #endif /* SLJIT_MIPS_R6 */
  205. #define MUL_S (HI(17) | FMT_S | LO(2))
  206. #define NEG_S (HI(17) | FMT_S | LO(7))
  207. #define NOP (HI(0) | LO(0))
  208. #define NOR (HI(0) | LO(39))
  209. #define OR (HI(0) | LO(37))
  210. #define ORI (HI(13))
  211. #define SD (HI(63))
  212. #define SDC1 (HI(61))
  213. #define SLT (HI(0) | LO(42))
  214. #define SLTI (HI(10))
  215. #define SLTIU (HI(11))
  216. #define SLTU (HI(0) | LO(43))
  217. #define SLL (HI(0) | LO(0))
  218. #define SLLV (HI(0) | LO(4))
  219. #define SRL (HI(0) | LO(2))
  220. #define SRLV (HI(0) | LO(6))
  221. #define SRA (HI(0) | LO(3))
  222. #define SRAV (HI(0) | LO(7))
  223. #define SUB_S (HI(17) | FMT_S | LO(1))
  224. #define SUBU (HI(0) | LO(35))
  225. #define SW (HI(43))
  226. #define SWC1 (HI(57))
  227. #define TRUNC_W_S (HI(17) | FMT_S | LO(13))
  228. #define XOR (HI(0) | LO(38))
  229. #define XORI (HI(14))
  230. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1) || (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  231. #define CLZ (HI(28) | LO(32))
  232. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  233. #define DCLZ (LO(18))
  234. #else /* !SLJIT_MIPS_R6 */
  235. #define DCLZ (HI(28) | LO(36))
  236. #define MOVF (HI(0) | (0 << 16) | LO(1))
  237. #define MOVN (HI(0) | LO(11))
  238. #define MOVT (HI(0) | (1 << 16) | LO(1))
  239. #define MOVZ (HI(0) | LO(10))
  240. #define MUL (HI(28) | LO(2))
  241. #endif /* SLJIT_MIPS_R6 */
  242. #define PREF (HI(51))
  243. #define PREFX (HI(19) | LO(15))
  244. #define SEB (HI(31) | (16 << 6) | LO(32))
  245. #define SEH (HI(31) | (24 << 6) | LO(32))
  246. #endif
  247. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  248. #define ADDU_W ADDU
  249. #define ADDIU_W ADDIU
  250. #define SLL_W SLL
  251. #define SUBU_W SUBU
  252. #else
  253. #define ADDU_W DADDU
  254. #define ADDIU_W DADDIU
  255. #define SLL_W DSLL
  256. #define SUBU_W DSUBU
  257. #endif
  258. #define SIMM_MAX (0x7fff)
  259. #define SIMM_MIN (-0x8000)
  260. #define UIMM_MAX (0xffff)
  261. /* dest_reg is the absolute name of the register
  262. Useful for reordering instructions in the delay slot. */
  263. static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_ins ins, sljit_s32 delay_slot)
  264. {
  265. SLJIT_ASSERT(delay_slot == MOVABLE_INS || delay_slot >= UNMOVABLE_INS
  266. || delay_slot == ((ins >> 11) & 0x1f) || delay_slot == ((ins >> 16) & 0x1f));
  267. sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
  268. FAIL_IF(!ptr);
  269. *ptr = ins;
  270. compiler->size++;
  271. compiler->delay_slot = delay_slot;
  272. return SLJIT_SUCCESS;
  273. }
  274. static SLJIT_INLINE sljit_ins invert_branch(sljit_s32 flags)
  275. {
  276. if (flags & IS_BIT26_COND)
  277. return (1 << 26);
  278. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  279. if (flags & IS_BIT23_COND)
  280. return (1 << 23);
  281. #endif /* SLJIT_MIPS_R6 */
  282. return (1 << 16);
  283. }
  284. static SLJIT_INLINE sljit_ins* detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code, sljit_sw executable_offset)
  285. {
  286. sljit_sw diff;
  287. sljit_uw target_addr;
  288. sljit_ins *inst;
  289. sljit_ins saved_inst;
  290. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  291. if (jump->flags & (SLJIT_REWRITABLE_JUMP | IS_CALL))
  292. return code_ptr;
  293. #else
  294. if (jump->flags & SLJIT_REWRITABLE_JUMP)
  295. return code_ptr;
  296. #endif
  297. if (jump->flags & JUMP_ADDR)
  298. target_addr = jump->u.target;
  299. else {
  300. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  301. target_addr = (sljit_uw)(code + jump->u.label->size) + (sljit_uw)executable_offset;
  302. }
  303. inst = (sljit_ins *)jump->addr;
  304. if (jump->flags & IS_COND)
  305. inst--;
  306. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  307. if (jump->flags & IS_CALL)
  308. goto keep_address;
  309. #endif
  310. /* B instructions. */
  311. if (jump->flags & IS_MOVABLE) {
  312. diff = ((sljit_sw)target_addr - (sljit_sw)inst - executable_offset) >> 2;
  313. if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
  314. jump->flags |= PATCH_B;
  315. if (!(jump->flags & IS_COND)) {
  316. inst[0] = inst[-1];
  317. inst[-1] = (jump->flags & IS_JAL) ? BAL : B;
  318. jump->addr -= sizeof(sljit_ins);
  319. return inst;
  320. }
  321. saved_inst = inst[0];
  322. inst[0] = inst[-1];
  323. inst[-1] = saved_inst ^ invert_branch(jump->flags);
  324. jump->addr -= 2 * sizeof(sljit_ins);
  325. return inst;
  326. }
  327. }
  328. else {
  329. diff = ((sljit_sw)target_addr - (sljit_sw)(inst + 1) - executable_offset) >> 2;
  330. if (diff <= SIMM_MAX && diff >= SIMM_MIN) {
  331. jump->flags |= PATCH_B;
  332. if (!(jump->flags & IS_COND)) {
  333. inst[0] = (jump->flags & IS_JAL) ? BAL : B;
  334. inst[1] = NOP;
  335. return inst + 1;
  336. }
  337. inst[0] = inst[0] ^ invert_branch(jump->flags);
  338. inst[1] = NOP;
  339. jump->addr -= sizeof(sljit_ins);
  340. return inst + 1;
  341. }
  342. }
  343. if (jump->flags & IS_COND) {
  344. if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == ((jump->addr + 2 * sizeof(sljit_ins)) & ~0xfffffff)) {
  345. jump->flags |= PATCH_J;
  346. saved_inst = inst[0];
  347. inst[0] = inst[-1];
  348. inst[-1] = (saved_inst & 0xffff0000) | 3;
  349. inst[1] = J;
  350. inst[2] = NOP;
  351. return inst + 2;
  352. }
  353. else if ((target_addr & ~0xfffffff) == ((jump->addr + 3 * sizeof(sljit_ins)) & ~0xfffffff)) {
  354. jump->flags |= PATCH_J;
  355. inst[0] = (inst[0] & 0xffff0000) | 3;
  356. inst[1] = NOP;
  357. inst[2] = J;
  358. inst[3] = NOP;
  359. jump->addr += sizeof(sljit_ins);
  360. return inst + 3;
  361. }
  362. }
  363. else {
  364. /* J instuctions. */
  365. if ((jump->flags & IS_MOVABLE) && (target_addr & ~0xfffffff) == (jump->addr & ~0xfffffff)) {
  366. jump->flags |= PATCH_J;
  367. inst[0] = inst[-1];
  368. inst[-1] = (jump->flags & IS_JAL) ? JAL : J;
  369. jump->addr -= sizeof(sljit_ins);
  370. return inst;
  371. }
  372. if ((target_addr & ~0xfffffff) == ((jump->addr + sizeof(sljit_ins)) & ~0xfffffff)) {
  373. jump->flags |= PATCH_J;
  374. inst[0] = (jump->flags & IS_JAL) ? JAL : J;
  375. inst[1] = NOP;
  376. return inst + 1;
  377. }
  378. }
  379. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  380. keep_address:
  381. if (target_addr <= 0x7fffffff) {
  382. jump->flags |= PATCH_ABS32;
  383. if (jump->flags & IS_COND) {
  384. inst[0] -= 4;
  385. inst++;
  386. }
  387. inst[2] = inst[6];
  388. inst[3] = inst[7];
  389. return inst + 3;
  390. }
  391. if (target_addr <= 0x7fffffffffffl) {
  392. jump->flags |= PATCH_ABS48;
  393. if (jump->flags & IS_COND) {
  394. inst[0] -= 2;
  395. inst++;
  396. }
  397. inst[4] = inst[6];
  398. inst[5] = inst[7];
  399. return inst + 5;
  400. }
  401. #endif
  402. return code_ptr;
  403. }
  404. #ifdef __GNUC__
  405. static __attribute__ ((noinline)) void sljit_cache_flush(void* code, void* code_ptr)
  406. {
  407. SLJIT_CACHE_FLUSH(code, code_ptr);
  408. }
  409. #endif
  410. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  411. static SLJIT_INLINE sljit_sw put_label_get_length(struct sljit_put_label *put_label, sljit_uw max_label)
  412. {
  413. if (max_label < 0x80000000l) {
  414. put_label->flags = 0;
  415. return 1;
  416. }
  417. if (max_label < 0x800000000000l) {
  418. put_label->flags = 1;
  419. return 3;
  420. }
  421. put_label->flags = 2;
  422. return 5;
  423. }
  424. static SLJIT_INLINE void put_label_set(struct sljit_put_label *put_label)
  425. {
  426. sljit_uw addr = put_label->label->addr;
  427. sljit_ins *inst = (sljit_ins *)put_label->addr;
  428. sljit_s32 reg = *inst;
  429. if (put_label->flags == 0) {
  430. SLJIT_ASSERT(addr < 0x80000000l);
  431. inst[0] = LUI | T(reg) | IMM(addr >> 16);
  432. }
  433. else if (put_label->flags == 1) {
  434. SLJIT_ASSERT(addr < 0x800000000000l);
  435. inst[0] = LUI | T(reg) | IMM(addr >> 32);
  436. inst[1] = ORI | S(reg) | T(reg) | IMM((addr >> 16) & 0xffff);
  437. inst[2] = DSLL | T(reg) | D(reg) | SH_IMM(16);
  438. inst += 2;
  439. }
  440. else {
  441. inst[0] = LUI | T(reg) | IMM(addr >> 48);
  442. inst[1] = ORI | S(reg) | T(reg) | IMM((addr >> 32) & 0xffff);
  443. inst[2] = DSLL | T(reg) | D(reg) | SH_IMM(16);
  444. inst[3] = ORI | S(reg) | T(reg) | IMM((addr >> 16) & 0xffff);
  445. inst[4] = DSLL | T(reg) | D(reg) | SH_IMM(16);
  446. inst += 4;
  447. }
  448. inst[1] = ORI | S(reg) | T(reg) | IMM(addr & 0xffff);
  449. }
  450. #endif
  451. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
  452. {
  453. struct sljit_memory_fragment *buf;
  454. sljit_ins *code;
  455. sljit_ins *code_ptr;
  456. sljit_ins *buf_ptr;
  457. sljit_ins *buf_end;
  458. sljit_uw word_count;
  459. sljit_uw next_addr;
  460. sljit_sw executable_offset;
  461. sljit_uw addr;
  462. struct sljit_label *label;
  463. struct sljit_jump *jump;
  464. struct sljit_const *const_;
  465. struct sljit_put_label *put_label;
  466. CHECK_ERROR_PTR();
  467. CHECK_PTR(check_sljit_generate_code(compiler));
  468. reverse_buf(compiler);
  469. code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
  470. PTR_FAIL_WITH_EXEC_IF(code);
  471. buf = compiler->buf;
  472. code_ptr = code;
  473. word_count = 0;
  474. next_addr = 0;
  475. executable_offset = SLJIT_EXEC_OFFSET(code);
  476. label = compiler->labels;
  477. jump = compiler->jumps;
  478. const_ = compiler->consts;
  479. put_label = compiler->put_labels;
  480. do {
  481. buf_ptr = (sljit_ins*)buf->memory;
  482. buf_end = buf_ptr + (buf->used_size >> 2);
  483. do {
  484. *code_ptr = *buf_ptr++;
  485. if (next_addr == word_count) {
  486. SLJIT_ASSERT(!label || label->size >= word_count);
  487. SLJIT_ASSERT(!jump || jump->addr >= word_count);
  488. SLJIT_ASSERT(!const_ || const_->addr >= word_count);
  489. SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
  490. /* These structures are ordered by their address. */
  491. if (label && label->size == word_count) {
  492. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  493. label->size = code_ptr - code;
  494. label = label->next;
  495. }
  496. if (jump && jump->addr == word_count) {
  497. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  498. jump->addr = (sljit_uw)(code_ptr - 3);
  499. #else
  500. jump->addr = (sljit_uw)(code_ptr - 7);
  501. #endif
  502. code_ptr = detect_jump_type(jump, code_ptr, code, executable_offset);
  503. jump = jump->next;
  504. }
  505. if (const_ && const_->addr == word_count) {
  506. const_->addr = (sljit_uw)code_ptr;
  507. const_ = const_->next;
  508. }
  509. if (put_label && put_label->addr == word_count) {
  510. SLJIT_ASSERT(put_label->label);
  511. put_label->addr = (sljit_uw)code_ptr;
  512. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  513. code_ptr += put_label_get_length(put_label, (sljit_uw)(SLJIT_ADD_EXEC_OFFSET(code, executable_offset) + put_label->label->size));
  514. word_count += 5;
  515. #endif
  516. put_label = put_label->next;
  517. }
  518. next_addr = compute_next_addr(label, jump, const_, put_label);
  519. }
  520. code_ptr ++;
  521. word_count ++;
  522. } while (buf_ptr < buf_end);
  523. buf = buf->next;
  524. } while (buf);
  525. if (label && label->size == word_count) {
  526. label->addr = (sljit_uw)code_ptr;
  527. label->size = code_ptr - code;
  528. label = label->next;
  529. }
  530. SLJIT_ASSERT(!label);
  531. SLJIT_ASSERT(!jump);
  532. SLJIT_ASSERT(!const_);
  533. SLJIT_ASSERT(!put_label);
  534. SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
  535. jump = compiler->jumps;
  536. while (jump) {
  537. do {
  538. addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
  539. buf_ptr = (sljit_ins *)jump->addr;
  540. if (jump->flags & PATCH_B) {
  541. addr = (sljit_sw)(addr - ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset) + sizeof(sljit_ins))) >> 2;
  542. SLJIT_ASSERT((sljit_sw)addr <= SIMM_MAX && (sljit_sw)addr >= SIMM_MIN);
  543. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | (addr & 0xffff);
  544. break;
  545. }
  546. if (jump->flags & PATCH_J) {
  547. SLJIT_ASSERT((addr & ~0xfffffff) == (((sljit_uw)SLJIT_ADD_EXEC_OFFSET(buf_ptr, executable_offset) + sizeof(sljit_ins)) & ~0xfffffff));
  548. buf_ptr[0] |= (addr >> 2) & 0x03ffffff;
  549. break;
  550. }
  551. /* Set the fields of immediate loads. */
  552. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  553. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
  554. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
  555. #else
  556. if (jump->flags & PATCH_ABS32) {
  557. SLJIT_ASSERT(addr <= 0x7fffffff);
  558. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
  559. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
  560. }
  561. else if (jump->flags & PATCH_ABS48) {
  562. SLJIT_ASSERT(addr <= 0x7fffffffffffl);
  563. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 32) & 0xffff);
  564. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 16) & 0xffff);
  565. buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | (addr & 0xffff);
  566. }
  567. else {
  568. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
  569. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
  570. buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
  571. buf_ptr[5] = (buf_ptr[5] & 0xffff0000) | (addr & 0xffff);
  572. }
  573. #endif
  574. } while (0);
  575. jump = jump->next;
  576. }
  577. put_label = compiler->put_labels;
  578. while (put_label) {
  579. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  580. addr = put_label->label->addr;
  581. buf_ptr = (sljit_ins *)put_label->addr;
  582. SLJIT_ASSERT((buf_ptr[0] & 0xffe00000) == LUI && (buf_ptr[1] & 0xfc000000) == ORI);
  583. buf_ptr[0] |= (addr >> 16) & 0xffff;
  584. buf_ptr[1] |= addr & 0xffff;
  585. #else
  586. put_label_set(put_label);
  587. #endif
  588. put_label = put_label->next;
  589. }
  590. compiler->error = SLJIT_ERR_COMPILED;
  591. compiler->executable_offset = executable_offset;
  592. compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
  593. code = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  594. code_ptr = (sljit_ins *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  595. #ifndef __GNUC__
  596. SLJIT_CACHE_FLUSH(code, code_ptr);
  597. #else
  598. /* GCC workaround for invalid code generation with -O2. */
  599. sljit_cache_flush(code, code_ptr);
  600. #endif
  601. return code;
  602. }
  603. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
  604. {
  605. sljit_sw fir = 0;
  606. switch (feature_type) {
  607. case SLJIT_HAS_FPU:
  608. #ifdef SLJIT_IS_FPU_AVAILABLE
  609. return SLJIT_IS_FPU_AVAILABLE;
  610. #elif defined(__GNUC__)
  611. asm ("cfc1 %0, $0" : "=r"(fir));
  612. return (fir >> 22) & 0x1;
  613. #else
  614. #error "FIR check is not implemented for this architecture"
  615. #endif
  616. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  617. case SLJIT_HAS_CLZ:
  618. case SLJIT_HAS_CMOV:
  619. return 1;
  620. #endif
  621. default:
  622. return fir;
  623. }
  624. }
  625. /* --------------------------------------------------------------------- */
  626. /* Entry, exit */
  627. /* --------------------------------------------------------------------- */
  628. /* Creates an index in data_transfer_insts array. */
  629. #define LOAD_DATA 0x01
  630. #define WORD_DATA 0x00
  631. #define BYTE_DATA 0x02
  632. #define HALF_DATA 0x04
  633. #define INT_DATA 0x06
  634. #define SIGNED_DATA 0x08
  635. /* Separates integer and floating point registers */
  636. #define GPR_REG 0x0f
  637. #define DOUBLE_DATA 0x10
  638. #define SINGLE_DATA 0x12
  639. #define MEM_MASK 0x1f
  640. #define ARG_TEST 0x00020
  641. #define ALT_KEEP_CACHE 0x00040
  642. #define CUMULATIVE_OP 0x00080
  643. #define LOGICAL_OP 0x00100
  644. #define IMM_OP 0x00200
  645. #define SRC2_IMM 0x00400
  646. #define UNUSED_DEST 0x00800
  647. #define REG_DEST 0x01000
  648. #define REG1_SOURCE 0x02000
  649. #define REG2_SOURCE 0x04000
  650. #define SLOW_SRC1 0x08000
  651. #define SLOW_SRC2 0x10000
  652. #define SLOW_DEST 0x20000
  653. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  654. #define STACK_STORE SW
  655. #define STACK_LOAD LW
  656. #else
  657. #define STACK_STORE SD
  658. #define STACK_LOAD LD
  659. #endif
  660. static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw);
  661. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  662. #include "sljitNativeMIPS_32.c"
  663. #else
  664. #include "sljitNativeMIPS_64.c"
  665. #endif
  666. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  667. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  668. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  669. {
  670. sljit_ins base;
  671. sljit_s32 args, i, tmp, offs;
  672. CHECK_ERROR();
  673. CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  674. set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  675. local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
  676. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  677. local_size = (local_size + 15) & ~0xf;
  678. #else
  679. local_size = (local_size + 31) & ~0x1f;
  680. #endif
  681. compiler->local_size = local_size;
  682. if (local_size <= SIMM_MAX) {
  683. /* Frequent case. */
  684. FAIL_IF(push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(-local_size), DR(SLJIT_SP)));
  685. base = S(SLJIT_SP);
  686. offs = local_size - (sljit_sw)sizeof(sljit_sw);
  687. }
  688. else {
  689. FAIL_IF(load_immediate(compiler, DR(OTHER_FLAG), local_size));
  690. FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
  691. FAIL_IF(push_inst(compiler, SUBU_W | S(SLJIT_SP) | T(OTHER_FLAG) | D(SLJIT_SP), DR(SLJIT_SP)));
  692. base = S(TMP_REG2);
  693. local_size = 0;
  694. offs = -(sljit_sw)sizeof(sljit_sw);
  695. }
  696. FAIL_IF(push_inst(compiler, STACK_STORE | base | TA(RETURN_ADDR_REG) | IMM(offs), MOVABLE_INS));
  697. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  698. for (i = SLJIT_S0; i >= tmp; i--) {
  699. offs -= (sljit_s32)(sizeof(sljit_sw));
  700. FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
  701. }
  702. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
  703. offs -= (sljit_s32)(sizeof(sljit_sw));
  704. FAIL_IF(push_inst(compiler, STACK_STORE | base | T(i) | IMM(offs), MOVABLE_INS));
  705. }
  706. args = get_arg_count(arg_types);
  707. if (args >= 1)
  708. FAIL_IF(push_inst(compiler, ADDU_W | SA(4) | TA(0) | D(SLJIT_S0), DR(SLJIT_S0)));
  709. if (args >= 2)
  710. FAIL_IF(push_inst(compiler, ADDU_W | SA(5) | TA(0) | D(SLJIT_S1), DR(SLJIT_S1)));
  711. if (args >= 3)
  712. FAIL_IF(push_inst(compiler, ADDU_W | SA(6) | TA(0) | D(SLJIT_S2), DR(SLJIT_S2)));
  713. return SLJIT_SUCCESS;
  714. }
  715. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  716. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  717. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  718. {
  719. CHECK_ERROR();
  720. CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  721. set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  722. local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
  723. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  724. compiler->local_size = (local_size + 15) & ~0xf;
  725. #else
  726. compiler->local_size = (local_size + 31) & ~0x1f;
  727. #endif
  728. return SLJIT_SUCCESS;
  729. }
  730. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  731. {
  732. sljit_s32 local_size, i, tmp, offs;
  733. sljit_ins base;
  734. CHECK_ERROR();
  735. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  736. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  737. local_size = compiler->local_size;
  738. if (local_size <= SIMM_MAX)
  739. base = S(SLJIT_SP);
  740. else {
  741. FAIL_IF(load_immediate(compiler, DR(TMP_REG1), local_size));
  742. FAIL_IF(push_inst(compiler, ADDU_W | S(SLJIT_SP) | T(TMP_REG1) | D(TMP_REG1), DR(TMP_REG1)));
  743. base = S(TMP_REG1);
  744. local_size = 0;
  745. }
  746. FAIL_IF(push_inst(compiler, STACK_LOAD | base | TA(RETURN_ADDR_REG) | IMM(local_size - (sljit_s32)sizeof(sljit_sw)), RETURN_ADDR_REG));
  747. offs = local_size - (sljit_s32)GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1);
  748. tmp = compiler->scratches;
  749. for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
  750. FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
  751. offs += (sljit_s32)(sizeof(sljit_sw));
  752. }
  753. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  754. for (i = tmp; i <= SLJIT_S0; i++) {
  755. FAIL_IF(push_inst(compiler, STACK_LOAD | base | T(i) | IMM(offs), DR(i)));
  756. offs += (sljit_s32)(sizeof(sljit_sw));
  757. }
  758. SLJIT_ASSERT(offs == local_size - (sljit_sw)(sizeof(sljit_sw)));
  759. FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
  760. if (compiler->local_size <= SIMM_MAX)
  761. return push_inst(compiler, ADDIU_W | S(SLJIT_SP) | T(SLJIT_SP) | IMM(compiler->local_size), UNMOVABLE_INS);
  762. else
  763. return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_SP), UNMOVABLE_INS);
  764. }
  765. #undef STACK_STORE
  766. #undef STACK_LOAD
  767. /* --------------------------------------------------------------------- */
  768. /* Operators */
  769. /* --------------------------------------------------------------------- */
  770. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  771. #define ARCH_32_64(a, b) a
  772. #else
  773. #define ARCH_32_64(a, b) b
  774. #endif
  775. static const sljit_ins data_transfer_insts[16 + 4] = {
  776. /* u w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
  777. /* u w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
  778. /* u b s */ HI(40) /* sb */,
  779. /* u b l */ HI(36) /* lbu */,
  780. /* u h s */ HI(41) /* sh */,
  781. /* u h l */ HI(37) /* lhu */,
  782. /* u i s */ HI(43) /* sw */,
  783. /* u i l */ ARCH_32_64(HI(35) /* lw */, HI(39) /* lwu */),
  784. /* s w s */ ARCH_32_64(HI(43) /* sw */, HI(63) /* sd */),
  785. /* s w l */ ARCH_32_64(HI(35) /* lw */, HI(55) /* ld */),
  786. /* s b s */ HI(40) /* sb */,
  787. /* s b l */ HI(32) /* lb */,
  788. /* s h s */ HI(41) /* sh */,
  789. /* s h l */ HI(33) /* lh */,
  790. /* s i s */ HI(43) /* sw */,
  791. /* s i l */ HI(35) /* lw */,
  792. /* d s */ HI(61) /* sdc1 */,
  793. /* d l */ HI(53) /* ldc1 */,
  794. /* s s */ HI(57) /* swc1 */,
  795. /* s l */ HI(49) /* lwc1 */,
  796. };
  797. #undef ARCH_32_64
  798. /* reg_ar is an absoulute register! */
  799. /* Can perform an operation using at most 1 instruction. */
  800. static sljit_s32 getput_arg_fast(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw)
  801. {
  802. SLJIT_ASSERT(arg & SLJIT_MEM);
  803. if (!(arg & OFFS_REG_MASK) && argw <= SIMM_MAX && argw >= SIMM_MIN) {
  804. /* Works for both absoulte and relative addresses. */
  805. if (SLJIT_UNLIKELY(flags & ARG_TEST))
  806. return 1;
  807. FAIL_IF(push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(arg & REG_MASK)
  808. | TA(reg_ar) | IMM(argw), ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) ? reg_ar : MOVABLE_INS));
  809. return -1;
  810. }
  811. return 0;
  812. }
  813. /* See getput_arg below.
  814. Note: can_cache is called only for binary operators. Those
  815. operators always uses word arguments without write back. */
  816. static sljit_s32 can_cache(sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
  817. {
  818. SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
  819. /* Simple operation except for updates. */
  820. if (arg & OFFS_REG_MASK) {
  821. argw &= 0x3;
  822. next_argw &= 0x3;
  823. if (argw && argw == next_argw && (arg == next_arg || (arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK)))
  824. return 1;
  825. return 0;
  826. }
  827. if (arg == next_arg) {
  828. if (((next_argw - argw) <= SIMM_MAX && (next_argw - argw) >= SIMM_MIN))
  829. return 1;
  830. return 0;
  831. }
  832. return 0;
  833. }
  834. /* Emit the necessary instructions. See can_cache above. */
  835. static sljit_s32 getput_arg(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw, sljit_s32 next_arg, sljit_sw next_argw)
  836. {
  837. sljit_s32 tmp_ar, base, delay_slot;
  838. SLJIT_ASSERT(arg & SLJIT_MEM);
  839. if (!(next_arg & SLJIT_MEM)) {
  840. next_arg = 0;
  841. next_argw = 0;
  842. }
  843. if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
  844. tmp_ar = reg_ar;
  845. delay_slot = reg_ar;
  846. }
  847. else {
  848. tmp_ar = DR(TMP_REG1);
  849. delay_slot = MOVABLE_INS;
  850. }
  851. base = arg & REG_MASK;
  852. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  853. argw &= 0x3;
  854. /* Using the cache. */
  855. if (argw == compiler->cache_argw) {
  856. if (arg == compiler->cache_arg)
  857. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
  858. if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg) {
  859. if (arg == next_arg && argw == (next_argw & 0x3)) {
  860. compiler->cache_arg = arg;
  861. compiler->cache_argw = argw;
  862. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
  863. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
  864. }
  865. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(TMP_REG3) | DA(tmp_ar), tmp_ar));
  866. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
  867. }
  868. }
  869. if (SLJIT_UNLIKELY(argw)) {
  870. compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
  871. compiler->cache_argw = argw;
  872. FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | D(TMP_REG3) | SH_IMM(argw), DR(TMP_REG3)));
  873. }
  874. if (arg == next_arg && argw == (next_argw & 0x3)) {
  875. compiler->cache_arg = arg;
  876. compiler->cache_argw = argw;
  877. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | D(TMP_REG3), DR(TMP_REG3)));
  878. tmp_ar = DR(TMP_REG3);
  879. }
  880. else
  881. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(!argw ? OFFS_REG(arg) : TMP_REG3) | DA(tmp_ar), tmp_ar));
  882. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
  883. }
  884. if (compiler->cache_arg == arg && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
  885. if (argw != compiler->cache_argw) {
  886. FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
  887. compiler->cache_argw = argw;
  888. }
  889. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
  890. }
  891. if (compiler->cache_arg == SLJIT_MEM && argw - compiler->cache_argw <= SIMM_MAX && argw - compiler->cache_argw >= SIMM_MIN) {
  892. if (argw != compiler->cache_argw)
  893. FAIL_IF(push_inst(compiler, ADDIU_W | S(TMP_REG3) | T(TMP_REG3) | IMM(argw - compiler->cache_argw), DR(TMP_REG3)));
  894. }
  895. else {
  896. compiler->cache_arg = SLJIT_MEM;
  897. FAIL_IF(load_immediate(compiler, DR(TMP_REG3), argw));
  898. }
  899. compiler->cache_argw = argw;
  900. if (!base)
  901. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
  902. if (arg == next_arg && next_argw - argw <= SIMM_MAX && next_argw - argw >= SIMM_MIN) {
  903. compiler->cache_arg = arg;
  904. FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | D(TMP_REG3), DR(TMP_REG3)));
  905. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | S(TMP_REG3) | TA(reg_ar), delay_slot);
  906. }
  907. FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | T(base) | DA(tmp_ar), tmp_ar));
  908. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
  909. }
  910. static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg_ar, sljit_s32 arg, sljit_sw argw)
  911. {
  912. sljit_s32 tmp_ar, base, delay_slot;
  913. if (getput_arg_fast(compiler, flags, reg_ar, arg, argw))
  914. return compiler->error;
  915. if ((flags & MEM_MASK) <= GPR_REG && (flags & LOAD_DATA)) {
  916. tmp_ar = reg_ar;
  917. delay_slot = reg_ar;
  918. }
  919. else {
  920. tmp_ar = DR(TMP_REG1);
  921. delay_slot = MOVABLE_INS;
  922. }
  923. base = arg & REG_MASK;
  924. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  925. argw &= 0x3;
  926. if (SLJIT_UNLIKELY(argw)) {
  927. FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(arg)) | DA(tmp_ar) | SH_IMM(argw), tmp_ar));
  928. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | TA(tmp_ar) | DA(tmp_ar), tmp_ar));
  929. }
  930. else
  931. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | T(OFFS_REG(arg)) | DA(tmp_ar), tmp_ar));
  932. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
  933. }
  934. FAIL_IF(load_immediate(compiler, tmp_ar, argw));
  935. if (base != 0)
  936. FAIL_IF(push_inst(compiler, ADDU_W | S(base) | TA(tmp_ar) | DA(tmp_ar), tmp_ar));
  937. return push_inst(compiler, data_transfer_insts[flags & MEM_MASK] | SA(tmp_ar) | TA(reg_ar), delay_slot);
  938. }
  939. static SLJIT_INLINE sljit_s32 emit_op_mem2(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg1, sljit_sw arg1w, sljit_s32 arg2, sljit_sw arg2w)
  940. {
  941. if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
  942. return compiler->error;
  943. return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
  944. }
  945. static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  946. sljit_s32 dst, sljit_sw dstw,
  947. sljit_s32 src1, sljit_sw src1w,
  948. sljit_s32 src2, sljit_sw src2w)
  949. {
  950. /* arg1 goes to TMP_REG1 or src reg
  951. arg2 goes to TMP_REG2, imm or src reg
  952. TMP_REG3 can be used for caching
  953. result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
  954. sljit_s32 dst_r = TMP_REG2;
  955. sljit_s32 src1_r;
  956. sljit_sw src2_r = 0;
  957. sljit_s32 sugg_src2_r = TMP_REG2;
  958. if (!(flags & ALT_KEEP_CACHE)) {
  959. compiler->cache_arg = 0;
  960. compiler->cache_argw = 0;
  961. }
  962. if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
  963. SLJIT_ASSERT(HAS_FLAGS(op));
  964. flags |= UNUSED_DEST;
  965. }
  966. else if (FAST_IS_REG(dst)) {
  967. dst_r = dst;
  968. flags |= REG_DEST;
  969. if (op >= SLJIT_MOV && op <= SLJIT_MOV_P)
  970. sugg_src2_r = dst_r;
  971. }
  972. else if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, flags | ARG_TEST, DR(TMP_REG1), dst, dstw))
  973. flags |= SLOW_DEST;
  974. if (flags & IMM_OP) {
  975. if ((src2 & SLJIT_IMM) && src2w) {
  976. if ((!(flags & LOGICAL_OP) && (src2w <= SIMM_MAX && src2w >= SIMM_MIN))
  977. || ((flags & LOGICAL_OP) && !(src2w & ~UIMM_MAX))) {
  978. flags |= SRC2_IMM;
  979. src2_r = src2w;
  980. }
  981. }
  982. if (!(flags & SRC2_IMM) && (flags & CUMULATIVE_OP) && (src1 & SLJIT_IMM) && src1w) {
  983. if ((!(flags & LOGICAL_OP) && (src1w <= SIMM_MAX && src1w >= SIMM_MIN))
  984. || ((flags & LOGICAL_OP) && !(src1w & ~UIMM_MAX))) {
  985. flags |= SRC2_IMM;
  986. src2_r = src1w;
  987. /* And swap arguments. */
  988. src1 = src2;
  989. src1w = src2w;
  990. src2 = SLJIT_IMM;
  991. /* src2w = src2_r unneeded. */
  992. }
  993. }
  994. }
  995. /* Source 1. */
  996. if (FAST_IS_REG(src1)) {
  997. src1_r = src1;
  998. flags |= REG1_SOURCE;
  999. }
  1000. else if (src1 & SLJIT_IMM) {
  1001. if (src1w) {
  1002. FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w));
  1003. src1_r = TMP_REG1;
  1004. }
  1005. else
  1006. src1_r = 0;
  1007. }
  1008. else {
  1009. if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w))
  1010. FAIL_IF(compiler->error);
  1011. else
  1012. flags |= SLOW_SRC1;
  1013. src1_r = TMP_REG1;
  1014. }
  1015. /* Source 2. */
  1016. if (FAST_IS_REG(src2)) {
  1017. src2_r = src2;
  1018. flags |= REG2_SOURCE;
  1019. if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOV_P)
  1020. dst_r = src2_r;
  1021. }
  1022. else if (src2 & SLJIT_IMM) {
  1023. if (!(flags & SRC2_IMM)) {
  1024. if (src2w) {
  1025. FAIL_IF(load_immediate(compiler, DR(sugg_src2_r), src2w));
  1026. src2_r = sugg_src2_r;
  1027. }
  1028. else {
  1029. src2_r = 0;
  1030. if ((op >= SLJIT_MOV && op <= SLJIT_MOV_P) && (dst & SLJIT_MEM))
  1031. dst_r = 0;
  1032. }
  1033. }
  1034. }
  1035. else {
  1036. if (getput_arg_fast(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w))
  1037. FAIL_IF(compiler->error);
  1038. else
  1039. flags |= SLOW_SRC2;
  1040. src2_r = sugg_src2_r;
  1041. }
  1042. if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
  1043. SLJIT_ASSERT(src2_r == TMP_REG2);
  1044. if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
  1045. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, src1, src1w));
  1046. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
  1047. }
  1048. else {
  1049. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, src2, src2w));
  1050. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG2), src2, src2w, dst, dstw));
  1051. }
  1052. }
  1053. else if (flags & SLOW_SRC1)
  1054. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(TMP_REG1), src1, src1w, dst, dstw));
  1055. else if (flags & SLOW_SRC2)
  1056. FAIL_IF(getput_arg(compiler, flags | LOAD_DATA, DR(sugg_src2_r), src2, src2w, dst, dstw));
  1057. FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
  1058. if (dst & SLJIT_MEM) {
  1059. if (!(flags & SLOW_DEST)) {
  1060. getput_arg_fast(compiler, flags, DR(dst_r), dst, dstw);
  1061. return compiler->error;
  1062. }
  1063. return getput_arg(compiler, flags, DR(dst_r), dst, dstw, 0, 0);
  1064. }
  1065. return SLJIT_SUCCESS;
  1066. }
  1067. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
  1068. {
  1069. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1070. sljit_s32 int_op = op & SLJIT_I32_OP;
  1071. #endif
  1072. CHECK_ERROR();
  1073. CHECK(check_sljit_emit_op0(compiler, op));
  1074. op = GET_OPCODE(op);
  1075. switch (op) {
  1076. case SLJIT_BREAKPOINT:
  1077. return push_inst(compiler, BREAK, UNMOVABLE_INS);
  1078. case SLJIT_NOP:
  1079. return push_inst(compiler, NOP, UNMOVABLE_INS);
  1080. case SLJIT_LMUL_UW:
  1081. case SLJIT_LMUL_SW:
  1082. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  1083. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1084. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMULU : DMUL) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
  1085. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMUHU : DMUH) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
  1086. #else /* !SLJIT_CONFIG_MIPS_64 */
  1087. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MULU : MUL) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
  1088. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MUHU : MUH) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
  1089. #endif /* SLJIT_CONFIG_MIPS_64 */
  1090. FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | TA(0) | D(SLJIT_R0), DR(SLJIT_R0)));
  1091. return push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_R1), DR(SLJIT_R1));
  1092. #else /* !SLJIT_MIPS_R6 */
  1093. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1094. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? DMULTU : DMULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
  1095. #else /* !SLJIT_CONFIG_MIPS_64 */
  1096. FAIL_IF(push_inst(compiler, (op == SLJIT_LMUL_UW ? MULTU : MULT) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
  1097. #endif /* SLJIT_CONFIG_MIPS_64 */
  1098. FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
  1099. return push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
  1100. #endif /* SLJIT_MIPS_R6 */
  1101. case SLJIT_DIVMOD_UW:
  1102. case SLJIT_DIVMOD_SW:
  1103. case SLJIT_DIV_UW:
  1104. case SLJIT_DIV_SW:
  1105. SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
  1106. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  1107. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1108. if (int_op) {
  1109. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
  1110. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? MODU : MOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
  1111. }
  1112. else {
  1113. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DDIVU : DDIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
  1114. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DMODU : DMOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
  1115. }
  1116. #else /* !SLJIT_CONFIG_MIPS_64 */
  1117. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG3), DR(TMP_REG3)));
  1118. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? MODU : MOD) | S(SLJIT_R0) | T(SLJIT_R1) | D(TMP_REG1), DR(TMP_REG1)));
  1119. #endif /* SLJIT_CONFIG_MIPS_64 */
  1120. FAIL_IF(push_inst(compiler, ADDU_W | S(TMP_REG3) | TA(0) | D(SLJIT_R0), DR(SLJIT_R0)));
  1121. return (op >= SLJIT_DIV_UW) ? SLJIT_SUCCESS : push_inst(compiler, ADDU_W | S(TMP_REG1) | TA(0) | D(SLJIT_R1), DR(SLJIT_R1));
  1122. #else /* !SLJIT_MIPS_R6 */
  1123. #if !(defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  1124. FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
  1125. FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
  1126. #endif /* !SLJIT_MIPS_R1 */
  1127. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1128. if (int_op)
  1129. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
  1130. else
  1131. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DDIVU : DDIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
  1132. #else /* !SLJIT_CONFIG_MIPS_64 */
  1133. FAIL_IF(push_inst(compiler, ((op | 0x2) == SLJIT_DIV_UW ? DIVU : DIV) | S(SLJIT_R0) | T(SLJIT_R1), MOVABLE_INS));
  1134. #endif /* SLJIT_CONFIG_MIPS_64 */
  1135. FAIL_IF(push_inst(compiler, MFLO | D(SLJIT_R0), DR(SLJIT_R0)));
  1136. return (op >= SLJIT_DIV_UW) ? SLJIT_SUCCESS : push_inst(compiler, MFHI | D(SLJIT_R1), DR(SLJIT_R1));
  1137. #endif /* SLJIT_MIPS_R6 */
  1138. }
  1139. return SLJIT_SUCCESS;
  1140. }
  1141. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  1142. static sljit_s32 emit_prefetch(struct sljit_compiler *compiler,
  1143. sljit_s32 src, sljit_sw srcw)
  1144. {
  1145. if (!(src & OFFS_REG_MASK)) {
  1146. if (srcw <= SIMM_MAX && srcw >= SIMM_MIN)
  1147. return push_inst(compiler, PREF | S(src & REG_MASK) | IMM(srcw), MOVABLE_INS);
  1148. FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
  1149. return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
  1150. }
  1151. srcw &= 0x3;
  1152. if (SLJIT_UNLIKELY(srcw != 0)) {
  1153. FAIL_IF(push_inst(compiler, SLL_W | T(OFFS_REG(src)) | D(TMP_REG1) | SH_IMM(srcw), DR(TMP_REG1)));
  1154. return push_inst(compiler, PREFX | S(src & REG_MASK) | T(TMP_REG1), MOVABLE_INS);
  1155. }
  1156. return push_inst(compiler, PREFX | S(src & REG_MASK) | T(OFFS_REG(src)), MOVABLE_INS);
  1157. }
  1158. #endif
  1159. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
  1160. sljit_s32 dst, sljit_sw dstw,
  1161. sljit_s32 src, sljit_sw srcw)
  1162. {
  1163. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1164. # define flags 0
  1165. #else
  1166. sljit_s32 flags = 0;
  1167. #endif
  1168. CHECK_ERROR();
  1169. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1170. ADJUST_LOCAL_OFFSET(dst, dstw);
  1171. ADJUST_LOCAL_OFFSET(src, srcw);
  1172. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
  1173. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  1174. if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
  1175. return emit_prefetch(compiler, src, srcw);
  1176. #endif
  1177. return SLJIT_SUCCESS;
  1178. }
  1179. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1180. if ((op & SLJIT_I32_OP) && GET_OPCODE(op) >= SLJIT_NOT)
  1181. flags |= INT_DATA | SIGNED_DATA;
  1182. #endif
  1183. switch (GET_OPCODE(op)) {
  1184. case SLJIT_MOV:
  1185. case SLJIT_MOV_P:
  1186. return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
  1187. case SLJIT_MOV_U32:
  1188. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1189. return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
  1190. #else
  1191. return emit_op(compiler, SLJIT_MOV_U32, INT_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u32)srcw : srcw);
  1192. #endif
  1193. case SLJIT_MOV_S32:
  1194. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1195. return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
  1196. #else
  1197. return emit_op(compiler, SLJIT_MOV_S32, INT_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s32)srcw : srcw);
  1198. #endif
  1199. case SLJIT_MOV_U8:
  1200. return emit_op(compiler, SLJIT_MOV_U8, BYTE_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
  1201. case SLJIT_MOV_S8:
  1202. return emit_op(compiler, SLJIT_MOV_S8, BYTE_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
  1203. case SLJIT_MOV_U16:
  1204. return emit_op(compiler, SLJIT_MOV_U16, HALF_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
  1205. case SLJIT_MOV_S16:
  1206. return emit_op(compiler, SLJIT_MOV_S16, HALF_DATA | SIGNED_DATA, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
  1207. case SLJIT_NOT:
  1208. return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
  1209. case SLJIT_NEG:
  1210. return emit_op(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), flags | IMM_OP, dst, dstw, SLJIT_IMM, 0, src, srcw);
  1211. case SLJIT_CLZ:
  1212. return emit_op(compiler, op, flags, dst, dstw, TMP_REG1, 0, src, srcw);
  1213. }
  1214. SLJIT_UNREACHABLE();
  1215. return SLJIT_SUCCESS;
  1216. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1217. # undef flags
  1218. #endif
  1219. }
  1220. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
  1221. sljit_s32 dst, sljit_sw dstw,
  1222. sljit_s32 src1, sljit_sw src1w,
  1223. sljit_s32 src2, sljit_sw src2w)
  1224. {
  1225. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1226. # define flags 0
  1227. #else
  1228. sljit_s32 flags = 0;
  1229. #endif
  1230. CHECK_ERROR();
  1231. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1232. ADJUST_LOCAL_OFFSET(dst, dstw);
  1233. ADJUST_LOCAL_OFFSET(src1, src1w);
  1234. ADJUST_LOCAL_OFFSET(src2, src2w);
  1235. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
  1236. return SLJIT_SUCCESS;
  1237. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1238. if (op & SLJIT_I32_OP) {
  1239. flags |= INT_DATA | SIGNED_DATA;
  1240. if (src1 & SLJIT_IMM)
  1241. src1w = (sljit_s32)src1w;
  1242. if (src2 & SLJIT_IMM)
  1243. src2w = (sljit_s32)src2w;
  1244. }
  1245. #endif
  1246. switch (GET_OPCODE(op)) {
  1247. case SLJIT_ADD:
  1248. case SLJIT_ADDC:
  1249. return emit_op(compiler, op, flags | CUMULATIVE_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
  1250. case SLJIT_SUB:
  1251. case SLJIT_SUBC:
  1252. return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
  1253. case SLJIT_MUL:
  1254. return emit_op(compiler, op, flags | CUMULATIVE_OP, dst, dstw, src1, src1w, src2, src2w);
  1255. case SLJIT_AND:
  1256. case SLJIT_OR:
  1257. case SLJIT_XOR:
  1258. return emit_op(compiler, op, flags | CUMULATIVE_OP | LOGICAL_OP | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
  1259. case SLJIT_SHL:
  1260. case SLJIT_LSHR:
  1261. case SLJIT_ASHR:
  1262. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1263. if (src2 & SLJIT_IMM)
  1264. src2w &= 0x1f;
  1265. #else
  1266. if (src2 & SLJIT_IMM) {
  1267. if (op & SLJIT_I32_OP)
  1268. src2w &= 0x1f;
  1269. else
  1270. src2w &= 0x3f;
  1271. }
  1272. #endif
  1273. return emit_op(compiler, op, flags | IMM_OP, dst, dstw, src1, src1w, src2, src2w);
  1274. }
  1275. SLJIT_UNREACHABLE();
  1276. return SLJIT_SUCCESS;
  1277. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1278. # undef flags
  1279. #endif
  1280. }
  1281. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
  1282. {
  1283. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1284. return reg_map[reg];
  1285. }
  1286. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
  1287. {
  1288. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1289. return FR(reg);
  1290. }
  1291. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
  1292. void *instruction, sljit_s32 size)
  1293. {
  1294. CHECK_ERROR();
  1295. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1296. return push_inst(compiler, *(sljit_ins*)instruction, UNMOVABLE_INS);
  1297. }
  1298. /* --------------------------------------------------------------------- */
  1299. /* Floating point operators */
  1300. /* --------------------------------------------------------------------- */
  1301. #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_F32_OP) >> 7))
  1302. #define FMT(op) (((op & SLJIT_F32_OP) ^ SLJIT_F32_OP) << (21 - 8))
  1303. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
  1304. sljit_s32 dst, sljit_sw dstw,
  1305. sljit_s32 src, sljit_sw srcw)
  1306. {
  1307. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1308. # define flags 0
  1309. #else
  1310. sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_SW_FROM_F64) << 21;
  1311. #endif
  1312. if (src & SLJIT_MEM) {
  1313. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src, srcw, dst, dstw));
  1314. src = TMP_FREG1;
  1315. }
  1316. FAIL_IF(push_inst(compiler, (TRUNC_W_S ^ (flags >> 19)) | FMT(op) | FS(src) | FD(TMP_FREG1), MOVABLE_INS));
  1317. if (FAST_IS_REG(dst))
  1318. return push_inst(compiler, MFC1 | flags | T(dst) | FS(TMP_FREG1), MOVABLE_INS);
  1319. /* Store the integer value from a VFP register. */
  1320. return emit_op_mem2(compiler, flags ? DOUBLE_DATA : SINGLE_DATA, FR(TMP_FREG1), dst, dstw, 0, 0);
  1321. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1322. # undef is_long
  1323. #endif
  1324. }
  1325. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
  1326. sljit_s32 dst, sljit_sw dstw,
  1327. sljit_s32 src, sljit_sw srcw)
  1328. {
  1329. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1330. # define flags 0
  1331. #else
  1332. sljit_s32 flags = (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_SW) << 21;
  1333. #endif
  1334. sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1335. if (FAST_IS_REG(src))
  1336. FAIL_IF(push_inst(compiler, MTC1 | flags | T(src) | FS(TMP_FREG1), MOVABLE_INS));
  1337. else if (src & SLJIT_MEM) {
  1338. /* Load the integer value into a VFP register. */
  1339. FAIL_IF(emit_op_mem2(compiler, ((flags) ? DOUBLE_DATA : SINGLE_DATA) | LOAD_DATA, FR(TMP_FREG1), src, srcw, dst, dstw));
  1340. }
  1341. else {
  1342. #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
  1343. if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_S32)
  1344. srcw = (sljit_s32)srcw;
  1345. #endif
  1346. FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
  1347. FAIL_IF(push_inst(compiler, MTC1 | flags | T(TMP_REG1) | FS(TMP_FREG1), MOVABLE_INS));
  1348. }
  1349. FAIL_IF(push_inst(compiler, CVT_S_S | flags | (4 << 21) | (((op & SLJIT_F32_OP) ^ SLJIT_F32_OP) >> 8) | FS(TMP_FREG1) | FD(dst_r), MOVABLE_INS));
  1350. if (dst & SLJIT_MEM)
  1351. return emit_op_mem2(compiler, FLOAT_DATA(op), FR(TMP_FREG1), dst, dstw, 0, 0);
  1352. return SLJIT_SUCCESS;
  1353. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1354. # undef flags
  1355. #endif
  1356. }
  1357. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
  1358. sljit_s32 src1, sljit_sw src1w,
  1359. sljit_s32 src2, sljit_sw src2w)
  1360. {
  1361. sljit_ins inst;
  1362. if (src1 & SLJIT_MEM) {
  1363. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, src2, src2w));
  1364. src1 = TMP_FREG1;
  1365. }
  1366. if (src2 & SLJIT_MEM) {
  1367. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, 0, 0));
  1368. src2 = TMP_FREG2;
  1369. }
  1370. switch (GET_FLAG_TYPE(op)) {
  1371. case SLJIT_EQUAL_F64:
  1372. case SLJIT_NOT_EQUAL_F64:
  1373. inst = C_UEQ_S;
  1374. break;
  1375. case SLJIT_LESS_F64:
  1376. case SLJIT_GREATER_EQUAL_F64:
  1377. inst = C_ULT_S;
  1378. break;
  1379. case SLJIT_GREATER_F64:
  1380. case SLJIT_LESS_EQUAL_F64:
  1381. inst = C_ULE_S;
  1382. break;
  1383. default:
  1384. SLJIT_ASSERT(GET_FLAG_TYPE(op) == SLJIT_UNORDERED_F64 || GET_FLAG_TYPE(op) == SLJIT_ORDERED_F64);
  1385. inst = C_UN_S;
  1386. break;
  1387. }
  1388. return push_inst(compiler, inst | FMT(op) | FT(src2) | FS(src1) | C_FD, UNMOVABLE_INS);
  1389. }
  1390. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
  1391. sljit_s32 dst, sljit_sw dstw,
  1392. sljit_s32 src, sljit_sw srcw)
  1393. {
  1394. sljit_s32 dst_r;
  1395. CHECK_ERROR();
  1396. compiler->cache_arg = 0;
  1397. compiler->cache_argw = 0;
  1398. SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100) && !(DOUBLE_DATA & 0x2), float_transfer_bit_error);
  1399. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1400. if (GET_OPCODE(op) == SLJIT_CONV_F64_FROM_F32)
  1401. op ^= SLJIT_F32_OP;
  1402. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1403. if (src & SLJIT_MEM) {
  1404. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(dst_r), src, srcw, dst, dstw));
  1405. src = dst_r;
  1406. }
  1407. switch (GET_OPCODE(op)) {
  1408. case SLJIT_MOV_F64:
  1409. if (src != dst_r) {
  1410. if (dst_r != TMP_FREG1)
  1411. FAIL_IF(push_inst(compiler, MOV_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
  1412. else
  1413. dst_r = src;
  1414. }
  1415. break;
  1416. case SLJIT_NEG_F64:
  1417. FAIL_IF(push_inst(compiler, NEG_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
  1418. break;
  1419. case SLJIT_ABS_F64:
  1420. FAIL_IF(push_inst(compiler, ABS_S | FMT(op) | FS(src) | FD(dst_r), MOVABLE_INS));
  1421. break;
  1422. case SLJIT_CONV_F64_FROM_F32:
  1423. FAIL_IF(push_inst(compiler, CVT_S_S | ((op & SLJIT_F32_OP) ? 1 : (1 << 21)) | FS(src) | FD(dst_r), MOVABLE_INS));
  1424. op ^= SLJIT_F32_OP;
  1425. break;
  1426. }
  1427. if (dst & SLJIT_MEM)
  1428. return emit_op_mem2(compiler, FLOAT_DATA(op), FR(dst_r), dst, dstw, 0, 0);
  1429. return SLJIT_SUCCESS;
  1430. }
  1431. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
  1432. sljit_s32 dst, sljit_sw dstw,
  1433. sljit_s32 src1, sljit_sw src1w,
  1434. sljit_s32 src2, sljit_sw src2w)
  1435. {
  1436. sljit_s32 dst_r, flags = 0;
  1437. CHECK_ERROR();
  1438. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1439. ADJUST_LOCAL_OFFSET(dst, dstw);
  1440. ADJUST_LOCAL_OFFSET(src1, src1w);
  1441. ADJUST_LOCAL_OFFSET(src2, src2w);
  1442. compiler->cache_arg = 0;
  1443. compiler->cache_argw = 0;
  1444. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG2;
  1445. if (src1 & SLJIT_MEM) {
  1446. if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w)) {
  1447. FAIL_IF(compiler->error);
  1448. src1 = TMP_FREG1;
  1449. } else
  1450. flags |= SLOW_SRC1;
  1451. }
  1452. if (src2 & SLJIT_MEM) {
  1453. if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w)) {
  1454. FAIL_IF(compiler->error);
  1455. src2 = TMP_FREG2;
  1456. } else
  1457. flags |= SLOW_SRC2;
  1458. }
  1459. if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
  1460. if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
  1461. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, src1, src1w));
  1462. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, dst, dstw));
  1463. }
  1464. else {
  1465. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, src2, src2w));
  1466. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, dst, dstw));
  1467. }
  1468. }
  1469. else if (flags & SLOW_SRC1)
  1470. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG1), src1, src1w, dst, dstw));
  1471. else if (flags & SLOW_SRC2)
  1472. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, FR(TMP_FREG2), src2, src2w, dst, dstw));
  1473. if (flags & SLOW_SRC1)
  1474. src1 = TMP_FREG1;
  1475. if (flags & SLOW_SRC2)
  1476. src2 = TMP_FREG2;
  1477. switch (GET_OPCODE(op)) {
  1478. case SLJIT_ADD_F64:
  1479. FAIL_IF(push_inst(compiler, ADD_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
  1480. break;
  1481. case SLJIT_SUB_F64:
  1482. FAIL_IF(push_inst(compiler, SUB_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
  1483. break;
  1484. case SLJIT_MUL_F64:
  1485. FAIL_IF(push_inst(compiler, MUL_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
  1486. break;
  1487. case SLJIT_DIV_F64:
  1488. FAIL_IF(push_inst(compiler, DIV_S | FMT(op) | FT(src2) | FS(src1) | FD(dst_r), MOVABLE_INS));
  1489. break;
  1490. }
  1491. if (dst_r == TMP_FREG2)
  1492. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), FR(TMP_FREG2), dst, dstw, 0, 0));
  1493. return SLJIT_SUCCESS;
  1494. }
  1495. /* --------------------------------------------------------------------- */
  1496. /* Other instructions */
  1497. /* --------------------------------------------------------------------- */
  1498. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  1499. {
  1500. CHECK_ERROR();
  1501. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1502. ADJUST_LOCAL_OFFSET(dst, dstw);
  1503. if (FAST_IS_REG(dst))
  1504. return push_inst(compiler, ADDU_W | SA(RETURN_ADDR_REG) | TA(0) | D(dst), DR(dst));
  1505. /* Memory. */
  1506. return emit_op_mem(compiler, WORD_DATA, RETURN_ADDR_REG, dst, dstw);
  1507. }
  1508. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  1509. {
  1510. CHECK_ERROR();
  1511. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1512. ADJUST_LOCAL_OFFSET(src, srcw);
  1513. if (FAST_IS_REG(src))
  1514. FAIL_IF(push_inst(compiler, ADDU_W | S(src) | TA(0) | DA(RETURN_ADDR_REG), RETURN_ADDR_REG));
  1515. else
  1516. FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, RETURN_ADDR_REG, src, srcw));
  1517. FAIL_IF(push_inst(compiler, JR | SA(RETURN_ADDR_REG), UNMOVABLE_INS));
  1518. return push_inst(compiler, NOP, UNMOVABLE_INS);
  1519. }
  1520. /* --------------------------------------------------------------------- */
  1521. /* Conditional instructions */
  1522. /* --------------------------------------------------------------------- */
  1523. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1524. {
  1525. struct sljit_label *label;
  1526. CHECK_ERROR_PTR();
  1527. CHECK_PTR(check_sljit_emit_label(compiler));
  1528. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1529. return compiler->last_label;
  1530. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1531. PTR_FAIL_IF(!label);
  1532. set_label(label, compiler);
  1533. compiler->delay_slot = UNMOVABLE_INS;
  1534. return label;
  1535. }
  1536. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1537. #define JUMP_LENGTH 4
  1538. #else
  1539. #define JUMP_LENGTH 8
  1540. #endif
  1541. #define BR_Z(src) \
  1542. inst = BEQ | SA(src) | TA(0) | JUMP_LENGTH; \
  1543. flags = IS_BIT26_COND; \
  1544. delay_check = src;
  1545. #define BR_NZ(src) \
  1546. inst = BNE | SA(src) | TA(0) | JUMP_LENGTH; \
  1547. flags = IS_BIT26_COND; \
  1548. delay_check = src;
  1549. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  1550. #define BR_T() \
  1551. inst = BC1NEZ; \
  1552. flags = IS_BIT23_COND; \
  1553. delay_check = FCSR_FCC;
  1554. #define BR_F() \
  1555. inst = BC1EQZ; \
  1556. flags = IS_BIT23_COND; \
  1557. delay_check = FCSR_FCC;
  1558. #else /* !SLJIT_MIPS_R6 */
  1559. #define BR_T() \
  1560. inst = BC1T | JUMP_LENGTH; \
  1561. flags = IS_BIT16_COND; \
  1562. delay_check = FCSR_FCC;
  1563. #define BR_F() \
  1564. inst = BC1F | JUMP_LENGTH; \
  1565. flags = IS_BIT16_COND; \
  1566. delay_check = FCSR_FCC;
  1567. #endif /* SLJIT_MIPS_R6 */
  1568. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
  1569. {
  1570. struct sljit_jump *jump;
  1571. sljit_ins inst;
  1572. sljit_s32 flags = 0;
  1573. sljit_s32 delay_check = UNMOVABLE_INS;
  1574. CHECK_ERROR_PTR();
  1575. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1576. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1577. PTR_FAIL_IF(!jump);
  1578. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1579. type &= 0xff;
  1580. switch (type) {
  1581. case SLJIT_EQUAL:
  1582. BR_NZ(EQUAL_FLAG);
  1583. break;
  1584. case SLJIT_NOT_EQUAL:
  1585. BR_Z(EQUAL_FLAG);
  1586. break;
  1587. case SLJIT_LESS:
  1588. case SLJIT_GREATER:
  1589. case SLJIT_SIG_LESS:
  1590. case SLJIT_SIG_GREATER:
  1591. case SLJIT_OVERFLOW:
  1592. case SLJIT_MUL_OVERFLOW:
  1593. BR_Z(OTHER_FLAG);
  1594. break;
  1595. case SLJIT_GREATER_EQUAL:
  1596. case SLJIT_LESS_EQUAL:
  1597. case SLJIT_SIG_GREATER_EQUAL:
  1598. case SLJIT_SIG_LESS_EQUAL:
  1599. case SLJIT_NOT_OVERFLOW:
  1600. case SLJIT_MUL_NOT_OVERFLOW:
  1601. BR_NZ(OTHER_FLAG);
  1602. break;
  1603. case SLJIT_NOT_EQUAL_F64:
  1604. case SLJIT_GREATER_EQUAL_F64:
  1605. case SLJIT_GREATER_F64:
  1606. case SLJIT_ORDERED_F64:
  1607. BR_T();
  1608. break;
  1609. case SLJIT_EQUAL_F64:
  1610. case SLJIT_LESS_F64:
  1611. case SLJIT_LESS_EQUAL_F64:
  1612. case SLJIT_UNORDERED_F64:
  1613. BR_F();
  1614. break;
  1615. default:
  1616. /* Not conditional branch. */
  1617. inst = 0;
  1618. break;
  1619. }
  1620. jump->flags |= flags;
  1621. if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != delay_check))
  1622. jump->flags |= IS_MOVABLE;
  1623. if (inst)
  1624. PTR_FAIL_IF(push_inst(compiler, inst, UNMOVABLE_INS));
  1625. PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
  1626. if (type <= SLJIT_JUMP)
  1627. PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
  1628. else {
  1629. jump->flags |= IS_JAL;
  1630. PTR_FAIL_IF(push_inst(compiler, JALR | S(TMP_REG2) | DA(RETURN_ADDR_REG), UNMOVABLE_INS));
  1631. }
  1632. jump->addr = compiler->size;
  1633. PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
  1634. return jump;
  1635. }
  1636. #define RESOLVE_IMM1() \
  1637. if (src1 & SLJIT_IMM) { \
  1638. if (src1w) { \
  1639. PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG1), src1w)); \
  1640. src1 = TMP_REG1; \
  1641. } \
  1642. else \
  1643. src1 = 0; \
  1644. }
  1645. #define RESOLVE_IMM2() \
  1646. if (src2 & SLJIT_IMM) { \
  1647. if (src2w) { \
  1648. PTR_FAIL_IF(load_immediate(compiler, DR(TMP_REG2), src2w)); \
  1649. src2 = TMP_REG2; \
  1650. } \
  1651. else \
  1652. src2 = 0; \
  1653. }
  1654. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
  1655. sljit_s32 src1, sljit_sw src1w,
  1656. sljit_s32 src2, sljit_sw src2w)
  1657. {
  1658. struct sljit_jump *jump;
  1659. sljit_s32 flags;
  1660. sljit_ins inst;
  1661. CHECK_ERROR_PTR();
  1662. CHECK_PTR(check_sljit_emit_cmp(compiler, type, src1, src1w, src2, src2w));
  1663. ADJUST_LOCAL_OFFSET(src1, src1w);
  1664. ADJUST_LOCAL_OFFSET(src2, src2w);
  1665. compiler->cache_arg = 0;
  1666. compiler->cache_argw = 0;
  1667. flags = ((type & SLJIT_I32_OP) ? INT_DATA : WORD_DATA) | LOAD_DATA;
  1668. if (src1 & SLJIT_MEM) {
  1669. PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG1), src1, src1w, src2, src2w));
  1670. src1 = TMP_REG1;
  1671. }
  1672. if (src2 & SLJIT_MEM) {
  1673. PTR_FAIL_IF(emit_op_mem2(compiler, flags, DR(TMP_REG2), src2, src2w, 0, 0));
  1674. src2 = TMP_REG2;
  1675. }
  1676. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1677. PTR_FAIL_IF(!jump);
  1678. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1679. type &= 0xff;
  1680. if (type <= SLJIT_NOT_EQUAL) {
  1681. RESOLVE_IMM1();
  1682. RESOLVE_IMM2();
  1683. jump->flags |= IS_BIT26_COND;
  1684. if (compiler->delay_slot == MOVABLE_INS || (compiler->delay_slot != UNMOVABLE_INS && compiler->delay_slot != DR(src1) && compiler->delay_slot != DR(src2)))
  1685. jump->flags |= IS_MOVABLE;
  1686. PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(src1) | T(src2) | JUMP_LENGTH, UNMOVABLE_INS));
  1687. }
  1688. else if (type >= SLJIT_SIG_LESS && (((src1 & SLJIT_IMM) && (src1w == 0)) || ((src2 & SLJIT_IMM) && (src2w == 0)))) {
  1689. inst = NOP;
  1690. if ((src1 & SLJIT_IMM) && (src1w == 0)) {
  1691. RESOLVE_IMM2();
  1692. switch (type) {
  1693. case SLJIT_SIG_LESS:
  1694. inst = BLEZ;
  1695. jump->flags |= IS_BIT26_COND;
  1696. break;
  1697. case SLJIT_SIG_GREATER_EQUAL:
  1698. inst = BGTZ;
  1699. jump->flags |= IS_BIT26_COND;
  1700. break;
  1701. case SLJIT_SIG_GREATER:
  1702. inst = BGEZ;
  1703. jump->flags |= IS_BIT16_COND;
  1704. break;
  1705. case SLJIT_SIG_LESS_EQUAL:
  1706. inst = BLTZ;
  1707. jump->flags |= IS_BIT16_COND;
  1708. break;
  1709. }
  1710. src1 = src2;
  1711. }
  1712. else {
  1713. RESOLVE_IMM1();
  1714. switch (type) {
  1715. case SLJIT_SIG_LESS:
  1716. inst = BGEZ;
  1717. jump->flags |= IS_BIT16_COND;
  1718. break;
  1719. case SLJIT_SIG_GREATER_EQUAL:
  1720. inst = BLTZ;
  1721. jump->flags |= IS_BIT16_COND;
  1722. break;
  1723. case SLJIT_SIG_GREATER:
  1724. inst = BLEZ;
  1725. jump->flags |= IS_BIT26_COND;
  1726. break;
  1727. case SLJIT_SIG_LESS_EQUAL:
  1728. inst = BGTZ;
  1729. jump->flags |= IS_BIT26_COND;
  1730. break;
  1731. }
  1732. }
  1733. PTR_FAIL_IF(push_inst(compiler, inst | S(src1) | JUMP_LENGTH, UNMOVABLE_INS));
  1734. }
  1735. else {
  1736. if (type == SLJIT_LESS || type == SLJIT_GREATER_EQUAL || type == SLJIT_SIG_LESS || type == SLJIT_SIG_GREATER_EQUAL) {
  1737. RESOLVE_IMM1();
  1738. if ((src2 & SLJIT_IMM) && src2w <= SIMM_MAX && src2w >= SIMM_MIN)
  1739. PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src1) | T(TMP_REG1) | IMM(src2w), DR(TMP_REG1)));
  1740. else {
  1741. RESOLVE_IMM2();
  1742. PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src1) | T(src2) | D(TMP_REG1), DR(TMP_REG1)));
  1743. }
  1744. type = (type == SLJIT_LESS || type == SLJIT_SIG_LESS) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
  1745. }
  1746. else {
  1747. RESOLVE_IMM2();
  1748. if ((src1 & SLJIT_IMM) && src1w <= SIMM_MAX && src1w >= SIMM_MIN)
  1749. PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTIU : SLTI) | S(src2) | T(TMP_REG1) | IMM(src1w), DR(TMP_REG1)));
  1750. else {
  1751. RESOLVE_IMM1();
  1752. PTR_FAIL_IF(push_inst(compiler, (type <= SLJIT_LESS_EQUAL ? SLTU : SLT) | S(src2) | T(src1) | D(TMP_REG1), DR(TMP_REG1)));
  1753. }
  1754. type = (type == SLJIT_GREATER || type == SLJIT_SIG_GREATER) ? SLJIT_NOT_EQUAL : SLJIT_EQUAL;
  1755. }
  1756. jump->flags |= IS_BIT26_COND;
  1757. PTR_FAIL_IF(push_inst(compiler, (type == SLJIT_EQUAL ? BNE : BEQ) | S(TMP_REG1) | TA(0) | JUMP_LENGTH, UNMOVABLE_INS));
  1758. }
  1759. PTR_FAIL_IF(emit_const(compiler, TMP_REG2, 0));
  1760. PTR_FAIL_IF(push_inst(compiler, JR | S(TMP_REG2), UNMOVABLE_INS));
  1761. jump->addr = compiler->size;
  1762. PTR_FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
  1763. return jump;
  1764. }
  1765. #undef RESOLVE_IMM1
  1766. #undef RESOLVE_IMM2
  1767. #undef JUMP_LENGTH
  1768. #undef BR_Z
  1769. #undef BR_NZ
  1770. #undef BR_T
  1771. #undef BR_F
  1772. #undef FLOAT_DATA
  1773. #undef FMT
  1774. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
  1775. {
  1776. struct sljit_jump *jump = NULL;
  1777. CHECK_ERROR();
  1778. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  1779. ADJUST_LOCAL_OFFSET(src, srcw);
  1780. if (src & SLJIT_IMM) {
  1781. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1782. FAIL_IF(!jump);
  1783. set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_JAL : 0));
  1784. jump->u.target = srcw;
  1785. if (compiler->delay_slot != UNMOVABLE_INS)
  1786. jump->flags |= IS_MOVABLE;
  1787. FAIL_IF(emit_const(compiler, TMP_REG2, 0));
  1788. src = TMP_REG2;
  1789. }
  1790. else if (src & SLJIT_MEM) {
  1791. FAIL_IF(emit_op_mem(compiler, WORD_DATA | LOAD_DATA, DR(TMP_REG2), src, srcw));
  1792. src = TMP_REG2;
  1793. }
  1794. FAIL_IF(push_inst(compiler, JR | S(src), UNMOVABLE_INS));
  1795. if (jump)
  1796. jump->addr = compiler->size;
  1797. FAIL_IF(push_inst(compiler, NOP, UNMOVABLE_INS));
  1798. return SLJIT_SUCCESS;
  1799. }
  1800. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
  1801. sljit_s32 dst, sljit_sw dstw,
  1802. sljit_s32 type)
  1803. {
  1804. sljit_s32 src_ar, dst_ar;
  1805. sljit_s32 saved_op = op;
  1806. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1807. sljit_s32 mem_type = WORD_DATA;
  1808. #else
  1809. sljit_s32 mem_type = (op & SLJIT_I32_OP) ? (INT_DATA | SIGNED_DATA) : WORD_DATA;
  1810. #endif
  1811. CHECK_ERROR();
  1812. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
  1813. ADJUST_LOCAL_OFFSET(dst, dstw);
  1814. op = GET_OPCODE(op);
  1815. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1816. if (op == SLJIT_MOV_S32)
  1817. mem_type = INT_DATA | SIGNED_DATA;
  1818. #endif
  1819. dst_ar = DR((op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2);
  1820. compiler->cache_arg = 0;
  1821. compiler->cache_argw = 0;
  1822. if (op >= SLJIT_ADD && (dst & SLJIT_MEM))
  1823. FAIL_IF(emit_op_mem2(compiler, mem_type | LOAD_DATA, DR(TMP_REG1), dst, dstw, dst, dstw));
  1824. switch (type & 0xff) {
  1825. case SLJIT_EQUAL:
  1826. case SLJIT_NOT_EQUAL:
  1827. FAIL_IF(push_inst(compiler, SLTIU | SA(EQUAL_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
  1828. src_ar = dst_ar;
  1829. break;
  1830. case SLJIT_MUL_OVERFLOW:
  1831. case SLJIT_MUL_NOT_OVERFLOW:
  1832. FAIL_IF(push_inst(compiler, SLTIU | SA(OTHER_FLAG) | TA(dst_ar) | IMM(1), dst_ar));
  1833. src_ar = dst_ar;
  1834. type ^= 0x1; /* Flip type bit for the XORI below. */
  1835. break;
  1836. case SLJIT_GREATER_F64:
  1837. case SLJIT_LESS_EQUAL_F64:
  1838. type ^= 0x1; /* Flip type bit for the XORI below. */
  1839. case SLJIT_EQUAL_F64:
  1840. case SLJIT_NOT_EQUAL_F64:
  1841. case SLJIT_LESS_F64:
  1842. case SLJIT_GREATER_EQUAL_F64:
  1843. case SLJIT_UNORDERED_F64:
  1844. case SLJIT_ORDERED_F64:
  1845. #if (defined SLJIT_MIPS_R6 && SLJIT_MIPS_R6)
  1846. FAIL_IF(push_inst(compiler, MFC1 | TA(dst_ar) | FS(TMP_FREG3), dst_ar));
  1847. #else /* !SLJIT_MIPS_R6 */
  1848. FAIL_IF(push_inst(compiler, CFC1 | TA(dst_ar) | DA(FCSR_REG), dst_ar));
  1849. #endif /* SLJIT_MIPS_R6 */
  1850. FAIL_IF(push_inst(compiler, SRL | TA(dst_ar) | DA(dst_ar) | SH_IMM(23), dst_ar));
  1851. FAIL_IF(push_inst(compiler, ANDI | SA(dst_ar) | TA(dst_ar) | IMM(1), dst_ar));
  1852. src_ar = dst_ar;
  1853. break;
  1854. default:
  1855. src_ar = OTHER_FLAG;
  1856. break;
  1857. }
  1858. if (type & 0x1) {
  1859. FAIL_IF(push_inst(compiler, XORI | SA(src_ar) | TA(dst_ar) | IMM(1), dst_ar));
  1860. src_ar = dst_ar;
  1861. }
  1862. if (op < SLJIT_ADD) {
  1863. if (dst & SLJIT_MEM)
  1864. return emit_op_mem(compiler, mem_type, src_ar, dst, dstw);
  1865. if (src_ar != dst_ar)
  1866. return push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | DA(dst_ar), dst_ar);
  1867. return SLJIT_SUCCESS;
  1868. }
  1869. /* OTHER_FLAG cannot be specified as src2 argument at the moment. */
  1870. if (DR(TMP_REG2) != src_ar)
  1871. FAIL_IF(push_inst(compiler, ADDU_W | SA(src_ar) | TA(0) | D(TMP_REG2), DR(TMP_REG2)));
  1872. mem_type |= CUMULATIVE_OP | LOGICAL_OP | IMM_OP | ALT_KEEP_CACHE;
  1873. if (dst & SLJIT_MEM)
  1874. return emit_op(compiler, saved_op, mem_type, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
  1875. return emit_op(compiler, saved_op, mem_type, dst, dstw, dst, dstw, TMP_REG2, 0);
  1876. }
  1877. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
  1878. sljit_s32 dst_reg,
  1879. sljit_s32 src, sljit_sw srcw)
  1880. {
  1881. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  1882. sljit_ins ins;
  1883. #endif
  1884. CHECK_ERROR();
  1885. CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
  1886. #if (defined SLJIT_MIPS_R1 && SLJIT_MIPS_R1)
  1887. if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
  1888. #if (defined SLJIT_CONFIG_MIPS_64 && SLJIT_CONFIG_MIPS_64)
  1889. if (dst_reg & SLJIT_I32_OP)
  1890. srcw = (sljit_s32)srcw;
  1891. #endif
  1892. FAIL_IF(load_immediate(compiler, DR(TMP_REG1), srcw));
  1893. src = TMP_REG1;
  1894. srcw = 0;
  1895. }
  1896. dst_reg &= ~SLJIT_I32_OP;
  1897. switch (type & 0xff) {
  1898. case SLJIT_EQUAL:
  1899. ins = MOVZ | TA(EQUAL_FLAG);
  1900. break;
  1901. case SLJIT_NOT_EQUAL:
  1902. ins = MOVN | TA(EQUAL_FLAG);
  1903. break;
  1904. case SLJIT_LESS:
  1905. case SLJIT_GREATER:
  1906. case SLJIT_SIG_LESS:
  1907. case SLJIT_SIG_GREATER:
  1908. case SLJIT_OVERFLOW:
  1909. case SLJIT_MUL_OVERFLOW:
  1910. ins = MOVN | TA(OTHER_FLAG);
  1911. break;
  1912. case SLJIT_GREATER_EQUAL:
  1913. case SLJIT_LESS_EQUAL:
  1914. case SLJIT_SIG_GREATER_EQUAL:
  1915. case SLJIT_SIG_LESS_EQUAL:
  1916. case SLJIT_NOT_OVERFLOW:
  1917. case SLJIT_MUL_NOT_OVERFLOW:
  1918. ins = MOVZ | TA(OTHER_FLAG);
  1919. break;
  1920. case SLJIT_EQUAL_F64:
  1921. case SLJIT_LESS_F64:
  1922. case SLJIT_LESS_EQUAL_F64:
  1923. case SLJIT_UNORDERED_F64:
  1924. ins = MOVT;
  1925. break;
  1926. case SLJIT_NOT_EQUAL_F64:
  1927. case SLJIT_GREATER_EQUAL_F64:
  1928. case SLJIT_GREATER_F64:
  1929. case SLJIT_ORDERED_F64:
  1930. ins = MOVF;
  1931. break;
  1932. default:
  1933. ins = MOVZ | TA(OTHER_FLAG);
  1934. SLJIT_UNREACHABLE();
  1935. break;
  1936. }
  1937. return push_inst(compiler, ins | S(src) | D(dst_reg), DR(dst_reg));
  1938. #else
  1939. return sljit_emit_cmov_generic(compiler, type, dst_reg, src, srcw);
  1940. #endif
  1941. }
  1942. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
  1943. {
  1944. struct sljit_const *const_;
  1945. sljit_s32 dst_r;
  1946. CHECK_ERROR_PTR();
  1947. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  1948. ADJUST_LOCAL_OFFSET(dst, dstw);
  1949. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  1950. PTR_FAIL_IF(!const_);
  1951. set_const(const_, compiler);
  1952. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
  1953. PTR_FAIL_IF(emit_const(compiler, dst_r, init_value));
  1954. if (dst & SLJIT_MEM)
  1955. PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
  1956. return const_;
  1957. }
  1958. SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  1959. {
  1960. struct sljit_put_label *put_label;
  1961. sljit_s32 dst_r;
  1962. CHECK_ERROR_PTR();
  1963. CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
  1964. ADJUST_LOCAL_OFFSET(dst, dstw);
  1965. put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
  1966. PTR_FAIL_IF(!put_label);
  1967. set_put_label(put_label, compiler, 0);
  1968. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
  1969. #if (defined SLJIT_CONFIG_MIPS_32 && SLJIT_CONFIG_MIPS_32)
  1970. PTR_FAIL_IF(emit_const(compiler, dst_r, 0));
  1971. #else
  1972. PTR_FAIL_IF(push_inst(compiler, dst_r, UNMOVABLE_INS));
  1973. compiler->size += 5;
  1974. #endif
  1975. if (dst & SLJIT_MEM)
  1976. PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
  1977. return put_label;
  1978. }