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.
252 The latest version can be downloaded from
253 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
263 Please note: the compiler will probably always be some steps ahead of this
268 \begin_inset LatexCommand \index{Status of documentation}
278 Obviously this has pros and cons
287 All packages used in this compiler system are
295 ; source code for all the sub-packages (pre-processor, assemblers, linkers
296 etc) is distributed with the package.
297 This documentation is maintained using a freeware word processor (LyX).
299 This program is free software; you can redistribute it and/or modify it
300 under the terms of the GNU General Public License
301 \begin_inset LatexCommand \index{GNU General Public License, GPL}
305 as published by the Free Software Foundation; either version 2, or (at
306 your option) any later version.
307 This program is distributed in the hope that it will be useful, but WITHOUT
308 ANY WARRANTY; without even the implied warranty
309 \begin_inset LatexCommand \index{warranty}
313 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
314 See the GNU General Public License for more details.
315 You should have received a copy of the GNU General Public License along
316 with this program; if not, write to the Free Software Foundation, 59 Temple
317 Place - Suite 330, Boston, MA 02111-1307, USA.
318 In other words, you are welcome to use, share and improve this program.
319 You are forbidden to forbid anyone else to use, share and improve what
321 Help stamp out software-hoarding!
324 Typographic conventions
325 \begin_inset LatexCommand \index{Typographic conventions}
332 Throughout this manual, we will use the following convention.
333 Commands you have to type in are printed in
341 Code samples are printed in
346 Interesting items and new terms are printed in
351 Compatibility with previous versions
352 \begin_inset LatexCommand \index{Compatibility with previous versions}
359 This version has numerous bug fixes compared with the previous version.
360 But we also introduced some incompatibilities with older versions.
361 Not just for the fun of it, but to make the compiler more stable, efficient
363 \begin_inset LatexCommand \index{ANSI-compliance}
368 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
372 for ANSI-Compliance).
378 short is now equivalent to int (16 bits), it used to be equivalent to char
379 (8 bits) which is not ANSI compliant.
382 the default directory for gcc-builds where include, library and documentation
383 files are stored is now in /usr/local/share.
386 char type parameters to vararg functions are casted to int unless explicitly
403 will push a as an int and as a char resp.
416 -regextend has been removed.
429 -noregparms has been removed.
442 -stack-after-data has been removed.
446 \begin_inset LatexCommand \index{bit}
451 \begin_inset LatexCommand \index{sbit}
456 \begin_inset LatexCommand \index{\_\_sbit}
460 types now consistently behave like the C99 _Bool type with respect to type
462 \begin_inset LatexCommand \index{type conversion}
467 \begin_inset LatexCommand \index{type promotion}
472 The most common incompatibility resulting from this change is related to
474 \begin_inset LatexCommand \index{Bit toggling}
488 b = ~b; /* equivalent to b=1 instead of toggling b */
492 b = !b; /* toggles b */
496 In previous versions, both forms would have toggled the bit.
501 <pending: more incompatibilities?>
507 What do you need before you start installation of SDCC? A computer, and
509 The preferred method of installation is to compile SDCC from source using
511 For Windows some pre-compiled binary distributions are available for your
513 You should have some experience with command line tools and compiler use.
519 The SDCC home page at
520 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
524 is a great place to find distribution sets.
525 You can also find links to the user mailing lists that offer help or discuss
526 SDCC with other SDCC users.
527 Web links to other SDCC related sites can also be found here.
528 This document can be found in the DOC directory of the source package as
530 A pdf version of this document is available at
531 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
536 Some of the other tools (simulator and assembler) included with SDCC contain
537 their own documentation and can be found in the source distribution.
538 If you want the latest unreleased software, the complete source package
539 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
542 Wishes for the future
545 There are (and always will be) some things that could be done.
546 Here are some I can think of:
553 char KernelFunction3(char p) at 0x340;
561 \begin_inset LatexCommand \index{code banking (limited support)}
571 If you can think of some more, please see the section
572 \begin_inset LatexCommand \ref{sub:Requesting-Features}
576 about filing feature requests
577 \begin_inset LatexCommand \index{Requesting features}
582 \begin_inset LatexCommand \index{Feature request}
592 \begin_inset LatexCommand \index{Installation}
599 For most users it is sufficient to skip to either section
600 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
605 \begin_inset LatexCommand \ref{sub:Windows-Install}
610 More detailled instructions follow below.
614 \begin_inset LatexCommand \index{Options SDCC configuration}
621 The install paths, search paths and other options are defined when running
623 The defaults can be overridden by:
625 \labelwidthstring 00.00.0000
637 -prefix see table below
639 \labelwidthstring 00.00.0000
651 -exec_prefix see table below
653 \labelwidthstring 00.00.0000
665 -bindir see table below
667 \labelwidthstring 00.00.0000
679 -datadir see table below
681 \labelwidthstring 00.00.0000
683 docdir environment variable, see table below
685 \labelwidthstring 00.00.0000
687 include_dir_suffix environment variable, see table below
689 \labelwidthstring 00.00.0000
691 lib_dir_suffix environment variable, see table below
693 \labelwidthstring 00.00.0000
695 sdccconf_h_dir_separator environment variable, either / or
700 This character will only be used in sdccconf.h; don't forget it's a C-header,
701 therefore a double-backslash is needed there.
703 \labelwidthstring 00.00.0000
715 -disable-mcs51-port Excludes the Intel mcs51 port
717 \labelwidthstring 00.00.0000
729 -disable-gbz80-port Excludes the Gameboy gbz80 port
731 \labelwidthstring 00.00.0000
743 -disable-z80-port Excludes the z80 port
745 \labelwidthstring 00.00.0000
757 -disable-avr-port Excludes the AVR port
759 \labelwidthstring 00.00.0000
771 -disable-ds390-port Excludes the DS390 port
773 \labelwidthstring 00.00.0000
785 -disable-hc08-port Excludes the HC08 port
787 \labelwidthstring 00.00.0000
799 -disable-pic-port Excludes the PIC port
801 \labelwidthstring 00.00.0000
813 -disable-xa51-port Excludes the XA51 port
815 \labelwidthstring 00.00.0000
827 -disable-ucsim Disables configuring and building of ucsim
829 \labelwidthstring 00.00.0000
841 -disable-device-lib-build Disables automatically building device libraries
843 \labelwidthstring 00.00.0000
855 -disable-packihx Disables building packihx
857 \labelwidthstring 00.00.0000
869 -enable-libgc Use the Bohem memory allocator.
870 Lower runtime footprint.
873 Furthermore the environment variables CC, CFLAGS, ...
874 the tools and their arguments can be influenced.
875 Please see `configure -
885 -help` and the man/info pages of `configure` for details.
889 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
890 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
891 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
892 At the moment it's not possible to change the default settings (it was
893 simply never required).
897 These configure options are compiled into the binaries, and can only be
898 changed by rerunning 'configure' and recompiling SDCC.
899 The configure options are written in
903 to distinguish them from run time environment variables (see section search
909 \begin_inset Quotes sld
913 \begin_inset Quotes srd
916 are used by the SDCC team to build the official Win32 binaries.
917 The SDCC team uses Mingw32 to build the official Windows binaries, because
924 a gcc compiler and last but not least
927 the binaries can be built by cross compiling on Sourceforge's compile farm.
930 See the examples, how to pass the Win32 settings to 'configure'.
931 The other Win32 builds using Borland, VC or whatever don't use 'configure',
932 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
944 <lyxtabular version="3" rows="8" columns="3">
946 <column alignment="block" valignment="top" leftline="true" width="0in">
947 <column alignment="block" valignment="top" leftline="true" width="0in">
948 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
949 <row topline="true" bottomline="true">
950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1005 <row topline="true">
1006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1037 <row topline="true">
1038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1075 <row topline="true">
1076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1109 <row topline="true">
1110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1147 <row topline="true">
1148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1175 <row topline="true" bottomline="true">
1176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1212 'configure' also computes relative paths.
1213 This is needed for full relocatability of a binary package and to complete
1214 search paths (see section search paths below):
1220 \begin_inset Tabular
1221 <lyxtabular version="3" rows="4" columns="3">
1223 <column alignment="block" valignment="top" leftline="true" width="0in">
1224 <column alignment="block" valignment="top" leftline="true" width="0in">
1225 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1226 <row topline="true" bottomline="true">
1227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1252 <row topline="true" bottomline="true">
1253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1280 <row bottomline="true">
1281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1308 <row bottomline="true">
1309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1360 \begin_inset Quotes srd
1364 \begin_inset Quotes srd
1378 \begin_inset Quotes srd
1382 \begin_inset Quotes srd
1410 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1419 \begin_inset Quotes srd
1422 i586-mingw32msvc-gcc
1423 \begin_inset Quotes srd
1427 \begin_inset Quotes srd
1430 i586-mingw32msvc-g++
1431 \begin_inset Quotes srd
1439 \begin_inset Quotes srd
1442 i586-mingw32msvc-ranlib
1443 \begin_inset Quotes srd
1451 \begin_inset Quotes srd
1454 i586-mingw32msvc-strip
1455 \begin_inset Quotes srd
1473 \begin_inset Quotes srd
1477 \begin_inset Quotes srd
1495 \begin_inset Quotes srd
1499 \begin_inset Quotes srd
1507 \begin_inset Quotes srd
1511 \begin_inset Quotes srd
1519 \begin_inset Quotes srd
1523 \begin_inset Quotes srd
1531 \begin_inset Quotes srd
1535 \begin_inset Quotes srd
1542 sdccconf_h_dir_separator=
1543 \begin_inset Quotes srd
1555 \begin_inset Quotes srd
1572 -disable-device-lib-build
1600 -host=i586-mingw32msvc -
1610 -build=unknown-unknown-linux-gnu
1614 \begin_inset Quotes sld
1618 \begin_inset Quotes srd
1621 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1630 \begin_inset Quotes srd
1634 \begin_inset Quotes srd
1642 \begin_inset Quotes srd
1646 \begin_inset Quotes srd
1664 \begin_inset Quotes srd
1668 \begin_inset Quotes srd
1686 \begin_inset Quotes srd
1690 \begin_inset Quotes srd
1698 \begin_inset Quotes srd
1702 \begin_inset Quotes srd
1710 \begin_inset Quotes srd
1714 \begin_inset Quotes srd
1722 \begin_inset Quotes srd
1726 \begin_inset Quotes srd
1733 sdccconf_h_dir_separator=
1734 \begin_inset Quotes srd
1746 \begin_inset Quotes srd
1766 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1777 -C' turns on caching, which gives a little bit extra speed.
1778 However if options are changed, it can be necessary to delete the config.cache
1783 \begin_inset LatexCommand \label{sub:Install-paths}
1788 \begin_inset LatexCommand \index{Install paths}
1794 \added_space_top medskip \align center
1796 \begin_inset Tabular
1797 <lyxtabular version="3" rows="5" columns="4">
1799 <column alignment="center" valignment="top" leftline="true" width="0">
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" rightline="true" width="0">
1803 <row topline="true" bottomline="true">
1804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1845 <row topline="true">
1846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1885 <row topline="true">
1886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1901 $DATADIR/ $INCLUDE_DIR_SUFFIX
1904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1909 /usr/local/share/sdcc/include
1912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1925 <row topline="true">
1926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1941 $DATADIR/$LIB_DIR_SUFFIX
1944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1949 /usr/local/share/sdcc/lib
1952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1965 <row topline="true" bottomline="true">
1966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1989 /usr/local/share/sdcc/doc
1992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2014 *compiler, preprocessor, assembler, and linker
2020 is auto-appended by the compiler, e.g.
2021 small, large, z80, ds390 etc
2024 The install paths can still be changed during `make install` with e.g.:
2027 make install prefix=$(HOME)/local/sdcc
2030 Of course this doesn't change the search paths compiled into the binaries.
2034 Moreover the install path can be changed by defining DESTDIR
2035 \begin_inset LatexCommand \index{DESTDIR}
2042 make install DESTDIR=$(HOME)/sdcc.rpm/
2045 Please note that DESTDIR must have a trailing slash!
2049 \begin_inset LatexCommand \label{sub:Search-Paths}
2054 \begin_inset LatexCommand \index{Search path}
2061 Some search paths or parts of them are determined by configure variables
2066 , see section above).
2067 Further search paths are determined by environment variables during runtime.
2070 The paths searched when running the compiler are as follows (the first catch
2076 Binary files (preprocessor, assembler and linker)
2082 \begin_inset Tabular
2083 <lyxtabular version="3" rows="4" columns="3">
2085 <column alignment="block" valignment="top" leftline="true" width="0in">
2086 <column alignment="block" valignment="top" leftline="true" width="0in">
2087 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2088 <row topline="true" bottomline="true">
2089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2114 <row topline="true">
2115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2144 <row topline="true">
2145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2150 Path of argv[0] (if available)
2153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2170 <row topline="true" bottomline="true">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2187 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 \begin_inset Tabular
2213 <lyxtabular version="3" rows="6" columns="3">
2215 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2216 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2217 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2218 <row topline="true" bottomline="true">
2219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2244 <row topline="true">
2245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2300 <row topline="true">
2301 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2326 <row topline="true">
2327 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2364 <row topline="true">
2365 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2446 <row topline="true" bottomline="true">
2447 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2468 /usr/local/share/sdcc/
2473 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2501 -nostdinc disables the last two search paths.
2511 With the exception of
2512 \begin_inset Quotes sld
2526 \begin_inset Quotes srd
2533 is auto-appended by the compiler (e.g.
2534 small, large, z80, ds390 etc.).
2541 \begin_inset Tabular
2542 <lyxtabular version="3" rows="6" columns="3">
2544 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2545 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2546 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2547 <row topline="true" bottomline="true">
2548 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2556 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2573 <row topline="true">
2574 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2629 <row topline="true">
2630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2642 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2669 <row topline="true">
2670 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2681 $LIB_DIR_SUFFIX/<model>
2684 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2698 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2715 <row topline="true">
2716 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2731 $LIB_DIR_SUFFIX/<model>
2734 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2787 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2843 <row topline="true" bottomline="true">
2844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2853 $LIB_DIR_SUFFIX/<model>
2856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2861 /usr/local/share/sdcc/
2868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2886 Don't delete any of the stray spaces in the table above without checking
2887 the HTML output (last line)!
2903 -nostdlib disables the last two search paths.
2907 \begin_inset LatexCommand \index{Building SDCC}
2914 Building SDCC on Linux
2915 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2924 Download the source package
2926 either from the SDCC CVS repository or from the nightly snapshots
2928 , it will be named something like sdcc
2939 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2948 Bring up a command line terminal, such as xterm.
2953 Unpack the file using a command like:
2956 "tar -xvzf sdcc.src.tar.gz
2961 , this will create a sub-directory called sdcc with all of the sources.
2964 Change directory into the main SDCC directory, for example type:
2981 This configures the package for compilation on your system.
2997 All of the source packages will compile, this can take a while.
3013 This copies the binary executables, the include files, the libraries and
3014 the documentation to the install directories.
3015 Proceed with section
3016 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3023 Building SDCC on OSX 2.x
3026 Follow the instruction for Linux.
3030 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3031 )) fails to compile SDCC.
3032 Fortunately there's also gcc 2.9.x installed, which works fine.
3033 This compiler can be selected by running 'configure' with:
3036 ./configure CC=gcc2 CXX=g++2
3039 Cross compiling SDCC on Linux for Windows
3042 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3043 See section 'Configure Options'.
3046 Building SDCC on Windows
3049 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3051 They use Unix-sockets, which are not available on Win32.
3054 Building SDCC using Cygwin and Mingw32
3057 For building and installing a Cygwin executable follow the instructions
3063 \begin_inset Quotes sld
3067 \begin_inset Quotes srd
3070 Win32-binary can be built, which will not need the Cygwin-DLL.
3071 For the necessary 'configure' options see section 'configure options' or
3072 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3076 In order to install Cygwin on Windows download setup.exe from
3077 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3083 \begin_inset Quotes sld
3086 default text file type
3087 \begin_inset Quotes srd
3091 \begin_inset Quotes sld
3095 \begin_inset Quotes srd
3098 and download/install at least the following packages.
3099 Some packages are selected by default, others will be automatically selected
3100 because of dependencies with the manually selected packages.
3101 Never deselect these packages!
3110 gcc ; version 3.x is fine, no need to use the old 2.9x
3113 binutils ; selected with gcc
3119 rxvt ; a nice console, which makes life much easier under windoze (see below)
3122 man ; not really needed for building SDCC, but you'll miss it sooner or
3126 less ; not really needed for building SDCC, but you'll miss it sooner or
3130 cvs ; only if you use CVS access
3133 If you want to develop something you'll need:
3136 python ; for the regression tests
3139 gdb ; the gnu debugger, together with the nice GUI
3140 \begin_inset Quotes sld
3144 \begin_inset Quotes srd
3150 openssh ; to access the CF or commit changes
3153 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3154 use autoconf-stable!
3157 rxvt is a nice console with history.
3158 Replace in your cygwin.bat the line
3177 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3180 -bg black -fg white -geometry 100x65 -e bash -
3193 Text selected with the mouse is automatically copied to the clipboard, pasting
3194 works with shift-insert.
3198 The other good tip is to make sure you have no //c/-style paths anywhere,
3199 use /cygdrive/c/ instead.
3200 Using // invokes a network lookup which is very slow.
3202 \begin_inset Quotes sld
3206 \begin_inset Quotes srd
3209 is too long, you can change it with e.g.
3215 SDCC sources use the unix line ending LF.
3216 Life is much easier, if you store the source tree on a drive which is mounted
3218 And use an editor which can handle LF-only line endings.
3219 Make sure not to commit files with windows line endings.
3220 The tabulator spacing
3221 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3225 used in the project is 8.
3226 Although a tabulator spacing of 8 is a sensible choice for programmers
3227 (it's a power of 2 and allows to display 8/16 bit signed variables without
3228 loosing columns) the plan is to move towards using only spaces in the source.
3231 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3236 Download the source package
3238 either from the SDCC CVS repository or from the
3239 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3245 , it will be named something like sdcc
3252 SDCC is distributed with all the projects, workspaces, and files you need
3253 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3254 doesn't build under MSVC).
3255 The workspace name is 'sdcc.dsw'.
3256 Please note that as it is now, all the executables are created in a folder
3260 Once built you need to copy the executables from sdcc
3264 bin before running SDCC.
3269 WARNING: Visual studio is very picky with line terminations; it expects
3270 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3271 When using the CVS repository it's easiest to configure the cvs client
3272 to convert automatically for you.
3273 If however you are getting a message such as "This makefile was not generated
3274 by Developer Studio etc.
3276 \begin_inset Quotes srd
3279 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3280 need to convert the Unix style line endings to DOS style line endings.
3281 To do so you can use the
3282 \begin_inset Quotes sld
3286 \begin_inset Quotes srd
3289 utility freely available on the internet.
3290 Doug Hawkins reported in the sdcc-user list that this works:
3298 SDCC> unix2dos sdcc.dsw
3304 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3308 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3309 flex.exe, and gawk.exe.
3310 One good place to get them is
3311 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3319 Download the file UnxUtils
3320 \begin_inset LatexCommand \index{UnxUtils}
3325 Now you have to install the utilities and setup MSVC so it can locate the
3327 Here there are two alternatives (choose one!):
3334 a) Extract UnxUtils.zip to your C:
3336 hard disk PRESERVING the original paths, otherwise bison won't work.
3337 (If you are using WinZip make certain that 'Use folder names' is selected)
3341 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3342 in 'Show directories for:' select 'Executable files', and in the directories
3343 window add a new path: 'C:
3353 (As a side effect, you get a bunch of Unix utilities that could be useful,
3354 such as diff and patch.)
3361 This one avoids extracting a bunch of files you may not use, but requires
3366 a) Create a directory were to put the tools needed, or use a directory already
3374 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3375 to such directory WITHOUT preserving the original paths.
3376 (If you are using WinZip make certain that 'Use folder names' is not selected)
3380 c) Rename bison.exe to '_bison.exe'.
3384 d) Create a batch file 'bison.bat' in 'C:
3388 ' and add these lines:
3408 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3412 Steps 'c' and 'd' are needed because bison requires by default that the
3413 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3414 '/usr/local/share/' I think.
3415 So it is necessary to tell bison where those files are located if they
3416 are not in such directory.
3417 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3421 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3422 in 'Show directories for:' select 'Executable files', and in the directories
3423 window add a new path: 'c:
3426 Note that you can use any other path instead of 'c:
3428 util', even the path where the Visual C++ tools are, probably: 'C:
3432 Microsoft Visual Studio
3437 So you don't have to execute step 'e' :)
3441 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3442 the executables from sdcc
3446 bin, and you can compile using SDCC.
3449 Building SDCC Using Borland
3452 From the sdcc directory, run the command "make -f Makefile.bcc".
3453 This should regenerate all the .exe files in the bin directory except for
3454 sdcdb.exe (which currently doesn't build under Borland C++).
3457 If you modify any source files and need to rebuild, be aware that the dependenci
3458 es may not be correctly calculated.
3459 The safest option is to delete all .obj files and run the build again.
3460 From a Cygwin BASH prompt, this can easily be done with the command (be
3461 sure you are in the sdcc directory):
3471 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3473 ) -print -exec rm {}
3482 or on Windows NT/2000/XP from the command prompt with the command:
3489 del /s *.obj *.lib *.rul
3492 from the sdcc directory.
3495 Windows Install Using a ZIP Package
3498 Download the binary zip package from
3499 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3503 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3504 This should unpack to a group of sub-directories.
3505 An example directory structure after unpacking the mingw32 package is:
3510 bin for the executables, c:
3518 lib for the include and libraries.
3521 Adjust your environment variable PATH to include the location of the bin
3522 directory or start sdcc using the full path.
3525 Windows Install Using the Setup Program
3526 \begin_inset LatexCommand \label{sub:Windows-Install}
3533 Download the setup program
3535 sdcc-x.y.z-setup.exe
3537 for an official release from
3540 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3544 or a setup program for one of the snapshots
3546 sdcc_yyyymmdd_setup.exe
3549 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3554 A windows typical installer will guide you through the installation process.
3557 Building the Documentation
3560 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3561 as changing into the doc directory and typing
3565 \begin_inset Quotes srd
3569 \begin_inset Quotes srd
3576 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3579 \begin_inset LatexCommand \url{http://www.lyx.org}
3583 as editor this is straightforward.
3584 Prebuilt documentation in html and pdf format is available from
3585 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3592 Reading the Documentation
3595 Currently reading the document in pdf format is recommended, as for unknown
3596 reason the hyperlinks are working there whereas in the html version they
3603 If you should know why please drop us a note
3609 You'll find the pdf version
3610 \begin_inset LatexCommand \index{PDF version of this document}
3615 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3623 \begin_inset LatexCommand \index{HTML version of this document}
3628 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3634 This documentation is in some aspects different from a commercial documentation:
3638 It tries to document SDCC for several processor architectures in one document
3639 (commercially these probably would be separate documents/products).
3641 \begin_inset LatexCommand \index{Status of documentation}
3645 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3647 Z80, PIC14, PIC16 and HC08.
3650 There are many references pointing away from this documentation.
3651 Don't let this distract you.
3653 was a reference like
3654 \begin_inset LatexCommand \url{http://www.opencores.org}
3658 together with a statement
3659 \begin_inset Quotes sld
3662 some processors which are targetted by SDCC can be implemented in a
3679 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3684 \begin_inset Quotes srd
3687 we expect you to have a quick look there and come back.
3688 If you read this you are on the right track.
3691 Some sections attribute more space to problems, restrictions and warnings
3692 than to the solution.
3695 The installation section and the section about the debugger is intimidating.
3698 There are still lots of typos and there are more different writing styles
3702 Testing the SDCC Compiler
3703 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3710 The first thing you should do after installing your SDCC compiler is to
3726 \begin_inset LatexCommand \index{version}
3733 at the prompt, and the program should run and tell you the version.
3734 If it doesn't run, or gives a message about not finding sdcc program, then
3735 you need to check over your installation.
3736 Make sure that the sdcc bin directory is in your executable search path
3737 defined by the PATH environment setting (
3742 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3749 Install trouble-shooting for suggestions
3752 Make sure that the sdcc program is in the bin folder, if not perhaps something
3753 did not install correctly.
3761 is commonly installed as described in section
3762 \begin_inset Quotes sld
3765 Install and search paths
3766 \begin_inset Quotes srd
3775 Make sure the compiler works on a very simple example.
3776 Type in the following test.c program using your favorite
3802 Compile this using the following command:
3811 If all goes well, the compiler will generate a test.asm and test.rel file.
3812 Congratulations, you've just compiled your first program with SDCC.
3813 We used the -c option to tell SDCC not to link the generated code, just
3814 to keep things simple for this step.
3822 The next step is to try it with the linker.
3832 If all goes well the compiler will link with the libraries and produce
3833 a test.ihx output file.
3838 (no test.ihx, and the linker generates warnings), then the problem is most
3847 usr/local/share/sdcc/lib directory
3854 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3861 Install trouble-shooting for suggestions).
3869 The final test is to ensure
3877 header files and libraries.
3878 Edit test.c and change it to the following:
3895 strcpy(str1, "testing");
3902 Compile this by typing
3909 This should generate a test.ihx output file, and it should give no warnings
3910 such as not finding the string.h file.
3911 If it cannot find the string.h file, then the problem is that
3915 cannot find the /usr/local/share/sdcc/include directory
3922 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3929 Install trouble-shooting section for suggestions).
3947 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3951 to find exactly where SDCC is looking for the include and lib files.
3954 Install Trouble-shooting
3955 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3960 \begin_inset LatexCommand \index{Install trouble-shooting}
3967 If SDCC does not build correctly
3970 A thing to try is starting from scratch by unpacking the .tgz source package
3971 again in an empty directory.
3979 ./configure 2>&1 | tee configure.log
3993 make 2>&1 | tee make.log
4000 If anything goes wrong, you can review the log files to locate the problem.
4001 Or a relevant part of this can be attached to an email that could be helpful
4002 when requesting help from the mailing list.
4006 \begin_inset Quotes sld
4010 \begin_inset Quotes srd
4017 \begin_inset Quotes sld
4021 \begin_inset Quotes srd
4024 command is a script that analyzes your system and performs some configuration
4025 to ensure the source package compiles on your system.
4026 It will take a few minutes to run, and will compile a few tests to determine
4027 what compiler features are installed.
4031 \begin_inset Quotes sld
4035 \begin_inset Quotes srd
4041 This runs the GNU make tool, which automatically compiles all the source
4042 packages into the final installed binary executables.
4046 \begin_inset Quotes sld
4050 \begin_inset Quotes erd
4056 This will install the compiler, other executables libraries and include
4057 files into the appropriate directories.
4059 \begin_inset LatexCommand \ref{sub:Install-paths}
4065 \begin_inset LatexCommand \ref{sub:Search-Paths}
4070 about install and search paths.
4072 On most systems you will need super-user privileges to do this.
4078 SDCC is not just a compiler, but a collection of tools by various developers.
4079 These include linkers, assemblers, simulators and other components.
4080 Here is a summary of some of the components.
4081 Note that the included simulator and assembler have separate documentation
4082 which you can find in the source package in their respective directories.
4083 As SDCC grows to include support for other processors, other packages from
4084 various developers are included and may have their own sets of documentation.
4088 You might want to look at the files which are installed in <installdir>.
4089 At the time of this writing, we find the following programs for gcc-builds:
4093 In <installdir>/bin:
4096 sdcc - The compiler.
4099 sdcpp - The C preprocessor.
4102 asx8051 - The assembler for 8051 type processors.
4109 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4112 aslink -The linker for 8051 type processors.
4119 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4122 s51 - The ucSim 8051 simulator.
4125 sdcdb - The source debugger.
4128 packihx - A tool to pack (compress) Intel hex files.
4131 In <installdir>/share/sdcc/include
4137 In <installdir>/share/sdcc/lib
4140 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4144 In <installdir>/share/sdcc/doc
4150 As development for other processors proceeds, this list will expand to include
4151 executables to support processors like AVR, PIC, etc.
4157 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4158 the assembler and linkage editor.
4161 sdcpp - The C-Preprocessor
4165 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4169 is a modified version of the GNU preprocessor.
4170 The C preprocessor is used to pull in #include sources, process #ifdef
4171 statements, #defines and so on.
4182 - The Assemblers and Linkage Editors
4185 This is retargettable assembler & linkage editor, it was developed by Alan
4187 John Hartman created the version for 8051, and I (Sandeep) have made some
4188 enhancements and bug fixes for it to work properly with SDCC.
4195 \begin_inset LatexCommand \index{s51}
4199 is a freeware, opensource simulator developed by Daniel Drotos.
4200 The simulator is built as part of the build process.
4201 For more information visit Daniel's web site at:
4202 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4207 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4211 sdcdb - Source Level Debugger
4215 \begin_inset LatexCommand \index{sdcdb (debugger)}
4219 is the companion source level debugger.
4220 More about sdcdb in section
4221 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4226 The current version of the debugger uses Daniel's Simulator S51
4227 \begin_inset LatexCommand \index{s51}
4231 , but can be easily changed to use other simulators.
4241 Single Source File Projects
4244 For single source file 8051 projects the process is very simple.
4245 Compile your programs with the following command
4248 "sdcc sourcefile.c".
4252 This will compile, assemble and link your source file.
4253 Output files are as follows:
4257 \begin_inset LatexCommand \index{<file>.asm}
4262 \begin_inset LatexCommand \index{Assembler source}
4266 file created by the compiler
4270 \begin_inset LatexCommand \index{<file>.lst}
4275 \begin_inset LatexCommand \index{Assembler listing}
4279 file created by the Assembler
4283 \begin_inset LatexCommand \index{<file>.rst}
4288 \begin_inset LatexCommand \index{Assembler listing}
4292 file updated with linkedit information, created by linkage editor
4296 \begin_inset LatexCommand \index{<file>.sym}
4301 \begin_inset LatexCommand \index{Symbol listing}
4305 for the sourcefile, created by the assembler
4309 \begin_inset LatexCommand \index{<file>.rel}
4314 \begin_inset LatexCommand \index{<file>.o}
4319 \begin_inset LatexCommand \index{Object file}
4323 created by the assembler, input to Linkage editor
4327 \begin_inset LatexCommand \index{<file>.map}
4332 \begin_inset LatexCommand \index{Memory map}
4336 for the load module, created by the Linker
4340 \begin_inset LatexCommand \index{<file>.mem}
4344 - A file with a summary of the memory usage
4348 \begin_inset LatexCommand \index{<file>.ihx}
4352 - The load module in Intel hex format
4353 \begin_inset LatexCommand \index{Intel hex format}
4357 (you can select the Motorola S19 format
4358 \begin_inset LatexCommand \index{Motorola S19 format}
4373 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4378 If you need another format you might want to use
4385 \begin_inset LatexCommand \index{objdump (tool)}
4396 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4401 Both formats are documented in the documentation of srecord
4402 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4410 \begin_inset LatexCommand \index{<file>.adb}
4414 - An intermediate file containing debug information needed to create the
4426 \begin_inset LatexCommand \index{-\/-debug}
4434 \begin_inset LatexCommand \index{<file>.cdb}
4438 - An optional file (with -
4448 -debug) containing debug information.
4449 The format is documented in cdbfileformat.pdf
4454 \begin_inset LatexCommand \index{<file> (no extension)}
4458 An optional AOMF or AOMF51
4459 \begin_inset LatexCommand \index{AOMF, AOMF51}
4463 file containing debug information (generated with option -
4490 ormat is commonly used by third party tools (debuggers
4491 \begin_inset LatexCommand \index{Debugger}
4495 , simulators, emulators)
4499 \begin_inset LatexCommand \index{<file>.dump*}
4503 - Dump file to debug the compiler it self (generated with option -
4513 -dumpall) (see section
4514 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4520 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4526 \begin_inset Quotes sld
4529 Anatomy of the compiler
4530 \begin_inset Quotes srd
4536 Projects with Multiple Source Files
4539 SDCC can compile only ONE file at a time.
4540 Let us for example assume that you have a project containing the following
4545 foo1.c (contains some functions)
4547 foo2.c (contains some more functions)
4549 foomain.c (contains more functions and the function main)
4557 The first two files will need to be compiled separately with the commands:
4589 Then compile the source file containing the
4594 \begin_inset LatexCommand \index{Linker}
4598 the files together with the following command:
4606 foomain.c\SpecialChar ~
4607 foo1.rel\SpecialChar ~
4612 \begin_inset LatexCommand \index{<file>.rel}
4624 can be separately compiled as well:
4635 sdcc foomain.rel foo1.rel foo2.rel
4642 The file containing the
4657 file specified in the command line, since the linkage editor processes
4658 file in the order they are presented to it.
4659 The linker is invoked from SDCC using a script file with extension .lnk
4660 \begin_inset LatexCommand \index{<file>.lnk}
4665 You can view this file to troubleshoot linking problems such as those arising
4666 from missing libraries.
4669 Projects with Additional Libraries
4670 \begin_inset LatexCommand \index{Libraries}
4677 Some reusable routines may be compiled into a library, see the documentation
4678 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4682 \begin_inset LatexCommand \index{<file>.lib}
4689 Libraries created in this manner can be included in the command line.
4690 Make sure you include the -L <library-path> option to tell the linker where
4691 to look for these files if they are not in the current directory.
4692 Here is an example, assuming you have the source file
4704 (if that is not the same as your current project):
4711 sdcc foomain.c foolib.lib -L mylib
4722 must be an absolute path name.
4726 The most efficient way to use libraries is to keep separate modules in separate
4728 The lib file now should name all the modules.rel
4729 \begin_inset LatexCommand \index{<file>.rel}
4734 For an example see the standard library file
4738 in the directory <installdir>/share/lib/small.
4741 Using sdcclib to Create and Manage Libraries
4742 \begin_inset LatexCommand \index{sdcclib}
4749 Alternatively, instead of having a .rel file for each entry on the library
4750 file as described in the preceding section, sdcclib can be used to embed
4751 all the modules belonging to such library in the library file itself.
4752 This results in a larger library file, but it greatly reduces the number
4753 of disk files accessed by the linker.
4754 Additionally, the packed library file contains an index of all include
4755 modules and symbols that significantly speeds up the linking process.
4756 To display a list of options supported by sdcclib type:
4765 \begin_inset LatexCommand \index{sdcclib}
4776 To create a new library file, start by compiling all the required modules.
4814 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4816 The next step is to add the .rel files to the library file:
4824 sdcclib libint.lib _divsint.rel
4827 \begin_inset LatexCommand \index{sdcclib}
4837 sdcclib libint.lib _divuint.rel
4843 sdcclib libint.lib _modsint.rel
4849 sdcclib libint.lib _moduint.rel
4855 sdcclib libint.lib _mulint.rel
4862 If the file already exists in the library, it will be replaced.
4863 To see what modules and symbols are included in the library, options -s
4864 and -m are available.
4872 sdcclib -s libint.lib
4875 \begin_inset LatexCommand \index{sdcclib}
4985 If the source files are compiled using -
4996 \begin_inset LatexCommand \index{-\/-debug}
5000 , the corresponding debug information file .adb will be include in the library
5002 The library files created with sdcclib are plain text files, so they can
5003 be viewed with a text editor.
5004 It is not recomended to modify a library file created with sdcclib using
5005 a text editor, as there are file indexes numbers located accross the file
5006 used by the linker to quickly locate the required module to link.
5007 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5008 it can be safely deleted, since all the information required for linking
5009 is embedded in the library file itself.
5010 Library files created using sdcclib are used as described in the preceding
5014 Command Line Options
5015 \begin_inset LatexCommand \index{Command Line Options}
5022 Processor Selection Options
5023 \begin_inset LatexCommand \index{Options processor selection}
5028 \begin_inset LatexCommand \index{Processor selection options}
5034 \labelwidthstring 00.00.0000
5039 \begin_inset LatexCommand \index{-mmcs51}
5045 Generate code for the Intel MCS51
5046 \begin_inset LatexCommand \index{MCS51}
5050 family of processors.
5051 This is the default processor target.
5053 \labelwidthstring 00.00.0000
5058 \begin_inset LatexCommand \index{-mds390}
5064 Generate code for the Dallas DS80C390
5065 \begin_inset LatexCommand \index{DS80C390}
5071 \labelwidthstring 00.00.0000
5076 \begin_inset LatexCommand \index{-mds400}
5082 Generate code for the Dallas DS80C400
5083 \begin_inset LatexCommand \index{DS80C400}
5089 \labelwidthstring 00.00.0000
5094 \begin_inset LatexCommand \index{-mhc08}
5100 Generate code for the Freescale/Motorola HC08
5101 \begin_inset LatexCommand \index{HC08}
5105 family of processors.
5107 \labelwidthstring 00.00.0000
5112 \begin_inset LatexCommand \index{-mz80}
5118 Generate code for the Zilog Z80
5119 \begin_inset LatexCommand \index{Z80}
5123 family of processors.
5125 \labelwidthstring 00.00.0000
5130 \begin_inset LatexCommand \index{-mgbz80}
5136 Generate code for the GameBoy Z80
5137 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5141 processor (Not actively maintained).
5143 \labelwidthstring 00.00.0000
5148 \begin_inset LatexCommand \index{-mavr}
5154 Generate code for the Atmel AVR
5155 \begin_inset LatexCommand \index{AVR}
5159 processor (In development, not complete).
5160 AVR users should probably have a look at winavr
5161 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5166 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5173 I think it is fair to direct users there for now.
5174 Open source is also about avoiding unnecessary work .
5175 But I didn't find the 'official' link.
5177 \labelwidthstring 00.00.0000
5182 \begin_inset LatexCommand \index{-mpic14}
5188 Generate code for the Microchip PIC 14
5189 \begin_inset LatexCommand \index{PIC14}
5193 -bit processors (p16f84 and variants.
5194 In development, not complete).
5197 p16f627 p16f628 p16f84 p16f873 p16f877?
5199 \labelwidthstring 00.00.0000
5204 \begin_inset LatexCommand \index{-mpic16}
5210 Generate code for the Microchip PIC 16
5211 \begin_inset LatexCommand \index{PIC16}
5215 -bit processors (p18f452 and variants.
5216 In development, not complete).
5218 \labelwidthstring 00.00.0000
5224 Generate code for the Toshiba TLCS-900H
5225 \begin_inset LatexCommand \index{TLCS-900H}
5229 processor (Not maintained, not complete).
5231 \labelwidthstring 00.00.0000
5236 \begin_inset LatexCommand \index{-mxa51}
5242 Generate code for the Phillips XA51
5243 \begin_inset LatexCommand \index{XA51}
5247 processor (Not maintained, not complete).
5250 Preprocessor Options
5251 \begin_inset LatexCommand \index{Options preprocessor}
5256 \begin_inset LatexCommand \index{Preprocessor options}
5261 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5267 \labelwidthstring 00.00.0000
5272 \begin_inset LatexCommand \index{-I<path>}
5278 The additional location where the pre processor will look for <..h> or
5279 \begin_inset Quotes eld
5283 \begin_inset Quotes erd
5288 \labelwidthstring 00.00.0000
5293 \begin_inset LatexCommand \index{-D<macro[=value]>}
5299 Command line definition of macros.
5300 Passed to the preprocessor.
5302 \labelwidthstring 00.00.0000
5307 \begin_inset LatexCommand \index{-M}
5313 Tell the preprocessor to output a rule suitable for make describing the
5314 dependencies of each object file.
5315 For each source file, the preprocessor outputs one make-rule whose target
5316 is the object file name for that source file and whose dependencies are
5317 all the files `#include'd in it.
5318 This rule may be a single line or may be continued with `
5320 '-newline if it is long.
5321 The list of rules is printed on standard output instead of the preprocessed
5324 \begin_inset LatexCommand \index{-E}
5330 \labelwidthstring 00.00.0000
5335 \begin_inset LatexCommand \index{-C}
5341 Tell the preprocessor not to discard comments.
5342 Used with the `-E' option.
5344 \labelwidthstring 00.00.0000
5349 \begin_inset LatexCommand \index{-MM}
5360 Like `-M' but the output mentions only the user header files included with
5362 \begin_inset Quotes eld
5366 System header files included with `#include <file>' are omitted.
5368 \labelwidthstring 00.00.0000
5373 \begin_inset LatexCommand \index{-Aquestion(answer)}
5379 Assert the answer answer for question, in case it is tested with a preprocessor
5380 conditional such as `#if #question(answer)'.
5381 `-A-' disables the standard assertions that normally describe the target
5384 \labelwidthstring 00.00.0000
5389 \begin_inset LatexCommand \index{-Umacro}
5395 Undefine macro macro.
5396 `-U' options are evaluated after all `-D' options, but before any `-include'
5397 and `-imacros' options.
5399 \labelwidthstring 00.00.0000
5404 \begin_inset LatexCommand \index{-dM}
5410 Tell the preprocessor to output only a list of the macro definitions that
5411 are in effect at the end of preprocessing.
5412 Used with the `-E' option.
5414 \labelwidthstring 00.00.0000
5419 \begin_inset LatexCommand \index{-dD}
5425 Tell the preprocessor to pass all macro definitions into the output, in
5426 their proper sequence in the rest of the output.
5428 \labelwidthstring 00.00.0000
5433 \begin_inset LatexCommand \index{-dN}
5444 Like `-dD' except that the macro arguments and contents are omitted.
5445 Only `#define name' is included in the output.
5447 \labelwidthstring 00.00.0000
5452 preprocessorOption[,preprocessorOption]
5455 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5460 Pass the preprocessorOption to the preprocessor
5465 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5470 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5471 Collection (gcc), if you need more dedicated options please refer to the
5473 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5481 \begin_inset LatexCommand \index{Options linker}
5486 \begin_inset LatexCommand \index{Linker options}
5492 \labelwidthstring 00.00.0000
5512 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5517 \begin_inset LatexCommand \index{-L -\/-lib-path}
5524 <absolute path to additional libraries> This option is passed to the linkage
5525 editor's additional libraries
5526 \begin_inset LatexCommand \index{Libraries}
5531 The path name must be absolute.
5532 Additional library files may be specified in the command line.
5533 See section Compiling programs for more details.
5535 \labelwidthstring 00.00.0000
5552 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5557 <Value> The start location of the external ram
5558 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5562 , default value is 0.
5563 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5573 -xram-loc 0x8000 or -
5585 \labelwidthstring 00.00.0000
5602 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5607 <Value> The start location of the code
5608 \begin_inset LatexCommand \index{code}
5612 segment, default value 0.
5613 Note when this option is used the interrupt vector table is also relocated
5614 to the given address.
5615 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5625 -code-loc 0x8000 or -
5637 \labelwidthstring 00.00.0000
5654 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5659 <Value> By default the stack
5660 \begin_inset LatexCommand \index{stack}
5664 is placed after the data segment.
5665 Using this option the stack can be placed anywhere in the internal memory
5667 The value entered can be in Hexadecimal or Decimal format, e.g.
5678 -stack-loc 0x20 or -
5689 Since the sp register is incremented before a push or call, the initial
5690 sp will be set to one byte prior the provided value.
5691 The provided value should not overlap any other memory areas such as used
5692 register banks or the data segment and with enough space for the current
5710 \begin_inset LatexCommand \index{-\/-pack-iram}
5714 option (which is now a default setting) will override this setting, so
5715 you should also specify the
5731 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5735 option if you need to manually place the stack.
5737 \labelwidthstring 00.00.0000
5754 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5759 <Value> The start location of the internal ram data
5760 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5765 The value entered can be in Hexadecimal or Decimal format, eg.
5787 (By default, the start location of the internal ram data segment is set
5788 as low as possible in memory, taking into account the used register banks
5789 and the bit segment at address 0x20.
5790 For example if register banks 0 and 1 are used without bit variables, the
5791 data segment will be set, if -
5801 -data-loc is not used, to location 0x10.)
5803 \labelwidthstring 00.00.0000
5820 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5825 <Value> The start location of the indirectly addressable internal ram
5826 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5830 of the 8051, default value is 0x80.
5831 The value entered can be in Hexadecimal or Decimal format, eg.
5842 -idata-loc 0x88 or -
5854 \labelwidthstring 00.00.0000
5871 <Value> The start location of the bit
5872 \begin_inset LatexCommand \index{bit}
5876 addressable internal ram of the 8051.
5882 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5885 \labelwidthstring 00.00.0000
5900 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5909 The linker output (final object code) is in Intel Hex format.
5910 \begin_inset LatexCommand \index{Intel hex format}
5914 This is the default option.
5915 The format itself is documented in the documentation of srecord
5916 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5922 \labelwidthstring 00.00.0000
5937 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5946 The linker output (final object code) is in Motorola S19 format
5947 \begin_inset LatexCommand \index{Motorola S19 format}
5952 The format itself is documented in the documentation of srecord.
5954 \labelwidthstring 00.00.0000
5969 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5978 The linker output (final object code) is in ELF format
5979 \begin_inset LatexCommand \index{ELF format}
5984 (Currently only supported for the HC08 processors)
5986 \labelwidthstring 00.00.0000
5991 linkOption[,linkOption]
5994 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5999 Pass the linkOption to the linker.
6000 See file sdcc/as/doc/asxhtm.html for more on linker options.
6004 \begin_inset LatexCommand \index{Options MCS51}
6009 \begin_inset LatexCommand \index{MCS51 options}
6015 \labelwidthstring 00.00.0000
6030 \begin_inset LatexCommand \index{-\/-model-small}
6041 Generate code for Small Model programs, see section Memory Models for more
6043 This is the default model.
6045 \labelwidthstring 00.00.0000
6060 \begin_inset LatexCommand \index{-\/-model-medium}
6066 Generate code for Medium model programs, see section Memory Models for
6068 If this option is used all source files in the project have to be compiled
6070 It must also be used when invoking the linker.
6072 \labelwidthstring 00.00.0000
6087 \begin_inset LatexCommand \index{-\/-model-large}
6093 Generate code for Large model programs, see section Memory Models for more
6095 If this option is used all source files in the project have to be compiled
6097 It must also be used when invoking the linker.
6099 \labelwidthstring 00.00.0000
6114 \begin_inset LatexCommand \index{-\/-xstack}
6120 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6121 variables and passing parameters.
6123 \begin_inset LatexCommand \ref{sub:External-Stack}
6128 External Stack for more details.
6130 \labelwidthstring 00.00.0000
6148 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6152 Causes the linker to check if the internal ram usage is within limits of
6155 \labelwidthstring 00.00.0000
6173 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6177 Causes the linker to check if the external ram usage is within limits of
6180 \labelwidthstring 00.00.0000
6198 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6202 Causes the linker to check if the code memory usage is within limits of
6205 \labelwidthstring 00.00.0000
6223 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6227 Causes the linker to check if there is at minimum <Value> bytes for stack.
6229 \labelwidthstring 00.00.0000
6247 \begin_inset LatexCommand \index{-\/-pack-iram}
6251 Causes the linker to use unused register banks for data variables and pack
6252 data, idata and stack together.
6253 This is the default now.
6255 \labelwidthstring 00.00.0000
6273 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6277 Causes the linker to use old style for allocating memory areas.
6280 DS390 / DS400 Options
6281 \begin_inset LatexCommand \index{Options DS390}
6286 \begin_inset LatexCommand \index{DS390 options}
6292 \labelwidthstring 00.00.0000
6309 \begin_inset LatexCommand \index{-\/-model-flat24}
6319 Generate 24-bit flat mode code.
6320 This is the one and only that the ds390 code generator supports right now
6321 and is default when using
6326 See section Memory Models for more details.
6328 \labelwidthstring 00.00.0000
6343 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6349 disable interrupts during ESP:SP updates.
6351 \labelwidthstring 00.00.0000
6368 \begin_inset LatexCommand \index{-\/-stack-10bit}
6372 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6373 This is the one and only that the ds390 code generator supports right now
6374 and is default when using
6379 In this mode, the stack is located in the lower 1K of the internal RAM,
6380 which is mapped to 0x400000.
6381 Note that the support is incomplete, since it still uses a single byte
6382 as the stack pointer.
6383 This means that only the lower 256 bytes of the potential 1K stack space
6384 will actually be used.
6385 However, this does allow you to reclaim the precious 256 bytes of low RAM
6386 for use for the DATA and IDATA segments.
6387 The compiler will not generate any code to put the processor into 10 bit
6389 It is important to ensure that the processor is in this mode before calling
6390 any re-entrant functions compiled with this option.
6391 In principle, this should work with the
6404 \begin_inset LatexCommand \index{-\/-stack-auto}
6410 option, but that has not been tested.
6411 It is incompatible with the
6424 \begin_inset LatexCommand \index{-\/-xstack}
6431 It also only makes sense if the processor is in 24 bit contiguous addressing
6444 -model-flat24 option
6448 \labelwidthstring 00.00.0000
6463 \begin_inset LatexCommand \index{-\/-stack-probe}
6469 insert call to function __stack_probe at each function prologue.
6471 \labelwidthstring 00.00.0000
6486 \begin_inset LatexCommand \index{-\/-tini-libid}
6492 <nnnn> LibraryID used in -mTININative.
6495 \labelwidthstring 00.00.0000
6510 \begin_inset LatexCommand \index{-\/-use-accelerator}
6516 generate code for DS390 Arithmetic Accelerator.
6521 \begin_inset LatexCommand \index{Options Z80}
6526 \begin_inset LatexCommand \index{Z80 options}
6532 \labelwidthstring 00.00.0000
6549 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6559 Force a called function to always save BC.
6561 \labelwidthstring 00.00.0000
6578 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6582 When linking, skip the standard crt0.o object file.
6583 You must provide your own crt0.o for your system when linking.
6587 Optimization Options
6588 \begin_inset LatexCommand \index{Options optimization}
6593 \begin_inset LatexCommand \index{Optimization options}
6599 \labelwidthstring 00.00.0000
6614 \begin_inset LatexCommand \index{-\/-nogcse}
6620 Will not do global subexpression elimination, this option may be used when
6621 the compiler creates undesirably large stack/data spaces to store compiler
6631 \begin_inset LatexCommand \index{sloc (spill location)}
6636 A warning message will be generated when this happens and the compiler
6637 will indicate the number of extra bytes it allocated.
6638 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6640 \begin_inset LatexCommand \index{\#pragma nogcse}
6644 can be used to turn off global subexpression elimination
6645 \begin_inset LatexCommand \index{Subexpression elimination}
6649 for a given function only.
6651 \labelwidthstring 00.00.0000
6666 \begin_inset LatexCommand \index{-\/-noinvariant}
6672 Will not do loop invariant optimizations, this may be turned off for reasons
6673 explained for the previous option.
6674 For more details of loop optimizations performed see Loop Invariants in
6676 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6681 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6683 \begin_inset LatexCommand \index{\#pragma noinvariant}
6687 can be used to turn off invariant optimizations for a given function only.
6689 \labelwidthstring 00.00.0000
6704 \begin_inset LatexCommand \index{-\/-noinduction}
6710 Will not do loop induction optimizations, see section strength reduction
6712 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6714 \begin_inset LatexCommand \index{\#pragma noinduction}
6718 can be used to turn off induction optimizations for a given function only.
6720 \labelwidthstring 00.00.0000
6735 \begin_inset LatexCommand \index{-\/-nojtbound}
6746 Will not generate boundary condition check when switch statements
6747 \begin_inset LatexCommand \index{switch statement}
6751 are implemented using jump-tables.
6753 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6758 Switch Statements for more details.
6759 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6761 \begin_inset LatexCommand \index{\#pragma nojtbound}
6765 can be used to turn off boundary checking for jump tables for a given function
6768 \labelwidthstring 00.00.0000
6783 \begin_inset LatexCommand \index{-\/-noloopreverse}
6792 Will not do loop reversal
6793 \begin_inset LatexCommand \index{Loop reversing}
6799 \labelwidthstring 00.00.0000
6816 \begin_inset LatexCommand \index{-\/-nolabelopt }
6820 Will not optimize labels (makes the dumpfiles more readable).
6822 \labelwidthstring 00.00.0000
6837 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6843 Will not memcpy initialized data from code space into xdata space.
6844 This saves a few bytes in code space if you don't have initialized data
6845 \begin_inset LatexCommand \index{Variable initialization}
6851 \labelwidthstring 00.00.0000
6866 \begin_inset LatexCommand \index{-\/-nooverlay}
6872 The compiler will not overlay parameters and local variables of any function,
6873 see section Parameters and local variables for more details.
6875 \labelwidthstring 00.00.0000
6890 \begin_inset LatexCommand \index{-\/-no-peep}
6896 Disable peep-hole optimization.
6898 \labelwidthstring 00.00.0000
6915 \begin_inset LatexCommand \index{-\/-peep-file}
6920 <filename> This option can be used to use additional rules to be used by
6921 the peep hole optimizer.
6923 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6928 Peep Hole optimizations for details on how to write these rules.
6930 \labelwidthstring 00.00.0000
6945 \begin_inset LatexCommand \index{-\/-peep-asm}
6951 Pass the inline assembler code through the peep hole optimizer.
6952 This can cause unexpected changes to inline assembler code, please go through
6953 the peephole optimizer
6954 \begin_inset LatexCommand \index{Peephole optimizer}
6958 rules defined in the source file tree '<target>/peeph.def' before using
6961 \labelwidthstring 00.00.0000
6976 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6982 The compiler will optimize code generation towards fast code, possibly
6983 at the expense of code size.
6985 \labelwidthstring 00.00.0000
7000 \begin_inset LatexCommand \index{-\/-opt-code-size}
7006 The compiler will optimize code generation towards compact code, possibly
7007 at the expense of code speed.
7011 \begin_inset LatexCommand \index{Options other}
7017 \labelwidthstring 00.00.0000
7033 \begin_inset LatexCommand \index{-\/-compile-only}
7038 \begin_inset LatexCommand \index{-c -\/-compile-only}
7044 will compile and assemble the source, but will not call the linkage editor.
7046 \labelwidthstring 00.00.0000
7065 \begin_inset LatexCommand \index{-\/-c1mode}
7071 reads the preprocessed source from standard input and compiles it.
7072 The file name for the assembler output must be specified using the -o option.
7074 \labelwidthstring 00.00.0000
7079 \begin_inset LatexCommand \index{-E}
7085 Run only the C preprocessor.
7086 Preprocess all the C source files specified and output the results to standard
7089 \labelwidthstring 00.00.0000
7095 \begin_inset LatexCommand \index{-o <path/file>}
7101 The output path resp.
7102 file where everything will be placed.
7103 If the parameter is a path, it must have a trailing slash (or backslash
7104 for the Windows binaries) to be recognized as a path.
7107 \labelwidthstring 00.00.0000
7122 \begin_inset LatexCommand \index{-\/-stack-auto}
7133 All functions in the source file will be compiled as
7138 \begin_inset LatexCommand \index{reentrant}
7143 the parameters and local variables will be allocated on the stack
7144 \begin_inset LatexCommand \index{stack}
7150 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7154 Parameters and Local Variables for more details.
7155 If this option is used all source files in the project should be compiled
7157 It automatically implies --int-long-reent and --float-reent.
7160 \labelwidthstring 00.00.0000
7175 \begin_inset LatexCommand \index{-\/-callee-saves}
7179 function1[,function2][,function3]....
7182 The compiler by default uses a caller saves convention for register saving
7183 across function calls, however this can cause unnecessary register pushing
7184 & popping when calling small functions from larger functions.
7185 This option can be used to switch the register saving convention for the
7186 function names specified.
7187 The compiler will not save registers when calling these functions, no extra
7188 code will be generated at the entry & exit (function prologue
7191 \begin_inset LatexCommand \index{function prologue}
7200 \begin_inset LatexCommand \index{function epilogue}
7206 ) for these functions to save & restore the registers used by these functions,
7207 this can SUBSTANTIALLY reduce code & improve run time performance of the
7209 In the future the compiler (with inter procedural analysis) will be able
7210 to determine the appropriate scheme to use for each function call.
7211 DO NOT use this option for built-in functions such as _mulint..., if this
7212 option is used for a library function the appropriate library function
7213 needs to be recompiled with the same option.
7214 If the project consists of multiple source files then all the source file
7215 should be compiled with the same -
7225 -callee-saves option string.
7226 Also see #pragma\SpecialChar ~
7228 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7234 \labelwidthstring 00.00.0000
7249 \begin_inset LatexCommand \index{-\/-debug}
7258 When this option is used the compiler will generate debug information.
7259 The debug information collected in a file with .cdb extension can be used
7261 For more information see documentation for SDCDB.
7262 Another file with no extension contains debug information in AOMF or AOMF51
7263 \begin_inset LatexCommand \index{AOMF, AOMF51}
7267 format which is commonly used by third party tools.
7269 \labelwidthstring 00.00.0000
7274 \begin_inset LatexCommand \index{-S}
7285 Stop after the stage of compilation proper; do not assemble.
7286 The output is an assembler code file for the input file specified.
7288 \labelwidthstring 00.00.0000
7303 \begin_inset LatexCommand \index{-\/-int-long-reent}
7309 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7310 Note by default these libraries are compiled as non-reentrant.
7311 See section Installation for more details.
7313 \labelwidthstring 00.00.0000
7328 \begin_inset LatexCommand \index{-\/-cyclomatic}
7337 This option will cause the compiler to generate an information message for
7338 each function in the source file.
7339 The message contains some
7343 information about the function.
7344 The number of edges and nodes the compiler detected in the control flow
7345 graph of the function, and most importantly the
7347 cyclomatic complexity
7348 \begin_inset LatexCommand \index{Cyclomatic complexity}
7354 see section on Cyclomatic Complexity for more details.
7356 \labelwidthstring 00.00.0000
7371 \begin_inset LatexCommand \index{-\/-float-reent}
7377 Floating point library is compiled as reentrant
7378 \begin_inset LatexCommand \index{reentrant}
7383 See section Installation for more details.
7385 \labelwidthstring 00.00.0000
7400 \begin_inset LatexCommand \index{-\/-main-return}
7406 This option can be used if the code generated is called by a monitor program
7407 or if the main routine includes an endless loop.
7408 This option might result in slightly smaller code and save two bytes of
7410 The return from the 'main'
7411 \begin_inset LatexCommand \index{main return}
7415 function will return to the function calling main.
7416 The default setting is to lock up i.e.
7423 \labelwidthstring 00.00.0000
7438 \begin_inset LatexCommand \index{-\/-nostdinc}
7444 This will prevent the compiler from passing on the default include path
7445 to the preprocessor.
7447 \labelwidthstring 00.00.0000
7462 \begin_inset LatexCommand \index{-\/-nostdlib}
7468 This will prevent the compiler from passing on the default library
7469 \begin_inset LatexCommand \index{Libraries}
7475 \labelwidthstring 00.00.0000
7490 \begin_inset LatexCommand \index{-\/-verbose}
7496 Shows the various actions the compiler is performing.
7498 \labelwidthstring 00.00.0000
7503 \begin_inset LatexCommand \index{-V}
7509 Shows the actual commands the compiler is executing.
7511 \labelwidthstring 00.00.0000
7526 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7532 Hides your ugly and inefficient c-code from the asm file, so you can always
7533 blame the compiler :)
7535 \labelwidthstring 00.00.0000
7550 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7556 Will not include peep-hole comments in the generated files.
7558 \labelwidthstring 00.00.0000
7573 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7579 Include i-codes in the asm file.
7580 Sounds like noise but is most helpful for debugging the compiler itself.
7582 \labelwidthstring 00.00.0000
7597 \begin_inset LatexCommand \index{-\/-less-pedantic}
7603 Disable some of the more pedantic warnings
7604 \begin_inset LatexCommand \index{Warnings}
7608 (jwk burps: please be more specific here, please!).
7610 \labelwidthstring 00.00.0000
7624 -disable-warning\SpecialChar ~
7626 \begin_inset LatexCommand \index{-\/-disable-warning}
7632 Disable specific warning with number <nnnn>.
7634 \labelwidthstring 00.00.0000
7649 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7655 Display the directories in the compiler's search path
7657 \labelwidthstring 00.00.0000
7672 \begin_inset LatexCommand \index{-\/-vc}
7678 Display errors and warnings using MSVC style, so you can use SDCC with
7681 \labelwidthstring 00.00.0000
7696 \begin_inset LatexCommand \index{-\/-use-stdout}
7702 Send errors and warnings to stdout instead of stderr.
7704 \labelwidthstring 00.00.0000
7709 asmOption[,asmOption]
7712 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7717 Pass the asmOption to the assembler
7718 \begin_inset LatexCommand \index{Options assembler}
7723 \begin_inset LatexCommand \index{Assembler options}
7728 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7730 \labelwidthstring 00.00.0000
7745 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7751 Generally follow the C89 standard, but allow SDCC features that conflict
7752 with the standard (default).
7754 \labelwidthstring 00.00.0000
7769 \begin_inset LatexCommand \index{-\/-std-c89}
7775 Follow the C89 standard and disable SDCC features that conflict with the
7778 \labelwidthstring 00.00.0000
7793 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7799 Generally follow the C99 standard, but allow SDCC features that conflict
7800 with the standard (incomplete support).
7802 \labelwidthstring 00.00.0000
7817 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7823 Follow the C99 standard and disable SDCC features that conflict with the
7824 standard (incomplete support).
7826 \labelwidthstring 00.00.0000
7843 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
7848 <Name> The name to be used for the code
7849 \begin_inset LatexCommand \index{code}
7853 segment, default CSEG.
7854 This is useful if you need to tell the compiler to put the code in a special
7855 segment so you can later on tell the linker to put this segment in a special
7857 Can be used for instance when using bank switching to put the code in a
7860 \labelwidthstring 00.00.0000
7877 \begin_inset LatexCommand \index{-\/-constseg <Value>}
7882 <Name> The name to be used for the const
7883 \begin_inset LatexCommand \index{code}
7887 segment, default CONST.
7888 This is useful if you need to tell the compiler to put the const data in
7889 a special segment so you can later on tell the linker to put this segment
7890 in a special place in memory.
7891 Can be used for instance when using bank switching to put the const data
7894 \labelwidthstring 00.00.0000
7906 a SDCC compiler option but if you want
7910 warnings you can use a separate tool dedicated to syntax checking like
7912 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
7917 \begin_inset LatexCommand \index{lint (syntax checking tool)}
7922 \begin_inset LatexCommand \url{http://www.splint.org}
7927 To make your source files parseable by splint you will have to include
7933 \begin_inset LatexCommand \index{splint (syntax checking tool)}
7937 in your source file and add brackets around extended keywords (like
7940 \begin_inset Quotes sld
7953 \begin_inset Quotes srd
7961 \begin_inset Quotes sld
7964 __interrupt\SpecialChar ~
7966 \begin_inset Quotes srd
7974 Splint has an excellent on line manual at
7975 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
7979 and it's capabilities go beyond pure syntax checking.
7980 You'll need to tell splint the location of SDCC's include files so a typical
7981 command line could look like this:
7985 splint\SpecialChar ~
7987 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
7992 Intermediate Dump Options
7993 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7998 \begin_inset LatexCommand \index{Options intermediate dump}
8003 \begin_inset LatexCommand \index{Intermediate dump options}
8010 The following options are provided for the purpose of retargetting and debugging
8012 They provide a means to dump the intermediate code (iCode
8013 \begin_inset LatexCommand \index{iCode}
8017 ) generated by the compiler in human readable form at various stages of
8018 the compilation process.
8019 More on iCodes see chapter
8020 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8025 \begin_inset Quotes srd
8028 The anatomy of the compiler
8029 \begin_inset Quotes srd
8034 \labelwidthstring 00.00.0000
8049 \begin_inset LatexCommand \index{-\/-dumpraw}
8055 This option will cause the compiler to dump the intermediate code into
8058 <source filename>.dumpraw
8060 just after the intermediate code has been generated for a function, i.e.
8061 before any optimizations are done.
8063 \begin_inset LatexCommand \index{Basic blocks}
8067 at this stage ordered in the depth first number, so they may not be in
8068 sequence of execution.
8070 \labelwidthstring 00.00.0000
8085 \begin_inset LatexCommand \index{-\/-dumpgcse}
8091 Will create a dump of iCode's, after global subexpression elimination
8092 \begin_inset LatexCommand \index{Global subexpression elimination}
8098 <source filename>.dumpgcse.
8100 \labelwidthstring 00.00.0000
8115 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8121 Will create a dump of iCode's, after deadcode elimination
8122 \begin_inset LatexCommand \index{Dead-code elimination}
8128 <source filename>.dumpdeadcode.
8130 \labelwidthstring 00.00.0000
8145 \begin_inset LatexCommand \index{-\/-dumploop}
8154 Will create a dump of iCode's, after loop optimizations
8155 \begin_inset LatexCommand \index{Loop optimization}
8161 <source filename>.dumploop.
8163 \labelwidthstring 00.00.0000
8178 \begin_inset LatexCommand \index{-\/-dumprange}
8187 Will create a dump of iCode's, after live range analysis
8188 \begin_inset LatexCommand \index{Live range analysis}
8194 <source filename>.dumprange.
8196 \labelwidthstring 00.00.0000
8211 \begin_inset LatexCommand \index{-\/-dumlrange}
8217 Will dump the life ranges
8218 \begin_inset LatexCommand \index{Live range analysis}
8224 \labelwidthstring 00.00.0000
8239 \begin_inset LatexCommand \index{-\/-dumpregassign}
8248 Will create a dump of iCode's, after register assignment
8249 \begin_inset LatexCommand \index{Register assignment}
8255 <source filename>.dumprassgn.
8257 \labelwidthstring 00.00.0000
8272 \begin_inset LatexCommand \index{-\/-dumplrange}
8278 Will create a dump of the live ranges of iTemp's
8280 \labelwidthstring 00.00.0000
8295 \begin_inset LatexCommand \index{-\/-dumpall}
8306 Will cause all the above mentioned dumps to be created.
8309 Redirecting output on Windows Shells
8312 By default SDCC writes it's error messages to
8313 \begin_inset Quotes sld
8317 \begin_inset Quotes srd
8321 To force all messages to
8322 \begin_inset Quotes sld
8326 \begin_inset Quotes srd
8350 \begin_inset LatexCommand \index{-\/-use-stdout}
8355 Additionally, if you happen to have visual studio installed in your windows
8356 machine, you can use it to compile your sources using a custom build and
8372 \begin_inset LatexCommand \index{-\/-vc}
8377 Something like this should work:
8421 -model-large -c $(InputPath)
8424 Environment variables
8425 \begin_inset LatexCommand \index{Environment variables}
8432 SDCC recognizes the following environment variables:
8434 \labelwidthstring 00.00.0000
8439 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8445 SDCC installs a signal handler
8446 \begin_inset LatexCommand \index{signal handler}
8450 to be able to delete temporary files after an user break (^C) or an exception.
8451 If this environment variable is set, SDCC won't install the signal handler
8452 in order to be able to debug SDCC.
8454 \labelwidthstring 00.00.0000
8461 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8467 Path, where temporary files will be created.
8468 The order of the variables is the search order.
8469 In a standard *nix environment these variables are not set, and there's
8470 no need to set them.
8471 On Windows it's recommended to set one of them.
8473 \labelwidthstring 00.00.0000
8478 \begin_inset LatexCommand \index{SDCC\_HOME}
8485 \begin_inset LatexCommand \ref{sub:Install-paths}
8491 \begin_inset Quotes sld
8495 \begin_inset Quotes srd
8500 \labelwidthstring 00.00.0000
8505 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8512 \begin_inset LatexCommand \ref{sub:Search-Paths}
8518 \begin_inset Quotes sld
8522 \begin_inset Quotes srd
8527 \labelwidthstring 00.00.0000
8532 \begin_inset LatexCommand \index{SDCC\_LIB}
8539 \begin_inset LatexCommand \ref{sub:Search-Paths}
8545 \begin_inset Quotes sld
8549 \begin_inset Quotes srd
8555 There are some more environment variables recognized by SDCC, but these
8556 are solely used for debugging purposes.
8557 They can change or disappear very quickly, and will never be documented.
8560 Storage Class Language Extensions
8563 MCS51/DS390 Storage Class
8564 \begin_inset LatexCommand \index{Storage class}
8571 In addition to the ANSI storage classes SDCC allows the following MCS51
8572 specific storage classes:
8573 \layout Subsubsection
8576 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8581 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8586 \begin_inset LatexCommand \index{near (storage class)}
8591 \begin_inset LatexCommand \index{\_\_near (storage class)}
8602 storage class for the Small Memory model (
8610 can be used synonymously).
8611 Variables declared with this storage class will be allocated in the directly
8612 addressable portion of the internal RAM of a 8051, e.g.:
8617 data unsigned char test_data;
8620 Writing 0x01 to this variable generates the assembly code:
8625 75*00 01\SpecialChar ~
8631 \layout Subsubsection
8634 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8639 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8644 \begin_inset LatexCommand \index{far (storage class)}
8649 \begin_inset LatexCommand \index{\_\_far (storage class)}
8656 Variables declared with this storage class will be placed in the external
8662 storage class for the Large Memory model, e.g.:
8667 xdata unsigned char test_xdata;
8670 Writing 0x01 to this variable generates the assembly code:
8675 90s00r00\SpecialChar ~
8704 \layout Subsubsection
8707 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8712 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8719 Variables declared with this storage class will be allocated into the indirectly
8720 addressable portion of the internal ram of a 8051, e.g.:
8725 idata unsigned char test_idata;
8728 Writing 0x01 to this variable generates the assembly code:
8757 Please note, the first 128 byte of idata physically access the same RAM
8759 The original 8051 had 128 byte idata memory, nowadays most devices have
8760 256 byte idata memory.
8762 \begin_inset LatexCommand \index{stack}
8766 is located in idata memory.
8767 \layout Subsubsection
8770 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8775 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8782 Paged xdata access is just as straightforward as using the other addressing
8784 It is typically located at the start of xdata and has a maximum size of
8786 The following example writes 0x01 to the pdata variable.
8787 Please note, pdata access physically accesses xdata memory.
8788 The high byte of the address is determined by port P2
8789 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8793 (or in case of some 8051 variants by a separate Special Function Register,
8795 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8804 storage class for the Medium Memory model, e.g.:
8809 pdata unsigned char test_pdata;
8812 Writing 0x01 to this variable generates the assembly code:
8856 \begin_inset LatexCommand \index{-\/-xstack}
8860 option is used the pdata memory area is followed by the xstack memory area
8861 and the sum of their sizes is limited to 256 bytes.
8862 \layout Subsubsection
8865 \begin_inset LatexCommand \index{code}
8870 \begin_inset LatexCommand \index{\_\_code}
8877 'Variables' declared with this storage class will be placed in the code
8883 code unsigned char test_code;
8886 Read access to this variable generates the assembly code:
8891 90s00r6F\SpecialChar ~
8894 mov dptr,#_test_code
8923 indexed arrays of characters in code memory can be accessed efficiently:
8928 code char test_array[] = {'c','h','e','a','p'};
8931 Read access to this array using an 8-bit unsigned index generates the assembly
8948 90s00r41\SpecialChar ~
8951 mov dptr,#_test_array
8966 \layout Subsubsection
8969 \begin_inset LatexCommand \index{bit}
8974 \begin_inset LatexCommand \index{\_\_bit}
8981 This is a data-type and a storage class specifier.
8982 When a variable is declared as a bit, it is allocated into the bit addressable
8983 memory of 8051, e.g.:
8991 Writing 1 to this variable generates the assembly code:
9007 The bit addressable memory consists of 128 bits which are located from 0x20
9008 to 0x2f in data memory.
9011 Apart from this 8051 specific storage class most architectures support ANSI-C
9013 \begin_inset LatexCommand \index{bitfields}
9023 Not really meant as examples, but nevertheless showing what bitfields are
9024 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9028 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9029 signed modifier are implemented as unsigned.
9030 \layout Subsubsection
9033 \begin_inset LatexCommand \index{sfr}
9038 \begin_inset LatexCommand \index{\_\_sfr}
9043 \begin_inset LatexCommand \index{sfr16}
9048 \begin_inset LatexCommand \index{\_\_sfr16}
9053 \begin_inset LatexCommand \index{sfr32}
9058 \begin_inset LatexCommand \index{\_\_sfr32}
9063 \begin_inset LatexCommand \index{\_\_sbit}
9070 Like the bit keyword,
9072 sfr / sfr16 / sfr32 / sbit
9074 signify both a data-type and storage class, they are used to describe the
9095 variables of a 8051, eg:
9101 \begin_inset LatexCommand \index{at}
9106 \begin_inset LatexCommand \index{\_\_at}
9110 0x80 P0;\SpecialChar ~
9111 /* special function register P0 at location 0x80 */
9113 /* 16 bit special function register combination for timer 0 */
9115 /* with the high byte at location 0x8C and the low byte at location 0x8A
9119 \begin_inset LatexCommand \index{at}
9124 \begin_inset LatexCommand \index{\_\_at}
9130 sbit at 0xd7 CY; /* CY (Carry Flag
9131 \begin_inset LatexCommand \index{Flags}
9136 \begin_inset LatexCommand \index{Carry flag}
9143 Special function registers which are located on an address dividable by
9144 8 are bit-addressable, an
9148 addresses a specific bit within these sfr.
9150 16 Bit and 32 bit special function register combinations which require a
9151 certain access order are better not declared using
9160 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9161 this is not guaranteed.
9162 \layout Subsubsection
9165 \begin_inset LatexCommand \index{Pointer}
9169 to MCS51/DS390 specific memory spaces
9172 SDCC allows (via language extensions) pointers to explicitly point to any
9173 of the memory spaces
9174 \begin_inset LatexCommand \index{Memory model}
9179 In addition to the explicit pointers, the compiler uses (by default) generic
9180 pointers which can be used to point to any of the memory spaces.
9184 Pointer declaration examples:
9189 /* pointer physically in internal ram pointing to object in external ram
9192 xdata unsigned char * data p;
9196 /* pointer physically in external ram pointing to object in internal ram
9199 data unsigned char * xdata p;
9203 /* pointer physically in code rom pointing to data in xdata space */
9205 xdata unsigned char * code p;
9209 /* pointer physically in code space pointing to data in code space */
9211 code unsigned char * code p;
9215 /* the following is a generic pointer physically located in xdata space
9222 /* the following is a function pointer physically located in data space
9225 char (* data fp)(void);
9228 Well you get the idea.
9233 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9246 The highest order byte of the
9250 pointers contains the data space information.
9251 Assembler support routines are called whenever data is stored or retrieved
9257 These are useful for developing reusable library
9258 \begin_inset LatexCommand \index{Libraries}
9263 Explicitly specifying the pointer type will generate the most efficient
9265 \layout Subsubsection
9267 Notes on MCS51 memory
9268 \begin_inset LatexCommand \index{MCS51 memory}
9275 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9276 RAM memory which is structured as follows:
9280 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9283 - Bytes 20-2F - 16 bytes to hold 128 bit
9284 \begin_inset LatexCommand \index{bit}
9290 - Bytes 30-7F - 80 bytes for general purpose use.
9295 Additionally some members of the MCS51 family may have up to 128 bytes of
9296 additional, indirectly addressable, internal RAM memory (
9301 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9306 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9311 Furthermore, some chips may have some built in external memory (
9316 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9321 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9325 ) which should not be confused with the internal, directly addressable RAM
9331 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9336 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9341 Sometimes this built in
9345 memory has to be activated before using it (you can probably find this
9346 information on the datasheet of the microcontroller your are using, see
9348 \begin_inset LatexCommand \ref{sub:Startup-Code}
9356 Normally SDCC will only use the first bank
9357 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9361 of registers (register bank 0), but it is possible to specify that other
9362 banks of registers (keyword
9369 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9374 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9380 ) should be used in interrupt
9381 \begin_inset LatexCommand \index{interrupt}
9386 \begin_inset LatexCommand \index{\_\_interrupt}
9391 By default, the compiler will place the stack after the last byte of allocated
9392 memory for variables.
9393 For example, if the first 2 banks of registers are used, and only four
9398 variables, it will position the base of the internal stack at address 20
9400 This implies that as the stack
9401 \begin_inset LatexCommand \index{stack}
9405 grows, it will use up the remaining register banks, and the 16 bytes used
9406 by the 128 bit variables, and 80 bytes for general purpose use.
9407 If any bit variables are used, the data variables will be placed in unused
9408 register banks and after the byte holding the last bit variable.
9409 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9414 variables will be placed starting from address 0x10 to 0x20 and continue
9427 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9431 to specify the start address of the
9446 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9450 to specify the size of the total internal RAM (
9462 By default the 8051 linker will place the stack after the last byte of (i)data
9475 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9479 allows you to specify the start of the stack, i.e.
9480 you could start it after any data in the general purpose area.
9481 If your microcontroller has additional indirectly addressable internal
9486 ) you can place the stack on it.
9487 You may also need to use -
9498 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9502 to set the start address of the external RAM (
9517 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9521 to specify its size.
9522 Same goes for the code memory, using -
9533 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9548 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9553 If in doubt, don't specify any options and see if the resulting memory
9554 layout is appropriate, then you can adjust it.
9557 The linker generates two files with memory allocation information.
9558 The first, with extension .map
9559 \begin_inset LatexCommand \index{<file>.map}
9563 shows all the variables and segments.
9564 The second with extension .mem
9565 \begin_inset LatexCommand \index{<file>.mem}
9569 shows the final memory layout.
9570 The linker will complain either if memory segments overlap, there is not
9571 enough memory, or there is not enough space for stack.
9572 If you get any linking warnings and/or errors related to stack or segments
9573 allocation, take a look at either the .map or .mem files to find out what
9575 The .mem file may even suggest a solution to the problem.
9578 Z80/Z180 Storage Class
9579 \begin_inset LatexCommand \index{Storage class}
9584 \layout Subsubsection
9587 \begin_inset LatexCommand \index{sfr}
9592 \begin_inset LatexCommand \index{\_\_sfr}
9596 (in/out to 8-bit addresses)
9600 \begin_inset LatexCommand \index{Z80}
9604 family has separate address spaces for memory and
9614 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9618 is accessed with special instructions, e.g.:
9623 sfr at 0x78 IoPort;\SpecialChar ~
9625 /* define a var in I/O space at 78h called IoPort */
9629 Writing 0x01 to this variable generates the assembly code:
9649 \layout Subsubsection
9652 \begin_inset LatexCommand \index{sfr}
9657 \begin_inset LatexCommand \index{\_\_sfr}
9661 (in/out to 16-bit addresses)
9668 is used to support 16 bit addresses in I/O memory e.g.:
9674 \begin_inset LatexCommand \index{at}
9679 \begin_inset LatexCommand \index{\_\_at}
9686 Writing 0x01 to this variable generates the assembly code:
9691 01 23 01\SpecialChar ~
9711 \layout Subsubsection
9714 \begin_inset LatexCommand \index{sfr}
9719 \begin_inset LatexCommand \index{\_\_sfr}
9723 (in0/out0 to 8 bit addresses on Z180
9724 \begin_inset LatexCommand \index{Z180}
9729 \begin_inset LatexCommand \index{HD64180}
9736 The compiler option -
9746 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9748 \begin_inset LatexCommand \index{\#pragma portmode}
9752 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9762 If you include the file z180.h this will be set automatically.
9766 \begin_inset LatexCommand \index{Storage class}
9771 \layout Subsubsection
9774 \begin_inset LatexCommand \index{data (hc08 storage class)}
9779 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9786 The data storage class declares a variable that resides in the first 256
9787 bytes of memory (the direct page).
9788 The HC08 is most efficient at accessing variables (especially pointers)
9790 \layout Subsubsection
9793 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9798 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9805 The xdata storage class declares a variable that can reside anywhere in
9807 This is the default if no storage class is specified.
9812 \begin_inset LatexCommand \index{Absolute addressing}
9819 Data items can be assigned an absolute address with the
9822 \begin_inset LatexCommand \index{at}
9827 \begin_inset LatexCommand \index{\_\_at}
9833 keyword, in addition to a storage class, e.g.:
9839 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9844 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9849 \begin_inset LatexCommand \index{at}
9854 \begin_inset LatexCommand \index{\_\_at}
9858 0x7ffe unsigned int chksum;
9861 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9862 of the external ram.
9867 reserve any space for variables declared in this way (they are implemented
9868 with an equate in the assembler).
9869 Thus it is left to the programmer to make sure there are no overlaps with
9870 other variables that are declared without the absolute address.
9871 The assembler listing file (.lst
9872 \begin_inset LatexCommand \index{<file>.lst}
9876 ) and the linker output files (.rst
9877 \begin_inset LatexCommand \index{<file>.rst}
9882 \begin_inset LatexCommand \index{<file>.map}
9886 ) are good places to look for such overlaps.
9887 Variables with an absolute address are
9892 \begin_inset LatexCommand \index{Variable initialization}
9899 In case of memory mapped I/O devices the keyword
9903 has to be used to tell the compiler that accesses might not be removed:
9909 \begin_inset LatexCommand \index{volatile}
9914 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9919 \begin_inset LatexCommand \index{at}
9923 0x8000 unsigned char PORTA_8255;
9926 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9931 \begin_inset LatexCommand \index{Aligned array}
9938 starts at a block (256 byte) boundary
9939 \begin_inset LatexCommand \index{block boundary}
9944 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9950 Absolute addresses can be specified for variables in all storage classes,
9957 \begin_inset LatexCommand \index{bit}
9962 \begin_inset LatexCommand \index{at}
9969 The above example will allocate the variable at offset 0x02 in the bit-addressab
9971 There is no real advantage to assigning absolute addresses to variables
9972 in this manner, unless you want strict control over all the variables allocated.
9973 One possible use would be to write hardware portable code.
9974 For example, if you have a routine that uses one or more of the microcontroller
9975 I/O pins, and such pins are different for two different hardwares, you
9976 can declare the I/O pins in your routine using:
9982 \begin_inset LatexCommand \index{volatile}
9986 bit MOSI;\SpecialChar ~
9990 /* master out, slave in */
9992 extern volatile bit MISO;\SpecialChar ~
9996 /* master in, slave out */
9998 extern volatile bit MCLK;\SpecialChar ~
10006 /* Input and Output of a byte on a 3-wire serial bus.
10011 If needed adapt polarity of clock, polarity of data and bit order
10016 unsigned char spi_io(unsigned char out_byte)
10040 MOSI = out_byte & 0x80;
10070 /* _asm nop _endasm; */\SpecialChar ~
10078 /* for slow peripherals */
10129 Then, someplace in the code for the first hardware you would use
10135 \begin_inset LatexCommand \index{at}
10140 \begin_inset LatexCommand \index{\_\_at}
10144 0x80 MOSI;\SpecialChar ~
10148 /* I/O port 0, bit 0 */
10150 bit at 0x81 MISO;\SpecialChar ~
10154 /* I/O port 0, bit 1 */
10156 bit at 0x82 MCLK;\SpecialChar ~
10160 /* I/O port 0, bit 2 */
10163 Similarly, for the second hardware you would use
10168 bit at 0x83 MOSI;\SpecialChar ~
10172 /* I/O port 0, bit 3 */
10174 bit at 0x91 MISO;\SpecialChar ~
10178 /* I/O port 1, bit 1 */
10181 \begin_inset LatexCommand \index{bit}
10185 at 0x92 MCLK;\SpecialChar ~
10189 /* I/O port 1, bit 2 */
10192 and you can use the same hardware dependent routine without changes, as
10193 for example in a library.
10194 This is somehow similar to sbit, but only one absolute address has to be
10195 specified in the whole project.
10199 \begin_inset LatexCommand \index{Parameters}
10204 \begin_inset LatexCommand \index{function parameter}
10209 \begin_inset LatexCommand \index{local variables}
10214 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10221 Automatic (local) variables and parameters to functions can either be placed
10222 on the stack or in data-space.
10223 The default action of the compiler is to place these variables in the internal
10224 RAM (for small model) or external RAM (for large model).
10225 This in fact makes them similar to
10228 \begin_inset LatexCommand \index{static}
10234 so by default functions are non-reentrant
10235 \begin_inset LatexCommand \index{reentrant}
10244 They can be placed on the stack
10245 \begin_inset LatexCommand \index{stack}
10262 \begin_inset LatexCommand \index{-\/-stack-auto}
10270 #pragma\SpecialChar ~
10274 \begin_inset LatexCommand \index{\#pragma stackauto}
10281 \begin_inset LatexCommand \index{reentrant}
10287 keyword in the function declaration, e.g.:
10292 unsigned char foo(char i) reentrant
10306 Since stack space on 8051 is limited, the
10324 option should be used sparingly.
10325 Note that the reentrant keyword just means that the parameters & local
10326 variables will be allocated to the stack, it
10330 mean that the function is register bank
10331 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10340 \begin_inset LatexCommand \index{local variables}
10344 can be assigned storage classes and absolute
10345 \begin_inset LatexCommand \index{Absolute addressing}
10354 unsigned char foo()
10362 xdata unsigned char i;
10375 \begin_inset LatexCommand \index{at}
10379 0x31 unsigned char j;
10391 In the above example the variable
10395 will be allocated in the external ram,
10399 in bit addressable space and
10418 or when a function is declared as
10422 this should only be done for static variables.
10426 \begin_inset LatexCommand \index{function parameter}
10430 however are not allowed any storage class
10431 \begin_inset LatexCommand \index{Storage class}
10435 , (storage classes for parameters will be ignored), their allocation is
10436 governed by the memory model in use, and the reentrancy options.
10439 It is however allowed to use bit parameters in reentrant functions and also
10440 non-static local bit variables are supported.
10441 Efficient use is limited to 8 semi-bitregisters in bit space.
10442 They are pushed and popped to stack as a single byte just like the normal
10447 \begin_inset LatexCommand \label{sub:Overlaying}
10452 \begin_inset LatexCommand \index{Overlaying}
10460 \begin_inset LatexCommand \index{reentrant}
10464 functions SDCC will try to reduce internal ram space usage by overlaying
10465 parameters and local variables of a function (if possible).
10466 Parameters and local variables
10467 \begin_inset LatexCommand \index{local variables}
10471 of a function will be allocated to an overlayable segment if the function
10474 no other function calls and the function is non-reentrant and the memory
10476 \begin_inset LatexCommand \index{Memory model}
10483 If an explicit storage class
10484 \begin_inset LatexCommand \index{Storage class}
10488 is specified for a local variable, it will NOT be overlayed.
10491 Note that the compiler (not the linkage editor) makes the decision for overlayin
10493 Functions that are called from an interrupt service routine should be preceded
10494 by a #pragma\SpecialChar ~
10496 \begin_inset LatexCommand \index{\#pragma nooverlay}
10500 if they are not reentrant.
10503 Also note that the compiler does not do any processing of inline assembler
10504 code, so the compiler might incorrectly assign local variables and parameters
10505 of a function into the overlay segment if the inline assembler code calls
10506 other c-functions that might use the overlay.
10507 In that case the #pragma\SpecialChar ~
10508 nooverlay should be used.
10511 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10513 \begin_inset LatexCommand \index{Multiplication}
10518 \begin_inset LatexCommand \index{Division}
10522 will NOT be overlayed since these are implemented using external functions,
10531 \begin_inset LatexCommand \index{\#pragma nooverlay}
10537 void set_error(unsigned char errcd)
10553 void some_isr () interrupt
10554 \begin_inset LatexCommand \index{interrupt}
10584 In the above example the parameter
10592 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10594 not present, this could cause unpredictable runtime behavior when called
10595 from an interrupt service routine.
10596 The #pragma\SpecialChar ~
10597 nooverlay ensures that the parameters and local variables for
10598 the function are NOT overlayed.
10601 Interrupt Service Routines
10602 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10609 General Information
10624 outines to be coded in C, with some extended keywords.
10629 void timer_isr (void) interrupt 1 using 1
10643 The optional number following the
10646 \begin_inset LatexCommand \index{interrupt}
10651 \begin_inset LatexCommand \index{\_\_interrupt}
10657 keyword is the interrupt number this routine will service.
10658 When present, the compiler will insert a call to this routine in the interrupt
10659 vector table for the interrupt number specified.
10660 If you have multiple source files in your project, interrupt service routines
10661 can be present in any of them, but a prototype of the isr MUST be present
10662 or included in the file that contains the function
10670 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10675 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10681 keyword can be used to tell the compiler to use the specified register
10682 bank (8051 specific) when generating code for this function.
10688 Interrupt service routines open the door for some very interesting bugs:
10690 If an interrupt service routine changes variables which are accessed by
10691 other functions these variables have to be declared
10696 \begin_inset LatexCommand \index{volatile}
10704 If the access to these variables is not
10707 \begin_inset LatexCommand \index{atomic}
10714 the processor needs more than one instruction for the access and could
10715 be interrupted while accessing the variable) the interrupt must be disabled
10716 during the access to avoid inconsistent data.
10717 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10718 and should be protected by disabling interrupts.
10719 You're not automatically on the safe side if you use 8 bit variables though.
10720 We need an example here: f.e.
10721 on the 8051 the harmless looking
10722 \begin_inset Quotes srd
10727 flags\SpecialChar ~
10732 \begin_inset Quotes sld
10741 \begin_inset Quotes srd
10746 flags\SpecialChar ~
10751 \begin_inset Quotes sld
10754 from within an interrupt routine might get lost if the interrupt occurs
10757 \begin_inset Quotes sld
10762 counter\SpecialChar ~
10767 \begin_inset Quotes srd
10770 is not atomic on the 8051 even if
10774 is located in data memory.
10775 Bugs like these are hard to reproduce and can cause a lot of trouble.
10779 The return address and the registers used in the interrupt service routine
10780 are saved on the stack
10781 \begin_inset LatexCommand \index{stack}
10785 so there must be sufficient stack space.
10786 If there isn't variables or registers (or even the return address itself)
10793 \begin_inset LatexCommand \index{stack overflow}
10797 is most likely to happen if the interrupt occurs during the
10798 \begin_inset Quotes sld
10802 \begin_inset Quotes srd
10805 subroutine when the stack is already in use for f.e.
10806 many return addresses.
10809 A special note here, int (16 bit) and long (32 bit) integer division
10810 \begin_inset LatexCommand \index{Division}
10815 \begin_inset LatexCommand \index{Multiplication}
10820 \begin_inset LatexCommand \index{Modulus}
10825 \begin_inset LatexCommand \index{Floating point support}
10829 operations are implemented using external support routines developed in
10831 If an interrupt service routine needs to do any of these operations then
10832 the support routines (as mentioned in a following section) will have to
10833 be recompiled using the
10846 \begin_inset LatexCommand \index{-\/-stack-auto}
10852 option and the source file will need to be compiled using the
10867 \begin_inset LatexCommand \index{-\/-int-long-reent}
10874 Calling other functions from an interrupt service routine is not recommended,
10875 avoid it if possible.
10876 Note that when some function is called from an interrupt service routine
10877 it should be preceded by a #pragma\SpecialChar ~
10879 \begin_inset LatexCommand \index{\#pragma nooverlay}
10883 if it is not reentrant.
10884 Furthermore nonreentrant functions should not be called from the main program
10885 while the interrupt service routine might be active.
10891 \begin_inset LatexCommand \ref{sub:Overlaying}
10896 about Overlaying and section
10897 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10902 about Functions using private register banks.
10905 MCS51/DS390 Interrupt Service Routines
10908 Interrupt numbers and the corresponding address & descriptions for the Standard
10909 8051/8052 are listed below.
10910 SDCC will automatically adjust the interrupt vector table to the maximum
10911 interrupt number specified.
10917 \begin_inset Tabular
10918 <lyxtabular version="3" rows="7" columns="3">
10920 <column alignment="center" valignment="top" leftline="true" width="0in">
10921 <column alignment="center" valignment="top" leftline="true" width="0in">
10922 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10923 <row topline="true" bottomline="true">
10924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10949 <row topline="true">
10950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10975 <row topline="true">
10976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11001 <row topline="true">
11002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11027 <row topline="true">
11028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11044 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11053 <row topline="true">
11054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11079 <row topline="true" bottomline="true">
11080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11096 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11114 If the interrupt service routine is defined without
11117 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11122 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11128 a register bank or with register bank 0 (
11132 0), the compiler will save the registers used by itself on the stack upon
11133 entry and restore them at exit, however if such an interrupt service routine
11134 calls another function then the entire register bank will be saved on the
11136 This scheme may be advantageous for small interrupt service routines which
11137 have low register usage.
11140 If the interrupt service routine is defined to be using a specific register
11145 & psw are saved and restored, if such an interrupt service routine calls
11146 another function (using another register bank) then the entire register
11147 bank of the called function will be saved on the stack.
11148 This scheme is recommended for larger interrupt service routines.
11151 HC08 Interrupt Service Routines
11154 Since the number of interrupts available is chip specific and the interrupt
11155 vector table always ends at the last byte of memory, the interrupt numbers
11156 corresponds to the interrupt vectors in reverse order of address.
11157 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11158 2 will use the interrupt vector at 0xfffa, and so on.
11159 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11160 this way; instead see section
11161 \begin_inset LatexCommand \ref{sub:Startup-Code}
11165 for details on customizing startup.
11168 Z80 Interrupt Service Routines
11171 The Z80 uses several different methods for determining the correct interrupt
11172 vector depending on the hardware implementation.
11173 Therefore, SDCC ignores the optional interrupt number and does not attempt
11174 to generate an interrupt vector table.
11177 By default, SDCC generates code for a maskable interrupt, which uses an
11178 RETI instruction to return from the interrupt.
11179 To write an interrupt handler for the non-maskable interrupt, which needs
11180 an RETN instruction instead, add the
11189 void nmi_isr (void) critical interrupt
11203 Enabling and Disabling Interrupts
11206 Critical Functions and Critical Statements
11209 A special keyword may be associated with a block or a function declaring
11215 SDCC will generate code to disable all interrupts
11216 \begin_inset LatexCommand \index{interrupt}
11220 upon entry to a critical function and restore the interrupt enable to the
11221 previous state before returning.
11222 Nesting critical functions will need one additional byte on the stack
11223 \begin_inset LatexCommand \index{stack}
11232 int foo () critical
11233 \begin_inset LatexCommand \index{critical}
11238 \begin_inset LatexCommand \index{\_\_critical}
11263 The critical attribute maybe used with other attributes like
11273 may also be used to disable interrupts more locally:
11281 More than one statement could have been included in the block.
11284 Enabling and Disabling Interrupts directly
11288 \begin_inset LatexCommand \index{interrupt}
11292 can also be disabled and enabled directly (8051):
11297 EA = 0;\SpecialChar ~
11360 EA = 1;\SpecialChar ~
11427 On other architectures which have seperate opcodes for enabling and disabling
11428 interrupts you might want to make use of defines with inline assembly
11429 \begin_inset LatexCommand \index{Assembler routines}
11439 \begin_inset LatexCommand \index{\_asm}
11448 \begin_inset LatexCommand \index{\_endasm}
11457 #define SEI _asm\SpecialChar ~
11469 Note: it is sometimes sufficient to disable only a specific interrupt source
11471 a timer or serial interrupt by manipulating an
11474 \begin_inset LatexCommand \index{interrupt mask}
11484 Usually the time during which interrupts are disabled should be kept as
11486 This minimizes both
11491 \begin_inset LatexCommand \index{interrupt latency}
11495 (the time between the occurrence of the interrupt and the execution of
11496 the first code in the interrupt routine) and
11501 \begin_inset LatexCommand \index{interrupt jitter}
11505 (the difference between the shortest and the longest interrupt latency).
11506 These really are something different, f.e.
11507 a serial interrupt has to be served before its buffer overruns so it cares
11508 for the maximum interrupt latency, whereas it does not care about jitter.
11509 On a loudspeaker driven via a digital to analog converter which is fed
11510 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11511 a much smaller jitter will be very audible.
11514 You can reenable interrupts within an interrupt routine and on some architecture
11515 s you can make use of two (or more) levels of
11517 interrupt priorities
11520 \begin_inset LatexCommand \index{interrupt priority}
11525 On some architectures which don't support interrupt priorities these can
11526 be implemented by manipulating the interrupt mask and reenabling interrupts
11527 within the interrupt routine.
11528 Check there is sufficient space on the stack
11529 \begin_inset LatexCommand \index{stack}
11533 and don't add complexity unless you have to.
11538 \begin_inset LatexCommand \index{semaphore}
11542 locking (mcs51/ds390)
11545 Some architectures (mcs51/ds390) have an atomic
11546 \begin_inset LatexCommand \index{atomic}
11559 These type of instructions are typically used in preemptive multitasking
11560 systems, where a routine f.e.
11561 claims the use of a data structure ('acquires a lock
11562 \begin_inset LatexCommand \index{lock}
11566 on it'), makes some modifications and then releases the lock when the data
11567 structure is consistent again.
11568 The instruction may also be used if interrupt and non-interrupt code have
11569 to compete for a resource.
11570 With the atomic bit test and clear instruction interrupts
11571 \begin_inset LatexCommand \index{interrupt}
11575 don't have to be disabled for the locking operation.
11579 SDCC generates this instruction if the source follows this pattern:
11585 \begin_inset LatexCommand \index{volatile}
11589 bit resource_is_free;
11593 if (resource_is_free)
11603 resource_is_free=0;
11616 resource_is_free=1;
11623 Note, mcs51 and ds390 support only an atomic
11624 \begin_inset LatexCommand \index{atomic}
11632 instruction (as opposed to atomic bit test and
11637 Functions using private register banks
11638 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11645 Some architectures have support for quickly changing register sets.
11646 SDCC supports this feature with the
11649 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11654 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11660 attribute (which tells the compiler to use a register bank
11661 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11665 other than the default bank zero).
11666 It should only be applied to
11669 \begin_inset LatexCommand \index{interrupt}
11675 functions (see footnote below).
11676 This will in most circumstances make the generated ISR code more efficient
11677 since it will not have to save registers on the stack.
11684 attribute will have no effect on the generated code for a
11688 function (but may occasionally be useful anyway
11694 possible exception: if a function is called ONLY from 'interrupt' functions
11695 using a particular bank, it can be declared with the same 'using' attribute
11696 as the calling 'interrupt' functions.
11697 For instance, if you have several ISRs using bank one, and all of them
11698 call memcpy(), it might make sense to create a specialized version of memcpy()
11699 'using 1', since this would prevent the ISR from having to save bank zero
11700 to the stack on entry and switch to bank zero before calling the function
11707 (pending: I don't think this has been done yet)
11714 function using a non-zero bank will assume that it can trash that register
11715 bank, and will not save it.
11716 Since high-priority interrupts
11717 \begin_inset LatexCommand \index{interrupts}
11722 \begin_inset LatexCommand \index{interrupt priority}
11726 can interrupt low-priority ones on the 8051 and friends, this means that
11727 if a high-priority ISR
11731 a particular bank occurs while processing a low-priority ISR
11735 the same bank, terrible and bad things can happen.
11736 To prevent this, no single register bank should be
11740 by both a high priority and a low priority ISR.
11741 This is probably most easily done by having all high priority ISRs use
11742 one bank and all low priority ISRs use another.
11743 If you have an ISR which can change priority at runtime, you're on your
11744 own: I suggest using the default bank zero and taking the small performance
11748 It is most efficient if your ISR calls no other functions.
11749 If your ISR must call other functions, it is most efficient if those functions
11750 use the same bank as the ISR (see note 1 below); the next best is if the
11751 called functions use bank zero.
11752 It is very inefficient to call a function using a different, non-zero bank
11758 \begin_inset LatexCommand \label{sub:Startup-Code}
11763 \begin_inset LatexCommand \index{Startup code}
11770 MCS51/DS390 Startup Code
11773 The compiler inserts a call to the C routine
11775 _sdcc_external_startup()
11776 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11785 at the start of the CODE area.
11786 This routine is in the runtime library
11787 \begin_inset LatexCommand \index{Runtime library}
11792 By default this routine returns 0, if this routine returns a non-zero value,
11793 the static & global variable initialization will be skipped and the function
11794 main will be invoked.
11795 Otherwise static & global variables will be initialized before the function
11799 _sdcc_external_startup()
11801 routine to your program to override the default if you need to setup hardware
11802 or perform some other critical operation prior to static & global variable
11804 \begin_inset LatexCommand \index{Variable initialization}
11809 On some mcs51 variants xdata
11810 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11814 memory has to be explicitly enabled before it can be accessed or if the
11815 watchdog needs to be disabled, this is the place to do it.
11816 The startup code clears all internal data memory, 256 bytes by default,
11817 but from 0 to n-1 if
11830 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11837 (recommended for Chipcon CC1010).
11840 See also the compiler option
11859 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11864 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11869 about MCS51-variants.
11875 The HC08 startup code follows the same scheme as the MCS51 startup code.
11881 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11882 from sdcc/device/lib/z80/crt0.s.
11883 If you need a different startup code you can use the compiler option
11904 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11908 and provide your own crt0.o.
11912 Inline Assembler Code
11913 \begin_inset LatexCommand \index{Assembler routines}
11920 A Step by Step Introduction
11921 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11928 Starting from a small snippet of c-code this example shows for the MCS51
11929 how to use inline assembly, access variables, a function parameter and
11930 an array in xdata memory.
11931 The example uses an MCS51 here but is easily adapted for other architectures.
11932 This is a buffer routine which should be optimized:
11939 \begin_inset LatexCommand \index{far (storage class)}
11944 \begin_inset LatexCommand \index{\_\_far (storage class)}
11949 \begin_inset LatexCommand \index{at}
11954 \begin_inset LatexCommand \index{\_\_at}
11959 \begin_inset LatexCommand \index{Aligned array}
11965 unsigned char head,tail;
11969 void to_buffer( unsigned char c )
11977 if( head != tail-1 )
11987 buf[ head++ ] = c;\SpecialChar ~
11991 /* access to a 256 byte aligned array */
11996 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11997 then a corresponding buffer.asm file is generated.
11998 We define a new function
12002 in file buffer.c in which we cut and paste the generated code, removing
12003 unwanted comments and some ':'.
12005 \begin_inset Quotes sld
12009 \begin_inset Quotes srd
12013 \begin_inset Quotes sld
12017 \begin_inset Quotes srd
12020 to the beginning and the end of the function body:
12026 /* With a cut and paste from the .asm file, we have something to start with.
12031 The function is not yet OK! (registers aren't saved) */
12033 void to_buffer_asm( unsigned char c )
12042 \begin_inset LatexCommand \index{\_asm}
12047 \begin_inset LatexCommand \index{\_\_asm}
12061 ;buffer.c if( head != tail-1 )
12109 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12110 \begin_inset LatexCommand \index{Aligned array}
12175 \begin_inset LatexCommand \index{\_endasm}
12180 \begin_inset LatexCommand \index{\_\_endasm}
12189 The new file buffer.c should compile with only one warning about the unreferenced
12190 function argument 'c'.
12191 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12192 (1) and finally have:
12198 unsigned char far at 0x7f00 buf[0x100];
12200 unsigned char head,tail;
12202 #define USE_ASSEMBLY (1)
12210 void to_buffer( unsigned char c )
12218 if( head != tail-1 )
12238 void to_buffer( unsigned char c )
12246 c; // to avoid warning: unreferenced function argument
12253 \begin_inset LatexCommand \index{\_asm}
12258 \begin_inset LatexCommand \index{\_\_asm}
12272 ; save used registers here.
12283 ; If we were still using r2,r3 we would have to push them here.
12286 ; if( head != tail-1 )
12329 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12353 ; buf[ head++ ] = c;
12364 a,dpl \SpecialChar ~
12371 ; dpl holds lower byte of function argument
12382 dpl,_head \SpecialChar ~
12385 ; buf is 0x100 byte aligned so head can be used directly
12427 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12439 ; restore used registers here
12446 \begin_inset LatexCommand \index{\_endasm}
12451 \begin_inset LatexCommand \index{\_\_endasm}
12462 The inline assembler code can contain any valid code understood by the assembler
12463 , this includes any assembler directives and comment lines
12469 The assembler does not like some characters like ':' or ''' in comments.
12470 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12471 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12476 \begin_inset LatexCommand \index{Assembler documentation}
12484 The compiler does not do any validation of the code within the
12487 \begin_inset LatexCommand \index{\_asm}
12492 \begin_inset LatexCommand \index{\_\_asm}
12500 \begin_inset LatexCommand \index{\_endasm}
12505 \begin_inset LatexCommand \index{\_\_endasm}
12514 Specifically it will not know which registers are used and thus register
12516 \begin_inset LatexCommand \index{push/pop}
12520 has to be done manually.
12524 It is recommended that each assembly instruction (including labels) be placed
12525 in a separate line (as the example shows).
12539 \begin_inset LatexCommand \index{-\/-peep-asm}
12545 command line option is used, the inline assembler code will be passed through
12546 the peephole optimizer
12547 \begin_inset LatexCommand \index{Peephole optimizer}
12552 There are only a few (if any) cases where this option makes sense, it might
12553 cause some unexpected changes in the inline assembler code.
12554 Please go through the peephole optimizer rules defined in file
12558 before using this option.
12562 \begin_inset LatexCommand \label{sub:Naked-Functions}
12567 \begin_inset LatexCommand \index{Naked functions}
12574 A special keyword may be associated with a function declaring it as
12577 \begin_inset LatexCommand \index{\_naked}
12582 \begin_inset LatexCommand \index{\_\_naked}
12593 function modifier attribute prevents the compiler from generating prologue
12594 \begin_inset LatexCommand \index{function prologue}
12599 \begin_inset LatexCommand \index{function epilogue}
12603 code for that function.
12604 This means that the user is entirely responsible for such things as saving
12605 any registers that may need to be preserved, selecting the proper register
12606 bank, generating the
12610 instruction at the end, etc.
12611 Practically, this means that the contents of the function must be written
12612 in inline assembler.
12613 This is particularly useful for interrupt functions, which can have a large
12614 (and often unnecessary) prologue/epilogue.
12615 For example, compare the code generated by these two functions:
12621 \begin_inset LatexCommand \index{volatile}
12625 data unsigned char counter;
12629 void simpleInterrupt(void) interrupt
12630 \begin_inset LatexCommand \index{interrupt}
12635 \begin_inset LatexCommand \index{\_\_interrupt}
12653 void nakedInterrupt(void) interrupt 2 _naked
12662 \begin_inset LatexCommand \index{\_asm}
12667 \begin_inset LatexCommand \index{\_\_asm}
12684 _counter ; does not change flags, no need to save psw
12696 ; MUST explicitly include ret or reti in _naked function.
12703 \begin_inset LatexCommand \index{\_endasm}
12708 \begin_inset LatexCommand \index{\_\_endasm}
12717 For an 8051 target, the generated simpleInterrupt looks like:
12858 whereas nakedInterrupt looks like:
12873 _counter ; does not change flags, no need to save psw
12891 ; MUST explicitly include ret or reti in _naked function
12894 The related directive #pragma exclude
12895 \begin_inset LatexCommand \index{\#pragma exclude}
12899 allows a more fine grained control over pushing & popping
12900 \begin_inset LatexCommand \index{push/pop}
12907 While there is nothing preventing you from writing C code inside a
12911 function, there are many ways to shoot yourself in the foot doing this,
12912 and it is recommended that you stick to inline assembler.
12915 Use of Labels within Inline Assembler
12918 SDCC allows the use of in-line assembler with a few restrictions regarding
12920 In older versions of the compiler all labels defined within inline assembler
12929 where nnnn is a number less than 100 (which implies a limit of utmost 100
12930 inline assembler labels
12944 \begin_inset LatexCommand \index{\_asm}
12949 \begin_inset LatexCommand \index{\_\_asm}
12979 \begin_inset LatexCommand \index{\_endasm}
12984 \begin_inset LatexCommand \index{\_\_endasm}
12991 Inline assembler code cannot reference any C-Labels, however it can reference
12993 \begin_inset LatexCommand \index{Labels}
12997 defined by the inline assembler, e.g.:
13022 ; some assembler code
13042 /* some more c code */
13044 clabel:\SpecialChar ~
13046 /* inline assembler cannot reference this label */
13058 $0003: ;label (can be referenced by inline assembler only)
13065 \begin_inset LatexCommand \index{\_endasm}
13070 \begin_inset LatexCommand \index{\_\_endasm}
13080 /* some more c code */
13085 In other words inline assembly code can access labels defined in inline
13086 assembly within the scope of the function.
13087 The same goes the other way, i.e.
13088 labels defines in inline assembly can not be accessed by C statements.
13091 Interfacing with Assembler Code
13092 \begin_inset LatexCommand \index{Assembler routines}
13099 Global Registers used for Parameter Passing
13100 \begin_inset LatexCommand \index{Parameter passing}
13107 The compiler always uses the global registers
13110 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13115 \begin_inset LatexCommand \index{DPTR}
13120 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13129 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13135 to pass the first parameter to a routine.
13136 The second parameter onwards is either allocated on the stack (for reentrant
13147 -stack-auto is used) or in data / xdata memory (depending on the memory
13152 Assembler Routine (non-reentrant)
13155 In the following example
13156 \begin_inset LatexCommand \index{reentrant}
13161 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13165 the function c_func calls an assembler routine asm_func, which takes two
13167 \begin_inset LatexCommand \index{function parameter}
13176 extern int asm_func(unsigned char, unsigned char);
13180 int c_func (unsigned char i, unsigned char j)
13188 return asm_func(i,j);
13202 return c_func(10,9);
13207 The corresponding assembler function is:
13212 .globl _asm_func_PARM_2
13313 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13330 Note here that the return values
13331 \begin_inset LatexCommand \index{return value}
13335 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13337 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13338 b' & 'acc' for four byte values.
13341 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13342 the parameter number starting from 1, and counting from the left.
13343 The first parameter is passed in
13344 \begin_inset Quotes eld
13348 \begin_inset Quotes erd
13351 for a one byte parameter,
13352 \begin_inset Quotes eld
13356 \begin_inset Quotes erd
13360 \begin_inset Quotes eld
13364 \begin_inset Quotes erd
13367 for three bytes and
13368 \begin_inset Quotes eld
13372 \begin_inset Quotes erd
13375 for a four bytes parameter.
13376 The variable name for the second parameter will be _<function_name>_PARM_2.
13380 Assemble the assembler routine with the following command:
13387 asx8051 -losg asmfunc.asm
13394 Then compile and link the assembler routine to the C source file with the
13402 sdcc cfunc.c asmfunc.rel
13405 Assembler Routine (reentrant)
13409 \begin_inset LatexCommand \index{reentrant}
13414 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13418 the second parameter
13419 \begin_inset LatexCommand \index{function parameter}
13423 onwards will be passed on the stack, the parameters are pushed from right
13425 after the call the leftmost parameter will be on the top of the stack.
13426 Here is an example:
13431 extern int asm_func(unsigned char, unsigned char);
13435 int c_func (unsigned char i, unsigned char j) reentrant
13443 return asm_func(i,j);
13457 return c_func(10,9);
13462 The corresponding assembler routine is:
13562 The compiling and linking procedure remains the same, however note the extra
13563 entry & exit linkage required for the assembler code, _bp is the stack
13564 frame pointer and is used to compute the offset into the stack for parameters
13565 and local variables.
13569 \begin_inset LatexCommand \index{int (16 bit)}
13574 \begin_inset LatexCommand \index{long (32 bit)}
13581 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13582 multiplication and modulus operations are implemented by support routines.
13583 These support routines are all developed in ANSI-C to facilitate porting
13584 to other MCUs, although some model specific assembler optimizations are
13586 The following files contain the described routines, all of them can be
13587 found in <installdir>/share/sdcc/lib.
13593 \begin_inset Tabular
13594 <lyxtabular version="3" rows="11" columns="2">
13596 <column alignment="center" valignment="top" leftline="true" width="0">
13597 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13598 <row topline="true" bottomline="true">
13599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13620 <row topline="true">
13621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13634 16 bit multiplication
13638 <row topline="true">
13639 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13647 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13652 signed 16 bit division (calls _divuint)
13656 <row topline="true">
13657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13670 unsigned 16 bit division
13674 <row topline="true">
13675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13683 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13688 signed 16 bit modulus (calls _moduint)
13692 <row topline="true">
13693 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13706 unsigned 16 bit modulus
13710 <row topline="true">
13711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13724 32 bit multiplication
13728 <row topline="true">
13729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13742 signed 32 division (calls _divulong)
13746 <row topline="true">
13747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13755 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13760 unsigned 32 division
13764 <row topline="true">
13765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13778 signed 32 bit modulus (calls _modulong)
13782 <row topline="true" bottomline="true">
13783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13796 unsigned 32 bit modulus
13809 Since they are compiled as
13814 \begin_inset LatexCommand \index{reentrant}
13819 \begin_inset LatexCommand \index{interrupt}
13823 service routines should not do any of the above operations.
13824 If this is unavoidable then the above routines will need to be compiled
13838 \begin_inset LatexCommand \index{-\/-stack-auto}
13844 option, after which the source program will have to be compiled with
13857 \begin_inset LatexCommand \index{-\/-int-long-reent}
13864 Notice that you don't have to call these routines directly.
13865 The compiler will use them automatically every time an integer operation
13869 Floating Point Support
13870 \begin_inset LatexCommand \index{Floating point support}
13877 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13878 floating point support routines are derived from gcc's floatlib.c and consist
13879 of the following routines:
13887 \begin_inset Tabular
13888 <lyxtabular version="3" rows="17" columns="2">
13890 <column alignment="center" valignment="top" leftline="true" width="0">
13891 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13892 <row topline="true" bottomline="true">
13893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13910 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13919 <row topline="true">
13920 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13937 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13951 add floating point numbers
13955 <row topline="true">
13956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13987 subtract floating point numbers
13991 <row topline="true">
13992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14009 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14023 divide floating point numbers
14027 <row topline="true">
14028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14045 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14059 multiply floating point numbers
14063 <row topline="true">
14064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14081 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14095 convert floating point to unsigned char
14099 <row topline="true">
14100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14117 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14131 convert floating point to signed char
14135 <row topline="true">
14136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14167 convert floating point to unsigned int
14171 <row topline="true">
14172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14203 convert floating point to signed int
14207 <row topline="true">
14208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14248 convert floating point to unsigned long
14252 <row topline="true">
14253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14270 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14284 convert floating point to signed long
14288 <row topline="true">
14289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14306 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14320 convert unsigned char to floating point
14324 <row topline="true">
14325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14356 convert char to floating point number
14360 <row topline="true">
14361 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14392 convert unsigned int to floating point
14396 <row topline="true">
14397 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14414 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14428 convert int to floating point numbers
14432 <row topline="true">
14433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14464 convert unsigned long to floating point number
14468 <row topline="true" bottomline="true">
14469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14486 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14500 convert long to floating point number
14513 These support routines are developed in ANSI-C so there is room for space
14514 and speed improvement
14520 The floating point routines for the mcs51 are implemented in assembler
14524 Note if all these routines are used simultaneously the data space might
14526 For serious floating point usage the large model might be needed.
14527 Also notice that you don't have to call this routines directly.
14528 The compiler will use them automatically every time a floating point operation
14533 \begin_inset LatexCommand \index{Libraries}
14542 <pending: this is messy and incomplete - a little more information is in
14543 sdcc/doc/libdoc.txt
14548 Compiler support routines (_gptrget, _mulint etc.)
14551 Stdclib functions (puts, printf, strcat etc.)
14552 \layout Subsubsection
14558 \begin_inset LatexCommand \index{<stdio.h>}
14562 As usual on embedded systems you have to provide your own
14565 \begin_inset LatexCommand \index{getchar()}
14574 \begin_inset LatexCommand \index{putchar()}
14581 SDCC does not know whether the system connects to a serial line with or
14582 without handshake, LCD, keyboard or other device.
14583 You'll find examples for serial routines f.e.
14584 in sdcc/device/lib.
14590 \begin_inset LatexCommand \index{printf()}
14600 does not support float (except on ds390).
14601 To enable this recompile it with the option
14614 \begin_inset LatexCommand \index{USE\_FLOATS}
14620 on the command line.
14634 \begin_inset LatexCommand \index{-\/-model-large}
14640 for the mcs51 port, since this uses a lot of memory.
14643 If you're short on memory you might want to use
14646 \begin_inset LatexCommand \index{printf\_small()}
14661 For the mcs51 there additionally are assembly versions
14664 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14673 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14682 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14688 which should fit the requirements of many embedded systems (printf_fast()
14689 can be customized by unsetting #defines to
14693 support long variables and field widths).
14696 Math functions (sin, pow, sqrt etc.)
14703 \begin_inset LatexCommand \index{Libraries}
14707 included in SDCC should have a license at least as liberal as the GNU Lesser
14708 General Public License
14709 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14720 license statements for the libraries are missing.
14721 sdcc/device/lib/ser_ir.c
14725 come with a GPL (as opposed to LGPL) License - this will not be liberal
14726 enough for many embedded programmers.
14729 If you have ported some library or want to share experience about some code
14731 falls into any of these categories Busses (I
14732 \begin_inset Formula $^{\textrm{2}}$
14735 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14736 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14737 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14738 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14743 would certainly like to hear about it.
14744 Programmers coding for embedded systems are not especially famous for being
14745 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14746 e these references are very valuable.
14747 Let's help to create a climate where information is shared.
14753 MCS51 Memory Models
14754 \begin_inset LatexCommand \index{Memory model}
14759 \begin_inset LatexCommand \index{MCS51 memory model}
14764 \layout Subsubsection
14766 Small, Medium and Large
14769 SDCC allows three memory models for MCS51 code,
14778 Modules compiled with different memory models should
14782 be combined together or the results would be unpredictable.
14783 The library routines supplied with the compiler are compiled as small,
14785 The compiled library modules are contained in separate directories as small,
14786 medium and large so that you can link to the appropriate set.
14789 When the medium or large model is used all variables declared without a
14790 storage class will be allocated into the external ram, this includes all
14791 parameters and local variables (for non-reentrant
14792 \begin_inset LatexCommand \index{reentrant}
14797 When the small model is used variables without storage class are allocated
14798 in the internal ram.
14801 Judicious usage of the processor specific storage classes
14802 \begin_inset LatexCommand \index{Storage class}
14806 and the 'reentrant' function type will yield much more efficient code,
14807 than using the large model.
14808 Several optimizations are disabled when the program is compiled using the
14809 large model, it is therefore recommended that the small model be used unless
14810 absolutely required.
14811 \layout Subsubsection
14814 \begin_inset LatexCommand \label{sub:External-Stack}
14819 \begin_inset LatexCommand \index{stack}
14824 \begin_inset LatexCommand \index{External stack (mcs51)}
14831 The external stack (-
14842 \begin_inset LatexCommand \index{-\/-xstack}
14846 ) is located in pdata
14847 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14851 memory (usually at the start of the external ram segment) and uses all
14852 unused space in pdata (max.
14864 -xstack option is used to compile the program, the parameters and local
14866 \begin_inset LatexCommand \index{local variables}
14870 of all reentrant functions are allocated in this area.
14871 This option is provided for programs with large stack space requirements.
14872 When used with the -
14883 \begin_inset LatexCommand \index{-\/-stack-auto}
14887 option, all parameters and local variables are allocated on the external
14888 stack (note: support libraries will need to be recompiled with the same
14890 There is a predefined target in the library makefile).
14893 The compiler outputs the higher order address byte of the external ram segment
14895 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14900 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14904 ), therefore when using the External Stack option, this port
14908 be used by the application program.
14912 \begin_inset LatexCommand \index{Memory model}
14917 \begin_inset LatexCommand \index{DS390 memory model}
14924 The only model supported is Flat 24
14925 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14930 This generates code for the 24 bit contiguous addressing mode of the Dallas
14932 In this mode, up to four meg of external RAM or code space can be directly
14934 See the data sheets at www.dalsemi.com for further information on this part.
14938 Note that the compiler does not generate any code to place the processor
14939 into 24 bitmode (although
14943 in the ds390 libraries will do that for you).
14949 \begin_inset LatexCommand \index{Tinibios (DS390)}
14953 , the boot loader or similar code must ensure that the processor is in 24
14954 bit contiguous addressing mode before calling the SDCC startup code.
14972 option, variables will by default be placed into the XDATA segment.
14977 Segments may be placed anywhere in the 4 meg address space using the usual
14989 Note that if any segments are located above 64K, the -r flag must be passed
14990 to the linker to generate the proper segment relocations, and the Intel
14991 HEX output format must be used.
14992 The -r flag can be passed to the linker by using the option
14996 on the SDCC command line.
14997 However, currently the linker can not handle code segments > 64k.
15001 \begin_inset LatexCommand \index{Pragmas}
15008 SDCC supports the following #pragma directives:
15012 \begin_inset LatexCommand \index{\#pragma save}
15016 - this will save all current options to the save/restore stack.
15017 See #pragma\SpecialChar ~
15022 \begin_inset LatexCommand \index{\#pragma restore}
15026 - will restore saved options from the last save.
15027 saves & restores can be nested.
15028 SDCC uses a save/restore stack: save pushes current options to the stack,
15029 restore pulls current options from the stack.
15030 See #pragma\SpecialChar ~
15037 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15042 \begin_inset LatexCommand \index{function prologue}
15046 function1[,function2[,function3...]] - The compiler by default uses a caller
15047 saves convention for register saving across function calls, however this
15048 can cause unnecessary register pushing & popping
15049 \begin_inset LatexCommand \index{push/pop}
15053 when calling small functions from larger functions.
15054 This option can be used to switch off the register saving convention for
15055 the function names specified.
15056 The compiler will not save registers when calling these functions, extra
15057 code need to be manually inserted at the entry & exit for these functions
15058 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15059 reduce code & improve run time performance of the generated code.
15060 In the future the compiler (with inter procedural analysis) may be able
15061 to determine the appropriate scheme to use for each function call.
15072 -callee-saves command line option is used, the function names specified
15073 in #pragma\SpecialChar ~
15075 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15079 is appended to the list of functions specified in the command line.
15083 \begin_inset LatexCommand \index{\#pragma exclude}
15087 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15088 of pairs of push/pop
15089 \begin_inset LatexCommand \index{push/pop}
15098 \begin_inset LatexCommand \index{interrupt}
15111 The directive should be placed immediately before the ISR function definition
15112 and it affects ALL ISR functions following it.
15113 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15114 exclude\SpecialChar ~
15116 \begin_inset LatexCommand \index{\#pragma exclude}
15121 See also the related keyword _naked
15122 \begin_inset LatexCommand \index{\_naked}
15127 \begin_inset LatexCommand \index{\_\_naked}
15135 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15139 - the compiler will not warn you anymore for obvious mistakes, you'r on
15143 disable_warning <nnnn>
15144 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15148 - the compiler will not warn you anymore about warning number <nnnn>.
15152 \begin_inset LatexCommand \index{\#pragma nogcse}
15156 - will stop global common subexpression elimination.
15160 \begin_inset LatexCommand \index{\#pragma noinduction}
15164 - will stop loop induction optimizations.
15168 \begin_inset LatexCommand \index{\#pragma noinvariant}
15172 - will not do loop invariant optimizations.
15173 For more details see Loop Invariants in section
15174 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15182 \begin_inset LatexCommand \index{\#pragma noiv}
15186 - Do not generate interrupt
15187 \begin_inset LatexCommand \index{interrupt}
15191 vector table entries for all ISR functions defined after the pragma.
15192 This is useful in cases where the interrupt vector table must be defined
15193 manually, or when there is a secondary, manually defined interrupt vector
15195 for the autovector feature of the Cypress EZ-USB FX2).
15196 More elegantly this can be achieved by obmitting the optional interrupt
15197 number after the interrupt keyword, see section
15198 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15207 \begin_inset LatexCommand \index{\#pragma nojtbound}
15211 - will not generate code for boundary value checking, when switch statements
15212 are turned into jump-tables (dangerous).
15213 For more details see section
15214 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15222 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15226 - Will not do loop reversal optimization
15230 \begin_inset LatexCommand \index{\#pragma nooverlay}
15234 - the compiler will not overlay the parameters and local variables of a
15239 \begin_inset LatexCommand \index{\#pragma stackauto}
15254 \begin_inset LatexCommand \index{-\/-stack-auto}
15259 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15263 Parameters and Local Variables.
15267 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15271 - The compiler will optimize code generation towards fast code, possibly
15272 at the expense of code size.
15276 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15280 - The compiler will optimize code generation towards compact code, possibly
15281 at the expense of code speed.
15285 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15289 - The compiler will attempt to generate code that is both compact and fast,
15290 as long as meeting one goal is not a detriment to the other (this is the
15296 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15300 - Generally follow the C89 standard, but allow SDCC features that conflict
15301 with the standard (default).
15305 \begin_inset LatexCommand \index{\#pragma std\_c89}
15309 - Follow the C89 standard and disable SDCC features that conflict with the
15314 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15318 - Generally follow the C99 standard, but allow SDCC features that conflict
15319 with the standard (incomplete support).
15323 \begin_inset LatexCommand \index{\#pragma std\_c99}
15327 - Follow the C99 standard and disable SDCC features that conflict with the
15328 standard (incomplete support).
15332 \begin_inset LatexCommand \index{\#pragma codeseg}
15336 - Use this name (max.
15337 8 characters) for the code segment.
15341 \begin_inset LatexCommand \index{\#pragma constseg}
15345 - Use this name (max.
15346 8 characters) for the const segment.
15349 SDCPP supports the following #pragma directives:
15353 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15357 (+ | -) - switch _asm _endasm block preprocessing on / off.
15361 The pragma's are intended to be used to turn-on or off certain optimizations
15362 which might cause the compiler to generate extra stack / data space to
15363 store compiler generated temporary variables.
15364 This usually happens in large functions.
15365 Pragma directives should be used as shown in the following example, they
15366 are used to control options & optimizations for a given function; pragmas
15367 should be placed before and/or after a function, placing pragma's inside
15368 a function body could have unpredictable results.
15374 \begin_inset LatexCommand \index{\#pragma save}
15385 /* save the current settings */
15388 \begin_inset LatexCommand \index{\#pragma nogcse}
15397 /* turnoff global subexpression elimination */
15399 #pragma noinduction
15400 \begin_inset LatexCommand \index{\#pragma noinduction}
15404 /* turn off induction optimizations */
15427 \begin_inset LatexCommand \index{\#pragma restore}
15431 /* turn the optimizations back on */
15434 The compiler will generate a warning message when extra space is allocated.
15435 It is strongly recommended that the save and restore pragma's be used when
15436 changing options for a function.
15439 Defines Created by the Compiler
15442 The compiler creates the following #defines
15443 \begin_inset LatexCommand \index{\#defines}
15448 \begin_inset LatexCommand \index{Defines created by the compiler}
15458 \begin_inset Tabular
15459 <lyxtabular version="3" rows="11" columns="2">
15461 <column alignment="center" valignment="top" leftline="true" width="0">
15462 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15463 <row topline="true" bottomline="true">
15464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15485 <row topline="true">
15486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15492 \begin_inset LatexCommand \index{SDCC}
15499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15504 this Symbol is always defined
15508 <row topline="true">
15509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15515 \begin_inset LatexCommand \index{SDCC\_mcs51}
15520 \begin_inset LatexCommand \index{SDCC\_ds390}
15525 \begin_inset LatexCommand \index{SDCC\_z80}
15532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15537 depending on the model used (e.g.: -mds390
15541 <row topline="true">
15542 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15548 \begin_inset LatexCommand \index{\_\_mcs51}
15553 \begin_inset LatexCommand \index{\_\_ds390}
15558 \begin_inset LatexCommand \index{\_\_hc08}
15563 \begin_inset LatexCommand \index{\_\_z80}
15570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15575 depending on the model used (e.g.
15580 <row topline="true">
15581 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15587 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15594 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15617 <row topline="true">
15618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15624 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15654 <row topline="true">
15655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15661 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15691 <row topline="true">
15692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15698 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15728 <row topline="true">
15729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15735 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15765 <row topline="true">
15766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15772 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15779 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15792 <row topline="true" bottomline="true">
15793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15799 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15826 Notes on supported Processors
15830 \begin_inset LatexCommand \label{sub:MCS51-variants}
15835 \begin_inset LatexCommand \index{MCS51 variants}
15842 MCS51 processors are available from many vendors and come in many different
15844 While they might differ considerably in respect to Special Function Registers
15845 the core MCS51 is usually not modified or is kept compatible.
15849 pdata access by SFR
15852 With the upcome of devices with internal xdata and flash memory devices
15854 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15858 as dedicated I/O port is becoming more popular.
15859 Switching the high byte for pdata
15860 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15864 access which was formerly done by port P2 is then achieved by a Special
15866 \begin_inset LatexCommand \index{sfr}
15871 In well-established MCS51 tradition the address of this
15875 is where the chip designers decided to put it.
15876 Needless to say that they didn't agree on a common name either.
15877 So that the startup code can correctly initialize xdata variables, you
15878 should define an sfr with the name _XPAGE
15881 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15887 at the appropriate location if the default, port P2, is not used for this.
15893 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15898 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15903 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15906 For more exotic implementations further customizations may be needed.
15908 \begin_inset LatexCommand \ref{sub:Startup-Code}
15912 for other possibilities.
15915 Other Features available by SFR
15918 Some MCS51 variants offer features like Double DPTR
15919 \begin_inset LatexCommand \index{DPTR}
15923 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15924 These are currently not used for the MCS51 port.
15925 If you absolutely need them you can fall back to inline assembly or submit
15932 The DS80C400 microcontroller has a rich set of peripherals.
15933 In its built-in ROM library it includes functions to access some of the
15934 features, among them is a TCP stack with IP4 and IP6 support.
15935 Library headers (currently in beta status) and other files are provided
15939 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
15947 The Z80 and gbz80 port
15950 SDCC can target both the Zilog
15951 \begin_inset LatexCommand \index{Z80}
15955 and the Nintendo Gameboy's Z80-like gbz80
15956 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15961 The Z80 port is passed through the same
15964 \begin_inset LatexCommand \index{Regression test}
15970 as the MCS51 and DS390 ports, so floating point support, support for long
15971 variables and bitfield support is fine.
15972 See mailing lists and forums about interrupt routines.
15975 As always, the code is the authoritative reference - see z80/ralloc.c and
15978 \begin_inset LatexCommand \index{stack}
15982 frame is similar to that generated by the IAR Z80 compiler.
15983 IX is used as the base pointer, HL and IY are used as a temporary registers,
15984 and BC and DE are available for holding variables.
15986 \begin_inset LatexCommand \index{return value}
15990 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15992 The gbz80 port use the same set of registers for the return values, but
15993 in a different order of significance: E (one byte), DE (two bytes), or
16000 The port to the Motorola HC08
16001 \begin_inset LatexCommand \index{HC08}
16005 family has been added in October 2003, and is still undergoing some basic
16007 The code generator is complete, but the register allocation is still quite
16009 Some of the SDCC's standard C library functions have embedded non-HC08
16010 inline assembly and so are not yet usable.
16021 \begin_inset LatexCommand \index{PIC14}
16025 port still requires a major effort from the development community.
16026 However it can work for very simple code.
16029 C code and 14bit PIC code page
16030 \begin_inset LatexCommand \index{code page (pic14)}
16035 \begin_inset LatexCommand \index{RAM bank (pic14)}
16042 The linker organizes allocation for the code page and RAM banks.
16043 It does not have intimate knowledge of the code flow.
16044 It will put all the code section of a single asm file into a single code
16046 In order to make use of multiple code pages, separate asm files must be
16048 The compiler treats all functions of a single C file as being in the same
16049 code page unless it is non static.
16050 The compiler treats all local variables of a single C file as being in
16051 the same RAM bank unless it is an extern.
16055 To get the best follow these guide lines:
16058 make local functions static, as non static functions require code page selection
16062 Make local variables static as extern variables require RAM bank selection
16066 For devices that have multiple code pages it is more efficient to use the
16067 same number of files as pages, i.e.
16068 for the 16F877 use 4 separate files and i.e.
16069 for the 16F874 use 2 separate files.
16070 This way the linker can put the code for each file into different code
16071 pages and the compiler can allocate reusable variables more efficiently
16072 and there's less page selection overhead.
16073 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16074 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16077 Creating a device include file
16080 For generating a device include file use the support perl script inc2h.pl
16081 kept in directory support/script.
16087 For the interrupt function, use the keyword 'interrupt'
16088 \begin_inset LatexCommand \index{interrupt}
16092 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16093 there to avoid a syntax error - it ought to be fixed).
16099 void Intr(void) interrupt 0
16105 T0IF = 0; /* Clear timer interrupt */
16110 Linking and assembling
16113 For assembling you can use either GPUTILS'
16114 \begin_inset LatexCommand \index{gputils (pic tools)}
16118 gpasm.exe or MPLAB's mpasmwin.exe.
16119 GPUTILS is available from
16120 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16125 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16126 If you use MPLAB and an interrupt function then the linker script file
16127 vectors section will need to be enlarged to link with mplink.
16150 sdcc -S -V -mpic14 -p16F877 $<
16164 $(PRJ).hex: $(OBJS)
16174 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16196 sdcc -S -V -mpic14 -p16F877 $<
16206 mpasmwin /q /o $*.asm
16210 $(PRJ).hex: $(OBJS)
16220 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16223 Please note that indentations within a
16227 have to be done with a tabulator character.
16230 Command-line options
16233 Besides the switches common to all SDCC backends, the PIC14 port accepts
16234 the following options (for an updated list see sdcc -
16246 \labelwidthstring 00.00.0000
16258 -debug-extra emit debug info in assembly output
16260 \labelwidthstring 00.00.0000
16272 -no-pcode-opt disable (slightly faulty) optimization on pCode
16276 \layout Subsubsection
16278 error: missing definition for symbol
16279 \begin_inset Quotes sld
16283 \begin_inset Quotes srd
16289 The PIC14 port uses library routines to provide more complex operations
16290 like multiplication, division/modulus and (generic) pointer dereferencing.
16291 In order to add these routines to your project, you must link with PIC14's
16297 For single source file projects this is done automatically, more complex
16302 to the linker's arguments.
16303 Make sure you also add an include path for the library (using the -I switch
16305 \layout Subsubsection
16307 Processor mismatch in file
16308 \begin_inset Quotes sld
16312 \begin_inset Quotes srd
16318 This warning can usually be ignored due to the very good compatibility amongst
16319 14 bit PIC devices.
16322 You might also consider recompiling the library for your specific device
16323 by changing the ARCH=p16f877 (default target) entry in
16325 device/lib/pic/Makefile.in
16329 device/lib/pic/Makefile
16331 to reflect your device.
16332 This might even improve performance for smaller devices as unneccesary
16333 BANKSELs migth be removed.
16337 \layout Subsubsection
16342 Currently, data can only be initialized if it resides in the source file
16348 Data in other source files will silently
16356 \begin_inset LatexCommand \index{PIC16}
16364 \begin_inset LatexCommand \index{PIC16}
16368 port is the portion of SDCC that is responsible to produce code for the
16370 \begin_inset LatexCommand \index{Microchip}
16374 (TM) microcontrollers with 16 bit core.
16375 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16376 Currently supported devices are:
16380 \begin_inset Tabular
16381 <lyxtabular version="3" rows="4" columns="6">
16383 <column alignment="center" valignment="top" leftline="true" width="0">
16384 <column alignment="center" valignment="top" leftline="true" width="0">
16385 <column alignment="center" valignment="top" leftline="true" width="0">
16386 <column alignment="center" valignment="top" leftline="true" width="0">
16387 <column alignment="center" valignment="top" leftline="true" width="0">
16388 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16389 <row topline="true">
16390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16398 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16406 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16414 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16422 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16430 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16439 <row topline="true">
16440 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16480 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16489 <row topline="true">
16490 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16514 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16530 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16539 <row topline="true" bottomline="true">
16540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16556 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16571 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16578 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16596 PIC16 port supports the standard command line arguments as supposed, with
16597 the exception of certain cases that will be mentioned in the following
16600 \labelwidthstring 00.00.0000
16612 -callee-saves See -
16624 \labelwidthstring 00.00.0000
16636 -all-callee-saves All function arguments are passed on stack by default.
16639 There is no need to specify this in the command line.
16641 \labelwidthstring 00.00.0000
16653 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16654 no local variables.
16657 Port Specific Options
16658 \begin_inset LatexCommand \index{Options PIC16}
16665 The port specific options appear after the global options in the sdcc --help
16667 \layout Subsubsection
16672 General options enable certain port features and optimizations.
16674 \labelwidthstring 00.00.0000
16686 -stack-model=[model] Used in conjuction with the command above.
16687 Defines the stack model to be used, valid stack models are :
16690 \labelwidthstring 00.00.0000
16696 Selects small stack model.
16697 8 bit stack and frame pointers.
16698 Supports 256 bytes stack size.
16700 \labelwidthstring 00.00.0000
16706 Selects large stack model.
16707 16 bit stack and frame pointers.
16708 Supports 65536 bytes stack size.
16711 \labelwidthstring 00.00.0000
16723 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16724 unitialized data variables with [kword].
16725 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16727 \labelwidthstring 00.00.0000
16739 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16740 Useful for bootloaders.
16742 \labelwidthstring 00.00.0000
16754 -asm= sets the full path and name of an external assembler to call.
16756 \labelwidthstring 00.00.0000
16768 -link= sets the full path and name of an external linker to call.
16770 \labelwidthstring 00.00.0000
16782 -mplab-comp MPLAB compatibility option.
16783 Currently only suppresses special gpasm directives.
16784 \layout Subsubsection
16786 Optimization Options
16788 \labelwidthstring 00.00.0000
16800 -optimize-goto Try to use (conditional) BRA instead of GOTO
16802 \labelwidthstring 00.00.0000
16814 -optimize-cmp Try to optimize some compares.
16816 \labelwidthstring 00.00.0000
16828 -optimize-df Analyze the dataflow of the generated code and improve it.
16830 \labelwidthstring 00.00.0000
16842 -obanksel=nn Set optimization level for inserting BANKSELs.
16847 \labelwidthstring 00.00.0000
16851 \labelwidthstring 00.00.0000
16853 1 checks previous used register and if it is the same then does not emit
16854 BANKSEL, accounts only for labels.
16856 \labelwidthstring 00.00.0000
16858 2 tries to check the location of (even different) symbols and removes BANKSELs
16859 if they are in the same bank.
16864 Important: There might be problems if the linker script has data sections
16865 across bank borders!
16867 \layout Subsubsection
16871 \labelwidthstring 00.00.0000
16883 -nodefaultlibs do not link default libraries when linking
16885 \labelwidthstring 00.00.0000
16897 -no-crt Don't link the default run-time modules
16899 \labelwidthstring 00.00.0000
16911 -use-crt= Use a custom run-time module instead of the defaults.
16912 \layout Subsubsection
16917 Debugging options enable extra debugging information in the output files.
16919 \labelwidthstring 00.00.0000
16931 -debug-xtra Similar to -
16942 \begin_inset LatexCommand \index{-\/-debug}
16946 , but dumps more information.
16948 \labelwidthstring 00.00.0000
16960 -debug-ralloc Force register allocator to dump <source>.d file with debugging
16962 <source> is the name of the file compiled.
16964 \labelwidthstring 00.00.0000
16976 -pcode-verbose Enable pcode debugging information in translation.
16978 \labelwidthstring 00.00.0000
16990 -denable-peeps Force the usage of peepholes.
16993 \labelwidthstring 00.00.0000
17005 -gstack Trace push/pops for stack pointer overflow
17007 \labelwidthstring 00.00.0000
17019 -call-tree dump call tree in .calltree file
17022 Enviromental Variables
17025 There is a number of enviromental variables that can be used when running
17026 SDCC to enable certain optimizations or force a specific program behaviour.
17027 these variables are primarily for debugging purposes so they can be enabled/dis
17031 Currently there is only two such variables available:
17033 \labelwidthstring 00.00.0000
17035 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17036 bitfields is optimized by directly loading FSR0 with the address of the
17037 bitfield structure.
17038 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17040 This step saves data ram and code space for functions that perform heavy
17043 80 bytes of code space are saved when compiling malloc.c with this option).
17046 \labelwidthstring 00.00.0000
17048 NO_REG_OPT do not perform pCode registers optimization.
17049 This should be used for debugging purposes.
17050 In some where bugs in the pcode optimizer are found, users can benefit
17051 from temporarily disabling the optimizer until the bug is fixed.
17054 Preprocessor Macros
17057 PIC16 port defines the following preprocessor macros while translating a
17062 \begin_inset Tabular
17063 <lyxtabular version="3" rows="6" columns="2">
17065 <column alignment="center" valignment="top" leftline="true" width="0">
17066 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17067 <row topline="true" bottomline="true">
17068 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17085 <row topline="true">
17086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17099 Port identification
17103 <row topline="true">
17104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17127 Port identification (same as above)
17131 <row topline="true">
17132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17145 MCU Identification.
17150 is the microcontrol identification number, i.e.
17155 <row topline="true">
17156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17179 MCU Identification (same as above)
17183 <row topline="true" bottomline="true">
17184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17197 nnn = SMALL or LARGE respectively according to the stack model used
17208 In addition the following macros are defined when calling assembler:
17212 \begin_inset Tabular
17213 <lyxtabular version="3" rows="4" columns="2">
17215 <column alignment="center" valignment="top" leftline="true" width="0">
17216 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17217 <row topline="true" bottomline="true">
17218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17226 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17235 <row topline="true">
17236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17249 MCU Identification.
17254 is the microcontrol identification number, i.e.
17259 <row topline="true">
17260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17273 nnn = SMALL or LARGE respectively according to the memory model used for
17278 <row topline="true" bottomline="true">
17279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17287 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17292 nnn = SMALL or LARGE respectively according to the stack model used
17307 \begin_inset LatexCommand \index{PIC16}
17311 port uses the following directories for searching header files and libraries.
17315 \begin_inset Tabular
17316 <lyxtabular version="3" rows="3" columns="4">
17318 <column alignment="center" valignment="top" leftline="true" width="0">
17319 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17320 <column alignment="center" valignment="top" width="0">
17321 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17322 <row topline="true" bottomline="true">
17323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17331 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17339 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17356 <row topline="true">
17357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17362 PREFIX/sdcc/include/pic16
17365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17370 PIC16 specific headers
17373 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17390 <row topline="true" bottomline="true">
17391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17396 PREFIX/sdcc/lib/pic16
17399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17404 PIC16 specific libraries
17407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17432 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17439 PIC16 port currently supports the following pragmas:
17441 \labelwidthstring 00.00.0000
17443 stack pragma stack forces the code generator to initialize the stack & frame
17444 pointers at a specific address.
17445 This is an adhoc solution for cases where no STACK directive is available
17446 in the linker script or gplink is not instructed to create a stack section.
17448 The stack pragma should be used only once in a project.
17449 Multiple pragmas may result in indeterminate behaviour of the program.
17455 The old format (ie.
17456 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17457 cross page boundaries (or even exceed the available data RAM) and crash
17459 Make sure that stack does not cross page boundaries when using the SMALL
17465 The format is as follows:
17468 #pragma stack bottom_address [stack_size]
17475 is the lower bound of the stack section.
17476 The stack pointer initially will point at address (bottom_address+stack_size-1).
17484 /* initializes stack of 100 bytes at RAM address 0x200 */
17487 #pragma stack 0x200 100
17490 If the stack_size field is omitted then a stack is created with the default
17492 This size might be enough for most programs, but its not enough for operations
17493 with deep function nesting or excessive stack usage.
17495 \labelwidthstring 00.00.0000
17499 This pragma is deprecated.
17500 Its use will cause a warning message to be issued.
17506 \labelwidthstring 00.00.0000
17508 code place a function symbol at static FLASH address
17516 /* place function test_func at 0x4000 */
17519 #pragma code test_func 0x4000
17523 \labelwidthstring 00.00.0000
17525 library instructs the linker to use a library module.
17530 #pragma library module_name
17537 can be any library or object file (including its path).
17538 Note that there are four reserved keywords which have special meaning.
17543 \begin_inset Tabular
17544 <lyxtabular version="3" rows="6" columns="3">
17546 <column alignment="center" valignment="top" leftline="true" width="0">
17547 <column alignment="block" valignment="top" leftline="true" width="20page%">
17548 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17549 <row topline="true" bottomline="true">
17550 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17575 <row topline="true">
17576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17591 ignore all library pragmas
17594 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17605 <row topline="true">
17606 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17616 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17637 <row topline="true">
17638 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17653 link the Math libarary
17656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17669 <row topline="true">
17670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17680 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17685 link the I/O library
17688 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17701 <row topline="true" bottomline="true">
17702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17717 link the debug library
17720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17739 * is the device number, i.e.
17740 452 for PIC18F452 MCU.
17743 This feature allows for linking with specific libraries withoug having to
17744 explicit name them in the command line.
17749 keyword will reject all modules specified by the library pragma.
17751 \labelwidthstring 00.00.0000
17753 udata pragma udata instructs the compiler to emit code so that linker will
17754 place a variable at a specific memory bank
17762 /* places variable foo at bank2 */
17765 #pragma udata bank2 foo
17771 In order for this pragma to work extra SECTION directives should be added
17772 in the .lkr script.
17773 In the following example a sample .lkr file is shown:
17778 // Sample linker script for the PIC18F452 processor
17784 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17787 CODEPAGE NAME=page START=0x2A END=0x7FFF
17790 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17793 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17796 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17799 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17802 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17807 DATABANK NAME=gpr0 START=0x80 END=0xFF
17810 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17813 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17816 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17819 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17822 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17825 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17830 SECTION NAME=CONFIG ROM=config
17835 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17838 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17841 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17844 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17847 SECTION NAME=bank4 RAM=gpr4
17850 SECTION NAME=bank5 RAM=gpr5
17853 The linker will recognise the section name set in the pragma statement and
17854 will position the variable at the memory bank set with the RAM field at
17855 the SECTION line in the linker script file.
17859 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17866 There is one main header file that can be included to the source files using
17873 This header file contains the definitions for the processor special registers,
17874 so it is necessary if the source accesses them.
17875 It can be included by adding the following line in the beginning of the
17879 #include <pic18fregs.h>
17882 The specific microcontroller is selected within the pic18fregs.h automatically,
17883 so the same source can be used with a variety of devices.
17889 The libraries that PIC16
17890 \begin_inset LatexCommand \index{PIC16}
17894 port depends on are the microcontroller device libraries which contain
17895 the symbol definitions for the microcontroller special function registers.
17896 These libraries have the format pic18fxxxx.lib, where
17900 is the microcontroller identification number.
17901 The specific library is selected automatically by the compiler at link
17902 stage according to the selected device.
17905 Libraries are created with gplib which is part of the gputils package
17906 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17911 \layout Subsubsection*
17913 Building the libraries
17916 Before using SDCC/pic16 there are some libraries that need to be compiled.
17917 This process is not done automatically by SDCC since not all users use
17918 SDCC for pic16 projects.
17919 So each user should compile the libraries separately.
17922 The steps to compile the pic16 libraries under Linux are:
17925 cd device/lib/pic16
17940 su -c 'make install' # install the libraries, you need the root password
17943 If you need to install the headers too, do:
17949 su -c 'make install' # install the headers, you need the root password
17952 There exist a special target to build the I/O libraries.
17953 This target is not automatically build because it will build the I/O library
17959 This way building will take quite a lot of time.
17960 Users are advised to edit the
17962 device/lib/pic16/pics.build
17964 file and then execute:
17973 The following memory models are supported by the PIC16 port:
17982 Memory model affects the default size of pointers within the source.
17983 The sizes are shown in the next table:
17987 \begin_inset Tabular
17988 <lyxtabular version="3" rows="3" columns="3">
17990 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17991 <column alignment="center" valignment="top" leftline="true" width="0">
17992 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17993 <row topline="true" bottomline="true">
17994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17999 Pointer sizes according to memory model
18002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18019 <row topline="true" bottomline="true">
18020 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18028 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18045 <row topline="true" bottomline="true">
18046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18054 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18078 It is advisable that all sources within a project are compiled with the
18080 If one wants to override the default memory model, this can be done by
18081 declaring a pointer as
18090 Far selects large memory model's pointers, while near selects small memory
18094 The standard device libraries (see
18095 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18099 ) contain no reference to pointers, so they can be used with both memory
18106 The stack implementation for the PIC16 port uses two indirect registers,
18109 \labelwidthstring 00.00.0000
18111 FSR1 is assigned as stack pointer
18113 \labelwidthstring 00.00.0000
18115 FSR2 is assigned as frame pointer
18118 The following stack models are supported by the PIC16 port
18139 model means that only the FSRxL byte is used to access stack and frame,
18146 uses both FSRxL and FSRxH registers.
18147 The following table shows the stack/frame pointers sizes according to stack
18148 model and the maximum space they can address:
18152 \begin_inset Tabular
18153 <lyxtabular version="3" rows="3" columns="3">
18155 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18156 <column alignment="center" valignment="top" leftline="true" width="0">
18157 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18158 <row topline="true" bottomline="true">
18159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18164 Stack & Frame pointer sizes according to stack model
18167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18184 <row topline="true">
18185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18210 <row topline="true" bottomline="true">
18211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18247 stack model is currently not working properly throughout the code generator.
18248 So its use is not advised.
18249 Also there are some other points that need special care:
18254 Do not create stack sections with size more than one physical bank (that
18258 Stack sections should no cross physical bank limits (i.e.
18259 #pragma stack 0x50 0x100)
18262 These limitations are caused by the fact that only FSRxL is modified when
18263 using SMALL stack model, so no more than 256 bytes of stack can be used.
18264 This problem will disappear after LARGE model is fully implemented.
18270 In addition to the standard SDCC function keywords, PIC16 port makes available
18273 \labelwidthstring 00.00.0000
18275 wparam Use the WREG to pass one byte of the first function argument.
18276 This improves speed but you may not use this for functions with arguments
18277 that are called via function pointers, otherwise the first byte of the
18278 first parameter will get lost.
18282 void func_wparam(int a) wparam
18288 /* WREG hold the lower part of a */
18291 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18301 This keyword replaces the deprecated wparam pragma.
18303 \labelwidthstring 00.00.0000
18305 shadowregs When entering/exiting an ISR, it is possible to take advantage
18306 of the PIC18F hardware shadow registers which hold the values of WREG,
18307 STATUS and BSR registers.
18308 This can be done by adding the keyword
18316 keyword in the function's header.
18319 void isr_shadow(void) shadowregs interrupt 1
18335 instructs the code generator not to store/restore WREG, STATUS, BSR when
18336 entering/exiting the ISR.
18339 Function return values
18342 Return values from functions are placed to the appropriate registers following
18343 a modified Microchip policy optimized for SDCC.
18344 The following table shows these registers:
18348 \begin_inset Tabular
18349 <lyxtabular version="3" rows="6" columns="2">
18351 <column alignment="center" valignment="top" leftline="true" width="0">
18352 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18353 <row topline="true" bottomline="true">
18354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18367 destination register
18371 <row topline="true">
18372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18389 <row topline="true">
18390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18407 <row topline="true">
18408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18425 <row topline="true">
18426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18439 FSR0L:PRODH:PRODL:WREG
18443 <row topline="true" bottomline="true">
18444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18457 on stack, FSR0 points to the beginning
18471 An interrupt servive routine (ISR) is declared using the
18478 void isr(void) interrupt
18496 is the interrupt number, which for PIC18F devices can be:
18500 \begin_inset Tabular
18501 <lyxtabular version="3" rows="4" columns="3">
18503 <column alignment="center" valignment="top" leftline="true" width="0">
18504 <column alignment="center" valignment="top" leftline="true" width="0">
18505 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18506 <row topline="true" bottomline="true">
18507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18525 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18530 Interrupt Vector Address
18534 <row topline="true">
18535 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18551 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18560 <row topline="true">
18561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18578 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18592 HIGH priority interrupts
18595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18604 <row topline="true" bottomline="true">
18605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18618 LOW priority interrupts
18621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18637 When generating assembly code for ISR the code generator places a
18643 Interrupt Vector Address
18645 which points at the genetated ISR.
18646 This single GOTO instruction is part of an automatically generated
18648 interrupt entry point
18651 The actuall ISR code is placed as normally would in the code space.
18652 Upon interrupt request, the GOTO instruction is executed which jumps to
18654 When declaring interrupt functions as _naked this GOTO instruction is
18659 The whole interrupt functions is therefore placed at the Interrupt Vector
18660 Address of the specific interrupt.
18661 This is not a problem for the LOW priority interrupts, but it is a problem
18662 for the RESET and the HIGH priority interrupts because code may be written
18663 at the next interrupt´s vector address and cause undeterminate program
18664 behaviour if that interrupt is raised.
18670 This is not a problem when
18673 this is a HIGH interrupt ISR and LOW interrupts are
18680 when the ISR is small enough not to reach the next interrupt´s vector address.
18690 is possible to be omitted.
18691 This way a function is generated similar to an ISR, but it is not assigned
18695 When entering an interrupt, currently the PIC16
18696 \begin_inset LatexCommand \index{PIC16}
18700 port automatically saves the following registers:
18712 PROD (PRODL and PRODH)
18715 FSR0 (FSR0L and FSR0H)
18718 These registers are restored upon return from the interrupt routine.
18724 NOTE that when the _naked attribute is specified for an interrupt routine,
18725 then NO registers are stored or restored.
18734 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18735 There are 3 types of generic pointers currently implemented data, code
18736 and eeprom pointers.
18737 They are differentiated by the value of the 7th and 6th bits of the upper
18742 \begin_inset Tabular
18743 <lyxtabular version="3" rows="5" columns="5">
18745 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18746 <column alignment="center" valignment="top" width="0">
18747 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18748 <column alignment="center" valignment="top" width="0">
18749 <column alignment="left" valignment="top" rightline="true" width="0">
18750 <row topline="true" bottomline="true">
18751 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18759 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18780 rest of the pointer
18783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18792 <row topline="true" bottomline="true">
18793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18817 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18826 uuuuuu uuuuxxxx xxxxxxxx
18829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18834 a 12-bit data pointer in data RAM memory
18838 <row bottomline="true">
18839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18847 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18855 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18863 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18872 uxxxxx xxxxxxxx xxxxxxxx
18875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18880 a 21-bit code pointer in FLASH memory
18884 <row bottomline="true">
18885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18901 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18909 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18918 uuuuuu uuuuuuxx xxxxxxxx
18921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18926 a 10-bit eeprom pointer in EEPROM memory
18930 <row bottomline="true">
18931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18939 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18947 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18955 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18964 xxxxxx xxxxxxxx xxxxxxxx
18967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18972 unimplemented pointer type
18983 Generic pointer are read and written with a set of library functions which
18984 read/write 1, 2, 3, 4 bytes.
18988 \layout Subsubsection
18990 Standard I/O Streams
18997 the type FILE is defined as:
19000 typedef char * FILE;
19003 This type is the stream type implemented I/O in the PIC18F devices.
19004 Also the standard input and output streams are declared in stdio.h:
19007 extern FILE * stdin;
19010 extern FILE * stdout;
19013 The FILE type is actually a generic pointer which defines one more type
19014 of generic pointers, the
19019 This new type has the format:
19023 \begin_inset Tabular
19024 <lyxtabular version="3" rows="2" columns="7">
19026 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19027 <column alignment="center" valignment="top" width="0">
19028 <column alignment="center" valignment="top" leftline="true" width="0">
19029 <column alignment="center" valignment="top" leftline="true" width="0">
19030 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19031 <column alignment="center" valignment="top" width="0">
19032 <column alignment="left" valignment="top" rightline="true" width="0">
19033 <row topline="true" bottomline="true">
19034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19050 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19058 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19079 rest of the pointer
19082 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19091 <row topline="true" bottomline="true">
19092 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19108 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19124 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19149 upper byte high nubble is 0x2n, the rest are zeroes
19160 Currently implemented there are 3 types of streams defined:
19164 \begin_inset Tabular
19165 <lyxtabular version="3" rows="4" columns="4">
19167 <column alignment="center" valignment="top" leftline="true" width="0">
19168 <column alignment="center" valignment="top" leftline="true" width="0">
19169 <column alignment="center" valignment="top" leftline="true" width="0">
19170 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19171 <row topline="true" bottomline="true">
19172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19205 <row topline="true">
19206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19237 Writes/Reads characters via the USART peripheral
19241 <row topline="true">
19242 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19273 Writes/Reads characters via the MSSP peripheral
19277 <row topline="true" bottomline="true">
19278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19286 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19304 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19309 Writes/Reads characters via used defined functions
19320 The stream identifiers are declared as macros in the stdio.h header.
19323 In the libc library there exist the functions that are used to write to
19324 each of the above streams.
19327 \labelwidthstring 00.00.0000
19339 _stream_usart_putchar writes a character at the USART stream
19341 \labelwidthstring 00.00.0000
19353 _stream_mssp_putchar writes a character at the MSSP stream
19355 \labelwidthstring 00.00.0000
19357 putchar dummy function.
19358 This writes a character to a user specified manner.
19361 In order to increase performance
19365 is declared in stdio.h as having its parameter in WREG (it has the wparam
19367 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19368 in a user-friendly way.
19373 is the name of the variable that holds the character to print.
19374 An example follows:
19377 #include <pic18fregs.h>
19389 PORTA = c; /* dump character c to PORTA */
19402 stdout = STREAM_USER; /* this is not necessery, since stdout points
19405 * by default to STREAM_USER */
19408 printf (¨This is a printf test
19416 \layout Subsubsection
19421 PIC16 contains an implementation of the printf-family of functions.
19422 There exist the following functions:
19425 extern unsigned int sprintf(char *buf, char *fmt, ...);
19428 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19433 extern unsigned int printf(char *fmt, ...);
19436 extern unsigned int vprintf(char *fmt, va_lista ap);
19441 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19444 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19447 For sprintf and vsprintf
19451 should normally be a data pointer where the resulting string will be placed.
19452 No range checking is done so the user should allocate the necessery buffer.
19453 For fprintf and vfprintf
19457 should be a stream pointer (i.e.
19458 stdout, STREAM_MSSP, etc...).
19459 \layout Subsubsection
19464 The PIC18F family of microcontrollers supports a number of interrupt sources.
19465 A list of these interrupts is shown in the following table:
19469 \begin_inset Tabular
19470 <lyxtabular version="3" rows="11" columns="4">
19472 <column alignment="left" valignment="top" leftline="true" width="0">
19473 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19474 <column alignment="left" valignment="top" leftline="true" width="0">
19475 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19476 <row topline="true" bottomline="true">
19477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19485 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19501 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19510 <row topline="true">
19511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19519 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19524 PORTB change interrupt
19527 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19540 EEPROM/FLASH write complete interrupt
19544 <row topline="true">
19545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19558 INT0 external interrupt
19561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19574 Bus collision interrupt
19578 <row topline="true">
19579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19592 INT1 external interrupt
19595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19608 Low voltage detect interrupt
19612 <row topline="true">
19613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19626 INT2 external interrupt
19629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19642 Parallel slave port interrupt
19646 <row topline="true">
19647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19660 CCP1 module interrupt
19663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19676 AD convertion complete interrupt
19680 <row topline="true">
19681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19689 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19694 CCP2 module interrupt
19697 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19710 USART receive interrupt
19714 <row topline="true">
19715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19723 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19728 TMR0 overflow interrupt
19731 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19744 USART transmit interrupt
19748 <row topline="true">
19749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19762 TMR1 overflow interrupt
19765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19778 SSP receive/transmit interrupt
19782 <row topline="true">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19796 TMR2 matches PR2 interrupt
19799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19814 <row topline="true" bottomline="true">
19815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19823 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19828 TMR3 overflow interrupt
19831 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19853 The prototypes for these names are defined in the header file
19860 In order to simplify signal handling, a number of macros is provided:
19862 \labelwidthstring 00.00.0000
19864 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19865 high priority interrupts.
19870 is the function name to use.
19872 \labelwidthstring 00.00.0000
19874 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19875 low priority interrupt.
19880 is the function name to use.
19882 \labelwidthstring 00.00.0000
19884 DEF_HANDLER(sig,handler) define a handler for signal
19888 \labelwidthstring 00.00.0000
19890 END_DEF end the declaration of the dispatch table.
19893 Additionally there are two more macros to simplify the declaration of the
19896 \labelwidthstring 00.00.0000
19900 SIGHANDLER(handler)
19902 this declares the function prototype for the
19908 \labelwidthstring 00.00.0000
19910 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19913 An example of using the macros above is shown below:
19916 #include <pic18fregs.h>
19919 #include <signal.h>
19923 DEF_INTHIGH(high_int)
19926 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19929 DEF_HANDLER(SIG_BCOL, _bcol_handler)
19936 SIGHANDLER(_tmr0_handler)
19942 /* action to be taken when timer 0 overflows */
19949 SIGHANDLERNAKED(_bcol_handler)
19958 /* action to be taken when bus collision occurs */
19974 Special care should be taken when using the above scheme:
19977 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
19980 when declaring SIGHANDLERNAKED handler never forget to use
19984 for proper returning.
19990 Here you can find some general tips for compiling programs with SDCC/pic16.
19991 \layout Subsubsection
19996 The default stack size (that is 64 bytes) probably is enough for many programs.
19997 One must take care that when there are many levels of function nesting,
19998 or there is excessive usage of stack, its size should be extended.
19999 An example of such a case is the printf/sprintf family of functions.
20000 If you encounter problems like not being able to print integers, then you
20001 need to set the stack size around the maximum (256 for small stack model).
20002 The following diagram shows what happens when calling printf to print an
20006 printf () --> ltoa () --> ultoa () --> divschar ()
20009 It is should be understood that stack is easily consumed when calling complicate
20011 Using command line arguments like -
20021 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20023 Other ways to reduce stack usage may exist.
20026 Debugging with SDCDB
20027 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20032 \begin_inset LatexCommand \index{sdcdb (debugger)}
20039 SDCC is distributed with a source level debugger
20040 \begin_inset LatexCommand \index{Debugger}
20045 The debugger uses a command line interface, the command repertoire of the
20046 debugger has been kept as close to gdb
20047 \begin_inset LatexCommand \index{gdb}
20051 (the GNU debugger) as possible.
20052 The configuration and build process is part of the standard compiler installati
20053 on, which also builds and installs the debugger in the target directory
20054 specified during configuration.
20055 The debugger allows you debug BOTH at the C source and at the ASM source
20057 Sdcdb is available on Unix platforms only.
20060 Compiling for Debugging
20074 \begin_inset LatexCommand \index{-\/-debug}
20078 option must be specified for all files for which debug information is to
20080 The complier generates a .adb file for each of these files.
20081 The linker creates the .cdb
20082 \begin_inset LatexCommand \index{<file>.cdb}
20087 \begin_inset LatexCommand \index{<file>.adb}
20091 files and the address information.
20092 This .cdb is used by the debugger.
20095 How the Debugger Works
20108 -debug option is specified the compiler generates extra symbol information
20109 some of which are put into the assembler source and some are put into the
20111 Then the linker creates the .cdb file from the individual .adb files with
20112 the address information for the symbols.
20113 The debugger reads the symbolic information generated by the compiler &
20114 the address information generated by the linker.
20115 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20116 execution is controlled by the debugger.
20117 When a command is issued for the debugger, it translates it into appropriate
20118 commands for the simulator.
20121 Starting the Debugger
20124 The debugger can be started using the following command line.
20125 (Assume the file you are debugging has the file name foo).
20139 The debugger will look for the following files.
20142 foo.c - the source file.
20145 foo.cdb - the debugger symbol information file.
20148 foo.ihx - the Intel hex format
20149 \begin_inset LatexCommand \index{Intel hex format}
20156 Command Line Options
20169 -directory=<source file directory> this option can used to specify the directory
20171 The debugger will look into the directory list specified for source, cdb
20173 The items in the directory list must be separated by ':', e.g.
20174 if the source files can be in the directories /home/src1 and /home/src2,
20185 -directory option should be -
20195 -directory=/home/src1:/home/src2.
20196 Note there can be no spaces in the option.
20200 -cd <directory> - change to the <directory>.
20203 -fullname - used by GUI front ends.
20206 -cpu <cpu-type> - this argument is passed to the simulator please see the
20207 simulator docs for details.
20210 -X <Clock frequency > this options is passed to the simulator please see
20211 the simulator docs for details.
20214 -s <serial port file> passed to simulator see the simulator docs for details.
20217 -S <serial in,out> passed to simulator see the simulator docs for details.
20220 -k <port number> passed to simulator see the simulator docs for details.
20226 As mentioned earlier the command interface for the debugger has been deliberatel
20227 y kept as close the GNU debugger gdb, as possible.
20228 This will help the integration with existing graphical user interfaces
20229 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20230 If you use a graphical user interface for the debugger you can skip this
20232 \layout Subsubsection*
20234 break [line | file:line | function | file:function]
20237 Set breakpoint at specified line or function:
20246 sdcdb>break foo.c:100
20248 sdcdb>break funcfoo
20250 sdcdb>break foo.c:funcfoo
20251 \layout Subsubsection*
20253 clear [line | file:line | function | file:function ]
20256 Clear breakpoint at specified line or function:
20265 sdcdb>clear foo.c:100
20267 sdcdb>clear funcfoo
20269 sdcdb>clear foo.c:funcfoo
20270 \layout Subsubsection*
20275 Continue program being debugged, after breakpoint.
20276 \layout Subsubsection*
20281 Execute till the end of the current function.
20282 \layout Subsubsection*
20287 Delete breakpoint number 'n'.
20288 If used without any option clear ALL user defined break points.
20289 \layout Subsubsection*
20291 info [break | stack | frame | registers ]
20294 info break - list all breakpoints
20297 info stack - show the function call stack.
20300 info frame - show information about the current execution frame.
20303 info registers - show content of all registers.
20304 \layout Subsubsection*
20309 Step program until it reaches a different source line.
20310 Note: pressing <return> repeats the last command.
20311 \layout Subsubsection*
20316 Step program, proceeding through subroutine calls.
20317 \layout Subsubsection*
20322 Start debugged program.
20323 \layout Subsubsection*
20328 Print type information of the variable.
20329 \layout Subsubsection*
20334 print value of variable.
20335 \layout Subsubsection*
20340 load the given file name.
20341 Note this is an alternate method of loading file for debugging.
20342 \layout Subsubsection*
20347 print information about current frame.
20348 \layout Subsubsection*
20353 Toggle between C source & assembly source.
20354 \layout Subsubsection*
20356 ! simulator command
20359 Send the string following '!' to the simulator, the simulator response is
20361 Note the debugger does not interpret the command being sent to the simulator,
20362 so if a command like 'go' is sent the debugger can loose its execution
20363 context and may display incorrect values.
20364 \layout Subsubsection*
20371 My name is Bobby Brown"
20374 Interfacing with DDD
20377 The screenshot was converted from png to eps with:
20378 \begin_inset Quotes sld
20381 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20382 \begin_inset Quotes srd
20385 which produces a pretty compact eps file which is free from compression
20389 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20390 as this broke the build system on Sourceforge (pdf-file was broken).
20396 \begin_inset LatexCommand \url{http://cvs.sourceforge.net/viewcvs.py/*checkout*/sdcc/sdcc/doc/figures/ddd_example.eps}
20402 shows a screenshot of a debugging session with DDD
20403 \begin_inset LatexCommand \index{DDD (debugger)}
20407 (Unix only) on a simulated 8032.
20408 The debugging session might not run as smoothly as the screenshot suggests.
20409 The debugger allows setting of breakpoints, displaying and changing variables,
20410 single stepping through C and assembler code.
20413 The source was compiled with
20436 -debug ddd_example.c
20449 and DDD was invoked with
20456 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20459 Interfacing with XEmacs
20460 \begin_inset LatexCommand \index{XEmacs}
20465 \begin_inset LatexCommand \index{Emacs}
20472 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20473 sdcdb.el and sdcdbsrc.el.
20474 These two files can be found in the $(prefix)/bin directory after the installat
20476 These files need to be loaded into XEmacs for the interface to work.
20477 This can be done at XEmacs startup time by inserting the following into
20478 your '.xemacs' file (which can be found in your HOME directory):
20484 (load-file sdcdbsrc.el)
20490 .xemacs is a lisp file so the () around the command is REQUIRED.
20491 The files can also be loaded dynamically while XEmacs is running, set the
20492 environment variable 'EMACSLOADPATH' to the installation bin directory
20493 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20494 To start the interface enter the following command:
20508 You will prompted to enter the file name to be debugged.
20513 The command line options that are passed to the simulator directly are bound
20514 to default values in the file sdcdbsrc.el.
20515 The variables are listed below, these values maybe changed as required.
20518 sdcdbsrc-cpu-type '51
20521 sdcdbsrc-frequency '11059200
20524 sdcdbsrc-serial nil
20527 The following is a list of key mapping for the debugger interface.
20538 ;;key\SpecialChar ~
20552 binding\SpecialChar ~
20576 ;;---\SpecialChar ~
20590 -------\SpecialChar ~
20632 sdcdb-next-from-src\SpecialChar ~
20660 sdcdb-back-from-src\SpecialChar ~
20688 sdcdb-cont-from-src\SpecialChar ~
20698 SDCDB continue command
20716 sdcdb-step-from-src\SpecialChar ~
20744 sdcdb-whatis-c-sexp\SpecialChar ~
20754 SDCDB ptypecommand for data at
20821 sdcdbsrc-delete\SpecialChar ~
20835 SDCDB Delete all breakpoints if no arg
20884 given or delete arg (C-u arg x)
20902 sdcdbsrc-frame\SpecialChar ~
20917 SDCDB Display current frame if no arg,
20966 given or display frame arg
21033 sdcdbsrc-goto-sdcdb\SpecialChar ~
21043 Goto the SDCDB output buffer
21061 sdcdb-print-c-sexp\SpecialChar ~
21072 SDCDB print command for data at
21139 sdcdbsrc-goto-sdcdb\SpecialChar ~
21149 Goto the SDCDB output buffer
21167 sdcdbsrc-mode\SpecialChar ~
21183 Toggles Sdcdbsrc mode (turns it off)
21198 sdcdb-finish-from-src\SpecialChar ~
21206 SDCDB finish command
21221 sdcdb-break\SpecialChar ~
21239 Set break for line with point
21254 sdcdbsrc-mode\SpecialChar ~
21270 Toggle Sdcdbsrc mode
21285 sdcdbsrc-srcmode\SpecialChar ~
21308 Here are a few guidelines that will help the compiler generate more efficient
21309 code, some of the tips are specific to this compiler others are generally
21310 good programming practice.
21313 Use the smallest data type to represent your data-value.
21314 If it is known in advance that the value is going to be less than 256 then
21315 use an 'unsigned char' instead of a 'short' or 'int'.
21316 Please note, that ANSI C requires both signed and unsigned chars to be
21317 promoted to 'signed int' before doing any operation.
21319 \begin_inset LatexCommand \index{type promotion}
21323 can be omitted, if the result is the same.
21324 The effect of the promotion rules together with the sign-extension is often
21331 unsigned char uc = 0xfe;
21333 if (uc * uc < 0) /* this is true! */
21352 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21362 (unsigned char) -12 / (signed char) -3 = ...
21365 No, the result is not 4:
21370 (int) (unsigned char) -12 / (int) (signed char) -3 =
21372 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21374 (int) 0x00f4 / (int) 0xfffd =
21376 (int) 0x00f4 / (int) 0xfffd =
21378 (int) 244 / (int) -3 =
21380 (int) -81 = (int) 0xffaf;
21383 Don't complain, that gcc gives you a different result.
21384 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21385 Therefore the results are different.
21388 \begin_inset Quotes sld
21392 \begin_inset Quotes srd
21398 If well-defined overflow characteristics are important and negative values
21399 are not, or if you want to steer clear of sign-extension problems when
21400 manipulating bits or bytes, use one of the corresponding unsigned types.
21401 (Beware when mixing signed and unsigned values in expressions, though.)
21403 Although character types (especially unsigned char) can be used as "tiny"
21404 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21405 ble sign extension and increased code size.
21409 Use unsigned when it is known in advance that the value is not going to
21411 This helps especially if you are doing division or multiplication, bit-shifting
21412 or are using an array index.
21415 NEVER jump into a LOOP.
21418 Declare the variables to be local
21419 \begin_inset LatexCommand \index{local variables}
21423 whenever possible, especially loop control variables (induction).
21426 Since the compiler does not always do implicit integral promotion, the programme
21427 r should do an explicit cast when integral promotion is required.
21430 Reducing the size of division, multiplication & modulus operations can reduce
21431 code size substantially.
21432 Take the following code for example.
21438 foobar(unsigned int p1, unsigned char ch)
21446 unsigned char ch1 = p1 % ch ;
21457 For the modulus operation the variable ch will be promoted to unsigned int
21458 first then the modulus operation will be performed (this will lead to a
21459 call to support routine _moduint()), and the result will be casted to a
21461 If the code is changed to
21466 foobar(unsigned int p1, unsigned char ch)
21474 unsigned char ch1 = (unsigned char)p1 % ch ;
21485 It would substantially reduce the code generated (future versions of the
21486 compiler will be smart enough to detect such optimization opportunities).
21490 Have a look at the assembly listing to get a
21491 \begin_inset Quotes sld
21495 \begin_inset Quotes srd
21498 for the code generation.
21502 \begin_inset LatexCommand \index{Tools}
21506 included in the distribution
21510 \begin_inset Tabular
21511 <lyxtabular version="3" rows="12" columns="3">
21513 <column alignment="center" valignment="top" leftline="true" width="0pt">
21514 <column alignment="center" valignment="top" leftline="true" width="0pt">
21515 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21516 <row topline="true" bottomline="true">
21517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21542 <row topline="true">
21543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21551 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21556 Simulator for various architectures
21559 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21568 <row topline="true">
21569 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21582 header file conversion
21585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21590 sdcc/support/scripts
21594 <row topline="true">
21595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21608 header file conversion
21611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21616 sdcc/support/scripts
21620 <row topline="true">
21621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21655 <row topline="true">
21656 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21672 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21690 <row topline="true">
21691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21725 <row topline="true">
21726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21760 <row topline="true">
21761 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21777 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21795 <row topline="true">
21796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21812 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21830 <row topline="true">
21831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21847 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21865 <row topline="true" bottomline="true">
21866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21910 \begin_inset LatexCommand \index{Documentation}
21914 included in the distribution
21918 \begin_inset Tabular
21919 <lyxtabular version="3" rows="10" columns="2">
21921 <column alignment="left" valignment="top" leftline="true" width="0">
21922 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21923 <row topline="true" bottomline="true">
21924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21937 Where to get / filename
21941 <row topline="true">
21942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21947 SDCC Compiler User Guide
21950 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21955 You're reading it right now
21959 <row topline="true">
21960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21968 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21977 <row topline="true">
21978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21984 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
21989 \begin_inset LatexCommand \index{Assembler documentation}
21993 Assemblers and ASLINK
21994 \begin_inset LatexCommand \index{aslink}
21999 \begin_inset LatexCommand \index{Linker documentation}
22006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22011 sdcc/as/doc/asxhtm.html
22015 <row topline="true">
22016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22021 SDCC regression test
22022 \begin_inset LatexCommand \index{Regression test}
22029 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22034 sdcc/doc/test_suite_spec.pdf
22038 <row topline="true">
22039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22056 <row topline="true">
22057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22062 Notes on debugging with sdcdb
22063 \begin_inset LatexCommand \index{sdcdb (debugger)}
22070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22075 sdcc/debugger/README
22079 <row topline="true">
22080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22085 Software simulator for microcontrollers
22088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22115 <row topline="true">
22116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22121 Temporary notes on the pic16
22122 \begin_inset LatexCommand \index{PIC16}
22129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22134 sdcc/src/pic16/NOTES
22138 <row topline="true" bottomline="true">
22139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22144 SDCC internal documentation (debugging file format)
22147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22183 Related open source tools
22184 \begin_inset LatexCommand \index{Related tools}
22192 \begin_inset Tabular
22193 <lyxtabular version="3" rows="11" columns="3">
22195 <column alignment="center" valignment="top" leftline="true" width="0pt">
22196 <column alignment="block" valignment="top" leftline="true" width="30line%">
22197 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22198 <row topline="true" bottomline="true">
22199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22224 <row topline="true">
22225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22231 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22252 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22260 <row topline="true">
22261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22267 \begin_inset LatexCommand \index{gputils (pic tools)}
22274 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22288 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22296 <row topline="true">
22297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22305 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22313 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22319 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22327 <row topline="true">
22328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22334 \begin_inset LatexCommand \index{indent (source formatting tool)}
22341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22346 Formats C source - Master of the white spaces
22349 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22355 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22363 <row topline="true">
22364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22370 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22377 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22382 Object file conversion, checksumming, ...
22385 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22391 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22399 <row topline="true">
22400 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22406 \begin_inset LatexCommand \index{objdump (tool)}
22413 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22418 Object file conversion, ...
22421 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22426 Part of binutils (should be there anyway)
22430 <row topline="true">
22431 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22437 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22449 Source code documentation system
22452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22458 \begin_inset LatexCommand \url{http://www.doxygen.org}
22466 <row topline="true">
22467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22480 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22489 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22497 <row topline="true">
22498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22504 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22516 Statically checks c sources (see
22517 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22530 \begin_inset LatexCommand \url{http://www.splint.org}
22538 <row topline="true" bottomline="true">
22539 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22545 \begin_inset LatexCommand \index{ddd (debugger)}
22552 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22557 Debugger, serves nicely as GUI to sdcdb
22558 \begin_inset LatexCommand \index{sdcdb (debugger)}
22565 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22571 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22588 Related documentation / recommended reading
22592 \begin_inset Tabular
22593 <lyxtabular version="3" rows="6" columns="3">
22595 <column alignment="center" valignment="top" leftline="true" width="0pt">
22596 <column alignment="block" valignment="top" leftline="true" width="30line%">
22597 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22598 <row topline="true" bottomline="true">
22599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22624 <row topline="true">
22625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22648 \begin_inset LatexCommand \index{C Reference card}
22655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22661 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22669 <row topline="true">
22670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22692 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22700 <row topline="true">
22701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22713 Latest datasheet of the target CPU
22716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22725 <row topline="true">
22726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22738 Revision history of datasheet
22741 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22750 <row topline="true" bottomline="true">
22751 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22761 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22766 Advanced Compiler Design and Implementation
22769 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22774 bookstore (very dedicated, probably read other books first)
22790 Some questions answered, some pointers given - it might be time to in turn
22798 can you solve your project with the selected microcontroller? Would you
22799 find out early or rather late that your target is too small/slow/whatever?
22800 Can you switch to a slightly better device if it doesn't fit?
22803 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22804 and/or another programming language be more adequate? Would an operating
22805 system on the target device help?
22808 if you solved the problem, will the marketing department be happy?
22811 if the marketing department is happy, will customers be happy?
22814 if you're the project manager, marketing department and maybe even the customer
22815 in one person, have you tried to see the project from the outside?
22818 is the project done if you think it is done? Or is just that other interface/pro
22819 tocol/feature/configuration/option missing? How about website, manual(s),
22820 internationali(z|s)ation, packaging, labels, 2nd source for components,
22821 electromagnetic compatability/interference, documentation for production,
22822 production test software, update mechanism, patent issues?
22825 is your project adequately positioned in that magic triangle: fame, fortune,
22829 Maybe not all answers to these questions are known and some answers may
22834 , nevertheless knowing these questions may help you to avoid burnout
22840 burnout is bad for electronic devices, programmers and motorcycle tyres
22844 Chances are you didn't want to hear some of them...
22848 \begin_inset LatexCommand \index{Support}
22855 SDCC has grown to be a large project.
22856 The compiler alone (without the preprocessor, assembler and linker) is
22857 well over 100,000 lines of code (blank stripped).
22858 The open source nature of this project is a key to its continued growth
22860 You gain the benefit and support of many active software developers and
22862 Is SDCC perfect? No, that's why we need your help.
22863 The developers take pride in fixing reported bugs.
22864 You can help by reporting the bugs and helping other SDCC users.
22865 There are lots of ways to contribute, and we encourage you to take part
22866 in making SDCC a great software package.
22870 The SDCC project is hosted on the SDCC sourceforge site at
22871 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22876 You'll find the complete set of mailing lists
22877 \begin_inset LatexCommand \index{Mailing list(s)}
22881 , forums, bug reporting system, patch submission
22882 \begin_inset LatexCommand \index{Patch submission}
22887 \begin_inset LatexCommand \index{download}
22891 area and cvs code repository
22892 \begin_inset LatexCommand \index{cvs code repository}
22900 \begin_inset LatexCommand \index{Bug reporting}
22905 \begin_inset LatexCommand \index{Reporting bugs}
22912 The recommended way of reporting bugs is using the infrastructure of the
22914 You can follow the status of bug reports there and have an overview about
22918 Bug reports are automatically forwarded to the developer mailing list and
22919 will be fixed ASAP.
22920 When reporting a bug, it is very useful to include a small test program
22921 (the smaller the better) which reproduces the problem.
22922 If you can isolate the problem by looking at the generated assembly code,
22923 this can be very helpful.
22924 Compiling your program with the -
22935 \begin_inset LatexCommand \index{-\/-dumpall}
22939 option can sometimes be useful in locating optimization problems.
22940 When reporting a bug please maker sure you:
22943 Attach the code you are compiling with SDCC.
22947 Specify the exact command you use to run SDCC, or attach your Makefile.
22951 Specify the SDCC version (type "
22957 "), your platform, and operating system.
22961 Provide an exact copy of any error message or incorrect output.
22965 Put something meaningful in the subject of your message.
22968 Please attempt to include these 5 important parts, as applicable, in all
22969 requests for support or when reporting any problems or bugs with SDCC.
22970 Though this will make your message lengthy, it will greatly improve your
22971 chance that SDCC users and developers will be able to help you.
22972 Some SDCC developers are frustrated by bug reports without code provided
22973 that they can use to reproduce and ultimately fix the problem, so please
22974 be sure to provide sample code if you are reporting a bug!
22977 Please have a short check that you are using a recent version of SDCC and
22978 the bug is not yet known.
22979 This is the link for reporting bugs:
22980 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
22987 Requesting Features
22988 \begin_inset LatexCommand \label{sub:Requesting-Features}
22993 \begin_inset LatexCommand \index{Feature request}
22998 \begin_inset LatexCommand \index{Requesting features}
23005 Like bug reports feature requests are forwarded to the developer mailing
23007 This is the link for requesting features:
23008 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23018 Like bug reports contributed patches are forwarded to the developer mailing
23020 This is the link for submitting patches
23021 \begin_inset LatexCommand \index{Patch submission}
23026 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23033 You need to specify some parameters to the
23037 command for the patches to be useful.
23038 If you modified more than one file a patch created f.e.
23043 \begin_inset Quotes sld
23046 diff -Naur unmodified_directory modified_directory >my_changes.patch
23047 \begin_inset Quotes srd
23053 will be fine, otherwise
23057 \begin_inset Quotes sld
23060 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23061 \begin_inset Quotes srd
23074 These links should take you directly to the
23075 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23085 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23086 automated messages (mid 2003)
23090 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23095 \begin_inset LatexCommand \index{Mailing list(s)}
23099 and forums are archived and searchable so if you are lucky someone already
23100 had a similar problem.
23101 While mails to the lists themselves are delivered promptly their web front
23102 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23103 if you're seriously using SDCC please consider subscribing to the lists.
23109 You can follow the status of the cvs version
23110 \begin_inset LatexCommand \index{version}
23114 of SDCC by watching the Changelog
23115 \begin_inset LatexCommand \index{Changelog}
23119 in the cvs-repository
23124 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
23132 \begin_inset LatexCommand \index{Release policy}
23139 Historically there often were long delays between official releases and
23140 the sourceforge download area tends to get not updated at all.
23141 Excuses in the past might have referred to problems with live range analysis,
23142 but as this was fixed a while ago, the current problem is that another
23143 excuse has to be found.
23144 Kidding aside, we have to get better there! On the other hand there are
23145 daily snapshots available at
23146 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23150 , and you can always build the very last version (hopefully with many bugs
23151 fixed, and features added) from the source code available at
23152 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23160 \begin_inset LatexCommand \index{Examples}
23167 You'll find some small examples in the directory
23169 sdcc/device/examples/.
23172 More examples and libraries are available at
23174 The SDCC Open Knowledge Resource
23175 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23182 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23189 I did insert a reference to Paul's web site here although it seems rather
23190 dedicated to a specific 8032 board (I think it's okay because it f.e.
23191 shows LCD/Harddisc interface and has a free 8051 monitor.
23192 Independent 8032 board vendors face hard competition of heavily subsidized
23193 development boards anyway).
23196 Maybe we should include some links to real world applications.
23197 Preferably pointer to pointers (one for each architecture) so this stays
23202 \begin_inset LatexCommand \index{Quality control}
23209 The compiler is passed through nightly compile and build checks.
23215 \begin_inset LatexCommand \index{Regression test}
23219 check that SDCC itself compiles flawlessly on several platforms and checks
23220 the quality of the code generated by SDCC by running the code through simulator
23222 There is a separate document
23225 \begin_inset LatexCommand \index{Test suite}
23234 You'll find the test code in the directory
23236 sdcc/support/regression
23239 You can run these tests manually by running
23243 in this directory (or f.e.
23248 \begin_inset Quotes sld
23252 \begin_inset Quotes srd
23258 if you don't want to run the complete tests).
23259 The test code might also be interesting if you want to look for examples
23260 \begin_inset LatexCommand \index{Examples}
23264 checking corner cases of SDCC or if you plan to submit patches
23265 \begin_inset LatexCommand \index{Patch submission}
23272 The pic port uses a different set of regression tests, you'll find them
23275 sdcc/src/regression
23280 SDCC Technical Data
23284 \begin_inset LatexCommand \index{Optimizations}
23291 SDCC performs a host of standard optimizations in addition to some MCU specific
23296 Sub-expression Elimination
23297 \begin_inset LatexCommand \index{Subexpression elimination}
23304 The compiler does local and
23330 will be translated to
23342 Some subexpressions are not as obvious as the above example, e.g.:
23352 In this case the address arithmetic a->b[i] will be computed only once;
23353 the equivalent code in C would be.
23365 The compiler will try to keep these temporary variables in registers.
23368 Dead-Code Elimination
23369 \begin_inset LatexCommand \index{Dead-code elimination}
23390 i = 1; \SpecialChar ~
23399 global = 1;\SpecialChar ~
23412 global = 3;\SpecialChar ~
23437 \begin_inset LatexCommand \index{Copy propagation}
23493 Note: the dead stores created by this copy propagation will be eliminated
23494 by dead-code elimination.
23498 \begin_inset LatexCommand \index{Loop optimization}
23503 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23510 Two types of loop optimizations are done by SDCC
23518 of loop induction variables.
23519 In addition to the strength reduction the optimizer marks the induction
23520 variables and the register allocator tries to keep the induction variables
23521 in registers for the duration of the loop.
23522 Because of this preference of the register allocator
23523 \begin_inset LatexCommand \index{Register allocation}
23527 , loop induction optimization causes an increase in register pressure, which
23528 may cause unwanted spilling of other temporary variables into the stack
23529 \begin_inset LatexCommand \index{stack}
23534 The compiler will generate a warning message when it is forced to allocate
23535 extra space either on the stack or data space.
23536 If this extra space allocation is undesirable then induction optimization
23537 can be eliminated either for the entire source file (with -
23547 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23549 \begin_inset LatexCommand \index{\#pragma noinduction}
23562 for (i = 0 ; i < 100 ; i ++)
23578 for (i = 0; i < 100; i++)
23587 As mentioned previously some loop invariants are not as apparent, all static
23588 address computations are also moved out of the loop.
23593 \begin_inset LatexCommand \index{Strength reduction}
23597 , this optimization substitutes an expression by a cheaper expression:
23602 for (i=0;i < 100; i++)
23620 for (i=0;i< 100;i++) {
23626 ar[itemp1] = itemp2;
23643 The more expensive multiplication
23644 \begin_inset LatexCommand \index{Multiplication}
23648 is changed to a less expensive addition.
23652 \begin_inset LatexCommand \index{Loop reversing}
23659 This optimization is done to reduce the overhead of checking loop boundaries
23660 for every iteration.
23661 Some simple loops can be reversed and implemented using a
23662 \begin_inset Quotes eld
23665 decrement and jump if not zero
23666 \begin_inset Quotes erd
23670 SDCC checks for the following criterion to determine if a loop is reversible
23671 (note: more sophisticated compilers use data-dependency analysis to make
23672 this determination, SDCC uses a more simple minded analysis).
23675 The 'for' loop is of the form
23681 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23691 The <for body> does not contain
23692 \begin_inset Quotes eld
23696 \begin_inset Quotes erd
23700 \begin_inset Quotes erd
23706 All goto's are contained within the loop.
23709 No function calls within the loop.
23712 The loop control variable <sym> is not assigned any value within the loop
23715 The loop control variable does NOT participate in any arithmetic operation
23719 There are NO switch statements in the loop.
23722 Algebraic Simplifications
23725 SDCC does numerous algebraic simplifications, the following is a small sub-set
23726 of these optimizations.
23731 i = j + 0;\SpecialChar ~
23735 /* changed to: */\SpecialChar ~
23741 i /= 2;\SpecialChar ~
23748 /* changed to: */\SpecialChar ~
23754 i = j - j;\SpecialChar ~
23758 /* changed to: */\SpecialChar ~
23764 i = j / 1;\SpecialChar ~
23768 /* changed to: */\SpecialChar ~
23775 Note the subexpressions
23776 \begin_inset LatexCommand \index{Subexpression}
23780 given above are generally introduced by macro expansions or as a result
23781 of copy/constant propagation.
23784 'switch' Statements
23785 \begin_inset LatexCommand \label{sub:'switch'-Statements}
23790 \begin_inset LatexCommand \index{switch statement}
23797 SDCC can optimize switch statements to jump tables
23798 \begin_inset LatexCommand \index{jump tables}
23803 It makes the decision based on an estimate of the generated code size.
23804 SDCC is quite liberal in the requirements for jump table generation:
23807 The labels need not be in order, and the starting number need not be one
23808 or zero, the case labels are in numerical sequence or not too many case
23809 labels are missing.
23815 switch(i) {\SpecialChar ~
23846 case 4: ...\SpecialChar ~
23878 case 5: ...\SpecialChar ~
23910 case 3: ...\SpecialChar ~
23941 case 6: ...\SpecialChar ~
23973 case 7: ...\SpecialChar ~
24005 case 8: ...\SpecialChar ~
24037 case 9: ...\SpecialChar ~
24069 case 10: ...\SpecialChar ~
24100 case 11: ...\SpecialChar ~
24167 Both the above switch statements will be implemented using a jump-table.
24168 The example to the right side is slightly more efficient as the check for
24169 the lower boundary of the jump-table is not needed.
24173 The number of case labels is not larger than supported by the target architectur
24177 If the case labels are not in numerical sequence ('gaps' between cases)
24178 SDCC checks whether a jump table with additionally inserted dummy cases
24179 is still attractive.
24183 If the starting number is not zero and a check for the lower boundary of
24184 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24189 Switch statements which have large gaps in the numeric sequence or those
24190 that have too many case labels can be split into more than one switch statement
24191 for efficient code generation, e.g.:
24271 If the above switch statement is broken down into two switch statements
24361 then both the switch statements will be implemented using jump-tables whereas
24362 the unmodified switch statement will not be.
24365 There might be reasons which SDCC cannot know about to either favour or
24366 not favour jump tables.
24367 If the target system has to be as quick for the last switch case as for
24368 the first (pro jump table), or if the switch argument is known to be zero
24369 in the majority of the cases (contra jump table).
24372 The pragma nojtbound
24373 \begin_inset LatexCommand \index{\#pragma nojtbound}
24377 can be used to turn off checking the
24390 It has no effect if a default label is supplied.
24391 Use of this pragma is dangerous: if the switch
24392 \begin_inset LatexCommand \index{switch statement}
24396 argument is not matched by a case statement the processor will happily
24400 Bit-shifting Operations
24401 \begin_inset LatexCommand \index{Bit shifting}
24408 Bit shifting is one of the most frequently used operation in embedded programmin
24410 SDCC tries to implement bit-shift operations in the most efficient way
24426 generates the following code:
24443 In general SDCC will never setup a loop if the shift count is known.
24486 \begin_inset LatexCommand \index{Bit rotation}
24493 A special case of the bit-shift operation is bit rotation
24494 \begin_inset LatexCommand \index{rotating bits}
24498 , SDCC recognizes the following expression to be a left bit-rotation:
24508 char i;\SpecialChar ~
24519 /* unsigned is needed for rotation */
24524 i = ((i << 1) | (i >> 7));
24533 will generate the following code:
24552 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24553 ns of this case will also be recognized as bit-rotation, i.e.:
24558 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24561 Nibble and Byte Swapping
24564 Other special cases of the bit-shift operations are nibble or byte swapping
24565 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24569 , SDCC recognizes the following expressions:
24592 i = ((i << 4) | (i >> 4));
24598 j = ((j << 8) | (j >> 8));
24601 and generates a swap instruction for the nibble swapping
24602 \begin_inset LatexCommand \index{Nibble swapping}
24606 or move instructions for the byte swapping
24607 \begin_inset LatexCommand \index{Byte swapping}
24613 \begin_inset Quotes sld
24617 \begin_inset Quotes srd
24620 example can be used to convert from little to big-endian or vice versa.
24621 If you want to change the endianness of a
24625 integer you have to cast to
24632 Note that SDCC stores numbers in little-endian
24638 Usually 8-bit processors don't care much about endianness.
24639 This is not the case for the standard 8051 which only has an instruction
24645 \begin_inset LatexCommand \index{DPTR}
24653 so little-endian is the more efficient byte order.
24657 \begin_inset LatexCommand \index{little-endian}
24662 \begin_inset LatexCommand \index{Endianness}
24667 lowest order first).
24671 \begin_inset LatexCommand \index{Highest Order Bit}
24676 \begin_inset LatexCommand \index{Any Order Bit}
24683 It is frequently required to obtain the highest order bit of an integral
24684 type (long, int, short or char types).
24685 Also obtaining any other order bit is not uncommon.
24686 SDCC recognizes the following expressions to yield the highest order bit
24687 and generates optimized code for it, e.g.:
24700 unsigned char hob1, aob1;
24704 bit hob2, hob3, aob2, aob3;
24713 hob1 = (gint >> 15) & 1;
24717 hob2 = (gint >> 15) & 1;
24721 hob3 = gint & 0x8000;
24725 aob1 = (gint >> 9) & 1;
24729 aob2 = (gint >> 8) & 1;
24733 aob3 = gint & 0x0800;
24743 will generate the following code:
24776 000A E5*01\SpecialChar ~
24803 000C 23\SpecialChar ~
24834 000D 54 01\SpecialChar ~
24861 000F F5*02\SpecialChar ~
24916 0011 E5*01\SpecialChar ~
24943 0013 33\SpecialChar ~
24973 0014 92*00\SpecialChar ~
25028 0016 E5*01\SpecialChar ~
25055 0018 33\SpecialChar ~
25085 0019 92*01\SpecialChar ~
25140 001B E5*01\SpecialChar ~
25167 001D 03\SpecialChar ~
25198 001E 54 01\SpecialChar ~
25225 0020 F5*03\SpecialChar ~
25280 0022 E5*01\SpecialChar ~
25307 0024 13\SpecialChar ~
25337 0025 92*02\SpecialChar ~
25392 0027 E5*01\SpecialChar ~
25419 0029 A2 E3\SpecialChar ~
25446 002B 92*03\SpecialChar ~
25474 Other variations of these cases however will
25479 They are standard C expressions, so I heartily recommend these be the only
25480 way to get the highest order bit, (it is portable).
25481 Of course it will be recognized even if it is embedded in other expressions,
25487 xyz = gint + ((gint >> 15) & 1);
25490 will still be recognized.
25494 \begin_inset LatexCommand \index{Higher Order Byte}
25498 / Higher Order Word
25499 \begin_inset LatexCommand \index{Higher Order Word}
25506 It is also frequently required to obtain a higher order byte or word of
25507 a larger integral type (long, int or short types).
25508 SDCC recognizes the following expressions to yield the higher order byte
25509 or word and generates optimized code for it, e.g.:
25516 unsigned long int glong;
25524 unsigned char hob1, hob2;
25528 unsigned int how1, how2;
25537 hob1 = (gint >> 8) & 0xFF;
25541 hob2 = glong >> 24;
25545 how1 = (glong >> 16) & 0xFFFF;
25559 will generate the following code:
25592 0037 85*01*06\SpecialChar ~
25614 _foo_hob1_1_1,(_gint + 1)
25644 003A 85*05*07\SpecialChar ~
25666 _foo_hob2_1_1,(_glong + 3)
25696 003D 85*04*08\SpecialChar ~
25718 _foo_how1_1_1,(_glong + 2)
25720 0040 85*05*09\SpecialChar ~
25742 (_foo_how1_1_1 + 1),(_glong + 3)
25744 0043 85*03*0A\SpecialChar ~
25766 _foo_how2_1_1,(_glong + 1)
25768 0046 85*04*0B\SpecialChar ~
25790 (_foo_how2_1_1 + 1),(_glong + 2)
25793 Again, variations of these cases may
25798 They are standard C expressions, so I heartily recommend these be the only
25799 way to get the higher order byte/word, (it is portable).
25800 Of course it will be recognized even if it is embedded in other expressions,
25806 xyz = gint + ((gint >> 8) & 0xFF);
25809 will still be recognized.
25813 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
25818 \begin_inset LatexCommand \index{Peephole optimizer}
25825 The compiler uses a rule based, pattern matching and re-writing mechanism
25826 for peep-hole optimization.
25831 a peep-hole optimizer by Christopher W.
25832 Fraser (cwfraser\SpecialChar ~
25835 A default set of rules are compiled into the compiler, additional rules
25836 may be added with the
25849 \begin_inset LatexCommand \index{-\/-peep-file}
25856 The rule language is best illustrated with examples.
25880 The above rule will change the following assembly
25881 \begin_inset LatexCommand \index{Assembler routines}
25903 Note: All occurrences of a
25907 (pattern variable) must denote the same string.
25908 With the above rule, the assembly sequence:
25918 will remain unmodified.
25922 Other special case optimizations may be added by the user (via
25938 some variants of the 8051 MCU
25939 \begin_inset LatexCommand \index{MCS51 variants}
25952 The following two rules will change all
25971 replace { lcall %1 } by { acall %1 }
25973 replace { ljmp %1 } by { ajmp %1 }
25978 inline-assembler code
25980 is also passed through the peep hole optimizer, thus the peephole optimizer
25981 can also be used as an assembly level macro expander.
25982 The rules themselves are MCU dependent whereas the rule language infra-structur
25983 e is MCU independent.
25984 Peephole optimization rules for other MCU can be easily programmed using
25989 The syntax for a rule is as follows:
25994 rule := replace [ restart ] '{' <assembly sequence> '
26032 <assembly sequence> '
26050 '}' [if <functionName> ] '
26055 <assembly sequence> := assembly instruction (each instruction including
26056 labels must be on a separate line).
26060 The optimizer will apply to the rules one by one from the top in the sequence
26061 of their appearance, it will terminate when all rules are exhausted.
26062 If the 'restart' option is specified, then the optimizer will start matching
26063 the rules again from the top, this option for a rule is expensive (performance)
26064 , it is intended to be used in situations where a transformation will trigger
26065 the same rule again.
26066 An example of this (not a good one, it has side effects) is the following
26089 Note that the replace pattern cannot be a blank, but can be a comment line.
26090 Without the 'restart' option only the innermost 'pop' 'push' pair would
26091 be eliminated, i.e.:
26121 the restart option the rule will be applied again to the resulting code
26122 and then all the pop-push pairs will be eliminated to yield:
26132 A conditional function can be attached to a rule.
26133 Attaching rules are somewhat more involved, let me illustrate this with
26160 The optimizer does a look-up of a function name table defined in function
26165 in the source file SDCCpeeph.c, with the name
26170 If it finds a corresponding entry the function is called.
26171 Note there can be no parameters specified for these functions, in this
26176 is crucial, since the function
26180 expects to find the label in that particular variable (the hash table containin
26181 g the variable bindings is passed as a parameter).
26182 If you want to code more such functions, take a close look at the function
26183 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26184 Currently implemented are
26186 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26187 24bitMode, portIsDS390, 24bitModeAndPortDS390
26196 I know this whole thing is a little kludgey, but maybe some day we will
26197 have some better means.
26198 If you are looking at this file, you will see the default rules that are
26199 compiled into the compiler, you can add your own rules in the default set
26200 there if you get tired of specifying the -
26214 \begin_inset LatexCommand \index{ANSI-compliance}
26219 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26226 Deviations from the compliance:
26229 functions are not reentrant
26230 \begin_inset LatexCommand \index{reentrant}
26234 unless explicitly declared as such or the
26247 \begin_inset LatexCommand \index{-\/-stack-auto}
26253 command line option is specified.
26256 structures and unions cannot be assigned values directly, cannot be passed
26257 as function parameters or assigned to each other and cannot be a return
26258 value from a function, e.g.:
26284 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26295 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26317 return rets;/* is invalid in SDCC although allowed in ANSI */
26323 initialization of structure arrays must be fully braced.
26329 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26331 struct s { char x } a[] = {{1}, {2}}; /* OK */
26336 \begin_inset LatexCommand \index{long long (not supported)}
26341 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26349 \begin_inset LatexCommand \index{double (not supported)}
26353 ' precision floating point
26354 \begin_inset LatexCommand \index{Floating point support}
26361 No support for setjmp
26362 \begin_inset LatexCommand \index{setjmp (not supported)}
26367 \begin_inset LatexCommand \index{longjmp (not supported)}
26375 \begin_inset LatexCommand \index{K\&R style}
26379 function declarations are NOT allowed.
26385 foo(i,j) /* this old style of function declarations */
26387 int i,j; /* are valid in ANSI but not valid in SDCC */
26402 Certain words that are valid identifiers in the standard may be reserved
26403 words in SDCC unless the
26416 \begin_inset LatexCommand \index{-\/-std-c89}
26431 \begin_inset LatexCommand \index{-\/-std-c99}
26437 command line options are used.
26438 These may include (depending on the selected processor): 'at', 'banked',
26439 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26440 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26441 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26443 Compliant equivalents of these keywords are always available in a form
26444 that begin with two underscores
26445 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26450 '__data' instead of 'data'.
26453 Cyclomatic Complexity
26454 \begin_inset LatexCommand \index{Cyclomatic complexity}
26461 Cyclomatic complexity of a function is defined as the number of independent
26462 paths the program can take during execution of the function.
26463 This is an important number since it defines the number test cases you
26464 have to generate to validate the function.
26465 The accepted industry standard for complexity number is 10, if the cyclomatic
26466 complexity reported by SDCC exceeds 10 you should think about simplification
26467 of the function logic.
26468 Note that the complexity level is not related to the number of lines of
26469 code in a function.
26470 Large functions can have low complexity, and small functions can have large
26476 SDCC uses the following formula to compute the complexity:
26481 complexity = (number of edges in control flow graph) - (number of nodes
26482 in control flow graph) + 2;
26486 Having said that the industry standard is 10, you should be aware that in
26487 some cases it be may unavoidable to have a complexity level of less than
26489 For example if you have switch statement with more than 10 case labels,
26490 each case label adds one to the complexity level.
26491 The complexity level is by no means an absolute measure of the algorithmic
26492 complexity of the function, it does however provide a good starting point
26493 for which functions you might look at for further optimization.
26496 Retargetting for other Processors
26499 The issues for retargetting the compiler are far too numerous to be covered
26501 What follows is a brief description of each of the seven phases of the
26502 compiler and its MCU dependency.
26505 Parsing the source and building the annotated parse tree.
26506 This phase is largely MCU independent (except for the language extensions).
26507 Syntax & semantic checks are also done in this phase, along with some initial
26508 optimizations like back patching labels and the pattern matching optimizations
26509 like bit-rotation etc.
26512 The second phase involves generating an intermediate code which can be easy
26513 manipulated during the later phases.
26514 This phase is entirely MCU independent.
26515 The intermediate code generation assumes the target machine has unlimited
26516 number of registers, and designates them with the name iTemp.
26517 The compiler can be made to dump a human readable form of the code generated
26531 This phase does the bulk of the standard optimizations and is also MCU independe
26533 This phase can be broken down into several sub-phases:
26537 Break down intermediate code (iCode) into basic blocks.
26539 Do control flow & data flow analysis on the basic blocks.
26541 Do local common subexpression elimination, then global subexpression elimination
26543 Dead code elimination
26547 If loop optimizations caused any changes then do 'global subexpression eliminati
26548 on' and 'dead code elimination' again.
26551 This phase determines the live-ranges; by live range I mean those iTemp
26552 variables defined by the compiler that still survive after all the optimization
26554 Live range analysis
26555 \begin_inset LatexCommand \index{Live range analysis}
26559 is essential for register allocation, since these computation determines
26560 which of these iTemps will be assigned to registers, and for how long.
26563 Phase five is register allocation.
26564 There are two parts to this process.
26568 The first part I call 'register packing' (for lack of a better term).
26569 In this case several MCU specific expression folding is done to reduce
26574 The second part is more MCU independent and deals with allocating registers
26575 to the remaining live ranges.
26576 A lot of MCU specific code does creep into this phase because of the limited
26577 number of index registers available in the 8051.
26580 The Code generation phase is (unhappily), entirely MCU dependent and very
26581 little (if any at all) of this code can be reused for other MCU.
26582 However the scheme for allocating a homogenized assembler operand for each
26583 iCode operand may be reused.
26586 As mentioned in the optimization section the peep-hole optimizer is rule
26587 based system, which can reprogrammed for other MCUs.
26591 \begin_inset LatexCommand \index{Compiler internals}
26598 The anatomy of the compiler
26599 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26608 This is an excerpt from an article published in Circuit Cellar Magazine
26614 It's a little outdated (the compiler is much more efficient now and user/develo
26615 per friendly), but pretty well exposes the guts of it all.
26621 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26622 It is fairly easy to retarget for other 8-bit MCU.
26623 Here we take a look at some of the internals of the compiler.
26628 \begin_inset LatexCommand \index{Parsing}
26635 Parsing the input source file and creating an AST (Annotated Syntax Tree
26636 \begin_inset LatexCommand \index{Annotated syntax tree}
26641 This phase also involves propagating types (annotating each node of the
26642 parse tree with type information) and semantic analysis.
26643 There are some MCU specific parsing rules.
26644 For example the storage classes, the extended storage classes are MCU specific
26645 while there may be a xdata storage class for 8051 there is no such storage
26646 class for z80 or Atmel AVR.
26647 SDCC allows MCU specific storage class extensions, i.e.
26648 xdata will be treated as a storage class specifier when parsing 8051 C
26649 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
26654 \begin_inset LatexCommand \index{iCode}
26661 Intermediate code generation.
26662 In this phase the AST is broken down into three-operand form (iCode).
26663 These three operand forms are represented as doubly linked lists.
26664 ICode is the term given to the intermediate form generated by the compiler.
26665 ICode example section shows some examples of iCode generated for some simple
26666 C source functions.
26670 \begin_inset LatexCommand \index{Optimizations}
26677 Bulk of the target independent optimizations is performed in this phase.
26678 The optimizations include constant propagation, common sub-expression eliminati
26679 on, loop invariant code movement, strength reduction of loop induction variables
26680 and dead-code elimination.
26683 Live range analysis
26684 \begin_inset LatexCommand \index{Live range analysis}
26691 During intermediate code generation phase, the compiler assumes the target
26692 machine has infinite number of registers and generates a lot of temporary
26694 The live range computation determines the lifetime of each of these compiler-ge
26695 nerated temporaries.
26696 A picture speaks a thousand words.
26697 ICode example sections show the live range annotations for each of the
26699 It is important to note here, each iCode is assigned a number in the order
26700 of its execution in the function.
26701 The live ranges are computed in terms of these numbers.
26702 The from number is the number of the iCode which first defines the operand
26703 and the to number signifies the iCode which uses this operand last.
26706 Register Allocation
26707 \begin_inset LatexCommand \index{Register allocation}
26714 The register allocation determines the type and number of registers needed
26716 In most MCUs only a few registers can be used for indirect addressing.
26717 In case of 8051 for example the registers R0 & R1 can be used to indirectly
26718 address the internal ram and DPTR to indirectly address the external ram.
26719 The compiler will try to allocate the appropriate register to pointer variables
26721 ICode example section shows the operands annotated with the registers assigned
26723 The compiler will try to keep operands in registers as much as possible;
26724 there are several schemes the compiler uses to do achieve this.
26725 When the compiler runs out of registers the compiler will check to see
26726 if there are any live operands which is not used or defined in the current
26727 basic block being processed, if there are any found then it will push that
26728 operand and use the registers in this block, the operand will then be popped
26729 at the end of the basic block.
26733 There are other MCU specific considerations in this phase.
26734 Some MCUs have an accumulator; very short-lived operands could be assigned
26735 to the accumulator instead of a general-purpose register.
26741 Figure II gives a table of iCode operations supported by the compiler.
26742 The code generation involves translating these operations into corresponding
26743 assembly code for the processor.
26744 This sounds overly simple but that is the essence of code generation.
26745 Some of the iCode operations are generated on a MCU specific manner for
26746 example, the z80 port does not use registers to pass parameters so the
26747 SEND and RECV iCode operations will not be generated, and it also does
26748 not support JUMPTABLES.
26755 <Where is Figure II?>
26758 In the original article Figure II was announced to be downloadable on
26763 Unfortunately it never seemed to have shown up there, so: where is Figure
26768 \begin_inset LatexCommand \index{iCode}
26775 This section shows some details of iCode.
26776 The example C code does not do anything useful; it is used as an example
26777 to illustrate the intermediate code generated by the compiler.
26789 /* This function does nothing useful.
26796 for the purpose of explaining iCode */
26799 short function (data int *x)
26807 short i=10; \SpecialChar ~
26809 /* dead initialization eliminated */
26814 short sum=10; /* dead initialization eliminated */
26827 while (*x) *x++ = *p++;
26841 /* compiler detects i,j to be induction variables */
26845 for (i = 0, j = 10 ; i < 10 ; i++, j
26871 mul += i * 3; \SpecialChar ~
26873 /* this multiplication remains */
26879 gint += j * 3;\SpecialChar ~
26881 /* this multiplication changed to addition */
26895 In addition to the operands each iCode contains information about the filename
26896 and line it corresponds to in the source file.
26897 The first field in the listing should be interpreted as follows:
26902 Filename(linenumber: iCode Execution sequence number : ICode hash table
26903 key : loop depth of the iCode).
26908 Then follows the human readable form of the ICode operation.
26909 Each operand of this triplet form can be of three basic types a) compiler
26910 generated temporary b) user defined variable c) a constant value.
26911 Note that local variables and parameters are replaced by compiler generated
26914 \begin_inset LatexCommand \index{Live range analysis}
26918 are computed only for temporaries (i.e.
26919 live ranges are not computed for global variables).
26921 \begin_inset LatexCommand \index{Register allocation}
26925 are allocated for temporaries only.
26926 Operands are formatted in the following manner:
26931 Operand Name [lr live-from : live-to ] { type information } [ registers
26937 As mentioned earlier the live ranges are computed in terms of the execution
26938 sequence number of the iCodes, for example
26940 the iTemp0 is live from (i.e.
26941 first defined in iCode with execution sequence number 3, and is last used
26942 in the iCode with sequence number 5).
26943 For induction variables such as iTemp21 the live range computation extends
26944 the lifetime from the start to the end of the loop.
26946 The register allocator used the live range information to allocate registers,
26947 the same registers may be used for different temporaries if their live
26948 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
26949 iTemp17 since their live ranges do not overlap.
26950 In addition the allocator also takes into consideration the type and usage
26951 of a temporary, for example itemp6 is a pointer to near space and is used
26952 as to fetch data from (i.e.
26953 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
26954 Some short lived temporaries are allocated to special registers which have
26955 meaning to the code generator e.g.
26956 iTemp13 is allocated to a pseudo register CC which tells the back end that
26957 the temporary is used only for a conditional jump the code generation makes
26958 use of this information to optimize a compare and jump ICode.
26960 There are several loop optimizations
26961 \begin_inset LatexCommand \index{Loop optimization}
26965 performed by the compiler.
26966 It can detect induction variables iTemp21(i) and iTemp23(j).
26967 Also note the compiler does selective strength reduction
26968 \begin_inset LatexCommand \index{Strength reduction}
26973 the multiplication of an induction variable in line 18 (gint = j * 3) is
26974 changed to addition, a new temporary iTemp17 is allocated and assigned
26975 a initial value, a constant 3 is then added for each iteration of the loop.
26976 The compiler does not change the multiplication
26977 \begin_inset LatexCommand \index{Multiplication}
26981 in line 17 however since the processor does support an 8 * 8 bit multiplication.
26983 Note the dead code elimination
26984 \begin_inset LatexCommand \index{Dead-code elimination}
26988 optimization eliminated the dead assignments in line 7 & 8 to I and sum
26996 Sample.c (5:1:0:0) _entry($9) :
27001 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27006 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27011 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27016 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27022 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27027 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27033 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27038 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27044 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27050 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27056 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27062 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27063 * int}[r0] + 0x2 {short}
27068 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27073 Sample.c(11:17:21:0)_whilebreak_0($3) :
27078 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27083 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27088 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27093 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27098 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27103 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27108 Sample.c(15:24:26:1)_forcond_0($4) :
27113 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27119 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27124 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27125 + ITemp21 [lr21:38]{short}[r4]
27130 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27136 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27137 + iTemp15 [lr29:30]{short}[r1]
27142 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27148 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27154 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27160 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27166 Sample.c(19:38:47:1) goto _forcond_0($4)
27171 Sample.c(19:39:48:0)_forbreak_0($7) :
27176 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27177 + ITemp11 [lr19:40]{short}[r3]
27182 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27187 Sample.c(20:42:51:0)_return($8) :
27192 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27198 Finally the code generated for this function:
27239 ; ----------------------------------------------
27244 ; function function
27249 ; ----------------------------------------------
27259 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27271 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27283 ;_whilecontinue_0($1) :
27293 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27298 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27357 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27376 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27423 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27463 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27489 ; iTemp6 [lr5:16]{_near * int}[r0] =
27494 ; iTemp6 [lr5:16]{_near * int}[r0] +
27511 ; goto _whilecontinue_0($1)
27523 ; _whilebreak_0($3) :
27533 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27545 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27557 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27569 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27588 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27617 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27622 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27667 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
27672 ; iTemp21 [lr21:38]{short}[r4]
27698 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
27731 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
27736 ; iTemp15 [lr29:30]{short}[r1]
27755 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
27802 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
27849 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
27861 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
27875 cjne r5,#0xff,00104$
27887 ; goto _forcond_0($4)
27899 ; _forbreak_0($7) :
27909 ; ret iTemp24 [lr40:41]{short}
27952 A few words about basic block successors, predecessors and dominators
27955 Successors are basic blocks
27956 \begin_inset LatexCommand \index{Basic blocks}
27960 that might execute after this basic block.
27962 Predecessors are basic blocks that might execute before reaching this basic
27965 Dominators are basic blocks that WILL execute before reaching this basic
27999 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28002 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28005 c) domVect of [BB4] = BB1 ...
28006 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28014 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28024 Thanks to all the other volunteer developers who have helped with coding,
28025 testing, web-page creation, distribution sets, etc.
28026 You know who you are :-)
28033 This document was initially written by Sandeep Dutta
28036 All product names mentioned herein may be trademarks
28037 \begin_inset LatexCommand \index{Trademarks}
28041 of their respective companies.
28048 To avoid confusion, the installation and building options for SDCC itself
28049 (chapter 2) are not part of the index.
28053 \begin_inset LatexCommand \printindex{}