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 Looks like noise but is most helpfull for debugging the compiler itself.
3452 \layout Subsubsection
3454 Intermediate Dump Options
3457 The following options are provided for the purpose of retargetting and debugging
3459 These provided a means to dump the intermediate code (iCode) generated
3460 by the compiler in human readable form at various stages of the compilation
3464 \labelwidthstring 00.00.0000
3470 This option will cause the compiler to dump the intermediate code into
3473 <source filename>.dumpraw
3475 just after the intermediate code has been generated for a function, i.e.
3476 before any optimizations are done.
3477 The basic blocks at this stage ordered in the depth first number, so they
3478 may not be in sequence of execution.
3480 \labelwidthstring 00.00.0000
3486 Will create a dump of iCode's, after global subexpression elimination,
3489 <source filename>.dumpgcse.
3491 \labelwidthstring 00.00.0000
3497 Will create a dump of iCode's, after deadcode elimination, into a file
3500 <source filename>.dumpdeadcode.
3502 \labelwidthstring 00.00.0000
3511 Will create a dump of iCode's, after loop optimizations, into a file named
3514 <source filename>.dumploop.
3516 \labelwidthstring 00.00.0000
3525 Will create a dump of iCode's, after live range analysis, into a file named
3528 <source filename>.dumprange.
3530 \labelwidthstring 00.00.0000
3536 Will dump the life ranges for all symbols.
3538 \labelwidthstring 00.00.0000
3547 Will create a dump of iCode's, after register assignment, into a file named
3550 <source filename>.dumprassgn.
3552 \labelwidthstring 00.00.0000
3558 Will create a dump of the live ranges of iTemp's
3560 \labelwidthstring 00.00.0000
3571 Will cause all the above mentioned dumps to be created.
3574 Environment variables
3577 SDCC recognizes the following environment variables:
3579 \labelwidthstring 00.00.0000
3585 SDCC installs a signal handler to be able to delete temporary files after
3586 an user break (^C) or an exception.
3587 If this environment variable is set, SDCC won't install the signal handler
3588 in order to be able to debug SDCC.
3590 \labelwidthstring 00.00.0000
3598 Path, where temporary files will be created.
3599 The order of the variables is the search order.
3600 In a standard *nix environment these variables are not set, and there's
3601 no need to set them.
3602 On Windows it's recommended to set one of them.
3604 \labelwidthstring 00.00.0000
3608 (coming\SpecialChar ~
3613 \begin_inset Quotes sld
3616 2.1 Install and search paths
3617 \begin_inset Quotes srd
3622 \labelwidthstring 00.00.0000
3626 (coming\SpecialChar ~
3631 \begin_inset Quotes sld
3634 2.1 Install and search paths
3635 \begin_inset Quotes srd
3640 \labelwidthstring 00.00.0000
3644 (coming\SpecialChar ~
3649 \begin_inset Quotes sld
3652 2.1 Install and search paths
3653 \begin_inset Quotes srd
3658 \labelwidthstring 00.00.0000
3662 SDCCDIR\SpecialChar ~
3664 replaced\SpecialChar ~
3669 \begin_inset Quotes sld
3672 2.1 Install and search paths
3673 \begin_inset Quotes srd
3679 There are some more environment variables recognized by SDCC, but these
3680 are solely used for debugging purposes.
3681 They can change or disappear very quickly, and will never be documentated.
3684 MCS51/DS390 Storage Class Language Extensions
3687 In addition to the ANSI storage classes SDCC allows the following MCS51
3688 specific storage classes.
3689 \layout Subsubsection
3694 Variables declared with this storage class will be placed in the extern
3700 storage class for Large Memory model, e.g.:
3706 xdata unsigned char xduc;
3707 \layout Subsubsection
3716 storage class for Small Memory model.
3717 Variables declared with this storage class will be allocated in the internal
3725 \layout Subsubsection
3730 Variables declared with this storage class will be allocated into the indirectly
3731 addressable portion of the internal ram of a 8051, e.g.:
3738 \layout Subsubsection
3743 This is a data-type and a storage class specifier.
3744 When a variable is declared as a bit, it is allocated into the bit addressable
3745 memory of 8051, e.g.:
3752 \layout Subsubsection
3757 Like the bit keyword,
3761 signifies both a data-type and storage class, they are used to describe
3762 the special function registers and special bit variables of a 8051, eg:
3768 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
3770 sbit at 0xd7 CY; /* CY (Carry Flag) */
3776 SDCC allows (via language extensions) pointers to explicitly point to any
3777 of the memory spaces of the 8051.
3778 In addition to the explicit pointers, the compiler uses (by default) generic
3779 pointers which can be used to point to any of the memory spaces.
3783 Pointer declaration examples:
3792 /* pointer physically in xternal ram pointing to object in internal ram
3795 data unsigned char * xdata p;
3799 /* pointer physically in code rom pointing to data in xdata space */
3801 xdata unsigned char * code p;
3805 /* pointer physically in code space pointing to data in code space */
3807 code unsigned char * code p;
3811 /* the folowing is a generic pointer physically located in xdata space */
3822 Well you get the idea.
3827 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
3840 The highest order byte of the
3844 pointers contains the data space information.
3845 Assembler support routines are called whenever data is stored or retrieved
3851 These are useful for developing reusable library routines.
3852 Explicitly specifying the pointer type will generate the most efficient
3856 Parameters & Local Variables
3859 Automatic (local) variables and parameters to functions can either be placed
3860 on the stack or in data-space.
3861 The default action of the compiler is to place these variables in the internal
3862 RAM (for small model) or external RAM (for large model).
3863 This in fact makes them
3867 so by default functions are non-reentrant.
3871 They can be placed on the stack either by using the
3875 option or by using the
3879 keyword in the function declaration, e.g.:
3888 unsigned char foo(char i) reentrant
3901 Since stack space on 8051 is limited, the
3909 option should be used sparingly.
3910 Note that the reentrant keyword just means that the parameters & local
3911 variables will be allocated to the stack, it
3915 mean that the function is register bank independent.
3919 Local variables can be assigned storage classes and absolute addresses,
3926 unsigned char foo() {
3932 xdata unsigned char i;
3944 data at 0x31 unsiged char j;
3959 In the above example the variable
3963 will be allocated in the external ram,
3967 in bit addressable space and
3976 or when a function is declared as
3980 this should only be done for static variables.
3983 Parameters however are not allowed any storage class, (storage classes for
3984 parameters will be ignored), their allocation is governed by the memory
3985 model in use, and the reentrancy options.
3991 For non-reentrant functions SDCC will try to reduce internal ram space usage
3992 by overlaying parameters and local variables of a function (if possible).
3993 Parameters and local variables of a function will be allocated to an overlayabl
3994 e segment if the function has
3996 no other function calls and the function is non-reentrant and the memory
4000 If an explicit storage class is specified for a local variable, it will
4004 Note that the compiler (not the linkage editor) makes the decision for overlayin
4006 Functions that are called from an interrupt service routine should be preceded
4007 by a #pragma\SpecialChar ~
4008 NOOVERLAY if they are not reentrant.
4011 Also note that the compiler does not do any processing of inline assembler
4012 code, so the compiler might incorrectly assign local variables and parameters
4013 of a function into the overlay segment if the inline assembler code calls
4014 other c-functions that might use the overlay.
4015 In that case the #pragma\SpecialChar ~
4016 NOOVERLAY should be used.
4019 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
4020 tion or division will NOT be overlayed since these are implemented using
4021 external functions, e.g.:
4031 void set_error(unsigned char errcd)
4047 void some_isr () interrupt 2 using 1
4076 In the above example the parameter
4084 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
4086 not present, this could cause unpredictable runtime behavior when called
4088 The #pragma\SpecialChar ~
4089 NOOVERLAY ensures that the parameters and local variables for
4090 the function are NOT overlayed.
4093 Interrupt Service Routines
4096 SDCC allows interrupt service routines to be coded in C, with some extended
4103 void timer_isr (void) interrupt 2 using 1
4116 The number following the
4120 keyword is the interrupt number this routine will service.
4121 The compiler will insert a call to this routine in the interrupt vector
4122 table for the interrupt number specified.
4127 keyword is used to tell the compiler to use the specified register bank
4128 (8051 specific) when generating code for this function.
4129 Note that when some function is called from an interrupt service routine
4130 it should be preceded by a #pragma\SpecialChar ~
4131 NOOVERLAY if it is not reentrant.
4132 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
4133 ation & modulus operations are implemented using external support routines
4134 developed in ANSI-C, if an interrupt service routine needs to do any of
4135 these operations then the support routines (as mentioned in a following
4136 section) will have to be recompiled using the
4140 option and the source file will need to be compiled using the
4147 If you have multiple source files in your project, interrupt service routines
4148 can be present in any of them, but a prototype of the isr MUST be present
4149 or included in the file that contains the function
4156 Interrupt Numbers and the corresponding address & descriptions for the Standard
4157 8051 are listed below.
4158 SDCC will automatically adjust the interrupt vector table to the maximum
4159 interrupt number specified.
4165 \begin_inset Tabular
4166 <lyxtabular version="3" rows="6" columns="3">
4168 <column alignment="center" valignment="top" leftline="true" width="0pt">
4169 <column alignment="center" valignment="top" leftline="true" width="0pt">
4170 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
4171 <row topline="true" bottomline="true">
4172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4188 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4197 <row topline="true">
4198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4223 <row topline="true">
4224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4232 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4249 <row topline="true">
4250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4275 <row topline="true">
4276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4292 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4301 <row topline="true" bottomline="true">
4302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4335 If the interrupt service routine is defined without
4339 a register bank or with register bank 0 (using 0), the compiler will save
4340 the registers used by itself on the stack upon entry and restore them at
4341 exit, however if such an interrupt service routine calls another function
4342 then the entire register bank will be saved on the stack.
4343 This scheme may be advantageous for small interrupt service routines which
4344 have low register usage.
4347 If the interrupt service routine is defined to be using a specific register
4352 are save and restored, if such an interrupt service routine calls another
4353 function (using another register bank) then the entire register bank of
4354 the called function will be saved on the stack.
4355 This scheme is recommended for larger interrupt service routines.
4358 Calling other functions from an interrupt service routine is not recommended,
4359 avoid it if possible.
4363 Also see the _naked modifier.
4371 <TODO: this isn't implemented at all!>
4377 A special keyword may be associated with a function declaring it as
4382 SDCC will generate code to disable all interrupts upon entry to a critical
4383 function and enable them back before returning.
4384 Note that nesting critical functions may cause unpredictable results.
4409 The critical attribute maybe used with other attributes like
4417 A special keyword may be associated with a function declaring it as
4426 function modifier attribute prevents the compiler from generating prologue
4427 and epilogue code for that function.
4428 This means that the user is entirely responsible for such things as saving
4429 any registers that may need to be preserved, selecting the proper register
4430 bank, generating the
4434 instruction at the end, etc.
4435 Practically, this means that the contents of the function must be written
4436 in inline assembler.
4437 This is particularly useful for interrupt functions, which can have a large
4438 (and often unnecessary) prologue/epilogue.
4439 For example, compare the code generated by these two functions:
4445 data unsigned char counter;
4447 void simpleInterrupt(void) interrupt 1
4461 void nakedInterrupt(void) interrupt 2 _naked
4494 ; MUST explicitly include ret in _naked function.
4508 For an 8051 target, the generated simpleInterrupt looks like:
4653 whereas nakedInterrupt looks like:
4678 ; MUST explicitly include ret(i) in _naked function.
4684 While there is nothing preventing you from writing C code inside a _naked
4685 function, there are many ways to shoot yourself in the foot doing this,
4686 and it is recommended that you stick to inline assembler.
4689 Functions using private banks
4696 attribute (which tells the compiler to use a register bank other than the
4697 default bank zero) should only be applied to
4701 functions (see note 1 below).
4702 This will in most circumstances make the generated ISR code more efficient
4703 since it will not have to save registers on the stack.
4710 attribute will have no effect on the generated code for a
4714 function (but may occasionally be useful anyway
4720 possible exception: if a function is called ONLY from 'interrupt' functions
4721 using a particular bank, it can be declared with the same 'using' attribute
4722 as the calling 'interrupt' functions.
4723 For instance, if you have several ISRs using bank one, and all of them
4724 call memcpy(), it might make sense to create a specialized version of memcpy()
4725 'using 1', since this would prevent the ISR from having to save bank zero
4726 to the stack on entry and switch to bank zero before calling the function
4733 (pending: I don't think this has been done yet)
4740 function using a non-zero bank will assume that it can trash that register
4741 bank, and will not save it.
4742 Since high-priority interrupts can interrupt low-priority ones on the 8051
4743 and friends, this means that if a high-priority ISR
4747 a particular bank occurs while processing a low-priority ISR
4751 the same bank, terrible and bad things can happen.
4752 To prevent this, no single register bank should be
4756 by both a high priority and a low priority ISR.
4757 This is probably most easily done by having all high priority ISRs use
4758 one bank and all low priority ISRs use another.
4759 If you have an ISR which can change priority at runtime, you're on your
4760 own: I suggest using the default bank zero and taking the small performance
4764 It is most efficient if your ISR calls no other functions.
4765 If your ISR must call other functions, it is most efficient if those functions
4766 use the same bank as the ISR (see note 1 below); the next best is if the
4767 called functions use bank zero.
4768 It is very inefficient to call a function using a different, non-zero bank
4776 Data items can be assigned an absolute address with the
4780 keyword, in addition to a storage class, e.g.:
4786 xdata at 0x8000 unsigned char PORTA_8255 ;
4792 In the above example the PORTA_8255 will be allocated to the location 0x8000
4793 of the external ram.
4794 Note that this feature is provided to give the programmer access to
4798 devices attached to the controller.
4799 The compiler does not actually reserve any space for variables declared
4800 in this way (they are implemented with an equate in the assembler).
4801 Thus it is left to the programmer to make sure there are no overlaps with
4802 other variables that are declared without the absolute address.
4803 The assembler listing file (.lst) and the linker output files (.rst) and
4804 (.map) are a good places to look for such overlaps.
4808 Absolute address can be specified for variables in all storage classes,
4821 The above example will allocate the variable at offset 0x02 in the bit-addressab
4823 There is no real advantage to assigning absolute addresses to variables
4824 in this manner, unless you want strict control over all the variables allocated.
4830 The compiler inserts a call to the C routine
4832 _sdcc__external__startup()
4837 at the start of the CODE area.
4838 This routine is in the runtime library.
4839 By default this routine returns 0, if this routine returns a non-zero value,
4840 the static & global variable initialization will be skipped and the function
4841 main will be invoked Other wise static & global variables will be initialized
4842 before the function main is invoked.
4845 _sdcc__external__startup()
4847 routine to your program to override the default if you need to setup hardware
4848 or perform some other critical operation prior to static & global variable
4852 Inline Assembler Code
4855 SDCC allows the use of in-line assembler with a few restriction as regards
4857 All labels defined within inline assembler code
4865 where nnnn is a number less than 100 (which implies a limit of utmost 100
4866 inline assembler labels
4874 It is strongly recommended that each assembly instruction (including labels)
4875 be placed in a separate line (as the example shows).
4880 command line option is used, the inline assembler code will be passed through
4881 the peephole optimizer.
4882 This might cause some unexpected changes in the inline assembler code.
4883 Please go throught the peephole optimizer rules defined in file
4887 carefully before using this option.
4927 The inline assembler code can contain any valid code understood by the assembler
4928 , this includes any assembler directives and comment lines.
4929 The compiler does not do any validation of the code within the
4939 Inline assembler code cannot reference any C-Labels, however it can reference
4940 labels defined by the inline assembler, e.g.:
4966 ; some assembler code
4986 /* some more c code */
4988 clabel:\SpecialChar ~
4990 /* inline assembler cannot reference this label */
5002 $0003: ;label (can be reference by inline assembler only)
5014 /* some more c code */
5022 In other words inline assembly code can access labels defined in inline
5023 assembly within the scope of the funtion.
5027 The same goes the other way, ie.
5028 labels defines in inline assembly CANNOT be accessed by C statements.
5031 int (16 bit) and long (32 bit) Support
5034 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
5035 multiplication and modulus operations are implemented by support routines.
5036 These support routines are all developed in ANSI-C to facilitate porting
5037 to other MCUs, although some model specific assembler optimations are used.
5038 The following files contain the described routine, all of them can be found
5039 in <installdir>/share/sdcc/lib.
5045 <pending: tabularise this>
5051 _mulsint.c - signed 16 bit multiplication (calls _muluint)
5053 _muluint.c - unsigned 16 bit multiplication
5055 _divsint.c - signed 16 bit division (calls _divuint)
5057 _divuint.c - unsigned 16 bit division
5059 _modsint.c - signed 16 bit modulus (call _moduint)
5061 _moduint.c - unsigned 16 bit modulus
5063 _mulslong.c - signed 32 bit multiplication (calls _mululong)
5065 _mululong.c - unsigned32 bit multiplication
5067 _divslong.c - signed 32 division (calls _divulong)
5069 _divulong.c - unsigned 32 division
5071 _modslong.c - signed 32 bit modulus (calls _modulong)
5073 _modulong.c - unsigned 32 bit modulus
5081 Since they are compiled as
5085 , interrupt service routines should not do any of the above operations.
5086 If this is unavoidable then the above routines will need to be compiled
5091 option, after which the source program will have to be compiled with
5098 Floating Point Support
5101 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
5102 point support routines are derived from gcc's floatlib.c and consists of
5103 the following routines:
5109 <pending: tabularise this>
5115 _fsadd.c - add floating point numbers
5117 _fssub.c - subtract floating point numbers
5119 _fsdiv.c - divide floating point numbers
5121 _fsmul.c - multiply floating point numbers
5123 _fs2uchar.c - convert floating point to unsigned char
5125 _fs2char.c - convert floating point to signed char
5127 _fs2uint.c - convert floating point to unsigned int
5129 _fs2int.c - convert floating point to signed int
5131 _fs2ulong.c - convert floating point to unsigned long
5133 _fs2long.c - convert floating point to signed long
5135 _uchar2fs.c - convert unsigned char to floating point
5137 _char2fs.c - convert char to floating point number
5139 _uint2fs.c - convert unsigned int to floating point
5141 _int2fs.c - convert int to floating point numbers
5143 _ulong2fs.c - convert unsigned long to floating point number
5145 _long2fs.c - convert long to floating point number
5153 Note if all these routines are used simultaneously the data space might
5155 For serious floating point usage it is strongly recommended that the large
5162 SDCC allows two memory models for MCS51 code, small and large.
5163 Modules compiled with different memory models should
5167 be combined together or the results would be unpredictable.
5168 The library routines supplied with the compiler are compiled as both small
5170 The compiled library modules are contained in seperate directories as small
5171 and large so that you can link to either set.
5175 When the large model is used all variables declared without a storage class
5176 will be allocated into the external ram, this includes all parameters and
5177 local variables (for non-reentrant functions).
5178 When the small model is used variables without storage class are allocated
5179 in the internal ram.
5182 Judicious usage of the processor specific storage classes and the 'reentrant'
5183 function type will yield much more efficient code, than using the large
5185 Several optimizations are disabled when the program is compiled using the
5186 large model, it is therefore strongly recommdended that the small model
5187 be used unless absolutely required.
5193 The only model supported is Flat 24.
5194 This generates code for the 24 bit contiguous addressing mode of the Dallas
5196 In this mode, up to four meg of external RAM or code space can be directly
5198 See the data sheets at www.dalsemi.com for further information on this part.
5202 In older versions of the compiler, this option was used with the MCS51 code
5208 Now, however, the '390 has it's own code generator, selected by the
5217 Note that the compiler does not generate any code to place the processor
5218 into 24 bitmode (although
5222 in the ds390 libraries will do that for you).
5227 , the boot loader or similar code must ensure that the processor is in 24
5228 bit contiguous addressing mode before calling the SDCC startup code.
5236 option, variables will by default be placed into the XDATA segment.
5241 Segments may be placed anywhere in the 4 meg address space using the usual
5243 Note that if any segments are located above 64K, the -r flag must be passed
5244 to the linker to generate the proper segment relocations, and the Intel
5245 HEX output format must be used.
5246 The -r flag can be passed to the linker by using the option
5250 on the sdcc command line.
5251 However, currently the linker can not handle code segments > 64k.
5254 Defines Created by the Compiler
5257 The compiler creates the following #defines.
5260 SDCC - this Symbol is always defined.
5263 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
5267 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
5271 SDCC_STACK_AUTO - this symbol is defined when
5278 SDCC_MODEL_SMALL - when
5285 SDCC_MODEL_LARGE - when
5292 SDCC_USE_XSTACK - when
5299 SDCC_STACK_TENBIT - when
5306 SDCC_MODEL_FLAT24 - when
5319 SDCC performs a host of standard optimizations in addition to some MCU specific
5322 \layout Subsubsection
5324 Sub-expression Elimination
5327 The compiler does local and global common subexpression elimination, e.g.:
5342 will be translated to
5358 Some subexpressions are not as obvious as the above example, e.g.:
5372 In this case the address arithmetic a->b[i] will be computed only once;
5373 the equivalent code in C would be.
5389 The compiler will try to keep these temporary variables in registers.
5390 \layout Subsubsection
5392 Dead-Code Elimination
5407 i = 1; \SpecialChar ~
5412 global = 1;\SpecialChar ~
5425 global = 3;\SpecialChar ~
5440 int global; void f ()
5453 \layout Subsubsection
5514 Note: the dead stores created by this copy propagation will be eliminated
5515 by dead-code elimination.
5516 \layout Subsubsection
5521 Two types of loop optimizations are done by SDCC loop invariant lifting
5522 and strength reduction of loop induction variables.
5523 In addition to the strength reduction the optimizer marks the induction
5524 variables and the register allocator tries to keep the induction variables
5525 in registers for the duration of the loop.
5526 Because of this preference of the register allocator, loop induction optimizati
5527 on causes an increase in register pressure, which may cause unwanted spilling
5528 of other temporary variables into the stack / data space.
5529 The compiler will generate a warning message when it is forced to allocate
5530 extra space either on the stack or data space.
5531 If this extra space allocation is undesirable then induction optimization
5532 can be eliminated either for the entire source file (with ---noinduction
5533 option) or for a given function only using #pragma\SpecialChar ~
5544 for (i = 0 ; i < 100 ; i ++)
5562 for (i = 0; i < 100; i++)
5572 As mentioned previously some loop invariants are not as apparent, all static
5573 address computations are also moved out of the loop.
5577 Strength Reduction, this optimization substitutes an expression by a cheaper
5584 for (i=0;i < 100; i++)
5604 for (i=0;i< 100;i++) {
5608 ar[itemp1] = itemp2;
5624 The more expensive multiplication is changed to a less expensive addition.
5625 \layout Subsubsection
5630 This optimization is done to reduce the overhead of checking loop boundaries
5631 for every iteration.
5632 Some simple loops can be reversed and implemented using a
5633 \begin_inset Quotes eld
5636 decrement and jump if not zero
5637 \begin_inset Quotes erd
5641 SDCC checks for the following criterion to determine if a loop is reversible
5642 (note: more sophisticated compilers use data-dependency analysis to make
5643 this determination, SDCC uses a more simple minded analysis).
5646 The 'for' loop is of the form
5652 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
5662 The <for body> does not contain
5663 \begin_inset Quotes eld
5667 \begin_inset Quotes erd
5671 \begin_inset Quotes erd
5677 All goto's are contained within the loop.
5680 No function calls within the loop.
5683 The loop control variable <sym> is not assigned any value within the loop
5686 The loop control variable does NOT participate in any arithmetic operation
5690 There are NO switch statements in the loop.
5691 \layout Subsubsection
5693 Algebraic Simplifications
5696 SDCC does numerous algebraic simplifications, the following is a small sub-set
5697 of these optimizations.
5703 i = j + 0 ; /* changed to */ i = j;
5705 i /= 2; /* changed to */ i >>= 1;
5707 i = j - j ; /* changed to */ i = 0;
5709 i = j / 1 ; /* changed to */ i = j;
5715 Note the subexpressions given above are generally introduced by macro expansions
5716 or as a result of copy/constant propagation.
5717 \layout Subsubsection
5722 SDCC changes switch statements to jump tables when the following conditions
5727 The case labels are in numerical sequence, the labels need not be in order,
5728 and the starting number need not be one or zero.
5734 switch(i) {\SpecialChar ~
5841 Both the above switch statements will be implemented using a jump-table.
5844 The number of case labels is at least three, since it takes two conditional
5845 statements to handle the boundary conditions.
5848 The number of case labels is less than 84, since each label takes 3 bytes
5849 and a jump-table can be utmost 256 bytes long.
5853 Switch statements which have gaps in the numeric sequence or those that
5854 have more that 84 case labels can be split into more than one switch statement
5855 for efficient code generation, e.g.:
5893 If the above switch statement is broken down into two switch statements
5927 case 9: \SpecialChar ~
5937 case 12:\SpecialChar ~
5947 then both the switch statements will be implemented using jump-tables whereas
5948 the unmodified switch statement will not be.
5949 \layout Subsubsection
5951 Bit-shifting Operations.
5954 Bit shifting is one of the most frequently used operation in embedded programmin
5956 SDCC tries to implement bit-shift operations in the most efficient way
5976 generates the following code:
5994 In general SDCC will never setup a loop if the shift count is known.
6034 Note that SDCC stores numbers in little-endian format (i.e.
6035 lowest order first).
6036 \layout Subsubsection
6041 A special case of the bit-shift operation is bit rotation, SDCC recognizes
6042 the following expression to be a left bit-rotation:
6053 i = ((i << 1) | (i >> 7));
6061 will generate the following code:
6077 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
6078 ns of this case will also be recognized as bit-rotation, i.e.:
6084 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
6085 \layout Subsubsection
6090 It is frequently required to obtain the highest order bit of an integral
6091 type (long, int, short or char types).
6092 SDCC recognizes the following expression to yield the highest order bit
6093 and generates optimized code for it, e.g.:
6114 hob = (gint >> 15) & 1;
6127 will generate the following code:
6166 000A E5*01\SpecialChar ~
6194 000C 33\SpecialChar ~
6225 000D E4\SpecialChar ~
6256 000E 13\SpecialChar ~
6287 000F F5*02\SpecialChar ~
6317 Variations of this case however will
6322 It is a standard C expression, so I heartily recommend this be the only
6323 way to get the highest order bit, (it is portable).
6324 Of course it will be recognized even if it is embedded in other expressions,
6331 xyz = gint + ((gint >> 15) & 1);
6337 will still be recognized.
6338 \layout Subsubsection
6343 The compiler uses a rule based, pattern matching and re-writing mechanism
6344 for peep-hole optimization.
6349 a peep-hole optimizer by Christopher W.
6350 Fraser (cwfraser@microsoft.com).
6351 A default set of rules are compiled into the compiler, additional rules
6352 may be added with the
6354 ---peep-file <filename>
6357 The rule language is best illustrated with examples.
6385 The above rule will change the following assembly sequence:
6415 Note: All occurrences of a
6419 (pattern variable) must denote the same string.
6420 With the above rule, the assembly sequence:
6438 will remain unmodified.
6442 Other special case optimizations may be added by the user (via
6448 some variants of the 8051 MCU allow only
6457 The following two rules will change all
6479 replace { lcall %1 } by { acall %1 }
6481 replace { ljmp %1 } by { ajmp %1 }
6489 inline-assembler code
6491 is also passed through the peep hole optimizer, thus the peephole optimizer
6492 can also be used as an assembly level macro expander.
6493 The rules themselves are MCU dependent whereas the rule language infra-structur
6494 e is MCU independent.
6495 Peephole optimization rules for other MCU can be easily programmed using
6500 The syntax for a rule is as follows:
6506 rule := replace [ restart ] '{' <assembly sequence> '
6544 <assembly sequence> '
6562 '}' [if <functionName> ] '
6570 <assembly sequence> := assembly instruction (each instruction including
6571 labels must be on a separate line).
6575 The optimizer will apply to the rules one by one from the top in the sequence
6576 of their appearance, it will terminate when all rules are exhausted.
6577 If the 'restart' option is specified, then the optimizer will start matching
6578 the rules again from the top, this option for a rule is expensive (performance)
6579 , it is intended to be used in situations where a transformation will trigger
6580 the same rule again.
6581 An example of this (not a good one, it has side effects) is the following
6608 Note that the replace pattern cannot be a blank, but can be a comment line.
6609 Without the 'restart' option only the inner most 'pop' 'push' pair would
6610 be eliminated, i.e.:
6662 the restart option the rule will be applied again to the resulting code
6663 and then all the pop-push pairs will be eliminated to yield:
6681 A conditional function can be attached to a rule.
6682 Attaching rules are somewhat more involved, let me illustrate this with
6713 The optimizer does a look-up of a function name table defined in function
6718 in the source file SDCCpeeph.c, with the name
6723 If it finds a corresponding entry the function is called.
6724 Note there can be no parameters specified for these functions, in this
6729 is crucial, since the function
6733 expects to find the label in that particular variable (the hash table containin
6734 g the variable bindings is passed as a parameter).
6735 If you want to code more such functions, take a close look at the function
6736 labelInRange and the calling mechanism in source file SDCCpeeph.c.
6737 I know this whole thing is a little kludgey, but maybe some day we will
6738 have some better means.
6739 If you are looking at this file, you will also see the default rules that
6740 are compiled into the compiler, you can add your own rules in the default
6741 set there if you get tired of specifying the ---peep-file option.
6747 SDCC supports the following #pragma directives.
6748 This directives are applicable only at a function level.
6751 SAVE - this will save all the current options.
6754 RESTORE - will restore the saved options from the last save.
6755 Note that SAVES & RESTOREs cannot be nested.
6756 SDCC uses the same buffer to save the options each time a SAVE is called.
6759 NOGCSE - will stop global subexpression elimination.
6762 NOINDUCTION - will stop loop induction optimizations.
6765 NOJTBOUND - will not generate code for boundary value checking, when switch
6766 statements are turned into jump-tables.
6769 NOOVERLAY - the compiler will not overlay the parameters and local variables
6773 NOLOOPREVERSE - Will not do loop reversal optimization
6776 EXCLUDE NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation
6777 of pair of push/pop instruction in ISR function (using interrupt keyword).
6778 The directive should be placed immediately before the ISR function definition
6779 and it affects ALL ISR functions following it.
6780 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
6781 EXCLUDE\SpecialChar ~
6785 NOIV - Do not generate interrupt vector table entries for all ISR functions
6786 defined after the pragma.
6787 This is useful in cases where the interrupt vector table must be defined
6788 manually, or when there is a secondary, manually defined interrupt vector
6790 for the autovector feature of the Cypress EZ-USB FX2).
6793 CALLEE-SAVES function1[,function2[,function3...]] - The compiler by default
6794 uses a caller saves convention for register saving across function calls,
6795 however this can cause unneccessary register pushing & popping when calling
6796 small functions from larger functions.
6797 This option can be used to switch the register saving convention for the
6798 function names specified.
6799 The compiler will not save registers when calling these functions, extra
6800 code will be generated at the entry & exit for these functions to save
6801 & restore the registers used by these functions, this can SUBSTANTIALLY
6802 reduce code & improve run time performance of the generated code.
6803 In future the compiler (with interprocedural analysis) will be able to
6804 determine the appropriate scheme to use for each function call.
6805 If ---callee-saves command line option is used, the function names specified
6806 in #pragma\SpecialChar ~
6807 CALLEE-SAVES is appended to the list of functions specified inthe
6811 The pragma's are intended to be used to turn-off certain optimizations which
6812 might cause the compiler to generate extra stack / data space to store
6813 compiler generated temporary variables.
6814 This usually happens in large functions.
6815 Pragma directives should be used as shown in the following example, they
6816 are used to control options & optimizations for a given function; pragmas
6817 should be placed before and/or after a function, placing pragma's inside
6818 a function body could have unpredictable results.
6824 #pragma SAVE /* save the current settings */
6826 #pragma NOGCSE /* turnoff global subexpression elimination */
6828 #pragma NOINDUCTION /* turn off induction optimizations */
6850 #pragma RESTORE /* turn the optimizations back on */
6856 The compiler will generate a warning message when extra space is allocated.
6857 It is strongly recommended that the SAVE and RESTORE pragma's be used when
6858 changing options for a function.
6863 <pending: this is messy and incomplete>
6868 Compiler support routines (_gptrget, _mulint etc)
6871 Stdclib functions (puts, printf, strcat etc)
6874 Math functions (sin, pow, sqrt etc)
6877 Interfacing with Assembly Routines
6878 \layout Subsubsection
6880 Global Registers used for Parameter Passing
6883 The compiler always uses the global registers
6891 to pass the first parameter to a routine.
6892 The second parameter onwards is either allocated on the stack (for reentrant
6893 routines or if ---stack-auto is used) or in the internal / external ram
6894 (depending on the memory model).
6896 \layout Subsubsection
6898 Assembler Routine(non-reentrant)
6901 In the following example the function cfunc calls an assembler routine asm_func,
6902 which takes two parameters.
6908 extern int asm_func(unsigned char, unsigned char);
6912 int c_func (unsigned char i, unsigned char j)
6920 return asm_func(i,j);
6934 return c_func(10,9);
6942 The corresponding assembler function is:
6948 .globl _asm_func_PARM_2
7012 add a,_asm_func_PARM_2
7048 Note here that the return values are placed in 'dpl' - One byte return value,
7049 'dpl' LSB & 'dph' MSB for two byte values.
7050 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
7051 b' & 'acc' for four byte values.
7054 The parameter naming convention is _<function_name>_PARM_<n>, where n is
7055 the parameter number starting from 1, and counting from the left.
7056 The first parameter is passed in
7057 \begin_inset Quotes eld
7061 \begin_inset Quotes erd
7064 for One bye parameter,
7065 \begin_inset Quotes eld
7069 \begin_inset Quotes erd
7073 \begin_inset Quotes eld
7077 \begin_inset Quotes erd
7081 \begin_inset Quotes eld
7085 \begin_inset Quotes erd
7088 for four bytes, the varible name for the second parameter will be _<function_na
7093 Assemble the assembler routine with the following command:
7100 asx8051 -losg asmfunc.asm
7107 Then compile and link the assembler routine to the C source file with the
7115 sdcc cfunc.c asmfunc.rel
7116 \layout Subsubsection
7118 Assembler Routine(reentrant)
7121 In this case the second parameter onwards will be passed on the stack, the
7122 parameters are pushed from right to left i.e.
7123 after the call the left most parameter will be on the top of the stack.
7130 extern int asm_func(unsigned char, unsigned char);
7134 int c_func (unsigned char i, unsigned char j) reentrant
7142 return asm_func(i,j);
7156 return c_func(10,9);
7164 The corresponding assembler routine is:
7274 The compiling and linking procedure remains the same, however note the extra
7275 entry & exit linkage required for the assembler code, _bp is the stack
7276 frame pointer and is used to compute the offset into the stack for parameters
7277 and local variables.
7283 The external stack is located at the start of the external ram segment,
7284 and is 256 bytes in size.
7285 When ---xstack option is used to compile the program, the parameters and
7286 local variables of all reentrant functions are allocated in this area.
7287 This option is provided for programs with large stack space requirements.
7288 When used with the ---stack-auto option, all parameters and local variables
7289 are allocated on the external stack (note support libraries will need to
7290 be recompiled with the same options).
7293 The compiler outputs the higher order address byte of the external ram segment
7294 into PORT P2, therefore when using the External Stack option, this port
7295 MAY NOT be used by the application program.
7301 Deviations from the compliancy.
7304 functions are not always reentrant.
7307 structures cannot be assigned values directly, cannot be passed as function
7308 parameters or assigned to each other and cannot be a return value from
7335 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
7346 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
7368 return rets;/* is invalid in SDCC although allowed in ANSI */
7373 'long long' (64 bit integers) not supported.
7376 'double' precision floating point not supported.
7379 No support for setjmp and longjmp (for now).
7382 Old K&R style function declarations are NOT allowed.
7388 foo(i,j) /* this old style of function declarations */
7390 int i,j; /* are valid in ANSI but not valid in SDCC */
7404 functions declared as pointers must be dereferenced during the call.
7415 /* has to be called like this */
7417 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
7420 Cyclomatic Complexity
7423 Cyclomatic complexity of a function is defined as the number of independent
7424 paths the program can take during execution of the function.
7425 This is an important number since it defines the number test cases you
7426 have to generate to validate the function.
7427 The accepted industry standard for complexity number is 10, if the cyclomatic
7428 complexity reported by SDCC exceeds 10 you should think about simplification
7429 of the function logic.
7430 Note that the complexity level is not related to the number of lines of
7432 Large functions can have low complexity, and small functions can have large
7438 SDCC uses the following formula to compute the complexity:
7443 complexity = (number of edges in control flow graph) - (number of nodes
7444 in control flow graph) + 2;
7448 Having said that the industry standard is 10, you should be aware that in
7449 some cases it be may unavoidable to have a complexity level of less than
7451 For example if you have switch statement with more than 10 case labels,
7452 each case label adds one to the complexity level.
7453 The complexity level is by no means an absolute measure of the algorithmic
7454 complexity of the function, it does however provide a good starting point
7455 for which functions you might look at for further optimization.
7461 Here are a few guidelines that will help the compiler generate more efficient
7462 code, some of the tips are specific to this compiler others are generally
7463 good programming practice.
7466 Use the smallest data type to represent your data-value.
7467 If it is known in advance that the value is going to be less than 256 then
7468 use an 'unsigned char' instead of a 'short' or 'int'.
7471 Use unsigned when it is known in advance that the value is not going to
7473 This helps especially if you are doing division or multiplication.
7476 NEVER jump into a LOOP.
7479 Declare the variables to be local whenever possible, especially loop control
7480 variables (induction).
7483 Since the compiler does not always do implicit integral promotion, the programme
7484 r should do an explicit cast when integral promotion is required.
7487 Reducing the size of division, multiplication & modulus operations can reduce
7488 code size substantially.
7489 Take the following code for example.
7495 foobar(unsigned int p1, unsigned char ch)
7499 unsigned char ch1 = p1 % ch ;
7510 For the modulus operation the variable ch will be promoted to unsigned int
7511 first then the modulus operation will be performed (this will lead to a
7512 call to support routine _moduint()), and the result will be casted to a
7514 If the code is changed to
7520 foobar(unsigned int p1, unsigned char ch)
7524 unsigned char ch1 = (unsigned char)p1 % ch ;
7535 It would substantially reduce the code generated (future versions of the
7536 compiler will be smart enough to detect such optimization oppurtunities).
7539 Notes on MCS51 memory layout
7542 The 8051 family of micro controller have a minimum of 128 bytes of internal
7543 memory which is structured as follows
7547 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
7550 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
7552 - Bytes 30-7F - 60 bytes for general purpose use.
7556 Normally the SDCC compiler will only utilise the first bank of registers,
7557 but it is possible to specify that other banks of registers should be used
7558 in interrupt routines.
7559 By default, the compiler will place the stack after the last bank of used
7561 if the first 2 banks of registers are used, it will position the base of
7562 the internal stack at address 16 (0X10).
7563 This implies that as the stack grows, it will use up the remaining register
7564 banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
7565 general purpose use.
7568 By default, the compiler uses the 60 general purpose bytes to hold "near
7570 The compiler/optimiser may also declare some Local Variables in this area
7575 If any of the 128 bit variables are used, or near data is being used then
7576 care needs to be taken to ensure that the stack does not grow so much that
7577 it starts to over write either your bit variables or "near data".
7578 There is no runtime checking to prevent this from happening.
7581 The amount of stack being used is affected by the use of the "internal stack"
7582 to save registers before a subroutine call is made (---stack-auto will
7583 declare parameters and local variables on the stack) and the number of
7587 If you detect that the stack is over writing you data, then the following
7589 ---xstack will cause an external stack to be used for saving registers
7590 and (if ---stack-auto is being used) storing parameters and local variables.
7591 However this will produce more code which will be slower to execute.
7595 ---stack-loc will allow you specify the start of the stack, i.e.
7596 you could start it after any data in the general purpose area.
7597 However this may waste the memory not used by the register banks and if
7598 the size of the "near data" increases, it may creep into the bottom of
7602 ---stack-after-data, similar to the ---stack-loc, but it automatically places
7603 the stack after the end of the "near data".
7604 Again this could waste any spare register space.
7607 ---data-loc allows you to specify the start address of the near data.
7608 This could be used to move the "near data" further away from the stack
7609 giving it more room to grow.
7610 This will only work if no bit variables are being used and the stack can
7611 grow to use the bit variable space.
7619 If you find that the stack is over writing your bit variables or "near data"
7620 then the approach which best utilised the internal memory is to position
7621 the "near data" after the last bank of used registers or, if you use bit
7622 variables, after the last bit variable by using the ---data-loc, e.g.
7623 if two register banks are being used and no bit variables, ---data-loc
7624 16, and use the ---stack-after-data option.
7627 If bit variables are being used, another method would be to try and squeeze
7628 the data area in the unused register banks if it will fit, and start the
7629 stack after the last bit variable.
7632 Retargetting for other MCUs.
7635 The issues for retargetting the compiler are far too numerous to be covered
7637 What follows is a brief description of each of the seven phases of the
7638 compiler and its MCU dependency.
7641 Parsing the source and building the annotated parse tree.
7642 This phase is largely MCU independent (except for the language extensions).
7643 Syntax & semantic checks are also done in this phase, along with some initial
7644 optimizations like back patching labels and the pattern matching optimizations
7645 like bit-rotation etc.
7648 The second phase involves generating an intermediate code which can be easy
7649 manipulated during the later phases.
7650 This phase is entirely MCU independent.
7651 The intermediate code generation assumes the target machine has unlimited
7652 number of registers, and designates them with the name iTemp.
7653 The compiler can be made to dump a human readable form of the code generated
7654 by using the ---dumpraw option.
7657 This phase does the bulk of the standard optimizations and is also MCU independe
7659 This phase can be broken down into several sub-phases:
7663 Break down intermediate code (iCode) into basic blocks.
7665 Do control flow & data flow analysis on the basic blocks.
7667 Do local common subexpression elimination, then global subexpression elimination
7669 Dead code elimination
7673 If loop optimizations caused any changes then do 'global subexpression eliminati
7674 on' and 'dead code elimination' again.
7677 This phase determines the live-ranges; by live range I mean those iTemp
7678 variables defined by the compiler that still survive after all the optimization
7680 Live range analysis is essential for register allocation, since these computati
7681 on determines which of these iTemps will be assigned to registers, and for
7685 Phase five is register allocation.
7686 There are two parts to this process.
7690 The first part I call 'register packing' (for lack of a better term).
7691 In this case several MCU specific expression folding is done to reduce
7696 The second part is more MCU independent and deals with allocating registers
7697 to the remaining live ranges.
7698 A lot of MCU specific code does creep into this phase because of the limited
7699 number of index registers available in the 8051.
7702 The Code generation phase is (unhappily), entirely MCU dependent and very
7703 little (if any at all) of this code can be reused for other MCU.
7704 However the scheme for allocating a homogenized assembler operand for each
7705 iCode operand may be reused.
7708 As mentioned in the optimization section the peep-hole optimizer is rule
7709 based system, which can reprogrammed for other MCUs.
7712 SDCDB - Source Level Debugger
7715 SDCC is distributed with a source level debugger.
7716 The debugger uses a command line interface, the command repertoire of the
7717 debugger has been kept as close to gdb (the GNU debugger) as possible.
7718 The configuration and build process is part of the standard compiler installati
7719 on, which also builds and installs the debugger in the target directory
7720 specified during configuration.
7721 The debugger allows you debug BOTH at the C source and at the ASM source
7725 Compiling for Debugging
7730 debug option must be specified for all files for which debug information
7732 The complier generates a .cdb file for each of these files.
7733 The linker updates the .cdb file with the address information.
7734 This .cdb is used by the debugger.
7737 How the Debugger Works
7740 When the ---debug option is specified the compiler generates extra symbol
7741 information some of which are put into the the assembler source and some
7742 are put into the .cdb file, the linker updates the .cdb file with the address
7743 information for the symbols.
7744 The debugger reads the symbolic information generated by the compiler &
7745 the address information generated by the linker.
7746 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
7747 execution is controlled by the debugger.
7748 When a command is issued for the debugger, it translates it into appropriate
7749 commands for the simulator.
7752 Starting the Debugger
7755 The debugger can be started using the following command line.
7756 (Assume the file you are debugging has the file name foo).
7770 The debugger will look for the following files.
7773 foo.c - the source file.
7776 foo.cdb - the debugger symbol information file.
7779 foo.ihx - the intel hex format object file.
7782 Command Line Options.
7785 ---directory=<source file directory> this option can used to specify the
7786 directory search list.
7787 The debugger will look into the directory list specified for source, cdb
7789 The items in the directory list must be separated by ':', e.g.
7790 if the source files can be in the directories /home/src1 and /home/src2,
7791 the ---directory option should be ---directory=/home/src1:/home/src2.
7792 Note there can be no spaces in the option.
7796 -cd <directory> - change to the <directory>.
7799 -fullname - used by GUI front ends.
7802 -cpu <cpu-type> - this argument is passed to the simulator please see the
7803 simulator docs for details.
7806 -X <Clock frequency > this options is passed to the simulator please see
7807 the simulator docs for details.
7810 -s <serial port file> passed to simulator see the simulator docs for details.
7813 -S <serial in,out> passed to simulator see the simulator docs for details.
7819 As mention earlier the command interface for the debugger has been deliberately
7820 kept as close the GNU debugger gdb, as possible.
7821 This will help the integration with existing graphical user interfaces
7822 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
7823 \layout Subsubsection
7825 break [line | file:line | function | file:function]
7828 Set breakpoint at specified line or function:
7837 sdcdb>break foo.c:100
7841 sdcdb>break foo.c:funcfoo
7842 \layout Subsubsection
7844 clear [line | file:line | function | file:function ]
7847 Clear breakpoint at specified line or function:
7856 sdcdb>clear foo.c:100
7860 sdcdb>clear foo.c:funcfoo
7861 \layout Subsubsection
7866 Continue program being debugged, after breakpoint.
7867 \layout Subsubsection
7872 Execute till the end of the current function.
7873 \layout Subsubsection
7878 Delete breakpoint number 'n'.
7879 If used without any option clear ALL user defined break points.
7880 \layout Subsubsection
7882 info [break | stack | frame | registers ]
7885 info break - list all breakpoints
7888 info stack - show the function call stack.
7891 info frame - show information about the current execution frame.
7894 info registers - show content of all registers.
7895 \layout Subsubsection
7900 Step program until it reaches a different source line.
7901 \layout Subsubsection
7906 Step program, proceeding through subroutine calls.
7907 \layout Subsubsection
7912 Start debugged program.
7913 \layout Subsubsection
7918 Print type information of the variable.
7919 \layout Subsubsection
7924 print value of variable.
7925 \layout Subsubsection
7930 load the given file name.
7931 Note this is an alternate method of loading file for debugging.
7932 \layout Subsubsection
7937 print information about current frame.
7938 \layout Subsubsection
7943 Toggle between C source & assembly source.
7944 \layout Subsubsection
7949 Send the string following '!' to the simulator, the simulator response is
7951 Note the debugger does not interpret the command being sent to the simulator,
7952 so if a command like 'go' is sent the debugger can loose its execution
7953 context and may display incorrect values.
7954 \layout Subsubsection
7961 My name is Bobby Brown"
7964 Interfacing with XEmacs.
7967 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
7968 sdcdb.el and sdcdbsrc.el.
7969 These two files can be found in the $(prefix)/bin directory after the installat
7971 These files need to be loaded into XEmacs for the interface to work.
7972 This can be done at XEmacs startup time by inserting the following into
7973 your '.xemacs' file (which can be found in your HOME directory):
7979 (load-file sdcdbsrc.el)
7985 .xemacs is a lisp file so the () around the command is REQUIRED.
7986 The files can also be loaded dynamically while XEmacs is running, set the
7987 environment variable 'EMACSLOADPATH' to the installation bin directory
7988 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
7989 To start the interface enter the following command:
8003 You will prompted to enter the file name to be debugged.
8008 The command line options that are passed to the simulator directly are bound
8009 to default values in the file sdcdbsrc.el.
8010 The variables are listed below, these values maybe changed as required.
8013 sdcdbsrc-cpu-type '51
8016 sdcdbsrc-frequency '11059200
8022 The following is a list of key mapping for the debugger interface.
8030 ;; Current Listing ::
8047 binding\SpecialChar ~
8086 ------\SpecialChar ~
8126 sdcdb-next-from-src\SpecialChar ~
8152 sdcdb-back-from-src\SpecialChar ~
8178 sdcdb-cont-from-src\SpecialChar ~
8188 SDCDB continue command
8204 sdcdb-step-from-src\SpecialChar ~
8230 sdcdb-whatis-c-sexp\SpecialChar ~
8240 SDCDB ptypecommand for data at
8304 sdcdbsrc-delete\SpecialChar ~
8318 SDCDB Delete all breakpoints if no arg
8366 given or delete arg (C-u arg x)
8382 sdcdbsrc-frame\SpecialChar ~
8397 SDCDB Display current frame if no arg,
8446 given or display frame arg
8511 sdcdbsrc-goto-sdcdb\SpecialChar ~
8521 Goto the SDCDB output buffer
8537 sdcdb-print-c-sexp\SpecialChar ~
8548 SDCDB print command for data at
8612 sdcdbsrc-goto-sdcdb\SpecialChar ~
8622 Goto the SDCDB output buffer
8638 sdcdbsrc-mode\SpecialChar ~
8654 Toggles Sdcdbsrc mode (turns it off)
8658 ;; C-c C-f\SpecialChar ~
8666 sdcdb-finish-from-src\SpecialChar ~
8674 SDCDB finish command
8678 ;; C-x SPC\SpecialChar ~
8686 sdcdb-break\SpecialChar ~
8704 Set break for line with point
8706 ;; ESC t\SpecialChar ~
8716 sdcdbsrc-mode\SpecialChar ~
8732 Toggle Sdcdbsrc mode
8734 ;; ESC m\SpecialChar ~
8744 sdcdbsrc-srcmode\SpecialChar ~
8768 The Z80 and gbz80 port
8771 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
8772 The port is incomplete - long support is incomplete (mul, div and mod are
8773 unimplimented), and both float and bitfield support is missing.
8774 Apart from that the code generated is correct.
8777 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
8778 The stack frame is similar to that generated by the IAR Z80 compiler.
8779 IX is used as the base pointer, HL is used as a temporary register, and
8780 BC and DE are available for holding varibles.
8781 IY is currently unusued.
8782 Return values are stored in HL.
8783 One bad side effect of using IX as the base pointer is that a functions
8784 stack frame is limited to 127 bytes - this will be fixed in a later version.
8790 SDCC has grown to be a large project.
8791 The compiler alone (without the preprocessor, assembler and linker) is
8792 about 40,000 lines of code (blank stripped).
8793 The open source nature of this project is a key to its continued growth
8795 You gain the benefit and support of many active software developers and
8797 Is SDCC perfect? No, that's why we need your help.
8798 The developers take pride in fixing reported bugs.
8799 You can help by reporting the bugs and helping other SDCC users.
8800 There are lots of ways to contribute, and we encourage you to take part
8801 in making SDCC a great software package.
8807 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
8808 cc@sdcc.sourceforge.net'.
8809 Bugs will be fixed ASAP.
8810 When reporting a bug, it is very useful to include a small test program
8811 which reproduces the problem.
8812 If you can isolate the problem by looking at the generated assembly code,
8813 this can be very helpful.
8814 Compiling your program with the ---dumpall option can sometimes be useful
8815 in locating optimization problems.
8821 The anatomy of the compiler
8826 This is an excerpt from an atricle published in Circuit Cellar MagaZine
8828 It's a little outdated (the compiler is much more efficient now and user/devell
8829 oper friendly), but pretty well exposes the guts of it all.
8835 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
8836 It is fairly easy to retarget for other 8-bit MCU.
8837 Here we take a look at some of the internals of the compiler.
8844 Parsing the input source file and creating an AST (Annotated Syntax Tree).
8845 This phase also involves propagating types (annotating each node of the
8846 parse tree with type information) and semantic analysis.
8847 There are some MCU specific parsing rules.
8848 For example the storage classes, the extended storage classes are MCU specific
8849 while there may be a xdata storage class for 8051 there is no such storage
8850 class for z80 or Atmel AVR.
8851 SDCC allows MCU specific storage class extensions, i.e.
8852 xdata will be treated as a storage class specifier when parsing 8051 C
8853 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
8860 Intermediate code generation.
8861 In this phase the AST is broken down into three-operand form (iCode).
8862 These three operand forms are represented as doubly linked lists.
8863 ICode is the term given to the intermediate form generated by the compiler.
8864 ICode example section shows some examples of iCode generated for some simple
8871 Bulk of the target independent optimizations is performed in this phase.
8872 The optimizations include constant propagation, common sub-expression eliminati
8873 on, loop invariant code movement, strength reduction of loop induction variables
8874 and dead-code elimination.
8880 During intermediate code generation phase, the compiler assumes the target
8881 machine has infinite number of registers and generates a lot of temporary
8883 The live range computation determines the lifetime of each of these compiler-ge
8884 nerated temporaries.
8885 A picture speaks a thousand words.
8886 ICode example sections show the live range annotations for each of the
8888 It is important to note here, each iCode is assigned a number in the order
8889 of its execution in the function.
8890 The live ranges are computed in terms of these numbers.
8891 The from number is the number of the iCode which first defines the operand
8892 and the to number signifies the iCode which uses this operand last.
8898 The register allocation determines the type and number of registers needed
8900 In most MCUs only a few registers can be used for indirect addressing.
8901 In case of 8051 for example the registers R0 & R1 can be used to indirectly
8902 address the internal ram and DPTR to indirectly address the external ram.
8903 The compiler will try to allocate the appropriate register to pointer variables
8905 ICode example section shows the operands annotated with the registers assigned
8907 The compiler will try to keep operands in registers as much as possible;
8908 there are several schemes the compiler uses to do achieve this.
8909 When the compiler runs out of registers the compiler will check to see
8910 if there are any live operands which is not used or defined in the current
8911 basic block being processed, if there are any found then it will push that
8912 operand and use the registers in this block, the operand will then be popped
8913 at the end of the basic block.
8917 There are other MCU specific considerations in this phase.
8918 Some MCUs have an accumulator; very short-lived operands could be assigned
8919 to the accumulator instead of general-purpose register.
8925 Figure II gives a table of iCode operations supported by the compiler.
8926 The code generation involves translating these operations into corresponding
8927 assembly code for the processor.
8928 This sounds overly simple but that is the essence of code generation.
8929 Some of the iCode operations are generated on a MCU specific manner for
8930 example, the z80 port does not use registers to pass parameters so the
8931 SEND and RECV iCode operations will not be generated, and it also does
8932 not support JUMPTABLES.
8939 <Where is Figure II ?>
8945 This section shows some details of iCode.
8946 The example C code does not do anything useful; it is used as an example
8947 to illustrate the intermediate code generated by the compiler.
8960 /* This function does nothing useful.
8967 for the purpose of explaining iCode */
8970 short function (data int *x)
8978 short i=10; /* dead initialization eliminated */
8983 short sum=10; /* dead initialization eliminated */
8996 while (*x) *x++ = *p++;
9010 /* compiler detects i,j to be induction variables */
9014 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
9026 mul += i * 3; /* this multiplication remains */
9032 gint += j * 3;/* this multiplication changed to addition */
9049 In addition to the operands each iCode contains information about the filename
9050 and line it corresponds to in the source file.
9051 The first field in the listing should be interpreted as follows:
9056 Filename(linenumber: iCode Execution sequence number : ICode hash table
9057 key : loop depth of the iCode).
9062 Then follows the human readable form of the ICode operation.
9063 Each operand of this triplet form can be of three basic types a) compiler
9064 generated temporary b) user defined variable c) a constant value.
9065 Note that local variables and parameters are replaced by compiler generated
9067 Live ranges are computed only for temporaries (i.e.
9068 live ranges are not computed for global variables).
9069 Registers are allocated for temporaries only.
9070 Operands are formatted in the following manner:
9075 Operand Name [lr live-from : live-to ] { type information } [ registers
9081 As mentioned earlier the live ranges are computed in terms of the execution
9082 sequence number of the iCodes, for example
9084 the iTemp0 is live from (i.e.
9085 first defined in iCode with execution sequence number 3, and is last used
9086 in the iCode with sequence number 5).
9087 For induction variables such as iTemp21 the live range computation extends
9088 the lifetime from the start to the end of the loop.
9090 The register allocator used the live range information to allocate registers,
9091 the same registers may be used for different temporaries if their live
9092 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
9093 iTemp17 since their live ranges do not overlap.
9094 In addition the allocator also takes into consideration the type and usage
9095 of a temporary, for example itemp6 is a pointer to near space and is used
9096 as to fetch data from (i.e.
9097 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
9098 Some short lived temporaries are allocated to special registers which have
9099 meaning to the code generator e.g.
9100 iTemp13 is allocated to a pseudo register CC which tells the back end that
9101 the temporary is used only for a conditional jump the code generation makes
9102 use of this information to optimize a compare and jump ICode.
9104 There are several loop optimizations performed by the compiler.
9105 It can detect induction variables iTemp21(i) and iTemp23(j).
9106 Also note the compiler does selective strength reduction, i.e.
9107 the multiplication of an induction variable in line 18 (gint = j * 3) is
9108 changed to addition, a new temporary iTemp17 is allocated and assigned
9109 a initial value, a constant 3 is then added for each iteration of the loop.
9110 The compiler does not change the multiplication in line 17 however since
9111 the processor does support an 8 * 8 bit multiplication.
9113 Note the dead code elimination optimization eliminated the dead assignments
9114 in line 7 & 8 to I and sum respectively.
9121 Sample.c (5:1:0:0) _entry($9) :
9126 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
9131 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
9136 Sample.c(11:4:53:0) preHeaderLbl0($11) :
9141 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
9147 Sample.c(11:6:5:1) _whilecontinue_0($1) :
9152 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
9158 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9163 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
9169 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
9175 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
9181 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
9187 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
9188 * int}[r0] + 0x2 {short}
9193 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
9198 Sample.c(11:17:21:0)_whilebreak_0($3) :
9203 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9208 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9213 Sample.c(15:20:54:0)preHeaderLbl1($13) :
9218 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9223 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9228 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9233 Sample.c(15:24:26:1)_forcond_0($4) :
9238 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
9244 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9249 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
9250 + ITemp21 [lr21:38]{short}[r4]
9255 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
9261 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
9262 + iTemp15 [lr29:30]{short}[r1]
9267 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
9273 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
9279 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
9285 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
9291 Sample.c(19:38:47:1) goto _forcond_0($4)
9296 Sample.c(19:39:48:0)_forbreak_0($7) :
9301 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
9302 + ITemp11 [lr19:40]{short}[r3]
9307 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
9312 Sample.c(20:42:51:0)_return($8) :
9317 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
9323 Finally the code generated for this function:
9364 ; ----------------------------------------------
9374 ; ----------------------------------------------
9384 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
9396 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
9408 ;_whilecontinue_0($1) :
9418 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
9423 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9482 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
9501 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
9548 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
9588 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
9614 ; iTemp6 [lr5:16]{_near * int}[r0] =
9619 ; iTemp6 [lr5:16]{_near * int}[r0] +
9636 ; goto _whilecontinue_0($1)
9648 ; _whilebreak_0($3) :
9658 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9670 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9682 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9694 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9713 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9742 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
9747 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9792 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
9797 ; iTemp21 [lr21:38]{short}[r4]
9823 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
9856 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
9861 ; iTemp15 [lr29:30]{short}[r1]
9880 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
9927 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
9974 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
9986 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
10000 cjne r5,#0xff,00104$
10012 ; goto _forcond_0($4)
10024 ; _forbreak_0($7) :
10034 ; ret iTemp24 [lr40:41]{short}
10077 A few words about basic block successors, predecessors and dominators
10080 Successors are basic blocks that might execute after this basic block.
10082 Predecessors are basic blocks that might execute before reaching this basic
10085 Dominators are basic blocks that WILL execute before reaching this basic
10111 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
10114 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
10117 c) domVect of [BB4] = BB1 ...
10118 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
10126 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
10136 Thanks to all the other volunteer developers who have helped with coding,
10137 testing, web-page creation, distribution sets, etc.
10138 You know who you are :-)
10145 This document was initially written by Sandeep Dutta
10148 All product names mentioned herein may be trademarks of their respective
10154 \begin_inset LatexCommand \printindex{}