Initial revision
[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
14 (file-comment
15   "$Header$")
16 \f
17 (defun fixnumize (num)
18   #!+sb-doc
19   "Make a fixnum out of NUM. (i.e. shift by two bits if it will fit.)"
20   (if (<= #x-20000000 num #x1fffffff)
21       (ash num 2)
22       (error "~D is too big for a fixnum." num)))
23 \f
24 ;;;; routines for dealing with static symbols
25
26 (defun static-symbol-p (symbol)
27   (or (null symbol)
28       (and (member symbol *static-symbols*) t)))
29
30 (defun static-symbol-offset (symbol)
31   #!+sb-doc
32   "the byte offset of the static symbol SYMBOL"
33   (if symbol
34       (let ((posn (position symbol *static-symbols*)))
35         (unless posn (error "~S is not a static symbol." symbol))
36         (+ (* posn (pad-data-block symbol-size))
37            (pad-data-block (1- symbol-size))
38            other-pointer-type
39            (- list-pointer-type)))
40       0))
41
42 (defun offset-static-symbol (offset)
43   #!+sb-doc
44   "Given a byte offset, OFFSET, return the appropriate static symbol."
45   (if (zerop offset)
46       nil
47       (multiple-value-bind (n rem)
48           (truncate (+ offset list-pointer-type (- other-pointer-type)
49                        (- (pad-data-block (1- symbol-size))))
50                     (pad-data-block symbol-size))
51         (unless (and (zerop rem) (<= 0 n (1- (length *static-symbols*))))
52           (error "The byte offset ~D is not valid." offset))
53         (elt *static-symbols* n))))
54
55 (defun static-function-offset (name)
56   #!+sb-doc
57   "Return the (byte) offset from NIL to the start of the fdefn object
58    for the static function NAME."
59   (let ((static-syms (length *static-symbols*))
60         (static-function-index (position name *static-functions*)))
61     (unless static-function-index
62       (error "~S isn't a static function." name))
63     (+ (* static-syms (pad-data-block symbol-size))
64        (pad-data-block (1- symbol-size))
65        (- list-pointer-type)
66        (* static-function-index (pad-data-block fdefn-size))
67        (* fdefn-raw-addr-slot word-bytes))))