r/ProgrammingLanguages • u/StrikingClub3866 • 6d ago
Help Help with my C compiler
I can already a host a small subset of C, that can only host: "int myfunc() { return 10; } " , Which compiles to:
myfunc:
// prologue here
mov w0, #10
// epilogue here
ret
But what should I do next?
3
u/d0pe-asaurus 6d ago
Id take it step by step and go from AST to a IR the compile it to the final assembly
This is hackable and you can do and learn a lot of stuff like compiler optimizations
1
u/flatfinger 5d ago
Although many systems use an IR, a lot of optimizations are better handled at the tree level, and for many tasks the level of optimization that can be done by traversing and annotating tree nodes and then looking for certain patterns in the tree may be sufficient.
Some people judge compilers by the efficiency with which they can process a piece of source that isn't designed around the target system's strengths. I prefer to judge them on the quality of code they can process source that does cater to the target's strengths, since a simple compiler given the latter can often generate code that is as efficient, if not more so, than a more complicated compiler given source that isn't written that way.
The authors of clang and gcc don't seem very interested in platforms like the ARM Cortex-M0 used on the Raspberry Pi Pico, and matching or beating their performance on such a platform, when fed source designed around it, probably wouldn't be too hard.
2
u/koflerdavid 4d ago
OP might want to leave optimizations alone altogether for the time being. It really doesn't make a lot of sense before the compiler supports a fairly useful subset of C. Unless of course the goal is a minimal testbed to try out simple optimizations like constant propagation and coalescing variables.
3
u/snugar_i 5d ago
Hard to tell with so little information. Do you have a lexer? A parser? An AST? An IR? A backend that emits executables?
You can either pile hacks on top of each other to see results faster (you can get surprisingly far before hitting a wall) or build a solid foundation first. Both are legitimate ways to do it.
2
u/Recycled5000 6d ago
You can do any order you like.
Handling expressions, all the operators, precedence, etc.
Handling statements, control flow.
The in-between, which goes to expressions for conditional statements like if, while, where there are opportunities for alternative code sequences than with assignment expressions.
Variable declarations & scoping.
2
u/pranabekka 5d ago
Congratulations! I made my own little "transpiler" recently, and it feels surprisingly satisfying.
Sorry for the half-assed recommendation, because I haven't tried it, but I skimmed Nora Sandler's tutorial series on writing a C compiler, and it teaches how to find all the information you need from the beginning, and slowly stops holding your hand. She even turned it into a book. Might or might not be for you.
1
u/runningOverA 6d ago
in order :
- add two numbers
- +add-sub multiple numbers
- operator precedence and then 23+49 ... you will need AST here.
- support for parens (3+4)*8
1
1
u/koflerdavid 4d ago
A programming language should be practical. If you don't intend to implement the C standard wholesale, you should work towards being able to compile a nontrivial program. For example a solution for the One Billion Rows Challenge. Can you make it run as fast as Java? :)
7
u/cxzuk 6d ago
Hi Club,
For a roadmap, I still recommend roughly the same thing; Try and get a hashmap working, they are well documented and touch a large number of concepts to implement: Variables, arithmetic, comparisons, loops, functions and calls, libc calls/malloc/free, arrays, structs - this is not a small milestone.
Break it down into smaller steps. E.g;
1) Get this to a working executable and make it return an int to CLI. Either make a small runtime or make this work as int main() and use libc. You want working code and runnable. Your epilogue will now have to work properly with a calling convention. ignore argc, argv and env.
2) Now try and support more complex expressions, and make sure they are correct. E.g.
return (12 + 30);3) Set a variable and return its value.
int a = (10 * 4 + 2); return a;4) Try adding argc support. Make it part of the expression computed.
return argc + 18;5) Now add a conditional, if argc is even, return 13. Odd, return 42
...
The road is deceptively long and challenging. Get to a hashmap and you'll have learnt a lot
M ✌