编译器:增量式和可扩展性
Compilers: Incrementally and Extensibly (2024)

原始链接: https://okmij.org/ftp/tagless-final/Compiler/index.html

本编译器课程专注于构建一个完整的、可工作的Tiger语言编译器。Tiger语言类似于Pascal和C语言,目标平台为x86-64汇编。课程使用OCaml语言,采用tagless-final风格增量构建编译器,从而实现代码和类型检查结果的广泛复用。学生将遵循测试驱动开发方法并使用版本控制,学习如何扩展现有代码。 课程强调从解析到汇编的一系列领域特定语言,方便通过添加操作进行扩展。课程将重点关注变量名和属性管理,类似于代数效应。课程涵盖了标准的编译器主题,例如解析、类型检查、优化、调用约定和汇编代码生成,但采用非传统、可扩展的方式。本课程提供现代软件开发和编译器构建的实践经验。课程还包含一个x86-64模拟器,用于解释汇编概念。

Hacker News 最新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 编译器:增量式和可扩展式 (okmij.org) todsacerdoti 2小时前 16分 | 隐藏 | 过去 | 收藏 | 讨论 加入我们,参加 6月16-17日在旧金山举办的 AI 初创公司学校! 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系我们 搜索:
相关文章

原文
Compilers is a practical course. Its goal is to build a real compiler, which compiles a high-level language down to the actual x86-64 machine code and produces an executable that runs on student's laptops. The source language is Tiger': a procedural language in the spirit of Pascal -- or C with arbitrarily nested functions. The compiler itself is to be developed in OCaml.

The other goal of the course is to give a taste of modern software development, specifically: test-driven development, version control, and the stress on reading, comprehending and extending code rather than writing from scratch.

The characteristic of the course is an iterative, incremental development: we start with the most trivial source language, develop the full compiler for it, and then keep extending the source language and the compiler in small steps, reusing the earlier work as much as possible. At each iteration, we build a complete end-to-end compiler producing runnable and testable executables, for a (progressively larger) subset of the source language.

Another characteristic is the extensive use of tagless-final style, taking the full advantage of extensibility afforded by it. The extensibility here means reuse -- of type-checked and compiled artifacts from the previous increment -- rather than copy-paste. The compiler is hence structured as a stack of domain-specific languages, with parsing at the bottom and assembly at the top. The languages are extended by adding new operations here and there (and only occasionally by redirection).

Yet another feature is the attention given to names, or `variables', and associating attributes to them. Our approach, which readily permits adding attributes at will and analyzing variable usage, may remind some of algebraic effects.

We cover all standard material for the compiler course, from parsing and type-checking to analyses, optimizations, calling conventions and assembly generation -- but in a quite non-traditional fashion.

This course was taught as an elective 15-lecture upper-level undergraduate course in the Fall/Winter of 2022 and 2023.

course-notes.pdf [1123K]
Course notes (the first few chapters)

<tfintro/0README.dr>
Code for the section: Introduction to tagless-final style

Free Variable as Effect, in Practice
A greatly simplified version of the course, which still, hopefully, gives the taste of the incremental, step-wise compiler development. The article also explains extensible effects in compilation -- the technique currently used in the course.

emulator.ml [8K]
An elementary x86-64 emulator, to explain assembly in the introduction. The knowledge of assembly is not a prerequisite.

联系我们 contact @ memedata.com