vm.texi 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2008,2009,2010,2011,2013,2015
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node A Virtual Machine for Guile
  7. @section A Virtual Machine for Guile
  8. Guile has both an interpreter and a compiler. To a user, the difference
  9. is transparent---interpreted and compiled procedures can call each other
  10. as they please.
  11. The difference is that the compiler creates and interprets bytecode
  12. for a custom virtual machine, instead of interpreting the
  13. S-expressions directly. Loading and running compiled code is faster
  14. than loading and running source code.
  15. The virtual machine that does the bytecode interpretation is a part of
  16. Guile itself. This section describes the nature of Guile's virtual
  17. machine.
  18. @menu
  19. * Why a VM?::
  20. * VM Concepts::
  21. * Stack Layout::
  22. * Variables and the VM::
  23. * VM Programs::
  24. * Object File Format::
  25. * Instruction Set::
  26. @end menu
  27. @node Why a VM?
  28. @subsection Why a VM?
  29. @cindex interpreter
  30. For a long time, Guile only had an interpreter. Guile's interpreter
  31. operated directly on the S-expression representation of Scheme source
  32. code.
  33. But while the interpreter was highly optimized and hand-tuned, it still
  34. performed many needless computations during the course of evaluating an
  35. expression. For example, application of a function to arguments
  36. needlessly consed up the arguments in a list. Evaluation of an
  37. expression always had to figure out what the car of the expression is --
  38. a procedure, a memoized form, or something else. All values have to be
  39. allocated on the heap. Et cetera.
  40. The solution to this problem was to compile the higher-level language,
  41. Scheme, into a lower-level language for which all of the checks and
  42. dispatching have already been done---the code is instead stripped to
  43. the bare minimum needed to ``do the job''.
  44. The question becomes then, what low-level language to choose? There
  45. are many options. We could compile to native code directly, but that
  46. poses portability problems for Guile, as it is a highly cross-platform
  47. project.
  48. So we want the performance gains that compilation provides, but we
  49. also want to maintain the portability benefits of a single code path.
  50. The obvious solution is to compile to a virtual machine that is
  51. present on all Guile installations.
  52. The easiest (and most fun) way to depend on a virtual machine is to
  53. implement the virtual machine within Guile itself. This way the
  54. virtual machine provides what Scheme needs (tail calls, multiple
  55. values, @code{call/cc}) and can provide optimized inline instructions
  56. for Guile (@code{cons}, @code{struct-ref}, etc.).
  57. So this is what Guile does. The rest of this section describes that VM
  58. that Guile implements, and the compiled procedures that run on it.
  59. Before moving on, though, we should note that though we spoke of the
  60. interpreter in the past tense, Guile still has an interpreter. The
  61. difference is that before, it was Guile's main evaluator, and so was
  62. implemented in highly optimized C; now, it is actually implemented in
  63. Scheme, and compiled down to VM bytecode, just like any other program.
  64. (There is still a C interpreter around, used to bootstrap the compiler,
  65. but it is not normally used at runtime.)
  66. The upside of implementing the interpreter in Scheme is that we preserve
  67. tail calls and multiple-value handling between interpreted and compiled
  68. code. The downside is that the interpreter in Guile 2.2 is still about
  69. twice as slow as the interpreter in 1.8. Since Scheme users are mostly
  70. running compiled code, the compiler's speed more than makes up for the
  71. loss. In any case, once we have native compilation for Scheme code, we
  72. expect the self-hosted interpreter to handily beat the old hand-tuned C
  73. implementation.
  74. Also note that this decision to implement a bytecode compiler does not
  75. preclude native compilation. We can compile from bytecode to native
  76. code at runtime, or even do ahead of time compilation. More
  77. possibilities are discussed in @ref{Extending the Compiler}.
  78. @node VM Concepts
  79. @subsection VM Concepts
  80. Compiled code is run by a virtual machine (VM). Each thread has its own
  81. VM. The virtual machine executes the sequence of instructions in a
  82. procedure.
  83. Each VM instruction starts by indicating which operation it is, and then
  84. follows by encoding its source and destination operands. Each procedure
  85. declares that it has some number of local variables, including the
  86. function arguments. These local variables form the available operands
  87. of the procedure, and are accessed by index.
  88. The local variables for a procedure are stored on a stack. Calling a
  89. procedure typically enlarges the stack, and returning from a procedure
  90. shrinks it. Stack memory is exclusive to the virtual machine that owns
  91. it.
  92. In addition to their stacks, virtual machines also have access to the
  93. global memory (modules, global bindings, etc) that is shared among other
  94. parts of Guile, including other VMs.
  95. The registers that a VM has are as follows:
  96. @itemize
  97. @item ip - Instruction pointer
  98. @item sp - Stack pointer
  99. @item fp - Frame pointer
  100. @end itemize
  101. In other architectures, the instruction pointer is sometimes called the
  102. ``program counter'' (pc). This set of registers is pretty typical for
  103. virtual machines; their exact meanings in the context of Guile's VM are
  104. described in the next section.
  105. @node Stack Layout
  106. @subsection Stack Layout
  107. The stack of Guile's virtual machine is composed of @dfn{frames}. Each
  108. frame corresponds to the application of one compiled procedure, and
  109. contains storage space for arguments, local variables, and some
  110. bookkeeping information (such as what to do after the frame is
  111. finished).
  112. While the compiler is free to do whatever it wants to, as long as the
  113. semantics of a computation are preserved, in practice every time you
  114. call a function, a new frame is created. (The notable exception of
  115. course is the tail call case, @pxref{Tail Calls}.)
  116. The structure of the top stack frame is as follows:
  117. @example
  118. | ... |
  119. +==================+ <- fp + 2 = SCM_FRAME_PREVIOUS_SP (fp)
  120. | Dynamic link |
  121. +------------------+
  122. | Return address |
  123. +==================+ <- fp
  124. | Local 0 |
  125. +------------------+
  126. | Local 1 |
  127. +------------------+
  128. | ... |
  129. +------------------+
  130. | Local N-1 |
  131. \------------------/ <- sp
  132. @end example
  133. In the above drawing, the stack grows downward. At the beginning of a
  134. function call, the procedure being applied is in local 0, followed by
  135. the arguments from local 1. After the procedure checks that it is being
  136. passed a compatible set of arguments, the procedure allocates some
  137. additional space in the frame to hold variables local to the function.
  138. Note that once a value in a local variable slot is no longer needed,
  139. Guile is free to re-use that slot. This applies to the slots that were
  140. initially used for the callee and arguments, too. For this reason,
  141. backtraces in Guile aren't always able to show all of the arguments: it
  142. could be that the slot corresponding to that argument was re-used by
  143. some other variable.
  144. The @dfn{return address} is the @code{ip} that was in effect before this
  145. program was applied. When we return from this activation frame, we will
  146. jump back to this @code{ip}. Likewise, the @dfn{dynamic link} is the
  147. offset of the @code{fp} that was in effect before this program was
  148. applied, relative to the current @code{fp}.
  149. To prepare for a non-tail application, Guile's VM will emit code that
  150. shuffles the function to apply and its arguments into appropriate stack
  151. slots, with two free slots below them. The call then initializes those
  152. free slots with the current @code{ip} and @code{fp}, and updates
  153. @code{ip} to point to the function entry, and @code{fp} to point to the
  154. new call frame.
  155. In this way, the dynamic link links the current frame to the previous
  156. frame. Computing a stack trace involves traversing these frames.
  157. Each stack local in Guile is 64 bits wide, even on 32-bit architectures.
  158. This allows Guile to preserve its uniform treatment of stack locals
  159. while allowing for unboxed arithmetic on 64-bit integers and
  160. floating-point numbers. @xref{Instruction Set}, for more on unboxed
  161. arithmetic.
  162. As an implementation detail, we actually store the dynamic link as an
  163. offset and not an absolute value because the stack can move at runtime
  164. as it expands or during partial continuation calls. If it were an
  165. absolute value, we would have to walk the frames, relocating frame
  166. pointers.
  167. @node Variables and the VM
  168. @subsection Variables and the VM
  169. Consider the following Scheme code as an example:
  170. @example
  171. (define (foo a)
  172. (lambda (b) (list foo a b)))
  173. @end example
  174. Within the lambda expression, @code{foo} is a top-level variable,
  175. @code{a} is a lexically captured variable, and @code{b} is a local
  176. variable.
  177. Another way to refer to @code{a} and @code{b} is to say that @code{a} is
  178. a ``free'' variable, since it is not defined within the lambda, and
  179. @code{b} is a ``bound'' variable. These are the terms used in the
  180. @dfn{lambda calculus}, a mathematical notation for describing functions.
  181. The lambda calculus is useful because it is a language in which to
  182. reason precisely about functions and variables. It is especially good
  183. at describing scope relations, and it is for that reason that we mention
  184. it here.
  185. Guile allocates all variables on the stack. When a lexically enclosed
  186. procedure with free variables---a @dfn{closure}---is created, it copies
  187. those variables into its free variable vector. References to free
  188. variables are then redirected through the free variable vector.
  189. If a variable is ever @code{set!}, however, it will need to be
  190. heap-allocated instead of stack-allocated, so that different closures
  191. that capture the same variable can see the same value. Also, this
  192. allows continuations to capture a reference to the variable, instead
  193. of to its value at one point in time. For these reasons, @code{set!}
  194. variables are allocated in ``boxes''---actually, in variable cells.
  195. @xref{Variables}, for more information. References to @code{set!}
  196. variables are indirected through the boxes.
  197. Thus perhaps counterintuitively, what would seem ``closer to the
  198. metal'', viz @code{set!}, actually forces an extra memory allocation and
  199. indirection. Sometimes Guile's optimizer can remove this allocation,
  200. but not always.
  201. Going back to our example, @code{b} may be allocated on the stack, as
  202. it is never mutated.
  203. @code{a} may also be allocated on the stack, as it too is never
  204. mutated. Within the enclosed lambda, its value will be copied into
  205. (and referenced from) the free variables vector.
  206. @code{foo} is a top-level variable, because @code{foo} is not
  207. lexically bound in this example.
  208. @node VM Programs
  209. @subsection Compiled Procedures are VM Programs
  210. By default, when you enter in expressions at Guile's REPL, they are
  211. first compiled to bytecode. Then that bytecode is executed to produce a
  212. value. If the expression evaluates to a procedure, the result of this
  213. process is a compiled procedure.
  214. A compiled procedure is a compound object consisting of its bytecode and
  215. a reference to any captured lexical variables. In addition, when a
  216. procedure is compiled, it has associated metadata written to side
  217. tables, for instance a line number mapping, or its docstring. You can
  218. pick apart these pieces with the accessors in @code{(system vm
  219. program)}. @xref{Compiled Procedures}, for a full API reference.
  220. A procedure may reference data that was statically allocated when the
  221. procedure was compiled. For example, a pair of immediate objects
  222. (@pxref{Immediate objects}) can be allocated directly in the memory
  223. segment that contains the compiled bytecode, and accessed directly by
  224. the bytecode.
  225. Another use for statically allocated data is to serve as a cache for a
  226. bytecode. Top-level variable lookups are handled in this way. If the
  227. @code{toplevel-box} instruction finds that it does not have a cached
  228. variable for a top-level reference, it accesses other static data to
  229. resolve the reference, and fills in the cache slot. Thereafter all
  230. access to the variable goes through the cache cell. The variable's
  231. value may change in the future, but the variable itself will not.
  232. We can see how these concepts tie together by disassembling the
  233. @code{foo} function we defined earlier to see what is going on:
  234. @smallexample
  235. scheme@@(guile-user)> (define (foo a) (lambda (b) (list foo a b)))
  236. scheme@@(guile-user)> ,x foo
  237. Disassembly of #<procedure foo (a)> at #xea4ce4:
  238. 0 (assert-nargs-ee/locals 2 0) ;; 2 slots (1 arg) at (unknown file):1:0
  239. 1 (make-closure 1 7 1) ;; anonymous procedure at #xea4d04 (1 free var)
  240. 4 (free-set! 1 0 0) ;; free var 0
  241. 6 (mov 0 1)
  242. 7 (return-values 2) ;; 1 value
  243. ----------------------------------------
  244. Disassembly of anonymous procedure at #xea4d04:
  245. 0 (assert-nargs-ee/locals 2 2) ;; 4 slots (1 arg) at (unknown file):1:16
  246. 1 (toplevel-box 1 74 58 68 #t) ;; `foo'
  247. 6 (box-ref 1 1)
  248. 7 (make-short-immediate 0 772) ;; () at (unknown file):1:28
  249. 8 (cons 2 2 0)
  250. 9 (free-ref 3 3 0) ;; free var 0
  251. 11 (cons 3 3 2)
  252. 12 (cons 2 1 3)
  253. 13 (return-values 2) ;; 1 value
  254. @end smallexample
  255. First there's some prelude, where @code{foo} checks that it was called
  256. with only 1 argument. Then at @code{ip} 1, we allocate a new closure
  257. and store it in slot 1, relative to the @code{sp}.
  258. At run-time, local variables in Guile are usually addressed relative to
  259. the stack pointer, which leads to a pleasantly efficient
  260. @code{sp[@var{n}]} access. However it can make the disassembly hard to
  261. read, because the @code{sp} can change during the function, and because
  262. incoming arguments are relative to the @code{fp}, not the @code{sp}.
  263. To know what @code{fp}-relative slot corresponds to an
  264. @code{sp}-relative reference, scan up in the disassembly until you get
  265. to a ``@var{n} slots'' annotation; in our case, 2, indicating that the
  266. frame has space for 2 slots. Thus a zero-indexed @code{sp}-relative
  267. slot of 1 corresponds to the @code{fp}-relative slot of 0, which
  268. initially held the value of the closure being called. This means that
  269. Guile doesn't need the value of the closure to compute its result, and
  270. so slot 0 was free for re-use, in this case for the result of making a
  271. new closure.
  272. A closure is code with data. The @code{6} in the @code{(make-closure 1
  273. 6 1)} is a relative offset from the instruction pointer of the code for
  274. the closure, and the final @code{1} indicates that the closure has space
  275. for 1 free variable. @code{Ip} 4 initializes free variable 0 in the new
  276. closure with the value from @code{sp}-relative slot 0, which corresponds
  277. to @code{fp}-relative slot 1, the first argument of @code{foo}:
  278. @code{a}. Finally we return the closure.
  279. The second stanza disassembles the code for the closure. After the
  280. prelude, we load the variable for the toplevel variable @code{foo} into
  281. slot 1. This lookup occurs lazily, the first time the variable is
  282. actually referenced, and the location of the lookup is cached so that
  283. future references are very cheap. @xref{Top-Level Environment
  284. Instructions}, for more details. The @code{box-ref} dereferences the
  285. variable cell, replacing the contents of slot 1.
  286. What follows is a sequence of conses to build up the result list.
  287. @code{Ip} 7 makes the tail of the list. @code{Ip} 8 conses on the value
  288. in slot 2, corresponding to the first argument to the closure: @code{b}.
  289. @code{Ip} 9 loads free variable 0 of slot 3 -- the procedure being
  290. called, in @code{fp}-relative slot 0 -- into slot 3, then @code{ip} 11
  291. conses it onto the list. Finally we cons the value in slot 1,
  292. containing the @code{foo} toplevel, onto the front of the list, and we
  293. return it.
  294. @node Object File Format
  295. @subsection Object File Format
  296. To compile a file to disk, we need a format in which to write the
  297. compiled code to disk, and later load it into Guile. A good @dfn{object
  298. file format} has a number of characteristics:
  299. @itemize
  300. @item Above all else, it should be very cheap to load a compiled file.
  301. @item It should be possible to statically allocate constants in the
  302. file. For example, a bytevector literal in source code can be emitted
  303. directly into the object file.
  304. @item The compiled file should enable maximum code and data sharing
  305. between different processes.
  306. @item The compiled file should contain debugging information, such as
  307. line numbers, but that information should be separated from the code
  308. itself. It should be possible to strip debugging information if space
  309. is tight.
  310. @end itemize
  311. These characteristics are not specific to Scheme. Indeed, mainstream
  312. languages like C and C++ have solved this issue many times in the past.
  313. Guile builds on their work by adopting ELF, the object file format of
  314. GNU and other Unix-like systems, as its object file format. Although
  315. Guile uses ELF on all platforms, we do not use platform support for ELF.
  316. Guile implements its own linker and loader. The advantage of using ELF
  317. is not sharing code, but sharing ideas. ELF is simply a well-designed
  318. object file format.
  319. An ELF file has two meta-tables describing its contents. The first
  320. meta-table is for the loader, and is called the @dfn{program table} or
  321. sometimes the @dfn{segment table}. The program table divides the file
  322. into big chunks that should be treated differently by the loader.
  323. Mostly the difference between these @dfn{segments} is their
  324. permissions.
  325. Typically all segments of an ELF file are marked as read-only, except
  326. that part that represents modifiable static data or static data that
  327. needs load-time initialization. Loading an ELF file is as simple as
  328. mmapping the thing into memory with read-only permissions, then using
  329. the segment table to mark a small sub-region of the file as writable.
  330. This writable section is typically added to the root set of the garbage
  331. collector as well.
  332. One ELF segment is marked as ``dynamic'', meaning that it has data of
  333. interest to the loader. Guile uses this segment to record the Guile
  334. version corresponding to this file. There is also an entry in the
  335. dynamic segment that points to the address of an initialization thunk
  336. that is run to perform any needed link-time initialization. (This is
  337. like dynamic relocations for normal ELF shared objects, except that we
  338. compile the relocations as a procedure instead of having the loader
  339. interpret a table of relocations.) Finally, the dynamic segment marks
  340. the location of the ``entry thunk'' of the object file. This thunk is
  341. returned to the caller of @code{load-thunk-from-memory} or
  342. @code{load-thunk-from-file}. When called, it will execute the ``body''
  343. of the compiled expression.
  344. The other meta-table in an ELF file is the @dfn{section table}. Whereas
  345. the program table divides an ELF file into big chunks for the loader,
  346. the section table specifies small sections for use by introspective
  347. tools like debuggers or the like. One segment (program table entry)
  348. typically contains many sections. There may be sections outside of any
  349. segment, as well.
  350. Typical sections in a Guile @code{.go} file include:
  351. @table @code
  352. @item .rtl-text
  353. Bytecode.
  354. @item .data
  355. Data that needs initialization, or which may be modified at runtime.
  356. @item .rodata
  357. Statically allocated data that needs no run-time initialization, and
  358. which therefore can be shared between processes.
  359. @item .dynamic
  360. The dynamic section, discussed above.
  361. @item .symtab
  362. @itemx .strtab
  363. A table mapping addresses in the @code{.rtl-text} to procedure names.
  364. @code{.strtab} is used by @code{.symtab}.
  365. @item .guile.procprops
  366. @itemx .guile.arities
  367. @itemx .guile.arities.strtab
  368. @itemx .guile.docstrs
  369. @itemx .guile.docstrs.strtab
  370. Side tables of procedure properties, arities, and docstrings.
  371. @item .guile.docstrs.strtab
  372. Side table of frame maps, describing the set of live slots for ever
  373. return point in the program text, and whether those slots are pointers
  374. are not. Used by the garbage collector.
  375. @item .debug_info
  376. @itemx .debug_abbrev
  377. @itemx .debug_str
  378. @itemx .debug_loc
  379. @itemx .debug_line
  380. Debugging information, in DWARF format. See the DWARF specification,
  381. for more information.
  382. @item .shstrtab
  383. Section name string table.
  384. @end table
  385. For more information, see @uref{http://linux.die.net/man/5/elf,,the
  386. elf(5) man page}. See @uref{http://dwarfstd.org/,the DWARF
  387. specification} for more on the DWARF debugging format. Or if you are an
  388. adventurous explorer, try running @code{readelf} or @code{objdump} on
  389. compiled @code{.go} files. It's good times!
  390. @node Instruction Set
  391. @subsection Instruction Set
  392. There are currently about 175 instructions in Guile's virtual machine.
  393. These instructions represent atomic units of a program's execution.
  394. Ideally, they perform one task without conditional branches, then
  395. dispatch to the next instruction in the stream.
  396. Instructions themselves are composed of 1 or more 32-bit units. The low
  397. 8 bits of the first word indicate the opcode, and the rest of
  398. instruction describe the operands. There are a number of different ways
  399. operands can be encoded.
  400. @table @code
  401. @item s@var{n}
  402. An unsigned @var{n}-bit integer, indicating the @code{sp}-relative index
  403. of a local variable.
  404. @item f@var{n}
  405. An unsigned @var{n}-bit integer, indicating the @code{fp}-relative index
  406. of a local variable. Used when a continuation accepts a variable number
  407. of values, to shuffle received values into known locations in the
  408. frame.
  409. @item c@var{n}
  410. An unsigned @var{n}-bit integer, indicating a constant value.
  411. @item l24
  412. An offset from the current @code{ip}, in 32-bit units, as a signed
  413. 24-bit value. Indicates a bytecode address, for a relative jump.
  414. @item i16
  415. @itemx i32
  416. An immediate Scheme value (@pxref{Immediate objects}), encoded directly
  417. in 16 or 32 bits.
  418. @item a32
  419. @itemx b32
  420. An immediate Scheme value, encoded as a pair of 32-bit words.
  421. @code{a32} and @code{b32} values always go together on the same opcode,
  422. and indicate the high and low bits, respectively. Normally only used on
  423. 64-bit systems.
  424. @item n32
  425. A statically allocated non-immediate. The address of the non-immediate
  426. is encoded as a signed 32-bit integer, and indicates a relative offset
  427. in 32-bit units. Think of it as @code{SCM x = ip + offset}.
  428. @item r32
  429. Indirect scheme value, like @code{n32} but indirected. Think of it as
  430. @code{SCM *x = ip + offset}.
  431. @item l32
  432. @item lo32
  433. An ip-relative address, as a signed 32-bit integer. Could indicate a
  434. bytecode address, as in @code{make-closure}, or a non-immediate address,
  435. as with @code{static-patch!}.
  436. @code{l32} and @code{lo32} are the same from the perspective of the
  437. virtual machine. The difference is that an assembler might want to
  438. allow an @code{lo32} address to be specified as a label and then some
  439. number of words offset from that label, for example when patching a
  440. field of a statically allocated object.
  441. @item b1
  442. A boolean value: 1 for true, otherwise 0.
  443. @item x@var{n}
  444. An ignored sequence of @var{n} bits.
  445. @end table
  446. An instruction is specified by giving its name, then describing its
  447. operands. The operands are packed by 32-bit words, with earlier
  448. operands occupying the lower bits.
  449. For example, consider the following instruction specification:
  450. @deftypefn Instruction {} free-set! s12:@var{dst} s12:@var{src} x8:@var{_} c24:@var{idx}
  451. Set free variable @var{idx} from the closure @var{dst} to @var{src}.
  452. @end deftypefn
  453. The first word in the instruction will start with the 8-bit value
  454. corresponding to the @var{free-set!} opcode in the low bits, followed by
  455. @var{dst} and @var{src} as 12-bit values. The second word starts with 8
  456. dead bits, followed by the index as a 24-bit immediate value.
  457. Sometimes the compiler can figure out that it is compiling a special
  458. case that can be run more efficiently. So, for example, while Guile
  459. offers a generic test-and-branch instruction, it also offers specific
  460. instructions for special cases, so that the following cases all have
  461. their own test-and-branch instructions:
  462. @example
  463. (if pred then else)
  464. (if (not pred) then else)
  465. (if (null? l) then else)
  466. (if (not (null? l)) then else)
  467. @end example
  468. In addition, some Scheme primitives have their own inline
  469. implementations. For example, in the previous section we saw
  470. @code{cons}.
  471. Finally, for instructions with operands that encode references to the
  472. stack, the interpretation of those stack values is up to the instruction
  473. itself. Most instructions expect their operands to be tagged SCM values
  474. (@code{scm} representation), but some instructions expect unboxed
  475. integers (@code{u64} and @code{s64} representations) or floating-point
  476. numbers (@var{f64} representation). Instructions have static types:
  477. they must receive their operands in the format they expect. It's up to
  478. the compiler to ensure this is the case. Unless otherwise mentioned,
  479. all operands and results are boxed as SCM values.
  480. @menu
  481. * Lexical Environment Instructions::
  482. * Top-Level Environment Instructions::
  483. * Procedure Call and Return Instructions::
  484. * Function Prologue Instructions::
  485. * Trampoline Instructions::
  486. * Branch Instructions::
  487. * Constant Instructions::
  488. * Dynamic Environment Instructions::
  489. * Miscellaneous Instructions::
  490. * Inlined Scheme Instructions::
  491. * Inlined Mathematical Instructions::
  492. * Inlined Bytevector Instructions::
  493. * Unboxed Integer Arithmetic::
  494. * Unboxed Floating-Point Arithmetic::
  495. @end menu
  496. @node Lexical Environment Instructions
  497. @subsubsection Lexical Environment Instructions
  498. These instructions access and mutate the lexical environment of a
  499. compiled procedure---its free and bound variables. @xref{Stack Layout},
  500. for more information on the format of stack frames.
  501. @deftypefn Instruction {} mov s12:@var{dst} s12:@var{src}
  502. @deftypefnx Instruction {} long-mov s24:@var{dst} x8:@var{_} s24:@var{src}
  503. Copy a value from one local slot to another.
  504. As discussed previously, procedure arguments and local variables are
  505. allocated to local slots. Guile's compiler tries to avoid shuffling
  506. variables around to different slots, which often makes @code{mov}
  507. instructions redundant. However there are some cases in which shuffling
  508. is necessary, and in those cases, @code{mov} is the thing to use.
  509. @end deftypefn
  510. @deftypefn Instruction {} long-fmov f24:@var{dst} x8:@var{_} f24:@var{src}
  511. Copy a value from one local slot to another, but addressing slots
  512. relative to the @code{fp} instead of the @code{sp}. This is used when
  513. shuffling values into place after multiple-value returns.
  514. @end deftypefn
  515. @deftypefn Instruction {} make-closure s24:@var{dst} l32:@var{offset} x8:@var{_} c24:@var{nfree}
  516. Make a new closure, and write it to @var{dst}. The code for the closure
  517. will be found at @var{offset} words from the current @code{ip}.
  518. @var{offset} is a signed 32-bit integer. Space for @var{nfree} free
  519. variables will be allocated.
  520. The size of a closure is currently two words, plus one word per free
  521. variable.
  522. @end deftypefn
  523. @deftypefn Instruction {} free-ref s12:@var{dst} s12:@var{src} x8:@var{_} c24:@var{idx}
  524. Load free variable @var{idx} from the closure @var{src} into local slot
  525. @var{dst}.
  526. @end deftypefn
  527. @deftypefn Instruction {} free-set! s12:@var{dst} s12:@var{src} x8:@var{_} c24:@var{idx}
  528. Set free variable @var{idx} from the closure @var{dst} to @var{src}.
  529. This instruction is usually used when initializing a closure's free
  530. variables, but not to mutate free variables, as variables that are
  531. assigned are boxed.
  532. @end deftypefn
  533. Recall that variables that are assigned are usually allocated in boxes,
  534. so that continuations and closures can capture their identity and not
  535. their value at one point in time. Variables are also used in the
  536. implementation of top-level bindings; see the next section for more
  537. information.
  538. @deftypefn Instruction {} box s12:@var{dst} s12:@var{src}
  539. Create a new variable holding @var{src}, and place it in @var{dst}.
  540. @end deftypefn
  541. @deftypefn Instruction {} box-ref s12:@var{dst} s12:@var{src}
  542. Unpack the variable at @var{src} into @var{dst}, asserting that the
  543. variable is actually bound.
  544. @end deftypefn
  545. @deftypefn Instruction {} box-set! s12:@var{dst} s12:@var{src}
  546. Set the contents of the variable at @var{dst} to @var{set}.
  547. @end deftypefn
  548. @node Top-Level Environment Instructions
  549. @subsubsection Top-Level Environment Instructions
  550. These instructions access values in the top-level environment: bindings
  551. that were not lexically apparent at the time that the code in question
  552. was compiled.
  553. The location in which a toplevel binding is stored can be looked up once
  554. and cached for later. The binding itself may change over time, but its
  555. location will stay constant.
  556. @deftypefn Instruction {} current-module s24:@var{dst}
  557. Store the current module in @var{dst}.
  558. @end deftypefn
  559. @deftypefn Instruction {} resolve s24:@var{dst} b1:@var{bound?} x7:@var{_} s24:@var{sym}
  560. Resolve @var{sym} in the current module, and place the resulting
  561. variable in @var{dst}. An error will be signalled if no variable is
  562. found. If @var{bound?} is true, an error will be signalled if the
  563. variable is unbound.
  564. @end deftypefn
  565. @deftypefn Instruction {} define! s12:@var{sym} s12:@var{val}
  566. Look up a binding for @var{sym} in the current module, creating it if
  567. necessary. Set its value to @var{val}.
  568. @end deftypefn
  569. @deftypefn Instruction {} toplevel-box s24:@var{dst} r32:@var{var-offset} r32:@var{mod-offset} n32:@var{sym-offset} b1:@var{bound?} x31:@var{_}
  570. Load a value. The value will be fetched from memory, @var{var-offset}
  571. 32-bit words away from the current instruction pointer.
  572. @var{var-offset} is a signed value. Up to here, @code{toplevel-box} is
  573. like @code{static-ref}.
  574. Then, if the loaded value is a variable, it is placed in @var{dst}, and
  575. control flow continues.
  576. Otherwise, we have to resolve the variable. In that case we load the
  577. module from @var{mod-offset}, just as we loaded the variable. Usually
  578. the module gets set when the closure is created. @var{sym-offset}
  579. specifies the name, as an offset to a symbol.
  580. We use the module and the symbol to resolve the variable, placing it in
  581. @var{dst}, and caching the resolved variable so that we will hit the
  582. cache next time. If @var{bound?} is true, an error will be signalled if
  583. the variable is unbound.
  584. @end deftypefn
  585. @deftypefn Instruction {} module-box s24:@var{dst} r32:@var{var-offset} n32:@var{mod-offset} n32:@var{sym-offset} b1:@var{bound?} x31:@var{_}
  586. Like @code{toplevel-box}, except @var{mod-offset} points at a module
  587. identifier instead of the module itself. A module identifier is a
  588. module name, as a list, prefixed by a boolean. If the prefix is true,
  589. then the variable is resolved relative to the module's public interface
  590. instead of its private interface.
  591. @end deftypefn
  592. @node Procedure Call and Return Instructions
  593. @subsubsection Procedure Call and Return Instructions
  594. As described earlier (@pxref{Stack Layout}), Guile's calling convention
  595. is that arguments are passed and values returned on the stack.
  596. For calls, both in tail position and in non-tail position, we require
  597. that the procedure and the arguments already be shuffled into place
  598. befor the call instruction. ``Into place'' for a tail call means that
  599. the procedure should be in slot 0, relative to the @code{fp}, and the
  600. arguments should follow. For a non-tail call, if the procedure is in
  601. @code{fp}-relative slot @var{n}, the arguments should follow from slot
  602. @var{n}+1, and there should be two free slots at @var{n}-1 and @var{n}-2
  603. in which to save the @code{ip} and @code{fp}.
  604. Returning values is similar. Multiple-value returns should have values
  605. already shuffled down to start from @code{fp}-relative slot 1 before
  606. emitting @code{return-values}. We start from slot 1 instead of slot 0
  607. to make tail calls to @code{values} trivial.
  608. In both calls and returns, the @code{sp} is used to indicate to the
  609. callee or caller the number of arguments or return values, respectively.
  610. After receiving return values, it is the caller's responsibility to
  611. @dfn{restore the frame} by resetting the @code{sp} to its former value.
  612. @deftypefn Instruction {} call f24:@var{proc} x8:@var{_} c24:@var{nlocals}
  613. Call a procedure. @var{proc} is the local corresponding to a procedure.
  614. The two values below @var{proc} will be overwritten by the saved call
  615. frame data. The new frame will have space for @var{nlocals} locals: one
  616. for the procedure, and the rest for the arguments which should already
  617. have been pushed on.
  618. When the call returns, execution proceeds with the next instruction.
  619. There may be any number of values on the return stack; the precise
  620. number can be had by subtracting the address of @var{proc} from the
  621. post-call @code{sp}.
  622. @end deftypefn
  623. @deftypefn Instruction {} call-label f24:@var{proc} x8:@var{_} c24:@var{nlocals} l32:@var{label}
  624. Call a procedure in the same compilation unit.
  625. This instruction is just like @code{call}, except that instead of
  626. dereferencing @var{proc} to find the call target, the call target is
  627. known to be at @var{label}, a signed 32-bit offset in 32-bit units from
  628. the current @code{ip}. Since @var{proc} is not dereferenced, it may be
  629. some other representation of the closure.
  630. @end deftypefn
  631. @deftypefn Instruction {} tail-call c24:@var{nlocals}
  632. Tail-call a procedure. Requires that the procedure and all of the
  633. arguments have already been shuffled into position. Will reset the
  634. frame to @var{nlocals}.
  635. @end deftypefn
  636. @deftypefn Instruction {} tail-call-label c24:@var{nlocals} l32:@var{label}
  637. Tail-call a known procedure. As @code{call} is to @code{call-label},
  638. @code{tail-call} is to @code{tail-call-label}.
  639. @end deftypefn
  640. @deftypefn Instruction {} tail-call/shuffle f24:@var{from}
  641. Tail-call a procedure. The procedure should already be set to slot 0.
  642. The rest of the args are taken from the frame, starting at @var{from},
  643. shuffled down to start at slot 0. This is part of the implementation of
  644. the @code{call-with-values} builtin.
  645. @end deftypefn
  646. @deftypefn Instruction {} receive f12:@var{dst} f12:@var{proc} x8:@var{_} c24:@var{nlocals}
  647. Receive a single return value from a call whose procedure was in
  648. @var{proc}, asserting that the call actually returned at least one
  649. value. Afterwards, resets the frame to @var{nlocals} locals.
  650. @end deftypefn
  651. @deftypefn Instruction {} receive-values f24:@var{proc} b1:@var{allow-extra?} x7:@var{_} c24:@var{nvalues}
  652. Receive a return of multiple values from a call whose procedure was in
  653. @var{proc}. If fewer than @var{nvalues} values were returned, signal an
  654. error. Unless @var{allow-extra?} is true, require that the number of
  655. return values equals @var{nvalues} exactly. After @code{receive-values}
  656. has run, the values can be copied down via @code{mov}, or used in place.
  657. @end deftypefn
  658. @deftypefn Instruction {} return-values c24:@var{nlocals}
  659. Return a number of values from a call frame. This opcode corresponds to
  660. an application of @code{values} in tail position. As with tail calls,
  661. we expect that the values have already been shuffled down to a
  662. contiguous array starting at slot 1. If @var{nlocals} is nonzero, reset
  663. the frame to hold that number of locals. Note that a frame reset to 1
  664. local returns 0 values.
  665. @end deftypefn
  666. @deftypefn Instruction {} call/cc x24:@var{_}
  667. Capture the current continuation, and tail-apply the procedure in local
  668. slot 1 to it. This instruction is part of the implementation of
  669. @code{call/cc}, and is not generated by the compiler.
  670. @end deftypefn
  671. @node Function Prologue Instructions
  672. @subsubsection Function Prologue Instructions
  673. A function call in Guile is very cheap: the VM simply hands control to
  674. the procedure. The procedure itself is responsible for asserting that it
  675. has been passed an appropriate number of arguments. This strategy allows
  676. arbitrarily complex argument parsing idioms to be developed, without
  677. harming the common case.
  678. For example, only calls to keyword-argument procedures ``pay'' for the
  679. cost of parsing keyword arguments. (At the time of this writing, calling
  680. procedures with keyword arguments is typically two to four times as
  681. costly as calling procedures with a fixed set of arguments.)
  682. @deftypefn Instruction {} assert-nargs-ee c24:@var{expected}
  683. @deftypefnx Instruction {} assert-nargs-ge c24:@var{expected}
  684. @deftypefnx Instruction {} assert-nargs-le c24:@var{expected}
  685. If the number of actual arguments is not @code{==}, @code{>=}, or
  686. @code{<=} @var{expected}, respectively, signal an error.
  687. The number of arguments is determined by subtracting the stack pointer
  688. from the frame pointer (@code{fp - sp}). @xref{Stack Layout}, for more
  689. details on stack frames. Note that @var{expected} includes the
  690. procedure itself.
  691. @end deftypefn
  692. @deftypefn Instruction {} br-if-nargs-ne c24:@var{expected} x8:@var{_} l24:@var{offset}
  693. @deftypefnx Instruction {} br-if-nargs-lt c24:@var{expected} x8:@var{_} l24:@var{offset}
  694. @deftypefnx Instruction {} br-if-nargs-gt c24:@var{expected} x8:@var{_} l24:@var{offset}
  695. If the number of actual arguments is not equal, less than, or greater
  696. than @var{expected}, respectively, add @var{offset}, a signed 24-bit
  697. number, to the current instruction pointer. Note that @var{expected}
  698. includes the procedure itself.
  699. These instructions are used to implement multiple arities, as in
  700. @code{case-lambda}. @xref{Case-lambda}, for more information.
  701. @end deftypefn
  702. @deftypefn Instruction {} alloc-frame c24:@var{nlocals}
  703. Ensure that there is space on the stack for @var{nlocals} local
  704. variables, setting them all to @code{SCM_UNDEFINED}, except those values
  705. that are already on the stack.
  706. @end deftypefn
  707. @deftypefn Instruction {} reset-frame c24:@var{nlocals}
  708. Like @code{alloc-frame}, but doesn't check that the stack is big enough,
  709. and doesn't initialize values to @code{SCM_UNDEFINED}. Used to reset
  710. the frame size to something less than the size that was previously set
  711. via alloc-frame.
  712. @end deftypefn
  713. @deftypefn Instruction {} assert-nargs-ee/locals c12:@var{expected} c12:@var{nlocals}
  714. Equivalent to a sequence of @code{assert-nargs-ee} and
  715. @code{reserve-locals}. The number of locals reserved is @var{expected}
  716. + @var{nlocals}.
  717. @end deftypefn
  718. @deftypefn Instruction {} br-if-npos-gt c24:@var{nreq} x8:@var{_} c24:@var{npos} x8:@var{_} l24:@var{offset}
  719. Find the first positional argument after @var{nreq}. If it is greater
  720. than @var{npos}, jump to @var{offset}.
  721. This instruction is only emitted for functions with multiple clauses,
  722. and an earlier clause has keywords and no rest arguments.
  723. @xref{Case-lambda}, for more on how @code{case-lambda} chooses the
  724. clause to apply.
  725. @end deftypefn
  726. @deftypefn Instruction {} bind-kwargs c24:@var{nreq} c8:@var{flags} c24:@var{nreq-and-opt} x8:@var{_} c24:@var{ntotal} n32:@var{kw-offset}
  727. @var{flags} is a bitfield, whose lowest bit is @var{allow-other-keys},
  728. second bit is @var{has-rest}, and whose following six bits are unused.
  729. Find the last positional argument, and shuffle all the rest above
  730. @var{ntotal}. Initialize the intervening locals to
  731. @code{SCM_UNDEFINED}. Then load the constant at @var{kw-offset} words
  732. from the current @var{ip}, and use it and the @var{allow-other-keys}
  733. flag to bind keyword arguments. If @var{has-rest}, collect all shuffled
  734. arguments into a list, and store it in @var{nreq-and-opt}. Finally,
  735. clear the arguments that we shuffled up.
  736. The parsing is driven by a keyword arguments association list, looked up
  737. using @var{kw-offset}. The alist is a list of pairs of the form
  738. @code{(@var{kw} . @var{index})}, mapping keyword arguments to their
  739. local slot indices. Unless @code{allow-other-keys} is set, the parser
  740. will signal an error if an unknown key is found.
  741. A macro-mega-instruction.
  742. @end deftypefn
  743. @deftypefn Instruction {} bind-rest f24:@var{dst}
  744. Collect any arguments at or above @var{dst} into a list, and store that
  745. list at @var{dst}.
  746. @end deftypefn
  747. @node Trampoline Instructions
  748. @subsubsection Trampoline Instructions
  749. Though most applicable objects in Guile are procedures implemented in
  750. bytecode, not all are. There are primitives, continuations, and other
  751. procedure-like objects that have their own calling convention. Instead
  752. of adding special cases to the @code{call} instruction, Guile wraps
  753. these other applicable objects in VM trampoline procedures, then
  754. provides special support for these objects in bytecode.
  755. Trampoline procedures are typically generated by Guile at runtime, for
  756. example in response to a call to @code{scm_c_make_gsubr}. As such, a
  757. compiler probably shouldn't emit code with these instructions. However,
  758. it's still interesting to know how these things work, so we document
  759. these trampoline instructions here.
  760. @deftypefn Instruction {} subr-call x24:@var{_}
  761. Call a subr, passing all locals in this frame as arguments. Return from
  762. the calling frame.
  763. @end deftypefn
  764. @deftypefn Instruction {} foreign-call c12:@var{cif-idx} c12:@var{ptr-idx}
  765. Call a foreign function. Fetch the @var{cif} and foreign pointer from
  766. @var{cif-idx} and @var{ptr-idx}, both free variables. Return from the calling
  767. frame. Arguments are taken from the stack.
  768. @end deftypefn
  769. @deftypefn Instruction {} continuation-call c24:@var{contregs}
  770. Return to a continuation, nonlocally. The arguments to the continuation
  771. are taken from the stack. @var{contregs} is a free variable containing
  772. the reified continuation.
  773. @end deftypefn
  774. @deftypefn Instruction {} compose-continuation c24:@var{cont}
  775. Compose a partial continution with the current continuation. The
  776. arguments to the continuation are taken from the stack. @var{cont} is a
  777. free variable containing the reified continuation.
  778. @end deftypefn
  779. @deftypefn Instruction {} tail-apply x24:@var{_}
  780. Tail-apply the procedure in local slot 0 to the rest of the arguments.
  781. This instruction is part of the implementation of @code{apply}, and is
  782. not generated by the compiler.
  783. @end deftypefn
  784. @deftypefn Instruction {} builtin-ref s12:@var{dst} c12:@var{idx}
  785. Load a builtin stub by index into @var{dst}.
  786. @end deftypefn
  787. @deftypefn Instruction {} apply-non-program x24:@var{_}
  788. An instruction used only by a special trampoline that the VM uses to
  789. apply non-programs. Using that trampoline allows profilers and
  790. backtrace utilities to avoid seeing the instruction pointer from the
  791. calling frame.
  792. @end deftypefn
  793. @node Branch Instructions
  794. @subsubsection Branch Instructions
  795. All offsets to branch instructions are 24-bit signed numbers, which
  796. count 32-bit units. This gives Guile effectively a 26-bit address range
  797. for relative jumps.
  798. @deftypefn Instruction {} br l24:@var{offset}
  799. Add @var{offset} to the current instruction pointer.
  800. @end deftypefn
  801. All the conditional branch instructions described below have an
  802. @var{invert} parameter, which if true reverses the test:
  803. @code{br-if-true} becomes @code{br-if-false}, and so on.
  804. @deftypefn Instruction {} br-if-true s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  805. If the value in @var{test} is true for the purposes of Scheme, add
  806. @var{offset} to the current instruction pointer.
  807. @end deftypefn
  808. @deftypefn Instruction {} br-if-null s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  809. If the value in @var{test} is the end-of-list or Lisp nil, add
  810. @var{offset} to the current instruction pointer.
  811. @end deftypefn
  812. @deftypefn Instruction {} br-if-nil s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  813. If the value in @var{test} is false to Lisp, add @var{offset} to the
  814. current instruction pointer.
  815. @end deftypefn
  816. @deftypefn Instruction {} br-if-pair s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  817. If the value in @var{test} is a pair, add @var{offset} to the current
  818. instruction pointer.
  819. @end deftypefn
  820. @deftypefn Instruction {} br-if-struct s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  821. If the value in @var{test} is a struct, add @var{offset} number to the
  822. current instruction pointer.
  823. @end deftypefn
  824. @deftypefn Instruction {} br-if-char s24:@var{test} b1:@var{invert} x7:@var{_} l24:@var{offset}
  825. If the value in @var{test} is a char, add @var{offset} to the current
  826. instruction pointer.
  827. @end deftypefn
  828. @deftypefn Instruction {} br-if-tc7 s24:@var{test} b1:@var{invert} u7:@var{tc7} l24:@var{offset}
  829. If the value in @var{test} has the TC7 given in the second word, add
  830. @var{offset} to the current instruction pointer. TC7 codes are part of
  831. the way Guile represents non-immediate objects, and are deep wizardry.
  832. See @code{libguile/tags.h} for all the details.
  833. @end deftypefn
  834. @deftypefn Instruction {} br-if-eq s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  835. @deftypefnx Instruction {} br-if-eqv s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  836. If the value in @var{a} is @code{eq?} or @code{eqv?} to the value in
  837. @var{b}, respectively, add @var{offset} to the current instruction
  838. pointer.
  839. @end deftypefn
  840. @deftypefn Instruction {} br-if-= s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  841. @deftypefnx Instruction {} br-if-< s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  842. @deftypefnx Instruction {} br-if-<= s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  843. If the value in @var{a} is @code{=}, @code{<}, or @code{<=} to the value
  844. in @var{b}, respectively, add @var{offset} to the current instruction
  845. pointer.
  846. @end deftypefn
  847. @deftypefn Instruction {} br-if-logtest s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  848. If the bitwise intersection of the integers in @var{a} and @var{b} is
  849. nonzero, add @var{offset} to the current instruction pointer.
  850. @end deftypefn
  851. @node Constant Instructions
  852. @subsubsection Constant Instructions
  853. The following instructions load literal data into a program. There are
  854. two kinds.
  855. The first set of instructions loads immediate values. These
  856. instructions encode the immediate directly into the instruction stream.
  857. @deftypefn Instruction {} make-short-immediate s8:@var{dst} i16:@var{low-bits}
  858. Make an immediate whose low bits are @var{low-bits}, and whose top bits are
  859. 0.
  860. @end deftypefn
  861. @deftypefn Instruction {} make-long-immediate s24:@var{dst} i32:@var{low-bits}
  862. Make an immediate whose low bits are @var{low-bits}, and whose top bits are
  863. 0.
  864. @end deftypefn
  865. @deftypefn Instruction {} make-long-long-immediate s24:@var{dst} a32:@var{high-bits} b32:@var{low-bits}
  866. Make an immediate with @var{high-bits} and @var{low-bits}.
  867. @end deftypefn
  868. Non-immediate constant literals are referenced either directly or
  869. indirectly. For example, Guile knows at compile-time what the layout of
  870. a string will be like, and arranges to embed that object directly in the
  871. compiled image. A reference to a string will use
  872. @code{make-non-immediate} to treat a pointer into the compilation unit
  873. as a @code{SCM} value directly.
  874. @deftypefn Instruction {} make-non-immediate s24:@var{dst} n32:@var{offset}
  875. Load a pointer to statically allocated memory into @var{dst}. The
  876. object's memory is will be found @var{offset} 32-bit words away from the
  877. current instruction pointer. Whether the object is mutable or immutable
  878. depends on where it was allocated by the compiler, and loaded by the
  879. loader.
  880. @end deftypefn
  881. Some objects must be unique across the whole system. This is the case
  882. for symbols and keywords. For these objects, Guile arranges to
  883. initialize them when the compilation unit is loaded, storing them into a
  884. slot in the image. References go indirectly through that slot.
  885. @code{static-ref} is used in this case.
  886. @deftypefn Instruction {} static-ref s24:@var{dst} r32:@var{offset}
  887. Load a @var{scm} value into @var{dst}. The @var{scm} value will be fetched from
  888. memory, @var{offset} 32-bit words away from the current instruction
  889. pointer. @var{offset} is a signed value.
  890. @end deftypefn
  891. Fields of non-immediates may need to be fixed up at load time, because
  892. we do not know in advance at what address they will be loaded. This is
  893. the case, for example, for a pair containing a non-immediate in one of
  894. its fields. @code{static-ref} and @code{static-patch!} are used in
  895. these situations.
  896. @deftypefn Instruction {} static-set! s24:@var{src} lo32:@var{offset}
  897. Store a @var{scm} value into memory, @var{offset} 32-bit words away from the
  898. current instruction pointer. @var{offset} is a signed value.
  899. @end deftypefn
  900. @deftypefn Instruction {} static-patch! x24:@var{_} lo32:@var{dst-offset} l32:@var{src-offset}
  901. Patch a pointer at @var{dst-offset} to point to @var{src-offset}. Both offsets
  902. are signed 32-bit values, indicating a memory address as a number
  903. of 32-bit words away from the current instruction pointer.
  904. @end deftypefn
  905. Many kinds of literals can be loaded with the above instructions, once
  906. the compiler has prepared the statically allocated data. This is the
  907. case for vectors, strings, uniform vectors, pairs, and procedures with
  908. no free variables. Other kinds of data might need special initializers;
  909. those instructions follow.
  910. @deftypefn Instruction {} string->number s12:@var{dst} s12:@var{src}
  911. Parse a string in @var{src} to a number, and store in @var{dst}.
  912. @end deftypefn
  913. @deftypefn Instruction {} string->symbol s12:@var{dst} s12:@var{src}
  914. Parse a string in @var{src} to a symbol, and store in @var{dst}.
  915. @end deftypefn
  916. @deftypefn Instruction {} symbol->keyword s12:@var{dst} s12:@var{src}
  917. Make a keyword from the symbol in @var{src}, and store it in @var{dst}.
  918. @end deftypefn
  919. @deftypefn Instruction {} load-typed-array s24:@var{dst} x8:@var{_} s24:@var{type} x8:@var{_} s24:@var{shape} n32:@var{offset} u32:@var{len}
  920. Load the contiguous typed array located at @var{offset} 32-bit words away
  921. from the instruction pointer, and store into @var{dst}. @var{len} is a byte
  922. length. @var{offset} is signed.
  923. @end deftypefn
  924. @node Dynamic Environment Instructions
  925. @subsubsection Dynamic Environment Instructions
  926. Guile's virtual machine has low-level support for @code{dynamic-wind},
  927. dynamic binding, and composable prompts and aborts.
  928. @deftypefn Instruction {} abort x24:@var{_}
  929. Abort to a prompt handler. The tag is expected in slot 1, and the rest
  930. of the values in the frame are returned to the prompt handler. This
  931. corresponds to a tail application of abort-to-prompt.
  932. If no prompt can be found in the dynamic environment with the given tag,
  933. an error is signalled. Otherwise all arguments are passed to the
  934. prompt's handler, along with the captured continuation, if necessary.
  935. If the prompt's handler can be proven to not reference the captured
  936. continuation, no continuation is allocated. This decision happens
  937. dynamically, at run-time; the general case is that the continuation may
  938. be captured, and thus resumed. A reinstated continuation will have its
  939. arguments pushed on the stack from slot 1, as if from a multiple-value
  940. return, and control resumes in the caller. Thus to the calling
  941. function, a call to @code{abort-to-prompt} looks like any other function
  942. call.
  943. @end deftypefn
  944. @deftypefn Instruction {} prompt s24:@var{tag} b1:@var{escape-only?} x7:@var{_} f24:@var{proc-slot} x8:@var{_} l24:@var{handler-offset}
  945. Push a new prompt on the dynamic stack, with a tag from @var{tag} and a
  946. handler at @var{handler-offset} words from the current @var{ip}.
  947. If an abort is made to this prompt, control will jump to the handler.
  948. The handler will expect a multiple-value return as if from a call with
  949. the procedure at @var{proc-slot}, with the reified partial continuation
  950. as the first argument, followed by the values returned to the handler.
  951. If control returns to the handler, the prompt is already popped off by
  952. the abort mechanism. (Guile's @code{prompt} implements Felleisen's
  953. @dfn{--F--} operator.)
  954. If @var{escape-only?} is nonzero, the prompt will be marked as
  955. escape-only, which allows an abort to this prompt to avoid reifying the
  956. continuation.
  957. @xref{Prompts}, for more information on prompts.
  958. @end deftypefn
  959. @deftypefn Instruction {} wind s12:@var{winder} s12:@var{unwinder}
  960. Push wind and unwind procedures onto the dynamic stack. Note that
  961. neither are actually called; the compiler should emit calls to wind and
  962. unwind for the normal dynamic-wind control flow. Also note that the
  963. compiler should have inserted checks that they wind and unwind procs are
  964. thunks, if it could not prove that to be the case. @xref{Dynamic Wind}.
  965. @end deftypefn
  966. @deftypefn Instruction {} unwind x24:@var{_}
  967. @var{a} normal exit from the dynamic extent of an expression. Pop the top
  968. entry off of the dynamic stack.
  969. @end deftypefn
  970. @deftypefn Instruction {} push-fluid s12:@var{fluid} s12:@var{value}
  971. Dynamically bind @var{value} to @var{fluid} by creating a with-fluids
  972. object and pushing that object on the dynamic stack. @xref{Fluids and
  973. Dynamic States}.
  974. @end deftypefn
  975. @deftypefn Instruction {} pop-fluid x24:@var{_}
  976. Leave the dynamic extent of a @code{with-fluid*} expression, restoring
  977. the fluid to its previous value. @code{push-fluid} should always be
  978. balanced with @code{pop-fluid}.
  979. @end deftypefn
  980. @deftypefn Instruction {} fluid-ref s12:@var{dst} s12:@var{src}
  981. Reference the fluid in @var{src}, and place the value in @var{dst}.
  982. @end deftypefn
  983. @deftypefn Instruction {} fluid-set s12:@var{fluid} s12:@var{val}
  984. Set the value of the fluid in @var{dst} to the value in @var{src}.
  985. @end deftypefn
  986. @deftypefn Instruction {} current-thread s24:@var{dst}
  987. Write the value of the current thread to @var{dst}.
  988. @end deftypefn
  989. @node Miscellaneous Instructions
  990. @subsubsection Miscellaneous Instructions
  991. @deftypefn Instruction {} halt x24:@var{_}
  992. Bring the VM to a halt, returning all the values from the stack. Used
  993. in the ``boot continuation'', which is used when entering the VM from C.
  994. @end deftypefn
  995. @deftypefn Instruction {} push s24:@var{src}
  996. Bump the stack pointer by one word, and fill it with the value from slot
  997. @var{src}. The offset to @var{src} is calculated before the stack
  998. pointer is adjusted.
  999. @end deftypefn
  1000. The @code{push} instruction is used when another instruction is unable
  1001. to address an operand because the operand is encoded with fewer than 24
  1002. bits. In that case, Guile's assembler will transparently emit code that
  1003. temporarily pushes any needed operands onto the stack, emits the
  1004. original instruction to address those now-near variables, then shuffles
  1005. the result (if any) back into place.
  1006. @deftypefn Instruction {} pop s24:@var{dst}
  1007. Pop the stack pointer, storing the value that was there in slot
  1008. @var{dst}. The offset to @var{dst} is calculated after the stack
  1009. pointer is adjusted.
  1010. @end deftypefn
  1011. @deftypefn Instruction {} drop c24:@var{count}
  1012. Pop the stack pointer by @var{count} words, discarding any values that
  1013. were stored there.
  1014. @end deftypefn
  1015. @node Inlined Scheme Instructions
  1016. @subsubsection Inlined Scheme Instructions
  1017. The Scheme compiler can recognize the application of standard Scheme
  1018. procedures. It tries to inline these small operations to avoid the
  1019. overhead of creating new stack frames. This allows the compiler to
  1020. optimize better.
  1021. @deftypefn Instruction {} make-vector s8:@var{dst} s8:@var{length} s8:@var{init}
  1022. Make a vector and write it to @var{dst}. The vector will have space for
  1023. @var{length} slots. They will be filled with the value in slot
  1024. @var{init}.
  1025. @end deftypefn
  1026. @deftypefn Instruction {} make-vector/immediate s8:@var{dst} s8:@var{length} c8:@var{init}
  1027. Make a short vector of known size and write it to @var{dst}. The vector
  1028. will have space for @var{length} slots, an immediate value. They will
  1029. be filled with the value in slot @var{init}.
  1030. @end deftypefn
  1031. @deftypefn Instruction {} vector-length s12:@var{dst} s12:@var{src}
  1032. Store the length of the vector in @var{src} in @var{dst}, as an unboxed
  1033. unsigned 64-bit integer.
  1034. @end deftypefn
  1035. @deftypefn Instruction {} vector-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1036. Fetch the item at position @var{idx} in the vector in @var{src}, and
  1037. store it in @var{dst}. The @var{idx} value should be an unboxed
  1038. unsigned 64-bit integer.
  1039. @end deftypefn
  1040. @deftypefn Instruction {} vector-ref/immediate s8:@var{dst} s8:@var{src} c8:@var{idx}
  1041. Fill @var{dst} with the item @var{idx} elements into the vector at
  1042. @var{src}. Useful for building data types using vectors.
  1043. @end deftypefn
  1044. @deftypefn Instruction {} vector-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1045. Store @var{src} into the vector @var{dst} at index @var{idx}. The
  1046. @var{idx} value should be an unboxed unsigned 64-bit integer.
  1047. @end deftypefn
  1048. @deftypefn Instruction {} vector-set!/immediate s8:@var{dst} c8:@var{idx} s8:@var{src}
  1049. Store @var{src} into the vector @var{dst} at index @var{idx}. Here
  1050. @var{idx} is an immediate value.
  1051. @end deftypefn
  1052. @deftypefn Instruction {} struct-vtable s12:@var{dst} s12:@var{src}
  1053. Store the vtable of @var{src} into @var{dst}.
  1054. @end deftypefn
  1055. @deftypefn Instruction {} allocate-struct s8:@var{dst} s8:@var{vtable} s8:@var{nfields}
  1056. Allocate a new struct with @var{vtable}, and place it in @var{dst}. The
  1057. struct will be constructed with space for @var{nfields} fields, which
  1058. should correspond to the field count of the @var{vtable}. The @var{idx}
  1059. value should be an unboxed unsigned 64-bit integer.
  1060. @end deftypefn
  1061. @deftypefn Instruction {} struct-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1062. Fetch the item at slot @var{idx} in the struct in @var{src}, and store
  1063. it in @var{dst}. The @var{idx} value should be an unboxed unsigned
  1064. 64-bit integer.
  1065. @end deftypefn
  1066. @deftypefn Instruction {} struct-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1067. Store @var{src} into the struct @var{dst} at slot @var{idx}. The
  1068. @var{idx} value should be an unboxed unsigned 64-bit integer.
  1069. @end deftypefn
  1070. @deftypefn Instruction {} allocate-struct/immediate s8:@var{dst} s8:@var{vtable} c8:@var{nfields}
  1071. @deftypefnx Instruction {} struct-ref/immediate s8:@var{dst} s8:@var{src} c8:@var{idx}
  1072. @deftypefnx Instruction {} struct-set!/immediate s8:@var{dst} c8:@var{idx} s8:@var{src}
  1073. Variants of the struct instructions, but in which the @var{nfields} or
  1074. @var{idx} fields are immediate values.
  1075. @end deftypefn
  1076. @deftypefn Instruction {} class-of s12:@var{dst} s12:@var{type}
  1077. Store the vtable of @var{src} into @var{dst}.
  1078. @end deftypefn
  1079. @deftypefn Instruction {} make-array s24:@var{dst} x8:@var{_} s24:@var{type} x8:@var{_} s24:@var{fill} x8:@var{_} s24:@var{bounds}
  1080. Make a new array with @var{type}, @var{fill}, and @var{bounds}, storing it in @var{dst}.
  1081. @end deftypefn
  1082. @deftypefn Instruction {} string-length s12:@var{dst} s12:@var{src}
  1083. Store the length of the string in @var{src} in @var{dst}, as an unboxed
  1084. unsigned 64-bit integer.
  1085. @end deftypefn
  1086. @deftypefn Instruction {} string-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1087. Fetch the character at position @var{idx} in the string in @var{src},
  1088. and store it in @var{dst}. The @var{idx} value should be an unboxed
  1089. unsigned 64-bit integer.
  1090. @end deftypefn
  1091. @deftypefn Instruction {} cons s8:@var{dst} s8:@var{car} s8:@var{cdr}
  1092. Cons @var{car} and @var{cdr}, and store the result in @var{dst}.
  1093. @end deftypefn
  1094. @deftypefn Instruction {} car s12:@var{dst} s12:@var{src}
  1095. Place the car of @var{src} in @var{dst}.
  1096. @end deftypefn
  1097. @deftypefn Instruction {} cdr s12:@var{dst} s12:@var{src}
  1098. Place the cdr of @var{src} in @var{dst}.
  1099. @end deftypefn
  1100. @deftypefn Instruction {} set-car! s12:@var{pair} s12:@var{car}
  1101. Set the car of @var{dst} to @var{src}.
  1102. @end deftypefn
  1103. @deftypefn Instruction {} set-cdr! s12:@var{pair} s12:@var{cdr}
  1104. Set the cdr of @var{dst} to @var{src}.
  1105. @end deftypefn
  1106. Note that @code{caddr} and friends compile to a series of @code{car}
  1107. and @code{cdr} instructions.
  1108. @node Inlined Mathematical Instructions
  1109. @subsubsection Inlined Mathematical Instructions
  1110. Inlining mathematical operations has the obvious advantage of handling
  1111. fixnums without function calls or allocations. The trick, of course,
  1112. is knowing when the result of an operation will be a fixnum, and there
  1113. might be a couple bugs here.
  1114. More instructions could be added here over time.
  1115. All of these operations place their result in their first operand,
  1116. @var{dst}.
  1117. @deftypefn Instruction {} add s8:@var{dst} s8:@var{a} s8:@var{b}
  1118. Add @var{a} to @var{b}.
  1119. @end deftypefn
  1120. @deftypefn Instruction {} add/immediate s8:@var{dst} s8:@var{src} c8:@var{imm}
  1121. Add the unsigned integer @var{imm} to the value in @var{src}.
  1122. @end deftypefn
  1123. @deftypefn Instruction {} sub s8:@var{dst} s8:@var{a} s8:@var{b}
  1124. Subtract @var{b} from @var{a}.
  1125. @end deftypefn
  1126. @deftypefn Instruction {} sub/immediate s8:@var{dst} s8:@var{src} s8:@var{imm}
  1127. Subtract the unsigned integer @var{imm} from the value in @var{src}.
  1128. @end deftypefn
  1129. @deftypefn Instruction {} mul s8:@var{dst} s8:@var{a} s8:@var{b}
  1130. Multiply @var{a} and @var{b}.
  1131. @end deftypefn
  1132. @deftypefn Instruction {} div s8:@var{dst} s8:@var{a} s8:@var{b}
  1133. Divide @var{a} by @var{b}.
  1134. @end deftypefn
  1135. @deftypefn Instruction {} quo s8:@var{dst} s8:@var{a} s8:@var{b}
  1136. Divide @var{a} by @var{b}.
  1137. @end deftypefn
  1138. @deftypefn Instruction {} rem s8:@var{dst} s8:@var{a} s8:@var{b}
  1139. Divide @var{a} by @var{b}.
  1140. @end deftypefn
  1141. @deftypefn Instruction {} mod s8:@var{dst} s8:@var{a} s8:@var{b}
  1142. Compute the modulo of @var{a} by @var{b}.
  1143. @end deftypefn
  1144. @deftypefn Instruction {} ash s8:@var{dst} s8:@var{a} s8:@var{b}
  1145. Shift @var{a} arithmetically by @var{b} bits.
  1146. @end deftypefn
  1147. @deftypefn Instruction {} logand s8:@var{dst} s8:@var{a} s8:@var{b}
  1148. Compute the bitwise @code{and} of @var{a} and @var{b}.
  1149. @end deftypefn
  1150. @deftypefn Instruction {} logior s8:@var{dst} s8:@var{a} s8:@var{b}
  1151. Compute the bitwise inclusive @code{or} of @var{a} with @var{b}.
  1152. @end deftypefn
  1153. @deftypefn Instruction {} logxor s8:@var{dst} s8:@var{a} s8:@var{b}
  1154. Compute the bitwise exclusive @code{or} of @var{a} with @var{b}.
  1155. @end deftypefn
  1156. @deftypefn Instruction {} logsub s8:@var{dst} s8:@var{a} s8:@var{b}
  1157. Place the bitwise @code{and} of @var{a} and the bitwise @code{not} of
  1158. @var{b} into @var{dst}.
  1159. @end deftypefn
  1160. @node Inlined Bytevector Instructions
  1161. @subsubsection Inlined Bytevector Instructions
  1162. Bytevector operations correspond closely to what the current hardware
  1163. can do, so it makes sense to inline them to VM instructions, providing
  1164. a clear path for eventual native compilation. Without this, Scheme
  1165. programs would need other primitives for accessing raw bytes -- but
  1166. these primitives are as good as any.
  1167. @deftypefn Instruction {} bv-length s12:@var{dst} s12:@var{src}
  1168. Store the length of the bytevector in @var{src} in @var{dst}, as an
  1169. unboxed unsigned 64-bit integer.
  1170. @end deftypefn
  1171. @deftypefn Instruction {} bv-u8-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1172. @deftypefnx Instruction {} bv-s8-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1173. @deftypefnx Instruction {} bv-u16-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1174. @deftypefnx Instruction {} bv-s16-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1175. @deftypefnx Instruction {} bv-u32-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1176. @deftypefnx Instruction {} bv-s32-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1177. @deftypefnx Instruction {} bv-u64-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1178. @deftypefnx Instruction {} bv-s64-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1179. @deftypefnx Instruction {} bv-f32-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1180. @deftypefnx Instruction {} bv-f64-ref s8:@var{dst} s8:@var{src} s8:@var{idx}
  1181. Fetch the item at byte offset @var{idx} in the bytevector @var{src}, and
  1182. store it in @var{dst}. All accesses use native endianness.
  1183. The @var{idx} value should be an unboxed unsigned 64-bit integer.
  1184. The results are all written to the stack as unboxed values, either as
  1185. signed 64-bit integers, unsigned 64-bit integers, or IEEE double
  1186. floating point numbers.
  1187. @end deftypefn
  1188. @deftypefn Instruction {} bv-u8-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1189. @deftypefnx Instruction {} bv-s8-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1190. @deftypefnx Instruction {} bv-u16-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1191. @deftypefnx Instruction {} bv-s16-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1192. @deftypefnx Instruction {} bv-u32-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1193. @deftypefnx Instruction {} bv-s32-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1194. @deftypefnx Instruction {} bv-u64-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1195. @deftypefnx Instruction {} bv-s64-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1196. @deftypefnx Instruction {} bv-f32-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1197. @deftypefnx Instruction {} bv-f64-set! s8:@var{dst} s8:@var{idx} s8:@var{src}
  1198. Store @var{src} into the bytevector @var{dst} at byte offset @var{idx}.
  1199. Multibyte values are written using native endianness.
  1200. The @var{idx} value should be an unboxed unsigned 64-bit integer.
  1201. The @var{src} values are all unboxed, either as signed 64-bit integers,
  1202. unsigned 64-bit integers, or IEEE double floating point numbers.
  1203. @end deftypefn
  1204. @node Unboxed Integer Arithmetic
  1205. @subsubsection Unboxed Integer Arithmetic
  1206. Guile supports two kinds of unboxed integers: unsigned 64-bit integers,
  1207. and signed 64-bit integers. Guile prefers unsigned integers, in the
  1208. sense that Guile's compiler supports them better and the virtual machine
  1209. has more operations that work on them. Still, signed integers are
  1210. supported at least to allow @code{bv-s64-ref} and related instructions
  1211. to avoid boxing their values.
  1212. @deftypefn Instruction {} scm->u64 s12:@var{dst} s12:@var{src}
  1213. Unbox the SCM value at @var{src} to a unsigned 64-bit integer, placing
  1214. the result in @var{dst}. If the @var{src} value is not an exact integer
  1215. in the unsigned 64-bit range, signal an error.
  1216. @end deftypefn
  1217. @deftypefn Instruction {} u64->scm s12:@var{dst} s12:@var{src}
  1218. Box the unsigned 64-bit integer at @var{src} to a SCM value and place
  1219. the result in @var{dst}. The result will be a fixnum or a bignum.
  1220. @end deftypefn
  1221. @deftypefn Instruction {} load-u64 s24:@var{dst} au32:@var{high-bits} au32:@var{low-bits}
  1222. Load a 64-bit value formed by joining @var{high-bits} and
  1223. @var{low-bits}, and write it to @var{dst}.
  1224. @end deftypefn
  1225. @deftypefn Instruction {} scm->s64 s12:@var{dst} s12:@var{src}
  1226. @deftypefnx Instruction {} s64->scm s12:@var{dst} s12:@var{src}
  1227. @deftypefnx Instruction {} load-s64 s24:@var{dst} as32:@var{high-bits} as32:@var{low-bits}
  1228. Like @code{scm->u64}, @code{u64->scm}, and @code{load-u64}, but for
  1229. signed 64-bit integers.
  1230. @end deftypefn
  1231. Sometimes the compiler can know that we will only need a subset of the
  1232. bits in an integer. In that case we can sometimes unbox an integer even
  1233. if it might be out of range.
  1234. @deftypefn Instruction {} scm->u64/truncate s12:@var{dst} s12:@var{src}
  1235. Take the SCM value in @var{dst} and @code{logand} it with @code{(1- (ash
  1236. 1 64))}. Place the unboxed result in @var{dst}.
  1237. @end deftypefn
  1238. @deftypefn Instruction {} br-if-u64-= s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1239. @deftypefnx Instruction {} br-if-u64-< s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1240. @deftypefnx Instruction {} br-if-u64-<= s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1241. If the unboxed unsigned 64-bit integer value in @var{a} is @code{=},
  1242. @code{<}, or @code{<=} to the unboxed unsigned 64-bit integer value in
  1243. @var{b}, respectively, add @var{offset} to the current instruction
  1244. pointer.
  1245. @end deftypefn
  1246. @deftypefn Instruction {} br-if-u64-=-scm s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1247. @deftypefnx Instruction {} br-if-u64-<-scm s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1248. @deftypefnx Instruction {} br-if-u64-<=-scm s24:@var{a} x8:@var{_} s24:@var{b} b1:@var{invert} x7:@var{_} l24:@var{offset}
  1249. If the unboxed unsigned 64-bit integer value in @var{a} is @code{=},
  1250. @code{<}, or @code{<=} to the SCM value in @var{b}, respectively, add
  1251. @var{offset} to the current instruction pointer.
  1252. @end deftypefn
  1253. @deftypefn Instruction {} uadd s8:@var{dst} s8:@var{a} s8:@var{b}
  1254. @deftypefnx Instruction {} usub s8:@var{dst} s8:@var{a} s8:@var{b}
  1255. @deftypefnx Instruction {} umul s8:@var{dst} s8:@var{a} s8:@var{b}
  1256. Like @code{add}, @code{sub}, and @code{mul}, except taking
  1257. the operands as unboxed unsigned 64-bit integers, and producing the
  1258. same. The result will be silently truncated to 64 bits.
  1259. @end deftypefn
  1260. @deftypefn Instruction {} uadd/immediate s8:@var{dst} s8:@var{a} c8:@var{b}
  1261. @deftypefnx Instruction {} usub/immediate s8:@var{dst} s8:@var{a} c8:@var{b}
  1262. @deftypefnx Instruction {} umul/immediate s8:@var{dst} s8:@var{a} c8:@var{b}
  1263. Like @code{uadd}, @code{usub}, and @code{umul}, except the second
  1264. operand is an immediate unsigned 8-bit integer.
  1265. @end deftypefn
  1266. @deftypefn Instruction {} ulogand s8:@var{dst} s8:@var{a} s8:@var{b}
  1267. @deftypefnx Instruction {} ulogior s8:@var{dst} s8:@var{a} s8:@var{b}
  1268. @deftypefnx Instruction {} ulogsub s8:@var{dst} s8:@var{a} s8:@var{b}
  1269. Like @code{logand}, @code{logior}, and @code{logsub}, but operating on
  1270. unboxed unsigned 64-bit integers.
  1271. @end deftypefn
  1272. @deftypefn Instruction {} ulsh s8:@var{dst} s8:@var{a} s8:@var{b}
  1273. Shift the unboxed unsigned 64-bit integer in @var{a} left by @var{b}
  1274. bits, also an unboxed unsigned 64-bit integer. Truncate to 64 bits and
  1275. write to @var{dst} as an unboxed value. Only the lower 6 bits of
  1276. @var{b} are used.
  1277. @end deftypefn
  1278. @deftypefn Instruction {} ursh s8:@var{dst} s8:@var{a} s8:@var{b}
  1279. Like @code{ulsh}, but shifting right.
  1280. @end deftypefn
  1281. @deftypefn Instruction {} ulsh/immediate s8:@var{dst} s8:@var{a} c8:@var{b}
  1282. @deftypefnx Instruction {} ursh/immediate s8:@var{dst} s8:@var{a} c8:@var{b}
  1283. Like @code{ulsh} and @code{ursh}, but encoding @code{b} as an immediate
  1284. 8-bit unsigned integer.
  1285. @end deftypefn
  1286. @node Unboxed Floating-Point Arithmetic
  1287. @subsubsection Unboxed Floating-Point Arithmetic
  1288. @deftypefn Instruction {} scm->f64 s12:@var{dst} s12:@var{src}
  1289. Unbox the SCM value at @var{src} to an IEEE double, placing the result
  1290. in @var{dst}. If the @var{src} value is not a real number, signal an
  1291. error.
  1292. @end deftypefn
  1293. @deftypefn Instruction {} f64->scm s12:@var{dst} s12:@var{src}
  1294. Box the IEEE double at @var{src} to a SCM value and place the result in
  1295. @var{dst}.
  1296. @end deftypefn
  1297. @deftypefn Instruction {} load-f64 s24:@var{dst} au32:@var{high-bits} au32:@var{low-bits}
  1298. Load a 64-bit value formed by joining @var{high-bits} and
  1299. @var{low-bits}, and write it to @var{dst}.
  1300. @end deftypefn
  1301. @deftypefn Instruction {} fadd s8:@var{dst} s8:@var{a} s8:@var{b}
  1302. @deftypefnx Instruction {} fsub s8:@var{dst} s8:@var{a} s8:@var{b}
  1303. @deftypefnx Instruction {} fmul s8:@var{dst} s8:@var{a} s8:@var{b}
  1304. @deftypefnx Instruction {} fdiv s8:@var{dst} s8:@var{a} s8:@var{b}
  1305. Like @code{add}, @code{sub}, @code{div}, and @code{mul}, except taking
  1306. the operands as unboxed IEEE double floating-point numbers, and producing
  1307. the same.
  1308. @end deftypefn