0.pre7.56:
[sbcl.git] / src / compiler / generic / utils.lisp
1 ;;;; utility functions needed by the back end to generate code
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13 \f
14 (defun fixnumize (num)
15   #!+sb-doc
16   "Make a fixnum out of NUM. (i.e. shift by two bits if it will fit.)"
17   (if (<= #x-20000000 num #x1fffffff)
18       (ash num 2)
19       (error "~D is too big for a fixnum." num)))
20 \f
21 ;;;; routines for dealing with static symbols
22
23 (defun static-symbol-p (symbol)
24   (or (null symbol)
25       (and (member symbol *static-symbols*) t)))
26
27 (defun static-symbol-offset (symbol)
28   #!+sb-doc
29   "the byte offset of the static symbol SYMBOL"
30   (if symbol
31       (let ((posn (position symbol *static-symbols*)))
32         (unless posn (error "~S is not a static symbol." symbol))
33         (+ (* posn (pad-data-block symbol-size))
34            (pad-data-block (1- symbol-size))
35            other-pointer-lowtag
36            (- list-pointer-lowtag)))
37       0))
38
39 (defun offset-static-symbol (offset)
40   #!+sb-doc
41   "Given a byte offset, OFFSET, return the appropriate static symbol."
42   (if (zerop offset)
43       nil
44       (multiple-value-bind (n rem)
45           (truncate (+ offset list-pointer-lowtag (- other-pointer-lowtag)
46                        (- (pad-data-block (1- symbol-size))))
47                     (pad-data-block symbol-size))
48         (unless (and (zerop rem) (<= 0 n (1- (length *static-symbols*))))
49           (error "The byte offset ~D is not valid." offset))
50         (elt *static-symbols* n))))
51
52 (defun static-function-offset (name)
53   #!+sb-doc
54   "Return the (byte) offset from NIL to the start of the fdefn object
55    for the static function NAME."
56   (let ((static-syms (length *static-symbols*))
57         (static-function-index (position name *static-functions*)))
58     (unless static-function-index
59       (error "~S isn't a static function." name))
60     (+ (* static-syms (pad-data-block symbol-size))
61        (pad-data-block (1- symbol-size))
62        (- list-pointer-lowtag)
63        (* static-function-index (pad-data-block fdefn-size))
64        (* fdefn-raw-addr-slot word-bytes))))