HackerTrans
TopNewTrendsCommentsPastAskShowJobs

_f5ah

no profile record

comments

_f5ah
·hace 2 años·discuss
A couple months ago I finished translating a simple generational garbage collector from MIPS[1] to x86-64 assembly[2], so that programms emitted by my toy compiler[3] run on the actual hardware instead of a MIPS emulator.

And sure enough, I had introduced a few bugs along the way. Here is a part of just one commit message:

> NoLeak-InsertionSort.cool exposed a problem where we make a copy of a prototype object to pass it as `this` to the constructor. But while evaluating all the other constructor parameters the copy gets promoted to Old Area. As a result any assignments the constructor makes end up recorded in the assignment stack. So that the referenced objects survive the next minor collection whereas they shouldn't have otherwise.

[1]: https://theory.stanford.edu/~aiken/software/cooldist/lib/tra...

[2]: https://github.com/mykolav/coollang-2020-fs/blob/master/src/...

[3]: https://github.com/mykolav/coollang-2020-fs
_f5ah
·hace 2 años·discuss
There is a class `ParameterizedAutoFactoryFactoryFactory`[1] in one of my pet projects.

The name does correctly reflect the purpose of this class. Although I could of course have come up with another one, the temptation of `Factory`x3 was just too strong to resist.

[1]: https://github.com/mykolav/unitycontainer-param-autofactory/...
_f5ah
·hace 3 años·discuss
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/
_f5ah
·hace 3 años·discuss
Looks interesting.

Once I saw it's a Czech university course using F#, I knew Tomáš Petříček would be the lecturer :)

A couple years back, I wrote a compiler of tiny-ish Scala subset in F# (the code is imperative, though)[1]

[1]: https://github.com/mykolav/coollang-2020-fs