1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
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, 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 a Freeware, retargettable, optimizing ANSI-C compiler by
135 designed for 8 bit Microprocessors.
136 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
138 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
142 , etc.), Dallas DS80C390 variants, Motorola HC08 and Zilog Z80 based MCUs.
143 It can be retargetted for other microprocessors, support for Microchip
144 PIC, Atmel AVR is under development.
145 The entire source code for the compiler is distributed under GPL.
146 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
147 SDCC has extensive language extensions suitable for utilizing various microcont
148 rollers and underlying hardware effectively.
153 In addition to the MCU specific optimizations SDCC also does a host of standard
157 global sub expression elimination,
160 loop optimizations (loop invariant, strength reduction of induction variables
164 constant folding & propagation,
170 dead code elimination
180 For the back-end SDCC uses a global register allocation scheme which should
181 be well suited for other 8 bit MCUs.
186 The peep hole optimizer uses a rule based substitution mechanism which is
192 Supported data-types are:
195 char (8 bits, 1 byte),
198 short and int (16 bits, 2 bytes),
201 long (32 bit, 4 bytes)
208 The compiler also allows
210 inline assembler code
212 to be embedded anywhere in a function.
213 In addition, routines developed in assembly can also be called.
217 SDCC also provides an option (-
227 -cyclomatic) to report the relative complexity of a function.
228 These functions can then be further optimized, or hand coded in assembly
234 SDCC also comes with a companion source level debugger SDCDB, the debugger
235 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
240 The latest version can be downloaded from
241 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
251 Please note: the compiler will probably always be some steps ahead of this
256 \begin_inset LatexCommand \index{Status of documentation}
266 Obviously this has pros and cons
275 All packages used in this compiler system are
283 ; source code for all the sub-packages (pre-processor, assemblers, linkers
284 etc) is distributed with the package.
285 This documentation is maintained using a freeware word processor (LyX).
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of the GNU General Public License
289 \begin_inset LatexCommand \index{GNU General Public License, GPL}
293 as published by the Free Software Foundation; either version 2, or (at
294 your option) any later version.
295 This program is distributed in the hope that it will be useful, but WITHOUT
296 ANY WARRANTY; without even the implied warranty
297 \begin_inset LatexCommand \index{warranty}
301 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
302 See the GNU General Public License for more details.
303 You should have received a copy of the GNU General Public License along
304 with this program; if not, write to the Free Software Foundation, 59 Temple
305 Place - Suite 330, Boston, MA 02111-1307, USA.
306 In other words, you are welcome to use, share and improve this program.
307 You are forbidden to forbid anyone else to use, share and improve what
309 Help stamp out software-hoarding!
312 Typographic conventions
313 \begin_inset LatexCommand \index{Typographic conventions}
320 Throughout this manual, we will use the following convention.
321 Commands you have to type in are printed in
329 Code samples are printed in
334 Interesting items and new terms are printed in
339 Compatibility with previous versions
342 This version has numerous bug fixes compared with the previous version.
343 But we also introduced some incompatibilities with older versions.
344 Not just for the fun of it, but to make the compiler more stable, efficient
346 \begin_inset LatexCommand \index{ANSI-compliance}
351 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
355 for ANSI-Compliance).
361 short is now equivalent to int (16 bits), it used to be equivalent to char
362 (8 bits) which is not ANSI compliant
365 the default directory for gcc-builds where include, library and documentation
366 files are stored is now in /usr/local/share
369 char type parameters to vararg functions are casted to int unless explicitly
386 will push a as an int and as a char resp.
399 -regextend has been removed
412 -noregparms has been removed
425 -stack-after-data has been removed
430 <pending: more incompatibilities?>
436 What do you need before you start installation of SDCC? A computer, and
438 The preferred method of installation is to compile SDCC from source using
440 For Windows some pre-compiled binary distributions are available for your
442 You should have some experience with command line tools and compiler use.
448 The SDCC home page at
449 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
453 is a great place to find distribution sets.
454 You can also find links to the user mailing lists that offer help or discuss
455 SDCC with other SDCC users.
456 Web links to other SDCC related sites can also be found here.
457 This document can be found in the DOC directory of the source package as
459 A pdf version of this document is available at
460 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
465 Some of the other tools (simulator and assembler) included with SDCC contain
466 their own documentation and can be found in the source distribution.
467 If you want the latest unreleased software, the complete source package
468 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
471 Wishes for the future
474 There are (and always will be) some things that could be done.
475 Here are some I can think of:
482 char KernelFunction3(char p) at 0x340;
490 \begin_inset LatexCommand \index{code banking (not supported)}
500 If you can think of some more, please see the section
501 \begin_inset LatexCommand \ref{sub:Requesting-Features}
505 about filing feature requests
506 \begin_inset LatexCommand \index{Requesting features}
511 \begin_inset LatexCommand \index{Feature request}
521 \begin_inset LatexCommand \index{Installation}
528 For most users it is sufficient to skip to either section
529 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
534 \begin_inset LatexCommand \ref{sub:Windows-Install}
539 More detailled instructions follow below.
543 \begin_inset LatexCommand \index{Options SDCC configuration}
550 The install paths, search paths and other options are defined when running
552 The defaults can be overridden by:
554 \labelwidthstring 00.00.0000
566 -prefix see table below
568 \labelwidthstring 00.00.0000
580 -exec_prefix see table below
582 \labelwidthstring 00.00.0000
594 -bindir see table below
596 \labelwidthstring 00.00.0000
608 -datadir see table below
610 \labelwidthstring 00.00.0000
612 docdir environment variable, see table below
614 \labelwidthstring 00.00.0000
616 include_dir_suffix environment variable, see table below
618 \labelwidthstring 00.00.0000
620 lib_dir_suffix environment variable, see table below
622 \labelwidthstring 00.00.0000
624 sdccconf_h_dir_separator environment variable, either / or
629 This character will only be used in sdccconf.h; don't forget it's a C-header,
630 therefore a double-backslash is needed there.
632 \labelwidthstring 00.00.0000
644 -disable-mcs51-port Excludes the Intel mcs51 port
646 \labelwidthstring 00.00.0000
658 -disable-gbz80-port Excludes the Gameboy gbz80 port
660 \labelwidthstring 00.00.0000
672 -disable-z80-port Excludes the z80 port
674 \labelwidthstring 00.00.0000
686 -disable-avr-port Excludes the AVR port
688 \labelwidthstring 00.00.0000
700 -disable-ds390-port Excludes the DS390 port
702 \labelwidthstring 00.00.0000
714 -disable-hc08-port Excludes the HC08 port
716 \labelwidthstring 00.00.0000
728 -disable-pic-port Excludes the PIC port
730 \labelwidthstring 00.00.0000
742 -disable-xa51-port Excludes the XA51 port
744 \labelwidthstring 00.00.0000
756 -disable-ucsim Disables configuring and building of ucsim
758 \labelwidthstring 00.00.0000
770 -disable-device-lib-build Disables automatically building device libraries
772 \labelwidthstring 00.00.0000
784 -disable-packihx Disables building packihx
786 \labelwidthstring 00.00.0000
798 -enable-libgc Use the Bohem memory allocator.
799 Lower runtime footprint.
802 Furthermore the environment variables CC, CFLAGS, ...
803 the tools and their arguments can be influenced.
804 Please see `configure -
814 -help` and the man/info pages of `configure` for details.
818 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
819 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
820 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
821 At the moment it's not possible to change the default settings (it was
822 simply never required).
826 These configure options are compiled into the binaries, and can only be
827 changed by rerunning 'configure' and recompiling SDCC.
828 The configure options are written in
832 to distinguish them from run time environment variables (see section search
838 \begin_inset Quotes sld
842 \begin_inset Quotes srd
845 are used by the SDCC team to build the official Win32 binaries.
846 The SDCC team uses Mingw32 to build the official Windows binaries, because
853 a gcc compiler and last but not least
856 the binaries can be built by cross compiling on Sourceforge's compile farm.
859 See the examples, how to pass the Win32 settings to 'configure'.
860 The other Win32 builds using Borland, VC or whatever don't use 'configure',
861 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
873 <lyxtabular version="3" rows="8" columns="3">
875 <column alignment="block" valignment="top" leftline="true" width="0in">
876 <column alignment="block" valignment="top" leftline="true" width="0in">
877 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
878 <row topline="true" bottomline="true">
879 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
895 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
935 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
955 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1004 <row topline="true">
1005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1027 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1038 <row topline="true">
1039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1076 <row topline="true">
1077 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1104 <row topline="true" bottomline="true">
1105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1123 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1141 'configure' also computes relative paths.
1142 This is needed for full relocatability of a binary package and to complete
1143 search paths (see section search paths below):
1149 \begin_inset Tabular
1150 <lyxtabular version="3" rows="4" columns="3">
1152 <column alignment="block" valignment="top" leftline="true" width="0in">
1153 <column alignment="block" valignment="top" leftline="true" width="0in">
1154 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1155 <row topline="true" bottomline="true">
1156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1181 <row topline="true" bottomline="true">
1182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1209 <row bottomline="true">
1210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1237 <row bottomline="true">
1238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1289 \begin_inset Quotes srd
1293 \begin_inset Quotes srd
1307 \begin_inset Quotes srd
1311 \begin_inset Quotes srd
1339 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1348 \begin_inset Quotes srd
1351 i586-mingw32msvc-gcc
1352 \begin_inset Quotes srd
1356 \begin_inset Quotes srd
1359 i586-mingw32msvc-g++
1360 \begin_inset Quotes srd
1368 \begin_inset Quotes srd
1371 i586-mingw32msvc-ranlib
1372 \begin_inset Quotes srd
1380 \begin_inset Quotes srd
1383 i586-mingw32msvc-strip
1384 \begin_inset Quotes srd
1402 \begin_inset Quotes srd
1406 \begin_inset Quotes srd
1424 \begin_inset Quotes srd
1428 \begin_inset Quotes srd
1436 \begin_inset Quotes srd
1440 \begin_inset Quotes srd
1448 \begin_inset Quotes srd
1452 \begin_inset Quotes srd
1460 \begin_inset Quotes srd
1464 \begin_inset Quotes srd
1471 sdccconf_h_dir_separator=
1472 \begin_inset Quotes srd
1484 \begin_inset Quotes srd
1501 -disable-device-lib-build
1529 -host=i586-mingw32msvc -
1539 -build=unknown-unknown-linux-gnu
1543 \begin_inset Quotes sld
1547 \begin_inset Quotes srd
1550 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1559 \begin_inset Quotes srd
1563 \begin_inset Quotes srd
1571 \begin_inset Quotes srd
1575 \begin_inset Quotes srd
1593 \begin_inset Quotes srd
1597 \begin_inset Quotes srd
1615 \begin_inset Quotes srd
1619 \begin_inset Quotes srd
1627 \begin_inset Quotes srd
1631 \begin_inset Quotes srd
1639 \begin_inset Quotes srd
1643 \begin_inset Quotes srd
1651 \begin_inset Quotes srd
1655 \begin_inset Quotes srd
1662 sdccconf_h_dir_separator=
1663 \begin_inset Quotes srd
1675 \begin_inset Quotes srd
1695 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1706 -C' turns on caching, which gives a little bit extra speed.
1707 However if options are changed, it can be necessary to delete the config.cache
1712 \begin_inset LatexCommand \label{sub:Install-paths}
1717 \begin_inset LatexCommand \index{Install paths}
1723 \added_space_top medskip \align center
1725 \begin_inset Tabular
1726 <lyxtabular version="3" rows="5" columns="4">
1728 <column alignment="center" valignment="top" leftline="true" width="0">
1729 <column alignment="center" valignment="top" leftline="true" width="0">
1730 <column alignment="center" valignment="top" leftline="true" width="0">
1731 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1732 <row topline="true" bottomline="true">
1733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1763 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1774 <row topline="true">
1775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1814 <row topline="true">
1815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1830 $DATADIR/ $INCLUDE_DIR_SUFFIX
1833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1838 /usr/local/share/sdcc/include
1841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1854 <row topline="true">
1855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1870 $DATADIR/$LIB_DIR_SUFFIX
1873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1878 /usr/local/share/sdcc/lib
1881 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1894 <row topline="true" bottomline="true">
1895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1918 /usr/local/share/sdcc/doc
1921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1943 *compiler, preprocessor, assembler, and linker
1949 is auto-appended by the compiler, e.g.
1950 small, large, z80, ds390 etc
1953 The install paths can still be changed during `make install` with e.g.:
1956 make install prefix=$(HOME)/local/sdcc
1959 Of course this doesn't change the search paths compiled into the binaries.
1963 Moreover the install path can be changed by defining DESTDIR
1964 \begin_inset LatexCommand \index{DESTDIR}
1971 make install DESTDIR=$(HOME)/sdcc.rpm/
1974 Please note that DESTDIR must have a trailing slash!
1978 \begin_inset LatexCommand \label{sub:Search-Paths}
1983 \begin_inset LatexCommand \index{Search path}
1990 Some search paths or parts of them are determined by configure variables
1995 , see section above).
1996 Further search paths are determined by environment variables during runtime.
1999 The paths searched when running the compiler are as follows (the first catch
2005 Binary files (preprocessor, assembler and linker)
2011 \begin_inset Tabular
2012 <lyxtabular version="3" rows="4" columns="3">
2014 <column alignment="block" valignment="top" leftline="true" width="0in">
2015 <column alignment="block" valignment="top" leftline="true" width="0in">
2016 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2017 <row topline="true" bottomline="true">
2018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2043 <row topline="true">
2044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2073 <row topline="true">
2074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2079 Path of argv[0] (if available)
2082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2099 <row topline="true" bottomline="true">
2100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2141 \begin_inset Tabular
2142 <lyxtabular version="3" rows="6" columns="3">
2144 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2145 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2146 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2147 <row topline="true" bottomline="true">
2148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2173 <row topline="true">
2174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2229 <row topline="true">
2230 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2255 <row topline="true">
2256 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2270 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2293 <row topline="true">
2294 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2375 <row topline="true" bottomline="true">
2376 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2392 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2397 /usr/local/share/sdcc/
2402 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2430 -nostdinc disables the last two search paths.
2440 With the exception of
2441 \begin_inset Quotes sld
2455 \begin_inset Quotes srd
2462 is auto-appended by the compiler (e.g.
2463 small, large, z80, ds390 etc.).
2470 \begin_inset Tabular
2471 <lyxtabular version="3" rows="6" columns="3">
2473 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2474 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2475 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2476 <row topline="true" bottomline="true">
2477 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2485 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2502 <row topline="true">
2503 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2558 <row topline="true">
2559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2571 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2598 <row topline="true">
2599 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2610 $LIB_DIR_SUFFIX/<model>
2613 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2627 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2644 <row topline="true">
2645 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2660 $LIB_DIR_SUFFIX/<model>
2663 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2772 <row topline="true" bottomline="true">
2773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2782 $LIB_DIR_SUFFIX/<model>
2785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2790 /usr/local/share/sdcc/
2797 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2815 Don't delete any of the stray spaces in the table above without checking
2816 the HTML output (last line)!
2832 -nostdlib disables the last two search paths.
2836 \begin_inset LatexCommand \index{Building SDCC}
2843 Building SDCC on Linux
2844 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2853 Download the source package
2855 either from the SDCC CVS repository or from the nightly snapshots
2857 , it will be named something like sdcc
2868 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2877 Bring up a command line terminal, such as xterm.
2882 Unpack the file using a command like:
2885 "tar -xvzf sdcc.src.tar.gz
2890 , this will create a sub-directory called sdcc with all of the sources.
2893 Change directory into the main SDCC directory, for example type:
2910 This configures the package for compilation on your system.
2926 All of the source packages will compile, this can take a while.
2942 This copies the binary executables, the include files, the libraries and
2943 the documentation to the install directories.
2944 Proceed with section
2945 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
2952 Building SDCC on OSX 2.x
2955 Follow the instruction for Linux.
2959 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2960 )) fails to compile SDCC.
2961 Fortunately there's also gcc 2.9.x installed, which works fine.
2962 This compiler can be selected by running 'configure' with:
2965 ./configure CC=gcc2 CXX=g++2
2968 Cross compiling SDCC on Linux for Windows
2971 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2972 See section 'Configure Options'.
2975 Building SDCC on Windows
2978 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2980 They use Unix-sockets, which are not available on Win32.
2983 Building SDCC using Cygwin and Mingw32
2986 For building and installing a Cygwin executable follow the instructions
2992 \begin_inset Quotes sld
2996 \begin_inset Quotes srd
2999 Win32-binary can be built, which will not need the Cygwin-DLL.
3000 For the necessary 'configure' options see section 'configure options' or
3001 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3005 In order to install Cygwin on Windows download setup.exe from
3006 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3012 \begin_inset Quotes sld
3015 default text file type
3016 \begin_inset Quotes srd
3020 \begin_inset Quotes sld
3024 \begin_inset Quotes srd
3027 and download/install at least the following packages.
3028 Some packages are selected by default, others will be automatically selected
3029 because of dependencies with the manually selected packages.
3030 Never deselect these packages!
3039 gcc ; version 3.x is fine, no need to use the old 2.9x
3042 binutils ; selected with gcc
3048 rxvt ; a nice console, which makes life much easier under windoze (see below)
3051 man ; not really needed for building SDCC, but you'll miss it sooner or
3055 less ; not really needed for building SDCC, but you'll miss it sooner or
3059 cvs ; only if you use CVS access
3062 If you want to develop something you'll need:
3065 python ; for the regression tests
3068 gdb ; the gnu debugger, together with the nice GUI
3069 \begin_inset Quotes sld
3073 \begin_inset Quotes srd
3079 openssh ; to access the CF or commit changes
3082 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3083 use autoconf-stable!
3086 rxvt is a nice console with history.
3087 Replace in your cygwin.bat the line
3106 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3109 -bg black -fg white -geometry 100x65 -e bash -
3122 Text selected with the mouse is automatically copied to the clipboard, pasting
3123 works with shift-insert.
3127 The other good tip is to make sure you have no //c/-style paths anywhere,
3128 use /cygdrive/c/ instead.
3129 Using // invokes a network lookup which is very slow.
3131 \begin_inset Quotes sld
3135 \begin_inset Quotes srd
3138 is too long, you can change it with e.g.
3144 SDCC sources use the unix line ending LF.
3145 Life is much easier, if you store the source tree on a drive which is mounted
3147 And use an editor which can handle LF-only line endings.
3148 Make sure not to commit files with windows line endings.
3149 The tabulator spacing
3150 \begin_inset LatexCommand \index{tabulator spacing (8)}
3154 used in the project is 8.
3157 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3162 Download the source package
3164 either from the SDCC CVS repository or from the
3165 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3171 , it will be named something like sdcc
3178 SDCC is distributed with all the projects, workspaces, and files you need
3179 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3180 doesn't build under MSVC).
3181 The workspace name is 'sdcc.dsw'.
3182 Please note that as it is now, all the executables are created in a folder
3186 Once built you need to copy the executables from sdcc
3190 bin before running SDCC.
3195 WARNING: Visual studio is very picky with line terminations; it expects
3196 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3197 If you are getting a message such as "This makefile was not generated by
3198 Developer Studio etc.
3200 \begin_inset Quotes srd
3203 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3204 need to convert the Unix style line endings to DOS style line endings.
3205 To do so you can use the
3206 \begin_inset Quotes sld
3210 \begin_inset Quotes srd
3213 utility freely available on the internet.
3214 Doug Hawkins reported in the sdcc-user list that this works:
3222 SDCC> unix2dos sdcc.dsw
3228 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3232 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3233 flex.exe, and gawk.exe.
3234 One good place to get them is
3235 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3243 Download the file UnxUtils
3244 \begin_inset LatexCommand \index{UnxUtils}
3249 Now you have to install the utilities and setup MSVC so it can locate the
3251 Here there are two alternatives (choose one!):
3258 a) Extract UnxUtils.zip to your C:
3260 hard disk PRESERVING the original paths, otherwise bison won't work.
3261 (If you are using WinZip make certain that 'Use folder names' is selected)
3265 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3266 in 'Show directories for:' select 'Executable files', and in the directories
3267 window add a new path: 'C:
3277 (As a side effect, you get a bunch of Unix utilities that could be useful,
3278 such as diff and patch.)
3285 This one avoids extracting a bunch of files you may not use, but requires
3290 a) Create a directory were to put the tools needed, or use a directory already
3298 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3299 to such directory WITHOUT preserving the original paths.
3300 (If you are using WinZip make certain that 'Use folder names' is not selected)
3304 c) Rename bison.exe to '_bison.exe'.
3308 d) Create a batch file 'bison.bat' in 'C:
3312 ' and add these lines:
3332 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3336 Steps 'c' and 'd' are needed because bison requires by default that the
3337 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3338 '/usr/local/share/' I think.
3339 So it is necessary to tell bison where those files are located if they
3340 are not in such directory.
3341 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3345 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3346 in 'Show directories for:' select 'Executable files', and in the directories
3347 window add a new path: 'c:
3350 Note that you can use any other path instead of 'c:
3352 util', even the path where the Visual C++ tools are, probably: 'C:
3356 Microsoft Visual Studio
3361 So you don't have to execute step 'e' :)
3365 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3366 the executables from sdcc
3370 bin, and you can compile using SDCC.
3373 Building SDCC Using Borland
3376 From the sdcc directory, run the command "make -f Makefile.bcc".
3377 This should regenerate all the .exe files in the bin directory except for
3378 sdcdb.exe (which currently doesn't build under Borland C++).
3381 If you modify any source files and need to rebuild, be aware that the dependenci
3382 es may not be correctly calculated.
3383 The safest option is to delete all .obj files and run the build again.
3384 From a Cygwin BASH prompt, this can easily be done with the command (be
3385 sure you are in the sdcc directory):
3395 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3397 ) -print -exec rm {}
3406 or on Windows NT/2000/XP from the command prompt with the command:
3413 del /s *.obj *.lib *.rul
3416 from the sdcc directory.
3419 Windows Install Using a Binary Package
3420 \begin_inset LatexCommand \label{sub:Windows-Install}
3427 Download the binary package from
3428 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3432 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3433 This should unpack to a group of sub-directories.
3434 An example directory structure after unpacking the mingw32 package is:
3439 bin for the executables, c:
3447 lib for the include and libraries.
3450 Adjust your environment variable PATH to include the location of the bin
3451 directory or start sdcc using the full path.
3454 Building the Documentation
3457 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3458 as changing into the doc directory and typing
3462 \begin_inset Quotes srd
3466 \begin_inset Quotes srd
3473 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3476 \begin_inset LatexCommand \url{www.lyx.org}
3480 as editor this is straightforward.
3481 Prebuilt documentation in html and pdf format is available from
3482 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3489 Reading the Documentation
3492 Currently reading the document in pdf format is recommended, as for unknown
3493 reason the hyperlinks are working there whereas in the html version they
3500 If you should know why please drop us a note
3504 You'll find the pdf version at
3505 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
3511 This documentation is in some aspects different from a commercial documentation:
3515 It tries to document SDCC for several processor architectures in one document
3516 (commercially these probably would be separate documents/products).
3518 \begin_inset LatexCommand \index{Status of documentation}
3522 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3524 Z80, PIC14, PIC16 and HC08.
3527 There are many references pointing away from this documentation.
3528 Don't let this distract you.
3530 was a reference like
3531 \begin_inset LatexCommand \url{www.opencores.org}
3535 together with a statement
3536 \begin_inset Quotes sld
3539 some processors which are targetted by SDCC can be implemented in a
3556 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3561 \begin_inset Quotes srd
3564 we expect you to have a quick look there and come back.
3565 If you read this you are on the right track.
3568 Some sections attribute more space to problems, restrictions and warnings
3569 than to the solution.
3572 The installation section and the section about the debugger is intimidating.
3575 There are still lots of typos and there are more different writing styles
3579 Testing the SDCC Compiler
3580 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3587 The first thing you should do after installing your SDCC compiler is to
3603 \begin_inset LatexCommand \index{version}
3610 at the prompt, and the program should run and tell you the version.
3611 If it doesn't run, or gives a message about not finding sdcc program, then
3612 you need to check over your installation.
3613 Make sure that the sdcc bin directory is in your executable search path
3614 defined by the PATH environment setting (
3619 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3626 Install trouble-shooting for suggestions
3629 Make sure that the sdcc program is in the bin folder, if not perhaps something
3630 did not install correctly.
3638 is commonly installed as described in section
3639 \begin_inset Quotes sld
3642 Install and search paths
3643 \begin_inset Quotes srd
3652 Make sure the compiler works on a very simple example.
3653 Type in the following test.c program using your favorite
3679 Compile this using the following command:
3688 If all goes well, the compiler will generate a test.asm and test.rel file.
3689 Congratulations, you've just compiled your first program with SDCC.
3690 We used the -c option to tell SDCC not to link the generated code, just
3691 to keep things simple for this step.
3699 The next step is to try it with the linker.
3709 If all goes well the compiler will link with the libraries and produce
3710 a test.ihx output file.
3715 (no test.ihx, and the linker generates warnings), then the problem is most
3724 usr/local/share/sdcc/lib directory
3731 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3738 Install trouble-shooting for suggestions).
3746 The final test is to ensure
3754 header files and libraries.
3755 Edit test.c and change it to the following:
3772 strcpy(str1, "testing");
3779 Compile this by typing
3786 This should generate a test.ihx output file, and it should give no warnings
3787 such as not finding the string.h file.
3788 If it cannot find the string.h file, then the problem is that
3792 cannot find the /usr/local/share/sdcc/include directory
3799 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3806 Install trouble-shooting section for suggestions).
3824 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3828 to find exactly where SDCC is looking for the include and lib files.
3831 Install Trouble-shooting
3832 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3837 \begin_inset LatexCommand \index{Install trouble-shooting}
3844 If SDCC does not build correctly
3847 A thing to try is starting from scratch by unpacking the .tgz source package
3848 again in an empty directory.
3856 ./configure 2>&1 | tee configure.log
3870 make 2>&1 | tee make.log
3877 If anything goes wrong, you can review the log files to locate the problem.
3878 Or a relevant part of this can be attached to an email that could be helpful
3879 when requesting help from the mailing list.
3883 \begin_inset Quotes sld
3887 \begin_inset Quotes srd
3894 \begin_inset Quotes sld
3898 \begin_inset Quotes srd
3901 command is a script that analyzes your system and performs some configuration
3902 to ensure the source package compiles on your system.
3903 It will take a few minutes to run, and will compile a few tests to determine
3904 what compiler features are installed.
3908 \begin_inset Quotes sld
3912 \begin_inset Quotes srd
3918 This runs the GNU make tool, which automatically compiles all the source
3919 packages into the final installed binary executables.
3923 \begin_inset Quotes sld
3927 \begin_inset Quotes erd
3933 This will install the compiler, other executables libraries and include
3934 files into the appropriate directories.
3936 \begin_inset LatexCommand \ref{sub:Install-paths}
3942 \begin_inset LatexCommand \ref{sub:Search-Paths}
3947 about install and search paths.
3949 On most systems you will need super-user privileges to do this.
3955 SDCC is not just a compiler, but a collection of tools by various developers.
3956 These include linkers, assemblers, simulators and other components.
3957 Here is a summary of some of the components.
3958 Note that the included simulator and assembler have separate documentation
3959 which you can find in the source package in their respective directories.
3960 As SDCC grows to include support for other processors, other packages from
3961 various developers are included and may have their own sets of documentation.
3965 You might want to look at the files which are installed in <installdir>.
3966 At the time of this writing, we find the following programs for gcc-builds:
3970 In <installdir>/bin:
3973 sdcc - The compiler.
3976 sdcpp - The C preprocessor.
3979 asx8051 - The assembler for 8051 type processors.
3986 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3989 aslink -The linker for 8051 type processors.
3996 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3999 s51 - The ucSim 8051 simulator.
4002 sdcdb - The source debugger.
4005 packihx - A tool to pack (compress) Intel hex files.
4008 In <installdir>/share/sdcc/include
4014 In <installdir>/share/sdcc/lib
4017 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4021 In <installdir>/share/sdcc/doc
4027 As development for other processors proceeds, this list will expand to include
4028 executables to support processors like AVR, PIC, etc.
4034 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4035 the assembler and linkage editor.
4038 sdcpp - The C-Preprocessor
4042 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4046 is a modified version of the GNU preprocessor.
4047 The C preprocessor is used to pull in #include sources, process #ifdef
4048 statements, #defines and so on.
4059 - The Assemblers and Linkage Editors
4062 This is retargettable assembler & linkage editor, it was developed by Alan
4064 John Hartman created the version for 8051, and I (Sandeep) have made some
4065 enhancements and bug fixes for it to work properly with SDCC.
4072 \begin_inset LatexCommand \index{s51}
4076 is a freeware, opensource simulator developed by Daniel Drotos (
4077 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
4082 The simulator is built as part of the build process.
4083 For more information visit Daniel's web site at:
4084 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4089 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4093 sdcdb - Source Level Debugger
4097 \begin_inset LatexCommand \index{sdcdb (debugger)}
4101 is the companion source level debugger.
4102 More about sdcdb in section
4103 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4108 The current version of the debugger uses Daniel's Simulator S51
4109 \begin_inset LatexCommand \index{s51}
4113 , but can be easily changed to use other simulators.
4123 Single Source File Projects
4126 For single source file 8051 projects the process is very simple.
4127 Compile your programs with the following command
4130 "sdcc sourcefile.c".
4134 This will compile, assemble and link your source file.
4135 Output files are as follows:
4139 \begin_inset LatexCommand \index{<file>.asm}
4144 \begin_inset LatexCommand \index{Assembler source}
4148 file created by the compiler
4152 \begin_inset LatexCommand \index{<file>.lst}
4157 \begin_inset LatexCommand \index{Assembler listing}
4161 file created by the Assembler
4165 \begin_inset LatexCommand \index{<file>.rst}
4170 \begin_inset LatexCommand \index{Assembler listing}
4174 file updated with linkedit information, created by linkage editor
4178 \begin_inset LatexCommand \index{<file>.sym}
4183 \begin_inset LatexCommand \index{Symbol listing}
4187 for the sourcefile, created by the assembler
4191 \begin_inset LatexCommand \index{<file>.rel}
4196 \begin_inset LatexCommand \index{<file>.o}
4201 \begin_inset LatexCommand \index{Object file}
4205 created by the assembler, input to Linkage editor
4209 \begin_inset LatexCommand \index{<file>.map}
4214 \begin_inset LatexCommand \index{Memory map}
4218 for the load module, created by the Linker
4222 \begin_inset LatexCommand \index{<file>.mem}
4226 - A file with a summary of the memory usage
4230 \begin_inset LatexCommand \index{<file>.ihx}
4234 - The load module in Intel hex format
4235 \begin_inset LatexCommand \index{Intel hex format}
4239 (you can select the Motorola S19 format
4240 \begin_inset LatexCommand \index{Motorola S19 format}
4255 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4260 If you need another format you might want to use
4267 \begin_inset LatexCommand \index{objdump (tool)}
4278 \begin_inset LatexCommand \index{srecord (tool)}
4283 Both formats are documented in the documentation of srecord
4284 \begin_inset LatexCommand \index{srecord (tool)}
4292 \begin_inset LatexCommand \index{<file>.adb}
4296 - An intermediate file containing debug information needed to create the
4308 \begin_inset LatexCommand \index{-\/-debug}
4316 \begin_inset LatexCommand \index{<file>.cdb}
4320 - An optional file (with -
4330 -debug) containing debug information.
4331 The format is documented in cdbfileformat.pdf.
4336 \begin_inset LatexCommand \index{<file> (no extension)}
4340 An optional AOMF or AOMF51
4341 \begin_inset LatexCommand \index{AOMF, AOMF51}
4345 file containing debug information (generated with option -
4372 ormat is commonly used by third party tools (debuggers
4373 \begin_inset LatexCommand \index{Debugger}
4377 , simulators, emulators)
4381 \begin_inset LatexCommand \index{<file>.dump*}
4385 - Dump file to debug the compiler it self (generated with option -
4395 -dumpall) (see section
4396 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4402 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4408 \begin_inset Quotes sld
4411 Anatomy of the compiler
4412 \begin_inset Quotes srd
4418 Projects with Multiple Source Files
4421 SDCC can compile only ONE file at a time.
4422 Let us for example assume that you have a project containing the following
4427 foo1.c (contains some functions)
4429 foo2.c (contains some more functions)
4431 foomain.c (contains more functions and the function main)
4439 The first two files will need to be compiled separately with the commands:
4471 Then compile the source file containing the
4476 \begin_inset LatexCommand \index{Linker}
4480 the files together with the following command:
4488 foomain.c\SpecialChar ~
4489 foo1.rel\SpecialChar ~
4494 \begin_inset LatexCommand \index{<file>.rel}
4506 can be separately compiled as well:
4517 sdcc foomain.rel foo1.rel foo2.rel
4524 The file containing the
4539 file specified in the command line, since the linkage editor processes
4540 file in the order they are presented to it.
4541 The linker is invoked from SDCC using a script file with extension .lnk
4542 \begin_inset LatexCommand \index{<file>.lnk}
4547 You can view this file to troubleshoot linking problems such as those arising
4548 from missing libraries.
4551 Projects with Additional Libraries
4552 \begin_inset LatexCommand \index{Libraries}
4559 Some reusable routines may be compiled into a library, see the documentation
4560 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4564 \begin_inset LatexCommand \index{<file>.lib}
4571 Libraries created in this manner can be included in the command line.
4572 Make sure you include the -L <library-path> option to tell the linker where
4573 to look for these files if they are not in the current directory.
4574 Here is an example, assuming you have the source file
4586 (if that is not the same as your current project):
4593 sdcc foomain.c foolib.lib -L mylib
4604 must be an absolute path name.
4608 The most efficient way to use libraries is to keep separate modules in separate
4610 The lib file now should name all the modules.rel
4611 \begin_inset LatexCommand \index{<file>.rel}
4616 For an example see the standard library file
4620 in the directory <installdir>/share/lib/small.
4623 Using sdcclib to Create and Manage Libraries
4624 \begin_inset LatexCommand \index{sdcclib}
4631 Alternatively, instead of having a .rel file for each entry on the library
4632 file as described in the preceding section, sdcclib can be used to embed
4633 all the modules belonging to such library in the library file itself.
4634 This results in a larger library file, but it greatly reduces the number
4635 of disk files accessed by the linker.
4636 Additionally, the packed library file contains an index of all include
4637 modules and symbols that significantly speeds up the linking process.
4638 To display a list of options supported by sdcclib type:
4647 \begin_inset LatexCommand \index{sdcclib}
4658 To create a new library file, start by compiling all the required modules.
4696 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4698 The next step is to add the .rel files to the library file:
4706 sdcclib libint.lib _divsint.rel
4709 \begin_inset LatexCommand \index{sdcclib}
4719 sdcclib libint.lib _divuint.rel
4725 sdcclib libint.lib _modsint.rel
4731 sdcclib libint.lib _moduint.rel
4737 sdcclib libint.lib _mulint.rel
4744 If the file already exists in the library, it will be replaced.
4745 To see what modules and symbols are included in the library, options -s
4746 and -m are available.
4754 sdcclib -s libint.lib
4757 \begin_inset LatexCommand \index{sdcclib}
4867 If the source files are compiled using -
4878 \begin_inset LatexCommand \index{-\/-debug}
4882 , the corresponding debug information file .adb will be include in the library
4884 The library files created with sdcclib are plain text files, so they can
4885 be viewed with a text editor.
4886 It is not recomended to modify a library file created with sdcclib using
4887 a text editor, as there are file indexes numbers located accross the file
4888 used by the linker to quickly locate the required module to link.
4889 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
4890 it can be safely deleted, since all the information required for linking
4891 is embedded in the library file itself.
4892 Library files created using sdcclib are used as described in the preceding
4896 Command Line Options
4897 \begin_inset LatexCommand \index{Command Line Options}
4904 Processor Selection Options
4905 \begin_inset LatexCommand \index{Options processor selection}
4910 \begin_inset LatexCommand \index{Processor selection options}
4916 \labelwidthstring 00.00.0000
4921 \begin_inset LatexCommand \index{-mmcs51}
4927 Generate code for the Intel MCS51
4928 \begin_inset LatexCommand \index{MCS51}
4932 family of processors.
4933 This is the default processor target.
4935 \labelwidthstring 00.00.0000
4940 \begin_inset LatexCommand \index{-mds390}
4946 Generate code for the Dallas DS80C390
4947 \begin_inset LatexCommand \index{DS80C390}
4953 \labelwidthstring 00.00.0000
4958 \begin_inset LatexCommand \index{-mds400}
4964 Generate code for the Dallas DS80C400
4965 \begin_inset LatexCommand \index{DS80C400}
4971 \labelwidthstring 00.00.0000
4976 \begin_inset LatexCommand \index{-mhc08}
4982 Generate code for the Motorola HC08
4983 \begin_inset LatexCommand \index{HC08}
4987 family of processors (added Oct 2003).
4989 \labelwidthstring 00.00.0000
4994 \begin_inset LatexCommand \index{-mz80}
5000 Generate code for the Zilog Z80
5001 \begin_inset LatexCommand \index{Z80}
5005 family of processors.
5007 \labelwidthstring 00.00.0000
5012 \begin_inset LatexCommand \index{-mgbz80}
5018 Generate code for the GameBoy Z80
5019 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5023 processor (Not actively maintained).
5025 \labelwidthstring 00.00.0000
5030 \begin_inset LatexCommand \index{-mavr}
5036 Generate code for the Atmel AVR
5037 \begin_inset LatexCommand \index{AVR}
5041 processor (In development, not complete).
5042 AVR users should probably have a look at avr-gcc
5043 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
5048 \begin_inset LatexCommand \url{http://winavr.sourceforge.net}
5055 I think it is fair to direct users there for now.
5056 Open source is also about avoiding unnecessary work .
5057 But I didn't find the 'official' link.
5059 \labelwidthstring 00.00.0000
5064 \begin_inset LatexCommand \index{-mpic14}
5070 Generate code for the Microchip PIC 14
5071 \begin_inset LatexCommand \index{PIC14}
5075 -bit processors (p16f84 and variants.
5076 In development, not complete).
5079 p16f627 p16f628 p16f84 p16f873 p16f877?
5081 \labelwidthstring 00.00.0000
5086 \begin_inset LatexCommand \index{-mpic16}
5092 Generate code for the Microchip PIC 16
5093 \begin_inset LatexCommand \index{PIC16}
5097 -bit processors (p18f452 and variants.
5098 In development, not complete).
5100 \labelwidthstring 00.00.0000
5106 Generate code for the Toshiba TLCS-900H
5107 \begin_inset LatexCommand \index{TLCS-900H}
5111 processor (Not maintained, not complete).
5113 \labelwidthstring 00.00.0000
5118 \begin_inset LatexCommand \index{-mxa51}
5124 Generate code for the Phillips XA51
5125 \begin_inset LatexCommand \index{XA51}
5129 processor (Not maintained, not complete).
5132 Preprocessor Options
5133 \begin_inset LatexCommand \index{Options preprocessor}
5138 \begin_inset LatexCommand \index{Preprocessor options}
5143 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5149 \labelwidthstring 00.00.0000
5154 \begin_inset LatexCommand \index{-I<path>}
5160 The additional location where the pre processor will look for <..h> or
5161 \begin_inset Quotes eld
5165 \begin_inset Quotes erd
5170 \labelwidthstring 00.00.0000
5175 \begin_inset LatexCommand \index{-D<macro[=value]>}
5181 Command line definition of macros.
5182 Passed to the preprocessor.
5184 \labelwidthstring 00.00.0000
5189 \begin_inset LatexCommand \index{-M}
5195 Tell the preprocessor to output a rule suitable for make describing the
5196 dependencies of each object file.
5197 For each source file, the preprocessor outputs one make-rule whose target
5198 is the object file name for that source file and whose dependencies are
5199 all the files `#include'd in it.
5200 This rule may be a single line or may be continued with `
5202 '-newline if it is long.
5203 The list of rules is printed on standard output instead of the preprocessed
5206 \begin_inset LatexCommand \index{-E}
5212 \labelwidthstring 00.00.0000
5217 \begin_inset LatexCommand \index{-C}
5223 Tell the preprocessor not to discard comments.
5224 Used with the `-E' option.
5226 \labelwidthstring 00.00.0000
5231 \begin_inset LatexCommand \index{-MM}
5242 Like `-M' but the output mentions only the user header files included with
5244 \begin_inset Quotes eld
5248 System header files included with `#include <file>' are omitted.
5250 \labelwidthstring 00.00.0000
5255 \begin_inset LatexCommand \index{-Aquestion(answer)}
5261 Assert the answer answer for question, in case it is tested with a preprocessor
5262 conditional such as `#if #question(answer)'.
5263 `-A-' disables the standard assertions that normally describe the target
5266 \labelwidthstring 00.00.0000
5271 \begin_inset LatexCommand \index{-Umacro}
5277 Undefine macro macro.
5278 `-U' options are evaluated after all `-D' options, but before any `-include'
5279 and `-imacros' options.
5281 \labelwidthstring 00.00.0000
5286 \begin_inset LatexCommand \index{-dM}
5292 Tell the preprocessor to output only a list of the macro definitions that
5293 are in effect at the end of preprocessing.
5294 Used with the `-E' option.
5296 \labelwidthstring 00.00.0000
5301 \begin_inset LatexCommand \index{-dD}
5307 Tell the preprocessor to pass all macro definitions into the output, in
5308 their proper sequence in the rest of the output.
5310 \labelwidthstring 00.00.0000
5315 \begin_inset LatexCommand \index{-dN}
5326 Like `-dD' except that the macro arguments and contents are omitted.
5327 Only `#define name' is included in the output.
5329 \labelwidthstring 00.00.0000
5334 preprocessorOption[,preprocessorOption]
5337 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5342 Pass the preprocessorOption to the preprocessor
5347 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5352 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5353 Collection (gcc), if you need more dedicated options please refer to the
5355 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5363 \begin_inset LatexCommand \index{Options linker}
5368 \begin_inset LatexCommand \index{Linker options}
5374 \labelwidthstring 00.00.0000
5394 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5399 \begin_inset LatexCommand \index{-L -\/-lib-path}
5406 <absolute path to additional libraries> This option is passed to the linkage
5407 editor's additional libraries
5408 \begin_inset LatexCommand \index{Libraries}
5413 The path name must be absolute.
5414 Additional library files may be specified in the command line.
5415 See section Compiling programs for more details.
5417 \labelwidthstring 00.00.0000
5434 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5439 <Value> The start location of the external ram
5440 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5444 , default value is 0.
5445 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5455 -xram-loc 0x8000 or -
5467 \labelwidthstring 00.00.0000
5484 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5489 <Value> The start location of the code
5490 \begin_inset LatexCommand \index{code}
5494 segment, default value 0.
5495 Note when this option is used the interrupt vector table is also relocated
5496 to the given address.
5497 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5507 -code-loc 0x8000 or -
5519 \labelwidthstring 00.00.0000
5536 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5541 <Value> By default the stack
5542 \begin_inset LatexCommand \index{stack}
5546 is placed after the data segment.
5547 Using this option the stack can be placed anywhere in the internal memory
5549 The value entered can be in Hexadecimal or Decimal format, e.g.
5560 -stack-loc 0x20 or -
5571 Since the sp register is incremented before a push or call, the initial
5572 sp will be set to one byte prior the provided value.
5573 The provided value should not overlap any other memory areas such as used
5574 register banks or the data segment and with enough space for the current
5577 \labelwidthstring 00.00.0000
5594 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5599 <Value> The start location of the internal ram data
5600 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5605 The value entered can be in Hexadecimal or Decimal format, eg.
5627 (By default, the start location of the internal ram data segment is set
5628 as low as possible in memory, taking into account the used register banks
5629 and the bit segment at address 0x20.
5630 For example if register banks 0 and 1 are used without bit variables, the
5631 data segment will be set, if -
5641 -data-loc is not used, to location 0x10.)
5643 \labelwidthstring 00.00.0000
5660 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5665 <Value> The start location of the indirectly addressable internal ram
5666 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5670 of the 8051, default value is 0x80.
5671 The value entered can be in Hexadecimal or Decimal format, eg.
5682 -idata-loc 0x88 or -
5694 \labelwidthstring 00.00.0000
5711 <Value> The start location of the bit
5712 \begin_inset LatexCommand \index{bit}
5716 addressable internal ram of the 8051.
5722 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5725 \labelwidthstring 00.00.0000
5740 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5749 The linker output (final object code) is in Intel Hex format.
5750 \begin_inset LatexCommand \index{Intel hex format}
5754 This is the default option.
5755 The format itself is documented in the documentation of srecord
5756 \begin_inset LatexCommand \index{srecord (tool)}
5762 \labelwidthstring 00.00.0000
5777 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5786 The linker output (final object code) is in Motorola S19 format
5787 \begin_inset LatexCommand \index{Motorola S19 format}
5792 The format itself is documented in the documentation of srecord.
5794 \labelwidthstring 00.00.0000
5799 linkOption[,linkOption]
5802 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5807 Pass the linkOption to the linker.
5808 See file sdcc/as/doc/asxhtm.html for more on linker options.
5812 \begin_inset LatexCommand \index{Options MCS51}
5817 \begin_inset LatexCommand \index{MCS51 options}
5823 \labelwidthstring 00.00.0000
5838 \begin_inset LatexCommand \index{-\/-model-small}
5849 Generate code for Small Model programs, see section Memory Models for more
5851 This is the default model.
5853 \labelwidthstring 00.00.0000
5868 \begin_inset LatexCommand \index{-\/-model-large}
5874 Generate code for Large model programs, see section Memory Models for more
5876 If this option is used all source files in the project have to be compiled
5879 \labelwidthstring 00.00.0000
5894 \begin_inset LatexCommand \index{-\/-xstack}
5900 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5901 variables and passing parameters.
5903 \begin_inset LatexCommand \ref{sub:External-Stack}
5908 External Stack for more details.
5910 \labelwidthstring 00.00.0000
5928 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
5932 Causes the linker to check if the internal ram usage is within limits of
5935 \labelwidthstring 00.00.0000
5953 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
5957 Causes the linker to check if the external ram usage is within limits of
5960 \labelwidthstring 00.00.0000
5978 \begin_inset LatexCommand \index{-\/-code-size <Value>}
5982 Causes the linker to check if the code memory usage is within limits of
5985 \labelwidthstring 00.00.0000
6003 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6007 Causes the linker to check if there is at minimum <Value> bytes for stack.
6009 \labelwidthstring 00.00.0000
6027 \begin_inset LatexCommand \index{-\/-pack-iram}
6031 Causes the linker use unused register banks for data variables or stack.
6034 DS390 / DS400 Options
6035 \begin_inset LatexCommand \index{Options DS390}
6040 \begin_inset LatexCommand \index{DS390 options}
6046 \labelwidthstring 00.00.0000
6063 \begin_inset LatexCommand \index{-\/-model-flat24}
6073 Generate 24-bit flat mode code.
6074 This is the one and only that the ds390 code generator supports right now
6075 and is default when using
6080 See section Memory Models for more details.
6082 \labelwidthstring 00.00.0000
6097 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6103 disable interrupts during ESP:SP updates.
6105 \labelwidthstring 00.00.0000
6122 \begin_inset LatexCommand \index{-\/-stack-10bit}
6126 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6127 This is the one and only that the ds390 code generator supports right now
6128 and is default when using
6133 In this mode, the stack is located in the lower 1K of the internal RAM,
6134 which is mapped to 0x400000.
6135 Note that the support is incomplete, since it still uses a single byte
6136 as the stack pointer.
6137 This means that only the lower 256 bytes of the potential 1K stack space
6138 will actually be used.
6139 However, this does allow you to reclaim the precious 256 bytes of low RAM
6140 for use for the DATA and IDATA segments.
6141 The compiler will not generate any code to put the processor into 10 bit
6143 It is important to ensure that the processor is in this mode before calling
6144 any re-entrant functions compiled with this option.
6145 In principle, this should work with the
6158 \begin_inset LatexCommand \index{-\/-stack-auto}
6164 option, but that has not been tested.
6165 It is incompatible with the
6178 \begin_inset LatexCommand \index{-\/-xstack}
6185 It also only makes sense if the processor is in 24 bit contiguous addressing
6198 -model-flat24 option
6202 \labelwidthstring 00.00.0000
6217 \begin_inset LatexCommand \index{-\/-stack-probe}
6223 insert call to function __stack_probe at each function prologue.
6225 \labelwidthstring 00.00.0000
6240 \begin_inset LatexCommand \index{-\/-tini-libid}
6246 <nnnn> LibraryID used in -mTININative.
6249 \labelwidthstring 00.00.0000
6264 \begin_inset LatexCommand \index{-\/-use-accelerator}
6270 generate code for DS390 Arithmetic Accelerator.
6275 \begin_inset LatexCommand \index{Options Z80}
6280 \begin_inset LatexCommand \index{Z80 options}
6286 \labelwidthstring 00.00.0000
6303 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6313 Force a called function to always save BC.
6315 \labelwidthstring 00.00.0000
6332 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6336 When linking, skip the standard crt0.o object file.
6337 You must provide your own crt0.o for your system when linking.
6341 Optimization Options
6342 \begin_inset LatexCommand \index{Options optimization}
6347 \begin_inset LatexCommand \index{Optimization options}
6353 \labelwidthstring 00.00.0000
6368 \begin_inset LatexCommand \index{-\/-nogcse}
6374 Will not do global subexpression elimination, this option may be used when
6375 the compiler creates undesirably large stack/data spaces to store compiler
6377 A warning message will be generated when this happens and the compiler
6378 will indicate the number of extra bytes it allocated.
6379 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6381 \begin_inset LatexCommand \index{\#pragma nogcse}
6385 can be used to turn off global subexpression elimination
6386 \begin_inset LatexCommand \index{Subexpression elimination}
6390 for a given function only.
6392 \labelwidthstring 00.00.0000
6407 \begin_inset LatexCommand \index{-\/-noinvariant}
6413 Will not do loop invariant optimizations, this may be turned off for reasons
6414 explained for the previous option.
6415 For more details of loop optimizations performed see Loop Invariants in
6417 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6422 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6424 \begin_inset LatexCommand \index{\#pragma noinvariant}
6428 can be used to turn off invariant optimizations for a given function only.
6430 \labelwidthstring 00.00.0000
6445 \begin_inset LatexCommand \index{-\/-noinduction}
6451 Will not do loop induction optimizations, see section strength reduction
6453 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6455 \begin_inset LatexCommand \index{\#pragma noinduction}
6459 can be used to turn off induction optimizations for a given function only.
6461 \labelwidthstring 00.00.0000
6476 \begin_inset LatexCommand \index{-\/-nojtbound}
6487 Will not generate boundary condition check when switch statements
6488 \begin_inset LatexCommand \index{switch statement}
6492 are implemented using jump-tables.
6494 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6499 Switch Statements for more details.
6500 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6502 \begin_inset LatexCommand \index{\#pragma nojtbound}
6506 can be used to turn off boundary checking for jump tables for a given function
6509 \labelwidthstring 00.00.0000
6524 \begin_inset LatexCommand \index{-\/-noloopreverse}
6533 Will not do loop reversal
6534 \begin_inset LatexCommand \index{Loop reversing}
6540 \labelwidthstring 00.00.0000
6557 \begin_inset LatexCommand \index{-\/-nolabelopt }
6561 Will not optimize labels (makes the dumpfiles more readable).
6563 \labelwidthstring 00.00.0000
6578 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6584 Will not memcpy initialized data from code space into xdata space.
6585 This saves a few bytes in code space if you don't have initialized data.
6587 \labelwidthstring 00.00.0000
6602 \begin_inset LatexCommand \index{-\/-nooverlay}
6608 The compiler will not overlay parameters and local variables of any function,
6609 see section Parameters and local variables for more details.
6611 \labelwidthstring 00.00.0000
6626 \begin_inset LatexCommand \index{-\/-no-peep}
6632 Disable peep-hole optimization.
6634 \labelwidthstring 00.00.0000
6651 \begin_inset LatexCommand \index{-\/-peep-file}
6656 <filename> This option can be used to use additional rules to be used by
6657 the peep hole optimizer.
6659 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6664 Peep Hole optimizations for details on how to write these rules.
6666 \labelwidthstring 00.00.0000
6681 \begin_inset LatexCommand \index{-\/-peep-asm}
6687 Pass the inline assembler code through the peep hole optimizer.
6688 This can cause unexpected changes to inline assembler code, please go through
6689 the peephole optimizer
6690 \begin_inset LatexCommand \index{Peephole optimizer}
6694 rules defined in the source file tree '<target>/peeph.def' before using
6699 \begin_inset LatexCommand \index{Options other}
6705 \labelwidthstring 00.00.0000
6721 \begin_inset LatexCommand \index{-\/-compile-only}
6726 \begin_inset LatexCommand \index{-c -\/-compile-only}
6732 will compile and assemble the source, but will not call the linkage editor.
6734 \labelwidthstring 00.00.0000
6753 \begin_inset LatexCommand \index{-\/-c1mode}
6759 reads the preprocessed source from standard input and compiles it.
6760 The file name for the assembler output must be specified using the -o option.
6762 \labelwidthstring 00.00.0000
6767 \begin_inset LatexCommand \index{-E}
6773 Run only the C preprocessor.
6774 Preprocess all the C source files specified and output the results to standard
6777 \labelwidthstring 00.00.0000
6783 \begin_inset LatexCommand \index{-o <path/file>}
6789 The output path resp.
6790 file where everything will be placed.
6791 If the parameter is a path, it must have a trailing slash (or backslash
6792 for the Windows binaries) to be recognized as a path.
6795 \labelwidthstring 00.00.0000
6810 \begin_inset LatexCommand \index{-\/-stack-auto}
6821 All functions in the source file will be compiled as
6826 \begin_inset LatexCommand \index{reentrant}
6831 the parameters and local variables will be allocated on the stack
6832 \begin_inset LatexCommand \index{stack}
6838 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
6842 Parameters and Local Variables for more details.
6843 If this option is used all source files in the project should be compiled
6847 \labelwidthstring 00.00.0000
6862 \begin_inset LatexCommand \index{-\/-callee-saves}
6866 function1[,function2][,function3]....
6869 The compiler by default uses a caller saves convention for register saving
6870 across function calls, however this can cause unnecessary register pushing
6871 & popping when calling small functions from larger functions.
6872 This option can be used to switch the register saving convention for the
6873 function names specified.
6874 The compiler will not save registers when calling these functions, no extra
6875 code will be generated at the entry & exit (function prologue
6878 \begin_inset LatexCommand \index{function prologue}
6887 \begin_inset LatexCommand \index{function epilogue}
6893 ) for these functions to save & restore the registers used by these functions,
6894 this can SUBSTANTIALLY reduce code & improve run time performance of the
6896 In the future the compiler (with inter procedural analysis) will be able
6897 to determine the appropriate scheme to use for each function call.
6898 DO NOT use this option for built-in functions such as _mulint..., if this
6899 option is used for a library function the appropriate library function
6900 needs to be recompiled with the same option.
6901 If the project consists of multiple source files then all the source file
6902 should be compiled with the same -
6912 -callee-saves option string.
6913 Also see #pragma\SpecialChar ~
6915 \begin_inset LatexCommand \index{\#pragma callee\_saves}
6921 \labelwidthstring 00.00.0000
6936 \begin_inset LatexCommand \index{-\/-debug}
6945 When this option is used the compiler will generate debug information.
6946 The debug information collected in a file with .cdb extension can be used
6948 For more information see documentation for SDCDB.
6949 Another file with no extension contains debug information in AOMF or AOMF51
6950 \begin_inset LatexCommand \index{AOMF, AOMF51}
6954 format which is commonly used by third party tools.
6956 \labelwidthstring 00.00.0000
6961 \begin_inset LatexCommand \index{-S}
6972 Stop after the stage of compilation proper; do not assemble.
6973 The output is an assembler code file for the input file specified.
6975 \labelwidthstring 00.00.0000
6990 \begin_inset LatexCommand \index{-\/-int-long-reent}
6996 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
6997 Note by default these libraries are compiled as non-reentrant.
6998 See section Installation for more details.
7000 \labelwidthstring 00.00.0000
7015 \begin_inset LatexCommand \index{-\/-cyclomatic}
7024 This option will cause the compiler to generate an information message for
7025 each function in the source file.
7026 The message contains some
7030 information about the function.
7031 The number of edges and nodes the compiler detected in the control flow
7032 graph of the function, and most importantly the
7034 cyclomatic complexity
7035 \begin_inset LatexCommand \index{Cyclomatic complexity}
7041 see section on Cyclomatic Complexity for more details.
7043 \labelwidthstring 00.00.0000
7058 \begin_inset LatexCommand \index{-\/-float-reent}
7064 Floating point library is compiled as reentrant
7065 \begin_inset LatexCommand \index{reentrant}
7070 See section Installation for more details.
7072 \labelwidthstring 00.00.0000
7087 \begin_inset LatexCommand \index{-\/-main-return}
7093 This option can be used if the code generated is called by a monitor program
7094 or if the main routine includes an endless loop.
7095 This option might result in slightly smaller code and save two bytes of
7097 The return from the 'main'
7098 \begin_inset LatexCommand \index{main return}
7102 function will return to the function calling main.
7103 The default setting is to lock up i.e.
7110 \labelwidthstring 00.00.0000
7125 \begin_inset LatexCommand \index{-\/-nostdincl}
7131 This will prevent the compiler from passing on the default include path
7132 to the preprocessor.
7134 \labelwidthstring 00.00.0000
7149 \begin_inset LatexCommand \index{-\/-nostdlib}
7155 This will prevent the compiler from passing on the default library
7156 \begin_inset LatexCommand \index{Libraries}
7162 \labelwidthstring 00.00.0000
7177 \begin_inset LatexCommand \index{-\/-verbose}
7183 Shows the various actions the compiler is performing.
7185 \labelwidthstring 00.00.0000
7190 \begin_inset LatexCommand \index{-V}
7196 Shows the actual commands the compiler is executing.
7198 \labelwidthstring 00.00.0000
7213 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7219 Hides your ugly and inefficient c-code from the asm file, so you can always
7220 blame the compiler :)
7222 \labelwidthstring 00.00.0000
7237 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7243 Will not include peep-hole comments in the generated files.
7245 \labelwidthstring 00.00.0000
7260 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7266 Include i-codes in the asm file.
7267 Sounds like noise but is most helpful for debugging the compiler itself.
7269 \labelwidthstring 00.00.0000
7284 \begin_inset LatexCommand \index{-\/-less-pedantic}
7290 Disable some of the more pedantic warnings
7291 \begin_inset LatexCommand \index{Warnings}
7295 (jwk burps: please be more specific here, please!).
7296 If you want rather more than less warnings you should consider using a
7297 separate tool dedicated to syntax checking like splint
7298 \begin_inset LatexCommand \url{www.splint.org}
7304 \labelwidthstring 00.00.0000
7319 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7325 Display the directories in the compiler's search path
7327 \labelwidthstring 00.00.0000
7342 \begin_inset LatexCommand \index{-\/-vc}
7348 Display errors and warnings using MSVC style, so you can use SDCC with
7351 \labelwidthstring 00.00.0000
7366 \begin_inset LatexCommand \index{-\/-use-stdout}
7372 Send errors and warnings to stdout instead of stderr.
7374 \labelwidthstring 00.00.0000
7379 asmOption[,asmOption]
7382 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7387 Pass the asmOption to the assembler
7388 \begin_inset LatexCommand \index{Options assembler}
7393 \begin_inset LatexCommand \index{Assembler options}
7398 See file sdcc/as/doc/asxhtm.html for assembler options.
7401 Intermediate Dump Options
7402 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7407 \begin_inset LatexCommand \index{Options intermediate dump}
7412 \begin_inset LatexCommand \index{Intermediate dump options}
7419 The following options are provided for the purpose of retargetting and debugging
7421 These provided a means to dump the intermediate code (iCode
7422 \begin_inset LatexCommand \index{iCode}
7426 ) generated by the compiler in human readable form at various stages of
7427 the compilation process.
7428 More on iCodes see chapter
7429 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7434 \begin_inset Quotes srd
7437 The anatomy of the compiler
7438 \begin_inset Quotes srd
7443 \labelwidthstring 00.00.0000
7458 \begin_inset LatexCommand \index{-\/-dumpraw}
7464 This option will cause the compiler to dump the intermediate code into
7467 <source filename>.dumpraw
7469 just after the intermediate code has been generated for a function, i.e.
7470 before any optimizations are done.
7472 \begin_inset LatexCommand \index{Basic blocks}
7476 at this stage ordered in the depth first number, so they may not be in
7477 sequence of execution.
7479 \labelwidthstring 00.00.0000
7494 \begin_inset LatexCommand \index{-\/-dumpgcse}
7500 Will create a dump of iCode's, after global subexpression elimination
7501 \begin_inset LatexCommand \index{Global subexpression elimination}
7507 <source filename>.dumpgcse.
7509 \labelwidthstring 00.00.0000
7524 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7530 Will create a dump of iCode's, after deadcode elimination
7531 \begin_inset LatexCommand \index{Dead-code elimination}
7537 <source filename>.dumpdeadcode.
7539 \labelwidthstring 00.00.0000
7554 \begin_inset LatexCommand \index{-\/-dumploop}
7563 Will create a dump of iCode's, after loop optimizations
7564 \begin_inset LatexCommand \index{Loop optimization}
7570 <source filename>.dumploop.
7572 \labelwidthstring 00.00.0000
7587 \begin_inset LatexCommand \index{-\/-dumprange}
7596 Will create a dump of iCode's, after live range analysis
7597 \begin_inset LatexCommand \index{Live range analysis}
7603 <source filename>.dumprange.
7605 \labelwidthstring 00.00.0000
7620 \begin_inset LatexCommand \index{-\/-dumlrange}
7626 Will dump the life ranges
7627 \begin_inset LatexCommand \index{Live range analysis}
7633 \labelwidthstring 00.00.0000
7648 \begin_inset LatexCommand \index{-\/-dumpregassign}
7657 Will create a dump of iCode's, after register assignment
7658 \begin_inset LatexCommand \index{Register assignment}
7664 <source filename>.dumprassgn.
7666 \labelwidthstring 00.00.0000
7681 \begin_inset LatexCommand \index{-\/-dumplrange}
7687 Will create a dump of the live ranges of iTemp's
7689 \labelwidthstring 00.00.0000
7704 \begin_inset LatexCommand \index{-\/-dumpall}
7715 Will cause all the above mentioned dumps to be created.
7718 Redirecting output on Windows Shells
7721 By default SDCC writes it's error messages to
7722 \begin_inset Quotes sld
7726 \begin_inset Quotes srd
7730 To force all messages to
7731 \begin_inset Quotes sld
7735 \begin_inset Quotes srd
7759 \begin_inset LatexCommand \index{-\/-use-stdout}
7764 Additionally, if you happen to have visual studio installed in your windows
7765 machine, you can use it to compile your sources using a custom build and
7781 \begin_inset LatexCommand \index{-\/-vc}
7786 Something like this should work:
7830 -model-large -c $(InputPath)
7833 Environment variables
7834 \begin_inset LatexCommand \index{Environment variables}
7841 SDCC recognizes the following environment variables:
7843 \labelwidthstring 00.00.0000
7848 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7854 SDCC installs a signal handler
7855 \begin_inset LatexCommand \index{signal handler}
7859 to be able to delete temporary files after an user break (^C) or an exception.
7860 If this environment variable is set, SDCC won't install the signal handler
7861 in order to be able to debug SDCC.
7863 \labelwidthstring 00.00.0000
7870 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7876 Path, where temporary files will be created.
7877 The order of the variables is the search order.
7878 In a standard *nix environment these variables are not set, and there's
7879 no need to set them.
7880 On Windows it's recommended to set one of them.
7882 \labelwidthstring 00.00.0000
7887 \begin_inset LatexCommand \index{SDCC\_HOME}
7894 \begin_inset LatexCommand \ref{sub:Install-paths}
7900 \begin_inset Quotes sld
7904 \begin_inset Quotes srd
7909 \labelwidthstring 00.00.0000
7914 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
7921 \begin_inset LatexCommand \ref{sub:Search-Paths}
7927 \begin_inset Quotes sld
7931 \begin_inset Quotes srd
7936 \labelwidthstring 00.00.0000
7941 \begin_inset LatexCommand \index{SDCC\_LIB}
7948 \begin_inset LatexCommand \ref{sub:Search-Paths}
7954 \begin_inset Quotes sld
7958 \begin_inset Quotes srd
7964 There are some more environment variables recognized by SDCC, but these
7965 are solely used for debugging purposes.
7966 They can change or disappear very quickly, and will never be documented.
7969 Storage Class Language Extensions
7972 MCS51/DS390 Storage Class
7973 \begin_inset LatexCommand \index{Storage class}
7980 In addition to the ANSI storage classes SDCC allows the following MCS51
7981 specific storage classes:
7982 \layout Subsubsection
7985 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
7990 \begin_inset LatexCommand \index{near (storage class)}
8001 storage class for the Small Memory model (
8009 can be used synonymously).
8010 Variables declared with this storage class will be allocated in the directly
8011 addressable portion of the internal RAM of a 8051, e.g.:
8016 data unsigned char test_data;
8019 Writing 0x01 to this variable generates the assembly code:
8024 75*00 01\SpecialChar ~
8030 \layout Subsubsection
8033 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8038 \begin_inset LatexCommand \index{far (storage class)}
8045 Variables declared with this storage class will be placed in the external
8051 storage class for the Large Memory model, e.g.:
8056 xdata unsigned char test_xdata;
8059 Writing 0x01 to this variable generates the assembly code:
8064 90s00r00\SpecialChar ~
8093 \layout Subsubsection
8096 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8103 Variables declared with this storage class will be allocated into the indirectly
8104 addressable portion of the internal ram of a 8051, e.g.:
8109 idata unsigned char test_idata;
8112 Writing 0x01 to this variable generates the assembly code:
8141 Please note, the first 128 byte of idata physically access the same RAM
8143 The original 8051 had 128 byte idata memory, nowadays most devices have
8144 256 byte idata memory.
8146 \begin_inset LatexCommand \index{stack}
8150 is located in idata memory.
8151 \layout Subsubsection
8154 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8161 Paged xdata access is currently not as straightforward as using the other
8162 addressing modes of a 8051.
8163 The following example writes 0x01 to the address pointed to.
8164 Please note, pdata access physically accesses xdata memory.
8165 The high byte of the address is determined by port P2
8166 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8170 (or in case of some 8051 variants by a separate Special Function Register,
8172 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8181 pdata unsigned char *test_pdata_ptr;
8193 test_pdata_ptr = (pdata *)0xfe;
8199 *test_pdata_ptr = 1;
8204 Generates the assembly code:
8209 75*01 FE\SpecialChar ~
8213 _test_pdata_ptr,#0xFE
8245 Be extremely carefull if you use pdata together with the -
8256 \begin_inset LatexCommand \index{-\/-xstack}
8261 \layout Subsubsection
8264 \begin_inset LatexCommand \index{code}
8271 'Variables' declared with this storage class will be placed in the code
8277 code unsigned char test_code;
8280 Read access to this variable generates the assembly code:
8285 90s00r6F\SpecialChar ~
8288 mov dptr,#_test_code
8317 indexed arrays of characters in code memory can be accessed efficiently:
8322 code char test_array[] = {'c','h','e','a','p'};
8325 Read access to this array using an 8-bit unsigned index generates the assembly
8342 90s00r41\SpecialChar ~
8345 mov dptr,#_test_array
8360 \layout Subsubsection
8363 \begin_inset LatexCommand \index{bit}
8370 This is a data-type and a storage class specifier.
8371 When a variable is declared as a bit, it is allocated into the bit addressable
8372 memory of 8051, e.g.:
8380 Writing 1 to this variable generates the assembly code:
8396 The bit addressable memory consists of 128 bits which are located from 0x20
8397 to 0x2f in data memory.
8400 Apart from this 8051 specific storage class most architectures support ANSI-C
8402 \begin_inset LatexCommand \index{bitfields}
8412 Not really meant as examples, but nevertheless showing what bitfields are
8413 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8417 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8418 signed modifier are implemented as unsigned.
8419 \layout Subsubsection
8422 \begin_inset LatexCommand \index{sfr}
8427 \begin_inset LatexCommand \index{sbit}
8434 Like the bit keyword,
8438 signifies both a data-type and storage class, they are used to describe
8459 variables of a 8051, eg:
8465 \begin_inset LatexCommand \index{at}
8469 0x80 P0;\SpecialChar ~
8470 /* special function register P0 at location 0x80 */
8472 sbit at 0xd7 CY; /* CY (Carry Flag
8473 \begin_inset LatexCommand \index{Flags}
8478 \begin_inset LatexCommand \index{Carry flag}
8485 Special function registers which are located on an address dividable by
8486 8 are bit-addressable, an
8490 addresses a specific bit within these sfr.
8491 \layout Subsubsection
8494 \begin_inset LatexCommand \index{Pointer}
8498 to MCS51/DS390 specific memory spaces
8501 SDCC allows (via language extensions) pointers to explicitly point to any
8502 of the memory spaces
8503 \begin_inset LatexCommand \index{Memory model}
8508 In addition to the explicit pointers, the compiler uses (by default) generic
8509 pointers which can be used to point to any of the memory spaces.
8513 Pointer declaration examples:
8518 /* pointer physically in internal ram pointing to object in external ram
8521 xdata unsigned char * data p;
8525 /* pointer physically in external ram pointing to object in internal ram
8528 data unsigned char * xdata p;
8532 /* pointer physically in code rom pointing to data in xdata space */
8534 xdata unsigned char * code p;
8538 /* pointer physically in code space pointing to data in code space */
8540 code unsigned char * code p;
8544 /* the following is a generic pointer physically located in xdata space
8550 Well you get the idea.
8555 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8568 The highest order byte of the
8572 pointers contains the data space information.
8573 Assembler support routines are called whenever data is stored or retrieved
8579 These are useful for developing reusable library
8580 \begin_inset LatexCommand \index{Libraries}
8585 Explicitly specifying the pointer type will generate the most efficient
8587 \layout Subsubsection
8589 Notes on MCS51 memory
8590 \begin_inset LatexCommand \index{MCS51 memory}
8597 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8598 RAM memory which is structured as follows:
8602 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8605 - Bytes 20-2F - 16 bytes to hold 128 bit
8606 \begin_inset LatexCommand \index{bit}
8612 - Bytes 30-7F - 80 bytes for general purpose use.
8617 Additionally some members of the MCS51 family may have up to 128 bytes of
8618 additional, indirectly addressable, internal RAM memory (
8623 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8628 Furthermore, some chips may have some built in external memory (
8633 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8637 ) which should not be confused with the internal, directly addressable RAM
8643 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8648 Sometimes this built in
8652 memory has to be activated before using it (you can probably find this
8653 information on the datasheet of the microcontroller your are using, see
8655 \begin_inset LatexCommand \ref{sub:Startup-Code}
8663 Normally SDCC will only use the first bank
8664 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8668 of registers (register bank 0), but it is possible to specify that other
8669 banks of registers should be used in interrupt
8670 \begin_inset LatexCommand \index{interrupt}
8675 By default, the compiler will place the stack after the last byte of allocated
8676 memory for variables.
8677 For example, if the first 2 banks of registers are used, and only four
8682 variables, it will position the base of the internal stack at address 20
8684 This implies that as the stack
8685 \begin_inset LatexCommand \index{stack}
8689 grows, it will use up the remaining register banks, and the 16 bytes used
8690 by the 128 bit variables, and 80 bytes for general purpose use.
8691 If any bit variables are used, the data variables will be placed after
8692 the byte holding the last bit variable.
8693 For example, if register banks 0 and 1 are used, and there are 9 bit variables
8698 variables will be placed starting at address 0x22.
8710 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
8714 to specify the start address of the
8728 -iram-size to specify the size of the total internal RAM (
8740 By default the 8051 linker will place the stack after the last byte of data
8753 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
8757 allows you to specify the start of the stack, i.e.
8758 you could start it after any data in the general purpose area.
8759 If your microcontroller has additional indirectly addressable internal
8764 ) you can place the stack on it.
8765 You may also need to use -
8776 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
8780 to set the start address of the external RAM (
8795 \begin_inset LatexCommand \index{-\/-data-loc}
8799 to specify its size.
8800 Same goes for the code memory, using -
8811 \begin_inset LatexCommand \index{-\/-data-loc}
8826 \begin_inset LatexCommand \index{-\/-data-loc}
8831 If in doubt, don't specify any options and see if the resulting memory
8832 layout is appropriate, then you can adjust it.
8835 The linker generates two files with memory allocation information.
8836 The first, with extension .map
8837 \begin_inset LatexCommand \index{<file>.map}
8841 shows all the variables and segments.
8842 The second with extension .mem
8843 \begin_inset LatexCommand \index{<file>.mem}
8847 shows the final memory layout.
8848 The linker will complain either if memory segments overlap, there is not
8849 enough memory, or there is not enough space for stack.
8850 If you get any linking warnings and/or errors related to stack or segments
8851 allocation, take a look at either the .map or .mem files to find out what
8853 The .mem file may even suggest a solution to the problem.
8856 Z80/Z180 Storage Class
8857 \begin_inset LatexCommand \index{Storage class}
8862 \layout Subsubsection
8865 \begin_inset LatexCommand \index{sfr}
8869 (in/out to 8-bit addresses)
8873 \begin_inset LatexCommand \index{Z80}
8877 family has separate address spaces for memory and
8887 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
8891 is accessed with special instructions, e.g.:
8896 sfr at 0x78 IoPort;\SpecialChar ~
8898 /* define a var in I/O space at 78h called IoPort */
8902 Writing 0x01 to this variable generates the assembly code:
8922 \layout Subsubsection
8925 \begin_inset LatexCommand \index{sfr}
8929 (in/out to 16-bit addresses)
8936 is used to support 16 bit addresses in I/O memory e.g.:
8942 \begin_inset LatexCommand \index{at}
8949 Writing 0x01 to this variable generates the assembly code:
8954 01 23 01\SpecialChar ~
8974 \layout Subsubsection
8977 \begin_inset LatexCommand \index{sfr}
8981 (in0/out0 to 8 bit addresses on Z180
8982 \begin_inset LatexCommand \index{Z180}
8987 \begin_inset LatexCommand \index{HD64180}
8994 The compiler option -
9004 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9006 \begin_inset LatexCommand \index{\#pragma portmode}
9010 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9020 If you include the file z180.h this will be set automatically.
9024 \begin_inset LatexCommand \index{Storage class}
9029 \layout Subsubsection
9032 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9039 The data storage class declares a variable that resides in the first 256
9040 bytes of memory (the direct page).
9041 The HC08 is most efficient at accessing variables (especially pointers)
9043 \layout Subsubsection
9046 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9053 The xdata storage class declares a variable that can reside anywhere in
9055 This is the default if no storage class is specified.
9060 \begin_inset LatexCommand \index{Absolute addressing}
9067 Data items can be assigned an absolute address with the
9070 \begin_inset LatexCommand \index{at}
9076 keyword, in addition to a storage class, e.g.:
9082 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9087 \begin_inset LatexCommand \index{at}
9091 0x7ffe unsigned int chksum;
9094 In the above example the variable chksum will located at 0x7ffe and 0x7fff
9095 of the external ram.
9100 reserve any space for variables declared in this way (they are implemented
9101 with an equate in the assembler).
9102 Thus it is left to the programmer to make sure there are no overlaps with
9103 other variables that are declared without the absolute address.
9104 The assembler listing file (.lst
9105 \begin_inset LatexCommand \index{<file>.lst}
9109 ) and the linker output files (.rst
9110 \begin_inset LatexCommand \index{<file>.rst}
9115 \begin_inset LatexCommand \index{<file>.map}
9119 ) are good places to look for such overlaps.
9120 Variables with an absolute address are
9125 \begin_inset LatexCommand \index{Variable initialization}
9132 In case of memory mapped I/O devices the keyword
9136 have to be used to tell the compiler that accesses might not be optimized
9143 \begin_inset LatexCommand \index{volatile}
9148 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9153 \begin_inset LatexCommand \index{at}
9157 0x8000 unsigned char PORTA_8255;
9160 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9165 \begin_inset LatexCommand \index{Aligned array}
9172 starts at a block (256 byte) boundary
9173 \begin_inset LatexCommand \index{block boundary}
9178 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9184 Absolute addresses can be specified for variables in all storage classes,
9191 \begin_inset LatexCommand \index{bit}
9196 \begin_inset LatexCommand \index{at}
9203 The above example will allocate the variable at offset 0x02 in the bit-addressab
9205 There is no real advantage to assigning absolute addresses to variables
9206 in this manner, unless you want strict control over all the variables allocated.
9207 One possible use would be to write hardware portable code.
9208 For example, if you have a routine that uses one or more of the microcontroller
9209 I/O pins, and such pins are different for two different hardwares, you
9210 can declare the I/O pins in your routine using:
9215 extern volatile bit MOSI;\SpecialChar ~
9219 /* master out, slave in */
9221 extern volatile bit MISO;\SpecialChar ~
9225 /* master in, slave out */
9227 extern volatile bit MCLK;\SpecialChar ~
9235 /* Input and Output of a byte on a 3-wire serial bus.
9240 If needed adapt polarity of clock, polarity of data and bit order
9245 unsigned char spi_io(unsigned char out_byte)
9269 MOSI = out_byte & 0x80;
9299 /* _asm nop _endasm; */\SpecialChar ~
9307 /* for slow peripherals */
9358 Then, someplace in the code for the first hardware you would use
9363 bit at 0x80 MOSI;\SpecialChar ~
9367 /* I/O port 0, bit 0 */
9369 bit at 0x81 MISO;\SpecialChar ~
9373 /* I/O port 0, bit 1 */
9375 bit at 0x82 MCLK;\SpecialChar ~
9379 /* I/O port 0, bit 2 */
9382 Similarly, for the second hardware you would use
9387 bit at 0x83 MOSI;\SpecialChar ~
9391 /* I/O port 0, bit 3 */
9393 bit at 0x91 MISO;\SpecialChar ~
9397 /* I/O port 1, bit 1 */
9400 \begin_inset LatexCommand \index{bit}
9404 at 0x92 MCLK;\SpecialChar ~
9408 /* I/O port 1, bit 2 */
9411 and you can use the same hardware dependent routine without changes, as
9412 for example in a library.
9413 This is somehow similar to sbit, but only one absolute address has to be
9414 specified in the whole project.
9418 \begin_inset LatexCommand \index{Parameters}
9423 \begin_inset LatexCommand \index{function parameter}
9428 \begin_inset LatexCommand \index{local variables}
9433 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9440 Automatic (local) variables and parameters to functions can either be placed
9441 on the stack or in data-space.
9442 The default action of the compiler is to place these variables in the internal
9443 RAM (for small model) or external RAM (for large model).
9444 This in fact makes them similar to
9447 \begin_inset LatexCommand \index{static}
9453 so by default functions are non-reentrant
9454 \begin_inset LatexCommand \index{reentrant}
9463 They can be placed on the stack
9464 \begin_inset LatexCommand \index{stack}
9481 \begin_inset LatexCommand \index{-\/-stack-auto}
9489 #pragma\SpecialChar ~
9493 \begin_inset LatexCommand \index{\#pragma stackauto}
9500 \begin_inset LatexCommand \index{reentrant}
9506 keyword in the function declaration, e.g.:
9511 unsigned char foo(char i) reentrant
9525 Since stack space on 8051 is limited, the
9543 option should be used sparingly.
9544 Note that the reentrant keyword just means that the parameters & local
9545 variables will be allocated to the stack, it
9549 mean that the function is register bank
9550 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9559 \begin_inset LatexCommand \index{local variables}
9563 can be assigned storage classes and absolute
9564 \begin_inset LatexCommand \index{Absolute addressing}
9581 xdata unsigned char i;
9594 \begin_inset LatexCommand \index{at}
9598 0x31 unsigned char j;
9610 In the above example the variable
9614 will be allocated in the external ram,
9618 in bit addressable space and
9637 or when a function is declared as
9641 this should only be done for static variables.
9645 \begin_inset LatexCommand \index{function parameter}
9649 however are not allowed any storage class
9650 \begin_inset LatexCommand \index{Storage class}
9654 , (storage classes for parameters will be ignored), their allocation is
9655 governed by the memory model in use, and the reentrancy options.
9659 \begin_inset LatexCommand \label{sub:Overlaying}
9664 \begin_inset LatexCommand \index{Overlaying}
9672 \begin_inset LatexCommand \index{reentrant}
9676 functions SDCC will try to reduce internal ram space usage by overlaying
9677 parameters and local variables of a function (if possible).
9678 Parameters and local variables
9679 \begin_inset LatexCommand \index{local variables}
9683 of a function will be allocated to an overlayable segment if the function
9686 no other function calls and the function is non-reentrant and the memory
9688 \begin_inset LatexCommand \index{Memory model}
9695 If an explicit storage class
9696 \begin_inset LatexCommand \index{Storage class}
9700 is specified for a local variable, it will NOT be overlayed.
9703 Note that the compiler (not the linkage editor) makes the decision for overlayin
9705 Functions that are called from an interrupt service routine should be preceded
9706 by a #pragma\SpecialChar ~
9708 \begin_inset LatexCommand \index{\#pragma nooverlay}
9712 if they are not reentrant.
9715 Also note that the compiler does not do any processing of inline assembler
9716 code, so the compiler might incorrectly assign local variables and parameters
9717 of a function into the overlay segment if the inline assembler code calls
9718 other c-functions that might use the overlay.
9719 In that case the #pragma\SpecialChar ~
9720 nooverlay should be used.
9723 Parameters and local variables of functions that contain 16 or 32 bit multiplica
9725 \begin_inset LatexCommand \index{Multiplication}
9730 \begin_inset LatexCommand \index{Division}
9734 will NOT be overlayed since these are implemented using external functions,
9743 \begin_inset LatexCommand \index{\#pragma nooverlay}
9749 void set_error(unsigned char errcd)
9765 void some_isr () interrupt
9766 \begin_inset LatexCommand \index{interrupt}
9796 In the above example the parameter
9804 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
9806 not present, this could cause unpredictable runtime behavior when called
9807 from an interrupt service routine.
9808 The #pragma\SpecialChar ~
9809 nooverlay ensures that the parameters and local variables for
9810 the function are NOT overlayed.
9813 Interrupt Service Routines
9814 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
9836 outines to be coded in C, with some extended keywords.
9841 void timer_isr (void) interrupt 1 using 1
9855 The optional number following the
9858 \begin_inset LatexCommand \index{interrupt}
9864 keyword is the interrupt number this routine will service.
9865 When present, the compiler will insert a call to this routine in the interrupt
9866 vector table for the interrupt number specified.
9867 If you have multiple source files in your project, interrupt service routines
9868 can be present in any of them, but a prototype of the isr MUST be present
9869 or included in the file that contains the function
9878 keyword can be used to tell the compiler to use the specified register
9879 bank (8051 specific) when generating code for this function.
9885 Interrupt service routines open the door for some very interesting bugs:
9887 If an interrupt service routine changes variables which are accessed by
9888 other functions these variables have to be declared
9893 \begin_inset LatexCommand \index{volatile}
9901 If the access to these variables is not
9904 \begin_inset LatexCommand \index{atomic access}
9911 the processor needs more than one instruction for the access and could
9912 be interrupted while accessing the variable) the interrupt must be disabled
9913 during the access to avoid inconsistent data.
9914 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
9915 and should be protected by disabling interrupts.
9916 You're not automatically on the safe side if you use 8 bit variables though.
9917 We need an example here: f.e.
9918 on the 8051 the harmless looking
9919 \begin_inset Quotes srd
9929 \begin_inset Quotes sld
9938 \begin_inset Quotes srd
9948 \begin_inset Quotes sld
9951 from within an interrupt routine might get lost if the interrupt occurs
9954 \begin_inset Quotes sld
9959 counter\SpecialChar ~
9964 \begin_inset Quotes srd
9967 is not atomic on the 8051 even if
9971 is located in data memory.
9972 Bugs like these are hard to reproduce and can cause a lot of trouble.
9976 The return address and the registers used in the interrupt service routine
9977 are saved on the stack
9978 \begin_inset LatexCommand \index{stack}
9982 so there must be sufficient stack space.
9983 If there isn't variables or registers (or even the return address itself)
9990 \begin_inset LatexCommand \index{stack overflow}
9994 is most likely to happen if the interrupt occurs during the
9995 \begin_inset Quotes sld
9999 \begin_inset Quotes srd
10002 subroutine when the stack is already in use for f.e.
10003 many return addresses.
10006 A special note here, int (16 bit) and long (32 bit) integer division
10007 \begin_inset LatexCommand \index{Division}
10012 \begin_inset LatexCommand \index{Multiplication}
10017 \begin_inset LatexCommand \index{Modulus}
10022 \begin_inset LatexCommand \index{Floating point support}
10026 operations are implemented using external support routines developed in
10028 If an interrupt service routine needs to do any of these operations then
10029 the support routines (as mentioned in a following section) will have to
10030 be recompiled using the
10043 \begin_inset LatexCommand \index{-\/-stack-auto}
10049 option and the source file will need to be compiled using the
10064 \begin_inset LatexCommand \index{-\/-int-long-reent}
10071 Calling other functions from an interrupt service routine is not recommended,
10072 avoid it if possible.
10073 Note that when some function is called from an interrupt service routine
10074 it should be preceded by a #pragma\SpecialChar ~
10076 \begin_inset LatexCommand \index{\#pragma nooverlay}
10080 if it is not reentrant.
10081 Furthermore nonreentrant functions should not be called from the main program
10082 while the interrupt service routine might be active.
10088 \begin_inset LatexCommand \ref{sub:Overlaying}
10093 about Overlaying and section
10094 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10099 about Functions using private register banks.
10102 MCS51/DS390 Interrupt Service Routines
10105 Interrupt numbers and the corresponding address & descriptions for the Standard
10106 8051/8052 are listed below.
10107 SDCC will automatically adjust the interrupt vector table to the maximum
10108 interrupt number specified.
10114 \begin_inset Tabular
10115 <lyxtabular version="3" rows="7" columns="3">
10117 <column alignment="center" valignment="top" leftline="true" width="0in">
10118 <column alignment="center" valignment="top" leftline="true" width="0in">
10119 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10120 <row topline="true" bottomline="true">
10121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10146 <row topline="true">
10147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10172 <row topline="true">
10173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10198 <row topline="true">
10199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10224 <row topline="true">
10225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10250 <row topline="true">
10251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10267 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10276 <row topline="true" bottomline="true">
10277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10311 If the interrupt service routine is defined without
10314 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10320 a register bank or with register bank 0 (
10324 0), the compiler will save the registers used by itself on the stack upon
10325 entry and restore them at exit, however if such an interrupt service routine
10326 calls another function then the entire register bank will be saved on the
10328 This scheme may be advantageous for small interrupt service routines which
10329 have low register usage.
10332 If the interrupt service routine is defined to be using a specific register
10337 & psw are saved and restored, if such an interrupt service routine calls
10338 another function (using another register bank) then the entire register
10339 bank of the called function will be saved on the stack.
10340 This scheme is recommended for larger interrupt service routines.
10343 HC08 Interrupt Service Routines
10346 Since the number of interrupts available is chip specific and the interrupt
10347 vector table always ends at the last byte of memory, the interrupt numbers
10348 corresponds to the interrupt vectors in reverse order of address.
10349 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10350 2 will use the interrupt vector at 0xfffa, and so on.
10351 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10352 this way; instead see section
10353 \begin_inset LatexCommand \ref{sub:Startup-Code}
10357 for details on customizing startup.
10360 Z80 Interrupt Service Routines
10363 The Z80 uses several different methods for determining the correct interrupt
10364 vector depending on the hardware implementation.
10365 Therefore, SDCC ignores the optional interrupt number and does not attempt
10366 to generate an interrupt vector table.
10369 By default, SDCC generates code for a maskable interrupt, which uses an
10370 RETI instruction to return from the interrupt.
10371 To write an interrupt handler for the non-maskable interrupt, which needs
10372 an RETN instruction instead, add the
10381 void nmi_isr (void) critical interrupt
10395 Enabling and Disabling Interrupts
10398 Critical Functions and Critical Statements
10401 A special keyword may be associated with a block or a function declaring
10407 SDCC will generate code to disable all interrupts
10408 \begin_inset LatexCommand \index{interrupt}
10412 upon entry to a critical function and restore the interrupt enable to the
10413 previous state before returning.
10414 Nesting critical functions will need one additional byte on the stack
10415 \begin_inset LatexCommand \index{stack}
10424 int foo () critical
10425 \begin_inset LatexCommand \index{critical}
10450 The critical attribute maybe used with other attributes like
10460 may also be used to disable interrupts more locally:
10468 More than one statement could have been included in the block.
10471 Enabling and Disabling Interrupts directly
10475 \begin_inset LatexCommand \index{interrupt}
10479 can also be disabled and enabled directly (8051):
10484 EA = 0;\SpecialChar ~
10547 EA = 1;\SpecialChar ~
10614 On other architectures which have seperate opcodes for enabling and disabling
10615 interrupts you might want to make use of defines with inline assembly
10616 \begin_inset LatexCommand \index{Assembler routines}
10626 \begin_inset LatexCommand \index{\_asm}
10635 \begin_inset LatexCommand \index{\_endasm}
10644 #define SEI _asm\SpecialChar ~
10656 Note: it is sometimes sufficient to disable only a specific interrupt source
10658 a timer or serial interrupt by manipulating an
10661 \begin_inset LatexCommand \index{interrupt mask}
10671 Usually the time during which interrupts are disabled should be kept as
10673 This minimizes both
10678 \begin_inset LatexCommand \index{interrupt latency}
10682 (the time between the occurrence of the interrupt and the execution of
10683 the first code in the interrupt routine) and
10688 \begin_inset LatexCommand \index{interrupt jitter}
10692 (the difference between the shortest and the longest interrupt latency).
10693 These really are something different, f.e.
10694 a serial interrupt has to be served before its buffer overruns so it cares
10695 for the maximum interrupt latency, whereas it does not care about jitter.
10696 On a loudspeaker driven via a digital to analog converter which is fed
10697 by an interrupt a latency of a few milliseconds might be tolerable, whereas
10698 a much smaller jitter will be very audible.
10701 You can reenable interrupts within an interrupt routine and on some architecture
10702 s you can make use of two (or more) levels of
10704 interrupt priorities
10707 \begin_inset LatexCommand \index{interrupt priority}
10712 On some architectures which don't support interrupt priorities these can
10713 be implemented by manipulating the interrupt mask and reenabling interrupts
10714 within the interrupt routine.
10715 Check there is sufficient space on the stack
10716 \begin_inset LatexCommand \index{stack}
10720 and don't add complexity unless you have to.
10724 Functions using private register banks
10725 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
10732 Some architectures have support for quickly changing register sets.
10733 SDCC supports this feature with the
10736 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10742 attribute (which tells the compiler to use a register bank
10743 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10747 other than the default bank zero).
10748 It should only be applied to
10751 \begin_inset LatexCommand \index{interrupt}
10757 functions (see footnote below).
10758 This will in most circumstances make the generated ISR code more efficient
10759 since it will not have to save registers on the stack.
10766 attribute will have no effect on the generated code for a
10770 function (but may occasionally be useful anyway
10776 possible exception: if a function is called ONLY from 'interrupt' functions
10777 using a particular bank, it can be declared with the same 'using' attribute
10778 as the calling 'interrupt' functions.
10779 For instance, if you have several ISRs using bank one, and all of them
10780 call memcpy(), it might make sense to create a specialized version of memcpy()
10781 'using 1', since this would prevent the ISR from having to save bank zero
10782 to the stack on entry and switch to bank zero before calling the function
10789 (pending: I don't think this has been done yet)
10796 function using a non-zero bank will assume that it can trash that register
10797 bank, and will not save it.
10798 Since high-priority interrupts
10799 \begin_inset LatexCommand \index{interrupt priority}
10803 can interrupt low-priority ones on the 8051 and friends, this means that
10804 if a high-priority ISR
10808 a particular bank occurs while processing a low-priority ISR
10812 the same bank, terrible and bad things can happen.
10813 To prevent this, no single register bank should be
10817 by both a high priority and a low priority ISR.
10818 This is probably most easily done by having all high priority ISRs use
10819 one bank and all low priority ISRs use another.
10820 If you have an ISR which can change priority at runtime, you're on your
10821 own: I suggest using the default bank zero and taking the small performance
10825 It is most efficient if your ISR calls no other functions.
10826 If your ISR must call other functions, it is most efficient if those functions
10827 use the same bank as the ISR (see note 1 below); the next best is if the
10828 called functions use bank zero.
10829 It is very inefficient to call a function using a different, non-zero bank
10835 \begin_inset LatexCommand \label{sub:Startup-Code}
10840 \begin_inset LatexCommand \index{Startup code}
10847 MCS51/DS390 Startup Code
10850 The compiler inserts a call to the C routine
10852 _sdcc_external_startup()
10853 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
10862 at the start of the CODE area.
10863 This routine is in the runtime library
10864 \begin_inset LatexCommand \index{Runtime library}
10869 By default this routine returns 0, if this routine returns a non-zero value,
10870 the static & global variable initialization will be skipped and the function
10871 main will be invoked.
10872 Otherwise static & global variables will be initialized before the function
10876 _sdcc_external_startup()
10878 routine to your program to override the default if you need to setup hardware
10879 or perform some other critical operation prior to static & global variable
10881 On some mcs51 variants xdata has to be explicitly enabled before it can
10882 be accessed, this is the place to do it.
10883 The startup code clears the complete 256 byte of idata memory, this might
10884 cause problems for 128 byte devices (endless loop reported for Chipcon
10888 See also the compiler option
10907 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
10912 \begin_inset LatexCommand \ref{sub:MCS51-variants}
10917 about MCS51-variants.
10923 The HC08 startup code follows the same scheme as the MCS51 startup code.
10929 On the Z80 the startup code is inserted by linking with crt0.o which is generated
10930 from sdcc/device/lib/z80/crt0.s.
10931 If you need a different startup code you can use the compiler option
10952 \begin_inset LatexCommand \index{-\/-no-std-crt0}
10956 and provide your own crt0.o.
10960 Inline Assembler Code
10961 \begin_inset LatexCommand \index{Assembler routines}
10968 A Step by Step Introduction
10969 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
10976 Starting from a small snippet of c-code this example shows for the MCS51
10977 how to use inline assembly, access variables, a function parameter and
10978 an array in xdata memory.
10979 The example uses an MCS51 here but is easily adapted for other architectures.
10980 This is a buffer routine which should be optimized:
10987 \begin_inset LatexCommand \index{far (storage class)}
10992 \begin_inset LatexCommand \index{at}
10997 \begin_inset LatexCommand \index{Aligned array}
11003 unsigned char head,tail;
11007 void to_buffer( unsigned char c )
11015 if( head != tail-1 )
11025 buf[ head++ ] = c;\SpecialChar ~
11029 /* access to a 256 byte aligned array */
11034 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11035 then a corresponding buffer.asm file is generated.
11036 We define a new function
11040 in file buffer.c in which we cut and paste the generated code, removing
11041 unwanted comments and some ':'.
11043 \begin_inset Quotes sld
11047 \begin_inset Quotes srd
11051 \begin_inset Quotes sld
11055 \begin_inset Quotes srd
11058 to the beginning and the end of the function body:
11064 /* With a cut and paste from the .asm file, we have something to start with.
11069 The function is not yet OK! (registers aren't saved) */
11071 void to_buffer_asm( unsigned char c )
11080 \begin_inset LatexCommand \index{\_asm}
11094 ;buffer.c if( head != tail-1 )
11142 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11143 \begin_inset LatexCommand \index{Aligned array}
11212 The new file buffer.c should compile with only one warning about the unreferenced
11213 function argument 'c'.
11214 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11215 (1) and finally have:
11221 unsigned char far at 0x7f00 buf[0x100];
11223 unsigned char head,tail;
11225 #define USE_ASSEMBLY (1)
11233 void to_buffer( unsigned char c )
11241 if( head != tail-1 )
11261 void to_buffer( unsigned char c )
11269 c; // to avoid warning: unreferenced function argument
11276 \begin_inset LatexCommand \index{\_asm}
11290 ; save used registers here.
11301 ; If we were still using r2,r3 we would have to push them here.
11304 ; if( head != tail-1 )
11347 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11371 ; buf[ head++ ] = c;
11382 a,dpl \SpecialChar ~
11389 ; dpl holds lower byte of function argument
11400 dpl,_head \SpecialChar ~
11403 ; buf is 0x100 byte aligned so head can be used directly
11445 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11457 ; restore used registers here
11470 The inline assembler code can contain any valid code understood by the assembler
11471 , this includes any assembler directives and comment lines
11477 The assembler does not like some characters like ':' or ''' in comments.
11478 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11482 The compiler does not do any validation of the code within the
11485 \begin_inset LatexCommand \index{\_asm}
11493 Specifically it will not know which registers are used and thus register
11495 \begin_inset LatexCommand \index{push/pop}
11499 has to be done manually.
11503 It is recommended that each assembly instruction (including labels) be placed
11504 in a separate line (as the example shows).
11518 \begin_inset LatexCommand \index{-\/-peep-asm}
11524 command line option is used, the inline assembler code will be passed through
11525 the peephole optimizer
11526 \begin_inset LatexCommand \index{Peephole optimizer}
11531 There are only a few (if any) cases where this option makes sense, it might
11532 cause some unexpected changes in the inline assembler code.
11533 Please go through the peephole optimizer rules defined in file
11537 before using this option.
11541 \begin_inset LatexCommand \label{sub:Naked-Functions}
11546 \begin_inset LatexCommand \index{Naked functions}
11553 A special keyword may be associated with a function declaring it as
11556 \begin_inset LatexCommand \index{\_naked}
11567 function modifier attribute prevents the compiler from generating prologue
11568 \begin_inset LatexCommand \index{function prologue}
11573 \begin_inset LatexCommand \index{function epilogue}
11577 code for that function.
11578 This means that the user is entirely responsible for such things as saving
11579 any registers that may need to be preserved, selecting the proper register
11580 bank, generating the
11584 instruction at the end, etc.
11585 Practically, this means that the contents of the function must be written
11586 in inline assembler.
11587 This is particularly useful for interrupt functions, which can have a large
11588 (and often unnecessary) prologue/epilogue.
11589 For example, compare the code generated by these two functions:
11595 \begin_inset LatexCommand \index{volatile}
11599 data unsigned char counter;
11603 void simpleInterrupt(void) interrupt
11604 \begin_inset LatexCommand \index{interrupt}
11622 void nakedInterrupt(void) interrupt 2 _naked
11631 \begin_inset LatexCommand \index{\_asm}
11648 _counter ; does not change flags, no need to save psw
11660 ; MUST explicitly include ret or reti in _naked function.
11667 \begin_inset LatexCommand \index{\_endasm}
11676 For an 8051 target, the generated simpleInterrupt looks like:
11817 whereas nakedInterrupt looks like:
11832 _counter ; does not change flags, no need to save psw
11850 ; MUST explicitly include ret or reti in _naked function
11853 The related directive #pragma exclude
11854 \begin_inset LatexCommand \index{\#pragma exclude}
11858 allows a more fine grained control over pushing & popping
11859 \begin_inset LatexCommand \index{push/pop}
11866 While there is nothing preventing you from writing C code inside a
11870 function, there are many ways to shoot yourself in the foot doing this,
11871 and it is recommended that you stick to inline assembler.
11874 Use of Labels within Inline Assembler
11877 SDCC allows the use of in-line assembler with a few restrictions regarding
11879 In older versions of the compiler all labels defined within inline assembler
11888 where nnnn is a number less than 100 (which implies a limit of utmost 100
11889 inline assembler labels
11903 \begin_inset LatexCommand \index{\_asm}
11933 \begin_inset LatexCommand \index{\_endasm}
11940 Inline assembler code cannot reference any C-Labels, however it can reference
11942 \begin_inset LatexCommand \index{Labels}
11946 defined by the inline assembler, e.g.:
11971 ; some assembler code
11991 /* some more c code */
11993 clabel:\SpecialChar ~
11995 /* inline assembler cannot reference this label */
12007 $0003: ;label (can be referenced by inline assembler only)
12019 /* some more c code */
12024 In other words inline assembly code can access labels defined in inline
12025 assembly within the scope of the function.
12026 The same goes the other way, i.e.
12027 labels defines in inline assembly can not be accessed by C statements.
12030 Interfacing with Assembler Code
12031 \begin_inset LatexCommand \index{Assembler routines}
12038 Global Registers used for Parameter Passing
12039 \begin_inset LatexCommand \index{Parameter passing}
12046 The compiler always uses the global registers
12049 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12054 \begin_inset LatexCommand \index{DPTR}
12059 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12068 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12074 to pass the first parameter to a routine.
12075 The second parameter onwards is either allocated on the stack (for reentrant
12086 -stack-auto is used) or in data / xdata memory (depending on the memory
12091 Assembler Routine (non-reentrant)
12094 In the following example
12095 \begin_inset LatexCommand \index{reentrant}
12100 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12104 the function c_func calls an assembler routine asm_func, which takes two
12106 \begin_inset LatexCommand \index{function parameter}
12115 extern int asm_func(unsigned char, unsigned char);
12119 int c_func (unsigned char i, unsigned char j)
12127 return asm_func(i,j);
12141 return c_func(10,9);
12146 The corresponding assembler function is:
12151 .globl _asm_func_PARM_2
12252 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12269 Note here that the return values
12270 \begin_inset LatexCommand \index{return value}
12274 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12276 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12277 b' & 'acc' for four byte values.
12280 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12281 the parameter number starting from 1, and counting from the left.
12282 The first parameter is passed in
12283 \begin_inset Quotes eld
12287 \begin_inset Quotes erd
12290 for a one byte parameter,
12291 \begin_inset Quotes eld
12295 \begin_inset Quotes erd
12299 \begin_inset Quotes eld
12303 \begin_inset Quotes erd
12306 for three bytes and
12307 \begin_inset Quotes eld
12311 \begin_inset Quotes erd
12314 for a four bytes parameter.
12315 The variable name for the second parameter will be _<function_name>_PARM_2.
12319 Assemble the assembler routine with the following command:
12326 asx8051 -losg asmfunc.asm
12333 Then compile and link the assembler routine to the C source file with the
12341 sdcc cfunc.c asmfunc.rel
12344 Assembler Routine (reentrant)
12348 \begin_inset LatexCommand \index{reentrant}
12353 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12357 the second parameter
12358 \begin_inset LatexCommand \index{function parameter}
12362 onwards will be passed on the stack, the parameters are pushed from right
12364 after the call the leftmost parameter will be on the top of the stack.
12365 Here is an example:
12370 extern int asm_func(unsigned char, unsigned char);
12374 int c_func (unsigned char i, unsigned char j) reentrant
12382 return asm_func(i,j);
12396 return c_func(10,9);
12401 The corresponding assembler routine is:
12501 The compiling and linking procedure remains the same, however note the extra
12502 entry & exit linkage required for the assembler code, _bp is the stack
12503 frame pointer and is used to compute the offset into the stack for parameters
12504 and local variables.
12508 \begin_inset LatexCommand \index{int (16 bit)}
12513 \begin_inset LatexCommand \index{long (32 bit)}
12520 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12521 multiplication and modulus operations are implemented by support routines.
12522 These support routines are all developed in ANSI-C to facilitate porting
12523 to other MCUs, although some model specific assembler optimizations are
12525 The following files contain the described routines, all of them can be
12526 found in <installdir>/share/sdcc/lib.
12532 \begin_inset Tabular
12533 <lyxtabular version="3" rows="11" columns="2">
12535 <column alignment="center" valignment="top" leftline="true" width="0">
12536 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12537 <row topline="true" bottomline="true">
12538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12559 <row topline="true">
12560 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12568 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12573 16 bit multiplication
12577 <row topline="true">
12578 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12591 signed 16 bit division (calls _divuint)
12595 <row topline="true">
12596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12604 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12609 unsigned 16 bit division
12613 <row topline="true">
12614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12622 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12627 signed 16 bit modulus (calls _moduint)
12631 <row topline="true">
12632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12645 unsigned 16 bit modulus
12649 <row topline="true">
12650 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12658 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12663 32 bit multiplication
12667 <row topline="true">
12668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12681 signed 32 division (calls _divulong)
12685 <row topline="true">
12686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12699 unsigned 32 division
12703 <row topline="true">
12704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12712 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12717 signed 32 bit modulus (calls _modulong)
12721 <row topline="true" bottomline="true">
12722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12730 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12735 unsigned 32 bit modulus
12748 Since they are compiled as
12753 \begin_inset LatexCommand \index{reentrant}
12758 \begin_inset LatexCommand \index{interrupt}
12762 service routines should not do any of the above operations.
12763 If this is unavoidable then the above routines will need to be compiled
12777 \begin_inset LatexCommand \index{-\/-stack-auto}
12783 option, after which the source program will have to be compiled with
12796 \begin_inset LatexCommand \index{-\/-int-long-reent}
12803 Notice that you don't have to call these routines directly.
12804 The compiler will use them automatically every time an integer operation
12808 Floating Point Support
12809 \begin_inset LatexCommand \index{Floating point support}
12816 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
12817 floating point support routines are derived from gcc's floatlib.c and consist
12818 of the following routines:
12826 \begin_inset Tabular
12827 <lyxtabular version="3" rows="17" columns="2">
12829 <column alignment="center" valignment="top" leftline="true" width="0">
12830 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12831 <row topline="true" bottomline="true">
12832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12849 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12858 <row topline="true">
12859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12876 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12890 add floating point numbers
12894 <row topline="true">
12895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12926 subtract floating point numbers
12930 <row topline="true">
12931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12962 divide floating point numbers
12966 <row topline="true">
12967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12998 multiply floating point numbers
13002 <row topline="true">
13003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13034 convert floating point to unsigned char
13038 <row topline="true">
13039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13070 convert floating point to signed char
13074 <row topline="true">
13075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13106 convert floating point to unsigned int
13110 <row topline="true">
13111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13142 convert floating point to signed int
13146 <row topline="true">
13147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13187 convert floating point to unsigned long
13191 <row topline="true">
13192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13223 convert floating point to signed long
13227 <row topline="true">
13228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13259 convert unsigned char to floating point
13263 <row topline="true">
13264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13295 convert char to floating point number
13299 <row topline="true">
13300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13331 convert unsigned int to floating point
13335 <row topline="true">
13336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13367 convert int to floating point numbers
13371 <row topline="true">
13372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13403 convert unsigned long to floating point number
13407 <row topline="true" bottomline="true">
13408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13439 convert long to floating point number
13452 These support routines are developed in ANSI-C so there is room for space
13453 and speed improvement.
13454 Note if all these routines are used simultaneously the data space might
13456 For serious floating point usage it is recommended that the large model
13458 Also notice that you don't have to call this routines directly.
13459 The compiler will use them automatically every time a floating point operation
13464 \begin_inset LatexCommand \index{Libraries}
13473 <pending: this is messy and incomplete - a little more information is in
13474 sdcc/doc/libdoc.txt
13479 Compiler support routines (_gptrget, _mulint etc.)
13482 Stdclib functions (puts, printf, strcat etc.)
13483 \layout Subsubsection
13489 \begin_inset LatexCommand \index{<stdio.h>}
13493 As usual on embedded systems you have to provide your own
13496 \begin_inset LatexCommand \index{getchar()}
13505 \begin_inset LatexCommand \index{putchar()}
13512 SDCC does not know whether the system connects to a serial line with or
13513 without handshake, LCD, keyboard or other device.
13514 You'll find examples for serial routines f.e.
13515 in sdcc/device/lib.
13518 If you're short on memory you might want to use
13529 \begin_inset LatexCommand \index{printf()}
13536 For the mcs51 there is an assembly version
13540 which should fit the requirements of many embedded systems (by unsetting
13541 #defines it can be customized to
13545 support long variables and field widths).
13548 Math functions (sin, pow, sqrt etc.)
13555 \begin_inset LatexCommand \index{Libraries}
13559 included in SDCC should have a license at least as liberal as the GNU Lesser
13560 General Public License
13561 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
13572 license statements for the libraries are missing.
13573 sdcc/device/lib/ser_ir.c
13577 come with a GPL (as opposed to LGPL) License - this will not be liberal
13578 enough for many embedded programmers.
13581 If you have ported some library or want to share experience about some code
13583 falls into any of these categories Busses (I
13584 \begin_inset Formula $^{\textrm{2}}$
13587 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
13588 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
13589 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
13590 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
13595 would certainly like to hear about it.
13596 Programmers coding for embedded systems are not especially famous for being
13597 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
13598 e these references are very valuable.
13599 Let's help to create a climate where information is shared.
13605 MCS51 Memory Models
13606 \begin_inset LatexCommand \index{Memory model}
13611 \begin_inset LatexCommand \index{MCS51 memory model}
13616 \layout Subsubsection
13621 SDCC allows two memory models for MCS51 code,
13630 Modules compiled with different memory models should
13634 be combined together or the results would be unpredictable.
13635 The library routines supplied with the compiler are compiled as both small
13637 The compiled library modules are contained in separate directories as small
13638 and large so that you can link to either set.
13642 When the large model is used all variables declared without a storage class
13643 will be allocated into the external ram, this includes all parameters and
13644 local variables (for non-reentrant
13645 \begin_inset LatexCommand \index{reentrant}
13650 When the small model is used variables without storage class are allocated
13651 in the internal ram.
13654 Judicious usage of the processor specific storage classes
13655 \begin_inset LatexCommand \index{Storage class}
13659 and the 'reentrant' function type will yield much more efficient code,
13660 than using the large model.
13661 Several optimizations are disabled when the program is compiled using the
13662 large model, it is therefore recommended that the small model be used unless
13663 absolutely required.
13664 \layout Subsubsection
13667 \begin_inset LatexCommand \label{sub:External-Stack}
13672 \begin_inset LatexCommand \index{stack}
13677 \begin_inset LatexCommand \index{External stack (mcs51)}
13688 : this option wasn't maintained for a long time and is quite buggy.
13689 Small programs might work.
13690 You've been warned!
13693 The external stack (-
13704 \begin_inset LatexCommand \index{-\/-xstack}
13708 ) is located in pdata
13709 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
13713 memory (usually at the start of the external ram segment) and is 256 bytes
13725 -xstack option is used to compile the program, the parameters and local
13727 \begin_inset LatexCommand \index{local variables}
13731 of all reentrant functions are allocated in this area.
13732 This option is provided for programs with large stack space requirements.
13733 When used with the -
13744 \begin_inset LatexCommand \index{-\/-stack-auto}
13748 option, all parameters and local variables are allocated on the external
13749 stack (note: support libraries will need to be recompiled with the same
13753 The compiler outputs the higher order address byte of the external ram segment
13755 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
13760 \begin_inset LatexCommand \ref{sub:MCS51-variants}
13764 ), therefore when using the External Stack option, this port
13768 be used by the application program.
13772 \begin_inset LatexCommand \index{Memory model}
13777 \begin_inset LatexCommand \index{DS390 memory model}
13784 The only model supported is Flat 24
13785 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
13790 This generates code for the 24 bit contiguous addressing mode of the Dallas
13792 In this mode, up to four meg of external RAM or code space can be directly
13794 See the data sheets at www.dalsemi.com for further information on this part.
13798 Note that the compiler does not generate any code to place the processor
13799 into 24 bitmode (although
13803 in the ds390 libraries will do that for you).
13809 \begin_inset LatexCommand \index{Tinibios (DS390)}
13813 , the boot loader or similar code must ensure that the processor is in 24
13814 bit contiguous addressing mode before calling the SDCC startup code.
13832 option, variables will by default be placed into the XDATA segment.
13837 Segments may be placed anywhere in the 4 meg address space using the usual
13849 Note that if any segments are located above 64K, the -r flag must be passed
13850 to the linker to generate the proper segment relocations, and the Intel
13851 HEX output format must be used.
13852 The -r flag can be passed to the linker by using the option
13856 on the SDCC command line.
13857 However, currently the linker can not handle code segments > 64k.
13861 \begin_inset LatexCommand \index{Pragmas}
13868 SDCC supports the following #pragma directives:
13872 \begin_inset LatexCommand \index{\#pragma save}
13876 - this will save all current options to the save/restore stack.
13877 See #pragma\SpecialChar ~
13882 \begin_inset LatexCommand \index{\#pragma restore}
13886 - will restore saved options from the last save.
13887 saves & restores can be nested.
13888 SDCC uses a save/restore stack: save pushes current options to the stack,
13889 restore pulls current options from the stack.
13890 See #pragma\SpecialChar ~
13897 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13902 \begin_inset LatexCommand \index{function prologue}
13906 function1[,function2[,function3...]] - The compiler by default uses a caller
13907 saves convention for register saving across function calls, however this
13908 can cause unnecessary register pushing & popping
13909 \begin_inset LatexCommand \index{push/pop}
13913 when calling small functions from larger functions.
13914 This option can be used to switch off the register saving convention for
13915 the function names specified.
13916 The compiler will not save registers when calling these functions, extra
13917 code need to be manually inserted at the entry & exit for these functions
13918 to save & restore the registers used by these functions, this can SUBSTANTIALLY
13919 reduce code & improve run time performance of the generated code.
13920 In the future the compiler (with inter procedural analysis) may be able
13921 to determine the appropriate scheme to use for each function call.
13932 -callee-saves command line option is used, the function names specified
13933 in #pragma\SpecialChar ~
13935 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13939 is appended to the list of functions specified in the command line.
13943 \begin_inset LatexCommand \index{\#pragma exclude}
13947 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
13948 of pairs of push/pop
13949 \begin_inset LatexCommand \index{push/pop}
13958 \begin_inset LatexCommand \index{interrupt}
13971 The directive should be placed immediately before the ISR function definition
13972 and it affects ALL ISR functions following it.
13973 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
13974 exclude\SpecialChar ~
13976 \begin_inset LatexCommand \index{\#pragma exclude}
13981 See also the related keyword _naked
13982 \begin_inset LatexCommand \index{\_naked}
13990 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
13994 - the compiler will not warn you anymore for obvious mistakes, you'r on
13999 \begin_inset LatexCommand \index{\#pragma nogcse}
14003 - will stop global common subexpression elimination.
14007 \begin_inset LatexCommand \index{\#pragma noinduction}
14011 - will stop loop induction optimizations.
14015 \begin_inset LatexCommand \index{\#pragma noinvariant}
14019 - will not do loop invariant optimizations.
14020 For more details see Loop Invariants in section
14021 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14029 \begin_inset LatexCommand \index{\#pragma noiv}
14033 - Do not generate interrupt
14034 \begin_inset LatexCommand \index{interrupt}
14038 vector table entries for all ISR functions defined after the pragma.
14039 This is useful in cases where the interrupt vector table must be defined
14040 manually, or when there is a secondary, manually defined interrupt vector
14042 for the autovector feature of the Cypress EZ-USB FX2).
14043 More elegantly this can be achieved by obmitting the optional interrupt
14044 number after the interrupt keyword, see section
14045 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14054 \begin_inset LatexCommand \index{\#pragma nojtbound}
14058 - will not generate code for boundary value checking, when switch statements
14059 are turned into jump-tables (dangerous).
14060 For more details see section
14061 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14069 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14073 - Will not do loop reversal optimization
14077 \begin_inset LatexCommand \index{\#pragma nooverlay}
14081 - the compiler will not overlay the parameters and local variables of a
14086 \begin_inset LatexCommand \index{\#pragma stackauto}
14101 \begin_inset LatexCommand \index{-\/-stack-auto}
14106 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
14110 Parameters and Local Variables.
14113 SDCPP supports the following #pragma directives:
14117 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
14121 (+ | -) - switch _asm _endasm block preprocessing on / off.
14125 The pragma's are intended to be used to turn-on or off certain optimizations
14126 which might cause the compiler to generate extra stack / data space to
14127 store compiler generated temporary variables.
14128 This usually happens in large functions.
14129 Pragma directives should be used as shown in the following example, they
14130 are used to control options & optimizations for a given function; pragmas
14131 should be placed before and/or after a function, placing pragma's inside
14132 a function body could have unpredictable results.
14138 \begin_inset LatexCommand \index{\#pragma save}
14149 /* save the current settings */
14152 \begin_inset LatexCommand \index{\#pragma nogcse}
14161 /* turnoff global subexpression elimination */
14163 #pragma noinduction
14164 \begin_inset LatexCommand \index{\#pragma noinduction}
14168 /* turn off induction optimizations */
14191 \begin_inset LatexCommand \index{\#pragma restore}
14195 /* turn the optimizations back on */
14198 The compiler will generate a warning message when extra space is allocated.
14199 It is strongly recommended that the save and restore pragma's be used when
14200 changing options for a function.
14203 Defines Created by the Compiler
14206 The compiler creates the following #defines
14207 \begin_inset LatexCommand \index{\#defines}
14212 \begin_inset LatexCommand \index{Defines created by the compiler}
14222 \begin_inset Tabular
14223 <lyxtabular version="3" rows="10" columns="2">
14225 <column alignment="center" valignment="top" leftline="true" width="0">
14226 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14227 <row topline="true" bottomline="true">
14228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14249 <row topline="true">
14250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14256 \begin_inset LatexCommand \index{SDCC}
14263 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14268 this Symbol is always defined
14272 <row topline="true">
14273 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14279 \begin_inset LatexCommand \index{SDCC\_mcs51}
14284 \begin_inset LatexCommand \index{SDCC\_ds390}
14289 \begin_inset LatexCommand \index{SDCC\_z80}
14296 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14301 depending on the model used (e.g.: -mds390
14305 <row topline="true">
14306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14312 \begin_inset LatexCommand \index{\_\_mcs51}
14317 \begin_inset LatexCommand \index{\_\_ds390}
14322 \begin_inset LatexCommand \index{\_\_hc08}
14327 \begin_inset LatexCommand \index{\_\_z80}
14334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14339 depending on the model used (e.g.
14344 <row topline="true">
14345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14351 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14358 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14381 <row topline="true">
14382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14388 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14395 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14418 <row topline="true">
14419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14425 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14455 <row topline="true">
14456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14462 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
14469 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14492 <row topline="true">
14493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14499 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
14506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14519 <row topline="true" bottomline="true">
14520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14526 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
14533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14553 Notes on supported Processors
14557 \begin_inset LatexCommand \label{sub:MCS51-variants}
14562 \begin_inset LatexCommand \index{MCS51 variants}
14569 MCS51 processors are available from many vendors and come in many different
14571 While they might differ considerably in respect to Special Function Registers
14572 the core MCS51 is usually not modified or is kept compatible.
14576 pdata access by SFR
14579 With the upcome of devices with internal xdata and flash memory devices
14581 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14585 as dedicated I/O port is becoming more popular.
14586 Switching the high byte for pdata
14587 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14591 access which was formerly done by port P2 is then achieved by a Special
14593 \begin_inset LatexCommand \index{sfr}
14598 In well-established MCS51 tradition the address of this
14602 is where the chip designers decided to put it.
14603 Needless to say that they didn't agree on a common name either.
14604 So that the startup code can correctly initialize xdata variables, you
14605 should define an sfr with the name _XPAGE
14608 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
14614 at the appropriate location if the default, port P2, is not used for this.
14620 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
14625 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
14630 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
14633 For more exotic implementations further customizations may be needed.
14635 \begin_inset LatexCommand \ref{sub:Startup-Code}
14639 for other possibilities.
14642 Other Features available by SFR
14645 Some MCS51 variants offer features like Double DPTR
14646 \begin_inset LatexCommand \index{DPTR}
14650 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
14651 These are currently not used for the MCS51 port.
14652 If you absolutely need them you can fall back to inline assembly or submit
14659 The DS80C400 microcontroller has a rich set of peripherals.
14660 In its built-in ROM library it includes functions to access some of the
14661 features, among them is a TCP stack with IP4 and IP6 support.
14662 Library headers (currently in beta status) and other files are provided
14666 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
14674 The Z80 and gbz80 port
14677 SDCC can target both the Zilog
14678 \begin_inset LatexCommand \index{Z80}
14682 and the Nintendo Gameboy's Z80-like gbz80
14683 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
14688 The Z80 port is passed through the same
14691 \begin_inset LatexCommand \index{Regression test}
14697 as the MCS51 and DS390 ports, so floating point support, support for long
14698 variables and bitfield support is fine.
14699 See mailing lists and forums about interrupt routines.
14702 As always, the code is the authoritative reference - see z80/ralloc.c and
14705 \begin_inset LatexCommand \index{stack}
14709 frame is similar to that generated by the IAR Z80 compiler.
14710 IX is used as the base pointer, HL and IY are used as a temporary registers,
14711 and BC and DE are available for holding variables.
14713 \begin_inset LatexCommand \index{return value}
14717 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
14719 The gbz80 port use the same set of registers for the return values, but
14720 in a different order of significance: E (one byte), DE (two bytes), or
14727 The port to the Motorola HC08
14728 \begin_inset LatexCommand \index{HC08}
14732 family has been added in October 2003, and is still undergoing some basic
14734 The code generator is complete, but the register allocation is still quite
14736 Some of the SDCC's standard C library functions have embedded non-HC08
14737 inline assembly and so are not yet usable.
14748 \begin_inset LatexCommand \index{PIC14}
14752 port still requires a major effort from the development community.
14753 However it can work for very simple code.
14756 C code and 14bit PIC code page
14757 \begin_inset LatexCommand \index{code page (pic14)}
14762 \begin_inset LatexCommand \index{RAM bank (pic14)}
14769 The linker organizes allocation for the code page and RAM banks.
14770 It does not have intimate knowledge of the code flow.
14771 It will put all the code section of a single asm file into a single code
14773 In order to make use of multiple code pages, separate asm files must be
14775 The compiler treats all functions of a single C file as being in the same
14776 code page unless it is non static.
14777 The compiler treats all local variables of a single C file as being in
14778 the same RAM bank unless it is an extern.
14782 To get the best follow these guide lines:
14785 make local functions static, as non static functions require code page selection
14789 Make local variables static as extern variables require RAM bank selection
14793 For devices that have multiple code pages it is more efficient to use the
14794 same number of files as pages, i.e.
14795 for the 16F877 use 4 separate files and i.e.
14796 for the 16F874 use 2 separate files.
14797 This way the linker can put the code for each file into different code
14798 pages and the compiler can allocate reusable variables more efficiently
14799 and there's less page selection overhead.
14800 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
14801 instruction set) use 'unsigned char' whereever possible instead of 'int'.
14804 Creating a device include file
14807 For generating a device include file use the support perl script inc2h.pl
14808 kept in directory support/script.
14814 For the interrupt function, use the keyword 'interrupt'
14815 \begin_inset LatexCommand \index{interrupt}
14819 with level number of 0 (PIC14 only has 1 interrupt so this number is only
14820 there to avoid a syntax error - it ought to be fixed).
14826 void Intr(void) interrupt 0
14832 T0IF = 0; /* Clear timer interrupt */
14837 Linking and assembling
14840 For assembling you can use either GPUTILS'
14841 \begin_inset LatexCommand \index{gputils (pic tools)}
14845 gpasm.exe or MPLAB's mpasmwin.exe.
14846 GPUTILS is available from
14847 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
14852 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
14853 If you use MPLAB and an interrupt function then the linker script file
14854 vectors section will need to be enlarged to link with mplink.
14877 sdcc -S -V -mpic14 -p16F877 $<
14891 $(PRJ).hex: $(OBJS)
14901 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
14923 sdcc -S -V -mpic14 -p16F877 $<
14933 mpasmwin /q /o $*.asm
14937 $(PRJ).hex: $(OBJS)
14947 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
14950 Please note that indentations within a
14954 have to be done with a tabulator character.
14958 \begin_inset LatexCommand \index{PIC16}
14966 \begin_inset LatexCommand \index{PIC16}
14970 port is the portion of SDCC that is responsible to produce code for the
14972 \begin_inset LatexCommand \index{Microchip}
14976 (TM) microcontrollers with 16 bit core.
14977 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
14983 PIC16 port supports the standard command line arguments as supposed, with
14984 the exception of certain cases that will be mentioned in the following
14987 \labelwidthstring 00.00.0000
14999 -stack-auto Auto variables that are function parameters, will be saved on
15003 There is no need to specify this in the command line.
15005 \labelwidthstring 00.00.0000
15017 -float-reent All floating point functions are reentrant by default.
15020 There is no need to specifiy this in the command line.
15022 \labelwidthstring 00.00.0000
15034 -callee-saves See -
15046 \labelwidthstring 00.00.0000
15058 -all-callee-saves All function arguments are passed on stack by default.
15061 There is no need to specify this in the command line.
15063 \labelwidthstring 00.00.0000
15075 -fommit-frame-pointer Frame pointer will be omitted when the function uses
15076 no local variables.
15079 Port Specific Options
15080 \begin_inset LatexCommand \index{Options PIC16}
15087 The port specific options appear after the global options in the sdcc --help
15089 \layout Subsubsection
15094 General options enable certain port features and optimizations.
15096 \labelwidthstring 00.00.0000
15108 -pgen-bank Instructs the port to insert BANKSEL directives before instructions
15109 that use the Bank Select Register (BSR).
15111 \labelwidthstring 00.00.0000
15123 -pomit-config-words Instructs the port to omit the generation of the configurati
15126 \labelwidthstring 00.00.0000
15138 -pomit-ivt Instructs the port to omit the generation of the interrupt vectors
15140 \labelwidthstring 00.00.0000
15152 -pleave-reset-vector Used in conjuction with the previous command, instructs
15153 the port NOT to omit the reset vector.
15155 \labelwidthstring 00.00.0000
15167 -stack-model=[model] Used in conjuction with the command above.
15168 Defines the stack model to be used, valid stack models are :
15171 \labelwidthstring 00.00.0000
15177 Selects small stack model.
15178 8 bit stack and frame pointers.
15179 Supports 256 bytes stack size.
15181 \labelwidthstring 00.00.0000
15187 Selects large stack model.
15188 16 bit stack and frame pointers.
15189 Supports 65536 bytes stack size.
15192 \labelwidthstring 00.00.0000
15204 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
15205 unitialized data variables with [kword].
15206 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
15208 \labelwidthstring 00.00.0000
15220 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
15221 Useful for bootloaders.
15223 \labelwidthstring 00.00.0000
15235 -asm= sets the full path and name of an external assembler to call.
15237 \labelwidthstring 00.00.0000
15249 -link= sets the full path and name of an external linker to call.
15250 \layout Subsubsection
15255 Debugging options enable extra debugging information in the output files.
15257 \labelwidthstring 00.00.0000
15269 -debug-xtra Similar to -
15280 \begin_inset LatexCommand \index{-\/-debug}
15284 , but dumps more information.
15286 \labelwidthstring 00.00.0000
15298 -debug-ralloc Force register allocator to dump <source>.d file with debugging
15300 <source> is the name of the file compiled.
15302 \labelwidthstring 00.00.0000
15314 -pcode-verbose Enable pcode debugging information in translation.
15317 Preprocessor Macros
15320 PIC16 port defines the following preprocessor macros while translating a
15325 \begin_inset Tabular
15326 <lyxtabular version="3" rows="2" columns="2">
15328 <column alignment="center" valignment="top" leftline="true" width="0">
15329 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15330 <row topline="true" bottomline="true">
15331 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15339 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15348 <row topline="true" bottomline="true">
15349 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15357 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15362 MCU Identification.
15367 is the microcontrol identification number, i.e.
15383 \begin_inset LatexCommand \index{PIC16}
15387 port uses the following directories for searching header files and libraries.
15391 \begin_inset Tabular
15392 <lyxtabular version="3" rows="3" columns="4">
15394 <column alignment="center" valignment="top" leftline="true" width="0">
15395 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15396 <column alignment="center" valignment="top" width="0">
15397 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15398 <row topline="true" bottomline="true">
15399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15423 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15432 <row topline="true">
15433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15438 PREFIX/sdcc/include/pic16
15441 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15446 PIC16 specific headers
15449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15457 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15466 <row topline="true" bottomline="true">
15467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15472 PREFIX/sdcc/lib/pic16
15475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15480 PIC16 specific libraries
15483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15491 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15508 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
15515 PIC16 port currently supports the following pragmas:
15517 \labelwidthstring 00.00.0000
15519 stack pragma stack forces the code generator to initialize the stack & frame
15520 pointers at a specific address.
15521 This is an adhoc solution since gplink does not support yet stack.
15522 When the gplink issue is resolved the pragma will be deprecated
15530 It is important to initialize the stack, otherwise strange things can happen.
15531 Stack is not initialized by default because there are some sources that
15533 (like library sources)
15538 The stack pragma should be used only once in a project.
15539 Multiple pragmas may result in indeterminate behaviour of the program.
15547 /* initializes stack at RAM address 0x5ff */
15550 #pragma stack 0x5ff
15552 \labelwidthstring 00.00.0000
15554 udata pragma udata instructs the compiler to emit code so that linker will
15555 place a variable at a specific memory bank
15563 /* places variable foo at bank2 */
15566 #pragma udata bank2 foo
15572 In order for this pragma to work there are some changes that must be made
15573 in the .lkr script used in link stage.
15574 In the following example a sample .lkr file is shown:
15579 // Sample linker script for the PIC18F452 processor
15585 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
15588 CODEPAGE NAME=page START=0x2A END=0x7FFF
15591 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
15594 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
15597 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
15600 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
15603 ACCESSBANK NAME=accessram START=0x0 END=0x7F
15608 DATABANK NAME=gpr0 START=0x80 END=0xFF
15611 DATABANK NAME=gpr1 START=0x100 END=0x1FF
15614 DATABANK NAME=gpr2 START=0x200 END=0x2FF
15617 DATABANK NAME=gpr3 START=0x300 END=0x3FF
15620 DATABANK NAME=gpr4 START=0x400 END=0x4FF
15623 DATABANK NAME=gpr5 START=0x500 END=0x5FF
15626 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
15631 SECTION NAME=CONFIG ROM=config
15636 SECTION NAME=bank0 RAM=gpr0
15639 SECTION NAME=bank1 RAM=gpr1
15642 SECTION NAME=bank2 RAM=gpr2
15645 SECTION NAME=bank3 RAM=gpr3
15648 SECTION NAME=bank4 RAM=gpr4
15651 SECTION NAME=bank5 RAM=gpr5
15654 The linker will recognise the section name set in the pragma statement and
15655 will position the variable at the memory bank set with the RAM field at
15656 the SECTION line in the linker script file.
15660 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
15667 There is one main header file that can be included to the source files using
15674 This header file contains the definitions for the processor special registers,
15675 so it is necessary if the source accesses them.
15676 It can be included by adding the following line in the beginning of the
15680 #include <pic18fregs.h>
15683 The specific microcontroller is selected within the pic18fregs.h automatically,
15684 so the same source can be used with a variety of devices.
15690 The libraries that PIC16
15691 \begin_inset LatexCommand \index{PIC16}
15695 port depends on are the microcontroller device libraries which contain
15696 the symbol definitions for the microcontroller special function registers.
15697 These libraries have the format pic18fxxxx.lib, where
15701 is the microcontroller identification number.
15702 The specific library is selected automatically by the compiler at link
15703 stage according to the selected device.
15706 Libraries are created with gplib which is part of the gputils package
15707 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
15717 The following memory models are supported by the PIC16 port:
15726 Memory model affects the default size of pointers within the source.
15727 The sizes are shown in the next table:
15731 \begin_inset Tabular
15732 <lyxtabular version="3" rows="3" columns="3">
15734 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15735 <column alignment="center" valignment="top" leftline="true" width="0">
15736 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15737 <row topline="true" bottomline="true">
15738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15743 Pointer sizes according to memory model
15746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15763 <row topline="true" bottomline="true">
15764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15772 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15789 <row topline="true" bottomline="true">
15790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15798 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
15806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15822 It is advisable that all sources within a project are compiled with the
15824 If one wants to override the default memory model, this can be done by
15825 declaring a pointer as
15834 Far selects large memory model's pointers, while near selects small memory
15838 The standard device libraries (see
15839 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
15843 ) contain no reference to pointers, so they can be used with both memory
15850 The stack implementation for the PIC16 port uses two indirect registers,
15853 \labelwidthstring 00.00.0000
15855 FSR1 is assigned as stack pointer
15857 \labelwidthstring 00.00.0000
15859 FSR2 is assigned as frame pointer
15862 The following stack models are supported by the PIC16 port
15875 model means that only the FSRxL byte is used to access stack and frame,
15882 uses both FSRxL and FSRxH registers.
15883 The following table shows the stack/frame pointers sizes according to stack
15884 model and the maximum space they can address:
15888 \begin_inset Tabular
15889 <lyxtabular version="3" rows="3" columns="3">
15891 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15892 <column alignment="center" valignment="top" leftline="true" width="0">
15893 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15894 <row topline="true" bottomline="true">
15895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15900 Stack & Frame pointer sizes according to stack model
15903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15911 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15920 <row topline="true">
15921 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15929 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15937 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15946 <row topline="true" bottomline="true">
15947 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15955 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15981 Currently stack and frame pointers should be initialized explicit by the
15982 user at the desired Data RAM position (see
15983 \begin_inset LatexCommand \ref{sub:PIC16_Pragmas}
15988 Uninitialized stack and frame pointers can result in unexpected behavior
15989 of the resulting binary.
15992 Function return values
15995 Return values from functions are placed to the appropriate registers following
15996 a modified Microchip policy optimized for SDCC.
15997 The following table shows these registers:
16001 \begin_inset Tabular
16002 <lyxtabular version="3" rows="6" columns="2">
16004 <column alignment="center" valignment="top" leftline="true" width="0">
16005 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16006 <row topline="true" bottomline="true">
16007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16015 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16020 destination register
16024 <row topline="true">
16025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16042 <row topline="true">
16043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16060 <row topline="true">
16061 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16069 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16078 <row topline="true">
16079 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16087 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16092 FSR0L:PRODH:PRODL:WREG
16096 <row topline="true" bottomline="true">
16097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16110 on stack, FSR0 points to the beginning
16124 When entering an interrupt, currently the PIC16
16125 \begin_inset LatexCommand \index{PIC16}
16129 port automatically saves the following registers:
16141 PROD (PRODL and PRODH)
16144 FSR0 (FSR0L and FSR0H)
16147 These registers are restored upon return from the interrupt routine.
16151 When entering a high priority interrupt WREG, STATUS and BSR are not explicit
16153 The hardware shadow registers for WREG, STATUS and BSR are used in these
16163 NOTE that when the _naked attribute is specified for an interrupt routine,
16164 then NO registers are stored or restored.
16170 Debugging with SDCDB
16171 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
16176 \begin_inset LatexCommand \index{sdcdb (debugger)}
16183 SDCC is distributed with a source level debugger
16184 \begin_inset LatexCommand \index{Debugger}
16189 The debugger uses a command line interface, the command repertoire of the
16190 debugger has been kept as close to gdb
16191 \begin_inset LatexCommand \index{gdb}
16195 (the GNU debugger) as possible.
16196 The configuration and build process is part of the standard compiler installati
16197 on, which also builds and installs the debugger in the target directory
16198 specified during configuration.
16199 The debugger allows you debug BOTH at the C source and at the ASM source
16201 Sdcdb is available on Unix platforms only.
16204 Compiling for Debugging
16218 \begin_inset LatexCommand \index{-\/-debug}
16222 option must be specified for all files for which debug information is to
16224 The complier generates a .adb file for each of these files.
16225 The linker creates the .cdb
16226 \begin_inset LatexCommand \index{<file>.cdb}
16231 \begin_inset LatexCommand \index{<file>.adb}
16235 files and the address information.
16236 This .cdb is used by the debugger.
16239 How the Debugger Works
16252 -debug option is specified the compiler generates extra symbol information
16253 some of which are put into the assembler source and some are put into the
16255 Then the linker creates the .cdb file from the individual .adb files with
16256 the address information for the symbols.
16257 The debugger reads the symbolic information generated by the compiler &
16258 the address information generated by the linker.
16259 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
16260 execution is controlled by the debugger.
16261 When a command is issued for the debugger, it translates it into appropriate
16262 commands for the simulator.
16265 Starting the Debugger
16268 The debugger can be started using the following command line.
16269 (Assume the file you are debugging has the file name foo).
16283 The debugger will look for the following files.
16286 foo.c - the source file.
16289 foo.cdb - the debugger symbol information file.
16292 foo.ihx - the Intel hex format
16293 \begin_inset LatexCommand \index{Intel hex format}
16300 Command Line Options.
16313 -directory=<source file directory> this option can used to specify the directory
16315 The debugger will look into the directory list specified for source, cdb
16317 The items in the directory list must be separated by ':', e.g.
16318 if the source files can be in the directories /home/src1 and /home/src2,
16329 -directory option should be -
16339 -directory=/home/src1:/home/src2.
16340 Note there can be no spaces in the option.
16344 -cd <directory> - change to the <directory>.
16347 -fullname - used by GUI front ends.
16350 -cpu <cpu-type> - this argument is passed to the simulator please see the
16351 simulator docs for details.
16354 -X <Clock frequency > this options is passed to the simulator please see
16355 the simulator docs for details.
16358 -s <serial port file> passed to simulator see the simulator docs for details.
16361 -S <serial in,out> passed to simulator see the simulator docs for details.
16364 -k <port number> passed to simulator see the simulator docs for details.
16370 As mentioned earlier the command interface for the debugger has been deliberatel
16371 y kept as close the GNU debugger gdb, as possible.
16372 This will help the integration with existing graphical user interfaces
16373 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
16374 If you use a graphical user interface for the debugger you can skip the
16376 \layout Subsubsection*
16378 break [line | file:line | function | file:function]
16381 Set breakpoint at specified line or function:
16390 sdcdb>break foo.c:100
16392 sdcdb>break funcfoo
16394 sdcdb>break foo.c:funcfoo
16395 \layout Subsubsection*
16397 clear [line | file:line | function | file:function ]
16400 Clear breakpoint at specified line or function:
16409 sdcdb>clear foo.c:100
16411 sdcdb>clear funcfoo
16413 sdcdb>clear foo.c:funcfoo
16414 \layout Subsubsection*
16419 Continue program being debugged, after breakpoint.
16420 \layout Subsubsection*
16425 Execute till the end of the current function.
16426 \layout Subsubsection*
16431 Delete breakpoint number 'n'.
16432 If used without any option clear ALL user defined break points.
16433 \layout Subsubsection*
16435 info [break | stack | frame | registers ]
16438 info break - list all breakpoints
16441 info stack - show the function call stack.
16444 info frame - show information about the current execution frame.
16447 info registers - show content of all registers.
16448 \layout Subsubsection*
16453 Step program until it reaches a different source line.
16454 Note: pressing <return> repeats the last command.
16455 \layout Subsubsection*
16460 Step program, proceeding through subroutine calls.
16461 \layout Subsubsection*
16466 Start debugged program.
16467 \layout Subsubsection*
16472 Print type information of the variable.
16473 \layout Subsubsection*
16478 print value of variable.
16479 \layout Subsubsection*
16484 load the given file name.
16485 Note this is an alternate method of loading file for debugging.
16486 \layout Subsubsection*
16491 print information about current frame.
16492 \layout Subsubsection*
16497 Toggle between C source & assembly source.
16498 \layout Subsubsection*
16500 ! simulator command
16503 Send the string following '!' to the simulator, the simulator response is
16505 Note the debugger does not interpret the command being sent to the simulator,
16506 so if a command like 'go' is sent the debugger can loose its execution
16507 context and may display incorrect values.
16508 \layout Subsubsection*
16515 My name is Bobby Brown"
16518 Interfacing with XEmacs
16519 \begin_inset LatexCommand \index{XEmacs}
16524 \begin_inset LatexCommand \index{Emacs}
16531 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
16532 sdcdb.el and sdcdbsrc.el.
16533 These two files can be found in the $(prefix)/bin directory after the installat
16535 These files need to be loaded into XEmacs for the interface to work.
16536 This can be done at XEmacs startup time by inserting the following into
16537 your '.xemacs' file (which can be found in your HOME directory):
16543 (load-file sdcdbsrc.el)
16549 .xemacs is a lisp file so the () around the command is REQUIRED.
16550 The files can also be loaded dynamically while XEmacs is running, set the
16551 environment variable 'EMACSLOADPATH' to the installation bin directory
16552 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
16553 To start the interface enter the following command:
16567 You will prompted to enter the file name to be debugged.
16572 The command line options that are passed to the simulator directly are bound
16573 to default values in the file sdcdbsrc.el.
16574 The variables are listed below, these values maybe changed as required.
16577 sdcdbsrc-cpu-type '51
16580 sdcdbsrc-frequency '11059200
16583 sdcdbsrc-serial nil
16586 The following is a list of key mapping for the debugger interface.
16597 ;;key\SpecialChar ~
16611 binding\SpecialChar ~
16635 ;;---\SpecialChar ~
16649 -------\SpecialChar ~
16691 sdcdb-next-from-src\SpecialChar ~
16719 sdcdb-back-from-src\SpecialChar ~
16747 sdcdb-cont-from-src\SpecialChar ~
16757 SDCDB continue command
16775 sdcdb-step-from-src\SpecialChar ~
16803 sdcdb-whatis-c-sexp\SpecialChar ~
16813 SDCDB ptypecommand for data at
16880 sdcdbsrc-delete\SpecialChar ~
16894 SDCDB Delete all breakpoints if no arg
16943 given or delete arg (C-u arg x)
16961 sdcdbsrc-frame\SpecialChar ~
16976 SDCDB Display current frame if no arg,
17025 given or display frame arg
17092 sdcdbsrc-goto-sdcdb\SpecialChar ~
17102 Goto the SDCDB output buffer
17120 sdcdb-print-c-sexp\SpecialChar ~
17131 SDCDB print command for data at
17198 sdcdbsrc-goto-sdcdb\SpecialChar ~
17208 Goto the SDCDB output buffer
17226 sdcdbsrc-mode\SpecialChar ~
17242 Toggles Sdcdbsrc mode (turns it off)
17257 sdcdb-finish-from-src\SpecialChar ~
17265 SDCDB finish command
17280 sdcdb-break\SpecialChar ~
17298 Set break for line with point
17313 sdcdbsrc-mode\SpecialChar ~
17329 Toggle Sdcdbsrc mode
17344 sdcdbsrc-srcmode\SpecialChar ~
17367 Here are a few guidelines that will help the compiler generate more efficient
17368 code, some of the tips are specific to this compiler others are generally
17369 good programming practice.
17372 Use the smallest data type to represent your data-value.
17373 If it is known in advance that the value is going to be less than 256 then
17374 use an 'unsigned char' instead of a 'short' or 'int'.
17375 Please note, that ANSI C requires both signed and unsigned chars to be
17376 promoted to 'signed int' before doing any operation.
17377 This promotion can be omitted, if the result is the same.
17378 The effect of the promotion rules together with the sign-extension is often
17385 unsigned char uc = 0xfe;
17387 if (uc * uc < 0) /* this is true! */
17406 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
17416 (unsigned char) -12 / (signed char) -3 = ...
17419 No, the result is not 4:
17424 (int) (unsigned char) -12 / (int) (signed char) -3 =
17426 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
17428 (int) 0x00f4 / (int) 0xfffd =
17430 (int) 0x00f4 / (int) 0xfffd =
17432 (int) 244 / (int) -3 =
17434 (int) -81 = (int) 0xffaf;
17437 Don't complain, that gcc gives you a different result.
17438 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
17439 Therefore the results are different.
17442 \begin_inset Quotes sld
17446 \begin_inset Quotes srd
17452 If well-defined overflow characteristics are important and negative values
17453 are not, or if you want to steer clear of sign-extension problems when
17454 manipulating bits or bytes, use one of the corresponding unsigned types.
17455 (Beware when mixing signed and unsigned values in expressions, though.)
17457 Although character types (especially unsigned char) can be used as "tiny"
17458 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
17459 ble sign extension and increased code size.
17463 Use unsigned when it is known in advance that the value is not going to
17465 This helps especially if you are doing division or multiplication, bit-shifting
17466 or are using an array index.
17469 NEVER jump into a LOOP.
17472 Declare the variables to be local
17473 \begin_inset LatexCommand \index{local variables}
17477 whenever possible, especially loop control variables (induction).
17480 Since the compiler does not always do implicit integral promotion, the programme
17481 r should do an explicit cast when integral promotion is required.
17484 Reducing the size of division, multiplication & modulus operations can reduce
17485 code size substantially.
17486 Take the following code for example.
17492 foobar(unsigned int p1, unsigned char ch)
17500 unsigned char ch1 = p1 % ch ;
17511 For the modulus operation the variable ch will be promoted to unsigned int
17512 first then the modulus operation will be performed (this will lead to a
17513 call to support routine _moduint()), and the result will be casted to a
17515 If the code is changed to
17520 foobar(unsigned int p1, unsigned char ch)
17528 unsigned char ch1 = (unsigned char)p1 % ch ;
17539 It would substantially reduce the code generated (future versions of the
17540 compiler will be smart enough to detect such optimization opportunities).
17544 Have a look at the assembly listing to get a
17545 \begin_inset Quotes sld
17549 \begin_inset Quotes srd
17552 for the code generation.
17556 \begin_inset LatexCommand \index{Tools}
17560 included in the distribution
17564 \begin_inset Tabular
17565 <lyxtabular version="3" rows="12" columns="3">
17567 <column alignment="center" valignment="top" leftline="true" width="0pt">
17568 <column alignment="center" valignment="top" leftline="true" width="0pt">
17569 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
17570 <row topline="true" bottomline="true">
17571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17596 <row topline="true">
17597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17610 Simulator for various architectures
17613 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17622 <row topline="true">
17623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17636 header file conversion
17639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17644 sdcc/support/scripts
17648 <row topline="true">
17649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17662 header file conversion
17665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17670 sdcc/support/scripts
17674 <row topline="true">
17675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17691 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17709 <row topline="true">
17710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17718 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17726 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17744 <row topline="true">
17745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17761 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17779 <row topline="true">
17780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17814 <row topline="true">
17815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17831 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17849 <row topline="true">
17850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17884 <row topline="true">
17885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17901 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17919 <row topline="true" bottomline="true">
17920 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17964 \begin_inset LatexCommand \index{Documentation}
17968 included in the distribution
17972 \begin_inset Tabular
17973 <lyxtabular version="3" rows="10" columns="2">
17975 <column alignment="left" valignment="top" leftline="true" width="0">
17976 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
17977 <row topline="true" bottomline="true">
17978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17991 Where to get / filename
17995 <row topline="true">
17996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18001 SDCC Compiler User Guide
18004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18009 You're reading it right now
18013 <row topline="true">
18014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18031 <row topline="true">
18032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18037 ASXXXX Assemblers and ASLINK Relocating Linker
18040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18045 sdcc/as/doc/asxhtm.html
18049 <row topline="true">
18050 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18055 SDCC regression test
18056 \begin_inset LatexCommand \index{Regression test}
18063 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18068 sdcc/doc/test_suite_spec.pdf
18072 <row topline="true">
18073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18081 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18090 <row topline="true">
18091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18096 Notes on debugging with sdcdb
18097 \begin_inset LatexCommand \index{sdcdb (debugger)}
18104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18109 sdcc/debugger/README
18113 <row topline="true">
18114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18119 Software simulator for microcontrollers
18122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18149 <row topline="true">
18150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18155 Temporary notes on the pic16
18156 \begin_inset LatexCommand \index{PIC16}
18163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18168 sdcc/src/pic16/NOTES
18172 <row topline="true" bottomline="true">
18173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18178 SDCC internal documentation (debugging file format)
18181 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18217 Related open source tools
18218 \begin_inset LatexCommand \index{Related tools}
18226 \begin_inset Tabular
18227 <lyxtabular version="3" rows="11" columns="3">
18229 <column alignment="center" valignment="top" leftline="true" width="0pt">
18230 <column alignment="block" valignment="top" leftline="true" width="30line%">
18231 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18232 <row topline="true" bottomline="true">
18233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18258 <row topline="true">
18259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18265 \begin_inset LatexCommand \index{gpsim (pic simulator)}
18272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18286 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
18294 <row topline="true">
18295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18301 \begin_inset LatexCommand \index{gputils (pic tools)}
18308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18322 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
18330 <row topline="true">
18331 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18353 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
18361 <row topline="true">
18362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18368 \begin_inset LatexCommand \index{indent (source formatting tool)}
18375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18380 Formats C source - Master of the white spaces
18383 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18389 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
18397 <row topline="true">
18398 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18404 \begin_inset LatexCommand \index{srecord (tool)}
18411 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18416 Object file conversion, checksumming, ...
18419 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18425 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
18433 <row topline="true">
18434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18440 \begin_inset LatexCommand \index{objdump (tool)}
18447 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18452 Object file conversion, ...
18455 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18460 Part of binutils (should be there anyway)
18464 <row topline="true">
18465 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18471 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
18478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18483 Source code documentation system
18486 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18492 \begin_inset LatexCommand \url{http://www.doxygen.org}
18500 <row topline="true">
18501 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18514 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
18517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18523 \begin_inset LatexCommand \url{http://www.kdevelop.org}
18531 <row topline="true">
18532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18538 \begin_inset LatexCommand \index{splint (syntax checking tool)}
18545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18550 Statically checks c sources (has anyone adapted splint for SDCC?)
18553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18559 \begin_inset LatexCommand \url{http://www.splint.org}
18567 <row topline="true" bottomline="true">
18568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18574 \begin_inset LatexCommand \index{ddd (debugger)}
18581 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18586 Debugger, serves nicely as GUI to sdcdb
18587 \begin_inset LatexCommand \index{sdcdb (debugger)}
18594 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18600 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
18617 Related documentation / recommended reading
18621 \begin_inset Tabular
18622 <lyxtabular version="3" rows="6" columns="3">
18624 <column alignment="center" valignment="top" leftline="true" width="0pt">
18625 <column alignment="block" valignment="top" leftline="true" width="30line%">
18626 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18627 <row topline="true" bottomline="true">
18628 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18644 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18653 <row topline="true">
18654 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18677 \begin_inset LatexCommand \index{C Reference card}
18684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18690 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
18698 <row topline="true">
18699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18721 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
18729 <row topline="true">
18730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18737 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18742 Latest datasheet of the target CPU
18745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18754 <row topline="true">
18755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18767 Revision history of datasheet
18770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18779 <row topline="true" bottomline="true">
18780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18795 Advanced Compiler Design and Implementation
18798 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18803 bookstore (very dedicated, probably read other books first)
18819 Some questions answered, some pointers given - it might be time to in turn
18827 can you solve your project with the selected microcontroller? Would you
18828 find out early or rather late that your target is too small/slow/whatever?
18829 Can you switch to a slightly better device if it doesn't fit?
18832 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
18833 and/or another programming language be more adequate? Would an operating
18834 system on the target device help?
18837 if you solved the problem, will the marketing department be happy?
18840 if the marketing department is happy, will customers be happy?
18843 if you're the project manager, marketing department and maybe even the customer
18844 in one person, have you tried to see the project from the outside?
18847 is the project done if you think it is done? Or is just that other interface/pro
18848 tocol/feature/configuration/option missing? How about website, manual(s),
18849 internationali(z|s)ation, packaging, labels, 2nd source for components,
18850 electromagnetic compatability/interference, documentation for production,
18851 production test software, update mechanism, patent issues?
18854 is your project adequately positioned in that magic triangle: fame, fortune,
18858 Maybe not all answers to these questions are known and some answers may
18863 , nevertheless knowing these questions may help you to avoid burnout
18869 burnout is bad for electronic devices, programmers and motorcycle tyres
18873 Chances are you didn't want to hear some of them...
18877 \begin_inset LatexCommand \index{Support}
18884 SDCC has grown to be a large project.
18885 The compiler alone (without the preprocessor, assembler and linker) is
18886 well over 100,000 lines of code (blank stripped).
18887 The open source nature of this project is a key to its continued growth
18889 You gain the benefit and support of many active software developers and
18891 Is SDCC perfect? No, that's why we need your help.
18892 The developers take pride in fixing reported bugs.
18893 You can help by reporting the bugs and helping other SDCC users.
18894 There are lots of ways to contribute, and we encourage you to take part
18895 in making SDCC a great software package.
18899 The SDCC project is hosted on the SDCC sourceforge site at
18900 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
18905 You'll find the complete set of mailing lists
18906 \begin_inset LatexCommand \index{Mailing list(s)}
18910 , forums, bug reporting system, patch submission
18911 \begin_inset LatexCommand \index{Patch submission}
18916 \begin_inset LatexCommand \index{download}
18920 area and cvs code repository
18921 \begin_inset LatexCommand \index{cvs code repository}
18929 \begin_inset LatexCommand \index{Bug reporting}
18934 \begin_inset LatexCommand \index{Reporting bugs}
18941 The recommended way of reporting bugs is using the infrastructure of the
18943 You can follow the status of bug reports there and have an overview about
18947 Bug reports are automatically forwarded to the developer mailing list and
18948 will be fixed ASAP.
18949 When reporting a bug, it is very useful to include a small test program
18950 (the smaller the better) which reproduces the problem.
18951 If you can isolate the problem by looking at the generated assembly code,
18952 this can be very helpful.
18953 Compiling your program with the -
18964 \begin_inset LatexCommand \index{-\/-dumpall}
18968 option can sometimes be useful in locating optimization problems.
18969 When reporting a bug please maker sure you:
18972 Attach the code you are compiling with SDCC.
18976 Specify the exact command you use to run SDCC, or attach your Makefile.
18980 Specify the SDCC version (type "
18986 "), your platform, and operating system.
18990 Provide an exact copy of any error message or incorrect output.
18994 Put something meaningful in the subject of your message.
18997 Please attempt to include these 5 important parts, as applicable, in all
18998 requests for support or when reporting any problems or bugs with SDCC.
18999 Though this will make your message lengthy, it will greatly improve your
19000 chance that SDCC users and developers will be able to help you.
19001 Some SDCC developers are frustrated by bug reports without code provided
19002 that they can use to reproduce and ultimately fix the problem, so please
19003 be sure to provide sample code if you are reporting a bug!
19006 Please have a short check that you are using a recent version of SDCC and
19007 the bug is not yet known.
19008 This is the link for reporting bugs:
19009 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
19016 Requesting Features
19017 \begin_inset LatexCommand \label{sub:Requesting-Features}
19022 \begin_inset LatexCommand \index{Feature request}
19027 \begin_inset LatexCommand \index{Requesting features}
19034 Like bug reports feature requests are forwarded to the developer mailing
19036 This is the link for requesting features:
19037 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
19047 Like bug reports contributed patches are forwarded to the developer mailing
19049 This is the link for submitting patches
19050 \begin_inset LatexCommand \index{Patch submission}
19055 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
19062 You need to specify some parameters to the
19066 command for the patches to be useful.
19067 If you modified more than one file a patch created f.e.
19072 \begin_inset Quotes sld
19075 diff -Naur unmodified_directory modified_directory >my_changes.patch
19076 \begin_inset Quotes srd
19082 will be fine, otherwise
19086 \begin_inset Quotes sld
19089 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
19090 \begin_inset Quotes srd
19103 These links should take you directly to the
19104 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
19114 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
19115 automated messages (mid 2003)
19119 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
19124 \begin_inset LatexCommand \index{Mailing list(s)}
19128 and forums are archived and searchable so if you are lucky someone already
19129 had a similar problem.
19130 While mails to the lists themselves are delivered promptly their web front
19131 end on sourceforge sometimes shows a severe time lag (up to several weeks),
19132 if you're seriously using SDCC please consider subscribing to the lists.
19138 You can follow the status of the cvs version
19139 \begin_inset LatexCommand \index{version}
19143 of SDCC by watching the Changelog
19144 \begin_inset LatexCommand \index{Changelog}
19148 in the cvs-repository
19153 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
19161 \begin_inset LatexCommand \index{Release policy}
19168 Historically there often were long delays between official releases and
19169 the sourceforge download area tends to get not updated at all.
19170 Excuses in the past might have referred to problems with live range analysis,
19171 but as this was fixed a while ago, the current problem is that another
19172 excuse has to be found.
19173 Kidding aside, we have to get better there! On the other hand there are
19174 daily snapshots available at
19175 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
19179 , and you can always build the very last version (hopefully with many bugs
19180 fixed, and features added) from the source code available at
19181 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
19189 \begin_inset LatexCommand \index{Examples}
19196 You'll find some small examples in the directory
19198 sdcc/device/examples/.
19201 More examples and libraries are available at
19203 The SDCC Open Knowledge Resource
19204 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
19211 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
19218 I did insert a reference to Paul's web site here although it seems rather
19219 dedicated to a specific 8032 board (I think it's okay because it f.e.
19220 shows LCD/Harddisc interface and has a free 8051 monitor.
19221 Independent 8032 board vendors face hard competition of heavily subsidized
19222 development boards anyway).
19225 Maybe we should include some links to real world applications.
19226 Preferably pointer to pointers (one for each architecture) so this stays
19231 \begin_inset LatexCommand \index{Quality control}
19238 The compiler is passed through nightly compile and build checks.
19244 \begin_inset LatexCommand \index{Regression test}
19248 check that SDCC itself compiles flawlessly on several platforms and checks
19249 the quality of the code generated by SDCC by running the code through simulator
19251 There is a separate document
19254 \begin_inset LatexCommand \index{Test suite}
19263 You'll find the test code in the directory
19265 sdcc/support/regression
19268 You can run these tests manually by running
19272 in this directory (or f.e.
19277 \begin_inset Quotes sld
19281 \begin_inset Quotes srd
19287 if you don't want to run the complete tests).
19288 The test code might also be interesting if you want to look for examples
19289 \begin_inset LatexCommand \index{Examples}
19293 checking corner cases of SDCC or if you plan to submit patches
19294 \begin_inset LatexCommand \index{Patch submission}
19301 The pic port uses a different set of regression tests, you'll find them
19304 sdcc/src/regression
19309 SDCC Technical Data
19313 \begin_inset LatexCommand \index{Optimizations}
19320 SDCC performs a host of standard optimizations in addition to some MCU specific
19325 Sub-expression Elimination
19326 \begin_inset LatexCommand \index{Subexpression elimination}
19333 The compiler does local and
19359 will be translated to
19371 Some subexpressions are not as obvious as the above example, e.g.:
19381 In this case the address arithmetic a->b[i] will be computed only once;
19382 the equivalent code in C would be.
19394 The compiler will try to keep these temporary variables in registers.
19397 Dead-Code Elimination
19398 \begin_inset LatexCommand \index{Dead-code elimination}
19419 i = 1; \SpecialChar ~
19428 global = 1;\SpecialChar ~
19441 global = 3;\SpecialChar ~
19470 \begin_inset LatexCommand \index{Copy propagation}
19526 Note: the dead stores created by this copy propagation will be eliminated
19527 by dead-code elimination.
19531 \begin_inset LatexCommand \index{Loop optimization}
19536 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
19543 Two types of loop optimizations are done by SDCC
19551 of loop induction variables.
19552 In addition to the strength reduction the optimizer marks the induction
19553 variables and the register allocator tries to keep the induction variables
19554 in registers for the duration of the loop.
19555 Because of this preference of the register allocator
19556 \begin_inset LatexCommand \index{Register allocation}
19560 , loop induction optimization causes an increase in register pressure, which
19561 may cause unwanted spilling of other temporary variables into the stack
19562 \begin_inset LatexCommand \index{stack}
19567 The compiler will generate a warning message when it is forced to allocate
19568 extra space either on the stack or data space.
19569 If this extra space allocation is undesirable then induction optimization
19570 can be eliminated either for the entire source file (with -
19580 -noinduction option) or for a given function only using #pragma\SpecialChar ~
19582 \begin_inset LatexCommand \index{\#pragma noinduction}
19595 for (i = 0 ; i < 100 ; i ++)
19611 for (i = 0; i < 100; i++)
19620 As mentioned previously some loop invariants are not as apparent, all static
19621 address computations are also moved out of the loop.
19626 \begin_inset LatexCommand \index{Strength reduction}
19630 , this optimization substitutes an expression by a cheaper expression:
19635 for (i=0;i < 100; i++)
19653 for (i=0;i< 100;i++) {
19659 ar[itemp1] = itemp2;
19676 The more expensive multiplication
19677 \begin_inset LatexCommand \index{Multiplication}
19681 is changed to a less expensive addition.
19685 \begin_inset LatexCommand \index{Loop reversing}
19692 This optimization is done to reduce the overhead of checking loop boundaries
19693 for every iteration.
19694 Some simple loops can be reversed and implemented using a
19695 \begin_inset Quotes eld
19698 decrement and jump if not zero
19699 \begin_inset Quotes erd
19703 SDCC checks for the following criterion to determine if a loop is reversible
19704 (note: more sophisticated compilers use data-dependency analysis to make
19705 this determination, SDCC uses a more simple minded analysis).
19708 The 'for' loop is of the form
19714 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
19724 The <for body> does not contain
19725 \begin_inset Quotes eld
19729 \begin_inset Quotes erd
19733 \begin_inset Quotes erd
19739 All goto's are contained within the loop.
19742 No function calls within the loop.
19745 The loop control variable <sym> is not assigned any value within the loop
19748 The loop control variable does NOT participate in any arithmetic operation
19752 There are NO switch statements in the loop.
19755 Algebraic Simplifications
19758 SDCC does numerous algebraic simplifications, the following is a small sub-set
19759 of these optimizations.
19764 i = j + 0;\SpecialChar ~
19768 /* changed to: */\SpecialChar ~
19774 i /= 2;\SpecialChar ~
19781 /* changed to: */\SpecialChar ~
19787 i = j - j;\SpecialChar ~
19791 /* changed to: */\SpecialChar ~
19797 i = j / 1;\SpecialChar ~
19801 /* changed to: */\SpecialChar ~
19808 Note the subexpressions
19809 \begin_inset LatexCommand \index{Subexpression}
19813 given above are generally introduced by macro expansions or as a result
19814 of copy/constant propagation.
19817 'switch' Statements
19818 \begin_inset LatexCommand \label{sub:'switch'-Statements}
19823 \begin_inset LatexCommand \index{switch statement}
19830 SDCC changes switch statements to jump tables
19831 \begin_inset LatexCommand \index{jump tables}
19835 when the following conditions are true.
19839 The case labels are in numerical sequence, the labels need not be in order,
19840 and the starting number need not be one or zero.
19846 switch(i) {\SpecialChar ~
19877 case 4: ...\SpecialChar ~
19909 case 5: ...\SpecialChar ~
19941 case 3: ...\SpecialChar ~
19973 case 6: ...\SpecialChar ~
20041 Both the above switch statements will be implemented using a jump-table.
20042 The example to the right side is slightly more efficient as the check for
20043 the lower boundary of the jump-table is not needed.
20047 The number of case labels is at least three, since it takes two conditional
20048 statements to handle the boundary conditions.
20051 The number of case labels is less than 84, since each label takes 3 bytes
20052 and a jump-table can be utmost 256 bytes long.
20055 Switch statements which have gaps in the numeric sequence or those that
20056 have more that 84 case labels can be split into more than one switch statement
20057 for efficient code generation, e.g.:
20107 If the above switch statement is broken down into two switch statements
20146 case 9:\SpecialChar ~
20153 case 10:\SpecialChar ~
20159 case 11:\SpecialChar ~
20165 case 12:\SpecialChar ~
20172 then both the switch statements will be implemented using jump-tables whereas
20173 the unmodified switch statement will not be.
20174 You might also consider inserting dummy cases 0 and 5 to 8 in this example.
20177 The pragma nojtbound
20178 \begin_inset LatexCommand \index{\#pragma nojtbound}
20182 can be used to turn off checking the
20195 It has no effect if a default label is supplied.
20196 Use of this pragma is dangerous: if the switch argument is not matched
20197 by a case statement the processor will happily jump into Nirvana.
20200 Bit-shifting Operations
20201 \begin_inset LatexCommand \index{Bit shifting}
20208 Bit shifting is one of the most frequently used operation in embedded programmin
20210 SDCC tries to implement bit-shift operations in the most efficient way
20226 generates the following code:
20243 In general SDCC will never setup a loop if the shift count is known.
20286 \begin_inset LatexCommand \index{Bit rotation}
20293 A special case of the bit-shift operation is bit rotation
20294 \begin_inset LatexCommand \index{rotating bits}
20298 , SDCC recognizes the following expression to be a left bit-rotation:
20308 char i;\SpecialChar ~
20319 /* unsigned is needed for rotation */
20324 i = ((i << 1) | (i >> 7));
20333 will generate the following code:
20352 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
20353 ns of this case will also be recognized as bit-rotation, i.e.:
20358 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
20361 Nibble and Byte Swapping
20364 Other special cases of the bit-shift operations are nibble or byte swapping
20365 \begin_inset LatexCommand \index{swapping nibbles/bytes}
20369 , SDCC recognizes the following expressions:
20392 i = ((i << 4) | (i >> 4));
20398 j = ((j << 8) | (j >> 8));
20401 and generates a swap instruction for the nibble swapping
20402 \begin_inset LatexCommand \index{Nibble swapping}
20406 or move instructions for the byte swapping
20407 \begin_inset LatexCommand \index{Byte swapping}
20413 \begin_inset Quotes sld
20417 \begin_inset Quotes srd
20420 example can be used to convert from little to big-endian or vice versa.
20421 If you want to change the endianness of a
20425 integer you have to cast to
20432 Note that SDCC stores numbers in little-endian
20438 Usually 8-bit processors don't care much about endianness.
20439 This is not the case for the standard 8051 which only has an instruction
20445 \begin_inset LatexCommand \index{DPTR}
20453 so little-endian is the more efficient byte order.
20457 \begin_inset LatexCommand \index{little-endian}
20462 \begin_inset LatexCommand \index{Endianness}
20467 lowest order first).
20471 \begin_inset LatexCommand \index{Highest Order Bit}
20478 It is frequently required to obtain the highest order bit of an integral
20479 type (long, int, short or char types).
20480 SDCC recognizes the following expression to yield the highest order bit
20481 and generates optimized code for it, e.g.:
20503 hob = (gint >> 15) & 1;
20513 will generate the following code:
20546 000A E5*01\SpecialChar ~
20573 000C 23\SpecialChar ~
20604 000D 54 01\SpecialChar ~
20631 000F F5*02\SpecialChar ~
20659 Variations of this case however will
20664 It is a standard C expression, so I heartily recommend this be the only
20665 way to get the highest order bit, (it is portable).
20666 Of course it will be recognized even if it is embedded in other expressions,
20672 xyz = gint + ((gint >> 15) & 1);
20675 will still be recognized.
20679 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
20684 \begin_inset LatexCommand \index{Peephole optimizer}
20691 The compiler uses a rule based, pattern matching and re-writing mechanism
20692 for peep-hole optimization.
20697 a peep-hole optimizer by Christopher W.
20698 Fraser (cwfraser@microsoft.com).
20699 A default set of rules are compiled into the compiler, additional rules
20700 may be added with the
20713 \begin_inset LatexCommand \index{-\/-peep-file}
20720 The rule language is best illustrated with examples.
20744 The above rule will change the following assembly
20745 \begin_inset LatexCommand \index{Assembler routines}
20767 Note: All occurrences of a
20771 (pattern variable) must denote the same string.
20772 With the above rule, the assembly sequence:
20782 will remain unmodified.
20786 Other special case optimizations may be added by the user (via
20802 some variants of the 8051 MCU
20803 \begin_inset LatexCommand \index{MCS51 variants}
20816 The following two rules will change all
20835 replace { lcall %1 } by { acall %1 }
20837 replace { ljmp %1 } by { ajmp %1 }
20842 inline-assembler code
20844 is also passed through the peep hole optimizer, thus the peephole optimizer
20845 can also be used as an assembly level macro expander.
20846 The rules themselves are MCU dependent whereas the rule language infra-structur
20847 e is MCU independent.
20848 Peephole optimization rules for other MCU can be easily programmed using
20853 The syntax for a rule is as follows:
20858 rule := replace [ restart ] '{' <assembly sequence> '
20896 <assembly sequence> '
20914 '}' [if <functionName> ] '
20919 <assembly sequence> := assembly instruction (each instruction including
20920 labels must be on a separate line).
20924 The optimizer will apply to the rules one by one from the top in the sequence
20925 of their appearance, it will terminate when all rules are exhausted.
20926 If the 'restart' option is specified, then the optimizer will start matching
20927 the rules again from the top, this option for a rule is expensive (performance)
20928 , it is intended to be used in situations where a transformation will trigger
20929 the same rule again.
20930 An example of this (not a good one, it has side effects) is the following
20953 Note that the replace pattern cannot be a blank, but can be a comment line.
20954 Without the 'restart' option only the innermost 'pop' 'push' pair would
20955 be eliminated, i.e.:
20985 the restart option the rule will be applied again to the resulting code
20986 and then all the pop-push pairs will be eliminated to yield:
20996 A conditional function can be attached to a rule.
20997 Attaching rules are somewhat more involved, let me illustrate this with
21024 The optimizer does a look-up of a function name table defined in function
21029 in the source file SDCCpeeph.c, with the name
21034 If it finds a corresponding entry the function is called.
21035 Note there can be no parameters specified for these functions, in this
21040 is crucial, since the function
21044 expects to find the label in that particular variable (the hash table containin
21045 g the variable bindings is passed as a parameter).
21046 If you want to code more such functions, take a close look at the function
21047 labelInRange and the calling mechanism in source file SDCCpeeph.c.
21048 Currently implemented are
21050 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
21051 24bitMode, portIsDS390, 24bitModeAndPortDS390
21060 I know this whole thing is a little kludgey, but maybe some day we will
21061 have some better means.
21062 If you are looking at this file, you will see the default rules that are
21063 compiled into the compiler, you can add your own rules in the default set
21064 there if you get tired of specifying the -
21078 \begin_inset LatexCommand \index{ANSI-compliance}
21083 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
21090 Deviations from the compliance:
21093 functions are not always reentrant
21094 \begin_inset LatexCommand \index{reentrant}
21101 structures cannot be assigned values directly, cannot be passed as function
21102 parameters or assigned to each other and cannot be a return value from
21129 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
21140 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
21162 return rets;/* is invalid in SDCC although allowed in ANSI */
21169 \begin_inset LatexCommand \index{long long (not supported)}
21174 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
21182 \begin_inset LatexCommand \index{double (not supported)}
21186 ' precision floating point
21187 \begin_inset LatexCommand \index{Floating point support}
21194 No support for setjmp
21195 \begin_inset LatexCommand \index{setjmp (not supported)}
21200 \begin_inset LatexCommand \index{longjmp (not supported)}
21208 \begin_inset LatexCommand \index{K\&R style}
21212 function declarations are NOT allowed.
21218 foo(i,j) /* this old style of function declarations */
21220 int i,j; /* are valid in ANSI but not valid in SDCC */
21235 Cyclomatic Complexity
21236 \begin_inset LatexCommand \index{Cyclomatic complexity}
21243 Cyclomatic complexity of a function is defined as the number of independent
21244 paths the program can take during execution of the function.
21245 This is an important number since it defines the number test cases you
21246 have to generate to validate the function.
21247 The accepted industry standard for complexity number is 10, if the cyclomatic
21248 complexity reported by SDCC exceeds 10 you should think about simplification
21249 of the function logic.
21250 Note that the complexity level is not related to the number of lines of
21251 code in a function.
21252 Large functions can have low complexity, and small functions can have large
21258 SDCC uses the following formula to compute the complexity:
21263 complexity = (number of edges in control flow graph) - (number of nodes
21264 in control flow graph) + 2;
21268 Having said that the industry standard is 10, you should be aware that in
21269 some cases it be may unavoidable to have a complexity level of less than
21271 For example if you have switch statement with more than 10 case labels,
21272 each case label adds one to the complexity level.
21273 The complexity level is by no means an absolute measure of the algorithmic
21274 complexity of the function, it does however provide a good starting point
21275 for which functions you might look at for further optimization.
21278 Retargetting for other Processors
21281 The issues for retargetting the compiler are far too numerous to be covered
21283 What follows is a brief description of each of the seven phases of the
21284 compiler and its MCU dependency.
21287 Parsing the source and building the annotated parse tree.
21288 This phase is largely MCU independent (except for the language extensions).
21289 Syntax & semantic checks are also done in this phase, along with some initial
21290 optimizations like back patching labels and the pattern matching optimizations
21291 like bit-rotation etc.
21294 The second phase involves generating an intermediate code which can be easy
21295 manipulated during the later phases.
21296 This phase is entirely MCU independent.
21297 The intermediate code generation assumes the target machine has unlimited
21298 number of registers, and designates them with the name iTemp.
21299 The compiler can be made to dump a human readable form of the code generated
21313 This phase does the bulk of the standard optimizations and is also MCU independe
21315 This phase can be broken down into several sub-phases:
21319 Break down intermediate code (iCode) into basic blocks.
21321 Do control flow & data flow analysis on the basic blocks.
21323 Do local common subexpression elimination, then global subexpression elimination
21325 Dead code elimination
21329 If loop optimizations caused any changes then do 'global subexpression eliminati
21330 on' and 'dead code elimination' again.
21333 This phase determines the live-ranges; by live range I mean those iTemp
21334 variables defined by the compiler that still survive after all the optimization
21336 Live range analysis
21337 \begin_inset LatexCommand \index{Live range analysis}
21341 is essential for register allocation, since these computation determines
21342 which of these iTemps will be assigned to registers, and for how long.
21345 Phase five is register allocation.
21346 There are two parts to this process.
21350 The first part I call 'register packing' (for lack of a better term).
21351 In this case several MCU specific expression folding is done to reduce
21356 The second part is more MCU independent and deals with allocating registers
21357 to the remaining live ranges.
21358 A lot of MCU specific code does creep into this phase because of the limited
21359 number of index registers available in the 8051.
21362 The Code generation phase is (unhappily), entirely MCU dependent and very
21363 little (if any at all) of this code can be reused for other MCU.
21364 However the scheme for allocating a homogenized assembler operand for each
21365 iCode operand may be reused.
21368 As mentioned in the optimization section the peep-hole optimizer is rule
21369 based system, which can reprogrammed for other MCUs.
21373 \begin_inset LatexCommand \index{Compiler internals}
21380 The anatomy of the compiler
21381 \begin_inset LatexCommand \label{sub:The-anatomy-of}
21390 This is an excerpt from an article published in Circuit Cellar Magazine
21392 It's a little outdated (the compiler is much more efficient now and user/develo
21393 per friendly), but pretty well exposes the guts of it all.
21399 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
21400 It is fairly easy to retarget for other 8-bit MCU.
21401 Here we take a look at some of the internals of the compiler.
21406 \begin_inset LatexCommand \index{Parsing}
21413 Parsing the input source file and creating an AST (Annotated Syntax Tree
21414 \begin_inset LatexCommand \index{Annotated syntax tree}
21419 This phase also involves propagating types (annotating each node of the
21420 parse tree with type information) and semantic analysis.
21421 There are some MCU specific parsing rules.
21422 For example the storage classes, the extended storage classes are MCU specific
21423 while there may be a xdata storage class for 8051 there is no such storage
21424 class for z80 or Atmel AVR.
21425 SDCC allows MCU specific storage class extensions, i.e.
21426 xdata will be treated as a storage class specifier when parsing 8051 C
21427 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
21432 \begin_inset LatexCommand \index{iCode}
21439 Intermediate code generation.
21440 In this phase the AST is broken down into three-operand form (iCode).
21441 These three operand forms are represented as doubly linked lists.
21442 ICode is the term given to the intermediate form generated by the compiler.
21443 ICode example section shows some examples of iCode generated for some simple
21444 C source functions.
21448 \begin_inset LatexCommand \index{Optimizations}
21455 Bulk of the target independent optimizations is performed in this phase.
21456 The optimizations include constant propagation, common sub-expression eliminati
21457 on, loop invariant code movement, strength reduction of loop induction variables
21458 and dead-code elimination.
21461 Live range analysis
21462 \begin_inset LatexCommand \index{Live range analysis}
21469 During intermediate code generation phase, the compiler assumes the target
21470 machine has infinite number of registers and generates a lot of temporary
21472 The live range computation determines the lifetime of each of these compiler-ge
21473 nerated temporaries.
21474 A picture speaks a thousand words.
21475 ICode example sections show the live range annotations for each of the
21477 It is important to note here, each iCode is assigned a number in the order
21478 of its execution in the function.
21479 The live ranges are computed in terms of these numbers.
21480 The from number is the number of the iCode which first defines the operand
21481 and the to number signifies the iCode which uses this operand last.
21484 Register Allocation
21485 \begin_inset LatexCommand \index{Register allocation}
21492 The register allocation determines the type and number of registers needed
21494 In most MCUs only a few registers can be used for indirect addressing.
21495 In case of 8051 for example the registers R0 & R1 can be used to indirectly
21496 address the internal ram and DPTR to indirectly address the external ram.
21497 The compiler will try to allocate the appropriate register to pointer variables
21499 ICode example section shows the operands annotated with the registers assigned
21501 The compiler will try to keep operands in registers as much as possible;
21502 there are several schemes the compiler uses to do achieve this.
21503 When the compiler runs out of registers the compiler will check to see
21504 if there are any live operands which is not used or defined in the current
21505 basic block being processed, if there are any found then it will push that
21506 operand and use the registers in this block, the operand will then be popped
21507 at the end of the basic block.
21511 There are other MCU specific considerations in this phase.
21512 Some MCUs have an accumulator; very short-lived operands could be assigned
21513 to the accumulator instead of a general-purpose register.
21519 Figure II gives a table of iCode operations supported by the compiler.
21520 The code generation involves translating these operations into corresponding
21521 assembly code for the processor.
21522 This sounds overly simple but that is the essence of code generation.
21523 Some of the iCode operations are generated on a MCU specific manner for
21524 example, the z80 port does not use registers to pass parameters so the
21525 SEND and RECV iCode operations will not be generated, and it also does
21526 not support JUMPTABLES.
21533 <Where is Figure II ?>
21537 \begin_inset LatexCommand \index{iCode}
21544 This section shows some details of iCode.
21545 The example C code does not do anything useful; it is used as an example
21546 to illustrate the intermediate code generated by the compiler.
21558 /* This function does nothing useful.
21565 for the purpose of explaining iCode */
21568 short function (data int *x)
21576 short i=10; \SpecialChar ~
21578 /* dead initialization eliminated */
21583 short sum=10; /* dead initialization eliminated */
21596 while (*x) *x++ = *p++;
21610 /* compiler detects i,j to be induction variables */
21614 for (i = 0, j = 10 ; i < 10 ; i++, j
21640 mul += i * 3; \SpecialChar ~
21642 /* this multiplication remains */
21648 gint += j * 3;\SpecialChar ~
21650 /* this multiplication changed to addition */
21664 In addition to the operands each iCode contains information about the filename
21665 and line it corresponds to in the source file.
21666 The first field in the listing should be interpreted as follows:
21671 Filename(linenumber: iCode Execution sequence number : ICode hash table
21672 key : loop depth of the iCode).
21677 Then follows the human readable form of the ICode operation.
21678 Each operand of this triplet form can be of three basic types a) compiler
21679 generated temporary b) user defined variable c) a constant value.
21680 Note that local variables and parameters are replaced by compiler generated
21683 \begin_inset LatexCommand \index{Live range analysis}
21687 are computed only for temporaries (i.e.
21688 live ranges are not computed for global variables).
21690 \begin_inset LatexCommand \index{Register allocation}
21694 are allocated for temporaries only.
21695 Operands are formatted in the following manner:
21700 Operand Name [lr live-from : live-to ] { type information } [ registers
21706 As mentioned earlier the live ranges are computed in terms of the execution
21707 sequence number of the iCodes, for example
21709 the iTemp0 is live from (i.e.
21710 first defined in iCode with execution sequence number 3, and is last used
21711 in the iCode with sequence number 5).
21712 For induction variables such as iTemp21 the live range computation extends
21713 the lifetime from the start to the end of the loop.
21715 The register allocator used the live range information to allocate registers,
21716 the same registers may be used for different temporaries if their live
21717 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
21718 iTemp17 since their live ranges do not overlap.
21719 In addition the allocator also takes into consideration the type and usage
21720 of a temporary, for example itemp6 is a pointer to near space and is used
21721 as to fetch data from (i.e.
21722 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
21723 Some short lived temporaries are allocated to special registers which have
21724 meaning to the code generator e.g.
21725 iTemp13 is allocated to a pseudo register CC which tells the back end that
21726 the temporary is used only for a conditional jump the code generation makes
21727 use of this information to optimize a compare and jump ICode.
21729 There are several loop optimizations
21730 \begin_inset LatexCommand \index{Loop optimization}
21734 performed by the compiler.
21735 It can detect induction variables iTemp21(i) and iTemp23(j).
21736 Also note the compiler does selective strength reduction
21737 \begin_inset LatexCommand \index{Strength reduction}
21742 the multiplication of an induction variable in line 18 (gint = j * 3) is
21743 changed to addition, a new temporary iTemp17 is allocated and assigned
21744 a initial value, a constant 3 is then added for each iteration of the loop.
21745 The compiler does not change the multiplication
21746 \begin_inset LatexCommand \index{Multiplication}
21750 in line 17 however since the processor does support an 8 * 8 bit multiplication.
21752 Note the dead code elimination
21753 \begin_inset LatexCommand \index{Dead-code elimination}
21757 optimization eliminated the dead assignments in line 7 & 8 to I and sum
21765 Sample.c (5:1:0:0) _entry($9) :
21770 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
21775 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
21780 Sample.c(11:4:53:0) preHeaderLbl0($11) :
21785 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
21791 Sample.c(11:6:5:1) _whilecontinue_0($1) :
21796 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
21802 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
21807 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
21813 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
21819 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
21825 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
21831 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
21832 * int}[r0] + 0x2 {short}
21837 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
21842 Sample.c(11:17:21:0)_whilebreak_0($3) :
21847 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
21852 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
21857 Sample.c(15:20:54:0)preHeaderLbl1($13) :
21862 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
21867 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
21872 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
21877 Sample.c(15:24:26:1)_forcond_0($4) :
21882 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
21888 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
21893 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
21894 + ITemp21 [lr21:38]{short}[r4]
21899 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
21905 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
21906 + iTemp15 [lr29:30]{short}[r1]
21911 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
21917 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
21923 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
21929 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
21935 Sample.c(19:38:47:1) goto _forcond_0($4)
21940 Sample.c(19:39:48:0)_forbreak_0($7) :
21945 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
21946 + ITemp11 [lr19:40]{short}[r3]
21951 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
21956 Sample.c(20:42:51:0)_return($8) :
21961 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
21967 Finally the code generated for this function:
22008 ; ----------------------------------------------
22013 ; function function
22018 ; ----------------------------------------------
22028 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
22040 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
22052 ;_whilecontinue_0($1) :
22062 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
22067 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22126 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
22145 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
22192 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
22232 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
22258 ; iTemp6 [lr5:16]{_near * int}[r0] =
22263 ; iTemp6 [lr5:16]{_near * int}[r0] +
22280 ; goto _whilecontinue_0($1)
22292 ; _whilebreak_0($3) :
22302 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
22314 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
22326 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
22338 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
22357 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
22386 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
22391 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
22436 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
22441 ; iTemp21 [lr21:38]{short}[r4]
22467 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
22500 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
22505 ; iTemp15 [lr29:30]{short}[r1]
22524 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
22571 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
22618 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
22630 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
22644 cjne r5,#0xff,00104$
22656 ; goto _forcond_0($4)
22668 ; _forbreak_0($7) :
22678 ; ret iTemp24 [lr40:41]{short}
22721 A few words about basic block successors, predecessors and dominators
22724 Successors are basic blocks
22725 \begin_inset LatexCommand \index{Basic blocks}
22729 that might execute after this basic block.
22731 Predecessors are basic blocks that might execute before reaching this basic
22734 Dominators are basic blocks that WILL execute before reaching this basic
22768 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
22771 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
22774 c) domVect of [BB4] = BB1 ...
22775 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
22783 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
22793 Thanks to all the other volunteer developers who have helped with coding,
22794 testing, web-page creation, distribution sets, etc.
22795 You know who you are :-)
22802 This document was initially written by Sandeep Dutta
22805 All product names mentioned herein may be trademarks
22806 \begin_inset LatexCommand \index{Trademarks}
22810 of their respective companies.
22817 To avoid confusion, the installation and building options for SDCC itself
22818 (chapter 2) are not part of the index.
22822 \begin_inset LatexCommand \printindex{}