sljitNativeARM_T2_32.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370
  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_uw next_addr;
  339. sljit_sw executable_offset;
  340. struct sljit_label *label;
  341. struct sljit_jump *jump;
  342. struct sljit_const *const_;
  343. struct sljit_put_label *put_label;
  344. CHECK_ERROR_PTR();
  345. CHECK_PTR(check_sljit_generate_code(compiler));
  346. reverse_buf(compiler);
  347. code = (sljit_u16*)SLJIT_MALLOC_EXEC(compiler->size * sizeof(sljit_u16));
  348. PTR_FAIL_WITH_EXEC_IF(code);
  349. buf = compiler->buf;
  350. code_ptr = code;
  351. half_count = 0;
  352. next_addr = 0;
  353. executable_offset = SLJIT_EXEC_OFFSET(code);
  354. label = compiler->labels;
  355. jump = compiler->jumps;
  356. const_ = compiler->consts;
  357. put_label = compiler->put_labels;
  358. do {
  359. buf_ptr = (sljit_u16*)buf->memory;
  360. buf_end = buf_ptr + (buf->used_size >> 1);
  361. do {
  362. *code_ptr = *buf_ptr++;
  363. if (next_addr == half_count) {
  364. SLJIT_ASSERT(!label || label->size >= half_count);
  365. SLJIT_ASSERT(!jump || jump->addr >= half_count);
  366. SLJIT_ASSERT(!const_ || const_->addr >= half_count);
  367. SLJIT_ASSERT(!put_label || put_label->addr >= half_count);
  368. /* These structures are ordered by their address. */
  369. if (label && label->size == half_count) {
  370. label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
  371. label->size = code_ptr - code;
  372. label = label->next;
  373. }
  374. if (jump && jump->addr == half_count) {
  375. jump->addr = (sljit_uw)code_ptr - ((jump->flags & IS_COND) ? 10 : 8);
  376. code_ptr -= detect_jump_type(jump, code_ptr, code, executable_offset);
  377. jump = jump->next;
  378. }
  379. if (const_ && const_->addr == half_count) {
  380. const_->addr = (sljit_uw)code_ptr;
  381. const_ = const_->next;
  382. }
  383. if (put_label && put_label->addr == half_count) {
  384. SLJIT_ASSERT(put_label->label);
  385. put_label->addr = (sljit_uw)code_ptr;
  386. put_label = put_label->next;
  387. }
  388. next_addr = compute_next_addr(label, jump, const_, put_label);
  389. }
  390. code_ptr ++;
  391. half_count ++;
  392. } while (buf_ptr < buf_end);
  393. buf = buf->next;
  394. } while (buf);
  395. if (label && label->size == half_count) {
  396. label->addr = ((sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset)) | 0x1;
  397. label->size = code_ptr - code;
  398. label = label->next;
  399. }
  400. SLJIT_ASSERT(!label);
  401. SLJIT_ASSERT(!jump);
  402. SLJIT_ASSERT(!const_);
  403. SLJIT_ASSERT(!put_label);
  404. SLJIT_ASSERT(code_ptr - code <= (sljit_sw)compiler->size);
  405. jump = compiler->jumps;
  406. while (jump) {
  407. set_jump_instruction(jump, executable_offset);
  408. jump = jump->next;
  409. }
  410. put_label = compiler->put_labels;
  411. while (put_label) {
  412. modify_imm32_const((sljit_u16 *)put_label->addr, put_label->label->addr);
  413. put_label = put_label->next;
  414. }
  415. compiler->error = SLJIT_ERR_COMPILED;
  416. compiler->executable_offset = executable_offset;
  417. compiler->executable_size = (code_ptr - code) * sizeof(sljit_u16);
  418. code = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  419. code_ptr = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  420. SLJIT_CACHE_FLUSH(code, code_ptr);
  421. /* Set thumb mode flag. */
  422. return (void*)((sljit_uw)code | 0x1);
  423. }
  424. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
  425. {
  426. switch (feature_type) {
  427. case SLJIT_HAS_FPU:
  428. #ifdef SLJIT_IS_FPU_AVAILABLE
  429. return SLJIT_IS_FPU_AVAILABLE;
  430. #else
  431. /* Available by default. */
  432. return 1;
  433. #endif
  434. case SLJIT_HAS_CLZ:
  435. case SLJIT_HAS_CMOV:
  436. return 1;
  437. default:
  438. return 0;
  439. }
  440. }
  441. /* --------------------------------------------------------------------- */
  442. /* Core code generator functions. */
  443. /* --------------------------------------------------------------------- */
  444. #define INVALID_IMM 0x80000000
  445. static sljit_uw get_imm(sljit_uw imm)
  446. {
  447. /* Thumb immediate form. */
  448. sljit_s32 counter;
  449. if (imm <= 0xff)
  450. return imm;
  451. if ((imm & 0xffff) == (imm >> 16)) {
  452. /* Some special cases. */
  453. if (!(imm & 0xff00))
  454. return (1 << 12) | (imm & 0xff);
  455. if (!(imm & 0xff))
  456. return (2 << 12) | ((imm >> 8) & 0xff);
  457. if ((imm & 0xff00) == ((imm & 0xff) << 8))
  458. return (3 << 12) | (imm & 0xff);
  459. }
  460. /* Assembly optimization: count leading zeroes? */
  461. counter = 8;
  462. if (!(imm & 0xffff0000)) {
  463. counter += 16;
  464. imm <<= 16;
  465. }
  466. if (!(imm & 0xff000000)) {
  467. counter += 8;
  468. imm <<= 8;
  469. }
  470. if (!(imm & 0xf0000000)) {
  471. counter += 4;
  472. imm <<= 4;
  473. }
  474. if (!(imm & 0xc0000000)) {
  475. counter += 2;
  476. imm <<= 2;
  477. }
  478. if (!(imm & 0x80000000)) {
  479. counter += 1;
  480. imm <<= 1;
  481. }
  482. /* Since imm >= 128, this must be true. */
  483. SLJIT_ASSERT(counter <= 31);
  484. if (imm & 0x00ffffff)
  485. return INVALID_IMM; /* Cannot be encoded. */
  486. return ((imm >> 24) & 0x7f) | COPY_BITS(counter, 4, 26, 1) | COPY_BITS(counter, 1, 12, 3) | COPY_BITS(counter, 0, 7, 1);
  487. }
  488. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 dst, sljit_uw imm)
  489. {
  490. sljit_uw tmp;
  491. /* MOVS cannot be used since it destroy flags. */
  492. if (imm >= 0x10000) {
  493. tmp = get_imm(imm);
  494. if (tmp != INVALID_IMM)
  495. return push_inst32(compiler, MOV_WI | RD4(dst) | tmp);
  496. tmp = get_imm(~imm);
  497. if (tmp != INVALID_IMM)
  498. return push_inst32(compiler, MVN_WI | RD4(dst) | tmp);
  499. }
  500. /* set low 16 bits, set hi 16 bits to 0. */
  501. FAIL_IF(push_inst32(compiler, MOVW | RD4(dst)
  502. | COPY_BITS(imm, 12, 16, 4) | COPY_BITS(imm, 11, 26, 1) | COPY_BITS(imm, 8, 12, 3) | (imm & 0xff)));
  503. /* set hi 16 bit if needed. */
  504. if (imm >= 0x10000)
  505. return push_inst32(compiler, MOVT | RD4(dst)
  506. | COPY_BITS(imm, 12 + 16, 16, 4) | COPY_BITS(imm, 11 + 16, 26, 1) | COPY_BITS(imm, 8 + 16, 12, 3) | ((imm & 0xff0000) >> 16));
  507. return SLJIT_SUCCESS;
  508. }
  509. #define ARG1_IMM 0x0010000
  510. #define ARG2_IMM 0x0020000
  511. /* SET_FLAGS must be 0x100000 as it is also the value of S bit (can be used for optimization). */
  512. #define SET_FLAGS 0x0100000
  513. #define UNUSED_RETURN 0x0200000
  514. static sljit_s32 emit_op_imm(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 dst, sljit_uw arg1, sljit_uw arg2)
  515. {
  516. /* dst must be register, TMP_REG1
  517. arg1 must be register, imm
  518. arg2 must be register, imm */
  519. sljit_s32 reg;
  520. sljit_uw imm, nimm;
  521. if (SLJIT_UNLIKELY((flags & (ARG1_IMM | ARG2_IMM)) == (ARG1_IMM | ARG2_IMM))) {
  522. /* Both are immediates, no temporaries are used. */
  523. flags &= ~ARG1_IMM;
  524. FAIL_IF(load_immediate(compiler, TMP_REG1, arg1));
  525. arg1 = TMP_REG1;
  526. }
  527. if (flags & (ARG1_IMM | ARG2_IMM)) {
  528. reg = (flags & ARG2_IMM) ? arg1 : arg2;
  529. imm = (flags & ARG2_IMM) ? arg2 : arg1;
  530. switch (flags & 0xffff) {
  531. case SLJIT_CLZ:
  532. case SLJIT_MUL:
  533. /* No form with immediate operand. */
  534. break;
  535. case SLJIT_MOV:
  536. SLJIT_ASSERT(!(flags & SET_FLAGS) && (flags & ARG2_IMM) && arg1 == TMP_REG2);
  537. return load_immediate(compiler, dst, imm);
  538. case SLJIT_NOT:
  539. if (!(flags & SET_FLAGS))
  540. return load_immediate(compiler, dst, ~imm);
  541. /* Since the flags should be set, we just fallback to the register mode.
  542. Although some clever things could be done here, "NOT IMM" does not worth the efforts. */
  543. break;
  544. case SLJIT_ADD:
  545. nimm = -imm;
  546. if (IS_2_LO_REGS(reg, dst)) {
  547. if (imm <= 0x7)
  548. return push_inst16(compiler, ADDSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  549. if (nimm <= 0x7)
  550. return push_inst16(compiler, SUBSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  551. if (reg == dst) {
  552. if (imm <= 0xff)
  553. return push_inst16(compiler, ADDSI8 | IMM8(imm) | RDN3(dst));
  554. if (nimm <= 0xff)
  555. return push_inst16(compiler, SUBSI8 | IMM8(nimm) | RDN3(dst));
  556. }
  557. }
  558. if (!(flags & SET_FLAGS)) {
  559. if (imm <= 0xfff)
  560. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(imm));
  561. if (nimm <= 0xfff)
  562. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  563. }
  564. nimm = get_imm(imm);
  565. if (nimm != INVALID_IMM)
  566. return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  567. nimm = get_imm(-imm);
  568. if (nimm != INVALID_IMM)
  569. return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  570. break;
  571. case SLJIT_ADDC:
  572. imm = get_imm(imm);
  573. if (imm != INVALID_IMM)
  574. return push_inst32(compiler, ADCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  575. break;
  576. case SLJIT_SUB:
  577. /* SUB operation can be replaced by ADD because of the negative carry flag. */
  578. if (flags & ARG1_IMM) {
  579. if (imm == 0 && IS_2_LO_REGS(reg, dst))
  580. return push_inst16(compiler, RSBSI | RD3(dst) | RN3(reg));
  581. imm = get_imm(imm);
  582. if (imm != INVALID_IMM)
  583. return push_inst32(compiler, RSB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  584. break;
  585. }
  586. if (flags & UNUSED_RETURN) {
  587. if (imm <= 0xff && reg_map[reg] <= 7)
  588. return push_inst16(compiler, CMPI | IMM8(imm) | RDN3(reg));
  589. nimm = get_imm(imm);
  590. if (nimm != INVALID_IMM)
  591. return push_inst32(compiler, CMPI_W | RN4(reg) | nimm);
  592. nimm = get_imm(-imm);
  593. if (nimm != INVALID_IMM)
  594. return push_inst32(compiler, CMNI_W | RN4(reg) | nimm);
  595. }
  596. nimm = -imm;
  597. if (IS_2_LO_REGS(reg, dst)) {
  598. if (imm <= 0x7)
  599. return push_inst16(compiler, SUBSI3 | IMM3(imm) | RD3(dst) | RN3(reg));
  600. if (nimm <= 0x7)
  601. return push_inst16(compiler, ADDSI3 | IMM3(nimm) | RD3(dst) | RN3(reg));
  602. if (reg == dst) {
  603. if (imm <= 0xff)
  604. return push_inst16(compiler, SUBSI8 | IMM8(imm) | RDN3(dst));
  605. if (nimm <= 0xff)
  606. return push_inst16(compiler, ADDSI8 | IMM8(nimm) | RDN3(dst));
  607. }
  608. }
  609. if (!(flags & SET_FLAGS)) {
  610. if (imm <= 0xfff)
  611. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(imm));
  612. if (nimm <= 0xfff)
  613. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(nimm));
  614. }
  615. nimm = get_imm(imm);
  616. if (nimm != INVALID_IMM)
  617. return push_inst32(compiler, SUB_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  618. nimm = get_imm(-imm);
  619. if (nimm != INVALID_IMM)
  620. return push_inst32(compiler, ADD_WI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  621. break;
  622. case SLJIT_SUBC:
  623. if (flags & ARG1_IMM)
  624. break;
  625. imm = get_imm(imm);
  626. if (imm != INVALID_IMM)
  627. return push_inst32(compiler, SBCI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  628. break;
  629. case SLJIT_AND:
  630. nimm = get_imm(imm);
  631. if (nimm != INVALID_IMM)
  632. return push_inst32(compiler, ANDI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  633. imm = get_imm(imm);
  634. if (imm != INVALID_IMM)
  635. return push_inst32(compiler, BICI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  636. break;
  637. case SLJIT_OR:
  638. nimm = get_imm(imm);
  639. if (nimm != INVALID_IMM)
  640. return push_inst32(compiler, ORRI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | nimm);
  641. imm = get_imm(imm);
  642. if (imm != INVALID_IMM)
  643. return push_inst32(compiler, ORNI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  644. break;
  645. case SLJIT_XOR:
  646. imm = get_imm(imm);
  647. if (imm != INVALID_IMM)
  648. return push_inst32(compiler, EORI | (flags & SET_FLAGS) | RD4(dst) | RN4(reg) | imm);
  649. break;
  650. case SLJIT_SHL:
  651. case SLJIT_LSHR:
  652. case SLJIT_ASHR:
  653. if (flags & ARG1_IMM)
  654. break;
  655. imm &= 0x1f;
  656. if (imm == 0) {
  657. if (!(flags & SET_FLAGS))
  658. return push_inst16(compiler, MOV | SET_REGS44(dst, reg));
  659. if (IS_2_LO_REGS(dst, reg))
  660. return push_inst16(compiler, MOVS | RD3(dst) | RN3(reg));
  661. return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(dst) | RM4(reg));
  662. }
  663. switch (flags & 0xffff) {
  664. case SLJIT_SHL:
  665. if (IS_2_LO_REGS(dst, reg))
  666. return push_inst16(compiler, LSLSI | RD3(dst) | RN3(reg) | (imm << 6));
  667. return push_inst32(compiler, LSL_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  668. case SLJIT_LSHR:
  669. if (IS_2_LO_REGS(dst, reg))
  670. return push_inst16(compiler, LSRSI | RD3(dst) | RN3(reg) | (imm << 6));
  671. return push_inst32(compiler, LSR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  672. default: /* SLJIT_ASHR */
  673. if (IS_2_LO_REGS(dst, reg))
  674. return push_inst16(compiler, ASRSI | RD3(dst) | RN3(reg) | (imm << 6));
  675. return push_inst32(compiler, ASR_WI | (flags & SET_FLAGS) | RD4(dst) | RM4(reg) | IMM5(imm));
  676. }
  677. default:
  678. SLJIT_UNREACHABLE();
  679. break;
  680. }
  681. if (flags & ARG2_IMM) {
  682. imm = arg2;
  683. arg2 = (arg1 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
  684. FAIL_IF(load_immediate(compiler, arg2, imm));
  685. }
  686. else {
  687. imm = arg1;
  688. arg1 = (arg2 == TMP_REG1) ? TMP_REG2 : TMP_REG1;
  689. FAIL_IF(load_immediate(compiler, arg1, imm));
  690. }
  691. SLJIT_ASSERT(arg1 != arg2);
  692. }
  693. /* Both arguments are registers. */
  694. switch (flags & 0xffff) {
  695. case SLJIT_MOV:
  696. case SLJIT_MOV_U32:
  697. case SLJIT_MOV_S32:
  698. case SLJIT_MOV_P:
  699. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  700. if (dst == arg2)
  701. return SLJIT_SUCCESS;
  702. return push_inst16(compiler, MOV | SET_REGS44(dst, arg2));
  703. case SLJIT_MOV_U8:
  704. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  705. if (IS_2_LO_REGS(dst, arg2))
  706. return push_inst16(compiler, UXTB | RD3(dst) | RN3(arg2));
  707. return push_inst32(compiler, UXTB_W | RD4(dst) | RM4(arg2));
  708. case SLJIT_MOV_S8:
  709. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  710. if (IS_2_LO_REGS(dst, arg2))
  711. return push_inst16(compiler, SXTB | RD3(dst) | RN3(arg2));
  712. return push_inst32(compiler, SXTB_W | RD4(dst) | RM4(arg2));
  713. case SLJIT_MOV_U16:
  714. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  715. if (IS_2_LO_REGS(dst, arg2))
  716. return push_inst16(compiler, UXTH | RD3(dst) | RN3(arg2));
  717. return push_inst32(compiler, UXTH_W | RD4(dst) | RM4(arg2));
  718. case SLJIT_MOV_S16:
  719. SLJIT_ASSERT(!(flags & SET_FLAGS) && arg1 == TMP_REG2);
  720. if (IS_2_LO_REGS(dst, arg2))
  721. return push_inst16(compiler, SXTH | RD3(dst) | RN3(arg2));
  722. return push_inst32(compiler, SXTH_W | RD4(dst) | RM4(arg2));
  723. case SLJIT_NOT:
  724. SLJIT_ASSERT(arg1 == TMP_REG2);
  725. if (IS_2_LO_REGS(dst, arg2))
  726. return push_inst16(compiler, MVNS | RD3(dst) | RN3(arg2));
  727. return push_inst32(compiler, MVN_W | (flags & SET_FLAGS) | RD4(dst) | RM4(arg2));
  728. case SLJIT_CLZ:
  729. SLJIT_ASSERT(arg1 == TMP_REG2);
  730. FAIL_IF(push_inst32(compiler, CLZ | RN4(arg2) | RD4(dst) | RM4(arg2)));
  731. return SLJIT_SUCCESS;
  732. case SLJIT_ADD:
  733. if (IS_3_LO_REGS(dst, arg1, arg2))
  734. return push_inst16(compiler, ADDS | RD3(dst) | RN3(arg1) | RM3(arg2));
  735. if (dst == arg1 && !(flags & SET_FLAGS))
  736. return push_inst16(compiler, ADD | SET_REGS44(dst, arg2));
  737. return push_inst32(compiler, ADD_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  738. case SLJIT_ADDC:
  739. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  740. return push_inst16(compiler, ADCS | RD3(dst) | RN3(arg2));
  741. return push_inst32(compiler, ADC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  742. case SLJIT_SUB:
  743. if (flags & UNUSED_RETURN) {
  744. if (IS_2_LO_REGS(arg1, arg2))
  745. return push_inst16(compiler, CMP | RD3(arg1) | RN3(arg2));
  746. return push_inst16(compiler, CMP_X | SET_REGS44(arg1, arg2));
  747. }
  748. if (IS_3_LO_REGS(dst, arg1, arg2))
  749. return push_inst16(compiler, SUBS | RD3(dst) | RN3(arg1) | RM3(arg2));
  750. return push_inst32(compiler, SUB_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  751. case SLJIT_SUBC:
  752. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  753. return push_inst16(compiler, SBCS | RD3(dst) | RN3(arg2));
  754. return push_inst32(compiler, SBC_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  755. case SLJIT_MUL:
  756. if (!(flags & SET_FLAGS))
  757. return push_inst32(compiler, MUL | RD4(dst) | RN4(arg1) | RM4(arg2));
  758. SLJIT_ASSERT(dst != TMP_REG2);
  759. FAIL_IF(push_inst32(compiler, SMULL | RT4(dst) | RD4(TMP_REG2) | RN4(arg1) | RM4(arg2)));
  760. /* cmp TMP_REG2, dst asr #31. */
  761. return push_inst32(compiler, CMP_W | RN4(TMP_REG2) | 0x70e0 | RM4(dst));
  762. case SLJIT_AND:
  763. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  764. return push_inst16(compiler, ANDS | RD3(dst) | RN3(arg2));
  765. if ((flags & UNUSED_RETURN) && IS_2_LO_REGS(arg1, arg2))
  766. return push_inst16(compiler, TST | RD3(arg1) | RN3(arg2));
  767. return push_inst32(compiler, AND_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  768. case SLJIT_OR:
  769. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  770. return push_inst16(compiler, ORRS | RD3(dst) | RN3(arg2));
  771. return push_inst32(compiler, ORR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  772. case SLJIT_XOR:
  773. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  774. return push_inst16(compiler, EORS | RD3(dst) | RN3(arg2));
  775. return push_inst32(compiler, EOR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  776. case SLJIT_SHL:
  777. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  778. return push_inst16(compiler, LSLS | RD3(dst) | RN3(arg2));
  779. return push_inst32(compiler, LSL_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  780. case SLJIT_LSHR:
  781. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  782. return push_inst16(compiler, LSRS | RD3(dst) | RN3(arg2));
  783. return push_inst32(compiler, LSR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  784. case SLJIT_ASHR:
  785. if (dst == arg1 && IS_2_LO_REGS(dst, arg2))
  786. return push_inst16(compiler, ASRS | RD3(dst) | RN3(arg2));
  787. return push_inst32(compiler, ASR_W | (flags & SET_FLAGS) | RD4(dst) | RN4(arg1) | RM4(arg2));
  788. }
  789. SLJIT_UNREACHABLE();
  790. return SLJIT_SUCCESS;
  791. }
  792. #define STORE 0x01
  793. #define SIGNED 0x02
  794. #define WORD_SIZE 0x00
  795. #define BYTE_SIZE 0x04
  796. #define HALF_SIZE 0x08
  797. #define PRELOAD 0x0c
  798. #define IS_WORD_SIZE(flags) (!(flags & (BYTE_SIZE | HALF_SIZE)))
  799. #define OFFSET_CHECK(imm, shift) (!(argw & ~(imm << shift)))
  800. /*
  801. 1st letter:
  802. w = word
  803. b = byte
  804. h = half
  805. 2nd letter:
  806. s = signed
  807. u = unsigned
  808. 3rd letter:
  809. l = load
  810. s = store
  811. */
  812. static const sljit_ins sljit_mem16[12] = {
  813. /* w u l */ 0x5800 /* ldr */,
  814. /* w u s */ 0x5000 /* str */,
  815. /* w s l */ 0x5800 /* ldr */,
  816. /* w s s */ 0x5000 /* str */,
  817. /* b u l */ 0x5c00 /* ldrb */,
  818. /* b u s */ 0x5400 /* strb */,
  819. /* b s l */ 0x5600 /* ldrsb */,
  820. /* b s s */ 0x5400 /* strb */,
  821. /* h u l */ 0x5a00 /* ldrh */,
  822. /* h u s */ 0x5200 /* strh */,
  823. /* h s l */ 0x5e00 /* ldrsh */,
  824. /* h s s */ 0x5200 /* strh */,
  825. };
  826. static const sljit_ins sljit_mem16_imm5[12] = {
  827. /* w u l */ 0x6800 /* ldr imm5 */,
  828. /* w u s */ 0x6000 /* str imm5 */,
  829. /* w s l */ 0x6800 /* ldr imm5 */,
  830. /* w s s */ 0x6000 /* str imm5 */,
  831. /* b u l */ 0x7800 /* ldrb imm5 */,
  832. /* b u s */ 0x7000 /* strb imm5 */,
  833. /* b s l */ 0x0000 /* not allowed */,
  834. /* b s s */ 0x7000 /* strb imm5 */,
  835. /* h u l */ 0x8800 /* ldrh imm5 */,
  836. /* h u s */ 0x8000 /* strh imm5 */,
  837. /* h s l */ 0x0000 /* not allowed */,
  838. /* h s s */ 0x8000 /* strh imm5 */,
  839. };
  840. #define MEM_IMM8 0xc00
  841. #define MEM_IMM12 0x800000
  842. static const sljit_ins sljit_mem32[13] = {
  843. /* w u l */ 0xf8500000 /* ldr.w */,
  844. /* w u s */ 0xf8400000 /* str.w */,
  845. /* w s l */ 0xf8500000 /* ldr.w */,
  846. /* w s s */ 0xf8400000 /* str.w */,
  847. /* b u l */ 0xf8100000 /* ldrb.w */,
  848. /* b u s */ 0xf8000000 /* strb.w */,
  849. /* b s l */ 0xf9100000 /* ldrsb.w */,
  850. /* b s s */ 0xf8000000 /* strb.w */,
  851. /* h u l */ 0xf8300000 /* ldrh.w */,
  852. /* h u s */ 0xf8200000 /* strsh.w */,
  853. /* h s l */ 0xf9300000 /* ldrsh.w */,
  854. /* h s s */ 0xf8200000 /* strsh.w */,
  855. /* p u l */ 0xf8100000 /* pld */,
  856. };
  857. /* Helper function. Dst should be reg + value, using at most 1 instruction, flags does not set. */
  858. static sljit_s32 emit_set_delta(struct sljit_compiler *compiler, sljit_s32 dst, sljit_s32 reg, sljit_sw value)
  859. {
  860. if (value >= 0) {
  861. if (value <= 0xfff)
  862. return push_inst32(compiler, ADDWI | RD4(dst) | RN4(reg) | IMM12(value));
  863. value = get_imm(value);
  864. if (value != INVALID_IMM)
  865. return push_inst32(compiler, ADD_WI | RD4(dst) | RN4(reg) | value);
  866. }
  867. else {
  868. value = -value;
  869. if (value <= 0xfff)
  870. return push_inst32(compiler, SUBWI | RD4(dst) | RN4(reg) | IMM12(value));
  871. value = get_imm(value);
  872. if (value != INVALID_IMM)
  873. return push_inst32(compiler, SUB_WI | RD4(dst) | RN4(reg) | value);
  874. }
  875. return SLJIT_ERR_UNSUPPORTED;
  876. }
  877. static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
  878. sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
  879. {
  880. sljit_s32 other_r;
  881. sljit_uw tmp;
  882. SLJIT_ASSERT(arg & SLJIT_MEM);
  883. SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
  884. arg &= ~SLJIT_MEM;
  885. if (SLJIT_UNLIKELY(!(arg & REG_MASK))) {
  886. tmp = get_imm(argw & ~0xfff);
  887. if (tmp != INVALID_IMM) {
  888. FAIL_IF(push_inst32(compiler, MOV_WI | RD4(tmp_reg) | tmp));
  889. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg) | (argw & 0xfff));
  890. }
  891. FAIL_IF(load_immediate(compiler, tmp_reg, argw));
  892. if (IS_2_LO_REGS(reg, tmp_reg) && sljit_mem16_imm5[flags])
  893. return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(tmp_reg));
  894. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(tmp_reg));
  895. }
  896. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  897. argw &= 0x3;
  898. other_r = OFFS_REG(arg);
  899. arg &= 0xf;
  900. if (!argw && IS_3_LO_REGS(reg, arg, other_r))
  901. return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(other_r));
  902. return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(other_r) | (argw << 4));
  903. }
  904. if (argw > 0xfff) {
  905. tmp = get_imm(argw & ~0xfff);
  906. if (tmp != INVALID_IMM) {
  907. push_inst32(compiler, ADD_WI | RD4(tmp_reg) | RN4(arg) | tmp);
  908. arg = tmp_reg;
  909. argw = argw & 0xfff;
  910. }
  911. }
  912. else if (argw < -0xff) {
  913. tmp = get_imm(-argw & ~0xff);
  914. if (tmp != INVALID_IMM) {
  915. push_inst32(compiler, SUB_WI | RD4(tmp_reg) | RN4(arg) | tmp);
  916. arg = tmp_reg;
  917. argw = -(-argw & 0xff);
  918. }
  919. }
  920. if (IS_2_LO_REGS(reg, arg) && sljit_mem16_imm5[flags]) {
  921. tmp = 3;
  922. if (IS_WORD_SIZE(flags)) {
  923. if (OFFSET_CHECK(0x1f, 2))
  924. tmp = 2;
  925. }
  926. else if (flags & BYTE_SIZE)
  927. {
  928. if (OFFSET_CHECK(0x1f, 0))
  929. tmp = 0;
  930. }
  931. else {
  932. SLJIT_ASSERT(flags & HALF_SIZE);
  933. if (OFFSET_CHECK(0x1f, 1))
  934. tmp = 1;
  935. }
  936. if (tmp < 3)
  937. return push_inst16(compiler, sljit_mem16_imm5[flags] | RD3(reg) | RN3(arg) | (argw << (6 - tmp)));
  938. }
  939. else if (SLJIT_UNLIKELY(arg == SLJIT_SP) && IS_WORD_SIZE(flags) && OFFSET_CHECK(0xff, 2) && reg_map[reg] <= 7) {
  940. /* SP based immediate. */
  941. return push_inst16(compiler, STR_SP | ((flags & STORE) ? 0 : 0x800) | RDN3(reg) | (argw >> 2));
  942. }
  943. if (argw >= 0 && argw <= 0xfff)
  944. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM12 | RT4(reg) | RN4(arg) | argw);
  945. else if (argw < 0 && argw >= -0xff)
  946. return push_inst32(compiler, sljit_mem32[flags] | MEM_IMM8 | RT4(reg) | RN4(arg) | -argw);
  947. SLJIT_ASSERT(arg != tmp_reg);
  948. FAIL_IF(load_immediate(compiler, tmp_reg, argw));
  949. if (IS_3_LO_REGS(reg, arg, tmp_reg))
  950. return push_inst16(compiler, sljit_mem16[flags] | RD3(reg) | RN3(arg) | RM3(tmp_reg));
  951. return push_inst32(compiler, sljit_mem32[flags] | RT4(reg) | RN4(arg) | RM4(tmp_reg));
  952. }
  953. /* --------------------------------------------------------------------- */
  954. /* Entry, exit */
  955. /* --------------------------------------------------------------------- */
  956. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  957. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  958. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  959. {
  960. sljit_s32 args, size, i, tmp;
  961. sljit_ins push = 0;
  962. #ifdef _WIN32
  963. sljit_uw imm;
  964. #endif
  965. CHECK_ERROR();
  966. CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  967. set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  968. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  969. for (i = SLJIT_S0; i >= tmp; i--)
  970. push |= 1 << reg_map[i];
  971. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  972. push |= 1 << reg_map[i];
  973. FAIL_IF((push & 0xff00)
  974. ? push_inst32(compiler, PUSH_W | (1 << 14) | push)
  975. : push_inst16(compiler, PUSH | (1 << 8) | push));
  976. /* Stack must be aligned to 8 bytes: (LR, R4) */
  977. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  978. local_size = ((size + local_size + 7) & ~7) - size;
  979. compiler->local_size = local_size;
  980. #ifdef _WIN32
  981. if (local_size >= 256) {
  982. if (local_size > 4096)
  983. imm = get_imm(4096);
  984. else
  985. imm = get_imm(local_size & ~0xff);
  986. SLJIT_ASSERT(imm != INVALID_IMM);
  987. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(SLJIT_SP) | imm));
  988. }
  989. #else
  990. if (local_size > 0) {
  991. if (local_size <= (127 << 2))
  992. FAIL_IF(push_inst16(compiler, SUB_SP | (local_size >> 2)));
  993. else
  994. FAIL_IF(emit_op_imm(compiler, SLJIT_SUB | ARG2_IMM, SLJIT_SP, SLJIT_SP, local_size));
  995. }
  996. #endif
  997. args = get_arg_count(arg_types);
  998. if (args >= 1)
  999. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S0, SLJIT_R0)));
  1000. if (args >= 2)
  1001. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S1, SLJIT_R1)));
  1002. if (args >= 3)
  1003. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_S2, SLJIT_R2)));
  1004. #ifdef _WIN32
  1005. if (local_size >= 256) {
  1006. if (local_size > 4096) {
  1007. imm = get_imm(4096);
  1008. SLJIT_ASSERT(imm != INVALID_IMM);
  1009. if (local_size < 4 * 4096) {
  1010. if (local_size > 2 * 4096) {
  1011. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
  1012. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
  1013. local_size -= 4096;
  1014. }
  1015. if (local_size > 2 * 4096) {
  1016. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
  1017. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
  1018. local_size -= 4096;
  1019. }
  1020. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
  1021. local_size -= 4096;
  1022. SLJIT_ASSERT(local_size > 0);
  1023. }
  1024. else {
  1025. FAIL_IF(load_immediate(compiler, SLJIT_R3, (local_size >> 12) - 1));
  1026. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
  1027. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
  1028. SLJIT_ASSERT(reg_map[SLJIT_R3] < 7);
  1029. FAIL_IF(push_inst16(compiler, SUBSI8 | RDN3(SLJIT_R3) | 1));
  1030. FAIL_IF(push_inst16(compiler, BCC | (0x1 << 8) /* not-equal */ | (-7 & 0xff)));
  1031. local_size &= 0xfff;
  1032. if (local_size != 0)
  1033. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | RT4(TMP_REG2) | RN4(TMP_REG1)));
  1034. }
  1035. if (local_size >= 256) {
  1036. imm = get_imm(local_size & ~0xff);
  1037. SLJIT_ASSERT(imm != INVALID_IMM);
  1038. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(TMP_REG1) | imm));
  1039. }
  1040. }
  1041. local_size &= 0xff;
  1042. FAIL_IF(push_inst32(compiler, LDRI | 0x400 | (local_size > 0 ? 0x100 : 0) | RT4(TMP_REG2) | RN4(TMP_REG1) | local_size));
  1043. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_SP, TMP_REG1)));
  1044. }
  1045. else if (local_size > 0)
  1046. FAIL_IF(push_inst32(compiler, LDRI | 0x500 | RT4(TMP_REG1) | RN4(SLJIT_SP) | local_size));
  1047. #endif
  1048. return SLJIT_SUCCESS;
  1049. }
  1050. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  1051. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  1052. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  1053. {
  1054. sljit_s32 size;
  1055. CHECK_ERROR();
  1056. CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  1057. set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  1058. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  1059. compiler->local_size = ((size + local_size + 7) & ~7) - size;
  1060. return SLJIT_SUCCESS;
  1061. }
  1062. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  1063. {
  1064. sljit_s32 i, tmp;
  1065. sljit_ins pop = 0;
  1066. CHECK_ERROR();
  1067. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  1068. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  1069. if (compiler->local_size > 0) {
  1070. if (compiler->local_size <= (127 << 2))
  1071. FAIL_IF(push_inst16(compiler, ADD_SP | (compiler->local_size >> 2)));
  1072. else
  1073. FAIL_IF(emit_op_imm(compiler, SLJIT_ADD | ARG2_IMM, SLJIT_SP, SLJIT_SP, compiler->local_size));
  1074. }
  1075. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  1076. for (i = SLJIT_S0; i >= tmp; i--)
  1077. pop |= 1 << reg_map[i];
  1078. for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  1079. pop |= 1 << reg_map[i];
  1080. return (pop & 0xff00)
  1081. ? push_inst32(compiler, POP_W | (1 << 15) | pop)
  1082. : push_inst16(compiler, POP | (1 << 8) | pop);
  1083. }
  1084. /* --------------------------------------------------------------------- */
  1085. /* Operators */
  1086. /* --------------------------------------------------------------------- */
  1087. #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
  1088. #ifdef __cplusplus
  1089. extern "C" {
  1090. #endif
  1091. #ifdef _WIN32
  1092. extern unsigned long long __rt_udiv(unsigned int denominator, unsigned int numerator);
  1093. extern long long __rt_sdiv(int denominator, int numerator);
  1094. #elif defined(__GNUC__)
  1095. extern unsigned int __aeabi_uidivmod(unsigned int numerator, int unsigned denominator);
  1096. extern int __aeabi_idivmod(int numerator, int denominator);
  1097. #else
  1098. #error "Software divmod functions are needed"
  1099. #endif
  1100. #ifdef __cplusplus
  1101. }
  1102. #endif
  1103. #endif /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
  1104. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
  1105. {
  1106. #if !(defined __ARM_FEATURE_IDIV) && !(defined __ARM_ARCH_EXT_IDIV__)
  1107. sljit_sw saved_reg_list[3];
  1108. sljit_sw saved_reg_count;
  1109. #endif
  1110. CHECK_ERROR();
  1111. CHECK(check_sljit_emit_op0(compiler, op));
  1112. op = GET_OPCODE(op);
  1113. switch (op) {
  1114. case SLJIT_BREAKPOINT:
  1115. return push_inst16(compiler, BKPT);
  1116. case SLJIT_NOP:
  1117. return push_inst16(compiler, NOP);
  1118. case SLJIT_LMUL_UW:
  1119. case SLJIT_LMUL_SW:
  1120. return push_inst32(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
  1121. | (reg_map[SLJIT_R1] << 8)
  1122. | (reg_map[SLJIT_R0] << 12)
  1123. | (reg_map[SLJIT_R0] << 16)
  1124. | reg_map[SLJIT_R1]);
  1125. #if (defined __ARM_FEATURE_IDIV) || (defined __ARM_ARCH_EXT_IDIV__)
  1126. case SLJIT_DIVMOD_UW:
  1127. case SLJIT_DIVMOD_SW:
  1128. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
  1129. FAIL_IF(push_inst32(compiler, (op == SLJIT_DIVMOD_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
  1130. FAIL_IF(push_inst32(compiler, MUL | RD4(SLJIT_R1) | RN4(SLJIT_R0) | RM4(SLJIT_R1)));
  1131. return push_inst32(compiler, SUB_W | RD4(SLJIT_R1) | RN4(TMP_REG1) | RM4(SLJIT_R1));
  1132. case SLJIT_DIV_UW:
  1133. case SLJIT_DIV_SW:
  1134. return push_inst32(compiler, (op == SLJIT_DIV_UW ? UDIV : SDIV) | RD4(SLJIT_R0) | RN4(SLJIT_R0) | RM4(SLJIT_R1));
  1135. #else /* !__ARM_FEATURE_IDIV && !__ARM_ARCH_EXT_IDIV__ */
  1136. case SLJIT_DIVMOD_UW:
  1137. case SLJIT_DIVMOD_SW:
  1138. case SLJIT_DIV_UW:
  1139. case SLJIT_DIV_SW:
  1140. SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
  1141. SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
  1142. saved_reg_count = 0;
  1143. if (compiler->scratches >= 4)
  1144. saved_reg_list[saved_reg_count++] = 3;
  1145. if (compiler->scratches >= 3)
  1146. saved_reg_list[saved_reg_count++] = 2;
  1147. if (op >= SLJIT_DIV_UW)
  1148. saved_reg_list[saved_reg_count++] = 1;
  1149. if (saved_reg_count > 0) {
  1150. FAIL_IF(push_inst32(compiler, 0xf84d0d00 | (saved_reg_count >= 3 ? 16 : 8)
  1151. | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
  1152. if (saved_reg_count >= 2) {
  1153. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1154. FAIL_IF(push_inst16(compiler, 0x9001 | (saved_reg_list[1] << 8) /* str rX, [sp, #4] */));
  1155. }
  1156. if (saved_reg_count >= 3) {
  1157. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1158. FAIL_IF(push_inst16(compiler, 0x9002 | (saved_reg_list[2] << 8) /* str rX, [sp, #8] */));
  1159. }
  1160. }
  1161. #ifdef _WIN32
  1162. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG1, SLJIT_R0)));
  1163. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R0, SLJIT_R1)));
  1164. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(SLJIT_R1, TMP_REG1)));
  1165. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
  1166. ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__rt_udiv) : SLJIT_FUNC_OFFSET(__rt_sdiv))));
  1167. #elif defined(__GNUC__)
  1168. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
  1169. ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
  1170. #else
  1171. #error "Software divmod functions are needed"
  1172. #endif
  1173. if (saved_reg_count > 0) {
  1174. if (saved_reg_count >= 3) {
  1175. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1176. FAIL_IF(push_inst16(compiler, 0x9802 | (saved_reg_list[2] << 8) /* ldr rX, [sp, #8] */));
  1177. }
  1178. if (saved_reg_count >= 2) {
  1179. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1180. FAIL_IF(push_inst16(compiler, 0x9801 | (saved_reg_list[1] << 8) /* ldr rX, [sp, #4] */));
  1181. }
  1182. return push_inst32(compiler, 0xf85d0b00 | (saved_reg_count >= 3 ? 16 : 8)
  1183. | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
  1184. }
  1185. return SLJIT_SUCCESS;
  1186. #endif /* __ARM_FEATURE_IDIV || __ARM_ARCH_EXT_IDIV__ */
  1187. }
  1188. return SLJIT_SUCCESS;
  1189. }
  1190. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
  1191. sljit_s32 dst, sljit_sw dstw,
  1192. sljit_s32 src, sljit_sw srcw)
  1193. {
  1194. sljit_s32 dst_r, flags;
  1195. sljit_s32 op_flags = GET_ALL_FLAGS(op);
  1196. CHECK_ERROR();
  1197. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1198. ADJUST_LOCAL_OFFSET(dst, dstw);
  1199. ADJUST_LOCAL_OFFSET(src, srcw);
  1200. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
  1201. /* Since TMP_PC has index 15, IS_2_LO_REGS and IS_3_LO_REGS checks always fail. */
  1202. if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
  1203. return emit_op_mem(compiler, PRELOAD, TMP_PC, src, srcw, TMP_REG1);
  1204. return SLJIT_SUCCESS;
  1205. }
  1206. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG1;
  1207. op = GET_OPCODE(op);
  1208. if (op >= SLJIT_MOV && op <= SLJIT_MOV_P) {
  1209. switch (op) {
  1210. case SLJIT_MOV:
  1211. case SLJIT_MOV_U32:
  1212. case SLJIT_MOV_S32:
  1213. case SLJIT_MOV_P:
  1214. flags = WORD_SIZE;
  1215. break;
  1216. case SLJIT_MOV_U8:
  1217. flags = BYTE_SIZE;
  1218. if (src & SLJIT_IMM)
  1219. srcw = (sljit_u8)srcw;
  1220. break;
  1221. case SLJIT_MOV_S8:
  1222. flags = BYTE_SIZE | SIGNED;
  1223. if (src & SLJIT_IMM)
  1224. srcw = (sljit_s8)srcw;
  1225. break;
  1226. case SLJIT_MOV_U16:
  1227. flags = HALF_SIZE;
  1228. if (src & SLJIT_IMM)
  1229. srcw = (sljit_u16)srcw;
  1230. break;
  1231. case SLJIT_MOV_S16:
  1232. flags = HALF_SIZE | SIGNED;
  1233. if (src & SLJIT_IMM)
  1234. srcw = (sljit_s16)srcw;
  1235. break;
  1236. default:
  1237. SLJIT_UNREACHABLE();
  1238. flags = 0;
  1239. break;
  1240. }
  1241. if (src & SLJIT_IMM)
  1242. FAIL_IF(emit_op_imm(compiler, SLJIT_MOV | ARG2_IMM, dst_r, TMP_REG2, srcw));
  1243. else if (src & SLJIT_MEM) {
  1244. FAIL_IF(emit_op_mem(compiler, flags, dst_r, src, srcw, TMP_REG1));
  1245. } else {
  1246. if (dst_r != TMP_REG1)
  1247. return emit_op_imm(compiler, op, dst_r, TMP_REG2, src);
  1248. dst_r = src;
  1249. }
  1250. if (!(dst & SLJIT_MEM))
  1251. return SLJIT_SUCCESS;
  1252. return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
  1253. }
  1254. if (op == SLJIT_NEG) {
  1255. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1256. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1257. compiler->skip_checks = 1;
  1258. #endif
  1259. return sljit_emit_op2(compiler, SLJIT_SUB | op_flags, dst, dstw, SLJIT_IMM, 0, src, srcw);
  1260. }
  1261. flags = HAS_FLAGS(op_flags) ? SET_FLAGS : 0;
  1262. if (src & SLJIT_MEM) {
  1263. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
  1264. src = TMP_REG1;
  1265. }
  1266. emit_op_imm(compiler, flags | op, dst_r, TMP_REG2, src);
  1267. if (SLJIT_UNLIKELY(dst & SLJIT_MEM))
  1268. return emit_op_mem(compiler, flags | STORE, dst_r, dst, dstw, TMP_REG2);
  1269. return SLJIT_SUCCESS;
  1270. }
  1271. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
  1272. sljit_s32 dst, sljit_sw dstw,
  1273. sljit_s32 src1, sljit_sw src1w,
  1274. sljit_s32 src2, sljit_sw src2w)
  1275. {
  1276. sljit_s32 dst_reg, flags, src2_reg;
  1277. CHECK_ERROR();
  1278. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1279. ADJUST_LOCAL_OFFSET(dst, dstw);
  1280. ADJUST_LOCAL_OFFSET(src1, src1w);
  1281. ADJUST_LOCAL_OFFSET(src2, src2w);
  1282. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
  1283. return SLJIT_SUCCESS;
  1284. dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG1;
  1285. flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
  1286. if (src1 & SLJIT_IMM)
  1287. flags |= ARG1_IMM;
  1288. else if (src1 & SLJIT_MEM) {
  1289. emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src1, src1w, TMP_REG1);
  1290. src1w = TMP_REG1;
  1291. }
  1292. else
  1293. src1w = src1;
  1294. if (src2 & SLJIT_IMM)
  1295. flags |= ARG2_IMM;
  1296. else if (src2 & SLJIT_MEM) {
  1297. src2_reg = (!(flags & ARG1_IMM) && (src1w == TMP_REG1)) ? TMP_REG2 : TMP_REG1;
  1298. emit_op_mem(compiler, WORD_SIZE, src2_reg, src2, src2w, src2_reg);
  1299. src2w = src2_reg;
  1300. }
  1301. else
  1302. src2w = src2;
  1303. if (dst == SLJIT_UNUSED)
  1304. flags |= UNUSED_RETURN;
  1305. emit_op_imm(compiler, flags | GET_OPCODE(op), dst_reg, src1w, src2w);
  1306. if (!(dst & SLJIT_MEM))
  1307. return SLJIT_SUCCESS;
  1308. return emit_op_mem(compiler, WORD_SIZE | STORE, dst_reg, dst, dstw, TMP_REG2);
  1309. }
  1310. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
  1311. {
  1312. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1313. return reg_map[reg];
  1314. }
  1315. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
  1316. {
  1317. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1318. return (freg_map[reg] << 1);
  1319. }
  1320. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
  1321. void *instruction, sljit_s32 size)
  1322. {
  1323. CHECK_ERROR();
  1324. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1325. if (size == 2)
  1326. return push_inst16(compiler, *(sljit_u16*)instruction);
  1327. return push_inst32(compiler, *(sljit_ins*)instruction);
  1328. }
  1329. /* --------------------------------------------------------------------- */
  1330. /* Floating point operators */
  1331. /* --------------------------------------------------------------------- */
  1332. #define FPU_LOAD (1 << 20)
  1333. static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
  1334. {
  1335. sljit_uw imm;
  1336. sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
  1337. SLJIT_ASSERT(arg & SLJIT_MEM);
  1338. /* Fast loads and stores. */
  1339. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  1340. FAIL_IF(push_inst32(compiler, ADD_W | RD4(TMP_REG1) | RN4(arg & REG_MASK) | RM4(OFFS_REG(arg)) | ((argw & 0x3) << 6)));
  1341. arg = SLJIT_MEM | TMP_REG1;
  1342. argw = 0;
  1343. }
  1344. if ((arg & REG_MASK) && (argw & 0x3) == 0) {
  1345. if (!(argw & ~0x3fc))
  1346. return push_inst32(compiler, inst | 0x800000 | RN4(arg & REG_MASK) | DD4(reg) | (argw >> 2));
  1347. if (!(-argw & ~0x3fc))
  1348. return push_inst32(compiler, inst | RN4(arg & REG_MASK) | DD4(reg) | (-argw >> 2));
  1349. }
  1350. if (arg & REG_MASK) {
  1351. if (emit_set_delta(compiler, TMP_REG1, arg & REG_MASK, argw) != SLJIT_ERR_UNSUPPORTED) {
  1352. FAIL_IF(compiler->error);
  1353. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
  1354. }
  1355. imm = get_imm(argw & ~0x3fc);
  1356. if (imm != INVALID_IMM) {
  1357. FAIL_IF(push_inst32(compiler, ADD_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
  1358. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
  1359. }
  1360. imm = get_imm(-argw & ~0x3fc);
  1361. if (imm != INVALID_IMM) {
  1362. argw = -argw;
  1363. FAIL_IF(push_inst32(compiler, SUB_WI | RD4(TMP_REG1) | RN4(arg & REG_MASK) | imm));
  1364. return push_inst32(compiler, inst | RN4(TMP_REG1) | DD4(reg) | ((argw & 0x3fc) >> 2));
  1365. }
  1366. }
  1367. FAIL_IF(load_immediate(compiler, TMP_REG1, argw));
  1368. if (arg & REG_MASK)
  1369. FAIL_IF(push_inst16(compiler, ADD | SET_REGS44(TMP_REG1, (arg & REG_MASK))));
  1370. return push_inst32(compiler, inst | 0x800000 | RN4(TMP_REG1) | DD4(reg));
  1371. }
  1372. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
  1373. sljit_s32 dst, sljit_sw dstw,
  1374. sljit_s32 src, sljit_sw srcw)
  1375. {
  1376. op ^= SLJIT_F32_OP;
  1377. if (src & SLJIT_MEM) {
  1378. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
  1379. src = TMP_FREG1;
  1380. }
  1381. FAIL_IF(push_inst32(compiler, VCVT_S32_F32 | (op & SLJIT_F32_OP) | DD4(TMP_FREG1) | DM4(src)));
  1382. if (FAST_IS_REG(dst))
  1383. return push_inst32(compiler, VMOV | (1 << 20) | RT4(dst) | DN4(TMP_FREG1));
  1384. /* Store the integer value from a VFP register. */
  1385. return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
  1386. }
  1387. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
  1388. sljit_s32 dst, sljit_sw dstw,
  1389. sljit_s32 src, sljit_sw srcw)
  1390. {
  1391. sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1392. op ^= SLJIT_F32_OP;
  1393. if (FAST_IS_REG(src))
  1394. FAIL_IF(push_inst32(compiler, VMOV | RT4(src) | DN4(TMP_FREG1)));
  1395. else if (src & SLJIT_MEM) {
  1396. /* Load the integer value into a VFP register. */
  1397. FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
  1398. }
  1399. else {
  1400. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  1401. FAIL_IF(push_inst32(compiler, VMOV | RT4(TMP_REG1) | DN4(TMP_FREG1)));
  1402. }
  1403. FAIL_IF(push_inst32(compiler, VCVT_F32_S32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(TMP_FREG1)));
  1404. if (dst & SLJIT_MEM)
  1405. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
  1406. return SLJIT_SUCCESS;
  1407. }
  1408. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
  1409. sljit_s32 src1, sljit_sw src1w,
  1410. sljit_s32 src2, sljit_sw src2w)
  1411. {
  1412. op ^= SLJIT_F32_OP;
  1413. if (src1 & SLJIT_MEM) {
  1414. emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
  1415. src1 = TMP_FREG1;
  1416. }
  1417. if (src2 & SLJIT_MEM) {
  1418. emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
  1419. src2 = TMP_FREG2;
  1420. }
  1421. FAIL_IF(push_inst32(compiler, VCMP_F32 | (op & SLJIT_F32_OP) | DD4(src1) | DM4(src2)));
  1422. return push_inst32(compiler, VMRS);
  1423. }
  1424. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
  1425. sljit_s32 dst, sljit_sw dstw,
  1426. sljit_s32 src, sljit_sw srcw)
  1427. {
  1428. sljit_s32 dst_r;
  1429. CHECK_ERROR();
  1430. SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
  1431. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1432. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1433. if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
  1434. op ^= SLJIT_F32_OP;
  1435. if (src & SLJIT_MEM) {
  1436. emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw);
  1437. src = dst_r;
  1438. }
  1439. switch (GET_OPCODE(op)) {
  1440. case SLJIT_MOV_F64:
  1441. if (src != dst_r) {
  1442. if (dst_r != TMP_FREG1)
  1443. FAIL_IF(push_inst32(compiler, VMOV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
  1444. else
  1445. dst_r = src;
  1446. }
  1447. break;
  1448. case SLJIT_NEG_F64:
  1449. FAIL_IF(push_inst32(compiler, VNEG_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
  1450. break;
  1451. case SLJIT_ABS_F64:
  1452. FAIL_IF(push_inst32(compiler, VABS_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
  1453. break;
  1454. case SLJIT_CONV_F64_FROM_F32:
  1455. FAIL_IF(push_inst32(compiler, VCVT_F64_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DM4(src)));
  1456. op ^= SLJIT_F32_OP;
  1457. break;
  1458. }
  1459. if (dst & SLJIT_MEM)
  1460. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
  1461. return SLJIT_SUCCESS;
  1462. }
  1463. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
  1464. sljit_s32 dst, sljit_sw dstw,
  1465. sljit_s32 src1, sljit_sw src1w,
  1466. sljit_s32 src2, sljit_sw src2w)
  1467. {
  1468. sljit_s32 dst_r;
  1469. CHECK_ERROR();
  1470. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1471. ADJUST_LOCAL_OFFSET(dst, dstw);
  1472. ADJUST_LOCAL_OFFSET(src1, src1w);
  1473. ADJUST_LOCAL_OFFSET(src2, src2w);
  1474. op ^= SLJIT_F32_OP;
  1475. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1476. if (src1 & SLJIT_MEM) {
  1477. emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w);
  1478. src1 = TMP_FREG1;
  1479. }
  1480. if (src2 & SLJIT_MEM) {
  1481. emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w);
  1482. src2 = TMP_FREG2;
  1483. }
  1484. switch (GET_OPCODE(op)) {
  1485. case SLJIT_ADD_F64:
  1486. FAIL_IF(push_inst32(compiler, VADD_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1487. break;
  1488. case SLJIT_SUB_F64:
  1489. FAIL_IF(push_inst32(compiler, VSUB_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1490. break;
  1491. case SLJIT_MUL_F64:
  1492. FAIL_IF(push_inst32(compiler, VMUL_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1493. break;
  1494. case SLJIT_DIV_F64:
  1495. FAIL_IF(push_inst32(compiler, VDIV_F32 | (op & SLJIT_F32_OP) | DD4(dst_r) | DN4(src1) | DM4(src2)));
  1496. break;
  1497. }
  1498. if (!(dst & SLJIT_MEM))
  1499. return SLJIT_SUCCESS;
  1500. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
  1501. }
  1502. #undef FPU_LOAD
  1503. /* --------------------------------------------------------------------- */
  1504. /* Other instructions */
  1505. /* --------------------------------------------------------------------- */
  1506. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  1507. {
  1508. CHECK_ERROR();
  1509. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1510. ADJUST_LOCAL_OFFSET(dst, dstw);
  1511. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1512. if (FAST_IS_REG(dst))
  1513. return push_inst16(compiler, MOV | SET_REGS44(dst, TMP_REG2));
  1514. /* Memory. */
  1515. return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG2, dst, dstw, TMP_REG1);
  1516. }
  1517. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  1518. {
  1519. CHECK_ERROR();
  1520. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1521. ADJUST_LOCAL_OFFSET(src, srcw);
  1522. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1523. if (FAST_IS_REG(src))
  1524. FAIL_IF(push_inst16(compiler, MOV | SET_REGS44(TMP_REG2, src)));
  1525. else
  1526. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, src, srcw, TMP_REG2));
  1527. return push_inst16(compiler, BX | RN3(TMP_REG2));
  1528. }
  1529. /* --------------------------------------------------------------------- */
  1530. /* Conditional instructions */
  1531. /* --------------------------------------------------------------------- */
  1532. static sljit_uw get_cc(sljit_s32 type)
  1533. {
  1534. switch (type) {
  1535. case SLJIT_EQUAL:
  1536. case SLJIT_MUL_NOT_OVERFLOW:
  1537. case SLJIT_EQUAL_F64:
  1538. return 0x0;
  1539. case SLJIT_NOT_EQUAL:
  1540. case SLJIT_MUL_OVERFLOW:
  1541. case SLJIT_NOT_EQUAL_F64:
  1542. return 0x1;
  1543. case SLJIT_LESS:
  1544. case SLJIT_LESS_F64:
  1545. return 0x3;
  1546. case SLJIT_GREATER_EQUAL:
  1547. case SLJIT_GREATER_EQUAL_F64:
  1548. return 0x2;
  1549. case SLJIT_GREATER:
  1550. case SLJIT_GREATER_F64:
  1551. return 0x8;
  1552. case SLJIT_LESS_EQUAL:
  1553. case SLJIT_LESS_EQUAL_F64:
  1554. return 0x9;
  1555. case SLJIT_SIG_LESS:
  1556. return 0xb;
  1557. case SLJIT_SIG_GREATER_EQUAL:
  1558. return 0xa;
  1559. case SLJIT_SIG_GREATER:
  1560. return 0xc;
  1561. case SLJIT_SIG_LESS_EQUAL:
  1562. return 0xd;
  1563. case SLJIT_OVERFLOW:
  1564. case SLJIT_UNORDERED_F64:
  1565. return 0x6;
  1566. case SLJIT_NOT_OVERFLOW:
  1567. case SLJIT_ORDERED_F64:
  1568. return 0x7;
  1569. default: /* SLJIT_JUMP */
  1570. SLJIT_UNREACHABLE();
  1571. return 0xe;
  1572. }
  1573. }
  1574. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1575. {
  1576. struct sljit_label *label;
  1577. CHECK_ERROR_PTR();
  1578. CHECK_PTR(check_sljit_emit_label(compiler));
  1579. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1580. return compiler->last_label;
  1581. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1582. PTR_FAIL_IF(!label);
  1583. set_label(label, compiler);
  1584. return label;
  1585. }
  1586. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
  1587. {
  1588. struct sljit_jump *jump;
  1589. sljit_ins cc;
  1590. CHECK_ERROR_PTR();
  1591. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1592. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1593. PTR_FAIL_IF(!jump);
  1594. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1595. type &= 0xff;
  1596. PTR_FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
  1597. if (type < SLJIT_JUMP) {
  1598. jump->flags |= IS_COND;
  1599. cc = get_cc(type);
  1600. jump->flags |= cc << 8;
  1601. PTR_FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1602. }
  1603. jump->addr = compiler->size;
  1604. if (type <= SLJIT_JUMP)
  1605. PTR_FAIL_IF(push_inst16(compiler, BX | RN3(TMP_REG1)));
  1606. else {
  1607. jump->flags |= IS_BL;
  1608. PTR_FAIL_IF(push_inst16(compiler, BLX | RN3(TMP_REG1)));
  1609. }
  1610. return jump;
  1611. }
  1612. #ifdef __SOFTFP__
  1613. static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
  1614. {
  1615. sljit_s32 stack_offset = 0;
  1616. sljit_s32 arg_count = 0;
  1617. sljit_s32 word_arg_offset = 0;
  1618. sljit_s32 float_arg_count = 0;
  1619. sljit_s32 types = 0;
  1620. sljit_s32 src_offset = 4 * sizeof(sljit_sw);
  1621. sljit_u8 offsets[4];
  1622. if (src && FAST_IS_REG(*src))
  1623. src_offset = reg_map[*src] * sizeof(sljit_sw);
  1624. arg_types >>= SLJIT_DEF_SHIFT;
  1625. while (arg_types) {
  1626. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  1627. switch (arg_types & SLJIT_DEF_MASK) {
  1628. case SLJIT_ARG_TYPE_F32:
  1629. offsets[arg_count] = (sljit_u8)stack_offset;
  1630. stack_offset += sizeof(sljit_f32);
  1631. arg_count++;
  1632. float_arg_count++;
  1633. break;
  1634. case SLJIT_ARG_TYPE_F64:
  1635. if (stack_offset & 0x7)
  1636. stack_offset += sizeof(sljit_sw);
  1637. offsets[arg_count] = (sljit_u8)stack_offset;
  1638. stack_offset += sizeof(sljit_f64);
  1639. arg_count++;
  1640. float_arg_count++;
  1641. break;
  1642. default:
  1643. offsets[arg_count] = (sljit_u8)stack_offset;
  1644. stack_offset += sizeof(sljit_sw);
  1645. arg_count++;
  1646. word_arg_offset += sizeof(sljit_sw);
  1647. break;
  1648. }
  1649. arg_types >>= SLJIT_DEF_SHIFT;
  1650. }
  1651. if (stack_offset > 16)
  1652. FAIL_IF(push_inst16(compiler, SUB_SP | (((stack_offset - 16) + 0x7) & ~0x7) >> 2));
  1653. SLJIT_ASSERT(reg_map[TMP_REG1] == 12);
  1654. /* Process arguments in reversed direction. */
  1655. while (types) {
  1656. switch (types & SLJIT_DEF_MASK) {
  1657. case SLJIT_ARG_TYPE_F32:
  1658. arg_count--;
  1659. float_arg_count--;
  1660. stack_offset = offsets[arg_count];
  1661. if (stack_offset < 16) {
  1662. if (src_offset == stack_offset) {
  1663. FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
  1664. *src = TMP_REG1;
  1665. }
  1666. FAIL_IF(push_inst32(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
  1667. } else
  1668. FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800000 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1669. break;
  1670. case SLJIT_ARG_TYPE_F64:
  1671. arg_count--;
  1672. float_arg_count--;
  1673. stack_offset = offsets[arg_count];
  1674. SLJIT_ASSERT((stack_offset & 0x7) == 0);
  1675. if (stack_offset < 16) {
  1676. if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
  1677. FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
  1678. *src = TMP_REG1;
  1679. }
  1680. FAIL_IF(push_inst32(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
  1681. } else
  1682. FAIL_IF(push_inst32(compiler, VSTR_F32 | 0x800100 | RN4(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1683. break;
  1684. default:
  1685. arg_count--;
  1686. word_arg_offset -= sizeof(sljit_sw);
  1687. stack_offset = offsets[arg_count];
  1688. SLJIT_ASSERT(stack_offset >= word_arg_offset);
  1689. if (stack_offset != word_arg_offset) {
  1690. if (stack_offset < 16) {
  1691. if (src_offset == stack_offset) {
  1692. FAIL_IF(push_inst16(compiler, MOV | (src_offset << 1) | 4 | (1 << 7)));
  1693. *src = TMP_REG1;
  1694. }
  1695. else if (src_offset == word_arg_offset) {
  1696. *src = 1 + (stack_offset >> 2);
  1697. src_offset = stack_offset;
  1698. }
  1699. FAIL_IF(push_inst16(compiler, MOV | (stack_offset >> 2) | (word_arg_offset << 1)));
  1700. } else
  1701. FAIL_IF(push_inst16(compiler, STR_SP | (word_arg_offset << 6) | ((stack_offset - 16) >> 2)));
  1702. }
  1703. break;
  1704. }
  1705. types >>= SLJIT_DEF_SHIFT;
  1706. }
  1707. return SLJIT_SUCCESS;
  1708. }
  1709. static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1710. {
  1711. sljit_s32 stack_size = 0;
  1712. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
  1713. FAIL_IF(push_inst32(compiler, VMOV | (0 << 16) | (0 << 12)));
  1714. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
  1715. FAIL_IF(push_inst32(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
  1716. arg_types >>= SLJIT_DEF_SHIFT;
  1717. while (arg_types) {
  1718. switch (arg_types & SLJIT_DEF_MASK) {
  1719. case SLJIT_ARG_TYPE_F32:
  1720. stack_size += sizeof(sljit_f32);
  1721. break;
  1722. case SLJIT_ARG_TYPE_F64:
  1723. if (stack_size & 0x7)
  1724. stack_size += sizeof(sljit_sw);
  1725. stack_size += sizeof(sljit_f64);
  1726. break;
  1727. default:
  1728. stack_size += sizeof(sljit_sw);
  1729. break;
  1730. }
  1731. arg_types >>= SLJIT_DEF_SHIFT;
  1732. }
  1733. if (stack_size <= 16)
  1734. return SLJIT_SUCCESS;
  1735. return push_inst16(compiler, ADD_SP | ((((stack_size - 16) + 0x7) & ~0x7) >> 2));
  1736. }
  1737. #else
  1738. static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1739. {
  1740. sljit_u32 remap = 0;
  1741. sljit_u32 offset = 0;
  1742. sljit_u32 new_offset, mask;
  1743. /* Remove return value. */
  1744. arg_types >>= SLJIT_DEF_SHIFT;
  1745. while (arg_types) {
  1746. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
  1747. new_offset = 0;
  1748. mask = 1;
  1749. while (remap & mask) {
  1750. new_offset++;
  1751. mask <<= 1;
  1752. }
  1753. remap |= mask;
  1754. if (offset != new_offset)
  1755. FAIL_IF(push_inst32(compiler, VMOV_F32 | DD4((new_offset >> 1) + 1)
  1756. | ((new_offset & 0x1) ? 0x400000 : 0) | DM4((offset >> 1) + 1)));
  1757. offset += 2;
  1758. }
  1759. else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
  1760. new_offset = 0;
  1761. mask = 3;
  1762. while (remap & mask) {
  1763. new_offset += 2;
  1764. mask <<= 2;
  1765. }
  1766. remap |= mask;
  1767. if (offset != new_offset)
  1768. FAIL_IF(push_inst32(compiler, VMOV_F32 | SLJIT_F32_OP | DD4((new_offset >> 1) + 1) | DM4((offset >> 1) + 1)));
  1769. offset += 2;
  1770. }
  1771. arg_types >>= SLJIT_DEF_SHIFT;
  1772. }
  1773. return SLJIT_SUCCESS;
  1774. }
  1775. #endif
  1776. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  1777. sljit_s32 arg_types)
  1778. {
  1779. #ifdef __SOFTFP__
  1780. struct sljit_jump *jump;
  1781. #endif
  1782. CHECK_ERROR_PTR();
  1783. CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  1784. #ifdef __SOFTFP__
  1785. PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
  1786. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1787. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1788. compiler->skip_checks = 1;
  1789. #endif
  1790. jump = sljit_emit_jump(compiler, type);
  1791. PTR_FAIL_IF(jump == NULL);
  1792. PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
  1793. return jump;
  1794. #else
  1795. PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  1796. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1797. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1798. compiler->skip_checks = 1;
  1799. #endif
  1800. return sljit_emit_jump(compiler, type);
  1801. #endif
  1802. }
  1803. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
  1804. {
  1805. struct sljit_jump *jump;
  1806. CHECK_ERROR();
  1807. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  1808. ADJUST_LOCAL_OFFSET(src, srcw);
  1809. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  1810. if (!(src & SLJIT_IMM)) {
  1811. if (FAST_IS_REG(src)) {
  1812. SLJIT_ASSERT(reg_map[src] != 14);
  1813. return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(src));
  1814. }
  1815. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, src, srcw, TMP_REG1));
  1816. if (type >= SLJIT_FAST_CALL)
  1817. return push_inst16(compiler, BLX | RN3(TMP_REG1));
  1818. }
  1819. /* These jumps are converted to jump/call instructions when possible. */
  1820. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1821. FAIL_IF(!jump);
  1822. set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
  1823. jump->u.target = srcw;
  1824. FAIL_IF(emit_imm32_const(compiler, TMP_REG1, 0));
  1825. jump->addr = compiler->size;
  1826. return push_inst16(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RN3(TMP_REG1));
  1827. }
  1828. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  1829. sljit_s32 arg_types,
  1830. sljit_s32 src, sljit_sw srcw)
  1831. {
  1832. CHECK_ERROR();
  1833. CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  1834. #ifdef __SOFTFP__
  1835. if (src & SLJIT_MEM) {
  1836. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, src, srcw, TMP_REG1));
  1837. src = TMP_REG1;
  1838. }
  1839. FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
  1840. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1841. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1842. compiler->skip_checks = 1;
  1843. #endif
  1844. FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  1845. return softfloat_post_call_with_args(compiler, arg_types);
  1846. #else /* !__SOFTFP__ */
  1847. FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  1848. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1849. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1850. compiler->skip_checks = 1;
  1851. #endif
  1852. return sljit_emit_ijump(compiler, type, src, srcw);
  1853. #endif /* __SOFTFP__ */
  1854. }
  1855. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
  1856. sljit_s32 dst, sljit_sw dstw,
  1857. sljit_s32 type)
  1858. {
  1859. sljit_s32 dst_r, flags = GET_ALL_FLAGS(op);
  1860. sljit_ins cc;
  1861. CHECK_ERROR();
  1862. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
  1863. ADJUST_LOCAL_OFFSET(dst, dstw);
  1864. op = GET_OPCODE(op);
  1865. cc = get_cc(type & 0xff);
  1866. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
  1867. if (op < SLJIT_ADD) {
  1868. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
  1869. if (reg_map[dst_r] > 7) {
  1870. FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 1));
  1871. FAIL_IF(push_inst32(compiler, MOV_WI | RD4(dst_r) | 0));
  1872. } else {
  1873. /* The movsi (immediate) instruction does not set flags in IT block. */
  1874. FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 1));
  1875. FAIL_IF(push_inst16(compiler, MOVSI | RDN3(dst_r) | 0));
  1876. }
  1877. if (!(dst & SLJIT_MEM))
  1878. return SLJIT_SUCCESS;
  1879. return emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2);
  1880. }
  1881. if (dst & SLJIT_MEM)
  1882. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
  1883. if (op == SLJIT_AND) {
  1884. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | (((cc & 0x1) ^ 0x1) << 3) | 0x4));
  1885. FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 1));
  1886. FAIL_IF(push_inst32(compiler, ANDI | RN4(dst_r) | RD4(dst_r) | 0));
  1887. }
  1888. else {
  1889. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1890. FAIL_IF(push_inst32(compiler, ((op == SLJIT_OR) ? ORRI : EORI) | RN4(dst_r) | RD4(dst_r) | 1));
  1891. }
  1892. if (dst & SLJIT_MEM)
  1893. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, TMP_REG1, dst, dstw, TMP_REG2));
  1894. if (!(flags & SLJIT_SET_Z))
  1895. return SLJIT_SUCCESS;
  1896. /* The condition must always be set, even if the ORR/EORI is not executed above. */
  1897. return push_inst32(compiler, MOV_W | SET_FLAGS | RD4(TMP_REG1) | RM4(dst_r));
  1898. }
  1899. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
  1900. sljit_s32 dst_reg,
  1901. sljit_s32 src, sljit_sw srcw)
  1902. {
  1903. sljit_uw cc, tmp;
  1904. CHECK_ERROR();
  1905. CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
  1906. dst_reg &= ~SLJIT_I32_OP;
  1907. cc = get_cc(type & 0xff);
  1908. if (!(src & SLJIT_IMM)) {
  1909. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1910. return push_inst16(compiler, MOV | SET_REGS44(dst_reg, src));
  1911. }
  1912. tmp = (sljit_uw) srcw;
  1913. if (tmp < 0x10000) {
  1914. /* set low 16 bits, set hi 16 bits to 0. */
  1915. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1916. return push_inst32(compiler, MOVW | RD4(dst_reg)
  1917. | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff));
  1918. }
  1919. tmp = get_imm(srcw);
  1920. if (tmp != INVALID_IMM) {
  1921. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1922. return push_inst32(compiler, MOV_WI | RD4(dst_reg) | tmp);
  1923. }
  1924. tmp = get_imm(~srcw);
  1925. if (tmp != INVALID_IMM) {
  1926. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | 0x8));
  1927. return push_inst32(compiler, MVN_WI | RD4(dst_reg) | tmp);
  1928. }
  1929. FAIL_IF(push_inst16(compiler, IT | (cc << 4) | ((cc & 0x1) << 3) | 0x4));
  1930. tmp = (sljit_uw) srcw;
  1931. FAIL_IF(push_inst32(compiler, MOVW | RD4(dst_reg)
  1932. | COPY_BITS(tmp, 12, 16, 4) | COPY_BITS(tmp, 11, 26, 1) | COPY_BITS(tmp, 8, 12, 3) | (tmp & 0xff)));
  1933. return push_inst32(compiler, MOVT | RD4(dst_reg)
  1934. | COPY_BITS(tmp, 12 + 16, 16, 4) | COPY_BITS(tmp, 11 + 16, 26, 1) | COPY_BITS(tmp, 8 + 16, 12, 3) | ((tmp & 0xff0000) >> 16));
  1935. }
  1936. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
  1937. sljit_s32 reg,
  1938. sljit_s32 mem, sljit_sw memw)
  1939. {
  1940. sljit_s32 flags;
  1941. sljit_ins inst;
  1942. CHECK_ERROR();
  1943. CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
  1944. if ((mem & OFFS_REG_MASK) || (memw > 255 && memw < -255))
  1945. return SLJIT_ERR_UNSUPPORTED;
  1946. if (type & SLJIT_MEM_SUPP)
  1947. return SLJIT_SUCCESS;
  1948. switch (type & 0xff) {
  1949. case SLJIT_MOV:
  1950. case SLJIT_MOV_U32:
  1951. case SLJIT_MOV_S32:
  1952. case SLJIT_MOV_P:
  1953. flags = WORD_SIZE;
  1954. break;
  1955. case SLJIT_MOV_U8:
  1956. flags = BYTE_SIZE;
  1957. break;
  1958. case SLJIT_MOV_S8:
  1959. flags = BYTE_SIZE | SIGNED;
  1960. break;
  1961. case SLJIT_MOV_U16:
  1962. flags = HALF_SIZE;
  1963. break;
  1964. case SLJIT_MOV_S16:
  1965. flags = HALF_SIZE | SIGNED;
  1966. break;
  1967. default:
  1968. SLJIT_UNREACHABLE();
  1969. flags = WORD_SIZE;
  1970. break;
  1971. }
  1972. if (type & SLJIT_MEM_STORE)
  1973. flags |= STORE;
  1974. inst = sljit_mem32[flags] | 0x900;
  1975. if (type & SLJIT_MEM_PRE)
  1976. inst |= 0x400;
  1977. if (memw >= 0)
  1978. inst |= 0x200;
  1979. else
  1980. memw = -memw;
  1981. return push_inst32(compiler, inst | RT4(reg) | RN4(mem & REG_MASK) | memw);
  1982. }
  1983. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
  1984. {
  1985. struct sljit_const *const_;
  1986. sljit_s32 dst_r;
  1987. CHECK_ERROR_PTR();
  1988. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  1989. ADJUST_LOCAL_OFFSET(dst, dstw);
  1990. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  1991. PTR_FAIL_IF(!const_);
  1992. set_const(const_, compiler);
  1993. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
  1994. PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, init_value));
  1995. if (dst & SLJIT_MEM)
  1996. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
  1997. return const_;
  1998. }
  1999. SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  2000. {
  2001. struct sljit_put_label *put_label;
  2002. sljit_s32 dst_r;
  2003. CHECK_ERROR_PTR();
  2004. CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
  2005. ADJUST_LOCAL_OFFSET(dst, dstw);
  2006. put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
  2007. PTR_FAIL_IF(!put_label);
  2008. set_put_label(put_label, compiler, 0);
  2009. dst_r = FAST_IS_REG(dst) ? dst : TMP_REG1;
  2010. PTR_FAIL_IF(emit_imm32_const(compiler, dst_r, 0));
  2011. if (dst & SLJIT_MEM)
  2012. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE | STORE, dst_r, dst, dstw, TMP_REG2));
  2013. return put_label;
  2014. }
  2015. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  2016. {
  2017. sljit_u16 *inst = (sljit_u16*)addr;
  2018. modify_imm32_const(inst, new_target);
  2019. inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  2020. SLJIT_CACHE_FLUSH(inst, inst + 4);
  2021. }
  2022. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  2023. {
  2024. sljit_u16 *inst = (sljit_u16*)addr;
  2025. modify_imm32_const(inst, new_constant);
  2026. inst = (sljit_u16 *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  2027. SLJIT_CACHE_FLUSH(inst, inst + 4);
  2028. }