sljitNativeARM_T2_32.c 71 KB

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