Debian Codegraph @

src/cmp/cmppass1-cont.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 (in-package #:compiler)
11
12
13 ;;; A dummy variable is created to hold the block identifier. When a reference
14 ;;; to the block (via `return-from') is found, the `var-ref' count for that
15 ;;; variable is incremented only if the reference appears across a boundary
16 ;;; (`SI:FUNCTION-BOUNDARY' or `SI:UNWIND-PROTECT-BOUNDARY'), while the
17 ;;; `blk-ref' is always incremented. Therefore `blk-ref' represents whether the
18 ;;; block is used at all and `var-ref' for the dummy variable represents whether
19 ;;; a block identifier must be created and stored in such variable.
20
21 (defun c1block (args)
22 (check-args-number 'BLOCK args 1)
23 (let ((block-name (first args)))
24 (unless (symbolp block-name)
25 (cmperr "The block name ~s is not a symbol." block-name))
26 (add-symbol block-name)
27 (let* ((blk-var (make-var :name block-name :kind 'LEXICAL))
28 (blk (make-blk :var blk-var :name block-name))
29 (body (let ((*cmp-env* (cmp-env-copy)))
30 (cmp-env-register-block blk)
31 (c1progn (rest args)))))
32 (when (or (var-ref-ccb blk-var) (var-ref-clb blk-var))
33 (incf *setjmps*))
34 (if (plusp (blk-ref blk))
35 (make-c1form* 'BLOCK
36 :local-vars (list blk-var)
37 :type (values-type-or (blk-type blk) (c1form-type body))
38 :args blk body)
39 body))))
40
41 (defun c1return-from (args)
42 (check-args-number 'CL:RETURN-FROM args 1 2)
43 (let ((name (first args)))
44 (unless (symbolp name)
45 (cmperr "The block name ~s is not a symbol." name))
46 (multiple-value-bind (blk cfb unw)
47 (cmp-env-search-block name)
48 (unless blk
49 (cmperr "The block ~s is undefined." name))
50 (let* ((val (c1expr (second args)))
51 (var (blk-var blk))
52 (nonlocal (or cfb unw)))
53 (when cfb
54 (setf (var-ref-ccb var) T))
55 (incf (blk-ref blk))
56 (setf (blk-type blk) (values-type-or (blk-type blk) (c1form-type val)))
57 (let ((output (make-c1form* 'CL:RETURN-FROM :type 'T :args blk nonlocal val)))
58 (when (or cfb unw)
59 (add-to-read-nodes var output))
60 output)))))
61
62
63 ;;; A dummy variable is created to hold the tag identifier and one tag
64 ;;; structure (containing reference to such variable) is created for each label
65 ;;; in the body. When a reference to a tag (go instruction) is found, the
66 ;;; var-kind is stepped from NIL to OBJECT (if appearing inside an
67 ;;; unwind-protect) to LEXICAL or CLOSURE (if appearing across a boundary).
68 ;;; The tag-ref is also incremented. Therefore var-ref represents whether some
69 ;;; tag is used at all and var-kind variable represents whether a tag
70 ;;; identifier must be created and the kind of the dummy variable to store it.
71
72 (defun add-loop-registers (tagbody)
73 ;; Find a maximal iteration interval in TAGBODY from first to end
74 ;; then increment the var-ref slot.
75 (labels ((add-reg1 (form)
76 ;; increase the var-ref in FORM for all vars
77 (cond ((c1form-p form)
78 (dolist (v (c1form-args form))
79 (add-reg1 v)))
80 ((consp form)
81 (dolist (v form)
82 (add-reg1 v)))
83 ((var-p form)
84 (setf (var-ref form) most-positive-fixnum))))
85 (jumps-to-p (clause tag-name)
86 ;; Does CLAUSE have a go TAG-NAME in it?
87 (cond ((c1form-p clause)
88 (and (eq (c1form-name clause) 'GO)
89 (eq (tag-name (c1form-arg 0 clause)) tag-name)))
90 ((atom clause) nil)
91 (t (or (jumps-to-p (car clause) tag-name)
92 (jumps-to-p (cdr clause) tag-name))))))
93 (do ((v tagbody (cdr v))
94 (end nil)
95 (first nil))
96 ((null v)
97 (do ((ww first (cdr ww)))
98 ((eq ww end) (add-reg1 (car ww)))
99 (add-reg1 (car ww))))
100 (when (tag-p (car v))
101 (unless first (setq first v))
102 (do ((w (cdr v) (cdr w))
103 (name (tag-name (car v))))
104 ((null w))
105 (when (jumps-to-p (car w) name)
106 (setq end w)))))))
107
108 ;; FIXME! The variable name should not be a usable one!
109 (defun c1tagbody (orig-body &aux (*cmp-env* (cmp-env-copy))
110 (tag-var (make-var :name 'TAGBODY :kind NIL))
111 (tag-index 0)
112 (body nil))
113
114 ;;; Establish tags.
115 (setq body
116 (loop for x in orig-body
117 collect (if (consp x)
118 x
119 (let ((tag (make-tag :name x :var tag-var :index tag-index)))
120 (cmp-env-register-tag (tag-name tag) tag)
121 (incf tag-index)
122 tag))))
123 ;; Split forms according to the tag they are preceded by and compile
124 ;; them grouped by PROGN. This help us use the optimizations in
125 ;; C1PROGN to recognize transfers of control.
126 (make-var :name 'tagbody-beginnnig :kind nil) ; "this-tag"
127 (loop with output = '()
128 with tag-body = nil
129 for form in body
130 do (cond ((tag-p form)
131 (when tag-body
132 (setf output (cons (c1progn (nreconc tag-body '(nil))) output)
133 tag-body nil))
134 (push form output))
135 (t
136 (push form tag-body)))
137 finally (setf body
138 (if tag-body
139 (cons (c1progn (nreconc tag-body '(nil))) output)
140 output)))
141
142 ;;; Reverse the body list, deleting unused tags.
143 (loop for form in body
144 with output = '()
145 when (or (not (tag-p form)) (plusp (tag-ref form)))
146 do (push form output)
147 finally (setf body output))
148
149 ;;; Ensure that the end is not just a tag, but at least a NIL body.
150 (when (null body)
151 (return-from c1tagbody (c1progn nil)))
152 (when (tag-p (first (last body)))
153 (setf body (nconc body (list (c1expr nil)))))
154
155 ;;; Only produce a tagbody if it was needed.
156 (when (zerop (var-ref tag-var))
157 (return-from c1tagbody (make-c1form* 'PROGN :args
158 (delete-if #'tag-p body))))
159 (when (var-ref-ccb tag-var)
160 (incf *setjmps*))
161 (add-loop-registers body)
162 (make-c1form* 'TAGBODY :local-vars (list tag-var)
163 :args tag-var body))
164
165 (defun c1go (args)
166 (check-args-number 'GO args 1 1)
167 (let ((name (first args)))
168 (unless (or (symbolp name) (integerp name))
169 (cmperr "The tag name ~s is not a symbol nor an integer." name))
170 (multiple-value-bind (tag cfb unw)
171 (cmp-env-search-tag name)
172 (unless tag
173 (cmperr "Undefined tag ~A" name))
174 (let ((var (tag-var tag)))
175 (cond (cfb (setf (var-ref-clb var) t
176 (var-kind var) 'LEXICAL))
177 (unw (unless (var-kind var)
178 (setf (var-kind var) :OBJECT))))
179 (incf (tag-ref tag))
180 (add-to-read-nodes var (make-c1form* 'GO :args tag (or cfb unw)))))))
181
182
183 (defun c1throw (args)
184 (check-args-number 'THROW args 2 2)
185 (make-c1form* 'THROW :args (c1expr (first args)) (c1expr (second args))))
186
187 (defun c1catch (args)
188 (check-args-number 'CATCH args 1)
189 (incf *setjmps*)
190 (make-c1form* 'CATCH :sp-change t :type t :args (c1expr (first args))
191 (c1progn (rest args))))
192
193 (defun c1unwind-protect (args)
194 (check-args-number 'UNWIND-PROTECT args 1)
195 (cond
196 ((null (rest args))
197 (cmpdebug "UNWIND-PROTECT without CLEANUP-FORMS was replaced by its FORM.")
198 (c1expr (first args)))
199 (T
200 (incf *setjmps*)
201 (let ((form (let ((*cmp-env* (cmp-env-mark 'SI:UNWIND-PROTECT-BOUNDARY)))
202 (c1expr (first args)))))
203 (make-c1form* 'UNWIND-PROTECT :type (c1form-type form) :sp-change t
204 :args form (c1progn (rest args)))))))