sljitNativeARM_32.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674
  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. #ifdef __SOFTFP__
  27. #define ARM_ABI_INFO " ABI:softfp"
  28. #else
  29. #define ARM_ABI_INFO " ABI:hardfp"
  30. #endif
  31. SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void)
  32. {
  33. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  34. return "ARMv7" SLJIT_CPUINFO ARM_ABI_INFO;
  35. #elif (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  36. return "ARMv5" SLJIT_CPUINFO ARM_ABI_INFO;
  37. #else
  38. #error "Internal error: Unknown ARM architecture"
  39. #endif
  40. }
  41. /* Last register + 1. */
  42. #define TMP_REG1 (SLJIT_NUMBER_OF_REGISTERS + 2)
  43. #define TMP_REG2 (SLJIT_NUMBER_OF_REGISTERS + 3)
  44. #define TMP_PC (SLJIT_NUMBER_OF_REGISTERS + 4)
  45. #define TMP_FREG1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 1)
  46. #define TMP_FREG2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS + 2)
  47. /* In ARM instruction words.
  48. Cache lines are usually 32 byte aligned. */
  49. #define CONST_POOL_ALIGNMENT 8
  50. #define CONST_POOL_EMPTY 0xffffffff
  51. #define ALIGN_INSTRUCTION(ptr) \
  52. (sljit_uw*)(((sljit_uw)(ptr) + (CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1) & ~((CONST_POOL_ALIGNMENT * sizeof(sljit_uw)) - 1))
  53. #define MAX_DIFFERENCE(max_diff) \
  54. (((max_diff) / (sljit_s32)sizeof(sljit_uw)) - (CONST_POOL_ALIGNMENT - 1))
  55. /* See sljit_emit_enter and sljit_emit_op0 if you want to change them. */
  56. static const sljit_u8 reg_map[SLJIT_NUMBER_OF_REGISTERS + 5] = {
  57. 0, 0, 1, 2, 3, 11, 10, 9, 8, 7, 6, 5, 4, 13, 12, 14, 15
  58. };
  59. static const sljit_u8 freg_map[SLJIT_NUMBER_OF_FLOAT_REGISTERS + 3] = {
  60. 0, 0, 1, 2, 3, 4, 5, 6, 7
  61. };
  62. #define RM(rm) (reg_map[rm])
  63. #define RD(rd) (reg_map[rd] << 12)
  64. #define RN(rn) (reg_map[rn] << 16)
  65. /* --------------------------------------------------------------------- */
  66. /* Instrucion forms */
  67. /* --------------------------------------------------------------------- */
  68. /* The instruction includes the AL condition.
  69. INST_NAME - CONDITIONAL remove this flag. */
  70. #define COND_MASK 0xf0000000
  71. #define CONDITIONAL 0xe0000000
  72. #define PUSH_POOL 0xff000000
  73. #define ADC 0xe0a00000
  74. #define ADD 0xe0800000
  75. #define AND 0xe0000000
  76. #define B 0xea000000
  77. #define BIC 0xe1c00000
  78. #define BL 0xeb000000
  79. #define BLX 0xe12fff30
  80. #define BX 0xe12fff10
  81. #define CLZ 0xe16f0f10
  82. #define CMN 0xe1600000
  83. #define CMP 0xe1400000
  84. #define BKPT 0xe1200070
  85. #define EOR 0xe0200000
  86. #define MOV 0xe1a00000
  87. #define MUL 0xe0000090
  88. #define MVN 0xe1e00000
  89. #define NOP 0xe1a00000
  90. #define ORR 0xe1800000
  91. #define PUSH 0xe92d0000
  92. #define POP 0xe8bd0000
  93. #define RSB 0xe0600000
  94. #define RSC 0xe0e00000
  95. #define SBC 0xe0c00000
  96. #define SMULL 0xe0c00090
  97. #define SUB 0xe0400000
  98. #define UMULL 0xe0800090
  99. #define VABS_F32 0xeeb00ac0
  100. #define VADD_F32 0xee300a00
  101. #define VCMP_F32 0xeeb40a40
  102. #define VCVT_F32_S32 0xeeb80ac0
  103. #define VCVT_F64_F32 0xeeb70ac0
  104. #define VCVT_S32_F32 0xeebd0ac0
  105. #define VDIV_F32 0xee800a00
  106. #define VMOV_F32 0xeeb00a40
  107. #define VMOV 0xee000a10
  108. #define VMOV2 0xec400a10
  109. #define VMRS 0xeef1fa10
  110. #define VMUL_F32 0xee200a00
  111. #define VNEG_F32 0xeeb10a40
  112. #define VSTR_F32 0xed000a00
  113. #define VSUB_F32 0xee300a40
  114. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  115. /* Arm v7 specific instructions. */
  116. #define MOVW 0xe3000000
  117. #define MOVT 0xe3400000
  118. #define SXTB 0xe6af0070
  119. #define SXTH 0xe6bf0070
  120. #define UXTB 0xe6ef0070
  121. #define UXTH 0xe6ff0070
  122. #endif
  123. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  124. static sljit_s32 push_cpool(struct sljit_compiler *compiler)
  125. {
  126. /* Pushing the constant pool into the instruction stream. */
  127. sljit_uw* inst;
  128. sljit_uw* cpool_ptr;
  129. sljit_uw* cpool_end;
  130. sljit_s32 i;
  131. /* The label could point the address after the constant pool. */
  132. if (compiler->last_label && compiler->last_label->size == compiler->size)
  133. compiler->last_label->size += compiler->cpool_fill + (CONST_POOL_ALIGNMENT - 1) + 1;
  134. SLJIT_ASSERT(compiler->cpool_fill > 0 && compiler->cpool_fill <= CPOOL_SIZE);
  135. inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  136. FAIL_IF(!inst);
  137. compiler->size++;
  138. *inst = 0xff000000 | compiler->cpool_fill;
  139. for (i = 0; i < CONST_POOL_ALIGNMENT - 1; i++) {
  140. inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  141. FAIL_IF(!inst);
  142. compiler->size++;
  143. *inst = 0;
  144. }
  145. cpool_ptr = compiler->cpool;
  146. cpool_end = cpool_ptr + compiler->cpool_fill;
  147. while (cpool_ptr < cpool_end) {
  148. inst = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  149. FAIL_IF(!inst);
  150. compiler->size++;
  151. *inst = *cpool_ptr++;
  152. }
  153. compiler->cpool_diff = CONST_POOL_EMPTY;
  154. compiler->cpool_fill = 0;
  155. return SLJIT_SUCCESS;
  156. }
  157. static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
  158. {
  159. sljit_uw* ptr;
  160. if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
  161. FAIL_IF(push_cpool(compiler));
  162. ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  163. FAIL_IF(!ptr);
  164. compiler->size++;
  165. *ptr = inst;
  166. return SLJIT_SUCCESS;
  167. }
  168. static sljit_s32 push_inst_with_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
  169. {
  170. sljit_uw* ptr;
  171. sljit_uw cpool_index = CPOOL_SIZE;
  172. sljit_uw* cpool_ptr;
  173. sljit_uw* cpool_end;
  174. sljit_u8* cpool_unique_ptr;
  175. if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)))
  176. FAIL_IF(push_cpool(compiler));
  177. else if (compiler->cpool_fill > 0) {
  178. cpool_ptr = compiler->cpool;
  179. cpool_end = cpool_ptr + compiler->cpool_fill;
  180. cpool_unique_ptr = compiler->cpool_unique;
  181. do {
  182. if ((*cpool_ptr == literal) && !(*cpool_unique_ptr)) {
  183. cpool_index = cpool_ptr - compiler->cpool;
  184. break;
  185. }
  186. cpool_ptr++;
  187. cpool_unique_ptr++;
  188. } while (cpool_ptr < cpool_end);
  189. }
  190. if (cpool_index == CPOOL_SIZE) {
  191. /* Must allocate a new entry in the literal pool. */
  192. if (compiler->cpool_fill < CPOOL_SIZE) {
  193. cpool_index = compiler->cpool_fill;
  194. compiler->cpool_fill++;
  195. }
  196. else {
  197. FAIL_IF(push_cpool(compiler));
  198. cpool_index = 0;
  199. compiler->cpool_fill = 1;
  200. }
  201. }
  202. SLJIT_ASSERT((inst & 0xfff) == 0);
  203. ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  204. FAIL_IF(!ptr);
  205. compiler->size++;
  206. *ptr = inst | cpool_index;
  207. compiler->cpool[cpool_index] = literal;
  208. compiler->cpool_unique[cpool_index] = 0;
  209. if (compiler->cpool_diff == CONST_POOL_EMPTY)
  210. compiler->cpool_diff = compiler->size;
  211. return SLJIT_SUCCESS;
  212. }
  213. static sljit_s32 push_inst_with_unique_literal(struct sljit_compiler *compiler, sljit_uw inst, sljit_uw literal)
  214. {
  215. sljit_uw* ptr;
  216. if (SLJIT_UNLIKELY((compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4092)) || compiler->cpool_fill >= CPOOL_SIZE))
  217. FAIL_IF(push_cpool(compiler));
  218. SLJIT_ASSERT(compiler->cpool_fill < CPOOL_SIZE && (inst & 0xfff) == 0);
  219. ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  220. FAIL_IF(!ptr);
  221. compiler->size++;
  222. *ptr = inst | compiler->cpool_fill;
  223. compiler->cpool[compiler->cpool_fill] = literal;
  224. compiler->cpool_unique[compiler->cpool_fill] = 1;
  225. compiler->cpool_fill++;
  226. if (compiler->cpool_diff == CONST_POOL_EMPTY)
  227. compiler->cpool_diff = compiler->size;
  228. return SLJIT_SUCCESS;
  229. }
  230. static SLJIT_INLINE sljit_s32 prepare_blx(struct sljit_compiler *compiler)
  231. {
  232. /* Place for at least two instruction (doesn't matter whether the first has a literal). */
  233. if (SLJIT_UNLIKELY(compiler->cpool_diff != CONST_POOL_EMPTY && compiler->size - compiler->cpool_diff >= MAX_DIFFERENCE(4088)))
  234. return push_cpool(compiler);
  235. return SLJIT_SUCCESS;
  236. }
  237. static SLJIT_INLINE sljit_s32 emit_blx(struct sljit_compiler *compiler)
  238. {
  239. /* Must follow tightly the previous instruction (to be able to convert it to bl instruction). */
  240. SLJIT_ASSERT(compiler->cpool_diff == CONST_POOL_EMPTY || compiler->size - compiler->cpool_diff < MAX_DIFFERENCE(4092));
  241. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  242. return push_inst(compiler, BLX | RM(TMP_REG1));
  243. }
  244. static sljit_uw patch_pc_relative_loads(sljit_uw *last_pc_patch, sljit_uw *code_ptr, sljit_uw* const_pool, sljit_uw cpool_size)
  245. {
  246. sljit_uw diff;
  247. sljit_uw ind;
  248. sljit_uw counter = 0;
  249. sljit_uw* clear_const_pool = const_pool;
  250. sljit_uw* clear_const_pool_end = const_pool + cpool_size;
  251. SLJIT_ASSERT(const_pool - code_ptr <= CONST_POOL_ALIGNMENT);
  252. /* Set unused flag for all literals in the constant pool.
  253. I.e.: unused literals can belong to branches, which can be encoded as B or BL.
  254. We can "compress" the constant pool by discarding these literals. */
  255. while (clear_const_pool < clear_const_pool_end)
  256. *clear_const_pool++ = (sljit_uw)(-1);
  257. while (last_pc_patch < code_ptr) {
  258. /* Data transfer instruction with Rn == r15. */
  259. if ((*last_pc_patch & 0x0c0f0000) == 0x040f0000) {
  260. diff = const_pool - last_pc_patch;
  261. ind = (*last_pc_patch) & 0xfff;
  262. /* Must be a load instruction with immediate offset. */
  263. SLJIT_ASSERT(ind < cpool_size && !(*last_pc_patch & (1 << 25)) && (*last_pc_patch & (1 << 20)));
  264. if ((sljit_s32)const_pool[ind] < 0) {
  265. const_pool[ind] = counter;
  266. ind = counter;
  267. counter++;
  268. }
  269. else
  270. ind = const_pool[ind];
  271. SLJIT_ASSERT(diff >= 1);
  272. if (diff >= 2 || ind > 0) {
  273. diff = (diff + ind - 2) << 2;
  274. SLJIT_ASSERT(diff <= 0xfff);
  275. *last_pc_patch = (*last_pc_patch & ~0xfff) | diff;
  276. }
  277. else
  278. *last_pc_patch = (*last_pc_patch & ~(0xfff | (1 << 23))) | 0x004;
  279. }
  280. last_pc_patch++;
  281. }
  282. return counter;
  283. }
  284. /* In some rare ocasions we may need future patches. The probability is close to 0 in practice. */
  285. struct future_patch {
  286. struct future_patch* next;
  287. sljit_s32 index;
  288. sljit_s32 value;
  289. };
  290. static sljit_s32 resolve_const_pool_index(struct sljit_compiler *compiler, struct future_patch **first_patch, sljit_uw cpool_current_index, sljit_uw *cpool_start_address, sljit_uw *buf_ptr)
  291. {
  292. sljit_s32 value;
  293. struct future_patch *curr_patch, *prev_patch;
  294. SLJIT_UNUSED_ARG(compiler);
  295. /* Using the values generated by patch_pc_relative_loads. */
  296. if (!*first_patch)
  297. value = (sljit_s32)cpool_start_address[cpool_current_index];
  298. else {
  299. curr_patch = *first_patch;
  300. prev_patch = NULL;
  301. while (1) {
  302. if (!curr_patch) {
  303. value = (sljit_s32)cpool_start_address[cpool_current_index];
  304. break;
  305. }
  306. if ((sljit_uw)curr_patch->index == cpool_current_index) {
  307. value = curr_patch->value;
  308. if (prev_patch)
  309. prev_patch->next = curr_patch->next;
  310. else
  311. *first_patch = curr_patch->next;
  312. SLJIT_FREE(curr_patch, compiler->allocator_data);
  313. break;
  314. }
  315. prev_patch = curr_patch;
  316. curr_patch = curr_patch->next;
  317. }
  318. }
  319. if (value >= 0) {
  320. if ((sljit_uw)value > cpool_current_index) {
  321. curr_patch = (struct future_patch*)SLJIT_MALLOC(sizeof(struct future_patch), compiler->allocator_data);
  322. if (!curr_patch) {
  323. while (*first_patch) {
  324. curr_patch = *first_patch;
  325. *first_patch = (*first_patch)->next;
  326. SLJIT_FREE(curr_patch, compiler->allocator_data);
  327. }
  328. return SLJIT_ERR_ALLOC_FAILED;
  329. }
  330. curr_patch->next = *first_patch;
  331. curr_patch->index = value;
  332. curr_patch->value = cpool_start_address[value];
  333. *first_patch = curr_patch;
  334. }
  335. cpool_start_address[value] = *buf_ptr;
  336. }
  337. return SLJIT_SUCCESS;
  338. }
  339. #else
  340. static sljit_s32 push_inst(struct sljit_compiler *compiler, sljit_uw inst)
  341. {
  342. sljit_uw* ptr;
  343. ptr = (sljit_uw*)ensure_buf(compiler, sizeof(sljit_uw));
  344. FAIL_IF(!ptr);
  345. compiler->size++;
  346. *ptr = inst;
  347. return SLJIT_SUCCESS;
  348. }
  349. static SLJIT_INLINE sljit_s32 emit_imm(struct sljit_compiler *compiler, sljit_s32 reg, sljit_sw imm)
  350. {
  351. FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
  352. return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
  353. }
  354. #endif
  355. static SLJIT_INLINE sljit_s32 detect_jump_type(struct sljit_jump *jump, sljit_uw *code_ptr, sljit_uw *code, sljit_sw executable_offset)
  356. {
  357. sljit_sw diff;
  358. if (jump->flags & SLJIT_REWRITABLE_JUMP)
  359. return 0;
  360. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  361. if (jump->flags & IS_BL)
  362. code_ptr--;
  363. if (jump->flags & JUMP_ADDR)
  364. diff = ((sljit_sw)jump->u.target - (sljit_sw)(code_ptr + 2) - executable_offset);
  365. else {
  366. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  367. diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)(code_ptr + 2));
  368. }
  369. /* Branch to Thumb code has not been optimized yet. */
  370. if (diff & 0x3)
  371. return 0;
  372. if (jump->flags & IS_BL) {
  373. if (diff <= 0x01ffffff && diff >= -0x02000000) {
  374. *code_ptr = (BL - CONDITIONAL) | (*(code_ptr + 1) & COND_MASK);
  375. jump->flags |= PATCH_B;
  376. return 1;
  377. }
  378. }
  379. else {
  380. if (diff <= 0x01ffffff && diff >= -0x02000000) {
  381. *code_ptr = (B - CONDITIONAL) | (*code_ptr & COND_MASK);
  382. jump->flags |= PATCH_B;
  383. }
  384. }
  385. #else
  386. if (jump->flags & JUMP_ADDR)
  387. diff = ((sljit_sw)jump->u.target - (sljit_sw)code_ptr - executable_offset);
  388. else {
  389. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  390. diff = ((sljit_sw)(code + jump->u.label->size) - (sljit_sw)code_ptr);
  391. }
  392. /* Branch to Thumb code has not been optimized yet. */
  393. if (diff & 0x3)
  394. return 0;
  395. if (diff <= 0x01ffffff && diff >= -0x02000000) {
  396. code_ptr -= 2;
  397. *code_ptr = ((jump->flags & IS_BL) ? (BL - CONDITIONAL) : (B - CONDITIONAL)) | (code_ptr[2] & COND_MASK);
  398. jump->flags |= PATCH_B;
  399. return 1;
  400. }
  401. #endif
  402. return 0;
  403. }
  404. static SLJIT_INLINE void inline_set_jump_addr(sljit_uw jump_ptr, sljit_sw executable_offset, sljit_uw new_addr, sljit_s32 flush_cache)
  405. {
  406. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  407. sljit_uw *ptr = (sljit_uw *)jump_ptr;
  408. sljit_uw *inst = (sljit_uw *)ptr[0];
  409. sljit_uw mov_pc = ptr[1];
  410. sljit_s32 bl = (mov_pc & 0x0000f000) != RD(TMP_PC);
  411. sljit_sw diff = (sljit_sw)(((sljit_sw)new_addr - (sljit_sw)(inst + 2) - executable_offset) >> 2);
  412. if (diff <= 0x7fffff && diff >= -0x800000) {
  413. /* Turn to branch. */
  414. if (!bl) {
  415. inst[0] = (mov_pc & COND_MASK) | (B - CONDITIONAL) | (diff & 0xffffff);
  416. if (flush_cache) {
  417. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  418. SLJIT_CACHE_FLUSH(inst, inst + 1);
  419. }
  420. } else {
  421. inst[0] = (mov_pc & COND_MASK) | (BL - CONDITIONAL) | (diff & 0xffffff);
  422. inst[1] = NOP;
  423. if (flush_cache) {
  424. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  425. SLJIT_CACHE_FLUSH(inst, inst + 2);
  426. }
  427. }
  428. } else {
  429. /* Get the position of the constant. */
  430. if (mov_pc & (1 << 23))
  431. ptr = inst + ((mov_pc & 0xfff) >> 2) + 2;
  432. else
  433. ptr = inst + 1;
  434. if (*inst != mov_pc) {
  435. inst[0] = mov_pc;
  436. if (!bl) {
  437. if (flush_cache) {
  438. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  439. SLJIT_CACHE_FLUSH(inst, inst + 1);
  440. }
  441. } else {
  442. inst[1] = BLX | RM(TMP_REG1);
  443. if (flush_cache) {
  444. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  445. SLJIT_CACHE_FLUSH(inst, inst + 2);
  446. }
  447. }
  448. }
  449. *ptr = new_addr;
  450. }
  451. #else
  452. sljit_uw *inst = (sljit_uw*)jump_ptr;
  453. SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
  454. inst[0] = MOVW | (inst[0] & 0xf000) | ((new_addr << 4) & 0xf0000) | (new_addr & 0xfff);
  455. inst[1] = MOVT | (inst[1] & 0xf000) | ((new_addr >> 12) & 0xf0000) | ((new_addr >> 16) & 0xfff);
  456. if (flush_cache) {
  457. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  458. SLJIT_CACHE_FLUSH(inst, inst + 2);
  459. }
  460. #endif
  461. }
  462. static sljit_uw get_imm(sljit_uw imm);
  463. static SLJIT_INLINE void inline_set_const(sljit_uw addr, sljit_sw executable_offset, sljit_sw new_constant, sljit_s32 flush_cache)
  464. {
  465. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  466. sljit_uw *ptr = (sljit_uw*)addr;
  467. sljit_uw *inst = (sljit_uw*)ptr[0];
  468. sljit_uw ldr_literal = ptr[1];
  469. sljit_uw src2;
  470. src2 = get_imm(new_constant);
  471. if (src2) {
  472. *inst = 0xe3a00000 | (ldr_literal & 0xf000) | src2;
  473. if (flush_cache) {
  474. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  475. SLJIT_CACHE_FLUSH(inst, inst + 1);
  476. }
  477. return;
  478. }
  479. src2 = get_imm(~new_constant);
  480. if (src2) {
  481. *inst = 0xe3e00000 | (ldr_literal & 0xf000) | src2;
  482. if (flush_cache) {
  483. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  484. SLJIT_CACHE_FLUSH(inst, inst + 1);
  485. }
  486. return;
  487. }
  488. if (ldr_literal & (1 << 23))
  489. ptr = inst + ((ldr_literal & 0xfff) >> 2) + 2;
  490. else
  491. ptr = inst + 1;
  492. if (*inst != ldr_literal) {
  493. *inst = ldr_literal;
  494. if (flush_cache) {
  495. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  496. SLJIT_CACHE_FLUSH(inst, inst + 1);
  497. }
  498. }
  499. *ptr = new_constant;
  500. #else
  501. sljit_uw *inst = (sljit_uw*)addr;
  502. SLJIT_ASSERT((inst[0] & 0xfff00000) == MOVW && (inst[1] & 0xfff00000) == MOVT);
  503. inst[0] = MOVW | (inst[0] & 0xf000) | ((new_constant << 4) & 0xf0000) | (new_constant & 0xfff);
  504. inst[1] = MOVT | (inst[1] & 0xf000) | ((new_constant >> 12) & 0xf0000) | ((new_constant >> 16) & 0xfff);
  505. if (flush_cache) {
  506. inst = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(inst, executable_offset);
  507. SLJIT_CACHE_FLUSH(inst, inst + 2);
  508. }
  509. #endif
  510. }
  511. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler)
  512. {
  513. struct sljit_memory_fragment *buf;
  514. sljit_uw *code;
  515. sljit_uw *code_ptr;
  516. sljit_uw *buf_ptr;
  517. sljit_uw *buf_end;
  518. sljit_uw size;
  519. sljit_uw word_count;
  520. sljit_sw executable_offset;
  521. sljit_sw jump_addr;
  522. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  523. sljit_uw cpool_size;
  524. sljit_uw cpool_skip_alignment;
  525. sljit_uw cpool_current_index;
  526. sljit_uw *cpool_start_address;
  527. sljit_uw *last_pc_patch;
  528. struct future_patch *first_patch;
  529. #endif
  530. struct sljit_label *label;
  531. struct sljit_jump *jump;
  532. struct sljit_const *const_;
  533. CHECK_ERROR_PTR();
  534. CHECK_PTR(check_sljit_generate_code(compiler));
  535. reverse_buf(compiler);
  536. /* Second code generation pass. */
  537. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  538. size = compiler->size + (compiler->patches << 1);
  539. if (compiler->cpool_fill > 0)
  540. size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
  541. #else
  542. size = compiler->size;
  543. #endif
  544. code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw));
  545. PTR_FAIL_WITH_EXEC_IF(code);
  546. buf = compiler->buf;
  547. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  548. cpool_size = 0;
  549. cpool_skip_alignment = 0;
  550. cpool_current_index = 0;
  551. cpool_start_address = NULL;
  552. first_patch = NULL;
  553. last_pc_patch = code;
  554. #endif
  555. code_ptr = code;
  556. word_count = 0;
  557. executable_offset = SLJIT_EXEC_OFFSET(code);
  558. label = compiler->labels;
  559. jump = compiler->jumps;
  560. const_ = compiler->consts;
  561. if (label && label->size == 0) {
  562. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  563. label = label->next;
  564. }
  565. do {
  566. buf_ptr = (sljit_uw*)buf->memory;
  567. buf_end = buf_ptr + (buf->used_size >> 2);
  568. do {
  569. word_count++;
  570. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  571. if (cpool_size > 0) {
  572. if (cpool_skip_alignment > 0) {
  573. buf_ptr++;
  574. cpool_skip_alignment--;
  575. }
  576. else {
  577. if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
  578. SLJIT_FREE_EXEC(code);
  579. compiler->error = SLJIT_ERR_ALLOC_FAILED;
  580. return NULL;
  581. }
  582. buf_ptr++;
  583. if (++cpool_current_index >= cpool_size) {
  584. SLJIT_ASSERT(!first_patch);
  585. cpool_size = 0;
  586. if (label && label->size == word_count) {
  587. /* Points after the current instruction. */
  588. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  589. label->size = code_ptr - code;
  590. label = label->next;
  591. }
  592. }
  593. }
  594. }
  595. else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
  596. #endif
  597. *code_ptr = *buf_ptr++;
  598. /* These structures are ordered by their address. */
  599. SLJIT_ASSERT(!label || label->size >= word_count);
  600. SLJIT_ASSERT(!jump || jump->addr >= word_count);
  601. SLJIT_ASSERT(!const_ || const_->addr >= word_count);
  602. if (jump && jump->addr == word_count) {
  603. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  604. if (detect_jump_type(jump, code_ptr, code, executable_offset))
  605. code_ptr--;
  606. jump->addr = (sljit_uw)code_ptr;
  607. #else
  608. jump->addr = (sljit_uw)(code_ptr - 2);
  609. if (detect_jump_type(jump, code_ptr, code, executable_offset))
  610. code_ptr -= 2;
  611. #endif
  612. jump = jump->next;
  613. }
  614. if (label && label->size == word_count) {
  615. /* code_ptr can be affected above. */
  616. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
  617. label->size = (code_ptr + 1) - code;
  618. label = label->next;
  619. }
  620. if (const_ && const_->addr == word_count) {
  621. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  622. const_->addr = (sljit_uw)code_ptr;
  623. #else
  624. const_->addr = (sljit_uw)(code_ptr - 1);
  625. #endif
  626. const_ = const_->next;
  627. }
  628. code_ptr++;
  629. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  630. }
  631. else {
  632. /* Fortunately, no need to shift. */
  633. cpool_size = *buf_ptr++ & ~PUSH_POOL;
  634. SLJIT_ASSERT(cpool_size > 0);
  635. cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
  636. cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
  637. if (cpool_current_index > 0) {
  638. /* Unconditional branch. */
  639. *code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
  640. code_ptr = cpool_start_address + cpool_current_index;
  641. }
  642. cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
  643. cpool_current_index = 0;
  644. last_pc_patch = code_ptr;
  645. }
  646. #endif
  647. } while (buf_ptr < buf_end);
  648. buf = buf->next;
  649. } while (buf);
  650. SLJIT_ASSERT(!label);
  651. SLJIT_ASSERT(!jump);
  652. SLJIT_ASSERT(!const_);
  653. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  654. SLJIT_ASSERT(cpool_size == 0);
  655. if (compiler->cpool_fill > 0) {
  656. cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
  657. cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
  658. if (cpool_current_index > 0)
  659. code_ptr = cpool_start_address + cpool_current_index;
  660. buf_ptr = compiler->cpool;
  661. buf_end = buf_ptr + compiler->cpool_fill;
  662. cpool_current_index = 0;
  663. while (buf_ptr < buf_end) {
  664. if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
  665. SLJIT_FREE_EXEC(code);
  666. compiler->error = SLJIT_ERR_ALLOC_FAILED;
  667. return NULL;
  668. }
  669. buf_ptr++;
  670. cpool_current_index++;
  671. }
  672. SLJIT_ASSERT(!first_patch);
  673. }
  674. #endif
  675. jump = compiler->jumps;
  676. while (jump) {
  677. buf_ptr = (sljit_uw *)jump->addr;
  678. if (jump->flags & PATCH_B) {
  679. jump_addr = (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
  680. if (!(jump->flags & JUMP_ADDR)) {
  681. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  682. SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - jump_addr) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - jump_addr) >= -0x02000000);
  683. *buf_ptr |= (((sljit_sw)jump->u.label->addr - jump_addr) >> 2) & 0x00ffffff;
  684. }
  685. else {
  686. SLJIT_ASSERT(((sljit_sw)jump->u.target - jump_addr) <= 0x01ffffff && ((sljit_sw)jump->u.target - jump_addr) >= -0x02000000);
  687. *buf_ptr |= (((sljit_sw)jump->u.target - jump_addr) >> 2) & 0x00ffffff;
  688. }
  689. }
  690. else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
  691. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  692. jump->addr = (sljit_uw)code_ptr;
  693. code_ptr[0] = (sljit_uw)buf_ptr;
  694. code_ptr[1] = *buf_ptr;
  695. inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  696. code_ptr += 2;
  697. #else
  698. inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  699. #endif
  700. }
  701. else {
  702. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  703. if (jump->flags & IS_BL)
  704. buf_ptr--;
  705. if (*buf_ptr & (1 << 23))
  706. buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
  707. else
  708. buf_ptr += 1;
  709. *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
  710. #else
  711. inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  712. #endif
  713. }
  714. jump = jump->next;
  715. }
  716. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  717. const_ = compiler->consts;
  718. while (const_) {
  719. buf_ptr = (sljit_uw*)const_->addr;
  720. const_->addr = (sljit_uw)code_ptr;
  721. code_ptr[0] = (sljit_uw)buf_ptr;
  722. code_ptr[1] = *buf_ptr;
  723. if (*buf_ptr & (1 << 23))
  724. buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
  725. else
  726. buf_ptr += 1;
  727. /* Set the value again (can be a simple constant). */
  728. inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
  729. code_ptr += 2;
  730. const_ = const_->next;
  731. }
  732. #endif
  733. SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
  734. compiler->error = SLJIT_ERR_COMPILED;
  735. compiler->executable_offset = executable_offset;
  736. compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
  737. code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  738. code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  739. SLJIT_CACHE_FLUSH(code, code_ptr);
  740. return code;
  741. }
  742. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
  743. {
  744. switch (feature_type) {
  745. case SLJIT_HAS_FPU:
  746. #ifdef SLJIT_IS_FPU_AVAILABLE
  747. return SLJIT_IS_FPU_AVAILABLE;
  748. #else
  749. /* Available by default. */
  750. return 1;
  751. #endif
  752. case SLJIT_HAS_CLZ:
  753. case SLJIT_HAS_CMOV:
  754. return 1;
  755. default:
  756. return 0;
  757. }
  758. }
  759. /* --------------------------------------------------------------------- */
  760. /* Entry, exit */
  761. /* --------------------------------------------------------------------- */
  762. /* Creates an index in data_transfer_insts array. */
  763. #define WORD_SIZE 0x00
  764. #define BYTE_SIZE 0x01
  765. #define HALF_SIZE 0x02
  766. #define PRELOAD 0x03
  767. #define SIGNED 0x04
  768. #define LOAD_DATA 0x08
  769. /* Flag bits for emit_op. */
  770. #define ALLOW_IMM 0x10
  771. #define ALLOW_INV_IMM 0x20
  772. #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
  773. /* s/l - store/load (1 bit)
  774. u/s - signed/unsigned (1 bit)
  775. w/b/h/N - word/byte/half/NOT allowed (2 bit)
  776. Storing signed and unsigned values are the same operations. */
  777. static const sljit_uw data_transfer_insts[16] = {
  778. /* s u w */ 0xe5000000 /* str */,
  779. /* s u b */ 0xe5400000 /* strb */,
  780. /* s u h */ 0xe10000b0 /* strh */,
  781. /* s u N */ 0x00000000 /* not allowed */,
  782. /* s s w */ 0xe5000000 /* str */,
  783. /* s s b */ 0xe5400000 /* strb */,
  784. /* s s h */ 0xe10000b0 /* strh */,
  785. /* s s N */ 0x00000000 /* not allowed */,
  786. /* l u w */ 0xe5100000 /* ldr */,
  787. /* l u b */ 0xe5500000 /* ldrb */,
  788. /* l u h */ 0xe11000b0 /* ldrh */,
  789. /* l u p */ 0xf5500000 /* preload */,
  790. /* l s w */ 0xe5100000 /* ldr */,
  791. /* l s b */ 0xe11000d0 /* ldrsb */,
  792. /* l s h */ 0xe11000f0 /* ldrsh */,
  793. /* l s N */ 0x00000000 /* not allowed */,
  794. };
  795. #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
  796. (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
  797. /* Normal ldr/str instruction.
  798. Type2: ldrsb, ldrh, ldrsh */
  799. #define IS_TYPE1_TRANSFER(type) \
  800. (data_transfer_insts[(type) & 0xf] & 0x04000000)
  801. #define TYPE2_TRANSFER_IMM(imm) \
  802. (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
  803. static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
  804. sljit_s32 dst, sljit_sw dstw,
  805. sljit_s32 src1, sljit_sw src1w,
  806. sljit_s32 src2, sljit_sw src2w);
  807. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  808. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  809. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  810. {
  811. sljit_s32 args, size, i, tmp;
  812. sljit_uw push;
  813. CHECK_ERROR();
  814. CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  815. set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  816. /* Push saved registers, temporary registers
  817. stmdb sp!, {..., lr} */
  818. push = PUSH | (1 << 14);
  819. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  820. for (i = SLJIT_S0; i >= tmp; i--)
  821. push |= 1 << reg_map[i];
  822. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  823. push |= 1 << reg_map[i];
  824. FAIL_IF(push_inst(compiler, push));
  825. /* Stack must be aligned to 8 bytes: */
  826. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  827. local_size = ((size + local_size + 7) & ~7) - size;
  828. compiler->local_size = local_size;
  829. if (local_size > 0)
  830. FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
  831. args = get_arg_count(arg_types);
  832. if (args >= 1)
  833. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0) | RM(SLJIT_R0)));
  834. if (args >= 2)
  835. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S1) | RM(SLJIT_R1)));
  836. if (args >= 3)
  837. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S2) | RM(SLJIT_R2)));
  838. return SLJIT_SUCCESS;
  839. }
  840. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  841. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  842. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  843. {
  844. sljit_s32 size;
  845. CHECK_ERROR();
  846. CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  847. set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  848. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  849. compiler->local_size = ((size + local_size + 7) & ~7) - size;
  850. return SLJIT_SUCCESS;
  851. }
  852. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  853. {
  854. sljit_s32 i, tmp;
  855. sljit_uw pop;
  856. CHECK_ERROR();
  857. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  858. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  859. if (compiler->local_size > 0)
  860. FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  861. /* Push saved registers, temporary registers
  862. ldmia sp!, {..., pc} */
  863. pop = POP | (1 << 15);
  864. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  865. for (i = SLJIT_S0; i >= tmp; i--)
  866. pop |= 1 << reg_map[i];
  867. for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  868. pop |= 1 << reg_map[i];
  869. return push_inst(compiler, pop);
  870. }
  871. /* --------------------------------------------------------------------- */
  872. /* Operators */
  873. /* --------------------------------------------------------------------- */
  874. /* flags: */
  875. /* Arguments are swapped. */
  876. #define ARGS_SWAPPED 0x01
  877. /* Inverted immediate. */
  878. #define INV_IMM 0x02
  879. /* Source and destination is register. */
  880. #define MOVE_REG_CONV 0x04
  881. /* Unused return value. */
  882. #define UNUSED_RETURN 0x08
  883. /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
  884. #define SET_FLAGS (1 << 20)
  885. /* dst: reg
  886. src1: reg
  887. src2: reg or imm (if allowed)
  888. SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
  889. #define SRC2_IMM (1 << 25)
  890. #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
  891. SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
  892. if (compiler->shift_imm != 0x20) { \
  893. SLJIT_ASSERT(src1 == TMP_REG1); \
  894. SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
  895. \
  896. if (compiler->shift_imm != 0) \
  897. return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
  898. RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
  899. return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
  900. } \
  901. return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
  902. (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
  903. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  904. sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
  905. {
  906. switch (GET_OPCODE(op)) {
  907. case SLJIT_MOV:
  908. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  909. if (dst != src2) {
  910. if (src2 & SRC2_IMM) {
  911. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  912. }
  913. return push_inst(compiler, MOV | RD(dst) | RM(src2));
  914. }
  915. return SLJIT_SUCCESS;
  916. case SLJIT_MOV_U8:
  917. case SLJIT_MOV_S8:
  918. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  919. if (flags & MOVE_REG_CONV) {
  920. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  921. if (op == SLJIT_MOV_U8)
  922. return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
  923. FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
  924. return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
  925. #else
  926. return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
  927. #endif
  928. }
  929. else if (dst != src2) {
  930. SLJIT_ASSERT(src2 & SRC2_IMM);
  931. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  932. }
  933. return SLJIT_SUCCESS;
  934. case SLJIT_MOV_U16:
  935. case SLJIT_MOV_S16:
  936. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  937. if (flags & MOVE_REG_CONV) {
  938. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  939. FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
  940. return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
  941. #else
  942. return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
  943. #endif
  944. }
  945. else if (dst != src2) {
  946. SLJIT_ASSERT(src2 & SRC2_IMM);
  947. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  948. }
  949. return SLJIT_SUCCESS;
  950. case SLJIT_NOT:
  951. if (src2 & SRC2_IMM) {
  952. return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
  953. }
  954. return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
  955. case SLJIT_CLZ:
  956. SLJIT_ASSERT(!(flags & INV_IMM));
  957. SLJIT_ASSERT(!(src2 & SRC2_IMM));
  958. FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
  959. return SLJIT_SUCCESS;
  960. case SLJIT_ADD:
  961. SLJIT_ASSERT(!(flags & INV_IMM));
  962. if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
  963. return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  964. return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  965. case SLJIT_ADDC:
  966. SLJIT_ASSERT(!(flags & INV_IMM));
  967. return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  968. case SLJIT_SUB:
  969. SLJIT_ASSERT(!(flags & INV_IMM));
  970. if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
  971. return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  972. return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
  973. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  974. case SLJIT_SUBC:
  975. SLJIT_ASSERT(!(flags & INV_IMM));
  976. return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
  977. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  978. case SLJIT_MUL:
  979. SLJIT_ASSERT(!(flags & INV_IMM));
  980. SLJIT_ASSERT(!(src2 & SRC2_IMM));
  981. if (!HAS_FLAGS(op))
  982. return push_inst(compiler, MUL | (reg_map[dst] << 16) | (reg_map[src2] << 8) | reg_map[src1]);
  983. FAIL_IF(push_inst(compiler, SMULL | (reg_map[TMP_REG1] << 16) | (reg_map[dst] << 12) | (reg_map[src2] << 8) | reg_map[src1]));
  984. /* cmp TMP_REG1, dst asr #31. */
  985. return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
  986. case SLJIT_AND:
  987. return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
  988. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  989. case SLJIT_OR:
  990. SLJIT_ASSERT(!(flags & INV_IMM));
  991. return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  992. case SLJIT_XOR:
  993. SLJIT_ASSERT(!(flags & INV_IMM));
  994. return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  995. case SLJIT_SHL:
  996. EMIT_SHIFT_INS_AND_RETURN(0);
  997. case SLJIT_LSHR:
  998. EMIT_SHIFT_INS_AND_RETURN(1);
  999. case SLJIT_ASHR:
  1000. EMIT_SHIFT_INS_AND_RETURN(2);
  1001. }
  1002. SLJIT_UNREACHABLE();
  1003. return SLJIT_SUCCESS;
  1004. }
  1005. #undef EMIT_SHIFT_INS_AND_RETURN
  1006. /* Tests whether the immediate can be stored in the 12 bit imm field.
  1007. Returns with 0 if not possible. */
  1008. static sljit_uw get_imm(sljit_uw imm)
  1009. {
  1010. sljit_s32 rol;
  1011. if (imm <= 0xff)
  1012. return SRC2_IMM | imm;
  1013. if (!(imm & 0xff000000)) {
  1014. imm <<= 8;
  1015. rol = 8;
  1016. }
  1017. else {
  1018. imm = (imm << 24) | (imm >> 8);
  1019. rol = 0;
  1020. }
  1021. if (!(imm & 0xff000000)) {
  1022. imm <<= 8;
  1023. rol += 4;
  1024. }
  1025. if (!(imm & 0xf0000000)) {
  1026. imm <<= 4;
  1027. rol += 2;
  1028. }
  1029. if (!(imm & 0xc0000000)) {
  1030. imm <<= 2;
  1031. rol += 1;
  1032. }
  1033. if (!(imm & 0x00ffffff))
  1034. return SRC2_IMM | (imm >> 24) | (rol << 8);
  1035. else
  1036. return 0;
  1037. }
  1038. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1039. static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
  1040. {
  1041. sljit_uw mask;
  1042. sljit_uw imm1;
  1043. sljit_uw imm2;
  1044. sljit_s32 rol;
  1045. /* Step1: Search a zero byte (8 continous zero bit). */
  1046. mask = 0xff000000;
  1047. rol = 8;
  1048. while(1) {
  1049. if (!(imm & mask)) {
  1050. /* Rol imm by rol. */
  1051. imm = (imm << rol) | (imm >> (32 - rol));
  1052. /* Calculate arm rol. */
  1053. rol = 4 + (rol >> 1);
  1054. break;
  1055. }
  1056. rol += 2;
  1057. mask >>= 2;
  1058. if (mask & 0x3) {
  1059. /* rol by 8. */
  1060. imm = (imm << 8) | (imm >> 24);
  1061. mask = 0xff00;
  1062. rol = 24;
  1063. while (1) {
  1064. if (!(imm & mask)) {
  1065. /* Rol imm by rol. */
  1066. imm = (imm << rol) | (imm >> (32 - rol));
  1067. /* Calculate arm rol. */
  1068. rol = (rol >> 1) - 8;
  1069. break;
  1070. }
  1071. rol += 2;
  1072. mask >>= 2;
  1073. if (mask & 0x3)
  1074. return 0;
  1075. }
  1076. break;
  1077. }
  1078. }
  1079. /* The low 8 bit must be zero. */
  1080. SLJIT_ASSERT(!(imm & 0xff));
  1081. if (!(imm & 0xff000000)) {
  1082. imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
  1083. imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
  1084. }
  1085. else if (imm & 0xc0000000) {
  1086. imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
  1087. imm <<= 8;
  1088. rol += 4;
  1089. if (!(imm & 0xff000000)) {
  1090. imm <<= 8;
  1091. rol += 4;
  1092. }
  1093. if (!(imm & 0xf0000000)) {
  1094. imm <<= 4;
  1095. rol += 2;
  1096. }
  1097. if (!(imm & 0xc0000000)) {
  1098. imm <<= 2;
  1099. rol += 1;
  1100. }
  1101. if (!(imm & 0x00ffffff))
  1102. imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
  1103. else
  1104. return 0;
  1105. }
  1106. else {
  1107. if (!(imm & 0xf0000000)) {
  1108. imm <<= 4;
  1109. rol += 2;
  1110. }
  1111. if (!(imm & 0xc0000000)) {
  1112. imm <<= 2;
  1113. rol += 1;
  1114. }
  1115. imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
  1116. imm <<= 8;
  1117. rol += 4;
  1118. if (!(imm & 0xf0000000)) {
  1119. imm <<= 4;
  1120. rol += 2;
  1121. }
  1122. if (!(imm & 0xc0000000)) {
  1123. imm <<= 2;
  1124. rol += 1;
  1125. }
  1126. if (!(imm & 0x00ffffff))
  1127. imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
  1128. else
  1129. return 0;
  1130. }
  1131. FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
  1132. FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
  1133. return 1;
  1134. }
  1135. #endif
  1136. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
  1137. {
  1138. sljit_uw tmp;
  1139. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  1140. if (!(imm & ~0xffff))
  1141. return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
  1142. #endif
  1143. /* Create imm by 1 inst. */
  1144. tmp = get_imm(imm);
  1145. if (tmp)
  1146. return push_inst(compiler, MOV | RD(reg) | tmp);
  1147. tmp = get_imm(~imm);
  1148. if (tmp)
  1149. return push_inst(compiler, MVN | RD(reg) | tmp);
  1150. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1151. /* Create imm by 2 inst. */
  1152. FAIL_IF(generate_int(compiler, reg, imm, 1));
  1153. FAIL_IF(generate_int(compiler, reg, ~imm, 0));
  1154. /* Load integer. */
  1155. return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
  1156. #else
  1157. FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
  1158. if (imm <= 0xffff)
  1159. return SLJIT_SUCCESS;
  1160. return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
  1161. #endif
  1162. }
  1163. static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
  1164. sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
  1165. {
  1166. sljit_uw imm, offset_reg;
  1167. sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
  1168. SLJIT_ASSERT (arg & SLJIT_MEM);
  1169. SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
  1170. if ((arg & REG_MASK) == SLJIT_UNUSED) {
  1171. if (is_type1_transfer) {
  1172. FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xfff));
  1173. argw &= 0xfff;
  1174. }
  1175. else {
  1176. FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xff));
  1177. argw &= 0xff;
  1178. }
  1179. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
  1180. is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
  1181. }
  1182. if (arg & OFFS_REG_MASK) {
  1183. offset_reg = OFFS_REG(arg);
  1184. arg &= REG_MASK;
  1185. argw &= 0x3;
  1186. if (argw != 0 && !is_type1_transfer) {
  1187. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | (argw << 7)));
  1188. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
  1189. }
  1190. /* Bit 25: RM is offset. */
  1191. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
  1192. RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | (argw << 7)));
  1193. }
  1194. arg &= REG_MASK;
  1195. if (is_type1_transfer) {
  1196. if (argw > 0xfff) {
  1197. imm = get_imm(argw & ~0xfff);
  1198. if (imm) {
  1199. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
  1200. argw = argw & 0xfff;
  1201. arg = tmp_reg;
  1202. }
  1203. }
  1204. else if (argw < -0xfff) {
  1205. imm = get_imm(-argw & ~0xfff);
  1206. if (imm) {
  1207. FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
  1208. argw = -(-argw & 0xfff);
  1209. arg = tmp_reg;
  1210. }
  1211. }
  1212. if (argw >= 0 && argw <= 0xfff)
  1213. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
  1214. if (argw < 0 && argw >= -0xfff)
  1215. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
  1216. }
  1217. else {
  1218. if (argw > 0xff) {
  1219. imm = get_imm(argw & ~0xff);
  1220. if (imm) {
  1221. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
  1222. argw = argw & 0xff;
  1223. arg = tmp_reg;
  1224. }
  1225. }
  1226. else if (argw < -0xff) {
  1227. imm = get_imm(-argw & ~0xff);
  1228. if (imm) {
  1229. FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
  1230. argw = -(-argw & 0xff);
  1231. arg = tmp_reg;
  1232. }
  1233. }
  1234. if (argw >= 0 && argw <= 0xff)
  1235. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
  1236. if (argw < 0 && argw >= -0xff) {
  1237. argw = -argw;
  1238. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
  1239. }
  1240. }
  1241. FAIL_IF(load_immediate(compiler, tmp_reg, argw));
  1242. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
  1243. RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
  1244. }
  1245. static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
  1246. sljit_s32 dst, sljit_sw dstw,
  1247. sljit_s32 src1, sljit_sw src1w,
  1248. sljit_s32 src2, sljit_sw src2w)
  1249. {
  1250. /* src1 is reg or TMP_REG1
  1251. src2 is reg, TMP_REG2, or imm
  1252. result goes to TMP_REG2, so put result can use TMP_REG1. */
  1253. /* We prefers register and simple consts. */
  1254. sljit_s32 dst_reg;
  1255. sljit_s32 src1_reg;
  1256. sljit_s32 src2_reg;
  1257. sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
  1258. /* Destination check. */
  1259. if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED))
  1260. flags |= UNUSED_RETURN;
  1261. SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
  1262. src2_reg = 0;
  1263. do {
  1264. if (!(inp_flags & ALLOW_IMM))
  1265. break;
  1266. if (src2 & SLJIT_IMM) {
  1267. src2_reg = get_imm(src2w);
  1268. if (src2_reg)
  1269. break;
  1270. if (inp_flags & ALLOW_INV_IMM) {
  1271. src2_reg = get_imm(~src2w);
  1272. if (src2_reg) {
  1273. flags |= INV_IMM;
  1274. break;
  1275. }
  1276. }
  1277. if (GET_OPCODE(op) == SLJIT_ADD) {
  1278. src2_reg = get_imm(-src2w);
  1279. if (src2_reg) {
  1280. op = SLJIT_SUB | GET_ALL_FLAGS(op);
  1281. break;
  1282. }
  1283. }
  1284. if (GET_OPCODE(op) == SLJIT_SUB) {
  1285. src2_reg = get_imm(-src2w);
  1286. if (src2_reg) {
  1287. op = SLJIT_ADD | GET_ALL_FLAGS(op);
  1288. break;
  1289. }
  1290. }
  1291. }
  1292. if (src1 & SLJIT_IMM) {
  1293. src2_reg = get_imm(src1w);
  1294. if (src2_reg) {
  1295. flags |= ARGS_SWAPPED;
  1296. src1 = src2;
  1297. src1w = src2w;
  1298. break;
  1299. }
  1300. if (inp_flags & ALLOW_INV_IMM) {
  1301. src2_reg = get_imm(~src1w);
  1302. if (src2_reg) {
  1303. flags |= ARGS_SWAPPED | INV_IMM;
  1304. src1 = src2;
  1305. src1w = src2w;
  1306. break;
  1307. }
  1308. }
  1309. if (GET_OPCODE(op) == SLJIT_ADD) {
  1310. src2_reg = get_imm(-src1w);
  1311. if (src2_reg) {
  1312. /* Note: add is commutative operation. */
  1313. src1 = src2;
  1314. src1w = src2w;
  1315. op = SLJIT_SUB | GET_ALL_FLAGS(op);
  1316. break;
  1317. }
  1318. }
  1319. }
  1320. } while(0);
  1321. /* Source 1. */
  1322. if (FAST_IS_REG(src1))
  1323. src1_reg = src1;
  1324. else if (src1 & SLJIT_MEM) {
  1325. FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
  1326. src1_reg = TMP_REG1;
  1327. }
  1328. else {
  1329. FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
  1330. src1_reg = TMP_REG1;
  1331. }
  1332. /* Destination. */
  1333. dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  1334. if (op <= SLJIT_MOV_P) {
  1335. if (dst & SLJIT_MEM) {
  1336. if (inp_flags & BYTE_SIZE)
  1337. inp_flags &= ~SIGNED;
  1338. if (FAST_IS_REG(src2))
  1339. return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
  1340. }
  1341. if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
  1342. flags |= MOVE_REG_CONV;
  1343. }
  1344. /* Source 2. */
  1345. if (src2_reg == 0) {
  1346. src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
  1347. if (FAST_IS_REG(src2))
  1348. src2_reg = src2;
  1349. else if (src2 & SLJIT_MEM)
  1350. FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
  1351. else
  1352. FAIL_IF(load_immediate(compiler, src2_reg, src2w));
  1353. }
  1354. FAIL_IF(emit_single_op(compiler, op, flags, dst_reg, src1_reg, src2_reg));
  1355. if (!(dst & SLJIT_MEM))
  1356. return SLJIT_SUCCESS;
  1357. return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
  1358. }
  1359. #ifdef __cplusplus
  1360. extern "C" {
  1361. #endif
  1362. #if defined(__GNUC__)
  1363. extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
  1364. extern int __aeabi_idivmod(int numerator, int denominator);
  1365. #else
  1366. #error "Software divmod functions are needed"
  1367. #endif
  1368. #ifdef __cplusplus
  1369. }
  1370. #endif
  1371. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
  1372. {
  1373. sljit_sw saved_reg_list[3];
  1374. sljit_sw saved_reg_count;
  1375. CHECK_ERROR();
  1376. CHECK(check_sljit_emit_op0(compiler, op));
  1377. op = GET_OPCODE(op);
  1378. switch (op) {
  1379. case SLJIT_BREAKPOINT:
  1380. FAIL_IF(push_inst(compiler, BKPT));
  1381. break;
  1382. case SLJIT_NOP:
  1383. FAIL_IF(push_inst(compiler, NOP));
  1384. break;
  1385. case SLJIT_LMUL_UW:
  1386. case SLJIT_LMUL_SW:
  1387. return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
  1388. | (reg_map[SLJIT_R1] << 16)
  1389. | (reg_map[SLJIT_R0] << 12)
  1390. | (reg_map[SLJIT_R0] << 8)
  1391. | reg_map[SLJIT_R1]);
  1392. case SLJIT_DIVMOD_UW:
  1393. case SLJIT_DIVMOD_SW:
  1394. case SLJIT_DIV_UW:
  1395. case SLJIT_DIV_SW:
  1396. SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
  1397. SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
  1398. saved_reg_count = 0;
  1399. if (compiler->scratches >= 4)
  1400. saved_reg_list[saved_reg_count++] = 3;
  1401. if (compiler->scratches >= 3)
  1402. saved_reg_list[saved_reg_count++] = 2;
  1403. if (op >= SLJIT_DIV_UW)
  1404. saved_reg_list[saved_reg_count++] = 1;
  1405. if (saved_reg_count > 0) {
  1406. FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
  1407. | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
  1408. if (saved_reg_count >= 2) {
  1409. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1410. FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
  1411. }
  1412. if (saved_reg_count >= 3) {
  1413. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1414. FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
  1415. }
  1416. }
  1417. #if defined(__GNUC__)
  1418. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
  1419. ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
  1420. #else
  1421. #error "Software divmod functions are needed"
  1422. #endif
  1423. if (saved_reg_count > 0) {
  1424. if (saved_reg_count >= 3) {
  1425. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1426. FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
  1427. }
  1428. if (saved_reg_count >= 2) {
  1429. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1430. FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
  1431. }
  1432. return push_inst(compiler, 0xe49d0000 | (saved_reg_count >= 3 ? 16 : 8)
  1433. | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
  1434. }
  1435. return SLJIT_SUCCESS;
  1436. }
  1437. return SLJIT_SUCCESS;
  1438. }
  1439. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
  1440. sljit_s32 dst, sljit_sw dstw,
  1441. sljit_s32 src, sljit_sw srcw)
  1442. {
  1443. CHECK_ERROR();
  1444. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1445. ADJUST_LOCAL_OFFSET(dst, dstw);
  1446. ADJUST_LOCAL_OFFSET(src, srcw);
  1447. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
  1448. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  1449. if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
  1450. return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
  1451. #endif
  1452. return SLJIT_SUCCESS;
  1453. }
  1454. switch (GET_OPCODE(op)) {
  1455. case SLJIT_MOV:
  1456. case SLJIT_MOV_U32:
  1457. case SLJIT_MOV_S32:
  1458. case SLJIT_MOV_P:
  1459. return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
  1460. case SLJIT_MOV_U8:
  1461. return emit_op(compiler, SLJIT_MOV_U8, ALLOW_ANY_IMM | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u8)srcw : srcw);
  1462. case SLJIT_MOV_S8:
  1463. return emit_op(compiler, SLJIT_MOV_S8, ALLOW_ANY_IMM | SIGNED | BYTE_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s8)srcw : srcw);
  1464. case SLJIT_MOV_U16:
  1465. return emit_op(compiler, SLJIT_MOV_U16, ALLOW_ANY_IMM | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_u16)srcw : srcw);
  1466. case SLJIT_MOV_S16:
  1467. return emit_op(compiler, SLJIT_MOV_S16, ALLOW_ANY_IMM | SIGNED | HALF_SIZE, dst, dstw, TMP_REG1, 0, src, (src & SLJIT_IMM) ? (sljit_s16)srcw : srcw);
  1468. case SLJIT_NOT:
  1469. return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
  1470. case SLJIT_NEG:
  1471. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1472. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1473. compiler->skip_checks = 1;
  1474. #endif
  1475. return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
  1476. case SLJIT_CLZ:
  1477. return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
  1478. }
  1479. return SLJIT_SUCCESS;
  1480. }
  1481. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
  1482. sljit_s32 dst, sljit_sw dstw,
  1483. sljit_s32 src1, sljit_sw src1w,
  1484. sljit_s32 src2, sljit_sw src2w)
  1485. {
  1486. CHECK_ERROR();
  1487. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1488. ADJUST_LOCAL_OFFSET(dst, dstw);
  1489. ADJUST_LOCAL_OFFSET(src1, src1w);
  1490. ADJUST_LOCAL_OFFSET(src2, src2w);
  1491. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
  1492. return SLJIT_SUCCESS;
  1493. switch (GET_OPCODE(op)) {
  1494. case SLJIT_ADD:
  1495. case SLJIT_ADDC:
  1496. case SLJIT_SUB:
  1497. case SLJIT_SUBC:
  1498. case SLJIT_OR:
  1499. case SLJIT_XOR:
  1500. return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
  1501. case SLJIT_MUL:
  1502. return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
  1503. case SLJIT_AND:
  1504. return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
  1505. case SLJIT_SHL:
  1506. case SLJIT_LSHR:
  1507. case SLJIT_ASHR:
  1508. if (src2 & SLJIT_IMM) {
  1509. compiler->shift_imm = src2w & 0x1f;
  1510. return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
  1511. }
  1512. else {
  1513. compiler->shift_imm = 0x20;
  1514. return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
  1515. }
  1516. }
  1517. return SLJIT_SUCCESS;
  1518. }
  1519. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
  1520. {
  1521. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1522. return reg_map[reg];
  1523. }
  1524. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
  1525. {
  1526. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1527. return (freg_map[reg] << 1);
  1528. }
  1529. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
  1530. void *instruction, sljit_s32 size)
  1531. {
  1532. CHECK_ERROR();
  1533. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1534. return push_inst(compiler, *(sljit_uw*)instruction);
  1535. }
  1536. /* --------------------------------------------------------------------- */
  1537. /* Floating point operators */
  1538. /* --------------------------------------------------------------------- */
  1539. #define FPU_LOAD (1 << 20)
  1540. #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
  1541. ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
  1542. #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
  1543. ((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
  1544. static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
  1545. {
  1546. sljit_uw imm;
  1547. sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
  1548. SLJIT_ASSERT(arg & SLJIT_MEM);
  1549. arg &= ~SLJIT_MEM;
  1550. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  1551. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((argw & 0x3) << 7)));
  1552. arg = TMP_REG2;
  1553. argw = 0;
  1554. }
  1555. /* Fast loads and stores. */
  1556. if (arg) {
  1557. if (!(argw & ~0x3fc))
  1558. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
  1559. if (!(-argw & ~0x3fc))
  1560. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
  1561. imm = get_imm(argw & ~0x3fc);
  1562. if (imm) {
  1563. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
  1564. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
  1565. }
  1566. imm = get_imm(-argw & ~0x3fc);
  1567. if (imm) {
  1568. argw = -argw;
  1569. FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
  1570. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
  1571. }
  1572. }
  1573. if (arg) {
  1574. FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
  1575. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
  1576. }
  1577. else
  1578. FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
  1579. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
  1580. }
  1581. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
  1582. sljit_s32 dst, sljit_sw dstw,
  1583. sljit_s32 src, sljit_sw srcw)
  1584. {
  1585. op ^= SLJIT_F32_OP;
  1586. if (src & SLJIT_MEM) {
  1587. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
  1588. src = TMP_FREG1;
  1589. }
  1590. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
  1591. if (FAST_IS_REG(dst))
  1592. return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (freg_map[TMP_FREG1] << 16));
  1593. /* Store the integer value from a VFP register. */
  1594. return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
  1595. }
  1596. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
  1597. sljit_s32 dst, sljit_sw dstw,
  1598. sljit_s32 src, sljit_sw srcw)
  1599. {
  1600. sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1601. op ^= SLJIT_F32_OP;
  1602. if (FAST_IS_REG(src))
  1603. FAIL_IF(push_inst(compiler, VMOV | RD(src) | (freg_map[TMP_FREG1] << 16)));
  1604. else if (src & SLJIT_MEM) {
  1605. /* Load the integer value into a VFP register. */
  1606. FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
  1607. }
  1608. else {
  1609. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  1610. FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (freg_map[TMP_FREG1] << 16)));
  1611. }
  1612. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
  1613. if (dst & SLJIT_MEM)
  1614. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
  1615. return SLJIT_SUCCESS;
  1616. }
  1617. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
  1618. sljit_s32 src1, sljit_sw src1w,
  1619. sljit_s32 src2, sljit_sw src2w)
  1620. {
  1621. op ^= SLJIT_F32_OP;
  1622. if (src1 & SLJIT_MEM) {
  1623. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
  1624. src1 = TMP_FREG1;
  1625. }
  1626. if (src2 & SLJIT_MEM) {
  1627. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
  1628. src2 = TMP_FREG2;
  1629. }
  1630. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
  1631. return push_inst(compiler, VMRS);
  1632. }
  1633. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
  1634. sljit_s32 dst, sljit_sw dstw,
  1635. sljit_s32 src, sljit_sw srcw)
  1636. {
  1637. sljit_s32 dst_r;
  1638. CHECK_ERROR();
  1639. SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
  1640. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1641. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1642. if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
  1643. op ^= SLJIT_F32_OP;
  1644. if (src & SLJIT_MEM) {
  1645. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
  1646. src = dst_r;
  1647. }
  1648. switch (GET_OPCODE(op)) {
  1649. case SLJIT_MOV_F64:
  1650. if (src != dst_r) {
  1651. if (dst_r != TMP_FREG1)
  1652. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1653. else
  1654. dst_r = src;
  1655. }
  1656. break;
  1657. case SLJIT_NEG_F64:
  1658. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1659. break;
  1660. case SLJIT_ABS_F64:
  1661. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1662. break;
  1663. case SLJIT_CONV_F64_FROM_F32:
  1664. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1665. op ^= SLJIT_F32_OP;
  1666. break;
  1667. }
  1668. if (dst & SLJIT_MEM)
  1669. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
  1670. return SLJIT_SUCCESS;
  1671. }
  1672. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
  1673. sljit_s32 dst, sljit_sw dstw,
  1674. sljit_s32 src1, sljit_sw src1w,
  1675. sljit_s32 src2, sljit_sw src2w)
  1676. {
  1677. sljit_s32 dst_r;
  1678. CHECK_ERROR();
  1679. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1680. ADJUST_LOCAL_OFFSET(dst, dstw);
  1681. ADJUST_LOCAL_OFFSET(src1, src1w);
  1682. ADJUST_LOCAL_OFFSET(src2, src2w);
  1683. op ^= SLJIT_F32_OP;
  1684. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1685. if (src2 & SLJIT_MEM) {
  1686. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
  1687. src2 = TMP_FREG2;
  1688. }
  1689. if (src1 & SLJIT_MEM) {
  1690. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
  1691. src1 = TMP_FREG1;
  1692. }
  1693. switch (GET_OPCODE(op)) {
  1694. case SLJIT_ADD_F64:
  1695. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1696. break;
  1697. case SLJIT_SUB_F64:
  1698. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1699. break;
  1700. case SLJIT_MUL_F64:
  1701. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1702. break;
  1703. case SLJIT_DIV_F64:
  1704. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1705. break;
  1706. }
  1707. if (dst_r == TMP_FREG1)
  1708. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
  1709. return SLJIT_SUCCESS;
  1710. }
  1711. #undef FPU_LOAD
  1712. #undef EMIT_FPU_DATA_TRANSFER
  1713. /* --------------------------------------------------------------------- */
  1714. /* Other instructions */
  1715. /* --------------------------------------------------------------------- */
  1716. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  1717. {
  1718. CHECK_ERROR();
  1719. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1720. ADJUST_LOCAL_OFFSET(dst, dstw);
  1721. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1722. if (FAST_IS_REG(dst))
  1723. return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
  1724. /* Memory. */
  1725. return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
  1726. }
  1727. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  1728. {
  1729. CHECK_ERROR();
  1730. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1731. ADJUST_LOCAL_OFFSET(src, srcw);
  1732. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1733. if (FAST_IS_REG(src))
  1734. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
  1735. else
  1736. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
  1737. return push_inst(compiler, BX | RM(TMP_REG2));
  1738. }
  1739. /* --------------------------------------------------------------------- */
  1740. /* Conditional instructions */
  1741. /* --------------------------------------------------------------------- */
  1742. static sljit_uw get_cc(sljit_s32 type)
  1743. {
  1744. switch (type) {
  1745. case SLJIT_EQUAL:
  1746. case SLJIT_MUL_NOT_OVERFLOW:
  1747. case SLJIT_EQUAL_F64:
  1748. return 0x00000000;
  1749. case SLJIT_NOT_EQUAL:
  1750. case SLJIT_MUL_OVERFLOW:
  1751. case SLJIT_NOT_EQUAL_F64:
  1752. return 0x10000000;
  1753. case SLJIT_LESS:
  1754. case SLJIT_LESS_F64:
  1755. return 0x30000000;
  1756. case SLJIT_GREATER_EQUAL:
  1757. case SLJIT_GREATER_EQUAL_F64:
  1758. return 0x20000000;
  1759. case SLJIT_GREATER:
  1760. case SLJIT_GREATER_F64:
  1761. return 0x80000000;
  1762. case SLJIT_LESS_EQUAL:
  1763. case SLJIT_LESS_EQUAL_F64:
  1764. return 0x90000000;
  1765. case SLJIT_SIG_LESS:
  1766. return 0xb0000000;
  1767. case SLJIT_SIG_GREATER_EQUAL:
  1768. return 0xa0000000;
  1769. case SLJIT_SIG_GREATER:
  1770. return 0xc0000000;
  1771. case SLJIT_SIG_LESS_EQUAL:
  1772. return 0xd0000000;
  1773. case SLJIT_OVERFLOW:
  1774. case SLJIT_UNORDERED_F64:
  1775. return 0x60000000;
  1776. case SLJIT_NOT_OVERFLOW:
  1777. case SLJIT_ORDERED_F64:
  1778. return 0x70000000;
  1779. default:
  1780. SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
  1781. return 0xe0000000;
  1782. }
  1783. }
  1784. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1785. {
  1786. struct sljit_label *label;
  1787. CHECK_ERROR_PTR();
  1788. CHECK_PTR(check_sljit_emit_label(compiler));
  1789. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1790. return compiler->last_label;
  1791. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1792. PTR_FAIL_IF(!label);
  1793. set_label(label, compiler);
  1794. return label;
  1795. }
  1796. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
  1797. {
  1798. struct sljit_jump *jump;
  1799. CHECK_ERROR_PTR();
  1800. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1801. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1802. PTR_FAIL_IF(!jump);
  1803. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1804. type &= 0xff;
  1805. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  1806. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1807. if (type >= SLJIT_FAST_CALL)
  1808. PTR_FAIL_IF(prepare_blx(compiler));
  1809. PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
  1810. type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
  1811. if (jump->flags & SLJIT_REWRITABLE_JUMP) {
  1812. jump->addr = compiler->size;
  1813. compiler->patches++;
  1814. }
  1815. if (type >= SLJIT_FAST_CALL) {
  1816. jump->flags |= IS_BL;
  1817. PTR_FAIL_IF(emit_blx(compiler));
  1818. }
  1819. if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
  1820. jump->addr = compiler->size;
  1821. #else
  1822. if (type >= SLJIT_FAST_CALL)
  1823. jump->flags |= IS_BL;
  1824. PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
  1825. PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
  1826. jump->addr = compiler->size;
  1827. #endif
  1828. return jump;
  1829. }
  1830. #ifdef __SOFTFP__
  1831. static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
  1832. {
  1833. sljit_s32 stack_offset = 0;
  1834. sljit_s32 arg_count = 0;
  1835. sljit_s32 word_arg_offset = 0;
  1836. sljit_s32 float_arg_count = 0;
  1837. sljit_s32 types = 0;
  1838. sljit_s32 src_offset = 4 * sizeof(sljit_sw);
  1839. sljit_u8 offsets[4];
  1840. if (src && FAST_IS_REG(*src))
  1841. src_offset = reg_map[*src] * sizeof(sljit_sw);
  1842. arg_types >>= SLJIT_DEF_SHIFT;
  1843. while (arg_types) {
  1844. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  1845. switch (arg_types & SLJIT_DEF_MASK) {
  1846. case SLJIT_ARG_TYPE_F32:
  1847. offsets[arg_count] = (sljit_u8)stack_offset;
  1848. stack_offset += sizeof(sljit_f32);
  1849. arg_count++;
  1850. float_arg_count++;
  1851. break;
  1852. case SLJIT_ARG_TYPE_F64:
  1853. if (stack_offset & 0x7)
  1854. stack_offset += sizeof(sljit_sw);
  1855. offsets[arg_count] = (sljit_u8)stack_offset;
  1856. stack_offset += sizeof(sljit_f64);
  1857. arg_count++;
  1858. float_arg_count++;
  1859. break;
  1860. default:
  1861. offsets[arg_count] = (sljit_u8)stack_offset;
  1862. stack_offset += sizeof(sljit_sw);
  1863. arg_count++;
  1864. word_arg_offset += sizeof(sljit_sw);
  1865. break;
  1866. }
  1867. arg_types >>= SLJIT_DEF_SHIFT;
  1868. }
  1869. if (stack_offset > 16)
  1870. FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_offset - 16) + 0x7) & ~0x7)));
  1871. /* Process arguments in reversed direction. */
  1872. while (types) {
  1873. switch (types & SLJIT_DEF_MASK) {
  1874. case SLJIT_ARG_TYPE_F32:
  1875. arg_count--;
  1876. float_arg_count--;
  1877. stack_offset = offsets[arg_count];
  1878. if (stack_offset < 16) {
  1879. if (src_offset == stack_offset) {
  1880. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
  1881. *src = TMP_REG1;
  1882. }
  1883. FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
  1884. } else
  1885. FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1886. break;
  1887. case SLJIT_ARG_TYPE_F64:
  1888. arg_count--;
  1889. float_arg_count--;
  1890. stack_offset = offsets[arg_count];
  1891. SLJIT_ASSERT((stack_offset & 0x7) == 0);
  1892. if (stack_offset < 16) {
  1893. if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
  1894. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
  1895. *src = TMP_REG1;
  1896. }
  1897. FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
  1898. } else
  1899. FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1900. break;
  1901. default:
  1902. arg_count--;
  1903. word_arg_offset -= sizeof(sljit_sw);
  1904. stack_offset = offsets[arg_count];
  1905. SLJIT_ASSERT(stack_offset >= word_arg_offset);
  1906. if (stack_offset != word_arg_offset) {
  1907. if (stack_offset < 16) {
  1908. if (src_offset == stack_offset) {
  1909. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
  1910. *src = TMP_REG1;
  1911. }
  1912. else if (src_offset == word_arg_offset) {
  1913. *src = 1 + (stack_offset >> 2);
  1914. src_offset = stack_offset;
  1915. }
  1916. FAIL_IF(push_inst(compiler, MOV | (stack_offset << 10) | (word_arg_offset >> 2)));
  1917. } else
  1918. FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (stack_offset - 16)));
  1919. }
  1920. break;
  1921. }
  1922. types >>= SLJIT_DEF_SHIFT;
  1923. }
  1924. return SLJIT_SUCCESS;
  1925. }
  1926. static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1927. {
  1928. sljit_s32 stack_size = 0;
  1929. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
  1930. FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
  1931. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
  1932. FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
  1933. arg_types >>= SLJIT_DEF_SHIFT;
  1934. while (arg_types) {
  1935. switch (arg_types & SLJIT_DEF_MASK) {
  1936. case SLJIT_ARG_TYPE_F32:
  1937. stack_size += sizeof(sljit_f32);
  1938. break;
  1939. case SLJIT_ARG_TYPE_F64:
  1940. if (stack_size & 0x7)
  1941. stack_size += sizeof(sljit_sw);
  1942. stack_size += sizeof(sljit_f64);
  1943. break;
  1944. default:
  1945. stack_size += sizeof(sljit_sw);
  1946. break;
  1947. }
  1948. arg_types >>= SLJIT_DEF_SHIFT;
  1949. }
  1950. if (stack_size <= 16)
  1951. return SLJIT_SUCCESS;
  1952. return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_size - 16) + 0x7) & ~0x7));
  1953. }
  1954. #else /* !__SOFTFP__ */
  1955. static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1956. {
  1957. sljit_u32 remap = 0;
  1958. sljit_u32 offset = 0;
  1959. sljit_u32 new_offset, mask;
  1960. /* Remove return value. */
  1961. arg_types >>= SLJIT_DEF_SHIFT;
  1962. while (arg_types) {
  1963. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
  1964. new_offset = 0;
  1965. mask = 1;
  1966. while (remap & mask) {
  1967. new_offset++;
  1968. mask <<= 1;
  1969. }
  1970. remap |= mask;
  1971. if (offset != new_offset)
  1972. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
  1973. 0, (new_offset >> 1) + 1, (offset >> 1) + 1, 0) | ((new_offset & 0x1) ? 0x400000 : 0)));
  1974. offset += 2;
  1975. }
  1976. else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
  1977. new_offset = 0;
  1978. mask = 3;
  1979. while (remap & mask) {
  1980. new_offset += 2;
  1981. mask <<= 2;
  1982. }
  1983. remap |= mask;
  1984. if (offset != new_offset)
  1985. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, SLJIT_F32_OP, (new_offset >> 1) + 1, (offset >> 1) + 1, 0)));
  1986. offset += 2;
  1987. }
  1988. arg_types >>= SLJIT_DEF_SHIFT;
  1989. }
  1990. return SLJIT_SUCCESS;
  1991. }
  1992. #endif /* __SOFTFP__ */
  1993. #undef EMIT_FPU_OPERATION
  1994. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  1995. sljit_s32 arg_types)
  1996. {
  1997. #ifdef __SOFTFP__
  1998. struct sljit_jump *jump;
  1999. #endif
  2000. CHECK_ERROR_PTR();
  2001. CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  2002. #ifdef __SOFTFP__
  2003. PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
  2004. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2005. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2006. compiler->skip_checks = 1;
  2007. #endif
  2008. jump = sljit_emit_jump(compiler, type);
  2009. PTR_FAIL_IF(jump == NULL);
  2010. PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
  2011. return jump;
  2012. #else /* !__SOFTFP__ */
  2013. PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  2014. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2015. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2016. compiler->skip_checks = 1;
  2017. #endif
  2018. return sljit_emit_jump(compiler, type);
  2019. #endif /* __SOFTFP__ */
  2020. }
  2021. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
  2022. {
  2023. struct sljit_jump *jump;
  2024. CHECK_ERROR();
  2025. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  2026. ADJUST_LOCAL_OFFSET(src, srcw);
  2027. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  2028. if (!(src & SLJIT_IMM)) {
  2029. if (FAST_IS_REG(src)) {
  2030. SLJIT_ASSERT(reg_map[src] != 14);
  2031. return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
  2032. }
  2033. SLJIT_ASSERT(src & SLJIT_MEM);
  2034. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
  2035. return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
  2036. }
  2037. /* These jumps are converted to jump/call instructions when possible. */
  2038. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  2039. FAIL_IF(!jump);
  2040. set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
  2041. jump->u.target = srcw;
  2042. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  2043. if (type >= SLJIT_FAST_CALL)
  2044. FAIL_IF(prepare_blx(compiler));
  2045. FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0), 0));
  2046. if (type >= SLJIT_FAST_CALL)
  2047. FAIL_IF(emit_blx(compiler));
  2048. #else
  2049. FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
  2050. FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
  2051. #endif
  2052. jump->addr = compiler->size;
  2053. return SLJIT_SUCCESS;
  2054. }
  2055. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  2056. sljit_s32 arg_types,
  2057. sljit_s32 src, sljit_sw srcw)
  2058. {
  2059. CHECK_ERROR();
  2060. CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  2061. #ifdef __SOFTFP__
  2062. if (src & SLJIT_MEM) {
  2063. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
  2064. src = TMP_REG1;
  2065. }
  2066. FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
  2067. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2068. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2069. compiler->skip_checks = 1;
  2070. #endif
  2071. FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  2072. return softfloat_post_call_with_args(compiler, arg_types);
  2073. #else /* !__SOFTFP__ */
  2074. FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  2075. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2076. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2077. compiler->skip_checks = 1;
  2078. #endif
  2079. return sljit_emit_ijump(compiler, type, src, srcw);
  2080. #endif /* __SOFTFP__ */
  2081. }
  2082. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
  2083. sljit_s32 dst, sljit_sw dstw,
  2084. sljit_s32 type)
  2085. {
  2086. sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
  2087. sljit_uw cc, ins;
  2088. CHECK_ERROR();
  2089. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
  2090. ADJUST_LOCAL_OFFSET(dst, dstw);
  2091. op = GET_OPCODE(op);
  2092. cc = get_cc(type & 0xff);
  2093. dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
  2094. if (op < SLJIT_ADD) {
  2095. FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
  2096. FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
  2097. if (dst & SLJIT_MEM)
  2098. return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
  2099. return SLJIT_SUCCESS;
  2100. }
  2101. ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
  2102. if (dst & SLJIT_MEM)
  2103. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
  2104. FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
  2105. if (op == SLJIT_AND)
  2106. FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
  2107. if (dst & SLJIT_MEM)
  2108. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
  2109. if (flags & SLJIT_SET_Z)
  2110. return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
  2111. return SLJIT_SUCCESS;
  2112. }
  2113. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
  2114. sljit_s32 dst_reg,
  2115. sljit_s32 src, sljit_sw srcw)
  2116. {
  2117. sljit_uw cc, tmp;
  2118. CHECK_ERROR();
  2119. CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
  2120. dst_reg &= ~SLJIT_I32_OP;
  2121. cc = get_cc(type & 0xff);
  2122. if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
  2123. tmp = get_imm(srcw);
  2124. if (tmp)
  2125. return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
  2126. tmp = get_imm(~srcw);
  2127. if (tmp)
  2128. return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
  2129. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  2130. tmp = (sljit_uw) srcw;
  2131. FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
  2132. if (tmp <= 0xffff)
  2133. return SLJIT_SUCCESS;
  2134. return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
  2135. #else
  2136. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  2137. src = TMP_REG1;
  2138. #endif
  2139. }
  2140. return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
  2141. }
  2142. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
  2143. sljit_s32 reg,
  2144. sljit_s32 mem, sljit_sw memw)
  2145. {
  2146. sljit_s32 flags;
  2147. sljit_uw is_type1_transfer, inst;
  2148. CHECK_ERROR();
  2149. CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
  2150. is_type1_transfer = 1;
  2151. switch (type & 0xff) {
  2152. case SLJIT_MOV:
  2153. case SLJIT_MOV_U32:
  2154. case SLJIT_MOV_S32:
  2155. case SLJIT_MOV_P:
  2156. flags = WORD_SIZE;
  2157. break;
  2158. case SLJIT_MOV_U8:
  2159. flags = BYTE_SIZE;
  2160. break;
  2161. case SLJIT_MOV_S8:
  2162. if (!(type & SLJIT_MEM_STORE))
  2163. is_type1_transfer = 0;
  2164. flags = BYTE_SIZE | SIGNED;
  2165. break;
  2166. case SLJIT_MOV_U16:
  2167. is_type1_transfer = 0;
  2168. flags = HALF_SIZE;
  2169. break;
  2170. case SLJIT_MOV_S16:
  2171. is_type1_transfer = 0;
  2172. flags = HALF_SIZE | SIGNED;
  2173. break;
  2174. default:
  2175. SLJIT_UNREACHABLE();
  2176. flags = WORD_SIZE;
  2177. break;
  2178. }
  2179. if (!(type & SLJIT_MEM_STORE))
  2180. flags |= LOAD_DATA;
  2181. SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
  2182. if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
  2183. if (!is_type1_transfer && memw != 0)
  2184. return SLJIT_ERR_UNSUPPORTED;
  2185. }
  2186. else {
  2187. if (is_type1_transfer) {
  2188. if (memw > 4095 && memw < -4095)
  2189. return SLJIT_ERR_UNSUPPORTED;
  2190. }
  2191. else {
  2192. if (memw > 255 && memw < -255)
  2193. return SLJIT_ERR_UNSUPPORTED;
  2194. }
  2195. }
  2196. if (type & SLJIT_MEM_SUPP)
  2197. return SLJIT_SUCCESS;
  2198. if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
  2199. memw &= 0x3;
  2200. inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | (memw << 7));
  2201. if (is_type1_transfer)
  2202. inst |= (1 << 25);
  2203. if (type & SLJIT_MEM_PRE)
  2204. inst |= (1 << 21);
  2205. else
  2206. inst ^= (1 << 24);
  2207. return push_inst(compiler, inst);
  2208. }
  2209. inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
  2210. if (type & SLJIT_MEM_PRE)
  2211. inst |= (1 << 21);
  2212. else
  2213. inst ^= (1 << 24);
  2214. if (is_type1_transfer) {
  2215. if (memw >= 0)
  2216. inst |= (1 << 23);
  2217. else
  2218. memw = -memw;
  2219. return push_inst(compiler, inst | memw);
  2220. }
  2221. if (memw >= 0)
  2222. inst |= (1 << 23);
  2223. else
  2224. memw = -memw;
  2225. return push_inst(compiler, inst | TYPE2_TRANSFER_IMM(memw));
  2226. }
  2227. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
  2228. {
  2229. struct sljit_const *const_;
  2230. sljit_s32 reg;
  2231. CHECK_ERROR_PTR();
  2232. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  2233. ADJUST_LOCAL_OFFSET(dst, dstw);
  2234. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  2235. PTR_FAIL_IF(!const_);
  2236. reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  2237. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  2238. PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), init_value));
  2239. compiler->patches++;
  2240. #else
  2241. PTR_FAIL_IF(emit_imm(compiler, reg, init_value));
  2242. #endif
  2243. set_const(const_, compiler);
  2244. if (dst & SLJIT_MEM)
  2245. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
  2246. return const_;
  2247. }
  2248. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  2249. {
  2250. inline_set_jump_addr(addr, executable_offset, new_target, 1);
  2251. }
  2252. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  2253. {
  2254. inline_set_const(addr, executable_offset, new_constant, 1);
  2255. }