Reshaped doc's
[fw/sdcc] / doc / sdccman.html / node28.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2
3 <!--Converted with LaTeX2HTML 99.1 release (March 30, 1999)
4 original version by:  Nikos Drakos, CBLU, University of Leeds
5 * revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
6 * with significant contributions from:
7   Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
8 <HTML>
9 <HEAD>
10 <TITLE>3.9 Naked Functions</TITLE>
11 <META NAME="description" CONTENT="3.9 Naked Functions">
12 <META NAME="keywords" CONTENT="sdccman">
13 <META NAME="resource-type" CONTENT="document">
14 <META NAME="distribution" CONTENT="global">
15
16 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
17 <META NAME="Generator" CONTENT="LaTeX2HTML v99.1 release">
18 <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
19
20 <LINK REL="STYLESHEET" HREF="sdccman.css">
21
22 <LINK REL="next" HREF="node29.html">
23 <LINK REL="previous" HREF="node27.html">
24 <LINK REL="up" HREF="node19.html">
25 <LINK REL="next" HREF="node29.html">
26 </HEAD>
27
28 <BODY >
29 <!--Navigation Panel-->
30 <A NAME="tex2html668"
31  HREF="node29.html">
32 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
33 <A NAME="tex2html662"
34  HREF="node19.html">
35 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
36 <A NAME="tex2html656"
37  HREF="node27.html">
38 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
39 <A NAME="tex2html664"
40  HREF="node1.html">
41 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
42 <A NAME="tex2html666"
43  HREF="node61.html">
44 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
45 <BR>
46 <B> Next:</B> <A NAME="tex2html669"
47  HREF="node29.html">3.10 Functions using private</A>
48 <B> Up:</B> <A NAME="tex2html663"
49  HREF="node19.html">3. Using SDCC</A>
50 <B> Previous:</B> <A NAME="tex2html657"
51  HREF="node27.html">3.8 Critical Functions</A>
52  &nbsp <B>  <A NAME="tex2html665"
53  HREF="node1.html">Contents</A></B> 
54  &nbsp <B>  <A NAME="tex2html667"
55  HREF="node61.html">Index</A></B> 
56 <BR>
57 <BR>
58 <!--End of Navigation Panel-->
59
60 <H2><A NAME="SECTION00049000000000000000">
61 3.9 Naked Functions</A>
62 </H2>
63
64 <P>
65 A special keyword may be associated with a function declaring it as
66 <I>_naked.</I> The <I>_naked</I> function modifier attribute prevents
67 the compiler from generating prologue and epilogue code for that function.
68 This means that the user is entirely responsible for such things as
69 saving any registers that may need to be preserved, selecting the
70 proper register bank, generating the <I>return</I> instruction at
71 the end, etc. Practically, this means that the contents of the function
72 must be written in inline assembler. This is particularly useful for
73 interrupt functions, which can have a large (and often unnecessary)
74 prologue/epilogue. For example, compare the code generated by these
75 two functions:
76 <BR>
77
78 <BR>
79 <TT>data unsigned char counter;</TT>&nbsp;
80 <BR>
81 <TT>void simpleInterrupt(void) interrupt 1</TT>&nbsp;
82 <BR>
83 <TT>{</TT>&nbsp;
84 <BR>
85 <TT>&nbsp;&nbsp;&nbsp;&nbsp;counter++;</TT>&nbsp;
86 <BR>
87 <TT>}</TT>&nbsp;
88 <BR>&nbsp;
89 <BR>
90 <TT>void nakedInterrupt(void) interrupt 2 _naked</TT>&nbsp;
91 <BR>
92 <TT>{</TT>&nbsp;
93 <BR>
94 <TT>&nbsp;&nbsp;&nbsp;&nbsp;_asm</TT>&nbsp;
95 <BR>
96 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_counter</TT>&nbsp;
97 <BR>
98 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reti&nbsp;&nbsp;&nbsp;&nbsp;; MUST explicitly include ret in _naked
99 function.</TT>&nbsp;
100 <BR>
101 <TT>&nbsp;&nbsp;&nbsp;&nbsp;_endasm;</TT>&nbsp;
102 <BR>
103 <TT>}</TT>
104 <BR>
105
106 <BR>
107 For an 8051 target, the generated simpleInterrupt looks like:
108 <BR>
109
110 <BR>
111 <TT>_simpleIterrupt:</TT>&nbsp;
112 <BR>
113 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;acc</TT>&nbsp;
114 <BR>
115 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;b</TT>&nbsp;
116 <BR>
117 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;dpl</TT>&nbsp;
118 <BR>
119 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;dph</TT>&nbsp;
120 <BR>
121 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;psw</TT>&nbsp;
122 <BR>
123 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psw,#0x00</TT>&nbsp;
124 <BR>
125 <TT>&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_counter</TT>&nbsp;
126 <BR>
127 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psw</TT>&nbsp;
128 <BR>
129 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dph</TT>&nbsp;
130 <BR>
131 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpl</TT>&nbsp;
132 <BR>
133 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b</TT>&nbsp;
134 <BR>
135 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc</TT>&nbsp;
136 <BR>
137 <TT>&nbsp;&nbsp;&nbsp;&nbsp;reti</TT>
138 <BR>
139
140 <BR>
141 whereas nakedInterrupt looks like:
142 <BR>
143
144 <BR>
145 <TT>_nakedInterrupt:</TT>&nbsp;
146 <BR>
147 <TT>&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;_counter</TT>&nbsp;
148 <BR>
149 <TT>&nbsp;&nbsp;&nbsp;&nbsp;reti&nbsp;&nbsp;&nbsp;; MUST explicitly include ret(i) in _naked
150 function.</TT>
151 <BR>
152
153 <BR>
154 While there is nothing preventing you from writing C code inside a
155 _naked function, there are many ways to shoot yourself in the foot
156 doing this, and is is recommended that you stick to inline assembler.
157
158 <P>
159 <HR>
160 <!--Navigation Panel-->
161 <A NAME="tex2html668"
162  HREF="node29.html">
163 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
164 <A NAME="tex2html662"
165  HREF="node19.html">
166 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
167 <A NAME="tex2html656"
168  HREF="node27.html">
169 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
170 <A NAME="tex2html664"
171  HREF="node1.html">
172 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
173 <A NAME="tex2html666"
174  HREF="node61.html">
175 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
176 <BR>
177 <B> Next:</B> <A NAME="tex2html669"
178  HREF="node29.html">3.10 Functions using private</A>
179 <B> Up:</B> <A NAME="tex2html663"
180  HREF="node19.html">3. Using SDCC</A>
181 <B> Previous:</B> <A NAME="tex2html657"
182  HREF="node27.html">3.8 Critical Functions</A>
183  &nbsp <B>  <A NAME="tex2html665"
184  HREF="node1.html">Contents</A></B> 
185  &nbsp <B>  <A NAME="tex2html667"
186  HREF="node61.html">Index</A></B> 
187 <!--End of Navigation Panel-->
188 <ADDRESS>
189 <I>Johan Knol</I>
190 <BR><I>2001-07-13</I>
191 </ADDRESS>
192 </BODY>
193 </HTML>