1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={68hc08 8032 8051 ansi c compiler assembler CPU DS390 embedded development free Floating Point Arithmetic Freescale GPL HC08 inline Intel ISO/IEC 9899:1990 Linux MAC OS X manual Maxim mcs51 Microchip microcontroller open source PIC Unix Windows Z80 Zilog},
11 pdfpagemode=UseOutlines,
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by Subversion
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 Zilog Z80 based MCUs.
145 It can be retargeted for other microprocessors, support for Microchip PIC,
146 Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargetable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
209 <lyxtabular version="3" rows="8" columns="5">
211 <column alignment="center" valignment="top" leftline="true" width="0">
212 <column alignment="center" valignment="top" leftline="true" width="0">
213 <column alignment="center" valignment="top" leftline="true" width="0">
214 <column alignment="center" valignment="top" leftline="true" width="0">
215 <column alignment="block" valignment="top" leftline="true" rightline="true" width="20text%">
216 <row topline="true" bottomline="true">
217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
426 <row topline="true" bottomline="true">
427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
456 -2.147.483.648, +2.147.483.647
459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
468 <row topline="true" bottomline="true">
469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
518 <row topline="true" bottomline="true">
519 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
527 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
535 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
550 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
564 The compiler also allows
566 inline assembler code
568 to be embedded anywhere in a function.
569 In addition, routines developed in assembly can also be called.
573 SDCC also provides an option (-
583 -cyclomatic) to report the relative complexity of a function.
584 These functions can then be further optimized, or hand coded in assembly
590 SDCC also comes with a companion source level debugger SDCDB, the debugger
591 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.<
595 The latest version can be downloaded from
596 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
606 Please note: the compiler will probably always be some steps ahead of this
611 \begin_inset LatexCommand \index{Status of documentation}
621 Obviously this has pros and cons
630 All packages used in this compiler system are
638 ; source code for all the sub-packages (pre-processor, assemblers, linkers
639 etc) is distributed with the package.
640 This documentation is maintained using a freeware word processor (LyX).
642 This program is free software; you can redistribute it and/or modify it
643 under the terms of the GNU General Public License
644 \begin_inset LatexCommand \index{GNU General Public License, GPL}
648 as published by the Free Software Foundation; either version 2, or (at
649 your option) any later version.
650 This program is distributed in the hope that it will be useful, but WITHOUT
651 ANY WARRANTY; without even the implied warranty
652 \begin_inset LatexCommand \index{warranty}
656 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
657 See the GNU General Public License for more details.
658 You should have received a copy of the GNU General Public License along
659 with this program; if not, write to the Free Software Foundation, 59 Temple
660 Place - Suite 330, Boston, MA 02111-1307, USA.
661 In other words, you are welcome to use, share and improve this program.
662 You are forbidden to forbid anyone else to use, share and improve what
664 Help stamp out software-hoarding!
667 Typographic conventions
668 \begin_inset LatexCommand \index{Typographic conventions}
675 Throughout this manual, we will use the following convention.
676 Commands you have to type in are printed in
684 Code samples are printed in
689 Interesting items and new terms are printed in
695 \begin_inset LatexCommand \label{sec:Compatibility-with-previous}
699 with previous versions
700 \begin_inset LatexCommand \index{Compatibility with previous versions}
707 This version has numerous bug fixes compared with the previous version.
708 But we also introduced some incompatibilities with older versions.
709 Not just for the fun of it, but to make the compiler more stable, efficient
711 \begin_inset LatexCommand \index{ANSI-compliance}
716 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
720 for ANSI-Compliance).
726 short is now equivalent to int (16 bits), it used to be equivalent to char
727 (8 bits) which is not ANSI compliant.
730 the default directory for gcc-builds where include, library and documentation
731 files are stored is now in /usr/local/share.
734 char type parameters to vararg
735 \begin_inset LatexCommand \index{vararg, va\_arg}
739 functions are casted to int unless explicitly casted
740 \begin_inset Marginal
767 will push a as an int and as a char resp.
780 -regextend has been removed.
793 -noregparms has been removed.
806 -stack-after-data has been removed.
810 \begin_inset LatexCommand \index{bit}
815 \begin_inset LatexCommand \index{sbit}
820 \begin_inset LatexCommand \index{\_\_sbit}
824 types now consistently behave like the C99 _Bool type with respect to type
826 \begin_inset LatexCommand \index{type conversion}
831 \begin_inset LatexCommand \index{type promotion}
836 The most common incompatibility resulting from this change is related to
838 \begin_inset LatexCommand \index{Bit toggling}
853 \begin_inset LatexCommand \index{\~\/ Operator}
857 b; /* equivalent to b=1 instead of toggling b */
858 \begin_inset Marginal
873 b = !b; /* toggles b */
877 In previous versions, both forms would have toggled the bit.
882 <pending: more incompatibilities?>
888 What do you need before you start installation of SDCC? A computer, and
890 The preferred method of installation is to compile SDCC from source using
892 For Windows some pre-compiled binary distributions are available for your
894 You should have some experience with command line tools and compiler use.
900 The SDCC home page at
901 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
905 is a great place to find distribution sets.
906 You can also find links to the user mailing lists that offer help or discuss
907 SDCC with other SDCC users.
908 Web links to other SDCC related sites can also be found here.
909 This document can be found in the DOC directory of the source package as
911 A pdf version of this document is available at
912 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
917 Some of the other tools (simulator and assembler) included with SDCC contain
918 their own documentation and can be found in the source distribution.
919 If you want the latest unreleased software, the complete source package
920 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
924 Wishes for the future
927 There are (and always will be) some things that could be done.
928 Here are some I can think of:
935 char KernelFunction3(char p) at 0x340;
943 \begin_inset LatexCommand \index{code banking (limited support)}
953 If you can think of some more, please see the section
954 \begin_inset LatexCommand \ref{sub:Requesting-Features}
958 about filing feature requests
959 \begin_inset LatexCommand \index{Requesting features}
964 \begin_inset LatexCommand \index{Feature request}
974 \begin_inset LatexCommand \index{Installation}
981 For most users it is sufficient to skip to either section
982 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
987 \begin_inset LatexCommand \ref{sub:Windows-Install}
992 More detailed instructions follow below.
996 \begin_inset LatexCommand \index{Options SDCC configuration}
1003 The install paths, search paths and other options are defined when running
1005 The defaults can be overridden by:
1007 \labelwidthstring 00.00.0000
1019 -prefix see table below
1021 \labelwidthstring 00.00.0000
1033 -exec_prefix see table below
1035 \labelwidthstring 00.00.0000
1047 -bindir see table below
1049 \labelwidthstring 00.00.0000
1061 -datadir see table below
1063 \labelwidthstring 00.00.0000
1065 docdir environment variable, see table below
1067 \labelwidthstring 00.00.0000
1069 include_dir_suffix environment variable, see table below
1071 \labelwidthstring 00.00.0000
1073 lib_dir_suffix environment variable, see table below
1075 \labelwidthstring 00.00.0000
1077 sdccconf_h_dir_separator environment variable, either / or
1082 This character will only be used in sdccconf.h; don't forget it's a C-header,
1083 therefore a double-backslash is needed there.
1085 \labelwidthstring 00.00.0000
1097 -disable-mcs51-port Excludes the Intel mcs51 port
1099 \labelwidthstring 00.00.0000
1111 -disable-gbz80-port Excludes the Gameboy gbz80 port
1113 \labelwidthstring 00.00.0000
1125 -disable-z80-port Excludes the z80 port
1127 \labelwidthstring 00.00.0000
1139 -disable-avr-port Excludes the AVR port
1141 \labelwidthstring 00.00.0000
1153 -disable-ds390-port Excludes the DS390 port
1155 \labelwidthstring 00.00.0000
1167 -disable-hc08-port Excludes the HC08 port
1169 \labelwidthstring 00.00.0000
1181 -disable-pic-port Excludes the PIC port
1183 \labelwidthstring 00.00.0000
1195 -disable-xa51-port Excludes the XA51 port
1197 \labelwidthstring 00.00.0000
1209 -disable-ucsim Disables configuring and building of ucsim
1211 \labelwidthstring 00.00.0000
1223 -disable-device-lib Disables automatically building device libraries
1225 \labelwidthstring 00.00.0000
1237 -disable-packihx Disables building packihx
1239 \labelwidthstring 00.00.0000
1251 -enable-doc Build pdf, html and txt files from the lyx sources
1253 \labelwidthstring 00.00.0000
1265 -enable-libgc Use the Bohem memory allocator.
1266 Lower runtime footprint.
1269 Furthermore the environment variables CC, CFLAGS, ...
1270 the tools and their arguments can be influenced.
1271 Please see `configure -
1281 -help` and the man/info pages of `configure` for details.
1285 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1286 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1287 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1288 At the moment it's not possible to change the default settings (it was
1289 simply never required).
1293 These configure options are compiled into the binaries, and can only be
1294 changed by rerunning 'configure' and recompiling SDCC.
1295 The configure options are written in
1299 to distinguish them from run time environment variables (see section search
1305 \begin_inset Quotes sld
1309 \begin_inset Quotes srd
1312 are used by the SDCC team to build the official Win32 binaries.
1313 The SDCC team uses Mingw32 to build the official Windows binaries, because
1320 a gcc compiler and last but not least
1323 the binaries can be built by cross compiling on Sourceforge's compile farm.
1326 See the examples, how to pass the Win32 settings to 'configure'.
1327 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1328 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1339 \begin_inset Tabular
1340 <lyxtabular version="3" rows="8" columns="3">
1342 <column alignment="block" valignment="top" leftline="true" width="0in">
1343 <column alignment="block" valignment="top" leftline="true" width="0in">
1344 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1345 <row topline="true" bottomline="true">
1346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1371 <row topline="true">
1372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1390 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1401 <row topline="true">
1402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1422 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1433 <row topline="true">
1434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1471 <row topline="true">
1472 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1494 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1505 <row topline="true">
1506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1543 <row topline="true">
1544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1554 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1562 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1571 <row topline="true" bottomline="true">
1572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1608 'configure' also computes relative paths.
1609 This is needed for full relocatability of a binary package and to complete
1610 search paths (see section search paths below):
1616 \begin_inset Tabular
1617 <lyxtabular version="3" rows="4" columns="3">
1619 <column alignment="block" valignment="top" leftline="true" width="0in">
1620 <column alignment="block" valignment="top" leftline="true" width="0in">
1621 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1622 <row topline="true" bottomline="true">
1623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1648 <row topline="true" bottomline="true">
1649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1659 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1676 <row bottomline="true">
1677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1695 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1704 <row bottomline="true">
1705 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1723 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1756 \begin_inset Quotes srd
1760 \begin_inset Quotes srd
1774 \begin_inset Quotes srd
1778 \begin_inset Quotes srd
1806 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1815 \begin_inset Quotes srd
1818 i586-mingw32msvc-gcc
1819 \begin_inset Quotes srd
1823 \begin_inset Quotes srd
1826 i586-mingw32msvc-g++
1827 \begin_inset Quotes srd
1835 \begin_inset Quotes srd
1838 i586-mingw32msvc-ranlib
1839 \begin_inset Quotes srd
1847 \begin_inset Quotes srd
1850 i586-mingw32msvc-strip
1851 \begin_inset Quotes srd
1869 \begin_inset Quotes srd
1873 \begin_inset Quotes srd
1891 \begin_inset Quotes srd
1895 \begin_inset Quotes srd
1903 \begin_inset Quotes srd
1907 \begin_inset Quotes srd
1915 \begin_inset Quotes srd
1919 \begin_inset Quotes srd
1927 \begin_inset Quotes srd
1931 \begin_inset Quotes srd
1938 sdccconf_h_dir_separator=
1939 \begin_inset Quotes srd
1951 \begin_inset Quotes srd
1982 -host=i586-mingw32msvc
1996 -build=unknown-unknown-linux-gnu
2000 \begin_inset Quotes sld
2004 \begin_inset Quotes srd
2007 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
2026 \begin_inset Quotes srd
2030 \begin_inset Quotes srd
2048 \begin_inset Quotes srd
2052 \begin_inset Quotes srd
2060 \begin_inset Quotes srd
2064 \begin_inset Quotes srd
2072 \begin_inset Quotes srd
2076 \begin_inset Quotes srd
2084 \begin_inset Quotes srd
2088 \begin_inset Quotes srd
2095 sdccconf_h_dir_separator=
2096 \begin_inset Quotes srd
2108 \begin_inset Quotes srd
2116 \begin_inset Quotes srd
2120 \begin_inset Quotes srd
2128 \begin_inset Quotes srd
2132 \begin_inset Quotes srd
2138 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2149 -C' turns on caching, which gives a little bit extra speed.
2150 However if options are changed, it can be necessary to delete the config.cache
2155 \begin_inset LatexCommand \label{sub:Install-paths}
2160 \begin_inset LatexCommand \index{Install paths}
2166 \added_space_top medskip \align center
2168 \begin_inset Tabular
2169 <lyxtabular version="3" rows="5" columns="4">
2171 <column alignment="left" valignment="top" leftline="true" width="0">
2172 <column alignment="left" valignment="top" leftline="true" width="0">
2173 <column alignment="left" valignment="top" leftline="true" width="0">
2174 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2175 <row topline="true" bottomline="true">
2176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2196 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2217 <row topline="true">
2218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2257 <row topline="true">
2258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2273 $DATADIR/ $INCLUDE_DIR_SUFFIX
2276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2281 /usr/local/share/sdcc/include
2284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2297 <row topline="true">
2298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2313 $DATADIR/$LIB_DIR_SUFFIX
2316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2321 /usr/local/share/sdcc/lib
2324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2337 <row topline="true" bottomline="true">
2338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2361 /usr/local/share/sdcc/doc
2364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2386 *compiler, preprocessor, assembler, and linker
2392 is auto-appended by the compiler, e.g.
2393 small, large, z80, ds390 etc
2396 The install paths can still be changed during `make install` with e.g.:
2399 make install prefix=$(HOME)/local/sdcc
2402 Of course this doesn't change the search paths compiled into the binaries.
2406 Moreover the install path can be changed by defining DESTDIR
2407 \begin_inset LatexCommand \index{DESTDIR}
2414 make install DESTDIR=$(HOME)/sdcc.rpm/
2417 Please note that DESTDIR must have a trailing slash!
2421 \begin_inset LatexCommand \label{sub:Search-Paths}
2426 \begin_inset LatexCommand \index{Search path}
2433 Some search paths or parts of them are determined by configure variables
2438 , see section above).
2439 Further search paths are determined by environment variables during runtime.
2442 The paths searched when running the compiler are as follows (the first catch
2448 Binary files (preprocessor, assembler and linker)
2454 \begin_inset Tabular
2455 <lyxtabular version="3" rows="4" columns="3">
2457 <column alignment="block" valignment="top" leftline="true" width="0in">
2458 <column alignment="block" valignment="top" leftline="true" width="0in">
2459 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2460 <row topline="true" bottomline="true">
2461 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2486 <row topline="true">
2487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2505 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2516 <row topline="true">
2517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2522 Path of argv[0] (if available)
2525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2542 <row topline="true" bottomline="true">
2543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2551 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2559 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2584 \begin_inset Tabular
2585 <lyxtabular version="3" rows="6" columns="3">
2587 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2588 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2589 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2590 <row topline="true" bottomline="true">
2591 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2607 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2616 <row topline="true">
2617 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2672 <row topline="true">
2673 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2689 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2698 <row topline="true">
2699 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2713 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2736 <row topline="true">
2737 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2805 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2818 <row topline="true" bottomline="true">
2819 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2835 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2840 /usr/local/share/sdcc/
2845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2873 -nostdinc disables the last two search paths.
2883 With the exception of
2884 \begin_inset Quotes sld
2898 \begin_inset Quotes srd
2905 is auto-appended by the compiler (e.g.
2906 small, large, z80, ds390 etc.).
2913 \begin_inset Tabular
2914 <lyxtabular version="3" rows="6" columns="3">
2916 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2917 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2918 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2919 <row topline="true" bottomline="true">
2920 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2928 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2945 <row topline="true">
2946 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2982 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3001 <row topline="true">
3002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3014 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3041 <row topline="true">
3042 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3053 $LIB_DIR_SUFFIX/<model>
3056 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3070 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3087 <row topline="true">
3088 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3103 $LIB_DIR_SUFFIX/<model>
3106 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3159 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3215 <row topline="true" bottomline="true">
3216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3225 $LIB_DIR_SUFFIX/<model>
3228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3233 /usr/local/share/sdcc/
3240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3258 Don't delete any of the stray spaces in the table above without checking
3259 the HTML output (last line)!
3275 -nostdlib disables the last two search paths.
3279 \begin_inset LatexCommand \index{Building SDCC}
3286 Building SDCC on Linux
3287 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3296 Download the source package
3298 either from the SDCC Subversion repository or from the nightly snapshots
3300 , it will be named something like sdcc
3311 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3320 Bring up a command line terminal, such as xterm.
3325 Unpack the file using a command like:
3328 "tar -xvzf sdcc.src.tar.gz
3333 , this will create a sub-directory called sdcc with all of the sources.
3336 Change directory into the main SDCC directory, for example type:
3353 This configures the package for compilation on your system.
3369 All of the source packages will compile, this can take a while.
3385 This copies the binary executables, the include files, the libraries and
3386 the documentation to the install directories.
3387 Proceed with section
3388 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3395 Building SDCC on OSX 2.x
3398 Follow the instruction for Linux.
3402 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3403 )) fails to compile SDCC.
3404 Fortunately there's also gcc 2.9.x installed, which works fine.
3405 This compiler can be selected by running 'configure' with:
3408 ./configure CC=gcc2 CXX=g++2
3411 Cross compiling SDCC on Linux for Windows
3414 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3415 See section 'Configure Options'.
3418 Building SDCC using Cygwin and Mingw32
3421 For building and installing a Cygwin executable follow the instructions
3427 \begin_inset Quotes sld
3431 \begin_inset Quotes srd
3434 Win32-binary can be built, which will not need the Cygwin-DLL.
3435 For the necessary 'configure' options see section 'configure options' or
3436 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3440 In order to install Cygwin on Windows download setup.exe from
3441 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3447 \begin_inset Quotes sld
3450 default text file type
3451 \begin_inset Quotes srd
3455 \begin_inset Quotes sld
3459 \begin_inset Quotes srd
3462 and download/install at least the following packages.
3463 Some packages are selected by default, others will be automatically selected
3464 because of dependencies with the manually selected packages.
3465 Never deselect these packages!
3474 gcc ; version 3.x is fine, no need to use the old 2.9x
3477 binutils ; selected with gcc
3483 rxvt ; a nice console, which makes life much easier under windoze (see below)
3486 man ; not really needed for building SDCC, but you'll miss it sooner or
3490 less ; not really needed for building SDCC, but you'll miss it sooner or
3494 svn ; only if you use Subversion access
3497 If you want to develop something you'll need:
3500 python ; for the regression tests
3503 gdb ; the gnu debugger, together with the nice GUI
3504 \begin_inset Quotes sld
3508 \begin_inset Quotes srd
3514 openssh ; to access the CF or commit changes
3517 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3518 use autoconf-stable!
3521 rxvt is a nice console with history.
3522 Replace in your cygwin.bat the line
3541 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3544 -bg black -fg white -geometry 100x65 -e bash -
3557 Text selected with the mouse is automatically copied to the clipboard, pasting
3558 works with shift-insert.
3562 The other good tip is to make sure you have no //c/-style paths anywhere,
3563 use /cygdrive/c/ instead.
3564 Using // invokes a network lookup which is very slow.
3566 \begin_inset Quotes sld
3570 \begin_inset Quotes srd
3573 is too long, you can change it with e.g.
3579 SDCC sources use the unix line ending LF.
3580 Life is much easier, if you store the source tree on a drive which is mounted
3582 And use an editor which can handle LF-only line endings.
3583 Make sure not to commit files with windows line endings.
3584 The tabulator spacing
3585 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3589 used in the project is 8.
3590 Although a tabulator spacing of 8 is a sensible choice for programmers
3591 (it's a power of 2 and allows to display 8/16 bit signed variables without
3592 loosing columns) the plan is to move towards using only spaces in the source.
3595 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3600 Download the source package
3602 either from the SDCC Subversion repository or from the
3603 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3609 , it will be named something like sdcc
3616 SDCC is distributed with all the projects, workspaces, and files you need
3617 to build it using Visual C++ 6.0/NET (except for SDCDB and ucSim).
3618 The workspace name is 'sdcc.dsw'.
3619 Please note that as it is now, all the executables are created in a folder
3623 Once built you need to copy the executables from sdcc
3627 bin before running SDCC.
3632 WARNING: Visual studio is very picky with line terminations; it expects
3633 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3634 When using the Subversion repository it's easiest to configure the svn
3635 client to convert automatically for you.
3636 If however you are getting a message such as "This makefile was not generated
3637 by Developer Studio etc.
3639 \begin_inset Quotes srd
3642 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3643 need to convert the Unix style line endings to DOS style line endings.
3644 To do so you can use the
3645 \begin_inset Quotes sld
3649 \begin_inset Quotes srd
3652 utility freely available on the internet.
3653 Doug Hawkins reported in the sdcc-user list that this works:
3661 SDCC> unix2dos sdcc.dsw
3667 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3671 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3672 flex.exe, and gawk.exe.
3673 One good place to get them is
3674 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3682 Download the file UnxUtils
3683 \begin_inset LatexCommand \index{UnxUtils}
3688 Now you have to install the utilities and setup MSVC so it can locate the
3690 Here there are two alternatives (choose one!):
3697 a) Extract UnxUtils.zip to your C:
3699 hard disk PRESERVING the original paths, otherwise bison won't work.
3700 (If you are using WinZip make certain that 'Use folder names' is selected)
3704 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3705 in 'Show directories for:' select 'Executable files', and in the directories
3706 window add a new path: 'C:
3716 (As a side effect, you get a bunch of Unix utilities that could be useful,
3717 such as diff and patch.)
3724 This one avoids extracting a bunch of files you may not use, but requires
3729 a) Create a directory were to put the tools needed, or use a directory already
3737 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3738 to such directory WITHOUT preserving the original paths.
3739 (If you are using WinZip make certain that 'Use folder names' is not selected)
3743 c) Rename bison.exe to '_bison.exe'.
3747 d) Create a batch file 'bison.bat' in 'C:
3751 ' and add these lines:
3771 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3775 Steps 'c' and 'd' are needed because bison requires by default that the
3776 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3777 '/usr/local/share/' I think.
3778 So it is necessary to tell bison where those files are located if they
3779 are not in such directory.
3780 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3784 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3785 in 'Show directories for:' select 'Executable files', and in the directories
3786 window add a new path: 'c:
3789 Note that you can use any other path instead of 'c:
3791 util', even the path where the Visual C++ tools are, probably: 'C:
3795 Microsoft Visual Studio
3800 So you don't have to execute step 'e' :)
3804 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3805 the executables from sdcc
3809 bin, and you can compile using SDCC.
3812 Building SDCC Using Borland
3815 From the sdcc directory, run the command "make -f Makefile.bcc".
3816 This should regenerate all the .exe files in the bin directory except for
3820 If you modify any source files and need to rebuild, be aware that the dependenci
3821 es may not be correctly calculated.
3822 The safest option is to delete all .obj files and run the build again.
3823 From a Cygwin BASH prompt, this can easily be done with the command (be
3824 sure you are in the sdcc directory):
3834 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3836 ) -print -exec rm {}
3845 or on Windows NT/2000/XP from the command prompt with the command:
3852 del /s *.obj *.lib *.rul
3855 from the sdcc directory.
3858 Windows Install Using a ZIP Package
3861 Download the binary zip package from
3862 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3866 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3867 This should unpack to a group of sub-directories.
3868 An example directory structure after unpacking the mingw32 package is:
3873 bin for the executables, c:
3881 lib for the include and libraries.
3884 Adjust your environment variable PATH to include the location of the bin
3885 directory or start sdcc using the full path.
3888 Windows Install Using the Setup Program
3889 \begin_inset LatexCommand \label{sub:Windows-Install}
3896 Download the setup program
3898 sdcc-x.y.z-setup.exe
3900 for an official release from
3903 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3907 or a setup program for one of the snapshots
3909 sdcc-yyyymmdd-xxxx-setup.exe
3912 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3917 A windows typical installer will guide you through the installation process.
3921 \begin_inset LatexCommand \index{VPATH}
3928 SDCC supports the VPATH feature provided by configure and make.
3929 It allows to separate the source and build trees.
3961 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3962 # extract source to directory sdcc
3967 mkdir sdcc.build\SpecialChar ~
3976 # put output in sdcc.build
3986 ../sdcc/configure\SpecialChar ~
3994 # configure is doing all the magic!
4006 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
4007 It automagically computes the variables srcdir, top_srcdir and top_buildir
4013 the generated files will be in ~/sdcc.build, while the source files stay
4016 This is not only usefull for building different binaries, e.g.
4017 when cross compiling.
4018 It also gives you a much better overview in the source tree when all the
4019 generated files are not scattered between the source files.
4020 And the best thing is: if you want to change a file you can leave the original
4021 file untouched in the source directory.
4022 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4023 Makefile.dep` and `make`.
4028 will do the rest for you!
4031 Building the Documentation
4044 -enable-doc to the configure arguments to build the documentation together
4045 with all the other stuff.
4046 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4047 dvips and makeindex) to get the job done.
4048 Another possibility is to change to the doc directory and to type
4052 \begin_inset Quotes srd
4056 \begin_inset Quotes srd
4063 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4066 \begin_inset LatexCommand \url{http://www.lyx.org}
4070 as editor is straightforward.
4071 Prebuilt documentation in html and pdf format is available from
4072 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4079 Reading the Documentation
4080 \begin_inset LatexCommand \index{Documentation}
4087 Currently reading the document in pdf format is recommended, as for unknown
4088 reason the hyperlinks are working there whereas in the html version they
4095 If you should know why please drop us a note
4101 You'll find the pdf version
4102 \begin_inset LatexCommand \index{PDF version of this document}
4107 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4115 \begin_inset LatexCommand \index{HTML version of this document}
4120 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4126 This documentation is in some aspects different from a commercial documentation:
4130 It tries to document SDCC for several processor architectures in one document
4131 (commercially these probably would be separate documents/products).
4133 \begin_inset LatexCommand \index{Status of documentation}
4137 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4139 Z80, PIC14, PIC16 and HC08.
4142 There are many references pointing away from this documentation.
4143 Don't let this distract you.
4145 was a reference like
4146 \begin_inset LatexCommand \url{http://www.opencores.org}
4150 together with a statement
4151 \begin_inset Quotes sld
4154 some processors which are targetted by SDCC can be implemented in a
4171 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4176 \begin_inset Quotes srd
4180 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4185 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4190 \begin_inset Quotes sld
4193 have you ever heard of an open source compiler that compiles a subset of
4195 \begin_inset Quotes srd
4198 we expect you to have a quick look there and come back.
4199 If you read this you are on the right track.
4202 Some sections attribute more space to problems, restrictions and warnings
4203 than to the solution.
4206 The installation section and the section about the debugger is intimidating.
4209 There are still lots of typos and there are more different writing styles
4213 Testing the SDCC Compiler
4214 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4221 The first thing you should do after installing your SDCC compiler is to
4237 \begin_inset LatexCommand \index{version}
4244 at the prompt, and the program should run and output its version like:
4249 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4253 If it doesn't run, or gives a message about not finding sdcc program, then
4254 you need to check over your installation.
4255 Make sure that the sdcc bin directory is in your executable search path
4256 defined by the PATH environment setting (
4261 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4268 Install trouble-shooting for suggestions
4271 Make sure that the sdcc program is in the bin folder, if not perhaps something
4272 did not install correctly.
4280 is commonly installed as described in section
4281 \begin_inset Quotes sld
4284 Install and search paths
4285 \begin_inset Quotes srd
4294 Make sure the compiler works on a very simple example.
4295 Type in the following test.c program using your favorite
4321 Compile this using the following command:
4330 If all goes well, the compiler will generate a test.asm and test.rel file.
4331 Congratulations, you've just compiled your first program with SDCC.
4332 We used the -c option to tell SDCC not to link the generated code, just
4333 to keep things simple for this step.
4341 The next step is to try it with the linker.
4351 If all goes well the compiler will link with the libraries and produce
4352 a test.ihx output file.
4357 (no test.ihx, and the linker generates warnings), then the problem is most
4366 usr/local/share/sdcc/lib directory
4373 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4380 Install trouble-shooting for suggestions).
4388 The final test is to ensure
4396 header files and libraries.
4397 Edit test.c and change it to the following:
4414 strcpy(str1, "testing");
4421 Compile this by typing
4428 This should generate a test.ihx output file, and it should give no warnings
4429 such as not finding the string.h file.
4430 If it cannot find the string.h file, then the problem is that
4434 cannot find the /usr/local/share/sdcc/include directory
4441 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4448 Install trouble-shooting section for suggestions).
4466 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4470 to find exactly where SDCC is looking for the include and lib files.
4473 Install Trouble-shooting
4474 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4479 \begin_inset LatexCommand \index{Install trouble-shooting}
4486 If SDCC does not build correctly
4489 A thing to try is starting from scratch by unpacking the .tgz source package
4490 again in an empty directory.
4498 ./configure 2>&1 | tee configure.log
4512 make 2>&1 | tee make.log
4519 If anything goes wrong, you can review the log files to locate the problem.
4520 Or a relevant part of this can be attached to an email that could be helpful
4521 when requesting help from the mailing list.
4525 \begin_inset Quotes sld
4529 \begin_inset Quotes srd
4536 \begin_inset Quotes sld
4540 \begin_inset Quotes srd
4543 command is a script that analyzes your system and performs some configuration
4544 to ensure the source package compiles on your system.
4545 It will take a few minutes to run, and will compile a few tests to determine
4546 what compiler features are installed.
4550 \begin_inset Quotes sld
4554 \begin_inset Quotes srd
4560 This runs the GNU make tool, which automatically compiles all the source
4561 packages into the final installed binary executables.
4565 \begin_inset Quotes sld
4569 \begin_inset Quotes erd
4575 This will install the compiler, other executables libraries and include
4576 files into the appropriate directories.
4578 \begin_inset LatexCommand \ref{sub:Install-paths}
4584 \begin_inset LatexCommand \ref{sub:Search-Paths}
4589 about install and search paths.
4591 On most systems you will need super-user privileges to do this.
4597 SDCC is not just a compiler, but a collection of tools by various developers.
4598 These include linkers, assemblers, simulators and other components.
4599 Here is a summary of some of the components.
4600 Note that the included simulator and assembler have separate documentation
4601 which you can find in the source package in their respective directories.
4602 As SDCC grows to include support for other processors, other packages from
4603 various developers are included and may have their own sets of documentation.
4607 You might want to look at the files which are installed in <installdir>.
4608 At the time of this writing, we find the following programs for gcc-builds:
4612 In <installdir>/bin:
4615 sdcc - The compiler.
4618 sdcpp - The C preprocessor.
4621 asx8051 - The assembler for 8051 type processors.
4628 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4631 aslink -The linker for 8051 type processors.
4638 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4641 s51 - The ucSim 8051 simulator.
4644 sdcdb - The source debugger.
4647 packihx - A tool to pack (compress) Intel hex files.
4650 In <installdir>/share/sdcc/include
4656 In <installdir>/share/sdcc/lib
4659 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4663 In <installdir>/share/sdcc/doc
4669 As development for other processors proceeds, this list will expand to include
4670 executables to support processors like AVR, PIC, etc.
4676 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4677 the assembler and linkage editor.
4680 sdcpp - The C-Preprocessor
4684 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4688 is a modified version of the GNU preprocessor.
4689 The C preprocessor is used to pull in #include sources, process #ifdef
4690 statements, #defines and so on.
4701 - The Assemblers and Linkage Editors
4704 This is retargettable assembler & linkage editor, it was developed by Alan
4706 John Hartman created the version for 8051, and I (Sandeep) have made some
4707 enhancements and bug fixes for it to work properly with SDCC.
4714 \begin_inset LatexCommand \index{s51}
4718 is a freeware, opensource simulator developed by Daniel Drotos.
4719 The simulator is built as part of the build process.
4720 For more information visit Daniel's web site at:
4721 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4726 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4730 sdcdb - Source Level Debugger
4734 \begin_inset LatexCommand \index{SDCDB (debugger)}
4738 is the companion source level debugger.
4739 More about SDCDB in section
4740 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4745 The current version of the debugger uses Daniel's Simulator S51
4746 \begin_inset LatexCommand \index{s51}
4750 , but can be easily changed to use other simulators.
4759 Single Source File Projects
4762 For single source file 8051 projects the process is very simple.
4763 Compile your programs with the following command
4766 "sdcc sourcefile.c".
4770 This will compile, assemble and link your source file.
4771 Output files are as follows:
4775 \begin_inset LatexCommand \index{<file>.asm}
4780 \begin_inset LatexCommand \index{Assembler source}
4784 file created by the compiler
4788 \begin_inset LatexCommand \index{<file>.lst}
4793 \begin_inset LatexCommand \index{Assembler listing}
4797 file created by the Assembler
4801 \begin_inset LatexCommand \index{<file>.rst}
4806 \begin_inset LatexCommand \index{Assembler listing}
4810 file updated with linkedit information, created by linkage editor
4814 \begin_inset LatexCommand \index{<file>.sym}
4819 \begin_inset LatexCommand \index{Symbol listing}
4823 for the sourcefile, created by the assembler
4827 \begin_inset LatexCommand \index{<file>.rel}
4832 \begin_inset LatexCommand \index{<file>.o}
4837 \begin_inset LatexCommand \index{Object file}
4841 created by the assembler, input to Linkage editor
4845 \begin_inset LatexCommand \index{<file>.map}
4850 \begin_inset LatexCommand \index{Memory map}
4854 for the load module, created by the Linker
4858 \begin_inset LatexCommand \index{<file>.mem}
4862 - A file with a summary of the memory usage
4866 \begin_inset LatexCommand \index{<file>.ihx}
4870 - The load module in Intel hex format
4871 \begin_inset LatexCommand \index{Intel hex format}
4875 (you can select the Motorola S19 format
4876 \begin_inset LatexCommand \index{Motorola S19 format}
4891 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4896 If you need another format you might want to use
4903 \begin_inset LatexCommand \index{objdump (tool)}
4914 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4919 Both formats are documented in the documentation of srecord
4920 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4928 \begin_inset LatexCommand \index{<file>.adb}
4932 - An intermediate file containing debug information needed to create the
4944 \begin_inset LatexCommand \index{-\/-debug}
4952 \begin_inset LatexCommand \index{<file>.cdb}
4956 - An optional file (with -
4966 -debug) containing debug information.
4967 The format is documented in cdbfileformat.pdf
4972 \begin_inset LatexCommand \index{<file> (no extension)}
4976 An optional AOMF or AOMF51
4977 \begin_inset LatexCommand \index{AOMF, AOMF51}
4982 \begin_inset LatexCommand \label{OMF file}
4986 file containing debug information (generated with option -
5013 ormat is commonly used by third party tools (debuggers
5014 \begin_inset LatexCommand \index{Debugger}
5018 , simulators, emulators)
5022 \begin_inset LatexCommand \index{<file>.dump*}
5026 - Dump file to debug the compiler it self (generated with option -
5036 -dumpall) (see section
5037 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5043 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5049 \begin_inset Quotes sld
5052 Anatomy of the compiler
5053 \begin_inset Quotes srd
5059 Postprocessing the Intel Hex
5060 \begin_inset LatexCommand \index{Intel hex format}
5068 \begin_inset LatexCommand \index{<file>.ihx}
5072 which is generated by SDCC might include lines of varying length and the
5073 addresses within the file are not guaranteed to be strictly ascending.
5074 If your toolchain or a bootloader does not like this you can use the tool
5080 \begin_inset LatexCommand \index{packihx (tool)}
5084 which is part of the SDCC distribution:
5091 packihx sourcefile.ihx >sourcefile.hex
5098 The separately available
5103 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5107 package additionally allows to set undefined locations to a predefined
5108 value, to insert checksums of various flavours (crc, add, xor) and to perform
5109 other manipulations (convert, split, crop, offset, ...).
5117 srec_cat\SpecialChar ~
5119 sourcefile.ihx -intel\SpecialChar ~
5122 -fill 0xff 0x0000 0x8000\SpecialChar ~
5125 -o sourcefile.hex -intel
5132 The srecord package is available at
5133 \begin_inset LatexCommand \url{http://sf.net/projects/srecord}
5140 Projects with Multiple Source Files
5143 SDCC can compile only ONE file at a time.
5144 Let us for example assume that you have a project containing the following
5149 foo1.c (contains some functions)
5151 foo2.c (contains some more functions)
5153 foomain.c (contains more functions and the function main)
5161 The first two files will need to be compiled separately with the commands:
5193 Then compile the source file containing the
5198 \begin_inset LatexCommand \index{Linker}
5202 the files together with the following command:
5210 foomain.c\SpecialChar ~
5211 foo1.rel\SpecialChar ~
5216 \begin_inset LatexCommand \index{<file>.rel}
5228 can be separately compiled as well:
5239 sdcc foomain.rel foo1.rel foo2.rel
5246 The file containing the
5261 file specified in the command line, since the linkage editor processes
5262 file in the order they are presented to it.
5263 The linker is invoked from SDCC using a script file with extension .lnk
5264 \begin_inset LatexCommand \index{<file>.lnk}
5269 You can view this file to troubleshoot linking problems such as those arising
5270 from missing libraries.
5273 Projects with Additional Libraries
5274 \begin_inset LatexCommand \index{Libraries}
5281 Some reusable routines may be compiled into a library, see the documentation
5282 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5286 \begin_inset LatexCommand \index{<file>.lib}
5293 Libraries created in this manner can be included in the command line.
5294 Make sure you include the -L <library-path> option to tell the linker where
5295 to look for these files if they are not in the current directory.
5296 Here is an example, assuming you have the source file
5308 (if that is not the same as your current project):
5315 sdcc foomain.c foolib.lib -L mylib
5326 must be an absolute path name.
5330 The most efficient way to use libraries is to keep separate modules in separate
5332 The lib file now should name all the modules.rel
5333 \begin_inset LatexCommand \index{<file>.rel}
5338 For an example see the standard library file
5342 in the directory <installdir>/share/lib/small.
5345 Using sdcclib to Create and Manage Libraries
5346 \begin_inset LatexCommand \index{sdcclib}
5353 Alternatively, instead of having a .rel file for each entry on the library
5354 file as described in the preceding section, sdcclib can be used to embed
5355 all the modules belonging to such library in the library file itself.
5356 This results in a larger library file, but it greatly reduces the number
5357 of disk files accessed by the linker.
5358 Additionally, the packed library file contains an index of all include
5359 modules and symbols that significantly speeds up the linking process.
5360 To display a list of options supported by sdcclib type:
5369 \begin_inset LatexCommand \index{sdcclib}
5380 To create a new library file, start by compiling all the required modules.
5418 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5420 The next step is to add the .rel files to the library file:
5428 sdcclib libint.lib _divsint.rel
5431 \begin_inset LatexCommand \index{sdcclib}
5441 sdcclib libint.lib _divuint.rel
5447 sdcclib libint.lib _modsint.rel
5453 sdcclib libint.lib _moduint.rel
5459 sdcclib libint.lib _mulint.rel
5466 If the file already exists in the library, it will be replaced.
5467 To see what modules and symbols are included in the library, options -s
5468 and -m are available.
5476 sdcclib -s libint.lib
5479 \begin_inset LatexCommand \index{sdcclib}
5588 \added_space_bottom bigskip
5589 If the source files are compiled using -
5600 \begin_inset LatexCommand \index{-\/-debug}
5604 , the corresponding debug information file .adb will be include in the library
5606 The library files created with sdcclib are plain text files, so they can
5607 be viewed with a text editor.
5608 It is not recomended to modify a library file created with sdcclib using
5609 a text editor, as there are file indexes numbers located accross the file
5610 used by the linker to quickly locate the required module to link.
5611 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5612 it can be safely deleted, since all the information required for linking
5613 is embedded in the library file itself.
5614 Library files created using sdcclib are used as described in the preceding
5618 Command Line Options
5619 \begin_inset LatexCommand \index{Command Line Options}
5626 Processor Selection Options
5627 \begin_inset LatexCommand \index{Options processor selection}
5632 \begin_inset LatexCommand \index{Processor selection options}
5638 \labelwidthstring 00.00.0000
5643 \begin_inset LatexCommand \index{-mmcs51}
5649 Generate code for the Intel MCS51
5650 \begin_inset LatexCommand \index{MCS51}
5654 family of processors.
5655 This is the default processor target.
5657 \labelwidthstring 00.00.0000
5662 \begin_inset LatexCommand \index{-mds390}
5668 Generate code for the Dallas DS80C390
5669 \begin_inset LatexCommand \index{DS80C390}
5675 \labelwidthstring 00.00.0000
5680 \begin_inset LatexCommand \index{-mds400}
5686 Generate code for the Dallas DS80C400
5687 \begin_inset LatexCommand \index{DS80C400}
5693 \labelwidthstring 00.00.0000
5698 \begin_inset LatexCommand \index{-mhc08}
5704 Generate code for the Freescale/Motorola HC08
5705 \begin_inset LatexCommand \index{HC08}
5709 family of processors.
5711 \labelwidthstring 00.00.0000
5716 \begin_inset LatexCommand \index{-mz80}
5722 Generate code for the Zilog Z80
5723 \begin_inset LatexCommand \index{Z80}
5727 family of processors.
5729 \labelwidthstring 00.00.0000
5734 \begin_inset LatexCommand \index{-mgbz80}
5740 Generate code for the GameBoy Z80
5741 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5745 processor (Not actively maintained).
5747 \labelwidthstring 00.00.0000
5752 \begin_inset LatexCommand \index{-mavr}
5758 Generate code for the Atmel AVR
5759 \begin_inset LatexCommand \index{AVR}
5763 processor (In development, not complete).
5764 AVR users should probably have a look at winavr
5765 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5770 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5777 I think it is fair to direct users there for now.
5778 Open source is also about avoiding unnecessary work .
5779 But I didn't find the 'official' link.
5781 \labelwidthstring 00.00.0000
5786 \begin_inset LatexCommand \index{-mpic14}
5792 Generate code for the Microchip PIC 14
5793 \begin_inset LatexCommand \index{PIC14}
5797 -bit processors (p16f84 and variants.
5798 In development, not complete).
5801 p16f627 p16f628 p16f84 p16f873 p16f877?
5803 \labelwidthstring 00.00.0000
5808 \begin_inset LatexCommand \index{-mpic16}
5814 Generate code for the Microchip PIC 16
5815 \begin_inset LatexCommand \index{PIC16}
5819 -bit processors (p18f452 and variants.
5820 In development, not complete).
5822 \labelwidthstring 00.00.0000
5828 Generate code for the Toshiba TLCS-900H
5829 \begin_inset LatexCommand \index{TLCS-900H}
5833 processor (Not maintained, not complete).
5835 \added_space_bottom bigskip \labelwidthstring 00.00.0000
5840 \begin_inset LatexCommand \index{-mxa51}
5846 Generate code for the Phillips XA51
5847 \begin_inset LatexCommand \index{XA51}
5851 processor (Not maintained, not complete).
5854 Preprocessor Options
5855 \begin_inset LatexCommand \index{Options preprocessor}
5860 \begin_inset LatexCommand \index{Preprocessor options}
5865 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5871 \labelwidthstring 00.00.0000
5876 \begin_inset LatexCommand \index{-I<path>}
5882 The additional location where the pre processor will look for <..h> or
5883 \begin_inset Quotes eld
5887 \begin_inset Quotes erd
5892 \labelwidthstring 00.00.0000
5897 \begin_inset LatexCommand \index{-D<macro[=value]>}
5903 Command line definition of macros.
5904 Passed to the preprocessor.
5906 \labelwidthstring 00.00.0000
5911 \begin_inset LatexCommand \index{-M}
5917 Tell the preprocessor to output a rule suitable for make describing the
5918 dependencies of each object file.
5919 For each source file, the preprocessor outputs one make-rule whose target
5920 is the object file name for that source file and whose dependencies are
5921 all the files `#include'd in it.
5922 This rule may be a single line or may be continued with `
5924 '-newline if it is long.
5925 The list of rules is printed on standard output instead of the preprocessed
5928 \begin_inset LatexCommand \index{-E}
5934 \labelwidthstring 00.00.0000
5939 \begin_inset LatexCommand \index{-C}
5945 Tell the preprocessor not to discard comments.
5946 Used with the `-E' option.
5948 \labelwidthstring 00.00.0000
5953 \begin_inset LatexCommand \index{-MM}
5964 Like `-M' but the output mentions only the user header files included with
5966 \begin_inset Quotes eld
5970 System header files included with `#include <file>' are omitted.
5972 \labelwidthstring 00.00.0000
5977 \begin_inset LatexCommand \index{-Aquestion(answer)}
5983 Assert the answer answer for question, in case it is tested with a preprocessor
5984 conditional such as `#if #question(answer)'.
5985 `-A-' disables the standard assertions that normally describe the target
5988 \labelwidthstring 00.00.0000
5993 \begin_inset LatexCommand \index{-Umacro}
5999 Undefine macro macro.
6000 `-U' options are evaluated after all `-D' options, but before any `-include'
6001 and `-imacros' options.
6003 \labelwidthstring 00.00.0000
6008 \begin_inset LatexCommand \index{-dM}
6014 Tell the preprocessor to output only a list of the macro definitions that
6015 are in effect at the end of preprocessing.
6016 Used with the `-E' option.
6018 \labelwidthstring 00.00.0000
6023 \begin_inset LatexCommand \index{-dD}
6029 Tell the preprocessor to pass all macro definitions into the output, in
6030 their proper sequence in the rest of the output.
6032 \labelwidthstring 00.00.0000
6037 \begin_inset LatexCommand \index{-dN}
6048 Like `-dD' except that the macro arguments and contents are omitted.
6049 Only `#define name' is included in the output.
6051 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6056 preprocessorOption[,preprocessorOption]
6059 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
6064 Pass the preprocessorOption to the preprocessor
6069 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
6074 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
6075 Collection (gcc), if you need more dedicated options please refer to the
6077 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
6085 \begin_inset LatexCommand \index{Options linker}
6090 \begin_inset LatexCommand \index{Linker options}
6096 \labelwidthstring 00.00.0000
6116 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6121 \begin_inset LatexCommand \index{-L -\/-lib-path}
6128 <absolute path to additional libraries> This option is passed to the linkage
6129 editor's additional libraries
6130 \begin_inset LatexCommand \index{Libraries}
6135 The path name must be absolute.
6136 Additional library files may be specified in the command line.
6137 See section Compiling programs for more details.
6139 \labelwidthstring 00.00.0000
6156 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6161 <Value> The start location of the external ram
6162 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6166 , default value is 0.
6167 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6177 -xram-loc 0x8000 or -
6189 \labelwidthstring 00.00.0000
6206 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6211 <Value> The start location of the code
6212 \begin_inset LatexCommand \index{code}
6216 segment, default value 0.
6217 Note when this option is used the interrupt vector table is also relocated
6218 to the given address.
6219 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6229 -code-loc 0x8000 or -
6241 \labelwidthstring 00.00.0000
6258 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6263 <Value> By default the stack
6264 \begin_inset LatexCommand \index{stack}
6268 is placed after the data segment.
6269 Using this option the stack can be placed anywhere in the internal memory
6271 The value entered can be in Hexadecimal or Decimal format, e.g.
6282 -stack-loc 0x20 or -
6293 Since the sp register is incremented before a push or call, the initial
6294 sp will be set to one byte prior the provided value.
6295 The provided value should not overlap any other memory areas such as used
6296 register banks or the data segment and with enough space for the current
6314 \begin_inset LatexCommand \index{-\/-pack-iram}
6318 option (which is now a default setting) will override this setting, so
6319 you should also specify the
6335 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6339 option if you need to manually place the stack.
6341 \labelwidthstring 00.00.0000
6358 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6363 <Value> By default the external stack
6364 \begin_inset LatexCommand \index{xstack}
6368 is placed after the pdata
6369 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
6374 Using this option the xstack can be placed anywhere in the external memory
6376 The value entered can be in Hexadecimal or Decimal format, e.g.
6387 -xstack-loc 0x8000 or -
6398 The provided value should not overlap any other memory areas such as the
6399 pdata or xdata segment and with enough space for the current application.
6401 \labelwidthstring 00.00.0000
6418 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6423 <Value> The start location of the internal ram data
6424 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6429 The value entered can be in Hexadecimal or Decimal format, eg.
6451 (By default, the start location of the internal ram data segment is set
6452 as low as possible in memory, taking into account the used register banks
6453 and the bit segment at address 0x20.
6454 For example if register banks 0 and 1 are used without bit variables, the
6455 data segment will be set, if -
6465 -data-loc is not used, to location 0x10.)
6467 \labelwidthstring 00.00.0000
6484 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6489 <Value> The start location of the indirectly addressable internal ram
6490 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6494 of the 8051, default value is 0x80.
6495 The value entered can be in Hexadecimal or Decimal format, eg.
6506 -idata-loc 0x88 or -
6518 \labelwidthstring 00.00.0000
6535 <Value> The start location of the bit
6536 \begin_inset LatexCommand \index{bit}
6540 addressable internal ram of the 8051.
6546 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6549 \labelwidthstring 00.00.0000
6564 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6573 The linker output (final object code) is in Intel Hex format.
6574 \begin_inset LatexCommand \index{Intel hex format}
6578 This is the default option.
6579 The format itself is documented in the documentation of srecord
6580 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6586 \labelwidthstring 00.00.0000
6601 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6610 The linker output (final object code) is in Motorola S19 format
6611 \begin_inset LatexCommand \index{Motorola S19 format}
6616 The format itself is documented in the documentation of srecord.
6618 \labelwidthstring 00.00.0000
6633 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6638 \begin_inset LatexCommand \index{HC08!Options!-\/-out-fmt-elf}
6647 The linker output (final object code) is in ELF format
6648 \begin_inset LatexCommand \index{ELF format}
6653 (Currently only supported for the HC08
6654 \begin_inset LatexCommand \index{HC08}
6660 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6665 linkOption[,linkOption]
6668 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6673 Pass the linkOption to the linker.
6674 If a bootloader is used an option like
6675 \begin_inset Quotes sld
6680 \begin_inset Quotes srd
6683 would be typical to set the start of the code segment.
6684 See also #pragma constseg and #pragma codeseg in section
6685 \begin_inset LatexCommand \ref{sec:Pragmas}
6690 File sdcc/as/doc/asxhtm.html has more on linker options.
6694 \begin_inset LatexCommand \index{Options MCS51}
6699 \begin_inset LatexCommand \index{MCS51 options}
6705 \labelwidthstring 00.00.0000
6720 \begin_inset LatexCommand \index{-\/-model-small}
6731 Generate code for Small Model programs, see section Memory Models for more
6733 This is the default model.
6735 \labelwidthstring 00.00.0000
6750 \begin_inset LatexCommand \index{-\/-model-medium}
6756 Generate code for Medium model programs, see section Memory Models for
6758 If this option is used all source files in the project have to be compiled
6760 It must also be used when invoking the linker.
6762 \labelwidthstring 00.00.0000
6777 \begin_inset LatexCommand \index{-\/-model-large}
6783 Generate code for Large model programs, see section Memory Models for more
6785 If this option is used all source files in the project have to be compiled
6787 It must also be used when invoking the linker.
6789 \labelwidthstring 00.00.0000
6804 \begin_inset LatexCommand \index{-\/-xstack}
6810 Uses a pseudo stack in the pdata
6811 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
6815 area (usually the first 256 bytes in the external ram) for allocating variables
6816 and passing parameters.
6818 \begin_inset LatexCommand \ref{sub:External-Stack}
6823 External Stack for more details.
6825 \labelwidthstring 00.00.0000
6843 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6847 Causes the linker to check if the internal ram usage is within limits of
6850 \labelwidthstring 00.00.0000
6868 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6872 Causes the linker to check if the external ram usage is within limits of
6875 \labelwidthstring 00.00.0000
6893 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6897 Causes the linker to check if the code memory usage is within limits of
6900 \labelwidthstring 00.00.0000
6918 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6922 Causes the linker to check if there is at minimum <Value> bytes for stack.
6924 \labelwidthstring 00.00.0000
6942 \begin_inset LatexCommand \index{-\/-pack-iram}
6946 Causes the linker to use unused register banks for data variables and pack
6947 data, idata and stack together.
6948 This is the default now.
6950 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6968 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6972 Causes the linker to use old style for allocating memory areas.
6975 DS390 / DS400 Options
6976 \begin_inset LatexCommand \index{Options DS390}
6981 \begin_inset LatexCommand \index{DS390}
6987 \labelwidthstring 00.00.0000
7004 \begin_inset LatexCommand \index{DS390!Options!-\/-model-flat24}
7014 Generate 24-bit flat mode code.
7015 This is the one and only that the ds390 code generator supports right now
7016 and is default when using
7021 See section Memory Models for more details.
7023 \labelwidthstring 00.00.0000
7038 \begin_inset LatexCommand \index{DS390!Options!-\/-protect-sp-update}
7044 disable interrupts during ESP:SP updates.
7046 \labelwidthstring 00.00.0000
7063 \begin_inset LatexCommand \index{DS390!Options!-\/-stack-10bit}
7067 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
7068 This is the one and only that the ds390 code generator supports right now
7069 and is default when using
7074 In this mode, the stack is located in the lower 1K of the internal RAM,
7075 which is mapped to 0x400000.
7076 Note that the support is incomplete, since it still uses a single byte
7077 as the stack pointer.
7078 This means that only the lower 256 bytes of the potential 1K stack space
7079 will actually be used.
7080 However, this does allow you to reclaim the precious 256 bytes of low RAM
7081 for use for the DATA and IDATA segments.
7082 The compiler will not generate any code to put the processor into 10 bit
7084 It is important to ensure that the processor is in this mode before calling
7085 any re-entrant functions compiled with this option.
7086 In principle, this should work with the
7099 \begin_inset LatexCommand \index{-\/-stack-auto}
7105 option, but that has not been tested.
7106 It is incompatible with the
7119 \begin_inset LatexCommand \index{-\/-xstack}
7126 It also only makes sense if the processor is in 24 bit contiguous addressing
7139 -model-flat24 option
7143 \labelwidthstring 00.00.0000
7158 \begin_inset LatexCommand \index{DS390!Options!-\/-stack-probe}
7164 insert call to function __stack_probe at each function prologue.
7166 \labelwidthstring 00.00.0000
7181 \begin_inset LatexCommand \index{DS390!Options!-\/-tini-libid}
7187 <nnnn> LibraryID used in -mTININative.
7190 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7205 \begin_inset LatexCommand \index{DS390!Options!-\/-use-accelerator}
7211 generate code for DS390 Arithmetic Accelerator.
7216 \begin_inset LatexCommand \index{Options Z80}
7221 \begin_inset LatexCommand \index{Z80}
7227 \labelwidthstring 00.00.0000
7244 \begin_inset LatexCommand \index{Z80!Options!-\/-callee-saves-bc}
7254 Force a called function to always save BC.
7256 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7273 \begin_inset LatexCommand \index{Z80!Options!-\/-no-std-crt0}
7277 When linking, skip the standard crt0.o object file.
7278 You must provide your own crt0.o for your system when linking.
7282 Optimization Options
7283 \begin_inset LatexCommand \index{Options optimization}
7288 \begin_inset LatexCommand \index{Optimization options}
7294 \labelwidthstring 00.00.0000
7309 \begin_inset LatexCommand \index{-\/-nogcse}
7315 Will not do global subexpression elimination, this option may be used when
7316 the compiler creates undesirably large stack/data spaces to store compiler
7326 \begin_inset LatexCommand \index{sloc (spill location)}
7331 A warning message will be generated when this happens and the compiler
7332 will indicate the number of extra bytes it allocated.
7333 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7335 \begin_inset LatexCommand \index{\#pragma nogcse}
7339 can be used to turn off global subexpression elimination
7340 \begin_inset LatexCommand \index{Subexpression elimination}
7344 for a given function only.
7346 \labelwidthstring 00.00.0000
7361 \begin_inset LatexCommand \index{-\/-noinvariant}
7367 Will not do loop invariant optimizations, this may be turned off for reasons
7368 explained for the previous option.
7369 For more details of loop optimizations performed see Loop Invariants in
7371 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7376 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7378 \begin_inset LatexCommand \index{\#pragma noinvariant}
7382 can be used to turn off invariant optimizations for a given function only.
7384 \labelwidthstring 00.00.0000
7399 \begin_inset LatexCommand \index{-\/-noinduction}
7405 Will not do loop induction optimizations, see section strength reduction
7407 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7409 \begin_inset LatexCommand \index{\#pragma noinduction}
7413 can be used to turn off induction optimizations for a given function only.
7415 \labelwidthstring 00.00.0000
7430 \begin_inset LatexCommand \index{-\/-nojtbound}
7441 Will not generate boundary condition check when switch statements
7442 \begin_inset LatexCommand \index{switch statement}
7446 are implemented using jump-tables.
7448 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7453 Switch Statements for more details.
7454 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7456 \begin_inset LatexCommand \index{\#pragma nojtbound}
7460 can be used to turn off boundary checking for jump tables for a given function
7463 \labelwidthstring 00.00.0000
7478 \begin_inset LatexCommand \index{-\/-noloopreverse}
7487 Will not do loop reversal
7488 \begin_inset LatexCommand \index{Loop reversing}
7494 \labelwidthstring 00.00.0000
7511 \begin_inset LatexCommand \index{-\/-nolabelopt }
7515 Will not optimize labels (makes the dumpfiles more readable).
7517 \labelwidthstring 00.00.0000
7532 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7538 Will not memcpy initialized data from code space into xdata space.
7539 This saves a few bytes in code space if you don't have initialized data
7540 \begin_inset LatexCommand \index{Variable initialization}
7546 \labelwidthstring 00.00.0000
7561 \begin_inset LatexCommand \index{-\/-nooverlay}
7567 The compiler will not overlay parameters and local variables of any function,
7568 see section Parameters and local variables for more details.
7570 \labelwidthstring 00.00.0000
7585 \begin_inset LatexCommand \index{-\/-no-peep}
7591 Disable peep-hole optimization with built-in rules.
7593 \labelwidthstring 00.00.0000
7610 \begin_inset LatexCommand \index{-\/-peep-file}
7615 <filename> This option can be used to use additional rules to be used by
7616 the peep hole optimizer.
7618 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7623 Peep Hole optimizations for details on how to write these rules.
7625 \labelwidthstring 00.00.0000
7640 \begin_inset LatexCommand \index{-\/-peep-asm}
7646 Pass the inline assembler code through the peep hole optimizer.
7647 This can cause unexpected changes to inline assembler code, please go through
7648 the peephole optimizer
7649 \begin_inset LatexCommand \index{Peephole optimizer}
7653 rules defined in the source file tree '<target>/peeph.def' before using
7656 \labelwidthstring 00.00.0000
7671 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7677 The compiler will optimize code generation towards fast code, possibly
7678 at the expense of code size.
7680 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7695 \begin_inset LatexCommand \index{-\/-opt-code-size}
7701 The compiler will optimize code generation towards compact code, possibly
7702 at the expense of code speed.
7706 \begin_inset LatexCommand \index{Options other}
7712 \labelwidthstring 00.00.0000
7728 \begin_inset LatexCommand \index{-\/-compile-only}
7733 \begin_inset LatexCommand \index{-c -\/-compile-only}
7739 will compile and assemble the source, but will not call the linkage editor.
7741 \labelwidthstring 00.00.0000
7760 \begin_inset LatexCommand \index{-\/-c1mode}
7766 reads the preprocessed source from standard input and compiles it.
7767 The file name for the assembler output must be specified using the -o option.
7769 \labelwidthstring 00.00.0000
7774 \begin_inset LatexCommand \index{-E}
7780 Run only the C preprocessor.
7781 Preprocess all the C source files specified and output the results to standard
7784 \labelwidthstring 00.00.0000
7790 \begin_inset LatexCommand \index{-o <path/file>}
7796 The output path resp.
7797 file where everything will be placed.
7798 If the parameter is a path, it must have a trailing slash (or backslash
7799 for the Windows binaries) to be recognized as a path.
7802 \labelwidthstring 00.00.0000
7817 \begin_inset LatexCommand \index{-\/-stack-auto}
7828 All functions in the source file will be compiled as
7833 \begin_inset LatexCommand \index{reentrant}
7838 the parameters and local variables will be allocated on the stack
7839 \begin_inset LatexCommand \index{stack}
7845 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7849 Parameters and Local Variables for more details.
7850 If this option is used all source files in the project should be compiled
7852 It automatically implies --int-long-reent and --float-reent.
7855 \labelwidthstring 00.00.0000
7870 \begin_inset LatexCommand \index{-\/-callee-saves}
7874 function1[,function2][,function3]....
7877 The compiler by default uses a caller saves convention for register saving
7878 across function calls, however this can cause unnecessary register pushing
7879 & popping when calling small functions from larger functions.
7880 This option can be used to switch the register saving convention for the
7881 function names specified.
7882 The compiler will not save registers when calling these functions, no extra
7883 code will be generated at the entry & exit (function prologue
7886 \begin_inset LatexCommand \index{function prologue}
7895 \begin_inset LatexCommand \index{function epilogue}
7901 ) for these functions to save & restore the registers used by these functions,
7902 this can SUBSTANTIALLY reduce code & improve run time performance of the
7904 In the future the compiler (with inter procedural analysis) will be able
7905 to determine the appropriate scheme to use for each function call.
7906 DO NOT use this option for built-in functions such as _mulint..., if this
7907 option is used for a library function the appropriate library function
7908 needs to be recompiled with the same option.
7909 If the project consists of multiple source files then all the source file
7910 should be compiled with the same -
7920 -callee-saves option string.
7921 Also see #pragma\SpecialChar ~
7923 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7929 \labelwidthstring 00.00.0000
7944 \begin_inset LatexCommand \index{-\/-debug}
7953 When this option is used the compiler will generate debug information.
7954 The debug information collected in a file with .cdb extension can be used
7956 For more information see documentation for SDCDB.
7957 Another file with no extension contains debug information in AOMF or AOMF51
7958 \begin_inset LatexCommand \index{AOMF, AOMF51}
7962 format which is commonly used by third party tools.
7964 \labelwidthstring 00.00.0000
7969 \begin_inset LatexCommand \index{-S}
7980 Stop after the stage of compilation proper; do not assemble.
7981 The output is an assembler code file for the input file specified.
7983 \labelwidthstring 00.00.0000
7998 \begin_inset LatexCommand \index{-\/-int-long-reent}
8004 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
8005 Note by default these libraries are compiled as non-reentrant.
8006 See section Installation for more details.
8008 \labelwidthstring 00.00.0000
8023 \begin_inset LatexCommand \index{-\/-cyclomatic}
8032 This option will cause the compiler to generate an information message for
8033 each function in the source file.
8034 The message contains some
8038 information about the function.
8039 The number of edges and nodes the compiler detected in the control flow
8040 graph of the function, and most importantly the
8042 cyclomatic complexity
8043 \begin_inset LatexCommand \index{Cyclomatic complexity}
8049 see section on Cyclomatic Complexity for more details.
8051 \labelwidthstring 00.00.0000
8066 \begin_inset LatexCommand \index{-\/-float-reent}
8072 Floating point library is compiled as reentrant
8073 \begin_inset LatexCommand \index{reentrant}
8078 See section Installation for more details.
8080 \labelwidthstring 00.00.0000
8095 \begin_inset LatexCommand \index{-\/-main-return}
8101 This option can be used if the code generated is called by a monitor program
8102 or if the main routine includes an endless loop.
8103 This option results in slightly smaller code and saves two bytes of stack
8105 The return from the 'main'
8106 \begin_inset LatexCommand \index{main return}
8110 function will return to the function calling main.
8111 The default setting is to lock up i.e.
8118 \labelwidthstring 00.00.0000
8133 \begin_inset LatexCommand \index{-\/-nostdinc}
8139 This will prevent the compiler from passing on the default include path
8140 to the preprocessor.
8142 \labelwidthstring 00.00.0000
8157 \begin_inset LatexCommand \index{-\/-nostdlib}
8163 This will prevent the compiler from passing on the default library
8164 \begin_inset LatexCommand \index{Libraries}
8170 \labelwidthstring 00.00.0000
8185 \begin_inset LatexCommand \index{-\/-verbose}
8191 Shows the various actions the compiler is performing.
8193 \labelwidthstring 00.00.0000
8198 \begin_inset LatexCommand \index{-V}
8204 Shows the actual commands the compiler is executing.
8206 \labelwidthstring 00.00.0000
8221 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8227 Hides your ugly and inefficient c-code from the asm file, so you can always
8228 blame the compiler :)
8230 \labelwidthstring 00.00.0000
8245 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8251 Will not include peep-hole comments in the generated files.
8253 \labelwidthstring 00.00.0000
8268 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8274 Include i-codes in the asm file.
8275 Sounds like noise but is most helpful for debugging the compiler itself.
8277 \labelwidthstring 00.00.0000
8292 \begin_inset LatexCommand \index{-\/-less-pedantic}
8298 Disable some of the more pedantic warnings
8299 \begin_inset LatexCommand \index{Warnings}
8303 (jwk burps: please be more specific here, please!).
8305 \labelwidthstring 00.00.0000
8319 -disable-warning\SpecialChar ~
8321 \begin_inset LatexCommand \index{-\/-disable-warning}
8327 Disable specific warning with number <nnnn>.
8329 \labelwidthstring 00.00.0000
8344 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8350 Display the directories in the compiler's search path
8352 \labelwidthstring 00.00.0000
8367 \begin_inset LatexCommand \index{-\/-vc}
8373 Display errors and warnings using MSVC style, so you can use SDCC with
8375 With SDCC both offering a GCC-like (the default) and a MSVC-like output
8376 style, integration into most programming editors should be straightforward.
8378 \labelwidthstring 00.00.0000
8393 \begin_inset LatexCommand \index{-\/-use-stdout}
8399 Send errors and warnings to stdout instead of stderr.
8401 \labelwidthstring 00.00.0000
8406 asmOption[,asmOption]
8409 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8414 Pass the asmOption to the assembler
8415 \begin_inset LatexCommand \index{Options assembler}
8420 \begin_inset LatexCommand \index{Assembler options}
8425 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8427 \labelwidthstring 00.00.0000
8442 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8448 Generally follow the C89 standard, but allow SDCC features that conflict
8449 with the standard (default).
8451 \labelwidthstring 00.00.0000
8466 \begin_inset LatexCommand \index{-\/-std-c89}
8472 Follow the C89 standard and disable SDCC features that conflict with the
8475 \labelwidthstring 00.00.0000
8490 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8496 Generally follow the C99 standard, but allow SDCC features that conflict
8497 with the standard (incomplete support).
8499 \labelwidthstring 00.00.0000
8514 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8520 Follow the C99 standard and disable SDCC features that conflict with the
8521 standard (incomplete support).
8523 \labelwidthstring 00.00.0000
8540 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8545 <Name> The name to be used for the code
8546 \begin_inset LatexCommand \index{code}
8550 segment, default CSEG.
8551 This is useful if you need to tell the compiler to put the code in a special
8552 segment so you can later on tell the linker to put this segment in a special
8554 Can be used for instance when using bank switching to put the code in a
8557 \labelwidthstring 00.00.0000
8574 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8579 <Name> The name to be used for the const
8580 \begin_inset LatexCommand \index{code}
8584 segment, default CONST.
8585 This is useful if you need to tell the compiler to put the const data in
8586 a special segment so you can later on tell the linker to put this segment
8587 in a special place in memory.
8588 Can be used for instance when using bank switching to put the const data
8591 \added_space_bottom bigskip \labelwidthstring 00.00.0000
8603 a SDCC compiler option but if you want
8607 warnings you can use a separate tool dedicated to syntax checking like
8609 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8614 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8619 \begin_inset LatexCommand \url{http://www.splint.org}
8624 To make your source files parseable by splint you will have to include
8630 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8634 in your source file and add brackets around extended keywords (like
8637 \begin_inset Quotes sld
8650 \begin_inset Quotes srd
8658 \begin_inset Quotes sld
8661 __interrupt\SpecialChar ~
8663 \begin_inset Quotes srd
8671 Splint has an excellent on line manual at
8672 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8676 and it's capabilities go beyond pure syntax checking.
8677 You'll need to tell splint the location of SDCC's include files so a typical
8678 command line could look like this:
8682 splint\SpecialChar ~
8684 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8689 Intermediate Dump Options
8690 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8695 \begin_inset LatexCommand \index{Options intermediate dump}
8700 \begin_inset LatexCommand \index{Intermediate dump options}
8707 The following options are provided for the purpose of retargetting and debugging
8709 They provide a means to dump the intermediate code (iCode
8710 \begin_inset LatexCommand \index{iCode}
8714 ) generated by the compiler in human readable form at various stages of
8715 the compilation process.
8716 More on iCodes see chapter
8717 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8722 \begin_inset Quotes srd
8725 The anatomy of the compiler
8726 \begin_inset Quotes srd
8731 \labelwidthstring 00.00.0000
8746 \begin_inset LatexCommand \index{-\/-dumpraw}
8752 This option will cause the compiler to dump the intermediate code into
8755 <source filename>.dumpraw
8757 just after the intermediate code has been generated for a function, i.e.
8758 before any optimizations are done.
8760 \begin_inset LatexCommand \index{Basic blocks}
8764 at this stage ordered in the depth first number, so they may not be in
8765 sequence of execution.
8767 \labelwidthstring 00.00.0000
8782 \begin_inset LatexCommand \index{-\/-dumpgcse}
8788 Will create a dump of iCode's, after global subexpression elimination
8789 \begin_inset LatexCommand \index{Global subexpression elimination}
8795 <source filename>.dumpgcse.
8797 \labelwidthstring 00.00.0000
8812 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8818 Will create a dump of iCode's, after deadcode elimination
8819 \begin_inset LatexCommand \index{Dead-code elimination}
8825 <source filename>.dumpdeadcode.
8827 \labelwidthstring 00.00.0000
8842 \begin_inset LatexCommand \index{-\/-dumploop}
8851 Will create a dump of iCode's, after loop optimizations
8852 \begin_inset LatexCommand \index{Loop optimization}
8858 <source filename>.dumploop.
8860 \labelwidthstring 00.00.0000
8875 \begin_inset LatexCommand \index{-\/-dumprange}
8884 Will create a dump of iCode's, after live range analysis
8885 \begin_inset LatexCommand \index{Live range analysis}
8891 <source filename>.dumprange.
8893 \labelwidthstring 00.00.0000
8908 \begin_inset LatexCommand \index{-\/-dumlrange}
8914 Will dump the life ranges
8915 \begin_inset LatexCommand \index{Live range analysis}
8921 \labelwidthstring 00.00.0000
8936 \begin_inset LatexCommand \index{-\/-dumpregassign}
8945 Will create a dump of iCode's, after register assignment
8946 \begin_inset LatexCommand \index{Register assignment}
8952 <source filename>.dumprassgn.
8954 \labelwidthstring 00.00.0000
8969 \begin_inset LatexCommand \index{-\/-dumplrange}
8975 Will create a dump of the live ranges of iTemp's
8977 \added_space_bottom bigskip \labelwidthstring 00.00.0000
8992 \begin_inset LatexCommand \index{-\/-dumpall}
9003 Will cause all the above mentioned dumps to be created.
9006 Redirecting output on Windows Shells
9008 \added_space_bottom bigskip
9009 By default SDCC writes it's error messages to
9010 \begin_inset Quotes sld
9014 \begin_inset Quotes srd
9018 To force all messages to
9019 \begin_inset Quotes sld
9023 \begin_inset Quotes srd
9047 \begin_inset LatexCommand \index{-\/-use-stdout}
9052 Additionally, if you happen to have visual studio installed in your windows
9053 machine, you can use it to compile your sources using a custom build and
9069 \begin_inset LatexCommand \index{-\/-vc}
9074 Something like this should work:
9118 -model-large -c $(InputPath)
9121 Environment variables
9122 \begin_inset LatexCommand \index{Environment variables}
9129 SDCC recognizes the following environment variables:
9131 \labelwidthstring 00.00.0000
9136 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
9142 SDCC installs a signal handler
9143 \begin_inset LatexCommand \index{signal handler}
9147 to be able to delete temporary files after an user break (^C) or an exception.
9148 If this environment variable is set, SDCC won't install the signal handler
9149 in order to be able to debug SDCC.
9151 \labelwidthstring 00.00.0000
9158 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9164 Path, where temporary files will be created.
9165 The order of the variables is the search order.
9166 In a standard *nix environment these variables are not set, and there's
9167 no need to set them.
9168 On Windows it's recommended to set one of them.
9170 \labelwidthstring 00.00.0000
9175 \begin_inset LatexCommand \index{SDCC\_HOME}
9182 \begin_inset LatexCommand \ref{sub:Install-paths}
9188 \begin_inset Quotes sld
9192 \begin_inset Quotes srd
9197 \labelwidthstring 00.00.0000
9202 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9209 \begin_inset LatexCommand \ref{sub:Search-Paths}
9215 \begin_inset Quotes sld
9219 \begin_inset Quotes srd
9224 \labelwidthstring 00.00.0000
9229 \begin_inset LatexCommand \index{SDCC\_LIB}
9236 \begin_inset LatexCommand \ref{sub:Search-Paths}
9242 \begin_inset Quotes sld
9246 \begin_inset Quotes srd
9251 \added_space_bottom bigskip
9252 There are some more environment variables recognized by SDCC, but these
9253 are solely used for debugging purposes.
9254 They can change or disappear very quickly, and will never be documented.
9257 Storage Class Language Extensions
9260 MCS51/DS390 Storage Class
9261 \begin_inset LatexCommand \index{Storage class}
9268 In addition to the ANSI storage classes SDCC allows the following MCS51
9269 specific storage classes:
9270 \layout Subsubsection
9273 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9278 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9283 \begin_inset LatexCommand \index{near (storage class)}
9288 \begin_inset LatexCommand \index{\_\_near (storage class)}
9299 storage class for the Small Memory model (
9307 or the more ANSI-C compliant forms
9315 can be used synonymously).
9316 Variables declared with this storage class will be allocated in the directly
9317 addressable portion of the internal RAM of a 8051, e.g.:
9322 __data unsigned char test_data;
9325 Writing 0x01 to this variable generates the assembly code:
9330 75*00 01\SpecialChar ~
9336 \layout Subsubsection
9339 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9344 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9349 \begin_inset LatexCommand \index{far (storage class)}
9354 \begin_inset LatexCommand \index{\_\_far (storage class)}
9361 Variables declared with this storage class will be placed in the external
9367 storage class for the Large Memory model, e.g.:
9372 __xdata unsigned char test_xdata;
9375 Writing 0x01 to this variable generates the assembly code:
9380 90s00r00\SpecialChar ~
9409 \layout Subsubsection
9412 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9417 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9424 Variables declared with this storage class will be allocated into the indirectly
9425 addressable portion of the internal ram of a 8051, e.g.:
9430 __idata unsigned char test_idata;
9433 Writing 0x01 to this variable generates the assembly code:
9462 Please note, the first 128 byte of idata physically access the same RAM
9464 The original 8051 had 128 byte idata memory, nowadays most devices have
9465 256 byte idata memory.
9467 \begin_inset LatexCommand \index{stack}
9471 is located in idata memory.
9472 \layout Subsubsection
9475 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9480 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9487 Paged xdata access is just as straightforward as using the other addressing
9489 It is typically located at the start of xdata and has a maximum size of
9491 The following example writes 0x01 to the pdata variable.
9492 Please note, pdata access physically accesses xdata memory.
9493 The high byte of the address is determined by port P2
9494 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9498 (or in case of some 8051 variants by a separate Special Function Register,
9500 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9509 storage class for the Medium Memory model, e.g.:
9514 __pdata unsigned char test_pdata;
9517 Writing 0x01 to this variable generates the assembly code:
9561 \begin_inset LatexCommand \index{-\/-xstack}
9565 option is used the pdata memory area is followed by the xstack memory area
9566 and the sum of their sizes is limited to 256 bytes.
9567 \layout Subsubsection
9570 \begin_inset LatexCommand \index{code}
9575 \begin_inset LatexCommand \index{\_\_code}
9582 'Variables' declared with this storage class will be placed in the code
9588 __code unsigned char test_code;
9591 Read access to this variable generates the assembly code:
9596 90s00r6F\SpecialChar ~
9599 mov dptr,#_test_code
9628 indexed arrays of characters in code memory can be accessed efficiently:
9633 __code char test_array[] = {'c','h','e','a','p'};
9636 Read access to this array using an 8-bit unsigned index generates the assembly
9653 90s00r41\SpecialChar ~
9656 mov dptr,#_test_array
9671 \layout Subsubsection
9674 \begin_inset LatexCommand \index{bit}
9679 \begin_inset LatexCommand \index{\_\_bit}
9686 This is a data-type and a storage class specifier.
9687 When a variable is declared as a bit, it is allocated into the bit addressable
9688 memory of 8051, e.g.:
9696 Writing 1 to this variable generates the assembly code:
9712 The bit addressable memory consists of 128 bits which are located from 0x20
9713 to 0x2f in data memory.
9716 Apart from this 8051 specific storage class most architectures support ANSI-C
9718 \begin_inset LatexCommand \index{bitfields}
9728 Not really meant as examples, but nevertheless showing what bitfields are
9729 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9733 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9734 signed modifier are implemented as unsigned.
9735 \layout Subsubsection
9738 \begin_inset LatexCommand \index{sfr}
9743 \begin_inset LatexCommand \index{\_\_sfr}
9748 \begin_inset LatexCommand \index{sfr16}
9753 \begin_inset LatexCommand \index{\_\_sfr16}
9758 \begin_inset LatexCommand \index{sfr32}
9763 \begin_inset LatexCommand \index{\_\_sfr32}
9768 \begin_inset LatexCommand \index{\_\_sbit}
9773 \begin_inset LatexCommand \index{sbit}
9780 Like the bit keyword,
9782 sfr / sfr16 / sfr32 / sbit
9784 signify both a data-type and storage class, they are used to describe the
9805 variables of a 8051, eg:
9811 \begin_inset LatexCommand \index{at}
9816 \begin_inset LatexCommand \index{\_\_at}
9820 (0x80) P0;\SpecialChar ~
9821 /* special function register P0 at location 0x80 */
9823 /* 16 bit special function register combination for timer 0 */
9825 /* with the high byte at location 0x8C and the low byte at location 0x8A
9829 \begin_inset LatexCommand \index{at}
9834 \begin_inset LatexCommand \index{\_\_at}
9840 __sbit __at (0xd7) CY; /* CY (Carry Flag
9841 \begin_inset LatexCommand \index{Flags}
9846 \begin_inset LatexCommand \index{Carry flag}
9853 Special function registers which are located on an address dividable by
9854 8 are bit-addressable, an
9858 addresses a specific bit within these sfr.
9860 16 Bit and 32 bit special function register combinations which require a
9861 certain access order are better not declared using
9870 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9871 this is not guaranteed.
9876 Please note, if you use a header file which was written for another compiler
9877 then the sfr / sfr16 / sfr32 / sbit Storage Class extensions will most
9883 Specifically the syntax
9886 sfr P0 = 0x80;\SpecialChar ~
9893 by SDCC to an assignment of 0x80 to a variable called P0
9896 \begin_inset Marginal
9910 Nevertheless it is possible to write header files
9911 \begin_inset LatexCommand \index{Header files}
9916 \begin_inset LatexCommand \index{Include files}
9920 which can be shared among different compilers (see section
9921 \begin_inset LatexCommand \ref{sec:Porting-code-to-other-compilers}
9927 \layout Subsubsection
9930 \begin_inset LatexCommand \index{Pointer}
9934 to MCS51/DS390 specific memory spaces
9937 SDCC allows (via language extensions) pointers to explicitly point to any
9938 of the memory spaces
9939 \begin_inset LatexCommand \index{Memory model}
9944 In addition to the explicit pointers, the compiler uses (by default) generic
9945 pointers which can be used to point to any of the memory spaces.
9949 Pointer declaration examples:
9954 /* pointer physically in internal ram pointing to object in external ram
9957 __xdata unsigned char * __data p;
9961 /* pointer physically in external ram pointing to object in internal ram
9964 __data unsigned char * __xdata p;
9968 /* pointer physically in code rom pointing to data in xdata space */
9970 __xdata unsigned char * __code p;
9974 /* pointer physically in code space pointing to data in code space */
9976 __code unsigned char * __code p;
9980 /* generic pointer physically located in xdata space */
9982 unsigned char * __xdata p;
9986 /* generic pointer physically located in default memory space */
9992 /* the following is a function pointer physically located in data space
9995 char (* __data fp)(void);
9998 Well you get the idea.
10003 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
10016 The highest order byte of the
10020 pointers contains the data space information.
10021 Assembler support routines are called whenever data is stored or retrieved
10027 These are useful for developing reusable library
10028 \begin_inset LatexCommand \index{Libraries}
10033 Explicitly specifying the pointer type will generate the most efficient
10035 \layout Subsubsection
10037 Notes on MCS51 memory
10038 \begin_inset LatexCommand \index{MCS51 memory}
10045 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
10046 RAM memory which is structured as follows:
10050 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
10053 - Bytes 20-2F - 16 bytes to hold 128 bit
10054 \begin_inset LatexCommand \index{bit}
10060 - Bytes 30-7F - 80 bytes for general purpose use.
10065 Additionally some members of the MCS51 family may have up to 128 bytes of
10066 additional, indirectly addressable, internal RAM memory (
10071 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
10076 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
10081 Furthermore, some chips may have some built in external memory (
10086 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10091 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10095 ) which should not be confused with the internal, directly addressable RAM
10101 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
10106 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
10111 Sometimes this built in
10115 memory has to be activated before using it (you can probably find this
10116 information on the datasheet of the microcontroller your are using, see
10118 \begin_inset LatexCommand \ref{sub:Startup-Code}
10126 Normally SDCC will only use the first bank
10127 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10131 of registers (register bank 0), but it is possible to specify that other
10132 banks of registers (keyword
10139 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10144 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10150 ) should be used in interrupt
10151 \begin_inset LatexCommand \index{interrupt}
10156 \begin_inset LatexCommand \index{\_\_interrupt}
10161 By default, the compiler will place the stack after the last byte of allocated
10162 memory for variables.
10163 For example, if the first 2 banks of registers are used, and only four
10168 variables, it will position the base of the internal stack at address 20
10170 This implies that as the stack
10171 \begin_inset LatexCommand \index{stack}
10175 grows, it will use up the remaining register banks, and the 16 bytes used
10176 by the 128 bit variables, and 80 bytes for general purpose use.
10177 If any bit variables are used, the data variables will be placed in unused
10178 register banks and after the byte holding the last bit variable.
10179 For example, if register banks 0 and 1 are used, and there are 9 bit variables
10184 variables will be placed starting from address 0x10 to 0x20 and continue
10197 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
10201 to specify the start address of the
10216 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10220 to specify the size of the total internal RAM (
10234 By default the 8051 linker will place the stack after the last byte of (i)data
10247 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10251 allows you to specify the start of the stack, i.e.
10252 you could start it after any data in the general purpose area.
10253 If your microcontroller has additional indirectly addressable internal
10258 ) you can place the stack on it.
10259 You may also need to use -
10270 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10274 to set the start address of the external RAM (
10289 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10293 to specify its size.
10294 Same goes for the code memory, using -
10305 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10320 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10325 If in doubt, don't specify any options and see if the resulting memory
10326 layout is appropriate, then you can adjust it.
10328 \added_space_bottom bigskip
10329 The linker generates two files with memory allocation information.
10330 The first, with extension .map
10331 \begin_inset LatexCommand \index{<file>.map}
10335 shows all the variables and segments.
10336 The second with extension .mem
10337 \begin_inset LatexCommand \index{<file>.mem}
10341 shows the final memory layout.
10342 The linker will complain either if memory segments overlap, there is not
10343 enough memory, or there is not enough space for stack.
10344 If you get any linking warnings and/or errors related to stack or segments
10345 allocation, take a look at either the .map or .mem files to find out what
10347 The .mem file may even suggest a solution to the problem.
10350 Z80/Z180 Storage Class
10351 \begin_inset LatexCommand \index{Z80!Storage class}
10355 Language Extensions
10356 \layout Subsubsection
10359 \begin_inset LatexCommand \index{sfr}
10364 \begin_inset LatexCommand \index{\_\_sfr}
10368 (in/out to 8-bit addresses)
10372 \begin_inset LatexCommand \index{Z80}
10376 family has separate address spaces for memory and
10386 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10391 \begin_inset LatexCommand \index{Z80!I/O memory}
10396 \begin_inset LatexCommand \index{Z180!I/O memory}
10400 is accessed with special instructions, e.g.:
10405 sfr at 0x78 IoPort;\SpecialChar ~
10407 /* define a var in I/O space at 78h called IoPort */
10411 Writing 0x01 to this variable generates the assembly code:
10416 3E 01\SpecialChar ~
10424 D3 78\SpecialChar ~
10431 \layout Subsubsection
10434 \begin_inset LatexCommand \index{sfr}
10439 \begin_inset LatexCommand \index{\_\_sfr}
10443 (in/out to 16-bit addresses)
10450 is used to support 16 bit addresses in I/O memory e.g.:
10456 \begin_inset LatexCommand \index{at}
10461 \begin_inset LatexCommand \index{\_\_at}
10468 Writing 0x01 to this variable generates the assembly code:
10473 01 23 01\SpecialChar ~
10478 3E 01\SpecialChar ~
10486 ED 79\SpecialChar ~
10493 \layout Subsubsection
10496 \begin_inset LatexCommand \index{sfr}
10501 \begin_inset LatexCommand \index{\_\_sfr}
10505 (in0/out0 to 8 bit addresses on Z180
10506 \begin_inset LatexCommand \index{Z180}
10511 \begin_inset LatexCommand \index{HD64180 (see Z180)}
10517 \added_space_bottom bigskip
10518 The compiler option -
10529 \begin_inset LatexCommand \index{Z180!Options!-\/-portmode}
10533 =180 (80) and a compiler #pragma\SpecialChar ~
10535 \begin_inset LatexCommand \index{Z180!Pragmas!\#pragma portmode}
10539 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10549 If you include the file z180.h this will be set automatically.
10553 \begin_inset LatexCommand \index{HC08!Storage class}
10557 Language Extensions
10558 \layout Subsubsection
10561 \begin_inset LatexCommand \index{data (hc08 storage class)}
10566 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10573 The data storage class declares a variable that resides in the first 256
10574 bytes of memory (the direct page).
10576 \begin_inset LatexCommand \index{HC08}
10580 is most efficient at accessing variables (especially pointers) stored here.
10581 \layout Subsubsection
10584 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10589 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10595 \added_space_bottom bigskip
10596 The xdata storage class declares a variable that can reside anywhere in
10598 This is the default if no storage class is specified.
10602 Absolute Addressing
10603 \begin_inset LatexCommand \index{Absolute addressing}
10610 Data items can be assigned an absolute address with the
10613 \begin_inset LatexCommand \index{at}
10618 \begin_inset LatexCommand \index{\_\_at}
10624 keyword, in addition to a storage class, e.g.:
10630 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10635 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10640 \begin_inset LatexCommand \index{at}
10645 \begin_inset LatexCommand \index{\_\_at}
10649 0x7ffe unsigned int chksum;
10652 or, better conforming to ISO/IEC 9899 C:
10657 __xdata __at (0x7ffe) unsigned int chksum;
10660 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10661 of the external ram.
10666 reserve any space for variables declared in this way
10667 \begin_inset Marginal
10678 (they are implemented with an equate in the assembler).
10679 Thus it is left to the programmer to make sure there are no overlaps with
10680 other variables that are declared without the absolute address.
10681 The assembler listing file (.lst
10682 \begin_inset LatexCommand \index{<file>.lst}
10686 ) and the linker output files (.rst
10687 \begin_inset LatexCommand \index{<file>.rst}
10692 \begin_inset LatexCommand \index{<file>.map}
10696 ) are good places to look for such overlaps.
10697 Variables with an absolute address are
10700 \begin_inset Marginal
10714 \begin_inset LatexCommand \index{Variable initialization}
10721 In case of memory mapped I/O devices the keyword
10725 has to be used to tell the compiler that accesses might not be removed:
10731 \begin_inset LatexCommand \index{volatile}
10736 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10741 \begin_inset LatexCommand \index{at}
10745 (0x8000) unsigned char PORTA_8255;
10748 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10753 \begin_inset LatexCommand \index{Aligned array}
10760 starts at a block (256 byte) boundary
10761 \begin_inset LatexCommand \index{block boundary}
10766 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10772 Absolute addresses can be specified for variables in all storage classes,
10779 \begin_inset LatexCommand \index{bit}
10784 \begin_inset LatexCommand \index{at}
10791 The above example will allocate the variable at offset 0x02 in the bit-addressab
10793 There is no real advantage to assigning absolute addresses to variables
10794 in this manner, unless you want strict control over all the variables allocated.
10795 One possible use would be to write hardware portable code.
10796 For example, if you have a routine that uses one or more of the microcontroller
10797 I/O pins, and such pins are different for two different hardwares, you
10798 can declare the I/O pins in your routine using:
10804 \begin_inset LatexCommand \index{volatile}
10808 __bit MOSI;\SpecialChar ~
10812 /* master out, slave in */
10814 extern volatile __bit MISO;\SpecialChar ~
10818 /* master in, slave out */
10820 extern volatile __bit MCLK;\SpecialChar ~
10828 /* Input and Output of a byte on a 3-wire serial bus.
10833 If needed adapt polarity of clock, polarity of data and bit order
10838 unsigned char spi_io(unsigned char out_byte)
10862 MOSI = out_byte & 0x80;
10892 /* _asm nop _endasm; */\SpecialChar ~
10900 /* for slow peripherals */
10951 Then, someplace in the code for the first hardware you would use
10957 \begin_inset LatexCommand \index{at}
10962 \begin_inset LatexCommand \index{\_\_at}
10966 (0x80) MOSI;\SpecialChar ~
10970 /* I/O port 0, bit 0 */
10972 __bit __at (0x81) MISO;\SpecialChar ~
10976 /* I/O port 0, bit 1 */
10978 __bit __at (0x82) MCLK;\SpecialChar ~
10982 /* I/O port 0, bit 2 */
10985 Similarly, for the second hardware you would use
10990 __bit __at (0x83) MOSI;\SpecialChar ~
10994 /* I/O port 0, bit 3 */
10996 __bit __at (0x91) MISO;\SpecialChar ~
11000 /* I/O port 1, bit 1 */
11003 \begin_inset LatexCommand \index{bit}
11007 __at (0x92) MCLK;\SpecialChar ~
11011 /* I/O port 1, bit 2 */
11013 \added_space_bottom bigskip
11014 and you can use the same hardware dependent routine without changes, as
11015 for example in a library.
11016 This is somehow similar to sbit, but only one absolute address has to be
11017 specified in the whole project.
11021 \begin_inset LatexCommand \index{Parameters}
11026 \begin_inset LatexCommand \index{function parameter}
11031 \begin_inset LatexCommand \index{local variables}
11036 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
11043 Automatic (local) variables and parameters to functions can either be placed
11044 on the stack or in data-space.
11045 The default action of the compiler is to place these variables in the internal
11046 RAM (for small model) or external RAM (for large model).
11047 This in fact makes them similar to
11050 \begin_inset LatexCommand \index{static}
11056 so by default functions are non-reentrant
11057 \begin_inset LatexCommand \index{reentrant}
11066 They can be placed on the stack
11067 \begin_inset LatexCommand \index{stack}
11084 \begin_inset LatexCommand \index{-\/-stack-auto}
11092 #pragma\SpecialChar ~
11096 \begin_inset LatexCommand \index{\#pragma stackauto}
11103 \begin_inset LatexCommand \index{reentrant}
11109 keyword in the function declaration, e.g.:
11114 unsigned char foo(char i) __reentrant
11128 Since stack space on 8051 is limited, the
11146 option should be used sparingly.
11147 Note that the reentrant keyword just means that the parameters & local
11148 variables will be allocated to the stack, it
11152 mean that the function is register bank
11153 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11162 \begin_inset LatexCommand \index{local variables}
11166 can be assigned storage classes and absolute
11167 \begin_inset LatexCommand \index{Absolute addressing}
11176 unsigned char foo()
11184 __xdata unsigned char i;
11197 \begin_inset LatexCommand \index{at}
11201 (0x31) unsigned char j;
11213 In the above example the variable
11217 will be allocated in the external ram,
11221 in bit addressable space and
11240 or when a function is declared as
11244 this should only be done for static variables.
11248 \begin_inset LatexCommand \index{function parameter}
11252 however are not allowed any storage class
11253 \begin_inset LatexCommand \index{Storage class}
11257 , (storage classes for parameters will be ignored), their allocation is
11258 governed by the memory model in use, and the reentrancy options.
11261 It is however allowed to use bit parameters in reentrant functions and also
11262 non-static local bit variables are supported.
11263 Efficient use is limited to 8 semi-bitregisters in bit space.
11264 They are pushed and popped to stack
11265 \begin_inset LatexCommand \index{stack}
11269 as a single byte just like the normal registers.
11273 \begin_inset LatexCommand \label{sub:Overlaying}
11278 \begin_inset LatexCommand \index{Overlaying}
11286 \begin_inset LatexCommand \index{reentrant}
11290 functions SDCC will try to reduce internal ram space usage by overlaying
11291 parameters and local variables of a function (if possible).
11292 Parameters and local variables
11293 \begin_inset LatexCommand \index{local variables}
11297 of a function will be allocated to an overlayable segment if the function
11300 no other function calls and the function is non-reentrant and the memory
11302 \begin_inset LatexCommand \index{Memory model}
11309 If an explicit storage class
11310 \begin_inset LatexCommand \index{Storage class}
11314 is specified for a local variable, it will NOT be overlayed.
11317 Note that the compiler (not the linkage editor) makes the decision for overlayin
11319 Functions that are called from an interrupt service routine
11320 \begin_inset Marginal
11330 should be preceded by a #pragma\SpecialChar ~
11332 \begin_inset LatexCommand \index{\#pragma nooverlay}
11336 if they are not reentrant.
11339 Also note that the compiler does not do any processing of inline assembler
11340 code, so the compiler might incorrectly assign local variables and parameters
11341 of a function into the overlay segment if the inline assembler code calls
11342 other c-functions that might use the overlay.
11343 In that case the #pragma\SpecialChar ~
11344 nooverlay should be used.
11347 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11349 \begin_inset LatexCommand \index{Multiplication}
11354 \begin_inset LatexCommand \index{Division}
11358 will NOT be overlayed since these are implemented using external functions,
11367 \begin_inset LatexCommand \index{\#pragma nooverlay}
11373 void set_error(unsigned char errcd)
11389 void some_isr () __interrupt
11390 \begin_inset LatexCommand \index{interrupt}
11419 \added_space_bottom bigskip
11420 In the above example the parameter
11428 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11430 not present, this could cause unpredictable runtime behavior when called
11431 from an interrupt service routine.
11432 The #pragma\SpecialChar ~
11433 nooverlay ensures that the parameters and local variables for
11434 the function are NOT overlayed.
11437 Interrupt Service Routines
11438 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11445 General Information
11460 outines to be coded in C, with some extended keywords.
11465 void timer_isr (void) __interrupt (1) __using (1)
11479 The optional number following the
11482 \begin_inset LatexCommand \index{interrupt}
11487 \begin_inset LatexCommand \index{\_\_interrupt}
11493 keyword is the interrupt number this routine will service.
11494 When present, the compiler will insert a call to this routine in the interrupt
11495 vector table for the interrupt number specified.
11496 If you have multiple source files in your project, interrupt service routines
11497 can be present in any of them, but a prototype of the isr MUST be present
11498 or included in the file that contains the function
11503 The optional (8051 specific) keyword
11506 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11511 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11517 can be used to tell the compiler to use the specified register bank when
11518 generating code for this function.
11521 Interrupt service routines open the door for some very interesting bugs:
11522 \layout Subsubsection
11525 \begin_inset LatexCommand \label{sub:Common-interrupt-pitfall-volatile}
11529 Common interrupt pitfall: variable not declared
11534 If an interrupt service routine changes variables which are accessed by
11535 other functions these variables have to be declared
11540 \begin_inset LatexCommand \index{volatile}
11546 \begin_inset LatexCommand \url{http://en.wikipedia.org/wiki/Volatile_variable}
11551 \layout Subsubsection
11554 \begin_inset LatexCommand \label{sub:Common-interrupt-pitfall-non-atomic}
11558 Common interrupt pitfall:
11563 If the access to these variables is not
11566 \begin_inset LatexCommand \index{atomic}
11573 the processor needs more than one instruction for the access and could
11574 be interrupted while accessing the variable) the interrupt must be disabled
11575 during the access to avoid inconsistent data.
11578 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs and
11579 should be protected by disabling interrupts.
11580 You're not automatically on the safe side if you use 8 bit variables though.
11581 We need an example here: f.e.
11582 on the 8051 the harmless looking
11583 \begin_inset Quotes srd
11588 flags\SpecialChar ~
11593 \begin_inset Quotes sld
11602 \begin_inset Quotes srd
11607 flags\SpecialChar ~
11612 \begin_inset Quotes sld
11615 from within an interrupt routine might get lost if the interrupt occurs
11618 \begin_inset Quotes sld
11623 counter\SpecialChar ~
11628 \begin_inset Quotes srd
11631 is not atomic on the 8051 even if
11635 is located in data memory.
11637 Bugs like these are hard to reproduce and can cause a lot of trouble.
11639 \layout Subsubsection
11642 \begin_inset LatexCommand \label{sub:Common-interrupt-pitfall-stack-overflow}
11646 Common interrupt pitfall:
11651 The return address and the registers used in the interrupt service routine
11652 are saved on the stack
11653 \begin_inset LatexCommand \index{stack}
11657 so there must be sufficient stack space.
11658 If there isn't variables or registers (or even the return address itself)
11665 \begin_inset LatexCommand \index{stack overflow}
11669 is most likely to happen if the interrupt occurs during the
11670 \begin_inset Quotes sld
11674 \begin_inset Quotes srd
11677 subroutine when the stack is already in use for f.e.
11678 many return addresses.
11679 \layout Subsubsection
11682 \begin_inset LatexCommand \label{sub:Common-interrupt-pitfall-non-reentrant}
11686 Common interrupt pitfall:
11688 use of non-reentrant functions
11691 A special note here, int (16 bit) and long (32 bit) integer division
11692 \begin_inset LatexCommand \index{Division}
11697 \begin_inset LatexCommand \index{Multiplication}
11702 \begin_inset LatexCommand \index{Modulus}
11707 \begin_inset LatexCommand \index{Floating point support}
11711 operations are implemented using external support routines.
11712 If an interrupt service routine needs to do any of these operations then
11713 the support routines (as mentioned in a following section) will have to
11714 be recompiled using the
11727 \begin_inset LatexCommand \index{-\/-stack-auto}
11733 option and the source file will need to be compiled using the
11748 \begin_inset LatexCommand \index{-\/-int-long-reent}
11755 Note, the type promotion
11756 \begin_inset LatexCommand \index{type promotion}
11760 required by ANSI C can cause 16 bit routines to be used
11761 \begin_inset Marginal
11772 without the programmer being aware of it.
11776 (unsigned char)(tail-1)
11778 within the if clause in section
11779 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
11785 \added_space_bottom bigskip
11786 Calling other functions from an interrupt service routine is not recommended,
11787 avoid it if possible.
11788 Note that when some function is called from an interrupt service routine
11789 it should be preceded by a #pragma\SpecialChar ~
11791 \begin_inset LatexCommand \index{\#pragma nooverlay}
11795 if it is not reentrant.
11796 Furthermore nonreentrant functions should not be called from the main program
11797 while the interrupt service routine might be active.
11798 They also must not be called from low priority interrupt service routines
11799 while a high priority interrupt service routine might be active.
11800 You could use semaphores or make the function
11804 if all parameters are passed in registers.
11807 \begin_inset LatexCommand \ref{sub:Overlaying}
11812 about Overlaying and section
11813 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11818 about Functions using private register banks.
11821 MCS51/DS390 Interrupt Service Routines
11824 Interrupt numbers and the corresponding address & descriptions for the Standard
11825 8051/8052 are listed below.
11826 SDCC will automatically adjust the interrupt vector table to the maximum
11827 interrupt number specified.
11833 \begin_inset Tabular
11834 <lyxtabular version="3" rows="9" columns="3">
11836 <column alignment="center" valignment="top" leftline="true" width="0in">
11837 <column alignment="left" valignment="top" leftline="true" width="0in">
11838 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11839 <row topline="true" bottomline="true">
11840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11865 <row topline="true">
11866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11891 <row topline="true">
11892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11917 <row topline="true">
11918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11943 <row topline="true">
11944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11969 <row topline="true">
11970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11995 <row topline="true">
11996 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12021 <row topline="true">
12022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12037 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12046 <row topline="true" bottomline="true">
12047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12055 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12080 If the interrupt service routine is defined without
12083 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12088 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12094 a register bank or with register bank 0 (
12098 0), the compiler will save the registers used by itself on the stack upon
12099 entry and restore them at exit, however if such an interrupt service routine
12100 calls another function then the entire register bank will be saved on the
12102 This scheme may be advantageous for small interrupt service routines which
12103 have low register usage.
12105 \added_space_bottom bigskip
12106 If the interrupt service routine is defined to be using a specific register
12111 & psw are saved and restored, if such an interrupt service routine calls
12112 another function (using another register bank) then the entire register
12113 bank of the called function will be saved on the stack
12114 \begin_inset LatexCommand \index{stack}
12119 This scheme is recommended for larger interrupt service routines.
12123 \begin_inset LatexCommand \index{HC08}
12127 Interrupt Service Routines
12129 \added_space_bottom bigskip
12130 Since the number of interrupts
12131 \begin_inset LatexCommand \index{HC08!interrupt}
12135 available is chip specific and the interrupt vector table always ends at
12136 the last byte of memory, the interrupt numbers corresponds to the interrupt
12137 vectors in reverse order of address.
12138 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
12139 2 will use the interrupt vector at 0xfffa, and so on.
12140 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
12141 this way; instead see section
12142 \begin_inset LatexCommand \ref{sub:Startup-Code}
12146 for details on customizing startup.
12149 Z80 Interrupt Service Routines
12153 \begin_inset LatexCommand \index{Z80}
12157 uses several different methods for determining the correct interrupt
12158 \begin_inset LatexCommand \index{Z80!interrupt}
12162 vector depending on the hardware implementation.
12163 Therefore, SDCC ignores the optional interrupt number and does not attempt
12164 to generate an interrupt vector table.
12167 By default, SDCC generates code for a maskable interrupt, which uses a RETI
12168 instruction to return from the interrupt.
12169 To write an interrupt handler for the non-maskable interrupt, which needs
12170 a RETN instruction instead, add the
12179 void nmi_isr (void) critical interrupt
12192 \added_space_bottom bigskip
12193 However if you need to create a non-interruptable interrupt service routine
12194 you would also require the
12199 To distinguish between this and an nmi_isr you must provide an interrupt
12203 Enabling and Disabling Interrupts
12206 Critical Functions and Critical Statements
12209 A special keyword may be associated with a block or a function declaring
12215 SDCC will generate code to disable all interrupts
12216 \begin_inset LatexCommand \index{interrupt}
12220 upon entry to a critical function and restore the interrupt enable to the
12221 previous state before returning.
12222 Nesting critical functions will need one additional byte on the stack
12223 \begin_inset LatexCommand \index{stack}
12232 int foo () __critical
12233 \begin_inset LatexCommand \index{critical}
12238 \begin_inset LatexCommand \index{\_\_critical}
12263 The critical attribute maybe used with other attributes like
12273 may also be used to disable interrupts more locally:
12281 More than one statement could have been included in the block.
12284 Enabling and Disabling Interrupts directly
12288 \begin_inset LatexCommand \index{interrupt}
12292 can also be disabled and enabled directly (8051):
12297 EA = 0;\SpecialChar ~
12360 EA = 1;\SpecialChar ~
12427 On other architectures which have seperate opcodes for enabling and disabling
12428 interrupts you might want to make use of defines with inline assembly
12429 \begin_inset LatexCommand \index{Assembler routines}
12434 \begin_inset LatexCommand \index{HC08!interrupt}
12444 \begin_inset LatexCommand \index{\_asm}
12453 \begin_inset LatexCommand \index{\_endasm}
12462 #define SEI _asm\SpecialChar ~
12474 Note: it is sometimes sufficient to disable only a specific interrupt source
12476 a timer or serial interrupt by manipulating an
12479 \begin_inset LatexCommand \index{interrupt mask}
12489 Usually the time during which interrupts are disabled should be kept as
12491 This minimizes both
12496 \begin_inset LatexCommand \index{interrupt latency}
12500 (the time between the occurrence of the interrupt and the execution of
12501 the first code in the interrupt routine) and
12506 \begin_inset LatexCommand \index{interrupt jitter}
12510 (the difference between the shortest and the longest interrupt latency).
12511 These really are something different, f.e.
12512 a serial interrupt has to be served before its buffer overruns so it cares
12513 for the maximum interrupt latency, whereas it does not care about jitter.
12514 On a loudspeaker driven via a digital to analog converter which is fed
12515 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12516 a much smaller jitter will be very audible.
12519 You can reenable interrupts within an interrupt routine and on some architecture
12520 s you can make use of two (or more) levels of
12522 interrupt priorities
12525 \begin_inset LatexCommand \index{interrupt priority}
12530 On some architectures which don't support interrupt priorities these can
12531 be implemented by manipulating the interrupt mask and reenabling interrupts
12532 within the interrupt routine.
12533 Check there is sufficient space on the stack
12534 \begin_inset LatexCommand \index{stack}
12538 and don't add complexity unless you have to.
12543 \begin_inset LatexCommand \index{semaphore}
12547 locking (mcs51/ds390)
12550 Some architectures (mcs51/ds390) have an atomic
12551 \begin_inset LatexCommand \index{atomic}
12564 These type of instructions are typically used in preemptive multitasking
12565 systems, where a routine f.e.
12566 claims the use of a data structure ('acquires a lock
12567 \begin_inset LatexCommand \index{lock}
12571 on it'), makes some modifications and then releases the lock when the data
12572 structure is consistent again.
12573 The instruction may also be used if interrupt and non-interrupt code have
12574 to compete for a resource.
12575 With the atomic bit test and clear instruction interrupts
12576 \begin_inset LatexCommand \index{interrupt}
12580 don't have to be disabled for the locking operation.
12584 SDCC generates this instruction if the source follows this pattern:
12590 \begin_inset LatexCommand \index{volatile}
12594 bit resource_is_free;
12598 if (resource_is_free)
12608 resource_is_free=0;
12621 resource_is_free=1;
12628 Note, mcs51 and ds390 support only an atomic
12629 \begin_inset LatexCommand \index{atomic}
12637 instruction (as opposed to atomic bit test and
12642 Functions using private register banks
12643 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12650 Some architectures have support for quickly changing register sets.
12651 SDCC supports this feature with the
12654 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12659 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12665 attribute (which tells the compiler to use a register bank
12666 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12670 other than the default bank zero).
12671 It should only be applied to
12674 \begin_inset LatexCommand \index{interrupt}
12680 functions (see footnote below).
12681 This will in most circumstances make the generated ISR code more efficient
12682 since it will not have to save registers on the stack.
12689 attribute will have no effect on the generated code for a
12693 function (but may occasionally be useful anyway
12699 possible exception: if a function is called ONLY from 'interrupt' functions
12700 using a particular bank, it can be declared with the same 'using' attribute
12701 as the calling 'interrupt' functions.
12702 For instance, if you have several ISRs using bank one, and all of them
12703 call memcpy(), it might make sense to create a specialized version of memcpy()
12704 'using 1', since this would prevent the ISR from having to save bank zero
12705 to the stack on entry and switch to bank zero before calling the function
12712 (pending: Note, nowadays the
12716 attribute has an effect on
12720 the generated code for a
12735 function using a non-zero bank will assume that it can trash that register
12736 bank, and will not save it.
12737 Since high-priority interrupts
12738 \begin_inset LatexCommand \index{interrupts}
12743 \begin_inset LatexCommand \index{interrupt priority}
12747 can interrupt low-priority ones on the 8051 and friends, this means that
12748 if a high-priority ISR
12752 a particular bank occurs while processing a low-priority ISR
12756 the same bank, terrible and bad things can happen.
12757 To prevent this, no single register bank should be
12761 by both a high priority and a low priority ISR.
12762 This is probably most easily done by having all high priority ISRs use
12763 one bank and all low priority ISRs use another.
12764 If you have an ISR which can change priority at runtime, you're on your
12765 own: I suggest using the default bank zero and taking the small performance
12768 \added_space_bottom bigskip
12769 It is most efficient if your ISR calls no other functions.
12770 If your ISR must call other functions, it is most efficient if those functions
12771 use the same bank as the ISR (see note 1 below); the next best is if the
12772 called functions use bank zero.
12773 It is very inefficient to call a function using a different, non-zero bank
12779 \begin_inset LatexCommand \label{sub:Startup-Code}
12784 \begin_inset LatexCommand \index{Startup code}
12791 MCS51/DS390 Startup Code
12794 The compiler inserts a call to the C routine
12796 _sdcc_external_startup()
12797 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12806 at the start of the CODE area.
12807 This routine is in the runtime library
12808 \begin_inset LatexCommand \index{Runtime library}
12813 By default this routine returns 0, if this routine returns a non-zero value,
12814 the static & global variable initialization will be skipped and the function
12815 main will be invoked.
12816 Otherwise static & global variables will be initialized before the function
12820 _sdcc_external_startup()
12822 routine to your program to override the default if you need to setup hardware
12823 or perform some other critical operation prior to static & global variable
12825 \begin_inset LatexCommand \index{Variable initialization}
12830 On some mcs51 variants xdata
12831 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12835 memory has to be explicitly enabled before it can be accessed or if the
12837 \begin_inset LatexCommand \index{watchdog}
12841 needs to be disabled, this is the place to do it.
12842 The startup code clears all internal data memory, 256 bytes by default,
12843 but from 0 to n-1 if
12856 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12863 (recommended for Chipcon CC1010).
12865 \added_space_bottom bigskip
12866 See also the compiler options
12885 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12904 \begin_inset LatexCommand \index{-\/-main-return}
12909 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12914 about MCS51-variants.
12919 \added_space_bottom bigskip
12921 \begin_inset LatexCommand \index{HC08}
12925 startup code follows the same scheme as the MCS51 startup code.
12930 \added_space_bottom bigskip
12932 \begin_inset LatexCommand \index{Z80}
12936 the startup code is inserted by linking with crt0.o which is generated from
12937 sdcc/device/lib/z80/crt0.s.
12938 If you need a different startup code you can use the compiler option
12959 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12963 and provide your own crt0.o.
12967 Inline Assembler Code
12968 \begin_inset LatexCommand \index{Assembler routines}
12975 A Step by Step Introduction
12976 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12983 Starting from a small snippet of c-code this example shows for the MCS51
12984 how to use inline assembly, access variables, a function parameter and
12985 an array in xdata memory.
12986 The example uses an MCS51 here but is easily adapted for other architectures.
12987 This is a buffer routine which should be optimized:
12993 unsigned char __far
12994 \begin_inset LatexCommand \index{far (storage class)}
12999 \begin_inset LatexCommand \index{\_\_far (storage class)}
13004 \begin_inset LatexCommand \index{at}
13009 \begin_inset LatexCommand \index{\_\_at}
13013 (0x7f00) buf[0x100];
13014 \begin_inset LatexCommand \index{Aligned array}
13020 unsigned char head, tail;\SpecialChar ~
13038 \begin_inset LatexCommand \index{interrupt}
13090 \begin_inset LatexCommand \ref{sub:Common-interrupt-pitfall-volatile}
13102 void to_buffer( unsigned char c )
13110 if( head != (unsigned char)(tail-1) )\SpecialChar ~
13116 \begin_inset LatexCommand \index{promotion to signed int}
13121 \begin_inset LatexCommand \index{type promotion}
13126 \begin_inset Marginal
13147 buf[ head++ ] = c;\SpecialChar ~
13163 /* access to a 256 byte aligned array */
13168 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
13169 then a corresponding buffer.asm file is generated.
13170 We define a new function
13174 in file buffer.c in which we cut and paste the generated code, removing
13175 unwanted comments and some ':'.
13177 \begin_inset Quotes sld
13181 \begin_inset Quotes srd
13185 \begin_inset Quotes sld
13189 \begin_inset Quotes srd
13192 to the beginning and the end of the function body:
13198 /* With a cut and paste from the .asm file, we have something to start with.
13203 The function is not yet OK! (registers aren't saved) */
13205 void to_buffer_asm( unsigned char c )
13214 \begin_inset LatexCommand \index{\_asm}
13219 \begin_inset LatexCommand \index{\_\_asm}
13233 ;buffer.c if( head != (unsigned char)(tail-1) ) \SpecialChar ~
13239 \begin_inset LatexCommand \index{promotion to signed int}
13244 \begin_inset LatexCommand \index{type promotion}
13296 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
13297 \begin_inset LatexCommand \index{Aligned array}
13362 \begin_inset LatexCommand \index{\_endasm}
13367 \begin_inset LatexCommand \index{\_\_endasm}
13376 The new file buffer.c should compile with only one warning about the unreferenced
13377 function argument 'c'.
13378 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
13379 (1) and finally have:
13385 unsigned char __far __at(0x7f00) buf[0x100];
13387 unsigned char head, tail;
13389 #define USE_ASSEMBLY (1)
13397 void to_buffer( unsigned char c )
13405 if( head != (unsigned char)(tail-1) )
13425 void to_buffer( unsigned char c )
13433 c; // to avoid warning: unreferenced function argument
13440 \begin_inset LatexCommand \index{\_asm}
13445 \begin_inset LatexCommand \index{\_\_asm}
13459 ; save used registers here.
13470 ; If we were still using r2,r3 we would have to push them here.
13473 ; if( head != (unsigned char)(tail-1) )
13516 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13540 ; buf[ head++ ] = c;
13551 a,dpl \SpecialChar ~
13558 ; dpl holds lower byte of function argument
13569 dpl,_head \SpecialChar ~
13572 ; buf is 0x100 byte aligned so head can be used directly
13614 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13626 ; restore used registers here
13633 \begin_inset LatexCommand \index{\_endasm}
13638 \begin_inset LatexCommand \index{\_\_endasm}
13649 The inline assembler code can contain any valid code understood by the assembler
13650 , this includes any assembler directives and comment lines.
13651 The assembler does not like some characters like ':' or ''' in comments.
13652 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13653 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13658 \begin_inset LatexCommand \index{Assembler documentation}
13663 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/as/doc/asxhtm.html}
13671 The compiler does not do any validation of the code within the
13674 \begin_inset LatexCommand \index{\_asm}
13679 \begin_inset LatexCommand \index{\_\_asm}
13687 \begin_inset LatexCommand \index{\_endasm}
13692 \begin_inset LatexCommand \index{\_\_endasm}
13701 Specifically it will not know which registers are used and thus register
13703 \begin_inset LatexCommand \index{push/pop}
13707 has to be done manually.
13711 It is recommended that each assembly instruction (including labels) be placed
13712 in a separate line (as the example shows).
13726 \begin_inset LatexCommand \index{-\/-peep-asm}
13732 command line option is used, the inline assembler code will be passed through
13733 the peephole optimizer
13734 \begin_inset LatexCommand \index{Peephole optimizer}
13739 There are only a few (if any) cases where this option makes sense, it might
13740 cause some unexpected changes in the inline assembler code.
13741 Please go through the peephole optimizer rules defined in file
13745 before using this option.
13749 \begin_inset LatexCommand \label{sub:Naked-Functions}
13754 \begin_inset LatexCommand \index{Naked functions}
13761 A special keyword may be associated with a function declaring it as
13764 \begin_inset LatexCommand \index{\_naked}
13769 \begin_inset LatexCommand \index{\_\_naked}
13780 function modifier attribute prevents the compiler from generating prologue
13781 \begin_inset LatexCommand \index{function prologue}
13786 \begin_inset LatexCommand \index{function epilogue}
13790 code for that function.
13791 This means that the user is entirely responsible for such things as saving
13792 any registers that may need to be preserved, selecting the proper register
13793 bank, generating the
13797 instruction at the end, etc.
13798 Practically, this means that the contents of the function must be written
13799 in inline assembler.
13800 This is particularly useful for interrupt functions, which can have a large
13801 (and often unnecessary) prologue/epilogue.
13802 For example, compare the code generated by these two functions:
13808 \begin_inset LatexCommand \index{volatile}
13812 data unsigned char counter;
13816 void simpleInterrupt(void) __interrupt
13817 \begin_inset LatexCommand \index{interrupt}
13822 \begin_inset LatexCommand \index{\_\_interrupt}
13840 void nakedInterrupt(void) __interrupt (2) __naked
13849 \begin_inset LatexCommand \index{\_asm}
13854 \begin_inset LatexCommand \index{\_\_asm}
13871 _counter ; does not change flags, no need to save psw
13883 ; MUST explicitly include ret or reti in _naked function.
13890 \begin_inset LatexCommand \index{\_endasm}
13895 \begin_inset LatexCommand \index{\_\_endasm}
13904 For an 8051 target, the generated simpleInterrupt looks like:
13913 example, recent versions of SDCC generate
13919 code for simpleInterrupt() and nakedInterrupt()!
14059 whereas nakedInterrupt looks like:
14074 _counter ; does not change flags, no need to save psw
14092 ; MUST explicitly include ret or reti in _naked function
14095 The related directive #pragma exclude
14096 \begin_inset LatexCommand \index{\#pragma exclude}
14100 allows a more fine grained control over pushing & popping
14101 \begin_inset LatexCommand \index{push/pop}
14108 While there is nothing preventing you from writing C code inside a
14112 function, there are many ways to shoot yourself in the foot doing this,
14113 and it is recommended that you stick to inline assembler.
14116 Use of Labels within Inline Assembler
14119 SDCC allows the use of in-line assembler with a few restrictions regarding
14121 In older versions of the compiler all labels defined within inline assembler
14130 where nnnn is a number less than 100 (which implies a limit of utmost 100
14131 inline assembler labels
14145 \begin_inset LatexCommand \index{\_asm}
14150 \begin_inset LatexCommand \index{\_\_asm}
14180 \begin_inset LatexCommand \index{\_endasm}
14185 \begin_inset LatexCommand \index{\_\_endasm}
14192 Inline assembler code cannot reference any C-Labels, however it can reference
14194 \begin_inset LatexCommand \index{Labels}
14198 defined by the inline assembler, e.g.:
14223 ; some assembler code
14243 /* some more c code */
14245 clabel:\SpecialChar ~
14247 /* inline assembler cannot reference this label */
14259 $0003: ;label (can be referenced by inline assembler only)
14266 \begin_inset LatexCommand \index{\_endasm}
14271 \begin_inset LatexCommand \index{\_\_endasm}
14281 /* some more c code */
14286 In other words inline assembly code can access labels defined in inline
14287 assembly within the scope of the function.
14288 The same goes the other way, i.e.
14289 labels defines in inline assembly can not be accessed by C statements.
14292 Interfacing with Assembler Code
14293 \begin_inset LatexCommand \index{Assembler routines}
14300 Global Registers used for Parameter Passing
14301 \begin_inset LatexCommand \index{Parameter passing}
14308 The compiler always uses the global registers
14311 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14316 \begin_inset LatexCommand \index{DPTR}
14321 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
14330 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
14336 to pass the first parameter to a routine.
14337 The second parameter onwards is either allocated on the stack (for reentrant
14348 -stack-auto is used) or in data / xdata memory (depending on the memory
14353 Assembler Routine (non-reentrant)
14356 In the following example
14357 \begin_inset LatexCommand \index{reentrant}
14362 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
14366 the function c_func calls an assembler routine asm_func, which takes two
14368 \begin_inset LatexCommand \index{function parameter}
14377 extern int asm_func(unsigned char, unsigned char);
14381 int c_func (unsigned char i, unsigned char j)
14389 return asm_func(i,j);
14403 return c_func(10,9);
14408 The corresponding assembler function is:
14413 .globl _asm_func_PARM_2
14514 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14531 Note here that the return values
14532 \begin_inset LatexCommand \index{return value}
14536 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14538 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14539 b' & 'acc' for four byte values.
14542 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14543 the parameter number starting from 1, and counting from the left.
14544 The first parameter is passed in
14545 \begin_inset Quotes eld
14549 \begin_inset Quotes erd
14552 for a one byte parameter,
14553 \begin_inset Quotes eld
14557 \begin_inset Quotes erd
14561 \begin_inset Quotes eld
14565 \begin_inset Quotes erd
14568 for three bytes and
14569 \begin_inset Quotes eld
14573 \begin_inset Quotes erd
14576 for a four bytes parameter.
14577 The variable name for the second parameter will be _<function_name>_PARM_2.
14581 Assemble the assembler routine with the following command:
14588 asx8051 -losg asmfunc.asm
14595 Then compile and link the assembler routine to the C source file with the
14603 sdcc cfunc.c asmfunc.rel
14606 Assembler Routine (reentrant)
14610 \begin_inset LatexCommand \index{reentrant}
14615 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14619 the second parameter
14620 \begin_inset LatexCommand \index{function parameter}
14624 onwards will be passed on the stack, the parameters are pushed from right
14626 after the call the leftmost parameter will be on the top of the stack.
14627 Here is an example:
14632 extern int asm_func(unsigned char, unsigned char);
14636 int c_func (unsigned char i, unsigned char j) reentrant
14644 return asm_func(i,j);
14658 return c_func(10,9);
14663 The corresponding assembler routine is:
14762 \added_space_bottom bigskip
14763 The compiling and linking procedure remains the same, however note the extra
14764 entry & exit linkage required for the assembler code, _bp is the stack
14765 frame pointer and is used to compute the offset into the stack for parameters
14766 and local variables.
14770 \begin_inset LatexCommand \index{int (16 bit)}
14775 \begin_inset LatexCommand \index{long (32 bit)}
14782 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14783 multiplication and modulus operations are implemented by support routines.
14784 These support routines are all developed in ANSI-C to facilitate porting
14785 to other MCUs, although some model specific assembler optimizations are
14787 The following files contain the described routines, all of them can be
14788 found in <installdir>/share/sdcc/lib.
14794 \begin_inset Tabular
14795 <lyxtabular version="3" rows="11" columns="2">
14797 <column alignment="left" valignment="top" leftline="true" width="0">
14798 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14799 <row topline="true" bottomline="true">
14800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14810 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14821 <row topline="true">
14822 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14835 16 bit multiplication
14839 <row topline="true">
14840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14848 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14853 signed 16 bit division (calls _divuint)
14857 <row topline="true">
14858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14871 unsigned 16 bit division
14875 <row topline="true">
14876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14889 signed 16 bit modulus (calls _moduint)
14893 <row topline="true">
14894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14902 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14907 unsigned 16 bit modulus
14911 <row topline="true">
14912 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14920 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14925 32 bit multiplication
14929 <row topline="true">
14930 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14943 signed 32 division (calls _divulong)
14947 <row topline="true">
14948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14956 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14961 unsigned 32 division
14965 <row topline="true">
14966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14974 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14979 signed 32 bit modulus (calls _modulong)
14983 <row topline="true" bottomline="true">
14984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14997 unsigned 32 bit modulus
15010 Since they are compiled as
15015 \begin_inset LatexCommand \index{reentrant}
15020 \begin_inset LatexCommand \index{interrupt}
15024 service routines should not do any of the above operations.
15025 If this is unavoidable then the above routines will need to be compiled
15039 \begin_inset LatexCommand \index{-\/-stack-auto}
15045 option, after which the source program will have to be compiled with
15058 \begin_inset LatexCommand \index{-\/-int-long-reent}
15065 Notice that you don't have to call these routines directly.
15066 The compiler will use them automatically every time an integer operation
15070 Floating Point Support
15071 \begin_inset LatexCommand \index{Floating point support}
15078 SDCC supports IEEE (single precision 4 bytes) floating point numbers.
15079 The floating point support routines are derived from gcc's floatlib.c and
15080 consist of the following routines:
15088 \begin_inset Tabular
15089 <lyxtabular version="3" rows="17" columns="2">
15091 <column alignment="left" valignment="top" leftline="true" width="0">
15092 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
15093 <row topline="true" bottomline="true">
15094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15111 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15120 <row topline="true">
15121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15138 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15152 add floating point numbers
15156 <row topline="true">
15157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15188 subtract floating point numbers
15192 <row topline="true">
15193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15224 divide floating point numbers
15228 <row topline="true">
15229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15260 multiply floating point numbers
15264 <row topline="true">
15265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15296 convert floating point to unsigned char
15300 <row topline="true">
15301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15332 convert floating point to signed char
15336 <row topline="true">
15337 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15368 convert floating point to unsigned int
15372 <row topline="true">
15373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15390 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15404 convert floating point to signed int
15408 <row topline="true">
15409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15435 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15449 convert floating point to unsigned long
15453 <row topline="true">
15454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15485 convert floating point to signed long
15489 <row topline="true">
15490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15507 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15521 convert unsigned char to floating point
15525 <row topline="true">
15526 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15543 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15557 convert char to floating point number
15561 <row topline="true">
15562 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15593 convert unsigned int to floating point
15597 <row topline="true">
15598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15629 convert int to floating point numbers
15633 <row topline="true">
15634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15665 convert unsigned long to floating point number
15669 <row topline="true" bottomline="true">
15670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15687 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15701 convert long to floating point number
15713 \added_space_bottom bigskip
15714 These support routines are developed in ANSI-C so there is room for space
15715 and speed improvement
15721 These floating point routines (
15725 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15730 Note if all these routines are used simultaneously the data space might
15732 For serious floating point usage the large model might be needed.
15733 Also notice that you don't have to call this routines directly.
15734 The compiler will use them automatically every time a floating point operation
15739 \begin_inset LatexCommand \index{Libraries}
15748 <pending: this is messy and incomplete - a little more information is in
15749 sdcc/doc/libdoc.txt
15754 Compiler support routines (_gptrget, _mulint etc.)
15757 Stdclib functions (puts, printf, strcat etc.)
15758 \layout Subsubsection
15764 \begin_inset LatexCommand \index{<stdio.h>}
15768 As usual on embedded systems you have to provide your own
15771 \begin_inset LatexCommand \index{getchar()}
15780 \begin_inset LatexCommand \index{putchar()}
15787 SDCC does not know whether the system connects to a serial line with or
15788 without handshake, LCD, keyboard or other device.
15802 You'll find examples for serial routines f.e.
15803 in sdcc/device/lib.
15809 \begin_inset LatexCommand \index{printf()}
15819 does not support float (except on ds390).
15820 To enable this recompile it with the option
15833 \begin_inset LatexCommand \index{USE\_FLOATS}
15839 on the command line.
15853 \begin_inset LatexCommand \index{-\/-model-large}
15859 for the mcs51 port, since this uses a lot of memory.
15862 If you're short on memory you might want to use
15865 \begin_inset LatexCommand \index{printf\_small()}
15880 For the mcs51 there additionally are assembly versions
15883 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15889 (subset of printf using less than 270 bytes) and
15892 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15901 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15907 (floating-point aware version of printf_fast) which should fit the requirements
15908 of many embedded systems (printf_fast() can be customized by unsetting
15913 support long variables and field widths).
15914 \layout Subsubsection
15917 \begin_inset LatexCommand \index{malloc.h}
15924 Before using dynamic
15925 \begin_inset LatexCommand \index{dynamic}
15929 memory allocation with SDCC, you have to provide heap
15930 \begin_inset LatexCommand \index{heap}
15934 space for malloc to allocate memory from
15939 You can acomplish this by including the following code into your source:
15944 #include <malloc.h>
15945 \begin_inset LatexCommand \index{malloc.h}
15950 \begin_inset LatexCommand \index{calloc}
15955 \begin_inset LatexCommand \index{malloc}
15960 \begin_inset LatexCommand \index{realloc}
15965 \begin_inset LatexCommand \index{free}
15973 #define HEAPSIZE 0x1000 /* Adjust depending on available memory */
15975 unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory
15987 /* Your variable declarations come here*/
15997 init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE);
16003 /* Rest of your code*/
16010 Math functions (sinf, powf, sqrtf etc.)
16011 \layout Subsubsection
16016 See definitions in file <math.h>.
16023 \begin_inset LatexCommand \index{Libraries}
16027 included in SDCC should have a license at least as liberal as the GNU Lesser
16028 General Public License
16029 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
16040 license statements for the libraries are missing.
16041 sdcc/device/lib/ser_ir.c
16045 come with a GPL (as opposed to LGPL) License - this will not be liberal
16046 enough for many embedded programmers.
16049 If you have ported some library or want to share experience about some code
16051 falls into any of these categories Busses (I
16052 \begin_inset Formula $^{\textrm{2}}$
16055 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
16056 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
16057 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
16058 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
16063 would certainly like to hear about it.
16065 \added_space_bottom bigskip
16066 Programmers coding for embedded systems are not especially famous for being
16067 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
16068 e these references are very valuable.
16069 Let's help to create a climate where information is shared.
16075 MCS51 Memory Models
16076 \begin_inset LatexCommand \index{Memory model}
16081 \begin_inset LatexCommand \index{MCS51 memory model}
16086 \layout Subsubsection
16088 Small, Medium and Large
16091 SDCC allows three memory models for MCS51 code,
16100 Modules compiled with different memory models should
16104 be combined together or the results would be unpredictable.
16105 The library routines supplied with the compiler are compiled as small,
16107 The compiled library modules are contained in separate directories as small,
16108 medium and large so that you can link to the appropriate set.
16111 When the medium or large model is used all variables declared without a
16112 storage class will be allocated into the external ram, this includes all
16113 parameters and local variables (for non-reentrant
16114 \begin_inset LatexCommand \index{reentrant}
16119 When the small model is used variables without storage class are allocated
16120 in the internal ram.
16123 Judicious usage of the processor specific storage classes
16124 \begin_inset LatexCommand \index{Storage class}
16128 and the 'reentrant' function type will yield much more efficient code,
16129 than using the large model.
16130 Several optimizations are disabled when the program is compiled using the
16131 large model, it is therefore recommended that the small model be used unless
16132 absolutely required.
16133 \layout Subsubsection
16136 \begin_inset LatexCommand \label{sub:External-Stack}
16141 \begin_inset LatexCommand \index{stack}
16146 \begin_inset LatexCommand \index{External stack (mcs51)}
16153 The external stack (-
16164 \begin_inset LatexCommand \index{-\/-xstack}
16168 ) is located in pdata
16169 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16173 memory (usually at the start of the external ram segment) and uses all
16174 unused space in pdata (max.
16186 -xstack option is used to compile the program, the parameters and local
16188 \begin_inset LatexCommand \index{local variables}
16192 of all reentrant functions are allocated in this area.
16193 This option is provided for programs with large stack space requirements.
16194 When used with the -
16205 \begin_inset LatexCommand \index{-\/-stack-auto}
16209 option, all parameters and local variables are allocated on the external
16210 stack (note: support libraries will need to be recompiled with the same
16212 There is a predefined target in the library makefile).
16215 The compiler outputs the higher order address byte of the external ram segment
16217 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16222 \begin_inset LatexCommand \ref{sub:MCS51-variants}
16226 ), therefore when using the External Stack option, this port
16230 be used by the application program.
16234 \begin_inset LatexCommand \index{Memory model}
16239 \begin_inset LatexCommand \index{DS390 memory model}
16246 The only model supported is Flat 24
16247 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
16252 This generates code for the 24 bit contiguous addressing mode of the Dallas
16254 In this mode, up to four meg of external RAM or code space can be directly
16256 See the data sheets at www.dalsemi.com for further information on this part.
16260 Note that the compiler does not generate any code to place the processor
16261 into 24 bitmode (although
16265 in the ds390 libraries will do that for you).
16271 \begin_inset LatexCommand \index{Tinibios (DS390)}
16275 , the boot loader or similar code must ensure that the processor is in 24
16276 bit contiguous addressing mode before calling the SDCC startup code.
16294 option, variables will by default be placed into the XDATA segment.
16299 Segments may be placed anywhere in the 4 meg address space using the usual
16311 Note that if any segments are located above 64K, the -r flag must be passed
16312 to the linker to generate the proper segment relocations, and the Intel
16313 HEX output format must be used.
16314 The -r flag can be passed to the linker by using the option
16318 on the SDCC command line.
16319 However, currently the linker can not handle code segments > 64k.
16323 \begin_inset LatexCommand \label{sec:Pragmas}
16328 \begin_inset LatexCommand \index{Pragmas}
16335 SDCC supports the following #pragma directives:
16343 \begin_inset LatexCommand \index{\#pragma save}
16347 - this will save most current options to the save/restore stack.
16348 See #pragma\SpecialChar ~
16357 \begin_inset LatexCommand \index{\#pragma restore}
16361 - will restore saved options from the last save.
16362 saves & restores can be nested.
16363 SDCC uses a save/restore stack: save pushes current options to the stack,
16364 restore pulls current options from the stack.
16365 See #pragma\SpecialChar ~
16376 \begin_inset LatexCommand \index{\#pragma callee\_saves}
16381 \begin_inset LatexCommand \index{function prologue}
16385 function1[,function2[,function3...]] - The compiler by default uses a caller
16386 saves convention for register saving across function calls, however this
16387 can cause unnecessary register pushing & popping
16388 \begin_inset LatexCommand \index{push/pop}
16392 when calling small functions from larger functions.
16393 This option can be used to switch off the register saving convention for
16394 the function names specified.
16395 The compiler will not save registers when calling these functions, extra
16396 code need to be manually inserted at the entry & exit for these functions
16397 to save & restore the registers used by these functions, this can SUBSTANTIALLY
16398 reduce code & improve run time performance of the generated code.
16399 In the future the compiler (with inter procedural analysis) may be able
16400 to determine the appropriate scheme to use for each function call.
16411 -callee-saves command line option is used, the function names specified
16412 in #pragma\SpecialChar ~
16414 \begin_inset LatexCommand \index{\#pragma callee\_saves}
16418 is appended to the list of functions specified in the command line.
16426 \begin_inset LatexCommand \index{\#pragma exclude}
16430 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
16431 of pairs of push/pop
16432 \begin_inset LatexCommand \index{push/pop}
16441 \begin_inset LatexCommand \index{interrupt}
16454 The directive should be placed immediately before the ISR function definition
16455 and it affects ALL ISR functions following it.
16456 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
16457 exclude\SpecialChar ~
16459 \begin_inset LatexCommand \index{\#pragma exclude}
16464 See also the related keyword _naked
16465 \begin_inset LatexCommand \index{\_naked}
16470 \begin_inset LatexCommand \index{\_\_naked}
16482 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
16486 - the compiler will not warn you anymore for obvious mistakes, you'r on
16495 \begin_inset LatexCommand \index{\#pragma disable\_warning}
16499 - the compiler will not warn you anymore about warning number <nnnn>.
16507 \begin_inset LatexCommand \index{\#pragma nogcse}
16511 - will stop global common subexpression elimination.
16519 \begin_inset LatexCommand \index{\#pragma noinduction}
16523 - will stop loop induction optimizations.
16531 \begin_inset LatexCommand \index{\#pragma noinvariant}
16535 - will not do loop invariant optimizations.
16536 For more details see Loop Invariants in section
16537 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
16549 \begin_inset LatexCommand \index{\#pragma noiv}
16553 - Do not generate interrupt
16554 \begin_inset LatexCommand \index{interrupt}
16558 vector table entries for all ISR functions defined after the pragma.
16559 This is useful in cases where the interrupt vector table must be defined
16560 manually, or when there is a secondary, manually defined interrupt vector
16562 for the autovector feature of the Cypress EZ-USB FX2).
16563 More elegantly this can be achieved by obmitting the optional interrupt
16564 number after the interrupt keyword, see section
16565 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
16578 \begin_inset LatexCommand \index{\#pragma nojtbound}
16582 - will not generate code for boundary value checking, when switch statements
16583 are turned into jump-tables (dangerous).
16584 For more details see section
16585 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16597 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16601 - Will not do loop reversal optimization
16609 \begin_inset LatexCommand \index{\#pragma nooverlay}
16613 - the compiler will not overlay the parameters and local variables of a
16622 \begin_inset LatexCommand \index{\#pragma stackauto}
16637 \begin_inset LatexCommand \index{-\/-stack-auto}
16642 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16646 Parameters and Local Variables.
16654 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16658 - The compiler will optimize code generation towards fast code, possibly
16659 at the expense of code size.
16660 Currently this has little effect.
16668 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16672 - The compiler will optimize code generation towards compact code, possibly
16673 at the expense of code speed.
16674 Currently this has little effect.
16682 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16686 - The compiler will attempt to generate code that is both compact and fast,
16687 as long as meeting one goal is not a detriment to the other (this is the
16697 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16701 - Generally follow the C89 standard, but allow SDCC features that conflict
16702 with the standard (default).
16710 \begin_inset LatexCommand \index{\#pragma std\_c89}
16714 - Follow the C89 standard and disable SDCC features that conflict with the
16723 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16727 - Generally follow the C99 standard, but allow SDCC features that conflict
16728 with the standard (incomplete support).
16736 \begin_inset LatexCommand \index{\#pragma std\_c99}
16740 - Follow the C99 standard and disable SDCC features that conflict with the
16741 standard (incomplete support).
16749 \begin_inset LatexCommand \index{\#pragma codeseg}
16753 - Use this name (max.
16754 8 characters) for the code segment.
16773 \begin_inset LatexCommand \index{\#pragma constseg}
16777 - Use this name (max.
16778 8 characters) for the const segment.
16792 The preprocessor SDCPP
16793 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
16797 supports the following #pragma directives:
16805 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16809 (+ | -) - switch _asm _endasm block preprocessing on / off.
16811 You use this prama to define multilines of assembly code.
16812 This will prevent the preprocessor from changing the formating required
16814 Below is an example on how to use this pragma.
16819 #pragma preproc_asm -
16820 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16826 #define MYDELAY _asm
16831 nop ;my assembly comment...
16845 #pragma preproc_asm +
16875 The pragma's are intended to be used to turn-on or off certain optimizations
16876 which might cause the compiler to generate extra stack / data space to
16877 store compiler generated temporary variables.
16878 This usually happens in large functions.
16879 Pragma directives should be used as shown in the following example, they
16880 are used to control options & optimizations for a given function; pragmas
16881 should be placed before and/or after a function, placing pragma's inside
16882 a function body could have unpredictable results.
16888 \begin_inset LatexCommand \index{\#pragma save}
16899 /* save the current settings */
16902 \begin_inset LatexCommand \index{\#pragma nogcse}
16911 /* turnoff global subexpression elimination */
16913 #pragma noinduction
16914 \begin_inset LatexCommand \index{\#pragma noinduction}
16918 /* turn off induction optimizations */
16941 \begin_inset LatexCommand \index{\#pragma restore}
16945 /* turn the optimizations back on */
16948 The compiler will generate a warning message when extra space is allocated.
16949 It is strongly recommended that the save and restore pragma's be used when
16950 changing options for a function.
16959 Defines Created by the Compiler
16962 The compiler creates the following #defines
16963 \begin_inset LatexCommand \index{\#defines}
16968 \begin_inset LatexCommand \index{Defines created by the compiler}
16978 \begin_inset Tabular
16979 <lyxtabular version="3" rows="11" columns="2">
16981 <column alignment="left" valignment="top" leftline="true" width="3in">
16982 <column alignment="left" valignment="top" leftline="true" rightline="true" width="3in">
16983 <row topline="true" bottomline="true">
16984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17005 <row topline="true">
17006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17012 \begin_inset LatexCommand \index{SDCC}
17019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17025 Since version 2.5.6 the version number as an int (ex.
17030 <row topline="true">
17031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17037 \begin_inset LatexCommand \index{SDCC\_mcs51}
17042 \begin_inset LatexCommand \index{SDCC\_ds390}
17047 \begin_inset LatexCommand \index{SDCC\_z80}
17054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17059 depending on the model used (e.g.: -mds390)
17063 <row topline="true">
17064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17070 \begin_inset LatexCommand \index{\_\_mcs51}
17075 \begin_inset LatexCommand \index{\_\_ds390}
17080 \begin_inset LatexCommand \index{\_\_hc08}
17085 \begin_inset LatexCommand \index{\_\_z80}
17092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17097 depending on the model used (e.g.
17102 <row topline="true">
17103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17109 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
17116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17139 <row topline="true">
17140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17146 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
17153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17176 <row topline="true">
17177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17183 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
17190 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17213 <row topline="true">
17214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17220 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
17227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17250 <row topline="true">
17251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17257 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
17264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17287 <row topline="true">
17288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17294 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
17301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17314 <row topline="true" bottomline="true">
17315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17321 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
17328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17348 Notes on supported Processors
17352 \begin_inset LatexCommand \label{sub:MCS51-variants}
17357 \begin_inset LatexCommand \index{MCS51 variants}
17364 MCS51 processors are available from many vendors and come in many different
17366 While they might differ considerably in respect to Special Function Registers
17367 the core MCS51 is usually not modified or is kept compatible.
17371 pdata access by SFR
17374 With the upcome of devices with internal xdata and flash memory devices
17376 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
17380 as dedicated I/O port is becoming more popular.
17381 Switching the high byte for pdata
17382 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
17386 access which was formerly done by port P2 is then achieved by a Special
17388 \begin_inset LatexCommand \index{sfr}
17393 In well-established MCS51 tradition the address of this
17397 is where the chip designers decided to put it.
17398 Needless to say that they didn't agree on a common name either.
17399 So that the startup code can correctly initialize xdata variables, you
17400 should define an sfr with the name _XPAGE
17403 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
17409 at the appropriate location if the default, port P2, is not used for this.
17415 __sfr __at (0x92) _XPAGE; /* Cypress EZ-USB family */
17420 __sfr __at (0xaf) _XPAGE; /* some Silicon Labs (Cygnal) chips */
17425 __sfr __at (0xaa) _XPAGE; /* some Silicon Labs (Cygnal) chips */
17428 For more exotic implementations further customizations may be needed.
17430 \begin_inset LatexCommand \ref{sub:Startup-Code}
17434 for other possibilities.
17437 Other Features available by SFR
17439 \added_space_bottom bigskip
17440 Some MCS51 variants offer features like Double DPTR
17441 \begin_inset LatexCommand \index{DPTR}
17445 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
17446 These are currently not used for the MCS51 port.
17447 If you absolutely need them you can fall back to inline assembly or submit
17453 \added_space_bottom bigskip
17455 \begin_inset LatexCommand \index{DS80C400}
17460 \begin_inset LatexCommand \index{DS400}
17464 microcontroller has a rich set of peripherals.
17465 In its built-in ROM library it includes functions to access some of the
17466 features, among them is a TCP stack with IP4 and IP6 support.
17467 Library headers (currently in beta status) and other files are provided
17471 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
17479 The Z80 and gbz80 port
17482 SDCC can target both the Zilog Z80
17483 \begin_inset LatexCommand \index{Z80}
17487 and the Nintendo Gameboy's Z80-like gbz80
17488 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
17493 The Z80 port is passed through the same
17496 \begin_inset LatexCommand \index{Regression test}
17502 as the MCS51 and DS390 ports, so floating point support, support for long
17503 variables and bitfield support is fine.
17504 See mailing lists and forums about interrupt routines.
17506 \added_space_bottom bigskip
17507 As always, the code is the authoritative reference - see z80/ralloc.c and
17510 \begin_inset LatexCommand \index{Z80!stack}
17514 frame is similar to that generated by the IAR Z80 compiler.
17515 IX is used as the base pointer, HL and IY are used as a temporary registers,
17516 and BC and DE are available for holding variables.
17518 \begin_inset LatexCommand \index{Z80!return value}
17522 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
17524 The gbz80 port use the same set of registers for the return values, but
17525 in a different order of significance: E (one byte), DE (two bytes), or
17531 \added_space_bottom bigskip
17532 The port to the Motorola HC08
17533 \begin_inset LatexCommand \index{HC08}
17537 family has been added in October 2003, and is still undergoing some basic
17539 The code generator is complete, but the register allocation is still quite
17541 Some of the SDCC's standard C library functions have embedded non-HC08
17542 inline assembly and so are not yet usable.
17549 \begin_inset LatexCommand \index{PIC14}
17553 port still requires a major effort from the development community.
17554 However it can work for very simple code.
17557 C code and 14bit PIC code page
17558 \begin_inset LatexCommand \index{code page (pic14)}
17563 \begin_inset LatexCommand \index{RAM bank (pic14)}
17570 The linker organizes allocation for the code page and RAM banks.
17571 It does not have intimate knowledge of the code flow.
17572 It will put all the code section of a single asm file into a single code
17574 In order to make use of multiple code pages, separate asm files must be
17576 The compiler treats all functions of a single C file as being in the same
17577 code page unless it is non static.
17578 The compiler treats all local variables of a single C file as being in
17579 the same RAM bank unless it is an extern.
17583 To get the best follow these guide lines:
17586 make local functions static, as non static functions require code page selection
17590 Make local variables static as extern variables require RAM bank selection
17594 For devices that have multiple code pages it is more efficient to use the
17595 same number of files as pages, i.e.
17596 for the 16F877 use 4 separate files and i.e.
17597 for the 16F874 use 2 separate files.
17598 This way the linker can put the code for each file into different code
17599 pages and the compiler can allocate reusable variables more efficiently
17600 and there's less page selection overhead.
17601 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
17602 instruction set) use 'unsigned char' whereever possible instead of 'int'.
17605 Creating a device include file
17608 For generating a device include file
17609 \begin_inset LatexCommand \index{PIC14!Header files}
17613 use the support perl script inc2h.pl kept in directory support/script.
17619 For the interrupt function, use the keyword 'interrupt'
17620 \begin_inset LatexCommand \index{PIC14!interrupt}
17624 with level number of 0 (PIC14 only has 1 interrupt so this number is only
17625 there to avoid a syntax error - it ought to be fixed).
17631 void Intr(void) interrupt 0
17637 T0IF = 0; /* Clear timer interrupt */
17642 Linking and assembling
17645 For assembling you can use either GPUTILS'
17646 \begin_inset LatexCommand \index{gputils (pic tools)}
17650 gpasm.exe or MPLAB's mpasmwin.exe.
17651 GPUTILS is available from
17652 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17657 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17658 If you use MPLAB and an interrupt function then the linker script file
17659 vectors section will need to be enlarged to link with mplink.
17682 sdcc -S -V -mpic14 -p16F877 $<
17696 $(PRJ).hex: $(OBJS)
17706 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17728 sdcc -S -V -mpic14 -p16F877 $<
17738 mpasmwin /q /o $*.asm
17742 $(PRJ).hex: $(OBJS)
17752 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17755 Please note that indentations within a
17759 have to be done with a tabulator character.
17762 Command-line options
17765 Besides the switches common to all SDCC backends, the PIC14 port accepts
17766 the following options (for an updated list see sdcc -
17778 \labelwidthstring 00.00.0000
17791 \begin_inset LatexCommand \index{PIC14!Options!-\/-debug-extra}
17795 emit debug info in assembly output
17797 \labelwidthstring 00.00.0000
17810 \begin_inset LatexCommand \index{PIC14!Options!-\/-no-pcode-opt}
17814 disable (slightly faulty) optimization on pCode
17818 \layout Subsubsection
17820 error: missing definition for symbol
17821 \begin_inset Quotes sld
17825 \begin_inset Quotes srd
17831 The PIC14 port uses library routines to provide more complex operations
17832 like multiplication, division/modulus and (generic) pointer dereferencing.
17833 In order to add these routines to your project, you must link with PIC14's
17839 For single source file projects this is done automatically, more complex
17844 to the linker's arguments.
17845 Make sure you also add an include path for the library (using the -I switch
17847 \layout Subsubsection
17849 Processor mismatch in file
17850 \begin_inset Quotes sld
17854 \begin_inset Quotes srd
17860 This warning can usually be ignored due to the very good compatibility amongst
17862 \begin_inset LatexCommand \index{PIC14}
17869 You might also consider recompiling the library for your specific device
17870 by changing the ARCH=p16f877 (default target) entry in
17872 device/lib/pic/Makefile.in
17876 device/lib/pic/Makefile
17878 to reflect your device.
17879 This might even improve performance for smaller devices as unneccesary
17880 BANKSELs migth be removed.
17884 \layout Subsubsection
17889 Currently, data can only be initialized if it resides in the source file
17895 Data in other source files will silently
17903 \begin_inset Marginal
17918 \begin_inset LatexCommand \index{PIC16}
17926 \begin_inset LatexCommand \index{PIC16}
17930 port is the portion of SDCC that is responsible to produce code for the
17932 \begin_inset LatexCommand \index{Microchip}
17936 (TM) microcontrollers with 16 bit core.
17937 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17938 Currently supported devices are:
17942 \begin_inset Tabular
17943 <lyxtabular version="3" rows="4" columns="6">
17945 <column alignment="center" valignment="top" leftline="true" width="0">
17946 <column alignment="center" valignment="top" leftline="true" width="0">
17947 <column alignment="center" valignment="top" leftline="true" width="0">
17948 <column alignment="center" valignment="top" leftline="true" width="0">
17949 <column alignment="center" valignment="top" leftline="true" width="0">
17950 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17951 <row topline="true">
17952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18001 <row topline="true">
18002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18051 <row topline="true">
18052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18068 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18101 <row topline="true" bottomline="true">
18102 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18118 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18158 PIC16 port supports the standard command line arguments as supposed, with
18159 the exception of certain cases that will be mentioned in the following
18162 \labelwidthstring 00.00.0000
18175 \begin_inset LatexCommand \index{PIC16!Options!-\/-callee-saves}
18191 \labelwidthstring 00.00.0000
18204 \begin_inset LatexCommand \index{PIC16!Options!-\/-all-callee-saves}
18208 All function arguments are passed on stack by default.
18211 There is no need to specify this in the command line.
18213 \labelwidthstring 00.00.0000
18225 -fommit-frame-pointer
18226 \begin_inset LatexCommand \index{PIC16!Options!-\/-fommit-frame-pointer}
18230 Frame pointer will be omitted when the function uses no local variables.
18233 Port Specific Options
18234 \begin_inset LatexCommand \index{Options PIC16}
18241 The port specific options appear after the global options in the sdcc --help
18243 \layout Subsubsection
18248 General options enable certain port features and optimizations.
18250 \labelwidthstring 00.00.0000
18262 -stack-model=[model] Used in conjuction with the command above.
18263 Defines the stack model to be used, valid stack models are :
18266 \labelwidthstring 00.00.0000
18272 Selects small stack model.
18273 8 bit stack and frame pointers.
18274 Supports 256 bytes stack size.
18276 \labelwidthstring 00.00.0000
18282 Selects large stack model.
18283 16 bit stack and frame pointers.
18284 Supports 65536 bytes stack size.
18287 \labelwidthstring 00.00.0000
18299 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
18300 unitialized data variables with [kword].
18301 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
18303 \labelwidthstring 00.00.0000
18315 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
18316 Useful for bootloaders.
18318 \labelwidthstring 00.00.0000
18330 -asm= sets the full path and name of an external assembler to call.
18332 \labelwidthstring 00.00.0000
18344 -link= sets the full path and name of an external linker to call.
18346 \labelwidthstring 00.00.0000
18359 \begin_inset LatexCommand \index{PIC16!MPLAB}
18363 compatibility option.
18364 Currently only suppresses special gpasm directives.
18365 \layout Subsubsection
18367 Optimization Options
18369 \labelwidthstring 00.00.0000
18381 -optimize-goto Try to use (conditional) BRA instead of GOTO
18383 \labelwidthstring 00.00.0000
18395 -optimize-cmp Try to optimize some compares.
18397 \labelwidthstring 00.00.0000
18409 -optimize-df Analyze the dataflow of the generated code and improve it.
18411 \labelwidthstring 00.00.0000
18423 -obanksel=nn Set optimization level for inserting BANKSELs.
18428 \labelwidthstring 00.00.0000
18432 \labelwidthstring 00.00.0000
18434 1 checks previous used register and if it is the same then does not emit
18435 BANKSEL, accounts only for labels.
18437 \labelwidthstring 00.00.0000
18439 2 tries to check the location of (even different) symbols and removes BANKSELs
18440 if they are in the same bank.
18445 Important: There might be problems if the linker script has data sections
18446 across bank borders!
18448 \layout Subsubsection
18452 \labelwidthstring 00.00.0000
18464 -nodefaultlibs do not link default libraries when linking
18466 \labelwidthstring 00.00.0000
18478 -no-crt Don't link the default run-time modules
18480 \labelwidthstring 00.00.0000
18492 -use-crt= Use a custom run-time module instead of the defaults.
18493 \layout Subsubsection
18498 Debugging options enable extra debugging information in the output files.
18500 \labelwidthstring 00.00.0000
18512 -debug-xtra Similar to -
18523 \begin_inset LatexCommand \index{-\/-debug}
18527 , but dumps more information.
18529 \labelwidthstring 00.00.0000
18541 -debug-ralloc Force register allocator to dump <source>.d file with debugging
18543 <source> is the name of the file compiled.
18545 \labelwidthstring 00.00.0000
18557 -pcode-verbose Enable pcode debugging information in translation.
18559 \labelwidthstring 00.00.0000
18571 -denable-peeps Force the usage of peepholes.
18574 \labelwidthstring 00.00.0000
18586 -gstack Trace push/pops for stack pointer overflow
18588 \labelwidthstring 00.00.0000
18600 -call-tree dump call tree in .calltree file
18603 Enviromental Variables
18606 There is a number of enviromental variables that can be used when running
18607 SDCC to enable certain optimizations or force a specific program behaviour.
18608 these variables are primarily for debugging purposes so they can be enabled/dis
18612 Currently there is only two such variables available:
18614 \labelwidthstring 00.00.0000
18616 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
18617 bitfields is optimized by directly loading FSR0 with the address of the
18618 bitfield structure.
18619 Normally SDCC will cast the bitfield structure to a bitfield pointer and
18621 This step saves data ram and code space for functions that perform heavy
18624 80 bytes of code space are saved when compiling malloc.c with this option).
18627 \labelwidthstring 00.00.0000
18629 NO_REG_OPT do not perform pCode registers optimization.
18630 This should be used for debugging purposes.
18631 In some where bugs in the pcode optimizer are found, users can benefit
18632 from temporarily disabling the optimizer until the bug is fixed.
18635 Preprocessor Macros
18639 \begin_inset LatexCommand \index{PIC16}
18643 port defines the following preprocessor macros while translating a source.
18647 \begin_inset Tabular
18648 <lyxtabular version="3" rows="6" columns="2">
18650 <column alignment="center" valignment="top" leftline="true" width="0">
18651 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18652 <row topline="true" bottomline="true">
18653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18661 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18670 <row topline="true">
18671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18684 Port identification
18688 <row topline="true">
18689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18712 Port identification (same as above)
18716 <row topline="true">
18717 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18730 MCU Identification.
18735 is the microcontrol identification number, i.e.
18740 <row topline="true">
18741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18759 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18764 MCU Identification (same as above)
18768 <row topline="true" bottomline="true">
18769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18777 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18782 nnn = SMALL or LARGE respectively according to the stack model used
18793 In addition the following macros are defined when calling assembler:
18797 \begin_inset Tabular
18798 <lyxtabular version="3" rows="4" columns="2">
18800 <column alignment="center" valignment="top" leftline="true" width="0">
18801 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18802 <row topline="true" bottomline="true">
18803 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18811 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18820 <row topline="true">
18821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18834 MCU Identification.
18839 is the microcontrol identification number, i.e.
18844 <row topline="true">
18845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18858 nnn = SMALL or LARGE respectively according to the memory model used for
18863 <row topline="true" bottomline="true">
18864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18877 nnn = SMALL or LARGE respectively according to the stack model used
18892 \begin_inset LatexCommand \index{PIC16}
18896 port uses the following directories for searching header files and libraries.
18900 \begin_inset Tabular
18901 <lyxtabular version="3" rows="3" columns="4">
18903 <column alignment="center" valignment="top" leftline="true" width="0">
18904 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18905 <column alignment="center" valignment="top" width="0">
18906 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18907 <row topline="true" bottomline="true">
18908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18924 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18941 <row topline="true">
18942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18947 PREFIX/sdcc/include/pic16
18950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18955 PIC16 specific headers
18958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18975 <row topline="true" bottomline="true">
18976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18981 PREFIX/sdcc/lib/pic16
18984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18989 PIC16 specific libraries
18992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19017 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
19025 \begin_inset LatexCommand \index{PIC16}
19029 port currently supports the following pragmas:
19031 \labelwidthstring 00.00.0000
19034 \begin_inset LatexCommand \index{PIC16!Pragmas!\#pragma stack}
19039 \begin_inset LatexCommand \index{PIC16!stack}
19043 forces the code generator to initialize the stack & frame pointers at a
19045 This is an adhoc solution for cases where no STACK directive is available
19046 in the linker script or gplink is not instructed to create a stack section.
19048 The stack pragma should be used only once in a project.
19049 Multiple pragmas may result in indeterminate behaviour of the program.
19055 The old format (ie.
19056 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
19057 cross page boundaries (or even exceed the available data RAM) and crash
19059 Make sure that stack does not cross page boundaries when using the SMALL
19065 The format is as follows:
19068 #pragma stack bottom_address [stack_size]
19075 is the lower bound of the stack section.
19076 The stack pointer initially will point at address (bottom_address+stack_size-1).
19084 /* initializes stack of 100 bytes at RAM address 0x200 */
19087 #pragma stack 0x200 100
19090 If the stack_size field is omitted then a stack is created with the default
19092 This size might be enough for most programs, but its not enough for operations
19093 with deep function nesting or excessive stack usage.
19095 \labelwidthstring 00.00.0000
19099 This pragma is deprecated.
19100 Its use will cause a warning message to be issued.
19106 \labelwidthstring 00.00.0000
19109 \begin_inset LatexCommand \index{PIC16!Pragmas!\#pragma code}
19113 place a function symbol at static FLASH address
19121 /* place function test_func at 0x4000 */
19124 #pragma code test_func 0x4000
19128 \labelwidthstring 00.00.0000
19130 library instructs the linker to use a library module.
19135 #pragma library module_name
19142 can be any library or object file (including its path).
19143 Note that there are four reserved keywords which have special meaning.
19148 \begin_inset Tabular
19149 <lyxtabular version="3" rows="6" columns="3">
19151 <column alignment="center" valignment="top" leftline="true" width="0">
19152 <column alignment="block" valignment="top" leftline="true" width="20page%">
19153 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
19154 <row topline="true" bottomline="true">
19155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19180 <row topline="true">
19181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19196 ignore all library pragmas
19199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19210 <row topline="true">
19211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19229 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19242 <row topline="true">
19243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19258 link the Math libarary
19261 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19274 <row topline="true">
19275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19290 link the I/O library
19293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19306 <row topline="true" bottomline="true">
19307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19322 link the debug library
19325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19344 * is the device number, i.e.
19345 452 for PIC18F452 MCU.
19348 This feature allows for linking with specific libraries withoug having to
19349 explicit name them in the command line.
19354 keyword will reject all modules specified by the library pragma.
19356 \labelwidthstring 00.00.0000
19358 udata pragma udata instructs the compiler to emit code so that linker will
19359 place a variable at a specific memory bank
19367 /* places variable foo at bank2 */
19370 #pragma udata bank2 foo
19376 In order for this pragma to work extra SECTION directives should be added
19377 in the .lkr script.
19378 In the following example a sample .lkr file is shown:
19383 // Sample linker script for the PIC18F452 processor
19389 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
19392 CODEPAGE NAME=page START=0x2A END=0x7FFF
19395 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
19398 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
19401 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
19404 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
19407 ACCESSBANK NAME=accessram START=0x0 END=0x7F
19412 DATABANK NAME=gpr0 START=0x80 END=0xFF
19415 DATABANK NAME=gpr1 START=0x100 END=0x1FF
19418 DATABANK NAME=gpr2 START=0x200 END=0x2FF
19421 DATABANK NAME=gpr3 START=0x300 END=0x3FF
19424 DATABANK NAME=gpr4 START=0x400 END=0x4FF
19427 DATABANK NAME=gpr5 START=0x500 END=0x5FF
19430 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
19435 SECTION NAME=CONFIG ROM=config
19440 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
19443 SECTION NAME=bank1 RAM=gpr1 # should be added to link
19446 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
19449 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
19452 SECTION NAME=bank4 RAM=gpr4
19455 SECTION NAME=bank5 RAM=gpr5
19458 The linker will recognise the section name set in the pragma statement and
19459 will position the variable at the memory bank set with the RAM field at
19460 the SECTION line in the linker script file.
19464 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
19471 There is one main header file
19472 \begin_inset LatexCommand \index{PIC16!Header files}
19476 that can be included to the source files using the pic16
19477 \begin_inset LatexCommand \index{PIC16}
19487 This header file contains the definitions for the processor special registers,
19488 so it is necessary if the source accesses them.
19489 It can be included by adding the following line in the beginning of the
19493 #include <pic18fregs.h>
19496 The specific microcontroller is selected within the pic18fregs.h automatically,
19497 so the same source can be used with a variety of devices.
19504 \begin_inset LatexCommand \index{PIC16!Libraries}
19509 \begin_inset LatexCommand \index{PIC16}
19513 port depends on are the microcontroller device libraries which contain
19514 the symbol definitions for the microcontroller special function registers.
19515 These libraries have the format pic18fxxxx.lib, where
19519 is the microcontroller identification number.
19520 The specific library is selected automatically by the compiler at link
19521 stage according to the selected device.
19524 Libraries are created with gplib which is part of the gputils package
19525 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
19530 \layout Subsubsection*
19532 Building the libraries
19535 Before using SDCC/pic16 there are some libraries that need to be compiled.
19536 This process is not done automatically by SDCC since not all users use
19537 SDCC for pic16 projects.
19538 So each user should compile the libraries separately.
19541 The steps to compile the pic16 libraries under Linux are:
19544 cd device/lib/pic16
19559 su -c 'make install' # install the libraries, you need the root password
19562 If you need to install the headers too, do:
19568 su -c 'make install' # install the headers, you need the root password
19571 There exist a special target to build the I/O libraries.
19572 This target is not automatically build because it will build the I/O library
19578 This way building will take quite a lot of time.
19579 Users are advised to edit the
19581 device/lib/pic16/pics.build
19583 file and then execute:
19592 The following memory models are supported by the PIC16 port:
19601 Memory model affects the default size of pointers within the source.
19602 The sizes are shown in the next table:
19606 \begin_inset Tabular
19607 <lyxtabular version="3" rows="3" columns="3">
19609 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19610 <column alignment="center" valignment="top" leftline="true" width="0">
19611 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19612 <row topline="true" bottomline="true">
19613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19618 Pointer sizes according to memory model
19621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19638 <row topline="true" bottomline="true">
19639 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19647 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19664 <row topline="true" bottomline="true">
19665 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19673 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
19681 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19697 It is advisable that all sources within a project are compiled with the
19699 If one wants to override the default memory model, this can be done by
19700 declaring a pointer as
19709 Far selects large memory model's pointers, while near selects small memory
19713 The standard device libraries (see
19714 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
19718 ) contain no reference to pointers, so they can be used with both memory
19726 \begin_inset LatexCommand \index{PIC16!stack}
19730 implementation for the PIC16 port uses two indirect registers, FSR1 and
19733 \labelwidthstring 00.00.0000
19735 FSR1 is assigned as stack pointer
19737 \labelwidthstring 00.00.0000
19739 FSR2 is assigned as frame pointer
19742 The following stack models are supported by the PIC16 port
19763 model means that only the FSRxL byte is used to access stack and frame,
19770 uses both FSRxL and FSRxH registers.
19771 The following table shows the stack/frame pointers sizes according to stack
19772 model and the maximum space they can address:
19776 \begin_inset Tabular
19777 <lyxtabular version="3" rows="3" columns="3">
19779 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19780 <column alignment="center" valignment="top" leftline="true" width="0">
19781 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19782 <row topline="true" bottomline="true">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19788 Stack & Frame pointer sizes according to stack model
19791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19808 <row topline="true">
19809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19825 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19834 <row topline="true" bottomline="true">
19835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19871 stack model is currently not working properly throughout the code generator.
19872 So its use is not advised.
19873 Also there are some other points that need special care:
19878 Do not create stack sections with size more than one physical bank (that
19882 Stack sections should no cross physical bank limits (i.e.
19883 #pragma stack 0x50 0x100)
19886 These limitations are caused by the fact that only FSRxL is modified when
19887 using SMALL stack model, so no more than 256 bytes of stack can be used.
19888 This problem will disappear after LARGE model is fully implemented.
19894 In addition to the standard SDCC function keywords, PIC16
19895 \begin_inset LatexCommand \index{PIC16}
19899 port makes available two more:
19901 \labelwidthstring 00.00.0000
19904 \begin_inset LatexCommand \index{PIC16!wparam}
19908 Use the WREG to pass one byte of the first function argument.
19909 This improves speed but you may not use this for functions with arguments
19910 that are called via function pointers, otherwise the first byte of the
19911 first parameter will get lost.
19915 void func_wparam(int a) wparam
19921 /* WREG hold the lower part of a */
19924 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19934 This keyword replaces the deprecated wparam pragma.
19936 \labelwidthstring 00.00.0000
19939 \begin_inset LatexCommand \index{PIC16!shadowregs}
19943 When entering/exiting an ISR, it is possible to take advantage of the PIC18F
19944 hardware shadow registers which hold the values of WREG, STATUS and BSR
19946 This can be done by adding the keyword
19954 keyword in the function's header.
19957 void isr_shadow(void) shadowregs interrupt 1
19973 instructs the code generator not to store/restore WREG, STATUS, BSR when
19974 entering/exiting the ISR.
19977 Function return values
19980 Return values from functions are placed to the appropriate registers following
19981 a modified Microchip policy optimized for SDCC.
19982 The following table shows these registers:
19986 \begin_inset Tabular
19987 <lyxtabular version="3" rows="6" columns="2">
19989 <column alignment="center" valignment="top" leftline="true" width="0">
19990 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19991 <row topline="true" bottomline="true">
19992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20005 destination register
20009 <row topline="true">
20010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20027 <row topline="true">
20028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20045 <row topline="true">
20046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20063 <row topline="true">
20064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20077 FSR0L:PRODH:PRODL:WREG
20081 <row topline="true" bottomline="true">
20082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20095 on stack, FSR0 points to the beginning
20110 \begin_inset LatexCommand \index{PIC16!interrupt}
20114 service routine (ISR) is declared using the
20121 void isr(void) interrupt
20139 is the interrupt number, which for PIC18F devices can be:
20143 \begin_inset Tabular
20144 <lyxtabular version="3" rows="4" columns="3">
20146 <column alignment="center" valignment="top" leftline="true" width="0">
20147 <column alignment="center" valignment="top" leftline="true" width="0">
20148 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20149 <row topline="true" bottomline="true">
20150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20173 Interrupt Vector Address
20177 <row topline="true">
20178 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20203 <row topline="true">
20204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20235 HIGH priority interrupts
20238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20247 <row topline="true" bottomline="true">
20248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20261 LOW priority interrupts
20264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20280 When generating assembly code for ISR the code generator places a
20286 Interrupt Vector Address
20288 which points at the genetated ISR.
20289 This single GOTO instruction is part of an automatically generated
20291 interrupt entry point
20294 The actuall ISR code is placed as normally would in the code space.
20295 Upon interrupt request, the GOTO instruction is executed which jumps to
20297 When declaring interrupt functions as _naked this GOTO instruction is
20302 The whole interrupt functions is therefore placed at the Interrupt Vector
20303 Address of the specific interrupt.
20304 This is not a problem for the LOW priority interrupts, but it is a problem
20305 for the RESET and the HIGH priority interrupts because code may be written
20306 at the next interrupt´s vector address and cause undeterminate program
20307 behaviour if that interrupt is raised.
20313 This is not a problem when
20316 this is a HIGH interrupt ISR and LOW interrupts are
20323 when the ISR is small enough not to reach the next interrupt´s vector address.
20333 is possible to be omitted.
20334 This way a function is generated similar to an ISR, but it is not assigned
20338 When entering an interrupt, currently the PIC16
20339 \begin_inset LatexCommand \index{PIC16}
20343 port automatically saves the following registers:
20355 PROD (PRODL and PRODH)
20358 FSR0 (FSR0L and FSR0H)
20361 These registers are restored upon return from the interrupt routine.
20367 NOTE that when the _naked attribute is specified for an interrupt routine,
20368 then NO registers are stored or restored.
20377 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
20378 There are 3 types of generic pointers currently implemented data, code
20379 and eeprom pointers.
20380 They are differentiated by the value of the 7th and 6th bits of the upper
20385 \begin_inset Tabular
20386 <lyxtabular version="3" rows="5" columns="5">
20388 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20389 <column alignment="center" valignment="top" width="0">
20390 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20391 <column alignment="center" valignment="top" width="0">
20392 <column alignment="left" valignment="top" rightline="true" width="0">
20393 <row topline="true" bottomline="true">
20394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20423 rest of the pointer
20426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20435 <row topline="true" bottomline="true">
20436 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20469 uuuuuu uuuuxxxx xxxxxxxx
20472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20477 a 12-bit data pointer in data RAM memory
20481 <row bottomline="true">
20482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20515 uxxxxx xxxxxxxx xxxxxxxx
20518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20523 a 21-bit code pointer in FLASH memory
20527 <row bottomline="true">
20528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20536 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20544 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20552 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20561 uuuuuu uuuuuuxx xxxxxxxx
20564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20569 a 10-bit eeprom pointer in EEPROM memory
20573 <row bottomline="true">
20574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20607 xxxxxx xxxxxxxx xxxxxxxx
20610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20615 unimplemented pointer type
20626 Generic pointer are read and written with a set of library functions which
20627 read/write 1, 2, 3, 4 bytes.
20631 \layout Subsubsection
20633 Standard I/O Streams
20640 the type FILE is defined as:
20643 typedef char * FILE;
20646 This type is the stream type implemented I/O in the PIC18F devices.
20647 Also the standard input and output streams are declared in stdio.h:
20650 extern FILE * stdin;
20653 extern FILE * stdout;
20656 The FILE type is actually a generic pointer which defines one more type
20657 of generic pointers, the
20662 This new type has the format:
20666 \begin_inset Tabular
20667 <lyxtabular version="3" rows="2" columns="7">
20669 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20670 <column alignment="center" valignment="top" width="0">
20671 <column alignment="center" valignment="top" leftline="true" width="0">
20672 <column alignment="center" valignment="top" leftline="true" width="0">
20673 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20674 <column alignment="center" valignment="top" width="0">
20675 <column alignment="left" valignment="top" rightline="true" width="0">
20676 <row topline="true" bottomline="true">
20677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20717 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20722 rest of the pointer
20725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20734 <row topline="true" bottomline="true">
20735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20759 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20787 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20792 upper byte high nubble is 0x2n, the rest are zeroes
20803 Currently implemented there are 3 types of streams defined:
20807 \begin_inset Tabular
20808 <lyxtabular version="3" rows="4" columns="4">
20810 <column alignment="center" valignment="top" leftline="true" width="0">
20811 <column alignment="center" valignment="top" leftline="true" width="0">
20812 <column alignment="center" valignment="top" leftline="true" width="0">
20813 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20814 <row topline="true" bottomline="true">
20815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20848 <row topline="true">
20849 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20880 Writes/Reads characters via the USART peripheral
20884 <row topline="true">
20885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20916 Writes/Reads characters via the MSSP peripheral
20920 <row topline="true" bottomline="true">
20921 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20929 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20939 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20947 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20952 Writes/Reads characters via used defined functions
20963 The stream identifiers are declared as macros in the stdio.h header.
20966 In the libc library there exist the functions that are used to write to
20967 each of the above streams.
20970 \labelwidthstring 00.00.0000
20982 _stream_usart_putchar writes a character at the USART stream
20984 \labelwidthstring 00.00.0000
20996 _stream_mssp_putchar writes a character at the MSSP stream
20998 \labelwidthstring 00.00.0000
21000 putchar dummy function.
21001 This writes a character to a user specified manner.
21004 In order to increase performance
21008 is declared in stdio.h as having its parameter in WREG (it has the wparam
21010 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
21011 in a user-friendly way.
21016 is the name of the variable that holds the character to print.
21017 An example follows:
21020 #include <pic18fregs.h>
21032 PORTA = c; /* dump character c to PORTA */
21045 stdout = STREAM_USER; /* this is not necessary, since stdout points
21048 * by default to STREAM_USER */
21051 printf (¨This is a printf test
21059 \layout Subsubsection
21064 PIC16 contains an implementation of the printf-family of functions.
21065 There exist the following functions:
21068 extern unsigned int sprintf(char *buf, char *fmt, ...);
21071 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
21076 extern unsigned int printf(char *fmt, ...);
21079 extern unsigned int vprintf(char *fmt, va_lista ap);
21084 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
21087 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
21090 For sprintf and vsprintf
21094 should normally be a data pointer where the resulting string will be placed.
21095 No range checking is done so the user should allocate the necessery buffer.
21096 For fprintf and vfprintf
21100 should be a stream pointer (i.e.
21101 stdout, STREAM_MSSP, etc...).
21102 \layout Subsubsection
21107 The PIC18F family of microcontrollers supports a number of interrupt sources.
21108 A list of these interrupts is shown in the following table:
21112 \begin_inset Tabular
21113 <lyxtabular version="3" rows="11" columns="4">
21115 <column alignment="left" valignment="top" leftline="true" width="0">
21116 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
21117 <column alignment="left" valignment="top" leftline="true" width="0">
21118 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
21119 <row topline="true" bottomline="true">
21120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21136 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21153 <row topline="true">
21154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21162 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21167 PORTB change interrupt
21170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21178 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21183 EEPROM/FLASH write complete interrupt
21187 <row topline="true">
21188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21201 INT0 external interrupt
21204 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21212 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21217 Bus collision interrupt
21221 <row topline="true">
21222 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21230 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21235 INT1 external interrupt
21238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21251 Low voltage detect interrupt
21255 <row topline="true">
21256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21269 INT2 external interrupt
21272 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21285 Parallel slave port interrupt
21289 <row topline="true">
21290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21298 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21303 CCP1 module interrupt
21306 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21314 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21319 AD convertion complete interrupt
21323 <row topline="true">
21324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21337 CCP2 module interrupt
21340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21353 USART receive interrupt
21357 <row topline="true">
21358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21366 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21371 TMR0 overflow interrupt
21374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21387 USART transmit interrupt
21391 <row topline="true">
21392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21405 TMR1 overflow interrupt
21408 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21421 SSP receive/transmit interrupt
21425 <row topline="true">
21426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21439 TMR2 matches PR2 interrupt
21442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21457 <row topline="true" bottomline="true">
21458 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21466 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21471 TMR3 overflow interrupt
21474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21496 The prototypes for these names are defined in the header file
21503 In order to simplify signal handling, a number of macros is provided:
21505 \labelwidthstring 00.00.0000
21507 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
21508 high priority interrupts.
21513 is the function name to use.
21515 \labelwidthstring 00.00.0000
21517 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
21518 low priority interrupt.
21523 is the function name to use.
21525 \labelwidthstring 00.00.0000
21527 DEF_HANDLER(sig,handler) define a handler for signal
21531 \labelwidthstring 00.00.0000
21533 END_DEF end the declaration of the dispatch table.
21536 Additionally there are two more macros to simplify the declaration of the
21539 \labelwidthstring 00.00.0000
21543 SIGHANDLER(handler)
21545 this declares the function prototype for the
21551 \labelwidthstring 00.00.0000
21553 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
21556 An example of using the macros above is shown below:
21559 #include <pic18fregs.h>
21562 #include <signal.h>
21566 DEF_INTHIGH(high_int)
21569 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
21572 DEF_HANDLER(SIG_BCOL, _bcol_handler)
21579 SIGHANDLER(_tmr0_handler)
21585 /* action to be taken when timer 0 overflows */
21592 SIGHANDLERNAKED(_bcol_handler)
21601 /* action to be taken when bus collision occurs */
21617 Special care should be taken when using the above scheme:
21620 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
21623 when declaring SIGHANDLERNAKED handler never forget to use
21627 for proper returning.
21633 Here you can find some general tips for compiling programs with SDCC/pic16.
21634 \layout Subsubsection
21640 \begin_inset LatexCommand \index{PIC16!stack}
21644 size (that is 64 bytes) probably is enough for many programs.
21645 One must take care that when there are many levels of function nesting,
21646 or there is excessive usage of stack, its size should be extended.
21647 An example of such a case is the printf/sprintf family of functions.
21648 If you encounter problems like not being able to print integers, then you
21649 need to set the stack size around the maximum (256 for small stack model).
21650 The following diagram shows what happens when calling printf to print an
21654 printf () --> ltoa () --> ultoa () --> divschar ()
21657 It is should be understood that stack is easily consumed when calling complicate
21659 Using command line arguments like -
21669 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
21671 Other ways to reduce stack usage may exist.
21677 There are several approaches to debugging your code.
21678 This chapter is meant to show your options and to give detail on some of
21683 When writing your code:
21686 write your code with debugging in mind (avoid duplicating code, put conceptually
21687 similar variables into structs, use structured code, have strategic points
21688 within your code where all variables are consistent, ...)
21691 run a syntax-checking tool like splint
21692 \begin_inset LatexCommand \index{splint (syntax checking tool)}
21697 \begin_inset LatexCommand \index{lint (syntax checking tool)}
21712 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21719 for the high level code use a C-compiler (like f.e.
21720 GCC) to compile run and debug the code on your host.
21732 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21736 ) on howto handle syntax extensions like __xdata, __at(), ...
21740 use another C-compiler to compile code for your target.
21741 Always an option but not recommended:) And not very likely to help you.
21742 If you seriously consider walking this path you should at least occasionally
21743 check portability of your code.
21744 Most commercial compiler vendors will offer an evaluation version so you
21745 can test compile your code or snippets of your code.
21748 Debugging on a simulator:
21751 there is a separate section about SDCDB (section
21752 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21759 or (8051 specific) use a freeware/commercial simulator which interfaces
21761 \begin_inset LatexCommand \index{AOMF, AOMF51}
21766 \begin_inset LatexCommand \ref{OMF file}
21770 ) optionally generated by SDCC.
21773 Debugging On-target:
21776 use a MCU port pin to serially output debug data to the RS232 port of your
21778 You'll probably want some level shifting device typically involving a MAX232
21780 If the hardware serial port of the MCU is not available search for 'Software
21781 UART' in your favourite search machine.
21784 use an on-target monitor.
21785 In this context a monitor is a small program which usually accepts commands
21786 via a serial line and allows to set program counter, to single step through
21787 a program and read/write memory locations.
21788 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21790 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21797 toggle MCU port pins at strategic points within your code and use an oscilloscop
21801 digital oscilloscope
21804 \begin_inset LatexCommand \index{Oscilloscope}
21808 with deep trace memory is really helpful especially if you have to debug
21809 a realtime application.
21810 If you need to monitor more pins than your oscilloscope provides you can
21811 sometimes get away with a small R-2R network.
21812 On a single channel oscilloscope you could f.e.
21813 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21815 \begin_inset Formula $\Omega$
21818 resistor and the other one by a 5\SpecialChar ~
21820 \begin_inset Formula $\Omega$
21823 resistor to the oscilloscope probe (check output drive capability of the
21824 pins you want to monitor).
21825 If you need to monitor many more pins a
21845 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21850 Usually very expensive.
21851 And very nice to have too.
21852 And usually locks you (for years...) to the devices the ICE can emulate.
21856 use a remote debugger.
21857 In most 8-bit systems the symbol information is not available on the target,
21858 and a complete debugger is too bulky for the target system.
21859 Therefore usually a debugger on the host system connects to an on-target
21860 debugging stub which accepts only primitive commands.
21863 Terms to enter into your favourite search engine could be 'remote debugging',
21864 'gdb stub' or 'inferior debugger'.
21868 use an on target hardware debugger.
21869 Some of the more modern MCUs include hardware support for setting break
21870 points and monitoring/changing variables by using dedicated hardware pins.
21871 This facility doesn't require additional code to run on the target and
21876 doesn't affect runtime behaviour until a breakpoint is hit.
21877 For the mcs51 most hardware debuggers use the AOMF
21878 \begin_inset LatexCommand \index{AOMF, AOMF51}
21883 \begin_inset LatexCommand \ref{OMF file}
21894 if you are not familiar with any of the following terms you're likely to
21895 run into problems rather sooner than later:
21912 As an embedded programmer you
21916 to know them so why not look them up
21920 you have problems?)
21923 tell someone else about your problem (actually this is a surprisingly effective
21924 means to hunt down the bug even if the listener is not familiar with your
21926 As 'failure to communicate' is probably one of the job-induced deformations
21927 of an embedded programmer this is highly encouraged.
21930 Debugging with SDCDB
21931 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21936 \begin_inset LatexCommand \index{SDCDB (debugger)}
21943 SDCC is distributed with a source level debugger
21944 \begin_inset LatexCommand \index{Debugger}
21949 The debugger uses a command line interface, the command repertoire of the
21950 debugger has been kept as close to gdb
21951 \begin_inset LatexCommand \index{gdb}
21955 (the GNU debugger) as possible.
21956 The configuration and build process is part of the standard compiler installati
21957 on, which also builds and installs the debugger in the target directory
21958 specified during configuration.
21959 The debugger allows you debug BOTH at the C source and at the ASM source
21963 Compiling for Debugging
21977 \begin_inset LatexCommand \index{-\/-debug}
21981 option must be specified for all files for which debug information is to
21983 The compiler generates a .adb file for each of these files.
21984 The linker creates the .cdb
21985 \begin_inset LatexCommand \index{<file>.cdb}
21990 \begin_inset LatexCommand \index{<file>.adb}
21994 files and the address information.
21995 This .cdb is used by the debugger.
21998 How the Debugger Works
22011 -debug option is specified the compiler generates extra symbol information
22012 some of which are put into the assembler source and some are put into the
22014 Then the linker creates the .cdb file from the individual .adb files with
22015 the address information for the symbols.
22016 The debugger reads the symbolic information generated by the compiler &
22017 the address information generated by the linker.
22018 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
22019 execution is controlled by the debugger.
22020 When a command is issued for the debugger, it translates it into appropriate
22021 commands for the simulator.
22022 (Currently SDCDM only connects to the simulator but
22027 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
22031 is an effort to connect directly to the hardware.)
22034 Starting the Debugger SDCDB
22037 The debugger can be started using the following command line.
22038 (Assume the file you are debugging has the file name foo).
22052 The debugger will look for the following files.
22055 foo.c - the source file.
22058 foo.cdb - the debugger symbol information file.
22061 foo.ihx - the Intel hex format
22062 \begin_inset LatexCommand \index{Intel hex format}
22069 SDCDB Command Line Options
22082 -directory=<source file directory> this option can used to specify the directory
22084 The debugger will look into the directory list specified for source, cdb
22086 The items in the directory list must be separated by ':', e.g.
22087 if the source files can be in the directories /home/src1 and /home/src2,
22098 -directory option should be -
22108 -directory=/home/src1:/home/src2.
22109 Note there can be no spaces in the option.
22113 -cd <directory> - change to the <directory>.
22116 -fullname - used by GUI front ends.
22119 -cpu <cpu-type> - this argument is passed to the simulator please see the
22120 simulator docs for details.
22123 -X <Clock frequency > this options is passed to the simulator please see
22124 the simulator docs for details.
22127 -s <serial port file> passed to simulator see the simulator docs for details.
22130 -S <serial in,out> passed to simulator see the simulator docs for details.
22133 -k <port number> passed to simulator see the simulator docs for details.
22136 SDCDB Debugger Commands
22139 As mentioned earlier the command interface for the debugger has been deliberatel
22140 y kept as close the GNU debugger gdb, as possible.
22141 This will help the integration with existing graphical user interfaces
22142 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
22143 If you use a graphical user interface for the debugger you can skip this
22145 \layout Subsubsection*
22147 break [line | file:line | function | file:function]
22150 Set breakpoint at specified line or function:
22159 sdcdb>break foo.c:100
22161 sdcdb>break funcfoo
22163 sdcdb>break foo.c:funcfoo
22164 \layout Subsubsection*
22166 clear [line | file:line | function | file:function ]
22169 Clear breakpoint at specified line or function:
22178 sdcdb>clear foo.c:100
22180 sdcdb>clear funcfoo
22182 sdcdb>clear foo.c:funcfoo
22183 \layout Subsubsection*
22188 Continue program being debugged, after breakpoint.
22189 \layout Subsubsection*
22194 Execute till the end of the current function.
22195 \layout Subsubsection*
22200 Delete breakpoint number 'n'.
22201 If used without any option clear ALL user defined break points.
22202 \layout Subsubsection*
22204 info [break | stack | frame | registers ]
22207 info break - list all breakpoints
22210 info stack - show the function call stack.
22213 info frame - show information about the current execution frame.
22216 info registers - show content of all registers.
22217 \layout Subsubsection*
22222 Step program until it reaches a different source line.
22223 Note: pressing <return> repeats the last command.
22224 \layout Subsubsection*
22229 Step program, proceeding through subroutine calls.
22230 \layout Subsubsection*
22235 Start debugged program.
22236 \layout Subsubsection*
22241 Print type information of the variable.
22242 \layout Subsubsection*
22247 print value of variable.
22248 \layout Subsubsection*
22253 load the given file name.
22254 Note this is an alternate method of loading file for debugging.
22255 \layout Subsubsection*
22260 print information about current frame.
22261 \layout Subsubsection*
22266 Toggle between C source & assembly source.
22267 \layout Subsubsection*
22269 ! simulator command
22272 Send the string following '!' to the simulator, the simulator response is
22274 Note the debugger does not interpret the command being sent to the simulator,
22275 so if a command like 'go' is sent the debugger can loose its execution
22276 context and may display incorrect values.
22277 \layout Subsubsection*
22284 My name is Bobby Brown"
22287 Interfacing SDCDB with DDD
22290 The screenshot was converted from png to eps with:
22291 \begin_inset Quotes sld
22294 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
22295 \begin_inset Quotes srd
22298 which produces a pretty compact eps file which is free from compression
22302 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
22303 as this broke the build system on Sourceforge (pdf-file was broken.
22304 pdflatex does not accept eps files).
22322 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.png}
22328 shows a screenshot of a debugging session with DDD
22329 \begin_inset LatexCommand \index{DDD (debugger)}
22333 (Unix only) on a simulated 8032.
22334 The debugging session might not run as smoothly as the screenshot suggests.
22335 The debugger allows setting of breakpoints, displaying and changing variables,
22336 single stepping through C and assembler code.
22339 The source was compiled with
22362 -debug ddd_example.c
22375 and DDD was invoked with
22382 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
22385 Interfacing SDCDB with XEmacs
22386 \begin_inset LatexCommand \index{XEmacs}
22391 \begin_inset LatexCommand \index{Emacs}
22398 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
22399 sdcdb.el and sdcdbsrc.el.
22400 These two files can be found in the $(prefix)/bin directory after the installat
22402 These files need to be loaded into XEmacs for the interface to work.
22403 This can be done at XEmacs startup time by inserting the following into
22404 your '.xemacs' file (which can be found in your HOME directory):
22410 (load-file sdcdbsrc.el)
22416 .xemacs is a lisp file so the () around the command is REQUIRED.
22417 The files can also be loaded dynamically while XEmacs is running, set the
22418 environment variable 'EMACSLOADPATH' to the installation bin directory
22419 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
22420 To start the interface enter the following command:
22434 You will prompted to enter the file name to be debugged.
22439 The command line options that are passed to the simulator directly are bound
22440 to default values in the file sdcdbsrc.el.
22441 The variables are listed below, these values maybe changed as required.
22444 sdcdbsrc-cpu-type '51
22447 sdcdbsrc-frequency '11059200
22450 sdcdbsrc-serial nil
22453 The following is a list of key mapping for the debugger interface.
22464 ;;key\SpecialChar ~
22478 binding\SpecialChar ~
22502 ;;---\SpecialChar ~
22516 -------\SpecialChar ~
22558 sdcdb-next-from-src\SpecialChar ~
22586 sdcdb-back-from-src\SpecialChar ~
22614 sdcdb-cont-from-src\SpecialChar ~
22624 SDCDB continue command
22642 sdcdb-step-from-src\SpecialChar ~
22670 sdcdb-whatis-c-sexp\SpecialChar ~
22680 SDCDB ptypecommand for data at
22747 sdcdbsrc-delete\SpecialChar ~
22761 SDCDB Delete all breakpoints if no arg
22810 given or delete arg (C-u arg x)
22828 sdcdbsrc-frame\SpecialChar ~
22843 SDCDB Display current frame if no arg,
22892 given or display frame arg
22959 sdcdbsrc-goto-sdcdb\SpecialChar ~
22969 Goto the SDCDB output buffer
22987 sdcdb-print-c-sexp\SpecialChar ~
22998 SDCDB print command for data at
23065 sdcdbsrc-goto-sdcdb\SpecialChar ~
23075 Goto the SDCDB output buffer
23093 sdcdbsrc-mode\SpecialChar ~
23109 Toggles Sdcdbsrc mode (turns it off)
23124 sdcdb-finish-from-src\SpecialChar ~
23132 SDCDB finish command
23147 sdcdb-break\SpecialChar ~
23165 Set break for line with point
23180 sdcdbsrc-mode\SpecialChar ~
23196 Toggle Sdcdbsrc mode
23211 sdcdbsrc-srcmode\SpecialChar ~
23234 Here are a few guidelines that will help the compiler generate more efficient
23235 code, some of the tips are specific to this compiler others are generally
23236 good programming practice.
23239 Use the smallest data type to represent your data-value.
23240 If it is known in advance that the value is going to be less than 256 then
23241 use an 'unsigned char' instead of a 'short' or 'int'.
23242 Please note, that ANSI C requires both signed and unsigned chars to be
23243 promoted to 'signed int'
23244 \begin_inset LatexCommand \index{promotion to signed int}
23249 \begin_inset Marginal
23260 before doing any operation.
23262 \begin_inset LatexCommand \index{type promotion}
23267 \begin_inset LatexCommand \label{type promotion}
23271 can be omitted, if the result is the same.
23272 The effect of the promotion rules together with the sign-extension is often
23279 unsigned char uc = 0xfe;
23281 if (uc * uc < 0) /* this is true! */
23300 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
23310 (unsigned char) -12 / (signed char) -3 = ...
23313 No, the result is not 4:
23318 (int) (unsigned char) -12 / (int) (signed char) -3 =
23320 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
23322 (int) 0x00f4 / (int) 0xfffd =
23324 (int) 0x00f4 / (int) 0xfffd =
23326 (int) 244 / (int) -3 =
23328 (int) -81 = (int) 0xffaf;
23331 Don't complain, that gcc gives you a different result.
23332 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
23333 Therefore the results are different.
23336 \begin_inset Quotes sld
23340 \begin_inset Quotes srd
23348 If well-defined overflow characteristics are important and negative values
23349 are not, or if you want to steer clear of sign-extension problems when
23350 manipulating bits or bytes, use one of the corresponding unsigned types.
23351 (Beware when mixing signed and unsigned values in expressions, though.)
23353 Although character types (especially unsigned char) can be used as "tiny"
23354 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
23355 ble sign extension and increased code size.
23359 Use unsigned when it is known in advance that the value is not going to
23361 This helps especially if you are doing division or multiplication, bit-shifting
23362 or are using an array index.
23365 NEVER jump into a LOOP.
23368 Declare the variables to be local
23369 \begin_inset LatexCommand \index{local variables}
23373 whenever possible, especially loop control variables (induction).
23376 Since the compiler does not always do implicit integral promotion, the programme
23377 r should do an explicit cast when integral promotion is required.
23380 Reducing the size of division, multiplication & modulus operations can reduce
23381 code size substantially.
23382 Take the following code for example.
23388 foobar(unsigned int p1, unsigned char ch)
23396 unsigned char ch1 = p1 % ch ;
23407 For the modulus operation the variable ch will be promoted to unsigned int
23408 first then the modulus operation will be performed (this will lead to a
23409 call to support routine _moduint()), and the result will be casted to a
23411 If the code is changed to
23416 foobar(unsigned int p1, unsigned char ch)
23424 unsigned char ch1 = (unsigned char)p1 % ch ;
23435 It would substantially reduce the code generated (future versions of the
23436 compiler will be smart enough to detect such optimization opportunities).
23440 Have a look at the assembly listing to get a
23441 \begin_inset Quotes sld
23445 \begin_inset Quotes srd
23448 for the code generation.
23451 Porting code from or to other compilers
23452 \begin_inset LatexCommand \label{sec:Porting-code-to-other-compilers}
23459 check whether endianness of the compilers differs and adapt where needed.
23462 check the device specific header files
23463 \begin_inset LatexCommand \index{Header files}
23468 \begin_inset LatexCommand \index{Include files}
23472 for compiler specific syntax.
23473 Eventually include the file <compiler.h
23474 \begin_inset LatexCommand \index{compiler.h (include file)}
23478 > to allow using common header files.
23481 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewvc/sdcc/trunk/sdcc/device/include/mcs51/P89c51RD2.h?view=markup}
23488 check whether the startup code contains the correct initialization (watchdog,
23492 check whether the sizes of short, int, long match.
23495 check if some 16 or 32 bit hardware registers require a specific addressing
23496 order (least significant or most significant byte first) and adapt if needed
23505 relate to time and not to lower/upper memory location here, so this is
23510 the same as endianness).
23513 check whether the keyword
23517 is used where needed.
23518 The compilers might differ in their optimization characteristics (as different
23519 versions of the same compiler might also use more clever optimizations
23520 this is good idea anyway).
23522 \begin_inset LatexCommand \ref{sub:Common-interrupt-pitfall-volatile}
23529 check that the compilers are not told to supress warnings.
23532 check and convert compiler specific extensions (interrupts, memory areas,
23536 check for differences in type promotion.
23537 Especially check for math operations on
23546 For the sake of C99 compatibility SDCC will probably promote these to
23550 more often than other compilers.
23551 Eventually insert explicit casts to
23560 Also check that the ~\SpecialChar ~
23562 \begin_inset LatexCommand \index{\~\/ Operator}
23569 \begin_inset LatexCommand \index{bit}
23575 variables, use the !\SpecialChar ~
23578 \begin_inset LatexCommand \ref{type promotion}
23583 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
23590 check the assembly code generated for interrupt routines (f.e.
23591 for calls to possibly non-reentrant library functions).
23594 check whether timing loops result in proper timing (or preferably consider
23595 a rewrite of the code with timer based delays instead).
23598 check for differences in printf parameters (some compilers push (va_arg
23599 \begin_inset LatexCommand \index{vararg, va\_arg}
23603 ) char variables as
23607 others push them as
23613 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
23620 check the resulting memory map
23621 \begin_inset LatexCommand \index{Memory map}
23626 Usage of different memory spaces: code, stack, data (for mcs51/ds390 additional
23627 ly idata, pdata, xdata).
23628 Eventually check if unexpected library functions are included.
23632 \begin_inset LatexCommand \index{Tools}
23636 included in the distribution
23640 \begin_inset Tabular
23641 <lyxtabular version="3" rows="12" columns="3">
23643 <column alignment="left" valignment="top" leftline="true" width="0pt">
23644 <column alignment="left" valignment="top" leftline="true" width="0pt">
23645 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23646 <row topline="true" bottomline="true">
23647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23672 <row topline="true">
23673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23686 Simulator for various architectures
23689 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23698 <row topline="true">
23699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23713 \begin_inset LatexCommand \index{Header files}
23718 \begin_inset LatexCommand \index{Include files}
23725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23730 sdcc/support/scripts
23734 <row topline="true">
23735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23748 header file conversion
23751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23756 sdcc/support/scripts
23760 <row topline="true">
23761 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23777 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23795 <row topline="true">
23796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23812 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23830 <row topline="true">
23831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23847 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23865 <row topline="true">
23866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23900 <row topline="true">
23901 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23917 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23935 <row topline="true">
23936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23970 <row topline="true">
23971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23987 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24005 <row topline="true" bottomline="true">
24006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24020 \begin_inset LatexCommand \index{packihx (tool)}
24027 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24055 \begin_inset LatexCommand \index{Documentation}
24059 included in the distribution
24063 \begin_inset Tabular
24064 <lyxtabular version="3" rows="10" columns="2">
24066 <column alignment="left" valignment="top" leftline="true" width="0">
24067 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24068 <row topline="true" bottomline="true">
24069 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24077 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24082 Where to get / filename
24086 <row topline="true">
24087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24092 SDCC Compiler User Guide
24095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24100 You're reading it right now
24104 <row topline="true">
24105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24122 <row topline="true">
24123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24129 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
24134 \begin_inset LatexCommand \index{Assembler documentation}
24138 Assemblers and ASLINK
24139 \begin_inset LatexCommand \index{aslink}
24144 \begin_inset LatexCommand \index{Linker documentation}
24151 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24156 sdcc/as/doc/asxhtm.html
24160 <row topline="true">
24161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24166 SDCC regression test
24167 \begin_inset LatexCommand \index{Regression test}
24174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24179 sdcc/doc/test_suite_spec.pdf
24183 <row topline="true">
24184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24201 <row topline="true">
24202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24207 Notes on debugging with SDCDB
24208 \begin_inset LatexCommand \index{SDCDB (debugger)}
24215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24220 sdcc/debugger/README
24224 <row topline="true">
24225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24230 Software simulator for microcontrollers
24233 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24260 <row topline="true">
24261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24266 Temporary notes on the pic16
24267 \begin_inset LatexCommand \index{PIC16}
24274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24279 sdcc/src/pic16/NOTES
24283 <row topline="true" bottomline="true">
24284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24289 SDCC internal documentation (debugging file format)
24292 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24328 Related open source tools
24329 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
24334 \begin_inset LatexCommand \index{Related tools}
24342 \begin_inset Tabular
24343 <lyxtabular version="3" rows="14" columns="3">
24345 <column alignment="left" valignment="top" leftline="true" width="0pt">
24346 <column alignment="block" valignment="top" leftline="true" width="30line%">
24347 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24348 <row topline="true" bottomline="true">
24349 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24374 <row topline="true">
24375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24381 \begin_inset LatexCommand \index{gpsim (pic simulator)}
24388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24396 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24402 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
24410 <row topline="true">
24411 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24417 \begin_inset LatexCommand \index{gputils (pic tools)}
24424 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24438 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
24446 <row topline="true">
24447 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24455 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24469 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
24477 <row topline="true">
24478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24491 Tools for Silicon Laboratories JTAG debug adapter, partly based on SDCDB
24495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24501 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
24509 <row topline="true">
24510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24516 \begin_inset LatexCommand \index{indent (source formatting tool)}
24523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24528 Formats C source - Master of the white spaces
24531 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24537 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
24545 <row topline="true">
24546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24552 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
24559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24564 Object file conversion, checksumming, ...
24567 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24573 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
24581 <row topline="true">
24582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24588 \begin_inset LatexCommand \index{objdump (tool)}
24595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24600 Object file conversion, ...
24603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24608 Part of binutils (should be there anyway)
24612 <row topline="true">
24613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24626 8051 monitor (hex up-/download, single step, disassemble)
24629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24635 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
24643 <row topline="true">
24644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24650 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
24657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24662 Source code documentation system
24665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24671 \begin_inset LatexCommand \url{http://www.doxygen.org}
24679 <row topline="true">
24680 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24688 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24693 IDE (has anyone tried integrating SDCC & SDCDB? Unix only)
24696 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24702 \begin_inset LatexCommand \url{http://www.kdevelop.org}
24710 <row topline="true">
24711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24724 8051 monitor (hex up-/download, single step, disassemble)
24727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24733 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
24741 <row topline="true">
24742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24748 \begin_inset LatexCommand \index{splint (syntax checking tool)}
24755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24760 Statically checks c sources (see
24761 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
24768 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24774 \begin_inset LatexCommand \url{http://www.splint.org}
24782 <row topline="true" bottomline="true">
24783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24789 \begin_inset LatexCommand \index{ddd (debugger)}
24796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24801 Debugger, serves nicely as GUI to SDCDB
24802 \begin_inset LatexCommand \index{SDCDB (debugger)}
24809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24815 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
24832 Related documentation / recommended reading
24836 \begin_inset Tabular
24837 <lyxtabular version="3" rows="8" columns="3">
24839 <column alignment="left" valignment="top" leftline="true" width="0pt">
24840 <column alignment="block" valignment="top" leftline="true" width="30line%">
24841 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24842 <row topline="true" bottomline="true">
24843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24859 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24868 <row topline="true">
24869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24892 \begin_inset LatexCommand \index{C Reference card}
24899 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24905 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24913 <row topline="true">
24914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24936 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24944 <row topline="true">
24945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24953 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24959 \begin_inset Quotes sld
24963 \begin_inset Quotes srd
24969 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24977 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24985 <row topline="true">
24986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25000 \begin_inset Quotes sld
25003 Extensions for Embedded C
25004 \begin_inset Quotes srd
25010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25018 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
25026 <row topline="true">
25027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25039 Latest datasheet of the target CPU
25042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25051 <row topline="true">
25052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25064 Revision history of datasheet
25067 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25076 <row topline="true" bottomline="true">
25077 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25092 Advanced Compiler Design and Implementation
25095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25100 bookstore (very dedicated, probably read other books first)
25113 Application notes specifically for SDCC
25116 SDCC makes no claims about the completeness of this list and about up-to-datenes
25117 s or correctness of the application notes
25118 \begin_inset LatexCommand \index{Application notes}
25128 \begin_inset Tabular
25129 <lyxtabular version="3" rows="7" columns="3">
25131 <column alignment="block" valignment="top" leftline="true" width="17col%">
25132 <column alignment="block" valignment="top" leftline="true" width="27col%">
25133 <column alignment="block" valignment="top" leftline="true" rightline="true" width="57col%">
25134 <row topline="true" bottomline="true">
25135 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25145 <cell alignment="left" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
25155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25166 <row topline="true">
25167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25184 Using the SDCC Compiler for the DS80C400
25185 \begin_inset LatexCommand \index{DS80C400}
25192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25200 \begin_inset LatexCommand \url{http://pdfserv.maxim-ic.com/en/an/AN3346.pdf}
25208 <row topline="true">
25209 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25219 <cell multicolumn="1" alignment="left" valignment="top" topline="true" leftline="true" usebox="none" width="30line%">
25226 Using the Free SDCC C Compiler to Develop Firmware for the DS89C420/430/440/450
25227 \begin_inset LatexCommand \index{DS89C4x0}
25231 Family of Microcontrollers
25234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25242 \begin_inset LatexCommand \url{http://pdfserv.maxim-ic.com/en/an/AN3477.pdf}
25250 <row topline="true">
25251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25258 Silicon Laboratories / Cygnal
25261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25268 Integrating SDCC 8051 Tools Into The Silicon Labs IDE
25271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25279 \begin_inset LatexCommand \url{http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/en/an198.pdf}
25287 <row topline="true">
25288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25295 Ramtron / Goal Semiconductor
25298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25305 Interfacing SDCC to Syn and Textpad
25308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25316 \begin_inset LatexCommand \url{http://www.ramtron.com/doc/Products/Microcontroller/Support_Tools.asp}
25324 <row topline="true">
25325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25332 Ramtron / Goal Semiconductor
25335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25342 Installing and Configuring SDCC and Crimson Editor
25345 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25353 \begin_inset LatexCommand \url{http://www.ramtron.com/doc/Products/Microcontroller/Support_Tools.asp}
25361 <row topline="true" bottomline="true">
25362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25379 MSC12xx Programming with SDCC
25382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25390 \begin_inset LatexCommand \url{http://focus.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=sbaa109&fileType=pdf}
25408 Some questions answered, some pointers given - it might be time to in turn
25416 can you solve your project with the selected microcontroller? Would you
25417 find out early or rather late that your target is too small/slow/whatever?
25418 Can you switch to a slightly better device if it doesn't fit?
25421 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
25422 and/or another programming language be more adequate? Would an operating
25423 system on the target device help?
25426 if you solved the problem, will the marketing department be happy?
25429 if the marketing department is happy, will customers be happy?
25432 if you're the project manager, marketing department and maybe even the customer
25433 in one person, have you tried to see the project from the outside?
25436 is the project done if you think it is done? Or is just that other interface/pro
25437 tocol/feature/configuration/option missing? How about website, manual(s),
25438 internationali(z|s)ation, packaging, labels, 2nd source for components,
25439 electromagnetic compatability/interference, documentation for production,
25440 production test software, update mechanism, patent issues?
25443 is your project adequately positioned in that magic triangle: fame, fortune,
25447 Maybe not all answers to these questions are known and some answers may
25452 , nevertheless knowing these questions may help you to avoid burnout
25458 burnout is bad for electronic devices, programmers and motorcycle tyres
25462 Chances are you didn't want to hear some of them...
25466 \begin_inset LatexCommand \index{Support}
25473 SDCC has grown to be a large project.
25474 The compiler alone (without the preprocessor, assembler and linker) is
25475 well over 150,000 lines of code (blank stripped).
25476 The open source nature of this project is a key to its continued growth
25478 You gain the benefit and support of many active software developers and
25480 Is SDCC perfect? No, that's why we need your help.
25481 The developers take pride in fixing reported bugs.
25482 You can help by reporting the bugs and helping other SDCC users.
25483 There are lots of ways to contribute, and we encourage you to take part
25484 in making SDCC a great software package.
25488 The SDCC project is hosted on the SDCC sourceforge site at
25489 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
25494 You'll find the complete set of mailing lists
25495 \begin_inset LatexCommand \index{Mailing list(s)}
25499 , forums, bug reporting system, patch submission
25500 \begin_inset LatexCommand \index{Patch submission}
25505 \begin_inset LatexCommand \index{download}
25509 area and Subversion code repository
25510 \begin_inset LatexCommand \index{Subversion code repository}
25518 \begin_inset LatexCommand \index{Bug reporting}
25523 \begin_inset LatexCommand \index{Reporting bugs}
25530 The recommended way of reporting bugs is using the infrastructure of the
25532 You can follow the status of bug reports there and have an overview about
25536 Bug reports are automatically forwarded to the developer mailing list and
25537 will be fixed ASAP.
25538 When reporting a bug, it is very useful to include a small test program
25539 (the smaller the better) which reproduces the problem.
25540 If you can isolate the problem by looking at the generated assembly code,
25541 this can be very helpful.
25542 Compiling your program with the -
25553 \begin_inset LatexCommand \index{-\/-dumpall}
25557 option can sometimes be useful in locating optimization problems.
25558 When reporting a bug please make sure you:
25561 Attach the code you are compiling with SDCC.
25565 Specify the exact command you use to run SDCC, or attach your Makefile.
25569 Specify the SDCC version (type "
25575 "), your platform, and operating system.
25579 Provide an exact copy of any error message or incorrect output.
25583 Put something meaningful in the subject of your message.
25586 Please attempt to include these 5 important parts, as applicable, in all
25587 requests for support or when reporting any problems or bugs with SDCC.
25588 Though this will make your message lengthy, it will greatly improve your
25589 chance that SDCC users and developers will be able to help you.
25590 Some SDCC developers are frustrated by bug reports without code provided
25591 that they can use to reproduce and ultimately fix the problem, so please
25592 be sure to provide sample code if you are reporting a bug!
25595 Please have a short check that you are using a recent version of SDCC and
25596 the bug is not yet known.
25597 This is the link for reporting bugs:
25598 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
25603 With SDCC on average having more than 200 downloads
25604 \begin_inset LatexCommand \index{download}
25608 on sourceforge per day
25614 220 daily downloads on average Jan-Sept 2006 and about 150 daily downloads
25615 between 2002 and 2005.
25616 This does not include other methods of distribution.
25619 there must be some users.
25620 So it's not exactly easy to find a new bug.
25621 If you find one we need it:
25623 reporting bugs is good
25628 Requesting Features
25629 \begin_inset LatexCommand \label{sub:Requesting-Features}
25634 \begin_inset LatexCommand \index{Feature request}
25639 \begin_inset LatexCommand \index{Requesting features}
25646 Like bug reports feature requests are forwarded to the developer mailing
25648 This is the link for requesting features:
25649 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
25659 Like bug reports contributed patches are forwarded to the developer mailing
25661 This is the link for submitting patches
25662 \begin_inset LatexCommand \index{Patch submission}
25667 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
25674 You need to specify some parameters to the
25678 command for the patches to be useful.
25679 If you modified more than one file a patch created f.e.
25684 \begin_inset Quotes sld
25687 diff -Naur unmodified_directory modified_directory >my_changes.patch
25688 \begin_inset Quotes srd
25694 will be fine, otherwise
25698 \begin_inset Quotes sld
25701 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
25702 \begin_inset Quotes srd
25715 These links should take you directly to the
25716 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
25726 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
25727 automated messages (mid 2003)
25731 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
25736 \begin_inset LatexCommand \index{Mailing list(s)}
25740 and forums are archived and searchable so if you are lucky someone already
25741 had a similar problem.
25742 While mails to the lists themselves are delivered promptly their web front
25743 end on sourceforge sometimes shows a severe time lag (up to several weeks),
25744 if you're seriously using SDCC please consider subscribing to the lists.
25750 You can follow the status of the Subversion version
25751 \begin_inset LatexCommand \index{version}
25755 of SDCC by watching the Changelog
25756 \begin_inset LatexCommand \index{Changelog}
25760 in the Subversion repository
25763 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
25770 Subversion Source Code Repository
25779 or the filenames of the snapshot versions of SDCC include date and its
25781 \begin_inset LatexCommand \index{Subversion code repository}
25786 Subversion allows to download the source of recent or previous versions
25788 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
25792 (by number or by date).
25793 An on-line source code browser and detailled instructions are also available
25795 SDCC versions starting from 1999 up to now are available (currently the
25796 versions prior to the conversion from cvs
25797 \begin_inset LatexCommand \index{cvs (see Subversion)}
25801 to Subversion (April 2006) are either by accessible by Subversion or by
25806 \begin_inset LatexCommand \index{Release policy}
25813 Historically there often were long delays between official releases and
25814 the sourceforge download area tends to get not updated at all.
25815 Excuses in the past might have referred to problems with live range analysis,
25816 but as this was fixed a while ago, the current problem is that another
25817 excuse has to be found.
25818 Kidding aside, we have to get better there! On the other hand there are
25819 daily snapshots available at
25820 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
25824 , and you can always build the very last version (hopefully with many bugs
25825 fixed, and features added) from the source code available at
25826 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
25834 \begin_inset LatexCommand \index{Examples}
25841 You'll find some small examples in the directory
25843 sdcc/device/examples/.
25846 More examples and libraries are available at
25848 The SDCC Open Knowledge Resource
25849 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
25856 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
25863 I did insert a reference to Paul's web site here although it seems rather
25864 dedicated to a specific 8032 board (I think it's okay because it f.e.
25865 shows LCD/Harddisc interface and has a free 8051 monitor.
25866 Independent 8032 board vendors face hard competition of heavily subsidized
25867 development boards anyway).
25870 Maybe we should include some links to real world applications.
25871 Preferably pointer to pointers (one for each architecture) so this stays
25876 \begin_inset LatexCommand \index{Quality control}
25883 The compiler is passed through nightly compile and build checks.
25889 \begin_inset LatexCommand \index{Regression test}
25893 check that SDCC itself compiles flawlessly on several platforms and checks
25894 the quality of the code generated by SDCC by running the code through simulator
25896 There is a separate document
25899 \begin_inset LatexCommand \index{Test suite}
25908 You'll find the test code in the directory
25910 sdcc/support/regression
25913 You can run these tests manually by running
25917 in this directory (or f.e.
25922 \begin_inset Quotes sld
25926 \begin_inset Quotes srd
25932 if you don't want to run the complete tests).
25933 The test code might also be interesting if you want to look for examples
25934 \begin_inset LatexCommand \index{Examples}
25938 checking corner cases of SDCC or if you plan to submit patches
25939 \begin_inset LatexCommand \index{Patch submission}
25946 The pic port uses a different set of regression tests, you'll find them
25949 sdcc/src/regression
25954 Use of SDCC in Education
25967 the phrase "use in education" might evoke the association "
25971 fit for use in education".
25972 This connotation is not intended but nevertheless risked as the licensing
25973 of SDCC makes it difficult to offer educational discounts
25977 If your rationales are to:
25980 give students a chance to understand the
25984 steps of code generation
25987 have a curriculum that can be extended for years.
25988 Then you could use an fpga board as target and your curriculum will seamlessly
25989 extend from logic synthesis (
25990 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
25995 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
25999 ), over assembly programming, to C to FPGA compilers (
26000 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
26007 be able to insert excursions about skills like using a revision control
26008 system, submitting/applying patches, using a type-setting (as opposed to
26009 word-processing) engine LyX/LaTeX, using
26010 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
26015 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
26019 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
26020 Source Software, CPU simulation, compiler regression tests
26021 \begin_inset LatexCommand \index{Regression test}
26028 And if there should be a shortage of ideas then you can always point students
26029 to the ever-growing feature request list
26030 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
26037 not tie students to a specific host platform and instead allow them to use
26042 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
26044 \begin_inset LatexCommand \url[OLPC]{http://www.laptop.org}
26051 not encourage students to use illegal copies of educational software
26054 be immune to licensing/availability/price changes of the chosen tool chain
26057 be able to change to a new target platform without having to adopt a new
26061 have complete control over and insight into the tool chain
26064 make your students aware about the pros and cons of open source software
26068 give back to the public as you are probably at least partially publically
26072 give students a chance to publically prove their skills and to possibly
26073 see a world wide impact
26076 then SDCC is probably among the first choices.
26077 Well, probably SDCC might be the only choice.
26080 SDCC Technical Data
26084 \begin_inset LatexCommand \index{Optimizations}
26091 SDCC performs a host of standard optimizations in addition to some MCU specific
26096 Sub-expression Elimination
26097 \begin_inset LatexCommand \index{Subexpression elimination}
26104 The compiler does local and
26130 will be translated to
26142 Some subexpressions are not as obvious as the above example, e.g.:
26152 In this case the address arithmetic a->b[i] will be computed only once;
26153 the equivalent code in C would be.
26165 The compiler will try to keep these temporary variables in registers.
26168 Dead-Code Elimination
26169 \begin_inset LatexCommand \index{Dead-code elimination}
26190 i = 1; \SpecialChar ~
26199 global = 1;\SpecialChar ~
26212 global = 3;\SpecialChar ~
26237 \begin_inset LatexCommand \index{Copy propagation}
26293 Note: the dead stores created by this copy propagation will be eliminated
26294 by dead-code elimination.
26298 \begin_inset LatexCommand \index{Loop optimization}
26303 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
26310 Two types of loop optimizations are done by SDCC
26318 of loop induction variables.
26319 In addition to the strength reduction the optimizer marks the induction
26320 variables and the register allocator tries to keep the induction variables
26321 in registers for the duration of the loop.
26322 Because of this preference of the register allocator
26323 \begin_inset LatexCommand \index{Register allocation}
26327 , loop induction optimization causes an increase in register pressure, which
26328 may cause unwanted spilling of other temporary variables into the stack
26329 \begin_inset LatexCommand \index{stack}
26334 The compiler will generate a warning message when it is forced to allocate
26335 extra space either on the stack or data space.
26336 If this extra space allocation is undesirable then induction optimization
26337 can be eliminated either for the entire source file (with -
26347 -noinduction option) or for a given function only using #pragma\SpecialChar ~
26349 \begin_inset LatexCommand \index{\#pragma noinduction}
26362 for (i = 0 ; i < 100 ; i ++)
26378 for (i = 0; i < 100; i++)
26387 As mentioned previously some loop invariants are not as apparent, all static
26388 address computations are also moved out of the loop.
26393 \begin_inset LatexCommand \index{Strength reduction}
26397 , this optimization substitutes an expression by a cheaper expression:
26402 for (i=0;i < 100; i++)
26420 for (i=0;i< 100;i++) {
26426 ar[itemp1] = itemp2;
26443 The more expensive multiplication
26444 \begin_inset LatexCommand \index{Multiplication}
26448 is changed to a less expensive addition.
26452 \begin_inset LatexCommand \index{Loop reversing}
26459 This optimization is done to reduce the overhead of checking loop boundaries
26460 for every iteration.
26461 Some simple loops can be reversed and implemented using a
26462 \begin_inset Quotes eld
26465 decrement and jump if not zero
26466 \begin_inset Quotes erd
26470 SDCC checks for the following criterion to determine if a loop is reversible
26471 (note: more sophisticated compilers use data-dependency analysis to make
26472 this determination, SDCC uses a more simple minded analysis).
26475 The 'for' loop is of the form
26481 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
26491 The <for body> does not contain
26492 \begin_inset Quotes eld
26496 \begin_inset Quotes erd
26500 \begin_inset Quotes erd
26506 All goto's are contained within the loop.
26509 No function calls within the loop.
26512 The loop control variable <sym> is not assigned any value within the loop
26515 The loop control variable does NOT participate in any arithmetic operation
26519 There are NO switch statements in the loop.
26522 Algebraic Simplifications
26525 SDCC does numerous algebraic simplifications, the following is a small sub-set
26526 of these optimizations.
26531 i = j + 0;\SpecialChar ~
26535 /* changed to: */\SpecialChar ~
26541 i /= 2;\SpecialChar ~
26548 /* changed to: */\SpecialChar ~
26554 i = j - j;\SpecialChar ~
26558 /* changed to: */\SpecialChar ~
26564 i = j / 1;\SpecialChar ~
26568 /* changed to: */\SpecialChar ~
26575 Note the subexpressions
26576 \begin_inset LatexCommand \index{Subexpression}
26580 given above are generally introduced by macro expansions or as a result
26581 of copy/constant propagation.
26584 'switch' Statements
26585 \begin_inset LatexCommand \label{sub:'switch'-Statements}
26590 \begin_inset LatexCommand \index{switch statement}
26597 SDCC can optimize switch statements to jump tables
26598 \begin_inset LatexCommand \index{jump tables}
26603 It makes the decision based on an estimate of the generated code size.
26604 SDCC is quite liberal in the requirements for jump table generation:
26607 The labels need not be in order, and the starting number need not be one
26608 or zero, the case labels are in numerical sequence or not too many case
26609 labels are missing.
26615 switch(i) {\SpecialChar ~
26646 case 4: ...\SpecialChar ~
26678 case 5: ...\SpecialChar ~
26710 case 3: ...\SpecialChar ~
26741 case 6: ...\SpecialChar ~
26773 case 7: ...\SpecialChar ~
26805 case 8: ...\SpecialChar ~
26837 case 9: ...\SpecialChar ~
26869 case 10: ...\SpecialChar ~
26900 case 11: ...\SpecialChar ~
26967 Both the above switch statements will be implemented using a jump-table.
26968 The example to the right side is slightly more efficient as the check for
26969 the lower boundary of the jump-table is not needed.
26973 The number of case labels is not larger than supported by the target architectur
26977 If the case labels are not in numerical sequence ('gaps' between cases)
26978 SDCC checks whether a jump table with additionally inserted dummy cases
26979 is still attractive.
26983 If the starting number is not zero and a check for the lower boundary of
26984 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
26989 Switch statements which have large gaps in the numeric sequence or those
26990 that have too many case labels can be split into more than one switch statement
26991 for efficient code generation, e.g.:
27071 If the above switch statement is broken down into two switch statements
27161 then both the switch statements will be implemented using jump-tables whereas
27162 the unmodified switch statement will not be.
27165 There might be reasons which SDCC cannot know about to either favour or
27166 not favour jump tables.
27167 If the target system has to be as quick for the last switch case as for
27168 the first (pro jump table), or if the switch argument is known to be zero
27169 in the majority of the cases (contra jump table).
27172 The pragma nojtbound
27173 \begin_inset LatexCommand \index{\#pragma nojtbound}
27177 can be used to turn off checking the
27190 It has no effect if a default label is supplied.
27191 Use of this pragma is dangerous: if the switch
27192 \begin_inset LatexCommand \index{switch statement}
27196 argument is not matched by a case statement the processor will happily
27200 Bit-shifting Operations
27201 \begin_inset LatexCommand \index{Bit shifting}
27208 Bit shifting is one of the most frequently used operation in embedded programmin
27210 SDCC tries to implement bit-shift operations in the most efficient way
27226 generates the following code:
27243 In general SDCC will never setup a loop if the shift count is known.
27286 \begin_inset LatexCommand \index{Bit rotation}
27293 A special case of the bit-shift operation is bit rotation
27294 \begin_inset LatexCommand \index{rotating bits}
27298 , SDCC recognizes the following expression to be a left bit-rotation:
27308 char i;\SpecialChar ~
27319 /* unsigned is needed for rotation */
27324 i = ((i << 1) | (i >> 7));
27333 will generate the following code:
27352 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
27353 ns of this case will also be recognized as bit-rotation, i.e.:
27358 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
27361 Nibble and Byte Swapping
27364 Other special cases of the bit-shift operations are nibble or byte swapping
27365 \begin_inset LatexCommand \index{swapping nibbles/bytes}
27369 , SDCC recognizes the following expressions:
27392 i = ((i << 4) | (i >> 4));
27398 j = ((j << 8) | (j >> 8));
27401 and generates a swap instruction for the nibble swapping
27402 \begin_inset LatexCommand \index{Nibble swapping}
27406 or move instructions for the byte swapping
27407 \begin_inset LatexCommand \index{Byte swapping}
27413 \begin_inset Quotes sld
27417 \begin_inset Quotes srd
27420 example can be used to convert from little to big-endian or vice versa.
27421 If you want to change the endianness of a
27425 integer you have to cast to
27432 Note that SDCC stores numbers in little-endian
27438 Usually 8-bit processors don't care much about endianness.
27439 This is not the case for the standard 8051 which only has an instruction
27445 \begin_inset LatexCommand \index{DPTR}
27453 so little-endian is the more efficient byte order.
27457 \begin_inset LatexCommand \index{little-endian}
27462 \begin_inset LatexCommand \index{Endianness}
27467 lowest order first).
27471 \begin_inset LatexCommand \index{Highest Order Bit}
27476 \begin_inset LatexCommand \index{Any Order Bit}
27483 It is frequently required to obtain the highest order bit of an integral
27484 type (long, int, short or char types).
27485 Also obtaining any other order bit is not uncommon.
27486 SDCC recognizes the following expressions to yield the highest order bit
27487 and generates optimized code for it, e.g.:
27500 unsigned char hob1, aob1;
27504 bit hob2, hob3, aob2, aob3;
27513 hob1 = (gint >> 15) & 1;
27517 hob2 = (gint >> 15) & 1;
27521 hob3 = gint & 0x8000;
27525 aob1 = (gint >> 9) & 1;
27529 aob2 = (gint >> 8) & 1;
27533 aob3 = gint & 0x0800;
27543 will generate the following code:
27576 000A E5*01\SpecialChar ~
27603 000C 23\SpecialChar ~
27634 000D 54 01\SpecialChar ~
27661 000F F5*02\SpecialChar ~
27716 0011 E5*01\SpecialChar ~
27743 0013 33\SpecialChar ~
27773 0014 92*00\SpecialChar ~
27828 0016 E5*01\SpecialChar ~
27855 0018 33\SpecialChar ~
27885 0019 92*01\SpecialChar ~
27940 001B E5*01\SpecialChar ~
27967 001D 03\SpecialChar ~
27998 001E 54 01\SpecialChar ~
28025 0020 F5*03\SpecialChar ~
28080 0022 E5*01\SpecialChar ~
28107 0024 13\SpecialChar ~
28137 0025 92*02\SpecialChar ~
28192 0027 E5*01\SpecialChar ~
28219 0029 A2 E3\SpecialChar ~
28246 002B 92*03\SpecialChar ~
28274 Other variations of these cases however will
28279 They are standard C expressions, so I heartily recommend these be the only
28280 way to get the highest order bit, (it is portable).
28281 Of course it will be recognized even if it is embedded in other expressions,
28287 xyz = gint + ((gint >> 15) & 1);
28290 will still be recognized.
28294 \begin_inset LatexCommand \index{Higher Order Byte}
28298 / Higher Order Word
28299 \begin_inset LatexCommand \index{Higher Order Word}
28306 It is also frequently required to obtain a higher order byte or word of
28307 a larger integral type (long, int or short types).
28308 SDCC recognizes the following expressions to yield the higher order byte
28309 or word and generates optimized code for it, e.g.:
28316 unsigned long int glong;
28324 unsigned char hob1, hob2;
28328 unsigned int how1, how2;
28337 hob1 = (gint >> 8) & 0xFF;
28341 hob2 = glong >> 24;
28345 how1 = (glong >> 16) & 0xFFFF;
28359 will generate the following code:
28392 0037 85*01*06\SpecialChar ~
28414 _foo_hob1_1_1,(_gint + 1)
28444 003A 85*05*07\SpecialChar ~
28466 _foo_hob2_1_1,(_glong + 3)
28496 003D 85*04*08\SpecialChar ~
28518 _foo_how1_1_1,(_glong + 2)
28520 0040 85*05*09\SpecialChar ~
28542 (_foo_how1_1_1 + 1),(_glong + 3)
28544 0043 85*03*0A\SpecialChar ~
28566 _foo_how2_1_1,(_glong + 1)
28568 0046 85*04*0B\SpecialChar ~
28590 (_foo_how2_1_1 + 1),(_glong + 2)
28593 Again, variations of these cases may
28598 They are standard C expressions, so I heartily recommend these be the only
28599 way to get the higher order byte/word, (it is portable).
28600 Of course it will be recognized even if it is embedded in other expressions,
28606 xyz = gint + ((gint >> 8) & 0xFF);
28609 will still be recognized.
28613 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
28618 \begin_inset LatexCommand \index{Peephole optimizer}
28625 The compiler uses a rule based, pattern matching and re-writing mechanism
28626 for peep-hole optimization.
28631 a peep-hole optimizer by Christopher W.
28632 Fraser (cwfraser\SpecialChar ~
28635 A default set of rules are compiled into the compiler, additional rules
28636 may be added with the
28649 \begin_inset LatexCommand \index{-\/-peep-file}
28656 The rule language is best illustrated with examples.
28680 The above rule will change the following assembly
28681 \begin_inset LatexCommand \index{Assembler routines}
28703 Note: All occurrences of a
28707 (pattern variable) must denote the same string.
28708 With the above rule, the assembly sequence:
28718 will remain unmodified.
28722 Other special case optimizations may be added by the user (via
28738 some variants of the 8051 MCU
28739 \begin_inset LatexCommand \index{MCS51 variants}
28752 The following two rules will change all
28771 replace { lcall %1 } by { acall %1 }
28773 replace { ljmp %1 } by { ajmp %1 }
28778 inline-assembler code
28780 is also passed through the peep hole optimizer, thus the peephole optimizer
28781 can also be used as an assembly level macro expander.
28782 The rules themselves are MCU dependent whereas the rule language infra-structur
28783 e is MCU independent.
28784 Peephole optimization rules for other MCU can be easily programmed using
28789 The syntax for a rule is as follows:
28794 rule := replace [ restart ] '{' <assembly sequence> '
28832 <assembly sequence> '
28850 '}' [if <functionName> ] '
28855 <assembly sequence> := assembly instruction (each instruction including
28856 labels must be on a separate line).
28860 The optimizer will apply to the rules one by one from the top in the sequence
28861 of their appearance, it will terminate when all rules are exhausted.
28862 If the 'restart' option is specified, then the optimizer will start matching
28863 the rules again from the top, this option for a rule is expensive (performance)
28864 , it is intended to be used in situations where a transformation will trigger
28865 the same rule again.
28866 An example of this (not a good one, it has side effects) is the following
28889 Note that the replace pattern cannot be a blank, but can be a comment line.
28890 Without the 'restart' option only the innermost 'pop' 'push' pair would
28891 be eliminated, i.e.:
28921 the restart option the rule will be applied again to the resulting code
28922 and then all the pop-push pairs will be eliminated to yield:
28932 A conditional function can be attached to a rule.
28933 Attaching rules are somewhat more involved, let me illustrate this with
28960 The optimizer does a look-up of a function name table defined in function
28965 in the source file SDCCpeeph.c, with the name
28970 If it finds a corresponding entry the function is called.
28971 Note there can be no parameters specified for these functions, in this
28976 is crucial, since the function
28980 expects to find the label in that particular variable (the hash table containin
28981 g the variable bindings is passed as a parameter).
28982 If you want to code more such functions, take a close look at the function
28983 labelInRange and the calling mechanism in source file SDCCpeeph.c.
28984 Currently implemented are
28986 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
28987 24bitMode, portIsDS390, 24bitModeAndPortDS390
28996 I know this whole thing is a little kludgey, but maybe some day we will
28997 have some better means.
28998 If you are looking at this file, you will see the default rules that are
28999 compiled into the compiler, you can add your own rules in the default set
29000 there if you get tired of specifying the -
29014 \begin_inset LatexCommand \index{ANSI-compliance}
29019 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
29026 The latest publically available version of the standard
29028 ISO/IEC 9899 - Programming languages - C
29030 should be available at:
29031 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
29040 Deviations from the compliance:
29043 functions are not reentrant
29044 \begin_inset LatexCommand \index{reentrant}
29048 unless explicitly declared as such or the
29061 \begin_inset LatexCommand \index{-\/-stack-auto}
29067 command line option is specified.
29071 \begin_inset LatexCommand \index{struct}
29076 \begin_inset LatexCommand \index{union}
29080 cannot be assigned values directly, cannot be passed as function parameters
29081 or assigned to each other and cannot be a return value
29082 \begin_inset LatexCommand \index{return value}
29086 from a function, e.g.:
29112 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
29131 s parms) /* invalid in SDCC although allowed in ANSI */
29152 return rets; /* is invalid in SDCC although allowed in ANSI */
29158 initialization of structure arrays must be fully braced.
29164 struct s { char x } a[] = {1, 2};\SpecialChar ~
29169 /* invalid in SDCC */
29171 struct s { char x } a[] = {{1}, {2}}; /* OK */
29176 \begin_inset LatexCommand \index{long long (not supported)}
29181 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
29189 \begin_inset LatexCommand \index{double (not supported)}
29193 ' precision floating point
29194 \begin_inset LatexCommand \index{Floating point support}
29202 \begin_inset LatexCommand \index{K\&R style}
29206 function declarations are NOT allowed.
29212 foo(i,j) /* this old style of function declarations */
29214 int i,j; /* are valid in ANSI but not valid in SDCC */
29229 Most enhancements in C99 are not supported, f.e.:
29237 \begin_inset LatexCommand \index{inline (not supported)}
29243 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
29249 can be used as a work around */
29257 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
29261 Certain words that are valid identifiers in the standard may be reserved
29262 words in SDCC unless the
29275 \begin_inset LatexCommand \index{-\/-std-c89}
29290 \begin_inset LatexCommand \index{-\/-std-c99}
29296 command line options are used.
29297 These may include (depending on the selected processor): 'at', 'banked',
29298 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
29299 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
29300 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
29302 Compliant equivalents of these keywords are always available in a form
29303 that begin with two underscores
29304 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
29309 '__data' instead of 'data'.
29312 Cyclomatic Complexity
29313 \begin_inset LatexCommand \index{Cyclomatic complexity}
29320 Cyclomatic complexity of a function is defined as the number of independent
29321 paths the program can take during execution of the function.
29322 This is an important number since it defines the number test cases you
29323 have to generate to validate the function.
29324 The accepted industry standard for complexity number is 10, if the cyclomatic
29325 complexity reported by SDCC exceeds 10 you should think about simplification
29326 of the function logic.
29327 Note that the complexity level is not related to the number of lines of
29328 code in a function.
29329 Large functions can have low complexity, and small functions can have large
29335 SDCC uses the following formula to compute the complexity:
29340 complexity = (number of edges in control flow graph) - (number of nodes
29341 in control flow graph) + 2;
29345 Having said that the industry standard is 10, you should be aware that in
29346 some cases it be may unavoidable to have a complexity level of less than
29348 For example if you have switch statement with more than 10 case labels,
29349 each case label adds one to the complexity level.
29350 The complexity level is by no means an absolute measure of the algorithmic
29351 complexity of the function, it does however provide a good starting point
29352 for which functions you might look at for further optimization.
29355 Retargetting for other Processors
29358 The issues for retargetting the compiler are far too numerous to be covered
29360 What follows is a brief description of each of the seven phases of the
29361 compiler and its MCU dependency.
29364 Parsing the source and building the annotated parse tree.
29365 This phase is largely MCU independent (except for the language extensions).
29366 Syntax & semantic checks are also done in this phase, along with some initial
29367 optimizations like back patching labels and the pattern matching optimizations
29368 like bit-rotation etc.
29371 The second phase involves generating an intermediate code which can be easy
29372 manipulated during the later phases.
29373 This phase is entirely MCU independent.
29374 The intermediate code generation assumes the target machine has unlimited
29375 number of registers, and designates them with the name iTemp.
29376 The compiler can be made to dump a human readable form of the code generated
29390 This phase does the bulk of the standard optimizations and is also MCU independe
29392 This phase can be broken down into several sub-phases:
29396 Break down intermediate code (iCode) into basic blocks.
29398 Do control flow & data flow analysis on the basic blocks.
29400 Do local common subexpression elimination, then global subexpression elimination
29402 Dead code elimination
29406 If loop optimizations caused any changes then do 'global subexpression eliminati
29407 on' and 'dead code elimination' again.
29410 This phase determines the live-ranges; by live range I mean those iTemp
29411 variables defined by the compiler that still survive after all the optimization
29413 Live range analysis
29414 \begin_inset LatexCommand \index{Live range analysis}
29418 is essential for register allocation, since these computation determines
29419 which of these iTemps will be assigned to registers, and for how long.
29422 Phase five is register allocation.
29423 There are two parts to this process.
29427 The first part I call 'register packing' (for lack of a better term).
29428 In this case several MCU specific expression folding is done to reduce
29433 The second part is more MCU independent and deals with allocating registers
29434 to the remaining live ranges.
29435 A lot of MCU specific code does creep into this phase because of the limited
29436 number of index registers available in the 8051.
29439 The Code generation phase is (unhappily), entirely MCU dependent and very
29440 little (if any at all) of this code can be reused for other MCU.
29441 However the scheme for allocating a homogenized assembler operand for each
29442 iCode operand may be reused.
29445 As mentioned in the optimization section the peep-hole optimizer is rule
29446 based system, which can reprogrammed for other MCUs.
29450 \begin_inset LatexCommand \index{Compiler internals}
29457 The anatomy of the compiler
29458 \begin_inset LatexCommand \label{sub:The-anatomy-of}
29467 This is an excerpt from an article published in Circuit Cellar Magazine
29473 It's a little outdated (the compiler is much more efficient now and user/develo
29474 per friendly), but pretty well exposes the guts of it all.
29480 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
29481 It is fairly easy to retarget for other 8-bit MCU.
29482 Here we take a look at some of the internals of the compiler.
29487 \begin_inset LatexCommand \index{Parsing}
29494 Parsing the input source file and creating an AST (Annotated Syntax Tree
29495 \begin_inset LatexCommand \index{Annotated syntax tree}
29500 This phase also involves propagating types (annotating each node of the
29501 parse tree with type information) and semantic analysis.
29502 There are some MCU specific parsing rules.
29503 For example the storage classes, the extended storage classes are MCU specific
29504 while there may be a xdata storage class for 8051 there is no such storage
29505 class for z80 or Atmel AVR.
29506 SDCC allows MCU specific storage class extensions, i.e.
29507 xdata will be treated as a storage class specifier when parsing 8051 C
29508 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
29513 \begin_inset LatexCommand \index{iCode}
29520 Intermediate code generation.
29521 In this phase the AST is broken down into three-operand form (iCode).
29522 These three operand forms are represented as doubly linked lists.
29523 ICode is the term given to the intermediate form generated by the compiler.
29524 ICode example section shows some examples of iCode generated for some simple
29525 C source functions.
29529 \begin_inset LatexCommand \index{Optimizations}
29536 Bulk of the target independent optimizations is performed in this phase.
29537 The optimizations include constant propagation, common sub-expression eliminati
29538 on, loop invariant code movement, strength reduction of loop induction variables
29539 and dead-code elimination.
29542 Live range analysis
29543 \begin_inset LatexCommand \index{Live range analysis}
29550 During intermediate code generation phase, the compiler assumes the target
29551 machine has infinite number of registers and generates a lot of temporary
29553 The live range computation determines the lifetime of each of these compiler-ge
29554 nerated temporaries.
29555 A picture speaks a thousand words.
29556 ICode example sections show the live range annotations for each of the
29558 It is important to note here, each iCode is assigned a number in the order
29559 of its execution in the function.
29560 The live ranges are computed in terms of these numbers.
29561 The from number is the number of the iCode which first defines the operand
29562 and the to number signifies the iCode which uses this operand last.
29565 Register Allocation
29566 \begin_inset LatexCommand \index{Register allocation}
29573 The register allocation determines the type and number of registers needed
29575 In most MCUs only a few registers can be used for indirect addressing.
29576 In case of 8051 for example the registers R0 & R1 can be used to indirectly
29577 address the internal ram and DPTR to indirectly address the external ram.
29578 The compiler will try to allocate the appropriate register to pointer variables
29580 ICode example section shows the operands annotated with the registers assigned
29582 The compiler will try to keep operands in registers as much as possible;
29583 there are several schemes the compiler uses to do achieve this.
29584 When the compiler runs out of registers the compiler will check to see
29585 if there are any live operands which is not used or defined in the current
29586 basic block being processed, if there are any found then it will push that
29587 operand and use the registers in this block, the operand will then be popped
29588 at the end of the basic block.
29592 There are other MCU specific considerations in this phase.
29593 Some MCUs have an accumulator; very short-lived operands could be assigned
29594 to the accumulator instead of a general-purpose register.
29600 Figure II gives a table of iCode operations supported by the compiler.
29601 The code generation involves translating these operations into corresponding
29602 assembly code for the processor.
29603 This sounds overly simple but that is the essence of code generation.
29604 Some of the iCode operations are generated on a MCU specific manner for
29605 example, the z80 port does not use registers to pass parameters so the
29606 SEND and RECV iCode operations will not be generated, and it also does
29607 not support JUMPTABLES.
29614 <Where is Figure II?>
29617 In the original article Figure II was announced to be downloadable on
29622 Unfortunately it never seemed to have shown up there, so: where is Figure
29627 \begin_inset LatexCommand \index{iCode}
29634 This section shows some details of iCode.
29635 The example C code does not do anything useful; it is used as an example
29636 to illustrate the intermediate code generated by the compiler.
29648 /* This function does nothing useful.
29655 for the purpose of explaining iCode */
29658 short function (data int *x)
29666 short i=10; \SpecialChar ~
29668 /* dead initialization eliminated */
29673 short sum=10; /* dead initialization eliminated */
29686 while (*x) *x++ = *p++;
29700 /* compiler detects i,j to be induction variables */
29704 for (i = 0, j = 10 ; i < 10 ; i++, j
29730 mul += i * 3; \SpecialChar ~
29732 /* this multiplication remains */
29738 gint += j * 3;\SpecialChar ~
29740 /* this multiplication changed to addition */
29754 In addition to the operands each iCode contains information about the filename
29755 and line it corresponds to in the source file.
29756 The first field in the listing should be interpreted as follows:
29761 Filename(linenumber: iCode Execution sequence number : ICode hash table
29762 key : loop depth of the iCode).
29767 Then follows the human readable form of the ICode operation.
29768 Each operand of this triplet form can be of three basic types a) compiler
29769 generated temporary b) user defined variable c) a constant value.
29770 Note that local variables and parameters are replaced by compiler generated
29773 \begin_inset LatexCommand \index{Live range analysis}
29777 are computed only for temporaries (i.e.
29778 live ranges are not computed for global variables).
29780 \begin_inset LatexCommand \index{Register allocation}
29784 are allocated for temporaries only.
29785 Operands are formatted in the following manner:
29790 Operand Name [lr live-from : live-to ] { type information } [ registers
29796 As mentioned earlier the live ranges are computed in terms of the execution
29797 sequence number of the iCodes, for example
29799 the iTemp0 is live from (i.e.
29800 first defined in iCode with execution sequence number 3, and is last used
29801 in the iCode with sequence number 5).
29802 For induction variables such as iTemp21 the live range computation extends
29803 the lifetime from the start to the end of the loop.
29805 The register allocator used the live range information to allocate registers,
29806 the same registers may be used for different temporaries if their live
29807 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
29808 iTemp17 since their live ranges do not overlap.
29809 In addition the allocator also takes into consideration the type and usage
29810 of a temporary, for example itemp6 is a pointer to near space and is used
29811 as to fetch data from (i.e.
29812 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
29813 Some short lived temporaries are allocated to special registers which have
29814 meaning to the code generator e.g.
29815 iTemp13 is allocated to a pseudo register CC which tells the back end that
29816 the temporary is used only for a conditional jump the code generation makes
29817 use of this information to optimize a compare and jump ICode.
29819 There are several loop optimizations
29820 \begin_inset LatexCommand \index{Loop optimization}
29824 performed by the compiler.
29825 It can detect induction variables iTemp21(i) and iTemp23(j).
29826 Also note the compiler does selective strength reduction
29827 \begin_inset LatexCommand \index{Strength reduction}
29832 the multiplication of an induction variable in line 18 (gint = j * 3) is
29833 changed to addition, a new temporary iTemp17 is allocated and assigned
29834 a initial value, a constant 3 is then added for each iteration of the loop.
29835 The compiler does not change the multiplication
29836 \begin_inset LatexCommand \index{Multiplication}
29840 in line 17 however since the processor does support an 8 * 8 bit multiplication.
29842 Note the dead code elimination
29843 \begin_inset LatexCommand \index{Dead-code elimination}
29847 optimization eliminated the dead assignments in line 7 & 8 to I and sum
29855 Sample.c (5:1:0:0) _entry($9) :
29860 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
29865 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
29870 Sample.c(11:4:53:0) preHeaderLbl0($11) :
29875 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
29881 Sample.c(11:6:5:1) _whilecontinue_0($1) :
29886 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
29892 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
29897 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
29903 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
29909 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
29915 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
29921 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
29922 * int}[r0] + 0x2 {short}
29927 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
29932 Sample.c(11:17:21:0)_whilebreak_0($3) :
29937 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29942 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29947 Sample.c(15:20:54:0)preHeaderLbl1($13) :
29952 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29957 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29962 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29967 Sample.c(15:24:26:1)_forcond_0($4) :
29972 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
29978 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29983 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
29984 + ITemp21 [lr21:38]{short}[r4]
29989 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
29995 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
29996 + iTemp15 [lr29:30]{short}[r1]
30001 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
30007 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
30013 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
30019 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
30025 Sample.c(19:38:47:1) goto _forcond_0($4)
30030 Sample.c(19:39:48:0)_forbreak_0($7) :
30035 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
30036 + ITemp11 [lr19:40]{short}[r3]
30041 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
30046 Sample.c(20:42:51:0)_return($8) :
30051 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
30057 Finally the code generated for this function:
30098 ; ----------------------------------------------
30103 ; function function
30108 ; ----------------------------------------------
30118 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
30130 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
30142 ;_whilecontinue_0($1) :
30152 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
30157 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
30216 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
30235 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
30282 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
30322 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
30348 ; iTemp6 [lr5:16]{_near * int}[r0] =
30353 ; iTemp6 [lr5:16]{_near * int}[r0] +
30370 ; goto _whilecontinue_0($1)
30382 ; _whilebreak_0($3) :
30392 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
30404 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
30416 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
30428 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
30447 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
30476 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
30481 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
30526 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
30531 ; iTemp21 [lr21:38]{short}[r4]
30557 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
30590 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
30595 ; iTemp15 [lr29:30]{short}[r1]
30614 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
30661 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
30708 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
30720 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
30734 cjne r5,#0xff,00104$
30746 ; goto _forcond_0($4)
30758 ; _forbreak_0($7) :
30768 ; ret iTemp24 [lr40:41]{short}
30811 A few words about basic block successors, predecessors and dominators
30814 Successors are basic blocks
30815 \begin_inset LatexCommand \index{Basic blocks}
30819 that might execute after this basic block.
30821 Predecessors are basic blocks that might execute before reaching this basic
30824 Dominators are basic blocks that WILL execute before reaching this basic
30858 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
30861 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
30864 c) domVect of [BB4] = BB1 ...
30865 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
30873 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
30883 Thanks to all the other volunteer developers who have helped with coding,
30884 testing, web-page creation, distribution sets, etc.
30885 You know who you are :-)
30892 This document was initially written by Sandeep Dutta
30895 All product names mentioned herein may be trademarks
30896 \begin_inset LatexCommand \index{Trademarks}
30900 of their respective companies.
30907 To avoid confusion, the installation and building options for SDCC itself
30908 (chapter 2) are not part of the index.
30912 \begin_inset LatexCommand \printindex{}