Optimizing and pre-evaluating

Many present compilers try to simplify mathematical expressions during compile time in order to ease the burden of the final executable. Naturally this results in faster runtime code execution in some cases and even the size of the executable may shrink a bit. I’ve been working on some similar mechanics for CoolBasic V3 compiler, and at this point in time I’ve grown pretty satisfied with it. Most constant expressions will now be pre-calculated, meaning that statements get shorter before the actual syntax checks begin. All of this will help me to establish as bug-free development as possible. Compilation based on very very strict rules, in the end, tends to prevent exceptions in the algorithms. Simplicity is beautiful.

This kind of optimization is, however, very hard to carry out perfectly. An an example of this matter would be the following expression to optimize well:

1 + 2 + a [into] 3 + a

But grouping is a bit trickier:

1 + a + 2 [into] 3 + a

For now, I decided to leave as it is i.e. only the first example remains in effect.

Another funny part of this implementation of pre-evaluation is that the compiler’s source code almost doubled just by adding this feature. You can’t really say it wasn’t worth it, though. As a result, you gain:

  • Faster runtime execution
  • Faster compilation
  • Smaller executable
  • False conditions (like If/Until/While 0) can be stripped from the final executable

Comments are closed.

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress