[COURSE] Add PKU compiler practice course (#594)

* add PKU compiler practice course

* nits
This commit is contained in:
Yinmin Zhong 2024-04-14 14:35:08 +08:00 committed by GitHub
parent 4979ddabbe
commit 7a320f474a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 91 additions and 2 deletions

View File

@ -18,7 +18,7 @@ Unlike the small but comprehensive design philosophy in MIT's xv6 labs, *Pintos*
Although it is tough, Stanford, Berkeley, JHU and many other top U.S. colleges have chosen *Pintos* as their OS course project. If you're really interested in operating systems, it will greatly improve your ability to write and debug low-level system code. For me, it is an invaluable experience to design, implement, and debug a large system independently.
*Pintos* will also be introduced as a course project in Peking University's OS Course. In the Spring 2022 semester, I worked with [another TA](https://github.com/AlfredThiel) to write a comprehensive [lab documentation](https://alfredthiel.gitbook.io/pintosbook/) and provided a docker image for the ease of cross-platform development. In the last semester before graduation, I hope such an attempt can make more people fall in love with systems and contribute to the field of systems in China.
*Pintos* will also be introduced as a course project in Peking University's OS Course. In the Spring 2022 semester, I worked with [another TA](https://github.com/AlfredThiel) to write a comprehensive [lab documentation](https://pkuflyingpig.gitbook.io/pintos) and provided a docker image for the ease of cross-platform development. In the last semester before graduation, I hope such an attempt can make more people fall in love with systems and contribute to the field of systems in China.
## Course Resources

View File

@ -20,7 +20,7 @@
虽然难度很大,但 Stanford, Berkeley, JHU 等多所美国顶尖名校的操统课程均采用了 Pintos。如果你真的对操作系统很感兴趣Pintos 会极大地提高你编写和 debug 底层系统代码的能力。在本科阶段,能自己设计、实现并 debug 一个大型系统,是一段非常珍贵的经历。
北大 2022 年春季学期的操作系统实验班也将会首次引入 Pintos 作为课程 Project。我和该课程的[另一位助教](https://github.com/AlfredThiel)整理并完善了 Pintos 的[实验文档](https://alfredthiel.gitbook.io/pintosbook/),并利用 Docker 配置了跨平台的实验环境,想自学的同学可以按文档自行学习。在毕业前的最后一个学期,希望能用这样的尝试,让更多人爱上系统领域,为国内的系统研究添砖加瓦。
北大 2022 年春季学期的操作系统实验班也将会首次引入 Pintos 作为课程 Project。我和该课程的[另一位助教](https://github.com/AlfredThiel)整理并完善了 Pintos 的[实验文档](https://pkuflyingpig.gitbook.io/pintos),并利用 Docker 配置了跨平台的实验环境,想自学的同学可以按文档自行学习。在毕业前的最后一个学期,希望能用这样的尝试,让更多人爱上系统领域,为国内的系统研究添砖加瓦。
## 课程资源

View File

@ -15,3 +15,7 @@ Stanford 的进阶编程课CS106X 在难度和深度上会比 CS106B 有所
- 课程网站:[CS106B](https://web.stanford.edu/class/cs106b/), [CS106X](https://web.stanford.edu/class/cs106x/)
- 课程教材:<https://web.stanford.edu/class/cs106x/res/reader/CS106BX-Reader.pdf>
- 课程视频:<https://www.bilibili.com/video/BV1G7411k7jG>
## 资源汇总
@akun0311 在学习这门课中用到的所有资源和作业实现都汇总在 [akun0311/CS106B-X-CS106L - GitHub](https://github.com/akun0311/CS106B-X-CS106L-) 中。

View File

@ -0,0 +1,42 @@
# PKU Compiler Principles Practice
## Descriptions
- Offered by: Peking University
- Prerequisites: Basics of Computer Systems, Data Structures and Algorithms, Programming Fundamentals
- Programming Languages: Choose one from C/C++/Rust
- Difficulty: 🌟🌟🌟🌟
- Class Hour: 60 hours
The practice course of Compiler Principles at Peking University aims to teach student to implement a compiler that translates from a simplified version of the C language — SysY to RISC-V assembly. Unlike other courses that provide skeleton code, this course grants you a great degree of freedom. The test programs only check the correctness of the assembly code your compiler outputs, without imposing any restrictions on the specific design of your compiler or even the programming language used. You need to start from scratch and gradually build your own compiler. However, this is quite challenging even for Peking University students, so in 2022, the course TA [@MaxXing](https://github.com/MaxXSoft) significantly upgraded this course, designing a special intermediate representation, Koopa IR, for the course. Koopa IR is more rationally designed and, while similar to LLVM IR, it simplifies many details, focusing only on the practical aspects needed. At the same time, a corresponding runtime library was designed for Koopa IR, allowing you to easily parse/generate/modify/output Koopa IR without having to consider other superfluous details. Additionally, a detailed document breaks down the large project of building a compiler into nine small steps, making it easier for any student willing to invest the time to implement their own compiler.
The following content is excerpted from @MaxXing's [Blog](https://blog.maxxsoft.net/index.php/archives/145/) which introduces this course in more details:
---
You may have seen countless tutorials teaching how to write a compiler, but they may have various issues that still leave you puzzled about where to start:
- The tutorials are not aimed at people with "zero compiler foundation," and can be difficult to understand.
- They teach how to make a compiler but not why to do it that way, leaving you still in a fog after reading.
- Complicated environment setup is required before starting coding, discouraging beginners at the start.
- The initial tasks are unrelated to "compiling a program," and it can take a month following the tutorial to see a running program from your compiler, resulting in a long feedback cycle.
- What you finally write is just an interpreter, or it can't compile to a real ISA (like outputting some bytecode), or it's highly dependent on other frameworks (like LLVM), which doesn't let you feel the accomplishment of "I really made a compiler" and the exhilaration of "doing everything myself."
- The compiler implemented can only compile simple programs, such as sorting algorithms, which seem very boring. Who sorts just for fun?
- Tutorials and their resources are not in Chinese, or they require payment and are not open source.
During my graduate studies, I was the TA in the undergraduate Compiler Principles course. To help undergraduates better understand how compilers work, after referring to several other tutorials, I designed a new set of tutorials that teach you how to write a compiler from scratch:
- You can use C/C++/Rust to develop your compiler, the tutorial only requires you to have the ability to program in these languages, without any need for foundational knowledge of compiler principles or related practical experience.
- The tutorial not only tells you how to write a compiler but also why to do it that way, and what else you could do.
- The tutorial adopts an incremental, iterative approach, guiding you from a compiler that can only handle the main function to gradually expanding to implement a compiler for a C-like language that includes control flow, function calls, and arrays. The compiler can output RISC-V assembly.
- Providing a Docker-based environment for one-click setup. The environment also includes automatic testing scripts for one-click testing of your compiler.
- The tutorial introduces tools that can take over some of the tedious grunt work, such as generating lexer/parser, parsing and generating IR, etc. The rest, like building AST, semantic analysis, generating IR, and producing target code, is entirely up to you to implement.
- The compiler you ultimately implement will be capable of compiling many complex programs, such as the Mandelbrot set drawing program or a Lisp interpreter (this interpreter can even interpret another Lisp interpreter). We will provide these programs for you to experience the real joy of using your compiler.
- The tutorial and its accompanying tools, experimental environments, etc., are all open source and completely free!
- You can access this tutorial on GitHub Pages: [Peking University Compiler Practice Course Online Documentation](https://pku-minic.github.io/online-doc/#/). If you have never tried writing a compiler yourself, why not try it now?
My wish is that everyone feels the joy of writing compilers.
---
Finally, my highest regards to @MaxXing. — from PKUFlyingPig

View File

@ -0,0 +1,42 @@
# PKU 编译原理实践
## 课程简介
- 所属大学:北京大学
- 先修要求:计算机系统基础、数据结构与算法、编程基础
- 编程语言C/C++/Rust 任选其一
- 课程难度:🌟🌟🌟🌟
- 预计学时60 小时
北京大学的编译原理实践课程旨在实现一个从精简版的 C 语言 —— SysY 编译到 RISC-V 汇编的编译器。和其他提供了框架代码的课程不同,该课程给予了你极大的自由度,测试程序只会对你输出的汇编代码的正确性进行测试,而不会对你编译器的具体设计甚至使用的编程语言做任何限制。你需要从一个空文件夹开始一步步构建出独属于你自己的编译器。但平地起高楼即使对于北大的同学也是相当有难度的,因此课程助教 [@MaxXing](https://github.com/MaxXSoft) 在2022年对课程实验进行重大升级为课程专门设计了一种中间表示 Koopa IR (intermediate representation)。Koopa IR 在设计上更为合理,在形式上类似于 LLVM IR但简化了相当多的内容只专注于实践需要的部分。与此同时还为 Koopa IR 设计了配套的运行时库,你可以借助这套运行时库轻松地解析/生成/修改/输出 Koopa IR完全不需要对其他无用的细节做过多考虑。另外还有一份保姆级的文档将构建编译器这样一个大工程循序渐进地拆分成了9个小步骤让任何愿意花时间的同学都可以更容易地实现自己的编译器。
以下内容摘自 @MaxXing 在其 [Blog](https://blog.maxxsoft.net/index.php/archives/145/) 中对该课程实验的介绍:
---
你可能已经见过无数个教你写编译器的教程了,但它们也许或多或少存在各种问题,导致你看了它们之后,依然觉得无从下手:
- 教程并非面向“零编译器基础”的人群,理解起来比较费劲。
- 只教了怎么做一个编译器,但没教为什么这么做,读完之后还是觉得一头雾水。
- 开始编码之前,需要复杂的环境配置,开局劝退。
- 上来先做和“编译一个程序”这件事八竿子打不着的东西,跟着教程学一个月才能看到自己的编译器编出一个能跑的程序,反馈周期过长。
- 最终写出来的只是个解释器,或者不能编译到真实的 ISA例如输出了某种字节码或者高度依赖其他框架例如 LLVM让人体会不到那种“我真的写出个编译器”的成就感以及“所有事情都由自己实现”的酣畅感。
- 实现的编译器只能编译一些过于简单的程序,比如排序算法等等,看起来非常无聊。谁会没事干排序玩啊?
- 教程及其配套资源非中文,或需要付费,且不开源。
研究生期间,我一直在担任本科编译原理课程的助教。为了让本科生更好地理解编译器工作的原理,在参考多个其他教程之后,我设计了一套全新的,教你从零开始写个编译器的教程:
- 你可以使用 C/C++/Rust 开发你的编译器,教程只要求你具备使用这些语言编程的能力,不要求任何编译原理的基础和相关实践经验。
- 教程除了告诉你要如何写一个编译器,还会告诉你为什么这么做,以及除此之外还能怎么做。
- 教程采用增量式、迭代式的思路,引导你从一个只能处理 main 函数的编译器开始,逐步扩展实现一个能处理包括控制流、函数调用、数组在内的,类 C 语言的编译器。编译器可以输出 RISC-V 汇编。
- 提供基于 Docker 的实验环境,一键配置。环境中还附带自动测试脚本,一键测试你写的编译器。
- 教程介绍了相关工具,可以为你代劳一些枯燥的苦力活,比如生成 lexer/parser解析和生成 IR 等等。剩下的事情,如构建 AST、语义分析、生成 IR 以及生成目标代码,完全由你自己编程实现。
- 你最终实现的编译器足以编译很多复杂的程序,例如 Mandelbrot 集绘制程序,或者 Lisp 解释器(这个解释器甚至可以解释另一个 Lisp 解释器)。我们会提供这些程序,让你用你写的编译器体验到最真实的快乐。
- 教程及其配套工具、实验环境等,全部开源,完全免费!
- 你可以从 GitHub Pages 上访问到这个教程:[北京大学编译实践课程在线文档](https://pku-minic.github.io/online-doc/#/)。如果你之前从来没有尝试过自己写一个编译器,不如现在就来试一下吧!
我的愿望是,让每个人都感受到写编译器的快乐。
---
最后,向 @MaxXing 致以我崇高的敬意。—— from PKUFlyingPig

View File

@ -219,6 +219,7 @@ nav:
- "Stanford CS346: Database System Implementation": "数据库系统/CS346.md"
- "CMU 15-799: Special Topics in Database Systems": "数据库系统/15799.md"
- 编译原理:
- "PKU 编译原理实践": "编译原理/PKU-Compilers.md"
- "Stanford CS143: Compilers": "编译原理/CS143.md"
- "NJU 编译原理": "编译原理/NJU-Compilers.md"
- 编程语言设计与分析: