AKD BASIC User Guide | 3 AKDBASICLanguage
folded under certain conditions. For example, in the statement:
const PI = 3.1415926535
Main
Print PI^2
End Main
The value of PI^2 is not computed at run-time. It is detected as a constant value and pre-com-
puted by the compiler as a single literal constant to be printed.
Similarly, the literal constant 3*4*PI in
x = 3 * 4 * PI * x
is folded at compile-time, leaving only one multiplication to be performed at run-time.
However, certain constant expressions are not folded. For example:
x = 3 * PI * x * 4
is computed at run-time, involving 3 multiplications because the analysis of constant expres-
sions does not attempt to exploit algebraic commutativity laws. Since the basic arithmetic oper-
ators are ‘left associative’, you can ensure the best performance by grouping constant factors
together towards the left (or using a new constant definition).
If a function is not referenced (transitively from MAIN, plus any interrupt handlers), the compiler
does not generate code for it. So, you can freely $include libraries with unused code (e.g., a
comprehensive library containing functions supporting several possible axis configurations).
Although the compiler parses and type-checks all the included source, it does not generate
code into the downloaded program.
If select-case cases are all constants, more efficient code is generated. If a case is a variable,
the generated code is equivalent to a string of if-then-else statements for all cases.
If any of the cases is an open-ended range (e.g., is 10), or covers a large range (e.g., 1 to 1000),
a fast table-lookup is generated.
If all of the cases are constant, and can be grouped into locally dense subsets, the fastest pos-
sible code is generated — a binary search of dispatch tables, followed by an indirect jump
through the table. If speed is a consideration, keep your cases constant and close together.
(values form a reasonably dense set.)
The compiler performs limited dead-code elimination based on simple constant analysis. For
example:
const DEBUGGING = FALSE
Main
dim i, sum as integer
for i = 1 to 10
sum = sum + i
if DEBUGGING then print “partial sum is
”;sum
next i
End Main
Since the value of DEBUGGING is FALSE, the compiler recognizes that the printing of the par-
tial sum never happens and does not generate the print statement. This allows you to place
debugging code in strategic locations in your programs and effectively disable it when shipping
a production version (shrinks the size of the generated code).
Kollmorgen™ | March 30, 2012 40