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={8032 8051 ansi c compiler CPU DS390
11 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
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 cvs
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 retargetted for other microprocessors, support for Microchip
146 PIC, 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 retargettable 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:
207 char (8 bits, 1 byte),
210 short and int (16 bits, 2 bytes),
213 long (32 bit, 4 bytes)
220 The compiler also allows
222 inline assembler code
224 to be embedded anywhere in a function.
225 In addition, routines developed in assembly can also be called.
229 SDCC also provides an option (-
239 -cyclomatic) to report the relative complexity of a function.
240 These functions can then be further optimized, or hand coded in assembly
246 SDCC also comes with a companion source level debugger SDCDB, the debugger
247 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
248 SDCDB and ucSim are currently not available on Win32 platforms.
253 The latest version can be downloaded from
254 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
264 Please note: the compiler will probably always be some steps ahead of this
269 \begin_inset LatexCommand \index{Status of documentation}
279 Obviously this has pros and cons
288 All packages used in this compiler system are
296 ; source code for all the sub-packages (pre-processor, assemblers, linkers
297 etc) is distributed with the package.
298 This documentation is maintained using a freeware word processor (LyX).
300 This program is free software; you can redistribute it and/or modify it
301 under the terms of the GNU General Public License
302 \begin_inset LatexCommand \index{GNU General Public License, GPL}
306 as published by the Free Software Foundation; either version 2, or (at
307 your option) any later version.
308 This program is distributed in the hope that it will be useful, but WITHOUT
309 ANY WARRANTY; without even the implied warranty
310 \begin_inset LatexCommand \index{warranty}
314 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
315 See the GNU General Public License for more details.
316 You should have received a copy of the GNU General Public License along
317 with this program; if not, write to the Free Software Foundation, 59 Temple
318 Place - Suite 330, Boston, MA 02111-1307, USA.
319 In other words, you are welcome to use, share and improve this program.
320 You are forbidden to forbid anyone else to use, share and improve what
322 Help stamp out software-hoarding!
325 Typographic conventions
326 \begin_inset LatexCommand \index{Typographic conventions}
333 Throughout this manual, we will use the following convention.
334 Commands you have to type in are printed in
342 Code samples are printed in
347 Interesting items and new terms are printed in
352 Compatibility with previous versions
353 \begin_inset LatexCommand \index{Compatibility with previous versions}
360 This version has numerous bug fixes compared with the previous version.
361 But we also introduced some incompatibilities with older versions.
362 Not just for the fun of it, but to make the compiler more stable, efficient
364 \begin_inset LatexCommand \index{ANSI-compliance}
369 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
373 for ANSI-Compliance).
379 short is now equivalent to int (16 bits), it used to be equivalent to char
380 (8 bits) which is not ANSI compliant.
383 the default directory for gcc-builds where include, library and documentation
384 files are stored is now in /usr/local/share.
387 char type parameters to vararg functions are casted to int unless explicitly
404 will push a as an int and as a char resp.
417 -regextend has been removed.
430 -noregparms has been removed.
443 -stack-after-data has been removed.
447 \begin_inset LatexCommand \index{bit}
452 \begin_inset LatexCommand \index{sbit}
457 \begin_inset LatexCommand \index{\_\_sbit}
461 types now consistently behave like the C99 _Bool type with respect to type
463 \begin_inset LatexCommand \index{type conversion}
468 \begin_inset LatexCommand \index{type promotion}
473 The most common incompatibility resulting from this change is related to
475 \begin_inset LatexCommand \index{Bit toggling}
489 b = ~b; /* equivalent to b=1 instead of toggling b */
493 b = !b; /* toggles b */
497 In previous versions, both forms would have toggled the bit.
502 <pending: more incompatibilities?>
508 What do you need before you start installation of SDCC? A computer, and
510 The preferred method of installation is to compile SDCC from source using
512 For Windows some pre-compiled binary distributions are available for your
514 You should have some experience with command line tools and compiler use.
520 The SDCC home page at
521 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
525 is a great place to find distribution sets.
526 You can also find links to the user mailing lists that offer help or discuss
527 SDCC with other SDCC users.
528 Web links to other SDCC related sites can also be found here.
529 This document can be found in the DOC directory of the source package as
531 A pdf version of this document is available at
532 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
537 Some of the other tools (simulator and assembler) included with SDCC contain
538 their own documentation and can be found in the source distribution.
539 If you want the latest unreleased software, the complete source package
540 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
543 Wishes for the future
546 There are (and always will be) some things that could be done.
547 Here are some I can think of:
554 char KernelFunction3(char p) at 0x340;
562 \begin_inset LatexCommand \index{code banking (limited support)}
572 If you can think of some more, please see the section
573 \begin_inset LatexCommand \ref{sub:Requesting-Features}
577 about filing feature requests
578 \begin_inset LatexCommand \index{Requesting features}
583 \begin_inset LatexCommand \index{Feature request}
593 \begin_inset LatexCommand \index{Installation}
600 For most users it is sufficient to skip to either section
601 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
606 \begin_inset LatexCommand \ref{sub:Windows-Install}
611 More detailled instructions follow below.
615 \begin_inset LatexCommand \index{Options SDCC configuration}
622 The install paths, search paths and other options are defined when running
624 The defaults can be overridden by:
626 \labelwidthstring 00.00.0000
638 -prefix see table below
640 \labelwidthstring 00.00.0000
652 -exec_prefix see table below
654 \labelwidthstring 00.00.0000
666 -bindir see table below
668 \labelwidthstring 00.00.0000
680 -datadir see table below
682 \labelwidthstring 00.00.0000
684 docdir environment variable, see table below
686 \labelwidthstring 00.00.0000
688 include_dir_suffix environment variable, see table below
690 \labelwidthstring 00.00.0000
692 lib_dir_suffix environment variable, see table below
694 \labelwidthstring 00.00.0000
696 sdccconf_h_dir_separator environment variable, either / or
701 This character will only be used in sdccconf.h; don't forget it's a C-header,
702 therefore a double-backslash is needed there.
704 \labelwidthstring 00.00.0000
716 -disable-mcs51-port Excludes the Intel mcs51 port
718 \labelwidthstring 00.00.0000
730 -disable-gbz80-port Excludes the Gameboy gbz80 port
732 \labelwidthstring 00.00.0000
744 -disable-z80-port Excludes the z80 port
746 \labelwidthstring 00.00.0000
758 -disable-avr-port Excludes the AVR port
760 \labelwidthstring 00.00.0000
772 -disable-ds390-port Excludes the DS390 port
774 \labelwidthstring 00.00.0000
786 -disable-hc08-port Excludes the HC08 port
788 \labelwidthstring 00.00.0000
800 -disable-pic-port Excludes the PIC port
802 \labelwidthstring 00.00.0000
814 -disable-xa51-port Excludes the XA51 port
816 \labelwidthstring 00.00.0000
828 -disable-ucsim Disables configuring and building of ucsim
830 \labelwidthstring 00.00.0000
842 -disable-device-lib-build Disables automatically building device libraries
844 \labelwidthstring 00.00.0000
856 -disable-packihx Disables building packihx
858 \labelwidthstring 00.00.0000
870 -enable-libgc Use the Bohem memory allocator.
871 Lower runtime footprint.
874 Furthermore the environment variables CC, CFLAGS, ...
875 the tools and their arguments can be influenced.
876 Please see `configure -
886 -help` and the man/info pages of `configure` for details.
890 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
891 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
892 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
893 At the moment it's not possible to change the default settings (it was
894 simply never required).
898 These configure options are compiled into the binaries, and can only be
899 changed by rerunning 'configure' and recompiling SDCC.
900 The configure options are written in
904 to distinguish them from run time environment variables (see section search
910 \begin_inset Quotes sld
914 \begin_inset Quotes srd
917 are used by the SDCC team to build the official Win32 binaries.
918 The SDCC team uses Mingw32 to build the official Windows binaries, because
925 a gcc compiler and last but not least
928 the binaries can be built by cross compiling on Sourceforge's compile farm.
931 See the examples, how to pass the Win32 settings to 'configure'.
932 The other Win32 builds using Borland, VC or whatever don't use 'configure',
933 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
945 <lyxtabular version="3" rows="8" columns="3">
947 <column alignment="block" valignment="top" leftline="true" width="0in">
948 <column alignment="block" valignment="top" leftline="true" width="0in">
949 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
950 <row topline="true" bottomline="true">
951 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
987 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
995 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1006 <row topline="true">
1007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1027 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1038 <row topline="true">
1039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1076 <row topline="true">
1077 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1099 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1110 <row topline="true">
1111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1148 <row topline="true">
1149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1176 <row topline="true" bottomline="true">
1177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1195 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1213 'configure' also computes relative paths.
1214 This is needed for full relocatability of a binary package and to complete
1215 search paths (see section search paths below):
1221 \begin_inset Tabular
1222 <lyxtabular version="3" rows="4" columns="3">
1224 <column alignment="block" valignment="top" leftline="true" width="0in">
1225 <column alignment="block" valignment="top" leftline="true" width="0in">
1226 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1227 <row topline="true" bottomline="true">
1228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1253 <row topline="true" bottomline="true">
1254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1272 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1281 <row bottomline="true">
1282 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1300 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1309 <row bottomline="true">
1310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1361 \begin_inset Quotes srd
1365 \begin_inset Quotes srd
1379 \begin_inset Quotes srd
1383 \begin_inset Quotes srd
1411 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1420 \begin_inset Quotes srd
1423 i586-mingw32msvc-gcc
1424 \begin_inset Quotes srd
1428 \begin_inset Quotes srd
1431 i586-mingw32msvc-g++
1432 \begin_inset Quotes srd
1440 \begin_inset Quotes srd
1443 i586-mingw32msvc-ranlib
1444 \begin_inset Quotes srd
1452 \begin_inset Quotes srd
1455 i586-mingw32msvc-strip
1456 \begin_inset Quotes srd
1474 \begin_inset Quotes srd
1478 \begin_inset Quotes srd
1496 \begin_inset Quotes srd
1500 \begin_inset Quotes srd
1508 \begin_inset Quotes srd
1512 \begin_inset Quotes srd
1520 \begin_inset Quotes srd
1524 \begin_inset Quotes srd
1532 \begin_inset Quotes srd
1536 \begin_inset Quotes srd
1543 sdccconf_h_dir_separator=
1544 \begin_inset Quotes srd
1556 \begin_inset Quotes srd
1573 -disable-device-lib-build
1601 -host=i586-mingw32msvc -
1611 -build=unknown-unknown-linux-gnu
1615 \begin_inset Quotes sld
1619 \begin_inset Quotes srd
1622 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1631 \begin_inset Quotes srd
1635 \begin_inset Quotes srd
1643 \begin_inset Quotes srd
1647 \begin_inset Quotes srd
1665 \begin_inset Quotes srd
1669 \begin_inset Quotes srd
1687 \begin_inset Quotes srd
1691 \begin_inset Quotes srd
1699 \begin_inset Quotes srd
1703 \begin_inset Quotes srd
1711 \begin_inset Quotes srd
1715 \begin_inset Quotes srd
1723 \begin_inset Quotes srd
1727 \begin_inset Quotes srd
1734 sdccconf_h_dir_separator=
1735 \begin_inset Quotes srd
1747 \begin_inset Quotes srd
1767 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1778 -C' turns on caching, which gives a little bit extra speed.
1779 However if options are changed, it can be necessary to delete the config.cache
1784 \begin_inset LatexCommand \label{sub:Install-paths}
1789 \begin_inset LatexCommand \index{Install paths}
1795 \added_space_top medskip \align center
1797 \begin_inset Tabular
1798 <lyxtabular version="3" rows="5" columns="4">
1800 <column alignment="center" valignment="top" leftline="true" width="0">
1801 <column alignment="center" valignment="top" leftline="true" width="0">
1802 <column alignment="center" valignment="top" leftline="true" width="0">
1803 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1804 <row topline="true" bottomline="true">
1805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1825 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1835 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1846 <row topline="true">
1847 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1865 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1873 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1886 <row topline="true">
1887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1902 $DATADIR/ $INCLUDE_DIR_SUFFIX
1905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1910 /usr/local/share/sdcc/include
1913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1926 <row topline="true">
1927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1935 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1942 $DATADIR/$LIB_DIR_SUFFIX
1945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1950 /usr/local/share/sdcc/lib
1953 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1966 <row topline="true" bottomline="true">
1967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1990 /usr/local/share/sdcc/doc
1993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2015 *compiler, preprocessor, assembler, and linker
2021 is auto-appended by the compiler, e.g.
2022 small, large, z80, ds390 etc
2025 The install paths can still be changed during `make install` with e.g.:
2028 make install prefix=$(HOME)/local/sdcc
2031 Of course this doesn't change the search paths compiled into the binaries.
2035 Moreover the install path can be changed by defining DESTDIR
2036 \begin_inset LatexCommand \index{DESTDIR}
2043 make install DESTDIR=$(HOME)/sdcc.rpm/
2046 Please note that DESTDIR must have a trailing slash!
2050 \begin_inset LatexCommand \label{sub:Search-Paths}
2055 \begin_inset LatexCommand \index{Search path}
2062 Some search paths or parts of them are determined by configure variables
2067 , see section above).
2068 Further search paths are determined by environment variables during runtime.
2071 The paths searched when running the compiler are as follows (the first catch
2077 Binary files (preprocessor, assembler and linker)
2083 \begin_inset Tabular
2084 <lyxtabular version="3" rows="4" columns="3">
2086 <column alignment="block" valignment="top" leftline="true" width="0in">
2087 <column alignment="block" valignment="top" leftline="true" width="0in">
2088 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2089 <row topline="true" bottomline="true">
2090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2115 <row topline="true">
2116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2145 <row topline="true">
2146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2151 Path of argv[0] (if available)
2154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2162 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2171 <row topline="true" bottomline="true">
2172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2188 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2213 \begin_inset Tabular
2214 <lyxtabular version="3" rows="6" columns="3">
2216 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2217 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2218 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2219 <row topline="true" bottomline="true">
2220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2236 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2245 <row topline="true">
2246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2301 <row topline="true">
2302 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2327 <row topline="true">
2328 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2342 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2365 <row topline="true">
2366 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2447 <row topline="true" bottomline="true">
2448 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2464 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2469 /usr/local/share/sdcc/
2474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2502 -nostdinc disables the last two search paths.
2512 With the exception of
2513 \begin_inset Quotes sld
2527 \begin_inset Quotes srd
2534 is auto-appended by the compiler (e.g.
2535 small, large, z80, ds390 etc.).
2542 \begin_inset Tabular
2543 <lyxtabular version="3" rows="6" columns="3">
2545 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2546 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2547 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2548 <row topline="true" bottomline="true">
2549 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2557 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2565 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2574 <row topline="true">
2575 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2630 <row topline="true">
2631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2643 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2670 <row topline="true">
2671 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2682 $LIB_DIR_SUFFIX/<model>
2685 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2699 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2716 <row topline="true">
2717 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2732 $LIB_DIR_SUFFIX/<model>
2735 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2788 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2844 <row topline="true" bottomline="true">
2845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2854 $LIB_DIR_SUFFIX/<model>
2857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2862 /usr/local/share/sdcc/
2869 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2887 Don't delete any of the stray spaces in the table above without checking
2888 the HTML output (last line)!
2904 -nostdlib disables the last two search paths.
2908 \begin_inset LatexCommand \index{Building SDCC}
2915 Building SDCC on Linux
2916 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2925 Download the source package
2927 either from the SDCC CVS repository or from the nightly snapshots
2929 , it will be named something like sdcc
2940 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2949 Bring up a command line terminal, such as xterm.
2954 Unpack the file using a command like:
2957 "tar -xvzf sdcc.src.tar.gz
2962 , this will create a sub-directory called sdcc with all of the sources.
2965 Change directory into the main SDCC directory, for example type:
2982 This configures the package for compilation on your system.
2998 All of the source packages will compile, this can take a while.
3014 This copies the binary executables, the include files, the libraries and
3015 the documentation to the install directories.
3016 Proceed with section
3017 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3024 Building SDCC on OSX 2.x
3027 Follow the instruction for Linux.
3031 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3032 )) fails to compile SDCC.
3033 Fortunately there's also gcc 2.9.x installed, which works fine.
3034 This compiler can be selected by running 'configure' with:
3037 ./configure CC=gcc2 CXX=g++2
3040 Cross compiling SDCC on Linux for Windows
3043 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3044 See section 'Configure Options'.
3047 Building SDCC on Windows
3050 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3052 They use Unix-sockets, which are not available on Win32.
3055 Building SDCC using Cygwin and Mingw32
3058 For building and installing a Cygwin executable follow the instructions
3064 \begin_inset Quotes sld
3068 \begin_inset Quotes srd
3071 Win32-binary can be built, which will not need the Cygwin-DLL.
3072 For the necessary 'configure' options see section 'configure options' or
3073 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3077 In order to install Cygwin on Windows download setup.exe from
3078 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3084 \begin_inset Quotes sld
3087 default text file type
3088 \begin_inset Quotes srd
3092 \begin_inset Quotes sld
3096 \begin_inset Quotes srd
3099 and download/install at least the following packages.
3100 Some packages are selected by default, others will be automatically selected
3101 because of dependencies with the manually selected packages.
3102 Never deselect these packages!
3111 gcc ; version 3.x is fine, no need to use the old 2.9x
3114 binutils ; selected with gcc
3120 rxvt ; a nice console, which makes life much easier under windoze (see below)
3123 man ; not really needed for building SDCC, but you'll miss it sooner or
3127 less ; not really needed for building SDCC, but you'll miss it sooner or
3131 cvs ; only if you use CVS access
3134 If you want to develop something you'll need:
3137 python ; for the regression tests
3140 gdb ; the gnu debugger, together with the nice GUI
3141 \begin_inset Quotes sld
3145 \begin_inset Quotes srd
3151 openssh ; to access the CF or commit changes
3154 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3155 use autoconf-stable!
3158 rxvt is a nice console with history.
3159 Replace in your cygwin.bat the line
3178 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3181 -bg black -fg white -geometry 100x65 -e bash -
3194 Text selected with the mouse is automatically copied to the clipboard, pasting
3195 works with shift-insert.
3199 The other good tip is to make sure you have no //c/-style paths anywhere,
3200 use /cygdrive/c/ instead.
3201 Using // invokes a network lookup which is very slow.
3203 \begin_inset Quotes sld
3207 \begin_inset Quotes srd
3210 is too long, you can change it with e.g.
3216 SDCC sources use the unix line ending LF.
3217 Life is much easier, if you store the source tree on a drive which is mounted
3219 And use an editor which can handle LF-only line endings.
3220 Make sure not to commit files with windows line endings.
3221 The tabulator spacing
3222 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3226 used in the project is 8.
3227 Although a tabulator spacing of 8 is a sensible choice for programmers
3228 (it's a power of 2 and allows to display 8/16 bit signed variables without
3229 loosing columns) the plan is to move towards using only spaces in the source.
3232 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3237 Download the source package
3239 either from the SDCC CVS repository or from the
3240 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3246 , it will be named something like sdcc
3253 SDCC is distributed with all the projects, workspaces, and files you need
3254 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3255 doesn't build under MSVC).
3256 The workspace name is 'sdcc.dsw'.
3257 Please note that as it is now, all the executables are created in a folder
3261 Once built you need to copy the executables from sdcc
3265 bin before running SDCC.
3270 WARNING: Visual studio is very picky with line terminations; it expects
3271 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3272 When using the CVS repository it's easiest to configure the cvs client
3273 to convert automatically for you.
3274 If however you are getting a message such as "This makefile was not generated
3275 by Developer Studio etc.
3277 \begin_inset Quotes srd
3280 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3281 need to convert the Unix style line endings to DOS style line endings.
3282 To do so you can use the
3283 \begin_inset Quotes sld
3287 \begin_inset Quotes srd
3290 utility freely available on the internet.
3291 Doug Hawkins reported in the sdcc-user list that this works:
3299 SDCC> unix2dos sdcc.dsw
3305 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3309 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3310 flex.exe, and gawk.exe.
3311 One good place to get them is
3312 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3320 Download the file UnxUtils
3321 \begin_inset LatexCommand \index{UnxUtils}
3326 Now you have to install the utilities and setup MSVC so it can locate the
3328 Here there are two alternatives (choose one!):
3335 a) Extract UnxUtils.zip to your C:
3337 hard disk PRESERVING the original paths, otherwise bison won't work.
3338 (If you are using WinZip make certain that 'Use folder names' is selected)
3342 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3343 in 'Show directories for:' select 'Executable files', and in the directories
3344 window add a new path: 'C:
3354 (As a side effect, you get a bunch of Unix utilities that could be useful,
3355 such as diff and patch.)
3362 This one avoids extracting a bunch of files you may not use, but requires
3367 a) Create a directory were to put the tools needed, or use a directory already
3375 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3376 to such directory WITHOUT preserving the original paths.
3377 (If you are using WinZip make certain that 'Use folder names' is not selected)
3381 c) Rename bison.exe to '_bison.exe'.
3385 d) Create a batch file 'bison.bat' in 'C:
3389 ' and add these lines:
3409 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3413 Steps 'c' and 'd' are needed because bison requires by default that the
3414 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3415 '/usr/local/share/' I think.
3416 So it is necessary to tell bison where those files are located if they
3417 are not in such directory.
3418 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3422 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3423 in 'Show directories for:' select 'Executable files', and in the directories
3424 window add a new path: 'c:
3427 Note that you can use any other path instead of 'c:
3429 util', even the path where the Visual C++ tools are, probably: 'C:
3433 Microsoft Visual Studio
3438 So you don't have to execute step 'e' :)
3442 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3443 the executables from sdcc
3447 bin, and you can compile using SDCC.
3450 Building SDCC Using Borland
3453 From the sdcc directory, run the command "make -f Makefile.bcc".
3454 This should regenerate all the .exe files in the bin directory except for
3455 sdcdb.exe (which currently doesn't build under Borland C++).
3458 If you modify any source files and need to rebuild, be aware that the dependenci
3459 es may not be correctly calculated.
3460 The safest option is to delete all .obj files and run the build again.
3461 From a Cygwin BASH prompt, this can easily be done with the command (be
3462 sure you are in the sdcc directory):
3472 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3474 ) -print -exec rm {}
3483 or on Windows NT/2000/XP from the command prompt with the command:
3490 del /s *.obj *.lib *.rul
3493 from the sdcc directory.
3496 Windows Install Using a ZIP Package
3499 Download the binary zip package from
3500 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3504 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3505 This should unpack to a group of sub-directories.
3506 An example directory structure after unpacking the mingw32 package is:
3511 bin for the executables, c:
3519 lib for the include and libraries.
3522 Adjust your environment variable PATH to include the location of the bin
3523 directory or start sdcc using the full path.
3526 Windows Install Using the Setup Program
3527 \begin_inset LatexCommand \label{sub:Windows-Install}
3534 Download the setup program
3536 sdcc-x.y.z-setup.exe
3538 for an official release from
3541 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3545 or a setup program for one of the snapshots
3547 sdcc_yyyymmdd_setup.exe
3550 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3555 A windows typical installer will guide you through the installation process.
3558 Building the Documentation
3561 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3562 as changing into the doc directory and typing
3566 \begin_inset Quotes srd
3570 \begin_inset Quotes srd
3577 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3580 \begin_inset LatexCommand \url{http://www.lyx.org}
3584 as editor this is straightforward.
3585 Prebuilt documentation in html and pdf format is available from
3586 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3593 Reading the Documentation
3596 Currently reading the document in pdf format is recommended, as for unknown
3597 reason the hyperlinks are working there whereas in the html version they
3604 If you should know why please drop us a note
3610 You'll find the pdf version
3611 \begin_inset LatexCommand \index{PDF version of this document}
3616 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3624 \begin_inset LatexCommand \index{HTML version of this document}
3629 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3635 This documentation is in some aspects different from a commercial documentation:
3639 It tries to document SDCC for several processor architectures in one document
3640 (commercially these probably would be separate documents/products).
3642 \begin_inset LatexCommand \index{Status of documentation}
3646 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3648 Z80, PIC14, PIC16 and HC08.
3651 There are many references pointing away from this documentation.
3652 Don't let this distract you.
3654 was a reference like
3655 \begin_inset LatexCommand \url{http://www.opencores.org}
3659 together with a statement
3660 \begin_inset Quotes sld
3663 some processors which are targetted by SDCC can be implemented in a
3680 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3685 \begin_inset Quotes srd
3688 we expect you to have a quick look there and come back.
3689 If you read this you are on the right track.
3692 Some sections attribute more space to problems, restrictions and warnings
3693 than to the solution.
3696 The installation section and the section about the debugger is intimidating.
3699 There are still lots of typos and there are more different writing styles
3703 Testing the SDCC Compiler
3704 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3711 The first thing you should do after installing your SDCC compiler is to
3727 \begin_inset LatexCommand \index{version}
3734 at the prompt, and the program should run and tell you the version.
3735 If it doesn't run, or gives a message about not finding sdcc program, then
3736 you need to check over your installation.
3737 Make sure that the sdcc bin directory is in your executable search path
3738 defined by the PATH environment setting (
3743 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3750 Install trouble-shooting for suggestions
3753 Make sure that the sdcc program is in the bin folder, if not perhaps something
3754 did not install correctly.
3762 is commonly installed as described in section
3763 \begin_inset Quotes sld
3766 Install and search paths
3767 \begin_inset Quotes srd
3776 Make sure the compiler works on a very simple example.
3777 Type in the following test.c program using your favorite
3803 Compile this using the following command:
3812 If all goes well, the compiler will generate a test.asm and test.rel file.
3813 Congratulations, you've just compiled your first program with SDCC.
3814 We used the -c option to tell SDCC not to link the generated code, just
3815 to keep things simple for this step.
3823 The next step is to try it with the linker.
3833 If all goes well the compiler will link with the libraries and produce
3834 a test.ihx output file.
3839 (no test.ihx, and the linker generates warnings), then the problem is most
3848 usr/local/share/sdcc/lib directory
3855 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3862 Install trouble-shooting for suggestions).
3870 The final test is to ensure
3878 header files and libraries.
3879 Edit test.c and change it to the following:
3896 strcpy(str1, "testing");
3903 Compile this by typing
3910 This should generate a test.ihx output file, and it should give no warnings
3911 such as not finding the string.h file.
3912 If it cannot find the string.h file, then the problem is that
3916 cannot find the /usr/local/share/sdcc/include directory
3923 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3930 Install trouble-shooting section for suggestions).
3948 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3952 to find exactly where SDCC is looking for the include and lib files.
3955 Install Trouble-shooting
3956 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3961 \begin_inset LatexCommand \index{Install trouble-shooting}
3968 If SDCC does not build correctly
3971 A thing to try is starting from scratch by unpacking the .tgz source package
3972 again in an empty directory.
3980 ./configure 2>&1 | tee configure.log
3994 make 2>&1 | tee make.log
4001 If anything goes wrong, you can review the log files to locate the problem.
4002 Or a relevant part of this can be attached to an email that could be helpful
4003 when requesting help from the mailing list.
4007 \begin_inset Quotes sld
4011 \begin_inset Quotes srd
4018 \begin_inset Quotes sld
4022 \begin_inset Quotes srd
4025 command is a script that analyzes your system and performs some configuration
4026 to ensure the source package compiles on your system.
4027 It will take a few minutes to run, and will compile a few tests to determine
4028 what compiler features are installed.
4032 \begin_inset Quotes sld
4036 \begin_inset Quotes srd
4042 This runs the GNU make tool, which automatically compiles all the source
4043 packages into the final installed binary executables.
4047 \begin_inset Quotes sld
4051 \begin_inset Quotes erd
4057 This will install the compiler, other executables libraries and include
4058 files into the appropriate directories.
4060 \begin_inset LatexCommand \ref{sub:Install-paths}
4066 \begin_inset LatexCommand \ref{sub:Search-Paths}
4071 about install and search paths.
4073 On most systems you will need super-user privileges to do this.
4079 SDCC is not just a compiler, but a collection of tools by various developers.
4080 These include linkers, assemblers, simulators and other components.
4081 Here is a summary of some of the components.
4082 Note that the included simulator and assembler have separate documentation
4083 which you can find in the source package in their respective directories.
4084 As SDCC grows to include support for other processors, other packages from
4085 various developers are included and may have their own sets of documentation.
4089 You might want to look at the files which are installed in <installdir>.
4090 At the time of this writing, we find the following programs for gcc-builds:
4094 In <installdir>/bin:
4097 sdcc - The compiler.
4100 sdcpp - The C preprocessor.
4103 asx8051 - The assembler for 8051 type processors.
4110 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4113 aslink -The linker for 8051 type processors.
4120 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4123 s51 - The ucSim 8051 simulator.
4124 Not available on Win32 platforms.
4127 sdcdb - The source debugger.
4128 Not available on Win32 platforms.
4131 packihx - A tool to pack (compress) Intel hex files.
4134 In <installdir>/share/sdcc/include
4140 In <installdir>/share/sdcc/lib
4143 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4147 In <installdir>/share/sdcc/doc
4153 As development for other processors proceeds, this list will expand to include
4154 executables to support processors like AVR, PIC, etc.
4160 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4161 the assembler and linkage editor.
4164 sdcpp - The C-Preprocessor
4168 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4172 is a modified version of the GNU preprocessor.
4173 The C preprocessor is used to pull in #include sources, process #ifdef
4174 statements, #defines and so on.
4185 - The Assemblers and Linkage Editors
4188 This is retargettable assembler & linkage editor, it was developed by Alan
4190 John Hartman created the version for 8051, and I (Sandeep) have made some
4191 enhancements and bug fixes for it to work properly with SDCC.
4198 \begin_inset LatexCommand \index{s51}
4202 is a freeware, opensource simulator developed by Daniel Drotos.
4203 The simulator is built as part of the build process.
4204 For more information visit Daniel's web site at:
4205 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4210 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4212 S51 is currently not available on Win32 platfors.
4215 sdcdb - Source Level Debugger
4219 \begin_inset LatexCommand \index{sdcdb (debugger)}
4223 is the companion source level debugger.
4224 More about sdcdb in section
4225 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4230 The current version of the debugger uses Daniel's Simulator S51
4231 \begin_inset LatexCommand \index{s51}
4235 , but can be easily changed to use other simulators.
4236 Sdcdb is currently not available on Win32 platfors.
4245 Single Source File Projects
4248 For single source file 8051 projects the process is very simple.
4249 Compile your programs with the following command
4252 "sdcc sourcefile.c".
4256 This will compile, assemble and link your source file.
4257 Output files are as follows:
4261 \begin_inset LatexCommand \index{<file>.asm}
4266 \begin_inset LatexCommand \index{Assembler source}
4270 file created by the compiler
4274 \begin_inset LatexCommand \index{<file>.lst}
4279 \begin_inset LatexCommand \index{Assembler listing}
4283 file created by the Assembler
4287 \begin_inset LatexCommand \index{<file>.rst}
4292 \begin_inset LatexCommand \index{Assembler listing}
4296 file updated with linkedit information, created by linkage editor
4300 \begin_inset LatexCommand \index{<file>.sym}
4305 \begin_inset LatexCommand \index{Symbol listing}
4309 for the sourcefile, created by the assembler
4313 \begin_inset LatexCommand \index{<file>.rel}
4318 \begin_inset LatexCommand \index{<file>.o}
4323 \begin_inset LatexCommand \index{Object file}
4327 created by the assembler, input to Linkage editor
4331 \begin_inset LatexCommand \index{<file>.map}
4336 \begin_inset LatexCommand \index{Memory map}
4340 for the load module, created by the Linker
4344 \begin_inset LatexCommand \index{<file>.mem}
4348 - A file with a summary of the memory usage
4352 \begin_inset LatexCommand \index{<file>.ihx}
4356 - The load module in Intel hex format
4357 \begin_inset LatexCommand \index{Intel hex format}
4361 (you can select the Motorola S19 format
4362 \begin_inset LatexCommand \index{Motorola S19 format}
4377 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4382 If you need another format you might want to use
4389 \begin_inset LatexCommand \index{objdump (tool)}
4400 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4405 Both formats are documented in the documentation of srecord
4406 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4414 \begin_inset LatexCommand \index{<file>.adb}
4418 - An intermediate file containing debug information needed to create the
4430 \begin_inset LatexCommand \index{-\/-debug}
4438 \begin_inset LatexCommand \index{<file>.cdb}
4442 - An optional file (with -
4452 -debug) containing debug information.
4453 The format is documented in cdbfileformat.pdf
4458 \begin_inset LatexCommand \index{<file> (no extension)}
4462 An optional AOMF or AOMF51
4463 \begin_inset LatexCommand \index{AOMF, AOMF51}
4467 file containing debug information (generated with option -
4494 ormat is commonly used by third party tools (debuggers
4495 \begin_inset LatexCommand \index{Debugger}
4499 , simulators, emulators)
4503 \begin_inset LatexCommand \index{<file>.dump*}
4507 - Dump file to debug the compiler it self (generated with option -
4517 -dumpall) (see section
4518 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4524 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4530 \begin_inset Quotes sld
4533 Anatomy of the compiler
4534 \begin_inset Quotes srd
4540 Projects with Multiple Source Files
4543 SDCC can compile only ONE file at a time.
4544 Let us for example assume that you have a project containing the following
4549 foo1.c (contains some functions)
4551 foo2.c (contains some more functions)
4553 foomain.c (contains more functions and the function main)
4561 The first two files will need to be compiled separately with the commands:
4593 Then compile the source file containing the
4598 \begin_inset LatexCommand \index{Linker}
4602 the files together with the following command:
4610 foomain.c\SpecialChar ~
4611 foo1.rel\SpecialChar ~
4616 \begin_inset LatexCommand \index{<file>.rel}
4628 can be separately compiled as well:
4639 sdcc foomain.rel foo1.rel foo2.rel
4646 The file containing the
4661 file specified in the command line, since the linkage editor processes
4662 file in the order they are presented to it.
4663 The linker is invoked from SDCC using a script file with extension .lnk
4664 \begin_inset LatexCommand \index{<file>.lnk}
4669 You can view this file to troubleshoot linking problems such as those arising
4670 from missing libraries.
4673 Projects with Additional Libraries
4674 \begin_inset LatexCommand \index{Libraries}
4681 Some reusable routines may be compiled into a library, see the documentation
4682 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4686 \begin_inset LatexCommand \index{<file>.lib}
4693 Libraries created in this manner can be included in the command line.
4694 Make sure you include the -L <library-path> option to tell the linker where
4695 to look for these files if they are not in the current directory.
4696 Here is an example, assuming you have the source file
4708 (if that is not the same as your current project):
4715 sdcc foomain.c foolib.lib -L mylib
4726 must be an absolute path name.
4730 The most efficient way to use libraries is to keep separate modules in separate
4732 The lib file now should name all the modules.rel
4733 \begin_inset LatexCommand \index{<file>.rel}
4738 For an example see the standard library file
4742 in the directory <installdir>/share/lib/small.
4745 Using sdcclib to Create and Manage Libraries
4746 \begin_inset LatexCommand \index{sdcclib}
4753 Alternatively, instead of having a .rel file for each entry on the library
4754 file as described in the preceding section, sdcclib can be used to embed
4755 all the modules belonging to such library in the library file itself.
4756 This results in a larger library file, but it greatly reduces the number
4757 of disk files accessed by the linker.
4758 Additionally, the packed library file contains an index of all include
4759 modules and symbols that significantly speeds up the linking process.
4760 To display a list of options supported by sdcclib type:
4769 \begin_inset LatexCommand \index{sdcclib}
4780 To create a new library file, start by compiling all the required modules.
4818 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4820 The next step is to add the .rel files to the library file:
4828 sdcclib libint.lib _divsint.rel
4831 \begin_inset LatexCommand \index{sdcclib}
4841 sdcclib libint.lib _divuint.rel
4847 sdcclib libint.lib _modsint.rel
4853 sdcclib libint.lib _moduint.rel
4859 sdcclib libint.lib _mulint.rel
4866 If the file already exists in the library, it will be replaced.
4867 To see what modules and symbols are included in the library, options -s
4868 and -m are available.
4876 sdcclib -s libint.lib
4879 \begin_inset LatexCommand \index{sdcclib}
4989 If the source files are compiled using -
5000 \begin_inset LatexCommand \index{-\/-debug}
5004 , the corresponding debug information file .adb will be include in the library
5006 The library files created with sdcclib are plain text files, so they can
5007 be viewed with a text editor.
5008 It is not recomended to modify a library file created with sdcclib using
5009 a text editor, as there are file indexes numbers located accross the file
5010 used by the linker to quickly locate the required module to link.
5011 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5012 it can be safely deleted, since all the information required for linking
5013 is embedded in the library file itself.
5014 Library files created using sdcclib are used as described in the preceding
5018 Command Line Options
5019 \begin_inset LatexCommand \index{Command Line Options}
5026 Processor Selection Options
5027 \begin_inset LatexCommand \index{Options processor selection}
5032 \begin_inset LatexCommand \index{Processor selection options}
5038 \labelwidthstring 00.00.0000
5043 \begin_inset LatexCommand \index{-mmcs51}
5049 Generate code for the Intel MCS51
5050 \begin_inset LatexCommand \index{MCS51}
5054 family of processors.
5055 This is the default processor target.
5057 \labelwidthstring 00.00.0000
5062 \begin_inset LatexCommand \index{-mds390}
5068 Generate code for the Dallas DS80C390
5069 \begin_inset LatexCommand \index{DS80C390}
5075 \labelwidthstring 00.00.0000
5080 \begin_inset LatexCommand \index{-mds400}
5086 Generate code for the Dallas DS80C400
5087 \begin_inset LatexCommand \index{DS80C400}
5093 \labelwidthstring 00.00.0000
5098 \begin_inset LatexCommand \index{-mhc08}
5104 Generate code for the Freescale/Motorola HC08
5105 \begin_inset LatexCommand \index{HC08}
5109 family of processors.
5111 \labelwidthstring 00.00.0000
5116 \begin_inset LatexCommand \index{-mz80}
5122 Generate code for the Zilog Z80
5123 \begin_inset LatexCommand \index{Z80}
5127 family of processors.
5129 \labelwidthstring 00.00.0000
5134 \begin_inset LatexCommand \index{-mgbz80}
5140 Generate code for the GameBoy Z80
5141 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5145 processor (Not actively maintained).
5147 \labelwidthstring 00.00.0000
5152 \begin_inset LatexCommand \index{-mavr}
5158 Generate code for the Atmel AVR
5159 \begin_inset LatexCommand \index{AVR}
5163 processor (In development, not complete).
5164 AVR users should probably have a look at winavr
5165 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5170 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5177 I think it is fair to direct users there for now.
5178 Open source is also about avoiding unnecessary work .
5179 But I didn't find the 'official' link.
5181 \labelwidthstring 00.00.0000
5186 \begin_inset LatexCommand \index{-mpic14}
5192 Generate code for the Microchip PIC 14
5193 \begin_inset LatexCommand \index{PIC14}
5197 -bit processors (p16f84 and variants.
5198 In development, not complete).
5201 p16f627 p16f628 p16f84 p16f873 p16f877?
5203 \labelwidthstring 00.00.0000
5208 \begin_inset LatexCommand \index{-mpic16}
5214 Generate code for the Microchip PIC 16
5215 \begin_inset LatexCommand \index{PIC16}
5219 -bit processors (p18f452 and variants.
5220 In development, not complete).
5222 \labelwidthstring 00.00.0000
5228 Generate code for the Toshiba TLCS-900H
5229 \begin_inset LatexCommand \index{TLCS-900H}
5233 processor (Not maintained, not complete).
5235 \labelwidthstring 00.00.0000
5240 \begin_inset LatexCommand \index{-mxa51}
5246 Generate code for the Phillips XA51
5247 \begin_inset LatexCommand \index{XA51}
5251 processor (Not maintained, not complete).
5254 Preprocessor Options
5255 \begin_inset LatexCommand \index{Options preprocessor}
5260 \begin_inset LatexCommand \index{Preprocessor options}
5265 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5271 \labelwidthstring 00.00.0000
5276 \begin_inset LatexCommand \index{-I<path>}
5282 The additional location where the pre processor will look for <..h> or
5283 \begin_inset Quotes eld
5287 \begin_inset Quotes erd
5292 \labelwidthstring 00.00.0000
5297 \begin_inset LatexCommand \index{-D<macro[=value]>}
5303 Command line definition of macros.
5304 Passed to the preprocessor.
5306 \labelwidthstring 00.00.0000
5311 \begin_inset LatexCommand \index{-M}
5317 Tell the preprocessor to output a rule suitable for make describing the
5318 dependencies of each object file.
5319 For each source file, the preprocessor outputs one make-rule whose target
5320 is the object file name for that source file and whose dependencies are
5321 all the files `#include'd in it.
5322 This rule may be a single line or may be continued with `
5324 '-newline if it is long.
5325 The list of rules is printed on standard output instead of the preprocessed
5328 \begin_inset LatexCommand \index{-E}
5334 \labelwidthstring 00.00.0000
5339 \begin_inset LatexCommand \index{-C}
5345 Tell the preprocessor not to discard comments.
5346 Used with the `-E' option.
5348 \labelwidthstring 00.00.0000
5353 \begin_inset LatexCommand \index{-MM}
5364 Like `-M' but the output mentions only the user header files included with
5366 \begin_inset Quotes eld
5370 System header files included with `#include <file>' are omitted.
5372 \labelwidthstring 00.00.0000
5377 \begin_inset LatexCommand \index{-Aquestion(answer)}
5383 Assert the answer answer for question, in case it is tested with a preprocessor
5384 conditional such as `#if #question(answer)'.
5385 `-A-' disables the standard assertions that normally describe the target
5388 \labelwidthstring 00.00.0000
5393 \begin_inset LatexCommand \index{-Umacro}
5399 Undefine macro macro.
5400 `-U' options are evaluated after all `-D' options, but before any `-include'
5401 and `-imacros' options.
5403 \labelwidthstring 00.00.0000
5408 \begin_inset LatexCommand \index{-dM}
5414 Tell the preprocessor to output only a list of the macro definitions that
5415 are in effect at the end of preprocessing.
5416 Used with the `-E' option.
5418 \labelwidthstring 00.00.0000
5423 \begin_inset LatexCommand \index{-dD}
5429 Tell the preprocessor to pass all macro definitions into the output, in
5430 their proper sequence in the rest of the output.
5432 \labelwidthstring 00.00.0000
5437 \begin_inset LatexCommand \index{-dN}
5448 Like `-dD' except that the macro arguments and contents are omitted.
5449 Only `#define name' is included in the output.
5451 \labelwidthstring 00.00.0000
5456 preprocessorOption[,preprocessorOption]
5459 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5464 Pass the preprocessorOption to the preprocessor
5469 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5474 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5475 Collection (gcc), if you need more dedicated options please refer to the
5477 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5485 \begin_inset LatexCommand \index{Options linker}
5490 \begin_inset LatexCommand \index{Linker options}
5496 \labelwidthstring 00.00.0000
5516 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5521 \begin_inset LatexCommand \index{-L -\/-lib-path}
5528 <absolute path to additional libraries> This option is passed to the linkage
5529 editor's additional libraries
5530 \begin_inset LatexCommand \index{Libraries}
5535 The path name must be absolute.
5536 Additional library files may be specified in the command line.
5537 See section Compiling programs for more details.
5539 \labelwidthstring 00.00.0000
5556 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5561 <Value> The start location of the external ram
5562 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5566 , default value is 0.
5567 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5577 -xram-loc 0x8000 or -
5589 \labelwidthstring 00.00.0000
5606 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5611 <Value> The start location of the code
5612 \begin_inset LatexCommand \index{code}
5616 segment, default value 0.
5617 Note when this option is used the interrupt vector table is also relocated
5618 to the given address.
5619 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5629 -code-loc 0x8000 or -
5641 \labelwidthstring 00.00.0000
5658 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5663 <Value> By default the stack
5664 \begin_inset LatexCommand \index{stack}
5668 is placed after the data segment.
5669 Using this option the stack can be placed anywhere in the internal memory
5671 The value entered can be in Hexadecimal or Decimal format, e.g.
5682 -stack-loc 0x20 or -
5693 Since the sp register is incremented before a push or call, the initial
5694 sp will be set to one byte prior the provided value.
5695 The provided value should not overlap any other memory areas such as used
5696 register banks or the data segment and with enough space for the current
5714 \begin_inset LatexCommand \index{-\/-pack-iram}
5718 option (which is now a default setting) will override this setting, so
5719 you should also specify the
5735 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5739 option if you need to manually place the stack.
5741 \labelwidthstring 00.00.0000
5758 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
5763 <Value> By default the external stack
5764 \begin_inset LatexCommand \index{xstack}
5768 is placed after the pdata segment.
5769 Using this option the xstack can be placed anywhere in the external memory
5771 The value entered can be in Hexadecimal or Decimal format, e.g.
5782 -xstack-loc 0x8000 or -
5793 The provided value should not overlap any other memory areas such as the
5794 pdata or xdata segment and with enough space for the current application.
5796 \labelwidthstring 00.00.0000
5813 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5818 <Value> The start location of the internal ram data
5819 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5824 The value entered can be in Hexadecimal or Decimal format, eg.
5846 (By default, the start location of the internal ram data segment is set
5847 as low as possible in memory, taking into account the used register banks
5848 and the bit segment at address 0x20.
5849 For example if register banks 0 and 1 are used without bit variables, the
5850 data segment will be set, if -
5860 -data-loc is not used, to location 0x10.)
5862 \labelwidthstring 00.00.0000
5879 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5884 <Value> The start location of the indirectly addressable internal ram
5885 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5889 of the 8051, default value is 0x80.
5890 The value entered can be in Hexadecimal or Decimal format, eg.
5901 -idata-loc 0x88 or -
5913 \labelwidthstring 00.00.0000
5930 <Value> The start location of the bit
5931 \begin_inset LatexCommand \index{bit}
5935 addressable internal ram of the 8051.
5941 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5944 \labelwidthstring 00.00.0000
5959 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5968 The linker output (final object code) is in Intel Hex format.
5969 \begin_inset LatexCommand \index{Intel hex format}
5973 This is the default option.
5974 The format itself is documented in the documentation of srecord
5975 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5981 \labelwidthstring 00.00.0000
5996 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6005 The linker output (final object code) is in Motorola S19 format
6006 \begin_inset LatexCommand \index{Motorola S19 format}
6011 The format itself is documented in the documentation of srecord.
6013 \labelwidthstring 00.00.0000
6028 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6037 The linker output (final object code) is in ELF format
6038 \begin_inset LatexCommand \index{ELF format}
6043 (Currently only supported for the HC08 processors)
6045 \labelwidthstring 00.00.0000
6050 linkOption[,linkOption]
6053 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6058 Pass the linkOption to the linker.
6059 See file sdcc/as/doc/asxhtm.html for more on linker options.
6063 \begin_inset LatexCommand \index{Options MCS51}
6068 \begin_inset LatexCommand \index{MCS51 options}
6074 \labelwidthstring 00.00.0000
6089 \begin_inset LatexCommand \index{-\/-model-small}
6100 Generate code for Small Model programs, see section Memory Models for more
6102 This is the default model.
6104 \labelwidthstring 00.00.0000
6119 \begin_inset LatexCommand \index{-\/-model-medium}
6125 Generate code for Medium model programs, see section Memory Models for
6127 If this option is used all source files in the project have to be compiled
6129 It must also be used when invoking the linker.
6131 \labelwidthstring 00.00.0000
6146 \begin_inset LatexCommand \index{-\/-model-large}
6152 Generate code for Large model programs, see section Memory Models for more
6154 If this option is used all source files in the project have to be compiled
6156 It must also be used when invoking the linker.
6158 \labelwidthstring 00.00.0000
6173 \begin_inset LatexCommand \index{-\/-xstack}
6179 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6180 variables and passing parameters.
6182 \begin_inset LatexCommand \ref{sub:External-Stack}
6187 External Stack for more details.
6189 \labelwidthstring 00.00.0000
6207 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6211 Causes the linker to check if the internal ram usage is within limits of
6214 \labelwidthstring 00.00.0000
6232 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6236 Causes the linker to check if the external ram usage is within limits of
6239 \labelwidthstring 00.00.0000
6257 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6261 Causes the linker to check if the code memory usage is within limits of
6264 \labelwidthstring 00.00.0000
6282 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6286 Causes the linker to check if there is at minimum <Value> bytes for stack.
6288 \labelwidthstring 00.00.0000
6306 \begin_inset LatexCommand \index{-\/-pack-iram}
6310 Causes the linker to use unused register banks for data variables and pack
6311 data, idata and stack together.
6312 This is the default now.
6314 \labelwidthstring 00.00.0000
6332 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6336 Causes the linker to use old style for allocating memory areas.
6339 DS390 / DS400 Options
6340 \begin_inset LatexCommand \index{Options DS390}
6345 \begin_inset LatexCommand \index{DS390 options}
6351 \labelwidthstring 00.00.0000
6368 \begin_inset LatexCommand \index{-\/-model-flat24}
6378 Generate 24-bit flat mode code.
6379 This is the one and only that the ds390 code generator supports right now
6380 and is default when using
6385 See section Memory Models for more details.
6387 \labelwidthstring 00.00.0000
6402 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6408 disable interrupts during ESP:SP updates.
6410 \labelwidthstring 00.00.0000
6427 \begin_inset LatexCommand \index{-\/-stack-10bit}
6431 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6432 This is the one and only that the ds390 code generator supports right now
6433 and is default when using
6438 In this mode, the stack is located in the lower 1K of the internal RAM,
6439 which is mapped to 0x400000.
6440 Note that the support is incomplete, since it still uses a single byte
6441 as the stack pointer.
6442 This means that only the lower 256 bytes of the potential 1K stack space
6443 will actually be used.
6444 However, this does allow you to reclaim the precious 256 bytes of low RAM
6445 for use for the DATA and IDATA segments.
6446 The compiler will not generate any code to put the processor into 10 bit
6448 It is important to ensure that the processor is in this mode before calling
6449 any re-entrant functions compiled with this option.
6450 In principle, this should work with the
6463 \begin_inset LatexCommand \index{-\/-stack-auto}
6469 option, but that has not been tested.
6470 It is incompatible with the
6483 \begin_inset LatexCommand \index{-\/-xstack}
6490 It also only makes sense if the processor is in 24 bit contiguous addressing
6503 -model-flat24 option
6507 \labelwidthstring 00.00.0000
6522 \begin_inset LatexCommand \index{-\/-stack-probe}
6528 insert call to function __stack_probe at each function prologue.
6530 \labelwidthstring 00.00.0000
6545 \begin_inset LatexCommand \index{-\/-tini-libid}
6551 <nnnn> LibraryID used in -mTININative.
6554 \labelwidthstring 00.00.0000
6569 \begin_inset LatexCommand \index{-\/-use-accelerator}
6575 generate code for DS390 Arithmetic Accelerator.
6580 \begin_inset LatexCommand \index{Options Z80}
6585 \begin_inset LatexCommand \index{Z80 options}
6591 \labelwidthstring 00.00.0000
6608 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6618 Force a called function to always save BC.
6620 \labelwidthstring 00.00.0000
6637 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6641 When linking, skip the standard crt0.o object file.
6642 You must provide your own crt0.o for your system when linking.
6646 Optimization Options
6647 \begin_inset LatexCommand \index{Options optimization}
6652 \begin_inset LatexCommand \index{Optimization options}
6658 \labelwidthstring 00.00.0000
6673 \begin_inset LatexCommand \index{-\/-nogcse}
6679 Will not do global subexpression elimination, this option may be used when
6680 the compiler creates undesirably large stack/data spaces to store compiler
6690 \begin_inset LatexCommand \index{sloc (spill location)}
6695 A warning message will be generated when this happens and the compiler
6696 will indicate the number of extra bytes it allocated.
6697 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6699 \begin_inset LatexCommand \index{\#pragma nogcse}
6703 can be used to turn off global subexpression elimination
6704 \begin_inset LatexCommand \index{Subexpression elimination}
6708 for a given function only.
6710 \labelwidthstring 00.00.0000
6725 \begin_inset LatexCommand \index{-\/-noinvariant}
6731 Will not do loop invariant optimizations, this may be turned off for reasons
6732 explained for the previous option.
6733 For more details of loop optimizations performed see Loop Invariants in
6735 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6740 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6742 \begin_inset LatexCommand \index{\#pragma noinvariant}
6746 can be used to turn off invariant optimizations for a given function only.
6748 \labelwidthstring 00.00.0000
6763 \begin_inset LatexCommand \index{-\/-noinduction}
6769 Will not do loop induction optimizations, see section strength reduction
6771 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6773 \begin_inset LatexCommand \index{\#pragma noinduction}
6777 can be used to turn off induction optimizations for a given function only.
6779 \labelwidthstring 00.00.0000
6794 \begin_inset LatexCommand \index{-\/-nojtbound}
6805 Will not generate boundary condition check when switch statements
6806 \begin_inset LatexCommand \index{switch statement}
6810 are implemented using jump-tables.
6812 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6817 Switch Statements for more details.
6818 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6820 \begin_inset LatexCommand \index{\#pragma nojtbound}
6824 can be used to turn off boundary checking for jump tables for a given function
6827 \labelwidthstring 00.00.0000
6842 \begin_inset LatexCommand \index{-\/-noloopreverse}
6851 Will not do loop reversal
6852 \begin_inset LatexCommand \index{Loop reversing}
6858 \labelwidthstring 00.00.0000
6875 \begin_inset LatexCommand \index{-\/-nolabelopt }
6879 Will not optimize labels (makes the dumpfiles more readable).
6881 \labelwidthstring 00.00.0000
6896 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6902 Will not memcpy initialized data from code space into xdata space.
6903 This saves a few bytes in code space if you don't have initialized data
6904 \begin_inset LatexCommand \index{Variable initialization}
6910 \labelwidthstring 00.00.0000
6925 \begin_inset LatexCommand \index{-\/-nooverlay}
6931 The compiler will not overlay parameters and local variables of any function,
6932 see section Parameters and local variables for more details.
6934 \labelwidthstring 00.00.0000
6949 \begin_inset LatexCommand \index{-\/-no-peep}
6955 Disable peep-hole optimization.
6957 \labelwidthstring 00.00.0000
6974 \begin_inset LatexCommand \index{-\/-peep-file}
6979 <filename> This option can be used to use additional rules to be used by
6980 the peep hole optimizer.
6982 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6987 Peep Hole optimizations for details on how to write these rules.
6989 \labelwidthstring 00.00.0000
7004 \begin_inset LatexCommand \index{-\/-peep-asm}
7010 Pass the inline assembler code through the peep hole optimizer.
7011 This can cause unexpected changes to inline assembler code, please go through
7012 the peephole optimizer
7013 \begin_inset LatexCommand \index{Peephole optimizer}
7017 rules defined in the source file tree '<target>/peeph.def' before using
7020 \labelwidthstring 00.00.0000
7035 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7041 The compiler will optimize code generation towards fast code, possibly
7042 at the expense of code size.
7044 \labelwidthstring 00.00.0000
7059 \begin_inset LatexCommand \index{-\/-opt-code-size}
7065 The compiler will optimize code generation towards compact code, possibly
7066 at the expense of code speed.
7070 \begin_inset LatexCommand \index{Options other}
7076 \labelwidthstring 00.00.0000
7092 \begin_inset LatexCommand \index{-\/-compile-only}
7097 \begin_inset LatexCommand \index{-c -\/-compile-only}
7103 will compile and assemble the source, but will not call the linkage editor.
7105 \labelwidthstring 00.00.0000
7124 \begin_inset LatexCommand \index{-\/-c1mode}
7130 reads the preprocessed source from standard input and compiles it.
7131 The file name for the assembler output must be specified using the -o option.
7133 \labelwidthstring 00.00.0000
7138 \begin_inset LatexCommand \index{-E}
7144 Run only the C preprocessor.
7145 Preprocess all the C source files specified and output the results to standard
7148 \labelwidthstring 00.00.0000
7154 \begin_inset LatexCommand \index{-o <path/file>}
7160 The output path resp.
7161 file where everything will be placed.
7162 If the parameter is a path, it must have a trailing slash (or backslash
7163 for the Windows binaries) to be recognized as a path.
7166 \labelwidthstring 00.00.0000
7181 \begin_inset LatexCommand \index{-\/-stack-auto}
7192 All functions in the source file will be compiled as
7197 \begin_inset LatexCommand \index{reentrant}
7202 the parameters and local variables will be allocated on the stack
7203 \begin_inset LatexCommand \index{stack}
7209 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7213 Parameters and Local Variables for more details.
7214 If this option is used all source files in the project should be compiled
7216 It automatically implies --int-long-reent and --float-reent.
7219 \labelwidthstring 00.00.0000
7234 \begin_inset LatexCommand \index{-\/-callee-saves}
7238 function1[,function2][,function3]....
7241 The compiler by default uses a caller saves convention for register saving
7242 across function calls, however this can cause unnecessary register pushing
7243 & popping when calling small functions from larger functions.
7244 This option can be used to switch the register saving convention for the
7245 function names specified.
7246 The compiler will not save registers when calling these functions, no extra
7247 code will be generated at the entry & exit (function prologue
7250 \begin_inset LatexCommand \index{function prologue}
7259 \begin_inset LatexCommand \index{function epilogue}
7265 ) for these functions to save & restore the registers used by these functions,
7266 this can SUBSTANTIALLY reduce code & improve run time performance of the
7268 In the future the compiler (with inter procedural analysis) will be able
7269 to determine the appropriate scheme to use for each function call.
7270 DO NOT use this option for built-in functions such as _mulint..., if this
7271 option is used for a library function the appropriate library function
7272 needs to be recompiled with the same option.
7273 If the project consists of multiple source files then all the source file
7274 should be compiled with the same -
7284 -callee-saves option string.
7285 Also see #pragma\SpecialChar ~
7287 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7293 \labelwidthstring 00.00.0000
7308 \begin_inset LatexCommand \index{-\/-debug}
7317 When this option is used the compiler will generate debug information.
7318 The debug information collected in a file with .cdb extension can be used
7320 For more information see documentation for SDCDB.
7321 Another file with no extension contains debug information in AOMF or AOMF51
7322 \begin_inset LatexCommand \index{AOMF, AOMF51}
7326 format which is commonly used by third party tools.
7328 \labelwidthstring 00.00.0000
7333 \begin_inset LatexCommand \index{-S}
7344 Stop after the stage of compilation proper; do not assemble.
7345 The output is an assembler code file for the input file specified.
7347 \labelwidthstring 00.00.0000
7362 \begin_inset LatexCommand \index{-\/-int-long-reent}
7368 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7369 Note by default these libraries are compiled as non-reentrant.
7370 See section Installation for more details.
7372 \labelwidthstring 00.00.0000
7387 \begin_inset LatexCommand \index{-\/-cyclomatic}
7396 This option will cause the compiler to generate an information message for
7397 each function in the source file.
7398 The message contains some
7402 information about the function.
7403 The number of edges and nodes the compiler detected in the control flow
7404 graph of the function, and most importantly the
7406 cyclomatic complexity
7407 \begin_inset LatexCommand \index{Cyclomatic complexity}
7413 see section on Cyclomatic Complexity for more details.
7415 \labelwidthstring 00.00.0000
7430 \begin_inset LatexCommand \index{-\/-float-reent}
7436 Floating point library is compiled as reentrant
7437 \begin_inset LatexCommand \index{reentrant}
7442 See section Installation for more details.
7444 \labelwidthstring 00.00.0000
7459 \begin_inset LatexCommand \index{-\/-main-return}
7465 This option can be used if the code generated is called by a monitor program
7466 or if the main routine includes an endless loop.
7467 This option might result in slightly smaller code and save two bytes of
7469 The return from the 'main'
7470 \begin_inset LatexCommand \index{main return}
7474 function will return to the function calling main.
7475 The default setting is to lock up i.e.
7482 \labelwidthstring 00.00.0000
7497 \begin_inset LatexCommand \index{-\/-nostdinc}
7503 This will prevent the compiler from passing on the default include path
7504 to the preprocessor.
7506 \labelwidthstring 00.00.0000
7521 \begin_inset LatexCommand \index{-\/-nostdlib}
7527 This will prevent the compiler from passing on the default library
7528 \begin_inset LatexCommand \index{Libraries}
7534 \labelwidthstring 00.00.0000
7549 \begin_inset LatexCommand \index{-\/-verbose}
7555 Shows the various actions the compiler is performing.
7557 \labelwidthstring 00.00.0000
7562 \begin_inset LatexCommand \index{-V}
7568 Shows the actual commands the compiler is executing.
7570 \labelwidthstring 00.00.0000
7585 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7591 Hides your ugly and inefficient c-code from the asm file, so you can always
7592 blame the compiler :)
7594 \labelwidthstring 00.00.0000
7609 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7615 Will not include peep-hole comments in the generated files.
7617 \labelwidthstring 00.00.0000
7632 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7638 Include i-codes in the asm file.
7639 Sounds like noise but is most helpful for debugging the compiler itself.
7641 \labelwidthstring 00.00.0000
7656 \begin_inset LatexCommand \index{-\/-less-pedantic}
7662 Disable some of the more pedantic warnings
7663 \begin_inset LatexCommand \index{Warnings}
7667 (jwk burps: please be more specific here, please!).
7669 \labelwidthstring 00.00.0000
7683 -disable-warning\SpecialChar ~
7685 \begin_inset LatexCommand \index{-\/-disable-warning}
7691 Disable specific warning with number <nnnn>.
7693 \labelwidthstring 00.00.0000
7708 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7714 Display the directories in the compiler's search path
7716 \labelwidthstring 00.00.0000
7731 \begin_inset LatexCommand \index{-\/-vc}
7737 Display errors and warnings using MSVC style, so you can use SDCC with
7740 \labelwidthstring 00.00.0000
7755 \begin_inset LatexCommand \index{-\/-use-stdout}
7761 Send errors and warnings to stdout instead of stderr.
7763 \labelwidthstring 00.00.0000
7768 asmOption[,asmOption]
7771 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7776 Pass the asmOption to the assembler
7777 \begin_inset LatexCommand \index{Options assembler}
7782 \begin_inset LatexCommand \index{Assembler options}
7787 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7789 \labelwidthstring 00.00.0000
7804 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7810 Generally follow the C89 standard, but allow SDCC features that conflict
7811 with the standard (default).
7813 \labelwidthstring 00.00.0000
7828 \begin_inset LatexCommand \index{-\/-std-c89}
7834 Follow the C89 standard and disable SDCC features that conflict with the
7837 \labelwidthstring 00.00.0000
7852 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7858 Generally follow the C99 standard, but allow SDCC features that conflict
7859 with the standard (incomplete support).
7861 \labelwidthstring 00.00.0000
7876 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7882 Follow the C99 standard and disable SDCC features that conflict with the
7883 standard (incomplete support).
7885 \labelwidthstring 00.00.0000
7902 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
7907 <Name> The name to be used for the code
7908 \begin_inset LatexCommand \index{code}
7912 segment, default CSEG.
7913 This is useful if you need to tell the compiler to put the code in a special
7914 segment so you can later on tell the linker to put this segment in a special
7916 Can be used for instance when using bank switching to put the code in a
7919 \labelwidthstring 00.00.0000
7936 \begin_inset LatexCommand \index{-\/-constseg <Value>}
7941 <Name> The name to be used for the const
7942 \begin_inset LatexCommand \index{code}
7946 segment, default CONST.
7947 This is useful if you need to tell the compiler to put the const data in
7948 a special segment so you can later on tell the linker to put this segment
7949 in a special place in memory.
7950 Can be used for instance when using bank switching to put the const data
7953 \labelwidthstring 00.00.0000
7965 a SDCC compiler option but if you want
7969 warnings you can use a separate tool dedicated to syntax checking like
7971 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
7976 \begin_inset LatexCommand \index{lint (syntax checking tool)}
7981 \begin_inset LatexCommand \url{http://www.splint.org}
7986 To make your source files parseable by splint you will have to include
7992 \begin_inset LatexCommand \index{splint (syntax checking tool)}
7996 in your source file and add brackets around extended keywords (like
7999 \begin_inset Quotes sld
8012 \begin_inset Quotes srd
8020 \begin_inset Quotes sld
8023 __interrupt\SpecialChar ~
8025 \begin_inset Quotes srd
8033 Splint has an excellent on line manual at
8034 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8038 and it's capabilities go beyond pure syntax checking.
8039 You'll need to tell splint the location of SDCC's include files so a typical
8040 command line could look like this:
8044 splint\SpecialChar ~
8046 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8051 Intermediate Dump Options
8052 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8057 \begin_inset LatexCommand \index{Options intermediate dump}
8062 \begin_inset LatexCommand \index{Intermediate dump options}
8069 The following options are provided for the purpose of retargetting and debugging
8071 They provide a means to dump the intermediate code (iCode
8072 \begin_inset LatexCommand \index{iCode}
8076 ) generated by the compiler in human readable form at various stages of
8077 the compilation process.
8078 More on iCodes see chapter
8079 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8084 \begin_inset Quotes srd
8087 The anatomy of the compiler
8088 \begin_inset Quotes srd
8093 \labelwidthstring 00.00.0000
8108 \begin_inset LatexCommand \index{-\/-dumpraw}
8114 This option will cause the compiler to dump the intermediate code into
8117 <source filename>.dumpraw
8119 just after the intermediate code has been generated for a function, i.e.
8120 before any optimizations are done.
8122 \begin_inset LatexCommand \index{Basic blocks}
8126 at this stage ordered in the depth first number, so they may not be in
8127 sequence of execution.
8129 \labelwidthstring 00.00.0000
8144 \begin_inset LatexCommand \index{-\/-dumpgcse}
8150 Will create a dump of iCode's, after global subexpression elimination
8151 \begin_inset LatexCommand \index{Global subexpression elimination}
8157 <source filename>.dumpgcse.
8159 \labelwidthstring 00.00.0000
8174 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8180 Will create a dump of iCode's, after deadcode elimination
8181 \begin_inset LatexCommand \index{Dead-code elimination}
8187 <source filename>.dumpdeadcode.
8189 \labelwidthstring 00.00.0000
8204 \begin_inset LatexCommand \index{-\/-dumploop}
8213 Will create a dump of iCode's, after loop optimizations
8214 \begin_inset LatexCommand \index{Loop optimization}
8220 <source filename>.dumploop.
8222 \labelwidthstring 00.00.0000
8237 \begin_inset LatexCommand \index{-\/-dumprange}
8246 Will create a dump of iCode's, after live range analysis
8247 \begin_inset LatexCommand \index{Live range analysis}
8253 <source filename>.dumprange.
8255 \labelwidthstring 00.00.0000
8270 \begin_inset LatexCommand \index{-\/-dumlrange}
8276 Will dump the life ranges
8277 \begin_inset LatexCommand \index{Live range analysis}
8283 \labelwidthstring 00.00.0000
8298 \begin_inset LatexCommand \index{-\/-dumpregassign}
8307 Will create a dump of iCode's, after register assignment
8308 \begin_inset LatexCommand \index{Register assignment}
8314 <source filename>.dumprassgn.
8316 \labelwidthstring 00.00.0000
8331 \begin_inset LatexCommand \index{-\/-dumplrange}
8337 Will create a dump of the live ranges of iTemp's
8339 \labelwidthstring 00.00.0000
8354 \begin_inset LatexCommand \index{-\/-dumpall}
8365 Will cause all the above mentioned dumps to be created.
8368 Redirecting output on Windows Shells
8371 By default SDCC writes it's error messages to
8372 \begin_inset Quotes sld
8376 \begin_inset Quotes srd
8380 To force all messages to
8381 \begin_inset Quotes sld
8385 \begin_inset Quotes srd
8409 \begin_inset LatexCommand \index{-\/-use-stdout}
8414 Additionally, if you happen to have visual studio installed in your windows
8415 machine, you can use it to compile your sources using a custom build and
8431 \begin_inset LatexCommand \index{-\/-vc}
8436 Something like this should work:
8480 -model-large -c $(InputPath)
8483 Environment variables
8484 \begin_inset LatexCommand \index{Environment variables}
8491 SDCC recognizes the following environment variables:
8493 \labelwidthstring 00.00.0000
8498 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8504 SDCC installs a signal handler
8505 \begin_inset LatexCommand \index{signal handler}
8509 to be able to delete temporary files after an user break (^C) or an exception.
8510 If this environment variable is set, SDCC won't install the signal handler
8511 in order to be able to debug SDCC.
8513 \labelwidthstring 00.00.0000
8520 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8526 Path, where temporary files will be created.
8527 The order of the variables is the search order.
8528 In a standard *nix environment these variables are not set, and there's
8529 no need to set them.
8530 On Windows it's recommended to set one of them.
8532 \labelwidthstring 00.00.0000
8537 \begin_inset LatexCommand \index{SDCC\_HOME}
8544 \begin_inset LatexCommand \ref{sub:Install-paths}
8550 \begin_inset Quotes sld
8554 \begin_inset Quotes srd
8559 \labelwidthstring 00.00.0000
8564 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8571 \begin_inset LatexCommand \ref{sub:Search-Paths}
8577 \begin_inset Quotes sld
8581 \begin_inset Quotes srd
8586 \labelwidthstring 00.00.0000
8591 \begin_inset LatexCommand \index{SDCC\_LIB}
8598 \begin_inset LatexCommand \ref{sub:Search-Paths}
8604 \begin_inset Quotes sld
8608 \begin_inset Quotes srd
8614 There are some more environment variables recognized by SDCC, but these
8615 are solely used for debugging purposes.
8616 They can change or disappear very quickly, and will never be documented.
8619 Storage Class Language Extensions
8622 MCS51/DS390 Storage Class
8623 \begin_inset LatexCommand \index{Storage class}
8630 In addition to the ANSI storage classes SDCC allows the following MCS51
8631 specific storage classes:
8632 \layout Subsubsection
8635 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8640 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8645 \begin_inset LatexCommand \index{near (storage class)}
8650 \begin_inset LatexCommand \index{\_\_near (storage class)}
8661 storage class for the Small Memory model (
8669 can be used synonymously).
8670 Variables declared with this storage class will be allocated in the directly
8671 addressable portion of the internal RAM of a 8051, e.g.:
8676 data unsigned char test_data;
8679 Writing 0x01 to this variable generates the assembly code:
8684 75*00 01\SpecialChar ~
8690 \layout Subsubsection
8693 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8698 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8703 \begin_inset LatexCommand \index{far (storage class)}
8708 \begin_inset LatexCommand \index{\_\_far (storage class)}
8715 Variables declared with this storage class will be placed in the external
8721 storage class for the Large Memory model, e.g.:
8726 xdata unsigned char test_xdata;
8729 Writing 0x01 to this variable generates the assembly code:
8734 90s00r00\SpecialChar ~
8763 \layout Subsubsection
8766 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8771 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8778 Variables declared with this storage class will be allocated into the indirectly
8779 addressable portion of the internal ram of a 8051, e.g.:
8784 idata unsigned char test_idata;
8787 Writing 0x01 to this variable generates the assembly code:
8816 Please note, the first 128 byte of idata physically access the same RAM
8818 The original 8051 had 128 byte idata memory, nowadays most devices have
8819 256 byte idata memory.
8821 \begin_inset LatexCommand \index{stack}
8825 is located in idata memory.
8826 \layout Subsubsection
8829 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8834 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8841 Paged xdata access is just as straightforward as using the other addressing
8843 It is typically located at the start of xdata and has a maximum size of
8845 The following example writes 0x01 to the pdata variable.
8846 Please note, pdata access physically accesses xdata memory.
8847 The high byte of the address is determined by port P2
8848 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8852 (or in case of some 8051 variants by a separate Special Function Register,
8854 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8863 storage class for the Medium Memory model, e.g.:
8868 pdata unsigned char test_pdata;
8871 Writing 0x01 to this variable generates the assembly code:
8915 \begin_inset LatexCommand \index{-\/-xstack}
8919 option is used the pdata memory area is followed by the xstack memory area
8920 and the sum of their sizes is limited to 256 bytes.
8921 \layout Subsubsection
8924 \begin_inset LatexCommand \index{code}
8929 \begin_inset LatexCommand \index{\_\_code}
8936 'Variables' declared with this storage class will be placed in the code
8942 code unsigned char test_code;
8945 Read access to this variable generates the assembly code:
8950 90s00r6F\SpecialChar ~
8953 mov dptr,#_test_code
8982 indexed arrays of characters in code memory can be accessed efficiently:
8987 code char test_array[] = {'c','h','e','a','p'};
8990 Read access to this array using an 8-bit unsigned index generates the assembly
9007 90s00r41\SpecialChar ~
9010 mov dptr,#_test_array
9025 \layout Subsubsection
9028 \begin_inset LatexCommand \index{bit}
9033 \begin_inset LatexCommand \index{\_\_bit}
9040 This is a data-type and a storage class specifier.
9041 When a variable is declared as a bit, it is allocated into the bit addressable
9042 memory of 8051, e.g.:
9050 Writing 1 to this variable generates the assembly code:
9066 The bit addressable memory consists of 128 bits which are located from 0x20
9067 to 0x2f in data memory.
9070 Apart from this 8051 specific storage class most architectures support ANSI-C
9072 \begin_inset LatexCommand \index{bitfields}
9082 Not really meant as examples, but nevertheless showing what bitfields are
9083 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9087 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9088 signed modifier are implemented as unsigned.
9089 \layout Subsubsection
9092 \begin_inset LatexCommand \index{sfr}
9097 \begin_inset LatexCommand \index{\_\_sfr}
9102 \begin_inset LatexCommand \index{sfr16}
9107 \begin_inset LatexCommand \index{\_\_sfr16}
9112 \begin_inset LatexCommand \index{sfr32}
9117 \begin_inset LatexCommand \index{\_\_sfr32}
9122 \begin_inset LatexCommand \index{\_\_sbit}
9129 Like the bit keyword,
9131 sfr / sfr16 / sfr32 / sbit
9133 signify both a data-type and storage class, they are used to describe the
9154 variables of a 8051, eg:
9160 \begin_inset LatexCommand \index{at}
9165 \begin_inset LatexCommand \index{\_\_at}
9169 0x80 P0;\SpecialChar ~
9170 /* special function register P0 at location 0x80 */
9172 /* 16 bit special function register combination for timer 0 */
9174 /* with the high byte at location 0x8C and the low byte at location 0x8A
9178 \begin_inset LatexCommand \index{at}
9183 \begin_inset LatexCommand \index{\_\_at}
9189 sbit at 0xd7 CY; /* CY (Carry Flag
9190 \begin_inset LatexCommand \index{Flags}
9195 \begin_inset LatexCommand \index{Carry flag}
9202 Special function registers which are located on an address dividable by
9203 8 are bit-addressable, an
9207 addresses a specific bit within these sfr.
9209 16 Bit and 32 bit special function register combinations which require a
9210 certain access order are better not declared using
9219 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9220 this is not guaranteed.
9221 \layout Subsubsection
9224 \begin_inset LatexCommand \index{Pointer}
9228 to MCS51/DS390 specific memory spaces
9231 SDCC allows (via language extensions) pointers to explicitly point to any
9232 of the memory spaces
9233 \begin_inset LatexCommand \index{Memory model}
9238 In addition to the explicit pointers, the compiler uses (by default) generic
9239 pointers which can be used to point to any of the memory spaces.
9243 Pointer declaration examples:
9248 /* pointer physically in internal ram pointing to object in external ram
9251 xdata unsigned char * data p;
9255 /* pointer physically in external ram pointing to object in internal ram
9258 data unsigned char * xdata p;
9262 /* pointer physically in code rom pointing to data in xdata space */
9264 xdata unsigned char * code p;
9268 /* pointer physically in code space pointing to data in code space */
9270 code unsigned char * code p;
9274 /* the following is a generic pointer physically located in xdata space
9281 /* the following is a function pointer physically located in data space
9284 char (* data fp)(void);
9287 Well you get the idea.
9292 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9305 The highest order byte of the
9309 pointers contains the data space information.
9310 Assembler support routines are called whenever data is stored or retrieved
9316 These are useful for developing reusable library
9317 \begin_inset LatexCommand \index{Libraries}
9322 Explicitly specifying the pointer type will generate the most efficient
9324 \layout Subsubsection
9326 Notes on MCS51 memory
9327 \begin_inset LatexCommand \index{MCS51 memory}
9334 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9335 RAM memory which is structured as follows:
9339 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9342 - Bytes 20-2F - 16 bytes to hold 128 bit
9343 \begin_inset LatexCommand \index{bit}
9349 - Bytes 30-7F - 80 bytes for general purpose use.
9354 Additionally some members of the MCS51 family may have up to 128 bytes of
9355 additional, indirectly addressable, internal RAM memory (
9360 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9365 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9370 Furthermore, some chips may have some built in external memory (
9375 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9380 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9384 ) which should not be confused with the internal, directly addressable RAM
9390 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9395 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9400 Sometimes this built in
9404 memory has to be activated before using it (you can probably find this
9405 information on the datasheet of the microcontroller your are using, see
9407 \begin_inset LatexCommand \ref{sub:Startup-Code}
9415 Normally SDCC will only use the first bank
9416 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9420 of registers (register bank 0), but it is possible to specify that other
9421 banks of registers (keyword
9428 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9433 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9439 ) should be used in interrupt
9440 \begin_inset LatexCommand \index{interrupt}
9445 \begin_inset LatexCommand \index{\_\_interrupt}
9450 By default, the compiler will place the stack after the last byte of allocated
9451 memory for variables.
9452 For example, if the first 2 banks of registers are used, and only four
9457 variables, it will position the base of the internal stack at address 20
9459 This implies that as the stack
9460 \begin_inset LatexCommand \index{stack}
9464 grows, it will use up the remaining register banks, and the 16 bytes used
9465 by the 128 bit variables, and 80 bytes for general purpose use.
9466 If any bit variables are used, the data variables will be placed in unused
9467 register banks and after the byte holding the last bit variable.
9468 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9473 variables will be placed starting from address 0x10 to 0x20 and continue
9486 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9490 to specify the start address of the
9505 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9509 to specify the size of the total internal RAM (
9521 By default the 8051 linker will place the stack after the last byte of (i)data
9534 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9538 allows you to specify the start of the stack, i.e.
9539 you could start it after any data in the general purpose area.
9540 If your microcontroller has additional indirectly addressable internal
9545 ) you can place the stack on it.
9546 You may also need to use -
9557 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9561 to set the start address of the external RAM (
9576 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9580 to specify its size.
9581 Same goes for the code memory, using -
9592 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9607 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9612 If in doubt, don't specify any options and see if the resulting memory
9613 layout is appropriate, then you can adjust it.
9616 The linker generates two files with memory allocation information.
9617 The first, with extension .map
9618 \begin_inset LatexCommand \index{<file>.map}
9622 shows all the variables and segments.
9623 The second with extension .mem
9624 \begin_inset LatexCommand \index{<file>.mem}
9628 shows the final memory layout.
9629 The linker will complain either if memory segments overlap, there is not
9630 enough memory, or there is not enough space for stack.
9631 If you get any linking warnings and/or errors related to stack or segments
9632 allocation, take a look at either the .map or .mem files to find out what
9634 The .mem file may even suggest a solution to the problem.
9637 Z80/Z180 Storage Class
9638 \begin_inset LatexCommand \index{Storage class}
9643 \layout Subsubsection
9646 \begin_inset LatexCommand \index{sfr}
9651 \begin_inset LatexCommand \index{\_\_sfr}
9655 (in/out to 8-bit addresses)
9659 \begin_inset LatexCommand \index{Z80}
9663 family has separate address spaces for memory and
9673 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9677 is accessed with special instructions, e.g.:
9682 sfr at 0x78 IoPort;\SpecialChar ~
9684 /* define a var in I/O space at 78h called IoPort */
9688 Writing 0x01 to this variable generates the assembly code:
9708 \layout Subsubsection
9711 \begin_inset LatexCommand \index{sfr}
9716 \begin_inset LatexCommand \index{\_\_sfr}
9720 (in/out to 16-bit addresses)
9727 is used to support 16 bit addresses in I/O memory e.g.:
9733 \begin_inset LatexCommand \index{at}
9738 \begin_inset LatexCommand \index{\_\_at}
9745 Writing 0x01 to this variable generates the assembly code:
9750 01 23 01\SpecialChar ~
9770 \layout Subsubsection
9773 \begin_inset LatexCommand \index{sfr}
9778 \begin_inset LatexCommand \index{\_\_sfr}
9782 (in0/out0 to 8 bit addresses on Z180
9783 \begin_inset LatexCommand \index{Z180}
9788 \begin_inset LatexCommand \index{HD64180}
9795 The compiler option -
9805 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9807 \begin_inset LatexCommand \index{\#pragma portmode}
9811 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9821 If you include the file z180.h this will be set automatically.
9825 \begin_inset LatexCommand \index{Storage class}
9830 \layout Subsubsection
9833 \begin_inset LatexCommand \index{data (hc08 storage class)}
9838 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9845 The data storage class declares a variable that resides in the first 256
9846 bytes of memory (the direct page).
9847 The HC08 is most efficient at accessing variables (especially pointers)
9849 \layout Subsubsection
9852 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9857 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9864 The xdata storage class declares a variable that can reside anywhere in
9866 This is the default if no storage class is specified.
9871 \begin_inset LatexCommand \index{Absolute addressing}
9878 Data items can be assigned an absolute address with the
9881 \begin_inset LatexCommand \index{at}
9886 \begin_inset LatexCommand \index{\_\_at}
9892 keyword, in addition to a storage class, e.g.:
9898 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9903 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9908 \begin_inset LatexCommand \index{at}
9913 \begin_inset LatexCommand \index{\_\_at}
9917 0x7ffe unsigned int chksum;
9920 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9921 of the external ram.
9926 reserve any space for variables declared in this way (they are implemented
9927 with an equate in the assembler).
9928 Thus it is left to the programmer to make sure there are no overlaps with
9929 other variables that are declared without the absolute address.
9930 The assembler listing file (.lst
9931 \begin_inset LatexCommand \index{<file>.lst}
9935 ) and the linker output files (.rst
9936 \begin_inset LatexCommand \index{<file>.rst}
9941 \begin_inset LatexCommand \index{<file>.map}
9945 ) are good places to look for such overlaps.
9946 Variables with an absolute address are
9951 \begin_inset LatexCommand \index{Variable initialization}
9958 In case of memory mapped I/O devices the keyword
9962 has to be used to tell the compiler that accesses might not be removed:
9968 \begin_inset LatexCommand \index{volatile}
9973 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9978 \begin_inset LatexCommand \index{at}
9982 0x8000 unsigned char PORTA_8255;
9985 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9990 \begin_inset LatexCommand \index{Aligned array}
9997 starts at a block (256 byte) boundary
9998 \begin_inset LatexCommand \index{block boundary}
10003 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10009 Absolute addresses can be specified for variables in all storage classes,
10016 \begin_inset LatexCommand \index{bit}
10021 \begin_inset LatexCommand \index{at}
10028 The above example will allocate the variable at offset 0x02 in the bit-addressab
10030 There is no real advantage to assigning absolute addresses to variables
10031 in this manner, unless you want strict control over all the variables allocated.
10032 One possible use would be to write hardware portable code.
10033 For example, if you have a routine that uses one or more of the microcontroller
10034 I/O pins, and such pins are different for two different hardwares, you
10035 can declare the I/O pins in your routine using:
10041 \begin_inset LatexCommand \index{volatile}
10045 bit MOSI;\SpecialChar ~
10049 /* master out, slave in */
10051 extern volatile bit MISO;\SpecialChar ~
10055 /* master in, slave out */
10057 extern volatile bit MCLK;\SpecialChar ~
10065 /* Input and Output of a byte on a 3-wire serial bus.
10070 If needed adapt polarity of clock, polarity of data and bit order
10075 unsigned char spi_io(unsigned char out_byte)
10099 MOSI = out_byte & 0x80;
10129 /* _asm nop _endasm; */\SpecialChar ~
10137 /* for slow peripherals */
10188 Then, someplace in the code for the first hardware you would use
10194 \begin_inset LatexCommand \index{at}
10199 \begin_inset LatexCommand \index{\_\_at}
10203 0x80 MOSI;\SpecialChar ~
10207 /* I/O port 0, bit 0 */
10209 bit at 0x81 MISO;\SpecialChar ~
10213 /* I/O port 0, bit 1 */
10215 bit at 0x82 MCLK;\SpecialChar ~
10219 /* I/O port 0, bit 2 */
10222 Similarly, for the second hardware you would use
10227 bit at 0x83 MOSI;\SpecialChar ~
10231 /* I/O port 0, bit 3 */
10233 bit at 0x91 MISO;\SpecialChar ~
10237 /* I/O port 1, bit 1 */
10240 \begin_inset LatexCommand \index{bit}
10244 at 0x92 MCLK;\SpecialChar ~
10248 /* I/O port 1, bit 2 */
10251 and you can use the same hardware dependent routine without changes, as
10252 for example in a library.
10253 This is somehow similar to sbit, but only one absolute address has to be
10254 specified in the whole project.
10258 \begin_inset LatexCommand \index{Parameters}
10263 \begin_inset LatexCommand \index{function parameter}
10268 \begin_inset LatexCommand \index{local variables}
10273 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10280 Automatic (local) variables and parameters to functions can either be placed
10281 on the stack or in data-space.
10282 The default action of the compiler is to place these variables in the internal
10283 RAM (for small model) or external RAM (for large model).
10284 This in fact makes them similar to
10287 \begin_inset LatexCommand \index{static}
10293 so by default functions are non-reentrant
10294 \begin_inset LatexCommand \index{reentrant}
10303 They can be placed on the stack
10304 \begin_inset LatexCommand \index{stack}
10321 \begin_inset LatexCommand \index{-\/-stack-auto}
10329 #pragma\SpecialChar ~
10333 \begin_inset LatexCommand \index{\#pragma stackauto}
10340 \begin_inset LatexCommand \index{reentrant}
10346 keyword in the function declaration, e.g.:
10351 unsigned char foo(char i) reentrant
10365 Since stack space on 8051 is limited, the
10383 option should be used sparingly.
10384 Note that the reentrant keyword just means that the parameters & local
10385 variables will be allocated to the stack, it
10389 mean that the function is register bank
10390 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10399 \begin_inset LatexCommand \index{local variables}
10403 can be assigned storage classes and absolute
10404 \begin_inset LatexCommand \index{Absolute addressing}
10413 unsigned char foo()
10421 xdata unsigned char i;
10434 \begin_inset LatexCommand \index{at}
10438 0x31 unsigned char j;
10450 In the above example the variable
10454 will be allocated in the external ram,
10458 in bit addressable space and
10477 or when a function is declared as
10481 this should only be done for static variables.
10485 \begin_inset LatexCommand \index{function parameter}
10489 however are not allowed any storage class
10490 \begin_inset LatexCommand \index{Storage class}
10494 , (storage classes for parameters will be ignored), their allocation is
10495 governed by the memory model in use, and the reentrancy options.
10498 It is however allowed to use bit parameters in reentrant functions and also
10499 non-static local bit variables are supported.
10500 Efficient use is limited to 8 semi-bitregisters in bit space.
10501 They are pushed and popped to stack as a single byte just like the normal
10506 \begin_inset LatexCommand \label{sub:Overlaying}
10511 \begin_inset LatexCommand \index{Overlaying}
10519 \begin_inset LatexCommand \index{reentrant}
10523 functions SDCC will try to reduce internal ram space usage by overlaying
10524 parameters and local variables of a function (if possible).
10525 Parameters and local variables
10526 \begin_inset LatexCommand \index{local variables}
10530 of a function will be allocated to an overlayable segment if the function
10533 no other function calls and the function is non-reentrant and the memory
10535 \begin_inset LatexCommand \index{Memory model}
10542 If an explicit storage class
10543 \begin_inset LatexCommand \index{Storage class}
10547 is specified for a local variable, it will NOT be overlayed.
10550 Note that the compiler (not the linkage editor) makes the decision for overlayin
10552 Functions that are called from an interrupt service routine should be preceded
10553 by a #pragma\SpecialChar ~
10555 \begin_inset LatexCommand \index{\#pragma nooverlay}
10559 if they are not reentrant.
10562 Also note that the compiler does not do any processing of inline assembler
10563 code, so the compiler might incorrectly assign local variables and parameters
10564 of a function into the overlay segment if the inline assembler code calls
10565 other c-functions that might use the overlay.
10566 In that case the #pragma\SpecialChar ~
10567 nooverlay should be used.
10570 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10572 \begin_inset LatexCommand \index{Multiplication}
10577 \begin_inset LatexCommand \index{Division}
10581 will NOT be overlayed since these are implemented using external functions,
10590 \begin_inset LatexCommand \index{\#pragma nooverlay}
10596 void set_error(unsigned char errcd)
10612 void some_isr () interrupt
10613 \begin_inset LatexCommand \index{interrupt}
10643 In the above example the parameter
10651 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10653 not present, this could cause unpredictable runtime behavior when called
10654 from an interrupt service routine.
10655 The #pragma\SpecialChar ~
10656 nooverlay ensures that the parameters and local variables for
10657 the function are NOT overlayed.
10660 Interrupt Service Routines
10661 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10668 General Information
10683 outines to be coded in C, with some extended keywords.
10688 void timer_isr (void) interrupt 1 using 1
10702 The optional number following the
10705 \begin_inset LatexCommand \index{interrupt}
10710 \begin_inset LatexCommand \index{\_\_interrupt}
10716 keyword is the interrupt number this routine will service.
10717 When present, the compiler will insert a call to this routine in the interrupt
10718 vector table for the interrupt number specified.
10719 If you have multiple source files in your project, interrupt service routines
10720 can be present in any of them, but a prototype of the isr MUST be present
10721 or included in the file that contains the function
10729 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10734 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10740 keyword can be used to tell the compiler to use the specified register
10741 bank (8051 specific) when generating code for this function.
10747 Interrupt service routines open the door for some very interesting bugs:
10749 If an interrupt service routine changes variables which are accessed by
10750 other functions these variables have to be declared
10755 \begin_inset LatexCommand \index{volatile}
10763 If the access to these variables is not
10766 \begin_inset LatexCommand \index{atomic}
10773 the processor needs more than one instruction for the access and could
10774 be interrupted while accessing the variable) the interrupt must be disabled
10775 during the access to avoid inconsistent data.
10776 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10777 and should be protected by disabling interrupts.
10778 You're not automatically on the safe side if you use 8 bit variables though.
10779 We need an example here: f.e.
10780 on the 8051 the harmless looking
10781 \begin_inset Quotes srd
10786 flags\SpecialChar ~
10791 \begin_inset Quotes sld
10800 \begin_inset Quotes srd
10805 flags\SpecialChar ~
10810 \begin_inset Quotes sld
10813 from within an interrupt routine might get lost if the interrupt occurs
10816 \begin_inset Quotes sld
10821 counter\SpecialChar ~
10826 \begin_inset Quotes srd
10829 is not atomic on the 8051 even if
10833 is located in data memory.
10834 Bugs like these are hard to reproduce and can cause a lot of trouble.
10838 The return address and the registers used in the interrupt service routine
10839 are saved on the stack
10840 \begin_inset LatexCommand \index{stack}
10844 so there must be sufficient stack space.
10845 If there isn't variables or registers (or even the return address itself)
10852 \begin_inset LatexCommand \index{stack overflow}
10856 is most likely to happen if the interrupt occurs during the
10857 \begin_inset Quotes sld
10861 \begin_inset Quotes srd
10864 subroutine when the stack is already in use for f.e.
10865 many return addresses.
10868 A special note here, int (16 bit) and long (32 bit) integer division
10869 \begin_inset LatexCommand \index{Division}
10874 \begin_inset LatexCommand \index{Multiplication}
10879 \begin_inset LatexCommand \index{Modulus}
10884 \begin_inset LatexCommand \index{Floating point support}
10888 operations are implemented using external support routines developed in
10890 If an interrupt service routine needs to do any of these operations then
10891 the support routines (as mentioned in a following section) will have to
10892 be recompiled using the
10905 \begin_inset LatexCommand \index{-\/-stack-auto}
10911 option and the source file will need to be compiled using the
10926 \begin_inset LatexCommand \index{-\/-int-long-reent}
10933 Calling other functions from an interrupt service routine is not recommended,
10934 avoid it if possible.
10935 Note that when some function is called from an interrupt service routine
10936 it should be preceded by a #pragma\SpecialChar ~
10938 \begin_inset LatexCommand \index{\#pragma nooverlay}
10942 if it is not reentrant.
10943 Furthermore nonreentrant functions should not be called from the main program
10944 while the interrupt service routine might be active.
10945 They also must not be called from low priority interrupt service routines
10946 while a high priority interrupt service routine might be active. You could use
10947 semaphores or make the function
10951 if all parameters are passed in registers.
10956 \begin_inset LatexCommand \ref{sub:Overlaying}
10961 about Overlaying and section
10962 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10967 about Functions using private register banks.
10970 MCS51/DS390 Interrupt Service Routines
10973 Interrupt numbers and the corresponding address & descriptions for the Standard
10974 8051/8052 are listed below.
10975 SDCC will automatically adjust the interrupt vector table to the maximum
10976 interrupt number specified.
10982 \begin_inset Tabular
10983 <lyxtabular version="3" rows="7" columns="3">
10985 <column alignment="center" valignment="top" leftline="true" width="0in">
10986 <column alignment="center" valignment="top" leftline="true" width="0in">
10987 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10988 <row topline="true" bottomline="true">
10989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11014 <row topline="true">
11015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11023 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11031 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11040 <row topline="true">
11041 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11057 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11066 <row topline="true">
11067 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11083 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11092 <row topline="true">
11093 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11101 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11109 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11118 <row topline="true">
11119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11144 <row topline="true" bottomline="true">
11145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11179 If the interrupt service routine is defined without
11182 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11187 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11193 a register bank or with register bank 0 (
11197 0), the compiler will save the registers used by itself on the stack upon
11198 entry and restore them at exit, however if such an interrupt service routine
11199 calls another function then the entire register bank will be saved on the
11201 This scheme may be advantageous for small interrupt service routines which
11202 have low register usage.
11205 If the interrupt service routine is defined to be using a specific register
11210 & psw are saved and restored, if such an interrupt service routine calls
11211 another function (using another register bank) then the entire register
11212 bank of the called function will be saved on the stack.
11213 This scheme is recommended for larger interrupt service routines.
11216 HC08 Interrupt Service Routines
11219 Since the number of interrupts available is chip specific and the interrupt
11220 vector table always ends at the last byte of memory, the interrupt numbers
11221 corresponds to the interrupt vectors in reverse order of address.
11222 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11223 2 will use the interrupt vector at 0xfffa, and so on.
11224 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11225 this way; instead see section
11226 \begin_inset LatexCommand \ref{sub:Startup-Code}
11230 for details on customizing startup.
11233 Z80 Interrupt Service Routines
11236 The Z80 uses several different methods for determining the correct interrupt
11237 vector depending on the hardware implementation.
11238 Therefore, SDCC ignores the optional interrupt number and does not attempt
11239 to generate an interrupt vector table.
11242 By default, SDCC generates code for a maskable interrupt, which uses an
11243 RETI instruction to return from the interrupt.
11244 To write an interrupt handler for the non-maskable interrupt, which needs
11245 an RETN instruction instead, add the
11254 void nmi_isr (void) critical interrupt
11268 Enabling and Disabling Interrupts
11271 Critical Functions and Critical Statements
11274 A special keyword may be associated with a block or a function declaring
11280 SDCC will generate code to disable all interrupts
11281 \begin_inset LatexCommand \index{interrupt}
11285 upon entry to a critical function and restore the interrupt enable to the
11286 previous state before returning.
11287 Nesting critical functions will need one additional byte on the stack
11288 \begin_inset LatexCommand \index{stack}
11297 int foo () critical
11298 \begin_inset LatexCommand \index{critical}
11303 \begin_inset LatexCommand \index{\_\_critical}
11328 The critical attribute maybe used with other attributes like
11338 may also be used to disable interrupts more locally:
11346 More than one statement could have been included in the block.
11349 Enabling and Disabling Interrupts directly
11353 \begin_inset LatexCommand \index{interrupt}
11357 can also be disabled and enabled directly (8051):
11362 EA = 0;\SpecialChar ~
11425 EA = 1;\SpecialChar ~
11492 On other architectures which have seperate opcodes for enabling and disabling
11493 interrupts you might want to make use of defines with inline assembly
11494 \begin_inset LatexCommand \index{Assembler routines}
11504 \begin_inset LatexCommand \index{\_asm}
11513 \begin_inset LatexCommand \index{\_endasm}
11522 #define SEI _asm\SpecialChar ~
11534 Note: it is sometimes sufficient to disable only a specific interrupt source
11536 a timer or serial interrupt by manipulating an
11539 \begin_inset LatexCommand \index{interrupt mask}
11549 Usually the time during which interrupts are disabled should be kept as
11551 This minimizes both
11556 \begin_inset LatexCommand \index{interrupt latency}
11560 (the time between the occurrence of the interrupt and the execution of
11561 the first code in the interrupt routine) and
11566 \begin_inset LatexCommand \index{interrupt jitter}
11570 (the difference between the shortest and the longest interrupt latency).
11571 These really are something different, f.e.
11572 a serial interrupt has to be served before its buffer overruns so it cares
11573 for the maximum interrupt latency, whereas it does not care about jitter.
11574 On a loudspeaker driven via a digital to analog converter which is fed
11575 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11576 a much smaller jitter will be very audible.
11579 You can reenable interrupts within an interrupt routine and on some architecture
11580 s you can make use of two (or more) levels of
11582 interrupt priorities
11585 \begin_inset LatexCommand \index{interrupt priority}
11590 On some architectures which don't support interrupt priorities these can
11591 be implemented by manipulating the interrupt mask and reenabling interrupts
11592 within the interrupt routine.
11593 Check there is sufficient space on the stack
11594 \begin_inset LatexCommand \index{stack}
11598 and don't add complexity unless you have to.
11603 \begin_inset LatexCommand \index{semaphore}
11607 locking (mcs51/ds390)
11610 Some architectures (mcs51/ds390) have an atomic
11611 \begin_inset LatexCommand \index{atomic}
11624 These type of instructions are typically used in preemptive multitasking
11625 systems, where a routine f.e.
11626 claims the use of a data structure ('acquires a lock
11627 \begin_inset LatexCommand \index{lock}
11631 on it'), makes some modifications and then releases the lock when the data
11632 structure is consistent again.
11633 The instruction may also be used if interrupt and non-interrupt code have
11634 to compete for a resource.
11635 With the atomic bit test and clear instruction interrupts
11636 \begin_inset LatexCommand \index{interrupt}
11640 don't have to be disabled for the locking operation.
11644 SDCC generates this instruction if the source follows this pattern:
11650 \begin_inset LatexCommand \index{volatile}
11654 bit resource_is_free;
11658 if (resource_is_free)
11668 resource_is_free=0;
11681 resource_is_free=1;
11688 Note, mcs51 and ds390 support only an atomic
11689 \begin_inset LatexCommand \index{atomic}
11697 instruction (as opposed to atomic bit test and
11702 Functions using private register banks
11703 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11710 Some architectures have support for quickly changing register sets.
11711 SDCC supports this feature with the
11714 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11719 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11725 attribute (which tells the compiler to use a register bank
11726 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11730 other than the default bank zero).
11731 It should only be applied to
11734 \begin_inset LatexCommand \index{interrupt}
11740 functions (see footnote below).
11741 This will in most circumstances make the generated ISR code more efficient
11742 since it will not have to save registers on the stack.
11749 attribute will have no effect on the generated code for a
11753 function (but may occasionally be useful anyway
11759 possible exception: if a function is called ONLY from 'interrupt' functions
11760 using a particular bank, it can be declared with the same 'using' attribute
11761 as the calling 'interrupt' functions.
11762 For instance, if you have several ISRs using bank one, and all of them
11763 call memcpy(), it might make sense to create a specialized version of memcpy()
11764 'using 1', since this would prevent the ISR from having to save bank zero
11765 to the stack on entry and switch to bank zero before calling the function
11772 (pending: I don't think this has been done yet)
11779 function using a non-zero bank will assume that it can trash that register
11780 bank, and will not save it.
11781 Since high-priority interrupts
11782 \begin_inset LatexCommand \index{interrupts}
11787 \begin_inset LatexCommand \index{interrupt priority}
11791 can interrupt low-priority ones on the 8051 and friends, this means that
11792 if a high-priority ISR
11796 a particular bank occurs while processing a low-priority ISR
11800 the same bank, terrible and bad things can happen.
11801 To prevent this, no single register bank should be
11805 by both a high priority and a low priority ISR.
11806 This is probably most easily done by having all high priority ISRs use
11807 one bank and all low priority ISRs use another.
11808 If you have an ISR which can change priority at runtime, you're on your
11809 own: I suggest using the default bank zero and taking the small performance
11813 It is most efficient if your ISR calls no other functions.
11814 If your ISR must call other functions, it is most efficient if those functions
11815 use the same bank as the ISR (see note 1 below); the next best is if the
11816 called functions use bank zero.
11817 It is very inefficient to call a function using a different, non-zero bank
11823 \begin_inset LatexCommand \label{sub:Startup-Code}
11828 \begin_inset LatexCommand \index{Startup code}
11835 MCS51/DS390 Startup Code
11838 The compiler inserts a call to the C routine
11840 _sdcc_external_startup()
11841 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11850 at the start of the CODE area.
11851 This routine is in the runtime library
11852 \begin_inset LatexCommand \index{Runtime library}
11857 By default this routine returns 0, if this routine returns a non-zero value,
11858 the static & global variable initialization will be skipped and the function
11859 main will be invoked.
11860 Otherwise static & global variables will be initialized before the function
11864 _sdcc_external_startup()
11866 routine to your program to override the default if you need to setup hardware
11867 or perform some other critical operation prior to static & global variable
11869 \begin_inset LatexCommand \index{Variable initialization}
11874 On some mcs51 variants xdata
11875 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11879 memory has to be explicitly enabled before it can be accessed or if the
11880 watchdog needs to be disabled, this is the place to do it.
11881 The startup code clears all internal data memory, 256 bytes by default,
11882 but from 0 to n-1 if
11895 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11902 (recommended for Chipcon CC1010).
11905 See also the compiler option
11924 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11929 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11934 about MCS51-variants.
11940 The HC08 startup code follows the same scheme as the MCS51 startup code.
11946 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11947 from sdcc/device/lib/z80/crt0.s.
11948 If you need a different startup code you can use the compiler option
11969 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11973 and provide your own crt0.o.
11977 Inline Assembler Code
11978 \begin_inset LatexCommand \index{Assembler routines}
11985 A Step by Step Introduction
11986 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11993 Starting from a small snippet of c-code this example shows for the MCS51
11994 how to use inline assembly, access variables, a function parameter and
11995 an array in xdata memory.
11996 The example uses an MCS51 here but is easily adapted for other architectures.
11997 This is a buffer routine which should be optimized:
12004 \begin_inset LatexCommand \index{far (storage class)}
12009 \begin_inset LatexCommand \index{\_\_far (storage class)}
12014 \begin_inset LatexCommand \index{at}
12019 \begin_inset LatexCommand \index{\_\_at}
12024 \begin_inset LatexCommand \index{Aligned array}
12030 unsigned char head,tail;
12034 void to_buffer( unsigned char c )
12042 if( head != tail-1 )
12052 buf[ head++ ] = c;\SpecialChar ~
12056 /* access to a 256 byte aligned array */
12061 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12062 then a corresponding buffer.asm file is generated.
12063 We define a new function
12067 in file buffer.c in which we cut and paste the generated code, removing
12068 unwanted comments and some ':'.
12070 \begin_inset Quotes sld
12074 \begin_inset Quotes srd
12078 \begin_inset Quotes sld
12082 \begin_inset Quotes srd
12085 to the beginning and the end of the function body:
12091 /* With a cut and paste from the .asm file, we have something to start with.
12096 The function is not yet OK! (registers aren't saved) */
12098 void to_buffer_asm( unsigned char c )
12107 \begin_inset LatexCommand \index{\_asm}
12112 \begin_inset LatexCommand \index{\_\_asm}
12126 ;buffer.c if( head != tail-1 )
12174 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12175 \begin_inset LatexCommand \index{Aligned array}
12240 \begin_inset LatexCommand \index{\_endasm}
12245 \begin_inset LatexCommand \index{\_\_endasm}
12254 The new file buffer.c should compile with only one warning about the unreferenced
12255 function argument 'c'.
12256 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12257 (1) and finally have:
12263 unsigned char far at 0x7f00 buf[0x100];
12265 unsigned char head,tail;
12267 #define USE_ASSEMBLY (1)
12275 void to_buffer( unsigned char c )
12283 if( head != tail-1 )
12303 void to_buffer( unsigned char c )
12311 c; // to avoid warning: unreferenced function argument
12318 \begin_inset LatexCommand \index{\_asm}
12323 \begin_inset LatexCommand \index{\_\_asm}
12337 ; save used registers here.
12348 ; If we were still using r2,r3 we would have to push them here.
12351 ; if( head != tail-1 )
12394 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12418 ; buf[ head++ ] = c;
12429 a,dpl \SpecialChar ~
12436 ; dpl holds lower byte of function argument
12447 dpl,_head \SpecialChar ~
12450 ; buf is 0x100 byte aligned so head can be used directly
12492 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12504 ; restore used registers here
12511 \begin_inset LatexCommand \index{\_endasm}
12516 \begin_inset LatexCommand \index{\_\_endasm}
12527 The inline assembler code can contain any valid code understood by the assembler
12528 , this includes any assembler directives and comment lines
12534 The assembler does not like some characters like ':' or ''' in comments.
12535 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12536 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12541 \begin_inset LatexCommand \index{Assembler documentation}
12549 The compiler does not do any validation of the code within the
12552 \begin_inset LatexCommand \index{\_asm}
12557 \begin_inset LatexCommand \index{\_\_asm}
12565 \begin_inset LatexCommand \index{\_endasm}
12570 \begin_inset LatexCommand \index{\_\_endasm}
12579 Specifically it will not know which registers are used and thus register
12581 \begin_inset LatexCommand \index{push/pop}
12585 has to be done manually.
12589 It is recommended that each assembly instruction (including labels) be placed
12590 in a separate line (as the example shows).
12604 \begin_inset LatexCommand \index{-\/-peep-asm}
12610 command line option is used, the inline assembler code will be passed through
12611 the peephole optimizer
12612 \begin_inset LatexCommand \index{Peephole optimizer}
12617 There are only a few (if any) cases where this option makes sense, it might
12618 cause some unexpected changes in the inline assembler code.
12619 Please go through the peephole optimizer rules defined in file
12623 before using this option.
12627 \begin_inset LatexCommand \label{sub:Naked-Functions}
12632 \begin_inset LatexCommand \index{Naked functions}
12639 A special keyword may be associated with a function declaring it as
12642 \begin_inset LatexCommand \index{\_naked}
12647 \begin_inset LatexCommand \index{\_\_naked}
12658 function modifier attribute prevents the compiler from generating prologue
12659 \begin_inset LatexCommand \index{function prologue}
12664 \begin_inset LatexCommand \index{function epilogue}
12668 code for that function.
12669 This means that the user is entirely responsible for such things as saving
12670 any registers that may need to be preserved, selecting the proper register
12671 bank, generating the
12675 instruction at the end, etc.
12676 Practically, this means that the contents of the function must be written
12677 in inline assembler.
12678 This is particularly useful for interrupt functions, which can have a large
12679 (and often unnecessary) prologue/epilogue.
12680 For example, compare the code generated by these two functions:
12686 \begin_inset LatexCommand \index{volatile}
12690 data unsigned char counter;
12694 void simpleInterrupt(void) interrupt
12695 \begin_inset LatexCommand \index{interrupt}
12700 \begin_inset LatexCommand \index{\_\_interrupt}
12718 void nakedInterrupt(void) interrupt 2 _naked
12727 \begin_inset LatexCommand \index{\_asm}
12732 \begin_inset LatexCommand \index{\_\_asm}
12749 _counter ; does not change flags, no need to save psw
12761 ; MUST explicitly include ret or reti in _naked function.
12768 \begin_inset LatexCommand \index{\_endasm}
12773 \begin_inset LatexCommand \index{\_\_endasm}
12782 For an 8051 target, the generated simpleInterrupt looks like:
12923 whereas nakedInterrupt looks like:
12938 _counter ; does not change flags, no need to save psw
12956 ; MUST explicitly include ret or reti in _naked function
12959 The related directive #pragma exclude
12960 \begin_inset LatexCommand \index{\#pragma exclude}
12964 allows a more fine grained control over pushing & popping
12965 \begin_inset LatexCommand \index{push/pop}
12972 While there is nothing preventing you from writing C code inside a
12976 function, there are many ways to shoot yourself in the foot doing this,
12977 and it is recommended that you stick to inline assembler.
12980 Use of Labels within Inline Assembler
12983 SDCC allows the use of in-line assembler with a few restrictions regarding
12985 In older versions of the compiler all labels defined within inline assembler
12994 where nnnn is a number less than 100 (which implies a limit of utmost 100
12995 inline assembler labels
13009 \begin_inset LatexCommand \index{\_asm}
13014 \begin_inset LatexCommand \index{\_\_asm}
13044 \begin_inset LatexCommand \index{\_endasm}
13049 \begin_inset LatexCommand \index{\_\_endasm}
13056 Inline assembler code cannot reference any C-Labels, however it can reference
13058 \begin_inset LatexCommand \index{Labels}
13062 defined by the inline assembler, e.g.:
13087 ; some assembler code
13107 /* some more c code */
13109 clabel:\SpecialChar ~
13111 /* inline assembler cannot reference this label */
13123 $0003: ;label (can be referenced by inline assembler only)
13130 \begin_inset LatexCommand \index{\_endasm}
13135 \begin_inset LatexCommand \index{\_\_endasm}
13145 /* some more c code */
13150 In other words inline assembly code can access labels defined in inline
13151 assembly within the scope of the function.
13152 The same goes the other way, i.e.
13153 labels defines in inline assembly can not be accessed by C statements.
13156 Interfacing with Assembler Code
13157 \begin_inset LatexCommand \index{Assembler routines}
13164 Global Registers used for Parameter Passing
13165 \begin_inset LatexCommand \index{Parameter passing}
13172 The compiler always uses the global registers
13175 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13180 \begin_inset LatexCommand \index{DPTR}
13185 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13194 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13200 to pass the first parameter to a routine.
13201 The second parameter onwards is either allocated on the stack (for reentrant
13212 -stack-auto is used) or in data / xdata memory (depending on the memory
13217 Assembler Routine (non-reentrant)
13220 In the following example
13221 \begin_inset LatexCommand \index{reentrant}
13226 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13230 the function c_func calls an assembler routine asm_func, which takes two
13232 \begin_inset LatexCommand \index{function parameter}
13241 extern int asm_func(unsigned char, unsigned char);
13245 int c_func (unsigned char i, unsigned char j)
13253 return asm_func(i,j);
13267 return c_func(10,9);
13272 The corresponding assembler function is:
13277 .globl _asm_func_PARM_2
13378 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13395 Note here that the return values
13396 \begin_inset LatexCommand \index{return value}
13400 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13402 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13403 b' & 'acc' for four byte values.
13406 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13407 the parameter number starting from 1, and counting from the left.
13408 The first parameter is passed in
13409 \begin_inset Quotes eld
13413 \begin_inset Quotes erd
13416 for a one byte parameter,
13417 \begin_inset Quotes eld
13421 \begin_inset Quotes erd
13425 \begin_inset Quotes eld
13429 \begin_inset Quotes erd
13432 for three bytes and
13433 \begin_inset Quotes eld
13437 \begin_inset Quotes erd
13440 for a four bytes parameter.
13441 The variable name for the second parameter will be _<function_name>_PARM_2.
13445 Assemble the assembler routine with the following command:
13452 asx8051 -losg asmfunc.asm
13459 Then compile and link the assembler routine to the C source file with the
13467 sdcc cfunc.c asmfunc.rel
13470 Assembler Routine (reentrant)
13474 \begin_inset LatexCommand \index{reentrant}
13479 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13483 the second parameter
13484 \begin_inset LatexCommand \index{function parameter}
13488 onwards will be passed on the stack, the parameters are pushed from right
13490 after the call the leftmost parameter will be on the top of the stack.
13491 Here is an example:
13496 extern int asm_func(unsigned char, unsigned char);
13500 int c_func (unsigned char i, unsigned char j) reentrant
13508 return asm_func(i,j);
13522 return c_func(10,9);
13527 The corresponding assembler routine is:
13627 The compiling and linking procedure remains the same, however note the extra
13628 entry & exit linkage required for the assembler code, _bp is the stack
13629 frame pointer and is used to compute the offset into the stack for parameters
13630 and local variables.
13634 \begin_inset LatexCommand \index{int (16 bit)}
13639 \begin_inset LatexCommand \index{long (32 bit)}
13646 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13647 multiplication and modulus operations are implemented by support routines.
13648 These support routines are all developed in ANSI-C to facilitate porting
13649 to other MCUs, although some model specific assembler optimizations are
13651 The following files contain the described routines, all of them can be
13652 found in <installdir>/share/sdcc/lib.
13658 \begin_inset Tabular
13659 <lyxtabular version="3" rows="11" columns="2">
13661 <column alignment="center" valignment="top" leftline="true" width="0">
13662 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13663 <row topline="true" bottomline="true">
13664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13674 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13685 <row topline="true">
13686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13699 16 bit multiplication
13703 <row topline="true">
13704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13712 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13717 signed 16 bit division (calls _divuint)
13721 <row topline="true">
13722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13730 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13735 unsigned 16 bit division
13739 <row topline="true">
13740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13748 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13753 signed 16 bit modulus (calls _moduint)
13757 <row topline="true">
13758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13766 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13771 unsigned 16 bit modulus
13775 <row topline="true">
13776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13784 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13789 32 bit multiplication
13793 <row topline="true">
13794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13807 signed 32 division (calls _divulong)
13811 <row topline="true">
13812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13820 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13825 unsigned 32 division
13829 <row topline="true">
13830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13843 signed 32 bit modulus (calls _modulong)
13847 <row topline="true" bottomline="true">
13848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13861 unsigned 32 bit modulus
13874 Since they are compiled as
13879 \begin_inset LatexCommand \index{reentrant}
13884 \begin_inset LatexCommand \index{interrupt}
13888 service routines should not do any of the above operations.
13889 If this is unavoidable then the above routines will need to be compiled
13903 \begin_inset LatexCommand \index{-\/-stack-auto}
13909 option, after which the source program will have to be compiled with
13922 \begin_inset LatexCommand \index{-\/-int-long-reent}
13929 Notice that you don't have to call these routines directly.
13930 The compiler will use them automatically every time an integer operation
13934 Floating Point Support
13935 \begin_inset LatexCommand \index{Floating point support}
13942 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13943 floating point support routines are derived from gcc's floatlib.c and consist
13944 of the following routines:
13952 \begin_inset Tabular
13953 <lyxtabular version="3" rows="17" columns="2">
13955 <column alignment="center" valignment="top" leftline="true" width="0">
13956 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13957 <row topline="true" bottomline="true">
13958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13984 <row topline="true">
13985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14016 add floating point numbers
14020 <row topline="true">
14021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14038 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14052 subtract floating point numbers
14056 <row topline="true">
14057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14088 divide floating point numbers
14092 <row topline="true">
14093 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14110 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14124 multiply floating point numbers
14128 <row topline="true">
14129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14146 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14160 convert floating point to unsigned char
14164 <row topline="true">
14165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14182 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14196 convert floating point to signed char
14200 <row topline="true">
14201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14218 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14232 convert floating point to unsigned int
14236 <row topline="true">
14237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14254 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14268 convert floating point to signed int
14272 <row topline="true">
14273 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14313 convert floating point to unsigned long
14317 <row topline="true">
14318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14335 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14349 convert floating point to signed long
14353 <row topline="true">
14354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14371 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14385 convert unsigned char to floating point
14389 <row topline="true">
14390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14421 convert char to floating point number
14425 <row topline="true">
14426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14443 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14457 convert unsigned int to floating point
14461 <row topline="true">
14462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14493 convert int to floating point numbers
14497 <row topline="true">
14498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14529 convert unsigned long to floating point number
14533 <row topline="true" bottomline="true">
14534 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14551 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14565 convert long to floating point number
14578 These support routines are developed in ANSI-C so there is room for space
14579 and speed improvement
14585 The floating point routines for the mcs51 are implemented in assembler
14589 Note if all these routines are used simultaneously the data space might
14591 For serious floating point usage the large model might be needed.
14592 Also notice that you don't have to call this routines directly.
14593 The compiler will use them automatically every time a floating point operation
14598 \begin_inset LatexCommand \index{Libraries}
14607 <pending: this is messy and incomplete - a little more information is in
14608 sdcc/doc/libdoc.txt
14613 Compiler support routines (_gptrget, _mulint etc.)
14616 Stdclib functions (puts, printf, strcat etc.)
14617 \layout Subsubsection
14623 \begin_inset LatexCommand \index{<stdio.h>}
14627 As usual on embedded systems you have to provide your own
14630 \begin_inset LatexCommand \index{getchar()}
14639 \begin_inset LatexCommand \index{putchar()}
14646 SDCC does not know whether the system connects to a serial line with or
14647 without handshake, LCD, keyboard or other device.
14648 You'll find examples for serial routines f.e.
14649 in sdcc/device/lib.
14655 \begin_inset LatexCommand \index{printf()}
14665 does not support float (except on ds390).
14666 To enable this recompile it with the option
14679 \begin_inset LatexCommand \index{USE\_FLOATS}
14685 on the command line.
14699 \begin_inset LatexCommand \index{-\/-model-large}
14705 for the mcs51 port, since this uses a lot of memory.
14708 If you're short on memory you might want to use
14711 \begin_inset LatexCommand \index{printf\_small()}
14726 For the mcs51 there additionally are assembly versions
14729 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14738 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14747 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14753 which should fit the requirements of many embedded systems (printf_fast()
14754 can be customized by unsetting #defines to
14758 support long variables and field widths).
14761 Math functions (sin, pow, sqrt etc.)
14768 \begin_inset LatexCommand \index{Libraries}
14772 included in SDCC should have a license at least as liberal as the GNU Lesser
14773 General Public License
14774 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14785 license statements for the libraries are missing.
14786 sdcc/device/lib/ser_ir.c
14790 come with a GPL (as opposed to LGPL) License - this will not be liberal
14791 enough for many embedded programmers.
14794 If you have ported some library or want to share experience about some code
14796 falls into any of these categories Busses (I
14797 \begin_inset Formula $^{\textrm{2}}$
14800 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14801 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14802 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14803 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14808 would certainly like to hear about it.
14809 Programmers coding for embedded systems are not especially famous for being
14810 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14811 e these references are very valuable.
14812 Let's help to create a climate where information is shared.
14818 MCS51 Memory Models
14819 \begin_inset LatexCommand \index{Memory model}
14824 \begin_inset LatexCommand \index{MCS51 memory model}
14829 \layout Subsubsection
14831 Small, Medium and Large
14834 SDCC allows three memory models for MCS51 code,
14843 Modules compiled with different memory models should
14847 be combined together or the results would be unpredictable.
14848 The library routines supplied with the compiler are compiled as small,
14850 The compiled library modules are contained in separate directories as small,
14851 medium and large so that you can link to the appropriate set.
14854 When the medium or large model is used all variables declared without a
14855 storage class will be allocated into the external ram, this includes all
14856 parameters and local variables (for non-reentrant
14857 \begin_inset LatexCommand \index{reentrant}
14862 When the small model is used variables without storage class are allocated
14863 in the internal ram.
14866 Judicious usage of the processor specific storage classes
14867 \begin_inset LatexCommand \index{Storage class}
14871 and the 'reentrant' function type will yield much more efficient code,
14872 than using the large model.
14873 Several optimizations are disabled when the program is compiled using the
14874 large model, it is therefore recommended that the small model be used unless
14875 absolutely required.
14876 \layout Subsubsection
14879 \begin_inset LatexCommand \label{sub:External-Stack}
14884 \begin_inset LatexCommand \index{stack}
14889 \begin_inset LatexCommand \index{External stack (mcs51)}
14896 The external stack (-
14907 \begin_inset LatexCommand \index{-\/-xstack}
14911 ) is located in pdata
14912 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14916 memory (usually at the start of the external ram segment) and uses all
14917 unused space in pdata (max.
14929 -xstack option is used to compile the program, the parameters and local
14931 \begin_inset LatexCommand \index{local variables}
14935 of all reentrant functions are allocated in this area.
14936 This option is provided for programs with large stack space requirements.
14937 When used with the -
14948 \begin_inset LatexCommand \index{-\/-stack-auto}
14952 option, all parameters and local variables are allocated on the external
14953 stack (note: support libraries will need to be recompiled with the same
14955 There is a predefined target in the library makefile).
14958 The compiler outputs the higher order address byte of the external ram segment
14960 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14965 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14969 ), therefore when using the External Stack option, this port
14973 be used by the application program.
14977 \begin_inset LatexCommand \index{Memory model}
14982 \begin_inset LatexCommand \index{DS390 memory model}
14989 The only model supported is Flat 24
14990 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14995 This generates code for the 24 bit contiguous addressing mode of the Dallas
14997 In this mode, up to four meg of external RAM or code space can be directly
14999 See the data sheets at www.dalsemi.com for further information on this part.
15003 Note that the compiler does not generate any code to place the processor
15004 into 24 bitmode (although
15008 in the ds390 libraries will do that for you).
15014 \begin_inset LatexCommand \index{Tinibios (DS390)}
15018 , the boot loader or similar code must ensure that the processor is in 24
15019 bit contiguous addressing mode before calling the SDCC startup code.
15037 option, variables will by default be placed into the XDATA segment.
15042 Segments may be placed anywhere in the 4 meg address space using the usual
15054 Note that if any segments are located above 64K, the -r flag must be passed
15055 to the linker to generate the proper segment relocations, and the Intel
15056 HEX output format must be used.
15057 The -r flag can be passed to the linker by using the option
15061 on the SDCC command line.
15062 However, currently the linker can not handle code segments > 64k.
15066 \begin_inset LatexCommand \index{Pragmas}
15073 SDCC supports the following #pragma directives:
15077 \begin_inset LatexCommand \index{\#pragma save}
15081 - this will save all current options to the save/restore stack.
15082 See #pragma\SpecialChar ~
15087 \begin_inset LatexCommand \index{\#pragma restore}
15091 - will restore saved options from the last save.
15092 saves & restores can be nested.
15093 SDCC uses a save/restore stack: save pushes current options to the stack,
15094 restore pulls current options from the stack.
15095 See #pragma\SpecialChar ~
15102 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15107 \begin_inset LatexCommand \index{function prologue}
15111 function1[,function2[,function3...]] - The compiler by default uses a caller
15112 saves convention for register saving across function calls, however this
15113 can cause unnecessary register pushing & popping
15114 \begin_inset LatexCommand \index{push/pop}
15118 when calling small functions from larger functions.
15119 This option can be used to switch off the register saving convention for
15120 the function names specified.
15121 The compiler will not save registers when calling these functions, extra
15122 code need to be manually inserted at the entry & exit for these functions
15123 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15124 reduce code & improve run time performance of the generated code.
15125 In the future the compiler (with inter procedural analysis) may be able
15126 to determine the appropriate scheme to use for each function call.
15137 -callee-saves command line option is used, the function names specified
15138 in #pragma\SpecialChar ~
15140 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15144 is appended to the list of functions specified in the command line.
15148 \begin_inset LatexCommand \index{\#pragma exclude}
15152 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15153 of pairs of push/pop
15154 \begin_inset LatexCommand \index{push/pop}
15163 \begin_inset LatexCommand \index{interrupt}
15176 The directive should be placed immediately before the ISR function definition
15177 and it affects ALL ISR functions following it.
15178 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15179 exclude\SpecialChar ~
15181 \begin_inset LatexCommand \index{\#pragma exclude}
15186 See also the related keyword _naked
15187 \begin_inset LatexCommand \index{\_naked}
15192 \begin_inset LatexCommand \index{\_\_naked}
15200 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15204 - the compiler will not warn you anymore for obvious mistakes, you'r on
15208 disable_warning <nnnn>
15209 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15213 - the compiler will not warn you anymore about warning number <nnnn>.
15217 \begin_inset LatexCommand \index{\#pragma nogcse}
15221 - will stop global common subexpression elimination.
15225 \begin_inset LatexCommand \index{\#pragma noinduction}
15229 - will stop loop induction optimizations.
15233 \begin_inset LatexCommand \index{\#pragma noinvariant}
15237 - will not do loop invariant optimizations.
15238 For more details see Loop Invariants in section
15239 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15247 \begin_inset LatexCommand \index{\#pragma noiv}
15251 - Do not generate interrupt
15252 \begin_inset LatexCommand \index{interrupt}
15256 vector table entries for all ISR functions defined after the pragma.
15257 This is useful in cases where the interrupt vector table must be defined
15258 manually, or when there is a secondary, manually defined interrupt vector
15260 for the autovector feature of the Cypress EZ-USB FX2).
15261 More elegantly this can be achieved by obmitting the optional interrupt
15262 number after the interrupt keyword, see section
15263 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15272 \begin_inset LatexCommand \index{\#pragma nojtbound}
15276 - will not generate code for boundary value checking, when switch statements
15277 are turned into jump-tables (dangerous).
15278 For more details see section
15279 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15287 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15291 - Will not do loop reversal optimization
15295 \begin_inset LatexCommand \index{\#pragma nooverlay}
15299 - the compiler will not overlay the parameters and local variables of a
15304 \begin_inset LatexCommand \index{\#pragma stackauto}
15319 \begin_inset LatexCommand \index{-\/-stack-auto}
15324 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15328 Parameters and Local Variables.
15332 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15336 - The compiler will optimize code generation towards fast code, possibly
15337 at the expense of code size.
15341 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15345 - The compiler will optimize code generation towards compact code, possibly
15346 at the expense of code speed.
15350 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15354 - The compiler will attempt to generate code that is both compact and fast,
15355 as long as meeting one goal is not a detriment to the other (this is the
15361 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15365 - Generally follow the C89 standard, but allow SDCC features that conflict
15366 with the standard (default).
15370 \begin_inset LatexCommand \index{\#pragma std\_c89}
15374 - Follow the C89 standard and disable SDCC features that conflict with the
15379 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15383 - Generally follow the C99 standard, but allow SDCC features that conflict
15384 with the standard (incomplete support).
15388 \begin_inset LatexCommand \index{\#pragma std\_c99}
15392 - Follow the C99 standard and disable SDCC features that conflict with the
15393 standard (incomplete support).
15397 \begin_inset LatexCommand \index{\#pragma codeseg}
15401 - Use this name (max.
15402 8 characters) for the code segment.
15406 \begin_inset LatexCommand \index{\#pragma constseg}
15410 - Use this name (max.
15411 8 characters) for the const segment.
15414 SDCPP supports the following #pragma directives:
15418 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15422 (+ | -) - switch _asm _endasm block preprocessing on / off.
15426 The pragma's are intended to be used to turn-on or off certain optimizations
15427 which might cause the compiler to generate extra stack / data space to
15428 store compiler generated temporary variables.
15429 This usually happens in large functions.
15430 Pragma directives should be used as shown in the following example, they
15431 are used to control options & optimizations for a given function; pragmas
15432 should be placed before and/or after a function, placing pragma's inside
15433 a function body could have unpredictable results.
15439 \begin_inset LatexCommand \index{\#pragma save}
15450 /* save the current settings */
15453 \begin_inset LatexCommand \index{\#pragma nogcse}
15462 /* turnoff global subexpression elimination */
15464 #pragma noinduction
15465 \begin_inset LatexCommand \index{\#pragma noinduction}
15469 /* turn off induction optimizations */
15492 \begin_inset LatexCommand \index{\#pragma restore}
15496 /* turn the optimizations back on */
15499 The compiler will generate a warning message when extra space is allocated.
15500 It is strongly recommended that the save and restore pragma's be used when
15501 changing options for a function.
15504 Defines Created by the Compiler
15507 The compiler creates the following #defines
15508 \begin_inset LatexCommand \index{\#defines}
15513 \begin_inset LatexCommand \index{Defines created by the compiler}
15523 \begin_inset Tabular
15524 <lyxtabular version="3" rows="11" columns="2">
15526 <column alignment="center" valignment="top" leftline="true" width="0">
15527 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15528 <row topline="true" bottomline="true">
15529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15550 <row topline="true">
15551 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15557 \begin_inset LatexCommand \index{SDCC}
15564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15569 this Symbol is always defined
15573 <row topline="true">
15574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15580 \begin_inset LatexCommand \index{SDCC\_mcs51}
15585 \begin_inset LatexCommand \index{SDCC\_ds390}
15590 \begin_inset LatexCommand \index{SDCC\_z80}
15597 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15602 depending on the model used (e.g.: -mds390
15606 <row topline="true">
15607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15613 \begin_inset LatexCommand \index{\_\_mcs51}
15618 \begin_inset LatexCommand \index{\_\_ds390}
15623 \begin_inset LatexCommand \index{\_\_hc08}
15628 \begin_inset LatexCommand \index{\_\_z80}
15635 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15640 depending on the model used (e.g.
15645 <row topline="true">
15646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15652 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15682 <row topline="true">
15683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15689 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15696 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15719 <row topline="true">
15720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15726 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15733 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15756 <row topline="true">
15757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15763 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15793 <row topline="true">
15794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15800 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15830 <row topline="true">
15831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15837 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15844 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15857 <row topline="true" bottomline="true">
15858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15864 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15871 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15891 Notes on supported Processors
15895 \begin_inset LatexCommand \label{sub:MCS51-variants}
15900 \begin_inset LatexCommand \index{MCS51 variants}
15907 MCS51 processors are available from many vendors and come in many different
15909 While they might differ considerably in respect to Special Function Registers
15910 the core MCS51 is usually not modified or is kept compatible.
15914 pdata access by SFR
15917 With the upcome of devices with internal xdata and flash memory devices
15919 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15923 as dedicated I/O port is becoming more popular.
15924 Switching the high byte for pdata
15925 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15929 access which was formerly done by port P2 is then achieved by a Special
15931 \begin_inset LatexCommand \index{sfr}
15936 In well-established MCS51 tradition the address of this
15940 is where the chip designers decided to put it.
15941 Needless to say that they didn't agree on a common name either.
15942 So that the startup code can correctly initialize xdata variables, you
15943 should define an sfr with the name _XPAGE
15946 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15952 at the appropriate location if the default, port P2, is not used for this.
15958 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15963 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15968 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15971 For more exotic implementations further customizations may be needed.
15973 \begin_inset LatexCommand \ref{sub:Startup-Code}
15977 for other possibilities.
15980 Other Features available by SFR
15983 Some MCS51 variants offer features like Double DPTR
15984 \begin_inset LatexCommand \index{DPTR}
15988 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15989 These are currently not used for the MCS51 port.
15990 If you absolutely need them you can fall back to inline assembly or submit
15997 The DS80C400 microcontroller has a rich set of peripherals.
15998 In its built-in ROM library it includes functions to access some of the
15999 features, among them is a TCP stack with IP4 and IP6 support.
16000 Library headers (currently in beta status) and other files are provided
16004 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16012 The Z80 and gbz80 port
16015 SDCC can target both the Zilog
16016 \begin_inset LatexCommand \index{Z80}
16020 and the Nintendo Gameboy's Z80-like gbz80
16021 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16026 The Z80 port is passed through the same
16029 \begin_inset LatexCommand \index{Regression test}
16035 as the MCS51 and DS390 ports, so floating point support, support for long
16036 variables and bitfield support is fine.
16037 See mailing lists and forums about interrupt routines.
16040 As always, the code is the authoritative reference - see z80/ralloc.c and
16043 \begin_inset LatexCommand \index{stack}
16047 frame is similar to that generated by the IAR Z80 compiler.
16048 IX is used as the base pointer, HL and IY are used as a temporary registers,
16049 and BC and DE are available for holding variables.
16051 \begin_inset LatexCommand \index{return value}
16055 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16057 The gbz80 port use the same set of registers for the return values, but
16058 in a different order of significance: E (one byte), DE (two bytes), or
16065 The port to the Motorola HC08
16066 \begin_inset LatexCommand \index{HC08}
16070 family has been added in October 2003, and is still undergoing some basic
16072 The code generator is complete, but the register allocation is still quite
16074 Some of the SDCC's standard C library functions have embedded non-HC08
16075 inline assembly and so are not yet usable.
16086 \begin_inset LatexCommand \index{PIC14}
16090 port still requires a major effort from the development community.
16091 However it can work for very simple code.
16094 C code and 14bit PIC code page
16095 \begin_inset LatexCommand \index{code page (pic14)}
16100 \begin_inset LatexCommand \index{RAM bank (pic14)}
16107 The linker organizes allocation for the code page and RAM banks.
16108 It does not have intimate knowledge of the code flow.
16109 It will put all the code section of a single asm file into a single code
16111 In order to make use of multiple code pages, separate asm files must be
16113 The compiler treats all functions of a single C file as being in the same
16114 code page unless it is non static.
16115 The compiler treats all local variables of a single C file as being in
16116 the same RAM bank unless it is an extern.
16120 To get the best follow these guide lines:
16123 make local functions static, as non static functions require code page selection
16127 Make local variables static as extern variables require RAM bank selection
16131 For devices that have multiple code pages it is more efficient to use the
16132 same number of files as pages, i.e.
16133 for the 16F877 use 4 separate files and i.e.
16134 for the 16F874 use 2 separate files.
16135 This way the linker can put the code for each file into different code
16136 pages and the compiler can allocate reusable variables more efficiently
16137 and there's less page selection overhead.
16138 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16139 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16142 Creating a device include file
16145 For generating a device include file use the support perl script inc2h.pl
16146 kept in directory support/script.
16152 For the interrupt function, use the keyword 'interrupt'
16153 \begin_inset LatexCommand \index{interrupt}
16157 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16158 there to avoid a syntax error - it ought to be fixed).
16164 void Intr(void) interrupt 0
16170 T0IF = 0; /* Clear timer interrupt */
16175 Linking and assembling
16178 For assembling you can use either GPUTILS'
16179 \begin_inset LatexCommand \index{gputils (pic tools)}
16183 gpasm.exe or MPLAB's mpasmwin.exe.
16184 GPUTILS is available from
16185 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16190 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16191 If you use MPLAB and an interrupt function then the linker script file
16192 vectors section will need to be enlarged to link with mplink.
16215 sdcc -S -V -mpic14 -p16F877 $<
16229 $(PRJ).hex: $(OBJS)
16239 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16261 sdcc -S -V -mpic14 -p16F877 $<
16271 mpasmwin /q /o $*.asm
16275 $(PRJ).hex: $(OBJS)
16285 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16288 Please note that indentations within a
16292 have to be done with a tabulator character.
16295 Command-line options
16298 Besides the switches common to all SDCC backends, the PIC14 port accepts
16299 the following options (for an updated list see sdcc -
16311 \labelwidthstring 00.00.0000
16323 -debug-extra emit debug info in assembly output
16325 \labelwidthstring 00.00.0000
16337 -no-pcode-opt disable (slightly faulty) optimization on pCode
16341 \layout Subsubsection
16343 error: missing definition for symbol
16344 \begin_inset Quotes sld
16348 \begin_inset Quotes srd
16354 The PIC14 port uses library routines to provide more complex operations
16355 like multiplication, division/modulus and (generic) pointer dereferencing.
16356 In order to add these routines to your project, you must link with PIC14's
16362 For single source file projects this is done automatically, more complex
16367 to the linker's arguments.
16368 Make sure you also add an include path for the library (using the -I switch
16370 \layout Subsubsection
16372 Processor mismatch in file
16373 \begin_inset Quotes sld
16377 \begin_inset Quotes srd
16383 This warning can usually be ignored due to the very good compatibility amongst
16384 14 bit PIC devices.
16387 You might also consider recompiling the library for your specific device
16388 by changing the ARCH=p16f877 (default target) entry in
16390 device/lib/pic/Makefile.in
16394 device/lib/pic/Makefile
16396 to reflect your device.
16397 This might even improve performance for smaller devices as unneccesary
16398 BANKSELs migth be removed.
16402 \layout Subsubsection
16407 Currently, data can only be initialized if it resides in the source file
16413 Data in other source files will silently
16421 \begin_inset LatexCommand \index{PIC16}
16429 \begin_inset LatexCommand \index{PIC16}
16433 port is the portion of SDCC that is responsible to produce code for the
16435 \begin_inset LatexCommand \index{Microchip}
16439 (TM) microcontrollers with 16 bit core.
16440 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16441 Currently supported devices are:
16445 \begin_inset Tabular
16446 <lyxtabular version="3" rows="4" columns="6">
16448 <column alignment="center" valignment="top" leftline="true" width="0">
16449 <column alignment="center" valignment="top" leftline="true" width="0">
16450 <column alignment="center" valignment="top" leftline="true" width="0">
16451 <column alignment="center" valignment="top" leftline="true" width="0">
16452 <column alignment="center" valignment="top" leftline="true" width="0">
16453 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16454 <row topline="true">
16455 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16504 <row topline="true">
16505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16545 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16554 <row topline="true">
16555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16604 <row topline="true" bottomline="true">
16605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16636 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16661 PIC16 port supports the standard command line arguments as supposed, with
16662 the exception of certain cases that will be mentioned in the following
16665 \labelwidthstring 00.00.0000
16677 -callee-saves See -
16689 \labelwidthstring 00.00.0000
16701 -all-callee-saves All function arguments are passed on stack by default.
16704 There is no need to specify this in the command line.
16706 \labelwidthstring 00.00.0000
16718 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16719 no local variables.
16722 Port Specific Options
16723 \begin_inset LatexCommand \index{Options PIC16}
16730 The port specific options appear after the global options in the sdcc --help
16732 \layout Subsubsection
16737 General options enable certain port features and optimizations.
16739 \labelwidthstring 00.00.0000
16751 -stack-model=[model] Used in conjuction with the command above.
16752 Defines the stack model to be used, valid stack models are :
16755 \labelwidthstring 00.00.0000
16761 Selects small stack model.
16762 8 bit stack and frame pointers.
16763 Supports 256 bytes stack size.
16765 \labelwidthstring 00.00.0000
16771 Selects large stack model.
16772 16 bit stack and frame pointers.
16773 Supports 65536 bytes stack size.
16776 \labelwidthstring 00.00.0000
16788 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16789 unitialized data variables with [kword].
16790 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16792 \labelwidthstring 00.00.0000
16804 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16805 Useful for bootloaders.
16807 \labelwidthstring 00.00.0000
16819 -asm= sets the full path and name of an external assembler to call.
16821 \labelwidthstring 00.00.0000
16833 -link= sets the full path and name of an external linker to call.
16835 \labelwidthstring 00.00.0000
16847 -mplab-comp MPLAB compatibility option.
16848 Currently only suppresses special gpasm directives.
16849 \layout Subsubsection
16851 Optimization Options
16853 \labelwidthstring 00.00.0000
16865 -optimize-goto Try to use (conditional) BRA instead of GOTO
16867 \labelwidthstring 00.00.0000
16879 -optimize-cmp Try to optimize some compares.
16881 \labelwidthstring 00.00.0000
16893 -optimize-df Analyze the dataflow of the generated code and improve it.
16895 \labelwidthstring 00.00.0000
16907 -obanksel=nn Set optimization level for inserting BANKSELs.
16912 \labelwidthstring 00.00.0000
16916 \labelwidthstring 00.00.0000
16918 1 checks previous used register and if it is the same then does not emit
16919 BANKSEL, accounts only for labels.
16921 \labelwidthstring 00.00.0000
16923 2 tries to check the location of (even different) symbols and removes BANKSELs
16924 if they are in the same bank.
16929 Important: There might be problems if the linker script has data sections
16930 across bank borders!
16932 \layout Subsubsection
16936 \labelwidthstring 00.00.0000
16948 -nodefaultlibs do not link default libraries when linking
16950 \labelwidthstring 00.00.0000
16962 -no-crt Don't link the default run-time modules
16964 \labelwidthstring 00.00.0000
16976 -use-crt= Use a custom run-time module instead of the defaults.
16977 \layout Subsubsection
16982 Debugging options enable extra debugging information in the output files.
16984 \labelwidthstring 00.00.0000
16996 -debug-xtra Similar to -
17007 \begin_inset LatexCommand \index{-\/-debug}
17011 , but dumps more information.
17013 \labelwidthstring 00.00.0000
17025 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17027 <source> is the name of the file compiled.
17029 \labelwidthstring 00.00.0000
17041 -pcode-verbose Enable pcode debugging information in translation.
17043 \labelwidthstring 00.00.0000
17055 -denable-peeps Force the usage of peepholes.
17058 \labelwidthstring 00.00.0000
17070 -gstack Trace push/pops for stack pointer overflow
17072 \labelwidthstring 00.00.0000
17084 -call-tree dump call tree in .calltree file
17087 Enviromental Variables
17090 There is a number of enviromental variables that can be used when running
17091 SDCC to enable certain optimizations or force a specific program behaviour.
17092 these variables are primarily for debugging purposes so they can be enabled/dis
17096 Currently there is only two such variables available:
17098 \labelwidthstring 00.00.0000
17100 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17101 bitfields is optimized by directly loading FSR0 with the address of the
17102 bitfield structure.
17103 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17105 This step saves data ram and code space for functions that perform heavy
17108 80 bytes of code space are saved when compiling malloc.c with this option).
17111 \labelwidthstring 00.00.0000
17113 NO_REG_OPT do not perform pCode registers optimization.
17114 This should be used for debugging purposes.
17115 In some where bugs in the pcode optimizer are found, users can benefit
17116 from temporarily disabling the optimizer until the bug is fixed.
17119 Preprocessor Macros
17122 PIC16 port defines the following preprocessor macros while translating a
17127 \begin_inset Tabular
17128 <lyxtabular version="3" rows="6" columns="2">
17130 <column alignment="center" valignment="top" leftline="true" width="0">
17131 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17132 <row topline="true" bottomline="true">
17133 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17141 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17150 <row topline="true">
17151 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17159 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17164 Port identification
17168 <row topline="true">
17169 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17187 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17192 Port identification (same as above)
17196 <row topline="true">
17197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17210 MCU Identification.
17215 is the microcontrol identification number, i.e.
17220 <row topline="true">
17221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17239 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17244 MCU Identification (same as above)
17248 <row topline="true" bottomline="true">
17249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17262 nnn = SMALL or LARGE respectively according to the stack model used
17273 In addition the following macros are defined when calling assembler:
17277 \begin_inset Tabular
17278 <lyxtabular version="3" rows="4" columns="2">
17280 <column alignment="center" valignment="top" leftline="true" width="0">
17281 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17282 <row topline="true" bottomline="true">
17283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17300 <row topline="true">
17301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17314 MCU Identification.
17319 is the microcontrol identification number, i.e.
17324 <row topline="true">
17325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17338 nnn = SMALL or LARGE respectively according to the memory model used for
17343 <row topline="true" bottomline="true">
17344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17357 nnn = SMALL or LARGE respectively according to the stack model used
17372 \begin_inset LatexCommand \index{PIC16}
17376 port uses the following directories for searching header files and libraries.
17380 \begin_inset Tabular
17381 <lyxtabular version="3" rows="3" columns="4">
17383 <column alignment="center" valignment="top" leftline="true" width="0">
17384 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17385 <column alignment="center" valignment="top" width="0">
17386 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17387 <row topline="true" bottomline="true">
17388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17396 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17404 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17412 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17421 <row topline="true">
17422 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17427 PREFIX/sdcc/include/pic16
17430 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17435 PIC16 specific headers
17438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17455 <row topline="true" bottomline="true">
17456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17461 PREFIX/sdcc/lib/pic16
17464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17469 PIC16 specific libraries
17472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17480 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17497 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17504 PIC16 port currently supports the following pragmas:
17506 \labelwidthstring 00.00.0000
17508 stack pragma stack forces the code generator to initialize the stack & frame
17509 pointers at a specific address.
17510 This is an adhoc solution for cases where no STACK directive is available
17511 in the linker script or gplink is not instructed to create a stack section.
17513 The stack pragma should be used only once in a project.
17514 Multiple pragmas may result in indeterminate behaviour of the program.
17520 The old format (ie.
17521 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17522 cross page boundaries (or even exceed the available data RAM) and crash
17524 Make sure that stack does not cross page boundaries when using the SMALL
17530 The format is as follows:
17533 #pragma stack bottom_address [stack_size]
17540 is the lower bound of the stack section.
17541 The stack pointer initially will point at address (bottom_address+stack_size-1).
17549 /* initializes stack of 100 bytes at RAM address 0x200 */
17552 #pragma stack 0x200 100
17555 If the stack_size field is omitted then a stack is created with the default
17557 This size might be enough for most programs, but its not enough for operations
17558 with deep function nesting or excessive stack usage.
17560 \labelwidthstring 00.00.0000
17564 This pragma is deprecated.
17565 Its use will cause a warning message to be issued.
17571 \labelwidthstring 00.00.0000
17573 code place a function symbol at static FLASH address
17581 /* place function test_func at 0x4000 */
17584 #pragma code test_func 0x4000
17588 \labelwidthstring 00.00.0000
17590 library instructs the linker to use a library module.
17595 #pragma library module_name
17602 can be any library or object file (including its path).
17603 Note that there are four reserved keywords which have special meaning.
17608 \begin_inset Tabular
17609 <lyxtabular version="3" rows="6" columns="3">
17611 <column alignment="center" valignment="top" leftline="true" width="0">
17612 <column alignment="block" valignment="top" leftline="true" width="20page%">
17613 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17614 <row topline="true" bottomline="true">
17615 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17640 <row topline="true">
17641 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17656 ignore all library pragmas
17659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17670 <row topline="true">
17671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17689 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17702 <row topline="true">
17703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17718 link the Math libarary
17721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17734 <row topline="true">
17735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17750 link the I/O library
17753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17766 <row topline="true" bottomline="true">
17767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17782 link the debug library
17785 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17804 * is the device number, i.e.
17805 452 for PIC18F452 MCU.
17808 This feature allows for linking with specific libraries withoug having to
17809 explicit name them in the command line.
17814 keyword will reject all modules specified by the library pragma.
17816 \labelwidthstring 00.00.0000
17818 udata pragma udata instructs the compiler to emit code so that linker will
17819 place a variable at a specific memory bank
17827 /* places variable foo at bank2 */
17830 #pragma udata bank2 foo
17836 In order for this pragma to work extra SECTION directives should be added
17837 in the .lkr script.
17838 In the following example a sample .lkr file is shown:
17843 // Sample linker script for the PIC18F452 processor
17849 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17852 CODEPAGE NAME=page START=0x2A END=0x7FFF
17855 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17858 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17861 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17864 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17867 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17872 DATABANK NAME=gpr0 START=0x80 END=0xFF
17875 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17878 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17881 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17884 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17887 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17890 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17895 SECTION NAME=CONFIG ROM=config
17900 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17903 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17906 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17909 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17912 SECTION NAME=bank4 RAM=gpr4
17915 SECTION NAME=bank5 RAM=gpr5
17918 The linker will recognise the section name set in the pragma statement and
17919 will position the variable at the memory bank set with the RAM field at
17920 the SECTION line in the linker script file.
17924 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17931 There is one main header file that can be included to the source files using
17938 This header file contains the definitions for the processor special registers,
17939 so it is necessary if the source accesses them.
17940 It can be included by adding the following line in the beginning of the
17944 #include <pic18fregs.h>
17947 The specific microcontroller is selected within the pic18fregs.h automatically,
17948 so the same source can be used with a variety of devices.
17954 The libraries that PIC16
17955 \begin_inset LatexCommand \index{PIC16}
17959 port depends on are the microcontroller device libraries which contain
17960 the symbol definitions for the microcontroller special function registers.
17961 These libraries have the format pic18fxxxx.lib, where
17965 is the microcontroller identification number.
17966 The specific library is selected automatically by the compiler at link
17967 stage according to the selected device.
17970 Libraries are created with gplib which is part of the gputils package
17971 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17976 \layout Subsubsection*
17978 Building the libraries
17981 Before using SDCC/pic16 there are some libraries that need to be compiled.
17982 This process is not done automatically by SDCC since not all users use
17983 SDCC for pic16 projects.
17984 So each user should compile the libraries separately.
17987 The steps to compile the pic16 libraries under Linux are:
17990 cd device/lib/pic16
18005 su -c 'make install' # install the libraries, you need the root password
18008 If you need to install the headers too, do:
18014 su -c 'make install' # install the headers, you need the root password
18017 There exist a special target to build the I/O libraries.
18018 This target is not automatically build because it will build the I/O library
18024 This way building will take quite a lot of time.
18025 Users are advised to edit the
18027 device/lib/pic16/pics.build
18029 file and then execute:
18038 The following memory models are supported by the PIC16 port:
18047 Memory model affects the default size of pointers within the source.
18048 The sizes are shown in the next table:
18052 \begin_inset Tabular
18053 <lyxtabular version="3" rows="3" columns="3">
18055 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18056 <column alignment="center" valignment="top" leftline="true" width="0">
18057 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18058 <row topline="true" bottomline="true">
18059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18064 Pointer sizes according to memory model
18067 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18075 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18084 <row topline="true" bottomline="true">
18085 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18093 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18101 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18110 <row topline="true" bottomline="true">
18111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18119 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18127 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18143 It is advisable that all sources within a project are compiled with the
18145 If one wants to override the default memory model, this can be done by
18146 declaring a pointer as
18155 Far selects large memory model's pointers, while near selects small memory
18159 The standard device libraries (see
18160 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18164 ) contain no reference to pointers, so they can be used with both memory
18171 The stack implementation for the PIC16 port uses two indirect registers,
18174 \labelwidthstring 00.00.0000
18176 FSR1 is assigned as stack pointer
18178 \labelwidthstring 00.00.0000
18180 FSR2 is assigned as frame pointer
18183 The following stack models are supported by the PIC16 port
18204 model means that only the FSRxL byte is used to access stack and frame,
18211 uses both FSRxL and FSRxH registers.
18212 The following table shows the stack/frame pointers sizes according to stack
18213 model and the maximum space they can address:
18217 \begin_inset Tabular
18218 <lyxtabular version="3" rows="3" columns="3">
18220 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18221 <column alignment="center" valignment="top" leftline="true" width="0">
18222 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18223 <row topline="true" bottomline="true">
18224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18229 Stack & Frame pointer sizes according to stack model
18232 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18249 <row topline="true">
18250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18275 <row topline="true" bottomline="true">
18276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18312 stack model is currently not working properly throughout the code generator.
18313 So its use is not advised.
18314 Also there are some other points that need special care:
18319 Do not create stack sections with size more than one physical bank (that
18323 Stack sections should no cross physical bank limits (i.e.
18324 #pragma stack 0x50 0x100)
18327 These limitations are caused by the fact that only FSRxL is modified when
18328 using SMALL stack model, so no more than 256 bytes of stack can be used.
18329 This problem will disappear after LARGE model is fully implemented.
18335 In addition to the standard SDCC function keywords, PIC16 port makes available
18338 \labelwidthstring 00.00.0000
18340 wparam Use the WREG to pass one byte of the first function argument.
18341 This improves speed but you may not use this for functions with arguments
18342 that are called via function pointers, otherwise the first byte of the
18343 first parameter will get lost.
18347 void func_wparam(int a) wparam
18353 /* WREG hold the lower part of a */
18356 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18366 This keyword replaces the deprecated wparam pragma.
18368 \labelwidthstring 00.00.0000
18370 shadowregs When entering/exiting an ISR, it is possible to take advantage
18371 of the PIC18F hardware shadow registers which hold the values of WREG,
18372 STATUS and BSR registers.
18373 This can be done by adding the keyword
18381 keyword in the function's header.
18384 void isr_shadow(void) shadowregs interrupt 1
18400 instructs the code generator not to store/restore WREG, STATUS, BSR when
18401 entering/exiting the ISR.
18404 Function return values
18407 Return values from functions are placed to the appropriate registers following
18408 a modified Microchip policy optimized for SDCC.
18409 The following table shows these registers:
18413 \begin_inset Tabular
18414 <lyxtabular version="3" rows="6" columns="2">
18416 <column alignment="center" valignment="top" leftline="true" width="0">
18417 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18418 <row topline="true" bottomline="true">
18419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18427 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18432 destination register
18436 <row topline="true">
18437 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18445 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18454 <row topline="true">
18455 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18472 <row topline="true">
18473 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18490 <row topline="true">
18491 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18504 FSR0L:PRODH:PRODL:WREG
18508 <row topline="true" bottomline="true">
18509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18522 on stack, FSR0 points to the beginning
18536 An interrupt servive routine (ISR) is declared using the
18543 void isr(void) interrupt
18561 is the interrupt number, which for PIC18F devices can be:
18565 \begin_inset Tabular
18566 <lyxtabular version="3" rows="4" columns="3">
18568 <column alignment="center" valignment="top" leftline="true" width="0">
18569 <column alignment="center" valignment="top" leftline="true" width="0">
18570 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18571 <row topline="true" bottomline="true">
18572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18595 Interrupt Vector Address
18599 <row topline="true">
18600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18625 <row topline="true">
18626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18657 HIGH priority interrupts
18660 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18669 <row topline="true" bottomline="true">
18670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18683 LOW priority interrupts
18686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18702 When generating assembly code for ISR the code generator places a
18708 Interrupt Vector Address
18710 which points at the genetated ISR.
18711 This single GOTO instruction is part of an automatically generated
18713 interrupt entry point
18716 The actuall ISR code is placed as normally would in the code space.
18717 Upon interrupt request, the GOTO instruction is executed which jumps to
18719 When declaring interrupt functions as _naked this GOTO instruction is
18724 The whole interrupt functions is therefore placed at the Interrupt Vector
18725 Address of the specific interrupt.
18726 This is not a problem for the LOW priority interrupts, but it is a problem
18727 for the RESET and the HIGH priority interrupts because code may be written
18728 at the next interrupt´s vector address and cause undeterminate program
18729 behaviour if that interrupt is raised.
18735 This is not a problem when
18738 this is a HIGH interrupt ISR and LOW interrupts are
18745 when the ISR is small enough not to reach the next interrupt´s vector address.
18755 is possible to be omitted.
18756 This way a function is generated similar to an ISR, but it is not assigned
18760 When entering an interrupt, currently the PIC16
18761 \begin_inset LatexCommand \index{PIC16}
18765 port automatically saves the following registers:
18777 PROD (PRODL and PRODH)
18780 FSR0 (FSR0L and FSR0H)
18783 These registers are restored upon return from the interrupt routine.
18789 NOTE that when the _naked attribute is specified for an interrupt routine,
18790 then NO registers are stored or restored.
18799 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18800 There are 3 types of generic pointers currently implemented data, code
18801 and eeprom pointers.
18802 They are differentiated by the value of the 7th and 6th bits of the upper
18807 \begin_inset Tabular
18808 <lyxtabular version="3" rows="5" columns="5">
18810 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18811 <column alignment="center" valignment="top" width="0">
18812 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18813 <column alignment="center" valignment="top" width="0">
18814 <column alignment="left" valignment="top" rightline="true" width="0">
18815 <row topline="true" bottomline="true">
18816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18845 rest of the pointer
18848 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18857 <row topline="true" bottomline="true">
18858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18874 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18891 uuuuuu uuuuxxxx xxxxxxxx
18894 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18899 a 12-bit data pointer in data RAM memory
18903 <row bottomline="true">
18904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18912 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18920 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18928 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18937 uxxxxx xxxxxxxx xxxxxxxx
18940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18945 a 21-bit code pointer in FLASH memory
18949 <row bottomline="true">
18950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18974 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18983 uuuuuu uuuuuuxx xxxxxxxx
18986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18991 a 10-bit eeprom pointer in EEPROM memory
18995 <row bottomline="true">
18996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19029 xxxxxx xxxxxxxx xxxxxxxx
19032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19037 unimplemented pointer type
19048 Generic pointer are read and written with a set of library functions which
19049 read/write 1, 2, 3, 4 bytes.
19053 \layout Subsubsection
19055 Standard I/O Streams
19062 the type FILE is defined as:
19065 typedef char * FILE;
19068 This type is the stream type implemented I/O in the PIC18F devices.
19069 Also the standard input and output streams are declared in stdio.h:
19072 extern FILE * stdin;
19075 extern FILE * stdout;
19078 The FILE type is actually a generic pointer which defines one more type
19079 of generic pointers, the
19084 This new type has the format:
19088 \begin_inset Tabular
19089 <lyxtabular version="3" rows="2" columns="7">
19091 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19092 <column alignment="center" valignment="top" width="0">
19093 <column alignment="center" valignment="top" leftline="true" width="0">
19094 <column alignment="center" valignment="top" leftline="true" width="0">
19095 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19096 <column alignment="center" valignment="top" width="0">
19097 <column alignment="left" valignment="top" rightline="true" width="0">
19098 <row topline="true" bottomline="true">
19099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19107 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19115 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19123 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19139 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19144 rest of the pointer
19147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19156 <row topline="true" bottomline="true">
19157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19181 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19197 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19214 upper byte high nubble is 0x2n, the rest are zeroes
19225 Currently implemented there are 3 types of streams defined:
19229 \begin_inset Tabular
19230 <lyxtabular version="3" rows="4" columns="4">
19232 <column alignment="center" valignment="top" leftline="true" width="0">
19233 <column alignment="center" valignment="top" leftline="true" width="0">
19234 <column alignment="center" valignment="top" leftline="true" width="0">
19235 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19236 <row topline="true" bottomline="true">
19237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19261 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19270 <row topline="true">
19271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19302 Writes/Reads characters via the USART peripheral
19306 <row topline="true">
19307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19338 Writes/Reads characters via the MSSP peripheral
19342 <row topline="true" bottomline="true">
19343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19361 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19369 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19374 Writes/Reads characters via used defined functions
19385 The stream identifiers are declared as macros in the stdio.h header.
19388 In the libc library there exist the functions that are used to write to
19389 each of the above streams.
19392 \labelwidthstring 00.00.0000
19404 _stream_usart_putchar writes a character at the USART stream
19406 \labelwidthstring 00.00.0000
19418 _stream_mssp_putchar writes a character at the MSSP stream
19420 \labelwidthstring 00.00.0000
19422 putchar dummy function.
19423 This writes a character to a user specified manner.
19426 In order to increase performance
19430 is declared in stdio.h as having its parameter in WREG (it has the wparam
19432 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19433 in a user-friendly way.
19438 is the name of the variable that holds the character to print.
19439 An example follows:
19442 #include <pic18fregs.h>
19454 PORTA = c; /* dump character c to PORTA */
19467 stdout = STREAM_USER; /* this is not necessery, since stdout points
19470 * by default to STREAM_USER */
19473 printf (¨This is a printf test
19481 \layout Subsubsection
19486 PIC16 contains an implementation of the printf-family of functions.
19487 There exist the following functions:
19490 extern unsigned int sprintf(char *buf, char *fmt, ...);
19493 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19498 extern unsigned int printf(char *fmt, ...);
19501 extern unsigned int vprintf(char *fmt, va_lista ap);
19506 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19509 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19512 For sprintf and vsprintf
19516 should normally be a data pointer where the resulting string will be placed.
19517 No range checking is done so the user should allocate the necessery buffer.
19518 For fprintf and vfprintf
19522 should be a stream pointer (i.e.
19523 stdout, STREAM_MSSP, etc...).
19524 \layout Subsubsection
19529 The PIC18F family of microcontrollers supports a number of interrupt sources.
19530 A list of these interrupts is shown in the following table:
19534 \begin_inset Tabular
19535 <lyxtabular version="3" rows="11" columns="4">
19537 <column alignment="left" valignment="top" leftline="true" width="0">
19538 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19539 <column alignment="left" valignment="top" leftline="true" width="0">
19540 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19541 <row topline="true" bottomline="true">
19542 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19550 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19575 <row topline="true">
19576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19584 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19589 PORTB change interrupt
19592 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19600 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19605 EEPROM/FLASH write complete interrupt
19609 <row topline="true">
19610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19618 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19623 INT0 external interrupt
19626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19634 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19639 Bus collision interrupt
19643 <row topline="true">
19644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19652 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19657 INT1 external interrupt
19660 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19673 Low voltage detect interrupt
19677 <row topline="true">
19678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19691 INT2 external interrupt
19694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19707 Parallel slave port interrupt
19711 <row topline="true">
19712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19725 CCP1 module interrupt
19728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19736 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19741 AD convertion complete interrupt
19745 <row topline="true">
19746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19759 CCP2 module interrupt
19762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19775 USART receive interrupt
19779 <row topline="true">
19780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19788 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19793 TMR0 overflow interrupt
19796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19809 USART transmit interrupt
19813 <row topline="true">
19814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19827 TMR1 overflow interrupt
19830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19843 SSP receive/transmit interrupt
19847 <row topline="true">
19848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19861 TMR2 matches PR2 interrupt
19864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19871 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19879 <row topline="true" bottomline="true">
19880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19893 TMR3 overflow interrupt
19896 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19918 The prototypes for these names are defined in the header file
19925 In order to simplify signal handling, a number of macros is provided:
19927 \labelwidthstring 00.00.0000
19929 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19930 high priority interrupts.
19935 is the function name to use.
19937 \labelwidthstring 00.00.0000
19939 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19940 low priority interrupt.
19945 is the function name to use.
19947 \labelwidthstring 00.00.0000
19949 DEF_HANDLER(sig,handler) define a handler for signal
19953 \labelwidthstring 00.00.0000
19955 END_DEF end the declaration of the dispatch table.
19958 Additionally there are two more macros to simplify the declaration of the
19961 \labelwidthstring 00.00.0000
19965 SIGHANDLER(handler)
19967 this declares the function prototype for the
19973 \labelwidthstring 00.00.0000
19975 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19978 An example of using the macros above is shown below:
19981 #include <pic18fregs.h>
19984 #include <signal.h>
19988 DEF_INTHIGH(high_int)
19991 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19994 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20001 SIGHANDLER(_tmr0_handler)
20007 /* action to be taken when timer 0 overflows */
20014 SIGHANDLERNAKED(_bcol_handler)
20023 /* action to be taken when bus collision occurs */
20039 Special care should be taken when using the above scheme:
20042 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20045 when declaring SIGHANDLERNAKED handler never forget to use
20049 for proper returning.
20055 Here you can find some general tips for compiling programs with SDCC/pic16.
20056 \layout Subsubsection
20061 The default stack size (that is 64 bytes) probably is enough for many programs.
20062 One must take care that when there are many levels of function nesting,
20063 or there is excessive usage of stack, its size should be extended.
20064 An example of such a case is the printf/sprintf family of functions.
20065 If you encounter problems like not being able to print integers, then you
20066 need to set the stack size around the maximum (256 for small stack model).
20067 The following diagram shows what happens when calling printf to print an
20071 printf () --> ltoa () --> ultoa () --> divschar ()
20074 It is should be understood that stack is easily consumed when calling complicate
20076 Using command line arguments like -
20086 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20088 Other ways to reduce stack usage may exist.
20091 Debugging with SDCDB
20092 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20097 \begin_inset LatexCommand \index{sdcdb (debugger)}
20104 SDCC is distributed with a source level debugger
20105 \begin_inset LatexCommand \index{Debugger}
20110 The debugger uses a command line interface, the command repertoire of the
20111 debugger has been kept as close to gdb
20112 \begin_inset LatexCommand \index{gdb}
20116 (the GNU debugger) as possible.
20117 The configuration and build process is part of the standard compiler installati
20118 on, which also builds and installs the debugger in the target directory
20119 specified during configuration.
20120 The debugger allows you debug BOTH at the C source and at the ASM source
20122 Sdcdb is currently not available on Win32 platforms.
20125 Compiling for Debugging
20139 \begin_inset LatexCommand \index{-\/-debug}
20143 option must be specified for all files for which debug information is to
20145 The complier generates a .adb file for each of these files.
20146 The linker creates the .cdb
20147 \begin_inset LatexCommand \index{<file>.cdb}
20152 \begin_inset LatexCommand \index{<file>.adb}
20156 files and the address information.
20157 This .cdb is used by the debugger.
20160 How the Debugger Works
20173 -debug option is specified the compiler generates extra symbol information
20174 some of which are put into the assembler source and some are put into the
20176 Then the linker creates the .cdb file from the individual .adb files with
20177 the address information for the symbols.
20178 The debugger reads the symbolic information generated by the compiler &
20179 the address information generated by the linker.
20180 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20181 execution is controlled by the debugger.
20182 When a command is issued for the debugger, it translates it into appropriate
20183 commands for the simulator.
20186 Starting the Debugger
20189 The debugger can be started using the following command line.
20190 (Assume the file you are debugging has the file name foo).
20204 The debugger will look for the following files.
20207 foo.c - the source file.
20210 foo.cdb - the debugger symbol information file.
20213 foo.ihx - the Intel hex format
20214 \begin_inset LatexCommand \index{Intel hex format}
20221 Command Line Options
20234 -directory=<source file directory> this option can used to specify the directory
20236 The debugger will look into the directory list specified for source, cdb
20238 The items in the directory list must be separated by ':', e.g.
20239 if the source files can be in the directories /home/src1 and /home/src2,
20250 -directory option should be -
20260 -directory=/home/src1:/home/src2.
20261 Note there can be no spaces in the option.
20265 -cd <directory> - change to the <directory>.
20268 -fullname - used by GUI front ends.
20271 -cpu <cpu-type> - this argument is passed to the simulator please see the
20272 simulator docs for details.
20275 -X <Clock frequency > this options is passed to the simulator please see
20276 the simulator docs for details.
20279 -s <serial port file> passed to simulator see the simulator docs for details.
20282 -S <serial in,out> passed to simulator see the simulator docs for details.
20285 -k <port number> passed to simulator see the simulator docs for details.
20291 As mentioned earlier the command interface for the debugger has been deliberatel
20292 y kept as close the GNU debugger gdb, as possible.
20293 This will help the integration with existing graphical user interfaces
20294 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20295 If you use a graphical user interface for the debugger you can skip this
20297 \layout Subsubsection*
20299 break [line | file:line | function | file:function]
20302 Set breakpoint at specified line or function:
20311 sdcdb>break foo.c:100
20313 sdcdb>break funcfoo
20315 sdcdb>break foo.c:funcfoo
20316 \layout Subsubsection*
20318 clear [line | file:line | function | file:function ]
20321 Clear breakpoint at specified line or function:
20330 sdcdb>clear foo.c:100
20332 sdcdb>clear funcfoo
20334 sdcdb>clear foo.c:funcfoo
20335 \layout Subsubsection*
20340 Continue program being debugged, after breakpoint.
20341 \layout Subsubsection*
20346 Execute till the end of the current function.
20347 \layout Subsubsection*
20352 Delete breakpoint number 'n'.
20353 If used without any option clear ALL user defined break points.
20354 \layout Subsubsection*
20356 info [break | stack | frame | registers ]
20359 info break - list all breakpoints
20362 info stack - show the function call stack.
20365 info frame - show information about the current execution frame.
20368 info registers - show content of all registers.
20369 \layout Subsubsection*
20374 Step program until it reaches a different source line.
20375 Note: pressing <return> repeats the last command.
20376 \layout Subsubsection*
20381 Step program, proceeding through subroutine calls.
20382 \layout Subsubsection*
20387 Start debugged program.
20388 \layout Subsubsection*
20393 Print type information of the variable.
20394 \layout Subsubsection*
20399 print value of variable.
20400 \layout Subsubsection*
20405 load the given file name.
20406 Note this is an alternate method of loading file for debugging.
20407 \layout Subsubsection*
20412 print information about current frame.
20413 \layout Subsubsection*
20418 Toggle between C source & assembly source.
20419 \layout Subsubsection*
20421 ! simulator command
20424 Send the string following '!' to the simulator, the simulator response is
20426 Note the debugger does not interpret the command being sent to the simulator,
20427 so if a command like 'go' is sent the debugger can loose its execution
20428 context and may display incorrect values.
20429 \layout Subsubsection*
20436 My name is Bobby Brown"
20439 Interfacing with DDD
20442 The screenshot was converted from png to eps with:
20443 \begin_inset Quotes sld
20446 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20447 \begin_inset Quotes srd
20450 which produces a pretty compact eps file which is free from compression
20454 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20455 as this broke the build system on Sourceforge (pdf-file was broken).
20461 \begin_inset LatexCommand \url{http://cvs.sourceforge.net/viewcvs.py/*checkout*/sdcc/sdcc/doc/figures/ddd_example.eps}
20467 shows a screenshot of a debugging session with DDD
20468 \begin_inset LatexCommand \index{DDD (debugger)}
20472 (Unix only) on a simulated 8032.
20473 The debugging session might not run as smoothly as the screenshot suggests.
20474 The debugger allows setting of breakpoints, displaying and changing variables,
20475 single stepping through C and assembler code.
20478 The source was compiled with
20501 -debug ddd_example.c
20514 and DDD was invoked with
20521 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20524 Interfacing with XEmacs
20525 \begin_inset LatexCommand \index{XEmacs}
20530 \begin_inset LatexCommand \index{Emacs}
20537 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20538 sdcdb.el and sdcdbsrc.el.
20539 These two files can be found in the $(prefix)/bin directory after the installat
20541 These files need to be loaded into XEmacs for the interface to work.
20542 This can be done at XEmacs startup time by inserting the following into
20543 your '.xemacs' file (which can be found in your HOME directory):
20549 (load-file sdcdbsrc.el)
20555 .xemacs is a lisp file so the () around the command is REQUIRED.
20556 The files can also be loaded dynamically while XEmacs is running, set the
20557 environment variable 'EMACSLOADPATH' to the installation bin directory
20558 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20559 To start the interface enter the following command:
20573 You will prompted to enter the file name to be debugged.
20578 The command line options that are passed to the simulator directly are bound
20579 to default values in the file sdcdbsrc.el.
20580 The variables are listed below, these values maybe changed as required.
20583 sdcdbsrc-cpu-type '51
20586 sdcdbsrc-frequency '11059200
20589 sdcdbsrc-serial nil
20592 The following is a list of key mapping for the debugger interface.
20603 ;;key\SpecialChar ~
20617 binding\SpecialChar ~
20641 ;;---\SpecialChar ~
20655 -------\SpecialChar ~
20697 sdcdb-next-from-src\SpecialChar ~
20725 sdcdb-back-from-src\SpecialChar ~
20753 sdcdb-cont-from-src\SpecialChar ~
20763 SDCDB continue command
20781 sdcdb-step-from-src\SpecialChar ~
20809 sdcdb-whatis-c-sexp\SpecialChar ~
20819 SDCDB ptypecommand for data at
20886 sdcdbsrc-delete\SpecialChar ~
20900 SDCDB Delete all breakpoints if no arg
20949 given or delete arg (C-u arg x)
20967 sdcdbsrc-frame\SpecialChar ~
20982 SDCDB Display current frame if no arg,
21031 given or display frame arg
21098 sdcdbsrc-goto-sdcdb\SpecialChar ~
21108 Goto the SDCDB output buffer
21126 sdcdb-print-c-sexp\SpecialChar ~
21137 SDCDB print command for data at
21204 sdcdbsrc-goto-sdcdb\SpecialChar ~
21214 Goto the SDCDB output buffer
21232 sdcdbsrc-mode\SpecialChar ~
21248 Toggles Sdcdbsrc mode (turns it off)
21263 sdcdb-finish-from-src\SpecialChar ~
21271 SDCDB finish command
21286 sdcdb-break\SpecialChar ~
21304 Set break for line with point
21319 sdcdbsrc-mode\SpecialChar ~
21335 Toggle Sdcdbsrc mode
21350 sdcdbsrc-srcmode\SpecialChar ~
21373 Here are a few guidelines that will help the compiler generate more efficient
21374 code, some of the tips are specific to this compiler others are generally
21375 good programming practice.
21378 Use the smallest data type to represent your data-value.
21379 If it is known in advance that the value is going to be less than 256 then
21380 use an 'unsigned char' instead of a 'short' or 'int'.
21381 Please note, that ANSI C requires both signed and unsigned chars to be
21382 promoted to 'signed int' before doing any operation.
21384 \begin_inset LatexCommand \index{type promotion}
21388 can be omitted, if the result is the same.
21389 The effect of the promotion rules together with the sign-extension is often
21396 unsigned char uc = 0xfe;
21398 if (uc * uc < 0) /* this is true! */
21417 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21427 (unsigned char) -12 / (signed char) -3 = ...
21430 No, the result is not 4:
21435 (int) (unsigned char) -12 / (int) (signed char) -3 =
21437 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21439 (int) 0x00f4 / (int) 0xfffd =
21441 (int) 0x00f4 / (int) 0xfffd =
21443 (int) 244 / (int) -3 =
21445 (int) -81 = (int) 0xffaf;
21448 Don't complain, that gcc gives you a different result.
21449 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21450 Therefore the results are different.
21453 \begin_inset Quotes sld
21457 \begin_inset Quotes srd
21463 If well-defined overflow characteristics are important and negative values
21464 are not, or if you want to steer clear of sign-extension problems when
21465 manipulating bits or bytes, use one of the corresponding unsigned types.
21466 (Beware when mixing signed and unsigned values in expressions, though.)
21468 Although character types (especially unsigned char) can be used as "tiny"
21469 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21470 ble sign extension and increased code size.
21474 Use unsigned when it is known in advance that the value is not going to
21476 This helps especially if you are doing division or multiplication, bit-shifting
21477 or are using an array index.
21480 NEVER jump into a LOOP.
21483 Declare the variables to be local
21484 \begin_inset LatexCommand \index{local variables}
21488 whenever possible, especially loop control variables (induction).
21491 Since the compiler does not always do implicit integral promotion, the programme
21492 r should do an explicit cast when integral promotion is required.
21495 Reducing the size of division, multiplication & modulus operations can reduce
21496 code size substantially.
21497 Take the following code for example.
21503 foobar(unsigned int p1, unsigned char ch)
21511 unsigned char ch1 = p1 % ch ;
21522 For the modulus operation the variable ch will be promoted to unsigned int
21523 first then the modulus operation will be performed (this will lead to a
21524 call to support routine _moduint()), and the result will be casted to a
21526 If the code is changed to
21531 foobar(unsigned int p1, unsigned char ch)
21539 unsigned char ch1 = (unsigned char)p1 % ch ;
21550 It would substantially reduce the code generated (future versions of the
21551 compiler will be smart enough to detect such optimization opportunities).
21555 Have a look at the assembly listing to get a
21556 \begin_inset Quotes sld
21560 \begin_inset Quotes srd
21563 for the code generation.
21567 \begin_inset LatexCommand \index{Tools}
21571 included in the distribution
21575 \begin_inset Tabular
21576 <lyxtabular version="3" rows="12" columns="3">
21578 <column alignment="center" valignment="top" leftline="true" width="0pt">
21579 <column alignment="center" valignment="top" leftline="true" width="0pt">
21580 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21581 <row topline="true" bottomline="true">
21582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21607 <row topline="true">
21608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21616 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21621 Simulator for various architectures
21624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21633 <row topline="true">
21634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21647 header file conversion
21650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21655 sdcc/support/scripts
21659 <row topline="true">
21660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21673 header file conversion
21676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21681 sdcc/support/scripts
21685 <row topline="true">
21686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21720 <row topline="true">
21721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21755 <row topline="true">
21756 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21772 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21790 <row topline="true">
21791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21825 <row topline="true">
21826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21860 <row topline="true">
21861 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21895 <row topline="true">
21896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21930 <row topline="true" bottomline="true">
21931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21939 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21947 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21975 \begin_inset LatexCommand \index{Documentation}
21979 included in the distribution
21983 \begin_inset Tabular
21984 <lyxtabular version="3" rows="10" columns="2">
21986 <column alignment="left" valignment="top" leftline="true" width="0">
21987 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21988 <row topline="true" bottomline="true">
21989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21997 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22002 Where to get / filename
22006 <row topline="true">
22007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22012 SDCC Compiler User Guide
22015 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22020 You're reading it right now
22024 <row topline="true">
22025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22042 <row topline="true">
22043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22049 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22054 \begin_inset LatexCommand \index{Assembler documentation}
22058 Assemblers and ASLINK
22059 \begin_inset LatexCommand \index{aslink}
22064 \begin_inset LatexCommand \index{Linker documentation}
22071 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22076 sdcc/as/doc/asxhtm.html
22080 <row topline="true">
22081 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22086 SDCC regression test
22087 \begin_inset LatexCommand \index{Regression test}
22094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22099 sdcc/doc/test_suite_spec.pdf
22103 <row topline="true">
22104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22112 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22121 <row topline="true">
22122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22127 Notes on debugging with sdcdb
22128 \begin_inset LatexCommand \index{sdcdb (debugger)}
22135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22140 sdcc/debugger/README
22144 <row topline="true">
22145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22150 Software simulator for microcontrollers
22153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22180 <row topline="true">
22181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22186 Temporary notes on the pic16
22187 \begin_inset LatexCommand \index{PIC16}
22194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22199 sdcc/src/pic16/NOTES
22203 <row topline="true" bottomline="true">
22204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22209 SDCC internal documentation (debugging file format)
22212 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22248 Related open source tools
22249 \begin_inset LatexCommand \index{Related tools}
22257 \begin_inset Tabular
22258 <lyxtabular version="3" rows="11" columns="3">
22260 <column alignment="center" valignment="top" leftline="true" width="0pt">
22261 <column alignment="block" valignment="top" leftline="true" width="30line%">
22262 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22263 <row topline="true" bottomline="true">
22264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22289 <row topline="true">
22290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22296 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22311 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22317 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22325 <row topline="true">
22326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22332 \begin_inset LatexCommand \index{gputils (pic tools)}
22339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22353 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22361 <row topline="true">
22362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22384 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22392 <row topline="true">
22393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22399 \begin_inset LatexCommand \index{indent (source formatting tool)}
22406 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22411 Formats C source - Master of the white spaces
22414 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22420 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22428 <row topline="true">
22429 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22435 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22447 Object file conversion, checksumming, ...
22450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22456 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22464 <row topline="true">
22465 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22471 \begin_inset LatexCommand \index{objdump (tool)}
22478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22483 Object file conversion, ...
22486 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22491 Part of binutils (should be there anyway)
22495 <row topline="true">
22496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22502 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22514 Source code documentation system
22517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22523 \begin_inset LatexCommand \url{http://www.doxygen.org}
22531 <row topline="true">
22532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22545 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22554 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22562 <row topline="true">
22563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22569 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22581 Statically checks c sources (see
22582 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22589 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22595 \begin_inset LatexCommand \url{http://www.splint.org}
22603 <row topline="true" bottomline="true">
22604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22610 \begin_inset LatexCommand \index{ddd (debugger)}
22617 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22622 Debugger, serves nicely as GUI to sdcdb
22623 \begin_inset LatexCommand \index{sdcdb (debugger)}
22630 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22636 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22653 Related documentation / recommended reading
22657 \begin_inset Tabular
22658 <lyxtabular version="3" rows="6" columns="3">
22660 <column alignment="center" valignment="top" leftline="true" width="0pt">
22661 <column alignment="block" valignment="top" leftline="true" width="30line%">
22662 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22663 <row topline="true" bottomline="true">
22664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22672 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22689 <row topline="true">
22690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22713 \begin_inset LatexCommand \index{C Reference card}
22720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22726 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22734 <row topline="true">
22735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22757 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22765 <row topline="true">
22766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22778 Latest datasheet of the target CPU
22781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22790 <row topline="true">
22791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22803 Revision history of datasheet
22806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22815 <row topline="true" bottomline="true">
22816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22831 Advanced Compiler Design and Implementation
22834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22839 bookstore (very dedicated, probably read other books first)
22855 Some questions answered, some pointers given - it might be time to in turn
22863 can you solve your project with the selected microcontroller? Would you
22864 find out early or rather late that your target is too small/slow/whatever?
22865 Can you switch to a slightly better device if it doesn't fit?
22868 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22869 and/or another programming language be more adequate? Would an operating
22870 system on the target device help?
22873 if you solved the problem, will the marketing department be happy?
22876 if the marketing department is happy, will customers be happy?
22879 if you're the project manager, marketing department and maybe even the customer
22880 in one person, have you tried to see the project from the outside?
22883 is the project done if you think it is done? Or is just that other interface/pro
22884 tocol/feature/configuration/option missing? How about website, manual(s),
22885 internationali(z|s)ation, packaging, labels, 2nd source for components,
22886 electromagnetic compatability/interference, documentation for production,
22887 production test software, update mechanism, patent issues?
22890 is your project adequately positioned in that magic triangle: fame, fortune,
22894 Maybe not all answers to these questions are known and some answers may
22899 , nevertheless knowing these questions may help you to avoid burnout
22905 burnout is bad for electronic devices, programmers and motorcycle tyres
22909 Chances are you didn't want to hear some of them...
22913 \begin_inset LatexCommand \index{Support}
22920 SDCC has grown to be a large project.
22921 The compiler alone (without the preprocessor, assembler and linker) is
22922 well over 100,000 lines of code (blank stripped).
22923 The open source nature of this project is a key to its continued growth
22925 You gain the benefit and support of many active software developers and
22927 Is SDCC perfect? No, that's why we need your help.
22928 The developers take pride in fixing reported bugs.
22929 You can help by reporting the bugs and helping other SDCC users.
22930 There are lots of ways to contribute, and we encourage you to take part
22931 in making SDCC a great software package.
22935 The SDCC project is hosted on the SDCC sourceforge site at
22936 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22941 You'll find the complete set of mailing lists
22942 \begin_inset LatexCommand \index{Mailing list(s)}
22946 , forums, bug reporting system, patch submission
22947 \begin_inset LatexCommand \index{Patch submission}
22952 \begin_inset LatexCommand \index{download}
22956 area and cvs code repository
22957 \begin_inset LatexCommand \index{cvs code repository}
22965 \begin_inset LatexCommand \index{Bug reporting}
22970 \begin_inset LatexCommand \index{Reporting bugs}
22977 The recommended way of reporting bugs is using the infrastructure of the
22979 You can follow the status of bug reports there and have an overview about
22983 Bug reports are automatically forwarded to the developer mailing list and
22984 will be fixed ASAP.
22985 When reporting a bug, it is very useful to include a small test program
22986 (the smaller the better) which reproduces the problem.
22987 If you can isolate the problem by looking at the generated assembly code,
22988 this can be very helpful.
22989 Compiling your program with the -
23000 \begin_inset LatexCommand \index{-\/-dumpall}
23004 option can sometimes be useful in locating optimization problems.
23005 When reporting a bug please maker sure you:
23008 Attach the code you are compiling with SDCC.
23012 Specify the exact command you use to run SDCC, or attach your Makefile.
23016 Specify the SDCC version (type "
23022 "), your platform, and operating system.
23026 Provide an exact copy of any error message or incorrect output.
23030 Put something meaningful in the subject of your message.
23033 Please attempt to include these 5 important parts, as applicable, in all
23034 requests for support or when reporting any problems or bugs with SDCC.
23035 Though this will make your message lengthy, it will greatly improve your
23036 chance that SDCC users and developers will be able to help you.
23037 Some SDCC developers are frustrated by bug reports without code provided
23038 that they can use to reproduce and ultimately fix the problem, so please
23039 be sure to provide sample code if you are reporting a bug!
23042 Please have a short check that you are using a recent version of SDCC and
23043 the bug is not yet known.
23044 This is the link for reporting bugs:
23045 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23052 Requesting Features
23053 \begin_inset LatexCommand \label{sub:Requesting-Features}
23058 \begin_inset LatexCommand \index{Feature request}
23063 \begin_inset LatexCommand \index{Requesting features}
23070 Like bug reports feature requests are forwarded to the developer mailing
23072 This is the link for requesting features:
23073 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23083 Like bug reports contributed patches are forwarded to the developer mailing
23085 This is the link for submitting patches
23086 \begin_inset LatexCommand \index{Patch submission}
23091 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23098 You need to specify some parameters to the
23102 command for the patches to be useful.
23103 If you modified more than one file a patch created f.e.
23108 \begin_inset Quotes sld
23111 diff -Naur unmodified_directory modified_directory >my_changes.patch
23112 \begin_inset Quotes srd
23118 will be fine, otherwise
23122 \begin_inset Quotes sld
23125 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23126 \begin_inset Quotes srd
23139 These links should take you directly to the
23140 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23150 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23151 automated messages (mid 2003)
23155 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23160 \begin_inset LatexCommand \index{Mailing list(s)}
23164 and forums are archived and searchable so if you are lucky someone already
23165 had a similar problem.
23166 While mails to the lists themselves are delivered promptly their web front
23167 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23168 if you're seriously using SDCC please consider subscribing to the lists.
23174 You can follow the status of the cvs version
23175 \begin_inset LatexCommand \index{version}
23179 of SDCC by watching the Changelog
23180 \begin_inset LatexCommand \index{Changelog}
23184 in the cvs-repository
23189 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
23197 \begin_inset LatexCommand \index{Release policy}
23204 Historically there often were long delays between official releases and
23205 the sourceforge download area tends to get not updated at all.
23206 Excuses in the past might have referred to problems with live range analysis,
23207 but as this was fixed a while ago, the current problem is that another
23208 excuse has to be found.
23209 Kidding aside, we have to get better there! On the other hand there are
23210 daily snapshots available at
23211 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23215 , and you can always build the very last version (hopefully with many bugs
23216 fixed, and features added) from the source code available at
23217 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23225 \begin_inset LatexCommand \index{Examples}
23232 You'll find some small examples in the directory
23234 sdcc/device/examples/.
23237 More examples and libraries are available at
23239 The SDCC Open Knowledge Resource
23240 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23247 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23254 I did insert a reference to Paul's web site here although it seems rather
23255 dedicated to a specific 8032 board (I think it's okay because it f.e.
23256 shows LCD/Harddisc interface and has a free 8051 monitor.
23257 Independent 8032 board vendors face hard competition of heavily subsidized
23258 development boards anyway).
23261 Maybe we should include some links to real world applications.
23262 Preferably pointer to pointers (one for each architecture) so this stays
23267 \begin_inset LatexCommand \index{Quality control}
23274 The compiler is passed through nightly compile and build checks.
23280 \begin_inset LatexCommand \index{Regression test}
23284 check that SDCC itself compiles flawlessly on several platforms and checks
23285 the quality of the code generated by SDCC by running the code through simulator
23287 There is a separate document
23290 \begin_inset LatexCommand \index{Test suite}
23299 You'll find the test code in the directory
23301 sdcc/support/regression
23304 You can run these tests manually by running
23308 in this directory (or f.e.
23313 \begin_inset Quotes sld
23317 \begin_inset Quotes srd
23323 if you don't want to run the complete tests).
23324 The test code might also be interesting if you want to look for examples
23325 \begin_inset LatexCommand \index{Examples}
23329 checking corner cases of SDCC or if you plan to submit patches
23330 \begin_inset LatexCommand \index{Patch submission}
23337 The pic port uses a different set of regression tests, you'll find them
23340 sdcc/src/regression
23345 SDCC Technical Data
23349 \begin_inset LatexCommand \index{Optimizations}
23356 SDCC performs a host of standard optimizations in addition to some MCU specific
23361 Sub-expression Elimination
23362 \begin_inset LatexCommand \index{Subexpression elimination}
23369 The compiler does local and
23395 will be translated to
23407 Some subexpressions are not as obvious as the above example, e.g.:
23417 In this case the address arithmetic a->b[i] will be computed only once;
23418 the equivalent code in C would be.
23430 The compiler will try to keep these temporary variables in registers.
23433 Dead-Code Elimination
23434 \begin_inset LatexCommand \index{Dead-code elimination}
23455 i = 1; \SpecialChar ~
23464 global = 1;\SpecialChar ~
23477 global = 3;\SpecialChar ~
23502 \begin_inset LatexCommand \index{Copy propagation}
23558 Note: the dead stores created by this copy propagation will be eliminated
23559 by dead-code elimination.
23563 \begin_inset LatexCommand \index{Loop optimization}
23568 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23575 Two types of loop optimizations are done by SDCC
23583 of loop induction variables.
23584 In addition to the strength reduction the optimizer marks the induction
23585 variables and the register allocator tries to keep the induction variables
23586 in registers for the duration of the loop.
23587 Because of this preference of the register allocator
23588 \begin_inset LatexCommand \index{Register allocation}
23592 , loop induction optimization causes an increase in register pressure, which
23593 may cause unwanted spilling of other temporary variables into the stack
23594 \begin_inset LatexCommand \index{stack}
23599 The compiler will generate a warning message when it is forced to allocate
23600 extra space either on the stack or data space.
23601 If this extra space allocation is undesirable then induction optimization
23602 can be eliminated either for the entire source file (with -
23612 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23614 \begin_inset LatexCommand \index{\#pragma noinduction}
23627 for (i = 0 ; i < 100 ; i ++)
23643 for (i = 0; i < 100; i++)
23652 As mentioned previously some loop invariants are not as apparent, all static
23653 address computations are also moved out of the loop.
23658 \begin_inset LatexCommand \index{Strength reduction}
23662 , this optimization substitutes an expression by a cheaper expression:
23667 for (i=0;i < 100; i++)
23685 for (i=0;i< 100;i++) {
23691 ar[itemp1] = itemp2;
23708 The more expensive multiplication
23709 \begin_inset LatexCommand \index{Multiplication}
23713 is changed to a less expensive addition.
23717 \begin_inset LatexCommand \index{Loop reversing}
23724 This optimization is done to reduce the overhead of checking loop boundaries
23725 for every iteration.
23726 Some simple loops can be reversed and implemented using a
23727 \begin_inset Quotes eld
23730 decrement and jump if not zero
23731 \begin_inset Quotes erd
23735 SDCC checks for the following criterion to determine if a loop is reversible
23736 (note: more sophisticated compilers use data-dependency analysis to make
23737 this determination, SDCC uses a more simple minded analysis).
23740 The 'for' loop is of the form
23746 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23756 The <for body> does not contain
23757 \begin_inset Quotes eld
23761 \begin_inset Quotes erd
23765 \begin_inset Quotes erd
23771 All goto's are contained within the loop.
23774 No function calls within the loop.
23777 The loop control variable <sym> is not assigned any value within the loop
23780 The loop control variable does NOT participate in any arithmetic operation
23784 There are NO switch statements in the loop.
23787 Algebraic Simplifications
23790 SDCC does numerous algebraic simplifications, the following is a small sub-set
23791 of these optimizations.
23796 i = j + 0;\SpecialChar ~
23800 /* changed to: */\SpecialChar ~
23806 i /= 2;\SpecialChar ~
23813 /* changed to: */\SpecialChar ~
23819 i = j - j;\SpecialChar ~
23823 /* changed to: */\SpecialChar ~
23829 i = j / 1;\SpecialChar ~
23833 /* changed to: */\SpecialChar ~
23840 Note the subexpressions
23841 \begin_inset LatexCommand \index{Subexpression}
23845 given above are generally introduced by macro expansions or as a result
23846 of copy/constant propagation.
23849 'switch' Statements
23850 \begin_inset LatexCommand \label{sub:'switch'-Statements}
23855 \begin_inset LatexCommand \index{switch statement}
23862 SDCC can optimize switch statements to jump tables
23863 \begin_inset LatexCommand \index{jump tables}
23868 It makes the decision based on an estimate of the generated code size.
23869 SDCC is quite liberal in the requirements for jump table generation:
23872 The labels need not be in order, and the starting number need not be one
23873 or zero, the case labels are in numerical sequence or not too many case
23874 labels are missing.
23880 switch(i) {\SpecialChar ~
23911 case 4: ...\SpecialChar ~
23943 case 5: ...\SpecialChar ~
23975 case 3: ...\SpecialChar ~
24006 case 6: ...\SpecialChar ~
24038 case 7: ...\SpecialChar ~
24070 case 8: ...\SpecialChar ~
24102 case 9: ...\SpecialChar ~
24134 case 10: ...\SpecialChar ~
24165 case 11: ...\SpecialChar ~
24232 Both the above switch statements will be implemented using a jump-table.
24233 The example to the right side is slightly more efficient as the check for
24234 the lower boundary of the jump-table is not needed.
24238 The number of case labels is not larger than supported by the target architectur
24242 If the case labels are not in numerical sequence ('gaps' between cases)
24243 SDCC checks whether a jump table with additionally inserted dummy cases
24244 is still attractive.
24248 If the starting number is not zero and a check for the lower boundary of
24249 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24254 Switch statements which have large gaps in the numeric sequence or those
24255 that have too many case labels can be split into more than one switch statement
24256 for efficient code generation, e.g.:
24336 If the above switch statement is broken down into two switch statements
24426 then both the switch statements will be implemented using jump-tables whereas
24427 the unmodified switch statement will not be.
24430 There might be reasons which SDCC cannot know about to either favour or
24431 not favour jump tables.
24432 If the target system has to be as quick for the last switch case as for
24433 the first (pro jump table), or if the switch argument is known to be zero
24434 in the majority of the cases (contra jump table).
24437 The pragma nojtbound
24438 \begin_inset LatexCommand \index{\#pragma nojtbound}
24442 can be used to turn off checking the
24455 It has no effect if a default label is supplied.
24456 Use of this pragma is dangerous: if the switch
24457 \begin_inset LatexCommand \index{switch statement}
24461 argument is not matched by a case statement the processor will happily
24465 Bit-shifting Operations
24466 \begin_inset LatexCommand \index{Bit shifting}
24473 Bit shifting is one of the most frequently used operation in embedded programmin
24475 SDCC tries to implement bit-shift operations in the most efficient way
24491 generates the following code:
24508 In general SDCC will never setup a loop if the shift count is known.
24551 \begin_inset LatexCommand \index{Bit rotation}
24558 A special case of the bit-shift operation is bit rotation
24559 \begin_inset LatexCommand \index{rotating bits}
24563 , SDCC recognizes the following expression to be a left bit-rotation:
24573 char i;\SpecialChar ~
24584 /* unsigned is needed for rotation */
24589 i = ((i << 1) | (i >> 7));
24598 will generate the following code:
24617 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24618 ns of this case will also be recognized as bit-rotation, i.e.:
24623 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24626 Nibble and Byte Swapping
24629 Other special cases of the bit-shift operations are nibble or byte swapping
24630 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24634 , SDCC recognizes the following expressions:
24657 i = ((i << 4) | (i >> 4));
24663 j = ((j << 8) | (j >> 8));
24666 and generates a swap instruction for the nibble swapping
24667 \begin_inset LatexCommand \index{Nibble swapping}
24671 or move instructions for the byte swapping
24672 \begin_inset LatexCommand \index{Byte swapping}
24678 \begin_inset Quotes sld
24682 \begin_inset Quotes srd
24685 example can be used to convert from little to big-endian or vice versa.
24686 If you want to change the endianness of a
24690 integer you have to cast to
24697 Note that SDCC stores numbers in little-endian
24703 Usually 8-bit processors don't care much about endianness.
24704 This is not the case for the standard 8051 which only has an instruction
24710 \begin_inset LatexCommand \index{DPTR}
24718 so little-endian is the more efficient byte order.
24722 \begin_inset LatexCommand \index{little-endian}
24727 \begin_inset LatexCommand \index{Endianness}
24732 lowest order first).
24736 \begin_inset LatexCommand \index{Highest Order Bit}
24741 \begin_inset LatexCommand \index{Any Order Bit}
24748 It is frequently required to obtain the highest order bit of an integral
24749 type (long, int, short or char types).
24750 Also obtaining any other order bit is not uncommon.
24751 SDCC recognizes the following expressions to yield the highest order bit
24752 and generates optimized code for it, e.g.:
24765 unsigned char hob1, aob1;
24769 bit hob2, hob3, aob2, aob3;
24778 hob1 = (gint >> 15) & 1;
24782 hob2 = (gint >> 15) & 1;
24786 hob3 = gint & 0x8000;
24790 aob1 = (gint >> 9) & 1;
24794 aob2 = (gint >> 8) & 1;
24798 aob3 = gint & 0x0800;
24808 will generate the following code:
24841 000A E5*01\SpecialChar ~
24868 000C 23\SpecialChar ~
24899 000D 54 01\SpecialChar ~
24926 000F F5*02\SpecialChar ~
24981 0011 E5*01\SpecialChar ~
25008 0013 33\SpecialChar ~
25038 0014 92*00\SpecialChar ~
25093 0016 E5*01\SpecialChar ~
25120 0018 33\SpecialChar ~
25150 0019 92*01\SpecialChar ~
25205 001B E5*01\SpecialChar ~
25232 001D 03\SpecialChar ~
25263 001E 54 01\SpecialChar ~
25290 0020 F5*03\SpecialChar ~
25345 0022 E5*01\SpecialChar ~
25372 0024 13\SpecialChar ~
25402 0025 92*02\SpecialChar ~
25457 0027 E5*01\SpecialChar ~
25484 0029 A2 E3\SpecialChar ~
25511 002B 92*03\SpecialChar ~
25539 Other variations of these cases however will
25544 They are standard C expressions, so I heartily recommend these be the only
25545 way to get the highest order bit, (it is portable).
25546 Of course it will be recognized even if it is embedded in other expressions,
25552 xyz = gint + ((gint >> 15) & 1);
25555 will still be recognized.
25559 \begin_inset LatexCommand \index{Higher Order Byte}
25563 / Higher Order Word
25564 \begin_inset LatexCommand \index{Higher Order Word}
25571 It is also frequently required to obtain a higher order byte or word of
25572 a larger integral type (long, int or short types).
25573 SDCC recognizes the following expressions to yield the higher order byte
25574 or word and generates optimized code for it, e.g.:
25581 unsigned long int glong;
25589 unsigned char hob1, hob2;
25593 unsigned int how1, how2;
25602 hob1 = (gint >> 8) & 0xFF;
25606 hob2 = glong >> 24;
25610 how1 = (glong >> 16) & 0xFFFF;
25624 will generate the following code:
25657 0037 85*01*06\SpecialChar ~
25679 _foo_hob1_1_1,(_gint + 1)
25709 003A 85*05*07\SpecialChar ~
25731 _foo_hob2_1_1,(_glong + 3)
25761 003D 85*04*08\SpecialChar ~
25783 _foo_how1_1_1,(_glong + 2)
25785 0040 85*05*09\SpecialChar ~
25807 (_foo_how1_1_1 + 1),(_glong + 3)
25809 0043 85*03*0A\SpecialChar ~
25831 _foo_how2_1_1,(_glong + 1)
25833 0046 85*04*0B\SpecialChar ~
25855 (_foo_how2_1_1 + 1),(_glong + 2)
25858 Again, variations of these cases may
25863 They are standard C expressions, so I heartily recommend these be the only
25864 way to get the higher order byte/word, (it is portable).
25865 Of course it will be recognized even if it is embedded in other expressions,
25871 xyz = gint + ((gint >> 8) & 0xFF);
25874 will still be recognized.
25878 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
25883 \begin_inset LatexCommand \index{Peephole optimizer}
25890 The compiler uses a rule based, pattern matching and re-writing mechanism
25891 for peep-hole optimization.
25896 a peep-hole optimizer by Christopher W.
25897 Fraser (cwfraser\SpecialChar ~
25900 A default set of rules are compiled into the compiler, additional rules
25901 may be added with the
25914 \begin_inset LatexCommand \index{-\/-peep-file}
25921 The rule language is best illustrated with examples.
25945 The above rule will change the following assembly
25946 \begin_inset LatexCommand \index{Assembler routines}
25968 Note: All occurrences of a
25972 (pattern variable) must denote the same string.
25973 With the above rule, the assembly sequence:
25983 will remain unmodified.
25987 Other special case optimizations may be added by the user (via
26003 some variants of the 8051 MCU
26004 \begin_inset LatexCommand \index{MCS51 variants}
26017 The following two rules will change all
26036 replace { lcall %1 } by { acall %1 }
26038 replace { ljmp %1 } by { ajmp %1 }
26043 inline-assembler code
26045 is also passed through the peep hole optimizer, thus the peephole optimizer
26046 can also be used as an assembly level macro expander.
26047 The rules themselves are MCU dependent whereas the rule language infra-structur
26048 e is MCU independent.
26049 Peephole optimization rules for other MCU can be easily programmed using
26054 The syntax for a rule is as follows:
26059 rule := replace [ restart ] '{' <assembly sequence> '
26097 <assembly sequence> '
26115 '}' [if <functionName> ] '
26120 <assembly sequence> := assembly instruction (each instruction including
26121 labels must be on a separate line).
26125 The optimizer will apply to the rules one by one from the top in the sequence
26126 of their appearance, it will terminate when all rules are exhausted.
26127 If the 'restart' option is specified, then the optimizer will start matching
26128 the rules again from the top, this option for a rule is expensive (performance)
26129 , it is intended to be used in situations where a transformation will trigger
26130 the same rule again.
26131 An example of this (not a good one, it has side effects) is the following
26154 Note that the replace pattern cannot be a blank, but can be a comment line.
26155 Without the 'restart' option only the innermost 'pop' 'push' pair would
26156 be eliminated, i.e.:
26186 the restart option the rule will be applied again to the resulting code
26187 and then all the pop-push pairs will be eliminated to yield:
26197 A conditional function can be attached to a rule.
26198 Attaching rules are somewhat more involved, let me illustrate this with
26225 The optimizer does a look-up of a function name table defined in function
26230 in the source file SDCCpeeph.c, with the name
26235 If it finds a corresponding entry the function is called.
26236 Note there can be no parameters specified for these functions, in this
26241 is crucial, since the function
26245 expects to find the label in that particular variable (the hash table containin
26246 g the variable bindings is passed as a parameter).
26247 If you want to code more such functions, take a close look at the function
26248 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26249 Currently implemented are
26251 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26252 24bitMode, portIsDS390, 24bitModeAndPortDS390
26261 I know this whole thing is a little kludgey, but maybe some day we will
26262 have some better means.
26263 If you are looking at this file, you will see the default rules that are
26264 compiled into the compiler, you can add your own rules in the default set
26265 there if you get tired of specifying the -
26279 \begin_inset LatexCommand \index{ANSI-compliance}
26284 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26291 Deviations from the compliance:
26294 functions are not reentrant
26295 \begin_inset LatexCommand \index{reentrant}
26299 unless explicitly declared as such or the
26312 \begin_inset LatexCommand \index{-\/-stack-auto}
26318 command line option is specified.
26321 structures and unions cannot be assigned values directly, cannot be passed
26322 as function parameters or assigned to each other and cannot be a return
26323 value from a function, e.g.:
26349 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26360 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26382 return rets;/* is invalid in SDCC although allowed in ANSI */
26388 initialization of structure arrays must be fully braced.
26394 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26396 struct s { char x } a[] = {{1}, {2}}; /* OK */
26401 \begin_inset LatexCommand \index{long long (not supported)}
26406 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26414 \begin_inset LatexCommand \index{double (not supported)}
26418 ' precision floating point
26419 \begin_inset LatexCommand \index{Floating point support}
26427 \begin_inset LatexCommand \index{K\&R style}
26431 function declarations are NOT allowed.
26437 foo(i,j) /* this old style of function declarations */
26439 int i,j; /* are valid in ANSI but not valid in SDCC */
26454 Most enhancements in C99 are not supported, f.e.:
26463 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
26470 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
26474 Certain words that are valid identifiers in the standard may be reserved
26475 words in SDCC unless the
26488 \begin_inset LatexCommand \index{-\/-std-c89}
26503 \begin_inset LatexCommand \index{-\/-std-c99}
26509 command line options are used.
26510 These may include (depending on the selected processor): 'at', 'banked',
26511 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26512 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26513 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26515 Compliant equivalents of these keywords are always available in a form
26516 that begin with two underscores
26517 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26522 '__data' instead of 'data'.
26525 Cyclomatic Complexity
26526 \begin_inset LatexCommand \index{Cyclomatic complexity}
26533 Cyclomatic complexity of a function is defined as the number of independent
26534 paths the program can take during execution of the function.
26535 This is an important number since it defines the number test cases you
26536 have to generate to validate the function.
26537 The accepted industry standard for complexity number is 10, if the cyclomatic
26538 complexity reported by SDCC exceeds 10 you should think about simplification
26539 of the function logic.
26540 Note that the complexity level is not related to the number of lines of
26541 code in a function.
26542 Large functions can have low complexity, and small functions can have large
26548 SDCC uses the following formula to compute the complexity:
26553 complexity = (number of edges in control flow graph) - (number of nodes
26554 in control flow graph) + 2;
26558 Having said that the industry standard is 10, you should be aware that in
26559 some cases it be may unavoidable to have a complexity level of less than
26561 For example if you have switch statement with more than 10 case labels,
26562 each case label adds one to the complexity level.
26563 The complexity level is by no means an absolute measure of the algorithmic
26564 complexity of the function, it does however provide a good starting point
26565 for which functions you might look at for further optimization.
26568 Retargetting for other Processors
26571 The issues for retargetting the compiler are far too numerous to be covered
26573 What follows is a brief description of each of the seven phases of the
26574 compiler and its MCU dependency.
26577 Parsing the source and building the annotated parse tree.
26578 This phase is largely MCU independent (except for the language extensions).
26579 Syntax & semantic checks are also done in this phase, along with some initial
26580 optimizations like back patching labels and the pattern matching optimizations
26581 like bit-rotation etc.
26584 The second phase involves generating an intermediate code which can be easy
26585 manipulated during the later phases.
26586 This phase is entirely MCU independent.
26587 The intermediate code generation assumes the target machine has unlimited
26588 number of registers, and designates them with the name iTemp.
26589 The compiler can be made to dump a human readable form of the code generated
26603 This phase does the bulk of the standard optimizations and is also MCU independe
26605 This phase can be broken down into several sub-phases:
26609 Break down intermediate code (iCode) into basic blocks.
26611 Do control flow & data flow analysis on the basic blocks.
26613 Do local common subexpression elimination, then global subexpression elimination
26615 Dead code elimination
26619 If loop optimizations caused any changes then do 'global subexpression eliminati
26620 on' and 'dead code elimination' again.
26623 This phase determines the live-ranges; by live range I mean those iTemp
26624 variables defined by the compiler that still survive after all the optimization
26626 Live range analysis
26627 \begin_inset LatexCommand \index{Live range analysis}
26631 is essential for register allocation, since these computation determines
26632 which of these iTemps will be assigned to registers, and for how long.
26635 Phase five is register allocation.
26636 There are two parts to this process.
26640 The first part I call 'register packing' (for lack of a better term).
26641 In this case several MCU specific expression folding is done to reduce
26646 The second part is more MCU independent and deals with allocating registers
26647 to the remaining live ranges.
26648 A lot of MCU specific code does creep into this phase because of the limited
26649 number of index registers available in the 8051.
26652 The Code generation phase is (unhappily), entirely MCU dependent and very
26653 little (if any at all) of this code can be reused for other MCU.
26654 However the scheme for allocating a homogenized assembler operand for each
26655 iCode operand may be reused.
26658 As mentioned in the optimization section the peep-hole optimizer is rule
26659 based system, which can reprogrammed for other MCUs.
26663 \begin_inset LatexCommand \index{Compiler internals}
26670 The anatomy of the compiler
26671 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26680 This is an excerpt from an article published in Circuit Cellar Magazine
26686 It's a little outdated (the compiler is much more efficient now and user/develo
26687 per friendly), but pretty well exposes the guts of it all.
26693 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26694 It is fairly easy to retarget for other 8-bit MCU.
26695 Here we take a look at some of the internals of the compiler.
26700 \begin_inset LatexCommand \index{Parsing}
26707 Parsing the input source file and creating an AST (Annotated Syntax Tree
26708 \begin_inset LatexCommand \index{Annotated syntax tree}
26713 This phase also involves propagating types (annotating each node of the
26714 parse tree with type information) and semantic analysis.
26715 There are some MCU specific parsing rules.
26716 For example the storage classes, the extended storage classes are MCU specific
26717 while there may be a xdata storage class for 8051 there is no such storage
26718 class for z80 or Atmel AVR.
26719 SDCC allows MCU specific storage class extensions, i.e.
26720 xdata will be treated as a storage class specifier when parsing 8051 C
26721 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
26726 \begin_inset LatexCommand \index{iCode}
26733 Intermediate code generation.
26734 In this phase the AST is broken down into three-operand form (iCode).
26735 These three operand forms are represented as doubly linked lists.
26736 ICode is the term given to the intermediate form generated by the compiler.
26737 ICode example section shows some examples of iCode generated for some simple
26738 C source functions.
26742 \begin_inset LatexCommand \index{Optimizations}
26749 Bulk of the target independent optimizations is performed in this phase.
26750 The optimizations include constant propagation, common sub-expression eliminati
26751 on, loop invariant code movement, strength reduction of loop induction variables
26752 and dead-code elimination.
26755 Live range analysis
26756 \begin_inset LatexCommand \index{Live range analysis}
26763 During intermediate code generation phase, the compiler assumes the target
26764 machine has infinite number of registers and generates a lot of temporary
26766 The live range computation determines the lifetime of each of these compiler-ge
26767 nerated temporaries.
26768 A picture speaks a thousand words.
26769 ICode example sections show the live range annotations for each of the
26771 It is important to note here, each iCode is assigned a number in the order
26772 of its execution in the function.
26773 The live ranges are computed in terms of these numbers.
26774 The from number is the number of the iCode which first defines the operand
26775 and the to number signifies the iCode which uses this operand last.
26778 Register Allocation
26779 \begin_inset LatexCommand \index{Register allocation}
26786 The register allocation determines the type and number of registers needed
26788 In most MCUs only a few registers can be used for indirect addressing.
26789 In case of 8051 for example the registers R0 & R1 can be used to indirectly
26790 address the internal ram and DPTR to indirectly address the external ram.
26791 The compiler will try to allocate the appropriate register to pointer variables
26793 ICode example section shows the operands annotated with the registers assigned
26795 The compiler will try to keep operands in registers as much as possible;
26796 there are several schemes the compiler uses to do achieve this.
26797 When the compiler runs out of registers the compiler will check to see
26798 if there are any live operands which is not used or defined in the current
26799 basic block being processed, if there are any found then it will push that
26800 operand and use the registers in this block, the operand will then be popped
26801 at the end of the basic block.
26805 There are other MCU specific considerations in this phase.
26806 Some MCUs have an accumulator; very short-lived operands could be assigned
26807 to the accumulator instead of a general-purpose register.
26813 Figure II gives a table of iCode operations supported by the compiler.
26814 The code generation involves translating these operations into corresponding
26815 assembly code for the processor.
26816 This sounds overly simple but that is the essence of code generation.
26817 Some of the iCode operations are generated on a MCU specific manner for
26818 example, the z80 port does not use registers to pass parameters so the
26819 SEND and RECV iCode operations will not be generated, and it also does
26820 not support JUMPTABLES.
26827 <Where is Figure II?>
26830 In the original article Figure II was announced to be downloadable on
26835 Unfortunately it never seemed to have shown up there, so: where is Figure
26840 \begin_inset LatexCommand \index{iCode}
26847 This section shows some details of iCode.
26848 The example C code does not do anything useful; it is used as an example
26849 to illustrate the intermediate code generated by the compiler.
26861 /* This function does nothing useful.
26868 for the purpose of explaining iCode */
26871 short function (data int *x)
26879 short i=10; \SpecialChar ~
26881 /* dead initialization eliminated */
26886 short sum=10; /* dead initialization eliminated */
26899 while (*x) *x++ = *p++;
26913 /* compiler detects i,j to be induction variables */
26917 for (i = 0, j = 10 ; i < 10 ; i++, j
26943 mul += i * 3; \SpecialChar ~
26945 /* this multiplication remains */
26951 gint += j * 3;\SpecialChar ~
26953 /* this multiplication changed to addition */
26967 In addition to the operands each iCode contains information about the filename
26968 and line it corresponds to in the source file.
26969 The first field in the listing should be interpreted as follows:
26974 Filename(linenumber: iCode Execution sequence number : ICode hash table
26975 key : loop depth of the iCode).
26980 Then follows the human readable form of the ICode operation.
26981 Each operand of this triplet form can be of three basic types a) compiler
26982 generated temporary b) user defined variable c) a constant value.
26983 Note that local variables and parameters are replaced by compiler generated
26986 \begin_inset LatexCommand \index{Live range analysis}
26990 are computed only for temporaries (i.e.
26991 live ranges are not computed for global variables).
26993 \begin_inset LatexCommand \index{Register allocation}
26997 are allocated for temporaries only.
26998 Operands are formatted in the following manner:
27003 Operand Name [lr live-from : live-to ] { type information } [ registers
27009 As mentioned earlier the live ranges are computed in terms of the execution
27010 sequence number of the iCodes, for example
27012 the iTemp0 is live from (i.e.
27013 first defined in iCode with execution sequence number 3, and is last used
27014 in the iCode with sequence number 5).
27015 For induction variables such as iTemp21 the live range computation extends
27016 the lifetime from the start to the end of the loop.
27018 The register allocator used the live range information to allocate registers,
27019 the same registers may be used for different temporaries if their live
27020 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27021 iTemp17 since their live ranges do not overlap.
27022 In addition the allocator also takes into consideration the type and usage
27023 of a temporary, for example itemp6 is a pointer to near space and is used
27024 as to fetch data from (i.e.
27025 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27026 Some short lived temporaries are allocated to special registers which have
27027 meaning to the code generator e.g.
27028 iTemp13 is allocated to a pseudo register CC which tells the back end that
27029 the temporary is used only for a conditional jump the code generation makes
27030 use of this information to optimize a compare and jump ICode.
27032 There are several loop optimizations
27033 \begin_inset LatexCommand \index{Loop optimization}
27037 performed by the compiler.
27038 It can detect induction variables iTemp21(i) and iTemp23(j).
27039 Also note the compiler does selective strength reduction
27040 \begin_inset LatexCommand \index{Strength reduction}
27045 the multiplication of an induction variable in line 18 (gint = j * 3) is
27046 changed to addition, a new temporary iTemp17 is allocated and assigned
27047 a initial value, a constant 3 is then added for each iteration of the loop.
27048 The compiler does not change the multiplication
27049 \begin_inset LatexCommand \index{Multiplication}
27053 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27055 Note the dead code elimination
27056 \begin_inset LatexCommand \index{Dead-code elimination}
27060 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27068 Sample.c (5:1:0:0) _entry($9) :
27073 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27078 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27083 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27088 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27094 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27099 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27105 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27110 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27116 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27122 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27128 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27134 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27135 * int}[r0] + 0x2 {short}
27140 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27145 Sample.c(11:17:21:0)_whilebreak_0($3) :
27150 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27155 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27160 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27165 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27170 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27175 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27180 Sample.c(15:24:26:1)_forcond_0($4) :
27185 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27191 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27196 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27197 + ITemp21 [lr21:38]{short}[r4]
27202 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27208 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27209 + iTemp15 [lr29:30]{short}[r1]
27214 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27220 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27226 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27232 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27238 Sample.c(19:38:47:1) goto _forcond_0($4)
27243 Sample.c(19:39:48:0)_forbreak_0($7) :
27248 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27249 + ITemp11 [lr19:40]{short}[r3]
27254 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27259 Sample.c(20:42:51:0)_return($8) :
27264 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27270 Finally the code generated for this function:
27311 ; ----------------------------------------------
27316 ; function function
27321 ; ----------------------------------------------
27331 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27343 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27355 ;_whilecontinue_0($1) :
27365 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27370 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27429 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27448 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27495 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27535 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27561 ; iTemp6 [lr5:16]{_near * int}[r0] =
27566 ; iTemp6 [lr5:16]{_near * int}[r0] +
27583 ; goto _whilecontinue_0($1)
27595 ; _whilebreak_0($3) :
27605 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27617 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27629 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27641 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27660 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27689 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27694 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27739 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
27744 ; iTemp21 [lr21:38]{short}[r4]
27770 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
27803 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
27808 ; iTemp15 [lr29:30]{short}[r1]
27827 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
27874 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
27921 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
27933 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
27947 cjne r5,#0xff,00104$
27959 ; goto _forcond_0($4)
27971 ; _forbreak_0($7) :
27981 ; ret iTemp24 [lr40:41]{short}
28024 A few words about basic block successors, predecessors and dominators
28027 Successors are basic blocks
28028 \begin_inset LatexCommand \index{Basic blocks}
28032 that might execute after this basic block.
28034 Predecessors are basic blocks that might execute before reaching this basic
28037 Dominators are basic blocks that WILL execute before reaching this basic
28071 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28074 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28077 c) domVect of [BB4] = BB1 ...
28078 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28086 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28096 Thanks to all the other volunteer developers who have helped with coding,
28097 testing, web-page creation, distribution sets, etc.
28098 You know who you are :-)
28105 This document was initially written by Sandeep Dutta
28108 All product names mentioned herein may be trademarks
28109 \begin_inset LatexCommand \index{Trademarks}
28113 of their respective companies.
28120 To avoid confusion, the installation and building options for SDCC itself
28121 (chapter 2) are not part of the index.
28125 \begin_inset LatexCommand \printindex{}