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), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
120 It can be retargetted for other microprocessors, support for Microchip
121 PIC, Atmel AVR is under development.
122 The entire source code for the compiler is distributed under GPL.
123 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
124 SDCC has extensive language extensions suitable for utilizing various microcont
125 rollers and underlying hardware effectively.
130 In addition to the MCU specific optimizations SDCC also does a host of standard
134 global sub expression elimination,
137 loop optimizations (loop invariant, strength reduction of induction variables
141 constant folding & propagation,
147 dead code elimination
157 For the back-end SDCC uses a global register allocation scheme which should
158 be well suited for other 8 bit MCUs.
163 The peep hole optimizer uses a rule based substitution mechanism which is
169 Supported data-types are:
172 char (8 bits, 1 byte),
175 short and int (16 bits, 2 bytes),
178 long (32 bit, 4 bytes)
185 The compiler also allows
187 inline assembler code
189 to be embedded anywhere in a function.
190 In addition, routines developed in assembly can also be called.
194 SDCC also provides an option (-
204 -cyclomatic) to report the relative complexity of a function.
205 These functions can then be further optimized, or hand coded in assembly
211 SDCC also comes with a companion source level debugger SDCDB, the debugger
212 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
217 The latest version can be downloaded from
218 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
228 Please note: the compiler will probably always be some steps ahead of this
233 \begin_inset LatexCommand \index{Status of documentation}
243 Obviously this has pros and cons
252 All packages used in this compiler system are
260 ; source code for all the sub-packages (pre-processor, assemblers, linkers
261 etc) is distributed with the package.
262 This documentation is maintained using a freeware word processor (LyX).
264 This program is free software; you can redistribute it and/or modify it
265 under the terms of the GNU General Public License
266 \begin_inset LatexCommand \index{GNU General Public License, GPL}
270 as published by the Free Software Foundation; either version 2, or (at
271 your option) any later version.
272 This program is distributed in the hope that it will be useful, but WITHOUT
273 ANY WARRANTY; without even the implied warranty
274 \begin_inset LatexCommand \index{warranty}
278 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
279 See the GNU General Public License for more details.
280 You should have received a copy of the GNU General Public License along
281 with this program; if not, write to the Free Software Foundation, 59 Temple
282 Place - Suite 330, Boston, MA 02111-1307, USA.
283 In other words, you are welcome to use, share and improve this program.
284 You are forbidden to forbid anyone else to use, share and improve what
286 Help stamp out software-hoarding!
289 Typographic conventions
290 \begin_inset LatexCommand \index{Typographic conventions}
297 Throughout this manual, we will use the following convention.
298 Commands you have to type in are printed in
306 Code samples are printed in
311 Interesting items and new terms are printed in
316 Compatibility with previous versions
319 This version has numerous bug fixes compared with the previous version.
320 But we also introduced some incompatibilities with older versions.
321 Not just for the fun of it, but to make the compiler more stable, efficient
323 \begin_inset LatexCommand \index{ANSI-compliance}
328 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
332 for ANSI-Compliance).
338 short is now equivalent to int (16 bits), it used to be equivalent to char
339 (8 bits) which is not ANSI compliant
342 the default directory for gcc-builds where include, library and documentation
343 files are stored is now in /usr/local/share
346 char type parameters to vararg functions are casted to int unless explicitly
363 will push a as an int and as a char resp.
376 -regextend has been removed
389 -noregparms has been removed
402 -stack-after-data has been removed
407 <pending: more incompatibilities?>
413 What do you need before you start installation of SDCC? A computer, and
415 The preferred method of installation is to compile SDCC from source using
417 For Windows some pre-compiled binary distributions are available for your
419 You should have some experience with command line tools and compiler use.
425 The SDCC home page at
426 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
430 is a great place to find distribution sets.
431 You can also find links to the user mailing lists that offer help or discuss
432 SDCC with other SDCC users.
433 Web links to other SDCC related sites can also be found here.
434 This document can be found in the DOC directory of the source package as
436 Some of the other tools (simulator and assembler) included with SDCC contain
437 their own documentation and can be found in the source distribution.
438 If you want the latest unreleased software, the complete source package
439 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
442 Wishes for the future
445 There are (and always will be) some things that could be done.
446 Here are some I can think of:
453 char KernelFunction3(char p) at 0x340;
461 \begin_inset LatexCommand \index{code banking (not supported)}
471 If you can think of some more, please see the chapter
472 \begin_inset LatexCommand \ref{sub:Requesting-Features}
476 about filing feature requests
477 \begin_inset LatexCommand \index{Requesting features}
482 \begin_inset LatexCommand \index{Feature request}
492 \begin_inset LatexCommand \index{Installation}
499 For most users it is sufficient to skip to either section
500 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
505 \begin_inset LatexCommand \ref{sub:Windows-Install}
510 More detailled instructions follow below.
514 \begin_inset LatexCommand \index{Options SDCC configuration}
521 The install paths, search paths and other options are defined when running
523 The defaults can be overridden by:
525 \labelwidthstring 00.00.0000
537 -prefix see table below
539 \labelwidthstring 00.00.0000
551 -exec_prefix see table below
553 \labelwidthstring 00.00.0000
565 -bindir see table below
567 \labelwidthstring 00.00.0000
579 -datadir see table below
581 \labelwidthstring 00.00.0000
583 docdir environment variable, see table below
585 \labelwidthstring 00.00.0000
587 include_dir_suffix environment variable, see table below
589 \labelwidthstring 00.00.0000
591 lib_dir_suffix environment variable, see table below
593 \labelwidthstring 00.00.0000
595 sdccconf_h_dir_separator environment variable, either / or
600 This character will only be used in sdccconf.h; don't forget it's a C-header,
601 therefore a double-backslash is needed there.
603 \labelwidthstring 00.00.0000
615 -disable-mcs51-port Excludes the Intel mcs51 port
617 \labelwidthstring 00.00.0000
629 -disable-gbz80-port Excludes the Gameboy gbz80 port
631 \labelwidthstring 00.00.0000
643 -z80-port Excludes the z80 port
645 \labelwidthstring 00.00.0000
657 -disable-avr-port Excludes the AVR port
659 \labelwidthstring 00.00.0000
671 -disable-ds390-port Excludes the DS390 port
673 \labelwidthstring 00.00.0000
685 -disable-pic-port Excludes the PIC port
687 \labelwidthstring 00.00.0000
699 -disable-xa51-port Excludes the XA51 port
701 \labelwidthstring 00.00.0000
713 -disable-ucsim Disables configuring and building of ucsim
715 \labelwidthstring 00.00.0000
727 -disable-device-lib-build Disables automatically building device libraries
729 \labelwidthstring 00.00.0000
741 -disable-packihx Disables building packihx
743 \labelwidthstring 00.00.0000
755 -enable-libgc Use the Bohem memory allocator.
756 Lower runtime footprint.
759 Furthermore the environment variables CC, CFLAGS, ...
760 the tools and their arguments can be influenced.
761 Please see `configure -
771 -help` and the man/info pages of `configure` for details.
775 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
776 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
777 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
778 At the moment it's not possible to change the default settings (it was
779 simply never required.
783 These configure options are compiled into the binaries, and can only be
784 changed by rerunning 'configure' and recompiling SDCC.
785 The configure options are written in
789 to distinguish them from run time environment variables (see section search
795 \begin_inset Quotes sld
799 \begin_inset Quotes srd
802 are used by the SDCC team to build the official Win32 binaries.
803 The SDCC team uses Mingw32 to build the official Windows binaries, because
810 a gcc compiler and last but not least
813 the binaries can be built by cross compiling on Sourceforge's compile farm.
816 See the examples, how to pass the Win32 settings to 'configure'.
817 The other Win32 builds using Borland, VC or whatever don't use 'configure',
818 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
830 <lyxtabular version="3" rows="8" columns="3">
832 <column alignment="block" valignment="top" leftline="true" width="0in">
833 <column alignment="block" valignment="top" leftline="true" width="0in">
834 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
835 <row topline="true" bottomline="true">
836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
852 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
862 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
902 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
946 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
962 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1033 <row topline="true">
1034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1061 <row topline="true" bottomline="true">
1062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1080 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1098 'configure' also computes relative paths.
1099 This is needed for full relocatability of a binary package and to complete
1100 search paths (see section search paths below):
1106 \begin_inset Tabular
1107 <lyxtabular version="3" rows="4" columns="3">
1109 <column alignment="block" valignment="top" leftline="true" width="0in">
1110 <column alignment="block" valignment="top" leftline="true" width="0in">
1111 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1112 <row topline="true" bottomline="true">
1113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1138 <row topline="true" bottomline="true">
1139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1157 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1166 <row bottomline="true">
1167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1194 <row bottomline="true">
1195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1205 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1213 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1246 \begin_inset Quotes srd
1250 \begin_inset Quotes srd
1264 \begin_inset Quotes srd
1268 \begin_inset Quotes srd
1296 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1305 \begin_inset Quotes srd
1308 i586-mingw32msvc-gcc
1309 \begin_inset Quotes srd
1313 \begin_inset Quotes srd
1316 i586-mingw32msvc-g++
1317 \begin_inset Quotes srd
1325 \begin_inset Quotes srd
1328 i586-mingw32msvc-ranlib
1329 \begin_inset Quotes srd
1337 \begin_inset Quotes srd
1340 i586-mingw32msvc-strip
1341 \begin_inset Quotes srd
1359 \begin_inset Quotes srd
1363 \begin_inset Quotes srd
1381 \begin_inset Quotes srd
1385 \begin_inset Quotes srd
1393 \begin_inset Quotes srd
1397 \begin_inset Quotes srd
1405 \begin_inset Quotes srd
1409 \begin_inset Quotes srd
1417 \begin_inset Quotes srd
1421 \begin_inset Quotes srd
1428 sdccconf_h_dir_separator=
1429 \begin_inset Quotes srd
1441 \begin_inset Quotes srd
1458 -disable-device-lib-build
1486 -host=i586-mingw32msvc -
1496 -build=unknown-unknown-linux-gnu
1500 \begin_inset Quotes sld
1504 \begin_inset Quotes srd
1507 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1516 \begin_inset Quotes srd
1520 \begin_inset Quotes srd
1528 \begin_inset Quotes srd
1532 \begin_inset Quotes srd
1550 \begin_inset Quotes srd
1554 \begin_inset Quotes srd
1572 \begin_inset Quotes srd
1576 \begin_inset Quotes srd
1584 \begin_inset Quotes srd
1588 \begin_inset Quotes srd
1596 \begin_inset Quotes srd
1600 \begin_inset Quotes srd
1608 \begin_inset Quotes srd
1612 \begin_inset Quotes srd
1619 sdccconf_h_dir_separator=
1620 \begin_inset Quotes srd
1632 \begin_inset Quotes srd
1652 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1663 -C' turns on caching, which gives a little bit extra speed.
1664 However if options are changed, it can be necessary to delete the config.cache
1669 \begin_inset LatexCommand \label{sub:Install-paths}
1674 \begin_inset LatexCommand \index{Install paths}
1680 \added_space_top medskip \align center
1682 \begin_inset Tabular
1683 <lyxtabular version="3" rows="5" columns="4">
1685 <column alignment="center" valignment="top" leftline="true" width="0">
1686 <column alignment="center" valignment="top" leftline="true" width="0">
1687 <column alignment="center" valignment="top" leftline="true" width="0">
1688 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1689 <row topline="true" bottomline="true">
1690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1731 <row topline="true">
1732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1771 <row topline="true">
1772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1787 $DATADIR/ $INCLUDE_DIR_SUFFIX
1790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1795 /usr/local/share/sdcc/include
1798 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1811 <row topline="true">
1812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1827 $DATADIR/$LIB_DIR_SUFFIX
1830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1835 /usr/local/share/sdcc/lib
1838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1851 <row topline="true" bottomline="true">
1852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1875 /usr/local/share/sdcc/doc
1878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1900 *compiler, preprocessor, assembler, and linker
1906 is auto-appended by the compiler, e.g.
1907 small, large, z80, ds390 etc
1910 The install paths can still be changed during `make install` with e.g.:
1913 make install prefix=$(HOME)/local/sdcc
1916 Of course this doesn't change the search paths compiled into the binaries.
1920 \begin_inset LatexCommand \label{sub:Search-Paths}
1925 \begin_inset LatexCommand \index{Search path}
1932 Some search paths or parts of them are determined by configure variables
1937 , see section above).
1938 Further search paths are determined by environment variables during runtime.
1941 The paths searched when running the compiler are as follows (the first catch
1947 Binary files (preprocessor, assembler and linker)
1953 \begin_inset Tabular
1954 <lyxtabular version="3" rows="4" columns="3">
1956 <column alignment="block" valignment="top" leftline="true" width="0in">
1957 <column alignment="block" valignment="top" leftline="true" width="0in">
1958 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1959 <row topline="true" bottomline="true">
1960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1985 <row topline="true">
1986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2015 <row topline="true">
2016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2021 Path of argv[0] (if available)
2024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2041 <row topline="true" bottomline="true">
2042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2050 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2058 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2083 \begin_inset Tabular
2084 <lyxtabular version="3" rows="6" columns="3">
2086 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2087 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2088 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2089 <row topline="true" bottomline="true">
2090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2115 <row topline="true">
2116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2152 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2171 <row topline="true">
2172 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2188 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2197 <row topline="true">
2198 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2212 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2235 <row topline="true">
2236 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2304 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2317 <row topline="true" bottomline="true">
2318 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2334 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2339 /usr/local/share/sdcc/
2344 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2372 -nostdinc disables the last two search paths.
2382 With the exception of
2383 \begin_inset Quotes sld
2397 \begin_inset Quotes srd
2404 is auto-appended by the compiler (e.g.
2405 small, large, z80, ds390 etc.).
2412 \begin_inset Tabular
2413 <lyxtabular version="3" rows="6" columns="3">
2415 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2416 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2417 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2418 <row topline="true" bottomline="true">
2419 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2427 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2435 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2444 <row topline="true">
2445 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2500 <row topline="true">
2501 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2513 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2525 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2540 <row topline="true">
2541 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2552 $LIB_DIR_SUFFIX/<model>
2555 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2569 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2586 <row topline="true">
2587 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2602 $LIB_DIR_SUFFIX/<model>
2605 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2658 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2714 <row topline="true" bottomline="true">
2715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2724 $LIB_DIR_SUFFIX/<model>
2727 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2732 /usr/local/share/sdcc/
2739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2757 Don't delete any of the stray spaces in the table above without checking
2758 the HTML output (last line)!
2774 -nostdlib disables the last two search paths.
2778 \begin_inset LatexCommand \index{Building SDCC}
2783 \layout Subsubsection
2785 Building SDCC on Linux
2786 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2795 Download the source package
2797 either from the SDCC CVS repository or from the
2798 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2804 , it will be named something like sdcc
2817 Bring up a command line terminal, such as xterm.
2822 Unpack the file using a command like:
2825 "tar -xvzf sdcc.src.tar.gz
2830 , this will create a sub-directory called sdcc with all of the sources.
2833 Change directory into the main SDCC directory, for example type:
2850 This configures the package for compilation on your system.
2866 All of the source packages will compile, this can take a while.
2882 This copies the binary executables, the include files, the libraries and
2883 the documentation to the install directories.
2884 \layout Subsubsection
2886 Building SDCC on OSX 2.x
2889 Follow the instruction for Linux.
2893 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2894 )) fails to compile SDCC.
2895 Fortunately there's also gcc 2.9.x installed, which works fine.
2896 This compiler can be selected by running 'configure' with:
2899 ./configure CC=gcc2 CXX=g++2
2900 \layout Subsubsection
2902 Cross compiling SDCC on Linux for Windows
2905 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2906 See section 'Configure Options'.
2907 \layout Subsubsection
2909 Building SDCC on Windows
2912 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2914 They use Unix-sockets, which are not available on Win32.
2915 \layout Subsubsection
2917 Building SDCC using Cygwin and Mingw32
2920 For building and installing a Cygwin executable follow the instructions
2926 \begin_inset Quotes sld
2930 \begin_inset Quotes srd
2933 Win32-binary can be built, which will not need the Cygwin-DLL.
2934 For the necessary 'configure' options see section 'configure options' or
2935 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
2939 In order to install Cygwin on Windows download setup.exe from
2940 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2946 \begin_inset Quotes sld
2949 default text file type
2950 \begin_inset Quotes srd
2954 \begin_inset Quotes sld
2958 \begin_inset Quotes srd
2961 and download/install at least the following packages.
2962 Some packages are selected by default, others will be automatically selected
2963 because of dependencies with the manually selected packages.
2964 Never deselect these packages!
2973 gcc ; version 3.x is fine, no need to use the old 2.9x
2976 binutils ; selected with gcc
2982 rxvt ; a nice console, which makes life much easier under windoze (see below)
2985 man ; not really needed for building SDCC, but you'll miss it sooner or
2989 less ; not really needed for building SDCC, but you'll miss it sooner or
2993 cvs ; only if you use CVS access
2996 If you want to develop something you'll need:
2999 python ; for the regression tests
3002 gdb ; the gnu debugger, together with the nice GUI
3003 \begin_inset Quotes sld
3007 \begin_inset Quotes srd
3013 openssh ; to access the CF or commit changes
3016 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3017 use autoconf-stable!
3020 rxvt is a nice console with history.
3021 Replace in your cygwin.bat the line
3040 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3043 -bg black -fg white -geometry 100x65 -e bash -
3056 Text selected with the mouse is automatically copied to the clipboard, pasting
3057 works with shift-insert.
3061 The other good tip is to make sure you have no //c/-style paths anywhere,
3062 use /cygdrive/c/ instead.
3063 Using // invokes a network lookup which is very slow.
3065 \begin_inset Quotes sld
3069 \begin_inset Quotes srd
3072 is too long, you can change it with e.g.
3078 SDCC sources use the unix line ending LF.
3079 Life is much easier, if you store the source tree on a drive which is mounted
3081 And use an editor which can handle LF-only line endings.
3082 Make sure not to commit files with windows line endings.
3083 The tabulator spacing used in the project is 8.
3084 \layout Subsubsection
3086 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3091 Download the source package
3093 either from the SDCC CVS repository or from the
3094 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3100 , it will be named something like sdcc
3107 SDCC is distributed with all the projects, workspaces, and files you need
3108 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3109 doesn't build under MSVC).
3110 The workspace name is 'sdcc.dsw'.
3111 Please note that as it is now, all the executables are created in a folder
3115 Once built you need to copy the executables from sdcc
3119 bin before running SDCC.
3124 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3125 flex.exe, and gawk.exe.
3126 One good place to get them is
3127 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3135 Download the file UnxUtils
3136 \begin_inset LatexCommand \index{UnxUtils}
3141 Now you have to install the utilities and setup MSVC so it can locate the
3143 Here there are two alternatives (choose one!):
3150 a) Extract UnxUtils.zip to your C:
3152 hard disk PRESERVING the original paths, otherwise bison won't work.
3153 (If you are using WinZip make certain that 'Use folder names' is selected)
3157 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3158 in 'Show directories for:' select 'Executable files', and in the directories
3159 window add a new path: 'C:
3169 (As a side effect, you get a bunch of Unix utilities that could be useful,
3170 such as diff and patch.)
3177 This one avoids extracting a bunch of files you may not use, but requires
3182 a) Create a directory were to put the tools needed, or use a directory already
3190 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3191 to such directory WITHOUT preserving the original paths.
3192 (If you are using WinZip make certain that 'Use folder names' is not selected)
3196 c) Rename bison.exe to '_bison.exe'.
3200 d) Create a batch file 'bison.bat' in 'C:
3204 ' and add these lines:
3224 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3228 Steps 'c' and 'd' are needed because bison requires by default that the
3229 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3230 '/usr/local/share/' I think.
3231 So it is necessary to tell bison where those files are located if they
3232 are not in such directory.
3233 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3237 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3238 in 'Show directories for:' select 'Executable files', and in the directories
3239 window add a new path: 'c:
3242 Note that you can use any other path instead of 'c:
3244 util', even the path where the Visual C++ tools are, probably: 'C:
3248 Microsoft Visual Studio
3253 So you don't have to execute step 'e' :)
3257 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3258 the executables from sdcc
3262 bin, and you can compile using SDCC.
3263 \layout Subsubsection
3265 Building SDCC Using Borland
3268 From the sdcc directory, run the command "make -f Makefile.bcc".
3269 This should regenerate all the .exe files in the bin directory except for
3270 sdcdb.exe (which currently doesn't build under Borland C++).
3273 If you modify any source files and need to rebuild, be aware that the dependenci
3274 es may not be correctly calculated.
3275 The safest option is to delete all .obj files and run the build again.
3276 From a Cygwin BASH prompt, this can easily be done with the command (be
3277 sure you are in the sdcc directory):
3287 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3289 ) -print -exec rm {}
3298 or on Windows NT/2000/XP from the command prompt with the command:
3305 del /s *.obj *.lib *.rul
3308 from the sdcc directory.
3309 \layout Subsubsection
3311 Windows Install Using a Binary Package
3312 \begin_inset LatexCommand \label{sub:Windows-Install}
3319 Download the binary package from
3320 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3324 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3325 This should unpack to a group of sub-directories.
3326 An example directory structure after unpacking the mingw32 package is:
3331 bin for the executables, c:
3339 lib for the include and libraries.
3342 Adjust your environment variable PATH to include the location of the bin
3343 directory or start sdcc using the full path.
3346 Building the Documentation
3349 If the necessary tools are installed it is as easy as changing into the
3350 doc directory and typing
3354 \begin_inset Quotes srd
3358 \begin_inset Quotes srd
3365 If you want to avoid installing the tools you will have some success with
3366 a bootable Knoppix CD
3367 \begin_inset LatexCommand \url{http://www.knopper.net}
3372 Prebuilt documentation in html and pdf format is available from
3373 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3380 Reading the Documentation
3383 Currently reading the document in pdf format is recommended, as for unknown
3384 reason the hyperlinks are working there whereas in the html version they
3387 This documentation is in some aspects different from a commercial documentation:
3391 It tries to document SDCC for several processor architectures in one document
3392 (commercially these probably would be separate documents/products).
3393 This document currently matches SDCC for mcs51 and DS390 best and does
3394 give too few information about f.e.
3398 There are many references pointing away from this documentation.
3399 Don't let this distract you.
3401 was a reference like
3402 \begin_inset LatexCommand \url{www.opencores.org}
3406 together with a statement
3407 \begin_inset Quotes sld
3410 some processors which are targetted by SDCC can be implemented in a field
3412 \begin_inset LatexCommand \index{fpga (field programmable array)}
3417 \begin_inset Quotes srd
3420 we expect you to have a quick look there and come back.
3421 If you read this you are on the right track.
3424 Some sections attribute more space to problems, restrictions and warnings
3425 than to the solution.
3428 The installation section and the section about the debugger is intimidating.
3431 There are still lots of typos and there are more different writing styles
3435 Testing the SDCC Compiler
3438 The first thing you should do after installing your SDCC compiler is to
3454 \begin_inset LatexCommand \index{version}
3461 at the prompt, and the program should run and tell you the version.
3462 If it doesn't run, or gives a message about not finding sdcc program, then
3463 you need to check over your installation.
3464 Make sure that the sdcc bin directory is in your executable search path
3465 defined by the PATH environment setting (
3470 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3477 Install trouble-shooting for suggestions
3480 Make sure that the sdcc program is in the bin folder, if not perhaps something
3481 did not install correctly.
3489 is commonly installed as described in section
3490 \begin_inset Quotes sld
3493 Install and search paths
3494 \begin_inset Quotes srd
3503 Make sure the compiler works on a very simple example.
3504 Type in the following test.c program using your favorite
3530 Compile this using the following command:
3539 If all goes well, the compiler will generate a test.asm and test.rel file.
3540 Congratulations, you've just compiled your first program with SDCC.
3541 We used the -c option to tell SDCC not to link the generated code, just
3542 to keep things simple for this step.
3550 The next step is to try it with the linker.
3560 If all goes well the compiler will link with the libraries and produce
3561 a test.ihx output file.
3566 (no test.ihx, and the linker generates warnings), then the problem is most
3575 usr/local/share/sdcc/lib directory
3582 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3589 Install trouble-shooting for suggestions).
3597 The final test is to ensure
3605 header files and libraries.
3606 Edit test.c and change it to the following:
3623 strcpy(str1, "testing");
3630 Compile this by typing
3637 This should generate a test.ihx output file, and it should give no warnings
3638 such as not finding the string.h file.
3639 If it cannot find the string.h file, then the problem is that
3643 cannot find the /usr/local/share/sdcc/include directory
3650 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3657 Install trouble-shooting section for suggestions).
3675 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3679 to find exactly where SDCC is looking for the include and lib files.
3682 Install Trouble-shooting
3683 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3688 \begin_inset LatexCommand \index{Install trouble-shooting}
3693 \layout Subsubsection
3695 SDCC does not build correctly.
3698 A thing to try is starting from scratch by unpacking the .tgz source package
3699 again in an empty directory.
3707 ./configure 2>&1 | tee configure.log
3721 make 2>&1 | tee make.log
3728 If anything goes wrong, you can review the log files to locate the problem.
3729 Or a relevant part of this can be attached to an email that could be helpful
3730 when requesting help from the mailing list.
3731 \layout Subsubsection
3734 \begin_inset Quotes sld
3738 \begin_inset Quotes srd
3745 \begin_inset Quotes sld
3749 \begin_inset Quotes srd
3752 command is a script that analyzes your system and performs some configuration
3753 to ensure the source package compiles on your system.
3754 It will take a few minutes to run, and will compile a few tests to determine
3755 what compiler features are installed.
3756 \layout Subsubsection
3759 \begin_inset Quotes sld
3763 \begin_inset Quotes srd
3769 This runs the GNU make tool, which automatically compiles all the source
3770 packages into the final installed binary executables.
3771 \layout Subsubsection
3774 \begin_inset Quotes sld
3778 \begin_inset Quotes erd
3784 This will install the compiler, other executables libraries and include
3785 files into the appropriate directories.
3787 \begin_inset LatexCommand \ref{sub:Install-paths}
3793 \begin_inset LatexCommand \ref{sub:Search-Paths}
3798 about install and search paths.
3800 On most systems you will need super-user privileges to do this.
3806 SDCC is not just a compiler, but a collection of tools by various developers.
3807 These include linkers, assemblers, simulators and other components.
3808 Here is a summary of some of the components.
3809 Note that the included simulator and assembler have separate documentation
3810 which you can find in the source package in their respective directories.
3811 As SDCC grows to include support for other processors, other packages from
3812 various developers are included and may have their own sets of documentation.
3816 You might want to look at the files which are installed in <installdir>.
3817 At the time of this writing, we find the following programs for gcc-builds:
3821 In <installdir>/bin:
3824 sdcc - The compiler.
3827 sdcpp - The C preprocessor.
3830 asx8051 - The assembler for 8051 type processors.
3837 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3840 aslink -The linker for 8051 type processors.
3847 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3850 s51 - The ucSim 8051 simulator.
3853 sdcdb - The source debugger.
3856 packihx - A tool to pack (compress) Intel hex files.
3859 In <installdir>/share/sdcc/include
3865 In <installdir>/share/sdcc/lib
3868 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3872 In <installdir>/share/sdcc/doc
3878 As development for other processors proceeds, this list will expand to include
3879 executables to support processors like AVR, PIC, etc.
3880 \layout Subsubsection
3885 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3886 the assembler and linkage editor.
3887 \layout Subsubsection
3890 \begin_inset LatexCommand \index{sdcpp}
3894 - The C-Preprocessor
3897 The preprocessor is a modified version of the GNU preprocessor.
3898 The C preprocessor is used to pull in #include sources, process #ifdef
3899 statements, #defines and so on.
3900 \layout Subsubsection
3902 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3906 This is retargettable assembler & linkage editor, it was developed by Alan
3908 John Hartman created the version for 8051, and I (Sandeep) have made some
3909 enhancements and bug fixes for it to work properly with SDCC.
3910 \layout Subsubsection
3913 \begin_inset LatexCommand \index{s51}
3920 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3921 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3926 The simulator is built as part of the build process.
3927 For more information visit Daniel's web site at:
3928 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3933 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
3935 \layout Subsubsection
3938 \begin_inset LatexCommand \index{sdcdb}
3942 - Source Level Debugger
3945 Sdcdb is the companion source level debugger.
3946 The current version of the debugger uses Daniel's Simulator S51
3947 \begin_inset LatexCommand \index{s51}
3951 , but can be easily changed to use other simulators.
3958 \layout Subsubsection
3960 Single Source File Projects
3963 For single source file 8051 projects the process is very simple.
3964 Compile your programs with the following command
3967 "sdcc sourcefile.c".
3971 This will compile, assemble and link your source file.
3972 Output files are as follows
3976 \begin_inset LatexCommand \index{.asm}
3981 \begin_inset LatexCommand \index{Assembler source}
3985 file created by the compiler
3989 \begin_inset LatexCommand \index{.lst}
3994 \begin_inset LatexCommand \index{Assembler listing}
3998 file created by the Assembler
4002 \begin_inset LatexCommand \index{.rst}
4007 \begin_inset LatexCommand \index{Assembler listing}
4011 file updated with linkedit information, created by linkage editor
4015 \begin_inset LatexCommand \index{.sym}
4020 \begin_inset LatexCommand \index{Symbol listing}
4024 for the sourcefile, created by the assembler
4028 \begin_inset LatexCommand \index{.rel}
4033 \begin_inset LatexCommand \index{Object file}
4037 created by the assembler, input to Linkage editor
4041 \begin_inset LatexCommand \index{.map}
4046 \begin_inset LatexCommand \index{Memory map}
4050 for the load module, created by the Linker
4054 \begin_inset LatexCommand \index{.mem}
4058 - A file with a summary of the memory usage
4062 \begin_inset LatexCommand \index{.ihx}
4066 - The load module in Intel hex format
4067 \begin_inset LatexCommand \index{Intel hex format}
4071 (you can select the Motorola S19 format
4072 \begin_inset LatexCommand \index{Motorola S19 format}
4087 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4092 If you need another format you might want to use
4099 \begin_inset LatexCommand \index{objdump}
4110 \begin_inset LatexCommand \index{srecord}
4118 \begin_inset LatexCommand \index{.adb}
4122 - An intermediate file containing debug information needed to create the
4134 \begin_inset LatexCommand \index{-\/-debug}
4142 \begin_inset LatexCommand \index{.cdb}
4146 - An optional file (with -
4156 -debug) containing debug information
4161 \begin_inset LatexCommand \index{. (no extension)}
4166 \begin_inset LatexCommand \index{AOMF51}
4170 file containing debug information (with -
4181 This format is commonly used by third party tools (debuggers
4182 \begin_inset LatexCommand \index{Debugger}
4186 , simulators, emulators)
4190 \begin_inset LatexCommand \index{.dump*}
4194 - Dump file to debug the compiler it self (with -
4204 -dumpall) (see section
4205 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4211 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4217 \begin_inset Quotes sld
4220 Anatomy of the compiler
4221 \begin_inset Quotes srd
4225 \layout Subsubsection
4227 Projects with Multiple Source Files
4230 SDCC can compile only ONE file at a time.
4231 Let us for example assume that you have a project containing the following
4236 foo1.c (contains some functions)
4238 foo2.c (contains some more functions)
4240 foomain.c (contains more functions and the function main)
4248 The first two files will need to be compiled separately with the commands:
4280 Then compile the source file containing the
4285 \begin_inset LatexCommand \index{Linker}
4289 the files together with the following command:
4297 foomain.c\SpecialChar ~
4298 foo1.rel\SpecialChar ~
4303 \begin_inset LatexCommand \index{.rel}
4315 can be separately compiled as well:
4326 sdcc foomain.rel foo1.rel foo2.rel
4333 The file containing the
4348 file specified in the command line, since the linkage editor processes
4349 file in the order they are presented to it.
4350 The linker is invoked from SDCC using a script file with extension .lnk
4351 \begin_inset LatexCommand \index{.lnk}
4356 You can view this file to troubleshoot linking problems such as those arising
4357 from missing libraries.
4358 \layout Subsubsection
4360 Projects with Additional Libraries
4361 \begin_inset LatexCommand \index{Libraries}
4368 Some reusable routines may be compiled into a library, see the documentation
4369 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4373 \begin_inset LatexCommand \index{.lib}
4380 Libraries created in this manner can be included in the command line.
4381 Make sure you include the -L <library-path> option to tell the linker where
4382 to look for these files if they are not in the current directory.
4383 Here is an example, assuming you have the source file
4395 (if that is not the same as your current project):
4402 sdcc foomain.c foolib.lib -L mylib
4413 must be an absolute path name.
4417 The most efficient way to use libraries is to keep separate modules in separate
4419 The lib file now should name all the modules.rel
4420 \begin_inset LatexCommand \index{rel}
4425 For an example see the standard library file
4429 in the directory <installdir>/share/lib/small.
4432 Command Line Options
4433 \begin_inset LatexCommand \index{Command Line Options}
4438 \layout Subsubsection
4440 Processor Selection Options
4441 \begin_inset LatexCommand \index{Options processor selection}
4446 \begin_inset LatexCommand \index{Processor selection options}
4452 \labelwidthstring 00.00.0000
4457 \begin_inset LatexCommand \index{-mmcs51}
4463 Generate code for the Intel MCS51
4464 \begin_inset LatexCommand \index{MCS51}
4468 family of processors.
4469 This is the default processor target.
4471 \labelwidthstring 00.00.0000
4476 \begin_inset LatexCommand \index{-mds390}
4482 Generate code for the Dallas DS80C390
4483 \begin_inset LatexCommand \index{DS80C390}
4489 \labelwidthstring 00.00.0000
4494 \begin_inset LatexCommand \index{-mds400}
4500 Generate code for the Dallas DS80C400
4501 \begin_inset LatexCommand \index{DS80C400}
4507 \labelwidthstring 00.00.0000
4512 \begin_inset LatexCommand \index{-mz80}
4518 Generate code for the Zilog Z80
4519 \begin_inset LatexCommand \index{Z80}
4523 family of processors.
4525 \labelwidthstring 00.00.0000
4530 \begin_inset LatexCommand \index{-mgbz80}
4536 Generate code for the GameBoy Z80
4537 \begin_inset LatexCommand \index{GameBoy Z80}
4543 \labelwidthstring 00.00.0000
4548 \begin_inset LatexCommand \index{-mavr}
4554 Generate code for the Atmel AVR
4555 \begin_inset LatexCommand \index{AVR}
4559 processor (In development, not complete).
4560 AVR users should probably have a look at avr-gcc
4561 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4568 I think it is fair to direct users there for now.
4569 Open source is also about avoiding unnecessary work .
4570 But I didn't find the 'official' link.
4572 \labelwidthstring 00.00.0000
4577 \begin_inset LatexCommand \index{-mpic14}
4583 Generate code for the Microchip PIC 14
4584 \begin_inset LatexCommand \index{PIC14}
4588 -bit processors (p16f84 and variants).
4591 p16f627 p16f628 p16f84 p16f873 p16f877?
4593 \labelwidthstring 00.00.0000
4598 \begin_inset LatexCommand \index{-mpic16}
4604 Generate code for the Microchip PIC 16
4605 \begin_inset LatexCommand \index{PIC16}
4609 -bit processors (p18f452 and variants).
4611 \labelwidthstring 00.00.0000
4617 Generate code for the Toshiba TLCS-900H
4618 \begin_inset LatexCommand \index{TLCS-900H}
4622 processor (In development, not complete).
4624 \labelwidthstring 00.00.0000
4629 \begin_inset LatexCommand \index{-mxa51}
4635 Generate code for the Phillips XA51
4636 \begin_inset LatexCommand \index{XA51}
4640 processor (In development, not complete).
4641 \layout Subsubsection
4643 Preprocessor Options
4644 \begin_inset LatexCommand \index{Options preprocessor}
4649 \begin_inset LatexCommand \index{Preprocessor options}
4655 \labelwidthstring 00.00.0000
4660 \begin_inset LatexCommand \index{-I<path>}
4666 The additional location where the pre processor will look for <..h> or
4667 \begin_inset Quotes eld
4671 \begin_inset Quotes erd
4676 \labelwidthstring 00.00.0000
4681 \begin_inset LatexCommand \index{-D<macro[=value]>}
4687 Command line definition of macros.
4688 Passed to the preprocessor.
4690 \labelwidthstring 00.00.0000
4695 \begin_inset LatexCommand \index{-M}
4701 Tell the preprocessor to output a rule suitable for make describing the
4702 dependencies of each object file.
4703 For each source file, the preprocessor outputs one make-rule whose target
4704 is the object file name for that source file and whose dependencies are
4705 all the files `#include'd in it.
4706 This rule may be a single line or may be continued with `
4708 '-newline if it is long.
4709 The list of rules is printed on standard output instead of the preprocessed
4712 \begin_inset LatexCommand \index{-E}
4718 \labelwidthstring 00.00.0000
4723 \begin_inset LatexCommand \index{-C}
4729 Tell the preprocessor not to discard comments.
4730 Used with the `-E' option.
4732 \labelwidthstring 00.00.0000
4737 \begin_inset LatexCommand \index{-MM}
4748 Like `-M' but the output mentions only the user header files included with
4750 \begin_inset Quotes eld
4754 System header files included with `#include <file>' are omitted.
4756 \labelwidthstring 00.00.0000
4761 \begin_inset LatexCommand \index{-Aquestion(answer)}
4767 Assert the answer answer for question, in case it is tested with a preprocessor
4768 conditional such as `#if #question(answer)'.
4769 `-A-' disables the standard assertions that normally describe the target
4772 \labelwidthstring 00.00.0000
4777 \begin_inset LatexCommand \index{-Umacro}
4783 Undefine macro macro.
4784 `-U' options are evaluated after all `-D' options, but before any `-include'
4785 and `-imacros' options.
4787 \labelwidthstring 00.00.0000
4792 \begin_inset LatexCommand \index{-dM}
4798 Tell the preprocessor to output only a list of the macro definitions that
4799 are in effect at the end of preprocessing.
4800 Used with the `-E' option.
4802 \labelwidthstring 00.00.0000
4807 \begin_inset LatexCommand \index{-dD}
4813 Tell the preprocessor to pass all macro definitions into the output, in
4814 their proper sequence in the rest of the output.
4816 \labelwidthstring 00.00.0000
4821 \begin_inset LatexCommand \index{-dN}
4832 Like `-dD' except that the macro arguments and contents are omitted.
4833 Only `#define name' is included in the output.
4834 \layout Subsubsection
4837 \begin_inset LatexCommand \index{Options linker}
4842 \begin_inset LatexCommand \index{Linker options}
4848 \labelwidthstring 00.00.0000
4868 \begin_inset LatexCommand \index{-\/-lib-path}
4873 \begin_inset LatexCommand \index{-L -\/-lib-path}
4882 <absolute path to additional libraries> This option is passed to the linkage
4883 editor's additional libraries
4884 \begin_inset LatexCommand \index{Libraries}
4889 The path name must be absolute.
4890 Additional library files may be specified in the command line.
4891 See section Compiling programs for more details.
4893 \labelwidthstring 00.00.0000
4910 \begin_inset LatexCommand \index{-\/-xram-loc}
4914 <Value> The start location of the external ram
4915 \begin_inset LatexCommand \index{xdata}
4919 , default value is 0.
4920 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4930 -xram-loc 0x8000 or -
4942 \labelwidthstring 00.00.0000
4959 \begin_inset LatexCommand \index{-\/-code-loc}
4963 <Value> The start location of the code
4964 \begin_inset LatexCommand \index{code}
4968 segment, default value 0.
4969 Note when this option is used the interrupt vector table is also relocated
4970 to the given address.
4971 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4981 -code-loc 0x8000 or -
4993 \labelwidthstring 00.00.0000
5010 \begin_inset LatexCommand \index{-\/-stack-loc}
5014 <Value> By default the stack
5015 \begin_inset LatexCommand \index{stack}
5019 is placed after the data segment.
5020 Using this option the stack can be placed anywhere in the internal memory
5022 The value entered can be in Hexadecimal or Decimal format, e.g.
5033 -stack-loc 0x20 or -
5044 Since the sp register is incremented before a push or call, the initial
5045 sp will be set to one byte prior the provided value.
5046 The provided value should not overlap any other memory areas such as used
5047 register banks or the data segment and with enough space for the current
5050 \labelwidthstring 00.00.0000
5067 \begin_inset LatexCommand \index{-\/-data-loc}
5071 <Value> The start location of the internal ram data
5072 \begin_inset LatexCommand \index{data}
5077 The value entered can be in Hexadecimal or Decimal format, eg.
5099 (By default, the start location of the internal ram data segment is set
5100 as low as possible in memory, taking into account the used register banks
5101 and the bit segment at address 0x20.
5102 For example if register banks 0 and 1 are used without bit variables, the
5103 data segment will be set, if -
5113 -data-loc is not used, to location 0x10.)
5115 \labelwidthstring 00.00.0000
5132 \begin_inset LatexCommand \index{-\/-idata-loc}
5136 <Value> The start location of the indirectly addressable internal ram
5137 \begin_inset LatexCommand \index{idata}
5141 , default value is 0x80.
5142 The value entered can be in Hexadecimal or Decimal format, eg.
5153 -idata-loc 0x88 or -
5165 \labelwidthstring 00.00.0000
5180 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5189 The linker output (final object code) is in Intel Hex format.
5190 \begin_inset LatexCommand \index{Intel hex format}
5194 (This is the default option).
5196 \labelwidthstring 00.00.0000
5211 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5220 The linker output (final object code) is in Motorola S19 format
5221 \begin_inset LatexCommand \index{Motorola S19 format}
5226 \layout Subsubsection
5229 \begin_inset LatexCommand \index{Options MCS51}
5234 \begin_inset LatexCommand \index{MCS51 options}
5240 \labelwidthstring 00.00.0000
5255 \begin_inset LatexCommand \index{-\/-model-small}
5266 Generate code for Small Model programs see section Memory Models for more
5268 This is the default model.
5270 \labelwidthstring 00.00.0000
5285 \begin_inset LatexCommand \index{-\/-model-large}
5291 Generate code for Large model programs see section Memory Models for more
5293 If this option is used all source files in the project have to be compiled
5296 \labelwidthstring 00.00.0000
5311 \begin_inset LatexCommand \index{-\/-xstack}
5317 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5318 variables and passing parameters.
5320 \begin_inset LatexCommand \ref{sub:External-Stack}
5325 External Stack for more details.
5327 \labelwidthstring 00.00.0000
5344 \begin_inset LatexCommand \index{-\/-iram-size<Value>}
5348 Causes the linker to check if the internal ram usage is within limits of
5351 \labelwidthstring 00.00.0000
5368 \begin_inset LatexCommand \index{-\/-xram-size<Value>}
5372 Causes the linker to check if the external ram usage is within limits of
5375 \labelwidthstring 00.00.0000
5392 \begin_inset LatexCommand \index{-\/-data-loc}
5396 Causes the linker to check if the code memory usage is within limits of
5398 \layout Subsubsection
5401 \begin_inset LatexCommand \index{Options DS390}
5406 \begin_inset LatexCommand \index{DS390 options}
5412 \labelwidthstring 00.00.0000
5429 \begin_inset LatexCommand \index{-\/-model-flat24}
5439 Generate 24-bit flat mode code.
5440 This is the one and only that the ds390 code generator supports right now
5441 and is default when using
5446 See section Memory Models for more details.
5448 \labelwidthstring 00.00.0000
5465 \begin_inset LatexCommand \index{-\/-stack-10bit}
5469 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
5470 This is the one and only that the ds390 code generator supports right now
5471 and is default when using
5476 In this mode, the stack is located in the lower 1K of the internal RAM,
5477 which is mapped to 0x400000.
5478 Note that the support is incomplete, since it still uses a single byte
5479 as the stack pointer.
5480 This means that only the lower 256 bytes of the potential 1K stack space
5481 will actually be used.
5482 However, this does allow you to reclaim the precious 256 bytes of low RAM
5483 for use for the DATA and IDATA segments.
5484 The compiler will not generate any code to put the processor into 10 bit
5486 It is important to ensure that the processor is in this mode before calling
5487 any re-entrant functions compiled with this option.
5488 In principle, this should work with the
5501 \begin_inset LatexCommand \index{-\/-stack-auto}
5507 option, but that has not been tested.
5508 It is incompatible with the
5521 \begin_inset LatexCommand \index{-\/-xstack}
5528 It also only makes sense if the processor is in 24 bit contiguous addressing
5541 -model-flat24 option
5544 \layout Subsubsection
5547 \begin_inset LatexCommand \index{Options Z80}
5552 \begin_inset LatexCommand \index{Z80 options}
5558 \labelwidthstring 00.00.0000
5575 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
5585 Force a called function to always save BC.
5587 \labelwidthstring 00.00.0000
5604 \begin_inset LatexCommand \index{-\/-no-std-crt0}
5608 When linking, skip the standard crt0.o object file.
5609 You must provide your own crt0.o for your system when linking.
5611 \layout Subsubsection
5613 Optimization Options
5614 \begin_inset LatexCommand \index{Options optimization}
5619 \begin_inset LatexCommand \index{Optimization options}
5625 \labelwidthstring 00.00.0000
5640 \begin_inset LatexCommand \index{-\/-nogcse}
5646 Will not do global subexpression elimination, this option may be used when
5647 the compiler creates undesirably large stack/data spaces to store compiler
5649 A warning message will be generated when this happens and the compiler
5650 will indicate the number of extra bytes it allocated.
5651 It recommended that this option NOT be used, #pragma\SpecialChar ~
5653 \begin_inset LatexCommand \index{\#pragma NOGCSE}
5657 can be used to turn off global subexpression elimination
5658 \begin_inset LatexCommand \index{Subexpression elimination}
5662 for a given function only.
5664 \labelwidthstring 00.00.0000
5679 \begin_inset LatexCommand \index{-\/-noinvariant}
5685 Will not do loop invariant optimizations, this may be turned off for reasons
5686 explained for the previous option.
5687 For more details of loop optimizations performed see section Loop Invariants.It
5688 recommended that this option NOT be used, #pragma\SpecialChar ~
5690 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
5694 can be used to turn off invariant optimizations for a given function only.
5696 \labelwidthstring 00.00.0000
5711 \begin_inset LatexCommand \index{-\/-noinduction}
5717 Will not do loop induction optimizations, see section strength reduction
5718 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
5721 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
5725 can be used to turn off induction optimizations for a given function only.
5727 \labelwidthstring 00.00.0000
5742 \begin_inset LatexCommand \index{-\/-nojtbound}
5753 Will not generate boundary condition check when switch statements
5754 \begin_inset LatexCommand \index{switch statement}
5758 are implemented using jump-tables.
5760 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
5765 Switch Statements for more details.
5766 It is recommended that this option is NOT used, #pragma\SpecialChar ~
5768 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
5772 can be used to turn off boundary checking for jump tables for a given function
5775 \labelwidthstring 00.00.0000
5790 \begin_inset LatexCommand \index{-\/-noloopreverse}
5799 Will not do loop reversal
5800 \begin_inset LatexCommand \index{Loop reversing}
5806 \labelwidthstring 00.00.0000
5823 \begin_inset LatexCommand \index{-\/-nolabelopt }
5827 Will not optimize labels (makes the dumpfiles more readable).
5829 \labelwidthstring 00.00.0000
5844 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
5850 Will not memcpy initialized data from code space into xdata space.
5851 This saves a few bytes in code space if you don't have initialized data.
5852 \layout Subsubsection
5855 \begin_inset LatexCommand \index{Options other}
5861 \labelwidthstring 00.00.0000
5877 \begin_inset LatexCommand \index{-\/-compile-only}
5882 \begin_inset LatexCommand \index{-c -\/-compile-only}
5888 will compile and assemble the source, but will not call the linkage editor.
5890 \labelwidthstring 00.00.0000
5909 \begin_inset LatexCommand \index{-\/-c1mode}
5915 reads the preprocessed source from standard input and compiles it.
5916 The file name for the assembler output must be specified using the -o option.
5918 \labelwidthstring 00.00.0000
5923 \begin_inset LatexCommand \index{-E}
5929 Run only the C preprocessor.
5930 Preprocess all the C source files specified and output the results to standard
5933 \labelwidthstring 00.00.0000
5939 \begin_inset LatexCommand \index{-o <path/file>}
5945 The output path resp.
5946 file where everything will be placed.
5947 If the parameter is a path, it must have a trailing slash (or backslash
5948 for the Windows binaries) to be recognized as a path.
5951 \labelwidthstring 00.00.0000
5966 \begin_inset LatexCommand \index{-\/-stack-auto}
5977 All functions in the source file will be compiled as
5982 \begin_inset LatexCommand \index{reentrant}
5987 the parameters and local variables will be allocated on the stack
5988 \begin_inset LatexCommand \index{stack}
5993 see section Parameters and Local Variables for more details.
5994 If this option is used all source files in the project should be compiled
5998 \labelwidthstring 00.00.0000
6013 \begin_inset LatexCommand \index{-\/-callee-saves}
6017 function1[,function2][,function3]....
6020 The compiler by default uses a caller saves convention for register saving
6021 across function calls, however this can cause unnecessary register pushing
6022 & popping when calling small functions from larger functions.
6023 This option can be used to switch the register saving convention for the
6024 function names specified.
6025 The compiler will not save registers when calling these functions, no extra
6026 code will be generated at the entry & exit (function prologue
6029 \begin_inset LatexCommand \index{function prologue}
6038 \begin_inset LatexCommand \index{function epilogue}
6044 ) for these functions to save & restore the registers used by these functions,
6045 this can SUBSTANTIALLY reduce code & improve run time performance of the
6047 In the future the compiler (with inter procedural analysis) will be able
6048 to determine the appropriate scheme to use for each function call.
6049 DO NOT use this option for built-in functions such as _mulint..., if this
6050 option is used for a library function the appropriate library function
6051 needs to be recompiled with the same option.
6052 If the project consists of multiple source files then all the source file
6053 should be compiled with the same -
6063 -callee-saves option string.
6064 Also see #pragma\SpecialChar ~
6066 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
6072 \labelwidthstring 00.00.0000
6087 \begin_inset LatexCommand \index{-\/-debug}
6096 When this option is used the compiler will generate debug information, that
6097 can be used with the SDCDB.
6098 The debug information is collected in a file with .cdb extension.
6099 For more information see documentation for SDCDB.
6101 \labelwidthstring 00.00.0000
6106 \begin_inset LatexCommand \index{-S}
6117 Stop after the stage of compilation proper; do not assemble.
6118 The output is an assembler code file for the input file specified.
6120 \labelwidthstring 00.00.0000
6124 -Wa_asmOption[,asmOption]
6127 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
6132 Pass the asmOption to the assembler.
6134 \labelwidthstring 00.00.0000
6138 -Wl_linkOption[,linkOption]
6141 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
6146 Pass the linkOption to the linker.
6148 \labelwidthstring 00.00.0000
6163 \begin_inset LatexCommand \index{-\/-int-long-reent}
6169 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
6170 Note by default these libraries are compiled as non-reentrant.
6171 See section Installation for more details.
6173 \labelwidthstring 00.00.0000
6188 \begin_inset LatexCommand \index{-\/-cyclomatic}
6197 This option will cause the compiler to generate an information message for
6198 each function in the source file.
6199 The message contains some
6203 information about the function.
6204 The number of edges and nodes the compiler detected in the control flow
6205 graph of the function, and most importantly the
6207 cyclomatic complexity
6208 \begin_inset LatexCommand \index{Cyclomatic complexity}
6214 see section on Cyclomatic Complexity for more details.
6216 \labelwidthstring 00.00.0000
6231 \begin_inset LatexCommand \index{-\/-float-reent}
6240 Floating point library is compiled as reentrant
6241 \begin_inset LatexCommand \index{reentrant}
6246 See section Installation for more details.
6248 \labelwidthstring 00.00.0000
6263 \begin_inset LatexCommand \index{-\/-nooverlay}
6269 The compiler will not overlay parameters and local variables of any function,
6270 see section Parameters and local variables for more details.
6272 \labelwidthstring 00.00.0000
6287 \begin_inset LatexCommand \index{-\/-main-return}
6293 This option can be used when the code generated is called by a monitor
6295 The compiler will generate a 'ret' upon return from the 'main'
6296 \begin_inset LatexCommand \index{main return}
6301 The default setting is to lock up i.e.
6308 \labelwidthstring 00.00.0000
6325 \begin_inset LatexCommand \index{-\/-peep-file}
6329 <filename> This option can be used to use additional rules to be used by
6330 the peep hole optimizer.
6332 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6337 Peep Hole optimizations for details on how to write these rules.
6339 \labelwidthstring 00.00.0000
6354 \begin_inset LatexCommand \index{-\/-no-peep}
6360 Disable peep-hole optimization.
6362 \labelwidthstring 00.00.0000
6377 \begin_inset LatexCommand \index{-\/-peep-asm}
6383 Pass the inline assembler code through the peep hole optimizer.
6384 This can cause unexpected changes to inline assembler code, please go through
6385 the peephole optimizer
6386 \begin_inset LatexCommand \index{Peephole optimizer}
6390 rules defined in the source file tree '<target>/peeph.def' before using
6393 \labelwidthstring 00.00.0000
6408 \begin_inset LatexCommand \index{-\/-nostdincl}
6414 This will prevent the compiler from passing on the default include path
6415 to the preprocessor.
6417 \labelwidthstring 00.00.0000
6432 \begin_inset LatexCommand \index{-\/-nostdlib}
6438 This will prevent the compiler from passing on the default library
6439 \begin_inset LatexCommand \index{Libraries}
6445 \labelwidthstring 00.00.0000
6460 \begin_inset LatexCommand \index{-\/-verbose}
6466 Shows the various actions the compiler is performing.
6468 \labelwidthstring 00.00.0000
6473 \begin_inset LatexCommand \index{-V}
6479 Shows the actual commands the compiler is executing.
6481 \labelwidthstring 00.00.0000
6496 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
6502 Hides your ugly and inefficient c-code from the asm file, so you can always
6503 blame the compiler :).
6505 \labelwidthstring 00.00.0000
6520 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
6526 Include i-codes in the asm file.
6527 Sounds like noise but is most helpful for debugging the compiler itself.
6529 \labelwidthstring 00.00.0000
6544 \begin_inset LatexCommand \index{-\/-less-pedantic}
6550 Disable some of the more pedantic warnings
6551 \begin_inset LatexCommand \index{Warnings}
6555 (jwk burps: please be more specific here, please!).
6556 If you want rather more than less warnings you should consider using a
6557 separate tool dedicated to syntax checking like
6558 \begin_inset LatexCommand \url{www.splint.org}
6564 \labelwidthstring 00.00.0000
6579 \begin_inset LatexCommand \index{-\/-print-search-dirs}
6585 Display the directories in the compiler's search path
6587 \labelwidthstring 00.00.0000
6602 \begin_inset LatexCommand \index{-\/-vc}
6608 Display errors and warnings using MSVC style, so you can use SDCC with
6611 \labelwidthstring 00.00.0000
6626 \begin_inset LatexCommand \index{-\/-use-stdout}
6632 Send errors and warnings to stdout instead of stderr.
6633 \layout Subsubsection
6635 Intermediate Dump Options
6636 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
6641 \begin_inset LatexCommand \index{Options intermediate dump}
6646 \begin_inset LatexCommand \index{Intermediate dump options}
6653 The following options are provided for the purpose of retargetting and debugging
6655 These provided a means to dump the intermediate code (iCode
6656 \begin_inset LatexCommand \index{iCode}
6660 ) generated by the compiler in human readable form at various stages of
6661 the compilation process.
6664 \labelwidthstring 00.00.0000
6679 \begin_inset LatexCommand \index{-\/-dumpraw}
6685 This option will cause the compiler to dump the intermediate code into
6688 <source filename>.dumpraw
6690 just after the intermediate code has been generated for a function, i.e.
6691 before any optimizations are done.
6693 \begin_inset LatexCommand \index{Basic blocks}
6697 at this stage ordered in the depth first number, so they may not be in
6698 sequence of execution.
6700 \labelwidthstring 00.00.0000
6715 \begin_inset LatexCommand \index{-\/-dumpgcse}
6721 Will create a dump of iCode's, after global subexpression elimination
6722 \begin_inset LatexCommand \index{Global subexpression elimination}
6728 <source filename>.dumpgcse.
6730 \labelwidthstring 00.00.0000
6745 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
6751 Will create a dump of iCode's, after deadcode elimination
6752 \begin_inset LatexCommand \index{Dead-code elimination}
6758 <source filename>.dumpdeadcode.
6760 \labelwidthstring 00.00.0000
6775 \begin_inset LatexCommand \index{-\/-dumploop}
6784 Will create a dump of iCode's, after loop optimizations
6785 \begin_inset LatexCommand \index{Loop optimization}
6791 <source filename>.dumploop.
6793 \labelwidthstring 00.00.0000
6808 \begin_inset LatexCommand \index{-\/-dumprange}
6817 Will create a dump of iCode's, after live range analysis
6818 \begin_inset LatexCommand \index{Live range analysis}
6824 <source filename>.dumprange.
6826 \labelwidthstring 00.00.0000
6841 \begin_inset LatexCommand \index{-\/-dumlrange}
6847 Will dump the life ranges
6848 \begin_inset LatexCommand \index{Live range analysis}
6854 \labelwidthstring 00.00.0000
6869 \begin_inset LatexCommand \index{-\/-dumpregassign}
6878 Will create a dump of iCode's, after register assignment
6879 \begin_inset LatexCommand \index{Register assignment}
6885 <source filename>.dumprassgn.
6887 \labelwidthstring 00.00.0000
6902 \begin_inset LatexCommand \index{-\/-dumplrange}
6908 Will create a dump of the live ranges of iTemp's
6910 \labelwidthstring 00.00.0000
6925 \begin_inset LatexCommand \index{-\/-dumpall}
6936 Will cause all the above mentioned dumps to be created.
6937 \layout Subsubsection
6939 Redirecting output on Windows Shells
6942 By default SDCC writes it's error messages to
6943 \begin_inset Quotes sld
6947 \begin_inset Quotes srd
6951 To force all messages to
6952 \begin_inset Quotes sld
6956 \begin_inset Quotes srd
6980 \begin_inset LatexCommand \index{-\/-use-stdout}
6985 Aditionaly, if you happen to have visual studio installed in your windows
6986 machine, you can use it to compile your sources using a custom build and
7002 \begin_inset LatexCommand \index{-\/-vc}
7007 Something like this should work:
7051 -model-large -c $(InputPath)
7054 Environment variables
7055 \begin_inset LatexCommand \index{Environment variables}
7062 SDCC recognizes the following environment variables:
7064 \labelwidthstring 00.00.0000
7069 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7075 SDCC installs a signal handler
7076 \begin_inset LatexCommand \index{signal handler}
7080 to be able to delete temporary files after an user break (^C) or an exception.
7081 If this environment variable is set, SDCC won't install the signal handler
7082 in order to be able to debug SDCC.
7084 \labelwidthstring 00.00.0000
7091 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7097 Path, where temporary files will be created.
7098 The order of the variables is the search order.
7099 In a standard *nix environment these variables are not set, and there's
7100 no need to set them.
7101 On Windows it's recommended to set one of them.
7103 \labelwidthstring 00.00.0000
7108 \begin_inset LatexCommand \index{SDCC\_HOME}
7115 \begin_inset LatexCommand \ref{sub:Install-paths}
7121 \begin_inset Quotes sld
7125 \begin_inset Quotes srd
7130 \labelwidthstring 00.00.0000
7135 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
7142 \begin_inset LatexCommand \ref{sub:Search-Paths}
7148 \begin_inset Quotes sld
7152 \begin_inset Quotes srd
7157 \labelwidthstring 00.00.0000
7162 \begin_inset LatexCommand \index{SDCC\_LIB}
7169 \begin_inset LatexCommand \ref{sub:Search-Paths}
7175 \begin_inset Quotes sld
7179 \begin_inset Quotes srd
7185 There are some more environment variables recognized by SDCC, but these
7186 are solely used for debugging purposes.
7187 They can change or disappear very quickly, and will never be documented.
7190 MCS51/DS390 Storage Class
7191 \begin_inset LatexCommand \index{Storage class}
7198 In addition to the ANSI storage classes SDCC allows the following MCS51
7199 specific storage classes.
7200 \layout Subsubsection
7203 \begin_inset LatexCommand \index{data}
7214 storage class for the Small Memory model.
7215 Variables declared with this storage class will be allocated in the directly
7216 addressable portion of the internal RAM of a 8051, e.g.:
7221 data unsigned char test_data;
7224 Writing 0x01 to this variable generates the assembly code:
7229 75*00 01\SpecialChar ~
7235 \layout Subsubsection
7238 \begin_inset LatexCommand \index{xdata}
7245 Variables declared with this storage class will be placed in the external
7251 storage class for the Large Memory model, e.g.:
7256 xdata unsigned char test_xdata;
7259 Writing 0x01 to this variable generates the assembly code:
7264 90s00r00\SpecialChar ~
7293 \layout Subsubsection
7296 \begin_inset LatexCommand \index{idata}
7303 Variables declared with this storage class will be allocated into the indirectly
7304 addressable portion of the internal ram of a 8051, e.g.:
7309 idata unsigned char test_idata;
7312 Writing 0x01 to this variable generates the assembly code:
7339 \layout Subsubsection
7342 \begin_inset LatexCommand \index{pdata}
7349 Paged xdata access is currently not as straightforward as using the other
7350 addressing modes of a 8051.
7351 The following example writes 0x01 to the address pointed to.
7352 Please note, pdata access physically accesses xdata memory.
7353 The high byte of the address is determined by port P2 (or in case of some
7354 8051 variants by a separate Special Function Register).
7359 pdata unsigned char *test_pdata_ptr;
7371 test_pdata_ptr = (pdata *)0xfe;
7377 *test_pdata_ptr = 1;
7382 Generates the assembly code:
7387 75*01 FE\SpecialChar ~
7391 _test_pdata_ptr,#0xFE
7423 Be extremely carefull if you use pdata together with the -
7434 \begin_inset LatexCommand \index{-\/-xstack}
7439 \layout Subsubsection
7442 \begin_inset LatexCommand \index{code}
7449 'Variables' declared with this storage class will be placed in the code
7455 code unsigned char test_code;
7458 Read access to this variable generates the assembly code:
7463 90s00r6F\SpecialChar ~
7466 mov dptr,#_test_code
7489 \layout Subsubsection
7492 \begin_inset LatexCommand \index{bit}
7499 This is a data-type and a storage class specifier.
7500 When a variable is declared as a bit, it is allocated into the bit addressable
7501 memory of 8051, e.g.:
7509 Writing 1 to this variable generates the assembly code:
7523 \layout Subsubsection
7526 \begin_inset LatexCommand \index{sfr}
7531 \begin_inset LatexCommand \index{sbit}
7538 Like the bit keyword,
7542 signifies both a data-type and storage class, they are used to describe
7543 the special function registers and special bit variables of a 8051, eg:
7549 \begin_inset LatexCommand \index{at}
7553 0x80 P0;\SpecialChar ~
7554 /* special function register P0 at location 0x80 */
7556 sbit at 0xd7 CY; /* CY (Carry Flag
7557 \begin_inset LatexCommand \index{Flags}
7562 \begin_inset LatexCommand \index{Carry flag}
7567 \layout Subsubsection
7570 \begin_inset LatexCommand \index{Pointers}
7574 to MCS51/DS390 specific memory spaces
7577 SDCC allows (via language extensions) pointers to explicitly point to any
7578 of the memory spaces
7579 \begin_inset LatexCommand \index{Memory model}
7584 In addition to the explicit pointers, the compiler uses (by default) generic
7585 pointers which can be used to point to any of the memory spaces.
7589 Pointer declaration examples:
7594 /* pointer physically in internal ram pointing to object in external ram
7597 xdata unsigned char * data p;
7601 /* pointer physically in external ram pointing to object in internal ram
7604 data unsigned char * xdata p;
7608 /* pointer physically in code rom pointing to data in xdata space */
7610 xdata unsigned char * code p;
7614 /* pointer physically in code space pointing to data in code space */
7616 code unsigned char * code p;
7620 /* the following is a generic pointer physically located in xdata space
7626 Well you get the idea.
7631 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
7644 The highest order byte of the
7648 pointers contains the data space information.
7649 Assembler support routines are called whenever data is stored or retrieved
7655 These are useful for developing reusable library
7656 \begin_inset LatexCommand \index{Libraries}
7661 Explicitly specifying the pointer type will generate the most efficient
7666 \begin_inset LatexCommand \index{Absolute addressing}
7673 Data items can be assigned an absolute address with the
7676 \begin_inset LatexCommand \index{at}
7682 keyword, in addition to a storage class, e.g.:
7688 \begin_inset LatexCommand \index{xdata}
7693 \begin_inset LatexCommand \index{at}
7697 0x7ffe unsigned int chksum;
7700 In the above example the variable chksum will located at 0x7ffe and 0x7fff
7701 of the external ram.
7702 The compiler does not actually reserve any space for variables declared
7703 in this way (they are implemented with an equate in the assembler).
7704 Thus it is left to the programmer to make sure there are no overlaps with
7705 other variables that are declared without the absolute address.
7706 The assembler listing file (.lst
7707 \begin_inset LatexCommand \index{.lst}
7711 ) and the linker output files (.rst
7712 \begin_inset LatexCommand \index{.rst}
7717 \begin_inset LatexCommand \index{.map}
7721 ) are good places to look for such overlaps.
7724 In case of memory mapped I/O devices the keyword
7728 should be used to tell the compiler that accesses might not be optimized
7735 \begin_inset LatexCommand \index{volatile}
7740 \begin_inset LatexCommand \index{xdata}
7745 \begin_inset LatexCommand \index{at}
7749 0x8000 unsigned char PORTA_8255;
7752 Absolute address can be specified for variables in all storage classes,
7759 \begin_inset LatexCommand \index{bit}
7764 \begin_inset LatexCommand \index{at}
7771 The above example will allocate the variable at offset 0x02 in the bit-addressab
7773 There is no real advantage to assigning absolute addresses to variables
7774 in this manner, unless you want strict control over all the variables allocated.
7775 One possible use would be to write hardware portable code.
7776 For example, if you have a routine that uses one or more of the microcontroller
7777 I/O pins, and such pins are different for two different hardwares, you
7778 can declare the I/O pins in your routine using:
7783 extern volatile bit SDI;
7785 extern volatile bit SCLK;
7787 extern volatile bit CPOL;
7791 void DS1306_put(unsigned char value)
7799 unsigned char mask=0x80;
7823 SDI=(value & mask)?1:0;
7864 Then, someplace in the code for the first hardware you would use
7869 bit at 0x80 SDI;\SpecialChar ~
7873 /* I/O port 0, bit 0 */
7875 bit at 0x81 SCLK;\SpecialChar ~
7878 /* I/O port 0, bit 1 */
7880 bit CPOL;\SpecialChar ~
7891 /* This is a variable, let the linker allocate this one */
7894 Similarly, for the second hardware you would use
7899 bit at 0x83 SDI;\SpecialChar ~
7903 /* I/O port 0, bit 3 */
7905 bit at 0x91 SCLK;\SpecialChar ~
7908 /* I/O port 1, bit 1 */
7910 bit CPOL;\SpecialChar ~
7921 /* This is a variable, let the linker allocate this one */
7924 and you can use the same hardware dependent routine without changes, as
7925 for example in a library.
7926 This is somehow similar to sbit, but only one absolute address has to be
7927 specified in the whole project.
7931 \begin_inset LatexCommand \index{Parameters}
7936 \begin_inset LatexCommand \index{Local variable}
7943 Automatic (local) variables and parameters to functions can either be placed
7944 on the stack or in data-space.
7945 The default action of the compiler is to place these variables in the internal
7946 RAM (for small model) or external RAM (for large model).
7947 This in fact makes them similar to
7950 \begin_inset LatexCommand \index{static}
7956 so by default functions are non-reentrant
7957 \begin_inset LatexCommand \index{reentrant}
7966 They can be placed on the stack
7967 \begin_inset LatexCommand \index{stack}
7984 \begin_inset LatexCommand \index{-\/-stack-auto}
7990 option or by using the
7993 \begin_inset LatexCommand \index{reentrant}
7999 keyword in the function declaration, e.g.:
8004 unsigned char foo(char i) reentrant
8018 Since stack space on 8051 is limited, the
8036 option should be used sparingly.
8037 Note that the reentrant keyword just means that the parameters & local
8038 variables will be allocated to the stack, it
8042 mean that the function is register bank independent.
8046 Local variables can be assigned storage classes and absolute
8047 \begin_inset LatexCommand \index{Absolute addressing}
8064 xdata unsigned char i;
8076 data at 0x31 unsigned char j;
8088 In the above example the variable
8092 will be allocated in the external ram,
8096 in bit addressable space and
8115 or when a function is declared as
8119 this should only be done for static variables.
8122 Parameters however are not allowed any storage class, (storage classes for
8123 parameters will be ignored), their allocation is governed by the memory
8124 model in use, and the reentrancy options.
8128 \begin_inset LatexCommand \label{sub:Overlaying}
8133 \begin_inset LatexCommand \index{Overlaying}
8141 \begin_inset LatexCommand \index{reentrant}
8145 functions SDCC will try to reduce internal ram space usage by overlaying
8146 parameters and local variables of a function (if possible).
8147 Parameters and local variables of a function will be allocated to an overlayabl
8148 e segment if the function has
8150 no other function calls and the function is non-reentrant and the memory
8152 \begin_inset LatexCommand \index{Memory model}
8159 If an explicit storage class
8160 \begin_inset LatexCommand \index{Storage class}
8164 is specified for a local variable, it will NOT be overlayed.
8167 Note that the compiler (not the linkage editor) makes the decision for overlayin
8169 Functions that are called from an interrupt service routine should be preceded
8170 by a #pragma\SpecialChar ~
8172 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8176 if they are not reentrant.
8179 Also note that the compiler does not do any processing of inline
8180 \begin_inset LatexCommand \index{inline}
8184 assembler code, so the compiler might incorrectly assign local variables
8185 and parameters of a function into the overlay segment if the inline assembler
8186 code calls other c-functions that might use the overlay.
8187 In that case the #pragma\SpecialChar ~
8188 NOOVERLAY should be used.
8191 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
8193 \begin_inset LatexCommand \index{Multiplication}
8198 \begin_inset LatexCommand \index{Division}
8202 will NOT be overlayed since these are implemented using external functions,
8211 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8217 void set_error(unsigned char errcd)
8233 void some_isr () interrupt
8234 \begin_inset LatexCommand \index{interrupt}
8264 In the above example the parameter
8272 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
8274 not present, this could cause unpredictable runtime behavior when called
8276 The #pragma\SpecialChar ~
8277 NOOVERLAY ensures that the parameters and local variables for
8278 the function are NOT overlayed.
8281 Interrupt Service Routines
8282 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
8289 SDCC allows interrupt service routines to be coded in C, with some extended
8295 void timer_isr (void) interrupt 1 using 1
8309 The optional number following the
8312 \begin_inset LatexCommand \index{interrupt}
8318 keyword is the interrupt number this routine will service.
8319 When present, the compiler will insert a call to this routine in the interrupt
8320 vector table for the interrupt number specified.
8325 keyword can be used to tell the compiler to use the specified register
8326 bank (8051 specific) when generating code for this function.
8332 If you have multiple source files in your project, interrupt service routines
8333 can be present in any of them, but a prototype of the isr MUST be present
8334 or included in the file that contains the function
8341 Interrupt numbers and the corresponding address & descriptions for the Standard
8342 8051/8052 are listed below.
8343 SDCC will automatically adjust the interrupt vector table to the maximum
8344 interrupt number specified.
8350 \begin_inset Tabular
8351 <lyxtabular version="3" rows="7" columns="3">
8353 <column alignment="center" valignment="top" leftline="true" width="0in">
8354 <column alignment="center" valignment="top" leftline="true" width="0in">
8355 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
8356 <row topline="true" bottomline="true">
8357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8365 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8373 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8382 <row topline="true">
8383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8408 <row topline="true">
8409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8434 <row topline="true">
8435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8451 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8460 <row topline="true">
8461 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8486 <row topline="true">
8487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8512 <row topline="true" bottomline="true">
8513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8547 If the interrupt service routine is defined without
8550 \begin_inset LatexCommand \index{using}
8556 a register bank or with register bank 0 (using 0), the compiler will save
8557 the registers used by itself on the stack upon entry and restore them at
8558 exit, however if such an interrupt service routine calls another function
8559 then the entire register bank will be saved on the stack.
8560 This scheme may be advantageous for small interrupt service routines which
8561 have low register usage.
8564 If the interrupt service routine is defined to be using a specific register
8569 are save and restored, if such an interrupt service routine calls another
8570 function (using another register bank) then the entire register bank of
8571 the called function will be saved on the stack.
8572 This scheme is recommended for larger interrupt service routines.
8575 Using interrupts opens the door for some very interesting bugs:
8578 If the interrupt service routines changes variables which are accessed by
8579 other functions these variables should be declared
8584 \begin_inset LatexCommand \index{volatile}
8589 If the access to these variables is not
8592 \begin_inset LatexCommand \index{atomic access}
8599 the processor needs more than one instruction for the access and could
8600 be interrupted while accessing the variable) the interrupt must disabled
8601 during the access to avoid inconsistent data.
8602 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
8603 and should be protected by disabling interrupts.
8604 You're not automatically on the safe side if you use 8 bit variables though.
8605 We need an example here: f.e.
8606 on the 8051 the harmless looking
8607 \begin_inset Quotes srd
8617 \begin_inset Quotes sld
8626 \begin_inset Quotes srd
8636 \begin_inset Quotes sld
8639 from within an interrupt routine might get lost if the interrupt occurs
8642 \begin_inset Quotes sld
8647 counter\SpecialChar ~
8652 \begin_inset Quotes srd
8655 is not atomic on the 8051 even if
8659 is located in data memory.
8660 Bugs like these are hard to reproduce and can cause a lot of trouble.
8664 A special note here, int (16 bit) and long (32 bit) integer division
8665 \begin_inset LatexCommand \index{Division}
8670 \begin_inset LatexCommand \index{Multiplication}
8675 \begin_inset LatexCommand \index{Modulus}
8680 \begin_inset LatexCommand \index{Floating point support}
8684 operations are implemented using external support routines developed in
8686 If an interrupt service routine needs to do any of these operations then
8687 the support routines (as mentioned in a following section) will have to
8688 be recompiled using the
8701 \begin_inset LatexCommand \index{-\/-stack-auto}
8707 option and the source file will need to be compiled using the
8722 \begin_inset LatexCommand \index{-\/-int-long-rent}
8729 Calling other functions from an interrupt service routine is not recommended,
8730 avoid it if possible.
8731 Note that when some function is called from an interrupt service routine
8732 it should be preceded by a #pragma\SpecialChar ~
8734 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
8738 if it is not reentrant.
8739 Furthermore nonreentrant functions should not be called from the main program
8740 while the interrupt service routine might be active.
8746 \begin_inset LatexCommand \ref{sub:Overlaying}
8751 about Overlaying and section
8752 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
8757 about Functions using private banks.
8765 A special keyword may be associated with a function declaring it as
8770 SDCC will generate code to disable all interrupts
8771 \begin_inset LatexCommand \index{interrupt}
8775 upon entry to a critical function and restore the interrupt enable to the
8776 previous state before returning.
8777 Note that nesting critical functions will need one additional byte on the
8779 \begin_inset LatexCommand \index{stack}
8789 \begin_inset LatexCommand \index{critical}
8814 The critical attribute maybe used with other attributes like
8819 Functions using private banks
8820 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
8825 \begin_inset LatexCommand \index{bank}
8835 \begin_inset LatexCommand \index{using}
8841 attribute (which tells the compiler to use a register bank other than the
8842 default bank zero) should only be applied to
8845 \begin_inset LatexCommand \index{interrupt}
8851 functions (see note 1 below).
8852 This will in most circumstances make the generated ISR code more efficient
8853 since it will not have to save registers on the stack.
8860 attribute will have no effect on the generated code for a
8864 function (but may occasionally be useful anyway
8870 possible exception: if a function is called ONLY from 'interrupt' functions
8871 using a particular bank, it can be declared with the same 'using' attribute
8872 as the calling 'interrupt' functions.
8873 For instance, if you have several ISRs using bank one, and all of them
8874 call memcpy(), it might make sense to create a specialized version of memcpy()
8875 'using 1', since this would prevent the ISR from having to save bank zero
8876 to the stack on entry and switch to bank zero before calling the function
8883 (pending: I don't think this has been done yet)
8890 function using a non-zero bank will assume that it can trash that register
8891 bank, and will not save it.
8892 Since high-priority interrupts
8893 \begin_inset LatexCommand \index{interrupt priority}
8897 can interrupt low-priority ones on the 8051 and friends, this means that
8898 if a high-priority ISR
8902 a particular bank occurs while processing a low-priority ISR
8906 the same bank, terrible and bad things can happen.
8907 To prevent this, no single register bank should be
8911 by both a high priority and a low priority ISR.
8912 This is probably most easily done by having all high priority ISRs use
8913 one bank and all low priority ISRs use another.
8914 If you have an ISR which can change priority at runtime, you're on your
8915 own: I suggest using the default bank zero and taking the small performance
8919 It is most efficient if your ISR calls no other functions.
8920 If your ISR must call other functions, it is most efficient if those functions
8921 use the same bank as the ISR (see note 1 below); the next best is if the
8922 called functions use bank zero.
8923 It is very inefficient to call a function using a different, non-zero bank
8929 \begin_inset LatexCommand \label{sub:Startup-Code}
8934 \begin_inset LatexCommand \index{Startup code}
8939 \layout Subsubsection
8941 MCS51/DS390 Startup Code
8944 The compiler inserts a call to the C routine
8946 _sdcc_external_startup()
8947 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
8956 at the start of the CODE area.
8957 This routine is in the runtime library
8958 \begin_inset LatexCommand \index{Runtime library}
8963 By default this routine returns 0, if this routine returns a non-zero value,
8964 the static & global variable initialization will be skipped and the function
8965 main will be invoked.
8966 Otherwise static & global variables will be initialized before the function
8970 _sdcc_external_startup()
8972 routine to your program to override the default if you need to setup hardware
8973 or perform some other critical operation prior to static & global variable
8975 See also the compiler option
8994 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
8999 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9004 about MCS51-variants.
9005 \layout Subsubsection
9010 On the Z80 the startup code is inserted by linking with crt0.o which is generated
9011 from sdcc/device/lib/z80/crt0.s.
9012 If you need a different startup code you can use the compiler option
9033 \begin_inset LatexCommand \index{-\/-no-std-crt0}
9037 and provide your own crt0.o.
9041 Inline Assembler Code
9042 \begin_inset LatexCommand \index{Assembler routines}
9047 \layout Subsubsection
9049 A Step by Step Introduction
9052 Starting from a small snippet of c-code this example shows for the MCS51
9053 how to use inline assembly, access variables, a function parameter and
9054 an array in xdata memory.
9055 This is a buffer routine which should be optimized:
9061 unsigned char xdata at 0x7f00 buf[0x100];
9063 unsigned char head,tail;
9067 void to_buffer( unsigned char c )
9075 if( head != tail-1 )
9090 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
9091 then a corresponding buffer.asm file is generated.
9092 We define a new function
9096 in file buffer.c in which we cut and paste the generated code, removing
9097 unwanted comments and some ':'.
9099 \begin_inset Quotes sld
9103 \begin_inset Quotes srd
9107 \begin_inset Quotes sld
9111 \begin_inset Quotes srd
9114 to the beginning and the end of the function body:
9120 /* With a cut and paste from the .asm file, we have something to start with.
9125 The function is not yet OK! (registers aren't saved) */
9127 void to_buffer_asm( unsigned char c )
9136 \begin_inset LatexCommand \index{\_asm}
9150 ;buffer.c if( head != tail-1 )
9192 ;buffer.c buf[ head++ ] = c;
9288 The new file buffer.c should compile with only one warning about the unreferenced
9289 function argument 'c'.
9290 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
9297 unsigned char xdata at 0x7f00 buf[0x100];
9299 unsigned char head,tail;
9301 #define USE_ASSEMBLY (1)
9305 #ifndef USE_ASSEMBLY
9307 void to_buffer( unsigned char c )
9315 if( head != tail-1 )
9335 void to_buffer( unsigned char c )
9343 c; // to avoid warning: unreferenced function argument
9350 \begin_inset LatexCommand \index{\_asm}
9364 ; save used registers here.
9375 ; If we were still using r2,r3 we would have to push them here.
9378 ; if( head != tail-1 )
9421 ; we could do an ANL a,#0x0f here to use a smaller buffer
9445 ; buf[ head++ ] = c;
9456 a,dpl \SpecialChar ~
9463 ; dpl holds lower byte of function argument
9474 dpl,_head \SpecialChar ~
9477 ; buf is 0x100 byte aligned so head can be used directly
9531 ; restore used registers here
9544 The inline assembler code can contain any valid code understood by the assembler
9545 , this includes any assembler directives and comment lines
9551 The assembler does not like some characters like ':' or ''' in comments.
9555 The compiler does not do any validation of the code within the
9558 \begin_inset LatexCommand \index{\_asm}
9566 Specifically it will not know which registers are used and thus register
9568 \begin_inset LatexCommand \index{push/pop}
9572 has to be done manually.
9576 It is strongly recommended that each assembly instruction (including labels)
9577 be placed in a separate line (as the example shows).
9591 \begin_inset LatexCommand \index{-\/-peep-asm}
9597 command line option is used, the inline assembler code will be passed through
9598 the peephole optimizer
9599 \begin_inset LatexCommand \index{Peephole optimizer}
9604 There are only a few (if any) cases where this option makes sense, it might
9605 cause some unexpected changes in the inline assembler code.
9606 Please go through the peephole optimizer rules defined in file
9610 carefully before using this option.
9611 \layout Subsubsection
9614 \begin_inset LatexCommand \label{sub:Naked-Functions}
9619 \begin_inset LatexCommand \index{Naked functions}
9626 A special keyword may be associated with a function declaring it as
9629 \begin_inset LatexCommand \index{\_naked}
9640 function modifier attribute prevents the compiler from generating prologue
9641 \begin_inset LatexCommand \index{function prologue}
9646 \begin_inset LatexCommand \index{function epilogue}
9650 code for that function.
9651 This means that the user is entirely responsible for such things as saving
9652 any registers that may need to be preserved, selecting the proper register
9653 bank, generating the
9657 instruction at the end, etc.
9658 Practically, this means that the contents of the function must be written
9659 in inline assembler.
9660 This is particularly useful for interrupt functions, which can have a large
9661 (and often unnecessary) prologue/epilogue.
9662 For example, compare the code generated by these two functions:
9668 \begin_inset LatexCommand \index{volatile}
9672 data unsigned char counter;
9676 void simpleInterrupt(void) interrupt
9677 \begin_inset LatexCommand \index{interrupt}
9695 void nakedInterrupt(void) interrupt 2 _naked
9704 \begin_inset LatexCommand \index{\_asm}
9733 ; MUST explicitly include ret or reti in _naked function.
9740 \begin_inset LatexCommand \index{\_endasm}
9749 For an 8051 target, the generated simpleInterrupt looks like:
9890 whereas nakedInterrupt looks like:
9914 ; MUST explicitly include ret or reti in _naked function.
9917 The #pragma directive EXCLUDE
9918 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
9922 allows a more fine grained control over pushing & popping
9923 \begin_inset LatexCommand \index{push/pop}
9930 While there is nothing preventing you from writing C code inside a _naked
9931 function, there are many ways to shoot yourself in the foot doing this,
9932 and it is recommended that you stick to inline assembler.
9933 \layout Subsubsection
9935 Use of Labels within Inline Assembler
9938 SDCC allows the use of in-line assembler with a few restriction as regards
9940 In older versions of the compiler all labels defined within inline assembler
9949 where nnnn is a number less than 100 (which implies a limit of utmost 100
9950 inline assembler labels
9964 \begin_inset LatexCommand \index{\_asm}
9994 \begin_inset LatexCommand \index{\_endasm}
10001 Inline assembler code cannot reference any C-Labels, however it can reference
10003 \begin_inset LatexCommand \index{Labels}
10007 defined by the inline assembler, e.g.:
10032 ; some assembler code
10052 /* some more c code */
10054 clabel:\SpecialChar ~
10056 /* inline assembler cannot reference this label */
10068 $0003: ;label (can be reference by inline assembler only)
10080 /* some more c code */
10085 In other words inline assembly code can access labels defined in inline
10086 assembly within the scope of the function.
10087 The same goes the other way, ie.
10088 labels defines in inline assembly can not be accessed by C statements.
10091 Interfacing with Assembler Code
10092 \begin_inset LatexCommand \index{Assembler routines}
10097 \layout Subsubsection
10099 Global Registers used for Parameter Passing
10100 \begin_inset LatexCommand \index{Parameter passing}
10107 The compiler always uses the global registers
10110 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10115 \begin_inset LatexCommand \index{B (register)}
10124 \begin_inset LatexCommand \index{ACC}
10130 to pass the first parameter to a routine.
10131 The second parameter onwards is either allocated on the stack (for reentrant
10142 -stack-auto is used) or in data / xdata memory (depending on the memory
10145 \layout Subsubsection
10147 Assembler Routine(non-reentrant
10148 \begin_inset LatexCommand \index{reentrant}
10153 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
10160 In the following example the function c_func calls an assembler routine
10161 asm_func, which takes two parameters.
10166 extern int asm_func(unsigned char, unsigned char);
10170 int c_func (unsigned char i, unsigned char j)
10178 return asm_func(i,j);
10192 return c_func(10,9);
10197 The corresponding assembler function is:
10202 .globl _asm_func_PARM_2
10266 add a,_asm_func_PARM_2
10291 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10308 Note here that the return values are placed in 'dpl' - One byte return value,
10309 'dpl' LSB & 'dph' MSB for two byte values.
10310 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
10311 b' & 'acc' for four byte values.
10314 The parameter naming convention is _<function_name>_PARM_<n>, where n is
10315 the parameter number starting from 1, and counting from the left.
10316 The first parameter is passed in
10317 \begin_inset Quotes eld
10321 \begin_inset Quotes erd
10324 for One bye parameter,
10325 \begin_inset Quotes eld
10329 \begin_inset Quotes erd
10333 \begin_inset Quotes eld
10337 \begin_inset Quotes erd
10340 for three bytes and
10341 \begin_inset Quotes eld
10345 \begin_inset Quotes erd
10348 for four bytes, the variable name for the second parameter will be _<function_n
10353 Assemble the assembler routine with the following command:
10360 asx8051 -losg asmfunc.asm
10367 Then compile and link the assembler routine to the C source file with the
10375 sdcc cfunc.c asmfunc.rel
10376 \layout Subsubsection
10378 Assembler Routine(reentrant
10379 \begin_inset LatexCommand \index{reentrant}
10384 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
10391 In this case the second parameter onwards will be passed on the stack, the
10392 parameters are pushed from right to left i.e.
10393 after the call the left most parameter will be on the top of the stack.
10394 Here is an example:
10399 extern int asm_func(unsigned char, unsigned char);
10403 int c_func (unsigned char i, unsigned char j) reentrant
10411 return asm_func(i,j);
10425 return c_func(10,9);
10430 The corresponding assembler routine is:
10536 The compiling and linking procedure remains the same, however note the extra
10537 entry & exit linkage required for the assembler code, _bp is the stack
10538 frame pointer and is used to compute the offset into the stack for parameters
10539 and local variables.
10543 \begin_inset LatexCommand \index{int (16 bit)}
10548 \begin_inset LatexCommand \index{long (32 bit)}
10555 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
10556 multiplication and modulus operations are implemented by support routines.
10557 These support routines are all developed in ANSI-C to facilitate porting
10558 to other MCUs, although some model specific assembler optimizations are
10560 The following files contain the described routines, all of them can be
10561 found in <installdir>/share/sdcc/lib.
10567 \begin_inset Tabular
10568 <lyxtabular version="3" rows="11" columns="2">
10570 <column alignment="center" valignment="top" leftline="true" width="0">
10571 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
10572 <row topline="true" bottomline="true">
10573 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10594 <row topline="true">
10595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10608 16 bit multiplication
10612 <row topline="true">
10613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10626 signed 16 bit division (calls _divuint)
10630 <row topline="true">
10631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10639 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10644 unsigned 16 bit division
10648 <row topline="true">
10649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10662 signed 16 bit modulus (calls _moduint)
10666 <row topline="true">
10667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10675 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10680 unsigned 16 bit modulus
10684 <row topline="true">
10685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10698 32 bit multiplication
10702 <row topline="true">
10703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10716 signed 32 division (calls _divulong)
10720 <row topline="true">
10721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10734 unsigned 32 division
10738 <row topline="true">
10739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10747 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10752 signed 32 bit modulus (calls _modulong)
10756 <row topline="true" bottomline="true">
10757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10770 unsigned 32 bit modulus
10783 Since they are compiled as
10788 \begin_inset LatexCommand \index{reentrant}
10793 \begin_inset LatexCommand \index{interrupt}
10797 service routines should not do any of the above operations.
10798 If this is unavoidable then the above routines will need to be compiled
10812 \begin_inset LatexCommand \index{-\/-stack-auto}
10818 option, after which the source program will have to be compiled with
10831 \begin_inset LatexCommand \index{-\/-int-long-rent}
10838 Notice that you don't have to call this routines directly.
10839 The compiler will use them automatically every time an integer operation
10843 Floating Point Support
10844 \begin_inset LatexCommand \index{Floating point support}
10851 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
10852 floating point support routines are derived from gcc's floatlib.c and consist
10853 of the following routines:
10861 \begin_inset Tabular
10862 <lyxtabular version="3" rows="17" columns="2">
10864 <column alignment="center" valignment="top" leftline="true" width="0">
10865 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
10866 <row topline="true" bottomline="true">
10867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10893 <row topline="true">
10894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10925 add floating point numbers
10929 <row topline="true">
10930 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10947 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10961 subtract floating point numbers
10965 <row topline="true">
10966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10983 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10997 divide floating point numbers
11001 <row topline="true">
11002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11033 multiply floating point numbers
11037 <row topline="true">
11038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11055 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11069 convert floating point to unsigned char
11073 <row topline="true">
11074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11091 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11105 convert floating point to signed char
11109 <row topline="true">
11110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11127 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11141 convert floating point to unsigned int
11145 <row topline="true">
11146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11177 convert floating point to signed int
11181 <row topline="true">
11182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11208 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11222 convert floating point to unsigned long
11226 <row topline="true">
11227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11258 convert floating point to signed long
11262 <row topline="true">
11263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11294 convert unsigned char to floating point
11298 <row topline="true">
11299 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11316 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11330 convert char to floating point number
11334 <row topline="true">
11335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11366 convert unsigned int to floating point
11370 <row topline="true">
11371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11388 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11402 convert int to floating point numbers
11406 <row topline="true">
11407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11438 convert unsigned long to floating point number
11442 <row topline="true" bottomline="true">
11443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11474 convert long to floating point number
11487 Note if all these routines are used simultaneously the data space might
11489 For serious floating point usage it is strongly recommended that the large
11491 Also notice that you don't have to call this routines directly.
11492 The compiler will use them automatically every time a floating point operation
11496 MCS51 Memory Models
11497 \begin_inset LatexCommand \index{Memory model}
11502 \begin_inset LatexCommand \index{MCS51 memory}
11509 SDCC allows two memory models for MCS51 code,
11518 Modules compiled with different memory models should
11522 be combined together or the results would be unpredictable.
11523 The library routines supplied with the compiler are compiled as both small
11525 The compiled library modules are contained in separate directories as small
11526 and large so that you can link to either set.
11530 When the large model is used all variables declared without a storage class
11531 will be allocated into the external ram, this includes all parameters and
11532 local variables (for non-reentrant
11533 \begin_inset LatexCommand \index{reentrant}
11538 When the small model is used variables without storage class are allocated
11539 in the internal ram.
11542 Judicious usage of the processor specific storage classes
11543 \begin_inset LatexCommand \index{Storage class}
11547 and the 'reentrant' function type will yield much more efficient code,
11548 than using the large model.
11549 Several optimizations are disabled when the program is compiled using the
11550 large model, it is therefore strongly recommended that the small model
11551 be used unless absolutely required.
11554 DS390 Memory Models
11555 \begin_inset LatexCommand \index{Memory model}
11560 \begin_inset LatexCommand \index{DS390 memory model}
11567 The only model supported is Flat 24
11568 \begin_inset LatexCommand \index{Flat 24 (memory model)}
11573 This generates code for the 24 bit contiguous addressing mode of the Dallas
11575 In this mode, up to four meg of external RAM or code space can be directly
11577 See the data sheets at www.dalsemi.com for further information on this part.
11581 Note that the compiler does not generate any code to place the processor
11582 into 24 bitmode (although
11586 in the ds390 libraries will do that for you).
11592 \begin_inset LatexCommand \index{Tinibios (DS390)}
11596 , the boot loader or similar code must ensure that the processor is in 24
11597 bit contiguous addressing mode before calling the SDCC startup code.
11615 option, variables will by default be placed into the XDATA segment.
11620 Segments may be placed anywhere in the 4 meg address space using the usual
11632 Note that if any segments are located above 64K, the -r flag must be passed
11633 to the linker to generate the proper segment relocations, and the Intel
11634 HEX output format must be used.
11635 The -r flag can be passed to the linker by using the option
11639 on the SDCC command line.
11640 However, currently the linker can not handle code segments > 64k.
11644 \begin_inset LatexCommand \index{Pragmas}
11651 SDCC supports the following #pragma directives.
11655 \begin_inset LatexCommand \index{\#pragma SAVE}
11659 - this will save all current options to the SAVE/RESTORE stack.
11664 \begin_inset LatexCommand \index{\#pragma RESTORE}
11668 - will restore saved options from the last save.
11669 SAVEs & RESTOREs can be nested.
11670 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
11671 RESTORE pulls current options from the stack.
11676 \begin_inset LatexCommand \index{\#pragma NOGCSE}
11680 - will stop global common subexpression elimination.
11684 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
11688 - will stop loop induction optimizations.
11692 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
11696 - will not generate code for boundary value checking, when switch statements
11697 are turned into jump-tables (dangerous).
11698 For more details see section
11699 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
11707 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
11711 - the compiler will not overlay the parameters and local variables of a
11716 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
11720 - the compiler will not warn you anymore for obvious mistakes, you'r on
11725 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
11729 - Will not do loop reversal optimization
11733 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11737 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
11739 \begin_inset LatexCommand \index{push/pop}
11743 instruction in ISR function (using interrupt
11744 \begin_inset LatexCommand \index{interrupt}
11749 The directive should be placed immediately before the ISR function definition
11750 and it affects ALL ISR functions following it.
11751 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
11752 EXCLUDE\SpecialChar ~
11754 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11762 \begin_inset LatexCommand \index{\#pragma NOIV}
11766 - Do not generate interrupt vector table entries for all ISR functions
11767 defined after the pragma.
11768 This is useful in cases where the interrupt vector table must be defined
11769 manually, or when there is a secondary, manually defined interrupt vector
11771 for the autovector feature of the Cypress EZ-USB FX2).
11772 More elegantly this can be achieved by obmitting the optional interrupt
11773 number after the interrupt keyword, see section
11774 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
11783 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11788 \begin_inset LatexCommand \index{function prologue}
11792 function1[,function2[,function3...]] - The compiler by default uses a caller
11793 saves convention for register saving across function calls, however this
11794 can cause unnecessary register pushing & popping when calling small functions
11795 from larger functions.
11796 This option can be used to switch off the register saving convention for
11797 the function names specified.
11798 The compiler will not save registers when calling these functions, extra
11799 code need to be manually inserted at the entry & exit for these functions
11800 to save & restore the registers used by these functions, this can SUBSTANTIALLY
11801 reduce code & improve run time performance of the generated code.
11802 In the future the compiler (with inter procedural analysis) may be able
11803 to determine the appropriate scheme to use for each function call.
11814 -callee-saves command line option is used, the function names specified
11815 in #pragma\SpecialChar ~
11817 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11821 is appended to the list of functions specified in the command line.
11824 The pragma's are intended to be used to turn-on or off certain optimizations
11825 which might cause the compiler to generate extra stack / data space to
11826 store compiler generated temporary variables.
11827 This usually happens in large functions.
11828 Pragma directives should be used as shown in the following example, they
11829 are used to control options & optimizations for a given function; pragmas
11830 should be placed before and/or after a function, placing pragma's inside
11831 a function body could have unpredictable results.
11837 \begin_inset LatexCommand \index{\#pragma SAVE}
11848 /* save the current settings */
11851 \begin_inset LatexCommand \index{\#pragma NOGCSE}
11860 /* turnoff global subexpression elimination */
11862 #pragma NOINDUCTION
11863 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
11867 /* turn off induction optimizations */
11890 \begin_inset LatexCommand \index{\#pragma RESTORE}
11894 /* turn the optimizations back on */
11897 The compiler will generate a warning message when extra space is allocated.
11898 It is strongly recommended that the SAVE and RESTORE pragma's be used when
11899 changing options for a function.
11902 Defines Created by the Compiler
11903 \begin_inset LatexCommand \index{Defines created by the compiler}
11910 The compiler creates the following #defines
11911 \begin_inset LatexCommand \index{\#defines}
11921 \begin_inset Tabular
11922 <lyxtabular version="3" rows="10" columns="2">
11924 <column alignment="center" valignment="top" leftline="true" width="0">
11925 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
11926 <row topline="true" bottomline="true">
11927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11937 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11948 <row topline="true">
11949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11955 \begin_inset LatexCommand \index{SDCC}
11962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11967 this Symbol is always defined
11971 <row topline="true">
11972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11978 \begin_inset LatexCommand \index{SDCC\_mcs51}
11983 \begin_inset LatexCommand \index{SDCC\_ds390}
11988 \begin_inset LatexCommand \index{SDCC\_z80}
11995 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12000 depending on the model used (e.g.: -mds390
12004 <row topline="true">
12005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12011 \begin_inset LatexCommand \index{\_\_mcs51}
12016 \begin_inset LatexCommand \index{\_\_ds390}
12021 \begin_inset LatexCommand \index{\_\_z80}
12028 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12033 depending on the model used (e.g.
12038 <row topline="true">
12039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12045 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
12052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12075 <row topline="true">
12076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12082 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
12089 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12112 <row topline="true">
12113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12119 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
12126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12149 <row topline="true">
12150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12156 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
12163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12186 <row topline="true">
12187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12193 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
12200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12213 <row topline="true" bottomline="true">
12214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12220 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
12227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12249 Debugging with SDCDB
12250 \begin_inset LatexCommand \index{sdcdb}
12257 SDCC is distributed with a source level debugger
12258 \begin_inset LatexCommand \index{Debugger}
12263 The debugger uses a command line interface, the command repertoire of the
12264 debugger has been kept as close to gdb
12265 \begin_inset LatexCommand \index{gdb}
12269 (the GNU debugger) as possible.
12270 The configuration and build process is part of the standard compiler installati
12271 on, which also builds and installs the debugger in the target directory
12272 specified during configuration.
12273 The debugger allows you debug BOTH at the C source and at the ASM source
12275 Sdcdb is available on Unix platforms only.
12278 Compiling for Debugging
12281 The \SpecialChar \-
12283 debug option must be specified for all files for which debug information
12284 is to be generated.
12285 The complier generates a .adb file for each of these files.
12286 The linker creates the .cdb file from the .adb files and the address information.
12287 This .cdb is used by the debugger.
12290 How the Debugger Works
12303 -debug option is specified the compiler generates extra symbol information
12304 some of which are put into the assembler source and some are put into the
12306 Then the linker creates the .cdb file from the individual .adb files with
12307 the address information for the symbols.
12308 The debugger reads the symbolic information generated by the compiler &
12309 the address information generated by the linker.
12310 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
12311 execution is controlled by the debugger.
12312 When a command is issued for the debugger, it translates it into appropriate
12313 commands for the simulator.
12316 Starting the Debugger
12319 The debugger can be started using the following command line.
12320 (Assume the file you are debugging has the file name foo).
12334 The debugger will look for the following files.
12337 foo.c - the source file.
12340 foo.cdb - the debugger symbol information file.
12343 foo.ihx - the Intel hex format
12344 \begin_inset LatexCommand \index{Intel hex format}
12351 Command Line Options.
12364 -directory=<source file directory> this option can used to specify the directory
12366 The debugger will look into the directory list specified for source, cdb
12368 The items in the directory list must be separated by ':', e.g.
12369 if the source files can be in the directories /home/src1 and /home/src2,
12380 -directory option should be -
12390 -directory=/home/src1:/home/src2.
12391 Note there can be no spaces in the option.
12395 -cd <directory> - change to the <directory>.
12398 -fullname - used by GUI front ends.
12401 -cpu <cpu-type> - this argument is passed to the simulator please see the
12402 simulator docs for details.
12405 -X <Clock frequency > this options is passed to the simulator please see
12406 the simulator docs for details.
12409 -s <serial port file> passed to simulator see the simulator docs for details.
12412 -S <serial in,out> passed to simulator see the simulator docs for details.
12418 As mentioned earlier the command interface for the debugger has been deliberatel
12419 y kept as close the GNU debugger gdb, as possible.
12420 This will help the integration with existing graphical user interfaces
12421 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
12422 If you use a graphical user interface for the debugger you can skip the
12424 \layout Subsubsection*
12426 break [line | file:line | function | file:function]
12429 Set breakpoint at specified line or function:
12438 sdcdb>break foo.c:100
12440 sdcdb>break funcfoo
12442 sdcdb>break foo.c:funcfoo
12443 \layout Subsubsection*
12445 clear [line | file:line | function | file:function ]
12448 Clear breakpoint at specified line or function:
12457 sdcdb>clear foo.c:100
12459 sdcdb>clear funcfoo
12461 sdcdb>clear foo.c:funcfoo
12462 \layout Subsubsection*
12467 Continue program being debugged, after breakpoint.
12468 \layout Subsubsection*
12473 Execute till the end of the current function.
12474 \layout Subsubsection*
12479 Delete breakpoint number 'n'.
12480 If used without any option clear ALL user defined break points.
12481 \layout Subsubsection*
12483 info [break | stack | frame | registers ]
12486 info break - list all breakpoints
12489 info stack - show the function call stack.
12492 info frame - show information about the current execution frame.
12495 info registers - show content of all registers.
12496 \layout Subsubsection*
12501 Step program until it reaches a different source line.
12502 \layout Subsubsection*
12507 Step program, proceeding through subroutine calls.
12508 \layout Subsubsection*
12513 Start debugged program.
12514 \layout Subsubsection*
12519 Print type information of the variable.
12520 \layout Subsubsection*
12525 print value of variable.
12526 \layout Subsubsection*
12531 load the given file name.
12532 Note this is an alternate method of loading file for debugging.
12533 \layout Subsubsection*
12538 print information about current frame.
12539 \layout Subsubsection*
12544 Toggle between C source & assembly source.
12545 \layout Subsubsection*
12547 ! simulator command
12550 Send the string following '!' to the simulator, the simulator response is
12552 Note the debugger does not interpret the command being sent to the simulator,
12553 so if a command like 'go' is sent the debugger can loose its execution
12554 context and may display incorrect values.
12555 \layout Subsubsection*
12562 My name is Bobby Brown"
12565 Interfacing with XEmacs
12566 \begin_inset LatexCommand \index{XEmacs}
12571 \begin_inset LatexCommand \index{Emacs}
12578 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
12579 sdcdb.el and sdcdbsrc.el.
12580 These two files can be found in the $(prefix)/bin directory after the installat
12582 These files need to be loaded into XEmacs for the interface to work.
12583 This can be done at XEmacs startup time by inserting the following into
12584 your '.xemacs' file (which can be found in your HOME directory):
12590 (load-file sdcdbsrc.el)
12596 .xemacs is a lisp file so the () around the command is REQUIRED.
12597 The files can also be loaded dynamically while XEmacs is running, set the
12598 environment variable 'EMACSLOADPATH' to the installation bin directory
12599 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
12600 To start the interface enter the following command:
12614 You will prompted to enter the file name to be debugged.
12619 The command line options that are passed to the simulator directly are bound
12620 to default values in the file sdcdbsrc.el.
12621 The variables are listed below, these values maybe changed as required.
12624 sdcdbsrc-cpu-type '51
12627 sdcdbsrc-frequency '11059200
12630 sdcdbsrc-serial nil
12633 The following is a list of key mapping for the debugger interface.
12641 ;; Current Listing ::
12643 ;;key\SpecialChar ~
12658 binding\SpecialChar ~
12682 ;;---\SpecialChar ~
12697 ------\SpecialChar ~
12737 sdcdb-next-from-src\SpecialChar ~
12763 sdcdb-back-from-src\SpecialChar ~
12789 sdcdb-cont-from-src\SpecialChar ~
12799 SDCDB continue command
12815 sdcdb-step-from-src\SpecialChar ~
12841 sdcdb-whatis-c-sexp\SpecialChar ~
12851 SDCDB ptypecommand for data at
12915 sdcdbsrc-delete\SpecialChar ~
12929 SDCDB Delete all breakpoints if no arg
12977 given or delete arg (C-u arg x)
12993 sdcdbsrc-frame\SpecialChar ~
13008 SDCDB Display current frame if no arg,
13057 given or display frame arg
13122 sdcdbsrc-goto-sdcdb\SpecialChar ~
13132 Goto the SDCDB output buffer
13148 sdcdb-print-c-sexp\SpecialChar ~
13159 SDCDB print command for data at
13223 sdcdbsrc-goto-sdcdb\SpecialChar ~
13233 Goto the SDCDB output buffer
13249 sdcdbsrc-mode\SpecialChar ~
13265 Toggles Sdcdbsrc mode (turns it off)
13269 ;; C-c C-f\SpecialChar ~
13277 sdcdb-finish-from-src\SpecialChar ~
13285 SDCDB finish command
13289 ;; C-x SPC\SpecialChar ~
13297 sdcdb-break\SpecialChar ~
13315 Set break for line with point
13317 ;; ESC t\SpecialChar ~
13327 sdcdbsrc-mode\SpecialChar ~
13343 Toggle Sdcdbsrc mode
13345 ;; ESC m\SpecialChar ~
13355 sdcdbsrc-srcmode\SpecialChar ~
13377 Here are a few guidelines that will help the compiler generate more efficient
13378 code, some of the tips are specific to this compiler others are generally
13379 good programming practice.
13382 Use the smallest data type to represent your data-value.
13383 If it is known in advance that the value is going to be less than 256 then
13384 use an 'unsigned char' instead of a 'short' or 'int'.
13387 Use unsigned when it is known in advance that the value is not going to
13389 This helps especially if you are doing division or multiplication.
13392 NEVER jump into a LOOP.
13395 Declare the variables to be local whenever possible, especially loop control
13396 variables (induction).
13399 Since the compiler does not always do implicit integral promotion, the programme
13400 r should do an explicit cast when integral promotion is required.
13403 Reducing the size of division, multiplication & modulus operations can reduce
13404 code size substantially.
13405 Take the following code for example.
13411 foobar(unsigned int p1, unsigned char ch)
13419 unsigned char ch1 = p1 % ch ;
13430 For the modulus operation the variable ch will be promoted to unsigned int
13431 first then the modulus operation will be performed (this will lead to a
13432 call to support routine _moduint()), and the result will be casted to a
13434 If the code is changed to
13439 foobar(unsigned int p1, unsigned char ch)
13447 unsigned char ch1 = (unsigned char)p1 % ch ;
13458 It would substantially reduce the code generated (future versions of the
13459 compiler will be smart enough to detect such optimization opportunities).
13463 Have a look at the assembly listing to get a
13464 \begin_inset Quotes sld
13468 \begin_inset Quotes srd
13471 for the code generation.
13474 Notes on MCS51 memory
13475 \begin_inset LatexCommand \index{MCS51 memory}
13482 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
13483 RAM memory which is structured as follows
13487 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
13490 - Bytes 20-2F - 16 bytes to hold 128 bit
13491 \begin_inset LatexCommand \index{bit}
13497 - Bytes 30-7F - 80 bytes for general purpose use.
13502 Additionally some members of the MCS51 family may have up to 128 bytes of
13503 additional, indirectly addressable, internal RAM memory (
13508 \begin_inset LatexCommand \index{idata}
13513 Furthermore, some chips may have some built in external memory (
13518 \begin_inset LatexCommand \index{xdata}
13522 ) which should not be confused with the internal, directly addressable RAM
13528 \begin_inset LatexCommand \index{data}
13533 Sometimes this built in
13537 memory has to be activated before using it (you can probably find this
13538 information on the datasheet of the microcontroller your are using).
13541 Normally SDCC will only use the first bank
13542 \begin_inset LatexCommand \index{bank}
13546 of registers (register bank 0), but it is possible to specify that other
13547 banks of registers should be used in interrupt
13548 \begin_inset LatexCommand \index{interrupt}
13553 By default, the compiler will place the stack after the last byte of allocated
13554 memory for variables.
13555 For example, if the first 2 banks of registers are used, and only four
13560 variables, it will position the base of the internal stack at address 20
13562 This implies that as the stack
13563 \begin_inset LatexCommand \index{stack}
13567 grows, it will use up the remaining register banks, and the 16 bytes used
13568 by the 128 bit variables, and 80 bytes for general purpose use.
13569 If any bit variables are used, the data variables will be placed after
13570 the byte holding the last bit variable.
13571 For example, if register banks 0 and 1 are used, and there are 9 bit variables
13576 variables will be placed starting at address 0x22.
13588 \begin_inset LatexCommand \index{-\/-data-loc}
13592 to specify the start address of the
13606 -iram-size to specify the size of the total internal RAM (
13618 By default the 8051 linker will place the stack after the last byte of data
13631 \begin_inset LatexCommand \index{-\/-stack-loc}
13635 allows you to specify the start of the stack, i.e.
13636 you could start it after any data in the general purpose area.
13637 If your microcontroller has additional indirectly addressable internal
13642 ) you can place the stack on it.
13643 You may also need to use -
13654 \begin_inset LatexCommand \index{-\/-data-loc}
13658 to set the start address of the external RAM (
13673 \begin_inset LatexCommand \index{-\/-data-loc}
13677 to specify its size.
13678 Same goes for the code memory, using -
13689 \begin_inset LatexCommand \index{-\/-data-loc}
13704 \begin_inset LatexCommand \index{-\/-data-loc}
13709 If in doubt, don't specify any options and see if the resulting memory
13710 layout is appropriate, then you can adjust it.
13713 The 8051 linker generates two files with memory allocation information.
13714 The first, with extension .map shows all the variables and segments.
13715 The second with extension .mem shows the final memory layout.
13716 The linker will complaint either if memory segments overlap, there is not
13717 enough memory, or there is not enough space for stack.
13718 If you get any linking warnings and/or errors related to stack or segments
13719 allocation, take a look at either the .map or .mem files to find out what
13721 The .mem file may even suggest a solution to the problem.
13725 \begin_inset LatexCommand \index{Tools}
13729 included in the distribution
13733 \begin_inset Tabular
13734 <lyxtabular version="3" rows="12" columns="3">
13736 <column alignment="center" valignment="top" leftline="true" width="0pt">
13737 <column alignment="center" valignment="top" leftline="true" width="0pt">
13738 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
13739 <row topline="true" bottomline="true">
13740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13748 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13765 <row topline="true">
13766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13774 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13779 Simulator for various architectures
13782 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13791 <row topline="true">
13792 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13805 header file conversion
13808 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13813 sdcc/support/scripts
13817 <row topline="true">
13818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13831 header file conversion
13834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13839 sdcc/support/scripts
13843 <row topline="true">
13844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13860 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13878 <row topline="true">
13879 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13895 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13913 <row topline="true">
13914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13948 <row topline="true">
13949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13965 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13983 <row topline="true">
13984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14018 <row topline="true">
14019 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14035 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14053 <row topline="true">
14054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14088 <row topline="true" bottomline="true">
14089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14132 Related open source tools
14133 \begin_inset LatexCommand \index{Related tools}
14141 \begin_inset Tabular
14142 <lyxtabular version="3" rows="8" columns="3">
14144 <column alignment="center" valignment="top" leftline="true" width="0pt">
14145 <column alignment="block" valignment="top" leftline="true" width="30line%">
14146 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
14147 <row topline="true" bottomline="true">
14148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14164 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14173 <row topline="true">
14174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14180 \begin_inset LatexCommand \index{gpsim}
14187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14195 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14201 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
14209 <row topline="true">
14210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14226 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14232 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
14240 <row topline="true">
14241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14247 \begin_inset LatexCommand \index{srecord}
14254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14259 Object file conversion, checksumming, ...
14262 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14268 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
14276 <row topline="true">
14277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14283 \begin_inset LatexCommand \index{objdump}
14290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14295 Object file conversion, ...
14298 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14303 Part of binutils (should be there anyway)
14307 <row topline="true">
14308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14314 \begin_inset LatexCommand \index{doxygen}
14321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14326 Source code documentation system
14329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14335 \begin_inset LatexCommand \url{http://www.doxygen.org}
14343 <row topline="true">
14344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14350 \begin_inset LatexCommand \index{splint}
14357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14362 Statically checks c sources
14365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14371 \begin_inset LatexCommand \url{http://www.splint.org}
14379 <row topline="true" bottomline="true">
14380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14386 \begin_inset LatexCommand \index{ddd}
14393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14398 Debugger, serves nicely as GUI to sdcdb
14399 \begin_inset LatexCommand \index{sdcdb}
14406 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14412 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
14429 Related documentation / recommended reading
14433 \begin_inset Tabular
14434 <lyxtabular version="3" rows="5" columns="3">
14436 <column alignment="center" valignment="top" leftline="true" width="0pt">
14437 <column alignment="block" valignment="top" leftline="true" width="30line%">
14438 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
14439 <row topline="true" bottomline="true">
14440 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14465 <row topline="true">
14466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14476 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14481 Advanced Compiler Design and Implementation
14484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14493 <row topline="true">
14494 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14517 \begin_inset LatexCommand \index{C Reference card}
14524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14530 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
14538 <row topline="true">
14539 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14544 test_suite_spec.pdf
14547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14552 sdcc regression test
14553 \begin_inset LatexCommand \index{Regression test}
14560 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14569 <row topline="true" bottomline="true">
14570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14601 sdcc internal documentation
14604 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14623 \begin_inset LatexCommand \index{Support}
14630 SDCC has grown to be a large project.
14631 The compiler alone (without the preprocessor, assembler and linker) is
14632 well over 100,000 lines of code (blank stripped).
14633 The open source nature of this project is a key to its continued growth
14635 You gain the benefit and support of many active software developers and
14637 Is SDCC perfect? No, that's why we need your help.
14638 The developers take pride in fixing reported bugs.
14639 You can help by reporting the bugs and helping other SDCC users.
14640 There are lots of ways to contribute, and we encourage you to take part
14641 in making SDCC a great software package.
14645 The SDCC project is hosted on the SDCC sourceforge site at
14646 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
14651 You'll find the complete set of mailing lists
14652 \begin_inset LatexCommand \index{Mailing list}
14656 , forums, bug reporting system, patch submission
14657 \begin_inset LatexCommand \index{Patch submission}
14662 \begin_inset LatexCommand \index{download}
14666 area and cvs code repository
14667 \begin_inset LatexCommand \index{cvs code repository}
14675 \begin_inset LatexCommand \index{Bugs}
14680 \begin_inset LatexCommand \index{Reporting bugs}
14687 The recommended way of reporting bugs is using the infrastructure of the
14689 You can follow the status of bug reports there and have an overview about
14693 Bug reports are automatically forwarded to the developer mailing list and
14694 will be fixed ASAP.
14695 When reporting a bug, it is very useful to include a small test program
14696 (the smaller the better) which reproduces the problem.
14697 If you can isolate the problem by looking at the generated assembly code,
14698 this can be very helpful.
14699 Compiling your program with the -
14710 \begin_inset LatexCommand \index{-\/-dumpall}
14714 option can sometimes be useful in locating optimization problems.
14715 When reporting a bug please maker sure you:
14718 Attach the code you are compiling with SDCC.
14722 Specify the exact command you use to run SDCC, or attach your Makefile.
14726 Specify the SDCC version (type "sdcc -v"), your platform, and operating
14731 Provide an exact copy of any error message or incorrect output.
14735 Put something meaningful in the subject of your message.
14738 Please attempt to include these 5 important parts, as applicable, in all
14739 requests for support or when reporting any problems or bugs with SDCC.
14740 Though this will make your message lengthy, it will greatly improve your
14741 chance that SDCC users and developers will be able to help you.
14742 Some SDCC developers are frustrated by bug reports without code provided
14743 that they can use to reproduce and ultimately fix the problem, so please
14744 be sure to provide sample code if you are reporting a bug!
14747 Please have a short check that you are using a recent version of SDCC and
14748 the bug is not yet known.
14749 This is the link for reporting bugs:
14750 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
14757 Requesting Features
14758 \begin_inset LatexCommand \label{sub:Requesting-Features}
14763 \begin_inset LatexCommand \index{Feature request}
14768 \begin_inset LatexCommand \index{Requesting features}
14775 Like bug reports feature requests are forwarded to the developer mailing
14777 This is the link for requesting features:
14778 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
14788 These links should take you directly to the
14789 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
14799 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
14800 automated messages (mid 2003)
14804 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
14808 , lists and forums are archived so if you are lucky someone already had
14813 \begin_inset LatexCommand \index{Changelog}
14820 You can follow the status of the cvs version
14821 \begin_inset LatexCommand \index{version}
14825 of SDCC by watching the file
14826 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
14830 in the cvs-repository.
14834 \begin_inset LatexCommand \index{Release policy}
14841 Historically there often were long delays between official releases and
14842 the sourceforge download area tends to get not updated at all.
14843 Current excuses might refer to problems with live range analysis, but if
14844 this is fixed, the next problem rising is that another excuse will have
14846 Kidding aside, we have to get better there! On the other hand there are
14847 daily snapshots available at
14848 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
14852 , and you can always built the very last version (hopefully with many bugs
14853 fixed, and features added) from the source code available at
14854 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
14862 \begin_inset LatexCommand \index{Examples}
14869 You'll find some small examples in the directory
14871 sdcc/device/examples/.
14874 More examples and libraries are available at
14876 The SDCC Open Knowledge Resource
14877 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
14884 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
14891 I did insert a reference to Paul's web site here although it seems rather
14892 dedicated to a specific 8032 board (I think it's okay because it f.e.
14893 shows LCD/Harddisc interface and has a free 8051 monitor.
14894 Independent 8032 board vendors face hard competition of heavily subsidized
14895 development boards anyway).
14898 Maybe we should include some links to real world applications.
14899 Preferably pointer to pointers (one for each architecture) so this stays
14904 \begin_inset LatexCommand \index{Quality control}
14911 The compiler is passed through nightly compile and build checks.
14917 \begin_inset LatexCommand \index{Regression test}
14921 check that SDCC itself compiles flawlessly on several platforms and checks
14922 the quality of the code generated by SDCC by running the code through simulator
14924 There is a separate document
14927 \begin_inset LatexCommand \index{Test suite}
14936 You'll find the test code in the directory
14938 sdcc/support/regression
14941 You can run these tests manually by running
14945 in this directory (or f.e.
14950 if you don't want to run the complete tests).
14951 The test code might also be interesting if you want to look for examples
14952 \begin_inset LatexCommand \index{Examples}
14956 checking corner cases of SDCC or if you plan to submit patches
14957 \begin_inset LatexCommand \index{Patch submission}
14964 The pic port uses a different set of regression tests, you'll find them
14967 sdcc/src/regression
14972 SDCC Technical Data
14976 \begin_inset LatexCommand \index{Optimizations}
14983 SDCC performs a host of standard optimizations in addition to some MCU specific
14986 \layout Subsubsection
14988 Sub-expression Elimination
14989 \begin_inset LatexCommand \index{Subexpression elimination}
14996 The compiler does local and global common subexpression elimination, e.g.:
15007 will be translated to
15019 Some subexpressions are not as obvious as the above example, e.g.:
15029 In this case the address arithmetic a->b[i] will be computed only once;
15030 the equivalent code in C would be.
15042 The compiler will try to keep these temporary variables in registers.
15043 \layout Subsubsection
15045 Dead-Code Elimination
15046 \begin_inset LatexCommand \index{Dead-code elimination}
15067 i = 1; \SpecialChar ~
15076 global = 1;\SpecialChar ~
15089 global = 3;\SpecialChar ~
15115 \layout Subsubsection
15118 \begin_inset LatexCommand \index{Copy propagation}
15174 Note: the dead stores created by this copy propagation will be eliminated
15175 by dead-code elimination.
15176 \layout Subsubsection
15179 \begin_inset LatexCommand \index{Loop optimization}
15186 Two types of loop optimizations are done by SDCC loop invariant lifting
15187 and strength reduction of loop induction variables.
15188 In addition to the strength reduction the optimizer marks the induction
15189 variables and the register allocator tries to keep the induction variables
15190 in registers for the duration of the loop.
15191 Because of this preference of the register allocator
15192 \begin_inset LatexCommand \index{Register allocation}
15196 , loop induction optimization causes an increase in register pressure, which
15197 may cause unwanted spilling of other temporary variables into the stack
15198 \begin_inset LatexCommand \index{stack}
15203 The compiler will generate a warning message when it is forced to allocate
15204 extra space either on the stack or data space.
15205 If this extra space allocation is undesirable then induction optimization
15206 can be eliminated either for the entire source file (with -
15216 -noinduction option) or for a given function only using #pragma\SpecialChar ~
15218 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
15231 for (i = 0 ; i < 100 ; i ++)
15247 for (i = 0; i < 100; i++)
15256 As mentioned previously some loop invariants are not as apparent, all static
15257 address computations are also moved out of the loop.
15262 \begin_inset LatexCommand \index{Strength reduction}
15266 , this optimization substitutes an expression by a cheaper expression:
15271 for (i=0;i < 100; i++)
15289 for (i=0;i< 100;i++) {
15295 ar[itemp1] = itemp2;
15312 The more expensive multiplication
15313 \begin_inset LatexCommand \index{Multiplication}
15317 is changed to a less expensive addition.
15318 \layout Subsubsection
15321 \begin_inset LatexCommand \index{Loop reversing}
15328 This optimization is done to reduce the overhead of checking loop boundaries
15329 for every iteration.
15330 Some simple loops can be reversed and implemented using a
15331 \begin_inset Quotes eld
15334 decrement and jump if not zero
15335 \begin_inset Quotes erd
15339 SDCC checks for the following criterion to determine if a loop is reversible
15340 (note: more sophisticated compilers use data-dependency analysis to make
15341 this determination, SDCC uses a more simple minded analysis).
15344 The 'for' loop is of the form
15350 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
15360 The <for body> does not contain
15361 \begin_inset Quotes eld
15365 \begin_inset Quotes erd
15369 \begin_inset Quotes erd
15375 All goto's are contained within the loop.
15378 No function calls within the loop.
15381 The loop control variable <sym> is not assigned any value within the loop
15384 The loop control variable does NOT participate in any arithmetic operation
15388 There are NO switch statements in the loop.
15389 \layout Subsubsection
15391 Algebraic Simplifications
15394 SDCC does numerous algebraic simplifications, the following is a small sub-set
15395 of these optimizations.
15400 i = j + 0 ; /* changed to */ i = j;
15402 i /= 2;\SpecialChar ~
15406 /* changed to */ i >>= 1;
15408 i = j - j ; /* changed to */ i = 0;
15410 i = j / 1 ; /* changed to */ i = j;
15413 Note the subexpressions
15414 \begin_inset LatexCommand \index{Subexpression}
15418 given above are generally introduced by macro expansions or as a result
15419 of copy/constant propagation.
15420 \layout Subsubsection
15422 'switch' Statements
15423 \begin_inset LatexCommand \label{sub:'switch'-Statements}
15428 \begin_inset LatexCommand \index{switch statement}
15435 SDCC changes switch statements to jump tables
15436 \begin_inset LatexCommand \index{jump tables}
15440 when the following conditions are true.
15444 The case labels are in numerical sequence, the labels need not be in order,
15445 and the starting number need not be one or zero.
15451 switch(i) {\SpecialChar ~
15482 case 4: ...\SpecialChar ~
15514 case 5: ...\SpecialChar ~
15546 case 3: ...\SpecialChar ~
15578 case 6: ...\SpecialChar ~
15646 Both the above switch statements will be implemented using a jump-table.
15647 The example to the right side is slightly more efficient as the check for
15648 the lower boundary of the jump-table is not needed.
15652 The number of case labels is at least three, since it takes two conditional
15653 statements to handle the boundary conditions.
15656 The number of case labels is less than 84, since each label takes 3 bytes
15657 and a jump-table can be utmost 256 bytes long.
15660 Switch statements which have gaps in the numeric sequence or those that
15661 have more that 84 case labels can be split into more than one switch statement
15662 for efficient code generation, e.g.:
15712 If the above switch statement is broken down into two switch statements
15751 case 9:\SpecialChar ~
15758 case 10:\SpecialChar ~
15764 case 11:\SpecialChar ~
15770 case 12:\SpecialChar ~
15777 then both the switch statements will be implemented using jump-tables whereas
15778 the unmodified switch statement will not be.
15779 You might also consider dummy cases 0 and 5 to 8 in this example.
15782 The pragma NOJTBOUND
15783 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
15787 can be used to turn off checking the
15800 It has no effect if a default label is supplied.
15801 Use of this pragma is dangerous: if the switch argument is not matched
15802 by a case statement the processor will happily jump into Nirvana.
15803 \layout Subsubsection
15805 Bit-shifting Operations
15806 \begin_inset LatexCommand \index{Bit shifting}
15813 Bit shifting is one of the most frequently used operation in embedded programmin
15815 SDCC tries to implement bit-shift operations in the most efficient way
15831 generates the following code:
15848 In general SDCC will never setup a loop if the shift count is known.
15890 Note that SDCC stores numbers in little-endian
15896 Usually 8-bit processors don't care much about endianness.
15897 This is not the case for the standard 8051 which only has an instruction
15903 \begin_inset LatexCommand \index{DPTR}
15911 so little-endian is the more efficient byte order.
15915 \begin_inset LatexCommand \index{little-endian}
15920 \begin_inset LatexCommand \index{Endianness}
15925 lowest order first).
15926 \layout Subsubsection
15929 \begin_inset LatexCommand \index{Bit rotation}
15936 A special case of the bit-shift operation is bit rotation, SDCC recognizes
15937 the following expression to be a left bit-rotation:
15947 i = ((i << 1) | (i >> 7));
15956 will generate the following code:
15975 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
15976 ns of this case will also be recognized as bit-rotation, i.e.:
15981 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
15982 \layout Subsubsection
15985 \begin_inset LatexCommand \index{Highest Order Bit}
15992 It is frequently required to obtain the highest order bit of an integral
15993 type (long, int, short or char types).
15994 SDCC recognizes the following expression to yield the highest order bit
15995 and generates optimized code for it, e.g.:
16017 hob = (gint >> 15) & 1;
16027 will generate the following code:
16060 000A E5*01\SpecialChar ~
16087 000C 23\SpecialChar ~
16118 000D 54 01\SpecialChar ~
16145 000F F5*02\SpecialChar ~
16173 Variations of this case however will
16178 It is a standard C expression, so I heartily recommend this be the only
16179 way to get the highest order bit, (it is portable).
16180 Of course it will be recognized even if it is embedded in other expressions,
16186 xyz = gint + ((gint >> 15) & 1);
16189 will still be recognized.
16190 \layout Subsubsection
16193 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
16198 \begin_inset LatexCommand \index{Peephole optimizer}
16205 The compiler uses a rule based, pattern matching and re-writing mechanism
16206 for peep-hole optimization.
16211 a peep-hole optimizer by Christopher W.
16212 Fraser (cwfraser@microsoft.com).
16213 A default set of rules are compiled into the compiler, additional rules
16214 may be added with the
16227 \begin_inset LatexCommand \index{-\/-peep-file}
16234 The rule language is best illustrated with examples.
16258 The above rule will change the following assembly
16259 \begin_inset LatexCommand \index{Assembler routines}
16281 Note: All occurrences of a
16285 (pattern variable) must denote the same string.
16286 With the above rule, the assembly sequence:
16296 will remain unmodified.
16300 Other special case optimizations may be added by the user (via
16316 some variants of the 8051 MCU allow only
16325 The following two rules will change all
16344 replace { lcall %1 } by { acall %1 }
16346 replace { ljmp %1 } by { ajmp %1 }
16351 inline-assembler code
16353 is also passed through the peep hole optimizer, thus the peephole optimizer
16354 can also be used as an assembly level macro expander.
16355 The rules themselves are MCU dependent whereas the rule language infra-structur
16356 e is MCU independent.
16357 Peephole optimization rules for other MCU can be easily programmed using
16362 The syntax for a rule is as follows:
16367 rule := replace [ restart ] '{' <assembly sequence> '
16405 <assembly sequence> '
16423 '}' [if <functionName> ] '
16428 <assembly sequence> := assembly instruction (each instruction including
16429 labels must be on a separate line).
16433 The optimizer will apply to the rules one by one from the top in the sequence
16434 of their appearance, it will terminate when all rules are exhausted.
16435 If the 'restart' option is specified, then the optimizer will start matching
16436 the rules again from the top, this option for a rule is expensive (performance)
16437 , it is intended to be used in situations where a transformation will trigger
16438 the same rule again.
16439 An example of this (not a good one, it has side effects) is the following
16462 Note that the replace pattern cannot be a blank, but can be a comment line.
16463 Without the 'restart' option only the inner most 'pop' 'push' pair would
16464 be eliminated, i.e.:
16494 the restart option the rule will be applied again to the resulting code
16495 and then all the pop-push pairs will be eliminated to yield:
16505 A conditional function can be attached to a rule.
16506 Attaching rules are somewhat more involved, let me illustrate this with
16533 The optimizer does a look-up of a function name table defined in function
16538 in the source file SDCCpeeph.c, with the name
16543 If it finds a corresponding entry the function is called.
16544 Note there can be no parameters specified for these functions, in this
16549 is crucial, since the function
16553 expects to find the label in that particular variable (the hash table containin
16554 g the variable bindings is passed as a parameter).
16555 If you want to code more such functions, take a close look at the function
16556 labelInRange and the calling mechanism in source file SDCCpeeph.c.
16557 Currently implemented are
16559 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
16560 24bitMode, portIsDS390, 24bitModeAndPortDS390
16569 I know this whole thing is a little kludgey, but maybe some day we will
16570 have some better means.
16571 If you are looking at this file, you will see the default rules that are
16572 compiled into the compiler, you can add your own rules in the default set
16573 there if you get tired of specifying the -
16593 <pending: this is messy and incomplete>
16598 Compiler support routines (_gptrget, _mulint etc)
16601 Stdclib functions (puts, printf, strcat etc)
16604 Math functions (sin, pow, sqrt etc)
16608 \begin_inset LatexCommand \index{Libraries}
16612 included in SDCC should have a license at least as liberal as the GNU Lesser
16613 General Public License
16614 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
16625 license statements for the libraries are missing.
16626 sdcc/device/lib/ser_ir.c
16630 come with a GPL (as opposed to LGPL) License - this will not be liberal
16631 enough for many embedded programmers.
16634 If you have ported some library or want to share experience about some code
16636 falls into any of these categories Busses (I
16637 \begin_inset Formula $^{\textrm{2}}$
16640 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
16641 cards, eeprom, flash...), En-/Decryption, remote debugging, Keyboard, LCD,
16642 RTC, FPGA, PID then the sdcc-user mailing list
16643 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
16648 would certainly like to hear about it.
16649 Programmers coding for embedded systems are not especially famous for being
16650 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
16651 e these references are very valuable.
16655 \begin_inset LatexCommand \label{sub:External-Stack}
16660 \begin_inset LatexCommand \index{stack}
16665 \begin_inset LatexCommand \index{External stack}
16672 The external stack (-
16683 \begin_inset LatexCommand \index{-\/-xstack}
16687 ) is located at the start of the external ram segment, and is 256 bytes
16699 -xstack option is used to compile the program, the parameters and local
16700 variables of all reentrant functions are allocated in this area.
16701 This option is provided for programs with large stack space requirements.
16702 When used with the -
16713 \begin_inset LatexCommand \index{-\/-stack-auto}
16717 option, all parameters and local variables are allocated on the external
16718 stack (note support libraries will need to be recompiled with the same
16722 The compiler outputs the higher order address byte of the external ram segment
16723 into PORT P2, therefore when using the External Stack option, this port
16724 MAY NOT be used by the application program.
16728 \begin_inset LatexCommand \index{ANSI-compliance}
16733 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
16740 Deviations from the compliance:
16743 functions are not always reentrant.
16746 structures cannot be assigned values directly, cannot be passed as function
16747 parameters or assigned to each other and cannot be a return value from
16774 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
16785 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
16807 return rets;/* is invalid in SDCC although allowed in ANSI */
16814 \begin_inset LatexCommand \index{long long (not supported)}
16819 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
16827 \begin_inset LatexCommand \index{double (not supported)}
16831 ' precision floating point
16832 \begin_inset LatexCommand \index{Floating point support}
16839 No support for setjmp and longjmp (for now).
16843 \begin_inset LatexCommand \index{K\&R style}
16847 function declarations are NOT allowed.
16853 foo(i,j) /* this old style of function declarations */
16855 int i,j; /* are valid in ANSI but not valid in SDCC */
16870 functions declared as pointers
16871 \begin_inset LatexCommand \index{Pointers}
16876 \begin_inset LatexCommand \index{function pointers}
16880 must be dereferenced during the call.
16891 /* has to be called like this */
16893 (*foo)(); /* ANSI standard allows calls to be made like 'foo()' */
16897 Cyclomatic Complexity
16898 \begin_inset LatexCommand \index{Cyclomatic complexity}
16905 Cyclomatic complexity of a function is defined as the number of independent
16906 paths the program can take during execution of the function.
16907 This is an important number since it defines the number test cases you
16908 have to generate to validate the function.
16909 The accepted industry standard for complexity number is 10, if the cyclomatic
16910 complexity reported by SDCC exceeds 10 you should think about simplification
16911 of the function logic.
16912 Note that the complexity level is not related to the number of lines of
16913 code in a function.
16914 Large functions can have low complexity, and small functions can have large
16920 SDCC uses the following formula to compute the complexity:
16925 complexity = (number of edges in control flow graph) - (number of nodes
16926 in control flow graph) + 2;
16930 Having said that the industry standard is 10, you should be aware that in
16931 some cases it be may unavoidable to have a complexity level of less than
16933 For example if you have switch statement with more than 10 case labels,
16934 each case label adds one to the complexity level.
16935 The complexity level is by no means an absolute measure of the algorithmic
16936 complexity of the function, it does however provide a good starting point
16937 for which functions you might look at for further optimization.
16941 \layout Subsubsection
16944 \begin_inset LatexCommand \label{sub:MCS51-variants}
16949 \begin_inset LatexCommand \index{MCS51 variants}
16956 MCS51 processors are available from many vendors and come in many different
16958 While they might differ considerably in respect to Special Function Registers
16959 the core MCS51 is usually not modified or is kept compatible.
16961 \layout Subsubsection*
16963 pdata access by SFR
16966 With the upcome of devices with internal xdata and flash memory devices
16967 using port P2 as dedicated I/O port is becoming more popular.
16968 Switching the high byte for pdata
16969 \begin_inset LatexCommand \index{pdata}
16973 access which was formerly done by port P2 is then achieved by a Special
16975 In well-established MCS51 tradition the address of this
16979 is where the chip designers decided to put it.
16980 As pdata addressing is used in the startup code for the initialization
16981 of xdata variables a separate startup code should be used as described
16983 \begin_inset LatexCommand \ref{sub:Startup-Code}
16988 \layout Subsubsection*
16990 Other Features available by SFR
16993 Some MCS51 variants offer features like Double DPTR
16994 \begin_inset LatexCommand \index{DPTR}
16998 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16999 These are currently not used for the MCS51 port.
17000 If you absolutely need them you can fall back to inline assembly or submit
17002 \layout Subsubsection
17004 The Z80 and gbz80 port
17007 SDCC can target both the Zilog
17008 \begin_inset LatexCommand \index{Z80}
17012 and the Nintendo Gameboy's Z80-like gbz80
17013 \begin_inset LatexCommand \index{GameBoy Z80}
17018 The Z80 port is passed through the same
17021 \begin_inset LatexCommand \index{Regression test}
17027 as MCS51 and DS390 ports, so floating point support, support for long variables
17028 and bitfield support is fine.
17031 As always, the code is the authoritative reference - see z80/ralloc.c and
17033 The stack frame is similar to that generated by the IAR Z80 compiler.
17034 IX is used as the base pointer, HL is used as a temporary register, and
17035 BC and DE are available for holding variables.
17036 IY is currently unused.
17037 Return values are stored in HL.
17038 One bad side effect of using IX as the base pointer is that a functions
17039 stack frame is limited to 127 bytes - this will be fixed in a later version.
17042 Retargetting for other MCUs.
17045 The issues for retargetting the compiler are far too numerous to be covered
17047 What follows is a brief description of each of the seven phases of the
17048 compiler and its MCU dependency.
17051 Parsing the source and building the annotated parse tree.
17052 This phase is largely MCU independent (except for the language extensions).
17053 Syntax & semantic checks are also done in this phase, along with some initial
17054 optimizations like back patching labels and the pattern matching optimizations
17055 like bit-rotation etc.
17058 The second phase involves generating an intermediate code which can be easy
17059 manipulated during the later phases.
17060 This phase is entirely MCU independent.
17061 The intermediate code generation assumes the target machine has unlimited
17062 number of registers, and designates them with the name iTemp.
17063 The compiler can be made to dump a human readable form of the code generated
17077 This phase does the bulk of the standard optimizations and is also MCU independe
17079 This phase can be broken down into several sub-phases:
17083 Break down intermediate code (iCode) into basic blocks.
17085 Do control flow & data flow analysis on the basic blocks.
17087 Do local common subexpression elimination, then global subexpression elimination
17089 Dead code elimination
17093 If loop optimizations caused any changes then do 'global subexpression eliminati
17094 on' and 'dead code elimination' again.
17097 This phase determines the live-ranges; by live range I mean those iTemp
17098 variables defined by the compiler that still survive after all the optimization
17100 Live range analysis
17101 \begin_inset LatexCommand \index{Live range analysis}
17105 is essential for register allocation, since these computation determines
17106 which of these iTemps will be assigned to registers, and for how long.
17109 Phase five is register allocation.
17110 There are two parts to this process.
17114 The first part I call 'register packing' (for lack of a better term).
17115 In this case several MCU specific expression folding is done to reduce
17120 The second part is more MCU independent and deals with allocating registers
17121 to the remaining live ranges.
17122 A lot of MCU specific code does creep into this phase because of the limited
17123 number of index registers available in the 8051.
17126 The Code generation phase is (unhappily), entirely MCU dependent and very
17127 little (if any at all) of this code can be reused for other MCU.
17128 However the scheme for allocating a homogenized assembler operand for each
17129 iCode operand may be reused.
17132 As mentioned in the optimization section the peep-hole optimizer is rule
17133 based system, which can reprogrammed for other MCUs.
17137 \begin_inset LatexCommand \index{Compiler internals}
17144 The anatomy of the compiler
17145 \begin_inset LatexCommand \label{sub:The-anatomy-of}
17154 This is an excerpt from an article published in Circuit Cellar Magazine
17156 It's a little outdated (the compiler is much more efficient now and user/develo
17157 per friendly), but pretty well exposes the guts of it all.
17163 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
17164 It is fairly easy to retarget for other 8-bit MCU.
17165 Here we take a look at some of the internals of the compiler.
17170 \begin_inset LatexCommand \index{Parsing}
17177 Parsing the input source file and creating an AST (Annotated Syntax Tree
17178 \begin_inset LatexCommand \index{Annotated syntax tree}
17183 This phase also involves propagating types (annotating each node of the
17184 parse tree with type information) and semantic analysis.
17185 There are some MCU specific parsing rules.
17186 For example the storage classes, the extended storage classes are MCU specific
17187 while there may be a xdata storage class for 8051 there is no such storage
17188 class for z80 or Atmel AVR.
17189 SDCC allows MCU specific storage class extensions, i.e.
17190 xdata will be treated as a storage class specifier when parsing 8051 C
17191 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
17196 \begin_inset LatexCommand \index{iCode}
17203 Intermediate code generation.
17204 In this phase the AST is broken down into three-operand form (iCode).
17205 These three operand forms are represented as doubly linked lists.
17206 ICode is the term given to the intermediate form generated by the compiler.
17207 ICode example section shows some examples of iCode generated for some simple
17208 C source functions.
17212 \begin_inset LatexCommand \index{Optimizations}
17219 Bulk of the target independent optimizations is performed in this phase.
17220 The optimizations include constant propagation, common sub-expression eliminati
17221 on, loop invariant code movement, strength reduction of loop induction variables
17222 and dead-code elimination.
17225 Live range analysis
17226 \begin_inset LatexCommand \index{Live range analysis}
17233 During intermediate code generation phase, the compiler assumes the target
17234 machine has infinite number of registers and generates a lot of temporary
17236 The live range computation determines the lifetime of each of these compiler-ge
17237 nerated temporaries.
17238 A picture speaks a thousand words.
17239 ICode example sections show the live range annotations for each of the
17241 It is important to note here, each iCode is assigned a number in the order
17242 of its execution in the function.
17243 The live ranges are computed in terms of these numbers.
17244 The from number is the number of the iCode which first defines the operand
17245 and the to number signifies the iCode which uses this operand last.
17248 Register Allocation
17249 \begin_inset LatexCommand \index{Register allocation}
17256 The register allocation determines the type and number of registers needed
17258 In most MCUs only a few registers can be used for indirect addressing.
17259 In case of 8051 for example the registers R0 & R1 can be used to indirectly
17260 address the internal ram and DPTR to indirectly address the external ram.
17261 The compiler will try to allocate the appropriate register to pointer variables
17263 ICode example section shows the operands annotated with the registers assigned
17265 The compiler will try to keep operands in registers as much as possible;
17266 there are several schemes the compiler uses to do achieve this.
17267 When the compiler runs out of registers the compiler will check to see
17268 if there are any live operands which is not used or defined in the current
17269 basic block being processed, if there are any found then it will push that
17270 operand and use the registers in this block, the operand will then be popped
17271 at the end of the basic block.
17275 There are other MCU specific considerations in this phase.
17276 Some MCUs have an accumulator; very short-lived operands could be assigned
17277 to the accumulator instead of general-purpose register.
17283 Figure II gives a table of iCode operations supported by the compiler.
17284 The code generation involves translating these operations into corresponding
17285 assembly code for the processor.
17286 This sounds overly simple but that is the essence of code generation.
17287 Some of the iCode operations are generated on a MCU specific manner for
17288 example, the z80 port does not use registers to pass parameters so the
17289 SEND and RECV iCode operations will not be generated, and it also does
17290 not support JUMPTABLES.
17297 <Where is Figure II ?>
17301 \begin_inset LatexCommand \index{iCode}
17308 This section shows some details of iCode.
17309 The example C code does not do anything useful; it is used as an example
17310 to illustrate the intermediate code generated by the compiler.
17322 /* This function does nothing useful.
17329 for the purpose of explaining iCode */
17332 short function (data int *x)
17340 short i=10; /* dead initialization eliminated */
17345 short sum=10; /* dead initialization eliminated */
17358 while (*x) *x++ = *p++;
17372 /* compiler detects i,j to be induction variables */
17376 for (i = 0, j = 10 ; i < 10 ; i++, j
17402 mul += i * 3; /* this multiplication remains */
17408 gint += j * 3;/* this multiplication changed to addition */
17422 In addition to the operands each iCode contains information about the filename
17423 and line it corresponds to in the source file.
17424 The first field in the listing should be interpreted as follows:
17429 Filename(linenumber: iCode Execution sequence number : ICode hash table
17430 key : loop depth of the iCode).
17435 Then follows the human readable form of the ICode operation.
17436 Each operand of this triplet form can be of three basic types a) compiler
17437 generated temporary b) user defined variable c) a constant value.
17438 Note that local variables and parameters are replaced by compiler generated
17441 \begin_inset LatexCommand \index{Live range analysis}
17445 are computed only for temporaries (i.e.
17446 live ranges are not computed for global variables).
17448 \begin_inset LatexCommand \index{Register allocation}
17452 are allocated for temporaries only.
17453 Operands are formatted in the following manner:
17458 Operand Name [lr live-from : live-to ] { type information } [ registers
17464 As mentioned earlier the live ranges are computed in terms of the execution
17465 sequence number of the iCodes, for example
17467 the iTemp0 is live from (i.e.
17468 first defined in iCode with execution sequence number 3, and is last used
17469 in the iCode with sequence number 5).
17470 For induction variables such as iTemp21 the live range computation extends
17471 the lifetime from the start to the end of the loop.
17473 The register allocator used the live range information to allocate registers,
17474 the same registers may be used for different temporaries if their live
17475 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
17476 iTemp17 since their live ranges do not overlap.
17477 In addition the allocator also takes into consideration the type and usage
17478 of a temporary, for example itemp6 is a pointer to near space and is used
17479 as to fetch data from (i.e.
17480 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
17481 Some short lived temporaries are allocated to special registers which have
17482 meaning to the code generator e.g.
17483 iTemp13 is allocated to a pseudo register CC which tells the back end that
17484 the temporary is used only for a conditional jump the code generation makes
17485 use of this information to optimize a compare and jump ICode.
17487 There are several loop optimizations
17488 \begin_inset LatexCommand \index{Loop optimization}
17492 performed by the compiler.
17493 It can detect induction variables iTemp21(i) and iTemp23(j).
17494 Also note the compiler does selective strength reduction
17495 \begin_inset LatexCommand \index{Strength reduction}
17500 the multiplication of an induction variable in line 18 (gint = j * 3) is
17501 changed to addition, a new temporary iTemp17 is allocated and assigned
17502 a initial value, a constant 3 is then added for each iteration of the loop.
17503 The compiler does not change the multiplication
17504 \begin_inset LatexCommand \index{Multiplication}
17508 in line 17 however since the processor does support an 8 * 8 bit multiplication.
17510 Note the dead code elimination
17511 \begin_inset LatexCommand \index{Dead-code elimination}
17515 optimization eliminated the dead assignments in line 7 & 8 to I and sum
17523 Sample.c (5:1:0:0) _entry($9) :
17528 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
17533 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
17538 Sample.c(11:4:53:0) preHeaderLbl0($11) :
17543 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
17549 Sample.c(11:6:5:1) _whilecontinue_0($1) :
17554 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
17560 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
17565 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
17571 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
17577 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
17583 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
17589 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
17590 * int}[r0] + 0x2 {short}
17595 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
17600 Sample.c(11:17:21:0)_whilebreak_0($3) :
17605 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
17610 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
17615 Sample.c(15:20:54:0)preHeaderLbl1($13) :
17620 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
17625 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
17630 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
17635 Sample.c(15:24:26:1)_forcond_0($4) :
17640 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
17646 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
17651 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
17652 + ITemp21 [lr21:38]{short}[r4]
17657 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
17663 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
17664 + iTemp15 [lr29:30]{short}[r1]
17669 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
17675 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
17681 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
17687 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
17693 Sample.c(19:38:47:1) goto _forcond_0($4)
17698 Sample.c(19:39:48:0)_forbreak_0($7) :
17703 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
17704 + ITemp11 [lr19:40]{short}[r3]
17709 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
17714 Sample.c(20:42:51:0)_return($8) :
17719 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
17725 Finally the code generated for this function:
17766 ; ----------------------------------------------
17771 ; function function
17776 ; ----------------------------------------------
17786 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
17798 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
17810 ;_whilecontinue_0($1) :
17820 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
17825 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
17884 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
17903 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
17950 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
17990 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
18016 ; iTemp6 [lr5:16]{_near * int}[r0] =
18021 ; iTemp6 [lr5:16]{_near * int}[r0] +
18038 ; goto _whilecontinue_0($1)
18050 ; _whilebreak_0($3) :
18060 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
18072 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
18084 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
18096 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
18115 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
18144 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
18149 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
18194 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
18199 ; iTemp21 [lr21:38]{short}[r4]
18225 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
18258 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
18263 ; iTemp15 [lr29:30]{short}[r1]
18282 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
18329 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
18376 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
18388 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
18402 cjne r5,#0xff,00104$
18414 ; goto _forcond_0($4)
18426 ; _forbreak_0($7) :
18436 ; ret iTemp24 [lr40:41]{short}
18479 A few words about basic block successors, predecessors and dominators
18482 Successors are basic blocks
18483 \begin_inset LatexCommand \index{Basic blocks}
18487 that might execute after this basic block.
18489 Predecessors are basic blocks that might execute before reaching this basic
18492 Dominators are basic blocks that WILL execute before reaching this basic
18526 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
18529 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
18532 c) domVect of [BB4] = BB1 ...
18533 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
18541 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
18551 Thanks to all the other volunteer developers who have helped with coding,
18552 testing, web-page creation, distribution sets, etc.
18553 You know who you are :-)
18560 This document was initially written by Sandeep Dutta
18563 All product names mentioned herein may be trademarks
18564 \begin_inset LatexCommand \index{Trademarks}
18568 of their respective companies.
18575 To avoid confusion, the installation and building options for SDCC itself
18576 (chapter 2) are not part of the index.
18580 \begin_inset LatexCommand \printindex{}