Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > e677bbbdff6d27fe001f15e0ef2bb4cc > files > 249

sdcc-3.0.0-0.fc14.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

<!--Converted with LaTeX2HTML 2008 (1.71)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>3.13.2 Naked Functions</TITLE>
<META NAME="description" CONTENT="3.13.2 Naked Functions">
<META NAME="keywords" CONTENT="sdccman">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">

<META NAME="Generator" CONTENT="LaTeX2HTML v2008">
<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

<LINK REL="STYLESHEET" HREF="sdccman.css">

<LINK REL="next" HREF="node84.html">
<LINK REL="previous" HREF="node82.html">
<LINK REL="up" HREF="node81.html">
<LINK REL="next" HREF="node84.html">
</HEAD>

<BODY >
<!--Navigation Panel-->
<A NAME="tex2html1921"
  HREF="node84.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html1915"
  HREF="node81.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html1909"
  HREF="node82.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html1917"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html1919"
  HREF="node191.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html1922"
  HREF="node84.html">3.13.3 Use of Labels</A>
<B> Up:</B> <A NAME="tex2html1916"
  HREF="node81.html">3.13 Inline Assembler Code</A>
<B> Previous:</B> <A NAME="tex2html1910"
  HREF="node82.html">3.13.1 A Step by</A>
 &nbsp; <B>  <A NAME="tex2html1918"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html1920"
  HREF="node191.html">Index</A></B> 
<BR>
<BR>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION004132000000000000000"></A><A NAME="sub:Naked-Functions"></A><A NAME="2247"></A>
<BR>
3.13.2 Naked Functions
</H2>

<P>
A special keyword may be associated with a function declaring it as
<I>_naked<A NAME="2248"></A><A NAME="2249"></A>.</I> The
<I>_naked</I> function modifier attribute prevents the compiler from
generating prologue<A NAME="2251"></A> and epilogue<A NAME="2252"></A>
code for that function. This means that the user is entirely responsible
for such things as saving any registers that may need to be preserved,
selecting the proper register bank, generating the <I>return</I> instruction
at the end, etc. Practically, this means that the contents of the
function must be written in inline assembler. 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:
<BLOCKQUOTE>
<TT>volatile<A NAME="2255"></A> data unsigned char counter;</TT>&nbsp;
<BR>&nbsp;
<BR><TT>void simpleInterrupt(void) __interrupt<A NAME="2257"></A><A NAME="2258"></A>
(1)</TT>&nbsp;
<BR><TT>{</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;counter++;</TT>&nbsp;
<BR><TT>}</TT>&nbsp;
<BR>&nbsp;
<BR><TT>void nakedInterrupt(void) __interrupt (2) __naked</TT>&nbsp;
<BR><TT>{</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;__asm<A NAME="2265"></A><A NAME="2266"></A></TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_counter ; does not change flags,
no need to save psw</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reti&nbsp;&nbsp;&nbsp;&nbsp;; MUST explicitly include ret or
reti in _naked function.</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;__endasm<A NAME="2269"></A><A NAME="2270"></A>;</TT>&nbsp;
<BR><TT>}</TT>

</BLOCKQUOTE>
For an 8051 target, the generated simpleInterrupt looks like:
<BLOCKQUOTE>
<TT>Note, this is an</TT> <TT><I>outdated</I></TT> <TT>example,
recent versions of SDCC generate</TT>&nbsp;
<BR><TT>the</TT> <TT><I>same</I></TT> <TT>code for simpleInterrupt()
and nakedInterrupt()!</TT>&nbsp;
<BR>&nbsp;
<BR><TT>_simpleInterrupt:</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;acc</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;b</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;dpl</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;dph</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;&nbsp;&nbsp;&nbsp;psw</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psw,#0x00</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_counter</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;psw</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dph</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dpl</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;acc</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;reti</TT>

</BLOCKQUOTE>
whereas nakedInterrupt looks like:
<BLOCKQUOTE>
<TT>_nakedInterrupt:</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;inc&nbsp;&nbsp;&nbsp;&nbsp;_counter ; does not change flags, no
need to save psw</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;reti&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;; MUST explicitly include
ret or reti in _naked function</TT>

</BLOCKQUOTE>
The related directive #pragma exclude<A NAME="2301"></A>
allows a more fine grained control over pushing &amp; popping<A NAME="2302"></A>
the registers.

<P>
While there is nothing preventing you from writing C code inside a
<TT>_naked</TT> function, there are many ways to shoot yourself in
the foot doing this, and it is recommended that you stick to inline
assembler.

<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html1921"
  HREF="node84.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> 
<A NAME="tex2html1915"
  HREF="node81.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> 
<A NAME="tex2html1909"
  HREF="node82.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> 
<A NAME="tex2html1917"
  HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> 
<A NAME="tex2html1919"
  HREF="node191.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> 
<BR>
<B> Next:</B> <A NAME="tex2html1922"
  HREF="node84.html">3.13.3 Use of Labels</A>
<B> Up:</B> <A NAME="tex2html1916"
  HREF="node81.html">3.13 Inline Assembler Code</A>
<B> Previous:</B> <A NAME="tex2html1910"
  HREF="node82.html">3.13.1 A Step by</A>
 &nbsp; <B>  <A NAME="tex2html1918"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html1920"
  HREF="node191.html">Index</A></B> 
<!--End of Navigation Panel-->
<ADDRESS>

2011-03-20
</ADDRESS>
</BODY>
</HTML>