sljitNativeARM_32.c 82 KB

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