1 #LyX 1.2 created this file. For more info see http://www.lyx.org/
15 \use_numerical_citations 0
16 \paperorientation portrait
19 \paragraph_separation indent
21 \quotes_language swedish
29 Please note: double dashed longoptions (e.g.
30 --version) need three dashes in this document to be visable in html and
34 SDCC Compiler User Guide
38 \begin_inset LatexCommand \tableofcontents{}
55 is a Freeware, retargettable, optimizing ANSI-C compiler by
59 designed for 8 bit Microprocessors.
60 The current version targets Intel MCS51 based Microprocessors(8051,8052,
61 etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
62 It can be retargetted for other microprocessors, support for PIC, AVR and
63 186 is under development.
64 The entire source code for the compiler is distributed under GPL.
65 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
66 SDCC has extensive language extensions suitable for utilizing various microcont
67 rollers and underlying hardware effectively.
72 In addition to the MCU specific optimizations SDCC also does a host of standard
76 global sub expression elimination,
79 loop optimizations (loop invariant, strength reduction of induction variables
83 constant folding & propagation,
99 For the back-end SDCC uses a global register allocation scheme which should
100 be well suited for other 8 bit MCUs.
105 The peep hole optimizer uses a rule based substitution mechanism which is
111 Supported data-types are:
114 char (8 bits, 1 byte),
117 short and int (16 bits, 2 bytes),
120 long (32 bit, 4 bytes)
127 The compiler also allows
129 inline assembler code
131 to be embedded anywhere in a function.
132 In addition, routines developed in assembly can also be called.
136 SDCC also provides an option (--cyclomatic) to report the relative complexity
138 These functions can then be further optimized, or hand coded in assembly
144 SDCC also comes with a companion source level debugger SDCDB, the debugger
145 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
150 The latest version can be downloaded from
151 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
163 All packages used in this compiler system are
171 ; source code for all the sub-packages (pre-processor, assemblers, linkers
172 etc) is distributed with the package.
173 This documentation is maintained using a freeware word processor (LyX).
175 This program is free software; you can redistribute it and/or modify it
176 under the terms of the GNU General Public License as published by the Free
177 Software Foundation; either version 2, or (at your option) any later version.
178 This program is distributed in the hope that it will be useful, but WITHOUT
179 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
180 FOR A PARTICULAR PURPOSE.
181 See the GNU General Public License for more details.
182 You should have received a copy of the GNU General Public License along
183 with this program; if not, write to the Free Software Foundation, 59 Temple
184 Place - Suite 330, Boston, MA 02111-1307, USA.
185 In other words, you are welcome to use, share and improve this program.
186 You are forbidden to forbid anyone else to use, share and improve what
188 Help stamp out software-hoarding!
191 Typographic conventions
194 Throughout this manual, we will use the following convention.
195 Commands you have to type in are printed in
203 Code samples are printed in
208 Interesting items and new terms are printed in
213 Compatibility with previous versions
216 This version has numerous bug fixes compared with the previous version.
217 But we also introduced some incompatibilities with older versions.
218 Not just for the fun of it, but to make the compiler more stable, efficient
225 short is now equivalent to int (16 bits), it used to be equivalent to char
226 (8 bits) which is not ANSI compliant
229 the default directory for gcc-builds where include, library and documention
230 files are stored is now in /usr/local/share
233 char type parameters to vararg functions are casted to int unless explicitly
250 will push a as an int and as a char resp.
253 option ---regextend has been removed
256 option ---noregparms has been removed
259 option ---stack-after-data has been removed
264 <pending: more incompatibilities?>
270 What do you need before you start installation of SDCC? A computer, and
272 The preferred method of installation is to compile SDCC from source using
274 For Windows some pre-compiled binary distributions are available for your
276 You should have some experience with command line tools and compiler use.
282 The SDCC home page at
283 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
287 is a great place to find distribution sets.
288 You can also find links to the user mailing lists that offer help or discuss
289 SDCC with other SDCC users.
290 Web links to other SDCC related sites can also be found here.
291 This document can be found in the DOC directory of the source package as
293 Some of the other tools (simulator and assembler) included with SDCC contain
294 their own documentation and can be found in the source distribution.
295 If you want the latest unreleased software, the complete source package
296 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
299 Wishes for the future
302 There are (and always will be) some things that could be done.
303 Here are some I can think of:
310 char KernelFunction3(char p) at 0x340;
316 If you can think of some more, please send them to the list.
322 <pending: And then of course a proper index-table
323 \begin_inset LatexCommand \index{index}
336 The install paths and default search paths are defined when running 'configure'.
337 The defaults can be overriden by
345 These configure variables are compiled into the binaries, and can only be
346 changed by rerunning 'configure' and recompiling SDCC.
347 The configure variables are written in
351 to distinguish them from run time environment variables (see section search
356 Cygwin is handled like a *nix, Mingw32 however belongs to the Win32 builds.
357 The SDCC team uses Mingw32 to build the official Windows binaries, because
364 a gcc compiler and last but not least
367 the binaries can be built by cross compiling on Sourceforge's compile farm.
370 The other Win32 builds using Borland, VC or whatever don't use 'configure',
371 but they (hopefully) use the default Win32 paths.
381 <lyxtabular version="3" rows="8" columns="3">
383 <column alignment="left" valignment="top" leftline="true" width="0in">
384 <column alignment="left" valignment="top" leftline="true" width="0in">
385 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
386 <row topline="true" bottomline="true">
387 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
395 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
403 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
413 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
497 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
557 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
612 <row topline="true" bottomline="true">
613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
623 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
631 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
650 'configure' also computes relative paths.
651 This is needed for full relocatability of a binary package and to complete
652 search paths (see section search paths below):
658 <lyxtabular version="3" rows="4" columns="3">
660 <column alignment="left" valignment="top" leftline="true" width="0in">
661 <column alignment="left" valignment="top" leftline="true" width="0in">
662 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
663 <row topline="true" bottomline="true">
664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
672 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
689 <row topline="true" bottomline="true">
690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
719 <row bottomline="true">
720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
738 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
747 <row bottomline="true">
748 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
766 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
785 Binary files (preprocessor, assembler and linker)
790 <lyxtabular version="3" rows="2" columns="3">
792 <column alignment="left" valignment="top" leftline="true" width="0in">
793 <column alignment="left" valignment="top" leftline="true" width="0in">
794 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
795 <row topline="true" bottomline="true">
796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
812 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
821 <row topline="true" bottomline="true">
822 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
867 <lyxtabular version="3" rows="2" columns="3">
869 <column alignment="block" valignment="top" leftline="true" width="1.6in">
870 <column alignment="center" valignment="top" leftline="true" width="0in">
871 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
872 <row topline="true" bottomline="true">
873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
881 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
889 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
898 <row topline="true" bottomline="true">
899 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
911 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
916 /usr/local/share/sdcc/include
919 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
945 is auto-appended by the compiler, e.g.
946 small, large, z80, ds390 etc.)
951 <lyxtabular version="3" rows="2" columns="3">
953 <column alignment="left" valignment="top" leftline="true" width="0in">
954 <column alignment="left" valignment="top" leftline="true" width="0in">
955 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
956 <row topline="true" bottomline="true">
957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
965 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
982 <row topline="true" bottomline="true">
983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
990 $DATADIR/$LIB_DIR_SUFFIX
993 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
998 /usr/local/share/sdcc/lib
1001 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1027 \begin_inset Tabular
1028 <lyxtabular version="3" rows="2" columns="3">
1030 <column alignment="left" valignment="top" leftline="true" width="0in">
1031 <column alignment="left" valignment="top" leftline="true" width="0in">
1032 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1033 <row topline="true" bottomline="true">
1034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1050 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1059 <row topline="true" bottomline="true">
1060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1075 /usr/local/share/sdcc/doc
1078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1101 Some search paths or parts of them are determined by configure variables
1106 , see section above).
1107 Further search paths are determined by environment variables during runtime.
1110 The paths searched when running the compiler are as follows (the first catch
1116 Binary files (preprocessor, assembler and linker)
1119 \begin_inset Tabular
1120 <lyxtabular version="3" rows="4" columns="3">
1122 <column alignment="left" valignment="top" leftline="true" width="0in">
1123 <column alignment="left" valignment="top" leftline="true" width="0in">
1124 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1125 <row topline="true" bottomline="true">
1126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1151 <row topline="true">
1152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1162 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1181 <row topline="true">
1182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1187 Path of argv[0] (if available)
1190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1207 <row topline="true" bottomline="true">
1208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1245 \begin_inset Tabular
1246 <lyxtabular version="3" rows="6" columns="3">
1248 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1249 <column alignment="block" valignment="top" leftline="true" width="1.5in">
1250 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
1251 <row topline="true" bottomline="true">
1252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1277 <row topline="true">
1278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1286 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1294 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1303 <row topline="true">
1304 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1320 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1329 <row topline="true">
1330 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1344 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1356 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1367 <row topline="true">
1368 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1386 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1436 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1449 <row topline="true" bottomline="true">
1450 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1466 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1471 /usr/local/share/sdcc/
1476 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1493 The option ---nostdinc disables the last two search paths.
1500 With the exception of
1501 \begin_inset Quotes sld
1505 \begin_inset Quotes srd
1512 is auto-appended by the compiler (e.g.
1513 small, large, z80, ds390 etc.).
1517 \begin_inset Tabular
1518 <lyxtabular version="3" rows="6" columns="3">
1520 <column alignment="block" valignment="top" leftline="true" width="1.7in">
1521 <column alignment="left" valignment="top" leftline="true" width="1.2in">
1522 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
1523 <row topline="true" bottomline="true">
1524 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1532 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1549 <row topline="true">
1550 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1575 <row topline="true">
1576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1588 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1600 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1615 <row topline="true">
1616 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1627 $LIB_DIR_SUFFIX/<model>
1630 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1644 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1661 <row topline="true">
1662 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1677 $LIB_DIR_SUFFIX/<model>
1680 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1733 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1752 <row topline="true" bottomline="true">
1753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1762 $LIB_DIR_SUFFIX/<model>
1765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1770 /usr/local/share/sdcc/
1777 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1793 Don't delete any of the stray spaces in the table above without checking
1794 the HTML output (last line)!
1800 The option ---nostdlib disables the last two search paths.
1803 Linux and other gcc-based systems (cygwin, mingw32, osx)
1808 Download the source package
1810 either from the SDCC CVS repository or from the
1811 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1817 , it will be named something like sdcc
1830 Bring up a command line terminal, such as xterm.
1835 Unpack the file using a command like:
1838 "tar -xzf sdcc.src.tar.gz
1843 , this will create a sub-directory called sdcc with all of the sources.
1846 Change directory into the main SDCC directory, for example type:
1863 This configures the package for compilation on your system.
1879 All of the source packages will compile, this can take a while.
1895 This copies the binary executables, the include files, the libraries and
1896 the documentation to the install directories.
1899 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
1900 )) fails to compile SDCC.
1901 Fortunately there's also gcc 2.9.x installed, which works fine.
1902 This compiler can be selected by running 'configure' with:
1905 ./configure CC=gcc2 CXX=g++2
1909 \layout Subsubsection
1911 Windows Install Using a Binary Package
1914 Download the binary package and unpack it using your favorite unpacking
1915 tool (gunzip, WinZip, etc).
1916 This should unpack to a group of sub-directories.
1917 An example directory structure after unpacking the mingw32 package is:
1924 bin for the executables, c:
1944 lib for the include and libraries.
1947 Adjust your environment variable PATH to include the location of the bin
1948 directory or start sdcc using the full path.
1949 \layout Subsubsection
1951 Windows Install Using Cygwin and Mingw32
1954 Follow the instruction in
1956 Linux and other gcc-based systems
1959 \layout Subsubsection
1961 Windows Install Using Microsoft Visual C++ 6.0/NET
1966 Download the source package
1968 either from the SDCC CVS repository or from the
1969 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1975 , it will be named something like sdcc
1982 SDCC is distributed with all the projects, workspaces, and files you need
1983 to build it using Visual C++ 6.0/NET.
1984 The workspace name is 'sdcc.dsw'.
1985 Please note that as it is now, all the executables are created in a folder
1989 Once built you need to copy the executables from sdcc
1993 bin before runnng SDCC.
1998 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
1999 of bison.exe, flex.exe, and gawk.exe.
2000 One good place to get them is
2001 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
2009 Download the file UnxUtils.zip.
2010 Now you have to install the utilities and setup Visual C++ so it can locate
2011 the required programs.
2012 Here there are two alternatives (choose one!):
2019 a) Extract UnxUtils.zip to your C:
2021 hard disk PRESERVING the original paths, otherwise bison won't work.
2022 (If you are using WinZip make certain that 'Use folder names' is selected)
2026 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2027 in 'Show directories for:' select 'Executable files', and in the directories
2028 window add a new path: 'C:
2038 (As a side effect, you get a bunch of Unix utilities that could be useful,
2039 such as diff and patch.)
2046 This one avoids extracting a bunch of files you may not use, but requires
2051 a) Create a directory were to put the tools needed, or use a directory already
2059 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
2060 to such directory WITHOUT preserving the original paths.
2061 (If you are using WinZip make certain that 'Use folder names' is not selected)
2065 c) Rename bison.exe to '_bison.exe'.
2069 d) Create a batch file 'bison.bat' in 'C:
2073 ' and add these lines:
2093 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
2097 Steps 'c' and 'd' are needed because bison requires by default that the
2098 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
2099 '/usr/local/share/' I think.
2100 So it is necessary to tell bison where those files are located if they
2101 are not in such directory.
2102 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
2106 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
2107 in 'Show directories for:' select 'Executable files', and in the directories
2108 window add a new path: 'c:
2111 Note that you can use any other path instead of 'c:
2113 util', even the path where the Visual C++ tools are, probably: 'C:
2117 Microsoft Visual Studio
2122 So you don't have to execute step 'e' :)
2126 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
2127 the executables from sdcc
2131 bin, and you can compile using sdcc.
2132 \layout Subsubsection
2134 Windows Install Using Borland
2137 From the sdcc directory, run the command "make -f Makefile.bcc".
2138 This should regenerate all the .exe files in the bin directory except for
2139 sdcdb.exe (which currently doesn't build under Borland C++).
2142 If you modify any source files and need to rebuild, be aware that the dependanci
2143 es may not be correctly calculated.
2144 The safest option is to delete all .obj files and run the build again.
2145 From a Cygwin BASH prompt, this can easily be done with the commmand:
2155 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
2157 ) -print -exec rm {}
2166 or on Windows NT/2000/XP from the command prompt with the commmand:
2173 del /s *.obj *.lib *.rul
2176 from the sdcc directory.
2179 Testing out the SDCC Compiler
2182 The first thing you should do after installing your SDCC compiler is to
2190 at the prompt, and the program should run and tell you the version.
2191 If it doesn't run, or gives a message about not finding sdcc program, then
2192 you need to check over your installation.
2193 Make sure that the sdcc bin directory is in your executable search path
2194 defined by the PATH environment setting (see the Trouble-shooting section
2196 Make sure that the sdcc program is in the bin folder, if not perhaps something
2197 did not install correctly.
2205 is commonly installed as described in section
2206 \begin_inset Quotes sld
2209 Install and search paths
2210 \begin_inset Quotes srd
2219 Make sure the compiler works on a very simple example.
2220 Type in the following test.c program using your favorite
2255 Compile this using the following command:
2264 If all goes well, the compiler will generate a test.asm and test.rel file.
2265 Congratulations, you've just compiled your first program with SDCC.
2266 We used the -c option to tell SDCC not to link the generated code, just
2267 to keep things simple for this step.
2275 The next step is to try it with the linker.
2285 If all goes well the compiler will link with the libraries and produce
2286 a test.ihx output file.
2291 (no test.ihx, and the linker generates warnings), then the problem is most
2292 likely that sdcc cannot find the
2296 usr/local/share/sdcc/lib directory
2300 (see the Install trouble-shooting section for suggestions).
2308 The final test is to ensure sdcc can use the
2312 header files and libraries.
2313 Edit test.c and change it to the following:
2333 strcpy(str1, "testing");
2342 Compile this by typing
2349 This should generate a test.ihx output file, and it should give no warnings
2350 such as not finding the string.h file.
2351 If it cannot find the string.h file, then the problem is that sdcc cannot
2352 find the /usr/local/share/sdcc/include directory
2356 (see the Install trouble-shooting section for suggestions).
2359 Install Trouble-shooting
2360 \layout Subsubsection
2362 SDCC does not build correctly.
2365 A thing to try is starting from scratch by unpacking the .tgz source package
2366 again in an empty directory.
2374 ./configure 2>&1 | tee configure.log
2388 make 2>&1 | tee make.log
2395 If anything goes wrong, you can review the log files to locate the problem.
2396 Or a relevant part of this can be attached to an email that could be helpful
2397 when requesting help from the mailing list.
2398 \layout Subsubsection
2401 \begin_inset Quotes sld
2405 \begin_inset Quotes srd
2412 \begin_inset Quotes sld
2416 \begin_inset Quotes srd
2419 command is a script that analyzes your system and performs some configuration
2420 to ensure the source package compiles on your system.
2421 It will take a few minutes to run, and will compile a few tests to determine
2422 what compiler features are installed.
2423 \layout Subsubsection
2426 \begin_inset Quotes sld
2430 \begin_inset Quotes srd
2436 This runs the GNU make tool, which automatically compiles all the source
2437 packages into the final installed binary executables.
2438 \layout Subsubsection
2441 \begin_inset Quotes sld
2445 \begin_inset Quotes erd
2451 This will install the compiler, other executables libraries and include
2452 files in to the appropriate directories.
2454 \begin_inset Quotes sld
2457 Install and Search PATHS
2458 \begin_inset Quotes srd
2463 On most systems you will need super-user privilages to do this.
2469 SDCC is not just a compiler, but a collection of tools by various developers.
2470 These include linkers, assemblers, simulators and other components.
2471 Here is a summary of some of the components.
2472 Note that the included simulator and assembler have separate documentation
2473 which you can find in the source package in their respective directories.
2474 As SDCC grows to include support for other processors, other packages from
2475 various developers are included and may have their own sets of documentation.
2479 You might want to look at the files which are installed in <installdir>.
2480 At the time of this writing, we find the following programs for gcc-builds:
2484 In <installdir>/bin:
2487 sdcc - The compiler.
2490 sdcpp - The C preprocessor.
2493 asx8051 - The assembler for 8051 type processors.
2500 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
2503 aslink -The linker for 8051 type processors.
2510 link-gbz80 - The Z80 and GameBoy Z80 linkers.
2513 s51 - The ucSim 8051 simulator.
2516 sdcdb - The source debugger.
2519 packihx - A tool to pack (compress) Intel hex files.
2522 In <installdir>/share/sdcc/include
2528 In <installdir>/share/sdcc/lib
2531 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
2535 In <installdir>/share/sdcc/doc
2541 As development for other processors proceeds, this list will expand to include
2542 executables to support processors like AVR, PIC, etc.
2543 \layout Subsubsection
2548 This is the actual compiler, it in turn uses the c-preprocessor and invokes
2549 the assembler and linkage editor.
2550 \layout Subsubsection
2552 sdcpp - The C-Preprocessor
2555 The preprocessor is a modified version of the GNU preprocessor.
2556 The C preprocessor is used to pull in #include sources, process #ifdef
2557 statements, #defines and so on.
2558 \layout Subsubsection
2560 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
2564 This is retargettable assembler & linkage editor, it was developed by Alan
2566 John Hartman created the version for 8051, and I (Sandeep) have made some
2567 enhancements and bug fixes for it to work properly with the SDCC.
2568 \layout Subsubsection
2573 S51 is a freeware, opensource simulator developed by Daniel Drotos (
2574 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
2579 The simulator is built as part of the build process.
2580 For more information visit Daniel's website at:
2581 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
2586 It currently support the core mcs51, the Dallas DS80C390 and the Philips
2588 \layout Subsubsection
2590 sdcdb - Source Level Debugger
2596 <todo: is this thing still alive?>
2603 Sdcdb is the companion source level debugger.
2604 The current version of the debugger uses Daniel's Simulator S51, but can
2605 be easily changed to use other simulators.
2612 \layout Subsubsection
2614 Single Source File Projects
2617 For single source file 8051 projects the process is very simple.
2618 Compile your programs with the following command
2621 "sdcc sourcefile.c".
2625 This will compile, assemble and link your source file.
2626 Output files are as follows
2630 sourcefile.asm - Assembler source file created by the compiler
2632 sourcefile.lst - Assembler listing file created by the Assembler
2634 sourcefile.rst - Assembler listing file updated with linkedit information,
2635 created by linkage editor
2637 sourcefile.sym - symbol listing for the sourcefile, created by the assembler
2639 sourcefile.rel - Object file created by the assembler, input to Linkage editor
2641 sourcefile.map - The memory map for the load module, created by the Linker
2643 sourcefile.ihx - The load module in Intel hex format (you can select the
2644 Motorola S19 format with ---out-fmt-s19)
2646 sourcefile.cdb - An optional file (with ---debug) containing debug information
2648 sourcefile.dump* - Dump file to debug the compiler it self (with ---dumpall)
2650 \begin_inset Quotes sld
2653 Anatomy of the compiler
2654 \begin_inset Quotes srd
2658 \layout Subsubsection
2660 Projects with Multiple Source Files
2663 SDCC can compile only ONE file at a time.
2664 Let us for example assume that you have a project containing the following
2669 foo1.c (contains some functions)
2671 foo2.c (contains some more functions)
2673 foomain.c (contains more functions and the function main)
2681 The first two files will need to be compiled separately with the commands:
2713 Then compile the source file containing the
2717 function and link the files together with the following command:
2725 foomain.c\SpecialChar ~
2726 foo1.rel\SpecialChar ~
2738 can be separately compiled as well:
2749 sdcc foomain.rel foo1.rel foo2.rel
2756 The file containing the
2771 file specified in the command line, since the linkage editor processes
2772 file in the order they are presented to it.
2773 \layout Subsubsection
2775 Projects with Additional Libraries
2778 Some reusable routines may be compiled into a library, see the documentation
2779 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
2785 Libraries created in this manner can be included in the command line.
2786 Make sure you include the -L <library-path> option to tell the linker where
2787 to look for these files if they are not in the current directory.
2788 Here is an example, assuming you have the source file
2800 (if that is not the same as your current project):
2807 sdcc foomain.c foolib.lib -L mylib
2818 must be an absolute path name.
2822 The most efficient way to use libraries is to keep seperate modules in seperate
2824 The lib file now should name all the modules.rel files.
2825 For an example see the standard library file
2829 in the directory <installdir>/share/lib/small.
2832 Command Line Options
2833 \layout Subsubsection
2835 Processor Selection Options
2837 \labelwidthstring 00.00.0000
2843 Generate code for the MCS51 (8051) family of processors.
2844 This is the default processor target.
2846 \labelwidthstring 00.00.0000
2852 Generate code for the DS80C390 processor.
2854 \labelwidthstring 00.00.0000
2860 Generate code for the Z80 family of processors.
2862 \labelwidthstring 00.00.0000
2868 Generate code for the GameBoy Z80 processor.
2870 \labelwidthstring 00.00.0000
2876 Generate code for the Atmel AVR processor (In development, not complete).
2878 \labelwidthstring 00.00.0000
2884 Generate code for the PIC 14-bit processors (In development, not complete).
2886 \labelwidthstring 00.00.0000
2892 Generate code for the Toshiba TLCS-900H processor (In development, not
2895 \labelwidthstring 00.00.0000
2901 Generate code for the Philips XA51 processor (In development, not complete).
2902 \layout Subsubsection
2904 Preprocessor Options
2906 \labelwidthstring 00.00.0000
2912 The additional location where the pre processor will look for <..h> or
2913 \begin_inset Quotes eld
2917 \begin_inset Quotes erd
2922 \labelwidthstring 00.00.0000
2928 Command line definition of macros.
2929 Passed to the pre processor.
2931 \labelwidthstring 00.00.0000
2937 Tell the preprocessor to output a rule suitable for make describing the
2938 dependencies of each object file.
2939 For each source file, the preprocessor outputs one make-rule whose target
2940 is the object file name for that source file and whose dependencies are
2941 all the files `#include'd in it.
2942 This rule may be a single line or may be continued with `
2944 '-newline if it is long.
2945 The list of rules is printed on standard output instead of the preprocessed
2949 \labelwidthstring 00.00.0000
2955 Tell the preprocessor not to discard comments.
2956 Used with the `-E' option.
2958 \labelwidthstring 00.00.0000
2969 Like `-M' but the output mentions only the user header files included with
2971 \begin_inset Quotes eld
2975 System header files included with `#include <file>' are omitted.
2977 \labelwidthstring 00.00.0000
2983 Assert the answer answer for question, in case it is tested with a preprocessor
2984 conditional such as `#if #question(answer)'.
2985 `-A-' disables the standard assertions that normally describe the target
2988 \labelwidthstring 00.00.0000
2994 (answer) Assert the answer answer for question, in case it is tested with
2995 a preprocessor conditional such as `#if #question(answer)'.
2996 `-A-' disables the standard assertions that normally describe the target
2999 \labelwidthstring 00.00.0000
3005 Undefine macro macro.
3006 `-U' options are evaluated after all `-D' options, but before any `-include'
3007 and `-imacros' options.
3009 \labelwidthstring 00.00.0000
3015 Tell the preprocessor to output only a list of the macro definitions that
3016 are in effect at the end of preprocessing.
3017 Used with the `-E' option.
3019 \labelwidthstring 00.00.0000
3025 Tell the preprocessor to pass all macro definitions into the output, in
3026 their proper sequence in the rest of the output.
3028 \labelwidthstring 00.00.0000
3039 Like `-dD' except that the macro arguments and contents are omitted.
3040 Only `#define name' is included in the output.
3041 \layout Subsubsection
3045 \labelwidthstring 00.00.0000
3055 <absolute path to additional libraries> This option is passed to the linkage
3056 editor's additional libraries search path.
3057 The path name must be absolute.
3058 Additional library files may be specified in the command line.
3059 See section Compiling programs for more details.
3061 \labelwidthstring 00.00.0000
3067 <Value> The start location of the external ram, default value is 0.
3068 The value entered can be in Hexadecimal or Decimal format, e.g.: ---xram-loc
3069 0x8000 or ---xram-loc 32768.
3071 \labelwidthstring 00.00.0000
3077 <Value> The start location of the code segment, default value 0.
3078 Note when this option is used the interrupt vector table is also relocated
3079 to the given address.
3080 The value entered can be in Hexadecimal or Decimal format, e.g.: ---code-loc
3081 0x8000 or ---code-loc 32768.
3083 \labelwidthstring 00.00.0000
3089 <Value> By default the stack is placed after the data segment.
3090 Using this option the stack can be placed anywhere in the internal memory
3092 The value entered can be in Hexadecimal or Decimal format, e.g.
3093 ---stack-loc 0x20 or ---stack-loc 32.
3094 Since the sp register is incremented before a push or call, the initial
3095 sp will be set to one byte prior the provided value.
3096 The provided value should not overlap any other memory areas such as used
3097 register banks or the data segment and with enough space for the current
3100 \labelwidthstring 00.00.0000
3106 <Value> The start location of the internal ram data segment.
3107 The value entered can be in Hexadecimal or Decimal format, eg.
3108 ---data-loc 0x20 or ---data-loc 32.
3109 (By default, the start location of the internal ram data segment is set
3110 as low as possible in memory, taking into account the used register banks
3111 and the bit segment at address 0x20.
3112 For example if register banks 0 and 1 are used without bit variables, the
3113 data segment will be set, if ---data-loc is not used, to location 0x10.)
3115 \labelwidthstring 00.00.0000
3121 <Value> The start location of the indirectly addressable internal ram, default
3123 The value entered can be in Hexadecimal or Decimal format, eg.
3124 ---idata-loc 0x88 or ---idata-loc 136.
3126 \labelwidthstring 00.00.0000
3135 The linker output (final object code) is in Intel Hex format.
3136 (This is the default option).
3138 \labelwidthstring 00.00.0000
3147 The linker output (final object code) is in Motorola S19 format.
3148 \layout Subsubsection
3152 \labelwidthstring 00.00.0000
3158 Generate code for Large model programs see section Memory Models for more
3160 If this option is used all source files in the project should be compiled
3162 In addition the standard library routines are compiled with small model,
3163 they will need to be recompiled.
3165 \labelwidthstring 00.00.0000
3176 Generate code for Small Model programs see section Memory Models for more
3178 This is the default model.
3179 \layout Subsubsection
3183 \labelwidthstring 00.00.0000
3194 Generate 24-bit flat mode code.
3195 This is the one and only that the ds390 code generator supports right now
3196 and is default when using
3201 See section Memory Models for more details.
3203 \labelwidthstring 00.00.0000
3209 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
3210 This is the one and only that the ds390 code generator supports right now
3211 and is default when using
3216 In this mode, the stack is located in the lower 1K of the internal RAM,
3217 which is mapped to 0x400000.
3218 Note that the support is incomplete, since it still uses a single byte
3219 as the stack pointer.
3220 This means that only the lower 256 bytes of the potential 1K stack space
3221 will actually be used.
3222 However, this does allow you to reclaim the precious 256 bytes of low RAM
3223 for use for the DATA and IDATA segments.
3224 The compiler will not generate any code to put the processor into 10 bit
3226 It is important to ensure that the processor is in this mode before calling
3227 any re-entrant functions compiled with this option.
3228 In principle, this should work with the
3232 option, but that has not been tested.
3233 It is incompatible with the
3238 It also only makes sense if the processor is in 24 bit contiguous addressing
3241 ---model-flat24 option
3244 \layout Subsubsection
3246 Optimization Options
3248 \labelwidthstring 00.00.0000
3254 Will not do global subexpression elimination, this option may be used when
3255 the compiler creates undesirably large stack/data spaces to store compiler
3257 A warning message will be generated when this happens and the compiler
3258 will indicate the number of extra bytes it allocated.
3259 It recommended that this option NOT be used, #pragma\SpecialChar ~
3261 to turn off global subexpression elimination for a given function only.
3263 \labelwidthstring 00.00.0000
3269 Will not do loop invariant optimizations, this may be turned off for reasons
3270 explained for the previous option.
3271 For more details of loop optimizations performed see section Loop Invariants.It
3272 recommended that this option NOT be used, #pragma\SpecialChar ~
3273 NOINVARIANT can be used
3274 to turn off invariant optimizations for a given function only.
3276 \labelwidthstring 00.00.0000
3282 Will not do loop induction optimizations, see section strength reduction
3283 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
3285 ION can be used to turn off induction optimizations for a given function
3288 \labelwidthstring 00.00.0000
3299 Will not generate boundary condition check when switch statements are implement
3300 ed using jump-tables.
3301 See section Switch Statements for more details.
3302 It is recommended that this option is NOT used, #pragma\SpecialChar ~
3304 used to turn off boundary checking for jump tables for a given function
3307 \labelwidthstring 00.00.0000
3316 Will not do loop reversal optimization.
3318 \labelwidthstring 00.00.0000
3324 Will not optimize labels (makes the dumpfiles more readable).
3326 \labelwidthstring 00.00.0000
3332 Will not memcpy initialized data in far space from code space.
3333 This saves a few bytes in code space if you don't have initialized data.
3334 \layout Subsubsection
3338 \labelwidthstring 00.00.0000
3345 will compile and assemble the source, but will not call the linkage editor.
3347 \labelwidthstring 00.00.0000
3353 reads the preprocessed source from standard input and compiles it.
3354 The file name for the assembler output must be specified using the -o option.
3356 \labelwidthstring 00.00.0000
3362 Run only the C preprocessor.
3363 Preprocess all the C source files specified and output the results to standard
3366 \labelwidthstring 00.00.0000
3373 The output path resp.
3374 file where everything will be placed.
3375 If the parameter is a path, it must have a trailing slash (or backslash
3376 for the Windows binaries) to be recognized as a path.
3379 \labelwidthstring 00.00.0000
3390 All functions in the source file will be compiled as
3395 the parameters and local variables will be allocated on the stack.
3396 see section Parameters and Local Variables for more details.
3397 If this option is used all source files in the project should be compiled
3401 \labelwidthstring 00.00.0000
3407 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
3408 variables and passing parameters.
3409 See section on external stack for more details.
3411 \labelwidthstring 00.00.0000
3415 ---callee-saves function1[,function2][,function3]....
3418 The compiler by default uses a caller saves convention for register saving
3419 across function calls, however this can cause unneccessary register pushing
3420 & popping when calling small functions from larger functions.
3421 This option can be used to switch the register saving convention for the
3422 function names specified.
3423 The compiler will not save registers when calling these functions, no extra
3424 code will be generated at the entry & exit for these functions to save
3425 & restore the registers used by these functions, this can SUBSTANTIALLY
3426 reduce code & improve run time performance of the generated code.
3427 In the future the compiler (with interprocedural analysis) will be able
3428 to determine the appropriate scheme to use for each function call.
3429 DO NOT use this option for built-in functions such as _muluint..., if this
3430 option is used for a library function the appropriate library function
3431 needs to be recompiled with the same option.
3432 If the project consists of multiple source files then all the source file
3433 should be compiled with the same ---callee-saves option string.
3434 Also see #pragma\SpecialChar ~
3437 \labelwidthstring 00.00.0000
3446 When this option is used the compiler will generate debug information, that
3447 can be used with the SDCDB.
3448 The debug information is collected in a file with .cdb extension.
3449 For more information see documentation for SDCDB.
3451 \labelwidthstring 00.00.0000
3457 <filename> This option can be used to use additional rules to be used by
3458 the peep hole optimizer.
3459 See section Peep Hole optimizations for details on how to write these rules.
3461 \labelwidthstring 00.00.0000
3472 Stop after the stage of compilation proper; do not assemble.
3473 The output is an assembler code file for the input file specified.
3475 \labelwidthstring 00.00.0000
3479 -Wa_asmOption[,asmOption]
3482 Pass the asmOption to the assembler.
3484 \labelwidthstring 00.00.0000
3488 -Wl_linkOption[,linkOption]
3491 Pass the linkOption to the linker.
3493 \labelwidthstring 00.00.0000
3502 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
3503 Note by default these libraries are compiled as non-reentrant.
3504 See section Installation for more details.
3506 \labelwidthstring 00.00.0000
3515 This option will cause the compiler to generate an information message for
3516 each function in the source file.
3517 The message contains some
3521 information about the function.
3522 The number of edges and nodes the compiler detected in the control flow
3523 graph of the function, and most importantly the
3525 cyclomatic complexity
3527 see section on Cyclomatic Complexity for more details.
3529 \labelwidthstring 00.00.0000
3538 Floating point library is compiled as reentrant.See section Installation
3541 \labelwidthstring 00.00.0000
3547 The compiler will not overlay parameters and local variables of any function,
3548 see section Parameters and local variables for more details.
3550 \labelwidthstring 00.00.0000
3556 This option can be used when the code generated is called by a monitor
3558 The compiler will generate a 'ret' upon return from the 'main' function.
3559 The default option is to lock up i.e.
3562 \labelwidthstring 00.00.0000
3568 Disable peep-hole optimization.
3570 \labelwidthstring 00.00.0000
3576 Pass the inline assembler code through the peep hole optimizer.
3577 This can cause unexpected changes to inline assembler code, please go through
3578 the peephole optimizer rules defined in the source file tree '<target>/peeph.def
3579 ' before using this option.
3581 \labelwidthstring 00.00.0000
3587 <Value> Causes the linker to check if the internal ram usage is within limits
3590 \labelwidthstring 00.00.0000
3596 <Value> Causes the linker to check if the external ram usage is within limits
3599 \labelwidthstring 00.00.0000
3605 <Value> Causes the linker to check if the code usage is within limits of
3608 \labelwidthstring 00.00.0000
3614 This will prevent the compiler from passing on the default include path
3615 to the preprocessor.
3617 \labelwidthstring 00.00.0000
3623 This will prevent the compiler from passing on the default library path
3626 \labelwidthstring 00.00.0000
3632 Shows the various actions the compiler is performing.
3634 \labelwidthstring 00.00.0000
3640 Shows the actual commands the compiler is executing.
3642 \labelwidthstring 00.00.0000
3648 Hides your ugly and inefficient c-code from the asm file, so you can always
3649 blame the compiler :).
3651 \labelwidthstring 00.00.0000
3657 Include i-codes in the asm file.
3658 Sounds like noise but is most helpfull for debugging the compiler itself.
3660 \labelwidthstring 00.00.0000
3666 Disable some of the more pedantic warnings (jwk burps: please be more specific
3668 \layout Subsubsection
3670 Intermediate Dump Options
3673 The following options are provided for the purpose of retargetting and debugging
3675 These provided a means to dump the intermediate code (iCode) generated
3676 by the compiler in human readable form at various stages of the compilation
3680 \labelwidthstring 00.00.0000
3686 This option will cause the compiler to dump the intermediate code into
3689 <source filename>.dumpraw
3691 just after the intermediate code has been generated for a function, i.e.
3692 before any optimizations are done.
3693 The basic blocks at this stage ordered in the depth first number, so they
3694 may not be in sequence of execution.
3696 \labelwidthstring 00.00.0000
3702 Will create a dump of iCode's, after global subexpression elimination,
3705 <source filename>.dumpgcse.
3707 \labelwidthstring 00.00.0000
3713 Will create a dump of iCode's, after deadcode elimination, into a file
3716 <source filename>.dumpdeadcode.
3718 \labelwidthstring 00.00.0000
3727 Will create a dump of iCode's, after loop optimizations, into a file named
3730 <source filename>.dumploop.
3732 \labelwidthstring 00.00.0000
3741 Will create a dump of iCode's, after live range analysis, into a file named
3744 <source filename>.dumprange.
3746 \labelwidthstring 00.00.0000
3752 Will dump the life ranges for all symbols.
3754 \labelwidthstring 00.00.0000
3763 Will create a dump of iCode's, after register assignment, into a file named
3766 <source filename>.dumprassgn.
3768 \labelwidthstring 00.00.0000
3774 Will create a dump of the live ranges of iTemp's
3776 \labelwidthstring 00.00.0000
3787 Will cause all the above mentioned dumps to be created.
3790 Environment variables
3793 SDCC recognizes the following environment variables:
3795 \labelwidthstring 00.00.0000
3801 SDCC installs a signal handler to be able to delete temporary files after
3802 an user break (^C) or an exception.
3803 If this environment variable is set, SDCC won't install the signal handler
3804 in order to be able to debug SDCC.
3806 \labelwidthstring 00.00.0000
3814 Path, where temporary files will be created.
3815 The order of the variables is the search order.
3816 In a standard *nix environment these variables are not set, and there's
3817 no need to set them.
3818 On Windows it's recommended to set one of them.
3820 \labelwidthstring 00.00.0000
3827 \begin_inset Quotes sld
3830 2.3 Install and search paths
3831 \begin_inset Quotes srd
3836 \labelwidthstring 00.00.0000
3843 \begin_inset Quotes sld
3846 2.3 Install and search paths
3847 \begin_inset Quotes srd
3852 \labelwidthstring 00.00.0000
3859 \begin_inset Quotes sld
3862 2.3 Install and search paths
3863 \begin_inset Quotes srd
3869 There are some more environment variables recognized by SDCC, but these
3870 are solely used for debugging purposes.
3871 They can change or disappear very quickly, and will never be documentated.
3874 MCS51/DS390 Storage Class Language Extensions
3877 In addition to the ANSI storage classes SDCC allows the following MCS51
3878 specific storage classes.
3879 \layout Subsubsection
3884 Variables declared with this storage class will be placed in the extern
3890 storage class for Large Memory model, e.g.:
3896 xdata unsigned char xduc;
3897 \layout Subsubsection
3906 storage class for Small Memory model.
3907 Variables declared with this storage class will be allocated in the internal
3915 \layout Subsubsection
3920 Variables declared with this storage class will be allocated into the indirectly
3921 addressable portion of the internal ram of a 8051, e.g.:
3928 \layout Subsubsection
3933 This is a data-type and a storage class specifier.
3934 When a variable is declared as a bit, it is allocated into the bit addressable
3935 memory of 8051, e.g.:
3942 \layout Subsubsection
3947 Like the bit keyword,
3951 signifies both a data-type and storage class, they are used to describe
3952 the special function registers and special bit variables of a 8051, eg:
3958 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
3960 sbit at 0xd7 CY; /* CY (Carry Flag) */
3966 SDCC allows (via language extensions) pointers to explicitly point to any
3967 of the memory spaces of the 8051.
3968 In addition to the explicit pointers, the compiler uses (by default) generic
3969 pointers which can be used to point to any of the memory spaces.
3973 Pointer declaration examples:
3982 /* pointer physically in xternal ram pointing to object in internal ram
3985 data unsigned char * xdata p;
3989 /* pointer physically in code rom pointing to data in xdata space */
3991 xdata unsigned char * code p;
3995 /* pointer physically in code space pointing to data in code space */
3997 code unsigned char * code p;
4001 /* the folowing is a generic pointer physically located in xdata space */
4012 Well you get the idea.
4017 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
4030 The highest order byte of the
4034 pointers contains the data space information.
4035 Assembler support routines are called whenever data is stored or retrieved
4041 These are useful for developing reusable library routines.
4042 Explicitly specifying the pointer type will generate the most efficient
4046 Parameters & Local Variables
4049 Automatic (local) variables and parameters to functions can either be placed
4050 on the stack or in data-space.
4051 The default action of the compiler is to place these variables in the internal
4052 RAM (for small model) or external RAM (for large model).
4053 This in fact makes them
4057 so by default functions are non-reentrant.
4061 They can be placed on the stack either by using the
4065 option or by using the
4069 keyword in the function declaration, e.g.:
4078 unsigned char foo(char i) reentrant
4091 Since stack space on 8051 is limited, the
4099 option should be used sparingly.
4100 Note that the reentrant keyword just means that the parameters & local
4101 variables will be allocated to the stack, it
4105 mean that the function is register bank independent.
4109 Local variables can be assigned storage classes and absolute addresses,
4116 unsigned char foo() {
4122 xdata unsigned char i;
4134 data at 0x31 unsiged char j;
4149 In the above example the variable
4153 will be allocated in the external ram,
4157 in bit addressable space and
4166 or when a function is declared as
4170 this should only be done for static variables.
4173 Parameters however are not allowed any storage class, (storage classes for
4174 parameters will be ignored), their allocation is governed by the memory
4175 model in use, and the reentrancy options.
4181 For non-reentrant functions SDCC will try to reduce internal ram space usage
4182 by overlaying parameters and local variables of a function (if possible).
4183 Parameters and local variables of a function will be allocated to an overlayabl
4184 e segment if the function has
4186 no other function calls and the function is non-reentrant and the memory
4190 If an explicit storage class is specified for a local variable, it will
4194 Note that the compiler (not the linkage editor) makes the decision for overlayin
4196 Functions that are called from an interrupt service routine should be preceded
4197 by a #pragma\SpecialChar ~
4198 NOOVERLAY if they are not reentrant.
4201 Also note that the compiler does not do any processing of inline assembler
4202 code, so the compiler might incorrectly assign local variables and parameters
4203 of a function into the overlay segment if the inline assembler code calls
4204 other c-functions that might use the overlay.
4205 In that case the #pragma\SpecialChar ~
4206 NOOVERLAY should be used.
4209 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
4210 tion or division will NOT be overlayed since these are implemented using
4211 external functions, e.g.:
4221 void set_error(unsigned char errcd)
4237 void some_isr () interrupt 2 using 1
4266 In the above example the parameter
4274 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
4276 not present, this could cause unpredictable runtime behavior when called
4278 The #pragma\SpecialChar ~
4279 NOOVERLAY ensures that the parameters and local variables for
4280 the function are NOT overlayed.
4283 Interrupt Service Routines
4286 SDCC allows interrupt service routines to be coded in C, with some extended
4293 void timer_isr (void) interrupt 2 using 1
4306 The number following the
4310 keyword is the interrupt number this routine will service.
4311 The compiler will insert a call to this routine in the interrupt vector
4312 table for the interrupt number specified.
4317 keyword is used to tell the compiler to use the specified register bank
4318 (8051 specific) when generating code for this function.
4319 Note that when some function is called from an interrupt service routine
4320 it should be preceded by a #pragma\SpecialChar ~
4321 NOOVERLAY if it is not reentrant.
4322 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
4323 ation & modulus operations are implemented using external support routines
4324 developed in ANSI-C, if an interrupt service routine needs to do any of
4325 these operations then the support routines (as mentioned in a following
4326 section) will have to be recompiled using the
4330 option and the source file will need to be compiled using the
4337 If you have multiple source files in your project, interrupt service routines
4338 can be present in any of them, but a prototype of the isr MUST be present
4339 or included in the file that contains the function
4346 Interrupt Numbers and the corresponding address & descriptions for the Standard
4347 8051 are listed below.
4348 SDCC will automatically adjust the interrupt vector table to the maximum
4349 interrupt number specified.
4355 \begin_inset Tabular
4356 <lyxtabular version="3" rows="6" columns="3">
4358 <column alignment="center" valignment="top" leftline="true" width="0in">
4359 <column alignment="center" valignment="top" leftline="true" width="0in">
4360 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
4361 <row topline="true" bottomline="true">
4362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4387 <row topline="true">
4388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4396 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4404 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4413 <row topline="true">
4414 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4422 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4430 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4439 <row topline="true">
4440 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4465 <row topline="true">
4466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4491 <row topline="true" bottomline="true">
4492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4500 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4508 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4525 If the interrupt service routine is defined without
4529 a register bank or with register bank 0 (using 0), the compiler will save
4530 the registers used by itself on the stack upon entry and restore them at
4531 exit, however if such an interrupt service routine calls another function
4532 then the entire register bank will be saved on the stack.
4533 This scheme may be advantageous for small interrupt service routines which
4534 have low register usage.
4537 If the interrupt service routine is defined to be using a specific register
4542 are save and restored, if such an interrupt service routine calls another
4543 function (using another register bank) then the entire register bank of
4544 the called function will be saved on the stack.
4545 This scheme is recommended for larger interrupt service routines.
4548 Calling other functions from an interrupt service routine is not recommended,
4549 avoid it if possible.
4553 Also see the _naked modifier.
4561 <TODO: this isn't implemented at all!>
4567 A special keyword may be associated with a function declaring it as
4572 SDCC will generate code to disable all interrupts upon entry to a critical
4573 function and enable them back before returning.
4574 Note that nesting critical functions may cause unpredictable results.
4599 The critical attribute maybe used with other attributes like
4607 A special keyword may be associated with a function declaring it as
4616 function modifier attribute prevents the compiler from generating prologue
4617 and epilogue code for that function.
4618 This means that the user is entirely responsible for such things as saving
4619 any registers that may need to be preserved, selecting the proper register
4620 bank, generating the
4624 instruction at the end, etc.
4625 Practically, this means that the contents of the function must be written
4626 in inline assembler.
4627 This is particularly useful for interrupt functions, which can have a large
4628 (and often unnecessary) prologue/epilogue.
4629 For example, compare the code generated by these two functions:
4635 data unsigned char counter;
4637 void simpleInterrupt(void) interrupt 1
4651 void nakedInterrupt(void) interrupt 2 _naked
4684 ; MUST explicitly include ret in _naked function.
4698 For an 8051 target, the generated simpleInterrupt looks like:
4843 whereas nakedInterrupt looks like:
4868 ; MUST explicitly include ret(i) in _naked function.
4874 While there is nothing preventing you from writing C code inside a _naked
4875 function, there are many ways to shoot yourself in the foot doing this,
4876 and it is recommended that you stick to inline assembler.
4879 Functions using private banks
4886 attribute (which tells the compiler to use a register bank other than the
4887 default bank zero) should only be applied to
4891 functions (see note 1 below).
4892 This will in most circumstances make the generated ISR code more efficient
4893 since it will not have to save registers on the stack.
4900 attribute will have no effect on the generated code for a
4904 function (but may occasionally be useful anyway
4910 possible exception: if a function is called ONLY from 'interrupt' functions
4911 using a particular bank, it can be declared with the same 'using' attribute
4912 as the calling 'interrupt' functions.
4913 For instance, if you have several ISRs using bank one, and all of them
4914 call memcpy(), it might make sense to create a specialized version of memcpy()
4915 'using 1', since this would prevent the ISR from having to save bank zero
4916 to the stack on entry and switch to bank zero before calling the function
4923 (pending: I don't think this has been done yet)
4930 function using a non-zero bank will assume that it can trash that register
4931 bank, and will not save it.
4932 Since high-priority interrupts can interrupt low-priority ones on the 8051
4933 and friends, this means that if a high-priority ISR
4937 a particular bank occurs while processing a low-priority ISR
4941 the same bank, terrible and bad things can happen.
4942 To prevent this, no single register bank should be
4946 by both a high priority and a low priority ISR.
4947 This is probably most easily done by having all high priority ISRs use
4948 one bank and all low priority ISRs use another.
4949 If you have an ISR which can change priority at runtime, you're on your
4950 own: I suggest using the default bank zero and taking the small performance
4954 It is most efficient if your ISR calls no other functions.
4955 If your ISR must call other functions, it is most efficient if those functions
4956 use the same bank as the ISR (see note 1 below); the next best is if the
4957 called functions use bank zero.
4958 It is very inefficient to call a function using a different, non-zero bank
4966 Data items can be assigned an absolute address with the
4970 keyword, in addition to a storage class, e.g.:
4976 xdata at 0x8000 unsigned char PORTA_8255 ;
4982 In the above example the PORTA_8255 will be allocated to the location 0x8000
4983 of the external ram.
4984 Note that this feature is provided to give the programmer access to
4988 devices attached to the controller.
4989 The compiler does not actually reserve any space for variables declared
4990 in this way (they are implemented with an equate in the assembler).
4991 Thus it is left to the programmer to make sure there are no overlaps with
4992 other variables that are declared without the absolute address.
4993 The assembler listing file (.lst) and the linker output files (.rst) and
4994 (.map) are a good places to look for such overlaps.
4998 Absolute address can be specified for variables in all storage classes,
5011 The above example will allocate the variable at offset 0x02 in the bit-addressab
5013 There is no real advantage to assigning absolute addresses to variables
5014 in this manner, unless you want strict control over all the variables allocated.
5020 The compiler inserts a call to the C routine
5022 _sdcc__external__startup()
5027 at the start of the CODE area.
5028 This routine is in the runtime library.
5029 By default this routine returns 0, if this routine returns a non-zero value,
5030 the static & global variable initialization will be skipped and the function
5031 main will be invoked Other wise static & global variables will be initialized
5032 before the function main is invoked.
5035 _sdcc__external__startup()
5037 routine to your program to override the default if you need to setup hardware
5038 or perform some other critical operation prior to static & global variable
5042 Inline Assembler Code
5045 SDCC allows the use of in-line assembler with a few restriction as regards
5047 All labels defined within inline assembler code
5055 where nnnn is a number less than 100 (which implies a limit of utmost 100
5056 inline assembler labels
5064 It is strongly recommended that each assembly instruction (including labels)
5065 be placed in a separate line (as the example shows).
5070 command line option is used, the inline assembler code will be passed through
5071 the peephole optimizer.
5072 This might cause some unexpected changes in the inline assembler code.
5073 Please go throught the peephole optimizer rules defined in file
5077 carefully before using this option.
5117 The inline assembler code can contain any valid code understood by the assembler
5118 , this includes any assembler directives and comment lines.
5119 The compiler does not do any validation of the code within the
5129 Inline assembler code cannot reference any C-Labels, however it can reference
5130 labels defined by the inline assembler, e.g.:
5156 ; some assembler code
5176 /* some more c code */
5178 clabel:\SpecialChar ~
5180 /* inline assembler cannot reference this label */
5192 $0003: ;label (can be reference by inline assembler only)
5204 /* some more c code */
5212 In other words inline assembly code can access labels defined in inline
5213 assembly within the scope of the funtion.
5217 The same goes the other way, ie.
5218 labels defines in inline assembly CANNOT be accessed by C statements.
5221 int (16 bit) and long (32 bit) Support
5224 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
5225 multiplication and modulus operations are implemented by support routines.
5226 These support routines are all developed in ANSI-C to facilitate porting
5227 to other MCUs, although some model specific assembler optimations are used.
5228 The following files contain the described routine, all of them can be found
5229 in <installdir>/share/sdcc/lib.
5235 <pending: tabularise this>
5241 _mulsint.c - signed 16 bit multiplication (calls _muluint)
5243 _muluint.c - unsigned 16 bit multiplication
5245 _divsint.c - signed 16 bit division (calls _divuint)
5247 _divuint.c - unsigned 16 bit division
5249 _modsint.c - signed 16 bit modulus (call _moduint)
5251 _moduint.c - unsigned 16 bit modulus
5253 _mulslong.c - signed 32 bit multiplication (calls _mululong)
5255 _mululong.c - unsigned32 bit multiplication
5257 _divslong.c - signed 32 division (calls _divulong)
5259 _divulong.c - unsigned 32 division
5261 _modslong.c - signed 32 bit modulus (calls _modulong)
5263 _modulong.c - unsigned 32 bit modulus
5271 Since they are compiled as
5275 , interrupt service routines should not do any of the above operations.
5276 If this is unavoidable then the above routines will need to be compiled
5281 option, after which the source program will have to be compiled with
5288 Floating Point Support
5291 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
5292 point support routines are derived from gcc's floatlib.c and consists of
5293 the following routines:
5299 <pending: tabularise this>
5305 _fsadd.c - add floating point numbers
5307 _fssub.c - subtract floating point numbers
5309 _fsdiv.c - divide floating point numbers
5311 _fsmul.c - multiply floating point numbers
5313 _fs2uchar.c - convert floating point to unsigned char
5315 _fs2char.c - convert floating point to signed char
5317 _fs2uint.c - convert floating point to unsigned int
5319 _fs2int.c - convert floating point to signed int
5321 _fs2ulong.c - convert floating point to unsigned long
5323 _fs2long.c - convert floating point to signed long
5325 _uchar2fs.c - convert unsigned char to floating point
5327 _char2fs.c - convert char to floating point number
5329 _uint2fs.c - convert unsigned int to floating point
5331 _int2fs.c - convert int to floating point numbers
5333 _ulong2fs.c - convert unsigned long to floating point number
5335 _long2fs.c - convert long to floating point number
5343 Note if all these routines are used simultaneously the data space might
5345 For serious floating point usage it is strongly recommended that the large
5352 SDCC allows two memory models for MCS51 code, small and large.
5353 Modules compiled with different memory models should
5357 be combined together or the results would be unpredictable.
5358 The library routines supplied with the compiler are compiled as both small
5360 The compiled library modules are contained in seperate directories as small
5361 and large so that you can link to either set.
5365 When the large model is used all variables declared without a storage class
5366 will be allocated into the external ram, this includes all parameters and
5367 local variables (for non-reentrant functions).
5368 When the small model is used variables without storage class are allocated
5369 in the internal ram.
5372 Judicious usage of the processor specific storage classes and the 'reentrant'
5373 function type will yield much more efficient code, than using the large
5375 Several optimizations are disabled when the program is compiled using the
5376 large model, it is therefore strongly recommdended that the small model
5377 be used unless absolutely required.
5383 The only model supported is Flat 24.
5384 This generates code for the 24 bit contiguous addressing mode of the Dallas
5386 In this mode, up to four meg of external RAM or code space can be directly
5388 See the data sheets at www.dalsemi.com for further information on this part.
5392 In older versions of the compiler, this option was used with the MCS51 code
5398 Now, however, the '390 has it's own code generator, selected by the
5407 Note that the compiler does not generate any code to place the processor
5408 into 24 bitmode (although
5412 in the ds390 libraries will do that for you).
5417 , the boot loader or similar code must ensure that the processor is in 24
5418 bit contiguous addressing mode before calling the SDCC startup code.
5426 option, variables will by default be placed into the XDATA segment.
5431 Segments may be placed anywhere in the 4 meg address space using the usual
5433 Note that if any segments are located above 64K, the -r flag must be passed
5434 to the linker to generate the proper segment relocations, and the Intel
5435 HEX output format must be used.
5436 The -r flag can be passed to the linker by using the option
5440 on the sdcc command line.
5441 However, currently the linker can not handle code segments > 64k.
5444 Defines Created by the Compiler
5447 The compiler creates the following #defines.
5450 SDCC - this Symbol is always defined.
5453 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
5457 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
5461 SDCC_STACK_AUTO - this symbol is defined when
5468 SDCC_MODEL_SMALL - when
5475 SDCC_MODEL_LARGE - when
5482 SDCC_USE_XSTACK - when
5489 SDCC_STACK_TENBIT - when
5496 SDCC_MODEL_FLAT24 - when
5509 SDCC performs a host of standard optimizations in addition to some MCU specific
5512 \layout Subsubsection
5514 Sub-expression Elimination
5517 The compiler does local and global common subexpression elimination, e.g.:
5532 will be translated to
5548 Some subexpressions are not as obvious as the above example, e.g.:
5562 In this case the address arithmetic a->b[i] will be computed only once;
5563 the equivalent code in C would be.
5579 The compiler will try to keep these temporary variables in registers.
5580 \layout Subsubsection
5582 Dead-Code Elimination
5597 i = 1; \SpecialChar ~
5602 global = 1;\SpecialChar ~
5615 global = 3;\SpecialChar ~
5630 int global; void f ()
5643 \layout Subsubsection
5704 Note: the dead stores created by this copy propagation will be eliminated
5705 by dead-code elimination.
5706 \layout Subsubsection
5711 Two types of loop optimizations are done by SDCC loop invariant lifting
5712 and strength reduction of loop induction variables.
5713 In addition to the strength reduction the optimizer marks the induction
5714 variables and the register allocator tries to keep the induction variables
5715 in registers for the duration of the loop.
5716 Because of this preference of the register allocator, loop induction optimizati
5717 on causes an increase in register pressure, which may cause unwanted spilling
5718 of other temporary variables into the stack / data space.
5719 The compiler will generate a warning message when it is forced to allocate
5720 extra space either on the stack or data space.
5721 If this extra space allocation is undesirable then induction optimization
5722 can be eliminated either for the entire source file (with ---noinduction
5723 option) or for a given function only using #pragma\SpecialChar ~
5734 for (i = 0 ; i < 100 ; i ++)
5752 for (i = 0; i < 100; i++)
5762 As mentioned previously some loop invariants are not as apparent, all static
5763 address computations are also moved out of the loop.
5767 Strength Reduction, this optimization substitutes an expression by a cheaper
5774 for (i=0;i < 100; i++)
5794 for (i=0;i< 100;i++) {
5798 ar[itemp1] = itemp2;
5814 The more expensive multiplication is changed to a less expensive addition.
5815 \layout Subsubsection
5820 This optimization is done to reduce the overhead of checking loop boundaries
5821 for every iteration.
5822 Some simple loops can be reversed and implemented using a
5823 \begin_inset Quotes eld
5826 decrement and jump if not zero
5827 \begin_inset Quotes erd
5831 SDCC checks for the following criterion to determine if a loop is reversible
5832 (note: more sophisticated compilers use data-dependency analysis to make
5833 this determination, SDCC uses a more simple minded analysis).
5836 The 'for' loop is of the form
5842 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
5852 The <for body> does not contain
5853 \begin_inset Quotes eld
5857 \begin_inset Quotes erd
5861 \begin_inset Quotes erd
5867 All goto's are contained within the loop.
5870 No function calls within the loop.
5873 The loop control variable <sym> is not assigned any value within the loop
5876 The loop control variable does NOT participate in any arithmetic operation
5880 There are NO switch statements in the loop.
5881 \layout Subsubsection
5883 Algebraic Simplifications
5886 SDCC does numerous algebraic simplifications, the following is a small sub-set
5887 of these optimizations.
5893 i = j + 0 ; /* changed to */ i = j;
5895 i /= 2; /* changed to */ i >>= 1;
5897 i = j - j ; /* changed to */ i = 0;
5899 i = j / 1 ; /* changed to */ i = j;
5905 Note the subexpressions given above are generally introduced by macro expansions
5906 or as a result of copy/constant propagation.
5907 \layout Subsubsection
5912 SDCC changes switch statements to jump tables when the following conditions
5917 The case labels are in numerical sequence, the labels need not be in order,
5918 and the starting number need not be one or zero.
5924 switch(i) {\SpecialChar ~
6031 Both the above switch statements will be implemented using a jump-table.
6034 The number of case labels is at least three, since it takes two conditional
6035 statements to handle the boundary conditions.
6038 The number of case labels is less than 84, since each label takes 3 bytes
6039 and a jump-table can be utmost 256 bytes long.
6043 Switch statements which have gaps in the numeric sequence or those that
6044 have more that 84 case labels can be split into more than one switch statement
6045 for efficient code generation, e.g.:
6083 If the above switch statement is broken down into two switch statements
6117 case 9: \SpecialChar ~
6127 case 12:\SpecialChar ~
6137 then both the switch statements will be implemented using jump-tables whereas
6138 the unmodified switch statement will not be.
6139 \layout Subsubsection
6141 Bit-shifting Operations.
6144 Bit shifting is one of the most frequently used operation in embedded programmin
6146 SDCC tries to implement bit-shift operations in the most efficient way
6166 generates the following code:
6184 In general SDCC will never setup a loop if the shift count is known.
6224 Note that SDCC stores numbers in little-endian format (i.e.
6225 lowest order first).
6226 \layout Subsubsection
6231 A special case of the bit-shift operation is bit rotation, SDCC recognizes
6232 the following expression to be a left bit-rotation:
6243 i = ((i << 1) | (i >> 7));
6251 will generate the following code:
6267 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
6268 ns of this case will also be recognized as bit-rotation, i.e.:
6274 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
6275 \layout Subsubsection
6280 It is frequently required to obtain the highest order bit of an integral
6281 type (long, int, short or char types).
6282 SDCC recognizes the following expression to yield the highest order bit
6283 and generates optimized code for it, e.g.:
6304 hob = (gint >> 15) & 1;
6317 will generate the following code:
6356 000A E5*01\SpecialChar ~
6384 000C 33\SpecialChar ~
6415 000D E4\SpecialChar ~
6446 000E 13\SpecialChar ~
6477 000F F5*02\SpecialChar ~
6507 Variations of this case however will
6512 It is a standard C expression, so I heartily recommend this be the only
6513 way to get the highest order bit, (it is portable).
6514 Of course it will be recognized even if it is embedded in other expressions,
6521 xyz = gint + ((gint >> 15) & 1);
6527 will still be recognized.
6528 \layout Subsubsection
6533 The compiler uses a rule based, pattern matching and re-writing mechanism
6534 for peep-hole optimization.
6539 a peep-hole optimizer by Christopher W.
6540 Fraser (cwfraser@microsoft.com).
6541 A default set of rules are compiled into the compiler, additional rules
6542 may be added with the
6544 ---peep-file <filename>
6547 The rule language is best illustrated with examples.
6575 The above rule will change the following assembly sequence:
6605 Note: All occurrences of a
6609 (pattern variable) must denote the same string.
6610 With the above rule, the assembly sequence:
6628 will remain unmodified.
6632 Other special case optimizations may be added by the user (via
6638 some variants of the 8051 MCU allow only
6647 The following two rules will change all
6669 replace { lcall %1 } by { acall %1 }
6671 replace { ljmp %1 } by { ajmp %1 }
6679 inline-assembler code
6681 is also passed through the peep hole optimizer, thus the peephole optimizer
6682 can also be used as an assembly level macro expander.
6683 The rules themselves are MCU dependent whereas the rule language infra-structur
6684 e is MCU independent.
6685 Peephole optimization rules for other MCU can be easily programmed using
6690 The syntax for a rule is as follows:
6696 rule := replace [ restart ] '{' <assembly sequence> '
6734 <assembly sequence> '
6752 '}' [if <functionName> ] '
6760 <assembly sequence> := assembly instruction (each instruction including
6761 labels must be on a separate line).
6765 The optimizer will apply to the rules one by one from the top in the sequence
6766 of their appearance, it will terminate when all rules are exhausted.
6767 If the 'restart' option is specified, then the optimizer will start matching
6768 the rules again from the top, this option for a rule is expensive (performance)
6769 , it is intended to be used in situations where a transformation will trigger
6770 the same rule again.
6771 An example of this (not a good one, it has side effects) is the following
6798 Note that the replace pattern cannot be a blank, but can be a comment line.
6799 Without the 'restart' option only the inner most 'pop' 'push' pair would
6800 be eliminated, i.e.:
6852 the restart option the rule will be applied again to the resulting code
6853 and then all the pop-push pairs will be eliminated to yield:
6871 A conditional function can be attached to a rule.
6872 Attaching rules are somewhat more involved, let me illustrate this with
6903 The optimizer does a look-up of a function name table defined in function
6908 in the source file SDCCpeeph.c, with the name
6913 If it finds a corresponding entry the function is called.
6914 Note there can be no parameters specified for these functions, in this
6919 is crucial, since the function
6923 expects to find the label in that particular variable (the hash table containin
6924 g the variable bindings is passed as a parameter).
6925 If you want to code more such functions, take a close look at the function
6926 labelInRange and the calling mechanism in source file SDCCpeeph.c.
6927 I know this whole thing is a little kludgey, but maybe some day we will
6928 have some better means.
6929 If you are looking at this file, you will also see the default rules that
6930 are compiled into the compiler, you can add your own rules in the default
6931 set there if you get tired of specifying the ---peep-file option.
6937 SDCC supports the following #pragma directives.
6940 SAVE - this will save all the current options.
6943 RESTORE - will restore the saved options from the last save.
6944 Note that SAVEs & RESTOREs cannot be nested.
6945 SDCC uses the same buffer to save the options each time a SAVE is called.
6946 (jwk burps: either fix that or throw a warning)
6949 NOGCSE - will stop global subexpression elimination.
6952 NOINDUCTION - will stop loop induction optimizations.
6955 NOJTBOUND - will not generate code for boundary value checking, when switch
6956 statements are turned into jump-tables.
6959 NOOVERLAY - the compiler will not overlay the parameters and local variables
6963 LESS_PEDANTIC - the compiler will not warn you anymore for obvious mistakes,
6964 you'r on your own now ;-(
6967 NOLOOPREVERSE - Will not do loop reversal optimization
6970 EXCLUDE NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation
6971 of pair of push/pop instruction in ISR function (using interrupt keyword).
6972 The directive should be placed immediately before the ISR function definition
6973 and it affects ALL ISR functions following it.
6974 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
6975 EXCLUDE\SpecialChar ~
6979 NOIV - Do not generate interrupt vector table entries for all ISR functions
6980 defined after the pragma.
6981 This is useful in cases where the interrupt vector table must be defined
6982 manually, or when there is a secondary, manually defined interrupt vector
6984 for the autovector feature of the Cypress EZ-USB FX2).
6987 CALLEE-SAVES function1[,function2[,function3...]] - The compiler by default
6988 uses a caller saves convention for register saving across function calls,
6989 however this can cause unneccessary register pushing & popping when calling
6990 small functions from larger functions.
6991 This option can be used to switch the register saving convention for the
6992 function names specified.
6993 The compiler will not save registers when calling these functions, extra
6994 code will be generated at the entry & exit for these functions to save
6995 & restore the registers used by these functions, this can SUBSTANTIALLY
6996 reduce code & improve run time performance of the generated code.
6997 In future the compiler (with interprocedural analysis) will be able to
6998 determine the appropriate scheme to use for each function call.
6999 If ---callee-saves command line option is used, the function names specified
7000 in #pragma\SpecialChar ~
7001 CALLEE-SAVES is appended to the list of functions specified inthe
7005 The pragma's are intended to be used to turn-off certain optimizations which
7006 might cause the compiler to generate extra stack / data space to store
7007 compiler generated temporary variables.
7008 This usually happens in large functions.
7009 Pragma directives should be used as shown in the following example, they
7010 are used to control options & optimizations for a given function; pragmas
7011 should be placed before and/or after a function, placing pragma's inside
7012 a function body could have unpredictable results.
7018 #pragma SAVE /* save the current settings */
7020 #pragma NOGCSE /* turnoff global subexpression elimination */
7022 #pragma NOINDUCTION /* turn off induction optimizations */
7044 #pragma RESTORE /* turn the optimizations back on */
7050 The compiler will generate a warning message when extra space is allocated.
7051 It is strongly recommended that the SAVE and RESTORE pragma's be used when
7052 changing options for a function.
7057 <pending: this is messy and incomplete>
7062 Compiler support routines (_gptrget, _mulint etc)
7065 Stdclib functions (puts, printf, strcat etc)
7068 Math functions (sin, pow, sqrt etc)
7071 Interfacing with Assembly Routines
7072 \layout Subsubsection
7074 Global Registers used for Parameter Passing
7077 The compiler always uses the global registers
7085 to pass the first parameter to a routine.
7086 The second parameter onwards is either allocated on the stack (for reentrant
7087 routines or if ---stack-auto is used) or in the internal / external ram
7088 (depending on the memory model).
7090 \layout Subsubsection
7092 Assembler Routine(non-reentrant)
7095 In the following example the function cfunc calls an assembler routine asm_func,
7096 which takes two parameters.
7102 extern int asm_func(unsigned char, unsigned char);
7106 int c_func (unsigned char i, unsigned char j)
7114 return asm_func(i,j);
7128 return c_func(10,9);
7136 The corresponding assembler function is:
7142 .globl _asm_func_PARM_2
7206 add a,_asm_func_PARM_2
7242 Note here that the return values are placed in 'dpl' - One byte return value,
7243 'dpl' LSB & 'dph' MSB for two byte values.
7244 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
7245 b' & 'acc' for four byte values.
7248 The parameter naming convention is _<function_name>_PARM_<n>, where n is
7249 the parameter number starting from 1, and counting from the left.
7250 The first parameter is passed in
7251 \begin_inset Quotes eld
7255 \begin_inset Quotes erd
7258 for One bye parameter,
7259 \begin_inset Quotes eld
7263 \begin_inset Quotes erd
7267 \begin_inset Quotes eld
7271 \begin_inset Quotes erd
7275 \begin_inset Quotes eld
7279 \begin_inset Quotes erd
7282 for four bytes, the varible name for the second parameter will be _<function_na
7287 Assemble the assembler routine with the following command:
7294 asx8051 -losg asmfunc.asm
7301 Then compile and link the assembler routine to the C source file with the
7309 sdcc cfunc.c asmfunc.rel
7310 \layout Subsubsection
7312 Assembler Routine(reentrant)
7315 In this case the second parameter onwards will be passed on the stack, the
7316 parameters are pushed from right to left i.e.
7317 after the call the left most parameter will be on the top of the stack.
7324 extern int asm_func(unsigned char, unsigned char);
7328 int c_func (unsigned char i, unsigned char j) reentrant
7336 return asm_func(i,j);
7350 return c_func(10,9);
7358 The corresponding assembler routine is:
7468 The compiling and linking procedure remains the same, however note the extra
7469 entry & exit linkage required for the assembler code, _bp is the stack
7470 frame pointer and is used to compute the offset into the stack for parameters
7471 and local variables.
7477 The external stack is located at the start of the external ram segment,
7478 and is 256 bytes in size.
7479 When ---xstack option is used to compile the program, the parameters and
7480 local variables of all reentrant functions are allocated in this area.
7481 This option is provided for programs with large stack space requirements.
7482 When used with the ---stack-auto option, all parameters and local variables
7483 are allocated on the external stack (note support libraries will need to
7484 be recompiled with the same options).
7487 The compiler outputs the higher order address byte of the external ram segment
7488 into PORT P2, therefore when using the External Stack option, this port
7489 MAY NOT be used by the application program.
7495 Deviations from the compliancy.
7498 functions are not always reentrant.
7501 structures cannot be assigned values directly, cannot be passed as function
7502 parameters or assigned to each other and cannot be a return value from
7529 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
7540 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
7562 return rets;/* is invalid in SDCC although allowed in ANSI */
7567 'long long' (64 bit integers) not supported.
7570 'double' precision floating point not supported.
7573 No support for setjmp and longjmp (for now).
7576 Old K&R style function declarations are NOT allowed.
7582 foo(i,j) /* this old style of function declarations */
7584 int i,j; /* are valid in ANSI but not valid in SDCC */
7598 functions declared as pointers must be dereferenced during the call.
7609 /* has to be called like this */
7611 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
7614 Cyclomatic Complexity
7617 Cyclomatic complexity of a function is defined as the number of independent
7618 paths the program can take during execution of the function.
7619 This is an important number since it defines the number test cases you
7620 have to generate to validate the function.
7621 The accepted industry standard for complexity number is 10, if the cyclomatic
7622 complexity reported by SDCC exceeds 10 you should think about simplification
7623 of the function logic.
7624 Note that the complexity level is not related to the number of lines of
7626 Large functions can have low complexity, and small functions can have large
7632 SDCC uses the following formula to compute the complexity:
7637 complexity = (number of edges in control flow graph) - (number of nodes
7638 in control flow graph) + 2;
7642 Having said that the industry standard is 10, you should be aware that in
7643 some cases it be may unavoidable to have a complexity level of less than
7645 For example if you have switch statement with more than 10 case labels,
7646 each case label adds one to the complexity level.
7647 The complexity level is by no means an absolute measure of the algorithmic
7648 complexity of the function, it does however provide a good starting point
7649 for which functions you might look at for further optimization.
7655 Here are a few guidelines that will help the compiler generate more efficient
7656 code, some of the tips are specific to this compiler others are generally
7657 good programming practice.
7660 Use the smallest data type to represent your data-value.
7661 If it is known in advance that the value is going to be less than 256 then
7662 use an 'unsigned char' instead of a 'short' or 'int'.
7665 Use unsigned when it is known in advance that the value is not going to
7667 This helps especially if you are doing division or multiplication.
7670 NEVER jump into a LOOP.
7673 Declare the variables to be local whenever possible, especially loop control
7674 variables (induction).
7677 Since the compiler does not always do implicit integral promotion, the programme
7678 r should do an explicit cast when integral promotion is required.
7681 Reducing the size of division, multiplication & modulus operations can reduce
7682 code size substantially.
7683 Take the following code for example.
7689 foobar(unsigned int p1, unsigned char ch)
7693 unsigned char ch1 = p1 % ch ;
7704 For the modulus operation the variable ch will be promoted to unsigned int
7705 first then the modulus operation will be performed (this will lead to a
7706 call to support routine _moduint()), and the result will be casted to a
7708 If the code is changed to
7714 foobar(unsigned int p1, unsigned char ch)
7718 unsigned char ch1 = (unsigned char)p1 % ch ;
7729 It would substantially reduce the code generated (future versions of the
7730 compiler will be smart enough to detect such optimization oppurtunities).
7733 Notes on MCS51 memory layout
7736 The 8051 family of micro controller have a minimum of 128 bytes of internal
7737 memory which is structured as follows
7741 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
7744 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
7746 - Bytes 30-7F - 60 bytes for general purpose use.
7750 Normally the SDCC compiler will only utilise the first bank of registers,
7751 but it is possible to specify that other banks of registers should be used
7752 in interrupt routines.
7753 By default, the compiler will place the stack after the last bank of used
7755 if the first 2 banks of registers are used, it will position the base of
7756 the internal stack at address 16 (0X10).
7757 This implies that as the stack grows, it will use up the remaining register
7758 banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
7759 general purpose use.
7762 By default, the compiler uses the 60 general purpose bytes to hold "near
7764 The compiler/optimiser may also declare some Local Variables in this area
7769 If any of the 128 bit variables are used, or near data is being used then
7770 care needs to be taken to ensure that the stack does not grow so much that
7771 it starts to over write either your bit variables or "near data".
7772 There is no runtime checking to prevent this from happening.
7775 The amount of stack being used is affected by the use of the "internal stack"
7776 to save registers before a subroutine call is made (---stack-auto will
7777 declare parameters and local variables on the stack) and the number of
7781 If you detect that the stack is over writing you data, then the following
7783 ---xstack will cause an external stack to be used for saving registers
7784 and (if ---stack-auto is being used) storing parameters and local variables.
7785 However this will produce more code which will be slower to execute.
7789 ---stack-loc will allow you specify the start of the stack, i.e.
7790 you could start it after any data in the general purpose area.
7791 However this may waste the memory not used by the register banks and if
7792 the size of the "near data" increases, it may creep into the bottom of
7796 ---stack-after-data, similar to the ---stack-loc, but it automatically places
7797 the stack after the end of the "near data".
7798 Again this could waste any spare register space.
7801 ---data-loc allows you to specify the start address of the near data.
7802 This could be used to move the "near data" further away from the stack
7803 giving it more room to grow.
7804 This will only work if no bit variables are being used and the stack can
7805 grow to use the bit variable space.
7813 If you find that the stack is over writing your bit variables or "near data"
7814 then the approach which best utilised the internal memory is to position
7815 the "near data" after the last bank of used registers or, if you use bit
7816 variables, after the last bit variable by using the ---data-loc, e.g.
7817 if two register banks are being used and no bit variables, ---data-loc
7818 16, and use the ---stack-after-data option.
7821 If bit variables are being used, another method would be to try and squeeze
7822 the data area in the unused register banks if it will fit, and start the
7823 stack after the last bit variable.
7826 Retargetting for other MCUs.
7829 The issues for retargetting the compiler are far too numerous to be covered
7831 What follows is a brief description of each of the seven phases of the
7832 compiler and its MCU dependency.
7835 Parsing the source and building the annotated parse tree.
7836 This phase is largely MCU independent (except for the language extensions).
7837 Syntax & semantic checks are also done in this phase, along with some initial
7838 optimizations like back patching labels and the pattern matching optimizations
7839 like bit-rotation etc.
7842 The second phase involves generating an intermediate code which can be easy
7843 manipulated during the later phases.
7844 This phase is entirely MCU independent.
7845 The intermediate code generation assumes the target machine has unlimited
7846 number of registers, and designates them with the name iTemp.
7847 The compiler can be made to dump a human readable form of the code generated
7848 by using the ---dumpraw option.
7851 This phase does the bulk of the standard optimizations and is also MCU independe
7853 This phase can be broken down into several sub-phases:
7857 Break down intermediate code (iCode) into basic blocks.
7859 Do control flow & data flow analysis on the basic blocks.
7861 Do local common subexpression elimination, then global subexpression elimination
7863 Dead code elimination
7867 If loop optimizations caused any changes then do 'global subexpression eliminati
7868 on' and 'dead code elimination' again.
7871 This phase determines the live-ranges; by live range I mean those iTemp
7872 variables defined by the compiler that still survive after all the optimization
7874 Live range analysis is essential for register allocation, since these computati
7875 on determines which of these iTemps will be assigned to registers, and for
7879 Phase five is register allocation.
7880 There are two parts to this process.
7884 The first part I call 'register packing' (for lack of a better term).
7885 In this case several MCU specific expression folding is done to reduce
7890 The second part is more MCU independent and deals with allocating registers
7891 to the remaining live ranges.
7892 A lot of MCU specific code does creep into this phase because of the limited
7893 number of index registers available in the 8051.
7896 The Code generation phase is (unhappily), entirely MCU dependent and very
7897 little (if any at all) of this code can be reused for other MCU.
7898 However the scheme for allocating a homogenized assembler operand for each
7899 iCode operand may be reused.
7902 As mentioned in the optimization section the peep-hole optimizer is rule
7903 based system, which can reprogrammed for other MCUs.
7906 SDCDB - Source Level Debugger
7909 SDCC is distributed with a source level debugger.
7910 The debugger uses a command line interface, the command repertoire of the
7911 debugger has been kept as close to gdb (the GNU debugger) as possible.
7912 The configuration and build process is part of the standard compiler installati
7913 on, which also builds and installs the debugger in the target directory
7914 specified during configuration.
7915 The debugger allows you debug BOTH at the C source and at the ASM source
7919 Compiling for Debugging
7924 debug option must be specified for all files for which debug information
7926 The complier generates a .cdb file for each of these files.
7927 The linker updates the .cdb file with the address information.
7928 This .cdb is used by the debugger.
7931 How the Debugger Works
7934 When the ---debug option is specified the compiler generates extra symbol
7935 information some of which are put into the the assembler source and some
7936 are put into the .cdb file, the linker updates the .cdb file with the address
7937 information for the symbols.
7938 The debugger reads the symbolic information generated by the compiler &
7939 the address information generated by the linker.
7940 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
7941 execution is controlled by the debugger.
7942 When a command is issued for the debugger, it translates it into appropriate
7943 commands for the simulator.
7946 Starting the Debugger
7949 The debugger can be started using the following command line.
7950 (Assume the file you are debugging has the file name foo).
7964 The debugger will look for the following files.
7967 foo.c - the source file.
7970 foo.cdb - the debugger symbol information file.
7973 foo.ihx - the intel hex format object file.
7976 Command Line Options.
7979 ---directory=<source file directory> this option can used to specify the
7980 directory search list.
7981 The debugger will look into the directory list specified for source, cdb
7983 The items in the directory list must be separated by ':', e.g.
7984 if the source files can be in the directories /home/src1 and /home/src2,
7985 the ---directory option should be ---directory=/home/src1:/home/src2.
7986 Note there can be no spaces in the option.
7990 -cd <directory> - change to the <directory>.
7993 -fullname - used by GUI front ends.
7996 -cpu <cpu-type> - this argument is passed to the simulator please see the
7997 simulator docs for details.
8000 -X <Clock frequency > this options is passed to the simulator please see
8001 the simulator docs for details.
8004 -s <serial port file> passed to simulator see the simulator docs for details.
8007 -S <serial in,out> passed to simulator see the simulator docs for details.
8013 As mention earlier the command interface for the debugger has been deliberately
8014 kept as close the GNU debugger gdb, as possible.
8015 This will help the integration with existing graphical user interfaces
8016 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
8017 \layout Subsubsection
8019 break [line | file:line | function | file:function]
8022 Set breakpoint at specified line or function:
8031 sdcdb>break foo.c:100
8035 sdcdb>break foo.c:funcfoo
8036 \layout Subsubsection
8038 clear [line | file:line | function | file:function ]
8041 Clear breakpoint at specified line or function:
8050 sdcdb>clear foo.c:100
8054 sdcdb>clear foo.c:funcfoo
8055 \layout Subsubsection
8060 Continue program being debugged, after breakpoint.
8061 \layout Subsubsection
8066 Execute till the end of the current function.
8067 \layout Subsubsection
8072 Delete breakpoint number 'n'.
8073 If used without any option clear ALL user defined break points.
8074 \layout Subsubsection
8076 info [break | stack | frame | registers ]
8079 info break - list all breakpoints
8082 info stack - show the function call stack.
8085 info frame - show information about the current execution frame.
8088 info registers - show content of all registers.
8089 \layout Subsubsection
8094 Step program until it reaches a different source line.
8095 \layout Subsubsection
8100 Step program, proceeding through subroutine calls.
8101 \layout Subsubsection
8106 Start debugged program.
8107 \layout Subsubsection
8112 Print type information of the variable.
8113 \layout Subsubsection
8118 print value of variable.
8119 \layout Subsubsection
8124 load the given file name.
8125 Note this is an alternate method of loading file for debugging.
8126 \layout Subsubsection
8131 print information about current frame.
8132 \layout Subsubsection
8137 Toggle between C source & assembly source.
8138 \layout Subsubsection
8143 Send the string following '!' to the simulator, the simulator response is
8145 Note the debugger does not interpret the command being sent to the simulator,
8146 so if a command like 'go' is sent the debugger can loose its execution
8147 context and may display incorrect values.
8148 \layout Subsubsection
8155 My name is Bobby Brown"
8158 Interfacing with XEmacs.
8161 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
8162 sdcdb.el and sdcdbsrc.el.
8163 These two files can be found in the $(prefix)/bin directory after the installat
8165 These files need to be loaded into XEmacs for the interface to work.
8166 This can be done at XEmacs startup time by inserting the following into
8167 your '.xemacs' file (which can be found in your HOME directory):
8173 (load-file sdcdbsrc.el)
8179 .xemacs is a lisp file so the () around the command is REQUIRED.
8180 The files can also be loaded dynamically while XEmacs is running, set the
8181 environment variable 'EMACSLOADPATH' to the installation bin directory
8182 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
8183 To start the interface enter the following command:
8197 You will prompted to enter the file name to be debugged.
8202 The command line options that are passed to the simulator directly are bound
8203 to default values in the file sdcdbsrc.el.
8204 The variables are listed below, these values maybe changed as required.
8207 sdcdbsrc-cpu-type '51
8210 sdcdbsrc-frequency '11059200
8216 The following is a list of key mapping for the debugger interface.
8224 ;; Current Listing ::
8241 binding\SpecialChar ~
8280 ------\SpecialChar ~
8320 sdcdb-next-from-src\SpecialChar ~
8346 sdcdb-back-from-src\SpecialChar ~
8372 sdcdb-cont-from-src\SpecialChar ~
8382 SDCDB continue command
8398 sdcdb-step-from-src\SpecialChar ~
8424 sdcdb-whatis-c-sexp\SpecialChar ~
8434 SDCDB ptypecommand for data at
8498 sdcdbsrc-delete\SpecialChar ~
8512 SDCDB Delete all breakpoints if no arg
8560 given or delete arg (C-u arg x)
8576 sdcdbsrc-frame\SpecialChar ~
8591 SDCDB Display current frame if no arg,
8640 given or display frame arg
8705 sdcdbsrc-goto-sdcdb\SpecialChar ~
8715 Goto the SDCDB output buffer
8731 sdcdb-print-c-sexp\SpecialChar ~
8742 SDCDB print command for data at
8806 sdcdbsrc-goto-sdcdb\SpecialChar ~
8816 Goto the SDCDB output buffer
8832 sdcdbsrc-mode\SpecialChar ~
8848 Toggles Sdcdbsrc mode (turns it off)
8852 ;; C-c C-f\SpecialChar ~
8860 sdcdb-finish-from-src\SpecialChar ~
8868 SDCDB finish command
8872 ;; C-x SPC\SpecialChar ~
8880 sdcdb-break\SpecialChar ~
8898 Set break for line with point
8900 ;; ESC t\SpecialChar ~
8910 sdcdbsrc-mode\SpecialChar ~
8926 Toggle Sdcdbsrc mode
8928 ;; ESC m\SpecialChar ~
8938 sdcdbsrc-srcmode\SpecialChar ~
8962 The Z80 and gbz80 port
8965 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
8966 The port is incomplete - long support is incomplete (mul, div and mod are
8967 unimplimented), and both float and bitfield support is missing.
8968 Apart from that the code generated is correct.
8971 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
8972 The stack frame is similar to that generated by the IAR Z80 compiler.
8973 IX is used as the base pointer, HL is used as a temporary register, and
8974 BC and DE are available for holding varibles.
8975 IY is currently unusued.
8976 Return values are stored in HL.
8977 One bad side effect of using IX as the base pointer is that a functions
8978 stack frame is limited to 127 bytes - this will be fixed in a later version.
8984 SDCC has grown to be a large project.
8985 The compiler alone (without the preprocessor, assembler and linker) is
8986 about 40,000 lines of code (blank stripped).
8987 The open source nature of this project is a key to its continued growth
8989 You gain the benefit and support of many active software developers and
8991 Is SDCC perfect? No, that's why we need your help.
8992 The developers take pride in fixing reported bugs.
8993 You can help by reporting the bugs and helping other SDCC users.
8994 There are lots of ways to contribute, and we encourage you to take part
8995 in making SDCC a great software package.
9001 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
9002 cc@sdcc.sourceforge.net'.
9003 Bugs will be fixed ASAP.
9004 When reporting a bug, it is very useful to include a small test program
9005 which reproduces the problem.
9006 If you can isolate the problem by looking at the generated assembly code,
9007 this can be very helpful.
9008 Compiling your program with the ---dumpall option can sometimes be useful
9009 in locating optimization problems.
9015 The anatomy of the compiler
9020 This is an excerpt from an atricle published in Circuit Cellar MagaZine
9022 It's a little outdated (the compiler is much more efficient now and user/devell
9023 oper friendly), but pretty well exposes the guts of it all.
9029 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
9030 It is fairly easy to retarget for other 8-bit MCU.
9031 Here we take a look at some of the internals of the compiler.
9038 Parsing the input source file and creating an AST (Annotated Syntax Tree).
9039 This phase also involves propagating types (annotating each node of the
9040 parse tree with type information) and semantic analysis.
9041 There are some MCU specific parsing rules.
9042 For example the storage classes, the extended storage classes are MCU specific
9043 while there may be a xdata storage class for 8051 there is no such storage
9044 class for z80 or Atmel AVR.
9045 SDCC allows MCU specific storage class extensions, i.e.
9046 xdata will be treated as a storage class specifier when parsing 8051 C
9047 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
9054 Intermediate code generation.
9055 In this phase the AST is broken down into three-operand form (iCode).
9056 These three operand forms are represented as doubly linked lists.
9057 ICode is the term given to the intermediate form generated by the compiler.
9058 ICode example section shows some examples of iCode generated for some simple
9065 Bulk of the target independent optimizations is performed in this phase.
9066 The optimizations include constant propagation, common sub-expression eliminati
9067 on, loop invariant code movement, strength reduction of loop induction variables
9068 and dead-code elimination.
9074 During intermediate code generation phase, the compiler assumes the target
9075 machine has infinite number of registers and generates a lot of temporary
9077 The live range computation determines the lifetime of each of these compiler-ge
9078 nerated temporaries.
9079 A picture speaks a thousand words.
9080 ICode example sections show the live range annotations for each of the
9082 It is important to note here, each iCode is assigned a number in the order
9083 of its execution in the function.
9084 The live ranges are computed in terms of these numbers.
9085 The from number is the number of the iCode which first defines the operand
9086 and the to number signifies the iCode which uses this operand last.
9092 The register allocation determines the type and number of registers needed
9094 In most MCUs only a few registers can be used for indirect addressing.
9095 In case of 8051 for example the registers R0 & R1 can be used to indirectly
9096 address the internal ram and DPTR to indirectly address the external ram.
9097 The compiler will try to allocate the appropriate register to pointer variables
9099 ICode example section shows the operands annotated with the registers assigned
9101 The compiler will try to keep operands in registers as much as possible;
9102 there are several schemes the compiler uses to do achieve this.
9103 When the compiler runs out of registers the compiler will check to see
9104 if there are any live operands which is not used or defined in the current
9105 basic block being processed, if there are any found then it will push that
9106 operand and use the registers in this block, the operand will then be popped
9107 at the end of the basic block.
9111 There are other MCU specific considerations in this phase.
9112 Some MCUs have an accumulator; very short-lived operands could be assigned
9113 to the accumulator instead of general-purpose register.
9119 Figure II gives a table of iCode operations supported by the compiler.
9120 The code generation involves translating these operations into corresponding
9121 assembly code for the processor.
9122 This sounds overly simple but that is the essence of code generation.
9123 Some of the iCode operations are generated on a MCU specific manner for
9124 example, the z80 port does not use registers to pass parameters so the
9125 SEND and RECV iCode operations will not be generated, and it also does
9126 not support JUMPTABLES.
9133 <Where is Figure II ?>
9139 This section shows some details of iCode.
9140 The example C code does not do anything useful; it is used as an example
9141 to illustrate the intermediate code generated by the compiler.
9154 /* This function does nothing useful.
9161 for the purpose of explaining iCode */
9164 short function (data int *x)
9172 short i=10; /* dead initialization eliminated */
9177 short sum=10; /* dead initialization eliminated */
9190 while (*x) *x++ = *p++;
9204 /* compiler detects i,j to be induction variables */
9208 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
9220 mul += i * 3; /* this multiplication remains */
9226 gint += j * 3;/* this multiplication changed to addition */
9243 In addition to the operands each iCode contains information about the filename
9244 and line it corresponds to in the source file.
9245 The first field in the listing should be interpreted as follows:
9250 Filename(linenumber: iCode Execution sequence number : ICode hash table
9251 key : loop depth of the iCode).
9256 Then follows the human readable form of the ICode operation.
9257 Each operand of this triplet form can be of three basic types a) compiler
9258 generated temporary b) user defined variable c) a constant value.
9259 Note that local variables and parameters are replaced by compiler generated
9261 Live ranges are computed only for temporaries (i.e.
9262 live ranges are not computed for global variables).
9263 Registers are allocated for temporaries only.
9264 Operands are formatted in the following manner:
9269 Operand Name [lr live-from : live-to ] { type information } [ registers
9275 As mentioned earlier the live ranges are computed in terms of the execution
9276 sequence number of the iCodes, for example
9278 the iTemp0 is live from (i.e.
9279 first defined in iCode with execution sequence number 3, and is last used
9280 in the iCode with sequence number 5).
9281 For induction variables such as iTemp21 the live range computation extends
9282 the lifetime from the start to the end of the loop.
9284 The register allocator used the live range information to allocate registers,
9285 the same registers may be used for different temporaries if their live
9286 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
9287 iTemp17 since their live ranges do not overlap.
9288 In addition the allocator also takes into consideration the type and usage
9289 of a temporary, for example itemp6 is a pointer to near space and is used
9290 as to fetch data from (i.e.
9291 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
9292 Some short lived temporaries are allocated to special registers which have
9293 meaning to the code generator e.g.
9294 iTemp13 is allocated to a pseudo register CC which tells the back end that
9295 the temporary is used only for a conditional jump the code generation makes
9296 use of this information to optimize a compare and jump ICode.
9298 There are several loop optimizations performed by the compiler.
9299 It can detect induction variables iTemp21(i) and iTemp23(j).
9300 Also note the compiler does selective strength reduction, i.e.
9301 the multiplication of an induction variable in line 18 (gint = j * 3) is
9302 changed to addition, a new temporary iTemp17 is allocated and assigned
9303 a initial value, a constant 3 is then added for each iteration of the loop.
9304 The compiler does not change the multiplication in line 17 however since
9305 the processor does support an 8 * 8 bit multiplication.
9307 Note the dead code elimination optimization eliminated the dead assignments
9308 in line 7 & 8 to I and sum respectively.
9315 Sample.c (5:1:0:0) _entry($9) :
9320 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
9325 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
9330 Sample.c(11:4:53:0) preHeaderLbl0($11) :
9335 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
9341 Sample.c(11:6:5:1) _whilecontinue_0($1) :
9346 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
9352 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9357 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
9363 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
9369 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
9375 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
9381 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
9382 * int}[r0] + 0x2 {short}
9387 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
9392 Sample.c(11:17:21:0)_whilebreak_0($3) :
9397 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9402 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9407 Sample.c(15:20:54:0)preHeaderLbl1($13) :
9412 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9417 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9422 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9427 Sample.c(15:24:26:1)_forcond_0($4) :
9432 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
9438 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9443 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
9444 + ITemp21 [lr21:38]{short}[r4]
9449 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
9455 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
9456 + iTemp15 [lr29:30]{short}[r1]
9461 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
9467 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
9473 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
9479 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
9485 Sample.c(19:38:47:1) goto _forcond_0($4)
9490 Sample.c(19:39:48:0)_forbreak_0($7) :
9495 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
9496 + ITemp11 [lr19:40]{short}[r3]
9501 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
9506 Sample.c(20:42:51:0)_return($8) :
9511 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
9517 Finally the code generated for this function:
9558 ; ----------------------------------------------
9568 ; ----------------------------------------------
9578 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
9590 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
9602 ;_whilecontinue_0($1) :
9612 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
9617 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9676 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
9695 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
9742 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
9782 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
9808 ; iTemp6 [lr5:16]{_near * int}[r0] =
9813 ; iTemp6 [lr5:16]{_near * int}[r0] +
9830 ; goto _whilecontinue_0($1)
9842 ; _whilebreak_0($3) :
9852 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9864 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9876 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9888 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9907 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9936 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
9941 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9986 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
9991 ; iTemp21 [lr21:38]{short}[r4]
10017 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
10050 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
10055 ; iTemp15 [lr29:30]{short}[r1]
10074 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
10121 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
10168 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
10180 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
10194 cjne r5,#0xff,00104$
10206 ; goto _forcond_0($4)
10218 ; _forbreak_0($7) :
10228 ; ret iTemp24 [lr40:41]{short}
10271 A few words about basic block successors, predecessors and dominators
10274 Successors are basic blocks that might execute after this basic block.
10276 Predecessors are basic blocks that might execute before reaching this basic
10279 Dominators are basic blocks that WILL execute before reaching this basic
10305 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
10308 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
10311 c) domVect of [BB4] = BB1 ...
10312 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
10320 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
10330 Thanks to all the other volunteer developers who have helped with coding,
10331 testing, web-page creation, distribution sets, etc.
10332 You know who you are :-)
10339 This document was initially written by Sandeep Dutta
10342 All product names mentioned herein may be trademarks of their respective
10348 \begin_inset LatexCommand \printindex{}