Initial revision
[sbcl.git] / doc / cmucl / internals / compiler-overview.tex
1 \chapter{Compiler Overview} % -*- Dictionary: design -*-
2
3 The structure of the compiler may be broadly characterized by describing the
4 compilation phases and the data structures that they manipulate.  The steps in
5 the compilation are called phases rather than passes since they don't
6 necessarily involve a full pass over the code.  The data structure used to
7 represent the code at some point is called an {\it intermediate
8 representation.}
9
10 Two major intermediate representations are used in the compiler:
11 \begin{itemize}
12
13 \item The Implicit Continuation Representation (ICR) represents the lisp-level
14 semantics of the source code during the initial phases.  Partial evaluation and
15 semantic analysis are done on this representation.  ICR is roughly equivalent
16 to a subset of Common Lisp, but is represented as a flow-graph rather than a
17 syntax tree.  Phases which only manipulate ICR comprise the "front end".  It
18 would be possible to use a different back end such as one that directly
19 generated code for a stack machine.
20
21 \item The Virtual Machine Representation (VMR) represents the implementation of
22 the source code on a virtual machine.  The virtual machine may vary depending
23 on the the target hardware, but VMR is sufficiently stylized that most of the
24 phases which manipulate it are portable.
25 \end{itemize}
26
27 Each phase is briefly described here.  The phases from ``local call analysis''
28 to ``constraint propagation'' all interact; for maximum optimization, they
29 are generally repeated until nothing new is discovered.  The source files which
30 primarily contain each phase are listed after ``Files: ''.
31 \begin{description}
32
33 \item[ICR conversion]
34 Convert the source into ICR, doing macroexpansion and simple source-to-source
35 transformation.  All names are resolved at this time, so we don't have to worry
36 about name conflicts later on.  Files: {\tt ir1tran, srctran, typetran}
37
38 \item[Local call analysis] Find calls to local functions and convert them to
39 local calls to the correct entry point, doing keyword parsing, etc.  Recognize
40 once-called functions as lets.  Create {\it external entry points} for
41 entry-point functions.  Files: {\tt locall}
42
43 \item[Find components]
44 Find flow graph components and compute depth-first ordering.  Separate
45 top-level code from run-time code, and determine which components are top-level
46 components.  Files: {\tt dfo}
47
48 \item[ICR optimize] A grab-bag of all the non-flow ICR optimizations.  Fold
49 constant functions, propagate types and eliminate code that computes unused
50 values.  Special-case calls to some known global functions by replacing them
51 with a computed function.  Merge blocks and eliminate IF-IFs.  Substitute let
52 variables.  Files: {\tt ir1opt, ir1tran, typetran, seqtran, vm/vm-tran}
53
54 \item[Type constraint propagation]
55 Use global flow analysis to propagate information about lexical variable
56 types.   Eliminate unnecessary type checks and tests.  Files: {\tt constraint}
57
58 \item[Type check generation]
59 Emit explicit ICR code for any necessary type checks that are too complex to be
60 easily generated on the fly by the back end.  Files: {\tt checkgen}
61
62 \item[Event driven operations]
63 Various parts of ICR are incrementally recomputed, either eagerly on
64 modification of the ICR, or lazily, when the relevant information is needed.
65 \begin{itemize}
66 \item Check that type assertions are satisfied, marking places where type
67 checks need to be done.
68
69 \item Locate let calls.
70
71 \item Delete functions and variables with no references
72 \end{itemize}
73 Files: {\tt ir1util}, {\tt ir1opt}
74
75 \item[ICR finalize]
76 This phase is run after all components have been compiled.  It scans the
77 global variable references, looking for references to undefined variables
78 and incompatible function redefinitions.  Files: {\tt ir1final}, {\tt main}.
79
80 \item[Environment analysis]
81 Determine which distinct environments need to be allocated, and what
82 context needed to be closed over by each environment.  We detect non-local
83 exits and set closure variables.  We also emit cleanup code as funny
84 function calls.  This is the last pure ICR pass.  Files: {\tt envanal}
85
86 \item[Global TN allocation (GTN)]
87 Iterate over all defined functions, determining calling conventions
88 and assigning TNs to local variables.  Files: {\tt gtn}
89
90 \item[Local TN allocation (LTN)]
91 Use type and policy information to determine which VMR translation to use
92 for known functions, and then create TNs for expression evaluation
93 temporaries.  We also accumulate some random information needed by VMR
94 conversion.  Files: {\tt ltn}
95
96 \item[Control analysis]
97 Linearize the flow graph in a way that minimizes the number of branches.  The
98 block-level structure of the flow graph is basically frozen at this point.
99 Files: {\tt control}
100
101 \item[Stack analysis]
102 Maintain stack discipline for unknown-values continuation in the presence
103 of local exits.  Files: {\tt stack}
104
105 \item[Entry analysis]
106 Collect some back-end information for each externally callable function.
107
108 \item[VMR conversion] Convert ICR into VMR by translating nodes into VOPs.
109 Emit type checks.  Files: {\tt ir2tran, vmdef}
110
111 \item[Copy propagation] Use flow analysis to eliminate unnecessary copying of
112 TN values.  Files: {\tt copyprop}
113
114 \item[Representation selection]
115 Look at all references to each TN to determine which representation has the
116 lowest cost.  Emit appropriate move and coerce VOPS for that representation.
117
118 \item[Lifetime analysis]
119 Do flow analysis to find the set of TNs whose lifetimes 
120 overlap with the lifetimes of each TN being packed.  Annotate call VOPs with
121 the TNs that need to be saved.  Files: {\tt life}
122
123 \item[Pack]
124 Find a legal register allocation, attempting to minimize unnecessary moves.
125 Files: {\tt pack}
126
127 \item[Code generation]
128 Call the VOP generators to emit assembly code.  Files: {\tt codegen}
129
130 \item[Pipeline reorganization] On some machines, move memory references
131 backward in the code so that they can overlap with computation.  On machines
132 with delayed branch instructions, locate instructions that can be moved into
133 delay slots.  Files: {\tt assem-opt}
134
135 \item[Assembly]
136 Resolve branches and convert in to object code and fixup information.
137 Files: {\tt assembler}
138
139 \item[Dumping] Convert the compiled code into an object file or in-core
140 function.  Files: {\tt debug-dump}, {\tt dump}, {\tt vm/core}
141
142 \end{description}
143
144 \chapter{The Implicit Continuation Representation}
145
146 The set of special forms recognized is exactly that specified in the Common
147 Lisp manual.  Everything that is described as a macro in CLTL is a macro.
148
149 Large amounts of syntactic information are thrown away by the conversion to an
150 anonymous flow graph representation.  The elimination of names eliminates the
151 need to represent most environment manipulation special forms.  The explicit
152 representation of control eliminates the need to represent BLOCK and GO, and
153 makes flow analysis easy.  The full Common Lisp LAMBDA is implemented with a
154 simple fixed-arg lambda, which greatly simplifies later code.
155       
156 The elimination of syntactic information eliminates the need for most of the
157 "beta transformation" optimizations in Rabbit.  There are no progns, no
158 tagbodys and no returns.  There are no "close parens" which get in the way of
159 determining which node receives a given value.
160
161 In ICR, computation is represented by Nodes.  These are the node types:
162 \begin{description}
163 \item[if]  Represents all conditionals.
164
165 \item[set] Represents a {\tt setq}.
166
167 \item[ref] Represents a constant or variable reference.
168
169 \item[combination] Represents a normal function call.
170
171 \item[MV-combination] Represents a {\tt multiple-value-call}.  This is used to
172 implement all multiple value receiving forms except for {\tt
173 multiple-value-prog1}, which is implicit.
174
175 \item[bind]
176 This represents the allocation and initialization of the variables in
177 a lambda.
178
179 \item[return]
180 This collects the return value from a lambda and represents the
181 control transfer on return.
182
183 \item[entry] Marks the start of a dynamic extent that can have non-local exits
184 to it.  Dynamic state can be saved at this point for restoration on re-entry.
185
186 \item[exit] Marks a potentially non-local exit.  This node is interposed
187 between the non-local uses of a continuation and the {\tt dest} so that code to
188 do a non-local exit can be inserted if necessary.
189 \end{description}
190
191 Some slots are shared between all node types (via defstruct inheritance.)  This
192 information held in common between all nodes often makes it possible to avoid
193 special-casing nodes on the basis of type.  This shared information is
194 primarily concerned with the order of evaluation and destinations and
195 properties of results.  This control and value flow is indicated in the node
196 primarily by pointing to continuations.
197
198 The {\tt continuation} structure represents information sufficiently related
199 to the normal notion of a continuation that naming it so seems sensible.
200 Basically, a continuation represents a place in the code, or alternatively the
201 destination of an expression result and a transfer of control.  These two
202 notions are bound together for the same reasons that they are related in the
203 standard functional continuation interpretation.
204
205 A continuation may be deprived of either or both of its value or control
206 significance.  If the value of a continuation is unused due to evaluation for
207 effect, then the continuation will have a null {\tt dest}.  If the {\tt next}
208 node for a continuation is deleted by some optimization, then {\tt next} will
209 be {\tt :none}.
210
211   [\#\#\# Continuation kinds...]
212
213 The {\tt block} structure represents a basic block, in the the normal sense.
214 Control transfers other than simple sequencing are represented by information
215 in the block structure.  The continuation for the last node in a block
216 represents only the destination for the result.
217
218 It is very difficult to reconstruct anything resembling the original source
219 from ICR, so we record the original source form in each node.  The location of
220 the source form within the input is also recorded, allowing for interfaces such
221 as "Edit Compiler Warnings".  See section \ref{source-paths}.
222
223 Forms such as special-bind and catch need to have cleanup code executed at all
224 exit points from the form.  We represent this constraint in ICR by annotating
225 the code syntactically within the form with a Cleanup structure describing what
226 needs to be cleaned up.  Environment analysis determines the cleanup locations
227 by watching for a change in the cleanup between two continuations.  We can't
228 emit cleanup code during ICR conversion, since we don't know which exits will
229 be local until after ICR optimizations are done.
230
231 Special binding is represented by a call to the funny function %Special-Bind.
232 The first argument is the Global-Var structure for the variable bound and the
233 second argument is the value to bind it to.
234
235 Some subprimitives are implemented using a macro-like mechanism for translating
236 %PRIMITIVE forms into arbitrary lisp code.  Subprimitives special-cased by VMR
237 conversion are represented by a call to the funny function %%Primitive.  The
238 corresponding Template structure is passed as the first argument.
239
240 We check global function calls for syntactic legality with respect to any
241 defined function type function.  If the call is illegal or we are unable to
242 tell if it is legal due to non-constant keywords, then we give a warning and
243 mark the function reference as :notinline to force a full call and cause
244 subsequent phases to ignore the call.  If the call is legal and is to a known
245 function, then we annotate the Combination node with the Function-Info
246 structure that contains the compiler information for the function.
247
248 \f
249 \section{Tail sets}
250 \#|
251 Probably want to have a GTN-like function result equivalence class mechanism
252 for ICR type inference.  This would be like the return value propagation being
253 done by Propagate-From-Calls, but more powerful, less hackish, and known to
254 terminate.  The ICR equivalence classes could probably be used by GTN, as well.
255
256 What we do is have local call analysis eagerly maintain the equivalence classes
257 of functions that return the same way by annotating functions with a Tail-Info
258 structure shared between all functions whose value could be the value of this
259 function.  We don't require that the calls actually be tail-recursive, only
260 that the call deliver its value to the result continuation.  [\#\#\# Actually
261 now done by ICR-OPTIMIZE-RETURN, which is currently making ICR optimize
262 mandatory.]
263
264 We can then use the Tail-Set during ICR type inference.  It would have a type
265 that is the union across all equivalent functions of the types of all the uses
266 other than in local calls.  This type would be recomputed during optimization
267 of return nodes.  When the type changes, we would propagate it to all calls to
268 any of the equivalent functions.  How do we know when and how to recompute the
269 type for a tail-set?  Recomputation is driven by type propagation on the result
270 continuation.
271
272 This is really special-casing of RETURN nodes.  The return node has the type
273 which is the union of all the non-call uses of the result.  The tail-set is
274 found though the lambda.  We can then recompute the overall union by taking the
275 union of the type per return node, rather than per-use.
276
277
278 How do result type assertions work?  We can't intersect the assertions across
279 all functions in the equivalence class, since some of the call combinations may
280 not happen (or even be possible).  We can intersect the assertion of the result
281 with the derived types for non-call uses.
282
283 When we do a tail call, we obviously can't check that the returned value
284 matches our assertion.  Although in principle, we would like to be able to
285 check all assertions, to preserve system integrity, we only need to check
286 assertions that we depend on.  We can afford to lose some assertion information
287 as long as we entirely lose it, ignoring it for type inference as well as for
288 type checking.
289
290 Things will work out, since the caller will see the tail-info type as the
291 derived type for the call, and will emit a type check if it needs a stronger
292 result.
293
294 A remaining question is whether we should intersect the assertion with
295 per-RETURN derived types from the very beginning (i.e. before the type check
296 pass).  I think the answer is yes.  We delay the type check pass so that we can
297 get our best guess for the derived type before we decide whether a check is
298 necessary.  But with the function return type, we aren't committing to doing
299 any type check when we intersect with the type assertion; the need to type
300 check is still determined in the type check pass by examination of the result
301 continuation.
302
303 What is the relationship between the per-RETURN types and the types in the
304 result continuation?  The assertion is exactly the Continuation-Asserted-Type
305 (note that the asserted type of result continuations will never change after
306 ICR conversion).  The per-RETURN derived type is different than the
307 Continuation-Derived-Type, since it is intersected with the asserted type even
308 before Type Check runs.  Ignoring the Continuation-Derived-Type probably makes
309 life simpler anyway, since this breaks the potential circularity of the
310 Tail-Info-Type will affecting the Continuation-Derived-Type, which affects...
311
312 When a given return has no non-call uses, we represent this by using
313 *empty-type*.  This consistent with the interpretation that a return type of
314 NIL means the function can't return.
315
316 \f
317 \section{Hairy function representation}
318
319 Non-fixed-arg functions are represented using Optional-Dispatch.  An
320 Optional-Dispatch has an entry-point function for each legal number of
321 optionals, and one for when extra args are present.  Each entry point function
322 is a simple lambda.  The entry point function for an optional is passed the
323 arguments which were actually supplied; the entry point function is expected to
324 default any remaining parameters and evaluate the actual function body.
325
326 If no supplied-p arg is present, then we can do this fairly easily by having
327 each entry point supply its default and call the next entry point, with the
328 last entry point containing the body.  If there are supplied-p args, then entry
329 point function is replaced with a function that calls the original entry
330 function with T's inserted at the position of all the supplied args with
331 supplied-p parameters.
332
333 We want to be a bit clever about how we handle arguments declared special when
334 doing optional defaulting, or we will emit really gross code for special
335 optionals.  If we bound the arg specially over the entire entry-point function,
336 then the entry point function would be caused to be non-tail-recursive.  What
337 we can do is only bind the variable specially around the evaluation of the
338 default, and then read the special and store the final value of the special
339 into a lexical variable which we then pass as the argument.  In the common case
340 where the default is a constant, we don't have to special-bind at all, since
341 the computation of the default is not affected by and cannot affect any special
342 bindings.
343
344 Keyword and rest args are both implemented using a LEXPR-like "more args"
345 convention.  The More-Entry takes two arguments in addition to the fixed and
346 optional arguments: the argument context and count.  (ARG <context> <n>)
347 accesses the N'th additional argument.  Keyword args are implemented directly
348 using this mechanism.  Rest args are created by calling %Listify-Rest-Args with
349 the context and count.
350
351 The More-Entry parses the keyword arguments and passes the values to the main
352 function as positional arguments.  If a keyword default is not constant, then
353 we pass a supplied-p parameter into the main entry and let it worry about
354 defaulting the argument.  Since the main entry accepts keywords in parsed form,
355 we can parse keywords at compile time for calls to known functions.  We keep
356 around the original parsed lambda-list and related information so that people
357 can figure out how to call the main entry.
358
359 \f
360 \section{ICR representation of non-local exits}
361
362 All exits are initially represented by EXIT nodes:
363 How about an Exit node:
364     (defstruct (exit (:include node))
365       value)
366 The Exit node uses the continuation that is to receive the thrown Value.
367 During optimization, if we discover that the Cont's home-lambda is the same is
368 the exit node's, then we can delete the Exit node, substituting the Cont for
369 all of the Value's uses.
370
371 The successor block of an EXIT is the entry block in the entered environment.
372 So we use the Exit node to mark the place where exit code is inserted.  During
373 environment analysis, we need only insert a single block containing the entry
374 point stub.
375
376 We ensure that all Exits that aren't for a NLX don't have any Value, so that
377 local exits never require any value massaging.
378
379 The Entry node marks the beginning of a block or tagbody:
380     (defstruct (entry (:include node))
381       (continuations nil :type list)) 
382
383 It contains a list of all the continuations that the body could exit to.  The
384 Entry node is used as a marker for the the place to snapshot state, including
385 the control stack pointer.  Each lambda has a list of its Entries so
386 that environment analysis can figure out which continuations are really being
387 closed over.  There is no reason for optimization to delete Entry nodes,
388 since they are harmless in the degenerate case: we just emit no code (like a
389 no-var let).
390
391
392 We represent CATCH using the lexical exit mechanism.  We do a transformation
393 like this:
394    (catch 'foo xxx)  ==>
395    (block \#:foo
396      (%catch \#'(lambda () (return-from \#:foo (%unknown-values))) 'foo)
397      (%within-cleanup :catch
398        xxx))
399
400 %CATCH just sets up the catch frame which points to the exit function.  %Catch
401 is an ordinary function as far as ICR is concerned.  The fact that the catcher
402 needs to be cleaned up is expressed by the Cleanup slots in the continuations
403 in the body.  %UNKNOWN-VALUES is a dummy function call which represents the
404 fact that we don't know what values will be thrown.  
405
406 %WITHIN-CLEANUP is a special special form that instantiates its first argument
407 as the current cleanup when converting the body.  In reality, the lambda is
408 also created by the special special form %ESCAPE-FUNCTION, which gives the
409 lambda a special :ESCAPE kind so that the back end knows not to generate any
410 code for it.
411
412
413 We use a similar hack in Unwind-Protect to represent the fact that the cleanup
414 forms can be invoked at arbitrarily random times.
415     (unwind-protect p c)  ==>
416     (flet ((\#:cleanup () c))
417       (block \#:return
418         (multiple-value-bind
419             (\#:next \#:start \#:count)
420             (block \#:unwind
421               (%unwind-protect \#'(lambda (x) (return-from \#:unwind x)))
422               (%within-cleanup :unwind-protect
423                 (return-from \#:return p)))
424           (\#:cleanup)
425           (%continue-unwind \#:next \#:start \#:count))))
426
427 We use the block \#:unwind to represent the entry to cleanup code in the case
428 where we are non-locally unwound.  Calling of the cleanup function in the
429 drop-through case (or any local exit) is handled by cleanup generation.  We
430 make the cleanup a function so that cleanup generation can add calls at local
431 exits from the protected form.  \#:next, \#:start and \#:count are state used in
432 the case where we are unwound.  They indicate where to go after doing the
433 cleanup and what values are being thrown.  The cleanup encloses only the
434 protected form.  As in CATCH, the escape function is specially tagged as
435 :ESCAPE.  The cleanup function is tagged as :CLEANUP to inhibit let conversion
436 (since references are added in environment analysis.)
437
438 Notice that implementing these forms using closures over continuations
439 eliminates any need to special-case ICR flow analysis.  Obviously we don't
440 really want to make heap-closures here.  In reality these functions are
441 special-cased by the back-end according to their KIND.
442
443 \f
444 \section{Block compilation}
445
446 One of the properties of ICR is that supports "block compilation" by allowing
447 arbitrarily large amounts of code to be converted at once, with actual
448 compilation of the code being done at will.
449
450
451 In order to preserve the normal semantics we must recognize that proclamations
452 (possibly implicit) are scoped.  A proclamation is in effect only from the time
453 of appearance of the proclamation to the time it is contradicted.  The current
454 global environment at the end of a block is not necessarily the correct global
455 environment for compilation of all the code within the block.  We solve this
456 problem by closing over the relevant information in the ICR at the time it is
457 converted.  For example, each functional variable reference is marked as
458 inline, notinline or don't care.  Similarly, each node contains a structure
459 known as a Cookie which contains the appropriate settings of the compiler
460 policy switches.
461
462 We actually convert each form in the file separately, creating a separate
463 "initial component" for each one.  Later on, these components are merged as
464 needed.  The main reason for doing this is to cause EVAL-WHEN processing to be
465 interleaved with reading. 
466
467 \f
468 \section{Entry points}
469
470 \#|
471
472 Since we need to evaluate potentially arbitrary code in the XEP argument forms
473 (for type checking), we can't leave the arguments in the wired passing
474 locations.  Instead, it seems better to give the XEP max-args fixed arguments,
475 with the passing locations being the true passing locations.  Instead of using
476 %XEP-ARG, we reference the appropriate variable.
477
478 Also, it might be a good idea to do argument count checking and dispatching
479 with explicit conditional code in the XEP.  This would simplify both the code
480 that creates the XEP and the VMR conversion of XEPs.  Also, argument count
481 dispatching would automatically benefit from any cleverness in compilation of
482 case-like forms (jump tables, etc).  On the downside, this would push some
483 assumptions about how arg dispatching is done into ICR.  But then we are
484 currently violating abstraction at least as badly in VMR conversion, which is
485 also supposed to be implementation independent.
486 |\#
487
488 As a side-effect of finding which references to known functions can be
489 converted to local calls, we find any references that cannot be converted.
490 References that cannot be converted to a local call must evaluate to a
491 "function object" (or function-entry) that can be called using the full call
492 convention.  A function that can be called from outside the component is called
493 an "entry-point".
494
495 Lots of stuff that happens at compile-time with local function calls must be
496 done at run-time when an entry-point is called.
497
498 It is desirable for optimization and other purposes if all the calls to every
499 function were directly present in ICR as local calls.  We cannot directly do
500 this with entry-point functions, since we don't know where and how the
501 entry-point will be called until run-time.
502
503 What we do is represent all the calls possible from outside the component by
504 local calls within the component.  For each entry-point function, we create a
505 corresponding lambda called the external entry point or XEP.  This is a
506 function which takes the number of arguments passed as the first argument,
507 followed by arguments corresponding to each required or optional argument.
508
509 If an optional argument is unsupplied, the value passed into the XEP is
510 undefined.  The XEP is responsible for doing argument count checking and
511 dispatching.  
512
513 In the case of a fixed-arg lambda, we emit a call to the %VERIFY-ARGUMENT-COUNT
514 funny function (conditional on policy), then call the real function on the
515 passed arguments.  Even in this simple case, we benefit several ways from
516 having a separate XEP:
517  -- The argument count checking is factored out, and only needs to be done in
518     full calls.
519  -- Argument type checking happens automatically as a consequence of passing
520     the XEP arguments in a local call to the real function.  This type checking
521     is also only done in full calls.
522  -- The real function may use a non-standard calling convention for the benefit
523     of recursive or block-compiled calls.  The XEP converts arguments/return
524     values to/from the standard convention.  This also requires little
525     special-casing of XEPs.
526
527 If the function has variable argument count (represented by an
528 OPTIONAL-DISPATCH), then the XEP contains a COND which dispatches off of the
529 argument count, calling the appropriate entry-point function (which then does
530 defaulting).  If there is a more entry (for keyword or rest args), then the XEP
531 obtains the more arg context and count by calling the %MORE-ARG-CONTEXT funny
532 function.
533
534 All non-local-call references to functions are replaced with references to the
535 corresponding XEP.  ICR optimization may discover a local call that was
536 previously a non-local reference.  When we delete the reference to the XEP, we
537 may find that it has no references.  In this case, we can delete the XEP,
538 causing the function to no longer be an entry-point.
539
540 \f