1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
6 pdftitle={SDCC Compiler User Guide},
7 pdfauthor={SDCC development team},
8 pdfsubject={installation, user manual},
9 pdfkeywords={8032, 8051, ansi, c, compiler, CPU, DS390,
10 embedded, GPL, manual, mcs51, PIC, small, Z80},
12 linkcolor=blue] {hyperref}
16 \inputencoding default
19 \paperfontsize default
26 \use_numerical_citations 0
27 \paperorientation portrait
34 \paragraph_separation indent
36 \quotes_language swedish
44 Please note: double dashed longoptions (e.g.
45 --version) are written this way: -
59 three consecutive dashes simply result in a long resp.
63 SDCC Compiler User Guide
69 The strings enclosed in $ are automatically updated by cvs:
74 cvs: $Revision$ $Date$
78 \begin_inset LatexCommand \tableofcontents{}
95 is a Freeware, retargettable, optimizing ANSI-C compiler by
99 designed for 8 bit Microprocessors.
100 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
102 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052 CPU}
106 , etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
107 It can be retargetted for other microprocessors, support for PIC, AVR and
108 186 is under development.
109 The entire source code for the compiler is distributed under GPL.
110 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
111 SDCC has extensive language extensions suitable for utilizing various microcont
112 rollers and underlying hardware effectively.
117 In addition to the MCU specific optimizations SDCC also does a host of standard
121 global sub expression elimination,
124 loop optimizations (loop invariant, strength reduction of induction variables
128 constant folding & propagation,
134 dead code elimination
144 For the back-end SDCC uses a global register allocation scheme which should
145 be well suited for other 8 bit MCUs.
150 The peep hole optimizer uses a rule based substitution mechanism which is
156 Supported data-types are:
159 char (8 bits, 1 byte),
162 short and int (16 bits, 2 bytes),
165 long (32 bit, 4 bytes)
172 The compiler also allows
174 inline assembler code
176 to be embedded anywhere in a function.
177 In addition, routines developed in assembly can also be called.
181 SDCC also provides an option (-
191 -cyclomatic) to report the relative complexity of a function.
192 These functions can then be further optimized, or hand coded in assembly
198 SDCC also comes with a companion source level debugger SDCDB, the debugger
199 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
204 The latest version can be downloaded from
205 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
215 Please note: the compiler will probably always be some steps ahead of this
220 \begin_inset LatexCommand \index{Status of documentation}
230 Obviously this has pros and cons
239 All packages used in this compiler system are
247 ; source code for all the sub-packages (pre-processor, assemblers, linkers
248 etc) is distributed with the package.
249 This documentation is maintained using a freeware word processor (LyX).
251 This program is free software; you can redistribute it and/or modify it
252 under the terms of the GNU General Public License
253 \begin_inset LatexCommand \index{GNU General Public License, GPL}
257 as published by the Free Software Foundation; either version 2, or (at
258 your option) any later version.
259 This program is distributed in the hope that it will be useful, but WITHOUT
260 ANY WARRANTY; without even the implied warranty
261 \begin_inset LatexCommand \index{warranty}
265 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
266 See the GNU General Public License for more details.
267 You should have received a copy of the GNU General Public License along
268 with this program; if not, write to the Free Software Foundation, 59 Temple
269 Place - Suite 330, Boston, MA 02111-1307, USA.
270 In other words, you are welcome to use, share and improve this program.
271 You are forbidden to forbid anyone else to use, share and improve what
273 Help stamp out software-hoarding!
276 Typographic conventions
277 \begin_inset LatexCommand \index{Typographic conventions}
284 Throughout this manual, we will use the following convention.
285 Commands you have to type in are printed in
293 Code samples are printed in
298 Interesting items and new terms are printed in
303 Compatibility with previous versions
306 This version has numerous bug fixes compared with the previous version.
307 But we also introduced some incompatibilities with older versions.
308 Not just for the fun of it, but to make the compiler more stable, efficient
310 \begin_inset LatexCommand \index{ANSI-compliance}
320 short is now equivalent to int (16 bits), it used to be equivalent to char
321 (8 bits) which is not ANSI compliant
324 the default directory for gcc-builds where include, library and documentation
325 files are stored is now in /usr/local/share
328 char type parameters to vararg functions are casted to int unless explicitly
345 will push a as an int and as a char resp.
358 -regextend has been removed
371 -noregparms has been removed
384 -stack-after-data has been removed
389 <pending: more incompatibilities?>
395 What do you need before you start installation of SDCC? A computer, and
397 The preferred method of installation is to compile SDCC from source using
399 For Windows some pre-compiled binary distributions are available for your
401 You should have some experience with command line tools and compiler use.
407 The SDCC home page at
408 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
412 is a great place to find distribution sets.
413 You can also find links to the user mailing lists that offer help or discuss
414 SDCC with other SDCC users.
415 Web links to other SDCC related sites can also be found here.
416 This document can be found in the DOC directory of the source package as
418 Some of the other tools (simulator and assembler) included with SDCC contain
419 their own documentation and can be found in the source distribution.
420 If you want the latest unreleased software, the complete source package
421 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
424 ANSI C reference / related tools / recommended reading / compiler building:
428 Wishes for the future
431 There are (and always will be) some things that could be done.
432 Here are some I can think of:
439 char KernelFunction3(char p) at 0x340;
447 \begin_inset LatexCommand \index{code banking (not supported)}
455 If you can think of some more, please see the chapter 9 about filing feature
457 \begin_inset LatexCommand \index{Requesting features}
462 \begin_inset LatexCommand \index{Feature request}
472 \begin_inset LatexCommand \index{Installation}
480 \begin_inset LatexCommand \index{Options SDCC configuration}
487 The install paths, search paths and other options are defined when running
489 The defaults can be overridden by:
491 \labelwidthstring 00.00.0000
503 -prefix see table below
505 \labelwidthstring 00.00.0000
517 -exec_prefix see table below
519 \labelwidthstring 00.00.0000
531 -bindir see table below
533 \labelwidthstring 00.00.0000
545 -datadir see table below
547 \labelwidthstring 00.00.0000
549 docdir environment variable, see table below
551 \labelwidthstring 00.00.0000
553 include_dir_suffix environment variable, see table below
555 \labelwidthstring 00.00.0000
557 lib_dir_suffix environment variable, see table below
559 \labelwidthstring 00.00.0000
561 sdccconf_h_dir_separator environment variable, either / or
566 This character will only be used in sdccconf.h; don't forget it's a C-header,
567 therefore a double-backslash is needed there.
569 \labelwidthstring 00.00.0000
581 -disable-mcs51-port Excludes the Intel mcs51 port
583 \labelwidthstring 00.00.0000
595 -disable-gbz80-port Excludes the Gameboy gbz80 port
597 \labelwidthstring 00.00.0000
609 -z80-port Excludes the z80 port
611 \labelwidthstring 00.00.0000
623 -disable-avr-port Excludes the AVR port
625 \labelwidthstring 00.00.0000
637 -disable-ds390-port Excludes the DS390 port
639 \labelwidthstring 00.00.0000
651 -disable-pic-port Excludes the PIC port
653 \labelwidthstring 00.00.0000
665 -disable-xa51-port Excludes the XA51 port
667 \labelwidthstring 00.00.0000
679 -disable-ucsim Disables configuring and building of ucsim
681 \labelwidthstring 00.00.0000
693 -disable-device-lib-build Disables automatically building device libraries
695 \labelwidthstring 00.00.0000
707 -disable-packihx Disables building packihx
709 \labelwidthstring 00.00.0000
721 -enable-libgc Use the Bohem memory allocator.
722 Lower runtime footprint.
725 Furthermore the environment variables CC, CFLAGS, ...
726 the tools and their arguments can be influenced.
727 Please see `configure -
737 -help` and the man/info pages of `configure` for details.
741 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
742 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
743 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
744 At the moment it's not possible to change the default settings (it was
745 simply never required.
749 These configure options are compiled into the binaries, and can only be
750 changed by rerunning 'configure' and recompiling SDCC.
751 The configure options are written in
755 to distinguish them from run time environment variables (see section search
761 \begin_inset Quotes sld
765 \begin_inset Quotes srd
768 are used by the SDCC team to build the official Win32 binaries.
769 The SDCC team uses Mingw32 to build the official Windows binaries, because
776 a gcc compiler and last but not least
779 the binaries can be built by cross compiling on Sourceforge's compile farm.
782 See the examples, how to pass the Win32 settings to 'configure'.
783 The other Win32 builds using Borland, VC or whatever don't use 'configure',
784 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
795 <lyxtabular version="3" rows="8" columns="3">
797 <column alignment="block" valignment="top" leftline="true" width="0in">
798 <column alignment="block" valignment="top" leftline="true" width="0in">
799 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
800 <row topline="true" bottomline="true">
801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
817 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
889 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
899 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
937 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
949 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
961 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
983 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1009 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1017 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1026 <row topline="true" bottomline="true">
1027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1037 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1045 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1064 'configure' also computes relative paths.
1065 This is needed for full relocatability of a binary package and to complete
1066 search paths (see section search paths below):
1071 \begin_inset Tabular
1072 <lyxtabular version="3" rows="4" columns="3">
1074 <column alignment="block" valignment="top" leftline="true" width="0in">
1075 <column alignment="block" valignment="top" leftline="true" width="0in">
1076 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1077 <row topline="true" bottomline="true">
1078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1103 <row topline="true" bottomline="true">
1104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1133 <row bottomline="true">
1134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1152 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1161 <row bottomline="true">
1162 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1180 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1202 ./configure --prefix=
1203 \begin_inset Quotes srd
1207 \begin_inset Quotes srd
1211 \begin_inset Quotes srd
1215 \begin_inset Quotes srd
1221 ./configure --disable-avr-port --disable-xa51-port
1224 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1234 \begin_inset Quotes srd
1237 i586-mingw32msvc-gcc
1238 \begin_inset Quotes srd
1242 \begin_inset Quotes srd
1245 i586-mingw32msvc-g++
1246 \begin_inset Quotes srd
1255 \begin_inset Quotes srd
1258 i586-mingw32msvc-ranlib
1259 \begin_inset Quotes srd
1268 \begin_inset Quotes srd
1271 i586-mingw32msvc-strip
1272 \begin_inset Quotes srd
1281 \begin_inset Quotes srd
1285 \begin_inset Quotes srd
1294 \begin_inset Quotes srd
1298 \begin_inset Quotes srd
1307 \begin_inset Quotes srd
1311 \begin_inset Quotes srd
1320 \begin_inset Quotes srd
1324 \begin_inset Quotes srd
1333 \begin_inset Quotes srd
1337 \begin_inset Quotes srd
1345 sdccconf_h_dir_separator=
1346 \begin_inset Quotes srd
1358 \begin_inset Quotes srd
1366 --disable-device-lib-build
1376 --host=i586-mingw32msvc --build=unknown-unknown-linux-gnu
1380 \begin_inset Quotes sld
1384 \begin_inset Quotes srd
1387 compile on Cygwin for Mingw32(see also sdcc/support/scripts/sdcc_cygwin_mingw32)
1397 \begin_inset Quotes srd
1401 \begin_inset Quotes srd
1410 \begin_inset Quotes srd
1414 \begin_inset Quotes srd
1423 \begin_inset Quotes srd
1427 \begin_inset Quotes srd
1436 \begin_inset Quotes srd
1440 \begin_inset Quotes srd
1449 \begin_inset Quotes srd
1453 \begin_inset Quotes srd
1462 \begin_inset Quotes srd
1466 \begin_inset Quotes srd
1475 \begin_inset Quotes srd
1479 \begin_inset Quotes srd
1487 sdccconf_h_dir_separator=
1488 \begin_inset Quotes srd
1500 \begin_inset Quotes srd
1511 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1522 -C' turns on caching, which gives a little bit extra speed.
1523 However if options are changed, it can be necessary to delete the config.cache
1528 \begin_inset LatexCommand \index{Install paths}
1536 Binary files (preprocessor, assembler and linker)
1540 \begin_inset Tabular
1541 <lyxtabular version="3" rows="2" columns="3">
1543 <column alignment="block" valignment="top" leftline="true" width="0in">
1544 <column alignment="block" valignment="top" leftline="true" width="0in">
1545 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1546 <row topline="true" bottomline="true">
1547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1563 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1572 <row topline="true" bottomline="true">
1573 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1591 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1617 \begin_inset Tabular
1618 <lyxtabular version="3" rows="2" columns="3">
1620 <column alignment="block" valignment="top" leftline="true" width="1.6in">
1621 <column alignment="block" valignment="top" leftline="true" width="0in">
1622 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1623 <row topline="true" bottomline="true">
1624 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1649 <row topline="true" bottomline="true">
1650 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
1662 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1667 /usr/local/share/sdcc/include
1670 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1696 is auto-appended by the compiler, e.g.
1697 small, large, z80, ds390 etc.)
1701 \begin_inset Tabular
1702 <lyxtabular version="3" rows="2" columns="3">
1704 <column alignment="block" valignment="top" leftline="true" width="0in">
1705 <column alignment="block" valignment="top" leftline="true" width="0in">
1706 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1707 <row topline="true" bottomline="true">
1708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1716 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1724 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1733 <row topline="true" bottomline="true">
1734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1741 $DATADIR/$LIB_DIR_SUFFIX
1744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1749 /usr/local/share/sdcc/lib
1752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1778 \begin_inset Tabular
1779 <lyxtabular version="3" rows="2" columns="3">
1781 <column alignment="block" valignment="top" leftline="true" width="0in">
1782 <column alignment="block" valignment="top" leftline="true" width="0in">
1783 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1784 <row topline="true" bottomline="true">
1785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1810 <row topline="true" bottomline="true">
1811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1826 /usr/local/share/sdcc/doc
1829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1850 The install paths can still be changed during `make install` with e.g.:
1853 make install prefix=$(HOME)/local/sdcc
1856 Of course this doesn't change the search paths compiled into the binaries.
1860 \begin_inset LatexCommand \index{Search path}
1867 Some search paths or parts of them are determined by configure variables
1872 , see section above).
1873 Further search paths are determined by environment variables during runtime.
1876 The paths searched when running the compiler are as follows (the first catch
1882 Binary files (preprocessor, assembler and linker)
1885 \begin_inset Tabular
1886 <lyxtabular version="3" rows="4" columns="3">
1888 <column alignment="block" valignment="top" leftline="true" width="0in">
1889 <column alignment="block" valignment="top" leftline="true" width="0in">
1890 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1891 <row topline="true" bottomline="true">
1892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1917 <row topline="true">
1918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1947 <row topline="true">
1948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1953 Path of argv[0] (if available)
1956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1973 <row topline="true" bottomline="true">
1974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1990 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2011 \begin_inset Tabular
2012 <lyxtabular version="3" rows="6" columns="3">
2014 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2015 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2016 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2017 <row topline="true" bottomline="true">
2018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2043 <row topline="true">
2044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2080 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2099 <row topline="true">
2100 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2125 <row topline="true">
2126 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2140 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2152 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2163 <row topline="true">
2164 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2245 <row topline="true" bottomline="true">
2246 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2262 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2267 /usr/local/share/sdcc/
2272 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2299 -nostdinc disables the last two search paths.
2306 With the exception of
2307 \begin_inset Quotes sld
2321 \begin_inset Quotes srd
2328 is auto-appended by the compiler (e.g.
2329 small, large, z80, ds390 etc.).
2333 \begin_inset Tabular
2334 <lyxtabular version="3" rows="6" columns="3">
2336 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2337 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2338 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2339 <row topline="true" bottomline="true">
2340 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2348 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2356 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2365 <row topline="true">
2366 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2402 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2421 <row topline="true">
2422 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2434 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2461 <row topline="true">
2462 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2473 $LIB_DIR_SUFFIX/<model>
2476 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2490 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2507 <row topline="true">
2508 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2523 $LIB_DIR_SUFFIX/<model>
2526 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2635 <row topline="true" bottomline="true">
2636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2645 $LIB_DIR_SUFFIX/<model>
2648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2653 /usr/local/share/sdcc/
2660 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2676 Don't delete any of the stray spaces in the table above without checking
2677 the HTML output (last line)!
2693 -nostdlib disables the last two search paths.
2697 \begin_inset LatexCommand \index{Building SDCC}
2702 \layout Subsubsection
2704 Building SDCC on Linux
2709 Download the source package
2711 either from the SDCC CVS repository or from the
2712 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2718 , it will be named something like sdcc
2731 Bring up a command line terminal, such as xterm.
2736 Unpack the file using a command like:
2739 "tar -xzf sdcc.src.tar.gz
2744 , this will create a sub-directory called sdcc with all of the sources.
2747 Change directory into the main SDCC directory, for example type:
2764 This configures the package for compilation on your system.
2780 All of the source packages will compile, this can take a while.
2796 This copies the binary executables, the include files, the libraries and
2797 the documentation to the install directories.
2798 \layout Subsubsection
2800 Building SDCC on OSX 2.x
2803 Follow the instruction for Linux.
2807 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2808 )) fails to compile SDCC.
2809 Fortunately there's also gcc 2.9.x installed, which works fine.
2810 This compiler can be selected by running 'configure' with:
2813 ./configure CC=gcc2 CXX=g++2
2814 \layout Subsubsection
2816 Cross compiling SDCC on Linux for Windows
2819 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2820 See section 'Configure Options'.
2821 \layout Subsubsection
2823 Building SDCC on Windows
2826 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2828 They use Unix-sockets, which are not available on Win32.
2829 \layout Subsubsection
2831 Windows Install Using a Binary Package
2834 Download the binary package and unpack it using your favorite unpacking
2835 tool (gunzip, WinZip, etc).
2836 This should unpack to a group of sub-directories.
2837 An example directory structure after unpacking the mingw32 package is:
2842 bin for the executables, c:
2850 lib for the include and libraries.
2853 Adjust your environment variable PATH to include the location of the bin
2854 directory or start sdcc using the full path.
2855 \layout Subsubsection
2857 Building SDCC using Cygwin and Mingw32
2860 For building and installing a Cygwin executable follow the instructions
2866 \begin_inset Quotes sld
2870 \begin_inset Quotes srd
2873 Win32-binary can be built, which will not need the Cygwin-DLL.
2874 For the necessary 'configure' options see section 'configure options' or
2875 the script 'sdcc/support/scripts/sdcc_cygwinmingw32'.
2879 In order to install Cygwin on Windows download setup.exe from
2880 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2886 \begin_inset Quotes sld
2889 default text file type
2890 \begin_inset Quotes srd
2894 \begin_inset Quotes sld
2898 \begin_inset Quotes srd
2901 and download/install at least the following packages.
2902 Some packages are selected by default, others will be automatically selected
2903 because of dependencies with the manually selected packages.
2904 Never deselect these packages!
2913 gcc ; version 3.x is fine, no need to use the old 2.9x
2916 binutils ; selected with gcc
2922 rxvt ; a nice console, which makes life much easier under windoze (see below)
2925 man ; not really needed for building SDCC, but you'll miss it sooner or
2929 less ; not really needed for building SDCC, but you'll miss it sooner or
2933 cvs ; only if you use CVS access
2936 If you want to develop something you'll need:
2939 python ; for the regression tests
2942 gdb ; the gnu debugger, together with the nice GUI
2943 \begin_inset Quotes sld
2947 \begin_inset Quotes srd
2953 openssh ; to access the CF or commit changes
2956 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
2957 use autoconf-stable!
2960 rxvt is a nice console with history.
2961 Replace in your cygwin.bat the line
2970 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
2973 -bg black -fg white -geometry 100x65 -e bash --login
2976 Text selected with the mouse is automatically copied to the clipboard, pasting
2977 works with shift-insert.
2981 The other good tip is to make sure you have no //c/-style paths anywhere,
2982 use /cygdrive/c/ instead.
2983 Using // invokes a network lookup which is very slow.
2985 \begin_inset Quotes sld
2989 \begin_inset Quotes srd
2992 is too long, you can change it with e.g.
2998 SDCC sources use the unix line ending LF.
2999 Life is much easier, if you store the source tree on a drive, which is
3000 mount in binary mode.
3001 And use an editor which can handle LF-only line endings.
3002 Make sure not to commit files with windows line endings.
3003 \layout Subsubsection
3005 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3010 Download the source package
3012 either from the SDCC CVS repository or from the
3013 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3019 , it will be named something like sdcc
3026 SDCC is distributed with all the projects, workspaces, and files you need
3027 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3028 doesn't build under MSVC).
3029 The workspace name is 'sdcc.dsw'.
3030 Please note that as it is now, all the executables are created in a folder
3034 Once built you need to copy the executables from sdcc
3038 bin before running SDCC.
3043 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3044 flex.exe, and gawk.exe.
3045 One good place to get them is
3046 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3054 Download the file UnxUtils
3055 \begin_inset LatexCommand \index{UnxUtils}
3060 Now you have to install the utilities and setup MSVC so it can locate the
3062 Here there are two alternatives (choose one!):
3069 a) Extract UnxUtils.zip to your C:
3071 hard disk PRESERVING the original paths, otherwise bison won't work.
3072 (If you are using WinZip make certain that 'Use folder names' is selected)
3076 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3077 in 'Show directories for:' select 'Executable files', and in the directories
3078 window add a new path: 'C:
3088 (As a side effect, you get a bunch of Unix utilities that could be useful,
3089 such as diff and patch.)
3096 This one avoids extracting a bunch of files you may not use, but requires
3101 a) Create a directory were to put the tools needed, or use a directory already
3109 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3110 to such directory WITHOUT preserving the original paths.
3111 (If you are using WinZip make certain that 'Use folder names' is not selected)
3115 c) Rename bison.exe to '_bison.exe'.
3119 d) Create a batch file 'bison.bat' in 'C:
3123 ' and add these lines:
3143 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3147 Steps 'c' and 'd' are needed because bison requires by default that the
3148 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3149 '/usr/local/share/' I think.
3150 So it is necessary to tell bison where those files are located if they
3151 are not in such directory.
3152 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3156 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3157 in 'Show directories for:' select 'Executable files', and in the directories
3158 window add a new path: 'c:
3161 Note that you can use any other path instead of 'c:
3163 util', even the path where the Visual C++ tools are, probably: 'C:
3167 Microsoft Visual Studio
3172 So you don't have to execute step 'e' :)
3176 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3177 the executables from sdcc
3181 bin, and you can compile using sdcc.
3182 \layout Subsubsection
3184 Building SDCC Using Borland
3187 From the sdcc directory, run the command "make -f Makefile.bcc".
3188 This should regenerate all the .exe files in the bin directory except for
3189 sdcdb.exe (which currently doesn't build under Borland C++).
3192 If you modify any source files and need to rebuild, be aware that the dependenci
3193 es may not be correctly calculated.
3194 The safest option is to delete all .obj files and run the build again.
3195 From a Cygwin BASH prompt, this can easily be done with the command:
3205 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3207 ) -print -exec rm {}
3216 or on Windows NT/2000/XP from the command prompt with the command:
3223 del /s *.obj *.lib *.rul
3226 from the sdcc directory.
3229 Building the Documentation
3236 Testing out the SDCC Compiler
3239 The first thing you should do after installing your SDCC compiler is to
3255 \begin_inset LatexCommand \index{version}
3262 at the prompt, and the program should run and tell you the version.
3263 If it doesn't run, or gives a message about not finding sdcc program, then
3264 you need to check over your installation.
3265 Make sure that the sdcc bin directory is in your executable search path
3266 defined by the PATH environment setting (see the Trouble-shooting section
3268 Make sure that the sdcc program is in the bin folder, if not perhaps something
3269 did not install correctly.
3277 is commonly installed as described in section
3278 \begin_inset Quotes sld
3281 Install and search paths
3282 \begin_inset Quotes srd
3291 Make sure the compiler works on a very simple example.
3292 Type in the following test.c program using your favorite
3318 Compile this using the following command:
3327 If all goes well, the compiler will generate a test.asm and test.rel file.
3328 Congratulations, you've just compiled your first program with SDCC.
3329 We used the -c option to tell SDCC not to link the generated code, just
3330 to keep things simple for this step.
3338 The next step is to try it with the linker.
3348 If all goes well the compiler will link with the libraries and produce
3349 a test.ihx output file.
3354 (no test.ihx, and the linker generates warnings), then the problem is most
3355 likely that sdcc cannot find the
3359 usr/local/share/sdcc/lib directory
3363 (see the Install trouble-shooting section for suggestions).
3371 The final test is to ensure sdcc can use the
3375 header files and libraries.
3376 Edit test.c and change it to the following:
3393 strcpy(str1, "testing");
3400 Compile this by typing
3407 This should generate a test.ihx output file, and it should give no warnings
3408 such as not finding the string.h file.
3409 If it cannot find the string.h file, then the problem is that sdcc cannot
3410 find the /usr/local/share/sdcc/include directory
3414 (see the Install trouble-shooting section for suggestions).
3417 Install Trouble-shooting
3418 \begin_inset LatexCommand \index{Install trouble-shooting}
3423 \layout Subsubsection
3425 SDCC does not build correctly.
3428 A thing to try is starting from scratch by unpacking the .tgz source package
3429 again in an empty directory.
3437 ./configure 2>&1 | tee configure.log
3451 make 2>&1 | tee make.log
3458 If anything goes wrong, you can review the log files to locate the problem.
3459 Or a relevant part of this can be attached to an email that could be helpful
3460 when requesting help from the mailing list.
3461 \layout Subsubsection
3464 \begin_inset Quotes sld
3468 \begin_inset Quotes srd
3475 \begin_inset Quotes sld
3479 \begin_inset Quotes srd
3482 command is a script that analyzes your system and performs some configuration
3483 to ensure the source package compiles on your system.
3484 It will take a few minutes to run, and will compile a few tests to determine
3485 what compiler features are installed.
3486 \layout Subsubsection
3489 \begin_inset Quotes sld
3493 \begin_inset Quotes srd
3499 This runs the GNU make tool, which automatically compiles all the source
3500 packages into the final installed binary executables.
3501 \layout Subsubsection
3504 \begin_inset Quotes sld
3508 \begin_inset Quotes erd
3514 This will install the compiler, other executables libraries and include
3515 files in to the appropriate directories.
3517 \begin_inset Quotes sld
3520 Install and Search PATHS
3521 \begin_inset Quotes srd
3526 On most systems you will need super-user privileges to do this.
3532 SDCC is not just a compiler, but a collection of tools by various developers.
3533 These include linkers, assemblers, simulators and other components.
3534 Here is a summary of some of the components.
3535 Note that the included simulator and assembler have separate documentation
3536 which you can find in the source package in their respective directories.
3537 As SDCC grows to include support for other processors, other packages from
3538 various developers are included and may have their own sets of documentation.
3542 You might want to look at the files which are installed in <installdir>.
3543 At the time of this writing, we find the following programs for gcc-builds:
3547 In <installdir>/bin:
3550 sdcc - The compiler.
3553 sdcpp - The C preprocessor.
3556 asx8051 - The assembler for 8051 type processors.
3563 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3566 aslink -The linker for 8051 type processors.
3573 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3576 s51 - The ucSim 8051 simulator.
3579 sdcdb - The source debugger.
3582 packihx - A tool to pack (compress) Intel hex files.
3585 In <installdir>/share/sdcc/include
3591 In <installdir>/share/sdcc/lib
3594 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3598 In <installdir>/share/sdcc/doc
3604 As development for other processors proceeds, this list will expand to include
3605 executables to support processors like AVR, PIC, etc.
3606 \layout Subsubsection
3611 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3612 the assembler and linkage editor.
3613 \layout Subsubsection
3616 \begin_inset LatexCommand \index{sdcpp}
3620 - The C-Preprocessor
3623 The preprocessor is a modified version of the GNU preprocessor.
3624 The C preprocessor is used to pull in #include sources, process #ifdef
3625 statements, #defines and so on.
3626 \layout Subsubsection
3628 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3632 This is retargettable assembler & linkage editor, it was developed by Alan
3634 John Hartman created the version for 8051, and I (Sandeep) have made some
3635 enhancements and bug fixes for it to work properly with the SDCC.
3636 \layout Subsubsection
3639 \begin_inset LatexCommand \index{s51}
3646 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3647 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3652 The simulator is built as part of the build process.
3653 For more information visit Daniel's web site at:
3654 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3659 It currently support the core mcs51, the Dallas DS80C390 and the Phillips
3661 \layout Subsubsection
3664 \begin_inset LatexCommand \index{sdcdb}
3668 - Source Level Debugger
3671 Sdcdb is the companion source level debugger.
3672 The current version of the debugger uses Daniel's Simulator S51, but can
3673 be easily changed to use other simulators.
3680 \layout Subsubsection
3682 Single Source File Projects
3685 For single source file 8051 projects the process is very simple.
3686 Compile your programs with the following command
3689 "sdcc sourcefile.c".
3693 This will compile, assemble and link your source file.
3694 Output files are as follows
3698 \begin_inset LatexCommand \index{.asm}
3703 \begin_inset LatexCommand \index{Assembler source}
3707 file created by the compiler
3711 \begin_inset LatexCommand \index{.lst}
3716 \begin_inset LatexCommand \index{Assembler listing}
3720 file created by the Assembler
3724 \begin_inset LatexCommand \index{.rst}
3729 \begin_inset LatexCommand \index{Assembler listing}
3733 file updated with linkedit information, created by linkage editor
3737 \begin_inset LatexCommand \index{.sym}
3742 \begin_inset LatexCommand \index{Symbol listing}
3746 for the sourcefile, created by the assembler
3750 \begin_inset LatexCommand \index{.rel}
3755 \begin_inset LatexCommand \index{Object file}
3759 created by the assembler, input to Linkage editor
3763 \begin_inset LatexCommand \index{.map}
3768 \begin_inset LatexCommand \index{Memory map}
3772 for the load module, created by the Linker
3776 \begin_inset LatexCommand \index{.mem}
3780 - A file with a summary of the memory usage
3784 \begin_inset LatexCommand \index{.ihx}
3788 - The load module in Intel hex format
3789 \begin_inset LatexCommand \index{Intel hex format}
3793 (you can select the Motorola S19 format
3794 \begin_inset LatexCommand \index{Motorola S19 format}
3809 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
3814 If you need another format you might want to use objdump
3815 \begin_inset LatexCommand \index{objdump}
3820 \begin_inset LatexCommand \index{srecord}
3828 \begin_inset LatexCommand \index{.adb}
3832 - An intermediate file containing debug information needed to create the
3844 \begin_inset LatexCommand \index{-\/-debug}
3852 \begin_inset LatexCommand \index{.cdb}
3856 - An optional file (with -
3866 -debug) containing debug information
3871 \begin_inset LatexCommand \index{. (no extension)}
3875 An optional AOMF51 file containing debug information (with -
3889 \begin_inset LatexCommand \index{.dump*}
3893 - Dump file to debug the compiler it self (with -
3903 -dumpall) (see section
3904 \begin_inset Quotes sld
3907 Anatomy of the compiler
3908 \begin_inset Quotes srd
3912 \layout Subsubsection
3914 Projects with Multiple Source Files
3917 SDCC can compile only ONE file at a time.
3918 Let us for example assume that you have a project containing the following
3923 foo1.c (contains some functions)
3925 foo2.c (contains some more functions)
3927 foomain.c (contains more functions and the function main)
3935 The first two files will need to be compiled separately with the commands:
3967 Then compile the source file containing the
3972 \begin_inset LatexCommand \index{Linker}
3976 the files together with the following command:
3984 foomain.c\SpecialChar ~
3985 foo1.rel\SpecialChar ~
3990 \begin_inset LatexCommand \index{.rel}
4002 can be separately compiled as well:
4013 sdcc foomain.rel foo1.rel foo2.rel
4020 The file containing the
4035 file specified in the command line, since the linkage editor processes
4036 file in the order they are presented to it.
4037 \layout Subsubsection
4039 Projects with Additional Libraries
4040 \begin_inset LatexCommand \index{Libraries}
4047 Some reusable routines may be compiled into a library, see the documentation
4048 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4052 \begin_inset LatexCommand \index{.lib}
4059 Libraries created in this manner can be included in the command line.
4060 Make sure you include the -L <library-path> option to tell the linker where
4061 to look for these files if they are not in the current directory.
4062 Here is an example, assuming you have the source file
4074 (if that is not the same as your current project):
4081 sdcc foomain.c foolib.lib -L mylib
4092 must be an absolute path name.
4096 The most efficient way to use libraries is to keep separate modules in separate
4098 The lib file now should name all the modules.rel files.
4099 For an example see the standard library file
4103 in the directory <installdir>/share/lib/small.
4106 Command Line Options
4107 \begin_inset LatexCommand \index{Command Line Options}
4112 \layout Subsubsection
4114 Processor Selection Options
4115 \begin_inset LatexCommand \index{Options processor selection}
4120 \begin_inset LatexCommand \index{Processor selection options}
4126 \labelwidthstring 00.00.0000
4131 \begin_inset LatexCommand \index{-mmcs51}
4137 Generate code for the MCS51
4138 \begin_inset LatexCommand \index{MCS51}
4142 family of processors.
4143 This is the default processor target.
4145 \labelwidthstring 00.00.0000
4150 \begin_inset LatexCommand \index{-mds390}
4156 Generate code for the DS80C390
4157 \begin_inset LatexCommand \index{DS80C390}
4163 \labelwidthstring 00.00.0000
4168 \begin_inset LatexCommand \index{-mds400}
4174 Generate code for the DS80C400
4175 \begin_inset LatexCommand \index{DS80C400}
4181 \labelwidthstring 00.00.0000
4186 \begin_inset LatexCommand \index{-mz80}
4192 Generate code for the Z80
4193 \begin_inset LatexCommand \index{Z80}
4197 family of processors.
4199 \labelwidthstring 00.00.0000
4204 \begin_inset LatexCommand \index{-mgbz80}
4210 Generate code for the GameBoy Z80
4211 \begin_inset LatexCommand \index{GameBoy Z80}
4217 \labelwidthstring 00.00.0000
4222 \begin_inset LatexCommand \index{-mavr}
4228 Generate code for the Atmel AVR
4229 \begin_inset LatexCommand \index{AVR}
4233 processor (In development, not complete).
4234 AVR users should probably have a look at avr-gcc
4235 \begin_inset LatexCommand \url[FIXME: official URL?]{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4242 I think it is fair to direct users there for now.
4243 Open source is also about avoiding unnecessary work .
4244 But I didn't find the 'official' link.
4246 \labelwidthstring 00.00.0000
4251 \begin_inset LatexCommand \index{-mpic14}
4257 Generate code for the PIC 14
4258 \begin_inset LatexCommand \index{PIC14}
4262 -bit processors (In development, not complete).
4265 p16f627 p16f628 p16f84 p16f873 p16f877?
4267 \labelwidthstring 00.00.0000
4273 Generate code for the Toshiba TLCS-900H
4274 \begin_inset LatexCommand \index{TLCS-900H}
4278 processor (In development, not complete).
4280 \labelwidthstring 00.00.0000
4285 \begin_inset LatexCommand \index{-mxa51}
4291 Generate code for the Phillips XA51
4292 \begin_inset LatexCommand \index{XA51}
4296 processor (In development, not complete).
4297 \layout Subsubsection
4299 Preprocessor Options
4300 \begin_inset LatexCommand \index{Options preprocessor}
4305 \begin_inset LatexCommand \index{Preprocessor options}
4311 \labelwidthstring 00.00.0000
4316 \begin_inset LatexCommand \index{-I<path>}
4322 The additional location where the pre processor will look for <..h> or
4323 \begin_inset Quotes eld
4327 \begin_inset Quotes erd
4332 \labelwidthstring 00.00.0000
4337 \begin_inset LatexCommand \index{-D<macro[=value]>}
4343 Command line definition of macros.
4344 Passed to the pre processor.
4346 \labelwidthstring 00.00.0000
4351 \begin_inset LatexCommand \index{-M}
4357 Tell the preprocessor to output a rule suitable for make describing the
4358 dependencies of each object file.
4359 For each source file, the preprocessor outputs one make-rule whose target
4360 is the object file name for that source file and whose dependencies are
4361 all the files `#include'd in it.
4362 This rule may be a single line or may be continued with `
4364 '-newline if it is long.
4365 The list of rules is printed on standard output instead of the preprocessed
4369 \labelwidthstring 00.00.0000
4374 \begin_inset LatexCommand \index{-C}
4380 Tell the preprocessor not to discard comments.
4381 Used with the `-E' option.
4383 \labelwidthstring 00.00.0000
4388 \begin_inset LatexCommand \index{-MM}
4399 Like `-M' but the output mentions only the user header files included with
4401 \begin_inset Quotes eld
4405 System header files included with `#include <file>' are omitted.
4407 \labelwidthstring 00.00.0000
4412 \begin_inset LatexCommand \index{-Aquestion(answer)}
4418 Assert the answer answer for question, in case it is tested with a preprocessor
4419 conditional such as `#if #question(answer)'.
4420 `-A-' disables the standard assertions that normally describe the target
4423 \labelwidthstring 00.00.0000
4429 (answer) Assert the answer answer for question, in case it is tested with
4430 a preprocessor conditional such as `#if #question(answer)'.
4431 `-A-' disables the standard assertions that normally describe the target
4434 \labelwidthstring 00.00.0000
4439 \begin_inset LatexCommand \index{-Umacro}
4445 Undefine macro macro.
4446 `-U' options are evaluated after all `-D' options, but before any `-include'
4447 and `-imacros' options.
4449 \labelwidthstring 00.00.0000
4454 \begin_inset LatexCommand \index{-dM}
4460 Tell the preprocessor to output only a list of the macro definitions that
4461 are in effect at the end of preprocessing.
4462 Used with the `-E' option.
4464 \labelwidthstring 00.00.0000
4469 \begin_inset LatexCommand \index{-dD}
4475 Tell the preprocessor to pass all macro definitions into the output, in
4476 their proper sequence in the rest of the output.
4478 \labelwidthstring 00.00.0000
4483 \begin_inset LatexCommand \index{-dN}
4494 Like `-dD' except that the macro arguments and contents are omitted.
4495 Only `#define name' is included in the output.
4496 \layout Subsubsection
4499 \begin_inset LatexCommand \index{Options linker}
4504 \begin_inset LatexCommand \index{Linker options}
4510 \labelwidthstring 00.00.0000
4530 \begin_inset LatexCommand \index{-\/-lib-path}
4535 \begin_inset LatexCommand \index{-L -\/-lib-path}
4544 <absolute path to additional libraries> This option is passed to the linkage
4545 editor's additional libraries
4546 \begin_inset LatexCommand \index{Libraries}
4551 The path name must be absolute.
4552 Additional library files may be specified in the command line.
4553 See section Compiling programs for more details.
4555 \labelwidthstring 00.00.0000
4572 \begin_inset LatexCommand \index{-\/-xram-loc}
4576 <Value> The start location of the external ram
4577 \begin_inset LatexCommand \index{xdata}
4581 , default value is 0.
4582 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4592 -xram-loc 0x8000 or -
4604 \labelwidthstring 00.00.0000
4621 \begin_inset LatexCommand \index{-\/-code-loc}
4625 <Value> The start location of the code
4626 \begin_inset LatexCommand \index{code}
4630 segment, default value 0.
4631 Note when this option is used the interrupt vector table is also relocated
4632 to the given address.
4633 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4643 -code-loc 0x8000 or -
4655 \labelwidthstring 00.00.0000
4672 \begin_inset LatexCommand \index{-\/-stack-loc}
4676 <Value> By default the stack
4677 \begin_inset LatexCommand \index{stack}
4681 is placed after the data segment.
4682 Using this option the stack can be placed anywhere in the internal memory
4684 The value entered can be in Hexadecimal or Decimal format, e.g.
4695 -stack-loc 0x20 or -
4706 Since the sp register is incremented before a push or call, the initial
4707 sp will be set to one byte prior the provided value.
4708 The provided value should not overlap any other memory areas such as used
4709 register banks or the data segment and with enough space for the current
4712 \labelwidthstring 00.00.0000
4729 \begin_inset LatexCommand \index{-\/-data-loc}
4733 <Value> The start location of the internal ram data
4734 \begin_inset LatexCommand \index{data}
4739 The value entered can be in Hexadecimal or Decimal format, eg.
4761 (By default, the start location of the internal ram data segment is set
4762 as low as possible in memory, taking into account the used register banks
4763 and the bit segment at address 0x20.
4764 For example if register banks 0 and 1 are used without bit variables, the
4765 data segment will be set, if -
4775 -data-loc is not used, to location 0x10.)
4777 \labelwidthstring 00.00.0000
4794 \begin_inset LatexCommand \index{-\/-idata-loc}
4798 <Value> The start location of the indirectly addressable internal ram
4799 \begin_inset LatexCommand \index{idata}
4803 , default value is 0x80.
4804 The value entered can be in Hexadecimal or Decimal format, eg.
4815 -idata-loc 0x88 or -
4827 \labelwidthstring 00.00.0000
4842 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
4851 The linker output (final object code) is in Intel Hex format.
4852 \begin_inset LatexCommand \index{Intel hex format}
4856 (This is the default option).
4858 \labelwidthstring 00.00.0000
4873 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4882 The linker output (final object code) is in Motorola S19 format
4883 \begin_inset LatexCommand \index{Motorola S19 format}
4888 \layout Subsubsection
4891 \begin_inset LatexCommand \index{Options MCS51}
4896 \begin_inset LatexCommand \index{MCS51 options}
4902 \labelwidthstring 00.00.0000
4917 \begin_inset LatexCommand \index{-\/-model-large}
4923 Generate code for Large model programs see section Memory Models for more
4925 If this option is used all source files in the project should be compiled
4927 In addition the standard library routines are compiled with small model,
4928 they will need to be recompiled.
4930 \labelwidthstring 00.00.0000
4945 \begin_inset LatexCommand \index{-\/-model-small}
4956 Generate code for Small Model programs see section Memory Models for more
4958 This is the default model.
4959 \layout Subsubsection
4962 \begin_inset LatexCommand \index{Options DS390}
4967 \begin_inset LatexCommand \index{DS390 options}
4973 \labelwidthstring 00.00.0000
4990 \begin_inset LatexCommand \index{-\/-model-flat24}
5000 Generate 24-bit flat mode code.
5001 This is the one and only that the ds390 code generator supports right now
5002 and is default when using
5007 See section Memory Models for more details.
5009 \labelwidthstring 00.00.0000
5026 \begin_inset LatexCommand \index{-\/-stack-10bit}
5030 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
5031 This is the one and only that the ds390 code generator supports right now
5032 and is default when using
5037 In this mode, the stack is located in the lower 1K of the internal RAM,
5038 which is mapped to 0x400000.
5039 Note that the support is incomplete, since it still uses a single byte
5040 as the stack pointer.
5041 This means that only the lower 256 bytes of the potential 1K stack space
5042 will actually be used.
5043 However, this does allow you to reclaim the precious 256 bytes of low RAM
5044 for use for the DATA and IDATA segments.
5045 The compiler will not generate any code to put the processor into 10 bit
5047 It is important to ensure that the processor is in this mode before calling
5048 any re-entrant functions compiled with this option.
5049 In principle, this should work with the
5062 \begin_inset LatexCommand \index{-\/-stack-auto}
5068 option, but that has not been tested.
5069 It is incompatible with the
5082 \begin_inset LatexCommand \index{-\/-xstack}
5089 It also only makes sense if the processor is in 24 bit contiguous addressing
5102 -model-flat24 option
5105 \layout Subsubsection
5107 Optimization Options
5108 \begin_inset LatexCommand \index{Options optimization}
5113 \begin_inset LatexCommand \index{Optimization options}
5119 \labelwidthstring 00.00.0000
5134 \begin_inset LatexCommand \index{-\/-nogcse}
5140 Will not do global subexpression elimination, this option may be used when
5141 the compiler creates undesirably large stack/data spaces to store compiler
5143 A warning message will be generated when this happens and the compiler
5144 will indicate the number of extra bytes it allocated.
5145 It recommended that this option NOT be used, #pragma\SpecialChar ~
5147 \begin_inset LatexCommand \index{\#pragma NOGCSE}
5151 can be used to turn off global subexpression elimination
5152 \begin_inset LatexCommand \index{Subexpression elimination}
5156 for a given function only.
5158 \labelwidthstring 00.00.0000
5173 \begin_inset LatexCommand \index{-\/-noinvariant}
5179 Will not do loop invariant optimizations, this may be turned off for reasons
5180 explained for the previous option.
5181 For more details of loop optimizations performed see section Loop Invariants.It
5182 recommended that this option NOT be used, #pragma\SpecialChar ~
5184 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
5188 can be used to turn off invariant optimizations for a given function only.
5190 \labelwidthstring 00.00.0000
5205 \begin_inset LatexCommand \index{-\/-noinduction}
5211 Will not do loop induction optimizations, see section strength reduction
5212 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
5215 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
5219 can be used to turn off induction optimizations for a given function only.
5221 \labelwidthstring 00.00.0000
5236 \begin_inset LatexCommand \index{-\/-nojtbound}
5247 Will not generate boundary condition check when switch statements
5248 \begin_inset LatexCommand \index{switch statement}
5252 are implemented using jump-tables.
5253 See section Switch Statements for more details.
5254 It is recommended that this option is NOT used, #pragma\SpecialChar ~
5256 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
5260 can be used to turn off boundary checking for jump tables for a given function
5263 \labelwidthstring 00.00.0000
5278 \begin_inset LatexCommand \index{-\/-noloopreverse}
5287 Will not do loop reversal
5288 \begin_inset LatexCommand \index{Loop reversing}
5294 \labelwidthstring 00.00.0000
5311 \begin_inset LatexCommand \index{-\/-nolabelopt }
5315 Will not optimize labels (makes the dumpfiles more readable).
5317 \labelwidthstring 00.00.0000
5332 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
5338 Will not memcpy initialized data in far space from code space.
5339 This saves a few bytes in code space if you don't have initialized data.
5340 \layout Subsubsection
5343 \begin_inset LatexCommand \index{Options other}
5349 \labelwidthstring 00.00.0000
5365 \begin_inset LatexCommand \index{-\/-compile-only}
5370 \begin_inset LatexCommand \index{-c -\/-compile-only}
5376 will compile and assemble the source, but will not call the linkage editor.
5378 \labelwidthstring 00.00.0000
5397 \begin_inset LatexCommand \index{-\/-c1mode}
5403 reads the preprocessed source from standard input and compiles it.
5404 The file name for the assembler output must be specified using the -o option.
5406 \labelwidthstring 00.00.0000
5411 \begin_inset LatexCommand \index{-E}
5417 Run only the C preprocessor.
5418 Preprocess all the C source files specified and output the results to standard
5421 \labelwidthstring 00.00.0000
5427 \begin_inset LatexCommand \index{-o <path/file>}
5433 The output path resp.
5434 file where everything will be placed.
5435 If the parameter is a path, it must have a trailing slash (or backslash
5436 for the Windows binaries) to be recognized as a path.
5439 \labelwidthstring 00.00.0000
5454 \begin_inset LatexCommand \index{-\/-stack-auto}
5465 All functions in the source file will be compiled as
5470 \begin_inset LatexCommand \index{reentrant}
5475 the parameters and local variables will be allocated on the stack
5476 \begin_inset LatexCommand \index{stack}
5481 see section Parameters and Local Variables for more details.
5482 If this option is used all source files in the project should be compiled
5486 \labelwidthstring 00.00.0000
5501 \begin_inset LatexCommand \index{-\/-xstack}
5507 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5508 variables and passing parameters.
5509 See section on external stack for more details.
5511 \labelwidthstring 00.00.0000
5526 \begin_inset LatexCommand \index{-\/-callee-saves}
5531 \begin_inset LatexCommand \index{function prologue}
5535 function1[,function2][,function3]....
5538 The compiler by default uses a caller saves convention for register saving
5539 across function calls, however this can cause unnecessary register pushing
5540 & popping when calling small functions from larger functions.
5541 This option can be used to switch the register saving convention for the
5542 function names specified.
5543 The compiler will not save registers when calling these functions, no extra
5544 code will be generated at the entry & exit for these functions to save
5545 & restore the registers used by these functions, this can SUBSTANTIALLY
5546 reduce code & improve run time performance of the generated code.
5547 In the future the compiler (with inter procedural analysis) will be able
5548 to determine the appropriate scheme to use for each function call.
5549 DO NOT use this option for built-in functions such as _mulint..., if this
5550 option is used for a library function the appropriate library function
5551 needs to be recompiled with the same option.
5552 If the project consists of multiple source files then all the source file
5553 should be compiled with the same -
5563 -callee-saves option string.
5564 Also see #pragma\SpecialChar ~
5566 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
5572 \labelwidthstring 00.00.0000
5587 \begin_inset LatexCommand \index{-\/-debug}
5596 When this option is used the compiler will generate debug information, that
5597 can be used with the SDCDB.
5598 The debug information is collected in a file with .cdb extension.
5599 For more information see documentation for SDCDB.
5601 \labelwidthstring 00.00.0000
5618 \begin_inset LatexCommand \index{-\/-peep-file}
5622 <filename> This option can be used to use additional rules to be used by
5623 the peep hole optimizer.
5624 See section Peep Hole optimizations for details on how to write these rules.
5626 \labelwidthstring 00.00.0000
5631 \begin_inset LatexCommand \index{-S}
5642 Stop after the stage of compilation proper; do not assemble.
5643 The output is an assembler code file for the input file specified.
5645 \labelwidthstring 00.00.0000
5649 -Wa_asmOption[,asmOption]
5652 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
5657 Pass the asmOption to the assembler.
5659 \labelwidthstring 00.00.0000
5663 -Wl_linkOption[,linkOption]
5666 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
5671 Pass the linkOption to the linker.
5673 \labelwidthstring 00.00.0000
5688 \begin_inset LatexCommand \index{-\/-int-long-reent}
5694 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
5695 Note by default these libraries are compiled as non-reentrant.
5696 See section Installation for more details.
5698 \labelwidthstring 00.00.0000
5713 \begin_inset LatexCommand \index{-\/-cyclomatic}
5722 This option will cause the compiler to generate an information message for
5723 each function in the source file.
5724 The message contains some
5728 information about the function.
5729 The number of edges and nodes the compiler detected in the control flow
5730 graph of the function, and most importantly the
5732 cyclomatic complexity
5733 \begin_inset LatexCommand \index{Cyclomatic complexity}
5739 see section on Cyclomatic Complexity for more details.
5741 \labelwidthstring 00.00.0000
5756 \begin_inset LatexCommand \index{-\/-float-reent}
5765 Floating point library is compiled as reentrant
5766 \begin_inset LatexCommand \index{reentrant}
5770 .See section Installation for more details.
5772 \labelwidthstring 00.00.0000
5787 \begin_inset LatexCommand \index{-\/-nooverlay}
5793 The compiler will not overlay parameters and local variables of any function,
5794 see section Parameters and local variables for more details.
5796 \labelwidthstring 00.00.0000
5811 \begin_inset LatexCommand \index{-\/-main-return}
5817 This option can be used when the code generated is called by a monitor
5819 The compiler will generate a 'ret' upon return from the 'main'
5820 \begin_inset LatexCommand \index{main return}
5825 The default option is to lock up i.e.
5828 \labelwidthstring 00.00.0000
5843 \begin_inset LatexCommand \index{-\/-no-peep}
5849 Disable peep-hole optimization.
5851 \labelwidthstring 00.00.0000
5866 \begin_inset LatexCommand \index{-\/-peep-asm}
5872 Pass the inline assembler code through the peep hole optimizer.
5873 This can cause unexpected changes to inline assembler code, please go through
5874 the peephole optimizer
5875 \begin_inset LatexCommand \index{Peephole optimizer}
5879 rules defined in the source file tree '<target>/peeph.def' before using
5882 \labelwidthstring 00.00.0000
5899 \begin_inset LatexCommand \index{-\/-iram-size<Value>}
5903 Causes the linker to check if the internal ram usage is within limits of
5906 \labelwidthstring 00.00.0000
5923 \begin_inset LatexCommand \index{-\/-xram-size<Value>}
5927 Causes the linker to check if the external ram usage is within limits of
5930 \labelwidthstring 00.00.0000
5947 \begin_inset LatexCommand \index{-\/-code-size<Value>}
5951 Causes the linker to check if the code usage is within limits of the given
5954 \labelwidthstring 00.00.0000
5969 \begin_inset LatexCommand \index{-\/-nostdincl}
5975 This will prevent the compiler from passing on the default include path
5976 to the preprocessor.
5978 \labelwidthstring 00.00.0000
5993 \begin_inset LatexCommand \index{-\/-nostdlib}
5999 This will prevent the compiler from passing on the default library
6000 \begin_inset LatexCommand \index{Libraries}
6006 \labelwidthstring 00.00.0000
6021 \begin_inset LatexCommand \index{-\/-verbose}
6027 Shows the various actions the compiler is performing.
6029 \labelwidthstring 00.00.0000
6034 \begin_inset LatexCommand \index{-V}
6040 Shows the actual commands the compiler is executing.
6042 \labelwidthstring 00.00.0000
6057 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
6063 Hides your ugly and inefficient c-code from the asm file, so you can always
6064 blame the compiler :).
6066 \labelwidthstring 00.00.0000
6081 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
6087 Include i-codes in the asm file.
6088 Sounds like noise but is most helpful for debugging the compiler itself.
6090 \labelwidthstring 00.00.0000
6105 \begin_inset LatexCommand \index{-\/-less-pedantic}
6111 Disable some of the more pedantic warnings (jwk burps: please be more specific
6114 \labelwidthstring 00.00.0000
6129 \begin_inset LatexCommand \index{-\/-print-search-dirs}
6135 Display the directories in the compiler's search path
6136 \layout Subsubsection
6138 Intermediate Dump Options
6139 \begin_inset LatexCommand \index{Options intermediate dump}
6144 \begin_inset LatexCommand \index{Intermediate dump options}
6151 The following options are provided for the purpose of retargetting and debugging
6153 These provided a means to dump the intermediate code (iCode
6154 \begin_inset LatexCommand \index{iCode}
6158 ) generated by the compiler in human readable form at various stages of
6159 the compilation process.
6162 \labelwidthstring 00.00.0000
6177 \begin_inset LatexCommand \index{-\/-dumpraw}
6183 This option will cause the compiler to dump the intermediate code into
6186 <source filename>.dumpraw
6188 just after the intermediate code has been generated for a function, i.e.
6189 before any optimizations are done.
6191 \begin_inset LatexCommand \index{Basic blocks}
6195 at this stage ordered in the depth first number, so they may not be in
6196 sequence of execution.
6198 \labelwidthstring 00.00.0000
6213 \begin_inset LatexCommand \index{-\/-dumpgcse}
6219 Will create a dump of iCode's, after global subexpression elimination
6220 \begin_inset LatexCommand \index{Global subexpression elimination}
6226 <source filename>.dumpgcse.
6228 \labelwidthstring 00.00.0000
6243 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
6249 Will create a dump of iCode's, after deadcode elimination
6250 \begin_inset LatexCommand \index{Dead-code elimination}
6256 <source filename>.dumpdeadcode.
6258 \labelwidthstring 00.00.0000
6273 \begin_inset LatexCommand \index{-\/-dumploop}
6282 Will create a dump of iCode's, after loop optimizations
6283 \begin_inset LatexCommand \index{Loop optimization}
6289 <source filename>.dumploop.
6291 \labelwidthstring 00.00.0000
6306 \begin_inset LatexCommand \index{-\/-dumprange}
6315 Will create a dump of iCode's, after live range analysis
6316 \begin_inset LatexCommand \index{Live range analysis}
6322 <source filename>.dumprange.
6324 \labelwidthstring 00.00.0000
6339 \begin_inset LatexCommand \index{-\/-dumlrange}
6345 Will dump the life ranges
6346 \begin_inset LatexCommand \index{Live range analysis}
6352 \labelwidthstring 00.00.0000
6367 \begin_inset LatexCommand \index{-\/-dumpregassign}
6376 Will create a dump of iCode's, after register assignment
6377 \begin_inset LatexCommand \index{Register assignment}
6383 <source filename>.dumprassgn.
6385 \labelwidthstring 00.00.0000
6400 \begin_inset LatexCommand \index{-\/-dumplrange}
6406 Will create a dump of the live ranges of iTemp's
6408 \labelwidthstring 00.00.0000
6423 \begin_inset LatexCommand \index{-\/-dumpall}
6434 Will cause all the above mentioned dumps to be created.
6437 Environment variables
6438 \begin_inset LatexCommand \index{Environment variables}
6445 SDCC recognizes the following environment variables:
6447 \labelwidthstring 00.00.0000
6452 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
6458 SDCC installs a signal handler
6459 \begin_inset LatexCommand \index{signal handler}
6463 to be able to delete temporary files after an user break (^C) or an exception.
6464 If this environment variable is set, SDCC won't install the signal handler
6465 in order to be able to debug SDCC.
6467 \labelwidthstring 00.00.0000
6472 \begin_inset LatexCommand \index{TMP}
6478 \begin_inset LatexCommand \index{TEMP}
6484 \begin_inset LatexCommand \index{TMPDIR}
6490 Path, where temporary files will be created.
6491 The order of the variables is the search order.
6492 In a standard *nix environment these variables are not set, and there's
6493 no need to set them.
6494 On Windows it's recommended to set one of them.
6496 \labelwidthstring 00.00.0000
6501 \begin_inset LatexCommand \index{SDCC\_HOME}
6508 \begin_inset Quotes sld
6511 2.3 Install and search paths
6512 \begin_inset Quotes srd
6517 \labelwidthstring 00.00.0000
6522 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
6529 \begin_inset Quotes sld
6532 2.3 Install and search paths
6533 \begin_inset Quotes srd
6538 \labelwidthstring 00.00.0000
6543 \begin_inset LatexCommand \index{SDCC\_LIB}
6550 \begin_inset Quotes sld
6553 2.3 Install and search paths
6554 \begin_inset Quotes srd
6560 There are some more environment variables recognized by SDCC, but these
6561 are solely used for debugging purposes.
6562 They can change or disappear very quickly, and will never be documented.
6565 MCS51/DS390 Storage Class
6566 \begin_inset LatexCommand \index{Storage class}
6573 In addition to the ANSI storage classes SDCC allows the following MCS51
6574 specific storage classes.
6575 \layout Subsubsection
6578 \begin_inset LatexCommand \index{xdata}
6585 Variables declared with this storage class will be placed in the extern
6591 storage class for Large Memory model, e.g.:
6597 xdata unsigned char xduc;
6598 \layout Subsubsection
6601 \begin_inset LatexCommand \index{data}
6612 storage class for Small Memory model.
6613 Variables declared with this storage class will be allocated in the internal
6620 \layout Subsubsection
6623 \begin_inset LatexCommand \index{idata}
6630 Variables declared with this storage class will be allocated into the indirectly
6631 addressable portion of the internal ram of a 8051, e.g.:
6637 \layout Subsubsection
6640 \begin_inset LatexCommand \index{bit}
6647 This is a data-type and a storage class specifier.
6648 When a variable is declared as a bit, it is allocated into the bit addressable
6649 memory of 8051, e.g.:
6655 \layout Subsubsection
6658 \begin_inset LatexCommand \index{sfr}
6663 \begin_inset LatexCommand \index{sbit}
6670 Like the bit keyword,
6674 signifies both a data-type and storage class, they are used to describe
6675 the special function registers and special bit variables of a 8051, eg:
6680 sfr at 0x80 P0; /* special function register P0 at location 0x80 */
6682 sbit at 0xd7 CY; /* CY (Carry Flag
6683 \begin_inset LatexCommand \index{Flags}
6688 \begin_inset LatexCommand \index{Carry flag}
6696 \begin_inset LatexCommand \index{Pointers}
6703 SDCC allows (via language extensions) pointers to explicitly point to any
6704 of the memory spaces
6705 \begin_inset LatexCommand \index{Memory model}
6710 In addition to the explicit pointers, the compiler uses (by default) generic
6711 pointers which can be used to point to any of the memory spaces.
6715 Pointer declaration examples:
6720 /* pointer physically in xternal ram pointing to object in internal ram
6723 data unsigned char * xdata p;
6727 /* pointer physically in code rom pointing to data in xdata space */
6729 xdata unsigned char * code p;
6733 /* pointer physically in code space pointing to data in code space */
6735 code unsigned char * code p;
6739 /* the following is a generic pointer physically located in xdata space
6745 Well you get the idea.
6750 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
6763 The highest order byte of the
6767 pointers contains the data space information.
6768 Assembler support routines are called whenever data is stored or retrieved
6774 These are useful for developing reusable library
6775 \begin_inset LatexCommand \index{Libraries}
6780 Explicitly specifying the pointer type will generate the most efficient
6785 \begin_inset LatexCommand \index{Parameters}
6790 \begin_inset LatexCommand \index{Local variable}
6797 Automatic (local) variables and parameters to functions can either be placed
6798 on the stack or in data-space.
6799 The default action of the compiler is to place these variables in the internal
6800 RAM (for small model) or external RAM (for large model).
6801 This in fact makes them
6804 \begin_inset LatexCommand \index{static}
6810 so by default functions are non-reentrant
6811 \begin_inset LatexCommand \index{reentrant}
6819 They can be placed on the stack
6820 \begin_inset LatexCommand \index{stack}
6837 \begin_inset LatexCommand \index{-\/-stack-auto}
6843 option or by using the
6846 \begin_inset LatexCommand \index{reentrant}
6852 keyword in the function declaration, e.g.:
6857 unsigned char foo(char i) reentrant
6867 Since stack space on 8051 is limited, the
6885 option should be used sparingly.
6886 Note that the reentrant keyword just means that the parameters & local
6887 variables will be allocated to the stack, it
6891 mean that the function is register bank independent.
6895 Local variables can be assigned storage classes and absolute
6896 \begin_inset LatexCommand \index{Absolute addressing}
6905 unsigned char foo() {
6911 xdata unsigned char i;
6923 data at 0x31 unsigned char j;
6935 In the above example the variable
6939 will be allocated in the external ram,
6943 in bit addressable space and
6962 or when a function is declared as
6966 this should only be done for static variables.
6969 Parameters however are not allowed any storage class, (storage classes for
6970 parameters will be ignored), their allocation is governed by the memory
6971 model in use, and the reentrancy options.
6975 \begin_inset LatexCommand \index{Overlaying}
6983 \begin_inset LatexCommand \index{reentrant}
6987 functions SDCC will try to reduce internal ram space usage by overlaying
6988 parameters and local variables of a function (if possible).
6989 Parameters and local variables of a function will be allocated to an overlayabl
6990 e segment if the function has
6992 no other function calls and the function is non-reentrant and the memory
6994 \begin_inset LatexCommand \index{Memory model}
7001 If an explicit storage class
7002 \begin_inset LatexCommand \index{Storage class}
7006 is specified for a local variable, it will NOT be overlayed.
7009 Note that the compiler (not the linkage editor) makes the decision for overlayin
7011 Functions that are called from an interrupt service routine should be preceded
7012 by a #pragma\SpecialChar ~
7014 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7018 if they are not reentrant.
7021 Also note that the compiler does not do any processing of inline
7022 \begin_inset LatexCommand \index{inline}
7026 assembler code, so the compiler might incorrectly assign local variables
7027 and parameters of a function into the overlay segment if the inline assembler
7028 code calls other c-functions that might use the overlay.
7029 In that case the #pragma\SpecialChar ~
7030 NOOVERLAY should be used.
7033 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
7035 \begin_inset LatexCommand \index{Multiplication}
7040 \begin_inset LatexCommand \index{Division}
7044 will NOT be overlayed since these are implemented using external functions,
7053 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7059 void set_error(unsigned char errcd)
7075 void some_isr () interrupt
7076 \begin_inset LatexCommand \index{interrupt}
7081 \begin_inset LatexCommand \index{using}
7111 In the above example the parameter
7119 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
7121 not present, this could cause unpredictable runtime behavior when called
7123 The #pragma\SpecialChar ~
7124 NOOVERLAY ensures that the parameters and local variables for
7125 the function are NOT overlayed.
7128 Interrupt Service Routines
7131 SDCC allows interrupt service routines to be coded in C, with some extended
7137 void timer_isr (void) interrupt 2 using 1
7147 The number following the
7150 \begin_inset LatexCommand \index{interrupt}
7156 keyword is the interrupt number this routine will service.
7157 The compiler will insert a call to this routine in the interrupt vector
7158 table for the interrupt number specified.
7163 keyword is used to tell the compiler to use the specified register bank
7164 (8051 specific) when generating code for this function.
7165 Note that when some function is called from an interrupt service routine
7166 it should be preceded by a #pragma\SpecialChar ~
7168 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7172 if it is not reentrant.
7173 A special note here, int (16 bit) and long (32 bit) integer division
7174 \begin_inset LatexCommand \index{Division}
7179 \begin_inset LatexCommand \index{Multiplication}
7184 \begin_inset LatexCommand \index{Modulus}
7188 operations are implemented using external support routines developed in
7189 ANSI-C, if an interrupt service routine needs to do any of these operations
7190 then the support routines (as mentioned in a following section) will have
7191 to be recompiled using the
7204 \begin_inset LatexCommand \index{-\/-stack-auto}
7210 option and the source file will need to be compiled using the
7225 \begin_inset LatexCommand \index{-\/-int-long-rent}
7232 If you have multiple source files in your project, interrupt service routines
7233 can be present in any of them, but a prototype of the isr MUST be present
7234 or included in the file that contains the function
7241 Interrupt Numbers and the corresponding address & descriptions for the Standard
7242 8051 are listed below.
7243 SDCC will automatically adjust the interrupt vector table to the maximum
7244 interrupt number specified.
7250 \begin_inset Tabular
7251 <lyxtabular version="3" rows="6" columns="3">
7253 <column alignment="block" valignment="top" leftline="true" width="0in">
7254 <column alignment="block" valignment="top" leftline="true" width="0in">
7255 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
7256 <row topline="true" bottomline="true">
7257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7282 <row topline="true">
7283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7308 <row topline="true">
7309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7334 <row topline="true">
7335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7351 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7360 <row topline="true">
7361 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7377 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7386 <row topline="true" bottomline="true">
7387 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7395 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7403 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7420 If the interrupt service routine is defined without
7423 \begin_inset LatexCommand \index{using}
7429 a register bank or with register bank 0 (using 0), the compiler will save
7430 the registers used by itself on the stack upon entry and restore them at
7431 exit, however if such an interrupt service routine calls another function
7432 then the entire register bank will be saved on the stack.
7433 This scheme may be advantageous for small interrupt service routines which
7434 have low register usage.
7437 If the interrupt service routine is defined to be using a specific register
7442 are save and restored, if such an interrupt service routine calls another
7443 function (using another register bank) then the entire register bank of
7444 the called function will be saved on the stack.
7445 This scheme is recommended for larger interrupt service routines.
7448 Calling other functions from an interrupt service routine is not recommended,
7449 avoid it if possible.
7453 Also see the _naked modifier.
7461 <TODO: this isn't implemented at all!>
7467 A special keyword may be associated with a function declaring it as
7472 SDCC will generate code to disable all interrupts upon entry to a critical
7473 function and enable them back before returning.
7474 Note that nesting critical functions may cause unpredictable results.
7492 The critical attribute maybe used with other attributes like
7498 \begin_inset LatexCommand \index{Naked functions}
7505 A special keyword may be associated with a function declaring it as
7508 \begin_inset LatexCommand \index{\_naked}
7519 function modifier attribute prevents the compiler from generating prologue
7520 \begin_inset LatexCommand \index{function prologue}
7525 \begin_inset LatexCommand \index{function epilogue}
7529 code for that function.
7530 This means that the user is entirely responsible for such things as saving
7531 any registers that may need to be preserved, selecting the proper register
7532 bank, generating the
7536 instruction at the end, etc.
7537 Practically, this means that the contents of the function must be written
7538 in inline assembler.
7539 This is particularly useful for interrupt functions, which can have a large
7540 (and often unnecessary) prologue/epilogue.
7541 For example, compare the code generated by these two functions:
7546 data unsigned char counter;
7548 void simpleInterrupt(void) interrupt
7549 \begin_inset LatexCommand \index{interrupt}
7567 void nakedInterrupt(void) interrupt 2 _naked
7576 \begin_inset LatexCommand \index{\_asm}
7605 ; MUST explicitly include ret in _naked function.
7612 \begin_inset LatexCommand \index{\_endasm}
7621 For an 8051 target, the generated simpleInterrupt looks like:
7762 whereas nakedInterrupt looks like:
7786 ; MUST explicitly include ret(i) in _naked function.
7789 While there is nothing preventing you from writing C code inside a _naked
7790 function, there are many ways to shoot yourself in the foot doing this,
7791 and it is recommended that you stick to inline assembler.
7794 Functions using private banks
7795 \begin_inset LatexCommand \index{bank}
7805 \begin_inset LatexCommand \index{using}
7811 attribute (which tells the compiler to use a register bank other than the
7812 default bank zero) should only be applied to
7815 \begin_inset LatexCommand \index{interrupt}
7821 functions (see note 1 below).
7822 This will in most circumstances make the generated ISR code more efficient
7823 since it will not have to save registers on the stack.
7830 attribute will have no effect on the generated code for a
7834 function (but may occasionally be useful anyway
7840 possible exception: if a function is called ONLY from 'interrupt' functions
7841 using a particular bank, it can be declared with the same 'using' attribute
7842 as the calling 'interrupt' functions.
7843 For instance, if you have several ISRs using bank one, and all of them
7844 call memcpy(), it might make sense to create a specialized version of memcpy()
7845 'using 1', since this would prevent the ISR from having to save bank zero
7846 to the stack on entry and switch to bank zero before calling the function
7853 (pending: I don't think this has been done yet)
7860 function using a non-zero bank will assume that it can trash that register
7861 bank, and will not save it.
7862 Since high-priority interrupts
7863 \begin_inset LatexCommand \index{interrupt priority}
7867 can interrupt low-priority ones on the 8051 and friends, this means that
7868 if a high-priority ISR
7872 a particular bank occurs while processing a low-priority ISR
7876 the same bank, terrible and bad things can happen.
7877 To prevent this, no single register bank should be
7881 by both a high priority and a low priority ISR.
7882 This is probably most easily done by having all high priority ISRs use
7883 one bank and all low priority ISRs use another.
7884 If you have an ISR which can change priority at runtime, you're on your
7885 own: I suggest using the default bank zero and taking the small performance
7889 It is most efficient if your ISR calls no other functions.
7890 If your ISR must call other functions, it is most efficient if those functions
7891 use the same bank as the ISR (see note 1 below); the next best is if the
7892 called functions use bank zero.
7893 It is very inefficient to call a function using a different, non-zero bank
7899 \begin_inset LatexCommand \index{Absolute addressing}
7906 Data items can be assigned an absolute address with the
7909 \begin_inset LatexCommand \index{at}
7915 keyword, in addition to a storage class, e.g.:
7921 \begin_inset LatexCommand \index{xdata}
7926 \begin_inset LatexCommand \index{at}
7930 0x8000 unsigned char PORTA_8255 ;
7933 In the above example the PORTA_8255 will be allocated to the location 0x8000
7934 of the external ram.
7935 Note that this feature is provided to give the programmer access to
7939 devices attached to the controller.
7940 The compiler does not actually reserve any space for variables declared
7941 in this way (they are implemented with an equate in the assembler).
7942 Thus it is left to the programmer to make sure there are no overlaps with
7943 other variables that are declared without the absolute address.
7944 The assembler listing file (.lst
7945 \begin_inset LatexCommand \index{.lst}
7949 ) and the linker output files (.rst
7950 \begin_inset LatexCommand \index{.rst}
7955 \begin_inset LatexCommand \index{.map}
7959 ) are a good places to look for such overlaps.
7963 Absolute address can be specified for variables in all storage classes,
7970 \begin_inset LatexCommand \index{bit}
7975 \begin_inset LatexCommand \index{at}
7982 The above example will allocate the variable at offset 0x02 in the bit-addressab
7984 There is no real advantage to assigning absolute addresses to variables
7985 in this manner, unless you want strict control over all the variables allocated.
7986 One possible use would be to write hardware portable code.
7987 For example, if you have a routine that uses one or more of the microcontroller
7988 I/O pins, and such pins are different for two different hardwares, you
7989 can declare the I/O pins in you routine using
8002 void DS1306_put(unsigned char value)
8010 unsigned char mask=0x80;
8034 SDI=(value & mask)?1:0;
8075 Then, someplace in the code for the first hardware you would use
8080 bit at 0x80 SDI;\SpecialChar ~
8084 /*I/O port 0, bit 0*/
8086 bit at 0x81 SCLK;\SpecialChar ~
8089 /*I/O port 0, bit 1*/
8091 bit CPOL;\SpecialChar ~
8102 /*This is a variable, let the linker allocate this one*/
8105 Similarly, for the second hardware you would use
8110 bit at 0x83 SDI;\SpecialChar ~
8114 /*I/O port 0, bit 3*/
8116 bit at 0x91 SCLK;\SpecialChar ~
8119 /*I/O port 1, bit 1*/
8121 bit CPOL;\SpecialChar ~
8132 /*This is a variable, let the linker allocate this one*/
8135 and you can use the same hardware dependent routine without changes, as
8136 for example in a library.
8137 This is somehow similar to sbit, but only one absolute address has to be
8138 specified in the whole project.
8142 \begin_inset LatexCommand \index{Startup code}
8149 The compiler inserts a call to the C routine
8151 _sdcc_external_startup()
8152 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
8161 at the start of the CODE area.
8162 This routine is in the runtime library
8163 \begin_inset LatexCommand \index{Runtime library}
8168 By default this routine returns 0, if this routine returns a non-zero value,
8169 the static & global variable initialization will be skipped and the function
8170 main will be invoked Other wise static & global variables will be initialized
8171 before the function main is invoked.
8174 _sdcc_external_startup()
8176 routine to your program to override the default if you need to setup hardware
8177 or perform some other critical operation prior to static & global variable
8181 Inline Assembler Code
8182 \begin_inset LatexCommand \index{Assembler routines}
8189 SDCC allows the use of in-line assembler with a few restriction as regards
8191 All labels defined within inline assembler code
8199 where nnnn is a number less than 100 (which implies a limit of utmost 100
8200 inline assembler labels
8208 It is strongly recommended that each assembly instruction (including labels)
8209 be placed in a separate line (as the example shows).
8223 \begin_inset LatexCommand \index{-\/-peep-asm}
8229 command line option is used, the inline assembler code will be passed through
8230 the peephole optimizer
8231 \begin_inset LatexCommand \index{Peephole optimizer}
8236 This might cause some unexpected changes in the inline assembler code.
8237 Please go through the peephole optimizer rules defined in file
8241 carefully before using this option.
8247 \begin_inset LatexCommand \index{\_asm}
8277 \begin_inset LatexCommand \index{\_endasm}
8284 The inline assembler code can contain any valid code understood by the assembler
8285 , this includes any assembler directives and comment lines.
8286 The compiler does not do any validation of the code within the
8296 Inline assembler code cannot reference any C-Labels, however it can reference
8298 \begin_inset LatexCommand \index{Labels}
8302 defined by the inline assembler, e.g.:
8327 ; some assembler code
8347 /* some more c code */
8349 clabel:\SpecialChar ~
8351 /* inline assembler cannot reference this label */
8363 $0003: ;label (can be reference by inline assembler only)
8375 /* some more c code */
8380 In other words inline assembly code can access labels defined in inline
8381 assembly within the scope of the function.
8382 The same goes the other way, ie.
8383 labels defines in inline assembly CANNOT be accessed by C statements.
8387 \begin_inset LatexCommand \index{int (16 bit)}
8392 \begin_inset LatexCommand \index{long (32 bit)}
8399 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
8400 multiplication and modulus operations are implemented by support routines.
8401 These support routines are all developed in ANSI-C to facilitate porting
8402 to other MCUs, although some model specific assembler optimizations are
8404 The following files contain the described routine, all of them can be found
8405 in <installdir>/share/sdcc/lib.
8410 \begin_inset Tabular
8411 <lyxtabular version="3" rows="11" columns="2">
8413 <column alignment="center" valignment="top" leftline="true" width="0(null)">
8414 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0(null)">
8415 <row topline="true" bottomline="true">
8416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8437 <row topline="true">
8438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8451 16 bit multiplication
8455 <row topline="true">
8456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8469 signed 16 bit division (calls _divuint)
8473 <row topline="true">
8474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8487 unsigned 16 bit division
8491 <row topline="true">
8492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8505 signed 16 bit modulus (calls _moduint)
8509 <row topline="true">
8510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8523 unsigned 16 bit modulus
8527 <row topline="true">
8528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8536 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8541 32 bit multiplication
8545 <row topline="true">
8546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8559 signed 32 division (calls _divulong)
8563 <row topline="true">
8564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8572 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8577 unsigned 32 division
8581 <row topline="true">
8582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8595 signed 32 bit modulus (calls _modulong)
8599 <row topline="true" bottomline="true">
8600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8608 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8613 unsigned 32 bit modulus
8629 Since they are compiled as
8634 \begin_inset LatexCommand \index{reentrant}
8639 \begin_inset LatexCommand \index{interrupt}
8643 service routines should not do any of the above operations.
8644 If this is unavoidable then the above routines will need to be compiled
8658 \begin_inset LatexCommand \index{-\/-stack-auto}
8664 option, after which the source program will have to be compiled with
8677 \begin_inset LatexCommand \index{-\/-int-long-rent}
8684 Notice that you don't have to call this routines directly.
8685 The compiler will use them automatically every time a integer operation
8689 Floating Point Support
8690 \begin_inset LatexCommand \index{Floating point support}
8697 SDCC supports IEEE (single precision 4bytes) floating point numbers.The floating
8698 point support routines are derived from gcc's floatlib.c and consists of
8699 the following routines:
8706 \begin_inset Tabular
8707 <lyxtabular version="3" rows="17" columns="2">
8709 <column alignment="center" valignment="top" leftline="true" width="0(null)">
8710 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0(null)">
8711 <row topline="true" bottomline="true">
8712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8738 <row topline="true">
8739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8770 add floating point numbers
8774 <row topline="true">
8775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8806 subtract floating point numbers
8810 <row topline="true">
8811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8828 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8842 divide floating point numbers
8846 <row topline="true">
8847 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8878 multiply floating point numbers
8882 <row topline="true">
8883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8900 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8914 convert floating point to unsigned char
8918 <row topline="true">
8919 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8950 convert floating point to signed char
8954 <row topline="true">
8955 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8986 convert floating point to unsigned int
8990 <row topline="true">
8991 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9022 convert floating point to signed int
9026 <row topline="true">
9027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9053 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9067 convert floating point to unsigned long
9071 <row topline="true">
9072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9089 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9103 convert floating point to signed long
9107 <row topline="true">
9108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9125 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9139 convert unsigned char to floating point
9143 <row topline="true">
9144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9175 convert char to floating point number
9179 <row topline="true">
9180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9197 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9211 convert unsigned int to floating point
9215 <row topline="true">
9216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9233 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9247 convert int to floating point numbers
9251 <row topline="true">
9252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9269 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9283 convert unsigned long to floating point number
9287 <row topline="true" bottomline="true">
9288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9319 convert long to floating point number
9333 Note if all these routines are used simultaneously the data space might
9335 For serious floating point usage it is strongly recommended that the large
9337 Also notice that you don't have to call this routines directly.
9338 The compiler will use them automatically every time a floating point operation
9343 \begin_inset LatexCommand \index{Memory model}
9348 \begin_inset LatexCommand \index{MCS51 memory}
9355 SDCC allows two memory models for MCS51 code, small and large.
9356 Modules compiled with different memory models should
9360 be combined together or the results would be unpredictable.
9361 The library routines supplied with the compiler are compiled as both small
9363 The compiled library modules are contained in separate directories as small
9364 and large so that you can link to either set.
9368 When the large model is used all variables declared without a storage class
9369 will be allocated into the external ram, this includes all parameters and
9370 local variables (for non-reentrant
9371 \begin_inset LatexCommand \index{reentrant}
9376 When the small model is used variables without storage class are allocated
9377 in the internal ram.
9380 Judicious usage of the processor specific storage classes
9381 \begin_inset LatexCommand \index{Storage class}
9385 and the 'reentrant' function type will yield much more efficient code,
9386 than using the large model.
9387 Several optimizations are disabled when the program is compiled using the
9388 large model, it is therefore strongly recommended that the small model
9389 be used unless absolutely required.
9393 \begin_inset LatexCommand \index{Memory model}
9398 \begin_inset LatexCommand \index{DS390 memory model}
9405 The only model supported is Flat 24
9406 \begin_inset LatexCommand \index{Flat 24 (memory model)}
9411 This generates code for the 24 bit contiguous addressing mode of the Dallas
9413 In this mode, up to four meg of external RAM or code space can be directly
9415 See the data sheets at www.dalsemi.com for further information on this part.
9419 In older versions of the compiler, this option was used with the MCS51 code
9425 Now, however, the '390 has it's own code generator, selected by the
9434 Note that the compiler does not generate any code to place the processor
9435 into 24 bitmode (although
9439 in the ds390 libraries will do that for you).
9445 \begin_inset LatexCommand \index{Tinibios (DS390)}
9449 , the boot loader or similar code must ensure that the processor is in 24
9450 bit contiguous addressing mode before calling the SDCC startup code.
9468 option, variables will by default be placed into the XDATA segment.
9473 Segments may be placed anywhere in the 4 meg address space using the usual
9485 Note that if any segments are located above 64K, the -r flag must be passed
9486 to the linker to generate the proper segment relocations, and the Intel
9487 HEX output format must be used.
9488 The -r flag can be passed to the linker by using the option
9492 on the sdcc command line.
9493 However, currently the linker can not handle code segments > 64k.
9496 Defines Created by the Compiler
9497 \begin_inset LatexCommand \index{Defines created by the compiler}
9504 The compiler creates the following #defines
9505 \begin_inset LatexCommand \index{\#defines}
9513 \begin_inset LatexCommand \index{SDCC}
9517 - this Symbol is always defined.
9521 \begin_inset LatexCommand \index{SDCC\_mcs51}
9526 \begin_inset LatexCommand \index{SDCC\_ds390}
9531 \begin_inset LatexCommand \index{SDCC\_z80}
9535 , etc - depending on the model used (e.g.: -mds390)
9539 \begin_inset LatexCommand \index{\_\_mcs51}
9544 \begin_inset LatexCommand \index{\_\_ds390}
9549 \begin_inset LatexCommand \index{\_\_z80}
9553 , etc - depending on the model used (e.g.
9558 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
9562 - this symbol is defined when
9580 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
9602 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
9624 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
9646 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
9658 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
9673 \begin_inset LatexCommand \index{Optimizations}
9680 SDCC performs a host of standard optimizations in addition to some MCU specific
9683 \layout Subsubsection
9685 Sub-expression Elimination
9686 \begin_inset LatexCommand \index{Subexpression elimination}
9693 The compiler does local and global common subexpression elimination, e.g.:
9704 will be translated to
9716 Some subexpressions are not as obvious as the above example, e.g.:
9726 In this case the address arithmetic a->b[i] will be computed only once;
9727 the equivalent code in C would be.
9739 The compiler will try to keep these temporary variables in registers.
9740 \layout Subsubsection
9742 Dead-Code Elimination
9743 \begin_inset LatexCommand \index{Dead-code elimination}
9762 i = 1; \SpecialChar ~
9767 global = 1;\SpecialChar ~
9780 global = 3;\SpecialChar ~
9791 int global; void f ()
9804 \layout Subsubsection
9807 \begin_inset LatexCommand \index{Copy propagation}
9863 Note: the dead stores created by this copy propagation will be eliminated
9864 by dead-code elimination.
9865 \layout Subsubsection
9868 \begin_inset LatexCommand \index{Loop optimization}
9875 Two types of loop optimizations are done by SDCC loop invariant lifting
9876 and strength reduction of loop induction variables.
9877 In addition to the strength reduction the optimizer marks the induction
9878 variables and the register allocator tries to keep the induction variables
9879 in registers for the duration of the loop.
9880 Because of this preference of the register allocator
9881 \begin_inset LatexCommand \index{Register allocation}
9885 , loop induction optimization causes an increase in register pressure, which
9886 may cause unwanted spilling of other temporary variables into the stack
9887 \begin_inset LatexCommand \index{stack}
9892 The compiler will generate a warning message when it is forced to allocate
9893 extra space either on the stack or data space.
9894 If this extra space allocation is undesirable then induction optimization
9895 can be eliminated either for the entire source file (with -
9905 -noinduction option) or for a given function only using #pragma\SpecialChar ~
9907 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
9920 for (i = 0 ; i < 100 ; i ++)
9934 for (i = 0; i < 100; i++)
9941 As mentioned previously some loop invariants are not as apparent, all static
9942 address computations are also moved out of the loop.
9947 \begin_inset LatexCommand \index{Strength reduction}
9951 , this optimization substitutes an expression by a cheaper expression:
9956 for (i=0;i < 100; i++)
9972 for (i=0;i< 100;i++) {
9976 ar[itemp1] = itemp2;
9989 The more expensive multiplication
9990 \begin_inset LatexCommand \index{Multiplication}
9994 is changed to a less expensive addition.
9995 \layout Subsubsection
9998 \begin_inset LatexCommand \index{Loop reversing}
10005 This optimization is done to reduce the overhead of checking loop boundaries
10006 for every iteration.
10007 Some simple loops can be reversed and implemented using a
10008 \begin_inset Quotes eld
10011 decrement and jump if not zero
10012 \begin_inset Quotes erd
10016 SDCC checks for the following criterion to determine if a loop is reversible
10017 (note: more sophisticated compilers use data-dependency analysis to make
10018 this determination, SDCC uses a more simple minded analysis).
10021 The 'for' loop is of the form
10027 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
10037 The <for body> does not contain
10038 \begin_inset Quotes eld
10042 \begin_inset Quotes erd
10046 \begin_inset Quotes erd
10052 All goto's are contained within the loop.
10055 No function calls within the loop.
10058 The loop control variable <sym> is not assigned any value within the loop
10061 The loop control variable does NOT participate in any arithmetic operation
10065 There are NO switch statements in the loop.
10066 \layout Subsubsection
10068 Algebraic Simplifications
10071 SDCC does numerous algebraic simplifications, the following is a small sub-set
10072 of these optimizations.
10077 i = j + 0 ; /* changed to */ i = j;
10079 i /= 2; /* changed to */ i >>= 1;
10081 i = j - j ; /* changed to */ i = 0;
10083 i = j / 1 ; /* changed to */ i = j;
10086 Note the subexpressions
10087 \begin_inset LatexCommand \index{Subexpression}
10091 given above are generally introduced by macro expansions or as a result
10092 of copy/constant propagation.
10093 \layout Subsubsection
10095 'switch' Statements
10096 \begin_inset LatexCommand \index{switch statement}
10103 SDCC changes switch statements to jump tables
10104 \begin_inset LatexCommand \index{jump tables}
10108 when the following conditions are true.
10112 The case labels are in numerical sequence, the labels need not be in order,
10113 and the starting number need not be one or zero.
10119 switch(i) {\SpecialChar ~
10223 Both the above switch statements will be implemented using a jump-table.
10227 The number of case labels is at least three, since it takes two conditional
10228 statements to handle the boundary conditions.
10231 The number of case labels is less than 84, since each label takes 3 bytes
10232 and a jump-table can be utmost 256 bytes long.
10236 Switch statements which have gaps in the numeric sequence or those that
10237 have more that 84 case labels can be split into more than one switch statement
10238 for efficient code generation, e.g.:
10288 If the above switch statement is broken down into two switch statements
10327 case 9: \SpecialChar ~
10343 case 12:\SpecialChar ~
10350 then both the switch statements will be implemented using jump-tables whereas
10351 the unmodified switch statement will not be.
10352 \layout Subsubsection
10354 Bit-shifting Operations
10355 \begin_inset LatexCommand \index{Bit shifting}
10362 Bit shifting is one of the most frequently used operation in embedded programmin
10364 SDCC tries to implement bit-shift operations in the most efficient way
10380 generates the following code:
10394 In general SDCC will never setup a loop if the shift count is known.
10426 Note that SDCC stores numbers in little-endian format (i.e.
10427 lowest order first).
10428 \layout Subsubsection
10431 \begin_inset LatexCommand \index{Bit rotation}
10438 A special case of the bit-shift operation is bit rotation, SDCC recognizes
10439 the following expression to be a left bit-rotation:
10449 i = ((i << 1) | (i >> 7));
10458 will generate the following code:
10470 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
10471 ns of this case will also be recognized as bit-rotation, i.e.:
10476 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
10477 \layout Subsubsection
10480 \begin_inset LatexCommand \index{Highest Order Bit}
10487 It is frequently required to obtain the highest order bit of an integral
10488 type (long, int, short or char types).
10489 SDCC recognizes the following expression to yield the highest order bit
10490 and generates optimized code for it, e.g.:
10510 hob = (gint >> 15) & 1;
10520 will generate the following code:
10553 000A E5*01\SpecialChar ~
10579 000C 33\SpecialChar ~
10608 000D E4\SpecialChar ~
10637 000E 13\SpecialChar ~
10666 000F F5*02\SpecialChar ~
10693 Variations of this case however will
10698 It is a standard C expression, so I heartily recommend this be the only
10699 way to get the highest order bit, (it is portable).
10700 Of course it will be recognized even if it is embedded in other expressions,
10706 xyz = gint + ((gint >> 15) & 1);
10709 will still be recognized.
10710 \layout Subsubsection
10713 \begin_inset LatexCommand \index{Peephole optimizer}
10720 The compiler uses a rule based, pattern matching and re-writing mechanism
10721 for peep-hole optimization.
10726 a peep-hole optimizer by Christopher W.
10727 Fraser (cwfraser@microsoft.com).
10728 A default set of rules are compiled into the compiler, additional rules
10729 may be added with the
10742 \begin_inset LatexCommand \index{-\/-peep-file}
10749 The rule language is best illustrated with examples.
10773 The above rule will change the following assembly
10774 \begin_inset LatexCommand \index{Assembler routines}
10796 Note: All occurrences of a
10800 (pattern variable) must denote the same string.
10801 With the above rule, the assembly sequence:
10811 will remain unmodified.
10815 Other special case optimizations may be added by the user (via
10831 some variants of the 8051 MCU allow only
10840 The following two rules will change all
10859 replace { lcall %1 } by { acall %1 }
10861 replace { ljmp %1 } by { ajmp %1 }
10866 inline-assembler code
10868 is also passed through the peep hole optimizer, thus the peephole optimizer
10869 can also be used as an assembly level macro expander.
10870 The rules themselves are MCU dependent whereas the rule language infra-structur
10871 e is MCU independent.
10872 Peephole optimization rules for other MCU can be easily programmed using
10877 The syntax for a rule is as follows:
10882 rule := replace [ restart ] '{' <assembly sequence> '
10920 <assembly sequence> '
10938 '}' [if <functionName> ] '
10943 <assembly sequence> := assembly instruction (each instruction including
10944 labels must be on a separate line).
10948 The optimizer will apply to the rules one by one from the top in the sequence
10949 of their appearance, it will terminate when all rules are exhausted.
10950 If the 'restart' option is specified, then the optimizer will start matching
10951 the rules again from the top, this option for a rule is expensive (performance)
10952 , it is intended to be used in situations where a transformation will trigger
10953 the same rule again.
10954 An example of this (not a good one, it has side effects) is the following
10977 Note that the replace pattern cannot be a blank, but can be a comment line.
10978 Without the 'restart' option only the inner most 'pop' 'push' pair would
10979 be eliminated, i.e.:
11009 the restart option the rule will be applied again to the resulting code
11010 and then all the pop-push pairs will be eliminated to yield:
11020 A conditional function can be attached to a rule.
11021 Attaching rules are somewhat more involved, let me illustrate this with
11048 The optimizer does a look-up of a function name table defined in function
11053 in the source file SDCCpeeph.c, with the name
11058 If it finds a corresponding entry the function is called.
11059 Note there can be no parameters specified for these functions, in this
11064 is crucial, since the function
11068 expects to find the label in that particular variable (the hash table containin
11069 g the variable bindings is passed as a parameter).
11070 If you want to code more such functions, take a close look at the function
11071 labelInRange and the calling mechanism in source file SDCCpeeph.c.
11072 I know this whole thing is a little kludgey, but maybe some day we will
11073 have some better means.
11074 If you are looking at this file, you will also see the default rules that
11075 are compiled into the compiler, you can add your own rules in the default
11076 set there if you get tired of specifying the -
11090 \begin_inset LatexCommand \index{Pragmas}
11097 SDCC supports the following #pragma directives.
11101 \begin_inset LatexCommand \index{\#pragma SAVE}
11105 - this will save all current options to the SAVE/RESTORE stack.
11110 \begin_inset LatexCommand \index{\#pragma RESTORE}
11114 - will restore saved options from the last save.
11115 SAVEs & RESTOREs can be nested.
11116 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
11117 RESTORE pulls current options from the stack.
11122 \begin_inset LatexCommand \index{\#pragma NOGCSE}
11126 - will stop global subexpression elimination.
11130 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
11134 - will stop loop induction optimizations.
11138 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
11142 - will not generate code for boundary value checking, when switch statements
11143 are turned into jump-tables.
11147 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
11151 - the compiler will not overlay the parameters and local variables of a
11156 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
11160 - the compiler will not warn you anymore for obvious mistakes, you'r on
11165 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
11169 - Will not do loop reversal optimization
11173 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11177 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
11179 \begin_inset LatexCommand \index{push/pop}
11183 instruction in ISR function (using interrupt
11184 \begin_inset LatexCommand \index{interrupt}
11189 The directive should be placed immediately before the ISR function definition
11190 and it affects ALL ISR functions following it.
11191 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
11192 EXCLUDE\SpecialChar ~
11194 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
11202 \begin_inset LatexCommand \index{\#pragma NOIV}
11206 - Do not generate interrupt vector table entries for all ISR functions
11207 defined after the pragma.
11208 This is useful in cases where the interrupt vector table must be defined
11209 manually, or when there is a secondary, manually defined interrupt vector
11211 for the autovector feature of the Cypress EZ-USB FX2).
11215 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11220 \begin_inset LatexCommand \index{function prologue}
11224 function1[,function2[,function3...]] - The compiler by default uses a caller
11225 saves convention for register saving across function calls, however this
11226 can cause unnecessary register pushing & popping when calling small functions
11227 from larger functions.
11228 This option can be used to switch off the register saving convention for
11229 the function names specified.
11230 The compiler will not save registers when calling these functions, extra
11231 code need to be manually inserted at the entry & exit for these functions
11232 to save & restore the registers used by these functions, this can SUBSTANTIALLY
11233 reduce code & improve run time performance of the generated code.
11234 In the future the compiler (with inter procedural analysis) may be able
11235 to determine the appropriate scheme to use for each function call.
11246 -callee-saves command line option is used, the function names specified
11247 in #pragma\SpecialChar ~
11249 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
11253 is appended to the list of functions specified in the command line.
11256 The pragma's are intended to be used to turn-off certain optimizations which
11257 might cause the compiler to generate extra stack / data space to store
11258 compiler generated temporary variables.
11259 This usually happens in large functions.
11260 Pragma directives should be used as shown in the following example, they
11261 are used to control options & optimizations for a given function; pragmas
11262 should be placed before and/or after a function, placing pragma's inside
11263 a function body could have unpredictable results.
11269 \begin_inset LatexCommand \index{\#pragma SAVE}
11273 /* save the current settings */
11276 \begin_inset LatexCommand \index{\#pragma NOGCSE}
11280 /* turnoff global subexpression elimination */
11282 #pragma NOINDUCTION
11283 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
11287 /* turn off induction optimizations */
11310 \begin_inset LatexCommand \index{\#pragma RESTORE}
11314 /* turn the optimizations back on */
11317 The compiler will generate a warning message when extra space is allocated.
11318 It is strongly recommended that the SAVE and RESTORE pragma's be used when
11319 changing options for a function.
11324 <pending: this is messy and incomplete>
11329 Compiler support routines (_gptrget, _mulint etc)
11332 Stdclib functions (puts, printf, strcat etc)
11335 Math functions (sin, pow, sqrt etc)
11338 license statements for the libraries are missing
11341 Interfacing with Assembly Routines
11342 \begin_inset LatexCommand \index{Assembler routines}
11347 \layout Subsubsection
11349 Global Registers used for Parameter Passing
11350 \begin_inset LatexCommand \index{Parameter passing}
11357 The compiler always uses the global registers
11360 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
11365 \begin_inset LatexCommand \index{B (register)}
11374 \begin_inset LatexCommand \index{ACC}
11380 to pass the first parameter to a routine.
11381 The second parameter onwards is either allocated on the stack (for reentrant
11392 -stack-auto is used) or in the internal / external ram (depending on the
11395 \layout Subsubsection
11397 Assembler Routine(non-reentrant
11398 \begin_inset LatexCommand \index{reentrant}
11403 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
11410 In the following example the function c_func calls an assembler routine
11411 asm_func, which takes two parameters.
11416 extern int asm_func(unsigned char, unsigned char);
11420 int c_func (unsigned char i, unsigned char j)
11428 return asm_func(i,j);
11442 return c_func(10,9);
11447 The corresponding assembler function is:
11452 .globl _asm_func_PARM_2
11516 add a,_asm_func_PARM_2
11541 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
11558 Note here that the return values are placed in 'dpl' - One byte return value,
11559 'dpl' LSB & 'dph' MSB for two byte values.
11560 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
11561 b' & 'acc' for four byte values.
11564 The parameter naming convention is _<function_name>_PARM_<n>, where n is
11565 the parameter number starting from 1, and counting from the left.
11566 The first parameter is passed in
11567 \begin_inset Quotes eld
11571 \begin_inset Quotes erd
11574 for One bye parameter,
11575 \begin_inset Quotes eld
11579 \begin_inset Quotes erd
11583 \begin_inset Quotes eld
11587 \begin_inset Quotes erd
11590 for three bytes and
11591 \begin_inset Quotes eld
11595 \begin_inset Quotes erd
11598 for four bytes, the variable name for the second parameter will be _<function_n
11603 Assemble the assembler routine with the following command:
11610 asx8051 -losg asmfunc.asm
11617 Then compile and link the assembler routine to the C source file with the
11625 sdcc cfunc.c asmfunc.rel
11626 \layout Subsubsection
11628 Assembler Routine(reentrant
11629 \begin_inset LatexCommand \index{reentrant}
11634 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
11641 In this case the second parameter onwards will be passed on the stack, the
11642 parameters are pushed from right to left i.e.
11643 after the call the left most parameter will be on the top of the stack.
11644 Here is an example:
11649 extern int asm_func(unsigned char, unsigned char);
11653 int c_func (unsigned char i, unsigned char j) reentrant
11661 return asm_func(i,j);
11675 return c_func(10,9);
11680 The corresponding assembler routine is:
11786 The compiling and linking procedure remains the same, however note the extra
11787 entry & exit linkage required for the assembler code, _bp is the stack
11788 frame pointer and is used to compute the offset into the stack for parameters
11789 and local variables.
11793 \begin_inset LatexCommand \index{stack}
11798 \begin_inset LatexCommand \index{External stack}
11805 The external stack is located at the start of the external ram segment,
11806 and is 256 bytes in size.
11817 -xstack option is used to compile the program, the parameters and local
11818 variables of all reentrant functions are allocated in this area.
11819 This option is provided for programs with large stack space requirements.
11820 When used with the -
11830 -stack-auto option, all parameters and local variables are allocated on
11831 the external stack (note support libraries will need to be recompiled with
11835 The compiler outputs the higher order address byte of the external ram segment
11836 into PORT P2, therefore when using the External Stack option, this port
11837 MAY NOT be used by the application program.
11841 \begin_inset LatexCommand \index{ANSI-compliance}
11848 Deviations from the compliance:
11851 functions are not always reentrant.
11854 structures cannot be assigned values directly, cannot be passed as function
11855 parameters or assigned to each other and cannot be a return value from
11882 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
11893 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
11915 return rets;/* is invalid in SDCC although allowed in ANSI */
11922 \begin_inset LatexCommand \index{long long (not supported)}
11927 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
11935 \begin_inset LatexCommand \index{double (not supported)}
11939 ' precision floating point
11940 \begin_inset LatexCommand \index{Floating point support}
11947 No support for setjmp and longjmp (for now).
11951 \begin_inset LatexCommand \index{K\&R style}
11955 function declarations are NOT allowed.
11961 foo(i,j) /* this old style of function declarations */
11963 int i,j; /* are valid in ANSI but not valid in SDCC */
11978 functions declared as pointers must be dereferenced during the call.
11989 /* has to be called like this */
11991 (*foo)(); /* ANSI standard allows calls to be made like 'foo()' */
11995 Cyclomatic Complexity
11996 \begin_inset LatexCommand \index{Cyclomatic complexity}
12003 Cyclomatic complexity of a function is defined as the number of independent
12004 paths the program can take during execution of the function.
12005 This is an important number since it defines the number test cases you
12006 have to generate to validate the function.
12007 The accepted industry standard for complexity number is 10, if the cyclomatic
12008 complexity reported by SDCC exceeds 10 you should think about simplification
12009 of the function logic.
12010 Note that the complexity level is not related to the number of lines of
12011 code in a function.
12012 Large functions can have low complexity, and small functions can have large
12018 SDCC uses the following formula to compute the complexity:
12023 complexity = (number of edges in control flow graph) - (number of nodes
12024 in control flow graph) + 2;
12028 Having said that the industry standard is 10, you should be aware that in
12029 some cases it be may unavoidable to have a complexity level of less than
12031 For example if you have switch statement with more than 10 case labels,
12032 each case label adds one to the complexity level.
12033 The complexity level is by no means an absolute measure of the algorithmic
12034 complexity of the function, it does however provide a good starting point
12035 for which functions you might look at for further optimization.
12041 Here are a few guidelines that will help the compiler generate more efficient
12042 code, some of the tips are specific to this compiler others are generally
12043 good programming practice.
12046 Use the smallest data type to represent your data-value.
12047 If it is known in advance that the value is going to be less than 256 then
12048 use an 'unsigned char' instead of a 'short' or 'int'.
12051 Use unsigned when it is known in advance that the value is not going to
12053 This helps especially if you are doing division or multiplication.
12056 NEVER jump into a LOOP.
12059 Declare the variables to be local whenever possible, especially loop control
12060 variables (induction).
12063 Since the compiler does not always do implicit integral promotion, the programme
12064 r should do an explicit cast when integral promotion is required.
12067 Reducing the size of division, multiplication & modulus operations can reduce
12068 code size substantially.
12069 Take the following code for example.
12075 foobar(unsigned int p1, unsigned char ch)
12083 unsigned char ch1 = p1 % ch ;
12094 For the modulus operation the variable ch will be promoted to unsigned int
12095 first then the modulus operation will be performed (this will lead to a
12096 call to support routine _moduint()), and the result will be casted to a
12098 If the code is changed to
12103 foobar(unsigned int p1, unsigned char ch)
12111 unsigned char ch1 = (unsigned char)p1 % ch ;
12122 It would substantially reduce the code generated (future versions of the
12123 compiler will be smart enough to detect such optimization opportunities).
12127 Notes on MCS51 memory
12128 \begin_inset LatexCommand \index{MCS51 memory}
12135 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
12136 RAM memory which is structured as follows
12140 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
12143 - Bytes 20-2F - 16 bytes to hold 128 bit variables and,
12145 - Bytes 30-7F - 80 bytes for general purpose use.
12150 Additionally some members of the MCS51 family may have up to 128 bytes of
12151 additional, indirectly addressable, internal RAM memory (
12156 Furthermore, some chips may have some built in external memory (
12160 ) which should not be confused with the internal, directly addressable RAM
12166 Usually this built in
12170 memory has to be activated before using it (you can probably find this
12171 information on the datasheet of the microcontroller your are using).
12174 Normally SDCC will only use the first bank
12175 \begin_inset LatexCommand \index{bank}
12179 of registers (register bank 0), but it is possible to specify that other
12180 banks of registers should be used in interrupt
12181 \begin_inset LatexCommand \index{interrupt}
12186 By default, the compiler will place the stack after the last byte of allocated
12187 memory for variables.
12188 For example, if the first 2 banks of registers are used, and only four
12193 variables, it will position the base of the internal stack at address 20
12195 This implies that as the stack
12196 \begin_inset LatexCommand \index{stack}
12200 grows, it will use up the remaining register banks, and the 16 bytes used
12201 by the 128 bit variables, and 80 bytes for general purpose use.
12202 If any bit variables are used, the data variables will be placed after
12203 the byte holding the last bit variable.
12204 For example, if register banks 0 and 1 are used, and there are 9 bit variables
12209 variables will be placed starting at address 0x22.
12221 \begin_inset LatexCommand \index{-\/-data-loc}
12225 to specify the start address of the
12239 -iram-size to specify the size of the total internal RAM (
12251 By default the 8051 linker will place the stack after the last byte of data
12264 \begin_inset LatexCommand \index{-\/-stack-loc}
12268 allows you to specify the start of the stack, i.e.
12269 you could start it after any data in the general purpose area.
12270 If your microcontroller has additional indirectly addressable internal
12275 ) you can place the stack on it.
12276 You may also need to use -
12287 \begin_inset LatexCommand \index{-\/-data-loc}
12291 to set the start address of the external RAM (
12306 \begin_inset LatexCommand \index{-\/-data-loc}
12310 to specify its size.
12311 Same goes for the code memory, using -
12322 \begin_inset LatexCommand \index{-\/-data-loc}
12337 \begin_inset LatexCommand \index{-\/-data-loc}
12342 If in doubt, don't specify any options and see if the resulting memory
12343 layout is appropriate, then you can adjust it.
12346 The 8051 linker generates two files with memory allocation information.
12347 The first, with extension .map shows all the variables and segments.
12348 The second with extension .mem shows the final memory layout.
12349 The linker will complaint either if memory segments overlap, there is not
12350 enough memory, or there is not enough space for stack.
12351 If you get any linking warnings and/or errors related to stack or segments
12352 allocation, take a look at either the .map or .mem files to find out what
12354 The .mem file may even suggest a solution to the problem.
12358 \begin_inset LatexCommand \index{Tools}
12362 included in the distribution
12366 \begin_inset Tabular
12367 <lyxtabular version="3" rows="12" columns="3">
12369 <column alignment="center" valignment="top" leftline="true" width="0pt">
12370 <column alignment="center" valignment="top" leftline="true" width="0pt">
12371 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
12372 <row topline="true" bottomline="true">
12373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12381 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12398 <row topline="true">
12399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12412 Simulator for various architectures
12415 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12424 <row topline="true">
12425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12438 header file conversion
12441 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12446 sdcc/support/scripts
12450 <row topline="true">
12451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12464 header file conversion
12467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12472 sdcc/support/scripts
12476 <row topline="true">
12477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12511 <row topline="true">
12512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12546 <row topline="true">
12547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12555 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12563 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12581 <row topline="true">
12582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12616 <row topline="true">
12617 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12633 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12651 <row topline="true">
12652 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12686 <row topline="true">
12687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12703 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12721 <row topline="true" bottomline="true">
12722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12738 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12763 Related open source tools
12764 \begin_inset LatexCommand \index{Related tools}
12772 \begin_inset Tabular
12773 <lyxtabular version="3" rows="7" columns="3">
12775 <column alignment="center" valignment="top" leftline="true" width="0pt">
12776 <column alignment="center" valignment="top" leftline="true" width="0pt">
12777 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
12778 <row topline="true" bottomline="true">
12779 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12795 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12804 <row topline="true">
12805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12811 \begin_inset LatexCommand \index{gpsim}
12818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12832 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
12840 <row topline="true">
12841 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12847 \begin_inset LatexCommand \index{srecord}
12854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12859 Object file conversion, checksumming, ...
12862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12868 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
12876 <row topline="true">
12877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12883 \begin_inset LatexCommand \index{objdump}
12890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12895 Object file conversion, ...
12898 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12903 Part of binutils (should be there anyway)
12907 <row topline="true">
12908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12914 \begin_inset LatexCommand \index{doxygen}
12921 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12926 Source code documentation system
12929 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12935 \begin_inset LatexCommand \url{http://www.doxygen.org}
12943 <row topline="true">
12944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12950 \begin_inset LatexCommand \index{splint}
12957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12962 Statically checks c sources
12965 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12971 \begin_inset LatexCommand \url{http://www.splint.org}
12979 <row topline="true" bottomline="true">
12980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12986 \begin_inset LatexCommand \index{ddd}
12993 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12998 Debugger, serves nicely as GUI to sdcdb
12999 \begin_inset LatexCommand \index{sdcdb}
13006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13012 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
13027 Related documentation / recommended reading
13031 \begin_inset Tabular
13032 <lyxtabular version="3" rows="5" columns="3">
13034 <column alignment="center" valignment="top" leftline="true" width="0pt">
13035 <column alignment="center" valignment="top" leftline="true" width="0pt">
13036 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
13037 <row topline="true" bottomline="true">
13038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13063 <row topline="true">
13064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13079 Advanced Compiler Design and Implementation
13082 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13091 <row topline="true">
13092 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13115 \begin_inset LatexCommand \index{C Reference card}
13122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13128 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
13136 <row topline="true">
13137 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13142 test_suite_spec.pdf
13145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13150 sdcc regression test
13151 \begin_inset LatexCommand \index{Regression test}
13158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13167 <row topline="true" bottomline="true">
13168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13199 sdcc internal documentation
13202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13218 Retargetting for other MCUs.
13221 The issues for retargetting the compiler are far too numerous to be covered
13223 What follows is a brief description of each of the seven phases of the
13224 compiler and its MCU dependency.
13227 Parsing the source and building the annotated parse tree.
13228 This phase is largely MCU independent (except for the language extensions).
13229 Syntax & semantic checks are also done in this phase, along with some initial
13230 optimizations like back patching labels and the pattern matching optimizations
13231 like bit-rotation etc.
13234 The second phase involves generating an intermediate code which can be easy
13235 manipulated during the later phases.
13236 This phase is entirely MCU independent.
13237 The intermediate code generation assumes the target machine has unlimited
13238 number of registers, and designates them with the name iTemp.
13239 The compiler can be made to dump a human readable form of the code generated
13253 This phase does the bulk of the standard optimizations and is also MCU independe
13255 This phase can be broken down into several sub-phases:
13259 Break down intermediate code (iCode) into basic blocks.
13261 Do control flow & data flow analysis on the basic blocks.
13263 Do local common subexpression elimination, then global subexpression elimination
13265 Dead code elimination
13269 If loop optimizations caused any changes then do 'global subexpression eliminati
13270 on' and 'dead code elimination' again.
13273 This phase determines the live-ranges; by live range I mean those iTemp
13274 variables defined by the compiler that still survive after all the optimization
13276 Live range analysis
13277 \begin_inset LatexCommand \index{Live range analysis}
13281 is essential for register allocation, since these computation determines
13282 which of these iTemps will be assigned to registers, and for how long.
13285 Phase five is register allocation.
13286 There are two parts to this process.
13290 The first part I call 'register packing' (for lack of a better term).
13291 In this case several MCU specific expression folding is done to reduce
13296 The second part is more MCU independent and deals with allocating registers
13297 to the remaining live ranges.
13298 A lot of MCU specific code does creep into this phase because of the limited
13299 number of index registers available in the 8051.
13302 The Code generation phase is (unhappily), entirely MCU dependent and very
13303 little (if any at all) of this code can be reused for other MCU.
13304 However the scheme for allocating a homogenized assembler operand for each
13305 iCode operand may be reused.
13308 As mentioned in the optimization section the peep-hole optimizer is rule
13309 based system, which can reprogrammed for other MCUs.
13313 \begin_inset LatexCommand \index{sdcdb}
13317 - Source Level Debugger
13318 \begin_inset LatexCommand \index{Debugger}
13325 SDCC is distributed with a source level debugger.
13326 The debugger uses a command line interface, the command repertoire of the
13327 debugger has been kept as close to gdb
13328 \begin_inset LatexCommand \index{gdb}
13332 (the GNU debugger) as possible.
13333 The configuration and build process is part of the standard compiler installati
13334 on, which also builds and installs the debugger in the target directory
13335 specified during configuration.
13336 The debugger allows you debug BOTH at the C source and at the ASM source
13340 Compiling for Debugging
13343 The \SpecialChar \-
13345 debug option must be specified for all files for which debug information
13346 is to be generated.
13347 The complier generates a .adb file for each of these files.
13348 The linker creates the .cdb file from the .adb files and the address information.
13349 This .cdb is used by the debugger.
13352 How the Debugger Works
13365 -debug option is specified the compiler generates extra symbol information
13366 some of which are put into the the assembler source and some are put into
13368 Then the linker creates the .cdb file from the individual .adb files with
13369 the address information for the symbols.
13370 The debugger reads the symbolic information generated by the compiler &
13371 the address information generated by the linker.
13372 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
13373 execution is controlled by the debugger.
13374 When a command is issued for the debugger, it translates it into appropriate
13375 commands for the simulator.
13378 Starting the Debugger
13381 The debugger can be started using the following command line.
13382 (Assume the file you are debugging has the file name foo).
13396 The debugger will look for the following files.
13399 foo.c - the source file.
13402 foo.cdb - the debugger symbol information file.
13405 foo.ihx - the Intel hex format
13406 \begin_inset LatexCommand \index{Intel hex format}
13413 Command Line Options.
13426 -directory=<source file directory> this option can used to specify the directory
13428 The debugger will look into the directory list specified for source, cdb
13430 The items in the directory list must be separated by ':', e.g.
13431 if the source files can be in the directories /home/src1 and /home/src2,
13442 -directory option should be -
13452 -directory=/home/src1:/home/src2.
13453 Note there can be no spaces in the option.
13457 -cd <directory> - change to the <directory>.
13460 -fullname - used by GUI front ends.
13463 -cpu <cpu-type> - this argument is passed to the simulator please see the
13464 simulator docs for details.
13467 -X <Clock frequency > this options is passed to the simulator please see
13468 the simulator docs for details.
13471 -s <serial port file> passed to simulator see the simulator docs for details.
13474 -S <serial in,out> passed to simulator see the simulator docs for details.
13480 As mention earlier the command interface for the debugger has been deliberately
13481 kept as close the GNU debugger gdb, as possible.
13482 This will help the integration with existing graphical user interfaces
13483 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
13484 \layout Subsubsection
13486 break [line | file:line | function | file:function]
13489 Set breakpoint at specified line or function:
13498 sdcdb>break foo.c:100
13500 sdcdb>break funcfoo
13502 sdcdb>break foo.c:funcfoo
13503 \layout Subsubsection
13505 clear [line | file:line | function | file:function ]
13508 Clear breakpoint at specified line or function:
13517 sdcdb>clear foo.c:100
13519 sdcdb>clear funcfoo
13521 sdcdb>clear foo.c:funcfoo
13522 \layout Subsubsection
13527 Continue program being debugged, after breakpoint.
13528 \layout Subsubsection
13533 Execute till the end of the current function.
13534 \layout Subsubsection
13539 Delete breakpoint number 'n'.
13540 If used without any option clear ALL user defined break points.
13541 \layout Subsubsection
13543 info [break | stack | frame | registers ]
13546 info break - list all breakpoints
13549 info stack - show the function call stack.
13552 info frame - show information about the current execution frame.
13555 info registers - show content of all registers.
13556 \layout Subsubsection
13561 Step program until it reaches a different source line.
13562 \layout Subsubsection
13567 Step program, proceeding through subroutine calls.
13568 \layout Subsubsection
13573 Start debugged program.
13574 \layout Subsubsection
13579 Print type information of the variable.
13580 \layout Subsubsection
13585 print value of variable.
13586 \layout Subsubsection
13591 load the given file name.
13592 Note this is an alternate method of loading file for debugging.
13593 \layout Subsubsection
13598 print information about current frame.
13599 \layout Subsubsection
13604 Toggle between C source & assembly source.
13605 \layout Subsubsection
13607 ! simulator command
13610 Send the string following '!' to the simulator, the simulator response is
13612 Note the debugger does not interpret the command being sent to the simulator,
13613 so if a command like 'go' is sent the debugger can loose its execution
13614 context and may display incorrect values.
13615 \layout Subsubsection
13622 My name is Bobby Brown"
13625 Interfacing with XEmacs
13626 \begin_inset LatexCommand \index{XEmacs}
13631 \begin_inset LatexCommand \index{Emacs}
13638 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
13639 sdcdb.el and sdcdbsrc.el.
13640 These two files can be found in the $(prefix)/bin directory after the installat
13642 These files need to be loaded into XEmacs for the interface to work.
13643 This can be done at XEmacs startup time by inserting the following into
13644 your '.xemacs' file (which can be found in your HOME directory):
13650 (load-file sdcdbsrc.el)
13656 .xemacs is a lisp file so the () around the command is REQUIRED.
13657 The files can also be loaded dynamically while XEmacs is running, set the
13658 environment variable 'EMACSLOADPATH' to the installation bin directory
13659 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
13660 To start the interface enter the following command:
13674 You will prompted to enter the file name to be debugged.
13679 The command line options that are passed to the simulator directly are bound
13680 to default values in the file sdcdbsrc.el.
13681 The variables are listed below, these values maybe changed as required.
13684 sdcdbsrc-cpu-type '51
13687 sdcdbsrc-frequency '11059200
13690 sdcdbsrc-serial nil
13693 The following is a list of key mapping for the debugger interface.
13701 ;; Current Listing ::
13703 ;;key\SpecialChar ~
13718 binding\SpecialChar ~
13742 ;;---\SpecialChar ~
13757 ------\SpecialChar ~
13797 sdcdb-next-from-src\SpecialChar ~
13823 sdcdb-back-from-src\SpecialChar ~
13849 sdcdb-cont-from-src\SpecialChar ~
13859 SDCDB continue command
13875 sdcdb-step-from-src\SpecialChar ~
13901 sdcdb-whatis-c-sexp\SpecialChar ~
13911 SDCDB ptypecommand for data at
13975 sdcdbsrc-delete\SpecialChar ~
13989 SDCDB Delete all breakpoints if no arg
14037 given or delete arg (C-u arg x)
14053 sdcdbsrc-frame\SpecialChar ~
14068 SDCDB Display current frame if no arg,
14117 given or display frame arg
14182 sdcdbsrc-goto-sdcdb\SpecialChar ~
14192 Goto the SDCDB output buffer
14208 sdcdb-print-c-sexp\SpecialChar ~
14219 SDCDB print command for data at
14283 sdcdbsrc-goto-sdcdb\SpecialChar ~
14293 Goto the SDCDB output buffer
14309 sdcdbsrc-mode\SpecialChar ~
14325 Toggles Sdcdbsrc mode (turns it off)
14329 ;; C-c C-f\SpecialChar ~
14337 sdcdb-finish-from-src\SpecialChar ~
14345 SDCDB finish command
14349 ;; C-x SPC\SpecialChar ~
14357 sdcdb-break\SpecialChar ~
14375 Set break for line with point
14377 ;; ESC t\SpecialChar ~
14387 sdcdbsrc-mode\SpecialChar ~
14403 Toggle Sdcdbsrc mode
14405 ;; ESC m\SpecialChar ~
14415 sdcdbsrc-srcmode\SpecialChar ~
14439 The Z80 and gbz80 port
14442 SDCC can target both the Zilog Z80 and the Nintendo Gameboy's Z80-like gbz80.
14443 The port is incomplete - long support is incomplete (mul, div and mod are
14444 unimplemented), and both float and bitfield support is missing.
14445 Apart from that the code generated is correct.
14448 As always, the code is the authoritave reference - see z80/ralloc.c and z80/gen.c.
14449 The stack frame is similar to that generated by the IAR Z80 compiler.
14450 IX is used as the base pointer, HL is used as a temporary register, and
14451 BC and DE are available for holding variables.
14452 IY is currently unused.
14453 Return values are stored in HL.
14454 One bad side effect of using IX as the base pointer is that a functions
14455 stack frame is limited to 127 bytes - this will be fixed in a later version.
14459 \begin_inset LatexCommand \index{Support}
14466 SDCC has grown to be a large project.
14467 The compiler alone (without the preprocessor, assembler and linker) is
14468 about 40,000 lines of code (blank stripped).
14469 The open source nature of this project is a key to its continued growth
14471 You gain the benefit and support of many active software developers and
14473 Is SDCC perfect? No, that's why we need your help.
14474 The developers take pride in fixing reported bugs.
14475 You can help by reporting the bugs and helping other SDCC users.
14476 There are lots of ways to contribute, and we encourage you to take part
14477 in making SDCC a great software package.
14481 The SDCC project is hosted on the sdcc sourceforge site at
14482 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
14487 You'll find the complete set of mailing lists
14488 \begin_inset LatexCommand \index{Mailing list}
14492 , forums, bug reporting system, patch submission
14493 \begin_inset LatexCommand \index{Patch submission}
14498 \begin_inset LatexCommand \index{download}
14502 area and cvs code repository
14503 \begin_inset LatexCommand \index{cvs code repository}
14511 \begin_inset LatexCommand \index{Bugs}
14516 \begin_inset LatexCommand \index{Reporting bugs}
14523 The recommended way of reporting bugs is using the infrastructure of the
14525 You can follow the status of bug reports there and have an overview about
14529 Bug reports are automatically forwarded to the developer mailing list and
14530 will be fixed ASAP.
14531 When reporting a bug, it is very useful to include a small test program
14532 which reproduces the problem.
14533 If you can isolate the problem by looking at the generated assembly code,
14534 this can be very helpful.
14535 Compiling your program with the -
14546 \begin_inset LatexCommand \index{-\/-dumpall}
14550 option can sometimes be useful in locating optimization problems.
14553 Please have a short check that you are using a recent version of SDCC and
14554 the bug is not yet known.
14555 This is the link for reporting bugs:
14556 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
14563 Requesting Features
14564 \begin_inset LatexCommand \index{Feature request}
14569 \begin_inset LatexCommand \index{Requesting features}
14576 Like bug reports feature requests are forwarded to the developer mailing
14578 This is the link for requesting features:
14579 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
14589 These links should take you directly to the
14590 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
14600 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
14601 automated messages (mid 2003)
14605 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
14609 , lists and forums are archived so if you are lucky someone already had
14614 \begin_inset LatexCommand \index{Changelog}
14621 You can follow the status of the cvs version
14622 \begin_inset LatexCommand \index{version}
14626 of SDCC by watching the file
14627 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
14631 in the cvs-repository.
14635 \begin_inset LatexCommand \index{Release policy}
14642 Historically there often were long delays between official releases and
14643 the sourceforge download area tends to get not updated at all.
14644 Current excuses might refer to problems with live range analysis, but if
14645 this is fixed, the next problem rising is that another excuse will have
14647 Kidding aside, we have to get better there!
14651 \begin_inset LatexCommand \index{Examples}
14658 You'll find some small examples in the directory sdcc/device/examples/
14661 Maybe we should include some links to real world applications.
14662 Preferably pointer to pointers (one for each architecture) so this stays
14667 \begin_inset LatexCommand \index{Quality control}
14674 The compiler is passed through nightly compile and build checks.
14680 \begin_inset LatexCommand \index{Regression test}
14684 check that SDCC itself compiles flawlessly on several platforms and checks
14685 the quality of the code generated by SDCC by running the code through simulator
14687 There is a separate document
14690 \begin_inset LatexCommand \index{Test suite}
14699 You'll find the test code in the directory
14701 sdcc/support/regression
14704 You can run these tests manually by running
14708 in this directory (or f.e.
14713 if you don't want to run the complete tests).
14714 The test code might also be interesting if you want to look for examples
14715 \begin_inset LatexCommand \index{Examples}
14719 checking corner cases of SDCC or if you plan to submit patches
14720 \begin_inset LatexCommand \index{Patch submission}
14727 The pic port uses a different set of regression tests, you'll find them
14730 sdcc/src/regression
14736 \begin_inset LatexCommand \index{Compiler internals}
14743 The anatomy of the compiler
14748 This is an excerpt from an article published in Circuit Cellar Magazine
14750 It's a little outdated (the compiler is much more efficient now and user/develo
14751 per friendly), but pretty well exposes the guts of it all.
14757 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
14758 It is fairly easy to retarget for other 8-bit MCU.
14759 Here we take a look at some of the internals of the compiler.
14764 \begin_inset LatexCommand \index{Parsing}
14771 Parsing the input source file and creating an AST (Annotated Syntax Tree
14772 \begin_inset LatexCommand \index{Annotated syntax tree}
14777 This phase also involves propagating types (annotating each node of the
14778 parse tree with type information) and semantic analysis.
14779 There are some MCU specific parsing rules.
14780 For example the storage classes, the extended storage classes are MCU specific
14781 while there may be a xdata storage class for 8051 there is no such storage
14782 class for z80 or Atmel AVR.
14783 SDCC allows MCU specific storage class extensions, i.e.
14784 xdata will be treated as a storage class specifier when parsing 8051 C
14785 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
14790 \begin_inset LatexCommand \index{iCode}
14797 Intermediate code generation.
14798 In this phase the AST is broken down into three-operand form (iCode).
14799 These three operand forms are represented as doubly linked lists.
14800 ICode is the term given to the intermediate form generated by the compiler.
14801 ICode example section shows some examples of iCode generated for some simple
14802 C source functions.
14806 \begin_inset LatexCommand \index{Optimizations}
14813 Bulk of the target independent optimizations is performed in this phase.
14814 The optimizations include constant propagation, common sub-expression eliminati
14815 on, loop invariant code movement, strength reduction of loop induction variables
14816 and dead-code elimination.
14819 Live range analysis
14820 \begin_inset LatexCommand \index{Live range analysis}
14827 During intermediate code generation phase, the compiler assumes the target
14828 machine has infinite number of registers and generates a lot of temporary
14830 The live range computation determines the lifetime of each of these compiler-ge
14831 nerated temporaries.
14832 A picture speaks a thousand words.
14833 ICode example sections show the live range annotations for each of the
14835 It is important to note here, each iCode is assigned a number in the order
14836 of its execution in the function.
14837 The live ranges are computed in terms of these numbers.
14838 The from number is the number of the iCode which first defines the operand
14839 and the to number signifies the iCode which uses this operand last.
14842 Register Allocation
14843 \begin_inset LatexCommand \index{Register allocation}
14850 The register allocation determines the type and number of registers needed
14852 In most MCUs only a few registers can be used for indirect addressing.
14853 In case of 8051 for example the registers R0 & R1 can be used to indirectly
14854 address the internal ram and DPTR to indirectly address the external ram.
14855 The compiler will try to allocate the appropriate register to pointer variables
14857 ICode example section shows the operands annotated with the registers assigned
14859 The compiler will try to keep operands in registers as much as possible;
14860 there are several schemes the compiler uses to do achieve this.
14861 When the compiler runs out of registers the compiler will check to see
14862 if there are any live operands which is not used or defined in the current
14863 basic block being processed, if there are any found then it will push that
14864 operand and use the registers in this block, the operand will then be popped
14865 at the end of the basic block.
14869 There are other MCU specific considerations in this phase.
14870 Some MCUs have an accumulator; very short-lived operands could be assigned
14871 to the accumulator instead of general-purpose register.
14877 Figure II gives a table of iCode operations supported by the compiler.
14878 The code generation involves translating these operations into corresponding
14879 assembly code for the processor.
14880 This sounds overly simple but that is the essence of code generation.
14881 Some of the iCode operations are generated on a MCU specific manner for
14882 example, the z80 port does not use registers to pass parameters so the
14883 SEND and RECV iCode operations will not be generated, and it also does
14884 not support JUMPTABLES.
14891 <Where is Figure II ?>
14895 \begin_inset LatexCommand \index{iCode}
14902 This section shows some details of iCode.
14903 The example C code does not do anything useful; it is used as an example
14904 to illustrate the intermediate code generated by the compiler.
14916 /* This function does nothing useful.
14923 for the purpose of explaining iCode */
14926 short function (data int *x)
14934 short i=10; /* dead initialization eliminated */
14939 short sum=10; /* dead initialization eliminated */
14952 while (*x) *x++ = *p++;
14966 /* compiler detects i,j to be induction variables */
14970 for (i = 0, j = 10 ; i < 10 ; i++, j--) {
14982 mul += i * 3; /* this multiplication remains */
14988 gint += j * 3;/* this multiplication changed to addition */
15002 In addition to the operands each iCode contains information about the filename
15003 and line it corresponds to in the source file.
15004 The first field in the listing should be interpreted as follows:
15009 Filename(linenumber: iCode Execution sequence number : ICode hash table
15010 key : loop depth of the iCode).
15015 Then follows the human readable form of the ICode operation.
15016 Each operand of this triplet form can be of three basic types a) compiler
15017 generated temporary b) user defined variable c) a constant value.
15018 Note that local variables and parameters are replaced by compiler generated
15021 \begin_inset LatexCommand \index{Live range analysis}
15025 are computed only for temporaries (i.e.
15026 live ranges are not computed for global variables).
15028 \begin_inset LatexCommand \index{Register allocation}
15032 are allocated for temporaries only.
15033 Operands are formatted in the following manner:
15038 Operand Name [lr live-from : live-to ] { type information } [ registers
15044 As mentioned earlier the live ranges are computed in terms of the execution
15045 sequence number of the iCodes, for example
15047 the iTemp0 is live from (i.e.
15048 first defined in iCode with execution sequence number 3, and is last used
15049 in the iCode with sequence number 5).
15050 For induction variables such as iTemp21 the live range computation extends
15051 the lifetime from the start to the end of the loop.
15053 The register allocator used the live range information to allocate registers,
15054 the same registers may be used for different temporaries if their live
15055 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
15056 iTemp17 since their live ranges do not overlap.
15057 In addition the allocator also takes into consideration the type and usage
15058 of a temporary, for example itemp6 is a pointer to near space and is used
15059 as to fetch data from (i.e.
15060 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
15061 Some short lived temporaries are allocated to special registers which have
15062 meaning to the code generator e.g.
15063 iTemp13 is allocated to a pseudo register CC which tells the back end that
15064 the temporary is used only for a conditional jump the code generation makes
15065 use of this information to optimize a compare and jump ICode.
15067 There are several loop optimizations
15068 \begin_inset LatexCommand \index{Loop optimization}
15072 performed by the compiler.
15073 It can detect induction variables iTemp21(i) and iTemp23(j).
15074 Also note the compiler does selective strength reduction
15075 \begin_inset LatexCommand \index{Strength reduction}
15080 the multiplication of an induction variable in line 18 (gint = j * 3) is
15081 changed to addition, a new temporary iTemp17 is allocated and assigned
15082 a initial value, a constant 3 is then added for each iteration of the loop.
15083 The compiler does not change the multiplication
15084 \begin_inset LatexCommand \index{Multiplication}
15088 in line 17 however since the processor does support an 8 * 8 bit multiplication.
15090 Note the dead code elimination
15091 \begin_inset LatexCommand \index{Dead-code elimination}
15095 optimization eliminated the dead assignments in line 7 & 8 to I and sum
15103 Sample.c (5:1:0:0) _entry($9) :
15108 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
15113 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
15118 Sample.c(11:4:53:0) preHeaderLbl0($11) :
15123 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
15129 Sample.c(11:6:5:1) _whilecontinue_0($1) :
15134 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
15140 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
15145 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
15151 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
15157 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
15163 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
15169 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
15170 * int}[r0] + 0x2 {short}
15175 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
15180 Sample.c(11:17:21:0)_whilebreak_0($3) :
15185 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
15190 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
15195 Sample.c(15:20:54:0)preHeaderLbl1($13) :
15200 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
15205 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
15210 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
15215 Sample.c(15:24:26:1)_forcond_0($4) :
15220 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
15226 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
15231 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
15232 + ITemp21 [lr21:38]{short}[r4]
15237 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
15243 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
15244 + iTemp15 [lr29:30]{short}[r1]
15249 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
15255 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
15261 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
15267 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
15273 Sample.c(19:38:47:1) goto _forcond_0($4)
15278 Sample.c(19:39:48:0)_forbreak_0($7) :
15283 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
15284 + ITemp11 [lr19:40]{short}[r3]
15289 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
15294 Sample.c(20:42:51:0)_return($8) :
15299 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
15305 Finally the code generated for this function:
15346 ; ----------------------------------------------
15351 ; function function
15356 ; ----------------------------------------------
15366 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
15378 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
15390 ;_whilecontinue_0($1) :
15400 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
15405 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
15464 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
15483 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
15530 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
15570 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
15596 ; iTemp6 [lr5:16]{_near * int}[r0] =
15601 ; iTemp6 [lr5:16]{_near * int}[r0] +
15618 ; goto _whilecontinue_0($1)
15630 ; _whilebreak_0($3) :
15640 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
15652 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
15664 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
15676 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
15695 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
15724 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
15729 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
15774 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
15779 ; iTemp21 [lr21:38]{short}[r4]
15805 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
15838 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
15843 ; iTemp15 [lr29:30]{short}[r1]
15862 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
15909 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
15956 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
15968 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
15982 cjne r5,#0xff,00104$
15994 ; goto _forcond_0($4)
16006 ; _forbreak_0($7) :
16016 ; ret iTemp24 [lr40:41]{short}
16059 A few words about basic block successors, predecessors and dominators
16062 Successors are basic blocks
16063 \begin_inset LatexCommand \index{Basic blocks}
16067 that might execute after this basic block.
16069 Predecessors are basic blocks that might execute before reaching this basic
16072 Dominators are basic blocks that WILL execute before reaching this basic
16098 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
16101 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
16104 c) domVect of [BB4] = BB1 ...
16105 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
16113 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
16123 Thanks to all the other volunteer developers who have helped with coding,
16124 testing, web-page creation, distribution sets, etc.
16125 You know who you are :-)
16132 This document was initially written by Sandeep Dutta
16135 All product names mentioned herein may be trademarks
16136 \begin_inset LatexCommand \index{Trademarks}
16140 of their respective companies.
16147 To avoid confusion, the installation and building options for sdcc itself
16148 (chapter 2) are not part of the index.
16152 \begin_inset LatexCommand \printindex{}