1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
5 \usepackage[colorlinks=true,linkcolor=blue]{hyperref}
12 \paperfontsize default
19 \use_numerical_citations 0
20 \paperorientation portrait
27 \paragraph_separation indent
29 \quotes_language swedish
37 Please note: double dashed longoptions (e.g.
38 --version) are written this way: -
52 three consecutive dashes simply result in a long resp.
56 SDCC Compiler User Guide
63 \begin_inset LatexCommand \tableofcontents{}
80 is a Freeware, retargettable, optimizing ANSI-C compiler by
84 designed for 8 bit Microprocessors.
85 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
87 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052 CPU}
91 , etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
92 It can be retargetted for other microprocessors, support for PIC, AVR and
93 186 is under development.
94 The entire source code for the compiler is distributed under GPL.
95 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
96 SDCC has extensive language extensions suitable for utilizing various microcont
97 rollers and underlying hardware effectively.
102 In addition to the MCU specific optimizations SDCC also does a host of standard
106 global sub expression elimination,
109 loop optimizations (loop invariant, strength reduction of induction variables
113 constant folding & propagation,
119 dead code elimination
129 For the back-end SDCC uses a global register allocation scheme which should
130 be well suited for other 8 bit MCUs.
135 The peep hole optimizer uses a rule based substitution mechanism which is
141 Supported data-types are:
144 char (8 bits, 1 byte),
147 short and int (16 bits, 2 bytes),
150 long (32 bit, 4 bytes)
157 The compiler also allows
159 inline assembler code
161 to be embedded anywhere in a function.
162 In addition, routines developed in assembly can also be called.
166 SDCC also provides an option (-
176 -cyclomatic) to report the relative complexity of a function.
177 These functions can then be further optimized, or hand coded in assembly
183 SDCC also comes with a companion source level debugger SDCDB, the debugger
184 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
189 The latest version can be downloaded from
190 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
200 Please note: the compiler will probably always be some steps ahead of this
205 \begin_inset LatexCommand \index{Status of documentation}
215 Obviously this has pros and cons
224 All packages used in this compiler system are
232 ; source code for all the sub-packages (pre-processor, assemblers, linkers
233 etc) is distributed with the package.
234 This documentation is maintained using a freeware word processor (LyX).
236 This program is free software; you can redistribute it and/or modify it
237 under the terms of the GNU General Public License
238 \begin_inset LatexCommand \index{GNU General Public License, GPL}
242 as published by the Free Software Foundation; either version 2, or (at
243 your option) any later version.
244 This program is distributed in the hope that it will be useful, but WITHOUT
245 ANY WARRANTY; without even the implied warranty
246 \begin_inset LatexCommand \index{warranty}
250 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
251 See the GNU General Public License for more details.
252 You should have received a copy of the GNU General Public License along
253 with this program; if not, write to the Free Software Foundation, 59 Temple
254 Place - Suite 330, Boston, MA 02111-1307, USA.
255 In other words, you are welcome to use, share and improve this program.
256 You are forbidden to forbid anyone else to use, share and improve what
258 Help stamp out software-hoarding!
261 Typographic conventions
262 \begin_inset LatexCommand \index{Typographic conventions}
269 Throughout this manual, we will use the following convention.
270 Commands you have to type in are printed in
278 Code samples are printed in
283 Interesting items and new terms are printed in
288 Compatibility with previous versions
291 This version has numerous bug fixes compared with the previous version.
292 But we also introduced some incompatibilities with older versions.
293 Not just for the fun of it, but to make the compiler more stable, efficient
295 \begin_inset LatexCommand \index{ANSI-compliance}
305 short is now equivalent to int (16 bits), it used to be equivalent to char
306 (8 bits) which is not ANSI compliant
309 the default directory for gcc-builds where include, library and documention
310 files are stored is now in /usr/local/share
313 char type parameters to vararg functions are casted to int unless explicitly
330 will push a as an int and as a char resp.
343 -regextend has been removed
356 -noregparms has been removed
369 -stack-after-data has been removed
374 <pending: more incompatibilities?>
380 What do you need before you start installation of SDCC? A computer, and
382 The preferred method of installation is to compile SDCC from source using
384 For Windows some pre-compiled binary distributions are available for your
386 You should have some experience with command line tools and compiler use.
392 The SDCC home page at
393 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
397 is a great place to find distribution sets.
398 You can also find links to the user mailing lists that offer help or discuss
399 SDCC with other SDCC users.
400 Web links to other SDCC related sites can also be found here.
401 This document can be found in the DOC directory of the source package as
403 Some of the other tools (simulator and assembler) included with SDCC contain
404 their own documentation and can be found in the source distribution.
405 If you want the latest unreleased software, the complete source package
406 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
409 ANSI C reference / related tools / recommended reading / compiler building:
413 Wishes for the future
416 There are (and always will be) some things that could be done.
417 Here are some I can think of:
424 char KernelFunction3(char p) at 0x340;
432 \begin_inset LatexCommand \index{code banking (not supported)}
440 If you can think of some more, please see the chapter 9 about filing feature
442 \begin_inset LatexCommand \index{Requesting features}
447 \begin_inset LatexCommand \index{Feature requests}
457 \begin_inset LatexCommand \index{Installation}
465 \begin_inset LatexCommand \index{Options SDCC configuration}
472 The install paths, search paths and other options are defined when running
474 The defaults can be overriden by:
476 \labelwidthstring 00.00.0000
488 -prefix see tabel below
490 \labelwidthstring 00.00.0000
502 -exec_prefix see tabel below
504 \labelwidthstring 00.00.0000
516 -bindir see tabel below
518 \labelwidthstring 00.00.0000
530 -datadir see tabel below
532 \labelwidthstring 00.00.0000
534 docdir environment variable, see tabel below
536 \labelwidthstring 00.00.0000
538 include_dir_suffix environment variable, see tabel below
540 \labelwidthstring 00.00.0000
542 lib_dir_suffix environment variable, see tabel below
544 \labelwidthstring 00.00.0000
546 sdccconf_h_dir_separator environment variable, either / or
551 This character will only be used in sdccconf.h; don't forget it's a C-header,
552 therefore a double-backslash is needed there.
554 \labelwidthstring 00.00.0000
566 -disable-mcs51-port Excludes the Intel mcs51 port
568 \labelwidthstring 00.00.0000
580 -disable-gbz80-port Excludes the Gameboy gbz80 port
582 \labelwidthstring 00.00.0000
594 -z80-port Excludes the z80 port
596 \labelwidthstring 00.00.0000
608 -disable-avr-port Excludes the AVR port
610 \labelwidthstring 00.00.0000
622 -disable-ds390-port Excludes the DS390 port
624 \labelwidthstring 00.00.0000
636 -disable-pic-port Excludes the PIC port
638 \labelwidthstring 00.00.0000
650 -disable-xa51-port Excludes the XA51 port
652 \labelwidthstring 00.00.0000
664 -disable-ucsim Disables configuring and building of ucsim
666 \labelwidthstring 00.00.0000
678 -disable-device-lib-build Disables automatically building device libraries
680 \labelwidthstring 00.00.0000
692 -disable-packihx Disables building packihx
694 \labelwidthstring 00.00.0000
706 -enable-libgc Use the Bohem memory allocator.
707 Lower runtime footprint.
710 Furthermore the environment variables CC, CFLAGS, ...
711 the tools and their arguments can be influenced.
712 Please see `configure -
722 -help` and the man/info pages of `configure` for details.
726 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
727 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
728 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
729 At the moment it's not possible to change the default settings (it was
730 simply never required.
734 These configure options are compiled into the binaries, and can only be
735 changed by rerunning 'configure' and recompiling SDCC.
736 The configure options are written in
740 to distinguish them from run time environment variables (see section search
746 \begin_inset Quotes sld
750 \begin_inset Quotes srd
753 are used by the SDCC team to build the official Win32 binaries.
754 The SDCC team uses Mingw32 to build the official Windows binaries, because
761 a gcc compiler and last but not least
764 the binaries can be built by cross compiling on Sourceforge's compile farm.
767 See the examples, how to pass the Win32 settings to 'configure'.
768 The other Win32 builds using Borland, VC or whatever don't use 'configure',
769 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
780 <lyxtabular version="3" rows="8" columns="3">
782 <column alignment="left" valignment="top" leftline="true" width="0in">
783 <column alignment="left" valignment="top" leftline="true" width="0in">
784 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
785 <row topline="true" bottomline="true">
786 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
822 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
884 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
896 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
912 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
968 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1011 <row topline="true" bottomline="true">
1012 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1049 'configure' also computes relative paths.
1050 This is needed for full relocatability of a binary package and to complete
1051 search paths (see section search paths below):
1056 \begin_inset Tabular
1057 <lyxtabular version="3" rows="4" columns="3">
1059 <column alignment="left" valignment="top" leftline="true" width="0in">
1060 <column alignment="left" valignment="top" leftline="true" width="0in">
1061 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1062 <row topline="true" bottomline="true">
1063 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1071 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1079 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1088 <row topline="true" bottomline="true">
1089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1118 <row bottomline="true">
1119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1146 <row bottomline="true">
1147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1187 ./configure --prefix=
1188 \begin_inset Quotes srd
1192 \begin_inset Quotes srd
1196 \begin_inset Quotes srd
1200 \begin_inset Quotes srd
1206 ./configure --disable-avr-port --disable-xa51-port
1209 To crosscompile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw3
1219 \begin_inset Quotes srd
1222 i586-mingw32msvc-gcc
1223 \begin_inset Quotes srd
1227 \begin_inset Quotes srd
1230 i586-mingw32msvc-g++
1231 \begin_inset Quotes srd
1240 \begin_inset Quotes srd
1243 i586-mingw32msvc-ranlib
1244 \begin_inset Quotes srd
1253 \begin_inset Quotes srd
1256 i586-mingw32msvc-strip
1257 \begin_inset Quotes srd
1266 \begin_inset Quotes srd
1270 \begin_inset Quotes srd
1279 \begin_inset Quotes srd
1283 \begin_inset Quotes srd
1292 \begin_inset Quotes srd
1296 \begin_inset Quotes srd
1305 \begin_inset Quotes srd
1309 \begin_inset Quotes srd
1318 \begin_inset Quotes srd
1322 \begin_inset Quotes srd
1330 sdccconf_h_dir_separator=
1331 \begin_inset Quotes srd
1343 \begin_inset Quotes srd
1351 --disable-device-lib-build
1361 --host=i586-mingw32msvc --build=unknown-unknown-linux-gnu
1365 \begin_inset Quotes sld
1369 \begin_inset Quotes srd
1372 compile on Cygwin for Mingw32(see also sdcc/support/scripts/sdcc_cygwin_mingw32)
1382 \begin_inset Quotes srd
1386 \begin_inset Quotes srd
1395 \begin_inset Quotes srd
1399 \begin_inset Quotes srd
1408 \begin_inset Quotes srd
1412 \begin_inset Quotes srd
1421 \begin_inset Quotes srd
1425 \begin_inset Quotes srd
1434 \begin_inset Quotes srd
1438 \begin_inset Quotes srd
1447 \begin_inset Quotes srd
1451 \begin_inset Quotes srd
1460 \begin_inset Quotes srd
1464 \begin_inset Quotes srd
1472 sdccconf_h_dir_separator=
1473 \begin_inset Quotes srd
1485 \begin_inset Quotes srd
1496 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1507 -C' turns on caching, which gives a little bit extra speed.
1508 However if options are changed, it can be necessary to delete the config.cache
1513 \begin_inset LatexCommand \index{Install paths}
1521 Binary files (preprocessor, assembler and linker)
1525 \begin_inset Tabular
1526 <lyxtabular version="3" rows="2" columns="3">
1528 <column alignment="left" valignment="top" leftline="true" width="0in">
1529 <column alignment="left" valignment="top" leftline="true" width="0in">
1530 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1531 <row topline="true" bottomline="true">
1532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1557 <row topline="true" bottomline="true">
1558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1602 \begin_inset Tabular
1603 <lyxtabular version="3" rows="2" columns="3">
1605 <column alignment="block" valignment="top" leftline="true" width="1.6in">
1606 <column alignment="left" valignment="top" leftline="true" width="0in">
1607 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
1608 <row topline="true" bottomline="true">
1609 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1617 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1625 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1634 <row topline="true" bottomline="true">
1635 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1652 /usr/local/share/sdcc/include
1655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1681 is auto-appended by the compiler, e.g.
1682 small, large, z80, ds390 etc.)
1686 \begin_inset Tabular
1687 <lyxtabular version="3" rows="2" columns="3">
1689 <column alignment="left" valignment="top" leftline="true" width="0in">
1690 <column alignment="left" valignment="top" leftline="true" width="0in">
1691 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1692 <row topline="true" bottomline="true">
1693 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1718 <row topline="true" bottomline="true">
1719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1726 $DATADIR/$LIB_DIR_SUFFIX
1729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1734 /usr/local/share/sdcc/lib
1737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1763 \begin_inset Tabular
1764 <lyxtabular version="3" rows="2" columns="3">
1766 <column alignment="left" valignment="top" leftline="true" width="0in">
1767 <column alignment="left" valignment="top" leftline="true" width="0in">
1768 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1769 <row topline="true" bottomline="true">
1770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1795 <row topline="true" bottomline="true">
1796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1811 /usr/local/share/sdcc/doc
1814 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1835 The install paths can still be changed during `make install` with e.g.:
1838 make install prefix=$(HOME)/local/sdcc
1841 Of course this doesn't change the search paths compiled into the binaries.
1845 \begin_inset LatexCommand \index{Search path}
1852 Some search paths or parts of them are determined by configure variables
1857 , see section above).
1858 Further search paths are determined by environment variables during runtime.
1861 The paths searched when running the compiler are as follows (the first catch
1867 Binary files (preprocessor, assembler and linker)
1870 \begin_inset Tabular
1871 <lyxtabular version="3" rows="4" columns="3">
1873 <column alignment="left" valignment="top" leftline="true" width="0in">
1874 <column alignment="left" valignment="top" leftline="true" width="0in">
1875 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1876 <row topline="true" bottomline="true">
1877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1902 <row topline="true">
1903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1932 <row topline="true">
1933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1938 Path of argv[0] (if available)
1941 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1949 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1958 <row topline="true" bottomline="true">
1959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1996 \begin_inset Tabular
1997 <lyxtabular version="3" rows="6" columns="3">
1999 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2000 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2001 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
2002 <row topline="true" bottomline="true">
2003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2028 <row topline="true">
2029 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2065 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2084 <row topline="true">
2085 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2093 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2101 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2110 <row topline="true">
2111 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2125 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2148 <row topline="true">
2149 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2217 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2230 <row topline="true" bottomline="true">
2231 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2247 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2252 /usr/local/share/sdcc/
2257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2284 -nostdinc disables the last two search paths.
2291 With the exception of
2292 \begin_inset Quotes sld
2306 \begin_inset Quotes srd
2313 is auto-appended by the compiler (e.g.
2314 small, large, z80, ds390 etc.).
2318 \begin_inset Tabular
2319 <lyxtabular version="3" rows="6" columns="3">
2321 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2322 <column alignment="left" valignment="top" leftline="true" width="1.2in">
2323 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2324 <row topline="true" bottomline="true">
2325 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2333 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2350 <row topline="true">
2351 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2387 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2406 <row topline="true">
2407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2419 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2446 <row topline="true">
2447 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2458 $LIB_DIR_SUFFIX/<model>
2461 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2475 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2492 <row topline="true">
2493 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2508 $LIB_DIR_SUFFIX/<model>
2511 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2620 <row topline="true" bottomline="true">
2621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2630 $LIB_DIR_SUFFIX/<model>
2633 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2638 /usr/local/share/sdcc/
2645 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2661 Don't delete any of the stray spaces in the table above without checking
2662 the HTML output (last line)!
2678 -nostdlib disables the last two search paths.
2682 \begin_inset LatexCommand \index{Building SDCC}
2687 \layout Subsubsection
2689 Building SDCC on Linux
2694 Download the source package
2696 either from the SDCC CVS repository or from the
2697 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2703 , it will be named something like sdcc
2716 Bring up a command line terminal, such as xterm.
2721 Unpack the file using a command like:
2724 "tar -xzf sdcc.src.tar.gz
2729 , this will create a sub-directory called sdcc with all of the sources.
2732 Change directory into the main SDCC directory, for example type:
2749 This configures the package for compilation on your system.
2765 All of the source packages will compile, this can take a while.
2781 This copies the binary executables, the include files, the libraries and
2782 the documentation to the install directories.
2783 \layout Subsubsection
2785 Building SDCC on OSX 2.x
2788 Follow the instruction for Linux.
2792 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2793 )) fails to compile SDCC.
2794 Fortunately there's also gcc 2.9.x installed, which works fine.
2795 This compiler can be selected by running 'configure' with:
2798 ./configure CC=gcc2 CXX=g++2
2799 \layout Subsubsection
2801 Crosscompiling SDCC on Linux for Windows
2804 With the Mingw32 gcc crosscompiler it's easy to compile SDCC for Win32.
2805 See section 'Configure Options'.
2806 \layout Subsubsection
2808 Building SDCC on Windows
2811 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2813 They use Unix-sockets, which are not available on Win32.
2814 \layout Subsubsection
2816 Windows Install Using a Binary Package
2819 Download the binary package and unpack it using your favorite unpacking
2820 tool (gunzip, WinZip, etc).
2821 This should unpack to a group of sub-directories.
2822 An example directory structure after unpacking the mingw32 package is:
2827 bin for the executables, c:
2835 lib for the include and libraries.
2838 Adjust your environment variable PATH to include the location of the bin
2839 directory or start sdcc using the full path.
2840 \layout Subsubsection
2842 Building SDCC using Cygwin and Mingw32
2845 For building and installing a Cygwin executable follow the instructions
2851 \begin_inset Quotes sld
2855 \begin_inset Quotes srd
2858 Win32-binary can be built, which will not need the Cygwin-DLL.
2859 For the necessary 'configure' options see section 'configure options' or
2860 the script 'sdcc/support/scripts/sdcc_cygwinmingw32'.
2864 In order to install Cygwin on Windows download setup.exe from
2865 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2871 \begin_inset Quotes sld
2874 default text file type
2875 \begin_inset Quotes srd
2879 \begin_inset Quotes sld
2883 \begin_inset Quotes srd
2886 and download/install at least the following packages.
2887 Some packages are selected by default, others will be automatically selected
2888 because of dependencies with the manually selected packages.
2889 Never deselect these packages!
2898 gcc ; version 3.x is fine, no need to use the old 2.9x
2901 binutils ; selected with gcc
2907 rxvt ; a nice console, which makes life much easier under windoze (see below)
2910 man ; not really needed for building SDCC, but you'll miss it sooner or
2914 less ; not really needed for building SDCC, but you'll miss it sooner or
2918 cvs ; only if you use CVS access
2921 If you want to develop something you'll need:
2924 python ; for the regression tests
2927 gdb ; the gnu debugger, together with the nice GUI
2928 \begin_inset Quotes sld
2932 \begin_inset Quotes srd
2938 openssh ; to access the CF or commit changes
2941 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
2942 use autoconf-stable!
2945 rxvt is a nice console with history.
2946 Replace in your cygwin.bat the line
2955 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
2958 -bg black -fg white -geometry 100x65 -e bash --login
2961 Text selected with the mouse is automatically copied to the clipboard, pasting
2962 works with shift-insert.
2966 The other good tip is to make sure you have no //c/-style paths anywhere,
2967 use /cygdrive/c/ instead.
2968 Using // invokes a network lookup which is very slow.
2970 \begin_inset Quotes sld
2974 \begin_inset Quotes srd
2977 is too long, you can change it with e.g.
2983 SDCC sources use the unix line ending LF.
2984 Life is much easier, if you store the source tree on a drive, which is
2985 mount in binary mode.
2986 And use an editor which can handle LF-only line endings.
2987 Make sure not to commit files with windows line endings.
2988 \layout Subsubsection
2990 Windows Install Using Microsoft Visual C++ 6.0/NET
2995 Download the source package
2997 either from the SDCC CVS repository or from the
2998 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3004 , it will be named something like sdcc
3011 SDCC is distributed with all the projects, workspaces, and files you need
3012 to build it using Visual C++ 6.0/NET.
3013 The workspace name is 'sdcc.dsw'.
3014 Please note that as it is now, all the executables are created in a folder
3018 Once built you need to copy the executables from sdcc
3022 bin before runnng SDCC.
3027 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
3028 of bison.exe, flex.exe, and gawk.exe.
3029 One good place to get them is
3030 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3038 Download the file UnxUtils
3039 \begin_inset LatexCommand \index{UnxUtils}
3044 Now you have to install the utilities and setup Visual C++ so it can locate
3045 the required programs.
3046 Here there are two alternatives (choose one!):
3053 a) Extract UnxUtils.zip to your C:
3055 hard disk PRESERVING the original paths, otherwise bison won't work.
3056 (If you are using WinZip make certain that 'Use folder names' is selected)
3060 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3061 in 'Show directories for:' select 'Executable files', and in the directories
3062 window add a new path: 'C:
3072 (As a side effect, you get a bunch of Unix utilities that could be useful,
3073 such as diff and patch.)
3080 This one avoids extracting a bunch of files you may not use, but requires
3085 a) Create a directory were to put the tools needed, or use a directory already
3093 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3094 to such directory WITHOUT preserving the original paths.
3095 (If you are using WinZip make certain that 'Use folder names' is not selected)
3099 c) Rename bison.exe to '_bison.exe'.
3103 d) Create a batch file 'bison.bat' in 'C:
3107 ' and add these lines:
3127 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3131 Steps 'c' and 'd' are needed because bison requires by default that the
3132 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3133 '/usr/local/share/' I think.
3134 So it is necessary to tell bison where those files are located if they
3135 are not in such directory.
3136 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3140 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3141 in 'Show directories for:' select 'Executable files', and in the directories
3142 window add a new path: 'c:
3145 Note that you can use any other path instead of 'c:
3147 util', even the path where the Visual C++ tools are, probably: 'C:
3151 Microsoft Visual Studio
3156 So you don't have to execute step 'e' :)
3160 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3161 the executables from sdcc
3165 bin, and you can compile using sdcc.
3166 \layout Subsubsection
3168 Windows Install Using Borland
3171 From the sdcc directory, run the command "make -f Makefile.bcc".
3172 This should regenerate all the .exe files in the bin directory except for
3173 sdcdb.exe (which currently doesn't build under Borland C++).
3176 If you modify any source files and need to rebuild, be aware that the dependanci
3177 es may not be correctly calculated.
3178 The safest option is to delete all .obj files and run the build again.
3179 From a Cygwin BASH prompt, this can easily be done with the commmand:
3189 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3191 ) -print -exec rm {}
3200 or on Windows NT/2000/XP from the command prompt with the commmand:
3207 del /s *.obj *.lib *.rul
3210 from the sdcc directory.
3213 Building the Documentation
3220 Testing out the SDCC Compiler
3223 The first thing you should do after installing your SDCC compiler is to
3239 \begin_inset LatexCommand \index{version}
3246 at the prompt, and the program should run and tell you the version.
3247 If it doesn't run, or gives a message about not finding sdcc program, then
3248 you need to check over your installation.
3249 Make sure that the sdcc bin directory is in your executable search path
3250 defined by the PATH environment setting (see the Trouble-shooting section
3252 Make sure that the sdcc program is in the bin folder, if not perhaps something
3253 did not install correctly.
3261 is commonly installed as described in section
3262 \begin_inset Quotes sld
3265 Install and search paths
3266 \begin_inset Quotes srd
3275 Make sure the compiler works on a very simple example.
3276 Type in the following test.c program using your favorite
3311 Compile this using the following command:
3320 If all goes well, the compiler will generate a test.asm and test.rel file.
3321 Congratulations, you've just compiled your first program with SDCC.
3322 We used the -c option to tell SDCC not to link the generated code, just
3323 to keep things simple for this step.
3331 The next step is to try it with the linker.
3341 If all goes well the compiler will link with the libraries and produce
3342 a test.ihx output file.
3347 (no test.ihx, and the linker generates warnings), then the problem is most
3348 likely that sdcc cannot find the
3352 usr/local/share/sdcc/lib directory
3356 (see the Install trouble-shooting section for suggestions).
3364 The final test is to ensure sdcc can use the
3368 header files and libraries.
3369 Edit test.c and change it to the following:
3389 strcpy(str1, "testing");
3398 Compile this by typing
3405 This should generate a test.ihx output file, and it should give no warnings
3406 such as not finding the string.h file.
3407 If it cannot find the string.h file, then the problem is that sdcc cannot
3408 find the /usr/local/share/sdcc/include directory
3412 (see the Install trouble-shooting section for suggestions).
3415 Install Trouble-shooting
3416 \begin_inset LatexCommand \index{Install trouble-shooting}
3421 \layout Subsubsection
3423 SDCC does not build correctly.
3426 A thing to try is starting from scratch by unpacking the .tgz source package
3427 again in an empty directory.
3435 ./configure 2>&1 | tee configure.log
3449 make 2>&1 | tee make.log
3456 If anything goes wrong, you can review the log files to locate the problem.
3457 Or a relevant part of this can be attached to an email that could be helpful
3458 when requesting help from the mailing list.
3459 \layout Subsubsection
3462 \begin_inset Quotes sld
3466 \begin_inset Quotes srd
3473 \begin_inset Quotes sld
3477 \begin_inset Quotes srd
3480 command is a script that analyzes your system and performs some configuration
3481 to ensure the source package compiles on your system.
3482 It will take a few minutes to run, and will compile a few tests to determine
3483 what compiler features are installed.
3484 \layout Subsubsection
3487 \begin_inset Quotes sld
3491 \begin_inset Quotes srd
3497 This runs the GNU make tool, which automatically compiles all the source
3498 packages into the final installed binary executables.
3499 \layout Subsubsection
3502 \begin_inset Quotes sld
3506 \begin_inset Quotes erd
3512 This will install the compiler, other executables libraries and include
3513 files in to the appropriate directories.
3515 \begin_inset Quotes sld
3518 Install and Search PATHS
3519 \begin_inset Quotes srd
3524 On most systems you will need super-user privilages to do this.
3530 SDCC is not just a compiler, but a collection of tools by various developers.
3531 These include linkers, assemblers, simulators and other components.
3532 Here is a summary of some of the components.
3533 Note that the included simulator and assembler have separate documentation
3534 which you can find in the source package in their respective directories.
3535 As SDCC grows to include support for other processors, other packages from
3536 various developers are included and may have their own sets of documentation.
3540 You might want to look at the files which are installed in <installdir>.
3541 At the time of this writing, we find the following programs for gcc-builds:
3545 In <installdir>/bin:
3548 sdcc - The compiler.
3551 sdcpp - The C preprocessor.
3554 asx8051 - The assembler for 8051 type processors.
3561 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3564 aslink -The linker for 8051 type processors.
3571 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3574 s51 - The ucSim 8051 simulator.
3577 sdcdb - The source debugger.
3580 packihx - A tool to pack (compress) Intel hex files.
3583 In <installdir>/share/sdcc/include
3589 In <installdir>/share/sdcc/lib
3592 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3596 In <installdir>/share/sdcc/doc
3602 As development for other processors proceeds, this list will expand to include
3603 executables to support processors like AVR, PIC, etc.
3604 \layout Subsubsection
3609 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3610 the assembler and linkage editor.
3611 \layout Subsubsection
3614 \begin_inset LatexCommand \index{sdcpp}
3618 - The C-Preprocessor
3621 The preprocessor is a modified version of the GNU preprocessor.
3622 The C preprocessor is used to pull in #include sources, process #ifdef
3623 statements, #defines and so on.
3624 \layout Subsubsection
3626 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3630 This is retargettable assembler & linkage editor, it was developed by Alan
3632 John Hartman created the version for 8051, and I (Sandeep) have made some
3633 enhancements and bug fixes for it to work properly with the SDCC.
3634 \layout Subsubsection
3637 \begin_inset LatexCommand \index{s51}
3644 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3645 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3650 The simulator is built as part of the build process.
3651 For more information visit Daniel's website at:
3652 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3657 It currently support the core mcs51, the Dallas DS80C390 and the Philips
3659 \layout Subsubsection
3662 \begin_inset LatexCommand \index{sdcdb}
3666 - Source Level Debugger
3669 Sdcdb is the companion source level debugger.
3670 The current version of the debugger uses Daniel's Simulator S51, but can
3671 be easily changed to use other simulators.
3678 \layout Subsubsection
3680 Single Source File Projects
3683 For single source file 8051 projects the process is very simple.
3684 Compile your programs with the following command
3687 "sdcc sourcefile.c".
3691 This will compile, assemble and link your source file.
3692 Output files are as follows
3696 \begin_inset LatexCommand \index{.asm}
3701 \begin_inset LatexCommand \index{Assembler source}
3705 file created by the compiler
3709 \begin_inset LatexCommand \index{.lst}
3714 \begin_inset LatexCommand \index{Assembler listing}
3718 file created by the Assembler
3722 \begin_inset LatexCommand \index{.rst}
3727 \begin_inset LatexCommand \index{Assembler listing}
3731 file updated with linkedit information, created by linkage editor
3735 \begin_inset LatexCommand \index{.sym}
3740 \begin_inset LatexCommand \index{Symbol listing}
3744 for the sourcefile, created by the assembler
3748 \begin_inset LatexCommand \index{.rel}
3753 \begin_inset LatexCommand \index{Object file}
3757 created by the assembler, input to Linkage editor
3761 \begin_inset LatexCommand \index{.map}
3766 \begin_inset LatexCommand \index{Memory map}
3770 for the load module, created by the Linker
3774 \begin_inset LatexCommand \index{.mem}
3778 - A file with a summary of the memory usage
3782 \begin_inset LatexCommand \index{.ihx}
3786 - The load module in Intel hex format
3787 \begin_inset LatexCommand \index{Intel hex format}
3791 (you can select the Motorola S19 format
3792 \begin_inset LatexCommand \index{Motorola S19 format}
3807 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
3812 If you need another format you might want to use objdump
3813 \begin_inset LatexCommand \index{objdump}
3818 \begin_inset LatexCommand \index{srecord}
3826 \begin_inset LatexCommand \index{.adb}
3830 - An intermediate file containing debug information needed to create the
3842 \begin_inset LatexCommand \index{-\/-debug}
3850 \begin_inset LatexCommand \index{.cdb}
3854 - An optional file (with -
3864 -debug) containing debug information
3869 \begin_inset LatexCommand \index{. (no extension)}
3873 An optional AOMF51 file containing debug information (with -
3887 \begin_inset LatexCommand \index{.dump*}
3891 - Dump file to debug the compiler it self (with -
3901 -dumpall) (see section
3902 \begin_inset Quotes sld
3905 Anatomy of the compiler
3906 \begin_inset Quotes srd
3910 \layout Subsubsection
3912 Projects with Multiple Source Files
3915 SDCC can compile only ONE file at a time.
3916 Let us for example assume that you have a project containing the following
3921 foo1.c (contains some functions)
3923 foo2.c (contains some more functions)
3925 foomain.c (contains more functions and the function main)
3933 The first two files will need to be compiled separately with the commands:
3965 Then compile the source file containing the
3970 \begin_inset LatexCommand \index{Linker}
3974 the files together with the following command:
3982 foomain.c\SpecialChar ~
3983 foo1.rel\SpecialChar ~
3988 \begin_inset LatexCommand \index{.rel}
4000 can be separately compiled as well:
4011 sdcc foomain.rel foo1.rel foo2.rel
4018 The file containing the
4033 file specified in the command line, since the linkage editor processes
4034 file in the order they are presented to it.
4035 \layout Subsubsection
4037 Projects with Additional Libraries
4038 \begin_inset LatexCommand \index{Libraries}
4045 Some reusable routines may be compiled into a library, see the documentation
4046 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4050 \begin_inset LatexCommand \index{.lib}
4057 Libraries created in this manner can be included in the command line.
4058 Make sure you include the -L <library-path> option to tell the linker where
4059 to look for these files if they are not in the current directory.
4060 Here is an example, assuming you have the source file
4072 (if that is not the same as your current project):
4079 sdcc foomain.c foolib.lib -L mylib
4090 must be an absolute path name.
4094 The most efficient way to use libraries is to keep seperate modules in seperate
4096 The lib file now should name all the modules.rel files.
4097 For an example see the standard library file
4101 in the directory <installdir>/share/lib/small.
4104 Command Line Options
4105 \begin_inset LatexCommand \index{Command Line Options}
4110 \layout Subsubsection
4112 Processor Selection Options
4113 \begin_inset LatexCommand \index{Options processor selection}
4118 \begin_inset LatexCommand \index{Processor selection options}
4124 \labelwidthstring 00.00.0000
4129 \begin_inset LatexCommand \index{-mmcs51}
4135 Generate code for the MCS51
4136 \begin_inset LatexCommand \index{MCS51}
4140 family of processors.
4141 This is the default processor target.
4143 \labelwidthstring 00.00.0000
4148 \begin_inset LatexCommand \index{-mds390}
4154 Generate code for the DS80C390
4155 \begin_inset LatexCommand \index{DS80C390}
4161 \labelwidthstring 00.00.0000
4166 \begin_inset LatexCommand \index{-mds400}
4172 Generate code for the DS80C400
4173 \begin_inset LatexCommand \index{DS80C400}
4179 \labelwidthstring 00.00.0000
4184 \begin_inset LatexCommand \index{-mz80}
4190 Generate code for the Z80
4191 \begin_inset LatexCommand \index{Z80}
4195 family of processors.
4197 \labelwidthstring 00.00.0000
4202 \begin_inset LatexCommand \index{-mgbz80}
4208 Generate code for the GameBoy Z80
4209 \begin_inset LatexCommand \index{GameBoy Z80}
4215 \labelwidthstring 00.00.0000
4220 \begin_inset LatexCommand \index{-mavr}
4226 Generate code for the Atmel AVR
4227 \begin_inset LatexCommand \index{AVR}
4231 processor (In development, not complete).
4232 AVR users should probably have a look at avr-gcc
4233 \begin_inset LatexCommand \url[FIXME: official URL?]{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4240 I think it is fair to direct users there for now.
4241 Open source is also about avoiding unnecessary work .
4242 But I didn't find the 'official' link.
4244 \labelwidthstring 00.00.0000
4249 \begin_inset LatexCommand \index{-mpic14}
4255 Generate code for the PIC 14
4256 \begin_inset LatexCommand \index{PIC14}
4260 -bit processors (In development, not complete).
4263 p16f627 p16f628 p16f84 p16f873 p16f877?
4265 \labelwidthstring 00.00.0000
4271 Generate code for the Toshiba TLCS-900H
4272 \begin_inset LatexCommand \index{TLCS-900H}
4276 processor (In development, not complete).
4278 \labelwidthstring 00.00.0000
4283 \begin_inset LatexCommand \index{-mxa51}
4289 Generate code for the Philips XA51
4290 \begin_inset LatexCommand \index{XA51}
4294 processor (In development, not complete).
4295 \layout Subsubsection
4297 Preprocessor Options
4298 \begin_inset LatexCommand \index{Options preprocessor}
4303 \begin_inset LatexCommand \index{Preprocessor options}
4309 \labelwidthstring 00.00.0000
4314 \begin_inset LatexCommand \index{-I<path>}
4320 The additional location where the pre processor will look for <..h> or
4321 \begin_inset Quotes eld
4325 \begin_inset Quotes erd
4330 \labelwidthstring 00.00.0000
4335 \begin_inset LatexCommand \index{-D<macro[=value]>}
4341 Command line definition of macros.
4342 Passed to the pre processor.
4344 \labelwidthstring 00.00.0000
4349 \begin_inset LatexCommand \index{-M}
4355 Tell the preprocessor to output a rule suitable for make describing the
4356 dependencies of each object file.
4357 For each source file, the preprocessor outputs one make-rule whose target
4358 is the object file name for that source file and whose dependencies are
4359 all the files `#include'd in it.
4360 This rule may be a single line or may be continued with `
4362 '-newline if it is long.
4363 The list of rules is printed on standard output instead of the preprocessed
4367 \labelwidthstring 00.00.0000
4372 \begin_inset LatexCommand \index{-C}
4378 Tell the preprocessor not to discard comments.
4379 Used with the `-E' option.
4381 \labelwidthstring 00.00.0000
4386 \begin_inset LatexCommand \index{-MM}
4397 Like `-M' but the output mentions only the user header files included with
4399 \begin_inset Quotes eld
4403 System header files included with `#include <file>' are omitted.
4405 \labelwidthstring 00.00.0000
4410 \begin_inset LatexCommand \index{-Aquestion(answer)}
4416 Assert the answer answer for question, in case it is tested with a preprocessor
4417 conditional such as `#if #question(answer)'.
4418 `-A-' disables the standard assertions that normally describe the target
4421 \labelwidthstring 00.00.0000
4427 (answer) Assert the answer answer for question, in case it is tested with
4428 a preprocessor conditional such as `#if #question(answer)'.
4429 `-A-' disables the standard assertions that normally describe the target
4432 \labelwidthstring 00.00.0000
4437 \begin_inset LatexCommand \index{-Umacro}
4443 Undefine macro macro.
4444 `-U' options are evaluated after all `-D' options, but before any `-include'
4445 and `-imacros' options.
4447 \labelwidthstring 00.00.0000
4452 \begin_inset LatexCommand \index{-dM}
4458 Tell the preprocessor to output only a list of the macro definitions that
4459 are in effect at the end of preprocessing.
4460 Used with the `-E' option.
4462 \labelwidthstring 00.00.0000
4467 \begin_inset LatexCommand \index{-dD}
4473 Tell the preprocessor to pass all macro definitions into the output, in
4474 their proper sequence in the rest of the output.
4476 \labelwidthstring 00.00.0000
4481 \begin_inset LatexCommand \index{-dN}
4492 Like `-dD' except that the macro arguments and contents are omitted.
4493 Only `#define name' is included in the output.
4494 \layout Subsubsection
4497 \begin_inset LatexCommand \index{Options linker}
4502 \begin_inset LatexCommand \index{Linker options}
4508 \labelwidthstring 00.00.0000
4528 \begin_inset LatexCommand \index{-\/-lib-path}
4533 \begin_inset LatexCommand \index{-L -\/-lib-path}
4542 <absolute path to additional libraries> This option is passed to the linkage
4543 editor's additional libraries
4544 \begin_inset LatexCommand \index{Libraries}
4549 The path name must be absolute.
4550 Additional library files may be specified in the command line.
4551 See section Compiling programs for more details.
4553 \labelwidthstring 00.00.0000
4570 \begin_inset LatexCommand \index{-\/-xram-loc}
4574 <Value> The start location of the external ram
4575 \begin_inset LatexCommand \index{xdata}
4579 , default value is 0.
4580 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4590 -xram-loc 0x8000 or -
4602 \labelwidthstring 00.00.0000
4619 \begin_inset LatexCommand \index{-\/-code-loc}
4623 <Value> The start location of the code
4624 \begin_inset LatexCommand \index{code}
4628 segment, default value 0.
4629 Note when this option is used the interrupt vector table is also relocated
4630 to the given address.
4631 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4641 -code-loc 0x8000 or -
4653 \labelwidthstring 00.00.0000
4670 \begin_inset LatexCommand \index{-\/-stack-loc}
4674 <Value> By default the stack
4675 \begin_inset LatexCommand \index{stack}
4679 is placed after the data segment.
4680 Using this option the stack can be placed anywhere in the internal memory
4682 The value entered can be in Hexadecimal or Decimal format, e.g.
4693 -stack-loc 0x20 or -
4704 Since the sp register is incremented before a push or call, the initial
4705 sp will be set to one byte prior the provided value.
4706 The provided value should not overlap any other memory areas such as used
4707 register banks or the data segment and with enough space for the current
4710 \labelwidthstring 00.00.0000
4727 \begin_inset LatexCommand \index{-\/-data-loc}
4731 <Value> The start location of the internal ram data
4732 \begin_inset LatexCommand \index{data}
4737 The value entered can be in Hexadecimal or Decimal format, eg.
4759 (By default, the start location of the internal ram data segment is set
4760 as low as possible in memory, taking into account the used register banks
4761 and the bit segment at address 0x20.
4762 For example if register banks 0 and 1 are used without bit variables, the
4763 data segment will be set, if -
4773 -data-loc is not used, to location 0x10.)
4775 \labelwidthstring 00.00.0000
4792 \begin_inset LatexCommand \index{-\/-idata-loc}
4796 <Value> The start location of the indirectly addressable internal ram
4797 \begin_inset LatexCommand \index{idata}
4801 , default value is 0x80.
4802 The value entered can be in Hexadecimal or Decimal format, eg.
4813 -idata-loc 0x88 or -
4825 \labelwidthstring 00.00.0000
4840 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
4849 The linker output (final object code) is in Intel Hex format.
4850 \begin_inset LatexCommand \index{Intel hex format}
4854 (This is the default option).
4856 \labelwidthstring 00.00.0000
4871 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4880 The linker output (final object code) is in Motorola S19 format
4881 \begin_inset LatexCommand \index{Motorola S19 format}
4886 \layout Subsubsection
4889 \begin_inset LatexCommand \index{Options MCS51}
4894 \begin_inset LatexCommand \index{MCS51 options}
4900 \labelwidthstring 00.00.0000
4915 \begin_inset LatexCommand \index{-\/-model-large}
4921 Generate code for Large model programs see section Memory Models for more
4923 If this option is used all source files in the project should be compiled
4925 In addition the standard library routines are compiled with small model,
4926 they will need to be recompiled.
4928 \labelwidthstring 00.00.0000
4943 \begin_inset LatexCommand \index{-\/-model-small}
4954 Generate code for Small Model programs see section Memory Models for more
4956 This is the default model.
4957 \layout Subsubsection
4960 \begin_inset LatexCommand \index{Options DS390}
4965 \begin_inset LatexCommand \index{DS390 options}
4971 \labelwidthstring 00.00.0000
4988 \begin_inset LatexCommand \index{-\/-model-flat24}
4998 Generate 24-bit flat mode code.
4999 This is the one and only that the ds390 code generator supports right now
5000 and is default when using
5005 See section Memory Models for more details.
5007 \labelwidthstring 00.00.0000
5024 \begin_inset LatexCommand \index{-\/-stack-10bit}
5028 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
5029 This is the one and only that the ds390 code generator supports right now
5030 and is default when using
5035 In this mode, the stack is located in the lower 1K of the internal RAM,
5036 which is mapped to 0x400000.
5037 Note that the support is incomplete, since it still uses a single byte
5038 as the stack pointer.
5039 This means that only the lower 256 bytes of the potential 1K stack space
5040 will actually be used.
5041 However, this does allow you to reclaim the precious 256 bytes of low RAM
5042 for use for the DATA and IDATA segments.
5043 The compiler will not generate any code to put the processor into 10 bit
5045 It is important to ensure that the processor is in this mode before calling
5046 any re-entrant functions compiled with this option.
5047 In principle, this should work with the
5060 \begin_inset LatexCommand \index{-\/-stack-auto}
5066 option, but that has not been tested.
5067 It is incompatible with the
5080 \begin_inset LatexCommand \index{-\/-xstack}
5087 It also only makes sense if the processor is in 24 bit contiguous addressing
5100 -model-flat24 option
5103 \layout Subsubsection
5105 Optimization Options
5106 \begin_inset LatexCommand \index{Options optimization}
5111 \begin_inset LatexCommand \index{Optimization options}
5117 \labelwidthstring 00.00.0000
5132 \begin_inset LatexCommand \index{-\/-nogcse}
5138 Will not do global subexpression elimination, this option may be used when
5139 the compiler creates undesirably large stack/data spaces to store compiler
5141 A warning message will be generated when this happens and the compiler
5142 will indicate the number of extra bytes it allocated.
5143 It recommended that this option NOT be used, #pragma\SpecialChar ~
5145 \begin_inset LatexCommand \index{\#pragma NOGCSE}
5149 can be used to turn off global subexpression elimination
5150 \begin_inset LatexCommand \index{Subexpression elimination}
5154 for a given function only.
5156 \labelwidthstring 00.00.0000
5171 \begin_inset LatexCommand \index{-\/-noinvariant}
5177 Will not do loop invariant optimizations, this may be turned off for reasons
5178 explained for the previous option.
5179 For more details of loop optimizations performed see section Loop Invariants.It
5180 recommended that this option NOT be used, #pragma\SpecialChar ~
5182 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
5186 can be used to turn off invariant optimizations for a given function only.
5188 \labelwidthstring 00.00.0000
5203 \begin_inset LatexCommand \index{-\/-noinduction}
5209 Will not do loop induction optimizations, see section strength reduction
5210 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
5213 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
5217 can be used to turn off induction optimizations for a given function only.
5219 \labelwidthstring 00.00.0000
5234 \begin_inset LatexCommand \index{-\/-nojtbound}
5245 Will not generate boundary condition check when switch statements
5246 \begin_inset LatexCommand \index{switch statement}
5250 are implemented using jump-tables.
5251 See section Switch Statements for more details.
5252 It is recommended that this option is NOT used, #pragma\SpecialChar ~
5254 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
5258 can be used to turn off boundary checking for jump tables for a given function
5261 \labelwidthstring 00.00.0000
5276 \begin_inset LatexCommand \index{-\/-noloopreverse}
5285 Will not do loop reversal
5286 \begin_inset LatexCommand \index{Loop reversing}
5292 \labelwidthstring 00.00.0000
5309 \begin_inset LatexCommand \index{-\/-nolabelopt }
5313 Will not optimize labels (makes the dumpfiles more readable).
5315 \labelwidthstring 00.00.0000
5330 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
5336 Will not memcpy initialized data in far space from code space.
5337 This saves a few bytes in code space if you don't have initialized data.
5338 \layout Subsubsection
5341 \begin_inset LatexCommand \index{Options other}
5347 \labelwidthstring 00.00.0000
5363 \begin_inset LatexCommand \index{-\/-compile-only}
5368 \begin_inset LatexCommand \index{-c -\/-compile-only}
5374 will compile and assemble the source, but will not call the linkage editor.
5376 \labelwidthstring 00.00.0000
5395 \begin_inset LatexCommand \index{-\/-c1mode}
5401 reads the preprocessed source from standard input and compiles it.
5402 The file name for the assembler output must be specified using the -o option.
5404 \labelwidthstring 00.00.0000
5409 \begin_inset LatexCommand \index{-E}
5415 Run only the C preprocessor.
5416 Preprocess all the C source files specified and output the results to standard
5419 \labelwidthstring 00.00.0000
5425 \begin_inset LatexCommand \index{-o <path/file>}
5431 The output path resp.
5432 file where everything will be placed.
5433 If the parameter is a path, it must have a trailing slash (or backslash
5434 for the Windows binaries) to be recognized as a path.
5437 \labelwidthstring 00.00.0000
5452 \begin_inset LatexCommand \index{-\/-stack-auto}
5463 All functions in the source file will be compiled as
5468 \begin_inset LatexCommand \index{reentrant}
5473 the parameters and local variables will be allocated on the stack
5474 \begin_inset LatexCommand \index{stack}
5479 see section Parameters and Local Variables for more details.
5480 If this option is used all source files in the project should be compiled
5484 \labelwidthstring 00.00.0000
5499 \begin_inset LatexCommand \index{-\/-xstack}
5505 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5506 variables and passing parameters.
5507 See section on external stack for more details.
5509 \labelwidthstring 00.00.0000
5524 \begin_inset LatexCommand \index{-\/-callee-saves}
5529 \begin_inset LatexCommand \index{function prologue}
5533 function1[,function2][,function3]....
5536 The compiler by default uses a caller saves convention for register saving
5537 across function calls, however this can cause unneccessary register pushing
5538 & popping when calling small functions from larger functions.
5539 This option can be used to switch the register saving convention for the
5540 function names specified.
5541 The compiler will not save registers when calling these functions, no extra
5542 code will be generated at the entry & exit for these functions to save
5543 & restore the registers used by these functions, this can SUBSTANTIALLY
5544 reduce code & improve run time performance of the generated code.
5545 In the future the compiler (with interprocedural analysis) will be able
5546 to determine the appropriate scheme to use for each function call.
5547 DO NOT use this option for built-in functions such as _mulint..., if this
5548 option is used for a library function the appropriate library function
5549 needs to be recompiled with the same option.
5550 If the project consists of multiple source files then all the source file
5551 should be compiled with the same -
5561 -callee-saves option string.
5562 Also see #pragma\SpecialChar ~
5564 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
5570 \labelwidthstring 00.00.0000
5585 \begin_inset LatexCommand \index{-\/-debug}
5594 When this option is used the compiler will generate debug information, that
5595 can be used with the SDCDB.
5596 The debug information is collected in a file with .cdb extension.
5597 For more information see documentation for SDCDB.
5599 \labelwidthstring 00.00.0000
5616 \begin_inset LatexCommand \index{-\/-peep-file}
5620 <filename> This option can be used to use additional rules to be used by
5621 the peep hole optimizer.
5622 See section Peep Hole optimizations for details on how to write these rules.
5624 \labelwidthstring 00.00.0000
5629 \begin_inset LatexCommand \index{-S}
5640 Stop after the stage of compilation proper; do not assemble.
5641 The output is an assembler code file for the input file specified.
5643 \labelwidthstring 00.00.0000
5647 -Wa_asmOption[,asmOption]
5650 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
5655 Pass the asmOption to the assembler.
5657 \labelwidthstring 00.00.0000
5661 -Wl_linkOption[,linkOption]
5664 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
5669 Pass the linkOption to the linker.
5671 \labelwidthstring 00.00.0000
5686 \begin_inset LatexCommand \index{-\/-int-long-reent}
5692 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
5693 Note by default these libraries are compiled as non-reentrant.
5694 See section Installation for more details.
5696 \labelwidthstring 00.00.0000
5711 \begin_inset LatexCommand \index{-\/-cyclomatic}
5720 This option will cause the compiler to generate an information message for
5721 each function in the source file.
5722 The message contains some
5726 information about the function.
5727 The number of edges and nodes the compiler detected in the control flow
5728 graph of the function, and most importantly the
5730 cyclomatic complexity
5731 \begin_inset LatexCommand \index{Cyclomatic complexity}
5737 see section on Cyclomatic Complexity for more details.
5739 \labelwidthstring 00.00.0000
5754 \begin_inset LatexCommand \index{-\/-float-reent}
5763 Floating point library is compiled as reentrant
5764 \begin_inset LatexCommand \index{reentrant}
5768 .See section Installation for more details.
5770 \labelwidthstring 00.00.0000
5785 \begin_inset LatexCommand \index{-\/-nooverlay}
5791 The compiler will not overlay parameters and local variables of any function,
5792 see section Parameters and local variables for more details.
5794 \labelwidthstring 00.00.0000
5809 \begin_inset LatexCommand \index{-\/-main-return}
5815 This option can be used when the code generated is called by a monitor
5817 The compiler will generate a 'ret' upon return from the 'main'
5818 \begin_inset LatexCommand \index{main return}
5823 The default option is to lock up i.e.
5826 \labelwidthstring 00.00.0000
5841 \begin_inset LatexCommand \index{-\/-no-peep}
5847 Disable peep-hole optimization.
5849 \labelwidthstring 00.00.0000
5864 \begin_inset LatexCommand \index{-\/-peep-asm}
5870 Pass the inline assembler code through the peep hole optimizer.
5871 This can cause unexpected changes to inline assembler code, please go through
5872 the peephole optimizer
5873 \begin_inset LatexCommand \index{Peephole optimizer}
5877 rules defined in the source file tree '<target>/peeph.def' before using
5880 \labelwidthstring 00.00.0000
5897 \begin_inset LatexCommand \index{-\/-iram-size<Value>}
5901 Causes the linker to check if the internal ram usage is within limits of
5904 \labelwidthstring 00.00.0000
5921 \begin_inset LatexCommand \index{-\/-xram-size<Value>}
5925 Causes the linker to check if the external ram usage is within limits of
5928 \labelwidthstring 00.00.0000
5945 \begin_inset LatexCommand \index{-\/-code-size<Value>}
5949 Causes the linker to check if the code usage is within limits of the given
5952 \labelwidthstring 00.00.0000
5967 \begin_inset LatexCommand \index{-\/-nostdincl}
5973 This will prevent the compiler from passing on the default include path
5974 to the preprocessor.
5976 \labelwidthstring 00.00.0000
5991 \begin_inset LatexCommand \index{-\/-nostdlib}
5997 This will prevent the compiler from passing on the default library
5998 \begin_inset LatexCommand \index{Libraries}
6004 \labelwidthstring 00.00.0000
6019 \begin_inset LatexCommand \index{-\/-verbose}
6025 Shows the various actions the compiler is performing.
6027 \labelwidthstring 00.00.0000
6032 \begin_inset LatexCommand \index{-V}
6038 Shows the actual commands the compiler is executing.
6040 \labelwidthstring 00.00.0000
6055 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
6061 Hides your ugly and inefficient c-code from the asm file, so you can always
6062 blame the compiler :).
6064 \labelwidthstring 00.00.0000
6079 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
6085 Include i-codes in the asm file.
6086 Sounds like noise but is most helpfull for debugging the compiler itself.
6088 \labelwidthstring 00.00.0000
6103 \begin_inset LatexCommand \index{-\/-less-pedantic}
6109 Disable some of the more pedantic warnings (jwk burps: please be more specific
6112 \labelwidthstring 00.00.0000
6127 \begin_inset LatexCommand \index{-\/-print-search-dirs}
6133 Display the directories in the compiler's search path
6134 \layout Subsubsection
6136 Intermediate Dump Options
6137 \begin_inset LatexCommand \index{Options intermediate dump}
6142 \begin_inset LatexCommand \index{Intermediate dump options}
6149 The following options are provided for the purpose of retargetting and debugging
6151 These provided a means to dump the intermediate code (iCode
6152 \begin_inset LatexCommand \index{iCode}
6156 ) generated by the compiler in human readable form at various stages of
6157 the compilation process.
6160 \labelwidthstring 00.00.0000
6175 \begin_inset LatexCommand \index{-\/-dumpraw}
6181 This option will cause the compiler to dump the intermediate code into
6184 <source filename>.dumpraw
6186 just after the intermediate code has been generated for a function, i.e.
6187 before any optimizations are done.
6189 \begin_inset LatexCommand \index{Basic blocks}
6193 at this stage ordered in the depth first number, so they may not be in
6194 sequence of execution.
6196 \labelwidthstring 00.00.0000
6211 \begin_inset LatexCommand \index{-\/-dumpgcse}
6217 Will create a dump of iCode's, after global subexpression elimination
6218 \begin_inset LatexCommand \index{Global subexpression elimination}
6224 <source filename>.dumpgcse.
6226 \labelwidthstring 00.00.0000
6241 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
6247 Will create a dump of iCode's, after deadcode elimination
6248 \begin_inset LatexCommand \index{Dead-code elimination}
6254 <source filename>.dumpdeadcode.
6256 \labelwidthstring 00.00.0000
6271 \begin_inset LatexCommand \index{-\/-dumploop}
6280 Will create a dump of iCode's, after loop optimizations
6281 \begin_inset LatexCommand \index{Loop optimization}
6287 <source filename>.dumploop.
6289 \labelwidthstring 00.00.0000
6304 \begin_inset LatexCommand \index{-\/-dumprange}
6313 Will create a dump of iCode's, after live range analysis
6314 \begin_inset LatexCommand \index{Live range analysis}
6320 <source filename>.dumprange.
6322 \labelwidthstring 00.00.0000
6337 \begin_inset LatexCommand \index{-\/-dumlrange}
6343 Will dump the life ranges
6344 \begin_inset LatexCommand \index{Live range analysis}
6350 \labelwidthstring 00.00.0000
6365 \begin_inset LatexCommand \index{-\/-dumpregassign}
6374 Will create a dump of iCode's, after register assignment
6375 \begin_inset LatexCommand \index{Register assignment}
6381 <source filename>.dumprassgn.
6383 \labelwidthstring 00.00.0000
6398 \begin_inset LatexCommand \index{-\/-dumplrange}
6404 Will create a dump of the live ranges of iTemp's
6406 \labelwidthstring 00.00.0000
6421 \begin_inset LatexCommand \index{-\/-dumpall}
6432 Will cause all the above mentioned dumps to be created.
6435 Environment variables
6436 \begin_inset LatexCommand \index{Environment variables}
6443 SDCC recognizes the following environment variables:
6445 \labelwidthstring 00.00.0000
6450 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
6456 SDCC installs a signal handler
6457 \begin_inset LatexCommand \index{signal handler}
6461 to be able to delete temporary files after an user break (^C) or an exception.
6462 If this environment variable is set, SDCC won't install the signal handler
6463 in order to be able to debug SDCC.
6465 \labelwidthstring 00.00.0000
6470 \begin_inset LatexCommand \index{TMP}
6476 \begin_inset LatexCommand \index{TEMP}
6482 \begin_inset LatexCommand \index{TMPDIR}
6488 Path, where temporary files will be created.
6489 The order of the variables is the search order.
6490 In a standard *nix environment these variables are not set, and there's
6491 no need to set them.
6492 On Windows it's recommended to set one of them.
6494 \labelwidthstring 00.00.0000
6499 \begin_inset LatexCommand \index{SDCC\_HOME}
6506 \begin_inset Quotes sld
6509 2.3 Install and search paths
6510 \begin_inset Quotes srd
6515 \labelwidthstring 00.00.0000
6520 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
6527 \begin_inset Quotes sld
6530 2.3 Install and search paths
6531 \begin_inset Quotes srd
6536 \labelwidthstring 00.00.0000
6541 \begin_inset LatexCommand \index{SDCC\_LIB}
6548 \begin_inset Quotes sld
6551 2.3 Install and search paths
6552 \begin_inset Quotes srd
6558 There are some more environment variables recognized by SDCC, but these
6559 are solely used for debugging purposes.
6560 They can change or disappear very quickly, and will never be documentated.
6563 MCS51/DS390 Storage Class
6564 \begin_inset LatexCommand \index{Storage class}
6571 In addition to the ANSI storage classes SDCC allows the following MCS51
6572 specific storage classes.
6573 \layout Subsubsection
6576 \begin_inset LatexCommand \index{xdata}
6583 Variables declared with this storage class will be placed in the extern
6589 storage class for Large Memory model, e.g.:
6595 xdata unsigned char xduc;
6596 \layout Subsubsection
6599 \begin_inset LatexCommand \index{data}
6610 storage class for Small Memory model.
6611 Variables declared with this storage class will be allocated in the internal
6619 \layout Subsubsection
6622 \begin_inset LatexCommand \index{idata}
6629 Variables declared with this storage class will be allocated into the indirectly
6630 addressable portion of the internal ram of a 8051, e.g.:
6637 \layout Subsubsection
6640 \begin_inset LatexCommand \index{bit}
6647 This is a data-type and a storage class specifier.
6648 When a variable is declared as a bit, it is allocated into the bit addressable
6649 memory of 8051, e.g.:
6656 \layout Subsubsection
6659 \begin_inset LatexCommand \index{sfr}
6664 \begin_inset LatexCommand \index{sbit}
6671 Like the bit keyword,
6675 signifies both a data-type and storage class, they are used to describe
6676 the special function registers and special bit variables of a 8051, eg:
6682 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
6684 sbit at 0xd7 CY; /* CY (Carry Flag
6685 \begin_inset LatexCommand \index{Flags}
6690 \begin_inset LatexCommand \index{Carry flag}
6698 \begin_inset LatexCommand \index{Pointers}
6705 SDCC allows (via language extensions) pointers to explicitly point to any
6706 of the memory spaces
6707 \begin_inset LatexCommand \index{Memory model}
6712 In addition to the explicit pointers, the compiler uses (by default) generic
6713 pointers which can be used to point to any of the memory spaces.
6717 Pointer declaration examples:
6726 /* pointer physically in xternal ram pointing to object in internal ram
6729 data unsigned char * xdata p;
6733 /* pointer physically in code rom pointing to data in xdata space */
6735 xdata unsigned char * code p;
6739 /* pointer physically in code space pointing to data in code space */
6741 code unsigned char * code p;
6745 /* the folowing is a generic pointer physically located in xdata space */
6756 Well you get the idea.
6761 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
6774 The highest order byte of the
6778 pointers contains the data space information.
6779 Assembler support routines are called whenever data is stored or retrieved
6785 These are useful for developing reusable library
6786 \begin_inset LatexCommand \index{Libraries}
6791 Explicitly specifying the pointer type will generate the most efficient
6796 \begin_inset LatexCommand \index{Parameters}
6801 \begin_inset LatexCommand \index{Local variable}
6808 Automatic (local) variables and parameters to functions can either be placed
6809 on the stack or in data-space.
6810 The default action of the compiler is to place these variables in the internal
6811 RAM (for small model) or external RAM (for large model).
6812 This in fact makes them
6815 \begin_inset LatexCommand \index{static}
6821 so by default functions are non-reentrant
6822 \begin_inset LatexCommand \index{reentrant}
6830 They can be placed on the stack
6831 \begin_inset LatexCommand \index{stack}
6848 \begin_inset LatexCommand \index{-\/-stack-auto}
6854 option or by using the
6857 \begin_inset LatexCommand \index{reentrant}
6863 keyword in the function declaration, e.g.:
6872 unsigned char foo(char i) reentrant
6885 Since stack space on 8051 is limited, the
6903 option should be used sparingly.
6904 Note that the reentrant keyword just means that the parameters & local
6905 variables will be allocated to the stack, it
6909 mean that the function is register bank independent.
6913 Local variables can be assigned storage classes and absolute
6914 \begin_inset LatexCommand \index{Absolute addressing}
6924 unsigned char foo() {
6930 xdata unsigned char i;
6942 data at 0x31 unsiged char j;
6957 In the above example the variable
6961 will be allocated in the external ram,
6965 in bit addressable space and
6984 or when a function is declared as
6988 this should only be done for static variables.
6991 Parameters however are not allowed any storage class, (storage classes for
6992 parameters will be ignored), their allocation is governed by the memory
6993 model in use, and the reentrancy options.
6997 \begin_inset LatexCommand \index{Overlaying}
7005 \begin_inset LatexCommand \index{reentrant}
7009 functions SDCC will try to reduce internal ram space usage by overlaying
7010 parameters and local variables of a function (if possible).
7011 Parameters and local variables of a function will be allocated to an overlayabl
7012 e segment if the function has
7014 no other function calls and the function is non-reentrant and the memory
7016 \begin_inset LatexCommand \index{Memory model}
7023 If an explicit storage class
7024 \begin_inset LatexCommand \index{Storage class}
7028 is specified for a local variable, it will NOT be overlayed.
7031 Note that the compiler (not the linkage editor) makes the decision for overlayin
7033 Functions that are called from an interrupt service routine should be preceded
7034 by a #pragma\SpecialChar ~
7036 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7040 if they are not reentrant.
7043 Also note that the compiler does not do any processing of inline
7044 \begin_inset LatexCommand \index{inline}
7048 assembler code, so the compiler might incorrectly assign local variables
7049 and parameters of a function into the overlay segment if the inline assembler
7050 code calls other c-functions that might use the overlay.
7051 In that case the #pragma\SpecialChar ~
7052 NOOVERLAY should be used.
7055 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
7057 \begin_inset LatexCommand \index{Multiplication}
7062 \begin_inset LatexCommand \index{Division}
7066 will NOT be overlayed since these are implemented using external functions,
7076 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7082 void set_error(unsigned char errcd)
7098 void some_isr () interrupt
7099 \begin_inset LatexCommand \index{interrupt}
7104 \begin_inset LatexCommand \index{using}
7137 In the above example the parameter
7145 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
7147 not present, this could cause unpredictable runtime behavior when called
7149 The #pragma\SpecialChar ~
7150 NOOVERLAY ensures that the parameters and local variables for
7151 the function are NOT overlayed.
7154 Interrupt Service Routines
7157 SDCC allows interrupt service routines to be coded in C, with some extended
7164 void timer_isr (void) interrupt 2 using 1
7177 The number following the
7180 \begin_inset LatexCommand \index{interrupt}
7186 keyword is the interrupt number this routine will service.
7187 The compiler will insert a call to this routine in the interrupt vector
7188 table for the interrupt number specified.
7193 keyword is used to tell the compiler to use the specified register bank
7194 (8051 specific) when generating code for this function.
7195 Note that when some function is called from an interrupt service routine
7196 it should be preceded by a #pragma\SpecialChar ~
7198 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7202 if it is not reentrant.
7203 A special note here, int (16 bit) and long (32 bit) integer division
7204 \begin_inset LatexCommand \index{Division}
7209 \begin_inset LatexCommand \index{Multiplication}
7214 \begin_inset LatexCommand \index{Modulus}
7218 operations are implemented using external support routines developed in
7219 ANSI-C, if an interrupt service routine needs to do any of these operations
7220 then the support routines (as mentioned in a following section) will have
7221 to be recompiled using the
7234 \begin_inset LatexCommand \index{-\/-stack-auto}
7240 option and the source file will need to be compiled using the
7255 \begin_inset LatexCommand \index{-\/-int-long-rent}
7262 If you have multiple source files in your project, interrupt service routines
7263 can be present in any of them, but a prototype of the isr MUST be present
7264 or included in the file that contains the function
7271 Interrupt Numbers and the corresponding address & descriptions for the Standard
7272 8051 are listed below.
7273 SDCC will automatically adjust the interrupt vector table to the maximum
7274 interrupt number specified.
7280 \begin_inset Tabular
7281 <lyxtabular version="3" rows="6" columns="3">
7283 <column alignment="center" valignment="top" leftline="true" width="0in">
7284 <column alignment="center" valignment="top" leftline="true" width="0in">
7285 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
7286 <row topline="true" bottomline="true">
7287 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7295 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7303 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7312 <row topline="true">
7313 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7338 <row topline="true">
7339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7355 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7364 <row topline="true">
7365 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7390 <row topline="true">
7391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7416 <row topline="true" bottomline="true">
7417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7450 If the interrupt service routine is defined without
7453 \begin_inset LatexCommand \index{using}
7459 a register bank or with register bank 0 (using 0), the compiler will save
7460 the registers used by itself on the stack upon entry and restore them at
7461 exit, however if such an interrupt service routine calls another function
7462 then the entire register bank will be saved on the stack.
7463 This scheme may be advantageous for small interrupt service routines which
7464 have low register usage.
7467 If the interrupt service routine is defined to be using a specific register
7472 are save and restored, if such an interrupt service routine calls another
7473 function (using another register bank) then the entire register bank of
7474 the called function will be saved on the stack.
7475 This scheme is recommended for larger interrupt service routines.
7478 Calling other functions from an interrupt service routine is not recommended,
7479 avoid it if possible.
7483 Also see the _naked modifier.
7491 <TODO: this isn't implemented at all!>
7497 A special keyword may be associated with a function declaring it as
7502 SDCC will generate code to disable all interrupts upon entry to a critical
7503 function and enable them back before returning.
7504 Note that nesting critical functions may cause unpredictable results.
7529 The critical attribute maybe used with other attributes like
7535 \begin_inset LatexCommand \index{Naked functions}
7542 A special keyword may be associated with a function declaring it as
7545 \begin_inset LatexCommand \index{\_naked}
7556 function modifier attribute prevents the compiler from generating prologue
7557 \begin_inset LatexCommand \index{function prologue}
7562 \begin_inset LatexCommand \index{function epilogue}
7566 code for that function.
7567 This means that the user is entirely responsible for such things as saving
7568 any registers that may need to be preserved, selecting the proper register
7569 bank, generating the
7573 instruction at the end, etc.
7574 Practically, this means that the contents of the function must be written
7575 in inline assembler.
7576 This is particularly useful for interrupt functions, which can have a large
7577 (and often unnecessary) prologue/epilogue.
7578 For example, compare the code generated by these two functions:
7584 data unsigned char counter;
7586 void simpleInterrupt(void) interrupt
7587 \begin_inset LatexCommand \index{interrupt}
7605 void nakedInterrupt(void) interrupt 2 _naked
7614 \begin_inset LatexCommand \index{\_asm}
7643 ; MUST explicitly include ret in _naked function.
7650 \begin_inset LatexCommand \index{\_endasm}
7662 For an 8051 target, the generated simpleInterrupt looks like:
7807 whereas nakedInterrupt looks like:
7832 ; MUST explicitly include ret(i) in _naked function.
7838 While there is nothing preventing you from writing C code inside a _naked
7839 function, there are many ways to shoot yourself in the foot doing this,
7840 and it is recommended that you stick to inline assembler.
7843 Functions using private banks
7844 \begin_inset LatexCommand \index{bank}
7854 \begin_inset LatexCommand \index{using}
7860 attribute (which tells the compiler to use a register bank other than the
7861 default bank zero) should only be applied to
7864 \begin_inset LatexCommand \index{interrupt}
7870 functions (see note 1 below).
7871 This will in most circumstances make the generated ISR code more efficient
7872 since it will not have to save registers on the stack.
7879 attribute will have no effect on the generated code for a
7883 function (but may occasionally be useful anyway
7889 possible exception: if a function is called ONLY from 'interrupt' functions
7890 using a particular bank, it can be declared with the same 'using' attribute
7891 as the calling 'interrupt' functions.
7892 For instance, if you have several ISRs using bank one, and all of them
7893 call memcpy(), it might make sense to create a specialized version of memcpy()
7894 'using 1', since this would prevent the ISR from having to save bank zero
7895 to the stack on entry and switch to bank zero before calling the function
7902 (pending: I don't think this has been done yet)
7909 function using a non-zero bank will assume that it can trash that register
7910 bank, and will not save it.
7911 Since high-priority interrupts
7912 \begin_inset LatexCommand \index{interrupt priority}
7916 can interrupt low-priority ones on the 8051 and friends, this means that
7917 if a high-priority ISR
7921 a particular bank occurs while processing a low-priority ISR
7925 the same bank, terrible and bad things can happen.
7926 To prevent this, no single register bank should be
7930 by both a high priority and a low priority ISR.
7931 This is probably most easily done by having all high priority ISRs use
7932 one bank and all low priority ISRs use another.
7933 If you have an ISR which can change priority at runtime, you're on your
7934 own: I suggest using the default bank zero and taking the small performance
7938 It is most efficient if your ISR calls no other functions.
7939 If your ISR must call other functions, it is most efficient if those functions
7940 use the same bank as the ISR (see note 1 below); the next best is if the
7941 called functions use bank zero.
7942 It is very inefficient to call a function using a different, non-zero bank
7948 \begin_inset LatexCommand \index{Absolute addressing}
7955 Data items can be assigned an absolute address with the
7958 \begin_inset LatexCommand \index{at}
7964 keyword, in addition to a storage class, e.g.:
7971 \begin_inset LatexCommand \index{xdata}
7976 \begin_inset LatexCommand \index{at}
7980 0x8000 unsigned char PORTA_8255 ;
7986 In the above example the PORTA_8255 will be allocated to the location 0x8000
7987 of the external ram.
7988 Note that this feature is provided to give the programmer access to
7992 devices attached to the controller.
7993 The compiler does not actually reserve any space for variables declared
7994 in this way (they are implemented with an equate in the assembler).
7995 Thus it is left to the programmer to make sure there are no overlaps with
7996 other variables that are declared without the absolute address.
7997 The assembler listing file (.lst
7998 \begin_inset LatexCommand \index{.lst}
8002 ) and the linker output files (.rst
8003 \begin_inset LatexCommand \index{.rst}
8008 \begin_inset LatexCommand \index{.map}
8012 ) are a good places to look for such overlaps.
8016 Absolute address can be specified for variables in all storage classes,
8024 \begin_inset LatexCommand \index{bit}
8029 \begin_inset LatexCommand \index{at}
8039 The above example will allocate the variable at offset 0x02 in the bit-addressab
8041 There is no real advantage to assigning absolute addresses to variables
8042 in this manner, unless you want strict control over all the variables allocated.
8046 \begin_inset LatexCommand \index{Startup code}
8053 The compiler inserts a call to the C routine
8055 _sdcc_external_startup()
8056 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
8065 at the start of the CODE area.
8066 This routine is in the runtime library
8067 \begin_inset LatexCommand \index{Runtime library}
8072 By default this routine returns 0, if this routine returns a non-zero value,
8073 the static & global variable initialization will be skipped and the function
8074 main will be invoked Other wise static & global variables will be initialized
8075 before the function main is invoked.
8078 _sdcc_external_startup()
8080 routine to your program to override the default if you need to setup hardware
8081 or perform some other critical operation prior to static & global variable
8085 Inline Assembler Code
8086 \begin_inset LatexCommand \index{Assembler routines}
8093 SDCC allows the use of in-line assembler with a few restriction as regards
8095 All labels defined within inline assembler code
8103 where nnnn is a number less than 100 (which implies a limit of utmost 100
8104 inline assembler labels
8112 It is strongly recommended that each assembly instruction (including labels)
8113 be placed in a separate line (as the example shows).
8127 \begin_inset LatexCommand \index{-\/-peep-asm}
8133 command line option is used, the inline assembler code will be passed through
8134 the peephole optimizer
8135 \begin_inset LatexCommand \index{Peephole optimizer}
8140 This might cause some unexpected changes in the inline assembler code.
8141 Please go throught the peephole optimizer rules defined in file
8145 carefully before using this option.
8152 \begin_inset LatexCommand \index{\_asm}
8182 \begin_inset LatexCommand \index{\_endasm}
8195 The inline assembler code can contain any valid code understood by the assembler
8196 , this includes any assembler directives and comment lines.
8197 The compiler does not do any validation of the code within the
8207 Inline assembler code cannot reference any C-Labels, however it can reference
8209 \begin_inset LatexCommand \index{Labels}
8213 defined by the inline assembler, e.g.:
8239 ; some assembler code
8259 /* some more c code */
8261 clabel:\SpecialChar ~
8263 /* inline assembler cannot reference this label */
8275 $0003: ;label (can be reference by inline assembler only)
8287 /* some more c code */
8295 In other words inline assembly code can access labels defined in inline
8296 assembly within the scope of the funtion.
8300 The same goes the other way, ie.
8301 labels defines in inline assembly CANNOT be accessed by C statements.
8305 \begin_inset LatexCommand \index{int (16 bit)}
8310 \begin_inset LatexCommand \index{long (32 bit)}
8317 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
8318 multiplication and modulus operations are implemented by support routines.
8319 These support routines are all developed in ANSI-C to facilitate porting
8320 to other MCUs, although some model specific assembler optimations are used.
8321 The following files contain the described routine, all of them can be found
8322 in <installdir>/share/sdcc/lib.
8328 <pending: tabularise this>
8334 _mulint.c - 16 bit multiplication
8336 _divsint.c - signed 16 bit division (calls _divuint)
8338 _divuint.c - unsigned 16 bit division
8340 _modsint.c - signed 16 bit modulus (call _moduint)
8342 _moduint.c - unsigned 16 bit modulus
8344 _mullong.c - 32 bit multiplication
8346 _divslong.c - signed 32 division (calls _divulong)
8348 _divulong.c - unsigned 32 division
8350 _modslong.c - signed 32 bit modulus (calls _modulong)
8352 _modulong.c - unsigned 32 bit modulus
8360 Since they are compiled as
8365 \begin_inset LatexCommand \index{reentrant}
8370 \begin_inset LatexCommand \index{interrupt}
8374 service routines should not do any of the above operations.
8375 If this is unavoidable then the above routines will need to be compiled
8389 \begin_inset LatexCommand \index{-\/-stack-auto}
8395 option, after which the source program will have to be compiled with
8408 \begin_inset LatexCommand \index{-\/-int-long-rent}
8417 Floating Point Support
8418 \begin_inset LatexCommand \index{Floating point support}
8425 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
8426 point support routines are derived from gcc's floatlib.c and consists of
8427 the following routines:
8433 <pending: tabularise this>
8439 _fsadd.c - add floating point numbers
8441 _fssub.c - subtract floating point numbers
8443 _fsdiv.c - divide floating point numbers
8445 _fsmul.c - multiply floating point numbers
8447 _fs2uchar.c - convert floating point to unsigned char
8449 _fs2char.c - convert floating point to signed char
8451 _fs2uint.c - convert floating point to unsigned int
8453 _fs2int.c - convert floating point to signed int
8455 _fs2ulong.c - convert floating point to unsigned long
8457 _fs2long.c - convert floating point to signed long
8459 _uchar2fs.c - convert unsigned char to floating point
8461 _char2fs.c - convert char to floating point number
8463 _uint2fs.c - convert unsigned int to floating point
8465 _int2fs.c - convert int to floating point numbers
8467 _ulong2fs.c - convert unsigned long to floating point number
8469 _long2fs.c - convert long to floating point number
8477 Note if all these routines are used simultaneously the data space might
8479 For serious floating point usage it is strongly recommended that the large
8484 \begin_inset LatexCommand \index{Memory model}
8489 \begin_inset LatexCommand \index{MCS51 memory}
8496 SDCC allows two memory models for MCS51 code, small and large.
8497 Modules compiled with different memory models should
8501 be combined together or the results would be unpredictable.
8502 The library routines supplied with the compiler are compiled as both small
8504 The compiled library modules are contained in seperate directories as small
8505 and large so that you can link to either set.
8509 When the large model is used all variables declared without a storage class
8510 will be allocated into the external ram, this includes all parameters and
8511 local variables (for non-reentrant
8512 \begin_inset LatexCommand \index{reentrant}
8517 When the small model is used variables without storage class are allocated
8518 in the internal ram.
8521 Judicious usage of the processor specific storage classes
8522 \begin_inset LatexCommand \index{Storage class}
8526 and the 'reentrant' function type will yield much more efficient code,
8527 than using the large model.
8528 Several optimizations are disabled when the program is compiled using the
8529 large model, it is therefore strongly recommdended that the small model
8530 be used unless absolutely required.
8534 \begin_inset LatexCommand \index{Memory model}
8539 \begin_inset LatexCommand \index{DS390 memory model}
8546 The only model supported is Flat 24
8547 \begin_inset LatexCommand \index{Flat 24 (memory model)}
8552 This generates code for the 24 bit contiguous addressing mode of the Dallas
8554 In this mode, up to four meg of external RAM or code space can be directly
8556 See the data sheets at www.dalsemi.com for further information on this part.
8560 In older versions of the compiler, this option was used with the MCS51 code
8566 Now, however, the '390 has it's own code generator, selected by the
8575 Note that the compiler does not generate any code to place the processor
8576 into 24 bitmode (although
8580 in the ds390 libraries will do that for you).
8586 \begin_inset LatexCommand \index{Tinibios (DS390)}
8590 , the boot loader or similar code must ensure that the processor is in 24
8591 bit contiguous addressing mode before calling the SDCC startup code.
8609 option, variables will by default be placed into the XDATA segment.
8614 Segments may be placed anywhere in the 4 meg address space using the usual
8626 Note that if any segments are located above 64K, the -r flag must be passed
8627 to the linker to generate the proper segment relocations, and the Intel
8628 HEX output format must be used.
8629 The -r flag can be passed to the linker by using the option
8633 on the sdcc command line.
8634 However, currently the linker can not handle code segments > 64k.
8637 Defines Created by the Compiler
8638 \begin_inset LatexCommand \index{Defines created by the compiler}
8645 The compiler creates the following #defines
8646 \begin_inset LatexCommand \index{\#defines}
8654 \begin_inset LatexCommand \index{SDCC}
8658 - this Symbol is always defined.
8662 \begin_inset LatexCommand \index{SDCC\_mcs51}
8667 \begin_inset LatexCommand \index{SDCC\_ds390}
8672 \begin_inset LatexCommand \index{SDCC\_z80}
8676 , etc - depending on the model used (e.g.: -mds390)
8680 \begin_inset LatexCommand \index{\_\_mcs51}
8685 \begin_inset LatexCommand \index{\_\_ds390}
8690 \begin_inset LatexCommand \index{\_\_z80}
8694 , etc - depending on the model used (e.g.
8699 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
8703 - this symbol is defined when
8721 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
8743 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
8765 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
8787 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
8799 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
8814 \begin_inset LatexCommand \index{Optimizations}
8821 SDCC performs a host of standard optimizations in addition to some MCU specific
8824 \layout Subsubsection
8826 Sub-expression Elimination
8827 \begin_inset LatexCommand \index{Subexpression elimination}
8834 The compiler does local and global common subexpression elimination, e.g.:
8849 will be translated to
8865 Some subexpressions are not as obvious as the above example, e.g.:
8879 In this case the address arithmetic a->b[i] will be computed only once;
8880 the equivalent code in C would be.
8896 The compiler will try to keep these temporary variables in registers.
8897 \layout Subsubsection
8899 Dead-Code Elimination
8900 \begin_inset LatexCommand \index{Dead-code elimination}
8919 i = 1; \SpecialChar ~
8924 global = 1;\SpecialChar ~
8937 global = 3;\SpecialChar ~
8952 int global; void f ()
8965 \layout Subsubsection
8968 \begin_inset LatexCommand \index{Copy propagation}
9031 Note: the dead stores created by this copy propagation will be eliminated
9032 by dead-code elimination.
9033 \layout Subsubsection
9036 \begin_inset LatexCommand \index{Loop optimization}
9043 Two types of loop optimizations are done by SDCC loop invariant lifting
9044 and strength reduction of loop induction variables.
9045 In addition to the strength reduction the optimizer marks the induction
9046 variables and the register allocator tries to keep the induction variables
9047 in registers for the duration of the loop.
9048 Because of this preference of the register allocator
9049 \begin_inset LatexCommand \index{Register allocation}
9053 , loop induction optimization causes an increase in register pressure, which
9054 may cause unwanted spilling of other temporary variables into the stack
9055 \begin_inset LatexCommand \index{stack}
9060 The compiler will generate a warning message when it is forced to allocate
9061 extra space either on the stack or data space.
9062 If this extra space allocation is undesirable then induction optimization
9063 can be eliminated either for the entire source file (with -
9073 -noinduction option) or for a given function only using #pragma\SpecialChar ~
9075 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
9089 for (i = 0 ; i < 100 ; i ++)
9107 for (i = 0; i < 100; i++)
9117 As mentioned previously some loop invariants are not as apparent, all static
9118 address computations are also moved out of the loop.
9123 \begin_inset LatexCommand \index{Strength reduction}
9127 , this optimization substitutes an expression by a cheaper expression:
9133 for (i=0;i < 100; i++)
9153 for (i=0;i< 100;i++) {
9157 ar[itemp1] = itemp2;
9173 The more expensive multiplication
9174 \begin_inset LatexCommand \index{Multiplication}
9178 is changed to a less expensive addition.
9179 \layout Subsubsection
9182 \begin_inset LatexCommand \index{Loop reversing}
9189 This optimization is done to reduce the overhead of checking loop boundaries
9190 for every iteration.
9191 Some simple loops can be reversed and implemented using a
9192 \begin_inset Quotes eld
9195 decrement and jump if not zero
9196 \begin_inset Quotes erd
9200 SDCC checks for the following criterion to determine if a loop is reversible
9201 (note: more sophisticated compilers use data-dependency analysis to make
9202 this determination, SDCC uses a more simple minded analysis).
9205 The 'for' loop is of the form
9211 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
9221 The <for body> does not contain
9222 \begin_inset Quotes eld
9226 \begin_inset Quotes erd
9230 \begin_inset Quotes erd
9236 All goto's are contained within the loop.
9239 No function calls within the loop.
9242 The loop control variable <sym> is not assigned any value within the loop
9245 The loop control variable does NOT participate in any arithmetic operation
9249 There are NO switch statements in the loop.
9250 \layout Subsubsection
9252 Algebraic Simplifications
9255 SDCC does numerous algebraic simplifications, the following is a small sub-set
9256 of these optimizations.
9262 i = j + 0 ; /* changed to */ i = j;
9264 i /= 2; /* changed to */ i >>= 1;
9266 i = j - j ; /* changed to */ i = 0;
9268 i = j / 1 ; /* changed to */ i = j;
9274 Note the subexpressions
9275 \begin_inset LatexCommand \index{Subexpression}
9279 given above are generally introduced by macro expansions or as a result
9280 of copy/constant propagation.
9281 \layout Subsubsection
9284 \begin_inset LatexCommand \index{switch statement}
9291 SDCC changes switch statements to jump tables
9292 \begin_inset LatexCommand \index{jump tables}
9296 when the following conditions are true.
9300 The case labels are in numerical sequence, the labels need not be in order,
9301 and the starting number need not be one or zero.
9307 switch(i) {\SpecialChar ~
9414 Both the above switch statements will be implemented using a jump-table.
9417 The number of case labels is at least three, since it takes two conditional
9418 statements to handle the boundary conditions.
9421 The number of case labels is less than 84, since each label takes 3 bytes
9422 and a jump-table can be utmost 256 bytes long.
9426 Switch statements which have gaps in the numeric sequence or those that
9427 have more that 84 case labels can be split into more than one switch statement
9428 for efficient code generation, e.g.:
9466 If the above switch statement is broken down into two switch statements
9500 case 9: \SpecialChar ~
9510 case 12:\SpecialChar ~
9520 then both the switch statements will be implemented using jump-tables whereas
9521 the unmodified switch statement will not be.
9522 \layout Subsubsection
9524 Bit-shifting Operations
9525 \begin_inset LatexCommand \index{Bit shifting}
9532 Bit shifting is one of the most frequently used operation in embedded programmin
9534 SDCC tries to implement bit-shift operations in the most efficient way
9554 generates the following code:
9572 In general SDCC will never setup a loop if the shift count is known.
9612 Note that SDCC stores numbers in little-endian format (i.e.
9613 lowest order first).
9614 \layout Subsubsection
9617 \begin_inset LatexCommand \index{Bit rotation}
9624 A special case of the bit-shift operation is bit rotation, SDCC recognizes
9625 the following expression to be a left bit-rotation:
9636 i = ((i << 1) | (i >> 7));
9644 will generate the following code:
9660 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
9661 ns of this case will also be recognized as bit-rotation, i.e.:
9667 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
9668 \layout Subsubsection
9671 \begin_inset LatexCommand \index{Highest Order Bit}
9678 It is frequently required to obtain the highest order bit of an integral
9679 type (long, int, short or char types).
9680 SDCC recognizes the following expression to yield the highest order bit
9681 and generates optimized code for it, e.g.:
9702 hob = (gint >> 15) & 1;
9715 will generate the following code:
9754 000A E5*01\SpecialChar ~
9782 000C 33\SpecialChar ~
9813 000D E4\SpecialChar ~
9844 000E 13\SpecialChar ~
9875 000F F5*02\SpecialChar ~
9905 Variations of this case however will
9910 It is a standard C expression, so I heartily recommend this be the only
9911 way to get the highest order bit, (it is portable).
9912 Of course it will be recognized even if it is embedded in other expressions,
9919 xyz = gint + ((gint >> 15) & 1);
9925 will still be recognized.
9926 \layout Subsubsection
9929 \begin_inset LatexCommand \index{Peephole optimizer}
9936 The compiler uses a rule based, pattern matching and re-writing mechanism
9937 for peep-hole optimization.
9942 a peep-hole optimizer by Christopher W.
9943 Fraser (cwfraser@microsoft.com).
9944 A default set of rules are compiled into the compiler, additional rules
9945 may be added with the
9958 \begin_inset LatexCommand \index{-\/-peep-file}
9965 The rule language is best illustrated with examples.
9993 The above rule will change the following assembly
9994 \begin_inset LatexCommand \index{Assembler routines}
10028 Note: All occurrences of a
10032 (pattern variable) must denote the same string.
10033 With the above rule, the assembly sequence:
10051 will remain unmodified.
10055 Other special case optimizations may be added by the user (via
10071 some variants of the 8051 MCU allow only
10080 The following two rules will change all
10102 replace { lcall %1 } by { acall %1 }
10104 replace { ljmp %1 } by { ajmp %1 }
10112 inline-assembler code
10114 is also passed through the peep hole optimizer, thus the peephole optimizer
10115 can also be used as an assembly level macro expander.
10116 The rules themselves are MCU dependent whereas the rule language infra-structur
10117 e is MCU independent.
10118 Peephole optimization rules for other MCU can be easily programmed using
10123 The syntax for a rule is as follows:
10129 rule := replace [ restart ] '{' <assembly sequence> '
10167 <assembly sequence> '
10185 '}' [if <functionName> ] '
10193 <assembly sequence> := assembly instruction (each instruction including
10194 labels must be on a separate line).
10198 The optimizer will apply to the rules one by one from the top in the sequence
10199 of their appearance, it will terminate when all rules are exhausted.
10200 If the 'restart' option is specified, then the optimizer will start matching
10201 the rules again from the top, this option for a rule is expensive (performance)
10202 , it is intended to be used in situations where a transformation will trigger
10203 the same rule again.
10204 An example of this (not a good one, it has side effects) is the following
10231 Note that the replace pattern cannot be a blank, but can be a comment line.
10232 Without the 'restart' option only the inner most 'pop' 'push' pair would
10233 be eliminated, i.e.:
10285 the restart option the rule will be applied again to the resulting code
10286 and then all the pop-push pairs will be eliminated to yield:
10304 A conditional function can be attached to a rule.
10305 Attaching rules are somewhat more involved, let me illustrate this with
10336 The optimizer does a look-up of a function name table defined in function
10341 in the source file SDCCpeeph.c, with the name
10346 If it finds a corresponding entry the function is called.
10347 Note there can be no parameters specified for these functions, in this
10352 is crucial, since the function
10356 expects to find the label in that particular variable (the hash table containin
10357 g the variable bindings is passed as a parameter).
10358 If you want to code more such functions, take a close look at the function
10359 labelInRange and the calling mechanism in source file SDCCpeeph.c.
10360 I know this whole thing is a little kludgey, but maybe some day we will
10361 have some better means.
10362 If you are looking at this file, you will also see the default rules that
10363 are compiled into the compiler, you can add your own rules in the default
10364 set there if you get tired of specifying the -
10378 \begin_inset LatexCommand \index{Pragmas}
10385 SDCC supports the following #pragma directives.
10389 \begin_inset LatexCommand \index{\#pragma SAVE}
10393 - this will save all current options to the SAVE/RESTORE stack.
10398 \begin_inset LatexCommand \index{\#pragma RESTORE}
10402 - will restore saved options from the last save.
10403 SAVEs & RESTOREs can be nested.
10404 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
10405 RESTORE pulls current options from the stack.
10410 \begin_inset LatexCommand \index{\#pragma NOGCSE}
10414 - will stop global subexpression elimination.
10418 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
10422 - will stop loop induction optimizations.
10426 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
10430 - will not generate code for boundary value checking, when switch statements
10431 are turned into jump-tables.
10435 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
10439 - the compiler will not overlay the parameters and local variables of a
10444 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
10448 - the compiler will not warn you anymore for obvious mistakes, you'r on
10453 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
10457 - Will not do loop reversal optimization
10461 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
10465 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
10467 \begin_inset LatexCommand \index{push/pop}
10471 instruction in ISR function (using interrupt
10472 \begin_inset LatexCommand \index{interrupt}
10477 The directive should be placed immediately before the ISR function definition
10478 and it affects ALL ISR functions following it.
10479 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
10480 EXCLUDE\SpecialChar ~
10482 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
10490 \begin_inset LatexCommand \index{\#pragma NOIV}
10494 - Do not generate interrupt vector table entries for all ISR functions
10495 defined after the pragma.
10496 This is useful in cases where the interrupt vector table must be defined
10497 manually, or when there is a secondary, manually defined interrupt vector
10499 for the autovector feature of the Cypress EZ-USB FX2).
10503 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
10508 \begin_inset LatexCommand \index{function prologue}
10512 function1[,function2[,function3...]] - The compiler by default uses a caller
10513 saves convention for register saving across function calls, however this
10514 can cause unneccessary register pushing & popping when calling small functions
10515 from larger functions.
10516 This option can be used to switch off the register saving convention for
10517 the function names specified.
10518 The compiler will not save registers when calling these functions, extra
10519 code need to be manually inserted at the entry & exit for these functions
10520 to save & restore the registers used by these functions, this can SUBSTANTIALLY
10521 reduce code & improve run time performance of the generated code.
10522 In the future the compiler (with interprocedural analysis) may be able
10523 to determine the appropriate scheme to use for each function call.
10534 -callee-saves command line option is used, the function names specified
10535 in #pragma\SpecialChar ~
10537 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
10541 is appended to the list of functions specified in the command line.
10544 The pragma's are intended to be used to turn-off certain optimizations which
10545 might cause the compiler to generate extra stack / data space to store
10546 compiler generated temporary variables.
10547 This usually happens in large functions.
10548 Pragma directives should be used as shown in the following example, they
10549 are used to control options & optimizations for a given function; pragmas
10550 should be placed before and/or after a function, placing pragma's inside
10551 a function body could have unpredictable results.
10558 \begin_inset LatexCommand \index{\#pragma SAVE}
10562 /* save the current settings */
10565 \begin_inset LatexCommand \index{\#pragma NOGCSE}
10569 /* turnoff global subexpression elimination */
10571 #pragma NOINDUCTION
10572 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
10576 /* turn off induction optimizations */
10599 \begin_inset LatexCommand \index{\#pragma RESTORE}
10603 /* turn the optimizations back on */
10609 The compiler will generate a warning message when extra space is allocated.
10610 It is strongly recommended that the SAVE and RESTORE pragma's be used when
10611 changing options for a function.
10616 <pending: this is messy and incomplete>
10621 Compiler support routines (_gptrget, _mulint etc)
10624 Stdclib functions (puts, printf, strcat etc)
10627 Math functions (sin, pow, sqrt etc)
10630 license statements for the libraries are missing
10633 Interfacing with Assembly Routines
10634 \begin_inset LatexCommand \index{Assembler routines}
10639 \layout Subsubsection
10641 Global Registers used for Parameter Passing
10642 \begin_inset LatexCommand \index{Parameter passing}
10649 The compiler always uses the global registers
10652 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10657 \begin_inset LatexCommand \index{B (register)}
10666 \begin_inset LatexCommand \index{ACC}
10672 to pass the first parameter to a routine.
10673 The second parameter onwards is either allocated on the stack (for reentrant
10684 -stack-auto is used) or in the internal / external ram (depending on the
10687 \layout Subsubsection
10689 Assembler Routine(non-reentrant
10690 \begin_inset LatexCommand \index{reentrant}
10695 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
10702 In the following example the function cfunc calls an assembler routine asm_func,
10703 which takes two parameters.
10709 extern int asm_func(unsigned char, unsigned char);
10713 int c_func (unsigned char i, unsigned char j)
10721 return asm_func(i,j);
10735 return c_func(10,9);
10743 The corresponding assembler function is:
10749 .globl _asm_func_PARM_2
10813 add a,_asm_func_PARM_2
10834 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
10854 Note here that the return values are placed in 'dpl' - One byte return value,
10855 'dpl' LSB & 'dph' MSB for two byte values.
10856 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
10857 b' & 'acc' for four byte values.
10860 The parameter naming convention is _<function_name>_PARM_<n>, where n is
10861 the parameter number starting from 1, and counting from the left.
10862 The first parameter is passed in
10863 \begin_inset Quotes eld
10867 \begin_inset Quotes erd
10870 for One bye parameter,
10871 \begin_inset Quotes eld
10875 \begin_inset Quotes erd
10879 \begin_inset Quotes eld
10883 \begin_inset Quotes erd
10886 for three bytes and
10887 \begin_inset Quotes eld
10891 \begin_inset Quotes erd
10894 for four bytes, the varible name for the second parameter will be _<function_na
10899 Assemble the assembler routine with the following command:
10906 asx8051 -losg asmfunc.asm
10913 Then compile and link the assembler routine to the C source file with the
10921 sdcc cfunc.c asmfunc.rel
10922 \layout Subsubsection
10924 Assembler Routine(reentrant
10925 \begin_inset LatexCommand \index{reentrant}
10930 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
10937 In this case the second parameter onwards will be passed on the stack, the
10938 parameters are pushed from right to left i.e.
10939 after the call the left most parameter will be on the top of the stack.
10940 Here is an example:
10946 extern int asm_func(unsigned char, unsigned char);
10950 int c_func (unsigned char i, unsigned char j) reentrant
10958 return asm_func(i,j);
10972 return c_func(10,9);
10980 The corresponding assembler routine is:
11090 The compiling and linking procedure remains the same, however note the extra
11091 entry & exit linkage required for the assembler code, _bp is the stack
11092 frame pointer and is used to compute the offset into the stack for parameters
11093 and local variables.
11097 \begin_inset LatexCommand \index{stack}
11102 \begin_inset LatexCommand \index{External stack}
11109 The external stack is located at the start of the external ram segment,
11110 and is 256 bytes in size.
11121 -xstack option is used to compile the program, the parameters and local
11122 variables of all reentrant functions are allocated in this area.
11123 This option is provided for programs with large stack space requirements.
11124 When used with the -
11134 -stack-auto option, all parameters and local variables are allocated on
11135 the external stack (note support libraries will need to be recompiled with
11139 The compiler outputs the higher order address byte of the external ram segment
11140 into PORT P2, therefore when using the External Stack option, this port
11141 MAY NOT be used by the application program.
11145 \begin_inset LatexCommand \index{ANSI-compliance}
11152 Deviations from the compliancy.
11155 functions are not always reentrant.
11158 structures cannot be assigned values directly, cannot be passed as function
11159 parameters or assigned to each other and cannot be a return value from
11186 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
11197 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
11219 return rets;/* is invalid in SDCC although allowed in ANSI */
11225 \begin_inset LatexCommand \index{long long (not supported)}
11230 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
11238 \begin_inset LatexCommand \index{double (not supported)}
11242 ' precision floating point
11243 \begin_inset LatexCommand \index{Floating point support}
11250 No support for setjmp and longjmp (for now).
11254 \begin_inset LatexCommand \index{K\&R style}
11258 function declarations are NOT allowed.
11264 foo(i,j) /* this old style of function declarations */
11266 int i,j; /* are valid in ANSI but not valid in SDCC */
11280 functions declared as pointers must be dereferenced during the call.
11291 /* has to be called like this */
11293 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
11296 Cyclomatic Complexity
11297 \begin_inset LatexCommand \index{Cyclomatic complexity}
11304 Cyclomatic complexity of a function is defined as the number of independent
11305 paths the program can take during execution of the function.
11306 This is an important number since it defines the number test cases you
11307 have to generate to validate the function.
11308 The accepted industry standard for complexity number is 10, if the cyclomatic
11309 complexity reported by SDCC exceeds 10 you should think about simplification
11310 of the function logic.
11311 Note that the complexity level is not related to the number of lines of
11312 code in a function.
11313 Large functions can have low complexity, and small functions can have large
11319 SDCC uses the following formula to compute the complexity:
11324 complexity = (number of edges in control flow graph) - (number of nodes
11325 in control flow graph) + 2;
11329 Having said that the industry standard is 10, you should be aware that in
11330 some cases it be may unavoidable to have a complexity level of less than
11332 For example if you have switch statement with more than 10 case labels,
11333 each case label adds one to the complexity level.
11334 The complexity level is by no means an absolute measure of the algorithmic
11335 complexity of the function, it does however provide a good starting point
11336 for which functions you might look at for further optimization.
11342 Here are a few guidelines that will help the compiler generate more efficient
11343 code, some of the tips are specific to this compiler others are generally
11344 good programming practice.
11347 Use the smallest data type to represent your data-value.
11348 If it is known in advance that the value is going to be less than 256 then
11349 use an 'unsigned char' instead of a 'short' or 'int'.
11352 Use unsigned when it is known in advance that the value is not going to
11354 This helps especially if you are doing division or multiplication.
11357 NEVER jump into a LOOP.
11360 Declare the variables to be local whenever possible, especially loop control
11361 variables (induction).
11364 Since the compiler does not always do implicit integral promotion, the programme
11365 r should do an explicit cast when integral promotion is required.
11368 Reducing the size of division, multiplication & modulus operations can reduce
11369 code size substantially.
11370 Take the following code for example.
11376 foobar(unsigned int p1, unsigned char ch)
11380 unsigned char ch1 = p1 % ch ;
11391 For the modulus operation the variable ch will be promoted to unsigned int
11392 first then the modulus operation will be performed (this will lead to a
11393 call to support routine _moduint()), and the result will be casted to a
11395 If the code is changed to
11401 foobar(unsigned int p1, unsigned char ch)
11405 unsigned char ch1 = (unsigned char)p1 % ch ;
11416 It would substantially reduce the code generated (future versions of the
11417 compiler will be smart enough to detect such optimization oppurtunities).
11420 Notes on MCS51 memory
11421 \begin_inset LatexCommand \index{MCS51 memory}
11428 The 8051 family of micro controller have a minimum of 128 bytes of internal
11429 memory which is structured as follows
11433 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
11436 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
11438 - Bytes 30-7F - 60 bytes for general purpose use.
11442 Normally the SDCC compiler will only utilise the first bank
11443 \begin_inset LatexCommand \index{bank}
11447 of registers, but it is possible to specify that other banks of registers
11448 should be used in interrupt
11449 \begin_inset LatexCommand \index{interrupt}
11454 By default, the compiler will place the stack after the last bank of used
11456 if the first 2 banks of registers are used, it will position the base of
11457 the internal stack at address 16 (0X10).
11458 This implies that as the stack
11459 \begin_inset LatexCommand \index{stack}
11463 grows, it will use up the remaining register banks, and the 16 bytes used
11464 by the 128 bit variables, and 60 bytes for general purpose use.
11467 By default, the compiler uses the 60 general purpose bytes to hold "near
11469 The compiler/optimiser may also declare some Local Variables in this area
11470 to hold local data.
11474 If any of the 128 bit variables are used, or near data is being used then
11475 care needs to be taken to ensure that the stack does not grow so much that
11476 it starts to over write either your bit variables or "near data".
11477 There is no runtime checking to prevent this from happening.
11480 The amount of stack being used is affected by the use of the "internal stack"
11481 to save registers before a subroutine call is made (-
11492 \begin_inset LatexCommand \index{-\/-stack-auto}
11496 will declare parameters and local variables on the stack) and the number
11497 of nested subroutines.
11500 If you detect that the stack is over writing you data, then the following
11512 -xstack will cause an external stack to be used for saving registers and
11523 -stack-auto is being used) storing parameters and local variables.
11524 However this will produce more code which will be slower to execute.
11539 \begin_inset LatexCommand \index{-\/-stack-loc}
11543 will allow you specify the start of the stack, i.e.
11544 you could start it after any data in the general purpose area.
11545 However this may waste the memory not used by the register banks and if
11546 the size of the "near data" increases, it may creep into the bottom of
11561 \begin_inset LatexCommand \index{-\/-data-loc}
11565 allows you to specify the start address of the near data.
11566 This could be used to move the "near data" further away from the stack
11567 giving it more room to grow.
11568 This will only work if no bit variables are being used and the stack can
11569 grow to use the bit variable space.
11577 If you find that the stack is over writing your bit variables or "near data"
11578 then the approach which best utilised the internal memory is to position
11579 the "near data" after the last bank of used registers or, if you use bit
11580 variables, after the last bit variable by using the -
11591 if two register banks are being used and no bit variables, -
11604 If bit variables are being used, another method would be to try and squeeze
11605 the data area in the unused register banks if it will fit, and start the
11606 stack after the last bit variable.
11610 \begin_inset LatexCommand \index{Tools}
11614 included in the distribution
11618 \begin_inset Tabular
11619 <lyxtabular version="3" rows="9" columns="3">
11621 <column alignment="center" valignment="top" leftline="true" width="0pt">
11622 <column alignment="center" valignment="top" leftline="true" width="0pt">
11623 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
11624 <row topline="true" bottomline="true">
11625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11633 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11641 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11650 <row topline="true">
11651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11659 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11664 Simulator for various architectures
11667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11676 <row topline="true">
11677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11690 header file conversion
11693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11698 sdcc/support/scripts
11702 <row topline="true">
11703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11716 header file conversion
11719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11724 sdcc/support/scripts
11728 <row topline="true">
11729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11737 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11763 <row topline="true">
11764 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11772 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11798 <row topline="true">
11799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11833 <row topline="true">
11834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11850 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11868 <row topline="true" bottomline="true">
11869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11883 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11898 Related open source tools
11899 \begin_inset LatexCommand \index{Related tools}
11907 \begin_inset Tabular
11908 <lyxtabular version="3" rows="5" columns="3">
11910 <column alignment="center" valignment="top" leftline="true" width="0pt">
11911 <column alignment="center" valignment="top" leftline="true" width="0pt">
11912 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
11913 <row topline="true" bottomline="true">
11914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11939 <row topline="true">
11940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11946 \begin_inset LatexCommand \index{gpsim}
11953 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11961 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11967 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
11975 <row topline="true">
11976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11982 \begin_inset LatexCommand \index{srecord}
11989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11994 Object file conversion, checksumming, ...
11997 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12003 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
12011 <row topline="true">
12012 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12018 \begin_inset LatexCommand \index{objdump}
12025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12030 Object file conversion
12033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12038 Part of binutils (should be there anyway)
12042 <row topline="true" bottomline="true">
12043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12049 \begin_inset LatexCommand \index{ddd}
12056 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12061 Debugger, serves nicely as GUI to sdcdb
12062 \begin_inset LatexCommand \index{sdcdb}
12069 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12075 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
12090 Related documentation / recommended reading
12094 \begin_inset Tabular
12095 <lyxtabular version="3" rows="4" columns="3">
12097 <column alignment="center" valignment="top" leftline="true" width="0pt">
12098 <column alignment="center" valignment="top" leftline="true" width="0pt">
12099 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
12100 <row topline="true" bottomline="true">
12101 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12117 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12126 <row topline="true">
12127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12139 Compiler architecture
12142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12151 <row topline="true">
12152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12157 test_suite_spec.pdf
12160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12165 sdcc regression test
12168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12177 <row topline="true" bottomline="true">
12178 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12209 sdcc internal documentation
12212 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12228 Retargetting for other MCUs.
12231 The issues for retargetting the compiler are far too numerous to be covered
12233 What follows is a brief description of each of the seven phases of the
12234 compiler and its MCU dependency.
12237 Parsing the source and building the annotated parse tree.
12238 This phase is largely MCU independent (except for the language extensions).
12239 Syntax & semantic checks are also done in this phase, along with some initial
12240 optimizations like back patching labels and the pattern matching optimizations
12241 like bit-rotation etc.
12244 The second phase involves generating an intermediate code which can be easy
12245 manipulated during the later phases.
12246 This phase is entirely MCU independent.
12247 The intermediate code generation assumes the target machine has unlimited
12248 number of registers, and designates them with the name iTemp.
12249 The compiler can be made to dump a human readable form of the code generated
12263 This phase does the bulk of the standard optimizations and is also MCU independe
12265 This phase can be broken down into several sub-phases:
12269 Break down intermediate code (iCode) into basic blocks.
12271 Do control flow & data flow analysis on the basic blocks.
12273 Do local common subexpression elimination, then global subexpression elimination
12275 Dead code elimination
12279 If loop optimizations caused any changes then do 'global subexpression eliminati
12280 on' and 'dead code elimination' again.
12283 This phase determines the live-ranges; by live range I mean those iTemp
12284 variables defined by the compiler that still survive after all the optimization
12286 Live range analysis
12287 \begin_inset LatexCommand \index{Live range analysis}
12291 is essential for register allocation, since these computation determines
12292 which of these iTemps will be assigned to registers, and for how long.
12295 Phase five is register allocation.
12296 There are two parts to this process.
12300 The first part I call 'register packing' (for lack of a better term).
12301 In this case several MCU specific expression folding is done to reduce
12306 The second part is more MCU independent and deals with allocating registers
12307 to the remaining live ranges.
12308 A lot of MCU specific code does creep into this phase because of the limited
12309 number of index registers available in the 8051.
12312 The Code generation phase is (unhappily), entirely MCU dependent and very
12313 little (if any at all) of this code can be reused for other MCU.
12314 However the scheme for allocating a homogenized assembler operand for each
12315 iCode operand may be reused.
12318 As mentioned in the optimization section the peep-hole optimizer is rule
12319 based system, which can reprogrammed for other MCUs.
12323 \begin_inset LatexCommand \index{sdcdb}
12327 - Source Level Debugger
12328 \begin_inset LatexCommand \index{Debugger}
12335 SDCC is distributed with a source level debugger.
12336 The debugger uses a command line interface, the command repertoire of the
12337 debugger has been kept as close to gdb
12338 \begin_inset LatexCommand \index{gdb}
12342 (the GNU debugger) as possible.
12343 The configuration and build process is part of the standard compiler installati
12344 on, which also builds and installs the debugger in the target directory
12345 specified during configuration.
12346 The debugger allows you debug BOTH at the C source and at the ASM source
12350 Compiling for Debugging
12353 The \SpecialChar \-
12355 debug option must be specified for all files for which debug information
12356 is to be generated.
12357 The complier generates a .adb file for each of these files.
12358 The linker creates the .cdb file from the .adb files and the address information.
12359 This .cdb is used by the debugger.
12362 How the Debugger Works
12375 -debug option is specified the compiler generates extra symbol information
12376 some of which are put into the the assembler source and some are put into
12378 Then the linker creates the .cdb file from the individual .adb files with
12379 the address information for the symbols.
12380 The debugger reads the symbolic information generated by the compiler &
12381 the address information generated by the linker.
12382 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
12383 execution is controlled by the debugger.
12384 When a command is issued for the debugger, it translates it into appropriate
12385 commands for the simulator.
12388 Starting the Debugger
12391 The debugger can be started using the following command line.
12392 (Assume the file you are debugging has the file name foo).
12406 The debugger will look for the following files.
12409 foo.c - the source file.
12412 foo.cdb - the debugger symbol information file.
12415 foo.ihx - the intel hex format
12416 \begin_inset LatexCommand \index{Intel hex format}
12423 Command Line Options.
12436 -directory=<source file directory> this option can used to specify the directory
12438 The debugger will look into the directory list specified for source, cdb
12440 The items in the directory list must be separated by ':', e.g.
12441 if the source files can be in the directories /home/src1 and /home/src2,
12452 -directory option should be -
12462 -directory=/home/src1:/home/src2.
12463 Note there can be no spaces in the option.
12467 -cd <directory> - change to the <directory>.
12470 -fullname - used by GUI front ends.
12473 -cpu <cpu-type> - this argument is passed to the simulator please see the
12474 simulator docs for details.
12477 -X <Clock frequency > this options is passed to the simulator please see
12478 the simulator docs for details.
12481 -s <serial port file> passed to simulator see the simulator docs for details.
12484 -S <serial in,out> passed to simulator see the simulator docs for details.
12490 As mention earlier the command interface for the debugger has been deliberately
12491 kept as close the GNU debugger gdb, as possible.
12492 This will help the integration with existing graphical user interfaces
12493 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
12494 \layout Subsubsection
12496 break [line | file:line | function | file:function]
12499 Set breakpoint at specified line or function:
12508 sdcdb>break foo.c:100
12510 sdcdb>break funcfoo
12512 sdcdb>break foo.c:funcfoo
12513 \layout Subsubsection
12515 clear [line | file:line | function | file:function ]
12518 Clear breakpoint at specified line or function:
12527 sdcdb>clear foo.c:100
12529 sdcdb>clear funcfoo
12531 sdcdb>clear foo.c:funcfoo
12532 \layout Subsubsection
12537 Continue program being debugged, after breakpoint.
12538 \layout Subsubsection
12543 Execute till the end of the current function.
12544 \layout Subsubsection
12549 Delete breakpoint number 'n'.
12550 If used without any option clear ALL user defined break points.
12551 \layout Subsubsection
12553 info [break | stack | frame | registers ]
12556 info break - list all breakpoints
12559 info stack - show the function call stack.
12562 info frame - show information about the current execution frame.
12565 info registers - show content of all registers.
12566 \layout Subsubsection
12571 Step program until it reaches a different source line.
12572 \layout Subsubsection
12577 Step program, proceeding through subroutine calls.
12578 \layout Subsubsection
12583 Start debugged program.
12584 \layout Subsubsection
12589 Print type information of the variable.
12590 \layout Subsubsection
12595 print value of variable.
12596 \layout Subsubsection
12601 load the given file name.
12602 Note this is an alternate method of loading file for debugging.
12603 \layout Subsubsection
12608 print information about current frame.
12609 \layout Subsubsection
12614 Toggle between C source & assembly source.
12615 \layout Subsubsection
12617 ! simulator command
12620 Send the string following '!' to the simulator, the simulator response is
12622 Note the debugger does not interpret the command being sent to the simulator,
12623 so if a command like 'go' is sent the debugger can loose its execution
12624 context and may display incorrect values.
12625 \layout Subsubsection
12632 My name is Bobby Brown"
12635 Interfacing with XEmacs
12636 \begin_inset LatexCommand \index{XEmacs}
12641 \begin_inset LatexCommand \index{Emacs}
12648 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
12649 sdcdb.el and sdcdbsrc.el.
12650 These two files can be found in the $(prefix)/bin directory after the installat
12652 These files need to be loaded into XEmacs for the interface to work.
12653 This can be done at XEmacs startup time by inserting the following into
12654 your '.xemacs' file (which can be found in your HOME directory):
12660 (load-file sdcdbsrc.el)
12666 .xemacs is a lisp file so the () around the command is REQUIRED.
12667 The files can also be loaded dynamically while XEmacs is running, set the
12668 environment variable 'EMACSLOADPATH' to the installation bin directory
12669 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
12670 To start the interface enter the following command:
12684 You will prompted to enter the file name to be debugged.
12689 The command line options that are passed to the simulator directly are bound
12690 to default values in the file sdcdbsrc.el.
12691 The variables are listed below, these values maybe changed as required.
12694 sdcdbsrc-cpu-type '51
12697 sdcdbsrc-frequency '11059200
12700 sdcdbsrc-serial nil
12703 The following is a list of key mapping for the debugger interface.
12711 ;; Current Listing ::
12713 ;;key\SpecialChar ~
12728 binding\SpecialChar ~
12752 ;;---\SpecialChar ~
12767 ------\SpecialChar ~
12807 sdcdb-next-from-src\SpecialChar ~
12833 sdcdb-back-from-src\SpecialChar ~
12859 sdcdb-cont-from-src\SpecialChar ~
12869 SDCDB continue command
12885 sdcdb-step-from-src\SpecialChar ~
12911 sdcdb-whatis-c-sexp\SpecialChar ~
12921 SDCDB ptypecommand for data at
12985 sdcdbsrc-delete\SpecialChar ~
12999 SDCDB Delete all breakpoints if no arg
13047 given or delete arg (C-u arg x)
13063 sdcdbsrc-frame\SpecialChar ~
13078 SDCDB Display current frame if no arg,
13127 given or display frame arg
13192 sdcdbsrc-goto-sdcdb\SpecialChar ~
13202 Goto the SDCDB output buffer
13218 sdcdb-print-c-sexp\SpecialChar ~
13229 SDCDB print command for data at
13293 sdcdbsrc-goto-sdcdb\SpecialChar ~
13303 Goto the SDCDB output buffer
13319 sdcdbsrc-mode\SpecialChar ~
13335 Toggles Sdcdbsrc mode (turns it off)
13339 ;; C-c C-f\SpecialChar ~
13347 sdcdb-finish-from-src\SpecialChar ~
13355 SDCDB finish command
13359 ;; C-x SPC\SpecialChar ~
13367 sdcdb-break\SpecialChar ~
13385 Set break for line with point
13387 ;; ESC t\SpecialChar ~
13397 sdcdbsrc-mode\SpecialChar ~
13413 Toggle Sdcdbsrc mode
13415 ;; ESC m\SpecialChar ~
13425 sdcdbsrc-srcmode\SpecialChar ~
13449 The Z80 and gbz80 port
13452 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
13453 The port is incomplete - long support is incomplete (mul, div and mod are
13454 unimplimented), and both float and bitfield support is missing.
13455 Apart from that the code generated is correct.
13458 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
13459 The stack frame is similar to that generated by the IAR Z80 compiler.
13460 IX is used as the base pointer, HL is used as a temporary register, and
13461 BC and DE are available for holding varibles.
13462 IY is currently unusued.
13463 Return values are stored in HL.
13464 One bad side effect of using IX as the base pointer is that a functions
13465 stack frame is limited to 127 bytes - this will be fixed in a later version.
13469 \begin_inset LatexCommand \index{Support}
13476 SDCC has grown to be a large project.
13477 The compiler alone (without the preprocessor, assembler and linker) is
13478 about 40,000 lines of code (blank stripped).
13479 The open source nature of this project is a key to its continued growth
13481 You gain the benefit and support of many active software developers and
13483 Is SDCC perfect? No, that's why we need your help.
13484 The developers take pride in fixing reported bugs.
13485 You can help by reporting the bugs and helping other SDCC users.
13486 There are lots of ways to contribute, and we encourage you to take part
13487 in making SDCC a great software package.
13491 The SDCC project is hosted on the sdcc sourceforge site at
13492 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
13497 You'll find the complete set of mailing lists
13498 \begin_inset LatexCommand \index{Mailing list}
13502 , forums, bug reporting system, patch submission
13503 \begin_inset LatexCommand \index{Patch submission}
13508 \begin_inset LatexCommand \index{download}
13512 area and cvs code repository
13513 \begin_inset LatexCommand \index{cvs code repository}
13521 \begin_inset LatexCommand \index{Bugs}
13526 \begin_inset LatexCommand \index{Reporting bugs}
13533 The recommended way of reporting bugs is using the infrastructure of the
13535 You can follow the status of bug reports there and have an overview about
13539 Bug reports are automatically forwarded to the developer mailing list and
13540 will be fixed ASAP.
13541 When reporting a bug, it is very useful to include a small test program
13542 which reproduces the problem.
13543 If you can isolate the problem by looking at the generated assembly code,
13544 this can be very helpful.
13545 Compiling your program with the -
13556 \begin_inset LatexCommand \index{-\/-dumpall}
13560 option can sometimes be useful in locating optimization problems.
13563 Please have a short check that you are using a recent version of SDCC and
13564 the bug is not yet known.
13565 This is the link for reporting bugs:
13566 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
13573 Requesting Features
13574 \begin_inset LatexCommand \index{Feature request}
13579 \begin_inset LatexCommand \index{Requesting features}
13586 Like bug reports feature requests are forwarded to the developer mailing
13588 This is the link for requesting features:
13589 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
13599 These links should take you directly to the
13600 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
13610 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
13611 automated messages (mid 2003)
13615 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
13619 , lists and forums are archived so if you are lucky someone already had
13624 \begin_inset LatexCommand \index{Changelog}
13631 You can follow the status of the cvs version
13632 \begin_inset LatexCommand \index{version}
13636 of SDCC by watching the file
13637 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
13641 in the cvs-repository.
13645 \begin_inset LatexCommand \index{Release policy}
13652 Historically there often were long delays between official releases and
13653 the sourceforge download area tends to get not updated at all.
13654 Current excuses might refer to problems with live range analysis, but if
13655 this is fixed, the next problem rising is that another excuse will have
13657 Kidding aside, we have to get better there!
13661 \begin_inset LatexCommand \index{Examples}
13668 You'll find some small examples in the directory sdcc/device/examples/
13671 Maybe we should include some links to real world applications.
13672 Preferrably pointer to pointers (one for each architecture) so this stays
13677 \begin_inset LatexCommand \index{Quality control}
13684 The compiler is passed through nightly compile and build checks.
13690 \begin_inset LatexCommand \index{Regression test}
13694 check that SDCC itself compiles flawlessly on several platforms and checks
13695 the quality of the code generated by SDCC by running the code through simulator
13697 There is a separate document
13700 \begin_inset LatexCommand \index{Test suite}
13709 You'll find the test code in the directory
13711 sdcc/support/regression
13714 You can run these tests manually by running
13718 in this directory (or f.e.
13723 if you don't want to run the complete tests).
13724 The test code might also be interesting if you want to look for examples
13725 \begin_inset LatexCommand \index{Examples}
13729 checking corner cases of SDCC or if you plan to submit patches
13730 \begin_inset LatexCommand \index{Patch submission}
13737 The pic port uses a different set of regression tests, you'll find them
13740 sdcc/src/regression
13746 \begin_inset LatexCommand \index{Compiler internals}
13753 The anatomy of the compiler
13758 This is an excerpt from an atricle published in Circuit Cellar MagaZine
13760 It's a little outdated (the compiler is much more efficient now and user/develo
13761 per friendly), but pretty well exposes the guts of it all.
13767 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
13768 It is fairly easy to retarget for other 8-bit MCU.
13769 Here we take a look at some of the internals of the compiler.
13774 \begin_inset LatexCommand \index{Parsing}
13781 Parsing the input source file and creating an AST (Annotated Syntax Tree
13782 \begin_inset LatexCommand \index{Annotated syntax tree}
13787 This phase also involves propagating types (annotating each node of the
13788 parse tree with type information) and semantic analysis.
13789 There are some MCU specific parsing rules.
13790 For example the storage classes, the extended storage classes are MCU specific
13791 while there may be a xdata storage class for 8051 there is no such storage
13792 class for z80 or Atmel AVR.
13793 SDCC allows MCU specific storage class extensions, i.e.
13794 xdata will be treated as a storage class specifier when parsing 8051 C
13795 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
13800 \begin_inset LatexCommand \index{iCode}
13807 Intermediate code generation.
13808 In this phase the AST is broken down into three-operand form (iCode).
13809 These three operand forms are represented as doubly linked lists.
13810 ICode is the term given to the intermediate form generated by the compiler.
13811 ICode example section shows some examples of iCode generated for some simple
13812 C source functions.
13816 \begin_inset LatexCommand \index{Optimizations}
13823 Bulk of the target independent optimizations is performed in this phase.
13824 The optimizations include constant propagation, common sub-expression eliminati
13825 on, loop invariant code movement, strength reduction of loop induction variables
13826 and dead-code elimination.
13829 Live range analysis
13830 \begin_inset LatexCommand \index{Live range analysis}
13837 During intermediate code generation phase, the compiler assumes the target
13838 machine has infinite number of registers and generates a lot of temporary
13840 The live range computation determines the lifetime of each of these compiler-ge
13841 nerated temporaries.
13842 A picture speaks a thousand words.
13843 ICode example sections show the live range annotations for each of the
13845 It is important to note here, each iCode is assigned a number in the order
13846 of its execution in the function.
13847 The live ranges are computed in terms of these numbers.
13848 The from number is the number of the iCode which first defines the operand
13849 and the to number signifies the iCode which uses this operand last.
13852 Register Allocation
13853 \begin_inset LatexCommand \index{Register allocation}
13860 The register allocation determines the type and number of registers needed
13862 In most MCUs only a few registers can be used for indirect addressing.
13863 In case of 8051 for example the registers R0 & R1 can be used to indirectly
13864 address the internal ram and DPTR to indirectly address the external ram.
13865 The compiler will try to allocate the appropriate register to pointer variables
13867 ICode example section shows the operands annotated with the registers assigned
13869 The compiler will try to keep operands in registers as much as possible;
13870 there are several schemes the compiler uses to do achieve this.
13871 When the compiler runs out of registers the compiler will check to see
13872 if there are any live operands which is not used or defined in the current
13873 basic block being processed, if there are any found then it will push that
13874 operand and use the registers in this block, the operand will then be popped
13875 at the end of the basic block.
13879 There are other MCU specific considerations in this phase.
13880 Some MCUs have an accumulator; very short-lived operands could be assigned
13881 to the accumulator instead of general-purpose register.
13887 Figure II gives a table of iCode operations supported by the compiler.
13888 The code generation involves translating these operations into corresponding
13889 assembly code for the processor.
13890 This sounds overly simple but that is the essence of code generation.
13891 Some of the iCode operations are generated on a MCU specific manner for
13892 example, the z80 port does not use registers to pass parameters so the
13893 SEND and RECV iCode operations will not be generated, and it also does
13894 not support JUMPTABLES.
13901 <Where is Figure II ?>
13905 \begin_inset LatexCommand \index{iCode}
13912 This section shows some details of iCode.
13913 The example C code does not do anything useful; it is used as an example
13914 to illustrate the intermediate code generated by the compiler.
13927 /* This function does nothing useful.
13934 for the purpose of explaining iCode */
13937 short function (data int *x)
13945 short i=10; /* dead initialization eliminated */
13950 short sum=10; /* dead initialization eliminated */
13963 while (*x) *x++ = *p++;
13977 /* compiler detects i,j to be induction variables */
13981 for (i = 0, j = 10 ; i < 10 ; i++, j--) {
13993 mul += i * 3; /* this multiplication remains */
13999 gint += j * 3;/* this multiplication changed to addition */
14016 In addition to the operands each iCode contains information about the filename
14017 and line it corresponds to in the source file.
14018 The first field in the listing should be interpreted as follows:
14023 Filename(linenumber: iCode Execution sequence number : ICode hash table
14024 key : loop depth of the iCode).
14029 Then follows the human readable form of the ICode operation.
14030 Each operand of this triplet form can be of three basic types a) compiler
14031 generated temporary b) user defined variable c) a constant value.
14032 Note that local variables and parameters are replaced by compiler generated
14035 \begin_inset LatexCommand \index{Live range analysis}
14039 are computed only for temporaries (i.e.
14040 live ranges are not computed for global variables).
14042 \begin_inset LatexCommand \index{Register allocation}
14046 are allocated for temporaries only.
14047 Operands are formatted in the following manner:
14052 Operand Name [lr live-from : live-to ] { type information } [ registers
14058 As mentioned earlier the live ranges are computed in terms of the execution
14059 sequence number of the iCodes, for example
14061 the iTemp0 is live from (i.e.
14062 first defined in iCode with execution sequence number 3, and is last used
14063 in the iCode with sequence number 5).
14064 For induction variables such as iTemp21 the live range computation extends
14065 the lifetime from the start to the end of the loop.
14067 The register allocator used the live range information to allocate registers,
14068 the same registers may be used for different temporaries if their live
14069 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
14070 iTemp17 since their live ranges do not overlap.
14071 In addition the allocator also takes into consideration the type and usage
14072 of a temporary, for example itemp6 is a pointer to near space and is used
14073 as to fetch data from (i.e.
14074 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
14075 Some short lived temporaries are allocated to special registers which have
14076 meaning to the code generator e.g.
14077 iTemp13 is allocated to a pseudo register CC which tells the back end that
14078 the temporary is used only for a conditional jump the code generation makes
14079 use of this information to optimize a compare and jump ICode.
14081 There are several loop optimizations
14082 \begin_inset LatexCommand \index{Loop optimization}
14086 performed by the compiler.
14087 It can detect induction variables iTemp21(i) and iTemp23(j).
14088 Also note the compiler does selective strength reduction
14089 \begin_inset LatexCommand \index{Strength reduction}
14094 the multiplication of an induction variable in line 18 (gint = j * 3) is
14095 changed to addition, a new temporary iTemp17 is allocated and assigned
14096 a initial value, a constant 3 is then added for each iteration of the loop.
14097 The compiler does not change the multiplication
14098 \begin_inset LatexCommand \index{Multiplication}
14102 in line 17 however since the processor does support an 8 * 8 bit multiplication.
14104 Note the dead code elimination
14105 \begin_inset LatexCommand \index{Dead-code elimination}
14109 optimization eliminated the dead assignments in line 7 & 8 to I and sum
14117 Sample.c (5:1:0:0) _entry($9) :
14122 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
14127 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
14132 Sample.c(11:4:53:0) preHeaderLbl0($11) :
14137 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
14143 Sample.c(11:6:5:1) _whilecontinue_0($1) :
14148 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
14154 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
14159 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
14165 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
14171 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
14177 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
14183 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
14184 * int}[r0] + 0x2 {short}
14189 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
14194 Sample.c(11:17:21:0)_whilebreak_0($3) :
14199 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
14204 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
14209 Sample.c(15:20:54:0)preHeaderLbl1($13) :
14214 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
14219 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
14224 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
14229 Sample.c(15:24:26:1)_forcond_0($4) :
14234 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
14240 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
14245 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
14246 + ITemp21 [lr21:38]{short}[r4]
14251 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
14257 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
14258 + iTemp15 [lr29:30]{short}[r1]
14263 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
14269 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
14275 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
14281 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
14287 Sample.c(19:38:47:1) goto _forcond_0($4)
14292 Sample.c(19:39:48:0)_forbreak_0($7) :
14297 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
14298 + ITemp11 [lr19:40]{short}[r3]
14303 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
14308 Sample.c(20:42:51:0)_return($8) :
14313 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
14319 Finally the code generated for this function:
14360 ; ----------------------------------------------
14365 ; function function
14370 ; ----------------------------------------------
14380 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
14392 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
14404 ;_whilecontinue_0($1) :
14414 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
14419 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
14478 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
14497 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
14544 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
14584 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
14610 ; iTemp6 [lr5:16]{_near * int}[r0] =
14615 ; iTemp6 [lr5:16]{_near * int}[r0] +
14632 ; goto _whilecontinue_0($1)
14644 ; _whilebreak_0($3) :
14654 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
14666 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
14678 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
14690 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
14709 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
14738 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
14743 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
14788 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
14793 ; iTemp21 [lr21:38]{short}[r4]
14819 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
14852 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
14857 ; iTemp15 [lr29:30]{short}[r1]
14876 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
14923 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
14970 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
14982 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
14996 cjne r5,#0xff,00104$
15008 ; goto _forcond_0($4)
15020 ; _forbreak_0($7) :
15030 ; ret iTemp24 [lr40:41]{short}
15073 A few words about basic block successors, predecessors and dominators
15076 Successors are basic blocks
15077 \begin_inset LatexCommand \index{Basic blocks}
15081 that might execute after this basic block.
15083 Predecessors are basic blocks that might execute before reaching this basic
15086 Dominators are basic blocks that WILL execute before reaching this basic
15112 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
15115 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
15118 c) domVect of [BB4] = BB1 ...
15119 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
15127 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
15137 Thanks to all the other volunteer developers who have helped with coding,
15138 testing, web-page creation, distribution sets, etc.
15139 You know who you are :-)
15146 This document was initially written by Sandeep Dutta
15149 All product names mentioned herein may be trademarks
15150 \begin_inset LatexCommand \index{Trademarks}
15154 of their respective companies.
15161 To avoid confusion, the installation and building options for sdcc itself
15162 (chapter 2) are not part of the index.
15166 \begin_inset LatexCommand \printindex{}