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.
6 ;;;; This software is part of the SBCL system. See the README file for
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.
17 ;;;; utilities used during code generation
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)))
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
30 (defun sb-allocated-size (name)
31 (finite-sb-current-size (sb-or-lose name)))
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
40 (block-physenv block)))
41 (ir2-component-nfp (component-info (block-component block)))))))
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
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*)))))
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))
56 ;;;; specials used during code generation
58 (defvar *trace-table-info*)
59 (defvar *code-segment* nil)
60 (defvar *elsewhere* nil)
61 (defvar *elsewhere-label* nil)
63 ;;;; noise to emit an instruction trace
65 (defvar *prev-segment*)
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-name segment))
72 (setf *prev-segment* segment))
73 (unless (eq *prev-vop* vop)
78 (format *compiler-trace-output* "~S~%" vop)))
80 (setf *prev-vop* vop))
83 (format t "~A:~%" args))
85 (format t "~0,8T.align~0,8T~A~%" args))
87 (format t "~0,8T~A~@[~0,8T~{~A~^, ~}~]~%" inst args))))
90 ;;;; GENERATE-CODE and support routines
92 ;;; standard defaults for slots of SEGMENT objects
93 (defun default-segment-run-scheduler ()
94 (and *assembly-optimize*
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))
103 (defun init-assembler ()
105 (sb!assem:make-segment :name "regular"
106 :run-scheduler (default-segment-run-scheduler)
107 :inst-hook (default-segment-inst-hook)))
109 (setf (sb!assem:segment-collect-dynamic-statistics *code-segment*)
110 *collect-dynamic-statistics*)
112 (sb!assem:make-segment :name "elsewhere"
113 :run-scheduler (default-segment-run-scheduler)
114 :inst-hook (default-segment-inst-hook)))
117 (defun generate-code (component)
118 (when *compiler-trace-output*
119 (format *compiler-trace-output*
120 "~|~%assembly code for ~S~2%"
123 (*trace-table-info* 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 (sb!assem:emit-label (block-label 1block)))
137 (let ((env (block-physenv 1block)))
138 (unless (eq env prev-env)
139 (let ((lab (gen-label)))
140 (setf (ir2-physenv-elsewhere-start (physenv-info env))
142 (emit-label-elsewhere lab))
143 (setq prev-env env)))))
144 (do ((vop (ir2-block-start-vop block) (vop-next vop)))
146 (let ((gen (vop-info-generator-function (vop-info vop))))
150 "missing generator for ~S~%"
151 (template-name (vop-info vop)))))))
152 (sb!assem:append-segment *code-segment* *elsewhere*)
153 (setf *elsewhere* nil)
154 (values (sb!assem:finalize-segment *code-segment*)
155 (nreverse *trace-table-info*)
158 (defun emit-label-elsewhere (label)
159 (sb!assem:assemble (*elsewhere*)
160 (sb!assem:emit-label label)))
162 (defun label-elsewhere-p (label-or-posn)
163 (<= (label-position *elsewhere-label*)
164 (etypecase label-or-posn
166 (label-position label-or-posn))