Optimizing
This section contains tips and tricks for optimizing your program.
Constant values
Before the compiler compiles the code to assembly, it will try to replace all constant values with their actual value.
For example, let's say we have the following code:
var a = 1 + 2
Without optimizing, the compiler will generate the following assembly code:
LDIA 1
LDIB 2
ADD
With optimizing, the compiler will replace the constant values with their actual value, and the generated assembly code will be:
LDIA 3
Inline functions
When you declare a function with inline
, the compiler will replace all calls to the function with the function body.
Together with constant values, this can drastically reduce the size of your program.
For example, let's create a program that sets the first pixel on the screen to red:
var screen = create_pointer(53870, 1)
int get_color(int r, int g, int b) => (r / 8 << 10) + (g / 8 << 5) + (b / 8);
screen[0] = get_color(255, 0, 0)
This will produce the following assembly code. Note, this is without the function body:
, Prepare the stack and call the function
LDW
HERE 132 , Label L6
SWPC
LDW
HERE 140 , Label L5
STA 24
BIN 24
JMP
HERE 25 , Yabal:call
, Label L6: Prepare arguments
LDIA 255
STA 7 , [Stack:0]
LDIA 0
STA 8 , [Stack:1]
LDIA 0
STA 9 , [Stack:2]
JMP
HERE 91
, Label L5: Return, load the return value and update the screen
AIN 12 , Yabal:return_value
SWP
LDW
HERE 53871
BNK 1
STAOUT
BNK 0
If we change the function to inline
, the compiler will replace all calls to the function with the function body:
var screen = create_pointer(53870, 1)
inline int get_color(int r, int g, int b) => (r / 8 << 10) + (g / 8 << 5) + (b / 8);
screen[0] = get_color(255, 0, 0)
Because the parameter values are constant, the compiler can calculate the result at compile time, and replace the inline function call with the result.
This will produce the following assembly code:
LDIA 992
SWP
LDW
HERE 53872
BNK 1
STAOUT
BNK 0