raz / sbcl / src / stmt.lisp

375 lines · 18.2 KiB · raw · history · last change 55bc6c0d

1;;;; stmt.lisp -- statements (report, section 7), each turned into a list
2;;;; of Lisp forms.
3;;;;
4;;;; A procedure's body is a Lisp BLOCK, left by RETURN-FROM; each loop is
5;;;; a block of its own, which `break` leaves. Deferred statements (7.7)
6;;;; are known where they are written, so the compiler puts a copy of each
7;;;; wherever its statement list can be left: at its end, and before each
8;;;; `return`, `try` and `break` that leaves it. The program never needs to
9;;;; keep a list of them as it runs.
10
11(in-package :raz)
12
13(defun in-loop (fn)
14 "The forms FN makes, given the name of the loop's block, within that
15block, which `break` leaves."
16 (let ((blk (gensym "LOOP")))
17 (push (list blk (length (proc-defers *proc*))) (proc-loops *proc*))
18 (let ((forms (unwind-protect (funcall fn blk) (pop (proc-loops *proc*)))))
19 (list `(block ,blk ,@forms)))))
20
21(defmacro with-loop ((blk) &body body) `(in-loop (lambda (,blk) ,@body)))
22
23(defun statements (&rest enders)
24 "Statements up to one of the tokens ENDERS: their forms, and after them
25the statements deferred among them, the last first."
26 (push '() (proc-defers *proc*))
27 (let ((forms (loop until (member (kind) enders)
28 append (prog1 (statement) (expect :|;| "; after a statement")))))
29 (append forms (pop (proc-defers *proc*)))))
30
31(defun pending-defers (&optional (depth 0))
32 "The deferred statements of the lists the program leaves, the innermost
33first: all of them, or those inside the DEPTH outer lists."
34 (let ((frames (proc-defers *proc*)))
35 (apply #'append (subseq frames 0 (- (length frames) depth)))))
36
37(defun statement ()
38 (let ((tk (tok)))
39 (case (kind)
40 ((:ident :blank) (assignment-or-call))
41 (:if (next-tok) (if-statement))
42 (:while (next-tok)
43 (let ((c (condition-form)))
44 (expect :do)
45 (with-loop (blk)
46 (prog1 `((loop (unless ,c (return-from ,blk)) ,@(statements :end)))
47 (expect :end)))))
48 (:repeat (next-tok)
49 (with-loop (blk)
50 (let ((body (statements :until)))
51 (expect :until)
52 `((loop ,@body (when ,(condition-form) (return-from ,blk)))))))
53 (:for (next-tok) (for-statement tk))
54 (:match (next-tok) (match-statement tk))
55 (:return (next-tok) (list (return-statement tk)))
56 (:break (next-tok)
57 (let ((l (first (proc-loops *proc*))))
58 (unless l (fail-tok tk "break stands outside a loop~:[~; of the deferred statement~]"
59 (proc-in-defer *proc*)))
60 `(,@(pending-defers (second l)) (return-from ,(first l)))))
61 (:defer (next-tok) (defer-statement tk) '())
62 (:try (let ((item (try-item (next-tok))))
63 (list (item-form item))))
64 (t (fail-here "a statement expected, not ~a" (describe-tok tk))))))
65
66(defun condition-form ()
67 (let* ((tk (tok)) (c (need-value (expression))))
68 (unless (is (item-type c) :bool) (fail-tok tk "the condition is ~a, not a BOOL" (type-name (item-type c))))
69 (value-form c)))
70
71(defun if-statement ()
72 (let ((clauses '()))
73 (loop
74 (let ((c (condition-form)))
75 (expect :then)
76 (push (cons c (statements :elsif :else :end)) clauses))
77 (unless (accept :elsif) (return)))
78 (when (accept :else)
79 (push (cons t (statements :end)) clauses))
80 (expect :end)
81 (list `(cond ,@(reverse clauses)))))
82
83;;; Assignments and calls (7.1).
84
85(defun check-target (x tk)
86 "X, which must be a variable that can be assigned."
87 (unless (eq (item-mode x) :var) (fail-tok tk "only a variable can be assigned"))
88 (check-writable x tk "assigned")
89 (when (and (item-vol x) (aggregate-p (item-type x)))
90 (fail-tok tk "a whole record or array can't be copied through a volatile pointer"))
91 x)
92
93(defun target ()
94 "An assignment's target: a writable variable's item, or nil for `_`."
95 (if (accept :blank) nil (let ((tk (tok))) (check-target (designator) tk))))
96
97(defun assignment-or-call ()
98 (let ((tk (tok)))
99 (if (or (eq (kind) :blank) (multiple-targets-p))
100 (assignment (loop collect (target) while (accept :|,|)))
101 (let ((x (designator)))
102 (if (eq (kind) :|:=|)
103 (assignment (list (check-target x tk)))
104 (list (call-statement x tk)))))))
105
106(defun multiple-targets-p ()
107 "Whether the statement is `a, b := ...`: a comma before the := at the
108statement's own level."
109 (let ((depth 0))
110 (loop for k from 0
111 for kd = (kind k)
112 do (case kd
113 ((:|(| :|[| :{) (incf depth))
114 ((:|)| :|]| :}) (decf depth))
115 (:|,| (when (= depth 0) (return t)))
116 ((:|:=| :|;| :eof :end) (return nil))))))
117
118(defun call-statement (x tk)
119 "A designator standing alone: a call, whose results are dropped."
120 (case (item-mode x)
121 (:proc (item-form (call-proc (item-obj x) nil tk)))
122 ((:none :multi) (item-form x))
123 (:builtin (if (string= (item-val x) "FENCE")
124 '(sb-thread:barrier (:memory))
125 (fail-tok tk "~a takes arguments" (item-val x))))
126 (t (if (consp (item-obj x))
127 (item-form x) ; a call with one result
128 (if (and (member (item-mode x) '(:var :val)) (is (item-type x) :proc))
129 (item-form (call-proc nil x tk))
130 (fail-tok tk "this isn't a statement"))))))
131
132(defun assignment (targets)
133 (progn
134 (expect :|:=|)
135 (let* ((rtk (tok)) (e (expression)) (syms (loop repeat (length targets) collect (var-sym "T"))))
136 (if (rest targets)
137 ;; several results, of exactly the targets' types
138 (progn
139 (unless (eq (item-mode e) :multi)
140 (fail-tok rtk "~d targets need a call with ~:*~d results" (length targets)))
141 (unless (= (length (item-type e)) (length targets))
142 (fail-tok rtk "~d targets for ~d results" (length targets) (length (item-type e))))
143 (let ((vals (loop repeat (length targets) collect (var-sym "V"))))
144 (list `(let ,(loop for s in syms for x in targets when x collect `(,s ,(item-form x)))
145 (multiple-value-bind ,vals ,(item-form e)
146 ,@(loop for s in syms for x in targets for v in vals for ty in (item-type e)
147 when x
148 collect (progn
149 (unless (same-type-p (item-type x) ty)
150 (fail-tok rtk "a result of ~a goes to ~a" (type-name ty) (type-name (item-type x))))
151 (if (aggregate-p ty)
152 `(move ,s ,v ,(ty-size ty))
153 `(st ,(access-kind ty) ,s ,v)))))))))
154 (let ((x (first targets)))
155 (if x
156 (list `(let ((,(first syms) ,(item-form x)))
157 ,(store-form (first syms) (item-type x) e rtk)))
158 ;; `_ := e`: e evaluated, and dropped
159 (list (if (member (item-mode e) '(:none :multi))
160 (fail-tok rtk "_ takes one value")
161 (value-form e)))))))))
162
163;;; for (7.2).
164
165(defun exact-form (item ty tk what)
166 "ITEM's value as TY, which it must be, or an untyped constant that fits:
167an INT doesn't widen here (5.10)."
168 (let ((item (need-value item)))
169 (unless (or (and (untyped-p item) (not (assign-error item ty)))
170 (same-type-p (item-type item) ty))
171 (fail-tok tk "~a is ~a, not ~a" what (type-name (item-type item)) (type-name ty)))
172 (coerce-item item ty tk)))
173
174(defun for-statement (tk)
175 (let* ((vtk (tok)) (name (ident)) (obj (lookup name)))
176 (unless (and obj (eq (obj-kind obj) :var)) (fail-tok vtk "~a isn't a variable" name))
177 (unless (eq (kind) :|:=|) (fail-tok (tok) "the for variable is named by its name alone"))
178 (expect :|:=|)
179 (let* ((v (obj-item obj vtk)) (ty (item-type v)))
180 (check-writable v vtk "a for variable")
181 (unless (member (ty-kind ty) '(:int :enum))
182 (fail-tok vtk "a for variable is an integer or an enumeration, not ~a" (type-name ty)))
183 (let* ((a (exact-form (expression) ty tk "the start"))
184 (b (progn (expect :to) (exact-form (expression) ty tk "the limit")))
185 (s (if (accept :by)
186 (let* ((stk (tok)) (s (need-value (expression))))
187 (unless (and (const-p s) (member (ty-kind (item-type s)) '(:untyped-int :int)))
188 (fail-tok stk "a for loop's step is an integer constant"))
189 (when (= (item-val s) 0) (fail-tok stk "a for loop's step isn't 0"))
190 (item-val s))
191 1))
192 (k (access-kind ty))
193 (wrap (if (is ty :enum) (basic "U8") ty)))
194 (expect :do)
195 (with-loop (blk)
196 (let ((body (statements :end)))
197 (expect :end)
198 ;; at a round's end: stop if one more step would pass the limit
199 `((let* ((at ,(item-form v)) (lo ,a) (hi ,b))
200 (st ,k at lo)
201 (when ,(if (> s 0) '(<= lo hi) '(>= lo hi))
202 (loop ,@body
203 (let ((i (ld ,k at)))
204 (when ,(if (> s 0) `(and (<= i hi) (< (- hi i) ,s)) `(and (>= i hi) (< (- i hi) ,(- s))))
205 (return-from ,blk))
206 (st ,k at ,(wrap-form wrap `(+ i ,s)))
207 (unless ,(if (> s 0) `(<= (ld ,k at) hi) `(>= (ld ,k at) hi))
208 (return-from ,blk)))))))))))))
209
210;;; match (7.4).
211
212(defun match-statement (tk)
213 (let* ((etk (tok)) (x (need-value (expression))) (ty (item-type x)))
214 (expect :of)
215 (case (ty-kind ty)
216 (:variant (match-variant x ty tk))
217 (:enum (match-enum x ty tk))
218 ((:int :untyped-int) (match-int x (if (is ty :int) ty (basic "INT")) tk))
219 (t (fail-tok etk "match takes a variant, an enumeration or an integer, not ~a" (type-name ty))))))
220
221(defun arm-else-p () (and (eq (kind) :else) (eq (kind 1) :|:|)))
222
223(defun match-variant (x ty tk)
224 ;; the arms' names stand for the fields of x itself if it is a variable
225 ;; that may change, else of a copy
226 (let* ((s (var-sym "S"))
227 (src (if (and (eq (item-mode x) :var) (not (item-ro x)))
228 (item-form x)
229 `(move ,(temp ty) ,(value-form x) ,(ty-size ty))))
230 (arms '()) (seen '()) (else nil))
231 (loop while (accept :|\||)
232 do (let ((atk (tok)))
233 (when else (fail-tok atk "else is the last arm"))
234 (if (arm-else-p)
235 (progn (next-tok) (next-tok) (setf else (cons t (statements :|\|| :end))))
236 (let* ((name (ident))
237 (c (find name (ty-cases ty) :key #'vcase-name :test #'string=)))
238 (unless c (fail-tok atk "~a isn't a case of the variant" name))
239 (when (member c seen) (fail-tok atk "the case ~a has two arms" name))
240 (push c seen)
241 (let ((names (when (accept :|(|)
242 (prog1 (loop collect (cons (tok) (ident)) while (accept :|,|))
243 (expect :|)|)))))
244 (when (and names (/= (length names) (length (vcase-fields c))))
245 (fail-tok atk "the case ~a has ~d fields" name (length (vcase-fields c))))
246 (expect :|:|)
247 (let ((*scope* (make-scope :parent *scope*)))
248 (loop for (ntk . n) in names for f in (vcase-fields c)
249 do (declare-name (make-obj :name n :kind :var :type (field-type f) :tok ntk
250 :where (list :at `(+ ,s ,(field-offset f))))))
251 (push (cons (list (vcase-index c)) (statements :|\|| :end)) arms)))))))
252 (expect :end)
253 (unless (or else (= (length seen) (length (ty-cases ty))))
254 (fail-tok tk "the case ~a has no arm" (vcase-name (find-if-not (lambda (c) (member c seen)) (ty-cases ty)))))
255 (list `(let ((,s ,src))
256 (case (ld :i64 ,s) ,@(reverse arms) ,@(when else `((t ,@(rest else)))))))))
257
258(defun match-enum (x ty tk)
259 (let ((arms '()) (seen '()) (else nil) (v (var-sym "V")))
260 (loop while (accept :|\||)
261 do (let ((atk (tok)))
262 (when else (fail-tok atk "else is the last arm"))
263 (if (arm-else-p)
264 (progn (next-tok) (next-tok) (setf else (cons t (statements :|\|| :end))))
265 (let ((positions
266 (loop collect (let* ((ntk (tok)) (name (ident))
267 (i (position name (ty-names ty) :test #'string=)))
268 (unless i (fail-tok ntk "~a isn't a name of the enumeration" name))
269 (when (member i seen) (fail-tok ntk "~a has two arms" name))
270 (push i seen)
271 i)
272 while (accept :|,|))))
273 (expect :|:|)
274 (push (cons positions (statements :|\|| :end)) arms)))))
275 (expect :end)
276 (unless (or else (= (length seen) (length (ty-names ty))))
277 (fail-tok tk "the name ~a has no arm"
278 (loop for n in (ty-names ty) for i from 0 unless (member i seen) return n)))
279 (list `(let ((,v ,(value-form x)))
280 (case ,v ,@(reverse arms) ,@(when else `((t ,@(rest else)))))))))
281
282(defun match-int (x ty tk)
283 (let ((arms '()) (ranges '()) (else nil) (v (var-sym "V")))
284 (loop while (accept :|\||)
285 do (let ((atk (tok)))
286 (when else (fail-tok atk "else is the last arm"))
287 (if (arm-else-p)
288 (progn (next-tok) (next-tok) (setf else (cons t (statements :|\|| :end))))
289 (let ((tests
290 (loop collect
291 (let* ((ltk (tok)) (lo (label-value ty ltk))
292 (hi (if (accept :..) (label-value ty ltk) lo)))
293 (when (> lo hi) (fail-tok ltk "the label's range runs backwards"))
294 (dolist (r ranges)
295 (when (and (<= lo (cdr r)) (>= hi (car r)))
296 (fail-tok ltk "a value is in two labels")))
297 (push (cons lo hi) ranges)
298 (if (= lo hi) `(= ,v ,lo) `(<= ,lo ,v ,hi)))
299 while (accept :|,|))))
300 (expect :|:|)
301 (push `((or ,@tests) ,@(statements :|\|| :end)) arms)))))
302 (expect :end)
303 (unless else (fail-tok tk "a match on an integer has an else arm"))
304 (list `(let ((,v ,(value-form x)))
305 (cond ,@(reverse arms) (t ,@(rest else)))))))
306
307(defun label-value (ty tk)
308 (let ((x (need-value (expression))))
309 (unless (and (const-p x) (member (ty-kind (item-type x)) '(:int :untyped-int)))
310 (fail-tok tk "a label is an integer constant"))
311 (let ((why (assign-error x ty)))
312 (when why (fail-tok tk "~a" why)))
313 (item-val x)))
314
315;;; return (7.3), and the return that `try` makes (7.8).
316
317(defun result-forms (forms)
318 "The forms of the results to return: an aggregate one copied to its
319destination first."
320 (loop for f in forms for d in (proc-dests *proc*) for ty in (proc-results *proc*)
321 collect (if d `(move ,d ,f ,(ty-size ty)) f)))
322
323(defun leave-form (values-form)
324 "Returns VALUES-FORM, evaluated before the pending deferred statements run."
325 `(return-from ,(proc-block *proc*) (multiple-value-prog1 ,values-form ,@(pending-defers))))
326
327(defun zeros-form (results)
328 "Each result as a variable of its type starts (section 4)."
329 `(values ,@(loop for ty in results for d in (proc-dests *proc*)
330 collect (if d `(zero ,d ,(ty-size ty)) (zero-value ty)))))
331
332(defun return-zeros-form (err)
333 "Return zeros and the error ERR, as try does."
334 (let ((results (proc-results *proc*)))
335 (leave-form `(values ,@(rest (zeros-form (butlast results))) ,err))))
336
337(defun return-statement (tk)
338 (when (proc-in-defer *proc*) (fail-tok tk "return can't leave a deferred statement"))
339 (let ((results (proc-results *proc*)))
340 (if (member (kind) '(:|;| :end :else :elsif :until :|\||))
341 (progn
342 (when results (fail-tok tk "the procedure returns ~d results" (length results)))
343 (leave-form '(values)))
344 (let ((items (loop collect (expression) while (accept :|,|))))
345 (unless results (fail-tok tk "the procedure has no results to return"))
346 (if (and (null (rest items)) (rest results))
347 ;; return f(x): f's results, exactly the procedure's
348 (let ((e (first items)))
349 (unless (and (eq (item-mode e) :multi)
350 (= (length (item-type e)) (length results))
351 (every #'same-type-p (item-type e) results))
352 (fail-tok tk "the procedure returns ~d results, of its own types" (length results)))
353 (let ((vals (loop repeat (length results) collect (var-sym "V"))))
354 (leave-form `(multiple-value-bind ,vals ,(item-form e)
355 (values ,@(result-forms vals))))))
356 (progn
357 (unless (= (length items) (length results))
358 (fail-tok tk "~d results for a procedure of ~d" (length items) (length results)))
359 (when (some (lambda (i) (member (item-mode i) '(:multi :none))) items)
360 (fail-tok tk "a call with several results or none returned among others"))
361 (leave-form `(values ,@(result-forms
362 (loop for i in items for ty in results
363 collect (coerce-item i ty)))))))))))
364
365;;; defer (7.7): the statement compiled now, run where the list is left.
366
367(defun defer-statement (tk)
368 (when (proc-in-defer *proc*) (fail-tok tk "a deferred statement can't defer"))
369 (let ((forms (let ((saved-loops (proc-loops *proc*)))
370 (setf (proc-in-defer *proc*) t (proc-loops *proc*) '())
371 (push '() (proc-defers *proc*))
372 (unwind-protect (statement)
373 (pop (proc-defers *proc*))
374 (setf (proc-in-defer *proc*) nil (proc-loops *proc*) saved-loops)))))
375 (push `(progn ,@forms) (first (proc-defers *proc*)))))