sljitLir.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /*
  2. * Stack-less Just-In-Time compiler
  3. *
  4. * Copyright 2009-2012 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. #ifndef _SLJIT_LIR_H_
  27. #define _SLJIT_LIR_H_
  28. /*
  29. ------------------------------------------------------------------------
  30. Stack-Less JIT compiler for multiple architectures (x86, ARM, PowerPC)
  31. ------------------------------------------------------------------------
  32. Short description
  33. Advantages:
  34. - The execution can be continued from any LIR instruction. In other
  35. words, it is possible to jump to any label from anywhere, even from
  36. a code fragment, which is compiled later, if both compiled code
  37. shares the same context. See sljit_emit_enter for more details
  38. - Supports self modifying code: target of (conditional) jump and call
  39. instructions and some constant values can be dynamically modified
  40. during runtime
  41. - although it is not suggested to do it frequently
  42. - can be used for inline caching: save an important value once
  43. in the instruction stream
  44. - since this feature limits the optimization possibilities, a
  45. special flag must be passed at compile time when these
  46. instructions are emitted
  47. - A fixed stack space can be allocated for local variables
  48. - The compiler is thread-safe
  49. - The compiler is highly configurable through preprocessor macros.
  50. You can disable unneeded features (multithreading in single
  51. threaded applications), and you can use your own system functions
  52. (including memory allocators). See sljitConfig.h
  53. Disadvantages:
  54. - No automatic register allocation, and temporary results are
  55. not stored on the stack. (hence the name comes)
  56. In practice:
  57. - This approach is very effective for interpreters
  58. - One of the saved registers typically points to a stack interface
  59. - It can jump to any exception handler anytime (even if it belongs
  60. to another function)
  61. - Hot paths can be modified during runtime reflecting the changes
  62. of the fastest execution path of the dynamic language
  63. - SLJIT supports complex memory addressing modes
  64. - mainly position and context independent code (except some cases)
  65. For valgrind users:
  66. - pass --smc-check=all argument to valgrind, since JIT is a "self-modifying code"
  67. */
  68. #if !(defined SLJIT_NO_DEFAULT_CONFIG && SLJIT_NO_DEFAULT_CONFIG)
  69. #include "sljitConfig.h"
  70. #endif
  71. /* The following header file defines useful macros for fine tuning
  72. sljit based code generators. They are listed in the beginning
  73. of sljitConfigInternal.h */
  74. #include "sljitConfigInternal.h"
  75. /* --------------------------------------------------------------------- */
  76. /* Error codes */
  77. /* --------------------------------------------------------------------- */
  78. /* Indicates no error. */
  79. #define SLJIT_SUCCESS 0
  80. /* After the call of sljit_generate_code(), the error code of the compiler
  81. is set to this value to avoid future sljit calls (in debug mode at least).
  82. The complier should be freed after sljit_generate_code(). */
  83. #define SLJIT_ERR_COMPILED 1
  84. /* Cannot allocate non executable memory. */
  85. #define SLJIT_ERR_ALLOC_FAILED 2
  86. /* Cannot allocate executable memory.
  87. Only for sljit_generate_code() */
  88. #define SLJIT_ERR_EX_ALLOC_FAILED 3
  89. /* Return value for SLJIT_CONFIG_UNSUPPORTED placeholder architecture. */
  90. #define SLJIT_ERR_UNSUPPORTED 4
  91. /* An ivalid argument is passed to any SLJIT function. */
  92. #define SLJIT_ERR_BAD_ARGUMENT 5
  93. /* --------------------------------------------------------------------- */
  94. /* Registers */
  95. /* --------------------------------------------------------------------- */
  96. /*
  97. Scratch (R) registers: registers whose may not preserve their values
  98. across function calls.
  99. Saved (S) registers: registers whose preserve their values across
  100. function calls.
  101. The scratch and saved register sets are overlap. The last scratch register
  102. is the first saved register, the one before the last is the second saved
  103. register, and so on.
  104. If an architecture provides two scratch and three saved registers,
  105. its scratch and saved register sets are the following:
  106. R0 | [S4] | R0 and S4 represent the same physical register
  107. R1 | [S3] | R1 and S3 represent the same physical register
  108. [R2] | S2 | R2 and S2 represent the same physical register
  109. [R3] | S1 | R3 and S1 represent the same physical register
  110. [R4] | S0 | R4 and S0 represent the same physical register
  111. Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
  112. SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
  113. Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 10
  114. and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 5. However, 4 registers
  115. are virtual on x86-32. See below.
  116. The purpose of this definition is convenience. Although a register
  117. is either scratch register or saved register, SLJIT allows accessing
  118. them from the other set. For example, four registers can be used as
  119. scratch registers and the fifth one as saved register on the architecture
  120. above. Of course the last two scratch registers (R2 and R3) from this
  121. four will be saved on the stack, because they are defined as saved
  122. registers in the application binary interface. Still R2 and R3 can be
  123. used for referencing to these registers instead of S2 and S1, which
  124. makes easier to write platform independent code. Scratch registers
  125. can be saved registers in a similar way, but these extra saved
  126. registers will not be preserved across function calls! Hence the
  127. application must save them on those platforms, where the number of
  128. saved registers is too low. This can be done by copy them onto
  129. the stack and restore them after a function call.
  130. Note: To emphasize that registers assigned to R2-R4 are saved
  131. registers, they are enclosed by square brackets. S3-S4
  132. are marked in a similar way.
  133. Note: sljit_emit_enter and sljit_set_context defines whether a register
  134. is S or R register. E.g: when 3 scratches and 1 saved is mapped
  135. by sljit_emit_enter, the allowed register set will be: R0-R2 and
  136. S0. Although S2 is mapped to the same position as R2, it does not
  137. available in the current configuration. Furthermore the R3 (S1)
  138. register does not available as well.
  139. */
  140. /* When SLJIT_UNUSED is specified as destination, the result is discarded. */
  141. #define SLJIT_UNUSED 0
  142. /* Scratch registers. */
  143. #define SLJIT_R0 1
  144. #define SLJIT_R1 2
  145. #define SLJIT_R2 3
  146. /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
  147. are allocated on the stack). These registers are called virtual
  148. and cannot be used for memory addressing (cannot be part of
  149. any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
  150. limitation on other CPUs. See sljit_get_register_index(). */
  151. #define SLJIT_R3 4
  152. #define SLJIT_R4 5
  153. #define SLJIT_R5 6
  154. #define SLJIT_R6 7
  155. #define SLJIT_R7 8
  156. #define SLJIT_R8 9
  157. #define SLJIT_R9 10
  158. /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
  159. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
  160. #define SLJIT_R(i) (1 + (i))
  161. /* Saved registers. */
  162. #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
  163. #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
  164. #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
  165. /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
  166. are allocated on the stack). These registers are called virtual
  167. and cannot be used for memory addressing (cannot be part of
  168. any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
  169. limitation on other CPUs. See sljit_get_register_index(). */
  170. #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
  171. #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
  172. #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
  173. #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
  174. #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
  175. #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
  176. #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
  177. /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
  178. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
  179. #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
  180. /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
  181. #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
  182. /* The SLJIT_SP provides direct access to the linear stack space allocated by
  183. sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
  184. The immediate offset is extended by the relative stack offset automatically.
  185. The sljit_get_local_base can be used to obtain the absolute offset. */
  186. #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
  187. /* Return with machine word. */
  188. #define SLJIT_RETURN_REG SLJIT_R0
  189. /* x86 prefers specific registers for special purposes. In case of shift
  190. by register it supports only SLJIT_R2 for shift argument
  191. (which is the src2 argument of sljit_emit_op2). If another register is
  192. used, sljit must exchange data between registers which cause a minor
  193. slowdown. Other architectures has no such limitation. */
  194. #define SLJIT_PREF_SHIFT_REG SLJIT_R2
  195. /* --------------------------------------------------------------------- */
  196. /* Floating point registers */
  197. /* --------------------------------------------------------------------- */
  198. /* Each floating point register can store a double or single precision
  199. value. The FR and FS register sets are overlap in the same way as R
  200. and S register sets. See above. */
  201. /* Note: SLJIT_UNUSED as destination is not valid for floating point
  202. operations, since they cannot be used for setting flags. */
  203. /* Floating point scratch registers. */
  204. #define SLJIT_FR0 1
  205. #define SLJIT_FR1 2
  206. #define SLJIT_FR2 3
  207. #define SLJIT_FR3 4
  208. #define SLJIT_FR4 5
  209. #define SLJIT_FR5 6
  210. /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
  211. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
  212. #define SLJIT_FR(i) (1 + (i))
  213. /* Floating point saved registers. */
  214. #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
  215. #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
  216. #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
  217. #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
  218. #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
  219. #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
  220. /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
  221. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
  222. #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
  223. /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
  224. #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
  225. /* --------------------------------------------------------------------- */
  226. /* Main structures and functions */
  227. /* --------------------------------------------------------------------- */
  228. /*
  229. The following structures are private, and can be changed in the
  230. future. Keeping them here allows code inlining.
  231. */
  232. struct sljit_memory_fragment {
  233. struct sljit_memory_fragment *next;
  234. sljit_uw used_size;
  235. /* Must be aligned to sljit_sw. */
  236. sljit_ub memory[1];
  237. };
  238. struct sljit_label {
  239. struct sljit_label *next;
  240. sljit_uw addr;
  241. /* The maximum size difference. */
  242. sljit_uw size;
  243. };
  244. struct sljit_jump {
  245. struct sljit_jump *next;
  246. sljit_uw addr;
  247. sljit_sw flags;
  248. union {
  249. sljit_uw target;
  250. struct sljit_label* label;
  251. } u;
  252. };
  253. struct sljit_const {
  254. struct sljit_const *next;
  255. sljit_uw addr;
  256. };
  257. struct sljit_compiler {
  258. sljit_si error;
  259. sljit_si options;
  260. struct sljit_label *labels;
  261. struct sljit_jump *jumps;
  262. struct sljit_const *consts;
  263. struct sljit_label *last_label;
  264. struct sljit_jump *last_jump;
  265. struct sljit_const *last_const;
  266. void *allocator_data;
  267. struct sljit_memory_fragment *buf;
  268. struct sljit_memory_fragment *abuf;
  269. /* Used scratch registers. */
  270. sljit_si scratches;
  271. /* Used saved registers. */
  272. sljit_si saveds;
  273. /* Used float scratch registers. */
  274. sljit_si fscratches;
  275. /* Used float saved registers. */
  276. sljit_si fsaveds;
  277. /* Local stack size. */
  278. sljit_si local_size;
  279. /* Code size. */
  280. sljit_uw size;
  281. /* For statistical purposes. */
  282. sljit_uw executable_size;
  283. #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
  284. sljit_si args;
  285. #endif
  286. #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
  287. sljit_si mode32;
  288. #endif
  289. #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
  290. sljit_si flags_saved;
  291. #endif
  292. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  293. /* Constant pool handling. */
  294. sljit_uw *cpool;
  295. sljit_ub *cpool_unique;
  296. sljit_uw cpool_diff;
  297. sljit_uw cpool_fill;
  298. /* Other members. */
  299. /* Contains pointer, "ldr pc, [...]" pairs. */
  300. sljit_uw patches;
  301. #endif
  302. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  303. /* Temporary fields. */
  304. sljit_uw shift_imm;
  305. sljit_si cache_arg;
  306. sljit_sw cache_argw;
  307. #endif
  308. #if (defined SLJIT_CONFIG_ARM_THUMB2 && SLJIT_CONFIG_ARM_THUMB2)
  309. sljit_si cache_arg;
  310. sljit_sw cache_argw;
  311. #endif
  312. #if (defined SLJIT_CONFIG_ARM_64 && SLJIT_CONFIG_ARM_64)
  313. sljit_si cache_arg;
  314. sljit_sw cache_argw;
  315. #endif
  316. #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
  317. sljit_sw imm;
  318. sljit_si cache_arg;
  319. sljit_sw cache_argw;
  320. #endif
  321. #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
  322. sljit_si delay_slot;
  323. sljit_si cache_arg;
  324. sljit_sw cache_argw;
  325. #endif
  326. #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  327. sljit_si delay_slot;
  328. sljit_si cache_arg;
  329. sljit_sw cache_argw;
  330. #endif
  331. #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
  332. sljit_si cache_arg;
  333. sljit_sw cache_argw;
  334. #endif
  335. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  336. FILE* verbose;
  337. #endif
  338. #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
  339. || (defined SLJIT_DEBUG && SLJIT_DEBUG)
  340. /* Local size passed to the functions. */
  341. sljit_si logical_local_size;
  342. #endif
  343. #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
  344. || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
  345. || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  346. sljit_si skip_checks;
  347. #endif
  348. };
  349. /* --------------------------------------------------------------------- */
  350. /* Main functions */
  351. /* --------------------------------------------------------------------- */
  352. /* Creates an sljit compiler. The allocator_data is required by some
  353. custom memory managers. This pointer is passed to SLJIT_MALLOC
  354. and SLJIT_FREE macros. Most allocators (including the default
  355. one) ignores this value, and it is recommended to pass NULL
  356. as a dummy value for allocator_data.
  357. Returns NULL if failed. */
  358. SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data);
  359. /* Frees everything except the compiled machine code. */
  360. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
  361. /* Returns the current error code. If an error is occurred, future sljit
  362. calls which uses the same compiler argument returns early with the same
  363. error code. Thus there is no need for checking the error after every
  364. call, it is enough to do it before the code is compiled. Removing
  365. these checks increases the performance of the compiling process. */
  366. static SLJIT_INLINE sljit_si sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
  367. /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
  368. if an error was detected before. After the error code is set
  369. the compiler behaves as if the allocation failure happened
  370. during an sljit function call. This can greatly simplify error
  371. checking, since only the compiler status needs to be checked
  372. after the compilation. */
  373. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
  374. /*
  375. Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
  376. and <= 128 bytes on 64 bit architectures. The memory area is owned by the
  377. compiler, and freed by sljit_free_compiler. The returned pointer is
  378. sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
  379. the compiling, and no need to worry about freeing them. The size is
  380. enough to contain at most 16 pointers. If the size is outside of the range,
  381. the function will return with NULL. However, this return value does not
  382. indicate that there is no more memory (does not set the current error code
  383. of the compiler to out-of-memory status).
  384. */
  385. SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_si size);
  386. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  387. /* Passing NULL disables verbose. */
  388. SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
  389. #endif
  390. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
  391. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
  392. /*
  393. After the machine code generation is finished we can retrieve the allocated
  394. executable memory size, although this area may not be fully filled with
  395. instructions depending on some optimizations. This function is useful only
  396. for statistical purposes.
  397. Before a successful code generation, this function returns with 0.
  398. */
  399. static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
  400. /* Instruction generation. Returns with any error code. If there is no
  401. error, they return with SLJIT_SUCCESS. */
  402. /*
  403. The executable code is a function call from the viewpoint of the C
  404. language. The function calls must obey to the ABI (Application
  405. Binary Interface) of the platform, which specify the purpose of
  406. all machine registers and stack handling among other things. The
  407. sljit_emit_enter function emits the necessary instructions for
  408. setting up a new context for the executable code and moves function
  409. arguments to the saved registers. Furthermore the options argument
  410. can be used to pass configuration options to the compiler. The
  411. available options are listed before sljit_emit_enter.
  412. The number of sljit_sw arguments passed to the generated function
  413. are specified in the "args" parameter. The number of arguments must
  414. be less than or equal to 3. The first argument goes to SLJIT_S0,
  415. the second goes to SLJIT_S1 and so on. The register set used by
  416. the function must be declared as well. The number of scratch and
  417. saved registers used by the function must be passed to sljit_emit_enter.
  418. Only R registers between R0 and "scratches" argument can be used
  419. later. E.g. if "scratches" is set to 2, the register set will be
  420. limited to R0 and R1. The S registers and the floating point
  421. registers ("fscratches" and "fsaveds") are specified in a similar
  422. way. The sljit_emit_enter is also capable of allocating a stack
  423. space for local variables. The "local_size" argument contains the
  424. size in bytes of this local area and its staring address is stored
  425. in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
  426. SLJIT_SP + local_size (exclusive) can be modified freely until
  427. the function returns. The stack space is not initialized.
  428. Note: the following conditions must met:
  429. 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
  430. 0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
  431. scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
  432. 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  433. 0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  434. fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  435. Note: every call of sljit_emit_enter and sljit_set_context
  436. overwrites the previous context.
  437. */
  438. /* The absolute address returned by sljit_get_local_base with
  439. offset 0 is aligned to sljit_d. Otherwise it is aligned to sljit_uw. */
  440. #define SLJIT_DOUBLE_ALIGNMENT 0x00000001
  441. /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
  442. #define SLJIT_MAX_LOCAL_SIZE 65536
  443. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_enter(struct sljit_compiler *compiler,
  444. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  445. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
  446. /* The machine code has a context (which contains the local stack space size,
  447. number of used registers, etc.) which initialized by sljit_emit_enter. Several
  448. functions (like sljit_emit_return) requres this context to be able to generate
  449. the appropriate code. However, some code fragments (like inline cache) may have
  450. no normal entry point so their context is unknown for the compiler. Their context
  451. can be provided to the compiler by the sljit_set_context function.
  452. Note: every call of sljit_emit_enter and sljit_set_context overwrites
  453. the previous context. */
  454. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_set_context(struct sljit_compiler *compiler,
  455. sljit_si options, sljit_si args, sljit_si scratches, sljit_si saveds,
  456. sljit_si fscratches, sljit_si fsaveds, sljit_si local_size);
  457. /* Return from machine code. The op argument can be SLJIT_UNUSED which means the
  458. function does not return with anything or any opcode between SLJIT_MOV and
  459. SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
  460. is SLJIT_UNUSED, otherwise see below the description about source and
  461. destination arguments. */
  462. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_return(struct sljit_compiler *compiler, sljit_si op,
  463. sljit_si src, sljit_sw srcw);
  464. /* Fast calling mechanism for utility functions (see SLJIT_FAST_CALL). All registers and
  465. even the stack frame is passed to the callee. The return address is preserved in
  466. dst/dstw by sljit_emit_fast_enter (the type of the value stored by this function
  467. is sljit_p), and sljit_emit_fast_return can use this as a return value later. */
  468. /* Note: only for sljit specific, non ABI compilant calls. Fast, since only a few machine
  469. instructions are needed. Excellent for small uility functions, where saving registers
  470. and setting up a new stack frame would cost too much performance. However, it is still
  471. possible to return to the address of the caller (or anywhere else). */
  472. /* Note: flags are not changed (unlike sljit_emit_enter / sljit_emit_return). */
  473. /* Note: although sljit_emit_fast_return could be replaced by an ijump, it is not suggested,
  474. since many architectures do clever branch prediction on call / return instruction pairs. */
  475. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw);
  476. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_si src, sljit_sw srcw);
  477. /*
  478. Source and destination values for arithmetical instructions
  479. imm - a simple immediate value (cannot be used as a destination)
  480. reg - any of the registers (immediate argument must be 0)
  481. [imm] - absolute immediate memory address
  482. [reg+imm] - indirect memory address
  483. [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
  484. useful for (byte, half, int, sljit_sw) array access
  485. (fully supported by both x86 and ARM architectures, and cheap operation on others)
  486. */
  487. /*
  488. IMPORATNT NOTE: memory access MUST be naturally aligned except
  489. SLJIT_UNALIGNED macro is defined and its value is 1.
  490. length | alignment
  491. ---------+-----------
  492. byte | 1 byte (any physical_address is accepted)
  493. half | 2 byte (physical_address & 0x1 == 0)
  494. int | 4 byte (physical_address & 0x3 == 0)
  495. word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
  496. | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
  497. pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
  498. | on 64 bit machines)
  499. Note: Different architectures have different addressing limitations.
  500. A single instruction is enough for the following addressing
  501. modes. Other adrressing modes are emulated by instruction
  502. sequences. This information could help to improve those code
  503. generators which focuses only a few architectures.
  504. x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
  505. [reg+(reg<<imm)] is supported
  506. [imm], -2^32+1 <= imm <= 2^32-1 is supported
  507. Write-back is not supported
  508. arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
  509. bytes, any halfs or floating point values)
  510. [reg+(reg<<imm)] is supported
  511. Write-back is supported
  512. arm-t2: [reg+imm], -255 <= imm <= 4095
  513. [reg+(reg<<imm)] is supported
  514. Write back is supported only for [reg+imm], where -255 <= imm <= 255
  515. ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
  516. signed load on 64 bit requires immediates divisible by 4.
  517. [reg+imm] is not supported for signed 8 bit values.
  518. [reg+reg] is supported
  519. Write-back is supported except for one instruction: 32 bit signed
  520. load with [reg+imm] addressing mode on 64 bit.
  521. mips: [reg+imm], -65536 <= imm <= 65535
  522. sparc: [reg+imm], -4096 <= imm <= 4095
  523. [reg+reg] is supported
  524. */
  525. /* Register output: simply the name of the register.
  526. For destination, you can use SLJIT_UNUSED as well. */
  527. #define SLJIT_MEM 0x80
  528. #define SLJIT_MEM0() (SLJIT_MEM)
  529. #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
  530. #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
  531. #define SLJIT_IMM 0x40
  532. /* Set 32 bit operation mode (I) on 64 bit CPUs. The flag is totally ignored on
  533. 32 bit CPUs. If this flag is set for an arithmetic operation, it uses only the
  534. lower 32 bit of the input register(s), and set the CPU status flags according
  535. to the 32 bit result. The higher 32 bits are undefined for both the input and
  536. output. However, the CPU might not ignore those higher 32 bits, like MIPS, which
  537. expects it to be the sign extension of the lower 32 bit. All 32 bit operations
  538. are undefined, if this condition is not fulfilled. Therefore, when SLJIT_INT_OP
  539. is specified, all register arguments must be the result of other operations with
  540. the same SLJIT_INT_OP flag. In other words, although a register can hold either
  541. a 64 or 32 bit value, these values cannot be mixed. The only exceptions are
  542. SLJIT_IMOV and SLJIT_IMOVU (SLJIT_MOV_SI/SLJIT_MOVU_SI with SLJIT_INT_OP flag)
  543. which can convert any source argument to SLJIT_INT_OP compatible result. This
  544. conversion might be unnecessary on some CPUs like x86-64, since the upper 32
  545. bit is always ignored. In this case SLJIT is clever enough to not generate any
  546. instructions if the source and destination operands are the same registers.
  547. Affects sljit_emit_op0, sljit_emit_op1 and sljit_emit_op2. */
  548. #define SLJIT_INT_OP 0x100
  549. /* Single precision mode (SP). This flag is similar to SLJIT_INT_OP, just
  550. it applies to floating point registers (it is even the same bit). When
  551. this flag is passed, the CPU performs single precision floating point
  552. operations. Similar to SLJIT_INT_OP, all register arguments must be the
  553. result of other floating point operations with this flag. Affects
  554. sljit_emit_fop1, sljit_emit_fop2 and sljit_emit_fcmp. */
  555. #define SLJIT_SINGLE_OP 0x100
  556. /* Common CPU status flags for all architectures (x86, ARM, PPC)
  557. - carry flag
  558. - overflow flag
  559. - zero flag
  560. - negative/positive flag (depends on arc)
  561. On mips, these flags are emulated by software. */
  562. /* By default, the instructions may, or may not set the CPU status flags.
  563. Forcing to set or keep status flags can be done with the following flags: */
  564. /* Note: sljit tries to emit the minimum number of instructions. Using these
  565. flags can increase them, so use them wisely to avoid unnecessary code generation. */
  566. /* Set Equal (Zero) status flag (E). */
  567. #define SLJIT_SET_E 0x0200
  568. /* Set unsigned status flag (U). */
  569. #define SLJIT_SET_U 0x0400
  570. /* Set signed status flag (S). */
  571. #define SLJIT_SET_S 0x0800
  572. /* Set signed overflow flag (O). */
  573. #define SLJIT_SET_O 0x1000
  574. /* Set carry flag (C).
  575. Note: Kinda unsigned overflow, but behaves differently on various cpus. */
  576. #define SLJIT_SET_C 0x2000
  577. /* Do not modify the flags (K).
  578. Note: This flag cannot be combined with any other SLJIT_SET_* flag. */
  579. #define SLJIT_KEEP_FLAGS 0x4000
  580. /* Notes:
  581. - you cannot postpone conditional jump instructions except if noted that
  582. the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
  583. - flag combinations: '|' means 'logical or'. */
  584. /* Starting index of opcodes for sljit_emit_op0. */
  585. #define SLJIT_OP0_BASE 0
  586. /* Flags: - (never set any flags)
  587. Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
  588. It falls back to SLJIT_NOP in those cases. */
  589. #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
  590. /* Flags: - (never set any flags)
  591. Note: may or may not cause an extra cycle wait
  592. it can even decrease the runtime in a few cases. */
  593. #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
  594. /* Flags: - (may destroy flags)
  595. Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
  596. Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
  597. #define SLJIT_LUMUL (SLJIT_OP0_BASE + 2)
  598. /* Flags: - (may destroy flags)
  599. Signed multiplication of SLJIT_R0 and SLJIT_R1.
  600. Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
  601. #define SLJIT_LSMUL (SLJIT_OP0_BASE + 3)
  602. /* Flags: I - (may destroy flags)
  603. Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  604. The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
  605. Note: if SLJIT_R1 is 0, the behaviour is undefined. */
  606. #define SLJIT_UDIVMOD (SLJIT_OP0_BASE + 4)
  607. #define SLJIT_IUDIVMOD (SLJIT_UDIVMOD | SLJIT_INT_OP)
  608. /* Flags: I - (may destroy flags)
  609. Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  610. The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
  611. Note: if SLJIT_R1 is 0, the behaviour is undefined.
  612. Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
  613. the behaviour is undefined. */
  614. #define SLJIT_SDIVMOD (SLJIT_OP0_BASE + 5)
  615. #define SLJIT_ISDIVMOD (SLJIT_SDIVMOD | SLJIT_INT_OP)
  616. /* Flags: I - (may destroy flags)
  617. Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  618. The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
  619. Note: if SLJIT_R1 is 0, the behaviour is undefined.
  620. Note: SLJIT_SDIV is single precision divide. */
  621. #define SLJIT_UDIVI (SLJIT_OP0_BASE + 6)
  622. #define SLJIT_IUDIVI (SLJIT_UDIVI | SLJIT_INT_OP)
  623. /* Flags: I - (may destroy flags)
  624. Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  625. The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
  626. Note: if SLJIT_R1 is 0, the behaviour is undefined.
  627. Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
  628. the behaviour is undefined.
  629. Note: SLJIT_SDIV is single precision divide. */
  630. #define SLJIT_SDIVI (SLJIT_OP0_BASE + 7)
  631. #define SLJIT_ISDIVI (SLJIT_SDIVI | SLJIT_INT_OP)
  632. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op0(struct sljit_compiler *compiler, sljit_si op);
  633. /* Starting index of opcodes for sljit_emit_op1. */
  634. #define SLJIT_OP1_BASE 32
  635. /* Notes for MOV instructions:
  636. U = Mov with update (pre form). If source or destination defined as SLJIT_MEM1(r1)
  637. or SLJIT_MEM2(r1, r2), r1 is increased by the sum of r2 and the constant argument
  638. UB = unsigned byte (8 bit)
  639. SB = signed byte (8 bit)
  640. UH = unsigned half (16 bit)
  641. SH = signed half (16 bit)
  642. UI = unsigned int (32 bit)
  643. SI = signed int (32 bit)
  644. P = pointer (sljit_p) size */
  645. /* Flags: - (never set any flags) */
  646. #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
  647. /* Flags: I - (never set any flags) */
  648. #define SLJIT_MOV_UB (SLJIT_OP1_BASE + 1)
  649. #define SLJIT_IMOV_UB (SLJIT_MOV_UB | SLJIT_INT_OP)
  650. /* Flags: I - (never set any flags) */
  651. #define SLJIT_MOV_SB (SLJIT_OP1_BASE + 2)
  652. #define SLJIT_IMOV_SB (SLJIT_MOV_SB | SLJIT_INT_OP)
  653. /* Flags: I - (never set any flags) */
  654. #define SLJIT_MOV_UH (SLJIT_OP1_BASE + 3)
  655. #define SLJIT_IMOV_UH (SLJIT_MOV_UH | SLJIT_INT_OP)
  656. /* Flags: I - (never set any flags) */
  657. #define SLJIT_MOV_SH (SLJIT_OP1_BASE + 4)
  658. #define SLJIT_IMOV_SH (SLJIT_MOV_SH | SLJIT_INT_OP)
  659. /* Flags: I - (never set any flags)
  660. Note: see SLJIT_INT_OP for further details. */
  661. #define SLJIT_MOV_UI (SLJIT_OP1_BASE + 5)
  662. /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOV. */
  663. /* Flags: I - (never set any flags)
  664. Note: see SLJIT_INT_OP for further details. */
  665. #define SLJIT_MOV_SI (SLJIT_OP1_BASE + 6)
  666. #define SLJIT_IMOV (SLJIT_MOV_SI | SLJIT_INT_OP)
  667. /* Flags: - (never set any flags) */
  668. #define SLJIT_MOV_P (SLJIT_OP1_BASE + 7)
  669. /* Flags: - (never set any flags) */
  670. #define SLJIT_MOVU (SLJIT_OP1_BASE + 8)
  671. /* Flags: I - (never set any flags) */
  672. #define SLJIT_MOVU_UB (SLJIT_OP1_BASE + 9)
  673. #define SLJIT_IMOVU_UB (SLJIT_MOVU_UB | SLJIT_INT_OP)
  674. /* Flags: I - (never set any flags) */
  675. #define SLJIT_MOVU_SB (SLJIT_OP1_BASE + 10)
  676. #define SLJIT_IMOVU_SB (SLJIT_MOVU_SB | SLJIT_INT_OP)
  677. /* Flags: I - (never set any flags) */
  678. #define SLJIT_MOVU_UH (SLJIT_OP1_BASE + 11)
  679. #define SLJIT_IMOVU_UH (SLJIT_MOVU_UH | SLJIT_INT_OP)
  680. /* Flags: I - (never set any flags) */
  681. #define SLJIT_MOVU_SH (SLJIT_OP1_BASE + 12)
  682. #define SLJIT_IMOVU_SH (SLJIT_MOVU_SH | SLJIT_INT_OP)
  683. /* Flags: I - (never set any flags)
  684. Note: see SLJIT_INT_OP for further details. */
  685. #define SLJIT_MOVU_UI (SLJIT_OP1_BASE + 13)
  686. /* No SLJIT_INT_OP form, since it is the same as SLJIT_IMOVU. */
  687. /* Flags: I - (never set any flags)
  688. Note: see SLJIT_INT_OP for further details. */
  689. #define SLJIT_MOVU_SI (SLJIT_OP1_BASE + 14)
  690. #define SLJIT_IMOVU (SLJIT_MOVU_SI | SLJIT_INT_OP)
  691. /* Flags: - (never set any flags) */
  692. #define SLJIT_MOVU_P (SLJIT_OP1_BASE + 15)
  693. /* Flags: I | E | K */
  694. #define SLJIT_NOT (SLJIT_OP1_BASE + 16)
  695. #define SLJIT_INOT (SLJIT_NOT | SLJIT_INT_OP)
  696. /* Flags: I | E | O | K */
  697. #define SLJIT_NEG (SLJIT_OP1_BASE + 17)
  698. #define SLJIT_INEG (SLJIT_NEG | SLJIT_INT_OP)
  699. /* Count leading zeroes
  700. Flags: I | E | K
  701. Important note! Sparc 32 does not support K flag, since
  702. the required popc instruction is introduced only in sparc 64. */
  703. #define SLJIT_CLZ (SLJIT_OP1_BASE + 18)
  704. #define SLJIT_ICLZ (SLJIT_CLZ | SLJIT_INT_OP)
  705. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op1(struct sljit_compiler *compiler, sljit_si op,
  706. sljit_si dst, sljit_sw dstw,
  707. sljit_si src, sljit_sw srcw);
  708. /* Starting index of opcodes for sljit_emit_op2. */
  709. #define SLJIT_OP2_BASE 96
  710. /* Flags: I | E | O | C | K */
  711. #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
  712. #define SLJIT_IADD (SLJIT_ADD | SLJIT_INT_OP)
  713. /* Flags: I | C | K */
  714. #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
  715. #define SLJIT_IADDC (SLJIT_ADDC | SLJIT_INT_OP)
  716. /* Flags: I | E | U | S | O | C | K */
  717. #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
  718. #define SLJIT_ISUB (SLJIT_SUB | SLJIT_INT_OP)
  719. /* Flags: I | C | K */
  720. #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
  721. #define SLJIT_ISUBC (SLJIT_SUBC | SLJIT_INT_OP)
  722. /* Note: integer mul
  723. Flags: I | O (see SLJIT_C_MUL_*) | K */
  724. #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
  725. #define SLJIT_IMUL (SLJIT_MUL | SLJIT_INT_OP)
  726. /* Flags: I | E | K */
  727. #define SLJIT_AND (SLJIT_OP2_BASE + 5)
  728. #define SLJIT_IAND (SLJIT_AND | SLJIT_INT_OP)
  729. /* Flags: I | E | K */
  730. #define SLJIT_OR (SLJIT_OP2_BASE + 6)
  731. #define SLJIT_IOR (SLJIT_OR | SLJIT_INT_OP)
  732. /* Flags: I | E | K */
  733. #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
  734. #define SLJIT_IXOR (SLJIT_XOR | SLJIT_INT_OP)
  735. /* Flags: I | E | K
  736. Let bit_length be the length of the shift operation: 32 or 64.
  737. If src2 is immediate, src2w is masked by (bit_length - 1).
  738. Otherwise, if the content of src2 is outside the range from 0
  739. to bit_length - 1, the result is undefined. */
  740. #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
  741. #define SLJIT_ISHL (SLJIT_SHL | SLJIT_INT_OP)
  742. /* Flags: I | E | K
  743. Let bit_length be the length of the shift operation: 32 or 64.
  744. If src2 is immediate, src2w is masked by (bit_length - 1).
  745. Otherwise, if the content of src2 is outside the range from 0
  746. to bit_length - 1, the result is undefined. */
  747. #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
  748. #define SLJIT_ILSHR (SLJIT_LSHR | SLJIT_INT_OP)
  749. /* Flags: I | E | K
  750. Let bit_length be the length of the shift operation: 32 or 64.
  751. If src2 is immediate, src2w is masked by (bit_length - 1).
  752. Otherwise, if the content of src2 is outside the range from 0
  753. to bit_length - 1, the result is undefined. */
  754. #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
  755. #define SLJIT_IASHR (SLJIT_ASHR | SLJIT_INT_OP)
  756. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op2(struct sljit_compiler *compiler, sljit_si op,
  757. sljit_si dst, sljit_sw dstw,
  758. sljit_si src1, sljit_sw src1w,
  759. sljit_si src2, sljit_sw src2w);
  760. /* Returns with non-zero if fpu is available. */
  761. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_is_fpu_available(void);
  762. /* Starting index of opcodes for sljit_emit_fop1. */
  763. #define SLJIT_FOP1_BASE 128
  764. /* Flags: SP - (never set any flags) */
  765. #define SLJIT_DMOV (SLJIT_FOP1_BASE + 0)
  766. #define SLJIT_SMOV (SLJIT_DMOV | SLJIT_SINGLE_OP)
  767. /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
  768. SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
  769. Rounding mode when the destination is W or I: round towards zero. */
  770. /* Flags: SP - (never set any flags) */
  771. #define SLJIT_CONVD_FROMS (SLJIT_FOP1_BASE + 1)
  772. #define SLJIT_CONVS_FROMD (SLJIT_CONVD_FROMS | SLJIT_SINGLE_OP)
  773. /* Flags: SP - (never set any flags) */
  774. #define SLJIT_CONVW_FROMD (SLJIT_FOP1_BASE + 2)
  775. #define SLJIT_CONVW_FROMS (SLJIT_CONVW_FROMD | SLJIT_SINGLE_OP)
  776. /* Flags: SP - (never set any flags) */
  777. #define SLJIT_CONVI_FROMD (SLJIT_FOP1_BASE + 3)
  778. #define SLJIT_CONVI_FROMS (SLJIT_CONVI_FROMD | SLJIT_SINGLE_OP)
  779. /* Flags: SP - (never set any flags) */
  780. #define SLJIT_CONVD_FROMW (SLJIT_FOP1_BASE + 4)
  781. #define SLJIT_CONVS_FROMW (SLJIT_CONVD_FROMW | SLJIT_SINGLE_OP)
  782. /* Flags: SP - (never set any flags) */
  783. #define SLJIT_CONVD_FROMI (SLJIT_FOP1_BASE + 5)
  784. #define SLJIT_CONVS_FROMI (SLJIT_CONVD_FROMI | SLJIT_SINGLE_OP)
  785. /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
  786. Note: NaN check is always performed. If SLJIT_C_FLOAT_UNORDERED flag
  787. is set, the comparison result is unpredictable.
  788. Flags: SP | E | S (see SLJIT_C_FLOAT_*) */
  789. #define SLJIT_DCMP (SLJIT_FOP1_BASE + 6)
  790. #define SLJIT_SCMP (SLJIT_DCMP | SLJIT_SINGLE_OP)
  791. /* Flags: SP - (never set any flags) */
  792. #define SLJIT_DNEG (SLJIT_FOP1_BASE + 7)
  793. #define SLJIT_SNEG (SLJIT_DNEG | SLJIT_SINGLE_OP)
  794. /* Flags: SP - (never set any flags) */
  795. #define SLJIT_DABS (SLJIT_FOP1_BASE + 8)
  796. #define SLJIT_SABS (SLJIT_DABS | SLJIT_SINGLE_OP)
  797. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop1(struct sljit_compiler *compiler, sljit_si op,
  798. sljit_si dst, sljit_sw dstw,
  799. sljit_si src, sljit_sw srcw);
  800. /* Starting index of opcodes for sljit_emit_fop2. */
  801. #define SLJIT_FOP2_BASE 160
  802. /* Flags: SP - (never set any flags) */
  803. #define SLJIT_DADD (SLJIT_FOP2_BASE + 0)
  804. #define SLJIT_SADD (SLJIT_DADD | SLJIT_SINGLE_OP)
  805. /* Flags: SP - (never set any flags) */
  806. #define SLJIT_DSUB (SLJIT_FOP2_BASE + 1)
  807. #define SLJIT_SSUB (SLJIT_DSUB | SLJIT_SINGLE_OP)
  808. /* Flags: SP - (never set any flags) */
  809. #define SLJIT_DMUL (SLJIT_FOP2_BASE + 2)
  810. #define SLJIT_SMUL (SLJIT_DMUL | SLJIT_SINGLE_OP)
  811. /* Flags: SP - (never set any flags) */
  812. #define SLJIT_DDIV (SLJIT_FOP2_BASE + 3)
  813. #define SLJIT_SDIV (SLJIT_DDIV | SLJIT_SINGLE_OP)
  814. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_fop2(struct sljit_compiler *compiler, sljit_si op,
  815. sljit_si dst, sljit_sw dstw,
  816. sljit_si src1, sljit_sw src1w,
  817. sljit_si src2, sljit_sw src2w);
  818. /* Label and jump instructions. */
  819. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
  820. /* Invert (negate) conditional type: xor (^) with 0x1 */
  821. /* Integer comparison types. */
  822. #define SLJIT_EQUAL 0
  823. #define SLJIT_I_EQUAL (SLJIT_EQUAL | SLJIT_INT_OP)
  824. #define SLJIT_ZERO 0
  825. #define SLJIT_I_ZERO (SLJIT_ZERO | SLJIT_INT_OP)
  826. #define SLJIT_NOT_EQUAL 1
  827. #define SLJIT_I_NOT_EQUAL (SLJIT_NOT_EQUAL | SLJIT_INT_OP)
  828. #define SLJIT_NOT_ZERO 1
  829. #define SLJIT_I_NOT_ZERO (SLJIT_NOT_ZERO | SLJIT_INT_OP)
  830. #define SLJIT_LESS 2
  831. #define SLJIT_I_LESS (SLJIT_LESS | SLJIT_INT_OP)
  832. #define SLJIT_GREATER_EQUAL 3
  833. #define SLJIT_I_GREATER_EQUAL (SLJIT_GREATER_EQUAL | SLJIT_INT_OP)
  834. #define SLJIT_GREATER 4
  835. #define SLJIT_I_GREATER (SLJIT_GREATER | SLJIT_INT_OP)
  836. #define SLJIT_LESS_EQUAL 5
  837. #define SLJIT_I_LESS_EQUAL (SLJIT_LESS_EQUAL | SLJIT_INT_OP)
  838. #define SLJIT_SIG_LESS 6
  839. #define SLJIT_I_SIG_LESS (SLJIT_SIG_LESS | SLJIT_INT_OP)
  840. #define SLJIT_SIG_GREATER_EQUAL 7
  841. #define SLJIT_I_SIG_GREATER_EQUAL (SLJIT_SIG_GREATER_EQUAL | SLJIT_INT_OP)
  842. #define SLJIT_SIG_GREATER 8
  843. #define SLJIT_I_SIG_GREATER (SLJIT_SIG_GREATER | SLJIT_INT_OP)
  844. #define SLJIT_SIG_LESS_EQUAL 9
  845. #define SLJIT_I_SIG_LESS_EQUAL (SLJIT_SIG_LESS_EQUAL | SLJIT_INT_OP)
  846. #define SLJIT_OVERFLOW 10
  847. #define SLJIT_I_OVERFLOW (SLJIT_OVERFLOW | SLJIT_INT_OP)
  848. #define SLJIT_NOT_OVERFLOW 11
  849. #define SLJIT_I_NOT_OVERFLOW (SLJIT_NOT_OVERFLOW | SLJIT_INT_OP)
  850. #define SLJIT_MUL_OVERFLOW 12
  851. #define SLJIT_I_MUL_OVERFLOW (SLJIT_MUL_OVERFLOW | SLJIT_INT_OP)
  852. #define SLJIT_MUL_NOT_OVERFLOW 13
  853. #define SLJIT_I_MUL_NOT_OVERFLOW (SLJIT_MUL_NOT_OVERFLOW | SLJIT_INT_OP)
  854. /* Floating point comparison types. */
  855. #define SLJIT_D_EQUAL 14
  856. #define SLJIT_S_EQUAL (SLJIT_D_EQUAL | SLJIT_SINGLE_OP)
  857. #define SLJIT_D_NOT_EQUAL 15
  858. #define SLJIT_S_NOT_EQUAL (SLJIT_D_NOT_EQUAL | SLJIT_SINGLE_OP)
  859. #define SLJIT_D_LESS 16
  860. #define SLJIT_S_LESS (SLJIT_D_LESS | SLJIT_SINGLE_OP)
  861. #define SLJIT_D_GREATER_EQUAL 17
  862. #define SLJIT_S_GREATER_EQUAL (SLJIT_D_GREATER_EQUAL | SLJIT_SINGLE_OP)
  863. #define SLJIT_D_GREATER 18
  864. #define SLJIT_S_GREATER (SLJIT_D_GREATER | SLJIT_SINGLE_OP)
  865. #define SLJIT_D_LESS_EQUAL 19
  866. #define SLJIT_S_LESS_EQUAL (SLJIT_D_LESS_EQUAL | SLJIT_SINGLE_OP)
  867. #define SLJIT_D_UNORDERED 20
  868. #define SLJIT_S_UNORDERED (SLJIT_D_UNORDERED | SLJIT_SINGLE_OP)
  869. #define SLJIT_D_ORDERED 21
  870. #define SLJIT_S_ORDERED (SLJIT_D_ORDERED | SLJIT_SINGLE_OP)
  871. /* Unconditional jump types. */
  872. #define SLJIT_JUMP 22
  873. #define SLJIT_FAST_CALL 23
  874. #define SLJIT_CALL0 24
  875. #define SLJIT_CALL1 25
  876. #define SLJIT_CALL2 26
  877. #define SLJIT_CALL3 27
  878. /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
  879. /* The target can be changed during runtime (see: sljit_set_jump_addr). */
  880. #define SLJIT_REWRITABLE_JUMP 0x1000
  881. /* Emit a jump instruction. The destination is not set, only the type of the jump.
  882. type must be between SLJIT_EQUAL and SLJIT_CALL3
  883. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  884. Flags: - (never set any flags) for both conditional and unconditional jumps.
  885. Flags: destroy all flags for calls. */
  886. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_si type);
  887. /* Basic arithmetic comparison. In most architectures it is implemented as
  888. an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
  889. appropriate flags) followed by a sljit_emit_jump. However some
  890. architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
  891. It is suggested to use this comparison form when appropriate.
  892. type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
  893. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  894. Flags: destroy flags. */
  895. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_si type,
  896. sljit_si src1, sljit_sw src1w,
  897. sljit_si src2, sljit_sw src2w);
  898. /* Basic floating point comparison. In most architectures it is implemented as
  899. an SLJIT_FCMP operation (setting appropriate flags) followed by a
  900. sljit_emit_jump. However some architectures (i.e: MIPS) may employ
  901. special optimizations here. It is suggested to use this comparison form
  902. when appropriate.
  903. type must be between SLJIT_D_EQUAL and SLJIT_S_ORDERED
  904. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  905. Flags: destroy flags.
  906. Note: if either operand is NaN, the behaviour is undefined for
  907. types up to SLJIT_S_LESS_EQUAL. */
  908. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_si type,
  909. sljit_si src1, sljit_sw src1w,
  910. sljit_si src2, sljit_sw src2w);
  911. /* Set the destination of the jump to this label. */
  912. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
  913. /* Set the destination address of the jump to this label. */
  914. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
  915. /* Call function or jump anywhere. Both direct and indirect form
  916. type must be between SLJIT_JUMP and SLJIT_CALL3
  917. Direct form: set src to SLJIT_IMM() and srcw to the address
  918. Indirect form: any other valid addressing mode
  919. Flags: - (never set any flags) for unconditional jumps.
  920. Flags: destroy all flags for calls. */
  921. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_ijump(struct sljit_compiler *compiler, sljit_si type, sljit_si src, sljit_sw srcw);
  922. /* Perform the operation using the conditional flags as the second argument.
  923. Type must always be between SLJIT_EQUAL and SLJIT_S_ORDERED. The value
  924. represented by the type is 1, if the condition represented by the type
  925. is fulfilled, and 0 otherwise.
  926. If op == SLJIT_MOV, SLJIT_MOV_SI, SLJIT_MOV_UI:
  927. Set dst to the value represented by the type (0 or 1).
  928. Src must be SLJIT_UNUSED, and srcw must be 0
  929. Flags: - (never set any flags)
  930. If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
  931. Performs the binary operation using src as the first, and the value
  932. represented by type as the second argument.
  933. Important note: only dst=src and dstw=srcw is supported at the moment!
  934. Flags: I | E | K
  935. Note: sljit_emit_op_flags does nothing, if dst is SLJIT_UNUSED (regardless of op). */
  936. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_si op,
  937. sljit_si dst, sljit_sw dstw,
  938. sljit_si src, sljit_sw srcw,
  939. sljit_si type);
  940. /* Copies the base address of SLJIT_SP + offset to dst.
  941. Flags: - (never set any flags) */
  942. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_local_base(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw offset);
  943. /* The constant can be changed runtime (see: sljit_set_const)
  944. Flags: - (never set any flags) */
  945. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_si dst, sljit_sw dstw, sljit_sw init_value);
  946. /* After the code generation the address for label, jump and const instructions
  947. are computed. Since these structures are freed by sljit_free_compiler, the
  948. addresses must be preserved by the user program elsewere. */
  949. static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
  950. static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
  951. static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
  952. /* Only the address is required to rewrite the code. */
  953. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_addr);
  954. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant);
  955. /* --------------------------------------------------------------------- */
  956. /* Miscellaneous utility functions */
  957. /* --------------------------------------------------------------------- */
  958. #define SLJIT_MAJOR_VERSION 0
  959. #define SLJIT_MINOR_VERSION 93
  960. /* Get the human readable name of the platform. Can be useful on platforms
  961. like ARM, where ARM and Thumb2 functions can be mixed, and
  962. it is useful to know the type of the code generator. */
  963. SLJIT_API_FUNC_ATTRIBUTE SLJIT_CONST char* sljit_get_platform_name(void);
  964. /* Portable helper function to get an offset of a member. */
  965. #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
  966. #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  967. /* This global lock is useful to compile common functions. */
  968. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_grab_lock(void);
  969. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_release_lock(void);
  970. #endif
  971. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
  972. /* The sljit_stack is a utiliy feature of sljit, which allocates a
  973. writable memory region between base (inclusive) and limit (exclusive).
  974. Both base and limit is a pointer, and base is always <= than limit.
  975. This feature uses the "address space reserve" feature
  976. of modern operating systems. Basically we don't need to allocate a
  977. huge memory block in one step for the worst case, we can start with
  978. a smaller chunk and extend it later. Since the address space is
  979. reserved, the data never copied to other regions, thus it is safe
  980. to store pointers here. */
  981. /* Note: The base field is aligned to PAGE_SIZE bytes (usually 4k or more).
  982. Note: stack growing should not happen in small steps: 4k, 16k or even
  983. bigger growth is better.
  984. Note: this structure may not be supported by all operating systems.
  985. Some kind of fallback mechanism is suggested when SLJIT_UTIL_STACK
  986. is not defined. */
  987. struct sljit_stack {
  988. /* User data, anything can be stored here.
  989. Starting with the same value as base. */
  990. sljit_uw top;
  991. /* These members are read only. */
  992. sljit_uw base;
  993. sljit_uw limit;
  994. sljit_uw max_limit;
  995. };
  996. /* Returns NULL if unsuccessful.
  997. Note: limit and max_limit contains the size for stack allocation.
  998. Note: the top field is initialized to base.
  999. Note: see sljit_create_compiler for the explanation of allocator_data. */
  1000. SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_CALL sljit_allocate_stack(sljit_uw limit, sljit_uw max_limit, void *allocator_data);
  1001. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_CALL sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
  1002. /* Can be used to increase (allocate) or decrease (free) the memory area.
  1003. Returns with a non-zero value if unsuccessful. If new_limit is greater than
  1004. max_limit, it will fail. It is very easy to implement a stack data structure,
  1005. since the growth ratio can be added to the current limit, and sljit_stack_resize
  1006. will do all the necessary checks. The fields of the stack are not changed if
  1007. sljit_stack_resize fails. */
  1008. SLJIT_API_FUNC_ATTRIBUTE sljit_sw SLJIT_CALL sljit_stack_resize(struct sljit_stack *stack, sljit_uw new_limit);
  1009. #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
  1010. #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  1011. /* Get the entry address of a given function. */
  1012. #define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name)
  1013. #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
  1014. /* All JIT related code should be placed in the same context (library, binary, etc.). */
  1015. #define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name)
  1016. /* For powerpc64, the function pointers point to a context descriptor. */
  1017. struct sljit_function_context {
  1018. sljit_sw addr;
  1019. sljit_sw r2;
  1020. sljit_sw r11;
  1021. };
  1022. /* Fill the context arguments using the addr and the function.
  1023. If func_ptr is NULL, it will not be set to the address of context
  1024. If addr is NULL, the function address also comes from the func pointer. */
  1025. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
  1026. #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
  1027. /* --------------------------------------------------------------------- */
  1028. /* CPU specific functions */
  1029. /* --------------------------------------------------------------------- */
  1030. /* The following function is a helper function for sljit_emit_op_custom.
  1031. It returns with the real machine register index ( >=0 ) of any SLJIT_R,
  1032. SLJIT_S and SLJIT_SP registers.
  1033. Note: it returns with -1 for virtual registers (only on x86-32). */
  1034. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_register_index(sljit_si reg);
  1035. /* The following function is a helper function for sljit_emit_op_custom.
  1036. It returns with the real machine register index of any SLJIT_FLOAT register.
  1037. Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
  1038. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_get_float_register_index(sljit_si reg);
  1039. /* Any instruction can be inserted into the instruction stream by
  1040. sljit_emit_op_custom. It has a similar purpose as inline assembly.
  1041. The size parameter must match to the instruction size of the target
  1042. architecture:
  1043. x86: 0 < size <= 15. The instruction argument can be byte aligned.
  1044. Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
  1045. if size == 4, the instruction argument must be 4 byte aligned.
  1046. Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
  1047. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_emit_op_custom(struct sljit_compiler *compiler,
  1048. void *instruction, sljit_si size);
  1049. #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
  1050. /* Returns with non-zero if sse2 is available. */
  1051. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_is_sse2_available(void);
  1052. /* Returns with non-zero if cmov instruction is available. */
  1053. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_is_cmov_available(void);
  1054. /* Emit a conditional mov instruction on x86 CPUs. This instruction
  1055. moves src to destination, if the condition is satisfied. Unlike
  1056. other arithmetic instructions, destination must be a register.
  1057. Before such instructions are emitted, cmov support should be
  1058. checked by sljit_x86_is_cmov_available function.
  1059. type must be between SLJIT_EQUAL and SLJIT_S_ORDERED
  1060. dst_reg must be a valid register and it can be combined
  1061. with SLJIT_INT_OP to perform 32 bit arithmetic
  1062. Flags: I - (never set any flags)
  1063. */
  1064. SLJIT_API_FUNC_ATTRIBUTE sljit_si sljit_x86_emit_cmov(struct sljit_compiler *compiler,
  1065. sljit_si type,
  1066. sljit_si dst_reg,
  1067. sljit_si src, sljit_sw srcw);
  1068. #endif
  1069. #endif /* _SLJIT_LIR_H_ */