Newer
Older
;;; -*- Package: SPARC -*-
;;;
;;; **********************************************************************
;;; This code was written as part of the Spice Lisp project at
;;; Carnegie-Mellon University, and has been placed in the public domain.
;;; If you want to use this code or any part of Spice Lisp, please contact
;;; Scott Fahlman (FAHLMAN@CMUC).
;;; **********************************************************************
;;;
;;; $Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/compiler/sparc/macros.lisp,v 1.7 1992/07/31 21:49:19 wlott Exp $
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
;;;
;;; This file contains various useful macros for generating SPARC code.
;;;
;;; Written by William Lott.
;;;
(in-package "SPARC")
;;; Instruction-like macros.
(defmacro move (dst src)
"Move SRC into DST unless they are location=."
(once-only ((n-dst dst)
(n-src src))
`(unless (location= ,n-dst ,n-src)
(inst move ,n-dst ,n-src))))
(macrolet
((frob (op inst shift)
`(defmacro ,op (object base &optional (offset 0) (lowtag 0))
`(inst ,',inst ,object ,base (- (ash ,offset ,,shift) ,lowtag)))))
(frob loadw ld word-shift)
(frob storew st word-shift))
(defmacro load-symbol (reg symbol)
`(inst add ,reg null-tn (static-symbol-offset ,symbol)))
(macrolet
((frob (slot)
(let ((loader (intern (concatenate 'simple-string
"LOAD-SYMBOL-"
(string slot))))
(storer (intern (concatenate 'simple-string
"STORE-SYMBOL-"
(string slot))))
(offset (intern (concatenate 'simple-string
"SYMBOL-"
(string slot)
"-SLOT")
(find-package "VM"))))
`(progn
(defmacro ,loader (reg symbol)
`(inst ld ,reg null-tn
(+ (static-symbol-offset ',symbol)
(ash ,',offset word-shift)
(- other-pointer-type))))
(defmacro ,storer (reg symbol)
`(inst st ,reg null-tn
(+ (static-symbol-offset ',symbol)
(ash ,',offset word-shift)
(- other-pointer-type))))))))
(frob value)
(frob function))
(defmacro load-type (target source &optional (offset 0))
"Loads the type bits of a pointer into target independent of
byte-ordering issues."
(once-only ((n-target target)
(n-source source)
(n-offset offset))
(ecase (backend-byte-order *target-backend*)
(:little-endian
`(inst ldub ,n-target ,n-source ,n-offset))
(:big-endian
`(inst ldub ,n-target ,n-source (+ ,n-offset 3))))))
;;; Macros to handle the fact that we cannot use the machine native call and
;;; return instructions.
(defmacro lisp-jump (function)
"Jump to the lisp function FUNCTION. LIP is an interior-reg temporary."
`(progn
(inst j ,function (- (ash function-header-code-offset
word-shift)
function-pointer-type))
(move code-tn ,function)))
(defmacro lisp-return (return-pc &key (offset 0) (frob-code t))
"Return to RETURN-PC."
`(progn
(inst j ,return-pc
(- (* (1+ ,offset) word-bytes) other-pointer-type))
,(if frob-code
`(move code-tn ,return-pc)
'(inst nop))))
(defmacro emit-return-pc (label)
"Emit a return-pc header word. LABEL is the label to use for this return-pc."
`(progn
(align lowtag-bits)
(emit-label ,label)
(inst lra-header-word)))
;;;; Stack TN's
;;; Load-Stack-TN, Store-Stack-TN -- Interface
;;;
;;; Move a stack TN to a register and vice-versa.
;;;
(defmacro load-stack-tn (reg stack)
`(let ((reg ,reg)
(stack ,stack))
(let ((offset (tn-offset stack)))
(sc-case stack
((control-stack)
(loadw reg cfp-tn offset))))))
(defmacro store-stack-tn (stack reg)
`(let ((stack ,stack)
(reg ,reg))
(let ((offset (tn-offset stack)))
(sc-case stack
((control-stack)
(storew reg cfp-tn offset))))))
;;; MAYBE-LOAD-STACK-TN -- Interface
;;;
(defmacro maybe-load-stack-tn (reg reg-or-stack)
"Move the TN Reg-Or-Stack into Reg if it isn't already there."
(once-only ((n-reg reg)
(n-stack reg-or-stack))
`(sc-case ,n-reg
((any-reg descriptor-reg)
(sc-case ,n-stack
((any-reg descriptor-reg)
(move ,n-reg ,n-stack))
((control-stack)
(loadw ,n-reg cfp-tn (tn-offset ,n-stack))))))))
;;;; Storage allocation:
(defmacro with-fixed-allocation ((result-tn temp-tn type-code size)
&body body)
"Do stuff to allocate an other-pointer object of fixed Size with a single
word header having the specified Type-Code. The result is placed in
Result-TN, and Temp-TN is a non-descriptor temp (which may be randomly used
by the body.) The body is placed inside the PSEUDO-ATOMIC, and presumably
initializes the object."
(once-only ((result-tn result-tn) (temp-tn temp-tn)
(type-code type-code) (size size))
`(pseudo-atomic (:extra (pad-data-block ,size))
(inst or ,result-tn alloc-tn other-pointer-type)
(inst li ,temp-tn (logior (ash (1- ,size) type-bits) ,type-code))
(storew ,temp-tn ,result-tn 0 other-pointer-type)
,@body)))
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
;;;; Type testing noise.
;;; GEN-RANGE-TEST -- internal
;;;
;;; Generate code that branches to TARGET iff REG contains one of VALUES.
;;; If NOT-P is true, invert the test. Jumping to NOT-TARGET is the same
;;; as falling out the bottom.
;;;
(defun gen-range-test (reg target not-target not-p min seperation max values)
(let ((tests nil)
(start nil)
(end nil)
(insts nil))
(multiple-value-bind (equal less-or-equal greater-or-equal label)
(if not-p
(values :ne :gt :lt not-target)
(values :eq :le :ge target))
(flet ((emit-test ()
(if (= start end)
(push start tests)
(push (cons start end) tests))))
(dolist (value values)
(cond ((< value min)
(error "~S is less than the specified minimum of ~S"
value min))
((> value max)
(error "~S is greater than the specified maximum of ~S"
value max))
((not (zerop (rem (- value min) seperation)))
(error "~S isn't an even multiple of ~S from ~S"
value seperation min))
((null start)
(setf start value))
((> value (+ end seperation))
(emit-test)
(setf start value)))
(setf end value))
(emit-test))
(macrolet ((inst (name &rest args)
`(push (list 'inst ',name ,@args) insts)))
(do ((remaining (nreverse tests) (cdr remaining)))
((null remaining))
(let ((test (car remaining))
(last (null (cdr remaining))))
(if (atom test)
(progn
(inst cmp reg test)
(if last
(inst b equal target)
(inst b :eq label)))
(let ((start (car test))
(end (cdr test)))
(cond ((and (= start min) (= end max))
(warn "The values ~S cover the entire range from ~
~S to ~S [step ~S]."
values min max seperation)
(push `(unless ,not-p (inst b ,target)) insts))
((= start min)
(inst cmp reg end)
(if last
(inst b less-or-equal target)
(inst b :le label)))
((= end max)
(inst cmp reg start)
(if last
(inst b greater-or-equal target)
(inst b :ge label)))
(t
(inst cmp reg start)
(inst b :lt (if not-p target not-target))
(inst cmp reg end)
(if last
(inst b less-or-equal target)
(inst b :le label))))))))))
(nreverse insts)))
(defun gen-other-immediate-test (reg target not-target not-p values)
(gen-range-test reg target not-target not-p
(+ other-immediate-0-type lowtag-limit)
(- other-immediate-1-type other-immediate-0-type)
(ash 1 type-bits)
values))
(defun test-type-aux (reg temp target not-target not-p lowtags immed hdrs
function-p)
(let* ((fixnump (and (member even-fixnum-type lowtags :test #'eql)
(member odd-fixnum-type lowtags :test #'eql)))
(lowtags (sort (if fixnump
(delete even-fixnum-type
(remove odd-fixnum-type lowtags
:test #'eql)
:test #'eql)
(copy-list lowtags))
#'<))
(lowtag (if function-p
vm:function-pointer-type
vm:other-pointer-type))
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
(hdrs (sort (copy-list hdrs) #'<))
(immed (sort (copy-list immed) #'<)))
(append
(when immed
`((inst and ,temp ,reg type-mask)
,@(if (or fixnump lowtags hdrs)
(let ((fall-through (gensym)))
`((let (,fall-through (gen-label))
,@(gen-other-immediate-test
temp (if not-p not-target target)
fall-through nil immed)
(emit-label ,fall-through))))
(gen-other-immediate-test temp target not-target not-p immed))))
(when fixnump
`((inst andcc zero-tn ,reg 3)
,(if (or lowtags hdrs)
`(inst b :eq ,(if not-p not-target target))
`(inst b ,(if not-p :ne :eq) ,target))))
(when (or lowtags hdrs)
`((inst and ,temp ,reg lowtag-mask)))
(when lowtags
(if hdrs
(let ((fall-through (gensym)))
`((let ((,fall-through (gen-label)))
,@(gen-range-test temp (if not-p not-target target)
fall-through nil
0 1 (1- lowtag-limit) lowtags)
(emit-label ,fall-through))))
(gen-range-test temp target not-target not-p 0 1
(1- lowtag-limit) lowtags)))
(when hdrs
`((inst cmp ,temp ,lowtag)
(load-type ,temp ,reg (- ,lowtag))
,@(gen-other-immediate-test temp target not-target not-p hdrs))))))
(defconstant immediate-types
(defconstant function-subtypes
(list funcallable-instance-header-type closure-header-type
function-header-type closure-function-header-type))
(defmacro test-type (register temp target not-p &rest type-codes)
(let* ((type-codes (mapcar #'eval type-codes))
(lowtags (remove lowtag-limit type-codes :test #'<))
(extended (remove lowtag-limit type-codes :test #'>))
(immediates (intersection extended immediate-types :test #'eql))
(headers (set-difference extended immediate-types :test #'eql))
(function-p nil))
(unless type-codes
(error "Must supply at least on type for test-type."))
(when (and headers (member other-pointer-type lowtags))
(warn "OTHER-POINTER-TYPE supersedes the use of ~S" headers)
(setf headers nil))
(when (and immediates
(or (member other-immediate-0-type lowtags)
(member other-immediate-1-type lowtags)))
(warn "OTHER-IMMEDIATE-n-TYPE supersedes the use of ~S" immediates)
(setf immediates nil))
(when (intersection headers function-subtypes)
(unless (subsetp headers function-subtypes)
(error "Can't test for mix of function subtypes and normal ~
header types."))
(setq function-p t))
(let ((n-reg (gensym))
(n-temp (gensym))
(n-target (gensym))
(not-target (gensym)))
`(let ((,n-reg ,register)
(,n-temp ,temp)
(,n-target ,target)
(,not-target (gen-label)))
(declare (ignorable ,n-temp))
,@(if (constantp not-p)
(test-type-aux n-reg n-temp n-target not-target
(eval not-p) lowtags immediates headers
function-p)
`((cond (,not-p
,@(test-type-aux n-reg n-temp n-target not-target t
lowtags immediates headers
function-p))
lowtags immediates headers
function-p)))))
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
(inst nop)
(emit-label ,not-target)))))
;;;; Error Code
(defvar *adjustable-vectors* nil)
(defmacro with-adjustable-vector ((var) &rest body)
`(let ((,var (or (pop *adjustable-vectors*)
(make-array 16
:element-type '(unsigned-byte 8)
:fill-pointer 0
:adjustable t))))
(setf (fill-pointer ,var) 0)
(unwind-protect
(progn
,@body)
(push ,var *adjustable-vectors*))))
(eval-when (compile load eval)
(defun emit-error-break (vop kind code values)
(let ((vector (gensym)))
`((let ((vop ,vop))
(when vop
(note-this-location vop :internal-error)))
(inst unimp ,kind)
(with-adjustable-vector (,vector)
(write-var-integer (error-number-or-lose ',code) ,vector)
,@(mapcar #'(lambda (tn)
`(let ((tn ,tn))
(write-var-integer (make-sc-offset (sc-number
(tn-sc tn))
(tn-offset tn))
,vector)))
values)
(inst byte (length ,vector))
(dotimes (i (length ,vector))
(inst byte (aref ,vector i))))
(align word-shift)))))
(defmacro error-call (vop error-code &rest values)
"Cause an error. ERROR-CODE is the error to cause."
(cons 'progn
(emit-error-break vop error-trap error-code values)))
(defmacro cerror-call (vop label error-code &rest values)
"Cause a continuable error. If the error is continued, execution resumes at
LABEL."
`(progn
(inst b ,label)
,@(emit-error-break vop cerror-trap error-code values)))
(defmacro generate-error-code (vop error-code &rest values)
"Generate-Error-Code Error-code Value*
Emit code for an error with the specified Error-Code and context Values."
`(assemble (*elsewhere*)
(let ((start-lab (gen-label)))
(emit-label start-lab)
(error-call ,vop ,error-code ,@values)
start-lab)))
(defmacro generate-cerror-code (vop error-code &rest values)
"Generate-CError-Code Error-code Value*
Emit code for a continuable error with the specified Error-Code and
context Values. If the error is continued, execution resumes after
the GENERATE-CERROR-CODE form."
(let ((continue (gensym "CONTINUE-LABEL-"))
(error (gensym "ERROR-LABEL-")))
`(let ((,continue (gen-label)))
(emit-label ,continue)
(assemble (*elsewhere*)
(let ((,error (gen-label)))
(emit-label ,error)
(cerror-call ,vop ,continue ,error-code ,@values)
,error)))))
;;; PSEUDO-ATOMIC -- Handy macro for making sequences look atomic.
;;;
(defmacro pseudo-atomic ((&key (extra 0)) &rest forms)
(let ((n-extra (gensym)))
`(let ((,n-extra ,extra))
(without-scheduling ()
(inst add alloc-tn 4))
(without-scheduling ()
(inst taddcctv alloc-tn (- ,n-extra 4))))))