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="center" valignment="top" leftline="true" rightline="true" width="0">
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">
508 <row topline="true" bottomline="true">
509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
554 The compiler also allows
556 inline assembler code
558 to be embedded anywhere in a function.
559 In addition, routines developed in assembly can also be called.
563 SDCC also provides an option (-
573 -cyclomatic) to report the relative complexity of a function.
574 These functions can then be further optimized, or hand coded in assembly
580 SDCC also comes with a companion source level debugger SDCDB, the debugger
581 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.<
585 The latest version can be downloaded from
586 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
596 Please note: the compiler will probably always be some steps ahead of this
601 \begin_inset LatexCommand \index{Status of documentation}
611 Obviously this has pros and cons
620 All packages used in this compiler system are
628 ; source code for all the sub-packages (pre-processor, assemblers, linkers
629 etc) is distributed with the package.
630 This documentation is maintained using a freeware word processor (LyX).
632 This program is free software; you can redistribute it and/or modify it
633 under the terms of the GNU General Public License
634 \begin_inset LatexCommand \index{GNU General Public License, GPL}
638 as published by the Free Software Foundation; either version 2, or (at
639 your option) any later version.
640 This program is distributed in the hope that it will be useful, but WITHOUT
641 ANY WARRANTY; without even the implied warranty
642 \begin_inset LatexCommand \index{warranty}
646 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
647 See the GNU General Public License for more details.
648 You should have received a copy of the GNU General Public License along
649 with this program; if not, write to the Free Software Foundation, 59 Temple
650 Place - Suite 330, Boston, MA 02111-1307, USA.
651 In other words, you are welcome to use, share and improve this program.
652 You are forbidden to forbid anyone else to use, share and improve what
654 Help stamp out software-hoarding!
657 Typographic conventions
658 \begin_inset LatexCommand \index{Typographic conventions}
665 Throughout this manual, we will use the following convention.
666 Commands you have to type in are printed in
674 Code samples are printed in
679 Interesting items and new terms are printed in
685 \begin_inset LatexCommand \label{sec:Compatibility-with-previous}
689 with previous versions
690 \begin_inset LatexCommand \index{Compatibility with previous versions}
697 This version has numerous bug fixes compared with the previous version.
698 But we also introduced some incompatibilities with older versions.
699 Not just for the fun of it, but to make the compiler more stable, efficient
701 \begin_inset LatexCommand \index{ANSI-compliance}
706 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
710 for ANSI-Compliance).
716 short is now equivalent to int (16 bits), it used to be equivalent to char
717 (8 bits) which is not ANSI compliant.
720 the default directory for gcc-builds where include, library and documentation
721 files are stored is now in /usr/local/share.
724 char type parameters to vararg
725 \begin_inset LatexCommand \index{vararg, va\_arg}
729 functions are casted to int unless explicitly casted
730 \begin_inset Marginal
757 will push a as an int and as a char resp.
770 -regextend has been removed.
783 -noregparms has been removed.
796 -stack-after-data has been removed.
800 \begin_inset LatexCommand \index{bit}
805 \begin_inset LatexCommand \index{sbit}
810 \begin_inset LatexCommand \index{\_\_sbit}
814 types now consistently behave like the C99 _Bool type with respect to type
816 \begin_inset LatexCommand \index{type conversion}
821 \begin_inset LatexCommand \index{type promotion}
826 The most common incompatibility resulting from this change is related to
828 \begin_inset LatexCommand \index{Bit toggling}
843 \begin_inset LatexCommand \index{\~\/ Operator}
847 b; /* equivalent to b=1 instead of toggling b */
848 \begin_inset Marginal
863 b = !b; /* toggles b */
867 In previous versions, both forms would have toggled the bit.
872 <pending: more incompatibilities?>
878 What do you need before you start installation of SDCC? A computer, and
880 The preferred method of installation is to compile SDCC from source using
882 For Windows some pre-compiled binary distributions are available for your
884 You should have some experience with command line tools and compiler use.
890 The SDCC home page at
891 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
895 is a great place to find distribution sets.
896 You can also find links to the user mailing lists that offer help or discuss
897 SDCC with other SDCC users.
898 Web links to other SDCC related sites can also be found here.
899 This document can be found in the DOC directory of the source package as
901 A pdf version of this document is available at
902 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
907 Some of the other tools (simulator and assembler) included with SDCC contain
908 their own documentation and can be found in the source distribution.
909 If you want the latest unreleased software, the complete source package
910 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
914 Wishes for the future
917 There are (and always will be) some things that could be done.
918 Here are some I can think of:
925 char KernelFunction3(char p) at 0x340;
933 \begin_inset LatexCommand \index{code banking (limited support)}
943 If you can think of some more, please see the section
944 \begin_inset LatexCommand \ref{sub:Requesting-Features}
948 about filing feature requests
949 \begin_inset LatexCommand \index{Requesting features}
954 \begin_inset LatexCommand \index{Feature request}
964 \begin_inset LatexCommand \index{Installation}
971 For most users it is sufficient to skip to either section
972 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
977 \begin_inset LatexCommand \ref{sub:Windows-Install}
982 More detailed instructions follow below.
986 \begin_inset LatexCommand \index{Options SDCC configuration}
993 The install paths, search paths and other options are defined when running
995 The defaults can be overridden by:
997 \labelwidthstring 00.00.0000
1009 -prefix see table below
1011 \labelwidthstring 00.00.0000
1023 -exec_prefix see table below
1025 \labelwidthstring 00.00.0000
1037 -bindir see table below
1039 \labelwidthstring 00.00.0000
1051 -datadir see table below
1053 \labelwidthstring 00.00.0000
1055 docdir environment variable, see table below
1057 \labelwidthstring 00.00.0000
1059 include_dir_suffix environment variable, see table below
1061 \labelwidthstring 00.00.0000
1063 lib_dir_suffix environment variable, see table below
1065 \labelwidthstring 00.00.0000
1067 sdccconf_h_dir_separator environment variable, either / or
1072 This character will only be used in sdccconf.h; don't forget it's a C-header,
1073 therefore a double-backslash is needed there.
1075 \labelwidthstring 00.00.0000
1087 -disable-mcs51-port Excludes the Intel mcs51 port
1089 \labelwidthstring 00.00.0000
1101 -disable-gbz80-port Excludes the Gameboy gbz80 port
1103 \labelwidthstring 00.00.0000
1115 -disable-z80-port Excludes the z80 port
1117 \labelwidthstring 00.00.0000
1129 -disable-avr-port Excludes the AVR port
1131 \labelwidthstring 00.00.0000
1143 -disable-ds390-port Excludes the DS390 port
1145 \labelwidthstring 00.00.0000
1157 -disable-hc08-port Excludes the HC08 port
1159 \labelwidthstring 00.00.0000
1171 -disable-pic-port Excludes the PIC port
1173 \labelwidthstring 00.00.0000
1185 -disable-xa51-port Excludes the XA51 port
1187 \labelwidthstring 00.00.0000
1199 -disable-ucsim Disables configuring and building of ucsim
1201 \labelwidthstring 00.00.0000
1213 -disable-device-lib Disables automatically building device libraries
1215 \labelwidthstring 00.00.0000
1227 -disable-packihx Disables building packihx
1229 \labelwidthstring 00.00.0000
1241 -enable-doc Build pdf, html and txt files from the lyx sources
1243 \labelwidthstring 00.00.0000
1255 -enable-libgc Use the Bohem memory allocator.
1256 Lower runtime footprint.
1259 Furthermore the environment variables CC, CFLAGS, ...
1260 the tools and their arguments can be influenced.
1261 Please see `configure -
1271 -help` and the man/info pages of `configure` for details.
1275 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1276 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1277 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1278 At the moment it's not possible to change the default settings (it was
1279 simply never required).
1283 These configure options are compiled into the binaries, and can only be
1284 changed by rerunning 'configure' and recompiling SDCC.
1285 The configure options are written in
1289 to distinguish them from run time environment variables (see section search
1295 \begin_inset Quotes sld
1299 \begin_inset Quotes srd
1302 are used by the SDCC team to build the official Win32 binaries.
1303 The SDCC team uses Mingw32 to build the official Windows binaries, because
1310 a gcc compiler and last but not least
1313 the binaries can be built by cross compiling on Sourceforge's compile farm.
1316 See the examples, how to pass the Win32 settings to 'configure'.
1317 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1318 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1329 \begin_inset Tabular
1330 <lyxtabular version="3" rows="8" columns="3">
1332 <column alignment="block" valignment="top" leftline="true" width="0in">
1333 <column alignment="block" valignment="top" leftline="true" width="0in">
1334 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1335 <row topline="true" bottomline="true">
1336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1361 <row topline="true">
1362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1391 <row topline="true">
1392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1412 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1423 <row topline="true">
1424 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1461 <row topline="true">
1462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1472 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1495 <row topline="true">
1496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1533 <row topline="true">
1534 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1552 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1561 <row topline="true" bottomline="true">
1562 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1580 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1598 'configure' also computes relative paths.
1599 This is needed for full relocatability of a binary package and to complete
1600 search paths (see section search paths below):
1606 \begin_inset Tabular
1607 <lyxtabular version="3" rows="4" columns="3">
1609 <column alignment="block" valignment="top" leftline="true" width="0in">
1610 <column alignment="block" valignment="top" leftline="true" width="0in">
1611 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1612 <row topline="true" bottomline="true">
1613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1638 <row topline="true" bottomline="true">
1639 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1666 <row bottomline="true">
1667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1685 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1694 <row bottomline="true">
1695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1705 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1746 \begin_inset Quotes srd
1750 \begin_inset Quotes srd
1764 \begin_inset Quotes srd
1768 \begin_inset Quotes srd
1796 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1805 \begin_inset Quotes srd
1808 i586-mingw32msvc-gcc
1809 \begin_inset Quotes srd
1813 \begin_inset Quotes srd
1816 i586-mingw32msvc-g++
1817 \begin_inset Quotes srd
1825 \begin_inset Quotes srd
1828 i586-mingw32msvc-ranlib
1829 \begin_inset Quotes srd
1837 \begin_inset Quotes srd
1840 i586-mingw32msvc-strip
1841 \begin_inset Quotes srd
1859 \begin_inset Quotes srd
1863 \begin_inset Quotes srd
1881 \begin_inset Quotes srd
1885 \begin_inset Quotes srd
1893 \begin_inset Quotes srd
1897 \begin_inset Quotes srd
1905 \begin_inset Quotes srd
1909 \begin_inset Quotes srd
1917 \begin_inset Quotes srd
1921 \begin_inset Quotes srd
1928 sdccconf_h_dir_separator=
1929 \begin_inset Quotes srd
1941 \begin_inset Quotes srd
1972 -host=i586-mingw32msvc
1986 -build=unknown-unknown-linux-gnu
1990 \begin_inset Quotes sld
1994 \begin_inset Quotes srd
1997 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
2016 \begin_inset Quotes srd
2020 \begin_inset Quotes srd
2038 \begin_inset Quotes srd
2042 \begin_inset Quotes srd
2050 \begin_inset Quotes srd
2054 \begin_inset Quotes srd
2062 \begin_inset Quotes srd
2066 \begin_inset Quotes srd
2074 \begin_inset Quotes srd
2078 \begin_inset Quotes srd
2085 sdccconf_h_dir_separator=
2086 \begin_inset Quotes srd
2098 \begin_inset Quotes srd
2106 \begin_inset Quotes srd
2110 \begin_inset Quotes srd
2118 \begin_inset Quotes srd
2122 \begin_inset Quotes srd
2128 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2139 -C' turns on caching, which gives a little bit extra speed.
2140 However if options are changed, it can be necessary to delete the config.cache
2145 \begin_inset LatexCommand \label{sub:Install-paths}
2150 \begin_inset LatexCommand \index{Install paths}
2156 \added_space_top medskip \align center
2158 \begin_inset Tabular
2159 <lyxtabular version="3" rows="5" columns="4">
2161 <column alignment="left" valignment="top" leftline="true" width="0">
2162 <column alignment="left" valignment="top" leftline="true" width="0">
2163 <column alignment="left" valignment="top" leftline="true" width="0">
2164 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2165 <row topline="true" bottomline="true">
2166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
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" rightline="true" usebox="none">
2207 <row topline="true">
2208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2247 <row topline="true">
2248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2263 $DATADIR/ $INCLUDE_DIR_SUFFIX
2266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2271 /usr/local/share/sdcc/include
2274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2287 <row topline="true">
2288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2303 $DATADIR/$LIB_DIR_SUFFIX
2306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2311 /usr/local/share/sdcc/lib
2314 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2327 <row topline="true" bottomline="true">
2328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2351 /usr/local/share/sdcc/doc
2354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2376 *compiler, preprocessor, assembler, and linker
2382 is auto-appended by the compiler, e.g.
2383 small, large, z80, ds390 etc
2386 The install paths can still be changed during `make install` with e.g.:
2389 make install prefix=$(HOME)/local/sdcc
2392 Of course this doesn't change the search paths compiled into the binaries.
2396 Moreover the install path can be changed by defining DESTDIR
2397 \begin_inset LatexCommand \index{DESTDIR}
2404 make install DESTDIR=$(HOME)/sdcc.rpm/
2407 Please note that DESTDIR must have a trailing slash!
2411 \begin_inset LatexCommand \label{sub:Search-Paths}
2416 \begin_inset LatexCommand \index{Search path}
2423 Some search paths or parts of them are determined by configure variables
2428 , see section above).
2429 Further search paths are determined by environment variables during runtime.
2432 The paths searched when running the compiler are as follows (the first catch
2438 Binary files (preprocessor, assembler and linker)
2444 \begin_inset Tabular
2445 <lyxtabular version="3" rows="4" columns="3">
2447 <column alignment="block" valignment="top" leftline="true" width="0in">
2448 <column alignment="block" valignment="top" leftline="true" width="0in">
2449 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2450 <row topline="true" bottomline="true">
2451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2476 <row topline="true">
2477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2506 <row topline="true">
2507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2512 Path of argv[0] (if available)
2515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2532 <row topline="true" bottomline="true">
2533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2574 \begin_inset Tabular
2575 <lyxtabular version="3" rows="6" columns="3">
2577 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2578 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2579 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2580 <row topline="true" bottomline="true">
2581 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2589 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2597 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2606 <row topline="true">
2607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2662 <row topline="true">
2663 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2688 <row topline="true">
2689 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2703 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2726 <row topline="true">
2727 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2795 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2808 <row topline="true" bottomline="true">
2809 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2825 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2830 /usr/local/share/sdcc/
2835 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2863 -nostdinc disables the last two search paths.
2873 With the exception of
2874 \begin_inset Quotes sld
2888 \begin_inset Quotes srd
2895 is auto-appended by the compiler (e.g.
2896 small, large, z80, ds390 etc.).
2903 \begin_inset Tabular
2904 <lyxtabular version="3" rows="6" columns="3">
2906 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2907 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2908 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2909 <row topline="true" bottomline="true">
2910 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2918 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2926 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2935 <row topline="true">
2936 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2954 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2991 <row topline="true">
2992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3004 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3016 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3031 <row topline="true">
3032 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3043 $LIB_DIR_SUFFIX/<model>
3046 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3060 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3077 <row topline="true">
3078 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3093 $LIB_DIR_SUFFIX/<model>
3096 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3149 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3205 <row topline="true" bottomline="true">
3206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3215 $LIB_DIR_SUFFIX/<model>
3218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3223 /usr/local/share/sdcc/
3230 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3248 Don't delete any of the stray spaces in the table above without checking
3249 the HTML output (last line)!
3265 -nostdlib disables the last two search paths.
3269 \begin_inset LatexCommand \index{Building SDCC}
3276 Building SDCC on Linux
3277 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3286 Download the source package
3288 either from the SDCC Subversion repository or from the nightly snapshots
3290 , it will be named something like sdcc
3301 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3310 Bring up a command line terminal, such as xterm.
3315 Unpack the file using a command like:
3318 "tar -xvzf sdcc.src.tar.gz
3323 , this will create a sub-directory called sdcc with all of the sources.
3326 Change directory into the main SDCC directory, for example type:
3343 This configures the package for compilation on your system.
3359 All of the source packages will compile, this can take a while.
3375 This copies the binary executables, the include files, the libraries and
3376 the documentation to the install directories.
3377 Proceed with section
3378 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3385 Building SDCC on OSX 2.x
3388 Follow the instruction for Linux.
3392 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3393 )) fails to compile SDCC.
3394 Fortunately there's also gcc 2.9.x installed, which works fine.
3395 This compiler can be selected by running 'configure' with:
3398 ./configure CC=gcc2 CXX=g++2
3401 Cross compiling SDCC on Linux for Windows
3404 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3405 See section 'Configure Options'.
3408 Building SDCC using Cygwin and Mingw32
3411 For building and installing a Cygwin executable follow the instructions
3417 \begin_inset Quotes sld
3421 \begin_inset Quotes srd
3424 Win32-binary can be built, which will not need the Cygwin-DLL.
3425 For the necessary 'configure' options see section 'configure options' or
3426 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3430 In order to install Cygwin on Windows download setup.exe from
3431 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3437 \begin_inset Quotes sld
3440 default text file type
3441 \begin_inset Quotes srd
3445 \begin_inset Quotes sld
3449 \begin_inset Quotes srd
3452 and download/install at least the following packages.
3453 Some packages are selected by default, others will be automatically selected
3454 because of dependencies with the manually selected packages.
3455 Never deselect these packages!
3464 gcc ; version 3.x is fine, no need to use the old 2.9x
3467 binutils ; selected with gcc
3473 rxvt ; a nice console, which makes life much easier under windoze (see below)
3476 man ; not really needed for building SDCC, but you'll miss it sooner or
3480 less ; not really needed for building SDCC, but you'll miss it sooner or
3484 svn ; only if you use Subversion access
3487 If you want to develop something you'll need:
3490 python ; for the regression tests
3493 gdb ; the gnu debugger, together with the nice GUI
3494 \begin_inset Quotes sld
3498 \begin_inset Quotes srd
3504 openssh ; to access the CF or commit changes
3507 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3508 use autoconf-stable!
3511 rxvt is a nice console with history.
3512 Replace in your cygwin.bat the line
3531 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3534 -bg black -fg white -geometry 100x65 -e bash -
3547 Text selected with the mouse is automatically copied to the clipboard, pasting
3548 works with shift-insert.
3552 The other good tip is to make sure you have no //c/-style paths anywhere,
3553 use /cygdrive/c/ instead.
3554 Using // invokes a network lookup which is very slow.
3556 \begin_inset Quotes sld
3560 \begin_inset Quotes srd
3563 is too long, you can change it with e.g.
3569 SDCC sources use the unix line ending LF.
3570 Life is much easier, if you store the source tree on a drive which is mounted
3572 And use an editor which can handle LF-only line endings.
3573 Make sure not to commit files with windows line endings.
3574 The tabulator spacing
3575 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3579 used in the project is 8.
3580 Although a tabulator spacing of 8 is a sensible choice for programmers
3581 (it's a power of 2 and allows to display 8/16 bit signed variables without
3582 loosing columns) the plan is to move towards using only spaces in the source.
3585 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3590 Download the source package
3592 either from the SDCC Subversion repository or from the
3593 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3599 , it will be named something like sdcc
3606 SDCC is distributed with all the projects, workspaces, and files you need
3607 to build it using Visual C++ 6.0/NET (except for SDCDB and ucSim).
3608 The workspace name is 'sdcc.dsw'.
3609 Please note that as it is now, all the executables are created in a folder
3613 Once built you need to copy the executables from sdcc
3617 bin before running SDCC.
3622 WARNING: Visual studio is very picky with line terminations; it expects
3623 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3624 When using the Subversion repository it's easiest to configure the svn
3625 client to convert automatically for you.
3626 If however you are getting a message such as "This makefile was not generated
3627 by Developer Studio etc.
3629 \begin_inset Quotes srd
3632 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3633 need to convert the Unix style line endings to DOS style line endings.
3634 To do so you can use the
3635 \begin_inset Quotes sld
3639 \begin_inset Quotes srd
3642 utility freely available on the internet.
3643 Doug Hawkins reported in the sdcc-user list that this works:
3651 SDCC> unix2dos sdcc.dsw
3657 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3661 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3662 flex.exe, and gawk.exe.
3663 One good place to get them is
3664 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3672 Download the file UnxUtils
3673 \begin_inset LatexCommand \index{UnxUtils}
3678 Now you have to install the utilities and setup MSVC so it can locate the
3680 Here there are two alternatives (choose one!):
3687 a) Extract UnxUtils.zip to your C:
3689 hard disk PRESERVING the original paths, otherwise bison won't work.
3690 (If you are using WinZip make certain that 'Use folder names' is selected)
3694 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3695 in 'Show directories for:' select 'Executable files', and in the directories
3696 window add a new path: 'C:
3706 (As a side effect, you get a bunch of Unix utilities that could be useful,
3707 such as diff and patch.)
3714 This one avoids extracting a bunch of files you may not use, but requires
3719 a) Create a directory were to put the tools needed, or use a directory already
3727 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3728 to such directory WITHOUT preserving the original paths.
3729 (If you are using WinZip make certain that 'Use folder names' is not selected)
3733 c) Rename bison.exe to '_bison.exe'.
3737 d) Create a batch file 'bison.bat' in 'C:
3741 ' and add these lines:
3761 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3765 Steps 'c' and 'd' are needed because bison requires by default that the
3766 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3767 '/usr/local/share/' I think.
3768 So it is necessary to tell bison where those files are located if they
3769 are not in such directory.
3770 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3774 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3775 in 'Show directories for:' select 'Executable files', and in the directories
3776 window add a new path: 'c:
3779 Note that you can use any other path instead of 'c:
3781 util', even the path where the Visual C++ tools are, probably: 'C:
3785 Microsoft Visual Studio
3790 So you don't have to execute step 'e' :)
3794 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3795 the executables from sdcc
3799 bin, and you can compile using SDCC.
3802 Building SDCC Using Borland
3805 From the sdcc directory, run the command "make -f Makefile.bcc".
3806 This should regenerate all the .exe files in the bin directory except for
3810 If you modify any source files and need to rebuild, be aware that the dependenci
3811 es may not be correctly calculated.
3812 The safest option is to delete all .obj files and run the build again.
3813 From a Cygwin BASH prompt, this can easily be done with the command (be
3814 sure you are in the sdcc directory):
3824 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3826 ) -print -exec rm {}
3835 or on Windows NT/2000/XP from the command prompt with the command:
3842 del /s *.obj *.lib *.rul
3845 from the sdcc directory.
3848 Windows Install Using a ZIP Package
3851 Download the binary zip package from
3852 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3856 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3857 This should unpack to a group of sub-directories.
3858 An example directory structure after unpacking the mingw32 package is:
3863 bin for the executables, c:
3871 lib for the include and libraries.
3874 Adjust your environment variable PATH to include the location of the bin
3875 directory or start sdcc using the full path.
3878 Windows Install Using the Setup Program
3879 \begin_inset LatexCommand \label{sub:Windows-Install}
3886 Download the setup program
3888 sdcc-x.y.z-setup.exe
3890 for an official release from
3893 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3897 or a setup program for one of the snapshots
3899 sdcc-yyyymmdd-xxxx-setup.exe
3902 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3907 A windows typical installer will guide you through the installation process.
3911 \begin_inset LatexCommand \index{VPATH}
3918 SDCC supports the VPATH feature provided by configure and make.
3919 It allows to separate the source and build trees.
3951 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3952 # extract source to directory sdcc
3957 mkdir sdcc.build\SpecialChar ~
3966 # put output in sdcc.build
3976 ../sdcc/configure\SpecialChar ~
3984 # configure is doing all the magic!
3996 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
3997 It automagically computes the variables srcdir, top_srcdir and top_buildir
4003 the generated files will be in ~/sdcc.build, while the source files stay
4006 This is not only usefull for building different binaries, e.g.
4007 when cross compiling.
4008 It also gives you a much better overview in the source tree when all the
4009 generated files are not scattered between the source files.
4010 And the best thing is: if you want to change a file you can leave the original
4011 file untouched in the source directory.
4012 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4013 Makefile.dep` and `make`.
4018 will do the rest for you!
4021 Building the Documentation
4034 -enable-doc to the configure arguments to build the documentation together
4035 with all the other stuff.
4036 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4037 dvips and makeindex) to get the job done.
4038 Another possibility is to change to the doc directory and to type
4042 \begin_inset Quotes srd
4046 \begin_inset Quotes srd
4053 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4056 \begin_inset LatexCommand \url{http://www.lyx.org}
4060 as editor is straightforward.
4061 Prebuilt documentation in html and pdf format is available from
4062 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4069 Reading the Documentation
4070 \begin_inset LatexCommand \index{Documentation}
4077 Currently reading the document in pdf format is recommended, as for unknown
4078 reason the hyperlinks are working there whereas in the html version they
4085 If you should know why please drop us a note
4091 You'll find the pdf version
4092 \begin_inset LatexCommand \index{PDF version of this document}
4097 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4105 \begin_inset LatexCommand \index{HTML version of this document}
4110 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4116 This documentation is in some aspects different from a commercial documentation:
4120 It tries to document SDCC for several processor architectures in one document
4121 (commercially these probably would be separate documents/products).
4123 \begin_inset LatexCommand \index{Status of documentation}
4127 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4129 Z80, PIC14, PIC16 and HC08.
4132 There are many references pointing away from this documentation.
4133 Don't let this distract you.
4135 was a reference like
4136 \begin_inset LatexCommand \url{http://www.opencores.org}
4140 together with a statement
4141 \begin_inset Quotes sld
4144 some processors which are targetted by SDCC can be implemented in a
4161 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4166 \begin_inset Quotes srd
4170 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4175 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4180 \begin_inset Quotes sld
4183 have you ever heard of an open source compiler that compiles a subset of
4185 \begin_inset Quotes srd
4188 we expect you to have a quick look there and come back.
4189 If you read this you are on the right track.
4192 Some sections attribute more space to problems, restrictions and warnings
4193 than to the solution.
4196 The installation section and the section about the debugger is intimidating.
4199 There are still lots of typos and there are more different writing styles
4203 Testing the SDCC Compiler
4204 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4211 The first thing you should do after installing your SDCC compiler is to
4227 \begin_inset LatexCommand \index{version}
4234 at the prompt, and the program should run and output its version like:
4239 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4243 If it doesn't run, or gives a message about not finding sdcc program, then
4244 you need to check over your installation.
4245 Make sure that the sdcc bin directory is in your executable search path
4246 defined by the PATH environment setting (
4251 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4258 Install trouble-shooting for suggestions
4261 Make sure that the sdcc program is in the bin folder, if not perhaps something
4262 did not install correctly.
4270 is commonly installed as described in section
4271 \begin_inset Quotes sld
4274 Install and search paths
4275 \begin_inset Quotes srd
4284 Make sure the compiler works on a very simple example.
4285 Type in the following test.c program using your favorite
4311 Compile this using the following command:
4320 If all goes well, the compiler will generate a test.asm and test.rel file.
4321 Congratulations, you've just compiled your first program with SDCC.
4322 We used the -c option to tell SDCC not to link the generated code, just
4323 to keep things simple for this step.
4331 The next step is to try it with the linker.
4341 If all goes well the compiler will link with the libraries and produce
4342 a test.ihx output file.
4347 (no test.ihx, and the linker generates warnings), then the problem is most
4356 usr/local/share/sdcc/lib directory
4363 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4370 Install trouble-shooting for suggestions).
4378 The final test is to ensure
4386 header files and libraries.
4387 Edit test.c and change it to the following:
4404 strcpy(str1, "testing");
4411 Compile this by typing
4418 This should generate a test.ihx output file, and it should give no warnings
4419 such as not finding the string.h file.
4420 If it cannot find the string.h file, then the problem is that
4424 cannot find the /usr/local/share/sdcc/include directory
4431 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4438 Install trouble-shooting section for suggestions).
4456 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4460 to find exactly where SDCC is looking for the include and lib files.
4463 Install Trouble-shooting
4464 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4469 \begin_inset LatexCommand \index{Install trouble-shooting}
4476 If SDCC does not build correctly
4479 A thing to try is starting from scratch by unpacking the .tgz source package
4480 again in an empty directory.
4488 ./configure 2>&1 | tee configure.log
4502 make 2>&1 | tee make.log
4509 If anything goes wrong, you can review the log files to locate the problem.
4510 Or a relevant part of this can be attached to an email that could be helpful
4511 when requesting help from the mailing list.
4515 \begin_inset Quotes sld
4519 \begin_inset Quotes srd
4526 \begin_inset Quotes sld
4530 \begin_inset Quotes srd
4533 command is a script that analyzes your system and performs some configuration
4534 to ensure the source package compiles on your system.
4535 It will take a few minutes to run, and will compile a few tests to determine
4536 what compiler features are installed.
4540 \begin_inset Quotes sld
4544 \begin_inset Quotes srd
4550 This runs the GNU make tool, which automatically compiles all the source
4551 packages into the final installed binary executables.
4555 \begin_inset Quotes sld
4559 \begin_inset Quotes erd
4565 This will install the compiler, other executables libraries and include
4566 files into the appropriate directories.
4568 \begin_inset LatexCommand \ref{sub:Install-paths}
4574 \begin_inset LatexCommand \ref{sub:Search-Paths}
4579 about install and search paths.
4581 On most systems you will need super-user privileges to do this.
4587 SDCC is not just a compiler, but a collection of tools by various developers.
4588 These include linkers, assemblers, simulators and other components.
4589 Here is a summary of some of the components.
4590 Note that the included simulator and assembler have separate documentation
4591 which you can find in the source package in their respective directories.
4592 As SDCC grows to include support for other processors, other packages from
4593 various developers are included and may have their own sets of documentation.
4597 You might want to look at the files which are installed in <installdir>.
4598 At the time of this writing, we find the following programs for gcc-builds:
4602 In <installdir>/bin:
4605 sdcc - The compiler.
4608 sdcpp - The C preprocessor.
4611 asx8051 - The assembler for 8051 type processors.
4618 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4621 aslink -The linker for 8051 type processors.
4628 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4631 s51 - The ucSim 8051 simulator.
4634 sdcdb - The source debugger.
4637 packihx - A tool to pack (compress) Intel hex files.
4640 In <installdir>/share/sdcc/include
4646 In <installdir>/share/sdcc/lib
4649 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4653 In <installdir>/share/sdcc/doc
4659 As development for other processors proceeds, this list will expand to include
4660 executables to support processors like AVR, PIC, etc.
4666 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4667 the assembler and linkage editor.
4670 sdcpp - The C-Preprocessor
4674 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4678 is a modified version of the GNU preprocessor.
4679 The C preprocessor is used to pull in #include sources, process #ifdef
4680 statements, #defines and so on.
4691 - The Assemblers and Linkage Editors
4694 This is retargettable assembler & linkage editor, it was developed by Alan
4696 John Hartman created the version for 8051, and I (Sandeep) have made some
4697 enhancements and bug fixes for it to work properly with SDCC.
4704 \begin_inset LatexCommand \index{s51}
4708 is a freeware, opensource simulator developed by Daniel Drotos.
4709 The simulator is built as part of the build process.
4710 For more information visit Daniel's web site at:
4711 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4716 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4720 sdcdb - Source Level Debugger
4724 \begin_inset LatexCommand \index{SDCDB (debugger)}
4728 is the companion source level debugger.
4729 More about SDCDB in section
4730 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4735 The current version of the debugger uses Daniel's Simulator S51
4736 \begin_inset LatexCommand \index{s51}
4740 , but can be easily changed to use other simulators.
4749 Single Source File Projects
4752 For single source file 8051 projects the process is very simple.
4753 Compile your programs with the following command
4756 "sdcc sourcefile.c".
4760 This will compile, assemble and link your source file.
4761 Output files are as follows:
4765 \begin_inset LatexCommand \index{<file>.asm}
4770 \begin_inset LatexCommand \index{Assembler source}
4774 file created by the compiler
4778 \begin_inset LatexCommand \index{<file>.lst}
4783 \begin_inset LatexCommand \index{Assembler listing}
4787 file created by the Assembler
4791 \begin_inset LatexCommand \index{<file>.rst}
4796 \begin_inset LatexCommand \index{Assembler listing}
4800 file updated with linkedit information, created by linkage editor
4804 \begin_inset LatexCommand \index{<file>.sym}
4809 \begin_inset LatexCommand \index{Symbol listing}
4813 for the sourcefile, created by the assembler
4817 \begin_inset LatexCommand \index{<file>.rel}
4822 \begin_inset LatexCommand \index{<file>.o}
4827 \begin_inset LatexCommand \index{Object file}
4831 created by the assembler, input to Linkage editor
4835 \begin_inset LatexCommand \index{<file>.map}
4840 \begin_inset LatexCommand \index{Memory map}
4844 for the load module, created by the Linker
4848 \begin_inset LatexCommand \index{<file>.mem}
4852 - A file with a summary of the memory usage
4856 \begin_inset LatexCommand \index{<file>.ihx}
4860 - The load module in Intel hex format
4861 \begin_inset LatexCommand \index{Intel hex format}
4865 (you can select the Motorola S19 format
4866 \begin_inset LatexCommand \index{Motorola S19 format}
4881 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4886 If you need another format you might want to use
4893 \begin_inset LatexCommand \index{objdump (tool)}
4904 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4909 Both formats are documented in the documentation of srecord
4910 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4918 \begin_inset LatexCommand \index{<file>.adb}
4922 - An intermediate file containing debug information needed to create the
4934 \begin_inset LatexCommand \index{-\/-debug}
4942 \begin_inset LatexCommand \index{<file>.cdb}
4946 - An optional file (with -
4956 -debug) containing debug information.
4957 The format is documented in cdbfileformat.pdf
4962 \begin_inset LatexCommand \index{<file> (no extension)}
4966 An optional AOMF or AOMF51
4967 \begin_inset LatexCommand \index{AOMF, AOMF51}
4972 \begin_inset LatexCommand \label{OMF file}
4976 file containing debug information (generated with option -
5003 ormat is commonly used by third party tools (debuggers
5004 \begin_inset LatexCommand \index{Debugger}
5008 , simulators, emulators)
5012 \begin_inset LatexCommand \index{<file>.dump*}
5016 - Dump file to debug the compiler it self (generated with option -
5026 -dumpall) (see section
5027 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5033 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5039 \begin_inset Quotes sld
5042 Anatomy of the compiler
5043 \begin_inset Quotes srd
5049 Postprocessing the Intel Hex
5050 \begin_inset LatexCommand \index{Intel hex format}
5058 \begin_inset LatexCommand \index{<file>.ihx}
5062 which is generated by SDCC might include lines of varying length and the
5063 addresses within the file are not guaranteed to be strictly ascending.
5064 If your toolchain or a bootloader does not like this you can use the tool
5070 \begin_inset LatexCommand \index{packihx (tool)}
5074 which is part of the SDCC distribution:
5081 packihx sourcefile.ihx >sourcefile.hex
5088 The separately available
5093 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5097 package additionally allows to set undefined locations to a predefined
5098 value, to insert checksums of various flavours (crc, add, xor) and to perform
5099 other manipulations (convert, split, crop, offset, ...).
5107 srec_cat\SpecialChar ~
5109 sourcefile.ihx -intel\SpecialChar ~
5112 -fill 0xff 0x0000 0x8000\SpecialChar ~
5115 -o sourcefile.hex -intel
5122 The srecord package is available at
5123 \begin_inset LatexCommand \url{http://sf.net/projects/srecord}
5130 Projects with Multiple Source Files
5133 SDCC can compile only ONE file at a time.
5134 Let us for example assume that you have a project containing the following
5139 foo1.c (contains some functions)
5141 foo2.c (contains some more functions)
5143 foomain.c (contains more functions and the function main)
5151 The first two files will need to be compiled separately with the commands:
5183 Then compile the source file containing the
5188 \begin_inset LatexCommand \index{Linker}
5192 the files together with the following command:
5200 foomain.c\SpecialChar ~
5201 foo1.rel\SpecialChar ~
5206 \begin_inset LatexCommand \index{<file>.rel}
5218 can be separately compiled as well:
5229 sdcc foomain.rel foo1.rel foo2.rel
5236 The file containing the
5251 file specified in the command line, since the linkage editor processes
5252 file in the order they are presented to it.
5253 The linker is invoked from SDCC using a script file with extension .lnk
5254 \begin_inset LatexCommand \index{<file>.lnk}
5259 You can view this file to troubleshoot linking problems such as those arising
5260 from missing libraries.
5263 Projects with Additional Libraries
5264 \begin_inset LatexCommand \index{Libraries}
5271 Some reusable routines may be compiled into a library, see the documentation
5272 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5276 \begin_inset LatexCommand \index{<file>.lib}
5283 Libraries created in this manner can be included in the command line.
5284 Make sure you include the -L <library-path> option to tell the linker where
5285 to look for these files if they are not in the current directory.
5286 Here is an example, assuming you have the source file
5298 (if that is not the same as your current project):
5305 sdcc foomain.c foolib.lib -L mylib
5316 must be an absolute path name.
5320 The most efficient way to use libraries is to keep separate modules in separate
5322 The lib file now should name all the modules.rel
5323 \begin_inset LatexCommand \index{<file>.rel}
5328 For an example see the standard library file
5332 in the directory <installdir>/share/lib/small.
5335 Using sdcclib to Create and Manage Libraries
5336 \begin_inset LatexCommand \index{sdcclib}
5343 Alternatively, instead of having a .rel file for each entry on the library
5344 file as described in the preceding section, sdcclib can be used to embed
5345 all the modules belonging to such library in the library file itself.
5346 This results in a larger library file, but it greatly reduces the number
5347 of disk files accessed by the linker.
5348 Additionally, the packed library file contains an index of all include
5349 modules and symbols that significantly speeds up the linking process.
5350 To display a list of options supported by sdcclib type:
5359 \begin_inset LatexCommand \index{sdcclib}
5370 To create a new library file, start by compiling all the required modules.
5408 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5410 The next step is to add the .rel files to the library file:
5418 sdcclib libint.lib _divsint.rel
5421 \begin_inset LatexCommand \index{sdcclib}
5431 sdcclib libint.lib _divuint.rel
5437 sdcclib libint.lib _modsint.rel
5443 sdcclib libint.lib _moduint.rel
5449 sdcclib libint.lib _mulint.rel
5456 If the file already exists in the library, it will be replaced.
5457 To see what modules and symbols are included in the library, options -s
5458 and -m are available.
5466 sdcclib -s libint.lib
5469 \begin_inset LatexCommand \index{sdcclib}
5578 \added_space_bottom bigskip
5579 If the source files are compiled using -
5590 \begin_inset LatexCommand \index{-\/-debug}
5594 , the corresponding debug information file .adb will be include in the library
5596 The library files created with sdcclib are plain text files, so they can
5597 be viewed with a text editor.
5598 It is not recomended to modify a library file created with sdcclib using
5599 a text editor, as there are file indexes numbers located accross the file
5600 used by the linker to quickly locate the required module to link.
5601 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5602 it can be safely deleted, since all the information required for linking
5603 is embedded in the library file itself.
5604 Library files created using sdcclib are used as described in the preceding
5608 Command Line Options
5609 \begin_inset LatexCommand \index{Command Line Options}
5616 Processor Selection Options
5617 \begin_inset LatexCommand \index{Options processor selection}
5622 \begin_inset LatexCommand \index{Processor selection options}
5628 \labelwidthstring 00.00.0000
5633 \begin_inset LatexCommand \index{-mmcs51}
5639 Generate code for the Intel MCS51
5640 \begin_inset LatexCommand \index{MCS51}
5644 family of processors.
5645 This is the default processor target.
5647 \labelwidthstring 00.00.0000
5652 \begin_inset LatexCommand \index{-mds390}
5658 Generate code for the Dallas DS80C390
5659 \begin_inset LatexCommand \index{DS80C390}
5665 \labelwidthstring 00.00.0000
5670 \begin_inset LatexCommand \index{-mds400}
5676 Generate code for the Dallas DS80C400
5677 \begin_inset LatexCommand \index{DS80C400}
5683 \labelwidthstring 00.00.0000
5688 \begin_inset LatexCommand \index{-mhc08}
5694 Generate code for the Freescale/Motorola HC08
5695 \begin_inset LatexCommand \index{HC08}
5699 family of processors.
5701 \labelwidthstring 00.00.0000
5706 \begin_inset LatexCommand \index{-mz80}
5712 Generate code for the Zilog Z80
5713 \begin_inset LatexCommand \index{Z80}
5717 family of processors.
5719 \labelwidthstring 00.00.0000
5724 \begin_inset LatexCommand \index{-mgbz80}
5730 Generate code for the GameBoy Z80
5731 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5735 processor (Not actively maintained).
5737 \labelwidthstring 00.00.0000
5742 \begin_inset LatexCommand \index{-mavr}
5748 Generate code for the Atmel AVR
5749 \begin_inset LatexCommand \index{AVR}
5753 processor (In development, not complete).
5754 AVR users should probably have a look at winavr
5755 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5760 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5767 I think it is fair to direct users there for now.
5768 Open source is also about avoiding unnecessary work .
5769 But I didn't find the 'official' link.
5771 \labelwidthstring 00.00.0000
5776 \begin_inset LatexCommand \index{-mpic14}
5782 Generate code for the Microchip PIC 14
5783 \begin_inset LatexCommand \index{PIC14}
5787 -bit processors (p16f84 and variants.
5788 In development, not complete).
5791 p16f627 p16f628 p16f84 p16f873 p16f877?
5793 \labelwidthstring 00.00.0000
5798 \begin_inset LatexCommand \index{-mpic16}
5804 Generate code for the Microchip PIC 16
5805 \begin_inset LatexCommand \index{PIC16}
5809 -bit processors (p18f452 and variants.
5810 In development, not complete).
5812 \labelwidthstring 00.00.0000
5818 Generate code for the Toshiba TLCS-900H
5819 \begin_inset LatexCommand \index{TLCS-900H}
5823 processor (Not maintained, not complete).
5825 \added_space_bottom bigskip \labelwidthstring 00.00.0000
5830 \begin_inset LatexCommand \index{-mxa51}
5836 Generate code for the Phillips XA51
5837 \begin_inset LatexCommand \index{XA51}
5841 processor (Not maintained, not complete).
5844 Preprocessor Options
5845 \begin_inset LatexCommand \index{Options preprocessor}
5850 \begin_inset LatexCommand \index{Preprocessor options}
5855 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5861 \labelwidthstring 00.00.0000
5866 \begin_inset LatexCommand \index{-I<path>}
5872 The additional location where the pre processor will look for <..h> or
5873 \begin_inset Quotes eld
5877 \begin_inset Quotes erd
5882 \labelwidthstring 00.00.0000
5887 \begin_inset LatexCommand \index{-D<macro[=value]>}
5893 Command line definition of macros.
5894 Passed to the preprocessor.
5896 \labelwidthstring 00.00.0000
5901 \begin_inset LatexCommand \index{-M}
5907 Tell the preprocessor to output a rule suitable for make describing the
5908 dependencies of each object file.
5909 For each source file, the preprocessor outputs one make-rule whose target
5910 is the object file name for that source file and whose dependencies are
5911 all the files `#include'd in it.
5912 This rule may be a single line or may be continued with `
5914 '-newline if it is long.
5915 The list of rules is printed on standard output instead of the preprocessed
5918 \begin_inset LatexCommand \index{-E}
5924 \labelwidthstring 00.00.0000
5929 \begin_inset LatexCommand \index{-C}
5935 Tell the preprocessor not to discard comments.
5936 Used with the `-E' option.
5938 \labelwidthstring 00.00.0000
5943 \begin_inset LatexCommand \index{-MM}
5954 Like `-M' but the output mentions only the user header files included with
5956 \begin_inset Quotes eld
5960 System header files included with `#include <file>' are omitted.
5962 \labelwidthstring 00.00.0000
5967 \begin_inset LatexCommand \index{-Aquestion(answer)}
5973 Assert the answer answer for question, in case it is tested with a preprocessor
5974 conditional such as `#if #question(answer)'.
5975 `-A-' disables the standard assertions that normally describe the target
5978 \labelwidthstring 00.00.0000
5983 \begin_inset LatexCommand \index{-Umacro}
5989 Undefine macro macro.
5990 `-U' options are evaluated after all `-D' options, but before any `-include'
5991 and `-imacros' options.
5993 \labelwidthstring 00.00.0000
5998 \begin_inset LatexCommand \index{-dM}
6004 Tell the preprocessor to output only a list of the macro definitions that
6005 are in effect at the end of preprocessing.
6006 Used with the `-E' option.
6008 \labelwidthstring 00.00.0000
6013 \begin_inset LatexCommand \index{-dD}
6019 Tell the preprocessor to pass all macro definitions into the output, in
6020 their proper sequence in the rest of the output.
6022 \labelwidthstring 00.00.0000
6027 \begin_inset LatexCommand \index{-dN}
6038 Like `-dD' except that the macro arguments and contents are omitted.
6039 Only `#define name' is included in the output.
6041 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6046 preprocessorOption[,preprocessorOption]
6049 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
6054 Pass the preprocessorOption to the preprocessor
6059 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
6064 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
6065 Collection (gcc), if you need more dedicated options please refer to the
6067 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
6075 \begin_inset LatexCommand \index{Options linker}
6080 \begin_inset LatexCommand \index{Linker options}
6086 \labelwidthstring 00.00.0000
6106 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6111 \begin_inset LatexCommand \index{-L -\/-lib-path}
6118 <absolute path to additional libraries> This option is passed to the linkage
6119 editor's additional libraries
6120 \begin_inset LatexCommand \index{Libraries}
6125 The path name must be absolute.
6126 Additional library files may be specified in the command line.
6127 See section Compiling programs for more details.
6129 \labelwidthstring 00.00.0000
6146 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6151 <Value> The start location of the external ram
6152 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6156 , default value is 0.
6157 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6167 -xram-loc 0x8000 or -
6179 \labelwidthstring 00.00.0000
6196 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6201 <Value> The start location of the code
6202 \begin_inset LatexCommand \index{code}
6206 segment, default value 0.
6207 Note when this option is used the interrupt vector table is also relocated
6208 to the given address.
6209 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6219 -code-loc 0x8000 or -
6231 \labelwidthstring 00.00.0000
6248 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6253 <Value> By default the stack
6254 \begin_inset LatexCommand \index{stack}
6258 is placed after the data segment.
6259 Using this option the stack can be placed anywhere in the internal memory
6261 The value entered can be in Hexadecimal or Decimal format, e.g.
6272 -stack-loc 0x20 or -
6283 Since the sp register is incremented before a push or call, the initial
6284 sp will be set to one byte prior the provided value.
6285 The provided value should not overlap any other memory areas such as used
6286 register banks or the data segment and with enough space for the current
6304 \begin_inset LatexCommand \index{-\/-pack-iram}
6308 option (which is now a default setting) will override this setting, so
6309 you should also specify the
6325 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6329 option if you need to manually place the stack.
6331 \labelwidthstring 00.00.0000
6348 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6353 <Value> By default the external stack
6354 \begin_inset LatexCommand \index{xstack}
6358 is placed after the pdata
6359 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
6364 Using this option the xstack can be placed anywhere in the external memory
6366 The value entered can be in Hexadecimal or Decimal format, e.g.
6377 -xstack-loc 0x8000 or -
6388 The provided value should not overlap any other memory areas such as the
6389 pdata or xdata segment and with enough space for the current application.
6391 \labelwidthstring 00.00.0000
6408 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6413 <Value> The start location of the internal ram data
6414 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6419 The value entered can be in Hexadecimal or Decimal format, eg.
6441 (By default, the start location of the internal ram data segment is set
6442 as low as possible in memory, taking into account the used register banks
6443 and the bit segment at address 0x20.
6444 For example if register banks 0 and 1 are used without bit variables, the
6445 data segment will be set, if -
6455 -data-loc is not used, to location 0x10.)
6457 \labelwidthstring 00.00.0000
6474 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6479 <Value> The start location of the indirectly addressable internal ram
6480 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6484 of the 8051, default value is 0x80.
6485 The value entered can be in Hexadecimal or Decimal format, eg.
6496 -idata-loc 0x88 or -
6508 \labelwidthstring 00.00.0000
6525 <Value> The start location of the bit
6526 \begin_inset LatexCommand \index{bit}
6530 addressable internal ram of the 8051.
6536 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6539 \labelwidthstring 00.00.0000
6554 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6563 The linker output (final object code) is in Intel Hex format.
6564 \begin_inset LatexCommand \index{Intel hex format}
6568 This is the default option.
6569 The format itself is documented in the documentation of srecord
6570 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6576 \labelwidthstring 00.00.0000
6591 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6600 The linker output (final object code) is in Motorola S19 format
6601 \begin_inset LatexCommand \index{Motorola S19 format}
6606 The format itself is documented in the documentation of srecord.
6608 \labelwidthstring 00.00.0000
6623 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6628 \begin_inset LatexCommand \index{HC08!Options!-\/-out-fmt-elf}
6637 The linker output (final object code) is in ELF format
6638 \begin_inset LatexCommand \index{ELF format}
6643 (Currently only supported for the HC08
6644 \begin_inset LatexCommand \index{HC08}
6650 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6655 linkOption[,linkOption]
6658 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6663 Pass the linkOption to the linker.
6664 If a bootloader is used an option like
6665 \begin_inset Quotes sld
6670 \begin_inset Quotes srd
6673 would be typical to set the start of the code segment.
6674 See also #pragma constseg and #pragma codeseg in section
6675 \begin_inset LatexCommand \ref{sec:Pragmas}
6680 File sdcc/as/doc/asxhtm.html has more on linker options.
6684 \begin_inset LatexCommand \index{Options MCS51}
6689 \begin_inset LatexCommand \index{MCS51 options}
6695 \labelwidthstring 00.00.0000
6710 \begin_inset LatexCommand \index{-\/-model-small}
6721 Generate code for Small Model programs, see section Memory Models for more
6723 This is the default model.
6725 \labelwidthstring 00.00.0000
6740 \begin_inset LatexCommand \index{-\/-model-medium}
6746 Generate code for Medium model programs, see section Memory Models for
6748 If this option is used all source files in the project have to be compiled
6750 It must also be used when invoking the linker.
6752 \labelwidthstring 00.00.0000
6767 \begin_inset LatexCommand \index{-\/-model-large}
6773 Generate code for Large model programs, see section Memory Models for more
6775 If this option is used all source files in the project have to be compiled
6777 It must also be used when invoking the linker.
6779 \labelwidthstring 00.00.0000
6794 \begin_inset LatexCommand \index{-\/-xstack}
6800 Uses a pseudo stack in the pdata
6801 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
6805 area (usually the first 256 bytes in the external ram) for allocating variables
6806 and passing parameters.
6808 \begin_inset LatexCommand \ref{sub:External-Stack}
6813 External Stack for more details.
6815 \labelwidthstring 00.00.0000
6833 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6837 Causes the linker to check if the internal ram usage is within limits of
6840 \labelwidthstring 00.00.0000
6858 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6862 Causes the linker to check if the external ram usage is within limits of
6865 \labelwidthstring 00.00.0000
6883 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6887 Causes the linker to check if the code memory usage is within limits of
6890 \labelwidthstring 00.00.0000
6908 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6912 Causes the linker to check if there is at minimum <Value> bytes for stack.
6914 \labelwidthstring 00.00.0000
6932 \begin_inset LatexCommand \index{-\/-pack-iram}
6936 Causes the linker to use unused register banks for data variables and pack
6937 data, idata and stack together.
6938 This is the default now.
6940 \added_space_bottom bigskip \labelwidthstring 00.00.0000
6958 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6962 Causes the linker to use old style for allocating memory areas.
6965 DS390 / DS400 Options
6966 \begin_inset LatexCommand \index{Options DS390}
6971 \begin_inset LatexCommand \index{DS390}
6977 \labelwidthstring 00.00.0000
6994 \begin_inset LatexCommand \index{DS390!Options!-\/-model-flat24}
7004 Generate 24-bit flat mode code.
7005 This is the one and only that the ds390 code generator supports right now
7006 and is default when using
7011 See section Memory Models for more details.
7013 \labelwidthstring 00.00.0000
7028 \begin_inset LatexCommand \index{DS390!Options!-\/-protect-sp-update}
7034 disable interrupts during ESP:SP updates.
7036 \labelwidthstring 00.00.0000
7053 \begin_inset LatexCommand \index{DS390!Options!-\/-stack-10bit}
7057 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
7058 This is the one and only that the ds390 code generator supports right now
7059 and is default when using
7064 In this mode, the stack is located in the lower 1K of the internal RAM,
7065 which is mapped to 0x400000.
7066 Note that the support is incomplete, since it still uses a single byte
7067 as the stack pointer.
7068 This means that only the lower 256 bytes of the potential 1K stack space
7069 will actually be used.
7070 However, this does allow you to reclaim the precious 256 bytes of low RAM
7071 for use for the DATA and IDATA segments.
7072 The compiler will not generate any code to put the processor into 10 bit
7074 It is important to ensure that the processor is in this mode before calling
7075 any re-entrant functions compiled with this option.
7076 In principle, this should work with the
7089 \begin_inset LatexCommand \index{-\/-stack-auto}
7095 option, but that has not been tested.
7096 It is incompatible with the
7109 \begin_inset LatexCommand \index{-\/-xstack}
7116 It also only makes sense if the processor is in 24 bit contiguous addressing
7129 -model-flat24 option
7133 \labelwidthstring 00.00.0000
7148 \begin_inset LatexCommand \index{DS390!Options!-\/-stack-probe}
7154 insert call to function __stack_probe at each function prologue.
7156 \labelwidthstring 00.00.0000
7171 \begin_inset LatexCommand \index{DS390!Options!-\/-tini-libid}
7177 <nnnn> LibraryID used in -mTININative.
7180 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7195 \begin_inset LatexCommand \index{DS390!Options!-\/-use-accelerator}
7201 generate code for DS390 Arithmetic Accelerator.
7206 \begin_inset LatexCommand \index{Options Z80}
7211 \begin_inset LatexCommand \index{Z80}
7217 \labelwidthstring 00.00.0000
7234 \begin_inset LatexCommand \index{Z80!Options!-\/-callee-saves-bc}
7244 Force a called function to always save BC.
7246 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7263 \begin_inset LatexCommand \index{Z80!Options!-\/-no-std-crt0}
7267 When linking, skip the standard crt0.o object file.
7268 You must provide your own crt0.o for your system when linking.
7272 Optimization Options
7273 \begin_inset LatexCommand \index{Options optimization}
7278 \begin_inset LatexCommand \index{Optimization options}
7284 \labelwidthstring 00.00.0000
7299 \begin_inset LatexCommand \index{-\/-nogcse}
7305 Will not do global subexpression elimination, this option may be used when
7306 the compiler creates undesirably large stack/data spaces to store compiler
7316 \begin_inset LatexCommand \index{sloc (spill location)}
7321 A warning message will be generated when this happens and the compiler
7322 will indicate the number of extra bytes it allocated.
7323 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7325 \begin_inset LatexCommand \index{\#pragma nogcse}
7329 can be used to turn off global subexpression elimination
7330 \begin_inset LatexCommand \index{Subexpression elimination}
7334 for a given function only.
7336 \labelwidthstring 00.00.0000
7351 \begin_inset LatexCommand \index{-\/-noinvariant}
7357 Will not do loop invariant optimizations, this may be turned off for reasons
7358 explained for the previous option.
7359 For more details of loop optimizations performed see Loop Invariants in
7361 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7366 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7368 \begin_inset LatexCommand \index{\#pragma noinvariant}
7372 can be used to turn off invariant optimizations for a given function only.
7374 \labelwidthstring 00.00.0000
7389 \begin_inset LatexCommand \index{-\/-noinduction}
7395 Will not do loop induction optimizations, see section strength reduction
7397 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7399 \begin_inset LatexCommand \index{\#pragma noinduction}
7403 can be used to turn off induction optimizations for a given function only.
7405 \labelwidthstring 00.00.0000
7420 \begin_inset LatexCommand \index{-\/-nojtbound}
7431 Will not generate boundary condition check when switch statements
7432 \begin_inset LatexCommand \index{switch statement}
7436 are implemented using jump-tables.
7438 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7443 Switch Statements for more details.
7444 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7446 \begin_inset LatexCommand \index{\#pragma nojtbound}
7450 can be used to turn off boundary checking for jump tables for a given function
7453 \labelwidthstring 00.00.0000
7468 \begin_inset LatexCommand \index{-\/-noloopreverse}
7477 Will not do loop reversal
7478 \begin_inset LatexCommand \index{Loop reversing}
7484 \labelwidthstring 00.00.0000
7501 \begin_inset LatexCommand \index{-\/-nolabelopt }
7505 Will not optimize labels (makes the dumpfiles more readable).
7507 \labelwidthstring 00.00.0000
7522 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7528 Will not memcpy initialized data from code space into xdata space.
7529 This saves a few bytes in code space if you don't have initialized data
7530 \begin_inset LatexCommand \index{Variable initialization}
7536 \labelwidthstring 00.00.0000
7551 \begin_inset LatexCommand \index{-\/-nooverlay}
7557 The compiler will not overlay parameters and local variables of any function,
7558 see section Parameters and local variables for more details.
7560 \labelwidthstring 00.00.0000
7575 \begin_inset LatexCommand \index{-\/-no-peep}
7581 Disable peep-hole optimization with built-in rules.
7583 \labelwidthstring 00.00.0000
7600 \begin_inset LatexCommand \index{-\/-peep-file}
7605 <filename> This option can be used to use additional rules to be used by
7606 the peep hole optimizer.
7608 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7613 Peep Hole optimizations for details on how to write these rules.
7615 \labelwidthstring 00.00.0000
7630 \begin_inset LatexCommand \index{-\/-peep-asm}
7636 Pass the inline assembler code through the peep hole optimizer.
7637 This can cause unexpected changes to inline assembler code, please go through
7638 the peephole optimizer
7639 \begin_inset LatexCommand \index{Peephole optimizer}
7643 rules defined in the source file tree '<target>/peeph.def' before using
7646 \labelwidthstring 00.00.0000
7661 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7667 The compiler will optimize code generation towards fast code, possibly
7668 at the expense of code size.
7670 \added_space_bottom bigskip \labelwidthstring 00.00.0000
7685 \begin_inset LatexCommand \index{-\/-opt-code-size}
7691 The compiler will optimize code generation towards compact code, possibly
7692 at the expense of code speed.
7696 \begin_inset LatexCommand \index{Options other}
7702 \labelwidthstring 00.00.0000
7718 \begin_inset LatexCommand \index{-\/-compile-only}
7723 \begin_inset LatexCommand \index{-c -\/-compile-only}
7729 will compile and assemble the source, but will not call the linkage editor.
7731 \labelwidthstring 00.00.0000
7750 \begin_inset LatexCommand \index{-\/-c1mode}
7756 reads the preprocessed source from standard input and compiles it.
7757 The file name for the assembler output must be specified using the -o option.
7759 \labelwidthstring 00.00.0000
7764 \begin_inset LatexCommand \index{-E}
7770 Run only the C preprocessor.
7771 Preprocess all the C source files specified and output the results to standard
7774 \labelwidthstring 00.00.0000
7780 \begin_inset LatexCommand \index{-o <path/file>}
7786 The output path resp.
7787 file where everything will be placed.
7788 If the parameter is a path, it must have a trailing slash (or backslash
7789 for the Windows binaries) to be recognized as a path.
7792 \labelwidthstring 00.00.0000
7807 \begin_inset LatexCommand \index{-\/-stack-auto}
7818 All functions in the source file will be compiled as
7823 \begin_inset LatexCommand \index{reentrant}
7828 the parameters and local variables will be allocated on the stack
7829 \begin_inset LatexCommand \index{stack}
7835 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7839 Parameters and Local Variables for more details.
7840 If this option is used all source files in the project should be compiled
7842 It automatically implies --int-long-reent and --float-reent.
7845 \labelwidthstring 00.00.0000
7860 \begin_inset LatexCommand \index{-\/-callee-saves}
7864 function1[,function2][,function3]....
7867 The compiler by default uses a caller saves convention for register saving
7868 across function calls, however this can cause unnecessary register pushing
7869 & popping when calling small functions from larger functions.
7870 This option can be used to switch the register saving convention for the
7871 function names specified.
7872 The compiler will not save registers when calling these functions, no extra
7873 code will be generated at the entry & exit (function prologue
7876 \begin_inset LatexCommand \index{function prologue}
7885 \begin_inset LatexCommand \index{function epilogue}
7891 ) for these functions to save & restore the registers used by these functions,
7892 this can SUBSTANTIALLY reduce code & improve run time performance of the
7894 In the future the compiler (with inter procedural analysis) will be able
7895 to determine the appropriate scheme to use for each function call.
7896 DO NOT use this option for built-in functions such as _mulint..., if this
7897 option is used for a library function the appropriate library function
7898 needs to be recompiled with the same option.
7899 If the project consists of multiple source files then all the source file
7900 should be compiled with the same -
7910 -callee-saves option string.
7911 Also see #pragma\SpecialChar ~
7913 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7919 \labelwidthstring 00.00.0000
7934 \begin_inset LatexCommand \index{-\/-debug}
7943 When this option is used the compiler will generate debug information.
7944 The debug information collected in a file with .cdb extension can be used
7946 For more information see documentation for SDCDB.
7947 Another file with no extension contains debug information in AOMF or AOMF51
7948 \begin_inset LatexCommand \index{AOMF, AOMF51}
7952 format which is commonly used by third party tools.
7954 \labelwidthstring 00.00.0000
7959 \begin_inset LatexCommand \index{-S}
7970 Stop after the stage of compilation proper; do not assemble.
7971 The output is an assembler code file for the input file specified.
7973 \labelwidthstring 00.00.0000
7988 \begin_inset LatexCommand \index{-\/-int-long-reent}
7994 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7995 Note by default these libraries are compiled as non-reentrant.
7996 See section Installation for more details.
7998 \labelwidthstring 00.00.0000
8013 \begin_inset LatexCommand \index{-\/-cyclomatic}
8022 This option will cause the compiler to generate an information message for
8023 each function in the source file.
8024 The message contains some
8028 information about the function.
8029 The number of edges and nodes the compiler detected in the control flow
8030 graph of the function, and most importantly the
8032 cyclomatic complexity
8033 \begin_inset LatexCommand \index{Cyclomatic complexity}
8039 see section on Cyclomatic Complexity for more details.
8041 \labelwidthstring 00.00.0000
8056 \begin_inset LatexCommand \index{-\/-float-reent}
8062 Floating point library is compiled as reentrant
8063 \begin_inset LatexCommand \index{reentrant}
8068 See section Installation for more details.
8070 \labelwidthstring 00.00.0000
8085 \begin_inset LatexCommand \index{-\/-main-return}
8091 This option can be used if the code generated is called by a monitor program
8092 or if the main routine includes an endless loop.
8093 This option results in slightly smaller code and saves two bytes of stack
8095 The return from the 'main'
8096 \begin_inset LatexCommand \index{main return}
8100 function will return to the function calling main.
8101 The default setting is to lock up i.e.
8108 \labelwidthstring 00.00.0000
8123 \begin_inset LatexCommand \index{-\/-nostdinc}
8129 This will prevent the compiler from passing on the default include path
8130 to the preprocessor.
8132 \labelwidthstring 00.00.0000
8147 \begin_inset LatexCommand \index{-\/-nostdlib}
8153 This will prevent the compiler from passing on the default library
8154 \begin_inset LatexCommand \index{Libraries}
8160 \labelwidthstring 00.00.0000
8175 \begin_inset LatexCommand \index{-\/-verbose}
8181 Shows the various actions the compiler is performing.
8183 \labelwidthstring 00.00.0000
8188 \begin_inset LatexCommand \index{-V}
8194 Shows the actual commands the compiler is executing.
8196 \labelwidthstring 00.00.0000
8211 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8217 Hides your ugly and inefficient c-code from the asm file, so you can always
8218 blame the compiler :)
8220 \labelwidthstring 00.00.0000
8235 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8241 Will not include peep-hole comments in the generated files.
8243 \labelwidthstring 00.00.0000
8258 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8264 Include i-codes in the asm file.
8265 Sounds like noise but is most helpful for debugging the compiler itself.
8267 \labelwidthstring 00.00.0000
8282 \begin_inset LatexCommand \index{-\/-less-pedantic}
8288 Disable some of the more pedantic warnings
8289 \begin_inset LatexCommand \index{Warnings}
8293 (jwk burps: please be more specific here, please!).
8295 \labelwidthstring 00.00.0000
8309 -disable-warning\SpecialChar ~
8311 \begin_inset LatexCommand \index{-\/-disable-warning}
8317 Disable specific warning with number <nnnn>.
8319 \labelwidthstring 00.00.0000
8334 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8340 Display the directories in the compiler's search path
8342 \labelwidthstring 00.00.0000
8357 \begin_inset LatexCommand \index{-\/-vc}
8363 Display errors and warnings using MSVC style, so you can use SDCC with
8365 With SDCC both offering a GCC-like (the default) and a MSVC-like output
8366 style, integration into most programming editors should be straightforward.
8368 \labelwidthstring 00.00.0000
8383 \begin_inset LatexCommand \index{-\/-use-stdout}
8389 Send errors and warnings to stdout instead of stderr.
8391 \labelwidthstring 00.00.0000
8396 asmOption[,asmOption]
8399 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8404 Pass the asmOption to the assembler
8405 \begin_inset LatexCommand \index{Options assembler}
8410 \begin_inset LatexCommand \index{Assembler options}
8415 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8417 \labelwidthstring 00.00.0000
8432 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8438 Generally follow the C89 standard, but allow SDCC features that conflict
8439 with the standard (default).
8441 \labelwidthstring 00.00.0000
8456 \begin_inset LatexCommand \index{-\/-std-c89}
8462 Follow the C89 standard and disable SDCC features that conflict with the
8465 \labelwidthstring 00.00.0000
8480 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8486 Generally follow the C99 standard, but allow SDCC features that conflict
8487 with the standard (incomplete support).
8489 \labelwidthstring 00.00.0000
8504 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8510 Follow the C99 standard and disable SDCC features that conflict with the
8511 standard (incomplete support).
8513 \labelwidthstring 00.00.0000
8530 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8535 <Name> The name to be used for the code
8536 \begin_inset LatexCommand \index{code}
8540 segment, default CSEG.
8541 This is useful if you need to tell the compiler to put the code in a special
8542 segment so you can later on tell the linker to put this segment in a special
8544 Can be used for instance when using bank switching to put the code in a
8547 \labelwidthstring 00.00.0000
8564 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8569 <Name> The name to be used for the const
8570 \begin_inset LatexCommand \index{code}
8574 segment, default CONST.
8575 This is useful if you need to tell the compiler to put the const data in
8576 a special segment so you can later on tell the linker to put this segment
8577 in a special place in memory.
8578 Can be used for instance when using bank switching to put the const data
8581 \added_space_bottom bigskip \labelwidthstring 00.00.0000
8593 a SDCC compiler option but if you want
8597 warnings you can use a separate tool dedicated to syntax checking like
8599 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8604 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8609 \begin_inset LatexCommand \url{http://www.splint.org}
8614 To make your source files parseable by splint you will have to include
8620 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8624 in your source file and add brackets around extended keywords (like
8627 \begin_inset Quotes sld
8640 \begin_inset Quotes srd
8648 \begin_inset Quotes sld
8651 __interrupt\SpecialChar ~
8653 \begin_inset Quotes srd
8661 Splint has an excellent on line manual at
8662 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8666 and it's capabilities go beyond pure syntax checking.
8667 You'll need to tell splint the location of SDCC's include files so a typical
8668 command line could look like this:
8672 splint\SpecialChar ~
8674 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8679 Intermediate Dump Options
8680 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8685 \begin_inset LatexCommand \index{Options intermediate dump}
8690 \begin_inset LatexCommand \index{Intermediate dump options}
8697 The following options are provided for the purpose of retargetting and debugging
8699 They provide a means to dump the intermediate code (iCode
8700 \begin_inset LatexCommand \index{iCode}
8704 ) generated by the compiler in human readable form at various stages of
8705 the compilation process.
8706 More on iCodes see chapter
8707 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8712 \begin_inset Quotes srd
8715 The anatomy of the compiler
8716 \begin_inset Quotes srd
8721 \labelwidthstring 00.00.0000
8736 \begin_inset LatexCommand \index{-\/-dumpraw}
8742 This option will cause the compiler to dump the intermediate code into
8745 <source filename>.dumpraw
8747 just after the intermediate code has been generated for a function, i.e.
8748 before any optimizations are done.
8750 \begin_inset LatexCommand \index{Basic blocks}
8754 at this stage ordered in the depth first number, so they may not be in
8755 sequence of execution.
8757 \labelwidthstring 00.00.0000
8772 \begin_inset LatexCommand \index{-\/-dumpgcse}
8778 Will create a dump of iCode's, after global subexpression elimination
8779 \begin_inset LatexCommand \index{Global subexpression elimination}
8785 <source filename>.dumpgcse.
8787 \labelwidthstring 00.00.0000
8802 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8808 Will create a dump of iCode's, after deadcode elimination
8809 \begin_inset LatexCommand \index{Dead-code elimination}
8815 <source filename>.dumpdeadcode.
8817 \labelwidthstring 00.00.0000
8832 \begin_inset LatexCommand \index{-\/-dumploop}
8841 Will create a dump of iCode's, after loop optimizations
8842 \begin_inset LatexCommand \index{Loop optimization}
8848 <source filename>.dumploop.
8850 \labelwidthstring 00.00.0000
8865 \begin_inset LatexCommand \index{-\/-dumprange}
8874 Will create a dump of iCode's, after live range analysis
8875 \begin_inset LatexCommand \index{Live range analysis}
8881 <source filename>.dumprange.
8883 \labelwidthstring 00.00.0000
8898 \begin_inset LatexCommand \index{-\/-dumlrange}
8904 Will dump the life ranges
8905 \begin_inset LatexCommand \index{Live range analysis}
8911 \labelwidthstring 00.00.0000
8926 \begin_inset LatexCommand \index{-\/-dumpregassign}
8935 Will create a dump of iCode's, after register assignment
8936 \begin_inset LatexCommand \index{Register assignment}
8942 <source filename>.dumprassgn.
8944 \labelwidthstring 00.00.0000
8959 \begin_inset LatexCommand \index{-\/-dumplrange}
8965 Will create a dump of the live ranges of iTemp's
8967 \added_space_bottom bigskip \labelwidthstring 00.00.0000
8982 \begin_inset LatexCommand \index{-\/-dumpall}
8993 Will cause all the above mentioned dumps to be created.
8996 Redirecting output on Windows Shells
8998 \added_space_bottom bigskip
8999 By default SDCC writes it's error messages to
9000 \begin_inset Quotes sld
9004 \begin_inset Quotes srd
9008 To force all messages to
9009 \begin_inset Quotes sld
9013 \begin_inset Quotes srd
9037 \begin_inset LatexCommand \index{-\/-use-stdout}
9042 Additionally, if you happen to have visual studio installed in your windows
9043 machine, you can use it to compile your sources using a custom build and
9059 \begin_inset LatexCommand \index{-\/-vc}
9064 Something like this should work:
9108 -model-large -c $(InputPath)
9111 Environment variables
9112 \begin_inset LatexCommand \index{Environment variables}
9119 SDCC recognizes the following environment variables:
9121 \labelwidthstring 00.00.0000
9126 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
9132 SDCC installs a signal handler
9133 \begin_inset LatexCommand \index{signal handler}
9137 to be able to delete temporary files after an user break (^C) or an exception.
9138 If this environment variable is set, SDCC won't install the signal handler
9139 in order to be able to debug SDCC.
9141 \labelwidthstring 00.00.0000
9148 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9154 Path, where temporary files will be created.
9155 The order of the variables is the search order.
9156 In a standard *nix environment these variables are not set, and there's
9157 no need to set them.
9158 On Windows it's recommended to set one of them.
9160 \labelwidthstring 00.00.0000
9165 \begin_inset LatexCommand \index{SDCC\_HOME}
9172 \begin_inset LatexCommand \ref{sub:Install-paths}
9178 \begin_inset Quotes sld
9182 \begin_inset Quotes srd
9187 \labelwidthstring 00.00.0000
9192 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9199 \begin_inset LatexCommand \ref{sub:Search-Paths}
9205 \begin_inset Quotes sld
9209 \begin_inset Quotes srd
9214 \labelwidthstring 00.00.0000
9219 \begin_inset LatexCommand \index{SDCC\_LIB}
9226 \begin_inset LatexCommand \ref{sub:Search-Paths}
9232 \begin_inset Quotes sld
9236 \begin_inset Quotes srd
9241 \added_space_bottom bigskip
9242 There are some more environment variables recognized by SDCC, but these
9243 are solely used for debugging purposes.
9244 They can change or disappear very quickly, and will never be documented.
9247 Storage Class Language Extensions
9250 MCS51/DS390 Storage Class
9251 \begin_inset LatexCommand \index{Storage class}
9258 In addition to the ANSI storage classes SDCC allows the following MCS51
9259 specific storage classes:
9260 \layout Subsubsection
9263 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9268 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9273 \begin_inset LatexCommand \index{near (storage class)}
9278 \begin_inset LatexCommand \index{\_\_near (storage class)}
9289 storage class for the Small Memory model (
9297 or the more ANSI-C compliant forms
9305 can be used synonymously).
9306 Variables declared with this storage class will be allocated in the directly
9307 addressable portion of the internal RAM of a 8051, e.g.:
9312 __data unsigned char test_data;
9315 Writing 0x01 to this variable generates the assembly code:
9320 75*00 01\SpecialChar ~
9326 \layout Subsubsection
9329 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9334 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9339 \begin_inset LatexCommand \index{far (storage class)}
9344 \begin_inset LatexCommand \index{\_\_far (storage class)}
9351 Variables declared with this storage class will be placed in the external
9357 storage class for the Large Memory model, e.g.:
9362 __xdata unsigned char test_xdata;
9365 Writing 0x01 to this variable generates the assembly code:
9370 90s00r00\SpecialChar ~
9399 \layout Subsubsection
9402 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9407 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9414 Variables declared with this storage class will be allocated into the indirectly
9415 addressable portion of the internal ram of a 8051, e.g.:
9420 __idata unsigned char test_idata;
9423 Writing 0x01 to this variable generates the assembly code:
9452 Please note, the first 128 byte of idata physically access the same RAM
9454 The original 8051 had 128 byte idata memory, nowadays most devices have
9455 256 byte idata memory.
9457 \begin_inset LatexCommand \index{stack}
9461 is located in idata memory.
9462 \layout Subsubsection
9465 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9470 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9477 Paged xdata access is just as straightforward as using the other addressing
9479 It is typically located at the start of xdata and has a maximum size of
9481 The following example writes 0x01 to the pdata variable.
9482 Please note, pdata access physically accesses xdata memory.
9483 The high byte of the address is determined by port P2
9484 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9488 (or in case of some 8051 variants by a separate Special Function Register,
9490 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9499 storage class for the Medium Memory model, e.g.:
9504 __pdata unsigned char test_pdata;
9507 Writing 0x01 to this variable generates the assembly code:
9551 \begin_inset LatexCommand \index{-\/-xstack}
9555 option is used the pdata memory area is followed by the xstack memory area
9556 and the sum of their sizes is limited to 256 bytes.
9557 \layout Subsubsection
9560 \begin_inset LatexCommand \index{code}
9565 \begin_inset LatexCommand \index{\_\_code}
9572 'Variables' declared with this storage class will be placed in the code
9578 __code unsigned char test_code;
9581 Read access to this variable generates the assembly code:
9586 90s00r6F\SpecialChar ~
9589 mov dptr,#_test_code
9618 indexed arrays of characters in code memory can be accessed efficiently:
9623 __code char test_array[] = {'c','h','e','a','p'};
9626 Read access to this array using an 8-bit unsigned index generates the assembly
9643 90s00r41\SpecialChar ~
9646 mov dptr,#_test_array
9661 \layout Subsubsection
9664 \begin_inset LatexCommand \index{bit}
9669 \begin_inset LatexCommand \index{\_\_bit}
9676 This is a data-type and a storage class specifier.
9677 When a variable is declared as a bit, it is allocated into the bit addressable
9678 memory of 8051, e.g.:
9686 Writing 1 to this variable generates the assembly code:
9702 The bit addressable memory consists of 128 bits which are located from 0x20
9703 to 0x2f in data memory.
9706 Apart from this 8051 specific storage class most architectures support ANSI-C
9708 \begin_inset LatexCommand \index{bitfields}
9718 Not really meant as examples, but nevertheless showing what bitfields are
9719 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9723 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9724 signed modifier are implemented as unsigned.
9725 \layout Subsubsection
9728 \begin_inset LatexCommand \index{sfr}
9733 \begin_inset LatexCommand \index{\_\_sfr}
9738 \begin_inset LatexCommand \index{sfr16}
9743 \begin_inset LatexCommand \index{\_\_sfr16}
9748 \begin_inset LatexCommand \index{sfr32}
9753 \begin_inset LatexCommand \index{\_\_sfr32}
9758 \begin_inset LatexCommand \index{\_\_sbit}
9763 \begin_inset LatexCommand \index{sbit}
9770 Like the bit keyword,
9772 sfr / sfr16 / sfr32 / sbit
9774 signify both a data-type and storage class, they are used to describe the
9795 variables of a 8051, eg:
9801 \begin_inset LatexCommand \index{at}
9806 \begin_inset LatexCommand \index{\_\_at}
9810 (0x80) P0;\SpecialChar ~
9811 /* special function register P0 at location 0x80 */
9813 /* 16 bit special function register combination for timer 0 */
9815 /* with the high byte at location 0x8C and the low byte at location 0x8A
9819 \begin_inset LatexCommand \index{at}
9824 \begin_inset LatexCommand \index{\_\_at}
9830 __sbit __at (0xd7) CY; /* CY (Carry Flag
9831 \begin_inset LatexCommand \index{Flags}
9836 \begin_inset LatexCommand \index{Carry flag}
9843 Special function registers which are located on an address dividable by
9844 8 are bit-addressable, an
9848 addresses a specific bit within these sfr.
9850 16 Bit and 32 bit special function register combinations which require a
9851 certain access order are better not declared using
9860 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9861 this is not guaranteed.
9866 Please note, if you use a header file which was written for another compiler
9867 then the sfr / sfr16 / sfr32 / sbit Storage Class extensions will most
9873 Specifically the syntax
9876 sfr P0 = 0x80;\SpecialChar ~
9883 by SDCC to an assignment of 0x80 to a variable called P0
9886 \begin_inset Marginal
9900 Nevertheless it is possible to write header files
9901 \begin_inset LatexCommand \index{Header files}
9906 \begin_inset LatexCommand \index{Include files}
9910 which can be shared among different compilers (see section
9911 \begin_inset LatexCommand \ref{sec:Porting-code-to-other-compilers}
9917 \layout Subsubsection
9920 \begin_inset LatexCommand \index{Pointer}
9924 to MCS51/DS390 specific memory spaces
9927 SDCC allows (via language extensions) pointers to explicitly point to any
9928 of the memory spaces
9929 \begin_inset LatexCommand \index{Memory model}
9934 In addition to the explicit pointers, the compiler uses (by default) generic
9935 pointers which can be used to point to any of the memory spaces.
9939 Pointer declaration examples:
9944 /* pointer physically in internal ram pointing to object in external ram
9947 __xdata unsigned char * __data p;
9951 /* pointer physically in external ram pointing to object in internal ram
9954 __data unsigned char * __xdata p;
9958 /* pointer physically in code rom pointing to data in xdata space */
9960 __xdata unsigned char * __code p;
9964 /* pointer physically in code space pointing to data in code space */
9966 __code unsigned char * __code p;
9970 /* generic pointer physically located in xdata space */
9972 unsigned char * __xdata p;
9976 /* generic pointer physically located in default memory space */
9982 /* the following is a function pointer physically located in data space
9985 char (* __data fp)(void);
9988 Well you get the idea.
9993 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
10006 The highest order byte of the
10010 pointers contains the data space information.
10011 Assembler support routines are called whenever data is stored or retrieved
10017 These are useful for developing reusable library
10018 \begin_inset LatexCommand \index{Libraries}
10023 Explicitly specifying the pointer type will generate the most efficient
10025 \layout Subsubsection
10027 Notes on MCS51 memory
10028 \begin_inset LatexCommand \index{MCS51 memory}
10035 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
10036 RAM memory which is structured as follows:
10040 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
10043 - Bytes 20-2F - 16 bytes to hold 128 bit
10044 \begin_inset LatexCommand \index{bit}
10050 - Bytes 30-7F - 80 bytes for general purpose use.
10055 Additionally some members of the MCS51 family may have up to 128 bytes of
10056 additional, indirectly addressable, internal RAM memory (
10061 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
10066 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
10071 Furthermore, some chips may have some built in external memory (
10076 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10081 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10085 ) which should not be confused with the internal, directly addressable RAM
10091 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
10096 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
10101 Sometimes this built in
10105 memory has to be activated before using it (you can probably find this
10106 information on the datasheet of the microcontroller your are using, see
10108 \begin_inset LatexCommand \ref{sub:Startup-Code}
10116 Normally SDCC will only use the first bank
10117 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10121 of registers (register bank 0), but it is possible to specify that other
10122 banks of registers (keyword
10129 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10134 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10140 ) should be used in interrupt
10141 \begin_inset LatexCommand \index{interrupt}
10146 \begin_inset LatexCommand \index{\_\_interrupt}
10151 By default, the compiler will place the stack after the last byte of allocated
10152 memory for variables.
10153 For example, if the first 2 banks of registers are used, and only four
10158 variables, it will position the base of the internal stack at address 20
10160 This implies that as the stack
10161 \begin_inset LatexCommand \index{stack}
10165 grows, it will use up the remaining register banks, and the 16 bytes used
10166 by the 128 bit variables, and 80 bytes for general purpose use.
10167 If any bit variables are used, the data variables will be placed in unused
10168 register banks and after the byte holding the last bit variable.
10169 For example, if register banks 0 and 1 are used, and there are 9 bit variables
10174 variables will be placed starting from address 0x10 to 0x20 and continue
10187 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
10191 to specify the start address of the
10206 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10210 to specify the size of the total internal RAM (
10224 By default the 8051 linker will place the stack after the last byte of (i)data
10237 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10241 allows you to specify the start of the stack, i.e.
10242 you could start it after any data in the general purpose area.
10243 If your microcontroller has additional indirectly addressable internal
10248 ) you can place the stack on it.
10249 You may also need to use -
10260 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10264 to set the start address of the external RAM (
10279 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10283 to specify its size.
10284 Same goes for the code memory, using -
10295 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10310 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10315 If in doubt, don't specify any options and see if the resulting memory
10316 layout is appropriate, then you can adjust it.
10318 \added_space_bottom bigskip
10319 The linker generates two files with memory allocation information.
10320 The first, with extension .map
10321 \begin_inset LatexCommand \index{<file>.map}
10325 shows all the variables and segments.
10326 The second with extension .mem
10327 \begin_inset LatexCommand \index{<file>.mem}
10331 shows the final memory layout.
10332 The linker will complain either if memory segments overlap, there is not
10333 enough memory, or there is not enough space for stack.
10334 If you get any linking warnings and/or errors related to stack or segments
10335 allocation, take a look at either the .map or .mem files to find out what
10337 The .mem file may even suggest a solution to the problem.
10340 Z80/Z180 Storage Class
10341 \begin_inset LatexCommand \index{Z80!Storage class}
10345 Language Extensions
10346 \layout Subsubsection
10349 \begin_inset LatexCommand \index{sfr}
10354 \begin_inset LatexCommand \index{\_\_sfr}
10358 (in/out to 8-bit addresses)
10362 \begin_inset LatexCommand \index{Z80}
10366 family has separate address spaces for memory and
10376 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10381 \begin_inset LatexCommand \index{Z80!I/O memory}
10386 \begin_inset LatexCommand \index{Z180!I/O memory}
10390 is accessed with special instructions, e.g.:
10395 sfr at 0x78 IoPort;\SpecialChar ~
10397 /* define a var in I/O space at 78h called IoPort */
10401 Writing 0x01 to this variable generates the assembly code:
10406 3E 01\SpecialChar ~
10414 D3 78\SpecialChar ~
10421 \layout Subsubsection
10424 \begin_inset LatexCommand \index{sfr}
10429 \begin_inset LatexCommand \index{\_\_sfr}
10433 (in/out to 16-bit addresses)
10440 is used to support 16 bit addresses in I/O memory e.g.:
10446 \begin_inset LatexCommand \index{at}
10451 \begin_inset LatexCommand \index{\_\_at}
10458 Writing 0x01 to this variable generates the assembly code:
10463 01 23 01\SpecialChar ~
10468 3E 01\SpecialChar ~
10476 ED 79\SpecialChar ~
10483 \layout Subsubsection
10486 \begin_inset LatexCommand \index{sfr}
10491 \begin_inset LatexCommand \index{\_\_sfr}
10495 (in0/out0 to 8 bit addresses on Z180
10496 \begin_inset LatexCommand \index{Z180}
10501 \begin_inset LatexCommand \index{HD64180 (see Z180)}
10507 \added_space_bottom bigskip
10508 The compiler option -
10519 \begin_inset LatexCommand \index{Z180!Options!-\/-portmode}
10523 =180 (80) and a compiler #pragma\SpecialChar ~
10525 \begin_inset LatexCommand \index{Z180!Pragmas!\#pragma portmode}
10529 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10539 If you include the file z180.h this will be set automatically.
10543 \begin_inset LatexCommand \index{HC08!Storage class}
10547 Language Extensions
10548 \layout Subsubsection
10551 \begin_inset LatexCommand \index{data (hc08 storage class)}
10556 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10563 The data storage class declares a variable that resides in the first 256
10564 bytes of memory (the direct page).
10566 \begin_inset LatexCommand \index{HC08}
10570 is most efficient at accessing variables (especially pointers) stored here.
10571 \layout Subsubsection
10574 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10579 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10585 \added_space_bottom bigskip
10586 The xdata storage class declares a variable that can reside anywhere in
10588 This is the default if no storage class is specified.
10592 Absolute Addressing
10593 \begin_inset LatexCommand \index{Absolute addressing}
10600 Data items can be assigned an absolute address with the
10603 \begin_inset LatexCommand \index{at}
10608 \begin_inset LatexCommand \index{\_\_at}
10614 keyword, in addition to a storage class, e.g.:
10620 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10625 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10630 \begin_inset LatexCommand \index{at}
10635 \begin_inset LatexCommand \index{\_\_at}
10639 0x7ffe unsigned int chksum;
10642 or, better conforming to ISO/IEC 9899 C:
10647 __xdata __at (0x7ffe) unsigned int chksum;
10650 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10651 of the external ram.
10656 reserve any space for variables declared in this way
10657 \begin_inset Marginal
10668 (they are implemented with an equate in the assembler).
10669 Thus it is left to the programmer to make sure there are no overlaps with
10670 other variables that are declared without the absolute address.
10671 The assembler listing file (.lst
10672 \begin_inset LatexCommand \index{<file>.lst}
10676 ) and the linker output files (.rst
10677 \begin_inset LatexCommand \index{<file>.rst}
10682 \begin_inset LatexCommand \index{<file>.map}
10686 ) are good places to look for such overlaps.
10687 Variables with an absolute address are
10690 \begin_inset Marginal
10704 \begin_inset LatexCommand \index{Variable initialization}
10711 In case of memory mapped I/O devices the keyword
10715 has to be used to tell the compiler that accesses might not be removed:
10721 \begin_inset LatexCommand \index{volatile}
10726 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10731 \begin_inset LatexCommand \index{at}
10735 (0x8000) unsigned char PORTA_8255;
10738 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10743 \begin_inset LatexCommand \index{Aligned array}
10750 starts at a block (256 byte) boundary
10751 \begin_inset LatexCommand \index{block boundary}
10756 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10762 Absolute addresses can be specified for variables in all storage classes,
10769 \begin_inset LatexCommand \index{bit}
10774 \begin_inset LatexCommand \index{at}
10781 The above example will allocate the variable at offset 0x02 in the bit-addressab
10783 There is no real advantage to assigning absolute addresses to variables
10784 in this manner, unless you want strict control over all the variables allocated.
10785 One possible use would be to write hardware portable code.
10786 For example, if you have a routine that uses one or more of the microcontroller
10787 I/O pins, and such pins are different for two different hardwares, you
10788 can declare the I/O pins in your routine using:
10794 \begin_inset LatexCommand \index{volatile}
10798 __bit MOSI;\SpecialChar ~
10802 /* master out, slave in */
10804 extern volatile __bit MISO;\SpecialChar ~
10808 /* master in, slave out */
10810 extern volatile __bit MCLK;\SpecialChar ~
10818 /* Input and Output of a byte on a 3-wire serial bus.
10823 If needed adapt polarity of clock, polarity of data and bit order
10828 unsigned char spi_io(unsigned char out_byte)
10852 MOSI = out_byte & 0x80;
10882 /* _asm nop _endasm; */\SpecialChar ~
10890 /* for slow peripherals */
10941 Then, someplace in the code for the first hardware you would use
10947 \begin_inset LatexCommand \index{at}
10952 \begin_inset LatexCommand \index{\_\_at}
10956 (0x80) MOSI;\SpecialChar ~
10960 /* I/O port 0, bit 0 */
10962 __bit __at (0x81) MISO;\SpecialChar ~
10966 /* I/O port 0, bit 1 */
10968 __bit __at (0x82) MCLK;\SpecialChar ~
10972 /* I/O port 0, bit 2 */
10975 Similarly, for the second hardware you would use
10980 __bit __at (0x83) MOSI;\SpecialChar ~
10984 /* I/O port 0, bit 3 */
10986 __bit __at (0x91) MISO;\SpecialChar ~
10990 /* I/O port 1, bit 1 */
10993 \begin_inset LatexCommand \index{bit}
10997 __at (0x92) MCLK;\SpecialChar ~
11001 /* I/O port 1, bit 2 */
11003 \added_space_bottom bigskip
11004 and you can use the same hardware dependent routine without changes, as
11005 for example in a library.
11006 This is somehow similar to sbit, but only one absolute address has to be
11007 specified in the whole project.
11011 \begin_inset LatexCommand \index{Parameters}
11016 \begin_inset LatexCommand \index{function parameter}
11021 \begin_inset LatexCommand \index{local variables}
11026 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
11033 Automatic (local) variables and parameters to functions can either be placed
11034 on the stack or in data-space.
11035 The default action of the compiler is to place these variables in the internal
11036 RAM (for small model) or external RAM (for large model).
11037 This in fact makes them similar to
11040 \begin_inset LatexCommand \index{static}
11046 so by default functions are non-reentrant
11047 \begin_inset LatexCommand \index{reentrant}
11056 They can be placed on the stack
11057 \begin_inset LatexCommand \index{stack}
11074 \begin_inset LatexCommand \index{-\/-stack-auto}
11082 #pragma\SpecialChar ~
11086 \begin_inset LatexCommand \index{\#pragma stackauto}
11093 \begin_inset LatexCommand \index{reentrant}
11099 keyword in the function declaration, e.g.:
11104 unsigned char foo(char i) __reentrant
11118 Since stack space on 8051 is limited, the
11136 option should be used sparingly.
11137 Note that the reentrant keyword just means that the parameters & local
11138 variables will be allocated to the stack, it
11142 mean that the function is register bank
11143 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11152 \begin_inset LatexCommand \index{local variables}
11156 can be assigned storage classes and absolute
11157 \begin_inset LatexCommand \index{Absolute addressing}
11166 unsigned char foo()
11174 __xdata unsigned char i;
11187 \begin_inset LatexCommand \index{at}
11191 (0x31) unsigned char j;
11203 In the above example the variable
11207 will be allocated in the external ram,
11211 in bit addressable space and
11230 or when a function is declared as
11234 this should only be done for static variables.
11238 \begin_inset LatexCommand \index{function parameter}
11242 however are not allowed any storage class
11243 \begin_inset LatexCommand \index{Storage class}
11247 , (storage classes for parameters will be ignored), their allocation is
11248 governed by the memory model in use, and the reentrancy options.
11251 It is however allowed to use bit parameters in reentrant functions and also
11252 non-static local bit variables are supported.
11253 Efficient use is limited to 8 semi-bitregisters in bit space.
11254 They are pushed and popped to stack
11255 \begin_inset LatexCommand \index{stack}
11259 as a single byte just like the normal registers.
11263 \begin_inset LatexCommand \label{sub:Overlaying}
11268 \begin_inset LatexCommand \index{Overlaying}
11276 \begin_inset LatexCommand \index{reentrant}
11280 functions SDCC will try to reduce internal ram space usage by overlaying
11281 parameters and local variables of a function (if possible).
11282 Parameters and local variables
11283 \begin_inset LatexCommand \index{local variables}
11287 of a function will be allocated to an overlayable segment if the function
11290 no other function calls and the function is non-reentrant and the memory
11292 \begin_inset LatexCommand \index{Memory model}
11299 If an explicit storage class
11300 \begin_inset LatexCommand \index{Storage class}
11304 is specified for a local variable, it will NOT be overlayed.
11307 Note that the compiler (not the linkage editor) makes the decision for overlayin
11309 Functions that are called from an interrupt service routine
11310 \begin_inset Marginal
11320 should be preceded by a #pragma\SpecialChar ~
11322 \begin_inset LatexCommand \index{\#pragma nooverlay}
11326 if they are not reentrant.
11329 Also note that the compiler does not do any processing of inline assembler
11330 code, so the compiler might incorrectly assign local variables and parameters
11331 of a function into the overlay segment if the inline assembler code calls
11332 other c-functions that might use the overlay.
11333 In that case the #pragma\SpecialChar ~
11334 nooverlay should be used.
11337 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11339 \begin_inset LatexCommand \index{Multiplication}
11344 \begin_inset LatexCommand \index{Division}
11348 will NOT be overlayed since these are implemented using external functions,
11357 \begin_inset LatexCommand \index{\#pragma nooverlay}
11363 void set_error(unsigned char errcd)
11379 void some_isr () __interrupt
11380 \begin_inset LatexCommand \index{interrupt}
11409 \added_space_bottom bigskip
11410 In the above example the parameter
11418 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11420 not present, this could cause unpredictable runtime behavior when called
11421 from an interrupt service routine.
11422 The #pragma\SpecialChar ~
11423 nooverlay ensures that the parameters and local variables for
11424 the function are NOT overlayed.
11427 Interrupt Service Routines
11428 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11435 General Information
11450 outines to be coded in C, with some extended keywords.
11455 void timer_isr (void) __interrupt (1) __using (1)
11469 The optional number following the
11472 \begin_inset LatexCommand \index{interrupt}
11477 \begin_inset LatexCommand \index{\_\_interrupt}
11483 keyword is the interrupt number this routine will service.
11484 When present, the compiler will insert a call to this routine in the interrupt
11485 vector table for the interrupt number specified.
11486 If you have multiple source files in your project, interrupt service routines
11487 can be present in any of them, but a prototype of the isr MUST be present
11488 or included in the file that contains the function
11496 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11501 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11507 keyword can be used to tell the compiler to use the specified register
11508 bank (8051 specific) when generating code for this function.
11514 Interrupt service routines open the door for some very interesting bugs:
11516 If an interrupt service routine changes variables which are accessed by
11517 other functions these variables have to be declared
11522 \begin_inset LatexCommand \index{volatile}
11530 If the access to these variables is not
11533 \begin_inset LatexCommand \index{atomic}
11540 the processor needs more than one instruction for the access and could
11541 be interrupted while accessing the variable) the interrupt must be disabled
11542 during the access to avoid inconsistent data.
11543 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11544 and should be protected by disabling interrupts.
11545 You're not automatically on the safe side if you use 8 bit variables though.
11546 We need an example here: f.e.
11547 on the 8051 the harmless looking
11548 \begin_inset Quotes srd
11553 flags\SpecialChar ~
11558 \begin_inset Quotes sld
11567 \begin_inset Quotes srd
11572 flags\SpecialChar ~
11577 \begin_inset Quotes sld
11580 from within an interrupt routine might get lost if the interrupt occurs
11583 \begin_inset Quotes sld
11588 counter\SpecialChar ~
11593 \begin_inset Quotes srd
11596 is not atomic on the 8051 even if
11600 is located in data memory.
11601 Bugs like these are hard to reproduce and can cause a lot of trouble.
11605 The return address and the registers used in the interrupt service routine
11606 are saved on the stack
11607 \begin_inset LatexCommand \index{stack}
11611 so there must be sufficient stack space.
11612 If there isn't variables or registers (or even the return address itself)
11619 \begin_inset LatexCommand \index{stack overflow}
11623 is most likely to happen if the interrupt occurs during the
11624 \begin_inset Quotes sld
11628 \begin_inset Quotes srd
11631 subroutine when the stack is already in use for f.e.
11632 many return addresses.
11635 A special note here, int (16 bit) and long (32 bit) integer division
11636 \begin_inset LatexCommand \index{Division}
11641 \begin_inset LatexCommand \index{Multiplication}
11646 \begin_inset LatexCommand \index{Modulus}
11651 \begin_inset LatexCommand \index{Floating point support}
11655 operations are implemented using external support routines developed in
11657 If an interrupt service routine needs to do any of these operations then
11658 the support routines (as mentioned in a following section) will have to
11659 be recompiled using the
11672 \begin_inset LatexCommand \index{-\/-stack-auto}
11678 option and the source file will need to be compiled using the
11693 \begin_inset LatexCommand \index{-\/-int-long-reent}
11698 Note, the type promotion
11699 \begin_inset LatexCommand \index{type promotion}
11703 required by ANSI C can cause 16 bit routines to be used without the programmer
11707 \added_space_bottom bigskip
11708 Calling other functions from an interrupt service routine is not recommended,
11709 avoid it if possible.
11710 Note that when some function is called from an interrupt service routine
11711 it should be preceded by a #pragma\SpecialChar ~
11713 \begin_inset LatexCommand \index{\#pragma nooverlay}
11717 if it is not reentrant.
11718 Furthermore nonreentrant functions should not be called from the main program
11719 while the interrupt service routine might be active.
11720 They also must not be called from low priority interrupt service routines
11721 while a high priority interrupt service routine might be active.
11722 You could use semaphores or make the function
11726 if all parameters are passed in registers.
11731 \begin_inset LatexCommand \ref{sub:Overlaying}
11736 about Overlaying and section
11737 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11742 about Functions using private register banks.
11745 MCS51/DS390 Interrupt Service Routines
11748 Interrupt numbers and the corresponding address & descriptions for the Standard
11749 8051/8052 are listed below.
11750 SDCC will automatically adjust the interrupt vector table to the maximum
11751 interrupt number specified.
11757 \begin_inset Tabular
11758 <lyxtabular version="3" rows="9" columns="3">
11760 <column alignment="center" valignment="top" leftline="true" width="0in">
11761 <column alignment="left" valignment="top" leftline="true" width="0in">
11762 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11763 <row topline="true" bottomline="true">
11764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11789 <row topline="true">
11790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11815 <row topline="true">
11816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11841 <row topline="true">
11842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11858 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11867 <row topline="true">
11868 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11893 <row topline="true">
11894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11902 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11910 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11919 <row topline="true">
11920 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11945 <row topline="true">
11946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11954 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11961 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11970 <row topline="true" bottomline="true">
11971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11979 <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">
12004 If the interrupt service routine is defined without
12007 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12012 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12018 a register bank or with register bank 0 (
12022 0), the compiler will save the registers used by itself on the stack upon
12023 entry and restore them at exit, however if such an interrupt service routine
12024 calls another function then the entire register bank will be saved on the
12026 This scheme may be advantageous for small interrupt service routines which
12027 have low register usage.
12029 \added_space_bottom bigskip
12030 If the interrupt service routine is defined to be using a specific register
12035 & psw are saved and restored, if such an interrupt service routine calls
12036 another function (using another register bank) then the entire register
12037 bank of the called function will be saved on the stack
12038 \begin_inset LatexCommand \index{stack}
12043 This scheme is recommended for larger interrupt service routines.
12047 \begin_inset LatexCommand \index{HC08}
12051 Interrupt Service Routines
12053 \added_space_bottom bigskip
12054 Since the number of interrupts
12055 \begin_inset LatexCommand \index{HC08!interrupt}
12059 available is chip specific and the interrupt vector table always ends at
12060 the last byte of memory, the interrupt numbers corresponds to the interrupt
12061 vectors in reverse order of address.
12062 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
12063 2 will use the interrupt vector at 0xfffa, and so on.
12064 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
12065 this way; instead see section
12066 \begin_inset LatexCommand \ref{sub:Startup-Code}
12070 for details on customizing startup.
12073 Z80 Interrupt Service Routines
12077 \begin_inset LatexCommand \index{Z80}
12081 uses several different methods for determining the correct interrupt
12082 \begin_inset LatexCommand \index{Z80!interrupt}
12086 vector depending on the hardware implementation.
12087 Therefore, SDCC ignores the optional interrupt number and does not attempt
12088 to generate an interrupt vector table.
12091 By default, SDCC generates code for a maskable interrupt, which uses a RETI
12092 instruction to return from the interrupt.
12093 To write an interrupt handler for the non-maskable interrupt, which needs
12094 a RETN instruction instead, add the
12103 void nmi_isr (void) critical interrupt
12116 \added_space_bottom bigskip
12117 However if you need to create a non-interruptable interrupt service routine
12118 you would also require the
12123 To distinguish between this and an nmi_isr you must provide an interrupt
12127 Enabling and Disabling Interrupts
12130 Critical Functions and Critical Statements
12133 A special keyword may be associated with a block or a function declaring
12139 SDCC will generate code to disable all interrupts
12140 \begin_inset LatexCommand \index{interrupt}
12144 upon entry to a critical function and restore the interrupt enable to the
12145 previous state before returning.
12146 Nesting critical functions will need one additional byte on the stack
12147 \begin_inset LatexCommand \index{stack}
12156 int foo () __critical
12157 \begin_inset LatexCommand \index{critical}
12162 \begin_inset LatexCommand \index{\_\_critical}
12187 The critical attribute maybe used with other attributes like
12197 may also be used to disable interrupts more locally:
12205 More than one statement could have been included in the block.
12208 Enabling and Disabling Interrupts directly
12212 \begin_inset LatexCommand \index{interrupt}
12216 can also be disabled and enabled directly (8051):
12221 EA = 0;\SpecialChar ~
12284 EA = 1;\SpecialChar ~
12351 On other architectures which have seperate opcodes for enabling and disabling
12352 interrupts you might want to make use of defines with inline assembly
12353 \begin_inset LatexCommand \index{Assembler routines}
12358 \begin_inset LatexCommand \index{HC08!interrupt}
12368 \begin_inset LatexCommand \index{\_asm}
12377 \begin_inset LatexCommand \index{\_endasm}
12386 #define SEI _asm\SpecialChar ~
12398 Note: it is sometimes sufficient to disable only a specific interrupt source
12400 a timer or serial interrupt by manipulating an
12403 \begin_inset LatexCommand \index{interrupt mask}
12413 Usually the time during which interrupts are disabled should be kept as
12415 This minimizes both
12420 \begin_inset LatexCommand \index{interrupt latency}
12424 (the time between the occurrence of the interrupt and the execution of
12425 the first code in the interrupt routine) and
12430 \begin_inset LatexCommand \index{interrupt jitter}
12434 (the difference between the shortest and the longest interrupt latency).
12435 These really are something different, f.e.
12436 a serial interrupt has to be served before its buffer overruns so it cares
12437 for the maximum interrupt latency, whereas it does not care about jitter.
12438 On a loudspeaker driven via a digital to analog converter which is fed
12439 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12440 a much smaller jitter will be very audible.
12443 You can reenable interrupts within an interrupt routine and on some architecture
12444 s you can make use of two (or more) levels of
12446 interrupt priorities
12449 \begin_inset LatexCommand \index{interrupt priority}
12454 On some architectures which don't support interrupt priorities these can
12455 be implemented by manipulating the interrupt mask and reenabling interrupts
12456 within the interrupt routine.
12457 Check there is sufficient space on the stack
12458 \begin_inset LatexCommand \index{stack}
12462 and don't add complexity unless you have to.
12467 \begin_inset LatexCommand \index{semaphore}
12471 locking (mcs51/ds390)
12474 Some architectures (mcs51/ds390) have an atomic
12475 \begin_inset LatexCommand \index{atomic}
12488 These type of instructions are typically used in preemptive multitasking
12489 systems, where a routine f.e.
12490 claims the use of a data structure ('acquires a lock
12491 \begin_inset LatexCommand \index{lock}
12495 on it'), makes some modifications and then releases the lock when the data
12496 structure is consistent again.
12497 The instruction may also be used if interrupt and non-interrupt code have
12498 to compete for a resource.
12499 With the atomic bit test and clear instruction interrupts
12500 \begin_inset LatexCommand \index{interrupt}
12504 don't have to be disabled for the locking operation.
12508 SDCC generates this instruction if the source follows this pattern:
12514 \begin_inset LatexCommand \index{volatile}
12518 bit resource_is_free;
12522 if (resource_is_free)
12532 resource_is_free=0;
12545 resource_is_free=1;
12552 Note, mcs51 and ds390 support only an atomic
12553 \begin_inset LatexCommand \index{atomic}
12561 instruction (as opposed to atomic bit test and
12566 Functions using private register banks
12567 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12574 Some architectures have support for quickly changing register sets.
12575 SDCC supports this feature with the
12578 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12583 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12589 attribute (which tells the compiler to use a register bank
12590 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12594 other than the default bank zero).
12595 It should only be applied to
12598 \begin_inset LatexCommand \index{interrupt}
12604 functions (see footnote below).
12605 This will in most circumstances make the generated ISR code more efficient
12606 since it will not have to save registers on the stack.
12613 attribute will have no effect on the generated code for a
12617 function (but may occasionally be useful anyway
12623 possible exception: if a function is called ONLY from 'interrupt' functions
12624 using a particular bank, it can be declared with the same 'using' attribute
12625 as the calling 'interrupt' functions.
12626 For instance, if you have several ISRs using bank one, and all of them
12627 call memcpy(), it might make sense to create a specialized version of memcpy()
12628 'using 1', since this would prevent the ISR from having to save bank zero
12629 to the stack on entry and switch to bank zero before calling the function
12636 (pending: Note, nowadays the
12640 attribute has an effect on
12644 the generated code for a
12659 function using a non-zero bank will assume that it can trash that register
12660 bank, and will not save it.
12661 Since high-priority interrupts
12662 \begin_inset LatexCommand \index{interrupts}
12667 \begin_inset LatexCommand \index{interrupt priority}
12671 can interrupt low-priority ones on the 8051 and friends, this means that
12672 if a high-priority ISR
12676 a particular bank occurs while processing a low-priority ISR
12680 the same bank, terrible and bad things can happen.
12681 To prevent this, no single register bank should be
12685 by both a high priority and a low priority ISR.
12686 This is probably most easily done by having all high priority ISRs use
12687 one bank and all low priority ISRs use another.
12688 If you have an ISR which can change priority at runtime, you're on your
12689 own: I suggest using the default bank zero and taking the small performance
12692 \added_space_bottom bigskip
12693 It is most efficient if your ISR calls no other functions.
12694 If your ISR must call other functions, it is most efficient if those functions
12695 use the same bank as the ISR (see note 1 below); the next best is if the
12696 called functions use bank zero.
12697 It is very inefficient to call a function using a different, non-zero bank
12703 \begin_inset LatexCommand \label{sub:Startup-Code}
12708 \begin_inset LatexCommand \index{Startup code}
12715 MCS51/DS390 Startup Code
12718 The compiler inserts a call to the C routine
12720 _sdcc_external_startup()
12721 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12730 at the start of the CODE area.
12731 This routine is in the runtime library
12732 \begin_inset LatexCommand \index{Runtime library}
12737 By default this routine returns 0, if this routine returns a non-zero value,
12738 the static & global variable initialization will be skipped and the function
12739 main will be invoked.
12740 Otherwise static & global variables will be initialized before the function
12744 _sdcc_external_startup()
12746 routine to your program to override the default if you need to setup hardware
12747 or perform some other critical operation prior to static & global variable
12749 \begin_inset LatexCommand \index{Variable initialization}
12754 On some mcs51 variants xdata
12755 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12759 memory has to be explicitly enabled before it can be accessed or if the
12761 \begin_inset LatexCommand \index{watchdog}
12765 needs to be disabled, this is the place to do it.
12766 The startup code clears all internal data memory, 256 bytes by default,
12767 but from 0 to n-1 if
12780 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12787 (recommended for Chipcon CC1010).
12789 \added_space_bottom bigskip
12790 See also the compiler options
12809 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12828 \begin_inset LatexCommand \index{-\/-main-return}
12833 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12838 about MCS51-variants.
12843 \added_space_bottom bigskip
12845 \begin_inset LatexCommand \index{HC08}
12849 startup code follows the same scheme as the MCS51 startup code.
12854 \added_space_bottom bigskip
12856 \begin_inset LatexCommand \index{Z80}
12860 the startup code is inserted by linking with crt0.o which is generated from
12861 sdcc/device/lib/z80/crt0.s.
12862 If you need a different startup code you can use the compiler option
12883 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12887 and provide your own crt0.o.
12891 Inline Assembler Code
12892 \begin_inset LatexCommand \index{Assembler routines}
12899 A Step by Step Introduction
12900 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12907 Starting from a small snippet of c-code this example shows for the MCS51
12908 how to use inline assembly, access variables, a function parameter and
12909 an array in xdata memory.
12910 The example uses an MCS51 here but is easily adapted for other architectures.
12911 This is a buffer routine which should be optimized:
12917 unsigned char __far
12918 \begin_inset LatexCommand \index{far (storage class)}
12923 \begin_inset LatexCommand \index{\_\_far (storage class)}
12928 \begin_inset LatexCommand \index{at}
12933 \begin_inset LatexCommand \index{\_\_at}
12937 (0x7f00) buf[0x100];
12938 \begin_inset LatexCommand \index{Aligned array}
12944 unsigned char head, tail;
12948 void to_buffer( unsigned char c )
12956 if( head != (unsigned char)(tail-1) )\SpecialChar ~
12962 \begin_inset LatexCommand \index{promotion to signed int}
12967 \begin_inset LatexCommand \index{type promotion}
12972 \begin_inset Marginal
12993 buf[ head++ ] = c;\SpecialChar ~
13009 /* access to a 256 byte aligned array */
13014 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
13015 then a corresponding buffer.asm file is generated.
13016 We define a new function
13020 in file buffer.c in which we cut and paste the generated code, removing
13021 unwanted comments and some ':'.
13023 \begin_inset Quotes sld
13027 \begin_inset Quotes srd
13031 \begin_inset Quotes sld
13035 \begin_inset Quotes srd
13038 to the beginning and the end of the function body:
13044 /* With a cut and paste from the .asm file, we have something to start with.
13049 The function is not yet OK! (registers aren't saved) */
13051 void to_buffer_asm( unsigned char c )
13060 \begin_inset LatexCommand \index{\_asm}
13065 \begin_inset LatexCommand \index{\_\_asm}
13079 ;buffer.c if( head != (unsigned char)(tail-1) ) \SpecialChar ~
13085 \begin_inset LatexCommand \index{promotion to signed int}
13090 \begin_inset LatexCommand \index{type promotion}
13142 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
13143 \begin_inset LatexCommand \index{Aligned array}
13208 \begin_inset LatexCommand \index{\_endasm}
13213 \begin_inset LatexCommand \index{\_\_endasm}
13222 The new file buffer.c should compile with only one warning about the unreferenced
13223 function argument 'c'.
13224 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
13225 (1) and finally have:
13231 unsigned char __far __at(0x7f00) buf[0x100];
13233 unsigned char head, tail;
13235 #define USE_ASSEMBLY (1)
13243 void to_buffer( unsigned char c )
13251 if( head != (unsigned char)(tail-1) )
13271 void to_buffer( unsigned char c )
13279 c; // to avoid warning: unreferenced function argument
13286 \begin_inset LatexCommand \index{\_asm}
13291 \begin_inset LatexCommand \index{\_\_asm}
13305 ; save used registers here.
13316 ; If we were still using r2,r3 we would have to push them here.
13319 ; if( head != (unsigned char)(tail-1) )
13362 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13386 ; buf[ head++ ] = c;
13397 a,dpl \SpecialChar ~
13404 ; dpl holds lower byte of function argument
13415 dpl,_head \SpecialChar ~
13418 ; buf is 0x100 byte aligned so head can be used directly
13460 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13472 ; restore used registers here
13479 \begin_inset LatexCommand \index{\_endasm}
13484 \begin_inset LatexCommand \index{\_\_endasm}
13495 The inline assembler code can contain any valid code understood by the assembler
13496 , this includes any assembler directives and comment lines.
13497 The assembler does not like some characters like ':' or ''' in comments.
13498 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13499 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13504 \begin_inset LatexCommand \index{Assembler documentation}
13509 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/as/doc/asxhtm.html}
13517 The compiler does not do any validation of the code within the
13520 \begin_inset LatexCommand \index{\_asm}
13525 \begin_inset LatexCommand \index{\_\_asm}
13533 \begin_inset LatexCommand \index{\_endasm}
13538 \begin_inset LatexCommand \index{\_\_endasm}
13547 Specifically it will not know which registers are used and thus register
13549 \begin_inset LatexCommand \index{push/pop}
13553 has to be done manually.
13557 It is recommended that each assembly instruction (including labels) be placed
13558 in a separate line (as the example shows).
13572 \begin_inset LatexCommand \index{-\/-peep-asm}
13578 command line option is used, the inline assembler code will be passed through
13579 the peephole optimizer
13580 \begin_inset LatexCommand \index{Peephole optimizer}
13585 There are only a few (if any) cases where this option makes sense, it might
13586 cause some unexpected changes in the inline assembler code.
13587 Please go through the peephole optimizer rules defined in file
13591 before using this option.
13595 \begin_inset LatexCommand \label{sub:Naked-Functions}
13600 \begin_inset LatexCommand \index{Naked functions}
13607 A special keyword may be associated with a function declaring it as
13610 \begin_inset LatexCommand \index{\_naked}
13615 \begin_inset LatexCommand \index{\_\_naked}
13626 function modifier attribute prevents the compiler from generating prologue
13627 \begin_inset LatexCommand \index{function prologue}
13632 \begin_inset LatexCommand \index{function epilogue}
13636 code for that function.
13637 This means that the user is entirely responsible for such things as saving
13638 any registers that may need to be preserved, selecting the proper register
13639 bank, generating the
13643 instruction at the end, etc.
13644 Practically, this means that the contents of the function must be written
13645 in inline assembler.
13646 This is particularly useful for interrupt functions, which can have a large
13647 (and often unnecessary) prologue/epilogue.
13648 For example, compare the code generated by these two functions:
13654 \begin_inset LatexCommand \index{volatile}
13658 data unsigned char counter;
13662 void simpleInterrupt(void) __interrupt
13663 \begin_inset LatexCommand \index{interrupt}
13668 \begin_inset LatexCommand \index{\_\_interrupt}
13686 void nakedInterrupt(void) __interrupt (2) __naked
13695 \begin_inset LatexCommand \index{\_asm}
13700 \begin_inset LatexCommand \index{\_\_asm}
13717 _counter ; does not change flags, no need to save psw
13729 ; MUST explicitly include ret or reti in _naked function.
13736 \begin_inset LatexCommand \index{\_endasm}
13741 \begin_inset LatexCommand \index{\_\_endasm}
13750 For an 8051 target, the generated simpleInterrupt looks like:
13759 example, recent versions of SDCC generate
13765 code for simpleInterrupt() and nakedInterrupt()!
13905 whereas nakedInterrupt looks like:
13920 _counter ; does not change flags, no need to save psw
13938 ; MUST explicitly include ret or reti in _naked function
13941 The related directive #pragma exclude
13942 \begin_inset LatexCommand \index{\#pragma exclude}
13946 allows a more fine grained control over pushing & popping
13947 \begin_inset LatexCommand \index{push/pop}
13954 While there is nothing preventing you from writing C code inside a
13958 function, there are many ways to shoot yourself in the foot doing this,
13959 and it is recommended that you stick to inline assembler.
13962 Use of Labels within Inline Assembler
13965 SDCC allows the use of in-line assembler with a few restrictions regarding
13967 In older versions of the compiler all labels defined within inline assembler
13976 where nnnn is a number less than 100 (which implies a limit of utmost 100
13977 inline assembler labels
13991 \begin_inset LatexCommand \index{\_asm}
13996 \begin_inset LatexCommand \index{\_\_asm}
14026 \begin_inset LatexCommand \index{\_endasm}
14031 \begin_inset LatexCommand \index{\_\_endasm}
14038 Inline assembler code cannot reference any C-Labels, however it can reference
14040 \begin_inset LatexCommand \index{Labels}
14044 defined by the inline assembler, e.g.:
14069 ; some assembler code
14089 /* some more c code */
14091 clabel:\SpecialChar ~
14093 /* inline assembler cannot reference this label */
14105 $0003: ;label (can be referenced by inline assembler only)
14112 \begin_inset LatexCommand \index{\_endasm}
14117 \begin_inset LatexCommand \index{\_\_endasm}
14127 /* some more c code */
14132 In other words inline assembly code can access labels defined in inline
14133 assembly within the scope of the function.
14134 The same goes the other way, i.e.
14135 labels defines in inline assembly can not be accessed by C statements.
14138 Interfacing with Assembler Code
14139 \begin_inset LatexCommand \index{Assembler routines}
14146 Global Registers used for Parameter Passing
14147 \begin_inset LatexCommand \index{Parameter passing}
14154 The compiler always uses the global registers
14157 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14162 \begin_inset LatexCommand \index{DPTR}
14167 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
14176 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
14182 to pass the first parameter to a routine.
14183 The second parameter onwards is either allocated on the stack (for reentrant
14194 -stack-auto is used) or in data / xdata memory (depending on the memory
14199 Assembler Routine (non-reentrant)
14202 In the following example
14203 \begin_inset LatexCommand \index{reentrant}
14208 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
14212 the function c_func calls an assembler routine asm_func, which takes two
14214 \begin_inset LatexCommand \index{function parameter}
14223 extern int asm_func(unsigned char, unsigned char);
14227 int c_func (unsigned char i, unsigned char j)
14235 return asm_func(i,j);
14249 return c_func(10,9);
14254 The corresponding assembler function is:
14259 .globl _asm_func_PARM_2
14360 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14377 Note here that the return values
14378 \begin_inset LatexCommand \index{return value}
14382 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14384 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14385 b' & 'acc' for four byte values.
14388 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14389 the parameter number starting from 1, and counting from the left.
14390 The first parameter is passed in
14391 \begin_inset Quotes eld
14395 \begin_inset Quotes erd
14398 for a one byte parameter,
14399 \begin_inset Quotes eld
14403 \begin_inset Quotes erd
14407 \begin_inset Quotes eld
14411 \begin_inset Quotes erd
14414 for three bytes and
14415 \begin_inset Quotes eld
14419 \begin_inset Quotes erd
14422 for a four bytes parameter.
14423 The variable name for the second parameter will be _<function_name>_PARM_2.
14427 Assemble the assembler routine with the following command:
14434 asx8051 -losg asmfunc.asm
14441 Then compile and link the assembler routine to the C source file with the
14449 sdcc cfunc.c asmfunc.rel
14452 Assembler Routine (reentrant)
14456 \begin_inset LatexCommand \index{reentrant}
14461 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14465 the second parameter
14466 \begin_inset LatexCommand \index{function parameter}
14470 onwards will be passed on the stack, the parameters are pushed from right
14472 after the call the leftmost parameter will be on the top of the stack.
14473 Here is an example:
14478 extern int asm_func(unsigned char, unsigned char);
14482 int c_func (unsigned char i, unsigned char j) reentrant
14490 return asm_func(i,j);
14504 return c_func(10,9);
14509 The corresponding assembler routine is:
14608 \added_space_bottom bigskip
14609 The compiling and linking procedure remains the same, however note the extra
14610 entry & exit linkage required for the assembler code, _bp is the stack
14611 frame pointer and is used to compute the offset into the stack for parameters
14612 and local variables.
14616 \begin_inset LatexCommand \index{int (16 bit)}
14621 \begin_inset LatexCommand \index{long (32 bit)}
14628 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14629 multiplication and modulus operations are implemented by support routines.
14630 These support routines are all developed in ANSI-C to facilitate porting
14631 to other MCUs, although some model specific assembler optimizations are
14633 The following files contain the described routines, all of them can be
14634 found in <installdir>/share/sdcc/lib.
14640 \begin_inset Tabular
14641 <lyxtabular version="3" rows="11" columns="2">
14643 <column alignment="left" valignment="top" leftline="true" width="0">
14644 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14645 <row topline="true" bottomline="true">
14646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14667 <row topline="true">
14668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14681 16 bit multiplication
14685 <row topline="true">
14686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14699 signed 16 bit division (calls _divuint)
14703 <row topline="true">
14704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14712 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14717 unsigned 16 bit division
14721 <row topline="true">
14722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14730 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14735 signed 16 bit modulus (calls _moduint)
14739 <row topline="true">
14740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14748 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14753 unsigned 16 bit modulus
14757 <row topline="true">
14758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14766 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14771 32 bit multiplication
14775 <row topline="true">
14776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14784 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14789 signed 32 division (calls _divulong)
14793 <row topline="true">
14794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14807 unsigned 32 division
14811 <row topline="true">
14812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14820 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14825 signed 32 bit modulus (calls _modulong)
14829 <row topline="true" bottomline="true">
14830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14843 unsigned 32 bit modulus
14856 Since they are compiled as
14861 \begin_inset LatexCommand \index{reentrant}
14866 \begin_inset LatexCommand \index{interrupt}
14870 service routines should not do any of the above operations.
14871 If this is unavoidable then the above routines will need to be compiled
14885 \begin_inset LatexCommand \index{-\/-stack-auto}
14891 option, after which the source program will have to be compiled with
14904 \begin_inset LatexCommand \index{-\/-int-long-reent}
14911 Notice that you don't have to call these routines directly.
14912 The compiler will use them automatically every time an integer operation
14916 Floating Point Support
14917 \begin_inset LatexCommand \index{Floating point support}
14924 SDCC supports IEEE (single precision 4 bytes) floating point numbers.
14925 The floating point support routines are derived from gcc's floatlib.c and
14926 consist of the following routines:
14934 \begin_inset Tabular
14935 <lyxtabular version="3" rows="17" columns="2">
14937 <column alignment="left" valignment="top" leftline="true" width="0">
14938 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14939 <row topline="true" bottomline="true">
14940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14957 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14966 <row topline="true">
14967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14998 add floating point numbers
15002 <row topline="true">
15003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15034 subtract floating point numbers
15038 <row topline="true">
15039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15070 divide floating point numbers
15074 <row topline="true">
15075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15106 multiply floating point numbers
15110 <row topline="true">
15111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15142 convert floating point to unsigned char
15146 <row topline="true">
15147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15178 convert floating point to signed char
15182 <row topline="true">
15183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15214 convert floating point to unsigned int
15218 <row topline="true">
15219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15236 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15250 convert floating point to signed int
15254 <row topline="true">
15255 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15295 convert floating point to unsigned long
15299 <row topline="true">
15300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15331 convert floating point to signed long
15335 <row topline="true">
15336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15367 convert unsigned char to floating point
15371 <row topline="true">
15372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15403 convert char to floating point number
15407 <row topline="true">
15408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15439 convert unsigned int to floating point
15443 <row topline="true">
15444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15461 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15475 convert int to floating point numbers
15479 <row topline="true">
15480 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15497 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15511 convert unsigned long to floating point number
15515 <row topline="true" bottomline="true">
15516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15547 convert long to floating point number
15559 \added_space_bottom bigskip
15560 These support routines are developed in ANSI-C so there is room for space
15561 and speed improvement
15567 These floating point routines (
15571 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15576 Note if all these routines are used simultaneously the data space might
15578 For serious floating point usage the large model might be needed.
15579 Also notice that you don't have to call this routines directly.
15580 The compiler will use them automatically every time a floating point operation
15585 \begin_inset LatexCommand \index{Libraries}
15594 <pending: this is messy and incomplete - a little more information is in
15595 sdcc/doc/libdoc.txt
15600 Compiler support routines (_gptrget, _mulint etc.)
15603 Stdclib functions (puts, printf, strcat etc.)
15604 \layout Subsubsection
15610 \begin_inset LatexCommand \index{<stdio.h>}
15614 As usual on embedded systems you have to provide your own
15617 \begin_inset LatexCommand \index{getchar()}
15626 \begin_inset LatexCommand \index{putchar()}
15633 SDCC does not know whether the system connects to a serial line with or
15634 without handshake, LCD, keyboard or other device.
15648 You'll find examples for serial routines f.e.
15649 in sdcc/device/lib.
15655 \begin_inset LatexCommand \index{printf()}
15665 does not support float (except on ds390).
15666 To enable this recompile it with the option
15679 \begin_inset LatexCommand \index{USE\_FLOATS}
15685 on the command line.
15699 \begin_inset LatexCommand \index{-\/-model-large}
15705 for the mcs51 port, since this uses a lot of memory.
15708 If you're short on memory you might want to use
15711 \begin_inset LatexCommand \index{printf\_small()}
15726 For the mcs51 there additionally are assembly versions
15729 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15735 (subset of printf using less than 270 bytes) and
15738 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15747 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15753 (floating-point aware version of printf_fast) which should fit the requirements
15754 of many embedded systems (printf_fast() can be customized by unsetting
15759 support long variables and field widths).
15760 \layout Subsubsection
15763 \begin_inset LatexCommand \index{malloc.h}
15770 Before using dynamic
15771 \begin_inset LatexCommand \index{dynamic}
15775 memory allocation with SDCC, you have to provide heap
15776 \begin_inset LatexCommand \index{heap}
15780 space for malloc to allocate memory from
15785 You can acomplish this by including the following code into your source:
15790 #include <malloc.h>
15791 \begin_inset LatexCommand \index{malloc.h}
15796 \begin_inset LatexCommand \index{calloc}
15801 \begin_inset LatexCommand \index{malloc}
15806 \begin_inset LatexCommand \index{realloc}
15811 \begin_inset LatexCommand \index{free}
15819 #define HEAPSIZE 0x1000 /* Adjust depending on available memory */
15821 unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory
15833 /* Your variable declarations come here*/
15843 init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE);
15849 /* Rest of your code*/
15856 Math functions (sinf, powf, sqrtf etc.)
15857 \layout Subsubsection
15862 See definitions in file <math.h>.
15869 \begin_inset LatexCommand \index{Libraries}
15873 included in SDCC should have a license at least as liberal as the GNU Lesser
15874 General Public License
15875 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15886 license statements for the libraries are missing.
15887 sdcc/device/lib/ser_ir.c
15891 come with a GPL (as opposed to LGPL) License - this will not be liberal
15892 enough for many embedded programmers.
15895 If you have ported some library or want to share experience about some code
15897 falls into any of these categories Busses (I
15898 \begin_inset Formula $^{\textrm{2}}$
15901 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15902 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15903 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15904 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15909 would certainly like to hear about it.
15911 \added_space_bottom bigskip
15912 Programmers coding for embedded systems are not especially famous for being
15913 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15914 e these references are very valuable.
15915 Let's help to create a climate where information is shared.
15921 MCS51 Memory Models
15922 \begin_inset LatexCommand \index{Memory model}
15927 \begin_inset LatexCommand \index{MCS51 memory model}
15932 \layout Subsubsection
15934 Small, Medium and Large
15937 SDCC allows three memory models for MCS51 code,
15946 Modules compiled with different memory models should
15950 be combined together or the results would be unpredictable.
15951 The library routines supplied with the compiler are compiled as small,
15953 The compiled library modules are contained in separate directories as small,
15954 medium and large so that you can link to the appropriate set.
15957 When the medium or large model is used all variables declared without a
15958 storage class will be allocated into the external ram, this includes all
15959 parameters and local variables (for non-reentrant
15960 \begin_inset LatexCommand \index{reentrant}
15965 When the small model is used variables without storage class are allocated
15966 in the internal ram.
15969 Judicious usage of the processor specific storage classes
15970 \begin_inset LatexCommand \index{Storage class}
15974 and the 'reentrant' function type will yield much more efficient code,
15975 than using the large model.
15976 Several optimizations are disabled when the program is compiled using the
15977 large model, it is therefore recommended that the small model be used unless
15978 absolutely required.
15979 \layout Subsubsection
15982 \begin_inset LatexCommand \label{sub:External-Stack}
15987 \begin_inset LatexCommand \index{stack}
15992 \begin_inset LatexCommand \index{External stack (mcs51)}
15999 The external stack (-
16010 \begin_inset LatexCommand \index{-\/-xstack}
16014 ) is located in pdata
16015 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16019 memory (usually at the start of the external ram segment) and uses all
16020 unused space in pdata (max.
16032 -xstack option is used to compile the program, the parameters and local
16034 \begin_inset LatexCommand \index{local variables}
16038 of all reentrant functions are allocated in this area.
16039 This option is provided for programs with large stack space requirements.
16040 When used with the -
16051 \begin_inset LatexCommand \index{-\/-stack-auto}
16055 option, all parameters and local variables are allocated on the external
16056 stack (note: support libraries will need to be recompiled with the same
16058 There is a predefined target in the library makefile).
16061 The compiler outputs the higher order address byte of the external ram segment
16063 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16068 \begin_inset LatexCommand \ref{sub:MCS51-variants}
16072 ), therefore when using the External Stack option, this port
16076 be used by the application program.
16080 \begin_inset LatexCommand \index{Memory model}
16085 \begin_inset LatexCommand \index{DS390 memory model}
16092 The only model supported is Flat 24
16093 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
16098 This generates code for the 24 bit contiguous addressing mode of the Dallas
16100 In this mode, up to four meg of external RAM or code space can be directly
16102 See the data sheets at www.dalsemi.com for further information on this part.
16106 Note that the compiler does not generate any code to place the processor
16107 into 24 bitmode (although
16111 in the ds390 libraries will do that for you).
16117 \begin_inset LatexCommand \index{Tinibios (DS390)}
16121 , the boot loader or similar code must ensure that the processor is in 24
16122 bit contiguous addressing mode before calling the SDCC startup code.
16140 option, variables will by default be placed into the XDATA segment.
16145 Segments may be placed anywhere in the 4 meg address space using the usual
16157 Note that if any segments are located above 64K, the -r flag must be passed
16158 to the linker to generate the proper segment relocations, and the Intel
16159 HEX output format must be used.
16160 The -r flag can be passed to the linker by using the option
16164 on the SDCC command line.
16165 However, currently the linker can not handle code segments > 64k.
16169 \begin_inset LatexCommand \label{sec:Pragmas}
16174 \begin_inset LatexCommand \index{Pragmas}
16181 SDCC supports the following #pragma directives:
16189 \begin_inset LatexCommand \index{\#pragma save}
16193 - this will save most current options to the save/restore stack.
16194 See #pragma\SpecialChar ~
16203 \begin_inset LatexCommand \index{\#pragma restore}
16207 - will restore saved options from the last save.
16208 saves & restores can be nested.
16209 SDCC uses a save/restore stack: save pushes current options to the stack,
16210 restore pulls current options from the stack.
16211 See #pragma\SpecialChar ~
16222 \begin_inset LatexCommand \index{\#pragma callee\_saves}
16227 \begin_inset LatexCommand \index{function prologue}
16231 function1[,function2[,function3...]] - The compiler by default uses a caller
16232 saves convention for register saving across function calls, however this
16233 can cause unnecessary register pushing & popping
16234 \begin_inset LatexCommand \index{push/pop}
16238 when calling small functions from larger functions.
16239 This option can be used to switch off the register saving convention for
16240 the function names specified.
16241 The compiler will not save registers when calling these functions, extra
16242 code need to be manually inserted at the entry & exit for these functions
16243 to save & restore the registers used by these functions, this can SUBSTANTIALLY
16244 reduce code & improve run time performance of the generated code.
16245 In the future the compiler (with inter procedural analysis) may be able
16246 to determine the appropriate scheme to use for each function call.
16257 -callee-saves command line option is used, the function names specified
16258 in #pragma\SpecialChar ~
16260 \begin_inset LatexCommand \index{\#pragma callee\_saves}
16264 is appended to the list of functions specified in the command line.
16272 \begin_inset LatexCommand \index{\#pragma exclude}
16276 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
16277 of pairs of push/pop
16278 \begin_inset LatexCommand \index{push/pop}
16287 \begin_inset LatexCommand \index{interrupt}
16300 The directive should be placed immediately before the ISR function definition
16301 and it affects ALL ISR functions following it.
16302 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
16303 exclude\SpecialChar ~
16305 \begin_inset LatexCommand \index{\#pragma exclude}
16310 See also the related keyword _naked
16311 \begin_inset LatexCommand \index{\_naked}
16316 \begin_inset LatexCommand \index{\_\_naked}
16328 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
16332 - the compiler will not warn you anymore for obvious mistakes, you'r on
16341 \begin_inset LatexCommand \index{\#pragma disable\_warning}
16345 - the compiler will not warn you anymore about warning number <nnnn>.
16353 \begin_inset LatexCommand \index{\#pragma nogcse}
16357 - will stop global common subexpression elimination.
16365 \begin_inset LatexCommand \index{\#pragma noinduction}
16369 - will stop loop induction optimizations.
16377 \begin_inset LatexCommand \index{\#pragma noinvariant}
16381 - will not do loop invariant optimizations.
16382 For more details see Loop Invariants in section
16383 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
16395 \begin_inset LatexCommand \index{\#pragma noiv}
16399 - Do not generate interrupt
16400 \begin_inset LatexCommand \index{interrupt}
16404 vector table entries for all ISR functions defined after the pragma.
16405 This is useful in cases where the interrupt vector table must be defined
16406 manually, or when there is a secondary, manually defined interrupt vector
16408 for the autovector feature of the Cypress EZ-USB FX2).
16409 More elegantly this can be achieved by obmitting the optional interrupt
16410 number after the interrupt keyword, see section
16411 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
16424 \begin_inset LatexCommand \index{\#pragma nojtbound}
16428 - will not generate code for boundary value checking, when switch statements
16429 are turned into jump-tables (dangerous).
16430 For more details see section
16431 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16443 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16447 - Will not do loop reversal optimization
16455 \begin_inset LatexCommand \index{\#pragma nooverlay}
16459 - the compiler will not overlay the parameters and local variables of a
16468 \begin_inset LatexCommand \index{\#pragma stackauto}
16483 \begin_inset LatexCommand \index{-\/-stack-auto}
16488 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16492 Parameters and Local Variables.
16500 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16504 - The compiler will optimize code generation towards fast code, possibly
16505 at the expense of code size.
16506 Currently this has little effect.
16514 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16518 - The compiler will optimize code generation towards compact code, possibly
16519 at the expense of code speed.
16520 Currently this has little effect.
16528 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16532 - The compiler will attempt to generate code that is both compact and fast,
16533 as long as meeting one goal is not a detriment to the other (this is the
16543 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16547 - Generally follow the C89 standard, but allow SDCC features that conflict
16548 with the standard (default).
16556 \begin_inset LatexCommand \index{\#pragma std\_c89}
16560 - Follow the C89 standard and disable SDCC features that conflict with the
16569 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16573 - Generally follow the C99 standard, but allow SDCC features that conflict
16574 with the standard (incomplete support).
16582 \begin_inset LatexCommand \index{\#pragma std\_c99}
16586 - Follow the C99 standard and disable SDCC features that conflict with the
16587 standard (incomplete support).
16595 \begin_inset LatexCommand \index{\#pragma codeseg}
16599 - Use this name (max.
16600 8 characters) for the code segment.
16619 \begin_inset LatexCommand \index{\#pragma constseg}
16623 - Use this name (max.
16624 8 characters) for the const segment.
16638 The preprocessor SDCPP
16639 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
16643 supports the following #pragma directives:
16651 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16655 (+ | -) - switch _asm _endasm block preprocessing on / off.
16657 You use this prama to define multilines of assembly code.
16658 This will prevent the preprocessor from changing the formating required
16660 Below is an example on how to use this pragma.
16665 #pragma preproc_asm -
16666 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16672 #define MYDELAY _asm
16677 nop ;my assembly comment...
16691 #pragma preproc_asm +
16721 The pragma's are intended to be used to turn-on or off certain optimizations
16722 which might cause the compiler to generate extra stack / data space to
16723 store compiler generated temporary variables.
16724 This usually happens in large functions.
16725 Pragma directives should be used as shown in the following example, they
16726 are used to control options & optimizations for a given function; pragmas
16727 should be placed before and/or after a function, placing pragma's inside
16728 a function body could have unpredictable results.
16734 \begin_inset LatexCommand \index{\#pragma save}
16745 /* save the current settings */
16748 \begin_inset LatexCommand \index{\#pragma nogcse}
16757 /* turnoff global subexpression elimination */
16759 #pragma noinduction
16760 \begin_inset LatexCommand \index{\#pragma noinduction}
16764 /* turn off induction optimizations */
16787 \begin_inset LatexCommand \index{\#pragma restore}
16791 /* turn the optimizations back on */
16794 The compiler will generate a warning message when extra space is allocated.
16795 It is strongly recommended that the save and restore pragma's be used when
16796 changing options for a function.
16805 Defines Created by the Compiler
16808 The compiler creates the following #defines
16809 \begin_inset LatexCommand \index{\#defines}
16814 \begin_inset LatexCommand \index{Defines created by the compiler}
16824 \begin_inset Tabular
16825 <lyxtabular version="3" rows="11" columns="2">
16827 <column alignment="left" valignment="top" leftline="true" width="3in">
16828 <column alignment="left" valignment="top" leftline="true" rightline="true" width="3in">
16829 <row topline="true" bottomline="true">
16830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16851 <row topline="true">
16852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16858 \begin_inset LatexCommand \index{SDCC}
16865 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16871 Since version 2.5.6 the version number as an int (ex.
16876 <row topline="true">
16877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16883 \begin_inset LatexCommand \index{SDCC\_mcs51}
16888 \begin_inset LatexCommand \index{SDCC\_ds390}
16893 \begin_inset LatexCommand \index{SDCC\_z80}
16900 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16905 depending on the model used (e.g.: -mds390)
16909 <row topline="true">
16910 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16916 \begin_inset LatexCommand \index{\_\_mcs51}
16921 \begin_inset LatexCommand \index{\_\_ds390}
16926 \begin_inset LatexCommand \index{\_\_hc08}
16931 \begin_inset LatexCommand \index{\_\_z80}
16938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16943 depending on the model used (e.g.
16948 <row topline="true">
16949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16955 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16985 <row topline="true">
16986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16992 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16999 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17022 <row topline="true">
17023 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17029 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
17036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17059 <row topline="true">
17060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17066 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
17073 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17096 <row topline="true">
17097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17103 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
17110 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17133 <row topline="true">
17134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17140 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
17147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17160 <row topline="true" bottomline="true">
17161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17167 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
17174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17194 Notes on supported Processors
17198 \begin_inset LatexCommand \label{sub:MCS51-variants}
17203 \begin_inset LatexCommand \index{MCS51 variants}
17210 MCS51 processors are available from many vendors and come in many different
17212 While they might differ considerably in respect to Special Function Registers
17213 the core MCS51 is usually not modified or is kept compatible.
17217 pdata access by SFR
17220 With the upcome of devices with internal xdata and flash memory devices
17222 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
17226 as dedicated I/O port is becoming more popular.
17227 Switching the high byte for pdata
17228 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
17232 access which was formerly done by port P2 is then achieved by a Special
17234 \begin_inset LatexCommand \index{sfr}
17239 In well-established MCS51 tradition the address of this
17243 is where the chip designers decided to put it.
17244 Needless to say that they didn't agree on a common name either.
17245 So that the startup code can correctly initialize xdata variables, you
17246 should define an sfr with the name _XPAGE
17249 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
17255 at the appropriate location if the default, port P2, is not used for this.
17261 __sfr __at (0x92) _XPAGE; /* Cypress EZ-USB family */
17266 __sfr __at (0xaf) _XPAGE; /* some Silicon Labs (Cygnal) chips */
17271 __sfr __at (0xaa) _XPAGE; /* some Silicon Labs (Cygnal) chips */
17274 For more exotic implementations further customizations may be needed.
17276 \begin_inset LatexCommand \ref{sub:Startup-Code}
17280 for other possibilities.
17283 Other Features available by SFR
17285 \added_space_bottom bigskip
17286 Some MCS51 variants offer features like Double DPTR
17287 \begin_inset LatexCommand \index{DPTR}
17291 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
17292 These are currently not used for the MCS51 port.
17293 If you absolutely need them you can fall back to inline assembly or submit
17299 \added_space_bottom bigskip
17301 \begin_inset LatexCommand \index{DS80C400}
17306 \begin_inset LatexCommand \index{DS400}
17310 microcontroller has a rich set of peripherals.
17311 In its built-in ROM library it includes functions to access some of the
17312 features, among them is a TCP stack with IP4 and IP6 support.
17313 Library headers (currently in beta status) and other files are provided
17317 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
17325 The Z80 and gbz80 port
17328 SDCC can target both the Zilog Z80
17329 \begin_inset LatexCommand \index{Z80}
17333 and the Nintendo Gameboy's Z80-like gbz80
17334 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
17339 The Z80 port is passed through the same
17342 \begin_inset LatexCommand \index{Regression test}
17348 as the MCS51 and DS390 ports, so floating point support, support for long
17349 variables and bitfield support is fine.
17350 See mailing lists and forums about interrupt routines.
17352 \added_space_bottom bigskip
17353 As always, the code is the authoritative reference - see z80/ralloc.c and
17356 \begin_inset LatexCommand \index{Z80!stack}
17360 frame is similar to that generated by the IAR Z80 compiler.
17361 IX is used as the base pointer, HL and IY are used as a temporary registers,
17362 and BC and DE are available for holding variables.
17364 \begin_inset LatexCommand \index{Z80!return value}
17368 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
17370 The gbz80 port use the same set of registers for the return values, but
17371 in a different order of significance: E (one byte), DE (two bytes), or
17377 \added_space_bottom bigskip
17378 The port to the Motorola HC08
17379 \begin_inset LatexCommand \index{HC08}
17383 family has been added in October 2003, and is still undergoing some basic
17385 The code generator is complete, but the register allocation is still quite
17387 Some of the SDCC's standard C library functions have embedded non-HC08
17388 inline assembly and so are not yet usable.
17395 \begin_inset LatexCommand \index{PIC14}
17399 port still requires a major effort from the development community.
17400 However it can work for very simple code.
17403 C code and 14bit PIC code page
17404 \begin_inset LatexCommand \index{code page (pic14)}
17409 \begin_inset LatexCommand \index{RAM bank (pic14)}
17416 The linker organizes allocation for the code page and RAM banks.
17417 It does not have intimate knowledge of the code flow.
17418 It will put all the code section of a single asm file into a single code
17420 In order to make use of multiple code pages, separate asm files must be
17422 The compiler treats all functions of a single C file as being in the same
17423 code page unless it is non static.
17424 The compiler treats all local variables of a single C file as being in
17425 the same RAM bank unless it is an extern.
17429 To get the best follow these guide lines:
17432 make local functions static, as non static functions require code page selection
17436 Make local variables static as extern variables require RAM bank selection
17440 For devices that have multiple code pages it is more efficient to use the
17441 same number of files as pages, i.e.
17442 for the 16F877 use 4 separate files and i.e.
17443 for the 16F874 use 2 separate files.
17444 This way the linker can put the code for each file into different code
17445 pages and the compiler can allocate reusable variables more efficiently
17446 and there's less page selection overhead.
17447 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
17448 instruction set) use 'unsigned char' whereever possible instead of 'int'.
17451 Creating a device include file
17454 For generating a device include file
17455 \begin_inset LatexCommand \index{PIC14!Header files}
17459 use the support perl script inc2h.pl kept in directory support/script.
17465 For the interrupt function, use the keyword 'interrupt'
17466 \begin_inset LatexCommand \index{PIC14!interrupt}
17470 with level number of 0 (PIC14 only has 1 interrupt so this number is only
17471 there to avoid a syntax error - it ought to be fixed).
17477 void Intr(void) interrupt 0
17483 T0IF = 0; /* Clear timer interrupt */
17488 Linking and assembling
17491 For assembling you can use either GPUTILS'
17492 \begin_inset LatexCommand \index{gputils (pic tools)}
17496 gpasm.exe or MPLAB's mpasmwin.exe.
17497 GPUTILS is available from
17498 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17503 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17504 If you use MPLAB and an interrupt function then the linker script file
17505 vectors section will need to be enlarged to link with mplink.
17528 sdcc -S -V -mpic14 -p16F877 $<
17542 $(PRJ).hex: $(OBJS)
17552 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17574 sdcc -S -V -mpic14 -p16F877 $<
17584 mpasmwin /q /o $*.asm
17588 $(PRJ).hex: $(OBJS)
17598 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17601 Please note that indentations within a
17605 have to be done with a tabulator character.
17608 Command-line options
17611 Besides the switches common to all SDCC backends, the PIC14 port accepts
17612 the following options (for an updated list see sdcc -
17624 \labelwidthstring 00.00.0000
17637 \begin_inset LatexCommand \index{PIC14!Options!-\/-debug-extra}
17641 emit debug info in assembly output
17643 \labelwidthstring 00.00.0000
17656 \begin_inset LatexCommand \index{PIC14!Options!-\/-no-pcode-opt}
17660 disable (slightly faulty) optimization on pCode
17664 \layout Subsubsection
17666 error: missing definition for symbol
17667 \begin_inset Quotes sld
17671 \begin_inset Quotes srd
17677 The PIC14 port uses library routines to provide more complex operations
17678 like multiplication, division/modulus and (generic) pointer dereferencing.
17679 In order to add these routines to your project, you must link with PIC14's
17685 For single source file projects this is done automatically, more complex
17690 to the linker's arguments.
17691 Make sure you also add an include path for the library (using the -I switch
17693 \layout Subsubsection
17695 Processor mismatch in file
17696 \begin_inset Quotes sld
17700 \begin_inset Quotes srd
17706 This warning can usually be ignored due to the very good compatibility amongst
17708 \begin_inset LatexCommand \index{PIC14}
17715 You might also consider recompiling the library for your specific device
17716 by changing the ARCH=p16f877 (default target) entry in
17718 device/lib/pic/Makefile.in
17722 device/lib/pic/Makefile
17724 to reflect your device.
17725 This might even improve performance for smaller devices as unneccesary
17726 BANKSELs migth be removed.
17730 \layout Subsubsection
17735 Currently, data can only be initialized if it resides in the source file
17741 Data in other source files will silently
17749 \begin_inset Marginal
17764 \begin_inset LatexCommand \index{PIC16}
17772 \begin_inset LatexCommand \index{PIC16}
17776 port is the portion of SDCC that is responsible to produce code for the
17778 \begin_inset LatexCommand \index{Microchip}
17782 (TM) microcontrollers with 16 bit core.
17783 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17784 Currently supported devices are:
17788 \begin_inset Tabular
17789 <lyxtabular version="3" rows="4" columns="6">
17791 <column alignment="center" valignment="top" leftline="true" width="0">
17792 <column alignment="center" valignment="top" leftline="true" width="0">
17793 <column alignment="center" valignment="top" leftline="true" width="0">
17794 <column alignment="center" valignment="top" leftline="true" width="0">
17795 <column alignment="center" valignment="top" leftline="true" width="0">
17796 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17797 <row topline="true">
17798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17847 <row topline="true">
17848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17897 <row topline="true">
17898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17922 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17947 <row topline="true" bottomline="true">
17948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17979 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18004 PIC16 port supports the standard command line arguments as supposed, with
18005 the exception of certain cases that will be mentioned in the following
18008 \labelwidthstring 00.00.0000
18021 \begin_inset LatexCommand \index{PIC16!Options!-\/-callee-saves}
18037 \labelwidthstring 00.00.0000
18050 \begin_inset LatexCommand \index{PIC16!Options!-\/-all-callee-saves}
18054 All function arguments are passed on stack by default.
18057 There is no need to specify this in the command line.
18059 \labelwidthstring 00.00.0000
18071 -fommit-frame-pointer
18072 \begin_inset LatexCommand \index{PIC16!Options!-\/-fommit-frame-pointer}
18076 Frame pointer will be omitted when the function uses no local variables.
18079 Port Specific Options
18080 \begin_inset LatexCommand \index{Options PIC16}
18087 The port specific options appear after the global options in the sdcc --help
18089 \layout Subsubsection
18094 General options enable certain port features and optimizations.
18096 \labelwidthstring 00.00.0000
18108 -stack-model=[model] Used in conjuction with the command above.
18109 Defines the stack model to be used, valid stack models are :
18112 \labelwidthstring 00.00.0000
18118 Selects small stack model.
18119 8 bit stack and frame pointers.
18120 Supports 256 bytes stack size.
18122 \labelwidthstring 00.00.0000
18128 Selects large stack model.
18129 16 bit stack and frame pointers.
18130 Supports 65536 bytes stack size.
18133 \labelwidthstring 00.00.0000
18145 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
18146 unitialized data variables with [kword].
18147 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
18149 \labelwidthstring 00.00.0000
18161 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
18162 Useful for bootloaders.
18164 \labelwidthstring 00.00.0000
18176 -asm= sets the full path and name of an external assembler to call.
18178 \labelwidthstring 00.00.0000
18190 -link= sets the full path and name of an external linker to call.
18192 \labelwidthstring 00.00.0000
18205 \begin_inset LatexCommand \index{PIC16!MPLAB}
18209 compatibility option.
18210 Currently only suppresses special gpasm directives.
18211 \layout Subsubsection
18213 Optimization Options
18215 \labelwidthstring 00.00.0000
18227 -optimize-goto Try to use (conditional) BRA instead of GOTO
18229 \labelwidthstring 00.00.0000
18241 -optimize-cmp Try to optimize some compares.
18243 \labelwidthstring 00.00.0000
18255 -optimize-df Analyze the dataflow of the generated code and improve it.
18257 \labelwidthstring 00.00.0000
18269 -obanksel=nn Set optimization level for inserting BANKSELs.
18274 \labelwidthstring 00.00.0000
18278 \labelwidthstring 00.00.0000
18280 1 checks previous used register and if it is the same then does not emit
18281 BANKSEL, accounts only for labels.
18283 \labelwidthstring 00.00.0000
18285 2 tries to check the location of (even different) symbols and removes BANKSELs
18286 if they are in the same bank.
18291 Important: There might be problems if the linker script has data sections
18292 across bank borders!
18294 \layout Subsubsection
18298 \labelwidthstring 00.00.0000
18310 -nodefaultlibs do not link default libraries when linking
18312 \labelwidthstring 00.00.0000
18324 -no-crt Don't link the default run-time modules
18326 \labelwidthstring 00.00.0000
18338 -use-crt= Use a custom run-time module instead of the defaults.
18339 \layout Subsubsection
18344 Debugging options enable extra debugging information in the output files.
18346 \labelwidthstring 00.00.0000
18358 -debug-xtra Similar to -
18369 \begin_inset LatexCommand \index{-\/-debug}
18373 , but dumps more information.
18375 \labelwidthstring 00.00.0000
18387 -debug-ralloc Force register allocator to dump <source>.d file with debugging
18389 <source> is the name of the file compiled.
18391 \labelwidthstring 00.00.0000
18403 -pcode-verbose Enable pcode debugging information in translation.
18405 \labelwidthstring 00.00.0000
18417 -denable-peeps Force the usage of peepholes.
18420 \labelwidthstring 00.00.0000
18432 -gstack Trace push/pops for stack pointer overflow
18434 \labelwidthstring 00.00.0000
18446 -call-tree dump call tree in .calltree file
18449 Enviromental Variables
18452 There is a number of enviromental variables that can be used when running
18453 SDCC to enable certain optimizations or force a specific program behaviour.
18454 these variables are primarily for debugging purposes so they can be enabled/dis
18458 Currently there is only two such variables available:
18460 \labelwidthstring 00.00.0000
18462 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
18463 bitfields is optimized by directly loading FSR0 with the address of the
18464 bitfield structure.
18465 Normally SDCC will cast the bitfield structure to a bitfield pointer and
18467 This step saves data ram and code space for functions that perform heavy
18470 80 bytes of code space are saved when compiling malloc.c with this option).
18473 \labelwidthstring 00.00.0000
18475 NO_REG_OPT do not perform pCode registers optimization.
18476 This should be used for debugging purposes.
18477 In some where bugs in the pcode optimizer are found, users can benefit
18478 from temporarily disabling the optimizer until the bug is fixed.
18481 Preprocessor Macros
18484 PIC16 port defines the following preprocessor macros while translating a
18489 \begin_inset Tabular
18490 <lyxtabular version="3" rows="6" columns="2">
18492 <column alignment="center" valignment="top" leftline="true" width="0">
18493 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18494 <row topline="true" bottomline="true">
18495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18512 <row topline="true">
18513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18526 Port identification
18530 <row topline="true">
18531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18554 Port identification (same as above)
18558 <row topline="true">
18559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18567 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18572 MCU Identification.
18577 is the microcontrol identification number, i.e.
18582 <row topline="true">
18583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18601 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18606 MCU Identification (same as above)
18610 <row topline="true" bottomline="true">
18611 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18619 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18624 nnn = SMALL or LARGE respectively according to the stack model used
18635 In addition the following macros are defined when calling assembler:
18639 \begin_inset Tabular
18640 <lyxtabular version="3" rows="4" columns="2">
18642 <column alignment="center" valignment="top" leftline="true" width="0">
18643 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18644 <row topline="true" bottomline="true">
18645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18662 <row topline="true">
18663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18676 MCU Identification.
18681 is the microcontrol identification number, i.e.
18686 <row topline="true">
18687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18695 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18700 nnn = SMALL or LARGE respectively according to the memory model used for
18705 <row topline="true" bottomline="true">
18706 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18719 nnn = SMALL or LARGE respectively according to the stack model used
18734 \begin_inset LatexCommand \index{PIC16}
18738 port uses the following directories for searching header files and libraries.
18742 \begin_inset Tabular
18743 <lyxtabular version="3" rows="3" columns="4">
18745 <column alignment="center" valignment="top" leftline="true" width="0">
18746 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18747 <column alignment="center" valignment="top" width="0">
18748 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18749 <row topline="true" bottomline="true">
18750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18766 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18774 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18783 <row topline="true">
18784 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18789 PREFIX/sdcc/include/pic16
18792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18797 PIC16 specific headers
18800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18808 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18817 <row topline="true" bottomline="true">
18818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18823 PREFIX/sdcc/lib/pic16
18826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18831 PIC16 specific libraries
18834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18859 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18866 PIC16 port currently supports the following pragmas:
18868 \labelwidthstring 00.00.0000
18871 \begin_inset LatexCommand \index{PIC16!Pragmas!\#pragma stack}
18876 \begin_inset LatexCommand \index{PIC16!stack}
18880 forces the code generator to initialize the stack & frame pointers at a
18882 This is an adhoc solution for cases where no STACK directive is available
18883 in the linker script or gplink is not instructed to create a stack section.
18885 The stack pragma should be used only once in a project.
18886 Multiple pragmas may result in indeterminate behaviour of the program.
18892 The old format (ie.
18893 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18894 cross page boundaries (or even exceed the available data RAM) and crash
18896 Make sure that stack does not cross page boundaries when using the SMALL
18902 The format is as follows:
18905 #pragma stack bottom_address [stack_size]
18912 is the lower bound of the stack section.
18913 The stack pointer initially will point at address (bottom_address+stack_size-1).
18921 /* initializes stack of 100 bytes at RAM address 0x200 */
18924 #pragma stack 0x200 100
18927 If the stack_size field is omitted then a stack is created with the default
18929 This size might be enough for most programs, but its not enough for operations
18930 with deep function nesting or excessive stack usage.
18932 \labelwidthstring 00.00.0000
18936 This pragma is deprecated.
18937 Its use will cause a warning message to be issued.
18943 \labelwidthstring 00.00.0000
18946 \begin_inset LatexCommand \index{PIC16!Pragmas!\#pragma code}
18950 place a function symbol at static FLASH address
18958 /* place function test_func at 0x4000 */
18961 #pragma code test_func 0x4000
18965 \labelwidthstring 00.00.0000
18967 library instructs the linker to use a library module.
18972 #pragma library module_name
18979 can be any library or object file (including its path).
18980 Note that there are four reserved keywords which have special meaning.
18985 \begin_inset Tabular
18986 <lyxtabular version="3" rows="6" columns="3">
18988 <column alignment="center" valignment="top" leftline="true" width="0">
18989 <column alignment="block" valignment="top" leftline="true" width="20page%">
18990 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18991 <row topline="true" bottomline="true">
18992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19017 <row topline="true">
19018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19033 ignore all library pragmas
19036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19047 <row topline="true">
19048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19058 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19079 <row topline="true">
19080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19095 link the Math libarary
19098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19111 <row topline="true">
19112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19127 link the I/O library
19130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19143 <row topline="true" bottomline="true">
19144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19159 link the debug library
19162 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19181 * is the device number, i.e.
19182 452 for PIC18F452 MCU.
19185 This feature allows for linking with specific libraries withoug having to
19186 explicit name them in the command line.
19191 keyword will reject all modules specified by the library pragma.
19193 \labelwidthstring 00.00.0000
19195 udata pragma udata instructs the compiler to emit code so that linker will
19196 place a variable at a specific memory bank
19204 /* places variable foo at bank2 */
19207 #pragma udata bank2 foo
19213 In order for this pragma to work extra SECTION directives should be added
19214 in the .lkr script.
19215 In the following example a sample .lkr file is shown:
19220 // Sample linker script for the PIC18F452 processor
19226 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
19229 CODEPAGE NAME=page START=0x2A END=0x7FFF
19232 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
19235 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
19238 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
19241 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
19244 ACCESSBANK NAME=accessram START=0x0 END=0x7F
19249 DATABANK NAME=gpr0 START=0x80 END=0xFF
19252 DATABANK NAME=gpr1 START=0x100 END=0x1FF
19255 DATABANK NAME=gpr2 START=0x200 END=0x2FF
19258 DATABANK NAME=gpr3 START=0x300 END=0x3FF
19261 DATABANK NAME=gpr4 START=0x400 END=0x4FF
19264 DATABANK NAME=gpr5 START=0x500 END=0x5FF
19267 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
19272 SECTION NAME=CONFIG ROM=config
19277 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
19280 SECTION NAME=bank1 RAM=gpr1 # should be added to link
19283 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
19286 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
19289 SECTION NAME=bank4 RAM=gpr4
19292 SECTION NAME=bank5 RAM=gpr5
19295 The linker will recognise the section name set in the pragma statement and
19296 will position the variable at the memory bank set with the RAM field at
19297 the SECTION line in the linker script file.
19301 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
19308 There is one main header file
19309 \begin_inset LatexCommand \index{PIC16!Header files}
19313 that can be included to the source files using the pic16 port.
19319 This header file contains the definitions for the processor special registers,
19320 so it is necessary if the source accesses them.
19321 It can be included by adding the following line in the beginning of the
19325 #include <pic18fregs.h>
19328 The specific microcontroller is selected within the pic18fregs.h automatically,
19329 so the same source can be used with a variety of devices.
19336 \begin_inset LatexCommand \index{PIC16!Libraries}
19341 \begin_inset LatexCommand \index{PIC16}
19345 port depends on are the microcontroller device libraries which contain
19346 the symbol definitions for the microcontroller special function registers.
19347 These libraries have the format pic18fxxxx.lib, where
19351 is the microcontroller identification number.
19352 The specific library is selected automatically by the compiler at link
19353 stage according to the selected device.
19356 Libraries are created with gplib which is part of the gputils package
19357 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
19362 \layout Subsubsection*
19364 Building the libraries
19367 Before using SDCC/pic16 there are some libraries that need to be compiled.
19368 This process is not done automatically by SDCC since not all users use
19369 SDCC for pic16 projects.
19370 So each user should compile the libraries separately.
19373 The steps to compile the pic16 libraries under Linux are:
19376 cd device/lib/pic16
19391 su -c 'make install' # install the libraries, you need the root password
19394 If you need to install the headers too, do:
19400 su -c 'make install' # install the headers, you need the root password
19403 There exist a special target to build the I/O libraries.
19404 This target is not automatically build because it will build the I/O library
19410 This way building will take quite a lot of time.
19411 Users are advised to edit the
19413 device/lib/pic16/pics.build
19415 file and then execute:
19424 The following memory models are supported by the PIC16 port:
19433 Memory model affects the default size of pointers within the source.
19434 The sizes are shown in the next table:
19438 \begin_inset Tabular
19439 <lyxtabular version="3" rows="3" columns="3">
19441 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19442 <column alignment="center" valignment="top" leftline="true" width="0">
19443 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19444 <row topline="true" bottomline="true">
19445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19450 Pointer sizes according to memory model
19453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19461 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19470 <row topline="true" bottomline="true">
19471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19479 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19496 <row topline="true" bottomline="true">
19497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19505 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
19513 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19529 It is advisable that all sources within a project are compiled with the
19531 If one wants to override the default memory model, this can be done by
19532 declaring a pointer as
19541 Far selects large memory model's pointers, while near selects small memory
19545 The standard device libraries (see
19546 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
19550 ) contain no reference to pointers, so they can be used with both memory
19558 \begin_inset LatexCommand \index{PIC16!stack}
19562 implementation for the PIC16 port uses two indirect registers, FSR1 and
19565 \labelwidthstring 00.00.0000
19567 FSR1 is assigned as stack pointer
19569 \labelwidthstring 00.00.0000
19571 FSR2 is assigned as frame pointer
19574 The following stack models are supported by the PIC16 port
19595 model means that only the FSRxL byte is used to access stack and frame,
19602 uses both FSRxL and FSRxH registers.
19603 The following table shows the stack/frame pointers sizes according to stack
19604 model and the maximum space they can address:
19608 \begin_inset Tabular
19609 <lyxtabular version="3" rows="3" columns="3">
19611 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19612 <column alignment="center" valignment="top" leftline="true" width="0">
19613 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19614 <row topline="true" bottomline="true">
19615 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19620 Stack & Frame pointer sizes according to stack model
19623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19640 <row topline="true">
19641 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19666 <row topline="true" bottomline="true">
19667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19703 stack model is currently not working properly throughout the code generator.
19704 So its use is not advised.
19705 Also there are some other points that need special care:
19710 Do not create stack sections with size more than one physical bank (that
19714 Stack sections should no cross physical bank limits (i.e.
19715 #pragma stack 0x50 0x100)
19718 These limitations are caused by the fact that only FSRxL is modified when
19719 using SMALL stack model, so no more than 256 bytes of stack can be used.
19720 This problem will disappear after LARGE model is fully implemented.
19726 In addition to the standard SDCC function keywords, PIC16 port makes available
19729 \labelwidthstring 00.00.0000
19732 \begin_inset LatexCommand \index{PIC16!wparam}
19736 Use the WREG to pass one byte of the first function argument.
19737 This improves speed but you may not use this for functions with arguments
19738 that are called via function pointers, otherwise the first byte of the
19739 first parameter will get lost.
19743 void func_wparam(int a) wparam
19749 /* WREG hold the lower part of a */
19752 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19762 This keyword replaces the deprecated wparam pragma.
19764 \labelwidthstring 00.00.0000
19767 \begin_inset LatexCommand \index{PIC16!shadowregs}
19771 When entering/exiting an ISR, it is possible to take advantage of the PIC18F
19772 hardware shadow registers which hold the values of WREG, STATUS and BSR
19774 This can be done by adding the keyword
19782 keyword in the function's header.
19785 void isr_shadow(void) shadowregs interrupt 1
19801 instructs the code generator not to store/restore WREG, STATUS, BSR when
19802 entering/exiting the ISR.
19805 Function return values
19808 Return values from functions are placed to the appropriate registers following
19809 a modified Microchip policy optimized for SDCC.
19810 The following table shows these registers:
19814 \begin_inset Tabular
19815 <lyxtabular version="3" rows="6" columns="2">
19817 <column alignment="center" valignment="top" leftline="true" width="0">
19818 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19819 <row topline="true" bottomline="true">
19820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19828 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19833 destination register
19837 <row topline="true">
19838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19855 <row topline="true">
19856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19873 <row topline="true">
19874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19891 <row topline="true">
19892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19900 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19905 FSR0L:PRODH:PRODL:WREG
19909 <row topline="true" bottomline="true">
19910 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19918 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19923 on stack, FSR0 points to the beginning
19938 \begin_inset LatexCommand \index{PIC16!interrupt}
19942 service routine (ISR) is declared using the
19949 void isr(void) interrupt
19967 is the interrupt number, which for PIC18F devices can be:
19971 \begin_inset Tabular
19972 <lyxtabular version="3" rows="4" columns="3">
19974 <column alignment="center" valignment="top" leftline="true" width="0">
19975 <column alignment="center" valignment="top" leftline="true" width="0">
19976 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19977 <row topline="true" bottomline="true">
19978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20001 Interrupt Vector Address
20005 <row topline="true">
20006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20031 <row topline="true">
20032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20063 HIGH priority interrupts
20066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20075 <row topline="true" bottomline="true">
20076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20089 LOW priority interrupts
20092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20108 When generating assembly code for ISR the code generator places a
20114 Interrupt Vector Address
20116 which points at the genetated ISR.
20117 This single GOTO instruction is part of an automatically generated
20119 interrupt entry point
20122 The actuall ISR code is placed as normally would in the code space.
20123 Upon interrupt request, the GOTO instruction is executed which jumps to
20125 When declaring interrupt functions as _naked this GOTO instruction is
20130 The whole interrupt functions is therefore placed at the Interrupt Vector
20131 Address of the specific interrupt.
20132 This is not a problem for the LOW priority interrupts, but it is a problem
20133 for the RESET and the HIGH priority interrupts because code may be written
20134 at the next interrupt´s vector address and cause undeterminate program
20135 behaviour if that interrupt is raised.
20141 This is not a problem when
20144 this is a HIGH interrupt ISR and LOW interrupts are
20151 when the ISR is small enough not to reach the next interrupt´s vector address.
20161 is possible to be omitted.
20162 This way a function is generated similar to an ISR, but it is not assigned
20166 When entering an interrupt, currently the PIC16
20167 \begin_inset LatexCommand \index{PIC16}
20171 port automatically saves the following registers:
20183 PROD (PRODL and PRODH)
20186 FSR0 (FSR0L and FSR0H)
20189 These registers are restored upon return from the interrupt routine.
20195 NOTE that when the _naked attribute is specified for an interrupt routine,
20196 then NO registers are stored or restored.
20205 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
20206 There are 3 types of generic pointers currently implemented data, code
20207 and eeprom pointers.
20208 They are differentiated by the value of the 7th and 6th bits of the upper
20213 \begin_inset Tabular
20214 <lyxtabular version="3" rows="5" columns="5">
20216 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20217 <column alignment="center" valignment="top" width="0">
20218 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20219 <column alignment="center" valignment="top" width="0">
20220 <column alignment="left" valignment="top" rightline="true" width="0">
20221 <row topline="true" bottomline="true">
20222 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20251 rest of the pointer
20254 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20263 <row topline="true" bottomline="true">
20264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20288 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20297 uuuuuu uuuuxxxx xxxxxxxx
20300 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20305 a 12-bit data pointer in data RAM memory
20309 <row bottomline="true">
20310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20343 uxxxxx xxxxxxxx xxxxxxxx
20346 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20351 a 21-bit code pointer in FLASH memory
20355 <row bottomline="true">
20356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20372 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20389 uuuuuu uuuuuuxx xxxxxxxx
20392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20397 a 10-bit eeprom pointer in EEPROM memory
20401 <row bottomline="true">
20402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20435 xxxxxx xxxxxxxx xxxxxxxx
20438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20443 unimplemented pointer type
20454 Generic pointer are read and written with a set of library functions which
20455 read/write 1, 2, 3, 4 bytes.
20459 \layout Subsubsection
20461 Standard I/O Streams
20468 the type FILE is defined as:
20471 typedef char * FILE;
20474 This type is the stream type implemented I/O in the PIC18F devices.
20475 Also the standard input and output streams are declared in stdio.h:
20478 extern FILE * stdin;
20481 extern FILE * stdout;
20484 The FILE type is actually a generic pointer which defines one more type
20485 of generic pointers, the
20490 This new type has the format:
20494 \begin_inset Tabular
20495 <lyxtabular version="3" rows="2" columns="7">
20497 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20498 <column alignment="center" valignment="top" width="0">
20499 <column alignment="center" valignment="top" leftline="true" width="0">
20500 <column alignment="center" valignment="top" leftline="true" width="0">
20501 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20502 <column alignment="center" valignment="top" width="0">
20503 <column alignment="left" valignment="top" rightline="true" width="0">
20504 <row topline="true" bottomline="true">
20505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20545 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20550 rest of the pointer
20553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20562 <row topline="true" bottomline="true">
20563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20620 upper byte high nubble is 0x2n, the rest are zeroes
20631 Currently implemented there are 3 types of streams defined:
20635 \begin_inset Tabular
20636 <lyxtabular version="3" rows="4" columns="4">
20638 <column alignment="center" valignment="top" leftline="true" width="0">
20639 <column alignment="center" valignment="top" leftline="true" width="0">
20640 <column alignment="center" valignment="top" leftline="true" width="0">
20641 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20642 <row topline="true" bottomline="true">
20643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20659 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20676 <row topline="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">
20695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20703 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20708 Writes/Reads characters via the USART peripheral
20712 <row topline="true">
20713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20744 Writes/Reads characters via the MSSP peripheral
20748 <row topline="true" bottomline="true">
20749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20780 Writes/Reads characters via used defined functions
20791 The stream identifiers are declared as macros in the stdio.h header.
20794 In the libc library there exist the functions that are used to write to
20795 each of the above streams.
20798 \labelwidthstring 00.00.0000
20810 _stream_usart_putchar writes a character at the USART stream
20812 \labelwidthstring 00.00.0000
20824 _stream_mssp_putchar writes a character at the MSSP stream
20826 \labelwidthstring 00.00.0000
20828 putchar dummy function.
20829 This writes a character to a user specified manner.
20832 In order to increase performance
20836 is declared in stdio.h as having its parameter in WREG (it has the wparam
20838 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20839 in a user-friendly way.
20844 is the name of the variable that holds the character to print.
20845 An example follows:
20848 #include <pic18fregs.h>
20860 PORTA = c; /* dump character c to PORTA */
20873 stdout = STREAM_USER; /* this is not necessary, since stdout points
20876 * by default to STREAM_USER */
20879 printf (¨This is a printf test
20887 \layout Subsubsection
20892 PIC16 contains an implementation of the printf-family of functions.
20893 There exist the following functions:
20896 extern unsigned int sprintf(char *buf, char *fmt, ...);
20899 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20904 extern unsigned int printf(char *fmt, ...);
20907 extern unsigned int vprintf(char *fmt, va_lista ap);
20912 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20915 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20918 For sprintf and vsprintf
20922 should normally be a data pointer where the resulting string will be placed.
20923 No range checking is done so the user should allocate the necessery buffer.
20924 For fprintf and vfprintf
20928 should be a stream pointer (i.e.
20929 stdout, STREAM_MSSP, etc...).
20930 \layout Subsubsection
20935 The PIC18F family of microcontrollers supports a number of interrupt sources.
20936 A list of these interrupts is shown in the following table:
20940 \begin_inset Tabular
20941 <lyxtabular version="3" rows="11" columns="4">
20943 <column alignment="left" valignment="top" leftline="true" width="0">
20944 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20945 <column alignment="left" valignment="top" leftline="true" width="0">
20946 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20947 <row topline="true" bottomline="true">
20948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20956 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20981 <row topline="true">
20982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20990 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20995 PORTB change interrupt
20998 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21011 EEPROM/FLASH write complete interrupt
21015 <row topline="true">
21016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21024 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21029 INT0 external interrupt
21032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21045 Bus collision interrupt
21049 <row topline="true">
21050 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21058 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21063 INT1 external interrupt
21066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21079 Low voltage detect interrupt
21083 <row topline="true">
21084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21097 INT2 external interrupt
21100 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21108 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21113 Parallel slave port interrupt
21117 <row topline="true">
21118 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21131 CCP1 module interrupt
21134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21147 AD convertion complete interrupt
21151 <row topline="true">
21152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21165 CCP2 module interrupt
21168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21181 USART receive interrupt
21185 <row topline="true">
21186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21199 TMR0 overflow interrupt
21202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21215 USART transmit interrupt
21219 <row topline="true">
21220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21233 TMR1 overflow interrupt
21236 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21249 SSP receive/transmit interrupt
21253 <row topline="true">
21254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21262 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21267 TMR2 matches PR2 interrupt
21270 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21277 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21285 <row topline="true" bottomline="true">
21286 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21294 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21299 TMR3 overflow interrupt
21302 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21324 The prototypes for these names are defined in the header file
21331 In order to simplify signal handling, a number of macros is provided:
21333 \labelwidthstring 00.00.0000
21335 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
21336 high priority interrupts.
21341 is the function name to use.
21343 \labelwidthstring 00.00.0000
21345 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
21346 low priority interrupt.
21351 is the function name to use.
21353 \labelwidthstring 00.00.0000
21355 DEF_HANDLER(sig,handler) define a handler for signal
21359 \labelwidthstring 00.00.0000
21361 END_DEF end the declaration of the dispatch table.
21364 Additionally there are two more macros to simplify the declaration of the
21367 \labelwidthstring 00.00.0000
21371 SIGHANDLER(handler)
21373 this declares the function prototype for the
21379 \labelwidthstring 00.00.0000
21381 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
21384 An example of using the macros above is shown below:
21387 #include <pic18fregs.h>
21390 #include <signal.h>
21394 DEF_INTHIGH(high_int)
21397 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
21400 DEF_HANDLER(SIG_BCOL, _bcol_handler)
21407 SIGHANDLER(_tmr0_handler)
21413 /* action to be taken when timer 0 overflows */
21420 SIGHANDLERNAKED(_bcol_handler)
21429 /* action to be taken when bus collision occurs */
21445 Special care should be taken when using the above scheme:
21448 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
21451 when declaring SIGHANDLERNAKED handler never forget to use
21455 for proper returning.
21461 Here you can find some general tips for compiling programs with SDCC/pic16.
21462 \layout Subsubsection
21468 \begin_inset LatexCommand \index{PIC16!stack}
21472 size (that is 64 bytes) probably is enough for many programs.
21473 One must take care that when there are many levels of function nesting,
21474 or there is excessive usage of stack, its size should be extended.
21475 An example of such a case is the printf/sprintf family of functions.
21476 If you encounter problems like not being able to print integers, then you
21477 need to set the stack size around the maximum (256 for small stack model).
21478 The following diagram shows what happens when calling printf to print an
21482 printf () --> ltoa () --> ultoa () --> divschar ()
21485 It is should be understood that stack is easily consumed when calling complicate
21487 Using command line arguments like -
21497 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
21499 Other ways to reduce stack usage may exist.
21505 There are several approaches to debugging your code.
21506 This chapter is meant to show your options and to give detail on some of
21511 When writing your code:
21514 write your code with debugging in mind (avoid duplicating code, put conceptually
21515 similar variables into structs, use structured code, have strategic points
21516 within your code where all variables are consistent, ...)
21519 run a syntax-checking tool like splint
21520 \begin_inset LatexCommand \index{splint (syntax checking tool)}
21525 \begin_inset LatexCommand \index{lint (syntax checking tool)}
21540 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21547 for the high level code use a C-compiler (like f.e.
21548 GCC) to compile run and debug the code on your host.
21560 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21564 ) on howto handle syntax extensions like __xdata, __at(), ...
21568 use another C-compiler to compile code for your target.
21569 Always an option but not recommended:) And not very likely to help you.
21570 If you seriously consider walking this path you should at least occasionally
21571 check portability of your code.
21572 Most commercial compiler vendors will offer an evaluation version so you
21573 can test compile your code or snippets of your code.
21576 Debugging on a simulator:
21579 there is a separate section about SDCDB (section
21580 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21587 or (8051 specific) use a freeware/commercial simulator which interfaces
21589 \begin_inset LatexCommand \index{AOMF, AOMF51}
21594 \begin_inset LatexCommand \ref{OMF file}
21598 ) optionally generated by SDCC.
21601 Debugging On-target:
21604 use a MCU port pin to serially output debug data to the RS232 port of your
21606 You'll probably want some level shifting device typically involving a MAX232
21608 If the hardware serial port of the MCU is not available search for 'Software
21609 UART' in your favourite search machine.
21612 use an on-target monitor.
21613 In this context a monitor is a small program which usually accepts commands
21614 via a serial line and allows to set program counter, to single step through
21615 a program and read/write memory locations.
21616 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21618 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21625 toggle MCU port pins at strategic points within your code and use an oscilloscop
21629 digital oscilloscope
21632 \begin_inset LatexCommand \index{Oscilloscope}
21636 with deep trace memory is really helpful especially if you have to debug
21637 a realtime application.
21638 If you need to monitor more pins than your oscilloscope provides you can
21639 sometimes get away with a small R-2R network.
21640 On a single channel oscilloscope you could f.e.
21641 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21643 \begin_inset Formula $\Omega$
21646 resistor and the other one by a 5\SpecialChar ~
21648 \begin_inset Formula $\Omega$
21651 resistor to the oscilloscope probe (check output drive capability of the
21652 pins you want to monitor).
21653 If you need to monitor many more pins a
21673 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21678 Usually very expensive.
21679 And very nice to have too.
21680 And usually locks you (for years...) to the devices the ICE can emulate.
21684 use a remote debugger.
21685 In most 8-bit systems the symbol information is not available on the target,
21686 and a complete debugger is too bulky for the target system.
21687 Therefore usually a debugger on the host system connects to an on-target
21688 debugging stub which accepts only primitive commands.
21691 Terms to enter into your favourite search engine could be 'remote debugging',
21692 'gdb stub' or 'inferior debugger'.
21696 use an on target hardware debugger.
21697 Some of the more modern MCUs include hardware support for setting break
21698 points and monitoring/changing variables by using dedicated hardware pins.
21699 This facility doesn't require additional code to run on the target and
21704 doesn't affect runtime behaviour until a breakpoint is hit.
21705 For the mcs51 most hardware debuggers use the AOMF
21706 \begin_inset LatexCommand \index{AOMF, AOMF51}
21711 \begin_inset LatexCommand \ref{OMF file}
21722 if you are not familiar with any of the following terms you're likely to
21723 run into problems rather sooner than later:
21740 As an embedded programmer you
21744 to know them so why not look them up
21748 you have problems?)
21751 tell someone else about your problem (actually this is a surprisingly effective
21752 means to hunt down the bug even if the listener is not familiar with your
21754 As 'failure to communicate' is probably one of the job-induced deformations
21755 of an embedded programmer this is highly encouraged.
21758 Debugging with SDCDB
21759 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21764 \begin_inset LatexCommand \index{SDCDB (debugger)}
21771 SDCC is distributed with a source level debugger
21772 \begin_inset LatexCommand \index{Debugger}
21777 The debugger uses a command line interface, the command repertoire of the
21778 debugger has been kept as close to gdb
21779 \begin_inset LatexCommand \index{gdb}
21783 (the GNU debugger) as possible.
21784 The configuration and build process is part of the standard compiler installati
21785 on, which also builds and installs the debugger in the target directory
21786 specified during configuration.
21787 The debugger allows you debug BOTH at the C source and at the ASM source
21791 Compiling for Debugging
21805 \begin_inset LatexCommand \index{-\/-debug}
21809 option must be specified for all files for which debug information is to
21811 The compiler generates a .adb file for each of these files.
21812 The linker creates the .cdb
21813 \begin_inset LatexCommand \index{<file>.cdb}
21818 \begin_inset LatexCommand \index{<file>.adb}
21822 files and the address information.
21823 This .cdb is used by the debugger.
21826 How the Debugger Works
21839 -debug option is specified the compiler generates extra symbol information
21840 some of which are put into the assembler source and some are put into the
21842 Then the linker creates the .cdb file from the individual .adb files with
21843 the address information for the symbols.
21844 The debugger reads the symbolic information generated by the compiler &
21845 the address information generated by the linker.
21846 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21847 execution is controlled by the debugger.
21848 When a command is issued for the debugger, it translates it into appropriate
21849 commands for the simulator.
21850 (Currently SDCDM only connects to the simulator but
21855 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21859 is an effort to connect directly to the hardware.)
21862 Starting the Debugger SDCDB
21865 The debugger can be started using the following command line.
21866 (Assume the file you are debugging has the file name foo).
21880 The debugger will look for the following files.
21883 foo.c - the source file.
21886 foo.cdb - the debugger symbol information file.
21889 foo.ihx - the Intel hex format
21890 \begin_inset LatexCommand \index{Intel hex format}
21897 SDCDB Command Line Options
21910 -directory=<source file directory> this option can used to specify the directory
21912 The debugger will look into the directory list specified for source, cdb
21914 The items in the directory list must be separated by ':', e.g.
21915 if the source files can be in the directories /home/src1 and /home/src2,
21926 -directory option should be -
21936 -directory=/home/src1:/home/src2.
21937 Note there can be no spaces in the option.
21941 -cd <directory> - change to the <directory>.
21944 -fullname - used by GUI front ends.
21947 -cpu <cpu-type> - this argument is passed to the simulator please see the
21948 simulator docs for details.
21951 -X <Clock frequency > this options is passed to the simulator please see
21952 the simulator docs for details.
21955 -s <serial port file> passed to simulator see the simulator docs for details.
21958 -S <serial in,out> passed to simulator see the simulator docs for details.
21961 -k <port number> passed to simulator see the simulator docs for details.
21964 SDCDB Debugger Commands
21967 As mentioned earlier the command interface for the debugger has been deliberatel
21968 y kept as close the GNU debugger gdb, as possible.
21969 This will help the integration with existing graphical user interfaces
21970 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21971 If you use a graphical user interface for the debugger you can skip this
21973 \layout Subsubsection*
21975 break [line | file:line | function | file:function]
21978 Set breakpoint at specified line or function:
21987 sdcdb>break foo.c:100
21989 sdcdb>break funcfoo
21991 sdcdb>break foo.c:funcfoo
21992 \layout Subsubsection*
21994 clear [line | file:line | function | file:function ]
21997 Clear breakpoint at specified line or function:
22006 sdcdb>clear foo.c:100
22008 sdcdb>clear funcfoo
22010 sdcdb>clear foo.c:funcfoo
22011 \layout Subsubsection*
22016 Continue program being debugged, after breakpoint.
22017 \layout Subsubsection*
22022 Execute till the end of the current function.
22023 \layout Subsubsection*
22028 Delete breakpoint number 'n'.
22029 If used without any option clear ALL user defined break points.
22030 \layout Subsubsection*
22032 info [break | stack | frame | registers ]
22035 info break - list all breakpoints
22038 info stack - show the function call stack.
22041 info frame - show information about the current execution frame.
22044 info registers - show content of all registers.
22045 \layout Subsubsection*
22050 Step program until it reaches a different source line.
22051 Note: pressing <return> repeats the last command.
22052 \layout Subsubsection*
22057 Step program, proceeding through subroutine calls.
22058 \layout Subsubsection*
22063 Start debugged program.
22064 \layout Subsubsection*
22069 Print type information of the variable.
22070 \layout Subsubsection*
22075 print value of variable.
22076 \layout Subsubsection*
22081 load the given file name.
22082 Note this is an alternate method of loading file for debugging.
22083 \layout Subsubsection*
22088 print information about current frame.
22089 \layout Subsubsection*
22094 Toggle between C source & assembly source.
22095 \layout Subsubsection*
22097 ! simulator command
22100 Send the string following '!' to the simulator, the simulator response is
22102 Note the debugger does not interpret the command being sent to the simulator,
22103 so if a command like 'go' is sent the debugger can loose its execution
22104 context and may display incorrect values.
22105 \layout Subsubsection*
22112 My name is Bobby Brown"
22115 Interfacing SDCDB with DDD
22118 The screenshot was converted from png to eps with:
22119 \begin_inset Quotes sld
22122 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
22123 \begin_inset Quotes srd
22126 which produces a pretty compact eps file which is free from compression
22130 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
22131 as this broke the build system on Sourceforge (pdf-file was broken.
22132 pdflatex does not accept eps files).
22150 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.png}
22156 shows a screenshot of a debugging session with DDD
22157 \begin_inset LatexCommand \index{DDD (debugger)}
22161 (Unix only) on a simulated 8032.
22162 The debugging session might not run as smoothly as the screenshot suggests.
22163 The debugger allows setting of breakpoints, displaying and changing variables,
22164 single stepping through C and assembler code.
22167 The source was compiled with
22190 -debug ddd_example.c
22203 and DDD was invoked with
22210 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
22213 Interfacing SDCDB with XEmacs
22214 \begin_inset LatexCommand \index{XEmacs}
22219 \begin_inset LatexCommand \index{Emacs}
22226 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
22227 sdcdb.el and sdcdbsrc.el.
22228 These two files can be found in the $(prefix)/bin directory after the installat
22230 These files need to be loaded into XEmacs for the interface to work.
22231 This can be done at XEmacs startup time by inserting the following into
22232 your '.xemacs' file (which can be found in your HOME directory):
22238 (load-file sdcdbsrc.el)
22244 .xemacs is a lisp file so the () around the command is REQUIRED.
22245 The files can also be loaded dynamically while XEmacs is running, set the
22246 environment variable 'EMACSLOADPATH' to the installation bin directory
22247 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
22248 To start the interface enter the following command:
22262 You will prompted to enter the file name to be debugged.
22267 The command line options that are passed to the simulator directly are bound
22268 to default values in the file sdcdbsrc.el.
22269 The variables are listed below, these values maybe changed as required.
22272 sdcdbsrc-cpu-type '51
22275 sdcdbsrc-frequency '11059200
22278 sdcdbsrc-serial nil
22281 The following is a list of key mapping for the debugger interface.
22292 ;;key\SpecialChar ~
22306 binding\SpecialChar ~
22330 ;;---\SpecialChar ~
22344 -------\SpecialChar ~
22386 sdcdb-next-from-src\SpecialChar ~
22414 sdcdb-back-from-src\SpecialChar ~
22442 sdcdb-cont-from-src\SpecialChar ~
22452 SDCDB continue command
22470 sdcdb-step-from-src\SpecialChar ~
22498 sdcdb-whatis-c-sexp\SpecialChar ~
22508 SDCDB ptypecommand for data at
22575 sdcdbsrc-delete\SpecialChar ~
22589 SDCDB Delete all breakpoints if no arg
22638 given or delete arg (C-u arg x)
22656 sdcdbsrc-frame\SpecialChar ~
22671 SDCDB Display current frame if no arg,
22720 given or display frame arg
22787 sdcdbsrc-goto-sdcdb\SpecialChar ~
22797 Goto the SDCDB output buffer
22815 sdcdb-print-c-sexp\SpecialChar ~
22826 SDCDB print command for data at
22893 sdcdbsrc-goto-sdcdb\SpecialChar ~
22903 Goto the SDCDB output buffer
22921 sdcdbsrc-mode\SpecialChar ~
22937 Toggles Sdcdbsrc mode (turns it off)
22952 sdcdb-finish-from-src\SpecialChar ~
22960 SDCDB finish command
22975 sdcdb-break\SpecialChar ~
22993 Set break for line with point
23008 sdcdbsrc-mode\SpecialChar ~
23024 Toggle Sdcdbsrc mode
23039 sdcdbsrc-srcmode\SpecialChar ~
23062 Here are a few guidelines that will help the compiler generate more efficient
23063 code, some of the tips are specific to this compiler others are generally
23064 good programming practice.
23067 Use the smallest data type to represent your data-value.
23068 If it is known in advance that the value is going to be less than 256 then
23069 use an 'unsigned char' instead of a 'short' or 'int'.
23070 Please note, that ANSI C requires both signed and unsigned chars to be
23071 promoted to 'signed int'
23072 \begin_inset LatexCommand \index{promotion to signed int}
23077 \begin_inset Marginal
23088 before doing any operation.
23090 \begin_inset LatexCommand \index{type promotion}
23095 \begin_inset LatexCommand \label{type promotion}
23099 can be omitted, if the result is the same.
23100 The effect of the promotion rules together with the sign-extension is often
23107 unsigned char uc = 0xfe;
23109 if (uc * uc < 0) /* this is true! */
23128 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
23138 (unsigned char) -12 / (signed char) -3 = ...
23141 No, the result is not 4:
23146 (int) (unsigned char) -12 / (int) (signed char) -3 =
23148 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
23150 (int) 0x00f4 / (int) 0xfffd =
23152 (int) 0x00f4 / (int) 0xfffd =
23154 (int) 244 / (int) -3 =
23156 (int) -81 = (int) 0xffaf;
23159 Don't complain, that gcc gives you a different result.
23160 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
23161 Therefore the results are different.
23164 \begin_inset Quotes sld
23168 \begin_inset Quotes srd
23176 If well-defined overflow characteristics are important and negative values
23177 are not, or if you want to steer clear of sign-extension problems when
23178 manipulating bits or bytes, use one of the corresponding unsigned types.
23179 (Beware when mixing signed and unsigned values in expressions, though.)
23181 Although character types (especially unsigned char) can be used as "tiny"
23182 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
23183 ble sign extension and increased code size.
23187 Use unsigned when it is known in advance that the value is not going to
23189 This helps especially if you are doing division or multiplication, bit-shifting
23190 or are using an array index.
23193 NEVER jump into a LOOP.
23196 Declare the variables to be local
23197 \begin_inset LatexCommand \index{local variables}
23201 whenever possible, especially loop control variables (induction).
23204 Since the compiler does not always do implicit integral promotion, the programme
23205 r should do an explicit cast when integral promotion is required.
23208 Reducing the size of division, multiplication & modulus operations can reduce
23209 code size substantially.
23210 Take the following code for example.
23216 foobar(unsigned int p1, unsigned char ch)
23224 unsigned char ch1 = p1 % ch ;
23235 For the modulus operation the variable ch will be promoted to unsigned int
23236 first then the modulus operation will be performed (this will lead to a
23237 call to support routine _moduint()), and the result will be casted to a
23239 If the code is changed to
23244 foobar(unsigned int p1, unsigned char ch)
23252 unsigned char ch1 = (unsigned char)p1 % ch ;
23263 It would substantially reduce the code generated (future versions of the
23264 compiler will be smart enough to detect such optimization opportunities).
23268 Have a look at the assembly listing to get a
23269 \begin_inset Quotes sld
23273 \begin_inset Quotes srd
23276 for the code generation.
23279 Porting code from or to other compilers
23280 \begin_inset LatexCommand \label{sec:Porting-code-to-other-compilers}
23287 check whether endianness of the compilers differs and adapt where needed.
23290 check the device specific header files
23291 \begin_inset LatexCommand \index{Header files}
23296 \begin_inset LatexCommand \index{Include files}
23300 for compiler specific syntax.
23301 Eventually include the file <compiler.h
23302 \begin_inset LatexCommand \index{compiler.h (include file)}
23306 > to allow using common header files.
23309 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewvc/sdcc/trunk/sdcc/device/include/mcs51/P89c51RD2.h?view=markup}
23316 check whether the startup code contains the correct initialization (watchdog,
23320 check whether the sizes of short, int, long match.
23323 check if some 16 or 32 bit hardware registers require a specific addressing
23324 order (least significant or most significant byte first) and adapt if needed
23333 relate to time and not to lower/upper memory location here, so this is
23338 the same as endianness).
23341 check whether the keyword
23345 is used where needed.
23346 The compilers might differ in their optimization characteristics (as different
23347 versions of the same compiler might also use more clever optimizations
23348 this is good idea anyway).
23351 check that the compilers are not told to supress warnings.
23354 check and convert compiler specific extensions (interrupts, memory areas,
23358 check for differences in type promotion.
23359 Especially check for math operations on
23368 For the sake of C99 compatibility SDCC will probably promote these to
23372 more often than other compilers.
23373 Eventually insert explicit casts to
23382 Also check that the ~\SpecialChar ~
23384 \begin_inset LatexCommand \index{\~\/ Operator}
23391 \begin_inset LatexCommand \index{bit}
23397 variables, use the !\SpecialChar ~
23400 \begin_inset LatexCommand \ref{type promotion}
23405 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
23412 check the assembly code generated for interrupt routines (f.e.
23413 for calls to possibly non-reentrant library functions).
23416 check whether timing loops result in proper timing (or preferably consider
23417 a rewrite of the code with timer based delays instead).
23420 check for differences in printf parameters (some compilers push (va_arg
23421 \begin_inset LatexCommand \index{vararg, va\_arg}
23425 ) char variables as
23429 others push them as
23435 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
23442 check the resulting memory map
23443 \begin_inset LatexCommand \index{Memory map}
23448 Usage of different memory spaces: code, stack, data (for mcs51/ds390 additional
23449 ly idata, pdata, xdata).
23450 Eventually check if unexpected library functions are included.
23454 \begin_inset LatexCommand \index{Tools}
23458 included in the distribution
23462 \begin_inset Tabular
23463 <lyxtabular version="3" rows="12" columns="3">
23465 <column alignment="left" valignment="top" leftline="true" width="0pt">
23466 <column alignment="left" valignment="top" leftline="true" width="0pt">
23467 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23468 <row topline="true" bottomline="true">
23469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23485 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23494 <row topline="true">
23495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23508 Simulator for various architectures
23511 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23520 <row topline="true">
23521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23535 \begin_inset LatexCommand \index{Header files}
23540 \begin_inset LatexCommand \index{Include files}
23547 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23552 sdcc/support/scripts
23556 <row topline="true">
23557 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23565 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23570 header file conversion
23573 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23578 sdcc/support/scripts
23582 <row topline="true">
23583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23591 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23599 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23617 <row topline="true">
23618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23634 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23652 <row topline="true">
23653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23687 <row topline="true">
23688 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23696 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23704 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23722 <row topline="true">
23723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23757 <row topline="true">
23758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23774 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23792 <row topline="true">
23793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23827 <row topline="true" bottomline="true">
23828 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23842 \begin_inset LatexCommand \index{packihx (tool)}
23849 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23877 \begin_inset LatexCommand \index{Documentation}
23881 included in the distribution
23885 \begin_inset Tabular
23886 <lyxtabular version="3" rows="10" columns="2">
23888 <column alignment="left" valignment="top" leftline="true" width="0">
23889 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23890 <row topline="true" bottomline="true">
23891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23899 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23904 Where to get / filename
23908 <row topline="true">
23909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23914 SDCC Compiler User Guide
23917 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23922 You're reading it right now
23926 <row topline="true">
23927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23935 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23944 <row topline="true">
23945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23951 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23956 \begin_inset LatexCommand \index{Assembler documentation}
23960 Assemblers and ASLINK
23961 \begin_inset LatexCommand \index{aslink}
23966 \begin_inset LatexCommand \index{Linker documentation}
23973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23978 sdcc/as/doc/asxhtm.html
23982 <row topline="true">
23983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23988 SDCC regression test
23989 \begin_inset LatexCommand \index{Regression test}
23996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24001 sdcc/doc/test_suite_spec.pdf
24005 <row topline="true">
24006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24023 <row topline="true">
24024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24029 Notes on debugging with SDCDB
24030 \begin_inset LatexCommand \index{SDCDB (debugger)}
24037 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24042 sdcc/debugger/README
24046 <row topline="true">
24047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24052 Software simulator for microcontrollers
24055 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24082 <row topline="true">
24083 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24088 Temporary notes on the pic16
24089 \begin_inset LatexCommand \index{PIC16}
24096 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24101 sdcc/src/pic16/NOTES
24105 <row topline="true" bottomline="true">
24106 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24111 SDCC internal documentation (debugging file format)
24114 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24150 Related open source tools
24151 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
24156 \begin_inset LatexCommand \index{Related tools}
24164 \begin_inset Tabular
24165 <lyxtabular version="3" rows="14" columns="3">
24167 <column alignment="left" valignment="top" leftline="true" width="0pt">
24168 <column alignment="block" valignment="top" leftline="true" width="30line%">
24169 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24170 <row topline="true" bottomline="true">
24171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24187 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24196 <row topline="true">
24197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24203 \begin_inset LatexCommand \index{gpsim (pic simulator)}
24210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24218 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24224 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
24232 <row topline="true">
24233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24239 \begin_inset LatexCommand \index{gputils (pic tools)}
24246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24254 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24260 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
24268 <row topline="true">
24269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24291 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
24299 <row topline="true">
24300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24313 Tools for Silicon Laboratories JTAG debug adapter, partly based on SDCDB
24317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24323 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
24331 <row topline="true">
24332 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24338 \begin_inset LatexCommand \index{indent (source formatting tool)}
24345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24350 Formats C source - Master of the white spaces
24353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24359 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
24367 <row topline="true">
24368 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24374 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
24381 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24386 Object file conversion, checksumming, ...
24389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24395 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
24403 <row topline="true">
24404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24410 \begin_inset LatexCommand \index{objdump (tool)}
24417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24422 Object file conversion, ...
24425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24430 Part of binutils (should be there anyway)
24434 <row topline="true">
24435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24448 8051 monitor (hex up-/download, single step, disassemble)
24451 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24457 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
24465 <row topline="true">
24466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24472 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
24479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24484 Source code documentation system
24487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24493 \begin_inset LatexCommand \url{http://www.doxygen.org}
24501 <row topline="true">
24502 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24515 IDE (has anyone tried integrating SDCC & SDCDB? Unix only)
24518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24524 \begin_inset LatexCommand \url{http://www.kdevelop.org}
24532 <row topline="true">
24533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24546 8051 monitor (hex up-/download, single step, disassemble)
24549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24555 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
24563 <row topline="true">
24564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24570 \begin_inset LatexCommand \index{splint (syntax checking tool)}
24577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24582 Statically checks c sources (see
24583 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
24590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24596 \begin_inset LatexCommand \url{http://www.splint.org}
24604 <row topline="true" bottomline="true">
24605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24611 \begin_inset LatexCommand \index{ddd (debugger)}
24618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24623 Debugger, serves nicely as GUI to SDCDB
24624 \begin_inset LatexCommand \index{SDCDB (debugger)}
24631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24637 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
24654 Related documentation / recommended reading
24658 \begin_inset Tabular
24659 <lyxtabular version="3" rows="8" columns="3">
24661 <column alignment="left" valignment="top" leftline="true" width="0pt">
24662 <column alignment="block" valignment="top" leftline="true" width="30line%">
24663 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24664 <row topline="true" bottomline="true">
24665 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24681 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24690 <row topline="true">
24691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24714 \begin_inset LatexCommand \index{C Reference card}
24721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24727 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24735 <row topline="true">
24736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24758 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24766 <row topline="true">
24767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24781 \begin_inset Quotes sld
24785 \begin_inset Quotes srd
24791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24799 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24807 <row topline="true">
24808 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24822 \begin_inset Quotes sld
24825 Extensions for Embedded C
24826 \begin_inset Quotes srd
24832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24840 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24848 <row topline="true">
24849 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24861 Latest datasheet of the target CPU
24864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24873 <row topline="true">
24874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24881 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24886 Revision history of datasheet
24889 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24898 <row topline="true" bottomline="true">
24899 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24914 Advanced Compiler Design and Implementation
24917 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24922 bookstore (very dedicated, probably read other books first)
24935 Application notes specifically for SDCC
24938 SDCC makes no claims about the completeness of this list and about up-to-datenes
24939 s or correctness of the application notes
24940 \begin_inset LatexCommand \index{Application notes}
24950 \begin_inset Tabular
24951 <lyxtabular version="3" rows="7" columns="3">
24953 <column alignment="block" valignment="top" leftline="true" width="17col%">
24954 <column alignment="block" valignment="top" leftline="true" width="27col%">
24955 <column alignment="block" valignment="top" leftline="true" rightline="true" width="57col%">
24956 <row topline="true" bottomline="true">
24957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24967 <cell alignment="left" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
24977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24988 <row topline="true">
24989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25006 Using the SDCC Compiler for the DS80C400
25007 \begin_inset LatexCommand \index{DS80C400}
25014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25022 \begin_inset LatexCommand \url{http://pdfserv.maxim-ic.com/en/an/AN3346.pdf}
25030 <row topline="true">
25031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25041 <cell multicolumn="1" alignment="left" valignment="top" topline="true" leftline="true" usebox="none" width="30line%">
25048 Using the Free SDCC C Compiler to Develop Firmware for the DS89C420/430/440/450
25049 \begin_inset LatexCommand \index{DS89C4x0}
25053 Family of Microcontrollers
25056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25064 \begin_inset LatexCommand \url{http://pdfserv.maxim-ic.com/en/an/AN3477.pdf}
25072 <row topline="true">
25073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25080 Silicon Laboratories / Cygnal
25083 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25090 Integrating SDCC 8051 Tools Into The Silicon Labs IDE
25093 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25101 \begin_inset LatexCommand \url{http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/en/an198.pdf}
25109 <row topline="true">
25110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25117 Ramtron / Goal Semiconductor
25120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25127 Interfacing SDCC to Syn and Textpad
25130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25138 \begin_inset LatexCommand \url{http://www.ramtron.com/doc/Products/Microcontroller/Support_Tools.asp}
25146 <row topline="true">
25147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25154 Ramtron / Goal Semiconductor
25157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25164 Installing and Configuring SDCC and Crimson Editor
25167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25175 \begin_inset LatexCommand \url{http://www.ramtron.com/doc/Products/Microcontroller/Support_Tools.asp}
25183 <row topline="true" bottomline="true">
25184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
25201 MSC12xx Programming with SDCC
25204 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
25212 \begin_inset LatexCommand \url{http://focus.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=sbaa109&fileType=pdf}
25230 Some questions answered, some pointers given - it might be time to in turn
25238 can you solve your project with the selected microcontroller? Would you
25239 find out early or rather late that your target is too small/slow/whatever?
25240 Can you switch to a slightly better device if it doesn't fit?
25243 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
25244 and/or another programming language be more adequate? Would an operating
25245 system on the target device help?
25248 if you solved the problem, will the marketing department be happy?
25251 if the marketing department is happy, will customers be happy?
25254 if you're the project manager, marketing department and maybe even the customer
25255 in one person, have you tried to see the project from the outside?
25258 is the project done if you think it is done? Or is just that other interface/pro
25259 tocol/feature/configuration/option missing? How about website, manual(s),
25260 internationali(z|s)ation, packaging, labels, 2nd source for components,
25261 electromagnetic compatability/interference, documentation for production,
25262 production test software, update mechanism, patent issues?
25265 is your project adequately positioned in that magic triangle: fame, fortune,
25269 Maybe not all answers to these questions are known and some answers may
25274 , nevertheless knowing these questions may help you to avoid burnout
25280 burnout is bad for electronic devices, programmers and motorcycle tyres
25284 Chances are you didn't want to hear some of them...
25288 \begin_inset LatexCommand \index{Support}
25295 SDCC has grown to be a large project.
25296 The compiler alone (without the preprocessor, assembler and linker) is
25297 well over 150,000 lines of code (blank stripped).
25298 The open source nature of this project is a key to its continued growth
25300 You gain the benefit and support of many active software developers and
25302 Is SDCC perfect? No, that's why we need your help.
25303 The developers take pride in fixing reported bugs.
25304 You can help by reporting the bugs and helping other SDCC users.
25305 There are lots of ways to contribute, and we encourage you to take part
25306 in making SDCC a great software package.
25310 The SDCC project is hosted on the SDCC sourceforge site at
25311 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
25316 You'll find the complete set of mailing lists
25317 \begin_inset LatexCommand \index{Mailing list(s)}
25321 , forums, bug reporting system, patch submission
25322 \begin_inset LatexCommand \index{Patch submission}
25327 \begin_inset LatexCommand \index{download}
25331 area and Subversion code repository
25332 \begin_inset LatexCommand \index{Subversion code repository}
25340 \begin_inset LatexCommand \index{Bug reporting}
25345 \begin_inset LatexCommand \index{Reporting bugs}
25352 The recommended way of reporting bugs is using the infrastructure of the
25354 You can follow the status of bug reports there and have an overview about
25358 Bug reports are automatically forwarded to the developer mailing list and
25359 will be fixed ASAP.
25360 When reporting a bug, it is very useful to include a small test program
25361 (the smaller the better) which reproduces the problem.
25362 If you can isolate the problem by looking at the generated assembly code,
25363 this can be very helpful.
25364 Compiling your program with the -
25375 \begin_inset LatexCommand \index{-\/-dumpall}
25379 option can sometimes be useful in locating optimization problems.
25380 When reporting a bug please make sure you:
25383 Attach the code you are compiling with SDCC.
25387 Specify the exact command you use to run SDCC, or attach your Makefile.
25391 Specify the SDCC version (type "
25397 "), your platform, and operating system.
25401 Provide an exact copy of any error message or incorrect output.
25405 Put something meaningful in the subject of your message.
25408 Please attempt to include these 5 important parts, as applicable, in all
25409 requests for support or when reporting any problems or bugs with SDCC.
25410 Though this will make your message lengthy, it will greatly improve your
25411 chance that SDCC users and developers will be able to help you.
25412 Some SDCC developers are frustrated by bug reports without code provided
25413 that they can use to reproduce and ultimately fix the problem, so please
25414 be sure to provide sample code if you are reporting a bug!
25417 Please have a short check that you are using a recent version of SDCC and
25418 the bug is not yet known.
25419 This is the link for reporting bugs:
25420 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
25425 With SDCC on average having more than 200 downloads
25426 \begin_inset LatexCommand \index{download}
25430 on sourceforge per day
25436 220 daily downloads on average Jan-Sept 2006 and about 150 daily downloads
25437 between 2002 and 2005.
25438 This does not include other methods of distribution.
25441 there must be some users.
25442 So it's not exactly easy to find a new bug.
25443 If you find one we need it:
25445 reporting bugs is good
25450 Requesting Features
25451 \begin_inset LatexCommand \label{sub:Requesting-Features}
25456 \begin_inset LatexCommand \index{Feature request}
25461 \begin_inset LatexCommand \index{Requesting features}
25468 Like bug reports feature requests are forwarded to the developer mailing
25470 This is the link for requesting features:
25471 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
25481 Like bug reports contributed patches are forwarded to the developer mailing
25483 This is the link for submitting patches
25484 \begin_inset LatexCommand \index{Patch submission}
25489 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
25496 You need to specify some parameters to the
25500 command for the patches to be useful.
25501 If you modified more than one file a patch created f.e.
25506 \begin_inset Quotes sld
25509 diff -Naur unmodified_directory modified_directory >my_changes.patch
25510 \begin_inset Quotes srd
25516 will be fine, otherwise
25520 \begin_inset Quotes sld
25523 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
25524 \begin_inset Quotes srd
25537 These links should take you directly to the
25538 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
25548 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
25549 automated messages (mid 2003)
25553 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
25558 \begin_inset LatexCommand \index{Mailing list(s)}
25562 and forums are archived and searchable so if you are lucky someone already
25563 had a similar problem.
25564 While mails to the lists themselves are delivered promptly their web front
25565 end on sourceforge sometimes shows a severe time lag (up to several weeks),
25566 if you're seriously using SDCC please consider subscribing to the lists.
25572 You can follow the status of the Subversion version
25573 \begin_inset LatexCommand \index{version}
25577 of SDCC by watching the Changelog
25578 \begin_inset LatexCommand \index{Changelog}
25582 in the Subversion repository
25585 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
25592 Subversion Source Code Repository
25601 or the filenames of the snapshot versions of SDCC include date and its
25603 \begin_inset LatexCommand \index{Subversion code repository}
25608 Subversion allows to download the source of recent or previous versions
25610 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
25614 (by number or by date).
25615 An on-line source code browser and detailled instructions are also available
25617 SDCC versions starting from 1999 up to now are available (currently the
25618 versions prior to the conversion from cvs
25619 \begin_inset LatexCommand \index{cvs (see Subversion)}
25623 to Subversion (April 2006) are either by accessible by Subversion or by
25628 \begin_inset LatexCommand \index{Release policy}
25635 Historically there often were long delays between official releases and
25636 the sourceforge download area tends to get not updated at all.
25637 Excuses in the past might have referred to problems with live range analysis,
25638 but as this was fixed a while ago, the current problem is that another
25639 excuse has to be found.
25640 Kidding aside, we have to get better there! On the other hand there are
25641 daily snapshots available at
25642 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
25646 , and you can always build the very last version (hopefully with many bugs
25647 fixed, and features added) from the source code available at
25648 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
25656 \begin_inset LatexCommand \index{Examples}
25663 You'll find some small examples in the directory
25665 sdcc/device/examples/.
25668 More examples and libraries are available at
25670 The SDCC Open Knowledge Resource
25671 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
25678 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
25685 I did insert a reference to Paul's web site here although it seems rather
25686 dedicated to a specific 8032 board (I think it's okay because it f.e.
25687 shows LCD/Harddisc interface and has a free 8051 monitor.
25688 Independent 8032 board vendors face hard competition of heavily subsidized
25689 development boards anyway).
25692 Maybe we should include some links to real world applications.
25693 Preferably pointer to pointers (one for each architecture) so this stays
25698 \begin_inset LatexCommand \index{Quality control}
25705 The compiler is passed through nightly compile and build checks.
25711 \begin_inset LatexCommand \index{Regression test}
25715 check that SDCC itself compiles flawlessly on several platforms and checks
25716 the quality of the code generated by SDCC by running the code through simulator
25718 There is a separate document
25721 \begin_inset LatexCommand \index{Test suite}
25730 You'll find the test code in the directory
25732 sdcc/support/regression
25735 You can run these tests manually by running
25739 in this directory (or f.e.
25744 \begin_inset Quotes sld
25748 \begin_inset Quotes srd
25754 if you don't want to run the complete tests).
25755 The test code might also be interesting if you want to look for examples
25756 \begin_inset LatexCommand \index{Examples}
25760 checking corner cases of SDCC or if you plan to submit patches
25761 \begin_inset LatexCommand \index{Patch submission}
25768 The pic port uses a different set of regression tests, you'll find them
25771 sdcc/src/regression
25776 Use of SDCC in Education
25789 the phrase "use in education" might evoke the association "
25793 fit for use in education".
25794 This connotation is not intended but nevertheless risked as the licensing
25795 of SDCC makes it difficult to offer educational discounts
25799 If your rationales are to:
25802 give students a chance to understand the
25806 steps of code generation
25809 have a curriculum that can be extended for years.
25810 Then you could use an fpga board as target and your curriculum will seamlessly
25811 extend from logic synthesis (
25812 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
25817 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
25821 ), over assembly programming, to C to FPGA compilers (
25822 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
25829 be able to insert excursions about skills like using a revision control
25830 system, submitting/applying patches, using a type-setting (as opposed to
25831 word-processing) engine LyX/LaTeX, using
25832 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
25837 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
25841 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
25842 Source Software, CPU simulation, compiler regression tests
25843 \begin_inset LatexCommand \index{Regression test}
25850 And if there should be a shortage of ideas then you can always point students
25851 to the ever-growing feature request list
25852 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
25859 not tie students to a specific host platform and instead allow them to use
25864 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
25866 \begin_inset LatexCommand \url[OLPC]{http://www.laptop.org}
25873 not encourage students to use illegal copies of educational software
25876 be immune to licensing/availability/price changes of the chosen tool chain
25879 be able to change to a new target platform without having to adopt a new
25883 have complete control over and insight into the tool chain
25886 make your students aware about the pros and cons of open source software
25890 give back to the public as you are probably at least partially publically
25894 give students a chance to publically prove their skills and to possibly
25895 see a world wide impact
25898 then SDCC is probably among the first choices.
25899 Well, probably SDCC might be the only choice.
25902 SDCC Technical Data
25906 \begin_inset LatexCommand \index{Optimizations}
25913 SDCC performs a host of standard optimizations in addition to some MCU specific
25918 Sub-expression Elimination
25919 \begin_inset LatexCommand \index{Subexpression elimination}
25926 The compiler does local and
25952 will be translated to
25964 Some subexpressions are not as obvious as the above example, e.g.:
25974 In this case the address arithmetic a->b[i] will be computed only once;
25975 the equivalent code in C would be.
25987 The compiler will try to keep these temporary variables in registers.
25990 Dead-Code Elimination
25991 \begin_inset LatexCommand \index{Dead-code elimination}
26012 i = 1; \SpecialChar ~
26021 global = 1;\SpecialChar ~
26034 global = 3;\SpecialChar ~
26059 \begin_inset LatexCommand \index{Copy propagation}
26115 Note: the dead stores created by this copy propagation will be eliminated
26116 by dead-code elimination.
26120 \begin_inset LatexCommand \index{Loop optimization}
26125 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
26132 Two types of loop optimizations are done by SDCC
26140 of loop induction variables.
26141 In addition to the strength reduction the optimizer marks the induction
26142 variables and the register allocator tries to keep the induction variables
26143 in registers for the duration of the loop.
26144 Because of this preference of the register allocator
26145 \begin_inset LatexCommand \index{Register allocation}
26149 , loop induction optimization causes an increase in register pressure, which
26150 may cause unwanted spilling of other temporary variables into the stack
26151 \begin_inset LatexCommand \index{stack}
26156 The compiler will generate a warning message when it is forced to allocate
26157 extra space either on the stack or data space.
26158 If this extra space allocation is undesirable then induction optimization
26159 can be eliminated either for the entire source file (with -
26169 -noinduction option) or for a given function only using #pragma\SpecialChar ~
26171 \begin_inset LatexCommand \index{\#pragma noinduction}
26184 for (i = 0 ; i < 100 ; i ++)
26200 for (i = 0; i < 100; i++)
26209 As mentioned previously some loop invariants are not as apparent, all static
26210 address computations are also moved out of the loop.
26215 \begin_inset LatexCommand \index{Strength reduction}
26219 , this optimization substitutes an expression by a cheaper expression:
26224 for (i=0;i < 100; i++)
26242 for (i=0;i< 100;i++) {
26248 ar[itemp1] = itemp2;
26265 The more expensive multiplication
26266 \begin_inset LatexCommand \index{Multiplication}
26270 is changed to a less expensive addition.
26274 \begin_inset LatexCommand \index{Loop reversing}
26281 This optimization is done to reduce the overhead of checking loop boundaries
26282 for every iteration.
26283 Some simple loops can be reversed and implemented using a
26284 \begin_inset Quotes eld
26287 decrement and jump if not zero
26288 \begin_inset Quotes erd
26292 SDCC checks for the following criterion to determine if a loop is reversible
26293 (note: more sophisticated compilers use data-dependency analysis to make
26294 this determination, SDCC uses a more simple minded analysis).
26297 The 'for' loop is of the form
26303 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
26313 The <for body> does not contain
26314 \begin_inset Quotes eld
26318 \begin_inset Quotes erd
26322 \begin_inset Quotes erd
26328 All goto's are contained within the loop.
26331 No function calls within the loop.
26334 The loop control variable <sym> is not assigned any value within the loop
26337 The loop control variable does NOT participate in any arithmetic operation
26341 There are NO switch statements in the loop.
26344 Algebraic Simplifications
26347 SDCC does numerous algebraic simplifications, the following is a small sub-set
26348 of these optimizations.
26353 i = j + 0;\SpecialChar ~
26357 /* changed to: */\SpecialChar ~
26363 i /= 2;\SpecialChar ~
26370 /* changed to: */\SpecialChar ~
26376 i = j - j;\SpecialChar ~
26380 /* changed to: */\SpecialChar ~
26386 i = j / 1;\SpecialChar ~
26390 /* changed to: */\SpecialChar ~
26397 Note the subexpressions
26398 \begin_inset LatexCommand \index{Subexpression}
26402 given above are generally introduced by macro expansions or as a result
26403 of copy/constant propagation.
26406 'switch' Statements
26407 \begin_inset LatexCommand \label{sub:'switch'-Statements}
26412 \begin_inset LatexCommand \index{switch statement}
26419 SDCC can optimize switch statements to jump tables
26420 \begin_inset LatexCommand \index{jump tables}
26425 It makes the decision based on an estimate of the generated code size.
26426 SDCC is quite liberal in the requirements for jump table generation:
26429 The labels need not be in order, and the starting number need not be one
26430 or zero, the case labels are in numerical sequence or not too many case
26431 labels are missing.
26437 switch(i) {\SpecialChar ~
26468 case 4: ...\SpecialChar ~
26500 case 5: ...\SpecialChar ~
26532 case 3: ...\SpecialChar ~
26563 case 6: ...\SpecialChar ~
26595 case 7: ...\SpecialChar ~
26627 case 8: ...\SpecialChar ~
26659 case 9: ...\SpecialChar ~
26691 case 10: ...\SpecialChar ~
26722 case 11: ...\SpecialChar ~
26789 Both the above switch statements will be implemented using a jump-table.
26790 The example to the right side is slightly more efficient as the check for
26791 the lower boundary of the jump-table is not needed.
26795 The number of case labels is not larger than supported by the target architectur
26799 If the case labels are not in numerical sequence ('gaps' between cases)
26800 SDCC checks whether a jump table with additionally inserted dummy cases
26801 is still attractive.
26805 If the starting number is not zero and a check for the lower boundary of
26806 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
26811 Switch statements which have large gaps in the numeric sequence or those
26812 that have too many case labels can be split into more than one switch statement
26813 for efficient code generation, e.g.:
26893 If the above switch statement is broken down into two switch statements
26983 then both the switch statements will be implemented using jump-tables whereas
26984 the unmodified switch statement will not be.
26987 There might be reasons which SDCC cannot know about to either favour or
26988 not favour jump tables.
26989 If the target system has to be as quick for the last switch case as for
26990 the first (pro jump table), or if the switch argument is known to be zero
26991 in the majority of the cases (contra jump table).
26994 The pragma nojtbound
26995 \begin_inset LatexCommand \index{\#pragma nojtbound}
26999 can be used to turn off checking the
27012 It has no effect if a default label is supplied.
27013 Use of this pragma is dangerous: if the switch
27014 \begin_inset LatexCommand \index{switch statement}
27018 argument is not matched by a case statement the processor will happily
27022 Bit-shifting Operations
27023 \begin_inset LatexCommand \index{Bit shifting}
27030 Bit shifting is one of the most frequently used operation in embedded programmin
27032 SDCC tries to implement bit-shift operations in the most efficient way
27048 generates the following code:
27065 In general SDCC will never setup a loop if the shift count is known.
27108 \begin_inset LatexCommand \index{Bit rotation}
27115 A special case of the bit-shift operation is bit rotation
27116 \begin_inset LatexCommand \index{rotating bits}
27120 , SDCC recognizes the following expression to be a left bit-rotation:
27130 char i;\SpecialChar ~
27141 /* unsigned is needed for rotation */
27146 i = ((i << 1) | (i >> 7));
27155 will generate the following code:
27174 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
27175 ns of this case will also be recognized as bit-rotation, i.e.:
27180 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
27183 Nibble and Byte Swapping
27186 Other special cases of the bit-shift operations are nibble or byte swapping
27187 \begin_inset LatexCommand \index{swapping nibbles/bytes}
27191 , SDCC recognizes the following expressions:
27214 i = ((i << 4) | (i >> 4));
27220 j = ((j << 8) | (j >> 8));
27223 and generates a swap instruction for the nibble swapping
27224 \begin_inset LatexCommand \index{Nibble swapping}
27228 or move instructions for the byte swapping
27229 \begin_inset LatexCommand \index{Byte swapping}
27235 \begin_inset Quotes sld
27239 \begin_inset Quotes srd
27242 example can be used to convert from little to big-endian or vice versa.
27243 If you want to change the endianness of a
27247 integer you have to cast to
27254 Note that SDCC stores numbers in little-endian
27260 Usually 8-bit processors don't care much about endianness.
27261 This is not the case for the standard 8051 which only has an instruction
27267 \begin_inset LatexCommand \index{DPTR}
27275 so little-endian is the more efficient byte order.
27279 \begin_inset LatexCommand \index{little-endian}
27284 \begin_inset LatexCommand \index{Endianness}
27289 lowest order first).
27293 \begin_inset LatexCommand \index{Highest Order Bit}
27298 \begin_inset LatexCommand \index{Any Order Bit}
27305 It is frequently required to obtain the highest order bit of an integral
27306 type (long, int, short or char types).
27307 Also obtaining any other order bit is not uncommon.
27308 SDCC recognizes the following expressions to yield the highest order bit
27309 and generates optimized code for it, e.g.:
27322 unsigned char hob1, aob1;
27326 bit hob2, hob3, aob2, aob3;
27335 hob1 = (gint >> 15) & 1;
27339 hob2 = (gint >> 15) & 1;
27343 hob3 = gint & 0x8000;
27347 aob1 = (gint >> 9) & 1;
27351 aob2 = (gint >> 8) & 1;
27355 aob3 = gint & 0x0800;
27365 will generate the following code:
27398 000A E5*01\SpecialChar ~
27425 000C 23\SpecialChar ~
27456 000D 54 01\SpecialChar ~
27483 000F F5*02\SpecialChar ~
27538 0011 E5*01\SpecialChar ~
27565 0013 33\SpecialChar ~
27595 0014 92*00\SpecialChar ~
27650 0016 E5*01\SpecialChar ~
27677 0018 33\SpecialChar ~
27707 0019 92*01\SpecialChar ~
27762 001B E5*01\SpecialChar ~
27789 001D 03\SpecialChar ~
27820 001E 54 01\SpecialChar ~
27847 0020 F5*03\SpecialChar ~
27902 0022 E5*01\SpecialChar ~
27929 0024 13\SpecialChar ~
27959 0025 92*02\SpecialChar ~
28014 0027 E5*01\SpecialChar ~
28041 0029 A2 E3\SpecialChar ~
28068 002B 92*03\SpecialChar ~
28096 Other variations of these cases however will
28101 They are standard C expressions, so I heartily recommend these be the only
28102 way to get the highest order bit, (it is portable).
28103 Of course it will be recognized even if it is embedded in other expressions,
28109 xyz = gint + ((gint >> 15) & 1);
28112 will still be recognized.
28116 \begin_inset LatexCommand \index{Higher Order Byte}
28120 / Higher Order Word
28121 \begin_inset LatexCommand \index{Higher Order Word}
28128 It is also frequently required to obtain a higher order byte or word of
28129 a larger integral type (long, int or short types).
28130 SDCC recognizes the following expressions to yield the higher order byte
28131 or word and generates optimized code for it, e.g.:
28138 unsigned long int glong;
28146 unsigned char hob1, hob2;
28150 unsigned int how1, how2;
28159 hob1 = (gint >> 8) & 0xFF;
28163 hob2 = glong >> 24;
28167 how1 = (glong >> 16) & 0xFFFF;
28181 will generate the following code:
28214 0037 85*01*06\SpecialChar ~
28236 _foo_hob1_1_1,(_gint + 1)
28266 003A 85*05*07\SpecialChar ~
28288 _foo_hob2_1_1,(_glong + 3)
28318 003D 85*04*08\SpecialChar ~
28340 _foo_how1_1_1,(_glong + 2)
28342 0040 85*05*09\SpecialChar ~
28364 (_foo_how1_1_1 + 1),(_glong + 3)
28366 0043 85*03*0A\SpecialChar ~
28388 _foo_how2_1_1,(_glong + 1)
28390 0046 85*04*0B\SpecialChar ~
28412 (_foo_how2_1_1 + 1),(_glong + 2)
28415 Again, variations of these cases may
28420 They are standard C expressions, so I heartily recommend these be the only
28421 way to get the higher order byte/word, (it is portable).
28422 Of course it will be recognized even if it is embedded in other expressions,
28428 xyz = gint + ((gint >> 8) & 0xFF);
28431 will still be recognized.
28435 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
28440 \begin_inset LatexCommand \index{Peephole optimizer}
28447 The compiler uses a rule based, pattern matching and re-writing mechanism
28448 for peep-hole optimization.
28453 a peep-hole optimizer by Christopher W.
28454 Fraser (cwfraser\SpecialChar ~
28457 A default set of rules are compiled into the compiler, additional rules
28458 may be added with the
28471 \begin_inset LatexCommand \index{-\/-peep-file}
28478 The rule language is best illustrated with examples.
28502 The above rule will change the following assembly
28503 \begin_inset LatexCommand \index{Assembler routines}
28525 Note: All occurrences of a
28529 (pattern variable) must denote the same string.
28530 With the above rule, the assembly sequence:
28540 will remain unmodified.
28544 Other special case optimizations may be added by the user (via
28560 some variants of the 8051 MCU
28561 \begin_inset LatexCommand \index{MCS51 variants}
28574 The following two rules will change all
28593 replace { lcall %1 } by { acall %1 }
28595 replace { ljmp %1 } by { ajmp %1 }
28600 inline-assembler code
28602 is also passed through the peep hole optimizer, thus the peephole optimizer
28603 can also be used as an assembly level macro expander.
28604 The rules themselves are MCU dependent whereas the rule language infra-structur
28605 e is MCU independent.
28606 Peephole optimization rules for other MCU can be easily programmed using
28611 The syntax for a rule is as follows:
28616 rule := replace [ restart ] '{' <assembly sequence> '
28654 <assembly sequence> '
28672 '}' [if <functionName> ] '
28677 <assembly sequence> := assembly instruction (each instruction including
28678 labels must be on a separate line).
28682 The optimizer will apply to the rules one by one from the top in the sequence
28683 of their appearance, it will terminate when all rules are exhausted.
28684 If the 'restart' option is specified, then the optimizer will start matching
28685 the rules again from the top, this option for a rule is expensive (performance)
28686 , it is intended to be used in situations where a transformation will trigger
28687 the same rule again.
28688 An example of this (not a good one, it has side effects) is the following
28711 Note that the replace pattern cannot be a blank, but can be a comment line.
28712 Without the 'restart' option only the innermost 'pop' 'push' pair would
28713 be eliminated, i.e.:
28743 the restart option the rule will be applied again to the resulting code
28744 and then all the pop-push pairs will be eliminated to yield:
28754 A conditional function can be attached to a rule.
28755 Attaching rules are somewhat more involved, let me illustrate this with
28782 The optimizer does a look-up of a function name table defined in function
28787 in the source file SDCCpeeph.c, with the name
28792 If it finds a corresponding entry the function is called.
28793 Note there can be no parameters specified for these functions, in this
28798 is crucial, since the function
28802 expects to find the label in that particular variable (the hash table containin
28803 g the variable bindings is passed as a parameter).
28804 If you want to code more such functions, take a close look at the function
28805 labelInRange and the calling mechanism in source file SDCCpeeph.c.
28806 Currently implemented are
28808 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
28809 24bitMode, portIsDS390, 24bitModeAndPortDS390
28818 I know this whole thing is a little kludgey, but maybe some day we will
28819 have some better means.
28820 If you are looking at this file, you will see the default rules that are
28821 compiled into the compiler, you can add your own rules in the default set
28822 there if you get tired of specifying the -
28836 \begin_inset LatexCommand \index{ANSI-compliance}
28841 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
28848 The latest publically available version of the standard
28850 ISO/IEC 9899 - Programming languages - C
28852 should be available at:
28853 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
28862 Deviations from the compliance:
28865 functions are not reentrant
28866 \begin_inset LatexCommand \index{reentrant}
28870 unless explicitly declared as such or the
28883 \begin_inset LatexCommand \index{-\/-stack-auto}
28889 command line option is specified.
28893 \begin_inset LatexCommand \index{struct}
28898 \begin_inset LatexCommand \index{union}
28902 cannot be assigned values directly, cannot be passed as function parameters
28903 or assigned to each other and cannot be a return value
28904 \begin_inset LatexCommand \index{return value}
28908 from a function, e.g.:
28934 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
28953 s parms) /* invalid in SDCC although allowed in ANSI */
28974 return rets; /* is invalid in SDCC although allowed in ANSI */
28980 initialization of structure arrays must be fully braced.
28986 struct s { char x } a[] = {1, 2};\SpecialChar ~
28991 /* invalid in SDCC */
28993 struct s { char x } a[] = {{1}, {2}}; /* OK */
28998 \begin_inset LatexCommand \index{long long (not supported)}
29003 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
29011 \begin_inset LatexCommand \index{double (not supported)}
29015 ' precision floating point
29016 \begin_inset LatexCommand \index{Floating point support}
29024 \begin_inset LatexCommand \index{K\&R style}
29028 function declarations are NOT allowed.
29034 foo(i,j) /* this old style of function declarations */
29036 int i,j; /* are valid in ANSI but not valid in SDCC */
29051 Most enhancements in C99 are not supported, f.e.:
29059 \begin_inset LatexCommand \index{inline (not supported)}
29065 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
29071 can be used as a work around */
29079 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
29083 Certain words that are valid identifiers in the standard may be reserved
29084 words in SDCC unless the
29097 \begin_inset LatexCommand \index{-\/-std-c89}
29112 \begin_inset LatexCommand \index{-\/-std-c99}
29118 command line options are used.
29119 These may include (depending on the selected processor): 'at', 'banked',
29120 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
29121 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
29122 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
29124 Compliant equivalents of these keywords are always available in a form
29125 that begin with two underscores
29126 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
29131 '__data' instead of 'data'.
29134 Cyclomatic Complexity
29135 \begin_inset LatexCommand \index{Cyclomatic complexity}
29142 Cyclomatic complexity of a function is defined as the number of independent
29143 paths the program can take during execution of the function.
29144 This is an important number since it defines the number test cases you
29145 have to generate to validate the function.
29146 The accepted industry standard for complexity number is 10, if the cyclomatic
29147 complexity reported by SDCC exceeds 10 you should think about simplification
29148 of the function logic.
29149 Note that the complexity level is not related to the number of lines of
29150 code in a function.
29151 Large functions can have low complexity, and small functions can have large
29157 SDCC uses the following formula to compute the complexity:
29162 complexity = (number of edges in control flow graph) - (number of nodes
29163 in control flow graph) + 2;
29167 Having said that the industry standard is 10, you should be aware that in
29168 some cases it be may unavoidable to have a complexity level of less than
29170 For example if you have switch statement with more than 10 case labels,
29171 each case label adds one to the complexity level.
29172 The complexity level is by no means an absolute measure of the algorithmic
29173 complexity of the function, it does however provide a good starting point
29174 for which functions you might look at for further optimization.
29177 Retargetting for other Processors
29180 The issues for retargetting the compiler are far too numerous to be covered
29182 What follows is a brief description of each of the seven phases of the
29183 compiler and its MCU dependency.
29186 Parsing the source and building the annotated parse tree.
29187 This phase is largely MCU independent (except for the language extensions).
29188 Syntax & semantic checks are also done in this phase, along with some initial
29189 optimizations like back patching labels and the pattern matching optimizations
29190 like bit-rotation etc.
29193 The second phase involves generating an intermediate code which can be easy
29194 manipulated during the later phases.
29195 This phase is entirely MCU independent.
29196 The intermediate code generation assumes the target machine has unlimited
29197 number of registers, and designates them with the name iTemp.
29198 The compiler can be made to dump a human readable form of the code generated
29212 This phase does the bulk of the standard optimizations and is also MCU independe
29214 This phase can be broken down into several sub-phases:
29218 Break down intermediate code (iCode) into basic blocks.
29220 Do control flow & data flow analysis on the basic blocks.
29222 Do local common subexpression elimination, then global subexpression elimination
29224 Dead code elimination
29228 If loop optimizations caused any changes then do 'global subexpression eliminati
29229 on' and 'dead code elimination' again.
29232 This phase determines the live-ranges; by live range I mean those iTemp
29233 variables defined by the compiler that still survive after all the optimization
29235 Live range analysis
29236 \begin_inset LatexCommand \index{Live range analysis}
29240 is essential for register allocation, since these computation determines
29241 which of these iTemps will be assigned to registers, and for how long.
29244 Phase five is register allocation.
29245 There are two parts to this process.
29249 The first part I call 'register packing' (for lack of a better term).
29250 In this case several MCU specific expression folding is done to reduce
29255 The second part is more MCU independent and deals with allocating registers
29256 to the remaining live ranges.
29257 A lot of MCU specific code does creep into this phase because of the limited
29258 number of index registers available in the 8051.
29261 The Code generation phase is (unhappily), entirely MCU dependent and very
29262 little (if any at all) of this code can be reused for other MCU.
29263 However the scheme for allocating a homogenized assembler operand for each
29264 iCode operand may be reused.
29267 As mentioned in the optimization section the peep-hole optimizer is rule
29268 based system, which can reprogrammed for other MCUs.
29272 \begin_inset LatexCommand \index{Compiler internals}
29279 The anatomy of the compiler
29280 \begin_inset LatexCommand \label{sub:The-anatomy-of}
29289 This is an excerpt from an article published in Circuit Cellar Magazine
29295 It's a little outdated (the compiler is much more efficient now and user/develo
29296 per friendly), but pretty well exposes the guts of it all.
29302 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
29303 It is fairly easy to retarget for other 8-bit MCU.
29304 Here we take a look at some of the internals of the compiler.
29309 \begin_inset LatexCommand \index{Parsing}
29316 Parsing the input source file and creating an AST (Annotated Syntax Tree
29317 \begin_inset LatexCommand \index{Annotated syntax tree}
29322 This phase also involves propagating types (annotating each node of the
29323 parse tree with type information) and semantic analysis.
29324 There are some MCU specific parsing rules.
29325 For example the storage classes, the extended storage classes are MCU specific
29326 while there may be a xdata storage class for 8051 there is no such storage
29327 class for z80 or Atmel AVR.
29328 SDCC allows MCU specific storage class extensions, i.e.
29329 xdata will be treated as a storage class specifier when parsing 8051 C
29330 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
29335 \begin_inset LatexCommand \index{iCode}
29342 Intermediate code generation.
29343 In this phase the AST is broken down into three-operand form (iCode).
29344 These three operand forms are represented as doubly linked lists.
29345 ICode is the term given to the intermediate form generated by the compiler.
29346 ICode example section shows some examples of iCode generated for some simple
29347 C source functions.
29351 \begin_inset LatexCommand \index{Optimizations}
29358 Bulk of the target independent optimizations is performed in this phase.
29359 The optimizations include constant propagation, common sub-expression eliminati
29360 on, loop invariant code movement, strength reduction of loop induction variables
29361 and dead-code elimination.
29364 Live range analysis
29365 \begin_inset LatexCommand \index{Live range analysis}
29372 During intermediate code generation phase, the compiler assumes the target
29373 machine has infinite number of registers and generates a lot of temporary
29375 The live range computation determines the lifetime of each of these compiler-ge
29376 nerated temporaries.
29377 A picture speaks a thousand words.
29378 ICode example sections show the live range annotations for each of the
29380 It is important to note here, each iCode is assigned a number in the order
29381 of its execution in the function.
29382 The live ranges are computed in terms of these numbers.
29383 The from number is the number of the iCode which first defines the operand
29384 and the to number signifies the iCode which uses this operand last.
29387 Register Allocation
29388 \begin_inset LatexCommand \index{Register allocation}
29395 The register allocation determines the type and number of registers needed
29397 In most MCUs only a few registers can be used for indirect addressing.
29398 In case of 8051 for example the registers R0 & R1 can be used to indirectly
29399 address the internal ram and DPTR to indirectly address the external ram.
29400 The compiler will try to allocate the appropriate register to pointer variables
29402 ICode example section shows the operands annotated with the registers assigned
29404 The compiler will try to keep operands in registers as much as possible;
29405 there are several schemes the compiler uses to do achieve this.
29406 When the compiler runs out of registers the compiler will check to see
29407 if there are any live operands which is not used or defined in the current
29408 basic block being processed, if there are any found then it will push that
29409 operand and use the registers in this block, the operand will then be popped
29410 at the end of the basic block.
29414 There are other MCU specific considerations in this phase.
29415 Some MCUs have an accumulator; very short-lived operands could be assigned
29416 to the accumulator instead of a general-purpose register.
29422 Figure II gives a table of iCode operations supported by the compiler.
29423 The code generation involves translating these operations into corresponding
29424 assembly code for the processor.
29425 This sounds overly simple but that is the essence of code generation.
29426 Some of the iCode operations are generated on a MCU specific manner for
29427 example, the z80 port does not use registers to pass parameters so the
29428 SEND and RECV iCode operations will not be generated, and it also does
29429 not support JUMPTABLES.
29436 <Where is Figure II?>
29439 In the original article Figure II was announced to be downloadable on
29444 Unfortunately it never seemed to have shown up there, so: where is Figure
29449 \begin_inset LatexCommand \index{iCode}
29456 This section shows some details of iCode.
29457 The example C code does not do anything useful; it is used as an example
29458 to illustrate the intermediate code generated by the compiler.
29470 /* This function does nothing useful.
29477 for the purpose of explaining iCode */
29480 short function (data int *x)
29488 short i=10; \SpecialChar ~
29490 /* dead initialization eliminated */
29495 short sum=10; /* dead initialization eliminated */
29508 while (*x) *x++ = *p++;
29522 /* compiler detects i,j to be induction variables */
29526 for (i = 0, j = 10 ; i < 10 ; i++, j
29552 mul += i * 3; \SpecialChar ~
29554 /* this multiplication remains */
29560 gint += j * 3;\SpecialChar ~
29562 /* this multiplication changed to addition */
29576 In addition to the operands each iCode contains information about the filename
29577 and line it corresponds to in the source file.
29578 The first field in the listing should be interpreted as follows:
29583 Filename(linenumber: iCode Execution sequence number : ICode hash table
29584 key : loop depth of the iCode).
29589 Then follows the human readable form of the ICode operation.
29590 Each operand of this triplet form can be of three basic types a) compiler
29591 generated temporary b) user defined variable c) a constant value.
29592 Note that local variables and parameters are replaced by compiler generated
29595 \begin_inset LatexCommand \index{Live range analysis}
29599 are computed only for temporaries (i.e.
29600 live ranges are not computed for global variables).
29602 \begin_inset LatexCommand \index{Register allocation}
29606 are allocated for temporaries only.
29607 Operands are formatted in the following manner:
29612 Operand Name [lr live-from : live-to ] { type information } [ registers
29618 As mentioned earlier the live ranges are computed in terms of the execution
29619 sequence number of the iCodes, for example
29621 the iTemp0 is live from (i.e.
29622 first defined in iCode with execution sequence number 3, and is last used
29623 in the iCode with sequence number 5).
29624 For induction variables such as iTemp21 the live range computation extends
29625 the lifetime from the start to the end of the loop.
29627 The register allocator used the live range information to allocate registers,
29628 the same registers may be used for different temporaries if their live
29629 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
29630 iTemp17 since their live ranges do not overlap.
29631 In addition the allocator also takes into consideration the type and usage
29632 of a temporary, for example itemp6 is a pointer to near space and is used
29633 as to fetch data from (i.e.
29634 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
29635 Some short lived temporaries are allocated to special registers which have
29636 meaning to the code generator e.g.
29637 iTemp13 is allocated to a pseudo register CC which tells the back end that
29638 the temporary is used only for a conditional jump the code generation makes
29639 use of this information to optimize a compare and jump ICode.
29641 There are several loop optimizations
29642 \begin_inset LatexCommand \index{Loop optimization}
29646 performed by the compiler.
29647 It can detect induction variables iTemp21(i) and iTemp23(j).
29648 Also note the compiler does selective strength reduction
29649 \begin_inset LatexCommand \index{Strength reduction}
29654 the multiplication of an induction variable in line 18 (gint = j * 3) is
29655 changed to addition, a new temporary iTemp17 is allocated and assigned
29656 a initial value, a constant 3 is then added for each iteration of the loop.
29657 The compiler does not change the multiplication
29658 \begin_inset LatexCommand \index{Multiplication}
29662 in line 17 however since the processor does support an 8 * 8 bit multiplication.
29664 Note the dead code elimination
29665 \begin_inset LatexCommand \index{Dead-code elimination}
29669 optimization eliminated the dead assignments in line 7 & 8 to I and sum
29677 Sample.c (5:1:0:0) _entry($9) :
29682 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
29687 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
29692 Sample.c(11:4:53:0) preHeaderLbl0($11) :
29697 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
29703 Sample.c(11:6:5:1) _whilecontinue_0($1) :
29708 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
29714 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
29719 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
29725 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
29731 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
29737 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
29743 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
29744 * int}[r0] + 0x2 {short}
29749 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
29754 Sample.c(11:17:21:0)_whilebreak_0($3) :
29759 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29764 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29769 Sample.c(15:20:54:0)preHeaderLbl1($13) :
29774 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29779 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29784 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29789 Sample.c(15:24:26:1)_forcond_0($4) :
29794 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
29800 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29805 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
29806 + ITemp21 [lr21:38]{short}[r4]
29811 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
29817 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
29818 + iTemp15 [lr29:30]{short}[r1]
29823 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
29829 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
29835 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
29841 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
29847 Sample.c(19:38:47:1) goto _forcond_0($4)
29852 Sample.c(19:39:48:0)_forbreak_0($7) :
29857 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
29858 + ITemp11 [lr19:40]{short}[r3]
29863 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
29868 Sample.c(20:42:51:0)_return($8) :
29873 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
29879 Finally the code generated for this function:
29920 ; ----------------------------------------------
29925 ; function function
29930 ; ----------------------------------------------
29940 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
29952 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
29964 ;_whilecontinue_0($1) :
29974 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
29979 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
30038 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
30057 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
30104 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
30144 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
30170 ; iTemp6 [lr5:16]{_near * int}[r0] =
30175 ; iTemp6 [lr5:16]{_near * int}[r0] +
30192 ; goto _whilecontinue_0($1)
30204 ; _whilebreak_0($3) :
30214 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
30226 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
30238 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
30250 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
30269 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
30298 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
30303 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
30348 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
30353 ; iTemp21 [lr21:38]{short}[r4]
30379 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
30412 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
30417 ; iTemp15 [lr29:30]{short}[r1]
30436 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
30483 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
30530 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
30542 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
30556 cjne r5,#0xff,00104$
30568 ; goto _forcond_0($4)
30580 ; _forbreak_0($7) :
30590 ; ret iTemp24 [lr40:41]{short}
30633 A few words about basic block successors, predecessors and dominators
30636 Successors are basic blocks
30637 \begin_inset LatexCommand \index{Basic blocks}
30641 that might execute after this basic block.
30643 Predecessors are basic blocks that might execute before reaching this basic
30646 Dominators are basic blocks that WILL execute before reaching this basic
30680 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
30683 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
30686 c) domVect of [BB4] = BB1 ...
30687 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
30695 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
30705 Thanks to all the other volunteer developers who have helped with coding,
30706 testing, web-page creation, distribution sets, etc.
30707 You know who you are :-)
30714 This document was initially written by Sandeep Dutta
30717 All product names mentioned herein may be trademarks
30718 \begin_inset LatexCommand \index{Trademarks}
30722 of their respective companies.
30729 To avoid confusion, the installation and building options for SDCC itself
30730 (chapter 2) are not part of the index.
30734 \begin_inset LatexCommand \printindex{}