sljitNativeARM_32.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734
  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_uw next_addr;
  521. sljit_sw executable_offset;
  522. sljit_sw addr;
  523. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  524. sljit_uw cpool_size;
  525. sljit_uw cpool_skip_alignment;
  526. sljit_uw cpool_current_index;
  527. sljit_uw *cpool_start_address;
  528. sljit_uw *last_pc_patch;
  529. struct future_patch *first_patch;
  530. #endif
  531. struct sljit_label *label;
  532. struct sljit_jump *jump;
  533. struct sljit_const *const_;
  534. struct sljit_put_label *put_label;
  535. CHECK_ERROR_PTR();
  536. CHECK_PTR(check_sljit_generate_code(compiler));
  537. reverse_buf(compiler);
  538. /* Second code generation pass. */
  539. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  540. size = compiler->size + (compiler->patches << 1);
  541. if (compiler->cpool_fill > 0)
  542. size += compiler->cpool_fill + CONST_POOL_ALIGNMENT - 1;
  543. #else
  544. size = compiler->size;
  545. #endif
  546. code = (sljit_uw*)SLJIT_MALLOC_EXEC(size * sizeof(sljit_uw));
  547. PTR_FAIL_WITH_EXEC_IF(code);
  548. buf = compiler->buf;
  549. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  550. cpool_size = 0;
  551. cpool_skip_alignment = 0;
  552. cpool_current_index = 0;
  553. cpool_start_address = NULL;
  554. first_patch = NULL;
  555. last_pc_patch = code;
  556. #endif
  557. code_ptr = code;
  558. word_count = 0;
  559. next_addr = 1;
  560. executable_offset = SLJIT_EXEC_OFFSET(code);
  561. label = compiler->labels;
  562. jump = compiler->jumps;
  563. const_ = compiler->consts;
  564. put_label = compiler->put_labels;
  565. if (label && label->size == 0) {
  566. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  567. label = label->next;
  568. }
  569. do {
  570. buf_ptr = (sljit_uw*)buf->memory;
  571. buf_end = buf_ptr + (buf->used_size >> 2);
  572. do {
  573. word_count++;
  574. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  575. if (cpool_size > 0) {
  576. if (cpool_skip_alignment > 0) {
  577. buf_ptr++;
  578. cpool_skip_alignment--;
  579. }
  580. else {
  581. if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
  582. SLJIT_FREE_EXEC(code);
  583. compiler->error = SLJIT_ERR_ALLOC_FAILED;
  584. return NULL;
  585. }
  586. buf_ptr++;
  587. if (++cpool_current_index >= cpool_size) {
  588. SLJIT_ASSERT(!first_patch);
  589. cpool_size = 0;
  590. if (label && label->size == word_count) {
  591. /* Points after the current instruction. */
  592. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  593. label->size = code_ptr - code;
  594. label = label->next;
  595. next_addr = compute_next_addr(label, jump, const_, put_label);
  596. }
  597. }
  598. }
  599. }
  600. else if ((*buf_ptr & 0xff000000) != PUSH_POOL) {
  601. #endif
  602. *code_ptr = *buf_ptr++;
  603. if (next_addr == word_count) {
  604. SLJIT_ASSERT(!label || label->size >= word_count);
  605. SLJIT_ASSERT(!jump || jump->addr >= word_count);
  606. SLJIT_ASSERT(!const_ || const_->addr >= word_count);
  607. SLJIT_ASSERT(!put_label || put_label->addr >= word_count);
  608. /* These structures are ordered by their address. */
  609. if (jump && jump->addr == word_count) {
  610. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  611. if (detect_jump_type(jump, code_ptr, code, executable_offset))
  612. code_ptr--;
  613. jump->addr = (sljit_uw)code_ptr;
  614. #else
  615. jump->addr = (sljit_uw)(code_ptr - 2);
  616. if (detect_jump_type(jump, code_ptr, code, executable_offset))
  617. code_ptr -= 2;
  618. #endif
  619. jump = jump->next;
  620. }
  621. if (label && label->size == word_count) {
  622. /* code_ptr can be affected above. */
  623. label->addr = (sljit_uw)SLJIT_ADD_EXEC_OFFSET(code_ptr + 1, executable_offset);
  624. label->size = (code_ptr + 1) - code;
  625. label = label->next;
  626. }
  627. if (const_ && const_->addr == word_count) {
  628. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  629. const_->addr = (sljit_uw)code_ptr;
  630. #else
  631. const_->addr = (sljit_uw)(code_ptr - 1);
  632. #endif
  633. const_ = const_->next;
  634. }
  635. if (put_label && put_label->addr == word_count) {
  636. SLJIT_ASSERT(put_label->label);
  637. put_label->addr = (sljit_uw)code_ptr;
  638. put_label = put_label->next;
  639. }
  640. next_addr = compute_next_addr(label, jump, const_, put_label);
  641. }
  642. code_ptr++;
  643. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  644. }
  645. else {
  646. /* Fortunately, no need to shift. */
  647. cpool_size = *buf_ptr++ & ~PUSH_POOL;
  648. SLJIT_ASSERT(cpool_size > 0);
  649. cpool_start_address = ALIGN_INSTRUCTION(code_ptr + 1);
  650. cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, cpool_size);
  651. if (cpool_current_index > 0) {
  652. /* Unconditional branch. */
  653. *code_ptr = B | (((cpool_start_address - code_ptr) + cpool_current_index - 2) & ~PUSH_POOL);
  654. code_ptr = cpool_start_address + cpool_current_index;
  655. }
  656. cpool_skip_alignment = CONST_POOL_ALIGNMENT - 1;
  657. cpool_current_index = 0;
  658. last_pc_patch = code_ptr;
  659. }
  660. #endif
  661. } while (buf_ptr < buf_end);
  662. buf = buf->next;
  663. } while (buf);
  664. SLJIT_ASSERT(!label);
  665. SLJIT_ASSERT(!jump);
  666. SLJIT_ASSERT(!const_);
  667. SLJIT_ASSERT(!put_label);
  668. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  669. SLJIT_ASSERT(cpool_size == 0);
  670. if (compiler->cpool_fill > 0) {
  671. cpool_start_address = ALIGN_INSTRUCTION(code_ptr);
  672. cpool_current_index = patch_pc_relative_loads(last_pc_patch, code_ptr, cpool_start_address, compiler->cpool_fill);
  673. if (cpool_current_index > 0)
  674. code_ptr = cpool_start_address + cpool_current_index;
  675. buf_ptr = compiler->cpool;
  676. buf_end = buf_ptr + compiler->cpool_fill;
  677. cpool_current_index = 0;
  678. while (buf_ptr < buf_end) {
  679. if (SLJIT_UNLIKELY(resolve_const_pool_index(compiler, &first_patch, cpool_current_index, cpool_start_address, buf_ptr))) {
  680. SLJIT_FREE_EXEC(code);
  681. compiler->error = SLJIT_ERR_ALLOC_FAILED;
  682. return NULL;
  683. }
  684. buf_ptr++;
  685. cpool_current_index++;
  686. }
  687. SLJIT_ASSERT(!first_patch);
  688. }
  689. #endif
  690. jump = compiler->jumps;
  691. while (jump) {
  692. buf_ptr = (sljit_uw *)jump->addr;
  693. if (jump->flags & PATCH_B) {
  694. addr = (sljit_sw)SLJIT_ADD_EXEC_OFFSET(buf_ptr + 2, executable_offset);
  695. if (!(jump->flags & JUMP_ADDR)) {
  696. SLJIT_ASSERT(jump->flags & JUMP_LABEL);
  697. SLJIT_ASSERT(((sljit_sw)jump->u.label->addr - addr) <= 0x01ffffff && ((sljit_sw)jump->u.label->addr - addr) >= -0x02000000);
  698. *buf_ptr |= (((sljit_sw)jump->u.label->addr - addr) >> 2) & 0x00ffffff;
  699. }
  700. else {
  701. SLJIT_ASSERT(((sljit_sw)jump->u.target - addr) <= 0x01ffffff && ((sljit_sw)jump->u.target - addr) >= -0x02000000);
  702. *buf_ptr |= (((sljit_sw)jump->u.target - addr) >> 2) & 0x00ffffff;
  703. }
  704. }
  705. else if (jump->flags & SLJIT_REWRITABLE_JUMP) {
  706. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  707. jump->addr = (sljit_uw)code_ptr;
  708. code_ptr[0] = (sljit_uw)buf_ptr;
  709. code_ptr[1] = *buf_ptr;
  710. inline_set_jump_addr((sljit_uw)code_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  711. code_ptr += 2;
  712. #else
  713. inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  714. #endif
  715. }
  716. else {
  717. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  718. if (jump->flags & IS_BL)
  719. buf_ptr--;
  720. if (*buf_ptr & (1 << 23))
  721. buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
  722. else
  723. buf_ptr += 1;
  724. *buf_ptr = (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target;
  725. #else
  726. inline_set_jump_addr((sljit_uw)buf_ptr, executable_offset, (jump->flags & JUMP_LABEL) ? jump->u.label->addr : jump->u.target, 0);
  727. #endif
  728. }
  729. jump = jump->next;
  730. }
  731. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  732. const_ = compiler->consts;
  733. while (const_) {
  734. buf_ptr = (sljit_uw*)const_->addr;
  735. const_->addr = (sljit_uw)code_ptr;
  736. code_ptr[0] = (sljit_uw)buf_ptr;
  737. code_ptr[1] = *buf_ptr;
  738. if (*buf_ptr & (1 << 23))
  739. buf_ptr += ((*buf_ptr & 0xfff) >> 2) + 2;
  740. else
  741. buf_ptr += 1;
  742. /* Set the value again (can be a simple constant). */
  743. inline_set_const((sljit_uw)code_ptr, executable_offset, *buf_ptr, 0);
  744. code_ptr += 2;
  745. const_ = const_->next;
  746. }
  747. #endif
  748. put_label = compiler->put_labels;
  749. while (put_label) {
  750. addr = put_label->label->addr;
  751. buf_ptr = (sljit_uw*)put_label->addr;
  752. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  753. SLJIT_ASSERT((buf_ptr[0] & 0xffff0000) == 0xe59f0000);
  754. buf_ptr[((buf_ptr[0] & 0xfff) >> 2) + 2] = addr;
  755. #else
  756. SLJIT_ASSERT((buf_ptr[-1] & 0xfff00000) == MOVW && (buf_ptr[0] & 0xfff00000) == MOVT);
  757. buf_ptr[-1] |= ((addr << 4) & 0xf0000) | (addr & 0xfff);
  758. buf_ptr[0] |= ((addr >> 12) & 0xf0000) | ((addr >> 16) & 0xfff);
  759. #endif
  760. put_label = put_label->next;
  761. }
  762. SLJIT_ASSERT(code_ptr - code <= (sljit_s32)size);
  763. compiler->error = SLJIT_ERR_COMPILED;
  764. compiler->executable_offset = executable_offset;
  765. compiler->executable_size = (code_ptr - code) * sizeof(sljit_uw);
  766. code = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code, executable_offset);
  767. code_ptr = (sljit_uw *)SLJIT_ADD_EXEC_OFFSET(code_ptr, executable_offset);
  768. SLJIT_CACHE_FLUSH(code, code_ptr);
  769. return code;
  770. }
  771. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type)
  772. {
  773. switch (feature_type) {
  774. case SLJIT_HAS_FPU:
  775. #ifdef SLJIT_IS_FPU_AVAILABLE
  776. return SLJIT_IS_FPU_AVAILABLE;
  777. #else
  778. /* Available by default. */
  779. return 1;
  780. #endif
  781. case SLJIT_HAS_CLZ:
  782. case SLJIT_HAS_CMOV:
  783. return 1;
  784. default:
  785. return 0;
  786. }
  787. }
  788. /* --------------------------------------------------------------------- */
  789. /* Entry, exit */
  790. /* --------------------------------------------------------------------- */
  791. /* Creates an index in data_transfer_insts array. */
  792. #define WORD_SIZE 0x00
  793. #define BYTE_SIZE 0x01
  794. #define HALF_SIZE 0x02
  795. #define PRELOAD 0x03
  796. #define SIGNED 0x04
  797. #define LOAD_DATA 0x08
  798. /* Flag bits for emit_op. */
  799. #define ALLOW_IMM 0x10
  800. #define ALLOW_INV_IMM 0x20
  801. #define ALLOW_ANY_IMM (ALLOW_IMM | ALLOW_INV_IMM)
  802. /* s/l - store/load (1 bit)
  803. u/s - signed/unsigned (1 bit)
  804. w/b/h/N - word/byte/half/NOT allowed (2 bit)
  805. Storing signed and unsigned values are the same operations. */
  806. static const sljit_uw data_transfer_insts[16] = {
  807. /* s u w */ 0xe5000000 /* str */,
  808. /* s u b */ 0xe5400000 /* strb */,
  809. /* s u h */ 0xe10000b0 /* strh */,
  810. /* s u N */ 0x00000000 /* not allowed */,
  811. /* s s w */ 0xe5000000 /* str */,
  812. /* s s b */ 0xe5400000 /* strb */,
  813. /* s s h */ 0xe10000b0 /* strh */,
  814. /* s s N */ 0x00000000 /* not allowed */,
  815. /* l u w */ 0xe5100000 /* ldr */,
  816. /* l u b */ 0xe5500000 /* ldrb */,
  817. /* l u h */ 0xe11000b0 /* ldrh */,
  818. /* l u p */ 0xf5500000 /* preload */,
  819. /* l s w */ 0xe5100000 /* ldr */,
  820. /* l s b */ 0xe11000d0 /* ldrsb */,
  821. /* l s h */ 0xe11000f0 /* ldrsh */,
  822. /* l s N */ 0x00000000 /* not allowed */,
  823. };
  824. #define EMIT_DATA_TRANSFER(type, add, target_reg, base_reg, arg) \
  825. (data_transfer_insts[(type) & 0xf] | ((add) << 23) | RD(target_reg) | RN(base_reg) | (arg))
  826. /* Normal ldr/str instruction.
  827. Type2: ldrsb, ldrh, ldrsh */
  828. #define IS_TYPE1_TRANSFER(type) \
  829. (data_transfer_insts[(type) & 0xf] & 0x04000000)
  830. #define TYPE2_TRANSFER_IMM(imm) \
  831. (((imm) & 0xf) | (((imm) & 0xf0) << 4) | (1 << 22))
  832. static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
  833. sljit_s32 dst, sljit_sw dstw,
  834. sljit_s32 src1, sljit_sw src1w,
  835. sljit_s32 src2, sljit_sw src2w);
  836. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  837. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  838. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  839. {
  840. sljit_s32 args, size, i, tmp;
  841. sljit_uw push;
  842. CHECK_ERROR();
  843. CHECK(check_sljit_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  844. set_emit_enter(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  845. /* Push saved registers, temporary registers
  846. stmdb sp!, {..., lr} */
  847. push = PUSH | (1 << 14);
  848. tmp = saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - saveds) : SLJIT_FIRST_SAVED_REG;
  849. for (i = SLJIT_S0; i >= tmp; i--)
  850. push |= 1 << reg_map[i];
  851. for (i = scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  852. push |= 1 << reg_map[i];
  853. FAIL_IF(push_inst(compiler, push));
  854. /* Stack must be aligned to 8 bytes: */
  855. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  856. local_size = ((size + local_size + 7) & ~7) - size;
  857. compiler->local_size = local_size;
  858. if (local_size > 0)
  859. FAIL_IF(emit_op(compiler, SLJIT_SUB, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, local_size));
  860. args = get_arg_count(arg_types);
  861. if (args >= 1)
  862. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S0) | RM(SLJIT_R0)));
  863. if (args >= 2)
  864. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S1) | RM(SLJIT_R1)));
  865. if (args >= 3)
  866. FAIL_IF(push_inst(compiler, MOV | RD(SLJIT_S2) | RM(SLJIT_R2)));
  867. return SLJIT_SUCCESS;
  868. }
  869. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  870. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  871. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size)
  872. {
  873. sljit_s32 size;
  874. CHECK_ERROR();
  875. CHECK(check_sljit_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size));
  876. set_set_context(compiler, options, arg_types, scratches, saveds, fscratches, fsaveds, local_size);
  877. size = GET_SAVED_REGISTERS_SIZE(scratches, saveds, 1);
  878. compiler->local_size = ((size + local_size + 7) & ~7) - size;
  879. return SLJIT_SUCCESS;
  880. }
  881. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 src, sljit_sw srcw)
  882. {
  883. sljit_s32 i, tmp;
  884. sljit_uw pop;
  885. CHECK_ERROR();
  886. CHECK(check_sljit_emit_return(compiler, op, src, srcw));
  887. FAIL_IF(emit_mov_before_return(compiler, op, src, srcw));
  888. if (compiler->local_size > 0)
  889. FAIL_IF(emit_op(compiler, SLJIT_ADD, ALLOW_IMM, SLJIT_SP, 0, SLJIT_SP, 0, SLJIT_IMM, compiler->local_size));
  890. /* Push saved registers, temporary registers
  891. ldmia sp!, {..., pc} */
  892. pop = POP | (1 << 15);
  893. tmp = compiler->saveds < SLJIT_NUMBER_OF_SAVED_REGISTERS ? (SLJIT_S0 + 1 - compiler->saveds) : SLJIT_FIRST_SAVED_REG;
  894. for (i = SLJIT_S0; i >= tmp; i--)
  895. pop |= 1 << reg_map[i];
  896. for (i = compiler->scratches; i >= SLJIT_FIRST_SAVED_REG; i--)
  897. pop |= 1 << reg_map[i];
  898. return push_inst(compiler, pop);
  899. }
  900. /* --------------------------------------------------------------------- */
  901. /* Operators */
  902. /* --------------------------------------------------------------------- */
  903. /* flags: */
  904. /* Arguments are swapped. */
  905. #define ARGS_SWAPPED 0x01
  906. /* Inverted immediate. */
  907. #define INV_IMM 0x02
  908. /* Source and destination is register. */
  909. #define MOVE_REG_CONV 0x04
  910. /* Unused return value. */
  911. #define UNUSED_RETURN 0x08
  912. /* SET_FLAGS must be (1 << 20) as it is also the value of S bit (can be used for optimization). */
  913. #define SET_FLAGS (1 << 20)
  914. /* dst: reg
  915. src1: reg
  916. src2: reg or imm (if allowed)
  917. SRC2_IMM must be (1 << 25) as it is also the value of I bit (can be used for optimization). */
  918. #define SRC2_IMM (1 << 25)
  919. #define EMIT_SHIFT_INS_AND_RETURN(opcode) \
  920. SLJIT_ASSERT(!(flags & INV_IMM) && !(src2 & SRC2_IMM)); \
  921. if (compiler->shift_imm != 0x20) { \
  922. SLJIT_ASSERT(src1 == TMP_REG1); \
  923. SLJIT_ASSERT(!(flags & ARGS_SWAPPED)); \
  924. \
  925. if (compiler->shift_imm != 0) \
  926. return push_inst(compiler, MOV | (flags & SET_FLAGS) | \
  927. RD(dst) | (compiler->shift_imm << 7) | (opcode << 5) | RM(src2)); \
  928. return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | RM(src2)); \
  929. } \
  930. return push_inst(compiler, MOV | (flags & SET_FLAGS) | RD(dst) | \
  931. (reg_map[(flags & ARGS_SWAPPED) ? src1 : src2] << 8) | (opcode << 5) | 0x10 | RM((flags & ARGS_SWAPPED) ? src2 : src1));
  932. static SLJIT_INLINE sljit_s32 emit_single_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 flags,
  933. sljit_s32 dst, sljit_s32 src1, sljit_s32 src2)
  934. {
  935. switch (GET_OPCODE(op)) {
  936. case SLJIT_MOV:
  937. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  938. if (dst != src2) {
  939. if (src2 & SRC2_IMM) {
  940. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  941. }
  942. return push_inst(compiler, MOV | RD(dst) | RM(src2));
  943. }
  944. return SLJIT_SUCCESS;
  945. case SLJIT_MOV_U8:
  946. case SLJIT_MOV_S8:
  947. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  948. if (flags & MOVE_REG_CONV) {
  949. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  950. if (op == SLJIT_MOV_U8)
  951. return push_inst(compiler, AND | RD(dst) | RN(src2) | SRC2_IMM | 0xff);
  952. FAIL_IF(push_inst(compiler, MOV | RD(dst) | (24 << 7) | RM(src2)));
  953. return push_inst(compiler, MOV | RD(dst) | (24 << 7) | (op == SLJIT_MOV_U8 ? 0x20 : 0x40) | RM(dst));
  954. #else
  955. return push_inst(compiler, (op == SLJIT_MOV_U8 ? UXTB : SXTB) | RD(dst) | RM(src2));
  956. #endif
  957. }
  958. else if (dst != src2) {
  959. SLJIT_ASSERT(src2 & SRC2_IMM);
  960. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  961. }
  962. return SLJIT_SUCCESS;
  963. case SLJIT_MOV_U16:
  964. case SLJIT_MOV_S16:
  965. SLJIT_ASSERT(src1 == TMP_REG1 && !(flags & ARGS_SWAPPED));
  966. if (flags & MOVE_REG_CONV) {
  967. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  968. FAIL_IF(push_inst(compiler, MOV | RD(dst) | (16 << 7) | RM(src2)));
  969. return push_inst(compiler, MOV | RD(dst) | (16 << 7) | (op == SLJIT_MOV_U16 ? 0x20 : 0x40) | RM(dst));
  970. #else
  971. return push_inst(compiler, (op == SLJIT_MOV_U16 ? UXTH : SXTH) | RD(dst) | RM(src2));
  972. #endif
  973. }
  974. else if (dst != src2) {
  975. SLJIT_ASSERT(src2 & SRC2_IMM);
  976. return push_inst(compiler, ((flags & INV_IMM) ? MVN : MOV) | RD(dst) | src2);
  977. }
  978. return SLJIT_SUCCESS;
  979. case SLJIT_NOT:
  980. if (src2 & SRC2_IMM) {
  981. return push_inst(compiler, ((flags & INV_IMM) ? MOV : MVN) | (flags & SET_FLAGS) | RD(dst) | src2);
  982. }
  983. return push_inst(compiler, MVN | (flags & SET_FLAGS) | RD(dst) | RM(src2));
  984. case SLJIT_CLZ:
  985. SLJIT_ASSERT(!(flags & INV_IMM));
  986. SLJIT_ASSERT(!(src2 & SRC2_IMM));
  987. FAIL_IF(push_inst(compiler, CLZ | RD(dst) | RM(src2)));
  988. return SLJIT_SUCCESS;
  989. case SLJIT_ADD:
  990. SLJIT_ASSERT(!(flags & INV_IMM));
  991. if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
  992. return push_inst(compiler, CMN | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  993. return push_inst(compiler, ADD | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  994. case SLJIT_ADDC:
  995. SLJIT_ASSERT(!(flags & INV_IMM));
  996. return push_inst(compiler, ADC | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  997. case SLJIT_SUB:
  998. SLJIT_ASSERT(!(flags & INV_IMM));
  999. if ((flags & (UNUSED_RETURN | SET_FLAGS)) == (UNUSED_RETURN | SET_FLAGS) && !(flags & ARGS_SWAPPED))
  1000. return push_inst(compiler, CMP | SET_FLAGS | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1001. return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SUB : RSB) | (flags & SET_FLAGS)
  1002. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1003. case SLJIT_SUBC:
  1004. SLJIT_ASSERT(!(flags & INV_IMM));
  1005. return push_inst(compiler, (!(flags & ARGS_SWAPPED) ? SBC : RSC) | (flags & SET_FLAGS)
  1006. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1007. case SLJIT_MUL:
  1008. SLJIT_ASSERT(!(flags & INV_IMM));
  1009. SLJIT_ASSERT(!(src2 & SRC2_IMM));
  1010. if (!HAS_FLAGS(op))
  1011. return push_inst(compiler, MUL | (reg_map[dst] << 16) | (reg_map[src2] << 8) | reg_map[src1]);
  1012. FAIL_IF(push_inst(compiler, SMULL | (reg_map[TMP_REG1] << 16) | (reg_map[dst] << 12) | (reg_map[src2] << 8) | reg_map[src1]));
  1013. /* cmp TMP_REG1, dst asr #31. */
  1014. return push_inst(compiler, CMP | SET_FLAGS | RN(TMP_REG1) | RM(dst) | 0xfc0);
  1015. case SLJIT_AND:
  1016. return push_inst(compiler, (!(flags & INV_IMM) ? AND : BIC) | (flags & SET_FLAGS)
  1017. | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1018. case SLJIT_OR:
  1019. SLJIT_ASSERT(!(flags & INV_IMM));
  1020. return push_inst(compiler, ORR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1021. case SLJIT_XOR:
  1022. SLJIT_ASSERT(!(flags & INV_IMM));
  1023. return push_inst(compiler, EOR | (flags & SET_FLAGS) | RD(dst) | RN(src1) | ((src2 & SRC2_IMM) ? src2 : RM(src2)));
  1024. case SLJIT_SHL:
  1025. EMIT_SHIFT_INS_AND_RETURN(0);
  1026. case SLJIT_LSHR:
  1027. EMIT_SHIFT_INS_AND_RETURN(1);
  1028. case SLJIT_ASHR:
  1029. EMIT_SHIFT_INS_AND_RETURN(2);
  1030. }
  1031. SLJIT_UNREACHABLE();
  1032. return SLJIT_SUCCESS;
  1033. }
  1034. #undef EMIT_SHIFT_INS_AND_RETURN
  1035. /* Tests whether the immediate can be stored in the 12 bit imm field.
  1036. Returns with 0 if not possible. */
  1037. static sljit_uw get_imm(sljit_uw imm)
  1038. {
  1039. sljit_s32 rol;
  1040. if (imm <= 0xff)
  1041. return SRC2_IMM | imm;
  1042. if (!(imm & 0xff000000)) {
  1043. imm <<= 8;
  1044. rol = 8;
  1045. }
  1046. else {
  1047. imm = (imm << 24) | (imm >> 8);
  1048. rol = 0;
  1049. }
  1050. if (!(imm & 0xff000000)) {
  1051. imm <<= 8;
  1052. rol += 4;
  1053. }
  1054. if (!(imm & 0xf0000000)) {
  1055. imm <<= 4;
  1056. rol += 2;
  1057. }
  1058. if (!(imm & 0xc0000000)) {
  1059. imm <<= 2;
  1060. rol += 1;
  1061. }
  1062. if (!(imm & 0x00ffffff))
  1063. return SRC2_IMM | (imm >> 24) | (rol << 8);
  1064. else
  1065. return 0;
  1066. }
  1067. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1068. static sljit_s32 generate_int(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm, sljit_s32 positive)
  1069. {
  1070. sljit_uw mask;
  1071. sljit_uw imm1;
  1072. sljit_uw imm2;
  1073. sljit_s32 rol;
  1074. /* Step1: Search a zero byte (8 continous zero bit). */
  1075. mask = 0xff000000;
  1076. rol = 8;
  1077. while(1) {
  1078. if (!(imm & mask)) {
  1079. /* Rol imm by rol. */
  1080. imm = (imm << rol) | (imm >> (32 - rol));
  1081. /* Calculate arm rol. */
  1082. rol = 4 + (rol >> 1);
  1083. break;
  1084. }
  1085. rol += 2;
  1086. mask >>= 2;
  1087. if (mask & 0x3) {
  1088. /* rol by 8. */
  1089. imm = (imm << 8) | (imm >> 24);
  1090. mask = 0xff00;
  1091. rol = 24;
  1092. while (1) {
  1093. if (!(imm & mask)) {
  1094. /* Rol imm by rol. */
  1095. imm = (imm << rol) | (imm >> (32 - rol));
  1096. /* Calculate arm rol. */
  1097. rol = (rol >> 1) - 8;
  1098. break;
  1099. }
  1100. rol += 2;
  1101. mask >>= 2;
  1102. if (mask & 0x3)
  1103. return 0;
  1104. }
  1105. break;
  1106. }
  1107. }
  1108. /* The low 8 bit must be zero. */
  1109. SLJIT_ASSERT(!(imm & 0xff));
  1110. if (!(imm & 0xff000000)) {
  1111. imm1 = SRC2_IMM | ((imm >> 16) & 0xff) | (((rol + 4) & 0xf) << 8);
  1112. imm2 = SRC2_IMM | ((imm >> 8) & 0xff) | (((rol + 8) & 0xf) << 8);
  1113. }
  1114. else if (imm & 0xc0000000) {
  1115. imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
  1116. imm <<= 8;
  1117. rol += 4;
  1118. if (!(imm & 0xff000000)) {
  1119. imm <<= 8;
  1120. rol += 4;
  1121. }
  1122. if (!(imm & 0xf0000000)) {
  1123. imm <<= 4;
  1124. rol += 2;
  1125. }
  1126. if (!(imm & 0xc0000000)) {
  1127. imm <<= 2;
  1128. rol += 1;
  1129. }
  1130. if (!(imm & 0x00ffffff))
  1131. imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
  1132. else
  1133. return 0;
  1134. }
  1135. else {
  1136. if (!(imm & 0xf0000000)) {
  1137. imm <<= 4;
  1138. rol += 2;
  1139. }
  1140. if (!(imm & 0xc0000000)) {
  1141. imm <<= 2;
  1142. rol += 1;
  1143. }
  1144. imm1 = SRC2_IMM | ((imm >> 24) & 0xff) | ((rol & 0xf) << 8);
  1145. imm <<= 8;
  1146. rol += 4;
  1147. if (!(imm & 0xf0000000)) {
  1148. imm <<= 4;
  1149. rol += 2;
  1150. }
  1151. if (!(imm & 0xc0000000)) {
  1152. imm <<= 2;
  1153. rol += 1;
  1154. }
  1155. if (!(imm & 0x00ffffff))
  1156. imm2 = SRC2_IMM | (imm >> 24) | ((rol & 0xf) << 8);
  1157. else
  1158. return 0;
  1159. }
  1160. FAIL_IF(push_inst(compiler, (positive ? MOV : MVN) | RD(reg) | imm1));
  1161. FAIL_IF(push_inst(compiler, (positive ? ORR : BIC) | RD(reg) | RN(reg) | imm2));
  1162. return 1;
  1163. }
  1164. #endif
  1165. static sljit_s32 load_immediate(struct sljit_compiler *compiler, sljit_s32 reg, sljit_uw imm)
  1166. {
  1167. sljit_uw tmp;
  1168. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  1169. if (!(imm & ~0xffff))
  1170. return push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff));
  1171. #endif
  1172. /* Create imm by 1 inst. */
  1173. tmp = get_imm(imm);
  1174. if (tmp)
  1175. return push_inst(compiler, MOV | RD(reg) | tmp);
  1176. tmp = get_imm(~imm);
  1177. if (tmp)
  1178. return push_inst(compiler, MVN | RD(reg) | tmp);
  1179. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1180. /* Create imm by 2 inst. */
  1181. FAIL_IF(generate_int(compiler, reg, imm, 1));
  1182. FAIL_IF(generate_int(compiler, reg, ~imm, 0));
  1183. /* Load integer. */
  1184. return push_inst_with_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, reg, TMP_PC, 0), imm);
  1185. #else
  1186. FAIL_IF(push_inst(compiler, MOVW | RD(reg) | ((imm << 4) & 0xf0000) | (imm & 0xfff)));
  1187. if (imm <= 0xffff)
  1188. return SLJIT_SUCCESS;
  1189. return push_inst(compiler, MOVT | RD(reg) | ((imm >> 12) & 0xf0000) | ((imm >> 16) & 0xfff));
  1190. #endif
  1191. }
  1192. static SLJIT_INLINE sljit_s32 emit_op_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg,
  1193. sljit_s32 arg, sljit_sw argw, sljit_s32 tmp_reg)
  1194. {
  1195. sljit_uw imm, offset_reg;
  1196. sljit_uw is_type1_transfer = IS_TYPE1_TRANSFER(flags);
  1197. SLJIT_ASSERT (arg & SLJIT_MEM);
  1198. SLJIT_ASSERT((arg & REG_MASK) != tmp_reg);
  1199. if ((arg & REG_MASK) == SLJIT_UNUSED) {
  1200. if (is_type1_transfer) {
  1201. FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xfff));
  1202. argw &= 0xfff;
  1203. }
  1204. else {
  1205. FAIL_IF(load_immediate(compiler, tmp_reg, argw & ~0xff));
  1206. argw &= 0xff;
  1207. }
  1208. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg,
  1209. is_type1_transfer ? argw : TYPE2_TRANSFER_IMM(argw)));
  1210. }
  1211. if (arg & OFFS_REG_MASK) {
  1212. offset_reg = OFFS_REG(arg);
  1213. arg &= REG_MASK;
  1214. argw &= 0x3;
  1215. if (argw != 0 && !is_type1_transfer) {
  1216. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | RM(offset_reg) | (argw << 7)));
  1217. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, tmp_reg, TYPE2_TRANSFER_IMM(0)));
  1218. }
  1219. /* Bit 25: RM is offset. */
  1220. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
  1221. RM(offset_reg) | (is_type1_transfer ? (1 << 25) : 0) | (argw << 7)));
  1222. }
  1223. arg &= REG_MASK;
  1224. if (is_type1_transfer) {
  1225. if (argw > 0xfff) {
  1226. imm = get_imm(argw & ~0xfff);
  1227. if (imm) {
  1228. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
  1229. argw = argw & 0xfff;
  1230. arg = tmp_reg;
  1231. }
  1232. }
  1233. else if (argw < -0xfff) {
  1234. imm = get_imm(-argw & ~0xfff);
  1235. if (imm) {
  1236. FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
  1237. argw = -(-argw & 0xfff);
  1238. arg = tmp_reg;
  1239. }
  1240. }
  1241. if (argw >= 0 && argw <= 0xfff)
  1242. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, argw));
  1243. if (argw < 0 && argw >= -0xfff)
  1244. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, -argw));
  1245. }
  1246. else {
  1247. if (argw > 0xff) {
  1248. imm = get_imm(argw & ~0xff);
  1249. if (imm) {
  1250. FAIL_IF(push_inst(compiler, ADD | RD(tmp_reg) | RN(arg) | imm));
  1251. argw = argw & 0xff;
  1252. arg = tmp_reg;
  1253. }
  1254. }
  1255. else if (argw < -0xff) {
  1256. imm = get_imm(-argw & ~0xff);
  1257. if (imm) {
  1258. FAIL_IF(push_inst(compiler, SUB | RD(tmp_reg) | RN(arg) | imm));
  1259. argw = -(-argw & 0xff);
  1260. arg = tmp_reg;
  1261. }
  1262. }
  1263. if (argw >= 0 && argw <= 0xff)
  1264. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg, TYPE2_TRANSFER_IMM(argw)));
  1265. if (argw < 0 && argw >= -0xff) {
  1266. argw = -argw;
  1267. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 0, reg, arg, TYPE2_TRANSFER_IMM(argw)));
  1268. }
  1269. }
  1270. FAIL_IF(load_immediate(compiler, tmp_reg, argw));
  1271. return push_inst(compiler, EMIT_DATA_TRANSFER(flags, 1, reg, arg,
  1272. RM(tmp_reg) | (is_type1_transfer ? (1 << 25) : 0)));
  1273. }
  1274. static sljit_s32 emit_op(struct sljit_compiler *compiler, sljit_s32 op, sljit_s32 inp_flags,
  1275. sljit_s32 dst, sljit_sw dstw,
  1276. sljit_s32 src1, sljit_sw src1w,
  1277. sljit_s32 src2, sljit_sw src2w)
  1278. {
  1279. /* src1 is reg or TMP_REG1
  1280. src2 is reg, TMP_REG2, or imm
  1281. result goes to TMP_REG2, so put result can use TMP_REG1. */
  1282. /* We prefers register and simple consts. */
  1283. sljit_s32 dst_reg;
  1284. sljit_s32 src1_reg;
  1285. sljit_s32 src2_reg;
  1286. sljit_s32 flags = HAS_FLAGS(op) ? SET_FLAGS : 0;
  1287. /* Destination check. */
  1288. if (SLJIT_UNLIKELY(dst == SLJIT_UNUSED))
  1289. flags |= UNUSED_RETURN;
  1290. SLJIT_ASSERT(!(inp_flags & ALLOW_INV_IMM) || (inp_flags & ALLOW_IMM));
  1291. src2_reg = 0;
  1292. do {
  1293. if (!(inp_flags & ALLOW_IMM))
  1294. break;
  1295. if (src2 & SLJIT_IMM) {
  1296. src2_reg = get_imm(src2w);
  1297. if (src2_reg)
  1298. break;
  1299. if (inp_flags & ALLOW_INV_IMM) {
  1300. src2_reg = get_imm(~src2w);
  1301. if (src2_reg) {
  1302. flags |= INV_IMM;
  1303. break;
  1304. }
  1305. }
  1306. if (GET_OPCODE(op) == SLJIT_ADD) {
  1307. src2_reg = get_imm(-src2w);
  1308. if (src2_reg) {
  1309. op = SLJIT_SUB | GET_ALL_FLAGS(op);
  1310. break;
  1311. }
  1312. }
  1313. if (GET_OPCODE(op) == SLJIT_SUB) {
  1314. src2_reg = get_imm(-src2w);
  1315. if (src2_reg) {
  1316. op = SLJIT_ADD | GET_ALL_FLAGS(op);
  1317. break;
  1318. }
  1319. }
  1320. }
  1321. if (src1 & SLJIT_IMM) {
  1322. src2_reg = get_imm(src1w);
  1323. if (src2_reg) {
  1324. flags |= ARGS_SWAPPED;
  1325. src1 = src2;
  1326. src1w = src2w;
  1327. break;
  1328. }
  1329. if (inp_flags & ALLOW_INV_IMM) {
  1330. src2_reg = get_imm(~src1w);
  1331. if (src2_reg) {
  1332. flags |= ARGS_SWAPPED | INV_IMM;
  1333. src1 = src2;
  1334. src1w = src2w;
  1335. break;
  1336. }
  1337. }
  1338. if (GET_OPCODE(op) == SLJIT_ADD) {
  1339. src2_reg = get_imm(-src1w);
  1340. if (src2_reg) {
  1341. /* Note: add is commutative operation. */
  1342. src1 = src2;
  1343. src1w = src2w;
  1344. op = SLJIT_SUB | GET_ALL_FLAGS(op);
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. } while(0);
  1350. /* Source 1. */
  1351. if (FAST_IS_REG(src1))
  1352. src1_reg = src1;
  1353. else if (src1 & SLJIT_MEM) {
  1354. FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, TMP_REG1, src1, src1w, TMP_REG1));
  1355. src1_reg = TMP_REG1;
  1356. }
  1357. else {
  1358. FAIL_IF(load_immediate(compiler, TMP_REG1, src1w));
  1359. src1_reg = TMP_REG1;
  1360. }
  1361. /* Destination. */
  1362. dst_reg = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  1363. if (op <= SLJIT_MOV_P) {
  1364. if (dst & SLJIT_MEM) {
  1365. if (inp_flags & BYTE_SIZE)
  1366. inp_flags &= ~SIGNED;
  1367. if (FAST_IS_REG(src2))
  1368. return emit_op_mem(compiler, inp_flags, src2, dst, dstw, TMP_REG2);
  1369. }
  1370. if (FAST_IS_REG(src2) && dst_reg != TMP_REG2)
  1371. flags |= MOVE_REG_CONV;
  1372. }
  1373. /* Source 2. */
  1374. if (src2_reg == 0) {
  1375. src2_reg = (op <= SLJIT_MOV_P) ? dst_reg : TMP_REG2;
  1376. if (FAST_IS_REG(src2))
  1377. src2_reg = src2;
  1378. else if (src2 & SLJIT_MEM)
  1379. FAIL_IF(emit_op_mem(compiler, inp_flags | LOAD_DATA, src2_reg, src2, src2w, TMP_REG2));
  1380. else
  1381. FAIL_IF(load_immediate(compiler, src2_reg, src2w));
  1382. }
  1383. FAIL_IF(emit_single_op(compiler, op, flags, dst_reg, src1_reg, src2_reg));
  1384. if (!(dst & SLJIT_MEM))
  1385. return SLJIT_SUCCESS;
  1386. return emit_op_mem(compiler, inp_flags, dst_reg, dst, dstw, TMP_REG1);
  1387. }
  1388. #ifdef __cplusplus
  1389. extern "C" {
  1390. #endif
  1391. #if defined(__GNUC__)
  1392. extern unsigned int __aeabi_uidivmod(unsigned int numerator, unsigned int denominator);
  1393. extern int __aeabi_idivmod(int numerator, int denominator);
  1394. #else
  1395. #error "Software divmod functions are needed"
  1396. #endif
  1397. #ifdef __cplusplus
  1398. }
  1399. #endif
  1400. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op)
  1401. {
  1402. sljit_sw saved_reg_list[3];
  1403. sljit_sw saved_reg_count;
  1404. CHECK_ERROR();
  1405. CHECK(check_sljit_emit_op0(compiler, op));
  1406. op = GET_OPCODE(op);
  1407. switch (op) {
  1408. case SLJIT_BREAKPOINT:
  1409. FAIL_IF(push_inst(compiler, BKPT));
  1410. break;
  1411. case SLJIT_NOP:
  1412. FAIL_IF(push_inst(compiler, NOP));
  1413. break;
  1414. case SLJIT_LMUL_UW:
  1415. case SLJIT_LMUL_SW:
  1416. return push_inst(compiler, (op == SLJIT_LMUL_UW ? UMULL : SMULL)
  1417. | (reg_map[SLJIT_R1] << 16)
  1418. | (reg_map[SLJIT_R0] << 12)
  1419. | (reg_map[SLJIT_R0] << 8)
  1420. | reg_map[SLJIT_R1]);
  1421. case SLJIT_DIVMOD_UW:
  1422. case SLJIT_DIVMOD_SW:
  1423. case SLJIT_DIV_UW:
  1424. case SLJIT_DIV_SW:
  1425. SLJIT_COMPILE_ASSERT((SLJIT_DIVMOD_UW & 0x2) == 0 && SLJIT_DIV_UW - 0x2 == SLJIT_DIVMOD_UW, bad_div_opcode_assignments);
  1426. SLJIT_ASSERT(reg_map[2] == 1 && reg_map[3] == 2 && reg_map[4] == 3);
  1427. saved_reg_count = 0;
  1428. if (compiler->scratches >= 4)
  1429. saved_reg_list[saved_reg_count++] = 3;
  1430. if (compiler->scratches >= 3)
  1431. saved_reg_list[saved_reg_count++] = 2;
  1432. if (op >= SLJIT_DIV_UW)
  1433. saved_reg_list[saved_reg_count++] = 1;
  1434. if (saved_reg_count > 0) {
  1435. FAIL_IF(push_inst(compiler, 0xe52d0000 | (saved_reg_count >= 3 ? 16 : 8)
  1436. | (saved_reg_list[0] << 12) /* str rX, [sp, #-8/-16]! */));
  1437. if (saved_reg_count >= 2) {
  1438. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1439. FAIL_IF(push_inst(compiler, 0xe58d0004 | (saved_reg_list[1] << 12) /* str rX, [sp, #4] */));
  1440. }
  1441. if (saved_reg_count >= 3) {
  1442. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1443. FAIL_IF(push_inst(compiler, 0xe58d0008 | (saved_reg_list[2] << 12) /* str rX, [sp, #8] */));
  1444. }
  1445. }
  1446. #if defined(__GNUC__)
  1447. FAIL_IF(sljit_emit_ijump(compiler, SLJIT_FAST_CALL, SLJIT_IMM,
  1448. ((op | 0x2) == SLJIT_DIV_UW ? SLJIT_FUNC_OFFSET(__aeabi_uidivmod) : SLJIT_FUNC_OFFSET(__aeabi_idivmod))));
  1449. #else
  1450. #error "Software divmod functions are needed"
  1451. #endif
  1452. if (saved_reg_count > 0) {
  1453. if (saved_reg_count >= 3) {
  1454. SLJIT_ASSERT(saved_reg_list[2] < 8);
  1455. FAIL_IF(push_inst(compiler, 0xe59d0008 | (saved_reg_list[2] << 12) /* ldr rX, [sp, #8] */));
  1456. }
  1457. if (saved_reg_count >= 2) {
  1458. SLJIT_ASSERT(saved_reg_list[1] < 8);
  1459. FAIL_IF(push_inst(compiler, 0xe59d0004 | (saved_reg_list[1] << 12) /* ldr rX, [sp, #4] */));
  1460. }
  1461. return push_inst(compiler, 0xe49d0000 | (saved_reg_count >= 3 ? 16 : 8)
  1462. | (saved_reg_list[0] << 12) /* ldr rX, [sp], #8/16 */);
  1463. }
  1464. return SLJIT_SUCCESS;
  1465. }
  1466. return SLJIT_SUCCESS;
  1467. }
  1468. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
  1469. sljit_s32 dst, sljit_sw dstw,
  1470. sljit_s32 src, sljit_sw srcw)
  1471. {
  1472. CHECK_ERROR();
  1473. CHECK(check_sljit_emit_op1(compiler, op, dst, dstw, src, srcw));
  1474. ADJUST_LOCAL_OFFSET(dst, dstw);
  1475. ADJUST_LOCAL_OFFSET(src, srcw);
  1476. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op)) {
  1477. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  1478. if (op <= SLJIT_MOV_P && (src & SLJIT_MEM))
  1479. return emit_op_mem(compiler, PRELOAD | LOAD_DATA, TMP_PC, src, srcw, TMP_REG1);
  1480. #endif
  1481. return SLJIT_SUCCESS;
  1482. }
  1483. switch (GET_OPCODE(op)) {
  1484. case SLJIT_MOV:
  1485. case SLJIT_MOV_U32:
  1486. case SLJIT_MOV_S32:
  1487. case SLJIT_MOV_P:
  1488. return emit_op(compiler, SLJIT_MOV, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
  1489. case SLJIT_MOV_U8:
  1490. 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);
  1491. case SLJIT_MOV_S8:
  1492. 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);
  1493. case SLJIT_MOV_U16:
  1494. 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);
  1495. case SLJIT_MOV_S16:
  1496. 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);
  1497. case SLJIT_NOT:
  1498. return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, TMP_REG1, 0, src, srcw);
  1499. case SLJIT_NEG:
  1500. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  1501. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  1502. compiler->skip_checks = 1;
  1503. #endif
  1504. return sljit_emit_op2(compiler, SLJIT_SUB | GET_ALL_FLAGS(op), dst, dstw, SLJIT_IMM, 0, src, srcw);
  1505. case SLJIT_CLZ:
  1506. return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src, srcw);
  1507. }
  1508. return SLJIT_SUCCESS;
  1509. }
  1510. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
  1511. sljit_s32 dst, sljit_sw dstw,
  1512. sljit_s32 src1, sljit_sw src1w,
  1513. sljit_s32 src2, sljit_sw src2w)
  1514. {
  1515. CHECK_ERROR();
  1516. CHECK(check_sljit_emit_op2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1517. ADJUST_LOCAL_OFFSET(dst, dstw);
  1518. ADJUST_LOCAL_OFFSET(src1, src1w);
  1519. ADJUST_LOCAL_OFFSET(src2, src2w);
  1520. if (dst == SLJIT_UNUSED && !HAS_FLAGS(op))
  1521. return SLJIT_SUCCESS;
  1522. switch (GET_OPCODE(op)) {
  1523. case SLJIT_ADD:
  1524. case SLJIT_ADDC:
  1525. case SLJIT_SUB:
  1526. case SLJIT_SUBC:
  1527. case SLJIT_OR:
  1528. case SLJIT_XOR:
  1529. return emit_op(compiler, op, ALLOW_IMM, dst, dstw, src1, src1w, src2, src2w);
  1530. case SLJIT_MUL:
  1531. return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
  1532. case SLJIT_AND:
  1533. return emit_op(compiler, op, ALLOW_ANY_IMM, dst, dstw, src1, src1w, src2, src2w);
  1534. case SLJIT_SHL:
  1535. case SLJIT_LSHR:
  1536. case SLJIT_ASHR:
  1537. if (src2 & SLJIT_IMM) {
  1538. compiler->shift_imm = src2w & 0x1f;
  1539. return emit_op(compiler, op, 0, dst, dstw, TMP_REG1, 0, src1, src1w);
  1540. }
  1541. else {
  1542. compiler->shift_imm = 0x20;
  1543. return emit_op(compiler, op, 0, dst, dstw, src1, src1w, src2, src2w);
  1544. }
  1545. }
  1546. return SLJIT_SUCCESS;
  1547. }
  1548. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg)
  1549. {
  1550. CHECK_REG_INDEX(check_sljit_get_register_index(reg));
  1551. return reg_map[reg];
  1552. }
  1553. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg)
  1554. {
  1555. CHECK_REG_INDEX(check_sljit_get_float_register_index(reg));
  1556. return (freg_map[reg] << 1);
  1557. }
  1558. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
  1559. void *instruction, sljit_s32 size)
  1560. {
  1561. CHECK_ERROR();
  1562. CHECK(check_sljit_emit_op_custom(compiler, instruction, size));
  1563. return push_inst(compiler, *(sljit_uw*)instruction);
  1564. }
  1565. /* --------------------------------------------------------------------- */
  1566. /* Floating point operators */
  1567. /* --------------------------------------------------------------------- */
  1568. #define FPU_LOAD (1 << 20)
  1569. #define EMIT_FPU_DATA_TRANSFER(inst, add, base, freg, offs) \
  1570. ((inst) | ((add) << 23) | (reg_map[base] << 16) | (freg_map[freg] << 12) | (offs))
  1571. #define EMIT_FPU_OPERATION(opcode, mode, dst, src1, src2) \
  1572. ((opcode) | (mode) | (freg_map[dst] << 12) | freg_map[src1] | (freg_map[src2] << 16))
  1573. static sljit_s32 emit_fop_mem(struct sljit_compiler *compiler, sljit_s32 flags, sljit_s32 reg, sljit_s32 arg, sljit_sw argw)
  1574. {
  1575. sljit_uw imm;
  1576. sljit_sw inst = VSTR_F32 | (flags & (SLJIT_F32_OP | FPU_LOAD));
  1577. SLJIT_ASSERT(arg & SLJIT_MEM);
  1578. arg &= ~SLJIT_MEM;
  1579. if (SLJIT_UNLIKELY(arg & OFFS_REG_MASK)) {
  1580. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(OFFS_REG(arg)) | ((argw & 0x3) << 7)));
  1581. arg = TMP_REG2;
  1582. argw = 0;
  1583. }
  1584. /* Fast loads and stores. */
  1585. if (arg) {
  1586. if (!(argw & ~0x3fc))
  1587. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, arg & REG_MASK, reg, argw >> 2));
  1588. if (!(-argw & ~0x3fc))
  1589. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, arg & REG_MASK, reg, (-argw) >> 2));
  1590. imm = get_imm(argw & ~0x3fc);
  1591. if (imm) {
  1592. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
  1593. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, (argw & 0x3fc) >> 2));
  1594. }
  1595. imm = get_imm(-argw & ~0x3fc);
  1596. if (imm) {
  1597. argw = -argw;
  1598. FAIL_IF(push_inst(compiler, SUB | RD(TMP_REG2) | RN(arg & REG_MASK) | imm));
  1599. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 0, TMP_REG2, reg, (argw & 0x3fc) >> 2));
  1600. }
  1601. }
  1602. if (arg) {
  1603. FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
  1604. FAIL_IF(push_inst(compiler, ADD | RD(TMP_REG2) | RN(arg & REG_MASK) | RM(TMP_REG2)));
  1605. }
  1606. else
  1607. FAIL_IF(load_immediate(compiler, TMP_REG2, argw));
  1608. return push_inst(compiler, EMIT_FPU_DATA_TRANSFER(inst, 1, TMP_REG2, reg, 0));
  1609. }
  1610. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_sw_from_f64(struct sljit_compiler *compiler, sljit_s32 op,
  1611. sljit_s32 dst, sljit_sw dstw,
  1612. sljit_s32 src, sljit_sw srcw)
  1613. {
  1614. op ^= SLJIT_F32_OP;
  1615. if (src & SLJIT_MEM) {
  1616. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src, srcw));
  1617. src = TMP_FREG1;
  1618. }
  1619. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_S32_F32, op & SLJIT_F32_OP, TMP_FREG1, src, 0)));
  1620. if (FAST_IS_REG(dst))
  1621. return push_inst(compiler, VMOV | (1 << 20) | RD(dst) | (freg_map[TMP_FREG1] << 16));
  1622. /* Store the integer value from a VFP register. */
  1623. return emit_fop_mem(compiler, 0, TMP_FREG1, dst, dstw);
  1624. }
  1625. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_conv_f64_from_sw(struct sljit_compiler *compiler, sljit_s32 op,
  1626. sljit_s32 dst, sljit_sw dstw,
  1627. sljit_s32 src, sljit_sw srcw)
  1628. {
  1629. sljit_s32 dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1630. op ^= SLJIT_F32_OP;
  1631. if (FAST_IS_REG(src))
  1632. FAIL_IF(push_inst(compiler, VMOV | RD(src) | (freg_map[TMP_FREG1] << 16)));
  1633. else if (src & SLJIT_MEM) {
  1634. /* Load the integer value into a VFP register. */
  1635. FAIL_IF(emit_fop_mem(compiler, FPU_LOAD, TMP_FREG1, src, srcw));
  1636. }
  1637. else {
  1638. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  1639. FAIL_IF(push_inst(compiler, VMOV | RD(TMP_REG1) | (freg_map[TMP_FREG1] << 16)));
  1640. }
  1641. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F32_S32, op & SLJIT_F32_OP, dst_r, TMP_FREG1, 0)));
  1642. if (dst & SLJIT_MEM)
  1643. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw);
  1644. return SLJIT_SUCCESS;
  1645. }
  1646. static SLJIT_INLINE sljit_s32 sljit_emit_fop1_cmp(struct sljit_compiler *compiler, sljit_s32 op,
  1647. sljit_s32 src1, sljit_sw src1w,
  1648. sljit_s32 src2, sljit_sw src2w)
  1649. {
  1650. op ^= SLJIT_F32_OP;
  1651. if (src1 & SLJIT_MEM) {
  1652. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
  1653. src1 = TMP_FREG1;
  1654. }
  1655. if (src2 & SLJIT_MEM) {
  1656. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
  1657. src2 = TMP_FREG2;
  1658. }
  1659. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCMP_F32, op & SLJIT_F32_OP, src1, src2, 0)));
  1660. return push_inst(compiler, VMRS);
  1661. }
  1662. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
  1663. sljit_s32 dst, sljit_sw dstw,
  1664. sljit_s32 src, sljit_sw srcw)
  1665. {
  1666. sljit_s32 dst_r;
  1667. CHECK_ERROR();
  1668. SLJIT_COMPILE_ASSERT((SLJIT_F32_OP == 0x100), float_transfer_bit_error);
  1669. SELECT_FOP1_OPERATION_WITH_CHECKS(compiler, op, dst, dstw, src, srcw);
  1670. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1671. if (GET_OPCODE(op) != SLJIT_CONV_F64_FROM_F32)
  1672. op ^= SLJIT_F32_OP;
  1673. if (src & SLJIT_MEM) {
  1674. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, dst_r, src, srcw));
  1675. src = dst_r;
  1676. }
  1677. switch (GET_OPCODE(op)) {
  1678. case SLJIT_MOV_F64:
  1679. if (src != dst_r) {
  1680. if (dst_r != TMP_FREG1)
  1681. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1682. else
  1683. dst_r = src;
  1684. }
  1685. break;
  1686. case SLJIT_NEG_F64:
  1687. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VNEG_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1688. break;
  1689. case SLJIT_ABS_F64:
  1690. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VABS_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1691. break;
  1692. case SLJIT_CONV_F64_FROM_F32:
  1693. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VCVT_F64_F32, op & SLJIT_F32_OP, dst_r, src, 0)));
  1694. op ^= SLJIT_F32_OP;
  1695. break;
  1696. }
  1697. if (dst & SLJIT_MEM)
  1698. return emit_fop_mem(compiler, (op & SLJIT_F32_OP), dst_r, dst, dstw);
  1699. return SLJIT_SUCCESS;
  1700. }
  1701. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
  1702. sljit_s32 dst, sljit_sw dstw,
  1703. sljit_s32 src1, sljit_sw src1w,
  1704. sljit_s32 src2, sljit_sw src2w)
  1705. {
  1706. sljit_s32 dst_r;
  1707. CHECK_ERROR();
  1708. CHECK(check_sljit_emit_fop2(compiler, op, dst, dstw, src1, src1w, src2, src2w));
  1709. ADJUST_LOCAL_OFFSET(dst, dstw);
  1710. ADJUST_LOCAL_OFFSET(src1, src1w);
  1711. ADJUST_LOCAL_OFFSET(src2, src2w);
  1712. op ^= SLJIT_F32_OP;
  1713. dst_r = FAST_IS_REG(dst) ? dst : TMP_FREG1;
  1714. if (src2 & SLJIT_MEM) {
  1715. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG2, src2, src2w));
  1716. src2 = TMP_FREG2;
  1717. }
  1718. if (src1 & SLJIT_MEM) {
  1719. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP) | FPU_LOAD, TMP_FREG1, src1, src1w));
  1720. src1 = TMP_FREG1;
  1721. }
  1722. switch (GET_OPCODE(op)) {
  1723. case SLJIT_ADD_F64:
  1724. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VADD_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1725. break;
  1726. case SLJIT_SUB_F64:
  1727. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VSUB_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1728. break;
  1729. case SLJIT_MUL_F64:
  1730. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMUL_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1731. break;
  1732. case SLJIT_DIV_F64:
  1733. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VDIV_F32, op & SLJIT_F32_OP, dst_r, src2, src1)));
  1734. break;
  1735. }
  1736. if (dst_r == TMP_FREG1)
  1737. FAIL_IF(emit_fop_mem(compiler, (op & SLJIT_F32_OP), TMP_FREG1, dst, dstw));
  1738. return SLJIT_SUCCESS;
  1739. }
  1740. #undef FPU_LOAD
  1741. #undef EMIT_FPU_DATA_TRANSFER
  1742. /* --------------------------------------------------------------------- */
  1743. /* Other instructions */
  1744. /* --------------------------------------------------------------------- */
  1745. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  1746. {
  1747. CHECK_ERROR();
  1748. CHECK(check_sljit_emit_fast_enter(compiler, dst, dstw));
  1749. ADJUST_LOCAL_OFFSET(dst, dstw);
  1750. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1751. if (FAST_IS_REG(dst))
  1752. return push_inst(compiler, MOV | RD(dst) | RM(TMP_REG2));
  1753. /* Memory. */
  1754. return emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1);
  1755. }
  1756. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw)
  1757. {
  1758. CHECK_ERROR();
  1759. CHECK(check_sljit_emit_fast_return(compiler, src, srcw));
  1760. ADJUST_LOCAL_OFFSET(src, srcw);
  1761. SLJIT_ASSERT(reg_map[TMP_REG2] == 14);
  1762. if (FAST_IS_REG(src))
  1763. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG2) | RM(src)));
  1764. else
  1765. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG2, src, srcw, TMP_REG1));
  1766. return push_inst(compiler, BX | RM(TMP_REG2));
  1767. }
  1768. /* --------------------------------------------------------------------- */
  1769. /* Conditional instructions */
  1770. /* --------------------------------------------------------------------- */
  1771. static sljit_uw get_cc(sljit_s32 type)
  1772. {
  1773. switch (type) {
  1774. case SLJIT_EQUAL:
  1775. case SLJIT_MUL_NOT_OVERFLOW:
  1776. case SLJIT_EQUAL_F64:
  1777. return 0x00000000;
  1778. case SLJIT_NOT_EQUAL:
  1779. case SLJIT_MUL_OVERFLOW:
  1780. case SLJIT_NOT_EQUAL_F64:
  1781. return 0x10000000;
  1782. case SLJIT_LESS:
  1783. case SLJIT_LESS_F64:
  1784. return 0x30000000;
  1785. case SLJIT_GREATER_EQUAL:
  1786. case SLJIT_GREATER_EQUAL_F64:
  1787. return 0x20000000;
  1788. case SLJIT_GREATER:
  1789. case SLJIT_GREATER_F64:
  1790. return 0x80000000;
  1791. case SLJIT_LESS_EQUAL:
  1792. case SLJIT_LESS_EQUAL_F64:
  1793. return 0x90000000;
  1794. case SLJIT_SIG_LESS:
  1795. return 0xb0000000;
  1796. case SLJIT_SIG_GREATER_EQUAL:
  1797. return 0xa0000000;
  1798. case SLJIT_SIG_GREATER:
  1799. return 0xc0000000;
  1800. case SLJIT_SIG_LESS_EQUAL:
  1801. return 0xd0000000;
  1802. case SLJIT_OVERFLOW:
  1803. case SLJIT_UNORDERED_F64:
  1804. return 0x60000000;
  1805. case SLJIT_NOT_OVERFLOW:
  1806. case SLJIT_ORDERED_F64:
  1807. return 0x70000000;
  1808. default:
  1809. SLJIT_ASSERT(type >= SLJIT_JUMP && type <= SLJIT_CALL_CDECL);
  1810. return 0xe0000000;
  1811. }
  1812. }
  1813. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler)
  1814. {
  1815. struct sljit_label *label;
  1816. CHECK_ERROR_PTR();
  1817. CHECK_PTR(check_sljit_emit_label(compiler));
  1818. if (compiler->last_label && compiler->last_label->size == compiler->size)
  1819. return compiler->last_label;
  1820. label = (struct sljit_label*)ensure_abuf(compiler, sizeof(struct sljit_label));
  1821. PTR_FAIL_IF(!label);
  1822. set_label(label, compiler);
  1823. return label;
  1824. }
  1825. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type)
  1826. {
  1827. struct sljit_jump *jump;
  1828. CHECK_ERROR_PTR();
  1829. CHECK_PTR(check_sljit_emit_jump(compiler, type));
  1830. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  1831. PTR_FAIL_IF(!jump);
  1832. set_jump(jump, compiler, type & SLJIT_REWRITABLE_JUMP);
  1833. type &= 0xff;
  1834. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  1835. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  1836. if (type >= SLJIT_FAST_CALL)
  1837. PTR_FAIL_IF(prepare_blx(compiler));
  1838. PTR_FAIL_IF(push_inst_with_unique_literal(compiler, ((EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1,
  1839. type <= SLJIT_JUMP ? TMP_PC : TMP_REG1, TMP_PC, 0)) & ~COND_MASK) | get_cc(type), 0));
  1840. if (jump->flags & SLJIT_REWRITABLE_JUMP) {
  1841. jump->addr = compiler->size;
  1842. compiler->patches++;
  1843. }
  1844. if (type >= SLJIT_FAST_CALL) {
  1845. jump->flags |= IS_BL;
  1846. PTR_FAIL_IF(emit_blx(compiler));
  1847. }
  1848. if (!(jump->flags & SLJIT_REWRITABLE_JUMP))
  1849. jump->addr = compiler->size;
  1850. #else
  1851. if (type >= SLJIT_FAST_CALL)
  1852. jump->flags |= IS_BL;
  1853. PTR_FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
  1854. PTR_FAIL_IF(push_inst(compiler, (((type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)) & ~COND_MASK) | get_cc(type)));
  1855. jump->addr = compiler->size;
  1856. #endif
  1857. return jump;
  1858. }
  1859. #ifdef __SOFTFP__
  1860. static sljit_s32 softfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types, sljit_s32 *src)
  1861. {
  1862. sljit_s32 stack_offset = 0;
  1863. sljit_s32 arg_count = 0;
  1864. sljit_s32 word_arg_offset = 0;
  1865. sljit_s32 float_arg_count = 0;
  1866. sljit_s32 types = 0;
  1867. sljit_s32 src_offset = 4 * sizeof(sljit_sw);
  1868. sljit_u8 offsets[4];
  1869. if (src && FAST_IS_REG(*src))
  1870. src_offset = reg_map[*src] * sizeof(sljit_sw);
  1871. arg_types >>= SLJIT_DEF_SHIFT;
  1872. while (arg_types) {
  1873. types = (types << SLJIT_DEF_SHIFT) | (arg_types & SLJIT_DEF_MASK);
  1874. switch (arg_types & SLJIT_DEF_MASK) {
  1875. case SLJIT_ARG_TYPE_F32:
  1876. offsets[arg_count] = (sljit_u8)stack_offset;
  1877. stack_offset += sizeof(sljit_f32);
  1878. arg_count++;
  1879. float_arg_count++;
  1880. break;
  1881. case SLJIT_ARG_TYPE_F64:
  1882. if (stack_offset & 0x7)
  1883. stack_offset += sizeof(sljit_sw);
  1884. offsets[arg_count] = (sljit_u8)stack_offset;
  1885. stack_offset += sizeof(sljit_f64);
  1886. arg_count++;
  1887. float_arg_count++;
  1888. break;
  1889. default:
  1890. offsets[arg_count] = (sljit_u8)stack_offset;
  1891. stack_offset += sizeof(sljit_sw);
  1892. arg_count++;
  1893. word_arg_offset += sizeof(sljit_sw);
  1894. break;
  1895. }
  1896. arg_types >>= SLJIT_DEF_SHIFT;
  1897. }
  1898. if (stack_offset > 16)
  1899. FAIL_IF(push_inst(compiler, SUB | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_offset - 16) + 0x7) & ~0x7)));
  1900. /* Process arguments in reversed direction. */
  1901. while (types) {
  1902. switch (types & SLJIT_DEF_MASK) {
  1903. case SLJIT_ARG_TYPE_F32:
  1904. arg_count--;
  1905. float_arg_count--;
  1906. stack_offset = offsets[arg_count];
  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. FAIL_IF(push_inst(compiler, VMOV | 0x100000 | (float_arg_count << 16) | (stack_offset << 10)));
  1913. } else
  1914. FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800000 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1915. break;
  1916. case SLJIT_ARG_TYPE_F64:
  1917. arg_count--;
  1918. float_arg_count--;
  1919. stack_offset = offsets[arg_count];
  1920. SLJIT_ASSERT((stack_offset & 0x7) == 0);
  1921. if (stack_offset < 16) {
  1922. if (src_offset == stack_offset || src_offset == stack_offset + sizeof(sljit_sw)) {
  1923. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
  1924. *src = TMP_REG1;
  1925. }
  1926. FAIL_IF(push_inst(compiler, VMOV2 | 0x100000 | (stack_offset << 10) | ((stack_offset + sizeof(sljit_sw)) << 14) | float_arg_count));
  1927. } else
  1928. FAIL_IF(push_inst(compiler, VSTR_F32 | 0x800100 | RN(SLJIT_SP) | (float_arg_count << 12) | ((stack_offset - 16) >> 2)));
  1929. break;
  1930. default:
  1931. arg_count--;
  1932. word_arg_offset -= sizeof(sljit_sw);
  1933. stack_offset = offsets[arg_count];
  1934. SLJIT_ASSERT(stack_offset >= word_arg_offset);
  1935. if (stack_offset != word_arg_offset) {
  1936. if (stack_offset < 16) {
  1937. if (src_offset == stack_offset) {
  1938. FAIL_IF(push_inst(compiler, MOV | RD(TMP_REG1) | (src_offset >> 2)));
  1939. *src = TMP_REG1;
  1940. }
  1941. else if (src_offset == word_arg_offset) {
  1942. *src = 1 + (stack_offset >> 2);
  1943. src_offset = stack_offset;
  1944. }
  1945. FAIL_IF(push_inst(compiler, MOV | (stack_offset << 10) | (word_arg_offset >> 2)));
  1946. } else
  1947. FAIL_IF(push_inst(compiler, data_transfer_insts[WORD_SIZE] | 0x800000 | RN(SLJIT_SP) | (word_arg_offset << 10) | (stack_offset - 16)));
  1948. }
  1949. break;
  1950. }
  1951. types >>= SLJIT_DEF_SHIFT;
  1952. }
  1953. return SLJIT_SUCCESS;
  1954. }
  1955. static sljit_s32 softfloat_post_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1956. {
  1957. sljit_s32 stack_size = 0;
  1958. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32)
  1959. FAIL_IF(push_inst(compiler, VMOV | (0 << 16) | (0 << 12)));
  1960. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64)
  1961. FAIL_IF(push_inst(compiler, VMOV2 | (1 << 16) | (0 << 12) | 0));
  1962. arg_types >>= SLJIT_DEF_SHIFT;
  1963. while (arg_types) {
  1964. switch (arg_types & SLJIT_DEF_MASK) {
  1965. case SLJIT_ARG_TYPE_F32:
  1966. stack_size += sizeof(sljit_f32);
  1967. break;
  1968. case SLJIT_ARG_TYPE_F64:
  1969. if (stack_size & 0x7)
  1970. stack_size += sizeof(sljit_sw);
  1971. stack_size += sizeof(sljit_f64);
  1972. break;
  1973. default:
  1974. stack_size += sizeof(sljit_sw);
  1975. break;
  1976. }
  1977. arg_types >>= SLJIT_DEF_SHIFT;
  1978. }
  1979. if (stack_size <= 16)
  1980. return SLJIT_SUCCESS;
  1981. return push_inst(compiler, ADD | RD(SLJIT_SP) | RN(SLJIT_SP) | SRC2_IMM | (((stack_size - 16) + 0x7) & ~0x7));
  1982. }
  1983. #else /* !__SOFTFP__ */
  1984. static sljit_s32 hardfloat_call_with_args(struct sljit_compiler *compiler, sljit_s32 arg_types)
  1985. {
  1986. sljit_u32 remap = 0;
  1987. sljit_u32 offset = 0;
  1988. sljit_u32 new_offset, mask;
  1989. /* Remove return value. */
  1990. arg_types >>= SLJIT_DEF_SHIFT;
  1991. while (arg_types) {
  1992. if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F32) {
  1993. new_offset = 0;
  1994. mask = 1;
  1995. while (remap & mask) {
  1996. new_offset++;
  1997. mask <<= 1;
  1998. }
  1999. remap |= mask;
  2000. if (offset != new_offset)
  2001. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32,
  2002. 0, (new_offset >> 1) + 1, (offset >> 1) + 1, 0) | ((new_offset & 0x1) ? 0x400000 : 0)));
  2003. offset += 2;
  2004. }
  2005. else if ((arg_types & SLJIT_DEF_MASK) == SLJIT_ARG_TYPE_F64) {
  2006. new_offset = 0;
  2007. mask = 3;
  2008. while (remap & mask) {
  2009. new_offset += 2;
  2010. mask <<= 2;
  2011. }
  2012. remap |= mask;
  2013. if (offset != new_offset)
  2014. FAIL_IF(push_inst(compiler, EMIT_FPU_OPERATION(VMOV_F32, SLJIT_F32_OP, (new_offset >> 1) + 1, (offset >> 1) + 1, 0)));
  2015. offset += 2;
  2016. }
  2017. arg_types >>= SLJIT_DEF_SHIFT;
  2018. }
  2019. return SLJIT_SUCCESS;
  2020. }
  2021. #endif /* __SOFTFP__ */
  2022. #undef EMIT_FPU_OPERATION
  2023. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type,
  2024. sljit_s32 arg_types)
  2025. {
  2026. #ifdef __SOFTFP__
  2027. struct sljit_jump *jump;
  2028. #endif
  2029. CHECK_ERROR_PTR();
  2030. CHECK_PTR(check_sljit_emit_call(compiler, type, arg_types));
  2031. #ifdef __SOFTFP__
  2032. PTR_FAIL_IF(softfloat_call_with_args(compiler, arg_types, NULL));
  2033. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2034. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2035. compiler->skip_checks = 1;
  2036. #endif
  2037. jump = sljit_emit_jump(compiler, type);
  2038. PTR_FAIL_IF(jump == NULL);
  2039. PTR_FAIL_IF(softfloat_post_call_with_args(compiler, arg_types));
  2040. return jump;
  2041. #else /* !__SOFTFP__ */
  2042. PTR_FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  2043. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2044. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2045. compiler->skip_checks = 1;
  2046. #endif
  2047. return sljit_emit_jump(compiler, type);
  2048. #endif /* __SOFTFP__ */
  2049. }
  2050. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw)
  2051. {
  2052. struct sljit_jump *jump;
  2053. CHECK_ERROR();
  2054. CHECK(check_sljit_emit_ijump(compiler, type, src, srcw));
  2055. ADJUST_LOCAL_OFFSET(src, srcw);
  2056. SLJIT_ASSERT(reg_map[TMP_REG1] != 14);
  2057. if (!(src & SLJIT_IMM)) {
  2058. if (FAST_IS_REG(src)) {
  2059. SLJIT_ASSERT(reg_map[src] != 14);
  2060. return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(src));
  2061. }
  2062. SLJIT_ASSERT(src & SLJIT_MEM);
  2063. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
  2064. return push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1));
  2065. }
  2066. /* These jumps are converted to jump/call instructions when possible. */
  2067. jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
  2068. FAIL_IF(!jump);
  2069. set_jump(jump, compiler, JUMP_ADDR | ((type >= SLJIT_FAST_CALL) ? IS_BL : 0));
  2070. jump->u.target = srcw;
  2071. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  2072. if (type >= SLJIT_FAST_CALL)
  2073. FAIL_IF(prepare_blx(compiler));
  2074. 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));
  2075. if (type >= SLJIT_FAST_CALL)
  2076. FAIL_IF(emit_blx(compiler));
  2077. #else
  2078. FAIL_IF(emit_imm(compiler, TMP_REG1, 0));
  2079. FAIL_IF(push_inst(compiler, (type <= SLJIT_JUMP ? BX : BLX) | RM(TMP_REG1)));
  2080. #endif
  2081. jump->addr = compiler->size;
  2082. return SLJIT_SUCCESS;
  2083. }
  2084. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type,
  2085. sljit_s32 arg_types,
  2086. sljit_s32 src, sljit_sw srcw)
  2087. {
  2088. CHECK_ERROR();
  2089. CHECK(check_sljit_emit_icall(compiler, type, arg_types, src, srcw));
  2090. #ifdef __SOFTFP__
  2091. if (src & SLJIT_MEM) {
  2092. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, src, srcw, TMP_REG1));
  2093. src = TMP_REG1;
  2094. }
  2095. FAIL_IF(softfloat_call_with_args(compiler, arg_types, &src));
  2096. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2097. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2098. compiler->skip_checks = 1;
  2099. #endif
  2100. FAIL_IF(sljit_emit_ijump(compiler, type, src, srcw));
  2101. return softfloat_post_call_with_args(compiler, arg_types);
  2102. #else /* !__SOFTFP__ */
  2103. FAIL_IF(hardfloat_call_with_args(compiler, arg_types));
  2104. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE) \
  2105. || (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS)
  2106. compiler->skip_checks = 1;
  2107. #endif
  2108. return sljit_emit_ijump(compiler, type, src, srcw);
  2109. #endif /* __SOFTFP__ */
  2110. }
  2111. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
  2112. sljit_s32 dst, sljit_sw dstw,
  2113. sljit_s32 type)
  2114. {
  2115. sljit_s32 dst_reg, flags = GET_ALL_FLAGS(op);
  2116. sljit_uw cc, ins;
  2117. CHECK_ERROR();
  2118. CHECK(check_sljit_emit_op_flags(compiler, op, dst, dstw, type));
  2119. ADJUST_LOCAL_OFFSET(dst, dstw);
  2120. op = GET_OPCODE(op);
  2121. cc = get_cc(type & 0xff);
  2122. dst_reg = FAST_IS_REG(dst) ? dst : TMP_REG1;
  2123. if (op < SLJIT_ADD) {
  2124. FAIL_IF(push_inst(compiler, MOV | RD(dst_reg) | SRC2_IMM | 0));
  2125. FAIL_IF(push_inst(compiler, ((MOV | RD(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
  2126. if (dst & SLJIT_MEM)
  2127. return emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2);
  2128. return SLJIT_SUCCESS;
  2129. }
  2130. ins = (op == SLJIT_AND ? AND : (op == SLJIT_OR ? ORR : EOR));
  2131. if (dst & SLJIT_MEM)
  2132. FAIL_IF(emit_op_mem(compiler, WORD_SIZE | LOAD_DATA, TMP_REG1, dst, dstw, TMP_REG2));
  2133. FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 1) & ~COND_MASK) | cc));
  2134. if (op == SLJIT_AND)
  2135. FAIL_IF(push_inst(compiler, ((ins | RD(dst_reg) | RN(dst_reg) | SRC2_IMM | 0) & ~COND_MASK) | (cc ^ 0x10000000)));
  2136. if (dst & SLJIT_MEM)
  2137. FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG1, dst, dstw, TMP_REG2));
  2138. if (flags & SLJIT_SET_Z)
  2139. return push_inst(compiler, MOV | SET_FLAGS | RD(TMP_REG2) | RM(dst_reg));
  2140. return SLJIT_SUCCESS;
  2141. }
  2142. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
  2143. sljit_s32 dst_reg,
  2144. sljit_s32 src, sljit_sw srcw)
  2145. {
  2146. sljit_uw cc, tmp;
  2147. CHECK_ERROR();
  2148. CHECK(check_sljit_emit_cmov(compiler, type, dst_reg, src, srcw));
  2149. dst_reg &= ~SLJIT_I32_OP;
  2150. cc = get_cc(type & 0xff);
  2151. if (SLJIT_UNLIKELY(src & SLJIT_IMM)) {
  2152. tmp = get_imm(srcw);
  2153. if (tmp)
  2154. return push_inst(compiler, ((MOV | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
  2155. tmp = get_imm(~srcw);
  2156. if (tmp)
  2157. return push_inst(compiler, ((MVN | RD(dst_reg) | tmp) & ~COND_MASK) | cc);
  2158. #if (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  2159. tmp = (sljit_uw) srcw;
  2160. FAIL_IF(push_inst(compiler, (MOVW & ~COND_MASK) | cc | RD(dst_reg) | ((tmp << 4) & 0xf0000) | (tmp & 0xfff)));
  2161. if (tmp <= 0xffff)
  2162. return SLJIT_SUCCESS;
  2163. return push_inst(compiler, (MOVT & ~COND_MASK) | cc | RD(dst_reg) | ((tmp >> 12) & 0xf0000) | ((tmp >> 16) & 0xfff));
  2164. #else
  2165. FAIL_IF(load_immediate(compiler, TMP_REG1, srcw));
  2166. src = TMP_REG1;
  2167. #endif
  2168. }
  2169. return push_inst(compiler, ((MOV | RD(dst_reg) | RM(src)) & ~COND_MASK) | cc);
  2170. }
  2171. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
  2172. sljit_s32 reg,
  2173. sljit_s32 mem, sljit_sw memw)
  2174. {
  2175. sljit_s32 flags;
  2176. sljit_uw is_type1_transfer, inst;
  2177. CHECK_ERROR();
  2178. CHECK(check_sljit_emit_mem(compiler, type, reg, mem, memw));
  2179. is_type1_transfer = 1;
  2180. switch (type & 0xff) {
  2181. case SLJIT_MOV:
  2182. case SLJIT_MOV_U32:
  2183. case SLJIT_MOV_S32:
  2184. case SLJIT_MOV_P:
  2185. flags = WORD_SIZE;
  2186. break;
  2187. case SLJIT_MOV_U8:
  2188. flags = BYTE_SIZE;
  2189. break;
  2190. case SLJIT_MOV_S8:
  2191. if (!(type & SLJIT_MEM_STORE))
  2192. is_type1_transfer = 0;
  2193. flags = BYTE_SIZE | SIGNED;
  2194. break;
  2195. case SLJIT_MOV_U16:
  2196. is_type1_transfer = 0;
  2197. flags = HALF_SIZE;
  2198. break;
  2199. case SLJIT_MOV_S16:
  2200. is_type1_transfer = 0;
  2201. flags = HALF_SIZE | SIGNED;
  2202. break;
  2203. default:
  2204. SLJIT_UNREACHABLE();
  2205. flags = WORD_SIZE;
  2206. break;
  2207. }
  2208. if (!(type & SLJIT_MEM_STORE))
  2209. flags |= LOAD_DATA;
  2210. SLJIT_ASSERT(is_type1_transfer == !!IS_TYPE1_TRANSFER(flags));
  2211. if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
  2212. if (!is_type1_transfer && memw != 0)
  2213. return SLJIT_ERR_UNSUPPORTED;
  2214. }
  2215. else {
  2216. if (is_type1_transfer) {
  2217. if (memw > 4095 && memw < -4095)
  2218. return SLJIT_ERR_UNSUPPORTED;
  2219. }
  2220. else {
  2221. if (memw > 255 && memw < -255)
  2222. return SLJIT_ERR_UNSUPPORTED;
  2223. }
  2224. }
  2225. if (type & SLJIT_MEM_SUPP)
  2226. return SLJIT_SUCCESS;
  2227. if (SLJIT_UNLIKELY(mem & OFFS_REG_MASK)) {
  2228. memw &= 0x3;
  2229. inst = EMIT_DATA_TRANSFER(flags, 1, reg, mem & REG_MASK, RM(OFFS_REG(mem)) | (memw << 7));
  2230. if (is_type1_transfer)
  2231. inst |= (1 << 25);
  2232. if (type & SLJIT_MEM_PRE)
  2233. inst |= (1 << 21);
  2234. else
  2235. inst ^= (1 << 24);
  2236. return push_inst(compiler, inst);
  2237. }
  2238. inst = EMIT_DATA_TRANSFER(flags, 0, reg, mem & REG_MASK, 0);
  2239. if (type & SLJIT_MEM_PRE)
  2240. inst |= (1 << 21);
  2241. else
  2242. inst ^= (1 << 24);
  2243. if (is_type1_transfer) {
  2244. if (memw >= 0)
  2245. inst |= (1 << 23);
  2246. else
  2247. memw = -memw;
  2248. return push_inst(compiler, inst | memw);
  2249. }
  2250. if (memw >= 0)
  2251. inst |= (1 << 23);
  2252. else
  2253. memw = -memw;
  2254. return push_inst(compiler, inst | TYPE2_TRANSFER_IMM(memw));
  2255. }
  2256. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value)
  2257. {
  2258. struct sljit_const *const_;
  2259. sljit_s32 dst_r;
  2260. CHECK_ERROR_PTR();
  2261. CHECK_PTR(check_sljit_emit_const(compiler, dst, dstw, init_value));
  2262. ADJUST_LOCAL_OFFSET(dst, dstw);
  2263. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  2264. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  2265. PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), init_value));
  2266. compiler->patches++;
  2267. #else
  2268. PTR_FAIL_IF(emit_imm(compiler, dst_r, init_value));
  2269. #endif
  2270. const_ = (struct sljit_const*)ensure_abuf(compiler, sizeof(struct sljit_const));
  2271. PTR_FAIL_IF(!const_);
  2272. set_const(const_, compiler);
  2273. if (dst & SLJIT_MEM)
  2274. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
  2275. return const_;
  2276. }
  2277. SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw)
  2278. {
  2279. struct sljit_put_label *put_label;
  2280. sljit_s32 dst_r;
  2281. CHECK_ERROR_PTR();
  2282. CHECK_PTR(check_sljit_emit_put_label(compiler, dst, dstw));
  2283. ADJUST_LOCAL_OFFSET(dst, dstw);
  2284. dst_r = SLOW_IS_REG(dst) ? dst : TMP_REG2;
  2285. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  2286. PTR_FAIL_IF(push_inst_with_unique_literal(compiler, EMIT_DATA_TRANSFER(WORD_SIZE | LOAD_DATA, 1, dst_r, TMP_PC, 0), 0));
  2287. compiler->patches++;
  2288. #else
  2289. PTR_FAIL_IF(emit_imm(compiler, dst_r, 0));
  2290. #endif
  2291. put_label = (struct sljit_put_label*)ensure_abuf(compiler, sizeof(struct sljit_put_label));
  2292. PTR_FAIL_IF(!put_label);
  2293. set_put_label(put_label, compiler, 0);
  2294. if (dst & SLJIT_MEM)
  2295. PTR_FAIL_IF(emit_op_mem(compiler, WORD_SIZE, TMP_REG2, dst, dstw, TMP_REG1));
  2296. return put_label;
  2297. }
  2298. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset)
  2299. {
  2300. inline_set_jump_addr(addr, executable_offset, new_target, 1);
  2301. }
  2302. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset)
  2303. {
  2304. inline_set_const(addr, executable_offset, new_constant, 1);
  2305. }