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

Contents of /src/compiler/tn.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations)
Tue May 1 15:23:52 1990 UTC (23 years, 11 months ago) by ram
Branch: MAIN
Changes since 1.3: +0 -1 lines
Deleted INLINE proclamation for REFERENCE-TN.  Now that MAKE-TN-REF is inline,
the code bloat was getting out of hand.
1 wlott 1.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 wlott 1.3 (cond ((or (not (eq (tn-kind tn) :normal))
54     (tn-reads tn)
55     (tn-writes tn))
56 wlott 1.1 (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 ram 1.2 :normal type nil)))
83 wlott 1.1 (push-in tn-next res (ir2-component-normal-tns component))
84     res))
85    
86    
87 ram 1.2 ;;; Make-Restricted-TN -- Interface
88 wlott 1.1 ;;;
89 ram 1.2 ;;; Create a packed TN restricted to the SC with number SCN.
90 wlott 1.1 ;;;
91 ram 1.2 (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 wlott 1.1 res))
98    
99    
100 ram 1.2 ;;; MAKE-REPRESENTATION-TN -- Interface
101 wlott 1.1 ;;;
102 ram 1.2 ;;; Create a normal packed TN with representation indicated by SCN.
103 wlott 1.1 ;;;
104 ram 1.2 (defun make-representation-tn (scn)
105     (declare (type sc-number scn))
106 wlott 1.1 (let* ((component (component-info *compile-component*))
107     (res (make-tn (incf (ir2-component-global-tn-counter component))
108 ram 1.2 :normal nil (svref *sc-numbers* scn))))
109     (push-in tn-next res (ir2-component-normal-tns component))
110 wlott 1.1 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 ram 1.2 ;;; Wired-TNs list.
118 wlott 1.1 ;;;
119 ram 1.2 (defun make-wired-tn (scn offset)
120     (declare (type sc-number scn) (type unsigned-byte offset))
121 wlott 1.1 (let* ((component (component-info *compile-component*))
122     (res (make-tn (incf (ir2-component-global-tn-counter component))
123 ram 1.2 :normal nil (svref *sc-numbers* scn))))
124 wlott 1.1 (setf (tn-offset res) offset)
125     (push-in tn-next res (ir2-component-wired-tns component))
126     res))
127    
128    
129 ram 1.2 ;;; Environment-Live-TN -- Interface
130 wlott 1.1 ;;;
131 ram 1.2 ;;; Make TN be live throughout environment. TN must be referenced only in
132     ;;; Env. Return TN.
133 wlott 1.1 ;;;
134 ram 1.2 (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 wlott 1.1
141    
142 ram 1.2 ;;; 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 wlott 1.1 ;;; 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     (defun reference-tn (tn write-p)
211     (declare (type tn tn) (type boolean write-p))
212     (let ((res (make-tn-ref tn write-p)))
213     (if write-p
214     (push-in tn-ref-next res (tn-writes tn))
215     (push-in tn-ref-next res (tn-reads tn)))
216     res))
217    
218    
219     ;;; Reference-TN-List -- Interface
220     ;;;
221     ;;; Make TN-Refs to reference each TN in TNs, linked together by
222     ;;; TN-Ref-Across. Write-P is the Write-P value for the refs. More is
223     ;;; stuck in the TN-Ref-Across of the ref for the last TN, or returned as the
224     ;;; result if there are no TNs.
225     ;;;
226     (defun reference-tn-list (tns write-p &optional more)
227     (declare (list tns) (type boolean write-p) (type (or tn-ref null) more))
228     (if tns
229     (let* ((first (reference-tn (first tns) write-p))
230     (prev first))
231     (dolist (tn (rest tns))
232     (let ((res (reference-tn tn write-p)))
233     (setf (tn-ref-across prev) res)
234     (setq prev res)))
235     (setf (tn-ref-across prev) more)
236     first)
237     more))
238    
239    
240     ;;; Delete-TN-Ref -- Interface
241     ;;;
242     ;;; Remove Ref from the references for its associated TN.
243     ;;;
244     (defun delete-tn-ref (ref)
245     (declare (type tn-ref ref))
246     (if (tn-ref-write-p ref)
247     (deletef-in tn-ref-next (tn-writes (tn-ref-tn ref)) ref)
248     (deletef-in tn-ref-next (tn-reads (tn-ref-tn ref)) ref))
249     (undefined-value))
250    
251    
252     ;;; Change-TN-Ref-TN -- Interface
253     ;;;
254     ;;; Do stuff to change the TN referenced by Ref. We remove Ref from it's
255     ;;; old TN's refs, add ref to TN's refs, and set the TN-Ref-TN.
256     ;;;
257     (defun change-tn-ref-tn (ref tn)
258     (declare (type tn-ref ref) (type tn tn))
259     (delete-tn-ref ref)
260     (setf (tn-ref-tn ref) tn)
261     (if (tn-ref-write-p ref)
262     (push-in tn-ref-next ref (tn-writes tn))
263     (push-in tn-ref-next ref (tn-reads tn)))
264     (undefined-value))
265    
266    
267     ;;;; Random utilities:
268    
269 ram 1.2
270     ;;; Emit-Move-Template -- Internal
271     ;;;
272     ;;; Emit a move-like template determined at run-time, with X as the argument
273     ;;; and Y as the result. Useful for move, coerce and type-check templates. If
274     ;;; supplied, then insert before VOP, otherwise insert at then end of the
275     ;;; block. Returns the last VOP inserted.
276     ;;;
277     (defun emit-move-template (node block template x y &optional before)
278     (declare (type node node) (type ir2-block block)
279     (type template template) (type tn x y))
280     (let ((arg (reference-tn x nil))
281     (result (reference-tn y t)))
282     (multiple-value-bind
283     (first last)
284     (funcall (template-emit-function template) node block template arg
285     result)
286     (insert-vop-sequence first last block before)
287     last)))
288    
289    
290     ;;; EMIT-LOAD-TEMPLATE -- Internal
291     ;;;
292     ;;; Like EMIT-MOVE-TEMPLATE, except that we pass in Info args too.
293     ;;;
294     (defun emit-load-template (node block template x y info &optional before)
295     (declare (type node node) (type ir2-block block)
296     (type template template) (type tn x y))
297     (let ((arg (reference-tn x nil))
298     (result (reference-tn y t)))
299     (multiple-value-bind
300     (first last)
301     (funcall (template-emit-function template) node block template arg
302     result info)
303     (insert-vop-sequence first last block before)
304     last)))
305    
306    
307     ;;; EMIT-MOVE-ARG-TEMPLATE -- Internal
308     ;;;
309     ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes two args.
310     ;;;
311     (defun emit-move-arg-template (node block template x f y &optional before)
312     (declare (type node node) (type ir2-block block)
313     (type template template) (type tn x f y))
314     (let ((x-ref (reference-tn x nil))
315     (f-ref (reference-tn f nil))
316     (y-ref (reference-tn y t)))
317     (setf (tn-ref-across x-ref) f-ref)
318     (multiple-value-bind
319     (first last)
320     (funcall (template-emit-function template) node block template x-ref
321     y-ref)
322     (insert-vop-sequence first last block before)
323     last)))
324    
325    
326     ;;; EMIT-CONTEXT-TEMPLATE -- Internal
327     ;;;
328     ;;; Like EMIT-MOVE-TEMPLATE, except that the VOP takes no args.
329     ;;;
330     (defun emit-context-template (node block template y &optional before)
331     (declare (type node node) (type ir2-block block)
332     (type template template) (type tn y))
333     (let ((y-ref (reference-tn y t)))
334     (multiple-value-bind
335     (first last)
336     (funcall (template-emit-function template) node block template nil
337     y-ref)
338     (insert-vop-sequence first last block before)
339     last)))
340    
341    
342     ;;; Block-Label -- Interface
343     ;;;
344     ;;; Return the label marking the start of Block, assigning one if necessary.
345     ;;;
346     (defun block-label (block)
347     (declare (type cblock block))
348     (let ((2block (block-info block)))
349     (or (ir2-block-%label 2block)
350     (setf (ir2-block-%label 2block) (gen-label)))))
351    
352    
353     ;;; Drop-Thru-P -- Interface
354     ;;;
355     ;;; Return true if Block is emitted immediately after the block ended by
356     ;;; Node.
357     ;;;
358     (defun drop-thru-p (node block)
359     (declare (type node node) (type cblock block))
360     (let ((next-block (ir2-block-next (block-info (node-block node)))))
361     (assert (eq node (block-last (node-block node))))
362     (eq next-block (block-info block))))
363    
364    
365     ;;; Insert-VOP-Sequence -- Interface
366     ;;;
367     ;;; Link a list of VOPs from First to Last into Block, Before the specified
368     ;;; VOP. If Before is NIL, insert at the end.
369     ;;;
370     (defun insert-vop-sequence (first last block before)
371     (declare (type vop first last) (type ir2-block block)
372     (type (or vop null) before))
373     (if before
374     (let ((prev (vop-prev before)))
375     (setf (vop-prev first) prev)
376     (if prev
377     (setf (vop-next prev) first)
378     (setf (ir2-block-start-vop block) first))
379     (setf (vop-next last) before)
380     (setf (vop-prev before) last))
381     (let ((current (ir2-block-last-vop block)))
382     (setf (vop-prev first) current)
383     (setf (ir2-block-last-vop block) last)
384     (if current
385     (setf (vop-next current) first)
386     (setf (ir2-block-start-vop block) first))))
387     (undefined-value))
388    
389    
390     ;;; DELETE-VOP -- Interface
391     ;;;
392     ;;; Delete all of the TN-Refs associated with VOP and remove VOP from the
393     ;;; IR2.
394     ;;;
395     (defun delete-vop (vop)
396     (declare (type vop vop))
397     (do ((ref (vop-refs vop) (tn-ref-next-ref ref)))
398     ((null ref))
399     (delete-tn-ref ref))
400    
401     (let ((prev (vop-prev vop))
402     (next (vop-next vop))
403     (block (vop-block vop)))
404     (if prev
405     (setf (vop-next prev) next)
406     (setf (ir2-block-start-vop block) next))
407     (if next
408     (setf (vop-prev next) prev)
409     (setf (ir2-block-last-vop block) prev)))
410    
411     (undefined-value))
412    
413    
414 wlott 1.1 ;;; Make-N-TNs -- Interface
415     ;;;
416     ;;; Return a list of N normal TNs of the specified primitive type.
417     ;;;
418     (defun make-n-tns (n ptype)
419     (declare (type unsigned-byte n) (type primitive-type ptype))
420     (collect ((res))
421     (dotimes (i n)
422     (res (make-normal-tn ptype)))
423     (res)))
424    
425    
426     ;;; Location= -- Interface
427     ;;;
428     ;;; Return true if X and Y are packed in the same location, false otherwise.
429     ;;; This is false if either operand is constant.
430     ;;;
431     (defun location= (x y)
432     (declare (type tn x y))
433     (and (eq (sc-sb (tn-sc x)) (sc-sb (tn-sc y)))
434     (eql (tn-offset x) (tn-offset y))
435     (not (or (eq (tn-kind x) :constant)
436     (eq (tn-kind y) :constant)))))
437    
438    
439     ;;; TN-Value -- Interface
440     ;;;
441     ;;; Return the value of an immediate constant TN.
442     ;;;
443     (defun tn-value (tn)
444     (declare (type tn tn))
445     (assert (member (tn-kind tn) '(:constant :cached-constant)))
446     (assert (/= (sc-number (tn-sc tn)) (sc-number-or-lose 'constant)))
447     (constant-value (tn-leaf tn)))
448    
449    
450     ;;; Force-TN-To-Stack -- Interface
451     ;;;
452 ram 1.2 ;;; Force TN to be allocated in a SC that doesn't need to be saved: an
453     ;;; unbounded non-save-p SC. We don't actually make it a real "restricted" TN,
454     ;;; but since we change the SC to an unbounded one, we should always succeed in
455     ;;; packing it in that SC.
456 wlott 1.1 ;;;
457     (defun force-tn-to-stack (tn)
458     (declare (type tn tn))
459 ram 1.2 (let ((sc (tn-sc tn)))
460     (unless (and (not (sc-save-p sc))
461     (eq (sb-kind (sc-sb sc)) :unbounded))
462     (dolist (alt (sc-alternate-scs sc)
463     (error "SC ~S has no :unbounded :save-p NIL alternate SC."
464     (sc-name sc)))
465     (when (and (not (sc-save-p alt))
466     (eq (sb-kind (sc-sb alt)) :unbounded))
467     (setf (tn-sc tn) alt)
468     (return)))))
469 wlott 1.1 (undefined-value))
470    
471    
472     ;;; TN-Environment -- Interface
473     ;;;
474 ram 1.2 ;;; Return some Environment that TN is referenced in. TN must have at least
475     ;;; one reference (either read or write.) Note that some TNs are referenced in
476     ;;; multiple environments.
477 wlott 1.1 ;;;
478     (defun tn-environment (tn)
479     (declare (type tn tn))
480     (let ((ref (or (tn-reads tn) (tn-writes tn))))
481     (assert ref)
482 ram 1.2 (lambda-environment
483     (block-lambda
484     (ir2-block-block (vop-block (tn-ref-vop ref)))))))

  ViewVC Help
Powered by ViewVC 1.1.5