3625f66ba110abe20ae31bdb11ac3eb175dee8cc
[sbcl.git] / src / compiler / generic / utils.lisp
1 ;;;; utility functions and macros needed by the back end to generate
2 ;;;; code
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!VM")
14 \f
15 ;;; Make a fixnum out of NUM. (I.e. shift by two bits if it will fit.)
16 (defun fixnumize (num)
17   (if (fixnump num)
18       (ash num (1- n-lowtag-bits))
19       (error "~W is too big for a fixnum." num)))
20
21 ;;; Determining whether a constant offset fits in an addressing mode.
22 #!+(or x86 x86-64)
23 (defun foldable-constant-offset-p (element-size lowtag data-offset offset)
24   (if (< element-size n-byte-bits)
25       nil
26       (multiple-value-bind (min max)
27           (sb!impl::displacement-bounds lowtag element-size data-offset)
28         (<= min offset max))))
29
30 \f
31 ;;;; routines for dealing with static symbols
32
33 (defun static-symbol-p (symbol)
34   (or (null symbol)
35       (and (member symbol *static-symbols*) t)))
36
37 ;;; the byte offset of the static symbol SYMBOL
38 (defun static-symbol-offset (symbol)
39   (if symbol
40       (let ((posn (position symbol *static-symbols*)))
41         (unless posn (error "~S is not a static symbol." symbol))
42         (+ (* posn (pad-data-block symbol-size))
43            (pad-data-block (1- symbol-size))
44            other-pointer-lowtag
45            (- list-pointer-lowtag)))
46       0))
47
48 ;;; Given a byte offset, OFFSET, return the appropriate static symbol.
49 (defun offset-static-symbol (offset)
50   (if (zerop offset)
51       nil
52       (multiple-value-bind (n rem)
53           (truncate (+ offset list-pointer-lowtag (- other-pointer-lowtag)
54                        (- (pad-data-block (1- symbol-size))))
55                     (pad-data-block symbol-size))
56         (unless (and (zerop rem) (<= 0 n (1- (length *static-symbols*))))
57           (error "The byte offset ~W is not valid." offset))
58         (elt *static-symbols* n))))
59
60 ;;; Return the (byte) offset from NIL to the start of the fdefn object
61 ;;; for the static function NAME.
62 (defun static-fdefn-offset (name)
63   (let ((static-syms (length *static-symbols*))
64         (static-fun-index (position name *static-funs*)))
65     (unless static-fun-index
66       (error "~S isn't a static function." name))
67     (+ (* static-syms (pad-data-block symbol-size))
68        (pad-data-block (1- symbol-size))
69        (- list-pointer-lowtag)
70        (* static-fun-index (pad-data-block fdefn-size))
71        other-pointer-lowtag)))
72
73 ;;; Return the (byte) offset from NIL to the raw-addr slot of the
74 ;;; fdefn object for the static function NAME.
75 (defun static-fun-offset (name)
76   (+ (static-fdefn-offset name)
77      (- other-pointer-lowtag)
78      (* fdefn-raw-addr-slot n-word-bytes)))
79 \f
80 ;;; Various error-code generating helpers
81 (defvar *adjustable-vectors* nil)
82
83 (defmacro with-adjustable-vector ((var) &rest body)
84   `(let ((,var (or (pop *adjustable-vectors*)
85                    (make-array 16
86                                :element-type '(unsigned-byte 8)
87                                :fill-pointer 0
88                                :adjustable t))))
89      (declare (type (vector (unsigned-byte 8) 16) ,var))
90      (setf (fill-pointer ,var) 0)
91      (unwind-protect
92          (progn
93            ,@body)
94        (push ,var *adjustable-vectors*))))