Initial revision
[sbcl.git] / doc / cmucl / internals / vm.tex
1 \chapter{Introduction} % -*- Dictionary: design -*-
2
3 (defun gvp (f)
4   (with-open-file (s f :direction :output :if-exists :supersede)
5     (maphash \#'(lambda (k v)
6                  (declare (ignore v))
7                  (format s "~A~%" k))
8              (c::backend-template-names c::*backend*))))
9
10 \f
11 \section{Scope and Purpose}
12
13 This document describes the Virtual Machine that serves as the basis for the
14 portable implementation of \ccl.  The Virtual Machine (hereafter referred to as
15 the VM) provides a layer of abstraction that hides low-level details of
16 hardware and implementation strategy, while still revealing enough of the
17 implementation so that most of the system can be written at the VM level or
18 above.
19
20 \begin{comment}
21
22 {\#\#\# Shouldn't specify VOPs.  Instead, should specify which \clisp functions
23 are primitive and which subprimitives exist.  Isn't really anyone's business
24 which VOPs actually exist.  Each primitive function or subprimitive is
25 implemented either as a VOP or as expansion into Lisp code, at the particular
26 implementation's discretion.
27
28 From this point of view, the document is expressing the contract that the Lisp
29 level code outside of the compiler must satisfy.  All functions must ultimately
30 be defined in terms of primitive functions and sub-primitives.  
31
32 The responsibility of the compiler is to implement these primitive operations,
33 and also to implement special forms, variables and function calling.
34
35 VOPs emitted by the hard-wired translators for non-function nodes are a
36 somewhat different story.  Each implementation will presumably implement all
37 these VOPs in order to avoid having to rewrite IR2 translation.  We also need
38 to spend quite a bit of time discussing the semantics of these operations,
39 since they don't just correspond to some \clisp function with type constraints.
40
41 Hard-wired stuff:
42
43 function call
44 variable access:
45   global
46   function
47   constant
48   closure
49   local
50 closure creation
51 non-local exit
52 special binding/unbinding
53 TN hacking:
54   move VOPs
55   TN address (???)
56 Conditionals:
57   Basic conditionals: EQ, ...
58   Interface to generation of other conditional VOPs.
59
60 Some VOPs don't need to be implemented at all:
61   VOPs to delimit the lifetimes of big stack TNs such as catch blocks
62   Others?  Move VOPs might be defined in terms of an implementation supplied
63   move routine, since we probably also need this info outside of VOP generators
64   so that implicit moves can be generated.
65
66
67 Type testing/checking (somehow)
68
69 }
70
71 What this document talks about:
72
73 Interface between compiler front-end and back end. (VOPs)
74    Primitive \clisp operations directly supported by the VM.
75    Support for complex language features such as function call.
76
77 Sub-primitives that allow system code to do things not possible in \clisp.
78
79 Descriptions of how the current \ccl system uses VM facilities, especially
80 non-standard ones accessed through sub-primitives.
81
82 Notes about known portability problems.
83
84 Guidelines for writing portable \ccl system code.  To some degree these
85 guidelines are implied by statements that certain things are true of \ccl
86 system code.
87
88 Descriptions of data structures that are not directly used by the VM, such as
89 debug information and Core files.
90
91 Descriptions of data structures that are directly used by the VM, such as
92 symbols and arrays.
93
94
95 Who should read it:
96
97 People who want to port \ccl.
98 People who want to understand the compiler.
99 People who want to understand how \ccl works.
100 People who need to write portable \ccl system code.
101 People such as debugger writers who need to access \ccl\t()'s internal data
102 structures.
103
104 What it won't do:
105
106 Tell you things that are obviously implementation dependent, such as type
107 systems or memory management disciplines.  See the the various implementation
108 VM documents.
109
110 Tell you only what you need to know.  Programmers shouldn't exploit properties
111 of the VM documented here unless there is no way to do the same thing in
112 portable \clisp.
113
114 Tell you how the compiler works.  In order to understand some of the subtleties
115 of VOP descriptions, you will have to understand the IR2 representation and how
116 it fits into the rest of the compiler.
117
118 Tell you anything about \clisp semantics.  When some part of the VM has a
119 direct relationship to \clisp semantics, the relationship will be directly
120 stated using \clisp terminology, since a restatement of the semantics is likely
121 to be inaccurate or misleading.  Exceptions will be made only when some
122 implication of the \clisp semantics is non-obvious.
123
124 Tell you everything about how \ccl works.  This document only offers
125 information that is likely to be needed by programmers doing a port or writing
126 system code; portable, self-contained parts of the system are totally ignored.
127 This document deliberately avoids replicating information that is easily
128 available in the system sources, since such replicated information is always
129 incorrect somewhere.  In some cases, a forwarding pointer to the appropriate
130 source will be given.
131
132
133 Things the VM won't do:
134
135 The VM specification does not totally solve the problem of porting \ccl, since
136 it is inevitable that it will not map cleanly to all possible combinations of
137 hardware and operating systems.  The VM should not be regarded as being cast in
138 concrete, since changes in many characteristics would only affect a tiny
139 fraction of the system sources.
140
141 One current major problem with porting is that large pieces of functionality
142 are entirely within the VM, and would need to be reimplemented for each port.
143 A major goal for future work on the system is moving code out of the VM, both
144 by supporting a "fast-call" convention that allows reasonable use of Lisp in
145 the out of line implementation of VOPs, and by having a "bugout" mechanism that
146 allows the VM to call Lisp functions to implement the hard cases in some VOPs.
147
148 The VM is designed to support conventional, untagged, general register
149 architectures.  Suitably lobotomized, it could be mapped to less flexible
150 hardware such as "Lisp machines", but the compiler would have serious
151 difficulties supporting stack architectures.
152
153 The VM does not support concurrent lightweight processes.  Locking primitives
154 and deep-binding of specials would be needed.
155
156 The VM does not deal with operating systems interface issues at all.  A minimal
157 port would require implementing at least file and terminal I/O streams.  \ccl
158 implements system interfaces using Aliens and other facilities built on top of
159 them.
160
161 \end{comment}
162
163
164
165 Major components:
166 \begin{itemize}
167 Specific virtual operations implemented by the VM (VOPs).  VOPs are primarily
168 the concern of the compiler, since it translates Lisp code into VOPs and then
169 translates VOPs into the implementation.
170
171 Sub-primitives that are used by Lisp code needing to perform operations
172 below the Lisp level.  The compiler implements some sub-primitives directly
173 using VOPs, while others are translated into Lisp code.  Sub-primitives provide
174 a layer of insulation between the Lisp system code and the VM, since the Lisp
175 code may assume the existence of operations that are not implemented directly
176 by the VM.  Only sub-primitives with fairly portable semantics are documented
177 here.  Others are in implementation-specific VM documentation.
178 \end{itemize}
179
180 \comment<Not all sub-primitives are VOPs, and most VOPs are not sub-primitives.>
181
182
183 \f
184 \subsection{VOP base name rules}
185
186 The names of VOPs that implement functions are based on the function name.
187 Other VOPs may use any base that doesn't conflict with a function name.  There
188 are some rules used to obtain the base name for related operations.
189
190 To get the name of a setting operation, replace the string "{\tt ref}" in the name
191 with "{\tt set}".  If "{\tt ref}" doesn't appear in the name, add the prefix "{\tt set-}" to the
192 base name.  For example, {\tt svref} becomes {\tt svset}, and {\tt symbol-value}
193 becomes {\tt set-symbol-value}.
194
195 To get the name of a conditional VOP from the name of a predicate, add the
196 prefix "{\tt if-}" to the predicate name.  For example, {\tt eq} becomes {\tt if-eq}.
197 {\tt eq} by itself would be a VOP that returned true or false value.
198
199 Some operations check for some error condition, magically signalling the error
200 through an implicit control transfer.  These operations are prefixed with
201 "{\tt check-}", as in {\tt check-fixnum} and {\tt check-bound}.
202
203
204 \f
205 \subsection{VOP name prefixes and suffixes}
206
207 Prefixes and suffixes are added to the base to get the names of variant
208 versions of the VOP.  The fully general VOP name looks like this:
209 \begin{format}
210    {"{\tt small-}" | "{\tt fast-}"} {\it name}{"{\tt -c}" {\it info}}{"{\tt /}" {\it type}{"{\tt =>}" {\it result-type}}
211 \end{format}
212 The "{\tt small-}" and "{\tt fast-}" prefixes indicates that the VOP does minimal
213 safety checking and is optimized for space or speed, respectively.  The absence
214 of a prefix indicates the safest (or only) version.  Usually if the "{\tt small-}"
215 VOP exists, it will be a synonym for either the fast version or the safe
216 version, depending on which is smaller.
217
218 The "{\tt -c}" suffix indicates that the some info that is passed as a normal
219 argument to the base version of the VOP is passed as Codegen-Info in this
220 version.  A typical use would be for VOPs where it is important to use a
221 different version when one of the arguments is a compile time constant.
222 {\it info} is some (possibly null) string that indicates which "{\tt -c}" variant
223 is involved.
224
225 The "{\tt /}{\it type}" suffix asserts that all operands that could be of {\it type} are.
226 For example, {\tt +/fixnum} adds two fixnums returning a fixnum, while
227 {\tt length/simple-vector} finds the length of a simple vector, but the result isn't
228 a simple vector.
229
230 The "{\tt =>}{\it result-type}" suffix supplies a result type assertion on the
231 operation.
232
233 A not totally silly example of all these modifiers simultaneously is
234  {\tt fast-+-c/fixnum=>integer}.  This operation would this operation adds two
235 fixnums, one of which is a constant passed as codegen info, resulting in an
236 integer.  The implementation is optimized for speed at the expense of space and
237 safety.
238
239
240 \f
241 \chapter{Data Types and Storage Resources}
242
243 \f
244 \section{Lisp Objects}
245 \index{Lisp objects}
246
247 A Lisp object is fixed-size data structure that is organized in a way mandated
248 by the VM implementation.  The fixed format allows the VM to determine the type
249 of the object.  \comment<Virtual type?  VM type?  Implementation type?
250 ...provides the VM enough information about the type of the object for the VM
251 to implement the VM-level semantics...  ...supports the "dynamic types"...>
252
253 Lisp objects are stored in locations known as cells. 
254
255
256 Has major types: immediate and non-immediate.
257 Non-immediate objects may have a subtype.
258 Non-immediate types:
259   symbol (nil may be weird)
260   cons 
261   ratio
262   complex
263   some float types
264   g-vector
265   i-vector
266   string
267   bit-vector
268   environment (always has subtype)
269   array header
270   bignum
271   structure
272   pc (code vector)
273   stack closure (control stack pointer)
274
275 Non-immediate objects are allocated in "type spaces".  The type space of an
276 object is characterized by a small integer known as the type code.  Any two
277 objects of one of the above boxed types will always have the same type code.
278 {But not really...  Some types might be allocated in different type spaces at
279 different times. (?)}
280
281 The type code doesn't totally describe the object.  In general, subtype
282 information may be involved.
283
284
285 Immediate types:
286   character
287   fixnum
288   unbound trap
289   short float
290
291
292 \f
293 \section{Type VOPs}
294
295 We consider control transfer to be the fundamental result of comparison, rather
296 than anything such as a condition code.  Although most compilers with whizzy
297 register allocation seem to explicitly allocate and manipulate the condition
298 codes, it seems that any benefit is small in our case.  This is partly because
299 our VOPs are at a somewhat higher level, making it difficult to tell which VOPs
300 do and don't trash the the CC.  Explicitly incorporating condition codes in our
301 VM also introduces another architecture dependency.
302
303 At the IR2 level, we have a class of IF-XXX VOPs which transfer control to one
304 of two places on the basis of some test on the operands.  When generating code
305 for a predicate, we peek at the destination IF node to find where to transfer
306 control to.
307                         
308 The exact representation of type tests in IR2 will be fairly implementation
309 dependent, since it will depend on the specific type system for the given
310 implementation.  For example, if an implementation can test some types with a
311 simple tag check, but other types require reading a field from the object in
312 addition, then the two different kinds of checks should be distinct at the VOP
313 level, since this will allow the VOP cost and storage information to be more
314 accurate.  Generation of type tests should be factored out of code which would
315 otherwise be more portable.  Probably the IR2 translator for TYPEP and the type
316 check generation code are the only places that should know about how type tests
317 are represented in IR2.
318
319 if-type (object)
320 if-type-range
321     If-Type Tests whether Object has the type code that is passed in the
322     codegen info.  If-Type-Range tests for a range of type codes.
323
324 {small, fast} if-vector-type (object)
325     Test that Object is either of the specified type code, or is a 1d array
326     header with data having the specified type code.
327
328 if-vector-subtype (object)
329     Test the subtype field of a vector-like object.  It is assumed that the
330     object has already been determined to be vector-like.
331
332 if-fixnump (object)
333 if-short-float-p
334 if-characterp
335     The rationale behind having these as separate VOPs is that they are likely
336     to be immediate types, and thus may have bizzare type schemes.
337
338 if-consp (object)
339 if-listp
340     We have distinct operations for these predicates since one or the other
341     isn't a simple tag test, but we don't know which one.
342
343 if-rationalp (object)
344 if-floatp
345 if-integerp
346 if-numberp
347 if-vectorp
348 if-functionp
349     The rationale behind having these operations is that they may take a lot of
350     code, so it is reasonable to put them out of line.
351
352
353 \f
354 \section{Type Sub-primitives}
355
356 change-type (object) => result
357     Change the type of an object according to codegen info.  The meaning of
358     this is highly type-system dependent, but it doesn't matter, since the
359     compiler will never emit this VOP directly.  The only way that it can show
360     up is through %Primitive.
361 get-type
362
363
364 Storage resources:
365
366 Boxed and unboxed locations:
367 Non-immediate objects may not be stored in unboxed locations.
368 Things not lisp objects may not be stored in boxed locations.
369
370 Control stack is boxed.
371 Optional number stack is unboxed.
372 Heap environment is boxed.
373 Fixed number of registers, some boxed and some unboxed.
374
375 PCs may be stored on the control stack or in boxed registers, subject to the
376 constraint that a corresponding environment is also stored.  Locations
377 containing PCs don't need to be zeroed when they are no longer used; nothing
378 bad will happen if an old PC is unaccompanied by an environment.
379
380
381 \item[Trap]Illegal object trap.  This value is used in symbols to signify an
382 undefined value or definition.
383
384 \f
385 \chapter{Characters}
386
387
388 Character is an immediate type.  Characters are manipulated primarily by
389 converting into an integer and accessing these fields:
390 \begin{description}
391 \item[{\tt %character-code-byte}]The character code.  This is effectively required to
392 start at bit 0, since \cl equates {\tt char-int} to {\tt char-code} when there is
393 no bits or font.  All current \ccl systems use ASCII for the character codes,
394 and define {\tt \#\newline} to be a linefeed, but system code should not count on
395 this.
396
397 \item[{\tt %character-control-byte}]The character bits.  Character bits are used by
398 Hemlock to describe modifiers in keyboard events, but there is no assumption of
399 any general portable significance of character bits.
400
401 {\tt %character-font-byte}\\The character font.  This is not used by \ccl, and is
402 not particularly useful.
403 \end{description}
404
405 Characters should be converted to and from integers by using the \clisp
406 {\tt char-int} and {\tt int-char} functions, which the compiler translates into
407 these VOPs:
408 \begin{example}
409 char-int (char) => int
410 int-char (int) => char
411 \end{example}
412 In the common case where Char is known to be a {\tt string-char}, these
413 operations are equivalent to {\tt char-code} and {\tt code-char}.  In addition to
414 providing a portable interface to character conversion, the VOP representation
415 of this type conversion allows the compiler to avoid unnecessary boxing and
416 unboxing of character objects.
417
418 Existing code explicitly converts fixnums to characters by using the
419 Make-Immediate-Type sub-primitive with %Character-Type.  Currently conversion
420 of characters to fixnums is rather confused.  Originally, characters were a
421 subtype of the Misc type code, and the result of the Make-Fixnum sub-primitive
422 had to be masked with {\tt %character-int-mask}; some code still does this, while
423 other code may not.
424
425 Character comparisons could be implemented by doing numeric comparisons on the
426 result of {\tt char-int}, or by using {\tt eq} in the case of {\tt char=}, but this
427 can result in unnecessary type conversions.  Instead, the compiler uses these
428 conditional VOPs:
429 \begin{example}
430 if-char= (x y)
431 if-char< (x y)
432 if-char> (x y)
433 \end{example}
434
435 \f
436 \chapter{Symbols}
437
438
439 Symbols are currently fairly boring in \ccl, containing only the obvious slots:
440 \begin{description}
441 {\tt %symbol-value-slot}\\The current dynamic value of this symbol.  If the
442 symbol is currently unbound, then the value of this slot is the unbound marker.
443
444 {\tt %symbol-function-slot}\\The global function function definition of this
445 symbol.  If the symbol is not fbound, then this slot holds the unbound marker.
446
447 \multiple{
448 {\tt %symbol-plist-slot} \*
449 {\tt %symbol-name-slot} \*
450 {\tt %symbol-package-slot}
451 }\\The property list, print name and package for this symbol.
452 \end{description}
453
454
455 \f
456 \section{Sub-primitives}
457
458 The {\tt alloc-symbol} sub-primitive allocates a new symbol object.  {\it name} is
459 the simple-string that is to be the name of the symbol.
460 \begin{example}
461 alloc-symbol (name) => symbol
462 \end{example}
463
464 The {\tt set-symbol-package} sub-primitive is used by system code that must set
465 the symbol package.
466 \begin{example}
467 set-symbol-package (symbol new-value)
468 \end{example}
469
470
471 \f
472 \section{Accessor VOPs}
473
474 These VOPs read the global symbol value and definition cells.  {\tt constant-ref}
475 may only be used on symbols that have been defined to be constants.  Since a
476 constant cannot change in value and cannot be dynamically bound, the compiler
477 may be able to compile uses of {\tt constant-ref} more efficiently.  Unsafe
478 versions of these VOPs may not check for the slot being unbound, which the
479 corresponding \clisp functions are required to do.
480 \begin{example}
481 {small, fast} symbol-value (symbol) => value
482 {small, fast} constant-ref (symbol) => value
483 {small, fast} symbol-function (symbol) => value
484 \end{example}
485
486 These VOPs set the global symbol value and definition cells.  {\tt makunbound}
487 and {\tt fmakunbound} are implemented by setting the value to the unbound marker.
488 \begin{example}
489 {small, fast} set-symbol-value (symbol new-value)
490 {small, fast} set-symbol-function (symbol new-value)
491 \end{example}
492
493 The \clisp accessors for other symbol slots are translated into uses of the
494 {\tt slot-ref} and {\tt slot-set} VOPs.
495
496
497 \f
498 \section{Special Binding}
499
500 These VOPs implement dynamic binding of special variables using shallow
501 binding.  {\tt bind} binds {\it symbol} to the specified {\it value}, while
502 {\tt unbind} undoes the most recent {\it count} special bindings on the binding
503 stack.
504 \begin{example}
505 bind (symbol value)
506 unbind (count)
507 \end{example}
508
509 \f
510 \section{Property Lists}
511
512 The {\tt get} VOP implements the corresponding \clisp function, while {\tt put}
513 implements its setf-inverse.
514 \begin{example}
515 get (symbol indicator default) => value
516 put (symbol indicator value)
517 \end{example}
518
519 \f
520 \chapter{Lists}
521
522
523 cons
524
525 list<n> (elt0 ... elt<n-1>) => list
526 list (elt0 ... elt<n-1> more-elts) => list
527     For some small N, we have fixed-arg versions of List.  For larger lists, we
528     pass in additional elements in a stack TN (possibly required to be on stack
529     top).  List* is similar.
530
531
532 These VOPs implement the corresponding \clisp functions:
533 \begin{example}
534 {small, fast} car (list) => value 
535 {small, fast} cdr (list) => value 
536 \end{example}
537
538 These VOPs set the car or cdr of a cons:
539 \begin{example}
540 {small, fast} set-car (cons new-value)
541 {small, fast} set-cdr (cons new-value)
542 \end{example}
543
544 These VOPs implement the \clisp {\tt assoc} and {\tt member} functions with test
545 functions of {\tt eql} and {\tt eq}:
546 \begin{example}
547 assoc (item alist) => cons-or-nil
548 assq (item alist) => cons-or-nil
549 member (item list) => cons-or-nil
550 memq (item list) => cons-or-nil
551 \end{example}
552
553
554 {\tt getf} implements the corresponding \clisp function, while {\tt putf} is used
555 to implement its setf-inverse.  {\tt putf} returns the new value for the list so
556 that it may stored back into the place.
557 \begin{example}
558 getf (list indicator default) => value
559 putf (list indicator new-value) => list
560 \end{example}
561
562 \f
563 \chapter{Numbers}
564
565 \index{Fixnum format}
566 Fixnum\\An N-bit two's complement integer.
567
568 \index{Short float format}
569 Short-Float\\An immediate float format.
570
571 \index{Bignum format}
572 \label{Bignums}
573 Bignum\\Bignums are infinite-precision integers, represented somehow.
574
575 \index{Flonum format}
576 \index{Floating point formats}
577 Floats\\Floats are stored as consecutive words of bits.
578
579 \index{Ratio format}
580 Ratio\\Ratios are stored as two consecutive words of Lisp objects, which should
581 both be integers.
582
583 \index{Complex number format}
584 Complex\\Complex numbers are stored as two consecutive words of Lisp objects,
585 which should both be numbers.
586
587
588 \f
589 \section{Number VOPs}
590
591 integer-length
592 {small, fast} integer-length/fixnum
593
594 float=>xxx-float
595
596 realpart
597 lmagpart
598 numerator
599 denominator
600 decode-float
601 {small, fast} decode-float/xxx-float
602 scale-float
603 {small, fast} scale-float/xxx-float
604
605 if-= (x y)
606 {small, fast} if-=/fixnum
607 {small, fast} if-=/xxx-float
608     Do numeric comparison of X and Y.  The codegen-info contains the
609     continuations to transfer to in the true and false cases.  Same for <, >.
610
611 + (x y) => z
612 {small, fast} +/fixnum
613 {small, fast} +/fixnum=>integer
614 {small, fast} +/xxx-float
615     Same for -, *.   Fixnum multiplication by a constant power of 2 (or near
616     power of 2) can be done by a transform.
617
618 / (x y) => z
619 {small, fast} //xxx-float
620
621 negate
622 {small, fast} negate/fixnum
623 {small, fast} negate/fixnum=>integer
624 {small, fast} negate/xxx-float
625     Ditto for Abs.
626
627 truncate (x y) => q r
628 {small, fast} truncate/fixnum
629
630 logand (x y) => z
631 {small, fast} logand/fixnum
632     Ditto for logior, logxor.
633     
634 lognot (n) => z
635 {small, fast} lognot/fixnum
636
637 ash (n x) => z
638 {small, fast} ash/fixnum
639 {small, fast} ash-c/fixnum
640
641 ldb
642 dpb
643 mask-field
644 deposit-field
645     These will only be used as a last resort.  There should be transforms that
646     turn fixnum operations with constant byte-specifiers into standard logical
647     operations.
648
649 \f
650 \section{Number Sub-primitives}
651
652
653 alloc-bignum
654 make-complex
655 make-ratio
656 lsh
657 logldb
658 logdpb
659
660
661 \f
662 \chapter{Arrays}
663
664 \cl arrays can be represented in a few different ways in \rtccl --
665 different representations have different performance advantages.  Simple
666 general vectors, simple vectors of integers, and simple strings are basic \rtccl
667  data types, and access to these structures is quicker than access to
668 non-simple (or ``complex'') arrays.  However, all multi-dimensional arrays in
669 \rtccl are complex arrays, so references to these are always through a
670 header structure.
671
672
673 Once a vector has been allocated, it is possible to reduce its length by using
674 the Shrink-Vector sub-primitive, but never to increase its length, even back to
675 the original size, since the space freed by the reduction may have been
676 reclaimed.
677
678
679 \f
680 \subsection{Arrays}
681 \label{Arrays}
682 \index{Arrays}
683
684 An array header is identical in form to a G-Vector.  At present, the following
685 subtype codes are defined:
686 \begin{itemize, spread 0, spacing 1}
687 0 Normal.
688 1 Array is displaced to another array (which may be simple).
689 \end{itemize}
690 The entries in the header-vector are interpreted as follows:
691
692 \index{Array header format}
693 \begin{description}
694 0 Data Vector \\This is a pointer to the I-Vector, G-Vector, or string that
695 contains the actual data of the array.  In a multi-dimensional array, the
696 supplied indices are converted into a single 1-D index which is used to access
697 the data vector in the usual way.  If the array is displaced, then this is
698 the array displaced to, which may be an array header.  In general, array
699 access must loop until it finds an actual data vector.
700
701 1 Number of Elements \\This is a fixnum indicating the number of elements for
702 which there is space in the data vector.
703
704 2 Fill Pointer \\This is a fixnum indicating how many elements of the data
705 vector are actually considered to be in use.  Normally this is initialized to
706 the same value as the Number of Elements field, but in some array applications
707 it will be given a smaller value.  Any access beyond the fill pointer is
708 illegal.
709
710 3 Displacement \\This fixnum value is added to the final code-vector index
711 after the index arithmetic is done but before the access occurs.  Used for
712 mapping a portion of one array into another.  For most arrays, this is 0.
713
714 4 Range of First Index \\This is the number of index values along the first
715 dimension, or one greater than the largest legal value of this index (since the
716 arrays are always zero-based).  A fixnum in the range 0 to 2\+{24}-1.  If any
717 of the indices has a range of 0, the array is legal but will contain no data
718 and accesses to it will always be out of range.  In a 0-dimension array, this
719 entry will not be present.
720
721 5 - N  Ranges of Subsequent Dimensions
722 \end{description}
723
724 The number of dimensions of an array can be determined by looking at the length
725 of the array header.  The rank will be this number minus 6.  The maximum array
726 rank is 65535 - 6, or 65529.
727
728 The ranges of all indices are checked on every access, during the conversion to
729 a single data-vector index.  In this conversion, each index is added to the
730 accumulating total, then the total is multiplied by the range of the following
731 dimension, the next index is added in, and so on.  In other words, if the data
732 vector is scanned linearly, the last array index is the one that varies most
733 rapidly, then the index before it, and so on.
734
735
736 \f
737 \section{Array VOPs}
738
739 alloc-bit-vector
740 alloc-i-vector
741 alloc-string
742 alloc-g-vector
743     Initialized and uninitialized versions?
744
745
746 length (sequence) => size
747 {small, fast} length/vector
748 {small, fast} length/simple-vector
749 {small, fast} length/simple-string
750 {small, fast} length/simple-bit-vector
751
752 aref1 (vector index) => value
753 {small, fast} aref1/simple-vector
754 {small, fast} aref1/simple-string
755 {small, fast} aref1/simple-bit-vector
756 {small, fast} aref1/simple-array-XXX-float
757
758 aset1 (vector index new-value)
759 {small, fast} aset1/simple-vector
760 {small, fast} aset1/simple-string
761 {small, fast} aset1/simple-bit-vector
762 {small, fast} aset1/simple-array-XXX-float
763
764 {small, fast} aref1/simple-array-unsigned-byte (vector index) => value
765 {small, fast} aset1/simple-array-unsigned-byte (vector index new-value)
766     Byte size is codegen info.
767
768 aref<N> (array index0 ... index<n-1>) => value
769 aset<N> (array index0 ... index<n-1> new-value)
770     For some small value of N.  Of course, higher dimensional arrays can also
771     be specialized in seven different ways....  Multi-dimensional simple array
772     reference with known dimensions can be open-coded using a transform (useful
773     for benchmarks.)
774
775
776 \f
777 \section{Array Sub-primitives}
778
779 alloc-array
780 vector-subtype
781 set-vector-subtype
782 vector-access-code
783 set-vector-access-code
784 shrink-vector
785
786 typed-vref
787 typed-vset
788
789 header-length (header) => size
790 header-ref (header index) => value
791 header-set (header index new-value)
792
793 bit-bash
794 byte-blt
795 {reverse-}find-character
796 {reverse-}find-character-with-attribute
797 {reverse-}string-compare
798 sxhash-simple-string
799 sxhash-simple-substring
800
801 \f
802 \chapter{Structures}
803
804 {small, fast} structure-ref (s) => value
805 {small, fast} structure-set (s new-value)
806     Read and write structure slots.  Defstruct slot description is in codegen
807     info.
808
809 alloc-structure
810
811 \f
812 \chapter{Runtime Environment}
813 \index{Runtime Environment}
814 \label{Runtime}
815
816 \f
817 \section{Register Allocation}
818 \index{Register allocation}
819
820 The main idea is to globally allocate only those registers with global
821 significance.
822
823 We permanently dedicate the CONT register to point to the current control stack
824 environment.  This is the "frame pointer" in standard terminology.  It isn't
825 possible to get pack to allocate this register on an as-needed basis due to the
826 classic phase-ordering problem.  We need to know if TNs are allocated on the
827 stack before we can determine tell how badly we need a frame pointer register.
828 This is of little significance with the control stack environment, since we
829 almost always need one, and if there are any stack TNs, we must allocate the
830 frame pointer in a register, since there is nowhere else to put it.  The
831 problem is more severe with a number stack environment pointer.  We can't
832 dedicate a register to it, since we usually don't have any TNs on the number
833 stack.  The only easy solution is to always allocate the number stack
834 environment pointer on the control stack.  This really isn't too bad, when you
835 compare the cost of doing an extra memory reference to get at the number stack
836 to the cost of number-consing.
837
838 We also dedicate the ENV register to the current constant pool.  It would be
839 possible to explicitly allocate the constant pointer as needed if we explicitly
840 represented non-immediate constant access by a VOP, but this would be extra
841 work, and there are major advantages to representing all constants using TNs.
842 Another potential efficiency advantage is since the same constant pool is
843 shared by all the code in a component, we need only initialize ENV on entry to
844 the component.  When we make local calls, we don't have to do anything to make
845 the constants available to the callee.
846
847 Since the constant pool will also contain the code vector and the debug info,
848 having it in a known place may make life easier for GC and the debugger.  We
849 may not be able to count on it too much, though, since ENV holds other things
850 will calls are in progress, and might be pretty random if we jumped into
851 hyperspace.
852
853 \f
854 Runtime environment:
855
856 CONT: the current control stack context.
857 PC is assumed to be accessible to the debugger when an error happens.
858 Current-Catch: pointer to the current catch frame.  Format of frame is assumed.
859 Current-Unwind-Protect: current unwind protect frame.  Similar to catch.
860
861 If shallow-bind, binding stack and binding stack pointer.
862 If deep-bind, current special binding.  Format of binding frame assumed.
863
864 Everything depends on the current environment, which is CONT.
865
866
867 PC
868 OLD-CONT
869 ENV
870 A<n>
871 CONT
872 CS
873
874 \f
875 \section{Other Dynamic State}
876
877 There are some dynamic state variables that are stored in known memory
878 locations, rather than having a dedicated register:
879 \begin{description}
880 binding stack pointer\\The current pointer to the top of the binding stack.
881
882 current catch\\The pointer to the current catch block.
883
884 current unwind-protect\\The pointer to the current unwind-protect block.
885 \end{description}
886
887
888
889 \f
890 \section{Control-Stack Format}
891 \label{Control-Stack-Format}
892 \index{Control-stack format}
893
894
895 The control stack contains only Lisp objects.  Every object pointed to by an
896 entry on this stack is kept alive.
897
898 The \rtccl control stack does not have a rigid frame structure.  The compiler
899 is allowed a large amount of freedom in the use of the stack so that it choose
900 the best calling sequences.  Mostly the compiler is the only system that cares
901 how the stack is laid out, so this isn't a big problem.  See chapter
902 \ref{debug-info} for a description of the structures which allow the debugger
903 to parse the stack.
904
905
906
907 \section{Values Passing Conventions}
908
909
910 The first {\it nregs} arguments are passed in registers, where nregs is an
911 implementation dependent constant.  Any additional arguments are the block of
912 storage between CONT and CS on the control stack.  The first nregs locations in
913 this block of storage are unused so that register more-args can be stored on
914 the stack without having to BLT the stack values up.
915
916 Returning unknown values are passed in a similar way, but the stack values
917 block is between OLD-CONT and CS.  There isn't any underneath the values: on
918 return OLD-CONT is always what CS was when the function was called.  The
919 function returned to must copy the values into the desired location in its
920 frame and deallocate excess stuff on the top of the stack.
921
922 More args are represented by a pointer to the block of values and a count.  The
923 function that originally created the more arg must allocate and deallocate this
924 stuff somehow.  In the case of a local call to a more arg entry, we can just
925 allocate it as a TN.  The external entry point for a more arg entry is more
926 magical.
927
928
929
930 The caller allocates the environment for the called function, stores the
931 arguments into it, and jumps to the function.  The caller makes the called
932 environment current, passing in the return OLD-CONT and PC as explicit arguments.
933
934 When returning values, the returner directly stores the return values into the
935 frame being returned to.  This works even though the caller doesn't know what
936 function it is returning to, since the same return locations are allocated in
937 all frames.
938
939 In a tail-recursive call, we can destructively modify the current frame and
940 jump right to the callee, rather than allocating a new frame.  We can do this
941 because TNBind globally allocates frame locations; all frames are the same size
942 and have the same TNs in the same place.
943
944 \f
945 \section{Binding-Stack Format}
946 \index{Binding stack format}
947 \comment<In a symbol chapter?>
948
949
950 The special binding stack is used to hold previous values of special variables
951 that have been bound.  It grows and shrinks with the depth of the binding
952 environment, as reflected in the control stack. This stack contains
953 symbol-value pairs, with only boxed Lisp objects present.
954
955 Each entry of the binding-stack consists of two boxed (32-bit) words.  Pushed
956 first is a pointer to the symbol being bound.  Pushed second is the symbol's
957 old value (any boxed item) that is to be restored when the binding stack is
958 popped.
959
960 \f
961 \chapter{Functions}
962
963 Function calling is a way of life.  
964
965 every function is a closure.  pointer to current closure is passed in ENV
966 unless it isn't (in local call may be elsewhere).
967
968 The description of the representation of functions and the function calling
969 conventions is a large part of the VM description, since:
970     Function calling is one of the most complicated facilities provided by the
971     VM.
972
973     Everything that happens, happens in a function, so all parts of the system
974     tend to get dragged in.
975
976
977 Aspects of function call:
978     Control
979     Environment CONT, ENV
980     Argument/value passing
981     Argument/value count dispatching
982
983
984
985 \f
986 \section{Function Object Format}
987 \label{Fn-Format}
988
989 The old notion of a "function object" is now broken down into four different
990 parts:
991 \begin{description}
992 Function entry\\A function entry is a structure that holds the information
993 that we need to call a function.  This is the user visible function object.
994
995 Environment\\The environment is stuff that a function needs when it runs.
996 This includes constants computed at load time and variables closed over at run
997 time.  Environment information may be allocated in the function entry structure
998 after the required linkage information.
999
1000 Entry information\\This is information about a specific function entry that is
1001 occasionally referenced at run time, but need not be immediately accessible.
1002 Entry information will be either allocated in the function entry
1003 or in the environment that it points to.
1004
1005 Debug information\\This is information about a function that isn't normally
1006 needed at run time.  Debug information can be found by poking around in
1007 environment objects.
1008 \end{description}
1009 See chapter \ref{control-conventions} for a description of how function objects
1010 are used.
1011
1012 \f
1013 \section{Environment Object Sub-primitives}
1014
1015 alloc-code ?
1016 alloc-closure?
1017
1018 \f
1019 \subsection{Debug Information Location}
1020
1021 If present, debug information is stored immediately following any fixed
1022 information in the environment object.  It may be necessary to chain up
1023 multiple levels of environments to find the debug information.  The debug
1024 information can be recognized because it is represented by a defstruct
1025 structure.  See chapter \ref{debug-info} for a description of the debug
1026 information.
1027
1028 \f               
1029 \section{Function Calls}
1030 \index{function call}
1031
1032 \ccl supports three major calling conventions.  The convention used
1033 depends on the amount of information available at compile time:
1034 \begin{description}
1035 Local\\Local call is used when the call and the called function are
1036 compiled at the same time.  Using the term "convention" to describe this
1037 call mechanism is somewhat of a misnomer, since the compiler can do
1038 whatever it wants.
1039
1040 Named\\Named call is used when the call is to a global function whose name
1041 is known at compile time.
1042
1043 Anonymous\\Anonymous call is used when the function called is unknown until
1044 run time.
1045 \end{description}
1046
1047 \#|
1048 IR2 function call:
1049
1050 Environment manipulation code is always emitted at the location of the Bind or
1051 Return node for a Lambda. 
1052
1053 Implicit args to functions in IR2:
1054   old-cont: cont to restore on return
1055   return-pc: pc to return to
1056   env: pointer to current closure (if heap)
1057   closure<n>: closed values for current closure (if stack)
1058
1059 Other info needed for IR2 conversion of functions:
1060     base pointers for all heap closures consed by this function
1061     also have passing locs for each explicit arg
1062     return strategy (known or unknown) and return locs
1063
1064 All arguments including implicit ones must have both a passing TN and a
1065 permanent TN.  Passing locs for let calls can be the actual TN that holds the
1066 variable in the case of local variables.  Set closure variables must still have
1067 a separate passing TN.
1068
1069 If we know the values counts for the argument continuations, then we compile
1070 local mv-calls by moving the TNs for the values continuations into the argument
1071 passing locations.  Other mv-calls must be compiled using various hairy
1072 stack-hacking VOPs and unknown argument count call VOPs.
1073
1074 For now, we will create the callee's frame just before the call, instead of
1075 creating it before the evaluation of the first argument.  If we created the
1076 environment early, then we would be able to move the argument values directly
1077 into the frame, instead of having to store them somewhere else for a while.
1078 The problem with early creation is that lifetime analysis gets confused because
1079 there is more than one instance of the same TN present simultaneously in the
1080 case where there are nested calls to the same function.
1081
1082 It turns out that there isn't a problem with a simple self-call, because the TN
1083 in the called frame is really the "same" TN as the one in the current frame,
1084 due to the restricted way in which we use the passing TNs.
1085
1086 We emit code for external entry points during IR2 conversion.  The external
1087 entry point is the place where we start running in a full call from a
1088 function-entry.  It does arg count checking and dispatching, moves the
1089 arguments into the passing locations for the for the lambda being called, and
1090 calls the lambda, moving the results into the standard locations if there
1091 aren't there already.
1092 |\#
1093
1094
1095 In IR2, the environment manipulation semantics of function call are decoupled
1096 from the control semantics.  When allocating closure variables for a Let, it is
1097 possible to do environment manipulation with only the normal sequential control
1098 flow.  In the case of a Let call with the same environment, we neither
1099 manipulate the environment nor transfer control; we merely initialize the
1100 variables with Move VOPs.
1101
1102 If a local function returns a known number of values which is less than the
1103 number expected by the caller, then additional code must be inserted at the
1104 return site which sets the unused values to NIL.
1105
1106 The full function call mechanism must effectively be a subset of the local call
1107 mechanism, since the two mechanisms must mesh at entry points and full function
1108 calls.  A full call turns into some kind of full call VOP.  There are different
1109 VOPs for calling named functions and closures.  We also have tail-recursive
1110 full call VOPs.  Arguments are set up using Move VOPs, just as for local call.
1111 The only difference is that the passing locations and conventions are
1112 restricted to the standard ones.
1113
1114 The gory details of arg count checking and dispatching are buried in the
1115 Function-Entry VOP, which takes a functional and a list of continuations, one
1116 pointing to each external entry.
1117
1118 \f
1119 \subsection{Local Call}
1120 \index{local call}
1121
1122 Named and anonymous call are called full calls, to distinguish them from
1123 local call.  When making full calls, the compiler must make many worst-case
1124 assumptions that aren't necessary in a local call.  The advantage of local
1125 call is that the compiler can choose to use only those parts of the full
1126 call sequence that are actually necessary. 
1127
1128 In local call, we always know the function being called, so we never have
1129 to do argument count checking, and can always use an immediate branch for
1130 the control transfer.  If the function doesn't return to more than one
1131 place, then can just use a simple branch, or even drop through.
1132
1133 The argument passing TNs may be allocated anywhere.  The caller allocates the
1134 stack frame for the called function, moving any non-register arguments into the
1135 passing locations in the callee's frame.
1136
1137 If we are calling a local function that doesn't always return the same
1138 number of values, then we must use the same values returning mechanism that
1139 is used in full call, but we don't have to use the standard registers.
1140
1141 A tail-recursive local call doesn't require any call VOP.  We just use Move
1142 VOPs to put the arguments into the passing locations and then jump to the the
1143 start of the code for the function.  We don't have to do any stack hackery
1144 since we use the same stack frame format for all the functions compiled at the
1145 same time.  In many cases tail-recursive local calls can be entirely optimized
1146 away, since they involve only some moves and a branch.  We preference the
1147 argument values to the passing locations of the called function, making it
1148 likely that no move will be necessary.  Often the control transfer can be done
1149 by simply dropping through.
1150
1151 We have to do some funny stuff with local calls in order to get the lifetimes
1152 for the passing locations right, since lifetime analysis skips directly from
1153 the return point to the call point, ignoring the uses of the passing locations
1154 in the called function.  Similarly, we pretend that a block ending in a return
1155 has no successors.
1156
1157 call-local (arg*) "fun" => value
1158 multiple-call-local (arg*) "fun" => start end val0 ... val<n>
1159     Call-Local is used for calls to local functions that are forced to use the
1160     unknown-values passing convention.  Value is the first return value
1161     register; we don't really do anything to it, but we specify it as a result
1162     to represent the assignment done by the calling function.
1163
1164     Multiple-Call-Local is similar, but specifies all the values used by the
1165     unknown-values convention.  Default-Values may be used to receive a
1166     specific number of values.
1167
1168 known-call-local (arg*) "fun" => value*
1169     This VOP is used for local calls to functions where we can determine at
1170     compile time that the number of values returned is always the same.  In
1171     this case, we don't need to indicate the number of values, and can pass
1172     them in separate TNs.  The Values are the actual return locations.  We
1173     don't really do anything to the return values; we just specify them as
1174     results to represent the assignment done by the called function.
1175
1176 known-return (return-pc value*) "fun"
1177     This VOP is used for returning from local calls using the known return
1178     values convention.  The specified return Values are moved into the passing
1179     locations in the caller's frame.
1180
1181
1182 If we know that the function we are calling is non-recursive, then we can
1183 compile it much like a tail-recursive call.  We must have a call VOP to compute
1184 the return PC, but we don't need to allocate a frame or save registers.  We
1185 just set up the arguments in the frame and do the call.
1186
1187 We require simple functions to use the known-values convention.  It would be
1188 possible to support unknown values, but it would potentially require BLT'ing
1189 return values out of the frame and on to the top of the stack.  Supporting
1190 unknown values would also require a bunch more VOPs, since we need different
1191 call and return VOPs for simple call.
1192
1193 Known values return causes no problems, since the callee knows how many values
1194 are wanted.  We store the values directly into the current frame, since it is
1195 also the caller's frame.
1196
1197 known-call-simple () "fun" => return-pc
1198 known-return-simple (return-pc) "fun"
1199     Similar to the non-simple VOPs, but don't allocate or deallocate frames,
1200     and assume that argument and value passing is done with explicit Move VOPs.
1201
1202 \f
1203 \subsection{Full Call}
1204 \index{full call}
1205
1206 Both named and anonymous call are optimized for calls where the number of
1207 arguments is known at compile time.  Unknown argument calls are a
1208 pathological case of anonymous call; this case will be ignored in the main
1209 discussion.  The difference between named and anonymous calls is in the
1210 argument count dispatching mechanism.
1211
1212 Named call allows an arbitrary number of entry points, with start PCs at
1213 arbitrary locations in the code vector.  The link-table mechanism described
1214 below allows named calls to jump directly to the actual entry point without any
1215 run-time argument count or type checking checking.
1216
1217 Anonymous call has a fixed number of entry points, with start PCs at fixed
1218 locations in the code vector.  This allows calls to be made without knowing
1219 what function is being called, but has more run-time overhead.  The object
1220 called must be checked to be a valid function-entry object.  The entry PC must
1221 be computed from the function entry, and argument count checking must be done
1222 if there are more than three required or optional arguments.
1223
1224 Argument passing in full call is conceptually similar to local call, but the
1225 caller can't allocate the entire frame for the callee, since it doesn't know
1226 how much stack is needed.  Instead we allocate the frame in two parts.  The
1227 caller only allocates the beginning of the frame, which contains the stack
1228 arguments in fixed locations.  We leave the first <n> locations unused so that
1229 the called function can move register more args onto the stack without having
1230 to BLT down any stack arguments.
1231
1232 The place in the code where a full call jumps in is called an external entry
1233 point.  The external entry point allocates the rest of the stack frame and then
1234 does a local call to the actual entry-point function, fetching the arguments
1235 from the standard passing locations.  Usually we can do a tail-recursive local
1236 call.  
1237
1238 There are two main cases where the call from the external entry point cannot be
1239 tail-recursive:
1240  -- It is desirable to use the known-values convention for calling the
1241     entry-point function if the entry-point is used in other local calls
1242     (perhaps because of recursion).  In this case, the called function stores
1243     the return values back into the frame allocated by the external entry point
1244     and then returns back to it.  The external entry point must then return
1245     these values using the standard unknown-values convention.
1246  -- In a more-arg entry point we don't know how many stack arguments there are
1247     at the beginning of the frame, so we can't really use the frame allocated
1248     by the external entry point at all.  Instead we do a local call to the
1249     more-arg entry point, passing in a pointer to the first extra value.  When
1250     the function returns, we deallocate the crap on the stack and then return
1251     the values.  It is still o.k. to use the known-values return convention
1252     from the more-arg entry since the extra arg values are no longer needed by
1253     the time the returning function stores the return values back into the
1254     external entry point frame.
1255
1256
1257 In full call we must always use the unknown-values convention for return.  The
1258 first <n> values are passed in the standard argument registers.  The Old-Cont
1259 register holds the Start of the values block and SP points to the End.
1260
1261
1262 {small, fast} call (function arg0 ... arg<n>) "nargs" => value
1263 {small, fast} call-named (arg0 ... arg<n>) "nargs" "name" => value
1264     Call-Closure calls Function with the specified register arguments,
1265     returning the first value as the result.  "nargs" is the total number of
1266     arguments passed.  Only the register arguments actually passed should be
1267     specified as operands.
1268
1269     Call-Named is similar, but calls a global function specified at compile
1270     time by "name".
1271
1272 {small, fast} tail-call (function pc arg0 ... arg<n>) "nargs"
1273 {small, fast} tail-call-named (pc arg0 ... arg<n>) "nargs" "name"
1274     Similar to the standard call VOPs, but passes PC as the return PC, rather
1275     than returning to the call site.  These VOPs have no results since they
1276     don't return.
1277
1278 {small, fast} multiple-call (function arg0 ... arg<n>) "nargs"
1279                                     => start end val0 ... val<n>
1280 {small, fast} multiple-call-named (arg0 ... arg<n>) "nargs" "name"
1281                                   => start end val0 ... val<n>
1282     These VOPs are similar to the standard call VOPs, but allow any number of 
1283     values to be received by returning all the value passing registers as
1284     results.  A specific number of values may be received by using
1285     Default-Values. 
1286
1287 call-unknown (function count arg0 ... arg<n>) => start end val0 ... val<n>
1288 tail-call-unknown (function pc count arg0 ... arg<n>)
1289     Call a function with an unknown number of arguments.  Used for apply and
1290     hairy multiple-value-call.
1291
1292 Function-Entry () "function" => env return-pc old-cont arg*
1293     This marks the place where we jump into a component for an external
1294     entry point.  It represents whatever magic is necessary to do argument
1295     count checking and dispatching.  The external entry points for each
1296     argument count will be successors of the entry-vector block (might be in
1297     the same block if only one?)
1298
1299     Function-Entry also represents argument passing by specifying the actual
1300     external passing locations as results, thus marking the beginning of their
1301     lifetimes.  All passing locations actually used by any entry point are
1302     specified as Args, including stack arguments.
1303    {\#\#\# Do we really need this?  If we do, then we probably also need similar
1304     entry markers for local functions.  The lifetimes don't really need to be
1305     explicitly bounded, since an entry point is effectively "the end of the
1306     world."}
1307
1308 \f
1309 \section(Returning from a Function Call)
1310 \label(Return)
1311 \index(Return)
1312
1313
1314 return (return-pc value)
1315 multiple-return (return-pc start end val0 ... val<n>)
1316     Return Value from the current function, jumping back to the location
1317     specified by Return-PC. {Perhaps allow to return any fixed, known number
1318     of values.}
1319
1320     Multiple-Return is similar, but allows an arbitrary number of values to be
1321     returned.  End - Start is the total number of values returned.  Start
1322     points to the beginning of the block of return values, but the first <n>
1323     values val0 ... val<n> are actually returned in registers.
1324
1325 default-values (start end val0 ... val<n>) => val0 ... val<j>
1326     This VOP is used when we want to receive exactly J values.  If fewer than J
1327     values were supplied, then missing values are defaulted to NIL.  As a
1328     side-effect, this VOP pops off any returned stack values.
1329
1330 \f
1331 \section{Saving and Restoring Registers}
1332
1333 We use a caller-saves convention.  The caller explicitly emits saving and
1334 restoring code.  Tail-recursive calls don't need
1335 any register saving since we never come back.
1336
1337
1338 \f
1339 \chapter{Non-local exits}
1340
1341 \f
1342 \subsection{Unwind Blocks}
1343 \index{Catch}
1344 \index{Catch frames}
1345
1346 There is one aspect of the control stack format that is fixed, and which
1347 concerns us at this level.  This is the format of the "frames" which mark the
1348 destination of non-local exits, such as for BLOCK and CATCH.  These frames are
1349 collectively known as unwind blocks.  The basic unwind block is used for
1350 lexical exists such as BLOCK, and for UNWIND-PROTECT.  Its format is the
1351 following:
1352 \begin{verbatim}
1353 0   Pointer to current unwind-protect.
1354 1   Control stack context to restore on entry.
1355 2   PC to enter at.
1356 \end{verbatim}
1357
1358 The unwind block for CATCH is identical except for additional cells
1359 containing the catch tag and previous catch.
1360 \begin{verbatim}
1361 0   Pointer to current unwind-protect.
1362 1   Control stack context to restore on entry.
1363 2   PC to enter at.
1364 3   Catch tag.
1365 4   Previous catch.
1366 \end{verbatim}
1367
1368 The conventions used to manipulate unwind blocks are described in chapter
1369 \ref{Control-Conventions}.
1370
1371
1372 \f
1373 \section{Non-Local Exits}
1374 \label{Catch}
1375 \index{Catch}
1376 \index{Throw}
1377 \index{Unwinding}
1378 \index{Unwind-Protect}
1379 \index{Non-Local Exits}
1380
1381 In the normal flow of control, each function that is called executes until it
1382 reaches a return point; under these conditions no special effort is needed to
1383 restore the environment as long as each function undoes any change that it
1384 makes to the dynamic state before it returns.  When we make a non-local
1385 transfer, we skip a potentially arbitrary collection of these cleanup actions.
1386 Since we cannot in general know what changes have been made to the dynamic
1387 environment below us on the stack, we must restore a snapshot of the dynamic
1388 environment at the re-entry point.
1389
1390 We represent the closed continuation by the pointer to the unwind-block for the
1391 reentry point.  At the exit point, we just pass this stack pointer to the
1392 Unwind VOP, which deals with processing any unwind-protects.  When Unwind is
1393 done, it grabs the re-entry PC out of the location at the stack pointer and
1394 jumps in.
1395
1396 Catch and Unwind-Protect work in pretty much the same way.  We make a stack TN
1397 to hold the catch frame or whatever, allocate TNs in them to represent the
1398 slots, and then initialize them.  The frame can be explicitly linked in by TN
1399 manipulations, since the active catch and whatnot are represented by TNs.
1400 Since allocation of the frame is decoupled from linking and unlinking, some of
1401 this stuff could be moved out of loops.  We will need a VOP for loading the PC
1402 for an arbitrary continuation so that we can set up the reentry PC.  This can
1403 be done using the Call VOP.  Using a call instruction is probably a good way to
1404 get a PC on most architectures anyway.
1405
1406 These TNs are allocated by Pack like any others; we use special alloc and
1407 dealloc VOPs to delimit the aggregate lifetimes.
1408
1409 In the non-local case, the the Block, Catch and Unwind-Protect special forms
1410 are implemented using unwind blocks.  The unwind blocks are built by move
1411 operations emitted inline by the compiler.  The compiler adds and removes
1412 catches and unwind protects by explicit moves to the locations that hold the
1413 current catch and unwind protect blocks.  The entry PC is loaded using the Call
1414 VOP.
1415
1416 The Unwind miscop is the basis non-local exits.  It takes the address of an
1417 unwind block and processes unwind-protects until the current unwind-protect is
1418 the one recorded in the unwind block, then jumps in at the entry in the unwind
1419 block.  The entry for the unwind block is responsible for restoring any state
1420 other than the current unwind-protect.  
1421
1422 Unwind is used directly to implement non-local Return-From.  The address of the
1423 unwind block is stored in a closure variable.
1424
1425 Catch just does a scan up the chain of Catch blocks, starting at the current
1426 catch.  When it finds the right one, it calls unwind on it.
1427
1428 Unwind-protects are represented by unwind blocks linked into the current
1429 unwind-protect chain.  The cleanup code is entered just like any other any
1430 other unwind entry.  As before, the entry is responsible for establishing the
1431 correct dynamic environment for the cleanup code.  The target unwind block is
1432 passed in some non-argument register.  When the cleanup code is done, it
1433 just calls Unwind with the block passed in.  The cleanup code must be careful
1434 not to trash the argument registers or CS, since there may be multiple values
1435 lurking out there.
1436
1437 With Catch/Throw, we always use the variable values return value passing convention,
1438 since we don't know how many values the catch wants.  With Block/Return-From,
1439 we can do whatever we want, since the returner and receiver know each other.
1440
1441 If a Block or Catch receives stack values, it must call a VOP that BLT's the
1442 values down the stack, squeezing out any intermediate crud.
1443
1444
1445 unwind (context)
1446 throw (tag)
1447     Unwind does a non-local exit, unwinding to the place indicated by Context.
1448     Context is a pointer to a block of storage allocated on the control stack,
1449     containing the entry PC, current environment and current unwind-protect.
1450     We scan up the stack, processing unwind-protects until we reach the entry
1451     point.  The values being returned are passed in the standard locations.
1452     Throw is similar, but does a dynamic lookup for the Tag to determine what
1453     context to unwind to.
1454