Use the optional fastaccess feature, Eliminate common sub-expressions – Echelon Neuron C User Manual

Page 217

Advertising
background image

Neuron C Programmer’s Guide

205

Use the Optional fastaccess Feature

Array accesses (both loads and stores) in Neuron C normally use the rules of

ANSI Standard C. These rules permit the array index to be interpreted as a
signed quantity, and furthermore permit the array index to exceed the bounds of

the declared array. These characteristics of array indexing increase the code size

for array references.

It is possible, given the Neuron machine instruction set, to generate better code

for accessing small arrays if the following additional rules are observed:
1

The array index can be promoted to unsigned by the compiler if it is a
signed short.

2

The program never attempts to access outside the bounds of the array,
and never computes the address of an array element outside the bounds

of the array. Computation of such an address is permitted in ANSI C for

the purpose of terminating a loop using a pointer, but using this
technique with fastaccess arrays yields undefined results.

To inform the compiler that these additional rules can be assumed for array

access, include the fastaccess keyword in the definition of such an array. Also,
the total size of the array must be no larger than 254 bytes. The fastaccess

keyword can appear anywhere in the declaration and applies to all arrays in the

declaration. For example, the following declares the arrays a1 and a2 to both be
fastaccess style arrays:

fastaccess int a1[4], a2[12];

You can combine the fastaccess keyword with other declaration syntax, including
network, far, eeprom, and const. Fastaccess arrays can appear on the local

procedure or function stack, as well as in global memory. The fastaccess feature
does not apply to the indexing operator used with a pointer.
One potential drawback to using fastaccess arrays in global memory is that the

linker locates these data items such that they do not span page boundaries (a
memory page consists of 256 bytes). Thus, declaration of

many

global arrays as

fastaccess could cause increased memory use due to possible fragmentation.

Eliminate Common Sub-Expressions

The Neuron C compiler does not automatically eliminate common sub-
expressions. Performing this optimization by hand would, in most cases, reduce

code size. Consider the following Before-and-After example, which saves 4 bytes

of code. The temp variable, in the After example, is declared such that it becomes
the top variable on the stack.
Before (compiles to 28 bytes of code):

int a, b, c, d, e;
void f(void) {
d = (a * 2) + (b * c * 4);
e = a - (b * c * 4);
}

After (compiles to 24 bytes of code):

int a, b, c, d, e;

Advertising