sljitNativeARM_T2_32.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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 "ARM-Thumb2" SLJIT_CPUINFO;
  29. }
  30. /* Length of an instruction word. */
  31. typedef sljit_ui sljit_ins;
  32. /* Last register + 1. */
  33. #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
  34. #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
  35. #define TMP_REG3 (SLJIT_NUMBER_OF_REGISTERS + 4)
  36. #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 5)
  37. #define TMP_FREG1 (0)
  38. #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
  39. /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
  40. static SLJIT_CONST sljit_ub reg_map[SLJIT_NUMBER_OF_REGISTERS + 6] = {
  41. 0, 0, 1, 2, 12, 11, 10, 9, 8, 7, 6, 5, 13, 3, 4, 14, 15
  42. };
  43. #define COPY_BITS(src, from, to, bits) \
  44. ((from >= to ? (src >> (from - to)) : (src << (to - from))) & (((1 << bits) - 1) << to))
  45. /* Thumb16 encodings. */
  46. #define RD3(rd) (reg_map[rd])
  47. #define RN3(rn) (reg_map[rn] << 3)
  48. #define RM3(rm) (reg_map[rm] << 6)
  49. #define RDN3(rdn) (reg_map[rdn] << 8)
  50. #define IMM3(imm) (imm << 6)
  51. #define IMM8(imm) (imm)
  52. /* Thumb16 helpers. */
  53. #define SET_REGS44(rd, rn) \
  54. ((reg_map[rn] << 3) | (reg_map[rd] & 0x7) | ((reg_map[rd] & 0x8) << 4))
  55. #define IS_2_LO_REGS(reg1, reg2) \
  56. (reg_map[reg1] <= 7 && reg_map[reg2] <= 7)
  57. #define IS_3_LO_REGS(reg1, reg2, reg3) \
  58. (reg_map[reg1] <= 7 && reg_map[reg2] <= 7 && reg_map[reg3] <= 7)
  59. /* Thumb32 encodings. */
  60. #define RD4(rd) (reg_map[rd] << 8)
  61. #define RN4(rn) (reg_map[rn] << 16)
  62. #define RM4(rm) (reg_map[rm])
  63. #define RT4(rt) (reg_map[rt] << 12)
  64. #define DD4(dd) ((dd) << 12)
  65. #define DN4(dn) ((dn) << 16)
  66. #define DM4(dm) (dm)
  67. #define IMM5(imm) \
  68. (COPY_BITS(imm, 2, 12, 3) | ((imm & 0x3) << 6))
  69. #define IMM12(imm) \
  70. (COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff))
  71. /* --------------------------------------------------------------------- */
  72. /* Instrucion forms */
  73. /* --------------------------------------------------------------------- */
  74. /* dot '.' changed to _
  75. I immediate form (possibly followed by number of immediate bits). */
  76. #define ADCI 0xf1400000
  77. #define ADCS 0x4140
  78. #define ADC_W 0xeb400000
  79. #define ADD 0x4400
  80. #define ADDS 0x1800
  81. #define ADDSI3 0x1c00
  82. #define ADDSI8 0x3000
  83. #define ADD_W 0xeb000000
  84. #define ADDWI 0xf2000000
  85. #define ADD_SP 0xb000
  86. #define ADD_W 0xeb000000
  87. #define ADD_WI 0xf1000000
  88. #define ANDI 0xf0000000
  89. #define ANDS 0x4000
  90. #define AND_W 0xea000000
  91. #define ASRS 0x4100
  92. #define ASRSI 0x1000
  93. #define ASR_W 0xfa40f000
  94. #define ASR_WI 0xea4f0020
  95. #define BICI 0xf0200000
  96. #define BKPT 0xbe00
  97. #define BLX 0x4780
  98. #define BX 0x4700
  99. #define CLZ 0xfab0f080
  100. #define CMPI 0x2800
  101. #define CMP_W 0xebb00f00
  102. #define EORI 0xf0800000
  103. #define EORS 0x4040
  104. #define EOR_W 0xea800000
  105. #define IT 0xbf00
  106. #define LSLS 0x4080
  107. #define LSLSI 0x0000
  108. #define LSL_W 0xfa00f000
  109. #define LSL_WI 0xea4f0000
  110. #define LSRS 0x40c0
  111. #define LSRSI 0x0800
  112. #define LSR_W 0xfa20f000
  113. #define LSR_WI 0xea4f0010
  114. #define MOV 0x4600
  115. #define MOVS 0x0000
  116. #define MOVSI 0x2000
  117. #define MOVT 0xf2c00000
  118. #define MOVW 0xf2400000
  119. #define MOV_W 0xea4f0000
  120. #define MOV_WI 0xf04f0000
  121. #define MUL 0xfb00f000
  122. #define MVNS 0x43c0
  123. #define MVN_W 0xea6f0000
  124. #define MVN_WI 0xf06f0000
  125. #define NOP 0xbf00
  126. #define ORNI 0xf0600000
  127. #define ORRI 0xf0400000
  128. #define ORRS 0x4300
  129. #define ORR_W 0xea400000
  130. #define POP 0xbc00
  131. #define POP_W 0xe8bd0000
  132. #define PUSH 0xb400
  133. #define PUSH_W 0xe92d0000
  134. #define RSB_WI 0xf1c00000
  135. #define RSBSI 0x4240
  136. #define SBCI 0xf1600000
  137. #define SBCS 0x4180
  138. #define SBC_W 0xeb600000
  139. #define SMULL 0xfb800000
  140. #define STR_SP 0x9000
  141. #define SUBS 0x1a00
  142. #define SUBSI3 0x1e00
  143. #define SUBSI8 0x3800
  144. #define SUB_W 0xeba00000
  145. #define SUBWI 0xf2a00000
  146. #define SUB_SP 0xb080
  147. #define SUB_WI 0xf1a00000
  148. #define SXTB 0xb240
  149. #define SXTB_W 0xfa4ff080
  150. #define SXTH 0xb200
  151. #define SXTH_W 0xfa0ff080
  152. #define TST 0x4200
  153. #define UMULL 0xfba00000
  154. #define UXTB 0xb2c0
  155. #define UXTB_W 0xfa5ff080
  156. #define UXTH 0xb280
  157. #define UXTH_W 0xfa1ff080
  158. #define VABS_F32 0xeeb00ac0
  159. #define VADD_F32 0xee300a00
  160. #define VCMP_F32 0xeeb40a40
  161. #define VCVT_F32_S32 0xeeb80ac0
  162. #define VCVT_F64_F32 0xeeb70ac0
  163. #define VCVT_S32_F32 0xeebd0ac0
  164. #define VDIV_F32 0xee800a00
  165. #define VMOV_F32 0xeeb00a40
  166. #define VMOV 0xee000a10
  167. #define VMRS 0xeef1fa10
  168. #define VMUL_F32 0xee200a00
  169. #define VNEG_F32 0xeeb10a40
  170. #define VSTR_F32 0xed000a00
  171. #define VSUB_F32 0xee300a40
  172. static sljit_si push_inst16(struct sljit_compiler *compiler, sljit_ins inst)
  173. {
  174. sljit_uh *ptr;
  175. SLJIT_ASSERT(!(inst & 0xffff0000));
  176. ptr = (sljit_uh*)ensure_buf(compiler, sizeof(sljit_uh));
  177. FAIL_IF(!ptr);
  178. *ptr = inst;
  179. compiler->size++;
  180. return SLJIT_SUCCESS;
  181. }
  182. static sljit_si push_inst32(struct sljit_compiler *compiler, sljit_ins inst)
  183. {
  184. sljit_uh *ptr = (sljit_uh*)ensure_buf(compiler, sizeof(sljit_ins));
  185. FAIL_IF(!ptr);
  186. *ptr++ = inst >> 16;
  187. *ptr = inst;
  188. compiler->size += 2;
  189. return SLJIT_SUCCESS;
  190. }
  191. static SLJIT_INLINE sljit_si emit_imm32_const(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
  192. {
  193. FAIL_IF(push_inst32(compiler, MOVW | RD4(dst) |
  194. COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
  195. return push_inst32(compiler, MOVT | RD4(dst) |
  196. COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
  197. }
  198. static SLJIT_INLINE void modify_imm32_const(sljit_uh *inst, sljit_uw new_imm)
  199. {
  200. sljit_si dst = inst[1] & 0x0f00;
  201. SLJIT_ASSERT(((inst[0] & 0xfbf0) == (MOVW >> 16)) && ((inst[2] & 0xfbf0) == (MOVT >> 16)) && dst == (inst[3] & 0x0f00));
  202. inst[0] = (MOVW >> 16) | COPY_BITS(new_imm, 12, 0, 4) | COPY_BITS(new_imm, 11, 10, 1);
  203. inst[1] = dst | COPY_BITS(new_imm, 8, 12, 3) | (new_imm & 0xff);
  204. inst[2] = (MOVT >> 16) | COPY_BITS(new_imm, 12 + 16, 0, 4) | COPY_BITS(new_imm, 11 + 16, 10, 1);
  205. inst[3] = dst | COPY_BITS(new_imm, 8 + 16, 12, 3) | ((new_imm & 0xff0000) >> 16);
  206. }
  207. static SLJIT_INLINE sljit_si detect_jump_type(struct sljit_jump *jump, sljit_uh *code_ptr, sljit_uh *code)
  208. {
  209. sljit_sw diff;
  210. if (jump->flags & SLJIT_REWRITABLE_JUMP)
  211. return 0;
  212. if (jump->flags & JUMP_ADDR) {
  213. /* Branch to ARM code is not optimized yet. */
  214. if (!(jump->u.target & 0x1))
  215. return 0;
  216. diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2)) >> 1;
  217. }
  218. else {
  219. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  220. diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2)) >> 1;
  221. }
  222. if (jump->flags & IS_COND) {
  223. SLJIT_ASSERT(!(jump->flags & IS_BL));
  224. if (diff <= 127 && diff >= -128) {
  225. jump->flags |= PATCH_TYPE1;
  226. return 5;
  227. }
  228. if (diff <= 524287 && diff >= -524288) {
  229. jump->flags |= PATCH_TYPE2;
  230. return 4;
  231. }
  232. /* +1 comes from the prefix IT instruction. */
  233. diff--;
  234. if (diff <= 8388607 && diff >= -8388608) {
  235. jump->flags |= PATCH_TYPE3;
  236. return 3;
  237. }
  238. }
  239. else if (jump->flags & IS_BL) {
  240. if (diff <= 8388607 && diff >= -8388608) {
  241. jump->flags |= PATCH_BL;
  242. return 3;
  243. }
  244. }
  245. else {
  246. if (diff <= 1023 && diff >= -1024) {
  247. jump->flags |= PATCH_TYPE4;
  248. return 4;
  249. }
  250. if (diff <= 8388607 && diff >= -8388608) {
  251. jump->flags |= PATCH_TYPE5;
  252. return 3;
  253. }
  254. }
  255. return 0;
  256. }
  257. static SLJIT_INLINE void set_jump_instruction(struct sljit_jump *jump)
  258. {
  259. sljit_si type = (jump->flags >> 4) & 0xf;
  260. sljit_sw diff;
  261. sljit_uh *jump_inst;
  262. sljit_si s, j1, j2;
  263. if (SLJIT_UNLIKELY(type == 0)) {
  264. modify_imm32_const((sljit_uh*)jump->addr, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target);
  265. return;
  266. }
  267. if (jump->flags & JUMP_ADDR) {
  268. SLJIT_ASSERT(jump->u.target & 0x1);
  269. diff = ((sljit_sw)jump->u.target - (sljit_sw)(jump->addr + 4)) >> 1;
  270. }
  271. else
  272. diff = ((sljit_sw)(jump->u.label->addr) - (sljit_sw)(jump->addr + 4)) >> 1;
  273. jump_inst = (sljit_uh*)jump->addr;
  274. switch (type) {
  275. case 1:
  276. /* Encoding T1 of 'B' instruction */
  277. SLJIT_ASSERT(diff <= 127 && diff >= -128 && (jump->flags & IS_COND));
  278. jump_inst[0] = 0xd000 | (jump->flags & 0xf00) | (diff & 0xff);
  279. return;
  280. case 2:
  281. /* Encoding T3 of 'B' instruction */
  282. SLJIT_ASSERT(diff <= 524287 && diff >= -524288 && (jump->flags & IS_COND));
  283. jump_inst[0] = 0xf000 | COPY_BITS(jump->flags, 8, 6, 4) | COPY_BITS(diff, 11, 0, 6) | COPY_BITS(diff, 19, 10, 1);
  284. jump_inst[1] = 0x8000 | COPY_BITS(diff, 17, 13, 1) | COPY_BITS(diff, 18, 11, 1) | (diff & 0x7ff);
  285. return;
  286. case 3:
  287. SLJIT_ASSERT(jump->flags & IS_COND);
  288. *jump_inst++ = IT | ((jump->flags >> 4) & 0xf0) | 0x8;
  289. diff--;
  290. type = 5;
  291. break;
  292. case 4:
  293. /* Encoding T2 of 'B' instruction */
  294. SLJIT_ASSERT(diff <= 1023 && diff >= -1024 && !(jump->flags & IS_COND));
  295. jump_inst[0] = 0xe000 | (diff & 0x7ff);
  296. return;
  297. }
  298. SLJIT_ASSERT(diff <= 8388607 && diff >= -8388608);
  299. /* Really complex instruction form for branches. */
  300. s = (diff >> 23) & 0x1;
  301. j1 = (~(diff >> 21) ^ s) & 0x1;
  302. j2 = (~(diff >> 22) ^ s) & 0x1;
  303. jump_inst[0] = 0xf000 | (s << 10) | COPY_BITS(diff, 11, 0, 10);
  304. jump_inst[1] = (j1 << 13) | (j2 << 11) | (diff & 0x7ff);
  305. /* The others have a common form. */
  306. if (type == 5) /* Encoding T4 of 'B' instruction */
  307. jump_inst[1] |= 0x9000;
  308. else if (type == 6) /* Encoding T1 of 'BL' instruction */
  309. jump_inst[1] |= 0xd000;
  310. else
  311. SLJIT_ASSERT_STOP();
  312. }
  313. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
  314. {
  315. struct sljit_memory_fragment *buf;
  316. sljit_uh *code;
  317. sljit_uh *code_ptr;
  318. sljit_uh *buf_ptr;
  319. sljit_uh *buf_end;
  320. sljit_uw half_count;
  321. struct sljit_label *label;
  322. struct sljit_jump *jump;
  323. struct sljit_const *const_;
  324. CHECK_ERROR_PTR();
  325. CHECK_PTR(check_sljit_generate_code(compiler));
  326. reverse_buf(compiler);
  327. code = (sljit_uh*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_uh));
  328. PTR_FAIL_WITH_EXEC_IF(code);
  329. buf = compiler->buf;
  330. code_ptr = code;
  331. half_count = 0;
  332. label = compiler->labels;
  333. jump = compiler->jumps;
  334. const_ = compiler->consts;
  335. do {
  336. buf_ptr = (sljit_uh*)buf->memory;
  337. buf_end = buf_ptr + (buf->used_size >> 1);
  338. do {
  339. *code_ptr = *buf_ptr++;
  340. /* These structures are ordered by their address. */
  341. SLJIT_ASSERT(!label || label->size >= half_count);
  342. SLJIT_ASSERT(!jump || jump->addr >= half_count);
  343. SLJIT_ASSERT(!const_ || const_->addr >= half_count);
  344. if (label && label->size == half_count) {
  345. label->addr = ((sljit_uw)code_ptr) | 0x1;
  346. label->size = code_ptr - code;
  347. label = label->next;
  348. }
  349. if (jump && jump->addr == half_count) {
  350. jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
  351. code_ptr -= detect_jump_type(jump, code_ptr, code);
  352. jump = jump->next;
  353. }
  354. if (const_ && const_->addr == half_count) {
  355. const_->addr = (sljit_uw)code_ptr;
  356. const_ = const_->next;
  357. }
  358. code_ptr ++;
  359. half_count ++;
  360. } while (buf_ptr < buf_end);
  361. buf = buf->next;
  362. } while (buf);
  363. if (label && label->size == half_count) {
  364. label->addr = ((sljit_uw)code_ptr) | 0x1;
  365. label->size = code_ptr - code;
  366. label = label->next;
  367. }
  368. SLJIT_ASSERT(!label);
  369. SLJIT_ASSERT(!jump);
  370. SLJIT_ASSERT(!const_);
  371. SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
  372. jump = compiler->jumps;
  373. while (jump) {
  374. set_jump_instruction(jump);
  375. jump = jump->next;
  376. }
  377. compiler->error = SLJIT_ERR_COMPILED;
  378. compiler->executable_size = (code_ptr - code) * sizeof(sljit_uh);
  379. SLJIT_CACHE_FLUSH(code, code_ptr);
  380. /* Set thumb mode flag. */
  381. return (void*)((sljit_uw)code | 0x1);
  382. }
  383. /* --------------------------------------------------------------------- */
  384. /* Core code generator functions. */
  385. /* --------------------------------------------------------------------- */
  386. #define INVALID_IMM 0x80000000
  387. static sljit_uw get_imm(sljit_uw imm)
  388. {
  389. /* Thumb immediate form. */
  390. sljit_si counter;
  391. if (imm <= 0xff)
  392. return imm;
  393. if ((imm & 0xffff) == (imm >> 16)) {
  394. /* Some special cases. */
  395. if (!(imm & 0xff00))
  396. return (1 << 12) | (imm & 0xff);
  397. if (!(imm & 0xff))
  398. return (2 << 12) | ((imm >> 8) & 0xff);
  399. if ((imm & 0xff00) == ((imm & 0xff) << 8))
  400. return (3 << 12) | (imm & 0xff);
  401. }
  402. /* Assembly optimization: count leading zeroes? */
  403. counter = 8;
  404. if (!(imm & 0xffff0000)) {
  405. counter += 16;
  406. imm <<= 16;
  407. }
  408. if (!(imm & 0xff000000)) {
  409. counter += 8;
  410. imm <<= 8;
  411. }
  412. if (!(imm & 0xf0000000)) {
  413. counter += 4;
  414. imm <<= 4;
  415. }
  416. if (!(imm & 0xc0000000)) {
  417. counter += 2;
  418. imm <<= 2;
  419. }
  420. if (!(imm & 0x80000000)) {
  421. counter += 1;
  422. imm <<= 1;
  423. }
  424. /* Since imm >= 128, this must be true. */
  425. SLJIT_ASSERT(counter <= 31);
  426. if (imm & 0x00ffffff)
  427. return INVALID_IMM; /* Cannot be encoded. */
  428. return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
  429. }
  430. static sljit_si load_immediate(struct sljit_compiler *compiler, sljit_si dst, sljit_uw imm)
  431. {
  432. sljit_uw tmp;
  433. if (imm >= 0x10000) {
  434. tmp = get_imm(imm);
  435. if (tmp != INVALID_IMM)
  436. return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
  437. tmp = get_imm(~imm);
  438. if (tmp != INVALID_IMM)
  439. return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
  440. }
  441. /* set low 16 bits, set hi 16 bits to 0. */
  442. FAIL_IF(push_inst32(compiler, MOVW | RD4(dst) |
  443. COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
  444. /* set hi 16 bit if needed. */
  445. if (imm >= 0x10000)
  446. return push_inst32(compiler, MOVT | RD4(dst) |
  447. COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
  448. return SLJIT_SUCCESS;
  449. }
  450. #define ARG1_IMM 0x0010000
  451. #define ARG2_IMM 0x0020000
  452. #define KEEP_FLAGS 0x0040000
  453. /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
  454. #define SET_FLAGS 0x0100000
  455. #define UNUSED_RETURN 0x0200000
  456. #define SLOW_DEST 0x0400000
  457. #define SLOW_SRC1 0x0800000
  458. #define SLOW_SRC2 0x1000000
  459. static sljit_si emit_op_imm(struct sljit_compiler *compiler, sljit_si flags, sljit_si dst, sljit_uw arg1, sljit_uw arg2)
  460. {
  461. /* dst must be register, TMP_REG1
  462. arg1 must be register, TMP_REG1, imm
  463. arg2 must be register, TMP_REG2, imm */
  464. sljit_si reg;
  465. sljit_uw imm, nimm;
  466. if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
  467. /* Both are immediates. */
  468. flags &= ~ARG1_IMM;
  469. FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
  470. arg1 = TMP_REG1;
  471. }
  472. if (flags & (ARG1_IMM | ARG2_IMM)) {
  473. reg = (flags & ARG2_IMM) ? arg1 : arg2;
  474. imm = (flags & ARG2_IMM) ? arg2 : arg1;
  475. switch (flags & 0xffff) {
  476. case SLJIT_CLZ:
  477. case SLJIT_MUL:
  478. /* No form with immediate operand. */
  479. break;
  480. case SLJIT_MOV:
  481. SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG1);
  482. return load_immediate(compiler, dst, imm);
  483. case SLJIT_NOT:
  484. if (!(flags & SET_FLAGS))
  485. return load_immediate(compiler, dst, ~imm);
  486. /* Since the flags should be set, we just fallback to the register mode.
  487. Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
  488. break;
  489. case SLJIT_ADD:
  490. nimm = -imm;
  491. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(reg, dst)) {
  492. if (imm <= 0x7)
  493. return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  494. if (nimm <= 0x7)
  495. return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  496. if (reg == dst) {
  497. if (imm <= 0xff)
  498. return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
  499. if (nimm <= 0xff)
  500. return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
  501. }
  502. }
  503. if (!(flags & SET_FLAGS)) {
  504. if (imm <= 0xfff)
  505. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
  506. if (nimm <= 0xfff)
  507. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  508. }
  509. imm = get_imm(imm);
  510. if (imm != INVALID_IMM)
  511. return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  512. break;
  513. case SLJIT_ADDC:
  514. imm = get_imm(imm);
  515. if (imm != INVALID_IMM)
  516. return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  517. break;
  518. case SLJIT_SUB:
  519. if (flags & ARG1_IMM) {
  520. if (!(flags & KEEP_FLAGS) && imm == 0 && IS_2_LO_REGS(reg, dst))
  521. return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
  522. imm = get_imm(imm);
  523. if (imm != INVALID_IMM)
  524. return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  525. break;
  526. }
  527. nimm = -imm;
  528. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(reg, dst)) {
  529. if (imm <= 0x7)
  530. return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  531. if (nimm <= 0x7)
  532. return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  533. if (reg == dst) {
  534. if (imm <= 0xff)
  535. return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
  536. if (nimm <= 0xff)
  537. return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
  538. }
  539. if (imm <= 0xff && (flags & UNUSED_RETURN))
  540. return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
  541. }
  542. if (!(flags & SET_FLAGS)) {
  543. if (imm <= 0xfff)
  544. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
  545. if (nimm <= 0xfff)
  546. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  547. }
  548. imm = get_imm(imm);
  549. if (imm != INVALID_IMM)
  550. return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  551. break;
  552. case SLJIT_SUBC:
  553. if (flags & ARG1_IMM)
  554. break;
  555. imm = get_imm(imm);
  556. if (imm != INVALID_IMM)
  557. return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  558. break;
  559. case SLJIT_AND:
  560. nimm = get_imm(imm);
  561. if (nimm != INVALID_IMM)
  562. return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  563. imm = get_imm(imm);
  564. if (imm != INVALID_IMM)
  565. return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  566. break;
  567. case SLJIT_OR:
  568. nimm = get_imm(imm);
  569. if (nimm != INVALID_IMM)
  570. return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  571. imm = get_imm(imm);
  572. if (imm != INVALID_IMM)
  573. return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  574. break;
  575. case SLJIT_XOR:
  576. imm = get_imm(imm);
  577. if (imm != INVALID_IMM)
  578. return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  579. break;
  580. case SLJIT_SHL:
  581. case SLJIT_LSHR:
  582. case SLJIT_ASHR:
  583. if (flags & ARG1_IMM)
  584. break;
  585. imm &= 0x1f;
  586. if (imm == 0) {
  587. if (!(flags & SET_FLAGS))
  588. return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
  589. if (IS_2_LO_REGS(dst, reg))
  590. return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
  591. return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
  592. }
  593. switch (flags & 0xffff) {
  594. case SLJIT_SHL:
  595. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
  596. return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
  597. return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  598. case SLJIT_LSHR:
  599. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
  600. return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
  601. return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  602. default: /* SLJIT_ASHR */
  603. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, reg))
  604. return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
  605. return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  606. }
  607. default:
  608. SLJIT_ASSERT_STOP();
  609. break;
  610. }
  611. if (flags & ARG2_IMM) {
  612. FAIL_IF(load_immediate(compiler, TMP_REG2, arg2));
  613. arg2 = TMP_REG2;
  614. }
  615. else {
  616. FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
  617. arg1 = TMP_REG1;
  618. }
  619. }
  620. /* Both arguments are registers. */
  621. switch (flags & 0xffff) {
  622. case SLJIT_MOV:
  623. case SLJIT_MOV_UI:
  624. case SLJIT_MOV_SI:
  625. case SLJIT_MOV_P:
  626. case SLJIT_MOVU:
  627. case SLJIT_MOVU_UI:
  628. case SLJIT_MOVU_SI:
  629. case SLJIT_MOVU_P:
  630. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
  631. if (dst == arg2)
  632. return SLJIT_SUCCESS;
  633. return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
  634. case SLJIT_MOV_UB:
  635. case SLJIT_MOVU_UB:
  636. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
  637. if (IS_2_LO_REGS(dst, arg2))
  638. return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
  639. return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
  640. case SLJIT_MOV_SB:
  641. case SLJIT_MOVU_SB:
  642. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
  643. if (IS_2_LO_REGS(dst, arg2))
  644. return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
  645. return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
  646. case SLJIT_MOV_UH:
  647. case SLJIT_MOVU_UH:
  648. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
  649. if (IS_2_LO_REGS(dst, arg2))
  650. return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
  651. return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
  652. case SLJIT_MOV_SH:
  653. case SLJIT_MOVU_SH:
  654. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG1);
  655. if (IS_2_LO_REGS(dst, arg2))
  656. return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
  657. return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
  658. case SLJIT_NOT:
  659. SLJIT_ASSERT(arg1 == TMP_REG1);
  660. if (!(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  661. return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
  662. return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
  663. case SLJIT_CLZ:
  664. SLJIT_ASSERT(arg1 == TMP_REG1);
  665. FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
  666. if (flags & SET_FLAGS) {
  667. if (reg_map[dst] <= 7)
  668. return push_inst16(compiler, CMPI | RDN3(dst));
  669. return push_inst32(compiler, ADD_WI | SET_FLAGS | RN4(dst) | RD4(dst));
  670. }
  671. return SLJIT_SUCCESS;
  672. case SLJIT_ADD:
  673. if (!(flags & KEEP_FLAGS) && IS_3_LO_REGS(dst, arg1, arg2))
  674. return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
  675. if (dst == arg1 && !(flags & SET_FLAGS))
  676. return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
  677. return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  678. case SLJIT_ADDC:
  679. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  680. return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
  681. return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  682. case SLJIT_SUB:
  683. if (!(flags & KEEP_FLAGS) && IS_3_LO_REGS(dst, arg1, arg2))
  684. return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
  685. return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  686. case SLJIT_SUBC:
  687. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  688. return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
  689. return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  690. case SLJIT_MUL:
  691. if (!(flags & SET_FLAGS))
  692. return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
  693. SLJIT_ASSERT(reg_map[TMP_REG2] <= 7 && dst != TMP_REG2);
  694. FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
  695. /* cmp TMP_REG2, dst asr #31. */
  696. return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
  697. case SLJIT_AND:
  698. if (!(flags & KEEP_FLAGS)) {
  699. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  700. return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
  701. if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
  702. return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
  703. }
  704. return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  705. case SLJIT_OR:
  706. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  707. return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
  708. return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  709. case SLJIT_XOR:
  710. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  711. return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
  712. return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  713. case SLJIT_SHL:
  714. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  715. return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
  716. return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  717. case SLJIT_LSHR:
  718. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  719. return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
  720. return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  721. case SLJIT_ASHR:
  722. if (dst == arg1 && !(flags & KEEP_FLAGS) && IS_2_LO_REGS(dst, arg2))
  723. return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
  724. return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  725. }
  726. SLJIT_ASSERT_STOP();
  727. return SLJIT_SUCCESS;
  728. }
  729. #define STORE 0x01
  730. #define SIGNED 0x02
  731. #define WORD_SIZE 0x00
  732. #define BYTE_SIZE 0x04
  733. #define HALF_SIZE 0x08
  734. #define UPDATE 0x10
  735. #define ARG_TEST 0x20
  736. #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
  737. #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
  738. /*
  739. 1st letter:
  740. w = word
  741. b = byte
  742. h = half
  743. 2nd letter:
  744. s = signed
  745. u = unsigned
  746. 3rd letter:
  747. l = load
  748. s = store
  749. */
  750. static SLJIT_CONST sljit_ins sljit_mem16[12] = {
  751. /* w u l */ 0x5800 /* ldr */,
  752. /* w u s */ 0x5000 /* str */,
  753. /* w s l */ 0x5800 /* ldr */,
  754. /* w s s */ 0x5000 /* str */,
  755. /* b u l */ 0x5c00 /* ldrb */,
  756. /* b u s */ 0x5400 /* strb */,
  757. /* b s l */ 0x5600 /* ldrsb */,
  758. /* b s s */ 0x5400 /* strb */,
  759. /* h u l */ 0x5a00 /* ldrh */,
  760. /* h u s */ 0x5200 /* strh */,
  761. /* h s l */ 0x5e00 /* ldrsh */,
  762. /* h s s */ 0x5200 /* strh */,
  763. };
  764. static SLJIT_CONST sljit_ins sljit_mem16_imm5[12] = {
  765. /* w u l */ 0x6800 /* ldr imm5 */,
  766. /* w u s */ 0x6000 /* str imm5 */,
  767. /* w s l */ 0x6800 /* ldr imm5 */,
  768. /* w s s */ 0x6000 /* str imm5 */,
  769. /* b u l */ 0x7800 /* ldrb imm5 */,
  770. /* b u s */ 0x7000 /* strb imm5 */,
  771. /* b s l */ 0x0000 /* not allowed */,
  772. /* b s s */ 0x7000 /* strb imm5 */,
  773. /* h u l */ 0x8800 /* ldrh imm5 */,
  774. /* h u s */ 0x8000 /* strh imm5 */,
  775. /* h s l */ 0x0000 /* not allowed */,
  776. /* h s s */ 0x8000 /* strh imm5 */,
  777. };
  778. #define MEM_IMM8 0xc00
  779. #define MEM_IMM12 0x800000
  780. static SLJIT_CONST sljit_ins sljit_mem32[12] = {
  781. /* w u l */ 0xf8500000 /* ldr.w */,
  782. /* w u s */ 0xf8400000 /* str.w */,
  783. /* w s l */ 0xf8500000 /* ldr.w */,
  784. /* w s s */ 0xf8400000 /* str.w */,
  785. /* b u l */ 0xf8100000 /* ldrb.w */,
  786. /* b u s */ 0xf8000000 /* strb.w */,
  787. /* b s l */ 0xf9100000 /* ldrsb.w */,
  788. /* b s s */ 0xf8000000 /* strb.w */,
  789. /* h u l */ 0xf8300000 /* ldrh.w */,
  790. /* h u s */ 0xf8200000 /* strsh.w */,
  791. /* h s l */ 0xf9300000 /* ldrsh.w */,
  792. /* h s s */ 0xf8200000 /* strsh.w */,
  793. };
  794. /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
  795. static sljit_si emit_set_delta(struct sljit_compiler *compiler, sljit_si dst, sljit_si reg, sljit_sw value)
  796. {
  797. if (value >= 0) {
  798. if (value <= 0xfff)
  799. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
  800. value = get_imm(value);
  801. if (value != INVALID_IMM)
  802. return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
  803. }
  804. else {
  805. value = -value;
  806. if (value <= 0xfff)
  807. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
  808. value = get_imm(value);
  809. if (value != INVALID_IMM)
  810. return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
  811. }
  812. return SLJIT_ERR_UNSUPPORTED;
  813. }
  814. /* Can perform an operation using at most 1 instruction. */
  815. static sljit_si getput_arg_fast(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
  816. {
  817. sljit_si other_r, shift;
  818. SLJIT_ASSERT(arg & SLJIT_MEM);
  819. if (SLJIT_UNLIKELY(flags & UPDATE)) {
  820. if ((arg & REG_MASK) && !(arg & OFFS_REG_MASK) && argw <= 0xff && argw >= -0xff) {
  821. if (SLJIT_UNLIKELY(flags & ARG_TEST))
  822. return 1;
  823. flags &= ~UPDATE;
  824. arg &= 0xf;
  825. if (argw >= 0)
  826. argw |= 0x200;
  827. else {
  828. argw = -argw;
  829. }
  830. SLJIT_ASSERT(argw >= 0 && (argw & 0xff) <= 0xff);
  831. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | 0x100 | argw));
  832. return -1;
  833. }
  834. return 0;
  835. }
  836. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  837. if (SLJIT_UNLIKELY(flags & ARG_TEST))
  838. return 1;
  839. argw &= 0x3;
  840. other_r = OFFS_REG(arg);
  841. arg &= 0xf;
  842. if (!argw && IS_3_LO_REGS(reg, arg, other_r))
  843. FAIL_IF(push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r)));
  844. else
  845. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4)));
  846. return -1;
  847. }
  848. if (!(arg & REG_MASK) || argw > 0xfff || argw < -0xff)
  849. return 0;
  850. if (SLJIT_UNLIKELY(flags & ARG_TEST))
  851. return 1;
  852. arg &= 0xf;
  853. if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
  854. shift = 3;
  855. if (IS_WORD_SIZE(flags)) {
  856. if (OFFSET_CHECK(0x1f, 2))
  857. shift = 2;
  858. }
  859. else if (flags & BYTE_SIZE)
  860. {
  861. if (OFFSET_CHECK(0x1f, 0))
  862. shift = 0;
  863. }
  864. else {
  865. SLJIT_ASSERT(flags & HALF_SIZE);
  866. if (OFFSET_CHECK(0x1f, 1))
  867. shift = 1;
  868. }
  869. if (shift != 3) {
  870. FAIL_IF(push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - shift))));
  871. return -1;
  872. }
  873. }
  874. /* SP based immediate. */
  875. if (SLJIT_UNLIKELY(arg == SLJIT_SP) && OFFSET_CHECK(0xff, 2) && IS_WORD_SIZE(flags) && reg_map[reg] <= 7) {
  876. FAIL_IF(push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2)));
  877. return -1;
  878. }
  879. if (argw >= 0)
  880. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw));
  881. else
  882. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw));
  883. return -1;
  884. }
  885. /* see getput_arg below.
  886. Note: can_cache is called only for binary operators. Those
  887. operators always uses word arguments without write back. */
  888. static sljit_si can_cache(sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
  889. {
  890. sljit_sw diff;
  891. if ((arg & OFFS_REG_MASK) || !(next_arg & SLJIT_MEM))
  892. return 0;
  893. if (!(arg & REG_MASK)) {
  894. diff = argw - next_argw;
  895. if (diff <= 0xfff && diff >= -0xfff)
  896. return 1;
  897. return 0;
  898. }
  899. if (argw == next_argw)
  900. return 1;
  901. diff = argw - next_argw;
  902. if (arg == next_arg && diff <= 0xfff && diff >= -0xfff)
  903. return 1;
  904. return 0;
  905. }
  906. /* Emit the necessary instructions. See can_cache above. */
  907. static sljit_si getput_arg(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg,
  908. sljit_si arg, sljit_sw argw, sljit_si next_arg, sljit_sw next_argw)
  909. {
  910. sljit_si tmp_r, other_r;
  911. sljit_sw diff;
  912. SLJIT_ASSERT(arg & SLJIT_MEM);
  913. if (!(next_arg & SLJIT_MEM)) {
  914. next_arg = 0;
  915. next_argw = 0;
  916. }
  917. tmp_r = (flags & STORE) ? TMP_REG3 : reg;
  918. if (SLJIT_UNLIKELY((flags & UPDATE) && (arg & REG_MASK))) {
  919. /* Update only applies if a base register exists. */
  920. /* There is no caching here. */
  921. other_r = OFFS_REG(arg);
  922. arg &= 0xf;
  923. flags &= ~UPDATE;
  924. if (!other_r) {
  925. if (!(argw & ~0xfff)) {
  926. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw));
  927. return push_inst32(compiler, ADDWI | RD4(arg) | RN4(arg) | IMM12(argw));
  928. }
  929. if (compiler->cache_arg == SLJIT_MEM) {
  930. if (argw == compiler->cache_argw) {
  931. other_r = TMP_REG3;
  932. argw = 0;
  933. }
  934. else if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
  935. FAIL_IF(compiler->error);
  936. compiler->cache_argw = argw;
  937. other_r = TMP_REG3;
  938. argw = 0;
  939. }
  940. }
  941. if (argw) {
  942. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  943. compiler->cache_arg = SLJIT_MEM;
  944. compiler->cache_argw = argw;
  945. other_r = TMP_REG3;
  946. argw = 0;
  947. }
  948. }
  949. argw &= 0x3;
  950. if (!argw && IS_3_LO_REGS(reg, arg, other_r)) {
  951. FAIL_IF(push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r)));
  952. return push_inst16(compiler, ADD | SET_REGS44(arg, other_r));
  953. }
  954. FAIL_IF(push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4)));
  955. return push_inst32(compiler, ADD_W | RD4(arg) | RN4(arg) | RM4(other_r) | (argw << 6));
  956. }
  957. flags &= ~UPDATE;
  958. SLJIT_ASSERT(!(arg & OFFS_REG_MASK));
  959. if (compiler->cache_arg == arg) {
  960. diff = argw - compiler->cache_argw;
  961. if (!(diff & ~0xfff))
  962. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | diff);
  963. if (!((compiler->cache_argw - argw) & ~0xff))
  964. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(TMP_REG3) | (compiler->cache_argw - argw));
  965. if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, diff) != SLJIT_ERR_UNSUPPORTED) {
  966. FAIL_IF(compiler->error);
  967. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | 0);
  968. }
  969. }
  970. next_arg = (arg & REG_MASK) && (arg == next_arg) && (argw != next_argw);
  971. arg &= 0xf;
  972. if (arg && compiler->cache_arg == SLJIT_MEM) {
  973. if (compiler->cache_argw == argw)
  974. return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(TMP_REG3));
  975. if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, argw - compiler->cache_argw) != SLJIT_ERR_UNSUPPORTED) {
  976. FAIL_IF(compiler->error);
  977. compiler->cache_argw = argw;
  978. return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(TMP_REG3));
  979. }
  980. }
  981. compiler->cache_argw = argw;
  982. if (next_arg && emit_set_delta(compiler, TMP_REG3, arg, argw) != SLJIT_ERR_UNSUPPORTED) {
  983. FAIL_IF(compiler->error);
  984. compiler->cache_arg = SLJIT_MEM | arg;
  985. arg = 0;
  986. }
  987. else {
  988. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  989. compiler->cache_arg = SLJIT_MEM;
  990. diff = argw - next_argw;
  991. if (next_arg && diff <= 0xfff && diff >= -0xfff) {
  992. FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG3, arg)));
  993. compiler->cache_arg = SLJIT_MEM | arg;
  994. arg = 0;
  995. }
  996. }
  997. if (arg)
  998. return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(TMP_REG3));
  999. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(TMP_REG3) | 0);
  1000. }
  1001. static SLJIT_INLINE sljit_si emit_op_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
  1002. {
  1003. if (getput_arg_fast(compiler, flags, reg, arg, argw))
  1004. return compiler->error;
  1005. compiler->cache_arg = 0;
  1006. compiler->cache_argw = 0;
  1007. return getput_arg(compiler, flags, reg, arg, argw, 0, 0);
  1008. }
  1009. 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)
  1010. {
  1011. if (getput_arg_fast(compiler, flags, reg, arg1, arg1w))
  1012. return compiler->error;
  1013. return getput_arg(compiler, flags, reg, arg1, arg1w, arg2, arg2w);
  1014. }
  1015. /* --------------------------------------------------------------------- */
  1016. /* Entry, exit */
  1017. /* --------------------------------------------------------------------- */
  1018. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
  1019. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  1020. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  1021. {
  1022. sljit_si size, i, tmp;
  1023. sljit_ins push;
  1024. CHECK_ERROR();
  1025. CHECK(check_sljit_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  1026. set_emit_enter(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  1027. push = (1 << 4);
  1028. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  1029. for (i = SLJIT_S0; i >= tmp; i--)
  1030. push |= 1 << reg_map[i];
  1031. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  1032. push |= 1 << reg_map[i];
  1033. FAIL_IF((push & 0xff00)
  1034. ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
  1035. : push_inst16(compiler, PUSH | (1 << 8) | push));
  1036. /* Stack must be aligned to 8 bytes: (LR, R4) */
  1037. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
  1038. local_size = ((size + local_size + 7) & ~7) - size;
  1039. compiler->local_size = local_size;
  1040. if (local_size > 0) {
  1041. if (local_size <= (127 << 2))
  1042. FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
  1043. else
  1044. FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
  1045. }
  1046. if (args >= 1)
  1047. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
  1048. if (args >= 2)
  1049. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
  1050. if (args >= 3)
  1051. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
  1052. return SLJIT_SUCCESS;
  1053. }
  1054. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_set_context(struct sljit_compiler *compiler,
  1055. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  1056. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size)
  1057. {
  1058. sljit_si size;
  1059. CHECK_ERROR();
  1060. CHECK(check_sljit_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size));
  1061. set_set_context(compiler, options, args, scratches, saveds, fscratches, fsaveds, local_size);
  1062. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 2);
  1063. compiler->local_size = ((size + local_size + 7) & ~7) - size;
  1064. return SLJIT_SUCCESS;
  1065. }
  1066. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op, sljit_si src, sljit_sw srcw)
  1067. {
  1068. sljit_si i, tmp;
  1069. sljit_ins pop;
  1070. CHECK_ERROR();
  1071. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  1072. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  1073. if (compiler->local_size > 0) {
  1074. if (compiler->local_size <= (127 << 2))
  1075. FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
  1076. else
  1077. FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
  1078. }
  1079. pop = (1 << 4);
  1080. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  1081. for (i = SLJIT_S0; i >= tmp; i--)
  1082. pop |= 1 << reg_map[i];
  1083. for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  1084. pop |= 1 << reg_map[i];
  1085. return (pop & 0xff00)
  1086. ? push_inst32(compiler, POP_W | (1 << 15) | pop)
  1087. : push_inst16(compiler, POP | (1 << 8) | pop);
  1088. }
  1089. /* --------------------------------------------------------------------- */
  1090. /* Operators */
  1091. /* --------------------------------------------------------------------- */
  1092. #ifdef __cplusplus
  1093. extern "C" {
  1094. #endif
  1095. #if defined(__GNUC__)
  1096. extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
  1097. extern int __aeabi_idivmod(int numerator, int denominator);
  1098. #else
  1099. #error "Software divmod functions are needed"
  1100. #endif
  1101. #ifdef __cplusplus
  1102. }
  1103. #endif
  1104. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op)
  1105. {
  1106. sljit_sw saved_reg_list[3];
  1107. sljit_sw saved_reg_count;
  1108. CHECK_ERROR();
  1109. CHECK(check_sljit_emit_op0(compiler, op));
  1110. op = GET_OPCODE(op);
  1111. switch (op) {
  1112. case SLJIT_BREAKPOINT:
  1113. return push_inst16(compiler, BKPT);
  1114. case SLJIT_NOP:
  1115. return push_inst16(compiler, NOP);
  1116. case SLJIT_LUMUL:
  1117. case SLJIT_LSMUL:
  1118. return push_inst32(compiler, (op == SLJIT_LUMUL ? UMULL : SMULL)
  1119. | (reg_map[SLJIT_R1] << 8)
  1120. | (reg_map[SLJIT_R0] << 12)
  1121. | (reg_map[SLJIT_R0] << 16)
  1122. | reg_map[SLJIT_R1]);
  1123. case SLJIT_UDIVMOD:
  1124. case SLJIT_SDIVMOD:
  1125. case SLJIT_UDIVI:
  1126. case SLJIT_SDIVI:
  1127. SLJIT_COMPILE_ASSERT((SLJIT_UDIVMOD & 0x2) == 0 && SLJIT_UDIVI - 0x2 == SLJIT_UDIVMOD, bad_div_opcode_assignments);
  1128. SLJIT_COMPILE_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 12, bad_register_mapping);
  1129. saved_reg_count = 0;
  1130. if (compiler->scratches >= 4)
  1131. saved_reg_list[saved_reg_count++] = 12;
  1132. if (compiler->scratches >= 3)
  1133. saved_reg_list[saved_reg_count++] = 2;
  1134. if (op >= SLJIT_UDIVI)
  1135. saved_reg_list[saved_reg_count++] = 1;
  1136. if (saved_reg_count > 0) {
  1137. FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
  1138. | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
  1139. if (saved_reg_count >= 2) {
  1140. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1141. FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
  1142. }
  1143. if (saved_reg_count >= 3) {
  1144. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1145. FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
  1146. }
  1147. }
  1148. #if defined(__GNUC__)
  1149. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
  1150. ((op | 0x2) == SLJIT_UDIVI ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
  1151. #else
  1152. #error "Software divmod functions are needed"
  1153. #endif
  1154. if (saved_reg_count > 0) {
  1155. if (saved_reg_count >= 3) {
  1156. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1157. FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
  1158. }
  1159. if (saved_reg_count >= 2) {
  1160. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1161. FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
  1162. }
  1163. return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
  1164. | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
  1165. }
  1166. return SLJIT_SUCCESS;
  1167. }
  1168. return SLJIT_SUCCESS;
  1169. }
  1170. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
  1171. sljit_si dst, sljit_sw dstw,
  1172. sljit_si src, sljit_sw srcw)
  1173. {
  1174. sljit_si dst_r, flags;
  1175. sljit_si op_flags = GET_ALL_FLAGS(op);
  1176. CHECK_ERROR();
  1177. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1178. ADJUST_LOCAL_OFFSET(dst, dstw);
  1179. ADJUST_LOCAL_OFFSET(src, srcw);
  1180. compiler->cache_arg = 0;
  1181. compiler->cache_argw = 0;
  1182. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
  1183. op = GET_OPCODE(op);
  1184. if (op >= SLJIT_MOV && op <= SLJIT_MOVU_P) {
  1185. switch (op) {
  1186. case SLJIT_MOV:
  1187. case SLJIT_MOV_UI:
  1188. case SLJIT_MOV_SI:
  1189. case SLJIT_MOV_P:
  1190. flags = WORD_SIZE;
  1191. break;
  1192. case SLJIT_MOV_UB:
  1193. flags = BYTE_SIZE;
  1194. if (src & SLJIT_IMM)
  1195. srcw = (sljit_ub)srcw;
  1196. break;
  1197. case SLJIT_MOV_SB:
  1198. flags = BYTE_SIZE | SIGNED;
  1199. if (src & SLJIT_IMM)
  1200. srcw = (sljit_sb)srcw;
  1201. break;
  1202. case SLJIT_MOV_UH:
  1203. flags = HALF_SIZE;
  1204. if (src & SLJIT_IMM)
  1205. srcw = (sljit_uh)srcw;
  1206. break;
  1207. case SLJIT_MOV_SH:
  1208. flags = HALF_SIZE | SIGNED;
  1209. if (src & SLJIT_IMM)
  1210. srcw = (sljit_sh)srcw;
  1211. break;
  1212. case SLJIT_MOVU:
  1213. case SLJIT_MOVU_UI:
  1214. case SLJIT_MOVU_SI:
  1215. case SLJIT_MOVU_P:
  1216. flags = WORD_SIZE | UPDATE;
  1217. break;
  1218. case SLJIT_MOVU_UB:
  1219. flags = BYTE_SIZE | UPDATE;
  1220. if (src & SLJIT_IMM)
  1221. srcw = (sljit_ub)srcw;
  1222. break;
  1223. case SLJIT_MOVU_SB:
  1224. flags = BYTE_SIZE | SIGNED | UPDATE;
  1225. if (src & SLJIT_IMM)
  1226. srcw = (sljit_sb)srcw;
  1227. break;
  1228. case SLJIT_MOVU_UH:
  1229. flags = HALF_SIZE | UPDATE;
  1230. if (src & SLJIT_IMM)
  1231. srcw = (sljit_uh)srcw;
  1232. break;
  1233. case SLJIT_MOVU_SH:
  1234. flags = HALF_SIZE | SIGNED | UPDATE;
  1235. if (src & SLJIT_IMM)
  1236. srcw = (sljit_sh)srcw;
  1237. break;
  1238. default:
  1239. SLJIT_ASSERT_STOP();
  1240. flags = 0;
  1241. break;
  1242. }
  1243. if (src & SLJIT_IMM)
  1244. FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG1, srcw));
  1245. else if (src & SLJIT_MEM) {
  1246. if (getput_arg_fast(compiler, flags, dst_r, src, srcw))
  1247. FAIL_IF(compiler->error);
  1248. else
  1249. FAIL_IF(getput_arg(compiler, flags, dst_r, src, srcw, dst, dstw));
  1250. } else {
  1251. if (dst_r != TMP_REG1)
  1252. return emit_op_imm(compiler, op, dst_r, TMP_REG1, src);
  1253. dst_r = src;
  1254. }
  1255. if (dst & SLJIT_MEM) {
  1256. if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
  1257. return compiler->error;
  1258. else
  1259. return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
  1260. }
  1261. return SLJIT_SUCCESS;
  1262. }
  1263. if (op == SLJIT_NEG) {
  1264. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1265. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1266. compiler->skip_checks = 1;
  1267. #endif
  1268. return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
  1269. }
  1270. flags = (GET_FLAGS(op_flags) ? SET_FLAGS : 0) | ((op_flags & SLJIT_KEEP_FLAGS) ? KEEP_FLAGS : 0);
  1271. if (src & SLJIT_MEM) {
  1272. if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG2, src, srcw))
  1273. FAIL_IF(compiler->error);
  1274. else
  1275. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src, srcw, dst, dstw));
  1276. src = TMP_REG2;
  1277. }
  1278. if (src & SLJIT_IMM)
  1279. flags |= ARG2_IMM;
  1280. else
  1281. srcw = src;
  1282. emit_op_imm(compiler, flags | op, dst_r, TMP_REG1, srcw);
  1283. if (dst & SLJIT_MEM) {
  1284. if (getput_arg_fast(compiler, flags | STORE, dst_r, dst, dstw))
  1285. return compiler->error;
  1286. else
  1287. return getput_arg(compiler, flags | STORE, dst_r, dst, dstw, 0, 0);
  1288. }
  1289. return SLJIT_SUCCESS;
  1290. }
  1291. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
  1292. sljit_si dst, sljit_sw dstw,
  1293. sljit_si src1, sljit_sw src1w,
  1294. sljit_si src2, sljit_sw src2w)
  1295. {
  1296. sljit_si dst_r, flags;
  1297. CHECK_ERROR();
  1298. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1299. ADJUST_LOCAL_OFFSET(dst, dstw);
  1300. ADJUST_LOCAL_OFFSET(src1, src1w);
  1301. ADJUST_LOCAL_OFFSET(src2, src2w);
  1302. compiler->cache_arg = 0;
  1303. compiler->cache_argw = 0;
  1304. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
  1305. flags = (GET_FLAGS(op) ? SET_FLAGS : 0) | ((op & SLJIT_KEEP_FLAGS) ? KEEP_FLAGS : 0);
  1306. if ((dst & SLJIT_MEM) && !getput_arg_fast(compiler, WORD_SIZE | STORE | ARG_TEST, TMP_REG1, dst, dstw))
  1307. flags |= SLOW_DEST;
  1308. if (src1 & SLJIT_MEM) {
  1309. if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG1, src1, src1w))
  1310. FAIL_IF(compiler->error);
  1311. else
  1312. flags |= SLOW_SRC1;
  1313. }
  1314. if (src2 & SLJIT_MEM) {
  1315. if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG2, src2, src2w))
  1316. FAIL_IF(compiler->error);
  1317. else
  1318. flags |= SLOW_SRC2;
  1319. }
  1320. if ((flags & (SLOW_SRC1 | SLOW_SRC2)) == (SLOW_SRC1 | SLOW_SRC2)) {
  1321. if (!can_cache(src1, src1w, src2, src2w) && can_cache(src1, src1w, dst, dstw)) {
  1322. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, src1, src1w));
  1323. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, dst, dstw));
  1324. }
  1325. else {
  1326. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, src2, src2w));
  1327. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, dst, dstw));
  1328. }
  1329. }
  1330. else if (flags & SLOW_SRC1)
  1331. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG1, src1, src1w, dst, dstw));
  1332. else if (flags & SLOW_SRC2)
  1333. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src2, src2w, dst, dstw));
  1334. if (src1 & SLJIT_MEM)
  1335. src1 = TMP_REG1;
  1336. if (src2 & SLJIT_MEM)
  1337. src2 = TMP_REG2;
  1338. if (src1 & SLJIT_IMM)
  1339. flags |= ARG1_IMM;
  1340. else
  1341. src1w = src1;
  1342. if (src2 & SLJIT_IMM)
  1343. flags |= ARG2_IMM;
  1344. else
  1345. src2w = src2;
  1346. if (dst == SLJIT_UNUSED)
  1347. flags |= UNUSED_RETURN;
  1348. emit_op_imm(compiler, flags | GET_OPCODE(op), dst_r, src1w, src2w);
  1349. if (dst & SLJIT_MEM) {
  1350. if (!(flags & SLOW_DEST)) {
  1351. getput_arg_fast(compiler, WORD_SIZE | STORE, dst_r, dst, dstw);
  1352. return compiler->error;
  1353. }
  1354. return getput_arg(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, 0, 0);
  1355. }
  1356. return SLJIT_SUCCESS;
  1357. }
  1358. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg)
  1359. {
  1360. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1361. return reg_map[reg];
  1362. }
  1363. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg)
  1364. {
  1365. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1366. return reg << 1;
  1367. }
  1368. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
  1369. void *instruction, sljit_si size)
  1370. {
  1371. CHECK_ERROR();
  1372. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1373. if (size == 2)
  1374. return push_inst16(compiler, *(sljit_uh*)instruction);
  1375. return push_inst32(compiler, *(sljit_ins*)instruction);
  1376. }
  1377. /* --------------------------------------------------------------------- */
  1378. /* Floating point operators */
  1379. /* --------------------------------------------------------------------- */
  1380. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void)
  1381. {
  1382. #ifdef SLJIT_IS_FPU_AVAILABLE
  1383. return SLJIT_IS_FPU_AVAILABLE;
  1384. #else
  1385. /* Available by default. */
  1386. return 1;
  1387. #endif
  1388. }
  1389. #define FPU_LOAD (1 << 20)
  1390. static sljit_si emit_fop_mem(struct sljit_compiler *compiler, sljit_si flags, sljit_si reg, sljit_si arg, sljit_sw argw)
  1391. {
  1392. sljit_sw tmp;
  1393. sljit_uw imm;
  1394. sljit_sw inst = VSTR_F32 | (flags & (SLJIT_SINGLE_OP | FPU_LOAD));
  1395. SLJIT_ASSERT(arg & SLJIT_MEM);
  1396. /* Fast loads and stores. */
  1397. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  1398. FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG2) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
  1399. arg = SLJIT_MEM | TMP_REG2;
  1400. argw = 0;
  1401. }
  1402. if ((arg & REG_MASK) && (argw & 0x3) == 0) {
  1403. if (!(argw & ~0x3fc))
  1404. return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
  1405. if (!(-argw & ~0x3fc))
  1406. return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
  1407. }
  1408. /* Slow cases */
  1409. SLJIT_ASSERT(!(arg & OFFS_REG_MASK));
  1410. if (compiler->cache_arg == arg) {
  1411. tmp = argw - compiler->cache_argw;
  1412. if (!(tmp & ~0x3fc))
  1413. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg) | (tmp >> 2));
  1414. if (!(-tmp & ~0x3fc))
  1415. return push_inst32(compiler, inst | RN4(TMP_REG3) | DD4(reg) | (-tmp >> 2));
  1416. if (emit_set_delta(compiler, TMP_REG3, TMP_REG3, tmp) != SLJIT_ERR_UNSUPPORTED) {
  1417. FAIL_IF(compiler->error);
  1418. compiler->cache_argw = argw;
  1419. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg));
  1420. }
  1421. }
  1422. if (arg & REG_MASK) {
  1423. if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
  1424. FAIL_IF(compiler->error);
  1425. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
  1426. }
  1427. imm = get_imm(argw & ~0x3fc);
  1428. if (imm != INVALID_IMM) {
  1429. FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
  1430. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
  1431. }
  1432. imm = get_imm(-argw & ~0x3fc);
  1433. if (imm != INVALID_IMM) {
  1434. argw = -argw;
  1435. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
  1436. return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
  1437. }
  1438. }
  1439. compiler->cache_arg = arg;
  1440. compiler->cache_argw = argw;
  1441. FAIL_IF(load_immediate(compiler, TMP_REG3, argw));
  1442. if (arg & REG_MASK)
  1443. FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG3, (arg & REG_MASK))));
  1444. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG3) | DD4(reg));
  1445. }
  1446. static SLJIT_INLINE sljit_si sljit_emit_fop1_convw_fromd(struct sljit_compiler *compiler, sljit_si op,
  1447. sljit_si dst, sljit_sw dstw,
  1448. sljit_si src, sljit_sw srcw)
  1449. {
  1450. if (src & SLJIT_MEM) {
  1451. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
  1452. src = TMP_FREG1;
  1453. }
  1454. FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_SINGLE_OP) | DD4(TMP_FREG1) | DM4(src)));
  1455. if (dst == SLJIT_UNUSED)
  1456. return SLJIT_SUCCESS;
  1457. if (FAST_IS_REG(dst))
  1458. return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
  1459. /* Store the integer value from a VFP register. */
  1460. return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
  1461. }
  1462. static SLJIT_INLINE sljit_si sljit_emit_fop1_convd_fromw(struct sljit_compiler *compiler, sljit_si op,
  1463. sljit_si dst, sljit_sw dstw,
  1464. sljit_si src, sljit_sw srcw)
  1465. {
  1466. sljit_si dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1467. if (FAST_IS_REG(src))
  1468. FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
  1469. else if (src & SLJIT_MEM) {
  1470. /* Load the integer value into a VFP register. */
  1471. FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
  1472. }
  1473. else {
  1474. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  1475. FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
  1476. }
  1477. FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
  1478. if (dst & SLJIT_MEM)
  1479. return emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP), TMP_FREG1, dst, dstw);
  1480. return SLJIT_SUCCESS;
  1481. }
  1482. static SLJIT_INLINE sljit_si sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_si op,
  1483. sljit_si src1, sljit_sw src1w,
  1484. sljit_si src2, sljit_sw src2w)
  1485. {
  1486. if (src1 & SLJIT_MEM) {
  1487. emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
  1488. src1 = TMP_FREG1;
  1489. }
  1490. if (src2 & SLJIT_MEM) {
  1491. emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
  1492. src2 = TMP_FREG2;
  1493. }
  1494. FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_SINGLE_OP) | DD4(src1) | DM4(src2)));
  1495. return push_inst32(compiler, VMRS);
  1496. }
  1497. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
  1498. sljit_si dst, sljit_sw dstw,
  1499. sljit_si src, sljit_sw srcw)
  1500. {
  1501. sljit_si dst_r;
  1502. CHECK_ERROR();
  1503. compiler->cache_arg = 0;
  1504. compiler->cache_argw = 0;
  1505. if (GET_OPCODE(op) != SLJIT_CONVD_FROMS)
  1506. op ^= SLJIT_SINGLE_OP;
  1507. SLJIT_COMPILE_ASSERT((SLJIT_SINGLE_OP == 0x100), float_transfer_bit_error);
  1508. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1509. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1510. if (src & SLJIT_MEM) {
  1511. emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, dst_r, src, srcw);
  1512. src = dst_r;
  1513. }
  1514. switch (GET_OPCODE(op)) {
  1515. case SLJIT_DMOV:
  1516. if (src != dst_r) {
  1517. if (dst_r != TMP_FREG1)
  1518. FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
  1519. else
  1520. dst_r = src;
  1521. }
  1522. break;
  1523. case SLJIT_DNEG:
  1524. FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
  1525. break;
  1526. case SLJIT_DABS:
  1527. FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
  1528. break;
  1529. case SLJIT_CONVD_FROMS:
  1530. FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DM4(src)));
  1531. op ^= SLJIT_SINGLE_OP;
  1532. break;
  1533. }
  1534. if (dst & SLJIT_MEM)
  1535. return emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP), dst_r, dst, dstw);
  1536. return SLJIT_SUCCESS;
  1537. }
  1538. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
  1539. sljit_si dst, sljit_sw dstw,
  1540. sljit_si src1, sljit_sw src1w,
  1541. sljit_si src2, sljit_sw src2w)
  1542. {
  1543. sljit_si dst_r;
  1544. CHECK_ERROR();
  1545. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1546. ADJUST_LOCAL_OFFSET(dst, dstw);
  1547. ADJUST_LOCAL_OFFSET(src1, src1w);
  1548. ADJUST_LOCAL_OFFSET(src2, src2w);
  1549. compiler->cache_arg = 0;
  1550. compiler->cache_argw = 0;
  1551. op ^= SLJIT_SINGLE_OP;
  1552. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1553. if (src1 & SLJIT_MEM) {
  1554. emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
  1555. src1 = TMP_FREG1;
  1556. }
  1557. if (src2 & SLJIT_MEM) {
  1558. emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
  1559. src2 = TMP_FREG2;
  1560. }
  1561. switch (GET_OPCODE(op)) {
  1562. case SLJIT_DADD:
  1563. FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1564. break;
  1565. case SLJIT_DSUB:
  1566. FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1567. break;
  1568. case SLJIT_DMUL:
  1569. FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1570. break;
  1571. case SLJIT_DDIV:
  1572. FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_SINGLE_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1573. break;
  1574. }
  1575. if (!(dst & SLJIT_MEM))
  1576. return SLJIT_SUCCESS;
  1577. return emit_fop_mem(compiler, (op & SLJIT_SINGLE_OP), TMP_FREG1, dst, dstw);
  1578. }
  1579. #undef FPU_LOAD
  1580. /* --------------------------------------------------------------------- */
  1581. /* Other instructions */
  1582. /* --------------------------------------------------------------------- */
  1583. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw)
  1584. {
  1585. CHECK_ERROR();
  1586. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1587. ADJUST_LOCAL_OFFSET(dst, dstw);
  1588. /* For UNUSED dst. Uncommon, but possible. */
  1589. if (dst == SLJIT_UNUSED)
  1590. return SLJIT_SUCCESS;
  1591. if (FAST_IS_REG(dst))
  1592. return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG3));
  1593. /* Memory. */
  1594. if (getput_arg_fast(compiler, WORD_SIZE | STORE, TMP_REG3, dst, dstw))
  1595. return compiler->error;
  1596. /* TMP_REG3 is used for caching. */
  1597. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, TMP_REG3)));
  1598. compiler->cache_arg = 0;
  1599. compiler->cache_argw = 0;
  1600. return getput_arg(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, 0, 0);
  1601. }
  1602. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw)
  1603. {
  1604. CHECK_ERROR();
  1605. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1606. ADJUST_LOCAL_OFFSET(src, srcw);
  1607. if (FAST_IS_REG(src))
  1608. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG3, src)));
  1609. else if (src & SLJIT_MEM) {
  1610. if (getput_arg_fast(compiler, WORD_SIZE, TMP_REG3, src, srcw))
  1611. FAIL_IF(compiler->error);
  1612. else {
  1613. compiler->cache_arg = 0;
  1614. compiler->cache_argw = 0;
  1615. FAIL_IF(getput_arg(compiler, WORD_SIZE, TMP_REG2, src, srcw, 0, 0));
  1616. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG3, TMP_REG2)));
  1617. }
  1618. }
  1619. else if (src & SLJIT_IMM)
  1620. FAIL_IF(load_immediate(compiler, TMP_REG3, srcw));
  1621. return push_inst16(compiler, BLX | RN3(TMP_REG3));
  1622. }
  1623. /* --------------------------------------------------------------------- */
  1624. /* Conditional instructions */
  1625. /* --------------------------------------------------------------------- */
  1626. static sljit_uw get_cc(sljit_si type)
  1627. {
  1628. switch (type) {
  1629. case SLJIT_EQUAL:
  1630. case SLJIT_MUL_NOT_OVERFLOW:
  1631. case SLJIT_D_EQUAL:
  1632. return 0x0;
  1633. case SLJIT_NOT_EQUAL:
  1634. case SLJIT_MUL_OVERFLOW:
  1635. case SLJIT_D_NOT_EQUAL:
  1636. return 0x1;
  1637. case SLJIT_LESS:
  1638. case SLJIT_D_LESS:
  1639. return 0x3;
  1640. case SLJIT_GREATER_EQUAL:
  1641. case SLJIT_D_GREATER_EQUAL:
  1642. return 0x2;
  1643. case SLJIT_GREATER:
  1644. case SLJIT_D_GREATER:
  1645. return 0x8;
  1646. case SLJIT_LESS_EQUAL:
  1647. case SLJIT_D_LESS_EQUAL:
  1648. return 0x9;
  1649. case SLJIT_SIG_LESS:
  1650. return 0xb;
  1651. case SLJIT_SIG_GREATER_EQUAL:
  1652. return 0xa;
  1653. case SLJIT_SIG_GREATER:
  1654. return 0xc;
  1655. case SLJIT_SIG_LESS_EQUAL:
  1656. return 0xd;
  1657. case SLJIT_OVERFLOW:
  1658. case SLJIT_D_UNORDERED:
  1659. return 0x6;
  1660. case SLJIT_NOT_OVERFLOW:
  1661. case SLJIT_D_ORDERED:
  1662. return 0x7;
  1663. default: /* SLJIT_JUMP */
  1664. SLJIT_ASSERT_STOP();
  1665. return 0xe;
  1666. }
  1667. }
  1668. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1669. {
  1670. struct sljit_label *label;
  1671. CHECK_ERROR_PTR();
  1672. CHECK_PTR(check_sljit_emit_label(compiler));
  1673. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1674. return compiler->last_label;
  1675. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1676. PTR_FAIL_IF(!label);
  1677. set_label(label, compiler);
  1678. return label;
  1679. }
  1680. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type)
  1681. {
  1682. struct sljit_jump *jump;
  1683. sljit_ins cc;
  1684. CHECK_ERROR_PTR();
  1685. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1686. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1687. PTR_FAIL_IF(!jump);
  1688. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1689. type &= 0xff;
  1690. /* In ARM, we don't need to touch the arguments. */
  1691. PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
  1692. if (type < SLJIT_JUMP) {
  1693. jump->flags |= IS_COND;
  1694. cc = get_cc(type);
  1695. jump->flags |= cc << 8;
  1696. PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1697. }
  1698. jump->addr = compiler->size;
  1699. if (type <= SLJIT_JUMP)
  1700. PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
  1701. else {
  1702. jump->flags |= IS_BL;
  1703. PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
  1704. }
  1705. return jump;
  1706. }
  1707. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw)
  1708. {
  1709. struct sljit_jump *jump;
  1710. CHECK_ERROR();
  1711. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  1712. ADJUST_LOCAL_OFFSET(src, srcw);
  1713. /* In ARM, we don't need to touch the arguments. */
  1714. if (!(src & SLJIT_IMM)) {
  1715. if (FAST_IS_REG(src))
  1716. return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
  1717. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw));
  1718. if (type >= SLJIT_FAST_CALL)
  1719. return push_inst16(compiler, BLX | RN3(TMP_REG1));
  1720. }
  1721. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1722. FAIL_IF(!jump);
  1723. set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
  1724. jump->u.target = srcw;
  1725. FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
  1726. jump->addr = compiler->size;
  1727. return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
  1728. }
  1729. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
  1730. sljit_si dst, sljit_sw dstw,
  1731. sljit_si src, sljit_sw srcw,
  1732. sljit_si type)
  1733. {
  1734. sljit_si dst_r, flags = GET_ALL_FLAGS(op);
  1735. sljit_ins cc, ins;
  1736. CHECK_ERROR();
  1737. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, src, srcw, type));
  1738. ADJUST_LOCAL_OFFSET(dst, dstw);
  1739. ADJUST_LOCAL_OFFSET(src, srcw);
  1740. if (dst == SLJIT_UNUSED)
  1741. return SLJIT_SUCCESS;
  1742. op = GET_OPCODE(op);
  1743. cc = get_cc(type & 0xff);
  1744. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG2;
  1745. if (op < SLJIT_ADD) {
  1746. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
  1747. if (reg_map[dst_r] > 7) {
  1748. FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
  1749. FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
  1750. } else {
  1751. FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
  1752. FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
  1753. }
  1754. if (dst_r != TMP_REG2)
  1755. return SLJIT_SUCCESS;
  1756. return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw);
  1757. }
  1758. ins = (op == SLJIT_AND ? ANDI : (op == SLJIT_OR ? ORRI : EORI));
  1759. if ((op == SLJIT_OR || op == SLJIT_XOR) && FAST_IS_REG(dst) && dst == src) {
  1760. /* Does not change the other bits. */
  1761. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1762. FAIL_IF(push_inst32(compiler, ins | RN4(src) | RD4(dst) | 1));
  1763. if (flags & SLJIT_SET_E) {
  1764. /* The condition must always be set, even if the ORRI/EORI is not executed above. */
  1765. if (reg_map[dst] <= 7)
  1766. return push_inst16(compiler, MOVS | RD3(TMP_REG1) | RN3(dst));
  1767. return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst));
  1768. }
  1769. return SLJIT_SUCCESS;
  1770. }
  1771. compiler->cache_arg = 0;
  1772. compiler->cache_argw = 0;
  1773. if (src & SLJIT_MEM) {
  1774. FAIL_IF(emit_op_mem2(compiler, WORD_SIZE, TMP_REG2, src, srcw, dst, dstw));
  1775. src = TMP_REG2;
  1776. srcw = 0;
  1777. } else if (src & SLJIT_IMM) {
  1778. FAIL_IF(load_immediate(compiler, TMP_REG2, srcw));
  1779. src = TMP_REG2;
  1780. srcw = 0;
  1781. }
  1782. if (op == SLJIT_AND || src != dst_r) {
  1783. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
  1784. FAIL_IF(push_inst32(compiler, ins | RN4(src) | RD4(dst_r) | 1));
  1785. FAIL_IF(push_inst32(compiler, ins | RN4(src) | RD4(dst_r) | 0));
  1786. }
  1787. else {
  1788. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1789. FAIL_IF(push_inst32(compiler, ins | RN4(src) | RD4(dst_r) | 1));
  1790. }
  1791. if (dst_r == TMP_REG2)
  1792. FAIL_IF(emit_op_mem2(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, 0, 0));
  1793. if (flags & SLJIT_SET_E) {
  1794. /* The condition must always be set, even if the ORR/EORI is not executed above. */
  1795. if (reg_map[dst_r] <= 7)
  1796. return push_inst16(compiler, MOVS | RD3(TMP_REG1) | RN3(dst_r));
  1797. return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
  1798. }
  1799. return SLJIT_SUCCESS;
  1800. }
  1801. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value)
  1802. {
  1803. struct sljit_const *const_;
  1804. sljit_si dst_r;
  1805. CHECK_ERROR_PTR();
  1806. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  1807. ADJUST_LOCAL_OFFSET(dst, dstw);
  1808. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  1809. PTR_FAIL_IF(!const_);
  1810. set_const(const_, compiler);
  1811. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
  1812. PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
  1813. if (dst & SLJIT_MEM)
  1814. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw));
  1815. return const_;
  1816. }
  1817. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr)
  1818. {
  1819. sljit_uh *inst = (sljit_uh*)addr;
  1820. modify_imm32_const(inst, new_addr);
  1821. SLJIT_CACHE_FLUSH(inst, inst + 4);
  1822. }
  1823. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant)
  1824. {
  1825. sljit_uh *inst = (sljit_uh*)addr;
  1826. modify_imm32_const(inst, new_constant);
  1827. SLJIT_CACHE_FLUSH(inst, inst + 4);
  1828. }