home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / src-16f.lha / code / debug-info.lisp < prev    next >
Encoding:
Text File  |  1992-07-28  |  18.6 KB  |  490 lines

  1. ;;; -*- Log: code.log; Package: C -*-
  2. ;;;
  3. ;;; **********************************************************************
  4. ;;; This code was written as part of the CMU Common Lisp project at
  5. ;;; Carnegie Mellon University, and has been placed in the public domain.
  6. ;;; If you want to use this code or any part of CMU Common Lisp, please contact
  7. ;;; Scott Fahlman or slisp-group@cs.cmu.edu.
  8. ;;;
  9. (ext:file-comment
  10.   "$Header: debug-info.lisp,v 1.24 92/05/21 22:50:15 wlott Locked $")
  11. ;;;
  12. ;;; **********************************************************************
  13. ;;;
  14. ;;;    This file contains structures used for recording debugger information.
  15. ;;;
  16. (in-package "C")
  17.  
  18. (export '(make-sc-offset sc-offset-scn sc-offset-offset
  19.       read-var-integer write-var-integer
  20.       read-var-string write-var-string
  21.       read-packed-bit-vector write-packed-bit-vector))
  22.  
  23.  
  24. ;;;; SC-Offsets:
  25. ;;;
  26. ;;;    We represent the place where some value is stored with a SC-OFFSET,
  27. ;;; which is the SC number and offset encoded as an integer.
  28.  
  29. (defconstant sc-offset-scn-byte (byte 5 0))
  30. (defconstant sc-offset-offset-byte (byte 22 5))
  31. (deftype sc-offset () '(unsigned-byte 27))
  32.  
  33. (defmacro make-sc-offset (scn offset)
  34.   `(dpb ,scn sc-offset-scn-byte
  35.     (dpb ,offset sc-offset-offset-byte 0)))
  36.  
  37. (defmacro sc-offset-scn (sco) `(ldb sc-offset-scn-byte ,sco))
  38. (defmacro sc-offset-offset (sco) `(ldb sc-offset-offset-byte ,sco))
  39.  
  40.  
  41. ;;;; Variable length integers:
  42. ;;;
  43. ;;;    The debug info representation makes extensive use of integers encoded in
  44. ;;; a byte vector using a variable number of bytes:
  45. ;;;    0..253 => the integer
  46. ;;;    254 => read next two bytes for integer
  47. ;;;    255 => read next four bytes for integer
  48.  
  49. ;;; READ-VAR-INTEGER  --  Interface
  50. ;;;
  51. ;;;    Given a byte vector Vec and an index variable Index, read a variable
  52. ;;; length integer and advance index.
  53. ;;;
  54. (defmacro read-var-integer (vec index)
  55.   (once-only ((val `(aref ,vec ,index)))
  56.     `(cond ((<= ,val 253)
  57.         (incf ,index)
  58.         ,val)
  59.        ((= ,val 254)
  60.         (prog1
  61.         (logior (aref ,vec (+ ,index 1))
  62.             (ash (aref ,vec (+ ,index 2)) 8))
  63.           (incf ,index 3)))
  64.        (t
  65.         (prog1
  66.         (logior (aref ,vec (+ ,index 1))
  67.             (ash (aref ,vec (+ ,index 2)) 8)
  68.                   (ash (aref ,vec (+ ,index 3)) 16)
  69.                   (ash (aref ,vec (+ ,index 4)) 24))
  70.           (incf ,index 5))))))
  71.  
  72.  
  73. ;;; WRITE-VAR-INTEGER  --  Interface
  74. ;;;
  75. ;;;    Takes an adjustable vector Vec with a fill pointer and pushes the
  76. ;;; variable length representation of Int on the end.
  77. ;;;
  78. (defun write-var-integer (int vec)
  79.   (declare (type (unsigned-byte 32) int))
  80.   (cond ((<= int 253)
  81.      (vector-push-extend int vec))
  82.     (t
  83.      (let ((32-p (> int #xFFFF)))
  84.        (vector-push-extend (if 32-p 255 254) vec)
  85.        (vector-push-extend (ldb (byte 8 0) int) vec)
  86.        (vector-push-extend (ldb (byte 8 8) int) vec)
  87.        (when 32-p
  88.          (vector-push-extend (ldb (byte 8 16) int) vec)
  89.          (vector-push-extend (ldb (byte 8 24) int) vec)))))
  90.   (undefined-value))
  91.  
  92.  
  93.  
  94. ;;;; Packed strings:
  95. ;;;
  96. ;;;    A packed string is a variable length integer length followed by the
  97. ;;; character codes.
  98.  
  99.  
  100. ;;; READ-VAR-STRING  --  Interface
  101. ;;;
  102. ;;;    Read a packed string from Vec starting at Index, leaving advancing
  103. ;;; Index.
  104. ;;;
  105. (defmacro read-var-string (vec index)
  106.   (once-only ((len `(read-var-integer ,vec ,index)))
  107.     (once-only ((res `(make-string ,len)))
  108.       `(progn
  109.      (%primitive byte-blt ,vec ,index ,res 0 ,len)
  110.      (incf ,index ,len)
  111.      ,res))))
  112.  
  113.  
  114. ;;; WRITE-VAR-STRING  --  Interface
  115. ;;;
  116. ;;;    Write String into Vec (adjustable, fill-pointer) represented as the
  117. ;;; length (in a var-length integer) followed by the codes of the characters.
  118. ;;;
  119. (defun write-var-string (string vec)
  120.   (declare (simple-string string))
  121.   (let ((len (length string)))
  122.     (write-var-integer len vec)
  123.     (dotimes (i len)
  124.       (vector-push-extend (char-code (schar string i)) vec)))
  125.   (undefined-value))
  126.  
  127.  
  128. ;;;; Packed bit vectors:
  129. ;;;
  130.  
  131. ;;; READ-PACKED-BIT-VECTOR  --  Interface
  132. ;;;
  133. ;;;    Read the specified number of Bytes out of Vec at Index and convert them
  134. ;;; to a bit-vector.  Index is incremented.
  135. ;;;
  136. (defmacro read-packed-bit-vector (bytes vec index)
  137.   (once-only ((n-bytes bytes))
  138.     (once-only ((n-res `(make-array (* ,n-bytes 8) :element-type 'bit)))
  139.       `(progn
  140.      (%primitive byte-blt ,vec ,index ,n-res 0 ,n-bytes)
  141.      (incf ,index ,n-bytes)
  142.      ,n-res))))
  143.  
  144.  
  145. ;;; WRITE-PACKED-BIT-VECTOR  --  Interface
  146. ;;;
  147. ;;;    Write Bits out to Vec.  Bits must be an eight-bit multiple.
  148. ;;;
  149. (defun write-packed-bit-vector (bits vec)
  150.   (declare (type simple-bit-vector bits))
  151.   (let ((len (length bits))
  152.     (start (fill-pointer vec)))
  153.     (cond ((eq (backend-byte-order *backend*)
  154.            (backend-byte-order *native-backend*))
  155.        (let ((bytes (ash len -3)))
  156.          (dotimes (i bytes)
  157.            (vector-push-extend 0 vec))
  158.          (lisp::with-array-data ((data vec) (ig1) (ig2))
  159.            (declare (ignore ig1 ig2))
  160.            (%primitive byte-blt bits 0 data start (+ start bytes)))))
  161.       (t
  162.        (macrolet ((frob (initial step done)
  163.             `(let ((shift ,initial)
  164.                    (byte 0))
  165.                (dotimes (i len)
  166.                  (let ((int (aref bits i)))
  167.                    (setq byte (logior byte (ash int shift)))
  168.                    (,step shift))
  169.                  (when ,done
  170.                    (vector-push-extend byte vec)
  171.                    (setq shift ,initial  byte 0)))
  172.                (unless (= shift ,initial)
  173.                  (vector-push-extend byte vec)))))
  174.          (ecase (backend-byte-order *backend*)
  175.            (:little-endian
  176.         (frob 0 incf (= shift 8)))
  177.            (:big-endian
  178.         (frob 7 decf (minusp shift))))))))
  179.   
  180.   (undefined-value))
  181.  
  182.  
  183. ;;;; Compiled debug variables:
  184. ;;;
  185. ;;;    Compiled debug variables are in a packed binary representation in the
  186. ;;; DEBUG-FUNCTION-VARIABLES:
  187. ;;;    single byte of boolean flags:
  188. ;;;        uninterned name
  189. ;;;       packaged name
  190. ;;;        environment-live
  191. ;;;        has distinct save location
  192. ;;;        has ID (name not unique in this fun)
  193. ;;;        minimal debug-info argument (name generated as ARG-0, ...)
  194. ;;;        deleted: placeholder for unused minimal argument
  195. ;;;    [name length in bytes (as var-length integer), if not minimal]
  196. ;;;    [...name bytes..., if not minimal]
  197. ;;;    [if packaged, var-length integer that is package name length]
  198. ;;;     ...package name bytes...]
  199. ;;;    [If has ID, ID as var-length integer]
  200. ;;;    SC-Offset of primary location (as var-length integer)
  201. ;;;    [If has save SC, SC-Offset of save location (as var-length integer)]
  202.  
  203. (defconstant compiled-debug-variable-uninterned        #b00000001)
  204. (defconstant compiled-debug-variable-packaged        #b00000010)
  205. (defconstant compiled-debug-variable-environment-live    #b00000100)
  206. (defconstant compiled-debug-variable-save-loc-p        #b00001000)
  207. (defconstant compiled-debug-variable-id-p        #b00010000)
  208. (defconstant compiled-debug-variable-minimal-p        #b00100000)
  209. (defconstant compiled-debug-variable-deleted-p        #b01000000)
  210.  
  211.  
  212. ;;;; Compiled debug blocks:
  213. ;;;
  214. ;;;    Compiled debug blocks are in a packed binary representation in the
  215. ;;; DEBUG-FUNCTION-BLOCKS:
  216. ;;;    number of successors + bit flags (single byte)
  217. ;;;        elsewhere-p
  218. ;;;    ...ordinal number of each successor in the function's blocks vector...
  219. ;;;    number of locations in this block
  220. ;;;    kind of first location (single byte)
  221. ;;;    delta from previous PC (or from 0 if first location in function.)
  222. ;;;    [offset of first top-level form, if no function TLF-NUMBER]
  223. ;;;    form number of first source form
  224. ;;;    first live mask (length in bytes determined by number of VARIABLES)
  225. ;;;    ...more <kind, delta, top-level form offset, form-number, live-set>
  226. ;;;       tuples...
  227.  
  228.  
  229. (defconstant compiled-debug-block-nsucc-byte (byte 2 0))
  230. (defconstant compiled-debug-block-elsewhere-p #b00000100)
  231.  
  232. (defconstant compiled-code-location-kind-byte (byte 3 0))
  233. (defconstant compiled-code-location-kinds
  234.   '#(:unknown-return :known-return :internal-error :non-local-exit
  235.      :block-start :call-site :single-value-return :non-local-entry))
  236.  
  237.  
  238.  
  239. ;;;; Debug function:
  240.  
  241. (defstruct debug-function)
  242.  
  243. (defstruct (compiled-debug-function (:include debug-function))
  244.   ;;
  245.   ;; The name of this function.  If from a DEFUN, etc., then this is the
  246.   ;; function name, otherwise it is a descriptive string.
  247.   (name (required-argument) :type (or simple-string cons symbol))
  248.   ;;
  249.   ;; The kind of function (same as FUNCTIONAL-KIND):
  250.   (kind nil :type (member nil :optional :external :top-level :cleanup))
  251.   ;;
  252.   ;; A vector of the packed binary representation of variable locations in this
  253.   ;; function.  These are in alphabetical order by name.  This ordering is used
  254.   ;; in lifetime info to refer to variables: the first entry is 0, the second
  255.   ;; entry is 1, etc.  Variable numbers are *not* the byte index at which the
  256.   ;; representation of the location starts.  This slot may be NIL to save
  257.   ;; space.
  258.   (variables nil :type (or (simple-array (unsigned-byte 8) (*)) null))
  259.   ;;
  260.   ;; A vector of the packed binary representation of the COMPILED-DEBUG-BLOCKS
  261.   ;; in this function, in the order that the blocks were emitted.  The first
  262.   ;; block is the start of the function.  This slot may be NIL to save space.
  263.   (blocks nil :type (or (simple-array (unsigned-byte 8) (*)) null))
  264.   ;;
  265.   ;; If all code locations in this function are in the same top-level form,
  266.   ;; then this is the number of that form, otherwise NIL.  If NIL, then each
  267.   ;; code location represented in the BLOCKS specifies the TLF number.
  268.   (tlf-number nil :type (or index null))
  269.   ;;
  270.   ;; A vector describing the variables that the argument values are stored in
  271.   ;; within this function.  The locations are represented by the ordinal number
  272.   ;; of the entry in the VARIABLES.  The locations are in the order that the
  273.   ;; arguments are actually passed in, but special marker symbols can be
  274.   ;; interspersed to indicate the orignal call syntax:
  275.   ;;
  276.   ;; DELETED
  277.   ;;    There was an argument to the function in this position, but it was
  278.   ;;    deleted due to lack of references.  The value cannot be recovered.
  279.   ;;
  280.   ;; SUPPLIED-P
  281.   ;;    The following location is the supplied-p value for the preceding
  282.   ;;    keyword or optional.
  283.   ;;
  284.   ;; OPTIONAL-ARGS
  285.   ;;    Indicates that following unqualified args are optionals, not required.
  286.   ;;
  287.   ;; REST-ARG
  288.   ;;    The following location holds the list of rest args.
  289.   ;;
  290.   ;; MORE-ARG
  291.   ;;    The following two locations are the more arg context and count.
  292.   ;;
  293.   ;; <any other symbol>
  294.   ;;    The following location is the value of the keyword argument with the
  295.   ;;    specified name.
  296.   ;;
  297.   ;; This may be NIL to save space.  If no symbols are present, then this will
  298.   ;; be represented with an I-vector with sufficiently large element type.  If
  299.   ;; this is :MINIMAL, then this means that the VARIABLES are all required
  300.   ;; arguments, and are in the order they appear in the VARIABLES vector.  In
  301.   ;; other words, :MINIMAL stands in for a vector where every element holds its
  302.   ;; index.
  303.   (arguments nil :type (or (simple-array * (*)) (member :minimal nil)))
  304.   ;;
  305.   ;; There are three alternatives for this slot:
  306.   ;; 
  307.   ;; A vector
  308.   ;;    A vector of SC-OFFSETS describing the return locations.  The
  309.   ;;    vector element type is chosen to hold the largest element.
  310.   ;;
  311.   ;; :Standard 
  312.   ;;    The function returns using the standard unknown-values convention.
  313.   ;;
  314.   ;; :Fixed
  315.   ;;    The function returns using the a fixed-values convention, but we
  316.   ;;    elected not to store a vector to save space.
  317.   (returns :fixed :type (or (simple-array * (*)) (member :standard :fixed)))
  318.   ;;
  319.   ;; SC-Offsets describing where the return PC and return FP are kept.
  320.   (return-pc (required-argument) :type sc-offset)
  321.   (old-fp (required-argument) :type sc-offset)
  322.   ;;
  323.   ;; SC-Offset for the number stack FP in this function, or NIL if no NFP
  324.   ;; allocated.
  325.   (nfp nil :type (or sc-offset null))
  326.   ;;
  327.   ;; The earliest PC in this function at which the environment is properly
  328.   ;; initialized (arguments moved from passing locations, etc.)
  329.   (start-pc (required-argument) :type index)
  330.   ;;
  331.   ;; The start of elsewhere code for this function (if any.)
  332.   (elsewhere-pc (required-argument) :type index))
  333.  
  334.  
  335. ;;;; Minimal debug function:
  336.  
  337. ;;; The minimal debug info format compactly represents debug-info for some
  338. ;;; cases where the other debug info (variables, blocks) is small enough so
  339. ;;; that the per-function overhead becomes relatively large.  The minimal
  340. ;;; debug-info format can represent any function at level 0, and any fixed-arg
  341. ;;; function at level 1.
  342. ;;;
  343. ;;; In the minimal format, the debug functions and function map are packed into
  344. ;;; a single byte-vector which is placed in the
  345. ;;; COMPILED-DEBUG-INFO-FUNCTION-MAP.  Because of this, all functions in a
  346. ;;; component must be representable in minimal format for any function to
  347. ;;; actually be dumped in minimal format.  The vector is a sequence of records
  348. ;;; in this format:
  349. ;;;    name representation + kind + return convention (single byte)
  350. ;;;    bit flags (single byte)
  351. ;;;        setf, nfp, variables
  352. ;;;    [package name length (as var-length int), if name is packaged]
  353. ;;;    [...package name bytes, if name is packaged]
  354. ;;;    [name length (as var-length int), if there is a name]
  355. ;;;    [...name bytes, if there is a name]
  356. ;;;    [variables length (as var-length int), if variables flag]
  357. ;;;    [...bytes holding variable descriptions]
  358. ;;;        If variables are dumped (level 1), then the variables are all
  359. ;;;        arguments (in order) with the minimal-arg bit set.
  360. ;;;    [If returns is specified, then the number of return values]
  361. ;;;    [...sequence of var-length ints holding sc-offsets of the return
  362. ;;;        value locations, if fixed return values are specified.]
  363. ;;;    return-pc location sc-offset (as var-length int)
  364. ;;;    old-fp location sc-offset (as var-length int)
  365. ;;;    [nfp location sc-offset (as var-length int), if nfp flag]
  366. ;;;    code-start-pc (as a var-length int)
  367. ;;;        This field implicitly encodes start of this function's code in the
  368. ;;;        function map, as a delta from the previous function's code start.
  369. ;;;        If the first function in the component, then this is the delta from
  370. ;;;        0 (i.e. the absolute offset.)
  371. ;;;    start-pc (as a var-length int)
  372. ;;;        This encodes the environment start PC as an offset from the
  373. ;;;        code-start PC.
  374. ;;;    elsewhere-pc
  375. ;;;        This encodes the elsewhere code start for this function, as a delta
  376. ;;;        from the previous function's elsewhere code start. (i.e. the
  377. ;;;        encoding is the same as for code-start-pc.)
  378. ;;;    
  379. ;;;    
  380.  
  381. #|
  382. ### For functions with XEPs, name could be represented more simply and
  383. compactly as some sort of info about with how to find the function-entry that
  384. this is a function for.  Actually, you really hardly need any info.  You can
  385. just chain through the functions in the component until you find the right one.
  386. Well, I guess you need to at least know which function is an XEP for the real
  387. function (which would be useful info anyway).
  388. |#
  389.  
  390. ;;; Following are definitions of bit-fields in the first byte of the minimal
  391. ;;; debug function:
  392. ;;;
  393. (defconstant minimal-debug-function-name-symbol 0)
  394. (defconstant minimal-debug-function-name-packaged 1)
  395. (defconstant minimal-debug-function-name-uninterned 2)
  396. (defconstant minimal-debug-function-name-component 3)
  397. (defconstant minimal-debug-function-name-style-byte (byte 2 0))
  398. (defconstant minimal-debug-function-kind-byte (byte 3 2))
  399. (defconstant minimal-debug-function-kinds
  400.   '#(nil :optional :external :top-level :cleanup))
  401. (defconstant minimal-debug-function-returns-standard 0)
  402. (defconstant minimal-debug-function-returns-specified 1)
  403. (defconstant minimal-debug-function-returns-fixed 2)
  404. (defconstant minimal-debug-function-returns-byte (byte 2 5))
  405.  
  406. ;;; The following are bit-flags in the second byte of the minimal debug
  407. ;;; function:
  408.  
  409. ;;; If true, wrap (SETF ...) around the name.
  410. (defconstant minimal-debug-function-setf-bit (ash 1 0))
  411.  
  412. ;;; If true, there is a NFP.
  413. (defconstant minimal-debug-function-nfp-bit (ash 1 1))
  414.  
  415. ;;; If true, variables (hence arguments) have been dumped.
  416. (defconstant minimal-debug-function-variables-bit (ash 1 2))
  417.  
  418.  
  419. ;;;; Debug source:
  420.  
  421. (defstruct debug-source
  422.   ;;
  423.   ;; This slot indicates where the definition came from:
  424.   ;;    :File - from a file (Compile-File)
  425.   ;;    :Lisp - from Lisp (Compile)
  426.   ;;  :Stream - from a non-file stream (Compile-From-Stream)
  427.   (from (required-argument) :type (member :file :stream :lisp))
  428.   ;;
  429.   ;; If :File, the file name, if :Lisp or :Stream, then a vector of the
  430.   ;; top-level forms.  When from COMPILE, form 0 is #'(LAMBDA ...).
  431.   (name nil)
  432.   ;;
  433.   ;; File comment for this file, if any.
  434.   (comment nil :type (or simple-string null))
  435.   ;;
  436.   ;; The universal time that the source was written, or NIL if unavailable.
  437.   (created nil :type (or unsigned-byte null))
  438.   ;;
  439.   ;; The universal time that the source was compiled.
  440.   (compiled (required-argument) :type unsigned-byte)
  441.   ;;
  442.   ;; The source path root number of the first form read from this source (i.e.
  443.   ;; the total number of forms converted previously in this compilation.)
  444.   (source-root 0 :type index)
  445.   ;;
  446.   ;; The file-positions of each truly top-level form read from this file (if
  447.   ;; applicable).  The vector element type will be chosen to hold the largest
  448.   ;; element.  May be null to save space.
  449.   (start-positions nil :type (or (simple-array * (*)) null))
  450.   ;;
  451.   ;; If from :LISP, this is the function whose source is form 0.
  452.   ;; If from :STREAM, this is whatever was the :SOURCE-INFO argument to
  453.   ;; COMPILE-FROM-STREAM.
  454.   (info nil))
  455.  
  456.  
  457. ;;;; The DEBUG-INFO structure:
  458.  
  459. (defstruct debug-info)
  460.  
  461. (defstruct (compiled-debug-info (:include debug-info))
  462.   ;;
  463.   ;; Some string describing something about the code in this component.
  464.   (name (required-argument) :type simple-string)
  465.   ;;
  466.   ;; A list of DEBUG-SOURCE structures describing where the code for this
  467.   ;; component came from, in the order that they were read.
  468.   ;;
  469.   ;; *** NOTE: the offset of this slot is wired into the fasl dumper so that it
  470.   ;; *** can backpatch the source info when compilation is complete.
  471.   (source nil :type list)
  472.   ;;
  473.   ;; The name of the package that DEBUG-FUNCTION-VARIABLES were dumped relative
  474.   ;; to.  Locations that aren't packaged are in this package.
  475.   (package (required-argument) :type simple-string)
  476.   ;;
  477.   ;; Either a simple-vector or a byte-vector holding the debug functions for
  478.   ;; this component.  This is used to map PCs to functions, so that we can
  479.   ;; figure out what function we were running in.  If a byte-vector, then it is
  480.   ;; a sequence of minimal debug functions in a packed binary representation.
  481.   ;;
  482.   ;; If a simple-vector, then it alternates Debug-Function structures and
  483.   ;; fixnum PCs.  The function is valid between the PC before it (inclusive)
  484.   ;; and the PC after it (exclusive).  The PCs are in sorted order, so we can
  485.   ;; binary-search.  We omit the first and last PC, since their values are 0
  486.   ;; and the length of the code vector.  Null only temporarily.
  487.   (function-map nil :type (or simple-vector
  488.                   (simple-array (unsigned-byte 8) (*))
  489.                   null)))
  490.