/[cmucl]/src/compiler/tn.lisp
ViewVC logotype

Contents of /src/compiler/tn.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Sun Apr 22 02:51:18 1990 UTC (24 years ago) by wlott
Branch: MAIN
Changes since 1.2: +3 -1 lines
Changed delete-unreferences-tns to only nuke the tn is it's :normal.
Therefore, :component tns will not be removed.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the Spice Lisp project at
5 ;;; Carnegie-Mellon University, and has been placed in the public domain.
6 ;;; If you want to use this code or any part of Spice Lisp, please contact
7 ;;; Scott Fahlman (FAHLMAN@CMUC).
8 ;;; **********************************************************************
9 ;;;
10 ;;; This file contains utilities used for creating and manipulating TNs, and
11 ;;; some other more assorted IR2 utilities.
12 ;;;
13 ;;; Written by Rob MacLachlan
14 ;;;
15 (in-package 'c)
16
17 ;;; The component that is currently being compiled. TNs are allocated in this
18 ;;; component.
19 ;;;
20 (defvar *compile-component*)
21
22
23 ;;; Do-Packed-TNs -- Interface
24 ;;;
25 (defmacro do-packed-tns ((tn component &optional result) &body body)
26 "Do-Packed-TNs (TN-Var Component [Result]) Declaration* Form*
27 Iterate over all packed TNs allocated in Component."
28 (let ((n-component (gensym)))
29 `(let ((,n-component (component-info ,component)))
30 (do ((,tn (ir2-component-normal-tns ,n-component) (tn-next ,tn)))
31 ((null ,tn))
32 ,@body)
33 (do ((,tn (ir2-component-restricted-tns ,n-component) (tn-next ,tn)))
34 ((null ,tn))
35 ,@body)
36 (do ((,tn (ir2-component-wired-tns ,n-component) (tn-next ,tn)))
37 ((null ,tn)
38 ,result)
39 ,@body))))
40
41
42 ;;; Delete-Unreferenced-TNs -- Interface
43 ;;;
44 ;;; Remove all TNs with no references from the lists of unpacked TNs. We
45 ;;; null out the Offset so that nobody will mistake deleted wired TNs for
46 ;;; properly packed TNs.
47 ;;;
48 (defun delete-unreferenced-tns (component)
49 (macrolet ((frob (name)
50 `(let ((prev nil))
51 (do ((tn ,name (tn-next tn)))
52 ((null tn))
53 (cond ((or (not (eq (tn-kind tn) :normal))
54 (tn-reads tn)
55 (tn-writes tn))
56 (setq prev tn))
57 (t
58 (if prev
59 (setf (tn-next prev) (tn-next tn))
60 (setf ,name (tn-next tn)))
61 (setf (tn-offset tn) nil)))))))
62 (let ((2comp (component-info component)))
63 (frob (ir2-component-normal-tns 2comp))
64 (frob (ir2-component-restricted-tns 2comp))
65 (frob (ir2-component-wired-tns 2comp))))
66 (undefined-value))
67
68
69
70 ;;;; TN Creation:
71
72 ;;; Make-Normal-TN -- Interface
73 ;;;
74 ;;; Create a packed TN of the specified primitive-type in the
75 ;;; *Compile-Component*. We use the SCs from the primitive type to determine
76 ;;; which SCs it can be packed in.
77 ;;;
78 (defun make-normal-tn (type)
79 (declare (type primitive-type type))
80 (let* ((component (component-info *compile-component*))
81 (res (make-tn (incf (ir2-component-global-tn-counter component))
82 :normal type nil)))
83 (push-in tn-next res (ir2-component-normal-tns component))
84 res))
85
86
87 ;;; Make-Restricted-TN -- Interface
88 ;;;
89 ;;; Create a packed TN restricted to the SC with number SCN.
90 ;;;
91 (defun make-restricted-tn (scn)
92 (declare (type sc-number scn))
93 (let* ((component (component-info *compile-component*))
94 (res (make-tn (incf (ir2-component-global-tn-counter component))
95 :normal nil (svref *sc-numbers* scn))))
96 (push-in tn-next res (ir2-component-restricted-tns component))
97 res))
98
99
100 ;;; MAKE-REPRESENTATION-TN -- Interface
101 ;;;
102 ;;; Create a normal packed TN with representation indicated by SCN.
103 ;;;
104 (defun make-representation-tn (scn)
105 (declare (type sc-number scn))
106 (let* ((component (component-info *compile-component*))
107 (res (make-tn (incf (ir2-component-global-tn-counter component))
108 :normal nil (svref *sc-numbers* scn))))
109 (push-in tn-next res (ir2-component-normal-tns component))
110 res))
111
112
113 ;;; Make-Wired-TN -- Interface
114 ;;;
115 ;;; Create a TN wired to a particular location in an SC. We set the Offset
116 ;;; and FSC to record where it goes, and then put it on the current component's
117 ;;; Wired-TNs list.
118 ;;;
119 (defun make-wired-tn (scn offset)
120 (declare (type sc-number scn) (type unsigned-byte offset))
121 (let* ((component (component-info *compile-component*))
122 (res (make-tn (incf (ir2-component-global-tn-counter component))
123 :normal nil (svref *sc-numbers* scn))))
124 (setf (tn-offset res) offset)
125 (push-in tn-next res (ir2-component-wired-tns component))
126 res))
127
128
129 ;;; Environment-Live-TN -- Interface
130 ;;;
131 ;;; Make TN be live throughout environment. TN must be referenced only in
132 ;;; Env. Return TN.
133 ;;;
134 (defun environment-live-tn (tn env)
135 (declare (type tn tn) (type environment env))
136 (assert (eq (tn-kind tn) :normal))
137 (setf (tn-kind tn) :environment)
138 (push tn (ir2-environment-live-tns (environment-info env)))
139 tn)
140
141
142 ;;; Component-Live-TN -- Interface
143 ;;;
144 ;;; Make TN be live throughout the current component. Return TN.
145 ;;;
146 (defun component-live-tn (tn)
147 (declare (type tn tn))
148 (assert (eq (tn-kind tn) :normal))
149 (setf (tn-kind tn) :component)
150 (push tn (ir2-component-component-tns (component-info *compile-component*)))
151 tn)
152
153
154 ;;; Make-Constant-TN -- Interface
155 ;;;
156 ;;; Create a constant TN. The implementation dependent
157 ;;; Immediate-Constant-SC function is used to determine whether the constant
158 ;;; has an immediate representation.
159 ;;;
160 (defun make-constant-tn (constant)
161 (declare (type constant constant))
162 (let* ((component (component-info *compile-component*))
163 (immed (immediate-constant-sc (constant-value constant)))
164 (sc (svref *sc-numbers* (or immed (sc-number-or-lose 'constant))))
165 (res (make-tn 0 :constant (primitive-type (leaf-type constant)) sc)))
166 (unless immed
167 (let ((constants (ir2-component-constants component)))
168 (setf (tn-offset res) (fill-pointer constants))
169 (vector-push-extend constant constants)))
170 (push-in tn-next res (ir2-component-constant-tns component))
171 (setf (tn-leaf res) constant)
172 res))
173
174
175 ;;; Make-Load-Time-Constant-TN -- Internal
176 ;;;
177 ;;; Return a load-time constant TN with the specified Kind and Info. If the
178 ;;; desired Constants entry already exists, then reuse it, otherwise allocate a
179 ;;; new load-time constant slot.
180 ;;;
181 (defun make-load-time-constant-tn (kind info)
182 (declare (type keyword kind))
183 (let* ((component (component-info *compile-component*))
184 (res (make-tn 0 :constant *any-primitive-type*
185 (svref *sc-numbers* (sc-number-or-lose 'constant))))
186 (constants (ir2-component-constants component)))
187
188 (do ((i 0 (1+ i)))
189 ((= i (length constants))
190 (setf (tn-offset res) i)
191 (vector-push-extend (cons kind info) constants))
192 (let ((entry (aref constants i)))
193 (when (and (consp entry)
194 (eq (car entry) kind)
195 (eq (cdr entry) info))
196 (setf (tn-offset res) i))))
197
198 (push-in tn-next res (ir2-component-constant-tns component))
199 res))
200
201
202 ;;;; TN referencing:
203
204 ;;; Reference-TN -- Interface
205 ;;;
206 ;;; Make a TN-Ref that references TN and return it. Write-P should be true
207 ;;; if this is a write reference, otherwise false. All we do other than
208 ;;; calling the constructor is add the reference to the TN's references.
209 ;;;
210 (proclaim '(inline reference-tn))
211 (defun reference-tn (tn write-p)
212 (declare (type tn tn) (type boolean write-p))
213 (let ((res (make-tn-ref tn write-p)))
214 (if write-p
215 (push-in tn-ref-next res (tn-writes tn))
216 (push-in tn-ref-next res (tn-reads tn)))
217 res))
218
219
220 ;;; Reference-TN-List -- Interface
221 ;;;
222 ;;; Make TN-Refs to reference each TN in TNs, linked together by
223 ;;; TN-Ref-Across. Write-P is the Write-P value for the refs. More is
224 ;;; stuck in the TN-Ref-Across of the ref for the last TN, or returned as the
225 ;;; result if there are no TNs.
226 ;;;
227 (defun reference-tn-list (tns write-p &optional more)
228 (declare (list tns) (type boolean write-p) (type (or tn-ref null) more))
229 (if tns
230 (let* ((first (reference-tn (first tns) write-p))
231 (prev first))
232 (dolist (tn (rest tns))
233 (let ((res (reference-tn tn write-p)))
234 (setf (tn-ref-across prev) res)
235 (setq prev res)))
236 (setf (tn-ref-across prev) more)
237 first)
238 more))
239
240
241 ;;; Delete-TN-Ref -- Interface
242 ;;;
243 ;;; Remove Ref from the references for its associated TN.
244 ;;;
245 (defun delete-tn-ref (ref)
246 (declare (type tn-ref ref))
247 (if (tn-ref-write-p ref)
248 (deletef-in tn-ref-next (tn-writes (tn-ref-tn ref)) ref)
249 (deletef-in tn-ref-next (tn-reads (tn-ref-tn ref)) ref))
250 (undefined-value))
251
252
253 ;;; Change-TN-Ref-TN -- Interface
254 ;;;
255 ;;; Do stuff to change the TN referenced by Ref. We remove Ref from it's
256 ;;; old TN's refs, add ref to TN's refs, and set the TN-Ref-TN.
257 ;;;
258 (defun change-tn-ref-tn (ref tn)
259 (declare (type tn-ref ref) (type tn tn))
260 (delete-tn-ref ref)
261 (setf (tn-ref-tn ref) tn)
262 (if (tn-ref-write-p ref)
263 (push-in tn-ref-next ref (tn-writes tn))
264 (push-in tn-ref-next ref (tn-reads tn)))
265 (undefined-value))
266
267
268 ;;;; Random utilities:
269
270
271 ;;; Emit-Move-Template -- Internal
272 ;;;
273 ;;; Emit a move-like template determined at run-time, with X as the argument
274 ;;; and Y as the result. Useful for move, coerce and type-check templates. If
275 ;;; supplied, then insert before VOP, otherwise insert at then end of the
276 ;;; block. Returns the last VOP inserted.
277 ;;;
278 (defun emit-move-template (node block template x y &optional before)
279 (declare (type node node) (type ir2-block block)
280 (type template template) (type tn x y))
281 (let ((arg (reference-tn x nil))
282 (result (reference-tn y t)))
283 (multiple-value-bind
284 (first last)
285 (funcall (template-emit-function template) node block template arg
286 result)
287 (insert-vop-sequence first last block before)
288 last)))
289
290
291 ;;; EMIT-LOAD-TEMPLATE -- Internal
292 ;;;
293 ;;; Like EMIT-MOVE-TEMPLATE, except that we pass in Info args too.
294 ;;;
295 (defun emit-load-template (node block template x y info &optional before)
296 (declare (type node node) (type ir2-block block)
297 (type template template) (type tn x y))
298 (let ((arg (reference-tn x nil))
299 (result (reference-tn y t)))
300 (multiple-value-bind
301 (first last)
302 (funcall (template-emit-function template) node block template arg
303 result info)
304 (insert-vop-sequence first last block before)
305 last)))
306
307
308 ;;; EMIT-MOVE-ARG-TEMPLATE -- Internal
309 ;;;
310 ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes two args.
311 ;;;
312 (defun emit-move-arg-template (node block template x f y &optional before)
313 (declare (type node node) (type ir2-block block)
314 (type template template) (type tn x f y))
315 (let ((x-ref (reference-tn x nil))
316 (f-ref (reference-tn f nil))
317 (y-ref (reference-tn y t)))
318 (setf (tn-ref-across x-ref) f-ref)
319 (multiple-value-bind
320 (first last)
321 (funcall (template-emit-function template) node block template x-ref
322 y-ref)
323 (insert-vop-sequence first last block before)
324 last)))
325
326
327 ;;; EMIT-CONTEXT-TEMPLATE -- Internal
328 ;;;
329 ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes no args.
330 ;;;
331 (defun emit-context-template (node block template y &optional before)
332 (declare (type node node) (type ir2-block block)
333 (type template template) (type tn y))
334 (let ((y-ref (reference-tn y t)))
335 (multiple-value-bind
336 (first last)
337 (funcall (template-emit-function template) node block template nil
338 y-ref)
339 (insert-vop-sequence first last block before)
340 last)))
341
342
343 ;;; Block-Label -- Interface
344 ;;;
345 ;;; Return the label marking the start of Block, assigning one if necessary.
346 ;;;
347 (defun block-label (block)
348 (declare (type cblock block))
349 (let ((2block (block-info block)))
350 (or (ir2-block-%label 2block)
351 (setf (ir2-block-%label 2block) (gen-label)))))
352
353
354 ;;; Drop-Thru-P -- Interface
355 ;;;
356 ;;; Return true if Block is emitted immediately after the block ended by
357 ;;; Node.
358 ;;;
359 (defun drop-thru-p (node block)
360 (declare (type node node) (type cblock block))
361 (let ((next-block (ir2-block-next (block-info (node-block node)))))
362 (assert (eq node (block-last (node-block node))))
363 (eq next-block (block-info block))))
364
365
366 ;;; Insert-VOP-Sequence -- Interface
367 ;;;
368 ;;; Link a list of VOPs from First to Last into Block, Before the specified
369 ;;; VOP. If Before is NIL, insert at the end.
370 ;;;
371 (defun insert-vop-sequence (first last block before)
372 (declare (type vop first last) (type ir2-block block)
373 (type (or vop null) before))
374 (if before
375 (let ((prev (vop-prev before)))
376 (setf (vop-prev first) prev)
377 (if prev
378 (setf (vop-next prev) first)
379 (setf (ir2-block-start-vop block) first))
380 (setf (vop-next last) before)
381 (setf (vop-prev before) last))
382 (let ((current (ir2-block-last-vop block)))
383 (setf (vop-prev first) current)
384 (setf (ir2-block-last-vop block) last)
385 (if current
386 (setf (vop-next current) first)
387 (setf (ir2-block-start-vop block) first))))
388 (undefined-value))
389
390
391 ;;; DELETE-VOP -- Interface
392 ;;;
393 ;;; Delete all of the TN-Refs associated with VOP and remove VOP from the
394 ;;; IR2.
395 ;;;
396 (defun delete-vop (vop)
397 (declare (type vop vop))
398 (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
399 ((null ref))
400 (delete-tn-ref ref))
401
402 (let ((prev (vop-prev vop))
403 (next (vop-next vop))
404 (block (vop-block vop)))
405 (if prev
406 (setf (vop-next prev) next)
407 (setf (ir2-block-start-vop block) next))
408 (if next
409 (setf (vop-prev next) prev)
410 (setf (ir2-block-last-vop block) prev)))
411
412 (undefined-value))
413
414
415 ;;; Make-N-TNs -- Interface
416 ;;;
417 ;;; Return a list of N normal TNs of the specified primitive type.
418 ;;;
419 (defun make-n-tns (n ptype)
420 (declare (type unsigned-byte n) (type primitive-type ptype))
421 (collect ((res))
422 (dotimes (i n)
423 (res (make-normal-tn ptype)))
424 (res)))
425
426
427 ;;; Location= -- Interface
428 ;;;
429 ;;; Return true if X and Y are packed in the same location, false otherwise.
430 ;;; This is false if either operand is constant.
431 ;;;
432 (defun location= (x y)
433 (declare (type tn x y))
434 (and (eq (sc-sb (tn-sc x)) (sc-sb (tn-sc y)))
435 (eql (tn-offset x) (tn-offset y))
436 (not (or (eq (tn-kind x) :constant)
437 (eq (tn-kind y) :constant)))))
438
439
440 ;;; TN-Value -- Interface
441 ;;;
442 ;;; Return the value of an immediate constant TN.
443 ;;;
444 (defun tn-value (tn)
445 (declare (type tn tn))
446 (assert (member (tn-kind tn) '(:constant :cached-constant)))
447 (assert (/= (sc-number (tn-sc tn)) (sc-number-or-lose 'constant)))
448 (constant-value (tn-leaf tn)))
449
450
451 ;;; Force-TN-To-Stack -- Interface
452 ;;;
453 ;;; Force TN to be allocated in a SC that doesn't need to be saved: an
454 ;;; unbounded non-save-p SC. We don't actually make it a real "restricted" TN,
455 ;;; but since we change the SC to an unbounded one, we should always succeed in
456 ;;; packing it in that SC.
457 ;;;
458 (defun force-tn-to-stack (tn)
459 (declare (type tn tn))
460 (let ((sc (tn-sc tn)))
461 (unless (and (not (sc-save-p sc))
462 (eq (sb-kind (sc-sb sc)) :unbounded))
463 (dolist (alt (sc-alternate-scs sc)
464 (error "SC ~S has no :unbounded :save-p NIL alternate SC."
465 (sc-name sc)))
466 (when (and (not (sc-save-p alt))
467 (eq (sb-kind (sc-sb alt)) :unbounded))
468 (setf (tn-sc tn) alt)
469 (return)))))
470 (undefined-value))
471
472
473 ;;; TN-Environment -- Interface
474 ;;;
475 ;;; Return some Environment that TN is referenced in. TN must have at least
476 ;;; one reference (either read or write.) Note that some TNs are referenced in
477 ;;; multiple environments.
478 ;;;
479 (defun tn-environment (tn)
480 (declare (type tn tn))
481 (let ((ref (or (tn-reads tn) (tn-writes tn))))
482 (assert ref)
483 (lambda-environment
484 (block-lambda
485 (ir2-block-block (vop-block (tn-ref-vop ref)))))))

  ViewVC Help
Powered by ViewVC 1.1.5