ascii
\series medium
editor:
-\series default
+\layout Verse
-\newline
\family typewriter
-
-\newline
char test;
\newline
test=0;
\newline
}
-\family default
-
-\newline
-
-\emph on
+\layout Standard
-\newline
\series medium
-\emph default
Compile this using the following command:
\family sans
\series bold
\series medium
header files and libraries.
Edit test.c and change it to the following:
-\series default
-
-\newline
+\layout Verse
-\newline
\family typewriter
#include <string.h>
strcpy(str1, "testing");
\newline
}
-\newline
+\layout Standard
-\newline
-\family default
\series medium
Compile this by typing
\family sans
storage class for Small Memory model.
Variables declared with this storage class will be allocated in the internal
RAM, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
data int iramdata;
Variables declared with this storage class will be allocated into the indirectly
addressable portion of the internal ram of a 8051, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
idata int idi;
This is a data-type and a storage class specifier.
When a variable is declared as a bit, it is allocated into the bit addressable
memory of 8051, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
bit iFlag;
\emph default
signifies both a data-type and storage class, they are used to describe
the special function registers and special bit variables of a 8051, eg:
-\newline
+\layout Verse
-\newline
\family typewriter
sfr at 0x80 P0; /* special function register P0 at location 0x80 */
\newline
Pointer declaration examples:
-\newline
-
-\size small
+\layout Verse
-\newline
\family typewriter
-\size default
/* pointer physically in xternal ram pointing to object in internal ram
*/
\newline
/* the folowing is a generic pointer physically located in xdata space */
\newline
char * xdata p;
-\family default
-\size small
-
-\newline
-
-\newline
+\layout Standard
-\size default
Well you get the idea.
\newline
\emph default
keyword in the function declaration, e.g.:
-\newline
-
-\size small
+\layout Verse
-\newline
\family typewriter
-\size default
unsigned char foo(char i) reentrant
\newline
{
\newline
}
-\newline
-
-\family default
+\layout Standard
-\newline
Since stack space on 8051 is limited, the
\emph on
reentrant
\end_inset
addresses, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
unsigned char foo() {
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
In the above example the variable
\emph on
i
will NOT be overlayed since these are implemented using external functions,
e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
#pragma SAVE
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
In the above example the parameter
\emph on
errcd
SDCC allows interrupt service routines to be coded in C, with some extended
keywords.
-\newline
+\layout Verse
-\newline
\family typewriter
void timer_isr (void) interrupt 2 using 1
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
The number following the
\emph on
interrupt
SDCC will generate code to disable all interrupts upon entry to a critical
function and enable them back before returning.
Note that nesting critical functions may cause unpredictable results.
-\newline
-
-\size small
+\layout Verse
-\newline
\family typewriter
-\size default
int foo () critical
\newline
{
\newline
}
-\newline
-
-\family default
+\layout Standard
-\newline
The critical attribute maybe used with other attributes like
\emph on
reentrant.
This is particularly useful for interrupt functions, which can have a large
(and often unnecessary) prologue/epilogue.
For example, compare the code generated by these two functions:
-\newline
+\layout Verse
-\newline
\family typewriter
data unsigned char counter;
;
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
For an 8051 target, the generated simpleInterrupt looks like:
-\newline
+\layout Verse
-\newline
\family typewriter
_simpleIterrupt:
\SpecialChar ~
\SpecialChar ~
reti
-\family default
-
-\newline
+\layout Standard
-\newline
whereas nakedInterrupt looks like:
-\newline
+\layout Verse
-\newline
\family typewriter
_nakedInterrupt:
\SpecialChar ~
\SpecialChar ~
; MUST explicitly include ret(i) in _naked function.
-\family default
-
-\newline
+\layout Standard
-\newline
While there is nothing preventing you from writing C code inside a _naked
function, there are many ways to shoot yourself in the foot doing this,
and it is recommended that you stick to inline assembler.
SDCCpeeph.def
\emph default
carefully before using this option.
-\newline
+\layout Verse
-\newline
\family typewriter
_asm
\end_inset
;
-\family default
-\size small
-
-\newline
-
-\newline
+\layout Standard
-\size default
The inline assembler code can contain any valid code understood by the assembler
, this includes any assembler directives and comment lines.
The compiler does not do any validation of the code within the
\end_inset
defined by the inline assembler, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
foo() {
/* some more c code */
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
In other words inline assembly code can access labels defined in inline
assembly within the scope of the funtion.
The same goes the other way, ie.
The compiler does local and global common subexpression elimination, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
i = x + y + 1;
\newline
j = x + y;
-\family default
-
-\newline
+\layout Standard
-\newline
will be translated to
-\newline
+\layout Verse
-\newline
\family typewriter
iTemp = x + y
i = iTemp + 1
\newline
j = iTemp
-\newline
-
-\family default
+\layout Standard
-\newline
Some subexpressions are not as obvious as the above example, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
a->b[i].c = 10;
\newline
a->b[i].d = 11;
-\family default
-
-\newline
+\layout Standard
-\newline
In this case the address arithmetic a->b[i] will be computed only once;
the equivalent code in C would be.
-\newline
+\layout Verse
-\newline
\family typewriter
iTemp = a->b[i];
iTemp.c = 10;
\newline
iTemp.d = 11;
-\family default
-
-\newline
+\layout Standard
-\newline
The compiler will try to keep these temporary variables in registers.
\layout Subsubsection
\end_inset
-\layout Standard
+\layout Verse
\family typewriter
/* unreachable */
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
will be changed to
-\newline
+\layout Verse
-\newline
\family typewriter
int global; void f ()
\end_inset
-\layout Standard
+\layout Verse
\family typewriter
return j;
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
will be changed to
-\newline
+\layout Verse
-\newline
\family typewriter
int f() {
return 10;
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
Note: the dead stores created by this copy propagation will be eliminated
by dead-code elimination.
\layout Subsubsection
\newline
Loop Invariant:
-\newline
+\layout Verse
-\newline
\family typewriter
for (i = 0 ; i < 100 ; i ++)
\SpecialChar ~
\SpecialChar ~
f += k + l;
-\family default
-
-\newline
+\layout Standard
-\newline
changed to
-\newline
+\layout Verse
-\newline
\family typewriter
itemp = k + l;
\SpecialChar ~
\SpecialChar ~
f += itemp;
-\family default
-
-\newline
+\layout Standard
-\newline
As mentioned previously some loop invariants are not as apparent, all static
address computations are also moved out of the loop.
\newline
\end_inset
, this optimization substitutes an expression by a cheaper expression:
-\newline
+\layout Verse
-\newline
\family typewriter
for (i=0;i < 100; i++)
\SpecialChar ~
\SpecialChar ~
ar[i*5] = i*3;
-\family default
-
-\newline
+\layout Standard
-\newline
changed to
-\newline
+\layout Verse
-\newline
\family typewriter
itemp1 = 0;
itemp2 += 3;
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
The more expensive multiplication
\begin_inset LatexCommand \index{Multiplication}
\newline
\family typewriter
-for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
- <sym> += 1])
+for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
+ += 1])
\newline
\SpecialChar ~
\SpecialChar ~
SDCC does numerous algebraic simplifications, the following is a small sub-set
of these optimizations.
-\newline
+\layout Verse
-\newline
\family typewriter
i = j + 0 ; /* changed to */ i = j;
i = j - j ; /* changed to */ i = 0;
\newline
i = j / 1 ; /* changed to */ i = j;
-\family default
-
-\newline
+\layout Standard
-\newline
Note the subexpressions
\begin_inset LatexCommand \index{Subexpression}
The case labels are in numerical sequence, the labels need not be in order,
and the starting number need not be one or zero.
-\newline
+\begin_deeper
+\layout Verse
-\newline
\family typewriter
switch(i) {\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
}
-\newline
-
-\newline
+\layout Standard
-\family default
Both the above switch statements will be implemented using a jump-table.
+\end_deeper
\layout Itemize
The number of case labels is at least three, since it takes two conditional
Switch statements which have gaps in the numeric sequence or those that
have more that 84 case labels can be split into more than one switch statement
for efficient code generation, e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
switch (i) {
\newline
+\SpecialChar ~
+\SpecialChar ~
case 1: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 2: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 3: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 4: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 9: ...
\newline
-case 10: ...
+\SpecialChar ~
+\SpecialChar ~
+case 10: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 11: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 12: ...
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
If the above switch statement is broken down into two switch statements
-\newline
+\layout Verse
-\newline
\family typewriter
switch (i) {
\newline
+\SpecialChar ~
+\SpecialChar ~
case 1: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 2: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 3: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 4: ...
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
and
-\family typewriter
+\layout Verse
-\newline
-\newline
+\family typewriter
switch (i) {
\newline
+\SpecialChar ~
+\SpecialChar ~
case 9: \SpecialChar ~
...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 10: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 11: ...
\newline
+\SpecialChar ~
+\SpecialChar ~
case 12:\SpecialChar ~
...
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
then both the switch statements will be implemented using jump-tables whereas
the unmodified switch statement will not be.
\layout Subsubsection
g.
SDCC tries to implement bit-shift operations in the most efficient way
possible, e.g.:
-\newline
+\layout Verse
-\family typewriter
-\newline
+\family typewriter
unsigned char i;
\newline
...
i>>= 4;
\newline
...
-\newline
-
-\family default
+\layout Standard
-\newline
generates the following code:
-\newline
+\layout Verse
-\family typewriter
-\newline
+\family typewriter
mov a,_i
\newline
swap a
anl a,#0x0f
\newline
mov _i,a
-\family default
-
-\newline
+\layout Standard
-\newline
In general SDCC will never setup a loop if the shift count is known.
Another example:
-\newline
+\layout Verse
-\newline
\family typewriter
unsigned int i;
i >>= 9;
\newline
...
-\family default
-
-\newline
+\layout Standard
-\newline
will generate:
-\newline
+\layout Verse
-\newline
\family typewriter
mov a,(_i + 1)
rrc a
\newline
mov _i,a
-\family default
-
-\newline
+\layout Standard
-\newline
Note that SDCC stores numbers in little-endian format (i.e.
lowest order first).
\layout Subsubsection
A special case of the bit-shift operation is bit rotation, SDCC recognizes
the following expression to be a left bit-rotation:
-\newline
+\layout Verse
-\newline
\family typewriter
unsigned char i;
\family default
\newline
+
+\family typewriter
...
-\newline
+\layout Standard
-\newline
will generate the following code:
-\newline
+\layout Verse
-\newline
\family typewriter
mov a,_i
rl a
\newline
mov _i,a
-\family default
-
-\newline
+\layout Standard
-\newline
SDCC uses pattern matching on the parse tree to determine this operation.Variatio
ns of this case will also be recognized as bit-rotation, i.e.:
-\newline
+\layout Verse
-\newline
\family typewriter
i = ((i >> 7) | (i << 1)); /* left-bit rotation */
type (long, int, short or char types).
SDCC recognizes the following expression to yield the highest order bit
and generates optimized code for it, e.g.:
-\newline
+\layout Verse
+
-\newline
-
\family typewriter
unsigned int gint;
\newline
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
will generate the following code:
-\newline
+\layout Verse
-\family typewriter
-\newline
-\SpecialChar ~
-\SpecialChar ~
-\SpecialChar ~
+\family typewriter
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
61 ;\SpecialChar ~
hob.c 7
\newline
-\SpecialChar ~
-\SpecialChar ~
- 000A E5*01\SpecialChar ~
+000A E5*01\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
mov\SpecialChar ~
a,(_gint + 1)
\newline
-\SpecialChar ~
-\SpecialChar ~
- 000C 33\SpecialChar ~
+000C 33\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
rlc\SpecialChar ~
a
\newline
-\SpecialChar ~
-\SpecialChar ~
- 000D E4\SpecialChar ~
+000D E4\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
clr\SpecialChar ~
a
\newline
-\SpecialChar ~
-\SpecialChar ~
- 000E 13\SpecialChar ~
+000E 13\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
rrc\SpecialChar ~
a
\newline
-\SpecialChar ~
-\SpecialChar ~
- 000F F5*02\SpecialChar ~
+000F F5*02\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
\SpecialChar ~
mov\SpecialChar ~
_foo_hob_1_1,a
-\newline
-
-\newline
+\layout Standard
-\family default
Variations of this case however will
\emph on
not
way to get the highest order bit, (it is portable).
Of course it will be recognized even if it is embedded in other expressions,
e.g.:
-\newline
+\layout Verse
-\newline
\family typewriter
xyz = gint + ((gint >> 15) & 1);
-\family default
-
-\newline
+\layout Standard
-\newline
will still be recognized.
\layout Subsubsection
\emph default
option.
The rule language is best illustrated with examples.
-\newline
+\layout Verse
-\newline
\family typewriter
replace {
mov %1,a
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
The above rule will change the following assembly
\begin_inset LatexCommand \index{Assembler routines}
\end_inset
sequence:
-\newline
+\layout Verse
-\newline
\family typewriter
-\SpecialChar ~
-\SpecialChar ~
mov r1,a
\newline
-\SpecialChar ~
-\SpecialChar ~
mov a,r1
-\family default
-
-\newline
+\layout Standard
-\newline
to
-\newline
+\layout Verse
-\newline
\family typewriter
mov r1,a
-\family default
-
-\newline
+\layout Standard
-\newline
Note: All occurrences of a
\emph on
%n
\emph default
(pattern variable) must denote the same string.
With the above rule, the assembly sequence:
-\newline
+\layout Verse
-\newline
\family typewriter
-\SpecialChar ~
-\SpecialChar ~
mov r1,a
\newline
-\SpecialChar ~
-\SpecialChar ~
mov a,r2
-\family default
-
-\newline
+\layout Standard
-\newline
will remain unmodified.
\newline
and
\family typewriter
acall
-\family default
-
-\newline
+\layout Verse
-\newline
\family typewriter
replace { lcall %1 } by { acall %1 }
\newline
replace { ljmp %1 } by { ajmp %1 }
-\family default
-
-\newline
+\layout Standard
-\newline
The
\emph on
inline-assembler code
\newline
The syntax for a rule is as follows:
-\newline
+\layout Verse
-\newline
\family typewriter
rule := replace [ restart ] '{' <assembly sequence> '
'}' [if <functionName> ] '
\backslash
n'
-\newline
-
-\family default
+\layout Standard
-\newline
<assembly sequence> := assembly instruction (each instruction including
labels must be on a separate line).
\newline
the same rule again.
An example of this (not a good one, it has side effects) is the following
rule:
-\newline
+\layout Verse
-\newline
\family typewriter
replace restart {
; nop
\newline
}
-\family default
-
-\newline
+\layout Standard
-\newline
Note that the replace pattern cannot be a blank, but can be a comment line.
Without the 'restart' option only the inner most 'pop' 'push' pair would
be eliminated, i.e.:
-\newline
+\layout Verse
-\newline
\family typewriter
-\SpecialChar ~
-\SpecialChar ~
pop ar1
\newline
-\SpecialChar ~
-\SpecialChar ~
pop ar2
\newline
-\SpecialChar ~
-\SpecialChar ~
push ar2
\newline
-\SpecialChar ~
-\SpecialChar ~
push ar1
-\family default
-
-\newline
+\layout Standard
-\newline
would result in:
-\newline
+\layout Verse
-\newline
\family typewriter
-\SpecialChar ~
-\SpecialChar ~
pop ar1
\newline
-\SpecialChar ~
-\SpecialChar ~
; nop
\newline
-\SpecialChar ~
-\SpecialChar ~
push ar1
-\family default
-
-\newline
+\layout Standard
-\newline
\emph on
with
\emph default
the restart option the rule will be applied again to the resulting code
and then all the pop-push pairs will be eliminated to yield:
-\newline
+\layout Verse
-\newline
\family typewriter
-\SpecialChar ~
-\SpecialChar ~
; nop
\newline
-\SpecialChar ~
-\SpecialChar ~
; nop
-\family default
-
-\newline
+\layout Standard
-\newline
A conditional function can be attached to a rule.
Attaching rules are somewhat more involved, let me illustrate this with
an example.
-\newline
+\layout Verse
-\newline
\family typewriter
replace {
%2:
\newline
} if labelInRange
-\family default
-
-\newline
+\layout Standard
-\newline
The optimizer does a look-up of a function name table defined in function
\emph on
are used to control options & optimizations for a given function; pragmas
should be placed before and/or after a function, placing pragma's inside
a function body could have unpredictable results.
-\newline
+\layout Verse
-\newline
\family typewriter
#pragma SAVE
\end_inset
/* turn the optimizations back on */
-\family default
-
-\newline
+\layout Standard
-\newline
The compiler will generate a warning message when extra space is allocated.
It is strongly recommended that the SAVE and RESTORE pragma's be used when
changing options for a function.
In the following example the function cfunc calls an assembler routine asm_func,
which takes two parameters.
-\newline
+\layout Verse
-\newline
\family typewriter
extern int asm_func(unsigned char, unsigned char);
return c_func(10,9);
\newline
}
-\newline
-
-\newline
+\layout Standard
-\family default
The corresponding assembler function is:
-\newline
+\layout Verse
-\newline
\family typewriter
.globl _asm_func_PARM_2
\SpecialChar ~
\SpecialChar ~
ret
-\newline
-
-\newline
+\layout Standard
-\family default
Note here that the return values are placed in 'dpl' - One byte return value,
'dpl' LSB & 'dph' MSB for two byte values.
'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
parameters are pushed from right to left i.e.
after the call the left most parameter will be on the top of the stack.
Here is an example:
-\newline
+\layout Verse
-\newline
\family typewriter
extern int asm_func(unsigned char, unsigned char);
return c_func(10,9);
\newline
}
-\newline
-
-\family default
+\layout Standard
-\newline
The corresponding assembler routine is:
-\newline
+\layout Verse
-\newline
\family typewriter
.globl _asm_func
\SpecialChar ~
\SpecialChar ~
ret
-\newline
-
-\newline
+\layout Standard
-\family default
The compiling and linking procedure remains the same, however note the extra
entry & exit linkage required for the assembler code, _bp is the stack
frame pointer and is used to compute the offset into the stack for parameters
structures cannot be assigned values directly, cannot be passed as function
parameters or assigned to each other and cannot be a return value from
a function, e.g.:
-\family typewriter
+\begin_deeper
+\layout Verse
-\newline
-\newline
+\family typewriter
struct s { ...
};
\newline
\newline
}
\newline
-struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
- ANSI */
+struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
+ */
\newline
{
\newline
return rets;/* is invalid in SDCC although allowed in ANSI */
\newline
}
+\end_deeper
\layout Itemize
'long long
\end_inset
function declarations are NOT allowed.
-\newline
+\begin_deeper
+\layout Verse
-\family typewriter
-\newline
+\family typewriter
foo(i,j) /* this old style of function declarations */
\newline
int i,j; /* are valid in ANSI but not valid in SDCC */
\newline
}
+\end_deeper
\layout Itemize
functions declared as pointers must be dereferenced during the call.
-\newline
+\begin_deeper
+\layout Verse
-\family typewriter
-\newline
+\family typewriter
int (*foo)();
\newline
...
/* has to be called like this */
\newline
(*foo)(); /* ansi standard allows calls to be made like 'foo()' */
+\end_deeper
\layout Subsection
Cyclomatic Complexity
Reducing the size of division, multiplication & modulus operations can reduce
code size substantially.
Take the following code for example.
-\family typewriter
+\begin_deeper
+\layout Verse
-\newline
-\newline
+\family typewriter
foobar(unsigned int p1, unsigned char ch)
\newline
{
\newline
- unsigned char ch1 = p1 % ch ;
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+unsigned char ch1 = p1 % ch ;
\newline
- ....
-
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+....
\newline
}
-\newline
-
-\family default
+\layout Standard
-\newline
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
-\newline
+\layout Verse
-\family typewriter
-\newline
+\family typewriter
foobar(unsigned int p1, unsigned char ch)
\newline
{
\newline
- unsigned char ch1 = (unsigned char)p1 % ch ;
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+unsigned char ch1 = (unsigned char)p1 % ch ;
\newline
- ....
-
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+\SpecialChar ~
+....
\newline
}
-\newline
-
-\family default
+\layout Standard
-\newline
It would substantially reduce the code generated (future versions of the
- compiler will be smart enough to detect such optimization oppurtunities).
+ compiler will be smart enough to detect such optimization opportunities).
+\end_deeper
\layout Subsection
Notes on MCS51 memory
This section shows some details of iCode.
The example C code does not do anything useful; it is used as an example
to illustrate the intermediate code generated by the compiler.
-\newline
+\layout Verse
-\newline
\family typewriter
1.\SpecialChar ~
\newline
21.\SpecialChar ~
}
-\newline
-
-\newline
+\layout Standard
-\family default
In addition to the operands each iCode contains information about the filename
and line it corresponds to in the source file.
The first field in the listing should be interpreted as follows: