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