sljitNativePPC_common.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375
  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. SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void)
  27. {
  28. return "PowerPC" SLJIT_CPUINFO;
  29. }
  30. /* Length of an instruction word.
  31. Both for ppc-32 and ppc-64. */
  32. typedef sljit_ui sljit_ins;
  33. #if ((defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32) && (defined _AIX)) \
  34. || (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  35. #define SLJIT_PPC_STACK_FRAME_V2 1
  36. #endif
  37. #ifdef _AIX
  38. #include <sys/cache.h>
  39. #endif
  40. #if (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
  41. #define SLJIT_PASS_ENTRY_ADDR_TO_CALL 1
  42. #endif
  43. static void ppc_cache_flush(sljit_ins *from, sljit_ins *to)
  44. {
  45. #ifdef _AIX
  46. _sync_cache_range((caddr_t)from, (int)((size_t)to - (size_t)from));
  47. #elif defined(__GNUC__) || (defined(__IBM_GCC_ASM) && __IBM_GCC_ASM)
  48. # if defined(_ARCH_PWR) || defined(_ARCH_PWR2)
  49. /* Cache flush for POWER architecture. */
  50. while (from < to) {
  51. __asm__ volatile (
  52. "clf 0, %0\n"
  53. "dcs\n"
  54. : : "r"(from)
  55. );
  56. from++;
  57. }
  58. __asm__ volatile ( "ics" );
  59. # elif defined(_ARCH_COM) && !defined(_ARCH_PPC)
  60. # error "Cache flush is not implemented for PowerPC/POWER common mode."
  61. # else
  62. /* Cache flush for PowerPC architecture. */
  63. while (from < to) {
  64. __asm__ volatile (
  65. "dcbf 0, %0\n"
  66. "sync\n"
  67. "icbi 0, %0\n"
  68. : : "r"(from)
  69. );
  70. from++;
  71. }
  72. __asm__ volatile ( "isync" );
  73. # endif
  74. # ifdef __xlc__
  75. # warning "This file may fail to compile if -qfuncsect is used"
  76. # endif
  77. #elif defined(__xlc__)
  78. #error "Please enable GCC syntax for inline assembly statements with -qasm=gcc"
  79. #else
  80. #error "This platform requires a cache flush implementation."
  81. #endif /* _AIX */
  82. }
  83. #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
  84. #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
  85. #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
  86. #define TMP_ZERO (SLJIT_NUMBER_OF_REGISTERS + 5)
  87. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL)
  88. #define TMP_CALL_REG (SLJIT_NUMBER_OF_REGISTERS + 6)
  89. #else
  90. #define TMP_CALL_REG TMP_REG2
  91. #endif
  92. #define TMP_FREG1 (0)
  93. #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
  94. static SLJIT_CONST sljit_ub reg_map[SLJIT_NUMBER_OF_REGISTERS + 7] = {
  95. 0, 3, 4, 5, 6, 7, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 1, 8, 9, 10, 31, 12
  96. };
  97. /* --------------------------------------------------------------------- */
  98. /* Instrucion forms */
  99. /* --------------------------------------------------------------------- */
  100. #define D(d) (reg_map[d] << 21)
  101. #define S(s) (reg_map[s] << 21)
  102. #define A(a) (reg_map[a] << 16)
  103. #define B(b) (reg_map[b] << 11)
  104. #define C(c) (reg_map[c] << 6)
  105. #define FD(fd) ((fd) << 21)
  106. #define FS(fs) ((fs) << 21)
  107. #define FA(fa) ((fa) << 16)
  108. #define FB(fb) ((fb) << 11)
  109. #define FC(fc) ((fc) << 6)
  110. #define IMM(imm) ((imm) & 0xffff)
  111. #define CRD(d) ((d) << 21)
  112. /* Instruction bit sections.
  113. OE and Rc flag (see ALT_SET_FLAGS). */
  114. #define OERC(flags) (((flags & ALT_SET_FLAGS) >> 10) | (flags & ALT_SET_FLAGS))
  115. /* Rc flag (see ALT_SET_FLAGS). */
  116. #define RC(flags) ((flags & ALT_SET_FLAGS) >> 10)
  117. #define HI(opcode) ((opcode) << 26)
  118. #define LO(opcode) ((opcode) << 1)
  119. #define ADD (HI(31) | LO(266))
  120. #define ADDC (HI(31) | LO(10))
  121. #define ADDE (HI(31) | LO(138))
  122. #define ADDI (HI(14))
  123. #define ADDIC (HI(13))
  124. #define ADDIS (HI(15))
  125. #define ADDME (HI(31) | LO(234))
  126. #define AND (HI(31) | LO(28))
  127. #define ANDI (HI(28))
  128. #define ANDIS (HI(29))
  129. #define Bx (HI(18))
  130. #define BCx (HI(16))
  131. #define BCCTR (HI(19) | LO(528) | (3 << 11))
  132. #define BLR (HI(19) | LO(16) | (0x14 << 21))
  133. #define CNTLZD (HI(31) | LO(58))
  134. #define CNTLZW (HI(31) | LO(26))
  135. #define CMP (HI(31) | LO(0))
  136. #define CMPI (HI(11))
  137. #define CMPL (HI(31) | LO(32))
  138. #define CMPLI (HI(10))
  139. #define CROR (HI(19) | LO(449))
  140. #define DIVD (HI(31) | LO(489))
  141. #define DIVDU (HI(31) | LO(457))
  142. #define DIVW (HI(31) | LO(491))
  143. #define DIVWU (HI(31) | LO(459))
  144. #define EXTSB (HI(31) | LO(954))
  145. #define EXTSH (HI(31) | LO(922))
  146. #define EXTSW (HI(31) | LO(986))
  147. #define FABS (HI(63) | LO(264))
  148. #define FADD (HI(63) | LO(21))
  149. #define FADDS (HI(59) | LO(21))
  150. #define FCFID (HI(63) | LO(846))
  151. #define FCMPU (HI(63) | LO(0))
  152. #define FCTIDZ (HI(63) | LO(815))
  153. #define FCTIWZ (HI(63) | LO(15))
  154. #define FDIV (HI(63) | LO(18))
  155. #define FDIVS (HI(59) | LO(18))
  156. #define FMR (HI(63) | LO(72))
  157. #define FMUL (HI(63) | LO(25))
  158. #define FMULS (HI(59) | LO(25))
  159. #define FNEG (HI(63) | LO(40))
  160. #define FRSP (HI(63) | LO(12))
  161. #define FSUB (HI(63) | LO(20))
  162. #define FSUBS (HI(59) | LO(20))
  163. #define LD (HI(58) | 0)
  164. #define LWZ (HI(32))
  165. #define MFCR (HI(31) | LO(19))
  166. #define MFLR (HI(31) | LO(339) | 0x80000)
  167. #define MFXER (HI(31) | LO(339) | 0x10000)
  168. #define MTCTR (HI(31) | LO(467) | 0x90000)
  169. #define MTLR (HI(31) | LO(467) | 0x80000)
  170. #define MTXER (HI(31) | LO(467) | 0x10000)
  171. #define MULHD (HI(31) | LO(73))
  172. #define MULHDU (HI(31) | LO(9))
  173. #define MULHW (HI(31) | LO(75))
  174. #define MULHWU (HI(31) | LO(11))
  175. #define MULLD (HI(31) | LO(233))
  176. #define MULLI (HI(7))
  177. #define MULLW (HI(31) | LO(235))
  178. #define NEG (HI(31) | LO(104))
  179. #define NOP (HI(24))
  180. #define NOR (HI(31) | LO(124))
  181. #define OR (HI(31) | LO(444))
  182. #define ORI (HI(24))
  183. #define ORIS (HI(25))
  184. #define RLDICL (HI(30))
  185. #define RLWINM (HI(21))
  186. #define SLD (HI(31) | LO(27))
  187. #define SLW (HI(31) | LO(24))
  188. #define SRAD (HI(31) | LO(794))
  189. #define SRADI (HI(31) | LO(413 << 1))
  190. #define SRAW (HI(31) | LO(792))
  191. #define SRAWI (HI(31) | LO(824))
  192. #define SRD (HI(31) | LO(539))
  193. #define SRW (HI(31) | LO(536))
  194. #define STD (HI(62) | 0)
  195. #define STDU (HI(62) | 1)
  196. #define STDUX (HI(31) | LO(181))
  197. #define STFIWX (HI(31) | LO(983))
  198. #define STW (HI(36))
  199. #define STWU (HI(37))
  200. #define STWUX (HI(31) | LO(183))
  201. #define SUBF (HI(31) | LO(40))
  202. #define SUBFC (HI(31) | LO(8))
  203. #define SUBFE (HI(31) | LO(136))
  204. #define SUBFIC (HI(8))
  205. #define XOR (HI(31) | LO(316))
  206. #define XORI (HI(26))
  207. #define XORIS (HI(27))
  208. #define SIMM_MAX (0x7fff)
  209. #define SIMM_MIN (-0x8000)
  210. #define UIMM_MAX (0xffff)
  211. #if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  212. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func)
  213. {
  214. sljit_sw* ptrs;
  215. if (func_ptr)
  216. *func_ptr = (void*)context;
  217. ptrs = (sljit_sw*)func;
  218. context->addr = addr ? addr : ptrs[0];
  219. context->r2 = ptrs[1];
  220. context->r11 = ptrs[2];
  221. }
  222. #endif
  223. static sljit_si push_inst(struct sljit_compiler *compiler, sljit_ins ins)
  224. {
  225. sljit_ins *ptr = (sljit_ins*)ensure_buf(compiler, sizeof(sljit_ins));
  226. FAIL_IF(!ptr);
  227. *ptr = ins;
  228. compiler->size++;
  229. return SLJIT_SUCCESS;
  230. }
  231. static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_ins *code_ptr, sljit_ins *code)
  232. {
  233. sljit_sw diff;
  234. sljit_uw target_addr;
  235. sljit_sw extra_jump_flags;
  236. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL) && (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  237. if (jump->flags & (SLJIT_REWRITABLE_JUMP | IS_CALL))
  238. return 0;
  239. #else
  240. if (jump->flags & SLJIT_REWRITABLE_JUMP)
  241. return 0;
  242. #endif
  243. if (jump->flags & JUMP_ADDR)
  244. target_addr = jump->u.target;
  245. else {
  246. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  247. target_addr = (sljit_uw)(code + jump->u.label->size);
  248. }
  249. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL) && (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  250. if (jump->flags & IS_CALL)
  251. goto keep_address;
  252. #endif
  253. diff = ((sljit_sw)target_addr - (sljit_sw)(code_ptr)) & ~0x3l;
  254. extra_jump_flags = 0;
  255. if (jump->flags & IS_COND) {
  256. if (diff <= 0x7fff && diff >= -0x8000) {
  257. jump->flags |= PATCH_B;
  258. return 1;
  259. }
  260. if (target_addr <= 0xffff) {
  261. jump->flags |= PATCH_B | PATCH_ABS_B;
  262. return 1;
  263. }
  264. extra_jump_flags = REMOVE_COND;
  265. diff -= sizeof(sljit_ins);
  266. }
  267. if (diff <= 0x01ffffff && diff >= -0x02000000) {
  268. jump->flags |= PATCH_B | extra_jump_flags;
  269. return 1;
  270. }
  271. if (target_addr <= 0x03ffffff) {
  272. jump->flags |= PATCH_B | PATCH_ABS_B | extra_jump_flags;
  273. return 1;
  274. }
  275. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  276. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL)
  277. keep_address:
  278. #endif
  279. if (target_addr <= 0x7fffffff) {
  280. jump->flags |= PATCH_ABS32;
  281. return 1;
  282. }
  283. if (target_addr <= 0x7fffffffffffl) {
  284. jump->flags |= PATCH_ABS48;
  285. return 1;
  286. }
  287. #endif
  288. return 0;
  289. }
  290. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
  291. {
  292. struct sljit_memory_fragment *buf;
  293. sljit_ins *code;
  294. sljit_ins *code_ptr;
  295. sljit_ins *buf_ptr;
  296. sljit_ins *buf_end;
  297. sljit_uw word_count;
  298. sljit_uw addr;
  299. struct sljit_label *label;
  300. struct sljit_jump *jump;
  301. struct sljit_const *const_;
  302. CHECK_ERROR_PTR();
  303. CHECK_PTR(check_sljit_generate_code(compiler));
  304. reverse_buf(compiler);
  305. #if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  306. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  307. compiler->size += (compiler->size & 0x1) + (sizeof(struct sljit_function_context) / sizeof(sljit_ins));
  308. #else
  309. compiler->size += (sizeof(struct sljit_function_context) / sizeof(sljit_ins));
  310. #endif
  311. #endif
  312. code = (sljit_ins*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_ins));
  313. PTR_FAIL_WITH_EXEC_IF(code);
  314. buf = compiler->buf;
  315. code_ptr = code;
  316. word_count = 0;
  317. label = compiler->labels;
  318. jump = compiler->jumps;
  319. const_ = compiler->consts;
  320. do {
  321. buf_ptr = (sljit_ins*)buf->memory;
  322. buf_end = buf_ptr + (buf->used_size >> 2);
  323. do {
  324. *code_ptr = *buf_ptr++;
  325. SLJIT_ASSERT(!label || label->size >= word_count);
  326. SLJIT_ASSERT(!jump || jump->addr >= word_count);
  327. SLJIT_ASSERT(!const_ || const_->addr >= word_count);
  328. /* These structures are ordered by their address. */
  329. if (label && label->size == word_count) {
  330. /* Just recording the address. */
  331. label->addr = (sljit_uw)code_ptr;
  332. label->size = code_ptr - code;
  333. label = label->next;
  334. }
  335. if (jump && jump->addr == word_count) {
  336. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  337. jump->addr = (sljit_uw)(code_ptr - 3);
  338. #else
  339. jump->addr = (sljit_uw)(code_ptr - 6);
  340. #endif
  341. if (detect_jump_type(jump, code_ptr, code)) {
  342. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  343. code_ptr[-3] = code_ptr[0];
  344. code_ptr -= 3;
  345. #else
  346. if (jump->flags & PATCH_ABS32) {
  347. code_ptr -= 3;
  348. code_ptr[-1] = code_ptr[2];
  349. code_ptr[0] = code_ptr[3];
  350. }
  351. else if (jump->flags & PATCH_ABS48) {
  352. code_ptr--;
  353. code_ptr[-1] = code_ptr[0];
  354. code_ptr[0] = code_ptr[1];
  355. /* rldicr rX,rX,32,31 -> rX,rX,16,47 */
  356. SLJIT_ASSERT((code_ptr[-3] & 0xfc00ffff) == 0x780007c6);
  357. code_ptr[-3] ^= 0x8422;
  358. /* oris -> ori */
  359. code_ptr[-2] ^= 0x4000000;
  360. }
  361. else {
  362. code_ptr[-6] = code_ptr[0];
  363. code_ptr -= 6;
  364. }
  365. #endif
  366. if (jump->flags & REMOVE_COND) {
  367. code_ptr[0] = BCx | (2 << 2) | ((code_ptr[0] ^ (8 << 21)) & 0x03ff0001);
  368. code_ptr++;
  369. jump->addr += sizeof(sljit_ins);
  370. code_ptr[0] = Bx;
  371. jump->flags -= IS_COND;
  372. }
  373. }
  374. jump = jump->next;
  375. }
  376. if (const_ && const_->addr == word_count) {
  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. #if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  394. SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size - (sizeof(struct sljit_function_context) / sizeof(sljit_ins)));
  395. #else
  396. SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
  397. #endif
  398. jump = compiler->jumps;
  399. while (jump) {
  400. do {
  401. addr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
  402. buf_ptr = (sljit_ins*)jump->addr;
  403. if (jump->flags & PATCH_B) {
  404. if (jump->flags & IS_COND) {
  405. if (!(jump->flags & PATCH_ABS_B)) {
  406. addr = addr - jump->addr;
  407. SLJIT_ASSERT((sljit_sw)addr <= 0x7fff && (sljit_sw)addr >= -0x8000);
  408. *buf_ptr = BCx | (addr & 0xfffc) | ((*buf_ptr) & 0x03ff0001);
  409. }
  410. else {
  411. SLJIT_ASSERT(addr <= 0xffff);
  412. *buf_ptr = BCx | (addr & 0xfffc) | 0x2 | ((*buf_ptr) & 0x03ff0001);
  413. }
  414. }
  415. else {
  416. if (!(jump->flags & PATCH_ABS_B)) {
  417. addr = addr - jump->addr;
  418. SLJIT_ASSERT((sljit_sw)addr <= 0x01ffffff && (sljit_sw)addr >= -0x02000000);
  419. *buf_ptr = Bx | (addr & 0x03fffffc) | ((*buf_ptr) & 0x1);
  420. }
  421. else {
  422. SLJIT_ASSERT(addr <= 0x03ffffff);
  423. *buf_ptr = Bx | (addr & 0x03fffffc) | 0x2 | ((*buf_ptr) & 0x1);
  424. }
  425. }
  426. break;
  427. }
  428. /* Set the fields of immediate loads. */
  429. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  430. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
  431. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
  432. #else
  433. if (jump->flags & PATCH_ABS32) {
  434. SLJIT_ASSERT(addr <= 0x7fffffff);
  435. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 16) & 0xffff);
  436. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | (addr & 0xffff);
  437. break;
  438. }
  439. if (jump->flags & PATCH_ABS48) {
  440. SLJIT_ASSERT(addr <= 0x7fffffffffff);
  441. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 32) & 0xffff);
  442. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 16) & 0xffff);
  443. buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | (addr & 0xffff);
  444. break;
  445. }
  446. buf_ptr[0] = (buf_ptr[0] & 0xffff0000) | ((addr >> 48) & 0xffff);
  447. buf_ptr[1] = (buf_ptr[1] & 0xffff0000) | ((addr >> 32) & 0xffff);
  448. buf_ptr[3] = (buf_ptr[3] & 0xffff0000) | ((addr >> 16) & 0xffff);
  449. buf_ptr[4] = (buf_ptr[4] & 0xffff0000) | (addr & 0xffff);
  450. #endif
  451. } while (0);
  452. jump = jump->next;
  453. }
  454. compiler->error = SLJIT_ERR_COMPILED;
  455. compiler->executable_size = (code_ptr - code) * sizeof(sljit_ins);
  456. SLJIT_CACHE_FLUSH(code, code_ptr);
  457. #if (defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  458. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  459. if (((sljit_sw)code_ptr) & 0x4)
  460. code_ptr++;
  461. sljit_set_function_context(NULL, (struct sljit_function_context*)code_ptr, (sljit_sw)code, (void*)sljit_generate_code);
  462. return code_ptr;
  463. #else
  464. sljit_set_function_context(NULL, (struct sljit_function_context*)code_ptr, (sljit_sw)code, (void*)sljit_generate_code);
  465. return code_ptr;
  466. #endif
  467. #else
  468. return code;
  469. #endif
  470. }
  471. /* --------------------------------------------------------------------- */
  472. /* Entry, exit */
  473. /* --------------------------------------------------------------------- */
  474. /* inp_flags: */
  475. /* Creates an index in data_transfer_insts array. */
  476. #define LOAD_DATA 0x01
  477. #define INDEXED 0x02
  478. #define WRITE_BACK 0x04
  479. #define WORD_DATA 0x00
  480. #define BYTE_DATA 0x08
  481. #define HALF_DATA 0x10
  482. #define INT_DATA 0x18
  483. #define SIGNED_DATA 0x20
  484. /* Separates integer and floating point registers */
  485. #define GPR_REG 0x3f
  486. #define DOUBLE_DATA 0x40
  487. #define MEM_MASK 0x7f
  488. /* Other inp_flags. */
  489. #define ARG_TEST 0x000100
  490. /* Integer opertion and set flags -> requires exts on 64 bit systems. */
  491. #define ALT_SIGN_EXT 0x000200
  492. /* This flag affects the RC() and OERC() macros. */
  493. #define ALT_SET_FLAGS 0x000400
  494. #define ALT_KEEP_CACHE 0x000800
  495. #define ALT_FORM1 0x010000
  496. #define ALT_FORM2 0x020000
  497. #define ALT_FORM3 0x040000
  498. #define ALT_FORM4 0x080000
  499. #define ALT_FORM5 0x100000
  500. #define ALT_FORM6 0x200000
  501. /* Source and destination is register. */
  502. #define REG_DEST 0x000001
  503. #define REG1_SOURCE 0x000002
  504. #define REG2_SOURCE 0x000004
  505. /* getput_arg_fast returned true. */
  506. #define FAST_DEST 0x000008
  507. /* Multiple instructions are required. */
  508. #define SLOW_DEST 0x000010
  509. /*
  510. ALT_SIGN_EXT 0x000200
  511. ALT_SET_FLAGS 0x000400
  512. ALT_FORM1 0x010000
  513. ...
  514. ALT_FORM6 0x200000 */
  515. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  516. #include "sljitNativePPC_32.c"
  517. #else
  518. #include "sljitNativePPC_64.c"
  519. #endif
  520. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  521. #define STACK_STORE STW
  522. #define STACK_LOAD LWZ
  523. #else
  524. #define STACK_STORE STD
  525. #define STACK_LOAD LD
  526. #endif
  527. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
  528. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  529. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  530. {
  531. sljit_si i, tmp, offs;
  532. CHECK_ERROR();
  533. CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  534. set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  535. FAIL_IF(push_inst(compiler, MFLR | D(0)));
  536. offs = -(sljit_si)(sizeof(sljit_sw));
  537. FAIL_IF(push_inst(compiler, STACK_STORE | S(TMP_ZERO) | A(SLJIT_SP) | IMM(offs)));
  538. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  539. for (i = SLJIT_S0; i >= tmp; i--) {
  540. offs -= (sljit_si)(sizeof(sljit_sw));
  541. FAIL_IF(push_inst(compiler, STACK_STORE | S(i) | A(SLJIT_SP) | IMM(offs)));
  542. }
  543. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--) {
  544. offs -= (sljit_si)(sizeof(sljit_sw));
  545. FAIL_IF(push_inst(compiler, STACK_STORE | S(i) | A(SLJIT_SP) | IMM(offs)));
  546. }
  547. SLJIT_ASSERT(offs == -(sljit_si)GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1));
  548. #if (defined SLJIT_PPC_STACK_FRAME_V2 && SLJIT_PPC_STACK_FRAME_V2)
  549. FAIL_IF(push_inst(compiler, STACK_STORE | S(0) | A(SLJIT_SP) | IMM(2 * sizeof(sljit_sw))));
  550. #else
  551. FAIL_IF(push_inst(compiler, STACK_STORE | S(0) | A(SLJIT_SP) | IMM(sizeof(sljit_sw))));
  552. #endif
  553. FAIL_IF(push_inst(compiler, ADDI | D(TMP_ZERO) | A(0) | 0));
  554. if (args >= 1)
  555. FAIL_IF(push_inst(compiler, OR | S(SLJIT_R0) | A(SLJIT_S0) | B(SLJIT_R0)));
  556. if (args >= 2)
  557. FAIL_IF(push_inst(compiler, OR | S(SLJIT_R1) | A(SLJIT_S1) | B(SLJIT_R1)));
  558. if (args >= 3)
  559. FAIL_IF(push_inst(compiler, OR | S(SLJIT_R2) | A(SLJIT_S2) | B(SLJIT_R2)));
  560. local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
  561. local_size = (local_size + 15) & ~0xf;
  562. compiler->local_size = local_size;
  563. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  564. if (local_size <= SIMM_MAX)
  565. FAIL_IF(push_inst(compiler, STWU | S(SLJIT_SP) | A(SLJIT_SP) | IMM(-local_size)));
  566. else {
  567. FAIL_IF(load_immediate(compiler, 0, -local_size));
  568. FAIL_IF(push_inst(compiler, STWUX | S(SLJIT_SP) | A(SLJIT_SP) | B(0)));
  569. }
  570. #else
  571. if (local_size <= SIMM_MAX)
  572. FAIL_IF(push_inst(compiler, STDU | S(SLJIT_SP) | A(SLJIT_SP) | IMM(-local_size)));
  573. else {
  574. FAIL_IF(load_immediate(compiler, 0, -local_size));
  575. FAIL_IF(push_inst(compiler, STDUX | S(SLJIT_SP) | A(SLJIT_SP) | B(0)));
  576. }
  577. #endif
  578. return SLJIT_SUCCESS;
  579. }
  580. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_set_context(struct sljit_compiler *compiler,
  581. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  582. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  583. {
  584. CHECK_ERROR();
  585. CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  586. set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  587. local_size += GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1) + SLJIT_LOCALS_OFFSET;
  588. compiler->local_size = (local_size + 15) & ~0xf;
  589. return SLJIT_SUCCESS;
  590. }
  591. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
  592. {
  593. sljit_si i, tmp, offs;
  594. CHECK_ERROR();
  595. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  596. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  597. if (compiler->local_size <= SIMM_MAX)
  598. FAIL_IF(push_inst(compiler, ADDI | D(SLJIT_SP) | A(SLJIT_SP) | IMM(compiler->local_size)));
  599. else {
  600. FAIL_IF(load_immediate(compiler, 0, compiler->local_size));
  601. FAIL_IF(push_inst(compiler, ADD | D(SLJIT_SP) | A(SLJIT_SP) | B(0)));
  602. }
  603. #if (defined SLJIT_PPC_STACK_FRAME_V2 && SLJIT_PPC_STACK_FRAME_V2)
  604. FAIL_IF(push_inst(compiler, STACK_LOAD | D(0) | A(SLJIT_SP) | IMM(2 * sizeof(sljit_sw))));
  605. #else
  606. FAIL_IF(push_inst(compiler, STACK_LOAD | D(0) | A(SLJIT_SP) | IMM(sizeof(sljit_sw))));
  607. #endif
  608. offs = -(sljit_si)GET_SAVED_REGISTERS_SIZE(compiler->scratches, compiler->saveds, 1);
  609. tmp = compiler->scratches;
  610. for (i = SLJIT_FIRST_SAVED_REG; i <= tmp; i++) {
  611. FAIL_IF(push_inst(compiler, STACK_LOAD | D(i) | A(SLJIT_SP) | IMM(offs)));
  612. offs += (sljit_si)(sizeof(sljit_sw));
  613. }
  614. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  615. for (i = tmp; i <= SLJIT_S0; i++) {
  616. FAIL_IF(push_inst(compiler, STACK_LOAD | D(i) | A(SLJIT_SP) | IMM(offs)));
  617. offs += (sljit_si)(sizeof(sljit_sw));
  618. }
  619. FAIL_IF(push_inst(compiler, STACK_LOAD | D(TMP_ZERO) | A(SLJIT_SP) | IMM(offs)));
  620. SLJIT_ASSERT(offs == -(sljit_sw)(sizeof(sljit_sw)));
  621. FAIL_IF(push_inst(compiler, MTLR | S(0)));
  622. FAIL_IF(push_inst(compiler, BLR));
  623. return SLJIT_SUCCESS;
  624. }
  625. #undef STACK_STORE
  626. #undef STACK_LOAD
  627. /* --------------------------------------------------------------------- */
  628. /* Operators */
  629. /* --------------------------------------------------------------------- */
  630. /* i/x - immediate/indexed form
  631. n/w - no write-back / write-back (1 bit)
  632. s/l - store/load (1 bit)
  633. u/s - signed/unsigned (1 bit)
  634. w/b/h/i - word/byte/half/int allowed (2 bit)
  635. It contans 32 items, but not all are different. */
  636. /* 64 bit only: [reg+imm] must be aligned to 4 bytes. */
  637. #define INT_ALIGNED 0x10000
  638. /* 64-bit only: there is no lwau instruction. */
  639. #define UPDATE_REQ 0x20000
  640. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  641. #define ARCH_32_64(a, b) a
  642. #define INST_CODE_AND_DST(inst, flags, reg) \
  643. ((inst) | (((flags) & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg)))
  644. #else
  645. #define ARCH_32_64(a, b) b
  646. #define INST_CODE_AND_DST(inst, flags, reg) \
  647. (((inst) & ~(INT_ALIGNED | UPDATE_REQ)) | (((flags) & MEM_MASK) <= GPR_REG ? D(reg) : FD(reg)))
  648. #endif
  649. static SLJIT_CONST sljit_ins data_transfer_insts[64 + 8] = {
  650. /* -------- Unsigned -------- */
  651. /* Word. */
  652. /* u w n i s */ ARCH_32_64(HI(36) /* stw */, HI(62) | INT_ALIGNED | 0x0 /* std */),
  653. /* u w n i l */ ARCH_32_64(HI(32) /* lwz */, HI(58) | INT_ALIGNED | 0x0 /* ld */),
  654. /* u w n x s */ ARCH_32_64(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
  655. /* u w n x l */ ARCH_32_64(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
  656. /* u w w i s */ ARCH_32_64(HI(37) /* stwu */, HI(62) | INT_ALIGNED | 0x1 /* stdu */),
  657. /* u w w i l */ ARCH_32_64(HI(33) /* lwzu */, HI(58) | INT_ALIGNED | 0x1 /* ldu */),
  658. /* u w w x s */ ARCH_32_64(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
  659. /* u w w x l */ ARCH_32_64(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
  660. /* Byte. */
  661. /* u b n i s */ HI(38) /* stb */,
  662. /* u b n i l */ HI(34) /* lbz */,
  663. /* u b n x s */ HI(31) | LO(215) /* stbx */,
  664. /* u b n x l */ HI(31) | LO(87) /* lbzx */,
  665. /* u b w i s */ HI(39) /* stbu */,
  666. /* u b w i l */ HI(35) /* lbzu */,
  667. /* u b w x s */ HI(31) | LO(247) /* stbux */,
  668. /* u b w x l */ HI(31) | LO(119) /* lbzux */,
  669. /* Half. */
  670. /* u h n i s */ HI(44) /* sth */,
  671. /* u h n i l */ HI(40) /* lhz */,
  672. /* u h n x s */ HI(31) | LO(407) /* sthx */,
  673. /* u h n x l */ HI(31) | LO(279) /* lhzx */,
  674. /* u h w i s */ HI(45) /* sthu */,
  675. /* u h w i l */ HI(41) /* lhzu */,
  676. /* u h w x s */ HI(31) | LO(439) /* sthux */,
  677. /* u h w x l */ HI(31) | LO(311) /* lhzux */,
  678. /* Int. */
  679. /* u i n i s */ HI(36) /* stw */,
  680. /* u i n i l */ HI(32) /* lwz */,
  681. /* u i n x s */ HI(31) | LO(151) /* stwx */,
  682. /* u i n x l */ HI(31) | LO(23) /* lwzx */,
  683. /* u i w i s */ HI(37) /* stwu */,
  684. /* u i w i l */ HI(33) /* lwzu */,
  685. /* u i w x s */ HI(31) | LO(183) /* stwux */,
  686. /* u i w x l */ HI(31) | LO(55) /* lwzux */,
  687. /* -------- Signed -------- */
  688. /* Word. */
  689. /* s w n i s */ ARCH_32_64(HI(36) /* stw */, HI(62) | INT_ALIGNED | 0x0 /* std */),
  690. /* s w n i l */ ARCH_32_64(HI(32) /* lwz */, HI(58) | INT_ALIGNED | 0x0 /* ld */),
  691. /* s w n x s */ ARCH_32_64(HI(31) | LO(151) /* stwx */, HI(31) | LO(149) /* stdx */),
  692. /* s w n x l */ ARCH_32_64(HI(31) | LO(23) /* lwzx */, HI(31) | LO(21) /* ldx */),
  693. /* s w w i s */ ARCH_32_64(HI(37) /* stwu */, HI(62) | INT_ALIGNED | 0x1 /* stdu */),
  694. /* s w w i l */ ARCH_32_64(HI(33) /* lwzu */, HI(58) | INT_ALIGNED | 0x1 /* ldu */),
  695. /* s w w x s */ ARCH_32_64(HI(31) | LO(183) /* stwux */, HI(31) | LO(181) /* stdux */),
  696. /* s w w x l */ ARCH_32_64(HI(31) | LO(55) /* lwzux */, HI(31) | LO(53) /* ldux */),
  697. /* Byte. */
  698. /* s b n i s */ HI(38) /* stb */,
  699. /* s b n i l */ HI(34) /* lbz */ /* EXTS_REQ */,
  700. /* s b n x s */ HI(31) | LO(215) /* stbx */,
  701. /* s b n x l */ HI(31) | LO(87) /* lbzx */ /* EXTS_REQ */,
  702. /* s b w i s */ HI(39) /* stbu */,
  703. /* s b w i l */ HI(35) /* lbzu */ /* EXTS_REQ */,
  704. /* s b w x s */ HI(31) | LO(247) /* stbux */,
  705. /* s b w x l */ HI(31) | LO(119) /* lbzux */ /* EXTS_REQ */,
  706. /* Half. */
  707. /* s h n i s */ HI(44) /* sth */,
  708. /* s h n i l */ HI(42) /* lha */,
  709. /* s h n x s */ HI(31) | LO(407) /* sthx */,
  710. /* s h n x l */ HI(31) | LO(343) /* lhax */,
  711. /* s h w i s */ HI(45) /* sthu */,
  712. /* s h w i l */ HI(43) /* lhau */,
  713. /* s h w x s */ HI(31) | LO(439) /* sthux */,
  714. /* s h w x l */ HI(31) | LO(375) /* lhaux */,
  715. /* Int. */
  716. /* s i n i s */ HI(36) /* stw */,
  717. /* s i n i l */ ARCH_32_64(HI(32) /* lwz */, HI(58) | INT_ALIGNED | 0x2 /* lwa */),
  718. /* s i n x s */ HI(31) | LO(151) /* stwx */,
  719. /* s i n x l */ ARCH_32_64(HI(31) | LO(23) /* lwzx */, HI(31) | LO(341) /* lwax */),
  720. /* s i w i s */ HI(37) /* stwu */,
  721. /* s i w i l */ ARCH_32_64(HI(33) /* lwzu */, HI(58) | INT_ALIGNED | UPDATE_REQ | 0x2 /* lwa */),
  722. /* s i w x s */ HI(31) | LO(183) /* stwux */,
  723. /* s i w x l */ ARCH_32_64(HI(31) | LO(55) /* lwzux */, HI(31) | LO(373) /* lwaux */),
  724. /* -------- Double -------- */
  725. /* d n i s */ HI(54) /* stfd */,
  726. /* d n i l */ HI(50) /* lfd */,
  727. /* d n x s */ HI(31) | LO(727) /* stfdx */,
  728. /* d n x l */ HI(31) | LO(599) /* lfdx */,
  729. /* s n i s */ HI(52) /* stfs */,
  730. /* s n i l */ HI(48) /* lfs */,
  731. /* s n x s */ HI(31) | LO(663) /* stfsx */,
  732. /* s n x l */ HI(31) | LO(535) /* lfsx */,
  733. };
  734. #undef ARCH_32_64
  735. /* Simple cases, (no caching is required). */
  736. static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si inp_flags, sljit_si reg, sljit_si arg, sljit_sw argw)
  737. {
  738. sljit_ins inst;
  739. /* Should work when (arg & REG_MASK) == 0. */
  740. SLJIT_COMPILE_ASSERT(A(0) == 0, a0_must_be_0);
  741. SLJIT_ASSERT(arg & SLJIT_MEM);
  742. if (arg & OFFS_REG_MASK) {
  743. if (argw & 0x3)
  744. return 0;
  745. if (inp_flags & ARG_TEST)
  746. return 1;
  747. inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
  748. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  749. FAIL_IF(push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | B(OFFS_REG(arg))));
  750. return -1;
  751. }
  752. if (SLJIT_UNLIKELY(!(arg & REG_MASK)))
  753. inp_flags &= ~WRITE_BACK;
  754. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  755. inst = data_transfer_insts[inp_flags & MEM_MASK];
  756. SLJIT_ASSERT((arg & REG_MASK) || !(inst & UPDATE_REQ));
  757. if (argw > SIMM_MAX || argw < SIMM_MIN || ((inst & INT_ALIGNED) && (argw & 0x3)) || (inst & UPDATE_REQ))
  758. return 0;
  759. if (inp_flags & ARG_TEST)
  760. return 1;
  761. #endif
  762. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  763. if (argw > SIMM_MAX || argw < SIMM_MIN)
  764. return 0;
  765. if (inp_flags & ARG_TEST)
  766. return 1;
  767. inst = data_transfer_insts[inp_flags & MEM_MASK];
  768. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  769. #endif
  770. FAIL_IF(push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | IMM(argw)));
  771. return -1;
  772. }
  773. /* See getput_arg below.
  774. Note: can_cache is called only for binary operators. Those operator always
  775. uses word arguments without write back. */
  776. static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
  777. {
  778. sljit_sw high_short, next_high_short;
  779. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  780. sljit_sw diff;
  781. #endif
  782. SLJIT_ASSERT((arg & SLJIT_MEM) && (next_arg & SLJIT_MEM));
  783. if (arg & OFFS_REG_MASK)
  784. return ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && (argw & 0x3) == (next_argw & 0x3));
  785. if (next_arg & OFFS_REG_MASK)
  786. return 0;
  787. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  788. high_short = (argw + ((argw & 0x8000) << 1)) & ~0xffff;
  789. next_high_short = (next_argw + ((next_argw & 0x8000) << 1)) & ~0xffff;
  790. return high_short == next_high_short;
  791. #else
  792. if (argw <= 0x7fffffffl && argw >= -0x80000000l) {
  793. high_short = (argw + ((argw & 0x8000) << 1)) & ~0xffff;
  794. next_high_short = (next_argw + ((next_argw & 0x8000) << 1)) & ~0xffff;
  795. if (high_short == next_high_short)
  796. return 1;
  797. }
  798. diff = argw - next_argw;
  799. if (!(arg & REG_MASK))
  800. return diff <= SIMM_MAX && diff >= SIMM_MIN;
  801. if (arg == next_arg && diff <= SIMM_MAX && diff >= SIMM_MIN)
  802. return 1;
  803. return 0;
  804. #endif
  805. }
  806. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  807. #define ADJUST_CACHED_IMM(imm) \
  808. if ((inst & INT_ALIGNED) && (imm & 0x3)) { \
  809. /* Adjust cached value. Fortunately this is really a rare case */ \
  810. compiler->cache_argw += imm & 0x3; \
  811. FAIL_IF(push_inst(compiler, ADDI | D(TMP_REG3) | A(TMP_REG3) | (imm & 0x3))); \
  812. imm &= ~0x3; \
  813. }
  814. #endif
  815. /* Emit the necessary instructions. See can_cache above. */
  816. static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si inp_flags, sljit_si reg, sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
  817. {
  818. sljit_si tmp_r;
  819. sljit_ins inst;
  820. sljit_sw high_short, next_high_short;
  821. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  822. sljit_sw diff;
  823. #endif
  824. SLJIT_ASSERT(arg & SLJIT_MEM);
  825. tmp_r = ((inp_flags & LOAD_DATA) && ((inp_flags) & MEM_MASK) <= GPR_REG) ? reg : TMP_REG1;
  826. /* Special case for "mov reg, [reg, ... ]". */
  827. if ((arg & REG_MASK) == tmp_r)
  828. tmp_r = TMP_REG1;
  829. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  830. argw &= 0x3;
  831. /* Otherwise getput_arg_fast would capture it. */
  832. SLJIT_ASSERT(argw);
  833. if ((SLJIT_MEM | (arg & OFFS_REG_MASK)) == compiler->cache_arg && argw == compiler->cache_argw)
  834. tmp_r = TMP_REG3;
  835. else {
  836. if ((arg & OFFS_REG_MASK) == (next_arg & OFFS_REG_MASK) && argw == (next_argw & 0x3)) {
  837. compiler->cache_arg = SLJIT_MEM | (arg & OFFS_REG_MASK);
  838. compiler->cache_argw = argw;
  839. tmp_r = TMP_REG3;
  840. }
  841. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  842. FAIL_IF(push_inst(compiler, RLWINM | S(OFFS_REG(arg)) | A(tmp_r) | (argw << 11) | ((31 - argw) << 1)));
  843. #else
  844. FAIL_IF(push_inst(compiler, RLDI(tmp_r, OFFS_REG(arg), argw, 63 - argw, 1)));
  845. #endif
  846. }
  847. inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
  848. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  849. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | B(tmp_r));
  850. }
  851. if (SLJIT_UNLIKELY(!(arg & REG_MASK)))
  852. inp_flags &= ~WRITE_BACK;
  853. inst = data_transfer_insts[inp_flags & MEM_MASK];
  854. SLJIT_ASSERT((arg & REG_MASK) || !(inst & UPDATE_REQ));
  855. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  856. if (argw <= 0x7fff7fffl && argw >= -0x80000000l
  857. && (!(inst & INT_ALIGNED) || !(argw & 0x3)) && !(inst & UPDATE_REQ)) {
  858. #endif
  859. arg &= REG_MASK;
  860. high_short = (sljit_si)(argw + ((argw & 0x8000) << 1)) & ~0xffff;
  861. /* The getput_arg_fast should handle this otherwise. */
  862. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  863. SLJIT_ASSERT(high_short && high_short <= 0x7fffffffl && high_short >= -0x80000000l);
  864. #else
  865. SLJIT_ASSERT(high_short && !(inst & (INT_ALIGNED | UPDATE_REQ)));
  866. #endif
  867. if (inp_flags & WRITE_BACK) {
  868. if (arg == reg) {
  869. FAIL_IF(push_inst(compiler, OR | S(reg) | A(tmp_r) | B(reg)));
  870. reg = tmp_r;
  871. }
  872. tmp_r = arg;
  873. FAIL_IF(push_inst(compiler, ADDIS | D(arg) | A(arg) | IMM(high_short >> 16)));
  874. }
  875. else if (compiler->cache_arg != (SLJIT_MEM | arg) || high_short != compiler->cache_argw) {
  876. if ((next_arg & SLJIT_MEM) && !(next_arg & OFFS_REG_MASK)) {
  877. next_high_short = (sljit_si)(next_argw + ((next_argw & 0x8000) << 1)) & ~0xffff;
  878. if (high_short == next_high_short) {
  879. compiler->cache_arg = SLJIT_MEM | arg;
  880. compiler->cache_argw = high_short;
  881. tmp_r = TMP_REG3;
  882. }
  883. }
  884. FAIL_IF(push_inst(compiler, ADDIS | D(tmp_r) | A(arg & REG_MASK) | IMM(high_short >> 16)));
  885. }
  886. else
  887. tmp_r = TMP_REG3;
  888. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(tmp_r) | IMM(argw));
  889. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  890. }
  891. /* Everything else is PPC-64 only. */
  892. if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
  893. diff = argw - compiler->cache_argw;
  894. if ((compiler->cache_arg & SLJIT_IMM) && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  895. ADJUST_CACHED_IMM(diff);
  896. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(TMP_REG3) | IMM(diff));
  897. }
  898. diff = argw - next_argw;
  899. if ((next_arg & SLJIT_MEM) && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  900. SLJIT_ASSERT(inp_flags & LOAD_DATA);
  901. compiler->cache_arg = SLJIT_IMM;
  902. compiler->cache_argw = argw;
  903. tmp_r = TMP_REG3;
  904. }
  905. FAIL_IF(load_immediate(compiler, tmp_r, argw));
  906. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(tmp_r));
  907. }
  908. diff = argw - compiler->cache_argw;
  909. if (compiler->cache_arg == arg && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  910. SLJIT_ASSERT(!(inp_flags & WRITE_BACK) && !(inst & UPDATE_REQ));
  911. ADJUST_CACHED_IMM(diff);
  912. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(TMP_REG3) | IMM(diff));
  913. }
  914. if ((compiler->cache_arg & SLJIT_IMM) && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  915. inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
  916. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  917. if (compiler->cache_argw != argw) {
  918. FAIL_IF(push_inst(compiler, ADDI | D(TMP_REG3) | A(TMP_REG3) | IMM(diff)));
  919. compiler->cache_argw = argw;
  920. }
  921. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | B(TMP_REG3));
  922. }
  923. if (argw == next_argw && (next_arg & SLJIT_MEM)) {
  924. SLJIT_ASSERT(inp_flags & LOAD_DATA);
  925. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  926. compiler->cache_arg = SLJIT_IMM;
  927. compiler->cache_argw = argw;
  928. inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
  929. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  930. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | B(TMP_REG3));
  931. }
  932. diff = argw - next_argw;
  933. if (arg == next_arg && !(inp_flags & WRITE_BACK) && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  934. SLJIT_ASSERT(inp_flags & LOAD_DATA);
  935. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  936. FAIL_IF(push_inst(compiler, ADD | D(TMP_REG3) | A(TMP_REG3) | B(arg & REG_MASK)));
  937. compiler->cache_arg = arg;
  938. compiler->cache_argw = argw;
  939. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(TMP_REG3));
  940. }
  941. if ((next_arg & SLJIT_MEM) && !(next_arg & OFFS_REG_MASK) && diff <= SIMM_MAX && diff >= SIMM_MIN) {
  942. SLJIT_ASSERT(inp_flags & LOAD_DATA);
  943. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  944. compiler->cache_arg = SLJIT_IMM;
  945. compiler->cache_argw = argw;
  946. tmp_r = TMP_REG3;
  947. }
  948. else
  949. FAIL_IF(load_immediate(compiler, tmp_r, argw));
  950. /* Get the indexed version instead of the normal one. */
  951. inst = data_transfer_insts[(inp_flags | INDEXED) & MEM_MASK];
  952. SLJIT_ASSERT(!(inst & (INT_ALIGNED | UPDATE_REQ)));
  953. return push_inst(compiler, INST_CODE_AND_DST(inst, inp_flags, reg) | A(arg & REG_MASK) | B(tmp_r));
  954. #endif
  955. }
  956. 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)
  957. {
  958. if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
  959. return compiler->error;
  960. return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
  961. }
  962. static sljit_si emit_op(struct sljit_compiler *compiler, sljit_si op, sljit_si input_flags,
  963. sljit_si dst, sljit_sw dstw,
  964. sljit_si src1, sljit_sw src1w,
  965. sljit_si src2, sljit_sw src2w)
  966. {
  967. /* arg1 goes to TMP_REG1 or src reg
  968. arg2 goes to TMP_REG2, imm or src reg
  969. TMP_REG3 can be used for caching
  970. result goes to TMP_REG2, so put result can use TMP_REG1 and TMP_REG3. */
  971. sljit_si dst_r;
  972. sljit_si src1_r;
  973. sljit_si src2_r;
  974. sljit_si sugg_src2_r = TMP_REG2;
  975. sljit_si flags = input_flags & (ALT_FORM1 | ALT_FORM2 | ALT_FORM3 | ALT_FORM4 | ALT_FORM5 | ALT_FORM6 | ALT_SIGN_EXT | ALT_SET_FLAGS);
  976. if (!(input_flags & ALT_KEEP_CACHE)) {
  977. compiler->cache_arg = 0;
  978. compiler->cache_argw = 0;
  979. }
  980. /* Destination check. */
  981. if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED)) {
  982. if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI && !(src2 & SLJIT_MEM))
  983. return SLJIT_SUCCESS;
  984. dst_r = TMP_REG2;
  985. }
  986. else if (FAST_IS_REG(dst)) {
  987. dst_r = dst;
  988. flags |= REG_DEST;
  989. if (op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
  990. sugg_src2_r = dst_r;
  991. }
  992. else {
  993. SLJIT_ASSERT(dst & SLJIT_MEM);
  994. if (getput_arg_fast(compiler, input_flags | ARG_TEST, TMP_REG2, dst, dstw)) {
  995. flags |= FAST_DEST;
  996. dst_r = TMP_REG2;
  997. }
  998. else {
  999. flags |= SLOW_DEST;
  1000. dst_r = 0;
  1001. }
  1002. }
  1003. /* Source 1. */
  1004. if (FAST_IS_REG(src1)) {
  1005. src1_r = src1;
  1006. flags |= REG1_SOURCE;
  1007. }
  1008. else if (src1 & SLJIT_IMM) {
  1009. FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
  1010. src1_r = TMP_REG1;
  1011. }
  1012. else if (getput_arg_fast(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w)) {
  1013. FAIL_IF(compiler->error);
  1014. src1_r = TMP_REG1;
  1015. }
  1016. else
  1017. src1_r = 0;
  1018. /* Source 2. */
  1019. if (FAST_IS_REG(src2)) {
  1020. src2_r = src2;
  1021. flags |= REG2_SOURCE;
  1022. if (!(flags & REG_DEST) && op >= SLJIT_MOV && op <= SLJIT_MOVU_SI)
  1023. dst_r = src2_r;
  1024. }
  1025. else if (src2 & SLJIT_IMM) {
  1026. FAIL_IF(load_immediate(compiler, sugg_src2_r, src2w));
  1027. src2_r = sugg_src2_r;
  1028. }
  1029. else if (getput_arg_fast(compiler, input_flags | LOAD_DATA, sugg_src2_r, src2, src2w)) {
  1030. FAIL_IF(compiler->error);
  1031. src2_r = sugg_src2_r;
  1032. }
  1033. else
  1034. src2_r = 0;
  1035. /* src1_r, src2_r and dst_r can be zero (=unprocessed).
  1036. All arguments are complex addressing modes, and it is a binary operator. */
  1037. if (src1_r == 0 && src2_r == 0 && dst_r == 0) {
  1038. if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
  1039. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG2, src2, src2w, src1, src1w));
  1040. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
  1041. }
  1042. else {
  1043. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
  1044. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG2, src2, src2w, dst, dstw));
  1045. }
  1046. src1_r = TMP_REG1;
  1047. src2_r = TMP_REG2;
  1048. }
  1049. else if (src1_r == 0 && src2_r == 0) {
  1050. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w, src2, src2w));
  1051. src1_r = TMP_REG1;
  1052. }
  1053. else if (src1_r == 0 && dst_r == 0) {
  1054. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w, dst, dstw));
  1055. src1_r = TMP_REG1;
  1056. }
  1057. else if (src2_r == 0 && dst_r == 0) {
  1058. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, sugg_src2_r, src2, src2w, dst, dstw));
  1059. src2_r = sugg_src2_r;
  1060. }
  1061. if (dst_r == 0)
  1062. dst_r = TMP_REG2;
  1063. if (src1_r == 0) {
  1064. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, TMP_REG1, src1, src1w, 0, 0));
  1065. src1_r = TMP_REG1;
  1066. }
  1067. if (src2_r == 0) {
  1068. FAIL_IF(getput_arg(compiler, input_flags | LOAD_DATA, sugg_src2_r, src2, src2w, 0, 0));
  1069. src2_r = sugg_src2_r;
  1070. }
  1071. FAIL_IF(emit_single_op(compiler, op, flags, dst_r, src1_r, src2_r));
  1072. if (flags & (FAST_DEST | SLOW_DEST)) {
  1073. if (flags & FAST_DEST)
  1074. FAIL_IF(getput_arg_fast(compiler, input_flags, dst_r, dst, dstw));
  1075. else
  1076. FAIL_IF(getput_arg(compiler, input_flags, dst_r, dst, dstw, 0, 0));
  1077. }
  1078. return SLJIT_SUCCESS;
  1079. }
  1080. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
  1081. {
  1082. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1083. sljit_si int_op = op & SLJIT_INT_OP;
  1084. #endif
  1085. CHECK_ERROR();
  1086. CHECK(check_sljit_emit_op0(compiler, op));
  1087. op = GET_OPCODE(op);
  1088. switch (op) {
  1089. case SLJIT_BREAKPOINT:
  1090. case SLJIT_NOP:
  1091. return push_inst(compiler, NOP);
  1092. case SLJIT_LUMUL:
  1093. case SLJIT_LSMUL:
  1094. FAIL_IF(push_inst(compiler, OR | S(SLJIT_R0) | A(TMP_REG1) | B(SLJIT_R0)));
  1095. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1096. FAIL_IF(push_inst(compiler, MULLD | D(SLJIT_R0) | A(TMP_REG1) | B(SLJIT_R1)));
  1097. return push_inst(compiler, (op == SLJIT_LUMUL ? MULHDU : MULHD) | D(SLJIT_R1) | A(TMP_REG1) | B(SLJIT_R1));
  1098. #else
  1099. FAIL_IF(push_inst(compiler, MULLW | D(SLJIT_R0) | A(TMP_REG1) | B(SLJIT_R1)));
  1100. return push_inst(compiler, (op == SLJIT_LUMUL ? MULHWU : MULHW) | D(SLJIT_R1) | A(TMP_REG1) | B(SLJIT_R1));
  1101. #endif
  1102. case SLJIT_UDIVMOD:
  1103. case SLJIT_SDIVMOD:
  1104. FAIL_IF(push_inst(compiler, OR | S(SLJIT_R0) | A(TMP_REG1) | B(SLJIT_R0)));
  1105. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1106. FAIL_IF(push_inst(compiler, (int_op ? (op == SLJIT_UDIVMOD ? DIVWU : DIVW) : (op == SLJIT_UDIVMOD ? DIVDU : DIVD)) | D(SLJIT_R0) | A(SLJIT_R0) | B(SLJIT_R1)));
  1107. FAIL_IF(push_inst(compiler, (int_op ? MULLW : MULLD) | D(SLJIT_R1) | A(SLJIT_R0) | B(SLJIT_R1)));
  1108. #else
  1109. FAIL_IF(push_inst(compiler, (op == SLJIT_UDIVMOD ? DIVWU : DIVW) | D(SLJIT_R0) | A(SLJIT_R0) | B(SLJIT_R1)));
  1110. FAIL_IF(push_inst(compiler, MULLW | D(SLJIT_R1) | A(SLJIT_R0) | B(SLJIT_R1)));
  1111. #endif
  1112. return push_inst(compiler, SUBF | D(SLJIT_R1) | A(SLJIT_R1) | B(TMP_REG1));
  1113. case SLJIT_UDIVI:
  1114. case SLJIT_SDIVI:
  1115. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1116. return push_inst(compiler, (int_op ? (op == SLJIT_UDIVI ? DIVWU : DIVW) : (op == SLJIT_UDIVI ? DIVDU : DIVD)) | D(SLJIT_R0) | A(SLJIT_R0) | B(SLJIT_R1));
  1117. #else
  1118. return push_inst(compiler, (op == SLJIT_UDIVI ? DIVWU : DIVW) | D(SLJIT_R0) | A(SLJIT_R0) | B(SLJIT_R1));
  1119. #endif
  1120. }
  1121. return SLJIT_SUCCESS;
  1122. }
  1123. #define EMIT_MOV(type, type_flags, type_cast) \
  1124. emit_op(compiler, (src & SLJIT_IMM) ? SLJIT_MOV : type, flags | (type_flags), dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? type_cast srcw : srcw)
  1125. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
  1126. sljit_si dst, sljit_sw dstw,
  1127. sljit_si src, sljit_sw srcw)
  1128. {
  1129. sljit_si flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
  1130. sljit_si op_flags = GET_ALL_FLAGS(op);
  1131. CHECK_ERROR();
  1132. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1133. ADJUST_LOCAL_OFFSET(dst, dstw);
  1134. ADJUST_LOCAL_OFFSET(src, srcw);
  1135. op = GET_OPCODE(op);
  1136. if ((src & SLJIT_IMM) && srcw == 0)
  1137. src = TMP_ZERO;
  1138. if (op_flags & SLJIT_SET_O)
  1139. FAIL_IF(push_inst(compiler, MTXER | S(TMP_ZERO)));
  1140. if (op_flags & SLJIT_INT_OP) {
  1141. if (op < SLJIT_NOT) {
  1142. if (FAST_IS_REG(src) && src == dst) {
  1143. if (!TYPE_CAST_NEEDED(op))
  1144. return SLJIT_SUCCESS;
  1145. }
  1146. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1147. if (op == SLJIT_MOV_SI && (src & SLJIT_MEM))
  1148. op = SLJIT_MOV_UI;
  1149. if (op == SLJIT_MOVU_SI && (src & SLJIT_MEM))
  1150. op = SLJIT_MOVU_UI;
  1151. if (op == SLJIT_MOV_UI && (src & SLJIT_IMM))
  1152. op = SLJIT_MOV_SI;
  1153. if (op == SLJIT_MOVU_UI && (src & SLJIT_IMM))
  1154. op = SLJIT_MOVU_SI;
  1155. #endif
  1156. }
  1157. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1158. else {
  1159. /* Most operations expect sign extended arguments. */
  1160. flags |= INT_DATA | SIGNED_DATA;
  1161. if (src & SLJIT_IMM)
  1162. srcw = (sljit_si)srcw;
  1163. }
  1164. #endif
  1165. }
  1166. switch (op) {
  1167. case SLJIT_MOV:
  1168. case SLJIT_MOV_P:
  1169. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  1170. case SLJIT_MOV_UI:
  1171. case SLJIT_MOV_SI:
  1172. #endif
  1173. return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA, dst, dstw, TMP_REG1, 0, src, srcw);
  1174. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1175. case SLJIT_MOV_UI:
  1176. return EMIT_MOV(SLJIT_MOV_UI, INT_DATA, (sljit_ui));
  1177. case SLJIT_MOV_SI:
  1178. return EMIT_MOV(SLJIT_MOV_SI, INT_DATA | SIGNED_DATA, (sljit_si));
  1179. #endif
  1180. case SLJIT_MOV_UB:
  1181. return EMIT_MOV(SLJIT_MOV_UB, BYTE_DATA, (sljit_ub));
  1182. case SLJIT_MOV_SB:
  1183. return EMIT_MOV(SLJIT_MOV_SB, BYTE_DATA | SIGNED_DATA, (sljit_sb));
  1184. case SLJIT_MOV_UH:
  1185. return EMIT_MOV(SLJIT_MOV_UH, HALF_DATA, (sljit_uh));
  1186. case SLJIT_MOV_SH:
  1187. return EMIT_MOV(SLJIT_MOV_SH, HALF_DATA | SIGNED_DATA, (sljit_sh));
  1188. case SLJIT_MOVU:
  1189. case SLJIT_MOVU_P:
  1190. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  1191. case SLJIT_MOVU_UI:
  1192. case SLJIT_MOVU_SI:
  1193. #endif
  1194. return emit_op(compiler, SLJIT_MOV, flags | WORD_DATA | WRITE_BACK, dst, dstw, TMP_REG1, 0, src, srcw);
  1195. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1196. case SLJIT_MOVU_UI:
  1197. return EMIT_MOV(SLJIT_MOV_UI, INT_DATA | WRITE_BACK, (sljit_ui));
  1198. case SLJIT_MOVU_SI:
  1199. return EMIT_MOV(SLJIT_MOV_SI, INT_DATA | SIGNED_DATA | WRITE_BACK, (sljit_si));
  1200. #endif
  1201. case SLJIT_MOVU_UB:
  1202. return EMIT_MOV(SLJIT_MOV_UB, BYTE_DATA | WRITE_BACK, (sljit_ub));
  1203. case SLJIT_MOVU_SB:
  1204. return EMIT_MOV(SLJIT_MOV_SB, BYTE_DATA | SIGNED_DATA | WRITE_BACK, (sljit_sb));
  1205. case SLJIT_MOVU_UH:
  1206. return EMIT_MOV(SLJIT_MOV_UH, HALF_DATA | WRITE_BACK, (sljit_uh));
  1207. case SLJIT_MOVU_SH:
  1208. return EMIT_MOV(SLJIT_MOV_SH, HALF_DATA | SIGNED_DATA | WRITE_BACK, (sljit_sh));
  1209. case SLJIT_NOT:
  1210. return emit_op(compiler, SLJIT_NOT, flags, dst, dstw, TMP_REG1, 0, src, srcw);
  1211. case SLJIT_NEG:
  1212. return emit_op(compiler, SLJIT_NEG, flags, dst, dstw, TMP_REG1, 0, src, srcw);
  1213. case SLJIT_CLZ:
  1214. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1215. return emit_op(compiler, SLJIT_CLZ, flags | (!(op_flags & SLJIT_INT_OP) ? 0 : ALT_FORM1), dst, dstw, TMP_REG1, 0, src, srcw);
  1216. #else
  1217. return emit_op(compiler, SLJIT_CLZ, flags, dst, dstw, TMP_REG1, 0, src, srcw);
  1218. #endif
  1219. }
  1220. return SLJIT_SUCCESS;
  1221. }
  1222. #undef EMIT_MOV
  1223. #define TEST_SL_IMM(src, srcw) \
  1224. (((src) & SLJIT_IMM) && (srcw) <= SIMM_MAX && (srcw) >= SIMM_MIN)
  1225. #define TEST_UL_IMM(src, srcw) \
  1226. (((src) & SLJIT_IMM) && !((srcw) & ~0xffff))
  1227. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1228. #define TEST_SH_IMM(src, srcw) \
  1229. (((src) & SLJIT_IMM) && !((srcw) & 0xffff) && (srcw) <= 0x7fffffffl && (srcw) >= -0x80000000l)
  1230. #else
  1231. #define TEST_SH_IMM(src, srcw) \
  1232. (((src) & SLJIT_IMM) && !((srcw) & 0xffff))
  1233. #endif
  1234. #define TEST_UH_IMM(src, srcw) \
  1235. (((src) & SLJIT_IMM) && !((srcw) & ~0xffff0000))
  1236. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1237. #define TEST_ADD_IMM(src, srcw) \
  1238. (((src) & SLJIT_IMM) && (srcw) <= 0x7fff7fffl && (srcw) >= -0x80000000l)
  1239. #else
  1240. #define TEST_ADD_IMM(src, srcw) \
  1241. ((src) & SLJIT_IMM)
  1242. #endif
  1243. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1244. #define TEST_UI_IMM(src, srcw) \
  1245. (((src) & SLJIT_IMM) && !((srcw) & ~0xffffffff))
  1246. #else
  1247. #define TEST_UI_IMM(src, srcw) \
  1248. ((src) & SLJIT_IMM)
  1249. #endif
  1250. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
  1251. sljit_si dst, sljit_sw dstw,
  1252. sljit_si src1, sljit_sw src1w,
  1253. sljit_si src2, sljit_sw src2w)
  1254. {
  1255. sljit_si flags = GET_FLAGS(op) ? ALT_SET_FLAGS : 0;
  1256. CHECK_ERROR();
  1257. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1258. ADJUST_LOCAL_OFFSET(dst, dstw);
  1259. ADJUST_LOCAL_OFFSET(src1, src1w);
  1260. ADJUST_LOCAL_OFFSET(src2, src2w);
  1261. if ((src1 & SLJIT_IMM) && src1w == 0)
  1262. src1 = TMP_ZERO;
  1263. if ((src2 & SLJIT_IMM) && src2w == 0)
  1264. src2 = TMP_ZERO;
  1265. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1266. if (op & SLJIT_INT_OP) {
  1267. /* Most operations expect sign extended arguments. */
  1268. flags |= INT_DATA | SIGNED_DATA;
  1269. if (src1 & SLJIT_IMM)
  1270. src1w = (sljit_si)(src1w);
  1271. if (src2 & SLJIT_IMM)
  1272. src2w = (sljit_si)(src2w);
  1273. if (GET_FLAGS(op))
  1274. flags |= ALT_SIGN_EXT;
  1275. }
  1276. #endif
  1277. if (op & SLJIT_SET_O)
  1278. FAIL_IF(push_inst(compiler, MTXER | S(TMP_ZERO)));
  1279. if (src2 == TMP_REG2)
  1280. flags |= ALT_KEEP_CACHE;
  1281. switch (GET_OPCODE(op)) {
  1282. case SLJIT_ADD:
  1283. if (!GET_FLAGS(op) && ((src1 | src2) & SLJIT_IMM)) {
  1284. if (TEST_SL_IMM(src2, src2w)) {
  1285. compiler->imm = src2w & 0xffff;
  1286. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
  1287. }
  1288. if (TEST_SL_IMM(src1, src1w)) {
  1289. compiler->imm = src1w & 0xffff;
  1290. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
  1291. }
  1292. if (TEST_SH_IMM(src2, src2w)) {
  1293. compiler->imm = (src2w >> 16) & 0xffff;
  1294. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
  1295. }
  1296. if (TEST_SH_IMM(src1, src1w)) {
  1297. compiler->imm = (src1w >> 16) & 0xffff;
  1298. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
  1299. }
  1300. /* Range between -1 and -32768 is covered above. */
  1301. if (TEST_ADD_IMM(src2, src2w)) {
  1302. compiler->imm = src2w & 0xffffffff;
  1303. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src1, src1w, TMP_REG2, 0);
  1304. }
  1305. if (TEST_ADD_IMM(src1, src1w)) {
  1306. compiler->imm = src1w & 0xffffffff;
  1307. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src2, src2w, TMP_REG2, 0);
  1308. }
  1309. }
  1310. if (!(GET_FLAGS(op) & (SLJIT_SET_E | SLJIT_SET_O))) {
  1311. if (TEST_SL_IMM(src2, src2w)) {
  1312. compiler->imm = src2w & 0xffff;
  1313. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
  1314. }
  1315. if (TEST_SL_IMM(src1, src1w)) {
  1316. compiler->imm = src1w & 0xffff;
  1317. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
  1318. }
  1319. }
  1320. return emit_op(compiler, SLJIT_ADD, flags, dst, dstw, src1, src1w, src2, src2w);
  1321. case SLJIT_ADDC:
  1322. return emit_op(compiler, SLJIT_ADDC, flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
  1323. case SLJIT_SUB:
  1324. if (!GET_FLAGS(op) && ((src1 | src2) & SLJIT_IMM)) {
  1325. if (TEST_SL_IMM(src2, -src2w)) {
  1326. compiler->imm = (-src2w) & 0xffff;
  1327. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
  1328. }
  1329. if (TEST_SL_IMM(src1, src1w)) {
  1330. compiler->imm = src1w & 0xffff;
  1331. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
  1332. }
  1333. if (TEST_SH_IMM(src2, -src2w)) {
  1334. compiler->imm = ((-src2w) >> 16) & 0xffff;
  1335. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
  1336. }
  1337. /* Range between -1 and -32768 is covered above. */
  1338. if (TEST_ADD_IMM(src2, -src2w)) {
  1339. compiler->imm = -src2w & 0xffffffff;
  1340. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM4, dst, dstw, src1, src1w, TMP_REG2, 0);
  1341. }
  1342. }
  1343. if (dst == SLJIT_UNUSED && (op & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S)) && !(op & (SLJIT_SET_O | SLJIT_SET_C))) {
  1344. if (!(op & SLJIT_SET_U)) {
  1345. /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
  1346. if (TEST_SL_IMM(src2, src2w)) {
  1347. compiler->imm = src2w & 0xffff;
  1348. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
  1349. }
  1350. if (GET_FLAGS(op) == SLJIT_SET_E && TEST_SL_IMM(src1, src1w)) {
  1351. compiler->imm = src1w & 0xffff;
  1352. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
  1353. }
  1354. }
  1355. if (!(op & (SLJIT_SET_E | SLJIT_SET_S))) {
  1356. /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
  1357. if (TEST_UL_IMM(src2, src2w)) {
  1358. compiler->imm = src2w & 0xffff;
  1359. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
  1360. }
  1361. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM4, dst, dstw, src1, src1w, src2, src2w);
  1362. }
  1363. if ((src2 & SLJIT_IMM) && src2w >= 0 && src2w <= 0x7fff) {
  1364. compiler->imm = src2w;
  1365. return emit_op(compiler, SLJIT_SUB, flags | ALT_FORM2 | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
  1366. }
  1367. return emit_op(compiler, SLJIT_SUB, flags | ((op & SLJIT_SET_U) ? ALT_FORM4 : 0) | ((op & (SLJIT_SET_E | SLJIT_SET_S)) ? ALT_FORM5 : 0), dst, dstw, src1, src1w, src2, src2w);
  1368. }
  1369. if (!(op & (SLJIT_SET_E | SLJIT_SET_U | SLJIT_SET_S | SLJIT_SET_O))) {
  1370. if (TEST_SL_IMM(src2, -src2w)) {
  1371. compiler->imm = (-src2w) & 0xffff;
  1372. return emit_op(compiler, SLJIT_ADD, flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
  1373. }
  1374. }
  1375. /* We know ALT_SIGN_EXT is set if it is an SLJIT_INT_OP on 64 bit systems. */
  1376. return emit_op(compiler, SLJIT_SUB, flags | (!(op & SLJIT_SET_U) ? 0 : ALT_FORM6), dst, dstw, src1, src1w, src2, src2w);
  1377. case SLJIT_SUBC:
  1378. return emit_op(compiler, SLJIT_SUBC, flags | (!(op & SLJIT_KEEP_FLAGS) ? 0 : ALT_FORM1), dst, dstw, src1, src1w, src2, src2w);
  1379. case SLJIT_MUL:
  1380. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1381. if (op & SLJIT_INT_OP)
  1382. flags |= ALT_FORM2;
  1383. #endif
  1384. if (!GET_FLAGS(op)) {
  1385. if (TEST_SL_IMM(src2, src2w)) {
  1386. compiler->imm = src2w & 0xffff;
  1387. return emit_op(compiler, SLJIT_MUL, flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
  1388. }
  1389. if (TEST_SL_IMM(src1, src1w)) {
  1390. compiler->imm = src1w & 0xffff;
  1391. return emit_op(compiler, SLJIT_MUL, flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
  1392. }
  1393. }
  1394. return emit_op(compiler, SLJIT_MUL, flags, dst, dstw, src1, src1w, src2, src2w);
  1395. case SLJIT_AND:
  1396. case SLJIT_OR:
  1397. case SLJIT_XOR:
  1398. /* Commutative unsigned operations. */
  1399. if (!GET_FLAGS(op) || GET_OPCODE(op) == SLJIT_AND) {
  1400. if (TEST_UL_IMM(src2, src2w)) {
  1401. compiler->imm = src2w;
  1402. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
  1403. }
  1404. if (TEST_UL_IMM(src1, src1w)) {
  1405. compiler->imm = src1w;
  1406. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src2, src2w, TMP_REG2, 0);
  1407. }
  1408. if (TEST_UH_IMM(src2, src2w)) {
  1409. compiler->imm = (src2w >> 16) & 0xffff;
  1410. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM2, dst, dstw, src1, src1w, TMP_REG2, 0);
  1411. }
  1412. if (TEST_UH_IMM(src1, src1w)) {
  1413. compiler->imm = (src1w >> 16) & 0xffff;
  1414. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM2, dst, dstw, src2, src2w, TMP_REG2, 0);
  1415. }
  1416. }
  1417. if (!GET_FLAGS(op) && GET_OPCODE(op) != SLJIT_AND) {
  1418. if (TEST_UI_IMM(src2, src2w)) {
  1419. compiler->imm = src2w;
  1420. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM3, dst, dstw, src1, src1w, TMP_REG2, 0);
  1421. }
  1422. if (TEST_UI_IMM(src1, src1w)) {
  1423. compiler->imm = src1w;
  1424. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM3, dst, dstw, src2, src2w, TMP_REG2, 0);
  1425. }
  1426. }
  1427. return emit_op(compiler, GET_OPCODE(op), flags, dst, dstw, src1, src1w, src2, src2w);
  1428. case SLJIT_ASHR:
  1429. if (op & SLJIT_KEEP_FLAGS)
  1430. flags |= ALT_FORM3;
  1431. /* Fall through. */
  1432. case SLJIT_SHL:
  1433. case SLJIT_LSHR:
  1434. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1435. if (op & SLJIT_INT_OP)
  1436. flags |= ALT_FORM2;
  1437. #endif
  1438. if (src2 & SLJIT_IMM) {
  1439. compiler->imm = src2w;
  1440. return emit_op(compiler, GET_OPCODE(op), flags | ALT_FORM1, dst, dstw, src1, src1w, TMP_REG2, 0);
  1441. }
  1442. return emit_op(compiler, GET_OPCODE(op), flags, dst, dstw, src1, src1w, src2, src2w);
  1443. }
  1444. return SLJIT_SUCCESS;
  1445. }
  1446. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
  1447. {
  1448. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1449. return reg_map[reg];
  1450. }
  1451. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
  1452. {
  1453. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1454. return reg;
  1455. }
  1456. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
  1457. void *instruction, sljit_si size)
  1458. {
  1459. CHECK_ERROR();
  1460. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1461. return push_inst(compiler, *(sljit_ins*)instruction);
  1462. }
  1463. /* --------------------------------------------------------------------- */
  1464. /* Floating point operators */
  1465. /* --------------------------------------------------------------------- */
  1466. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
  1467. {
  1468. #ifdef SLJIT_IS_FPU_AVAILABLE
  1469. return SLJIT_IS_FPU_AVAILABLE;
  1470. #else
  1471. /* Available by default. */
  1472. return 1;
  1473. #endif
  1474. }
  1475. #define FLOAT_DATA(op) (DOUBLE_DATA | ((op & SLJIT_SINGLE_OP) >> 6))
  1476. #define SELECT_FOP(op, single, double) ((op & SLJIT_SINGLE_OP) ? single : double)
  1477. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1478. #define FLOAT_TMP_MEM_OFFSET (6 * sizeof(sljit_sw))
  1479. #else
  1480. #define FLOAT_TMP_MEM_OFFSET (2 * sizeof(sljit_sw))
  1481. #if (defined SLJIT_LITTLE_ENDIAN && SLJIT_LITTLE_ENDIAN)
  1482. #define FLOAT_TMP_MEM_OFFSET_LOW (2 * sizeof(sljit_sw))
  1483. #define FLOAT_TMP_MEM_OFFSET_HI (3 * sizeof(sljit_sw))
  1484. #else
  1485. #define FLOAT_TMP_MEM_OFFSET_LOW (3 * sizeof(sljit_sw))
  1486. #define FLOAT_TMP_MEM_OFFSET_HI (2 * sizeof(sljit_sw))
  1487. #endif
  1488. #endif /* SLJIT_CONFIG_PPC_64 */
  1489. static SLJIT_INLINE sljit_si sljit_emit_fop1_convw_fromd(struct sljit_compiler *compiler, sljit_si op,
  1490. sljit_si dst, sljit_sw dstw,
  1491. sljit_si src, sljit_sw srcw)
  1492. {
  1493. if (src & SLJIT_MEM) {
  1494. /* We can ignore the temporary data store on the stack from caching point of view. */
  1495. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
  1496. src = TMP_FREG1;
  1497. }
  1498. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1499. op = GET_OPCODE(op);
  1500. FAIL_IF(push_inst(compiler, (op == SLJIT_CONVI_FROMD ? FCTIWZ : FCTIDZ) | FD(TMP_FREG1) | FB(src)));
  1501. if (dst == SLJIT_UNUSED)
  1502. return SLJIT_SUCCESS;
  1503. if (op == SLJIT_CONVW_FROMD) {
  1504. if (FAST_IS_REG(dst)) {
  1505. FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA, TMP_FREG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, 0, 0));
  1506. return emit_op_mem2(compiler, WORD_DATA | LOAD_DATA, dst, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, 0, 0);
  1507. }
  1508. return emit_op_mem2(compiler, DOUBLE_DATA, TMP_FREG1, dst, dstw, 0, 0);
  1509. }
  1510. #else
  1511. FAIL_IF(push_inst(compiler, FCTIWZ | FD(TMP_FREG1) | FB(src)));
  1512. if (dst == SLJIT_UNUSED)
  1513. return SLJIT_SUCCESS;
  1514. #endif
  1515. if (FAST_IS_REG(dst)) {
  1516. FAIL_IF(load_immediate(compiler, TMP_REG1, FLOAT_TMP_MEM_OFFSET));
  1517. FAIL_IF(push_inst(compiler, STFIWX | FS(TMP_FREG1) | A(SLJIT_SP) | B(TMP_REG1)));
  1518. return emit_op_mem2(compiler, INT_DATA | LOAD_DATA, dst, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, 0, 0);
  1519. }
  1520. SLJIT_ASSERT(dst & SLJIT_MEM);
  1521. if (dst & OFFS_REG_MASK) {
  1522. dstw &= 0x3;
  1523. if (dstw) {
  1524. #if (defined SLJIT_CONFIG_PPC_32 && SLJIT_CONFIG_PPC_32)
  1525. FAIL_IF(push_inst(compiler, RLWINM | S(OFFS_REG(dst)) | A(TMP_REG1) | (dstw << 11) | ((31 - dstw) << 1)));
  1526. #else
  1527. FAIL_IF(push_inst(compiler, RLDI(TMP_REG1, OFFS_REG(dst), dstw, 63 - dstw, 1)));
  1528. #endif
  1529. dstw = TMP_REG1;
  1530. }
  1531. else
  1532. dstw = OFFS_REG(dst);
  1533. }
  1534. else {
  1535. if ((dst & REG_MASK) && !dstw) {
  1536. dstw = dst & REG_MASK;
  1537. dst = 0;
  1538. }
  1539. else {
  1540. /* This works regardless we have SLJIT_MEM1 or SLJIT_MEM0. */
  1541. FAIL_IF(load_immediate(compiler, TMP_REG1, dstw));
  1542. dstw = TMP_REG1;
  1543. }
  1544. }
  1545. return push_inst(compiler, STFIWX | FS(TMP_FREG1) | A(dst & REG_MASK) | B(dstw));
  1546. }
  1547. static SLJIT_INLINE sljit_si sljit_emit_fop1_convd_fromw(struct sljit_compiler *compiler, sljit_si op,
  1548. sljit_si dst, sljit_sw dstw,
  1549. sljit_si src, sljit_sw srcw)
  1550. {
  1551. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1552. sljit_si dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1553. if (src & SLJIT_IMM) {
  1554. if (GET_OPCODE(op) == SLJIT_CONVD_FROMI)
  1555. srcw = (sljit_si)srcw;
  1556. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  1557. src = TMP_REG1;
  1558. }
  1559. else if (GET_OPCODE(op) == SLJIT_CONVD_FROMI) {
  1560. if (FAST_IS_REG(src))
  1561. FAIL_IF(push_inst(compiler, EXTSW | S(src) | A(TMP_REG1)));
  1562. else
  1563. FAIL_IF(emit_op_mem2(compiler, INT_DATA | SIGNED_DATA | LOAD_DATA, TMP_REG1, src, srcw, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
  1564. src = TMP_REG1;
  1565. }
  1566. if (FAST_IS_REG(src)) {
  1567. FAIL_IF(emit_op_mem2(compiler, WORD_DATA, src, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
  1568. FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, dst, dstw));
  1569. }
  1570. else
  1571. FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, src, srcw, dst, dstw));
  1572. FAIL_IF(push_inst(compiler, FCFID | FD(dst_r) | FB(TMP_FREG1)));
  1573. if (dst & SLJIT_MEM)
  1574. return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
  1575. if (op & SLJIT_SINGLE_OP)
  1576. return push_inst(compiler, FRSP | FD(dst_r) | FB(dst_r));
  1577. return SLJIT_SUCCESS;
  1578. #else
  1579. sljit_si dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1580. sljit_si invert_sign = 1;
  1581. if (src & SLJIT_IMM) {
  1582. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw ^ 0x80000000));
  1583. src = TMP_REG1;
  1584. invert_sign = 0;
  1585. }
  1586. else if (!FAST_IS_REG(src)) {
  1587. FAIL_IF(emit_op_mem2(compiler, WORD_DATA | SIGNED_DATA | LOAD_DATA, TMP_REG1, src, srcw, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_LOW));
  1588. src = TMP_REG1;
  1589. }
  1590. /* First, a special double floating point value is constructed: (2^53 + (input xor (2^31)))
  1591. The double precision format has exactly 53 bit precision, so the lower 32 bit represents
  1592. the lower 32 bit of such value. The result of xor 2^31 is the same as adding 0x80000000
  1593. to the input, which shifts it into the 0 - 0xffffffff range. To get the converted floating
  1594. point value, we need to substract 2^53 + 2^31 from the constructed value. */
  1595. FAIL_IF(push_inst(compiler, ADDIS | D(TMP_REG2) | A(0) | 0x4330));
  1596. if (invert_sign)
  1597. FAIL_IF(push_inst(compiler, XORIS | S(src) | A(TMP_REG1) | 0x8000));
  1598. FAIL_IF(emit_op_mem2(compiler, WORD_DATA, TMP_REG2, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_HI, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
  1599. FAIL_IF(emit_op_mem2(compiler, WORD_DATA, TMP_REG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_LOW, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_HI));
  1600. FAIL_IF(push_inst(compiler, ADDIS | D(TMP_REG1) | A(0) | 0x8000));
  1601. FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_LOW));
  1602. FAIL_IF(emit_op_mem2(compiler, WORD_DATA, TMP_REG1, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_LOW, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET));
  1603. FAIL_IF(emit_op_mem2(compiler, DOUBLE_DATA | LOAD_DATA, TMP_FREG2, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET, SLJIT_MEM1(SLJIT_SP), FLOAT_TMP_MEM_OFFSET_LOW));
  1604. FAIL_IF(push_inst(compiler, FSUB | FD(dst_r) | FA(TMP_FREG1) | FB(TMP_FREG2)));
  1605. if (dst & SLJIT_MEM)
  1606. return emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG1, dst, dstw, 0, 0);
  1607. if (op & SLJIT_SINGLE_OP)
  1608. return push_inst(compiler, FRSP | FD(dst_r) | FB(dst_r));
  1609. return SLJIT_SUCCESS;
  1610. #endif
  1611. }
  1612. static SLJIT_INLINE sljit_si sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_si op,
  1613. sljit_si src1, sljit_sw src1w,
  1614. sljit_si src2, sljit_sw src2w)
  1615. {
  1616. if (src1 & SLJIT_MEM) {
  1617. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
  1618. src1 = TMP_FREG1;
  1619. }
  1620. if (src2 & SLJIT_MEM) {
  1621. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, 0, 0));
  1622. src2 = TMP_FREG2;
  1623. }
  1624. return push_inst(compiler, FCMPU | CRD(4) | FA(src1) | FB(src2));
  1625. }
  1626. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
  1627. sljit_si dst, sljit_sw dstw,
  1628. sljit_si src, sljit_sw srcw)
  1629. {
  1630. sljit_si dst_r;
  1631. CHECK_ERROR();
  1632. compiler->cache_arg = 0;
  1633. compiler->cache_argw = 0;
  1634. SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100) && !(DOUBLE_DATA & 0x4), float_transfer_bit_error);
  1635. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1636. if (GET_OPCODE(op) == SLJIT_CONVD_FROMS)
  1637. op ^= SLJIT_SINGLE_OP;
  1638. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1639. if (src & SLJIT_MEM) {
  1640. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op) | LOAD_DATA, dst_r, src, srcw, dst, dstw));
  1641. src = dst_r;
  1642. }
  1643. switch (GET_OPCODE(op)) {
  1644. case SLJIT_CONVD_FROMS:
  1645. op ^= SLJIT_SINGLE_OP;
  1646. if (op & SLJIT_SINGLE_OP) {
  1647. FAIL_IF(push_inst(compiler, FRSP | FD(dst_r) | FB(src)));
  1648. break;
  1649. }
  1650. /* Fall through. */
  1651. case SLJIT_DMOV:
  1652. if (src != dst_r) {
  1653. if (dst_r != TMP_FREG1)
  1654. FAIL_IF(push_inst(compiler, FMR | FD(dst_r) | FB(src)));
  1655. else
  1656. dst_r = src;
  1657. }
  1658. break;
  1659. case SLJIT_DNEG:
  1660. FAIL_IF(push_inst(compiler, FNEG | FD(dst_r) | FB(src)));
  1661. break;
  1662. case SLJIT_DABS:
  1663. FAIL_IF(push_inst(compiler, FABS | FD(dst_r) | FB(src)));
  1664. break;
  1665. }
  1666. if (dst & SLJIT_MEM)
  1667. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), dst_r, dst, dstw, 0, 0));
  1668. return SLJIT_SUCCESS;
  1669. }
  1670. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
  1671. sljit_si dst, sljit_sw dstw,
  1672. sljit_si src1, sljit_sw src1w,
  1673. sljit_si src2, sljit_sw src2w)
  1674. {
  1675. sljit_si dst_r, flags = 0;
  1676. CHECK_ERROR();
  1677. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1678. ADJUST_LOCAL_OFFSET(dst, dstw);
  1679. ADJUST_LOCAL_OFFSET(src1, src1w);
  1680. ADJUST_LOCAL_OFFSET(src2, src2w);
  1681. compiler->cache_arg = 0;
  1682. compiler->cache_argw = 0;
  1683. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG2;
  1684. if (src1 & SLJIT_MEM) {
  1685. if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w)) {
  1686. FAIL_IF(compiler->error);
  1687. src1 = TMP_FREG1;
  1688. } else
  1689. flags |= ALT_FORM1;
  1690. }
  1691. if (src2 & SLJIT_MEM) {
  1692. if (getput_arg_fast(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w)) {
  1693. FAIL_IF(compiler->error);
  1694. src2 = TMP_FREG2;
  1695. } else
  1696. flags |= ALT_FORM2;
  1697. }
  1698. if ((flags & (ALT_FORM1 | ALT_FORM2)) == (ALT_FORM1 | ALT_FORM2)) {
  1699. if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
  1700. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, src1, src1w));
  1701. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
  1702. }
  1703. else {
  1704. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, src2, src2w));
  1705. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
  1706. }
  1707. }
  1708. else if (flags & ALT_FORM1)
  1709. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG1, src1, src1w, dst, dstw));
  1710. else if (flags & ALT_FORM2)
  1711. FAIL_IF(getput_arg(compiler, FLOAT_DATA(op) | LOAD_DATA, TMP_FREG2, src2, src2w, dst, dstw));
  1712. if (flags & ALT_FORM1)
  1713. src1 = TMP_FREG1;
  1714. if (flags & ALT_FORM2)
  1715. src2 = TMP_FREG2;
  1716. switch (GET_OPCODE(op)) {
  1717. case SLJIT_DADD:
  1718. FAIL_IF(push_inst(compiler, SELECT_FOP(op, FADDS, FADD) | FD(dst_r) | FA(src1) | FB(src2)));
  1719. break;
  1720. case SLJIT_DSUB:
  1721. FAIL_IF(push_inst(compiler, SELECT_FOP(op, FSUBS, FSUB) | FD(dst_r) | FA(src1) | FB(src2)));
  1722. break;
  1723. case SLJIT_DMUL:
  1724. FAIL_IF(push_inst(compiler, SELECT_FOP(op, FMULS, FMUL) | FD(dst_r) | FA(src1) | FC(src2) /* FMUL use FC as src2 */));
  1725. break;
  1726. case SLJIT_DDIV:
  1727. FAIL_IF(push_inst(compiler, SELECT_FOP(op, FDIVS, FDIV) | FD(dst_r) | FA(src1) | FB(src2)));
  1728. break;
  1729. }
  1730. if (dst_r == TMP_FREG2)
  1731. FAIL_IF(emit_op_mem2(compiler, FLOAT_DATA(op), TMP_FREG2, dst, dstw, 0, 0));
  1732. return SLJIT_SUCCESS;
  1733. }
  1734. #undef FLOAT_DATA
  1735. #undef SELECT_FOP
  1736. /* --------------------------------------------------------------------- */
  1737. /* Other instructions */
  1738. /* --------------------------------------------------------------------- */
  1739. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
  1740. {
  1741. CHECK_ERROR();
  1742. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1743. ADJUST_LOCAL_OFFSET(dst, dstw);
  1744. /* For UNUSED dst. Uncommon, but possible. */
  1745. if (dst == SLJIT_UNUSED)
  1746. return SLJIT_SUCCESS;
  1747. if (FAST_IS_REG(dst))
  1748. return push_inst(compiler, MFLR | D(dst));
  1749. /* Memory. */
  1750. FAIL_IF(push_inst(compiler, MFLR | D(TMP_REG2)));
  1751. return emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
  1752. }
  1753. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
  1754. {
  1755. CHECK_ERROR();
  1756. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1757. ADJUST_LOCAL_OFFSET(src, srcw);
  1758. if (FAST_IS_REG(src))
  1759. FAIL_IF(push_inst(compiler, MTLR | S(src)));
  1760. else {
  1761. if (src & SLJIT_MEM)
  1762. FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_REG2, 0, TMP_REG1, 0, src, srcw));
  1763. else if (src & SLJIT_IMM)
  1764. FAIL_IF(load_immediate(compiler, TMP_REG2, srcw));
  1765. FAIL_IF(push_inst(compiler, MTLR | S(TMP_REG2)));
  1766. }
  1767. return push_inst(compiler, BLR);
  1768. }
  1769. /* --------------------------------------------------------------------- */
  1770. /* Conditional instructions */
  1771. /* --------------------------------------------------------------------- */
  1772. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1773. {
  1774. struct sljit_label *label;
  1775. CHECK_ERROR_PTR();
  1776. CHECK_PTR(check_sljit_emit_label(compiler));
  1777. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1778. return compiler->last_label;
  1779. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1780. PTR_FAIL_IF(!label);
  1781. set_label(label, compiler);
  1782. return label;
  1783. }
  1784. static sljit_ins get_bo_bi_flags(sljit_si type)
  1785. {
  1786. switch (type) {
  1787. case SLJIT_EQUAL:
  1788. return (12 << 21) | (2 << 16);
  1789. case SLJIT_NOT_EQUAL:
  1790. return (4 << 21) | (2 << 16);
  1791. case SLJIT_LESS:
  1792. case SLJIT_D_LESS:
  1793. return (12 << 21) | ((4 + 0) << 16);
  1794. case SLJIT_GREATER_EQUAL:
  1795. case SLJIT_D_GREATER_EQUAL:
  1796. return (4 << 21) | ((4 + 0) << 16);
  1797. case SLJIT_GREATER:
  1798. case SLJIT_D_GREATER:
  1799. return (12 << 21) | ((4 + 1) << 16);
  1800. case SLJIT_LESS_EQUAL:
  1801. case SLJIT_D_LESS_EQUAL:
  1802. return (4 << 21) | ((4 + 1) << 16);
  1803. case SLJIT_SIG_LESS:
  1804. return (12 << 21) | (0 << 16);
  1805. case SLJIT_SIG_GREATER_EQUAL:
  1806. return (4 << 21) | (0 << 16);
  1807. case SLJIT_SIG_GREATER:
  1808. return (12 << 21) | (1 << 16);
  1809. case SLJIT_SIG_LESS_EQUAL:
  1810. return (4 << 21) | (1 << 16);
  1811. case SLJIT_OVERFLOW:
  1812. case SLJIT_MUL_OVERFLOW:
  1813. return (12 << 21) | (3 << 16);
  1814. case SLJIT_NOT_OVERFLOW:
  1815. case SLJIT_MUL_NOT_OVERFLOW:
  1816. return (4 << 21) | (3 << 16);
  1817. case SLJIT_D_EQUAL:
  1818. return (12 << 21) | ((4 + 2) << 16);
  1819. case SLJIT_D_NOT_EQUAL:
  1820. return (4 << 21) | ((4 + 2) << 16);
  1821. case SLJIT_D_UNORDERED:
  1822. return (12 << 21) | ((4 + 3) << 16);
  1823. case SLJIT_D_ORDERED:
  1824. return (4 << 21) | ((4 + 3) << 16);
  1825. default:
  1826. SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL3);
  1827. return (20 << 21);
  1828. }
  1829. }
  1830. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
  1831. {
  1832. struct sljit_jump *jump;
  1833. sljit_ins bo_bi_flags;
  1834. CHECK_ERROR_PTR();
  1835. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1836. bo_bi_flags = get_bo_bi_flags(type & 0xff);
  1837. if (!bo_bi_flags)
  1838. return NULL;
  1839. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1840. PTR_FAIL_IF(!jump);
  1841. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1842. type &= 0xff;
  1843. /* In PPC, we don't need to touch the arguments. */
  1844. if (type < SLJIT_JUMP)
  1845. jump->flags |= IS_COND;
  1846. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL)
  1847. if (type >= SLJIT_CALL0)
  1848. jump->flags |= IS_CALL;
  1849. #endif
  1850. PTR_FAIL_IF(emit_const(compiler, TMP_CALL_REG, 0));
  1851. PTR_FAIL_IF(push_inst(compiler, MTCTR | S(TMP_CALL_REG)));
  1852. jump->addr = compiler->size;
  1853. PTR_FAIL_IF(push_inst(compiler, BCCTR | bo_bi_flags | (type >= SLJIT_FAST_CALL ? 1 : 0)));
  1854. return jump;
  1855. }
  1856. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
  1857. {
  1858. struct sljit_jump *jump = NULL;
  1859. sljit_si src_r;
  1860. CHECK_ERROR();
  1861. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  1862. ADJUST_LOCAL_OFFSET(src, srcw);
  1863. if (FAST_IS_REG(src)) {
  1864. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL)
  1865. if (type >= SLJIT_CALL0) {
  1866. FAIL_IF(push_inst(compiler, OR | S(src) | A(TMP_CALL_REG) | B(src)));
  1867. src_r = TMP_CALL_REG;
  1868. }
  1869. else
  1870. src_r = src;
  1871. #else
  1872. src_r = src;
  1873. #endif
  1874. } else if (src & SLJIT_IMM) {
  1875. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1876. FAIL_IF(!jump);
  1877. set_jump(jump, compiler, JUMP_ADDR);
  1878. jump->u.target = srcw;
  1879. #if (defined SLJIT_PASS_ENTRY_ADDR_TO_CALL && SLJIT_PASS_ENTRY_ADDR_TO_CALL)
  1880. if (type >= SLJIT_CALL0)
  1881. jump->flags |= IS_CALL;
  1882. #endif
  1883. FAIL_IF(emit_const(compiler, TMP_CALL_REG, 0));
  1884. src_r = TMP_CALL_REG;
  1885. }
  1886. else {
  1887. FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, TMP_CALL_REG, 0, TMP_REG1, 0, src, srcw));
  1888. src_r = TMP_CALL_REG;
  1889. }
  1890. FAIL_IF(push_inst(compiler, MTCTR | S(src_r)));
  1891. if (jump)
  1892. jump->addr = compiler->size;
  1893. return push_inst(compiler, BCCTR | (20 << 21) | (type >= SLJIT_FAST_CALL ? 1 : 0));
  1894. }
  1895. /* Get a bit from CR, all other bits are zeroed. */
  1896. #define GET_CR_BIT(bit, dst) \
  1897. FAIL_IF(push_inst(compiler, MFCR | D(dst))); \
  1898. FAIL_IF(push_inst(compiler, RLWINM | S(dst) | A(dst) | ((1 + (bit)) << 11) | (31 << 6) | (31 << 1)));
  1899. #define INVERT_BIT(dst) \
  1900. FAIL_IF(push_inst(compiler, XORI | S(dst) | A(dst) | 0x1));
  1901. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
  1902. sljit_si dst, sljit_sw dstw,
  1903. sljit_si src, sljit_sw srcw,
  1904. sljit_si type)
  1905. {
  1906. sljit_si reg, input_flags;
  1907. sljit_si flags = GET_ALL_FLAGS(op);
  1908. sljit_sw original_dstw = dstw;
  1909. CHECK_ERROR();
  1910. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type));
  1911. ADJUST_LOCAL_OFFSET(dst, dstw);
  1912. if (dst == SLJIT_UNUSED)
  1913. return SLJIT_SUCCESS;
  1914. op = GET_OPCODE(op);
  1915. reg = (op < SLJIT_ADD && FAST_IS_REG(dst)) ? dst : TMP_REG2;
  1916. compiler->cache_arg = 0;
  1917. compiler->cache_argw = 0;
  1918. if (op >= SLJIT_ADD && (src & SLJIT_MEM)) {
  1919. ADJUST_LOCAL_OFFSET(src, srcw);
  1920. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1921. input_flags = (flags & SLJIT_INT_OP) ? INT_DATA : WORD_DATA;
  1922. #else
  1923. input_flags = WORD_DATA;
  1924. #endif
  1925. FAIL_IF(emit_op_mem2(compiler, input_flags | LOAD_DATA, TMP_REG1, src, srcw, dst, dstw));
  1926. src = TMP_REG1;
  1927. srcw = 0;
  1928. }
  1929. switch (type & 0xff) {
  1930. case SLJIT_EQUAL:
  1931. GET_CR_BIT(2, reg);
  1932. break;
  1933. case SLJIT_NOT_EQUAL:
  1934. GET_CR_BIT(2, reg);
  1935. INVERT_BIT(reg);
  1936. break;
  1937. case SLJIT_LESS:
  1938. case SLJIT_D_LESS:
  1939. GET_CR_BIT(4 + 0, reg);
  1940. break;
  1941. case SLJIT_GREATER_EQUAL:
  1942. case SLJIT_D_GREATER_EQUAL:
  1943. GET_CR_BIT(4 + 0, reg);
  1944. INVERT_BIT(reg);
  1945. break;
  1946. case SLJIT_GREATER:
  1947. case SLJIT_D_GREATER:
  1948. GET_CR_BIT(4 + 1, reg);
  1949. break;
  1950. case SLJIT_LESS_EQUAL:
  1951. case SLJIT_D_LESS_EQUAL:
  1952. GET_CR_BIT(4 + 1, reg);
  1953. INVERT_BIT(reg);
  1954. break;
  1955. case SLJIT_SIG_LESS:
  1956. GET_CR_BIT(0, reg);
  1957. break;
  1958. case SLJIT_SIG_GREATER_EQUAL:
  1959. GET_CR_BIT(0, reg);
  1960. INVERT_BIT(reg);
  1961. break;
  1962. case SLJIT_SIG_GREATER:
  1963. GET_CR_BIT(1, reg);
  1964. break;
  1965. case SLJIT_SIG_LESS_EQUAL:
  1966. GET_CR_BIT(1, reg);
  1967. INVERT_BIT(reg);
  1968. break;
  1969. case SLJIT_OVERFLOW:
  1970. case SLJIT_MUL_OVERFLOW:
  1971. GET_CR_BIT(3, reg);
  1972. break;
  1973. case SLJIT_NOT_OVERFLOW:
  1974. case SLJIT_MUL_NOT_OVERFLOW:
  1975. GET_CR_BIT(3, reg);
  1976. INVERT_BIT(reg);
  1977. break;
  1978. case SLJIT_D_EQUAL:
  1979. GET_CR_BIT(4 + 2, reg);
  1980. break;
  1981. case SLJIT_D_NOT_EQUAL:
  1982. GET_CR_BIT(4 + 2, reg);
  1983. INVERT_BIT(reg);
  1984. break;
  1985. case SLJIT_D_UNORDERED:
  1986. GET_CR_BIT(4 + 3, reg);
  1987. break;
  1988. case SLJIT_D_ORDERED:
  1989. GET_CR_BIT(4 + 3, reg);
  1990. INVERT_BIT(reg);
  1991. break;
  1992. default:
  1993. SLJIT_ASSERT_STOP();
  1994. break;
  1995. }
  1996. if (op < SLJIT_ADD) {
  1997. #if (defined SLJIT_CONFIG_PPC_64 && SLJIT_CONFIG_PPC_64)
  1998. if (op == SLJIT_MOV)
  1999. input_flags = WORD_DATA;
  2000. else {
  2001. op = SLJIT_MOV_UI;
  2002. input_flags = INT_DATA;
  2003. }
  2004. #else
  2005. op = SLJIT_MOV;
  2006. input_flags = WORD_DATA;
  2007. #endif
  2008. if (reg != TMP_REG2)
  2009. return SLJIT_SUCCESS;
  2010. return emit_op(compiler, op, input_flags, dst, dstw, TMP_REG1, 0, TMP_REG2, 0);
  2011. }
  2012. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2013. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2014. compiler->skip_checks = 1;
  2015. #endif
  2016. return sljit_emit_op2(compiler, op | flags, dst, original_dstw, src, srcw, TMP_REG2, 0);
  2017. }
  2018. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
  2019. {
  2020. struct sljit_const *const_;
  2021. sljit_si reg;
  2022. CHECK_ERROR_PTR();
  2023. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  2024. ADJUST_LOCAL_OFFSET(dst, dstw);
  2025. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  2026. PTR_FAIL_IF(!const_);
  2027. set_const(const_, compiler);
  2028. reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  2029. PTR_FAIL_IF(emit_const(compiler, reg, init_value));
  2030. if (dst & SLJIT_MEM)
  2031. PTR_FAIL_IF(emit_op(compiler, SLJIT_MOV, WORD_DATA, dst, dstw, TMP_REG1, 0, TMP_REG2, 0));
  2032. return const_;
  2033. }