编程与软件工程sigbus.info
我如何在 40 天内写出自举 C 编译器
How I wrote a self-hosting C compiler in 40 days
2015 — 2017,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
作者记录了自己在 40 天内从零写出一个能编译自身的 C 编译器(8cc)的全过程。文章以日志形式展开,从最初只能读入一个整数并退出的 20 行程序起步,逐步实现 C11 特性和预处理器,坦率记录了中途的迷茫、重写与自我怀疑。
为什么经久不衰:它既是难得的编译器实战教程,也真实呈现了大型项目从最小可行版本逐步生长的过程,这种诚实叙事让它在多年后仍被反复推荐。
HN 评论精选
文章坦诚记录失败与自我怀疑的叙事风格在技术写作中十分罕见,令人耳目一新
我很喜欢这篇文章:信息量大、有趣,而且语气难得地坦诚。计算机科学圈流传的故事往往是主角在关键时刻灵光一闪想出巧妙方案,很少有程序员坦白自己的后悔、自我修正,以及一路上的恐惧与怀疑。比如作者写道:“我本该从一开始就写出漂亮的代码,但因为需要通过写出能运行的代码来学习,重写不可避免。”还有他在低谷时提醒自己:一个月前编译器还只是一个用 scanf 读整数、用 printf 打印出来的单文件程序——自己已经进步了这么多,肯定能做完。
bad-joke查看英文原文
I really enjoyed reading this. It's informative, fun, and has a refreshingly honest tone. Too often, stories passed around by computer scientists entail clever solutions and elegant insight striking the protagonist like lightning in the hour of need. Rarely does the programmer express regret, make self-corrections, and confront fear and doubt along the way: >I should have written beautiful code from the beginning, but because I needed to learn by writing a working code, the rewriting was unavoidable. >I should probably change my mind to implement all the features from end to end. I may find it fun as I'm approaching the goal. Sometimes, I have to write more code than I want to write in order to achieve a goal. >In a tough situation like this, I probably should recall the fact that the compiler was just in one file, to see how much progress I've made in a month. It just reads an integer with scanf() and prints it out with printf(). Really, I made so much progress in one month. Yeah, I think I can do this.
标准化的 C 语言比自创玩具语言难实现得多
“我怀疑 Dennis Ritchie 发明了一种语法并为它写了实现,结果发现比他预想的更复杂,后来被 ANSI 委员会标准化。实现一个标准化语言很难,因为你必须把每件事都做对;而写自己的玩具语言则相当容易。”很喜欢这几句。
jaybosamiya查看英文原文
> I suspect that he [Dennis Ritchie] invented a syntax, wrote code for it, which turned out to be more complicated than he had expected. And that was eventually standardized by the ANSI committee. It's hard to implement a standardized language because you have to get everything right. It's rather easy to write your own toy language. Love those lines
在 40 天内完成 C11 和预处理器是惊人的成就
作者能在这么短时间内同时搞定 C11 和预处理器,让我有点惊讶。预处理器很难写,不过他复用了之前版本的代码,这就说得通了。总之是非常了不起的成就,祝贺作者!
tomcam查看英文原文
I was a little surprised that the author was able to manage both C11 and the preprocessor in that time. The preprocessor is hard. But there was existing code from a previous version of it, which makes sense. Still, a fantastic achievement! Congrats to the author!
有经验的开发者证实这个时间量级合理,且预处理器常是最难的部分
1998 年我为一种新指令集架构的仿真环境写自举 C 编译器和基础运行库时,也差不多花了这么长时间(它后来成了 Cray X1)。不过我是从早先项目留下的现成 C 预处理器起步的,而预处理器往往是最难写对的部分。在仿真环境中启动编译器比在真实硬件上有优势:能看到带计算值的指令轨迹,调试起来太爽了。
pklausler查看英文原文
That's about how long it took me to write a self-compiling C compiler and basic runtime library for a simulation environment for a new instruction set architecture back in '98 (it eventually became the Cray X-1). Although I did start with a working C preprocessor left over from an earlier project, and in many ways the preprocessor can be the hardest part to get right. Bringing up a compiler in a simulated environment has its advantages over real metal. Seeing instruction traces with computed values is awesome for debugging.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选