raz / sbcl / src / expr.lisp

997 lines · 49.5 KiB · raw · history · last change dc5e4bcc

1;;;; expr.lisp -- expressions (report, section 6): each read, checked and
2;;;; turned into an ITEM (comp.lisp) holding the Lisp form that computes it.
3;;;;
4;;;; Constants are folded here, as the compiler must evaluate them
5;;;; (section 4); everything else becomes Lisp over the run time's memory
6;;;; operations (rt.lisp).
7
8(in-package :raz)
9
10;;; Values of items.
11
12(defun const-item (type val &optional tok)
13 (make-item :mode :const :type type :val val :tok tok))
14
15(defun val-item (type form &optional tok)
16 (make-item :mode :val :type type :form form :tok tok))
17
18(defun const-p (item) (eq (item-mode item) :const))
19
20(defun untyped-p (item)
21 (member (ty-kind (item-type item)) '(:untyped-int :untyped-real)))
22
23(defun need-value (item)
24 "ITEM, checked to be a value: not a type, a module, nor a call without one."
25 (let ((tk (item-tok item)))
26 (case (item-mode item)
27 ((:const :var :val) item)
28 (:literal (fail-tok tk "the literal's type isn't known here"))
29 (:proc (let ((sig (obj-sig (item-obj item))))
30 (val-item (make-proc-type sig) (proc-handle (item-obj item)) tk)))
31 (:case (build-case item nil))
32 (:type (fail-tok tk "the type ~a isn't a value" (type-name (item-type item))))
33 (:multi (fail-tok tk "a call with several results isn't one value"))
34 (:none (fail-tok tk "the procedure has no result"))
35 (:builtin (fail-tok tk "~a is a built-in procedure, not a value" (item-val item)))
36 (:module (fail-tok tk "a module isn't a value"))
37 (t (fail-tok tk "this isn't a value")))))
38
39(defun const-form (item)
40 "The Lisp value of a constant ITEM, as the compiled code holds it."
41 (let ((ty (item-type item)) (v (item-val item)))
42 (case (ty-kind ty)
43 (:string (string-slice v))
44 (:untyped-real v)
45 (t v))))
46
47(defun value-form (item)
48 "The Lisp form of ITEM's value: a scalar's value, an aggregate's address."
49 (let ((item (need-value item)))
50 (ecase (item-mode item)
51 (:const (const-form item))
52 (:var (if (aggregate-p (item-type item))
53 (item-form item)
54 `(ld ,(access-kind (item-type item)) ,(item-form item))))
55 (:val (item-form item))
56 (:literal (fail-tok (item-tok item) "the literal's type isn't known here")))))
57
58;;; Assignability (5.10): whether a value of ITEM can go where a TY is
59;;; wanted, and the form of it there.
60
61(defun assign-error (item ty)
62 "Nil if ITEM can be assigned to a TY, or why not."
63 (let* ((from (item-type item)) (fk (and from (ty-kind from))) (tk (ty-kind ty)))
64 (cond ((eq (item-mode item) :literal) (literal-error item ty))
65 ((and (eq (item-mode item) :proc))
66 (unless (and (eq tk :proc) (sig-alike-p (obj-sig (item-obj item)) (ty-sig ty)))
67 "the procedure's signature isn't the type's"))
68 ((eq (item-mode item) :case)
69 (unless (and (eq tk :variant) (eq (vcase-variant (item-val item)) ty))
70 "the case isn't of this variant"))
71 ((same-type-p from ty) nil)
72 ((eq fk :untyped-int)
73 (cond ((not (eq tk :int)) (format nil "an integer constant isn't ~a" (type-name ty)))
74 ((not (fits-p (item-val item) ty))
75 (format nil "~d doesn't fit ~a" (item-val item) (type-name ty)))))
76 ((eq fk :untyped-real) (unless (eq tk :real) (format nil "a real constant isn't ~a" (type-name ty))))
77 ((eq fk :nil) (unless (nil-able-p ty) (format nil "nil isn't ~a" (type-name ty))))
78 ((eq fk :string)
79 (unless (or (and (eq tk :slice) (same-type-p (ty-elem ty) (basic "U8")))
80 (and (eq tk :ptr) (same-type-p (ptr-base ty) (basic "U8")) (not (ty-vol ty)))
81 (and (eq tk :array) (same-type-p (ty-elem ty) (basic "U8"))
82 (>= (ty-len ty) (ty-len from))))
83 (format nil "a string isn't ~a" (type-name ty))))
84 ((and (eq fk :ptr) (eq tk :ptr) (ty-vol ty) (not (ty-vol from))
85 (same-type-p (ptr-base from) (ptr-base ty)))
86 nil)
87 ((and (eq fk :array) (eq tk :slice) (same-type-p (ty-elem from) (ty-elem ty)))
88 (cond ((eq (item-ro item) :export) "another module's array can't be used as a slice")
89 ((item-vol item) "a volatile array can't be used as a slice")
90 ((item-noaddr item) "a field of a packed record has no address")))
91 ((and (eq fk :int) (eq tk :int)
92 (or (and (eq from (basic "INT")) (eq ty (basic "I64")))
93 (and (eq from (basic "UINT")) (eq ty (basic "U64")))))
94 nil)
95 (t (format nil "~a isn't ~a" (type-name from) (type-name ty))))))
96
97(defun coerce-item (item ty &optional (tok (item-tok item)))
98 "The form of ITEM's value as a TY, which it must be assignable to."
99 (unless (member (item-mode item) '(:literal :proc :case))
100 (need-value item))
101 (let ((why (assign-error item ty)))
102 (when why (fail-tok tok "~a" why)))
103 (when (and (item-vol item) (aggregate-p ty))
104 (fail-tok tok "a whole record or array can't be copied through a volatile pointer"))
105 (let ((from (item-type item)))
106 (cond ((eq (item-mode item) :literal) (build-literal item ty))
107 ((eq (item-mode item) :case) (value-form (build-case item nil)))
108 ((eq (item-mode item) :proc) (proc-handle (item-obj item)))
109 ((is from :untyped-real) (if (= (ty-bits ty) 32) (coerce (item-val item) 'single-float) (item-val item)))
110 ((is from :nil) 0)
111 ((is from :string)
112 (let ((bytes (item-val item)))
113 (ecase (ty-kind ty)
114 (:slice (string-slice bytes))
115 (:ptr (string-address bytes))
116 (:array (let ((padded (make-array (ty-len ty) :element-type '(unsigned-byte 8) :initial-element 0)))
117 (replace padded bytes)
118 ;; a padded copy, the rest zero, to copy from
119 (let ((a (data-alloc (ty-len ty) 1))) (data-put a padded) a))))))
120 ((and (is from :array) (is ty :slice))
121 (let ((tmp (temp ty)))
122 `(slice ,tmp ,(item-form item) ,(ty-len from))))
123 (t (value-form item)))))
124
125;;; Storing: the form that puts ITEM, as a TY, at ADDR.
126(defun store-form (addr ty item &optional (tok (item-tok item)))
127 (let ((v (coerce-item item ty tok)))
128 (if (aggregate-p ty)
129 `(move ,addr ,v ,(ty-size ty))
130 `(st ,(access-kind ty) ,addr ,v))))
131
132;;; Literals (6.5), built once their type is known.
133
134(defun literal-error (item ty)
135 (destructuring-bind (kind elems) (item-val item)
136 (ecase kind
137 (:array (cond ((not (member (ty-kind ty) '(:array :slice)))
138 (format nil "an array literal isn't ~a" (type-name ty)))
139 ((and (is ty :array) (/= (length elems) (ty-len ty)))
140 (format nil "the literal has ~d elements, not ~d" (length elems) (ty-len ty)))
141 (t (loop for e in elems thereis (assign-error e (ty-elem ty))))))
142 (:record (if (not (is ty :record))
143 (format nil "a record literal isn't ~a" (type-name ty))
144 (loop for (name . e) in elems
145 for f = (find-field ty name (item-tok e))
146 thereis (assign-error e (field-type f)))))
147 (:set (if (not (is ty :set))
148 (format nil "a set literal isn't ~a" (type-name ty))
149 (loop for e in elems thereis (assign-error e (ty-enum ty)))))
150 (:empty (unless (member (ty-kind ty) '(:record :set))
151 (format nil "{} isn't ~a" (type-name ty)))))))
152
153(defun build-literal (item ty)
154 "The form of the literal ITEM's value as a TY: the address of a
155temporary holding it, or a set's bits."
156 (destructuring-bind (kind elems) (item-val item)
157 (cond ((is ty :set)
158 (let ((consts (remove-if-not #'const-p elems)) (vars (remove-if #'const-p elems)))
159 (let ((bits (reduce #'logior (mapcar (lambda (e) (ash 1 (item-val e))) consts) :initial-value 0)))
160 (if vars
161 `(logior ,bits ,@(mapcar (lambda (e) `(ash 1 ,(value-form e))) vars))
162 bits))))
163 ((is ty :slice)
164 ;; a hidden array of the procedure (6.5), and a slice of it
165 (let* ((arr (make-array-type (max 1 (length elems)) (ty-elem ty)))
166 (a (build-literal item (if elems arr (make-array-type 1 (ty-elem ty)))))
167 (s (temp ty)))
168 `(slice ,s ,a ,(length elems))))
169 (t
170 (let ((tmp (temp ty)) (es (ty-size (or (ty-elem ty) ty))))
171 `(let ((at ,tmp))
172 (zero at ,(ty-size ty))
173 ,@(if (eq kind :array)
174 (loop for e in elems for i from 0
175 collect (store-form `(+ at ,(* i es)) (ty-elem ty) e))
176 (let ((seen '()))
177 (loop for (name . e) in elems
178 for f = (find-field ty name (item-tok e))
179 do (when (member name seen :test #'string=)
180 (fail-tok (item-tok e) "the field ~a is given twice" name))
181 (push name seen)
182 collect (store-form `(+ at ,(field-offset f)) (field-type f) e))))
183 at))))))
184
185(defun find-field (ty name tok)
186 (let ((f (find name (ty-fields ty) :key #'field-name :test #'string=)))
187 (unless f (fail-tok tok "~a has no field ~a" (type-name ty) name))
188 (unless (or (field-exported f) (eq (ty-module ty) *module*))
189 (fail-tok tok "the field ~a isn't exported" name))
190 f))
191
192;;; A variant's case, built by calling it (5.6): a temporary holding its
193;;; tag and its fields.
194(defun build-case (item args)
195 (let* ((c (item-val item)) (vty (vcase-variant c)) (fields (vcase-fields c))
196 (tok (item-tok item)))
197 (when (and (null args) fields)
198 (fail-tok tok "the case ~a needs its fields" (vcase-name c)))
199 (unless (= (length args) (length fields))
200 (fail-tok tok "the case ~a has ~d fields, not ~d" (vcase-name c) (length fields) (length args)))
201 (let ((tmp (temp vty)))
202 (val-item vty `(let ((at ,tmp))
203 (zero at ,(ty-size vty))
204 (st :i64 at ,(vcase-index c))
205 ,@(loop for f in fields for a in args
206 collect (store-form `(+ at ,(field-offset f)) (field-type f) a))
207 at)
208 tok))))
209
210;;; Expressions, by the grammar of section 6.
211
212(defun expression ()
213 (let ((x (conjunct)))
214 (loop while (eq (kind) :or)
215 do (let* ((tk (next-tok)) (y (conjunct)))
216 (setf x (logical :or x y tk))))
217 x))
218
219(defun conjunct ()
220 (let ((x (relation)))
221 (loop while (eq (kind) :and)
222 do (let* ((tk (next-tok)) (y (relation)))
223 (setf x (logical :and x y tk))))
224 x))
225
226(defun logical (op x y tk)
227 (dolist (i (list x y))
228 (unless (is (item-type (need-value i)) :bool)
229 (fail-tok tk "~(~a~) is for BOOLs only" op)))
230 (if (and (const-p x) (const-p y))
231 (const-item (basic "BOOL") (if (eq op :and) (and (item-val x) (item-val y)) (or (item-val x) (item-val y))) tk)
232 (val-item (basic "BOOL") `(,(if (eq op :and) 'and 'or) ,(value-form x) ,(value-form y)) tk)))
233
234(defun relation ()
235 (let ((x (simple-expr)))
236 (if (member (kind) '(:= :|#| :< :<= :> :>= :in))
237 (let* ((tk (next-tok)) (y (simple-expr)))
238 (compare (tok-kind tk) x y tk))
239 x)))
240
241(defun simple-expr ()
242 (let ((x (if (eq (kind) :-)
243 (let ((tk (next-tok))) (negate (term) tk))
244 (term))))
245 (loop while (member (kind) '(:+ :- :|\|| :xor))
246 do (let* ((tk (next-tok)) (y (term)))
247 (setf x (arith (tok-kind tk) x y tk))))
248 x))
249
250(defun term ()
251 (let ((x (factor)))
252 (loop while (member (kind) '(:* :/ :div :mod :& :shl :shr))
253 do (let* ((tk (next-tok)) (y (factor)))
254 (setf x (arith (tok-kind tk) x y tk))))
255 x))
256
257(defun factor ()
258 (let ((tk (tok)))
259 (case (kind)
260 (:int (next-tok)
261 (const-item (if (> (tok-val tk) (int-max (basic "I64"))) (basic "U64") *untyped-int*) (tok-val tk) tk))
262 (:char (next-tok) (const-item *untyped-int* (tok-val tk) tk))
263 (:real (next-tok) (const-item *untyped-real* (tok-val tk) tk))
264 (:string (next-tok) (const-item (string-type (length (tok-val tk))) (tok-val tk) tk))
265 (:|(| (next-tok) (prog1 (expression) (expect :|)|)))
266 (:not (next-tok)
267 (let ((x (need-value (factor))))
268 (unless (is (item-type x) :bool) (fail-tok tk "not is for BOOLs only"))
269 (if (const-p x)
270 (const-item (item-type x) (not (item-val x)) tk)
271 (val-item (item-type x) `(not ,(value-form x)) tk))))
272 (:~ (next-tok)
273 (let* ((x (need-value (factor))) (ty (item-type x)))
274 (unless (member (ty-kind ty) '(:int :untyped-int)) (fail-tok tk "~~ is for integers only"))
275 (cond ((and (const-p x) (is ty :untyped-int)) (const-item ty (lognot (item-val x)) tk))
276 ((const-p x) (const-item ty (wrap-int (lognot (item-val x)) ty) tk))
277 (t (val-item ty (wrap-form ty `(lognot ,(value-form x))) tk)))))
278 (:|[| (next-tok)
279 (let ((elems '()))
280 (unless (eq (kind) :|]|)
281 (loop (push (expression) elems) (unless (accept :|,|) (return))))
282 (expect :|]|)
283 (make-item :mode :literal :val (list :array (nreverse elems)) :tok tk)))
284 (:{ (next-tok) (brace-literal tk))
285 (:^ (next-tok)
286 (let ((ty (pointer-type-rest)))
287 (expect :|(|)
288 ;; selectors may follow, as in ^T(x)^ (section 6)
289 (selectors (prog1 (convert ty (expression) tk) (expect :|)|)))))
290 (:try (next-tok) (try-item tk))
291 (:ident (designator))
292 (t (fail-here "an expression expected, not ~a" (describe-tok tk))))))
293
294(defun brace-literal (tk)
295 "{...}: a record literal if its first element names a field, else a set."
296 (cond ((accept :})
297 (make-item :mode :literal :val (list :empty nil) :tok tk))
298 ((and (eq (kind) :ident) (eq (kind 1) :|:|))
299 (let ((fields '()))
300 (loop (let ((ftk (tok)) (name (ident)))
301 (expect :|:|)
302 (let ((e (expression))) (setf (item-tok e) (or (item-tok e) ftk)) (push (cons name e) fields)))
303 (unless (accept :|,|) (return)))
304 (expect :})
305 (make-item :mode :literal :val (list :record (nreverse fields)) :tok tk)))
306 (t
307 (let ((elems '()))
308 (loop (push (need-value (expression)) elems) (unless (accept :|,|) (return)))
309 (expect :})
310 (setf elems (nreverse elems))
311 ;; the set's type is its first name's enumeration's set (6.6)
312 (let ((enum (item-type (first elems))))
313 (unless (is enum :enum) (fail-tok tk "a set's elements are names of an enumeration"))
314 (dolist (e elems)
315 (unless (same-type-p (item-type e) enum)
316 (fail-tok (item-tok e) "the set's elements are of two enumerations")))
317 (let ((item (make-item :mode :literal :val (list :set elems) :tok tk))
318 (sty (set-type-of enum)))
319 (if (every #'const-p elems)
320 (const-item sty (build-literal item sty) tk)
321 (val-item sty (build-literal item sty) tk))))))))
322
323(defun set-type-of (enum)
324 "The anonymous set type of ENUM: alike wherever it is made."
325 (make-set-type enum))
326
327;;; Integers and reals: the operands' common type (6.1, 6.3).
328
329(defun wrap-form (ty form)
330 (if (ty-signed ty)
331 `(sb-c::mask-signed-field ,(ty-bits ty) ,form)
332 `(ldb (byte ,(ty-bits ty) 0) ,form)))
333
334(defun unify (x y tk)
335 "The type both X and Y have, an untyped constant taking the other's."
336 (let ((a (item-type x)) (b (item-type y)))
337 (flet ((takes (c ty) ; constant C taking type TY
338 (let ((why (assign-error c ty)))
339 (when why (fail-tok tk "~a" why)))))
340 (cond ((and (untyped-p x) (untyped-p y))
341 (unless (eq a b) (fail-tok tk "an integer and a real don't mix"))
342 a)
343 ((untyped-p x) (takes x b) b)
344 ((untyped-p y) (takes y a) a)
345 ((same-type-p a b) a)
346 (t (fail-tok tk "~a and ~a don't mix" (type-name a) (type-name b)))))))
347
348(defun typed-const (item ty)
349 "The value of constant ITEM as a TY."
350 (let ((v (item-val item)))
351 (if (and (is ty :real) (= (ty-bits ty) 32) (typep v 'double-float)) (coerce v 'single-float) v)))
352
353(defun check-const-int (v tk)
354 (unless (<= (- (expt 2 63)) v (1- (expt 2 63)))
355 (fail-tok tk "the constant ~d is beyond the I64s" v))
356 v)
357
358(defun arith (op x y tk)
359 "X op Y for the adding and multiplying operators."
360 (setf x (need-value x) y (need-value y))
361 (let ((a (item-type x)) (b (item-type y)))
362 (cond
363 ;; strings join (section 4)
364 ((and (eq op :+) (is a :string) (is b :string) (const-p x) (const-p y))
365 (let ((s (concatenate '(vector (unsigned-byte 8)) (item-val x) (item-val y))))
366 (const-item (string-type (length s)) (coerce s '(simple-array (unsigned-byte 8) (*))) tk)))
367 ;; pointers move by elements (6.4)
368 ((and (is a :ptr) (member op '(:+ :-)) (member (ty-kind b) '(:int :untyped-int)))
369 (val-item a `(ldb (byte 64 0) (,(if (eq op :+) '+ '-) ,(value-form x)
370 (* ,(value-form y) ,(ty-size (ptr-base a)))))
371 tk))
372 ((and (is a :ptr) (eq op :-) (is b :ptr))
373 (unless (same-type-p a b) (fail-tok tk "~a and ~a don't mix" (type-name a) (type-name b)))
374 (val-item (basic "INT") `(values (truncate (sb-c::mask-signed-field 64 (- ,(value-form x) ,(value-form y)))
375 ,(max 1 (ty-size (ptr-base a)))))
376 tk))
377 ((member op '(:shl :shr)) (shift op x y tk))
378 (t
379 (let ((ty (unify x y tk)))
380 (case (ty-kind ty)
381 ((:int :untyped-int) (int-arith op x y ty tk))
382 ((:real :untyped-real) (real-arith op x y ty tk))
383 (:set (set-arith op x y ty tk))
384 (t (fail-tok tk "~(~a~) isn't defined on ~a" op (type-name ty)))))))))
385
386(defun int-arith (op x y ty tk)
387 (unless (member op '(:+ :- :* :div :mod :& :|\|| :xor))
388 (fail-tok tk "~(~a~) isn't defined on integers~@[; div divides them~]" op (eq op :/)))
389 (let ((lisp-op (ecase op (:+ '+) (:- '-) (:* '*) (:& 'logand) (:|\|| 'logior) (:xor 'logxor)
390 (:div 'div) (:mod 'mod))))
391 (if (and (const-p x) (const-p y))
392 (let* ((a (item-val x)) (b (item-val y))
393 (v (case lisp-op
394 ((div mod)
395 (when (= b 0) (fail-tok tk "a division by zero in a constant"))
396 (when (and (is ty :int) (ty-signed ty) (= b -1) (= a (int-min ty)))
397 (fail-tok tk "the smallest ~a divided by -1 in a constant" (type-name ty)))
398 (if (eq lisp-op 'mod) (rem a b) (values (truncate a b))))
399 (t (funcall lisp-op a b)))))
400 (const-item ty (if (is ty :untyped-int) (check-const-int v tk) (wrap-int v ty)) tk))
401 (let* ((ty (if (is ty :untyped-int) (basic "INT") ty))
402 (a (coerce-item x ty tk)) (b (coerce-item y ty tk)))
403 (val-item ty
404 (if (member lisp-op '(div mod))
405 `(divide ,a ,b ,(ty-bits ty) ,(ty-signed ty) ,(where tk) ,(eq lisp-op 'mod))
406 (wrap-form ty `(,lisp-op ,a ,b)))
407 tk)))))
408
409(defun real-arith (op x y ty tk)
410 (unless (member op '(:+ :- :* :/))
411 (fail-tok tk "~(~a~) isn't defined on reals" op))
412 (let ((lisp-op (ecase op (:+ '+) (:- '-) (:* '*) (:/ '/))))
413 (if (and (const-p x) (const-p y))
414 (const-item ty (funcall lisp-op (typed-const x ty) (typed-const y ty)) tk)
415 (val-item ty `(,lisp-op ,(coerce-item x ty tk) ,(coerce-item y ty tk)) tk))))
416
417(defun set-arith (op x y ty tk)
418 (let ((f (case op (:+ 'logior) (:* 'logand) (:- 'logandc2)
419 (t (fail-tok tk "~(~a~) isn't defined on sets" op)))))
420 (if (and (const-p x) (const-p y))
421 (const-item ty (funcall f (item-val x) (item-val y)) tk)
422 (val-item ty `(,f ,(value-form x) ,(value-form y)) tk))))
423
424(defun shift (op x y tk)
425 ;; by y modulo the width of x's type; an untyped x stays untyped (6.1)
426 (let ((a (item-type x)) (b (item-type y)))
427 (unless (member (ty-kind b) '(:int :untyped-int)) (fail-tok tk "a shift's count is an integer"))
428 (unless (member (ty-kind a) '(:int :untyped-int)) (fail-tok tk "~(~a~) is for integers" op))
429 (when (and (is a :untyped-int) (not (const-p y)))
430 (setf a (basic "INT")))
431 (cond ((and (is a :untyped-int) (const-p x))
432 (let ((n (item-val y)))
433 (when (< n 0) (fail-tok tk "a negative shift of a constant"))
434 (const-item a (check-const-int (if (eq op :shl) (ash (item-val x) (min n 64)) (ash (item-val x) (- n))) tk) tk)))
435 ((and (const-p x) (const-p y))
436 (let ((n (mod (item-val y) (ty-bits a))))
437 (const-item a (wrap-int (ash (item-val x) (if (eq op :shl) n (- n))) a) tk)))
438 (t (let ((n `(mod ,(value-form y) ,(ty-bits a))))
439 (val-item a (if (eq op :shl)
440 (wrap-form a `(ash ,(coerce-item x a tk) ,n))
441 `(ash ,(coerce-item x a tk) (- ,n)))
442 tk))))))
443
444(defun negate (x tk)
445 (let* ((x (need-value x)) (ty (item-type x)))
446 (case (ty-kind ty)
447 (:untyped-int (if (const-p x) (const-item ty (- (item-val x)) tk) (fail-tok tk "?")))
448 (:untyped-real (const-item ty (- (item-val x)) tk))
449 (:int (if (const-p x)
450 (const-item ty (wrap-int (- (item-val x)) ty) tk)
451 (val-item ty (wrap-form ty `(- ,(value-form x))) tk)))
452 (:real (if (const-p x)
453 (const-item ty (- (item-val x)) tk)
454 (val-item ty `(- ,(value-form x)) tk)))
455 (t (fail-tok tk "- isn't defined on ~a" (type-name ty))))))
456
457;;; Relations.
458
459(defun sequence-parts (item)
460 "For an array, a slice or a string: the forms of its first element's
461address and its length, and its element type."
462 (let ((ty (item-type item)))
463 (ecase (ty-kind ty)
464 (:string (values (string-address (item-val item)) (length (item-val item)) (basic "U8")))
465 (:array (values (value-form item) (ty-len ty) (ty-elem ty)))
466 (:slice (let ((s (var-sym "S")))
467 (values `(ld :u64 ,s) `(ld :i64 (+ ,s 8)) (ty-elem ty) s (value-form item)))))))
468
469(defun compare (op x y tk)
470 (when (eq op :in) (return-from compare (membership x y tk)))
471 ;; a literal or nil takes the other side's type
472 (when (eq (item-mode x) :literal) (rotatef x y) (setf op (case op (:< :>) (:> :<) (:<= :>=) (:>= :<=) (t op))))
473 (setf x (need-value x))
474 (when (eq (item-mode y) :literal)
475 (setf y (val-item (item-type x) (coerce-item y (item-type x) tk) tk)))
476 (setf y (need-value y))
477 (let ((a (item-type x)) (b (item-type y)) (bool (basic "BOOL")))
478 (cond
479 ((and (member (ty-kind a) '(:array :slice :string)) (member (ty-kind b) '(:array :slice :string)))
480 (sequence-compare op x y tk))
481 ((or (is a :nil) (is b :nil))
482 (let ((other (if (is a :nil) b a)))
483 (unless (or (nil-able-p other) (is other :nil)) (fail-tok tk "nil isn't ~a" (type-name other)))
484 (unless (member op '(:= :|#|)) (fail-tok tk "~(~a~) doesn't compare nil" op))
485 (val-item bool `(,(if (eq op :=) '= '/=) ,(value-form x) ,(value-form y)) tk)))
486 (t
487 (let* ((ty (unify x y tk)) (k (ty-kind ty)))
488 (unless (member k '(:int :untyped-int :real :untyped-real :bool :enum :set :ptr :proc :error))
489 (fail-tok tk "~a doesn't compare" (type-name ty)))
490 (when (and (member k '(:bool :proc :error)) (not (member op '(:= :|#|))))
491 (fail-tok tk "~a compares with = and # only" (type-name ty)))
492 (when (and (eq k :set) (member op '(:< :>)))
493 (fail-tok tk "sets compare with =, #, <= and >="))
494 (let ((f (if (eq k :set)
495 (ecase op (:= '=) (:|#| '/=) (:<= 'subset) (:>= 'superset))
496 (ecase op (:= (if (eq k :bool) 'eq '=)) (:|#| (if (eq k :bool) 'neq '/=))
497 (:< '<) (:<= '<=) (:> '>) (:>= '>=)))))
498 (if (and (const-p x) (const-p y))
499 (const-item bool (funcall f (typed-const x ty) (typed-const y ty)) tk)
500 (val-item bool `(,f ,(coerce-item x ty tk) ,(coerce-item y ty tk)) tk))))))))
501
502(defun neq (a b) (not (eq a b)))
503(defun subset (a b) (= 0 (logandc2 a b)))
504(defun superset (a b) (= 0 (logandc2 b a)))
505
506(defun sequence-compare (op x y tk)
507 ;; equal lengths and equal elements, for elements that compare by bytes (6.5)
508 (unless (member op '(:= :|#|)) (fail-tok tk "arrays and strings compare with = and # only"))
509 (multiple-value-bind (pa la ea sa fa) (sequence-parts x)
510 (multiple-value-bind (pb lb eb sb fb) (sequence-parts y)
511 (unless (same-type-p ea eb) (fail-tok tk "the two sides' elements are of two types"))
512 (unless (bytewise-comparable-p ea)
513 (fail-tok tk "arrays of ~a don't compare" (type-name ea)))
514 (when (or (item-vol x) (item-vol y)) (fail-tok tk "a volatile array doesn't compare"))
515 (let ((form `(let* (,@(when sa `((,sa ,fa))) ,@(when sb `((,sb ,fb)))
516 (la ,la) (lb ,lb))
517 (and (= la lb) (bytes-equal ,pa ,pb (* la ,(ty-size ea)))))))
518 (val-item (basic "BOOL") (if (eq op :=) form `(not ,form)) tk)))))
519
520(defun membership (x y tk)
521 (setf x (need-value x) y (need-value y))
522 (unless (and (is (item-type y) :set) (same-type-p (item-type x) (ty-enum (item-type y))))
523 (fail-tok tk "in takes a name of an enumeration and a set of it"))
524 (if (and (const-p x) (const-p y))
525 (const-item (basic "BOOL") (logbitp (item-val x) (item-val y)) tk)
526 (val-item (basic "BOOL") `(logbitp ,(value-form x) ,(value-form y)) tk)))
527
528;;; Designators: a name and its selectors (section 6).
529
530(defun qualified-obj ()
531 "The object a QualIdent names, a module's export if it starts with a module."
532 (let* ((tk (tok)) (name (ident)) (obj (lookup name)))
533 (unless obj (fail-tok tk "~a isn't declared" name))
534 (if (eq (obj-kind obj) :module)
535 (progn
536 (expect :|.|)
537 (let* ((mtk (tok)) (member (ident)) (m (import-module (obj-val obj) tk))
538 (o (gethash member (module-exports m))))
539 (unless o
540 (fail-tok mtk "~a has no export ~a" (obj-name obj) member))
541 (values o mtk)))
542 (values obj tk))))
543
544(defun obj-item (obj tk)
545 "The item of a name."
546 (ecase (obj-kind obj)
547 (:const (if (aggregate-p (obj-type obj))
548 (if (is (obj-type obj) :string)
549 (const-item (obj-type obj) (obj-val obj) tk)
550 (make-item :mode :var :type (obj-type obj) :form (obj-val obj) :ro :const :tok tk))
551 (const-item (obj-type obj) (obj-val obj) tk)))
552 (:type (make-item :mode :type :type (obj-type obj) :tok tk))
553 (:var (make-item :mode :var :type (obj-type obj) :form (var-address obj) :tok tk
554 :ro (when (and (obj-module obj) (not (eq (obj-module obj) *module*))) :export)))
555 (:proc (make-item :mode :proc :obj obj :tok tk))
556 (:builtin (make-item :mode :builtin :val (obj-name obj) :tok tk))
557 (:case (make-item :mode :case :val (obj-val obj) :type (vcase-variant (obj-val obj)) :tok tk))))
558
559(defun var-address (obj)
560 (let ((w (obj-where obj)))
561 (ecase (first w)
562 (:global (second w))
563 (:local `(+ fp ,(second w)))
564 (:var-param `(ld :u64 (+ fp ,(second w))))
565 (:at (second w))
566 (:extern `(sb-sys:find-foreign-symbol-address ,(second w))))))
567
568(defun designator ()
569 (multiple-value-bind (obj tk) (qualified-obj)
570 (selectors (obj-item obj tk))))
571
572(defun selectors (item)
573 (loop
574 (let ((tk (tok)))
575 (case (kind)
576 (:|.| (next-tok) (setf item (select-field item (ident) tk)))
577 (:^ (next-tok) (setf item (dereference item tk)))
578 (:|[| (next-tok) (setf item (index item tk)))
579 (:|(| (setf item (call item tk)))
580 (t (return item))))))
581
582(defun select-field (item name tk)
583 (let* ((item (need-value item)) (ty (item-type item)))
584 (case (ty-kind ty)
585 (:record
586 (let ((f (find-field ty name tk)))
587 (make-item :mode :var :type (field-type f) :tok tk
588 :form `(+ ,(value-form item) ,(field-offset f))
589 :ro (or (item-ro item) (and (eq (item-mode item) :val) :value))
590 :vol (item-vol item)
591 :noaddr (or (item-noaddr item)
592 (and (ty-packed ty) (> (ty-align (field-type f)) 1))))))
593 (:ptr (fail-tok tk "a pointer's fields are p^.~a" name))
594 (:variant (fail-tok tk "a variant's fields are read by match"))
595 (t (fail-tok tk "~a has no fields" (type-name ty))))))
596
597(defun dereference (item tk)
598 (let* ((item (need-value item)) (ty (item-type item)))
599 (unless (is ty :ptr) (fail-tok tk "^ follows a pointer, not ~a" (type-name ty)))
600 (make-item :mode :var :type (ptr-base ty) :form (value-form item) :vol (ty-vol ty) :tok tk)))
601
602(defun integer-operand (tk what)
603 "An expression of any integer type (6.1): the form of its value."
604 (let ((x (need-value (expression))))
605 (unless (member (ty-kind (item-type x)) '(:int :untyped-int))
606 (fail-tok tk "~a is an integer, not ~a" what (type-name (item-type x))))
607 (value-form x)))
608
609(defun index (item tk)
610 "a[i] or a[lo..hi]; the `[` is read."
611 (let* ((item (need-value item)) (ty (item-type item)) (check (not (module-check-off *module*))))
612 (when (is ty :string)
613 ;; a string constant is indexed and sliced as an array of U8
614 (setf ty (make-array-type (ty-len ty) (basic "U8"))
615 item (make-item :mode :var :type ty :form (string-address (item-val item)) :ro :string :tok tk)))
616 (unless (member (ty-kind ty) '(:array :slice :ptr))
617 (fail-tok tk "~a can't be indexed" (type-name ty)))
618 (when (and (is ty :ptr) (is (ptr-base ty) :array))
619 (fail-tok tk "a pointer to an array is indexed as p^[i]"))
620 (let* ((elem (if (is ty :ptr) (ptr-base ty) (ty-elem ty)))
621 (size (ty-size elem))
622 (base (var-sym "A")) (len (var-sym "N"))
623 (lo (integer-operand tk "an index")))
624 (multiple-value-bind (base-form len-form)
625 (ecase (ty-kind ty)
626 (:array (values (value-form item) (ty-len ty)))
627 (:slice (values `(ld :u64 ,(value-form item)) `(ld :i64 (+ ,(value-form item) 8))))
628 (:ptr (values (value-form item) nil)))
629 (if (accept :..)
630 (let ((hi (integer-operand tk "a slice's bound")) (h (var-sym "H")) (l (var-sym "L"))
631 (sty (make-slice-type elem)))
632 (expect :|]|)
633 (when (is ty :array)
634 (case (item-ro item)
635 (:export (fail-tok tk "another module's array can't be sliced")))
636 (when (item-vol item) (fail-tok tk "a volatile array can't be sliced")))
637 (when (and (item-noaddr item) (> (ty-align elem) 1))
638 (fail-tok tk "a field of a packed record has no address to slice"))
639 (val-item sty
640 `(let* ((,base ,(if (is ty :slice) (value-form item) base-form))
641 (,l ,lo) (,h ,hi)
642 ,@(when (is ty :slice) `((,len (ld :i64 (+ ,base 8))) (,base (ld :u64 ,base)))))
643 ,(when (or check (is ty :ptr))
644 `(check-slice ,l ,h ,(cond ((is ty :ptr) nil) ((is ty :slice) len) (t len-form)) ,(where tk)))
645 (slice ,(temp sty) (ldb (byte 64 0) (+ ,base (* ,l ,size))) (- ,h ,l)))
646 tk))
647 (progn
648 (expect :|]|)
649 (make-item :mode :var :type elem :tok tk
650 :form (cond ((is ty :ptr) `(ldb (byte 64 0) (+ ,base-form (* ,lo ,size))))
651 ((is ty :slice)
652 `(let ((,base ,(value-form item)))
653 (+ (ld :u64 ,base)
654 (* ,(if check `(check-index ,lo (ld :i64 (+ ,base 8)) ,(where tk)) lo)
655 ,size))))
656 ((and (integerp lo) (not (< -1 lo len-form)))
657 ;; a constant index out of an array would stop the program
658 (fail-tok tk "index ~d is out of range for a length of ~d" lo len-form))
659 (t `(+ ,base-form (* ,(if check `(check-index ,lo ,len-form ,(where tk)) lo) ,size))))
660 :ro (if (is ty :array) (item-ro item) nil)
661 :vol (and (is ty :array) (item-vol item))
662 :noaddr (and (is ty :array) (item-noaddr item)))))))))
663
664;;; Conversions (6.7).
665
666(defun convert (ty x tk)
667 (let* ((x (need-value x)) (from (item-type x)) (fk (ty-kind from)) (tk2 (ty-kind ty)))
668 (flet ((bad () (fail-tok tk "~a doesn't convert to ~a" (type-name from) (type-name ty)))
669 (result (const form) (if (const-p x) (const-item ty const tk) (val-item ty form tk))))
670 (let ((v (unless (const-p x) (value-form x))) (c (when (const-p x) (item-val x))))
671 (case tk2
672 (:int
673 (case fk
674 ((:int :untyped-int) (result (and c (wrap-int c ty)) (wrap-form ty v)))
675 ((:real :untyped-real)
676 (result (and c (real-to-int c (ty-bits ty) (ty-signed ty)))
677 `(real-to-int ,v ,(ty-bits ty) ,(ty-signed ty))))
678 ((:enum :set) (result c v))
679 ((:ptr :proc)
680 (unless (member (ty-name ty) '("INT" "UINT") :test #'equal) (bad))
681 (val-item ty (if (ty-signed ty) `(sb-c::mask-signed-field 64 ,v) v) tk))
682 (t (bad))))
683 (:real
684 (let ((lisp-ty (if (= (ty-bits ty) 32) 'single-float 'double-float)))
685 (case fk
686 ((:int :untyped-int :real :untyped-real) (result (and c (coerce c lisp-ty)) `(coerce ,v ',lisp-ty)))
687 (t (bad)))))
688 (:enum
689 (unless (member fk '(:int :untyped-int)) (if (same-type-p from ty) (return-from convert x) (bad)))
690 (let ((n (length (ty-names ty))))
691 (when (and c (not (< -1 c n))) (fail-tok tk "~d is no position of the enumeration" c))
692 (result c `(check-enum ,v ,n ,(where tk)))))
693 (:set
694 (unless (member fk '(:int :untyped-int)) (if (same-type-p from ty) (return-from convert x) (bad)))
695 (result (and c (ldb (byte (* 8 (ty-size ty)) 0) c)) `(ldb (byte ,(* 8 (ty-size ty)) 0) ,v)))
696 ((:ptr :proc)
697 (cond ((member fk '(:ptr :proc)) (val-item ty (value-form x) tk))
698 ((or (eq fk :untyped-int)
699 (and (eq fk :int) (member (ty-name from) '("INT" "UINT") :test #'equal)))
700 (val-item ty `(ldb (byte 64 0) ,(value-form x)) tk))
701 ((eq fk :nil) (val-item ty 0 tk))
702 (t (bad))))
703 (:bool (if (eq fk :bool) x (bad)))
704 (t (bad)))))))
705
706;;; Calls (section 8): of procedures, procedure values, built-ins,
707;;; conversions and variants' cases.
708
709(defun call (item tk)
710 "ITEM applied to the arguments that follow, `(` and all."
711 (case (item-mode item)
712 (:type (next-tok)
713 (prog1 (convert (item-type item) (expression) tk) (expect :|)|)))
714 (:builtin (next-tok) (builtin (item-val item) tk))
715 (:case (next-tok)
716 (let ((args (unless (eq (kind) :|)|) (loop collect (expression) while (accept :|,|)))))
717 (expect :|)|)
718 (when (null args) (fail-tok tk "() after a case without fields"))
719 (build-case item args)))
720 (:proc (call-proc (item-obj item) nil tk))
721 (t (let ((v (need-value item)))
722 (unless (is (item-type v) :proc) (fail-tok tk "~a isn't a procedure" (type-name (item-type v))))
723 (call-proc nil v tk)))))
724
725(defun read-args ()
726 "The arguments in parentheses, if there are parentheses."
727 (when (accept :|(|)
728 (prog1 (unless (eq (kind) :|)|) (loop collect (expression) while (accept :|,|)))
729 (expect :|)|))))
730
731(defun arg-forms (sig args tk)
732 "The forms passing ARGS to the parameters of SIG."
733 (let ((params (sig-params sig)))
734 (when (or (< (length args) (length params))
735 (and (> (length args) (length params)) (not (sig-varargs sig))))
736 (fail-tok tk "~d arguments for ~d parameters" (length args) (length params)))
737 (loop for a in args for i from 0
738 for p = (nth i params)
739 collect (cond ((null p) (vararg-form a))
740 ((third p) (var-arg-form a (second p)))
741 (t (coerce-item a (second p) (item-tok a)))))))
742
743(defun var-arg-form (a ty)
744 "A `var` argument: the address of a variable of exactly the type."
745 (let ((tk (item-tok a)))
746 (unless (eq (item-mode a) :var) (fail-tok tk "a var parameter takes a variable"))
747 (check-writable a tk "passed as a var argument")
748 (when (item-vol a) (fail-tok tk "a volatile variable isn't passed as a var argument"))
749 (when (item-noaddr a) (fail-tok tk "a field of a packed record has no address"))
750 (unless (same-type-p (item-type a) ty)
751 (fail-tok tk "a var parameter of ~a takes a variable of it, not ~a" (type-name ty) (type-name (item-type a))))
752 (item-form a)))
753
754(defun check-writable (a tk what)
755 (case (item-ro a)
756 (:export (fail-tok tk "another module's variable can't be ~a" what))
757 (:const (fail-tok tk "a constant can't be ~a" what))
758 (:string (fail-tok tk "a string's bytes can't be ~a" what))
759 (:value (fail-tok tk "a part of a value can't be ~a" what))))
760
761(defun vararg-form (a)
762 "An argument past an extern's parameters (section 9): as C takes it."
763 (let* ((a (need-value a)) (ty (item-type a)) (tk (item-tok a)))
764 (case (ty-kind ty)
765 (:untyped-int (list 'sb-alien:long (item-val a)))
766 (:untyped-real (list 'double-float (item-val a)))
767 (:string (list 'sb-alien:unsigned-long (string-address (item-val a))))
768 (:real (when (= (ty-bits ty) 32) (fail-tok tk "C takes no F32 past the parameters: F64(x)"))
769 (list 'double-float (value-form a)))
770 ((:int :bool :ptr :proc) (list (alien-type ty) (c-value-form ty (value-form a))))
771 (t (fail-tok tk "~a doesn't go to C" (type-name ty))))))
772
773(defun alien-type (ty)
774 (ecase (ty-kind ty)
775 (:int `(,(if (ty-signed ty) 'sb-alien:signed 'sb-alien:unsigned) ,(ty-bits ty)))
776 (:bool '(sb-alien:unsigned 8))
777 (:real (if (= (ty-bits ty) 32) 'single-float 'double-float))
778 ((:ptr :proc :error) 'sb-alien:unsigned-long)))
779
780(defun c-value-form (ty form)
781 "FORM's value as C takes it: a BOOL as 0 or 1, a procedure as the
782address of a C function that calls it."
783 (case (ty-kind ty)
784 (:bool `(if ,form 1 0))
785 (:proc (callback-form (ty-sig ty) form))
786 (t form)))
787
788(defun callback-form (sig form)
789 "The address of a C function calling the procedure value FORM, made by
790SBCL the first time that value goes to C (section 9)."
791 (let* ((params (sig-params sig)) (result (first (sig-results sig)))
792 (args (loop for p in params collect (var-sym "A"))))
793 `(c-callback ,form
794 (lambda (fn)
795 (sb-alien::alien-sap
796 (sb-alien::alien-lambda ,(if result (alien-type result) 'sb-alien:void)
797 ,(loop for a in args for p in params collect (list a (alien-type (second p))))
798 (in-raz-thread
799 ,(let ((call `(funcall fn ,@(loop for a in args for p in params
800 collect (if (is (second p) :bool) `(/= 0 ,a) a)))))
801 (cond ((null result) `(progn ,call (values)))
802 ((is result :bool) `(if ,call 1 0))
803 (t call))))))))))
804
805(defun call-proc (obj value tk)
806 "A call of procedure OBJ, or of the procedure VALUE: an item of its results."
807 (let* ((sig (if obj (obj-sig obj) (ty-sig (item-type value))))
808 (fn (when value (value-form value)))
809 (args (read-args))
810 (forms (arg-forms sig args tk))
811 (results (sig-results sig))
812 (dests (loop for r in results when (aggregate-p r) collect (temp r)))
813 (form (cond ((and obj (sig-varargs sig))
814 (extern-call-form obj sig forms))
815 (obj `(,(obj-sym obj) ,@forms ,@dests))
816 (t `(funcall (proc-fn ,fn) ,@forms ,@dests)))))
817 (let ((item (cond ((null results) (make-item :mode :none :form form :tok tk))
818 ((null (rest results)) (val-item (first results) form tk))
819 (t (make-item :mode :multi :type results :form form :tok tk)))))
820 (setf (item-obj item) (list :call results))
821 item)))
822
823(defun extern-call-form (obj sig forms)
824 "A call of a C procedure with `...`: the arguments past its parameters
825as VARARG-FORM made them, (alien-type form)."
826 (let* ((params (sig-params sig)) (n (length params)) (r (first (sig-results sig))))
827 (c-result-form r
828 `(sb-alien:alien-funcall
829 (sb-alien:extern-alien ,(obj-extern obj)
830 (function ,(if r (alien-type r) 'sb-alien:void)
831 ,@(mapcar (lambda (p) (alien-type (second p))) params)
832 ,@(mapcar #'first (nthcdr n forms))))
833 ,@(loop for p in params for f in forms collect (c-value-form (second p) f))
834 ,@(mapcar #'second (nthcdr n forms))))))
835
836(defun c-result-form (ty form)
837 (cond ((null ty) `(progn ,form (values)))
838 ((is ty :bool) `(/= 0 ,form))
839 (t form)))
840
841;;; try (7.8): the call's error looked at; if it isn't nil, the procedure
842;;; returns it at once.
843
844(defun try-item (tk)
845 (when (and *proc* (proc-in-defer *proc*)) (fail-tok tk "try can't leave a deferred statement"))
846 (let* ((item (designator))
847 (item (if (eq (item-mode item) :proc) (call-proc (item-obj item) nil tk) item))
848 (call (item-obj item)))
849 (unless (and (consp call) (eq (first call) :call))
850 (fail-tok tk "try is applied to a call of a procedure"))
851 (let ((results (second call)))
852 (unless (and results (is (car (last results)) :error))
853 (fail-tok tk "try needs a procedure whose last result is an ERROR"))
854 (unless (and *proc* (proc-results *proc*) (is (car (last (proc-results *proc*))) :error))
855 (fail-tok tk "try stands in a procedure whose last result is an ERROR"))
856 (let* ((vals (butlast results))
857 (syms (loop repeat (length vals) collect (var-sym "V")))
858 (e (var-sym "E"))
859 (form `(multiple-value-bind (,@syms ,e) ,(item-form item)
860 (unless (= ,e 0) ,(return-zeros-form e))
861 (values ,@syms))))
862 (cond ((null vals) (make-item :mode :none :form form :tok tk))
863 ((null (rest vals)) (val-item (first vals) form tk))
864 (t (make-item :mode :multi :type vals :form form :tok tk)))))))
865
866;;; The built-in procedures (6.8); the `(` is read.
867
868(defun builtin-args ()
869 (prog1 (unless (eq (kind) :|)|) (loop collect (expression) while (accept :|,|)))
870 (expect :|)|)))
871
872(defun builtin (name tk)
873 "The item of a call of the built-in procedure NAME, marked as a call so
874that it may stand as a statement."
875 (let ((item (builtin-call name tk)))
876 (unless (item-obj item) (setf (item-obj item) (list :builtin)))
877 item))
878
879(defun builtin-call (name tk)
880 (flet ((arity (args n &optional (m n))
881 (unless (<= n (length args) m)
882 (fail-tok tk "~a takes ~d arguments, not ~d" name n (length args)))
883 args))
884 (cond
885 ((string= name "SIZE")
886 (let ((ty (parse-type)))
887 (expect :|)|)
888 (const-item *untyped-int* (ty-size ty) tk)))
889 (t
890 (let ((args (builtin-args)))
891 (macrolet ((arg (i) `(nth ,i args)))
892 (cond
893 ((string= name "ADR")
894 (arity args 1)
895 (let ((x (arg 0)))
896 (unless (eq (item-mode x) :var) (fail-tok tk "ADR takes a variable"))
897 ;; a string's bytes have an address, as a ^U8 made from it does (5.10)
898 (unless (eq (item-ro x) :string) (check-writable x tk "given to ADR"))
899 (when (item-noaddr x) (fail-tok tk "a field of a packed record has no address"))
900 (val-item (make-ptr-type (item-type x) (item-vol x)) (item-form x) tk)))
901 ((string= name "LEN")
902 (arity args 1)
903 (let* ((x (need-value (arg 0))) (ty (item-type x)))
904 (case (ty-kind ty)
905 (:array (const-item *untyped-int* (ty-len ty) tk))
906 (:string (const-item *untyped-int* (ty-len ty) tk))
907 (:slice (val-item (basic "INT") `(ld :i64 (+ ,(value-form x) 8)) tk))
908 (t (fail-tok tk "LEN takes an array, a slice or a string, not ~a" (type-name ty))))))
909 ((string= name "MSG")
910 (arity args 1)
911 (val-item (make-slice-type (basic "U8"))
912 `(let ((e ,(coerce-item (arg 0) (basic "ERROR")))) (if (= e 0) ,(string-slice #()) e))
913 tk))
914 ((string= name "NEW") (arity args 2) (new-form (arg 0) (arg 1) tk))
915 ((string= name "COPY") (arity args 2) (copy-form (arg 0) (arg 1) tk))
916 ((member name '("INC" "DEC") :test #'string=) (arity args 1 2) (inc-form name args tk))
917 ((string= name "FENCE") (arity args 0)
918 (make-item :mode :none :form '(sb-thread:barrier (:memory)) :tok tk))
919 ((member name '("CAS" "XCHG" "XADD") :test #'string=) (atomic name args tk))
920 ((string= name "SYSCALL")
921 (arity args 1 7)
922 (val-item (basic "INT")
923 `(syscall ,@(loop for a in args
924 collect (let* ((a (need-value a)) (ty (item-type a)))
925 (unless (or (member (ty-kind ty) '(:int :untyped-int :ptr)))
926 (fail-tok tk "SYSCALL takes integers and pointers"))
927 (value-form a))))
928 tk))
929 ((string= name "ARGC") (arity args 0) (val-item (basic "INT") '(argc) tk))
930 ((string= name "ARGV") (arity args 0)
931 (val-item (make-ptr-type (make-ptr-type (basic "U8") nil) nil) '(argv) tk))
932 (t (fail-tok tk "~a isn't a built-in procedure" name)))))))))
933
934(defun writable-var (x tk what)
935 (unless (eq (item-mode x) :var) (fail-tok tk "~a takes a variable" what))
936 (check-writable x tk (format nil "changed by ~a" what))
937 x)
938
939(defun inc-form (name args tk)
940 (let* ((v (writable-var (first args) tk name)) (ty (item-type v)) (a (var-sym "A")))
941 (unless (is ty :int) (fail-tok tk "~a takes an integer variable" name))
942 (let ((k (if (second args) (exact-form (second args) ty tk "the step") 1)))
943 (make-item :mode :none :tok tk
944 :form `(let ((,a ,(item-form v)))
945 (st ,(access-kind ty) ,a
946 ,(wrap-form ty `(,(if (string= name "INC") '+ '-) (ld ,(access-kind ty) ,a) ,k))))))))
947
948(defun new-form (p a tk)
949 ;; a^.alloc(a, SIZE(T), alignment), zeroed if not nil
950 (let* ((p (writable-var p tk "NEW")) (ty (item-type p)))
951 (unless (is ty :ptr) (fail-tok tk "NEW takes a pointer variable"))
952 (let* ((alloc-ty (allocator-type tk))
953 (af (coerce-item a (make-ptr-type alloc-ty nil)))
954 (base (ptr-base ty)))
955 (make-item :mode :none :tok tk
956 :form `(let* ((pa ,(item-form p)) (al ,af)
957 (r (funcall (proc-fn (ld :u64 al)) al ,(ty-size base) ,(ty-align base))))
958 (unless (= r 0) (zero r ,(ty-size base)))
959 (st :u64 pa r))))))
960
961(defun allocator-type (tk)
962 (let* ((m (gethash "Mem" *modules*))
963 (o (and m (gethash "Allocator" (module-exports m)))))
964 (unless o (fail-tok tk "NEW needs the library's Mem.Allocator, which no module imports"))
965 (obj-type o)))
966
967(defun copy-form (d s tk)
968 ;; the elements of s to the start of d, as one block (6.8)
969 (let ((d (need-value d)) (s (need-value s)))
970 (dolist (x (list d s))
971 (unless (member (ty-kind (item-type x)) '(:array :slice :string))
972 (fail-tok tk "COPY takes arrays and slices")))
973 (when (and (is (item-type d) :array) (not (eq (item-mode d) :var))) (fail-tok tk "COPY's first is a variable"))
974 (when (is (item-type d) :array) (check-writable d tk "changed by COPY"))
975 (when (is (item-type d) :string) (fail-tok tk "COPY can't write a string"))
976 (multiple-value-bind (pd ld ed sd fd) (sequence-parts d)
977 (multiple-value-bind (ps ls es ss fs) (sequence-parts s)
978 (unless (same-type-p ed es) (fail-tok tk "COPY's two sides' elements are of two types"))
979 (make-item :mode :none :tok tk
980 :form `(let* (,@(when sd `((,sd ,fd))) ,@(when ss `((,ss ,fs))) (ld ,ld) (ls ,ls))
981 ,(unless (module-check-off *module*) `(check-copy ls ld ,(where tk)))
982 (move ,pd ,ps (* ls ,(ty-size ed)))))))))
983
984(defun atomic (name args tk)
985 (let* ((n (if (string= name "CAS") 3 2)))
986 (unless (= (length args) n) (fail-tok tk "~a takes ~d arguments" name n))
987 (let* ((p (need-value (first args))) (pty (item-type p)))
988 (unless (is pty :ptr) (fail-tok tk "~a takes a pointer" name))
989 (let* ((ty (ptr-base pty)) (k (ty-kind ty)))
990 (unless (or (member (ty-name ty) '("INT" "UINT") :test #'equal)
991 (and (eq k :ptr) (not (string= name "XADD"))))
992 (fail-tok tk "~a works on ~:[INT, UINT and pointers~;INT and UINT~]" name (string= name "XADD")))
993 (let ((signed (and (is ty :int) (ty-signed ty)))
994 (rest (mapcar (lambda (a) (coerce-item a ty)) (rest args))))
995 (if (string= name "CAS")
996 (val-item (basic "BOOL") `(cas ,(value-form p) ,@rest ,signed) tk)
997 (val-item ty `(,(if (string= name "XCHG") 'xchg 'xadd) ,(value-form p) ,@rest ,signed) tk)))))))