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">
675 <lyxtabular version="3" rows="2" columns="3">
677 <column alignment="left" valignment="top" leftline="true" width="1.6in">
678 <column alignment="center" valignment="top" leftline="true" width="0pt">
679 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
680 <row topline="true" bottomline="true">
681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
697 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
706 <row topline="true" bottomline="true">
707 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
724 /usr/local/share/sdcc/include
727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
753 is auto-appended by the compiler, e.g.
754 small, large, z80, ds390 etc.)
759 <lyxtabular version="3" rows="2" columns="3">
761 <column alignment="left" valignment="top" leftline="true" width="0pt">
762 <column alignment="left" valignment="top" leftline="true" width="0pt">
763 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
764 <row topline="true" bottomline="true">
765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
790 <row topline="true" bottomline="true">
791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
798 $DATADIR/$LIB_DIR_SUFFIX
801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
806 /usr/local/share/sdcc/lib
809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
836 <lyxtabular version="3" rows="2" columns="3">
838 <column alignment="left" valignment="top" leftline="true" width="0pt">
839 <column alignment="left" valignment="top" leftline="true" width="0pt">
840 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
841 <row topline="true" bottomline="true">
842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
858 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
867 <row topline="true" bottomline="true">
868 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
885 /usr/local/share/sdcc/doc
888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
911 Some search paths or parts of them are determined by configure variables
916 , see section above).
917 Other search paths are determined by environment variables during runtime.
920 The paths searched when running the compiler are as follows (the first catch
926 Binary files (preprocessor, assembler and linker)
930 <lyxtabular version="3" rows="4" columns="3">
932 <column alignment="left" valignment="top" leftline="true" width="0pt">
933 <column alignment="left" valignment="top" leftline="true" width="0pt">
934 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
935 <row topline="true" bottomline="true">
936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
962 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
980 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
997 Path of argv[0] (if available)
1000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1017 <row topline="true" bottomline="true">
1018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1055 \begin_inset Tabular
1056 <lyxtabular version="3" rows="6" columns="3">
1058 <column alignment="left" valignment="top" leftline="true" width="1.5in">
1059 <column alignment="left" valignment="top" leftline="true" width="1.5in">
1060 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
1061 <row topline="true" bottomline="true">
1062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1087 <row topline="true">
1088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1113 <row topline="true">
1114 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1139 <row topline="true">
1140 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1154 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1177 <row topline="true">
1178 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1196 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1204 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1217 <row topline="true" bottomline="true">
1218 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1230 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1257 The option ---nostdinc disables the last two search paths.
1266 is auto-appended by the compiler, e.g.
1267 small, large, z80, ds390 etc.)
1270 \begin_inset Tabular
1271 <lyxtabular version="3" rows="6" columns="3">
1273 <column alignment="left" valignment="top" leftline="true" width="1.7in">
1274 <column alignment="left" valignment="top" leftline="true" width="1.6in">
1275 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
1276 <row topline="true" bottomline="true">
1277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1302 <row topline="true">
1303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1311 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1328 <row topline="true">
1329 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1368 <row topline="true">
1369 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1389 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1405 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1422 <row topline="true">
1423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1447 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1455 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1474 <row topline="true" bottomline="true">
1475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1493 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1498 /usr/local/share/sdcc/
1507 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1534 The option ---nostdlib disables the last two search paths.
1538 So, for windoze it is highly recommended to set the environment variable
1539 SDCC_HOME to prevent needless usage of -I and -L options.
1540 For *nix-builds SDCC_HOME should only be set when sdcc is installed in
1544 Linux and other gcc-based systems (cygwin, mingw32, osx)
1549 Download the source package
1551 either from the SDCC CVS repository or from the
1552 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1558 , it will be named something like sdcc
1571 Bring up a command line terminal, such as xterm.
1576 Unpack the file using a command like:
1579 "tar -xzf sdcc.src.tar.gz
1584 , this will create a sub-directory called sdcc with all of the sources.
1587 Change directory into the main SDCC directory, for example type:
1604 This configures the package for compilation on your system.
1620 All of the source packages will compile, this can take a while.
1636 This copies the binary executables, the include files, the libraries and
1637 the documentation to the install directories.
1641 \layout Subsubsection
1643 Windows Install Using a Binary Package
1646 Download the binary package and unpack it using your favorite unpacking
1647 tool (gunzip, WinZip, etc).
1648 This should unpack to a group of sub-directories.
1649 An example directory structure after unpacking the mingw32 package is:
1656 bin for the executables, c:
1676 lib for the include and libraries.
1679 Adjust your environment variable PATH to include the location of the bin
1680 directory or start sdcc using the full path.
1681 \layout Subsubsection
1683 Windows Install Using Cygwin and Mingw32
1686 Follow the instruction in
1688 Linux and other gcc-based systems
1691 \layout Subsubsection
1693 Windows Install Using Microsoft Visual C++ 6.0/NET
1698 Download the source package
1700 either from the SDCC CVS repository or from the
1701 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
1707 , it will be named something like sdcc
1714 SDCC is distributed with all the projects, workspaces, and files you need
1715 to build it using Visual C++ 6.0/NET.
1716 The workspace name is 'sdcc.dsw'.
1717 Please note that as it is now, all the executables are created in a folder
1721 Once built you need to copy the executables from sdcc
1725 bin before runnng SDCC.
1730 In order to build SDCC with Visual C++ 6.0/NET you need win32 executables
1731 of bison.exe, flex.exe, and gawk.exe.
1732 One good place to get them is
1733 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
1741 Download the file UnxUtils.zip.
1742 Now you have to install the utilities and setup Visual C++ so it can locate
1743 the required programs.
1744 Here there are two alternatives (choose one!):
1751 a) Extract UnxUtils.zip to your C:
1753 hard disk PRESERVING the original paths, otherwise bison won't work.
1754 (If you are using WinZip make certain that 'Use folder names' is selected)
1758 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
1759 in 'Show directories for:' select 'Executable files', and in the directories
1760 window add a new path: 'C:
1770 (As a side effect, you get a bunch of Unix utilities that could be useful,
1771 such as diff and patch.)
1778 This one avoids extracting a bunch of files you may not use, but requires
1783 a) Create a directory were to put the tools needed, or use a directory already
1791 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
1792 to such directory WITHOUT preserving the original paths.
1793 (If you are using WinZip make certain that 'Use folder names' is not selected)
1797 c) Rename bison.exe to '_bison.exe'.
1801 d) Create a batch file 'bison.bat' in 'C:
1805 ' and add these lines:
1825 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
1829 Steps 'c' and 'd' are needed because bison requires by default that the
1830 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
1831 '/usr/local/share/' I think.
1832 So it is necessary to tell bison where those files are located if they
1833 are not in such directory.
1834 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
1838 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
1839 in 'Show directories for:' select 'Executable files', and in the directories
1840 window add a new path: 'c:
1843 Note that you can use any other path instead of 'c:
1845 util', even the path where the Visual C++ tools are, probably: 'C:
1849 Microsoft Visual Studio
1854 So you don't have to execute step 'e' :)
1858 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
1859 the executables from sdcc
1863 bin, and you can compile using sdcc.
1864 \layout Subsubsection
1866 Windows Install Using Borland
1869 From the sdcc directory, run the command "make -f Makefile.bcc".
1870 This should regenerate all the .exe files in the bin directory except for
1871 sdcdb.exe (which currently doesn't build under Borland C++).
1874 If you modify any source files and need to rebuild, be aware that the dependanci
1875 es may not be correctly calculated.
1876 The safest option is to delete all .obj files and run the build again.
1877 From a Cygwin BASH prompt, this can easily be done with the commmand:
1887 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
1889 ) -print -exec rm {}
1898 or on Windows NT/2000/XP from the command prompt with the commmand:
1905 del /s *.obj *.lib *.rul
1908 from the sdcc directory.
1911 Testing out the SDCC Compiler
1914 The first thing you should do after installing your SDCC compiler is to
1922 at the prompt, and the program should run and tell you the version.
1923 If it doesn't run, or gives a message about not finding sdcc program, then
1924 you need to check over your installation.
1925 Make sure that the sdcc bin directory is in your executable search path
1926 defined by the PATH environment setting (see the Trouble-shooting section
1928 Make sure that the sdcc program is in the bin folder, if not perhaps something
1929 did not install correctly.
1937 is commonly installed as described in section
1938 \begin_inset Quotes sld
1941 Install and search paths
1942 \begin_inset Quotes srd
1951 Make sure the compiler works on a very simple example.
1952 Type in the following test.c program using your favorite
1987 Compile this using the following command:
1996 If all goes well, the compiler will generate a test.asm and test.rel file.
1997 Congratulations, you've just compiled your first program with SDCC.
1998 We used the -c option to tell SDCC not to link the generated code, just
1999 to keep things simple for this step.
2007 The next step is to try it with the linker.
2017 If all goes well the compiler will link with the libraries and produce
2018 a test.ihx output file.
2023 (no test.ihx, and the linker generates warnings), then the problem is most
2024 likely that sdcc cannot find the
2028 usr/local/share/sdcc/lib directory
2032 (see the Install trouble-shooting section for suggestions).
2040 The final test is to ensure sdcc can use the
2044 header files and libraries.
2045 Edit test.c and change it to the following:
2065 strcpy(str1, "testing");
2074 Compile this by typing
2081 This should generate a test.ihx output file, and it should give no warnings
2082 such as not finding the string.h file.
2083 If it cannot find the string.h file, then the problem is that sdcc cannot
2084 find the /usr/local/share/sdcc/include directory
2088 (see the Install trouble-shooting section for suggestions).
2091 Install Trouble-shooting
2092 \layout Subsubsection
2094 SDCC does not build correctly.
2097 A thing to try is starting from scratch by unpacking the .tgz source package
2098 again in an empty directory.
2106 ./configure 2>&1 | tee configure.log
2120 make 2>&1 | tee make.log
2127 If anything goes wrong, you can review the log files to locate the problem.
2128 Or a relevant part of this can be attached to an email that could be helpful
2129 when requesting help from the mailing list.
2130 \layout Subsubsection
2133 \begin_inset Quotes sld
2137 \begin_inset Quotes srd
2144 \begin_inset Quotes sld
2148 \begin_inset Quotes srd
2151 command is a script that analyzes your system and performs some configuration
2152 to ensure the source package compiles on your system.
2153 It will take a few minutes to run, and will compile a few tests to determine
2154 what compiler features are installed.
2155 \layout Subsubsection
2158 \begin_inset Quotes sld
2162 \begin_inset Quotes srd
2168 This runs the GNU make tool, which automatically compiles all the source
2169 packages into the final installed binary executables.
2170 \layout Subsubsection
2173 \begin_inset Quotes sld
2177 \begin_inset Quotes erd
2183 This will install the compiler, other executables libraries and include
2184 files in to the appropriate directories.
2186 \begin_inset Quotes sld
2189 Install and Search PATHS
2190 \begin_inset Quotes srd
2195 On most systems you will need super-user privilages to do this.
2201 SDCC is not just a compiler, but a collection of tools by various developers.
2202 These include linkers, assemblers, simulators and other components.
2203 Here is a summary of some of the components.
2204 Note that the included simulator and assembler have separate documentation
2205 which you can find in the source package in their respective directories.
2206 As SDCC grows to include support for other processors, other packages from
2207 various developers are included and may have their own sets of documentation.
2211 You might want to look at the files which are installed in <installdir>.
2212 At the time of this writing, we find the following programs for gcc-builds:
2216 In <installdir>/bin:
2219 sdcc - The compiler.
2222 sdcpp - The C preprocessor.
2225 asx8051 - The assembler for 8051 type processors.
2232 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
2235 aslink -The linker for 8051 type processors.
2242 link-gbz80 - The Z80 and GameBoy Z80 linkers.
2245 s51 - The ucSim 8051 simulator.
2248 sdcdb - The source debugger.
2251 packihx - A tool to pack (compress) Intel hex files.
2254 In <installdir>/share/sdcc/include
2260 In <installdir>/share/sdcc/lib
2263 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
2267 In <installdir>/share/sdcc/doc
2273 As development for other processors proceeds, this list will expand to include
2274 executables to support processors like AVR, PIC, etc.
2275 \layout Subsubsection
2280 This is the actual compiler, it in turn uses the c-preprocessor and invokes
2281 the assembler and linkage editor.
2282 \layout Subsubsection
2284 sdcpp - The C-Preprocessor
2287 The preprocessor is a modified version of the GNU preprocessor.
2288 The C preprocessor is used to pull in #include sources, process #ifdef
2289 statements, #defines and so on.
2290 \layout Subsubsection
2292 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
2296 This is retargettable assembler & linkage editor, it was developed by Alan
2298 John Hartman created the version for 8051, and I (Sandeep) have made some
2299 enhancements and bug fixes for it to work properly with the SDCC.
2300 \layout Subsubsection
2305 S51 is a freeware, opensource simulator developed by Daniel Drotos (
2306 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
2311 The simulator is built as part of the build process.
2312 For more information visit Daniel's website at:
2313 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
2318 It currently support the core mcs51, the Dallas DS80C390 and the Philips
2320 \layout Subsubsection
2322 sdcdb - Source Level Debugger
2328 <todo: is this thing still alive?>
2335 Sdcdb is the companion source level debugger.
2336 The current version of the debugger uses Daniel's Simulator S51, but can
2337 be easily changed to use other simulators.
2344 \layout Subsubsection
2346 Single Source File Projects
2349 For single source file 8051 projects the process is very simple.
2350 Compile your programs with the following command
2353 "sdcc sourcefile.c".
2357 This will compile, assemble and link your source file.
2358 Output files are as follows
2362 sourcefile.asm - Assembler source file created by the compiler
2364 sourcefile.lst - Assembler listing file created by the Assembler
2366 sourcefile.rst - Assembler listing file updated with linkedit information,
2367 created by linkage editor
2369 sourcefile.sym - symbol listing for the sourcefile, created by the assembler
2371 sourcefile.rel - Object file created by the assembler, input to Linkage editor
2373 sourcefile.map - The memory map for the load module, created by the Linker
2375 sourcefile.ihx - The load module in Intel hex format (you can select the
2376 Motorola S19 format with ---out-fmt-s19)
2378 sourcefile.cdb - An optional file (with ---debug) containing debug information
2380 sourcefile.dump* - Dump file to debug the compiler it self (with ---dumpall)
2382 \begin_inset Quotes sld
2385 Anatomy of the compiler
2386 \begin_inset Quotes srd
2390 \layout Subsubsection
2392 Projects with Multiple Source Files
2395 SDCC can compile only ONE file at a time.
2396 Let us for example assume that you have a project containing the following
2401 foo1.c (contains some functions)
2403 foo2.c (contains some more functions)
2405 foomain.c (contains more functions and the function main)
2413 The first two files will need to be compiled separately with the commands:
2445 Then compile the source file containing the
2449 function and link the files together with the following command:
2457 foomain.c\SpecialChar ~
2458 foo1.rel\SpecialChar ~
2470 can be separately compiled as well:
2481 sdcc foomain.rel foo1.rel foo2.rel
2488 The file containing the
2503 file specified in the command line, since the linkage editor processes
2504 file in the order they are presented to it.
2505 \layout Subsubsection
2507 Projects with Additional Libraries
2510 Some reusable routines may be compiled into a library, see the documentation
2511 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
2517 Libraries created in this manner can be included in the command line.
2518 Make sure you include the -L <library-path> option to tell the linker where
2519 to look for these files if they are not in the current directory.
2520 Here is an example, assuming you have the source file
2532 (if that is not the same as your current project):
2539 sdcc foomain.c foolib.lib -L mylib
2550 must be an absolute path name.
2554 The most efficient way to use libraries is to keep seperate modules in seperate
2556 The lib file now should name all the modules.rel files.
2557 For an example see the standard library file
2561 in the directory <installdir>/share/lib/small.
2564 Command Line Options
2565 \layout Subsubsection
2567 Processor Selection Options
2569 \labelwidthstring 00.00.0000
2575 Generate code for the MCS51 (8051) family of processors.
2576 This is the default processor target.
2578 \labelwidthstring 00.00.0000
2584 Generate code for the DS80C390 processor.
2586 \labelwidthstring 00.00.0000
2592 Generate code for the Z80 family of processors.
2594 \labelwidthstring 00.00.0000
2600 Generate code for the GameBoy Z80 processor.
2602 \labelwidthstring 00.00.0000
2608 Generate code for the Atmel AVR processor (In development, not complete).
2610 \labelwidthstring 00.00.0000
2616 Generate code for the PIC 14-bit processors (In development, not complete).
2618 \labelwidthstring 00.00.0000
2624 Generate code for the Toshiba TLCS-900H processor (In development, not
2627 \labelwidthstring 00.00.0000
2633 Generate code for the Philips XA51 processor (In development, not complete).
2634 \layout Subsubsection
2636 Preprocessor Options
2638 \labelwidthstring 00.00.0000
2644 The additional location where the pre processor will look for <..h> or
2645 \begin_inset Quotes eld
2649 \begin_inset Quotes erd
2654 \labelwidthstring 00.00.0000
2660 Command line definition of macros.
2661 Passed to the pre processor.
2663 \labelwidthstring 00.00.0000
2669 Tell the preprocessor to output a rule suitable for make describing the
2670 dependencies of each object file.
2671 For each source file, the preprocessor outputs one make-rule whose target
2672 is the object file name for that source file and whose dependencies are
2673 all the files `#include'd in it.
2674 This rule may be a single line or may be continued with `
2676 '-newline if it is long.
2677 The list of rules is printed on standard output instead of the preprocessed
2681 \labelwidthstring 00.00.0000
2687 Tell the preprocessor not to discard comments.
2688 Used with the `-E' option.
2690 \labelwidthstring 00.00.0000
2701 Like `-M' but the output mentions only the user header files included with
2703 \begin_inset Quotes eld
2707 System header files included with `#include <file>' are omitted.
2709 \labelwidthstring 00.00.0000
2715 Assert the answer answer for question, in case it is tested with a preprocessor
2716 conditional such as `#if #question(answer)'.
2717 `-A-' disables the standard assertions that normally describe the target
2720 \labelwidthstring 00.00.0000
2726 (answer) Assert the answer answer for question, in case it is tested with
2727 a preprocessor conditional such as `#if #question(answer)'.
2728 `-A-' disables the standard assertions that normally describe the target
2731 \labelwidthstring 00.00.0000
2737 Undefine macro macro.
2738 `-U' options are evaluated after all `-D' options, but before any `-include'
2739 and `-imacros' options.
2741 \labelwidthstring 00.00.0000
2747 Tell the preprocessor to output only a list of the macro definitions that
2748 are in effect at the end of preprocessing.
2749 Used with the `-E' option.
2751 \labelwidthstring 00.00.0000
2757 Tell the preprocessor to pass all macro definitions into the output, in
2758 their proper sequence in the rest of the output.
2760 \labelwidthstring 00.00.0000
2771 Like `-dD' except that the macro arguments and contents are omitted.
2772 Only `#define name' is included in the output.
2773 \layout Subsubsection
2777 \labelwidthstring 00.00.0000
2787 <absolute path to additional libraries> This option is passed to the linkage
2788 editor's additional libraries search path.
2789 The path name must be absolute.
2790 Additional library files may be specified in the command line.
2791 See section Compiling programs for more details.
2793 \labelwidthstring 00.00.0000
2799 <Value> The start location of the external ram, default value is 0.
2800 The value entered can be in Hexadecimal or Decimal format, e.g.: ---xram-loc
2801 0x8000 or ---xram-loc 32768.
2803 \labelwidthstring 00.00.0000
2809 <Value> The start location of the code segment, default value 0.
2810 Note when this option is used the interrupt vector table is also relocated
2811 to the given address.
2812 The value entered can be in Hexadecimal or Decimal format, e.g.: ---code-loc
2813 0x8000 or ---code-loc 32768.
2815 \labelwidthstring 00.00.0000
2821 <Value> By default the stack is placed after the data segment.
2822 Using this option the stack can be placed anywhere in the internal memory
2824 The value entered can be in Hexadecimal or Decimal format, e.g.
2825 ---stack-loc 0x20 or ---stack-loc 32.
2826 Since the sp register is incremented before a push or call, the initial
2827 sp will be set to one byte prior the provided value.
2828 The provided value should not overlap any other memory areas such as used
2829 register banks or the data segment and with enough space for the current
2832 \labelwidthstring 00.00.0000
2838 <Value> The start location of the internal ram data segment.
2839 The value entered can be in Hexadecimal or Decimal format, eg.
2840 ---data-loc 0x20 or ---data-loc 32.
2841 (By default, the start location of the internal ram data segment is set
2842 as low as possible in memory, taking into account the used register banks
2843 and the bit segment at address 0x20.
2844 For example if register banks 0 and 1 are used without bit variables, the
2845 data segment will be set, if ---data-loc is not used, to location 0x10.)
2847 \labelwidthstring 00.00.0000
2853 <Value> The start location of the indirectly addressable internal ram, default
2855 The value entered can be in Hexadecimal or Decimal format, eg.
2856 ---idata-loc 0x88 or ---idata-loc 136.
2858 \labelwidthstring 00.00.0000
2867 The linker output (final object code) is in Intel Hex format.
2868 (This is the default option).
2870 \labelwidthstring 00.00.0000
2879 The linker output (final object code) is in Motorola S19 format.
2880 \layout Subsubsection
2884 \labelwidthstring 00.00.0000
2890 Generate code for Large model programs see section Memory Models for more
2892 If this option is used all source files in the project should be compiled
2894 In addition the standard library routines are compiled with small model,
2895 they will need to be recompiled.
2897 \labelwidthstring 00.00.0000
2908 Generate code for Small Model programs see section Memory Models for more
2910 This is the default model.
2911 \layout Subsubsection
2915 \labelwidthstring 00.00.0000
2926 Generate 24-bit flat mode code.
2927 This is the one and only that the ds390 code generator supports right now
2928 and is default when using
2933 See section Memory Models for more details.
2935 \labelwidthstring 00.00.0000
2941 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
2942 This is the one and only that the ds390 code generator supports right now
2943 and is default when using
2948 In this mode, the stack is located in the lower 1K of the internal RAM,
2949 which is mapped to 0x400000.
2950 Note that the support is incomplete, since it still uses a single byte
2951 as the stack pointer.
2952 This means that only the lower 256 bytes of the potential 1K stack space
2953 will actually be used.
2954 However, this does allow you to reclaim the precious 256 bytes of low RAM
2955 for use for the DATA and IDATA segments.
2956 The compiler will not generate any code to put the processor into 10 bit
2958 It is important to ensure that the processor is in this mode before calling
2959 any re-entrant functions compiled with this option.
2960 In principle, this should work with the
2964 option, but that has not been tested.
2965 It is incompatible with the
2970 It also only makes sense if the processor is in 24 bit contiguous addressing
2973 ---model-flat24 option
2976 \layout Subsubsection
2978 Optimization Options
2980 \labelwidthstring 00.00.0000
2986 Will not do global subexpression elimination, this option may be used when
2987 the compiler creates undesirably large stack/data spaces to store compiler
2989 A warning message will be generated when this happens and the compiler
2990 will indicate the number of extra bytes it allocated.
2991 It recommended that this option NOT be used, #pragma\SpecialChar ~
2993 to turn off global subexpression elimination for a given function only.
2995 \labelwidthstring 00.00.0000
3001 Will not do loop invariant optimizations, this may be turned off for reasons
3002 explained for the previous option.
3003 For more details of loop optimizations performed see section Loop Invariants.It
3004 recommended that this option NOT be used, #pragma\SpecialChar ~
3005 NOINVARIANT can be used
3006 to turn off invariant optimizations for a given function only.
3008 \labelwidthstring 00.00.0000
3014 Will not do loop induction optimizations, see section strength reduction
3015 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
3017 ION can be used to turn off induction optimizations for a given function
3020 \labelwidthstring 00.00.0000
3031 Will not generate boundary condition check when switch statements are implement
3032 ed using jump-tables.
3033 See section Switch Statements for more details.
3034 It is recommended that this option is NOT used, #pragma\SpecialChar ~
3036 used to turn off boundary checking for jump tables for a given function
3039 \labelwidthstring 00.00.0000
3048 Will not do loop reversal optimization.
3050 \labelwidthstring 00.00.0000
3056 Will not optimize labels (makes the dumpfiles more readable).
3058 \labelwidthstring 00.00.0000
3064 Will not memcpy initialized data in far space from code space.
3065 This saves a few bytes in code space if you don't have initialized data.
3066 \layout Subsubsection
3070 \labelwidthstring 00.00.0000
3077 will compile and assemble the source, but will not call the linkage editor.
3079 \labelwidthstring 00.00.0000
3085 reads the preprocessed source from standard input and compiles it.
3086 The file name for the assembler output must be specified using the -o option.
3088 \labelwidthstring 00.00.0000
3094 Run only the C preprocessor.
3095 Preprocess all the C source files specified and output the results to standard
3098 \labelwidthstring 00.00.0000
3105 The output path resp.
3106 file where everything will be placed.
3107 If the parameter is a path, it must have a trailing slash (or backslash
3108 for the Windows binaries) to be recognized as a path.
3111 \labelwidthstring 00.00.0000
3122 All functions in the source file will be compiled as
3127 the parameters and local variables will be allocated on the stack.
3128 see section Parameters and Local Variables for more details.
3129 If this option is used all source files in the project should be compiled
3133 \labelwidthstring 00.00.0000
3139 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
3140 variables and passing parameters.
3141 See section on external stack for more details.
3143 \labelwidthstring 00.00.0000
3147 ---callee-saves function1[,function2][,function3]....
3150 The compiler by default uses a caller saves convention for register saving
3151 across function calls, however this can cause unneccessary register pushing
3152 & popping when calling small functions from larger functions.
3153 This option can be used to switch the register saving convention for the
3154 function names specified.
3155 The compiler will not save registers when calling these functions, no extra
3156 code will be generated at the entry & exit for these functions to save
3157 & restore the registers used by these functions, this can SUBSTANTIALLY
3158 reduce code & improve run time performance of the generated code.
3159 In the future the compiler (with interprocedural analysis) will be able
3160 to determine the appropriate scheme to use for each function call.
3161 DO NOT use this option for built-in functions such as _muluint..., if this
3162 option is used for a library function the appropriate library function
3163 needs to be recompiled with the same option.
3164 If the project consists of multiple source files then all the source file
3165 should be compiled with the same ---callee-saves option string.
3166 Also see #pragma\SpecialChar ~
3169 \labelwidthstring 00.00.0000
3178 When this option is used the compiler will generate debug information, that
3179 can be used with the SDCDB.
3180 The debug information is collected in a file with .cdb extension.
3181 For more information see documentation for SDCDB.
3183 \labelwidthstring 00.00.0000
3189 <filename> This option can be used to use additional rules to be used by
3190 the peep hole optimizer.
3191 See section Peep Hole optimizations for details on how to write these rules.
3193 \labelwidthstring 00.00.0000
3204 Stop after the stage of compilation proper; do not assemble.
3205 The output is an assembler code file for the input file specified.
3207 \labelwidthstring 00.00.0000
3211 -Wa_asmOption[,asmOption]
3214 Pass the asmOption to the assembler.
3216 \labelwidthstring 00.00.0000
3220 -Wl_linkOption[,linkOption]
3223 Pass the linkOption to the linker.
3225 \labelwidthstring 00.00.0000
3234 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
3235 Note by default these libraries are compiled as non-reentrant.
3236 See section Installation for more details.
3238 \labelwidthstring 00.00.0000
3247 This option will cause the compiler to generate an information message for
3248 each function in the source file.
3249 The message contains some
3253 information about the function.
3254 The number of edges and nodes the compiler detected in the control flow
3255 graph of the function, and most importantly the
3257 cyclomatic complexity
3259 see section on Cyclomatic Complexity for more details.
3261 \labelwidthstring 00.00.0000
3270 Floating point library is compiled as reentrant.See section Installation
3273 \labelwidthstring 00.00.0000
3279 The compiler will not overlay parameters and local variables of any function,
3280 see section Parameters and local variables for more details.
3282 \labelwidthstring 00.00.0000
3288 This option can be used when the code generated is called by a monitor
3290 The compiler will generate a 'ret' upon return from the 'main' function.
3291 The default option is to lock up i.e.
3294 \labelwidthstring 00.00.0000
3300 Disable peep-hole optimization.
3302 \labelwidthstring 00.00.0000
3308 Pass the inline assembler code through the peep hole optimizer.
3309 This can cause unexpected changes to inline assembler code, please go through
3310 the peephole optimizer rules defined in the source file tree '<target>/peeph.def
3311 ' before using this option.
3313 \labelwidthstring 00.00.0000
3319 <Value> Causes the linker to check if the internal ram usage is within limits
3322 \labelwidthstring 00.00.0000
3328 <Value> Causes the linker to check if the external ram usage is within limits
3331 \labelwidthstring 00.00.0000
3337 <Value> Causes the linker to check if the code usage is within limits of
3340 \labelwidthstring 00.00.0000
3346 This will prevent the compiler from passing on the default include path
3347 to the preprocessor.
3349 \labelwidthstring 00.00.0000
3355 This will prevent the compiler from passing on the default library path
3358 \labelwidthstring 00.00.0000
3364 Shows the various actions the compiler is performing.
3366 \labelwidthstring 00.00.0000
3372 Shows the actual commands the compiler is executing.
3374 \labelwidthstring 00.00.0000
3380 Hides your ugly and inefficient c-code from the asm file, so you can always
3381 blame the compiler :).
3383 \labelwidthstring 00.00.0000
3389 Include i-codes in the asm file.
3390 Looks like noise but is most helpfull for debugging the compiler itself.
3391 \layout Subsubsection
3393 Intermediate Dump Options
3396 The following options are provided for the purpose of retargetting and debugging
3398 These provided a means to dump the intermediate code (iCode) generated
3399 by the compiler in human readable form at various stages of the compilation
3403 \labelwidthstring 00.00.0000
3409 This option will cause the compiler to dump the intermediate code into
3412 <source filename>.dumpraw
3414 just after the intermediate code has been generated for a function, i.e.
3415 before any optimizations are done.
3416 The basic blocks at this stage ordered in the depth first number, so they
3417 may not be in sequence of execution.
3419 \labelwidthstring 00.00.0000
3425 Will create a dump of iCode's, after global subexpression elimination,
3428 <source filename>.dumpgcse.
3430 \labelwidthstring 00.00.0000
3436 Will create a dump of iCode's, after deadcode elimination, into a file
3439 <source filename>.dumpdeadcode.
3441 \labelwidthstring 00.00.0000
3450 Will create a dump of iCode's, after loop optimizations, into a file named
3453 <source filename>.dumploop.
3455 \labelwidthstring 00.00.0000
3464 Will create a dump of iCode's, after live range analysis, into a file named
3467 <source filename>.dumprange.
3469 \labelwidthstring 00.00.0000
3475 Will dump the life ranges for all symbols.
3477 \labelwidthstring 00.00.0000
3486 Will create a dump of iCode's, after register assignment, into a file named
3489 <source filename>.dumprassgn.
3491 \labelwidthstring 00.00.0000
3497 Will create a dump of the live ranges of iTemp's
3499 \labelwidthstring 00.00.0000
3510 Will cause all the above mentioned dumps to be created.
3513 Environment variables
3516 SDCC recognizes the following environment variables:
3518 \labelwidthstring 00.00.0000
3524 SDCC installs a signal handler to be able to delete temporary files after
3525 an user break (^C) or an exception.
3526 If this environment variable is set, SDCC won't install the signal handler
3527 in order to be able to debug SDCC.
3529 \labelwidthstring 00.00.0000
3537 Path, where temporary files will be created.
3538 The order of the variables is the search order.
3539 In a standard *nix environment these variables are not set, and there's
3540 no need to set them.
3541 On Windows it's recommended to set one of them.
3543 \labelwidthstring 00.00.0000
3547 (coming\SpecialChar ~
3552 \begin_inset Quotes sld
3555 2.1 Install and search paths
3556 \begin_inset Quotes srd
3561 \labelwidthstring 00.00.0000
3565 (coming\SpecialChar ~
3570 \begin_inset Quotes sld
3573 2.1 Install and search paths
3574 \begin_inset Quotes srd
3579 \labelwidthstring 00.00.0000
3583 (coming\SpecialChar ~
3588 \begin_inset Quotes sld
3591 2.1 Install and search paths
3592 \begin_inset Quotes srd
3597 \labelwidthstring 00.00.0000
3601 SDCCDIR\SpecialChar ~
3603 replaced\SpecialChar ~
3608 \begin_inset Quotes sld
3611 2.1 Install and search paths
3612 \begin_inset Quotes srd
3618 There are some more environment variables recognized by SDCC, but these
3619 are solely used for debugging purposes.
3620 They can change or disappear very quickly, and will never be documentated.
3623 MCS51/DS390 Storage Class Language Extensions
3626 In addition to the ANSI storage classes SDCC allows the following MCS51
3627 specific storage classes.
3628 \layout Subsubsection
3633 Variables declared with this storage class will be placed in the extern
3639 storage class for Large Memory model, e.g.:
3645 xdata unsigned char xduc;
3646 \layout Subsubsection
3655 storage class for Small Memory model.
3656 Variables declared with this storage class will be allocated in the internal
3664 \layout Subsubsection
3669 Variables declared with this storage class will be allocated into the indirectly
3670 addressable portion of the internal ram of a 8051, e.g.:
3677 \layout Subsubsection
3682 This is a data-type and a storage class specifier.
3683 When a variable is declared as a bit, it is allocated into the bit addressable
3684 memory of 8051, e.g.:
3691 \layout Subsubsection
3696 Like the bit keyword,
3700 signifies both a data-type and storage class, they are used to describe
3701 the special function registers and special bit variables of a 8051, eg:
3707 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
3709 sbit at 0xd7 CY; /* CY (Carry Flag) */
3715 SDCC allows (via language extensions) pointers to explicitly point to any
3716 of the memory spaces of the 8051.
3717 In addition to the explicit pointers, the compiler uses (by default) generic
3718 pointers which can be used to point to any of the memory spaces.
3722 Pointer declaration examples:
3731 /* pointer physically in xternal ram pointing to object in internal ram
3734 data unsigned char * xdata p;
3738 /* pointer physically in code rom pointing to data in xdata space */
3740 xdata unsigned char * code p;
3744 /* pointer physically in code space pointing to data in code space */
3746 code unsigned char * code p;
3750 /* the folowing is a generic pointer physically located in xdata space */
3761 Well you get the idea.
3766 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
3779 The highest order byte of the
3783 pointers contains the data space information.
3784 Assembler support routines are called whenever data is stored or retrieved
3790 These are useful for developing reusable library routines.
3791 Explicitly specifying the pointer type will generate the most efficient
3795 Parameters & Local Variables
3798 Automatic (local) variables and parameters to functions can either be placed
3799 on the stack or in data-space.
3800 The default action of the compiler is to place these variables in the internal
3801 RAM (for small model) or external RAM (for large model).
3802 This in fact makes them
3806 so by default functions are non-reentrant.
3810 They can be placed on the stack either by using the
3814 option or by using the
3818 keyword in the function declaration, e.g.:
3827 unsigned char foo(char i) reentrant
3840 Since stack space on 8051 is limited, the
3848 option should be used sparingly.
3849 Note that the reentrant keyword just means that the parameters & local
3850 variables will be allocated to the stack, it
3854 mean that the function is register bank independent.
3858 Local variables can be assigned storage classes and absolute addresses,
3865 unsigned char foo() {
3871 xdata unsigned char i;
3883 data at 0x31 unsiged char j;
3898 In the above example the variable
3902 will be allocated in the external ram,
3906 in bit addressable space and
3915 or when a function is declared as
3919 this should only be done for static variables.
3922 Parameters however are not allowed any storage class, (storage classes for
3923 parameters will be ignored), their allocation is governed by the memory
3924 model in use, and the reentrancy options.
3930 For non-reentrant functions SDCC will try to reduce internal ram space usage
3931 by overlaying parameters and local variables of a function (if possible).
3932 Parameters and local variables of a function will be allocated to an overlayabl
3933 e segment if the function has
3935 no other function calls and the function is non-reentrant and the memory
3939 If an explicit storage class is specified for a local variable, it will
3943 Note that the compiler (not the linkage editor) makes the decision for overlayin
3945 Functions that are called from an interrupt service routine should be preceded
3946 by a #pragma\SpecialChar ~
3947 NOOVERLAY if they are not reentrant.
3950 Also note that the compiler does not do any processing of inline assembler
3951 code, so the compiler might incorrectly assign local variables and parameters
3952 of a function into the overlay segment if the inline assembler code calls
3953 other c-functions that might use the overlay.
3954 In that case the #pragma\SpecialChar ~
3955 NOOVERLAY should be used.
3958 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
3959 tion or division will NOT be overlayed since these are implemented using
3960 external functions, e.g.:
3970 void set_error(unsigned char errcd)
3986 void some_isr () interrupt 2 using 1
4015 In the above example the parameter
4023 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
4025 not present, this could cause unpredictable runtime behavior when called
4027 The #pragma\SpecialChar ~
4028 NOOVERLAY ensures that the parameters and local variables for
4029 the function are NOT overlayed.
4032 Interrupt Service Routines
4035 SDCC allows interrupt service routines to be coded in C, with some extended
4042 void timer_isr (void) interrupt 2 using 1
4055 The number following the
4059 keyword is the interrupt number this routine will service.
4060 The compiler will insert a call to this routine in the interrupt vector
4061 table for the interrupt number specified.
4066 keyword is used to tell the compiler to use the specified register bank
4067 (8051 specific) when generating code for this function.
4068 Note that when some function is called from an interrupt service routine
4069 it should be preceded by a #pragma\SpecialChar ~
4070 NOOVERLAY if it is not reentrant.
4071 A special note here, int (16 bit) and long (32 bit) integer division, multiplic
4072 ation & modulus operations are implemented using external support routines
4073 developed in ANSI-C, if an interrupt service routine needs to do any of
4074 these operations then the support routines (as mentioned in a following
4075 section) will have to be recompiled using the
4079 option and the source file will need to be compiled using the
4086 If you have multiple source files in your project, interrupt service routines
4087 can be present in any of them, but a prototype of the isr MUST be present
4088 or included in the file that contains the function
4095 Interrupt Numbers and the corresponding address & descriptions for the Standard
4096 8051 are listed below.
4097 SDCC will automatically adjust the interrupt vector table to the maximum
4098 interrupt number specified.
4104 \begin_inset Tabular
4105 <lyxtabular version="3" rows="6" columns="3">
4107 <column alignment="center" valignment="top" leftline="true" width="0pt">
4108 <column alignment="center" valignment="top" leftline="true" width="0pt">
4109 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
4110 <row topline="true" bottomline="true">
4111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4127 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4136 <row topline="true">
4137 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4162 <row topline="true">
4163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4179 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4188 <row topline="true">
4189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4214 <row topline="true">
4215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4240 <row topline="true" bottomline="true">
4241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
4257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
4274 If the interrupt service routine is defined without
4278 a register bank or with register bank 0 (using 0), the compiler will save
4279 the registers used by itself on the stack upon entry and restore them at
4280 exit, however if such an interrupt service routine calls another function
4281 then the entire register bank will be saved on the stack.
4282 This scheme may be advantageous for small interrupt service routines which
4283 have low register usage.
4286 If the interrupt service routine is defined to be using a specific register
4291 are save and restored, if such an interrupt service routine calls another
4292 function (using another register bank) then the entire register bank of
4293 the called function will be saved on the stack.
4294 This scheme is recommended for larger interrupt service routines.
4297 Calling other functions from an interrupt service routine is not recommended,
4298 avoid it if possible.
4302 Also see the _naked modifier.
4310 <TODO: this isn't implemented at all!>
4316 A special keyword may be associated with a function declaring it as
4321 SDCC will generate code to disable all interrupts upon entry to a critical
4322 function and enable them back before returning.
4323 Note that nesting critical functions may cause unpredictable results.
4348 The critical attribute maybe used with other attributes like
4356 A special keyword may be associated with a function declaring it as
4365 function modifier attribute prevents the compiler from generating prologue
4366 and epilogue code for that function.
4367 This means that the user is entirely responsible for such things as saving
4368 any registers that may need to be preserved, selecting the proper register
4369 bank, generating the
4373 instruction at the end, etc.
4374 Practically, this means that the contents of the function must be written
4375 in inline assembler.
4376 This is particularly useful for interrupt functions, which can have a large
4377 (and often unnecessary) prologue/epilogue.
4378 For example, compare the code generated by these two functions:
4384 data unsigned char counter;
4386 void simpleInterrupt(void) interrupt 1
4400 void nakedInterrupt(void) interrupt 2 _naked
4433 ; MUST explicitly include ret in _naked function.
4447 For an 8051 target, the generated simpleInterrupt looks like:
4592 whereas nakedInterrupt looks like:
4617 ; MUST explicitly include ret(i) in _naked function.
4623 While there is nothing preventing you from writing C code inside a _naked
4624 function, there are many ways to shoot yourself in the foot doing this,
4625 and it is recommended that you stick to inline assembler.
4628 Functions using private banks
4635 attribute (which tells the compiler to use a register bank other than the
4636 default bank zero) should only be applied to
4640 functions (see note 1 below).
4641 This will in most circumstances make the generated ISR code more efficient
4642 since it will not have to save registers on the stack.
4649 attribute will have no effect on the generated code for a
4653 function (but may occasionally be useful anyway
4659 possible exception: if a function is called ONLY from 'interrupt' functions
4660 using a particular bank, it can be declared with the same 'using' attribute
4661 as the calling 'interrupt' functions.
4662 For instance, if you have several ISRs using bank one, and all of them
4663 call memcpy(), it might make sense to create a specialized version of memcpy()
4664 'using 1', since this would prevent the ISR from having to save bank zero
4665 to the stack on entry and switch to bank zero before calling the function
4672 (pending: I don't think this has been done yet)
4679 function using a non-zero bank will assume that it can trash that register
4680 bank, and will not save it.
4681 Since high-priority interrupts can interrupt low-priority ones on the 8051
4682 and friends, this means that if a high-priority ISR
4686 a particular bank occurs while processing a low-priority ISR
4690 the same bank, terrible and bad things can happen.
4691 To prevent this, no single register bank should be
4695 by both a high priority and a low priority ISR.
4696 This is probably most easily done by having all high priority ISRs use
4697 one bank and all low priority ISRs use another.
4698 If you have an ISR which can change priority at runtime, you're on your
4699 own: I suggest using the default bank zero and taking the small performance
4703 It is most efficient if your ISR calls no other functions.
4704 If your ISR must call other functions, it is most efficient if those functions
4705 use the same bank as the ISR (see note 1 below); the next best is if the
4706 called functions use bank zero.
4707 It is very inefficient to call a function using a different, non-zero bank
4715 Data items can be assigned an absolute address with the
4719 keyword, in addition to a storage class, e.g.:
4725 xdata at 0x8000 unsigned char PORTA_8255 ;
4731 In the above example the PORTA_8255 will be allocated to the location 0x8000
4732 of the external ram.
4733 Note that this feature is provided to give the programmer access to
4737 devices attached to the controller.
4738 The compiler does not actually reserve any space for variables declared
4739 in this way (they are implemented with an equate in the assembler).
4740 Thus it is left to the programmer to make sure there are no overlaps with
4741 other variables that are declared without the absolute address.
4742 The assembler listing file (.lst) and the linker output files (.rst) and
4743 (.map) are a good places to look for such overlaps.
4747 Absolute address can be specified for variables in all storage classes,
4760 The above example will allocate the variable at offset 0x02 in the bit-addressab
4762 There is no real advantage to assigning absolute addresses to variables
4763 in this manner, unless you want strict control over all the variables allocated.
4769 The compiler inserts a call to the C routine
4771 _sdcc__external__startup()
4776 at the start of the CODE area.
4777 This routine is in the runtime library.
4778 By default this routine returns 0, if this routine returns a non-zero value,
4779 the static & global variable initialization will be skipped and the function
4780 main will be invoked Other wise static & global variables will be initialized
4781 before the function main is invoked.
4784 _sdcc__external__startup()
4786 routine to your program to override the default if you need to setup hardware
4787 or perform some other critical operation prior to static & global variable
4791 Inline Assembler Code
4794 SDCC allows the use of in-line assembler with a few restriction as regards
4796 All labels defined within inline assembler code
4804 where nnnn is a number less than 100 (which implies a limit of utmost 100
4805 inline assembler labels
4813 It is strongly recommended that each assembly instruction (including labels)
4814 be placed in a separate line (as the example shows).
4819 command line option is used, the inline assembler code will be passed through
4820 the peephole optimizer.
4821 This might cause some unexpected changes in the inline assembler code.
4822 Please go throught the peephole optimizer rules defined in file
4826 carefully before using this option.
4866 The inline assembler code can contain any valid code understood by the assembler
4867 , this includes any assembler directives and comment lines.
4868 The compiler does not do any validation of the code within the
4878 Inline assembler code cannot reference any C-Labels, however it can reference
4879 labels defined by the inline assembler, e.g.:
4905 ; some assembler code
4925 /* some more c code */
4927 clabel:\SpecialChar ~
4929 /* inline assembler cannot reference this label */
4941 $0003: ;label (can be reference by inline assembler only)
4953 /* some more c code */
4961 In other words inline assembly code can access labels defined in inline
4962 assembly within the scope of the funtion.
4966 The same goes the other way, ie.
4967 labels defines in inline assembly CANNOT be accessed by C statements.
4970 int (16 bit) and long (32 bit) Support
4973 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
4974 multiplication and modulus operations are implemented by support routines.
4975 These support routines are all developed in ANSI-C to facilitate porting
4976 to other MCUs, although some model specific assembler optimations are used.
4977 The following files contain the described routine, all of them can be found
4978 in <installdir>/share/sdcc/lib.
4984 <pending: tabularise this>
4990 _mulsint.c - signed 16 bit multiplication (calls _muluint)
4992 _muluint.c - unsigned 16 bit multiplication
4994 _divsint.c - signed 16 bit division (calls _divuint)
4996 _divuint.c - unsigned 16 bit division
4998 _modsint.c - signed 16 bit modulus (call _moduint)
5000 _moduint.c - unsigned 16 bit modulus
5002 _mulslong.c - signed 32 bit multiplication (calls _mululong)
5004 _mululong.c - unsigned32 bit multiplication
5006 _divslong.c - signed 32 division (calls _divulong)
5008 _divulong.c - unsigned 32 division
5010 _modslong.c - signed 32 bit modulus (calls _modulong)
5012 _modulong.c - unsigned 32 bit modulus
5020 Since they are compiled as
5024 , interrupt service routines should not do any of the above operations.
5025 If this is unavoidable then the above routines will need to be compiled
5030 option, after which the source program will have to be compiled with
5037 Floating Point Support
5040 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
5041 point support routines are derived from gcc's floatlib.c and consists of
5042 the following routines:
5048 <pending: tabularise this>
5054 _fsadd.c - add floating point numbers
5056 _fssub.c - subtract floating point numbers
5058 _fsdiv.c - divide floating point numbers
5060 _fsmul.c - multiply floating point numbers
5062 _fs2uchar.c - convert floating point to unsigned char
5064 _fs2char.c - convert floating point to signed char
5066 _fs2uint.c - convert floating point to unsigned int
5068 _fs2int.c - convert floating point to signed int
5070 _fs2ulong.c - convert floating point to unsigned long
5072 _fs2long.c - convert floating point to signed long
5074 _uchar2fs.c - convert unsigned char to floating point
5076 _char2fs.c - convert char to floating point number
5078 _uint2fs.c - convert unsigned int to floating point
5080 _int2fs.c - convert int to floating point numbers
5082 _ulong2fs.c - convert unsigned long to floating point number
5084 _long2fs.c - convert long to floating point number
5092 Note if all these routines are used simultaneously the data space might
5094 For serious floating point usage it is strongly recommended that the large
5101 SDCC allows two memory models for MCS51 code, small and large.
5102 Modules compiled with different memory models should
5106 be combined together or the results would be unpredictable.
5107 The library routines supplied with the compiler are compiled as both small
5109 The compiled library modules are contained in seperate directories as small
5110 and large so that you can link to either set.
5114 When the large model is used all variables declared without a storage class
5115 will be allocated into the external ram, this includes all parameters and
5116 local variables (for non-reentrant functions).
5117 When the small model is used variables without storage class are allocated
5118 in the internal ram.
5121 Judicious usage of the processor specific storage classes and the 'reentrant'
5122 function type will yield much more efficient code, than using the large
5124 Several optimizations are disabled when the program is compiled using the
5125 large model, it is therefore strongly recommdended that the small model
5126 be used unless absolutely required.
5132 The only model supported is Flat 24.
5133 This generates code for the 24 bit contiguous addressing mode of the Dallas
5135 In this mode, up to four meg of external RAM or code space can be directly
5137 See the data sheets at www.dalsemi.com for further information on this part.
5141 In older versions of the compiler, this option was used with the MCS51 code
5147 Now, however, the '390 has it's own code generator, selected by the
5156 Note that the compiler does not generate any code to place the processor
5157 into 24 bitmode (although
5161 in the ds390 libraries will do that for you).
5166 , the boot loader or similar code must ensure that the processor is in 24
5167 bit contiguous addressing mode before calling the SDCC startup code.
5175 option, variables will by default be placed into the XDATA segment.
5180 Segments may be placed anywhere in the 4 meg address space using the usual
5182 Note that if any segments are located above 64K, the -r flag must be passed
5183 to the linker to generate the proper segment relocations, and the Intel
5184 HEX output format must be used.
5185 The -r flag can be passed to the linker by using the option
5189 on the sdcc command line.
5190 However, currently the linker can not handle code segments > 64k.
5193 Defines Created by the Compiler
5196 The compiler creates the following #defines.
5199 SDCC - this Symbol is always defined.
5202 SDCC_mcs51 or SDCC_ds390 or SDCC_z80, etc - depending on the model used
5206 __mcs51 or __ds390 or __z80, etc - depending on the model used (e.g.
5210 SDCC_STACK_AUTO - this symbol is defined when
5217 SDCC_MODEL_SMALL - when
5224 SDCC_MODEL_LARGE - when
5231 SDCC_USE_XSTACK - when
5238 SDCC_STACK_TENBIT - when
5245 SDCC_MODEL_FLAT24 - when
5258 SDCC performs a host of standard optimizations in addition to some MCU specific
5261 \layout Subsubsection
5263 Sub-expression Elimination
5266 The compiler does local and global common subexpression elimination, e.g.:
5281 will be translated to
5297 Some subexpressions are not as obvious as the above example, e.g.:
5311 In this case the address arithmetic a->b[i] will be computed only once;
5312 the equivalent code in C would be.
5328 The compiler will try to keep these temporary variables in registers.
5329 \layout Subsubsection
5331 Dead-Code Elimination
5346 i = 1; \SpecialChar ~
5351 global = 1;\SpecialChar ~
5364 global = 3;\SpecialChar ~
5379 int global; void f ()
5392 \layout Subsubsection
5453 Note: the dead stores created by this copy propagation will be eliminated
5454 by dead-code elimination.
5455 \layout Subsubsection
5460 Two types of loop optimizations are done by SDCC loop invariant lifting
5461 and strength reduction of loop induction variables.
5462 In addition to the strength reduction the optimizer marks the induction
5463 variables and the register allocator tries to keep the induction variables
5464 in registers for the duration of the loop.
5465 Because of this preference of the register allocator, loop induction optimizati
5466 on causes an increase in register pressure, which may cause unwanted spilling
5467 of other temporary variables into the stack / data space.
5468 The compiler will generate a warning message when it is forced to allocate
5469 extra space either on the stack or data space.
5470 If this extra space allocation is undesirable then induction optimization
5471 can be eliminated either for the entire source file (with ---noinduction
5472 option) or for a given function only using #pragma\SpecialChar ~
5483 for (i = 0 ; i < 100 ; i ++)
5501 for (i = 0; i < 100; i++)
5511 As mentioned previously some loop invariants are not as apparent, all static
5512 address computations are also moved out of the loop.
5516 Strength Reduction, this optimization substitutes an expression by a cheaper
5523 for (i=0;i < 100; i++)
5543 for (i=0;i< 100;i++) {
5547 ar[itemp1] = itemp2;
5563 The more expensive multiplication is changed to a less expensive addition.
5564 \layout Subsubsection
5569 This optimization is done to reduce the overhead of checking loop boundaries
5570 for every iteration.
5571 Some simple loops can be reversed and implemented using a
5572 \begin_inset Quotes eld
5575 decrement and jump if not zero
5576 \begin_inset Quotes erd
5580 SDCC checks for the following criterion to determine if a loop is reversible
5581 (note: more sophisticated compilers use data-dependency analysis to make
5582 this determination, SDCC uses a more simple minded analysis).
5585 The 'for' loop is of the form
5591 for (<symbol> = <expression> ; <sym> [< | <=] <expression> ; [<sym>++ |
5601 The <for body> does not contain
5602 \begin_inset Quotes eld
5606 \begin_inset Quotes erd
5610 \begin_inset Quotes erd
5616 All goto's are contained within the loop.
5619 No function calls within the loop.
5622 The loop control variable <sym> is not assigned any value within the loop
5625 The loop control variable does NOT participate in any arithmetic operation
5629 There are NO switch statements in the loop.
5630 \layout Subsubsection
5632 Algebraic Simplifications
5635 SDCC does numerous algebraic simplifications, the following is a small sub-set
5636 of these optimizations.
5642 i = j + 0 ; /* changed to */ i = j;
5644 i /= 2; /* changed to */ i >>= 1;
5646 i = j - j ; /* changed to */ i = 0;
5648 i = j / 1 ; /* changed to */ i = j;
5654 Note the subexpressions given above are generally introduced by macro expansions
5655 or as a result of copy/constant propagation.
5656 \layout Subsubsection
5661 SDCC changes switch statements to jump tables when the following conditions
5666 The case labels are in numerical sequence, the labels need not be in order,
5667 and the starting number need not be one or zero.
5673 switch(i) {\SpecialChar ~
5780 Both the above switch statements will be implemented using a jump-table.
5783 The number of case labels is at least three, since it takes two conditional
5784 statements to handle the boundary conditions.
5787 The number of case labels is less than 84, since each label takes 3 bytes
5788 and a jump-table can be utmost 256 bytes long.
5792 Switch statements which have gaps in the numeric sequence or those that
5793 have more that 84 case labels can be split into more than one switch statement
5794 for efficient code generation, e.g.:
5832 If the above switch statement is broken down into two switch statements
5866 case 9: \SpecialChar ~
5876 case 12:\SpecialChar ~
5886 then both the switch statements will be implemented using jump-tables whereas
5887 the unmodified switch statement will not be.
5888 \layout Subsubsection
5890 Bit-shifting Operations.
5893 Bit shifting is one of the most frequently used operation in embedded programmin
5895 SDCC tries to implement bit-shift operations in the most efficient way
5915 generates the following code:
5933 In general SDCC will never setup a loop if the shift count is known.
5973 Note that SDCC stores numbers in little-endian format (i.e.
5974 lowest order first).
5975 \layout Subsubsection
5980 A special case of the bit-shift operation is bit rotation, SDCC recognizes
5981 the following expression to be a left bit-rotation:
5992 i = ((i << 1) | (i >> 7));
6000 will generate the following code:
6016 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
6017 ns of this case will also be recognized as bit-rotation, i.e.:
6023 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
6024 \layout Subsubsection
6029 It is frequently required to obtain the highest order bit of an integral
6030 type (long, int, short or char types).
6031 SDCC recognizes the following expression to yield the highest order bit
6032 and generates optimized code for it, e.g.:
6053 hob = (gint >> 15) & 1;
6066 will generate the following code:
6105 000A E5*01\SpecialChar ~
6133 000C 33\SpecialChar ~
6164 000D E4\SpecialChar ~
6195 000E 13\SpecialChar ~
6226 000F F5*02\SpecialChar ~
6256 Variations of this case however will
6261 It is a standard C expression, so I heartily recommend this be the only
6262 way to get the highest order bit, (it is portable).
6263 Of course it will be recognized even if it is embedded in other expressions,
6270 xyz = gint + ((gint >> 15) & 1);
6276 will still be recognized.
6277 \layout Subsubsection
6282 The compiler uses a rule based, pattern matching and re-writing mechanism
6283 for peep-hole optimization.
6288 a peep-hole optimizer by Christopher W.
6289 Fraser (cwfraser@microsoft.com).
6290 A default set of rules are compiled into the compiler, additional rules
6291 may be added with the
6293 ---peep-file <filename>
6296 The rule language is best illustrated with examples.
6324 The above rule will change the following assembly sequence:
6354 Note: All occurrences of a
6358 (pattern variable) must denote the same string.
6359 With the above rule, the assembly sequence:
6377 will remain unmodified.
6381 Other special case optimizations may be added by the user (via
6387 some variants of the 8051 MCU allow only
6396 The following two rules will change all
6418 replace { lcall %1 } by { acall %1 }
6420 replace { ljmp %1 } by { ajmp %1 }
6428 inline-assembler code
6430 is also passed through the peep hole optimizer, thus the peephole optimizer
6431 can also be used as an assembly level macro expander.
6432 The rules themselves are MCU dependent whereas the rule language infra-structur
6433 e is MCU independent.
6434 Peephole optimization rules for other MCU can be easily programmed using
6439 The syntax for a rule is as follows:
6445 rule := replace [ restart ] '{' <assembly sequence> '
6483 <assembly sequence> '
6501 '}' [if <functionName> ] '
6509 <assembly sequence> := assembly instruction (each instruction including
6510 labels must be on a separate line).
6514 The optimizer will apply to the rules one by one from the top in the sequence
6515 of their appearance, it will terminate when all rules are exhausted.
6516 If the 'restart' option is specified, then the optimizer will start matching
6517 the rules again from the top, this option for a rule is expensive (performance)
6518 , it is intended to be used in situations where a transformation will trigger
6519 the same rule again.
6520 An example of this (not a good one, it has side effects) is the following
6547 Note that the replace pattern cannot be a blank, but can be a comment line.
6548 Without the 'restart' option only the inner most 'pop' 'push' pair would
6549 be eliminated, i.e.:
6601 the restart option the rule will be applied again to the resulting code
6602 and then all the pop-push pairs will be eliminated to yield:
6620 A conditional function can be attached to a rule.
6621 Attaching rules are somewhat more involved, let me illustrate this with
6652 The optimizer does a look-up of a function name table defined in function
6657 in the source file SDCCpeeph.c, with the name
6662 If it finds a corresponding entry the function is called.
6663 Note there can be no parameters specified for these functions, in this
6668 is crucial, since the function
6672 expects to find the label in that particular variable (the hash table containin
6673 g the variable bindings is passed as a parameter).
6674 If you want to code more such functions, take a close look at the function
6675 labelInRange and the calling mechanism in source file SDCCpeeph.c.
6676 I know this whole thing is a little kludgey, but maybe some day we will
6677 have some better means.
6678 If you are looking at this file, you will also see the default rules that
6679 are compiled into the compiler, you can add your own rules in the default
6680 set there if you get tired of specifying the ---peep-file option.
6686 SDCC supports the following #pragma directives.
6687 This directives are applicable only at a function level.
6690 SAVE - this will save all the current options.
6693 RESTORE - will restore the saved options from the last save.
6694 Note that SAVES & RESTOREs cannot be nested.
6695 SDCC uses the same buffer to save the options each time a SAVE is called.
6698 NOGCSE - will stop global subexpression elimination.
6701 NOINDUCTION - will stop loop induction optimizations.
6704 NOJTBOUND - will not generate code for boundary value checking, when switch
6705 statements are turned into jump-tables.
6708 NOOVERLAY - the compiler will not overlay the parameters and local variables
6712 NOLOOPREVERSE - Will not do loop reversal optimization
6715 EXCLUDE NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation
6716 of pair of push/pop instruction in ISR function (using interrupt keyword).
6717 The directive should be placed immediately before the ISR function definition
6718 and it affects ALL ISR functions following it.
6719 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
6720 EXCLUDE\SpecialChar ~
6724 NOIV - Do not generate interrupt vector table entries for all ISR functions
6725 defined after the pragma.
6726 This is useful in cases where the interrupt vector table must be defined
6727 manually, or when there is a secondary, manually defined interrupt vector
6729 for the autovector feature of the Cypress EZ-USB FX2).
6732 CALLEE-SAVES function1[,function2[,function3...]] - The compiler by default
6733 uses a caller saves convention for register saving across function calls,
6734 however this can cause unneccessary register pushing & popping when calling
6735 small functions from larger functions.
6736 This option can be used to switch the register saving convention for the
6737 function names specified.
6738 The compiler will not save registers when calling these functions, extra
6739 code will be generated at the entry & exit for these functions to save
6740 & restore the registers used by these functions, this can SUBSTANTIALLY
6741 reduce code & improve run time performance of the generated code.
6742 In future the compiler (with interprocedural analysis) will be able to
6743 determine the appropriate scheme to use for each function call.
6744 If ---callee-saves command line option is used, the function names specified
6745 in #pragma\SpecialChar ~
6746 CALLEE-SAVES is appended to the list of functions specified inthe
6750 The pragma's are intended to be used to turn-off certain optimizations which
6751 might cause the compiler to generate extra stack / data space to store
6752 compiler generated temporary variables.
6753 This usually happens in large functions.
6754 Pragma directives should be used as shown in the following example, they
6755 are used to control options & optimizations for a given function; pragmas
6756 should be placed before and/or after a function, placing pragma's inside
6757 a function body could have unpredictable results.
6763 #pragma SAVE /* save the current settings */
6765 #pragma NOGCSE /* turnoff global subexpression elimination */
6767 #pragma NOINDUCTION /* turn off induction optimizations */
6789 #pragma RESTORE /* turn the optimizations back on */
6795 The compiler will generate a warning message when extra space is allocated.
6796 It is strongly recommended that the SAVE and RESTORE pragma's be used when
6797 changing options for a function.
6802 <pending: this is messy and incomplete>
6807 Compiler support routines (_gptrget, _mulint etc)
6810 Stdclib functions (puts, printf, strcat etc)
6813 Math functions (sin, pow, sqrt etc)
6816 Interfacing with Assembly Routines
6817 \layout Subsubsection
6819 Global Registers used for Parameter Passing
6822 The compiler always uses the global registers
6830 to pass the first parameter to a routine.
6831 The second parameter onwards is either allocated on the stack (for reentrant
6832 routines or if ---stack-auto is used) or in the internal / external ram
6833 (depending on the memory model).
6835 \layout Subsubsection
6837 Assembler Routine(non-reentrant)
6840 In the following example the function cfunc calls an assembler routine asm_func,
6841 which takes two parameters.
6847 extern int asm_func(unsigned char, unsigned char);
6851 int c_func (unsigned char i, unsigned char j)
6859 return asm_func(i,j);
6873 return c_func(10,9);
6881 The corresponding assembler function is:
6887 .globl _asm_func_PARM_2
6951 add a,_asm_func_PARM_2
6987 Note here that the return values are placed in 'dpl' - One byte return value,
6988 'dpl' LSB & 'dph' MSB for two byte values.
6989 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
6990 b' & 'acc' for four byte values.
6993 The parameter naming convention is _<function_name>_PARM_<n>, where n is
6994 the parameter number starting from 1, and counting from the left.
6995 The first parameter is passed in
6996 \begin_inset Quotes eld
7000 \begin_inset Quotes erd
7003 for One bye parameter,
7004 \begin_inset Quotes eld
7008 \begin_inset Quotes erd
7012 \begin_inset Quotes eld
7016 \begin_inset Quotes erd
7020 \begin_inset Quotes eld
7024 \begin_inset Quotes erd
7027 for four bytes, the varible name for the second parameter will be _<function_na
7032 Assemble the assembler routine with the following command:
7039 asx8051 -losg asmfunc.asm
7046 Then compile and link the assembler routine to the C source file with the
7054 sdcc cfunc.c asmfunc.rel
7055 \layout Subsubsection
7057 Assembler Routine(reentrant)
7060 In this case the second parameter onwards will be passed on the stack, the
7061 parameters are pushed from right to left i.e.
7062 after the call the left most parameter will be on the top of the stack.
7069 extern int asm_func(unsigned char, unsigned char);
7073 int c_func (unsigned char i, unsigned char j) reentrant
7081 return asm_func(i,j);
7095 return c_func(10,9);
7103 The corresponding assembler routine is:
7213 The compiling and linking procedure remains the same, however note the extra
7214 entry & exit linkage required for the assembler code, _bp is the stack
7215 frame pointer and is used to compute the offset into the stack for parameters
7216 and local variables.
7222 The external stack is located at the start of the external ram segment,
7223 and is 256 bytes in size.
7224 When ---xstack option is used to compile the program, the parameters and
7225 local variables of all reentrant functions are allocated in this area.
7226 This option is provided for programs with large stack space requirements.
7227 When used with the ---stack-auto option, all parameters and local variables
7228 are allocated on the external stack (note support libraries will need to
7229 be recompiled with the same options).
7232 The compiler outputs the higher order address byte of the external ram segment
7233 into PORT P2, therefore when using the External Stack option, this port
7234 MAY NOT be used by the application program.
7240 Deviations from the compliancy.
7243 functions are not always reentrant.
7246 structures cannot be assigned values directly, cannot be passed as function
7247 parameters or assigned to each other and cannot be a return value from
7274 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
7285 struct s foo1 (struct s parms) /* is invalid in SDCC although allowed in
7307 return rets;/* is invalid in SDCC although allowed in ANSI */
7312 'long long' (64 bit integers) not supported.
7315 'double' precision floating point not supported.
7318 No support for setjmp and longjmp (for now).
7321 Old K&R style function declarations are NOT allowed.
7327 foo(i,j) /* this old style of function declarations */
7329 int i,j; /* are valid in ANSI but not valid in SDCC */
7343 functions declared as pointers must be dereferenced during the call.
7354 /* has to be called like this */
7356 (*foo)(); /* ansi standard allows calls to be made like 'foo()' */
7359 Cyclomatic Complexity
7362 Cyclomatic complexity of a function is defined as the number of independent
7363 paths the program can take during execution of the function.
7364 This is an important number since it defines the number test cases you
7365 have to generate to validate the function.
7366 The accepted industry standard for complexity number is 10, if the cyclomatic
7367 complexity reported by SDCC exceeds 10 you should think about simplification
7368 of the function logic.
7369 Note that the complexity level is not related to the number of lines of
7371 Large functions can have low complexity, and small functions can have large
7377 SDCC uses the following formula to compute the complexity:
7382 complexity = (number of edges in control flow graph) - (number of nodes
7383 in control flow graph) + 2;
7387 Having said that the industry standard is 10, you should be aware that in
7388 some cases it be may unavoidable to have a complexity level of less than
7390 For example if you have switch statement with more than 10 case labels,
7391 each case label adds one to the complexity level.
7392 The complexity level is by no means an absolute measure of the algorithmic
7393 complexity of the function, it does however provide a good starting point
7394 for which functions you might look at for further optimization.
7400 Here are a few guidelines that will help the compiler generate more efficient
7401 code, some of the tips are specific to this compiler others are generally
7402 good programming practice.
7405 Use the smallest data type to represent your data-value.
7406 If it is known in advance that the value is going to be less than 256 then
7407 use an 'unsigned char' instead of a 'short' or 'int'.
7410 Use unsigned when it is known in advance that the value is not going to
7412 This helps especially if you are doing division or multiplication.
7415 NEVER jump into a LOOP.
7418 Declare the variables to be local whenever possible, especially loop control
7419 variables (induction).
7422 Since the compiler does not always do implicit integral promotion, the programme
7423 r should do an explicit cast when integral promotion is required.
7426 Reducing the size of division, multiplication & modulus operations can reduce
7427 code size substantially.
7428 Take the following code for example.
7434 foobar(unsigned int p1, unsigned char ch)
7438 unsigned char ch1 = p1 % ch ;
7449 For the modulus operation the variable ch will be promoted to unsigned int
7450 first then the modulus operation will be performed (this will lead to a
7451 call to support routine _moduint()), and the result will be casted to a
7453 If the code is changed to
7459 foobar(unsigned int p1, unsigned char ch)
7463 unsigned char ch1 = (unsigned char)p1 % ch ;
7474 It would substantially reduce the code generated (future versions of the
7475 compiler will be smart enough to detect such optimization oppurtunities).
7478 Notes on MCS51 memory layout
7481 The 8051 family of micro controller have a minimum of 128 bytes of internal
7482 memory which is structured as follows
7486 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R7 to R7
7489 - Bytes 20-2F - 16 bytes to hold 128 bit variables and
7491 - Bytes 30-7F - 60 bytes for general purpose use.
7495 Normally the SDCC compiler will only utilise the first bank of registers,
7496 but it is possible to specify that other banks of registers should be used
7497 in interrupt routines.
7498 By default, the compiler will place the stack after the last bank of used
7500 if the first 2 banks of registers are used, it will position the base of
7501 the internal stack at address 16 (0X10).
7502 This implies that as the stack grows, it will use up the remaining register
7503 banks, and the 16 bytes used by the 128 bit variables, and 60 bytes for
7504 general purpose use.
7507 By default, the compiler uses the 60 general purpose bytes to hold "near
7509 The compiler/optimiser may also declare some Local Variables in this area
7514 If any of the 128 bit variables are used, or near data is being used then
7515 care needs to be taken to ensure that the stack does not grow so much that
7516 it starts to over write either your bit variables or "near data".
7517 There is no runtime checking to prevent this from happening.
7520 The amount of stack being used is affected by the use of the "internal stack"
7521 to save registers before a subroutine call is made (---stack-auto will
7522 declare parameters and local variables on the stack) and the number of
7526 If you detect that the stack is over writing you data, then the following
7528 ---xstack will cause an external stack to be used for saving registers
7529 and (if ---stack-auto is being used) storing parameters and local variables.
7530 However this will produce more code which will be slower to execute.
7534 ---stack-loc will allow you specify the start of the stack, i.e.
7535 you could start it after any data in the general purpose area.
7536 However this may waste the memory not used by the register banks and if
7537 the size of the "near data" increases, it may creep into the bottom of
7541 ---stack-after-data, similar to the ---stack-loc, but it automatically places
7542 the stack after the end of the "near data".
7543 Again this could waste any spare register space.
7546 ---data-loc allows you to specify the start address of the near data.
7547 This could be used to move the "near data" further away from the stack
7548 giving it more room to grow.
7549 This will only work if no bit variables are being used and the stack can
7550 grow to use the bit variable space.
7558 If you find that the stack is over writing your bit variables or "near data"
7559 then the approach which best utilised the internal memory is to position
7560 the "near data" after the last bank of used registers or, if you use bit
7561 variables, after the last bit variable by using the ---data-loc, e.g.
7562 if two register banks are being used and no bit variables, ---data-loc
7563 16, and use the ---stack-after-data option.
7566 If bit variables are being used, another method would be to try and squeeze
7567 the data area in the unused register banks if it will fit, and start the
7568 stack after the last bit variable.
7571 Retargetting for other MCUs.
7574 The issues for retargetting the compiler are far too numerous to be covered
7576 What follows is a brief description of each of the seven phases of the
7577 compiler and its MCU dependency.
7580 Parsing the source and building the annotated parse tree.
7581 This phase is largely MCU independent (except for the language extensions).
7582 Syntax & semantic checks are also done in this phase, along with some initial
7583 optimizations like back patching labels and the pattern matching optimizations
7584 like bit-rotation etc.
7587 The second phase involves generating an intermediate code which can be easy
7588 manipulated during the later phases.
7589 This phase is entirely MCU independent.
7590 The intermediate code generation assumes the target machine has unlimited
7591 number of registers, and designates them with the name iTemp.
7592 The compiler can be made to dump a human readable form of the code generated
7593 by using the ---dumpraw option.
7596 This phase does the bulk of the standard optimizations and is also MCU independe
7598 This phase can be broken down into several sub-phases:
7602 Break down intermediate code (iCode) into basic blocks.
7604 Do control flow & data flow analysis on the basic blocks.
7606 Do local common subexpression elimination, then global subexpression elimination
7608 Dead code elimination
7612 If loop optimizations caused any changes then do 'global subexpression eliminati
7613 on' and 'dead code elimination' again.
7616 This phase determines the live-ranges; by live range I mean those iTemp
7617 variables defined by the compiler that still survive after all the optimization
7619 Live range analysis is essential for register allocation, since these computati
7620 on determines which of these iTemps will be assigned to registers, and for
7624 Phase five is register allocation.
7625 There are two parts to this process.
7629 The first part I call 'register packing' (for lack of a better term).
7630 In this case several MCU specific expression folding is done to reduce
7635 The second part is more MCU independent and deals with allocating registers
7636 to the remaining live ranges.
7637 A lot of MCU specific code does creep into this phase because of the limited
7638 number of index registers available in the 8051.
7641 The Code generation phase is (unhappily), entirely MCU dependent and very
7642 little (if any at all) of this code can be reused for other MCU.
7643 However the scheme for allocating a homogenized assembler operand for each
7644 iCode operand may be reused.
7647 As mentioned in the optimization section the peep-hole optimizer is rule
7648 based system, which can reprogrammed for other MCUs.
7651 SDCDB - Source Level Debugger
7654 SDCC is distributed with a source level debugger.
7655 The debugger uses a command line interface, the command repertoire of the
7656 debugger has been kept as close to gdb (the GNU debugger) as possible.
7657 The configuration and build process is part of the standard compiler installati
7658 on, which also builds and installs the debugger in the target directory
7659 specified during configuration.
7660 The debugger allows you debug BOTH at the C source and at the ASM source
7664 Compiling for Debugging
7669 debug option must be specified for all files for which debug information
7671 The complier generates a .cdb file for each of these files.
7672 The linker updates the .cdb file with the address information.
7673 This .cdb is used by the debugger.
7676 How the Debugger Works
7679 When the ---debug option is specified the compiler generates extra symbol
7680 information some of which are put into the the assembler source and some
7681 are put into the .cdb file, the linker updates the .cdb file with the address
7682 information for the symbols.
7683 The debugger reads the symbolic information generated by the compiler &
7684 the address information generated by the linker.
7685 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
7686 execution is controlled by the debugger.
7687 When a command is issued for the debugger, it translates it into appropriate
7688 commands for the simulator.
7691 Starting the Debugger
7694 The debugger can be started using the following command line.
7695 (Assume the file you are debugging has the file name foo).
7709 The debugger will look for the following files.
7712 foo.c - the source file.
7715 foo.cdb - the debugger symbol information file.
7718 foo.ihx - the intel hex format object file.
7721 Command Line Options.
7724 ---directory=<source file directory> this option can used to specify the
7725 directory search list.
7726 The debugger will look into the directory list specified for source, cdb
7728 The items in the directory list must be separated by ':', e.g.
7729 if the source files can be in the directories /home/src1 and /home/src2,
7730 the ---directory option should be ---directory=/home/src1:/home/src2.
7731 Note there can be no spaces in the option.
7735 -cd <directory> - change to the <directory>.
7738 -fullname - used by GUI front ends.
7741 -cpu <cpu-type> - this argument is passed to the simulator please see the
7742 simulator docs for details.
7745 -X <Clock frequency > this options is passed to the simulator please see
7746 the simulator docs for details.
7749 -s <serial port file> passed to simulator see the simulator docs for details.
7752 -S <serial in,out> passed to simulator see the simulator docs for details.
7758 As mention earlier the command interface for the debugger has been deliberately
7759 kept as close the GNU debugger gdb, as possible.
7760 This will help the integration with existing graphical user interfaces
7761 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
7762 \layout Subsubsection
7764 break [line | file:line | function | file:function]
7767 Set breakpoint at specified line or function:
7776 sdcdb>break foo.c:100
7780 sdcdb>break foo.c:funcfoo
7781 \layout Subsubsection
7783 clear [line | file:line | function | file:function ]
7786 Clear breakpoint at specified line or function:
7795 sdcdb>clear foo.c:100
7799 sdcdb>clear foo.c:funcfoo
7800 \layout Subsubsection
7805 Continue program being debugged, after breakpoint.
7806 \layout Subsubsection
7811 Execute till the end of the current function.
7812 \layout Subsubsection
7817 Delete breakpoint number 'n'.
7818 If used without any option clear ALL user defined break points.
7819 \layout Subsubsection
7821 info [break | stack | frame | registers ]
7824 info break - list all breakpoints
7827 info stack - show the function call stack.
7830 info frame - show information about the current execution frame.
7833 info registers - show content of all registers.
7834 \layout Subsubsection
7839 Step program until it reaches a different source line.
7840 \layout Subsubsection
7845 Step program, proceeding through subroutine calls.
7846 \layout Subsubsection
7851 Start debugged program.
7852 \layout Subsubsection
7857 Print type information of the variable.
7858 \layout Subsubsection
7863 print value of variable.
7864 \layout Subsubsection
7869 load the given file name.
7870 Note this is an alternate method of loading file for debugging.
7871 \layout Subsubsection
7876 print information about current frame.
7877 \layout Subsubsection
7882 Toggle between C source & assembly source.
7883 \layout Subsubsection
7888 Send the string following '!' to the simulator, the simulator response is
7890 Note the debugger does not interpret the command being sent to the simulator,
7891 so if a command like 'go' is sent the debugger can loose its execution
7892 context and may display incorrect values.
7893 \layout Subsubsection
7900 My name is Bobby Brown"
7903 Interfacing with XEmacs.
7906 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
7907 sdcdb.el and sdcdbsrc.el.
7908 These two files can be found in the $(prefix)/bin directory after the installat
7910 These files need to be loaded into XEmacs for the interface to work.
7911 This can be done at XEmacs startup time by inserting the following into
7912 your '.xemacs' file (which can be found in your HOME directory):
7918 (load-file sdcdbsrc.el)
7924 .xemacs is a lisp file so the () around the command is REQUIRED.
7925 The files can also be loaded dynamically while XEmacs is running, set the
7926 environment variable 'EMACSLOADPATH' to the installation bin directory
7927 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
7928 To start the interface enter the following command:
7942 You will prompted to enter the file name to be debugged.
7947 The command line options that are passed to the simulator directly are bound
7948 to default values in the file sdcdbsrc.el.
7949 The variables are listed below, these values maybe changed as required.
7952 sdcdbsrc-cpu-type '51
7955 sdcdbsrc-frequency '11059200
7961 The following is a list of key mapping for the debugger interface.
7969 ;; Current Listing ::
7986 binding\SpecialChar ~
8025 ------\SpecialChar ~
8065 sdcdb-next-from-src\SpecialChar ~
8091 sdcdb-back-from-src\SpecialChar ~
8117 sdcdb-cont-from-src\SpecialChar ~
8127 SDCDB continue command
8143 sdcdb-step-from-src\SpecialChar ~
8169 sdcdb-whatis-c-sexp\SpecialChar ~
8179 SDCDB ptypecommand for data at
8243 sdcdbsrc-delete\SpecialChar ~
8257 SDCDB Delete all breakpoints if no arg
8305 given or delete arg (C-u arg x)
8321 sdcdbsrc-frame\SpecialChar ~
8336 SDCDB Display current frame if no arg,
8385 given or display frame arg
8450 sdcdbsrc-goto-sdcdb\SpecialChar ~
8460 Goto the SDCDB output buffer
8476 sdcdb-print-c-sexp\SpecialChar ~
8487 SDCDB print command for data at
8551 sdcdbsrc-goto-sdcdb\SpecialChar ~
8561 Goto the SDCDB output buffer
8577 sdcdbsrc-mode\SpecialChar ~
8593 Toggles Sdcdbsrc mode (turns it off)
8597 ;; C-c C-f\SpecialChar ~
8605 sdcdb-finish-from-src\SpecialChar ~
8613 SDCDB finish command
8617 ;; C-x SPC\SpecialChar ~
8625 sdcdb-break\SpecialChar ~
8643 Set break for line with point
8645 ;; ESC t\SpecialChar ~
8655 sdcdbsrc-mode\SpecialChar ~
8671 Toggle Sdcdbsrc mode
8673 ;; ESC m\SpecialChar ~
8683 sdcdbsrc-srcmode\SpecialChar ~
8707 The Z80 and gbz80 port
8710 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
8711 The port is incomplete - long support is incomplete (mul, div and mod are
8712 unimplimented), and both float and bitfield support is missing.
8713 Apart from that the code generated is correct.
8716 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
8717 The stack frame is similar to that generated by the IAR Z80 compiler.
8718 IX is used as the base pointer, HL is used as a temporary register, and
8719 BC and DE are available for holding varibles.
8720 IY is currently unusued.
8721 Return values are stored in HL.
8722 One bad side effect of using IX as the base pointer is that a functions
8723 stack frame is limited to 127 bytes - this will be fixed in a later version.
8729 SDCC has grown to be a large project.
8730 The compiler alone (without the preprocessor, assembler and linker) is
8731 about 40,000 lines of code (blank stripped).
8732 The open source nature of this project is a key to its continued growth
8734 You gain the benefit and support of many active software developers and
8736 Is SDCC perfect? No, that's why we need your help.
8737 The developers take pride in fixing reported bugs.
8738 You can help by reporting the bugs and helping other SDCC users.
8739 There are lots of ways to contribute, and we encourage you to take part
8740 in making SDCC a great software package.
8746 Send an email to the mailing list at 'user-sdcc@sdcc.sourceforge.net' or 'devel-sd
8747 cc@sdcc.sourceforge.net'.
8748 Bugs will be fixed ASAP.
8749 When reporting a bug, it is very useful to include a small test program
8750 which reproduces the problem.
8751 If you can isolate the problem by looking at the generated assembly code,
8752 this can be very helpful.
8753 Compiling your program with the ---dumpall option can sometimes be useful
8754 in locating optimization problems.
8760 The anatomy of the compiler
8765 This is an excerpt from an atricle published in Circuit Cellar MagaZine
8767 It's a little outdated (the compiler is much more efficient now and user/devell
8768 oper friendly), but pretty well exposes the guts of it all.
8774 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
8775 It is fairly easy to retarget for other 8-bit MCU.
8776 Here we take a look at some of the internals of the compiler.
8783 Parsing the input source file and creating an AST (Annotated Syntax Tree).
8784 This phase also involves propagating types (annotating each node of the
8785 parse tree with type information) and semantic analysis.
8786 There are some MCU specific parsing rules.
8787 For example the storage classes, the extended storage classes are MCU specific
8788 while there may be a xdata storage class for 8051 there is no such storage
8789 class for z80 or Atmel AVR.
8790 SDCC allows MCU specific storage class extensions, i.e.
8791 xdata will be treated as a storage class specifier when parsing 8051 C
8792 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
8799 Intermediate code generation.
8800 In this phase the AST is broken down into three-operand form (iCode).
8801 These three operand forms are represented as doubly linked lists.
8802 ICode is the term given to the intermediate form generated by the compiler.
8803 ICode example section shows some examples of iCode generated for some simple
8810 Bulk of the target independent optimizations is performed in this phase.
8811 The optimizations include constant propagation, common sub-expression eliminati
8812 on, loop invariant code movement, strength reduction of loop induction variables
8813 and dead-code elimination.
8819 During intermediate code generation phase, the compiler assumes the target
8820 machine has infinite number of registers and generates a lot of temporary
8822 The live range computation determines the lifetime of each of these compiler-ge
8823 nerated temporaries.
8824 A picture speaks a thousand words.
8825 ICode example sections show the live range annotations for each of the
8827 It is important to note here, each iCode is assigned a number in the order
8828 of its execution in the function.
8829 The live ranges are computed in terms of these numbers.
8830 The from number is the number of the iCode which first defines the operand
8831 and the to number signifies the iCode which uses this operand last.
8837 The register allocation determines the type and number of registers needed
8839 In most MCUs only a few registers can be used for indirect addressing.
8840 In case of 8051 for example the registers R0 & R1 can be used to indirectly
8841 address the internal ram and DPTR to indirectly address the external ram.
8842 The compiler will try to allocate the appropriate register to pointer variables
8844 ICode example section shows the operands annotated with the registers assigned
8846 The compiler will try to keep operands in registers as much as possible;
8847 there are several schemes the compiler uses to do achieve this.
8848 When the compiler runs out of registers the compiler will check to see
8849 if there are any live operands which is not used or defined in the current
8850 basic block being processed, if there are any found then it will push that
8851 operand and use the registers in this block, the operand will then be popped
8852 at the end of the basic block.
8856 There are other MCU specific considerations in this phase.
8857 Some MCUs have an accumulator; very short-lived operands could be assigned
8858 to the accumulator instead of general-purpose register.
8864 Figure II gives a table of iCode operations supported by the compiler.
8865 The code generation involves translating these operations into corresponding
8866 assembly code for the processor.
8867 This sounds overly simple but that is the essence of code generation.
8868 Some of the iCode operations are generated on a MCU specific manner for
8869 example, the z80 port does not use registers to pass parameters so the
8870 SEND and RECV iCode operations will not be generated, and it also does
8871 not support JUMPTABLES.
8878 <Where is Figure II ?>
8884 This section shows some details of iCode.
8885 The example C code does not do anything useful; it is used as an example
8886 to illustrate the intermediate code generated by the compiler.
8899 /* This function does nothing useful.
8906 for the purpose of explaining iCode */
8909 short function (data int *x)
8917 short i=10; /* dead initialization eliminated */
8922 short sum=10; /* dead initialization eliminated */
8935 while (*x) *x++ = *p++;
8949 /* compiler detects i,j to be induction variables */
8953 for (i = 0, j = 10 ; i < 10 ; i++, j---) {
8965 mul += i * 3; /* this multiplication remains */
8971 gint += j * 3;/* this multiplication changed to addition */
8988 In addition to the operands each iCode contains information about the filename
8989 and line it corresponds to in the source file.
8990 The first field in the listing should be interpreted as follows:
8995 Filename(linenumber: iCode Execution sequence number : ICode hash table
8996 key : loop depth of the iCode).
9001 Then follows the human readable form of the ICode operation.
9002 Each operand of this triplet form can be of three basic types a) compiler
9003 generated temporary b) user defined variable c) a constant value.
9004 Note that local variables and parameters are replaced by compiler generated
9006 Live ranges are computed only for temporaries (i.e.
9007 live ranges are not computed for global variables).
9008 Registers are allocated for temporaries only.
9009 Operands are formatted in the following manner:
9014 Operand Name [lr live-from : live-to ] { type information } [ registers
9020 As mentioned earlier the live ranges are computed in terms of the execution
9021 sequence number of the iCodes, for example
9023 the iTemp0 is live from (i.e.
9024 first defined in iCode with execution sequence number 3, and is last used
9025 in the iCode with sequence number 5).
9026 For induction variables such as iTemp21 the live range computation extends
9027 the lifetime from the start to the end of the loop.
9029 The register allocator used the live range information to allocate registers,
9030 the same registers may be used for different temporaries if their live
9031 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
9032 iTemp17 since their live ranges do not overlap.
9033 In addition the allocator also takes into consideration the type and usage
9034 of a temporary, for example itemp6 is a pointer to near space and is used
9035 as to fetch data from (i.e.
9036 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
9037 Some short lived temporaries are allocated to special registers which have
9038 meaning to the code generator e.g.
9039 iTemp13 is allocated to a pseudo register CC which tells the back end that
9040 the temporary is used only for a conditional jump the code generation makes
9041 use of this information to optimize a compare and jump ICode.
9043 There are several loop optimizations performed by the compiler.
9044 It can detect induction variables iTemp21(i) and iTemp23(j).
9045 Also note the compiler does selective strength reduction, i.e.
9046 the multiplication of an induction variable in line 18 (gint = j * 3) is
9047 changed to addition, a new temporary iTemp17 is allocated and assigned
9048 a initial value, a constant 3 is then added for each iteration of the loop.
9049 The compiler does not change the multiplication in line 17 however since
9050 the processor does support an 8 * 8 bit multiplication.
9052 Note the dead code elimination optimization eliminated the dead assignments
9053 in line 7 & 8 to I and sum respectively.
9060 Sample.c (5:1:0:0) _entry($9) :
9065 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
9070 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
9075 Sample.c(11:4:53:0) preHeaderLbl0($11) :
9080 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
9086 Sample.c(11:6:5:1) _whilecontinue_0($1) :
9091 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
9097 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9102 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
9108 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
9114 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
9120 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
9126 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
9127 * int}[r0] + 0x2 {short}
9132 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
9137 Sample.c(11:17:21:0)_whilebreak_0($3) :
9142 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9147 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9152 Sample.c(15:20:54:0)preHeaderLbl1($13) :
9157 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9162 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9167 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9172 Sample.c(15:24:26:1)_forcond_0($4) :
9177 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
9183 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9188 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
9189 + ITemp21 [lr21:38]{short}[r4]
9194 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
9200 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
9201 + iTemp15 [lr29:30]{short}[r1]
9206 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
9212 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
9218 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
9224 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
9230 Sample.c(19:38:47:1) goto _forcond_0($4)
9235 Sample.c(19:39:48:0)_forbreak_0($7) :
9240 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
9241 + ITemp11 [lr19:40]{short}[r3]
9246 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
9251 Sample.c(20:42:51:0)_return($8) :
9256 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
9262 Finally the code generated for this function:
9303 ; ----------------------------------------------
9313 ; ----------------------------------------------
9323 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
9335 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
9347 ;_whilecontinue_0($1) :
9357 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
9362 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
9421 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
9440 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
9487 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
9527 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
9553 ; iTemp6 [lr5:16]{_near * int}[r0] =
9558 ; iTemp6 [lr5:16]{_near * int}[r0] +
9575 ; goto _whilecontinue_0($1)
9587 ; _whilebreak_0($3) :
9597 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
9609 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
9621 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
9633 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
9652 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
9681 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
9686 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
9731 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
9736 ; iTemp21 [lr21:38]{short}[r4]
9762 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
9795 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
9800 ; iTemp15 [lr29:30]{short}[r1]
9819 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
9866 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
9913 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
9925 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
9939 cjne r5,#0xff,00104$
9951 ; goto _forcond_0($4)
9973 ; ret iTemp24 [lr40:41]{short}
10016 A few words about basic block successors, predecessors and dominators
10019 Successors are basic blocks that might execute after this basic block.
10021 Predecessors are basic blocks that might execute before reaching this basic
10024 Dominators are basic blocks that WILL execute before reaching this basic
10050 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
10053 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
10056 c) domVect of [BB4] = BB1 ...
10057 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
10065 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
10075 Thanks to all the other volunteer developers who have helped with coding,
10076 testing, web-page creation, distribution sets, etc.
10077 You know who you are :-)
10084 This document was initially written by Sandeep Dutta
10087 All product names mentioned herein may be trademarks of their respective
10093 \begin_inset LatexCommand \printindex{}