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