8 ms·
Compiling a Lisp to x86-64: Let expressions
- tekknolagi 6y agoThanks for posting! I'm the author and can answer any questions and receive any constructive criticism.
- anaphor 6y agoI don't have any questions, but I just started reading it a few days ago, and I'm re-implementing it in Nim (which is an interesting experience). One issue I ran into right away was that my code from step 1 was segfaulting, and the issue was that I accidentally called `mprotect` before copying the memory into the buffer. Easy mistake to make, so you might want to add a warning for people who aren't just copy-pasting the code.
- tekknolagi 6y agoExcellent! I'm so happy you're following along in another language. If you're developing in the open, can I link to it somewhere on the series? It's a good point and I'll try and remember to add a notice about order being important later.
- anaphor 6y agoOnce I'm ready I'm planning on throwing it up on github for sure. I'll make a note to send you an email with it :)
- tekknolagi 6y agoAwesome. Looking forward to it.
- emmanueloga_ 6y agoCritique: the TOC is only shown in first post and not clickable, makes it hard to navigate the series. It would also be nice to have the TOC repeated in each post. Question: I saw a chapter about unary functions. In some interpreters let bindings are explained as "sugar", or syntactic extensions on top of function calls. For instance `let` is not present in the core of scheme [1]. So in this way one could transform a let binding from: (let ((a 1) (b 2)) (+ a b)) to: ((((lambda (a) (lambda (b) (+ a b))) 1) 2) through: (define-syntax let (syntax-rules () [(_ ((x e) ...) b1 b2 ...) ((lambda (x ...) b1 b2 ...) e ...)])) ... then you wouldn't need to compile let bindings, just function application, which some people call "desugaring". I was wondering if this kind of desugaring is used in practical lisp compilers or not... without knowing too much about lisp compilers, I imagine one reason to not go this way is that it could potentially generate way more code than compiling higher level concepts like `let` directly. 1: https://scheme.com/tspl4/further.html https://scheme.com/tspl4/further.html
- moonchild 6y agoMost lisp compilers do exactly as you suggest. Generally, lisp is implemented on a small core of maybe 20~30 functions, with everything else as macros. If you're curious about how lisps are implemented, I recommend checking out this series: https://www.youtube.com/watch?v=Wa81OJnlsoI https://www.youtube.com/watch?v=Wa81OJnlsoI
- pfdietz 6y agoThat's really not true of Common Lisp implementations. These have many more than 20-30 functions, and they all have to be there because one can obtain them as values and pass those values around (and, in the case of standard generic functions, add new methods for them). There may be specific expansions for calls to particular standard functions when the types of the arguments are known, or when particular keyword and optional arguments are or are not present.
- moonchild 6y agoWell, of course; many of them will be functions, not macros. What I mean is, there will only be 20~30 builtins; the rest can be implemented 'in userspace', as it were. Though specifically in the case of cl, clos might complicate that somewhat, but probably not a ton.
- timonoko 6y agoMetoo 1982: C:\nokolisp (ncompile (macroexpand '(let ((x 1)) (+ x 1)))) $09CB:$7188: JMP ?? ; see below $09CB:$718B: CALL $0ECD ; CALL ONEARG $09CB:$718E: PUSH [$012C] $09CB:$7192: MOV [$012C],AX $09CB:$7195: JMP ?? ; see below $09CB:$7198: PUSH [STACKMARK] $09CB:$719C: MOV [STACKMARK],SP $09CB:$71A0: MOV AX,[$012C] $09CB:$71A3: CALL $0F1D ; CALL NUMVAL $09CB:$71A6: MOV BX,$01 $09CB:$71A9: ADD AX,BX $09CB:$71AB: CALL $05C9 ; CALL MAKNUM $09CB:$71AE: JMP $2080 $09CB:$7195: JMP $71B1 $09CB:$71B1: CALL $7198 $09CB:$71B4: POP [$012C] $09CB:$71B8: JMP $1DA7 $09CB:$7188: JMP $71BB $09CB:$71BB: MOV AX,$04 ; S-object 1 $09CB:$71BE: CALL $718E $09CB:$71C1: JMP $1DA7 (subru: eval=$7188, compile=$3B6F) One pass, but corrects forward jumps afterwards.
- lioeters 6y agoThanks to the author for an enjoyable intellectual journey, it's a wonderful series of articles. Having tried my hand at following the Make a Lisp project ¹, implementing an interpreter in a couple of languages, it's fascinating to see the process of designing the language and compiler from the ground up, discussing various tradeoffs for simplicity, reasons for internal data structure, etc. In fact, I think I'm learning more about C99 than Lisp - there's something about creating a language (or just following along like me), by necessity it leads to the foundations of computing/programming, the concepts that make up the basis of all languages. It's a perfect (meta)subject for an educational project. ¹ https://github.com/kanaka/mal https://github.com/kanaka/mal
- monadic2 6y agoI can't recommend Lisp In Small Pieces enough. I prefer it to SICP.
- lioeters 6y agoAppreciate the recommendation! I was able to track down the author's page: https://pages.lip6.fr/Christian.Queinnec/WWW/LiSP.html https://pages.lip6.fr/Christian.Queinnec/WWW/LiSP.html ..And someone who updated the book's source code to run on modern Schemes: https://github.com/appleby/Lisp-In-Small-Pieces https://github.com/appleby/Lisp-In-Small-Pieces
- tekknolagi 6y agoI'm so glad you're enjoying them!
- sillysaurusx 6y ago(lets a 1 b 2 (+ a b)) is such a nice macro. Everything but the last expression are bindings.
- TylerE 6y agoWhy is it nicer than (+ 1 2). What is gained from an extra layer of indirection?
- sillysaurusx 6y agoFewer parens. It was just a trivial example to illustrate the macro. (lets (a b) (list 1 2) c (+ a 3) (d e f) (some-func a b c) (* d e f)) It destructures automatically. The equivalent traditional macro might look like (destructuring-bind* [[(a b) (list 1 2)] [c (+ a 3)] [(d e f) (some-func a b c)]] (* d e f)) which is unreadable. And yeah, normally it's indented properly and yada-yada, and people use editor plugins like paredit to make it not such a pain to type, but still. It adds up.
- bjoli 6y agoWhat? The let way of variable bindings is standard just about everywhere. Why replace it with something that doesn't visually distinguish binding pairs? To my lispy eyes it is harder to navigate than (let ((a 1) (b 2)) (+ a b)) and harder to implement as a syntax rules macro :)
- sillysaurusx 6y agoFree yourself from the shackles of parens. Throw off your bindings and run into the streets. It's worth it. Arc uses (let a 1 (+ a 2)), and lets is simply the logical progression of that.
- bjoli 6y agoI have gone the other way. when I first learned macros I spent loads of time trying to make paren-free versions of the standard scheme forms, and now I can't even be assed to do the racketism of surrounding my binding clauses with []. The parens of let lets me easily distinguish the binding clauses, and lets me navigate it a lot easier in Emacs. The lets macro seems like the opposite of that. Every odd thing except the last element is an identifier and every even thing is that identifiers value.
- amelius 6y agoI wish these series on compilers would focus more on the harder parts, like JIT compilation or the virtual machine and concurrent garbage collecting.
- tekknolagi 6y agoYou're more than welcome to your opinion, but I've explicitly outlined that those are non-goals of this series. Maybe maybe I'll get to caching or optimization.
- ngcc_hk 6y agoSeems good and reading. But it should explain or say it will cover the difference to c if c makes a distinction of statement and expression. And why that matter. Not throw a bone into the sky and assume we know it will later become a space station.
- tekknolagi 6y agoGood point! It's sort of a throwaway comment to handwave away linguistic differences. I'll try to make it better when I have access to a computer.