Optimisation¶
Optimisation is performed in doptimize.c
and doptimize.h
, because I’m
trying to appeal to an American audience, and it totally isn’t the case that
I messed up the spelling the first time round.
The best way to describe Optimisation is like this: Code Generation is very good at creating bytecode - Optimisation is very good at removing it.
The less instructions in the final bytecode the better. We can reduce the number of instructions by finding specific situations in the bytecode and changing it so it is simpler or simply by removing redundant instructions.
Here is a really basic example of optimisation (this should actually be reduced in the code):
# Instruction
41 ... AND
42 ... NOT
43 ... NOT
44 ... JRCON
There is no point in those 2 NOT instructions being there, they cancel each other out!
Note
Another example is since code generation, for simplicity, only works with full immediate variations of instructions, optimisation tries to reduce those instructions to half or byte immediate variations of the same instruction.
Implementation¶
Each “situation” like the example above is in its own function in
doptimize.h
. Theese functions take in a Sheet
and find their
situations in the code, and reduces the bytecode using a helper function:
-
void
d_optimize_remove_bytecode
(struct _sheet *sheet, size_t start, size_t len) Remove a section of bytecode, and make any adjustments to the data that is nessesary.
- Parameters
sheet
: The sheet containing the bytecode to remove from.start
: The starting index of the bytecode to remove.len
: How many bytes to remove, starting fromstart
.
Which removes len
instructions from the .text
section of sheet
,
starting from the index start
.
The individual functions also return a boolean to say if they found a way to
optimise the bytecode (i,e, they return true
if they changed the
bytecode). This way, you can check for new situations that crop up when
bytecode is removed, but only if bytecode was removed.
All of the optimisations are attempted in one big helper function:
-
void
d_optimize_all
(struct _sheet *sheet) Try and optimise all possible senarios.
- Parameters
sheet
: The sheet containing the bytecode to optimise.