37.6. Examining compiled closures

The functions described here are defined in src/compiler.lisp and src/record.d and can be used to examine the internals of a compiled closure.

Warning

These function are internal CLISP functions, their names are not exported, this section is not supposed to be comprehensive and is not guaranteed to be up to date. It is intended for aspiring CLISP hackers who are supposed to graduate to reading the sources right away. All others should stick with the [ANSI CL standard] function DISASSEMBLE.

Closure name. The normal way to extract the name of a closure is FUNCTION-LAMBDA-EXPRESSION:

(defun my-plus-1 (x y) (declare (compile)) (+ x y))
⇒ MY-PLUS-1
(function-lambda-expression #'my-plus-1)
⇒ (LAMBDA (X Y) (DECLARE (COMPILE)) (+ X Y)) ;
⇒ T ;
⇒ MY-PLUS-1
;; works only on closure objects
(sys::closure-name #'my-plus-1)
⇒ MY-PLUS-1

Closure bytecode. The actual bytecode vector (if you modify it, you can get a segfault when the function is executed):

(sys::closure-codevec #'my-plus-1)
⇒ #(0 0 0 0 2 0 0 0 6 3 174 174 51 2 53 25 3)

Closure constants. A closure can depend on external and internal values:

(let ((x 123) (y 456))
  (defun my-plus-2 (z) (declare (compile)) (+ x y z)))
⇒ MY-PLUS-2
(sys::closure-consts #'my-plus-2)
⇒ (#(Y 456 X 123 NIL) 3 1)

Use DISASSEMBLE to see how the constants are used.

Closure signature. Function SYS::SIGNATURE returns 8 values:

  1. required parameter count
  2. optional parameters count
  3. rest-p
  4. key-p
  5. keyword-p
  6. allow-other-keys-p
  7. byte-list (codevec as a LIST)
  8. const-list

Mnemonic bytecodes. One can convert between numeric and mnemonic bytecodes (LAP stands for Lisp Assembly Program):

(multiple-value-bind (req-num opt-num rest-p key-p keyword-list
                      allow-other-keys-p byte-list const-list)
    (sys::signature #'my-plus-1)
  (sys::disassemble-LAP byte-list const-list))
⇒ ((0 LOAD&PUSH 2) (1 LOAD&PUSH 2) (2 CALLSR 2 53) (5 SKIP&RET 3))
(sys::assemble-LAP (mapcar #'rest *))
⇒ (174 174 51 2 53 25 3)


These notes document CLISP version 2.49.93+Last modified: 2018-02-19