Breakpoint statements
BreakpointStatement = "breakpoint" , ";"
;
Warning
Requires
breakpointpreview feature enabled.
A breakpoint statement compiles to a single LD B, B instruction at that point in the program. Loading B into
itself has no effect, so a program behaves identically with or without breakpoints, including on real hardware.
Many Game Boy emulators can be configured to pause execution when LD B, B executes, making it a software breakpoint
for debugging.
global COUNT: u8;
fn main() {
COUNT = 1;
breakpoint;
COUNT += 1;
}
Breakpoints are never removed or reordered by optimization, and no other code generated by the compiler uses
LD B, B.
Observable state
When execution pauses at a breakpoint, any value that is still used later in the program and is present in a register or memory holds the value the unoptimized program would have computed at that point. Optimization may leave a value absent instead. For example, a value may be folded into a constant or its computation may be combined with a later one, but a value is never replaced by a different intermediate result while it can be observed at a breakpoint.
global N: u8;
global RESULT: u8;
fn main() {
let n = N;
let m = n ^ 3;
let a = n + 5;
breakpoint; // `a` holds `n + 5` here
RESULT = a + m;
}
Without the breakpoint, the compiler is free to compute a + m as (n + m) + 5, so no register would ever hold
n + 5. The breakpoint prevents rewrites like this only where the affected value is observable, so the rest of the
program is optimized as usual. Values that are no longer used after a breakpoint have no such guarantee.
A breakpoint statement is not allowed inside a constant declaration, as constant declarations are evaluated at compile time.