Chapter 5
Generated Code Architecture
©
National Instruments Corporation
5-35
else {
total = U->bsb_1;
}
Y->bsb_22_1 = total;
INIT = 0;
Optimizations
When translating a BlockScript block into source code, certain
optimizations are automatically done. These optimization can reduce direct
traceability from the script to the code at the expense of tighter code.
Constant Propagation/Hard-Coding
A local variable that is assigned a constant value is replaced with its value
when code generates. The name of that constant local variable does not
appear in the generated code. If it is important to see the symbolic name in
the generated code, consider using a parameter instead and
-p
option for
code generation.
Dead Code Elimination
Code that is guaranteed to never execute is called dead code. When dead
code is detected (as shown in Example 5-16), that code is not translated into
generated source code. You can use this to your advantage by writing the
script in a more general way, taking advantage of special cases when they
occur while not generating dead code. Dead code is most commonly used
by combining constants and
if
statements. If the guard of the
if
statement
is a constant, then only one of the two branches is translated into generated
code.
Example 5-16
BlockScript Block Code with Dead Code
Inputs: u;
Outputs: y;
float u(y.size),y(:);
float threshold;
threshold = 0.001;
if threshold < 0.005 then
for i = 1:y.size do
y(i) = u(i) / threshold;
endfor;
else