38fa31cd57452ab83c6140ef2a2fb75c0c7af069
[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 (<= #x-20000000 num #x1fffffff)
18       (ash num 2)
19       (error "~W 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 ;;; the byte offset of the static symbol SYMBOL
28 (defun static-symbol-offset (symbol)
29   (if symbol
30       (let ((posn (position symbol *static-symbols*)))
31         (unless posn (error "~S is not a static symbol." symbol))
32         (+ (* posn (pad-data-block symbol-size))
33            (pad-data-block (1- symbol-size))
34            other-pointer-lowtag
35            (- list-pointer-lowtag)))
36       0))
37
38 ;;; Given a byte offset, OFFSET, return the appropriate static symbol.
39 (defun offset-static-symbol (offset)
40   (if (zerop offset)
41       nil
42       (multiple-value-bind (n rem)
43           (truncate (+ offset list-pointer-lowtag (- other-pointer-lowtag)
44                        (- (pad-data-block (1- symbol-size))))
45                     (pad-data-block symbol-size))
46         (unless (and (zerop rem) (<= 0 n (1- (length *static-symbols*))))
47           (error "The byte offset ~W is not valid." offset))
48         (elt *static-symbols* n))))
49
50 ;;; Return the (byte) offset from NIL to the start of the fdefn object
51 ;;; for the static function NAME.
52 (defun static-fun-offset (name)
53   (let ((static-syms (length *static-symbols*))
54         (static-fun-index (position name *static-funs*)))
55     (unless static-fun-index
56       (error "~S isn't a static function." name))
57     (+ (* static-syms (pad-data-block symbol-size))
58        (pad-data-block (1- symbol-size))
59        (- list-pointer-lowtag)
60        (* static-fun-index (pad-data-block fdefn-size))
61        (* fdefn-raw-addr-slot n-word-bytes))))
62 \f
63 ;;; Various error-code generating helpers
64 (defvar *adjustable-vectors* nil)
65
66 (defmacro with-adjustable-vector ((var) &rest body)
67   `(let ((,var (or (pop *adjustable-vectors*)
68                    (make-array 16
69                                :element-type '(unsigned-byte 8)
70                                :fill-pointer 0
71                                :adjustable t))))
72      (declare (type (vector (unsigned-byte 8) 16) ,var))
73      (setf (fill-pointer ,var) 0)
74      (unwind-protect
75          (progn
76            ,@body)
77        (push ,var *adjustable-vectors*))))