Make snarfing tools more robust to varied C preprocessor behavior.
[bpt/guile.git] / doc / ref / vm.texi
CommitLineData
8680d53b
AW
1@c -*-texinfo-*-
2@c This is part of the GNU Guile Reference Manual.
581f410f 3@c Copyright (C) 2008,2009,2010,2013
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
82code. The downside is that the interpreter in Guile 2.0 is slower than
83the interpreter in 1.8. We hope the that the compiler's speed makes up
84for the loss!
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
64de6db5 435Set the @var{index}th local variable to a box containing a variable
acc51c3e
AW
436whose value is unbound. Used when compiling some @code{letrec}
437expressions.
438@end deffn
439
440@deffn Instruction local-boxed-ref index
3248c954 441@deffnx Instruction local-boxed-set index
acc51c3e
AW
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
581f410f 775@deffnx Instruction bind-optionals/shuffle-or-br nreq nreq-and-opt ntotal offset
acc51c3e
AW
776Shuffle keyword arguments to the top of the stack, filling in the holes
777with @code{SCM_UNDEFINED}. Each argument is encoded over two bytes.
778
779This instruction is used by procedures with keyword arguments.
780@var{nreq} is the number of required arguments to the procedure, and
781@var{nreq-and-opt} is the total number of positional arguments (required
782plus optional). @code{bind-optionals/shuffle} will scan the stack from
783the @var{nreq}th argument up to the @var{nreq-and-opt}th, and start
784shuffling when it sees the first keyword argument or runs out of
785positional arguments.
786
581f410f
AW
787@code{bind-optionals/shuffle-or-br} does the same, except that it checks
788if there are too many positional arguments before shuffling. If this is
789the case, it jumps to @var{offset}, encoded using the normal three-byte
790encoding.
791
acc51c3e
AW
792Shuffling simply moves the keyword arguments past the total number of
793arguments, @var{ntotal}, which includes keyword and rest arguments. The
794free slots created by the shuffle are filled in with
795@code{SCM_UNDEFINED}, so they may be conditionally initialized later in
796the function's prologue.
797@end deffn
798
799@deffn Instruction bind-kwargs idx ntotal flags
800Parse keyword arguments, assigning their values to the corresponding
801local variables. The keyword arguments should already have been shuffled
802above the @var{ntotal}th stack slot by @code{bind-optionals/shuffle}.
803
804The parsing is driven by a keyword arguments association list, looked up
805from the @var{idx}th element of the procedures object array. The alist
806is a list of pairs of the form @code{(@var{kw} . @var{index})}, mapping
807keyword arguments to their local variable indices.
808
809There are two bitflags that affect the parser, @code{allow-other-keys?}
810(@code{0x1}) and @code{rest?} (@code{0x2}). Unless
811@code{allow-other-keys?} is set, the parser will signal an error if an
ecb87335 812unknown key is found. If @code{rest?} is set, errors parsing the
acc51c3e
AW
813keyword arguments will be ignored, as a later @code{bind-rest}
814instruction will collect all of the tail arguments, including the
815keywords, into a list. Otherwise if the keyword arguments are invalid,
816an error is signalled.
817
818@var{idx} and @var{ntotal} are encoded over two bytes each, and
819@var{flags} is encoded over one byte.
820@end deffn
821
822@deffn Instruction reserve-locals n
823Resets the stack pointer to have space for @var{n} local variables,
824including the arguments. If this operation increments the stack pointer,
825as in a push, the new slots are filled with @code{SCM_UNBOUND}. If this
826operation decrements the stack pointer, any excess values are dropped.
827
828@code{reserve-locals} is typically used after argument parsing to
829reserve space for local variables.
830@end deffn
831
de45d8ee
AW
832@deffn Instruction assert-nargs-ee/locals n
833@deffnx Instruction assert-nargs-ge/locals n
834A combination of @code{assert-nargs-ee} and @code{reserve-locals}. The
835number of arguments is encoded in the lower three bits of @var{n}, a
836one-byte value. The number of additional local variables is take from
837the upper 5 bits of @var{n}.
838@end deffn
839
acc51c3e
AW
840
841@node Trampoline Instructions
842@subsubsection Trampoline Instructions
843
844Though most applicable objects in Guile are procedures implemented
845in bytecode, not all are. There are primitives, continuations, and other
846procedure-like objects that have their own calling convention. Instead
847of adding special cases to the @code{call} instruction, Guile wraps
848these other applicable objects in VM trampoline procedures, then
849provides special support for these objects in bytecode.
850
851Trampoline procedures are typically generated by Guile at runtime, for
852example in response to a call to @code{scm_c_make_gsubr}. As such, a
853compiler probably shouldn't emit code with these instructions. However,
854it's still interesting to know how these things work, so we document
855these trampoline instructions here.
856
857@deffn Instruction subr-call nargs
858Pop off a foreign pointer (which should have been pushed on by the
859trampoline), and call it directly, with the @var{nargs} arguments from
860the stack. Return the resulting value or values to the calling
861procedure.
862@end deffn
863
864@deffn Instruction foreign-call nargs
865Pop off an internal foreign object (which should have been pushed on by
866the trampoline), and call that foreign function with the @var{nargs}
867arguments from the stack. Return the resulting value to the calling
868procedure.
acc51c3e
AW
869@end deffn
870
871@deffn Instruction continuation-call
872Pop off an internal continuation object (which should have been pushed
873on by the trampoline), and reinstate that continuation. All of the
874procedure's arguments are passed to the continuation. Does not return.
875@end deffn
876
877@deffn Instruction partial-cont-call
878Pop off two objects from the stack: the dynamic winds associated with
879the partial continuation, and the VM continuation object. Unroll the
880continuation onto the stack, rewinding the dynamic environment and
881overwriting the current frame, and pass all arguments to the
882continuation. Control flow proceeds where the continuation was captured.
883@end deffn
884
885
886@node Branch Instructions
887@subsubsection Branch Instructions
888
889All the conditional branch instructions described below work in the
890same way:
891
892@itemize
893@item They pop off Scheme object(s) located on the stack for use in the
894branch condition
895@item If the condition is true, then the instruction pointer is
896increased by the offset passed as an argument to the branch
897instruction;
898@item Program execution proceeds with the next instruction (that is,
899the one to which the instruction pointer points).
900@end itemize
901
902Note that the offset passed to the instruction is encoded as three 8-bit
903integers, in big-endian order, effectively giving Guile a 24-bit
904relative address space.
905
906@deffn Instruction br offset
907Jump to @var{offset}. No values are popped.
908@end deffn
909
910@deffn Instruction br-if offset
911Jump to @var{offset} if the object on the stack is not false.
912@end deffn
913
914@deffn Instruction br-if-not offset
915Jump to @var{offset} if the object on the stack is false.
916@end deffn
917
918@deffn Instruction br-if-eq offset
919Jump to @var{offset} if the two objects located on the stack are
64de6db5 920equal in the sense of @code{eq?}. Note that, for this instruction, the
acc51c3e
AW
921stack pointer is decremented by two Scheme objects instead of only
922one.
923@end deffn
924
925@deffn Instruction br-if-not-eq offset
64de6db5 926Same as @code{br-if-eq} for non-@code{eq?} objects.
acc51c3e
AW
927@end deffn
928
929@deffn Instruction br-if-null offset
930Jump to @var{offset} if the object on the stack is @code{'()}.
931@end deffn
932
933@deffn Instruction br-if-not-null offset
934Jump to @var{offset} if the object on the stack is not @code{'()}.
935@end deffn
936
937
938@node Data Constructor Instructions
939@subsubsection Data Constructor Instructions
940
941These instructions push simple immediate values onto the stack,
ecb87335 942or construct compound data structures from values on the stack.
bd7aa35f 943
8680d53b
AW
944@deffn Instruction make-int8 value
945Push @var{value}, an 8-bit integer, onto the stack.
946@end deffn
947
948@deffn Instruction make-int8:0
949Push the immediate value @code{0} onto the stack.
950@end deffn
951
952@deffn Instruction make-int8:1
953Push the immediate value @code{1} onto the stack.
954@end deffn
955
956@deffn Instruction make-int16 value
957Push @var{value}, a 16-bit integer, onto the stack.
958@end deffn
959
586cfdec
AW
960@deffn Instruction make-uint64 value
961Push @var{value}, an unsigned 64-bit integer, onto the stack. The
962value is encoded in 8 bytes, most significant byte first (big-endian).
963@end deffn
964
965@deffn Instruction make-int64 value
966Push @var{value}, a signed 64-bit integer, onto the stack. The value
967is encoded in 8 bytes, most significant byte first (big-endian), in
968twos-complement arithmetic.
969@end deffn
970
8680d53b
AW
971@deffn Instruction make-false
972Push @code{#f} onto the stack.
973@end deffn
974
975@deffn Instruction make-true
976Push @code{#t} onto the stack.
977@end deffn
978
4530432e 979@deffn Instruction make-nil
92a61010 980Push @code{#nil} onto the stack.
4530432e
DK
981@end deffn
982
8680d53b
AW
983@deffn Instruction make-eol
984Push @code{'()} onto the stack.
985@end deffn
986
987@deffn Instruction make-char8 value
988Push @var{value}, an 8-bit character, onto the stack.
989@end deffn
990
98850fd7
AW
991@deffn Instruction make-char32 value
992Push @var{value}, an 32-bit character, onto the stack. The value is
993encoded in big-endian order.
994@end deffn
995
996@deffn Instruction make-symbol
997Pops a string off the stack, and pushes a symbol.
998@end deffn
999
1000@deffn Instruction make-keyword value
1001Pops a symbol off the stack, and pushes a keyword.
1002@end deffn
1003
8680d53b
AW
1004@deffn Instruction list n
1005Pops off the top @var{n} values off of the stack, consing them up into
1006a list, then pushes that list on the stack. What was the topmost value
81fd3152
AW
1007will be the last element in the list. @var{n} is a two-byte value,
1008most significant byte first.
8680d53b
AW
1009@end deffn
1010
1011@deffn Instruction vector n
1012Create and fill a vector with the top @var{n} values from the stack,
81fd3152
AW
1013popping off those values and pushing on the resulting vector. @var{n}
1014is a two-byte value, like in @code{vector}.
8680d53b
AW
1015@end deffn
1016
acc51c3e
AW
1017@deffn Instruction make-struct n
1018Make a new struct from the top @var{n} values on the stack. The values
1019are popped, and the new struct is pushed.
1020
1021The deepest value is used as the vtable for the struct, and the rest are
1022used in order as the field initializers. Tail arrays are not supported
1023by this instruction.
1024@end deffn
1025
1026@deffn Instruction make-array n
1027Pop an array shape from the stack, then pop the remaining @var{n}
1028values, pushing a new array. @var{n} is encoded over three bytes.
1029
1030The array shape should be appropriate to store @var{n} values.
1031@xref{Array Procedures}, for more information on array shapes.
1032@end deffn
1033
1034Many of these data structures are constant, never changing over the
1035course of the different invocations of the procedure. In that case it is
1036often advantageous to make them once when the procedure is created, and
1037just reference them from the object table thereafter. @xref{Variables
1038and the VM}, for more information on the object table.
1039
1040@deffn Instruction object-ref n
1041@deffnx Instruction long-object-ref n
1042Push @var{n}th value from the current program's object vector. The
1043``long'' variant has a 16-bit index instead of an 8-bit index.
1044@end deffn
1045
1046
1047@node Loading Instructions
1048@subsubsection Loading Instructions
1049
1050In addition to VM instructions, an instruction stream may contain
1051variable-length data embedded within it. This data is always preceded
1052by special loading instructions, which interpret the data and advance
1053the instruction pointer to the next VM instruction.
1054
1055All of these loading instructions have a @code{length} parameter,
1056indicating the size of the embedded data, in bytes. The length itself
1057is encoded in 3 bytes.
1058
1059@deffn Instruction load-number length
1060Load an arbitrary number from the instruction stream. The number is
1061embedded in the stream as a string.
1062@end deffn
1063@deffn Instruction load-string length
1064Load a string from the instruction stream. The string is assumed to be
080a9d4f 1065encoded in the ``latin1'' locale.
acc51c3e
AW
1066@end deffn
1067@deffn Instruction load-wide-string length
1068Load a UTF-32 string from the instruction stream. @var{length} is the
ecb87335 1069length in bytes, not in codepoints.
acc51c3e
AW
1070@end deffn
1071@deffn Instruction load-symbol length
1072Load a symbol from the instruction stream. The symbol is assumed to be
080a9d4f 1073encoded in the ``latin1'' locale. Symbols backed by wide strings may
acc51c3e
AW
1074be loaded via @code{load-wide-string} then @code{make-symbol}.
1075@end deffn
1076@deffn Instruction load-array length
1077Load a uniform array from the instruction stream. The shape and type
1078of the array are popped off the stack, in that order.
1079@end deffn
1080
1081@deffn Instruction load-program
1082Load bytecode from the instruction stream, and push a compiled
1083procedure.
1084
1085This instruction pops one value from the stack: the program's object
1086table, as a vector, or @code{#f} in the case that the program has no
1087object table. A program that does not reference toplevel bindings and
1088does not use @code{object-ref} does not need an object table.
1089
1090This instruction is unlike the rest of the loading instructions,
1091because instead of parsing its data, it directly maps the instruction
1092stream onto a C structure, @code{struct scm_objcode}. @xref{Bytecode
1093and Objcode}, for more information.
1094
1095The resulting compiled procedure will not have any free variables
1096captured, so it may be loaded only once but used many times to create
1097closures.
1098@end deffn
1099
1100@node Dynamic Environment Instructions
1101@subsubsection Dynamic Environment Instructions
1102
1103Guile's virtual machine has low-level support for @code{dynamic-wind},
1104dynamic binding, and composable prompts and aborts.
1105
1106@deffn Instruction wind
1107Pop an unwind thunk and a wind thunk from the stack, in that order, and
1108push them onto the ``dynamic stack''. The unwind thunk will be called on
1109nonlocal exits, and the wind thunk on reentries. Used to implement
1110@code{dynamic-wind}.
1111
1112Note that neither thunk is actually called; the compiler should emit
1113calls to wind and unwind for the normal dynamic-wind control flow.
1114@xref{Dynamic Wind}.
1115@end deffn
1116
1117@deffn Instruction unwind
1118Pop off the top entry from the ``dynamic stack'', for example, a
1119wind/unwind thunk pair. @code{unwind} instructions should be properly
1120paired with their winding instructions, like @code{wind}.
1121@end deffn
1122
1123@deffn Instruction wind-fluids n
1124Pop off @var{n} values and @var{n} fluids from the stack, in that order.
1125Set the fluids to the values by creating a with-fluids object and
1126pushing that object on the dynamic stack. @xref{Fluids and Dynamic
1127States}.
1128@end deffn
1129
1130@deffn Instruction unwind-fluids
1131Pop a with-fluids object from the dynamic stack, and swap the current
1132values of its fluids with the saved values of its fluids. In this way,
1133the dynamic environment is left as it was before the corresponding
1134@code{wind-fluids} instruction was processed.
1135@end deffn
1136
1137@deffn Instruction fluid-ref
1138Pop a fluid from the stack, and push its current value.
1139@end deffn
1140
1141@deffn Instruction fluid-set
1142Pop a value and a fluid from the stack, in that order, and set the fluid
1143to the value.
1144@end deffn
1145
1146@deffn Instruction prompt escape-only? offset
1147Establish a dynamic prompt. @xref{Prompts}, for more information on
1148prompts.
1149
1150The prompt will be pushed on the dynamic stack. The normal control flow
1151should ensure that the prompt is popped off at the end, via
1152@code{unwind}.
1153
1154If an abort is made to this prompt, control will jump to @var{offset}, a
1155three-byte relative address. The continuation and all arguments to the
1156abort will be pushed on the stack, along with the total number of
1157arguments (including the continuation. If control returns to the
1158handler, the prompt is already popped off by the abort mechanism.
1159(Guile's @code{prompt} implements Felleisen's @dfn{--F--} operator.)
1160
1161If @var{escape-only?} is nonzero, the prompt will be marked as
1162escape-only, which allows an abort to this prompt to avoid reifying the
1163continuation.
1164@end deffn
1165
1166@deffn Instruction abort n
1167Abort to a dynamic prompt.
1168
1169This instruction pops one tail argument list, @var{n} arguments, and a
1170prompt tag from the stack. The dynamic environment is then searched for
1171a prompt having the given tag. If none is found, an error is signalled.
1172Otherwise all arguments are passed to the prompt's handler, along with
1173the captured continuation, if necessary.
1174
1175If the prompt's handler can be proven to not reference the captured
1176continuation, no continuation is allocated. This decision happens
1177dynamically, at run-time; the general case is that the continuation may
1178be captured, and thus resumed. A reinstated continuation will have its
1179arguments pushed on the stack, along with the number of arguments, as in
1180the multiple-value return convention. Therefore an @code{abort}
1181instruction should be followed by code ready to handle the equivalent of
1182a multiply-valued return.
1183@end deffn
1184
8680d53b
AW
1185@node Miscellaneous Instructions
1186@subsubsection Miscellaneous Instructions
1187
1188@deffn Instruction nop
98850fd7
AW
1189Does nothing! Used for padding other instructions to certain
1190alignments.
8680d53b
AW
1191@end deffn
1192
1193@deffn Instruction halt
bd7aa35f
AW
1194Exits the VM, returning a SCM value. Normally, this instruction is
1195only part of the ``bootstrap program'', a program run when a virtual
1196machine is first entered; compiled Scheme procedures will not contain
1197this instruction.
1198
1199If multiple values have been returned, the SCM value will be a
e3ba263d 1200multiple-values object (@pxref{Multiple Values}).
8680d53b
AW
1201@end deffn
1202
1203@deffn Instruction break
1204Does nothing, but invokes the break hook.
1205@end deffn
1206
1207@deffn Instruction drop
1208Pops off the top value from the stack, throwing it away.
1209@end deffn
1210
1211@deffn Instruction dup
1212Re-pushes the top value onto the stack.
1213@end deffn
1214
1215@deffn Instruction void
1216Pushes ``the unspecified value'' onto the stack.
1217@end deffn
1218
1219@node Inlined Scheme Instructions
1220@subsubsection Inlined Scheme Instructions
1221
bd7aa35f 1222The Scheme compiler can recognize the application of standard Scheme
81fd3152
AW
1223procedures. It tries to inline these small operations to avoid the
1224overhead of creating new stack frames.
bd7aa35f
AW
1225
1226Since most of these operations are historically implemented as C
1227primitives, not inlining them would entail constantly calling out from
86872cc3 1228the VM to the interpreter, which has some costs---registers must be
bd7aa35f 1229saved, the interpreter has to dispatch, called procedures have to do
ecb87335 1230much type checking, etc. It's much more efficient to inline these
bd7aa35f
AW
1231operations in the virtual machine itself.
1232
1233All of these instructions pop their arguments from the stack and push
1234their results, and take no parameters from the instruction stream.
1235Thus, unlike in the previous sections, these instruction definitions
1236show stack parameters instead of parameters from the instruction
1237stream.
1238
8680d53b 1239@deffn Instruction not x
bd7aa35f
AW
1240@deffnx Instruction not-not x
1241@deffnx Instruction eq? x y
1242@deffnx Instruction not-eq? x y
1243@deffnx Instruction null?
1244@deffnx Instruction not-null?
1245@deffnx Instruction eqv? x y
1246@deffnx Instruction equal? x y
1247@deffnx Instruction pair? x y
81fd3152 1248@deffnx Instruction list? x
bd7aa35f
AW
1249@deffnx Instruction set-car! pair x
1250@deffnx Instruction set-cdr! pair x
81fd3152 1251@deffnx Instruction cons x y
bd7aa35f
AW
1252@deffnx Instruction car x
1253@deffnx Instruction cdr x
98850fd7
AW
1254@deffnx Instruction vector-ref x y
1255@deffnx Instruction vector-set x n y
acc51c3e
AW
1256@deffnx Instruction struct? x
1257@deffnx Instruction struct-ref x n
1258@deffnx Instruction struct-set x n v
1259@deffnx Instruction struct-vtable x
1260@deffnx Instruction class-of x
1261@deffnx Instruction slot-ref struct n
1262@deffnx Instruction slot-set struct n x
bd7aa35f
AW
1263Inlined implementations of their Scheme equivalents.
1264@end deffn
1265
1266Note that @code{caddr} and friends compile to a series of @code{car}
1267and @code{cdr} instructions.
8680d53b
AW
1268
1269@node Inlined Mathematical Instructions
1270@subsubsection Inlined Mathematical Instructions
1271
bd7aa35f
AW
1272Inlining mathematical operations has the obvious advantage of handling
1273fixnums without function calls or allocations. The trick, of course,
1274is knowing when the result of an operation will be a fixnum, and there
1275might be a couple bugs here.
1276
1277More instructions could be added here over time.
1278
1279As in the previous section, the definitions below show stack
1280parameters instead of instruction stream parameters.
1281
1282@deffn Instruction add x y
98850fd7 1283@deffnx Instruction add1 x
bd7aa35f 1284@deffnx Instruction sub x y
98850fd7 1285@deffnx Instruction sub1 x
bd7aa35f
AW
1286@deffnx Instruction mul x y
1287@deffnx Instruction div x y
1288@deffnx Instruction quo x y
1289@deffnx Instruction rem x y
1290@deffnx Instruction mod x y
1291@deffnx Instruction ee? x y
1292@deffnx Instruction lt? x y
1293@deffnx Instruction gt? x y
1294@deffnx Instruction le? x y
1295@deffnx Instruction ge? x y
acc51c3e
AW
1296@deffnx Instruction ash x n
1297@deffnx Instruction logand x y
1298@deffnx Instruction logior x y
1299@deffnx Instruction logxor x y
bd7aa35f 1300Inlined implementations of the corresponding mathematical operations.
8680d53b 1301@end deffn
98850fd7
AW
1302
1303@node Inlined Bytevector Instructions
1304@subsubsection Inlined Bytevector Instructions
1305
1306Bytevector operations correspond closely to what the current hardware
1307can do, so it makes sense to inline them to VM instructions, providing
1308a clear path for eventual native compilation. Without this, Scheme
1309programs would need other primitives for accessing raw bytes -- but
1310these primitives are as good as any.
1311
1312As in the previous section, the definitions below show stack
1313parameters instead of instruction stream parameters.
1314
1315The multibyte formats (@code{u16}, @code{f64}, etc) take an extra
1316endianness argument. Only aligned native accesses are currently
1317fast-pathed in Guile's VM.
1318
1319@deffn Instruction bv-u8-ref bv n
1320@deffnx Instruction bv-s8-ref bv n
1321@deffnx Instruction bv-u16-native-ref bv n
1322@deffnx Instruction bv-s16-native-ref bv n
1323@deffnx Instruction bv-u32-native-ref bv n
1324@deffnx Instruction bv-s32-native-ref bv n
1325@deffnx Instruction bv-u64-native-ref bv n
1326@deffnx Instruction bv-s64-native-ref bv n
1327@deffnx Instruction bv-f32-native-ref bv n
1328@deffnx Instruction bv-f64-native-ref bv n
1329@deffnx Instruction bv-u16-ref bv n endianness
1330@deffnx Instruction bv-s16-ref bv n endianness
1331@deffnx Instruction bv-u32-ref bv n endianness
1332@deffnx Instruction bv-s32-ref bv n endianness
1333@deffnx Instruction bv-u64-ref bv n endianness
1334@deffnx Instruction bv-s64-ref bv n endianness
1335@deffnx Instruction bv-f32-ref bv n endianness
1336@deffnx Instruction bv-f64-ref bv n endianness
1337@deffnx Instruction bv-u8-set bv n val
1338@deffnx Instruction bv-s8-set bv n val
1339@deffnx Instruction bv-u16-native-set bv n val
1340@deffnx Instruction bv-s16-native-set bv n val
1341@deffnx Instruction bv-u32-native-set bv n val
1342@deffnx Instruction bv-s32-native-set bv n val
1343@deffnx Instruction bv-u64-native-set bv n val
1344@deffnx Instruction bv-s64-native-set bv n val
1345@deffnx Instruction bv-f32-native-set bv n val
1346@deffnx Instruction bv-f64-native-set bv n val
1347@deffnx Instruction bv-u16-set bv n val endianness
1348@deffnx Instruction bv-s16-set bv n val endianness
1349@deffnx Instruction bv-u32-set bv n val endianness
1350@deffnx Instruction bv-s32-set bv n val endianness
1351@deffnx Instruction bv-u64-set bv n val endianness
1352@deffnx Instruction bv-s64-set bv n val endianness
1353@deffnx Instruction bv-f32-set bv n val endianness
1354@deffnx Instruction bv-f64-set bv n val endianness
1355Inlined implementations of the corresponding bytevector operations.
1356@end deffn