by Jtsummers · 4y ago · 0 comments
I think it is easier to say forth is closer to compiled. I can kind of imagine each user defined word being translated into assembly. With lisp I am speechles.
As to your speechlessness regarding compiled Lisp, here's a quick example:
CL-USER> (defun foo (n)
(+ 1 n))
FOO
CL-USER> (disassemble #'foo)
; disassembly for FOO
; Size: 35 bytes. Origin: #x53641724 ; FOO
; 24: 498B4510 MOV RAX, [R13+16] ; thread.binding-stack-pointer
; 28: 488945F8 MOV [RBP-8], RAX
; 2C: BF02000000 MOV EDI, 2
; 31: 488BD3 MOV RDX, RBX
; 34: FF14250001A052 CALL QWORD PTR [#x52A00100] ; SB-VM::GENERIC-+
; 3B: 488B5DF0 MOV RBX, [RBP-16]
; 3F: 488BE5 MOV RSP, RBP
; 42: F8 CLC
; 43: 5D POP RBP
; 44: C3 RET
; 45: CC10 INT3 16 ; Invalid argument count trap
NIL
CL-USER>
(SBCL was used for the above)"The logic inside that helps dispatch based on the dynamic types at runtime" is the interpreter part IMHO. Plus you need logic to add the metaprogramming elements that require you to change the code after it has been written.
We need to generate an example of something we can't do in C, something which requires evaluation at runtime.
The use of the macro gets expanded at compile time and the expanded code then gets compiled.
Lisp macros are designed in such a way that they are be expanded before runtime.
> evaluation at runtime
Evaluation at runtime does not mean the code gets not compiled.
The SBCL implementation of Common Lisp:
* (let ((fn-code (quote (lambda (a)
(* a 42))))) ;source of function as a list -> FN-CODE
(let ((fn (eval fn-code))) ; evaluation of that list FN-CODE ->eval-> FN
(prog1
(funcall fn 11)
(disassemble fn))))
; disassembly for (LAMBDA (A))
; Size: 36 bytes. Origin: #x700568E8E4 ; (LAMBDA (A))
; 8E4: AA0A40F9 LDR R0, [THREAD, #16] ; binding-stack-pointer
; 8E8: 4A0B00F9 STR R0, [CFP, #16]
; 8EC: EA030CAA MOV R0, R2
; 8F0: 8B0A80D2 MOVZ R1, #84
; 8F4: 3C9880D2 MOVZ TMP, #1217
; 8F8: BE6B7CF8 LDR LR, [NULL, TMP] ; SB-KERNEL:TWO-ARG-*
; 8FC: DE130091 ADD LR, LR, #4
; 900: C0031FD6 BR LR
; 904: E00120D4 BRK #15 ; Invalid argument count trap
462
You'll see that EVAL at runtime compiles the code on-the-fly and in-memory to ARM64 machine code.Not sure what you mean by this but perhaps you're referring to how Lisp allows redefining functions on the fly without relinking? That's done by indirect function calls. Every function call in Lisp jumps indirectly through the function's symbol name (if it has one). This incurs a runtime penalty of one extra memory access per function call, but it enables functions to be redefined on the fly without changing any of their callers. Optimization switches exist to get rid of this extra overhead if you need maximum speed in deployed code.
I think you have a fundamental confusion about when and where macros are applied. In a compiled CL implementation, macros are expanded prior to compilation. The code will be exactly the same as if there was no macro involved. If you want to see what that might look like take the function body of this simple function:
(defun bar (filename)
(with-open-file (in filename :element-type '(unsigned-byte 16))
(let ((contents (make-array (file-length in) :element-type '(unsigned-byte 16))))
(read-sequence contents in)
contents)))
And run it through `macroexpand` (`with-open-file` is the macro I'm particularly interested in here): CL-USER> (macroexpand
'(with-open-file (in filename :element-type '(unsigned-byte 16))
(let ((contents (make-array (file-length in) :element-type '(unsigned-byte 16))))
(read-sequence contents in)
contents)))
(LET ((IN (OPEN FILENAME :ELEMENT-TYPE '(UNSIGNED-BYTE 16))) (#:G659 T))
(UNWIND-PROTECT
(MULTIPLE-VALUE-PROG1
(PROGN
(LET ((CONTENTS
(MAKE-ARRAY (FILE-LENGTH IN) :ELEMENT-TYPE
'(UNSIGNED-BYTE 16))))
(READ-SEQUENCE CONTENTS IN)
CONTENTS))
(SETQ #:G659 NIL))
(WHEN IN (CLOSE IN :ABORT #:G659))))
Now take that output and put it in a new function, call it `baz`. Then disassemble both `bar` and `baz` (I've done this and included the output of just one because they have the same instructions):EDIT: Snipped the excessively long code, here's a link:
https://topaz.github.io/paste/#XQAAAQCODwAAAAAAAAAQYOhAaDnr7... (still long, but at least HN will truncate it)
It doesn't really make sense to talk about disassembling a macro as a macro is not, itself, what gets compiled when it's applied, but rather its output gets compiled (in a compiled Lisp).
> "The logic inside that helps dispatch based on the dynamic types at runtime" is the interpreter part IMHO.
Then every compiled program that uses a tagged union is really an interpreted program (which is a statement you could make a strong argument for, perhaps). The code above is still machine code, though, it's not being interpreted. This is part of the distinction between an interpreter and a runtime. Dynamic dispatch can be applied to statically typed languages as well as dynamically typed languages.
> We need to generate an example of something we can't do in C, something which requires evaluation at runtime.
Strictly speaking, there is no such thing as something that can be done in Lisp that cannot be done in C. It'll just take a lot more effort in C to do some things like compiling a function on the fly, returning it, and then applying it later. You'd at least need to link in a compiler and feed it some structure which it can then compile into a function which gets returned, probably, as a function pointer.