Download Introduction To The C Language
Transcript
Alias checking on pointer access is an advanced option which can acheive a small code saving on pointer
operations. The NOALIAS control can cause unexpected results in when global or static objects are modified by pointers. In most cases source code that could be upset by this control does not conform to the
MISRA-C guidelines and should therefore not be present anyway. It is recommended that this control is not
used.
Example
Without NOALIAS
struct { int i1; inti2; } *p_struct ;
int val ;
void func1(int *p_val) {
p_struct->i1 = val ;
*p_val = 0 ;
p_struct->i2 = val ;
}
// Read val
// Zero val via pointer
// Read val again. Now it is zero
void func2(void) {
func1(&val) ;
}
With NOALIAS
struct { int i1; inti2; } *p_struct ;
int val ;
void func1(int *p_val) {
p_struct->i1 = val ;
*p_val = 0 ;
p_struct->i2 = val ;
}
//
//
//
//
Read val
Zero val via pointer
Read val again. Value from first read carried forward
so zeroing by pointer in previous line is not seen
void func2(void) {
func1(&val) ;
}
Use static memory for non-register automatics influences how the compiler treats local variables (automatics). Normally C166 will try to put as many local variables into registers (R1-R15) as possible as the
MOV Rw,Rw register-to-register instructions execute in 100ns (@20MHz). All the normal ADD, SUB,
CMP type instructions are available in the register-to-register variety so that any such operation will take just
100ns. Variables that overflow the available local registers are placed on the “User Stack”, as in a PC-type
compiler and are addressed via MOV R1,[R0 + #displacement] type instructions. As a RISC CPU, there
are few “stack-relative” instructions so that operating on user stack variables usually takes several instructions.
A significant performance advantage for interrupt functions or those with a large number of local variables can
therefore be had by forcing the compiler to put locals that cannot fit into registers (R1-R15) into (near) static
RAM segments to create a “compiled” stack, as in the C51 compiler. The common ADD, SUB and CMP
instructions all can operate directly on RAM so that there is little performance loss when compared to
reegister variables.
Note that any functions within modules compiled with this control will no longer be reentrant, thus if enabled
here in the C166 Options menu, no reentrancy will be possible across the entire program which may not be
© Copyright Hitex (UK) Ltd 1999
C166 Introduction
Page 24