Go to single file .html
[fw/sdcc] / doc / SDCCUdoc-8.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML>
3 <HEAD>
4  <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.7">
5  <TITLE>SDCC Compiler User Guide: Parameters &amp; Local Variables</TITLE>
6  <LINK HREF="SDCCUdoc-9.html" REL=next>
7  <LINK HREF="SDCCUdoc-7.html" REL=previous>
8  <LINK HREF="SDCCUdoc.html#toc8" REL=contents>
9 </HEAD>
10 <BODY>
11 <A HREF="SDCCUdoc-9.html">Next</A>
12 <A HREF="SDCCUdoc-7.html">Previous</A>
13 <A HREF="SDCCUdoc.html#toc8">Contents</A>
14 <HR>
15 <H2><A NAME="Auto Variables"></A> <A NAME="s8">8. Parameters &amp; Local Variables</A> </H2>
16
17 <P>Automatic (local) variables and parameters to functions can either be placed
18 on the stack or in data-space. The default action of the compiler is to place
19 these variables in the internal RAM ( for small model) or external RAM (for
20 Large model). They can be placed on the stack either by using the --stack-auto
21 compiler option or by using the 'reentrant' keyword in the function declaration.
22 <P><CODE>eg</CODE>
23 <P>
24 <PRE>
25 unsigned short foo( short i) reentrant { 
26 ... 
27 }
28  
29 </PRE>
30 <P>Note that when the parameters &amp; local variables are declared in the
31 internal/external ram the functions are non-reentrant. Since stack space on
32 8051 is limited the 'reentrant' keyword or the --stack-auto option should be
33 used sparingly. Note the reentrant keyword just means that the parameters &amp;
34 local variables will be allocated to the stack, it DOES NOT mean that the function
35 is register bank independent.
36 <P>When compiled with the default option (i.e. non-reentrant ), local variables
37 can be assigned storage classes and absolute addresses. 
38 <P><CODE>eg</CODE>
39 <P>
40 <PRE>
41 unsigned short foo() { 
42    xdata unsigned short i; 
43    bit bvar; 
44  
45   data at 0x31 unsiged short j; 
46 ... 
47 }
48  
49 </PRE>
50 <P>In the above example the variable i will be allocated in the external ram,
51 bvar in bit addressable space and j in internal ram. When compiled with the
52 --stack-auto or when a function is declared as 'reentrant' local variables
53 cannot be assigned storage classes or absolute addresses.
54 <P>Parameters however are not allowed any storage class, (storage classes
55 for parameters will be ignored), their allocation is governed by the memory
56 model in use , and the reentrancy options.
57 <H2><A NAME="Overlaying"></A> <A NAME="ss8.1">8.1 Overlaying</A>
58  </H2>
59
60 <P>For non-reentrant functions SDCC will try to reduce internal ram space
61 usage by overlaying parameters and local variables of a function (if possible).
62 Parameters and local variables of a function will be allocated to an overlayable
63 segment if the function has no other function calls and the function is non-reentrant
64 and the memory model is small. If an explicit storage class is specified for
65 a local variable , it will NOT be overplayed.
66 <P>Note that the compiler (not the linkage editor) makes the decision for
67 overlaying the data items. Functions that are called from an interrupt service
68 routine should be preceded by a #pragma NOOVERLAY if they are not reentrant
69 Along the same lines the compiler does not do any processing with the inline
70 assembler code so the compiler might incorrectly assign local variables and
71 parameters of a function into the overlay segment if the only function call
72 from a function is from inline assembler code, it is safe to use the #pragma
73 NOOVERLAY for functions which call other functions using inline assembler code.
74 <P>Parameters and Local variables of functions that contain 16 or 32 bit multiplication
75 or division will NOT be overlayed since these are implemented using external
76 functions.
77 <P>eg.
78 <P>
79 <PRE>
80 #pragma SAVE 
81 #pragma NOOVERLAY 
82 void set_error( unsigned short
83  errcd) 
84
85     P3 = errcd; 
86
87 #pragma RESTORE 
88 void some_isr
89  () interrupt 2 using 1 
90
91     ... 
92     set_error(10); 
93     ... 
94 }
95  
96 </PRE>
97 <P>In the above example the parameter errcd for the function set_error would
98 be assigned to the overlayable segment (if the #pragma NOOVERLAY was not
99 present) , this could cause unpredictable runtime behavior. The pragma NOOVERLAY
100 ensures that the parameters and local variables for the function are NOT overlayed.
101 <HR>
102 <A HREF="SDCCUdoc-9.html">Next</A>
103 <A HREF="SDCCUdoc-7.html">Previous</A>
104 <A HREF="SDCCUdoc.html#toc8">Contents</A>
105 </BODY>
106 </HTML>