vm.texi 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 2008,2009,2010,2011
  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. * Instruction Set::
  25. @end menu
  26. @node Why a VM?
  27. @subsection Why a VM?
  28. @cindex interpreter
  29. For a long time, Guile only had an interpreter. Guile's interpreter
  30. operated directly on the S-expression representation of Scheme source
  31. code.
  32. But while the interpreter was highly optimized and hand-tuned, it still
  33. performs many needless computations during the course of evaluating an
  34. expression. For example, application of a function to arguments
  35. needlessly consed up the arguments in a list. Evaluation of an
  36. expression always had to figure out what the car of the expression is --
  37. a procedure, a memoized form, or something else. All values have to be
  38. allocated on the heap. Et cetera.
  39. The solution to this problem was to compile the higher-level language,
  40. Scheme, into a lower-level language for which all of the checks and
  41. dispatching have already been done---the code is instead stripped to
  42. the bare minimum needed to ``do the job''.
  43. The question becomes then, what low-level language to choose? There
  44. are many options. We could compile to native code directly, but that
  45. poses portability problems for Guile, as it is a highly cross-platform
  46. project.
  47. So we want the performance gains that compilation provides, but we
  48. also want to maintain the portability benefits of a single code path.
  49. The obvious solution is to compile to a virtual machine that is
  50. present on all Guile installations.
  51. The easiest (and most fun) way to depend on a virtual machine is to
  52. implement the virtual machine within Guile itself. This way the
  53. virtual machine provides what Scheme needs (tail calls, multiple
  54. values, @code{call/cc}) and can provide optimized inline instructions
  55. for Guile (@code{cons}, @code{struct-ref}, etc.).
  56. So this is what Guile does. The rest of this section describes that VM
  57. that Guile implements, and the compiled procedures that run on it.
  58. Before moving on, though, we should note that though we spoke of the
  59. interpreter in the past tense, Guile still has an interpreter. The
  60. difference is that before, it was Guile's main evaluator, and so was
  61. implemented in highly optimized C; now, it is actually implemented in
  62. Scheme, and compiled down to VM bytecode, just like any other program.
  63. (There is still a C interpreter around, used to bootstrap the compiler,
  64. but it is not normally used at runtime.)
  65. The upside of implementing the interpreter in Scheme is that we preserve
  66. tail calls and multiple-value handling between interpreted and compiled
  67. code. The downside is that the interpreter in Guile 2.@var{x} is slower
  68. than the interpreter in 1.8. We hope the that the compiler's speed makes
  69. up for the loss!
  70. Also note that this decision to implement a bytecode compiler does not
  71. preclude native compilation. We can compile from bytecode to native
  72. code at runtime, or even do ahead of time compilation. More
  73. possibilities are discussed in @ref{Extending the Compiler}.
  74. @node VM Concepts
  75. @subsection VM Concepts
  76. Compiled code is run by a virtual machine (VM). Each thread has its own
  77. VM. When a compiled procedure is run, Guile looks up the virtual machine
  78. for the current thread and executes the procedure using that VM.
  79. Guile's virtual machine is a stack machine---that is, it has few
  80. registers, and the instructions defined in the VM operate by pushing
  81. and popping values from a stack.
  82. Stack memory is exclusive to the virtual machine that owns it. In
  83. addition to their stacks, virtual machines also have access to the
  84. global memory (modules, global bindings, etc) that is shared among
  85. other parts of Guile, including other VMs.
  86. A VM has generic instructions, such as those to reference local
  87. variables, and instructions designed to support Guile's languages --
  88. mathematical instructions that support the entire numerical tower, an
  89. inlined implementation of @code{cons}, etc.
  90. The registers that a VM has are as follows:
  91. @itemize
  92. @item ip - Instruction pointer
  93. @item sp - Stack pointer
  94. @item fp - Frame pointer
  95. @end itemize
  96. In other architectures, the instruction pointer is sometimes called
  97. the ``program counter'' (pc). This set of registers is pretty typical
  98. for stack machines; their exact meanings in the context of Guile's VM
  99. are described in the next section.
  100. @c wingo: The following is true, but I don't know in what context to
  101. @c describe it. A documentation FIXME.
  102. @c A VM may have one of three engines: reckless, regular, or debugging.
  103. @c Reckless engine is fastest but dangerous. Regular engine is normally
  104. @c fail-safe and reasonably fast. Debugging engine is safest and
  105. @c functional but very slow.
  106. @c (Actually we have just a regular and a debugging engine; normally
  107. @c we use the latter, it's almost as fast as the ``regular'' engine.)
  108. @node Stack Layout
  109. @subsection Stack Layout
  110. While not strictly necessary to understand how to work with the VM, it
  111. is instructive and sometimes entertaining to consider the structure of
  112. the VM stack.
  113. Logically speaking, a VM stack is composed of ``frames''. Each frame
  114. corresponds to the application of one compiled procedure, and contains
  115. storage space for arguments, local variables, intermediate values, and
  116. some bookkeeping information (such as what to do after the frame
  117. computes its value).
  118. While the compiler is free to do whatever it wants to, as long as the
  119. semantics of a computation are preserved, in practice every time you
  120. call a function, a new frame is created. (The notable exception of
  121. course is the tail call case, @pxref{Tail Calls}.)
  122. Within a frame, you have the data associated with the function
  123. application itself, which is of a fixed size, and the stack space for
  124. intermediate values. Sometimes only the former is referred to as the
  125. ``frame'', and the latter is the ``stack'', although all pending
  126. application frames can have some intermediate computations interleaved
  127. on the stack.
  128. The structure of the fixed part of an application frame is as follows:
  129. @example
  130. Stack
  131. | ... |
  132. | Intermed. val. 0 | <- fp + bp->nargs + bp->nlocs = SCM_FRAME_UPPER_ADDRESS (fp)
  133. +==================+
  134. | Local variable 1 |
  135. | Local variable 0 | <- fp + bp->nargs
  136. | Argument 1 |
  137. | Argument 0 | <- fp
  138. | Program | <- fp - 1
  139. +------------------+
  140. | Return address |
  141. | MV return address|
  142. | Dynamic link | <- fp - 4 = SCM_FRAME_DATA_ADDRESS (fp) = SCM_FRAME_LOWER_ADDRESS (fp)
  143. +==================+
  144. | |
  145. @end example
  146. In the above drawing, the stack grows upward. The intermediate values
  147. stored in the application of this frame are stored above
  148. @code{SCM_FRAME_UPPER_ADDRESS (fp)}. @code{bp} refers to the
  149. @code{struct scm_objcode} data associated with the program at
  150. @code{fp - 1}. @code{nargs} and @code{nlocs} are properties of the
  151. compiled procedure, which will be discussed later.
  152. The individual fields of the frame are as follows:
  153. @table @asis
  154. @item Return address
  155. The @code{ip} that was in effect before this program was applied. When
  156. we return from this activation frame, we will jump back to this
  157. @code{ip}.
  158. @item MV return address
  159. The @code{ip} to return to if this application returns multiple
  160. values. For continuations that only accept one value, this value will
  161. be @code{NULL}; for others, it will be an @code{ip} that points to a
  162. multiple-value return address in the calling code. That code will
  163. expect the top value on the stack to be an integer---the number of
  164. values being returned---and that below that integer there are the
  165. values being returned.
  166. @item Dynamic link
  167. This is the @code{fp} in effect before this program was applied. In
  168. effect, this and the return address are the registers that are always
  169. ``saved''. The dynamic link links the current frame to the previous
  170. frame; computing a stack trace involves traversing these frames.
  171. @item Local variable @var{n}
  172. Lambda-local variables that are all allocated as part of the frame.
  173. This makes access to variables very cheap.
  174. @item Argument @var{n}
  175. The calling convention of the VM requires arguments of a function
  176. application to be pushed on the stack, and here they are. References
  177. to arguments dispatch to these locations on the stack.
  178. @item Program
  179. This is the program being applied. For more information on how
  180. programs are implemented, @xref{VM Programs}.
  181. @end table
  182. @node Variables and the VM
  183. @subsection Variables and the VM
  184. Consider the following Scheme code as an example:
  185. @example
  186. (define (foo a)
  187. (lambda (b) (list foo a b)))
  188. @end example
  189. Within the lambda expression, @code{foo} is a top-level variable, @code{a} is a
  190. lexically captured variable, and @code{b} is a local variable.
  191. Another way to refer to @code{a} and @code{b} is to say that @code{a}
  192. is a ``free'' variable, since it is not defined within the lambda, and
  193. @code{b} is a ``bound'' variable. These are the terms used in the
  194. @dfn{lambda calculus}, a mathematical notation for describing
  195. functions. The lambda calculus is useful because it allows one to
  196. prove statements about functions. It is especially good at describing
  197. scope relations, and it is for that reason that we mention it here.
  198. Guile allocates all variables on the stack. When a lexically enclosed
  199. procedure with free variables---a @dfn{closure}---is created, it copies
  200. those variables into its free variable vector. References to free
  201. variables are then redirected through the free variable vector.
  202. If a variable is ever @code{set!}, however, it will need to be
  203. heap-allocated instead of stack-allocated, so that different closures
  204. that capture the same variable can see the same value. Also, this
  205. allows continuations to capture a reference to the variable, instead
  206. of to its value at one point in time. For these reasons, @code{set!}
  207. variables are allocated in ``boxes''---actually, in variable cells.
  208. @xref{Variables}, for more information. References to @code{set!}
  209. variables are indirected through the boxes.
  210. Thus perhaps counterintuitively, what would seem ``closer to the
  211. metal'', viz @code{set!}, actually forces an extra memory allocation
  212. and indirection.
  213. Going back to our example, @code{b} may be allocated on the stack, as
  214. it is never mutated.
  215. @code{a} may also be allocated on the stack, as it too is never
  216. mutated. Within the enclosed lambda, its value will be copied into
  217. (and referenced from) the free variables vector.
  218. @code{foo} is a top-level variable, because @code{foo} is not
  219. lexically bound in this example.
  220. @node VM Programs
  221. @subsection Compiled Procedures are VM Programs
  222. By default, when you enter in expressions at Guile's REPL, they are
  223. first compiled to VM object code, then that VM object code is executed
  224. to produce a value. If the expression evaluates to a procedure, the
  225. result of this process is a compiled procedure.
  226. A compiled procedure is a compound object, consisting of its bytecode,
  227. a reference to any captured lexical variables, an object array, and
  228. some metadata such as the procedure's arity, name, and documentation.
  229. You can pick apart these pieces with the accessors in @code{(system vm
  230. program)}. @xref{Compiled Procedures}, for a full API reference.
  231. @cindex object table
  232. @cindex object array
  233. The object array of a compiled procedure, also known as the
  234. @dfn{object table}, holds all Scheme objects whose values are known
  235. not to change across invocations of the procedure: constant strings,
  236. symbols, etc. The object table of a program is initialized right
  237. before a program is loaded with @code{load-program}.
  238. @xref{Loading Instructions}, for more information.
  239. Variable objects are one such type of constant object: when a global
  240. binding is defined, a variable object is associated to it and that
  241. object will remain constant over time, even if the value bound to it
  242. changes. Therefore, toplevel bindings only need to be looked up once.
  243. Thereafter, references to the corresponding toplevel variables from
  244. within the program are then performed via the @code{toplevel-ref}
  245. instruction, which uses the object vector, and are almost as fast as
  246. local variable references.
  247. We can see how these concepts tie together by disassembling the
  248. @code{foo} function we defined earlier to see what is going on:
  249. @smallexample
  250. scheme@@(guile-user)> (define (foo a) (lambda (b) (list foo a b)))
  251. scheme@@(guile-user)> ,x foo
  252. 0 (assert-nargs-ee/locals 1)
  253. 2 (object-ref 1) ;; #<procedure 8ebec20 at <current input>:0:17 (b)>
  254. 4 (local-ref 0) ;; `a'
  255. 6 (make-closure 0 1)
  256. 9 (return)
  257. ----------------------------------------
  258. Disassembly of #<procedure 8ebec20 at <current input>:0:17 (b)>:
  259. 0 (assert-nargs-ee/locals 1)
  260. 2 (toplevel-ref 1) ;; `foo'
  261. 4 (free-ref 0) ;; (closure variable)
  262. 6 (local-ref 0) ;; `b'
  263. 8 (list 0 3) ;; 3 elements at (unknown file):0:29
  264. 11 (return)
  265. @end smallexample
  266. First there's some prelude, where @code{foo} checks that it was called with only
  267. 1 argument. Then at @code{ip} 2, we load up the compiled lambda. @code{Ip} 4
  268. loads up `a', so that it can be captured into a closure by at @code{ip}
  269. 6---binding code (from the compiled lambda) with data (the free-variable
  270. vector). Finally we return the closure.
  271. The second stanza disassembles the compiled lambda. After the prelude, we note
  272. that toplevel variables are resolved relative to the module that was current
  273. when the procedure was created. This lookup occurs lazily, at the first time the
  274. variable is actually referenced, and the location of the lookup is cached so
  275. that future references are very cheap. @xref{Top-Level Environment Instructions},
  276. for more details.
  277. Then we see a reference to a free variable, corresponding to @code{a}. The
  278. disassembler doesn't have enough information to give a name to that variable, so
  279. it just marks it as being a ``closure variable''. Finally we see the reference
  280. to @code{b}, then the @code{list} opcode, an inline implementation of the
  281. @code{list} scheme routine.
  282. @node Instruction Set
  283. @subsection Instruction Set
  284. There are about 180 instructions in Guile's virtual machine. These
  285. instructions represent atomic units of a program's execution. Ideally,
  286. they perform one task without conditional branches, then dispatch to
  287. the next instruction in the stream.
  288. Instructions themselves are one byte long. Some instructions take
  289. parameters, which follow the instruction byte in the instruction
  290. stream.
  291. Sometimes the compiler can figure out that it is compiling a special
  292. case that can be run more efficiently. So, for example, while Guile
  293. offers a generic test-and-branch instruction, it also offers specific
  294. instructions for special cases, so that the following cases all have
  295. their own test-and-branch instructions:
  296. @example
  297. (if pred then else)
  298. (if (not pred) then else)
  299. (if (null? l) then else)
  300. (if (not (null? l)) then else)
  301. @end example
  302. In addition, some Scheme primitives have their own inline
  303. implementations, e.g.@: @code{cons}, and @code{list}, as we saw in the
  304. previous section.
  305. So Guile's instruction set is a @emph{complete} instruction set, in
  306. that it provides the instructions that are suited to the problem, and
  307. is not concerned with making a minimal, orthogonal set of
  308. instructions. More instructions may be added over time.
  309. @menu
  310. * Lexical Environment Instructions::
  311. * Top-Level Environment Instructions::
  312. * Procedure Call and Return Instructions::
  313. * Function Prologue Instructions::
  314. * Trampoline Instructions::
  315. * Branch Instructions::
  316. * Data Constructor Instructions::
  317. * Loading Instructions::
  318. * Dynamic Environment Instructions::
  319. * Miscellaneous Instructions::
  320. * Inlined Scheme Instructions::
  321. * Inlined Mathematical Instructions::
  322. * Inlined Bytevector Instructions::
  323. @end menu
  324. @node Lexical Environment Instructions
  325. @subsubsection Lexical Environment Instructions
  326. These instructions access and mutate the lexical environment of a
  327. compiled procedure---its free and bound variables.
  328. Some of these instructions have @code{long-} variants, the difference
  329. being that they take 16-bit arguments, encoded in big-endianness,
  330. instead of the normal 8-bit range.
  331. @xref{Stack Layout}, for more information on the format of stack frames.
  332. @deffn Instruction local-ref index
  333. @deffnx Instruction long-local-ref index
  334. Push onto the stack the value of the local variable located at
  335. @var{index} within the current stack frame.
  336. Note that arguments and local variables are all in one block. Thus the
  337. first argument, if any, is at index 0, and local bindings follow the
  338. arguments.
  339. @end deffn
  340. @deffn Instruction local-set index
  341. @deffnx Instruction long-local-set index
  342. Pop the Scheme object located on top of the stack and make it the new
  343. value of the local variable located at @var{index} within the current
  344. stack frame.
  345. @end deffn
  346. @deffn Instruction box index
  347. Pop a value off the stack, and set the @var{index}nth local variable
  348. to a box containing that value. A shortcut for @code{make-variable}
  349. then @code{local-set}, used when binding boxed variables.
  350. @end deffn
  351. @deffn Instruction empty-box index
  352. Set the @var{indext}h local variable to a box containing a variable
  353. whose value is unbound. Used when compiling some @code{letrec}
  354. expressions.
  355. @end deffn
  356. @deffn Instruction local-boxed-ref index
  357. @deffnx Instruction local-boxed-ref index
  358. Get or set the value of the variable located at @var{index} within the
  359. current stack frame. A shortcut for @code{local-ref} then
  360. @code{variable-ref} or @code{variable-set}, respectively.
  361. @end deffn
  362. @deffn Instruction free-ref index
  363. Push the value of the captured variable located at position
  364. @var{index} within the program's vector of captured variables.
  365. @end deffn
  366. @deffn Instruction free-boxed-ref index
  367. @deffnx Instruction free-boxed-set index
  368. Get or set a boxed free variable. A shortcut for @code{free-ref} then
  369. @code{variable-ref} or @code{variable-set}, respectively.
  370. Note that there is no @code{free-set} instruction, as variables that are
  371. @code{set!} must be boxed.
  372. @end deffn
  373. @deffn Instruction make-closure num-free-vars
  374. Pop @var{num-free-vars} values and a program object off the stack in
  375. that order, and push a new program object closing over the given free
  376. variables. @var{num-free-vars} is encoded as a two-byte big-endian
  377. value.
  378. The free variables are stored in an array, inline to the new program
  379. object, in the order that they were on the stack (not the order they are
  380. popped off). The new closure shares state with the original program. At
  381. the time of this writing, the space overhead of closures is 3 words,
  382. plus one word for each free variable.
  383. @end deffn
  384. @deffn Instruction fix-closure index
  385. Fix up the free variables array of the closure stored in the
  386. @var{index}th local variable. @var{index} is a two-byte big-endian
  387. integer.
  388. This instruction will pop as many values from the stack as are in the
  389. corresponding closure's free variables array. The topmost value on the
  390. stack will be stored as the closure's last free variable, with other
  391. values filling in free variable slots in order.
  392. @code{fix-closure} is part of a hack for allocating mutually recursive
  393. procedures. The hack is to store the procedures in their corresponding
  394. local variable slots, with space already allocated for free variables.
  395. Then once they are all in place, this instruction fixes up their
  396. procedures' free variable bindings in place. This allows most
  397. @code{letrec}-bound procedures to be allocated unboxed on the stack.
  398. @end deffn
  399. @deffn Instruction local-bound? index
  400. @deffnx Instruction long-local-bound? index
  401. Push @code{#t} on the stack if the @code{index}th local variable has
  402. been assigned, or @code{#f} otherwise. Mostly useful for handling
  403. optional arguments in procedure prologues.
  404. @end deffn
  405. @node Top-Level Environment Instructions
  406. @subsubsection Top-Level Environment Instructions
  407. These instructions access values in the top-level environment: bindings
  408. that were not lexically apparent at the time that the code in question
  409. was compiled.
  410. The location in which a toplevel binding is stored can be looked up once
  411. and cached for later. The binding itself may change over time, but its
  412. location will stay constant.
  413. Currently only toplevel references within procedures are cached, as only
  414. procedures have a place to cache them, in their object tables.
  415. @deffn Instruction toplevel-ref index
  416. @deffnx Instruction long-toplevel-ref index
  417. Push the value of the toplevel binding whose location is stored in at
  418. position @var{index} in the current procedure's object table. The
  419. @code{long-} variant encodes the index over two bytes.
  420. Initially, a cell in a procedure's object table that is used by
  421. @code{toplevel-ref} is initialized to one of two forms. The normal case
  422. is that the cell holds a symbol, whose binding will be looked up
  423. relative to the module that was current when the current program was
  424. created.
  425. Alternately, the lookup may be performed relative to a particular
  426. module, determined at compile-time (e.g.@: via @code{@@} or
  427. @code{@@@@}). In that case, the cell in the object table holds a list:
  428. @code{(@var{modname} @var{sym} @var{public?})}. The symbol @var{sym}
  429. will be looked up in the module named @var{modname} (a list of
  430. symbols). The lookup will be performed against the module's public
  431. interface, unless @var{public?} is @code{#f}, which it is for example
  432. when compiling @code{@@@@}.
  433. In any case, if the symbol is unbound, an error is signalled.
  434. Otherwise the initial form is replaced with the looked-up variable, an
  435. in-place mutation of the object table. This mechanism provides for
  436. lazy variable resolution, and an important cached fast-path once the
  437. variable has been successfully resolved.
  438. This instruction pushes the value of the variable onto the stack.
  439. @end deffn
  440. @deffn Instruction toplevel-set index
  441. @deffnx Instruction long-toplevel-set index
  442. Pop a value off the stack, and set it as the value of the toplevel
  443. variable stored at @var{index} in the object table. If the variable
  444. has not yet been looked up, we do the lookup as in
  445. @code{toplevel-ref}.
  446. @end deffn
  447. @deffn Instruction define
  448. Pop a symbol and a value from the stack, in that order. Look up its
  449. binding in the current toplevel environment, creating the binding if
  450. necessary. Set the variable to the value.
  451. @end deffn
  452. @deffn Instruction link-now
  453. Pop a value, @var{x}, from the stack. Look up the binding for @var{x},
  454. according to the rules for @code{toplevel-ref}, and push that variable
  455. on the stack. If the lookup fails, an error will be signalled.
  456. This instruction is mostly used when loading programs, because it can
  457. do toplevel variable lookups without an object table.
  458. @end deffn
  459. @deffn Instruction variable-ref
  460. Dereference the variable object which is on top of the stack and
  461. replace it by the value of the variable it represents.
  462. @end deffn
  463. @deffn Instruction variable-set
  464. Pop off two objects from the stack, a variable and a value, and set
  465. the variable to the value.
  466. @end deffn
  467. @deffn Instruction variable-bound?
  468. Pop off the variable object from top of the stack and push @code{#t} if
  469. it is bound, or @code{#f} otherwise. Mostly useful in procedure
  470. prologues for defining default values for boxed optional variables.
  471. @end deffn
  472. @deffn Instruction make-variable
  473. Replace the top object on the stack with a variable containing it.
  474. Used in some circumstances when compiling @code{letrec} expressions.
  475. @end deffn
  476. @node Procedure Call and Return Instructions
  477. @subsubsection Procedure Call and Return Instructions
  478. @c something about the calling convention here?
  479. @deffn Instruction new-frame
  480. Push a new frame on the stack, reserving space for the dynamic link,
  481. return address, and the multiple-values return address. The frame
  482. pointer is not yet updated, because the frame is not yet active -- it
  483. has to be patched by a @code{call} instruction to get the return
  484. address.
  485. @end deffn
  486. @deffn Instruction call nargs
  487. Call the procedure located at @code{sp[-nargs]} with the @var{nargs}
  488. arguments located from @code{sp[-nargs + 1]} to @code{sp[0]}.
  489. This instruction requires that a new frame be pushed on the stack before
  490. the procedure, via @code{new-frame}. @xref{Stack Layout}, for more
  491. information. It patches up that frame with the current @code{ip} as the
  492. return address, then dispatches to the first instruction in the called
  493. procedure, relying on the called procedure to return one value to the
  494. newly-created continuation. Because the new frame pointer will point to
  495. @code{sp[-nargs + 1]}, the arguments don't have to be shuffled around --
  496. they are already in place.
  497. @end deffn
  498. @deffn Instruction tail-call nargs
  499. Transfer control to the procedure located at @code{sp[-nargs]} with the
  500. @var{nargs} arguments located from @code{sp[-nargs + 1]} to
  501. @code{sp[0]}.
  502. Unlike @code{call}, which requires a new frame to be pushed onto the
  503. stack, @code{tail-call} simply shuffles down the procedure and arguments
  504. to the current stack frame. This instruction implements tail calls as
  505. required by RnRS.
  506. @end deffn
  507. @deffn Instruction apply nargs
  508. @deffnx Instruction tail-apply nargs
  509. Like @code{call} and @code{tail-call}, except that the top item on the
  510. stack must be a list. The elements of that list are then pushed on the
  511. stack and treated as additional arguments, replacing the list itself,
  512. then the procedure is invoked as usual.
  513. @end deffn
  514. @deffn Instruction call/nargs
  515. @deffnx Instruction tail-call/nargs
  516. These are like @code{call} and @code{tail-call}, except they take the
  517. number of arguments from the stack instead of the instruction stream.
  518. These instructions are used in the implementation of multiple value
  519. returns, where the actual number of values is pushed on the stack.
  520. @end deffn
  521. @deffn Instruction mv-call nargs offset
  522. Like @code{call}, except that a multiple-value continuation is created
  523. in addition to a single-value continuation.
  524. The offset (a three-byte value) is an offset within the instruction
  525. stream; the multiple-value return address in the new frame (@pxref{Stack
  526. Layout}) will be set to the normal return address plus this offset.
  527. Instructions at that offset will expect the top value of the stack to be
  528. the number of values, and below that values themselves, pushed
  529. separately.
  530. @end deffn
  531. @deffn Instruction return
  532. Free the program's frame, returning the top value from the stack to
  533. the current continuation. (The stack should have exactly one value on
  534. it.)
  535. Specifically, the @code{sp} is decremented to one below the current
  536. @code{fp}, the @code{ip} is reset to the current return address, the
  537. @code{fp} is reset to the value of the current dynamic link, and then
  538. the returned value is pushed on the stack.
  539. @end deffn
  540. @deffn Instruction return/values nvalues
  541. @deffnx Instruction return/nvalues
  542. Return the top @var{nvalues} to the current continuation. In the case of
  543. @code{return/nvalues}, @var{nvalues} itself is first popped from the top
  544. of the stack.
  545. If the current continuation is a multiple-value continuation,
  546. @code{return/values} pushes the number of values on the stack, then
  547. returns as in @code{return}, but to the multiple-value return address.
  548. Otherwise if the current continuation accepts only one value, i.e.@: the
  549. multiple-value return address is @code{NULL}, then we assume the user
  550. only wants one value, and we give them the first one. If there are no
  551. values, an error is signaled.
  552. @end deffn
  553. @deffn Instruction return/values* nvalues
  554. Like a combination of @code{apply} and @code{return/values}, in which
  555. the top value on the stack is interpreted as a list of additional
  556. values. This is an optimization for the common @code{(apply values
  557. ...)} case.
  558. @end deffn
  559. @deffn Instruction truncate-values nbinds nrest
  560. Used in multiple-value continuations, this instruction takes the
  561. values that are on the stack (including the number-of-values marker)
  562. and truncates them for a binding construct.
  563. For example, a call to @code{(receive (x y . z) (foo) ...)} would,
  564. logically speaking, pop off the values returned from @code{(foo)} and
  565. push them as three values, corresponding to @code{x}, @code{y}, and
  566. @code{z}. In that case, @var{nbinds} would be 3, and @var{nrest} would
  567. be 1 (to indicate that one of the bindings was a rest argument).
  568. Signals an error if there is an insufficient number of values.
  569. @end deffn
  570. @deffn Instruction call/cc
  571. @deffnx Instruction tail-call/cc
  572. Capture the current continuation, and then call (or tail-call) the
  573. procedure on the top of the stack, with the continuation as the
  574. argument.
  575. @code{call/cc} does not require a @code{new-frame} to be pushed on the
  576. stack, as @code{call} does, because it needs to capture the stack
  577. before the frame is pushed.
  578. Both the VM continuation and the C continuation are captured.
  579. @end deffn
  580. @node Function Prologue Instructions
  581. @subsubsection Function Prologue Instructions
  582. A function call in Guile is very cheap: the VM simply hands control to
  583. the procedure. The procedure itself is responsible for asserting that it
  584. has been passed an appropriate number of arguments. This strategy allows
  585. arbitrarily complex argument parsing idioms to be developed, without
  586. harming the common case.
  587. For example, only calls to keyword-argument procedures ``pay'' for the
  588. cost of parsing keyword arguments. (At the time of this writing, calling
  589. procedures with keyword arguments is typically two to four times as
  590. costly as calling procedures with a fixed set of arguments.)
  591. @deffn Instruction assert-nargs-ee n
  592. @deffnx Instruction assert-nargs-ge n
  593. Assert that the current procedure has been passed exactly @var{n}
  594. arguments, for the @code{-ee} case, or @var{n} or more arguments, for
  595. the @code{-ge} case. @var{n} is encoded over two bytes.
  596. The number of arguments is determined by subtracting the frame pointer
  597. from the stack pointer (@code{sp - (fp -1)}). @xref{Stack Layout}, for
  598. more details on stack frames.
  599. @end deffn
  600. @deffn Instruction br-if-nargs-ne n offset
  601. @deffnx Instruction br-if-nargs-gt n offset
  602. @deffnx Instruction br-if-nargs-lt n offset
  603. Jump to @var{offset} if the number of arguments is not equal to, greater
  604. than, or less than @var{n}. @var{n} is encoded over two bytes, and
  605. @var{offset} has the normal three-byte encoding.
  606. These instructions are used to implement multiple arities, as in
  607. @code{case-lambda}. @xref{Case-lambda}, for more information.
  608. @end deffn
  609. @deffn Instruction bind-optionals n
  610. If the procedure has been called with fewer than @var{n} arguments, fill
  611. in the remaining arguments with an unbound value (@code{SCM_UNDEFINED}).
  612. @var{n} is encoded over two bytes.
  613. The optionals can be later initialized conditionally via the
  614. @code{local-bound?} instruction.
  615. @end deffn
  616. @deffn Instruction push-rest n
  617. Pop off excess arguments (more than @var{n}), collecting them into a
  618. list, and push that list. Used to bind a rest argument, if the procedure
  619. has no keyword arguments. Procedures with keyword arguments use
  620. @code{bind-rest} instead.
  621. @end deffn
  622. @deffn Instruction bind-rest n idx
  623. Pop off excess arguments (more than @var{n}), collecting them into a
  624. list. The list is then assigned to the @var{idx}th local variable.
  625. @end deffn
  626. @deffn Instruction bind-optionals/shuffle nreq nreq-and-opt ntotal
  627. Shuffle keyword arguments to the top of the stack, filling in the holes
  628. with @code{SCM_UNDEFINED}. Each argument is encoded over two bytes.
  629. This instruction is used by procedures with keyword arguments.
  630. @var{nreq} is the number of required arguments to the procedure, and
  631. @var{nreq-and-opt} is the total number of positional arguments (required
  632. plus optional). @code{bind-optionals/shuffle} will scan the stack from
  633. the @var{nreq}th argument up to the @var{nreq-and-opt}th, and start
  634. shuffling when it sees the first keyword argument or runs out of
  635. positional arguments.
  636. Shuffling simply moves the keyword arguments past the total number of
  637. arguments, @var{ntotal}, which includes keyword and rest arguments. The
  638. free slots created by the shuffle are filled in with
  639. @code{SCM_UNDEFINED}, so they may be conditionally initialized later in
  640. the function's prologue.
  641. @end deffn
  642. @deffn Instruction bind-kwargs idx ntotal flags
  643. Parse keyword arguments, assigning their values to the corresponding
  644. local variables. The keyword arguments should already have been shuffled
  645. above the @var{ntotal}th stack slot by @code{bind-optionals/shuffle}.
  646. The parsing is driven by a keyword arguments association list, looked up
  647. from the @var{idx}th element of the procedures object array. The alist
  648. is a list of pairs of the form @code{(@var{kw} . @var{index})}, mapping
  649. keyword arguments to their local variable indices.
  650. There are two bitflags that affect the parser, @code{allow-other-keys?}
  651. (@code{0x1}) and @code{rest?} (@code{0x2}). Unless
  652. @code{allow-other-keys?} is set, the parser will signal an error if an
  653. unknown key is found. If @code{rest?} is set, errors parsing the
  654. keyword arguments will be ignored, as a later @code{bind-rest}
  655. instruction will collect all of the tail arguments, including the
  656. keywords, into a list. Otherwise if the keyword arguments are invalid,
  657. an error is signalled.
  658. @var{idx} and @var{ntotal} are encoded over two bytes each, and
  659. @var{flags} is encoded over one byte.
  660. @end deffn
  661. @deffn Instruction reserve-locals n
  662. Resets the stack pointer to have space for @var{n} local variables,
  663. including the arguments. If this operation increments the stack pointer,
  664. as in a push, the new slots are filled with @code{SCM_UNBOUND}. If this
  665. operation decrements the stack pointer, any excess values are dropped.
  666. @code{reserve-locals} is typically used after argument parsing to
  667. reserve space for local variables.
  668. @end deffn
  669. @deffn Instruction assert-nargs-ee/locals n
  670. @deffnx Instruction assert-nargs-ge/locals n
  671. A combination of @code{assert-nargs-ee} and @code{reserve-locals}. The
  672. number of arguments is encoded in the lower three bits of @var{n}, a
  673. one-byte value. The number of additional local variables is take from
  674. the upper 5 bits of @var{n}.
  675. @end deffn
  676. @node Trampoline Instructions
  677. @subsubsection Trampoline Instructions
  678. Though most applicable objects in Guile are procedures implemented
  679. in bytecode, not all are. There are primitives, continuations, and other
  680. procedure-like objects that have their own calling convention. Instead
  681. of adding special cases to the @code{call} instruction, Guile wraps
  682. these other applicable objects in VM trampoline procedures, then
  683. provides special support for these objects in bytecode.
  684. Trampoline procedures are typically generated by Guile at runtime, for
  685. example in response to a call to @code{scm_c_make_gsubr}. As such, a
  686. compiler probably shouldn't emit code with these instructions. However,
  687. it's still interesting to know how these things work, so we document
  688. these trampoline instructions here.
  689. @deffn Instruction subr-call nargs
  690. Pop off a foreign pointer (which should have been pushed on by the
  691. trampoline), and call it directly, with the @var{nargs} arguments from
  692. the stack. Return the resulting value or values to the calling
  693. procedure.
  694. @end deffn
  695. @deffn Instruction foreign-call nargs
  696. Pop off an internal foreign object (which should have been pushed on by
  697. the trampoline), and call that foreign function with the @var{nargs}
  698. arguments from the stack. Return the resulting value to the calling
  699. procedure.
  700. @end deffn
  701. @deffn Instruction smob-call nargs
  702. Pop off the smob object from the stack (which should have been pushed on
  703. by the trampoline), and call its descriptor's @code{apply} function with
  704. the @var{nargs} arguments from the stack. Return the resulting value or
  705. values to the calling procedure.
  706. @end deffn
  707. @deffn Instruction continuation-call
  708. Pop off an internal continuation object (which should have been pushed
  709. on by the trampoline), and reinstate that continuation. All of the
  710. procedure's arguments are passed to the continuation. Does not return.
  711. @end deffn
  712. @deffn Instruction partial-cont-call
  713. Pop off two objects from the stack: the dynamic winds associated with
  714. the partial continuation, and the VM continuation object. Unroll the
  715. continuation onto the stack, rewinding the dynamic environment and
  716. overwriting the current frame, and pass all arguments to the
  717. continuation. Control flow proceeds where the continuation was captured.
  718. @end deffn
  719. @node Branch Instructions
  720. @subsubsection Branch Instructions
  721. All the conditional branch instructions described below work in the
  722. same way:
  723. @itemize
  724. @item They pop off Scheme object(s) located on the stack for use in the
  725. branch condition
  726. @item If the condition is true, then the instruction pointer is
  727. increased by the offset passed as an argument to the branch
  728. instruction;
  729. @item Program execution proceeds with the next instruction (that is,
  730. the one to which the instruction pointer points).
  731. @end itemize
  732. Note that the offset passed to the instruction is encoded as three 8-bit
  733. integers, in big-endian order, effectively giving Guile a 24-bit
  734. relative address space.
  735. @deffn Instruction br offset
  736. Jump to @var{offset}. No values are popped.
  737. @end deffn
  738. @deffn Instruction br-if offset
  739. Jump to @var{offset} if the object on the stack is not false.
  740. @end deffn
  741. @deffn Instruction br-if-not offset
  742. Jump to @var{offset} if the object on the stack is false.
  743. @end deffn
  744. @deffn Instruction br-if-eq offset
  745. Jump to @var{offset} if the two objects located on the stack are
  746. equal in the sense of @var{eq?}. Note that, for this instruction, the
  747. stack pointer is decremented by two Scheme objects instead of only
  748. one.
  749. @end deffn
  750. @deffn Instruction br-if-not-eq offset
  751. Same as @var{br-if-eq} for non-@code{eq?} objects.
  752. @end deffn
  753. @deffn Instruction br-if-null offset
  754. Jump to @var{offset} if the object on the stack is @code{'()}.
  755. @end deffn
  756. @deffn Instruction br-if-not-null offset
  757. Jump to @var{offset} if the object on the stack is not @code{'()}.
  758. @end deffn
  759. @node Data Constructor Instructions
  760. @subsubsection Data Constructor Instructions
  761. These instructions push simple immediate values onto the stack,
  762. or construct compound data structures from values on the stack.
  763. @deffn Instruction make-int8 value
  764. Push @var{value}, an 8-bit integer, onto the stack.
  765. @end deffn
  766. @deffn Instruction make-int8:0
  767. Push the immediate value @code{0} onto the stack.
  768. @end deffn
  769. @deffn Instruction make-int8:1
  770. Push the immediate value @code{1} onto the stack.
  771. @end deffn
  772. @deffn Instruction make-int16 value
  773. Push @var{value}, a 16-bit integer, onto the stack.
  774. @end deffn
  775. @deffn Instruction make-uint64 value
  776. Push @var{value}, an unsigned 64-bit integer, onto the stack. The
  777. value is encoded in 8 bytes, most significant byte first (big-endian).
  778. @end deffn
  779. @deffn Instruction make-int64 value
  780. Push @var{value}, a signed 64-bit integer, onto the stack. The value
  781. is encoded in 8 bytes, most significant byte first (big-endian), in
  782. twos-complement arithmetic.
  783. @end deffn
  784. @deffn Instruction make-false
  785. Push @code{#f} onto the stack.
  786. @end deffn
  787. @deffn Instruction make-true
  788. Push @code{#t} onto the stack.
  789. @end deffn
  790. @deffn Instruction make-nil
  791. Push @code{#nil} onto the stack.
  792. @end deffn
  793. @deffn Instruction make-eol
  794. Push @code{'()} onto the stack.
  795. @end deffn
  796. @deffn Instruction make-char8 value
  797. Push @var{value}, an 8-bit character, onto the stack.
  798. @end deffn
  799. @deffn Instruction make-char32 value
  800. Push @var{value}, an 32-bit character, onto the stack. The value is
  801. encoded in big-endian order.
  802. @end deffn
  803. @deffn Instruction make-symbol
  804. Pops a string off the stack, and pushes a symbol.
  805. @end deffn
  806. @deffn Instruction make-keyword value
  807. Pops a symbol off the stack, and pushes a keyword.
  808. @end deffn
  809. @deffn Instruction list n
  810. Pops off the top @var{n} values off of the stack, consing them up into
  811. a list, then pushes that list on the stack. What was the topmost value
  812. will be the last element in the list. @var{n} is a two-byte value,
  813. most significant byte first.
  814. @end deffn
  815. @deffn Instruction vector n
  816. Create and fill a vector with the top @var{n} values from the stack,
  817. popping off those values and pushing on the resulting vector. @var{n}
  818. is a two-byte value, like in @code{vector}.
  819. @end deffn
  820. @deffn Instruction make-struct n
  821. Make a new struct from the top @var{n} values on the stack. The values
  822. are popped, and the new struct is pushed.
  823. The deepest value is used as the vtable for the struct, and the rest are
  824. used in order as the field initializers. Tail arrays are not supported
  825. by this instruction.
  826. @end deffn
  827. @deffn Instruction make-array n
  828. Pop an array shape from the stack, then pop the remaining @var{n}
  829. values, pushing a new array. @var{n} is encoded over three bytes.
  830. The array shape should be appropriate to store @var{n} values.
  831. @xref{Array Procedures}, for more information on array shapes.
  832. @end deffn
  833. Many of these data structures are constant, never changing over the
  834. course of the different invocations of the procedure. In that case it is
  835. often advantageous to make them once when the procedure is created, and
  836. just reference them from the object table thereafter. @xref{Variables
  837. and the VM}, for more information on the object table.
  838. @deffn Instruction object-ref n
  839. @deffnx Instruction long-object-ref n
  840. Push @var{n}th value from the current program's object vector. The
  841. ``long'' variant has a 16-bit index instead of an 8-bit index.
  842. @end deffn
  843. @node Loading Instructions
  844. @subsubsection Loading Instructions
  845. In addition to VM instructions, an instruction stream may contain
  846. variable-length data embedded within it. This data is always preceded
  847. by special loading instructions, which interpret the data and advance
  848. the instruction pointer to the next VM instruction.
  849. All of these loading instructions have a @code{length} parameter,
  850. indicating the size of the embedded data, in bytes. The length itself
  851. is encoded in 3 bytes.
  852. @deffn Instruction load-number length
  853. Load an arbitrary number from the instruction stream. The number is
  854. embedded in the stream as a string.
  855. @end deffn
  856. @deffn Instruction load-string length
  857. Load a string from the instruction stream. The string is assumed to be
  858. encoded in the ``latin1'' locale.
  859. @end deffn
  860. @deffn Instruction load-wide-string length
  861. Load a UTF-32 string from the instruction stream. @var{length} is the
  862. length in bytes, not in codepoints.
  863. @end deffn
  864. @deffn Instruction load-symbol length
  865. Load a symbol from the instruction stream. The symbol is assumed to be
  866. encoded in the ``latin1'' locale. Symbols backed by wide strings may
  867. be loaded via @code{load-wide-string} then @code{make-symbol}.
  868. @end deffn
  869. @deffn Instruction load-array length
  870. Load a uniform array from the instruction stream. The shape and type
  871. of the array are popped off the stack, in that order.
  872. @end deffn
  873. @deffn Instruction load-program
  874. Load bytecode from the instruction stream, and push a compiled
  875. procedure.
  876. This instruction pops one value from the stack: the program's object
  877. table, as a vector, or @code{#f} in the case that the program has no
  878. object table. A program that does not reference toplevel bindings and
  879. does not use @code{object-ref} does not need an object table.
  880. This instruction is unlike the rest of the loading instructions,
  881. because instead of parsing its data, it directly maps the instruction
  882. stream onto a C structure, @code{struct scm_objcode}. @xref{Bytecode
  883. and Objcode}, for more information.
  884. The resulting compiled procedure will not have any free variables
  885. captured, so it may be loaded only once but used many times to create
  886. closures.
  887. @end deffn
  888. @node Dynamic Environment Instructions
  889. @subsubsection Dynamic Environment Instructions
  890. Guile's virtual machine has low-level support for @code{dynamic-wind},
  891. dynamic binding, and composable prompts and aborts.
  892. @deffn Instruction wind
  893. Pop an unwind thunk and a wind thunk from the stack, in that order, and
  894. push them onto the ``dynamic stack''. The unwind thunk will be called on
  895. nonlocal exits, and the wind thunk on reentries. Used to implement
  896. @code{dynamic-wind}.
  897. Note that neither thunk is actually called; the compiler should emit
  898. calls to wind and unwind for the normal dynamic-wind control flow.
  899. @xref{Dynamic Wind}.
  900. @end deffn
  901. @deffn Instruction unwind
  902. Pop off the top entry from the ``dynamic stack'', for example, a
  903. wind/unwind thunk pair. @code{unwind} instructions should be properly
  904. paired with their winding instructions, like @code{wind}.
  905. @end deffn
  906. @deffn Instruction wind-fluids n
  907. Pop off @var{n} values and @var{n} fluids from the stack, in that order.
  908. Set the fluids to the values by creating a with-fluids object and
  909. pushing that object on the dynamic stack. @xref{Fluids and Dynamic
  910. States}.
  911. @end deffn
  912. @deffn Instruction unwind-fluids
  913. Pop a with-fluids object from the dynamic stack, and swap the current
  914. values of its fluids with the saved values of its fluids. In this way,
  915. the dynamic environment is left as it was before the corresponding
  916. @code{wind-fluids} instruction was processed.
  917. @end deffn
  918. @deffn Instruction fluid-ref
  919. Pop a fluid from the stack, and push its current value.
  920. @end deffn
  921. @deffn Instruction fluid-set
  922. Pop a value and a fluid from the stack, in that order, and set the fluid
  923. to the value.
  924. @end deffn
  925. @deffn Instruction prompt escape-only? offset
  926. Establish a dynamic prompt. @xref{Prompts}, for more information on
  927. prompts.
  928. The prompt will be pushed on the dynamic stack. The normal control flow
  929. should ensure that the prompt is popped off at the end, via
  930. @code{unwind}.
  931. If an abort is made to this prompt, control will jump to @var{offset}, a
  932. three-byte relative address. The continuation and all arguments to the
  933. abort will be pushed on the stack, along with the total number of
  934. arguments (including the continuation. If control returns to the
  935. handler, the prompt is already popped off by the abort mechanism.
  936. (Guile's @code{prompt} implements Felleisen's @dfn{--F--} operator.)
  937. If @var{escape-only?} is nonzero, the prompt will be marked as
  938. escape-only, which allows an abort to this prompt to avoid reifying the
  939. continuation.
  940. @end deffn
  941. @deffn Instruction abort n
  942. Abort to a dynamic prompt.
  943. This instruction pops one tail argument list, @var{n} arguments, and a
  944. prompt tag from the stack. The dynamic environment is then searched for
  945. a prompt having the given tag. If none is found, an error is signalled.
  946. Otherwise all arguments are passed to the prompt's handler, along with
  947. the captured continuation, if necessary.
  948. If the prompt's handler can be proven to not reference the captured
  949. continuation, no continuation is allocated. This decision happens
  950. dynamically, at run-time; the general case is that the continuation may
  951. be captured, and thus resumed. A reinstated continuation will have its
  952. arguments pushed on the stack, along with the number of arguments, as in
  953. the multiple-value return convention. Therefore an @code{abort}
  954. instruction should be followed by code ready to handle the equivalent of
  955. a multiply-valued return.
  956. @end deffn
  957. @node Miscellaneous Instructions
  958. @subsubsection Miscellaneous Instructions
  959. @deffn Instruction nop
  960. Does nothing! Used for padding other instructions to certain
  961. alignments.
  962. @end deffn
  963. @deffn Instruction halt
  964. Exits the VM, returning a SCM value. Normally, this instruction is
  965. only part of the ``bootstrap program'', a program run when a virtual
  966. machine is first entered; compiled Scheme procedures will not contain
  967. this instruction.
  968. If multiple values have been returned, the SCM value will be a
  969. multiple-values object (@pxref{Multiple Values}).
  970. @end deffn
  971. @deffn Instruction break
  972. Does nothing, but invokes the break hook.
  973. @end deffn
  974. @deffn Instruction drop
  975. Pops off the top value from the stack, throwing it away.
  976. @end deffn
  977. @deffn Instruction dup
  978. Re-pushes the top value onto the stack.
  979. @end deffn
  980. @deffn Instruction void
  981. Pushes ``the unspecified value'' onto the stack.
  982. @end deffn
  983. @node Inlined Scheme Instructions
  984. @subsubsection Inlined Scheme Instructions
  985. The Scheme compiler can recognize the application of standard Scheme
  986. procedures. It tries to inline these small operations to avoid the
  987. overhead of creating new stack frames.
  988. Since most of these operations are historically implemented as C
  989. primitives, not inlining them would entail constantly calling out from
  990. the VM to the interpreter, which has some costs---registers must be
  991. saved, the interpreter has to dispatch, called procedures have to do
  992. much type checking, etc. It's much more efficient to inline these
  993. operations in the virtual machine itself.
  994. All of these instructions pop their arguments from the stack and push
  995. their results, and take no parameters from the instruction stream.
  996. Thus, unlike in the previous sections, these instruction definitions
  997. show stack parameters instead of parameters from the instruction
  998. stream.
  999. @deffn Instruction not x
  1000. @deffnx Instruction not-not x
  1001. @deffnx Instruction eq? x y
  1002. @deffnx Instruction not-eq? x y
  1003. @deffnx Instruction null?
  1004. @deffnx Instruction not-null?
  1005. @deffnx Instruction eqv? x y
  1006. @deffnx Instruction equal? x y
  1007. @deffnx Instruction pair? x y
  1008. @deffnx Instruction list? x
  1009. @deffnx Instruction set-car! pair x
  1010. @deffnx Instruction set-cdr! pair x
  1011. @deffnx Instruction cons x y
  1012. @deffnx Instruction car x
  1013. @deffnx Instruction cdr x
  1014. @deffnx Instruction vector-ref x y
  1015. @deffnx Instruction vector-set x n y
  1016. @deffnx Instruction struct? x
  1017. @deffnx Instruction struct-ref x n
  1018. @deffnx Instruction struct-set x n v
  1019. @deffnx Instruction struct-vtable x
  1020. @deffnx Instruction class-of x
  1021. @deffnx Instruction slot-ref struct n
  1022. @deffnx Instruction slot-set struct n x
  1023. Inlined implementations of their Scheme equivalents.
  1024. @end deffn
  1025. Note that @code{caddr} and friends compile to a series of @code{car}
  1026. and @code{cdr} instructions.
  1027. @node Inlined Mathematical Instructions
  1028. @subsubsection Inlined Mathematical Instructions
  1029. Inlining mathematical operations has the obvious advantage of handling
  1030. fixnums without function calls or allocations. The trick, of course,
  1031. is knowing when the result of an operation will be a fixnum, and there
  1032. might be a couple bugs here.
  1033. More instructions could be added here over time.
  1034. As in the previous section, the definitions below show stack
  1035. parameters instead of instruction stream parameters.
  1036. @deffn Instruction add x y
  1037. @deffnx Instruction add1 x
  1038. @deffnx Instruction sub x y
  1039. @deffnx Instruction sub1 x
  1040. @deffnx Instruction mul x y
  1041. @deffnx Instruction div x y
  1042. @deffnx Instruction quo x y
  1043. @deffnx Instruction rem x y
  1044. @deffnx Instruction mod x y
  1045. @deffnx Instruction ee? x y
  1046. @deffnx Instruction lt? x y
  1047. @deffnx Instruction gt? x y
  1048. @deffnx Instruction le? x y
  1049. @deffnx Instruction ge? x y
  1050. @deffnx Instruction ash x n
  1051. @deffnx Instruction logand x y
  1052. @deffnx Instruction logior x y
  1053. @deffnx Instruction logxor x y
  1054. Inlined implementations of the corresponding mathematical operations.
  1055. @end deffn
  1056. @node Inlined Bytevector Instructions
  1057. @subsubsection Inlined Bytevector Instructions
  1058. Bytevector operations correspond closely to what the current hardware
  1059. can do, so it makes sense to inline them to VM instructions, providing
  1060. a clear path for eventual native compilation. Without this, Scheme
  1061. programs would need other primitives for accessing raw bytes -- but
  1062. these primitives are as good as any.
  1063. As in the previous section, the definitions below show stack
  1064. parameters instead of instruction stream parameters.
  1065. The multibyte formats (@code{u16}, @code{f64}, etc) take an extra
  1066. endianness argument. Only aligned native accesses are currently
  1067. fast-pathed in Guile's VM.
  1068. @deffn Instruction bv-u8-ref bv n
  1069. @deffnx Instruction bv-s8-ref bv n
  1070. @deffnx Instruction bv-u16-native-ref bv n
  1071. @deffnx Instruction bv-s16-native-ref bv n
  1072. @deffnx Instruction bv-u32-native-ref bv n
  1073. @deffnx Instruction bv-s32-native-ref bv n
  1074. @deffnx Instruction bv-u64-native-ref bv n
  1075. @deffnx Instruction bv-s64-native-ref bv n
  1076. @deffnx Instruction bv-f32-native-ref bv n
  1077. @deffnx Instruction bv-f64-native-ref bv n
  1078. @deffnx Instruction bv-u16-ref bv n endianness
  1079. @deffnx Instruction bv-s16-ref bv n endianness
  1080. @deffnx Instruction bv-u32-ref bv n endianness
  1081. @deffnx Instruction bv-s32-ref bv n endianness
  1082. @deffnx Instruction bv-u64-ref bv n endianness
  1083. @deffnx Instruction bv-s64-ref bv n endianness
  1084. @deffnx Instruction bv-f32-ref bv n endianness
  1085. @deffnx Instruction bv-f64-ref bv n endianness
  1086. @deffnx Instruction bv-u8-set bv n val
  1087. @deffnx Instruction bv-s8-set bv n val
  1088. @deffnx Instruction bv-u16-native-set bv n val
  1089. @deffnx Instruction bv-s16-native-set bv n val
  1090. @deffnx Instruction bv-u32-native-set bv n val
  1091. @deffnx Instruction bv-s32-native-set bv n val
  1092. @deffnx Instruction bv-u64-native-set bv n val
  1093. @deffnx Instruction bv-s64-native-set bv n val
  1094. @deffnx Instruction bv-f32-native-set bv n val
  1095. @deffnx Instruction bv-f64-native-set bv n val
  1096. @deffnx Instruction bv-u16-set bv n val endianness
  1097. @deffnx Instruction bv-s16-set bv n val endianness
  1098. @deffnx Instruction bv-u32-set bv n val endianness
  1099. @deffnx Instruction bv-s32-set bv n val endianness
  1100. @deffnx Instruction bv-u64-set bv n val endianness
  1101. @deffnx Instruction bv-s64-set bv n val endianness
  1102. @deffnx Instruction bv-f32-set bv n val endianness
  1103. @deffnx Instruction bv-f64-set bv n val endianness
  1104. Inlined implementations of the corresponding bytevector operations.
  1105. @end deffn