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 (defun component-header-length (&optional
20 (component *component-being-compiled*))
22 "Returns the number of bytes used by the code object header."
23 (let* ((2comp (component-info component))
24 (constants (ir2-component-constants 2comp))
25 (num-consts (length constants)))
26 (ash (logandc2 (1+ num-consts) 1) sb!vm:word-shift)))
28 (defun sb-allocated-size (name)
30 "The size of the Name'd SB in the currently compiled component. Useful
31 mainly for finding the size for allocating stack frames."
32 (finite-sb-current-size (sb-or-lose name)))
34 (defun current-nfp-tn (vop)
36 "Return the TN that is used to hold the number stack frame-pointer in VOP's
37 function. Returns NIL if no number stack frame was allocated."
38 (unless (zerop (sb-allocated-size 'non-descriptor-stack))
39 (let ((block (ir2-block-block (vop-block vop))))
40 (when (ir2-environment-number-stack-p
42 (block-environment block)))
43 (ir2-component-nfp (component-info (block-component block)))))))
45 (defun callee-nfp-tn (2env)
47 "Return the TN that is used to hold the number stack frame-pointer in the
48 function designated by 2env. Returns NIL if no number stack frame was
50 (unless (zerop (sb-allocated-size 'non-descriptor-stack))
51 (when (ir2-environment-number-stack-p 2env)
52 (ir2-component-nfp (component-info *component-being-compiled*)))))
54 (defun callee-return-pc-tn (2env)
56 "Return the TN used for passing the return PC in a local call to the function
58 (ir2-environment-return-pc-pass 2env))
60 ;;;; specials used during code generation
62 (defvar *trace-table-info*)
63 (defvar *code-segment* nil)
64 (defvar *elsewhere* nil)
65 (defvar *elsewhere-label* nil)
67 ;;;; noise to emit an instruction trace
69 (defvar *prev-segment*)
73 (defun trace-instruction (segment vop inst args)
74 (let ((*standard-output* *compiler-trace-output*))
75 (unless (eq *prev-segment* segment)
76 (format t "in the ~A segment:~%" (sb!assem:segment-name segment))
77 (setf *prev-segment* segment))
78 (unless (eq *prev-vop* vop)
83 (format *compiler-trace-output* "~S~%" vop)))
85 (setf *prev-vop* vop))
88 (format t "~A:~%" args))
90 (format t "~0,8T.align~0,8T~A~%" args))
92 (format t "~0,8T~A~@[~0,8T~{~A~^, ~}~]~%" inst args))))
95 ;;;; GENERATE-CODE and support routines
97 ;;; standard defaults for slots of SEGMENT objects
98 (defun default-segment-run-scheduler ()
99 (and *assembly-optimize*
102 (block-next (component-head *component-being-compiled*))))
103 (or (> speed compilation-speed) (> space compilation-speed)))))
104 (defun default-segment-inst-hook ()
106 (and *compiler-trace-output* #'trace-instruction))
108 (defun init-assembler ()
110 (sb!assem:make-segment :name "regular"
111 :run-scheduler (default-segment-run-scheduler)
112 :inst-hook (default-segment-inst-hook)))
114 (setf (sb!assem:segment-collect-dynamic-statistics *code-segment*)
115 *collect-dynamic-statistics*)
117 (sb!assem:make-segment :name "elsewhere"
118 :run-scheduler (default-segment-run-scheduler)
119 :inst-hook (default-segment-inst-hook)))
122 (defun generate-code (component)
124 (when *compiler-trace-output*
125 (format *compiler-trace-output*
126 "~|~%assembly code for ~S~2%"
129 (*trace-table-info* nil)
133 (let ((label (sb!assem:gen-label)))
134 (setf *elsewhere-label* label)
135 (sb!assem:assemble (*elsewhere*)
136 (sb!assem:emit-label label)))
137 (do-ir2-blocks (block component)
138 (let ((1block (ir2-block-block block)))
139 (when (and (eq (block-info 1block) block)
140 (block-start 1block))
141 (sb!assem:assemble (*code-segment*)
142 (sb!assem:emit-label (block-label 1block)))
143 (let ((env (block-environment 1block)))
144 (unless (eq env prev-env)
145 (let ((lab (gen-label)))
146 (setf (ir2-environment-elsewhere-start (environment-info env))
148 (emit-label-elsewhere lab))
149 (setq prev-env env)))))
150 (do ((vop (ir2-block-start-vop block) (vop-next vop)))
152 (let ((gen (vop-info-generator-function (vop-info vop))))
156 "missing generator for ~S~%"
157 (template-name (vop-info vop)))))))
158 (sb!assem:append-segment *code-segment* *elsewhere*)
159 (setf *elsewhere* nil)
160 (values (sb!assem:finalize-segment *code-segment*)
161 (nreverse *trace-table-info*)
164 (defun emit-label-elsewhere (label)
165 (sb!assem:assemble (*elsewhere*)
166 (sb!assem:emit-label label)))
168 (defun label-elsewhere-p (label-or-posn)
169 (<= (label-position *elsewhere-label*)
170 (etypecase label-or-posn
172 (label-position label-or-posn))