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 variables (see next section).
361 <lyxtabular version="3" rows="7" columns="3">
363 <column alignment="left" valignment="top" leftline="true" width="0pt">
364 <column alignment="left" valignment="top" leftline="true" width="0pt">
365 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
366 <row topline="true" bottomline="true">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
383 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
403 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
411 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
445 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
457 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
522 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
530 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
539 <row topline="true" bottomline="true">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
550 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
575 Cygwin is handled like a *nix, Mingw32 however belongs to the Win32 builds.
576 The SDCC team uses Mingw32 to build the official Windows binaries, because
583 a gcc compiler and last but not least
586 the binaries can be built by cross compiling on Sourceforge's compile farm.
589 The other Win32 builds using Borland, VC or whatever don't use 'configure',
590 but they (hopefully) use the default Win32 paths.
595 Binary files (preprocessor, assembler and linker)
600 <lyxtabular version="3" rows="2" columns="3">
602 <column alignment="left" valignment="top" leftline="true" width="0pt">
603 <column alignment="left" valignment="top" leftline="true" width="0pt">
604 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
605 <row topline="true" bottomline="true">
606 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
622 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
631 <row topline="true" bottomline="true">
632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
639 $PREFIX/$BIN_DIR_SUFFIX
642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
677 <lyxtabular version="3" rows="2" columns="3">
679 <column alignment="left" valignment="top" leftline="true" width="1.6in">
680 <column alignment="center" valignment="top" leftline="true" width="0pt">
681 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
682 <row topline="true" bottomline="true">
683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
699 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
708 <row topline="true" bottomline="true">
709 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
726 /usr/local/share/sdcc/include
729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
755 is auto-appended by the compiler, e.g.
756 small, large, z80, ds390 etc.)
761 <lyxtabular version="3" rows="2" columns="3">
763 <column alignment="left" valignment="top" leftline="true" width="0pt">
764 <column alignment="left" valignment="top" leftline="true" width="0pt">
765 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
766 <row topline="true" bottomline="true">
767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
792 <row topline="true" bottomline="true">
793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
800 $DATADIR/$LIB_DIR_SUFFIX
803 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
808 /usr/local/share/sdcc/lib
811 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
838 <lyxtabular version="3" rows="2" columns="3">
840 <column alignment="left" valignment="top" leftline="true" width="0pt">
841 <column alignment="left" valignment="top" leftline="true" width="0pt">
842 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
843 <row topline="true" bottomline="true">
844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
852 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
860 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
869 <row topline="true" bottomline="true">
870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
887 /usr/local/share/sdcc/doc
890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
913 Some search paths or parts of them are determined by configure variables
918 , see section above).
919 Other search paths are determined by environment variables during runtime.
922 The paths searched when running the compiler are as follows (the first catch
928 Binary files (preprocessor, assembler and linker)
932 <lyxtabular version="3" rows="4" columns="3">
934 <column alignment="left" valignment="top" leftline="true" width="0pt">
935 <column alignment="left" valignment="top" leftline="true" width="0pt">
936 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
937 <row topline="true" bottomline="true">
938 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
954 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
999 Path of argv[0] (if available)
1002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1019 <row topline="true" bottomline="true">
1020 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1057 \begin_inset Tabular
1058 <lyxtabular version="3" rows="6" columns="3">
1060 <column alignment="left" valignment="top" leftline="true" width="1.5in">
1061 <column alignment="left" valignment="top" leftline="true" width="1.5in">
1062 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
1063 <row topline="true" bottomline="true">
1064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1080 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1089 <row topline="true">
1090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1115 <row topline="true">
1116 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1141 <row topline="true">
1142 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1156 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1179 <row topline="true">
1180 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1219 <row topline="true" bottomline="true">
1220 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1236 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1241 /usr/local/share/sdcc/
1246 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1263 The option ---nostdinc disables the last two search paths.
1270 With the exception of
1271 \begin_inset Quotes sld
1275 \begin_inset Quotes srd
1282 is auto-appended by the compiler (e.g.
1283 small, large, z80, ds390 etc.).
1287 \begin_inset Tabular
1288 <lyxtabular version="3" rows="6" columns="3">
1290 <column alignment="left" valignment="top" leftline="true" width="1.7in">
1291 <column alignment="left" valignment="top" leftline="true" width="1.2in">
1292 <column alignment="left" valignment="top" leftline="true" rightline="true" width="1.2in">
1293 <row topline="true" bottomline="true">
1294 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1302 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1310 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1319 <row topline="true">
1320 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1336 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1345 <row topline="true">
1346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1358 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1385 <row topline="true">
1386 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1397 $LIB_DIR_SUFFIX/<model>
1400 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1414 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1431 <row topline="true">
1432 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1443 $LIB_DIR_SUFFIX/<model>
1446 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1494 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1554 <row topline="true" bottomline="true">
1555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1564 $LIB_DIR_SUFFIX/<model>
1567 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1572 /usr/local/share/sdcc/
1579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1595 Don't delete any of the stray spaces in the table line 5 without checking
1596 the HTML output (last line)!
1602 The option ---nostdlib disables the last two search paths.
1605 Linux and other gcc-based systems (cygwin, mingw32, osx)
1610 Download the source package
1612 either from the SDCC CVS repository or from the
1613 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1619 , it will be named something like sdcc
1632 Bring up a command line terminal, such as xterm.
1637 Unpack the file using a command like:
1640 "tar -xzf sdcc.src.tar.gz
1645 , this will create a sub-directory called sdcc with all of the sources.
1648 Change directory into the main SDCC directory, for example type:
1665 This configures the package for compilation on your system.
1681 All of the source packages will compile, this can take a while.
1697 This copies the binary executables, the include files, the libraries and
1698 the documentation to the install directories.
1702 \layout Subsubsection
1704 Windows Install Using a Binary Package
1707 Download the binary package and unpack it using your favorite unpacking
1708 tool (gunzip, WinZip, etc).
1709 This should unpack to a group of sub-directories.
1710 An example directory structure after unpacking the mingw32 package is:
1717 bin for the executables, c:
1737 lib for the include and libraries.
1740 Adjust your environment variable PATH to include the location of the bin
1741 directory or start sdcc using the full path.
1742 \layout Subsubsection
1744 Windows Install Using Cygwin and Mingw32
1747 Follow the instruction in
1749 Linux and other gcc-based systems
1752 \layout Subsubsection
1754 Windows Install Using Microsoft Visual C++ 6.0/NET
1759 Download the source package
1761 either from the SDCC CVS repository or from the
1762 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1768 , it will be named something like sdcc
1775 SDCC is distributed with all the projects, workspaces, and files you need
1776 to build it using Visual C++ 6.0/NET.
1777 The workspace name is 'sdcc.dsw'.
1778 Please note that as it is now, all the executables are created in a folder
1782 Once built you need to copy the executables from sdcc
1786 bin before runnng SDCC.
1791 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
1792 of bison.exe, flex.exe, and gawk.exe.
1793 One good place to get them is
1794 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
1802 Download the file UnxUtils.zip.
1803 Now you have to install the utilities and setup Visual C++ so it can locate
1804 the required programs.
1805 Here there are two alternatives (choose one!):
1812 a) Extract UnxUtils.zip to your C:
1814 hard disk PRESERVING the original paths, otherwise bison won't work.
1815 (If you are using WinZip make certain that 'Use folder names' is selected)
1819 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
1820 in 'Show directories for:' select 'Executable files', and in the directories
1821 window add a new path: 'C:
1831 (As a side effect, you get a bunch of Unix utilities that could be useful,
1832 such as diff and patch.)
1839 This one avoids extracting a bunch of files you may not use, but requires
1844 a) Create a directory were to put the tools needed, or use a directory already
1852 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
1853 to such directory WITHOUT preserving the original paths.
1854 (If you are using WinZip make certain that 'Use folder names' is not selected)
1858 c) Rename bison.exe to '_bison.exe'.
1862 d) Create a batch file 'bison.bat' in 'C:
1866 ' and add these lines:
1886 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
1890 Steps 'c' and 'd' are needed because bison requires by default that the
1891 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
1892 '/usr/local/share/' I think.
1893 So it is necessary to tell bison where those files are located if they
1894 are not in such directory.
1895 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
1899 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
1900 in 'Show directories for:' select 'Executable files', and in the directories
1901 window add a new path: 'c:
1904 Note that you can use any other path instead of 'c:
1906 util', even the path where the Visual C++ tools are, probably: 'C:
1910 Microsoft Visual Studio
1915 So you don't have to execute step 'e' :)
1919 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
1920 the executables from sdcc
1924 bin, and you can compile using sdcc.
1925 \layout Subsubsection
1927 Windows Install Using Borland
1930 From the sdcc directory, run the command "make -f Makefile.bcc".
1931 This should regenerate all the .exe files in the bin directory except for
1932 sdcdb.exe (which currently doesn't build under Borland C++).
1935 If you modify any source files and need to rebuild, be aware that the dependanci
1936 es may not be correctly calculated.
1937 The safest option is to delete all .obj files and run the build again.
1938 From a Cygwin BASH prompt, this can easily be done with the commmand:
1948 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
1950 ) -print -exec rm {}
1959 or on Windows NT/2000/XP from the command prompt with the commmand:
1966 del /s *.obj *.lib *.rul
1969 from the sdcc directory.
1972 Testing out the SDCC Compiler
1975 The first thing you should do after installing your SDCC compiler is to
1983 at the prompt, and the program should run and tell you the version.
1984 If it doesn't run, or gives a message about not finding sdcc program, then
1985 you need to check over your installation.
1986 Make sure that the sdcc bin directory is in your executable search path
1987 defined by the PATH environment setting (see the Trouble-shooting section
1989 Make sure that the sdcc program is in the bin folder, if not perhaps something
1990 did not install correctly.
1998 is commonly installed as described in section
1999 \begin_inset Quotes sld
2002 Install and search paths
2003 \begin_inset Quotes srd
2012 Make sure the compiler works on a very simple example.
2013 Type in the following test.c program using your favorite
2048 Compile this using the following command:
2057 If all goes well, the compiler will generate a test.asm and test.rel file.
2058 Congratulations, you've just compiled your first program with SDCC.
2059 We used the -c option to tell SDCC not to link the generated code, just
2060 to keep things simple for this step.
2068 The next step is to try it with the linker.
2078 If all goes well the compiler will link with the libraries and produce
2079 a test.ihx output file.
2084 (no test.ihx, and the linker generates warnings), then the problem is most
2085 likely that sdcc cannot find the
2089 usr/local/share/sdcc/lib directory
2093 (see the Install trouble-shooting section for suggestions).
2101 The final test is to ensure sdcc can use the
2105 header files and libraries.
2106 Edit test.c and change it to the following:
2126 strcpy(str1, "testing");
2135 Compile this by typing
2142 This should generate a test.ihx output file, and it should give no warnings
2143 such as not finding the string.h file.
2144 If it cannot find the string.h file, then the problem is that sdcc cannot
2145 find the /usr/local/share/sdcc/include directory
2149 (see the Install trouble-shooting section for suggestions).
2152 Install Trouble-shooting
2153 \layout Subsubsection
2155 SDCC does not build correctly.
2158 A thing to try is starting from scratch by unpacking the .tgz source package
2159 again in an empty directory.
2167 ./configure 2>&1 | tee configure.log
2181 make 2>&1 | tee make.log
2188 If anything goes wrong, you can review the log files to locate the problem.
2189 Or a relevant part of this can be attached to an email that could be helpful
2190 when requesting help from the mailing list.
2191 \layout Subsubsection
2194 \begin_inset Quotes sld
2198 \begin_inset Quotes srd
2205 \begin_inset Quotes sld
2209 \begin_inset Quotes srd
2212 command is a script that analyzes your system and performs some configuration
2213 to ensure the source package compiles on your system.
2214 It will take a few minutes to run, and will compile a few tests to determine
2215 what compiler features are installed.
2216 \layout Subsubsection
2219 \begin_inset Quotes sld
2223 \begin_inset Quotes srd
2229 This runs the GNU make tool, which automatically compiles all the source
2230 packages into the final installed binary executables.
2231 \layout Subsubsection
2234 \begin_inset Quotes sld
2238 \begin_inset Quotes erd
2244 This will install the compiler, other executables libraries and include
2245 files in to the appropriate directories.
2247 \begin_inset Quotes sld
2250 Install and Search PATHS
2251 \begin_inset Quotes srd
2256 On most systems you will need super-user privilages to do this.
2262 SDCC is not just a compiler, but a collection of tools by various developers.
2263 These include linkers, assemblers, simulators and other components.
2264 Here is a summary of some of the components.
2265 Note that the included simulator and assembler have separate documentation
2266 which you can find in the source package in their respective directories.
2267 As SDCC grows to include support for other processors, other packages from
2268 various developers are included and may have their own sets of documentation.
2272 You might want to look at the files which are installed in <installdir>.
2273 At the time of this writing, we find the following programs for gcc-builds:
2277 In <installdir>/bin:
2280 sdcc - The compiler.
2283 sdcpp - The C preprocessor.
2286 asx8051 - The assembler for 8051 type processors.
2293 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
2296 aslink -The linker for 8051 type processors.
2303 link-gbz80 - The Z80 and GameBoy Z80 linkers.
2306 s51 - The ucSim 8051 simulator.
2309 sdcdb - The source debugger.
2312 packihx - A tool to pack (compress) Intel hex files.
2315 In <installdir>/share/sdcc/include
2321 In <installdir>/share/sdcc/lib
2324 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
2328 In <installdir>/share/sdcc/doc
2334 As development for other processors proceeds, this list will expand to include
2335 executables to support processors like AVR, PIC, etc.
2336 \layout Subsubsection
2341 This is the actual compiler, it in turn uses the c-preprocessor and invokes
2342 the assembler and linkage editor.
2343 \layout Subsubsection
2345 sdcpp - The C-Preprocessor
2348 The preprocessor is a modified version of the GNU preprocessor.
2349 The C preprocessor is used to pull in #include sources, process #ifdef
2350 statements, #defines and so on.
2351 \layout Subsubsection
2353 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
2357 This is retargettable assembler & linkage editor, it was developed by Alan
2359 John Hartman created the version for 8051, and I (Sandeep) have made some
2360 enhancements and bug fixes for it to work properly with the SDCC.
2361 \layout Subsubsection
2366 S51 is a freeware, opensource simulator developed by Daniel Drotos (
2367 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
2372 The simulator is built as part of the build process.
2373 For more information visit Daniel's website at:
2374 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
2379 It currently support the core mcs51, the Dallas DS80C390 and the Philips
2381 \layout Subsubsection
2383 sdcdb - Source Level Debugger
2389 <todo: is this thing still alive?>
2396 Sdcdb is the companion source level debugger.
2397 The current version of the debugger uses Daniel's Simulator S51, but can
2398 be easily changed to use other simulators.
2405 \layout Subsubsection
2407 Single Source File Projects
2410 For single source file 8051 projects the process is very simple.
2411 Compile your programs with the following command
2414 "sdcc sourcefile.c".
2418 This will compile, assemble and link your source file.
2419 Output files are as follows
2423 sourcefile.asm - Assembler source file created by the compiler
2425 sourcefile.lst - Assembler listing file created by the Assembler
2427 sourcefile.rst - Assembler listing file updated with linkedit information,
2428 created by linkage editor
2430 sourcefile.sym - symbol listing for the sourcefile, created by the assembler
2432 sourcefile.rel - Object file created by the assembler, input to Linkage editor
2434 sourcefile.map - The memory map for the load module, created by the Linker
2436 sourcefile.ihx - The load module in Intel hex format (you can select the
2437 Motorola S19 format with ---out-fmt-s19)
2439 sourcefile.cdb - An optional file (with ---debug) containing debug information
2441 sourcefile.dump* - Dump file to debug the compiler it self (with ---dumpall)
2443 \begin_inset Quotes sld
2446 Anatomy of the compiler
2447 \begin_inset Quotes srd
2451 \layout Subsubsection
2453 Projects with Multiple Source Files
2456 SDCC can compile only ONE file at a time.
2457 Let us for example assume that you have a project containing the following
2462 foo1.c (contains some functions)
2464 foo2.c (contains some more functions)
2466 foomain.c (contains more functions and the function main)
2474 The first two files will need to be compiled separately with the commands:
2506 Then compile the source file containing the
2510 function and link the files together with the following command:
2518 foomain.c\SpecialChar ~
2519 foo1.rel\SpecialChar ~
2531 can be separately compiled as well:
2542 sdcc foomain.rel foo1.rel foo2.rel
2549 The file containing the
2564 file specified in the command line, since the linkage editor processes
2565 file in the order they are presented to it.
2566 \layout Subsubsection
2568 Projects with Additional Libraries
2571 Some reusable routines may be compiled into a library, see the documentation
2572 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
2578 Libraries created in this manner can be included in the command line.
2579 Make sure you include the -L <library-path> option to tell the linker where
2580 to look for these files if they are not in the current directory.
2581 Here is an example, assuming you have the source file
2593 (if that is not the same as your current project):
2600 sdcc foomain.c foolib.lib -L mylib
2611 must be an absolute path name.
2615 The most efficient way to use libraries is to keep seperate modules in seperate
2617 The lib file now should name all the modules.rel files.
2618 For an example see the standard library file
2622 in the directory <installdir>/share/lib/small.
2625 Command Line Options
2626 \layout Subsubsection
2628 Processor Selection Options
2630 \labelwidthstring 00.00.0000
2636 Generate code for the MCS51 (8051) family of processors.
2637 This is the default processor target.
2639 \labelwidthstring 00.00.0000
2645 Generate code for the DS80C390 processor.
2647 \labelwidthstring 00.00.0000
2653 Generate code for the Z80 family of processors.
2655 \labelwidthstring 00.00.0000
2661 Generate code for the GameBoy Z80 processor.
2663 \labelwidthstring 00.00.0000
2669 Generate code for the Atmel AVR processor (In development, not complete).
2671 \labelwidthstring 00.00.0000
2677 Generate code for the PIC 14-bit processors (In development, not complete).
2679 \labelwidthstring 00.00.0000
2685 Generate code for the Toshiba TLCS-900H processor (In development, not
2688 \labelwidthstring 00.00.0000
2694 Generate code for the Philips XA51 processor (In development, not complete).
2695 \layout Subsubsection
2697 Preprocessor Options
2699 \labelwidthstring 00.00.0000
2705 The additional location where the pre processor will look for <..h> or
2706 \begin_inset Quotes eld
2710 \begin_inset Quotes erd
2715 \labelwidthstring 00.00.0000
2721 Command line definition of macros.
2722 Passed to the pre processor.
2724 \labelwidthstring 00.00.0000
2730 Tell the preprocessor to output a rule suitable for make describing the
2731 dependencies of each object file.
2732 For each source file, the preprocessor outputs one make-rule whose target
2733 is the object file name for that source file and whose dependencies are
2734 all the files `#include'd in it.
2735 This rule may be a single line or may be continued with `
2737 '-newline if it is long.
2738 The list of rules is printed on standard output instead of the preprocessed
2742 \labelwidthstring 00.00.0000
2748 Tell the preprocessor not to discard comments.
2749 Used with the `-E' option.
2751 \labelwidthstring 00.00.0000
2762 Like `-M' but the output mentions only the user header files included with
2764 \begin_inset Quotes eld
2768 System header files included with `#include <file>' are omitted.
2770 \labelwidthstring 00.00.0000
2776 Assert the answer answer for question, in case it is tested with a preprocessor
2777 conditional such as `#if #question(answer)'.
2778 `-A-' disables the standard assertions that normally describe the target
2781 \labelwidthstring 00.00.0000
2787 (answer) Assert the answer answer for question, in case it is tested with
2788 a preprocessor conditional such as `#if #question(answer)'.
2789 `-A-' disables the standard assertions that normally describe the target
2792 \labelwidthstring 00.00.0000
2798 Undefine macro macro.
2799 `-U' options are evaluated after all `-D' options, but before any `-include'
2800 and `-imacros' options.
2802 \labelwidthstring 00.00.0000
2808 Tell the preprocessor to output only a list of the macro definitions that
2809 are in effect at the end of preprocessing.
2810 Used with the `-E' option.
2812 \labelwidthstring 00.00.0000
2818 Tell the preprocessor to pass all macro definitions into the output, in
2819 their proper sequence in the rest of the output.
2821 \labelwidthstring 00.00.0000
2832 Like `-dD' except that the macro arguments and contents are omitted.
2833 Only `#define name' is included in the output.
2834 \layout Subsubsection
2838 \labelwidthstring 00.00.0000
2848 <absolute path to additional libraries> This option is passed to the linkage
2849 editor's additional libraries search path.
2850 The path name must be absolute.
2851 Additional library files may be specified in the command line.
2852 See section Compiling programs for more details.
2854 \labelwidthstring 00.00.0000
2860 <Value> The start location of the external ram, default value is 0.
2861 The value entered can be in Hexadecimal or Decimal format, e.g.: ---xram-loc
2862 0x8000 or ---xram-loc 32768.
2864 \labelwidthstring 00.00.0000
2870 <Value> The start location of the code segment, default value 0.
2871 Note when this option is used the interrupt vector table is also relocated
2872 to the given address.
2873 The value entered can be in Hexadecimal or Decimal format, e.g.: ---code-loc
2874 0x8000 or ---code-loc 32768.
2876 \labelwidthstring 00.00.0000
2882 <Value> By default the stack is placed after the data segment.
2883 Using this option the stack can be placed anywhere in the internal memory
2885 The value entered can be in Hexadecimal or Decimal format, e.g.
2886 ---stack-loc 0x20 or ---stack-loc 32.
2887 Since the sp register is incremented before a push or call, the initial
2888 sp will be set to one byte prior the provided value.
2889 The provided value should not overlap any other memory areas such as used
2890 register banks or the data segment and with enough space for the current
2893 \labelwidthstring 00.00.0000
2899 <Value> The start location of the internal ram data segment.
2900 The value entered can be in Hexadecimal or Decimal format, eg.
2901 ---data-loc 0x20 or ---data-loc 32.
2902 (By default, the start location of the internal ram data segment is set
2903 as low as possible in memory, taking into account the used register banks
2904 and the bit segment at address 0x20.
2905 For example if register banks 0 and 1 are used without bit variables, the
2906 data segment will be set, if ---data-loc is not used, to location 0x10.)
2908 \labelwidthstring 00.00.0000
2914 <Value> The start location of the indirectly addressable internal ram, default
2916 The value entered can be in Hexadecimal or Decimal format, eg.
2917 ---idata-loc 0x88 or ---idata-loc 136.
2919 \labelwidthstring 00.00.0000
2928 The linker output (final object code) is in Intel Hex format.
2929 (This is the default option).
2931 \labelwidthstring 00.00.0000
2940 The linker output (final object code) is in Motorola S19 format.
2941 \layout Subsubsection
2945 \labelwidthstring 00.00.0000
2951 Generate code for Large model programs see section Memory Models for more
2953 If this option is used all source files in the project should be compiled
2955 In addition the standard library routines are compiled with small model,
2956 they will need to be recompiled.
2958 \labelwidthstring 00.00.0000
2969 Generate code for Small Model programs see section Memory Models for more
2971 This is the default model.
2972 \layout Subsubsection
2976 \labelwidthstring 00.00.0000
2987 Generate 24-bit flat mode code.
2988 This is the one and only that the ds390 code generator supports right now
2989 and is default when using
2994 See section Memory Models for more details.
2996 \labelwidthstring 00.00.0000
3002 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
3003 This is the one and only that the ds390 code generator supports right now
3004 and is default when using
3009 In this mode, the stack is located in the lower 1K of the internal RAM,
3010 which is mapped to 0x400000.
3011 Note that the support is incomplete, since it still uses a single byte
3012 as the stack pointer.
3013 This means that only the lower 256 bytes of the potential 1K stack space
3014 will actually be used.
3015 However, this does allow you to reclaim the precious 256 bytes of low RAM
3016 for use for the DATA and IDATA segments.
3017 The compiler will not generate any code to put the processor into 10 bit
3019 It is important to ensure that the processor is in this mode before calling
3020 any re-entrant functions compiled with this option.
3021 In principle, this should work with the
3025 option, but that has not been tested.
3026 It is incompatible with the
3031 It also only makes sense if the processor is in 24 bit contiguous addressing
3034 ---model-flat24 option
3037 \layout Subsubsection
3039 Optimization Options
3041 \labelwidthstring 00.00.0000
3047 Will not do global subexpression elimination, this option may be used when
3048 the compiler creates undesirably large stack/data spaces to store compiler
3050 A warning message will be generated when this happens and the compiler
3051 will indicate the number of extra bytes it allocated.
3052 It recommended that this option NOT be used, #pragma\SpecialChar ~
3054 to turn off global subexpression elimination for a given function only.
3056 \labelwidthstring 00.00.0000
3062 Will not do loop invariant optimizations, this may be turned off for reasons
3063 explained for the previous option.
3064 For more details of loop optimizations performed see section Loop Invariants.It
3065 recommended that this option NOT be used, #pragma\SpecialChar ~
3066 NOINVARIANT can be used
3067 to turn off invariant optimizations for a given function only.
3069 \labelwidthstring 00.00.0000
3075 Will not do loop induction optimizations, see section strength reduction
3076 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
3078 ION can be used to turn off induction optimizations for a given function
3081 \labelwidthstring 00.00.0000
3092 Will not generate boundary condition check when switch statements are implement
3093 ed using jump-tables.
3094 See section Switch Statements for more details.
3095 It is recommended that this option is NOT used, #pragma\SpecialChar ~
3097 used to turn off boundary checking for jump tables for a given function
3100 \labelwidthstring 00.00.0000
3109 Will not do loop reversal optimization.
3111 \labelwidthstring 00.00.0000
3117 Will not optimize labels (makes the dumpfiles more readable).
3119 \labelwidthstring 00.00.0000
3125 Will not memcpy initialized data in far space from code space.
3126 This saves a few bytes in code space if you don't have initialized data.
3127 \layout Subsubsection
3131 \labelwidthstring 00.00.0000
3138 will compile and assemble the source, but will not call the linkage editor.
3140 \labelwidthstring 00.00.0000
3146 reads the preprocessed source from standard input and compiles it.
3147 The file name for the assembler output must be specified using the -o option.
3149 \labelwidthstring 00.00.0000
3155 Run only the C preprocessor.
3156 Preprocess all the C source files specified and output the results to standard
3159 \labelwidthstring 00.00.0000
3166 The output path resp.
3167 file where everything will be placed.
3168 If the parameter is a path, it must have a trailing slash (or backslash
3169 for the Windows binaries) to be recognized as a path.
3172 \labelwidthstring 00.00.0000
3183 All functions in the source file will be compiled as
3188 the parameters and local variables will be allocated on the stack.
3189 see section Parameters and Local Variables for more details.
3190 If this option is used all source files in the project should be compiled
3194 \labelwidthstring 00.00.0000
3200 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
3201 variables and passing parameters.
3202 See section on external stack for more details.
3204 \labelwidthstring 00.00.0000
3208 ---callee-saves function1[,function2][,function3]....
3211 The compiler by default uses a caller saves convention for register saving
3212 across function calls, however this can cause unneccessary register pushing
3213 & popping when calling small functions from larger functions.
3214 This option can be used to switch the register saving convention for the
3215 function names specified.
3216 The compiler will not save registers when calling these functions, no extra
3217 code will be generated at the entry & exit for these functions to save
3218 & restore the registers used by these functions, this can SUBSTANTIALLY
3219 reduce code & improve run time performance of the generated code.
3220 In the future the compiler (with interprocedural analysis) will be able
3221 to determine the appropriate scheme to use for each function call.
3222 DO NOT use this option for built-in functions such as _muluint..., if this
3223 option is used for a library function the appropriate library function
3224 needs to be recompiled with the same option.
3225 If the project consists of multiple source files then all the source file
3226 should be compiled with the same ---callee-saves option string.
3227 Also see #pragma\SpecialChar ~
3230 \labelwidthstring 00.00.0000
3239 When this option is used the compiler will generate debug information, that
3240 can be used with the SDCDB.
3241 The debug information is collected in a file with .cdb extension.
3242 For more information see documentation for SDCDB.
3244 \labelwidthstring 00.00.0000
3250 <filename> This option can be used to use additional rules to be used by
3251 the peep hole optimizer.
3252 See section Peep Hole optimizations for details on how to write these rules.
3254 \labelwidthstring 00.00.0000
3265 Stop after the stage of compilation proper; do not assemble.
3266 The output is an assembler code file for the input file specified.
3268 \labelwidthstring 00.00.0000
3272 -Wa_asmOption[,asmOption]
3275 Pass the asmOption to the assembler.
3277 \labelwidthstring 00.00.0000
3281 -Wl_linkOption[,linkOption]
3284 Pass the linkOption to the linker.
3286 \labelwidthstring 00.00.0000
3295 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
3296 Note by default these libraries are compiled as non-reentrant.
3297 See section Installation for more details.
3299 \labelwidthstring 00.00.0000
3308 This option will cause the compiler to generate an information message for
3309 each function in the source file.
3310 The message contains some
3314 information about the function.
3315 The number of edges and nodes the compiler detected in the control flow
3316 graph of the function, and most importantly the
3318 cyclomatic complexity
3320 see section on Cyclomatic Complexity for more details.
3322 \labelwidthstring 00.00.0000
3331 Floating point library is compiled as reentrant.See section Installation
3334 \labelwidthstring 00.00.0000
3340 The compiler will not overlay parameters and local variables of any function,
3341 see section Parameters and local variables for more details.
3343 \labelwidthstring 00.00.0000
3349 This option can be used when the code generated is called by a monitor
3351 The compiler will generate a 'ret' upon return from the 'main' function.
3352 The default option is to lock up i.e.
3355 \labelwidthstring 00.00.0000
3361 Disable peep-hole optimization.
3363 \labelwidthstring 00.00.0000
3369 Pass the inline assembler code through the peep hole optimizer.
3370 This can cause unexpected changes to inline assembler code, please go through
3371 the peephole optimizer rules defined in the source file tree '<target>/peeph.def
3372 ' before using this option.
3374 \labelwidthstring 00.00.0000
3380 <Value> Causes the linker to check if the internal ram usage is within limits
3383 \labelwidthstring 00.00.0000
3389 <Value> Causes the linker to check if the external ram usage is within limits
3392 \labelwidthstring 00.00.0000
3398 <Value> Causes the linker to check if the code usage is within limits of
3401 \labelwidthstring 00.00.0000
3407 This will prevent the compiler from passing on the default include path
3408 to the preprocessor.
3410 \labelwidthstring 00.00.0000
3416 This will prevent the compiler from passing on the default library path
3419 \labelwidthstring 00.00.0000
3425 Shows the various actions the compiler is performing.
3427 \labelwidthstring 00.00.0000
3433 Shows the actual commands the compiler is executing.
3435 \labelwidthstring 00.00.0000
3441 Hides your ugly and inefficient c-code from the asm file, so you can always
3442 blame the compiler :).
3444 \labelwidthstring 00.00.0000
3450 Include i-codes in the asm file.
3451 Sounds like noise but is most helpfull for debugging the compiler itself.
3453 \labelwidthstring 00.00.0000
3459 Disable some of the more pedantic warnings (jwk burps: please be more specific
3461 \layout Subsubsection
3463 Intermediate Dump Options
3466 The following options are provided for the purpose of retargetting and debugging
3468 These provided a means to dump the intermediate code (iCode) generated
3469 by the compiler in human readable form at various stages of the compilation
3473 \labelwidthstring 00.00.0000
3479 This option will cause the compiler to dump the intermediate code into
3482 <source filename>.dumpraw
3484 just after the intermediate code has been generated for a function, i.e.
3485 before any optimizations are done.
3486 The basic blocks at this stage ordered in the depth first number, so they
3487 may not be in sequence of execution.
3489 \labelwidthstring 00.00.0000
3495 Will create a dump of iCode's, after global subexpression elimination,
3498 <source filename>.dumpgcse.
3500 \labelwidthstring 00.00.0000
3506 Will create a dump of iCode's, after deadcode elimination, into a file
3509 <source filename>.dumpdeadcode.
3511 \labelwidthstring 00.00.0000
3520 Will create a dump of iCode's, after loop optimizations, into a file named
3523 <source filename>.dumploop.
3525 \labelwidthstring 00.00.0000
3534 Will create a dump of iCode's, after live range analysis, into a file named
3537 <source filename>.dumprange.
3539 \labelwidthstring 00.00.0000
3545 Will dump the life ranges for all symbols.
3547 \labelwidthstring 00.00.0000
3556 Will create a dump of iCode's, after register assignment, into a file named
3559 <source filename>.dumprassgn.
3561 \labelwidthstring 00.00.0000
3567 Will create a dump of the live ranges of iTemp's
3569 \labelwidthstring 00.00.0000
3580 Will cause all the above mentioned dumps to be created.
3583 Environment variables
3586 SDCC recognizes the following environment variables:
3588 \labelwidthstring 00.00.0000
3594 SDCC installs a signal handler to be able to delete temporary files after
3595 an user break (^C) or an exception.
3596 If this environment variable is set, SDCC won't install the signal handler
3597 in order to be able to debug SDCC.
3599 \labelwidthstring 00.00.0000
3607 Path, where temporary files will be created.
3608 The order of the variables is the search order.
3609 In a standard *nix environment these variables are not set, and there's
3610 no need to set them.
3611 On Windows it's recommended to set one of them.
3613 \labelwidthstring 00.00.0000
3617 (coming\SpecialChar ~
3622 \begin_inset Quotes sld
3625 2.1 Install and search paths
3626 \begin_inset Quotes srd
3631 \labelwidthstring 00.00.0000
3635 (coming\SpecialChar ~
3640 \begin_inset Quotes sld
3643 2.1 Install and search paths
3644 \begin_inset Quotes srd
3649 \labelwidthstring 00.00.0000
3653 (coming\SpecialChar ~
3658 \begin_inset Quotes sld
3661 2.1 Install and search paths
3662 \begin_inset Quotes srd
3667 \labelwidthstring 00.00.0000
3671 SDCCDIR\SpecialChar ~
3673 replaced\SpecialChar ~
3678 \begin_inset Quotes sld
3681 2.1 Install and search paths
3682 \begin_inset Quotes srd
3688 There are some more environment variables recognized by SDCC, but these
3689 are solely used for debugging purposes.
3690 They can change or disappear very quickly, and will never be documentated.
3693 MCS51/DS390 Storage Class Language Extensions
3696 In addition to the ANSI storage classes SDCC allows the following MCS51
3697 specific storage classes.
3698 \layout Subsubsection
3703 Variables declared with this storage class will be placed in the extern
3709 storage class for Large Memory model, e.g.:
3715 xdata unsigned char xduc;
3716 \layout Subsubsection
3725 storage class for Small Memory model.
3726 Variables declared with this storage class will be allocated in the internal
3734 \layout Subsubsection
3739 Variables declared with this storage class will be allocated into the indirectly
3740 addressable portion of the internal ram of a 8051, e.g.:
3747 \layout Subsubsection
3752 This is a data-type and a storage class specifier.
3753 When a variable is declared as a bit, it is allocated into the bit addressable
3754 memory of 8051, e.g.:
3761 \layout Subsubsection
3766 Like the bit keyword,
3770 signifies both a data-type and storage class, they are used to describe
3771 the special function registers and special bit variables of a 8051, eg:
3777 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
3779 sbit at 0xd7 CY; /* CY (Carry Flag) */
3785 SDCC allows (via language extensions) pointers to explicitly point to any
3786 of the memory spaces of the 8051.
3787 In addition to the explicit pointers, the compiler uses (by default) generic
3788 pointers which can be used to point to any of the memory spaces.
3792 Pointer declaration examples:
3801 /* pointer physically in xternal ram pointing to object in internal ram
3804 data unsigned char * xdata p;
3808 /* pointer physically in code rom pointing to data in xdata space */
3810 xdata unsigned char * code p;
3814 /* pointer physically in code space pointing to data in code space */
3816 code unsigned char * code p;
3820 /* the folowing is a generic pointer physically located in xdata space */
3831 Well you get the idea.
3836 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
3849 The highest order byte of the
3853 pointers contains the data space information.
3854 Assembler support routines are called whenever data is stored or retrieved
3860 These are useful for developing reusable library routines.
3861 Explicitly specifying the pointer type will generate the most efficient
3865 Parameters & Local Variables
3868 Automatic (local) variables and parameters to functions can either be placed
3869 on the stack or in data-space.
3870 The default action of the compiler is to place these variables in the internal
3871 RAM (for small model) or external RAM (for large model).
3872 This in fact makes them
3876 so by default functions are non-reentrant.
3880 They can be placed on the stack either by using the
3884 option or by using the
3888 keyword in the function declaration, e.g.:
3897 unsigned char foo(char i) reentrant
3910 Since stack space on 8051 is limited, the
3918 option should be used sparingly.
3919 Note that the reentrant keyword just means that the parameters & local
3920 variables will be allocated to the stack, it
3924 mean that the function is register bank independent.
3928 Local variables can be assigned storage classes and absolute addresses,
3935 unsigned char foo() {
3941 xdata unsigned char i;
3953 data at 0x31 unsiged char j;
3968 In the above example the variable
3972 will be allocated in the external ram,
3976 in bit addressable space and
3985 or when a function is declared as
3989 this should only be done for static variables.
3992 Parameters however are not allowed any storage class, (storage classes for
3993 parameters will be ignored), their allocation is governed by the memory
3994 model in use, and the reentrancy options.
4000 For non-reentrant functions SDCC will try to reduce internal ram space usage
4001 by overlaying parameters and local variables of a function (if possible).
4002 Parameters and local variables of a function will be allocated to an overlayabl
4003 e segment if the function has
4005 no other function calls and the function is non-reentrant and the memory
4009 If an explicit storage class is specified for a local variable, it will
4013 Note that the compiler (not the linkage editor) makes the decision for overlayin
4015 Functions that are called from an interrupt service routine should be preceded
4016 by a #pragma\SpecialChar ~
4017 NOOVERLAY if they are not reentrant.
4020 Also note that the compiler does not do any processing of inline assembler
4021 code, so the compiler might incorrectly assign local variables and parameters
4022 of a function into the overlay segment if the inline assembler code calls
4023 other c-functions that might use the overlay.
4024 In that case the #pragma\SpecialChar ~
4025 NOOVERLAY should be used.
4028 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
4029 tion or division will NOT be overlayed since these are implemented using
4030 external functions, e.g.:
4040 void set_error(unsigned char errcd)
4056 void some_isr () interrupt 2 using 1
4085 In the above example the parameter
4093 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
4095 not present, this could cause unpredictable runtime behavior when called
4097 The #pragma\SpecialChar ~
4098 NOOVERLAY ensures that the parameters and local variables for
4099 the function are NOT overlayed.
4102 Interrupt Service Routines
4105 SDCC allows interrupt service routines to be coded in C, with some extended
4112 void timer_isr (void) interrupt 2 using 1
4125 The number following the
4129 keyword is the interrupt number this routine will service.
4130 The compiler will insert a call to this routine in the interrupt vector
4131 table for the interrupt number specified.
4136 keyword is used to tell the compiler to use the specified register bank
4137 (8051 specific) when generating code for this function.
4138 Note that when some function is called from an interrupt service routine
4139 it should be preceded by a #pragma\SpecialChar ~
4140 NOOVERLAY if it is not reentrant.
4141 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
4142 ation & modulus operations are implemented using external support routines
4143 developed in ANSI-C, if an interrupt service routine needs to do any of
4144 these operations then the support routines (as mentioned in a following
4145 section) will have to be recompiled using the
4149 option and the source file will need to be compiled using the
4156 If you have multiple source files in your project, interrupt service routines
4157 can be present in any of them, but a prototype of the isr MUST be present
4158 or included in the file that contains the function
4165 Interrupt Numbers and the corresponding address & descriptions for the Standard
4166 8051 are listed below.
4167 SDCC will automatically adjust the interrupt vector table to the maximum
4168 interrupt number specified.
4174 \begin_inset Tabular
4175 <lyxtabular version="3" rows="6" columns="3">
4177 <column alignment="center" valignment="top" leftline="true" width="0pt">
4178 <column alignment="center" valignment="top" leftline="true" width="0pt">
4179 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
4180 <row topline="true" bottomline="true">
4181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4197 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4206 <row topline="true">
4207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4223 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4232 <row topline="true">
4233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4258 <row topline="true">
4259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4275 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4284 <row topline="true">
4285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4310 <row topline="true" bottomline="true">
4311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4344 If the interrupt service routine is defined without
4348 a register bank or with register bank 0 (using 0), the compiler will save
4349 the registers used by itself on the stack upon entry and restore them at
4350 exit, however if such an interrupt service routine calls another function
4351 then the entire register bank will be saved on the stack.
4352 This scheme may be advantageous for small interrupt service routines which
4353 have low register usage.
4356 If the interrupt service routine is defined to be using a specific register
4361 are save and restored, if such an interrupt service routine calls another
4362 function (using another register bank) then the entire register bank of
4363 the called function will be saved on the stack.
4364 This scheme is recommended for larger interrupt service routines.
4367 Calling other functions from an interrupt service routine is not recommended,
4368 avoid it if possible.
4372 Also see the _naked modifier.
4380 <TODO: this isn't implemented at all!>
4386 A special keyword may be associated with a function declaring it as
4391 SDCC will generate code to disable all interrupts upon entry to a critical
4392 function and enable them back before returning.
4393 Note that nesting critical functions may cause unpredictable results.
4418 The critical attribute maybe used with other attributes like
4426 A special keyword may be associated with a function declaring it as
4435 function modifier attribute prevents the compiler from generating prologue
4436 and epilogue code for that function.
4437 This means that the user is entirely responsible for such things as saving
4438 any registers that may need to be preserved, selecting the proper register
4439 bank, generating the
4443 instruction at the end, etc.
4444 Practically, this means that the contents of the function must be written
4445 in inline assembler.
4446 This is particularly useful for interrupt functions, which can have a large
4447 (and often unnecessary) prologue/epilogue.
4448 For example, compare the code generated by these two functions:
4454 data unsigned char counter;
4456 void simpleInterrupt(void) interrupt 1
4470 void nakedInterrupt(void) interrupt 2 _naked
4503 ; MUST explicitly include ret in _naked function.
4517 For an 8051 target, the generated simpleInterrupt looks like:
4662 whereas nakedInterrupt looks like:
4687 ; MUST explicitly include ret(i) in _naked function.
4693 While there is nothing preventing you from writing C code inside a _naked
4694 function, there are many ways to shoot yourself in the foot doing this,
4695 and it is recommended that you stick to inline assembler.
4698 Functions using private banks
4705 attribute (which tells the compiler to use a register bank other than the
4706 default bank zero) should only be applied to
4710 functions (see note 1 below).
4711 This will in most circumstances make the generated ISR code more efficient
4712 since it will not have to save registers on the stack.
4719 attribute will have no effect on the generated code for a
4723 function (but may occasionally be useful anyway
4729 possible exception: if a function is called ONLY from 'interrupt' functions
4730 using a particular bank, it can be declared with the same 'using' attribute
4731 as the calling 'interrupt' functions.
4732 For instance, if you have several ISRs using bank one, and all of them
4733 call memcpy(), it might make sense to create a specialized version of memcpy()
4734 'using 1', since this would prevent the ISR from having to save bank zero
4735 to the stack on entry and switch to bank zero before calling the function
4742 (pending: I don't think this has been done yet)
4749 function using a non-zero bank will assume that it can trash that register
4750 bank, and will not save it.
4751 Since high-priority interrupts can interrupt low-priority ones on the 8051
4752 and friends, this means that if a high-priority ISR
4756 a particular bank occurs while processing a low-priority ISR
4760 the same bank, terrible and bad things can happen.
4761 To prevent this, no single register bank should be
4765 by both a high priority and a low priority ISR.
4766 This is probably most easily done by having all high priority ISRs use
4767 one bank and all low priority ISRs use another.
4768 If you have an ISR which can change priority at runtime, you're on your
4769 own: I suggest using the default bank zero and taking the small performance
4773 It is most efficient if your ISR calls no other functions.
4774 If your ISR must call other functions, it is most efficient if those functions
4775 use the same bank as the ISR (see note 1 below); the next best is if the
4776 called functions use bank zero.
4777 It is very inefficient to call a function using a different, non-zero bank
4785 Data items can be assigned an absolute address with the
4789 keyword, in addition to a storage class, e.g.:
4795 xdata at 0x8000 unsigned char PORTA_8255 ;
4801 In the above example the PORTA_8255 will be allocated to the location 0x8000
4802 of the external ram.
4803 Note that this feature is provided to give the programmer access to
4807 devices attached to the controller.
4808 The compiler does not actually reserve any space for variables declared
4809 in this way (they are implemented with an equate in the assembler).
4810 Thus it is left to the programmer to make sure there are no overlaps with
4811 other variables that are declared without the absolute address.
4812 The assembler listing file (.lst) and the linker output files (.rst) and
4813 (.map) are a good places to look for such overlaps.
4817 Absolute address can be specified for variables in all storage classes,
4830 The above example will allocate the variable at offset 0x02 in the bit-addressab
4832 There is no real advantage to assigning absolute addresses to variables
4833 in this manner, unless you want strict control over all the variables allocated.
4839 The compiler inserts a call to the C routine
4841 _sdcc__external__startup()
4846 at the start of the CODE area.
4847 This routine is in the runtime library.
4848 By default this routine returns 0, if this routine returns a non-zero value,
4849 the static & global variable initialization will be skipped and the function
4850 main will be invoked Other wise static & global variables will be initialized
4851 before the function main is invoked.
4854 _sdcc__external__startup()
4856 routine to your program to override the default if you need to setup hardware
4857 or perform some other critical operation prior to static & global variable
4861 Inline Assembler Code
4864 SDCC allows the use of in-line assembler with a few restriction as regards
4866 All labels defined within inline assembler code
4874 where nnnn is a number less than 100 (which implies a limit of utmost 100
4875 inline assembler labels
4883 It is strongly recommended that each assembly instruction (including labels)
4884 be placed in a separate line (as the example shows).
4889 command line option is used, the inline assembler code will be passed through
4890 the peephole optimizer.
4891 This might cause some unexpected changes in the inline assembler code.
4892 Please go throught the peephole optimizer rules defined in file
4896 carefully before using this option.
4936 The inline assembler code can contain any valid code understood by the assembler
4937 , this includes any assembler directives and comment lines.
4938 The compiler does not do any validation of the code within the
4948 Inline assembler code cannot reference any C-Labels, however it can reference
4949 labels defined by the inline assembler, e.g.:
4975 ; some assembler code
4995 /* some more c code */
4997 clabel:\SpecialChar ~
4999 /* inline assembler cannot reference this label */
5011 $0003: ;label (can be reference by inline assembler only)
5023 /* some more c code */
5031 In other words inline assembly code can access labels defined in inline
5032 assembly within the scope of the funtion.
5036 The same goes the other way, ie.
5037 labels defines in inline assembly CANNOT be accessed by C statements.
5040 int (16 bit) and long (32 bit) Support
5043 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
5044 multiplication and modulus operations are implemented by support routines.
5045 These support routines are all developed in ANSI-C to facilitate porting
5046 to other MCUs, although some model specific assembler optimations are used.
5047 The following files contain the described routine, all of them can be found
5048 in <installdir>/share/sdcc/lib.
5054 <pending: tabularise this>
5060 _mulsint.c - signed 16 bit multiplication (calls _muluint)
5062 _muluint.c - unsigned 16 bit multiplication
5064 _divsint.c - signed 16 bit division (calls _divuint)
5066 _divuint.c - unsigned 16 bit division
5068 _modsint.c - signed 16 bit modulus (call _moduint)
5070 _moduint.c - unsigned 16 bit modulus
5072 _mulslong.c - signed 32 bit multiplication (calls _mululong)
5074 _mululong.c - unsigned32 bit multiplication
5076 _divslong.c - signed 32 division (calls _divulong)
5078 _divulong.c - unsigned 32 division
5080 _modslong.c - signed 32 bit modulus (calls _modulong)
5082 _modulong.c - unsigned 32 bit modulus
5090 Since they are compiled as
5094 , interrupt service routines should not do any of the above operations.
5095 If this is unavoidable then the above routines will need to be compiled
5100 option, after which the source program will have to be compiled with
5107 Floating Point Support
5110 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
5111 point support routines are derived from gcc's floatlib.c and consists of
5112 the following routines:
5118 <pending: tabularise this>
5124 _fsadd.c - add floating point numbers
5126 _fssub.c - subtract floating point numbers
5128 _fsdiv.c - divide floating point numbers
5130 _fsmul.c - multiply floating point numbers
5132 _fs2uchar.c - convert floating point to unsigned char
5134 _fs2char.c - convert floating point to signed char
5136 _fs2uint.c - convert floating point to unsigned int
5138 _fs2int.c - convert floating point to signed int
5140 _fs2ulong.c - convert floating point to unsigned long
5142 _fs2long.c - convert floating point to signed long
5144 _uchar2fs.c - convert unsigned char to floating point
5146 _char2fs.c - convert char to floating point number
5148 _uint2fs.c - convert unsigned int to floating point
5150 _int2fs.c - convert int to floating point numbers
5152 _ulong2fs.c - convert unsigned long to floating point number
5154 _long2fs.c - convert long to floating point number
5162 Note if all these routines are used simultaneously the data space might
5164 For serious floating point usage it is strongly recommended that the large
5171 SDCC allows two memory models for MCS51 code, small and large.
5172 Modules compiled with different memory models should
5176 be combined together or the results would be unpredictable.
5177 The library routines supplied with the compiler are compiled as both small
5179 The compiled library modules are contained in seperate directories as small
5180 and large so that you can link to either set.
5184 When the large model is used all variables declared without a storage class
5185 will be allocated into the external ram, this includes all parameters and
5186 local variables (for non-reentrant functions).
5187 When the small model is used variables without storage class are allocated
5188 in the internal ram.
5191 Judicious usage of the processor specific storage classes and the 'reentrant'
5192 function type will yield much more efficient code, than using the large
5194 Several optimizations are disabled when the program is compiled using the
5195 large model, it is therefore strongly recommdended that the small model
5196 be used unless absolutely required.
5202 The only model supported is Flat 24.
5203 This generates code for the 24 bit contiguous addressing mode of the Dallas
5205 In this mode, up to four meg of external RAM or code space can be directly
5207 See the data sheets at www.dalsemi.com for further information on this part.
5211 In older versions of the compiler, this option was used with the MCS51 code
5217 Now, however, the '390 has it's own code generator, selected by the
5226 Note that the compiler does not generate any code to place the processor
5227 into 24 bitmode (although
5231 in the ds390 libraries will do that for you).
5236 , the boot loader or similar code must ensure that the processor is in 24
5237 bit contiguous addressing mode before calling the SDCC startup code.
5245 option, variables will by default be placed into the XDATA segment.
5250 Segments may be placed anywhere in the 4 meg address space using the usual
5252 Note that if any segments are located above 64K, the -r flag must be passed
5253 to the linker to generate the proper segment relocations, and the Intel
5254 HEX output format must be used.
5255 The -r flag can be passed to the linker by using the option
5259 on the sdcc command line.
5260 However, currently the linker can not handle code segments > 64k.
5263 Defines Created by the Compiler
5266 The compiler creates the following #defines.
5269 SDCC - this Symbol is always defined.
5272 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
5276 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
5280 SDCC_STACK_AUTO - this symbol is defined when
5287 SDCC_MODEL_SMALL - when
5294 SDCC_MODEL_LARGE - when
5301 SDCC_USE_XSTACK - when
5308 SDCC_STACK_TENBIT - when
5315 SDCC_MODEL_FLAT24 - when
5328 SDCC performs a host of standard optimizations in addition to some MCU specific
5331 \layout Subsubsection
5333 Sub-expression Elimination
5336 The compiler does local and global common subexpression elimination, e.g.:
5351 will be translated to
5367 Some subexpressions are not as obvious as the above example, e.g.:
5381 In this case the address arithmetic a->b[i] will be computed only once;
5382 the equivalent code in C would be.
5398 The compiler will try to keep these temporary variables in registers.
5399 \layout Subsubsection
5401 Dead-Code Elimination
5416 i = 1; \SpecialChar ~
5421 global = 1;\SpecialChar ~
5434 global = 3;\SpecialChar ~
5449 int global; void f ()
5462 \layout Subsubsection
5523 Note: the dead stores created by this copy propagation will be eliminated
5524 by dead-code elimination.
5525 \layout Subsubsection
5530 Two types of loop optimizations are done by SDCC loop invariant lifting
5531 and strength reduction of loop induction variables.
5532 In addition to the strength reduction the optimizer marks the induction
5533 variables and the register allocator tries to keep the induction variables
5534 in registers for the duration of the loop.
5535 Because of this preference of the register allocator, loop induction optimizati
5536 on causes an increase in register pressure, which may cause unwanted spilling
5537 of other temporary variables into the stack / data space.
5538 The compiler will generate a warning message when it is forced to allocate
5539 extra space either on the stack or data space.
5540 If this extra space allocation is undesirable then induction optimization
5541 can be eliminated either for the entire source file (with ---noinduction
5542 option) or for a given function only using #pragma\SpecialChar ~
5553 for (i = 0 ; i < 100 ; i ++)
5571 for (i = 0; i < 100; i++)
5581 As mentioned previously some loop invariants are not as apparent, all static
5582 address computations are also moved out of the loop.
5586 Strength Reduction, this optimization substitutes an expression by a cheaper
5593 for (i=0;i < 100; i++)
5613 for (i=0;i< 100;i++) {
5617 ar[itemp1] = itemp2;
5633 The more expensive multiplication is changed to a less expensive addition.
5634 \layout Subsubsection
5639 This optimization is done to reduce the overhead of checking loop boundaries
5640 for every iteration.
5641 Some simple loops can be reversed and implemented using a
5642 \begin_inset Quotes eld
5645 decrement and jump if not zero
5646 \begin_inset Quotes erd
5650 SDCC checks for the following criterion to determine if a loop is reversible
5651 (note: more sophisticated compilers use data-dependency analysis to make
5652 this determination, SDCC uses a more simple minded analysis).
5655 The 'for' loop is of the form
5661 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
5671 The <for body> does not contain
5672 \begin_inset Quotes eld
5676 \begin_inset Quotes erd
5680 \begin_inset Quotes erd
5686 All goto's are contained within the loop.
5689 No function calls within the loop.
5692 The loop control variable <sym> is not assigned any value within the loop
5695 The loop control variable does NOT participate in any arithmetic operation
5699 There are NO switch statements in the loop.
5700 \layout Subsubsection
5702 Algebraic Simplifications
5705 SDCC does numerous algebraic simplifications, the following is a small sub-set
5706 of these optimizations.
5712 i = j + 0 ; /* changed to */ i = j;
5714 i /= 2; /* changed to */ i >>= 1;
5716 i = j - j ; /* changed to */ i = 0;
5718 i = j / 1 ; /* changed to */ i = j;
5724 Note the subexpressions given above are generally introduced by macro expansions
5725 or as a result of copy/constant propagation.
5726 \layout Subsubsection
5731 SDCC changes switch statements to jump tables when the following conditions
5736 The case labels are in numerical sequence, the labels need not be in order,
5737 and the starting number need not be one or zero.
5743 switch(i) {\SpecialChar ~
5850 Both the above switch statements will be implemented using a jump-table.
5853 The number of case labels is at least three, since it takes two conditional
5854 statements to handle the boundary conditions.
5857 The number of case labels is less than 84, since each label takes 3 bytes
5858 and a jump-table can be utmost 256 bytes long.
5862 Switch statements which have gaps in the numeric sequence or those that
5863 have more that 84 case labels can be split into more than one switch statement
5864 for efficient code generation, e.g.:
5902 If the above switch statement is broken down into two switch statements
5936 case 9: \SpecialChar ~
5946 case 12:\SpecialChar ~
5956 then both the switch statements will be implemented using jump-tables whereas
5957 the unmodified switch statement will not be.
5958 \layout Subsubsection
5960 Bit-shifting Operations.
5963 Bit shifting is one of the most frequently used operation in embedded programmin
5965 SDCC tries to implement bit-shift operations in the most efficient way
5985 generates the following code:
6003 In general SDCC will never setup a loop if the shift count is known.
6043 Note that SDCC stores numbers in little-endian format (i.e.
6044 lowest order first).
6045 \layout Subsubsection
6050 A special case of the bit-shift operation is bit rotation, SDCC recognizes
6051 the following expression to be a left bit-rotation:
6062 i = ((i << 1) | (i >> 7));
6070 will generate the following code:
6086 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
6087 ns of this case will also be recognized as bit-rotation, i.e.:
6093 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
6094 \layout Subsubsection
6099 It is frequently required to obtain the highest order bit of an integral
6100 type (long, int, short or char types).
6101 SDCC recognizes the following expression to yield the highest order bit
6102 and generates optimized code for it, e.g.:
6123 hob = (gint >> 15) & 1;
6136 will generate the following code:
6175 000A E5*01\SpecialChar ~
6203 000C 33\SpecialChar ~
6234 000D E4\SpecialChar ~
6265 000E 13\SpecialChar ~
6296 000F F5*02\SpecialChar ~
6326 Variations of this case however will
6331 It is a standard C expression, so I heartily recommend this be the only
6332 way to get the highest order bit, (it is portable).
6333 Of course it will be recognized even if it is embedded in other expressions,
6340 xyz = gint + ((gint >> 15) & 1);
6346 will still be recognized.
6347 \layout Subsubsection
6352 The compiler uses a rule based, pattern matching and re-writing mechanism
6353 for peep-hole optimization.
6358 a peep-hole optimizer by Christopher W.
6359 Fraser (cwfraser@microsoft.com).
6360 A default set of rules are compiled into the compiler, additional rules
6361 may be added with the
6363 ---peep-file <filename>
6366 The rule language is best illustrated with examples.
6394 The above rule will change the following assembly sequence:
6424 Note: All occurrences of a
6428 (pattern variable) must denote the same string.
6429 With the above rule, the assembly sequence:
6447 will remain unmodified.
6451 Other special case optimizations may be added by the user (via
6457 some variants of the 8051 MCU allow only
6466 The following two rules will change all
6488 replace { lcall %1 } by { acall %1 }
6490 replace { ljmp %1 } by { ajmp %1 }
6498 inline-assembler code
6500 is also passed through the peep hole optimizer, thus the peephole optimizer
6501 can also be used as an assembly level macro expander.
6502 The rules themselves are MCU dependent whereas the rule language infra-structur
6503 e is MCU independent.
6504 Peephole optimization rules for other MCU can be easily programmed using
6509 The syntax for a rule is as follows:
6515 rule := replace [ restart ] '{' <assembly sequence> '
6553 <assembly sequence> '
6571 '}' [if <functionName> ] '
6579 <assembly sequence> := assembly instruction (each instruction including
6580 labels must be on a separate line).
6584 The optimizer will apply to the rules one by one from the top in the sequence
6585 of their appearance, it will terminate when all rules are exhausted.
6586 If the 'restart' option is specified, then the optimizer will start matching
6587 the rules again from the top, this option for a rule is expensive (performance)
6588 , it is intended to be used in situations where a transformation will trigger
6589 the same rule again.
6590 An example of this (not a good one, it has side effects) is the following
6617 Note that the replace pattern cannot be a blank, but can be a comment line.
6618 Without the 'restart' option only the inner most 'pop' 'push' pair would
6619 be eliminated, i.e.:
6671 the restart option the rule will be applied again to the resulting code
6672 and then all the pop-push pairs will be eliminated to yield:
6690 A conditional function can be attached to a rule.
6691 Attaching rules are somewhat more involved, let me illustrate this with
6722 The optimizer does a look-up of a function name table defined in function
6727 in the source file SDCCpeeph.c, with the name
6732 If it finds a corresponding entry the function is called.
6733 Note there can be no parameters specified for these functions, in this
6738 is crucial, since the function
6742 expects to find the label in that particular variable (the hash table containin
6743 g the variable bindings is passed as a parameter).
6744 If you want to code more such functions, take a close look at the function
6745 labelInRange and the calling mechanism in source file SDCCpeeph.c.
6746 I know this whole thing is a little kludgey, but maybe some day we will
6747 have some better means.
6748 If you are looking at this file, you will also see the default rules that
6749 are compiled into the compiler, you can add your own rules in the default
6750 set there if you get tired of specifying the ---peep-file option.
6756 SDCC supports the following #pragma directives.
6759 SAVE - this will save all the current options.
6762 RESTORE - will restore the saved options from the last save.
6763 Note that SAVEs & RESTOREs cannot be nested.
6764 SDCC uses the same buffer to save the options each time a SAVE is called.
6765 (jwk burps: either fix that or throw a warning)
6768 NOGCSE - will stop global subexpression elimination.
6771 NOINDUCTION - will stop loop induction optimizations.
6774 NOJTBOUND - will not generate code for boundary value checking, when switch
6775 statements are turned into jump-tables.
6778 NOOVERLAY - the compiler will not overlay the parameters and local variables
6782 LESS_PEDANTIC - the compiler will not warn you anymore for obvious mistakes,
6783 you'r on your own now ;-(
6786 NOLOOPREVERSE - Will not do loop reversal optimization
6789 EXCLUDE NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation
6790 of pair of push/pop instruction in ISR function (using interrupt keyword).
6791 The directive should be placed immediately before the ISR function definition
6792 and it affects ALL ISR functions following it.
6793 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
6794 EXCLUDE\SpecialChar ~
6798 NOIV - Do not generate interrupt vector table entries for all ISR functions
6799 defined after the pragma.
6800 This is useful in cases where the interrupt vector table must be defined
6801 manually, or when there is a secondary, manually defined interrupt vector
6803 for the autovector feature of the Cypress EZ-USB FX2).
6806 CALLEE-SAVES function1[,function2[,function3...]] - The compiler by default
6807 uses a caller saves convention for register saving across function calls,
6808 however this can cause unneccessary register pushing & popping when calling
6809 small functions from larger functions.
6810 This option can be used to switch the register saving convention for the
6811 function names specified.
6812 The compiler will not save registers when calling these functions, extra
6813 code will be generated at the entry & exit for these functions to save
6814 & restore the registers used by these functions, this can SUBSTANTIALLY
6815 reduce code & improve run time performance of the generated code.
6816 In future the compiler (with interprocedural analysis) will be able to
6817 determine the appropriate scheme to use for each function call.
6818 If ---callee-saves command line option is used, the function names specified
6819 in #pragma\SpecialChar ~
6820 CALLEE-SAVES is appended to the list of functions specified inthe
6824 The pragma's are intended to be used to turn-off certain optimizations which
6825 might cause the compiler to generate extra stack / data space to store
6826 compiler generated temporary variables.
6827 This usually happens in large functions.
6828 Pragma directives should be used as shown in the following example, they
6829 are used to control options & optimizations for a given function; pragmas
6830 should be placed before and/or after a function, placing pragma's inside
6831 a function body could have unpredictable results.
6837 #pragma SAVE /* save the current settings */
6839 #pragma NOGCSE /* turnoff global subexpression elimination */
6841 #pragma NOINDUCTION /* turn off induction optimizations */
6863 #pragma RESTORE /* turn the optimizations back on */
6869 The compiler will generate a warning message when extra space is allocated.
6870 It is strongly recommended that the SAVE and RESTORE pragma's be used when
6871 changing options for a function.
6876 <pending: this is messy and incomplete>
6881 Compiler support routines (_gptrget, _mulint etc)
6884 Stdclib functions (puts, printf, strcat etc)
6887 Math functions (sin, pow, sqrt etc)
6890 Interfacing with Assembly Routines
6891 \layout Subsubsection
6893 Global Registers used for Parameter Passing
6896 The compiler always uses the global registers
6904 to pass the first parameter to a routine.
6905 The second parameter onwards is either allocated on the stack (for reentrant
6906 routines or if ---stack-auto is used) or in the internal / external ram
6907 (depending on the memory model).
6909 \layout Subsubsection
6911 Assembler Routine(non-reentrant)
6914 In the following example the function cfunc calls an assembler routine asm_func,
6915 which takes two parameters.
6921 extern int asm_func(unsigned char, unsigned char);
6925 int c_func (unsigned char i, unsigned char j)
6933 return asm_func(i,j);
6947 return c_func(10,9);
6955 The corresponding assembler function is:
6961 .globl _asm_func_PARM_2
7025 add a,_asm_func_PARM_2
7061 Note here that the return values are placed in 'dpl' - One byte return value,
7062 'dpl' LSB & 'dph' MSB for two byte values.
7063 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
7064 b' & 'acc' for four byte values.
7067 The parameter naming convention is _<function_name>_PARM_<n>, where n is
7068 the parameter number starting from 1, and counting from the left.
7069 The first parameter is passed in
7070 \begin_inset Quotes eld
7074 \begin_inset Quotes erd
7077 for One bye parameter,
7078 \begin_inset Quotes eld
7082 \begin_inset Quotes erd
7086 \begin_inset Quotes eld
7090 \begin_inset Quotes erd
7094 \begin_inset Quotes eld
7098 \begin_inset Quotes erd
7101 for four bytes, the varible name for the second parameter will be _<function_na
7106 Assemble the assembler routine with the following command:
7113 asx8051 -losg asmfunc.asm
7120 Then compile and link the assembler routine to the C source file with the
7128 sdcc cfunc.c asmfunc.rel
7129 \layout Subsubsection
7131 Assembler Routine(reentrant)
7134 In this case the second parameter onwards will be passed on the stack, the
7135 parameters are pushed from right to left i.e.
7136 after the call the left most parameter will be on the top of the stack.
7143 extern int asm_func(unsigned char, unsigned char);
7147 int c_func (unsigned char i, unsigned char j) reentrant
7155 return asm_func(i,j);
7169 return c_func(10,9);
7177 The corresponding assembler routine is:
7287 The compiling and linking procedure remains the same, however note the extra
7288 entry & exit linkage required for the assembler code, _bp is the stack
7289 frame pointer and is used to compute the offset into the stack for parameters
7290 and local variables.
7296 The external stack is located at the start of the external ram segment,
7297 and is 256 bytes in size.
7298 When ---xstack option is used to compile the program, the parameters and
7299 local variables of all reentrant functions are allocated in this area.
7300 This option is provided for programs with large stack space requirements.
7301 When used with the ---stack-auto option, all parameters and local variables
7302 are allocated on the external stack (note support libraries will need to
7303 be recompiled with the same options).
7306 The compiler outputs the higher order address byte of the external ram segment
7307 into PORT P2, therefore when using the External Stack option, this port
7308 MAY NOT be used by the application program.
7314 Deviations from the compliancy.
7317 functions are not always reentrant.
7320 structures cannot be assigned values directly, cannot be passed as function
7321 parameters or assigned to each other and cannot be a return value from
7348 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
7359 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
7381 return rets;/* is invalid in SDCC although allowed in ANSI */
7386 'long long' (64 bit integers) not supported.
7389 'double' precision floating point not supported.
7392 No support for setjmp and longjmp (for now).
7395 Old K&R style function declarations are NOT allowed.
7401 foo(i,j) /* this old style of function declarations */
7403 int i,j; /* are valid in ANSI but not valid in SDCC */
7417 functions declared as pointers must be dereferenced during the call.
7428 /* has to be called like this */
7430 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
7433 Cyclomatic Complexity
7436 Cyclomatic complexity of a function is defined as the number of independent
7437 paths the program can take during execution of the function.
7438 This is an important number since it defines the number test cases you
7439 have to generate to validate the function.
7440 The accepted industry standard for complexity number is 10, if the cyclomatic
7441 complexity reported by SDCC exceeds 10 you should think about simplification
7442 of the function logic.
7443 Note that the complexity level is not related to the number of lines of
7445 Large functions can have low complexity, and small functions can have large
7451 SDCC uses the following formula to compute the complexity:
7456 complexity = (number of edges in control flow graph) - (number of nodes
7457 in control flow graph) + 2;
7461 Having said that the industry standard is 10, you should be aware that in
7462 some cases it be may unavoidable to have a complexity level of less than
7464 For example if you have switch statement with more than 10 case labels,
7465 each case label adds one to the complexity level.
7466 The complexity level is by no means an absolute measure of the algorithmic
7467 complexity of the function, it does however provide a good starting point
7468 for which functions you might look at for further optimization.
7474 Here are a few guidelines that will help the compiler generate more efficient
7475 code, some of the tips are specific to this compiler others are generally
7476 good programming practice.
7479 Use the smallest data type to represent your data-value.
7480 If it is known in advance that the value is going to be less than 256 then
7481 use an 'unsigned char' instead of a 'short' or 'int'.
7484 Use unsigned when it is known in advance that the value is not going to
7486 This helps especially if you are doing division or multiplication.
7489 NEVER jump into a LOOP.
7492 Declare the variables to be local whenever possible, especially loop control
7493 variables (induction).
7496 Since the compiler does not always do implicit integral promotion, the programme
7497 r should do an explicit cast when integral promotion is required.
7500 Reducing the size of division, multiplication & modulus operations can reduce
7501 code size substantially.
7502 Take the following code for example.
7508 foobar(unsigned int p1, unsigned char ch)
7512 unsigned char ch1 = p1 % ch ;
7523 For the modulus operation the variable ch will be promoted to unsigned int
7524 first then the modulus operation will be performed (this will lead to a
7525 call to support routine _moduint()), and the result will be casted to a
7527 If the code is changed to
7533 foobar(unsigned int p1, unsigned char ch)
7537 unsigned char ch1 = (unsigned char)p1 % ch ;
7548 It would substantially reduce the code generated (future versions of the
7549 compiler will be smart enough to detect such optimization oppurtunities).
7552 Notes on MCS51 memory layout
7555 The 8051 family of micro controller have a minimum of 128 bytes of internal
7556 memory which is structured as follows
7560 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
7563 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
7565 - Bytes 30-7F - 60 bytes for general purpose use.
7569 Normally the SDCC compiler will only utilise the first bank of registers,
7570 but it is possible to specify that other banks of registers should be used
7571 in interrupt routines.
7572 By default, the compiler will place the stack after the last bank of used
7574 if the first 2 banks of registers are used, it will position the base of
7575 the internal stack at address 16 (0X10).
7576 This implies that as the stack grows, it will use up the remaining register
7577 banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
7578 general purpose use.
7581 By default, the compiler uses the 60 general purpose bytes to hold "near
7583 The compiler/optimiser may also declare some Local Variables in this area
7588 If any of the 128 bit variables are used, or near data is being used then
7589 care needs to be taken to ensure that the stack does not grow so much that
7590 it starts to over write either your bit variables or "near data".
7591 There is no runtime checking to prevent this from happening.
7594 The amount of stack being used is affected by the use of the "internal stack"
7595 to save registers before a subroutine call is made (---stack-auto will
7596 declare parameters and local variables on the stack) and the number of
7600 If you detect that the stack is over writing you data, then the following
7602 ---xstack will cause an external stack to be used for saving registers
7603 and (if ---stack-auto is being used) storing parameters and local variables.
7604 However this will produce more code which will be slower to execute.
7608 ---stack-loc will allow you specify the start of the stack, i.e.
7609 you could start it after any data in the general purpose area.
7610 However this may waste the memory not used by the register banks and if
7611 the size of the "near data" increases, it may creep into the bottom of
7615 ---stack-after-data, similar to the ---stack-loc, but it automatically places
7616 the stack after the end of the "near data".
7617 Again this could waste any spare register space.
7620 ---data-loc allows you to specify the start address of the near data.
7621 This could be used to move the "near data" further away from the stack
7622 giving it more room to grow.
7623 This will only work if no bit variables are being used and the stack can
7624 grow to use the bit variable space.
7632 If you find that the stack is over writing your bit variables or "near data"
7633 then the approach which best utilised the internal memory is to position
7634 the "near data" after the last bank of used registers or, if you use bit
7635 variables, after the last bit variable by using the ---data-loc, e.g.
7636 if two register banks are being used and no bit variables, ---data-loc
7637 16, and use the ---stack-after-data option.
7640 If bit variables are being used, another method would be to try and squeeze
7641 the data area in the unused register banks if it will fit, and start the
7642 stack after the last bit variable.
7645 Retargetting for other MCUs.
7648 The issues for retargetting the compiler are far too numerous to be covered
7650 What follows is a brief description of each of the seven phases of the
7651 compiler and its MCU dependency.
7654 Parsing the source and building the annotated parse tree.
7655 This phase is largely MCU independent (except for the language extensions).
7656 Syntax & semantic checks are also done in this phase, along with some initial
7657 optimizations like back patching labels and the pattern matching optimizations
7658 like bit-rotation etc.
7661 The second phase involves generating an intermediate code which can be easy
7662 manipulated during the later phases.
7663 This phase is entirely MCU independent.
7664 The intermediate code generation assumes the target machine has unlimited
7665 number of registers, and designates them with the name iTemp.
7666 The compiler can be made to dump a human readable form of the code generated
7667 by using the ---dumpraw option.
7670 This phase does the bulk of the standard optimizations and is also MCU independe
7672 This phase can be broken down into several sub-phases:
7676 Break down intermediate code (iCode) into basic blocks.
7678 Do control flow & data flow analysis on the basic blocks.
7680 Do local common subexpression elimination, then global subexpression elimination
7682 Dead code elimination
7686 If loop optimizations caused any changes then do 'global subexpression eliminati
7687 on' and 'dead code elimination' again.
7690 This phase determines the live-ranges; by live range I mean those iTemp
7691 variables defined by the compiler that still survive after all the optimization
7693 Live range analysis is essential for register allocation, since these computati
7694 on determines which of these iTemps will be assigned to registers, and for
7698 Phase five is register allocation.
7699 There are two parts to this process.
7703 The first part I call 'register packing' (for lack of a better term).
7704 In this case several MCU specific expression folding is done to reduce
7709 The second part is more MCU independent and deals with allocating registers
7710 to the remaining live ranges.
7711 A lot of MCU specific code does creep into this phase because of the limited
7712 number of index registers available in the 8051.
7715 The Code generation phase is (unhappily), entirely MCU dependent and very
7716 little (if any at all) of this code can be reused for other MCU.
7717 However the scheme for allocating a homogenized assembler operand for each
7718 iCode operand may be reused.
7721 As mentioned in the optimization section the peep-hole optimizer is rule
7722 based system, which can reprogrammed for other MCUs.
7725 SDCDB - Source Level Debugger
7728 SDCC is distributed with a source level debugger.
7729 The debugger uses a command line interface, the command repertoire of the
7730 debugger has been kept as close to gdb (the GNU debugger) as possible.
7731 The configuration and build process is part of the standard compiler installati
7732 on, which also builds and installs the debugger in the target directory
7733 specified during configuration.
7734 The debugger allows you debug BOTH at the C source and at the ASM source
7738 Compiling for Debugging
7743 debug option must be specified for all files for which debug information
7745 The complier generates a .cdb file for each of these files.
7746 The linker updates the .cdb file with the address information.
7747 This .cdb is used by the debugger.
7750 How the Debugger Works
7753 When the ---debug option is specified the compiler generates extra symbol
7754 information some of which are put into the the assembler source and some
7755 are put into the .cdb file, the linker updates the .cdb file with the address
7756 information for the symbols.
7757 The debugger reads the symbolic information generated by the compiler &
7758 the address information generated by the linker.
7759 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
7760 execution is controlled by the debugger.
7761 When a command is issued for the debugger, it translates it into appropriate
7762 commands for the simulator.
7765 Starting the Debugger
7768 The debugger can be started using the following command line.
7769 (Assume the file you are debugging has the file name foo).
7783 The debugger will look for the following files.
7786 foo.c - the source file.
7789 foo.cdb - the debugger symbol information file.
7792 foo.ihx - the intel hex format object file.
7795 Command Line Options.
7798 ---directory=<source file directory> this option can used to specify the
7799 directory search list.
7800 The debugger will look into the directory list specified for source, cdb
7802 The items in the directory list must be separated by ':', e.g.
7803 if the source files can be in the directories /home/src1 and /home/src2,
7804 the ---directory option should be ---directory=/home/src1:/home/src2.
7805 Note there can be no spaces in the option.
7809 -cd <directory> - change to the <directory>.
7812 -fullname - used by GUI front ends.
7815 -cpu <cpu-type> - this argument is passed to the simulator please see the
7816 simulator docs for details.
7819 -X <Clock frequency > this options is passed to the simulator please see
7820 the simulator docs for details.
7823 -s <serial port file> passed to simulator see the simulator docs for details.
7826 -S <serial in,out> passed to simulator see the simulator docs for details.
7832 As mention earlier the command interface for the debugger has been deliberately
7833 kept as close the GNU debugger gdb, as possible.
7834 This will help the integration with existing graphical user interfaces
7835 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
7836 \layout Subsubsection
7838 break [line | file:line | function | file:function]
7841 Set breakpoint at specified line or function:
7850 sdcdb>break foo.c:100
7854 sdcdb>break foo.c:funcfoo
7855 \layout Subsubsection
7857 clear [line | file:line | function | file:function ]
7860 Clear breakpoint at specified line or function:
7869 sdcdb>clear foo.c:100
7873 sdcdb>clear foo.c:funcfoo
7874 \layout Subsubsection
7879 Continue program being debugged, after breakpoint.
7880 \layout Subsubsection
7885 Execute till the end of the current function.
7886 \layout Subsubsection
7891 Delete breakpoint number 'n'.
7892 If used without any option clear ALL user defined break points.
7893 \layout Subsubsection
7895 info [break | stack | frame | registers ]
7898 info break - list all breakpoints
7901 info stack - show the function call stack.
7904 info frame - show information about the current execution frame.
7907 info registers - show content of all registers.
7908 \layout Subsubsection
7913 Step program until it reaches a different source line.
7914 \layout Subsubsection
7919 Step program, proceeding through subroutine calls.
7920 \layout Subsubsection
7925 Start debugged program.
7926 \layout Subsubsection
7931 Print type information of the variable.
7932 \layout Subsubsection
7937 print value of variable.
7938 \layout Subsubsection
7943 load the given file name.
7944 Note this is an alternate method of loading file for debugging.
7945 \layout Subsubsection
7950 print information about current frame.
7951 \layout Subsubsection
7956 Toggle between C source & assembly source.
7957 \layout Subsubsection
7962 Send the string following '!' to the simulator, the simulator response is
7964 Note the debugger does not interpret the command being sent to the simulator,
7965 so if a command like 'go' is sent the debugger can loose its execution
7966 context and may display incorrect values.
7967 \layout Subsubsection
7974 My name is Bobby Brown"
7977 Interfacing with XEmacs.
7980 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
7981 sdcdb.el and sdcdbsrc.el.
7982 These two files can be found in the $(prefix)/bin directory after the installat
7984 These files need to be loaded into XEmacs for the interface to work.
7985 This can be done at XEmacs startup time by inserting the following into
7986 your '.xemacs' file (which can be found in your HOME directory):
7992 (load-file sdcdbsrc.el)
7998 .xemacs is a lisp file so the () around the command is REQUIRED.
7999 The files can also be loaded dynamically while XEmacs is running, set the
8000 environment variable 'EMACSLOADPATH' to the installation bin directory
8001 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
8002 To start the interface enter the following command:
8016 You will prompted to enter the file name to be debugged.
8021 The command line options that are passed to the simulator directly are bound
8022 to default values in the file sdcdbsrc.el.
8023 The variables are listed below, these values maybe changed as required.
8026 sdcdbsrc-cpu-type '51
8029 sdcdbsrc-frequency '11059200
8035 The following is a list of key mapping for the debugger interface.
8043 ;; Current Listing ::
8060 binding\SpecialChar ~
8099 ------\SpecialChar ~
8139 sdcdb-next-from-src\SpecialChar ~
8165 sdcdb-back-from-src\SpecialChar ~
8191 sdcdb-cont-from-src\SpecialChar ~
8201 SDCDB continue command
8217 sdcdb-step-from-src\SpecialChar ~
8243 sdcdb-whatis-c-sexp\SpecialChar ~
8253 SDCDB ptypecommand for data at
8317 sdcdbsrc-delete\SpecialChar ~
8331 SDCDB Delete all breakpoints if no arg
8379 given or delete arg (C-u arg x)
8395 sdcdbsrc-frame\SpecialChar ~
8410 SDCDB Display current frame if no arg,
8459 given or display frame arg
8524 sdcdbsrc-goto-sdcdb\SpecialChar ~
8534 Goto the SDCDB output buffer
8550 sdcdb-print-c-sexp\SpecialChar ~
8561 SDCDB print command for data at
8625 sdcdbsrc-goto-sdcdb\SpecialChar ~
8635 Goto the SDCDB output buffer
8651 sdcdbsrc-mode\SpecialChar ~
8667 Toggles Sdcdbsrc mode (turns it off)
8671 ;; C-c C-f\SpecialChar ~
8679 sdcdb-finish-from-src\SpecialChar ~
8687 SDCDB finish command
8691 ;; C-x SPC\SpecialChar ~
8699 sdcdb-break\SpecialChar ~
8717 Set break for line with point
8719 ;; ESC t\SpecialChar ~
8729 sdcdbsrc-mode\SpecialChar ~
8745 Toggle Sdcdbsrc mode
8747 ;; ESC m\SpecialChar ~
8757 sdcdbsrc-srcmode\SpecialChar ~
8781 The Z80 and gbz80 port
8784 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
8785 The port is incomplete - long support is incomplete (mul, div and mod are
8786 unimplimented), and both float and bitfield support is missing.
8787 Apart from that the code generated is correct.
8790 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
8791 The stack frame is similar to that generated by the IAR Z80 compiler.
8792 IX is used as the base pointer, HL is used as a temporary register, and
8793 BC and DE are available for holding varibles.
8794 IY is currently unusued.
8795 Return values are stored in HL.
8796 One bad side effect of using IX as the base pointer is that a functions
8797 stack frame is limited to 127 bytes - this will be fixed in a later version.
8803 SDCC has grown to be a large project.
8804 The compiler alone (without the preprocessor, assembler and linker) is
8805 about 40,000 lines of code (blank stripped).
8806 The open source nature of this project is a key to its continued growth
8808 You gain the benefit and support of many active software developers and
8810 Is SDCC perfect? No, that's why we need your help.
8811 The developers take pride in fixing reported bugs.
8812 You can help by reporting the bugs and helping other SDCC users.
8813 There are lots of ways to contribute, and we encourage you to take part
8814 in making SDCC a great software package.
8820 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
8821 cc@sdcc.sourceforge.net'.
8822 Bugs will be fixed ASAP.
8823 When reporting a bug, it is very useful to include a small test program
8824 which reproduces the problem.
8825 If you can isolate the problem by looking at the generated assembly code,
8826 this can be very helpful.
8827 Compiling your program with the ---dumpall option can sometimes be useful
8828 in locating optimization problems.
8834 The anatomy of the compiler
8839 This is an excerpt from an atricle published in Circuit Cellar MagaZine
8841 It's a little outdated (the compiler is much more efficient now and user/devell
8842 oper friendly), but pretty well exposes the guts of it all.
8848 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
8849 It is fairly easy to retarget for other 8-bit MCU.
8850 Here we take a look at some of the internals of the compiler.
8857 Parsing the input source file and creating an AST (Annotated Syntax Tree).
8858 This phase also involves propagating types (annotating each node of the
8859 parse tree with type information) and semantic analysis.
8860 There are some MCU specific parsing rules.
8861 For example the storage classes, the extended storage classes are MCU specific
8862 while there may be a xdata storage class for 8051 there is no such storage
8863 class for z80 or Atmel AVR.
8864 SDCC allows MCU specific storage class extensions, i.e.
8865 xdata will be treated as a storage class specifier when parsing 8051 C
8866 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
8873 Intermediate code generation.
8874 In this phase the AST is broken down into three-operand form (iCode).
8875 These three operand forms are represented as doubly linked lists.
8876 ICode is the term given to the intermediate form generated by the compiler.
8877 ICode example section shows some examples of iCode generated for some simple
8884 Bulk of the target independent optimizations is performed in this phase.
8885 The optimizations include constant propagation, common sub-expression eliminati
8886 on, loop invariant code movement, strength reduction of loop induction variables
8887 and dead-code elimination.
8893 During intermediate code generation phase, the compiler assumes the target
8894 machine has infinite number of registers and generates a lot of temporary
8896 The live range computation determines the lifetime of each of these compiler-ge
8897 nerated temporaries.
8898 A picture speaks a thousand words.
8899 ICode example sections show the live range annotations for each of the
8901 It is important to note here, each iCode is assigned a number in the order
8902 of its execution in the function.
8903 The live ranges are computed in terms of these numbers.
8904 The from number is the number of the iCode which first defines the operand
8905 and the to number signifies the iCode which uses this operand last.
8911 The register allocation determines the type and number of registers needed
8913 In most MCUs only a few registers can be used for indirect addressing.
8914 In case of 8051 for example the registers R0 & R1 can be used to indirectly
8915 address the internal ram and DPTR to indirectly address the external ram.
8916 The compiler will try to allocate the appropriate register to pointer variables
8918 ICode example section shows the operands annotated with the registers assigned
8920 The compiler will try to keep operands in registers as much as possible;
8921 there are several schemes the compiler uses to do achieve this.
8922 When the compiler runs out of registers the compiler will check to see
8923 if there are any live operands which is not used or defined in the current
8924 basic block being processed, if there are any found then it will push that
8925 operand and use the registers in this block, the operand will then be popped
8926 at the end of the basic block.
8930 There are other MCU specific considerations in this phase.
8931 Some MCUs have an accumulator; very short-lived operands could be assigned
8932 to the accumulator instead of general-purpose register.
8938 Figure II gives a table of iCode operations supported by the compiler.
8939 The code generation involves translating these operations into corresponding
8940 assembly code for the processor.
8941 This sounds overly simple but that is the essence of code generation.
8942 Some of the iCode operations are generated on a MCU specific manner for
8943 example, the z80 port does not use registers to pass parameters so the
8944 SEND and RECV iCode operations will not be generated, and it also does
8945 not support JUMPTABLES.
8952 <Where is Figure II ?>
8958 This section shows some details of iCode.
8959 The example C code does not do anything useful; it is used as an example
8960 to illustrate the intermediate code generated by the compiler.
8973 /* This function does nothing useful.
8980 for the purpose of explaining iCode */
8983 short function (data int *x)
8991 short i=10; /* dead initialization eliminated */
8996 short sum=10; /* dead initialization eliminated */
9009 while (*x) *x++ = *p++;
9023 /* compiler detects i,j to be induction variables */
9027 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
9039 mul += i * 3; /* this multiplication remains */
9045 gint += j * 3;/* this multiplication changed to addition */
9062 In addition to the operands each iCode contains information about the filename
9063 and line it corresponds to in the source file.
9064 The first field in the listing should be interpreted as follows:
9069 Filename(linenumber: iCode Execution sequence number : ICode hash table
9070 key : loop depth of the iCode).
9075 Then follows the human readable form of the ICode operation.
9076 Each operand of this triplet form can be of three basic types a) compiler
9077 generated temporary b) user defined variable c) a constant value.
9078 Note that local variables and parameters are replaced by compiler generated
9080 Live ranges are computed only for temporaries (i.e.
9081 live ranges are not computed for global variables).
9082 Registers are allocated for temporaries only.
9083 Operands are formatted in the following manner:
9088 Operand Name [lr live-from : live-to ] { type information } [ registers
9094 As mentioned earlier the live ranges are computed in terms of the execution
9095 sequence number of the iCodes, for example
9097 the iTemp0 is live from (i.e.
9098 first defined in iCode with execution sequence number 3, and is last used
9099 in the iCode with sequence number 5).
9100 For induction variables such as iTemp21 the live range computation extends
9101 the lifetime from the start to the end of the loop.
9103 The register allocator used the live range information to allocate registers,
9104 the same registers may be used for different temporaries if their live
9105 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
9106 iTemp17 since their live ranges do not overlap.
9107 In addition the allocator also takes into consideration the type and usage
9108 of a temporary, for example itemp6 is a pointer to near space and is used
9109 as to fetch data from (i.e.
9110 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
9111 Some short lived temporaries are allocated to special registers which have
9112 meaning to the code generator e.g.
9113 iTemp13 is allocated to a pseudo register CC which tells the back end that
9114 the temporary is used only for a conditional jump the code generation makes
9115 use of this information to optimize a compare and jump ICode.
9117 There are several loop optimizations performed by the compiler.
9118 It can detect induction variables iTemp21(i) and iTemp23(j).
9119 Also note the compiler does selective strength reduction, i.e.
9120 the multiplication of an induction variable in line 18 (gint = j * 3) is
9121 changed to addition, a new temporary iTemp17 is allocated and assigned
9122 a initial value, a constant 3 is then added for each iteration of the loop.
9123 The compiler does not change the multiplication in line 17 however since
9124 the processor does support an 8 * 8 bit multiplication.
9126 Note the dead code elimination optimization eliminated the dead assignments
9127 in line 7 & 8 to I and sum respectively.
9134 Sample.c (5:1:0:0) _entry($9) :
9139 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
9144 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
9149 Sample.c(11:4:53:0) preHeaderLbl0($11) :
9154 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
9160 Sample.c(11:6:5:1) _whilecontinue_0($1) :
9165 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
9171 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9176 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
9182 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
9188 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
9194 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
9200 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
9201 * int}[r0] + 0x2 {short}
9206 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
9211 Sample.c(11:17:21:0)_whilebreak_0($3) :
9216 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9221 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9226 Sample.c(15:20:54:0)preHeaderLbl1($13) :
9231 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9236 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9241 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9246 Sample.c(15:24:26:1)_forcond_0($4) :
9251 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
9257 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9262 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
9263 + ITemp21 [lr21:38]{short}[r4]
9268 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
9274 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
9275 + iTemp15 [lr29:30]{short}[r1]
9280 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
9286 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
9292 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
9298 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
9304 Sample.c(19:38:47:1) goto _forcond_0($4)
9309 Sample.c(19:39:48:0)_forbreak_0($7) :
9314 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
9315 + ITemp11 [lr19:40]{short}[r3]
9320 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
9325 Sample.c(20:42:51:0)_return($8) :
9330 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
9336 Finally the code generated for this function:
9377 ; ----------------------------------------------
9387 ; ----------------------------------------------
9397 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
9409 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
9421 ;_whilecontinue_0($1) :
9431 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
9436 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9495 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
9514 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
9561 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
9601 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
9627 ; iTemp6 [lr5:16]{_near * int}[r0] =
9632 ; iTemp6 [lr5:16]{_near * int}[r0] +
9649 ; goto _whilecontinue_0($1)
9661 ; _whilebreak_0($3) :
9671 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9683 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9695 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9707 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9726 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9755 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
9760 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9805 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
9810 ; iTemp21 [lr21:38]{short}[r4]
9836 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
9869 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
9874 ; iTemp15 [lr29:30]{short}[r1]
9893 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
9940 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
9987 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
9999 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
10013 cjne r5,#0xff,00104$
10025 ; goto _forcond_0($4)
10037 ; _forbreak_0($7) :
10047 ; ret iTemp24 [lr40:41]{short}
10090 A few words about basic block successors, predecessors and dominators
10093 Successors are basic blocks that might execute after this basic block.
10095 Predecessors are basic blocks that might execute before reaching this basic
10098 Dominators are basic blocks that WILL execute before reaching this basic
10124 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
10127 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
10130 c) domVect of [BB4] = BB1 ...
10131 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
10139 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
10149 Thanks to all the other volunteer developers who have helped with coding,
10150 testing, web-page creation, distribution sets, etc.
10151 You know who you are :-)
10158 This document was initially written by Sandeep Dutta
10161 All product names mentioned herein may be trademarks of their respective
10167 \begin_inset LatexCommand \printindex{}