home *** CD-ROM | disk | FTP | other *** search
- ;;;; disassembler.jl -- Disassembles compiled Lisp functions
- ;;; Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
-
- ;;; This file is part of Jade.
-
- ;;; Jade is free software; you can redistribute it and/or modify it
- ;;; under the terms of the GNU General Public License as published by
- ;;; the Free Software Foundation; either version 2, or (at your option)
- ;;; any later version.
-
- ;;; Jade is distributed in the hope that it will be useful, but
- ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;;; GNU General Public License for more details.
-
- ;;; You should have received a copy of the GNU General Public License
- ;;; along with Jade; see the file COPYING. If not, write to
- ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- ;; need this for the opcode constants
- (require 'compiler)
- (provide 'disassembler)
-
- (defun disassemble-fun (fun &optional stream)
- "(disassemble-fun FUN [STREAM])
- Disassembles the lisp-code form which is the function value of FUN. If
- STREAM is given prints to that stream."
- (when (symbolp fun)
- (setq fun (symbol-function fun)))
- (if (eq (car fun) 'macro)
- (setq fun (nthcdr 3 fun))
- (setq fun (nthcdr 2 fun)))
- (disassemble (car (if (or (stringp (car fun)) (numberp (car fun))) (cdr fun) fun)) stream))
-
- ;; Disassembles the LISP-CODE form, output goes to STREAM
- (defun disassemble (lisp-code &optional stream)
- (let
- ((code-string (nth 1 lisp-code))
- (consts (nth 2 lisp-code))
- (i 0)
- (screen-tab 24)
- c arg op)
- (unless stream
- (setq stream standard-output))
- (while (setq c (aref code-string i))
- (format stream "\n%d:\t" i)
- (cond
- ((< c op-last-with-args)
- (setq op (bit-and c 0xf8))
- (cond
- ((< (bit-and c 0x07) 6)
- (setq arg (bit-and c 0x07)))
- ((= (bit-and c 0x07) 6)
- (setq
- i (1+ i)
- arg (bit-and (aref code-string i) 0x7f)))
- (t
- (setq
- arg (bit-or (lsh (bit-and (aref code-string (1+ i)) 0x7f) 7)
- (bit-and (aref code-string (+ i 2)) 0x7f))
- i (+ i 2))))
- (cond
- ((= op op-call)
- (format stream "call\t#%d" arg))
- ((= op op-push)
- (let
- ((argobj (aref consts arg)))
- (if (and (consp argobj) (eq (car argobj) 'lisp-code))
- (progn
- (format stream "push\t[%d] %S\n<lisp-code" arg argobj)
- (disassemble argobj stream)
- (write stream "\n>"))
- (format stream "push\t[%d] %S" arg (aref consts arg)))))
- ((= op op-vrefc)
- (format stream "vrefc\t[%d] %S" arg (aref consts arg)))
- ((= op op-vsetc)
- (format stream "vsetc\t[%d] %S" arg (aref consts arg)))
- ((= op op-list)
- (format stream "list\t#%d" arg))
- ((= op op-bind)
- (format stream "bind\t[%d] %S" arg (aref consts arg)))))
- ((> c op-last-before-jmps)
- (setq
- arg (bit-or (lsh (bit-and (aref code-string (1+ i)) 0x7f) 7)
- (bit-and (aref code-string (+ i 2)) 0x7f))
- op c
- i (+ i 2))
- (format stream
- (cond
- ((= op op-jmp)
- "jmp\t%d")
- ((= op op-jmp-nil)
- "jmp-nil\t%d")
- ((= op op-jmp-t)
- "jmp-t\t%d")
- ((= op op-jmp-nil-else-pop)
- "jmp-nil-else-pop\t%d")
- ((= op op-jmp-t-else-pop)
- "jmp-t-else-pop\t%d"))
- arg))
- (t
- (setq op c)
- (write stream
- (cond
- ((= op op-pop)
- "pop")
- ((= op op-vref)
- "vref")
- ((= op op-vset)
- "vset")
- ((= op op-fref)
- "fref")
- ((= op op-fset)
- "fset")
- ((= op op-init-bind)
- "init-bind")
- ((= op op-unbind)
- "unbind")
- ((= op op-dup)
- "dup")
- ((= op op-swap)
- "swap")
- ((= op op-nil)
- "nil")
- ((= op op-t)
- "t")
- ((= op op-cons)
- "cons")
- ((= op op-car)
- "car")
- ((= op op-cdr)
- "cdr")
- ((= op op-rplaca)
- "rplaca")
- ((= op op-rplacd)
- "rplacd")
- ((= op op-nth)
- "nth")
- ((= op op-nthcdr)
- "nthcdr")
- ((= op op-aset)
- "aset")
- ((= op op-aref)
- "aref")
- ((= op op-length)
- "length")
- ((= op op-eval)
- "eval")
- ((= op op-plus-2)
- "plus-2")
- ((= op op-negate)
- "negate")
- ((= op op-minus-2)
- "minus-2")
- ((= op op-product-2)
- "product-2")
- ((= op op-divide-2)
- "divide-2")
- ((= op op-mod-2)
- "mod-2")
- ((= op op-bit-not)
- "bit-not")
- ((= op op-not)
- "not")
- ((= op op-bit-or-2)
- "bit-or-2")
- ((= op op-bit-and-2)
- "bit-and-2")
- ((= op op-equal)
- "equal")
- ((= op op-eq)
- "eq")
- ((= op op-num-eq)
- "num-eq")
- ((= op op-num-noteq)
- "num-noteq")
- ((= op op-gtthan)
- "gtthan")
- ((= op op-gethan)
- "gethan")
- ((= op op-ltthan)
- "ltthan")
- ((= op op-lethan)
- "lethan")
- ((= op op-inc)
- "inc")
- ((= op op-dec)
- "dec")
- ((= op op-lsh)
- "lsh")
- ((= op op-zerop)
- "zerop")
- ((= op op-null)
- "null")
- ((= op op-atom)
- "atom")
- ((= op op-consp)
- "consp")
- ((= op op-listp)
- "listp")
- ((= op op-numberp)
- "numberp")
- ((= op op-stringp)
- "stringp")
- ((= op op-vectorp)
- "vectorp")
- ((= op op-catch-kludge)
- "catch-kludge")
- ((= op op-throw)
- "throw")
- ((= op op-unwind-pro)
- "unwind-pro")
- ((= op op-un-unwind-pro)
- "un-unwind-pro")
- ((= op op-fboundp)
- "fboundp")
- ((= op op-boundp)
- "boundp")
- ((= op op-symbolp)
- "symbolp")
- ((= op op-get)
- "get")
- ((= op op-put)
- "put")
- ((= op op-error-pro)
- "error-pro")
- ((= op op-signal)
- "signal")
- ((= op op-set-current-buffer)
- "set-current-buffer")
- ((= op op-swap-buffer)
- "swap-buffer")
- ((= op op-current-buffer)
- "current-buffer")
- ((= op op-bufferp)
- "bufferp")
- ((= op op-mark-p)
- "mark-p")
- ((= op op-windowp)
- "windowp")
- ((= op op-swap-window)
- "swap-window")
- (t
- (format-string "<unknown %d>" op))))))
- (setq i (1+ i)))
- t))
-