Live data from Hacker News

Show HN: A compiler for a small language into x86-64 assembly

github.com

11–20 of 23 posts

Re: Show HN: A compiler for a small language into x86-64 assembly

#11

This is my first compiler, so I expect there to be several things I can improve :) I tried to follow the Assembly calling conventions the best I could. I am looking forward to any feedback!

Very cool. What resources did you use to learn how to write the compiler? The "turn code into ASM" step has always mystified me, and I'd like to learn more about how that part of the process works.

Those are some of my currently open tabs :) Lots of Google use on top of that. The parser is actually quite straight forward. The much harder part (for me) was the code generation afterwards (No experience with Assembly so far).

- General compiler design - This was one of the main ressources. https://www.tutorialspoint.com/compiler_design/compiler_desi...

- https://www.lua.org/manual/5.3/manual.html#9

- https://www.godbolt.org/

- Linux system call table: http://blog.rchapman.org/posts/Linux_System_Call_Table_for_x...

- A bit on floating point: https://cs.fit.edu/~mmahoney/cse3101/float.html

- Assembly https://www.cs.yale.edu/flint/cs421/papers/x86-asm/asm.html

- More assembly: https://www.complang.tuwien.ac.at/ubvl/amd64/amd64h.html

Re: Show HN: A compiler for a small language into x86-64 assembly

#12
post #8

This is my first compiler, so I expect there to be several things I can improve :) I tried to follow the Assembly calling conventions the best I could. I am looking forward to any feedback!

Very cool! Reading the "Why" section resonated with me, as I think creating one's own language is something every programmer should do for the experience. The syntax flows well, in fact it feels very intuitive for my taste. I like the type inference and no semicolons. I wonder if the latter posed any trickiness, for example, with the next line starting with an expression "(" or operator like "+". I'm also curious abo…

:) I (now) agree. In fact, each part isn't even really that hard. It's mostly just a lot to work. And then code generation. I had some headaches with multiple return values and keeping the calling conventions intact... And then with structs as well.

That language flow and general simplicity was one of my most important goals. Thanks for noticing :)

No, I had no problems regarding that. What you mention ('+', '(') are all part of simple expressions when parsing. And I strictly parse right recursive and re-order the expressions later (for operator priority). So that was not an issue. Most of these problems I solved, by making my parser a lookahead of >1. In a few cases, there is a lookahead of 3 to determine what exactly should be parsed.

I guess anything, that can run an X86-64 Elf executable? ;) Although there is still a lot missing, for it to be taken serious. Starting with strings, files, input, ... But thats for another time or whenever I need it, I guess.

Re: Show HN: A compiler for a small language into x86-64 assembly

#13

This is my first compiler, so I expect there to be several things I can improve :) I tried to follow the Assembly calling conventions the best I could. I am looking forward to any feedback!

Thank you for this. I like that it's small enough to be motivational instead of being complex and intimidating.

World needs more of these for other complex topics.

Re: Show HN: A compiler for a small language into x86-64 assembly

#14

This is my first compiler, so I expect there to be several things I can improve :) I tried to follow the Assembly calling conventions the best I could. I am looking forward to any feedback!

very cool. I'm writing a compiler and language myself and I was on the fence about anonymous structs / tuples vs multiple returns, and seeing multiple returns in your examples nudged me that way.

I'm also influenced by Lua and I picked it up in your grammar right away : )

Re: Show HN: A compiler for a small language into x86-64 assembly

#15

This is my first compiler, so I expect there to be several things I can improve :) I tried to follow the Assembly calling conventions the best I could. I am looking forward to any feedback!

Thank you for this. I like that it's small enough to be motivational instead of being complex and intimidating. World needs more of these for other complex topics.

That is really motivating, thank you for saying that!

Re: Show HN: A compiler for a small language into x86-64 assembly

#18

What would be your advice to someone who's about to write a compiler? I'm planning to start by reading the Dragon book.

I never got on with the Dragon Book despite several attempts.

You both should try "Compiler Construction: Principles and Practice" [0]. Theory is interleaved with substantial examples and exercises. You create an entire compiler for a tiny language called TINY (hah.) You write it in C and generate code for a portable virtual machine -- the book also goes into detail on the VM, with source code.

Lastly, the book's appendix has guidance for writing a compiler for a subset of the C language.

[0] https://www.cs.sjsu.edu/~louden/cmptext/

Re: Show HN: A compiler for a small language into x86-64 assembly

#19

What would be your advice to someone who's about to write a compiler? I'm planning to start by reading the Dragon book.

http://t3x.org/t3x/index.html#t3x9

If you plan to buy the book, you will have to go to Lulu.com and search for it, as order links seem to be down after a site redesign.

Re: Show HN: A compiler for a small language into x86-64 assembly

#20

What would be your advice to someone who's about to write a compiler? I'm planning to start by reading the Dragon book.

I didn't really read any books on that topic. But did lots of general research about compiler stages. I also posted a few links some comments up, that helped a lot.

For for it! Start small and increase. But seeing a program in your own language output something makes it all worth it :)

Post reply on HN