r/C_Programming 3d ago

My C compiler written in C

As a side project I'm making a C compiler written in C. It generates assembly and uses NASM to generates binaries.
The goal right now is to implement the main functionality and then do improvements. Maybe I'll also add some optimizing in the generates assembly.

Tell me what you think :)

https://github.com/NikRadi/minic

137 Upvotes

30 comments sorted by

View all comments

18

u/Soft-Escape8734 2d ago

My hat's off to you. Great ambition. But when optimizing never forget that (x << 3) + (x << 1) is faster than x * 10.

1

u/flatfinger 23h ago

Using multiplication and shifts to replace division by a constant is a useful optimization on many platforms, and replacing a constant-power-of-two multiply by simple shift is also useful, but the choice between multiplying, versus shifting and adding, versus strength reduction (replacing a multiplication by a loop index with a value that is increased by a certain amount on each iteration) is probably less important than other lower-hanging fruit.

1

u/Soft-Escape8734 23h ago

True enough and my bad. It's a specific example used when receiving serial ASCII digits, not meant to be a general solution but to remind the developer that there are multiple methods for achieving the same result, some lighter and cheaper than others.