Next: 5.1 Notes on MCS51
Up: SDCC Compiler User Guide
Previous: 4.6 Interfacing with XEmacs.
  Contents
  Index
Here are a few guidelines that will help the compiler generate more
efficient code, some of the tips are specific to this compiler others
are generally good programming practice.
- Use the smallest data type to represent your data-value. If it is
known in advance that the value is going to be less than 256 then
use an 'unsigned char' instead of a 'short' or 'int'.
- Use unsigned when it is known in advance that the value is not going
to be negative. This helps especially if you are doing division or
multiplication.
- NEVER jump into a LOOP.
- Declare the variables to be local whenever possible, especially loop
control variables (induction).
- Since the compiler does not always do implicit integral promotion,
the programmer should do an explicit cast when integral promotion
is required.
- Reducing the size of division, multiplication & modulus operations
can reduce code size substantially. Take the following code for example.
foobar(unsigned int p1, unsigned char ch)
{
unsigned char ch1 = p1 % ch ;
....
}
For the modulus operation the variable ch will be promoted to unsigned
int first then the modulus operation will be performed (this will
lead to a call to support routine _moduint()), and the result will
be casted to a char. If the code is changed to
foobar(unsigned int p1, unsigned char ch)
{
unsigned char ch1 = (unsigned char)p1 % ch ;
....
}
It would substantially reduce the code generated (future versions
of the compiler will be smart enough to detect such optimization opportunities).
- Have a look at the assembly listing to get a ''feeling'' for the
code generation.
Subsections
Next: 5.1 Notes on MCS51
Up: SDCC Compiler User Guide
Previous: 4.6 Interfacing with XEmacs.
  Contents
  Index
Bernhard Held
2003-08-29