r/C_Programming • u/ocotpi_ • 14d ago
Building a small C99 compiler to get better at low-level C and x86-64 assembly
Hey everyone,
I've been trying to deepen my understanding of C and x86-64, and instead of just doing LeetCode or toy projects, I decided to try writing a tiny, self-contained C compiler in C99.
I wanted zero external dependencies just standard C library headers (stdio, stdlib, string, ctype).
I just reached the milestone where it handles operator precedence and nested parentheses:
- Tokenizer: A linked-list tokenizer with character pointers that skips whitespace and formats visual Clang-style
^caret error diagnostics. - Parser & AST: A recursive descent parser that allocates binary AST nodes for operators and numbers.
- Code Generation: A stack-based code generator that recurses through the AST and emits x86-64 assembly (using
push/popfor expressions,imulfor multiplication, andcqo+idivfor signed 64-bit division).
Writing this has already taught me so much about memory layout, pointer arithmetic, and how the Linux ABI handles return values in %rax.
I have a test script with automated GitHub Actions CI verifying each arithmetic edge case against GCC.
I'd really appreciate any C veterans taking a look at my C style, error handling, or memory management in src/main.c:
https://github.com/mi-vault/mibicc
Any tips or feedback are very welcome!
Edit: Decided to add Windows support alongside Linux from the start (--target=windows and --target=linux). I'm on Windows myself so it felt silly to only target Linux System V and not be able to test it locally. Also feels nice putting my own stamp on it rather than just following chibicc 1:1.
(Had some great discussions over on r/Compilers about ABI quirks and stack architectures if anyone is interested in that side of things!)
•
u/mikeblas 14d ago
What role did AI have in the creation of your project?
5
u/tempestpdwn 13d ago
I am currently writing an assembler and do have plans to start with a c compiler sometime soon after im done with this.
Can you point me to any resources that you've found useful or the ones that you are following if any?
3
u/AutoModerator 14d ago
Hi /u/ocotpi_,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
You can read more at How do I post my Project?
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Hoshiqua 12d ago
I also have worked on my own C99 compiler, I switched to a different project since then but I want to come back to it sometimes soon. Github link. The current level of advancement is full tokenization, parsing, and nearly full solving / "integration" (as I've called it) of the C99 standard, with a few exceptions like gotos and switch statements which I wanted to handle later for some reason. It does manage stuff like typedefs and whatnot, which made me completely rewrite the AST logic multiple times as I got a more and more "pure" understanding of how the C standard works conceptually and thus how my compiler "should" read it. The main takeaway was that C is made up of object declarations, which always start with:
- Whether the objects are typedefs / aliases or actual objects
- A type (var types and function return types), in the form of a primitive type, a typedef'ed object name, or a defined struct / enum.
The start of an objects declaration statement is followed by a comma separate list of objects that use that type. They can be variables (N stars for pointer / indirection levels, a name, and N array sizes), functions (a name with parameters in parenthesis with comma-separate parameters) or function pointers (a hybrid of the two: a variable with parameters and the well-known weird syntax).
It ends when either a function definition or a semicolon is encountered.
A lot of the time in actual code, we don't tend to mix type definitions and declaring objects, but it's all the same really. It's only for readability. This means you can write something like:
And that's pretty much the entire langage right there, not counting pre-processor stuff.
Maybe that's obvious but it certainly wasn't to me even though I have years of experience in C / C++... that realization made the langage click to me as a compiler developer: my parsing stage which used to be this complicated tree of possibilities depending on the sort of statement I was facing violently collapsed to only being a single comparatively simple state machine, with the main "branch" being whether you're inside a function or not.
As for your code...
I like the way you're going with this, and that's something I've been thinking I should have done with mine - try to get the whole chain working for a small subset of features, rather than building the FULL AST then the FULL solver and later (not started yet) the FULL code generation.
However, I guess one drawback of that approach is that if you make a bad choice of architecture, it will bite you in the buttox over multiple stages instead of a single one at a time. And I think this is something that will create a bunch of work down the road - so far what you have is an expression parser, which is actually a big thing to build in pure C99 already so great job. However, I think you will find that, for example, defining your AST Nodes as the different operand / operator types will not be the right granularity.
For now the simplicity / size of the program makes it so putting it all in one file and separating the stages by function calls only is fine, but the reason I personally didn't do that is because I wanted to conceptually build the stage-based pipeline right away, allowing me to better separate the concerns of each stage, handle their errors with their own codes, encapsulate the code / symbols, etc...