sljitNativeMIPS_common.c 67 KB

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