1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
6 pdftitle={SDCC Compiler User Guide},
7 pdfauthor={SDCC development team},
8 pdfsubject={installation, user manual},
9 pdfkeywords={8032, 8051, ansi, c, compiler, CPU, DS390,
10 embedded, GPL, manual, mcs51, PIC, small, Z80},
12 linkcolor=blue] {hyperref}
16 \inputencoding default
19 \paperfontsize default
21 \papersize letterpaper
26 \use_numerical_citations 0
27 \paperorientation portrait
34 \paragraph_separation indent
36 \quotes_language swedish
44 Please note: double dashed longoptions (e.g.
45 --version) are written this way: -
59 three consecutive dashes simply result in a long resp.
63 Architecture specific stuff (like memory models, code examples) should maybe
67 into seperate sections/chapters/appendices (it is hard to document PIC or
71 a 8051 centered document)
74 SDCC Compiler User Guide
77 The strings enclosed in $ are automatically updated by cvs:
91 \begin_inset LatexCommand \tableofcontents{}
108 is a Freeware, retargettable, optimizing ANSI-C compiler by
112 designed for 8 bit Microprocessors.
113 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
115 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052 CPU}
119 , etc), Dallas DS80C390 variants, Motorola HC08 and Zilog Z80 based MCUs
121 It can be retargetted for other microprocessors, support for Microchip
122 PIC, Atmel AVR is under development.
123 The entire source code for the compiler is distributed under GPL.
124 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
125 SDCC has extensive language extensions suitable for utilizing various microcont
126 rollers and underlying hardware effectively.
131 In addition to the MCU specific optimizations SDCC also does a host of standard
135 global sub expression elimination,
138 loop optimizations (loop invariant, strength reduction of induction variables
142 constant folding & propagation,
148 dead code elimination
158 For the back-end SDCC uses a global register allocation scheme which should
159 be well suited for other 8 bit MCUs.
164 The peep hole optimizer uses a rule based substitution mechanism which is
170 Supported data-types are:
173 char (8 bits, 1 byte),
176 short and int (16 bits, 2 bytes),
179 long (32 bit, 4 bytes)
186 The compiler also allows
188 inline assembler code
190 to be embedded anywhere in a function.
191 In addition, routines developed in assembly can also be called.
195 SDCC also provides an option (-
205 -cyclomatic) to report the relative complexity of a function.
206 These functions can then be further optimized, or hand coded in assembly
212 SDCC also comes with a companion source level debugger SDCDB, the debugger
213 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
218 The latest version can be downloaded from
219 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
229 Please note: the compiler will probably always be some steps ahead of this
234 \begin_inset LatexCommand \index{Status of documentation}
244 Obviously this has pros and cons
253 All packages used in this compiler system are
261 ; source code for all the sub-packages (pre-processor, assemblers, linkers
262 etc) is distributed with the package.
263 This documentation is maintained using a freeware word processor (LyX).
265 This program is free software; you can redistribute it and/or modify it
266 under the terms of the GNU General Public License
267 \begin_inset LatexCommand \index{GNU General Public License, GPL}
271 as published by the Free Software Foundation; either version 2, or (at
272 your option) any later version.
273 This program is distributed in the hope that it will be useful, but WITHOUT
274 ANY WARRANTY; without even the implied warranty
275 \begin_inset LatexCommand \index{warranty}
279 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
280 See the GNU General Public License for more details.
281 You should have received a copy of the GNU General Public License along
282 with this program; if not, write to the Free Software Foundation, 59 Temple
283 Place - Suite 330, Boston, MA 02111-1307, USA.
284 In other words, you are welcome to use, share and improve this program.
285 You are forbidden to forbid anyone else to use, share and improve what
287 Help stamp out software-hoarding!
290 Typographic conventions
291 \begin_inset LatexCommand \index{Typographic conventions}
298 Throughout this manual, we will use the following convention.
299 Commands you have to type in are printed in
307 Code samples are printed in
312 Interesting items and new terms are printed in
317 Compatibility with previous versions
320 This version has numerous bug fixes compared with the previous version.
321 But we also introduced some incompatibilities with older versions.
322 Not just for the fun of it, but to make the compiler more stable, efficient
324 \begin_inset LatexCommand \index{ANSI-compliance}
329 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
333 for ANSI-Compliance).
339 short is now equivalent to int (16 bits), it used to be equivalent to char
340 (8 bits) which is not ANSI compliant
343 the default directory for gcc-builds where include, library and documentation
344 files are stored is now in /usr/local/share
347 char type parameters to vararg functions are casted to int unless explicitly
364 will push a as an int and as a char resp.
377 -regextend has been removed
390 -noregparms has been removed
403 -stack-after-data has been removed
408 <pending: more incompatibilities?>
414 What do you need before you start installation of SDCC? A computer, and
416 The preferred method of installation is to compile SDCC from source using
418 For Windows some pre-compiled binary distributions are available for your
420 You should have some experience with command line tools and compiler use.
426 The SDCC home page at
427 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
431 is a great place to find distribution sets.
432 You can also find links to the user mailing lists that offer help or discuss
433 SDCC with other SDCC users.
434 Web links to other SDCC related sites can also be found here.
435 This document can be found in the DOC directory of the source package as
437 Some of the other tools (simulator and assembler) included with SDCC contain
438 their own documentation and can be found in the source distribution.
439 If you want the latest unreleased software, the complete source package
440 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
443 Wishes for the future
446 There are (and always will be) some things that could be done.
447 Here are some I can think of:
454 char KernelFunction3(char p) at 0x340;
462 \begin_inset LatexCommand \index{code banking (not supported)}
472 If you can think of some more, please see the chapter
473 \begin_inset LatexCommand \ref{sub:Requesting-Features}
477 about filing feature requests
478 \begin_inset LatexCommand \index{Requesting features}
483 \begin_inset LatexCommand \index{Feature request}
493 \begin_inset LatexCommand \index{Installation}
500 For most users it is sufficient to skip to either section
501 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
506 \begin_inset LatexCommand \ref{sub:Windows-Install}
511 More detailled instructions follow below.
515 \begin_inset LatexCommand \index{Options SDCC configuration}
522 The install paths, search paths and other options are defined when running
524 The defaults can be overridden by:
526 \labelwidthstring 00.00.0000
538 -prefix see table below
540 \labelwidthstring 00.00.0000
552 -exec_prefix see table below
554 \labelwidthstring 00.00.0000
566 -bindir see table below
568 \labelwidthstring 00.00.0000
580 -datadir see table below
582 \labelwidthstring 00.00.0000
584 docdir environment variable, see table below
586 \labelwidthstring 00.00.0000
588 include_dir_suffix environment variable, see table below
590 \labelwidthstring 00.00.0000
592 lib_dir_suffix environment variable, see table below
594 \labelwidthstring 00.00.0000
596 sdccconf_h_dir_separator environment variable, either / or
601 This character will only be used in sdccconf.h; don't forget it's a C-header,
602 therefore a double-backslash is needed there.
604 \labelwidthstring 00.00.0000
616 -disable-mcs51-port Excludes the Intel mcs51 port
618 \labelwidthstring 00.00.0000
630 -disable-gbz80-port Excludes the Gameboy gbz80 port
632 \labelwidthstring 00.00.0000
644 -disable-z80-port Excludes the z80 port
646 \labelwidthstring 00.00.0000
658 -disable-avr-port Excludes the AVR port
660 \labelwidthstring 00.00.0000
672 -disable-ds390-port Excludes the DS390 port
674 \labelwidthstring 00.00.0000
686 -disable-hc08-port Excludes the HC08 port
688 \labelwidthstring 00.00.0000
700 -disable-pic-port Excludes the PIC port
702 \labelwidthstring 00.00.0000
714 -disable-xa51-port Excludes the XA51 port
716 \labelwidthstring 00.00.0000
728 -disable-ucsim Disables configuring and building of ucsim
730 \labelwidthstring 00.00.0000
742 -disable-device-lib-build Disables automatically building device libraries
744 \labelwidthstring 00.00.0000
756 -disable-packihx Disables building packihx
758 \labelwidthstring 00.00.0000
770 -enable-libgc Use the Bohem memory allocator.
771 Lower runtime footprint.
774 Furthermore the environment variables CC, CFLAGS, ...
775 the tools and their arguments can be influenced.
776 Please see `configure -
786 -help` and the man/info pages of `configure` for details.
790 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
791 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
792 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
793 At the moment it's not possible to change the default settings (it was
794 simply never required).
798 These configure options are compiled into the binaries, and can only be
799 changed by rerunning 'configure' and recompiling SDCC.
800 The configure options are written in
804 to distinguish them from run time environment variables (see section search
810 \begin_inset Quotes sld
814 \begin_inset Quotes srd
817 are used by the SDCC team to build the official Win32 binaries.
818 The SDCC team uses Mingw32 to build the official Windows binaries, because
825 a gcc compiler and last but not least
828 the binaries can be built by cross compiling on Sourceforge's compile farm.
831 See the examples, how to pass the Win32 settings to 'configure'.
832 The other Win32 builds using Borland, VC or whatever don't use 'configure',
833 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
845 <lyxtabular version="3" rows="8" columns="3">
847 <column alignment="block" valignment="top" leftline="true" width="0in">
848 <column alignment="block" valignment="top" leftline="true" width="0in">
849 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
850 <row topline="true" bottomline="true">
851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
877 <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">
907 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
927 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
939 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
961 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
987 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
999 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1010 <row topline="true">
1011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1048 <row topline="true">
1049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1067 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1076 <row topline="true" bottomline="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">
1113 'configure' also computes relative paths.
1114 This is needed for full relocatability of a binary package and to complete
1115 search paths (see section search paths below):
1121 \begin_inset Tabular
1122 <lyxtabular version="3" rows="4" columns="3">
1124 <column alignment="block" valignment="top" leftline="true" width="0in">
1125 <column alignment="block" valignment="top" leftline="true" width="0in">
1126 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1127 <row topline="true" bottomline="true">
1128 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1153 <row topline="true" bottomline="true">
1154 <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 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">
1261 \begin_inset Quotes srd
1265 \begin_inset Quotes srd
1279 \begin_inset Quotes srd
1283 \begin_inset Quotes srd
1311 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1320 \begin_inset Quotes srd
1323 i586-mingw32msvc-gcc
1324 \begin_inset Quotes srd
1328 \begin_inset Quotes srd
1331 i586-mingw32msvc-g++
1332 \begin_inset Quotes srd
1340 \begin_inset Quotes srd
1343 i586-mingw32msvc-ranlib
1344 \begin_inset Quotes srd
1352 \begin_inset Quotes srd
1355 i586-mingw32msvc-strip
1356 \begin_inset Quotes srd
1374 \begin_inset Quotes srd
1378 \begin_inset Quotes srd
1396 \begin_inset Quotes srd
1400 \begin_inset Quotes srd
1408 \begin_inset Quotes srd
1412 \begin_inset Quotes srd
1420 \begin_inset Quotes srd
1424 \begin_inset Quotes srd
1432 \begin_inset Quotes srd
1436 \begin_inset Quotes srd
1443 sdccconf_h_dir_separator=
1444 \begin_inset Quotes srd
1456 \begin_inset Quotes srd
1473 -disable-device-lib-build
1501 -host=i586-mingw32msvc -
1511 -build=unknown-unknown-linux-gnu
1515 \begin_inset Quotes sld
1519 \begin_inset Quotes srd
1522 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1531 \begin_inset Quotes srd
1535 \begin_inset Quotes srd
1543 \begin_inset Quotes srd
1547 \begin_inset Quotes srd
1565 \begin_inset Quotes srd
1569 \begin_inset Quotes srd
1587 \begin_inset Quotes srd
1591 \begin_inset Quotes srd
1599 \begin_inset Quotes srd
1603 \begin_inset Quotes srd
1611 \begin_inset Quotes srd
1615 \begin_inset Quotes srd
1623 \begin_inset Quotes srd
1627 \begin_inset Quotes srd
1634 sdccconf_h_dir_separator=
1635 \begin_inset Quotes srd
1647 \begin_inset Quotes srd
1667 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1678 -C' turns on caching, which gives a little bit extra speed.
1679 However if options are changed, it can be necessary to delete the config.cache
1684 \begin_inset LatexCommand \label{sub:Install-paths}
1689 \begin_inset LatexCommand \index{Install paths}
1695 \added_space_top medskip \align center
1697 \begin_inset Tabular
1698 <lyxtabular version="3" rows="5" columns="4">
1700 <column alignment="center" valignment="top" leftline="true" width="0">
1701 <column alignment="center" valignment="top" leftline="true" width="0">
1702 <column alignment="center" valignment="top" leftline="true" width="0">
1703 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1704 <row topline="true" bottomline="true">
1705 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1735 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1746 <row topline="true">
1747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1786 <row topline="true">
1787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1802 $DATADIR/ $INCLUDE_DIR_SUFFIX
1805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1810 /usr/local/share/sdcc/include
1813 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1826 <row topline="true">
1827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1842 $DATADIR/$LIB_DIR_SUFFIX
1845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1850 /usr/local/share/sdcc/lib
1853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1866 <row topline="true" bottomline="true">
1867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1875 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1890 /usr/local/share/sdcc/doc
1893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1915 *compiler, preprocessor, assembler, and linker
1921 is auto-appended by the compiler, e.g.
1922 small, large, z80, ds390 etc
1925 The install paths can still be changed during `make install` with e.g.:
1928 make install prefix=$(HOME)/local/sdcc
1931 Of course this doesn't change the search paths compiled into the binaries.
1935 \begin_inset LatexCommand \label{sub:Search-Paths}
1940 \begin_inset LatexCommand \index{Search path}
1947 Some search paths or parts of them are determined by configure variables
1952 , see section above).
1953 Further search paths are determined by environment variables during runtime.
1956 The paths searched when running the compiler are as follows (the first catch
1962 Binary files (preprocessor, assembler and linker)
1968 \begin_inset Tabular
1969 <lyxtabular version="3" rows="4" columns="3">
1971 <column alignment="block" valignment="top" leftline="true" width="0in">
1972 <column alignment="block" valignment="top" leftline="true" width="0in">
1973 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1974 <row topline="true" bottomline="true">
1975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1991 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2000 <row topline="true">
2001 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2030 <row topline="true">
2031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2036 Path of argv[0] (if available)
2039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2056 <row topline="true" bottomline="true">
2057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2065 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2073 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2098 \begin_inset Tabular
2099 <lyxtabular version="3" rows="6" columns="3">
2101 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2102 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2103 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2104 <row topline="true" bottomline="true">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2121 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2130 <row topline="true">
2131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2186 <row topline="true">
2187 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 <row topline="true">
2213 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2227 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2239 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2250 <row topline="true">
2251 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2332 <row topline="true" bottomline="true">
2333 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2349 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2354 /usr/local/share/sdcc/
2359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2387 -nostdinc disables the last two search paths.
2397 With the exception of
2398 \begin_inset Quotes sld
2412 \begin_inset Quotes srd
2419 is auto-appended by the compiler (e.g.
2420 small, large, z80, ds390 etc.).
2427 \begin_inset Tabular
2428 <lyxtabular version="3" rows="6" columns="3">
2430 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2431 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2432 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2433 <row topline="true" bottomline="true">
2434 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2442 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2459 <row topline="true">
2460 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2496 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2515 <row topline="true">
2516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2528 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2555 <row topline="true">
2556 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2567 $LIB_DIR_SUFFIX/<model>
2570 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2584 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2601 <row topline="true">
2602 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2617 $LIB_DIR_SUFFIX/<model>
2620 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2673 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2729 <row topline="true" bottomline="true">
2730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2739 $LIB_DIR_SUFFIX/<model>
2742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2747 /usr/local/share/sdcc/
2754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2772 Don't delete any of the stray spaces in the table above without checking
2773 the HTML output (last line)!
2789 -nostdlib disables the last two search paths.
2793 \begin_inset LatexCommand \index{Building SDCC}
2798 \layout Subsubsection
2800 Building SDCC on Linux
2801 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2810 Download the source package
2812 either from the SDCC CVS repository or from the
2813 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2819 , it will be named something like sdcc
2832 Bring up a command line terminal, such as xterm.
2837 Unpack the file using a command like:
2840 "tar -xvzf sdcc.src.tar.gz
2845 , this will create a sub-directory called sdcc with all of the sources.
2848 Change directory into the main SDCC directory, for example type:
2865 This configures the package for compilation on your system.
2881 All of the source packages will compile, this can take a while.
2897 This copies the binary executables, the include files, the libraries and
2898 the documentation to the install directories.
2899 \layout Subsubsection
2901 Building SDCC on OSX 2.x
2904 Follow the instruction for Linux.
2908 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2909 )) fails to compile SDCC.
2910 Fortunately there's also gcc 2.9.x installed, which works fine.
2911 This compiler can be selected by running 'configure' with:
2914 ./configure CC=gcc2 CXX=g++2
2915 \layout Subsubsection
2917 Cross compiling SDCC on Linux for Windows
2920 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2921 See section 'Configure Options'.
2922 \layout Subsubsection
2924 Building SDCC on Windows
2927 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2929 They use Unix-sockets, which are not available on Win32.
2930 \layout Subsubsection
2932 Building SDCC using Cygwin and Mingw32
2935 For building and installing a Cygwin executable follow the instructions
2941 \begin_inset Quotes sld
2945 \begin_inset Quotes srd
2948 Win32-binary can be built, which will not need the Cygwin-DLL.
2949 For the necessary 'configure' options see section 'configure options' or
2950 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
2954 In order to install Cygwin on Windows download setup.exe from
2955 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2961 \begin_inset Quotes sld
2964 default text file type
2965 \begin_inset Quotes srd
2969 \begin_inset Quotes sld
2973 \begin_inset Quotes srd
2976 and download/install at least the following packages.
2977 Some packages are selected by default, others will be automatically selected
2978 because of dependencies with the manually selected packages.
2979 Never deselect these packages!
2988 gcc ; version 3.x is fine, no need to use the old 2.9x
2991 binutils ; selected with gcc
2997 rxvt ; a nice console, which makes life much easier under windoze (see below)
3000 man ; not really needed for building SDCC, but you'll miss it sooner or
3004 less ; not really needed for building SDCC, but you'll miss it sooner or
3008 cvs ; only if you use CVS access
3011 If you want to develop something you'll need:
3014 python ; for the regression tests
3017 gdb ; the gnu debugger, together with the nice GUI
3018 \begin_inset Quotes sld
3022 \begin_inset Quotes srd
3028 openssh ; to access the CF or commit changes
3031 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3032 use autoconf-stable!
3035 rxvt is a nice console with history.
3036 Replace in your cygwin.bat the line
3055 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3058 -bg black -fg white -geometry 100x65 -e bash -
3071 Text selected with the mouse is automatically copied to the clipboard, pasting
3072 works with shift-insert.
3076 The other good tip is to make sure you have no //c/-style paths anywhere,
3077 use /cygdrive/c/ instead.
3078 Using // invokes a network lookup which is very slow.
3080 \begin_inset Quotes sld
3084 \begin_inset Quotes srd
3087 is too long, you can change it with e.g.
3093 SDCC sources use the unix line ending LF.
3094 Life is much easier, if you store the source tree on a drive which is mounted
3096 And use an editor which can handle LF-only line endings.
3097 Make sure not to commit files with windows line endings.
3098 The tabulator spacing used in the project is 8.
3099 \layout Subsubsection
3101 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3106 Download the source package
3108 either from the SDCC CVS repository or from the
3109 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3115 , it will be named something like sdcc
3122 SDCC is distributed with all the projects, workspaces, and files you need
3123 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3124 doesn't build under MSVC).
3125 The workspace name is 'sdcc.dsw'.
3126 Please note that as it is now, all the executables are created in a folder
3130 Once built you need to copy the executables from sdcc
3134 bin before running SDCC.
3139 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3140 flex.exe, and gawk.exe.
3141 One good place to get them is
3142 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3150 Download the file UnxUtils
3151 \begin_inset LatexCommand \index{UnxUtils}
3156 Now you have to install the utilities and setup MSVC so it can locate the
3158 Here there are two alternatives (choose one!):
3165 a) Extract UnxUtils.zip to your C:
3167 hard disk PRESERVING the original paths, otherwise bison won't work.
3168 (If you are using WinZip make certain that 'Use folder names' is selected)
3172 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3173 in 'Show directories for:' select 'Executable files', and in the directories
3174 window add a new path: 'C:
3184 (As a side effect, you get a bunch of Unix utilities that could be useful,
3185 such as diff and patch.)
3192 This one avoids extracting a bunch of files you may not use, but requires
3197 a) Create a directory were to put the tools needed, or use a directory already
3205 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3206 to such directory WITHOUT preserving the original paths.
3207 (If you are using WinZip make certain that 'Use folder names' is not selected)
3211 c) Rename bison.exe to '_bison.exe'.
3215 d) Create a batch file 'bison.bat' in 'C:
3219 ' and add these lines:
3239 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3243 Steps 'c' and 'd' are needed because bison requires by default that the
3244 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3245 '/usr/local/share/' I think.
3246 So it is necessary to tell bison where those files are located if they
3247 are not in such directory.
3248 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3252 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3253 in 'Show directories for:' select 'Executable files', and in the directories
3254 window add a new path: 'c:
3257 Note that you can use any other path instead of 'c:
3259 util', even the path where the Visual C++ tools are, probably: 'C:
3263 Microsoft Visual Studio
3268 So you don't have to execute step 'e' :)
3272 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3273 the executables from sdcc
3277 bin, and you can compile using SDCC.
3278 \layout Subsubsection
3280 Building SDCC Using Borland
3283 From the sdcc directory, run the command "make -f Makefile.bcc".
3284 This should regenerate all the .exe files in the bin directory except for
3285 sdcdb.exe (which currently doesn't build under Borland C++).
3288 If you modify any source files and need to rebuild, be aware that the dependenci
3289 es may not be correctly calculated.
3290 The safest option is to delete all .obj files and run the build again.
3291 From a Cygwin BASH prompt, this can easily be done with the command (be
3292 sure you are in the sdcc directory):
3302 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3304 ) -print -exec rm {}
3313 or on Windows NT/2000/XP from the command prompt with the command:
3320 del /s *.obj *.lib *.rul
3323 from the sdcc directory.
3324 \layout Subsubsection
3326 Windows Install Using a Binary Package
3327 \begin_inset LatexCommand \label{sub:Windows-Install}
3334 Download the binary package from
3335 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3339 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3340 This should unpack to a group of sub-directories.
3341 An example directory structure after unpacking the mingw32 package is:
3346 bin for the executables, c:
3354 lib for the include and libraries.
3357 Adjust your environment variable PATH to include the location of the bin
3358 directory or start sdcc using the full path.
3361 Building the Documentation
3364 If the necessary tools are installed it is as easy as changing into the
3365 doc directory and typing
3369 \begin_inset Quotes srd
3373 \begin_inset Quotes srd
3380 If you want to avoid installing the tools you will have some success with
3381 a bootable Knoppix CD
3382 \begin_inset LatexCommand \url{http://www.knopper.net}
3387 Prebuilt documentation in html and pdf format is available from
3388 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3395 Reading the Documentation
3398 Currently reading the document in pdf format is recommended, as for unknown
3399 reason the hyperlinks are working there whereas in the html version they
3402 This documentation is in some aspects different from a commercial documentation:
3406 It tries to document SDCC for several processor architectures in one document
3407 (commercially these probably would be separate documents/products).
3409 \begin_inset LatexCommand \index{Status of documentation}
3413 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3418 There are many references pointing away from this documentation.
3419 Don't let this distract you.
3421 was a reference like
3422 \begin_inset LatexCommand \url{www.opencores.org}
3426 together with a statement
3427 \begin_inset Quotes sld
3430 some processors which are targetted by SDCC can be implemented in a
3447 \begin_inset LatexCommand \index{fpga (field programmable array)}
3452 \begin_inset Quotes srd
3455 we expect you to have a quick look there and come back.
3456 If you read this you are on the right track.
3459 Some sections attribute more space to problems, restrictions and warnings
3460 than to the solution.
3463 The installation section and the section about the debugger is intimidating.
3466 There are still lots of typos and there are more different writing styles
3470 Testing the SDCC Compiler
3473 The first thing you should do after installing your SDCC compiler is to
3489 \begin_inset LatexCommand \index{version}
3496 at the prompt, and the program should run and tell you the version.
3497 If it doesn't run, or gives a message about not finding sdcc program, then
3498 you need to check over your installation.
3499 Make sure that the sdcc bin directory is in your executable search path
3500 defined by the PATH environment setting (
3505 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3512 Install trouble-shooting for suggestions
3515 Make sure that the sdcc program is in the bin folder, if not perhaps something
3516 did not install correctly.
3524 is commonly installed as described in section
3525 \begin_inset Quotes sld
3528 Install and search paths
3529 \begin_inset Quotes srd
3538 Make sure the compiler works on a very simple example.
3539 Type in the following test.c program using your favorite
3565 Compile this using the following command:
3574 If all goes well, the compiler will generate a test.asm and test.rel file.
3575 Congratulations, you've just compiled your first program with SDCC.
3576 We used the -c option to tell SDCC not to link the generated code, just
3577 to keep things simple for this step.
3585 The next step is to try it with the linker.
3595 If all goes well the compiler will link with the libraries and produce
3596 a test.ihx output file.
3601 (no test.ihx, and the linker generates warnings), then the problem is most
3610 usr/local/share/sdcc/lib directory
3617 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3624 Install trouble-shooting for suggestions).
3632 The final test is to ensure
3640 header files and libraries.
3641 Edit test.c and change it to the following:
3658 strcpy(str1, "testing");
3665 Compile this by typing
3672 This should generate a test.ihx output file, and it should give no warnings
3673 such as not finding the string.h file.
3674 If it cannot find the string.h file, then the problem is that
3678 cannot find the /usr/local/share/sdcc/include directory
3685 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3692 Install trouble-shooting section for suggestions).
3710 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3714 to find exactly where SDCC is looking for the include and lib files.
3717 Install Trouble-shooting
3718 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3723 \begin_inset LatexCommand \index{Install trouble-shooting}
3728 \layout Subsubsection
3730 SDCC does not build correctly.
3733 A thing to try is starting from scratch by unpacking the .tgz source package
3734 again in an empty directory.
3742 ./configure 2>&1 | tee configure.log
3756 make 2>&1 | tee make.log
3763 If anything goes wrong, you can review the log files to locate the problem.
3764 Or a relevant part of this can be attached to an email that could be helpful
3765 when requesting help from the mailing list.
3766 \layout Subsubsection
3769 \begin_inset Quotes sld
3773 \begin_inset Quotes srd
3780 \begin_inset Quotes sld
3784 \begin_inset Quotes srd
3787 command is a script that analyzes your system and performs some configuration
3788 to ensure the source package compiles on your system.
3789 It will take a few minutes to run, and will compile a few tests to determine
3790 what compiler features are installed.
3791 \layout Subsubsection
3794 \begin_inset Quotes sld
3798 \begin_inset Quotes srd
3804 This runs the GNU make tool, which automatically compiles all the source
3805 packages into the final installed binary executables.
3806 \layout Subsubsection
3809 \begin_inset Quotes sld
3813 \begin_inset Quotes erd
3819 This will install the compiler, other executables libraries and include
3820 files into the appropriate directories.
3822 \begin_inset LatexCommand \ref{sub:Install-paths}
3828 \begin_inset LatexCommand \ref{sub:Search-Paths}
3833 about install and search paths.
3835 On most systems you will need super-user privileges to do this.
3841 SDCC is not just a compiler, but a collection of tools by various developers.
3842 These include linkers, assemblers, simulators and other components.
3843 Here is a summary of some of the components.
3844 Note that the included simulator and assembler have separate documentation
3845 which you can find in the source package in their respective directories.
3846 As SDCC grows to include support for other processors, other packages from
3847 various developers are included and may have their own sets of documentation.
3851 You might want to look at the files which are installed in <installdir>.
3852 At the time of this writing, we find the following programs for gcc-builds:
3856 In <installdir>/bin:
3859 sdcc - The compiler.
3862 sdcpp - The C preprocessor.
3865 asx8051 - The assembler for 8051 type processors.
3872 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3875 aslink -The linker for 8051 type processors.
3882 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3885 s51 - The ucSim 8051 simulator.
3888 sdcdb - The source debugger.
3891 packihx - A tool to pack (compress) Intel hex files.
3894 In <installdir>/share/sdcc/include
3900 In <installdir>/share/sdcc/lib
3903 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3907 In <installdir>/share/sdcc/doc
3913 As development for other processors proceeds, this list will expand to include
3914 executables to support processors like AVR, PIC, etc.
3915 \layout Subsubsection
3920 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3921 the assembler and linkage editor.
3922 \layout Subsubsection
3925 \begin_inset LatexCommand \index{sdcpp}
3929 - The C-Preprocessor
3932 The preprocessor is a modified version of the GNU preprocessor.
3933 The C preprocessor is used to pull in #include sources, process #ifdef
3934 statements, #defines and so on.
3935 \layout Subsubsection
3937 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3941 This is retargettable assembler & linkage editor, it was developed by Alan
3943 John Hartman created the version for 8051, and I (Sandeep) have made some
3944 enhancements and bug fixes for it to work properly with SDCC.
3945 \layout Subsubsection
3948 \begin_inset LatexCommand \index{s51}
3955 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3956 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3961 The simulator is built as part of the build process.
3962 For more information visit Daniel's web site at:
3963 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3968 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
3970 \layout Subsubsection
3973 \begin_inset LatexCommand \index{sdcdb}
3977 - Source Level Debugger
3980 Sdcdb is the companion source level debugger.
3981 The current version of the debugger uses Daniel's Simulator S51
3982 \begin_inset LatexCommand \index{s51}
3986 , but can be easily changed to use other simulators.
3993 \layout Subsubsection
3995 Single Source File Projects
3998 For single source file 8051 projects the process is very simple.
3999 Compile your programs with the following command
4002 "sdcc sourcefile.c".
4006 This will compile, assemble and link your source file.
4007 Output files are as follows
4011 \begin_inset LatexCommand \index{.asm}
4016 \begin_inset LatexCommand \index{Assembler source}
4020 file created by the compiler
4024 \begin_inset LatexCommand \index{.lst}
4029 \begin_inset LatexCommand \index{Assembler listing}
4033 file created by the Assembler
4037 \begin_inset LatexCommand \index{.rst}
4042 \begin_inset LatexCommand \index{Assembler listing}
4046 file updated with linkedit information, created by linkage editor
4050 \begin_inset LatexCommand \index{.sym}
4055 \begin_inset LatexCommand \index{Symbol listing}
4059 for the sourcefile, created by the assembler
4063 \begin_inset LatexCommand \index{.rel}
4068 \begin_inset LatexCommand \index{Object file}
4072 created by the assembler, input to Linkage editor
4076 \begin_inset LatexCommand \index{.map}
4081 \begin_inset LatexCommand \index{Memory map}
4085 for the load module, created by the Linker
4089 \begin_inset LatexCommand \index{.mem}
4093 - A file with a summary of the memory usage
4097 \begin_inset LatexCommand \index{.ihx}
4101 - The load module in Intel hex format
4102 \begin_inset LatexCommand \index{Intel hex format}
4106 (you can select the Motorola S19 format
4107 \begin_inset LatexCommand \index{Motorola S19 format}
4122 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4127 If you need another format you might want to use
4134 \begin_inset LatexCommand \index{objdump}
4145 \begin_inset LatexCommand \index{srecord}
4153 \begin_inset LatexCommand \index{.adb}
4157 - An intermediate file containing debug information needed to create the
4169 \begin_inset LatexCommand \index{-\/-debug}
4177 \begin_inset LatexCommand \index{.cdb}
4181 - An optional file (with -
4191 -debug) containing debug information
4196 \begin_inset LatexCommand \index{. (no extension)}
4201 \begin_inset LatexCommand \index{AOMF51}
4205 file containing debug information (with -
4216 This format is commonly used by third party tools (debuggers
4217 \begin_inset LatexCommand \index{Debugger}
4221 , simulators, emulators)
4225 \begin_inset LatexCommand \index{.dump*}
4229 - Dump file to debug the compiler it self (with -
4239 -dumpall) (see section
4240 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4246 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4252 \begin_inset Quotes sld
4255 Anatomy of the compiler
4256 \begin_inset Quotes srd
4260 \layout Subsubsection
4262 Projects with Multiple Source Files
4265 SDCC can compile only ONE file at a time.
4266 Let us for example assume that you have a project containing the following
4271 foo1.c (contains some functions)
4273 foo2.c (contains some more functions)
4275 foomain.c (contains more functions and the function main)
4283 The first two files will need to be compiled separately with the commands:
4315 Then compile the source file containing the
4320 \begin_inset LatexCommand \index{Linker}
4324 the files together with the following command:
4332 foomain.c\SpecialChar ~
4333 foo1.rel\SpecialChar ~
4338 \begin_inset LatexCommand \index{.rel}
4350 can be separately compiled as well:
4361 sdcc foomain.rel foo1.rel foo2.rel
4368 The file containing the
4383 file specified in the command line, since the linkage editor processes
4384 file in the order they are presented to it.
4385 The linker is invoked from SDCC using a script file with extension .lnk
4386 \begin_inset LatexCommand \index{.lnk}
4391 You can view this file to troubleshoot linking problems such as those arising
4392 from missing libraries.
4393 \layout Subsubsection
4395 Projects with Additional Libraries
4396 \begin_inset LatexCommand \index{Libraries}
4403 Some reusable routines may be compiled into a library, see the documentation
4404 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4408 \begin_inset LatexCommand \index{.lib}
4415 Libraries created in this manner can be included in the command line.
4416 Make sure you include the -L <library-path> option to tell the linker where
4417 to look for these files if they are not in the current directory.
4418 Here is an example, assuming you have the source file
4430 (if that is not the same as your current project):
4437 sdcc foomain.c foolib.lib -L mylib
4448 must be an absolute path name.
4452 The most efficient way to use libraries is to keep separate modules in separate
4454 The lib file now should name all the modules.rel
4455 \begin_inset LatexCommand \index{rel}
4460 For an example see the standard library file
4464 in the directory <installdir>/share/lib/small.
4467 Command Line Options
4468 \begin_inset LatexCommand \index{Command Line Options}
4473 \layout Subsubsection
4475 Processor Selection Options
4476 \begin_inset LatexCommand \index{Options processor selection}
4481 \begin_inset LatexCommand \index{Processor selection options}
4487 \labelwidthstring 00.00.0000
4492 \begin_inset LatexCommand \index{-mmcs51}
4498 Generate code for the Intel MCS51
4499 \begin_inset LatexCommand \index{MCS51}
4503 family of processors.
4504 This is the default processor target.
4506 \labelwidthstring 00.00.0000
4511 \begin_inset LatexCommand \index{-mds390}
4517 Generate code for the Dallas DS80C390
4518 \begin_inset LatexCommand \index{DS80C390}
4524 \labelwidthstring 00.00.0000
4529 \begin_inset LatexCommand \index{-mds400}
4535 Generate code for the Dallas DS80C400
4536 \begin_inset LatexCommand \index{DS80C400}
4542 \labelwidthstring 00.00.0000
4547 \begin_inset LatexCommand \index{-mhc08}
4553 Generate code for the Motorola HC08
4554 \begin_inset LatexCommand \index{HC08}
4558 family of processors (added Oct 2003).
4560 \labelwidthstring 00.00.0000
4565 \begin_inset LatexCommand \index{-mz80}
4571 Generate code for the Zilog Z80
4572 \begin_inset LatexCommand \index{Z80}
4576 family of processors.
4578 \labelwidthstring 00.00.0000
4583 \begin_inset LatexCommand \index{-mgbz80}
4589 Generate code for the GameBoy Z80
4590 \begin_inset LatexCommand \index{GameBoy Z80}
4596 \labelwidthstring 00.00.0000
4601 \begin_inset LatexCommand \index{-mavr}
4607 Generate code for the Atmel AVR
4608 \begin_inset LatexCommand \index{AVR}
4612 processor (In development, not complete).
4613 AVR users should probably have a look at avr-gcc
4614 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4621 I think it is fair to direct users there for now.
4622 Open source is also about avoiding unnecessary work .
4623 But I didn't find the 'official' link.
4625 \labelwidthstring 00.00.0000
4630 \begin_inset LatexCommand \index{-mpic14}
4636 Generate code for the Microchip PIC 14
4637 \begin_inset LatexCommand \index{PIC14}
4641 -bit processors (p16f84 and variants).
4644 p16f627 p16f628 p16f84 p16f873 p16f877?
4646 \labelwidthstring 00.00.0000
4651 \begin_inset LatexCommand \index{-mpic16}
4657 Generate code for the Microchip PIC 16
4658 \begin_inset LatexCommand \index{PIC16}
4662 -bit processors (p18f452 and variants).
4664 \labelwidthstring 00.00.0000
4670 Generate code for the Toshiba TLCS-900H
4671 \begin_inset LatexCommand \index{TLCS-900H}
4675 processor (In development, not complete).
4677 \labelwidthstring 00.00.0000
4682 \begin_inset LatexCommand \index{-mxa51}
4688 Generate code for the Phillips XA51
4689 \begin_inset LatexCommand \index{XA51}
4693 processor (In development, not complete).
4694 \layout Subsubsection
4696 Preprocessor Options
4697 \begin_inset LatexCommand \index{Options preprocessor}
4702 \begin_inset LatexCommand \index{Preprocessor options}
4708 \labelwidthstring 00.00.0000
4713 \begin_inset LatexCommand \index{-I<path>}
4719 The additional location where the pre processor will look for <..h> or
4720 \begin_inset Quotes eld
4724 \begin_inset Quotes erd
4729 \labelwidthstring 00.00.0000
4734 \begin_inset LatexCommand \index{-D<macro[=value]>}
4740 Command line definition of macros.
4741 Passed to the preprocessor.
4743 \labelwidthstring 00.00.0000
4748 \begin_inset LatexCommand \index{-M}
4754 Tell the preprocessor to output a rule suitable for make describing the
4755 dependencies of each object file.
4756 For each source file, the preprocessor outputs one make-rule whose target
4757 is the object file name for that source file and whose dependencies are
4758 all the files `#include'd in it.
4759 This rule may be a single line or may be continued with `
4761 '-newline if it is long.
4762 The list of rules is printed on standard output instead of the preprocessed
4765 \begin_inset LatexCommand \index{-E}
4771 \labelwidthstring 00.00.0000
4776 \begin_inset LatexCommand \index{-C}
4782 Tell the preprocessor not to discard comments.
4783 Used with the `-E' option.
4785 \labelwidthstring 00.00.0000
4790 \begin_inset LatexCommand \index{-MM}
4801 Like `-M' but the output mentions only the user header files included with
4803 \begin_inset Quotes eld
4807 System header files included with `#include <file>' are omitted.
4809 \labelwidthstring 00.00.0000
4814 \begin_inset LatexCommand \index{-Aquestion(answer)}
4820 Assert the answer answer for question, in case it is tested with a preprocessor
4821 conditional such as `#if #question(answer)'.
4822 `-A-' disables the standard assertions that normally describe the target
4825 \labelwidthstring 00.00.0000
4830 \begin_inset LatexCommand \index{-Umacro}
4836 Undefine macro macro.
4837 `-U' options are evaluated after all `-D' options, but before any `-include'
4838 and `-imacros' options.
4840 \labelwidthstring 00.00.0000
4845 \begin_inset LatexCommand \index{-dM}
4851 Tell the preprocessor to output only a list of the macro definitions that
4852 are in effect at the end of preprocessing.
4853 Used with the `-E' option.
4855 \labelwidthstring 00.00.0000
4860 \begin_inset LatexCommand \index{-dD}
4866 Tell the preprocessor to pass all macro definitions into the output, in
4867 their proper sequence in the rest of the output.
4869 \labelwidthstring 00.00.0000
4874 \begin_inset LatexCommand \index{-dN}
4885 Like `-dD' except that the macro arguments and contents are omitted.
4886 Only `#define name' is included in the output.
4887 \layout Subsubsection
4890 \begin_inset LatexCommand \index{Options linker}
4895 \begin_inset LatexCommand \index{Linker options}
4901 \labelwidthstring 00.00.0000
4921 \begin_inset LatexCommand \index{-\/-lib-path}
4926 \begin_inset LatexCommand \index{-L -\/-lib-path}
4935 <absolute path to additional libraries> This option is passed to the linkage
4936 editor's additional libraries
4937 \begin_inset LatexCommand \index{Libraries}
4942 The path name must be absolute.
4943 Additional library files may be specified in the command line.
4944 See section Compiling programs for more details.
4946 \labelwidthstring 00.00.0000
4963 \begin_inset LatexCommand \index{-\/-xram-loc}
4967 <Value> The start location of the external ram
4968 \begin_inset LatexCommand \index{xdata}
4972 , default value is 0.
4973 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4983 -xram-loc 0x8000 or -
4995 \labelwidthstring 00.00.0000
5012 \begin_inset LatexCommand \index{-\/-code-loc}
5016 <Value> The start location of the code
5017 \begin_inset LatexCommand \index{code}
5021 segment, default value 0.
5022 Note when this option is used the interrupt vector table is also relocated
5023 to the given address.
5024 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5034 -code-loc 0x8000 or -
5046 \labelwidthstring 00.00.0000
5063 \begin_inset LatexCommand \index{-\/-stack-loc}
5067 <Value> By default the stack
5068 \begin_inset LatexCommand \index{stack}
5072 is placed after the data segment.
5073 Using this option the stack can be placed anywhere in the internal memory
5075 The value entered can be in Hexadecimal or Decimal format, e.g.
5086 -stack-loc 0x20 or -
5097 Since the sp register is incremented before a push or call, the initial
5098 sp will be set to one byte prior the provided value.
5099 The provided value should not overlap any other memory areas such as used
5100 register banks or the data segment and with enough space for the current
5103 \labelwidthstring 00.00.0000
5120 \begin_inset LatexCommand \index{-\/-data-loc}
5124 <Value> The start location of the internal ram data
5125 \begin_inset LatexCommand \index{data}
5130 The value entered can be in Hexadecimal or Decimal format, eg.
5152 (By default, the start location of the internal ram data segment is set
5153 as low as possible in memory, taking into account the used register banks
5154 and the bit segment at address 0x20.
5155 For example if register banks 0 and 1 are used without bit variables, the
5156 data segment will be set, if -
5166 -data-loc is not used, to location 0x10.)
5168 \labelwidthstring 00.00.0000
5185 \begin_inset LatexCommand \index{-\/-idata-loc}
5189 <Value> The start location of the indirectly addressable internal ram
5190 \begin_inset LatexCommand \index{idata}
5194 , default value is 0x80.
5195 The value entered can be in Hexadecimal or Decimal format, eg.
5206 -idata-loc 0x88 or -
5218 \labelwidthstring 00.00.0000
5233 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5242 The linker output (final object code) is in Intel Hex format.
5243 \begin_inset LatexCommand \index{Intel hex format}
5247 (This is the default option).
5249 \labelwidthstring 00.00.0000
5264 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5273 The linker output (final object code) is in Motorola S19 format
5274 \begin_inset LatexCommand \index{Motorola S19 format}
5279 \layout Subsubsection
5282 \begin_inset LatexCommand \index{Options MCS51}
5287 \begin_inset LatexCommand \index{MCS51 options}
5293 \labelwidthstring 00.00.0000
5308 \begin_inset LatexCommand \index{-\/-model-small}
5319 Generate code for Small Model programs see section Memory Models for more
5321 This is the default model.
5323 \labelwidthstring 00.00.0000
5338 \begin_inset LatexCommand \index{-\/-model-large}
5344 Generate code for Large model programs see section Memory Models for more
5346 If this option is used all source files in the project have to be compiled
5349 \labelwidthstring 00.00.0000
5364 \begin_inset LatexCommand \index{-\/-xstack}
5370 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5371 variables and passing parameters.
5373 \begin_inset LatexCommand \ref{sub:External-Stack}
5378 External Stack for more details.
5380 \labelwidthstring 00.00.0000
5397 \begin_inset LatexCommand \index{-\/-iram-size<Value>}
5401 Causes the linker to check if the internal ram usage is within limits of
5404 \labelwidthstring 00.00.0000
5421 \begin_inset LatexCommand \index{-\/-xram-size<Value>}
5425 Causes the linker to check if the external ram usage is within limits of
5428 \labelwidthstring 00.00.0000
5445 \begin_inset LatexCommand \index{-\/-data-loc}
5449 Causes the linker to check if the code memory usage is within limits of
5451 \layout Subsubsection
5454 \begin_inset LatexCommand \index{Options DS390}
5459 \begin_inset LatexCommand \index{DS390 options}
5465 \labelwidthstring 00.00.0000
5482 \begin_inset LatexCommand \index{-\/-model-flat24}
5492 Generate 24-bit flat mode code.
5493 This is the one and only that the ds390 code generator supports right now
5494 and is default when using
5499 See section Memory Models for more details.
5501 \labelwidthstring 00.00.0000
5518 \begin_inset LatexCommand \index{-\/-stack-10bit}
5522 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
5523 This is the one and only that the ds390 code generator supports right now
5524 and is default when using
5529 In this mode, the stack is located in the lower 1K of the internal RAM,
5530 which is mapped to 0x400000.
5531 Note that the support is incomplete, since it still uses a single byte
5532 as the stack pointer.
5533 This means that only the lower 256 bytes of the potential 1K stack space
5534 will actually be used.
5535 However, this does allow you to reclaim the precious 256 bytes of low RAM
5536 for use for the DATA and IDATA segments.
5537 The compiler will not generate any code to put the processor into 10 bit
5539 It is important to ensure that the processor is in this mode before calling
5540 any re-entrant functions compiled with this option.
5541 In principle, this should work with the
5554 \begin_inset LatexCommand \index{-\/-stack-auto}
5560 option, but that has not been tested.
5561 It is incompatible with the
5574 \begin_inset LatexCommand \index{-\/-xstack}
5581 It also only makes sense if the processor is in 24 bit contiguous addressing
5594 -model-flat24 option
5597 \layout Subsubsection
5600 \begin_inset LatexCommand \index{Options Z80}
5605 \begin_inset LatexCommand \index{Z80 options}
5611 \labelwidthstring 00.00.0000
5628 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
5638 Force a called function to always save BC.
5640 \labelwidthstring 00.00.0000
5657 \begin_inset LatexCommand \index{-\/-no-std-crt0}
5661 When linking, skip the standard crt0.o object file.
5662 You must provide your own crt0.o for your system when linking.
5664 \layout Subsubsection
5666 Optimization Options
5667 \begin_inset LatexCommand \index{Options optimization}
5672 \begin_inset LatexCommand \index{Optimization options}
5678 \labelwidthstring 00.00.0000
5693 \begin_inset LatexCommand \index{-\/-nogcse}
5699 Will not do global subexpression elimination, this option may be used when
5700 the compiler creates undesirably large stack/data spaces to store compiler
5702 A warning message will be generated when this happens and the compiler
5703 will indicate the number of extra bytes it allocated.
5704 It recommended that this option NOT be used, #pragma\SpecialChar ~
5706 \begin_inset LatexCommand \index{\#pragma NOGCSE}
5710 can be used to turn off global subexpression elimination
5711 \begin_inset LatexCommand \index{Subexpression elimination}
5715 for a given function only.
5717 \labelwidthstring 00.00.0000
5732 \begin_inset LatexCommand \index{-\/-noinvariant}
5738 Will not do loop invariant optimizations, this may be turned off for reasons
5739 explained for the previous option.
5740 For more details of loop optimizations performed see section Loop Invariants.It
5741 recommended that this option NOT be used, #pragma\SpecialChar ~
5743 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
5747 can be used to turn off invariant optimizations for a given function only.
5749 \labelwidthstring 00.00.0000
5764 \begin_inset LatexCommand \index{-\/-noinduction}
5770 Will not do loop induction optimizations, see section strength reduction
5771 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
5774 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
5778 can be used to turn off induction optimizations for a given function only.
5780 \labelwidthstring 00.00.0000
5795 \begin_inset LatexCommand \index{-\/-nojtbound}
5806 Will not generate boundary condition check when switch statements
5807 \begin_inset LatexCommand \index{switch statement}
5811 are implemented using jump-tables.
5813 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
5818 Switch Statements for more details.
5819 It is recommended that this option is NOT used, #pragma\SpecialChar ~
5821 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
5825 can be used to turn off boundary checking for jump tables for a given function
5828 \labelwidthstring 00.00.0000
5843 \begin_inset LatexCommand \index{-\/-noloopreverse}
5852 Will not do loop reversal
5853 \begin_inset LatexCommand \index{Loop reversing}
5859 \labelwidthstring 00.00.0000
5876 \begin_inset LatexCommand \index{-\/-nolabelopt }
5880 Will not optimize labels (makes the dumpfiles more readable).
5882 \labelwidthstring 00.00.0000
5897 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
5903 Will not memcpy initialized data from code space into xdata space.
5904 This saves a few bytes in code space if you don't have initialized data.
5905 \layout Subsubsection
5908 \begin_inset LatexCommand \index{Options other}
5914 \labelwidthstring 00.00.0000
5930 \begin_inset LatexCommand \index{-\/-compile-only}
5935 \begin_inset LatexCommand \index{-c -\/-compile-only}
5941 will compile and assemble the source, but will not call the linkage editor.
5943 \labelwidthstring 00.00.0000
5962 \begin_inset LatexCommand \index{-\/-c1mode}
5968 reads the preprocessed source from standard input and compiles it.
5969 The file name for the assembler output must be specified using the -o option.
5971 \labelwidthstring 00.00.0000
5976 \begin_inset LatexCommand \index{-E}
5982 Run only the C preprocessor.
5983 Preprocess all the C source files specified and output the results to standard
5986 \labelwidthstring 00.00.0000
5992 \begin_inset LatexCommand \index{-o <path/file>}
5998 The output path resp.
5999 file where everything will be placed.
6000 If the parameter is a path, it must have a trailing slash (or backslash
6001 for the Windows binaries) to be recognized as a path.
6004 \labelwidthstring 00.00.0000
6019 \begin_inset LatexCommand \index{-\/-stack-auto}
6030 All functions in the source file will be compiled as
6035 \begin_inset LatexCommand \index{reentrant}
6040 the parameters and local variables will be allocated on the stack
6041 \begin_inset LatexCommand \index{stack}
6046 see section Parameters and Local Variables for more details.
6047 If this option is used all source files in the project should be compiled
6051 \labelwidthstring 00.00.0000
6066 \begin_inset LatexCommand \index{-\/-callee-saves}
6070 function1[,function2][,function3]....
6073 The compiler by default uses a caller saves convention for register saving
6074 across function calls, however this can cause unnecessary register pushing
6075 & popping when calling small functions from larger functions.
6076 This option can be used to switch the register saving convention for the
6077 function names specified.
6078 The compiler will not save registers when calling these functions, no extra
6079 code will be generated at the entry & exit (function prologue
6082 \begin_inset LatexCommand \index{function prologue}
6091 \begin_inset LatexCommand \index{function epilogue}
6097 ) for these functions to save & restore the registers used by these functions,
6098 this can SUBSTANTIALLY reduce code & improve run time performance of the
6100 In the future the compiler (with inter procedural analysis) will be able
6101 to determine the appropriate scheme to use for each function call.
6102 DO NOT use this option for built-in functions such as _mulint..., if this
6103 option is used for a library function the appropriate library function
6104 needs to be recompiled with the same option.
6105 If the project consists of multiple source files then all the source file
6106 should be compiled with the same -
6116 -callee-saves option string.
6117 Also see #pragma\SpecialChar ~
6119 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
6125 \labelwidthstring 00.00.0000
6140 \begin_inset LatexCommand \index{-\/-debug}
6149 When this option is used the compiler will generate debug information, that
6150 can be used with the SDCDB.
6151 The debug information is collected in a file with .cdb extension.
6152 For more information see documentation for SDCDB.
6154 \labelwidthstring 00.00.0000
6159 \begin_inset LatexCommand \index{-S}
6170 Stop after the stage of compilation proper; do not assemble.
6171 The output is an assembler code file for the input file specified.
6173 \labelwidthstring 00.00.0000
6177 -Wa_asmOption[,asmOption]
6180 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
6185 Pass the asmOption to the assembler.
6187 \labelwidthstring 00.00.0000
6191 -Wl_linkOption[,linkOption]
6194 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
6199 Pass the linkOption to the linker.
6201 \labelwidthstring 00.00.0000
6216 \begin_inset LatexCommand \index{-\/-int-long-reent}
6222 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
6223 Note by default these libraries are compiled as non-reentrant.
6224 See section Installation for more details.
6226 \labelwidthstring 00.00.0000
6241 \begin_inset LatexCommand \index{-\/-cyclomatic}
6250 This option will cause the compiler to generate an information message for
6251 each function in the source file.
6252 The message contains some
6256 information about the function.
6257 The number of edges and nodes the compiler detected in the control flow
6258 graph of the function, and most importantly the
6260 cyclomatic complexity
6261 \begin_inset LatexCommand \index{Cyclomatic complexity}
6267 see section on Cyclomatic Complexity for more details.
6269 \labelwidthstring 00.00.0000
6284 \begin_inset LatexCommand \index{-\/-float-reent}
6293 Floating point library is compiled as reentrant
6294 \begin_inset LatexCommand \index{reentrant}
6299 See section Installation for more details.
6301 \labelwidthstring 00.00.0000
6316 \begin_inset LatexCommand \index{-\/-nooverlay}
6322 The compiler will not overlay parameters and local variables of any function,
6323 see section Parameters and local variables for more details.
6325 \labelwidthstring 00.00.0000
6340 \begin_inset LatexCommand \index{-\/-main-return}
6346 This option can be used when the code generated is called by a monitor
6348 The compiler will generate a 'ret' upon return from the 'main'
6349 \begin_inset LatexCommand \index{main return}
6354 The default setting is to lock up i.e.
6361 \labelwidthstring 00.00.0000
6378 \begin_inset LatexCommand \index{-\/-peep-file}
6382 <filename> This option can be used to use additional rules to be used by
6383 the peep hole optimizer.
6385 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6390 Peep Hole optimizations for details on how to write these rules.
6392 \labelwidthstring 00.00.0000
6407 \begin_inset LatexCommand \index{-\/-no-peep}
6413 Disable peep-hole optimization.
6415 \labelwidthstring 00.00.0000
6430 \begin_inset LatexCommand \index{-\/-peep-asm}
6436 Pass the inline assembler code through the peep hole optimizer.
6437 This can cause unexpected changes to inline assembler code, please go through
6438 the peephole optimizer
6439 \begin_inset LatexCommand \index{Peephole optimizer}
6443 rules defined in the source file tree '<target>/peeph.def' before using
6446 \labelwidthstring 00.00.0000
6461 \begin_inset LatexCommand \index{-\/-nostdincl}
6467 This will prevent the compiler from passing on the default include path
6468 to the preprocessor.
6470 \labelwidthstring 00.00.0000
6485 \begin_inset LatexCommand \index{-\/-nostdlib}
6491 This will prevent the compiler from passing on the default library
6492 \begin_inset LatexCommand \index{Libraries}
6498 \labelwidthstring 00.00.0000
6513 \begin_inset LatexCommand \index{-\/-verbose}
6519 Shows the various actions the compiler is performing.
6521 \labelwidthstring 00.00.0000
6526 \begin_inset LatexCommand \index{-V}
6532 Shows the actual commands the compiler is executing.
6534 \labelwidthstring 00.00.0000
6549 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
6555 Hides your ugly and inefficient c-code from the asm file, so you can always
6556 blame the compiler :).
6558 \labelwidthstring 00.00.0000
6573 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
6579 Include i-codes in the asm file.
6580 Sounds like noise but is most helpful for debugging the compiler itself.
6582 \labelwidthstring 00.00.0000
6597 \begin_inset LatexCommand \index{-\/-less-pedantic}
6603 Disable some of the more pedantic warnings
6604 \begin_inset LatexCommand \index{Warnings}
6608 (jwk burps: please be more specific here, please!).
6609 If you want rather more than less warnings you should consider using a
6610 separate tool dedicated to syntax checking like
6611 \begin_inset LatexCommand \url{www.splint.org}
6617 \labelwidthstring 00.00.0000
6632 \begin_inset LatexCommand \index{-\/-print-search-dirs}
6638 Display the directories in the compiler's search path
6640 \labelwidthstring 00.00.0000
6655 \begin_inset LatexCommand \index{-\/-vc}
6661 Display errors and warnings using MSVC style, so you can use SDCC with
6664 \labelwidthstring 00.00.0000
6679 \begin_inset LatexCommand \index{-\/-use-stdout}
6685 Send errors and warnings to stdout instead of stderr.
6686 \layout Subsubsection
6688 Intermediate Dump Options
6689 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
6694 \begin_inset LatexCommand \index{Options intermediate dump}
6699 \begin_inset LatexCommand \index{Intermediate dump options}
6706 The following options are provided for the purpose of retargetting and debugging
6708 These provided a means to dump the intermediate code (iCode
6709 \begin_inset LatexCommand \index{iCode}
6713 ) generated by the compiler in human readable form at various stages of
6714 the compilation process.
6717 \labelwidthstring 00.00.0000
6732 \begin_inset LatexCommand \index{-\/-dumpraw}
6738 This option will cause the compiler to dump the intermediate code into
6741 <source filename>.dumpraw
6743 just after the intermediate code has been generated for a function, i.e.
6744 before any optimizations are done.
6746 \begin_inset LatexCommand \index{Basic blocks}
6750 at this stage ordered in the depth first number, so they may not be in
6751 sequence of execution.
6753 \labelwidthstring 00.00.0000
6768 \begin_inset LatexCommand \index{-\/-dumpgcse}
6774 Will create a dump of iCode's, after global subexpression elimination
6775 \begin_inset LatexCommand \index{Global subexpression elimination}
6781 <source filename>.dumpgcse.
6783 \labelwidthstring 00.00.0000
6798 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
6804 Will create a dump of iCode's, after deadcode elimination
6805 \begin_inset LatexCommand \index{Dead-code elimination}
6811 <source filename>.dumpdeadcode.
6813 \labelwidthstring 00.00.0000
6828 \begin_inset LatexCommand \index{-\/-dumploop}
6837 Will create a dump of iCode's, after loop optimizations
6838 \begin_inset LatexCommand \index{Loop optimization}
6844 <source filename>.dumploop.
6846 \labelwidthstring 00.00.0000
6861 \begin_inset LatexCommand \index{-\/-dumprange}
6870 Will create a dump of iCode's, after live range analysis
6871 \begin_inset LatexCommand \index{Live range analysis}
6877 <source filename>.dumprange.
6879 \labelwidthstring 00.00.0000
6894 \begin_inset LatexCommand \index{-\/-dumlrange}
6900 Will dump the life ranges
6901 \begin_inset LatexCommand \index{Live range analysis}
6907 \labelwidthstring 00.00.0000
6922 \begin_inset LatexCommand \index{-\/-dumpregassign}
6931 Will create a dump of iCode's, after register assignment
6932 \begin_inset LatexCommand \index{Register assignment}
6938 <source filename>.dumprassgn.
6940 \labelwidthstring 00.00.0000
6955 \begin_inset LatexCommand \index{-\/-dumplrange}
6961 Will create a dump of the live ranges of iTemp's
6963 \labelwidthstring 00.00.0000
6978 \begin_inset LatexCommand \index{-\/-dumpall}
6989 Will cause all the above mentioned dumps to be created.
6990 \layout Subsubsection
6992 Redirecting output on Windows Shells
6995 By default SDCC writes it's error messages to
6996 \begin_inset Quotes sld
7000 \begin_inset Quotes srd
7004 To force all messages to
7005 \begin_inset Quotes sld
7009 \begin_inset Quotes srd
7033 \begin_inset LatexCommand \index{-\/-use-stdout}
7038 Aditionaly, if you happen to have visual studio installed in your windows
7039 machine, you can use it to compile your sources using a custom build and
7055 \begin_inset LatexCommand \index{-\/-vc}
7060 Something like this should work:
7104 -model-large -c $(InputPath)
7107 Environment variables
7108 \begin_inset LatexCommand \index{Environment variables}
7115 SDCC recognizes the following environment variables:
7117 \labelwidthstring 00.00.0000
7122 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7128 SDCC installs a signal handler
7129 \begin_inset LatexCommand \index{signal handler}
7133 to be able to delete temporary files after an user break (^C) or an exception.
7134 If this environment variable is set, SDCC won't install the signal handler
7135 in order to be able to debug SDCC.
7137 \labelwidthstring 00.00.0000
7144 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7150 Path, where temporary files will be created.
7151 The order of the variables is the search order.
7152 In a standard *nix environment these variables are not set, and there's
7153 no need to set them.
7154 On Windows it's recommended to set one of them.
7156 \labelwidthstring 00.00.0000
7161 \begin_inset LatexCommand \index{SDCC\_HOME}
7168 \begin_inset LatexCommand \ref{sub:Install-paths}
7174 \begin_inset Quotes sld
7178 \begin_inset Quotes srd
7183 \labelwidthstring 00.00.0000
7188 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
7195 \begin_inset LatexCommand \ref{sub:Search-Paths}
7201 \begin_inset Quotes sld
7205 \begin_inset Quotes srd
7210 \labelwidthstring 00.00.0000
7215 \begin_inset LatexCommand \index{SDCC\_LIB}
7222 \begin_inset LatexCommand \ref{sub:Search-Paths}
7228 \begin_inset Quotes sld
7232 \begin_inset Quotes srd
7238 There are some more environment variables recognized by SDCC, but these
7239 are solely used for debugging purposes.
7240 They can change or disappear very quickly, and will never be documented.
7243 MCS51/DS390 Storage Class
7244 \begin_inset LatexCommand \index{Storage class}
7251 In addition to the ANSI storage classes SDCC allows the following MCS51
7252 specific storage classes.
7253 \layout Subsubsection
7256 \begin_inset LatexCommand \index{data}
7267 storage class for the Small Memory model.
7268 Variables declared with this storage class will be allocated in the directly
7269 addressable portion of the internal RAM of a 8051, e.g.:
7274 data unsigned char test_data;
7277 Writing 0x01 to this variable generates the assembly code:
7282 75*00 01\SpecialChar ~
7288 \layout Subsubsection
7291 \begin_inset LatexCommand \index{xdata}
7298 Variables declared with this storage class will be placed in the external
7304 storage class for the Large Memory model, e.g.:
7309 xdata unsigned char test_xdata;
7312 Writing 0x01 to this variable generates the assembly code:
7317 90s00r00\SpecialChar ~
7346 \layout Subsubsection
7349 \begin_inset LatexCommand \index{idata}
7356 Variables declared with this storage class will be allocated into the indirectly
7357 addressable portion of the internal ram of a 8051, e.g.:
7362 idata unsigned char test_idata;
7365 Writing 0x01 to this variable generates the assembly code:
7392 \layout Subsubsection
7395 \begin_inset LatexCommand \index{pdata}
7402 Paged xdata access is currently not as straightforward as using the other
7403 addressing modes of a 8051.
7404 The following example writes 0x01 to the address pointed to.
7405 Please note, pdata access physically accesses xdata memory.
7406 The high byte of the address is determined by port P2 (or in case of some
7407 8051 variants by a separate Special Function Register, see section
7408 \begin_inset LatexCommand \ref{sub:MCS51-variants}
7417 pdata unsigned char *test_pdata_ptr;
7429 test_pdata_ptr = (pdata *)0xfe;
7435 *test_pdata_ptr = 1;
7440 Generates the assembly code:
7445 75*01 FE\SpecialChar ~
7449 _test_pdata_ptr,#0xFE
7481 Be extremely carefull if you use pdata together with the -
7492 \begin_inset LatexCommand \index{-\/-xstack}
7497 \layout Subsubsection
7500 \begin_inset LatexCommand \index{code}
7507 'Variables' declared with this storage class will be placed in the code
7513 code unsigned char test_code;
7516 Read access to this variable generates the assembly code:
7521 90s00r6F\SpecialChar ~
7524 mov dptr,#_test_code
7547 \layout Subsubsection
7550 \begin_inset LatexCommand \index{bit}
7557 This is a data-type and a storage class specifier.
7558 When a variable is declared as a bit, it is allocated into the bit addressable
7559 memory of 8051, e.g.:
7567 Writing 1 to this variable generates the assembly code:
7581 \layout Subsubsection
7584 \begin_inset LatexCommand \index{sfr}
7589 \begin_inset LatexCommand \index{sbit}
7596 Like the bit keyword,
7600 signifies both a data-type and storage class, they are used to describe
7601 the special function registers and special bit variables of a 8051, eg:
7607 \begin_inset LatexCommand \index{at}
7611 0x80 P0;\SpecialChar ~
7612 /* special function register P0 at location 0x80 */
7614 sbit at 0xd7 CY; /* CY (Carry Flag
7615 \begin_inset LatexCommand \index{Flags}
7620 \begin_inset LatexCommand \index{Carry flag}
7625 \layout Subsubsection
7628 \begin_inset LatexCommand \index{Pointers}
7632 to MCS51/DS390 specific memory spaces
7635 SDCC allows (via language extensions) pointers to explicitly point to any
7636 of the memory spaces
7637 \begin_inset LatexCommand \index{Memory model}
7642 In addition to the explicit pointers, the compiler uses (by default) generic
7643 pointers which can be used to point to any of the memory spaces.
7647 Pointer declaration examples:
7652 /* pointer physically in internal ram pointing to object in external ram
7655 xdata unsigned char * data p;
7659 /* pointer physically in external ram pointing to object in internal ram
7662 data unsigned char * xdata p;
7666 /* pointer physically in code rom pointing to data in xdata space */
7668 xdata unsigned char * code p;
7672 /* pointer physically in code space pointing to data in code space */
7674 code unsigned char * code p;
7678 /* the following is a generic pointer physically located in xdata space
7684 Well you get the idea.
7689 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
7702 The highest order byte of the
7706 pointers contains the data space information.
7707 Assembler support routines are called whenever data is stored or retrieved
7713 These are useful for developing reusable library
7714 \begin_inset LatexCommand \index{Libraries}
7719 Explicitly specifying the pointer type will generate the most efficient
7724 \begin_inset LatexCommand \index{Absolute addressing}
7731 Data items can be assigned an absolute address with the
7734 \begin_inset LatexCommand \index{at}
7740 keyword, in addition to a storage class, e.g.:
7746 \begin_inset LatexCommand \index{xdata}
7751 \begin_inset LatexCommand \index{at}
7755 0x7ffe unsigned int chksum;
7758 In the above example the variable chksum will located at 0x7ffe and 0x7fff
7759 of the external ram.
7760 The compiler does not actually reserve any space for variables declared
7761 in this way (they are implemented with an equate in the assembler).
7762 Thus it is left to the programmer to make sure there are no overlaps with
7763 other variables that are declared without the absolute address.
7764 The assembler listing file (.lst
7765 \begin_inset LatexCommand \index{.lst}
7769 ) and the linker output files (.rst
7770 \begin_inset LatexCommand \index{.rst}
7775 \begin_inset LatexCommand \index{.map}
7779 ) are good places to look for such overlaps.
7782 In case of memory mapped I/O devices the keyword
7786 should be used to tell the compiler that accesses might not be optimized
7793 \begin_inset LatexCommand \index{volatile}
7798 \begin_inset LatexCommand \index{xdata}
7803 \begin_inset LatexCommand \index{at}
7807 0x8000 unsigned char PORTA_8255;
7810 Absolute address can be specified for variables in all storage classes,
7817 \begin_inset LatexCommand \index{bit}
7822 \begin_inset LatexCommand \index{at}
7829 The above example will allocate the variable at offset 0x02 in the bit-addressab
7831 There is no real advantage to assigning absolute addresses to variables
7832 in this manner, unless you want strict control over all the variables allocated.
7833 One possible use would be to write hardware portable code.
7834 For example, if you have a routine that uses one or more of the microcontroller
7835 I/O pins, and such pins are different for two different hardwares, you
7836 can declare the I/O pins in your routine using:
7841 extern volatile bit SDI;
7843 extern volatile bit SCLK;
7845 extern volatile bit CPOL;
7849 void DS1306_put(unsigned char value)
7857 unsigned char mask=0x80;
7881 SDI=(value & mask)?1:0;
7922 Then, someplace in the code for the first hardware you would use
7927 bit at 0x80 SDI;\SpecialChar ~
7931 /* I/O port 0, bit 0 */
7933 bit at 0x81 SCLK;\SpecialChar ~
7936 /* I/O port 0, bit 1 */
7938 bit CPOL;\SpecialChar ~
7949 /* This is a variable, let the linker allocate this one */
7952 Similarly, for the second hardware you would use
7957 bit at 0x83 SDI;\SpecialChar ~
7961 /* I/O port 0, bit 3 */
7963 bit at 0x91 SCLK;\SpecialChar ~
7966 /* I/O port 1, bit 1 */
7968 bit CPOL;\SpecialChar ~
7979 /* This is a variable, let the linker allocate this one */
7982 and you can use the same hardware dependent routine without changes, as
7983 for example in a library.
7984 This is somehow similar to sbit, but only one absolute address has to be
7985 specified in the whole project.
7989 \begin_inset LatexCommand \index{Parameters}
7994 \begin_inset LatexCommand \index{Local variable}
8001 Automatic (local) variables and parameters to functions can either be placed
8002 on the stack or in data-space.
8003 The default action of the compiler is to place these variables in the internal
8004 RAM (for small model) or external RAM (for large model).
8005 This in fact makes them similar to
8008 \begin_inset LatexCommand \index{static}
8014 so by default functions are non-reentrant
8015 \begin_inset LatexCommand \index{reentrant}
8024 They can be placed on the stack
8025 \begin_inset LatexCommand \index{stack}
8042 \begin_inset LatexCommand \index{-\/-stack-auto}
8048 option or by using the
8051 \begin_inset LatexCommand \index{reentrant}
8057 keyword in the function declaration, e.g.:
8062 unsigned char foo(char i) reentrant
8076 Since stack space on 8051 is limited, the
8094 option should be used sparingly.
8095 Note that the reentrant keyword just means that the parameters & local
8096 variables will be allocated to the stack, it
8100 mean that the function is register bank independent.
8104 Local variables can be assigned storage classes and absolute
8105 \begin_inset LatexCommand \index{Absolute addressing}
8122 xdata unsigned char i;
8134 data at 0x31 unsigned char j;
8146 In the above example the variable
8150 will be allocated in the external ram,
8154 in bit addressable space and
8173 or when a function is declared as
8177 this should only be done for static variables.
8180 Parameters however are not allowed any storage class, (storage classes for
8181 parameters will be ignored), their allocation is governed by the memory
8182 model in use, and the reentrancy options.
8186 \begin_inset LatexCommand \label{sub:Overlaying}
8191 \begin_inset LatexCommand \index{Overlaying}
8199 \begin_inset LatexCommand \index{reentrant}
8203 functions SDCC will try to reduce internal ram space usage by overlaying
8204 parameters and local variables of a function (if possible).
8205 Parameters and local variables of a function will be allocated to an overlayabl
8206 e segment if the function has
8208 no other function calls and the function is non-reentrant and the memory
8210 \begin_inset LatexCommand \index{Memory model}
8217 If an explicit storage class
8218 \begin_inset LatexCommand \index{Storage class}
8222 is specified for a local variable, it will NOT be overlayed.
8225 Note that the compiler (not the linkage editor) makes the decision for overlayin
8227 Functions that are called from an interrupt service routine should be preceded
8228 by a #pragma\SpecialChar ~
8230 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8234 if they are not reentrant.
8237 Also note that the compiler does not do any processing of inline
8238 \begin_inset LatexCommand \index{inline}
8242 assembler code, so the compiler might incorrectly assign local variables
8243 and parameters of a function into the overlay segment if the inline assembler
8244 code calls other c-functions that might use the overlay.
8245 In that case the #pragma\SpecialChar ~
8246 NOOVERLAY should be used.
8249 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
8251 \begin_inset LatexCommand \index{Multiplication}
8256 \begin_inset LatexCommand \index{Division}
8260 will NOT be overlayed since these are implemented using external functions,
8269 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8275 void set_error(unsigned char errcd)
8291 void some_isr () interrupt
8292 \begin_inset LatexCommand \index{interrupt}
8322 In the above example the parameter
8330 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
8332 not present, this could cause unpredictable runtime behavior when called
8334 The #pragma\SpecialChar ~
8335 NOOVERLAY ensures that the parameters and local variables for
8336 the function are NOT overlayed.
8339 Interrupt Service Routines
8340 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
8347 SDCC allows interrupt service routines to be coded in C, with some extended
8353 void timer_isr (void) interrupt 1 using 1
8367 The optional number following the
8370 \begin_inset LatexCommand \index{interrupt}
8376 keyword is the interrupt number this routine will service.
8377 When present, the compiler will insert a call to this routine in the interrupt
8378 vector table for the interrupt number specified.
8383 keyword can be used to tell the compiler to use the specified register
8384 bank (8051 specific) when generating code for this function.
8390 If you have multiple source files in your project, interrupt service routines
8391 can be present in any of them, but a prototype of the isr MUST be present
8392 or included in the file that contains the function
8399 Interrupt numbers and the corresponding address & descriptions for the Standard
8400 8051/8052 are listed below.
8401 SDCC will automatically adjust the interrupt vector table to the maximum
8402 interrupt number specified.
8408 \begin_inset Tabular
8409 <lyxtabular version="3" rows="7" columns="3">
8411 <column alignment="center" valignment="top" leftline="true" width="0in">
8412 <column alignment="center" valignment="top" leftline="true" width="0in">
8413 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
8414 <row topline="true" bottomline="true">
8415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8440 <row topline="true">
8441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8449 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8457 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8466 <row topline="true">
8467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8492 <row topline="true">
8493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8501 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8509 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8518 <row topline="true">
8519 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8527 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8544 <row topline="true">
8545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8570 <row topline="true" bottomline="true">
8571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8605 If the interrupt service routine is defined without
8608 \begin_inset LatexCommand \index{using}
8614 a register bank or with register bank 0 (using 0), the compiler will save
8615 the registers used by itself on the stack upon entry and restore them at
8616 exit, however if such an interrupt service routine calls another function
8617 then the entire register bank will be saved on the stack.
8618 This scheme may be advantageous for small interrupt service routines which
8619 have low register usage.
8622 If the interrupt service routine is defined to be using a specific register
8627 are save and restored, if such an interrupt service routine calls another
8628 function (using another register bank) then the entire register bank of
8629 the called function will be saved on the stack.
8630 This scheme is recommended for larger interrupt service routines.
8633 Using interrupts opens the door for some very interesting bugs:
8636 If the interrupt service routines changes variables which are accessed by
8637 other functions these variables should be declared
8642 \begin_inset LatexCommand \index{volatile}
8647 If the access to these variables is not
8650 \begin_inset LatexCommand \index{atomic access}
8657 the processor needs more than one instruction for the access and could
8658 be interrupted while accessing the variable) the interrupt must disabled
8659 during the access to avoid inconsistent data.
8660 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
8661 and should be protected by disabling interrupts.
8662 You're not automatically on the safe side if you use 8 bit variables though.
8663 We need an example here: f.e.
8664 on the 8051 the harmless looking
8665 \begin_inset Quotes srd
8675 \begin_inset Quotes sld
8684 \begin_inset Quotes srd
8694 \begin_inset Quotes sld
8697 from within an interrupt routine might get lost if the interrupt occurs
8700 \begin_inset Quotes sld
8705 counter\SpecialChar ~
8710 \begin_inset Quotes srd
8713 is not atomic on the 8051 even if
8717 is located in data memory.
8718 Bugs like these are hard to reproduce and can cause a lot of trouble.
8722 A special note here, int (16 bit) and long (32 bit) integer division
8723 \begin_inset LatexCommand \index{Division}
8728 \begin_inset LatexCommand \index{Multiplication}
8733 \begin_inset LatexCommand \index{Modulus}
8738 \begin_inset LatexCommand \index{Floating point support}
8742 operations are implemented using external support routines developed in
8744 If an interrupt service routine needs to do any of these operations then
8745 the support routines (as mentioned in a following section) will have to
8746 be recompiled using the
8759 \begin_inset LatexCommand \index{-\/-stack-auto}
8765 option and the source file will need to be compiled using the
8780 \begin_inset LatexCommand \index{-\/-int-long-rent}
8787 Calling other functions from an interrupt service routine is not recommended,
8788 avoid it if possible.
8789 Note that when some function is called from an interrupt service routine
8790 it should be preceded by a #pragma\SpecialChar ~
8792 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8796 if it is not reentrant.
8797 Furthermore nonreentrant functions should not be called from the main program
8798 while the interrupt service routine might be active.
8804 \begin_inset LatexCommand \ref{sub:Overlaying}
8809 about Overlaying and section
8810 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
8815 about Functions using private banks.
8818 Enabling and Disabling Interrupts
8819 \layout Subsubsection
8821 Critical Functions and Critical Statements
8824 A special keyword may be associated with a function declaring it as
8829 SDCC will generate code to disable all interrupts
8830 \begin_inset LatexCommand \index{interrupt}
8834 upon entry to a critical function and restore the interrupt enable to the
8835 previous state before returning.
8836 Nesting critical functions will need one additional byte on the stack
8837 \begin_inset LatexCommand \index{stack}
8847 \begin_inset LatexCommand \index{critical}
8872 The critical attribute maybe used with other attributes like
8882 may also be used to disable interrupts more locally:
8890 More than one statement could have been included in the block.
8891 \layout Subsubsection
8893 Enabling and Disabling Interrupts directly
8897 \begin_inset LatexCommand \index{interrupt}
8901 can also be disabled and enabled directly (8051):
8919 Note: it is sometimes sufficient to disable only a specific interrupt source
8921 a timer or serial interrupt by manipulating an
8924 \begin_inset LatexCommand \index{interrupt mask}
8931 Usually the time during which interrupts are disabled should be kept as
8938 \begin_inset LatexCommand \index{interrupt latency}
8942 (the time between the occurrence of the interrupt and the execution of
8943 the first code in the interrupt routine) and
8948 \begin_inset LatexCommand \index{interrupt jitter}
8952 (the difference between the shortest and the longest interrupt latency).
8955 You can reenable interrupts within an interrupt routine and on some architecture
8956 s you can make use of two (or more) levels of
8958 interrupt priorities
8961 \begin_inset LatexCommand \index{interrupt priority}
8966 On architectures which don't support interrupt priorities these can be
8967 implemented by manipulating the interrupt mask and reenabling interrupts
8968 within the interrupt routine.
8969 Don't add complexity unless you have to.
8973 Functions using private banks
8974 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
8979 \begin_inset LatexCommand \index{bank}
8989 \begin_inset LatexCommand \index{using}
8995 attribute (which tells the compiler to use a register bank other than the
8996 default bank zero) should only be applied to
8999 \begin_inset LatexCommand \index{interrupt}
9005 functions (see note 1 below).
9006 This will in most circumstances make the generated ISR code more efficient
9007 since it will not have to save registers on the stack.
9014 attribute will have no effect on the generated code for a
9018 function (but may occasionally be useful anyway
9024 possible exception: if a function is called ONLY from 'interrupt' functions
9025 using a particular bank, it can be declared with the same 'using' attribute
9026 as the calling 'interrupt' functions.
9027 For instance, if you have several ISRs using bank one, and all of them
9028 call memcpy(), it might make sense to create a specialized version of memcpy()
9029 'using 1', since this would prevent the ISR from having to save bank zero
9030 to the stack on entry and switch to bank zero before calling the function
9037 (pending: I don't think this has been done yet)
9044 function using a non-zero bank will assume that it can trash that register
9045 bank, and will not save it.
9046 Since high-priority interrupts
9047 \begin_inset LatexCommand \index{interrupt priority}
9051 can interrupt low-priority ones on the 8051 and friends, this means that
9052 if a high-priority ISR
9056 a particular bank occurs while processing a low-priority ISR
9060 the same bank, terrible and bad things can happen.
9061 To prevent this, no single register bank should be
9065 by both a high priority and a low priority ISR.
9066 This is probably most easily done by having all high priority ISRs use
9067 one bank and all low priority ISRs use another.
9068 If you have an ISR which can change priority at runtime, you're on your
9069 own: I suggest using the default bank zero and taking the small performance
9073 It is most efficient if your ISR calls no other functions.
9074 If your ISR must call other functions, it is most efficient if those functions
9075 use the same bank as the ISR (see note 1 below); the next best is if the
9076 called functions use bank zero.
9077 It is very inefficient to call a function using a different, non-zero bank
9083 \begin_inset LatexCommand \label{sub:Startup-Code}
9088 \begin_inset LatexCommand \index{Startup code}
9093 \layout Subsubsection
9095 MCS51/DS390 Startup Code
9098 The compiler inserts a call to the C routine
9100 _sdcc_external_startup()
9101 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
9110 at the start of the CODE area.
9111 This routine is in the runtime library
9112 \begin_inset LatexCommand \index{Runtime library}
9117 By default this routine returns 0, if this routine returns a non-zero value,
9118 the static & global variable initialization will be skipped and the function
9119 main will be invoked.
9120 Otherwise static & global variables will be initialized before the function
9124 _sdcc_external_startup()
9126 routine to your program to override the default if you need to setup hardware
9127 or perform some other critical operation prior to static & global variable
9129 See also the compiler option
9148 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
9153 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9158 about MCS51-variants.
9159 \layout Subsubsection
9164 On the Z80 the startup code is inserted by linking with crt0.o which is generated
9165 from sdcc/device/lib/z80/crt0.s.
9166 If you need a different startup code you can use the compiler option
9187 \begin_inset LatexCommand \index{-\/-no-std-crt0}
9191 and provide your own crt0.o.
9195 Inline Assembler Code
9196 \begin_inset LatexCommand \index{Assembler routines}
9201 \layout Subsubsection
9203 A Step by Step Introduction
9206 Starting from a small snippet of c-code this example shows for the MCS51
9207 how to use inline assembly, access variables, a function parameter and
9208 an array in xdata memory.
9209 This is a buffer routine which should be optimized:
9215 unsigned char xdata at 0x7f00 buf[0x100];
9217 unsigned char head,tail;
9221 void to_buffer( unsigned char c )
9229 if( head != tail-1 )
9244 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
9245 then a corresponding buffer.asm file is generated.
9246 We define a new function
9250 in file buffer.c in which we cut and paste the generated code, removing
9251 unwanted comments and some ':'.
9253 \begin_inset Quotes sld
9257 \begin_inset Quotes srd
9261 \begin_inset Quotes sld
9265 \begin_inset Quotes srd
9268 to the beginning and the end of the function body:
9274 /* With a cut and paste from the .asm file, we have something to start with.
9279 The function is not yet OK! (registers aren't saved) */
9281 void to_buffer_asm( unsigned char c )
9290 \begin_inset LatexCommand \index{\_asm}
9304 ;buffer.c if( head != tail-1 )
9346 ;buffer.c buf[ head++ ] = c;
9442 The new file buffer.c should compile with only one warning about the unreferenced
9443 function argument 'c'.
9444 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
9451 unsigned char xdata at 0x7f00 buf[0x100];
9453 unsigned char head,tail;
9455 #define USE_ASSEMBLY (1)
9459 #ifndef USE_ASSEMBLY
9461 void to_buffer( unsigned char c )
9469 if( head != tail-1 )
9489 void to_buffer( unsigned char c )
9497 c; // to avoid warning: unreferenced function argument
9504 \begin_inset LatexCommand \index{\_asm}
9518 ; save used registers here.
9529 ; If we were still using r2,r3 we would have to push them here.
9532 ; if( head != tail-1 )
9575 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
9599 ; buf[ head++ ] = c;
9610 a,dpl \SpecialChar ~
9617 ; dpl holds lower byte of function argument
9628 dpl,_head \SpecialChar ~
9631 ; buf is 0x100 byte aligned so head can be used directly
9673 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
9685 ; restore used registers here
9698 The inline assembler code can contain any valid code understood by the assembler
9699 , this includes any assembler directives and comment lines
9705 The assembler does not like some characters like ':' or ''' in comments.
9709 The compiler does not do any validation of the code within the
9712 \begin_inset LatexCommand \index{\_asm}
9720 Specifically it will not know which registers are used and thus register
9722 \begin_inset LatexCommand \index{push/pop}
9726 has to be done manually.
9730 It is strongly recommended that each assembly instruction (including labels)
9731 be placed in a separate line (as the example shows).
9745 \begin_inset LatexCommand \index{-\/-peep-asm}
9751 command line option is used, the inline assembler code will be passed through
9752 the peephole optimizer
9753 \begin_inset LatexCommand \index{Peephole optimizer}
9758 There are only a few (if any) cases where this option makes sense, it might
9759 cause some unexpected changes in the inline assembler code.
9760 Please go through the peephole optimizer rules defined in file
9764 carefully before using this option.
9765 \layout Subsubsection
9768 \begin_inset LatexCommand \label{sub:Naked-Functions}
9773 \begin_inset LatexCommand \index{Naked functions}
9780 A special keyword may be associated with a function declaring it as
9783 \begin_inset LatexCommand \index{\_naked}
9794 function modifier attribute prevents the compiler from generating prologue
9795 \begin_inset LatexCommand \index{function prologue}
9800 \begin_inset LatexCommand \index{function epilogue}
9804 code for that function.
9805 This means that the user is entirely responsible for such things as saving
9806 any registers that may need to be preserved, selecting the proper register
9807 bank, generating the
9811 instruction at the end, etc.
9812 Practically, this means that the contents of the function must be written
9813 in inline assembler.
9814 This is particularly useful for interrupt functions, which can have a large
9815 (and often unnecessary) prologue/epilogue.
9816 For example, compare the code generated by these two functions:
9822 \begin_inset LatexCommand \index{volatile}
9826 data unsigned char counter;
9830 void simpleInterrupt(void) interrupt
9831 \begin_inset LatexCommand \index{interrupt}
9849 void nakedInterrupt(void) interrupt 2 _naked
9858 \begin_inset LatexCommand \index{\_asm}
9887 ; MUST explicitly include ret or reti in _naked function.
9894 \begin_inset LatexCommand \index{\_endasm}
9903 For an 8051 target, the generated simpleInterrupt looks like:
10044 whereas nakedInterrupt looks like:
10068 ; MUST explicitly include ret or reti in _naked function.
10071 The #pragma directive EXCLUDE
10072 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
10076 allows a more fine grained control over pushing & popping
10077 \begin_inset LatexCommand \index{push/pop}
10084 While there is nothing preventing you from writing C code inside a _naked
10085 function, there are many ways to shoot yourself in the foot doing this,
10086 and it is recommended that you stick to inline assembler.
10087 \layout Subsubsection
10089 Use of Labels within Inline Assembler
10092 SDCC allows the use of in-line assembler with a few restriction as regards
10094 In older versions of the compiler all labels defined within inline assembler
10103 where nnnn is a number less than 100 (which implies a limit of utmost 100
10104 inline assembler labels
10118 \begin_inset LatexCommand \index{\_asm}
10148 \begin_inset LatexCommand \index{\_endasm}
10155 Inline assembler code cannot reference any C-Labels, however it can reference
10157 \begin_inset LatexCommand \index{Labels}
10161 defined by the inline assembler, e.g.:
10186 ; some assembler code
10206 /* some more c code */
10208 clabel:\SpecialChar ~
10210 /* inline assembler cannot reference this label */
10222 $0003: ;label (can be reference by inline assembler only)
10234 /* some more c code */
10239 In other words inline assembly code can access labels defined in inline
10240 assembly within the scope of the function.
10241 The same goes the other way, ie.
10242 labels defines in inline assembly can not be accessed by C statements.
10245 Interfacing with Assembler Code
10246 \begin_inset LatexCommand \index{Assembler routines}
10251 \layout Subsubsection
10253 Global Registers used for Parameter Passing
10254 \begin_inset LatexCommand \index{Parameter passing}
10261 The compiler always uses the global registers
10264 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10269 \begin_inset LatexCommand \index{B (register)}
10278 \begin_inset LatexCommand \index{ACC}
10284 to pass the first parameter to a routine.
10285 The second parameter onwards is either allocated on the stack (for reentrant
10296 -stack-auto is used) or in data / xdata memory (depending on the memory
10299 \layout Subsubsection
10301 Assembler Routine(non-reentrant
10302 \begin_inset LatexCommand \index{reentrant}
10307 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
10314 In the following example the function c_func calls an assembler routine
10315 asm_func, which takes two parameters.
10320 extern int asm_func(unsigned char, unsigned char);
10324 int c_func (unsigned char i, unsigned char j)
10332 return asm_func(i,j);
10346 return c_func(10,9);
10351 The corresponding assembler function is:
10356 .globl _asm_func_PARM_2
10420 add a,_asm_func_PARM_2
10445 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10462 Note here that the return values are placed in 'dpl' - One byte return value,
10463 'dpl' LSB & 'dph' MSB for two byte values.
10464 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
10465 b' & 'acc' for four byte values.
10468 The parameter naming convention is _<function_name>_PARM_<n>, where n is
10469 the parameter number starting from 1, and counting from the left.
10470 The first parameter is passed in
10471 \begin_inset Quotes eld
10475 \begin_inset Quotes erd
10478 for One bye parameter,
10479 \begin_inset Quotes eld
10483 \begin_inset Quotes erd
10487 \begin_inset Quotes eld
10491 \begin_inset Quotes erd
10494 for three bytes and
10495 \begin_inset Quotes eld
10499 \begin_inset Quotes erd
10502 for four bytes, the variable name for the second parameter will be _<function_n
10507 Assemble the assembler routine with the following command:
10514 asx8051 -losg asmfunc.asm
10521 Then compile and link the assembler routine to the C source file with the
10529 sdcc cfunc.c asmfunc.rel
10530 \layout Subsubsection
10532 Assembler Routine(reentrant
10533 \begin_inset LatexCommand \index{reentrant}
10538 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
10545 In this case the second parameter onwards will be passed on the stack, the
10546 parameters are pushed from right to left i.e.
10547 after the call the left most parameter will be on the top of the stack.
10548 Here is an example:
10553 extern int asm_func(unsigned char, unsigned char);
10557 int c_func (unsigned char i, unsigned char j) reentrant
10565 return asm_func(i,j);
10579 return c_func(10,9);
10584 The corresponding assembler routine is:
10690 The compiling and linking procedure remains the same, however note the extra
10691 entry & exit linkage required for the assembler code, _bp is the stack
10692 frame pointer and is used to compute the offset into the stack for parameters
10693 and local variables.
10697 \begin_inset LatexCommand \index{int (16 bit)}
10702 \begin_inset LatexCommand \index{long (32 bit)}
10709 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
10710 multiplication and modulus operations are implemented by support routines.
10711 These support routines are all developed in ANSI-C to facilitate porting
10712 to other MCUs, although some model specific assembler optimizations are
10714 The following files contain the described routines, all of them can be
10715 found in <installdir>/share/sdcc/lib.
10721 \begin_inset Tabular
10722 <lyxtabular version="3" rows="11" columns="2">
10724 <column alignment="center" valignment="top" leftline="true" width="0">
10725 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
10726 <row topline="true" bottomline="true">
10727 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10748 <row topline="true">
10749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10762 16 bit multiplication
10766 <row topline="true">
10767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10780 signed 16 bit division (calls _divuint)
10784 <row topline="true">
10785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10793 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10798 unsigned 16 bit division
10802 <row topline="true">
10803 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10811 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10816 signed 16 bit modulus (calls _moduint)
10820 <row topline="true">
10821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10834 unsigned 16 bit modulus
10838 <row topline="true">
10839 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10847 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10852 32 bit multiplication
10856 <row topline="true">
10857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10865 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10870 signed 32 division (calls _divulong)
10874 <row topline="true">
10875 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10883 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10888 unsigned 32 division
10892 <row topline="true">
10893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10901 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10906 signed 32 bit modulus (calls _modulong)
10910 <row topline="true" bottomline="true">
10911 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10919 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10924 unsigned 32 bit modulus
10937 Since they are compiled as
10942 \begin_inset LatexCommand \index{reentrant}
10947 \begin_inset LatexCommand \index{interrupt}
10951 service routines should not do any of the above operations.
10952 If this is unavoidable then the above routines will need to be compiled
10966 \begin_inset LatexCommand \index{-\/-stack-auto}
10972 option, after which the source program will have to be compiled with
10985 \begin_inset LatexCommand \index{-\/-int-long-rent}
10992 Notice that you don't have to call this routines directly.
10993 The compiler will use them automatically every time an integer operation
10997 Floating Point Support
10998 \begin_inset LatexCommand \index{Floating point support}
11005 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
11006 floating point support routines are derived from gcc's floatlib.c and consist
11007 of the following routines:
11015 \begin_inset Tabular
11016 <lyxtabular version="3" rows="17" columns="2">
11018 <column alignment="center" valignment="top" leftline="true" width="0">
11019 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
11020 <row topline="true" bottomline="true">
11021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11038 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11047 <row topline="true">
11048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11065 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11079 add floating point numbers
11083 <row topline="true">
11084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11101 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11115 subtract floating point numbers
11119 <row topline="true">
11120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11151 divide floating point numbers
11155 <row topline="true">
11156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11187 multiply floating point numbers
11191 <row topline="true">
11192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11223 convert floating point to unsigned char
11227 <row topline="true">
11228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11259 convert floating point to signed char
11263 <row topline="true">
11264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11295 convert floating point to unsigned int
11299 <row topline="true">
11300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11331 convert floating point to signed int
11335 <row topline="true">
11336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11376 convert floating point to unsigned long
11380 <row topline="true">
11381 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11412 convert floating point to signed long
11416 <row topline="true">
11417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11448 convert unsigned char to floating point
11452 <row topline="true">
11453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11470 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11484 convert char to floating point number
11488 <row topline="true">
11489 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11520 convert unsigned int to floating point
11524 <row topline="true">
11525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11542 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11556 convert int to floating point numbers
11560 <row topline="true">
11561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11578 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11592 convert unsigned long to floating point number
11596 <row topline="true" bottomline="true">
11597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11614 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11628 convert long to floating point number
11641 Note if all these routines are used simultaneously the data space might
11643 For serious floating point usage it is strongly recommended that the large
11645 Also notice that you don't have to call this routines directly.
11646 The compiler will use them automatically every time a floating point operation
11650 MCS51 Memory Models
11651 \begin_inset LatexCommand \index{Memory model}
11656 \begin_inset LatexCommand \index{MCS51 memory}
11663 SDCC allows two memory models for MCS51 code,
11672 Modules compiled with different memory models should
11676 be combined together or the results would be unpredictable.
11677 The library routines supplied with the compiler are compiled as both small
11679 The compiled library modules are contained in separate directories as small
11680 and large so that you can link to either set.
11684 When the large model is used all variables declared without a storage class
11685 will be allocated into the external ram, this includes all parameters and
11686 local variables (for non-reentrant
11687 \begin_inset LatexCommand \index{reentrant}
11692 When the small model is used variables without storage class are allocated
11693 in the internal ram.
11696 Judicious usage of the processor specific storage classes
11697 \begin_inset LatexCommand \index{Storage class}
11701 and the 'reentrant' function type will yield much more efficient code,
11702 than using the large model.
11703 Several optimizations are disabled when the program is compiled using the
11704 large model, it is therefore strongly recommended that the small model
11705 be used unless absolutely required.
11708 DS390 Memory Models
11709 \begin_inset LatexCommand \index{Memory model}
11714 \begin_inset LatexCommand \index{DS390 memory model}
11721 The only model supported is Flat 24
11722 \begin_inset LatexCommand \index{Flat 24 (memory model)}
11727 This generates code for the 24 bit contiguous addressing mode of the Dallas
11729 In this mode, up to four meg of external RAM or code space can be directly
11731 See the data sheets at www.dalsemi.com for further information on this part.
11735 Note that the compiler does not generate any code to place the processor
11736 into 24 bitmode (although
11740 in the ds390 libraries will do that for you).
11746 \begin_inset LatexCommand \index{Tinibios (DS390)}
11750 , the boot loader or similar code must ensure that the processor is in 24
11751 bit contiguous addressing mode before calling the SDCC startup code.
11769 option, variables will by default be placed into the XDATA segment.
11774 Segments may be placed anywhere in the 4 meg address space using the usual
11786 Note that if any segments are located above 64K, the -r flag must be passed
11787 to the linker to generate the proper segment relocations, and the Intel
11788 HEX output format must be used.
11789 The -r flag can be passed to the linker by using the option
11793 on the SDCC command line.
11794 However, currently the linker can not handle code segments > 64k.
11798 \begin_inset LatexCommand \index{Pragmas}
11805 SDCC supports the following #pragma directives.
11809 \begin_inset LatexCommand \index{\#pragma SAVE}
11813 - this will save all current options to the SAVE/RESTORE stack.
11818 \begin_inset LatexCommand \index{\#pragma RESTORE}
11822 - will restore saved options from the last save.
11823 SAVEs & RESTOREs can be nested.
11824 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
11825 RESTORE pulls current options from the stack.
11830 \begin_inset LatexCommand \index{\#pragma NOGCSE}
11834 - will stop global common subexpression elimination.
11838 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
11842 - will stop loop induction optimizations.
11846 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
11850 - will not generate code for boundary value checking, when switch statements
11851 are turned into jump-tables (dangerous).
11852 For more details see section
11853 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
11861 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
11865 - the compiler will not overlay the parameters and local variables of a
11870 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
11874 - the compiler will not warn you anymore for obvious mistakes, you'r on
11879 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
11883 - Will not do loop reversal optimization
11887 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11891 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
11893 \begin_inset LatexCommand \index{push/pop}
11897 instruction in ISR function (using interrupt
11898 \begin_inset LatexCommand \index{interrupt}
11903 The directive should be placed immediately before the ISR function definition
11904 and it affects ALL ISR functions following it.
11905 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
11906 EXCLUDE\SpecialChar ~
11908 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11916 \begin_inset LatexCommand \index{\#pragma NOIV}
11920 - Do not generate interrupt vector table entries for all ISR functions
11921 defined after the pragma.
11922 This is useful in cases where the interrupt vector table must be defined
11923 manually, or when there is a secondary, manually defined interrupt vector
11925 for the autovector feature of the Cypress EZ-USB FX2).
11926 More elegantly this can be achieved by obmitting the optional interrupt
11927 number after the interrupt keyword, see section
11928 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
11937 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11942 \begin_inset LatexCommand \index{function prologue}
11946 function1[,function2[,function3...]] - The compiler by default uses a caller
11947 saves convention for register saving across function calls, however this
11948 can cause unnecessary register pushing & popping when calling small functions
11949 from larger functions.
11950 This option can be used to switch off the register saving convention for
11951 the function names specified.
11952 The compiler will not save registers when calling these functions, extra
11953 code need to be manually inserted at the entry & exit for these functions
11954 to save & restore the registers used by these functions, this can SUBSTANTIALLY
11955 reduce code & improve run time performance of the generated code.
11956 In the future the compiler (with inter procedural analysis) may be able
11957 to determine the appropriate scheme to use for each function call.
11968 -callee-saves command line option is used, the function names specified
11969 in #pragma\SpecialChar ~
11971 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11975 is appended to the list of functions specified in the command line.
11978 The pragma's are intended to be used to turn-on or off certain optimizations
11979 which might cause the compiler to generate extra stack / data space to
11980 store compiler generated temporary variables.
11981 This usually happens in large functions.
11982 Pragma directives should be used as shown in the following example, they
11983 are used to control options & optimizations for a given function; pragmas
11984 should be placed before and/or after a function, placing pragma's inside
11985 a function body could have unpredictable results.
11991 \begin_inset LatexCommand \index{\#pragma SAVE}
12002 /* save the current settings */
12005 \begin_inset LatexCommand \index{\#pragma NOGCSE}
12014 /* turnoff global subexpression elimination */
12016 #pragma NOINDUCTION
12017 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
12021 /* turn off induction optimizations */
12044 \begin_inset LatexCommand \index{\#pragma RESTORE}
12048 /* turn the optimizations back on */
12051 The compiler will generate a warning message when extra space is allocated.
12052 It is strongly recommended that the SAVE and RESTORE pragma's be used when
12053 changing options for a function.
12056 Defines Created by the Compiler
12057 \begin_inset LatexCommand \index{Defines created by the compiler}
12064 The compiler creates the following #defines
12065 \begin_inset LatexCommand \index{\#defines}
12075 \begin_inset Tabular
12076 <lyxtabular version="3" rows="10" columns="2">
12078 <column alignment="center" valignment="top" leftline="true" width="0">
12079 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12080 <row topline="true" bottomline="true">
12081 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12091 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12102 <row topline="true">
12103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12109 \begin_inset LatexCommand \index{SDCC}
12116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12121 this Symbol is always defined
12125 <row topline="true">
12126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12132 \begin_inset LatexCommand \index{SDCC\_mcs51}
12137 \begin_inset LatexCommand \index{SDCC\_ds390}
12142 \begin_inset LatexCommand \index{SDCC\_z80}
12149 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12154 depending on the model used (e.g.: -mds390
12158 <row topline="true">
12159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12165 \begin_inset LatexCommand \index{\_\_mcs51}
12170 \begin_inset LatexCommand \index{\_\_ds390}
12175 \begin_inset LatexCommand \index{\_\_z80}
12182 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12187 depending on the model used (e.g.
12192 <row topline="true">
12193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12199 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
12206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12229 <row topline="true">
12230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12236 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
12243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12266 <row topline="true">
12267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12273 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
12280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12303 <row topline="true">
12304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12310 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
12317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12340 <row topline="true">
12341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12347 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
12354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12367 <row topline="true" bottomline="true">
12368 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12374 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
12381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12403 Debugging with SDCDB
12404 \begin_inset LatexCommand \index{sdcdb}
12411 SDCC is distributed with a source level debugger
12412 \begin_inset LatexCommand \index{Debugger}
12417 The debugger uses a command line interface, the command repertoire of the
12418 debugger has been kept as close to gdb
12419 \begin_inset LatexCommand \index{gdb}
12423 (the GNU debugger) as possible.
12424 The configuration and build process is part of the standard compiler installati
12425 on, which also builds and installs the debugger in the target directory
12426 specified during configuration.
12427 The debugger allows you debug BOTH at the C source and at the ASM source
12429 Sdcdb is available on Unix platforms only.
12432 Compiling for Debugging
12435 The \SpecialChar \-
12437 debug option must be specified for all files for which debug information
12438 is to be generated.
12439 The complier generates a .adb file for each of these files.
12440 The linker creates the .cdb file from the .adb files and the address information.
12441 This .cdb is used by the debugger.
12444 How the Debugger Works
12457 -debug option is specified the compiler generates extra symbol information
12458 some of which are put into the assembler source and some are put into the
12460 Then the linker creates the .cdb file from the individual .adb files with
12461 the address information for the symbols.
12462 The debugger reads the symbolic information generated by the compiler &
12463 the address information generated by the linker.
12464 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
12465 execution is controlled by the debugger.
12466 When a command is issued for the debugger, it translates it into appropriate
12467 commands for the simulator.
12470 Starting the Debugger
12473 The debugger can be started using the following command line.
12474 (Assume the file you are debugging has the file name foo).
12488 The debugger will look for the following files.
12491 foo.c - the source file.
12494 foo.cdb - the debugger symbol information file.
12497 foo.ihx - the Intel hex format
12498 \begin_inset LatexCommand \index{Intel hex format}
12505 Command Line Options.
12518 -directory=<source file directory> this option can used to specify the directory
12520 The debugger will look into the directory list specified for source, cdb
12522 The items in the directory list must be separated by ':', e.g.
12523 if the source files can be in the directories /home/src1 and /home/src2,
12534 -directory option should be -
12544 -directory=/home/src1:/home/src2.
12545 Note there can be no spaces in the option.
12549 -cd <directory> - change to the <directory>.
12552 -fullname - used by GUI front ends.
12555 -cpu <cpu-type> - this argument is passed to the simulator please see the
12556 simulator docs for details.
12559 -X <Clock frequency > this options is passed to the simulator please see
12560 the simulator docs for details.
12563 -s <serial port file> passed to simulator see the simulator docs for details.
12566 -S <serial in,out> passed to simulator see the simulator docs for details.
12572 As mentioned earlier the command interface for the debugger has been deliberatel
12573 y kept as close the GNU debugger gdb, as possible.
12574 This will help the integration with existing graphical user interfaces
12575 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
12576 If you use a graphical user interface for the debugger you can skip the
12578 \layout Subsubsection*
12580 break [line | file:line | function | file:function]
12583 Set breakpoint at specified line or function:
12592 sdcdb>break foo.c:100
12594 sdcdb>break funcfoo
12596 sdcdb>break foo.c:funcfoo
12597 \layout Subsubsection*
12599 clear [line | file:line | function | file:function ]
12602 Clear breakpoint at specified line or function:
12611 sdcdb>clear foo.c:100
12613 sdcdb>clear funcfoo
12615 sdcdb>clear foo.c:funcfoo
12616 \layout Subsubsection*
12621 Continue program being debugged, after breakpoint.
12622 \layout Subsubsection*
12627 Execute till the end of the current function.
12628 \layout Subsubsection*
12633 Delete breakpoint number 'n'.
12634 If used without any option clear ALL user defined break points.
12635 \layout Subsubsection*
12637 info [break | stack | frame | registers ]
12640 info break - list all breakpoints
12643 info stack - show the function call stack.
12646 info frame - show information about the current execution frame.
12649 info registers - show content of all registers.
12650 \layout Subsubsection*
12655 Step program until it reaches a different source line.
12656 \layout Subsubsection*
12661 Step program, proceeding through subroutine calls.
12662 \layout Subsubsection*
12667 Start debugged program.
12668 \layout Subsubsection*
12673 Print type information of the variable.
12674 \layout Subsubsection*
12679 print value of variable.
12680 \layout Subsubsection*
12685 load the given file name.
12686 Note this is an alternate method of loading file for debugging.
12687 \layout Subsubsection*
12692 print information about current frame.
12693 \layout Subsubsection*
12698 Toggle between C source & assembly source.
12699 \layout Subsubsection*
12701 ! simulator command
12704 Send the string following '!' to the simulator, the simulator response is
12706 Note the debugger does not interpret the command being sent to the simulator,
12707 so if a command like 'go' is sent the debugger can loose its execution
12708 context and may display incorrect values.
12709 \layout Subsubsection*
12716 My name is Bobby Brown"
12719 Interfacing with XEmacs
12720 \begin_inset LatexCommand \index{XEmacs}
12725 \begin_inset LatexCommand \index{Emacs}
12732 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
12733 sdcdb.el and sdcdbsrc.el.
12734 These two files can be found in the $(prefix)/bin directory after the installat
12736 These files need to be loaded into XEmacs for the interface to work.
12737 This can be done at XEmacs startup time by inserting the following into
12738 your '.xemacs' file (which can be found in your HOME directory):
12744 (load-file sdcdbsrc.el)
12750 .xemacs is a lisp file so the () around the command is REQUIRED.
12751 The files can also be loaded dynamically while XEmacs is running, set the
12752 environment variable 'EMACSLOADPATH' to the installation bin directory
12753 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
12754 To start the interface enter the following command:
12768 You will prompted to enter the file name to be debugged.
12773 The command line options that are passed to the simulator directly are bound
12774 to default values in the file sdcdbsrc.el.
12775 The variables are listed below, these values maybe changed as required.
12778 sdcdbsrc-cpu-type '51
12781 sdcdbsrc-frequency '11059200
12784 sdcdbsrc-serial nil
12787 The following is a list of key mapping for the debugger interface.
12795 ;; Current Listing ::
12797 ;;key\SpecialChar ~
12812 binding\SpecialChar ~
12836 ;;---\SpecialChar ~
12851 ------\SpecialChar ~
12891 sdcdb-next-from-src\SpecialChar ~
12917 sdcdb-back-from-src\SpecialChar ~
12943 sdcdb-cont-from-src\SpecialChar ~
12953 SDCDB continue command
12969 sdcdb-step-from-src\SpecialChar ~
12995 sdcdb-whatis-c-sexp\SpecialChar ~
13005 SDCDB ptypecommand for data at
13069 sdcdbsrc-delete\SpecialChar ~
13083 SDCDB Delete all breakpoints if no arg
13131 given or delete arg (C-u arg x)
13147 sdcdbsrc-frame\SpecialChar ~
13162 SDCDB Display current frame if no arg,
13211 given or display frame arg
13276 sdcdbsrc-goto-sdcdb\SpecialChar ~
13286 Goto the SDCDB output buffer
13302 sdcdb-print-c-sexp\SpecialChar ~
13313 SDCDB print command for data at
13377 sdcdbsrc-goto-sdcdb\SpecialChar ~
13387 Goto the SDCDB output buffer
13403 sdcdbsrc-mode\SpecialChar ~
13419 Toggles Sdcdbsrc mode (turns it off)
13423 ;; C-c C-f\SpecialChar ~
13431 sdcdb-finish-from-src\SpecialChar ~
13439 SDCDB finish command
13443 ;; C-x SPC\SpecialChar ~
13451 sdcdb-break\SpecialChar ~
13469 Set break for line with point
13471 ;; ESC t\SpecialChar ~
13481 sdcdbsrc-mode\SpecialChar ~
13497 Toggle Sdcdbsrc mode
13499 ;; ESC m\SpecialChar ~
13509 sdcdbsrc-srcmode\SpecialChar ~
13531 Here are a few guidelines that will help the compiler generate more efficient
13532 code, some of the tips are specific to this compiler others are generally
13533 good programming practice.
13536 Use the smallest data type to represent your data-value.
13537 If it is known in advance that the value is going to be less than 256 then
13538 use an 'unsigned char' instead of a 'short' or 'int'.
13541 Use unsigned when it is known in advance that the value is not going to
13543 This helps especially if you are doing division or multiplication.
13546 NEVER jump into a LOOP.
13549 Declare the variables to be local whenever possible, especially loop control
13550 variables (induction).
13553 Since the compiler does not always do implicit integral promotion, the programme
13554 r should do an explicit cast when integral promotion is required.
13557 Reducing the size of division, multiplication & modulus operations can reduce
13558 code size substantially.
13559 Take the following code for example.
13565 foobar(unsigned int p1, unsigned char ch)
13573 unsigned char ch1 = p1 % ch ;
13584 For the modulus operation the variable ch will be promoted to unsigned int
13585 first then the modulus operation will be performed (this will lead to a
13586 call to support routine _moduint()), and the result will be casted to a
13588 If the code is changed to
13593 foobar(unsigned int p1, unsigned char ch)
13601 unsigned char ch1 = (unsigned char)p1 % ch ;
13612 It would substantially reduce the code generated (future versions of the
13613 compiler will be smart enough to detect such optimization opportunities).
13617 Have a look at the assembly listing to get a
13618 \begin_inset Quotes sld
13622 \begin_inset Quotes srd
13625 for the code generation.
13628 Notes on MCS51 memory
13629 \begin_inset LatexCommand \index{MCS51 memory}
13636 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
13637 RAM memory which is structured as follows
13641 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
13644 - Bytes 20-2F - 16 bytes to hold 128 bit
13645 \begin_inset LatexCommand \index{bit}
13651 - Bytes 30-7F - 80 bytes for general purpose use.
13656 Additionally some members of the MCS51 family may have up to 128 bytes of
13657 additional, indirectly addressable, internal RAM memory (
13662 \begin_inset LatexCommand \index{idata}
13667 Furthermore, some chips may have some built in external memory (
13672 \begin_inset LatexCommand \index{xdata}
13676 ) which should not be confused with the internal, directly addressable RAM
13682 \begin_inset LatexCommand \index{data}
13687 Sometimes this built in
13691 memory has to be activated before using it (you can probably find this
13692 information on the datasheet of the microcontroller your are using).
13695 Normally SDCC will only use the first bank
13696 \begin_inset LatexCommand \index{bank}
13700 of registers (register bank 0), but it is possible to specify that other
13701 banks of registers should be used in interrupt
13702 \begin_inset LatexCommand \index{interrupt}
13707 By default, the compiler will place the stack after the last byte of allocated
13708 memory for variables.
13709 For example, if the first 2 banks of registers are used, and only four
13714 variables, it will position the base of the internal stack at address 20
13716 This implies that as the stack
13717 \begin_inset LatexCommand \index{stack}
13721 grows, it will use up the remaining register banks, and the 16 bytes used
13722 by the 128 bit variables, and 80 bytes for general purpose use.
13723 If any bit variables are used, the data variables will be placed after
13724 the byte holding the last bit variable.
13725 For example, if register banks 0 and 1 are used, and there are 9 bit variables
13730 variables will be placed starting at address 0x22.
13742 \begin_inset LatexCommand \index{-\/-data-loc}
13746 to specify the start address of the
13760 -iram-size to specify the size of the total internal RAM (
13772 By default the 8051 linker will place the stack after the last byte of data
13785 \begin_inset LatexCommand \index{-\/-stack-loc}
13789 allows you to specify the start of the stack, i.e.
13790 you could start it after any data in the general purpose area.
13791 If your microcontroller has additional indirectly addressable internal
13796 ) you can place the stack on it.
13797 You may also need to use -
13808 \begin_inset LatexCommand \index{-\/-data-loc}
13812 to set the start address of the external RAM (
13827 \begin_inset LatexCommand \index{-\/-data-loc}
13831 to specify its size.
13832 Same goes for the code memory, using -
13843 \begin_inset LatexCommand \index{-\/-data-loc}
13858 \begin_inset LatexCommand \index{-\/-data-loc}
13863 If in doubt, don't specify any options and see if the resulting memory
13864 layout is appropriate, then you can adjust it.
13867 The 8051 linker generates two files with memory allocation information.
13868 The first, with extension .map shows all the variables and segments.
13869 The second with extension .mem shows the final memory layout.
13870 The linker will complaint either if memory segments overlap, there is not
13871 enough memory, or there is not enough space for stack.
13872 If you get any linking warnings and/or errors related to stack or segments
13873 allocation, take a look at either the .map or .mem files to find out what
13875 The .mem file may even suggest a solution to the problem.
13879 \begin_inset LatexCommand \index{Tools}
13883 included in the distribution
13887 \begin_inset Tabular
13888 <lyxtabular version="3" rows="12" columns="3">
13890 <column alignment="center" valignment="top" leftline="true" width="0pt">
13891 <column alignment="center" valignment="top" leftline="true" width="0pt">
13892 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
13893 <row topline="true" bottomline="true">
13894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13902 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13910 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13919 <row topline="true">
13920 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13933 Simulator for various architectures
13936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13945 <row topline="true">
13946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13954 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13959 header file conversion
13962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13967 sdcc/support/scripts
13971 <row topline="true">
13972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13985 header file conversion
13988 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13993 sdcc/support/scripts
13997 <row topline="true">
13998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14032 <row topline="true">
14033 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14041 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14067 <row topline="true">
14068 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14084 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14102 <row topline="true">
14103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14119 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14137 <row topline="true">
14138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14172 <row topline="true">
14173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14207 <row topline="true">
14208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14242 <row topline="true" bottomline="true">
14243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14259 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14286 Related open source tools
14287 \begin_inset LatexCommand \index{Related tools}
14295 \begin_inset Tabular
14296 <lyxtabular version="3" rows="8" columns="3">
14298 <column alignment="center" valignment="top" leftline="true" width="0pt">
14299 <column alignment="block" valignment="top" leftline="true" width="30line%">
14300 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
14301 <row topline="true" bottomline="true">
14302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14327 <row topline="true">
14328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14334 \begin_inset LatexCommand \index{gpsim}
14341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14349 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14355 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
14363 <row topline="true">
14364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14386 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
14394 <row topline="true">
14395 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14401 \begin_inset LatexCommand \index{srecord}
14408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14413 Object file conversion, checksumming, ...
14416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14422 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
14430 <row topline="true">
14431 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14437 \begin_inset LatexCommand \index{objdump}
14444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14449 Object file conversion, ...
14452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14457 Part of binutils (should be there anyway)
14461 <row topline="true">
14462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14468 \begin_inset LatexCommand \index{doxygen}
14475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14480 Source code documentation system
14483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14489 \begin_inset LatexCommand \url{http://www.doxygen.org}
14497 <row topline="true">
14498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14504 \begin_inset LatexCommand \index{splint}
14511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14516 Statically checks c sources
14519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14525 \begin_inset LatexCommand \url{http://www.splint.org}
14533 <row topline="true" bottomline="true">
14534 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14540 \begin_inset LatexCommand \index{ddd}
14547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14552 Debugger, serves nicely as GUI to sdcdb
14553 \begin_inset LatexCommand \index{sdcdb}
14560 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14566 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
14583 Related documentation / recommended reading
14587 \begin_inset Tabular
14588 <lyxtabular version="3" rows="5" columns="3">
14590 <column alignment="center" valignment="top" leftline="true" width="0pt">
14591 <column alignment="block" valignment="top" leftline="true" width="30line%">
14592 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
14593 <row topline="true" bottomline="true">
14594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14602 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14619 <row topline="true">
14620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14635 Advanced Compiler Design and Implementation
14638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14647 <row topline="true">
14648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14665 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14671 \begin_inset LatexCommand \index{C Reference card}
14678 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14684 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
14692 <row topline="true">
14693 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14698 test_suite_spec.pdf
14701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14706 sdcc regression test
14707 \begin_inset LatexCommand \index{Regression test}
14714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14723 <row topline="true" bottomline="true">
14724 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14755 sdcc internal documentation
14758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14777 \begin_inset LatexCommand \index{Support}
14784 SDCC has grown to be a large project.
14785 The compiler alone (without the preprocessor, assembler and linker) is
14786 well over 100,000 lines of code (blank stripped).
14787 The open source nature of this project is a key to its continued growth
14789 You gain the benefit and support of many active software developers and
14791 Is SDCC perfect? No, that's why we need your help.
14792 The developers take pride in fixing reported bugs.
14793 You can help by reporting the bugs and helping other SDCC users.
14794 There are lots of ways to contribute, and we encourage you to take part
14795 in making SDCC a great software package.
14799 The SDCC project is hosted on the SDCC sourceforge site at
14800 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
14805 You'll find the complete set of mailing lists
14806 \begin_inset LatexCommand \index{Mailing list}
14810 , forums, bug reporting system, patch submission
14811 \begin_inset LatexCommand \index{Patch submission}
14816 \begin_inset LatexCommand \index{download}
14820 area and cvs code repository
14821 \begin_inset LatexCommand \index{cvs code repository}
14829 \begin_inset LatexCommand \index{Bugs}
14834 \begin_inset LatexCommand \index{Reporting bugs}
14841 The recommended way of reporting bugs is using the infrastructure of the
14843 You can follow the status of bug reports there and have an overview about
14847 Bug reports are automatically forwarded to the developer mailing list and
14848 will be fixed ASAP.
14849 When reporting a bug, it is very useful to include a small test program
14850 (the smaller the better) which reproduces the problem.
14851 If you can isolate the problem by looking at the generated assembly code,
14852 this can be very helpful.
14853 Compiling your program with the -
14864 \begin_inset LatexCommand \index{-\/-dumpall}
14868 option can sometimes be useful in locating optimization problems.
14869 When reporting a bug please maker sure you:
14872 Attach the code you are compiling with SDCC.
14876 Specify the exact command you use to run SDCC, or attach your Makefile.
14880 Specify the SDCC version (type "sdcc -v"), your platform, and operating
14885 Provide an exact copy of any error message or incorrect output.
14889 Put something meaningful in the subject of your message.
14892 Please attempt to include these 5 important parts, as applicable, in all
14893 requests for support or when reporting any problems or bugs with SDCC.
14894 Though this will make your message lengthy, it will greatly improve your
14895 chance that SDCC users and developers will be able to help you.
14896 Some SDCC developers are frustrated by bug reports without code provided
14897 that they can use to reproduce and ultimately fix the problem, so please
14898 be sure to provide sample code if you are reporting a bug!
14901 Please have a short check that you are using a recent version of SDCC and
14902 the bug is not yet known.
14903 This is the link for reporting bugs:
14904 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
14911 Requesting Features
14912 \begin_inset LatexCommand \label{sub:Requesting-Features}
14917 \begin_inset LatexCommand \index{Feature request}
14922 \begin_inset LatexCommand \index{Requesting features}
14929 Like bug reports feature requests are forwarded to the developer mailing
14931 This is the link for requesting features:
14932 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
14942 These links should take you directly to the
14943 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
14953 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
14954 automated messages (mid 2003)
14958 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
14962 , lists and forums are archived so if you are lucky someone already had
14967 \begin_inset LatexCommand \index{Changelog}
14974 You can follow the status of the cvs version
14975 \begin_inset LatexCommand \index{version}
14979 of SDCC by watching the file
14980 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
14984 in the cvs-repository.
14988 \begin_inset LatexCommand \index{Release policy}
14995 Historically there often were long delays between official releases and
14996 the sourceforge download area tends to get not updated at all.
14997 Current excuses might refer to problems with live range analysis, but if
14998 this is fixed, the next problem rising is that another excuse will have
15000 Kidding aside, we have to get better there! On the other hand there are
15001 daily snapshots available at
15002 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
15006 , and you can always built the very last version (hopefully with many bugs
15007 fixed, and features added) from the source code available at
15008 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
15016 \begin_inset LatexCommand \index{Examples}
15023 You'll find some small examples in the directory
15025 sdcc/device/examples/.
15028 More examples and libraries are available at
15030 The SDCC Open Knowledge Resource
15031 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
15038 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
15045 I did insert a reference to Paul's web site here although it seems rather
15046 dedicated to a specific 8032 board (I think it's okay because it f.e.
15047 shows LCD/Harddisc interface and has a free 8051 monitor.
15048 Independent 8032 board vendors face hard competition of heavily subsidized
15049 development boards anyway).
15052 Maybe we should include some links to real world applications.
15053 Preferably pointer to pointers (one for each architecture) so this stays
15058 \begin_inset LatexCommand \index{Quality control}
15065 The compiler is passed through nightly compile and build checks.
15071 \begin_inset LatexCommand \index{Regression test}
15075 check that SDCC itself compiles flawlessly on several platforms and checks
15076 the quality of the code generated by SDCC by running the code through simulator
15078 There is a separate document
15081 \begin_inset LatexCommand \index{Test suite}
15090 You'll find the test code in the directory
15092 sdcc/support/regression
15095 You can run these tests manually by running
15099 in this directory (or f.e.
15104 if you don't want to run the complete tests).
15105 The test code might also be interesting if you want to look for examples
15106 \begin_inset LatexCommand \index{Examples}
15110 checking corner cases of SDCC or if you plan to submit patches
15111 \begin_inset LatexCommand \index{Patch submission}
15118 The pic port uses a different set of regression tests, you'll find them
15121 sdcc/src/regression
15126 SDCC Technical Data
15130 \begin_inset LatexCommand \index{Optimizations}
15137 SDCC performs a host of standard optimizations in addition to some MCU specific
15140 \layout Subsubsection
15142 Sub-expression Elimination
15143 \begin_inset LatexCommand \index{Subexpression elimination}
15150 The compiler does local and global common subexpression elimination, e.g.:
15161 will be translated to
15173 Some subexpressions are not as obvious as the above example, e.g.:
15183 In this case the address arithmetic a->b[i] will be computed only once;
15184 the equivalent code in C would be.
15196 The compiler will try to keep these temporary variables in registers.
15197 \layout Subsubsection
15199 Dead-Code Elimination
15200 \begin_inset LatexCommand \index{Dead-code elimination}
15221 i = 1; \SpecialChar ~
15230 global = 1;\SpecialChar ~
15243 global = 3;\SpecialChar ~
15269 \layout Subsubsection
15272 \begin_inset LatexCommand \index{Copy propagation}
15328 Note: the dead stores created by this copy propagation will be eliminated
15329 by dead-code elimination.
15330 \layout Subsubsection
15333 \begin_inset LatexCommand \index{Loop optimization}
15340 Two types of loop optimizations are done by SDCC loop invariant lifting
15341 and strength reduction of loop induction variables.
15342 In addition to the strength reduction the optimizer marks the induction
15343 variables and the register allocator tries to keep the induction variables
15344 in registers for the duration of the loop.
15345 Because of this preference of the register allocator
15346 \begin_inset LatexCommand \index{Register allocation}
15350 , loop induction optimization causes an increase in register pressure, which
15351 may cause unwanted spilling of other temporary variables into the stack
15352 \begin_inset LatexCommand \index{stack}
15357 The compiler will generate a warning message when it is forced to allocate
15358 extra space either on the stack or data space.
15359 If this extra space allocation is undesirable then induction optimization
15360 can be eliminated either for the entire source file (with -
15370 -noinduction option) or for a given function only using #pragma\SpecialChar ~
15372 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
15385 for (i = 0 ; i < 100 ; i ++)
15401 for (i = 0; i < 100; i++)
15410 As mentioned previously some loop invariants are not as apparent, all static
15411 address computations are also moved out of the loop.
15416 \begin_inset LatexCommand \index{Strength reduction}
15420 , this optimization substitutes an expression by a cheaper expression:
15425 for (i=0;i < 100; i++)
15443 for (i=0;i< 100;i++) {
15449 ar[itemp1] = itemp2;
15466 The more expensive multiplication
15467 \begin_inset LatexCommand \index{Multiplication}
15471 is changed to a less expensive addition.
15472 \layout Subsubsection
15475 \begin_inset LatexCommand \index{Loop reversing}
15482 This optimization is done to reduce the overhead of checking loop boundaries
15483 for every iteration.
15484 Some simple loops can be reversed and implemented using a
15485 \begin_inset Quotes eld
15488 decrement and jump if not zero
15489 \begin_inset Quotes erd
15493 SDCC checks for the following criterion to determine if a loop is reversible
15494 (note: more sophisticated compilers use data-dependency analysis to make
15495 this determination, SDCC uses a more simple minded analysis).
15498 The 'for' loop is of the form
15504 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
15514 The <for body> does not contain
15515 \begin_inset Quotes eld
15519 \begin_inset Quotes erd
15523 \begin_inset Quotes erd
15529 All goto's are contained within the loop.
15532 No function calls within the loop.
15535 The loop control variable <sym> is not assigned any value within the loop
15538 The loop control variable does NOT participate in any arithmetic operation
15542 There are NO switch statements in the loop.
15543 \layout Subsubsection
15545 Algebraic Simplifications
15548 SDCC does numerous algebraic simplifications, the following is a small sub-set
15549 of these optimizations.
15554 i = j + 0 ; /* changed to */ i = j;
15556 i /= 2;\SpecialChar ~
15560 /* changed to */ i >>= 1;
15562 i = j - j ; /* changed to */ i = 0;
15564 i = j / 1 ; /* changed to */ i = j;
15567 Note the subexpressions
15568 \begin_inset LatexCommand \index{Subexpression}
15572 given above are generally introduced by macro expansions or as a result
15573 of copy/constant propagation.
15574 \layout Subsubsection
15576 'switch' Statements
15577 \begin_inset LatexCommand \label{sub:'switch'-Statements}
15582 \begin_inset LatexCommand \index{switch statement}
15589 SDCC changes switch statements to jump tables
15590 \begin_inset LatexCommand \index{jump tables}
15594 when the following conditions are true.
15598 The case labels are in numerical sequence, the labels need not be in order,
15599 and the starting number need not be one or zero.
15605 switch(i) {\SpecialChar ~
15636 case 4: ...\SpecialChar ~
15668 case 5: ...\SpecialChar ~
15700 case 3: ...\SpecialChar ~
15732 case 6: ...\SpecialChar ~
15800 Both the above switch statements will be implemented using a jump-table.
15801 The example to the right side is slightly more efficient as the check for
15802 the lower boundary of the jump-table is not needed.
15806 The number of case labels is at least three, since it takes two conditional
15807 statements to handle the boundary conditions.
15810 The number of case labels is less than 84, since each label takes 3 bytes
15811 and a jump-table can be utmost 256 bytes long.
15814 Switch statements which have gaps in the numeric sequence or those that
15815 have more that 84 case labels can be split into more than one switch statement
15816 for efficient code generation, e.g.:
15866 If the above switch statement is broken down into two switch statements
15905 case 9:\SpecialChar ~
15912 case 10:\SpecialChar ~
15918 case 11:\SpecialChar ~
15924 case 12:\SpecialChar ~
15931 then both the switch statements will be implemented using jump-tables whereas
15932 the unmodified switch statement will not be.
15933 You might also consider dummy cases 0 and 5 to 8 in this example.
15936 The pragma NOJTBOUND
15937 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
15941 can be used to turn off checking the
15954 It has no effect if a default label is supplied.
15955 Use of this pragma is dangerous: if the switch argument is not matched
15956 by a case statement the processor will happily jump into Nirvana.
15957 \layout Subsubsection
15959 Bit-shifting Operations
15960 \begin_inset LatexCommand \index{Bit shifting}
15967 Bit shifting is one of the most frequently used operation in embedded programmin
15969 SDCC tries to implement bit-shift operations in the most efficient way
15985 generates the following code:
16002 In general SDCC will never setup a loop if the shift count is known.
16044 Note that SDCC stores numbers in little-endian
16050 Usually 8-bit processors don't care much about endianness.
16051 This is not the case for the standard 8051 which only has an instruction
16057 \begin_inset LatexCommand \index{DPTR}
16065 so little-endian is the more efficient byte order.
16069 \begin_inset LatexCommand \index{little-endian}
16074 \begin_inset LatexCommand \index{Endianness}
16079 lowest order first).
16080 \layout Subsubsection
16083 \begin_inset LatexCommand \index{Bit rotation}
16090 A special case of the bit-shift operation is bit rotation, SDCC recognizes
16091 the following expression to be a left bit-rotation:
16101 i = ((i << 1) | (i >> 7));
16110 will generate the following code:
16129 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
16130 ns of this case will also be recognized as bit-rotation, i.e.:
16135 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
16136 \layout Subsubsection
16139 \begin_inset LatexCommand \index{Highest Order Bit}
16146 It is frequently required to obtain the highest order bit of an integral
16147 type (long, int, short or char types).
16148 SDCC recognizes the following expression to yield the highest order bit
16149 and generates optimized code for it, e.g.:
16171 hob = (gint >> 15) & 1;
16181 will generate the following code:
16214 000A E5*01\SpecialChar ~
16241 000C 23\SpecialChar ~
16272 000D 54 01\SpecialChar ~
16299 000F F5*02\SpecialChar ~
16327 Variations of this case however will
16332 It is a standard C expression, so I heartily recommend this be the only
16333 way to get the highest order bit, (it is portable).
16334 Of course it will be recognized even if it is embedded in other expressions,
16340 xyz = gint + ((gint >> 15) & 1);
16343 will still be recognized.
16344 \layout Subsubsection
16347 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
16352 \begin_inset LatexCommand \index{Peephole optimizer}
16359 The compiler uses a rule based, pattern matching and re-writing mechanism
16360 for peep-hole optimization.
16365 a peep-hole optimizer by Christopher W.
16366 Fraser (cwfraser@microsoft.com).
16367 A default set of rules are compiled into the compiler, additional rules
16368 may be added with the
16381 \begin_inset LatexCommand \index{-\/-peep-file}
16388 The rule language is best illustrated with examples.
16412 The above rule will change the following assembly
16413 \begin_inset LatexCommand \index{Assembler routines}
16435 Note: All occurrences of a
16439 (pattern variable) must denote the same string.
16440 With the above rule, the assembly sequence:
16450 will remain unmodified.
16454 Other special case optimizations may be added by the user (via
16470 some variants of the 8051 MCU allow only
16479 The following two rules will change all
16498 replace { lcall %1 } by { acall %1 }
16500 replace { ljmp %1 } by { ajmp %1 }
16505 inline-assembler code
16507 is also passed through the peep hole optimizer, thus the peephole optimizer
16508 can also be used as an assembly level macro expander.
16509 The rules themselves are MCU dependent whereas the rule language infra-structur
16510 e is MCU independent.
16511 Peephole optimization rules for other MCU can be easily programmed using
16516 The syntax for a rule is as follows:
16521 rule := replace [ restart ] '{' <assembly sequence> '
16559 <assembly sequence> '
16577 '}' [if <functionName> ] '
16582 <assembly sequence> := assembly instruction (each instruction including
16583 labels must be on a separate line).
16587 The optimizer will apply to the rules one by one from the top in the sequence
16588 of their appearance, it will terminate when all rules are exhausted.
16589 If the 'restart' option is specified, then the optimizer will start matching
16590 the rules again from the top, this option for a rule is expensive (performance)
16591 , it is intended to be used in situations where a transformation will trigger
16592 the same rule again.
16593 An example of this (not a good one, it has side effects) is the following
16616 Note that the replace pattern cannot be a blank, but can be a comment line.
16617 Without the 'restart' option only the inner most 'pop' 'push' pair would
16618 be eliminated, i.e.:
16648 the restart option the rule will be applied again to the resulting code
16649 and then all the pop-push pairs will be eliminated to yield:
16659 A conditional function can be attached to a rule.
16660 Attaching rules are somewhat more involved, let me illustrate this with
16687 The optimizer does a look-up of a function name table defined in function
16692 in the source file SDCCpeeph.c, with the name
16697 If it finds a corresponding entry the function is called.
16698 Note there can be no parameters specified for these functions, in this
16703 is crucial, since the function
16707 expects to find the label in that particular variable (the hash table containin
16708 g the variable bindings is passed as a parameter).
16709 If you want to code more such functions, take a close look at the function
16710 labelInRange and the calling mechanism in source file SDCCpeeph.c.
16711 Currently implemented are
16713 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
16714 24bitMode, portIsDS390, 24bitModeAndPortDS390
16723 I know this whole thing is a little kludgey, but maybe some day we will
16724 have some better means.
16725 If you are looking at this file, you will see the default rules that are
16726 compiled into the compiler, you can add your own rules in the default set
16727 there if you get tired of specifying the -
16747 <pending: this is messy and incomplete>
16752 Compiler support routines (_gptrget, _mulint etc)
16755 Stdclib functions (puts, printf, strcat etc)
16758 Math functions (sin, pow, sqrt etc)
16762 \begin_inset LatexCommand \index{Libraries}
16766 included in SDCC should have a license at least as liberal as the GNU Lesser
16767 General Public License
16768 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
16779 license statements for the libraries are missing.
16780 sdcc/device/lib/ser_ir.c
16784 come with a GPL (as opposed to LGPL) License - this will not be liberal
16785 enough for many embedded programmers.
16788 If you have ported some library or want to share experience about some code
16790 falls into any of these categories Busses (I
16791 \begin_inset Formula $^{\textrm{2}}$
16794 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
16795 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
16796 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
16797 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
16802 would certainly like to hear about it.
16803 Programmers coding for embedded systems are not especially famous for being
16804 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
16805 e these references are very valuable.
16809 \begin_inset LatexCommand \label{sub:External-Stack}
16814 \begin_inset LatexCommand \index{stack}
16819 \begin_inset LatexCommand \index{External stack}
16826 The external stack (-
16837 \begin_inset LatexCommand \index{-\/-xstack}
16841 ) is located at the start of the external ram segment, and is 256 bytes
16853 -xstack option is used to compile the program, the parameters and local
16854 variables of all reentrant functions are allocated in this area.
16855 This option is provided for programs with large stack space requirements.
16856 When used with the -
16867 \begin_inset LatexCommand \index{-\/-stack-auto}
16871 option, all parameters and local variables are allocated on the external
16872 stack (note support libraries will need to be recompiled with the same
16876 The compiler outputs the higher order address byte of the external ram segment
16877 into PORT P2, therefore when using the External Stack option, this port
16878 MAY NOT be used by the application program.
16882 \begin_inset LatexCommand \index{ANSI-compliance}
16887 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
16894 Deviations from the compliance:
16897 functions are not always reentrant.
16900 structures cannot be assigned values directly, cannot be passed as function
16901 parameters or assigned to each other and cannot be a return value from
16928 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
16939 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
16961 return rets;/* is invalid in SDCC although allowed in ANSI */
16968 \begin_inset LatexCommand \index{long long (not supported)}
16973 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
16981 \begin_inset LatexCommand \index{double (not supported)}
16985 ' precision floating point
16986 \begin_inset LatexCommand \index{Floating point support}
16993 No support for setjmp and longjmp (for now).
16997 \begin_inset LatexCommand \index{K\&R style}
17001 function declarations are NOT allowed.
17007 foo(i,j) /* this old style of function declarations */
17009 int i,j; /* are valid in ANSI but not valid in SDCC */
17024 functions declared as pointers
17025 \begin_inset LatexCommand \index{Pointers}
17030 \begin_inset LatexCommand \index{function pointers}
17034 must be dereferenced during the call.
17045 /* has to be called like this */
17047 (*foo)(); /* ANSI standard allows calls to be made like 'foo()' */
17051 Cyclomatic Complexity
17052 \begin_inset LatexCommand \index{Cyclomatic complexity}
17059 Cyclomatic complexity of a function is defined as the number of independent
17060 paths the program can take during execution of the function.
17061 This is an important number since it defines the number test cases you
17062 have to generate to validate the function.
17063 The accepted industry standard for complexity number is 10, if the cyclomatic
17064 complexity reported by SDCC exceeds 10 you should think about simplification
17065 of the function logic.
17066 Note that the complexity level is not related to the number of lines of
17067 code in a function.
17068 Large functions can have low complexity, and small functions can have large
17074 SDCC uses the following formula to compute the complexity:
17079 complexity = (number of edges in control flow graph) - (number of nodes
17080 in control flow graph) + 2;
17084 Having said that the industry standard is 10, you should be aware that in
17085 some cases it be may unavoidable to have a complexity level of less than
17087 For example if you have switch statement with more than 10 case labels,
17088 each case label adds one to the complexity level.
17089 The complexity level is by no means an absolute measure of the algorithmic
17090 complexity of the function, it does however provide a good starting point
17091 for which functions you might look at for further optimization.
17095 \layout Subsubsection
17098 \begin_inset LatexCommand \label{sub:MCS51-variants}
17103 \begin_inset LatexCommand \index{MCS51 variants}
17110 MCS51 processors are available from many vendors and come in many different
17112 While they might differ considerably in respect to Special Function Registers
17113 the core MCS51 is usually not modified or is kept compatible.
17115 \layout Subsubsection*
17117 pdata access by SFR
17118 \begin_inset LatexCommand \index{sfr}
17125 With the upcome of devices with internal xdata and flash memory devices
17126 using port P2 as dedicated I/O port is becoming more popular.
17127 Switching the high byte for pdata
17128 \begin_inset LatexCommand \index{pdata}
17132 access which was formerly done by port P2 is then achieved by a Special
17134 In well-established MCS51 tradition the address of this
17138 is where the chip designers decided to put it.
17139 As pdata addressing is used in the startup code for the initialization
17140 of xdata variables a separate startup code should be used as described
17142 \begin_inset LatexCommand \ref{sub:Startup-Code}
17147 \layout Subsubsection*
17149 Other Features available by SFR
17152 Some MCS51 variants offer features like Double DPTR
17153 \begin_inset LatexCommand \index{DPTR}
17157 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
17158 These are currently not used for the MCS51 port.
17159 If you absolutely need them you can fall back to inline assembly or submit
17161 \layout Subsubsection
17163 The Z80 and gbz80 port
17166 SDCC can target both the Zilog
17167 \begin_inset LatexCommand \index{Z80}
17171 and the Nintendo Gameboy's Z80-like gbz80
17172 \begin_inset LatexCommand \index{GameBoy Z80}
17177 The Z80 port is passed through the same
17180 \begin_inset LatexCommand \index{Regression test}
17186 as MCS51 and DS390 ports, so floating point support, support for long variables
17187 and bitfield support is fine.
17188 See mailing lists and forums about interrupt routines and access to I/O
17192 As always, the code is the authoritative reference - see z80/ralloc.c and
17194 The stack frame is similar to that generated by the IAR Z80 compiler.
17195 IX is used as the base pointer, HL is used as a temporary register, and
17196 BC and DE are available for holding variables.
17197 IY is currently unused.
17198 Return values are stored in HL.
17199 One bad side effect of using IX as the base pointer is that a functions
17200 stack frame is limited to 127 bytes - this will be fixed in a later version.
17201 \layout Subsubsection
17206 The port to the Motorola HC08
17207 \begin_inset LatexCommand \index{HC08}
17211 family has been added in October 2003, thank you Erik!
17214 Retargetting for other MCUs.
17217 The issues for retargetting the compiler are far too numerous to be covered
17219 What follows is a brief description of each of the seven phases of the
17220 compiler and its MCU dependency.
17223 Parsing the source and building the annotated parse tree.
17224 This phase is largely MCU independent (except for the language extensions).
17225 Syntax & semantic checks are also done in this phase, along with some initial
17226 optimizations like back patching labels and the pattern matching optimizations
17227 like bit-rotation etc.
17230 The second phase involves generating an intermediate code which can be easy
17231 manipulated during the later phases.
17232 This phase is entirely MCU independent.
17233 The intermediate code generation assumes the target machine has unlimited
17234 number of registers, and designates them with the name iTemp.
17235 The compiler can be made to dump a human readable form of the code generated
17249 This phase does the bulk of the standard optimizations and is also MCU independe
17251 This phase can be broken down into several sub-phases:
17255 Break down intermediate code (iCode) into basic blocks.
17257 Do control flow & data flow analysis on the basic blocks.
17259 Do local common subexpression elimination, then global subexpression elimination
17261 Dead code elimination
17265 If loop optimizations caused any changes then do 'global subexpression eliminati
17266 on' and 'dead code elimination' again.
17269 This phase determines the live-ranges; by live range I mean those iTemp
17270 variables defined by the compiler that still survive after all the optimization
17272 Live range analysis
17273 \begin_inset LatexCommand \index{Live range analysis}
17277 is essential for register allocation, since these computation determines
17278 which of these iTemps will be assigned to registers, and for how long.
17281 Phase five is register allocation.
17282 There are two parts to this process.
17286 The first part I call 'register packing' (for lack of a better term).
17287 In this case several MCU specific expression folding is done to reduce
17292 The second part is more MCU independent and deals with allocating registers
17293 to the remaining live ranges.
17294 A lot of MCU specific code does creep into this phase because of the limited
17295 number of index registers available in the 8051.
17298 The Code generation phase is (unhappily), entirely MCU dependent and very
17299 little (if any at all) of this code can be reused for other MCU.
17300 However the scheme for allocating a homogenized assembler operand for each
17301 iCode operand may be reused.
17304 As mentioned in the optimization section the peep-hole optimizer is rule
17305 based system, which can reprogrammed for other MCUs.
17309 \begin_inset LatexCommand \index{Compiler internals}
17316 The anatomy of the compiler
17317 \begin_inset LatexCommand \label{sub:The-anatomy-of}
17326 This is an excerpt from an article published in Circuit Cellar Magazine
17328 It's a little outdated (the compiler is much more efficient now and user/develo
17329 per friendly), but pretty well exposes the guts of it all.
17335 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
17336 It is fairly easy to retarget for other 8-bit MCU.
17337 Here we take a look at some of the internals of the compiler.
17342 \begin_inset LatexCommand \index{Parsing}
17349 Parsing the input source file and creating an AST (Annotated Syntax Tree
17350 \begin_inset LatexCommand \index{Annotated syntax tree}
17355 This phase also involves propagating types (annotating each node of the
17356 parse tree with type information) and semantic analysis.
17357 There are some MCU specific parsing rules.
17358 For example the storage classes, the extended storage classes are MCU specific
17359 while there may be a xdata storage class for 8051 there is no such storage
17360 class for z80 or Atmel AVR.
17361 SDCC allows MCU specific storage class extensions, i.e.
17362 xdata will be treated as a storage class specifier when parsing 8051 C
17363 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
17368 \begin_inset LatexCommand \index{iCode}
17375 Intermediate code generation.
17376 In this phase the AST is broken down into three-operand form (iCode).
17377 These three operand forms are represented as doubly linked lists.
17378 ICode is the term given to the intermediate form generated by the compiler.
17379 ICode example section shows some examples of iCode generated for some simple
17380 C source functions.
17384 \begin_inset LatexCommand \index{Optimizations}
17391 Bulk of the target independent optimizations is performed in this phase.
17392 The optimizations include constant propagation, common sub-expression eliminati
17393 on, loop invariant code movement, strength reduction of loop induction variables
17394 and dead-code elimination.
17397 Live range analysis
17398 \begin_inset LatexCommand \index{Live range analysis}
17405 During intermediate code generation phase, the compiler assumes the target
17406 machine has infinite number of registers and generates a lot of temporary
17408 The live range computation determines the lifetime of each of these compiler-ge
17409 nerated temporaries.
17410 A picture speaks a thousand words.
17411 ICode example sections show the live range annotations for each of the
17413 It is important to note here, each iCode is assigned a number in the order
17414 of its execution in the function.
17415 The live ranges are computed in terms of these numbers.
17416 The from number is the number of the iCode which first defines the operand
17417 and the to number signifies the iCode which uses this operand last.
17420 Register Allocation
17421 \begin_inset LatexCommand \index{Register allocation}
17428 The register allocation determines the type and number of registers needed
17430 In most MCUs only a few registers can be used for indirect addressing.
17431 In case of 8051 for example the registers R0 & R1 can be used to indirectly
17432 address the internal ram and DPTR to indirectly address the external ram.
17433 The compiler will try to allocate the appropriate register to pointer variables
17435 ICode example section shows the operands annotated with the registers assigned
17437 The compiler will try to keep operands in registers as much as possible;
17438 there are several schemes the compiler uses to do achieve this.
17439 When the compiler runs out of registers the compiler will check to see
17440 if there are any live operands which is not used or defined in the current
17441 basic block being processed, if there are any found then it will push that
17442 operand and use the registers in this block, the operand will then be popped
17443 at the end of the basic block.
17447 There are other MCU specific considerations in this phase.
17448 Some MCUs have an accumulator; very short-lived operands could be assigned
17449 to the accumulator instead of general-purpose register.
17455 Figure II gives a table of iCode operations supported by the compiler.
17456 The code generation involves translating these operations into corresponding
17457 assembly code for the processor.
17458 This sounds overly simple but that is the essence of code generation.
17459 Some of the iCode operations are generated on a MCU specific manner for
17460 example, the z80 port does not use registers to pass parameters so the
17461 SEND and RECV iCode operations will not be generated, and it also does
17462 not support JUMPTABLES.
17469 <Where is Figure II ?>
17473 \begin_inset LatexCommand \index{iCode}
17480 This section shows some details of iCode.
17481 The example C code does not do anything useful; it is used as an example
17482 to illustrate the intermediate code generated by the compiler.
17494 /* This function does nothing useful.
17501 for the purpose of explaining iCode */
17504 short function (data int *x)
17512 short i=10; /* dead initialization eliminated */
17517 short sum=10; /* dead initialization eliminated */
17530 while (*x) *x++ = *p++;
17544 /* compiler detects i,j to be induction variables */
17548 for (i = 0, j = 10 ; i < 10 ; i++, j
17574 mul += i * 3; /* this multiplication remains */
17580 gint += j * 3;/* this multiplication changed to addition */
17594 In addition to the operands each iCode contains information about the filename
17595 and line it corresponds to in the source file.
17596 The first field in the listing should be interpreted as follows:
17601 Filename(linenumber: iCode Execution sequence number : ICode hash table
17602 key : loop depth of the iCode).
17607 Then follows the human readable form of the ICode operation.
17608 Each operand of this triplet form can be of three basic types a) compiler
17609 generated temporary b) user defined variable c) a constant value.
17610 Note that local variables and parameters are replaced by compiler generated
17613 \begin_inset LatexCommand \index{Live range analysis}
17617 are computed only for temporaries (i.e.
17618 live ranges are not computed for global variables).
17620 \begin_inset LatexCommand \index{Register allocation}
17624 are allocated for temporaries only.
17625 Operands are formatted in the following manner:
17630 Operand Name [lr live-from : live-to ] { type information } [ registers
17636 As mentioned earlier the live ranges are computed in terms of the execution
17637 sequence number of the iCodes, for example
17639 the iTemp0 is live from (i.e.
17640 first defined in iCode with execution sequence number 3, and is last used
17641 in the iCode with sequence number 5).
17642 For induction variables such as iTemp21 the live range computation extends
17643 the lifetime from the start to the end of the loop.
17645 The register allocator used the live range information to allocate registers,
17646 the same registers may be used for different temporaries if their live
17647 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
17648 iTemp17 since their live ranges do not overlap.
17649 In addition the allocator also takes into consideration the type and usage
17650 of a temporary, for example itemp6 is a pointer to near space and is used
17651 as to fetch data from (i.e.
17652 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
17653 Some short lived temporaries are allocated to special registers which have
17654 meaning to the code generator e.g.
17655 iTemp13 is allocated to a pseudo register CC which tells the back end that
17656 the temporary is used only for a conditional jump the code generation makes
17657 use of this information to optimize a compare and jump ICode.
17659 There are several loop optimizations
17660 \begin_inset LatexCommand \index{Loop optimization}
17664 performed by the compiler.
17665 It can detect induction variables iTemp21(i) and iTemp23(j).
17666 Also note the compiler does selective strength reduction
17667 \begin_inset LatexCommand \index{Strength reduction}
17672 the multiplication of an induction variable in line 18 (gint = j * 3) is
17673 changed to addition, a new temporary iTemp17 is allocated and assigned
17674 a initial value, a constant 3 is then added for each iteration of the loop.
17675 The compiler does not change the multiplication
17676 \begin_inset LatexCommand \index{Multiplication}
17680 in line 17 however since the processor does support an 8 * 8 bit multiplication.
17682 Note the dead code elimination
17683 \begin_inset LatexCommand \index{Dead-code elimination}
17687 optimization eliminated the dead assignments in line 7 & 8 to I and sum
17695 Sample.c (5:1:0:0) _entry($9) :
17700 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
17705 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
17710 Sample.c(11:4:53:0) preHeaderLbl0($11) :
17715 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
17721 Sample.c(11:6:5:1) _whilecontinue_0($1) :
17726 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
17732 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
17737 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
17743 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
17749 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
17755 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
17761 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
17762 * int}[r0] + 0x2 {short}
17767 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
17772 Sample.c(11:17:21:0)_whilebreak_0($3) :
17777 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
17782 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
17787 Sample.c(15:20:54:0)preHeaderLbl1($13) :
17792 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
17797 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
17802 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
17807 Sample.c(15:24:26:1)_forcond_0($4) :
17812 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
17818 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
17823 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
17824 + ITemp21 [lr21:38]{short}[r4]
17829 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
17835 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
17836 + iTemp15 [lr29:30]{short}[r1]
17841 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
17847 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
17853 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
17859 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
17865 Sample.c(19:38:47:1) goto _forcond_0($4)
17870 Sample.c(19:39:48:0)_forbreak_0($7) :
17875 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
17876 + ITemp11 [lr19:40]{short}[r3]
17881 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
17886 Sample.c(20:42:51:0)_return($8) :
17891 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
17897 Finally the code generated for this function:
17938 ; ----------------------------------------------
17943 ; function function
17948 ; ----------------------------------------------
17958 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
17970 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
17982 ;_whilecontinue_0($1) :
17992 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
17997 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
18056 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
18075 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
18122 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
18162 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
18188 ; iTemp6 [lr5:16]{_near * int}[r0] =
18193 ; iTemp6 [lr5:16]{_near * int}[r0] +
18210 ; goto _whilecontinue_0($1)
18222 ; _whilebreak_0($3) :
18232 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
18244 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
18256 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
18268 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
18287 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
18316 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
18321 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
18366 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
18371 ; iTemp21 [lr21:38]{short}[r4]
18397 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
18430 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
18435 ; iTemp15 [lr29:30]{short}[r1]
18454 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
18501 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
18548 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
18560 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
18574 cjne r5,#0xff,00104$
18586 ; goto _forcond_0($4)
18598 ; _forbreak_0($7) :
18608 ; ret iTemp24 [lr40:41]{short}
18651 A few words about basic block successors, predecessors and dominators
18654 Successors are basic blocks
18655 \begin_inset LatexCommand \index{Basic blocks}
18659 that might execute after this basic block.
18661 Predecessors are basic blocks that might execute before reaching this basic
18664 Dominators are basic blocks that WILL execute before reaching this basic
18698 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
18701 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
18704 c) domVect of [BB4] = BB1 ...
18705 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
18713 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
18723 Thanks to all the other volunteer developers who have helped with coding,
18724 testing, web-page creation, distribution sets, etc.
18725 You know who you are :-)
18732 This document was initially written by Sandeep Dutta
18735 All product names mentioned herein may be trademarks
18736 \begin_inset LatexCommand \index{Trademarks}
18740 of their respective companies.
18747 To avoid confusion, the installation and building options for SDCC itself
18748 (chapter 2) are not part of the index.
18752 \begin_inset LatexCommand \printindex{}