Wednesday, July 6, 2016

disassembly at repl?

I realize that "I just discovered something amazing about LISP that other people have known about for 30 years" posts can be kinda irritating, but I'm going to make one anyway.


Apparently in some (all?) dialects of common lisp, one can create a new function at the REPL, ask for its assembly at the REPL, and get the assembly for the function one just typed.


Check this out.


CL-USER> (defun snarf (n) (+ 5 n))
SNARF
CL-USER> (disassemble 'snarf)
; disassembly for SNARF
; Size: 39 bytes. Origin: #x100606317C
; 7C:       498B4C2460       MOV RCX, [R12+96]                ; thread.binding-stack-pointer
                                                              ; no-arg-parsing entry point
; 81:       48894DF8         MOV [RBP-8], RCX
; 85:       BF0A000000       MOV EDI, 10
; 8A:       488BD3           MOV RDX, RBX
; 8D:       41BBD0010020     MOV R11D, 536871376              ; GENERIC-+
; 93:       41FFD3           CALL R11
; 96:       488B5DF0         MOV RBX, [RBP-16]
; 9A:       488BE5           MOV RSP, RBP
; 9D:       F8               CLC
; 9E:       5D               POP RBP
; 9F:       C3               RET
; A0:       0F0B10           BREAK 16                         ; Invalid argument count trap
NIL
CL-USER> (documentation 'disassemble 'function)
"Disassemble the compiled code associated with OBJECT, which can be a
  function, a lambda expression, or a symbol with a function definition. If
  it is not already compiled, the compiler is called to produce something to
  disassemble."
CL-USER> 

2 comments:

  1. Also of note, '+ is a function call. I wonder what one would need to do in order to force that to be an actual add instruction.

    ReplyDelete
  2. I was reminded of this post when I stumbled across http://www.evanmiller.org/why-im-betting-on-julia.html

    Unlike the author of that post, I *do* care about the homoiconicity claims of Julia.

    I have not yet learned it though.

    ReplyDelete