(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=38591662

虽然探索新的语言和编译器很有价值,但它们的复杂性常常掩盖了使编程成为可能的基本概念和原则。 复古计算为个人在初级水平上掌握核心思想和机制提供了绝佳的机会。 通过学习逆向计算,我们可以在编程基础上打下坚实的基础,这些基础可以转化为 RISC-V 等当代技术。 此外,了解复古计算机上的汇编语言使我们能够理解尖端编译器中的低级函数。 复古计算机固有的简单性允许直接理解,使它们成为教授编程技能的理想平台。 最终,追求复古计算为获得当前技术的实际熟练程度提供了重要的垫脚石。

相关文章

原文
Hacker News new | past | comments | ask | show | jobs | submit login
Write your own retro compiler (t3x.org)
296 points by nils-m-holm 17 hours ago | hide | past | favorite | 45 comments










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).



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.



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/



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.


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!


Fediverse refers to a collection of decentralized applications that use some kind of common protocol. Like instead of centralized Twitter, you could choose Mastodon and so on. My understanding is very basic though.

Long story short, I'd love to see you advertise some more. I'm not sure how you'd reach your typical audience though.



Awesome Forth implementation! I'd love to read a high level book on the implementation too once you get around to it!


Thanks for being so transparent


Sure, no problem :)


$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)?


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.


This is great quality work---you'd crush it writing technical documentation or putting together technical internal corporate training programs.


I tried that some time ago, but do not seem to be compatible with all the business stuff. Thanks anyway! :)


Nils just wanted to say I love your work. I have your C compiler book and one of your lisp compiler ones as well. I’ve learned a lot and your work has helped me appreciate compilers a lot more, so thanks!


Recently I made C multipass compiler (and asembler) in typescript for such old x86 CPUs

https://github.com/Mati365/ts-c-compiler



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.



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



A 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.



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.


Thank you! The 2nd Ed. was quite an endeavor, I am glad you like it!


You may need to update the thumbnail image on your index page. It still shows the first edition:

https://t3x.org/index.html



Oops, good catch. :) Thank you!


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!)


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



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.



Fantastic resource, thank you.


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!


Apart 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 :)


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.


> 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.



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.


Make that six! I’m in!


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 :)



> 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!



It would also be interesting to have a book on writing your own CP/M-like OS.


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.)





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?



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).



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, à la COM, or (b) patch up length (one or two places, iirc) if you're feeling fancy.



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.


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?



> 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! :)



For me the interest in retro computing is that the computer is so much simpler that it is indeed possible to understand. The set of assembly instructions is small enough that a person can understand every opcode fairly easily, the amount of memory is limited, and the list of things that that your installed ROM can do is relatively accessible. Contrast that to modern machines and operating systems, and no mere mortal can understand everything that is happening in the machine.

Another thing to love about retro computers is how transferrable the knowledge is to modern machines. Once you know the essence of assembly language on a retro computer, you have a good basis for learning RISC-V or something else modern.

Depending on where your son is at, he might enjoy this video where I explain the essence of assembly language-- it's the video I wished someone had shown to me when I was a kid and thought I wasn't smart enough to learn it.

https://www.youtube.com/watch?v=ep7gcyrbutA



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.






Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact



Search:
联系我们 contact @ memedata.com