| 1 |
|
| 2 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya |
| 3 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 4 |
;;;; Copyright (c) 2003, Juan Jose Garcia-Ripoll |
| 5 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
(in-package #:compiler) |
| 10 |
|
| 11 |
;;; |
| 12 |
;;; REF OBJECT |
| 13 |
;;; |
| 14 |
;;; Base object for functions, variables and statements. We use it to |
| 15 |
;;; keep track of references to objects, how many times the object is |
| 16 |
;;; referenced, by whom, and whether the references cross some closure |
| 17 |
;;; boundaries. |
| 18 |
;;; |
| 19 |
|
| 20 |
(defstruct (ref (:print-object print-ref)) |
| 21 |
name ;; Identifier of reference. |
| 22 |
(ref 0 :type fixnum) ;; Number of references. |
| 23 |
ref-ccb ;; Cross closure reference: T or NIL. |
| 24 |
ref-clb ;; Cross local function reference: T or NIL. |
| 25 |
read-nodes ;; Nodes (c1forms) in which the reference occurs. |
| 26 |
) |
| 27 |
|
| 28 |
(defun print-ref (ref-object stream) |
| 29 |
(ext:if-let ((name (ref-name ref-object))) |
| 30 |
(format stream "#<a ~A: ~A>" (type-of ref-object) name) |
| 31 |
(format stream "#<a ~A>" (type-of ref-object)))) |
| 32 |
|
| 33 |
(deftype OBJECT () `(not (or fixnum character float))) |
| 34 |
|
| 35 |
(defstruct (var (:include ref) (:constructor %make-var) (:print-object print-var)) |
| 36 |
#| |
| 37 |
name ;;; Variable name. |
| 38 |
(ref 0 :type fixnum) ;;; Number of references to the variable (-1 means IGNORE). |
| 39 |
ref-ccb ;;; Cross closure reference: T or NIL. |
| 40 |
ref-clb ;;; Cross local function reference: T or NIL. |
| 41 |
read-nodes ;;; Nodes (c1forms) in which the reference occurs |
| 42 |
|# |
| 43 |
set-nodes ;;; Nodes in which the variable is modified |
| 44 |
kind ;;; One of LEXICAL, CLOSURE, SPECIAL, GLOBAL, :OBJECT, |
| 45 |
;;; or some C representation type (:FIXNUM, :CHAR, etc) |
| 46 |
(function *current-function*) |
| 47 |
;;; For local variables, in which function it was created. |
| 48 |
;;; For global variables, it doesn't have a meaning. |
| 49 |
(functions-setting nil) |
| 50 |
(functions-reading nil) |
| 51 |
;;; Functions in which the variable has been modified or read. |
| 52 |
(loc 'OBJECT) ;;; During Pass 1: OBJECT |
| 53 |
;;; During Pass 2: |
| 54 |
;;; For :FIXNUM, :CHAR, :FLOAT, :DOUBLE, :OBJECT: |
| 55 |
;;; the cvar for the C variable that holds the value. |
| 56 |
;;; For LEXICAL or CLOSURE: the frame-relative address for |
| 57 |
;;; the variable in the form of a cons '(lex-levl . lex-ndx) |
| 58 |
;;; lex-levl is the level of lexical environment |
| 59 |
;;; lex-ndx is the index within the array for this env. |
| 60 |
;;; For SPECIAL and GLOBAL: the vv-index for variable name. |
| 61 |
(type t) ;;; Type of the variable. |
| 62 |
(ignorable nil) ;;; Whether there was an IGNORABLE/IGNORE declaration |
| 63 |
) |
| 64 |
|
| 65 |
(defun print-var (var-object stream) |
| 66 |
(format stream "#<a VAR: ~A KIND: ~A>" (var-name var-object) (var-kind var-object))) |
| 67 |
|
| 68 |
;;; A function may be compiled into a CFUN, CCLOSURE or CCLOSURE+LISP_CLOSURE |
| 69 |
;;; Here are examples of function FOO for the 3 cases: |
| 70 |
;;; 1. (flet ((foo () (bar))) (foo)) CFUN |
| 71 |
;;; 2. (flet ((foo () (bar))) #'foo) CFUN+LISP_CFUN |
| 72 |
;;; 3. (flet ((foo () x)) #'(lambda () (foo))) CCLOSURE |
| 73 |
;;; 4. (flet ((foo () x)) #'foo) CCLOSURE+LISP_CLOSURE |
| 74 |
|
| 75 |
;;; A function can be referenced across a ccb without being a closure, e.g: |
| 76 |
;;; (flet ((foo () (bar))) #'(lambda () (foo))) |
| 77 |
;;; [the lambda also need not be a closure] |
| 78 |
;;; and it can be a closure without being referenced across ccb, e.g.: |
| 79 |
;;; (flet ((foo () x)) #'foo) [ is this a mistake in local-function-ref?] |
| 80 |
;;; Here instead the lambda must be a closure, but no closure is needed for foo |
| 81 |
;;; (flet ((foo () x)) #'(lambda () (foo))) |
| 82 |
;;; So we use two separate fields: ref-ccb and closure. |
| 83 |
;;; A CCLOSURE must be created for a function when: |
| 84 |
;;; 1. it appears within a FUNCTION construct and |
| 85 |
;;; 2. it uses some ccb references (directly or indirectly). |
| 86 |
;;; ref-ccb corresponds to the first condition, i.e. function is referenced |
| 87 |
;;; across CCB. It is computed during Pass 1. A value of 'RETURNED means |
| 88 |
;;; that it is immediately within FUNCTION. |
| 89 |
;;; closure corresponds to second condition and is computed in Pass 2 by |
| 90 |
;;; looking at the info-referenced-vars and info-local-referenced of its body. |
| 91 |
|
| 92 |
;;; A LISP_CFUN or LISP_CLOSURE must be created when the function is returned. |
| 93 |
;;; The LISP funob may then be referenced locally or across a function boundary: |
| 94 |
;;; (flet ((foo (z) (bar z))) (list #'foo))) |
| 95 |
;;; (flet ((foo (z) z)) (flet ((bar () #'foo)) (bar))) |
| 96 |
;;; (flet ((foo (z) (bar z))) #'(lambda () #'foo))) |
| 97 |
;;; therefore we need field funob. |
| 98 |
|
| 99 |
(defstruct (fun (:include ref)) |
| 100 |
#| |
| 101 |
name ;;; Function name. |
| 102 |
(ref 0 :type fixnum) ;;; Number of references. |
| 103 |
;;; During Pass1, T or NIL. |
| 104 |
;;; During Pass2, the vs-address for the |
| 105 |
;;; function closure, or NIL. |
| 106 |
ref-ccb ;;; Cross closure reference: T or NIL. |
| 107 |
ref-clb ;;; Unused. |
| 108 |
read-nodes ;;; Nodes (c1forms) in which the reference occurs. |
| 109 |
|# |
| 110 |
cfun ;;; The cfun for the function. |
| 111 |
(variadic-entrypoint-cfun :unknown) ;;; The cfun for an entrypoint with variadic signature. |
| 112 |
(level 0) ;;; Level of lexical nesting for a function. |
| 113 |
(env 0) ;;; Size of env of closure. |
| 114 |
(global nil) ;;; Global lisp function. |
| 115 |
(exported nil) ;;; Its C name can be seen outside the module. |
| 116 |
(no-entry nil) ;;; NIL if declared as C-LOCAL. Then we create no |
| 117 |
;;; function object and the C function is called |
| 118 |
;;; directly |
| 119 |
closure ;;; During Pass2, T if env is used inside the function |
| 120 |
var ;;; the variable holding the funob |
| 121 |
description ;;; Text for the object, in case NAME == NIL. |
| 122 |
lambda ;;; Lambda c1-form for this function. |
| 123 |
lambda-expression ;;; LAMBDA or LAMBDA-BLOCK expression |
| 124 |
(minarg 0) ;;; Min. number arguments that the function receives. |
| 125 |
(maxarg call-arguments-limit) |
| 126 |
;;; Max. number arguments that the function receives. |
| 127 |
(return-type '(VALUES &REST T)) |
| 128 |
(parent *current-function*) |
| 129 |
;;; Parent function, NIL if global. |
| 130 |
(local-vars nil) ;;; List of local variables created here. |
| 131 |
(referenced-vars nil) ;;; List of external variables referenced here. |
| 132 |
(referenced-funs nil) ;;; List of external functions called in this one. |
| 133 |
;;; We only register direct calls, not calls via object. |
| 134 |
(referencing-funs nil);;; Functions that reference this one |
| 135 |
(child-funs nil) ;;; List of local functions defined here. |
| 136 |
(file (car ext:*source-location*)) |
| 137 |
;;; Source file or NIL |
| 138 |
(file-position (or (cdr ext:*source-location*) *compile-file-position*)) |
| 139 |
;;; Top-level form number in source file |
| 140 |
(cmp-env (cmp-env-copy)) ;;; Environment |
| 141 |
required-lcls ;;; Names of the function arguments |
| 142 |
(optional-type-check-forms nil) ;;; Type check forms for optional arguments |
| 143 |
(keyword-type-check-forms nil) ;;; Type check forms for keyword arguments |
| 144 |
) |
| 145 |
|
| 146 |
(defstruct (blk (:include ref)) |
| 147 |
#| |
| 148 |
name ;;; Block name. |
| 149 |
(ref 0 :type fixnum) ;;; Total number of block references. |
| 150 |
ref-ccb ;;; Unused (see blk-var). |
| 151 |
ref-clb ;;; Unused (see blk-var). |
| 152 |
read-nodes ;;; Unused (see blk-var). |
| 153 |
|# |
| 154 |
exit ;;; Where to return. A label. |
| 155 |
destination ;;; Where the value of the block to go. |
| 156 |
var ;;; Variable containing the block id and its references. |
| 157 |
(type '(VALUES &REST T)) ;;; Estimated type. |
| 158 |
) |
| 159 |
|
| 160 |
(defstruct (tag (:include ref)) |
| 161 |
#| |
| 162 |
name ;;; Tag name. |
| 163 |
(ref 0 :type fixnum) ;;; Number of references. |
| 164 |
ref-ccb ;;; Unused (see tag-var). |
| 165 |
ref-clb ;;; Unused (see tag-var). |
| 166 |
read-nodes ;;; Unused (see tag-var). |
| 167 |
|# |
| 168 |
jump ;;; Where to escape. A label. |
| 169 |
var ;;; Variable containing frame ID. |
| 170 |
index ;;; An integer denoting the label. |
| 171 |
) |