sljitLir.h 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. #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. /* Dynamic code modification is not enabled. */
  94. #define SLJIT_ERR_DYN_CODE_MOD 6
  95. /* --------------------------------------------------------------------- */
  96. /* Registers */
  97. /* --------------------------------------------------------------------- */
  98. /*
  99. Scratch (R) registers: registers whose may not preserve their values
  100. across function calls.
  101. Saved (S) registers: registers whose preserve their values across
  102. function calls.
  103. The scratch and saved register sets are overlap. The last scratch register
  104. is the first saved register, the one before the last is the second saved
  105. register, and so on.
  106. If an architecture provides two scratch and three saved registers,
  107. its scratch and saved register sets are the following:
  108. R0 | | R0 is always a scratch register
  109. R1 | | R1 is always a scratch register
  110. [R2] | S2 | R2 and S2 represent the same physical register
  111. [R3] | S1 | R3 and S1 represent the same physical register
  112. [R4] | S0 | R4 and S0 represent the same physical register
  113. Note: SLJIT_NUMBER_OF_SCRATCH_REGISTERS would be 2 and
  114. SLJIT_NUMBER_OF_SAVED_REGISTERS would be 3 for this architecture.
  115. Note: On all supported architectures SLJIT_NUMBER_OF_REGISTERS >= 12
  116. and SLJIT_NUMBER_OF_SAVED_REGISTERS >= 6. However, 6 registers
  117. are virtual on x86-32. See below.
  118. The purpose of this definition is convenience: saved registers can
  119. be used as extra scratch registers. For example four registers can
  120. be specified as scratch registers and the fifth one as saved register
  121. on the CPU above and any user code which requires four scratch
  122. registers can run unmodified. The SLJIT compiler automatically saves
  123. the content of the two extra scratch register on the stack. Scratch
  124. registers can also be preserved by saving their value on the stack
  125. but this needs to be done manually.
  126. Note: To emphasize that registers assigned to R2-R4 are saved
  127. registers, they are enclosed by square brackets.
  128. Note: sljit_emit_enter and sljit_set_context defines whether a register
  129. is S or R register. E.g: when 3 scratches and 1 saved is mapped
  130. by sljit_emit_enter, the allowed register set will be: R0-R2 and
  131. S0. Although S2 is mapped to the same position as R2, it does not
  132. available in the current configuration. Furthermore the S1 register
  133. is not available at all.
  134. */
  135. /* When SLJIT_UNUSED is specified as the destination of sljit_emit_op1
  136. or sljit_emit_op2 operations the result is discarded. If no status
  137. flags are set, no instructions are emitted for these operations. Data
  138. prefetch is a special exception, see SLJIT_MOV operation. Other SLJIT
  139. operations do not support SLJIT_UNUSED as a destination operand. */
  140. #define SLJIT_UNUSED 0
  141. /* Scratch registers. */
  142. #define SLJIT_R0 1
  143. #define SLJIT_R1 2
  144. #define SLJIT_R2 3
  145. /* Note: on x86-32, R3 - R6 (same as S3 - S6) are emulated (they
  146. are allocated on the stack). These registers are called virtual
  147. and cannot be used for memory addressing (cannot be part of
  148. any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
  149. limitation on other CPUs. See sljit_get_register_index(). */
  150. #define SLJIT_R3 4
  151. #define SLJIT_R4 5
  152. #define SLJIT_R5 6
  153. #define SLJIT_R6 7
  154. #define SLJIT_R7 8
  155. #define SLJIT_R8 9
  156. #define SLJIT_R9 10
  157. /* All R registers provided by the architecture can be accessed by SLJIT_R(i)
  158. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_REGISTERS. */
  159. #define SLJIT_R(i) (1 + (i))
  160. /* Saved registers. */
  161. #define SLJIT_S0 (SLJIT_NUMBER_OF_REGISTERS)
  162. #define SLJIT_S1 (SLJIT_NUMBER_OF_REGISTERS - 1)
  163. #define SLJIT_S2 (SLJIT_NUMBER_OF_REGISTERS - 2)
  164. /* Note: on x86-32, S3 - S6 (same as R3 - R6) are emulated (they
  165. are allocated on the stack). These registers are called virtual
  166. and cannot be used for memory addressing (cannot be part of
  167. any SLJIT_MEM1, SLJIT_MEM2 construct). There is no such
  168. limitation on other CPUs. See sljit_get_register_index(). */
  169. #define SLJIT_S3 (SLJIT_NUMBER_OF_REGISTERS - 3)
  170. #define SLJIT_S4 (SLJIT_NUMBER_OF_REGISTERS - 4)
  171. #define SLJIT_S5 (SLJIT_NUMBER_OF_REGISTERS - 5)
  172. #define SLJIT_S6 (SLJIT_NUMBER_OF_REGISTERS - 6)
  173. #define SLJIT_S7 (SLJIT_NUMBER_OF_REGISTERS - 7)
  174. #define SLJIT_S8 (SLJIT_NUMBER_OF_REGISTERS - 8)
  175. #define SLJIT_S9 (SLJIT_NUMBER_OF_REGISTERS - 9)
  176. /* All S registers provided by the architecture can be accessed by SLJIT_S(i)
  177. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_REGISTERS. */
  178. #define SLJIT_S(i) (SLJIT_NUMBER_OF_REGISTERS - (i))
  179. /* Registers >= SLJIT_FIRST_SAVED_REG are saved registers. */
  180. #define SLJIT_FIRST_SAVED_REG (SLJIT_S0 - SLJIT_NUMBER_OF_SAVED_REGISTERS + 1)
  181. /* The SLJIT_SP provides direct access to the linear stack space allocated by
  182. sljit_emit_enter. It can only be used in the following form: SLJIT_MEM1(SLJIT_SP).
  183. The immediate offset is extended by the relative stack offset automatically.
  184. The sljit_get_local_base can be used to obtain the absolute offset. */
  185. #define SLJIT_SP (SLJIT_NUMBER_OF_REGISTERS + 1)
  186. /* Return with machine word. */
  187. #define SLJIT_RETURN_REG SLJIT_R0
  188. /* --------------------------------------------------------------------- */
  189. /* Floating point registers */
  190. /* --------------------------------------------------------------------- */
  191. /* Each floating point register can store a 32 or a 64 bit precision
  192. value. The FR and FS register sets are overlap in the same way as R
  193. and S register sets. See above. */
  194. /* Note: SLJIT_UNUSED as destination is not valid for floating point
  195. operations, since they cannot be used for setting flags. */
  196. /* Floating point scratch registers. */
  197. #define SLJIT_FR0 1
  198. #define SLJIT_FR1 2
  199. #define SLJIT_FR2 3
  200. #define SLJIT_FR3 4
  201. #define SLJIT_FR4 5
  202. #define SLJIT_FR5 6
  203. /* All FR registers provided by the architecture can be accessed by SLJIT_FR(i)
  204. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_FLOAT_REGISTERS. */
  205. #define SLJIT_FR(i) (1 + (i))
  206. /* Floating point saved registers. */
  207. #define SLJIT_FS0 (SLJIT_NUMBER_OF_FLOAT_REGISTERS)
  208. #define SLJIT_FS1 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 1)
  209. #define SLJIT_FS2 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 2)
  210. #define SLJIT_FS3 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 3)
  211. #define SLJIT_FS4 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 4)
  212. #define SLJIT_FS5 (SLJIT_NUMBER_OF_FLOAT_REGISTERS - 5)
  213. /* All S registers provided by the architecture can be accessed by SLJIT_FS(i)
  214. The i parameter must be >= 0 and < SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS. */
  215. #define SLJIT_FS(i) (SLJIT_NUMBER_OF_FLOAT_REGISTERS - (i))
  216. /* Float registers >= SLJIT_FIRST_SAVED_FLOAT_REG are saved registers. */
  217. #define SLJIT_FIRST_SAVED_FLOAT_REG (SLJIT_FS0 - SLJIT_NUMBER_OF_SAVED_FLOAT_REGISTERS + 1)
  218. /* --------------------------------------------------------------------- */
  219. /* Argument type definitions */
  220. /* --------------------------------------------------------------------- */
  221. /* Argument type definitions.
  222. Used by SLJIT_[DEF_]ARGx and SLJIT_[DEF]_RET macros. */
  223. #define SLJIT_ARG_TYPE_VOID 0
  224. #define SLJIT_ARG_TYPE_SW 1
  225. #define SLJIT_ARG_TYPE_UW 2
  226. #define SLJIT_ARG_TYPE_S32 3
  227. #define SLJIT_ARG_TYPE_U32 4
  228. #define SLJIT_ARG_TYPE_F32 5
  229. #define SLJIT_ARG_TYPE_F64 6
  230. /* The following argument type definitions are used by sljit_emit_enter,
  231. sljit_set_context, sljit_emit_call, and sljit_emit_icall functions.
  232. The following return type definitions are used by sljit_emit_call
  233. and sljit_emit_icall functions.
  234. When a function is called, the first integer argument must be placed
  235. in SLJIT_R0, the second in SLJIT_R1, and so on. Similarly the first
  236. floating point argument must be placed in SLJIT_FR0, the second in
  237. SLJIT_FR1, and so on.
  238. Example function definition:
  239. sljit_f32 SLJIT_FUNC example_c_callback(sljit_sw arg_a,
  240. sljit_f64 arg_b, sljit_u32 arg_c, sljit_f32 arg_d);
  241. Argument type definition:
  242. SLJIT_DEF_RET(SLJIT_ARG_TYPE_F32)
  243. | SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_SW) | SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_F64)
  244. | SLJIT_DEF_ARG3(SLJIT_ARG_TYPE_U32) | SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_F32)
  245. Short form of argument type definition:
  246. SLJIT_RET(F32) | SLJIT_ARG1(SW) | SLJIT_ARG2(F64)
  247. | SLJIT_ARG3(S32) | SLJIT_ARG4(F32)
  248. Argument passing:
  249. arg_a must be placed in SLJIT_R0
  250. arg_c must be placed in SLJIT_R1
  251. arg_b must be placed in SLJIT_FR0
  252. arg_d must be placed in SLJIT_FR1
  253. Note:
  254. The SLJIT_ARG_TYPE_VOID type is only supported by
  255. SLJIT_DEF_RET, and SLJIT_ARG_TYPE_VOID is also the
  256. default value when SLJIT_DEF_RET is not specified. */
  257. #define SLJIT_DEF_SHIFT 4
  258. #define SLJIT_DEF_RET(type) (type)
  259. #define SLJIT_DEF_ARG1(type) ((type) << SLJIT_DEF_SHIFT)
  260. #define SLJIT_DEF_ARG2(type) ((type) << (2 * SLJIT_DEF_SHIFT))
  261. #define SLJIT_DEF_ARG3(type) ((type) << (3 * SLJIT_DEF_SHIFT))
  262. #define SLJIT_DEF_ARG4(type) ((type) << (4 * SLJIT_DEF_SHIFT))
  263. /* Short form of the macros above.
  264. For example the following definition:
  265. SLJIT_DEF_RET(SLJIT_ARG_TYPE_SW) | SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_F32)
  266. can be shortened to:
  267. SLJIT_RET(SW) | SLJIT_ARG1(F32)
  268. Note:
  269. The VOID type is only supported by SLJIT_RET, and
  270. VOID is also the default value when SLJIT_RET is
  271. not specified. */
  272. #define SLJIT_RET(type) SLJIT_DEF_RET(SLJIT_ARG_TYPE_ ## type)
  273. #define SLJIT_ARG1(type) SLJIT_DEF_ARG1(SLJIT_ARG_TYPE_ ## type)
  274. #define SLJIT_ARG2(type) SLJIT_DEF_ARG2(SLJIT_ARG_TYPE_ ## type)
  275. #define SLJIT_ARG3(type) SLJIT_DEF_ARG3(SLJIT_ARG_TYPE_ ## type)
  276. #define SLJIT_ARG4(type) SLJIT_DEF_ARG4(SLJIT_ARG_TYPE_ ## type)
  277. /* --------------------------------------------------------------------- */
  278. /* Main structures and functions */
  279. /* --------------------------------------------------------------------- */
  280. /*
  281. The following structures are private, and can be changed in the
  282. future. Keeping them here allows code inlining.
  283. */
  284. struct sljit_memory_fragment {
  285. struct sljit_memory_fragment *next;
  286. sljit_uw used_size;
  287. /* Must be aligned to sljit_sw. */
  288. sljit_u8 memory[1];
  289. };
  290. struct sljit_label {
  291. struct sljit_label *next;
  292. sljit_uw addr;
  293. /* The maximum size difference. */
  294. sljit_uw size;
  295. };
  296. struct sljit_jump {
  297. struct sljit_jump *next;
  298. sljit_uw addr;
  299. sljit_uw flags;
  300. union {
  301. sljit_uw target;
  302. struct sljit_label *label;
  303. } u;
  304. };
  305. struct sljit_put_label {
  306. struct sljit_put_label *next;
  307. struct sljit_label *label;
  308. sljit_uw addr;
  309. sljit_uw flags;
  310. };
  311. struct sljit_const {
  312. struct sljit_const *next;
  313. sljit_uw addr;
  314. };
  315. struct sljit_compiler {
  316. sljit_s32 error;
  317. sljit_s32 options;
  318. struct sljit_label *labels;
  319. struct sljit_jump *jumps;
  320. struct sljit_put_label *put_labels;
  321. struct sljit_const *consts;
  322. struct sljit_label *last_label;
  323. struct sljit_jump *last_jump;
  324. struct sljit_const *last_const;
  325. struct sljit_put_label *last_put_label;
  326. void *allocator_data;
  327. struct sljit_memory_fragment *buf;
  328. struct sljit_memory_fragment *abuf;
  329. /* Used scratch registers. */
  330. sljit_s32 scratches;
  331. /* Used saved registers. */
  332. sljit_s32 saveds;
  333. /* Used float scratch registers. */
  334. sljit_s32 fscratches;
  335. /* Used float saved registers. */
  336. sljit_s32 fsaveds;
  337. /* Local stack size. */
  338. sljit_s32 local_size;
  339. /* Code size. */
  340. sljit_uw size;
  341. /* Relative offset of the executable mapping from the writable mapping. */
  342. sljit_uw executable_offset;
  343. /* Executable size for statistical purposes. */
  344. sljit_uw executable_size;
  345. #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)
  346. sljit_s32 args;
  347. sljit_s32 locals_offset;
  348. sljit_s32 saveds_offset;
  349. sljit_s32 stack_tmp_size;
  350. #endif
  351. #if (defined SLJIT_CONFIG_X86_64 && SLJIT_CONFIG_X86_64)
  352. sljit_s32 mode32;
  353. #ifdef _WIN64
  354. sljit_s32 locals_offset;
  355. #endif
  356. #endif
  357. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5)
  358. /* Constant pool handling. */
  359. sljit_uw *cpool;
  360. sljit_u8 *cpool_unique;
  361. sljit_uw cpool_diff;
  362. sljit_uw cpool_fill;
  363. /* Other members. */
  364. /* Contains pointer, "ldr pc, [...]" pairs. */
  365. sljit_uw patches;
  366. #endif
  367. #if (defined SLJIT_CONFIG_ARM_V5 && SLJIT_CONFIG_ARM_V5) || (defined SLJIT_CONFIG_ARM_V7 && SLJIT_CONFIG_ARM_V7)
  368. /* Temporary fields. */
  369. sljit_uw shift_imm;
  370. #endif
  371. #if (defined SLJIT_CONFIG_PPC && SLJIT_CONFIG_PPC)
  372. sljit_sw imm;
  373. #endif
  374. #if (defined SLJIT_CONFIG_MIPS && SLJIT_CONFIG_MIPS)
  375. sljit_s32 delay_slot;
  376. sljit_s32 cache_arg;
  377. sljit_sw cache_argw;
  378. #endif
  379. #if (defined SLJIT_CONFIG_SPARC_32 && SLJIT_CONFIG_SPARC_32)
  380. sljit_s32 delay_slot;
  381. sljit_s32 cache_arg;
  382. sljit_sw cache_argw;
  383. #endif
  384. #if (defined SLJIT_CONFIG_TILEGX && SLJIT_CONFIG_TILEGX)
  385. sljit_s32 cache_arg;
  386. sljit_sw cache_argw;
  387. #endif
  388. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  389. FILE* verbose;
  390. #endif
  391. #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
  392. || (defined SLJIT_DEBUG && SLJIT_DEBUG)
  393. /* Flags specified by the last arithmetic instruction.
  394. It contains the type of the variable flag. */
  395. sljit_s32 last_flags;
  396. /* Local size passed to the functions. */
  397. sljit_s32 logical_local_size;
  398. #endif
  399. #if (defined SLJIT_ARGUMENT_CHECKS && SLJIT_ARGUMENT_CHECKS) \
  400. || (defined SLJIT_DEBUG && SLJIT_DEBUG) \
  401. || (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  402. /* Trust arguments when the API function is called. */
  403. sljit_s32 skip_checks;
  404. #endif
  405. };
  406. /* --------------------------------------------------------------------- */
  407. /* Main functions */
  408. /* --------------------------------------------------------------------- */
  409. /* Creates an sljit compiler. The allocator_data is required by some
  410. custom memory managers. This pointer is passed to SLJIT_MALLOC
  411. and SLJIT_FREE macros. Most allocators (including the default
  412. one) ignores this value, and it is recommended to pass NULL
  413. as a dummy value for allocator_data.
  414. Returns NULL if failed. */
  415. SLJIT_API_FUNC_ATTRIBUTE struct sljit_compiler* sljit_create_compiler(void *allocator_data);
  416. /* Frees everything except the compiled machine code. */
  417. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_compiler(struct sljit_compiler *compiler);
  418. /* Returns the current error code. If an error is occurred, future sljit
  419. calls which uses the same compiler argument returns early with the same
  420. error code. Thus there is no need for checking the error after every
  421. call, it is enough to do it before the code is compiled. Removing
  422. these checks increases the performance of the compiling process. */
  423. static SLJIT_INLINE sljit_s32 sljit_get_compiler_error(struct sljit_compiler *compiler) { return compiler->error; }
  424. /* Sets the compiler error code to SLJIT_ERR_ALLOC_FAILED except
  425. if an error was detected before. After the error code is set
  426. the compiler behaves as if the allocation failure happened
  427. during an sljit function call. This can greatly simplify error
  428. checking, since only the compiler status needs to be checked
  429. after the compilation. */
  430. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_compiler_memory_error(struct sljit_compiler *compiler);
  431. /*
  432. Allocate a small amount of memory. The size must be <= 64 bytes on 32 bit,
  433. and <= 128 bytes on 64 bit architectures. The memory area is owned by the
  434. compiler, and freed by sljit_free_compiler. The returned pointer is
  435. sizeof(sljit_sw) aligned. Excellent for allocating small blocks during
  436. the compiling, and no need to worry about freeing them. The size is
  437. enough to contain at most 16 pointers. If the size is outside of the range,
  438. the function will return with NULL. However, this return value does not
  439. indicate that there is no more memory (does not set the current error code
  440. of the compiler to out-of-memory status).
  441. */
  442. SLJIT_API_FUNC_ATTRIBUTE void* sljit_alloc_memory(struct sljit_compiler *compiler, sljit_s32 size);
  443. #if (defined SLJIT_VERBOSE && SLJIT_VERBOSE)
  444. /* Passing NULL disables verbose. */
  445. SLJIT_API_FUNC_ATTRIBUTE void sljit_compiler_verbose(struct sljit_compiler *compiler, FILE* verbose);
  446. #endif
  447. /*
  448. Create executable code from the sljit instruction stream. This is the final step
  449. of the code generation so no more instructions can be added after this call.
  450. */
  451. SLJIT_API_FUNC_ATTRIBUTE void* sljit_generate_code(struct sljit_compiler *compiler);
  452. /* Free executable code. */
  453. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_code(void* code);
  454. /*
  455. When the protected executable allocator is used the JIT code is mapped
  456. twice. The first mapping has read/write and the second mapping has read/exec
  457. permissions. This function returns with the relative offset of the executable
  458. mapping using the writable mapping as the base after the machine code is
  459. successfully generated. The returned value is always 0 for the normal executable
  460. allocator, since it uses only one mapping with read/write/exec permissions.
  461. Dynamic code modifications requires this value.
  462. Before a successful code generation, this function returns with 0.
  463. */
  464. static SLJIT_INLINE sljit_sw sljit_get_executable_offset(struct sljit_compiler *compiler) { return compiler->executable_offset; }
  465. /*
  466. The executable memory consumption of the generated code can be retrieved by
  467. this function. The returned value can be used for statistical purposes.
  468. Before a successful code generation, this function returns with 0.
  469. */
  470. static SLJIT_INLINE sljit_uw sljit_get_generated_code_size(struct sljit_compiler *compiler) { return compiler->executable_size; }
  471. /* Returns with non-zero if the feature or limitation type passed as its
  472. argument is present on the current CPU.
  473. Some features (e.g. floating point operations) require hardware (CPU)
  474. support while others (e.g. move with update) are emulated if not available.
  475. However even if a feature is emulated, specialized code paths can be faster
  476. than the emulation. Some limitations are emulated as well so their general
  477. case is supported but it has extra performance costs. */
  478. /* [Not emulated] Floating-point support is available. */
  479. #define SLJIT_HAS_FPU 0
  480. /* [Limitation] Some registers are virtual registers. */
  481. #define SLJIT_HAS_VIRTUAL_REGISTERS 1
  482. /* [Emulated] Count leading zero is supported. */
  483. #define SLJIT_HAS_CLZ 2
  484. /* [Emulated] Conditional move is supported. */
  485. #define SLJIT_HAS_CMOV 3
  486. #if (defined SLJIT_CONFIG_X86 && SLJIT_CONFIG_X86)
  487. /* [Not emulated] SSE2 support is available on x86. */
  488. #define SLJIT_HAS_SSE2 100
  489. #endif
  490. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_has_cpu_feature(sljit_s32 feature_type);
  491. /* Instruction generation. Returns with any error code. If there is no
  492. error, they return with SLJIT_SUCCESS. */
  493. /*
  494. The executable code is a function from the viewpoint of the C
  495. language. The function calls must obey to the ABI (Application
  496. Binary Interface) of the platform, which specify the purpose of
  497. machine registers and stack handling among other things. The
  498. sljit_emit_enter function emits the necessary instructions for
  499. setting up a new context for the executable code and moves function
  500. arguments to the saved registers. Furthermore the options argument
  501. can be used to pass configuration options to the compiler. The
  502. available options are listed before sljit_emit_enter.
  503. The function argument list is the combination of SLJIT_ARGx
  504. (SLJIT_DEF_ARG1) macros. Currently maximum 3 SW / UW
  505. (SLJIT_ARG_TYPE_SW / LJIT_ARG_TYPE_UW) arguments are supported.
  506. The first argument goes to SLJIT_S0, the second goes to SLJIT_S1
  507. and so on. The register set used by the function must be declared
  508. as well. The number of scratch and saved registers used by the
  509. function must be passed to sljit_emit_enter. Only R registers
  510. between R0 and "scratches" argument can be used later. E.g. if
  511. "scratches" is set to 2, the scratch register set will be limited
  512. to SLJIT_R0 and SLJIT_R1. The S registers and the floating point
  513. registers ("fscratches" and "fsaveds") are specified in a similar
  514. manner. The sljit_emit_enter is also capable of allocating a stack
  515. space for local variables. The "local_size" argument contains the
  516. size in bytes of this local area and its staring address is stored
  517. in SLJIT_SP. The memory area between SLJIT_SP (inclusive) and
  518. SLJIT_SP + local_size (exclusive) can be modified freely until
  519. the function returns. The stack space is not initialized.
  520. Note: the following conditions must met:
  521. 0 <= scratches <= SLJIT_NUMBER_OF_REGISTERS
  522. 0 <= saveds <= SLJIT_NUMBER_OF_REGISTERS
  523. scratches + saveds <= SLJIT_NUMBER_OF_REGISTERS
  524. 0 <= fscratches <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  525. 0 <= fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  526. fscratches + fsaveds <= SLJIT_NUMBER_OF_FLOAT_REGISTERS
  527. Note: every call of sljit_emit_enter and sljit_set_context
  528. overwrites the previous context.
  529. */
  530. /* The absolute address returned by sljit_get_local_base with
  531. offset 0 is aligned to sljit_f64. Otherwise it is aligned to sljit_sw. */
  532. #define SLJIT_F64_ALIGNMENT 0x00000001
  533. /* The local_size must be >= 0 and <= SLJIT_MAX_LOCAL_SIZE. */
  534. #define SLJIT_MAX_LOCAL_SIZE 65536
  535. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_enter(struct sljit_compiler *compiler,
  536. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  537. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
  538. /* The machine code has a context (which contains the local stack space size,
  539. number of used registers, etc.) which initialized by sljit_emit_enter. Several
  540. functions (like sljit_emit_return) requres this context to be able to generate
  541. the appropriate code. However, some code fragments (like inline cache) may have
  542. no normal entry point so their context is unknown for the compiler. Their context
  543. can be provided to the compiler by the sljit_set_context function.
  544. Note: every call of sljit_emit_enter and sljit_set_context overwrites
  545. the previous context. */
  546. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_set_context(struct sljit_compiler *compiler,
  547. sljit_s32 options, sljit_s32 arg_types, sljit_s32 scratches, sljit_s32 saveds,
  548. sljit_s32 fscratches, sljit_s32 fsaveds, sljit_s32 local_size);
  549. /* Return from machine code. The op argument can be SLJIT_UNUSED which means the
  550. function does not return with anything or any opcode between SLJIT_MOV and
  551. SLJIT_MOV_P (see sljit_emit_op1). As for src and srcw they must be 0 if op
  552. is SLJIT_UNUSED, otherwise see below the description about source and
  553. destination arguments. */
  554. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_return(struct sljit_compiler *compiler, sljit_s32 op,
  555. sljit_s32 src, sljit_sw srcw);
  556. /* Generating entry and exit points for fast call functions (see SLJIT_FAST_CALL).
  557. Both sljit_emit_fast_enter and sljit_emit_fast_return functions preserve the
  558. values of all registers and stack frame. The return address is stored in the
  559. dst argument of sljit_emit_fast_enter, and this return address can be passed
  560. to sljit_emit_fast_return to continue the execution after the fast call.
  561. Fast calls are cheap operations (usually only a single call instruction is
  562. emitted) but they do not preserve any registers. However the callee function
  563. can freely use / update any registers and stack values which can be
  564. efficiently exploited by various optimizations. Registers can be saved
  565. manually by the callee function if needed.
  566. Although returning to different address by sljit_emit_fast_return is possible,
  567. this address usually cannot be predicted by the return address predictor of
  568. modern CPUs which may reduce performance. Furthermore using sljit_emit_ijump
  569. to return is also inefficient since return address prediction is usually
  570. triggered by a specific form of ijump.
  571. Flags: - (does not modify flags). */
  572. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_enter(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw);
  573. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fast_return(struct sljit_compiler *compiler, sljit_s32 src, sljit_sw srcw);
  574. /*
  575. Source and destination operands for arithmetical instructions
  576. imm - a simple immediate value (cannot be used as a destination)
  577. reg - any of the registers (immediate argument must be 0)
  578. [imm] - absolute immediate memory address
  579. [reg+imm] - indirect memory address
  580. [reg+(reg<<imm)] - indirect indexed memory address (shift must be between 0 and 3)
  581. useful for (byte, half, int, sljit_sw) array access
  582. (fully supported by both x86 and ARM architectures, and cheap operation on others)
  583. */
  584. /*
  585. IMPORATNT NOTE: memory access MUST be naturally aligned except
  586. SLJIT_UNALIGNED macro is defined and its value is 1.
  587. length | alignment
  588. ---------+-----------
  589. byte | 1 byte (any physical_address is accepted)
  590. half | 2 byte (physical_address & 0x1 == 0)
  591. int | 4 byte (physical_address & 0x3 == 0)
  592. word | 4 byte if SLJIT_32BIT_ARCHITECTURE is defined and its value is 1
  593. | 8 byte if SLJIT_64BIT_ARCHITECTURE is defined and its value is 1
  594. pointer | size of sljit_p type (4 byte on 32 bit machines, 4 or 8 byte
  595. | on 64 bit machines)
  596. Note: Different architectures have different addressing limitations.
  597. A single instruction is enough for the following addressing
  598. modes. Other adrressing modes are emulated by instruction
  599. sequences. This information could help to improve those code
  600. generators which focuses only a few architectures.
  601. x86: [reg+imm], -2^32+1 <= imm <= 2^32-1 (full address space on x86-32)
  602. [reg+(reg<<imm)] is supported
  603. [imm], -2^32+1 <= imm <= 2^32-1 is supported
  604. Write-back is not supported
  605. arm: [reg+imm], -4095 <= imm <= 4095 or -255 <= imm <= 255 for signed
  606. bytes, any halfs or floating point values)
  607. [reg+(reg<<imm)] is supported
  608. Write-back is supported
  609. arm-t2: [reg+imm], -255 <= imm <= 4095
  610. [reg+(reg<<imm)] is supported
  611. Write back is supported only for [reg+imm], where -255 <= imm <= 255
  612. arm64: [reg+imm], -256 <= imm <= 255, 0 <= aligned imm <= 4095 * alignment
  613. [reg+(reg<<imm)] is supported
  614. Write back is supported only for [reg+imm], where -256 <= imm <= 255
  615. ppc: [reg+imm], -65536 <= imm <= 65535. 64 bit loads/stores and 32 bit
  616. signed load on 64 bit requires immediates divisible by 4.
  617. [reg+imm] is not supported for signed 8 bit values.
  618. [reg+reg] is supported
  619. Write-back is supported except for one instruction: 32 bit signed
  620. load with [reg+imm] addressing mode on 64 bit.
  621. mips: [reg+imm], -65536 <= imm <= 65535
  622. sparc: [reg+imm], -4096 <= imm <= 4095
  623. [reg+reg] is supported
  624. */
  625. /* Macros for specifying operand types. */
  626. #define SLJIT_MEM 0x80
  627. #define SLJIT_MEM0() (SLJIT_MEM)
  628. #define SLJIT_MEM1(r1) (SLJIT_MEM | (r1))
  629. #define SLJIT_MEM2(r1, r2) (SLJIT_MEM | (r1) | ((r2) << 8))
  630. #define SLJIT_IMM 0x40
  631. /* Set 32 bit operation mode (I) on 64 bit CPUs. This option is ignored on
  632. 32 bit CPUs. When this option is set for an arithmetic operation, only
  633. the lower 32 bit of the input registers are used, and the CPU status
  634. flags are set according to the 32 bit result. Although the higher 32 bit
  635. of the input and the result registers are not defined by SLJIT, it might
  636. be defined by the CPU architecture (e.g. MIPS). To satisfy these CPU
  637. requirements all source registers must be the result of those operations
  638. where this option was also set. Memory loads read 32 bit values rather
  639. than 64 bit ones. In other words 32 bit and 64 bit operations cannot
  640. be mixed. The only exception is SLJIT_MOV32 and SLJIT_MOVU32 whose source
  641. register can hold any 32 or 64 bit value, and it is converted to a 32 bit
  642. compatible format first. This conversion is free (no instructions are
  643. emitted) on most CPUs. A 32 bit value can also be converted to a 64 bit
  644. value by SLJIT_MOV_S32 (sign extension) or SLJIT_MOV_U32 (zero extension).
  645. Note: memory addressing always uses 64 bit values on 64 bit systems so
  646. the result of a 32 bit operation must not be used with SLJIT_MEMx
  647. macros.
  648. This option is part of the instruction name, so there is no need to
  649. manually set it. E.g:
  650. SLJIT_ADD32 == (SLJIT_ADD | SLJIT_I32_OP) */
  651. #define SLJIT_I32_OP 0x100
  652. /* Set F32 (single) precision mode for floating-point computation. This
  653. option is similar to SLJIT_I32_OP, it just applies to floating point
  654. registers. When this option is passed, the CPU performs 32 bit floating
  655. point operations, rather than 64 bit one. Similar to SLJIT_I32_OP, all
  656. register arguments must be the result of those operations where this
  657. option was also set.
  658. This option is part of the instruction name, so there is no need to
  659. manually set it. E.g:
  660. SLJIT_MOV_F32 = (SLJIT_MOV_F64 | SLJIT_F32_OP)
  661. */
  662. #define SLJIT_F32_OP SLJIT_I32_OP
  663. /* Many CPUs (x86, ARM, PPC) have status flags which can be set according
  664. to the result of an operation. Other CPUs (MIPS) do not have status
  665. flags, and results must be stored in registers. To cover both architecture
  666. types efficiently only two flags are defined by SLJIT:
  667. * Zero (equal) flag: it is set if the result is zero
  668. * Variable flag: its value is defined by the last arithmetic operation
  669. SLJIT instructions can set any or both of these flags. The value of
  670. these flags is undefined if the instruction does not specify their value.
  671. The description of each instruction contains the list of allowed flag
  672. types.
  673. Example: SLJIT_ADD can set the Z, OVERFLOW, CARRY flags hence
  674. sljit_op2(..., SLJIT_ADD, ...)
  675. Both the zero and variable flags are undefined so they can
  676. have any value after the operation is completed.
  677. sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z, ...)
  678. Sets the zero flag if the result is zero, clears it otherwise.
  679. The variable flag is undefined.
  680. sljit_op2(..., SLJIT_ADD | SLJIT_SET_OVERFLOW, ...)
  681. Sets the variable flag if an integer overflow occurs, clears
  682. it otherwise. The zero flag is undefined.
  683. sljit_op2(..., SLJIT_ADD | SLJIT_SET_Z | SLJIT_SET_CARRY, ...)
  684. Sets the zero flag if the result is zero, clears it otherwise.
  685. Sets the variable flag if unsigned overflow (carry) occurs,
  686. clears it otherwise.
  687. If an instruction (e.g. SLJIT_MOV) does not modify flags the flags are
  688. unchanged.
  689. Using these flags can reduce the number of emitted instructions. E.g. a
  690. fast loop can be implemented by decreasing a counter register and set the
  691. zero flag to jump back if the counter register has not reached zero.
  692. Motivation: although CPUs can set a large number of flags, usually their
  693. values are ignored or only one of them is used. Emulating a large number
  694. of flags on systems without flag register is complicated so SLJIT
  695. instructions must specify the flag they want to use and only that flag
  696. will be emulated. The last arithmetic instruction can be repeated if
  697. multiple flags need to be checked.
  698. */
  699. /* Set Zero status flag. */
  700. #define SLJIT_SET_Z 0x0200
  701. /* Set the variable status flag if condition is true.
  702. See comparison types. */
  703. #define SLJIT_SET(condition) ((condition) << 10)
  704. /* Notes:
  705. - you cannot postpone conditional jump instructions except if noted that
  706. the instruction does not set flags (See: SLJIT_KEEP_FLAGS).
  707. - flag combinations: '|' means 'logical or'. */
  708. /* Starting index of opcodes for sljit_emit_op0. */
  709. #define SLJIT_OP0_BASE 0
  710. /* Flags: - (does not modify flags)
  711. Note: breakpoint instruction is not supported by all architectures (e.g. ppc)
  712. It falls back to SLJIT_NOP in those cases. */
  713. #define SLJIT_BREAKPOINT (SLJIT_OP0_BASE + 0)
  714. /* Flags: - (does not modify flags)
  715. Note: may or may not cause an extra cycle wait
  716. it can even decrease the runtime in a few cases. */
  717. #define SLJIT_NOP (SLJIT_OP0_BASE + 1)
  718. /* Flags: - (may destroy flags)
  719. Unsigned multiplication of SLJIT_R0 and SLJIT_R1.
  720. Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
  721. #define SLJIT_LMUL_UW (SLJIT_OP0_BASE + 2)
  722. /* Flags: - (may destroy flags)
  723. Signed multiplication of SLJIT_R0 and SLJIT_R1.
  724. Result is placed into SLJIT_R1:SLJIT_R0 (high:low) word */
  725. #define SLJIT_LMUL_SW (SLJIT_OP0_BASE + 3)
  726. /* Flags: - (may destroy flags)
  727. Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  728. The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
  729. Note: if SLJIT_R1 is 0, the behaviour is undefined. */
  730. #define SLJIT_DIVMOD_UW (SLJIT_OP0_BASE + 4)
  731. #define SLJIT_DIVMOD_U32 (SLJIT_DIVMOD_UW | SLJIT_I32_OP)
  732. /* Flags: - (may destroy flags)
  733. Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  734. The result is placed into SLJIT_R0 and the remainder into SLJIT_R1.
  735. Note: if SLJIT_R1 is 0, the behaviour is undefined.
  736. Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
  737. the behaviour is undefined. */
  738. #define SLJIT_DIVMOD_SW (SLJIT_OP0_BASE + 5)
  739. #define SLJIT_DIVMOD_S32 (SLJIT_DIVMOD_SW | SLJIT_I32_OP)
  740. /* Flags: - (may destroy flags)
  741. Unsigned divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  742. The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
  743. Note: if SLJIT_R1 is 0, the behaviour is undefined. */
  744. #define SLJIT_DIV_UW (SLJIT_OP0_BASE + 6)
  745. #define SLJIT_DIV_U32 (SLJIT_DIV_UW | SLJIT_I32_OP)
  746. /* Flags: - (may destroy flags)
  747. Signed divide of the value in SLJIT_R0 by the value in SLJIT_R1.
  748. The result is placed into SLJIT_R0. SLJIT_R1 preserves its value.
  749. Note: if SLJIT_R1 is 0, the behaviour is undefined.
  750. Note: if SLJIT_R1 is -1 and SLJIT_R0 is integer min (0x800..00),
  751. the behaviour is undefined. */
  752. #define SLJIT_DIV_SW (SLJIT_OP0_BASE + 7)
  753. #define SLJIT_DIV_S32 (SLJIT_DIV_SW | SLJIT_I32_OP)
  754. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op0(struct sljit_compiler *compiler, sljit_s32 op);
  755. /* Starting index of opcodes for sljit_emit_op1. */
  756. #define SLJIT_OP1_BASE 32
  757. /* The MOV instruction transfers data from source to destination.
  758. MOV instruction suffixes:
  759. U8 - unsigned 8 bit data transfer
  760. S8 - signed 8 bit data transfer
  761. U16 - unsigned 16 bit data transfer
  762. S16 - signed 16 bit data transfer
  763. U32 - unsigned int (32 bit) data transfer
  764. S32 - signed int (32 bit) data transfer
  765. P - pointer (sljit_p) data transfer
  766. If the destination of a MOV instruction is SLJIT_UNUSED and the source
  767. operand is a memory address the compiler emits a prefetch instruction
  768. if this instruction is supported by the current CPU. Higher data sizes
  769. bring the data closer to the core: a MOV with word size loads the data
  770. into a higher level cache than a byte size. Otherwise the type does not
  771. affect the prefetch instruction. Furthermore a prefetch instruction
  772. never fails, so it can be used to prefetch a data from an address and
  773. check whether that address is NULL afterwards.
  774. */
  775. /* Flags: - (does not modify flags) */
  776. #define SLJIT_MOV (SLJIT_OP1_BASE + 0)
  777. /* Flags: - (does not modify flags) */
  778. #define SLJIT_MOV_U8 (SLJIT_OP1_BASE + 1)
  779. #define SLJIT_MOV32_U8 (SLJIT_MOV_U8 | SLJIT_I32_OP)
  780. /* Flags: - (does not modify flags) */
  781. #define SLJIT_MOV_S8 (SLJIT_OP1_BASE + 2)
  782. #define SLJIT_MOV32_S8 (SLJIT_MOV_S8 | SLJIT_I32_OP)
  783. /* Flags: - (does not modify flags) */
  784. #define SLJIT_MOV_U16 (SLJIT_OP1_BASE + 3)
  785. #define SLJIT_MOV32_U16 (SLJIT_MOV_U16 | SLJIT_I32_OP)
  786. /* Flags: - (does not modify flags) */
  787. #define SLJIT_MOV_S16 (SLJIT_OP1_BASE + 4)
  788. #define SLJIT_MOV32_S16 (SLJIT_MOV_S16 | SLJIT_I32_OP)
  789. /* Flags: - (does not modify flags)
  790. Note: no SLJIT_MOV32_U32 form, since it is the same as SLJIT_MOV32 */
  791. #define SLJIT_MOV_U32 (SLJIT_OP1_BASE + 5)
  792. /* Flags: - (does not modify flags)
  793. Note: no SLJIT_MOV32_S32 form, since it is the same as SLJIT_MOV32 */
  794. #define SLJIT_MOV_S32 (SLJIT_OP1_BASE + 6)
  795. /* Flags: - (does not modify flags) */
  796. #define SLJIT_MOV32 (SLJIT_MOV_S32 | SLJIT_I32_OP)
  797. /* Flags: - (does not modify flags)
  798. Note: load a pointer sized data, useful on x32 (a 32 bit mode on x86-64
  799. where all x64 features are available, e.g. 16 register) or similar
  800. compiling modes */
  801. #define SLJIT_MOV_P (SLJIT_OP1_BASE + 7)
  802. /* Flags: Z
  803. Note: immediate source argument is not supported */
  804. #define SLJIT_NOT (SLJIT_OP1_BASE + 8)
  805. #define SLJIT_NOT32 (SLJIT_NOT | SLJIT_I32_OP)
  806. /* Flags: Z | OVERFLOW
  807. Note: immediate source argument is not supported */
  808. #define SLJIT_NEG (SLJIT_OP1_BASE + 9)
  809. #define SLJIT_NEG32 (SLJIT_NEG | SLJIT_I32_OP)
  810. /* Count leading zeroes
  811. Flags: - (may destroy flags)
  812. Note: immediate source argument is not supported */
  813. #define SLJIT_CLZ (SLJIT_OP1_BASE + 10)
  814. #define SLJIT_CLZ32 (SLJIT_CLZ | SLJIT_I32_OP)
  815. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op1(struct sljit_compiler *compiler, sljit_s32 op,
  816. sljit_s32 dst, sljit_sw dstw,
  817. sljit_s32 src, sljit_sw srcw);
  818. /* Starting index of opcodes for sljit_emit_op2. */
  819. #define SLJIT_OP2_BASE 96
  820. /* Flags: Z | OVERFLOW | CARRY */
  821. #define SLJIT_ADD (SLJIT_OP2_BASE + 0)
  822. #define SLJIT_ADD32 (SLJIT_ADD | SLJIT_I32_OP)
  823. /* Flags: CARRY */
  824. #define SLJIT_ADDC (SLJIT_OP2_BASE + 1)
  825. #define SLJIT_ADDC32 (SLJIT_ADDC | SLJIT_I32_OP)
  826. /* Flags: Z | LESS | GREATER_EQUAL | GREATER | LESS_EQUAL
  827. SIG_LESS | SIG_GREATER_EQUAL | SIG_GREATER
  828. SIG_LESS_EQUAL | CARRY */
  829. #define SLJIT_SUB (SLJIT_OP2_BASE + 2)
  830. #define SLJIT_SUB32 (SLJIT_SUB | SLJIT_I32_OP)
  831. /* Flags: CARRY */
  832. #define SLJIT_SUBC (SLJIT_OP2_BASE + 3)
  833. #define SLJIT_SUBC32 (SLJIT_SUBC | SLJIT_I32_OP)
  834. /* Note: integer mul
  835. Flags: MUL_OVERFLOW */
  836. #define SLJIT_MUL (SLJIT_OP2_BASE + 4)
  837. #define SLJIT_MUL32 (SLJIT_MUL | SLJIT_I32_OP)
  838. /* Flags: Z */
  839. #define SLJIT_AND (SLJIT_OP2_BASE + 5)
  840. #define SLJIT_AND32 (SLJIT_AND | SLJIT_I32_OP)
  841. /* Flags: Z */
  842. #define SLJIT_OR (SLJIT_OP2_BASE + 6)
  843. #define SLJIT_OR32 (SLJIT_OR | SLJIT_I32_OP)
  844. /* Flags: Z */
  845. #define SLJIT_XOR (SLJIT_OP2_BASE + 7)
  846. #define SLJIT_XOR32 (SLJIT_XOR | SLJIT_I32_OP)
  847. /* Flags: Z
  848. Let bit_length be the length of the shift operation: 32 or 64.
  849. If src2 is immediate, src2w is masked by (bit_length - 1).
  850. Otherwise, if the content of src2 is outside the range from 0
  851. to bit_length - 1, the result is undefined. */
  852. #define SLJIT_SHL (SLJIT_OP2_BASE + 8)
  853. #define SLJIT_SHL32 (SLJIT_SHL | SLJIT_I32_OP)
  854. /* Flags: Z
  855. Let bit_length be the length of the shift operation: 32 or 64.
  856. If src2 is immediate, src2w is masked by (bit_length - 1).
  857. Otherwise, if the content of src2 is outside the range from 0
  858. to bit_length - 1, the result is undefined. */
  859. #define SLJIT_LSHR (SLJIT_OP2_BASE + 9)
  860. #define SLJIT_LSHR32 (SLJIT_LSHR | SLJIT_I32_OP)
  861. /* Flags: Z
  862. Let bit_length be the length of the shift operation: 32 or 64.
  863. If src2 is immediate, src2w is masked by (bit_length - 1).
  864. Otherwise, if the content of src2 is outside the range from 0
  865. to bit_length - 1, the result is undefined. */
  866. #define SLJIT_ASHR (SLJIT_OP2_BASE + 10)
  867. #define SLJIT_ASHR32 (SLJIT_ASHR | SLJIT_I32_OP)
  868. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op2(struct sljit_compiler *compiler, sljit_s32 op,
  869. sljit_s32 dst, sljit_sw dstw,
  870. sljit_s32 src1, sljit_sw src1w,
  871. sljit_s32 src2, sljit_sw src2w);
  872. /* Starting index of opcodes for sljit_emit_fop1. */
  873. #define SLJIT_FOP1_BASE 128
  874. /* Flags: - (does not modify flags) */
  875. #define SLJIT_MOV_F64 (SLJIT_FOP1_BASE + 0)
  876. #define SLJIT_MOV_F32 (SLJIT_MOV_F64 | SLJIT_F32_OP)
  877. /* Convert opcodes: CONV[DST_TYPE].FROM[SRC_TYPE]
  878. SRC/DST TYPE can be: D - double, S - single, W - signed word, I - signed int
  879. Rounding mode when the destination is W or I: round towards zero. */
  880. /* Flags: - (does not modify flags) */
  881. #define SLJIT_CONV_F64_FROM_F32 (SLJIT_FOP1_BASE + 1)
  882. #define SLJIT_CONV_F32_FROM_F64 (SLJIT_CONV_F64_FROM_F32 | SLJIT_F32_OP)
  883. /* Flags: - (does not modify flags) */
  884. #define SLJIT_CONV_SW_FROM_F64 (SLJIT_FOP1_BASE + 2)
  885. #define SLJIT_CONV_SW_FROM_F32 (SLJIT_CONV_SW_FROM_F64 | SLJIT_F32_OP)
  886. /* Flags: - (does not modify flags) */
  887. #define SLJIT_CONV_S32_FROM_F64 (SLJIT_FOP1_BASE + 3)
  888. #define SLJIT_CONV_S32_FROM_F32 (SLJIT_CONV_S32_FROM_F64 | SLJIT_F32_OP)
  889. /* Flags: - (does not modify flags) */
  890. #define SLJIT_CONV_F64_FROM_SW (SLJIT_FOP1_BASE + 4)
  891. #define SLJIT_CONV_F32_FROM_SW (SLJIT_CONV_F64_FROM_SW | SLJIT_F32_OP)
  892. /* Flags: - (does not modify flags) */
  893. #define SLJIT_CONV_F64_FROM_S32 (SLJIT_FOP1_BASE + 5)
  894. #define SLJIT_CONV_F32_FROM_S32 (SLJIT_CONV_F64_FROM_S32 | SLJIT_F32_OP)
  895. /* Note: dst is the left and src is the right operand for SLJIT_CMPD.
  896. Flags: EQUAL_F | LESS_F | GREATER_EQUAL_F | GREATER_F | LESS_EQUAL_F */
  897. #define SLJIT_CMP_F64 (SLJIT_FOP1_BASE + 6)
  898. #define SLJIT_CMP_F32 (SLJIT_CMP_F64 | SLJIT_F32_OP)
  899. /* Flags: - (does not modify flags) */
  900. #define SLJIT_NEG_F64 (SLJIT_FOP1_BASE + 7)
  901. #define SLJIT_NEG_F32 (SLJIT_NEG_F64 | SLJIT_F32_OP)
  902. /* Flags: - (does not modify flags) */
  903. #define SLJIT_ABS_F64 (SLJIT_FOP1_BASE + 8)
  904. #define SLJIT_ABS_F32 (SLJIT_ABS_F64 | SLJIT_F32_OP)
  905. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop1(struct sljit_compiler *compiler, sljit_s32 op,
  906. sljit_s32 dst, sljit_sw dstw,
  907. sljit_s32 src, sljit_sw srcw);
  908. /* Starting index of opcodes for sljit_emit_fop2. */
  909. #define SLJIT_FOP2_BASE 160
  910. /* Flags: - (does not modify flags) */
  911. #define SLJIT_ADD_F64 (SLJIT_FOP2_BASE + 0)
  912. #define SLJIT_ADD_F32 (SLJIT_ADD_F64 | SLJIT_F32_OP)
  913. /* Flags: - (does not modify flags) */
  914. #define SLJIT_SUB_F64 (SLJIT_FOP2_BASE + 1)
  915. #define SLJIT_SUB_F32 (SLJIT_SUB_F64 | SLJIT_F32_OP)
  916. /* Flags: - (does not modify flags) */
  917. #define SLJIT_MUL_F64 (SLJIT_FOP2_BASE + 2)
  918. #define SLJIT_MUL_F32 (SLJIT_MUL_F64 | SLJIT_F32_OP)
  919. /* Flags: - (does not modify flags) */
  920. #define SLJIT_DIV_F64 (SLJIT_FOP2_BASE + 3)
  921. #define SLJIT_DIV_F32 (SLJIT_DIV_F64 | SLJIT_F32_OP)
  922. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fop2(struct sljit_compiler *compiler, sljit_s32 op,
  923. sljit_s32 dst, sljit_sw dstw,
  924. sljit_s32 src1, sljit_sw src1w,
  925. sljit_s32 src2, sljit_sw src2w);
  926. /* Label and jump instructions. */
  927. SLJIT_API_FUNC_ATTRIBUTE struct sljit_label* sljit_emit_label(struct sljit_compiler *compiler);
  928. /* Invert (negate) conditional type: xor (^) with 0x1 */
  929. /* Integer comparison types. */
  930. #define SLJIT_EQUAL 0
  931. #define SLJIT_EQUAL32 (SLJIT_EQUAL | SLJIT_I32_OP)
  932. #define SLJIT_ZERO 0
  933. #define SLJIT_ZERO32 (SLJIT_ZERO | SLJIT_I32_OP)
  934. #define SLJIT_NOT_EQUAL 1
  935. #define SLJIT_NOT_EQUAL32 (SLJIT_NOT_EQUAL | SLJIT_I32_OP)
  936. #define SLJIT_NOT_ZERO 1
  937. #define SLJIT_NOT_ZERO32 (SLJIT_NOT_ZERO | SLJIT_I32_OP)
  938. #define SLJIT_LESS 2
  939. #define SLJIT_LESS32 (SLJIT_LESS | SLJIT_I32_OP)
  940. #define SLJIT_SET_LESS SLJIT_SET(SLJIT_LESS)
  941. #define SLJIT_GREATER_EQUAL 3
  942. #define SLJIT_GREATER_EQUAL32 (SLJIT_GREATER_EQUAL | SLJIT_I32_OP)
  943. #define SLJIT_SET_GREATER_EQUAL SLJIT_SET(SLJIT_GREATER_EQUAL)
  944. #define SLJIT_GREATER 4
  945. #define SLJIT_GREATER32 (SLJIT_GREATER | SLJIT_I32_OP)
  946. #define SLJIT_SET_GREATER SLJIT_SET(SLJIT_GREATER)
  947. #define SLJIT_LESS_EQUAL 5
  948. #define SLJIT_LESS_EQUAL32 (SLJIT_LESS_EQUAL | SLJIT_I32_OP)
  949. #define SLJIT_SET_LESS_EQUAL SLJIT_SET(SLJIT_LESS_EQUAL)
  950. #define SLJIT_SIG_LESS 6
  951. #define SLJIT_SIG_LESS32 (SLJIT_SIG_LESS | SLJIT_I32_OP)
  952. #define SLJIT_SET_SIG_LESS SLJIT_SET(SLJIT_SIG_LESS)
  953. #define SLJIT_SIG_GREATER_EQUAL 7
  954. #define SLJIT_SIG_GREATER_EQUAL32 (SLJIT_SIG_GREATER_EQUAL | SLJIT_I32_OP)
  955. #define SLJIT_SET_SIG_GREATER_EQUAL SLJIT_SET(SLJIT_SIG_GREATER_EQUAL)
  956. #define SLJIT_SIG_GREATER 8
  957. #define SLJIT_SIG_GREATER32 (SLJIT_SIG_GREATER | SLJIT_I32_OP)
  958. #define SLJIT_SET_SIG_GREATER SLJIT_SET(SLJIT_SIG_GREATER)
  959. #define SLJIT_SIG_LESS_EQUAL 9
  960. #define SLJIT_SIG_LESS_EQUAL32 (SLJIT_SIG_LESS_EQUAL | SLJIT_I32_OP)
  961. #define SLJIT_SET_SIG_LESS_EQUAL SLJIT_SET(SLJIT_SIG_LESS_EQUAL)
  962. #define SLJIT_OVERFLOW 10
  963. #define SLJIT_OVERFLOW32 (SLJIT_OVERFLOW | SLJIT_I32_OP)
  964. #define SLJIT_SET_OVERFLOW SLJIT_SET(SLJIT_OVERFLOW)
  965. #define SLJIT_NOT_OVERFLOW 11
  966. #define SLJIT_NOT_OVERFLOW32 (SLJIT_NOT_OVERFLOW | SLJIT_I32_OP)
  967. #define SLJIT_MUL_OVERFLOW 12
  968. #define SLJIT_MUL_OVERFLOW32 (SLJIT_MUL_OVERFLOW | SLJIT_I32_OP)
  969. #define SLJIT_SET_MUL_OVERFLOW SLJIT_SET(SLJIT_MUL_OVERFLOW)
  970. #define SLJIT_MUL_NOT_OVERFLOW 13
  971. #define SLJIT_MUL_NOT_OVERFLOW32 (SLJIT_MUL_NOT_OVERFLOW | SLJIT_I32_OP)
  972. /* There is no SLJIT_CARRY or SLJIT_NOT_CARRY. */
  973. #define SLJIT_SET_CARRY SLJIT_SET(14)
  974. /* Floating point comparison types. */
  975. #define SLJIT_EQUAL_F64 16
  976. #define SLJIT_EQUAL_F32 (SLJIT_EQUAL_F64 | SLJIT_F32_OP)
  977. #define SLJIT_SET_EQUAL_F SLJIT_SET(SLJIT_EQUAL_F64)
  978. #define SLJIT_NOT_EQUAL_F64 17
  979. #define SLJIT_NOT_EQUAL_F32 (SLJIT_NOT_EQUAL_F64 | SLJIT_F32_OP)
  980. #define SLJIT_SET_NOT_EQUAL_F SLJIT_SET(SLJIT_NOT_EQUAL_F64)
  981. #define SLJIT_LESS_F64 18
  982. #define SLJIT_LESS_F32 (SLJIT_LESS_F64 | SLJIT_F32_OP)
  983. #define SLJIT_SET_LESS_F SLJIT_SET(SLJIT_LESS_F64)
  984. #define SLJIT_GREATER_EQUAL_F64 19
  985. #define SLJIT_GREATER_EQUAL_F32 (SLJIT_GREATER_EQUAL_F64 | SLJIT_F32_OP)
  986. #define SLJIT_SET_GREATER_EQUAL_F SLJIT_SET(SLJIT_GREATER_EQUAL_F64)
  987. #define SLJIT_GREATER_F64 20
  988. #define SLJIT_GREATER_F32 (SLJIT_GREATER_F64 | SLJIT_F32_OP)
  989. #define SLJIT_SET_GREATER_F SLJIT_SET(SLJIT_GREATER_F64)
  990. #define SLJIT_LESS_EQUAL_F64 21
  991. #define SLJIT_LESS_EQUAL_F32 (SLJIT_LESS_EQUAL_F64 | SLJIT_F32_OP)
  992. #define SLJIT_SET_LESS_EQUAL_F SLJIT_SET(SLJIT_LESS_EQUAL_F64)
  993. #define SLJIT_UNORDERED_F64 22
  994. #define SLJIT_UNORDERED_F32 (SLJIT_UNORDERED_F64 | SLJIT_F32_OP)
  995. #define SLJIT_SET_UNORDERED_F SLJIT_SET(SLJIT_UNORDERED_F64)
  996. #define SLJIT_ORDERED_F64 23
  997. #define SLJIT_ORDERED_F32 (SLJIT_ORDERED_F64 | SLJIT_F32_OP)
  998. #define SLJIT_SET_ORDERED_F SLJIT_SET(SLJIT_ORDERED_F64)
  999. /* Unconditional jump types. */
  1000. #define SLJIT_JUMP 24
  1001. /* Fast calling method. See sljit_emit_fast_enter / sljit_emit_fast_return. */
  1002. #define SLJIT_FAST_CALL 25
  1003. /* Called function must be declared with the SLJIT_FUNC attribute. */
  1004. #define SLJIT_CALL 26
  1005. /* Called function must be declared with cdecl attribute.
  1006. This is the default attribute for C functions. */
  1007. #define SLJIT_CALL_CDECL 27
  1008. /* The target can be changed during runtime (see: sljit_set_jump_addr). */
  1009. #define SLJIT_REWRITABLE_JUMP 0x1000
  1010. /* Emit a jump instruction. The destination is not set, only the type of the jump.
  1011. type must be between SLJIT_EQUAL and SLJIT_FAST_CALL
  1012. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  1013. Flags: does not modify flags. */
  1014. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_jump(struct sljit_compiler *compiler, sljit_s32 type);
  1015. /* Emit a C compiler (ABI) compatible function call.
  1016. type must be SLJIT_CALL or SLJIT_CALL_CDECL
  1017. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  1018. arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
  1019. Flags: destroy all flags. */
  1020. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_call(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 arg_types);
  1021. /* Basic arithmetic comparison. In most architectures it is implemented as
  1022. an SLJIT_SUB operation (with SLJIT_UNUSED destination and setting
  1023. appropriate flags) followed by a sljit_emit_jump. However some
  1024. architectures (i.e: ARM64 or MIPS) may employ special optimizations here.
  1025. It is suggested to use this comparison form when appropriate.
  1026. type must be between SLJIT_EQUAL and SLJIT_I_SIG_LESS_EQUAL
  1027. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  1028. Flags: may destroy flags. */
  1029. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_cmp(struct sljit_compiler *compiler, sljit_s32 type,
  1030. sljit_s32 src1, sljit_sw src1w,
  1031. sljit_s32 src2, sljit_sw src2w);
  1032. /* Basic floating point comparison. In most architectures it is implemented as
  1033. an SLJIT_FCMP operation (setting appropriate flags) followed by a
  1034. sljit_emit_jump. However some architectures (i.e: MIPS) may employ
  1035. special optimizations here. It is suggested to use this comparison form
  1036. when appropriate.
  1037. type must be between SLJIT_EQUAL_F64 and SLJIT_ORDERED_F32
  1038. type can be combined (or'ed) with SLJIT_REWRITABLE_JUMP
  1039. Flags: destroy flags.
  1040. Note: if either operand is NaN, the behaviour is undefined for
  1041. types up to SLJIT_S_LESS_EQUAL. */
  1042. SLJIT_API_FUNC_ATTRIBUTE struct sljit_jump* sljit_emit_fcmp(struct sljit_compiler *compiler, sljit_s32 type,
  1043. sljit_s32 src1, sljit_sw src1w,
  1044. sljit_s32 src2, sljit_sw src2w);
  1045. /* Set the destination of the jump to this label. */
  1046. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_label(struct sljit_jump *jump, struct sljit_label* label);
  1047. /* Set the destination address of the jump to this label. */
  1048. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_target(struct sljit_jump *jump, sljit_uw target);
  1049. /* Emit an indirect jump or fast call.
  1050. Direct form: set src to SLJIT_IMM() and srcw to the address
  1051. Indirect form: any other valid addressing mode
  1052. type must be between SLJIT_JUMP and SLJIT_FAST_CALL
  1053. Flags: does not modify flags. */
  1054. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_ijump(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 src, sljit_sw srcw);
  1055. /* Emit a C compiler (ABI) compatible function call.
  1056. Direct form: set src to SLJIT_IMM() and srcw to the address
  1057. Indirect form: any other valid addressing mode
  1058. type must be SLJIT_CALL or SLJIT_CALL_CDECL
  1059. arg_types is the combination of SLJIT_RET / SLJIT_ARGx (SLJIT_DEF_RET / SLJIT_DEF_ARGx) macros
  1060. Flags: destroy all flags. */
  1061. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_icall(struct sljit_compiler *compiler, sljit_s32 type, sljit_s32 arg_types, sljit_s32 src, sljit_sw srcw);
  1062. /* Perform the operation using the conditional flags as the second argument.
  1063. Type must always be between SLJIT_EQUAL and SLJIT_ORDERED_F64. The value
  1064. represented by the type is 1, if the condition represented by the type
  1065. is fulfilled, and 0 otherwise.
  1066. If op == SLJIT_MOV, SLJIT_MOV32:
  1067. Set dst to the value represented by the type (0 or 1).
  1068. Flags: - (does not modify flags)
  1069. If op == SLJIT_OR, op == SLJIT_AND, op == SLJIT_XOR
  1070. Performs the binary operation using dst as the first, and the value
  1071. represented by type as the second argument. Result is written into dst.
  1072. Flags: Z (may destroy flags) */
  1073. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_flags(struct sljit_compiler *compiler, sljit_s32 op,
  1074. sljit_s32 dst, sljit_sw dstw,
  1075. sljit_s32 type);
  1076. /* Emit a conditional mov instruction which moves source to destination,
  1077. if the condition is satisfied. Unlike other arithmetic operations this
  1078. instruction does not support memory access.
  1079. type must be between SLJIT_EQUAL and SLJIT_ORDERED_F64
  1080. dst_reg must be a valid register and it can be combined
  1081. with SLJIT_I32_OP to perform a 32 bit arithmetic operation
  1082. src must be register or immediate (SLJIT_IMM)
  1083. Flags: - (does not modify flags) */
  1084. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_cmov(struct sljit_compiler *compiler, sljit_s32 type,
  1085. sljit_s32 dst_reg,
  1086. sljit_s32 src, sljit_sw srcw);
  1087. /* The following flags are used by sljit_emit_mem() and sljit_emit_fmem(). */
  1088. /* When SLJIT_MEM_SUPP is passed, no instructions are emitted.
  1089. Instead the function returns with SLJIT_SUCCESS if the instruction
  1090. form is supported and SLJIT_ERR_UNSUPPORTED otherwise. This flag
  1091. allows runtime checking of available instruction forms. */
  1092. #define SLJIT_MEM_SUPP 0x0200
  1093. /* Memory load operation. This is the default. */
  1094. #define SLJIT_MEM_LOAD 0x0000
  1095. /* Memory store operation. */
  1096. #define SLJIT_MEM_STORE 0x0400
  1097. /* Base register is updated before the memory access. */
  1098. #define SLJIT_MEM_PRE 0x0800
  1099. /* Base register is updated after the memory access. */
  1100. #define SLJIT_MEM_POST 0x1000
  1101. /* Emit a single memory load or store with update instruction. When the
  1102. requested instruction form is not supported by the CPU, it returns
  1103. with SLJIT_ERR_UNSUPPORTED instead of emulating the instruction. This
  1104. allows specializing tight loops based on the supported instruction
  1105. forms (see SLJIT_MEM_SUPP flag).
  1106. type must be between SLJIT_MOV and SLJIT_MOV_P and can be
  1107. combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
  1108. or SLJIT_MEM_POST must be specified.
  1109. reg is the source or destination register, and must be
  1110. different from the base register of the mem operand
  1111. mem must be a SLJIT_MEM1() or SLJIT_MEM2() operand
  1112. Flags: - (does not modify flags) */
  1113. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_mem(struct sljit_compiler *compiler, sljit_s32 type,
  1114. sljit_s32 reg,
  1115. sljit_s32 mem, sljit_sw memw);
  1116. /* Same as sljit_emit_mem except the followings:
  1117. type must be SLJIT_MOV_F64 or SLJIT_MOV_F32 and can be
  1118. combined with SLJIT_MEM_* flags. Either SLJIT_MEM_PRE
  1119. or SLJIT_MEM_POST must be specified.
  1120. freg is the source or destination floating point register */
  1121. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_fmem(struct sljit_compiler *compiler, sljit_s32 type,
  1122. sljit_s32 freg,
  1123. sljit_s32 mem, sljit_sw memw);
  1124. /* Copies the base address of SLJIT_SP + offset to dst. The offset can be
  1125. anything to negate the effect of relative addressing. For example if an
  1126. array of sljit_sw values is stored on the stack from offset 0x40, and R0
  1127. contains the offset of an array item plus 0x120, this item can be
  1128. overwritten by two SLJIT instructions:
  1129. sljit_get_local_base(compiler, SLJIT_R1, 0, 0x40 - 0x120);
  1130. sljit_emit_op1(compiler, SLJIT_MOV, SLJIT_MEM2(SLJIT_R1, SLJIT_R0), 0, SLJIT_IMM, 0x5);
  1131. Flags: - (may destroy flags) */
  1132. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_local_base(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw offset);
  1133. /* Store a value that can be changed runtime (see: sljit_get_const_addr / sljit_set_const)
  1134. Flags: - (does not modify flags) */
  1135. SLJIT_API_FUNC_ATTRIBUTE struct sljit_const* sljit_emit_const(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw, sljit_sw init_value);
  1136. /* Store the value of a label (see: sljit_set_put_label)
  1137. Flags: - (does not modify flags) */
  1138. SLJIT_API_FUNC_ATTRIBUTE struct sljit_put_label* sljit_emit_put_label(struct sljit_compiler *compiler, sljit_s32 dst, sljit_sw dstw);
  1139. /* Set the value stored by put_label to this label. */
  1140. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_put_label(struct sljit_put_label *put_label, struct sljit_label *label);
  1141. /* After the code generation the address for label, jump and const instructions
  1142. are computed. Since these structures are freed by sljit_free_compiler, the
  1143. addresses must be preserved by the user program elsewere. */
  1144. static SLJIT_INLINE sljit_uw sljit_get_label_addr(struct sljit_label *label) { return label->addr; }
  1145. static SLJIT_INLINE sljit_uw sljit_get_jump_addr(struct sljit_jump *jump) { return jump->addr; }
  1146. static SLJIT_INLINE sljit_uw sljit_get_const_addr(struct sljit_const *const_) { return const_->addr; }
  1147. /* Only the address and executable offset are required to perform dynamic
  1148. code modifications. See sljit_get_executable_offset function. */
  1149. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_jump_addr(sljit_uw addr, sljit_uw new_target, sljit_sw executable_offset);
  1150. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_const(sljit_uw addr, sljit_sw new_constant, sljit_sw executable_offset);
  1151. /* --------------------------------------------------------------------- */
  1152. /* Miscellaneous utility functions */
  1153. /* --------------------------------------------------------------------- */
  1154. #define SLJIT_MAJOR_VERSION 0
  1155. #define SLJIT_MINOR_VERSION 94
  1156. /* Get the human readable name of the platform. Can be useful on platforms
  1157. like ARM, where ARM and Thumb2 functions can be mixed, and
  1158. it is useful to know the type of the code generator. */
  1159. SLJIT_API_FUNC_ATTRIBUTE const char* sljit_get_platform_name(void);
  1160. /* Portable helper function to get an offset of a member. */
  1161. #define SLJIT_OFFSETOF(base, member) ((sljit_sw)(&((base*)0x10)->member) - 0x10)
  1162. #if (defined SLJIT_UTIL_GLOBAL_LOCK && SLJIT_UTIL_GLOBAL_LOCK)
  1163. /* This global lock is useful to compile common functions. */
  1164. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_grab_lock(void);
  1165. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_release_lock(void);
  1166. #endif
  1167. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
  1168. /* The sljit_stack structure and its manipulation functions provides
  1169. an implementation for a top-down stack. The stack top is stored
  1170. in the end field of the sljit_stack structure and the stack goes
  1171. down to the min_start field, so the memory region reserved for
  1172. this stack is between min_start (inclusive) and end (exclusive)
  1173. fields. However the application can only use the region between
  1174. start (inclusive) and end (exclusive) fields. The sljit_stack_resize
  1175. function can be used to extend this region up to min_start.
  1176. This feature uses the "address space reserve" feature of modern
  1177. operating systems. Instead of allocating a large memory block
  1178. applications can allocate a small memory region and extend it
  1179. later without moving the content of the memory area. Therefore
  1180. after a successful resize by sljit_stack_resize all pointers into
  1181. this region are still valid.
  1182. Note:
  1183. this structure may not be supported by all operating systems.
  1184. end and max_limit fields are aligned to PAGE_SIZE bytes (usually
  1185. 4 Kbyte or more).
  1186. stack should grow in larger steps, e.g. 4Kbyte, 16Kbyte or more. */
  1187. struct sljit_stack {
  1188. /* User data, anything can be stored here.
  1189. Initialized to the same value as the end field. */
  1190. sljit_u8 *top;
  1191. /* These members are read only. */
  1192. /* End address of the stack */
  1193. sljit_u8 *end;
  1194. /* Current start address of the stack. */
  1195. sljit_u8 *start;
  1196. /* Lowest start address of the stack. */
  1197. sljit_u8 *min_start;
  1198. };
  1199. /* Allocates a new stack. Returns NULL if unsuccessful.
  1200. Note: see sljit_create_compiler for the explanation of allocator_data. */
  1201. SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data);
  1202. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data);
  1203. /* Can be used to increase (extend) or decrease (shrink) the stack
  1204. memory area. Returns with new_start if successful and NULL otherwise.
  1205. It always fails if new_start is less than min_start or greater or equal
  1206. than end fields. The fields of the stack are not changed if the returned
  1207. value is NULL (the current memory content is never lost). */
  1208. SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start);
  1209. #endif /* (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) */
  1210. #if !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL)
  1211. /* Get the entry address of a given function. */
  1212. #define SLJIT_FUNC_OFFSET(func_name) ((sljit_sw)func_name)
  1213. #else /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
  1214. /* All JIT related code should be placed in the same context (library, binary, etc.). */
  1215. #define SLJIT_FUNC_OFFSET(func_name) (*(sljit_sw*)(void*)func_name)
  1216. /* For powerpc64, the function pointers point to a context descriptor. */
  1217. struct sljit_function_context {
  1218. sljit_sw addr;
  1219. sljit_sw r2;
  1220. sljit_sw r11;
  1221. };
  1222. /* Fill the context arguments using the addr and the function.
  1223. If func_ptr is NULL, it will not be set to the address of context
  1224. If addr is NULL, the function address also comes from the func pointer. */
  1225. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_function_context(void** func_ptr, struct sljit_function_context* context, sljit_sw addr, void* func);
  1226. #endif /* !(defined SLJIT_INDIRECT_CALL && SLJIT_INDIRECT_CALL) */
  1227. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  1228. /* Free unused executable memory. The allocator keeps some free memory
  1229. around to reduce the number of OS executable memory allocations.
  1230. This improves performance since these calls are costly. However
  1231. it is sometimes desired to free all unused memory regions, e.g.
  1232. before the application terminates. */
  1233. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void);
  1234. #endif
  1235. /* --------------------------------------------------------------------- */
  1236. /* CPU specific functions */
  1237. /* --------------------------------------------------------------------- */
  1238. /* The following function is a helper function for sljit_emit_op_custom.
  1239. It returns with the real machine register index ( >=0 ) of any SLJIT_R,
  1240. SLJIT_S and SLJIT_SP registers.
  1241. Note: it returns with -1 for virtual registers (only on x86-32). */
  1242. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_register_index(sljit_s32 reg);
  1243. /* The following function is a helper function for sljit_emit_op_custom.
  1244. It returns with the real machine register index of any SLJIT_FLOAT register.
  1245. Note: the index is always an even number on ARM (except ARM-64), MIPS, and SPARC. */
  1246. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_get_float_register_index(sljit_s32 reg);
  1247. /* Any instruction can be inserted into the instruction stream by
  1248. sljit_emit_op_custom. It has a similar purpose as inline assembly.
  1249. The size parameter must match to the instruction size of the target
  1250. architecture:
  1251. x86: 0 < size <= 15. The instruction argument can be byte aligned.
  1252. Thumb2: if size == 2, the instruction argument must be 2 byte aligned.
  1253. if size == 4, the instruction argument must be 4 byte aligned.
  1254. Otherwise: size must be 4 and instruction argument must be 4 byte aligned. */
  1255. SLJIT_API_FUNC_ATTRIBUTE sljit_s32 sljit_emit_op_custom(struct sljit_compiler *compiler,
  1256. void *instruction, sljit_s32 size);
  1257. /* Define the currently available CPU status flags. It is usually used after an
  1258. sljit_emit_op_custom call to define which flags are set. */
  1259. SLJIT_API_FUNC_ATTRIBUTE void sljit_set_current_flags(struct sljit_compiler *compiler,
  1260. sljit_s32 current_flags);
  1261. #endif /* _SLJIT_LIR_H_ */