Optimizing Compiler | Devlog #03
New Features
─────────────────────────────────────────
Hello again!
I’ve turned Comet into an optimizing compiler! So far we don’t have much, but Comet now has constant folding and constant propagation. But what does that actually mean?
“Constant Folding”
Constant folding means we look through the code for any math expressions or other things of the like. If there are any constants in the math expression, then we can replace the expression with what it will equal.
For example:
int x = (2 + 3) * (4 + 5)
The constant folder will see (2+3), and because the expression only has constants it will replace it with 5
int x = 5 * (4 + 5)
The constant folder then sees (4+5), and replaces it with 9.
int x = 5 * 9
And finally it sees 5 * 9 and replaces it with 45.
int x = 45
We’ve turned a 7 instruction expression into 1 instruction!
“Constant Propogation”
Constant propogation means that if a variable stays a value that the compiler knows throughout the variable’s lifetime, then we can replace all instances of that variable with its value. For example:
int x = 2 + 3
int y = x + 3 * 2
Constant folding runs and we get
int x = 5
int y = x + 6
x is a compile time constant now, and because it never stops being a constant, we can replace all values of x with 5.
int x = 5
int y = 5 + 6
See how we can now run constant folding again now that we have more information? This is why compilers run optimizations over and over, until the code doesn’t change anymore.
Bug Fixes
─────────────────────────────────────────
Along with adding an optimizer, I’ve fixed a large bug Return statements are automatically added to functions now! Before, if you didn’t add a return statement, your function could go past where it should’ve been, crashing your program and possibly even running code it shouldn’t. That is now fixed. Comet also throws an error if you don’t include a return statement in a non-void function.
Docs
─────────────────────────────────────────
Finally, I’ve extended the docs on the Comet website. It previously only have information on everything up to loops, but now includes info on structs and arrays.
That’s all from me, see you next time!