Previous: , Up: Compiled Lisp   [Contents][Index]

5.17.4 Disassembly

It is possible to disassemble byte-code forms; originally this was so I could figure out why the compiler wasn’t working but if you’re curious about how the compiler compiles a form it may be of use to you.

Naturally, the output of the disassembler is a listing in the assembly language of the Librep virtual machine—it won’t take a byte-code form and produce the equivalent Lisp code!

Command: disassemble function #!optional stream

This function disassembles the compile Lisp function function. It writes a listing to the output stream stream (normally the value of the standard-output variable).

When called interactively it will prompt for a function to disassemble. Defined in rep.vm.disassembler.

When reading the output of the disassembler bear in mind that Librep simulates a stack machine for the code to run on. All calculations are performed on the stack, the value left on the stack when the piece of code ends is the value of the byte-code form.

Here is a small example. Consider the fib function given at the start of this section:

(define (fib n)
  (if (<= n 2)
    (+ (fib (- n 1))
       (fib (- n 2)))))

After compilation and disassembly, the following is produced (but without the annotations):

Disassembly of #<closure fib>:

21 bytes, 1 constants, and (5,0,1) stack slots

0    required-arg       ;requires a single parameter
1    dup
2    slot-set #0        ;store it in register 0 (r0)
3    pushi 2
4    le
5    jn 10              ;unless r0 <= 2, goto 10
8    pushi 1
9    return             ;else, return 1
10   refg [0] fib
11   slot-ref #0
12   dec
13   call #1            ;push result of (fib (1- n))
14   refg [0] fib
15   slot-ref #0
16   pushi 2
17   sub
18   call #1            ;push (fib (- n 2))
19   add
20   return             ;return the sum of the two calls

Previous: , Up: Compiled Lisp   [Contents][Index]