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
3689 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
3694 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
3699 \begin_inset Quotes sld
3702 have you ever heard of an open source compiler that compiles a subset of
3704 \begin_inset Quotes srd
3707 we expect you to have a quick look there and come back.
3708 If you read this you are on the right track.
3711 Some sections attribute more space to problems, restrictions and warnings
3712 than to the solution.
3715 The installation section and the section about the debugger is intimidating.
3718 There are still lots of typos and there are more different writing styles
3722 Testing the SDCC Compiler
3723 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3730 The first thing you should do after installing your SDCC compiler is to
3746 \begin_inset LatexCommand \index{version}
3753 at the prompt, and the program should run and tell you the version.
3754 If it doesn't run, or gives a message about not finding sdcc program, then
3755 you need to check over your installation.
3756 Make sure that the sdcc bin directory is in your executable search path
3757 defined by the PATH environment setting (
3762 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3769 Install trouble-shooting for suggestions
3772 Make sure that the sdcc program is in the bin folder, if not perhaps something
3773 did not install correctly.
3781 is commonly installed as described in section
3782 \begin_inset Quotes sld
3785 Install and search paths
3786 \begin_inset Quotes srd
3795 Make sure the compiler works on a very simple example.
3796 Type in the following test.c program using your favorite
3822 Compile this using the following command:
3831 If all goes well, the compiler will generate a test.asm and test.rel file.
3832 Congratulations, you've just compiled your first program with SDCC.
3833 We used the -c option to tell SDCC not to link the generated code, just
3834 to keep things simple for this step.
3842 The next step is to try it with the linker.
3852 If all goes well the compiler will link with the libraries and produce
3853 a test.ihx output file.
3858 (no test.ihx, and the linker generates warnings), then the problem is most
3867 usr/local/share/sdcc/lib directory
3874 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3881 Install trouble-shooting for suggestions).
3889 The final test is to ensure
3897 header files and libraries.
3898 Edit test.c and change it to the following:
3915 strcpy(str1, "testing");
3922 Compile this by typing
3929 This should generate a test.ihx output file, and it should give no warnings
3930 such as not finding the string.h file.
3931 If it cannot find the string.h file, then the problem is that
3935 cannot find the /usr/local/share/sdcc/include directory
3942 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3949 Install trouble-shooting section for suggestions).
3967 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3971 to find exactly where SDCC is looking for the include and lib files.
3974 Install Trouble-shooting
3975 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3980 \begin_inset LatexCommand \index{Install trouble-shooting}
3987 If SDCC does not build correctly
3990 A thing to try is starting from scratch by unpacking the .tgz source package
3991 again in an empty directory.
3999 ./configure 2>&1 | tee configure.log
4013 make 2>&1 | tee make.log
4020 If anything goes wrong, you can review the log files to locate the problem.
4021 Or a relevant part of this can be attached to an email that could be helpful
4022 when requesting help from the mailing list.
4026 \begin_inset Quotes sld
4030 \begin_inset Quotes srd
4037 \begin_inset Quotes sld
4041 \begin_inset Quotes srd
4044 command is a script that analyzes your system and performs some configuration
4045 to ensure the source package compiles on your system.
4046 It will take a few minutes to run, and will compile a few tests to determine
4047 what compiler features are installed.
4051 \begin_inset Quotes sld
4055 \begin_inset Quotes srd
4061 This runs the GNU make tool, which automatically compiles all the source
4062 packages into the final installed binary executables.
4066 \begin_inset Quotes sld
4070 \begin_inset Quotes erd
4076 This will install the compiler, other executables libraries and include
4077 files into the appropriate directories.
4079 \begin_inset LatexCommand \ref{sub:Install-paths}
4085 \begin_inset LatexCommand \ref{sub:Search-Paths}
4090 about install and search paths.
4092 On most systems you will need super-user privileges to do this.
4098 SDCC is not just a compiler, but a collection of tools by various developers.
4099 These include linkers, assemblers, simulators and other components.
4100 Here is a summary of some of the components.
4101 Note that the included simulator and assembler have separate documentation
4102 which you can find in the source package in their respective directories.
4103 As SDCC grows to include support for other processors, other packages from
4104 various developers are included and may have their own sets of documentation.
4108 You might want to look at the files which are installed in <installdir>.
4109 At the time of this writing, we find the following programs for gcc-builds:
4113 In <installdir>/bin:
4116 sdcc - The compiler.
4119 sdcpp - The C preprocessor.
4122 asx8051 - The assembler for 8051 type processors.
4129 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4132 aslink -The linker for 8051 type processors.
4139 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4142 s51 - The ucSim 8051 simulator.
4143 Not available on Win32 platforms.
4146 sdcdb - The source debugger.
4147 Not available on Win32 platforms.
4150 packihx - A tool to pack (compress) Intel hex files.
4153 In <installdir>/share/sdcc/include
4159 In <installdir>/share/sdcc/lib
4162 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4166 In <installdir>/share/sdcc/doc
4172 As development for other processors proceeds, this list will expand to include
4173 executables to support processors like AVR, PIC, etc.
4179 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4180 the assembler and linkage editor.
4183 sdcpp - The C-Preprocessor
4187 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4191 is a modified version of the GNU preprocessor.
4192 The C preprocessor is used to pull in #include sources, process #ifdef
4193 statements, #defines and so on.
4204 - The Assemblers and Linkage Editors
4207 This is retargettable assembler & linkage editor, it was developed by Alan
4209 John Hartman created the version for 8051, and I (Sandeep) have made some
4210 enhancements and bug fixes for it to work properly with SDCC.
4217 \begin_inset LatexCommand \index{s51}
4221 is a freeware, opensource simulator developed by Daniel Drotos.
4222 The simulator is built as part of the build process.
4223 For more information visit Daniel's web site at:
4224 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4229 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4231 S51 is currently not available on Win32 platfors.
4234 sdcdb - Source Level Debugger
4238 \begin_inset LatexCommand \index{sdcdb (debugger)}
4242 is the companion source level debugger.
4243 More about sdcdb in section
4244 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4249 The current version of the debugger uses Daniel's Simulator S51
4250 \begin_inset LatexCommand \index{s51}
4254 , but can be easily changed to use other simulators.
4255 Sdcdb is currently not available on Win32 platfors.
4264 Single Source File Projects
4267 For single source file 8051 projects the process is very simple.
4268 Compile your programs with the following command
4271 "sdcc sourcefile.c".
4275 This will compile, assemble and link your source file.
4276 Output files are as follows:
4280 \begin_inset LatexCommand \index{<file>.asm}
4285 \begin_inset LatexCommand \index{Assembler source}
4289 file created by the compiler
4293 \begin_inset LatexCommand \index{<file>.lst}
4298 \begin_inset LatexCommand \index{Assembler listing}
4302 file created by the Assembler
4306 \begin_inset LatexCommand \index{<file>.rst}
4311 \begin_inset LatexCommand \index{Assembler listing}
4315 file updated with linkedit information, created by linkage editor
4319 \begin_inset LatexCommand \index{<file>.sym}
4324 \begin_inset LatexCommand \index{Symbol listing}
4328 for the sourcefile, created by the assembler
4332 \begin_inset LatexCommand \index{<file>.rel}
4337 \begin_inset LatexCommand \index{<file>.o}
4342 \begin_inset LatexCommand \index{Object file}
4346 created by the assembler, input to Linkage editor
4350 \begin_inset LatexCommand \index{<file>.map}
4355 \begin_inset LatexCommand \index{Memory map}
4359 for the load module, created by the Linker
4363 \begin_inset LatexCommand \index{<file>.mem}
4367 - A file with a summary of the memory usage
4371 \begin_inset LatexCommand \index{<file>.ihx}
4375 - The load module in Intel hex format
4376 \begin_inset LatexCommand \index{Intel hex format}
4380 (you can select the Motorola S19 format
4381 \begin_inset LatexCommand \index{Motorola S19 format}
4396 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4401 If you need another format you might want to use
4408 \begin_inset LatexCommand \index{objdump (tool)}
4419 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4424 Both formats are documented in the documentation of srecord
4425 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4433 \begin_inset LatexCommand \index{<file>.adb}
4437 - An intermediate file containing debug information needed to create the
4449 \begin_inset LatexCommand \index{-\/-debug}
4457 \begin_inset LatexCommand \index{<file>.cdb}
4461 - An optional file (with -
4471 -debug) containing debug information.
4472 The format is documented in cdbfileformat.pdf
4477 \begin_inset LatexCommand \index{<file> (no extension)}
4481 An optional AOMF or AOMF51
4482 \begin_inset LatexCommand \index{AOMF, AOMF51}
4486 file containing debug information (generated with option -
4513 ormat is commonly used by third party tools (debuggers
4514 \begin_inset LatexCommand \index{Debugger}
4518 , simulators, emulators)
4522 \begin_inset LatexCommand \index{<file>.dump*}
4526 - Dump file to debug the compiler it self (generated with option -
4536 -dumpall) (see section
4537 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4543 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4549 \begin_inset Quotes sld
4552 Anatomy of the compiler
4553 \begin_inset Quotes srd
4559 Projects with Multiple Source Files
4562 SDCC can compile only ONE file at a time.
4563 Let us for example assume that you have a project containing the following
4568 foo1.c (contains some functions)
4570 foo2.c (contains some more functions)
4572 foomain.c (contains more functions and the function main)
4580 The first two files will need to be compiled separately with the commands:
4612 Then compile the source file containing the
4617 \begin_inset LatexCommand \index{Linker}
4621 the files together with the following command:
4629 foomain.c\SpecialChar ~
4630 foo1.rel\SpecialChar ~
4635 \begin_inset LatexCommand \index{<file>.rel}
4647 can be separately compiled as well:
4658 sdcc foomain.rel foo1.rel foo2.rel
4665 The file containing the
4680 file specified in the command line, since the linkage editor processes
4681 file in the order they are presented to it.
4682 The linker is invoked from SDCC using a script file with extension .lnk
4683 \begin_inset LatexCommand \index{<file>.lnk}
4688 You can view this file to troubleshoot linking problems such as those arising
4689 from missing libraries.
4692 Projects with Additional Libraries
4693 \begin_inset LatexCommand \index{Libraries}
4700 Some reusable routines may be compiled into a library, see the documentation
4701 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4705 \begin_inset LatexCommand \index{<file>.lib}
4712 Libraries created in this manner can be included in the command line.
4713 Make sure you include the -L <library-path> option to tell the linker where
4714 to look for these files if they are not in the current directory.
4715 Here is an example, assuming you have the source file
4727 (if that is not the same as your current project):
4734 sdcc foomain.c foolib.lib -L mylib
4745 must be an absolute path name.
4749 The most efficient way to use libraries is to keep separate modules in separate
4751 The lib file now should name all the modules.rel
4752 \begin_inset LatexCommand \index{<file>.rel}
4757 For an example see the standard library file
4761 in the directory <installdir>/share/lib/small.
4764 Using sdcclib to Create and Manage Libraries
4765 \begin_inset LatexCommand \index{sdcclib}
4772 Alternatively, instead of having a .rel file for each entry on the library
4773 file as described in the preceding section, sdcclib can be used to embed
4774 all the modules belonging to such library in the library file itself.
4775 This results in a larger library file, but it greatly reduces the number
4776 of disk files accessed by the linker.
4777 Additionally, the packed library file contains an index of all include
4778 modules and symbols that significantly speeds up the linking process.
4779 To display a list of options supported by sdcclib type:
4788 \begin_inset LatexCommand \index{sdcclib}
4799 To create a new library file, start by compiling all the required modules.
4837 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4839 The next step is to add the .rel files to the library file:
4847 sdcclib libint.lib _divsint.rel
4850 \begin_inset LatexCommand \index{sdcclib}
4860 sdcclib libint.lib _divuint.rel
4866 sdcclib libint.lib _modsint.rel
4872 sdcclib libint.lib _moduint.rel
4878 sdcclib libint.lib _mulint.rel
4885 If the file already exists in the library, it will be replaced.
4886 To see what modules and symbols are included in the library, options -s
4887 and -m are available.
4895 sdcclib -s libint.lib
4898 \begin_inset LatexCommand \index{sdcclib}
5008 If the source files are compiled using -
5019 \begin_inset LatexCommand \index{-\/-debug}
5023 , the corresponding debug information file .adb will be include in the library
5025 The library files created with sdcclib are plain text files, so they can
5026 be viewed with a text editor.
5027 It is not recomended to modify a library file created with sdcclib using
5028 a text editor, as there are file indexes numbers located accross the file
5029 used by the linker to quickly locate the required module to link.
5030 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5031 it can be safely deleted, since all the information required for linking
5032 is embedded in the library file itself.
5033 Library files created using sdcclib are used as described in the preceding
5037 Command Line Options
5038 \begin_inset LatexCommand \index{Command Line Options}
5045 Processor Selection Options
5046 \begin_inset LatexCommand \index{Options processor selection}
5051 \begin_inset LatexCommand \index{Processor selection options}
5057 \labelwidthstring 00.00.0000
5062 \begin_inset LatexCommand \index{-mmcs51}
5068 Generate code for the Intel MCS51
5069 \begin_inset LatexCommand \index{MCS51}
5073 family of processors.
5074 This is the default processor target.
5076 \labelwidthstring 00.00.0000
5081 \begin_inset LatexCommand \index{-mds390}
5087 Generate code for the Dallas DS80C390
5088 \begin_inset LatexCommand \index{DS80C390}
5094 \labelwidthstring 00.00.0000
5099 \begin_inset LatexCommand \index{-mds400}
5105 Generate code for the Dallas DS80C400
5106 \begin_inset LatexCommand \index{DS80C400}
5112 \labelwidthstring 00.00.0000
5117 \begin_inset LatexCommand \index{-mhc08}
5123 Generate code for the Freescale/Motorola HC08
5124 \begin_inset LatexCommand \index{HC08}
5128 family of processors.
5130 \labelwidthstring 00.00.0000
5135 \begin_inset LatexCommand \index{-mz80}
5141 Generate code for the Zilog Z80
5142 \begin_inset LatexCommand \index{Z80}
5146 family of processors.
5148 \labelwidthstring 00.00.0000
5153 \begin_inset LatexCommand \index{-mgbz80}
5159 Generate code for the GameBoy Z80
5160 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5164 processor (Not actively maintained).
5166 \labelwidthstring 00.00.0000
5171 \begin_inset LatexCommand \index{-mavr}
5177 Generate code for the Atmel AVR
5178 \begin_inset LatexCommand \index{AVR}
5182 processor (In development, not complete).
5183 AVR users should probably have a look at winavr
5184 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5189 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5196 I think it is fair to direct users there for now.
5197 Open source is also about avoiding unnecessary work .
5198 But I didn't find the 'official' link.
5200 \labelwidthstring 00.00.0000
5205 \begin_inset LatexCommand \index{-mpic14}
5211 Generate code for the Microchip PIC 14
5212 \begin_inset LatexCommand \index{PIC14}
5216 -bit processors (p16f84 and variants.
5217 In development, not complete).
5220 p16f627 p16f628 p16f84 p16f873 p16f877?
5222 \labelwidthstring 00.00.0000
5227 \begin_inset LatexCommand \index{-mpic16}
5233 Generate code for the Microchip PIC 16
5234 \begin_inset LatexCommand \index{PIC16}
5238 -bit processors (p18f452 and variants.
5239 In development, not complete).
5241 \labelwidthstring 00.00.0000
5247 Generate code for the Toshiba TLCS-900H
5248 \begin_inset LatexCommand \index{TLCS-900H}
5252 processor (Not maintained, not complete).
5254 \labelwidthstring 00.00.0000
5259 \begin_inset LatexCommand \index{-mxa51}
5265 Generate code for the Phillips XA51
5266 \begin_inset LatexCommand \index{XA51}
5270 processor (Not maintained, not complete).
5273 Preprocessor Options
5274 \begin_inset LatexCommand \index{Options preprocessor}
5279 \begin_inset LatexCommand \index{Preprocessor options}
5284 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5290 \labelwidthstring 00.00.0000
5295 \begin_inset LatexCommand \index{-I<path>}
5301 The additional location where the pre processor will look for <..h> or
5302 \begin_inset Quotes eld
5306 \begin_inset Quotes erd
5311 \labelwidthstring 00.00.0000
5316 \begin_inset LatexCommand \index{-D<macro[=value]>}
5322 Command line definition of macros.
5323 Passed to the preprocessor.
5325 \labelwidthstring 00.00.0000
5330 \begin_inset LatexCommand \index{-M}
5336 Tell the preprocessor to output a rule suitable for make describing the
5337 dependencies of each object file.
5338 For each source file, the preprocessor outputs one make-rule whose target
5339 is the object file name for that source file and whose dependencies are
5340 all the files `#include'd in it.
5341 This rule may be a single line or may be continued with `
5343 '-newline if it is long.
5344 The list of rules is printed on standard output instead of the preprocessed
5347 \begin_inset LatexCommand \index{-E}
5353 \labelwidthstring 00.00.0000
5358 \begin_inset LatexCommand \index{-C}
5364 Tell the preprocessor not to discard comments.
5365 Used with the `-E' option.
5367 \labelwidthstring 00.00.0000
5372 \begin_inset LatexCommand \index{-MM}
5383 Like `-M' but the output mentions only the user header files included with
5385 \begin_inset Quotes eld
5389 System header files included with `#include <file>' are omitted.
5391 \labelwidthstring 00.00.0000
5396 \begin_inset LatexCommand \index{-Aquestion(answer)}
5402 Assert the answer answer for question, in case it is tested with a preprocessor
5403 conditional such as `#if #question(answer)'.
5404 `-A-' disables the standard assertions that normally describe the target
5407 \labelwidthstring 00.00.0000
5412 \begin_inset LatexCommand \index{-Umacro}
5418 Undefine macro macro.
5419 `-U' options are evaluated after all `-D' options, but before any `-include'
5420 and `-imacros' options.
5422 \labelwidthstring 00.00.0000
5427 \begin_inset LatexCommand \index{-dM}
5433 Tell the preprocessor to output only a list of the macro definitions that
5434 are in effect at the end of preprocessing.
5435 Used with the `-E' option.
5437 \labelwidthstring 00.00.0000
5442 \begin_inset LatexCommand \index{-dD}
5448 Tell the preprocessor to pass all macro definitions into the output, in
5449 their proper sequence in the rest of the output.
5451 \labelwidthstring 00.00.0000
5456 \begin_inset LatexCommand \index{-dN}
5467 Like `-dD' except that the macro arguments and contents are omitted.
5468 Only `#define name' is included in the output.
5470 \labelwidthstring 00.00.0000
5475 preprocessorOption[,preprocessorOption]
5478 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5483 Pass the preprocessorOption to the preprocessor
5488 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5493 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5494 Collection (gcc), if you need more dedicated options please refer to the
5496 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5504 \begin_inset LatexCommand \index{Options linker}
5509 \begin_inset LatexCommand \index{Linker options}
5515 \labelwidthstring 00.00.0000
5535 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5540 \begin_inset LatexCommand \index{-L -\/-lib-path}
5547 <absolute path to additional libraries> This option is passed to the linkage
5548 editor's additional libraries
5549 \begin_inset LatexCommand \index{Libraries}
5554 The path name must be absolute.
5555 Additional library files may be specified in the command line.
5556 See section Compiling programs for more details.
5558 \labelwidthstring 00.00.0000
5575 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5580 <Value> The start location of the external ram
5581 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5585 , default value is 0.
5586 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5596 -xram-loc 0x8000 or -
5608 \labelwidthstring 00.00.0000
5625 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5630 <Value> The start location of the code
5631 \begin_inset LatexCommand \index{code}
5635 segment, default value 0.
5636 Note when this option is used the interrupt vector table is also relocated
5637 to the given address.
5638 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5648 -code-loc 0x8000 or -
5660 \labelwidthstring 00.00.0000
5677 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5682 <Value> By default the stack
5683 \begin_inset LatexCommand \index{stack}
5687 is placed after the data segment.
5688 Using this option the stack can be placed anywhere in the internal memory
5690 The value entered can be in Hexadecimal or Decimal format, e.g.
5701 -stack-loc 0x20 or -
5712 Since the sp register is incremented before a push or call, the initial
5713 sp will be set to one byte prior the provided value.
5714 The provided value should not overlap any other memory areas such as used
5715 register banks or the data segment and with enough space for the current
5733 \begin_inset LatexCommand \index{-\/-pack-iram}
5737 option (which is now a default setting) will override this setting, so
5738 you should also specify the
5754 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5758 option if you need to manually place the stack.
5760 \labelwidthstring 00.00.0000
5777 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
5782 <Value> By default the external stack
5783 \begin_inset LatexCommand \index{xstack}
5787 is placed after the pdata segment.
5788 Using this option the xstack can be placed anywhere in the external memory
5790 The value entered can be in Hexadecimal or Decimal format, e.g.
5801 -xstack-loc 0x8000 or -
5812 The provided value should not overlap any other memory areas such as the
5813 pdata or xdata segment and with enough space for the current application.
5815 \labelwidthstring 00.00.0000
5832 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5837 <Value> The start location of the internal ram data
5838 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5843 The value entered can be in Hexadecimal or Decimal format, eg.
5865 (By default, the start location of the internal ram data segment is set
5866 as low as possible in memory, taking into account the used register banks
5867 and the bit segment at address 0x20.
5868 For example if register banks 0 and 1 are used without bit variables, the
5869 data segment will be set, if -
5879 -data-loc is not used, to location 0x10.)
5881 \labelwidthstring 00.00.0000
5898 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5903 <Value> The start location of the indirectly addressable internal ram
5904 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5908 of the 8051, default value is 0x80.
5909 The value entered can be in Hexadecimal or Decimal format, eg.
5920 -idata-loc 0x88 or -
5932 \labelwidthstring 00.00.0000
5949 <Value> The start location of the bit
5950 \begin_inset LatexCommand \index{bit}
5954 addressable internal ram of the 8051.
5960 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5963 \labelwidthstring 00.00.0000
5978 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5987 The linker output (final object code) is in Intel Hex format.
5988 \begin_inset LatexCommand \index{Intel hex format}
5992 This is the default option.
5993 The format itself is documented in the documentation of srecord
5994 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6000 \labelwidthstring 00.00.0000
6015 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6024 The linker output (final object code) is in Motorola S19 format
6025 \begin_inset LatexCommand \index{Motorola S19 format}
6030 The format itself is documented in the documentation of srecord.
6032 \labelwidthstring 00.00.0000
6047 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6056 The linker output (final object code) is in ELF format
6057 \begin_inset LatexCommand \index{ELF format}
6062 (Currently only supported for the HC08 processors)
6064 \labelwidthstring 00.00.0000
6069 linkOption[,linkOption]
6072 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6077 Pass the linkOption to the linker.
6078 See file sdcc/as/doc/asxhtm.html for more on linker options.
6082 \begin_inset LatexCommand \index{Options MCS51}
6087 \begin_inset LatexCommand \index{MCS51 options}
6093 \labelwidthstring 00.00.0000
6108 \begin_inset LatexCommand \index{-\/-model-small}
6119 Generate code for Small Model programs, see section Memory Models for more
6121 This is the default model.
6123 \labelwidthstring 00.00.0000
6138 \begin_inset LatexCommand \index{-\/-model-medium}
6144 Generate code for Medium model programs, see section Memory Models for
6146 If this option is used all source files in the project have to be compiled
6148 It must also be used when invoking the linker.
6150 \labelwidthstring 00.00.0000
6165 \begin_inset LatexCommand \index{-\/-model-large}
6171 Generate code for Large model programs, see section Memory Models for more
6173 If this option is used all source files in the project have to be compiled
6175 It must also be used when invoking the linker.
6177 \labelwidthstring 00.00.0000
6192 \begin_inset LatexCommand \index{-\/-xstack}
6198 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6199 variables and passing parameters.
6201 \begin_inset LatexCommand \ref{sub:External-Stack}
6206 External Stack for more details.
6208 \labelwidthstring 00.00.0000
6226 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6230 Causes the linker to check if the internal ram usage is within limits of
6233 \labelwidthstring 00.00.0000
6251 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6255 Causes the linker to check if the external ram usage is within limits of
6258 \labelwidthstring 00.00.0000
6276 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6280 Causes the linker to check if the code memory usage is within limits of
6283 \labelwidthstring 00.00.0000
6301 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6305 Causes the linker to check if there is at minimum <Value> bytes for stack.
6307 \labelwidthstring 00.00.0000
6325 \begin_inset LatexCommand \index{-\/-pack-iram}
6329 Causes the linker to use unused register banks for data variables and pack
6330 data, idata and stack together.
6331 This is the default now.
6333 \labelwidthstring 00.00.0000
6351 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6355 Causes the linker to use old style for allocating memory areas.
6358 DS390 / DS400 Options
6359 \begin_inset LatexCommand \index{Options DS390}
6364 \begin_inset LatexCommand \index{DS390 options}
6370 \labelwidthstring 00.00.0000
6387 \begin_inset LatexCommand \index{-\/-model-flat24}
6397 Generate 24-bit flat mode code.
6398 This is the one and only that the ds390 code generator supports right now
6399 and is default when using
6404 See section Memory Models for more details.
6406 \labelwidthstring 00.00.0000
6421 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6427 disable interrupts during ESP:SP updates.
6429 \labelwidthstring 00.00.0000
6446 \begin_inset LatexCommand \index{-\/-stack-10bit}
6450 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6451 This is the one and only that the ds390 code generator supports right now
6452 and is default when using
6457 In this mode, the stack is located in the lower 1K of the internal RAM,
6458 which is mapped to 0x400000.
6459 Note that the support is incomplete, since it still uses a single byte
6460 as the stack pointer.
6461 This means that only the lower 256 bytes of the potential 1K stack space
6462 will actually be used.
6463 However, this does allow you to reclaim the precious 256 bytes of low RAM
6464 for use for the DATA and IDATA segments.
6465 The compiler will not generate any code to put the processor into 10 bit
6467 It is important to ensure that the processor is in this mode before calling
6468 any re-entrant functions compiled with this option.
6469 In principle, this should work with the
6482 \begin_inset LatexCommand \index{-\/-stack-auto}
6488 option, but that has not been tested.
6489 It is incompatible with the
6502 \begin_inset LatexCommand \index{-\/-xstack}
6509 It also only makes sense if the processor is in 24 bit contiguous addressing
6522 -model-flat24 option
6526 \labelwidthstring 00.00.0000
6541 \begin_inset LatexCommand \index{-\/-stack-probe}
6547 insert call to function __stack_probe at each function prologue.
6549 \labelwidthstring 00.00.0000
6564 \begin_inset LatexCommand \index{-\/-tini-libid}
6570 <nnnn> LibraryID used in -mTININative.
6573 \labelwidthstring 00.00.0000
6588 \begin_inset LatexCommand \index{-\/-use-accelerator}
6594 generate code for DS390 Arithmetic Accelerator.
6599 \begin_inset LatexCommand \index{Options Z80}
6604 \begin_inset LatexCommand \index{Z80 options}
6610 \labelwidthstring 00.00.0000
6627 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6637 Force a called function to always save BC.
6639 \labelwidthstring 00.00.0000
6656 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6660 When linking, skip the standard crt0.o object file.
6661 You must provide your own crt0.o for your system when linking.
6665 Optimization Options
6666 \begin_inset LatexCommand \index{Options optimization}
6671 \begin_inset LatexCommand \index{Optimization options}
6677 \labelwidthstring 00.00.0000
6692 \begin_inset LatexCommand \index{-\/-nogcse}
6698 Will not do global subexpression elimination, this option may be used when
6699 the compiler creates undesirably large stack/data spaces to store compiler
6709 \begin_inset LatexCommand \index{sloc (spill location)}
6714 A warning message will be generated when this happens and the compiler
6715 will indicate the number of extra bytes it allocated.
6716 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6718 \begin_inset LatexCommand \index{\#pragma nogcse}
6722 can be used to turn off global subexpression elimination
6723 \begin_inset LatexCommand \index{Subexpression elimination}
6727 for a given function only.
6729 \labelwidthstring 00.00.0000
6744 \begin_inset LatexCommand \index{-\/-noinvariant}
6750 Will not do loop invariant optimizations, this may be turned off for reasons
6751 explained for the previous option.
6752 For more details of loop optimizations performed see Loop Invariants in
6754 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6759 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6761 \begin_inset LatexCommand \index{\#pragma noinvariant}
6765 can be used to turn off invariant optimizations for a given function only.
6767 \labelwidthstring 00.00.0000
6782 \begin_inset LatexCommand \index{-\/-noinduction}
6788 Will not do loop induction optimizations, see section strength reduction
6790 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6792 \begin_inset LatexCommand \index{\#pragma noinduction}
6796 can be used to turn off induction optimizations for a given function only.
6798 \labelwidthstring 00.00.0000
6813 \begin_inset LatexCommand \index{-\/-nojtbound}
6824 Will not generate boundary condition check when switch statements
6825 \begin_inset LatexCommand \index{switch statement}
6829 are implemented using jump-tables.
6831 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6836 Switch Statements for more details.
6837 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6839 \begin_inset LatexCommand \index{\#pragma nojtbound}
6843 can be used to turn off boundary checking for jump tables for a given function
6846 \labelwidthstring 00.00.0000
6861 \begin_inset LatexCommand \index{-\/-noloopreverse}
6870 Will not do loop reversal
6871 \begin_inset LatexCommand \index{Loop reversing}
6877 \labelwidthstring 00.00.0000
6894 \begin_inset LatexCommand \index{-\/-nolabelopt }
6898 Will not optimize labels (makes the dumpfiles more readable).
6900 \labelwidthstring 00.00.0000
6915 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6921 Will not memcpy initialized data from code space into xdata space.
6922 This saves a few bytes in code space if you don't have initialized data
6923 \begin_inset LatexCommand \index{Variable initialization}
6929 \labelwidthstring 00.00.0000
6944 \begin_inset LatexCommand \index{-\/-nooverlay}
6950 The compiler will not overlay parameters and local variables of any function,
6951 see section Parameters and local variables for more details.
6953 \labelwidthstring 00.00.0000
6968 \begin_inset LatexCommand \index{-\/-no-peep}
6974 Disable peep-hole optimization.
6976 \labelwidthstring 00.00.0000
6993 \begin_inset LatexCommand \index{-\/-peep-file}
6998 <filename> This option can be used to use additional rules to be used by
6999 the peep hole optimizer.
7001 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7006 Peep Hole optimizations for details on how to write these rules.
7008 \labelwidthstring 00.00.0000
7023 \begin_inset LatexCommand \index{-\/-peep-asm}
7029 Pass the inline assembler code through the peep hole optimizer.
7030 This can cause unexpected changes to inline assembler code, please go through
7031 the peephole optimizer
7032 \begin_inset LatexCommand \index{Peephole optimizer}
7036 rules defined in the source file tree '<target>/peeph.def' before using
7039 \labelwidthstring 00.00.0000
7054 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7060 The compiler will optimize code generation towards fast code, possibly
7061 at the expense of code size.
7063 \labelwidthstring 00.00.0000
7078 \begin_inset LatexCommand \index{-\/-opt-code-size}
7084 The compiler will optimize code generation towards compact code, possibly
7085 at the expense of code speed.
7089 \begin_inset LatexCommand \index{Options other}
7095 \labelwidthstring 00.00.0000
7111 \begin_inset LatexCommand \index{-\/-compile-only}
7116 \begin_inset LatexCommand \index{-c -\/-compile-only}
7122 will compile and assemble the source, but will not call the linkage editor.
7124 \labelwidthstring 00.00.0000
7143 \begin_inset LatexCommand \index{-\/-c1mode}
7149 reads the preprocessed source from standard input and compiles it.
7150 The file name for the assembler output must be specified using the -o option.
7152 \labelwidthstring 00.00.0000
7157 \begin_inset LatexCommand \index{-E}
7163 Run only the C preprocessor.
7164 Preprocess all the C source files specified and output the results to standard
7167 \labelwidthstring 00.00.0000
7173 \begin_inset LatexCommand \index{-o <path/file>}
7179 The output path resp.
7180 file where everything will be placed.
7181 If the parameter is a path, it must have a trailing slash (or backslash
7182 for the Windows binaries) to be recognized as a path.
7185 \labelwidthstring 00.00.0000
7200 \begin_inset LatexCommand \index{-\/-stack-auto}
7211 All functions in the source file will be compiled as
7216 \begin_inset LatexCommand \index{reentrant}
7221 the parameters and local variables will be allocated on the stack
7222 \begin_inset LatexCommand \index{stack}
7228 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7232 Parameters and Local Variables for more details.
7233 If this option is used all source files in the project should be compiled
7235 It automatically implies --int-long-reent and --float-reent.
7238 \labelwidthstring 00.00.0000
7253 \begin_inset LatexCommand \index{-\/-callee-saves}
7257 function1[,function2][,function3]....
7260 The compiler by default uses a caller saves convention for register saving
7261 across function calls, however this can cause unnecessary register pushing
7262 & popping when calling small functions from larger functions.
7263 This option can be used to switch the register saving convention for the
7264 function names specified.
7265 The compiler will not save registers when calling these functions, no extra
7266 code will be generated at the entry & exit (function prologue
7269 \begin_inset LatexCommand \index{function prologue}
7278 \begin_inset LatexCommand \index{function epilogue}
7284 ) for these functions to save & restore the registers used by these functions,
7285 this can SUBSTANTIALLY reduce code & improve run time performance of the
7287 In the future the compiler (with inter procedural analysis) will be able
7288 to determine the appropriate scheme to use for each function call.
7289 DO NOT use this option for built-in functions such as _mulint..., if this
7290 option is used for a library function the appropriate library function
7291 needs to be recompiled with the same option.
7292 If the project consists of multiple source files then all the source file
7293 should be compiled with the same -
7303 -callee-saves option string.
7304 Also see #pragma\SpecialChar ~
7306 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7312 \labelwidthstring 00.00.0000
7327 \begin_inset LatexCommand \index{-\/-debug}
7336 When this option is used the compiler will generate debug information.
7337 The debug information collected in a file with .cdb extension can be used
7339 For more information see documentation for SDCDB.
7340 Another file with no extension contains debug information in AOMF or AOMF51
7341 \begin_inset LatexCommand \index{AOMF, AOMF51}
7345 format which is commonly used by third party tools.
7347 \labelwidthstring 00.00.0000
7352 \begin_inset LatexCommand \index{-S}
7363 Stop after the stage of compilation proper; do not assemble.
7364 The output is an assembler code file for the input file specified.
7366 \labelwidthstring 00.00.0000
7381 \begin_inset LatexCommand \index{-\/-int-long-reent}
7387 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7388 Note by default these libraries are compiled as non-reentrant.
7389 See section Installation for more details.
7391 \labelwidthstring 00.00.0000
7406 \begin_inset LatexCommand \index{-\/-cyclomatic}
7415 This option will cause the compiler to generate an information message for
7416 each function in the source file.
7417 The message contains some
7421 information about the function.
7422 The number of edges and nodes the compiler detected in the control flow
7423 graph of the function, and most importantly the
7425 cyclomatic complexity
7426 \begin_inset LatexCommand \index{Cyclomatic complexity}
7432 see section on Cyclomatic Complexity for more details.
7434 \labelwidthstring 00.00.0000
7449 \begin_inset LatexCommand \index{-\/-float-reent}
7455 Floating point library is compiled as reentrant
7456 \begin_inset LatexCommand \index{reentrant}
7461 See section Installation for more details.
7463 \labelwidthstring 00.00.0000
7478 \begin_inset LatexCommand \index{-\/-main-return}
7484 This option can be used if the code generated is called by a monitor program
7485 or if the main routine includes an endless loop.
7486 This option might result in slightly smaller code and save two bytes of
7488 The return from the 'main'
7489 \begin_inset LatexCommand \index{main return}
7493 function will return to the function calling main.
7494 The default setting is to lock up i.e.
7501 \labelwidthstring 00.00.0000
7516 \begin_inset LatexCommand \index{-\/-nostdinc}
7522 This will prevent the compiler from passing on the default include path
7523 to the preprocessor.
7525 \labelwidthstring 00.00.0000
7540 \begin_inset LatexCommand \index{-\/-nostdlib}
7546 This will prevent the compiler from passing on the default library
7547 \begin_inset LatexCommand \index{Libraries}
7553 \labelwidthstring 00.00.0000
7568 \begin_inset LatexCommand \index{-\/-verbose}
7574 Shows the various actions the compiler is performing.
7576 \labelwidthstring 00.00.0000
7581 \begin_inset LatexCommand \index{-V}
7587 Shows the actual commands the compiler is executing.
7589 \labelwidthstring 00.00.0000
7604 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7610 Hides your ugly and inefficient c-code from the asm file, so you can always
7611 blame the compiler :)
7613 \labelwidthstring 00.00.0000
7628 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7634 Will not include peep-hole comments in the generated files.
7636 \labelwidthstring 00.00.0000
7651 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7657 Include i-codes in the asm file.
7658 Sounds like noise but is most helpful for debugging the compiler itself.
7660 \labelwidthstring 00.00.0000
7675 \begin_inset LatexCommand \index{-\/-less-pedantic}
7681 Disable some of the more pedantic warnings
7682 \begin_inset LatexCommand \index{Warnings}
7686 (jwk burps: please be more specific here, please!).
7688 \labelwidthstring 00.00.0000
7702 -disable-warning\SpecialChar ~
7704 \begin_inset LatexCommand \index{-\/-disable-warning}
7710 Disable specific warning with number <nnnn>.
7712 \labelwidthstring 00.00.0000
7727 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7733 Display the directories in the compiler's search path
7735 \labelwidthstring 00.00.0000
7750 \begin_inset LatexCommand \index{-\/-vc}
7756 Display errors and warnings using MSVC style, so you can use SDCC with
7759 \labelwidthstring 00.00.0000
7774 \begin_inset LatexCommand \index{-\/-use-stdout}
7780 Send errors and warnings to stdout instead of stderr.
7782 \labelwidthstring 00.00.0000
7787 asmOption[,asmOption]
7790 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7795 Pass the asmOption to the assembler
7796 \begin_inset LatexCommand \index{Options assembler}
7801 \begin_inset LatexCommand \index{Assembler options}
7806 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7808 \labelwidthstring 00.00.0000
7823 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7829 Generally follow the C89 standard, but allow SDCC features that conflict
7830 with the standard (default).
7832 \labelwidthstring 00.00.0000
7847 \begin_inset LatexCommand \index{-\/-std-c89}
7853 Follow the C89 standard and disable SDCC features that conflict with the
7856 \labelwidthstring 00.00.0000
7871 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7877 Generally follow the C99 standard, but allow SDCC features that conflict
7878 with the standard (incomplete support).
7880 \labelwidthstring 00.00.0000
7895 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7901 Follow the C99 standard and disable SDCC features that conflict with the
7902 standard (incomplete support).
7904 \labelwidthstring 00.00.0000
7921 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
7926 <Name> The name to be used for the code
7927 \begin_inset LatexCommand \index{code}
7931 segment, default CSEG.
7932 This is useful if you need to tell the compiler to put the code in a special
7933 segment so you can later on tell the linker to put this segment in a special
7935 Can be used for instance when using bank switching to put the code in a
7938 \labelwidthstring 00.00.0000
7955 \begin_inset LatexCommand \index{-\/-constseg <Value>}
7960 <Name> The name to be used for the const
7961 \begin_inset LatexCommand \index{code}
7965 segment, default CONST.
7966 This is useful if you need to tell the compiler to put the const data in
7967 a special segment so you can later on tell the linker to put this segment
7968 in a special place in memory.
7969 Can be used for instance when using bank switching to put the const data
7972 \labelwidthstring 00.00.0000
7984 a SDCC compiler option but if you want
7988 warnings you can use a separate tool dedicated to syntax checking like
7990 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
7995 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8000 \begin_inset LatexCommand \url{http://www.splint.org}
8005 To make your source files parseable by splint you will have to include
8011 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8015 in your source file and add brackets around extended keywords (like
8018 \begin_inset Quotes sld
8031 \begin_inset Quotes srd
8039 \begin_inset Quotes sld
8042 __interrupt\SpecialChar ~
8044 \begin_inset Quotes srd
8052 Splint has an excellent on line manual at
8053 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8057 and it's capabilities go beyond pure syntax checking.
8058 You'll need to tell splint the location of SDCC's include files so a typical
8059 command line could look like this:
8063 splint\SpecialChar ~
8065 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8070 Intermediate Dump Options
8071 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8076 \begin_inset LatexCommand \index{Options intermediate dump}
8081 \begin_inset LatexCommand \index{Intermediate dump options}
8088 The following options are provided for the purpose of retargetting and debugging
8090 They provide a means to dump the intermediate code (iCode
8091 \begin_inset LatexCommand \index{iCode}
8095 ) generated by the compiler in human readable form at various stages of
8096 the compilation process.
8097 More on iCodes see chapter
8098 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8103 \begin_inset Quotes srd
8106 The anatomy of the compiler
8107 \begin_inset Quotes srd
8112 \labelwidthstring 00.00.0000
8127 \begin_inset LatexCommand \index{-\/-dumpraw}
8133 This option will cause the compiler to dump the intermediate code into
8136 <source filename>.dumpraw
8138 just after the intermediate code has been generated for a function, i.e.
8139 before any optimizations are done.
8141 \begin_inset LatexCommand \index{Basic blocks}
8145 at this stage ordered in the depth first number, so they may not be in
8146 sequence of execution.
8148 \labelwidthstring 00.00.0000
8163 \begin_inset LatexCommand \index{-\/-dumpgcse}
8169 Will create a dump of iCode's, after global subexpression elimination
8170 \begin_inset LatexCommand \index{Global subexpression elimination}
8176 <source filename>.dumpgcse.
8178 \labelwidthstring 00.00.0000
8193 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8199 Will create a dump of iCode's, after deadcode elimination
8200 \begin_inset LatexCommand \index{Dead-code elimination}
8206 <source filename>.dumpdeadcode.
8208 \labelwidthstring 00.00.0000
8223 \begin_inset LatexCommand \index{-\/-dumploop}
8232 Will create a dump of iCode's, after loop optimizations
8233 \begin_inset LatexCommand \index{Loop optimization}
8239 <source filename>.dumploop.
8241 \labelwidthstring 00.00.0000
8256 \begin_inset LatexCommand \index{-\/-dumprange}
8265 Will create a dump of iCode's, after live range analysis
8266 \begin_inset LatexCommand \index{Live range analysis}
8272 <source filename>.dumprange.
8274 \labelwidthstring 00.00.0000
8289 \begin_inset LatexCommand \index{-\/-dumlrange}
8295 Will dump the life ranges
8296 \begin_inset LatexCommand \index{Live range analysis}
8302 \labelwidthstring 00.00.0000
8317 \begin_inset LatexCommand \index{-\/-dumpregassign}
8326 Will create a dump of iCode's, after register assignment
8327 \begin_inset LatexCommand \index{Register assignment}
8333 <source filename>.dumprassgn.
8335 \labelwidthstring 00.00.0000
8350 \begin_inset LatexCommand \index{-\/-dumplrange}
8356 Will create a dump of the live ranges of iTemp's
8358 \labelwidthstring 00.00.0000
8373 \begin_inset LatexCommand \index{-\/-dumpall}
8384 Will cause all the above mentioned dumps to be created.
8387 Redirecting output on Windows Shells
8390 By default SDCC writes it's error messages to
8391 \begin_inset Quotes sld
8395 \begin_inset Quotes srd
8399 To force all messages to
8400 \begin_inset Quotes sld
8404 \begin_inset Quotes srd
8428 \begin_inset LatexCommand \index{-\/-use-stdout}
8433 Additionally, if you happen to have visual studio installed in your windows
8434 machine, you can use it to compile your sources using a custom build and
8450 \begin_inset LatexCommand \index{-\/-vc}
8455 Something like this should work:
8499 -model-large -c $(InputPath)
8502 Environment variables
8503 \begin_inset LatexCommand \index{Environment variables}
8510 SDCC recognizes the following environment variables:
8512 \labelwidthstring 00.00.0000
8517 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8523 SDCC installs a signal handler
8524 \begin_inset LatexCommand \index{signal handler}
8528 to be able to delete temporary files after an user break (^C) or an exception.
8529 If this environment variable is set, SDCC won't install the signal handler
8530 in order to be able to debug SDCC.
8532 \labelwidthstring 00.00.0000
8539 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8545 Path, where temporary files will be created.
8546 The order of the variables is the search order.
8547 In a standard *nix environment these variables are not set, and there's
8548 no need to set them.
8549 On Windows it's recommended to set one of them.
8551 \labelwidthstring 00.00.0000
8556 \begin_inset LatexCommand \index{SDCC\_HOME}
8563 \begin_inset LatexCommand \ref{sub:Install-paths}
8569 \begin_inset Quotes sld
8573 \begin_inset Quotes srd
8578 \labelwidthstring 00.00.0000
8583 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8590 \begin_inset LatexCommand \ref{sub:Search-Paths}
8596 \begin_inset Quotes sld
8600 \begin_inset Quotes srd
8605 \labelwidthstring 00.00.0000
8610 \begin_inset LatexCommand \index{SDCC\_LIB}
8617 \begin_inset LatexCommand \ref{sub:Search-Paths}
8623 \begin_inset Quotes sld
8627 \begin_inset Quotes srd
8633 There are some more environment variables recognized by SDCC, but these
8634 are solely used for debugging purposes.
8635 They can change or disappear very quickly, and will never be documented.
8638 Storage Class Language Extensions
8641 MCS51/DS390 Storage Class
8642 \begin_inset LatexCommand \index{Storage class}
8649 In addition to the ANSI storage classes SDCC allows the following MCS51
8650 specific storage classes:
8651 \layout Subsubsection
8654 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8659 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8664 \begin_inset LatexCommand \index{near (storage class)}
8669 \begin_inset LatexCommand \index{\_\_near (storage class)}
8680 storage class for the Small Memory model (
8688 can be used synonymously).
8689 Variables declared with this storage class will be allocated in the directly
8690 addressable portion of the internal RAM of a 8051, e.g.:
8695 data unsigned char test_data;
8698 Writing 0x01 to this variable generates the assembly code:
8703 75*00 01\SpecialChar ~
8709 \layout Subsubsection
8712 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8717 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8722 \begin_inset LatexCommand \index{far (storage class)}
8727 \begin_inset LatexCommand \index{\_\_far (storage class)}
8734 Variables declared with this storage class will be placed in the external
8740 storage class for the Large Memory model, e.g.:
8745 xdata unsigned char test_xdata;
8748 Writing 0x01 to this variable generates the assembly code:
8753 90s00r00\SpecialChar ~
8782 \layout Subsubsection
8785 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8790 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8797 Variables declared with this storage class will be allocated into the indirectly
8798 addressable portion of the internal ram of a 8051, e.g.:
8803 idata unsigned char test_idata;
8806 Writing 0x01 to this variable generates the assembly code:
8835 Please note, the first 128 byte of idata physically access the same RAM
8837 The original 8051 had 128 byte idata memory, nowadays most devices have
8838 256 byte idata memory.
8840 \begin_inset LatexCommand \index{stack}
8844 is located in idata memory.
8845 \layout Subsubsection
8848 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8853 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8860 Paged xdata access is just as straightforward as using the other addressing
8862 It is typically located at the start of xdata and has a maximum size of
8864 The following example writes 0x01 to the pdata variable.
8865 Please note, pdata access physically accesses xdata memory.
8866 The high byte of the address is determined by port P2
8867 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8871 (or in case of some 8051 variants by a separate Special Function Register,
8873 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8882 storage class for the Medium Memory model, e.g.:
8887 pdata unsigned char test_pdata;
8890 Writing 0x01 to this variable generates the assembly code:
8934 \begin_inset LatexCommand \index{-\/-xstack}
8938 option is used the pdata memory area is followed by the xstack memory area
8939 and the sum of their sizes is limited to 256 bytes.
8940 \layout Subsubsection
8943 \begin_inset LatexCommand \index{code}
8948 \begin_inset LatexCommand \index{\_\_code}
8955 'Variables' declared with this storage class will be placed in the code
8961 code unsigned char test_code;
8964 Read access to this variable generates the assembly code:
8969 90s00r6F\SpecialChar ~
8972 mov dptr,#_test_code
9001 indexed arrays of characters in code memory can be accessed efficiently:
9006 code char test_array[] = {'c','h','e','a','p'};
9009 Read access to this array using an 8-bit unsigned index generates the assembly
9026 90s00r41\SpecialChar ~
9029 mov dptr,#_test_array
9044 \layout Subsubsection
9047 \begin_inset LatexCommand \index{bit}
9052 \begin_inset LatexCommand \index{\_\_bit}
9059 This is a data-type and a storage class specifier.
9060 When a variable is declared as a bit, it is allocated into the bit addressable
9061 memory of 8051, e.g.:
9069 Writing 1 to this variable generates the assembly code:
9085 The bit addressable memory consists of 128 bits which are located from 0x20
9086 to 0x2f in data memory.
9089 Apart from this 8051 specific storage class most architectures support ANSI-C
9091 \begin_inset LatexCommand \index{bitfields}
9101 Not really meant as examples, but nevertheless showing what bitfields are
9102 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9106 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9107 signed modifier are implemented as unsigned.
9108 \layout Subsubsection
9111 \begin_inset LatexCommand \index{sfr}
9116 \begin_inset LatexCommand \index{\_\_sfr}
9121 \begin_inset LatexCommand \index{sfr16}
9126 \begin_inset LatexCommand \index{\_\_sfr16}
9131 \begin_inset LatexCommand \index{sfr32}
9136 \begin_inset LatexCommand \index{\_\_sfr32}
9141 \begin_inset LatexCommand \index{\_\_sbit}
9148 Like the bit keyword,
9150 sfr / sfr16 / sfr32 / sbit
9152 signify both a data-type and storage class, they are used to describe the
9173 variables of a 8051, eg:
9179 \begin_inset LatexCommand \index{at}
9184 \begin_inset LatexCommand \index{\_\_at}
9188 0x80 P0;\SpecialChar ~
9189 /* special function register P0 at location 0x80 */
9191 /* 16 bit special function register combination for timer 0 */
9193 /* with the high byte at location 0x8C and the low byte at location 0x8A
9197 \begin_inset LatexCommand \index{at}
9202 \begin_inset LatexCommand \index{\_\_at}
9208 sbit at 0xd7 CY; /* CY (Carry Flag
9209 \begin_inset LatexCommand \index{Flags}
9214 \begin_inset LatexCommand \index{Carry flag}
9221 Special function registers which are located on an address dividable by
9222 8 are bit-addressable, an
9226 addresses a specific bit within these sfr.
9228 16 Bit and 32 bit special function register combinations which require a
9229 certain access order are better not declared using
9238 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9239 this is not guaranteed.
9240 \layout Subsubsection
9243 \begin_inset LatexCommand \index{Pointer}
9247 to MCS51/DS390 specific memory spaces
9250 SDCC allows (via language extensions) pointers to explicitly point to any
9251 of the memory spaces
9252 \begin_inset LatexCommand \index{Memory model}
9257 In addition to the explicit pointers, the compiler uses (by default) generic
9258 pointers which can be used to point to any of the memory spaces.
9262 Pointer declaration examples:
9267 /* pointer physically in internal ram pointing to object in external ram
9270 xdata unsigned char * data p;
9274 /* pointer physically in external ram pointing to object in internal ram
9277 data unsigned char * xdata p;
9281 /* pointer physically in code rom pointing to data in xdata space */
9283 xdata unsigned char * code p;
9287 /* pointer physically in code space pointing to data in code space */
9289 code unsigned char * code p;
9293 /* the following is a generic pointer physically located in xdata space
9300 /* the following is a function pointer physically located in data space
9303 char (* data fp)(void);
9306 Well you get the idea.
9311 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9324 The highest order byte of the
9328 pointers contains the data space information.
9329 Assembler support routines are called whenever data is stored or retrieved
9335 These are useful for developing reusable library
9336 \begin_inset LatexCommand \index{Libraries}
9341 Explicitly specifying the pointer type will generate the most efficient
9343 \layout Subsubsection
9345 Notes on MCS51 memory
9346 \begin_inset LatexCommand \index{MCS51 memory}
9353 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9354 RAM memory which is structured as follows:
9358 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9361 - Bytes 20-2F - 16 bytes to hold 128 bit
9362 \begin_inset LatexCommand \index{bit}
9368 - Bytes 30-7F - 80 bytes for general purpose use.
9373 Additionally some members of the MCS51 family may have up to 128 bytes of
9374 additional, indirectly addressable, internal RAM memory (
9379 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9384 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9389 Furthermore, some chips may have some built in external memory (
9394 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9399 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9403 ) which should not be confused with the internal, directly addressable RAM
9409 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9414 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9419 Sometimes this built in
9423 memory has to be activated before using it (you can probably find this
9424 information on the datasheet of the microcontroller your are using, see
9426 \begin_inset LatexCommand \ref{sub:Startup-Code}
9434 Normally SDCC will only use the first bank
9435 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9439 of registers (register bank 0), but it is possible to specify that other
9440 banks of registers (keyword
9447 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9452 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9458 ) should be used in interrupt
9459 \begin_inset LatexCommand \index{interrupt}
9464 \begin_inset LatexCommand \index{\_\_interrupt}
9469 By default, the compiler will place the stack after the last byte of allocated
9470 memory for variables.
9471 For example, if the first 2 banks of registers are used, and only four
9476 variables, it will position the base of the internal stack at address 20
9478 This implies that as the stack
9479 \begin_inset LatexCommand \index{stack}
9483 grows, it will use up the remaining register banks, and the 16 bytes used
9484 by the 128 bit variables, and 80 bytes for general purpose use.
9485 If any bit variables are used, the data variables will be placed in unused
9486 register banks and after the byte holding the last bit variable.
9487 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9492 variables will be placed starting from address 0x10 to 0x20 and continue
9505 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9509 to specify the start address of the
9524 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9528 to specify the size of the total internal RAM (
9540 By default the 8051 linker will place the stack after the last byte of (i)data
9553 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9557 allows you to specify the start of the stack, i.e.
9558 you could start it after any data in the general purpose area.
9559 If your microcontroller has additional indirectly addressable internal
9564 ) you can place the stack on it.
9565 You may also need to use -
9576 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9580 to set the start address of the external RAM (
9595 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9599 to specify its size.
9600 Same goes for the code memory, using -
9611 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9626 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9631 If in doubt, don't specify any options and see if the resulting memory
9632 layout is appropriate, then you can adjust it.
9635 The linker generates two files with memory allocation information.
9636 The first, with extension .map
9637 \begin_inset LatexCommand \index{<file>.map}
9641 shows all the variables and segments.
9642 The second with extension .mem
9643 \begin_inset LatexCommand \index{<file>.mem}
9647 shows the final memory layout.
9648 The linker will complain either if memory segments overlap, there is not
9649 enough memory, or there is not enough space for stack.
9650 If you get any linking warnings and/or errors related to stack or segments
9651 allocation, take a look at either the .map or .mem files to find out what
9653 The .mem file may even suggest a solution to the problem.
9656 Z80/Z180 Storage Class
9657 \begin_inset LatexCommand \index{Storage class}
9662 \layout Subsubsection
9665 \begin_inset LatexCommand \index{sfr}
9670 \begin_inset LatexCommand \index{\_\_sfr}
9674 (in/out to 8-bit addresses)
9678 \begin_inset LatexCommand \index{Z80}
9682 family has separate address spaces for memory and
9692 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9696 is accessed with special instructions, e.g.:
9701 sfr at 0x78 IoPort;\SpecialChar ~
9703 /* define a var in I/O space at 78h called IoPort */
9707 Writing 0x01 to this variable generates the assembly code:
9727 \layout Subsubsection
9730 \begin_inset LatexCommand \index{sfr}
9735 \begin_inset LatexCommand \index{\_\_sfr}
9739 (in/out to 16-bit addresses)
9746 is used to support 16 bit addresses in I/O memory e.g.:
9752 \begin_inset LatexCommand \index{at}
9757 \begin_inset LatexCommand \index{\_\_at}
9764 Writing 0x01 to this variable generates the assembly code:
9769 01 23 01\SpecialChar ~
9789 \layout Subsubsection
9792 \begin_inset LatexCommand \index{sfr}
9797 \begin_inset LatexCommand \index{\_\_sfr}
9801 (in0/out0 to 8 bit addresses on Z180
9802 \begin_inset LatexCommand \index{Z180}
9807 \begin_inset LatexCommand \index{HD64180}
9814 The compiler option -
9824 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9826 \begin_inset LatexCommand \index{\#pragma portmode}
9830 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9840 If you include the file z180.h this will be set automatically.
9844 \begin_inset LatexCommand \index{Storage class}
9849 \layout Subsubsection
9852 \begin_inset LatexCommand \index{data (hc08 storage class)}
9857 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9864 The data storage class declares a variable that resides in the first 256
9865 bytes of memory (the direct page).
9866 The HC08 is most efficient at accessing variables (especially pointers)
9868 \layout Subsubsection
9871 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9876 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9883 The xdata storage class declares a variable that can reside anywhere in
9885 This is the default if no storage class is specified.
9890 \begin_inset LatexCommand \index{Absolute addressing}
9897 Data items can be assigned an absolute address with the
9900 \begin_inset LatexCommand \index{at}
9905 \begin_inset LatexCommand \index{\_\_at}
9911 keyword, in addition to a storage class, e.g.:
9917 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9922 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9927 \begin_inset LatexCommand \index{at}
9932 \begin_inset LatexCommand \index{\_\_at}
9936 0x7ffe unsigned int chksum;
9939 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9940 of the external ram.
9945 reserve any space for variables declared in this way (they are implemented
9946 with an equate in the assembler).
9947 Thus it is left to the programmer to make sure there are no overlaps with
9948 other variables that are declared without the absolute address.
9949 The assembler listing file (.lst
9950 \begin_inset LatexCommand \index{<file>.lst}
9954 ) and the linker output files (.rst
9955 \begin_inset LatexCommand \index{<file>.rst}
9960 \begin_inset LatexCommand \index{<file>.map}
9964 ) are good places to look for such overlaps.
9965 Variables with an absolute address are
9970 \begin_inset LatexCommand \index{Variable initialization}
9977 In case of memory mapped I/O devices the keyword
9981 has to be used to tell the compiler that accesses might not be removed:
9987 \begin_inset LatexCommand \index{volatile}
9992 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9997 \begin_inset LatexCommand \index{at}
10001 0x8000 unsigned char PORTA_8255;
10004 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10009 \begin_inset LatexCommand \index{Aligned array}
10016 starts at a block (256 byte) boundary
10017 \begin_inset LatexCommand \index{block boundary}
10022 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10028 Absolute addresses can be specified for variables in all storage classes,
10035 \begin_inset LatexCommand \index{bit}
10040 \begin_inset LatexCommand \index{at}
10047 The above example will allocate the variable at offset 0x02 in the bit-addressab
10049 There is no real advantage to assigning absolute addresses to variables
10050 in this manner, unless you want strict control over all the variables allocated.
10051 One possible use would be to write hardware portable code.
10052 For example, if you have a routine that uses one or more of the microcontroller
10053 I/O pins, and such pins are different for two different hardwares, you
10054 can declare the I/O pins in your routine using:
10060 \begin_inset LatexCommand \index{volatile}
10064 bit MOSI;\SpecialChar ~
10068 /* master out, slave in */
10070 extern volatile bit MISO;\SpecialChar ~
10074 /* master in, slave out */
10076 extern volatile bit MCLK;\SpecialChar ~
10084 /* Input and Output of a byte on a 3-wire serial bus.
10089 If needed adapt polarity of clock, polarity of data and bit order
10094 unsigned char spi_io(unsigned char out_byte)
10118 MOSI = out_byte & 0x80;
10148 /* _asm nop _endasm; */\SpecialChar ~
10156 /* for slow peripherals */
10207 Then, someplace in the code for the first hardware you would use
10213 \begin_inset LatexCommand \index{at}
10218 \begin_inset LatexCommand \index{\_\_at}
10222 0x80 MOSI;\SpecialChar ~
10226 /* I/O port 0, bit 0 */
10228 bit at 0x81 MISO;\SpecialChar ~
10232 /* I/O port 0, bit 1 */
10234 bit at 0x82 MCLK;\SpecialChar ~
10238 /* I/O port 0, bit 2 */
10241 Similarly, for the second hardware you would use
10246 bit at 0x83 MOSI;\SpecialChar ~
10250 /* I/O port 0, bit 3 */
10252 bit at 0x91 MISO;\SpecialChar ~
10256 /* I/O port 1, bit 1 */
10259 \begin_inset LatexCommand \index{bit}
10263 at 0x92 MCLK;\SpecialChar ~
10267 /* I/O port 1, bit 2 */
10270 and you can use the same hardware dependent routine without changes, as
10271 for example in a library.
10272 This is somehow similar to sbit, but only one absolute address has to be
10273 specified in the whole project.
10277 \begin_inset LatexCommand \index{Parameters}
10282 \begin_inset LatexCommand \index{function parameter}
10287 \begin_inset LatexCommand \index{local variables}
10292 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10299 Automatic (local) variables and parameters to functions can either be placed
10300 on the stack or in data-space.
10301 The default action of the compiler is to place these variables in the internal
10302 RAM (for small model) or external RAM (for large model).
10303 This in fact makes them similar to
10306 \begin_inset LatexCommand \index{static}
10312 so by default functions are non-reentrant
10313 \begin_inset LatexCommand \index{reentrant}
10322 They can be placed on the stack
10323 \begin_inset LatexCommand \index{stack}
10340 \begin_inset LatexCommand \index{-\/-stack-auto}
10348 #pragma\SpecialChar ~
10352 \begin_inset LatexCommand \index{\#pragma stackauto}
10359 \begin_inset LatexCommand \index{reentrant}
10365 keyword in the function declaration, e.g.:
10370 unsigned char foo(char i) reentrant
10384 Since stack space on 8051 is limited, the
10402 option should be used sparingly.
10403 Note that the reentrant keyword just means that the parameters & local
10404 variables will be allocated to the stack, it
10408 mean that the function is register bank
10409 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10418 \begin_inset LatexCommand \index{local variables}
10422 can be assigned storage classes and absolute
10423 \begin_inset LatexCommand \index{Absolute addressing}
10432 unsigned char foo()
10440 xdata unsigned char i;
10453 \begin_inset LatexCommand \index{at}
10457 0x31 unsigned char j;
10469 In the above example the variable
10473 will be allocated in the external ram,
10477 in bit addressable space and
10496 or when a function is declared as
10500 this should only be done for static variables.
10504 \begin_inset LatexCommand \index{function parameter}
10508 however are not allowed any storage class
10509 \begin_inset LatexCommand \index{Storage class}
10513 , (storage classes for parameters will be ignored), their allocation is
10514 governed by the memory model in use, and the reentrancy options.
10517 It is however allowed to use bit parameters in reentrant functions and also
10518 non-static local bit variables are supported.
10519 Efficient use is limited to 8 semi-bitregisters in bit space.
10520 They are pushed and popped to stack as a single byte just like the normal
10525 \begin_inset LatexCommand \label{sub:Overlaying}
10530 \begin_inset LatexCommand \index{Overlaying}
10538 \begin_inset LatexCommand \index{reentrant}
10542 functions SDCC will try to reduce internal ram space usage by overlaying
10543 parameters and local variables of a function (if possible).
10544 Parameters and local variables
10545 \begin_inset LatexCommand \index{local variables}
10549 of a function will be allocated to an overlayable segment if the function
10552 no other function calls and the function is non-reentrant and the memory
10554 \begin_inset LatexCommand \index{Memory model}
10561 If an explicit storage class
10562 \begin_inset LatexCommand \index{Storage class}
10566 is specified for a local variable, it will NOT be overlayed.
10569 Note that the compiler (not the linkage editor) makes the decision for overlayin
10571 Functions that are called from an interrupt service routine should be preceded
10572 by a #pragma\SpecialChar ~
10574 \begin_inset LatexCommand \index{\#pragma nooverlay}
10578 if they are not reentrant.
10581 Also note that the compiler does not do any processing of inline assembler
10582 code, so the compiler might incorrectly assign local variables and parameters
10583 of a function into the overlay segment if the inline assembler code calls
10584 other c-functions that might use the overlay.
10585 In that case the #pragma\SpecialChar ~
10586 nooverlay should be used.
10589 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10591 \begin_inset LatexCommand \index{Multiplication}
10596 \begin_inset LatexCommand \index{Division}
10600 will NOT be overlayed since these are implemented using external functions,
10609 \begin_inset LatexCommand \index{\#pragma nooverlay}
10615 void set_error(unsigned char errcd)
10631 void some_isr () interrupt
10632 \begin_inset LatexCommand \index{interrupt}
10662 In the above example the parameter
10670 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10672 not present, this could cause unpredictable runtime behavior when called
10673 from an interrupt service routine.
10674 The #pragma\SpecialChar ~
10675 nooverlay ensures that the parameters and local variables for
10676 the function are NOT overlayed.
10679 Interrupt Service Routines
10680 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10687 General Information
10702 outines to be coded in C, with some extended keywords.
10707 void timer_isr (void) interrupt 1 using 1
10721 The optional number following the
10724 \begin_inset LatexCommand \index{interrupt}
10729 \begin_inset LatexCommand \index{\_\_interrupt}
10735 keyword is the interrupt number this routine will service.
10736 When present, the compiler will insert a call to this routine in the interrupt
10737 vector table for the interrupt number specified.
10738 If you have multiple source files in your project, interrupt service routines
10739 can be present in any of them, but a prototype of the isr MUST be present
10740 or included in the file that contains the function
10748 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10753 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10759 keyword can be used to tell the compiler to use the specified register
10760 bank (8051 specific) when generating code for this function.
10766 Interrupt service routines open the door for some very interesting bugs:
10768 If an interrupt service routine changes variables which are accessed by
10769 other functions these variables have to be declared
10774 \begin_inset LatexCommand \index{volatile}
10782 If the access to these variables is not
10785 \begin_inset LatexCommand \index{atomic}
10792 the processor needs more than one instruction for the access and could
10793 be interrupted while accessing the variable) the interrupt must be disabled
10794 during the access to avoid inconsistent data.
10795 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10796 and should be protected by disabling interrupts.
10797 You're not automatically on the safe side if you use 8 bit variables though.
10798 We need an example here: f.e.
10799 on the 8051 the harmless looking
10800 \begin_inset Quotes srd
10805 flags\SpecialChar ~
10810 \begin_inset Quotes sld
10819 \begin_inset Quotes srd
10824 flags\SpecialChar ~
10829 \begin_inset Quotes sld
10832 from within an interrupt routine might get lost if the interrupt occurs
10835 \begin_inset Quotes sld
10840 counter\SpecialChar ~
10845 \begin_inset Quotes srd
10848 is not atomic on the 8051 even if
10852 is located in data memory.
10853 Bugs like these are hard to reproduce and can cause a lot of trouble.
10857 The return address and the registers used in the interrupt service routine
10858 are saved on the stack
10859 \begin_inset LatexCommand \index{stack}
10863 so there must be sufficient stack space.
10864 If there isn't variables or registers (or even the return address itself)
10871 \begin_inset LatexCommand \index{stack overflow}
10875 is most likely to happen if the interrupt occurs during the
10876 \begin_inset Quotes sld
10880 \begin_inset Quotes srd
10883 subroutine when the stack is already in use for f.e.
10884 many return addresses.
10887 A special note here, int (16 bit) and long (32 bit) integer division
10888 \begin_inset LatexCommand \index{Division}
10893 \begin_inset LatexCommand \index{Multiplication}
10898 \begin_inset LatexCommand \index{Modulus}
10903 \begin_inset LatexCommand \index{Floating point support}
10907 operations are implemented using external support routines developed in
10909 If an interrupt service routine needs to do any of these operations then
10910 the support routines (as mentioned in a following section) will have to
10911 be recompiled using the
10924 \begin_inset LatexCommand \index{-\/-stack-auto}
10930 option and the source file will need to be compiled using the
10945 \begin_inset LatexCommand \index{-\/-int-long-reent}
10952 Calling other functions from an interrupt service routine is not recommended,
10953 avoid it if possible.
10954 Note that when some function is called from an interrupt service routine
10955 it should be preceded by a #pragma\SpecialChar ~
10957 \begin_inset LatexCommand \index{\#pragma nooverlay}
10961 if it is not reentrant.
10962 Furthermore nonreentrant functions should not be called from the main program
10963 while the interrupt service routine might be active.
10964 They also must not be called from low priority interrupt service routines
10965 while a high priority interrupt service routine might be active.
10966 You could use semaphores or make the function
10970 if all parameters are passed in registers.
10975 \begin_inset LatexCommand \ref{sub:Overlaying}
10980 about Overlaying and section
10981 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10986 about Functions using private register banks.
10989 MCS51/DS390 Interrupt Service Routines
10992 Interrupt numbers and the corresponding address & descriptions for the Standard
10993 8051/8052 are listed below.
10994 SDCC will automatically adjust the interrupt vector table to the maximum
10995 interrupt number specified.
11001 \begin_inset Tabular
11002 <lyxtabular version="3" rows="7" columns="3">
11004 <column alignment="center" valignment="top" leftline="true" width="0in">
11005 <column alignment="center" valignment="top" leftline="true" width="0in">
11006 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
11007 <row topline="true" bottomline="true">
11008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11024 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11033 <row topline="true">
11034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11050 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11059 <row topline="true">
11060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11068 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11085 <row topline="true">
11086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11102 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11111 <row topline="true">
11112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11137 <row topline="true">
11138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11163 <row topline="true" bottomline="true">
11164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11180 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11198 If the interrupt service routine is defined without
11201 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11206 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11212 a register bank or with register bank 0 (
11216 0), the compiler will save the registers used by itself on the stack upon
11217 entry and restore them at exit, however if such an interrupt service routine
11218 calls another function then the entire register bank will be saved on the
11220 This scheme may be advantageous for small interrupt service routines which
11221 have low register usage.
11224 If the interrupt service routine is defined to be using a specific register
11229 & psw are saved and restored, if such an interrupt service routine calls
11230 another function (using another register bank) then the entire register
11231 bank of the called function will be saved on the stack.
11232 This scheme is recommended for larger interrupt service routines.
11235 HC08 Interrupt Service Routines
11238 Since the number of interrupts available is chip specific and the interrupt
11239 vector table always ends at the last byte of memory, the interrupt numbers
11240 corresponds to the interrupt vectors in reverse order of address.
11241 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11242 2 will use the interrupt vector at 0xfffa, and so on.
11243 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11244 this way; instead see section
11245 \begin_inset LatexCommand \ref{sub:Startup-Code}
11249 for details on customizing startup.
11252 Z80 Interrupt Service Routines
11255 The Z80 uses several different methods for determining the correct interrupt
11256 vector depending on the hardware implementation.
11257 Therefore, SDCC ignores the optional interrupt number and does not attempt
11258 to generate an interrupt vector table.
11261 By default, SDCC generates code for a maskable interrupt, which uses an
11262 RETI instruction to return from the interrupt.
11263 To write an interrupt handler for the non-maskable interrupt, which needs
11264 an RETN instruction instead, add the
11273 void nmi_isr (void) critical interrupt
11287 Enabling and Disabling Interrupts
11290 Critical Functions and Critical Statements
11293 A special keyword may be associated with a block or a function declaring
11299 SDCC will generate code to disable all interrupts
11300 \begin_inset LatexCommand \index{interrupt}
11304 upon entry to a critical function and restore the interrupt enable to the
11305 previous state before returning.
11306 Nesting critical functions will need one additional byte on the stack
11307 \begin_inset LatexCommand \index{stack}
11316 int foo () critical
11317 \begin_inset LatexCommand \index{critical}
11322 \begin_inset LatexCommand \index{\_\_critical}
11347 The critical attribute maybe used with other attributes like
11357 may also be used to disable interrupts more locally:
11365 More than one statement could have been included in the block.
11368 Enabling and Disabling Interrupts directly
11372 \begin_inset LatexCommand \index{interrupt}
11376 can also be disabled and enabled directly (8051):
11381 EA = 0;\SpecialChar ~
11444 EA = 1;\SpecialChar ~
11511 On other architectures which have seperate opcodes for enabling and disabling
11512 interrupts you might want to make use of defines with inline assembly
11513 \begin_inset LatexCommand \index{Assembler routines}
11523 \begin_inset LatexCommand \index{\_asm}
11532 \begin_inset LatexCommand \index{\_endasm}
11541 #define SEI _asm\SpecialChar ~
11553 Note: it is sometimes sufficient to disable only a specific interrupt source
11555 a timer or serial interrupt by manipulating an
11558 \begin_inset LatexCommand \index{interrupt mask}
11568 Usually the time during which interrupts are disabled should be kept as
11570 This minimizes both
11575 \begin_inset LatexCommand \index{interrupt latency}
11579 (the time between the occurrence of the interrupt and the execution of
11580 the first code in the interrupt routine) and
11585 \begin_inset LatexCommand \index{interrupt jitter}
11589 (the difference between the shortest and the longest interrupt latency).
11590 These really are something different, f.e.
11591 a serial interrupt has to be served before its buffer overruns so it cares
11592 for the maximum interrupt latency, whereas it does not care about jitter.
11593 On a loudspeaker driven via a digital to analog converter which is fed
11594 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11595 a much smaller jitter will be very audible.
11598 You can reenable interrupts within an interrupt routine and on some architecture
11599 s you can make use of two (or more) levels of
11601 interrupt priorities
11604 \begin_inset LatexCommand \index{interrupt priority}
11609 On some architectures which don't support interrupt priorities these can
11610 be implemented by manipulating the interrupt mask and reenabling interrupts
11611 within the interrupt routine.
11612 Check there is sufficient space on the stack
11613 \begin_inset LatexCommand \index{stack}
11617 and don't add complexity unless you have to.
11622 \begin_inset LatexCommand \index{semaphore}
11626 locking (mcs51/ds390)
11629 Some architectures (mcs51/ds390) have an atomic
11630 \begin_inset LatexCommand \index{atomic}
11643 These type of instructions are typically used in preemptive multitasking
11644 systems, where a routine f.e.
11645 claims the use of a data structure ('acquires a lock
11646 \begin_inset LatexCommand \index{lock}
11650 on it'), makes some modifications and then releases the lock when the data
11651 structure is consistent again.
11652 The instruction may also be used if interrupt and non-interrupt code have
11653 to compete for a resource.
11654 With the atomic bit test and clear instruction interrupts
11655 \begin_inset LatexCommand \index{interrupt}
11659 don't have to be disabled for the locking operation.
11663 SDCC generates this instruction if the source follows this pattern:
11669 \begin_inset LatexCommand \index{volatile}
11673 bit resource_is_free;
11677 if (resource_is_free)
11687 resource_is_free=0;
11700 resource_is_free=1;
11707 Note, mcs51 and ds390 support only an atomic
11708 \begin_inset LatexCommand \index{atomic}
11716 instruction (as opposed to atomic bit test and
11721 Functions using private register banks
11722 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11729 Some architectures have support for quickly changing register sets.
11730 SDCC supports this feature with the
11733 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11738 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11744 attribute (which tells the compiler to use a register bank
11745 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11749 other than the default bank zero).
11750 It should only be applied to
11753 \begin_inset LatexCommand \index{interrupt}
11759 functions (see footnote below).
11760 This will in most circumstances make the generated ISR code more efficient
11761 since it will not have to save registers on the stack.
11768 attribute will have no effect on the generated code for a
11772 function (but may occasionally be useful anyway
11778 possible exception: if a function is called ONLY from 'interrupt' functions
11779 using a particular bank, it can be declared with the same 'using' attribute
11780 as the calling 'interrupt' functions.
11781 For instance, if you have several ISRs using bank one, and all of them
11782 call memcpy(), it might make sense to create a specialized version of memcpy()
11783 'using 1', since this would prevent the ISR from having to save bank zero
11784 to the stack on entry and switch to bank zero before calling the function
11791 (pending: I don't think this has been done yet)
11798 function using a non-zero bank will assume that it can trash that register
11799 bank, and will not save it.
11800 Since high-priority interrupts
11801 \begin_inset LatexCommand \index{interrupts}
11806 \begin_inset LatexCommand \index{interrupt priority}
11810 can interrupt low-priority ones on the 8051 and friends, this means that
11811 if a high-priority ISR
11815 a particular bank occurs while processing a low-priority ISR
11819 the same bank, terrible and bad things can happen.
11820 To prevent this, no single register bank should be
11824 by both a high priority and a low priority ISR.
11825 This is probably most easily done by having all high priority ISRs use
11826 one bank and all low priority ISRs use another.
11827 If you have an ISR which can change priority at runtime, you're on your
11828 own: I suggest using the default bank zero and taking the small performance
11832 It is most efficient if your ISR calls no other functions.
11833 If your ISR must call other functions, it is most efficient if those functions
11834 use the same bank as the ISR (see note 1 below); the next best is if the
11835 called functions use bank zero.
11836 It is very inefficient to call a function using a different, non-zero bank
11842 \begin_inset LatexCommand \label{sub:Startup-Code}
11847 \begin_inset LatexCommand \index{Startup code}
11854 MCS51/DS390 Startup Code
11857 The compiler inserts a call to the C routine
11859 _sdcc_external_startup()
11860 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11869 at the start of the CODE area.
11870 This routine is in the runtime library
11871 \begin_inset LatexCommand \index{Runtime library}
11876 By default this routine returns 0, if this routine returns a non-zero value,
11877 the static & global variable initialization will be skipped and the function
11878 main will be invoked.
11879 Otherwise static & global variables will be initialized before the function
11883 _sdcc_external_startup()
11885 routine to your program to override the default if you need to setup hardware
11886 or perform some other critical operation prior to static & global variable
11888 \begin_inset LatexCommand \index{Variable initialization}
11893 On some mcs51 variants xdata
11894 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11898 memory has to be explicitly enabled before it can be accessed or if the
11899 watchdog needs to be disabled, this is the place to do it.
11900 The startup code clears all internal data memory, 256 bytes by default,
11901 but from 0 to n-1 if
11914 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11921 (recommended for Chipcon CC1010).
11924 See also the compiler option
11943 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11948 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11953 about MCS51-variants.
11959 The HC08 startup code follows the same scheme as the MCS51 startup code.
11965 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11966 from sdcc/device/lib/z80/crt0.s.
11967 If you need a different startup code you can use the compiler option
11988 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11992 and provide your own crt0.o.
11996 Inline Assembler Code
11997 \begin_inset LatexCommand \index{Assembler routines}
12004 A Step by Step Introduction
12005 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12012 Starting from a small snippet of c-code this example shows for the MCS51
12013 how to use inline assembly, access variables, a function parameter and
12014 an array in xdata memory.
12015 The example uses an MCS51 here but is easily adapted for other architectures.
12016 This is a buffer routine which should be optimized:
12023 \begin_inset LatexCommand \index{far (storage class)}
12028 \begin_inset LatexCommand \index{\_\_far (storage class)}
12033 \begin_inset LatexCommand \index{at}
12038 \begin_inset LatexCommand \index{\_\_at}
12043 \begin_inset LatexCommand \index{Aligned array}
12049 unsigned char head,tail;
12053 void to_buffer( unsigned char c )
12061 if( head != tail-1 )
12071 buf[ head++ ] = c;\SpecialChar ~
12075 /* access to a 256 byte aligned array */
12080 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12081 then a corresponding buffer.asm file is generated.
12082 We define a new function
12086 in file buffer.c in which we cut and paste the generated code, removing
12087 unwanted comments and some ':'.
12089 \begin_inset Quotes sld
12093 \begin_inset Quotes srd
12097 \begin_inset Quotes sld
12101 \begin_inset Quotes srd
12104 to the beginning and the end of the function body:
12110 /* With a cut and paste from the .asm file, we have something to start with.
12115 The function is not yet OK! (registers aren't saved) */
12117 void to_buffer_asm( unsigned char c )
12126 \begin_inset LatexCommand \index{\_asm}
12131 \begin_inset LatexCommand \index{\_\_asm}
12145 ;buffer.c if( head != tail-1 )
12193 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12194 \begin_inset LatexCommand \index{Aligned array}
12259 \begin_inset LatexCommand \index{\_endasm}
12264 \begin_inset LatexCommand \index{\_\_endasm}
12273 The new file buffer.c should compile with only one warning about the unreferenced
12274 function argument 'c'.
12275 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12276 (1) and finally have:
12282 unsigned char far at 0x7f00 buf[0x100];
12284 unsigned char head,tail;
12286 #define USE_ASSEMBLY (1)
12294 void to_buffer( unsigned char c )
12302 if( head != tail-1 )
12322 void to_buffer( unsigned char c )
12330 c; // to avoid warning: unreferenced function argument
12337 \begin_inset LatexCommand \index{\_asm}
12342 \begin_inset LatexCommand \index{\_\_asm}
12356 ; save used registers here.
12367 ; If we were still using r2,r3 we would have to push them here.
12370 ; if( head != tail-1 )
12413 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12437 ; buf[ head++ ] = c;
12448 a,dpl \SpecialChar ~
12455 ; dpl holds lower byte of function argument
12466 dpl,_head \SpecialChar ~
12469 ; buf is 0x100 byte aligned so head can be used directly
12511 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12523 ; restore used registers here
12530 \begin_inset LatexCommand \index{\_endasm}
12535 \begin_inset LatexCommand \index{\_\_endasm}
12546 The inline assembler code can contain any valid code understood by the assembler
12547 , this includes any assembler directives and comment lines
12553 The assembler does not like some characters like ':' or ''' in comments.
12554 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12555 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12560 \begin_inset LatexCommand \index{Assembler documentation}
12568 The compiler does not do any validation of the code within the
12571 \begin_inset LatexCommand \index{\_asm}
12576 \begin_inset LatexCommand \index{\_\_asm}
12584 \begin_inset LatexCommand \index{\_endasm}
12589 \begin_inset LatexCommand \index{\_\_endasm}
12598 Specifically it will not know which registers are used and thus register
12600 \begin_inset LatexCommand \index{push/pop}
12604 has to be done manually.
12608 It is recommended that each assembly instruction (including labels) be placed
12609 in a separate line (as the example shows).
12623 \begin_inset LatexCommand \index{-\/-peep-asm}
12629 command line option is used, the inline assembler code will be passed through
12630 the peephole optimizer
12631 \begin_inset LatexCommand \index{Peephole optimizer}
12636 There are only a few (if any) cases where this option makes sense, it might
12637 cause some unexpected changes in the inline assembler code.
12638 Please go through the peephole optimizer rules defined in file
12642 before using this option.
12646 \begin_inset LatexCommand \label{sub:Naked-Functions}
12651 \begin_inset LatexCommand \index{Naked functions}
12658 A special keyword may be associated with a function declaring it as
12661 \begin_inset LatexCommand \index{\_naked}
12666 \begin_inset LatexCommand \index{\_\_naked}
12677 function modifier attribute prevents the compiler from generating prologue
12678 \begin_inset LatexCommand \index{function prologue}
12683 \begin_inset LatexCommand \index{function epilogue}
12687 code for that function.
12688 This means that the user is entirely responsible for such things as saving
12689 any registers that may need to be preserved, selecting the proper register
12690 bank, generating the
12694 instruction at the end, etc.
12695 Practically, this means that the contents of the function must be written
12696 in inline assembler.
12697 This is particularly useful for interrupt functions, which can have a large
12698 (and often unnecessary) prologue/epilogue.
12699 For example, compare the code generated by these two functions:
12705 \begin_inset LatexCommand \index{volatile}
12709 data unsigned char counter;
12713 void simpleInterrupt(void) interrupt
12714 \begin_inset LatexCommand \index{interrupt}
12719 \begin_inset LatexCommand \index{\_\_interrupt}
12737 void nakedInterrupt(void) interrupt 2 _naked
12746 \begin_inset LatexCommand \index{\_asm}
12751 \begin_inset LatexCommand \index{\_\_asm}
12768 _counter ; does not change flags, no need to save psw
12780 ; MUST explicitly include ret or reti in _naked function.
12787 \begin_inset LatexCommand \index{\_endasm}
12792 \begin_inset LatexCommand \index{\_\_endasm}
12801 For an 8051 target, the generated simpleInterrupt looks like:
12942 whereas nakedInterrupt looks like:
12957 _counter ; does not change flags, no need to save psw
12975 ; MUST explicitly include ret or reti in _naked function
12978 The related directive #pragma exclude
12979 \begin_inset LatexCommand \index{\#pragma exclude}
12983 allows a more fine grained control over pushing & popping
12984 \begin_inset LatexCommand \index{push/pop}
12991 While there is nothing preventing you from writing C code inside a
12995 function, there are many ways to shoot yourself in the foot doing this,
12996 and it is recommended that you stick to inline assembler.
12999 Use of Labels within Inline Assembler
13002 SDCC allows the use of in-line assembler with a few restrictions regarding
13004 In older versions of the compiler all labels defined within inline assembler
13013 where nnnn is a number less than 100 (which implies a limit of utmost 100
13014 inline assembler labels
13028 \begin_inset LatexCommand \index{\_asm}
13033 \begin_inset LatexCommand \index{\_\_asm}
13063 \begin_inset LatexCommand \index{\_endasm}
13068 \begin_inset LatexCommand \index{\_\_endasm}
13075 Inline assembler code cannot reference any C-Labels, however it can reference
13077 \begin_inset LatexCommand \index{Labels}
13081 defined by the inline assembler, e.g.:
13106 ; some assembler code
13126 /* some more c code */
13128 clabel:\SpecialChar ~
13130 /* inline assembler cannot reference this label */
13142 $0003: ;label (can be referenced by inline assembler only)
13149 \begin_inset LatexCommand \index{\_endasm}
13154 \begin_inset LatexCommand \index{\_\_endasm}
13164 /* some more c code */
13169 In other words inline assembly code can access labels defined in inline
13170 assembly within the scope of the function.
13171 The same goes the other way, i.e.
13172 labels defines in inline assembly can not be accessed by C statements.
13175 Interfacing with Assembler Code
13176 \begin_inset LatexCommand \index{Assembler routines}
13183 Global Registers used for Parameter Passing
13184 \begin_inset LatexCommand \index{Parameter passing}
13191 The compiler always uses the global registers
13194 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13199 \begin_inset LatexCommand \index{DPTR}
13204 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13213 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13219 to pass the first parameter to a routine.
13220 The second parameter onwards is either allocated on the stack (for reentrant
13231 -stack-auto is used) or in data / xdata memory (depending on the memory
13236 Assembler Routine (non-reentrant)
13239 In the following example
13240 \begin_inset LatexCommand \index{reentrant}
13245 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13249 the function c_func calls an assembler routine asm_func, which takes two
13251 \begin_inset LatexCommand \index{function parameter}
13260 extern int asm_func(unsigned char, unsigned char);
13264 int c_func (unsigned char i, unsigned char j)
13272 return asm_func(i,j);
13286 return c_func(10,9);
13291 The corresponding assembler function is:
13296 .globl _asm_func_PARM_2
13397 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13414 Note here that the return values
13415 \begin_inset LatexCommand \index{return value}
13419 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13421 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13422 b' & 'acc' for four byte values.
13425 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13426 the parameter number starting from 1, and counting from the left.
13427 The first parameter is passed in
13428 \begin_inset Quotes eld
13432 \begin_inset Quotes erd
13435 for a one byte parameter,
13436 \begin_inset Quotes eld
13440 \begin_inset Quotes erd
13444 \begin_inset Quotes eld
13448 \begin_inset Quotes erd
13451 for three bytes and
13452 \begin_inset Quotes eld
13456 \begin_inset Quotes erd
13459 for a four bytes parameter.
13460 The variable name for the second parameter will be _<function_name>_PARM_2.
13464 Assemble the assembler routine with the following command:
13471 asx8051 -losg asmfunc.asm
13478 Then compile and link the assembler routine to the C source file with the
13486 sdcc cfunc.c asmfunc.rel
13489 Assembler Routine (reentrant)
13493 \begin_inset LatexCommand \index{reentrant}
13498 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13502 the second parameter
13503 \begin_inset LatexCommand \index{function parameter}
13507 onwards will be passed on the stack, the parameters are pushed from right
13509 after the call the leftmost parameter will be on the top of the stack.
13510 Here is an example:
13515 extern int asm_func(unsigned char, unsigned char);
13519 int c_func (unsigned char i, unsigned char j) reentrant
13527 return asm_func(i,j);
13541 return c_func(10,9);
13546 The corresponding assembler routine is:
13646 The compiling and linking procedure remains the same, however note the extra
13647 entry & exit linkage required for the assembler code, _bp is the stack
13648 frame pointer and is used to compute the offset into the stack for parameters
13649 and local variables.
13653 \begin_inset LatexCommand \index{int (16 bit)}
13658 \begin_inset LatexCommand \index{long (32 bit)}
13665 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13666 multiplication and modulus operations are implemented by support routines.
13667 These support routines are all developed in ANSI-C to facilitate porting
13668 to other MCUs, although some model specific assembler optimizations are
13670 The following files contain the described routines, all of them can be
13671 found in <installdir>/share/sdcc/lib.
13677 \begin_inset Tabular
13678 <lyxtabular version="3" rows="11" columns="2">
13680 <column alignment="center" valignment="top" leftline="true" width="0">
13681 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13682 <row topline="true" bottomline="true">
13683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13704 <row topline="true">
13705 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13718 16 bit multiplication
13722 <row topline="true">
13723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13731 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13736 signed 16 bit division (calls _divuint)
13740 <row topline="true">
13741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13749 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13754 unsigned 16 bit division
13758 <row topline="true">
13759 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13772 signed 16 bit modulus (calls _moduint)
13776 <row topline="true">
13777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13785 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13790 unsigned 16 bit modulus
13794 <row topline="true">
13795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13808 32 bit multiplication
13812 <row topline="true">
13813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13826 signed 32 division (calls _divulong)
13830 <row topline="true">
13831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13844 unsigned 32 division
13848 <row topline="true">
13849 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13857 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13862 signed 32 bit modulus (calls _modulong)
13866 <row topline="true" bottomline="true">
13867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13880 unsigned 32 bit modulus
13893 Since they are compiled as
13898 \begin_inset LatexCommand \index{reentrant}
13903 \begin_inset LatexCommand \index{interrupt}
13907 service routines should not do any of the above operations.
13908 If this is unavoidable then the above routines will need to be compiled
13922 \begin_inset LatexCommand \index{-\/-stack-auto}
13928 option, after which the source program will have to be compiled with
13941 \begin_inset LatexCommand \index{-\/-int-long-reent}
13948 Notice that you don't have to call these routines directly.
13949 The compiler will use them automatically every time an integer operation
13953 Floating Point Support
13954 \begin_inset LatexCommand \index{Floating point support}
13961 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13962 floating point support routines are derived from gcc's floatlib.c and consist
13963 of the following routines:
13971 \begin_inset Tabular
13972 <lyxtabular version="3" rows="17" columns="2">
13974 <column alignment="center" valignment="top" leftline="true" width="0">
13975 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13976 <row topline="true" bottomline="true">
13977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14003 <row topline="true">
14004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14035 add floating point numbers
14039 <row topline="true">
14040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14057 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14071 subtract floating point numbers
14075 <row topline="true">
14076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14093 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14107 divide floating point numbers
14111 <row topline="true">
14112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14143 multiply floating point numbers
14147 <row topline="true">
14148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14179 convert floating point to unsigned char
14183 <row topline="true">
14184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14215 convert floating point to signed char
14219 <row topline="true">
14220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14251 convert floating point to unsigned int
14255 <row topline="true">
14256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14287 convert floating point to signed int
14291 <row topline="true">
14292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14332 convert floating point to unsigned long
14336 <row topline="true">
14337 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14368 convert floating point to signed long
14372 <row topline="true">
14373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14390 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14404 convert unsigned char to floating point
14408 <row topline="true">
14409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14440 convert char to floating point number
14444 <row topline="true">
14445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14462 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14476 convert unsigned int to floating point
14480 <row topline="true">
14481 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14512 convert int to floating point numbers
14516 <row topline="true">
14517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14534 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14548 convert unsigned long to floating point number
14552 <row topline="true" bottomline="true">
14553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14584 convert long to floating point number
14597 These support routines are developed in ANSI-C so there is room for space
14598 and speed improvement
14604 The floating point routines for the mcs51 are implemented in assembler
14608 Note if all these routines are used simultaneously the data space might
14610 For serious floating point usage the large model might be needed.
14611 Also notice that you don't have to call this routines directly.
14612 The compiler will use them automatically every time a floating point operation
14617 \begin_inset LatexCommand \index{Libraries}
14626 <pending: this is messy and incomplete - a little more information is in
14627 sdcc/doc/libdoc.txt
14632 Compiler support routines (_gptrget, _mulint etc.)
14635 Stdclib functions (puts, printf, strcat etc.)
14636 \layout Subsubsection
14642 \begin_inset LatexCommand \index{<stdio.h>}
14646 As usual on embedded systems you have to provide your own
14649 \begin_inset LatexCommand \index{getchar()}
14658 \begin_inset LatexCommand \index{putchar()}
14665 SDCC does not know whether the system connects to a serial line with or
14666 without handshake, LCD, keyboard or other device.
14667 You'll find examples for serial routines f.e.
14668 in sdcc/device/lib.
14674 \begin_inset LatexCommand \index{printf()}
14684 does not support float (except on ds390).
14685 To enable this recompile it with the option
14698 \begin_inset LatexCommand \index{USE\_FLOATS}
14704 on the command line.
14718 \begin_inset LatexCommand \index{-\/-model-large}
14724 for the mcs51 port, since this uses a lot of memory.
14727 If you're short on memory you might want to use
14730 \begin_inset LatexCommand \index{printf\_small()}
14745 For the mcs51 there additionally are assembly versions
14748 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14757 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14766 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14772 which should fit the requirements of many embedded systems (printf_fast()
14773 can be customized by unsetting #defines to
14777 support long variables and field widths).
14780 Math functions (sin, pow, sqrt etc.)
14787 \begin_inset LatexCommand \index{Libraries}
14791 included in SDCC should have a license at least as liberal as the GNU Lesser
14792 General Public License
14793 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14804 license statements for the libraries are missing.
14805 sdcc/device/lib/ser_ir.c
14809 come with a GPL (as opposed to LGPL) License - this will not be liberal
14810 enough for many embedded programmers.
14813 If you have ported some library or want to share experience about some code
14815 falls into any of these categories Busses (I
14816 \begin_inset Formula $^{\textrm{2}}$
14819 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14820 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14821 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14822 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14827 would certainly like to hear about it.
14828 Programmers coding for embedded systems are not especially famous for being
14829 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14830 e these references are very valuable.
14831 Let's help to create a climate where information is shared.
14837 MCS51 Memory Models
14838 \begin_inset LatexCommand \index{Memory model}
14843 \begin_inset LatexCommand \index{MCS51 memory model}
14848 \layout Subsubsection
14850 Small, Medium and Large
14853 SDCC allows three memory models for MCS51 code,
14862 Modules compiled with different memory models should
14866 be combined together or the results would be unpredictable.
14867 The library routines supplied with the compiler are compiled as small,
14869 The compiled library modules are contained in separate directories as small,
14870 medium and large so that you can link to the appropriate set.
14873 When the medium or large model is used all variables declared without a
14874 storage class will be allocated into the external ram, this includes all
14875 parameters and local variables (for non-reentrant
14876 \begin_inset LatexCommand \index{reentrant}
14881 When the small model is used variables without storage class are allocated
14882 in the internal ram.
14885 Judicious usage of the processor specific storage classes
14886 \begin_inset LatexCommand \index{Storage class}
14890 and the 'reentrant' function type will yield much more efficient code,
14891 than using the large model.
14892 Several optimizations are disabled when the program is compiled using the
14893 large model, it is therefore recommended that the small model be used unless
14894 absolutely required.
14895 \layout Subsubsection
14898 \begin_inset LatexCommand \label{sub:External-Stack}
14903 \begin_inset LatexCommand \index{stack}
14908 \begin_inset LatexCommand \index{External stack (mcs51)}
14915 The external stack (-
14926 \begin_inset LatexCommand \index{-\/-xstack}
14930 ) is located in pdata
14931 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14935 memory (usually at the start of the external ram segment) and uses all
14936 unused space in pdata (max.
14948 -xstack option is used to compile the program, the parameters and local
14950 \begin_inset LatexCommand \index{local variables}
14954 of all reentrant functions are allocated in this area.
14955 This option is provided for programs with large stack space requirements.
14956 When used with the -
14967 \begin_inset LatexCommand \index{-\/-stack-auto}
14971 option, all parameters and local variables are allocated on the external
14972 stack (note: support libraries will need to be recompiled with the same
14974 There is a predefined target in the library makefile).
14977 The compiler outputs the higher order address byte of the external ram segment
14979 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14984 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14988 ), therefore when using the External Stack option, this port
14992 be used by the application program.
14996 \begin_inset LatexCommand \index{Memory model}
15001 \begin_inset LatexCommand \index{DS390 memory model}
15008 The only model supported is Flat 24
15009 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15014 This generates code for the 24 bit contiguous addressing mode of the Dallas
15016 In this mode, up to four meg of external RAM or code space can be directly
15018 See the data sheets at www.dalsemi.com for further information on this part.
15022 Note that the compiler does not generate any code to place the processor
15023 into 24 bitmode (although
15027 in the ds390 libraries will do that for you).
15033 \begin_inset LatexCommand \index{Tinibios (DS390)}
15037 , the boot loader or similar code must ensure that the processor is in 24
15038 bit contiguous addressing mode before calling the SDCC startup code.
15056 option, variables will by default be placed into the XDATA segment.
15061 Segments may be placed anywhere in the 4 meg address space using the usual
15073 Note that if any segments are located above 64K, the -r flag must be passed
15074 to the linker to generate the proper segment relocations, and the Intel
15075 HEX output format must be used.
15076 The -r flag can be passed to the linker by using the option
15080 on the SDCC command line.
15081 However, currently the linker can not handle code segments > 64k.
15085 \begin_inset LatexCommand \index{Pragmas}
15092 SDCC supports the following #pragma directives:
15096 \begin_inset LatexCommand \index{\#pragma save}
15100 - this will save all current options to the save/restore stack.
15101 See #pragma\SpecialChar ~
15106 \begin_inset LatexCommand \index{\#pragma restore}
15110 - will restore saved options from the last save.
15111 saves & restores can be nested.
15112 SDCC uses a save/restore stack: save pushes current options to the stack,
15113 restore pulls current options from the stack.
15114 See #pragma\SpecialChar ~
15121 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15126 \begin_inset LatexCommand \index{function prologue}
15130 function1[,function2[,function3...]] - The compiler by default uses a caller
15131 saves convention for register saving across function calls, however this
15132 can cause unnecessary register pushing & popping
15133 \begin_inset LatexCommand \index{push/pop}
15137 when calling small functions from larger functions.
15138 This option can be used to switch off the register saving convention for
15139 the function names specified.
15140 The compiler will not save registers when calling these functions, extra
15141 code need to be manually inserted at the entry & exit for these functions
15142 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15143 reduce code & improve run time performance of the generated code.
15144 In the future the compiler (with inter procedural analysis) may be able
15145 to determine the appropriate scheme to use for each function call.
15156 -callee-saves command line option is used, the function names specified
15157 in #pragma\SpecialChar ~
15159 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15163 is appended to the list of functions specified in the command line.
15167 \begin_inset LatexCommand \index{\#pragma exclude}
15171 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15172 of pairs of push/pop
15173 \begin_inset LatexCommand \index{push/pop}
15182 \begin_inset LatexCommand \index{interrupt}
15195 The directive should be placed immediately before the ISR function definition
15196 and it affects ALL ISR functions following it.
15197 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15198 exclude\SpecialChar ~
15200 \begin_inset LatexCommand \index{\#pragma exclude}
15205 See also the related keyword _naked
15206 \begin_inset LatexCommand \index{\_naked}
15211 \begin_inset LatexCommand \index{\_\_naked}
15219 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15223 - the compiler will not warn you anymore for obvious mistakes, you'r on
15227 disable_warning <nnnn>
15228 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15232 - the compiler will not warn you anymore about warning number <nnnn>.
15236 \begin_inset LatexCommand \index{\#pragma nogcse}
15240 - will stop global common subexpression elimination.
15244 \begin_inset LatexCommand \index{\#pragma noinduction}
15248 - will stop loop induction optimizations.
15252 \begin_inset LatexCommand \index{\#pragma noinvariant}
15256 - will not do loop invariant optimizations.
15257 For more details see Loop Invariants in section
15258 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15266 \begin_inset LatexCommand \index{\#pragma noiv}
15270 - Do not generate interrupt
15271 \begin_inset LatexCommand \index{interrupt}
15275 vector table entries for all ISR functions defined after the pragma.
15276 This is useful in cases where the interrupt vector table must be defined
15277 manually, or when there is a secondary, manually defined interrupt vector
15279 for the autovector feature of the Cypress EZ-USB FX2).
15280 More elegantly this can be achieved by obmitting the optional interrupt
15281 number after the interrupt keyword, see section
15282 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15291 \begin_inset LatexCommand \index{\#pragma nojtbound}
15295 - will not generate code for boundary value checking, when switch statements
15296 are turned into jump-tables (dangerous).
15297 For more details see section
15298 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15306 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15310 - Will not do loop reversal optimization
15314 \begin_inset LatexCommand \index{\#pragma nooverlay}
15318 - the compiler will not overlay the parameters and local variables of a
15323 \begin_inset LatexCommand \index{\#pragma stackauto}
15338 \begin_inset LatexCommand \index{-\/-stack-auto}
15343 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15347 Parameters and Local Variables.
15351 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15355 - The compiler will optimize code generation towards fast code, possibly
15356 at the expense of code size.
15360 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15364 - The compiler will optimize code generation towards compact code, possibly
15365 at the expense of code speed.
15369 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15373 - The compiler will attempt to generate code that is both compact and fast,
15374 as long as meeting one goal is not a detriment to the other (this is the
15380 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15384 - Generally follow the C89 standard, but allow SDCC features that conflict
15385 with the standard (default).
15389 \begin_inset LatexCommand \index{\#pragma std\_c89}
15393 - Follow the C89 standard and disable SDCC features that conflict with the
15398 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15402 - Generally follow the C99 standard, but allow SDCC features that conflict
15403 with the standard (incomplete support).
15407 \begin_inset LatexCommand \index{\#pragma std\_c99}
15411 - Follow the C99 standard and disable SDCC features that conflict with the
15412 standard (incomplete support).
15416 \begin_inset LatexCommand \index{\#pragma codeseg}
15420 - Use this name (max.
15421 8 characters) for the code segment.
15425 \begin_inset LatexCommand \index{\#pragma constseg}
15429 - Use this name (max.
15430 8 characters) for the const segment.
15433 SDCPP supports the following #pragma directives:
15437 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15441 (+ | -) - switch _asm _endasm block preprocessing on / off.
15445 The pragma's are intended to be used to turn-on or off certain optimizations
15446 which might cause the compiler to generate extra stack / data space to
15447 store compiler generated temporary variables.
15448 This usually happens in large functions.
15449 Pragma directives should be used as shown in the following example, they
15450 are used to control options & optimizations for a given function; pragmas
15451 should be placed before and/or after a function, placing pragma's inside
15452 a function body could have unpredictable results.
15458 \begin_inset LatexCommand \index{\#pragma save}
15469 /* save the current settings */
15472 \begin_inset LatexCommand \index{\#pragma nogcse}
15481 /* turnoff global subexpression elimination */
15483 #pragma noinduction
15484 \begin_inset LatexCommand \index{\#pragma noinduction}
15488 /* turn off induction optimizations */
15511 \begin_inset LatexCommand \index{\#pragma restore}
15515 /* turn the optimizations back on */
15518 The compiler will generate a warning message when extra space is allocated.
15519 It is strongly recommended that the save and restore pragma's be used when
15520 changing options for a function.
15523 Defines Created by the Compiler
15526 The compiler creates the following #defines
15527 \begin_inset LatexCommand \index{\#defines}
15532 \begin_inset LatexCommand \index{Defines created by the compiler}
15542 \begin_inset Tabular
15543 <lyxtabular version="3" rows="11" columns="2">
15545 <column alignment="center" valignment="top" leftline="true" width="0">
15546 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15547 <row topline="true" bottomline="true">
15548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15569 <row topline="true">
15570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15576 \begin_inset LatexCommand \index{SDCC}
15583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15588 this Symbol is always defined
15592 <row topline="true">
15593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15599 \begin_inset LatexCommand \index{SDCC\_mcs51}
15604 \begin_inset LatexCommand \index{SDCC\_ds390}
15609 \begin_inset LatexCommand \index{SDCC\_z80}
15616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15621 depending on the model used (e.g.: -mds390
15625 <row topline="true">
15626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15632 \begin_inset LatexCommand \index{\_\_mcs51}
15637 \begin_inset LatexCommand \index{\_\_ds390}
15642 \begin_inset LatexCommand \index{\_\_hc08}
15647 \begin_inset LatexCommand \index{\_\_z80}
15654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15659 depending on the model used (e.g.
15664 <row topline="true">
15665 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15671 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15678 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15701 <row topline="true">
15702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15708 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15738 <row topline="true">
15739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15745 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15775 <row topline="true">
15776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15782 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15812 <row topline="true">
15813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15819 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15849 <row topline="true">
15850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15856 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15863 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15876 <row topline="true" bottomline="true">
15877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15883 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15910 Notes on supported Processors
15914 \begin_inset LatexCommand \label{sub:MCS51-variants}
15919 \begin_inset LatexCommand \index{MCS51 variants}
15926 MCS51 processors are available from many vendors and come in many different
15928 While they might differ considerably in respect to Special Function Registers
15929 the core MCS51 is usually not modified or is kept compatible.
15933 pdata access by SFR
15936 With the upcome of devices with internal xdata and flash memory devices
15938 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15942 as dedicated I/O port is becoming more popular.
15943 Switching the high byte for pdata
15944 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15948 access which was formerly done by port P2 is then achieved by a Special
15950 \begin_inset LatexCommand \index{sfr}
15955 In well-established MCS51 tradition the address of this
15959 is where the chip designers decided to put it.
15960 Needless to say that they didn't agree on a common name either.
15961 So that the startup code can correctly initialize xdata variables, you
15962 should define an sfr with the name _XPAGE
15965 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15971 at the appropriate location if the default, port P2, is not used for this.
15977 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15982 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15987 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15990 For more exotic implementations further customizations may be needed.
15992 \begin_inset LatexCommand \ref{sub:Startup-Code}
15996 for other possibilities.
15999 Other Features available by SFR
16002 Some MCS51 variants offer features like Double DPTR
16003 \begin_inset LatexCommand \index{DPTR}
16007 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16008 These are currently not used for the MCS51 port.
16009 If you absolutely need them you can fall back to inline assembly or submit
16016 The DS80C400 microcontroller has a rich set of peripherals.
16017 In its built-in ROM library it includes functions to access some of the
16018 features, among them is a TCP stack with IP4 and IP6 support.
16019 Library headers (currently in beta status) and other files are provided
16023 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16031 The Z80 and gbz80 port
16034 SDCC can target both the Zilog
16035 \begin_inset LatexCommand \index{Z80}
16039 and the Nintendo Gameboy's Z80-like gbz80
16040 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16045 The Z80 port is passed through the same
16048 \begin_inset LatexCommand \index{Regression test}
16054 as the MCS51 and DS390 ports, so floating point support, support for long
16055 variables and bitfield support is fine.
16056 See mailing lists and forums about interrupt routines.
16059 As always, the code is the authoritative reference - see z80/ralloc.c and
16062 \begin_inset LatexCommand \index{stack}
16066 frame is similar to that generated by the IAR Z80 compiler.
16067 IX is used as the base pointer, HL and IY are used as a temporary registers,
16068 and BC and DE are available for holding variables.
16070 \begin_inset LatexCommand \index{return value}
16074 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16076 The gbz80 port use the same set of registers for the return values, but
16077 in a different order of significance: E (one byte), DE (two bytes), or
16084 The port to the Motorola HC08
16085 \begin_inset LatexCommand \index{HC08}
16089 family has been added in October 2003, and is still undergoing some basic
16091 The code generator is complete, but the register allocation is still quite
16093 Some of the SDCC's standard C library functions have embedded non-HC08
16094 inline assembly and so are not yet usable.
16105 \begin_inset LatexCommand \index{PIC14}
16109 port still requires a major effort from the development community.
16110 However it can work for very simple code.
16113 C code and 14bit PIC code page
16114 \begin_inset LatexCommand \index{code page (pic14)}
16119 \begin_inset LatexCommand \index{RAM bank (pic14)}
16126 The linker organizes allocation for the code page and RAM banks.
16127 It does not have intimate knowledge of the code flow.
16128 It will put all the code section of a single asm file into a single code
16130 In order to make use of multiple code pages, separate asm files must be
16132 The compiler treats all functions of a single C file as being in the same
16133 code page unless it is non static.
16134 The compiler treats all local variables of a single C file as being in
16135 the same RAM bank unless it is an extern.
16139 To get the best follow these guide lines:
16142 make local functions static, as non static functions require code page selection
16146 Make local variables static as extern variables require RAM bank selection
16150 For devices that have multiple code pages it is more efficient to use the
16151 same number of files as pages, i.e.
16152 for the 16F877 use 4 separate files and i.e.
16153 for the 16F874 use 2 separate files.
16154 This way the linker can put the code for each file into different code
16155 pages and the compiler can allocate reusable variables more efficiently
16156 and there's less page selection overhead.
16157 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16158 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16161 Creating a device include file
16164 For generating a device include file use the support perl script inc2h.pl
16165 kept in directory support/script.
16171 For the interrupt function, use the keyword 'interrupt'
16172 \begin_inset LatexCommand \index{interrupt}
16176 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16177 there to avoid a syntax error - it ought to be fixed).
16183 void Intr(void) interrupt 0
16189 T0IF = 0; /* Clear timer interrupt */
16194 Linking and assembling
16197 For assembling you can use either GPUTILS'
16198 \begin_inset LatexCommand \index{gputils (pic tools)}
16202 gpasm.exe or MPLAB's mpasmwin.exe.
16203 GPUTILS is available from
16204 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16209 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16210 If you use MPLAB and an interrupt function then the linker script file
16211 vectors section will need to be enlarged to link with mplink.
16234 sdcc -S -V -mpic14 -p16F877 $<
16248 $(PRJ).hex: $(OBJS)
16258 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16280 sdcc -S -V -mpic14 -p16F877 $<
16290 mpasmwin /q /o $*.asm
16294 $(PRJ).hex: $(OBJS)
16304 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16307 Please note that indentations within a
16311 have to be done with a tabulator character.
16314 Command-line options
16317 Besides the switches common to all SDCC backends, the PIC14 port accepts
16318 the following options (for an updated list see sdcc -
16330 \labelwidthstring 00.00.0000
16342 -debug-extra emit debug info in assembly output
16344 \labelwidthstring 00.00.0000
16356 -no-pcode-opt disable (slightly faulty) optimization on pCode
16360 \layout Subsubsection
16362 error: missing definition for symbol
16363 \begin_inset Quotes sld
16367 \begin_inset Quotes srd
16373 The PIC14 port uses library routines to provide more complex operations
16374 like multiplication, division/modulus and (generic) pointer dereferencing.
16375 In order to add these routines to your project, you must link with PIC14's
16381 For single source file projects this is done automatically, more complex
16386 to the linker's arguments.
16387 Make sure you also add an include path for the library (using the -I switch
16389 \layout Subsubsection
16391 Processor mismatch in file
16392 \begin_inset Quotes sld
16396 \begin_inset Quotes srd
16402 This warning can usually be ignored due to the very good compatibility amongst
16403 14 bit PIC devices.
16406 You might also consider recompiling the library for your specific device
16407 by changing the ARCH=p16f877 (default target) entry in
16409 device/lib/pic/Makefile.in
16413 device/lib/pic/Makefile
16415 to reflect your device.
16416 This might even improve performance for smaller devices as unneccesary
16417 BANKSELs migth be removed.
16421 \layout Subsubsection
16426 Currently, data can only be initialized if it resides in the source file
16432 Data in other source files will silently
16440 \begin_inset LatexCommand \index{PIC16}
16448 \begin_inset LatexCommand \index{PIC16}
16452 port is the portion of SDCC that is responsible to produce code for the
16454 \begin_inset LatexCommand \index{Microchip}
16458 (TM) microcontrollers with 16 bit core.
16459 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16460 Currently supported devices are:
16464 \begin_inset Tabular
16465 <lyxtabular version="3" rows="4" columns="6">
16467 <column alignment="center" valignment="top" leftline="true" width="0">
16468 <column alignment="center" valignment="top" leftline="true" width="0">
16469 <column alignment="center" valignment="top" leftline="true" width="0">
16470 <column alignment="center" valignment="top" leftline="true" width="0">
16471 <column alignment="center" valignment="top" leftline="true" width="0">
16472 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16473 <row topline="true">
16474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16514 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16523 <row topline="true">
16524 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16556 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16573 <row topline="true">
16574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16606 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16614 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16623 <row topline="true" bottomline="true">
16624 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16662 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16680 PIC16 port supports the standard command line arguments as supposed, with
16681 the exception of certain cases that will be mentioned in the following
16684 \labelwidthstring 00.00.0000
16696 -callee-saves See -
16708 \labelwidthstring 00.00.0000
16720 -all-callee-saves All function arguments are passed on stack by default.
16723 There is no need to specify this in the command line.
16725 \labelwidthstring 00.00.0000
16737 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16738 no local variables.
16741 Port Specific Options
16742 \begin_inset LatexCommand \index{Options PIC16}
16749 The port specific options appear after the global options in the sdcc --help
16751 \layout Subsubsection
16756 General options enable certain port features and optimizations.
16758 \labelwidthstring 00.00.0000
16770 -stack-model=[model] Used in conjuction with the command above.
16771 Defines the stack model to be used, valid stack models are :
16774 \labelwidthstring 00.00.0000
16780 Selects small stack model.
16781 8 bit stack and frame pointers.
16782 Supports 256 bytes stack size.
16784 \labelwidthstring 00.00.0000
16790 Selects large stack model.
16791 16 bit stack and frame pointers.
16792 Supports 65536 bytes stack size.
16795 \labelwidthstring 00.00.0000
16807 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16808 unitialized data variables with [kword].
16809 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16811 \labelwidthstring 00.00.0000
16823 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16824 Useful for bootloaders.
16826 \labelwidthstring 00.00.0000
16838 -asm= sets the full path and name of an external assembler to call.
16840 \labelwidthstring 00.00.0000
16852 -link= sets the full path and name of an external linker to call.
16854 \labelwidthstring 00.00.0000
16866 -mplab-comp MPLAB compatibility option.
16867 Currently only suppresses special gpasm directives.
16868 \layout Subsubsection
16870 Optimization Options
16872 \labelwidthstring 00.00.0000
16884 -optimize-goto Try to use (conditional) BRA instead of GOTO
16886 \labelwidthstring 00.00.0000
16898 -optimize-cmp Try to optimize some compares.
16900 \labelwidthstring 00.00.0000
16912 -optimize-df Analyze the dataflow of the generated code and improve it.
16914 \labelwidthstring 00.00.0000
16926 -obanksel=nn Set optimization level for inserting BANKSELs.
16931 \labelwidthstring 00.00.0000
16935 \labelwidthstring 00.00.0000
16937 1 checks previous used register and if it is the same then does not emit
16938 BANKSEL, accounts only for labels.
16940 \labelwidthstring 00.00.0000
16942 2 tries to check the location of (even different) symbols and removes BANKSELs
16943 if they are in the same bank.
16948 Important: There might be problems if the linker script has data sections
16949 across bank borders!
16951 \layout Subsubsection
16955 \labelwidthstring 00.00.0000
16967 -nodefaultlibs do not link default libraries when linking
16969 \labelwidthstring 00.00.0000
16981 -no-crt Don't link the default run-time modules
16983 \labelwidthstring 00.00.0000
16995 -use-crt= Use a custom run-time module instead of the defaults.
16996 \layout Subsubsection
17001 Debugging options enable extra debugging information in the output files.
17003 \labelwidthstring 00.00.0000
17015 -debug-xtra Similar to -
17026 \begin_inset LatexCommand \index{-\/-debug}
17030 , but dumps more information.
17032 \labelwidthstring 00.00.0000
17044 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17046 <source> is the name of the file compiled.
17048 \labelwidthstring 00.00.0000
17060 -pcode-verbose Enable pcode debugging information in translation.
17062 \labelwidthstring 00.00.0000
17074 -denable-peeps Force the usage of peepholes.
17077 \labelwidthstring 00.00.0000
17089 -gstack Trace push/pops for stack pointer overflow
17091 \labelwidthstring 00.00.0000
17103 -call-tree dump call tree in .calltree file
17106 Enviromental Variables
17109 There is a number of enviromental variables that can be used when running
17110 SDCC to enable certain optimizations or force a specific program behaviour.
17111 these variables are primarily for debugging purposes so they can be enabled/dis
17115 Currently there is only two such variables available:
17117 \labelwidthstring 00.00.0000
17119 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17120 bitfields is optimized by directly loading FSR0 with the address of the
17121 bitfield structure.
17122 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17124 This step saves data ram and code space for functions that perform heavy
17127 80 bytes of code space are saved when compiling malloc.c with this option).
17130 \labelwidthstring 00.00.0000
17132 NO_REG_OPT do not perform pCode registers optimization.
17133 This should be used for debugging purposes.
17134 In some where bugs in the pcode optimizer are found, users can benefit
17135 from temporarily disabling the optimizer until the bug is fixed.
17138 Preprocessor Macros
17141 PIC16 port defines the following preprocessor macros while translating a
17146 \begin_inset Tabular
17147 <lyxtabular version="3" rows="6" columns="2">
17149 <column alignment="center" valignment="top" leftline="true" width="0">
17150 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17151 <row topline="true" bottomline="true">
17152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17169 <row topline="true">
17170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17178 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17183 Port identification
17187 <row topline="true">
17188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17211 Port identification (same as above)
17215 <row topline="true">
17216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17229 MCU Identification.
17234 is the microcontrol identification number, i.e.
17239 <row topline="true">
17240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17263 MCU Identification (same as above)
17267 <row topline="true" bottomline="true">
17268 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17276 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17281 nnn = SMALL or LARGE respectively according to the stack model used
17292 In addition the following macros are defined when calling assembler:
17296 \begin_inset Tabular
17297 <lyxtabular version="3" rows="4" columns="2">
17299 <column alignment="center" valignment="top" leftline="true" width="0">
17300 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17301 <row topline="true" bottomline="true">
17302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17310 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17319 <row topline="true">
17320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17333 MCU Identification.
17338 is the microcontrol identification number, i.e.
17343 <row topline="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 memory model used for
17362 <row topline="true" bottomline="true">
17363 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17371 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17376 nnn = SMALL or LARGE respectively according to the stack model used
17391 \begin_inset LatexCommand \index{PIC16}
17395 port uses the following directories for searching header files and libraries.
17399 \begin_inset Tabular
17400 <lyxtabular version="3" rows="3" columns="4">
17402 <column alignment="center" valignment="top" leftline="true" width="0">
17403 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17404 <column alignment="center" valignment="top" width="0">
17405 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17406 <row topline="true" bottomline="true">
17407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17423 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17440 <row topline="true">
17441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17446 PREFIX/sdcc/include/pic16
17449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17454 PIC16 specific headers
17457 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17465 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17474 <row topline="true" bottomline="true">
17475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17480 PREFIX/sdcc/lib/pic16
17483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17488 PIC16 specific libraries
17491 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17516 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17523 PIC16 port currently supports the following pragmas:
17525 \labelwidthstring 00.00.0000
17527 stack pragma stack forces the code generator to initialize the stack & frame
17528 pointers at a specific address.
17529 This is an adhoc solution for cases where no STACK directive is available
17530 in the linker script or gplink is not instructed to create a stack section.
17532 The stack pragma should be used only once in a project.
17533 Multiple pragmas may result in indeterminate behaviour of the program.
17539 The old format (ie.
17540 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17541 cross page boundaries (or even exceed the available data RAM) and crash
17543 Make sure that stack does not cross page boundaries when using the SMALL
17549 The format is as follows:
17552 #pragma stack bottom_address [stack_size]
17559 is the lower bound of the stack section.
17560 The stack pointer initially will point at address (bottom_address+stack_size-1).
17568 /* initializes stack of 100 bytes at RAM address 0x200 */
17571 #pragma stack 0x200 100
17574 If the stack_size field is omitted then a stack is created with the default
17576 This size might be enough for most programs, but its not enough for operations
17577 with deep function nesting or excessive stack usage.
17579 \labelwidthstring 00.00.0000
17583 This pragma is deprecated.
17584 Its use will cause a warning message to be issued.
17590 \labelwidthstring 00.00.0000
17592 code place a function symbol at static FLASH address
17600 /* place function test_func at 0x4000 */
17603 #pragma code test_func 0x4000
17607 \labelwidthstring 00.00.0000
17609 library instructs the linker to use a library module.
17614 #pragma library module_name
17621 can be any library or object file (including its path).
17622 Note that there are four reserved keywords which have special meaning.
17627 \begin_inset Tabular
17628 <lyxtabular version="3" rows="6" columns="3">
17630 <column alignment="center" valignment="top" leftline="true" width="0">
17631 <column alignment="block" valignment="top" leftline="true" width="20page%">
17632 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17633 <row topline="true" bottomline="true">
17634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17659 <row topline="true">
17660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17675 ignore all library pragmas
17678 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17689 <row topline="true">
17690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17721 <row topline="true">
17722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17737 link the Math libarary
17740 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17753 <row topline="true">
17754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17769 link the I/O library
17772 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17785 <row topline="true" bottomline="true">
17786 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17801 link the debug library
17804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17823 * is the device number, i.e.
17824 452 for PIC18F452 MCU.
17827 This feature allows for linking with specific libraries withoug having to
17828 explicit name them in the command line.
17833 keyword will reject all modules specified by the library pragma.
17835 \labelwidthstring 00.00.0000
17837 udata pragma udata instructs the compiler to emit code so that linker will
17838 place a variable at a specific memory bank
17846 /* places variable foo at bank2 */
17849 #pragma udata bank2 foo
17855 In order for this pragma to work extra SECTION directives should be added
17856 in the .lkr script.
17857 In the following example a sample .lkr file is shown:
17862 // Sample linker script for the PIC18F452 processor
17868 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17871 CODEPAGE NAME=page START=0x2A END=0x7FFF
17874 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17877 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17880 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17883 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17886 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17891 DATABANK NAME=gpr0 START=0x80 END=0xFF
17894 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17897 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17900 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17903 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17906 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17909 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17914 SECTION NAME=CONFIG ROM=config
17919 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17922 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17925 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17928 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17931 SECTION NAME=bank4 RAM=gpr4
17934 SECTION NAME=bank5 RAM=gpr5
17937 The linker will recognise the section name set in the pragma statement and
17938 will position the variable at the memory bank set with the RAM field at
17939 the SECTION line in the linker script file.
17943 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17950 There is one main header file that can be included to the source files using
17957 This header file contains the definitions for the processor special registers,
17958 so it is necessary if the source accesses them.
17959 It can be included by adding the following line in the beginning of the
17963 #include <pic18fregs.h>
17966 The specific microcontroller is selected within the pic18fregs.h automatically,
17967 so the same source can be used with a variety of devices.
17973 The libraries that PIC16
17974 \begin_inset LatexCommand \index{PIC16}
17978 port depends on are the microcontroller device libraries which contain
17979 the symbol definitions for the microcontroller special function registers.
17980 These libraries have the format pic18fxxxx.lib, where
17984 is the microcontroller identification number.
17985 The specific library is selected automatically by the compiler at link
17986 stage according to the selected device.
17989 Libraries are created with gplib which is part of the gputils package
17990 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17995 \layout Subsubsection*
17997 Building the libraries
18000 Before using SDCC/pic16 there are some libraries that need to be compiled.
18001 This process is not done automatically by SDCC since not all users use
18002 SDCC for pic16 projects.
18003 So each user should compile the libraries separately.
18006 The steps to compile the pic16 libraries under Linux are:
18009 cd device/lib/pic16
18024 su -c 'make install' # install the libraries, you need the root password
18027 If you need to install the headers too, do:
18033 su -c 'make install' # install the headers, you need the root password
18036 There exist a special target to build the I/O libraries.
18037 This target is not automatically build because it will build the I/O library
18043 This way building will take quite a lot of time.
18044 Users are advised to edit the
18046 device/lib/pic16/pics.build
18048 file and then execute:
18057 The following memory models are supported by the PIC16 port:
18066 Memory model affects the default size of pointers within the source.
18067 The sizes are shown in the next table:
18071 \begin_inset Tabular
18072 <lyxtabular version="3" rows="3" columns="3">
18074 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18075 <column alignment="center" valignment="top" leftline="true" width="0">
18076 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18077 <row topline="true" bottomline="true">
18078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18083 Pointer sizes according to memory model
18086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18103 <row topline="true" bottomline="true">
18104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18112 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18120 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18129 <row topline="true" bottomline="true">
18130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18138 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18146 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18162 It is advisable that all sources within a project are compiled with the
18164 If one wants to override the default memory model, this can be done by
18165 declaring a pointer as
18174 Far selects large memory model's pointers, while near selects small memory
18178 The standard device libraries (see
18179 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18183 ) contain no reference to pointers, so they can be used with both memory
18190 The stack implementation for the PIC16 port uses two indirect registers,
18193 \labelwidthstring 00.00.0000
18195 FSR1 is assigned as stack pointer
18197 \labelwidthstring 00.00.0000
18199 FSR2 is assigned as frame pointer
18202 The following stack models are supported by the PIC16 port
18223 model means that only the FSRxL byte is used to access stack and frame,
18230 uses both FSRxL and FSRxH registers.
18231 The following table shows the stack/frame pointers sizes according to stack
18232 model and the maximum space they can address:
18236 \begin_inset Tabular
18237 <lyxtabular version="3" rows="3" columns="3">
18239 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18240 <column alignment="center" valignment="top" leftline="true" width="0">
18241 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18242 <row topline="true" bottomline="true">
18243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18248 Stack & Frame pointer sizes according to stack model
18251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18268 <row topline="true">
18269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18294 <row topline="true" bottomline="true">
18295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18331 stack model is currently not working properly throughout the code generator.
18332 So its use is not advised.
18333 Also there are some other points that need special care:
18338 Do not create stack sections with size more than one physical bank (that
18342 Stack sections should no cross physical bank limits (i.e.
18343 #pragma stack 0x50 0x100)
18346 These limitations are caused by the fact that only FSRxL is modified when
18347 using SMALL stack model, so no more than 256 bytes of stack can be used.
18348 This problem will disappear after LARGE model is fully implemented.
18354 In addition to the standard SDCC function keywords, PIC16 port makes available
18357 \labelwidthstring 00.00.0000
18359 wparam Use the WREG to pass one byte of the first function argument.
18360 This improves speed but you may not use this for functions with arguments
18361 that are called via function pointers, otherwise the first byte of the
18362 first parameter will get lost.
18366 void func_wparam(int a) wparam
18372 /* WREG hold the lower part of a */
18375 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18385 This keyword replaces the deprecated wparam pragma.
18387 \labelwidthstring 00.00.0000
18389 shadowregs When entering/exiting an ISR, it is possible to take advantage
18390 of the PIC18F hardware shadow registers which hold the values of WREG,
18391 STATUS and BSR registers.
18392 This can be done by adding the keyword
18400 keyword in the function's header.
18403 void isr_shadow(void) shadowregs interrupt 1
18419 instructs the code generator not to store/restore WREG, STATUS, BSR when
18420 entering/exiting the ISR.
18423 Function return values
18426 Return values from functions are placed to the appropriate registers following
18427 a modified Microchip policy optimized for SDCC.
18428 The following table shows these registers:
18432 \begin_inset Tabular
18433 <lyxtabular version="3" rows="6" columns="2">
18435 <column alignment="center" valignment="top" leftline="true" width="0">
18436 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18437 <row topline="true" bottomline="true">
18438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18451 destination register
18455 <row topline="true">
18456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18473 <row topline="true">
18474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18491 <row topline="true">
18492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18509 <row topline="true">
18510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18523 FSR0L:PRODH:PRODL:WREG
18527 <row topline="true" bottomline="true">
18528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18536 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18541 on stack, FSR0 points to the beginning
18555 An interrupt servive routine (ISR) is declared using the
18562 void isr(void) interrupt
18580 is the interrupt number, which for PIC18F devices can be:
18584 \begin_inset Tabular
18585 <lyxtabular version="3" rows="4" columns="3">
18587 <column alignment="center" valignment="top" leftline="true" width="0">
18588 <column alignment="center" valignment="top" leftline="true" width="0">
18589 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18590 <row topline="true" bottomline="true">
18591 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18601 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18614 Interrupt Vector Address
18618 <row topline="true">
18619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18627 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18635 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18644 <row topline="true">
18645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18662 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18676 HIGH priority interrupts
18679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18688 <row topline="true" bottomline="true">
18689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18702 LOW priority interrupts
18705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18721 When generating assembly code for ISR the code generator places a
18727 Interrupt Vector Address
18729 which points at the genetated ISR.
18730 This single GOTO instruction is part of an automatically generated
18732 interrupt entry point
18735 The actuall ISR code is placed as normally would in the code space.
18736 Upon interrupt request, the GOTO instruction is executed which jumps to
18738 When declaring interrupt functions as _naked this GOTO instruction is
18743 The whole interrupt functions is therefore placed at the Interrupt Vector
18744 Address of the specific interrupt.
18745 This is not a problem for the LOW priority interrupts, but it is a problem
18746 for the RESET and the HIGH priority interrupts because code may be written
18747 at the next interrupt´s vector address and cause undeterminate program
18748 behaviour if that interrupt is raised.
18754 This is not a problem when
18757 this is a HIGH interrupt ISR and LOW interrupts are
18764 when the ISR is small enough not to reach the next interrupt´s vector address.
18774 is possible to be omitted.
18775 This way a function is generated similar to an ISR, but it is not assigned
18779 When entering an interrupt, currently the PIC16
18780 \begin_inset LatexCommand \index{PIC16}
18784 port automatically saves the following registers:
18796 PROD (PRODL and PRODH)
18799 FSR0 (FSR0L and FSR0H)
18802 These registers are restored upon return from the interrupt routine.
18808 NOTE that when the _naked attribute is specified for an interrupt routine,
18809 then NO registers are stored or restored.
18818 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18819 There are 3 types of generic pointers currently implemented data, code
18820 and eeprom pointers.
18821 They are differentiated by the value of the 7th and 6th bits of the upper
18826 \begin_inset Tabular
18827 <lyxtabular version="3" rows="5" columns="5">
18829 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18830 <column alignment="center" valignment="top" width="0">
18831 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18832 <column alignment="center" valignment="top" width="0">
18833 <column alignment="left" valignment="top" rightline="true" width="0">
18834 <row topline="true" bottomline="true">
18835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18859 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18864 rest of the pointer
18867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18876 <row topline="true" bottomline="true">
18877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18901 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18910 uuuuuu uuuuxxxx xxxxxxxx
18913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18918 a 12-bit data pointer in data RAM memory
18922 <row bottomline="true">
18923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18939 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18947 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18956 uxxxxx xxxxxxxx xxxxxxxx
18959 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18964 a 21-bit code pointer in FLASH memory
18968 <row bottomline="true">
18969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18985 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19002 uuuuuu uuuuuuxx xxxxxxxx
19005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19010 a 10-bit eeprom pointer in EEPROM memory
19014 <row bottomline="true">
19015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19023 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19031 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19039 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19048 xxxxxx xxxxxxxx xxxxxxxx
19051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19056 unimplemented pointer type
19067 Generic pointer are read and written with a set of library functions which
19068 read/write 1, 2, 3, 4 bytes.
19072 \layout Subsubsection
19074 Standard I/O Streams
19081 the type FILE is defined as:
19084 typedef char * FILE;
19087 This type is the stream type implemented I/O in the PIC18F devices.
19088 Also the standard input and output streams are declared in stdio.h:
19091 extern FILE * stdin;
19094 extern FILE * stdout;
19097 The FILE type is actually a generic pointer which defines one more type
19098 of generic pointers, the
19103 This new type has the format:
19107 \begin_inset Tabular
19108 <lyxtabular version="3" rows="2" columns="7">
19110 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19111 <column alignment="center" valignment="top" width="0">
19112 <column alignment="center" valignment="top" leftline="true" width="0">
19113 <column alignment="center" valignment="top" leftline="true" width="0">
19114 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19115 <column alignment="center" valignment="top" width="0">
19116 <column alignment="left" valignment="top" rightline="true" width="0">
19117 <row topline="true" bottomline="true">
19118 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19150 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19163 rest of the pointer
19166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19175 <row topline="true" bottomline="true">
19176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19208 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19233 upper byte high nubble is 0x2n, the rest are zeroes
19244 Currently implemented there are 3 types of streams defined:
19248 \begin_inset Tabular
19249 <lyxtabular version="3" rows="4" columns="4">
19251 <column alignment="center" valignment="top" leftline="true" width="0">
19252 <column alignment="center" valignment="top" leftline="true" width="0">
19253 <column alignment="center" valignment="top" leftline="true" width="0">
19254 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19255 <row topline="true" bottomline="true">
19256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19289 <row topline="true">
19290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19321 Writes/Reads characters via the USART peripheral
19325 <row topline="true">
19326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19357 Writes/Reads characters via the MSSP peripheral
19361 <row topline="true" bottomline="true">
19362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19388 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19393 Writes/Reads characters via used defined functions
19404 The stream identifiers are declared as macros in the stdio.h header.
19407 In the libc library there exist the functions that are used to write to
19408 each of the above streams.
19411 \labelwidthstring 00.00.0000
19423 _stream_usart_putchar writes a character at the USART stream
19425 \labelwidthstring 00.00.0000
19437 _stream_mssp_putchar writes a character at the MSSP stream
19439 \labelwidthstring 00.00.0000
19441 putchar dummy function.
19442 This writes a character to a user specified manner.
19445 In order to increase performance
19449 is declared in stdio.h as having its parameter in WREG (it has the wparam
19451 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19452 in a user-friendly way.
19457 is the name of the variable that holds the character to print.
19458 An example follows:
19461 #include <pic18fregs.h>
19473 PORTA = c; /* dump character c to PORTA */
19486 stdout = STREAM_USER; /* this is not necessery, since stdout points
19489 * by default to STREAM_USER */
19492 printf (¨This is a printf test
19500 \layout Subsubsection
19505 PIC16 contains an implementation of the printf-family of functions.
19506 There exist the following functions:
19509 extern unsigned int sprintf(char *buf, char *fmt, ...);
19512 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19517 extern unsigned int printf(char *fmt, ...);
19520 extern unsigned int vprintf(char *fmt, va_lista ap);
19525 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19528 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19531 For sprintf and vsprintf
19535 should normally be a data pointer where the resulting string will be placed.
19536 No range checking is done so the user should allocate the necessery buffer.
19537 For fprintf and vfprintf
19541 should be a stream pointer (i.e.
19542 stdout, STREAM_MSSP, etc...).
19543 \layout Subsubsection
19548 The PIC18F family of microcontrollers supports a number of interrupt sources.
19549 A list of these interrupts is shown in the following table:
19553 \begin_inset Tabular
19554 <lyxtabular version="3" rows="11" columns="4">
19556 <column alignment="left" valignment="top" leftline="true" width="0">
19557 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19558 <column alignment="left" valignment="top" leftline="true" width="0">
19559 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19560 <row topline="true" bottomline="true">
19561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19577 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19594 <row topline="true">
19595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19603 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19608 PORTB change interrupt
19611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19619 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19624 EEPROM/FLASH write complete interrupt
19628 <row topline="true">
19629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19642 INT0 external interrupt
19645 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19653 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19658 Bus collision interrupt
19662 <row topline="true">
19663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19676 INT1 external interrupt
19679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19687 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19692 Low voltage detect interrupt
19696 <row topline="true">
19697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19710 INT2 external interrupt
19713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19726 Parallel slave port interrupt
19730 <row topline="true">
19731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19744 CCP1 module interrupt
19747 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19755 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19760 AD convertion complete interrupt
19764 <row topline="true">
19765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19778 CCP2 module interrupt
19781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19794 USART receive interrupt
19798 <row topline="true">
19799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19812 TMR0 overflow interrupt
19815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19823 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19828 USART transmit interrupt
19832 <row topline="true">
19833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19846 TMR1 overflow interrupt
19849 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19857 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19862 SSP receive/transmit interrupt
19866 <row topline="true">
19867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19880 TMR2 matches PR2 interrupt
19883 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19898 <row topline="true" bottomline="true">
19899 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19907 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19912 TMR3 overflow interrupt
19915 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19922 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19937 The prototypes for these names are defined in the header file
19944 In order to simplify signal handling, a number of macros is provided:
19946 \labelwidthstring 00.00.0000
19948 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19949 high priority interrupts.
19954 is the function name to use.
19956 \labelwidthstring 00.00.0000
19958 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19959 low priority interrupt.
19964 is the function name to use.
19966 \labelwidthstring 00.00.0000
19968 DEF_HANDLER(sig,handler) define a handler for signal
19972 \labelwidthstring 00.00.0000
19974 END_DEF end the declaration of the dispatch table.
19977 Additionally there are two more macros to simplify the declaration of the
19980 \labelwidthstring 00.00.0000
19984 SIGHANDLER(handler)
19986 this declares the function prototype for the
19992 \labelwidthstring 00.00.0000
19994 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19997 An example of using the macros above is shown below:
20000 #include <pic18fregs.h>
20003 #include <signal.h>
20007 DEF_INTHIGH(high_int)
20010 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20013 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20020 SIGHANDLER(_tmr0_handler)
20026 /* action to be taken when timer 0 overflows */
20033 SIGHANDLERNAKED(_bcol_handler)
20042 /* action to be taken when bus collision occurs */
20058 Special care should be taken when using the above scheme:
20061 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20064 when declaring SIGHANDLERNAKED handler never forget to use
20068 for proper returning.
20074 Here you can find some general tips for compiling programs with SDCC/pic16.
20075 \layout Subsubsection
20080 The default stack size (that is 64 bytes) probably is enough for many programs.
20081 One must take care that when there are many levels of function nesting,
20082 or there is excessive usage of stack, its size should be extended.
20083 An example of such a case is the printf/sprintf family of functions.
20084 If you encounter problems like not being able to print integers, then you
20085 need to set the stack size around the maximum (256 for small stack model).
20086 The following diagram shows what happens when calling printf to print an
20090 printf () --> ltoa () --> ultoa () --> divschar ()
20093 It is should be understood that stack is easily consumed when calling complicate
20095 Using command line arguments like -
20105 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20107 Other ways to reduce stack usage may exist.
20110 Debugging with SDCDB
20111 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20116 \begin_inset LatexCommand \index{sdcdb (debugger)}
20123 SDCC is distributed with a source level debugger
20124 \begin_inset LatexCommand \index{Debugger}
20129 The debugger uses a command line interface, the command repertoire of the
20130 debugger has been kept as close to gdb
20131 \begin_inset LatexCommand \index{gdb}
20135 (the GNU debugger) as possible.
20136 The configuration and build process is part of the standard compiler installati
20137 on, which also builds and installs the debugger in the target directory
20138 specified during configuration.
20139 The debugger allows you debug BOTH at the C source and at the ASM source
20141 Sdcdb is currently not available on Win32 platforms.
20144 Compiling for Debugging
20158 \begin_inset LatexCommand \index{-\/-debug}
20162 option must be specified for all files for which debug information is to
20164 The complier generates a .adb file for each of these files.
20165 The linker creates the .cdb
20166 \begin_inset LatexCommand \index{<file>.cdb}
20171 \begin_inset LatexCommand \index{<file>.adb}
20175 files and the address information.
20176 This .cdb is used by the debugger.
20179 How the Debugger Works
20192 -debug option is specified the compiler generates extra symbol information
20193 some of which are put into the assembler source and some are put into the
20195 Then the linker creates the .cdb file from the individual .adb files with
20196 the address information for the symbols.
20197 The debugger reads the symbolic information generated by the compiler &
20198 the address information generated by the linker.
20199 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20200 execution is controlled by the debugger.
20201 When a command is issued for the debugger, it translates it into appropriate
20202 commands for the simulator.
20205 Starting the Debugger
20208 The debugger can be started using the following command line.
20209 (Assume the file you are debugging has the file name foo).
20223 The debugger will look for the following files.
20226 foo.c - the source file.
20229 foo.cdb - the debugger symbol information file.
20232 foo.ihx - the Intel hex format
20233 \begin_inset LatexCommand \index{Intel hex format}
20240 Command Line Options
20253 -directory=<source file directory> this option can used to specify the directory
20255 The debugger will look into the directory list specified for source, cdb
20257 The items in the directory list must be separated by ':', e.g.
20258 if the source files can be in the directories /home/src1 and /home/src2,
20269 -directory option should be -
20279 -directory=/home/src1:/home/src2.
20280 Note there can be no spaces in the option.
20284 -cd <directory> - change to the <directory>.
20287 -fullname - used by GUI front ends.
20290 -cpu <cpu-type> - this argument is passed to the simulator please see the
20291 simulator docs for details.
20294 -X <Clock frequency > this options is passed to the simulator please see
20295 the simulator docs for details.
20298 -s <serial port file> passed to simulator see the simulator docs for details.
20301 -S <serial in,out> passed to simulator see the simulator docs for details.
20304 -k <port number> passed to simulator see the simulator docs for details.
20310 As mentioned earlier the command interface for the debugger has been deliberatel
20311 y kept as close the GNU debugger gdb, as possible.
20312 This will help the integration with existing graphical user interfaces
20313 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20314 If you use a graphical user interface for the debugger you can skip this
20316 \layout Subsubsection*
20318 break [line | file:line | function | file:function]
20321 Set breakpoint at specified line or function:
20330 sdcdb>break foo.c:100
20332 sdcdb>break funcfoo
20334 sdcdb>break foo.c:funcfoo
20335 \layout Subsubsection*
20337 clear [line | file:line | function | file:function ]
20340 Clear breakpoint at specified line or function:
20349 sdcdb>clear foo.c:100
20351 sdcdb>clear funcfoo
20353 sdcdb>clear foo.c:funcfoo
20354 \layout Subsubsection*
20359 Continue program being debugged, after breakpoint.
20360 \layout Subsubsection*
20365 Execute till the end of the current function.
20366 \layout Subsubsection*
20371 Delete breakpoint number 'n'.
20372 If used without any option clear ALL user defined break points.
20373 \layout Subsubsection*
20375 info [break | stack | frame | registers ]
20378 info break - list all breakpoints
20381 info stack - show the function call stack.
20384 info frame - show information about the current execution frame.
20387 info registers - show content of all registers.
20388 \layout Subsubsection*
20393 Step program until it reaches a different source line.
20394 Note: pressing <return> repeats the last command.
20395 \layout Subsubsection*
20400 Step program, proceeding through subroutine calls.
20401 \layout Subsubsection*
20406 Start debugged program.
20407 \layout Subsubsection*
20412 Print type information of the variable.
20413 \layout Subsubsection*
20418 print value of variable.
20419 \layout Subsubsection*
20424 load the given file name.
20425 Note this is an alternate method of loading file for debugging.
20426 \layout Subsubsection*
20431 print information about current frame.
20432 \layout Subsubsection*
20437 Toggle between C source & assembly source.
20438 \layout Subsubsection*
20440 ! simulator command
20443 Send the string following '!' to the simulator, the simulator response is
20445 Note the debugger does not interpret the command being sent to the simulator,
20446 so if a command like 'go' is sent the debugger can loose its execution
20447 context and may display incorrect values.
20448 \layout Subsubsection*
20455 My name is Bobby Brown"
20458 Interfacing with DDD
20461 The screenshot was converted from png to eps with:
20462 \begin_inset Quotes sld
20465 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20466 \begin_inset Quotes srd
20469 which produces a pretty compact eps file which is free from compression
20473 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20474 as this broke the build system on Sourceforge (pdf-file was broken).
20480 \begin_inset LatexCommand \url{http://cvs.sourceforge.net/viewcvs.py/*checkout*/sdcc/sdcc/doc/figures/ddd_example.eps}
20486 shows a screenshot of a debugging session with DDD
20487 \begin_inset LatexCommand \index{DDD (debugger)}
20491 (Unix only) on a simulated 8032.
20492 The debugging session might not run as smoothly as the screenshot suggests.
20493 The debugger allows setting of breakpoints, displaying and changing variables,
20494 single stepping through C and assembler code.
20497 The source was compiled with
20520 -debug ddd_example.c
20533 and DDD was invoked with
20540 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20543 Interfacing with XEmacs
20544 \begin_inset LatexCommand \index{XEmacs}
20549 \begin_inset LatexCommand \index{Emacs}
20556 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20557 sdcdb.el and sdcdbsrc.el.
20558 These two files can be found in the $(prefix)/bin directory after the installat
20560 These files need to be loaded into XEmacs for the interface to work.
20561 This can be done at XEmacs startup time by inserting the following into
20562 your '.xemacs' file (which can be found in your HOME directory):
20568 (load-file sdcdbsrc.el)
20574 .xemacs is a lisp file so the () around the command is REQUIRED.
20575 The files can also be loaded dynamically while XEmacs is running, set the
20576 environment variable 'EMACSLOADPATH' to the installation bin directory
20577 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20578 To start the interface enter the following command:
20592 You will prompted to enter the file name to be debugged.
20597 The command line options that are passed to the simulator directly are bound
20598 to default values in the file sdcdbsrc.el.
20599 The variables are listed below, these values maybe changed as required.
20602 sdcdbsrc-cpu-type '51
20605 sdcdbsrc-frequency '11059200
20608 sdcdbsrc-serial nil
20611 The following is a list of key mapping for the debugger interface.
20622 ;;key\SpecialChar ~
20636 binding\SpecialChar ~
20660 ;;---\SpecialChar ~
20674 -------\SpecialChar ~
20716 sdcdb-next-from-src\SpecialChar ~
20744 sdcdb-back-from-src\SpecialChar ~
20772 sdcdb-cont-from-src\SpecialChar ~
20782 SDCDB continue command
20800 sdcdb-step-from-src\SpecialChar ~
20828 sdcdb-whatis-c-sexp\SpecialChar ~
20838 SDCDB ptypecommand for data at
20905 sdcdbsrc-delete\SpecialChar ~
20919 SDCDB Delete all breakpoints if no arg
20968 given or delete arg (C-u arg x)
20986 sdcdbsrc-frame\SpecialChar ~
21001 SDCDB Display current frame if no arg,
21050 given or display frame arg
21117 sdcdbsrc-goto-sdcdb\SpecialChar ~
21127 Goto the SDCDB output buffer
21145 sdcdb-print-c-sexp\SpecialChar ~
21156 SDCDB print command for data at
21223 sdcdbsrc-goto-sdcdb\SpecialChar ~
21233 Goto the SDCDB output buffer
21251 sdcdbsrc-mode\SpecialChar ~
21267 Toggles Sdcdbsrc mode (turns it off)
21282 sdcdb-finish-from-src\SpecialChar ~
21290 SDCDB finish command
21305 sdcdb-break\SpecialChar ~
21323 Set break for line with point
21338 sdcdbsrc-mode\SpecialChar ~
21354 Toggle Sdcdbsrc mode
21369 sdcdbsrc-srcmode\SpecialChar ~
21392 Here are a few guidelines that will help the compiler generate more efficient
21393 code, some of the tips are specific to this compiler others are generally
21394 good programming practice.
21397 Use the smallest data type to represent your data-value.
21398 If it is known in advance that the value is going to be less than 256 then
21399 use an 'unsigned char' instead of a 'short' or 'int'.
21400 Please note, that ANSI C requires both signed and unsigned chars to be
21401 promoted to 'signed int' before doing any operation.
21403 \begin_inset LatexCommand \index{type promotion}
21407 can be omitted, if the result is the same.
21408 The effect of the promotion rules together with the sign-extension is often
21415 unsigned char uc = 0xfe;
21417 if (uc * uc < 0) /* this is true! */
21436 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21446 (unsigned char) -12 / (signed char) -3 = ...
21449 No, the result is not 4:
21454 (int) (unsigned char) -12 / (int) (signed char) -3 =
21456 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21458 (int) 0x00f4 / (int) 0xfffd =
21460 (int) 0x00f4 / (int) 0xfffd =
21462 (int) 244 / (int) -3 =
21464 (int) -81 = (int) 0xffaf;
21467 Don't complain, that gcc gives you a different result.
21468 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21469 Therefore the results are different.
21472 \begin_inset Quotes sld
21476 \begin_inset Quotes srd
21482 If well-defined overflow characteristics are important and negative values
21483 are not, or if you want to steer clear of sign-extension problems when
21484 manipulating bits or bytes, use one of the corresponding unsigned types.
21485 (Beware when mixing signed and unsigned values in expressions, though.)
21487 Although character types (especially unsigned char) can be used as "tiny"
21488 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21489 ble sign extension and increased code size.
21493 Use unsigned when it is known in advance that the value is not going to
21495 This helps especially if you are doing division or multiplication, bit-shifting
21496 or are using an array index.
21499 NEVER jump into a LOOP.
21502 Declare the variables to be local
21503 \begin_inset LatexCommand \index{local variables}
21507 whenever possible, especially loop control variables (induction).
21510 Since the compiler does not always do implicit integral promotion, the programme
21511 r should do an explicit cast when integral promotion is required.
21514 Reducing the size of division, multiplication & modulus operations can reduce
21515 code size substantially.
21516 Take the following code for example.
21522 foobar(unsigned int p1, unsigned char ch)
21530 unsigned char ch1 = p1 % ch ;
21541 For the modulus operation the variable ch will be promoted to unsigned int
21542 first then the modulus operation will be performed (this will lead to a
21543 call to support routine _moduint()), and the result will be casted to a
21545 If the code is changed to
21550 foobar(unsigned int p1, unsigned char ch)
21558 unsigned char ch1 = (unsigned char)p1 % ch ;
21569 It would substantially reduce the code generated (future versions of the
21570 compiler will be smart enough to detect such optimization opportunities).
21574 Have a look at the assembly listing to get a
21575 \begin_inset Quotes sld
21579 \begin_inset Quotes srd
21582 for the code generation.
21586 \begin_inset LatexCommand \index{Tools}
21590 included in the distribution
21594 \begin_inset Tabular
21595 <lyxtabular version="3" rows="12" columns="3">
21597 <column alignment="center" valignment="top" leftline="true" width="0pt">
21598 <column alignment="center" valignment="top" leftline="true" width="0pt">
21599 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21600 <row topline="true" bottomline="true">
21601 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21609 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21617 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21626 <row topline="true">
21627 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21640 Simulator for various architectures
21643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21652 <row topline="true">
21653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21666 header file conversion
21669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21674 sdcc/support/scripts
21678 <row topline="true">
21679 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21692 header file conversion
21695 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21700 sdcc/support/scripts
21704 <row topline="true">
21705 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21739 <row topline="true">
21740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21748 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21774 <row topline="true">
21775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21809 <row topline="true">
21810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21844 <row topline="true">
21845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21861 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21879 <row topline="true">
21880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21888 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21896 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21914 <row topline="true">
21915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21931 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21949 <row topline="true" bottomline="true">
21950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21994 \begin_inset LatexCommand \index{Documentation}
21998 included in the distribution
22002 \begin_inset Tabular
22003 <lyxtabular version="3" rows="10" columns="2">
22005 <column alignment="left" valignment="top" leftline="true" width="0">
22006 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22007 <row topline="true" bottomline="true">
22008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22016 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22021 Where to get / filename
22025 <row topline="true">
22026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22031 SDCC Compiler User Guide
22034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22039 You're reading it right now
22043 <row topline="true">
22044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22061 <row topline="true">
22062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22068 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22073 \begin_inset LatexCommand \index{Assembler documentation}
22077 Assemblers and ASLINK
22078 \begin_inset LatexCommand \index{aslink}
22083 \begin_inset LatexCommand \index{Linker documentation}
22090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22095 sdcc/as/doc/asxhtm.html
22099 <row topline="true">
22100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22105 SDCC regression test
22106 \begin_inset LatexCommand \index{Regression test}
22113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22118 sdcc/doc/test_suite_spec.pdf
22122 <row topline="true">
22123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22140 <row topline="true">
22141 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22146 Notes on debugging with sdcdb
22147 \begin_inset LatexCommand \index{sdcdb (debugger)}
22154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22159 sdcc/debugger/README
22163 <row topline="true">
22164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22169 Software simulator for microcontrollers
22172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22199 <row topline="true">
22200 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22205 Temporary notes on the pic16
22206 \begin_inset LatexCommand \index{PIC16}
22213 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22218 sdcc/src/pic16/NOTES
22222 <row topline="true" bottomline="true">
22223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22228 SDCC internal documentation (debugging file format)
22231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22267 Related open source tools
22268 \begin_inset LatexCommand \index{Related tools}
22276 \begin_inset Tabular
22277 <lyxtabular version="3" rows="11" columns="3">
22279 <column alignment="center" valignment="top" leftline="true" width="0pt">
22280 <column alignment="block" valignment="top" leftline="true" width="30line%">
22281 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22282 <row topline="true" bottomline="true">
22283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22308 <row topline="true">
22309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22315 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22322 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22330 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22336 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22344 <row topline="true">
22345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22351 \begin_inset LatexCommand \index{gputils (pic tools)}
22358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22366 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22372 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22380 <row topline="true">
22381 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22389 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22397 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22403 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22411 <row topline="true">
22412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22418 \begin_inset LatexCommand \index{indent (source formatting tool)}
22425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22430 Formats C source - Master of the white spaces
22433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22439 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22447 <row topline="true">
22448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22454 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22461 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22466 Object file conversion, checksumming, ...
22469 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22475 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22483 <row topline="true">
22484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22490 \begin_inset LatexCommand \index{objdump (tool)}
22497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22502 Object file conversion, ...
22505 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22510 Part of binutils (should be there anyway)
22514 <row topline="true">
22515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22521 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22533 Source code documentation system
22536 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22542 \begin_inset LatexCommand \url{http://www.doxygen.org}
22550 <row topline="true">
22551 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22564 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22567 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22573 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22581 <row topline="true">
22582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22588 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22600 Statically checks c sources (see
22601 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22608 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22614 \begin_inset LatexCommand \url{http://www.splint.org}
22622 <row topline="true" bottomline="true">
22623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22629 \begin_inset LatexCommand \index{ddd (debugger)}
22636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22641 Debugger, serves nicely as GUI to sdcdb
22642 \begin_inset LatexCommand \index{sdcdb (debugger)}
22649 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22655 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22672 Related documentation / recommended reading
22676 \begin_inset Tabular
22677 <lyxtabular version="3" rows="6" columns="3">
22679 <column alignment="center" valignment="top" leftline="true" width="0pt">
22680 <column alignment="block" valignment="top" leftline="true" width="30line%">
22681 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22682 <row topline="true" bottomline="true">
22683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22699 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22708 <row topline="true">
22709 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22732 \begin_inset LatexCommand \index{C Reference card}
22739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22745 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22753 <row topline="true">
22754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22776 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22784 <row topline="true">
22785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22792 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22797 Latest datasheet of the target CPU
22800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22809 <row topline="true">
22810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22822 Revision history of datasheet
22825 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22834 <row topline="true" bottomline="true">
22835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22850 Advanced Compiler Design and Implementation
22853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22858 bookstore (very dedicated, probably read other books first)
22874 Some questions answered, some pointers given - it might be time to in turn
22882 can you solve your project with the selected microcontroller? Would you
22883 find out early or rather late that your target is too small/slow/whatever?
22884 Can you switch to a slightly better device if it doesn't fit?
22887 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22888 and/or another programming language be more adequate? Would an operating
22889 system on the target device help?
22892 if you solved the problem, will the marketing department be happy?
22895 if the marketing department is happy, will customers be happy?
22898 if you're the project manager, marketing department and maybe even the customer
22899 in one person, have you tried to see the project from the outside?
22902 is the project done if you think it is done? Or is just that other interface/pro
22903 tocol/feature/configuration/option missing? How about website, manual(s),
22904 internationali(z|s)ation, packaging, labels, 2nd source for components,
22905 electromagnetic compatability/interference, documentation for production,
22906 production test software, update mechanism, patent issues?
22909 is your project adequately positioned in that magic triangle: fame, fortune,
22913 Maybe not all answers to these questions are known and some answers may
22918 , nevertheless knowing these questions may help you to avoid burnout
22924 burnout is bad for electronic devices, programmers and motorcycle tyres
22928 Chances are you didn't want to hear some of them...
22932 \begin_inset LatexCommand \index{Support}
22939 SDCC has grown to be a large project.
22940 The compiler alone (without the preprocessor, assembler and linker) is
22941 well over 100,000 lines of code (blank stripped).
22942 The open source nature of this project is a key to its continued growth
22944 You gain the benefit and support of many active software developers and
22946 Is SDCC perfect? No, that's why we need your help.
22947 The developers take pride in fixing reported bugs.
22948 You can help by reporting the bugs and helping other SDCC users.
22949 There are lots of ways to contribute, and we encourage you to take part
22950 in making SDCC a great software package.
22954 The SDCC project is hosted on the SDCC sourceforge site at
22955 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22960 You'll find the complete set of mailing lists
22961 \begin_inset LatexCommand \index{Mailing list(s)}
22965 , forums, bug reporting system, patch submission
22966 \begin_inset LatexCommand \index{Patch submission}
22971 \begin_inset LatexCommand \index{download}
22975 area and cvs code repository
22976 \begin_inset LatexCommand \index{cvs code repository}
22984 \begin_inset LatexCommand \index{Bug reporting}
22989 \begin_inset LatexCommand \index{Reporting bugs}
22996 The recommended way of reporting bugs is using the infrastructure of the
22998 You can follow the status of bug reports there and have an overview about
23002 Bug reports are automatically forwarded to the developer mailing list and
23003 will be fixed ASAP.
23004 When reporting a bug, it is very useful to include a small test program
23005 (the smaller the better) which reproduces the problem.
23006 If you can isolate the problem by looking at the generated assembly code,
23007 this can be very helpful.
23008 Compiling your program with the -
23019 \begin_inset LatexCommand \index{-\/-dumpall}
23023 option can sometimes be useful in locating optimization problems.
23024 When reporting a bug please maker sure you:
23027 Attach the code you are compiling with SDCC.
23031 Specify the exact command you use to run SDCC, or attach your Makefile.
23035 Specify the SDCC version (type "
23041 "), your platform, and operating system.
23045 Provide an exact copy of any error message or incorrect output.
23049 Put something meaningful in the subject of your message.
23052 Please attempt to include these 5 important parts, as applicable, in all
23053 requests for support or when reporting any problems or bugs with SDCC.
23054 Though this will make your message lengthy, it will greatly improve your
23055 chance that SDCC users and developers will be able to help you.
23056 Some SDCC developers are frustrated by bug reports without code provided
23057 that they can use to reproduce and ultimately fix the problem, so please
23058 be sure to provide sample code if you are reporting a bug!
23061 Please have a short check that you are using a recent version of SDCC and
23062 the bug is not yet known.
23063 This is the link for reporting bugs:
23064 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23071 Requesting Features
23072 \begin_inset LatexCommand \label{sub:Requesting-Features}
23077 \begin_inset LatexCommand \index{Feature request}
23082 \begin_inset LatexCommand \index{Requesting features}
23089 Like bug reports feature requests are forwarded to the developer mailing
23091 This is the link for requesting features:
23092 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23102 Like bug reports contributed patches are forwarded to the developer mailing
23104 This is the link for submitting patches
23105 \begin_inset LatexCommand \index{Patch submission}
23110 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23117 You need to specify some parameters to the
23121 command for the patches to be useful.
23122 If you modified more than one file a patch created f.e.
23127 \begin_inset Quotes sld
23130 diff -Naur unmodified_directory modified_directory >my_changes.patch
23131 \begin_inset Quotes srd
23137 will be fine, otherwise
23141 \begin_inset Quotes sld
23144 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23145 \begin_inset Quotes srd
23158 These links should take you directly to the
23159 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23169 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23170 automated messages (mid 2003)
23174 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23179 \begin_inset LatexCommand \index{Mailing list(s)}
23183 and forums are archived and searchable so if you are lucky someone already
23184 had a similar problem.
23185 While mails to the lists themselves are delivered promptly their web front
23186 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23187 if you're seriously using SDCC please consider subscribing to the lists.
23193 You can follow the status of the cvs version
23194 \begin_inset LatexCommand \index{version}
23198 of SDCC by watching the Changelog
23199 \begin_inset LatexCommand \index{Changelog}
23203 in the cvs-repository
23208 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
23216 \begin_inset LatexCommand \index{Release policy}
23223 Historically there often were long delays between official releases and
23224 the sourceforge download area tends to get not updated at all.
23225 Excuses in the past might have referred to problems with live range analysis,
23226 but as this was fixed a while ago, the current problem is that another
23227 excuse has to be found.
23228 Kidding aside, we have to get better there! On the other hand there are
23229 daily snapshots available at
23230 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23234 , and you can always build the very last version (hopefully with many bugs
23235 fixed, and features added) from the source code available at
23236 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23244 \begin_inset LatexCommand \index{Examples}
23251 You'll find some small examples in the directory
23253 sdcc/device/examples/.
23256 More examples and libraries are available at
23258 The SDCC Open Knowledge Resource
23259 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23266 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23273 I did insert a reference to Paul's web site here although it seems rather
23274 dedicated to a specific 8032 board (I think it's okay because it f.e.
23275 shows LCD/Harddisc interface and has a free 8051 monitor.
23276 Independent 8032 board vendors face hard competition of heavily subsidized
23277 development boards anyway).
23280 Maybe we should include some links to real world applications.
23281 Preferably pointer to pointers (one for each architecture) so this stays
23286 \begin_inset LatexCommand \index{Quality control}
23293 The compiler is passed through nightly compile and build checks.
23299 \begin_inset LatexCommand \index{Regression test}
23303 check that SDCC itself compiles flawlessly on several platforms and checks
23304 the quality of the code generated by SDCC by running the code through simulator
23306 There is a separate document
23309 \begin_inset LatexCommand \index{Test suite}
23318 You'll find the test code in the directory
23320 sdcc/support/regression
23323 You can run these tests manually by running
23327 in this directory (or f.e.
23332 \begin_inset Quotes sld
23336 \begin_inset Quotes srd
23342 if you don't want to run the complete tests).
23343 The test code might also be interesting if you want to look for examples
23344 \begin_inset LatexCommand \index{Examples}
23348 checking corner cases of SDCC or if you plan to submit patches
23349 \begin_inset LatexCommand \index{Patch submission}
23356 The pic port uses a different set of regression tests, you'll find them
23359 sdcc/src/regression
23364 Use of SDCC in Education
23377 the phrase "use in education" might evoke the association "
23381 fit for use in education".
23382 This connotation is not intended but nevertheless risked as the licensing
23383 of SDCC makes it difficult to offer educational discounts
23387 If your rationales are to:
23390 give students a chance to understand the
23394 steps of code generation
23397 have a curriculum that can be extended for years.
23398 Then you could use an fpga board as target and your curriculum will seamlessly
23399 extend from logic synthesis (
23400 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
23405 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
23409 ), over assembly programming, to C to FPGA compilers (
23410 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
23417 be able to insert excursions about skills like using a revision control
23418 system, submitting/applying patches, using a type-setting (as opposed to
23419 word-processing) engine LyX/LaTeX, using
23420 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
23425 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
23429 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
23430 Source Software, CPU simulation, compiler regression tests
23431 \begin_inset LatexCommand \index{Regression test}
23438 And if there should be a shortage of ideas then you can always point students
23439 to the ever-growing feature request list
23440 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23447 not tie students to a specific host platform and instead allow them to use
23452 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
23454 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
23461 not encourage students to use illegal copies of educational software
23464 be immune to licensing/availability/price changes of the chosen tool chain
23467 be able to change to a new target platform without having to adopt a new
23471 have complete control over and insight into the tool chain
23474 make your students aware about the pros and cons of open source software
23478 give back to the public as you are probably at least partially publically
23482 give students a chance to publically prove their skills and to possibly
23483 see a world wide impact
23486 then SDCC is probably among the first choices.
23487 Well, probably SDCC might be the only choice.
23490 SDCC Technical Data
23494 \begin_inset LatexCommand \index{Optimizations}
23501 SDCC performs a host of standard optimizations in addition to some MCU specific
23506 Sub-expression Elimination
23507 \begin_inset LatexCommand \index{Subexpression elimination}
23514 The compiler does local and
23540 will be translated to
23552 Some subexpressions are not as obvious as the above example, e.g.:
23562 In this case the address arithmetic a->b[i] will be computed only once;
23563 the equivalent code in C would be.
23575 The compiler will try to keep these temporary variables in registers.
23578 Dead-Code Elimination
23579 \begin_inset LatexCommand \index{Dead-code elimination}
23600 i = 1; \SpecialChar ~
23609 global = 1;\SpecialChar ~
23622 global = 3;\SpecialChar ~
23647 \begin_inset LatexCommand \index{Copy propagation}
23703 Note: the dead stores created by this copy propagation will be eliminated
23704 by dead-code elimination.
23708 \begin_inset LatexCommand \index{Loop optimization}
23713 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23720 Two types of loop optimizations are done by SDCC
23728 of loop induction variables.
23729 In addition to the strength reduction the optimizer marks the induction
23730 variables and the register allocator tries to keep the induction variables
23731 in registers for the duration of the loop.
23732 Because of this preference of the register allocator
23733 \begin_inset LatexCommand \index{Register allocation}
23737 , loop induction optimization causes an increase in register pressure, which
23738 may cause unwanted spilling of other temporary variables into the stack
23739 \begin_inset LatexCommand \index{stack}
23744 The compiler will generate a warning message when it is forced to allocate
23745 extra space either on the stack or data space.
23746 If this extra space allocation is undesirable then induction optimization
23747 can be eliminated either for the entire source file (with -
23757 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23759 \begin_inset LatexCommand \index{\#pragma noinduction}
23772 for (i = 0 ; i < 100 ; i ++)
23788 for (i = 0; i < 100; i++)
23797 As mentioned previously some loop invariants are not as apparent, all static
23798 address computations are also moved out of the loop.
23803 \begin_inset LatexCommand \index{Strength reduction}
23807 , this optimization substitutes an expression by a cheaper expression:
23812 for (i=0;i < 100; i++)
23830 for (i=0;i< 100;i++) {
23836 ar[itemp1] = itemp2;
23853 The more expensive multiplication
23854 \begin_inset LatexCommand \index{Multiplication}
23858 is changed to a less expensive addition.
23862 \begin_inset LatexCommand \index{Loop reversing}
23869 This optimization is done to reduce the overhead of checking loop boundaries
23870 for every iteration.
23871 Some simple loops can be reversed and implemented using a
23872 \begin_inset Quotes eld
23875 decrement and jump if not zero
23876 \begin_inset Quotes erd
23880 SDCC checks for the following criterion to determine if a loop is reversible
23881 (note: more sophisticated compilers use data-dependency analysis to make
23882 this determination, SDCC uses a more simple minded analysis).
23885 The 'for' loop is of the form
23891 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23901 The <for body> does not contain
23902 \begin_inset Quotes eld
23906 \begin_inset Quotes erd
23910 \begin_inset Quotes erd
23916 All goto's are contained within the loop.
23919 No function calls within the loop.
23922 The loop control variable <sym> is not assigned any value within the loop
23925 The loop control variable does NOT participate in any arithmetic operation
23929 There are NO switch statements in the loop.
23932 Algebraic Simplifications
23935 SDCC does numerous algebraic simplifications, the following is a small sub-set
23936 of these optimizations.
23941 i = j + 0;\SpecialChar ~
23945 /* changed to: */\SpecialChar ~
23951 i /= 2;\SpecialChar ~
23958 /* changed to: */\SpecialChar ~
23964 i = j - j;\SpecialChar ~
23968 /* changed to: */\SpecialChar ~
23974 i = j / 1;\SpecialChar ~
23978 /* changed to: */\SpecialChar ~
23985 Note the subexpressions
23986 \begin_inset LatexCommand \index{Subexpression}
23990 given above are generally introduced by macro expansions or as a result
23991 of copy/constant propagation.
23994 'switch' Statements
23995 \begin_inset LatexCommand \label{sub:'switch'-Statements}
24000 \begin_inset LatexCommand \index{switch statement}
24007 SDCC can optimize switch statements to jump tables
24008 \begin_inset LatexCommand \index{jump tables}
24013 It makes the decision based on an estimate of the generated code size.
24014 SDCC is quite liberal in the requirements for jump table generation:
24017 The labels need not be in order, and the starting number need not be one
24018 or zero, the case labels are in numerical sequence or not too many case
24019 labels are missing.
24025 switch(i) {\SpecialChar ~
24056 case 4: ...\SpecialChar ~
24088 case 5: ...\SpecialChar ~
24120 case 3: ...\SpecialChar ~
24151 case 6: ...\SpecialChar ~
24183 case 7: ...\SpecialChar ~
24215 case 8: ...\SpecialChar ~
24247 case 9: ...\SpecialChar ~
24279 case 10: ...\SpecialChar ~
24310 case 11: ...\SpecialChar ~
24377 Both the above switch statements will be implemented using a jump-table.
24378 The example to the right side is slightly more efficient as the check for
24379 the lower boundary of the jump-table is not needed.
24383 The number of case labels is not larger than supported by the target architectur
24387 If the case labels are not in numerical sequence ('gaps' between cases)
24388 SDCC checks whether a jump table with additionally inserted dummy cases
24389 is still attractive.
24393 If the starting number is not zero and a check for the lower boundary of
24394 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24399 Switch statements which have large gaps in the numeric sequence or those
24400 that have too many case labels can be split into more than one switch statement
24401 for efficient code generation, e.g.:
24481 If the above switch statement is broken down into two switch statements
24571 then both the switch statements will be implemented using jump-tables whereas
24572 the unmodified switch statement will not be.
24575 There might be reasons which SDCC cannot know about to either favour or
24576 not favour jump tables.
24577 If the target system has to be as quick for the last switch case as for
24578 the first (pro jump table), or if the switch argument is known to be zero
24579 in the majority of the cases (contra jump table).
24582 The pragma nojtbound
24583 \begin_inset LatexCommand \index{\#pragma nojtbound}
24587 can be used to turn off checking the
24600 It has no effect if a default label is supplied.
24601 Use of this pragma is dangerous: if the switch
24602 \begin_inset LatexCommand \index{switch statement}
24606 argument is not matched by a case statement the processor will happily
24610 Bit-shifting Operations
24611 \begin_inset LatexCommand \index{Bit shifting}
24618 Bit shifting is one of the most frequently used operation in embedded programmin
24620 SDCC tries to implement bit-shift operations in the most efficient way
24636 generates the following code:
24653 In general SDCC will never setup a loop if the shift count is known.
24696 \begin_inset LatexCommand \index{Bit rotation}
24703 A special case of the bit-shift operation is bit rotation
24704 \begin_inset LatexCommand \index{rotating bits}
24708 , SDCC recognizes the following expression to be a left bit-rotation:
24718 char i;\SpecialChar ~
24729 /* unsigned is needed for rotation */
24734 i = ((i << 1) | (i >> 7));
24743 will generate the following code:
24762 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24763 ns of this case will also be recognized as bit-rotation, i.e.:
24768 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24771 Nibble and Byte Swapping
24774 Other special cases of the bit-shift operations are nibble or byte swapping
24775 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24779 , SDCC recognizes the following expressions:
24802 i = ((i << 4) | (i >> 4));
24808 j = ((j << 8) | (j >> 8));
24811 and generates a swap instruction for the nibble swapping
24812 \begin_inset LatexCommand \index{Nibble swapping}
24816 or move instructions for the byte swapping
24817 \begin_inset LatexCommand \index{Byte swapping}
24823 \begin_inset Quotes sld
24827 \begin_inset Quotes srd
24830 example can be used to convert from little to big-endian or vice versa.
24831 If you want to change the endianness of a
24835 integer you have to cast to
24842 Note that SDCC stores numbers in little-endian
24848 Usually 8-bit processors don't care much about endianness.
24849 This is not the case for the standard 8051 which only has an instruction
24855 \begin_inset LatexCommand \index{DPTR}
24863 so little-endian is the more efficient byte order.
24867 \begin_inset LatexCommand \index{little-endian}
24872 \begin_inset LatexCommand \index{Endianness}
24877 lowest order first).
24881 \begin_inset LatexCommand \index{Highest Order Bit}
24886 \begin_inset LatexCommand \index{Any Order Bit}
24893 It is frequently required to obtain the highest order bit of an integral
24894 type (long, int, short or char types).
24895 Also obtaining any other order bit is not uncommon.
24896 SDCC recognizes the following expressions to yield the highest order bit
24897 and generates optimized code for it, e.g.:
24910 unsigned char hob1, aob1;
24914 bit hob2, hob3, aob2, aob3;
24923 hob1 = (gint >> 15) & 1;
24927 hob2 = (gint >> 15) & 1;
24931 hob3 = gint & 0x8000;
24935 aob1 = (gint >> 9) & 1;
24939 aob2 = (gint >> 8) & 1;
24943 aob3 = gint & 0x0800;
24953 will generate the following code:
24986 000A E5*01\SpecialChar ~
25013 000C 23\SpecialChar ~
25044 000D 54 01\SpecialChar ~
25071 000F F5*02\SpecialChar ~
25126 0011 E5*01\SpecialChar ~
25153 0013 33\SpecialChar ~
25183 0014 92*00\SpecialChar ~
25238 0016 E5*01\SpecialChar ~
25265 0018 33\SpecialChar ~
25295 0019 92*01\SpecialChar ~
25350 001B E5*01\SpecialChar ~
25377 001D 03\SpecialChar ~
25408 001E 54 01\SpecialChar ~
25435 0020 F5*03\SpecialChar ~
25490 0022 E5*01\SpecialChar ~
25517 0024 13\SpecialChar ~
25547 0025 92*02\SpecialChar ~
25602 0027 E5*01\SpecialChar ~
25629 0029 A2 E3\SpecialChar ~
25656 002B 92*03\SpecialChar ~
25684 Other variations of these cases however will
25689 They are standard C expressions, so I heartily recommend these be the only
25690 way to get the highest order bit, (it is portable).
25691 Of course it will be recognized even if it is embedded in other expressions,
25697 xyz = gint + ((gint >> 15) & 1);
25700 will still be recognized.
25704 \begin_inset LatexCommand \index{Higher Order Byte}
25708 / Higher Order Word
25709 \begin_inset LatexCommand \index{Higher Order Word}
25716 It is also frequently required to obtain a higher order byte or word of
25717 a larger integral type (long, int or short types).
25718 SDCC recognizes the following expressions to yield the higher order byte
25719 or word and generates optimized code for it, e.g.:
25726 unsigned long int glong;
25734 unsigned char hob1, hob2;
25738 unsigned int how1, how2;
25747 hob1 = (gint >> 8) & 0xFF;
25751 hob2 = glong >> 24;
25755 how1 = (glong >> 16) & 0xFFFF;
25769 will generate the following code:
25802 0037 85*01*06\SpecialChar ~
25824 _foo_hob1_1_1,(_gint + 1)
25854 003A 85*05*07\SpecialChar ~
25876 _foo_hob2_1_1,(_glong + 3)
25906 003D 85*04*08\SpecialChar ~
25928 _foo_how1_1_1,(_glong + 2)
25930 0040 85*05*09\SpecialChar ~
25952 (_foo_how1_1_1 + 1),(_glong + 3)
25954 0043 85*03*0A\SpecialChar ~
25976 _foo_how2_1_1,(_glong + 1)
25978 0046 85*04*0B\SpecialChar ~
26000 (_foo_how2_1_1 + 1),(_glong + 2)
26003 Again, variations of these cases may
26008 They are standard C expressions, so I heartily recommend these be the only
26009 way to get the higher order byte/word, (it is portable).
26010 Of course it will be recognized even if it is embedded in other expressions,
26016 xyz = gint + ((gint >> 8) & 0xFF);
26019 will still be recognized.
26023 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
26028 \begin_inset LatexCommand \index{Peephole optimizer}
26035 The compiler uses a rule based, pattern matching and re-writing mechanism
26036 for peep-hole optimization.
26041 a peep-hole optimizer by Christopher W.
26042 Fraser (cwfraser\SpecialChar ~
26045 A default set of rules are compiled into the compiler, additional rules
26046 may be added with the
26059 \begin_inset LatexCommand \index{-\/-peep-file}
26066 The rule language is best illustrated with examples.
26090 The above rule will change the following assembly
26091 \begin_inset LatexCommand \index{Assembler routines}
26113 Note: All occurrences of a
26117 (pattern variable) must denote the same string.
26118 With the above rule, the assembly sequence:
26128 will remain unmodified.
26132 Other special case optimizations may be added by the user (via
26148 some variants of the 8051 MCU
26149 \begin_inset LatexCommand \index{MCS51 variants}
26162 The following two rules will change all
26181 replace { lcall %1 } by { acall %1 }
26183 replace { ljmp %1 } by { ajmp %1 }
26188 inline-assembler code
26190 is also passed through the peep hole optimizer, thus the peephole optimizer
26191 can also be used as an assembly level macro expander.
26192 The rules themselves are MCU dependent whereas the rule language infra-structur
26193 e is MCU independent.
26194 Peephole optimization rules for other MCU can be easily programmed using
26199 The syntax for a rule is as follows:
26204 rule := replace [ restart ] '{' <assembly sequence> '
26242 <assembly sequence> '
26260 '}' [if <functionName> ] '
26265 <assembly sequence> := assembly instruction (each instruction including
26266 labels must be on a separate line).
26270 The optimizer will apply to the rules one by one from the top in the sequence
26271 of their appearance, it will terminate when all rules are exhausted.
26272 If the 'restart' option is specified, then the optimizer will start matching
26273 the rules again from the top, this option for a rule is expensive (performance)
26274 , it is intended to be used in situations where a transformation will trigger
26275 the same rule again.
26276 An example of this (not a good one, it has side effects) is the following
26299 Note that the replace pattern cannot be a blank, but can be a comment line.
26300 Without the 'restart' option only the innermost 'pop' 'push' pair would
26301 be eliminated, i.e.:
26331 the restart option the rule will be applied again to the resulting code
26332 and then all the pop-push pairs will be eliminated to yield:
26342 A conditional function can be attached to a rule.
26343 Attaching rules are somewhat more involved, let me illustrate this with
26370 The optimizer does a look-up of a function name table defined in function
26375 in the source file SDCCpeeph.c, with the name
26380 If it finds a corresponding entry the function is called.
26381 Note there can be no parameters specified for these functions, in this
26386 is crucial, since the function
26390 expects to find the label in that particular variable (the hash table containin
26391 g the variable bindings is passed as a parameter).
26392 If you want to code more such functions, take a close look at the function
26393 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26394 Currently implemented are
26396 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26397 24bitMode, portIsDS390, 24bitModeAndPortDS390
26406 I know this whole thing is a little kludgey, but maybe some day we will
26407 have some better means.
26408 If you are looking at this file, you will see the default rules that are
26409 compiled into the compiler, you can add your own rules in the default set
26410 there if you get tired of specifying the -
26424 \begin_inset LatexCommand \index{ANSI-compliance}
26429 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26436 Deviations from the compliance:
26439 functions are not reentrant
26440 \begin_inset LatexCommand \index{reentrant}
26444 unless explicitly declared as such or the
26457 \begin_inset LatexCommand \index{-\/-stack-auto}
26463 command line option is specified.
26466 structures and unions cannot be assigned values directly, cannot be passed
26467 as function parameters or assigned to each other and cannot be a return
26468 value from a function, e.g.:
26494 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26505 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26527 return rets;/* is invalid in SDCC although allowed in ANSI */
26533 initialization of structure arrays must be fully braced.
26539 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26541 struct s { char x } a[] = {{1}, {2}}; /* OK */
26546 \begin_inset LatexCommand \index{long long (not supported)}
26551 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26559 \begin_inset LatexCommand \index{double (not supported)}
26563 ' precision floating point
26564 \begin_inset LatexCommand \index{Floating point support}
26572 \begin_inset LatexCommand \index{K\&R style}
26576 function declarations are NOT allowed.
26582 foo(i,j) /* this old style of function declarations */
26584 int i,j; /* are valid in ANSI but not valid in SDCC */
26599 Most enhancements in C99 are not supported, f.e.:
26608 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
26615 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
26619 Certain words that are valid identifiers in the standard may be reserved
26620 words in SDCC unless the
26633 \begin_inset LatexCommand \index{-\/-std-c89}
26648 \begin_inset LatexCommand \index{-\/-std-c99}
26654 command line options are used.
26655 These may include (depending on the selected processor): 'at', 'banked',
26656 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26657 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26658 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26660 Compliant equivalents of these keywords are always available in a form
26661 that begin with two underscores
26662 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26667 '__data' instead of 'data'.
26670 Cyclomatic Complexity
26671 \begin_inset LatexCommand \index{Cyclomatic complexity}
26678 Cyclomatic complexity of a function is defined as the number of independent
26679 paths the program can take during execution of the function.
26680 This is an important number since it defines the number test cases you
26681 have to generate to validate the function.
26682 The accepted industry standard for complexity number is 10, if the cyclomatic
26683 complexity reported by SDCC exceeds 10 you should think about simplification
26684 of the function logic.
26685 Note that the complexity level is not related to the number of lines of
26686 code in a function.
26687 Large functions can have low complexity, and small functions can have large
26693 SDCC uses the following formula to compute the complexity:
26698 complexity = (number of edges in control flow graph) - (number of nodes
26699 in control flow graph) + 2;
26703 Having said that the industry standard is 10, you should be aware that in
26704 some cases it be may unavoidable to have a complexity level of less than
26706 For example if you have switch statement with more than 10 case labels,
26707 each case label adds one to the complexity level.
26708 The complexity level is by no means an absolute measure of the algorithmic
26709 complexity of the function, it does however provide a good starting point
26710 for which functions you might look at for further optimization.
26713 Retargetting for other Processors
26716 The issues for retargetting the compiler are far too numerous to be covered
26718 What follows is a brief description of each of the seven phases of the
26719 compiler and its MCU dependency.
26722 Parsing the source and building the annotated parse tree.
26723 This phase is largely MCU independent (except for the language extensions).
26724 Syntax & semantic checks are also done in this phase, along with some initial
26725 optimizations like back patching labels and the pattern matching optimizations
26726 like bit-rotation etc.
26729 The second phase involves generating an intermediate code which can be easy
26730 manipulated during the later phases.
26731 This phase is entirely MCU independent.
26732 The intermediate code generation assumes the target machine has unlimited
26733 number of registers, and designates them with the name iTemp.
26734 The compiler can be made to dump a human readable form of the code generated
26748 This phase does the bulk of the standard optimizations and is also MCU independe
26750 This phase can be broken down into several sub-phases:
26754 Break down intermediate code (iCode) into basic blocks.
26756 Do control flow & data flow analysis on the basic blocks.
26758 Do local common subexpression elimination, then global subexpression elimination
26760 Dead code elimination
26764 If loop optimizations caused any changes then do 'global subexpression eliminati
26765 on' and 'dead code elimination' again.
26768 This phase determines the live-ranges; by live range I mean those iTemp
26769 variables defined by the compiler that still survive after all the optimization
26771 Live range analysis
26772 \begin_inset LatexCommand \index{Live range analysis}
26776 is essential for register allocation, since these computation determines
26777 which of these iTemps will be assigned to registers, and for how long.
26780 Phase five is register allocation.
26781 There are two parts to this process.
26785 The first part I call 'register packing' (for lack of a better term).
26786 In this case several MCU specific expression folding is done to reduce
26791 The second part is more MCU independent and deals with allocating registers
26792 to the remaining live ranges.
26793 A lot of MCU specific code does creep into this phase because of the limited
26794 number of index registers available in the 8051.
26797 The Code generation phase is (unhappily), entirely MCU dependent and very
26798 little (if any at all) of this code can be reused for other MCU.
26799 However the scheme for allocating a homogenized assembler operand for each
26800 iCode operand may be reused.
26803 As mentioned in the optimization section the peep-hole optimizer is rule
26804 based system, which can reprogrammed for other MCUs.
26808 \begin_inset LatexCommand \index{Compiler internals}
26815 The anatomy of the compiler
26816 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26825 This is an excerpt from an article published in Circuit Cellar Magazine
26831 It's a little outdated (the compiler is much more efficient now and user/develo
26832 per friendly), but pretty well exposes the guts of it all.
26838 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26839 It is fairly easy to retarget for other 8-bit MCU.
26840 Here we take a look at some of the internals of the compiler.
26845 \begin_inset LatexCommand \index{Parsing}
26852 Parsing the input source file and creating an AST (Annotated Syntax Tree
26853 \begin_inset LatexCommand \index{Annotated syntax tree}
26858 This phase also involves propagating types (annotating each node of the
26859 parse tree with type information) and semantic analysis.
26860 There are some MCU specific parsing rules.
26861 For example the storage classes, the extended storage classes are MCU specific
26862 while there may be a xdata storage class for 8051 there is no such storage
26863 class for z80 or Atmel AVR.
26864 SDCC allows MCU specific storage class extensions, i.e.
26865 xdata will be treated as a storage class specifier when parsing 8051 C
26866 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
26871 \begin_inset LatexCommand \index{iCode}
26878 Intermediate code generation.
26879 In this phase the AST is broken down into three-operand form (iCode).
26880 These three operand forms are represented as doubly linked lists.
26881 ICode is the term given to the intermediate form generated by the compiler.
26882 ICode example section shows some examples of iCode generated for some simple
26883 C source functions.
26887 \begin_inset LatexCommand \index{Optimizations}
26894 Bulk of the target independent optimizations is performed in this phase.
26895 The optimizations include constant propagation, common sub-expression eliminati
26896 on, loop invariant code movement, strength reduction of loop induction variables
26897 and dead-code elimination.
26900 Live range analysis
26901 \begin_inset LatexCommand \index{Live range analysis}
26908 During intermediate code generation phase, the compiler assumes the target
26909 machine has infinite number of registers and generates a lot of temporary
26911 The live range computation determines the lifetime of each of these compiler-ge
26912 nerated temporaries.
26913 A picture speaks a thousand words.
26914 ICode example sections show the live range annotations for each of the
26916 It is important to note here, each iCode is assigned a number in the order
26917 of its execution in the function.
26918 The live ranges are computed in terms of these numbers.
26919 The from number is the number of the iCode which first defines the operand
26920 and the to number signifies the iCode which uses this operand last.
26923 Register Allocation
26924 \begin_inset LatexCommand \index{Register allocation}
26931 The register allocation determines the type and number of registers needed
26933 In most MCUs only a few registers can be used for indirect addressing.
26934 In case of 8051 for example the registers R0 & R1 can be used to indirectly
26935 address the internal ram and DPTR to indirectly address the external ram.
26936 The compiler will try to allocate the appropriate register to pointer variables
26938 ICode example section shows the operands annotated with the registers assigned
26940 The compiler will try to keep operands in registers as much as possible;
26941 there are several schemes the compiler uses to do achieve this.
26942 When the compiler runs out of registers the compiler will check to see
26943 if there are any live operands which is not used or defined in the current
26944 basic block being processed, if there are any found then it will push that
26945 operand and use the registers in this block, the operand will then be popped
26946 at the end of the basic block.
26950 There are other MCU specific considerations in this phase.
26951 Some MCUs have an accumulator; very short-lived operands could be assigned
26952 to the accumulator instead of a general-purpose register.
26958 Figure II gives a table of iCode operations supported by the compiler.
26959 The code generation involves translating these operations into corresponding
26960 assembly code for the processor.
26961 This sounds overly simple but that is the essence of code generation.
26962 Some of the iCode operations are generated on a MCU specific manner for
26963 example, the z80 port does not use registers to pass parameters so the
26964 SEND and RECV iCode operations will not be generated, and it also does
26965 not support JUMPTABLES.
26972 <Where is Figure II?>
26975 In the original article Figure II was announced to be downloadable on
26980 Unfortunately it never seemed to have shown up there, so: where is Figure
26985 \begin_inset LatexCommand \index{iCode}
26992 This section shows some details of iCode.
26993 The example C code does not do anything useful; it is used as an example
26994 to illustrate the intermediate code generated by the compiler.
27006 /* This function does nothing useful.
27013 for the purpose of explaining iCode */
27016 short function (data int *x)
27024 short i=10; \SpecialChar ~
27026 /* dead initialization eliminated */
27031 short sum=10; /* dead initialization eliminated */
27044 while (*x) *x++ = *p++;
27058 /* compiler detects i,j to be induction variables */
27062 for (i = 0, j = 10 ; i < 10 ; i++, j
27088 mul += i * 3; \SpecialChar ~
27090 /* this multiplication remains */
27096 gint += j * 3;\SpecialChar ~
27098 /* this multiplication changed to addition */
27112 In addition to the operands each iCode contains information about the filename
27113 and line it corresponds to in the source file.
27114 The first field in the listing should be interpreted as follows:
27119 Filename(linenumber: iCode Execution sequence number : ICode hash table
27120 key : loop depth of the iCode).
27125 Then follows the human readable form of the ICode operation.
27126 Each operand of this triplet form can be of three basic types a) compiler
27127 generated temporary b) user defined variable c) a constant value.
27128 Note that local variables and parameters are replaced by compiler generated
27131 \begin_inset LatexCommand \index{Live range analysis}
27135 are computed only for temporaries (i.e.
27136 live ranges are not computed for global variables).
27138 \begin_inset LatexCommand \index{Register allocation}
27142 are allocated for temporaries only.
27143 Operands are formatted in the following manner:
27148 Operand Name [lr live-from : live-to ] { type information } [ registers
27154 As mentioned earlier the live ranges are computed in terms of the execution
27155 sequence number of the iCodes, for example
27157 the iTemp0 is live from (i.e.
27158 first defined in iCode with execution sequence number 3, and is last used
27159 in the iCode with sequence number 5).
27160 For induction variables such as iTemp21 the live range computation extends
27161 the lifetime from the start to the end of the loop.
27163 The register allocator used the live range information to allocate registers,
27164 the same registers may be used for different temporaries if their live
27165 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27166 iTemp17 since their live ranges do not overlap.
27167 In addition the allocator also takes into consideration the type and usage
27168 of a temporary, for example itemp6 is a pointer to near space and is used
27169 as to fetch data from (i.e.
27170 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27171 Some short lived temporaries are allocated to special registers which have
27172 meaning to the code generator e.g.
27173 iTemp13 is allocated to a pseudo register CC which tells the back end that
27174 the temporary is used only for a conditional jump the code generation makes
27175 use of this information to optimize a compare and jump ICode.
27177 There are several loop optimizations
27178 \begin_inset LatexCommand \index{Loop optimization}
27182 performed by the compiler.
27183 It can detect induction variables iTemp21(i) and iTemp23(j).
27184 Also note the compiler does selective strength reduction
27185 \begin_inset LatexCommand \index{Strength reduction}
27190 the multiplication of an induction variable in line 18 (gint = j * 3) is
27191 changed to addition, a new temporary iTemp17 is allocated and assigned
27192 a initial value, a constant 3 is then added for each iteration of the loop.
27193 The compiler does not change the multiplication
27194 \begin_inset LatexCommand \index{Multiplication}
27198 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27200 Note the dead code elimination
27201 \begin_inset LatexCommand \index{Dead-code elimination}
27205 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27213 Sample.c (5:1:0:0) _entry($9) :
27218 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27223 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27228 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27233 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27239 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27244 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27250 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27255 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27261 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27267 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27273 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27279 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27280 * int}[r0] + 0x2 {short}
27285 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27290 Sample.c(11:17:21:0)_whilebreak_0($3) :
27295 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27300 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27305 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27310 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27315 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27320 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27325 Sample.c(15:24:26:1)_forcond_0($4) :
27330 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27336 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27341 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27342 + ITemp21 [lr21:38]{short}[r4]
27347 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27353 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27354 + iTemp15 [lr29:30]{short}[r1]
27359 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27365 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27371 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27377 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27383 Sample.c(19:38:47:1) goto _forcond_0($4)
27388 Sample.c(19:39:48:0)_forbreak_0($7) :
27393 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27394 + ITemp11 [lr19:40]{short}[r3]
27399 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27404 Sample.c(20:42:51:0)_return($8) :
27409 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27415 Finally the code generated for this function:
27456 ; ----------------------------------------------
27461 ; function function
27466 ; ----------------------------------------------
27476 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27488 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27500 ;_whilecontinue_0($1) :
27510 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27515 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27574 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27593 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27640 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27680 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27706 ; iTemp6 [lr5:16]{_near * int}[r0] =
27711 ; iTemp6 [lr5:16]{_near * int}[r0] +
27728 ; goto _whilecontinue_0($1)
27740 ; _whilebreak_0($3) :
27750 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27762 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27774 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27786 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27805 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27834 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27839 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27884 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
27889 ; iTemp21 [lr21:38]{short}[r4]
27915 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
27948 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
27953 ; iTemp15 [lr29:30]{short}[r1]
27972 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
28019 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
28066 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
28078 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
28092 cjne r5,#0xff,00104$
28104 ; goto _forcond_0($4)
28116 ; _forbreak_0($7) :
28126 ; ret iTemp24 [lr40:41]{short}
28169 A few words about basic block successors, predecessors and dominators
28172 Successors are basic blocks
28173 \begin_inset LatexCommand \index{Basic blocks}
28177 that might execute after this basic block.
28179 Predecessors are basic blocks that might execute before reaching this basic
28182 Dominators are basic blocks that WILL execute before reaching this basic
28216 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28219 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28222 c) domVect of [BB4] = BB1 ...
28223 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28231 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28241 Thanks to all the other volunteer developers who have helped with coding,
28242 testing, web-page creation, distribution sets, etc.
28243 You know who you are :-)
28250 This document was initially written by Sandeep Dutta
28253 All product names mentioned herein may be trademarks
28254 \begin_inset LatexCommand \index{Trademarks}
28258 of their respective companies.
28265 To avoid confusion, the installation and building options for SDCC itself
28266 (chapter 2) are not part of the index.
28270 \begin_inset LatexCommand \printindex{}