7 ms·
Write your own retro compiler
- scrawl 3y agoI 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 3y agoThank you! The 2nd Ed. was quite an endeavor, I am glad you like it!
- kqr2 3y agoYou may need to update the thumbnail image on your index page. It still shows the first edition: https://t3x.org/index.html https://t3x.org/index.html
- nils-m-holm 3y agoOops, good catch. :) Thank you!
- thinkmassive 3y agohttps://web.archive.org/web/20231210141834/http://t3x.org/t3x/0/book.html https://web.archive.org/web/20231210141834/http://t3x.org/t3...
- nils-m-holm 3y agoHere 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 3y agoAlways 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 3y agoThank 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/ http://t3x.org/t3xforth/
- Archbtw97543 3y agoThanks for being so transparent
- nils-m-holm 3y agoSure, no problem :)
- winter_blue 3y ago$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 3y agoI 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.
- mati365 3y agoRecently I made C multipass compiler (and asembler) in typescript for such old x86 CPUs https://github.com/Mati365/ts-c-compiler https://github.com/Mati365/ts-c-compiler
- nils-m-holm 3y agoCool! 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 3y agoDo 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 http://t3x.org/index.html
- AlexeyBrin 3y agoThis 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 3y ago> 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 3y agoYou'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.
- ramilefu 3y agoMake that six! I’m in!
- deleted 3y ago[deleted]
- mst 3y agoI don't see that as a disadvantage because (a) if you're planning to write a compiler, 'new programming language' is probably not something you're troubled by, and (b) having a simple+clean 'toy' language probably makes for better pedagogy. I'm -slightly- surprised it isn't using a C-like syntax rather than ALGOL-like but that's probably my own biases, and mentally mapping 'DO' and 'END' to '{' and '}' isn't much of a hardship.
- eterps 3y agoIt would also be interesting to have a book on writing your own CP/M-like OS.
- retrac 3y agoThat'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 3y agoThis 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 3y agoI 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 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 https://www.cs.cmu.edu/~15745/handouts.html
- tomcam 3y agoFantastic resource, thank you.
- kopecs 3y agoI 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.
- mst 3y agoI suspect that the emphasis is an artifact when the Dragon Book was written - getting a decent parser was rather more of a challenge, and heavily optimising compilers a lot less common. Tis one of those things, I guess.
- freedomben 3y agoWhy 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?
- deleted 3y ago[deleted]
- nils-m-holm 3y agoMy 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 3y agoHere's one way to avoid going into detail on ELF: https://news.ycombinator.com/item?id=38592000 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, à la COM, or (b) patch up length (one or two places, iirc) if you're feeling fancy.
- nils-m-holm 3y agoIn one of my other compiler books (http://t3x.org/t3x/book.html 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.
- Max-q 3y agoThis 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 :)
- deleted 3y ago[deleted]
- nils-m-holm 3y ago> 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!
- jdwithit 3y agoApart from the actual book content, I enjoy your appropriately retro and minimalist web design. It's giving me a huge nostalgia hit. Fond memories of hand crafting my own sites with a 6 inch thick book titled something like "HTML 3.2 UNLEASHED!!!!" on my desk :)
- nils-m-holm 3y agoGood times! It is all fun until you want to apply global changes to some 1500 static pages. :) So far the pain is not sufficiently intense to make me write a CMS, though. And even then the design would stay the same!
- pests 3y agoA compiler tutorial that gets past the lexing and parsing stages? First of its kind. I kid, but it is a common stopping point. Gonna pick this up.