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
21 \papersize letterpaper
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 Architecture specific stuff (like memory models, code examples) should maybe
67 into seperate sections/chapters/appendices (it is hard to document PIC or
71 a 8051 centered document)
74 SDCC Compiler User Guide
77 The strings enclosed in $ are automatically updated by cvs:
91 \begin_inset LatexCommand \tableofcontents{}
108 is a Freeware, retargettable, optimizing ANSI-C compiler by
112 designed for 8 bit Microprocessors.
113 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
115 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052 CPU}
119 , etc), Zilog Z80 based MCUs, and the Dallas DS80C390 variant.
120 It can be retargetted for other microprocessors, support for Microchip
121 PIC, Atmel AVR is under development.
122 The entire source code for the compiler is distributed under GPL.
123 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
124 SDCC has extensive language extensions suitable for utilizing various microcont
125 rollers and underlying hardware effectively.
130 In addition to the MCU specific optimizations SDCC also does a host of standard
134 global sub expression elimination,
137 loop optimizations (loop invariant, strength reduction of induction variables
141 constant folding & propagation,
147 dead code elimination
157 For the back-end SDCC uses a global register allocation scheme which should
158 be well suited for other 8 bit MCUs.
163 The peep hole optimizer uses a rule based substitution mechanism which is
169 Supported data-types are:
172 char (8 bits, 1 byte),
175 short and int (16 bits, 2 bytes),
178 long (32 bit, 4 bytes)
185 The compiler also allows
187 inline assembler code
189 to be embedded anywhere in a function.
190 In addition, routines developed in assembly can also be called.
194 SDCC also provides an option (-
204 -cyclomatic) to report the relative complexity of a function.
205 These functions can then be further optimized, or hand coded in assembly
211 SDCC also comes with a companion source level debugger SDCDB, the debugger
212 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
217 The latest version can be downloaded from
218 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
228 Please note: the compiler will probably always be some steps ahead of this
233 \begin_inset LatexCommand \index{Status of documentation}
243 Obviously this has pros and cons
252 All packages used in this compiler system are
260 ; source code for all the sub-packages (pre-processor, assemblers, linkers
261 etc) is distributed with the package.
262 This documentation is maintained using a freeware word processor (LyX).
264 This program is free software; you can redistribute it and/or modify it
265 under the terms of the GNU General Public License
266 \begin_inset LatexCommand \index{GNU General Public License, GPL}
270 as published by the Free Software Foundation; either version 2, or (at
271 your option) any later version.
272 This program is distributed in the hope that it will be useful, but WITHOUT
273 ANY WARRANTY; without even the implied warranty
274 \begin_inset LatexCommand \index{warranty}
278 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
279 See the GNU General Public License for more details.
280 You should have received a copy of the GNU General Public License along
281 with this program; if not, write to the Free Software Foundation, 59 Temple
282 Place - Suite 330, Boston, MA 02111-1307, USA.
283 In other words, you are welcome to use, share and improve this program.
284 You are forbidden to forbid anyone else to use, share and improve what
286 Help stamp out software-hoarding!
289 Typographic conventions
290 \begin_inset LatexCommand \index{Typographic conventions}
297 Throughout this manual, we will use the following convention.
298 Commands you have to type in are printed in
306 Code samples are printed in
311 Interesting items and new terms are printed in
316 Compatibility with previous versions
319 This version has numerous bug fixes compared with the previous version.
320 But we also introduced some incompatibilities with older versions.
321 Not just for the fun of it, but to make the compiler more stable, efficient
323 \begin_inset LatexCommand \index{ANSI-compliance}
333 short is now equivalent to int (16 bits), it used to be equivalent to char
334 (8 bits) which is not ANSI compliant
337 the default directory for gcc-builds where include, library and documentation
338 files are stored is now in /usr/local/share
341 char type parameters to vararg functions are casted to int unless explicitly
358 will push a as an int and as a char resp.
371 -regextend has been removed
384 -noregparms has been removed
397 -stack-after-data has been removed
402 <pending: more incompatibilities?>
408 What do you need before you start installation of SDCC? A computer, and
410 The preferred method of installation is to compile SDCC from source using
412 For Windows some pre-compiled binary distributions are available for your
414 You should have some experience with command line tools and compiler use.
420 The SDCC home page at
421 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
425 is a great place to find distribution sets.
426 You can also find links to the user mailing lists that offer help or discuss
427 SDCC with other SDCC users.
428 Web links to other SDCC related sites can also be found here.
429 This document can be found in the DOC directory of the source package as
431 Some of the other tools (simulator and assembler) included with SDCC contain
432 their own documentation and can be found in the source distribution.
433 If you want the latest unreleased software, the complete source package
434 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
437 Wishes for the future
440 There are (and always will be) some things that could be done.
441 Here are some I can think of:
448 char KernelFunction3(char p) at 0x340;
456 \begin_inset LatexCommand \index{code banking (not supported)}
466 If you can think of some more, please see the chapter 9 about filing feature
468 \begin_inset LatexCommand \index{Requesting features}
473 \begin_inset LatexCommand \index{Feature request}
483 \begin_inset LatexCommand \index{Installation}
490 For most users it is sufficient to skip to either section
491 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
496 \begin_inset LatexCommand \ref{sub:Windows-Install}
501 More detailled instructions follow below.
505 \begin_inset LatexCommand \index{Options SDCC configuration}
512 The install paths, search paths and other options are defined when running
514 The defaults can be overridden by:
516 \labelwidthstring 00.00.0000
528 -prefix see table below
530 \labelwidthstring 00.00.0000
542 -exec_prefix see table below
544 \labelwidthstring 00.00.0000
556 -bindir see table below
558 \labelwidthstring 00.00.0000
570 -datadir see table below
572 \labelwidthstring 00.00.0000
574 docdir environment variable, see table below
576 \labelwidthstring 00.00.0000
578 include_dir_suffix environment variable, see table below
580 \labelwidthstring 00.00.0000
582 lib_dir_suffix environment variable, see table below
584 \labelwidthstring 00.00.0000
586 sdccconf_h_dir_separator environment variable, either / or
591 This character will only be used in sdccconf.h; don't forget it's a C-header,
592 therefore a double-backslash is needed there.
594 \labelwidthstring 00.00.0000
606 -disable-mcs51-port Excludes the Intel mcs51 port
608 \labelwidthstring 00.00.0000
620 -disable-gbz80-port Excludes the Gameboy gbz80 port
622 \labelwidthstring 00.00.0000
634 -z80-port Excludes the z80 port
636 \labelwidthstring 00.00.0000
648 -disable-avr-port Excludes the AVR port
650 \labelwidthstring 00.00.0000
662 -disable-ds390-port Excludes the DS390 port
664 \labelwidthstring 00.00.0000
676 -disable-pic-port Excludes the PIC port
678 \labelwidthstring 00.00.0000
690 -disable-xa51-port Excludes the XA51 port
692 \labelwidthstring 00.00.0000
704 -disable-ucsim Disables configuring and building of ucsim
706 \labelwidthstring 00.00.0000
718 -disable-device-lib-build Disables automatically building device libraries
720 \labelwidthstring 00.00.0000
732 -disable-packihx Disables building packihx
734 \labelwidthstring 00.00.0000
746 -enable-libgc Use the Bohem memory allocator.
747 Lower runtime footprint.
750 Furthermore the environment variables CC, CFLAGS, ...
751 the tools and their arguments can be influenced.
752 Please see `configure -
762 -help` and the man/info pages of `configure` for details.
766 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
767 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
768 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
769 At the moment it's not possible to change the default settings (it was
770 simply never required.
774 These configure options are compiled into the binaries, and can only be
775 changed by rerunning 'configure' and recompiling SDCC.
776 The configure options are written in
780 to distinguish them from run time environment variables (see section search
786 \begin_inset Quotes sld
790 \begin_inset Quotes srd
793 are used by the SDCC team to build the official Win32 binaries.
794 The SDCC team uses Mingw32 to build the official Windows binaries, because
801 a gcc compiler and last but not least
804 the binaries can be built by cross compiling on Sourceforge's compile farm.
807 See the examples, how to pass the Win32 settings to 'configure'.
808 The other Win32 builds using Borland, VC or whatever don't use 'configure',
809 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
821 <lyxtabular version="3" rows="8" columns="3">
823 <column alignment="block" valignment="top" leftline="true" width="0in">
824 <column alignment="block" valignment="top" leftline="true" width="0in">
825 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
826 <row topline="true" bottomline="true">
827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
843 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
871 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
925 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
937 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
953 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
987 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1009 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1024 <row topline="true">
1025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1043 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1052 <row topline="true" bottomline="true">
1053 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1063 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1071 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1089 'configure' also computes relative paths.
1090 This is needed for full relocatability of a binary package and to complete
1091 search paths (see section search paths below):
1097 \begin_inset Tabular
1098 <lyxtabular version="3" rows="4" columns="3">
1100 <column alignment="block" valignment="top" leftline="true" width="0in">
1101 <column alignment="block" valignment="top" leftline="true" width="0in">
1102 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1103 <row topline="true" bottomline="true">
1104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1120 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1129 <row topline="true" bottomline="true">
1130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1159 <row bottomline="true">
1160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1178 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1187 <row bottomline="true">
1188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1239 \begin_inset Quotes srd
1243 \begin_inset Quotes srd
1257 \begin_inset Quotes srd
1261 \begin_inset Quotes srd
1289 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1298 \begin_inset Quotes srd
1301 i586-mingw32msvc-gcc
1302 \begin_inset Quotes srd
1306 \begin_inset Quotes srd
1309 i586-mingw32msvc-g++
1310 \begin_inset Quotes srd
1318 \begin_inset Quotes srd
1321 i586-mingw32msvc-ranlib
1322 \begin_inset Quotes srd
1330 \begin_inset Quotes srd
1333 i586-mingw32msvc-strip
1334 \begin_inset Quotes srd
1352 \begin_inset Quotes srd
1356 \begin_inset Quotes srd
1374 \begin_inset Quotes srd
1378 \begin_inset Quotes srd
1386 \begin_inset Quotes srd
1390 \begin_inset Quotes srd
1398 \begin_inset Quotes srd
1402 \begin_inset Quotes srd
1410 \begin_inset Quotes srd
1414 \begin_inset Quotes srd
1421 sdccconf_h_dir_separator=
1422 \begin_inset Quotes srd
1434 \begin_inset Quotes srd
1451 -disable-device-lib-build
1479 -host=i586-mingw32msvc -
1489 -build=unknown-unknown-linux-gnu
1493 \begin_inset Quotes sld
1497 \begin_inset Quotes srd
1500 compile on Cygwin for Mingw32(see also sdcc/support/scripts/sdcc_cygwin_mingw32)
1509 \begin_inset Quotes srd
1513 \begin_inset Quotes srd
1521 \begin_inset Quotes srd
1525 \begin_inset Quotes srd
1543 \begin_inset Quotes srd
1547 \begin_inset Quotes srd
1565 \begin_inset Quotes srd
1569 \begin_inset Quotes srd
1577 \begin_inset Quotes srd
1581 \begin_inset Quotes srd
1589 \begin_inset Quotes srd
1593 \begin_inset Quotes srd
1601 \begin_inset Quotes srd
1605 \begin_inset Quotes srd
1612 sdccconf_h_dir_separator=
1613 \begin_inset Quotes srd
1625 \begin_inset Quotes srd
1645 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1656 -C' turns on caching, which gives a little bit extra speed.
1657 However if options are changed, it can be necessary to delete the config.cache
1662 \begin_inset LatexCommand \index{Install paths}
1668 \added_space_top medskip \align center
1670 \begin_inset Tabular
1671 <lyxtabular version="3" rows="5" columns="4">
1673 <column alignment="center" valignment="top" leftline="true" width="0">
1674 <column alignment="center" valignment="top" leftline="true" width="0">
1675 <column alignment="center" valignment="top" leftline="true" width="0">
1676 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1677 <row topline="true" bottomline="true">
1678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1688 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1698 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1719 <row topline="true">
1720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1728 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1746 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1759 <row topline="true">
1760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1768 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1775 $DATADIR/ $INCLUDE_DIR_SUFFIX
1778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1783 /usr/local/share/sdcc/include
1786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1799 <row topline="true">
1800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1808 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1815 $DATADIR/$LIB_DIR_SUFFIX
1818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 /usr/local/share/sdcc/lib
1826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1839 <row topline="true" bottomline="true">
1840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1863 /usr/local/share/sdcc/doc
1866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1888 *compiler, preprocessor, assembler, and linker
1894 is auto-appended by the compiler, e.g.
1895 small, large, z80, ds390 etc
1898 The install paths can still be changed during `make install` with e.g.:
1901 make install prefix=$(HOME)/local/sdcc
1904 Of course this doesn't change the search paths compiled into the binaries.
1908 \begin_inset LatexCommand \index{Search path}
1915 Some search paths or parts of them are determined by configure variables
1920 , see section above).
1921 Further search paths are determined by environment variables during runtime.
1924 The paths searched when running the compiler are as follows (the first catch
1930 Binary files (preprocessor, assembler and linker)
1936 \begin_inset Tabular
1937 <lyxtabular version="3" rows="4" columns="3">
1939 <column alignment="block" valignment="top" leftline="true" width="0in">
1940 <column alignment="block" valignment="top" leftline="true" width="0in">
1941 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1942 <row topline="true" bottomline="true">
1943 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1951 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1959 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1968 <row topline="true">
1969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1987 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1998 <row topline="true">
1999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2004 Path of argv[0] (if available)
2007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2015 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2024 <row topline="true" bottomline="true">
2025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2033 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2066 \begin_inset Tabular
2067 <lyxtabular version="3" rows="6" columns="3">
2069 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2070 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2071 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2072 <row topline="true" bottomline="true">
2073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2081 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2089 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2098 <row topline="true">
2099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2154 <row topline="true">
2155 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2180 <row topline="true">
2181 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2195 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2218 <row topline="true">
2219 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2287 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2300 <row topline="true" bottomline="true">
2301 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2317 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2322 /usr/local/share/sdcc/
2327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2355 -nostdinc disables the last two search paths.
2365 With the exception of
2366 \begin_inset Quotes sld
2380 \begin_inset Quotes srd
2387 is auto-appended by the compiler (e.g.
2388 small, large, z80, ds390 etc.).
2395 \begin_inset Tabular
2396 <lyxtabular version="3" rows="6" columns="3">
2398 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2399 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2400 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2401 <row topline="true" bottomline="true">
2402 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2410 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2427 <row topline="true">
2428 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2483 <row topline="true">
2484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2496 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2508 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2523 <row topline="true">
2524 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2535 $LIB_DIR_SUFFIX/<model>
2538 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2552 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2569 <row topline="true">
2570 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2585 $LIB_DIR_SUFFIX/<model>
2588 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2641 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2697 <row topline="true" bottomline="true">
2698 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2707 $LIB_DIR_SUFFIX/<model>
2710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2715 /usr/local/share/sdcc/
2722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2740 Don't delete any of the stray spaces in the table above without checking
2741 the HTML output (last line)!
2757 -nostdlib disables the last two search paths.
2761 \begin_inset LatexCommand \index{Building SDCC}
2766 \layout Subsubsection
2768 Building SDCC on Linux
2769 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2778 Download the source package
2780 either from the SDCC CVS repository or from the
2781 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
2787 , it will be named something like sdcc
2800 Bring up a command line terminal, such as xterm.
2805 Unpack the file using a command like:
2808 "tar -xzf sdcc.src.tar.gz
2813 , this will create a sub-directory called sdcc with all of the sources.
2816 Change directory into the main SDCC directory, for example type:
2833 This configures the package for compilation on your system.
2849 All of the source packages will compile, this can take a while.
2865 This copies the binary executables, the include files, the libraries and
2866 the documentation to the install directories.
2867 \layout Subsubsection
2869 Building SDCC on OSX 2.x
2872 Follow the instruction for Linux.
2876 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2877 )) fails to compile SDCC.
2878 Fortunately there's also gcc 2.9.x installed, which works fine.
2879 This compiler can be selected by running 'configure' with:
2882 ./configure CC=gcc2 CXX=g++2
2883 \layout Subsubsection
2885 Cross compiling SDCC on Linux for Windows
2888 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2889 See section 'Configure Options'.
2890 \layout Subsubsection
2892 Building SDCC on Windows
2895 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2897 They use Unix-sockets, which are not available on Win32.
2898 \layout Subsubsection
2900 Building SDCC using Cygwin and Mingw32
2903 For building and installing a Cygwin executable follow the instructions
2909 \begin_inset Quotes sld
2913 \begin_inset Quotes srd
2916 Win32-binary can be built, which will not need the Cygwin-DLL.
2917 For the necessary 'configure' options see section 'configure options' or
2918 the script 'sdcc/support/scripts/sdcc_cygwinmingw32'.
2922 In order to install Cygwin on Windows download setup.exe from
2923 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2929 \begin_inset Quotes sld
2932 default text file type
2933 \begin_inset Quotes srd
2937 \begin_inset Quotes sld
2941 \begin_inset Quotes srd
2944 and download/install at least the following packages.
2945 Some packages are selected by default, others will be automatically selected
2946 because of dependencies with the manually selected packages.
2947 Never deselect these packages!
2956 gcc ; version 3.x is fine, no need to use the old 2.9x
2959 binutils ; selected with gcc
2965 rxvt ; a nice console, which makes life much easier under windoze (see below)
2968 man ; not really needed for building SDCC, but you'll miss it sooner or
2972 less ; not really needed for building SDCC, but you'll miss it sooner or
2976 cvs ; only if you use CVS access
2979 If you want to develop something you'll need:
2982 python ; for the regression tests
2985 gdb ; the gnu debugger, together with the nice GUI
2986 \begin_inset Quotes sld
2990 \begin_inset Quotes srd
2996 openssh ; to access the CF or commit changes
2999 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3000 use autoconf-stable!
3003 rxvt is a nice console with history.
3004 Replace in your cygwin.bat the line
3023 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3026 -bg black -fg white -geometry 100x65 -e bash -
3039 Text selected with the mouse is automatically copied to the clipboard, pasting
3040 works with shift-insert.
3044 The other good tip is to make sure you have no //c/-style paths anywhere,
3045 use /cygdrive/c/ instead.
3046 Using // invokes a network lookup which is very slow.
3048 \begin_inset Quotes sld
3052 \begin_inset Quotes srd
3055 is too long, you can change it with e.g.
3061 SDCC sources use the unix line ending LF.
3062 Life is much easier, if you store the source tree on a drive which is mounted
3064 And use an editor which can handle LF-only line endings.
3065 Make sure not to commit files with windows line endings.
3066 The tabulator spacing used in the project is 8.
3067 \layout Subsubsection
3069 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3074 Download the source package
3076 either from the SDCC CVS repository or from the
3077 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3083 , it will be named something like sdcc
3090 SDCC is distributed with all the projects, workspaces, and files you need
3091 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3092 doesn't build under MSVC).
3093 The workspace name is 'sdcc.dsw'.
3094 Please note that as it is now, all the executables are created in a folder
3098 Once built you need to copy the executables from sdcc
3102 bin before running SDCC.
3107 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3108 flex.exe, and gawk.exe.
3109 One good place to get them is
3110 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3118 Download the file UnxUtils
3119 \begin_inset LatexCommand \index{UnxUtils}
3124 Now you have to install the utilities and setup MSVC so it can locate the
3126 Here there are two alternatives (choose one!):
3133 a) Extract UnxUtils.zip to your C:
3135 hard disk PRESERVING the original paths, otherwise bison won't work.
3136 (If you are using WinZip make certain that 'Use folder names' is selected)
3140 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3141 in 'Show directories for:' select 'Executable files', and in the directories
3142 window add a new path: 'C:
3152 (As a side effect, you get a bunch of Unix utilities that could be useful,
3153 such as diff and patch.)
3160 This one avoids extracting a bunch of files you may not use, but requires
3165 a) Create a directory were to put the tools needed, or use a directory already
3173 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3174 to such directory WITHOUT preserving the original paths.
3175 (If you are using WinZip make certain that 'Use folder names' is not selected)
3179 c) Rename bison.exe to '_bison.exe'.
3183 d) Create a batch file 'bison.bat' in 'C:
3187 ' and add these lines:
3207 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3211 Steps 'c' and 'd' are needed because bison requires by default that the
3212 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3213 '/usr/local/share/' I think.
3214 So it is necessary to tell bison where those files are located if they
3215 are not in such directory.
3216 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3220 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3221 in 'Show directories for:' select 'Executable files', and in the directories
3222 window add a new path: 'c:
3225 Note that you can use any other path instead of 'c:
3227 util', even the path where the Visual C++ tools are, probably: 'C:
3231 Microsoft Visual Studio
3236 So you don't have to execute step 'e' :)
3240 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3241 the executables from sdcc
3245 bin, and you can compile using sdcc.
3246 \layout Subsubsection
3248 Building SDCC Using Borland
3251 From the sdcc directory, run the command "make -f Makefile.bcc".
3252 This should regenerate all the .exe files in the bin directory except for
3253 sdcdb.exe (which currently doesn't build under Borland C++).
3256 If you modify any source files and need to rebuild, be aware that the dependenci
3257 es may not be correctly calculated.
3258 The safest option is to delete all .obj files and run the build again.
3259 From a Cygwin BASH prompt, this can easily be done with the command (be
3260 sure you are in the sdcc directory):
3270 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3272 ) -print -exec rm {}
3281 or on Windows NT/2000/XP from the command prompt with the command:
3288 del /s *.obj *.lib *.rul
3291 from the sdcc directory.
3292 \layout Subsubsection
3294 Windows Install Using a Binary Package
3295 \begin_inset LatexCommand \label{sub:Windows-Install}
3302 Download the binary package and unpack it using your favorite unpacking
3303 tool (gunzip, WinZip, etc).
3304 This should unpack to a group of sub-directories.
3305 An example directory structure after unpacking the mingw32 package is:
3310 bin for the executables, c:
3318 lib for the include and libraries.
3321 Adjust your environment variable PATH to include the location of the bin
3322 directory or start sdcc using the full path.
3325 Building the Documentation
3328 If the necessary tools are installed it is as easy as changing into the
3329 doc directory and typing
3333 \begin_inset Quotes srd
3337 \begin_inset Quotes srd
3344 If you want to avoid installing the tools you will have some success with
3345 a bootable Knoppix CD
3346 \begin_inset LatexCommand \url{http://www.knopper.net}
3353 Testing the SDCC Compiler
3356 The first thing you should do after installing your SDCC compiler is to
3372 \begin_inset LatexCommand \index{version}
3379 at the prompt, and the program should run and tell you the version.
3380 If it doesn't run, or gives a message about not finding sdcc program, then
3381 you need to check over your installation.
3382 Make sure that the sdcc bin directory is in your executable search path
3383 defined by the PATH environment setting (see the Trouble-shooting section
3385 Make sure that the sdcc program is in the bin folder, if not perhaps something
3386 did not install correctly.
3394 is commonly installed as described in section
3395 \begin_inset Quotes sld
3398 Install and search paths
3399 \begin_inset Quotes srd
3408 Make sure the compiler works on a very simple example.
3409 Type in the following test.c program using your favorite
3435 Compile this using the following command:
3444 If all goes well, the compiler will generate a test.asm and test.rel file.
3445 Congratulations, you've just compiled your first program with SDCC.
3446 We used the -c option to tell SDCC not to link the generated code, just
3447 to keep things simple for this step.
3455 The next step is to try it with the linker.
3465 If all goes well the compiler will link with the libraries and produce
3466 a test.ihx output file.
3471 (no test.ihx, and the linker generates warnings), then the problem is most
3472 likely that sdcc cannot find the
3476 usr/local/share/sdcc/lib directory
3480 (see the Install trouble-shooting section for suggestions).
3488 The final test is to ensure sdcc can use the
3492 header files and libraries.
3493 Edit test.c and change it to the following:
3510 strcpy(str1, "testing");
3517 Compile this by typing
3524 This should generate a test.ihx output file, and it should give no warnings
3525 such as not finding the string.h file.
3526 If it cannot find the string.h file, then the problem is that sdcc cannot
3527 find the /usr/local/share/sdcc/include directory
3531 (see the Install trouble-shooting section for suggestions).
3549 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3553 to find exactly where SDCC is looking for the include and lib files.
3556 Install Trouble-shooting
3557 \begin_inset LatexCommand \index{Install trouble-shooting}
3562 \layout Subsubsection
3564 SDCC does not build correctly.
3567 A thing to try is starting from scratch by unpacking the .tgz source package
3568 again in an empty directory.
3576 ./configure 2>&1 | tee configure.log
3590 make 2>&1 | tee make.log
3597 If anything goes wrong, you can review the log files to locate the problem.
3598 Or a relevant part of this can be attached to an email that could be helpful
3599 when requesting help from the mailing list.
3600 \layout Subsubsection
3603 \begin_inset Quotes sld
3607 \begin_inset Quotes srd
3614 \begin_inset Quotes sld
3618 \begin_inset Quotes srd
3621 command is a script that analyzes your system and performs some configuration
3622 to ensure the source package compiles on your system.
3623 It will take a few minutes to run, and will compile a few tests to determine
3624 what compiler features are installed.
3625 \layout Subsubsection
3628 \begin_inset Quotes sld
3632 \begin_inset Quotes srd
3638 This runs the GNU make tool, which automatically compiles all the source
3639 packages into the final installed binary executables.
3640 \layout Subsubsection
3643 \begin_inset Quotes sld
3647 \begin_inset Quotes erd
3653 This will install the compiler, other executables libraries and include
3654 files into the appropriate directories.
3656 \begin_inset Quotes sld
3659 Install and Search PATHS
3660 \begin_inset Quotes srd
3665 On most systems you will need super-user privileges to do this.
3671 SDCC is not just a compiler, but a collection of tools by various developers.
3672 These include linkers, assemblers, simulators and other components.
3673 Here is a summary of some of the components.
3674 Note that the included simulator and assembler have separate documentation
3675 which you can find in the source package in their respective directories.
3676 As SDCC grows to include support for other processors, other packages from
3677 various developers are included and may have their own sets of documentation.
3681 You might want to look at the files which are installed in <installdir>.
3682 At the time of this writing, we find the following programs for gcc-builds:
3686 In <installdir>/bin:
3689 sdcc - The compiler.
3692 sdcpp - The C preprocessor.
3695 asx8051 - The assembler for 8051 type processors.
3702 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3705 aslink -The linker for 8051 type processors.
3712 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3715 s51 - The ucSim 8051 simulator.
3718 sdcdb - The source debugger.
3721 packihx - A tool to pack (compress) Intel hex files.
3724 In <installdir>/share/sdcc/include
3730 In <installdir>/share/sdcc/lib
3733 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3737 In <installdir>/share/sdcc/doc
3743 As development for other processors proceeds, this list will expand to include
3744 executables to support processors like AVR, PIC, etc.
3745 \layout Subsubsection
3750 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3751 the assembler and linkage editor.
3752 \layout Subsubsection
3755 \begin_inset LatexCommand \index{sdcpp}
3759 - The C-Preprocessor
3762 The preprocessor is a modified version of the GNU preprocessor.
3763 The C preprocessor is used to pull in #include sources, process #ifdef
3764 statements, #defines and so on.
3765 \layout Subsubsection
3767 asx8051, as-z80, as-gbz80, aslink, link-z80, link-gbz80 - The Assemblers
3771 This is retargettable assembler & linkage editor, it was developed by Alan
3773 John Hartman created the version for 8051, and I (Sandeep) have made some
3774 enhancements and bug fixes for it to work properly with SDCC.
3775 \layout Subsubsection
3778 \begin_inset LatexCommand \index{s51}
3785 S51 is a freeware, opensource simulator developed by Daniel Drotos (
3786 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
3791 The simulator is built as part of the build process.
3792 For more information visit Daniel's web site at:
3793 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
3798 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
3800 \layout Subsubsection
3803 \begin_inset LatexCommand \index{sdcdb}
3807 - Source Level Debugger
3810 Sdcdb is the companion source level debugger.
3811 The current version of the debugger uses Daniel's Simulator S51
3812 \begin_inset LatexCommand \index{s51}
3816 , but can be easily changed to use other simulators.
3823 \layout Subsubsection
3825 Single Source File Projects
3828 For single source file 8051 projects the process is very simple.
3829 Compile your programs with the following command
3832 "sdcc sourcefile.c".
3836 This will compile, assemble and link your source file.
3837 Output files are as follows
3841 \begin_inset LatexCommand \index{.asm}
3846 \begin_inset LatexCommand \index{Assembler source}
3850 file created by the compiler
3854 \begin_inset LatexCommand \index{.lst}
3859 \begin_inset LatexCommand \index{Assembler listing}
3863 file created by the Assembler
3867 \begin_inset LatexCommand \index{.rst}
3872 \begin_inset LatexCommand \index{Assembler listing}
3876 file updated with linkedit information, created by linkage editor
3880 \begin_inset LatexCommand \index{.sym}
3885 \begin_inset LatexCommand \index{Symbol listing}
3889 for the sourcefile, created by the assembler
3893 \begin_inset LatexCommand \index{.rel}
3898 \begin_inset LatexCommand \index{Object file}
3902 created by the assembler, input to Linkage editor
3906 \begin_inset LatexCommand \index{.map}
3911 \begin_inset LatexCommand \index{Memory map}
3915 for the load module, created by the Linker
3919 \begin_inset LatexCommand \index{.mem}
3923 - A file with a summary of the memory usage
3927 \begin_inset LatexCommand \index{.ihx}
3931 - The load module in Intel hex format
3932 \begin_inset LatexCommand \index{Intel hex format}
3936 (you can select the Motorola S19 format
3937 \begin_inset LatexCommand \index{Motorola S19 format}
3952 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
3957 If you need another format you might want to use
3964 \begin_inset LatexCommand \index{objdump}
3975 \begin_inset LatexCommand \index{srecord}
3983 \begin_inset LatexCommand \index{.adb}
3987 - An intermediate file containing debug information needed to create the
3999 \begin_inset LatexCommand \index{-\/-debug}
4007 \begin_inset LatexCommand \index{.cdb}
4011 - An optional file (with -
4021 -debug) containing debug information
4026 \begin_inset LatexCommand \index{. (no extension)}
4031 \begin_inset LatexCommand \index{AOMF51}
4035 file containing debug information (with -
4046 This format is commonly used by third party tools (debuggers
4047 \begin_inset LatexCommand \index{Debugger}
4051 , simulators, emulators)
4055 \begin_inset LatexCommand \index{.dump*}
4059 - Dump file to debug the compiler it self (with -
4069 -dumpall) (see section
4070 \begin_inset Quotes sld
4073 Anatomy of the compiler
4074 \begin_inset Quotes srd
4078 \layout Subsubsection
4080 Projects with Multiple Source Files
4083 SDCC can compile only ONE file at a time.
4084 Let us for example assume that you have a project containing the following
4089 foo1.c (contains some functions)
4091 foo2.c (contains some more functions)
4093 foomain.c (contains more functions and the function main)
4101 The first two files will need to be compiled separately with the commands:
4133 Then compile the source file containing the
4138 \begin_inset LatexCommand \index{Linker}
4142 the files together with the following command:
4150 foomain.c\SpecialChar ~
4151 foo1.rel\SpecialChar ~
4156 \begin_inset LatexCommand \index{.rel}
4168 can be separately compiled as well:
4179 sdcc foomain.rel foo1.rel foo2.rel
4186 The file containing the
4201 file specified in the command line, since the linkage editor processes
4202 file in the order they are presented to it.
4203 The linker is invoked from sdcc using a script file with extension .lnk
4204 \begin_inset LatexCommand \index{.lnk}
4209 You can view this file to troubleshoot linking problems such as those arising
4210 from missing libraries.
4211 \layout Subsubsection
4213 Projects with Additional Libraries
4214 \begin_inset LatexCommand \index{Libraries}
4221 Some reusable routines may be compiled into a library, see the documentation
4222 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4226 \begin_inset LatexCommand \index{.lib}
4233 Libraries created in this manner can be included in the command line.
4234 Make sure you include the -L <library-path> option to tell the linker where
4235 to look for these files if they are not in the current directory.
4236 Here is an example, assuming you have the source file
4248 (if that is not the same as your current project):
4255 sdcc foomain.c foolib.lib -L mylib
4266 must be an absolute path name.
4270 The most efficient way to use libraries is to keep separate modules in separate
4272 The lib file now should name all the modules.rel
4273 \begin_inset LatexCommand \index{rel}
4278 For an example see the standard library file
4282 in the directory <installdir>/share/lib/small.
4285 Command Line Options
4286 \begin_inset LatexCommand \index{Command Line Options}
4291 \layout Subsubsection
4293 Processor Selection Options
4294 \begin_inset LatexCommand \index{Options processor selection}
4299 \begin_inset LatexCommand \index{Processor selection options}
4305 \labelwidthstring 00.00.0000
4310 \begin_inset LatexCommand \index{-mmcs51}
4316 Generate code for the MCS51
4317 \begin_inset LatexCommand \index{MCS51}
4321 family of processors.
4322 This is the default processor target.
4324 \labelwidthstring 00.00.0000
4329 \begin_inset LatexCommand \index{-mds390}
4335 Generate code for the Dallas DS80C390
4336 \begin_inset LatexCommand \index{DS80C390}
4342 \labelwidthstring 00.00.0000
4347 \begin_inset LatexCommand \index{-mds400}
4353 Generate code for the Dallas DS80C400
4354 \begin_inset LatexCommand \index{DS80C400}
4360 \labelwidthstring 00.00.0000
4365 \begin_inset LatexCommand \index{-mz80}
4371 Generate code for the Zilog Z80
4372 \begin_inset LatexCommand \index{Z80}
4376 family of processors.
4378 \labelwidthstring 00.00.0000
4383 \begin_inset LatexCommand \index{-mgbz80}
4389 Generate code for the GameBoy Z80
4390 \begin_inset LatexCommand \index{GameBoy Z80}
4396 \labelwidthstring 00.00.0000
4401 \begin_inset LatexCommand \index{-mavr}
4407 Generate code for the Atmel AVR
4408 \begin_inset LatexCommand \index{AVR}
4412 processor (In development, not complete).
4413 AVR users should probably have a look at avr-gcc
4414 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4421 I think it is fair to direct users there for now.
4422 Open source is also about avoiding unnecessary work .
4423 But I didn't find the 'official' link.
4425 \labelwidthstring 00.00.0000
4430 \begin_inset LatexCommand \index{-mpic14}
4436 Generate code for the Microchip PIC 14
4437 \begin_inset LatexCommand \index{PIC14}
4441 -bit processors (p16f84 and variants).
4444 p16f627 p16f628 p16f84 p16f873 p16f877?
4446 \labelwidthstring 00.00.0000
4451 \begin_inset LatexCommand \index{-mpic16}
4457 Generate code for the Microchip PIC 16
4458 \begin_inset LatexCommand \index{PIC16}
4462 -bit processors (p18f452 and variants).
4464 \labelwidthstring 00.00.0000
4470 Generate code for the Toshiba TLCS-900H
4471 \begin_inset LatexCommand \index{TLCS-900H}
4475 processor (In development, not complete).
4477 \labelwidthstring 00.00.0000
4482 \begin_inset LatexCommand \index{-mxa51}
4488 Generate code for the Phillips XA51
4489 \begin_inset LatexCommand \index{XA51}
4493 processor (In development, not complete).
4494 \layout Subsubsection
4496 Preprocessor Options
4497 \begin_inset LatexCommand \index{Options preprocessor}
4502 \begin_inset LatexCommand \index{Preprocessor options}
4508 \labelwidthstring 00.00.0000
4513 \begin_inset LatexCommand \index{-I<path>}
4519 The additional location where the pre processor will look for <..h> or
4520 \begin_inset Quotes eld
4524 \begin_inset Quotes erd
4529 \labelwidthstring 00.00.0000
4534 \begin_inset LatexCommand \index{-D<macro[=value]>}
4540 Command line definition of macros.
4541 Passed to the preprocessor.
4543 \labelwidthstring 00.00.0000
4548 \begin_inset LatexCommand \index{-M}
4554 Tell the preprocessor to output a rule suitable for make describing the
4555 dependencies of each object file.
4556 For each source file, the preprocessor outputs one make-rule whose target
4557 is the object file name for that source file and whose dependencies are
4558 all the files `#include'd in it.
4559 This rule may be a single line or may be continued with `
4561 '-newline if it is long.
4562 The list of rules is printed on standard output instead of the preprocessed
4565 \begin_inset LatexCommand \index{-E}
4571 \labelwidthstring 00.00.0000
4576 \begin_inset LatexCommand \index{-C}
4582 Tell the preprocessor not to discard comments.
4583 Used with the `-E' option.
4585 \labelwidthstring 00.00.0000
4590 \begin_inset LatexCommand \index{-MM}
4601 Like `-M' but the output mentions only the user header files included with
4603 \begin_inset Quotes eld
4607 System header files included with `#include <file>' are omitted.
4609 \labelwidthstring 00.00.0000
4614 \begin_inset LatexCommand \index{-Aquestion(answer)}
4620 Assert the answer answer for question, in case it is tested with a preprocessor
4621 conditional such as `#if #question(answer)'.
4622 `-A-' disables the standard assertions that normally describe the target
4625 \labelwidthstring 00.00.0000
4630 \begin_inset LatexCommand \index{-Umacro}
4636 Undefine macro macro.
4637 `-U' options are evaluated after all `-D' options, but before any `-include'
4638 and `-imacros' options.
4640 \labelwidthstring 00.00.0000
4645 \begin_inset LatexCommand \index{-dM}
4651 Tell the preprocessor to output only a list of the macro definitions that
4652 are in effect at the end of preprocessing.
4653 Used with the `-E' option.
4655 \labelwidthstring 00.00.0000
4660 \begin_inset LatexCommand \index{-dD}
4666 Tell the preprocessor to pass all macro definitions into the output, in
4667 their proper sequence in the rest of the output.
4669 \labelwidthstring 00.00.0000
4674 \begin_inset LatexCommand \index{-dN}
4685 Like `-dD' except that the macro arguments and contents are omitted.
4686 Only `#define name' is included in the output.
4687 \layout Subsubsection
4690 \begin_inset LatexCommand \index{Options linker}
4695 \begin_inset LatexCommand \index{Linker options}
4701 \labelwidthstring 00.00.0000
4721 \begin_inset LatexCommand \index{-\/-lib-path}
4726 \begin_inset LatexCommand \index{-L -\/-lib-path}
4735 <absolute path to additional libraries> This option is passed to the linkage
4736 editor's additional libraries
4737 \begin_inset LatexCommand \index{Libraries}
4742 The path name must be absolute.
4743 Additional library files may be specified in the command line.
4744 See section Compiling programs for more details.
4746 \labelwidthstring 00.00.0000
4763 \begin_inset LatexCommand \index{-\/-xram-loc}
4767 <Value> The start location of the external ram
4768 \begin_inset LatexCommand \index{xdata}
4772 , default value is 0.
4773 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4783 -xram-loc 0x8000 or -
4795 \labelwidthstring 00.00.0000
4812 \begin_inset LatexCommand \index{-\/-code-loc}
4816 <Value> The start location of the code
4817 \begin_inset LatexCommand \index{code}
4821 segment, default value 0.
4822 Note when this option is used the interrupt vector table is also relocated
4823 to the given address.
4824 The value entered can be in Hexadecimal or Decimal format, e.g.: -
4834 -code-loc 0x8000 or -
4846 \labelwidthstring 00.00.0000
4863 \begin_inset LatexCommand \index{-\/-stack-loc}
4867 <Value> By default the stack
4868 \begin_inset LatexCommand \index{stack}
4872 is placed after the data segment.
4873 Using this option the stack can be placed anywhere in the internal memory
4875 The value entered can be in Hexadecimal or Decimal format, e.g.
4886 -stack-loc 0x20 or -
4897 Since the sp register is incremented before a push or call, the initial
4898 sp will be set to one byte prior the provided value.
4899 The provided value should not overlap any other memory areas such as used
4900 register banks or the data segment and with enough space for the current
4903 \labelwidthstring 00.00.0000
4920 \begin_inset LatexCommand \index{-\/-data-loc}
4924 <Value> The start location of the internal ram data
4925 \begin_inset LatexCommand \index{data}
4930 The value entered can be in Hexadecimal or Decimal format, eg.
4952 (By default, the start location of the internal ram data segment is set
4953 as low as possible in memory, taking into account the used register banks
4954 and the bit segment at address 0x20.
4955 For example if register banks 0 and 1 are used without bit variables, the
4956 data segment will be set, if -
4966 -data-loc is not used, to location 0x10.)
4968 \labelwidthstring 00.00.0000
4985 \begin_inset LatexCommand \index{-\/-idata-loc}
4989 <Value> The start location of the indirectly addressable internal ram
4990 \begin_inset LatexCommand \index{idata}
4994 , default value is 0x80.
4995 The value entered can be in Hexadecimal or Decimal format, eg.
5006 -idata-loc 0x88 or -
5018 \labelwidthstring 00.00.0000
5033 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5042 The linker output (final object code) is in Intel Hex format.
5043 \begin_inset LatexCommand \index{Intel hex format}
5047 (This is the default option).
5049 \labelwidthstring 00.00.0000
5064 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5073 The linker output (final object code) is in Motorola S19 format
5074 \begin_inset LatexCommand \index{Motorola S19 format}
5079 \layout Subsubsection
5082 \begin_inset LatexCommand \index{Options MCS51}
5087 \begin_inset LatexCommand \index{MCS51 options}
5093 \labelwidthstring 00.00.0000
5108 \begin_inset LatexCommand \index{-\/-model-small}
5119 Generate code for Small Model programs see section Memory Models for more
5121 This is the default model.
5123 \labelwidthstring 00.00.0000
5138 \begin_inset LatexCommand \index{-\/-model-large}
5144 Generate code for Large model programs see section Memory Models for more
5146 If this option is used all source files in the project should be compiled
5149 \labelwidthstring 00.00.0000
5164 \begin_inset LatexCommand \index{-\/-xstack}
5170 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5171 variables and passing parameters.
5172 See section on external stack for more details.
5174 \labelwidthstring 00.00.0000
5191 \begin_inset LatexCommand \index{-\/-iram-size<Value>}
5195 Causes the linker to check if the internal ram usage is within limits of
5198 \labelwidthstring 00.00.0000
5215 \begin_inset LatexCommand \index{-\/-xram-size<Value>}
5219 Causes the linker to check if the external ram usage is within limits of
5221 \layout Subsubsection
5224 \begin_inset LatexCommand \index{Options DS390}
5229 \begin_inset LatexCommand \index{DS390 options}
5235 \labelwidthstring 00.00.0000
5252 \begin_inset LatexCommand \index{-\/-model-flat24}
5262 Generate 24-bit flat mode code.
5263 This is the one and only that the ds390 code generator supports right now
5264 and is default when using
5269 See section Memory Models for more details.
5271 \labelwidthstring 00.00.0000
5288 \begin_inset LatexCommand \index{-\/-stack-10bit}
5292 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
5293 This is the one and only that the ds390 code generator supports right now
5294 and is default when using
5299 In this mode, the stack is located in the lower 1K of the internal RAM,
5300 which is mapped to 0x400000.
5301 Note that the support is incomplete, since it still uses a single byte
5302 as the stack pointer.
5303 This means that only the lower 256 bytes of the potential 1K stack space
5304 will actually be used.
5305 However, this does allow you to reclaim the precious 256 bytes of low RAM
5306 for use for the DATA and IDATA segments.
5307 The compiler will not generate any code to put the processor into 10 bit
5309 It is important to ensure that the processor is in this mode before calling
5310 any re-entrant functions compiled with this option.
5311 In principle, this should work with the
5324 \begin_inset LatexCommand \index{-\/-stack-auto}
5330 option, but that has not been tested.
5331 It is incompatible with the
5344 \begin_inset LatexCommand \index{-\/-xstack}
5351 It also only makes sense if the processor is in 24 bit contiguous addressing
5364 -model-flat24 option
5367 \layout Subsubsection
5370 \begin_inset LatexCommand \index{Options Z80}
5375 \begin_inset LatexCommand \index{Z80 options}
5381 \labelwidthstring 00.00.0000
5398 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
5408 Force a called function to always save BC.
5410 \labelwidthstring 00.00.0000
5427 \begin_inset LatexCommand \index{-\/-no-std-crt0}
5431 When linking, skip the standard crt0.o object file.
5432 You must provide your own crt0.o for your system when linking.
5434 \layout Subsubsection
5436 Optimization Options
5437 \begin_inset LatexCommand \index{Options optimization}
5442 \begin_inset LatexCommand \index{Optimization options}
5448 \labelwidthstring 00.00.0000
5463 \begin_inset LatexCommand \index{-\/-nogcse}
5469 Will not do global subexpression elimination, this option may be used when
5470 the compiler creates undesirably large stack/data spaces to store compiler
5472 A warning message will be generated when this happens and the compiler
5473 will indicate the number of extra bytes it allocated.
5474 It recommended that this option NOT be used, #pragma\SpecialChar ~
5476 \begin_inset LatexCommand \index{\#pragma NOGCSE}
5480 can be used to turn off global subexpression elimination
5481 \begin_inset LatexCommand \index{Subexpression elimination}
5485 for a given function only.
5487 \labelwidthstring 00.00.0000
5502 \begin_inset LatexCommand \index{-\/-noinvariant}
5508 Will not do loop invariant optimizations, this may be turned off for reasons
5509 explained for the previous option.
5510 For more details of loop optimizations performed see section Loop Invariants.It
5511 recommended that this option NOT be used, #pragma\SpecialChar ~
5513 \begin_inset LatexCommand \index{\#pragma NOINVARIANT}
5517 can be used to turn off invariant optimizations for a given function only.
5519 \labelwidthstring 00.00.0000
5534 \begin_inset LatexCommand \index{-\/-noinduction}
5540 Will not do loop induction optimizations, see section strength reduction
5541 for more details.It is recommended that this option is NOT used, #pragma\SpecialChar ~
5544 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
5548 can be used to turn off induction optimizations for a given function only.
5550 \labelwidthstring 00.00.0000
5565 \begin_inset LatexCommand \index{-\/-nojtbound}
5576 Will not generate boundary condition check when switch statements
5577 \begin_inset LatexCommand \index{switch statement}
5581 are implemented using jump-tables.
5582 See section Switch Statements for more details.
5583 It is recommended that this option is NOT used, #pragma\SpecialChar ~
5585 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
5589 can be used to turn off boundary checking for jump tables for a given function
5592 \labelwidthstring 00.00.0000
5607 \begin_inset LatexCommand \index{-\/-noloopreverse}
5616 Will not do loop reversal
5617 \begin_inset LatexCommand \index{Loop reversing}
5623 \labelwidthstring 00.00.0000
5640 \begin_inset LatexCommand \index{-\/-nolabelopt }
5644 Will not optimize labels (makes the dumpfiles more readable).
5646 \labelwidthstring 00.00.0000
5661 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
5667 Will not memcpy initialized data from code space into xdata space.
5668 This saves a few bytes in code space if you don't have initialized data.
5669 \layout Subsubsection
5672 \begin_inset LatexCommand \index{Options other}
5678 \labelwidthstring 00.00.0000
5694 \begin_inset LatexCommand \index{-\/-compile-only}
5699 \begin_inset LatexCommand \index{-c -\/-compile-only}
5705 will compile and assemble the source, but will not call the linkage editor.
5707 \labelwidthstring 00.00.0000
5726 \begin_inset LatexCommand \index{-\/-c1mode}
5732 reads the preprocessed source from standard input and compiles it.
5733 The file name for the assembler output must be specified using the -o option.
5735 \labelwidthstring 00.00.0000
5740 \begin_inset LatexCommand \index{-E}
5746 Run only the C preprocessor.
5747 Preprocess all the C source files specified and output the results to standard
5750 \labelwidthstring 00.00.0000
5756 \begin_inset LatexCommand \index{-o <path/file>}
5762 The output path resp.
5763 file where everything will be placed.
5764 If the parameter is a path, it must have a trailing slash (or backslash
5765 for the Windows binaries) to be recognized as a path.
5768 \labelwidthstring 00.00.0000
5783 \begin_inset LatexCommand \index{-\/-stack-auto}
5794 All functions in the source file will be compiled as
5799 \begin_inset LatexCommand \index{reentrant}
5804 the parameters and local variables will be allocated on the stack
5805 \begin_inset LatexCommand \index{stack}
5810 see section Parameters and Local Variables for more details.
5811 If this option is used all source files in the project should be compiled
5815 \labelwidthstring 00.00.0000
5830 \begin_inset LatexCommand \index{-\/-callee-saves}
5834 function1[,function2][,function3]....
5837 The compiler by default uses a caller saves convention for register saving
5838 across function calls, however this can cause unnecessary register pushing
5839 & popping when calling small functions from larger functions.
5840 This option can be used to switch the register saving convention for the
5841 function names specified.
5842 The compiler will not save registers when calling these functions, no extra
5843 code will be generated at the entry & exit (function prologue
5846 \begin_inset LatexCommand \index{function prologue}
5855 \begin_inset LatexCommand \index{function epilogue}
5861 ) for these functions to save & restore the registers used by these functions,
5862 this can SUBSTANTIALLY reduce code & improve run time performance of the
5864 In the future the compiler (with inter procedural analysis) will be able
5865 to determine the appropriate scheme to use for each function call.
5866 DO NOT use this option for built-in functions such as _mulint..., if this
5867 option is used for a library function the appropriate library function
5868 needs to be recompiled with the same option.
5869 If the project consists of multiple source files then all the source file
5870 should be compiled with the same -
5880 -callee-saves option string.
5881 Also see #pragma\SpecialChar ~
5883 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
5889 \labelwidthstring 00.00.0000
5904 \begin_inset LatexCommand \index{-\/-debug}
5913 When this option is used the compiler will generate debug information, that
5914 can be used with the SDCDB.
5915 The debug information is collected in a file with .cdb extension.
5916 For more information see documentation for SDCDB.
5918 \labelwidthstring 00.00.0000
5923 \begin_inset LatexCommand \index{-S}
5934 Stop after the stage of compilation proper; do not assemble.
5935 The output is an assembler code file for the input file specified.
5937 \labelwidthstring 00.00.0000
5941 -Wa_asmOption[,asmOption]
5944 \begin_inset LatexCommand \index{-Wa\_asmOption[,asmOption]}
5949 Pass the asmOption to the assembler.
5951 \labelwidthstring 00.00.0000
5955 -Wl_linkOption[,linkOption]
5958 \begin_inset LatexCommand \index{-Wl\_linkOption[,linkOption]}
5963 Pass the linkOption to the linker.
5965 \labelwidthstring 00.00.0000
5980 \begin_inset LatexCommand \index{-\/-int-long-reent}
5986 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
5987 Note by default these libraries are compiled as non-reentrant.
5988 See section Installation for more details.
5990 \labelwidthstring 00.00.0000
6005 \begin_inset LatexCommand \index{-\/-cyclomatic}
6014 This option will cause the compiler to generate an information message for
6015 each function in the source file.
6016 The message contains some
6020 information about the function.
6021 The number of edges and nodes the compiler detected in the control flow
6022 graph of the function, and most importantly the
6024 cyclomatic complexity
6025 \begin_inset LatexCommand \index{Cyclomatic complexity}
6031 see section on Cyclomatic Complexity for more details.
6033 \labelwidthstring 00.00.0000
6048 \begin_inset LatexCommand \index{-\/-float-reent}
6057 Floating point library is compiled as reentrant
6058 \begin_inset LatexCommand \index{reentrant}
6062 .See section Installation for more details.
6064 \labelwidthstring 00.00.0000
6079 \begin_inset LatexCommand \index{-\/-nooverlay}
6085 The compiler will not overlay parameters and local variables of any function,
6086 see section Parameters and local variables for more details.
6088 \labelwidthstring 00.00.0000
6103 \begin_inset LatexCommand \index{-\/-main-return}
6109 This option can be used when the code generated is called by a monitor
6111 The compiler will generate a 'ret' upon return from the 'main'
6112 \begin_inset LatexCommand \index{main return}
6117 The default option is to lock up i.e.
6120 \labelwidthstring 00.00.0000
6137 \begin_inset LatexCommand \index{-\/-peep-file}
6141 <filename> This option can be used to use additional rules to be used by
6142 the peep hole optimizer.
6143 See section Peep Hole optimizations for details on how to write these rules.
6145 \labelwidthstring 00.00.0000
6160 \begin_inset LatexCommand \index{-\/-no-peep}
6166 Disable peep-hole optimization.
6168 \labelwidthstring 00.00.0000
6183 \begin_inset LatexCommand \index{-\/-peep-asm}
6189 Pass the inline assembler code through the peep hole optimizer.
6190 This can cause unexpected changes to inline assembler code, please go through
6191 the peephole optimizer
6192 \begin_inset LatexCommand \index{Peephole optimizer}
6196 rules defined in the source file tree '<target>/peeph.def' before using
6199 \labelwidthstring 00.00.0000
6216 \begin_inset LatexCommand \index{-\/-code-size<Value>}
6220 Causes the linker to check if the code usage is within limits of the given
6223 \labelwidthstring 00.00.0000
6238 \begin_inset LatexCommand \index{-\/-nostdincl}
6244 This will prevent the compiler from passing on the default include path
6245 to the preprocessor.
6247 \labelwidthstring 00.00.0000
6262 \begin_inset LatexCommand \index{-\/-nostdlib}
6268 This will prevent the compiler from passing on the default library
6269 \begin_inset LatexCommand \index{Libraries}
6275 \labelwidthstring 00.00.0000
6290 \begin_inset LatexCommand \index{-\/-verbose}
6296 Shows the various actions the compiler is performing.
6298 \labelwidthstring 00.00.0000
6303 \begin_inset LatexCommand \index{-V}
6309 Shows the actual commands the compiler is executing.
6311 \labelwidthstring 00.00.0000
6326 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
6332 Hides your ugly and inefficient c-code from the asm file, so you can always
6333 blame the compiler :).
6335 \labelwidthstring 00.00.0000
6350 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
6356 Include i-codes in the asm file.
6357 Sounds like noise but is most helpful for debugging the compiler itself.
6359 \labelwidthstring 00.00.0000
6374 \begin_inset LatexCommand \index{-\/-less-pedantic}
6380 Disable some of the more pedantic warnings (jwk burps: please be more specific
6383 \labelwidthstring 00.00.0000
6398 \begin_inset LatexCommand \index{-\/-print-search-dirs}
6404 Display the directories in the compiler's search path
6406 \labelwidthstring 00.00.0000
6421 \begin_inset LatexCommand \index{-\/-vc}
6427 Display errors and warnings using MSVC style, so you can use SDCC with
6430 \labelwidthstring 00.00.0000
6445 \begin_inset LatexCommand \index{-\/-use-stdout}
6451 Send errors and warnings to stdout instead of stderr
6452 \layout Subsubsection
6454 Intermediate Dump Options
6455 \begin_inset LatexCommand \index{Options intermediate dump}
6460 \begin_inset LatexCommand \index{Intermediate dump options}
6467 The following options are provided for the purpose of retargetting and debugging
6469 These provided a means to dump the intermediate code (iCode
6470 \begin_inset LatexCommand \index{iCode}
6474 ) generated by the compiler in human readable form at various stages of
6475 the compilation process.
6478 \labelwidthstring 00.00.0000
6493 \begin_inset LatexCommand \index{-\/-dumpraw}
6499 This option will cause the compiler to dump the intermediate code into
6502 <source filename>.dumpraw
6504 just after the intermediate code has been generated for a function, i.e.
6505 before any optimizations are done.
6507 \begin_inset LatexCommand \index{Basic blocks}
6511 at this stage ordered in the depth first number, so they may not be in
6512 sequence of execution.
6514 \labelwidthstring 00.00.0000
6529 \begin_inset LatexCommand \index{-\/-dumpgcse}
6535 Will create a dump of iCode's, after global subexpression elimination
6536 \begin_inset LatexCommand \index{Global subexpression elimination}
6542 <source filename>.dumpgcse.
6544 \labelwidthstring 00.00.0000
6559 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
6565 Will create a dump of iCode's, after deadcode elimination
6566 \begin_inset LatexCommand \index{Dead-code elimination}
6572 <source filename>.dumpdeadcode.
6574 \labelwidthstring 00.00.0000
6589 \begin_inset LatexCommand \index{-\/-dumploop}
6598 Will create a dump of iCode's, after loop optimizations
6599 \begin_inset LatexCommand \index{Loop optimization}
6605 <source filename>.dumploop.
6607 \labelwidthstring 00.00.0000
6622 \begin_inset LatexCommand \index{-\/-dumprange}
6631 Will create a dump of iCode's, after live range analysis
6632 \begin_inset LatexCommand \index{Live range analysis}
6638 <source filename>.dumprange.
6640 \labelwidthstring 00.00.0000
6655 \begin_inset LatexCommand \index{-\/-dumlrange}
6661 Will dump the life ranges
6662 \begin_inset LatexCommand \index{Live range analysis}
6668 \labelwidthstring 00.00.0000
6683 \begin_inset LatexCommand \index{-\/-dumpregassign}
6692 Will create a dump of iCode's, after register assignment
6693 \begin_inset LatexCommand \index{Register assignment}
6699 <source filename>.dumprassgn.
6701 \labelwidthstring 00.00.0000
6716 \begin_inset LatexCommand \index{-\/-dumplrange}
6722 Will create a dump of the live ranges of iTemp's
6724 \labelwidthstring 00.00.0000
6739 \begin_inset LatexCommand \index{-\/-dumpall}
6750 Will cause all the above mentioned dumps to be created.
6753 Environment variables
6754 \begin_inset LatexCommand \index{Environment variables}
6761 SDCC recognizes the following environment variables:
6763 \labelwidthstring 00.00.0000
6768 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
6774 SDCC installs a signal handler
6775 \begin_inset LatexCommand \index{signal handler}
6779 to be able to delete temporary files after an user break (^C) or an exception.
6780 If this environment variable is set, SDCC won't install the signal handler
6781 in order to be able to debug SDCC.
6783 \labelwidthstring 00.00.0000
6790 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
6796 Path, where temporary files will be created.
6797 The order of the variables is the search order.
6798 In a standard *nix environment these variables are not set, and there's
6799 no need to set them.
6800 On Windows it's recommended to set one of them.
6802 \labelwidthstring 00.00.0000
6807 \begin_inset LatexCommand \index{SDCC\_HOME}
6814 \begin_inset Quotes sld
6817 2.3 Install and search paths
6818 \begin_inset Quotes srd
6823 \labelwidthstring 00.00.0000
6828 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
6835 \begin_inset Quotes sld
6838 2.3 Install and search paths
6839 \begin_inset Quotes srd
6844 \labelwidthstring 00.00.0000
6849 \begin_inset LatexCommand \index{SDCC\_LIB}
6856 \begin_inset Quotes sld
6859 2.3 Install and search paths
6860 \begin_inset Quotes srd
6866 There are some more environment variables recognized by SDCC, but these
6867 are solely used for debugging purposes.
6868 They can change or disappear very quickly, and will never be documented.
6871 MCS51/DS390 Storage Class
6872 \begin_inset LatexCommand \index{Storage class}
6879 In addition to the ANSI storage classes SDCC allows the following MCS51
6880 specific storage classes.
6881 \layout Subsubsection
6884 \begin_inset LatexCommand \index{data}
6895 storage class for Small Memory model.
6896 Variables declared with this storage class will be allocated in the directly
6897 addressable portion of the internal RAM of a 8051, e.g.:
6902 data unsigned char test_data;
6905 Writing 0x01 to this variable generates the assembly code:
6910 75*00 01\SpecialChar ~
6916 \layout Subsubsection
6919 \begin_inset LatexCommand \index{xdata}
6926 Variables declared with this storage class will be placed in the external
6932 storage class for Large Memory model, e.g.:
6937 xdata unsigned char test_xdata;
6940 Writing 0x01 to this variable generates the assembly code:
6945 90s00r00\SpecialChar ~
6974 \layout Subsubsection
6977 \begin_inset LatexCommand \index{idata}
6984 Variables declared with this storage class will be allocated into the indirectly
6985 addressable portion of the internal ram of a 8051, e.g.:
6990 idata unsigned char test_idata;
6993 Writing 0x01 to this variable generates the assembly code:
7020 \layout Subsubsection
7023 \begin_inset LatexCommand \index{pdata}
7030 Paged xdata access is currently not as straightforward as using the other
7031 addressing modes of a 8051.
7032 The following example writes 0x01 to the address pointed to.
7033 Please note, pdata access physically accesses xdata memory.
7034 The high byte of the address is determined by port P2 (or in case of some
7035 8051 variants by a separate Special Function Register).
7040 pdata unsigned char *test_pdata_ptr;
7052 test_pdata_ptr = (pdata *)0xfe;
7058 *test_pdata_ptr = 1;
7063 Generates the assembly code:
7068 75*01 FE\SpecialChar ~
7072 _test_pdata_ptr,#0xFE
7104 Be extremely carefull if you use pdata together with the -
7115 \begin_inset LatexCommand \index{-\/-xstack}
7120 \layout Subsubsection
7123 \begin_inset LatexCommand \index{code}
7130 'Variables' declared with this storage class will be placed in the code
7136 code unsigned char test_code;
7139 Read access to this variable generates the assembly code:
7144 90s00r6F\SpecialChar ~
7147 mov dptr,#_test_code
7170 \layout Subsubsection
7173 \begin_inset LatexCommand \index{bit}
7180 This is a data-type and a storage class specifier.
7181 When a variable is declared as a bit, it is allocated into the bit addressable
7182 memory of 8051, e.g.:
7190 Writing 1 to this variable generates the assembly code:
7204 \layout Subsubsection
7207 \begin_inset LatexCommand \index{sfr}
7212 \begin_inset LatexCommand \index{sbit}
7219 Like the bit keyword,
7223 signifies both a data-type and storage class, they are used to describe
7224 the special function registers and special bit variables of a 8051, eg:
7230 \begin_inset LatexCommand \index{at}
7234 0x80 P0;\SpecialChar ~
7235 /* special function register P0 at location 0x80 */
7237 sbit at 0xd7 CY; /* CY (Carry Flag
7238 \begin_inset LatexCommand \index{Flags}
7243 \begin_inset LatexCommand \index{Carry flag}
7251 \begin_inset LatexCommand \index{Pointers}
7258 SDCC allows (via language extensions) pointers to explicitly point to any
7259 of the memory spaces
7260 \begin_inset LatexCommand \index{Memory model}
7265 In addition to the explicit pointers, the compiler uses (by default) generic
7266 pointers which can be used to point to any of the memory spaces.
7270 Pointer declaration examples:
7275 /* pointer physically in xternal ram pointing to object in internal ram
7278 data unsigned char * xdata p;
7282 /* pointer physically in code rom pointing to data in xdata space */
7284 xdata unsigned char * code p;
7288 /* pointer physically in code space pointing to data in code space */
7290 code unsigned char * code p;
7294 /* the following is a generic pointer physically located in xdata space
7300 Well you get the idea.
7305 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
7318 The highest order byte of the
7322 pointers contains the data space information.
7323 Assembler support routines are called whenever data is stored or retrieved
7329 These are useful for developing reusable library
7330 \begin_inset LatexCommand \index{Libraries}
7335 Explicitly specifying the pointer type will generate the most efficient
7340 \begin_inset LatexCommand \index{Parameters}
7345 \begin_inset LatexCommand \index{Local variable}
7352 Automatic (local) variables and parameters to functions can either be placed
7353 on the stack or in data-space.
7354 The default action of the compiler is to place these variables in the internal
7355 RAM (for small model) or external RAM (for large model).
7356 This in fact makes them
7359 \begin_inset LatexCommand \index{static}
7365 so by default functions are non-reentrant
7366 \begin_inset LatexCommand \index{reentrant}
7374 They can be placed on the stack
7375 \begin_inset LatexCommand \index{stack}
7392 \begin_inset LatexCommand \index{-\/-stack-auto}
7398 option or by using the
7401 \begin_inset LatexCommand \index{reentrant}
7407 keyword in the function declaration, e.g.:
7412 unsigned char foo(char i) reentrant
7426 Since stack space on 8051 is limited, the
7444 option should be used sparingly.
7445 Note that the reentrant keyword just means that the parameters & local
7446 variables will be allocated to the stack, it
7450 mean that the function is register bank independent.
7454 Local variables can be assigned storage classes and absolute
7455 \begin_inset LatexCommand \index{Absolute addressing}
7472 xdata unsigned char i;
7484 data at 0x31 unsigned char j;
7496 In the above example the variable
7500 will be allocated in the external ram,
7504 in bit addressable space and
7523 or when a function is declared as
7527 this should only be done for static variables.
7530 Parameters however are not allowed any storage class, (storage classes for
7531 parameters will be ignored), their allocation is governed by the memory
7532 model in use, and the reentrancy options.
7536 \begin_inset LatexCommand \label{sub:Overlaying}
7541 \begin_inset LatexCommand \index{Overlaying}
7549 \begin_inset LatexCommand \index{reentrant}
7553 functions SDCC will try to reduce internal ram space usage by overlaying
7554 parameters and local variables of a function (if possible).
7555 Parameters and local variables of a function will be allocated to an overlayabl
7556 e segment if the function has
7558 no other function calls and the function is non-reentrant and the memory
7560 \begin_inset LatexCommand \index{Memory model}
7567 If an explicit storage class
7568 \begin_inset LatexCommand \index{Storage class}
7572 is specified for a local variable, it will NOT be overlayed.
7575 Note that the compiler (not the linkage editor) makes the decision for overlayin
7577 Functions that are called from an interrupt service routine should be preceded
7578 by a #pragma\SpecialChar ~
7580 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7584 if they are not reentrant.
7587 Also note that the compiler does not do any processing of inline
7588 \begin_inset LatexCommand \index{inline}
7592 assembler code, so the compiler might incorrectly assign local variables
7593 and parameters of a function into the overlay segment if the inline assembler
7594 code calls other c-functions that might use the overlay.
7595 In that case the #pragma\SpecialChar ~
7596 NOOVERLAY should be used.
7599 Parameters and Local variables of functions that contain 16 or 32 bit multiplica
7601 \begin_inset LatexCommand \index{Multiplication}
7606 \begin_inset LatexCommand \index{Division}
7610 will NOT be overlayed since these are implemented using external functions,
7619 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7625 void set_error(unsigned char errcd)
7641 void some_isr () interrupt
7642 \begin_inset LatexCommand \index{interrupt}
7672 In the above example the parameter
7680 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
7682 not present, this could cause unpredictable runtime behavior when called
7684 The #pragma\SpecialChar ~
7685 NOOVERLAY ensures that the parameters and local variables for
7686 the function are NOT overlayed.
7689 Interrupt Service Routines
7692 SDCC allows interrupt service routines to be coded in C, with some extended
7698 void timer_isr (void) interrupt 1 using 1
7712 The optional number following the
7715 \begin_inset LatexCommand \index{interrupt}
7721 keyword is the interrupt number this routine will service.
7722 When present, the compiler will insert a call to this routine in the interrupt
7723 vector table for the interrupt number specified.
7728 keyword can be used to tell the compiler to use the specified register
7729 bank (8051 specific) when generating code for this function.
7730 Note that when some function is called from an interrupt service routine
7731 it should be preceded by a #pragma\SpecialChar ~
7733 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
7737 if it is not reentrant.
7738 Furthermore nonreentrant functions should not be called from the main program
7739 while the interrupt service routine might be active.
7740 If the interrupt service routines changes variables which are accessed
7741 by other functions these variables should be declared
7746 \begin_inset LatexCommand \index{volatile}
7754 A special note here, int (16 bit) and long (32 bit) integer division
7755 \begin_inset LatexCommand \index{Division}
7760 \begin_inset LatexCommand \index{Multiplication}
7765 \begin_inset LatexCommand \index{Modulus}
7769 operations are implemented using external support routines developed in
7770 ANSI-C, if an interrupt service routine needs to do any of these operations
7771 then the support routines (as mentioned in a following section) will have
7772 to be recompiled using the
7785 \begin_inset LatexCommand \index{-\/-stack-auto}
7791 option and the source file will need to be compiled using the
7806 \begin_inset LatexCommand \index{-\/-int-long-rent}
7813 If you have multiple source files in your project, interrupt service routines
7814 can be present in any of them, but a prototype of the isr MUST be present
7815 or included in the file that contains the function
7822 Interrupt numbers and the corresponding address & descriptions for the Standard
7823 8051/8052 are listed below.
7824 SDCC will automatically adjust the interrupt vector table to the maximum
7825 interrupt number specified.
7831 \begin_inset Tabular
7832 <lyxtabular version="3" rows="7" columns="3">
7834 <column alignment="center" valignment="top" leftline="true" width="0in">
7835 <column alignment="center" valignment="top" leftline="true" width="0in">
7836 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
7837 <row topline="true" bottomline="true">
7838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7863 <row topline="true">
7864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7889 <row topline="true">
7890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7915 <row topline="true">
7916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7941 <row topline="true">
7942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7967 <row topline="true">
7968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
7984 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
7993 <row topline="true" bottomline="true">
7994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
8010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
8028 If the interrupt service routine is defined without
8031 \begin_inset LatexCommand \index{using}
8037 a register bank or with register bank 0 (using 0), the compiler will save
8038 the registers used by itself on the stack upon entry and restore them at
8039 exit, however if such an interrupt service routine calls another function
8040 then the entire register bank will be saved on the stack.
8041 This scheme may be advantageous for small interrupt service routines which
8042 have low register usage.
8045 If the interrupt service routine is defined to be using a specific register
8050 are save and restored, if such an interrupt service routine calls another
8051 function (using another register bank) then the entire register bank of
8052 the called function will be saved on the stack.
8053 This scheme is recommended for larger interrupt service routines.
8056 Calling other functions from an interrupt service routine is not recommended,
8057 avoid it if possible.
8059 For some pitfalls see section
8060 \begin_inset LatexCommand \ref{sub:Overlaying}
8064 about Overlaying and section
8065 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
8069 about Functions using private banks.
8079 <TODO: this isn't implemented at all!>
8085 A special keyword may be associated with a function declaring it as
8090 SDCC will generate code to disable all interrupts upon entry to a critical
8091 function and enable them back before returning.
8092 Note that nesting critical functions may cause unpredictable results.
8118 The critical attribute maybe used with other attributes like
8124 \begin_inset LatexCommand \label{sub:Naked-Functions}
8129 \begin_inset LatexCommand \index{Naked functions}
8136 A special keyword may be associated with a function declaring it as
8139 \begin_inset LatexCommand \index{\_naked}
8150 function modifier attribute prevents the compiler from generating prologue
8151 \begin_inset LatexCommand \index{function prologue}
8156 \begin_inset LatexCommand \index{function epilogue}
8160 code for that function.
8161 This means that the user is entirely responsible for such things as saving
8162 any registers that may need to be preserved, selecting the proper register
8163 bank, generating the
8167 instruction at the end, etc.
8168 Practically, this means that the contents of the function must be written
8169 in inline assembler.
8170 This is particularly useful for interrupt functions, which can have a large
8171 (and often unnecessary) prologue/epilogue.
8172 For example, compare the code generated by these two functions:
8178 \begin_inset LatexCommand \index{volatile}
8182 data unsigned char counter;
8186 void simpleInterrupt(void) interrupt
8187 \begin_inset LatexCommand \index{interrupt}
8205 void nakedInterrupt(void) interrupt 2 _naked
8214 \begin_inset LatexCommand \index{\_asm}
8243 ; MUST explicitly include ret or reti in _naked function.
8250 \begin_inset LatexCommand \index{\_endasm}
8259 For an 8051 target, the generated simpleInterrupt looks like:
8400 whereas nakedInterrupt looks like:
8424 ; MUST explicitly include ret or reti in _naked function.
8427 The #pragma directive EXCLUDE
8428 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
8432 also allows to reduce pushing & popping the registers.
8435 While there is nothing preventing you from writing C code inside a _naked
8436 function, there are many ways to shoot yourself in the foot doing this,
8437 and it is recommended that you stick to inline assembler.
8440 Functions using private banks
8441 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
8446 \begin_inset LatexCommand \index{bank}
8456 \begin_inset LatexCommand \index{using}
8462 attribute (which tells the compiler to use a register bank other than the
8463 default bank zero) should only be applied to
8466 \begin_inset LatexCommand \index{interrupt}
8472 functions (see note 1 below).
8473 This will in most circumstances make the generated ISR code more efficient
8474 since it will not have to save registers on the stack.
8481 attribute will have no effect on the generated code for a
8485 function (but may occasionally be useful anyway
8491 possible exception: if a function is called ONLY from 'interrupt' functions
8492 using a particular bank, it can be declared with the same 'using' attribute
8493 as the calling 'interrupt' functions.
8494 For instance, if you have several ISRs using bank one, and all of them
8495 call memcpy(), it might make sense to create a specialized version of memcpy()
8496 'using 1', since this would prevent the ISR from having to save bank zero
8497 to the stack on entry and switch to bank zero before calling the function
8504 (pending: I don't think this has been done yet)
8511 function using a non-zero bank will assume that it can trash that register
8512 bank, and will not save it.
8513 Since high-priority interrupts
8514 \begin_inset LatexCommand \index{interrupt priority}
8518 can interrupt low-priority ones on the 8051 and friends, this means that
8519 if a high-priority ISR
8523 a particular bank occurs while processing a low-priority ISR
8527 the same bank, terrible and bad things can happen.
8528 To prevent this, no single register bank should be
8532 by both a high priority and a low priority ISR.
8533 This is probably most easily done by having all high priority ISRs use
8534 one bank and all low priority ISRs use another.
8535 If you have an ISR which can change priority at runtime, you're on your
8536 own: I suggest using the default bank zero and taking the small performance
8540 It is most efficient if your ISR calls no other functions.
8541 If your ISR must call other functions, it is most efficient if those functions
8542 use the same bank as the ISR (see note 1 below); the next best is if the
8543 called functions use bank zero.
8544 It is very inefficient to call a function using a different, non-zero bank
8550 \begin_inset LatexCommand \index{Absolute addressing}
8557 Data items can be assigned an absolute address with the
8560 \begin_inset LatexCommand \index{at}
8566 keyword, in addition to a storage class, e.g.:
8572 \begin_inset LatexCommand \index{xdata}
8577 \begin_inset LatexCommand \index{at}
8581 0x7ffe unsigned int chksum;
8584 In the above example the variable chksum will located at 0x7ffe and 0x7fff
8585 of the external ram.
8586 The compiler does not actually reserve any space for variables declared
8587 in this way (they are implemented with an equate in the assembler).
8588 Thus it is left to the programmer to make sure there are no overlaps with
8589 other variables that are declared without the absolute address.
8590 The assembler listing file (.lst
8591 \begin_inset LatexCommand \index{.lst}
8595 ) and the linker output files (.rst
8596 \begin_inset LatexCommand \index{.rst}
8601 \begin_inset LatexCommand \index{.map}
8605 ) are good places to look for such overlaps.
8608 In case of memory mapped I/O devices the keyword
8612 should be used to tell the compiler that accesses might not be optimized
8619 \begin_inset LatexCommand \index{volatile}
8624 \begin_inset LatexCommand \index{xdata}
8629 \begin_inset LatexCommand \index{at}
8633 0x8000 unsigned char PORTA_8255;
8636 Absolute address can be specified for variables in all storage classes,
8643 \begin_inset LatexCommand \index{bit}
8648 \begin_inset LatexCommand \index{at}
8655 The above example will allocate the variable at offset 0x02 in the bit-addressab
8657 There is no real advantage to assigning absolute addresses to variables
8658 in this manner, unless you want strict control over all the variables allocated.
8659 One possible use would be to write hardware portable code.
8660 For example, if you have a routine that uses one or more of the microcontroller
8661 I/O pins, and such pins are different for two different hardwares, you
8662 can declare the I/O pins in your routine using:
8675 void DS1306_put(unsigned char value)
8683 unsigned char mask=0x80;
8707 SDI=(value & mask)?1:0;
8748 Then, someplace in the code for the first hardware you would use
8753 bit at 0x80 SDI;\SpecialChar ~
8757 /* I/O port 0, bit 0 */
8759 bit at 0x81 SCLK;\SpecialChar ~
8762 /* I/O port 0, bit 1 */
8764 bit CPOL;\SpecialChar ~
8775 /* This is a variable, let the linker allocate this one */
8778 Similarly, for the second hardware you would use
8783 bit at 0x83 SDI;\SpecialChar ~
8787 /* I/O port 0, bit 3 */
8789 bit at 0x91 SCLK;\SpecialChar ~
8792 /* I/O port 1, bit 1 */
8794 bit CPOL;\SpecialChar ~
8805 /* This is a variable, let the linker allocate this one */
8808 and you can use the same hardware dependent routine without changes, as
8809 for example in a library.
8810 This is somehow similar to sbit, but only one absolute address has to be
8811 specified in the whole project.
8815 \begin_inset LatexCommand \index{Startup code}
8822 The compiler inserts a call to the C routine
8824 _sdcc_external_startup()
8825 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
8834 at the start of the CODE area.
8835 This routine is in the runtime library
8836 \begin_inset LatexCommand \index{Runtime library}
8841 By default this routine returns 0, if this routine returns a non-zero value,
8842 the static & global variable initialization will be skipped and the function
8843 main will be invoked.
8844 Otherwise static & global variables will be initialized before the function
8848 _sdcc_external_startup()
8850 routine to your program to override the default if you need to setup hardware
8851 or perform some other critical operation prior to static & global variable
8853 See also the compiler option
8872 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
8879 Inline Assembler Code
8880 \begin_inset LatexCommand \index{Assembler routines}
8887 SDCC allows the use of in-line assembler with a few restriction as regards
8889 All labels defined within inline assembler code
8897 where nnnn is a number less than 100 (which implies a limit of utmost 100
8898 inline assembler labels
8906 It is strongly recommended that each assembly instruction (including labels)
8907 be placed in a separate line (as the example shows).
8921 \begin_inset LatexCommand \index{-\/-peep-asm}
8927 command line option is used, the inline assembler code will be passed through
8928 the peephole optimizer
8929 \begin_inset LatexCommand \index{Peephole optimizer}
8934 This might cause some unexpected changes in the inline assembler code.
8935 Please go through the peephole optimizer rules defined in file
8939 carefully before using this option.
8945 \begin_inset LatexCommand \index{\_asm}
8975 \begin_inset LatexCommand \index{\_endasm}
8982 The inline assembler code can contain any valid code understood by the assembler
8983 , this includes any assembler directives and comment lines.
8984 The compiler does not do any validation of the code within the
8994 Inline assembler code cannot reference any C-Labels, however it can reference
8996 \begin_inset LatexCommand \index{Labels}
9000 defined by the inline assembler, e.g.:
9025 ; some assembler code
9045 /* some more c code */
9047 clabel:\SpecialChar ~
9049 /* inline assembler cannot reference this label */
9061 $0003: ;label (can be reference by inline assembler only)
9073 /* some more c code */
9078 In other words inline assembly code can access labels defined in inline
9079 assembly within the scope of the function.
9080 The same goes the other way, ie.
9081 labels defines in inline assembly CANNOT be accessed by C statements.
9084 An example acessing a C variable is in section
9085 \begin_inset LatexCommand \ref{sub:Naked-Functions}
9093 \begin_inset LatexCommand \index{int (16 bit)}
9098 \begin_inset LatexCommand \index{long (32 bit)}
9105 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
9106 multiplication and modulus operations are implemented by support routines.
9107 These support routines are all developed in ANSI-C to facilitate porting
9108 to other MCUs, although some model specific assembler optimizations are
9110 The following files contain the described routines, all of them can be
9111 found in <installdir>/share/sdcc/lib.
9117 \begin_inset Tabular
9118 <lyxtabular version="3" rows="11" columns="2">
9120 <column alignment="center" valignment="top" leftline="true" width="0">
9121 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
9122 <row topline="true" bottomline="true">
9123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9144 <row topline="true">
9145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9158 16 bit multiplication
9162 <row topline="true">
9163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9176 signed 16 bit division (calls _divuint)
9180 <row topline="true">
9181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9194 unsigned 16 bit division
9198 <row topline="true">
9199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9212 signed 16 bit modulus (calls _moduint)
9216 <row topline="true">
9217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9225 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9230 unsigned 16 bit modulus
9234 <row topline="true">
9235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9248 32 bit multiplication
9252 <row topline="true">
9253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9261 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9266 signed 32 division (calls _divulong)
9270 <row topline="true">
9271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9279 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9284 unsigned 32 division
9288 <row topline="true">
9289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9302 signed 32 bit modulus (calls _modulong)
9306 <row topline="true" bottomline="true">
9307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9315 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9320 unsigned 32 bit modulus
9333 Since they are compiled as
9338 \begin_inset LatexCommand \index{reentrant}
9343 \begin_inset LatexCommand \index{interrupt}
9347 service routines should not do any of the above operations.
9348 If this is unavoidable then the above routines will need to be compiled
9362 \begin_inset LatexCommand \index{-\/-stack-auto}
9368 option, after which the source program will have to be compiled with
9381 \begin_inset LatexCommand \index{-\/-int-long-rent}
9388 Notice that you don't have to call this routines directly.
9389 The compiler will use them automatically every time a integer operation
9393 Floating Point Support
9394 \begin_inset LatexCommand \index{Floating point support}
9401 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
9402 floating point support routines are derived from gcc's floatlib.c and consists
9403 of the following routines:
9411 \begin_inset Tabular
9412 <lyxtabular version="3" rows="17" columns="2">
9414 <column alignment="center" valignment="top" leftline="true" width="0">
9415 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
9416 <row topline="true" bottomline="true">
9417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9443 <row topline="true">
9444 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9461 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9475 add floating point numbers
9479 <row topline="true">
9480 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9497 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9511 subtract floating point numbers
9515 <row topline="true">
9516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9547 divide floating point numbers
9551 <row topline="true">
9552 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9583 multiply floating point numbers
9587 <row topline="true">
9588 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9605 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9619 convert floating point to unsigned char
9623 <row topline="true">
9624 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9641 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9655 convert floating point to signed char
9659 <row topline="true">
9660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9677 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9691 convert floating point to unsigned int
9695 <row topline="true">
9696 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9713 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9727 convert floating point to signed int
9731 <row topline="true">
9732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9772 convert floating point to unsigned long
9776 <row topline="true">
9777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9794 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9808 convert floating point to signed long
9812 <row topline="true">
9813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9844 convert unsigned char to floating point
9848 <row topline="true">
9849 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9880 convert char to floating point number
9884 <row topline="true">
9885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9902 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9916 convert unsigned int to floating point
9920 <row topline="true">
9921 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9952 convert int to floating point numbers
9956 <row topline="true">
9957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9974 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9988 convert unsigned long to floating point number
9992 <row topline="true" bottomline="true">
9993 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10024 convert long to floating point number
10037 Note if all these routines are used simultaneously the data space might
10039 For serious floating point usage it is strongly recommended that the large
10041 Also notice that you don't have to call this routines directly.
10042 The compiler will use them automatically every time a floating point operation
10046 MCS51 Memory Models
10047 \begin_inset LatexCommand \index{Memory model}
10052 \begin_inset LatexCommand \index{MCS51 memory}
10059 SDCC allows two memory models for MCS51 code,
10068 Modules compiled with different memory models should
10072 be combined together or the results would be unpredictable.
10073 The library routines supplied with the compiler are compiled as both small
10075 The compiled library modules are contained in separate directories as small
10076 and large so that you can link to either set.
10080 When the large model is used all variables declared without a storage class
10081 will be allocated into the external ram, this includes all parameters and
10082 local variables (for non-reentrant
10083 \begin_inset LatexCommand \index{reentrant}
10088 When the small model is used variables without storage class are allocated
10089 in the internal ram.
10092 Judicious usage of the processor specific storage classes
10093 \begin_inset LatexCommand \index{Storage class}
10097 and the 'reentrant' function type will yield much more efficient code,
10098 than using the large model.
10099 Several optimizations are disabled when the program is compiled using the
10100 large model, it is therefore strongly recommended that the small model
10101 be used unless absolutely required.
10104 DS390 Memory Models
10105 \begin_inset LatexCommand \index{Memory model}
10110 \begin_inset LatexCommand \index{DS390 memory model}
10117 The only model supported is Flat 24
10118 \begin_inset LatexCommand \index{Flat 24 (memory model)}
10123 This generates code for the 24 bit contiguous addressing mode of the Dallas
10125 In this mode, up to four meg of external RAM or code space can be directly
10127 See the data sheets at www.dalsemi.com for further information on this part.
10131 Note that the compiler does not generate any code to place the processor
10132 into 24 bitmode (although
10136 in the ds390 libraries will do that for you).
10142 \begin_inset LatexCommand \index{Tinibios (DS390)}
10146 , the boot loader or similar code must ensure that the processor is in 24
10147 bit contiguous addressing mode before calling the SDCC startup code.
10165 option, variables will by default be placed into the XDATA segment.
10170 Segments may be placed anywhere in the 4 meg address space using the usual
10182 Note that if any segments are located above 64K, the -r flag must be passed
10183 to the linker to generate the proper segment relocations, and the Intel
10184 HEX output format must be used.
10185 The -r flag can be passed to the linker by using the option
10189 on the sdcc command line.
10190 However, currently the linker can not handle code segments > 64k.
10193 Defines Created by the Compiler
10194 \begin_inset LatexCommand \index{Defines created by the compiler}
10201 The compiler creates the following #defines
10202 \begin_inset LatexCommand \index{\#defines}
10212 \begin_inset Tabular
10213 <lyxtabular version="3" rows="10" columns="2">
10215 <column alignment="center" valignment="top" leftline="true" width="0">
10216 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
10217 <row topline="true" bottomline="true">
10218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10239 <row topline="true">
10240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10246 \begin_inset LatexCommand \index{SDCC}
10253 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10258 this Symbol is always defined
10262 <row topline="true">
10263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10269 \begin_inset LatexCommand \index{SDCC\_mcs51}
10274 \begin_inset LatexCommand \index{SDCC\_ds390}
10279 \begin_inset LatexCommand \index{SDCC\_z80}
10286 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10291 depending on the model used (e.g.: -mds390
10295 <row topline="true">
10296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10302 \begin_inset LatexCommand \index{\_\_mcs51}
10307 \begin_inset LatexCommand \index{\_\_ds390}
10312 \begin_inset LatexCommand \index{\_\_z80}
10319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10324 depending on the model used (e.g.
10329 <row topline="true">
10330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10336 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
10343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10366 <row topline="true">
10367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10373 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
10380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10403 <row topline="true">
10404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10410 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
10417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10440 <row topline="true">
10441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10447 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
10454 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10477 <row topline="true">
10478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10484 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
10491 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10504 <row topline="true" bottomline="true">
10505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10511 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
10518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10540 Redirecting output on Windows Shells
10543 By default SDCC writes it's error messages to
10544 \begin_inset Quotes sld
10548 \begin_inset Quotes srd
10552 To force all messages to
10553 \begin_inset Quotes sld
10557 \begin_inset Quotes srd
10581 \begin_inset LatexCommand \index{-\/-use-stdout}
10586 Aditionaly, if you happen to have visual studio installed in your windows
10587 machine, you can use it to compile your sources using a custom build and
10603 \begin_inset LatexCommand \index{-\/-vc}
10608 Something like this should work:
10652 -model-large -c $(InputPath)
10655 SDCC Technical Data
10659 \begin_inset LatexCommand \index{Optimizations}
10666 SDCC performs a host of standard optimizations in addition to some MCU specific
10669 \layout Subsubsection
10671 Sub-expression Elimination
10672 \begin_inset LatexCommand \index{Subexpression elimination}
10679 The compiler does local and global common subexpression elimination, e.g.:
10690 will be translated to
10702 Some subexpressions are not as obvious as the above example, e.g.:
10712 In this case the address arithmetic a->b[i] will be computed only once;
10713 the equivalent code in C would be.
10725 The compiler will try to keep these temporary variables in registers.
10726 \layout Subsubsection
10728 Dead-Code Elimination
10729 \begin_inset LatexCommand \index{Dead-code elimination}
10750 i = 1; \SpecialChar ~
10759 global = 1;\SpecialChar ~
10772 global = 3;\SpecialChar ~
10798 \layout Subsubsection
10801 \begin_inset LatexCommand \index{Copy propagation}
10857 Note: the dead stores created by this copy propagation will be eliminated
10858 by dead-code elimination.
10859 \layout Subsubsection
10862 \begin_inset LatexCommand \index{Loop optimization}
10869 Two types of loop optimizations are done by SDCC loop invariant lifting
10870 and strength reduction of loop induction variables.
10871 In addition to the strength reduction the optimizer marks the induction
10872 variables and the register allocator tries to keep the induction variables
10873 in registers for the duration of the loop.
10874 Because of this preference of the register allocator
10875 \begin_inset LatexCommand \index{Register allocation}
10879 , loop induction optimization causes an increase in register pressure, which
10880 may cause unwanted spilling of other temporary variables into the stack
10881 \begin_inset LatexCommand \index{stack}
10886 The compiler will generate a warning message when it is forced to allocate
10887 extra space either on the stack or data space.
10888 If this extra space allocation is undesirable then induction optimization
10889 can be eliminated either for the entire source file (with -
10899 -noinduction option) or for a given function only using #pragma\SpecialChar ~
10901 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
10914 for (i = 0 ; i < 100 ; i ++)
10930 for (i = 0; i < 100; i++)
10939 As mentioned previously some loop invariants are not as apparent, all static
10940 address computations are also moved out of the loop.
10945 \begin_inset LatexCommand \index{Strength reduction}
10949 , this optimization substitutes an expression by a cheaper expression:
10954 for (i=0;i < 100; i++)
10972 for (i=0;i< 100;i++) {
10978 ar[itemp1] = itemp2;
10995 The more expensive multiplication
10996 \begin_inset LatexCommand \index{Multiplication}
11000 is changed to a less expensive addition.
11001 \layout Subsubsection
11004 \begin_inset LatexCommand \index{Loop reversing}
11011 This optimization is done to reduce the overhead of checking loop boundaries
11012 for every iteration.
11013 Some simple loops can be reversed and implemented using a
11014 \begin_inset Quotes eld
11017 decrement and jump if not zero
11018 \begin_inset Quotes erd
11022 SDCC checks for the following criterion to determine if a loop is reversible
11023 (note: more sophisticated compilers use data-dependency analysis to make
11024 this determination, SDCC uses a more simple minded analysis).
11027 The 'for' loop is of the form
11033 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
11043 The <for body> does not contain
11044 \begin_inset Quotes eld
11048 \begin_inset Quotes erd
11052 \begin_inset Quotes erd
11058 All goto's are contained within the loop.
11061 No function calls within the loop.
11064 The loop control variable <sym> is not assigned any value within the loop
11067 The loop control variable does NOT participate in any arithmetic operation
11071 There are NO switch statements in the loop.
11072 \layout Subsubsection
11074 Algebraic Simplifications
11077 SDCC does numerous algebraic simplifications, the following is a small sub-set
11078 of these optimizations.
11083 i = j + 0 ; /* changed to */ i = j;
11085 i /= 2;\SpecialChar ~
11089 /* changed to */ i >>= 1;
11091 i = j - j ; /* changed to */ i = 0;
11093 i = j / 1 ; /* changed to */ i = j;
11096 Note the subexpressions
11097 \begin_inset LatexCommand \index{Subexpression}
11101 given above are generally introduced by macro expansions or as a result
11102 of copy/constant propagation.
11103 \layout Subsubsection
11105 'switch' Statements
11106 \begin_inset LatexCommand \index{switch statement}
11113 SDCC changes switch statements to jump tables
11114 \begin_inset LatexCommand \index{jump tables}
11118 when the following conditions are true.
11122 The case labels are in numerical sequence, the labels need not be in order,
11123 and the starting number need not be one or zero.
11129 switch(i) {\SpecialChar ~
11160 case 4: ...\SpecialChar ~
11192 case 5: ...\SpecialChar ~
11224 case 3: ...\SpecialChar ~
11256 case 6: ...\SpecialChar ~
11324 Both the above switch statements will be implemented using a jump-table.
11325 The example to the right side is slightly more efficient as the check for
11326 the lower bound of the jump-table is not needed.
11330 The number of case labels is at least three, since it takes two conditional
11331 statements to handle the boundary conditions.
11334 The number of case labels is less than 84, since each label takes 3 bytes
11335 and a jump-table can be utmost 256 bytes long.
11338 Switch statements which have gaps in the numeric sequence or those that
11339 have more that 84 case labels can be split into more than one switch statement
11340 for efficient code generation, e.g.:
11390 If the above switch statement is broken down into two switch statements
11429 case 9: \SpecialChar ~
11445 case 12:\SpecialChar ~
11452 then both the switch statements will be implemented using jump-tables whereas
11453 the unmodified switch statement will not be.
11454 You might also consider dummy cases 0 and 5 to 8 in this example.
11455 The pragma NOJTBOUND
11456 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
11460 can be used to turn off checking the
11473 \layout Subsubsection
11475 Bit-shifting Operations
11476 \begin_inset LatexCommand \index{Bit shifting}
11483 Bit shifting is one of the most frequently used operation in embedded programmin
11485 SDCC tries to implement bit-shift operations in the most efficient way
11501 generates the following code:
11515 In general SDCC will never setup a loop if the shift count is known.
11557 Note that SDCC stores numbers in little-endian format (i.e.
11558 lowest order first).
11559 \layout Subsubsection
11562 \begin_inset LatexCommand \index{Bit rotation}
11569 A special case of the bit-shift operation is bit rotation, SDCC recognizes
11570 the following expression to be a left bit-rotation:
11580 i = ((i << 1) | (i >> 7));
11589 will generate the following code:
11608 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
11609 ns of this case will also be recognized as bit-rotation, i.e.:
11614 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
11615 \layout Subsubsection
11618 \begin_inset LatexCommand \index{Highest Order Bit}
11625 It is frequently required to obtain the highest order bit of an integral
11626 type (long, int, short or char types).
11627 SDCC recognizes the following expression to yield the highest order bit
11628 and generates optimized code for it, e.g.:
11650 hob = (gint >> 15) & 1;
11660 will generate the following code:
11693 000A E5*01\SpecialChar ~
11720 000C 23\SpecialChar ~
11751 000D 54 01\SpecialChar ~
11778 000F F5*02\SpecialChar ~
11806 Variations of this case however will
11811 It is a standard C expression, so I heartily recommend this be the only
11812 way to get the highest order bit, (it is portable).
11813 Of course it will be recognized even if it is embedded in other expressions,
11819 xyz = gint + ((gint >> 15) & 1);
11822 will still be recognized.
11823 \layout Subsubsection
11826 \begin_inset LatexCommand \index{Peephole optimizer}
11833 The compiler uses a rule based, pattern matching and re-writing mechanism
11834 for peep-hole optimization.
11839 a peep-hole optimizer by Christopher W.
11840 Fraser (cwfraser@microsoft.com).
11841 A default set of rules are compiled into the compiler, additional rules
11842 may be added with the
11855 \begin_inset LatexCommand \index{-\/-peep-file}
11862 The rule language is best illustrated with examples.
11886 The above rule will change the following assembly
11887 \begin_inset LatexCommand \index{Assembler routines}
11909 Note: All occurrences of a
11913 (pattern variable) must denote the same string.
11914 With the above rule, the assembly sequence:
11924 will remain unmodified.
11928 Other special case optimizations may be added by the user (via
11944 some variants of the 8051 MCU allow only
11953 The following two rules will change all
11972 replace { lcall %1 } by { acall %1 }
11974 replace { ljmp %1 } by { ajmp %1 }
11979 inline-assembler code
11981 is also passed through the peep hole optimizer, thus the peephole optimizer
11982 can also be used as an assembly level macro expander.
11983 The rules themselves are MCU dependent whereas the rule language infra-structur
11984 e is MCU independent.
11985 Peephole optimization rules for other MCU can be easily programmed using
11990 The syntax for a rule is as follows:
11995 rule := replace [ restart ] '{' <assembly sequence> '
12033 <assembly sequence> '
12051 '}' [if <functionName> ] '
12056 <assembly sequence> := assembly instruction (each instruction including
12057 labels must be on a separate line).
12061 The optimizer will apply to the rules one by one from the top in the sequence
12062 of their appearance, it will terminate when all rules are exhausted.
12063 If the 'restart' option is specified, then the optimizer will start matching
12064 the rules again from the top, this option for a rule is expensive (performance)
12065 , it is intended to be used in situations where a transformation will trigger
12066 the same rule again.
12067 An example of this (not a good one, it has side effects) is the following
12090 Note that the replace pattern cannot be a blank, but can be a comment line.
12091 Without the 'restart' option only the inner most 'pop' 'push' pair would
12092 be eliminated, i.e.:
12122 the restart option the rule will be applied again to the resulting code
12123 and then all the pop-push pairs will be eliminated to yield:
12133 A conditional function can be attached to a rule.
12134 Attaching rules are somewhat more involved, let me illustrate this with
12161 The optimizer does a look-up of a function name table defined in function
12166 in the source file SDCCpeeph.c, with the name
12171 If it finds a corresponding entry the function is called.
12172 Note there can be no parameters specified for these functions, in this
12177 is crucial, since the function
12181 expects to find the label in that particular variable (the hash table containin
12182 g the variable bindings is passed as a parameter).
12183 If you want to code more such functions, take a close look at the function
12184 labelInRange and the calling mechanism in source file SDCCpeeph.c.
12185 Currently implemented are
12187 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
12188 24bitMode, portIsDS390, 24bitModeAndPortDS390
12197 I know this whole thing is a little kludgey, but maybe some day we will
12198 have some better means.
12199 If you are looking at this file, you will see the default rules that are
12200 compiled into the compiler, you can add your own rules in the default set
12201 there if you get tired of specifying the -
12215 \begin_inset LatexCommand \index{Pragmas}
12222 SDCC supports the following #pragma directives.
12226 \begin_inset LatexCommand \index{\#pragma SAVE}
12230 - this will save all current options to the SAVE/RESTORE stack.
12235 \begin_inset LatexCommand \index{\#pragma RESTORE}
12239 - will restore saved options from the last save.
12240 SAVEs & RESTOREs can be nested.
12241 SDCC uses a SAVE/RESTORE stack: SAVE pushes current options to the stack,
12242 RESTORE pulls current options from the stack.
12247 \begin_inset LatexCommand \index{\#pragma NOGCSE}
12251 - will stop global common subexpression elimination.
12255 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
12259 - will stop loop induction optimizations.
12263 \begin_inset LatexCommand \index{\#pragma NOJTBOUND}
12267 - will not generate code for boundary value checking, when switch statements
12268 are turned into jump-tables (dangerous).
12273 \begin_inset LatexCommand \index{\#pragma NOOVERLAY}
12277 - the compiler will not overlay the parameters and local variables of a
12282 \begin_inset LatexCommand \index{\#pragma LESS\_PEDANTIC}
12286 - the compiler will not warn you anymore for obvious mistakes, you'r on
12291 \begin_inset LatexCommand \index{\#pragma NOLOOPREVERSE}
12295 - Will not do loop reversal optimization
12299 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
12303 NONE | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
12305 \begin_inset LatexCommand \index{push/pop}
12309 instruction in ISR function (using interrupt
12310 \begin_inset LatexCommand \index{interrupt}
12315 The directive should be placed immediately before the ISR function definition
12316 and it affects ALL ISR functions following it.
12317 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
12318 EXCLUDE\SpecialChar ~
12320 \begin_inset LatexCommand \index{\#pragma EXCLUDE}
12328 \begin_inset LatexCommand \index{\#pragma NOIV}
12332 - Do not generate interrupt vector table entries for all ISR functions
12333 defined after the pragma.
12334 This is useful in cases where the interrupt vector table must be defined
12335 manually, or when there is a secondary, manually defined interrupt vector
12337 for the autovector feature of the Cypress EZ-USB FX2).
12341 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
12346 \begin_inset LatexCommand \index{function prologue}
12350 function1[,function2[,function3...]] - The compiler by default uses a caller
12351 saves convention for register saving across function calls, however this
12352 can cause unnecessary register pushing & popping when calling small functions
12353 from larger functions.
12354 This option can be used to switch off the register saving convention for
12355 the function names specified.
12356 The compiler will not save registers when calling these functions, extra
12357 code need to be manually inserted at the entry & exit for these functions
12358 to save & restore the registers used by these functions, this can SUBSTANTIALLY
12359 reduce code & improve run time performance of the generated code.
12360 In the future the compiler (with inter procedural analysis) may be able
12361 to determine the appropriate scheme to use for each function call.
12372 -callee-saves command line option is used, the function names specified
12373 in #pragma\SpecialChar ~
12375 \begin_inset LatexCommand \index{\#pragma CALLEE-SAVES}
12379 is appended to the list of functions specified in the command line.
12382 The pragma's are intended to be used to turn-off certain optimizations which
12383 might cause the compiler to generate extra stack / data space to store
12384 compiler generated temporary variables.
12385 This usually happens in large functions.
12386 Pragma directives should be used as shown in the following example, they
12387 are used to control options & optimizations for a given function; pragmas
12388 should be placed before and/or after a function, placing pragma's inside
12389 a function body could have unpredictable results.
12395 \begin_inset LatexCommand \index{\#pragma SAVE}
12406 /* save the current settings */
12409 \begin_inset LatexCommand \index{\#pragma NOGCSE}
12418 /* turnoff global subexpression elimination */
12420 #pragma NOINDUCTION
12421 \begin_inset LatexCommand \index{\#pragma NOINDUCTION}
12425 /* turn off induction optimizations */
12448 \begin_inset LatexCommand \index{\#pragma RESTORE}
12452 /* turn the optimizations back on */
12455 The compiler will generate a warning message when extra space is allocated.
12456 It is strongly recommended that the SAVE and RESTORE pragma's be used when
12457 changing options for a function.
12467 <pending: this is messy and incomplete>
12472 Compiler support routines (_gptrget, _mulint etc)
12475 Stdclib functions (puts, printf, strcat etc)
12478 Math functions (sin, pow, sqrt etc)
12481 Libraries included in SDCC should have a license at least as liberal as
12482 the GNU Lesser General Public License
12483 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
12494 license statements for the libraries are missing.
12495 sdcc/device/lib/ser_ir.c f.e.
12496 comes with a GPL (as opposed to LGPL) License - this will not be liberal
12497 enough for many embedded programmers.
12500 Interfacing with Assembly Routines
12501 \begin_inset LatexCommand \index{Assembler routines}
12506 \layout Subsubsection
12508 Global Registers used for Parameter Passing
12509 \begin_inset LatexCommand \index{Parameter passing}
12516 The compiler always uses the global registers
12519 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12524 \begin_inset LatexCommand \index{B (register)}
12533 \begin_inset LatexCommand \index{ACC}
12539 to pass the first parameter to a routine.
12540 The second parameter onwards is either allocated on the stack (for reentrant
12551 -stack-auto is used) or in data / xdata memory (depending on the memory
12554 \layout Subsubsection
12556 Assembler Routine(non-reentrant
12557 \begin_inset LatexCommand \index{reentrant}
12562 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12569 In the following example the function c_func calls an assembler routine
12570 asm_func, which takes two parameters.
12575 extern int asm_func(unsigned char, unsigned char);
12579 int c_func (unsigned char i, unsigned char j)
12587 return asm_func(i,j);
12601 return c_func(10,9);
12606 The corresponding assembler function is:
12611 .globl _asm_func_PARM_2
12675 add a,_asm_func_PARM_2
12700 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12717 Note here that the return values are placed in 'dpl' - One byte return value,
12718 'dpl' LSB & 'dph' MSB for two byte values.
12719 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12720 b' & 'acc' for four byte values.
12723 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12724 the parameter number starting from 1, and counting from the left.
12725 The first parameter is passed in
12726 \begin_inset Quotes eld
12730 \begin_inset Quotes erd
12733 for One bye parameter,
12734 \begin_inset Quotes eld
12738 \begin_inset Quotes erd
12742 \begin_inset Quotes eld
12746 \begin_inset Quotes erd
12749 for three bytes and
12750 \begin_inset Quotes eld
12754 \begin_inset Quotes erd
12757 for four bytes, the variable name for the second parameter will be _<function_n
12762 Assemble the assembler routine with the following command:
12769 asx8051 -losg asmfunc.asm
12776 Then compile and link the assembler routine to the C source file with the
12784 sdcc cfunc.c asmfunc.rel
12785 \layout Subsubsection
12787 Assembler Routine(reentrant
12788 \begin_inset LatexCommand \index{reentrant}
12793 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12800 In this case the second parameter onwards will be passed on the stack, the
12801 parameters are pushed from right to left i.e.
12802 after the call the left most parameter will be on the top of the stack.
12803 Here is an example:
12808 extern int asm_func(unsigned char, unsigned char);
12812 int c_func (unsigned char i, unsigned char j) reentrant
12820 return asm_func(i,j);
12834 return c_func(10,9);
12839 The corresponding assembler routine is:
12945 The compiling and linking procedure remains the same, however note the extra
12946 entry & exit linkage required for the assembler code, _bp is the stack
12947 frame pointer and is used to compute the offset into the stack for parameters
12948 and local variables.
12952 \begin_inset LatexCommand \label{sub:External-Stack}
12957 \begin_inset LatexCommand \index{stack}
12962 \begin_inset LatexCommand \index{External stack}
12969 The external stack is located at the start of the external ram segment,
12970 and is 256 bytes in size.
12981 -xstack option is used to compile the program, the parameters and local
12982 variables of all reentrant functions are allocated in this area.
12983 This option is provided for programs with large stack space requirements.
12984 When used with the -
12994 -stack-auto option, all parameters and local variables are allocated on
12995 the external stack (note support libraries will need to be recompiled with
12999 The compiler outputs the higher order address byte of the external ram segment
13000 into PORT P2, therefore when using the External Stack option, this port
13001 MAY NOT be used by the application program.
13005 \begin_inset LatexCommand \index{ANSI-compliance}
13012 Deviations from the compliance:
13015 functions are not always reentrant.
13018 structures cannot be assigned values directly, cannot be passed as function
13019 parameters or assigned to each other and cannot be a return value from
13046 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
13057 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
13079 return rets;/* is invalid in SDCC although allowed in ANSI */
13086 \begin_inset LatexCommand \index{long long (not supported)}
13091 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
13099 \begin_inset LatexCommand \index{double (not supported)}
13103 ' precision floating point
13104 \begin_inset LatexCommand \index{Floating point support}
13111 No support for setjmp and longjmp (for now).
13115 \begin_inset LatexCommand \index{K\&R style}
13119 function declarations are NOT allowed.
13125 foo(i,j) /* this old style of function declarations */
13127 int i,j; /* are valid in ANSI but not valid in SDCC */
13142 functions declared as pointers must be dereferenced during the call.
13153 /* has to be called like this */
13155 (*foo)(); /* ANSI standard allows calls to be made like 'foo()' */
13159 Cyclomatic Complexity
13160 \begin_inset LatexCommand \index{Cyclomatic complexity}
13167 Cyclomatic complexity of a function is defined as the number of independent
13168 paths the program can take during execution of the function.
13169 This is an important number since it defines the number test cases you
13170 have to generate to validate the function.
13171 The accepted industry standard for complexity number is 10, if the cyclomatic
13172 complexity reported by SDCC exceeds 10 you should think about simplification
13173 of the function logic.
13174 Note that the complexity level is not related to the number of lines of
13175 code in a function.
13176 Large functions can have low complexity, and small functions can have large
13182 SDCC uses the following formula to compute the complexity:
13187 complexity = (number of edges in control flow graph) - (number of nodes
13188 in control flow graph) + 2;
13192 Having said that the industry standard is 10, you should be aware that in
13193 some cases it be may unavoidable to have a complexity level of less than
13195 For example if you have switch statement with more than 10 case labels,
13196 each case label adds one to the complexity level.
13197 The complexity level is by no means an absolute measure of the algorithmic
13198 complexity of the function, it does however provide a good starting point
13199 for which functions you might look at for further optimization.
13205 Here are a few guidelines that will help the compiler generate more efficient
13206 code, some of the tips are specific to this compiler others are generally
13207 good programming practice.
13210 Use the smallest data type to represent your data-value.
13211 If it is known in advance that the value is going to be less than 256 then
13212 use an 'unsigned char' instead of a 'short' or 'int'.
13215 Use unsigned when it is known in advance that the value is not going to
13217 This helps especially if you are doing division or multiplication.
13220 NEVER jump into a LOOP.
13223 Declare the variables to be local whenever possible, especially loop control
13224 variables (induction).
13227 Since the compiler does not always do implicit integral promotion, the programme
13228 r should do an explicit cast when integral promotion is required.
13231 Reducing the size of division, multiplication & modulus operations can reduce
13232 code size substantially.
13233 Take the following code for example.
13239 foobar(unsigned int p1, unsigned char ch)
13247 unsigned char ch1 = p1 % ch ;
13258 For the modulus operation the variable ch will be promoted to unsigned int
13259 first then the modulus operation will be performed (this will lead to a
13260 call to support routine _moduint()), and the result will be casted to a
13262 If the code is changed to
13267 foobar(unsigned int p1, unsigned char ch)
13275 unsigned char ch1 = (unsigned char)p1 % ch ;
13286 It would substantially reduce the code generated (future versions of the
13287 compiler will be smart enough to detect such optimization opportunities).
13291 Have a look at the assembly listing to get a
13292 \begin_inset Quotes sld
13296 \begin_inset Quotes srd
13299 for the code generation.
13302 Notes on MCS51 memory
13303 \begin_inset LatexCommand \index{MCS51 memory}
13310 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
13311 RAM memory which is structured as follows
13315 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
13318 - Bytes 20-2F - 16 bytes to hold 128 bit variables and,
13320 - Bytes 30-7F - 80 bytes for general purpose use.
13325 Additionally some members of the MCS51 family may have up to 128 bytes of
13326 additional, indirectly addressable, internal RAM memory (
13331 Furthermore, some chips may have some built in external memory (
13335 ) which should not be confused with the internal, directly addressable RAM
13341 Sometimes this built in
13345 memory has to be activated before using it (you can probably find this
13346 information on the datasheet of the microcontroller your are using).
13349 Normally SDCC will only use the first bank
13350 \begin_inset LatexCommand \index{bank}
13354 of registers (register bank 0), but it is possible to specify that other
13355 banks of registers should be used in interrupt
13356 \begin_inset LatexCommand \index{interrupt}
13361 By default, the compiler will place the stack after the last byte of allocated
13362 memory for variables.
13363 For example, if the first 2 banks of registers are used, and only four
13368 variables, it will position the base of the internal stack at address 20
13370 This implies that as the stack
13371 \begin_inset LatexCommand \index{stack}
13375 grows, it will use up the remaining register banks, and the 16 bytes used
13376 by the 128 bit variables, and 80 bytes for general purpose use.
13377 If any bit variables are used, the data variables will be placed after
13378 the byte holding the last bit variable.
13379 For example, if register banks 0 and 1 are used, and there are 9 bit variables
13384 variables will be placed starting at address 0x22.
13396 \begin_inset LatexCommand \index{-\/-data-loc}
13400 to specify the start address of the
13414 -iram-size to specify the size of the total internal RAM (
13426 By default the 8051 linker will place the stack after the last byte of data
13439 \begin_inset LatexCommand \index{-\/-stack-loc}
13443 allows you to specify the start of the stack, i.e.
13444 you could start it after any data in the general purpose area.
13445 If your microcontroller has additional indirectly addressable internal
13450 ) you can place the stack on it.
13451 You may also need to use -
13462 \begin_inset LatexCommand \index{-\/-data-loc}
13466 to set the start address of the external RAM (
13481 \begin_inset LatexCommand \index{-\/-data-loc}
13485 to specify its size.
13486 Same goes for the code memory, using -
13497 \begin_inset LatexCommand \index{-\/-data-loc}
13512 \begin_inset LatexCommand \index{-\/-data-loc}
13517 If in doubt, don't specify any options and see if the resulting memory
13518 layout is appropriate, then you can adjust it.
13521 The 8051 linker generates two files with memory allocation information.
13522 The first, with extension .map shows all the variables and segments.
13523 The second with extension .mem shows the final memory layout.
13524 The linker will complaint either if memory segments overlap, there is not
13525 enough memory, or there is not enough space for stack.
13526 If you get any linking warnings and/or errors related to stack or segments
13527 allocation, take a look at either the .map or .mem files to find out what
13529 The .mem file may even suggest a solution to the problem.
13533 \begin_inset LatexCommand \index{Tools}
13537 included in the distribution
13541 \begin_inset Tabular
13542 <lyxtabular version="3" rows="12" columns="3">
13544 <column alignment="center" valignment="top" leftline="true" width="0pt">
13545 <column alignment="center" valignment="top" leftline="true" width="0pt">
13546 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
13547 <row topline="true" bottomline="true">
13548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13556 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13573 <row topline="true">
13574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13587 Simulator for various architectures
13590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13599 <row topline="true">
13600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13613 header file conversion
13616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13621 sdcc/support/scripts
13625 <row topline="true">
13626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13639 header file conversion
13642 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13647 sdcc/support/scripts
13651 <row topline="true">
13652 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13686 <row topline="true">
13687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13703 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13721 <row topline="true">
13722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13738 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13756 <row topline="true">
13757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13791 <row topline="true">
13792 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13808 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13826 <row topline="true">
13827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13843 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13861 <row topline="true">
13862 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13896 <row topline="true" bottomline="true">
13897 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13940 Related open source tools
13941 \begin_inset LatexCommand \index{Related tools}
13949 \begin_inset Tabular
13950 <lyxtabular version="3" rows="8" columns="3">
13952 <column alignment="center" valignment="top" leftline="true" width="0pt">
13953 <column alignment="block" valignment="top" leftline="true" width="30line%">
13954 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
13955 <row topline="true" bottomline="true">
13956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13981 <row topline="true">
13982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13988 \begin_inset LatexCommand \index{gpsim}
13995 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14003 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14009 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
14017 <row topline="true">
14018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14040 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
14048 <row topline="true">
14049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14055 \begin_inset LatexCommand \index{srecord}
14062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14067 Object file conversion, checksumming, ...
14070 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14076 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
14084 <row topline="true">
14085 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14091 \begin_inset LatexCommand \index{objdump}
14098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14103 Object file conversion, ...
14106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14111 Part of binutils (should be there anyway)
14115 <row topline="true">
14116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14122 \begin_inset LatexCommand \index{doxygen}
14129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14134 Source code documentation system
14137 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14143 \begin_inset LatexCommand \url{http://www.doxygen.org}
14151 <row topline="true">
14152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14158 \begin_inset LatexCommand \index{splint}
14165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14170 Statically checks c sources
14173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14179 \begin_inset LatexCommand \url{http://www.splint.org}
14187 <row topline="true" bottomline="true">
14188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14194 \begin_inset LatexCommand \index{ddd}
14201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14206 Debugger, serves nicely as GUI to sdcdb
14207 \begin_inset LatexCommand \index{sdcdb}
14214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14220 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
14237 Related documentation / recommended reading
14241 \begin_inset Tabular
14242 <lyxtabular version="3" rows="5" columns="3">
14244 <column alignment="center" valignment="top" leftline="true" width="0pt">
14245 <column alignment="block" valignment="top" leftline="true" width="30line%">
14246 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
14247 <row topline="true" bottomline="true">
14248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14273 <row topline="true">
14274 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14289 Advanced Compiler Design and Implementation
14292 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14301 <row topline="true">
14302 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14325 \begin_inset LatexCommand \index{C Reference card}
14332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14338 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
14346 <row topline="true">
14347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14352 test_suite_spec.pdf
14355 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14360 sdcc regression test
14361 \begin_inset LatexCommand \index{Regression test}
14368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14377 <row topline="true" bottomline="true">
14378 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14409 sdcc internal documentation
14412 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14430 Retargetting for other MCUs.
14433 The issues for retargetting the compiler are far too numerous to be covered
14435 What follows is a brief description of each of the seven phases of the
14436 compiler and its MCU dependency.
14439 Parsing the source and building the annotated parse tree.
14440 This phase is largely MCU independent (except for the language extensions).
14441 Syntax & semantic checks are also done in this phase, along with some initial
14442 optimizations like back patching labels and the pattern matching optimizations
14443 like bit-rotation etc.
14446 The second phase involves generating an intermediate code which can be easy
14447 manipulated during the later phases.
14448 This phase is entirely MCU independent.
14449 The intermediate code generation assumes the target machine has unlimited
14450 number of registers, and designates them with the name iTemp.
14451 The compiler can be made to dump a human readable form of the code generated
14465 This phase does the bulk of the standard optimizations and is also MCU independe
14467 This phase can be broken down into several sub-phases:
14471 Break down intermediate code (iCode) into basic blocks.
14473 Do control flow & data flow analysis on the basic blocks.
14475 Do local common subexpression elimination, then global subexpression elimination
14477 Dead code elimination
14481 If loop optimizations caused any changes then do 'global subexpression eliminati
14482 on' and 'dead code elimination' again.
14485 This phase determines the live-ranges; by live range I mean those iTemp
14486 variables defined by the compiler that still survive after all the optimization
14488 Live range analysis
14489 \begin_inset LatexCommand \index{Live range analysis}
14493 is essential for register allocation, since these computation determines
14494 which of these iTemps will be assigned to registers, and for how long.
14497 Phase five is register allocation.
14498 There are two parts to this process.
14502 The first part I call 'register packing' (for lack of a better term).
14503 In this case several MCU specific expression folding is done to reduce
14508 The second part is more MCU independent and deals with allocating registers
14509 to the remaining live ranges.
14510 A lot of MCU specific code does creep into this phase because of the limited
14511 number of index registers available in the 8051.
14514 The Code generation phase is (unhappily), entirely MCU dependent and very
14515 little (if any at all) of this code can be reused for other MCU.
14516 However the scheme for allocating a homogenized assembler operand for each
14517 iCode operand may be reused.
14520 As mentioned in the optimization section the peep-hole optimizer is rule
14521 based system, which can reprogrammed for other MCUs.
14525 \begin_inset LatexCommand \index{sdcdb}
14529 - Source Level Debugger
14530 \begin_inset LatexCommand \index{Debugger}
14537 SDCC is distributed with a source level debugger.
14538 The debugger uses a command line interface, the command repertoire of the
14539 debugger has been kept as close to gdb
14540 \begin_inset LatexCommand \index{gdb}
14544 (the GNU debugger) as possible.
14545 The configuration and build process is part of the standard compiler installati
14546 on, which also builds and installs the debugger in the target directory
14547 specified during configuration.
14548 The debugger allows you debug BOTH at the C source and at the ASM source
14550 Sdcdb is available on Unix platforms only.
14553 Compiling for Debugging
14556 The \SpecialChar \-
14558 debug option must be specified for all files for which debug information
14559 is to be generated.
14560 The complier generates a .adb file for each of these files.
14561 The linker creates the .cdb file from the .adb files and the address information.
14562 This .cdb is used by the debugger.
14565 How the Debugger Works
14578 -debug option is specified the compiler generates extra symbol information
14579 some of which are put into the the assembler source and some are put into
14581 Then the linker creates the .cdb file from the individual .adb files with
14582 the address information for the symbols.
14583 The debugger reads the symbolic information generated by the compiler &
14584 the address information generated by the linker.
14585 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
14586 execution is controlled by the debugger.
14587 When a command is issued for the debugger, it translates it into appropriate
14588 commands for the simulator.
14591 Starting the Debugger
14594 The debugger can be started using the following command line.
14595 (Assume the file you are debugging has the file name foo).
14609 The debugger will look for the following files.
14612 foo.c - the source file.
14615 foo.cdb - the debugger symbol information file.
14618 foo.ihx - the Intel hex format
14619 \begin_inset LatexCommand \index{Intel hex format}
14626 Command Line Options.
14639 -directory=<source file directory> this option can used to specify the directory
14641 The debugger will look into the directory list specified for source, cdb
14643 The items in the directory list must be separated by ':', e.g.
14644 if the source files can be in the directories /home/src1 and /home/src2,
14655 -directory option should be -
14665 -directory=/home/src1:/home/src2.
14666 Note there can be no spaces in the option.
14670 -cd <directory> - change to the <directory>.
14673 -fullname - used by GUI front ends.
14676 -cpu <cpu-type> - this argument is passed to the simulator please see the
14677 simulator docs for details.
14680 -X <Clock frequency > this options is passed to the simulator please see
14681 the simulator docs for details.
14684 -s <serial port file> passed to simulator see the simulator docs for details.
14687 -S <serial in,out> passed to simulator see the simulator docs for details.
14693 As mention earlier the command interface for the debugger has been deliberately
14694 kept as close the GNU debugger gdb, as possible.
14695 This will help the integration with existing graphical user interfaces
14696 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
14697 \layout Subsubsection
14699 break [line | file:line | function | file:function]
14702 Set breakpoint at specified line or function:
14711 sdcdb>break foo.c:100
14713 sdcdb>break funcfoo
14715 sdcdb>break foo.c:funcfoo
14716 \layout Subsubsection
14718 clear [line | file:line | function | file:function ]
14721 Clear breakpoint at specified line or function:
14730 sdcdb>clear foo.c:100
14732 sdcdb>clear funcfoo
14734 sdcdb>clear foo.c:funcfoo
14735 \layout Subsubsection
14740 Continue program being debugged, after breakpoint.
14741 \layout Subsubsection
14746 Execute till the end of the current function.
14747 \layout Subsubsection
14752 Delete breakpoint number 'n'.
14753 If used without any option clear ALL user defined break points.
14754 \layout Subsubsection
14756 info [break | stack | frame | registers ]
14759 info break - list all breakpoints
14762 info stack - show the function call stack.
14765 info frame - show information about the current execution frame.
14768 info registers - show content of all registers.
14769 \layout Subsubsection
14774 Step program until it reaches a different source line.
14775 \layout Subsubsection
14780 Step program, proceeding through subroutine calls.
14781 \layout Subsubsection
14786 Start debugged program.
14787 \layout Subsubsection
14792 Print type information of the variable.
14793 \layout Subsubsection
14798 print value of variable.
14799 \layout Subsubsection
14804 load the given file name.
14805 Note this is an alternate method of loading file for debugging.
14806 \layout Subsubsection
14811 print information about current frame.
14812 \layout Subsubsection
14817 Toggle between C source & assembly source.
14818 \layout Subsubsection
14820 ! simulator command
14823 Send the string following '!' to the simulator, the simulator response is
14825 Note the debugger does not interpret the command being sent to the simulator,
14826 so if a command like 'go' is sent the debugger can loose its execution
14827 context and may display incorrect values.
14828 \layout Subsubsection
14835 My name is Bobby Brown"
14838 Interfacing with XEmacs
14839 \begin_inset LatexCommand \index{XEmacs}
14844 \begin_inset LatexCommand \index{Emacs}
14851 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
14852 sdcdb.el and sdcdbsrc.el.
14853 These two files can be found in the $(prefix)/bin directory after the installat
14855 These files need to be loaded into XEmacs for the interface to work.
14856 This can be done at XEmacs startup time by inserting the following into
14857 your '.xemacs' file (which can be found in your HOME directory):
14863 (load-file sdcdbsrc.el)
14869 .xemacs is a lisp file so the () around the command is REQUIRED.
14870 The files can also be loaded dynamically while XEmacs is running, set the
14871 environment variable 'EMACSLOADPATH' to the installation bin directory
14872 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
14873 To start the interface enter the following command:
14887 You will prompted to enter the file name to be debugged.
14892 The command line options that are passed to the simulator directly are bound
14893 to default values in the file sdcdbsrc.el.
14894 The variables are listed below, these values maybe changed as required.
14897 sdcdbsrc-cpu-type '51
14900 sdcdbsrc-frequency '11059200
14903 sdcdbsrc-serial nil
14906 The following is a list of key mapping for the debugger interface.
14914 ;; Current Listing ::
14916 ;;key\SpecialChar ~
14931 binding\SpecialChar ~
14955 ;;---\SpecialChar ~
14970 ------\SpecialChar ~
15010 sdcdb-next-from-src\SpecialChar ~
15036 sdcdb-back-from-src\SpecialChar ~
15062 sdcdb-cont-from-src\SpecialChar ~
15072 SDCDB continue command
15088 sdcdb-step-from-src\SpecialChar ~
15114 sdcdb-whatis-c-sexp\SpecialChar ~
15124 SDCDB ptypecommand for data at
15188 sdcdbsrc-delete\SpecialChar ~
15202 SDCDB Delete all breakpoints if no arg
15250 given or delete arg (C-u arg x)
15266 sdcdbsrc-frame\SpecialChar ~
15281 SDCDB Display current frame if no arg,
15330 given or display frame arg
15395 sdcdbsrc-goto-sdcdb\SpecialChar ~
15405 Goto the SDCDB output buffer
15421 sdcdb-print-c-sexp\SpecialChar ~
15432 SDCDB print command for data at
15496 sdcdbsrc-goto-sdcdb\SpecialChar ~
15506 Goto the SDCDB output buffer
15522 sdcdbsrc-mode\SpecialChar ~
15538 Toggles Sdcdbsrc mode (turns it off)
15542 ;; C-c C-f\SpecialChar ~
15550 sdcdb-finish-from-src\SpecialChar ~
15558 SDCDB finish command
15562 ;; C-x SPC\SpecialChar ~
15570 sdcdb-break\SpecialChar ~
15588 Set break for line with point
15590 ;; ESC t\SpecialChar ~
15600 sdcdbsrc-mode\SpecialChar ~
15616 Toggle Sdcdbsrc mode
15618 ;; ESC m\SpecialChar ~
15628 sdcdbsrc-srcmode\SpecialChar ~
15652 The Z80 and gbz80 port
15655 SDCC can target both the Zilog
15656 \begin_inset LatexCommand \index{Z80}
15660 and the Nintendo Gameboy's Z80-like gbz80
15661 \begin_inset LatexCommand \index{GameBoy Z80}
15666 The port is incomplete - long support is incomplete (mul, div and mod are
15667 unimplemented), and both float and bitfield support is missing.
15668 Apart from that the code generated is correct.
15671 As always, the code is the authoritative reference - see z80/ralloc.c and
15673 The stack frame is similar to that generated by the IAR Z80 compiler.
15674 IX is used as the base pointer, HL is used as a temporary register, and
15675 BC and DE are available for holding variables.
15676 IY is currently unused.
15677 Return values are stored in HL.
15678 One bad side effect of using IX as the base pointer is that a functions
15679 stack frame is limited to 127 bytes - this will be fixed in a later version.
15683 \begin_inset LatexCommand \index{Support}
15690 SDCC has grown to be a large project.
15691 The compiler alone (without the preprocessor, assembler and linker) is
15692 about 40,000 lines of code (blank stripped).
15693 The open source nature of this project is a key to its continued growth
15695 You gain the benefit and support of many active software developers and
15697 Is SDCC perfect? No, that's why we need your help.
15698 The developers take pride in fixing reported bugs.
15699 You can help by reporting the bugs and helping other SDCC users.
15700 There are lots of ways to contribute, and we encourage you to take part
15701 in making SDCC a great software package.
15705 The SDCC project is hosted on the sdcc sourceforge site at
15706 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
15711 You'll find the complete set of mailing lists
15712 \begin_inset LatexCommand \index{Mailing list}
15716 , forums, bug reporting system, patch submission
15717 \begin_inset LatexCommand \index{Patch submission}
15722 \begin_inset LatexCommand \index{download}
15726 area and cvs code repository
15727 \begin_inset LatexCommand \index{cvs code repository}
15735 \begin_inset LatexCommand \index{Bugs}
15740 \begin_inset LatexCommand \index{Reporting bugs}
15747 The recommended way of reporting bugs is using the infrastructure of the
15749 You can follow the status of bug reports there and have an overview about
15753 Bug reports are automatically forwarded to the developer mailing list and
15754 will be fixed ASAP.
15755 When reporting a bug, it is very useful to include a small test program
15756 (the smaller the better) which reproduces the problem.
15757 If you can isolate the problem by looking at the generated assembly code,
15758 this can be very helpful.
15759 Compiling your program with the -
15770 \begin_inset LatexCommand \index{-\/-dumpall}
15774 option can sometimes be useful in locating optimization problems.
15775 When reporting a bug please maker sure you:
15778 Attach the code you are compiling with SDCC.
15782 Specify the exact command you use to run SDCC, or attach your Makefile.
15786 Specify the SDCC version (type "sdcc -v"), your platform, and operating
15791 Provide an exact copy of any error message or incorrect output.
15795 Put something meaningful in the subject of your message.
15798 Please attempt to include these 5 important parts, as applicable, in all
15799 requests for support or when reporting any problems or bugs with SDCC.
15800 Though this will make your message lengthy, it will greatly improve your
15801 chance that SDCC users and developers will be able to help you.
15802 Some SDCC developers are frustrated by bug reports without code provided
15803 that they can use to reproduce and ultimately fix the problem, so please
15804 be sure to provide sample code if you are reporting a bug!
15807 Please have a short check that you are using a recent version of SDCC and
15808 the bug is not yet known.
15809 This is the link for reporting bugs:
15810 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
15817 Requesting Features
15818 \begin_inset LatexCommand \index{Feature request}
15823 \begin_inset LatexCommand \index{Requesting features}
15830 Like bug reports feature requests are forwarded to the developer mailing
15832 This is the link for requesting features:
15833 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
15843 These links should take you directly to the
15844 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
15854 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
15855 automated messages (mid 2003)
15859 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
15863 , lists and forums are archived so if you are lucky someone already had
15868 \begin_inset LatexCommand \index{Changelog}
15875 You can follow the status of the cvs version
15876 \begin_inset LatexCommand \index{version}
15880 of SDCC by watching the file
15881 \begin_inset LatexCommand \htmlurl[ChangeLog]{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
15885 in the cvs-repository.
15889 \begin_inset LatexCommand \index{Release policy}
15896 Historically there often were long delays between official releases and
15897 the sourceforge download area tends to get not updated at all.
15898 Current excuses might refer to problems with live range analysis, but if
15899 this is fixed, the next problem rising is that another excuse will have
15901 Kidding aside, we have to get better there! On the other hand there are
15902 daily snapshots available at
15903 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
15907 , and you can always built the very last version (hopefully with many bugs
15908 fixed, and features added) from the source code available at
15909 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
15917 \begin_inset LatexCommand \index{Examples}
15924 You'll find some small examples in the directory sdcc/device/examples/
15927 Maybe we should include some links to real world applications.
15928 Preferably pointer to pointers (one for each architecture) so this stays
15933 \begin_inset LatexCommand \index{Quality control}
15940 The compiler is passed through nightly compile and build checks.
15946 \begin_inset LatexCommand \index{Regression test}
15950 check that SDCC itself compiles flawlessly on several platforms and checks
15951 the quality of the code generated by SDCC by running the code through simulator
15953 There is a separate document
15956 \begin_inset LatexCommand \index{Test suite}
15965 You'll find the test code in the directory
15967 sdcc/support/regression
15970 You can run these tests manually by running
15974 in this directory (or f.e.
15979 if you don't want to run the complete tests).
15980 The test code might also be interesting if you want to look for examples
15981 \begin_inset LatexCommand \index{Examples}
15985 checking corner cases of SDCC or if you plan to submit patches
15986 \begin_inset LatexCommand \index{Patch submission}
15993 The pic port uses a different set of regression tests, you'll find them
15996 sdcc/src/regression
16002 \begin_inset LatexCommand \index{Compiler internals}
16009 The anatomy of the compiler
16014 This is an excerpt from an article published in Circuit Cellar Magazine
16016 It's a little outdated (the compiler is much more efficient now and user/develo
16017 per friendly), but pretty well exposes the guts of it all.
16023 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
16024 It is fairly easy to retarget for other 8-bit MCU.
16025 Here we take a look at some of the internals of the compiler.
16030 \begin_inset LatexCommand \index{Parsing}
16037 Parsing the input source file and creating an AST (Annotated Syntax Tree
16038 \begin_inset LatexCommand \index{Annotated syntax tree}
16043 This phase also involves propagating types (annotating each node of the
16044 parse tree with type information) and semantic analysis.
16045 There are some MCU specific parsing rules.
16046 For example the storage classes, the extended storage classes are MCU specific
16047 while there may be a xdata storage class for 8051 there is no such storage
16048 class for z80 or Atmel AVR.
16049 SDCC allows MCU specific storage class extensions, i.e.
16050 xdata will be treated as a storage class specifier when parsing 8051 C
16051 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
16056 \begin_inset LatexCommand \index{iCode}
16063 Intermediate code generation.
16064 In this phase the AST is broken down into three-operand form (iCode).
16065 These three operand forms are represented as doubly linked lists.
16066 ICode is the term given to the intermediate form generated by the compiler.
16067 ICode example section shows some examples of iCode generated for some simple
16068 C source functions.
16072 \begin_inset LatexCommand \index{Optimizations}
16079 Bulk of the target independent optimizations is performed in this phase.
16080 The optimizations include constant propagation, common sub-expression eliminati
16081 on, loop invariant code movement, strength reduction of loop induction variables
16082 and dead-code elimination.
16085 Live range analysis
16086 \begin_inset LatexCommand \index{Live range analysis}
16093 During intermediate code generation phase, the compiler assumes the target
16094 machine has infinite number of registers and generates a lot of temporary
16096 The live range computation determines the lifetime of each of these compiler-ge
16097 nerated temporaries.
16098 A picture speaks a thousand words.
16099 ICode example sections show the live range annotations for each of the
16101 It is important to note here, each iCode is assigned a number in the order
16102 of its execution in the function.
16103 The live ranges are computed in terms of these numbers.
16104 The from number is the number of the iCode which first defines the operand
16105 and the to number signifies the iCode which uses this operand last.
16108 Register Allocation
16109 \begin_inset LatexCommand \index{Register allocation}
16116 The register allocation determines the type and number of registers needed
16118 In most MCUs only a few registers can be used for indirect addressing.
16119 In case of 8051 for example the registers R0 & R1 can be used to indirectly
16120 address the internal ram and DPTR to indirectly address the external ram.
16121 The compiler will try to allocate the appropriate register to pointer variables
16123 ICode example section shows the operands annotated with the registers assigned
16125 The compiler will try to keep operands in registers as much as possible;
16126 there are several schemes the compiler uses to do achieve this.
16127 When the compiler runs out of registers the compiler will check to see
16128 if there are any live operands which is not used or defined in the current
16129 basic block being processed, if there are any found then it will push that
16130 operand and use the registers in this block, the operand will then be popped
16131 at the end of the basic block.
16135 There are other MCU specific considerations in this phase.
16136 Some MCUs have an accumulator; very short-lived operands could be assigned
16137 to the accumulator instead of general-purpose register.
16143 Figure II gives a table of iCode operations supported by the compiler.
16144 The code generation involves translating these operations into corresponding
16145 assembly code for the processor.
16146 This sounds overly simple but that is the essence of code generation.
16147 Some of the iCode operations are generated on a MCU specific manner for
16148 example, the z80 port does not use registers to pass parameters so the
16149 SEND and RECV iCode operations will not be generated, and it also does
16150 not support JUMPTABLES.
16157 <Where is Figure II ?>
16161 \begin_inset LatexCommand \index{iCode}
16168 This section shows some details of iCode.
16169 The example C code does not do anything useful; it is used as an example
16170 to illustrate the intermediate code generated by the compiler.
16182 /* This function does nothing useful.
16189 for the purpose of explaining iCode */
16192 short function (data int *x)
16200 short i=10; /* dead initialization eliminated */
16205 short sum=10; /* dead initialization eliminated */
16218 while (*x) *x++ = *p++;
16232 /* compiler detects i,j to be induction variables */
16236 for (i = 0, j = 10 ; i < 10 ; i++, j
16262 mul += i * 3; /* this multiplication remains */
16268 gint += j * 3;/* this multiplication changed to addition */
16282 In addition to the operands each iCode contains information about the filename
16283 and line it corresponds to in the source file.
16284 The first field in the listing should be interpreted as follows:
16289 Filename(linenumber: iCode Execution sequence number : ICode hash table
16290 key : loop depth of the iCode).
16295 Then follows the human readable form of the ICode operation.
16296 Each operand of this triplet form can be of three basic types a) compiler
16297 generated temporary b) user defined variable c) a constant value.
16298 Note that local variables and parameters are replaced by compiler generated
16301 \begin_inset LatexCommand \index{Live range analysis}
16305 are computed only for temporaries (i.e.
16306 live ranges are not computed for global variables).
16308 \begin_inset LatexCommand \index{Register allocation}
16312 are allocated for temporaries only.
16313 Operands are formatted in the following manner:
16318 Operand Name [lr live-from : live-to ] { type information } [ registers
16324 As mentioned earlier the live ranges are computed in terms of the execution
16325 sequence number of the iCodes, for example
16327 the iTemp0 is live from (i.e.
16328 first defined in iCode with execution sequence number 3, and is last used
16329 in the iCode with sequence number 5).
16330 For induction variables such as iTemp21 the live range computation extends
16331 the lifetime from the start to the end of the loop.
16333 The register allocator used the live range information to allocate registers,
16334 the same registers may be used for different temporaries if their live
16335 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
16336 iTemp17 since their live ranges do not overlap.
16337 In addition the allocator also takes into consideration the type and usage
16338 of a temporary, for example itemp6 is a pointer to near space and is used
16339 as to fetch data from (i.e.
16340 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer registers (r0).
16341 Some short lived temporaries are allocated to special registers which have
16342 meaning to the code generator e.g.
16343 iTemp13 is allocated to a pseudo register CC which tells the back end that
16344 the temporary is used only for a conditional jump the code generation makes
16345 use of this information to optimize a compare and jump ICode.
16347 There are several loop optimizations
16348 \begin_inset LatexCommand \index{Loop optimization}
16352 performed by the compiler.
16353 It can detect induction variables iTemp21(i) and iTemp23(j).
16354 Also note the compiler does selective strength reduction
16355 \begin_inset LatexCommand \index{Strength reduction}
16360 the multiplication of an induction variable in line 18 (gint = j * 3) is
16361 changed to addition, a new temporary iTemp17 is allocated and assigned
16362 a initial value, a constant 3 is then added for each iteration of the loop.
16363 The compiler does not change the multiplication
16364 \begin_inset LatexCommand \index{Multiplication}
16368 in line 17 however since the processor does support an 8 * 8 bit multiplication.
16370 Note the dead code elimination
16371 \begin_inset LatexCommand \index{Dead-code elimination}
16375 optimization eliminated the dead assignments in line 7 & 8 to I and sum
16383 Sample.c (5:1:0:0) _entry($9) :
16388 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
16393 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
16398 Sample.c(11:4:53:0) preHeaderLbl0($11) :
16403 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
16409 Sample.c(11:6:5:1) _whilecontinue_0($1) :
16414 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
16420 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
16425 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
16431 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
16437 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
16443 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
16449 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
16450 * int}[r0] + 0x2 {short}
16455 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
16460 Sample.c(11:17:21:0)_whilebreak_0($3) :
16465 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
16470 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
16475 Sample.c(15:20:54:0)preHeaderLbl1($13) :
16480 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
16485 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
16490 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
16495 Sample.c(15:24:26:1)_forcond_0($4) :
16500 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
16506 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
16511 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
16512 + ITemp21 [lr21:38]{short}[r4]
16517 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
16523 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
16524 + iTemp15 [lr29:30]{short}[r1]
16529 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
16535 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
16541 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
16547 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
16553 Sample.c(19:38:47:1) goto _forcond_0($4)
16558 Sample.c(19:39:48:0)_forbreak_0($7) :
16563 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
16564 + ITemp11 [lr19:40]{short}[r3]
16569 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
16574 Sample.c(20:42:51:0)_return($8) :
16579 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
16585 Finally the code generated for this function:
16626 ; ----------------------------------------------
16631 ; function function
16636 ; ----------------------------------------------
16646 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
16658 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
16670 ;_whilecontinue_0($1) :
16680 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
16685 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
16744 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
16763 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
16810 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
16850 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
16876 ; iTemp6 [lr5:16]{_near * int}[r0] =
16881 ; iTemp6 [lr5:16]{_near * int}[r0] +
16898 ; goto _whilecontinue_0($1)
16910 ; _whilebreak_0($3) :
16920 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
16932 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
16944 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
16956 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
16975 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
17004 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
17009 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
17054 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
17059 ; iTemp21 [lr21:38]{short}[r4]
17085 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
17118 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
17123 ; iTemp15 [lr29:30]{short}[r1]
17142 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
17189 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
17236 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
17248 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
17262 cjne r5,#0xff,00104$
17274 ; goto _forcond_0($4)
17286 ; _forbreak_0($7) :
17296 ; ret iTemp24 [lr40:41]{short}
17339 A few words about basic block successors, predecessors and dominators
17342 Successors are basic blocks
17343 \begin_inset LatexCommand \index{Basic blocks}
17347 that might execute after this basic block.
17349 Predecessors are basic blocks that might execute before reaching this basic
17352 Dominators are basic blocks that WILL execute before reaching this basic
17386 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
17389 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
17392 c) domVect of [BB4] = BB1 ...
17393 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
17401 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
17411 Thanks to all the other volunteer developers who have helped with coding,
17412 testing, web-page creation, distribution sets, etc.
17413 You know who you are :-)
17420 This document was initially written by Sandeep Dutta
17423 All product names mentioned herein may be trademarks
17424 \begin_inset LatexCommand \index{Trademarks}
17428 of their respective companies.
17435 To avoid confusion, the installation and building options for sdcc itself
17436 (chapter 2) are not part of the index.
17440 \begin_inset LatexCommand \printindex{}