lightning.texi 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  1. \input texinfo.tex @c -*- texinfo -*-
  2. @c %**start of header (This is for running Texinfo on a region.)
  3. @setfilename lightning.info
  4. @set TITLE Using @sc{gnu} @i{lightning}
  5. @set TOPIC installing and using
  6. @settitle @value{TITLE}
  7. @c ---------------------------------------------------------------------
  8. @c Common macros
  9. @c ---------------------------------------------------------------------
  10. @macro bulletize{a}
  11. @item
  12. \a\
  13. @end macro
  14. @macro rem{a}
  15. @r{@i{\a\}}
  16. @end macro
  17. @macro gnu{}
  18. @sc{gnu}
  19. @end macro
  20. @macro lightning{}
  21. @gnu{} @i{lightning}
  22. @end macro
  23. @c ---------------------------------------------------------------------
  24. @c Macros for Texinfo 3.1/4.0 compatibility
  25. @c ---------------------------------------------------------------------
  26. @c @hlink (macro), @url and @email are used instead of @uref for Texinfo 3.1
  27. @c compatibility
  28. @macro hlink{url, link}
  29. \link\ (\url\)
  30. @end macro
  31. @c ifhtml can only be true in Texinfo 4.0, which has uref
  32. @ifhtml
  33. @unmacro hlink
  34. @macro hlink{url, link}
  35. @uref{\url\, \link\}
  36. @end macro
  37. @macro email{mail}
  38. @uref{mailto:\mail\, , \mail\}
  39. @end macro
  40. @macro url{url}
  41. @uref{\url\}
  42. @end macro
  43. @end ifhtml
  44. @c ---------------------------------------------------------------------
  45. @c References to the other half of the manual
  46. @c ---------------------------------------------------------------------
  47. @macro usingref{node, name}
  48. @ref{\node\, , \name\}
  49. @end macro
  50. @c ---------------------------------------------------------------------
  51. @c End of macro section
  52. @c ---------------------------------------------------------------------
  53. @set UPDATED 18 June 2018
  54. @set UPDATED-MONTH June 2018
  55. @set EDITION 2.1.2
  56. @set VERSION 2.1.2
  57. @ifnottex
  58. @dircategory Software development
  59. @direntry
  60. * lightning: (lightning). Library for dynamic code generation.
  61. @end direntry
  62. @end ifnottex
  63. @ifnottex
  64. @node Top
  65. @top @lightning{}
  66. @iftex
  67. @macro comma
  68. @verbatim{|,|}
  69. @end macro
  70. @end iftex
  71. @ifnottex
  72. @macro comma
  73. @verb{|,|}
  74. @end macro
  75. @end ifnottex
  76. This document describes @value{TOPIC} the @lightning{} library for
  77. dynamic code generation.
  78. @menu
  79. * Overview:: What GNU lightning is
  80. * Installation:: Configuring and installing GNU lightning
  81. * The instruction set:: The RISC instruction set used in GNU lightning
  82. * GNU lightning examples:: GNU lightning's examples
  83. * Reentrancy:: Re-entrant usage of GNU lightning
  84. * Customizations:: Advanced code generation customizations
  85. * Acknowledgements:: Acknowledgements for GNU lightning
  86. @end menu
  87. @end ifnottex
  88. @node Overview
  89. @chapter Introduction to @lightning{}
  90. @iftex
  91. This document describes @value{TOPIC} the @lightning{} library for
  92. dynamic code generation.
  93. @end iftex
  94. Dynamic code generation is the generation of machine code
  95. at runtime. It is typically used to strip a layer of interpretation
  96. by allowing compilation to occur at runtime. One of the most
  97. well-known applications of dynamic code generation is perhaps that
  98. of interpreters that compile source code to an intermediate bytecode
  99. form, which is then recompiled to machine code at run-time: this
  100. approach effectively combines the portability of bytecode
  101. representations with the speed of machine code. Another common
  102. application of dynamic code generation is in the field of hardware
  103. simulators and binary emulators, which can use the same techniques
  104. to translate simulated instructions to the instructions of the
  105. underlying machine.
  106. Yet other applications come to mind: for example, windowing
  107. @dfn{bitblt} operations, matrix manipulations, and network packet
  108. filters. Albeit very powerful and relatively well known within the
  109. compiler community, dynamic code generation techniques are rarely
  110. exploited to their full potential and, with the exception of the
  111. two applications described above, have remained curiosities because
  112. of their portability and functionality barriers: binary instructions
  113. are generated, so programs using dynamic code generation must be
  114. retargeted for each machine; in addition, coding a run-time code
  115. generator is a tedious and error-prone task more than a difficult one.
  116. @lightning{} provides a portable, fast and easily retargetable dynamic
  117. code generation system.
  118. To be portable, @lightning{} abstracts over current architectures'
  119. quirks and unorthogonalities. The interface that it exposes to is that
  120. of a standardized RISC architecture loosely based on the SPARC and MIPS
  121. chips. There are a few general-purpose registers (six, not including
  122. those used to receive and pass parameters between subroutines), and
  123. arithmetic operations involve three operands---either three registers
  124. or two registers and an arbitrarily sized immediate value.
  125. On one hand, this architecture is general enough that it is possible to
  126. generate pretty efficient code even on CISC architectures such as the
  127. Intel x86 or the Motorola 68k families. On the other hand, it matches
  128. real architectures closely enough that, most of the time, the
  129. compiler's constant folding pass ends up generating code which
  130. assembles machine instructions without further tests.
  131. @node Installation
  132. @chapter Configuring and installing @lightning{}
  133. The first thing to do to use @lightning{} is to configure the
  134. program, picking the set of macros to be used on the host
  135. architecture; this configuration is automatically performed by
  136. the @file{configure} shell script; to run it, merely type:
  137. @example
  138. ./configure
  139. @end example
  140. @lightning{} supports the @code{--enable-disassembler} option, that
  141. enables linking to GNU binutils and optionally print human readable
  142. disassembly of the jit code. This option can be disabled by the
  143. @code{--disable-disassembler} option.
  144. Another option that @file{configure} accepts is
  145. @code{--enable-assertions}, which enables several consistency checks in
  146. the run-time assemblers. These are not usually needed, so you can
  147. decide to simply forget about it; also remember that these consistency
  148. checks tend to slow down your code generator.
  149. After you've configured @lightning{}, run @file{make} as usual.
  150. @lightning{} has an extensive set of tests to validate it is working
  151. correctly in the build host. To test it run:
  152. @example
  153. make check
  154. @end example
  155. The next important step is:
  156. @example
  157. make install
  158. @end example
  159. This ends the process of installing @lightning{}.
  160. @node The instruction set
  161. @chapter @lightning{}'s instruction set
  162. @lightning{}'s instruction set was designed by deriving instructions
  163. that closely match those of most existing RISC architectures, or
  164. that can be easily syntesized if absent. Each instruction is composed
  165. of:
  166. @itemize @bullet
  167. @item
  168. an operation, like @code{sub} or @code{mul}
  169. @item
  170. most times, a register/immediate flag (@code{r} or @code{i})
  171. @item
  172. an unsigned modifier (@code{u}), a type identifier or two, when applicable.
  173. @end itemize
  174. Examples of legal mnemonics are @code{addr} (integer add, with three
  175. register operands) and @code{muli} (integer multiply, with two
  176. register operands and an immediate operand). Each instruction takes
  177. two or three operands; in most cases, one of them can be an immediate
  178. value instead of a register.
  179. Most @lightning{} integer operations are signed wordsize operations,
  180. with the exception of operations that convert types, or load or store
  181. values to/from memory. When applicable, the types and C types are as
  182. follow:
  183. @example
  184. _c @r{signed char}
  185. _uc @r{unsigned char}
  186. _s @r{short}
  187. _us @r{unsigned short}
  188. _i @r{int}
  189. _ui @r{unsigned int}
  190. _l @r{long}
  191. _f @r{float}
  192. _d @r{double}
  193. @end example
  194. Most integer operations do not need a type modifier, and when loading or
  195. storing values to memory there is an alias to the proper operation
  196. using wordsize operands, that is, if ommited, the type is @r{int} on
  197. 32-bit architectures and @r{long} on 64-bit architectures. Note
  198. that lightning also expects @code{sizeof(void*)} to match the wordsize.
  199. When an unsigned operation result differs from the equivalent signed
  200. operation, there is a the @code{_u} modifier.
  201. There are at least seven integer registers, of which six are
  202. general-purpose, while the last is used to contain the frame pointer
  203. (@code{FP}). The frame pointer can be used to allocate and access local
  204. variables on the stack, using the @code{allocai} or @code{allocar}
  205. instruction.
  206. Of the general-purpose registers, at least three are guaranteed to be
  207. preserved across function calls (@code{V0}, @code{V1} and
  208. @code{V2}) and at least three are not (@code{R0}, @code{R1} and
  209. @code{R2}). Six registers are not very much, but this
  210. restriction was forced by the need to target CISC architectures
  211. which, like the x86, are poor of registers; anyway, backends can
  212. specify the actual number of available registers with the calls
  213. @code{JIT_R_NUM} (for caller-save registers) and @code{JIT_V_NUM}
  214. (for callee-save registers).
  215. There are at least six floating-point registers, named @code{F0} to
  216. @code{F5}. These are usually caller-save and are separate from the integer
  217. registers on the supported architectures; on Intel architectures,
  218. in 32 bit mode if SSE2 is not available or use of X87 is forced,
  219. the register stack is mapped to a flat register file. As for the
  220. integer registers, the macro @code{JIT_F_NUM} yields the number of
  221. floating-point registers.
  222. The complete instruction set follows; as you can see, most non-memory
  223. operations only take integers (either signed or unsigned) as operands;
  224. this was done in order to reduce the instruction set, and because most
  225. architectures only provide word and long word operations on registers.
  226. There are instructions that allow operands to be extended to fit a larger
  227. data type, both in a signed and in an unsigned way.
  228. @table @b
  229. @item Binary ALU operations
  230. These accept three operands; the last one can be an immediate.
  231. @code{addx} operations must directly follow @code{addc}, and
  232. @code{subx} must follow @code{subc}; otherwise, results are undefined.
  233. Most, if not all, architectures do not support @r{float} or @r{double}
  234. immediate operands; lightning emulates those operations by moving the
  235. immediate to a temporary register and emiting the call with only
  236. register operands.
  237. @example
  238. addr _f _d O1 = O2 + O3
  239. addi _f _d O1 = O2 + O3
  240. addxr O1 = O2 + (O3 + carry)
  241. addxi O1 = O2 + (O3 + carry)
  242. addcr O1 = O2 + O3, set carry
  243. addci O1 = O2 + O3, set carry
  244. subr _f _d O1 = O2 - O3
  245. subi _f _d O1 = O2 - O3
  246. subxr O1 = O2 - (O3 + carry)
  247. subxi O1 = O2 - (O3 + carry)
  248. subcr O1 = O2 - O3, set carry
  249. subci O1 = O2 - O3, set carry
  250. rsbr _f _d O1 = O3 - O1
  251. rsbi _f _d O1 = O3 - O1
  252. mulr _f _d O1 = O2 * O3
  253. muli _f _d O1 = O2 * O3
  254. divr _u _f _d O1 = O2 / O3
  255. divi _u _f _d O1 = O2 / O3
  256. remr _u O1 = O2 % O3
  257. remi _u O1 = O2 % O3
  258. andr O1 = O2 & O3
  259. andi O1 = O2 & O3
  260. orr O1 = O2 | O3
  261. ori O1 = O2 | O3
  262. xorr O1 = O2 ^ O3
  263. xori O1 = O2 ^ O3
  264. lshr O1 = O2 << O3
  265. lshi O1 = O2 << O3
  266. rshr _u O1 = O2 >> O3@footnote{The sign bit is propagated unless using the @code{_u} modifier.}
  267. rshi _u O1 = O2 >> O3@footnote{The sign bit is propagated unless using the @code{_u} modifier.}
  268. @end example
  269. @item Four operand binary ALU operations
  270. These accept two result registers, and two operands; the last one can
  271. be an immediate. The first two arguments cannot be the same register.
  272. @code{qmul} stores the low word of the result in @code{O1} and the
  273. high word in @code{O2}. For unsigned multiplication, @code{O2} zero
  274. means there was no overflow. For signed multiplication, no overflow
  275. check is based on sign, and can be detected if @code{O2} is zero or
  276. minus one.
  277. @code{qdiv} stores the quotient in @code{O1} and the remainder in
  278. @code{O2}. It can be used as quick way to check if a division is
  279. exact, in which case the remainder is zero.
  280. @example
  281. qmulr _u O1 O2 = O3 * O4
  282. qmuli _u O1 O2 = O3 * O4
  283. qdivr _u O1 O2 = O3 / O4
  284. qdivi _u O1 O2 = O3 / O4
  285. @end example
  286. @item Unary ALU operations
  287. These accept two operands, both of which must be registers.
  288. @example
  289. negr _f _d O1 = -O2
  290. comr O1 = ~O2
  291. @end example
  292. These unary ALU operations are only defined for float operands.
  293. @example
  294. absr _f _d O1 = fabs(O2)
  295. sqrtr O1 = sqrt(O2)
  296. @end example
  297. Besides requiring the @code{r} modifier, there are no unary operations
  298. with an immediate operand.
  299. @item Compare instructions
  300. These accept three operands; again, the last can be an immediate.
  301. The last two operands are compared, and the first operand, that must be
  302. an integer register, is set to either 0 or 1, according to whether the
  303. given condition was met or not.
  304. The conditions given below are for the standard behavior of C,
  305. where the ``unordered'' comparison result is mapped to false.
  306. @example
  307. ltr _u _f _d O1 = (O2 < O3)
  308. lti _u _f _d O1 = (O2 < O3)
  309. ler _u _f _d O1 = (O2 <= O3)
  310. lei _u _f _d O1 = (O2 <= O3)
  311. gtr _u _f _d O1 = (O2 > O3)
  312. gti _u _f _d O1 = (O2 > O3)
  313. ger _u _f _d O1 = (O2 >= O3)
  314. gei _u _f _d O1 = (O2 >= O3)
  315. eqr _f _d O1 = (O2 == O3)
  316. eqi _f _d O1 = (O2 == O3)
  317. ner _f _d O1 = (O2 != O3)
  318. nei _f _d O1 = (O2 != O3)
  319. unltr _f _d O1 = !(O2 >= O3)
  320. unler _f _d O1 = !(O2 > O3)
  321. ungtr _f _d O1 = !(O2 <= O3)
  322. unger _f _d O1 = !(O2 < O3)
  323. uneqr _f _d O1 = !(O2 < O3) && !(O2 > O3)
  324. ltgtr _f _d O1 = !(O2 >= O3) || !(O2 <= O3)
  325. ordr _f _d O1 = (O2 == O2) && (O3 == O3)
  326. unordr _f _d O1 = (O2 != O2) || (O3 != O3)
  327. @end example
  328. @item Transfer operations
  329. These accept two operands; for @code{ext} both of them must be
  330. registers, while @code{mov} accepts an immediate value as the second
  331. operand.
  332. Unlike @code{movr} and @code{movi}, the other instructions are used
  333. to truncate a wordsize operand to a smaller integer data type or to
  334. convert float data types. You can also use @code{extr} to convert an
  335. integer to a floating point value: the usual options are @code{extr_f}
  336. and @code{extr_d}.
  337. @example
  338. movr _f _d O1 = O2
  339. movi _f _d O1 = O2
  340. extr _c _uc _s _us _i _ui _f _d O1 = O2
  341. truncr _f _d O1 = trunc(O2)
  342. @end example
  343. In 64-bit architectures it may be required to use @code{truncr_f_i},
  344. @code{truncr_f_l}, @code{truncr_d_i} and @code{truncr_d_l} to match
  345. the equivalent C code. Only the @code{_i} modifier is available in
  346. 32-bit architectures.
  347. @example
  348. truncr_f_i = <int> O1 = <float> O2
  349. truncr_f_l = <long>O1 = <float> O2
  350. truncr_d_i = <int> O1 = <double>O2
  351. truncr_d_l = <long>O1 = <double>O2
  352. @end example
  353. The float conversion operations are @emph{destination first,
  354. source second}, but the order of the types is reversed. This happens
  355. for historical reasons.
  356. @example
  357. extr_f_d = <double>O1 = <float> O2
  358. extr_d_f = <float> O1 = <double>O2
  359. @end example
  360. @item Network extensions
  361. These accept two operands, both of which must be registers; these
  362. two instructions actually perform the same task, yet they are
  363. assigned to two mnemonics for the sake of convenience and
  364. completeness. As usual, the first operand is the destination and
  365. the second is the source.
  366. The @code{_ul} variant is only available in 64-bit architectures.
  367. @example
  368. htonr _us _ui _ul @r{Host-to-network (big endian) order}
  369. ntohr _us _ui _ul @r{Network-to-host order }
  370. @end example
  371. @item Load operations
  372. @code{ld} accepts two operands while @code{ldx} accepts three;
  373. in both cases, the last can be either a register or an immediate
  374. value. Values are extended (with or without sign, according to
  375. the data type specification) to fit a whole register.
  376. The @code{_ui} and @code{_l} types are only available in 64-bit
  377. architectures. For convenience, there is a version without a
  378. type modifier for integer or pointer operands that uses the
  379. appropriate wordsize call.
  380. @example
  381. ldr _c _uc _s _us _i _ui _l _f _d O1 = *O2
  382. ldi _c _uc _s _us _i _ui _l _f _d O1 = *O2
  383. ldxr _c _uc _s _us _i _ui _l _f _d O1 = *(O2+O3)
  384. ldxi _c _uc _s _us _i _ui _l _f _d O1 = *(O2+O3)
  385. @end example
  386. @item Store operations
  387. @code{st} accepts two operands while @code{stx} accepts three; in
  388. both cases, the first can be either a register or an immediate
  389. value. Values are sign-extended to fit a whole register.
  390. @example
  391. str _c _uc _s _us _i _ui _l _f _d *O1 = O2
  392. sti _c _uc _s _us _i _ui _l _f _d *O1 = O2
  393. stxr _c _uc _s _us _i _ui _l _f _d *(O1+O2) = O3
  394. stxi _c _uc _s _us _i _ui _l _f _d *(O1+O2) = O3
  395. @end example
  396. As for the load operations, the @code{_ui} and @code{_l} types are
  397. only available in 64-bit architectures, and for convenience, there
  398. is a version without a type modifier for integer or pointer operands
  399. that uses the appropriate wordsize call.
  400. @item Argument management
  401. These are:
  402. @example
  403. prepare (not specified)
  404. va_start (not specified)
  405. pushargr _f _d
  406. pushargi _f _d
  407. va_push (not specified)
  408. arg _c _uc _s _us _i _ui _l _f _d
  409. getarg _c _uc _s _us _i _ui _l _f _d
  410. va_arg _d
  411. putargr _f _d
  412. putargi _f _d
  413. ret (not specified)
  414. retr _f _d
  415. reti _f _d
  416. va_end (not specified)
  417. retval _c _uc _s _us _i _ui _l _f _d
  418. epilog (not specified)
  419. @end example
  420. As with other operations that use a type modifier, the @code{_ui} and
  421. @code{_l} types are only available in 64-bit architectures, but there
  422. are operations without a type modifier that alias to the appropriate
  423. integer operation with wordsize operands.
  424. @code{prepare}, @code{pusharg}, and @code{retval} are used by the caller,
  425. while @code{arg}, @code{getarg} and @code{ret} are used by the callee.
  426. A code snippet that wants to call another procedure and has to pass
  427. arguments must, in order: use the @code{prepare} instruction and use
  428. the @code{pushargr} or @code{pushargi} to push the arguments @strong{in
  429. left to right order}; and use @code{finish} or @code{call} (explained below)
  430. to perform the actual call.
  431. @code{va_start} returns a @code{C} compatible @code{va_list}. To fetch
  432. arguments, use @code{va_arg} for integers and @code{va_arg_d} for doubles.
  433. @code{va_push} is required when passing a @code{va_list} to another function,
  434. because not all architectures expect it as a single pointer. Known case
  435. is DEC Alpha, that requires it as a structure passed by value.
  436. @code{arg}, @code{getarg} and @code{putarg} are used by the callee.
  437. @code{arg} is different from other instruction in that it does not
  438. actually generate any code: instead, it is a function which returns
  439. a value to be passed to @code{getarg} or @code{putarg}. @footnote{``Return
  440. a value'' means that @lightning{} code that compile these
  441. instructions return a value when expanded.} You should call
  442. @code{arg} as soon as possible, before any function call or, more
  443. easily, right after the @code{prolog} instructions
  444. (which is treated later).
  445. @code{getarg} accepts a register argument and a value returned by
  446. @code{arg}, and will move that argument to the register, extending
  447. it (with or without sign, according to the data type specification)
  448. to fit a whole register. These instructions are more intimately
  449. related to the usage of the @lightning{} instruction set in code
  450. that generates other code, so they will be treated more
  451. specifically in @ref{GNU lightning examples, , Generating code at
  452. run-time}.
  453. @code{putarg} is a mix of @code{getarg} and @code{pusharg} in that
  454. it accepts as first argument a register or immediate, and as
  455. second argument a value returned by @code{arg}. It allows changing,
  456. or restoring an argument to the current function, and is a
  457. construct required to implement tail call optimization. Note that
  458. arguments in registers are very cheap, but will be overwritten
  459. at any moment, including on some operations, for example division,
  460. that on several ports is implemented as a function call.
  461. Finally, the @code{retval} instruction fetches the return value of a
  462. called function in a register. The @code{retval} instruction takes a
  463. register argument and copies the return value of the previously called
  464. function in that register. A function with a return value should use
  465. @code{retr} or @code{reti} to put the return value in the return register
  466. before returning. @xref{Fibonacci, the Fibonacci numbers}, for an example.
  467. @code{epilog} is an optional call, that marks the end of a function
  468. body. It is automatically generated by @lightning{} if starting a new
  469. function (what should be done after a @code{ret} call) or finishing
  470. generating jit.
  471. It is very important to note that the fact that @code{epilog} being
  472. optional may cause a common mistake. Consider this:
  473. @example
  474. fun1:
  475. prolog
  476. ...
  477. ret
  478. fun2:
  479. prolog
  480. @end example
  481. Because @code{epilog} is added when finding a new @code{prolog},
  482. this will cause the @code{fun2} label to actually be before the
  483. return from @code{fun1}. Because @lightning{} will actually
  484. understand it as:
  485. @example
  486. fun1:
  487. prolog
  488. ...
  489. ret
  490. fun2:
  491. epilog
  492. prolog
  493. @end example
  494. You should observe a few rules when using these macros. First of
  495. all, if calling a varargs function, you should use the @code{ellipsis}
  496. call to mark the position of the ellipsis in the C prototype.
  497. You should not nest calls to @code{prepare} inside a
  498. @code{prepare/finish} block. Doing this will result in undefined
  499. behavior. Note that for functions with zero arguments you can use
  500. just @code{call}.
  501. @item Branch instructions
  502. Like @code{arg}, these also return a value which, in this case,
  503. is to be used to compile forward branches as explained in
  504. @ref{Fibonacci, , Fibonacci numbers}. They accept two operands to be
  505. compared; of these, the last can be either a register or an immediate.
  506. They are:
  507. @example
  508. bltr _u _f _d @r{if }(O2 < O3)@r{ goto }O1
  509. blti _u _f _d @r{if }(O2 < O3)@r{ goto }O1
  510. bler _u _f _d @r{if }(O2 <= O3)@r{ goto }O1
  511. blei _u _f _d @r{if }(O2 <= O3)@r{ goto }O1
  512. bgtr _u _f _d @r{if }(O2 > O3)@r{ goto }O1
  513. bgti _u _f _d @r{if }(O2 > O3)@r{ goto }O1
  514. bger _u _f _d @r{if }(O2 >= O3)@r{ goto }O1
  515. bgei _u _f _d @r{if }(O2 >= O3)@r{ goto }O1
  516. beqr _f _d @r{if }(O2 == O3)@r{ goto }O1
  517. beqi _f _d @r{if }(O2 == O3)@r{ goto }O1
  518. bner _f _d @r{if }(O2 != O3)@r{ goto }O1
  519. bnei _f _d @r{if }(O2 != O3)@r{ goto }O1
  520. bunltr _f _d @r{if }!(O2 >= O3)@r{ goto }O1
  521. bunler _f _d @r{if }!(O2 > O3)@r{ goto }O1
  522. bungtr _f _d @r{if }!(O2 <= O3)@r{ goto }O1
  523. bunger _f _d @r{if }!(O2 < O3)@r{ goto }O1
  524. buneqr _f _d @r{if }!(O2 < O3) && !(O2 > O3)@r{ goto }O1
  525. bltgtr _f _d @r{if }!(O2 >= O3) || !(O2 <= O3)@r{ goto }O1
  526. bordr _f _d @r{if } (O2 == O2) && (O3 == O3)@r{ goto }O1
  527. bunordr _f _d @r{if }!(O2 != O2) || (O3 != O3)@r{ goto }O1
  528. bmsr @r{if }O2 & O3@r{ goto }O1
  529. bmsi @r{if }O2 & O3@r{ goto }O1
  530. bmcr @r{if }!(O2 & O3)@r{ goto }O1
  531. bmci @r{if }!(O2 & O3)@r{ goto }O1@footnote{These mnemonics mean, respectively, @dfn{branch if mask set} and @dfn{branch if mask cleared}.}
  532. boaddr _u O2 += O3@r{, goto }O1@r{ if overflow}
  533. boaddi _u O2 += O3@r{, goto }O1@r{ if overflow}
  534. bxaddr _u O2 += O3@r{, goto }O1@r{ if no overflow}
  535. bxaddi _u O2 += O3@r{, goto }O1@r{ if no overflow}
  536. bosubr _u O2 -= O3@r{, goto }O1@r{ if overflow}
  537. bosubi _u O2 -= O3@r{, goto }O1@r{ if overflow}
  538. bxsubr _u O2 -= O3@r{, goto }O1@r{ if no overflow}
  539. bxsubi _u O2 -= O3@r{, goto }O1@r{ if no overflow}
  540. @end example
  541. @item Jump and return operations
  542. These accept one argument except @code{ret} and @code{jmpi} which
  543. have none; the difference between @code{finishi} and @code{calli}
  544. is that the latter does not clean the stack from pushed parameters
  545. (if any) and the former must @strong{always} follow a @code{prepare}
  546. instruction.
  547. @example
  548. callr (not specified) @r{function call to register O1}
  549. calli (not specified) @r{function call to immediate O1}
  550. finishr (not specified) @r{function call to register O1}
  551. finishi (not specified) @r{function call to immediate O1}
  552. jmpr (not specified) @r{unconditional jump to register}
  553. jmpi (not specified) @r{unconditional jump}
  554. ret (not specified) @r{return from subroutine}
  555. retr _c _uc _s _us _i _ui _l _f _d
  556. reti _c _uc _s _us _i _ui _l _f _d
  557. retval _c _uc _s _us _i _ui _l _f _d @r{move return value}
  558. @r{to register}
  559. @end example
  560. Like branch instruction, @code{jmpi} also returns a value which is to
  561. be used to compile forward branches. @xref{Fibonacci, , Fibonacci
  562. numbers}.
  563. @item Labels
  564. There are 3 @lightning{} instructions to create labels:
  565. @example
  566. label (not specified) @r{simple label}
  567. forward (not specified) @r{forward label}
  568. indirect (not specified) @r{special simple label}
  569. @end example
  570. @code{label} is normally used as @code{patch_at} argument for backward
  571. jumps.
  572. @example
  573. jit_node_t *jump, *label;
  574. label = jit_label();
  575. ...
  576. jump = jit_beqr(JIT_R0, JIT_R1);
  577. jit_patch_at(jump, label);
  578. @end example
  579. @code{forward} is used to patch code generation before the actual
  580. position of the label is known.
  581. @example
  582. jit_node_t *jump, *label;
  583. label = jit_forward();
  584. jump = jit_beqr(JIT_R0, JIT_R1);
  585. jit_patch_at(jump, label);
  586. ...
  587. jit_link(label);
  588. @end example
  589. @code{indirect} is useful when creating jump tables, and tells
  590. @lightning{} to not optimize out a label that is not the target of
  591. any jump, because an indirect jump may land where it is defined.
  592. @example
  593. jit_node_t *jump, *label;
  594. ...
  595. jmpr(JIT_R0); @rem{/* may jump to label */}
  596. ...
  597. label = jit_indirect();
  598. @end example
  599. @code{indirect} is an special case of @code{note} and @code{name}
  600. because it is a valid argument to @code{address}.
  601. Note that the usual idiom to write the previous example is
  602. @example
  603. jit_node_t *addr, *jump;
  604. addr = jit_movi(JIT_R0, 0); @rem{/* immediate is ignored */}
  605. ...
  606. jmpr(JIT_R0);
  607. ...
  608. jit_patch(addr); @rem{/* implicit label added */}
  609. @end example
  610. that automatically binds the implicit label added by @code{patch} with
  611. the @code{movi}, but on some special conditions it is required to create
  612. an "unbound" label.
  613. @item Function prolog
  614. These macros are used to set up a function prolog. The @code{allocai}
  615. call accept a single integer argument and returns an offset value
  616. for stack storage access. The @code{allocar} accepts two registers
  617. arguments, the first is set to the offset for stack access, and the
  618. second is the size in bytes argument.
  619. @example
  620. prolog (not specified) @r{function prolog}
  621. allocai (not specified) @r{reserve space on the stack}
  622. allocar (not specified) @r{allocate space on the stack}
  623. @end example
  624. @code{allocai} receives the number of bytes to allocate and returns
  625. the offset from the frame pointer register @code{FP} to the base of
  626. the area.
  627. @code{allocar} receives two register arguments. The first is where
  628. to store the offset from the frame pointer register @code{FP} to the
  629. base of the area. The second argument is the size in bytes. Note
  630. that @code{allocar} is dynamic allocation, and special attention
  631. should be taken when using it. If called in a loop, every iteration
  632. will allocate stack space. Stack space is aligned from 8 to 64 bytes
  633. depending on backend requirements, even if allocating only one byte.
  634. It is advisable to not use it with @code{frame} and @code{tramp}; it
  635. should work with @code{frame} with special care to call only once,
  636. but is not supported if used in @code{tramp}, even if called only
  637. once.
  638. As a small appetizer, here is a small function that adds 1 to the input
  639. parameter (an @code{int}). I'm using an assembly-like syntax here which
  640. is a bit different from the one used when writing real subroutines with
  641. @lightning{}; the real syntax will be introduced in @xref{GNU lightning
  642. examples, , Generating code at run-time}.
  643. @example
  644. incr:
  645. prolog
  646. in = arg @rem{! We have an integer argument}
  647. getarg R0, in @rem{! Move it to R0}
  648. addi R0, R0, 1 @rem{! Add 1}
  649. retr R0 @rem{! And return the result}
  650. @end example
  651. And here is another function which uses the @code{printf} function from
  652. the standard C library to write a number in hexadecimal notation:
  653. @example
  654. printhex:
  655. prolog
  656. in = arg @rem{! Same as above}
  657. getarg R0, in
  658. prepare @rem{! Begin call sequence for printf}
  659. pushargi "%x" @rem{! Push format string}
  660. ellipsis @rem{! Varargs start here}
  661. pushargr R0 @rem{! Push second argument}
  662. finishi printf @rem{! Call printf}
  663. ret @rem{! Return to caller}
  664. @end example
  665. @item Trampolines, continuations and tail call optimization
  666. Frequently it is required to generate jit code that must jump to
  667. code generated later, possibly from another @code{jit_context_t}.
  668. These require compatible stack frames.
  669. @lightning{} provides two primitives from where trampolines,
  670. continuations and tail call optimization can be implemented.
  671. @example
  672. frame (not specified) @r{create stack frame}
  673. tramp (not specified) @r{assume stack frame}
  674. @end example
  675. @code{frame} receives an integer argument@footnote{It is not
  676. automatically computed because it does not know about the
  677. requirement of later generated code.} that defines the size in
  678. bytes for the stack frame of the current, @code{C} callable,
  679. jit function. To calculate this value, a good formula is maximum
  680. number of arguments to any called native function times
  681. eight@footnote{Times eight so that it works for double arguments.
  682. And would not need conditionals for ports that pass arguments in
  683. the stack.}, plus the sum of the arguments to any call to
  684. @code{jit_allocai}. @lightning{} automatically adjusts this value
  685. for any backend specific stack memory it may need, or any
  686. alignment constraint.
  687. @code{frame} also instructs @lightning{} to save all callee
  688. save registers in the prolog and reload in the epilog.
  689. @example
  690. main: @rem{! jit entry point}
  691. prolog @rem{! function prolog}
  692. frame 256 @rem{! save all callee save registers and}
  693. @rem{! reserve at least 256 bytes in stack}
  694. main_loop:
  695. ...
  696. jmpi handler @rem{! jumps to external code}
  697. ...
  698. ret @rem{! return to the caller}
  699. @end example
  700. @code{tramp} differs from @code{frame} only that a prolog and epilog
  701. will not be generated. Note that @code{prolog} must still be used.
  702. The code under @code{tramp} must be ready to be entered with a jump
  703. at the prolog position, and instead of a return, it must end with
  704. a non conditional jump. @code{tramp} exists solely for the fact
  705. that it allows optimizing out prolog and epilog code that would
  706. never be executed.
  707. @example
  708. handler: @rem{! handler entry point}
  709. prolog @rem{! function prolog}
  710. tramp 256 @rem{! assumes all callee save registers}
  711. @rem{! are saved and there is at least}
  712. @rem{! 256 bytes in stack}
  713. ...
  714. jmpi main_loop @rem{! return to the main loop}
  715. @end example
  716. @lightning{} only supports Tail Call Optimization using the
  717. @code{tramp} construct. Any other way is not guaranteed to
  718. work on all ports.
  719. An example of a simple (recursive) tail call optimization:
  720. @example
  721. factorial: @rem{! Entry point of the factorial function}
  722. prolog
  723. in = arg @rem{! Receive an integer argument}
  724. getarg R0, in @rem{! Move argument to RO}
  725. prepare
  726. pushargi 1 @rem{! This is the accumulator}
  727. pushargr R0 @rem{! This is the argument}
  728. finishi fact @rem{! Call the tail call optimized function}
  729. retval R0 @rem{! Fetch the result}
  730. retr R0 @rem{! Return it}
  731. epilog @rem{! Epilog *before* label before prolog}
  732. fact: @rem{! Entry point of the helper function}
  733. prolog
  734. frame 16 @rem{! Reserve 16 bytes in the stack}
  735. fact_entry: @rem{! This is the tail call entry point}
  736. ac = arg @rem{! The accumulator is the first argument}
  737. in = arg @rem{! The factorial argument}
  738. getarg R0, ac @rem{! Move the accumulator to R0}
  739. getarg R1, in @rem{! Move the argument to R1}
  740. blei fact_out, R1, 1 @rem{! Done if argument is one or less}
  741. mulr R0, R0, R1 @rem{! accumulator *= argument}
  742. putargr R0, ac @rem{! Update the accumulator}
  743. subi R1, R1, 1 @rem{! argument -= 1}
  744. putargr R1, in @rem{! Update the argument}
  745. jmpi fact_entry @rem{! Tail Call Optimize it!}
  746. fact_out:
  747. retr R0 @rem{! Return the accumulator}
  748. @end example
  749. @item Predicates
  750. @example
  751. forward_p (not specified) @r{forward label predicate}
  752. indirect_p (not specified) @r{indirect label predicate}
  753. target_p (not specified) @r{used label predicate}
  754. arg_register_p (not specified) @r{argument kind predicate}
  755. callee_save_p (not specified) @r{callee save predicate}
  756. pointer_p (not specified) @r{pointer predicate}
  757. @end example
  758. @code{forward_p} expects a @code{jit_node_t*} argument, and
  759. returns non zero if it is a forward label reference, that is,
  760. a label returned by @code{forward}, that still needs a
  761. @code{link} call.
  762. @code{indirect_p} expects a @code{jit_node_t*} argument, and returns
  763. non zero if it is an indirect label reference, that is, a label that
  764. was returned by @code{indirect}.
  765. @code{target_p} expects a @code{jit_node_t*} argument, that is any
  766. kind of label, and will return non zero if there is at least one
  767. jump or move referencing it.
  768. @code{arg_register_p} expects a @code{jit_node_t*} argument, that must
  769. have been returned by @code{arg}, @code{arg_f} or @code{arg_d}, and
  770. will return non zero if the argument lives in a register. This call
  771. is useful to know the live range of register arguments, as those
  772. are very fast to read and write, but have volatile values.
  773. @code{callee_save_p} exects a valid @code{JIT_Rn}, @code{JIT_Vn}, or
  774. @code{JIT_Fn}, and will return non zero if the register is callee
  775. save. This call is useful because on several ports, the @code{JIT_Rn}
  776. and @code{JIT_Fn} registers are actually callee save; no need
  777. to save and load the values when making function calls.
  778. @code{pointer_p} expects a pointer argument, and will return non
  779. zero if the pointer is inside the generated jit code. Must be
  780. called after @code{jit_emit} and before @code{jit_destroy_state}.
  781. @end table
  782. @node GNU lightning examples
  783. @chapter Generating code at run-time
  784. To use @lightning{}, you should include the @file{lightning.h} file that
  785. is put in your include directory by the @samp{make install} command.
  786. Each of the instructions above translates to a macro or function call.
  787. All you have to do is prepend @code{jit_} (lowercase) to opcode names
  788. and @code{JIT_} (uppercase) to register names. Of course, parameters
  789. are to be put between parentheses.
  790. This small tutorial presents three examples:
  791. @iftex
  792. @itemize @bullet
  793. @item
  794. The @code{incr} function found in @ref{The instruction set, ,
  795. @lightning{}'s instruction set}:
  796. @item
  797. A simple function call to @code{printf}
  798. @item
  799. An RPN calculator.
  800. @item
  801. Fibonacci numbers
  802. @end itemize
  803. @end iftex
  804. @ifnottex
  805. @menu
  806. * incr:: A function which increments a number by one
  807. * printf:: A simple function call to printf
  808. * RPN calculator:: A more complex example, an RPN calculator
  809. * Fibonacci:: Calculating Fibonacci numbers
  810. @end menu
  811. @end ifnottex
  812. @node incr
  813. @section A function which increments a number by one
  814. Let's see how to create and use the sample @code{incr} function created
  815. in @ref{The instruction set, , @lightning{}'s instruction set}:
  816. @example
  817. #include <stdio.h>
  818. #include <lightning.h>
  819. static jit_state_t *_jit;
  820. typedef int (*pifi)(int); @rem{/* Pointer to Int Function of Int */}
  821. int main(int argc, char *argv[])
  822. @{
  823. jit_node_t *in;
  824. pifi incr;
  825. init_jit(argv[0]);
  826. _jit = jit_new_state();
  827. jit_prolog(); @rem{/* @t{ prolog } */}
  828. in = jit_arg(); @rem{/* @t{ in = arg } */}
  829. jit_getarg(JIT_R0, in); @rem{/* @t{ getarg R0 } */}
  830. jit_addi(JIT_R0, JIT_R0, 1); @rem{/* @t{ addi R0@comma{} R0@comma{} 1 } */}
  831. jit_retr(JIT_R0); @rem{/* @t{ retr R0 } */}
  832. incr = jit_emit();
  833. jit_clear_state();
  834. @rem{/* call the generated code@comma{} passing 5 as an argument */}
  835. printf("%d + 1 = %d\n", 5, incr(5));
  836. jit_destroy_state();
  837. finish_jit();
  838. return 0;
  839. @}
  840. @end example
  841. Let's examine the code line by line (well, almost@dots{}):
  842. @table @t
  843. @item #include <lightning.h>
  844. You already know about this. It defines all of @lightning{}'s macros.
  845. @item static jit_state_t *_jit;
  846. You might wonder about what is @code{jit_state_t}. It is a structure
  847. that stores jit code generation information. The name @code{_jit} is
  848. special, because since multiple jit generators can run at the same
  849. time, you must either @r{#define _jit my_jit_state} or name it
  850. @code{_jit}.
  851. @item typedef int (*pifi)(int);
  852. Just a handy typedef for a pointer to a function that takes an
  853. @code{int} and returns another.
  854. @item jit_node_t *in;
  855. Declares a variable to hold an identifier for a function argument. It
  856. is an opaque pointer, that will hold the return of a call to @code{arg}
  857. and be used as argument to @code{getarg}.
  858. @item pifi incr;
  859. Declares a function pointer variable to a function that receives an
  860. @code{int} and returns an @code{int}.
  861. @item init_jit(argv[0]);
  862. You must call this function before creating a @code{jit_state_t}
  863. object. This function does global state initialization, and may need
  864. to detect CPU or Operating System features. It receives a string
  865. argument that is later used to read symbols from a shared object using
  866. GNU binutils if disassembly was enabled at configure time. If no
  867. disassembly will be performed a NULL pointer can be used as argument.
  868. @item _jit = jit_new_state();
  869. This call initializes a @lightning{} jit state.
  870. @item jit_prolog();
  871. Ok, so we start generating code for our beloved function@dots{}
  872. @item in = jit_arg();
  873. @itemx jit_getarg(JIT_R0, in);
  874. We retrieve the first (and only) argument, an integer, and store it
  875. into the general-purpose register @code{R0}.
  876. @item jit_addi(JIT_R0, JIT_R0, 1);
  877. We add one to the content of the register.
  878. @item jit_retr(JIT_R0);
  879. This instruction generates a standard function epilog that returns
  880. the contents of the @code{R0} register.
  881. @item incr = jit_emit();
  882. This instruction is very important. It actually translates the
  883. @lightning{} macros used before to machine code, flushes the generated
  884. code area out of the processor's instruction cache and return a
  885. pointer to the start of the code.
  886. @item jit_clear_state();
  887. This call cleanups any data not required for jit execution. Note
  888. that it must be called after any call to @code{jit_print} or
  889. @code{jit_address}, as this call destroy the @lightning{}
  890. intermediate representation.
  891. @item printf("%d + 1 = %d", 5, incr(5));
  892. Calling our function is this simple---it is not distinguishable from
  893. a normal C function call, the only difference being that @code{incr}
  894. is a variable.
  895. @item jit_destroy_state();
  896. Releases all memory associated with the jit context. It should be
  897. called after known the jit will no longer be called.
  898. @item finish_jit();
  899. This call cleanups any global state hold by @lightning{}, and is
  900. advisable to call it once jit code will no longer be generated.
  901. @end table
  902. @lightning{} abstracts two phases of dynamic code generation: selecting
  903. instructions that map the standard representation, and emitting binary
  904. code for these instructions. The client program has the responsibility
  905. of describing the code to be generated using the standard @lightning{}
  906. instruction set.
  907. Let's examine the code generated for @code{incr} on the SPARC and x86_64
  908. architecture (on the right is the code that an assembly-language
  909. programmer would write):
  910. @table @b
  911. @item SPARC
  912. @example
  913. save %sp, -112, %sp
  914. mov %i0, %g2 retl
  915. inc %g2 inc %o0
  916. mov %g2, %i0
  917. restore
  918. retl
  919. nop
  920. @end example
  921. In this case, @lightning{} introduces overhead to create a register
  922. window (not knowing that the procedure is a leaf procedure) and to
  923. move the argument to the general purpose register @code{R0} (which
  924. maps to @code{%g2} on the SPARC).
  925. @end table
  926. @table @b
  927. @item x86_64
  928. @example
  929. sub $0x30,%rsp
  930. mov %rbp,(%rsp)
  931. mov %rsp,%rbp
  932. sub $0x18,%rsp
  933. mov %rdi,%rax mov %rdi, %rax
  934. add $0x1,%rax inc %rax
  935. mov %rbp,%rsp
  936. mov (%rsp),%rbp
  937. add $0x30,%rsp
  938. retq retq
  939. @end example
  940. In this case, the main overhead is due to the function's prolog and
  941. epilog, and stack alignment after reserving stack space for word
  942. to/from float conversions or moving data from/to x87 to/from SSE.
  943. Note that besides allocating space to save callee saved registers,
  944. no registers are saved/restored because @lightning{} notices those
  945. registers are not modified. There is currently no logic to detect
  946. if it needs to allocate stack space for type conversions neither
  947. proper leaf function detection, but these are subject to change
  948. (FIXME).
  949. @end table
  950. @node printf
  951. @section A simple function call to @code{printf}
  952. Again, here is the code for the example:
  953. @example
  954. #include <stdio.h>
  955. #include <lightning.h>
  956. static jit_state_t *_jit;
  957. typedef void (*pvfi)(int); @rem{/* Pointer to Void Function of Int */}
  958. int main(int argc, char *argv[])
  959. @{
  960. pvfi myFunction; @rem{/* ptr to generated code */}
  961. jit_node_t *start, *end; @rem{/* a couple of labels */}
  962. jit_node_t *in; @rem{/* to get the argument */}
  963. init_jit(argv[0]);
  964. _jit = jit_new_state();
  965. start = jit_note(__FILE__, __LINE__);
  966. jit_prolog();
  967. in = jit_arg();
  968. jit_getarg(JIT_R1, in);
  969. jit_pushargi((jit_word_t)"generated %d bytes\n");
  970. jit_ellipsis();
  971. jit_pushargr(JIT_R1);
  972. jit_finishi(printf);
  973. jit_ret();
  974. jit_epilog();
  975. end = jit_note(__FILE__, __LINE__);
  976. myFunction = jit_emit();
  977. @rem{/* call the generated code@comma{} passing its size as argument */}
  978. myFunction((char*)jit_address(end) - (char*)jit_address(start));
  979. jit_clear_state();
  980. jit_disassemble();
  981. jit_destroy_state();
  982. finish_jit();
  983. return 0;
  984. @}
  985. @end example
  986. The function shows how many bytes were generated. Most of the code
  987. is not very interesting, as it resembles very closely the program
  988. presented in @ref{incr, , A function which increments a number by one}.
  989. For this reason, we're going to concentrate on just a few statements.
  990. @table @t
  991. @item start = jit_note(__FILE__, __LINE__);
  992. @itemx @r{@dots{}}
  993. @itemx end = jit_note(__FILE__, __LINE__);
  994. These two instruction call the @code{jit_note} macro, which creates
  995. a note in the jit code; arguments to @code{jit_note} usually are a
  996. filename string and line number integer, but using NULL for the
  997. string argument is perfectly valid if only need to create a simple
  998. marker in the code.
  999. @item jit_ellipsis();
  1000. @code{ellipsis} usually is only required if calling varargs functions
  1001. with double arguments, but it is a good practice to properly describe
  1002. the @r{@dots{}} in the call sequence.
  1003. @item jit_pushargi((jit_word_t)"generated %d bytes\n");
  1004. Note the use of the @code{(jit_word_t)} cast, that is used only
  1005. to avoid a compiler warning, due to using a pointer where a
  1006. wordsize integer type was expected.
  1007. @item jit_prepare();
  1008. @itemx @r{@dots{}}
  1009. @itemx jit_finishi(printf);
  1010. Once the arguments to @code{printf} have been pushed, what means
  1011. moving them to stack or register arguments, the @code{printf}
  1012. function is called and the stack cleaned. Note how @lightning{}
  1013. abstracts the differences between different architectures and
  1014. ABI's -- the client program does not know how parameter passing
  1015. works on the host architecture.
  1016. @item jit_epilog();
  1017. Usually it is not required to call @code{epilog}, but because it
  1018. is implicitly called when noticing the end of a function, if the
  1019. @code{end} variable was set with a @code{note} call after the
  1020. @code{ret}, it would not consider the function epilog.
  1021. @item myFunction((char*)jit_address(end) - (char*)jit_address(start));
  1022. This calls the generate jit function passing as argument the offset
  1023. difference from the @code{start} and @code{end} notes. The @code{address}
  1024. call must be done after the @code{emit} call or either a fatal error
  1025. will happen (if @lightning{} is built with assertions enable) or an
  1026. undefined value will be returned.
  1027. @item jit_clear_state();
  1028. Note that @code{jit_clear_state} was called after executing jit in
  1029. this example. It was done because it must be called after any call
  1030. to @code{jit_address} or @code{jit_print}.
  1031. @item jit_disassemble();
  1032. @code{disassemble} will dump the generated code to standard output,
  1033. unless @lightning{} was built with the disassembler disabled, in which
  1034. case no output will be shown.
  1035. @end table
  1036. @node RPN calculator
  1037. @section A more complex example, an RPN calculator
  1038. We create a small stack-based RPN calculator which applies a series
  1039. of operators to a given parameter and to other numeric operands.
  1040. Unlike previous examples, the code generator is fully parameterized
  1041. and is able to compile different formulas to different functions.
  1042. Here is the code for the expression compiler; a sample usage will
  1043. follow.
  1044. Since @lightning{} does not provide push/pop instruction, this
  1045. example uses a stack-allocated area to store the data. Such an
  1046. area can be allocated using the macro @code{allocai}, which
  1047. receives the number of bytes to allocate and returns the offset
  1048. from the frame pointer register @code{FP} to the base of the
  1049. area.
  1050. Usually, you will use the @code{ldxi} and @code{stxi} instruction
  1051. to access stack-allocated variables. However, it is possible to
  1052. use operations such as @code{add} to compute the address of the
  1053. variables, and pass the address around.
  1054. @example
  1055. #include <stdio.h>
  1056. #include <lightning.h>
  1057. typedef int (*pifi)(int); @rem{/* Pointer to Int Function of Int */}
  1058. static jit_state_t *_jit;
  1059. void stack_push(int reg, int *sp)
  1060. @{
  1061. jit_stxi_i (*sp, JIT_FP, reg);
  1062. *sp += sizeof (int);
  1063. @}
  1064. void stack_pop(int reg, int *sp)
  1065. @{
  1066. *sp -= sizeof (int);
  1067. jit_ldxi_i (reg, JIT_FP, *sp);
  1068. @}
  1069. jit_node_t *compile_rpn(char *expr)
  1070. @{
  1071. jit_node_t *in, *fn;
  1072. int stack_base, stack_ptr;
  1073. fn = jit_note(NULL, 0);
  1074. jit_prolog();
  1075. in = jit_arg();
  1076. stack_ptr = stack_base = jit_allocai (32 * sizeof (int));
  1077. jit_getarg_i(JIT_R2, in);
  1078. while (*expr) @{
  1079. char buf[32];
  1080. int n;
  1081. if (sscanf(expr, "%[0-9]%n", buf, &n)) @{
  1082. expr += n - 1;
  1083. stack_push(JIT_R0, &stack_ptr);
  1084. jit_movi(JIT_R0, atoi(buf));
  1085. @} else if (*expr == 'x') @{
  1086. stack_push(JIT_R0, &stack_ptr);
  1087. jit_movr(JIT_R0, JIT_R2);
  1088. @} else if (*expr == '+') @{
  1089. stack_pop(JIT_R1, &stack_ptr);
  1090. jit_addr(JIT_R0, JIT_R1, JIT_R0);
  1091. @} else if (*expr == '-') @{
  1092. stack_pop(JIT_R1, &stack_ptr);
  1093. jit_subr(JIT_R0, JIT_R1, JIT_R0);
  1094. @} else if (*expr == '*') @{
  1095. stack_pop(JIT_R1, &stack_ptr);
  1096. jit_mulr(JIT_R0, JIT_R1, JIT_R0);
  1097. @} else if (*expr == '/') @{
  1098. stack_pop(JIT_R1, &stack_ptr);
  1099. jit_divr(JIT_R0, JIT_R1, JIT_R0);
  1100. @} else @{
  1101. fprintf(stderr, "cannot compile: %s\n", expr);
  1102. abort();
  1103. @}
  1104. ++expr;
  1105. @}
  1106. jit_retr(JIT_R0);
  1107. jit_epilog();
  1108. return fn;
  1109. @}
  1110. @end example
  1111. The principle on which the calculator is based is easy: the stack top
  1112. is held in R0, while the remaining items of the stack are held in the
  1113. memory area that we allocate with @code{allocai}. Compiling a numeric
  1114. operand or the argument @code{x} pushes the old stack top onto the
  1115. stack and moves the operand into R0; compiling an operator pops the
  1116. second operand off the stack into R1, and compiles the operation so
  1117. that the result goes into R0, thus becoming the new stack top.
  1118. This example allocates a fixed area for 32 @code{int}s. This is not
  1119. a problem when the function is a leaf like in this case; in a full-blown
  1120. compiler you will want to analyze the input and determine the number
  1121. of needed stack slots---a very simple example of register allocation.
  1122. The area is then managed like a stack using @code{stack_push} and
  1123. @code{stack_pop}.
  1124. Source code for the client (which lies in the same source file) follows:
  1125. @example
  1126. int main(int argc, char *argv[])
  1127. @{
  1128. jit_node_t *nc, *nf;
  1129. pifi c2f, f2c;
  1130. int i;
  1131. init_jit(argv[0]);
  1132. _jit = jit_new_state();
  1133. nc = compile_rpn("32x9*5/+");
  1134. nf = compile_rpn("x32-5*9/");
  1135. (void)jit_emit();
  1136. c2f = (pifi)jit_address(nc);
  1137. f2c = (pifi)jit_address(nf);
  1138. jit_clear_state();
  1139. printf("\nC:");
  1140. for (i = 0; i <= 100; i += 10) printf("%3d ", i);
  1141. printf("\nF:");
  1142. for (i = 0; i <= 100; i += 10) printf("%3d ", c2f(i));
  1143. printf("\n");
  1144. printf("\nF:");
  1145. for (i = 32; i <= 212; i += 18) printf("%3d ", i);
  1146. printf("\nC:");
  1147. for (i = 32; i <= 212; i += 18) printf("%3d ", f2c(i));
  1148. printf("\n");
  1149. jit_destroy_state();
  1150. finish_jit();
  1151. return 0;
  1152. @}
  1153. @end example
  1154. The client displays a conversion table between Celsius and Fahrenheit
  1155. degrees (both Celsius-to-Fahrenheit and Fahrenheit-to-Celsius). The
  1156. formulas are, @math{F(c) = c*9/5+32} and @math{C(f) = (f-32)*5/9},
  1157. respectively.
  1158. Providing the formula as an argument to @code{compile_rpn} effectively
  1159. parameterizes code generation, making it possible to use the same code
  1160. to compile different functions; this is what makes dynamic code
  1161. generation so powerful.
  1162. @node Fibonacci
  1163. @section Fibonacci numbers
  1164. The code in this section calculates the Fibonacci sequence. That is
  1165. modeled by the recurrence relation:
  1166. @display
  1167. f(0) = 0
  1168. f(1) = f(2) = 1
  1169. f(n) = f(n-1) + f(n-2)
  1170. @end display
  1171. The purpose of this example is to introduce branches. There are two
  1172. kind of branches: backward branches and forward branches. We'll
  1173. present the calculation in a recursive and iterative form; the
  1174. former only uses forward branches, while the latter uses both.
  1175. @example
  1176. #include <stdio.h>
  1177. #include <lightning.h>
  1178. static jit_state_t *_jit;
  1179. typedef int (*pifi)(int); @rem{/* Pointer to Int Function of Int */}
  1180. int main(int argc, char *argv[])
  1181. @{
  1182. pifi fib;
  1183. jit_node_t *label;
  1184. jit_node_t *call;
  1185. jit_node_t *in; @rem{/* offset of the argument */}
  1186. jit_node_t *ref; @rem{/* to patch the forward reference */}
  1187. jit_node_t *zero; @rem{/* to patch the forward reference */}
  1188. init_jit(argv[0]);
  1189. _jit = jit_new_state();
  1190. label = jit_label();
  1191. jit_prolog ();
  1192. in = jit_arg ();
  1193. jit_getarg (JIT_V0, in); @rem{/* R0 = n */}
  1194. zero = jit_beqi (JIT_R0, 0);
  1195. jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */
  1196. jit_movi (JIT_R0, 1);
  1197. ref = jit_blei (JIT_V0, 2);
  1198. jit_subi (JIT_V1, JIT_V0, 1); @rem{/* V1 = n-1 */}
  1199. jit_subi (JIT_V2, JIT_V0, 2); @rem{/* V2 = n-2 */}
  1200. jit_prepare();
  1201. jit_pushargr(JIT_V1);
  1202. call = jit_finishi(NULL);
  1203. jit_patch_at(call, label);
  1204. jit_retval(JIT_V1); @rem{/* V1 = fib(n-1) */}
  1205. jit_prepare();
  1206. jit_pushargr(JIT_V2);
  1207. call = jit_finishi(NULL);
  1208. jit_patch_at(call, label);
  1209. jit_retval(JIT_R0); @rem{/* R0 = fib(n-2) */}
  1210. jit_addr(JIT_R0, JIT_R0, JIT_V1); @rem{/* R0 = R0 + V1 */}
  1211. jit_patch(ref); @rem{/* patch jump */}
  1212. jit_patch(zero); @rem{/* patch jump */}
  1213. jit_retr(JIT_R0);
  1214. @rem{/* call the generated code@comma{} passing 32 as an argument */}
  1215. fib = jit_emit();
  1216. jit_clear_state();
  1217. printf("fib(%d) = %d\n", 32, fib(32));
  1218. jit_destroy_state();
  1219. finish_jit();
  1220. return 0;
  1221. @}
  1222. @end example
  1223. As said above, this is the first example of dynamically compiling
  1224. branches. Branch instructions have two operands containing the
  1225. values to be compared, and return a @code{jit_note_t *} object
  1226. to be patched.
  1227. Because labels final address are only known after calling @code{emit},
  1228. it is required to call @code{patch} or @code{patch_at}, what does
  1229. tell @lightning{} that the target to patch is actually a pointer to
  1230. a @code{jit_node_t *} object, otherwise, it would assume that is
  1231. a pointer to a C function. Note that conditional branches do not
  1232. receive a label argument, so they must be patched.
  1233. You need to call @code{patch_at} on the return of value @code{calli},
  1234. @code{finishi}, and @code{calli} if it is actually referencing a label
  1235. in the jit code. All branch instructions do not receive a label
  1236. argument. Note that @code{movi} is an special case, and patching it
  1237. is usually done to get the final address of a label, usually to later
  1238. call @code{jmpr}.
  1239. Now, here is the iterative version:
  1240. @example
  1241. #include <stdio.h>
  1242. #include <lightning.h>
  1243. static jit_state_t *_jit;
  1244. typedef int (*pifi)(int); @rem{/* Pointer to Int Function of Int */}
  1245. int main(int argc, char *argv[])
  1246. @{
  1247. pifi fib;
  1248. jit_node_t *in; @rem{/* offset of the argument */}
  1249. jit_node_t *ref; @rem{/* to patch the forward reference */}
  1250. jit_node_t *zero; @rem{/* to patch the forward reference */}
  1251. jit_node_t *jump; @rem{/* jump to start of loop */}
  1252. jit_node_t *loop; @rem{/* start of the loop */}
  1253. init_jit(argv[0]);
  1254. _jit = jit_new_state();
  1255. jit_prolog ();
  1256. in = jit_arg ();
  1257. jit_getarg (JIT_R0, in); @rem{/* R0 = n */}
  1258. zero = jit_beqi (JIT_R0, 0);
  1259. jit_movr (JIT_R1, JIT_R0);
  1260. jit_movi (JIT_R0, 1);
  1261. ref = jit_blti (JIT_R1, 2);
  1262. jit_subi (JIT_R2, JIT_R2, 2);
  1263. jit_movr (JIT_R1, JIT_R0);
  1264. loop= jit_label();
  1265. jit_subi (JIT_R2, JIT_R2, 1); @rem{/* decr. counter */}
  1266. jit_movr (JIT_V0, JIT_R0); /* V0 = R0 */
  1267. jit_addr (JIT_R0, JIT_R0, JIT_R1); /* R0 = R0 + R1 */
  1268. jit_movr (JIT_R1, JIT_V0); /* R1 = V0 */
  1269. jump= jit_bnei (JIT_R2, 0); /* if (R2) goto loop; */
  1270. jit_patch_at(jump, loop);
  1271. jit_patch(ref); @rem{/* patch forward jump */}
  1272. jit_patch(zero); @rem{/* patch forward jump */}
  1273. jit_retr (JIT_R0);
  1274. @rem{/* call the generated code@comma{} passing 36 as an argument */}
  1275. fib = jit_emit();
  1276. jit_clear_state();
  1277. printf("fib(%d) = %d\n", 36, fib(36));
  1278. jit_destroy_state();
  1279. finish_jit();
  1280. return 0;
  1281. @}
  1282. @end example
  1283. This code calculates the recurrence relation using iteration (a
  1284. @code{for} loop in high-level languages). There are no function
  1285. calls anymore: instead, there is a backward jump (the @code{bnei} at
  1286. the end of the loop).
  1287. Note that the program must remember the address for backward jumps;
  1288. for forward jumps it is only required to remember the jump code,
  1289. and call @code{patch} for the implicit label.
  1290. @node Reentrancy
  1291. @chapter Re-entrant usage of @lightning{}
  1292. @lightning{} uses the special @code{_jit} identifier. To be able
  1293. to be able to use multiple jit generation states at the same
  1294. time, it is required to used code similar to:
  1295. @example
  1296. struct jit_state lightning;
  1297. #define lightning _jit
  1298. @end example
  1299. This will cause the symbol defined to @code{_jit} to be passed as
  1300. the first argument to the underlying @lightning{} implementation,
  1301. that is usually a function with an @code{_} (underscode) prefix
  1302. and with an argument named @code{_jit}, in the pattern:
  1303. @example
  1304. static void _jit_mnemonic(jit_state_t *, jit_gpr_t, jit_gpr_t);
  1305. #define jit_mnemonic(u, v) _jit_mnemonic(_jit, u, v);
  1306. @end example
  1307. The reason for this is to use the same syntax as the initial lightning
  1308. implementation and to avoid needing the user to keep adding an extra
  1309. argument to every call, as multiple jit states generating code in
  1310. paralell should be very uncommon.
  1311. @section Registers
  1312. @chapter Accessing the whole register file
  1313. As mentioned earlier in this chapter, all @lightning{} back-ends are
  1314. guaranteed to have at least six general-purpose integer registers and
  1315. six floating-point registers, but many back-ends will have more.
  1316. To access the entire register files, you can use the
  1317. @code{JIT_R}, @code{JIT_V} and @code{JIT_F} macros. They
  1318. accept a parameter that identifies the register number, which
  1319. must be strictly less than @code{JIT_R_NUM}, @code{JIT_V_NUM}
  1320. and @code{JIT_F_NUM} respectively; the number need not be
  1321. constant. Of course, expressions like @code{JIT_R0} and
  1322. @code{JIT_R(0)} denote the same register, and likewise for
  1323. integer callee-saved, or floating-point, registers.
  1324. @node Customizations
  1325. @chapter Customizations
  1326. Frequently it is desirable to have more control over how code is
  1327. generated or how memory is used during jit generation or execution.
  1328. @section Memory functions
  1329. To aid in complete control of memory allocation and deallocation
  1330. @lightning{} provides wrappers that default to standard @code{malloc},
  1331. @code{realloc} and @code{free}. These are loosely based on the
  1332. GNU GMP counterparts, with the difference that they use the same
  1333. prototype of the system allocation functions, that is, no @code{size}
  1334. for @code{free} or @code{old_size} for @code{realloc}.
  1335. @deftypefun void jit_set_memory_functions (@* void *(*@var{alloc_func_ptr}) (size_t), @* void *(*@var{realloc_func_ptr}) (void *, size_t), @* void (*@var{free_func_ptr}) (void *))
  1336. @lightning{} guarantees that memory is only allocated or released
  1337. using these wrapped functions, but you must note that if lightning
  1338. was linked to GNU binutils, malloc is probably will be called multiple
  1339. times from there when initializing the disassembler.
  1340. Because @code{init_jit} may call memory functions, if you need to call
  1341. @code{jit_set_memory_functions}, it must be called before @code{init_jit},
  1342. otherwise, when calling @code{finish_jit}, a pointer allocated with the
  1343. previous or default wrappers will be passed.
  1344. @end deftypefun
  1345. @deftypefun void jit_get_memory_functions (@* void *(**@var{alloc_func_ptr}) (size_t), @* void *(**@var{realloc_func_ptr}) (void *, size_t), @* void (**@var{free_func_ptr}) (void *))
  1346. Get the current memory allocation function. Also, unlike the GNU GMP
  1347. counterpart, it is an error to pass @code{NULL} pointers as arguments.
  1348. @end deftypefun
  1349. @section Alternate code buffer
  1350. To instruct @lightning{} to use an alternate code buffer it is required
  1351. to call @code{jit_realize} before @code{jit_emit}, and then query states
  1352. and customize as appropriate.
  1353. @deftypefun void jit_realize ()
  1354. Must be called once, before @code{jit_emit}, to instruct @lightning{}
  1355. that no other @code{jit_xyz} call will be made.
  1356. @end deftypefun
  1357. @deftypefun jit_pointer_t jit_get_code (jit_word_t *@var{code_size})
  1358. Returns NULL or the previous value set with @code{jit_set_code}, and
  1359. sets the @var{code_size} argument to an appropriate value.
  1360. If @code{jit_get_code} is called before @code{jit_emit}, the
  1361. @var{code_size} argument is set to the expected amount of bytes
  1362. required to generate code.
  1363. If @code{jit_get_code} is called after @code{jit_emit}, the
  1364. @var{code_size} argument is set to the exact amount of bytes used
  1365. by the code.
  1366. @end deftypefun
  1367. @deftypefun void jit_set_code (jit_ponter_t @var{code}, jit_word_t @var{size})
  1368. Instructs @lightning{} to output to the @var{code} argument and
  1369. use @var{size} as a guard to not write to invalid memory. If during
  1370. @code{jit_emit} @lightning{} finds out that the code would not fit
  1371. in @var{size} bytes, it halts code emit and returns @code{NULL}.
  1372. @end deftypefun
  1373. A simple example of a loop using an alternate buffer is:
  1374. @example
  1375. jit_uint8_t *code;
  1376. int *(func)(int); @rem{/* function pointer */}
  1377. jit_word_t code_size;
  1378. jit_word_t real_code_size;
  1379. @rem{...}
  1380. jit_realize(); @rem{/* ready to generate code */}
  1381. jit_get_code(&code_size); @rem{/* get expected code size */}
  1382. code_size = (code_size + 4095) & -4096;
  1383. do (;;) @{
  1384. code = mmap(NULL, code_size, PROT_EXEC | PROT_READ | PROT_WRITE,
  1385. MAP_PRIVATE | MAP_ANON, -1, 0);
  1386. jit_set_code(code, code_size);
  1387. if ((func = jit_emit()) == NULL) @{
  1388. munmap(code, code_size);
  1389. code_size += 4096;
  1390. @}
  1391. @} while (func == NULL);
  1392. jit_get_code(&real_code_size); @rem{/* query exact size of the code */}
  1393. @end example
  1394. The first call to @code{jit_get_code} should return @code{NULL} and set
  1395. the @code{code_size} argument to the expected amount of bytes required
  1396. to emit code.
  1397. The second call to @code{jit_get_code} is after a successful call to
  1398. @code{jit_emit}, and will return the value previously set with
  1399. @code{jit_set_code} and set the @code{real_code_size} argument to the
  1400. exact amount of bytes used to emit the code.
  1401. @section Alternate data buffer
  1402. Sometimes it may be desirable to customize how, or to prevent
  1403. @lightning{} from using an extra buffer for constants or debug
  1404. annotation. Usually when also using an alternate code buffer.
  1405. @deftypefun jit_pointer_t jit_get_data (jit_word_t *@var{data_size}, jit_word_t *@var{note_size})
  1406. Returns @code{NULL} or the previous value set with @code{jit_set_data},
  1407. and sets the @var{data_size} argument to how many bytes are required
  1408. for the constants data buffer, and @var{note_size} to how many bytes
  1409. are required to store the debug note information.
  1410. Note that it always preallocate one debug note entry even if
  1411. @code{jit_name} or @code{jit_note} are never called, but will return
  1412. zero in the @var{data_size} argument if no constant is required;
  1413. constants are only used for the @code{float} and @code{double} operations
  1414. that have an immediate argument, and not in all @lightning{} ports.
  1415. @end deftypefun
  1416. @deftypefun void jit_set_data (jit_pointer_t @var{data}, jit_word_t @var{size}, jit_word_t @var{flags})
  1417. @var{data} can be NULL if disabling constants and annotations, otherwise,
  1418. a valid pointer must be passed. An assertion is done that the data will
  1419. fit in @var{size} bytes (but that is a noop if @lightning{} was built
  1420. with @code{-DNDEBUG}).
  1421. @var{size} tells the space in bytes available in @var{data}.
  1422. @var{flags} can be zero to tell to just use the alternate data buffer,
  1423. or a composition of @code{JIT_DISABLE_DATA} and @code{JIT_DISABLE_NOTE}
  1424. @table @t
  1425. @item JIT_DISABLE_DATA
  1426. @cindex JIT_DISABLE_DATA
  1427. Instructs @lightning{} to not use a constant table, but to use an
  1428. alternate method to synthesize those, usually with a larger code
  1429. sequence using stack space to transfer the value from a GPR to a
  1430. FPR register.
  1431. @item JIT_DISABLE_NOTE
  1432. @cindex JIT_DISABLE_NOTE
  1433. Instructs @lightning{} to not store file or function name, and
  1434. line numbers in the constant buffer.
  1435. @end table
  1436. @end deftypefun
  1437. A simple example of a preventing usage of a data buffer is:
  1438. @example
  1439. @rem{...}
  1440. jit_realize(); @rem{/* ready to generate code */}
  1441. jit_get_data(NULL, NULL);
  1442. jit_set_data(NULL, 0, JIT_DISABLE_DATA | JIT_DISABLE_NOTE);
  1443. @rem{...}
  1444. @end example
  1445. Or to only use a data buffer, if required:
  1446. @example
  1447. jit_uint8_t *data;
  1448. jit_word_t data_size;
  1449. @rem{...}
  1450. jit_realize(); @rem{/* ready to generate code */}
  1451. jit_get_data(&data_size, NULL);
  1452. if (data_size)
  1453. data = malloc(data_size);
  1454. else
  1455. data = NULL;
  1456. jit_set_data(data, data_size, JIT_DISABLE_NOTE);
  1457. @rem{...}
  1458. if (data)
  1459. free(data);
  1460. @rem{...}
  1461. @end example
  1462. @node Acknowledgements
  1463. @chapter Acknowledgements
  1464. As far as I know, the first general-purpose portable dynamic code
  1465. generator is @sc{dcg}, by Dawson R.@: Engler and T.@: A.@: Proebsting.
  1466. Further work by Dawson R. Engler resulted in the @sc{vcode} system;
  1467. unlike @sc{dcg}, @sc{vcode} used no intermediate representation and
  1468. directly inspired @lightning{}.
  1469. Thanks go to Ian Piumarta, who kindly accepted to release his own
  1470. program @sc{ccg} under the GNU General Public License, thereby allowing
  1471. @lightning{} to use the run-time assemblers he had wrote for @sc{ccg}.
  1472. @sc{ccg} provides a way of dynamically assemble programs written in the
  1473. underlying architecture's assembly language. So it is not portable,
  1474. yet very interesting.
  1475. I also thank Steve Byrne for writing GNU Smalltalk, since @lightning{}
  1476. was first developed as a tool to be used in GNU Smalltalk's dynamic
  1477. translator from bytecodes to native code.
  1478. @c %**end of header (This is for running Texinfo on a region.)
  1479. @c ***********************************************************************
  1480. @bye