Observe declaration order – Echelon Neuron C User Manual
Page 216

204 Memory
Management
In addition to the code size numbers, all sequences above, except the one for
unsigned short, make use of multiple calls to firmware helper functions. This
implies that the runtime of the code sequence for unsigned short is even more
efficient than it seems at first. Thus, the data type which permits the generation
of the most efficient code is unsigned short. The Neuron Chip and Smart
Transceiver instruction set is inherently most efficient when dealing with 8-bit
unsigned integers.
Also, an awareness of the stack architecture employed in the CPUs of the Neuron
Chip and of the Smart Transceiver can help in understanding how to write code
that can be compiled efficiently (see the
Neuron Assembly Language Reference
for more information about the stack architecture). As a general guideline, you
should keep the total size of active local variables plus parameters under eight
bytes. This restriction permits all local variables and parameters to be accessed
and stored using the smallest possible instructions. The following section,
, explains how the first local variable is accessed more
efficiently, and how you can use this fact.
The Neuron C language permits aggregates, such as arrays, structures, and
unions, to be declared on the local data stack. To the extent that such local
variable aggregates are declared, the compiler uses larger and slower
instructions to access these data items. Therefore, it is best to declare such
variables as static items, rather than as local variables. If you are limited in data
memory, and must declare these aggregates as local variables, then declare them
after the non-aggregate local variables; this declaration order permits the
compiler to use the shorter instructions for the non-aggregates.
Observe Declaration Order
The order of declaration of the automatic variables within a function can have an
effect on code size. The compiler places the first variable declared on the top of
the data stack, the second variable next, and so on. The Neuron C compiler
generates more efficient code to access the topmost variable on the stack,
especially when that variable is an eight bit scalar (int, short, or char). The least
efficient accesses (loads and stores) are to variables deep in the stack. Thus, best
results are generally obtained when the variable used most often is declared first.
For example, consider the following code fragment:
void arrayinit(int a[], int initval,
unsigned count) {
int
j;
unsigned
i;
j = initval;
for (i=0; i a[i] = j; } This function generates 23 bytes. However, if the variable i (which appears in more expressions than j and is thus used more often) were declared first, then the code generated would only be 21 bytes.
}