4 ms·
Compiler architecture is also a good metaphor for understanding what Common Lisp is all about. Common Lisp = the programmable compiler & runtime all in one. Mo
by nabla9 8y ago
Compiler architecture is also a good metaphor for understanding what Common Lisp is all about.
Common Lisp = the programmable compiler & runtime all in one. Most CL implementations compile before calling eval. Read–eval–print loop is actually parse-compile-execute-print loop.
* (defun foo (x) (+ x 1))
FOO
* (disassemble 'foo)
; disassembly for FOO
; Size: 38 bytes. Origin: #x1003A7DD1C
; 1C: 498B4C2460 MOV RCX, [R12+96] ; thread.binding-stack-pointer
; no-arg-parsing entry point
; 21: 48894DF8 MOV [RBP-8], RCX
; 25: BF02000000 MOV EDI, 2
; 2A: 488BD3 MOV RDX, RBX
; 2D: 41BBC0010020 MOV R11D, 536871360 ; GENERIC-+
; 33: 41FFD3 CALL R11
; 36: 488B5DF0 MOV RBX, [RBP-16]
; 3A: 488BE5 MOV RSP, RBP
; 3D: F8 CLC
; 3E: 5D POP RBP
; 3F: C3 RET
; 40: CC10 BREAK 16 ; Invalid argument count trap
NIL
Front end = Read function does the lexical analysis. CL syntax is almost one-to-one with the AST, so lexer-parser part is simple. You can reprogram the lexer with reader macros if you want. This is the last place where CL code is text. After it's reader is done with it, the code is represented with CL data structures.
Middle End = CL has multiple passes with AST. First normal macros, then compiler macros operate here. They deal with CL data. Lists, symbols, structs, objects, arrays, numbers, etc. CL packages are symbol tables. AST and IR are all CL data structures.
Back End = finally the function 'compile' function compiles the end result into into native code. Saving the image and exiting the runtime is like compiling in normal languages.
- lispm 8y agoCompiling will not change READ into PARSE. READ is still there. > Most CL implementations compile before calling eval Many or a lot, but I'm not sure actually most. There are quite a lot which use an Interpreter in the REPL, but where one can compile on demand. SBCL by default compiles. LispWorks for example does not. > After it's reader is done with it, the code is represented with CL data structures. Right, but the data structures carry no syntactic information. If you write (1 2 +) the reader accepts it and does not know that this is invalid syntax. > AST and IR are all CL data structures. True, but not necessarily like s-expressions... See for example data structures the SBCL compiler uses: https://github.com/sbcl/sbcl/blob/master/src/compiler/node.lisp https://github.com/sbcl/sbcl/blob/master/src/compiler/node.l... > Saving the image and exiting the runtime is like compiling in normal languages. Working with images is optional - the Common Lisp standard says nothing about images at all.