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