1 ;;;; the extra code necessary to feed an entire file of assembly code
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;;; If non-NIL, emit assembly code. If NIL, emit VOP templates.
16 (defvar *emit-assembly-code-not-vops-p
* nil
)
18 ;;; a list of (NAME LABEL OFFSET) for every entry point
19 (defvar *entry-points
* nil
)
21 ;;; Note: You might think from the name that this would act like
22 ;;; COMPILE-FILE, but in fact it's arguably more like LOAD, even down
23 ;;; to the return convention. It LOADs a file, then writes out any
24 ;;; assembly code created by the process.
25 (defun assemble-file (name
27 (output-file (make-pathname :defaults name
29 (when sb-cold
::*compile-for-effect-only
*
30 (return-from assemble-file t
))
31 ;; FIXME: Consider nuking the filename defaulting logic here.
32 (let* ((*emit-assembly-code-not-vops-p
* t
)
33 (name (pathname name
))
34 ;; the fasl file currently being output to
35 (lap-fasl-output (open-fasl-output (pathname output-file
) name
))
40 (*assembly-optimize
* nil
)
49 (let ((*features
* (cons :sb-assembling
*features
*)))
51 (load (merge-pathnames name
(make-pathname :type
"lisp")))
52 (sb!assem
:append-segment
*code-segment
* *elsewhere
*)
53 (setf *elsewhere
* nil
)
55 (emit-inline-constants)
56 (let ((length (sb!assem
:finalize-segment
*code-segment
*)))
57 (dump-assembler-routines *code-segment
*
63 (close-fasl-output lap-fasl-output
(not won
)))
66 (defstruct (reg-spec (:copier nil
))
67 (kind :temp
:type
(member :arg
:temp
:res
))
68 (name nil
:type symbol
)
69 (temp nil
:type symbol
)
70 (scs nil
:type
(or list symbol
))
72 (def!method print-object
((spec reg-spec
) stream
)
73 (print-unreadable-object (spec stream
:type t
)
75 ":KIND ~S :NAME ~S :SCS ~S :OFFSET ~S"
79 (reg-spec-offset spec
))))
81 (defun reg-spec-sc (spec)
82 (if (atom (reg-spec-scs spec
))
84 (car (reg-spec-scs spec
))))
86 (defun parse-reg-spec (kind name sc offset
)
87 (let ((reg (make-reg-spec :kind kind
:name name
:scs sc
:offset offset
)))
91 (setf (reg-spec-temp reg
) (make-symbol (symbol-name name
)))))
94 (defun expand-one-export-spec (export)
96 `(list ',export
,export
0)
98 (name (operator base-label offset
))
100 ;; KLUDGE: Presume that all compound export specs are of the
101 ;; form (NAME (OPERATOR BASE-LABEL OFFSET)), where OPERATOR is
102 ;; + or -, BASE-LABEL is a LABEL in the present scope, and
103 ;; OFFSET evaluates to an integer. Ideally, we should be
104 ;; smarter about this.
105 `(list ',name
,base-label
(,operator
,offset
)))))
107 (defun expand-export-option (exports)
109 for export in exports
110 collect
`(push ,(expand-one-export-spec export
) *entry-points
*)))
112 (defun expand-align-option (align)
114 `((emit-alignment ,align
))))
116 (defun emit-assemble (name options regs code
)
119 (if (and (consp code
) (consp (car code
)) (eq (caar code
) 'declare
))
122 `(let ,(mapcar (lambda (reg)
123 `(,(reg-spec-name reg
)
126 :sc
(sc-or-lose ',(reg-spec-sc reg
))
127 :offset
,(reg-spec-offset reg
))))
130 (sb!assem
:assemble
(*code-segment
* ',name
)
131 ,@(expand-align-option (cadr (assoc :align options
)))
133 (push (list ',name
,name
0) *entry-points
*)
134 ,@(expand-export-option (cdr (assoc :export options
)))
136 ,@(generate-return-sequence
137 (or (cadr (assoc :return-style options
)) :raw
))
138 (emit-alignment sb
!vm
:n-lowtag-bits
))
139 (when sb
!xc
:*compile-print
*
140 (format *error-output
* "~S assembled~%" ',name
)))))
142 (defun arg-or-res-spec (reg)
143 `(,(reg-spec-name reg
)
144 :scs
,(if (atom (reg-spec-scs reg
))
145 (list (reg-spec-scs reg
))
147 ,@(unless (eq (reg-spec-kind reg
) :res
)
148 `(:target
,(reg-spec-temp reg
)))))
150 (defun emit-assemble-vop (name options vars
)
151 (let* ((args (remove :arg vars
:key
#'reg-spec-kind
:test
#'neq
))
152 (temps (remove :temp vars
:key
#'reg-spec-kind
:test
#'neq
))
153 (results (remove :res vars
:key
#'reg-spec-kind
:test
#'neq
))
154 (return-style (or (cadr (assoc :return-style options
)) :raw
))
155 (cost (or (cadr (assoc :cost options
)) 247))
156 (vop (make-symbol "VOP")))
157 (unless (member return-style
'(:raw
:full-call
:none
))
158 (error "unknown return-style for ~S: ~S" name return-style
))
159 (multiple-value-bind (call-sequence call-temps
)
160 (generate-call-sequence name return-style vop options
)
161 `(define-vop ,(if (atom name
) (list name
) name
)
162 (:args
,@(mapcar #'arg-or-res-spec args
))
164 (mapcar (lambda (arg)
165 `(:temporary
(:sc
,(reg-spec-sc arg
)
166 :offset
,(reg-spec-offset arg
)
167 :from
(:argument
,(incf index
))
169 ,(reg-spec-temp arg
)))
171 ,@(mapcar (lambda (temp)
172 `(:temporary
(:sc
,(reg-spec-sc temp
)
173 :offset
,(reg-spec-offset temp
)
176 ,(reg-spec-name temp
)))
180 (mapcar (lambda (res)
181 `(:temporary
(:sc
,(reg-spec-sc res
)
182 :offset
,(reg-spec-offset res
)
184 :to
(:result
,(incf index
))
185 :target
,(reg-spec-name res
))
186 ,(reg-spec-temp res
)))
188 (:results
,@(mapcar #'arg-or-res-spec results
))
189 ;; This formerly unioned in the contents of an :ignore clause from
190 ;; the value of the 'call-temps' variable, for no good reason afaict.
191 (:ignore
,@(sort (set-difference (mapcar #'reg-spec-name temps
)
192 (cdr (assoc :call-temps options
)))
195 ;; This too is a tad sleazy - because of how VOP parsing works,
196 ;; any :SAVE-P specified in options supersedes one from call-temps.
197 ;; It would be wiser to signal an error about duplicate options.
198 ,@(remove-if (lambda (x)
199 (member x
'(:return-style
:cost
:call-temps
)))
203 ,@(mapcar (lambda (arg)
204 #!+(or hppa alpha
) `(move ,(reg-spec-name arg
)
205 ,(reg-spec-temp arg
))
206 #!-
(or hppa alpha
) `(move ,(reg-spec-temp arg
)
207 ,(reg-spec-name arg
)))
210 ,@(mapcar (lambda (res)
211 #!+(or hppa alpha
) `(move ,(reg-spec-temp res
)
212 ,(reg-spec-name res
))
213 #!-
(or hppa alpha
) `(move ,(reg-spec-name res
)
214 ,(reg-spec-temp res
)))