raz / sbcl / src / types.lisp

214 lines · 8.7 KiB · raw · history · last change cfa2bc20

1;;;; types.lisp -- raz's types: what they are, their layout, and when two
2;;;; are the same or one is assignable to another.
3;;;;
4;;;; Report, section 5. A type is a TY. Every type the program writes out
5;;;; is a fresh TY; a declaration `type P = ^Node` makes one with a name,
6;;;; and `type Count = I64` makes no type at all, only another name for
7;;;; I64's. Sameness (5.10) is then a matter of NAME and of what the type
8;;;; is built of.
9
10(in-package :raz)
11
12;;; KIND is one of
13;;; :int :bool :real :error the basic types (BITS and SIGNED for :int, BITS for :real)
14;;; :ptr BASE, the type pointed at, and VOL for ^VOLATILE
15;;; :array LEN elements of ELEM; :slice elements of ELEM
16;;; :record FIELDS, PACKED; :variant CASES
17;;; :enum NAMES, strings in order; :set ENUM, an :enum
18;;; :proc SIG, a SIG
19;;; and, for the values that have no type of their own yet,
20;;; :untyped-int, :untyped-real, :string (a string literal), :nil.
21;;; MODULE is the module that declared the type, which decides who may
22;;; see its unexported fields.
23
24(defstruct (ty (:print-function (lambda (ty s d) (declare (ignore d))
25 (write-string (type-name ty) s))))
26 kind name (size 0) (align 1)
27 bits signed base vol len elem fields packed cases names enum sig module)
28
29;;; A field of a record, or of a case of a variant: its OFFSET from the
30;;; record's start (from the variant's, for a case).
31(defstruct field name type offset exported)
32
33;;; A case of a variant: its INDEX, which is its tag, and its FIELDS.
34(defstruct vcase name index fields variant)
35
36;;; A procedure's signature: PARAMS are (name type var-p) lists, RESULTS
37;;; types; VARARGS for an extern's `...`.
38(defstruct sig params results varargs)
39
40(defun word-size () 8)
41
42;;; The basic types, made once.
43
44(defun make-int-type (name bits signed)
45 (make-ty :kind :int :name name :bits bits :signed signed
46 :size (/ bits 8) :align (/ bits 8)))
47
48(defparameter *basic-types*
49 (list (make-int-type "I8" 8 t) (make-int-type "I16" 16 t)
50 (make-int-type "I32" 32 t) (make-int-type "I64" 64 t)
51 (make-int-type "U8" 8 nil) (make-int-type "U16" 16 nil)
52 (make-int-type "U32" 32 nil) (make-int-type "U64" 64 nil)
53 (make-int-type "INT" 64 t) (make-int-type "UINT" 64 nil)
54 (make-ty :kind :bool :name "BOOL" :size 1 :align 1)
55 (make-ty :kind :real :name "F32" :bits 32 :size 4 :align 4)
56 (make-ty :kind :real :name "F64" :bits 64 :size 8 :align 8)
57 (make-ty :kind :error :name "ERROR" :size 8 :align 8)))
58
59(defun basic (name) (find name *basic-types* :key #'ty-name :test #'string=))
60
61(defparameter *untyped-int* (make-ty :kind :untyped-int :name "an integer constant"))
62(defparameter *untyped-real* (make-ty :kind :untyped-real :name "a real constant"))
63(defparameter *nil-type* (make-ty :kind :nil :name "nil"))
64
65(defun string-type (len)
66 (make-ty :kind :string :name "a string" :len len))
67
68(defun type-name (ty)
69 (or (ty-name ty)
70 (case (ty-kind ty)
71 (:ptr (format nil "^~:[~;VOLATILE ~]~a" (ty-vol ty) (type-name* (ty-base ty))))
72 (:array (format nil "[~d]~a" (ty-len ty) (type-name (ty-elem ty))))
73 (:slice (format nil "[]~a" (type-name (ty-elem ty))))
74 (:record "a record") (:variant "a variant") (:enum "an enumeration")
75 (:set (format nil "set of ~a" (type-name (ty-enum ty))))
76 (:proc "a procedure type")
77 (t (string-downcase (ty-kind ty))))))
78
79(defun type-name* (x) (if (ty-p x) (type-name x) (format nil "~a" (second x))))
80
81;;; Kinds of types, as the rules ask about them.
82
83(defun int-p (ty) (eq (ty-kind ty) :int))
84(defun real-p (ty) (eq (ty-kind ty) :real))
85(defun ptr-p (ty) (eq (ty-kind ty) :ptr))
86(defun aggregate-p (ty)
87 "A type whose values the compiled code handles by address."
88 (member (ty-kind ty) '(:array :slice :record :variant :string)))
89(defun is (ty kind) (eq (ty-kind ty) kind))
90
91;;; A pointer's base may name a type declared later in its `type`
92;;; section (section 4): until then BASE is (:forward name), and the
93;;; section's end puts the type in its place.
94(defun ptr-base (ty)
95 (let ((b (ty-base ty)))
96 (when (consp b) (error "pointer base still forward"))
97 b))
98
99;;; Layout (5.5, 5.6). Each field at the next multiple of its alignment,
100;;; the whole padded to the largest; a packed record has no padding.
101
102(defun round-up (n align) (* align (ceiling n align)))
103
104(defun lay-out-fields (fields start packed)
105 "Sets each field's offset, from START; returns the end and the largest alignment."
106 (let ((off start) (align 1))
107 (dolist (f fields)
108 (let ((ft (field-type f)))
109 (unless packed (setf off (round-up off (ty-align ft))))
110 (setf (field-offset f) off)
111 (incf off (ty-size ft))
112 (setf align (max align (if packed 1 (ty-align ft))))))
113 (values off align)))
114
115(defun make-record-type (fields packed module)
116 (multiple-value-bind (end align) (lay-out-fields fields 0 packed)
117 (make-ty :kind :record :fields fields :packed packed :module module
118 :size (round-up end align) :align align)))
119
120(defun make-variant-type (cases module)
121 ;; an INT tag, then each case's fields as a record's after it
122 (let ((size 8) (align 8) (ty (make-ty :kind :variant :cases cases :module module)))
123 (dolist (c cases)
124 (setf (vcase-variant c) ty)
125 (multiple-value-bind (end a) (lay-out-fields (vcase-fields c) 8 nil)
126 (setf size (max size end) align (max align a))))
127 (setf (ty-size ty) (round-up size align) (ty-align ty) align)
128 ty))
129
130(defun make-enum-type (names module)
131 (make-ty :kind :enum :names names :size 1 :align 1 :module module))
132
133(defun make-set-type (enum)
134 (let ((n (length (ty-names enum))))
135 (let ((size (cond ((<= n 8) 1) ((<= n 16) 2) ((<= n 32) 4) (t 8))))
136 (make-ty :kind :set :enum enum :size size :align size))))
137
138(defun make-ptr-type (base vol)
139 (make-ty :kind :ptr :base base :vol vol :size 8 :align 8))
140
141(defun make-array-type (len elem)
142 (make-ty :kind :array :len len :elem elem
143 :size (* len (ty-size elem)) :align (ty-align elem)))
144
145(defun make-slice-type (elem)
146 (make-ty :kind :slice :elem elem :size 16 :align 8))
147
148(defun make-proc-type (sig)
149 (make-ty :kind :proc :sig sig :size 8 :align 8))
150
151;;; Sameness (5.10). Two named types are the same only if they are one
152;;; declaration. A record, variant or enumeration is its own type
153;;; wherever it is written. Anonymous pointers, arrays, slices, sets and
154;;; procedure types are the same if built alike, and a named type is the
155;;; same as an anonymous one it is built like.
156
157(defun same-type-p (a b)
158 (cond ((eq a b) t)
159 ((and (ty-name a) (ty-name b)) nil)
160 ((not (eq (ty-kind a) (ty-kind b))) nil)
161 (t (case (ty-kind a)
162 (:ptr (and (eq (ty-vol a) (ty-vol b)) (same-type-p (ptr-base a) (ptr-base b))))
163 (:array (and (= (ty-len a) (ty-len b)) (same-type-p (ty-elem a) (ty-elem b))))
164 (:slice (same-type-p (ty-elem a) (ty-elem b)))
165 (:set (same-type-p (ty-enum a) (ty-enum b)))
166 (:proc (sig-alike-p (ty-sig a) (ty-sig b)))
167 (t nil)))))
168
169(defun sig-alike-p (a b)
170 "Two signatures are alike: their parameters' types, each var or not
171alike, and their results' types (5.9)."
172 (and (= (length (sig-params a)) (length (sig-params b)))
173 (= (length (sig-results a)) (length (sig-results b)))
174 (eq (sig-varargs a) (sig-varargs b))
175 (every (lambda (p q) (and (eq (third p) (third q)) (same-type-p (second p) (second q))))
176 (sig-params a) (sig-params b))
177 (every #'same-type-p (sig-results a) (sig-results b))))
178
179;;; Integer ranges.
180
181(defun int-min (ty) (if (ty-signed ty) (- (expt 2 (1- (ty-bits ty)))) 0))
182(defun int-max (ty) (if (ty-signed ty) (1- (expt 2 (1- (ty-bits ty)))) (1- (expt 2 (ty-bits ty)))))
183(defun fits-p (v ty) (<= (int-min ty) v (int-max ty)))
184
185(defun wrap-int (v ty)
186 "V cut to TY's width, as the program's arithmetic wraps."
187 (if (ty-signed ty)
188 (sb-c::mask-signed-field (ty-bits ty) v)
189 (ldb (byte (ty-bits ty) 0) v)))
190
191;;; What can hold a nil, and what compares only for equality.
192(defun nil-able-p (ty) (member (ty-kind ty) '(:ptr :proc :error)))
193
194;;; Elements whose arrays compare with = and # (6.5).
195(defun bytewise-comparable-p (ty)
196 (member (ty-kind ty) '(:int :bool :enum :set :ptr)))
197
198;;; The kind of memory access a scalar type needs: a keyword the run
199;;; time's LD and ST take (rt.lisp).
200(defun access-kind (ty)
201 (ecase (ty-kind ty)
202 (:int (intern (format nil "~:[U~;I~]~d" (ty-signed ty) (ty-bits ty)) :keyword))
203 (:bool :bool)
204 (:real (if (= (ty-bits ty) 32) :f32 :f64))
205 ((:ptr :proc :error) :u64)
206 (:enum :u8)
207 (:set (intern (format nil "U~d" (* 8 (ty-size ty))) :keyword))))
208
209(defun zero-value (ty)
210 "The Lisp value a scalar of TY starts at."
211 (case (ty-kind ty)
212 (:bool nil)
213 (:real (if (= (ty-bits ty) 32) 0f0 0d0))
214 (t 0)))