News for hackers. Stuff that matters.
Re:Apple hiring compiler developers for improving Swift / C+... by bluish29 on Saturday January 13, 2024 @07:16AM (#38979291)
On maybe slightly off topic point. I really want to learn more about compiler engineering, what people would suggest for someone with some background in programming but main background is physics (if that matters at all)?
Re:Apple hiring compiler developers for improving Swift / C+... by dashtiarian on Saturday January 13, 2024 @08:01AM (#38979566)
For a start you should, you should buy the latest version of the Dragon Book and then put it on the shelf.

Then you should get a copy of Modern Compiler implementation in ML. I love the Crafting Interpreters book, and also the Writing A Compiler In Go book; but they will not satisfy any person with undergraduate CS education.

Re:Apple hiring compiler developers for improving Swift / C+... by daemin on Saturday January 13, 2024 @09:51AM (#38980434)
I would completely agree.

From memory (because I left my copy of the Dragon book in another country) the book describes very well the first stages of compiler construction, describing the fundamentals of what Lex/Flex and Yacc/Bison do.

Right now for a compiler I'm writing I have used the Crafting Interpreters book and find it very useful. It's basic but can carry you through a project, and a decent reference for someone who took a Compiler Construction course about 20 years ago.

Re:Apple hiring compiler developers for improving Swift / C+... by moregrist on Saturday January 13, 2024 @11:09AM (#38981220)
For a contrary point of view, I don’t think the Dragon book actually holds up that well.

All of the front-end parsing stuff is good, but heavily biased toward LALR parsing, which is rarely used in production compilers these days because it’s really hard to get good error messaging. There’s also a lot of tooling that can help these days.

But where it really falls down is the backend part. Parsing is just not where you’ll spend most of your time. Once you parse you usually need to translate the parse tree (often an AST but not always) into some other representation.

I found the Appel books (Modern Compiler Implementation in <language>) to be much better at that part. I still own a Dragon book (an older one), but tend to refer to my Appel book a lot more when working on compiler-y things.

Re:Apple hiring compiler developers for improving Swift / C+... by steveklabnik on Saturday January 13, 2024 @12:00PM (#38981842)
I read your parent’s “and then put it on the shelf” as a cheeky way of saying “don’t read it.”
Re:Apple hiring compiler developers for improving Swift / C+... by smokel on Saturday January 13, 2024 @07:26AM (#38979339)
The field is called "compiler design". Consider taking a course [1] or buying a book?

A somewhat more playful approach might be to complete the 2019 edition of Advent of Code [2], where you'll learn some preliminaries while implementing the Intcode interpreter.

[1] https://online.stanford.edu/courses/soe-ycscs1-compilers

[2] https://adventofcode.com/2019

Re:Apple hiring compiler developers for improving Swift / C+... by astrange on Saturday January 13, 2024 @07:45AM (#38979460)
The trick is to ignore the people recommending the Dragon Book. It makes everything sound unnecessarily complicated and will scare you off.
Re:Apple hiring compiler developers for improving Swift / C+... by dhooper on Saturday January 13, 2024 @07:50AM (#38979504)
Crafting interpreters is a good place to start.

https://craftinginterpreters.com/

Re:Apple hiring compiler developers for improving Swift / C+... by superlopuh on Saturday January 13, 2024 @09:29AM (#38980218)
I work on a reimplementation of MLIR in Python called xDSL, and ported a part of MLIR's Toy tutorial, you can try it interactively online at xdsl.dev/notebooks, or by cloning the github repo and running them locally github.com/xdslproject/xdsl
Re:Apple hiring compiler developers for improving Swift / C+... by curling_grad on Saturday January 13, 2024 @09:50AM (#38980425)
LLVM's Kaleidoscope tutorial[0] is a nice starting point.

[0]: https://llvm.org/docs/tutorial/

Re:Apple hiring compiler developers for improving Swift / C+... by myk9001 on Saturday January 13, 2024 @10:16AM (#38980674)
Here's my take[1] on it. In essence,

# Learning Resources

## Theory

Take a look at these two books:

- Crafting Interpreters[2]. Compilers and interpreters have a lot in common and the book is exceptionally beginner-friendly. Basically, this is the perfect one to get started with.

- Introduction to Compilers and Language Design[3]. Doesn’t assume any preexisting compilers knowledge, and teaches all the basics necessary to build a compiler using a hands-down approach. The examples are in C.

Ignore the classics textbooks like the Dragon Book or "Modern Compiler Implementation" for the time. You can always come back to them later if you want to.

## Source code

Explore the sources of

- 8cc[4]. A compiler for the C programming language. It's intended to support all C11 language features while keeping the code as small and simple as possible.

- chibicc[5]. The successor of 8cc from the same author.

If you want a bit of background on these two compilers, check out the author's blog post "How I wrote a self-hosting C compiler in 40 days"[6]. Personally, I find it to be quite a fascinating read.

# Input language

Don’t try to come up with your own language just yet. Go with an existing educational language instead, and focus on learning about compilers. ChocoPy[7] is specifically designed for classroom use in compiler courses and by extension is great for a hobby compiler project.

# Target language

Many educational compilers emit MIPS assembly. Although it's possible to run it using an emulator, running a native executable produced by your own compiler feels much more rewarding. So I'd suggest your compiler emits x86-64 assembly.

For educational purposes, I'd avoid targeting languages such as LLVM IR or C. Emitting assembly helps understand many important concepts better:

- Managing a function's stack frame

- Calling conventions

- Finding the memory address of an identifier

- Converting expressions into assembly

- And so on and so forth.

[1]: https://mykolam.net/posts/toy-compiler-of-scala-subset/2-loo...

[2]: https://craftinginterpreters.com/contents.html

[3]: https://www3.nd.edu/~dthain/compilerbook/

[4]: https://github.com/rui314/8cc

[5]: https://github.com/rui314/chibicc

[6]: https://www.sigbus.info/how-i-wrote-a-self-hosting-c-compile...

[7]: https://chocopy.org/