[HN Gopher] Write your own retro compiler
       ___________________________________________________________________
        
       Write your own retro compiler
        
       Author : nils-m-holm
       Score  : 229 points
       Date   : 2023-12-10 14:18 UTC (8 hours ago)
        
 (HTM) web link (t3x.org)
 (TXT) w3m dump (t3x.org)
        
       | thinkmassive wrote:
       | https://web.archive.org/web/20231210141834/http://t3x.org/t3...
        
       | nils-m-holm wrote:
       | Here it is, my latest compiler book! Basically an expanded
       | version of "Write Your Own Compiler", this time discussing code
       | generation for CP/M on the Z80 (instead of ELF on modern
       | systems), which simplifies some things a lot.
       | 
       | How much complexity do you need to self-compile a compiler in 10
       | minutes on a 4MHz Z80 system? Take a look and find out! The code
       | is free (but the book is not).
        
         | 7thaccount wrote:
         | Always look forward to hearing about what you're working on
         | Nils! I hope your business of doing this is profitable as well!
         | One day I'm going to finally buy a copy of everything and work
         | through it all. It seems like there is so little time though
         | lol.
         | 
         | Edit: I'd also love to see you do a no-nonsense book on Forth
         | and your take on it.
        
           | nils-m-holm wrote:
           | Thank you!
           | 
           | Books are my biggest source of income, but "big" is relative
           | here: I am earning about $500 per month in revenues. This is
           | mostly a problem visibility, I think. Most people stumble
           | across my books by accident. Reviews, presence on the front
           | page of HN, etc. usually increase revenues significantly for
           | one month.
           | 
           | Regarding FORTH, lets see. The code is already there:
           | http://t3x.org/t3xforth/
        
             | Archbtw97543 wrote:
             | Thanks for being so transparent
        
               | nils-m-holm wrote:
               | Sure, no problem :)
        
             | winter_blue wrote:
             | $500 isn't really enough to live on -- do you have a job
             | that you use to support yourself (or do you live in a low
             | cost-of-living country)?
        
               | nils-m-holm wrote:
               | I have multiple sources of income, but book revenues are
               | the biggest one. The area I live in is a rather expensive
               | one, but I pay no rent, which helps. By the standard of
               | our country I qualify as "poor", but I do not mind much.
               | A little bit of safety would be nice, but I do not need
               | much to live comfortably.
        
               | tiberious726 wrote:
               | This is great quality work---you'd crush it writing
               | technical documentation or putting together technical
               | internal corporate training programs.
        
               | nils-m-holm wrote:
               | I tried that some time ago, but do not seem to be
               | compatible with all the business stuff. Thanks anyway! :)
        
             | pamoroso wrote:
             | Nils, regarding visibility, have you considered setting up
             | a Mastodon account? Yesterday I shared on Mastodon a link
             | to your book and my post got 28 reshares, 42 likes, and
             | half a dozen comments. And I'm not even the author of the
             | book. In the Fediverse people do notice you, read what you
             | write, and click your links. Which is mind blowing for
             | those used to traditional social platforms.
        
               | nils-m-holm wrote:
               | Thank you, but I do not even know what Mastodon or the
               | Fediverse are. :) Could you point me to some resources
               | that would get me started? Preferably to something for
               | the social media-illiterate! Which software to use (on
               | BSD), where to register (if that is a thing), etc. That
               | would be cool!
        
       | mati365 wrote:
       | Recently I made C multipass compiler (and asembler) in typescript
       | for such old x86 CPUs
       | 
       | https://github.com/Mati365/ts-c-compiler
        
         | nils-m-holm wrote:
         | Cool!
         | 
         | The one in the book is for the Z80, which is a bit older and
         | does not even have multiply or divide instructions. The
         | compiler can also output code for the 8086, though. And the
         | 386.
        
           | vanderZwan wrote:
           | Do you happen to have books that cover the latter two
           | targets?
           | 
           | EDIT: should have taken a look on the rest of your website
           | first. Clearly you do, hahaha
           | 
           | http://t3x.org/index.html
        
       | scrawl wrote:
       | I have a physical copy of Practical Compiler Construction 2nd Ed.
       | and like it a lot. I recommend Nils' books to anyone who may be
       | interested.
        
         | nils-m-holm wrote:
         | Thank you! The 2nd Ed. was quite an endeavor, I am glad you
         | like it!
        
           | kqr2 wrote:
           | You may need to update the thumbnail image on your index
           | page. It still shows the first edition:
           | 
           | https://t3x.org/index.html
        
             | nils-m-holm wrote:
             | Oops, good catch. :) Thank you!
        
       | AlexeyBrin wrote:
       | This looks really interesting, however a disadvantage is that the
       | reader needs to know or learn a new programming language first
       | T3X. I wonder if one could start from scratch on a CP/M system:
       | write and develop the compiler on a retro system that has no
       | connection to the outside world except the keyboard and display.
        
         | nils-m-holm wrote:
         | > however a disadvantage is that the reader needs to know or
         | learn a new programming language first
         | 
         | This is a good point, and I have thought about it a lot before
         | starting the book. What finally made my choose T3X is that its
         | compiler is much smaller[1] than my smallest C-subset compiler
         | and (IMHO) T3X is easier to learn or understand.
         | 
         | [1] SubC: 3815 lines, T3X/0: 2330 lines.
         | 
         | Of course you could start on CP/M without any outside tools,
         | but then you would have to write your bootstrapping compiler in
         | assembly language. Time-consuming, but certainly manageable. I
         | doubt that it would be an interesting reading, though.
        
           | JonChesterfield wrote:
           | You'd have some enthusiastic readers for writing compilers in
           | assembly. Especially if you went down the route of
           | progressively more capable assemblers. But "some" might be
           | fewer than five.
        
       | eterps wrote:
       | It would also be interesting to have a book on writing your own
       | CP/M-like OS.
        
         | retrac wrote:
         | That's Andrew Tanenbaum's _Operating Systems Design and
         | Implementation_.
         | 
         | Now, yes, that shows you how to write a Unix-like microkernel
         | OS; just skip everything except the file system chapter. And
         | don't follow the advice about tree data structures. Just use
         | flat tables, and don't bother to implement exact file sizes.
         | Presto: CP/M.
         | 
         | (I prefer the 2nd edition. The 3rd edition needlessly
         | complicates IMO, mostly so the demo Minix code will work on a
         | late 1990s PC instead of a 1980s PC.)
        
       | amelius wrote:
       | This book looks fun. But I'm still waiting for a worthy successor
       | of The Dragon Book, discussing optimizations for modern CPUs (and
       | perhaps GPUs), and also discusses how to design/write a modern VM
       | with a fast concurrent GC (something that some might say is even
       | harder than writing the compiler!)
        
         | tralarpa wrote:
         | I remember that I didn't like the Dragon Book when I was a
         | student. But I don't remember anymore why :) (I think I found
         | it poorly structured, and with too many details for some topics
         | and not enough details for others).
         | 
         | If you already have some base knowledge, you might like this:
         | 
         | https://www.cs.cmu.edu/~janh/courses/411/18/schedule.html
         | 
         | I particularly liked how they introduced SSA form.
         | 
         | More advanced topics:
         | 
         | https://www.cs.cmu.edu/~15745/handouts.html
        
           | tomcam wrote:
           | Fantastic resource, thank you.
        
           | kopecs wrote:
           | I also disliked the Dragon Book as a student. I found it to
           | have too much of an emphasis on lexing/parsing and not enough
           | discussion for optimizations/analysis for my liking. I liked
           | Advanced Compiler Design and Implementation by Muchnick,
           | although it does have some warts (ICAN; less discussion of
           | SSA than I would've liked) and I think it is a bit dated now.
           | 
           | FYI: replacing the 18 with 23 in your link to 411 gets you a
           | slightly updated version.
        
         | freedomben wrote:
         | Same, that's been my hope too. Stuff is getting so complex
         | nowadays with modern microcode/firmware and such, and there's
         | so many things that seem like "magic" to me. Feeling like
         | something is "magic" is my internal signal that somebody
         | figured out some clever way to get around what I think is/was
         | the limitations, and I _love_ discovering people 's clever
         | hacks. Recently been reading about Fake Bass (which is how
         | small speakers seemingly violate the laws of physics by
         | producing bigger bass than they are capable of) and how they
         | accomplish this by (ab)using harmonics to trick the brain into
         | hearing deeper notes than are actually tere. Fascinating stuff!
        
       | freedomben wrote:
       | Why such a focus on retro computing? As an oldie I think it's
       | cool (though a bit impractical learning the parts that aren't
       | applicable to modern stuff), but my son is interested in learning
       | operating systems, compilers, etc, and I could never get him to
       | use something so "outdated."
       | 
       | To be clear, I'm not attempting to criticize with this question
       | (my personal opinion is write about what interests you, even if
       | nobody else will care), I'm assuming you choose older targets for
       | a reason, and would like to undestand those reasons :-)
       | 
       | I.e. Do you believe the retro targets to be a lot simpler and
       | easier to understand, so people can iterate/build in layers? Or
       | do you just know the retro stuff better so it makes for a better
       | book?
        
         | nils-m-holm wrote:
         | My personal perspective is that computing has become much more
         | complicated than necessary in the past decades. Of course
         | abstraction will always create complexity, and some of that can
         | hardly be avoided, but in computing these days complexity is
         | really off the charts.
         | 
         | So for this book I chose a platform that is easy to understand
         | and does not make you wade through tons of abstractions that
         | are only loosely related to compiler construction (e.g. ELF
         | object file format).
        
           | 082349872349872 wrote:
           | Here's one way to avoid going into detail on ELF:
           | https://news.ycombinator.com/item?id=38592000
           | 
           | "(nearly) constant" means you can pick either (a) a constant
           | blob, at the cost of a fixed image size, a la COM, or (b)
           | patch up length (one or two places, iirc) if you're feeling
           | fancy.
        
             | nils-m-holm wrote:
             | In one of my other compiler books
             | (http://t3x.org/t3x/book.html) I just use a template for
             | the ELF header, but I still think it adds too much
             | complexity. One reader complained about it.
        
               | 082349872349872 wrote:
               | found it: _elfheader()_ in https://t3x.org/t3x/t.t.html
               | 
               | fwiw, I think you commented it very nicely; de gustibus!
               | 
               | Did you come up with _if_ vs. _ie ... else ..._
               | independently or inherit it from BCPL?
        
               | nils-m-holm wrote:
               | > fwiw, I think you commented it very nicely;
               | 
               | Thanks, I thought so, too, but I can also understand that
               | the comments are not very helpful, if you know nothing
               | about linkers, paging, and object files.
               | 
               | I think I adopted IE/ELSE from BCPL, but thought that IE
               | is nicer than TEST, because it is itself short for
               | If/Else and because it looks almost like IF.
               | 
               | > de gustibus!
               | 
               | Funny, I just started to brush up my Latin! :)
        
         | Archbtw97543 wrote:
         | Newer compilers or other low level programs have become
         | extremely complex. There a loads of features or optimizations
         | which are not strictly necessary but still add a lot of
         | complexity Attemptting to explain how modern compilers work
         | would most likely result in lots of confusion.
        
       | Max-q wrote:
       | This comment is not ment to be negative, just some insight that
       | might be valuable.
       | 
       | I read the free chapter. One thing I noticed right away was that
       | I think some things can be hard for people with not so much
       | knowledge about the topic: under each headline, it explains a
       | concept from the ground up, no knowledge required. Like "the
       | syntax of a language is...". But just a few sentences in,
       | advanced topics are touched, like assembly instructions, not
       | explained. It feels a bit like "the curse of knowledge", where
       | it's hard to know what the other party knows. But if the reader
       | needs to learn what syntax means, they will probably not
       | understand the next sentences.
       | 
       | So, I think more consistency could improve the product.
       | 
       | This is of course just my meaning and interpretation of the text,
       | it might not be relevant. But maybe something to have in mind for
       | your next masterpiece :)
        
         | nils-m-holm wrote:
         | > under each headline, it explains a concept from the ground
         | up, no knowledge required. Like "the syntax of a language
         | is...". But just a few sentences in, advanced topics are
         | touched, like assembly instructions, not explained.
         | 
         | As the blurb of the book states: no prior knowledge in the
         | field of compiler construction is required, but the reader
         | should be familiar with at least one procedural language and
         | one assembly language. So I thought it would be OK to assume
         | that the reader knows about things like assembly instructions.
         | 
         | Then the appendix of the book has a short introduction to Z80
         | assembly (which still assumes that you know the basics of
         | assembly language).
         | 
         | Every books starts _somewhere_. It would be hard to write a
         | compiler construction book and assuming zero knowledge about
         | computer programming.
         | 
         | I am not saying that the curse of knowledge is not a thing,
         | though, so I will definitely keep this in mind!
        
       ___________________________________________________________________
       (page generated 2023-12-10 23:00 UTC)