df6d06a6a0f5825d8ec2854f50faa8a0af50151d
[sbcl.git] / src / compiler / codegen.lisp
1 ;;;; the implementation-independent parts of the code generator. We use
2 ;;;; functions and information provided by the VM definition to convert
3 ;;;; IR2 into assembly code. After emitting code, we finish the
4 ;;;; assembly and then do the post-assembly phase.
5
6 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; more information.
8 ;;;;
9 ;;;; This software is derived from the CMU CL system, which was
10 ;;;; written at Carnegie Mellon University and released into the
11 ;;;; public domain. The software is in the public domain and is
12 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
13 ;;;; files for more information.
14
15 (in-package "SB!C")
16 \f
17 ;;;; utilities used during code generation
18
19 ;;; the number of bytes used by the code object header
20 (defun component-header-length (&optional
21                                 (component *component-being-compiled*))
22   (let* ((2comp (component-info component))
23          (constants (ir2-component-constants 2comp))
24          (num-consts (length constants)))
25     (ash (logandc2 (1+ num-consts) 1) sb!vm:word-shift)))
26
27 ;;; the size of the NAME'd SB in the currently compiled component.
28 ;;; This is useful mainly for finding the size for allocating stack
29 ;;; frames.
30 (defun sb-allocated-size (name)
31   (finite-sb-current-size (sb-or-lose name)))
32
33 ;;; the TN that is used to hold the number stack frame-pointer in
34 ;;; VOP's function, or NIL if no number stack frame was allocated
35 (defun current-nfp-tn (vop)
36   (unless (zerop (sb-allocated-size 'non-descriptor-stack))
37     (let ((block (ir2-block-block (vop-block vop))))
38     (when (ir2-physenv-number-stack-p
39            (physenv-info
40             (block-physenv block)))
41       (ir2-component-nfp (component-info (block-component block)))))))
42
43 ;;; the TN that is used to hold the number stack frame-pointer in the
44 ;;; function designated by 2ENV, or NIL if no number stack frame was
45 ;;; allocated
46 (defun callee-nfp-tn (2env)
47   (unless (zerop (sb-allocated-size 'non-descriptor-stack))
48     (when (ir2-physenv-number-stack-p 2env)
49       (ir2-component-nfp (component-info *component-being-compiled*)))))
50
51 ;;; the TN used for passing the return PC in a local call to the function
52 ;;; designated by 2ENV
53 (defun callee-return-pc-tn (2env)
54   (ir2-physenv-return-pc-pass 2env))
55 \f
56 ;;;; specials used during code generation
57
58 (defvar *trace-table-info*)
59 (defvar *code-segment* nil)
60 (defvar *elsewhere* nil)
61 (defvar *elsewhere-label* nil)
62 \f
63 ;;;; noise to emit an instruction trace
64
65 (defvar *prev-segment*)
66 (defvar *prev-vop*)
67
68 (defun trace-instruction (segment vop inst args)
69   (let ((*standard-output* *compiler-trace-output*))
70     (unless (eq *prev-segment* segment)
71       (format t "in the ~A segment:~%" (sb!assem:segment-type segment))
72       (setf *prev-segment* segment))
73     (unless (eq *prev-vop* vop)
74       (when vop
75         (format t "~%VOP ")
76         (if (vop-p vop)
77             (print-vop vop)
78             (format *compiler-trace-output* "~S~%" vop)))
79       (terpri)
80       (setf *prev-vop* vop))
81     (case inst
82       (:label
83        (format t "~A:~%" args))
84       (:align
85        (format t "~0,8T.align~0,8T~A~%" args))
86       (t
87        (format t "~0,8T~A~@[~0,8T~{~A~^, ~}~]~%" inst args))))
88   (values))
89 \f
90 ;;;; GENERATE-CODE and support routines
91
92 ;;; standard defaults for slots of SEGMENT objects
93 (defun default-segment-run-scheduler ()
94   (and *assembly-optimize*
95         (policy (lambda-bind
96                  (block-home-lambda
97                   (block-next (component-head *component-being-compiled*))))
98                 (or (> speed compilation-speed) (> space compilation-speed)))))
99 (defun default-segment-inst-hook ()
100   (and *compiler-trace-output*
101        #'trace-instruction))
102
103 (defun init-assembler ()
104   (setf *code-segment*
105         (sb!assem:make-segment :type :regular
106                                :run-scheduler (default-segment-run-scheduler)
107                                :inst-hook (default-segment-inst-hook)))
108   #!+sb-dyncount
109   (setf (sb!assem:segment-collect-dynamic-statistics *code-segment*)
110         *collect-dynamic-statistics*)
111   (setf *elsewhere*
112         (sb!assem:make-segment :type :elsewhere
113                                :run-scheduler (default-segment-run-scheduler)
114                                :inst-hook (default-segment-inst-hook)))
115   (values))
116
117 (defun generate-code (component)
118   (when *compiler-trace-output*
119     (format *compiler-trace-output*
120             "~|~%assembly code for ~S~2%"
121             component))
122   (let ((prev-env nil)
123         (*trace-table-info* nil)
124         (*prev-segment* nil)
125         (*prev-vop* nil)
126         (*fixup-notes* nil))
127     (let ((label (sb!assem:gen-label)))
128       (setf *elsewhere-label* label)
129       (sb!assem:assemble (*elsewhere*)
130         (sb!assem:emit-label label)))
131     (do-ir2-blocks (block component)
132       (let ((1block (ir2-block-block block)))
133         (when (and (eq (block-info 1block) block)
134                    (block-start 1block))
135           (sb!assem:assemble (*code-segment*)
136             ;; Align first emitted block of each loop: x86 and x86-64 both
137             ;; like 16 byte alignment, however, since x86 aligns code objects
138             ;; on 8 byte boundaries we cannot guarantee proper loop alignment
139             ;; there (yet.)
140             #!+x86-64
141             (let ((cloop (sb!c::block-loop 1block)))
142               (when (and cloop
143                          (sb!c::loop-tail cloop)
144                          (not (sb!c::loop-info cloop)))
145                 (sb!assem:emit-alignment sb!vm:n-lowtag-bits #x90)
146                 ;; Mark the loop as aligned by saving the IR1 block aligned.
147                 (setf (sb!c::loop-info cloop) 1block)))
148             (sb!assem:emit-label (block-label 1block)))
149           (let ((env (block-physenv 1block)))
150             (unless (eq env prev-env)
151               (let ((lab (gen-label)))
152                 (setf (ir2-physenv-elsewhere-start (physenv-info env))
153                       lab)
154                 (emit-label-elsewhere lab))
155               (setq prev-env env)))))
156       (do ((vop (ir2-block-start-vop block) (vop-next vop)))
157           ((null vop))
158         (let ((gen (vop-info-generator-function (vop-info vop))))
159           (if gen
160             (funcall gen vop)
161             (format t
162                     "missing generator for ~S~%"
163                     (template-name (vop-info vop)))))))
164     (sb!assem:append-segment *code-segment* *elsewhere*)
165     (setf *elsewhere* nil)
166     (values (sb!assem:finalize-segment *code-segment*)
167             (nreverse *trace-table-info*)
168             *fixup-notes*)))
169
170 (defun emit-label-elsewhere (label)
171   (sb!assem:assemble (*elsewhere*)
172     (sb!assem:emit-label label)))
173
174 (defun label-elsewhere-p (label-or-posn)
175   (<= (label-position *elsewhere-label*)
176       (etypecase label-or-posn
177         (label
178          (label-position label-or-posn))
179         (index
180          label-or-posn))))