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, HC08, manual, mcs51, PIC, Z80},
12 linkcolor=blue] {hyperref}
16 \emergencystretch=30pt
21 \inputencoding default
24 \paperfontsize default
26 \papersize letterpaper
31 \use_numerical_citations 0
32 \paperorientation portrait
39 \paragraph_separation indent
41 \quotes_language swedish
49 Please note: double dashed longoptions (e.g.
50 --version) are written this way: -
64 three consecutive dashes simply result in a long resp.
68 Architecture specific stuff (like memory models, code examples) should maybe
72 into seperate sections/chapters/appendices (it is hard to document PIC or
76 a 8051 centered document) - for now simply add.
79 SDCC Compiler User Guide
93 The above strings enclosed in $ are automatically updated by cvs
97 \begin_inset LatexCommand \tableofcontents{}
130 ompiler) is a Freeware, retargettable, optimizing ANSI-C compiler by
134 designed for 8 bit Microprocessors.
135 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
137 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
141 , etc.), Dallas DS80C390 variants, Motorola HC08 and Zilog Z80 based MCUs.
142 It can be retargetted for other microprocessors, support for Microchip
143 PIC, Atmel AVR is under development.
144 The entire source code for the compiler is distributed under GPL.
145 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
146 SDCC has extensive language extensions suitable for utilizing various microcont
147 rollers and underlying hardware effectively.
152 In addition to the MCU specific optimizations SDCC also does a host of standard
156 global sub expression elimination,
159 loop optimizations (loop invariant, strength reduction of induction variables
163 constant folding & propagation,
169 dead code elimination
179 For the back-end SDCC uses a global register allocation scheme which should
180 be well suited for other 8 bit MCUs.
185 The peep hole optimizer uses a rule based substitution mechanism which is
191 Supported data-types are:
194 char (8 bits, 1 byte),
197 short and int (16 bits, 2 bytes),
200 long (32 bit, 4 bytes)
207 The compiler also allows
209 inline assembler code
211 to be embedded anywhere in a function.
212 In addition, routines developed in assembly can also be called.
216 SDCC also provides an option (-
226 -cyclomatic) to report the relative complexity of a function.
227 These functions can then be further optimized, or hand coded in assembly
233 SDCC also comes with a companion source level debugger SDCDB, the debugger
234 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
239 The latest version can be downloaded from
240 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
250 Please note: the compiler will probably always be some steps ahead of this
255 \begin_inset LatexCommand \index{Status of documentation}
265 Obviously this has pros and cons
274 All packages used in this compiler system are
282 ; source code for all the sub-packages (pre-processor, assemblers, linkers
283 etc) is distributed with the package.
284 This documentation is maintained using a freeware word processor (LyX).
286 This program is free software; you can redistribute it and/or modify it
287 under the terms of the GNU General Public License
288 \begin_inset LatexCommand \index{GNU General Public License, GPL}
292 as published by the Free Software Foundation; either version 2, or (at
293 your option) any later version.
294 This program is distributed in the hope that it will be useful, but WITHOUT
295 ANY WARRANTY; without even the implied warranty
296 \begin_inset LatexCommand \index{warranty}
300 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
301 See the GNU General Public License for more details.
302 You should have received a copy of the GNU General Public License along
303 with this program; if not, write to the Free Software Foundation, 59 Temple
304 Place - Suite 330, Boston, MA 02111-1307, USA.
305 In other words, you are welcome to use, share and improve this program.
306 You are forbidden to forbid anyone else to use, share and improve what
308 Help stamp out software-hoarding!
311 Typographic conventions
312 \begin_inset LatexCommand \index{Typographic conventions}
319 Throughout this manual, we will use the following convention.
320 Commands you have to type in are printed in
328 Code samples are printed in
333 Interesting items and new terms are printed in
338 Compatibility with previous versions
341 This version has numerous bug fixes compared with the previous version.
342 But we also introduced some incompatibilities with older versions.
343 Not just for the fun of it, but to make the compiler more stable, efficient
345 \begin_inset LatexCommand \index{ANSI-compliance}
350 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
354 for ANSI-Compliance).
360 short is now equivalent to int (16 bits), it used to be equivalent to char
361 (8 bits) which is not ANSI compliant
364 the default directory for gcc-builds where include, library and documentation
365 files are stored is now in /usr/local/share
368 char type parameters to vararg functions are casted to int unless explicitly
385 will push a as an int and as a char resp.
398 -regextend has been removed
411 -noregparms has been removed
424 -stack-after-data has been removed
429 <pending: more incompatibilities?>
435 What do you need before you start installation of SDCC? A computer, and
437 The preferred method of installation is to compile SDCC from source using
439 For Windows some pre-compiled binary distributions are available for your
441 You should have some experience with command line tools and compiler use.
447 The SDCC home page at
448 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
452 is a great place to find distribution sets.
453 You can also find links to the user mailing lists that offer help or discuss
454 SDCC with other SDCC users.
455 Web links to other SDCC related sites can also be found here.
456 This document can be found in the DOC directory of the source package as
458 Some of the other tools (simulator and assembler) included with SDCC contain
459 their own documentation and can be found in the source distribution.
460 If you want the latest unreleased software, the complete source package
461 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
464 Wishes for the future
467 There are (and always will be) some things that could be done.
468 Here are some I can think of:
475 char KernelFunction3(char p) at 0x340;
483 \begin_inset LatexCommand \index{code banking (not supported)}
493 If you can think of some more, please see the section
494 \begin_inset LatexCommand \ref{sub:Requesting-Features}
498 about filing feature requests
499 \begin_inset LatexCommand \index{Requesting features}
504 \begin_inset LatexCommand \index{Feature request}
514 \begin_inset LatexCommand \index{Installation}
521 For most users it is sufficient to skip to either section
522 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
527 \begin_inset LatexCommand \ref{sub:Windows-Install}
532 More detailled instructions follow below.
536 \begin_inset LatexCommand \index{Options SDCC configuration}
543 The install paths, search paths and other options are defined when running
545 The defaults can be overridden by:
547 \labelwidthstring 00.00.0000
559 -prefix see table below
561 \labelwidthstring 00.00.0000
573 -exec_prefix see table below
575 \labelwidthstring 00.00.0000
587 -bindir see table below
589 \labelwidthstring 00.00.0000
601 -datadir see table below
603 \labelwidthstring 00.00.0000
605 docdir environment variable, see table below
607 \labelwidthstring 00.00.0000
609 include_dir_suffix environment variable, see table below
611 \labelwidthstring 00.00.0000
613 lib_dir_suffix environment variable, see table below
615 \labelwidthstring 00.00.0000
617 sdccconf_h_dir_separator environment variable, either / or
622 This character will only be used in sdccconf.h; don't forget it's a C-header,
623 therefore a double-backslash is needed there.
625 \labelwidthstring 00.00.0000
637 -disable-mcs51-port Excludes the Intel mcs51 port
639 \labelwidthstring 00.00.0000
651 -disable-gbz80-port Excludes the Gameboy gbz80 port
653 \labelwidthstring 00.00.0000
665 -disable-z80-port Excludes the z80 port
667 \labelwidthstring 00.00.0000
679 -disable-avr-port Excludes the AVR port
681 \labelwidthstring 00.00.0000
693 -disable-ds390-port Excludes the DS390 port
695 \labelwidthstring 00.00.0000
707 -disable-hc08-port Excludes the HC08 port
709 \labelwidthstring 00.00.0000
721 -disable-pic-port Excludes the PIC port
723 \labelwidthstring 00.00.0000
735 -disable-xa51-port Excludes the XA51 port
737 \labelwidthstring 00.00.0000
749 -disable-ucsim Disables configuring and building of ucsim
751 \labelwidthstring 00.00.0000
763 -disable-device-lib-build Disables automatically building device libraries
765 \labelwidthstring 00.00.0000
777 -disable-packihx Disables building packihx
779 \labelwidthstring 00.00.0000
791 -enable-libgc Use the Bohem memory allocator.
792 Lower runtime footprint.
795 Furthermore the environment variables CC, CFLAGS, ...
796 the tools and their arguments can be influenced.
797 Please see `configure -
807 -help` and the man/info pages of `configure` for details.
811 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
812 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
813 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
814 At the moment it's not possible to change the default settings (it was
815 simply never required).
819 These configure options are compiled into the binaries, and can only be
820 changed by rerunning 'configure' and recompiling SDCC.
821 The configure options are written in
825 to distinguish them from run time environment variables (see section search
831 \begin_inset Quotes sld
835 \begin_inset Quotes srd
838 are used by the SDCC team to build the official Win32 binaries.
839 The SDCC team uses Mingw32 to build the official Windows binaries, because
846 a gcc compiler and last but not least
849 the binaries can be built by cross compiling on Sourceforge's compile farm.
852 See the examples, how to pass the Win32 settings to 'configure'.
853 The other Win32 builds using Borland, VC or whatever don't use 'configure',
854 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
866 <lyxtabular version="3" rows="8" columns="3">
868 <column alignment="block" valignment="top" leftline="true" width="0in">
869 <column alignment="block" valignment="top" leftline="true" width="0in">
870 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
871 <row topline="true" bottomline="true">
872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
938 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1031 <row topline="true">
1032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1069 <row topline="true">
1070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1097 <row topline="true" bottomline="true">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1116 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1134 'configure' also computes relative paths.
1135 This is needed for full relocatability of a binary package and to complete
1136 search paths (see section search paths below):
1142 \begin_inset Tabular
1143 <lyxtabular version="3" rows="4" columns="3">
1145 <column alignment="block" valignment="top" leftline="true" width="0in">
1146 <column alignment="block" valignment="top" leftline="true" width="0in">
1147 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1148 <row topline="true" bottomline="true">
1149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1174 <row topline="true" bottomline="true">
1175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1202 <row bottomline="true">
1203 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1221 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1230 <row bottomline="true">
1231 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1282 \begin_inset Quotes srd
1286 \begin_inset Quotes srd
1300 \begin_inset Quotes srd
1304 \begin_inset Quotes srd
1332 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1341 \begin_inset Quotes srd
1344 i586-mingw32msvc-gcc
1345 \begin_inset Quotes srd
1349 \begin_inset Quotes srd
1352 i586-mingw32msvc-g++
1353 \begin_inset Quotes srd
1361 \begin_inset Quotes srd
1364 i586-mingw32msvc-ranlib
1365 \begin_inset Quotes srd
1373 \begin_inset Quotes srd
1376 i586-mingw32msvc-strip
1377 \begin_inset Quotes srd
1395 \begin_inset Quotes srd
1399 \begin_inset Quotes srd
1417 \begin_inset Quotes srd
1421 \begin_inset Quotes srd
1429 \begin_inset Quotes srd
1433 \begin_inset Quotes srd
1441 \begin_inset Quotes srd
1445 \begin_inset Quotes srd
1453 \begin_inset Quotes srd
1457 \begin_inset Quotes srd
1464 sdccconf_h_dir_separator=
1465 \begin_inset Quotes srd
1477 \begin_inset Quotes srd
1494 -disable-device-lib-build
1522 -host=i586-mingw32msvc -
1532 -build=unknown-unknown-linux-gnu
1536 \begin_inset Quotes sld
1540 \begin_inset Quotes srd
1543 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1552 \begin_inset Quotes srd
1556 \begin_inset Quotes srd
1564 \begin_inset Quotes srd
1568 \begin_inset Quotes srd
1586 \begin_inset Quotes srd
1590 \begin_inset Quotes srd
1608 \begin_inset Quotes srd
1612 \begin_inset Quotes srd
1620 \begin_inset Quotes srd
1624 \begin_inset Quotes srd
1632 \begin_inset Quotes srd
1636 \begin_inset Quotes srd
1644 \begin_inset Quotes srd
1648 \begin_inset Quotes srd
1655 sdccconf_h_dir_separator=
1656 \begin_inset Quotes srd
1668 \begin_inset Quotes srd
1688 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1699 -C' turns on caching, which gives a little bit extra speed.
1700 However if options are changed, it can be necessary to delete the config.cache
1705 \begin_inset LatexCommand \label{sub:Install-paths}
1710 \begin_inset LatexCommand \index{Install paths}
1716 \added_space_top medskip \align center
1718 \begin_inset Tabular
1719 <lyxtabular version="3" rows="5" columns="4">
1721 <column alignment="center" valignment="top" leftline="true" width="0">
1722 <column alignment="center" valignment="top" leftline="true" width="0">
1723 <column alignment="center" valignment="top" leftline="true" width="0">
1724 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1725 <row topline="true" bottomline="true">
1726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1767 <row topline="true">
1768 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1786 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1794 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1807 <row topline="true">
1808 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 $DATADIR/ $INCLUDE_DIR_SUFFIX
1826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1831 /usr/local/share/sdcc/include
1834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1847 <row topline="true">
1848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1863 $DATADIR/$LIB_DIR_SUFFIX
1866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1871 /usr/local/share/sdcc/lib
1874 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1887 <row topline="true" bottomline="true">
1888 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1911 /usr/local/share/sdcc/doc
1914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1936 *compiler, preprocessor, assembler, and linker
1942 is auto-appended by the compiler, e.g.
1943 small, large, z80, ds390 etc
1946 The install paths can still be changed during `make install` with e.g.:
1949 make install prefix=$(HOME)/local/sdcc
1952 Of course this doesn't change the search paths compiled into the binaries.
1956 \begin_inset LatexCommand \label{sub:Search-Paths}
1961 \begin_inset LatexCommand \index{Search path}
1968 Some search paths or parts of them are determined by configure variables
1973 , see section above).
1974 Further search paths are determined by environment variables during runtime.
1977 The paths searched when running the compiler are as follows (the first catch
1983 Binary files (preprocessor, assembler and linker)
1989 \begin_inset Tabular
1990 <lyxtabular version="3" rows="4" columns="3">
1992 <column alignment="block" valignment="top" leftline="true" width="0in">
1993 <column alignment="block" valignment="top" leftline="true" width="0in">
1994 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1995 <row topline="true" bottomline="true">
1996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2021 <row topline="true">
2022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2051 <row topline="true">
2052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2057 Path of argv[0] (if available)
2060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2077 <row topline="true" bottomline="true">
2078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2119 \begin_inset Tabular
2120 <lyxtabular version="3" rows="6" columns="3">
2122 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2123 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2124 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2125 <row topline="true" bottomline="true">
2126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2151 <row topline="true">
2152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2188 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2207 <row topline="true">
2208 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2224 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2233 <row topline="true">
2234 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2248 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2260 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2271 <row topline="true">
2272 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2353 <row topline="true" bottomline="true">
2354 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2370 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2375 /usr/local/share/sdcc/
2380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2408 -nostdinc disables the last two search paths.
2418 With the exception of
2419 \begin_inset Quotes sld
2433 \begin_inset Quotes srd
2440 is auto-appended by the compiler (e.g.
2441 small, large, z80, ds390 etc.).
2448 \begin_inset Tabular
2449 <lyxtabular version="3" rows="6" columns="3">
2451 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2452 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2453 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2454 <row topline="true" bottomline="true">
2455 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2480 <row topline="true">
2481 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2499 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2536 <row topline="true">
2537 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2549 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2576 <row topline="true">
2577 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2588 $LIB_DIR_SUFFIX/<model>
2591 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2605 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2622 <row topline="true">
2623 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2638 $LIB_DIR_SUFFIX/<model>
2641 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2750 <row topline="true" bottomline="true">
2751 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2760 $LIB_DIR_SUFFIX/<model>
2763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2768 /usr/local/share/sdcc/
2775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2793 Don't delete any of the stray spaces in the table above without checking
2794 the HTML output (last line)!
2810 -nostdlib disables the last two search paths.
2814 \begin_inset LatexCommand \index{Building SDCC}
2821 Building SDCC on Linux
2822 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2831 Download the source package
2833 either from the SDCC CVS repository or from the nightly snapshots
2835 , it will be named something like sdcc
2846 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2855 Bring up a command line terminal, such as xterm.
2860 Unpack the file using a command like:
2863 "tar -xvzf sdcc.src.tar.gz
2868 , this will create a sub-directory called sdcc with all of the sources.
2871 Change directory into the main SDCC directory, for example type:
2888 This configures the package for compilation on your system.
2904 All of the source packages will compile, this can take a while.
2920 This copies the binary executables, the include files, the libraries and
2921 the documentation to the install directories.
2922 Proceed with section
2923 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
2930 Building SDCC on OSX 2.x
2933 Follow the instruction for Linux.
2937 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2938 )) fails to compile SDCC.
2939 Fortunately there's also gcc 2.9.x installed, which works fine.
2940 This compiler can be selected by running 'configure' with:
2943 ./configure CC=gcc2 CXX=g++2
2946 Cross compiling SDCC on Linux for Windows
2949 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2950 See section 'Configure Options'.
2953 Building SDCC on Windows
2956 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2958 They use Unix-sockets, which are not available on Win32.
2961 Building SDCC using Cygwin and Mingw32
2964 For building and installing a Cygwin executable follow the instructions
2970 \begin_inset Quotes sld
2974 \begin_inset Quotes srd
2977 Win32-binary can be built, which will not need the Cygwin-DLL.
2978 For the necessary 'configure' options see section 'configure options' or
2979 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
2983 In order to install Cygwin on Windows download setup.exe from
2984 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2990 \begin_inset Quotes sld
2993 default text file type
2994 \begin_inset Quotes srd
2998 \begin_inset Quotes sld
3002 \begin_inset Quotes srd
3005 and download/install at least the following packages.
3006 Some packages are selected by default, others will be automatically selected
3007 because of dependencies with the manually selected packages.
3008 Never deselect these packages!
3017 gcc ; version 3.x is fine, no need to use the old 2.9x
3020 binutils ; selected with gcc
3026 rxvt ; a nice console, which makes life much easier under windoze (see below)
3029 man ; not really needed for building SDCC, but you'll miss it sooner or
3033 less ; not really needed for building SDCC, but you'll miss it sooner or
3037 cvs ; only if you use CVS access
3040 If you want to develop something you'll need:
3043 python ; for the regression tests
3046 gdb ; the gnu debugger, together with the nice GUI
3047 \begin_inset Quotes sld
3051 \begin_inset Quotes srd
3057 openssh ; to access the CF or commit changes
3060 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3061 use autoconf-stable!
3064 rxvt is a nice console with history.
3065 Replace in your cygwin.bat the line
3084 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3087 -bg black -fg white -geometry 100x65 -e bash -
3100 Text selected with the mouse is automatically copied to the clipboard, pasting
3101 works with shift-insert.
3105 The other good tip is to make sure you have no //c/-style paths anywhere,
3106 use /cygdrive/c/ instead.
3107 Using // invokes a network lookup which is very slow.
3109 \begin_inset Quotes sld
3113 \begin_inset Quotes srd
3116 is too long, you can change it with e.g.
3122 SDCC sources use the unix line ending LF.
3123 Life is much easier, if you store the source tree on a drive which is mounted
3125 And use an editor which can handle LF-only line endings.
3126 Make sure not to commit files with windows line endings.
3127 The tabulator spacing
3128 \begin_inset LatexCommand \index{tabulator spacing (8)}
3132 used in the project is 8.
3135 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3140 Download the source package
3142 either from the SDCC CVS repository or from the
3143 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3149 , it will be named something like sdcc
3156 SDCC is distributed with all the projects, workspaces, and files you need
3157 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3158 doesn't build under MSVC).
3159 The workspace name is 'sdcc.dsw'.
3160 Please note that as it is now, all the executables are created in a folder
3164 Once built you need to copy the executables from sdcc
3168 bin before running SDCC.
3173 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3174 flex.exe, and gawk.exe.
3175 One good place to get them is
3176 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3184 Download the file UnxUtils
3185 \begin_inset LatexCommand \index{UnxUtils}
3190 Now you have to install the utilities and setup MSVC so it can locate the
3192 Here there are two alternatives (choose one!):
3199 a) Extract UnxUtils.zip to your C:
3201 hard disk PRESERVING the original paths, otherwise bison won't work.
3202 (If you are using WinZip make certain that 'Use folder names' is selected)
3206 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3207 in 'Show directories for:' select 'Executable files', and in the directories
3208 window add a new path: 'C:
3218 (As a side effect, you get a bunch of Unix utilities that could be useful,
3219 such as diff and patch.)
3226 This one avoids extracting a bunch of files you may not use, but requires
3231 a) Create a directory were to put the tools needed, or use a directory already
3239 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3240 to such directory WITHOUT preserving the original paths.
3241 (If you are using WinZip make certain that 'Use folder names' is not selected)
3245 c) Rename bison.exe to '_bison.exe'.
3249 d) Create a batch file 'bison.bat' in 'C:
3253 ' and add these lines:
3273 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3277 Steps 'c' and 'd' are needed because bison requires by default that the
3278 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3279 '/usr/local/share/' I think.
3280 So it is necessary to tell bison where those files are located if they
3281 are not in such directory.
3282 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3286 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3287 in 'Show directories for:' select 'Executable files', and in the directories
3288 window add a new path: 'c:
3291 Note that you can use any other path instead of 'c:
3293 util', even the path where the Visual C++ tools are, probably: 'C:
3297 Microsoft Visual Studio
3302 So you don't have to execute step 'e' :)
3306 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3307 the executables from sdcc
3311 bin, and you can compile using SDCC.
3314 Building SDCC Using Borland
3317 From the sdcc directory, run the command "make -f Makefile.bcc".
3318 This should regenerate all the .exe files in the bin directory except for
3319 sdcdb.exe (which currently doesn't build under Borland C++).
3322 If you modify any source files and need to rebuild, be aware that the dependenci
3323 es may not be correctly calculated.
3324 The safest option is to delete all .obj files and run the build again.
3325 From a Cygwin BASH prompt, this can easily be done with the command (be
3326 sure you are in the sdcc directory):
3336 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3338 ) -print -exec rm {}
3347 or on Windows NT/2000/XP from the command prompt with the command:
3354 del /s *.obj *.lib *.rul
3357 from the sdcc directory.
3360 Windows Install Using a Binary Package
3361 \begin_inset LatexCommand \label{sub:Windows-Install}
3368 Download the binary package from
3369 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3373 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3374 This should unpack to a group of sub-directories.
3375 An example directory structure after unpacking the mingw32 package is:
3380 bin for the executables, c:
3388 lib for the include and libraries.
3391 Adjust your environment variable PATH to include the location of the bin
3392 directory or start sdcc using the full path.
3395 Building the Documentation
3398 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3399 as changing into the doc directory and typing
3403 \begin_inset Quotes srd
3407 \begin_inset Quotes srd
3414 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3417 \begin_inset LatexCommand \url{www.lyx.org}
3421 as editor this is straightforward.
3422 If you want to avoid installing the tools you will have some success with
3423 a bootable Knoppix CD
3424 \begin_inset LatexCommand \url{http://www.knoppix.net}
3429 Prebuilt documentation in html and pdf format is available from
3430 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3437 Reading the Documentation
3440 Currently reading the document in pdf format is recommended, as for unknown
3441 reason the hyperlinks are working there whereas in the html version they
3448 If you should know why please drop us a note
3452 You'll find the pdf version at
3453 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
3459 This documentation is in some aspects different from a commercial documentation:
3463 It tries to document SDCC for several processor architectures in one document
3464 (commercially these probably would be separate documents/products).
3466 \begin_inset LatexCommand \index{Status of documentation}
3470 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3472 Z80, PIC14, PIC16 and HC08.
3475 There are many references pointing away from this documentation.
3476 Don't let this distract you.
3478 was a reference like
3479 \begin_inset LatexCommand \url{www.opencores.org}
3483 together with a statement
3484 \begin_inset Quotes sld
3487 some processors which are targetted by SDCC can be implemented in a
3504 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3509 \begin_inset Quotes srd
3512 we expect you to have a quick look there and come back.
3513 If you read this you are on the right track.
3516 Some sections attribute more space to problems, restrictions and warnings
3517 than to the solution.
3520 The installation section and the section about the debugger is intimidating.
3523 There are still lots of typos and there are more different writing styles
3527 Testing the SDCC Compiler
3528 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3535 The first thing you should do after installing your SDCC compiler is to
3551 \begin_inset LatexCommand \index{version}
3558 at the prompt, and the program should run and tell you the version.
3559 If it doesn't run, or gives a message about not finding sdcc program, then
3560 you need to check over your installation.
3561 Make sure that the sdcc bin directory is in your executable search path
3562 defined by the PATH environment setting (
3567 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3574 Install trouble-shooting for suggestions
3577 Make sure that the sdcc program is in the bin folder, if not perhaps something
3578 did not install correctly.
3586 is commonly installed as described in section
3587 \begin_inset Quotes sld
3590 Install and search paths
3591 \begin_inset Quotes srd
3600 Make sure the compiler works on a very simple example.
3601 Type in the following test.c program using your favorite
3627 Compile this using the following command:
3636 If all goes well, the compiler will generate a test.asm and test.rel file.
3637 Congratulations, you've just compiled your first program with SDCC.
3638 We used the -c option to tell SDCC not to link the generated code, just
3639 to keep things simple for this step.
3647 The next step is to try it with the linker.
3657 If all goes well the compiler will link with the libraries and produce
3658 a test.ihx output file.
3663 (no test.ihx, and the linker generates warnings), then the problem is most
3672 usr/local/share/sdcc/lib directory
3679 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3686 Install trouble-shooting for suggestions).
3694 The final test is to ensure
3702 header files and libraries.
3703 Edit test.c and change it to the following:
3720 strcpy(str1, "testing");
3727 Compile this by typing
3734 This should generate a test.ihx output file, and it should give no warnings
3735 such as not finding the string.h file.
3736 If it cannot find the string.h file, then the problem is that
3740 cannot find the /usr/local/share/sdcc/include directory
3747 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3754 Install trouble-shooting section for suggestions).
3772 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3776 to find exactly where SDCC is looking for the include and lib files.
3779 Install Trouble-shooting
3780 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3785 \begin_inset LatexCommand \index{Install trouble-shooting}
3792 If SDCC does not build correctly
3795 A thing to try is starting from scratch by unpacking the .tgz source package
3796 again in an empty directory.
3804 ./configure 2>&1 | tee configure.log
3818 make 2>&1 | tee make.log
3825 If anything goes wrong, you can review the log files to locate the problem.
3826 Or a relevant part of this can be attached to an email that could be helpful
3827 when requesting help from the mailing list.
3831 \begin_inset Quotes sld
3835 \begin_inset Quotes srd
3842 \begin_inset Quotes sld
3846 \begin_inset Quotes srd
3849 command is a script that analyzes your system and performs some configuration
3850 to ensure the source package compiles on your system.
3851 It will take a few minutes to run, and will compile a few tests to determine
3852 what compiler features are installed.
3856 \begin_inset Quotes sld
3860 \begin_inset Quotes srd
3866 This runs the GNU make tool, which automatically compiles all the source
3867 packages into the final installed binary executables.
3871 \begin_inset Quotes sld
3875 \begin_inset Quotes erd
3881 This will install the compiler, other executables libraries and include
3882 files into the appropriate directories.
3884 \begin_inset LatexCommand \ref{sub:Install-paths}
3890 \begin_inset LatexCommand \ref{sub:Search-Paths}
3895 about install and search paths.
3897 On most systems you will need super-user privileges to do this.
3903 SDCC is not just a compiler, but a collection of tools by various developers.
3904 These include linkers, assemblers, simulators and other components.
3905 Here is a summary of some of the components.
3906 Note that the included simulator and assembler have separate documentation
3907 which you can find in the source package in their respective directories.
3908 As SDCC grows to include support for other processors, other packages from
3909 various developers are included and may have their own sets of documentation.
3913 You might want to look at the files which are installed in <installdir>.
3914 At the time of this writing, we find the following programs for gcc-builds:
3918 In <installdir>/bin:
3921 sdcc - The compiler.
3924 sdcpp - The C preprocessor.
3927 asx8051 - The assembler for 8051 type processors.
3934 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3937 aslink -The linker for 8051 type processors.
3944 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3947 s51 - The ucSim 8051 simulator.
3950 sdcdb - The source debugger.
3953 packihx - A tool to pack (compress) Intel hex files.
3956 In <installdir>/share/sdcc/include
3962 In <installdir>/share/sdcc/lib
3965 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
3969 In <installdir>/share/sdcc/doc
3975 As development for other processors proceeds, this list will expand to include
3976 executables to support processors like AVR, PIC, etc.
3982 This is the actual compiler, it in turn uses the c-preprocessor and invokes
3983 the assembler and linkage editor.
3986 sdcpp - The C-Preprocessor
3990 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
3994 is a modified version of the GNU preprocessor.
3995 The C preprocessor is used to pull in #include sources, process #ifdef
3996 statements, #defines and so on.
4007 - The Assemblers and Linkage Editors
4010 This is retargettable assembler & linkage editor, it was developed by Alan
4012 John Hartman created the version for 8051, and I (Sandeep) have made some
4013 enhancements and bug fixes for it to work properly with SDCC.
4020 \begin_inset LatexCommand \index{s51}
4024 is a freeware, opensource simulator developed by Daniel Drotos (
4025 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
4030 The simulator is built as part of the build process.
4031 For more information visit Daniel's web site at:
4032 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4037 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4041 sdcdb - Source Level Debugger
4045 \begin_inset LatexCommand \index{sdcdb (debugger)}
4049 is the companion source level debugger.
4050 More about sdcdb in section
4051 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4056 The current version of the debugger uses Daniel's Simulator S51
4057 \begin_inset LatexCommand \index{s51}
4061 , but can be easily changed to use other simulators.
4071 Single Source File Projects
4074 For single source file 8051 projects the process is very simple.
4075 Compile your programs with the following command
4078 "sdcc sourcefile.c".
4082 This will compile, assemble and link your source file.
4083 Output files are as follows:
4087 \begin_inset LatexCommand \index{<file>.asm}
4092 \begin_inset LatexCommand \index{Assembler source}
4096 file created by the compiler
4100 \begin_inset LatexCommand \index{<file>.lst}
4105 \begin_inset LatexCommand \index{Assembler listing}
4109 file created by the Assembler
4113 \begin_inset LatexCommand \index{<file>.rst}
4118 \begin_inset LatexCommand \index{Assembler listing}
4122 file updated with linkedit information, created by linkage editor
4126 \begin_inset LatexCommand \index{<file>.sym}
4131 \begin_inset LatexCommand \index{Symbol listing}
4135 for the sourcefile, created by the assembler
4139 \begin_inset LatexCommand \index{<file>.rel}
4144 \begin_inset LatexCommand \index{<file>.o}
4149 \begin_inset LatexCommand \index{Object file}
4153 created by the assembler, input to Linkage editor
4157 \begin_inset LatexCommand \index{<file>.map}
4162 \begin_inset LatexCommand \index{Memory map}
4166 for the load module, created by the Linker
4170 \begin_inset LatexCommand \index{<file>.mem}
4174 - A file with a summary of the memory usage
4178 \begin_inset LatexCommand \index{<file>.ihx}
4182 - The load module in Intel hex format
4183 \begin_inset LatexCommand \index{Intel hex format}
4187 (you can select the Motorola S19 format
4188 \begin_inset LatexCommand \index{Motorola S19 format}
4203 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4208 If you need another format you might want to use
4215 \begin_inset LatexCommand \index{objdump (tool)}
4226 \begin_inset LatexCommand \index{srecord (tool)}
4231 Both formats are documented in the documentation of srecord
4232 \begin_inset LatexCommand \index{srecord (tool)}
4240 \begin_inset LatexCommand \index{<file>.adb}
4244 - An intermediate file containing debug information needed to create the
4256 \begin_inset LatexCommand \index{-\/-debug}
4264 \begin_inset LatexCommand \index{<file>.cdb}
4268 - An optional file (with -
4278 -debug) containing debug information.
4279 The format is documented in cdbfileformat.pdf.
4284 \begin_inset LatexCommand \index{<file> (no extension)}
4288 An optional AOMF or AOMF51
4289 \begin_inset LatexCommand \index{AOMF, AOMF51}
4293 file containing debug information (generated with option -
4320 ormat is commonly used by third party tools (debuggers
4321 \begin_inset LatexCommand \index{Debugger}
4325 , simulators, emulators)
4329 \begin_inset LatexCommand \index{<file>.dump*}
4333 - Dump file to debug the compiler it self (generated with option -
4343 -dumpall) (see section
4344 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4350 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4356 \begin_inset Quotes sld
4359 Anatomy of the compiler
4360 \begin_inset Quotes srd
4366 Projects with Multiple Source Files
4369 SDCC can compile only ONE file at a time.
4370 Let us for example assume that you have a project containing the following
4375 foo1.c (contains some functions)
4377 foo2.c (contains some more functions)
4379 foomain.c (contains more functions and the function main)
4387 The first two files will need to be compiled separately with the commands:
4419 Then compile the source file containing the
4424 \begin_inset LatexCommand \index{Linker}
4428 the files together with the following command:
4436 foomain.c\SpecialChar ~
4437 foo1.rel\SpecialChar ~
4442 \begin_inset LatexCommand \index{<file>.rel}
4454 can be separately compiled as well:
4465 sdcc foomain.rel foo1.rel foo2.rel
4472 The file containing the
4487 file specified in the command line, since the linkage editor processes
4488 file in the order they are presented to it.
4489 The linker is invoked from SDCC using a script file with extension .lnk
4490 \begin_inset LatexCommand \index{<file>.lnk}
4495 You can view this file to troubleshoot linking problems such as those arising
4496 from missing libraries.
4499 Projects with Additional Libraries
4500 \begin_inset LatexCommand \index{Libraries}
4507 Some reusable routines may be compiled into a library, see the documentation
4508 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4512 \begin_inset LatexCommand \index{<file>.lib}
4519 Libraries created in this manner can be included in the command line.
4520 Make sure you include the -L <library-path> option to tell the linker where
4521 to look for these files if they are not in the current directory.
4522 Here is an example, assuming you have the source file
4534 (if that is not the same as your current project):
4541 sdcc foomain.c foolib.lib -L mylib
4552 must be an absolute path name.
4556 The most efficient way to use libraries is to keep separate modules in separate
4558 The lib file now should name all the modules.rel
4559 \begin_inset LatexCommand \index{<file>.rel}
4564 For an example see the standard library file
4568 in the directory <installdir>/share/lib/small.
4571 Using sdcclib to Create and Manage Libraries
4572 \begin_inset LatexCommand \index{sdcclib}
4579 Alternatively, instead of having a .rel file for each entry on the library
4580 file as described in the preceding section, sdcclib can be used to embed
4581 all the modules belonging to such library in the library file itself.
4582 This results in a larger library file, but it greatly reduces the number
4583 of disk files accessed by the linker.
4584 Additionally, the packed library file contains an index of all include
4585 modules and symbols that significantly speeds up the linking process.
4586 To display a list of options supported by sdcclib type:
4595 \begin_inset LatexCommand \index{sdcclib}
4606 To create a new library file, start by compiling all the required modules.
4639 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4641 The next step is to add the .rel files to the library file:
4648 sdcclib libint.lib _divsint.rel
4649 \begin_inset LatexCommand \index{sdcclib}
4658 sdcclib libint.lib _divuint.rel
4663 sdcclib libint.lib _modsint.rel
4668 sdcclib libint.lib _moduint.rel
4673 sdcclib libint.lib _mulint.rel
4680 If the file already exists in the library, it will be replaced.
4681 To see what modules and symbols are included in the library, options -s
4682 and -m are available.
4689 sdcclib -s libint.lib
4690 \begin_inset LatexCommand \index{sdcclib}
4794 If the source files are compiled using --debug, the corresponding debug
4795 information file .adb will be include in the library file as well.
4796 The library files created with sdcclib are plain text files, so they can
4797 be viewed with a text editor.
4798 It is not recomended to modify a library file created with sdcclib using
4799 a text editor, as there are file indexes numbers located accross the file
4800 used by the linker to quickly locate the required module to link.
4801 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
4802 it can be safely deleted, since all the information required for linking
4803 is embedded in the library file itself.
4804 Library files created using sdcclib are used as described in the preceding
4808 Command Line Options
4809 \begin_inset LatexCommand \index{Command Line Options}
4816 Processor Selection Options
4817 \begin_inset LatexCommand \index{Options processor selection}
4822 \begin_inset LatexCommand \index{Processor selection options}
4828 \labelwidthstring 00.00.0000
4833 \begin_inset LatexCommand \index{-mmcs51}
4839 Generate code for the Intel MCS51
4840 \begin_inset LatexCommand \index{MCS51}
4844 family of processors.
4845 This is the default processor target.
4847 \labelwidthstring 00.00.0000
4852 \begin_inset LatexCommand \index{-mds390}
4858 Generate code for the Dallas DS80C390
4859 \begin_inset LatexCommand \index{DS80C390}
4865 \labelwidthstring 00.00.0000
4870 \begin_inset LatexCommand \index{-mds400}
4876 Generate code for the Dallas DS80C400
4877 \begin_inset LatexCommand \index{DS80C400}
4883 \labelwidthstring 00.00.0000
4888 \begin_inset LatexCommand \index{-mhc08}
4894 Generate code for the Motorola HC08
4895 \begin_inset LatexCommand \index{HC08}
4899 family of processors (added Oct 2003).
4901 \labelwidthstring 00.00.0000
4906 \begin_inset LatexCommand \index{-mz80}
4912 Generate code for the Zilog Z80
4913 \begin_inset LatexCommand \index{Z80}
4917 family of processors.
4919 \labelwidthstring 00.00.0000
4924 \begin_inset LatexCommand \index{-mgbz80}
4930 Generate code for the GameBoy Z80
4931 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
4935 processor (Not actively maintained).
4937 \labelwidthstring 00.00.0000
4942 \begin_inset LatexCommand \index{-mavr}
4948 Generate code for the Atmel AVR
4949 \begin_inset LatexCommand \index{AVR}
4953 processor (In development, not complete).
4954 AVR users should probably have a look at avr-gcc
4955 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
4962 I think it is fair to direct users there for now.
4963 Open source is also about avoiding unnecessary work .
4964 But I didn't find the 'official' link.
4966 \labelwidthstring 00.00.0000
4971 \begin_inset LatexCommand \index{-mpic14}
4977 Generate code for the Microchip PIC 14
4978 \begin_inset LatexCommand \index{PIC14}
4982 -bit processors (p16f84 and variants.
4983 In development, not complete).
4986 p16f627 p16f628 p16f84 p16f873 p16f877?
4988 \labelwidthstring 00.00.0000
4993 \begin_inset LatexCommand \index{-mpic16}
4999 Generate code for the Microchip PIC 16
5000 \begin_inset LatexCommand \index{PIC16}
5004 -bit processors (p18f452 and variants.
5005 In development, not complete).
5007 \labelwidthstring 00.00.0000
5013 Generate code for the Toshiba TLCS-900H
5014 \begin_inset LatexCommand \index{TLCS-900H}
5018 processor (Not maintained, not complete).
5020 \labelwidthstring 00.00.0000
5025 \begin_inset LatexCommand \index{-mxa51}
5031 Generate code for the Phillips XA51
5032 \begin_inset LatexCommand \index{XA51}
5036 processor (Not maintained, not complete).
5039 Preprocessor Options
5040 \begin_inset LatexCommand \index{Options preprocessor}
5045 \begin_inset LatexCommand \index{Preprocessor options}
5050 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5056 \labelwidthstring 00.00.0000
5061 \begin_inset LatexCommand \index{-I<path>}
5067 The additional location where the pre processor will look for <..h> or
5068 \begin_inset Quotes eld
5072 \begin_inset Quotes erd
5077 \labelwidthstring 00.00.0000
5082 \begin_inset LatexCommand \index{-D<macro[=value]>}
5088 Command line definition of macros.
5089 Passed to the preprocessor.
5091 \labelwidthstring 00.00.0000
5096 \begin_inset LatexCommand \index{-M}
5102 Tell the preprocessor to output a rule suitable for make describing the
5103 dependencies of each object file.
5104 For each source file, the preprocessor outputs one make-rule whose target
5105 is the object file name for that source file and whose dependencies are
5106 all the files `#include'd in it.
5107 This rule may be a single line or may be continued with `
5109 '-newline if it is long.
5110 The list of rules is printed on standard output instead of the preprocessed
5113 \begin_inset LatexCommand \index{-E}
5119 \labelwidthstring 00.00.0000
5124 \begin_inset LatexCommand \index{-C}
5130 Tell the preprocessor not to discard comments.
5131 Used with the `-E' option.
5133 \labelwidthstring 00.00.0000
5138 \begin_inset LatexCommand \index{-MM}
5149 Like `-M' but the output mentions only the user header files included with
5151 \begin_inset Quotes eld
5155 System header files included with `#include <file>' are omitted.
5157 \labelwidthstring 00.00.0000
5162 \begin_inset LatexCommand \index{-Aquestion(answer)}
5168 Assert the answer answer for question, in case it is tested with a preprocessor
5169 conditional such as `#if #question(answer)'.
5170 `-A-' disables the standard assertions that normally describe the target
5173 \labelwidthstring 00.00.0000
5178 \begin_inset LatexCommand \index{-Umacro}
5184 Undefine macro macro.
5185 `-U' options are evaluated after all `-D' options, but before any `-include'
5186 and `-imacros' options.
5188 \labelwidthstring 00.00.0000
5193 \begin_inset LatexCommand \index{-dM}
5199 Tell the preprocessor to output only a list of the macro definitions that
5200 are in effect at the end of preprocessing.
5201 Used with the `-E' option.
5203 \labelwidthstring 00.00.0000
5208 \begin_inset LatexCommand \index{-dD}
5214 Tell the preprocessor to pass all macro definitions into the output, in
5215 their proper sequence in the rest of the output.
5217 \labelwidthstring 00.00.0000
5222 \begin_inset LatexCommand \index{-dN}
5233 Like `-dD' except that the macro arguments and contents are omitted.
5234 Only `#define name' is included in the output.
5236 \labelwidthstring 00.00.0000
5241 preprocessorOption[,preprocessorOption]
5244 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5249 Pass the preprocessorOption to the preprocessor
5254 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5259 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5260 Collection (gcc), if you need more dedicated options please refer to the
5262 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5270 \begin_inset LatexCommand \index{Options linker}
5275 \begin_inset LatexCommand \index{Linker options}
5281 \labelwidthstring 00.00.0000
5301 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5306 \begin_inset LatexCommand \index{-L -\/-lib-path}
5313 <absolute path to additional libraries> This option is passed to the linkage
5314 editor's additional libraries
5315 \begin_inset LatexCommand \index{Libraries}
5320 The path name must be absolute.
5321 Additional library files may be specified in the command line.
5322 See section Compiling programs for more details.
5324 \labelwidthstring 00.00.0000
5341 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5346 <Value> The start location of the external ram
5347 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5351 , default value is 0.
5352 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5362 -xram-loc 0x8000 or -
5374 \labelwidthstring 00.00.0000
5391 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5396 <Value> The start location of the code
5397 \begin_inset LatexCommand \index{code}
5401 segment, default value 0.
5402 Note when this option is used the interrupt vector table is also relocated
5403 to the given address.
5404 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5414 -code-loc 0x8000 or -
5426 \labelwidthstring 00.00.0000
5443 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5448 <Value> By default the stack
5449 \begin_inset LatexCommand \index{stack}
5453 is placed after the data segment.
5454 Using this option the stack can be placed anywhere in the internal memory
5456 The value entered can be in Hexadecimal or Decimal format, e.g.
5467 -stack-loc 0x20 or -
5478 Since the sp register is incremented before a push or call, the initial
5479 sp will be set to one byte prior the provided value.
5480 The provided value should not overlap any other memory areas such as used
5481 register banks or the data segment and with enough space for the current
5484 \labelwidthstring 00.00.0000
5501 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5506 <Value> The start location of the internal ram data
5507 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5512 The value entered can be in Hexadecimal or Decimal format, eg.
5534 (By default, the start location of the internal ram data segment is set
5535 as low as possible in memory, taking into account the used register banks
5536 and the bit segment at address 0x20.
5537 For example if register banks 0 and 1 are used without bit variables, the
5538 data segment will be set, if -
5548 -data-loc is not used, to location 0x10.)
5550 \labelwidthstring 00.00.0000
5567 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5572 <Value> The start location of the indirectly addressable internal ram
5573 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5577 of the 8051, default value is 0x80.
5578 The value entered can be in Hexadecimal or Decimal format, eg.
5589 -idata-loc 0x88 or -
5601 \labelwidthstring 00.00.0000
5618 <Value> The start location of the bit
5619 \begin_inset LatexCommand \index{bit}
5623 addressable internal ram of the 8051.
5629 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5632 \labelwidthstring 00.00.0000
5647 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5656 The linker output (final object code) is in Intel Hex format.
5657 \begin_inset LatexCommand \index{Intel hex format}
5661 This is the default option.
5662 The format itself is documented in the documentation of srecord
5663 \begin_inset LatexCommand \index{srecord (tool)}
5669 \labelwidthstring 00.00.0000
5684 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5693 The linker output (final object code) is in Motorola S19 format
5694 \begin_inset LatexCommand \index{Motorola S19 format}
5699 The format itself is documented in the documentation of srecord.
5701 \labelwidthstring 00.00.0000
5706 linkOption[,linkOption]
5709 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5714 Pass the linkOption to the linker.
5715 See file sdcc/as/doc/asxhtm.html for more on linker options.
5719 \begin_inset LatexCommand \index{Options MCS51}
5724 \begin_inset LatexCommand \index{MCS51 options}
5730 \labelwidthstring 00.00.0000
5745 \begin_inset LatexCommand \index{-\/-model-small}
5756 Generate code for Small Model programs, see section Memory Models for more
5758 This is the default model.
5760 \labelwidthstring 00.00.0000
5775 \begin_inset LatexCommand \index{-\/-model-large}
5781 Generate code for Large model programs, see section Memory Models for more
5783 If this option is used all source files in the project have to be compiled
5786 \labelwidthstring 00.00.0000
5801 \begin_inset LatexCommand \index{-\/-xstack}
5807 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5808 variables and passing parameters.
5810 \begin_inset LatexCommand \ref{sub:External-Stack}
5815 External Stack for more details.
5817 \labelwidthstring 00.00.0000
5835 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
5839 Causes the linker to check if the internal ram usage is within limits of
5842 \labelwidthstring 00.00.0000
5860 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
5864 Causes the linker to check if the external ram usage is within limits of
5867 \labelwidthstring 00.00.0000
5885 \begin_inset LatexCommand \index{-\/-code-size <Value>}
5889 Causes the linker to check if the code memory usage is within limits of
5892 \labelwidthstring 00.00.0000
5910 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
5914 Causes the linker to check if there is at minimum <Value> bytes for stack.
5916 \labelwidthstring 00.00.0000
5934 \begin_inset LatexCommand \index{-\/-pack-iram}
5938 Causes the linker use unused register banks for data variables or stack.
5941 DS390 / DS400 Options
5942 \begin_inset LatexCommand \index{Options DS390}
5947 \begin_inset LatexCommand \index{DS390 options}
5953 \labelwidthstring 00.00.0000
5970 \begin_inset LatexCommand \index{-\/-model-flat24}
5980 Generate 24-bit flat mode code.
5981 This is the one and only that the ds390 code generator supports right now
5982 and is default when using
5987 See section Memory Models for more details.
5989 \labelwidthstring 00.00.0000
6004 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6010 disable interrupts during ESP:SP updates.
6012 \labelwidthstring 00.00.0000
6029 \begin_inset LatexCommand \index{-\/-stack-10bit}
6033 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6034 This is the one and only that the ds390 code generator supports right now
6035 and is default when using
6040 In this mode, the stack is located in the lower 1K of the internal RAM,
6041 which is mapped to 0x400000.
6042 Note that the support is incomplete, since it still uses a single byte
6043 as the stack pointer.
6044 This means that only the lower 256 bytes of the potential 1K stack space
6045 will actually be used.
6046 However, this does allow you to reclaim the precious 256 bytes of low RAM
6047 for use for the DATA and IDATA segments.
6048 The compiler will not generate any code to put the processor into 10 bit
6050 It is important to ensure that the processor is in this mode before calling
6051 any re-entrant functions compiled with this option.
6052 In principle, this should work with the
6065 \begin_inset LatexCommand \index{-\/-stack-auto}
6071 option, but that has not been tested.
6072 It is incompatible with the
6085 \begin_inset LatexCommand \index{-\/-xstack}
6092 It also only makes sense if the processor is in 24 bit contiguous addressing
6105 -model-flat24 option
6109 \labelwidthstring 00.00.0000
6124 \begin_inset LatexCommand \index{-\/-stack-probe}
6130 insert call to function __stack_probe at each function prologue.
6132 \labelwidthstring 00.00.0000
6147 \begin_inset LatexCommand \index{-\/-tini-libid}
6153 <nnnn> LibraryID used in -mTININative.
6156 \labelwidthstring 00.00.0000
6171 \begin_inset LatexCommand \index{-\/-use-accelerator}
6177 generate code for DS390 Arithmetic Accelerator.
6182 \begin_inset LatexCommand \index{Options Z80}
6187 \begin_inset LatexCommand \index{Z80 options}
6193 \labelwidthstring 00.00.0000
6210 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6220 Force a called function to always save BC.
6222 \labelwidthstring 00.00.0000
6239 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6243 When linking, skip the standard crt0.o object file.
6244 You must provide your own crt0.o for your system when linking.
6248 Optimization Options
6249 \begin_inset LatexCommand \index{Options optimization}
6254 \begin_inset LatexCommand \index{Optimization options}
6260 \labelwidthstring 00.00.0000
6275 \begin_inset LatexCommand \index{-\/-nogcse}
6281 Will not do global subexpression elimination, this option may be used when
6282 the compiler creates undesirably large stack/data spaces to store compiler
6284 A warning message will be generated when this happens and the compiler
6285 will indicate the number of extra bytes it allocated.
6286 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6288 \begin_inset LatexCommand \index{\#pragma nogcse}
6292 can be used to turn off global subexpression elimination
6293 \begin_inset LatexCommand \index{Subexpression elimination}
6297 for a given function only.
6299 \labelwidthstring 00.00.0000
6314 \begin_inset LatexCommand \index{-\/-noinvariant}
6320 Will not do loop invariant optimizations, this may be turned off for reasons
6321 explained for the previous option.
6322 For more details of loop optimizations performed see section Loop Invariants.
6323 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6325 \begin_inset LatexCommand \index{\#pragma noinvariant}
6329 can be used to turn off invariant optimizations for a given function only.
6331 \labelwidthstring 00.00.0000
6346 \begin_inset LatexCommand \index{-\/-noinduction}
6352 Will not do loop induction optimizations, see section strength reduction
6354 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6356 \begin_inset LatexCommand \index{\#pragma noinduction}
6360 can be used to turn off induction optimizations for a given function only.
6362 \labelwidthstring 00.00.0000
6377 \begin_inset LatexCommand \index{-\/-nojtbound}
6388 Will not generate boundary condition check when switch statements
6389 \begin_inset LatexCommand \index{switch statement}
6393 are implemented using jump-tables.
6395 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6400 Switch Statements for more details.
6401 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6403 \begin_inset LatexCommand \index{\#pragma nojtbound}
6407 can be used to turn off boundary checking for jump tables for a given function
6410 \labelwidthstring 00.00.0000
6425 \begin_inset LatexCommand \index{-\/-noloopreverse}
6434 Will not do loop reversal
6435 \begin_inset LatexCommand \index{Loop reversing}
6441 \labelwidthstring 00.00.0000
6458 \begin_inset LatexCommand \index{-\/-nolabelopt }
6462 Will not optimize labels (makes the dumpfiles more readable).
6464 \labelwidthstring 00.00.0000
6479 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6485 Will not memcpy initialized data from code space into xdata space.
6486 This saves a few bytes in code space if you don't have initialized data.
6488 \labelwidthstring 00.00.0000
6503 \begin_inset LatexCommand \index{-\/-nooverlay}
6509 The compiler will not overlay parameters and local variables of any function,
6510 see section Parameters and local variables for more details.
6512 \labelwidthstring 00.00.0000
6527 \begin_inset LatexCommand \index{-\/-no-peep}
6533 Disable peep-hole optimization.
6535 \labelwidthstring 00.00.0000
6552 \begin_inset LatexCommand \index{-\/-peep-file}
6557 <filename> This option can be used to use additional rules to be used by
6558 the peep hole optimizer.
6560 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6565 Peep Hole optimizations for details on how to write these rules.
6567 \labelwidthstring 00.00.0000
6582 \begin_inset LatexCommand \index{-\/-peep-asm}
6588 Pass the inline assembler code through the peep hole optimizer.
6589 This can cause unexpected changes to inline assembler code, please go through
6590 the peephole optimizer
6591 \begin_inset LatexCommand \index{Peephole optimizer}
6595 rules defined in the source file tree '<target>/peeph.def' before using
6600 \begin_inset LatexCommand \index{Options other}
6606 \labelwidthstring 00.00.0000
6622 \begin_inset LatexCommand \index{-\/-compile-only}
6627 \begin_inset LatexCommand \index{-c -\/-compile-only}
6633 will compile and assemble the source, but will not call the linkage editor.
6635 \labelwidthstring 00.00.0000
6654 \begin_inset LatexCommand \index{-\/-c1mode}
6660 reads the preprocessed source from standard input and compiles it.
6661 The file name for the assembler output must be specified using the -o option.
6663 \labelwidthstring 00.00.0000
6668 \begin_inset LatexCommand \index{-E}
6674 Run only the C preprocessor.
6675 Preprocess all the C source files specified and output the results to standard
6678 \labelwidthstring 00.00.0000
6684 \begin_inset LatexCommand \index{-o <path/file>}
6690 The output path resp.
6691 file where everything will be placed.
6692 If the parameter is a path, it must have a trailing slash (or backslash
6693 for the Windows binaries) to be recognized as a path.
6696 \labelwidthstring 00.00.0000
6711 \begin_inset LatexCommand \index{-\/-stack-auto}
6722 All functions in the source file will be compiled as
6727 \begin_inset LatexCommand \index{reentrant}
6732 the parameters and local variables will be allocated on the stack
6733 \begin_inset LatexCommand \index{stack}
6738 see section Parameters and Local Variables for more details.
6739 If this option is used all source files in the project should be compiled
6743 \labelwidthstring 00.00.0000
6758 \begin_inset LatexCommand \index{-\/-callee-saves}
6762 function1[,function2][,function3]....
6765 The compiler by default uses a caller saves convention for register saving
6766 across function calls, however this can cause unnecessary register pushing
6767 & popping when calling small functions from larger functions.
6768 This option can be used to switch the register saving convention for the
6769 function names specified.
6770 The compiler will not save registers when calling these functions, no extra
6771 code will be generated at the entry & exit (function prologue
6774 \begin_inset LatexCommand \index{function prologue}
6783 \begin_inset LatexCommand \index{function epilogue}
6789 ) for these functions to save & restore the registers used by these functions,
6790 this can SUBSTANTIALLY reduce code & improve run time performance of the
6792 In the future the compiler (with inter procedural analysis) will be able
6793 to determine the appropriate scheme to use for each function call.
6794 DO NOT use this option for built-in functions such as _mulint..., if this
6795 option is used for a library function the appropriate library function
6796 needs to be recompiled with the same option.
6797 If the project consists of multiple source files then all the source file
6798 should be compiled with the same -
6808 -callee-saves option string.
6809 Also see #pragma\SpecialChar ~
6811 \begin_inset LatexCommand \index{\#pragma callee\_saves}
6817 \labelwidthstring 00.00.0000
6832 \begin_inset LatexCommand \index{-\/-debug}
6841 When this option is used the compiler will generate debug information.
6842 The debug information collected in a file with .cdb extension can be used
6844 For more information see documentation for SDCDB.
6845 Another file with no extension contains debug information in AOMF or AOMF51
6846 \begin_inset LatexCommand \index{AOMF, AOMF51}
6850 format which is commonly used by third party tools.
6852 \labelwidthstring 00.00.0000
6857 \begin_inset LatexCommand \index{-S}
6868 Stop after the stage of compilation proper; do not assemble.
6869 The output is an assembler code file for the input file specified.
6871 \labelwidthstring 00.00.0000
6886 \begin_inset LatexCommand \index{-\/-int-long-reent}
6892 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
6893 Note by default these libraries are compiled as non-reentrant.
6894 See section Installation for more details.
6896 \labelwidthstring 00.00.0000
6911 \begin_inset LatexCommand \index{-\/-cyclomatic}
6920 This option will cause the compiler to generate an information message for
6921 each function in the source file.
6922 The message contains some
6926 information about the function.
6927 The number of edges and nodes the compiler detected in the control flow
6928 graph of the function, and most importantly the
6930 cyclomatic complexity
6931 \begin_inset LatexCommand \index{Cyclomatic complexity}
6937 see section on Cyclomatic Complexity for more details.
6939 \labelwidthstring 00.00.0000
6954 \begin_inset LatexCommand \index{-\/-float-reent}
6960 Floating point library is compiled as reentrant
6961 \begin_inset LatexCommand \index{reentrant}
6966 See section Installation for more details.
6968 \labelwidthstring 00.00.0000
6983 \begin_inset LatexCommand \index{-\/-main-return}
6989 This option can be used when the code generated is called by a monitor
6991 The compiler will generate a 'ret' upon return from the 'main'
6992 \begin_inset LatexCommand \index{main return}
6997 The default setting is to lock up i.e.
7004 \labelwidthstring 00.00.0000
7019 \begin_inset LatexCommand \index{-\/-nostdincl}
7025 This will prevent the compiler from passing on the default include path
7026 to the preprocessor.
7028 \labelwidthstring 00.00.0000
7043 \begin_inset LatexCommand \index{-\/-nostdlib}
7049 This will prevent the compiler from passing on the default library
7050 \begin_inset LatexCommand \index{Libraries}
7056 \labelwidthstring 00.00.0000
7071 \begin_inset LatexCommand \index{-\/-verbose}
7077 Shows the various actions the compiler is performing.
7079 \labelwidthstring 00.00.0000
7084 \begin_inset LatexCommand \index{-V}
7090 Shows the actual commands the compiler is executing.
7092 \labelwidthstring 00.00.0000
7107 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7113 Hides your ugly and inefficient c-code from the asm file, so you can always
7114 blame the compiler :).
7116 \labelwidthstring 00.00.0000
7131 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7137 Include i-codes in the asm file.
7138 Sounds like noise but is most helpful for debugging the compiler itself.
7140 \labelwidthstring 00.00.0000
7155 \begin_inset LatexCommand \index{-\/-less-pedantic}
7161 Disable some of the more pedantic warnings
7162 \begin_inset LatexCommand \index{Warnings}
7166 (jwk burps: please be more specific here, please!).
7167 If you want rather more than less warnings you should consider using a
7168 separate tool dedicated to syntax checking like splint
7169 \begin_inset LatexCommand \url{www.splint.org}
7175 \labelwidthstring 00.00.0000
7190 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7196 Display the directories in the compiler's search path
7198 \labelwidthstring 00.00.0000
7213 \begin_inset LatexCommand \index{-\/-vc}
7219 Display errors and warnings using MSVC style, so you can use SDCC with
7222 \labelwidthstring 00.00.0000
7237 \begin_inset LatexCommand \index{-\/-use-stdout}
7243 Send errors and warnings to stdout instead of stderr.
7245 \labelwidthstring 00.00.0000
7250 asmOption[,asmOption]
7253 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7258 Pass the asmOption to the assembler
7259 \begin_inset LatexCommand \index{Options assembler}
7264 \begin_inset LatexCommand \index{Assembler options}
7269 See file sdcc/as/doc/asxhtm.html for assembler options.
7272 Intermediate Dump Options
7273 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7278 \begin_inset LatexCommand \index{Options intermediate dump}
7283 \begin_inset LatexCommand \index{Intermediate dump options}
7290 The following options are provided for the purpose of retargetting and debugging
7292 These provided a means to dump the intermediate code (iCode
7293 \begin_inset LatexCommand \index{iCode}
7297 ) generated by the compiler in human readable form at various stages of
7298 the compilation process.
7299 More on iCodes see chapter
7300 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7305 \begin_inset Quotes srd
7308 The anatomy of the compiler
7309 \begin_inset Quotes srd
7314 \labelwidthstring 00.00.0000
7329 \begin_inset LatexCommand \index{-\/-dumpraw}
7335 This option will cause the compiler to dump the intermediate code into
7338 <source filename>.dumpraw
7340 just after the intermediate code has been generated for a function, i.e.
7341 before any optimizations are done.
7343 \begin_inset LatexCommand \index{Basic blocks}
7347 at this stage ordered in the depth first number, so they may not be in
7348 sequence of execution.
7350 \labelwidthstring 00.00.0000
7365 \begin_inset LatexCommand \index{-\/-dumpgcse}
7371 Will create a dump of iCode's, after global subexpression elimination
7372 \begin_inset LatexCommand \index{Global subexpression elimination}
7378 <source filename>.dumpgcse.
7380 \labelwidthstring 00.00.0000
7395 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7401 Will create a dump of iCode's, after deadcode elimination
7402 \begin_inset LatexCommand \index{Dead-code elimination}
7408 <source filename>.dumpdeadcode.
7410 \labelwidthstring 00.00.0000
7425 \begin_inset LatexCommand \index{-\/-dumploop}
7434 Will create a dump of iCode's, after loop optimizations
7435 \begin_inset LatexCommand \index{Loop optimization}
7441 <source filename>.dumploop.
7443 \labelwidthstring 00.00.0000
7458 \begin_inset LatexCommand \index{-\/-dumprange}
7467 Will create a dump of iCode's, after live range analysis
7468 \begin_inset LatexCommand \index{Live range analysis}
7474 <source filename>.dumprange.
7476 \labelwidthstring 00.00.0000
7491 \begin_inset LatexCommand \index{-\/-dumlrange}
7497 Will dump the life ranges
7498 \begin_inset LatexCommand \index{Live range analysis}
7504 \labelwidthstring 00.00.0000
7519 \begin_inset LatexCommand \index{-\/-dumpregassign}
7528 Will create a dump of iCode's, after register assignment
7529 \begin_inset LatexCommand \index{Register assignment}
7535 <source filename>.dumprassgn.
7537 \labelwidthstring 00.00.0000
7552 \begin_inset LatexCommand \index{-\/-dumplrange}
7558 Will create a dump of the live ranges of iTemp's
7560 \labelwidthstring 00.00.0000
7575 \begin_inset LatexCommand \index{-\/-dumpall}
7586 Will cause all the above mentioned dumps to be created.
7589 Redirecting output on Windows Shells
7592 By default SDCC writes it's error messages to
7593 \begin_inset Quotes sld
7597 \begin_inset Quotes srd
7601 To force all messages to
7602 \begin_inset Quotes sld
7606 \begin_inset Quotes srd
7630 \begin_inset LatexCommand \index{-\/-use-stdout}
7635 Additionally, if you happen to have visual studio installed in your windows
7636 machine, you can use it to compile your sources using a custom build and
7652 \begin_inset LatexCommand \index{-\/-vc}
7657 Something like this should work:
7701 -model-large -c $(InputPath)
7704 Environment variables
7705 \begin_inset LatexCommand \index{Environment variables}
7712 SDCC recognizes the following environment variables:
7714 \labelwidthstring 00.00.0000
7719 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7725 SDCC installs a signal handler
7726 \begin_inset LatexCommand \index{signal handler}
7730 to be able to delete temporary files after an user break (^C) or an exception.
7731 If this environment variable is set, SDCC won't install the signal handler
7732 in order to be able to debug SDCC.
7734 \labelwidthstring 00.00.0000
7741 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7747 Path, where temporary files will be created.
7748 The order of the variables is the search order.
7749 In a standard *nix environment these variables are not set, and there's
7750 no need to set them.
7751 On Windows it's recommended to set one of them.
7753 \labelwidthstring 00.00.0000
7758 \begin_inset LatexCommand \index{SDCC\_HOME}
7765 \begin_inset LatexCommand \ref{sub:Install-paths}
7771 \begin_inset Quotes sld
7775 \begin_inset Quotes srd
7780 \labelwidthstring 00.00.0000
7785 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
7792 \begin_inset LatexCommand \ref{sub:Search-Paths}
7798 \begin_inset Quotes sld
7802 \begin_inset Quotes srd
7807 \labelwidthstring 00.00.0000
7812 \begin_inset LatexCommand \index{SDCC\_LIB}
7819 \begin_inset LatexCommand \ref{sub:Search-Paths}
7825 \begin_inset Quotes sld
7829 \begin_inset Quotes srd
7835 There are some more environment variables recognized by SDCC, but these
7836 are solely used for debugging purposes.
7837 They can change or disappear very quickly, and will never be documented.
7840 Storage Class Language Extensions
7843 MCS51/DS390 Storage Class
7844 \begin_inset LatexCommand \index{Storage class}
7851 In addition to the ANSI storage classes SDCC allows the following MCS51
7852 specific storage classes:
7853 \layout Subsubsection
7856 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
7861 \begin_inset LatexCommand \index{near (storage class)}
7872 storage class for the Small Memory model (
7880 can be used synonymously).
7881 Variables declared with this storage class will be allocated in the directly
7882 addressable portion of the internal RAM of a 8051, e.g.:
7887 data unsigned char test_data;
7890 Writing 0x01 to this variable generates the assembly code:
7895 75*00 01\SpecialChar ~
7901 \layout Subsubsection
7904 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
7909 \begin_inset LatexCommand \index{far (storage class)}
7916 Variables declared with this storage class will be placed in the external
7922 storage class for the Large Memory model, e.g.:
7927 xdata unsigned char test_xdata;
7930 Writing 0x01 to this variable generates the assembly code:
7935 90s00r00\SpecialChar ~
7964 \layout Subsubsection
7967 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
7974 Variables declared with this storage class will be allocated into the indirectly
7975 addressable portion of the internal ram of a 8051, e.g.:
7980 idata unsigned char test_idata;
7983 Writing 0x01 to this variable generates the assembly code:
8012 Please note, the first 128 byte of idata physically access the same RAM
8014 The original 8051 had 128 byte idata memory, nowadays most devices have
8015 256 byte idata memory.
8017 \begin_inset LatexCommand \index{stack}
8021 is located in idata memory.
8022 \layout Subsubsection
8025 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8032 Paged xdata access is currently not as straightforward as using the other
8033 addressing modes of a 8051.
8034 The following example writes 0x01 to the address pointed to.
8035 Please note, pdata access physically accesses xdata memory.
8036 The high byte of the address is determined by port P2
8037 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8041 (or in case of some 8051 variants by a separate Special Function Register,
8043 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8052 pdata unsigned char *test_pdata_ptr;
8064 test_pdata_ptr = (pdata *)0xfe;
8070 *test_pdata_ptr = 1;
8075 Generates the assembly code:
8080 75*01 FE\SpecialChar ~
8084 _test_pdata_ptr,#0xFE
8116 Be extremely carefull if you use pdata together with the -
8127 \begin_inset LatexCommand \index{-\/-xstack}
8132 \layout Subsubsection
8135 \begin_inset LatexCommand \index{code}
8142 'Variables' declared with this storage class will be placed in the code
8148 code unsigned char test_code;
8151 Read access to this variable generates the assembly code:
8156 90s00r6F\SpecialChar ~
8159 mov dptr,#_test_code
8188 indexed arrays of characters in code memory can be accessed efficiently:
8193 code char test_array[] = {'c','h','e','a','p'};
8196 Read access to this array using an 8-bit unsigned index generates the assembly
8213 90s00r41\SpecialChar ~
8216 mov dptr,#_test_array
8231 \layout Subsubsection
8234 \begin_inset LatexCommand \index{bit}
8241 This is a data-type and a storage class specifier.
8242 When a variable is declared as a bit, it is allocated into the bit addressable
8243 memory of 8051, e.g.:
8251 Writing 1 to this variable generates the assembly code:
8267 The bit addressable memory consists of 128 bits which are located from 0x20
8268 to 0x2f in data memory.
8272 Apart from this 8051 specific storage class most architectures support ANSI-C
8274 \begin_inset LatexCommand \index{bitfields}
8283 In accordance with ISO/IEC 9899 bits and bitfields without an explicit signed
8284 modifier are implemented as unsigned.
8293 Not really meant as examples, but nevertheless showing what bitfields are
8294 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8298 \layout Subsubsection
8301 \begin_inset LatexCommand \index{sfr}
8306 \begin_inset LatexCommand \index{sbit}
8313 Like the bit keyword,
8317 signifies both a data-type and storage class, they are used to describe
8338 variables of a 8051, eg:
8344 \begin_inset LatexCommand \index{at}
8348 0x80 P0;\SpecialChar ~
8349 /* special function register P0 at location 0x80 */
8351 sbit at 0xd7 CY; /* CY (Carry Flag
8352 \begin_inset LatexCommand \index{Flags}
8357 \begin_inset LatexCommand \index{Carry flag}
8364 Special function registers which are located on an address dividable by
8365 8 are bit-addressable, an
8369 addresses a specific bit within these sfr.
8370 \layout Subsubsection
8373 \begin_inset LatexCommand \index{Pointer}
8377 to MCS51/DS390 specific memory spaces
8380 SDCC allows (via language extensions) pointers to explicitly point to any
8381 of the memory spaces
8382 \begin_inset LatexCommand \index{Memory model}
8387 In addition to the explicit pointers, the compiler uses (by default) generic
8388 pointers which can be used to point to any of the memory spaces.
8392 Pointer declaration examples:
8397 /* pointer physically in internal ram pointing to object in external ram
8400 xdata unsigned char * data p;
8404 /* pointer physically in external ram pointing to object in internal ram
8407 data unsigned char * xdata p;
8411 /* pointer physically in code rom pointing to data in xdata space */
8413 xdata unsigned char * code p;
8417 /* pointer physically in code space pointing to data in code space */
8419 code unsigned char * code p;
8423 /* the following is a generic pointer physically located in xdata space
8429 Well you get the idea.
8434 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8447 The highest order byte of the
8451 pointers contains the data space information.
8452 Assembler support routines are called whenever data is stored or retrieved
8458 These are useful for developing reusable library
8459 \begin_inset LatexCommand \index{Libraries}
8464 Explicitly specifying the pointer type will generate the most efficient
8466 \layout Subsubsection
8468 Notes on MCS51 memory
8469 \begin_inset LatexCommand \index{MCS51 memory}
8476 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8477 RAM memory which is structured as follows:
8481 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8484 - Bytes 20-2F - 16 bytes to hold 128 bit
8485 \begin_inset LatexCommand \index{bit}
8491 - Bytes 30-7F - 80 bytes for general purpose use.
8496 Additionally some members of the MCS51 family may have up to 128 bytes of
8497 additional, indirectly addressable, internal RAM memory (
8502 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8507 Furthermore, some chips may have some built in external memory (
8512 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8516 ) which should not be confused with the internal, directly addressable RAM
8522 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8527 Sometimes this built in
8531 memory has to be activated before using it (you can probably find this
8532 information on the datasheet of the microcontroller your are using, see
8534 \begin_inset LatexCommand \ref{sub:Startup-Code}
8542 Normally SDCC will only use the first bank
8543 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8547 of registers (register bank 0), but it is possible to specify that other
8548 banks of registers should be used in interrupt
8549 \begin_inset LatexCommand \index{interrupt}
8554 By default, the compiler will place the stack after the last byte of allocated
8555 memory for variables.
8556 For example, if the first 2 banks of registers are used, and only four
8561 variables, it will position the base of the internal stack at address 20
8563 This implies that as the stack
8564 \begin_inset LatexCommand \index{stack}
8568 grows, it will use up the remaining register banks, and the 16 bytes used
8569 by the 128 bit variables, and 80 bytes for general purpose use.
8570 If any bit variables are used, the data variables will be placed after
8571 the byte holding the last bit variable.
8572 For example, if register banks 0 and 1 are used, and there are 9 bit variables
8577 variables will be placed starting at address 0x22.
8589 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
8593 to specify the start address of the
8607 -iram-size to specify the size of the total internal RAM (
8619 By default the 8051 linker will place the stack after the last byte of data
8632 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
8636 allows you to specify the start of the stack, i.e.
8637 you could start it after any data in the general purpose area.
8638 If your microcontroller has additional indirectly addressable internal
8643 ) you can place the stack on it.
8644 You may also need to use -
8655 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
8659 to set the start address of the external RAM (
8674 \begin_inset LatexCommand \index{-\/-data-loc}
8678 to specify its size.
8679 Same goes for the code memory, using -
8690 \begin_inset LatexCommand \index{-\/-data-loc}
8705 \begin_inset LatexCommand \index{-\/-data-loc}
8710 If in doubt, don't specify any options and see if the resulting memory
8711 layout is appropriate, then you can adjust it.
8714 The linker generates two files with memory allocation information.
8715 The first, with extension .map
8716 \begin_inset LatexCommand \index{<file>.map}
8720 shows all the variables and segments.
8721 The second with extension .mem
8722 \begin_inset LatexCommand \index{<file>.mem}
8726 shows the final memory layout.
8727 The linker will complain either if memory segments overlap, there is not
8728 enough memory, or there is not enough space for stack.
8729 If you get any linking warnings and/or errors related to stack or segments
8730 allocation, take a look at either the .map or .mem files to find out what
8732 The .mem file may even suggest a solution to the problem.
8735 Z80/Z180 Storage Class
8736 \begin_inset LatexCommand \index{Storage class}
8741 \layout Subsubsection
8744 \begin_inset LatexCommand \index{sfr}
8748 (in/out to 8-bit addresses)
8752 \begin_inset LatexCommand \index{Z80}
8756 family has separate address spaces for memory and
8766 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
8770 is accessed with special instructions, e.g.:
8775 sfr at 0x78 IoPort;\SpecialChar ~
8777 /* define a var in I/O space at 78h called IoPort */
8781 Writing 0x01 to this variable generates the assembly code:
8801 \layout Subsubsection
8804 \begin_inset LatexCommand \index{sfr}
8808 (in/out to 16-bit addresses)
8815 is used to support 16 bit addresses in I/O memory e.g.:
8821 \begin_inset LatexCommand \index{at}
8828 Writing 0x01 to this variable generates the assembly code:
8833 01 23 01\SpecialChar ~
8853 \layout Subsubsection
8856 \begin_inset LatexCommand \index{sfr}
8860 (in0/out0 to 8 bit addresses on Z180
8861 \begin_inset LatexCommand \index{Z180}
8866 \begin_inset LatexCommand \index{HD64180}
8873 The compiler option -
8883 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
8885 \begin_inset LatexCommand \index{\#pragma portmode}
8889 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
8899 If you include the file z180.h this will be set automatically.
8903 \begin_inset LatexCommand \index{Storage class}
8908 \layout Subsubsection
8911 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8918 The data storage class declares a variable that resides in the first 256
8919 bytes of memory (the direct page).
8920 The HC08 is most efficient at accessing variables (especially pointers)
8922 \layout Subsubsection
8925 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8932 The xdata storage class declares a variable that can reside anywhere in
8934 This is the default if no storage class is specified.
8939 \begin_inset LatexCommand \index{Absolute addressing}
8946 Data items can be assigned an absolute address with the
8949 \begin_inset LatexCommand \index{at}
8955 keyword, in addition to a storage class, e.g.:
8961 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8966 \begin_inset LatexCommand \index{at}
8970 0x7ffe unsigned int chksum;
8973 In the above example the variable chksum will located at 0x7ffe and 0x7fff
8974 of the external ram.
8979 reserve any space for variables declared in this way (they are implemented
8980 with an equate in the assembler).
8981 Thus it is left to the programmer to make sure there are no overlaps with
8982 other variables that are declared without the absolute address.
8983 The assembler listing file (.lst
8984 \begin_inset LatexCommand \index{<file>.lst}
8988 ) and the linker output files (.rst
8989 \begin_inset LatexCommand \index{<file>.rst}
8994 \begin_inset LatexCommand \index{<file>.map}
8998 ) are good places to look for such overlaps.
8999 Variables with an absolute address are
9004 \begin_inset LatexCommand \index{Variable initialization}
9011 In case of memory mapped I/O devices the keyword
9015 should be used to tell the compiler that accesses might not be optimized
9022 \begin_inset LatexCommand \index{volatile}
9027 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9032 \begin_inset LatexCommand \index{at}
9036 0x8000 unsigned char PORTA_8255;
9039 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9044 \begin_inset LatexCommand \index{Aligned array}
9051 starts at a block (256 byte) boundary
9052 \begin_inset LatexCommand \index{block boundary}
9057 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9063 Absolute addresses can be specified for variables in all storage classes,
9070 \begin_inset LatexCommand \index{bit}
9075 \begin_inset LatexCommand \index{at}
9082 The above example will allocate the variable at offset 0x02 in the bit-addressab
9084 There is no real advantage to assigning absolute addresses to variables
9085 in this manner, unless you want strict control over all the variables allocated.
9086 One possible use would be to write hardware portable code.
9087 For example, if you have a routine that uses one or more of the microcontroller
9088 I/O pins, and such pins are different for two different hardwares, you
9089 can declare the I/O pins in your routine using:
9094 extern volatile bit SDI;
9096 extern volatile bit SCLK;
9098 extern volatile bit CPOL;
9102 void DS1306_put(unsigned char value)
9110 unsigned char mask=0x80;
9134 SDI=(value & mask)?1:0;
9175 Then, someplace in the code for the first hardware you would use
9180 bit at 0x80 SDI;\SpecialChar ~
9184 /* I/O port 0, bit 0 */
9186 bit at 0x81 SCLK;\SpecialChar ~
9189 /* I/O port 0, bit 1 */
9191 bit CPOL;\SpecialChar ~
9202 /* This is a variable, let the linker allocate this one */
9205 Similarly, for the second hardware you would use
9210 bit at 0x83 SDI;\SpecialChar ~
9214 /* I/O port 0, bit 3 */
9216 bit at 0x91 SCLK;\SpecialChar ~
9219 /* I/O port 1, bit 1 */
9222 \begin_inset LatexCommand \index{bit}
9237 /* This is a variable, let the linker allocate this one */
9240 and you can use the same hardware dependent routine without changes, as
9241 for example in a library.
9242 This is somehow similar to sbit, but only one absolute address has to be
9243 specified in the whole project.
9247 \begin_inset LatexCommand \index{Parameters}
9252 \begin_inset LatexCommand \index{function parameter}
9257 \begin_inset LatexCommand \index{local variables}
9264 Automatic (local) variables and parameters to functions can either be placed
9265 on the stack or in data-space.
9266 The default action of the compiler is to place these variables in the internal
9267 RAM (for small model) or external RAM (for large model).
9268 This in fact makes them similar to
9271 \begin_inset LatexCommand \index{static}
9277 so by default functions are non-reentrant
9278 \begin_inset LatexCommand \index{reentrant}
9287 They can be placed on the stack
9288 \begin_inset LatexCommand \index{stack}
9305 \begin_inset LatexCommand \index{-\/-stack-auto}
9311 option or by using the
9314 \begin_inset LatexCommand \index{reentrant}
9320 keyword in the function declaration, e.g.:
9325 unsigned char foo(char i) reentrant
9339 Since stack space on 8051 is limited, the
9357 option should be used sparingly.
9358 Note that the reentrant keyword just means that the parameters & local
9359 variables will be allocated to the stack, it
9363 mean that the function is register bank
9364 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9373 \begin_inset LatexCommand \index{local variables}
9377 can be assigned storage classes and absolute
9378 \begin_inset LatexCommand \index{Absolute addressing}
9395 xdata unsigned char i;
9408 \begin_inset LatexCommand \index{at}
9412 0x31 unsigned char j;
9424 In the above example the variable
9428 will be allocated in the external ram,
9432 in bit addressable space and
9451 or when a function is declared as
9455 this should only be done for static variables.
9459 \begin_inset LatexCommand \index{function parameter}
9463 however are not allowed any storage class
9464 \begin_inset LatexCommand \index{Storage class}
9468 , (storage classes for parameters will be ignored), their allocation is
9469 governed by the memory model in use, and the reentrancy options.
9473 \begin_inset LatexCommand \label{sub:Overlaying}
9478 \begin_inset LatexCommand \index{Overlaying}
9486 \begin_inset LatexCommand \index{reentrant}
9490 functions SDCC will try to reduce internal ram space usage by overlaying
9491 parameters and local variables of a function (if possible).
9492 Parameters and local variables
9493 \begin_inset LatexCommand \index{local variables}
9497 of a function will be allocated to an overlayable segment if the function
9500 no other function calls and the function is non-reentrant and the memory
9502 \begin_inset LatexCommand \index{Memory model}
9509 If an explicit storage class
9510 \begin_inset LatexCommand \index{Storage class}
9514 is specified for a local variable, it will NOT be overlayed.
9517 Note that the compiler (not the linkage editor) makes the decision for overlayin
9519 Functions that are called from an interrupt service routine should be preceded
9520 by a #pragma\SpecialChar ~
9522 \begin_inset LatexCommand \index{\#pragma nooverlay}
9526 if they are not reentrant.
9529 Also note that the compiler does not do any processing of inline assembler
9530 code, so the compiler might incorrectly assign local variables and parameters
9531 of a function into the overlay segment if the inline assembler code calls
9532 other c-functions that might use the overlay.
9533 In that case the #pragma\SpecialChar ~
9534 nooverlay should be used.
9537 Parameters and local variables of functions that contain 16 or 32 bit multiplica
9539 \begin_inset LatexCommand \index{Multiplication}
9544 \begin_inset LatexCommand \index{Division}
9548 will NOT be overlayed since these are implemented using external functions,
9557 \begin_inset LatexCommand \index{\#pragma nooverlay}
9563 void set_error(unsigned char errcd)
9579 void some_isr () interrupt
9580 \begin_inset LatexCommand \index{interrupt}
9610 In the above example the parameter
9618 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
9620 not present, this could cause unpredictable runtime behavior when called
9621 from an interrupt service routine.
9622 The #pragma\SpecialChar ~
9623 nooverlay ensures that the parameters and local variables for
9624 the function are NOT overlayed.
9627 Interrupt Service Routines
9628 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
9650 outines to be coded in C, with some extended keywords.
9655 void timer_isr (void) interrupt 1 using 1
9669 The optional number following the
9672 \begin_inset LatexCommand \index{interrupt}
9678 keyword is the interrupt number this routine will service.
9679 When present, the compiler will insert a call to this routine in the interrupt
9680 vector table for the interrupt number specified.
9681 If you have multiple source files in your project, interrupt service routines
9682 can be present in any of them, but a prototype of the isr MUST be present
9683 or included in the file that contains the function
9692 keyword can be used to tell the compiler to use the specified register
9693 bank (8051 specific) when generating code for this function.
9699 Interrupt service routines open the door for some very interesting bugs:
9703 If the interrupt service routines changes variables which are accessed by
9704 other functions these variables should be declared
9709 \begin_inset LatexCommand \index{volatile}
9717 If the access to these variables is not
9720 \begin_inset LatexCommand \index{atomic access}
9727 the processor needs more than one instruction for the access and could
9728 be interrupted while accessing the variable) the interrupt must disabled
9729 during the access to avoid inconsistent data.
9730 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
9731 and should be protected by disabling interrupts.
9732 You're not automatically on the safe side if you use 8 bit variables though.
9733 We need an example here: f.e.
9734 on the 8051 the harmless looking
9735 \begin_inset Quotes srd
9745 \begin_inset Quotes sld
9754 \begin_inset Quotes srd
9764 \begin_inset Quotes sld
9767 from within an interrupt routine might get lost if the interrupt occurs
9770 \begin_inset Quotes sld
9775 counter\SpecialChar ~
9780 \begin_inset Quotes srd
9783 is not atomic on the 8051 even if
9787 is located in data memory.
9788 Bugs like these are hard to reproduce and can cause a lot of trouble.
9792 A special note here, int (16 bit) and long (32 bit) integer division
9793 \begin_inset LatexCommand \index{Division}
9798 \begin_inset LatexCommand \index{Multiplication}
9803 \begin_inset LatexCommand \index{Modulus}
9808 \begin_inset LatexCommand \index{Floating point support}
9812 operations are implemented using external support routines developed in
9814 If an interrupt service routine needs to do any of these operations then
9815 the support routines (as mentioned in a following section) will have to
9816 be recompiled using the
9829 \begin_inset LatexCommand \index{-\/-stack-auto}
9835 option and the source file will need to be compiled using the
9850 \begin_inset LatexCommand \index{-\/-int-long-reent}
9857 Calling other functions from an interrupt service routine is not recommended,
9858 avoid it if possible.
9859 Note that when some function is called from an interrupt service routine
9860 it should be preceded by a #pragma\SpecialChar ~
9862 \begin_inset LatexCommand \index{\#pragma nooverlay}
9866 if it is not reentrant.
9867 Furthermore nonreentrant functions should not be called from the main program
9868 while the interrupt service routine might be active.
9874 \begin_inset LatexCommand \ref{sub:Overlaying}
9879 about Overlaying and section
9880 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
9885 about Functions using private register banks.
9888 MCS51/DS390 Interrupt Service Routines
9891 Interrupt numbers and the corresponding address & descriptions for the Standard
9892 8051/8052 are listed below.
9893 SDCC will automatically adjust the interrupt vector table to the maximum
9894 interrupt number specified.
9900 \begin_inset Tabular
9901 <lyxtabular version="3" rows="7" columns="3">
9903 <column alignment="center" valignment="top" leftline="true" width="0in">
9904 <column alignment="center" valignment="top" leftline="true" width="0in">
9905 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
9906 <row topline="true" bottomline="true">
9907 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9932 <row topline="true">
9933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9941 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9949 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9958 <row topline="true">
9959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
9984 <row topline="true">
9985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
9993 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10001 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10010 <row topline="true">
10011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10019 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10027 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10036 <row topline="true">
10037 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10045 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10053 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10062 <row topline="true" bottomline="true">
10063 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10071 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10079 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10097 If the interrupt service routine is defined without
10100 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10106 a register bank or with register bank 0 (
10110 0), the compiler will save the registers used by itself on the stack upon
10111 entry and restore them at exit, however if such an interrupt service routine
10112 calls another function then the entire register bank will be saved on the
10114 This scheme may be advantageous for small interrupt service routines which
10115 have low register usage.
10118 If the interrupt service routine is defined to be using a specific register
10123 & psw are saved and restored, if such an interrupt service routine calls
10124 another function (using another register bank) then the entire register
10125 bank of the called function will be saved on the stack.
10126 This scheme is recommended for larger interrupt service routines.
10129 HC08 Interrupt Service Routines
10132 Since the number of interrupts available is chip specific and the interrupt
10133 vector table always ends at the last byte of memory, the interrupt numbers
10134 corresponds to the interrupt vectors in reverse order of address.
10135 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10136 2 will use the interrupt vector at 0xfffa, and so on.
10137 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10138 this way; instead see section
10139 \begin_inset LatexCommand \ref{sub:Startup-Code}
10143 for details on customizing startup.
10146 Enabling and Disabling Interrupts
10149 Critical Functions and Critical Statements
10152 A special keyword may be associated with a block or a function declaring
10158 SDCC will generate code to disable all interrupts
10159 \begin_inset LatexCommand \index{interrupt}
10163 upon entry to a critical function and restore the interrupt enable to the
10164 previous state before returning.
10165 Nesting critical functions will need one additional byte on the stack
10166 \begin_inset LatexCommand \index{stack}
10175 int foo () critical
10176 \begin_inset LatexCommand \index{critical}
10201 The critical attribute maybe used with other attributes like
10211 may also be used to disable interrupts more locally:
10219 More than one statement could have been included in the block.
10222 Enabling and Disabling Interrupts directly
10226 \begin_inset LatexCommand \index{interrupt}
10230 can also be disabled and enabled directly (8051):
10235 EA = 0;\SpecialChar ~
10298 EA = 1;\SpecialChar ~
10365 On other architectures which have seperate opcodes for enabling and disabling
10366 interrupts you might want to make use of defines with inline assembly
10367 \begin_inset LatexCommand \index{Assembler routines}
10377 \begin_inset LatexCommand \index{\_asm}
10386 \begin_inset LatexCommand \index{\_endasm}
10395 #define SEI _asm\SpecialChar ~
10407 Note: it is sometimes sufficient to disable only a specific interrupt source
10409 a timer or serial interrupt by manipulating an
10412 \begin_inset LatexCommand \index{interrupt mask}
10422 Usually the time during which interrupts are disabled should be kept as
10424 This minimizes both
10429 \begin_inset LatexCommand \index{interrupt latency}
10433 (the time between the occurrence of the interrupt and the execution of
10434 the first code in the interrupt routine) and
10439 \begin_inset LatexCommand \index{interrupt jitter}
10443 (the difference between the shortest and the longest interrupt latency).
10444 These really are something different, f.e.
10445 a serial interrupt has to be served before its buffer overruns so it cares
10446 for the maximum interrupt latency, whereas it does not care about jitter.
10447 On a loudspeaker driven via a digital to analog converter which is fed
10448 by an interrupt a latency of a few milliseconds might be tolerable, whereas
10449 a much smaller jitter will be very audible.
10452 You can reenable interrupts within an interrupt routine and on some architecture
10453 s you can make use of two (or more) levels of
10455 interrupt priorities
10458 \begin_inset LatexCommand \index{interrupt priority}
10463 On some architectures which don't support interrupt priorities these can
10464 be implemented by manipulating the interrupt mask and reenabling interrupts
10465 within the interrupt routine.
10466 Don't add complexity unless you have to.
10470 Functions using private register banks
10471 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
10478 Some architectures have support for quickly changing register sets.
10479 SDCC supports this feature with the
10482 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10488 attribute (which tells the compiler to use a register bank
10489 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10493 other than the default bank zero).
10494 It should only be applied to
10497 \begin_inset LatexCommand \index{interrupt}
10503 functions (see footnote below).
10504 This will in most circumstances make the generated ISR code more efficient
10505 since it will not have to save registers on the stack.
10512 attribute will have no effect on the generated code for a
10516 function (but may occasionally be useful anyway
10522 possible exception: if a function is called ONLY from 'interrupt' functions
10523 using a particular bank, it can be declared with the same 'using' attribute
10524 as the calling 'interrupt' functions.
10525 For instance, if you have several ISRs using bank one, and all of them
10526 call memcpy(), it might make sense to create a specialized version of memcpy()
10527 'using 1', since this would prevent the ISR from having to save bank zero
10528 to the stack on entry and switch to bank zero before calling the function
10535 (pending: I don't think this has been done yet)
10542 function using a non-zero bank will assume that it can trash that register
10543 bank, and will not save it.
10544 Since high-priority interrupts
10545 \begin_inset LatexCommand \index{interrupt priority}
10549 can interrupt low-priority ones on the 8051 and friends, this means that
10550 if a high-priority ISR
10554 a particular bank occurs while processing a low-priority ISR
10558 the same bank, terrible and bad things can happen.
10559 To prevent this, no single register bank should be
10563 by both a high priority and a low priority ISR.
10564 This is probably most easily done by having all high priority ISRs use
10565 one bank and all low priority ISRs use another.
10566 If you have an ISR which can change priority at runtime, you're on your
10567 own: I suggest using the default bank zero and taking the small performance
10571 It is most efficient if your ISR calls no other functions.
10572 If your ISR must call other functions, it is most efficient if those functions
10573 use the same bank as the ISR (see note 1 below); the next best is if the
10574 called functions use bank zero.
10575 It is very inefficient to call a function using a different, non-zero bank
10581 \begin_inset LatexCommand \label{sub:Startup-Code}
10586 \begin_inset LatexCommand \index{Startup code}
10593 MCS51/DS390 Startup Code
10596 The compiler inserts a call to the C routine
10598 _sdcc_external_startup()
10599 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
10608 at the start of the CODE area.
10609 This routine is in the runtime library
10610 \begin_inset LatexCommand \index{Runtime library}
10615 By default this routine returns 0, if this routine returns a non-zero value,
10616 the static & global variable initialization will be skipped and the function
10617 main will be invoked.
10618 Otherwise static & global variables will be initialized before the function
10622 _sdcc_external_startup()
10624 routine to your program to override the default if you need to setup hardware
10625 or perform some other critical operation prior to static & global variable
10627 On some mcs51 variants xdata has to be explicitly enabled before it can
10628 be accessed, this is the place to do it.
10629 See also the compiler option
10648 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
10653 \begin_inset LatexCommand \ref{sub:MCS51-variants}
10658 about MCS51-variants.
10664 The HC08 startup code follows the same scheme as the MCS51 startup code.
10670 On the Z80 the startup code is inserted by linking with crt0.o which is generated
10671 from sdcc/device/lib/z80/crt0.s.
10672 If you need a different startup code you can use the compiler option
10693 \begin_inset LatexCommand \index{-\/-no-std-crt0}
10697 and provide your own crt0.o.
10701 Inline Assembler Code
10702 \begin_inset LatexCommand \index{Assembler routines}
10709 A Step by Step Introduction
10710 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
10717 Starting from a small snippet of c-code this example shows for the MCS51
10718 how to use inline assembly, access variables, a function parameter and
10719 an array in xdata memory.
10720 The example uses an MCS51 here but is easily adapted for other architectures.
10721 This is a buffer routine which should be optimized:
10728 \begin_inset LatexCommand \index{far (storage class)}
10733 \begin_inset LatexCommand \index{at}
10738 \begin_inset LatexCommand \index{Aligned array}
10744 unsigned char head,tail;
10748 void to_buffer( unsigned char c )
10756 if( head != tail-1 )
10771 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
10772 then a corresponding buffer.asm file is generated.
10773 We define a new function
10777 in file buffer.c in which we cut and paste the generated code, removing
10778 unwanted comments and some ':'.
10780 \begin_inset Quotes sld
10784 \begin_inset Quotes srd
10788 \begin_inset Quotes sld
10792 \begin_inset Quotes srd
10795 to the beginning and the end of the function body:
10801 /* With a cut and paste from the .asm file, we have something to start with.
10806 The function is not yet OK! (registers aren't saved) */
10808 void to_buffer_asm( unsigned char c )
10817 \begin_inset LatexCommand \index{\_asm}
10831 ;buffer.c if( head != tail-1 )
10879 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
10880 \begin_inset LatexCommand \index{Aligned array}
10949 The new file buffer.c should compile with only one warning about the unreferenced
10950 function argument 'c'.
10951 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
10952 (1) and finally have:
10958 unsigned char far at 0x7f00 buf[0x100];
10960 unsigned char head,tail;
10962 #define USE_ASSEMBLY (1)
10970 void to_buffer( unsigned char c )
10978 if( head != tail-1 )
10998 void to_buffer( unsigned char c )
11006 c; // to avoid warning: unreferenced function argument
11013 \begin_inset LatexCommand \index{\_asm}
11027 ; save used registers here.
11038 ; If we were still using r2,r3 we would have to push them here.
11041 ; if( head != tail-1 )
11084 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11108 ; buf[ head++ ] = c;
11119 a,dpl \SpecialChar ~
11126 ; dpl holds lower byte of function argument
11137 dpl,_head \SpecialChar ~
11140 ; buf is 0x100 byte aligned so head can be used directly
11182 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11194 ; restore used registers here
11207 The inline assembler code can contain any valid code understood by the assembler
11208 , this includes any assembler directives and comment lines
11214 The assembler does not like some characters like ':' or ''' in comments.
11215 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11219 The compiler does not do any validation of the code within the
11222 \begin_inset LatexCommand \index{\_asm}
11230 Specifically it will not know which registers are used and thus register
11232 \begin_inset LatexCommand \index{push/pop}
11236 has to be done manually.
11240 It is recommended that each assembly instruction (including labels) be placed
11241 in a separate line (as the example shows).
11255 \begin_inset LatexCommand \index{-\/-peep-asm}
11261 command line option is used, the inline assembler code will be passed through
11262 the peephole optimizer
11263 \begin_inset LatexCommand \index{Peephole optimizer}
11268 There are only a few (if any) cases where this option makes sense, it might
11269 cause some unexpected changes in the inline assembler code.
11270 Please go through the peephole optimizer rules defined in file
11274 before using this option.
11278 \begin_inset LatexCommand \label{sub:Naked-Functions}
11283 \begin_inset LatexCommand \index{Naked functions}
11290 A special keyword may be associated with a function declaring it as
11293 \begin_inset LatexCommand \index{\_naked}
11304 function modifier attribute prevents the compiler from generating prologue
11305 \begin_inset LatexCommand \index{function prologue}
11310 \begin_inset LatexCommand \index{function epilogue}
11314 code for that function.
11315 This means that the user is entirely responsible for such things as saving
11316 any registers that may need to be preserved, selecting the proper register
11317 bank, generating the
11321 instruction at the end, etc.
11322 Practically, this means that the contents of the function must be written
11323 in inline assembler.
11324 This is particularly useful for interrupt functions, which can have a large
11325 (and often unnecessary) prologue/epilogue.
11326 For example, compare the code generated by these two functions:
11332 \begin_inset LatexCommand \index{volatile}
11336 data unsigned char counter;
11340 void simpleInterrupt(void) interrupt
11341 \begin_inset LatexCommand \index{interrupt}
11359 void nakedInterrupt(void) interrupt 2 _naked
11368 \begin_inset LatexCommand \index{\_asm}
11385 _counter ; does not change flags, no need to save psw
11397 ; MUST explicitly include ret or reti in _naked function.
11404 \begin_inset LatexCommand \index{\_endasm}
11413 For an 8051 target, the generated simpleInterrupt looks like:
11554 whereas nakedInterrupt looks like:
11569 _counter ; does not change flags, no need to save psw
11587 ; MUST explicitly include ret or reti in _naked function
11590 The related directive #pragma exclude
11591 \begin_inset LatexCommand \index{\#pragma exclude}
11595 allows a more fine grained control over pushing & popping
11596 \begin_inset LatexCommand \index{push/pop}
11603 While there is nothing preventing you from writing C code inside a
11607 function, there are many ways to shoot yourself in the foot doing this,
11608 and it is recommended that you stick to inline assembler.
11611 Use of Labels within Inline Assembler
11614 SDCC allows the use of in-line assembler with a few restrictions regarding
11616 In older versions of the compiler all labels defined within inline assembler
11625 where nnnn is a number less than 100 (which implies a limit of utmost 100
11626 inline assembler labels
11640 \begin_inset LatexCommand \index{\_asm}
11670 \begin_inset LatexCommand \index{\_endasm}
11677 Inline assembler code cannot reference any C-Labels, however it can reference
11679 \begin_inset LatexCommand \index{Labels}
11683 defined by the inline assembler, e.g.:
11708 ; some assembler code
11728 /* some more c code */
11730 clabel:\SpecialChar ~
11732 /* inline assembler cannot reference this label */
11744 $0003: ;label (can be referenced by inline assembler only)
11756 /* some more c code */
11761 In other words inline assembly code can access labels defined in inline
11762 assembly within the scope of the function.
11763 The same goes the other way, i.e.
11764 labels defines in inline assembly can not be accessed by C statements.
11767 Interfacing with Assembler Code
11768 \begin_inset LatexCommand \index{Assembler routines}
11775 Global Registers used for Parameter Passing
11776 \begin_inset LatexCommand \index{Parameter passing}
11783 The compiler always uses the global registers
11786 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
11791 \begin_inset LatexCommand \index{DPTR}
11796 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
11805 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
11811 to pass the first parameter to a routine.
11812 The second parameter onwards is either allocated on the stack (for reentrant
11823 -stack-auto is used) or in data / xdata memory (depending on the memory
11828 Assembler Routine (non-reentrant)
11831 In the following example
11832 \begin_inset LatexCommand \index{reentrant}
11837 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
11841 the function c_func calls an assembler routine asm_func, which takes two
11843 \begin_inset LatexCommand \index{function parameter}
11852 extern int asm_func(unsigned char, unsigned char);
11856 int c_func (unsigned char i, unsigned char j)
11864 return asm_func(i,j);
11878 return c_func(10,9);
11883 The corresponding assembler function is:
11888 .globl _asm_func_PARM_2
11989 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12006 Note here that the return values
12007 \begin_inset LatexCommand \index{return value}
12011 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12013 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12014 b' & 'acc' for four byte values.
12017 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12018 the parameter number starting from 1, and counting from the left.
12019 The first parameter is passed in
12020 \begin_inset Quotes eld
12024 \begin_inset Quotes erd
12027 for a one byte parameter,
12028 \begin_inset Quotes eld
12032 \begin_inset Quotes erd
12036 \begin_inset Quotes eld
12040 \begin_inset Quotes erd
12043 for three bytes and
12044 \begin_inset Quotes eld
12048 \begin_inset Quotes erd
12051 for a four bytes parameter.
12052 The variable name for the second parameter will be _<function_name>_PARM_2.
12056 Assemble the assembler routine with the following command:
12063 asx8051 -losg asmfunc.asm
12070 Then compile and link the assembler routine to the C source file with the
12078 sdcc cfunc.c asmfunc.rel
12081 Assembler Routine (reentrant)
12085 \begin_inset LatexCommand \index{reentrant}
12090 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12094 the second parameter
12095 \begin_inset LatexCommand \index{function parameter}
12099 onwards will be passed on the stack, the parameters are pushed from right
12101 after the call the leftmost parameter will be on the top of the stack.
12102 Here is an example:
12107 extern int asm_func(unsigned char, unsigned char);
12111 int c_func (unsigned char i, unsigned char j) reentrant
12119 return asm_func(i,j);
12133 return c_func(10,9);
12138 The corresponding assembler routine is:
12238 The compiling and linking procedure remains the same, however note the extra
12239 entry & exit linkage required for the assembler code, _bp is the stack
12240 frame pointer and is used to compute the offset into the stack for parameters
12241 and local variables.
12245 \begin_inset LatexCommand \index{int (16 bit)}
12250 \begin_inset LatexCommand \index{long (32 bit)}
12257 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12258 multiplication and modulus operations are implemented by support routines.
12259 These support routines are all developed in ANSI-C to facilitate porting
12260 to other MCUs, although some model specific assembler optimizations are
12262 The following files contain the described routines, all of them can be
12263 found in <installdir>/share/sdcc/lib.
12269 \begin_inset Tabular
12270 <lyxtabular version="3" rows="11" columns="2">
12272 <column alignment="center" valignment="top" leftline="true" width="0">
12273 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12274 <row topline="true" bottomline="true">
12275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12296 <row topline="true">
12297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12310 16 bit multiplication
12314 <row topline="true">
12315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12323 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12328 signed 16 bit division (calls _divuint)
12332 <row topline="true">
12333 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12341 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12346 unsigned 16 bit division
12350 <row topline="true">
12351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12364 signed 16 bit modulus (calls _moduint)
12368 <row topline="true">
12369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12377 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12382 unsigned 16 bit modulus
12386 <row topline="true">
12387 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12395 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12400 32 bit multiplication
12404 <row topline="true">
12405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12413 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12418 signed 32 division (calls _divulong)
12422 <row topline="true">
12423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12436 unsigned 32 division
12440 <row topline="true">
12441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12454 signed 32 bit modulus (calls _modulong)
12458 <row topline="true" bottomline="true">
12459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12472 unsigned 32 bit modulus
12485 Since they are compiled as
12490 \begin_inset LatexCommand \index{reentrant}
12495 \begin_inset LatexCommand \index{interrupt}
12499 service routines should not do any of the above operations.
12500 If this is unavoidable then the above routines will need to be compiled
12514 \begin_inset LatexCommand \index{-\/-stack-auto}
12520 option, after which the source program will have to be compiled with
12533 \begin_inset LatexCommand \index{-\/-int-long-reent}
12540 Notice that you don't have to call these routines directly.
12541 The compiler will use them automatically every time an integer operation
12545 Floating Point Support
12546 \begin_inset LatexCommand \index{Floating point support}
12553 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
12554 floating point support routines are derived from gcc's floatlib.c and consist
12555 of the following routines:
12563 \begin_inset Tabular
12564 <lyxtabular version="3" rows="17" columns="2">
12566 <column alignment="center" valignment="top" leftline="true" width="0">
12567 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12568 <row topline="true" bottomline="true">
12569 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12595 <row topline="true">
12596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12613 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12627 add floating point numbers
12631 <row topline="true">
12632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12649 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12663 subtract floating point numbers
12667 <row topline="true">
12668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12685 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12699 divide floating point numbers
12703 <row topline="true">
12704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12735 multiply floating point numbers
12739 <row topline="true">
12740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12771 convert floating point to unsigned char
12775 <row topline="true">
12776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12793 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12807 convert floating point to signed char
12811 <row topline="true">
12812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12843 convert floating point to unsigned int
12847 <row topline="true">
12848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12865 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12879 convert floating point to signed int
12883 <row topline="true">
12884 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12910 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12924 convert floating point to unsigned long
12928 <row topline="true">
12929 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12946 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12960 convert floating point to signed long
12964 <row topline="true">
12965 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12982 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12996 convert unsigned char to floating point
13000 <row topline="true">
13001 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13032 convert char to floating point number
13036 <row topline="true">
13037 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13068 convert unsigned int to floating point
13072 <row topline="true">
13073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13104 convert int to floating point numbers
13108 <row topline="true">
13109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13140 convert unsigned long to floating point number
13144 <row topline="true" bottomline="true">
13145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13162 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13176 convert long to floating point number
13189 These support routines are developed in ANSI-C so there is room for space
13190 and speed improvement.
13191 Note if all these routines are used simultaneously the data space might
13193 For serious floating point usage it is recommended that the large model
13195 Also notice that you don't have to call this routines directly.
13196 The compiler will use them automatically every time a floating point operation
13201 \begin_inset LatexCommand \index{Libraries}
13210 <pending: this is messy and incomplete - a little more information is in
13211 sdcc/doc/libdoc.txt
13216 Compiler support routines (_gptrget, _mulint etc.)
13219 Stdclib functions (puts, printf, strcat etc.)
13220 \layout Subsubsection
13226 \begin_inset LatexCommand \index{<stdio.h>}
13230 As usual on embedded systems you have to provide your own
13233 \begin_inset LatexCommand \index{getchar()}
13242 \begin_inset LatexCommand \index{putchar()}
13249 SDCC does not know whether the system connects to a serial line with or
13250 without handshake, LCD, keyboard or other device.
13251 You'll find examples for serial routines f.e.
13252 in sdcc/device/lib.
13255 If you're short on memory you might want to use
13266 \begin_inset LatexCommand \index{printf()}
13273 For the mcs51 there is an assembly version
13277 which should fit the requirements of many embedded systems (by unsetting
13278 #defines it can be customized to
13282 support long variables and field widths).
13285 Math functions (sin, pow, sqrt etc.)
13292 \begin_inset LatexCommand \index{Libraries}
13296 included in SDCC should have a license at least as liberal as the GNU Lesser
13297 General Public License
13298 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
13309 license statements for the libraries are missing.
13310 sdcc/device/lib/ser_ir.c
13314 come with a GPL (as opposed to LGPL) License - this will not be liberal
13315 enough for many embedded programmers.
13318 If you have ported some library or want to share experience about some code
13320 falls into any of these categories Busses (I
13321 \begin_inset Formula $^{\textrm{2}}$
13324 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
13325 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
13326 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
13327 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
13332 would certainly like to hear about it.
13333 Programmers coding for embedded systems are not especially famous for being
13334 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
13335 e these references are very valuable.
13336 Let's help to create a climate where information is shared.
13342 MCS51 Memory Models
13343 \begin_inset LatexCommand \index{Memory model}
13348 \begin_inset LatexCommand \index{MCS51 memory model}
13353 \layout Subsubsection
13358 SDCC allows two memory models for MCS51 code,
13367 Modules compiled with different memory models should
13371 be combined together or the results would be unpredictable.
13372 The library routines supplied with the compiler are compiled as both small
13374 The compiled library modules are contained in separate directories as small
13375 and large so that you can link to either set.
13379 When the large model is used all variables declared without a storage class
13380 will be allocated into the external ram, this includes all parameters and
13381 local variables (for non-reentrant
13382 \begin_inset LatexCommand \index{reentrant}
13387 When the small model is used variables without storage class are allocated
13388 in the internal ram.
13391 Judicious usage of the processor specific storage classes
13392 \begin_inset LatexCommand \index{Storage class}
13396 and the 'reentrant' function type will yield much more efficient code,
13397 than using the large model.
13398 Several optimizations are disabled when the program is compiled using the
13399 large model, it is therefore recommended that the small model be used unless
13400 absolutely required.
13401 \layout Subsubsection
13404 \begin_inset LatexCommand \label{sub:External-Stack}
13409 \begin_inset LatexCommand \index{stack}
13414 \begin_inset LatexCommand \index{External stack (mcs51)}
13425 : this option wasn't maintained for a long time and is quite buggy.
13426 Small programs might work.
13427 You've been warned!
13430 The external stack (-
13441 \begin_inset LatexCommand \index{-\/-xstack}
13445 ) is located in pdata
13446 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
13450 memory (usually at the start of the external ram segment) and is 256 bytes
13462 -xstack option is used to compile the program, the parameters and local
13464 \begin_inset LatexCommand \index{local variables}
13468 of all reentrant functions are allocated in this area.
13469 This option is provided for programs with large stack space requirements.
13470 When used with the -
13481 \begin_inset LatexCommand \index{-\/-stack-auto}
13485 option, all parameters and local variables are allocated on the external
13486 stack (note: support libraries will need to be recompiled with the same
13490 The compiler outputs the higher order address byte of the external ram segment
13492 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
13497 \begin_inset LatexCommand \ref{sub:MCS51-variants}
13501 ), therefore when using the External Stack option, this port
13505 be used by the application program.
13509 \begin_inset LatexCommand \index{Memory model}
13514 \begin_inset LatexCommand \index{DS390 memory model}
13521 The only model supported is Flat 24
13522 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
13527 This generates code for the 24 bit contiguous addressing mode of the Dallas
13529 In this mode, up to four meg of external RAM or code space can be directly
13531 See the data sheets at www.dalsemi.com for further information on this part.
13535 Note that the compiler does not generate any code to place the processor
13536 into 24 bitmode (although
13540 in the ds390 libraries will do that for you).
13546 \begin_inset LatexCommand \index{Tinibios (DS390)}
13550 , the boot loader or similar code must ensure that the processor is in 24
13551 bit contiguous addressing mode before calling the SDCC startup code.
13569 option, variables will by default be placed into the XDATA segment.
13574 Segments may be placed anywhere in the 4 meg address space using the usual
13586 Note that if any segments are located above 64K, the -r flag must be passed
13587 to the linker to generate the proper segment relocations, and the Intel
13588 HEX output format must be used.
13589 The -r flag can be passed to the linker by using the option
13593 on the SDCC command line.
13594 However, currently the linker can not handle code segments > 64k.
13598 \begin_inset LatexCommand \index{Pragmas}
13605 SDCC supports the following #pragma directives:
13609 \begin_inset LatexCommand \index{\#pragma save}
13613 - this will save all current options to the save/restore stack.
13618 \begin_inset LatexCommand \index{\#pragma restore}
13622 - will restore saved options from the last save.
13623 saves & restores can be nested.
13624 SDCC uses a save/restore stack: save pushes current options to the stack,
13625 restore pulls current options from the stack.
13630 \begin_inset LatexCommand \index{\#pragma nogcse}
13634 - will stop global common subexpression elimination.
13638 \begin_inset LatexCommand \index{\#pragma noinduction}
13642 - will stop loop induction optimizations.
13646 \begin_inset LatexCommand \index{\#pragma nojtbound}
13650 - will not generate code for boundary value checking, when switch statements
13651 are turned into jump-tables (dangerous).
13652 For more details see section
13653 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
13661 \begin_inset LatexCommand \index{\#pragma nooverlay}
13665 - the compiler will not overlay the parameters and local variables of a
13670 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
13674 - the compiler will not warn you anymore for obvious mistakes, you'r on
13679 \begin_inset LatexCommand \index{\#pragma noloopreverse}
13683 - Will not do loop reversal optimization
13687 \begin_inset LatexCommand \index{\#pragma exclude}
13691 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables generation of
13693 \begin_inset LatexCommand \index{push/pop}
13697 instruction in ISR function (using interrupt
13698 \begin_inset LatexCommand \index{interrupt}
13703 The directive should be placed immediately before the ISR function definition
13704 and it affects ALL ISR functions following it.
13705 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
13706 exclude\SpecialChar ~
13708 \begin_inset LatexCommand \index{\#pragma exclude}
13716 \begin_inset LatexCommand \index{\#pragma noiv}
13720 - Do not generate interrupt
13721 \begin_inset LatexCommand \index{interrupt}
13725 vector table entries for all ISR functions defined after the pragma.
13726 This is useful in cases where the interrupt vector table must be defined
13727 manually, or when there is a secondary, manually defined interrupt vector
13729 for the autovector feature of the Cypress EZ-USB FX2).
13730 More elegantly this can be achieved by obmitting the optional interrupt
13731 number after the interrupt keyword, see section
13732 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
13741 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13746 \begin_inset LatexCommand \index{function prologue}
13750 function1[,function2[,function3...]] - The compiler by default uses a caller
13751 saves convention for register saving across function calls, however this
13752 can cause unnecessary register pushing & popping
13753 \begin_inset LatexCommand \index{push/pop}
13757 when calling small functions from larger functions.
13758 This option can be used to switch off the register saving convention for
13759 the function names specified.
13760 The compiler will not save registers when calling these functions, extra
13761 code need to be manually inserted at the entry & exit for these functions
13762 to save & restore the registers used by these functions, this can SUBSTANTIALLY
13763 reduce code & improve run time performance of the generated code.
13764 In the future the compiler (with inter procedural analysis) may be able
13765 to determine the appropriate scheme to use for each function call.
13776 -callee-saves command line option is used, the function names specified
13777 in #pragma\SpecialChar ~
13779 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13783 is appended to the list of functions specified in the command line.
13786 SDCPP supports the following #pragma directives:
13790 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
13794 (+ | -) - switch _asm _endasm block preprocessing on / off.
13798 The pragma's are intended to be used to turn-on or off certain optimizations
13799 which might cause the compiler to generate extra stack / data space to
13800 store compiler generated temporary variables.
13801 This usually happens in large functions.
13802 Pragma directives should be used as shown in the following example, they
13803 are used to control options & optimizations for a given function; pragmas
13804 should be placed before and/or after a function, placing pragma's inside
13805 a function body could have unpredictable results.
13811 \begin_inset LatexCommand \index{\#pragma save}
13822 /* save the current settings */
13825 \begin_inset LatexCommand \index{\#pragma nogcse}
13834 /* turnoff global subexpression elimination */
13836 #pragma noinduction
13837 \begin_inset LatexCommand \index{\#pragma noinduction}
13841 /* turn off induction optimizations */
13864 \begin_inset LatexCommand \index{\#pragma restore}
13868 /* turn the optimizations back on */
13871 The compiler will generate a warning message when extra space is allocated.
13872 It is strongly recommended that the save and restore pragma's be used when
13873 changing options for a function.
13876 Defines Created by the Compiler
13879 The compiler creates the following #defines
13880 \begin_inset LatexCommand \index{\#defines}
13885 \begin_inset LatexCommand \index{Defines created by the compiler}
13895 \begin_inset Tabular
13896 <lyxtabular version="3" rows="10" columns="2">
13898 <column alignment="center" valignment="top" leftline="true" width="0">
13899 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13900 <row topline="true" bottomline="true">
13901 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13922 <row topline="true">
13923 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13929 \begin_inset LatexCommand \index{SDCC}
13936 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13941 this Symbol is always defined
13945 <row topline="true">
13946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13952 \begin_inset LatexCommand \index{SDCC\_mcs51}
13957 \begin_inset LatexCommand \index{SDCC\_ds390}
13962 \begin_inset LatexCommand \index{SDCC\_z80}
13969 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13974 depending on the model used (e.g.: -mds390
13978 <row topline="true">
13979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13985 \begin_inset LatexCommand \index{\_\_mcs51}
13990 \begin_inset LatexCommand \index{\_\_ds390}
13995 \begin_inset LatexCommand \index{\_\_hc08}
14000 \begin_inset LatexCommand \index{\_\_z80}
14007 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14012 depending on the model used (e.g.
14017 <row topline="true">
14018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14024 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14031 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14054 <row topline="true">
14055 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14061 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14091 <row topline="true">
14092 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14098 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14128 <row topline="true">
14129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14135 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
14142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14165 <row topline="true">
14166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14172 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
14179 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14192 <row topline="true" bottomline="true">
14193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14199 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
14206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14226 Notes on supported Processors
14230 \begin_inset LatexCommand \label{sub:MCS51-variants}
14235 \begin_inset LatexCommand \index{MCS51 variants}
14242 MCS51 processors are available from many vendors and come in many different
14244 While they might differ considerably in respect to Special Function Registers
14245 the core MCS51 is usually not modified or is kept compatible.
14249 pdata access by SFR
14252 With the upcome of devices with internal xdata and flash memory devices
14254 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14258 as dedicated I/O port is becoming more popular.
14259 Switching the high byte for pdata
14260 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14264 access which was formerly done by port P2 is then achieved by a Special
14266 \begin_inset LatexCommand \index{sfr}
14271 In well-established MCS51 tradition the address of this
14275 is where the chip designers decided to put it.
14276 Needless to say that they didn't agree on a common name either.
14277 So that the startup code can correctly initialize xdata variables, you
14278 should define an sfr with the name _XPAGE at the appropriate location if
14279 the default, port P2, is not used for this.
14285 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
14290 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
14295 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
14298 For more exotic implementations further customizations may be needed.
14300 \begin_inset LatexCommand \ref{sub:Startup-Code}
14304 for other possibilities.
14307 Other Features available by SFR
14310 Some MCS51 variants offer features like Double DPTR
14311 \begin_inset LatexCommand \index{DPTR}
14315 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
14316 These are currently not used for the MCS51 port.
14317 If you absolutely need them you can fall back to inline assembly or submit
14321 The Z80 and gbz80 port
14324 SDCC can target both the Zilog
14325 \begin_inset LatexCommand \index{Z80}
14329 and the Nintendo Gameboy's Z80-like gbz80
14330 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
14335 The Z80 port is passed through the same
14338 \begin_inset LatexCommand \index{Regression test}
14344 as the MCS51 and DS390 ports, so floating point support, support for long
14345 variables and bitfield support is fine.
14346 See mailing lists and forums about interrupt routines.
14349 As always, the code is the authoritative reference - see z80/ralloc.c and
14352 \begin_inset LatexCommand \index{stack}
14356 frame is similar to that generated by the IAR Z80 compiler.
14357 IX is used as the base pointer, HL and IY are used as a temporary registers,
14358 and BC and DE are available for holding variables.
14360 \begin_inset LatexCommand \index{return value}
14364 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
14366 The gbz80 port use the same set of registers for the return values, but
14367 in a different order of significance: E (one byte), DE (two bytes), or
14374 The port to the Motorola HC08
14375 \begin_inset LatexCommand \index{HC08}
14379 family has been added in October 2003, and is still undergoing some basic
14381 The code generator is complete, but the register allocation is still quite
14383 Some of the SDCC's standard C library functions have embedded non-HC08
14384 inline assembly and so are not yet usable.
14395 \begin_inset LatexCommand \index{PIC14}
14399 port still requires a major effort from the development community.
14400 However it can work for very simple code.
14403 C code and 14bit PIC code page
14404 \begin_inset LatexCommand \index{code page (pic14)}
14409 \begin_inset LatexCommand \index{RAM bank (pic14)}
14416 The linker organizes allocation for the code page and RAM banks.
14417 It does not have intimate knowledge of the code flow.
14418 It will put all the code section of a single asm file into a single code
14420 In order to make use of multiple code pages, separate asm files must be
14422 The compiler treats all functions of a single C file as being in the same
14423 code page unless it is non static.
14424 The compiler treats all local variables of a single C file as being in
14425 the same RAM bank unless it is an extern.
14429 To get the best follow these guide lines:
14432 make local functions static, as non static functions require code page selection
14436 Make local variables static as extern variables require RAM bank selection
14440 For devices that have multiple code pages it is more efficient to use the
14441 same number of files as pages, i.e.
14442 for the 16F877 use 4 separate files and i.e.
14443 for the 16F874 use 2 separate files.
14444 This way the linker can put the code for each file into different code
14445 pages and the compiler can allocate reusable variables more efficiently
14446 and there's less page selection overhead.
14447 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
14448 instruction set) use 'unsigned char' whereever possible instead of 'int'.
14451 Creating a device include file
14454 For generating a device include file use the support perl script inc2h.pl
14455 kept in directory support/script.
14461 For the interrupt function, use the keyword 'interrupt'
14462 \begin_inset LatexCommand \index{interrupt}
14466 with level number of 0 (PIC14 only has 1 interrupt so this number is only
14467 there to avoid a syntax error - it ought to be fixed).
14473 void Intr(void) interrupt 0
14479 T0IF = 0; /* Clear timer interrupt */
14484 Linking and assembling
14486 For assembling you can use either GPUTILS'
14487 \begin_inset LatexCommand \index{gputils (pic tools)}
14491 gpasm.exe or MPLAB's mpasmwin.exe.
14492 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
14493 If you use MPLAB and an interrupt function then the linker script file
14494 vectors section will need to be enlarged to link with mplink.
14517 sdcc -S -V -mpic14 -p16F877 $<
14531 $(PRJ).hex: $(OBJS)
14541 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
14563 sdcc -S -V -mpic14 -p16F877 $<
14573 mpasmwin /q /o $*.asm
14577 $(PRJ).hex: $(OBJS)
14587 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
14591 \begin_inset LatexCommand \index{PIC16}
14599 \begin_inset LatexCommand \index{PIC16}
14603 port is the portion of SDCC that is responsible to produce code for the
14605 \begin_inset LatexCommand \index{Microchip}
14609 (TM) microcontrollers with 16 bit core.
14610 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
14616 PIC16 port supports the standard command line arguments as supposed, with
14617 the exception of certain cases that will be mentioned in the following
14620 \labelwidthstring 00.00.0000
14632 -stack-auto Auto variables that are function parameters, will be saved on
14636 There is no need to specify this in the command line.
14638 \labelwidthstring 00.00.0000
14650 -float-reent All floating point functions are reentrant by default.
14653 There is no need to specifiy this in the command line.
14655 \labelwidthstring 00.00.0000
14667 -callee-saves See -
14679 \labelwidthstring 00.00.0000
14691 -all-callee-saves All function arguments are passed on stack by default.
14694 There is no need to specify this in the command line.
14696 \labelwidthstring 00.00.0000
14708 -fommit-frame-pointer Frame pointer will be omitted when the function uses
14709 no local variables.
14712 Port Specific Options
14715 The port specific options appear after the global options in the sdcc --help
14717 \layout Subsubsection
14722 General options enable certain port features and optimizations.
14724 \labelwidthstring 00.00.0000
14736 -pgen-bank Instructs the port to insert BANKSEL directives before instructions
14737 that use the Bank Select Register (BSR).
14739 \labelwidthstring 00.00.0000
14751 -pomit-config-words Instructs the port to omit the generation of the configurati
14754 \labelwidthstring 00.00.0000
14766 -pomit-ivt Instructs the port to omit the generation of the interrupt vectors
14768 \labelwidthstring 00.00.0000
14780 -pleave-reset-vector Used in conjuction with the previous command, instructs
14781 the port NOT to omit the reset vector.
14783 \labelwidthstring 00.00.0000
14795 -stack-model=[model] Used in conjuction with the command above.
14796 Defines the stack model to be used, valid stack models are :
14799 \labelwidthstring 00.00.0000
14805 Selects small stack model.
14806 8 bit stack and frame pointers.
14807 Supports 256 bytes stack size.
14809 \labelwidthstring 00.00.0000
14815 Selects large stack model.
14816 16 bit stack and frame pointers.
14817 Supports 65536 bytes stack size.
14820 \labelwidthstring 00.00.0000
14832 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
14833 unitialized data variables with [kword].
14834 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
14835 \layout Subsubsection
14840 Debugging options enable extra debugging information in the output files.
14842 \labelwidthstring 00.00.0000
14854 -debug-xtra Similar to --debug, but dumps more information.
14856 \labelwidthstring 00.00.0000
14868 -debug-ralloc Force register allocator to dump <source>.d file with debugging
14870 <source> is the name of the file compiled.
14872 \labelwidthstring 00.00.0000
14884 -pcode-verbose Enable pcode debugging information in translation.
14887 Preprocessor Macros
14890 PIC16 port defines the following preprocessor macros while translating a
14895 \begin_inset Tabular
14896 <lyxtabular version="3" rows="2" columns="2">
14898 <column alignment="center" valignment="top" leftline="true" width="0">
14899 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14900 <row topline="true" bottomline="true">
14901 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14909 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14918 <row topline="true" bottomline="true">
14919 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14927 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14932 MCU Identification.
14937 is the microcontrol identification number, i.e.
14952 PIC16 port uses the following directories for searching header files and
14957 \begin_inset Tabular
14958 <lyxtabular version="3" rows="3" columns="4">
14960 <column alignment="center" valignment="top" leftline="true" width="0">
14961 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14962 <column alignment="center" valignment="top" width="0">
14963 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14964 <row topline="true" bottomline="true">
14965 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14981 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14998 <row topline="true">
14999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15004 PREFIX/sdcc/include/pic16
15007 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15012 PIC16 specific headers
15015 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15023 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15032 <row topline="true" bottomline="true">
15033 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15038 PREFIX/sdcc/lib/pic16
15041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15046 PIC16 specific libraries
15049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15057 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15076 PIC16 port currently supports the following pragmas:
15078 \labelwidthstring 00.00.0000
15080 stack pragma stack forces the code generator to initialize the stack & frame
15081 pointers at a specific address.
15082 This is an adhoc solution since gplink does not support yet stack.
15083 When the gplink issue is resolved the pragma will be deprecated
15091 It is important to initialize the stack, otherwise strange things can happen.
15092 Stack is not initialized by default because there are some sources that
15094 (like library sources)
15099 The stack pragma should be used only once in a project.
15100 Multiple pragmas may result in indeterminate behaviour of the program.
15102 If you omit setting the pragma the port emits a warning message before linking.
15103 If not initializing the stack is desired ignore the message.
15111 /* initializes stack at RAM address 0x5ff*/
15114 #pragma stack 0x5ff
15122 There is one main header file that can be included to the source files using
15129 This header file contains the definitions for the processor special registers,
15130 so it is necessery if the soruce accesses them.
15131 It can be included by adding the following line in the beginning of the
15135 #include <pic18fregs.h>
15138 The specific microcontroller is selected within the pic18fregs.h automatically,
15139 so the same source can be used with a variety of devices.
15145 The libraries that pic16 port depends on are the microcontroller device
15146 libraries which contain the symbol definitions for the microcontroller
15147 special function registers.
15148 These libraries have the format pic18fxxxx.lib, where
15152 is the microcontroller identification number.
15153 The specific library is selected automatically by the compiler at link
15154 stage according to the selected device.
15157 Libraries are created with gplib which is part of the gputils package
15158 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
15168 The following memory models are supported by PIC16 port:
15177 Memory model affects the default size of pointers within the source.
15178 The sizes are shown in the next table:
15182 \begin_inset Tabular
15183 <lyxtabular version="3" rows="3" columns="3">
15185 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15186 <column alignment="center" valignment="top" leftline="true" width="0">
15187 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15188 <row topline="true" bottomline="true">
15189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15194 Pointer sizes according to memory model
15197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15214 <row topline="true" bottomline="true">
15215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15223 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15240 <row topline="true" bottomline="true">
15241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15249 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
15257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15273 It is advisable that all sources within a project are compiled with the
15275 If one wants to override the default memory model, this can be done by
15276 declaring a pointer as
15285 Far selects large memory model's pointers, while near selects small memory
15289 The standard device libraries (see 4.5.6) contain no reference to pointers,
15290 so they can be used with both memory models.
15296 The stack implementation for the PIC16 port uses two indirect registers,
15299 \labelwidthstring 00.00.0000
15301 FSR1 is assigned as stack pointer
15303 \labelwidthstring 00.00.0000
15305 FSR2 is assigned as frame pointer
15308 The following stack models are supported by PIC16 port
15321 model means that only the FSRxL byte is used to access stack and frame,
15328 uses both FSRxL and FSRxH registers.
15329 The following table shows the stack/frame pointers sizes according to stack
15330 model and the maximum space they can address:
15334 \begin_inset Tabular
15335 <lyxtabular version="3" rows="3" columns="3">
15337 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15338 <column alignment="center" valignment="top" leftline="true" width="0">
15339 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15340 <row topline="true" bottomline="true">
15341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15346 Stack & Frame pointer sizes according to stack model
15349 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15366 <row topline="true">
15367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15392 <row topline="true" bottomline="true">
15393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15427 Currently stack and frame pointers should be initialized explicit by the
15428 user at the desired Data RAM position (see 4.5.5 Pragma Stack).
15429 Uninitialized stack and frame pointers can result in unexpected behavior
15430 of the resulting binary.
15433 Function return values
15436 Return values from functions are placed to the appropriate registers following
15437 a modified Microchip policy optimized for SDCC.
15438 The following table shows these registers:
15442 \begin_inset Tabular
15443 <lyxtabular version="3" rows="6" columns="2">
15445 <column alignment="center" valignment="top" leftline="true" width="0">
15446 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15447 <row topline="true" bottomline="true">
15448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15461 destination register
15465 <row topline="true">
15466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15483 <row topline="true">
15484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15501 <row topline="true">
15502 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15510 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15519 <row topline="true">
15520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15533 FSR0L:PRODH:PRODL:WREG
15537 <row topline="true" bottomline="true">
15538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15551 on stack, FSR0 points to the beginning
15565 When entering an interrupt, currently the PIC16 port automatically saves
15566 the following registers:
15578 PROD (PRODL and PRODH)
15581 FSR0 (FSR0L and FSR0H)
15584 These registers are restored upon return from the interrupt routine
15590 NOTE that when the _naked attribute is specified for an interrupt routine,
15591 then NO registers are stored or restored.
15597 Debugging with SDCDB
15598 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
15603 \begin_inset LatexCommand \index{sdcdb (debugger)}
15610 SDCC is distributed with a source level debugger
15611 \begin_inset LatexCommand \index{Debugger}
15616 The debugger uses a command line interface, the command repertoire of the
15617 debugger has been kept as close to gdb
15618 \begin_inset LatexCommand \index{gdb}
15622 (the GNU debugger) as possible.
15623 The configuration and build process is part of the standard compiler installati
15624 on, which also builds and installs the debugger in the target directory
15625 specified during configuration.
15626 The debugger allows you debug BOTH at the C source and at the ASM source
15628 Sdcdb is available on Unix platforms only.
15631 Compiling for Debugging
15634 The \SpecialChar \-
15636 debug option must be specified for all files for which debug information
15637 is to be generated.
15638 The complier generates a .adb file for each of these files.
15639 The linker creates the .cdb
15640 \begin_inset LatexCommand \index{<file>.cdb}
15645 \begin_inset LatexCommand \index{<file>.adb}
15649 files and the address information.
15650 This .cdb is used by the debugger.
15653 How the Debugger Works
15666 -debug option is specified the compiler generates extra symbol information
15667 some of which are put into the assembler source and some are put into the
15669 Then the linker creates the .cdb file from the individual .adb files with
15670 the address information for the symbols.
15671 The debugger reads the symbolic information generated by the compiler &
15672 the address information generated by the linker.
15673 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
15674 execution is controlled by the debugger.
15675 When a command is issued for the debugger, it translates it into appropriate
15676 commands for the simulator.
15679 Starting the Debugger
15682 The debugger can be started using the following command line.
15683 (Assume the file you are debugging has the file name foo).
15697 The debugger will look for the following files.
15700 foo.c - the source file.
15703 foo.cdb - the debugger symbol information file.
15706 foo.ihx - the Intel hex format
15707 \begin_inset LatexCommand \index{Intel hex format}
15714 Command Line Options.
15727 -directory=<source file directory> this option can used to specify the directory
15729 The debugger will look into the directory list specified for source, cdb
15731 The items in the directory list must be separated by ':', e.g.
15732 if the source files can be in the directories /home/src1 and /home/src2,
15743 -directory option should be -
15753 -directory=/home/src1:/home/src2.
15754 Note there can be no spaces in the option.
15758 -cd <directory> - change to the <directory>.
15761 -fullname - used by GUI front ends.
15764 -cpu <cpu-type> - this argument is passed to the simulator please see the
15765 simulator docs for details.
15768 -X <Clock frequency > this options is passed to the simulator please see
15769 the simulator docs for details.
15772 -s <serial port file> passed to simulator see the simulator docs for details.
15775 -S <serial in,out> passed to simulator see the simulator docs for details.
15778 -k <port number> passed to simulator see the simulator docs for details.
15784 As mentioned earlier the command interface for the debugger has been deliberatel
15785 y kept as close the GNU debugger gdb, as possible.
15786 This will help the integration with existing graphical user interfaces
15787 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
15788 If you use a graphical user interface for the debugger you can skip the
15790 \layout Subsubsection*
15792 break [line | file:line | function | file:function]
15795 Set breakpoint at specified line or function:
15804 sdcdb>break foo.c:100
15806 sdcdb>break funcfoo
15808 sdcdb>break foo.c:funcfoo
15809 \layout Subsubsection*
15811 clear [line | file:line | function | file:function ]
15814 Clear breakpoint at specified line or function:
15823 sdcdb>clear foo.c:100
15825 sdcdb>clear funcfoo
15827 sdcdb>clear foo.c:funcfoo
15828 \layout Subsubsection*
15833 Continue program being debugged, after breakpoint.
15834 \layout Subsubsection*
15839 Execute till the end of the current function.
15840 \layout Subsubsection*
15845 Delete breakpoint number 'n'.
15846 If used without any option clear ALL user defined break points.
15847 \layout Subsubsection*
15849 info [break | stack | frame | registers ]
15852 info break - list all breakpoints
15855 info stack - show the function call stack.
15858 info frame - show information about the current execution frame.
15861 info registers - show content of all registers.
15862 \layout Subsubsection*
15867 Step program until it reaches a different source line.
15868 Note: pressing <return> repeats the last command.
15869 \layout Subsubsection*
15874 Step program, proceeding through subroutine calls.
15875 \layout Subsubsection*
15880 Start debugged program.
15881 \layout Subsubsection*
15886 Print type information of the variable.
15887 \layout Subsubsection*
15892 print value of variable.
15893 \layout Subsubsection*
15898 load the given file name.
15899 Note this is an alternate method of loading file for debugging.
15900 \layout Subsubsection*
15905 print information about current frame.
15906 \layout Subsubsection*
15911 Toggle between C source & assembly source.
15912 \layout Subsubsection*
15914 ! simulator command
15917 Send the string following '!' to the simulator, the simulator response is
15919 Note the debugger does not interpret the command being sent to the simulator,
15920 so if a command like 'go' is sent the debugger can loose its execution
15921 context and may display incorrect values.
15922 \layout Subsubsection*
15929 My name is Bobby Brown"
15932 Interfacing with XEmacs
15933 \begin_inset LatexCommand \index{XEmacs}
15938 \begin_inset LatexCommand \index{Emacs}
15945 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
15946 sdcdb.el and sdcdbsrc.el.
15947 These two files can be found in the $(prefix)/bin directory after the installat
15949 These files need to be loaded into XEmacs for the interface to work.
15950 This can be done at XEmacs startup time by inserting the following into
15951 your '.xemacs' file (which can be found in your HOME directory):
15957 (load-file sdcdbsrc.el)
15963 .xemacs is a lisp file so the () around the command is REQUIRED.
15964 The files can also be loaded dynamically while XEmacs is running, set the
15965 environment variable 'EMACSLOADPATH' to the installation bin directory
15966 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
15967 To start the interface enter the following command:
15981 You will prompted to enter the file name to be debugged.
15986 The command line options that are passed to the simulator directly are bound
15987 to default values in the file sdcdbsrc.el.
15988 The variables are listed below, these values maybe changed as required.
15991 sdcdbsrc-cpu-type '51
15994 sdcdbsrc-frequency '11059200
15997 sdcdbsrc-serial nil
16000 The following is a list of key mapping for the debugger interface.
16008 ;; Current Listing ::
16010 ;;key\SpecialChar ~
16025 binding\SpecialChar ~
16049 ;;---\SpecialChar ~
16064 ------\SpecialChar ~
16104 sdcdb-next-from-src\SpecialChar ~
16130 sdcdb-back-from-src\SpecialChar ~
16156 sdcdb-cont-from-src\SpecialChar ~
16166 SDCDB continue command
16182 sdcdb-step-from-src\SpecialChar ~
16208 sdcdb-whatis-c-sexp\SpecialChar ~
16218 SDCDB ptypecommand for data at
16282 sdcdbsrc-delete\SpecialChar ~
16296 SDCDB Delete all breakpoints if no arg
16344 given or delete arg (C-u arg x)
16360 sdcdbsrc-frame\SpecialChar ~
16375 SDCDB Display current frame if no arg,
16424 given or display frame arg
16489 sdcdbsrc-goto-sdcdb\SpecialChar ~
16499 Goto the SDCDB output buffer
16515 sdcdb-print-c-sexp\SpecialChar ~
16526 SDCDB print command for data at
16590 sdcdbsrc-goto-sdcdb\SpecialChar ~
16600 Goto the SDCDB output buffer
16616 sdcdbsrc-mode\SpecialChar ~
16632 Toggles Sdcdbsrc mode (turns it off)
16636 ;; C-c C-f\SpecialChar ~
16644 sdcdb-finish-from-src\SpecialChar ~
16652 SDCDB finish command
16656 ;; C-x SPC\SpecialChar ~
16664 sdcdb-break\SpecialChar ~
16682 Set break for line with point
16684 ;; ESC t\SpecialChar ~
16694 sdcdbsrc-mode\SpecialChar ~
16710 Toggle Sdcdbsrc mode
16712 ;; ESC m\SpecialChar ~
16722 sdcdbsrc-srcmode\SpecialChar ~
16744 Here are a few guidelines that will help the compiler generate more efficient
16745 code, some of the tips are specific to this compiler others are generally
16746 good programming practice.
16749 Use the smallest data type to represent your data-value.
16750 If it is known in advance that the value is going to be less than 256 then
16751 use an 'unsigned char' instead of a 'short' or 'int'.
16752 Please note, that ANSI C requires both signed and unsigned chars to be
16753 promoted to 'signed int' before doing any operation.
16754 This promotion can be omitted, if the result is the same.
16755 The effect of the promotion rules together with the sign-extension is often
16762 unsigned char uc = 0xfe;
16764 if (uc * uc < 0) /* this is true! */
16783 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
16793 (unsigned char) -12 / (signed char) -3 = ...
16796 No, the result is not 4:
16801 (int) (unsigned char) -12 / (int) (signed char) -3 =
16803 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
16805 (int) 0x00f4 / (int) 0xfffd =
16807 (int) 0x00f4 / (int) 0xfffd =
16809 (int) 244 / (int) -3 =
16811 (int) -81 = (int) 0xffaf;
16814 Don't complain, that gcc gives you a different result.
16815 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
16816 Therefore the results are different.
16819 \begin_inset Quotes sld
16823 \begin_inset Quotes srd
16829 If well-defined overflow characteristics are important and negative values
16830 are not, or if you want to steer clear of sign-extension problems when
16831 manipulating bits or bytes, use one of the corresponding unsigned types.
16832 (Beware when mixing signed and unsigned values in expressions, though.)
16834 Although character types (especially unsigned char) can be used as "tiny"
16835 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
16836 ble sign extension and increased code size.
16840 Use unsigned when it is known in advance that the value is not going to
16842 This helps especially if you are doing division or multiplication, bit-shifting
16843 or are using an array index.
16846 NEVER jump into a LOOP.
16849 Declare the variables to be local
16850 \begin_inset LatexCommand \index{local variables}
16854 whenever possible, especially loop control variables (induction).
16857 Since the compiler does not always do implicit integral promotion, the programme
16858 r should do an explicit cast when integral promotion is required.
16861 Reducing the size of division, multiplication & modulus operations can reduce
16862 code size substantially.
16863 Take the following code for example.
16869 foobar(unsigned int p1, unsigned char ch)
16877 unsigned char ch1 = p1 % ch ;
16888 For the modulus operation the variable ch will be promoted to unsigned int
16889 first then the modulus operation will be performed (this will lead to a
16890 call to support routine _moduint()), and the result will be casted to a
16892 If the code is changed to
16897 foobar(unsigned int p1, unsigned char ch)
16905 unsigned char ch1 = (unsigned char)p1 % ch ;
16916 It would substantially reduce the code generated (future versions of the
16917 compiler will be smart enough to detect such optimization opportunities).
16921 Have a look at the assembly listing to get a
16922 \begin_inset Quotes sld
16926 \begin_inset Quotes srd
16929 for the code generation.
16933 \begin_inset LatexCommand \index{Tools}
16937 included in the distribution
16941 \begin_inset Tabular
16942 <lyxtabular version="3" rows="12" columns="3">
16944 <column alignment="center" valignment="top" leftline="true" width="0pt">
16945 <column alignment="center" valignment="top" leftline="true" width="0pt">
16946 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
16947 <row topline="true" bottomline="true">
16948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16956 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16973 <row topline="true">
16974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16987 Simulator for various architectures
16990 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16999 <row topline="true">
17000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17013 header file conversion
17016 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17021 sdcc/support/scripts
17025 <row topline="true">
17026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17039 header file conversion
17042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17047 sdcc/support/scripts
17051 <row topline="true">
17052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17086 <row topline="true">
17087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17121 <row topline="true">
17122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17138 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17156 <row topline="true">
17157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17191 <row topline="true">
17192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17200 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17208 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17226 <row topline="true">
17227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17261 <row topline="true">
17262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17278 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17296 <row topline="true" bottomline="true">
17297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17305 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17313 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17341 \begin_inset LatexCommand \index{Documentation}
17345 included in the distribution
17349 \begin_inset Tabular
17350 <lyxtabular version="3" rows="10" columns="2">
17352 <column alignment="left" valignment="top" leftline="true" width="0">
17353 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
17354 <row topline="true" bottomline="true">
17355 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17363 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17368 Where to get / filename
17372 <row topline="true">
17373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17378 SDCC Compiler User Guide
17381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17386 You're reading it right now
17390 <row topline="true">
17391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17408 <row topline="true">
17409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17414 ASXXXX Assemblers and ASLINK Relocating Linker
17417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17422 sdcc/as/doc/asxhtm.html
17426 <row topline="true">
17427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17432 SDCC regression test
17433 \begin_inset LatexCommand \index{Regression test}
17440 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17445 sdcc/doc/test_suite_spec.pdf
17449 <row topline="true">
17450 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17458 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17467 <row topline="true">
17468 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17473 Notes on debugging with sdcdb
17474 \begin_inset LatexCommand \index{sdcdb (debugger)}
17481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17486 sdcc/debugger/README
17490 <row topline="true">
17491 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17496 Software simulator for microcontrollers
17499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17526 <row topline="true">
17527 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17532 Temporary notes on the pic16
17533 \begin_inset LatexCommand \index{PIC16}
17540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17545 sdcc/src/pic16/NOTES
17549 <row topline="true" bottomline="true">
17550 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17555 SDCC internal documentation (debugging file format)
17558 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17594 Related open source tools
17595 \begin_inset LatexCommand \index{Related tools}
17603 \begin_inset Tabular
17604 <lyxtabular version="3" rows="11" columns="3">
17606 <column alignment="center" valignment="top" leftline="true" width="0pt">
17607 <column alignment="block" valignment="top" leftline="true" width="30line%">
17608 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
17609 <row topline="true" bottomline="true">
17610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17635 <row topline="true">
17636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17642 \begin_inset LatexCommand \index{gpsim (pic simulator)}
17649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17663 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
17671 <row topline="true">
17672 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17678 \begin_inset LatexCommand \index{gputils (pic tools)}
17685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17699 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
17707 <row topline="true">
17708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17716 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17724 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17730 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
17738 <row topline="true">
17739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17745 \begin_inset LatexCommand \index{indent (source formatting tool)}
17752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17757 Formats C source - Master of the white spaces
17760 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17766 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
17774 <row topline="true">
17775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17781 \begin_inset LatexCommand \index{srecord (tool)}
17788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17793 Object file conversion, checksumming, ...
17796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17802 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
17810 <row topline="true">
17811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17817 \begin_inset LatexCommand \index{objdump (tool)}
17824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17829 Object file conversion, ...
17832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17837 Part of binutils (should be there anyway)
17841 <row topline="true">
17842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17848 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
17855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17860 Source code documentation system
17863 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17869 \begin_inset LatexCommand \url{http://www.doxygen.org}
17877 <row topline="true">
17878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17891 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
17894 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17900 \begin_inset LatexCommand \url{http://www.kdevelop.org}
17908 <row topline="true">
17909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17915 \begin_inset LatexCommand \index{splint (syntax checking tool)}
17922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17927 Statically checks c sources
17930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17936 \begin_inset LatexCommand \url{http://www.splint.org}
17944 <row topline="true" bottomline="true">
17945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17951 \begin_inset LatexCommand \index{ddd (debugger)}
17958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17963 Debugger, serves nicely as GUI to sdcdb
17964 \begin_inset LatexCommand \index{sdcdb (debugger)}
17971 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17977 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
17994 Related documentation / recommended reading
17998 \begin_inset Tabular
17999 <lyxtabular version="3" rows="6" columns="3">
18001 <column alignment="center" valignment="top" leftline="true" width="0pt">
18002 <column alignment="block" valignment="top" leftline="true" width="30line%">
18003 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18004 <row topline="true" bottomline="true">
18005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18030 <row topline="true">
18031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18054 \begin_inset LatexCommand \index{C Reference card}
18061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18067 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
18075 <row topline="true">
18076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18098 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
18106 <row topline="true">
18107 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18119 Latest datasheet of the target CPU
18122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18131 <row topline="true">
18132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18144 Revision history of datasheet
18147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18156 <row topline="true" bottomline="true">
18157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18172 Advanced Compiler Design and Implementation
18175 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18180 bookstore (very dedicated, probably read other books first)
18196 Some questions answered, some pointers given - it might be time to in turn
18204 can you solve your project with the selected microcontroller? Would you
18205 find out early or rather late that your target is too small/slow/whatever?
18206 Can you switch to a slightly better device if it doesn't fit?
18209 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
18210 and/or another programming language be more adequate? Would an operating
18211 system on the target device help?
18214 if you solved the problem, will the marketing department be happy?
18217 if the marketing department is happy, will customers be happy?
18220 if you're the project manager, marketing department and maybe even the customer
18221 in one person, have you tried to see the project from the outside?
18224 is the project done if you think it is done? Or is just that other interface/pro
18225 tocol/feature/configuration/option missing? How about website, manual(s),
18226 internationali(z|s)ation, packaging, labels, 2nd source for components,
18227 electromagnetic compatability/interference, documentation for production,
18228 production test software, update mechanism, patent issues?
18231 is your project adequately positioned in that magic triangle: fame, fortune,
18235 Maybe not all answers to these questions are known and some answers may
18240 , nevertheless knowing these questions may help you to avoid burnout
18246 burnout is bad for electronic devices, programmers and motorcycle tyres
18250 Chances are you didn't want to hear some of them...
18254 \begin_inset LatexCommand \index{Support}
18261 SDCC has grown to be a large project.
18262 The compiler alone (without the preprocessor, assembler and linker) is
18263 well over 100,000 lines of code (blank stripped).
18264 The open source nature of this project is a key to its continued growth
18266 You gain the benefit and support of many active software developers and
18268 Is SDCC perfect? No, that's why we need your help.
18269 The developers take pride in fixing reported bugs.
18270 You can help by reporting the bugs and helping other SDCC users.
18271 There are lots of ways to contribute, and we encourage you to take part
18272 in making SDCC a great software package.
18276 The SDCC project is hosted on the SDCC sourceforge site at
18277 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
18282 You'll find the complete set of mailing lists
18283 \begin_inset LatexCommand \index{Mailing list}
18287 , forums, bug reporting system, patch submission
18288 \begin_inset LatexCommand \index{Patch submission}
18293 \begin_inset LatexCommand \index{download}
18297 area and cvs code repository
18298 \begin_inset LatexCommand \index{cvs code repository}
18306 \begin_inset LatexCommand \index{Bug reporting}
18311 \begin_inset LatexCommand \index{Reporting bugs}
18318 The recommended way of reporting bugs is using the infrastructure of the
18320 You can follow the status of bug reports there and have an overview about
18324 Bug reports are automatically forwarded to the developer mailing list and
18325 will be fixed ASAP.
18326 When reporting a bug, it is very useful to include a small test program
18327 (the smaller the better) which reproduces the problem.
18328 If you can isolate the problem by looking at the generated assembly code,
18329 this can be very helpful.
18330 Compiling your program with the -
18341 \begin_inset LatexCommand \index{-\/-dumpall}
18345 option can sometimes be useful in locating optimization problems.
18346 When reporting a bug please maker sure you:
18349 Attach the code you are compiling with SDCC.
18353 Specify the exact command you use to run SDCC, or attach your Makefile.
18357 Specify the SDCC version (type "
18363 "), your platform, and operating system.
18367 Provide an exact copy of any error message or incorrect output.
18371 Put something meaningful in the subject of your message.
18374 Please attempt to include these 5 important parts, as applicable, in all
18375 requests for support or when reporting any problems or bugs with SDCC.
18376 Though this will make your message lengthy, it will greatly improve your
18377 chance that SDCC users and developers will be able to help you.
18378 Some SDCC developers are frustrated by bug reports without code provided
18379 that they can use to reproduce and ultimately fix the problem, so please
18380 be sure to provide sample code if you are reporting a bug!
18383 Please have a short check that you are using a recent version of SDCC and
18384 the bug is not yet known.
18385 This is the link for reporting bugs:
18386 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
18393 Requesting Features
18394 \begin_inset LatexCommand \label{sub:Requesting-Features}
18399 \begin_inset LatexCommand \index{Feature request}
18404 \begin_inset LatexCommand \index{Requesting features}
18411 Like bug reports feature requests are forwarded to the developer mailing
18413 This is the link for requesting features:
18414 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
18424 Like bug reports contributed patches are forwarded to the developer mailing
18426 This is the link for submitting patches
18427 \begin_inset LatexCommand \index{Patch submission}
18432 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
18439 You need to specify some parameters to the
18443 command for the patches to be useful.
18444 If you modified more than one file a patch created f.e.
18449 \begin_inset Quotes sld
18452 diff -Naur unmodified_directory modified_directory >my_changes.patch
18453 \begin_inset Quotes srd
18459 will be fine, otherwise
18463 \begin_inset Quotes sld
18466 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
18467 \begin_inset Quotes srd
18480 These links should take you directly to the
18481 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
18491 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
18492 automated messages (mid 2003)
18496 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
18500 , lists and forums are archived and searchable so if you are lucky someone
18501 already had a similar problem.
18507 You can follow the status of the cvs version
18508 \begin_inset LatexCommand \index{version}
18512 of SDCC by watching the Changelog
18513 \begin_inset LatexCommand \index{Changelog}
18517 in the cvs-repository
18522 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
18530 \begin_inset LatexCommand \index{Release policy}
18537 Historically there often were long delays between official releases and
18538 the sourceforge download area tends to get not updated at all.
18539 Excuses in the past might have referred to problems with live range analysis,
18540 but as this was fixed a while ago, the current problem is that another
18541 excuse has to be found.
18542 Kidding aside, we have to get better there! On the other hand there are
18543 daily snapshots available at
18544 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
18548 , and you can always build the very last version (hopefully with many bugs
18549 fixed, and features added) from the source code available at
18550 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
18558 \begin_inset LatexCommand \index{Examples}
18565 You'll find some small examples in the directory
18567 sdcc/device/examples/.
18570 More examples and libraries are available at
18572 The SDCC Open Knowledge Resource
18573 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
18580 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
18587 I did insert a reference to Paul's web site here although it seems rather
18588 dedicated to a specific 8032 board (I think it's okay because it f.e.
18589 shows LCD/Harddisc interface and has a free 8051 monitor.
18590 Independent 8032 board vendors face hard competition of heavily subsidized
18591 development boards anyway).
18594 Maybe we should include some links to real world applications.
18595 Preferably pointer to pointers (one for each architecture) so this stays
18600 \begin_inset LatexCommand \index{Quality control}
18607 The compiler is passed through nightly compile and build checks.
18613 \begin_inset LatexCommand \index{Regression test}
18617 check that SDCC itself compiles flawlessly on several platforms and checks
18618 the quality of the code generated by SDCC by running the code through simulator
18620 There is a separate document
18623 \begin_inset LatexCommand \index{Test suite}
18632 You'll find the test code in the directory
18634 sdcc/support/regression
18637 You can run these tests manually by running
18641 in this directory (or f.e.
18646 \begin_inset Quotes sld
18650 \begin_inset Quotes srd
18656 if you don't want to run the complete tests).
18657 The test code might also be interesting if you want to look for examples
18658 \begin_inset LatexCommand \index{Examples}
18662 checking corner cases of SDCC or if you plan to submit patches
18663 \begin_inset LatexCommand \index{Patch submission}
18670 The pic port uses a different set of regression tests, you'll find them
18673 sdcc/src/regression
18678 SDCC Technical Data
18682 \begin_inset LatexCommand \index{Optimizations}
18689 SDCC performs a host of standard optimizations in addition to some MCU specific
18694 Sub-expression Elimination
18695 \begin_inset LatexCommand \index{Subexpression elimination}
18702 The compiler does local and
18728 will be translated to
18740 Some subexpressions are not as obvious as the above example, e.g.:
18750 In this case the address arithmetic a->b[i] will be computed only once;
18751 the equivalent code in C would be.
18763 The compiler will try to keep these temporary variables in registers.
18766 Dead-Code Elimination
18767 \begin_inset LatexCommand \index{Dead-code elimination}
18788 i = 1; \SpecialChar ~
18797 global = 1;\SpecialChar ~
18810 global = 3;\SpecialChar ~
18839 \begin_inset LatexCommand \index{Copy propagation}
18895 Note: the dead stores created by this copy propagation will be eliminated
18896 by dead-code elimination.
18900 \begin_inset LatexCommand \index{Loop optimization}
18907 Two types of loop optimizations are done by SDCC loop invariant lifting
18908 and strength reduction of loop induction variables.
18909 In addition to the strength reduction the optimizer marks the induction
18910 variables and the register allocator tries to keep the induction variables
18911 in registers for the duration of the loop.
18912 Because of this preference of the register allocator
18913 \begin_inset LatexCommand \index{Register allocation}
18917 , loop induction optimization causes an increase in register pressure, which
18918 may cause unwanted spilling of other temporary variables into the stack
18919 \begin_inset LatexCommand \index{stack}
18924 The compiler will generate a warning message when it is forced to allocate
18925 extra space either on the stack or data space.
18926 If this extra space allocation is undesirable then induction optimization
18927 can be eliminated either for the entire source file (with -
18937 -noinduction option) or for a given function only using #pragma\SpecialChar ~
18939 \begin_inset LatexCommand \index{\#pragma noinduction}
18952 for (i = 0 ; i < 100 ; i ++)
18968 for (i = 0; i < 100; i++)
18977 As mentioned previously some loop invariants are not as apparent, all static
18978 address computations are also moved out of the loop.
18983 \begin_inset LatexCommand \index{Strength reduction}
18987 , this optimization substitutes an expression by a cheaper expression:
18992 for (i=0;i < 100; i++)
19010 for (i=0;i< 100;i++) {
19016 ar[itemp1] = itemp2;
19033 The more expensive multiplication
19034 \begin_inset LatexCommand \index{Multiplication}
19038 is changed to a less expensive addition.
19042 \begin_inset LatexCommand \index{Loop reversing}
19049 This optimization is done to reduce the overhead of checking loop boundaries
19050 for every iteration.
19051 Some simple loops can be reversed and implemented using a
19052 \begin_inset Quotes eld
19055 decrement and jump if not zero
19056 \begin_inset Quotes erd
19060 SDCC checks for the following criterion to determine if a loop is reversible
19061 (note: more sophisticated compilers use data-dependency analysis to make
19062 this determination, SDCC uses a more simple minded analysis).
19065 The 'for' loop is of the form
19071 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
19081 The <for body> does not contain
19082 \begin_inset Quotes eld
19086 \begin_inset Quotes erd
19090 \begin_inset Quotes erd
19096 All goto's are contained within the loop.
19099 No function calls within the loop.
19102 The loop control variable <sym> is not assigned any value within the loop
19105 The loop control variable does NOT participate in any arithmetic operation
19109 There are NO switch statements in the loop.
19112 Algebraic Simplifications
19115 SDCC does numerous algebraic simplifications, the following is a small sub-set
19116 of these optimizations.
19121 i = j + 0;\SpecialChar ~
19125 /* changed to: */\SpecialChar ~
19131 i /= 2;\SpecialChar ~
19138 /* changed to: */\SpecialChar ~
19144 i = j - j;\SpecialChar ~
19148 /* changed to: */\SpecialChar ~
19154 i = j / 1;\SpecialChar ~
19158 /* changed to: */\SpecialChar ~
19165 Note the subexpressions
19166 \begin_inset LatexCommand \index{Subexpression}
19170 given above are generally introduced by macro expansions or as a result
19171 of copy/constant propagation.
19174 'switch' Statements
19175 \begin_inset LatexCommand \label{sub:'switch'-Statements}
19180 \begin_inset LatexCommand \index{switch statement}
19187 SDCC changes switch statements to jump tables
19188 \begin_inset LatexCommand \index{jump tables}
19192 when the following conditions are true.
19196 The case labels are in numerical sequence, the labels need not be in order,
19197 and the starting number need not be one or zero.
19203 switch(i) {\SpecialChar ~
19234 case 4: ...\SpecialChar ~
19266 case 5: ...\SpecialChar ~
19298 case 3: ...\SpecialChar ~
19330 case 6: ...\SpecialChar ~
19398 Both the above switch statements will be implemented using a jump-table.
19399 The example to the right side is slightly more efficient as the check for
19400 the lower boundary of the jump-table is not needed.
19404 The number of case labels is at least three, since it takes two conditional
19405 statements to handle the boundary conditions.
19408 The number of case labels is less than 84, since each label takes 3 bytes
19409 and a jump-table can be utmost 256 bytes long.
19412 Switch statements which have gaps in the numeric sequence or those that
19413 have more that 84 case labels can be split into more than one switch statement
19414 for efficient code generation, e.g.:
19464 If the above switch statement is broken down into two switch statements
19503 case 9:\SpecialChar ~
19510 case 10:\SpecialChar ~
19516 case 11:\SpecialChar ~
19522 case 12:\SpecialChar ~
19529 then both the switch statements will be implemented using jump-tables whereas
19530 the unmodified switch statement will not be.
19531 You might also consider inserting dummy cases 0 and 5 to 8 in this example.
19534 The pragma nojtbound
19535 \begin_inset LatexCommand \index{\#pragma nojtbound}
19539 can be used to turn off checking the
19552 It has no effect if a default label is supplied.
19553 Use of this pragma is dangerous: if the switch argument is not matched
19554 by a case statement the processor will happily jump into Nirvana.
19557 Bit-shifting Operations
19558 \begin_inset LatexCommand \index{Bit shifting}
19565 Bit shifting is one of the most frequently used operation in embedded programmin
19567 SDCC tries to implement bit-shift operations in the most efficient way
19583 generates the following code:
19600 In general SDCC will never setup a loop if the shift count is known.
19643 \begin_inset LatexCommand \index{Bit rotation}
19650 A special case of the bit-shift operation is bit rotation
19651 \begin_inset LatexCommand \index{rotating bits}
19655 , SDCC recognizes the following expression to be a left bit-rotation:
19665 char i;\SpecialChar ~
19676 /* unsigned is needed for rotation */
19681 i = ((i << 1) | (i >> 7));
19690 will generate the following code:
19709 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
19710 ns of this case will also be recognized as bit-rotation, i.e.:
19715 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
19718 Nibble and Byte Swapping
19721 Other special cases of the bit-shift operations are nibble or byte swapping
19722 \begin_inset LatexCommand \index{swapping nibbles/bytes}
19726 , SDCC recognizes the following expressions:
19749 i = ((i << 4) | (i >> 4));
19755 j = ((j << 8) | (j >> 8));
19758 and generates a swap instruction for the nibble swapping
19759 \begin_inset LatexCommand \index{Nibble swapping}
19763 or move instructions for the byte swapping
19764 \begin_inset LatexCommand \index{Byte swapping}
19770 \begin_inset Quotes sld
19774 \begin_inset Quotes srd
19777 example can be used to convert from little to big-endian or vice versa.
19778 If you want to change the endianness of a
19782 integer you have to cast to
19789 Note that SDCC stores numbers in little-endian
19795 Usually 8-bit processors don't care much about endianness.
19796 This is not the case for the standard 8051 which only has an instruction
19802 \begin_inset LatexCommand \index{DPTR}
19810 so little-endian is the more efficient byte order.
19814 \begin_inset LatexCommand \index{little-endian}
19819 \begin_inset LatexCommand \index{Endianness}
19824 lowest order first).
19828 \begin_inset LatexCommand \index{Highest Order Bit}
19835 It is frequently required to obtain the highest order bit of an integral
19836 type (long, int, short or char types).
19837 SDCC recognizes the following expression to yield the highest order bit
19838 and generates optimized code for it, e.g.:
19860 hob = (gint >> 15) & 1;
19870 will generate the following code:
19903 000A E5*01\SpecialChar ~
19930 000C 23\SpecialChar ~
19961 000D 54 01\SpecialChar ~
19988 000F F5*02\SpecialChar ~
20016 Variations of this case however will
20021 It is a standard C expression, so I heartily recommend this be the only
20022 way to get the highest order bit, (it is portable).
20023 Of course it will be recognized even if it is embedded in other expressions,
20029 xyz = gint + ((gint >> 15) & 1);
20032 will still be recognized.
20036 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
20041 \begin_inset LatexCommand \index{Peephole optimizer}
20048 The compiler uses a rule based, pattern matching and re-writing mechanism
20049 for peep-hole optimization.
20054 a peep-hole optimizer by Christopher W.
20055 Fraser (cwfraser@microsoft.com).
20056 A default set of rules are compiled into the compiler, additional rules
20057 may be added with the
20070 \begin_inset LatexCommand \index{-\/-peep-file}
20077 The rule language is best illustrated with examples.
20101 The above rule will change the following assembly
20102 \begin_inset LatexCommand \index{Assembler routines}
20124 Note: All occurrences of a
20128 (pattern variable) must denote the same string.
20129 With the above rule, the assembly sequence:
20139 will remain unmodified.
20143 Other special case optimizations may be added by the user (via
20159 some variants of the 8051 MCU allow only
20168 The following two rules will change all
20187 replace { lcall %1 } by { acall %1 }
20189 replace { ljmp %1 } by { ajmp %1 }
20194 inline-assembler code
20196 is also passed through the peep hole optimizer, thus the peephole optimizer
20197 can also be used as an assembly level macro expander.
20198 The rules themselves are MCU dependent whereas the rule language infra-structur
20199 e is MCU independent.
20200 Peephole optimization rules for other MCU can be easily programmed using
20205 The syntax for a rule is as follows:
20210 rule := replace [ restart ] '{' <assembly sequence> '
20248 <assembly sequence> '
20266 '}' [if <functionName> ] '
20271 <assembly sequence> := assembly instruction (each instruction including
20272 labels must be on a separate line).
20276 The optimizer will apply to the rules one by one from the top in the sequence
20277 of their appearance, it will terminate when all rules are exhausted.
20278 If the 'restart' option is specified, then the optimizer will start matching
20279 the rules again from the top, this option for a rule is expensive (performance)
20280 , it is intended to be used in situations where a transformation will trigger
20281 the same rule again.
20282 An example of this (not a good one, it has side effects) is the following
20305 Note that the replace pattern cannot be a blank, but can be a comment line.
20306 Without the 'restart' option only the innermost 'pop' 'push' pair would
20307 be eliminated, i.e.:
20337 the restart option the rule will be applied again to the resulting code
20338 and then all the pop-push pairs will be eliminated to yield:
20348 A conditional function can be attached to a rule.
20349 Attaching rules are somewhat more involved, let me illustrate this with
20376 The optimizer does a look-up of a function name table defined in function
20381 in the source file SDCCpeeph.c, with the name
20386 If it finds a corresponding entry the function is called.
20387 Note there can be no parameters specified for these functions, in this
20392 is crucial, since the function
20396 expects to find the label in that particular variable (the hash table containin
20397 g the variable bindings is passed as a parameter).
20398 If you want to code more such functions, take a close look at the function
20399 labelInRange and the calling mechanism in source file SDCCpeeph.c.
20400 Currently implemented are
20402 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
20403 24bitMode, portIsDS390, 24bitModeAndPortDS390
20412 I know this whole thing is a little kludgey, but maybe some day we will
20413 have some better means.
20414 If you are looking at this file, you will see the default rules that are
20415 compiled into the compiler, you can add your own rules in the default set
20416 there if you get tired of specifying the -
20430 \begin_inset LatexCommand \index{ANSI-compliance}
20435 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
20442 Deviations from the compliance:
20445 functions are not always reentrant
20446 \begin_inset LatexCommand \index{reentrant}
20453 structures cannot be assigned values directly, cannot be passed as function
20454 parameters or assigned to each other and cannot be a return value from
20481 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
20492 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
20514 return rets;/* is invalid in SDCC although allowed in ANSI */
20521 \begin_inset LatexCommand \index{long long (not supported)}
20526 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
20534 \begin_inset LatexCommand \index{double (not supported)}
20538 ' precision floating point
20539 \begin_inset LatexCommand \index{Floating point support}
20546 No support for setjmp
20547 \begin_inset LatexCommand \index{setjmp (not supported)}
20552 \begin_inset LatexCommand \index{longjmp (not supported)}
20560 \begin_inset LatexCommand \index{K\&R style}
20564 function declarations are NOT allowed.
20570 foo(i,j) /* this old style of function declarations */
20572 int i,j; /* are valid in ANSI but not valid in SDCC */
20587 functions declared as pointers
20588 \begin_inset LatexCommand \index{Pointer (to function)}
20593 \begin_inset LatexCommand \index{function pointers}
20597 must be dereferenced during the call.
20608 /* has to be called like this */
20610 (*foo)(); /* ANSI standard allows calls to be made like 'foo()' */
20614 Cyclomatic Complexity
20615 \begin_inset LatexCommand \index{Cyclomatic complexity}
20622 Cyclomatic complexity of a function is defined as the number of independent
20623 paths the program can take during execution of the function.
20624 This is an important number since it defines the number test cases you
20625 have to generate to validate the function.
20626 The accepted industry standard for complexity number is 10, if the cyclomatic
20627 complexity reported by SDCC exceeds 10 you should think about simplification
20628 of the function logic.
20629 Note that the complexity level is not related to the number of lines of
20630 code in a function.
20631 Large functions can have low complexity, and small functions can have large
20637 SDCC uses the following formula to compute the complexity:
20642 complexity = (number of edges in control flow graph) - (number of nodes
20643 in control flow graph) + 2;
20647 Having said that the industry standard is 10, you should be aware that in
20648 some cases it be may unavoidable to have a complexity level of less than
20650 For example if you have switch statement with more than 10 case labels,
20651 each case label adds one to the complexity level.
20652 The complexity level is by no means an absolute measure of the algorithmic
20653 complexity of the function, it does however provide a good starting point
20654 for which functions you might look at for further optimization.
20657 Retargetting for other Processors
20660 The issues for retargetting the compiler are far too numerous to be covered
20662 What follows is a brief description of each of the seven phases of the
20663 compiler and its MCU dependency.
20666 Parsing the source and building the annotated parse tree.
20667 This phase is largely MCU independent (except for the language extensions).
20668 Syntax & semantic checks are also done in this phase, along with some initial
20669 optimizations like back patching labels and the pattern matching optimizations
20670 like bit-rotation etc.
20673 The second phase involves generating an intermediate code which can be easy
20674 manipulated during the later phases.
20675 This phase is entirely MCU independent.
20676 The intermediate code generation assumes the target machine has unlimited
20677 number of registers, and designates them with the name iTemp.
20678 The compiler can be made to dump a human readable form of the code generated
20692 This phase does the bulk of the standard optimizations and is also MCU independe
20694 This phase can be broken down into several sub-phases:
20698 Break down intermediate code (iCode) into basic blocks.
20700 Do control flow & data flow analysis on the basic blocks.
20702 Do local common subexpression elimination, then global subexpression elimination
20704 Dead code elimination
20708 If loop optimizations caused any changes then do 'global subexpression eliminati
20709 on' and 'dead code elimination' again.
20712 This phase determines the live-ranges; by live range I mean those iTemp
20713 variables defined by the compiler that still survive after all the optimization
20715 Live range analysis
20716 \begin_inset LatexCommand \index{Live range analysis}
20720 is essential for register allocation, since these computation determines
20721 which of these iTemps will be assigned to registers, and for how long.
20724 Phase five is register allocation.
20725 There are two parts to this process.
20729 The first part I call 'register packing' (for lack of a better term).
20730 In this case several MCU specific expression folding is done to reduce
20735 The second part is more MCU independent and deals with allocating registers
20736 to the remaining live ranges.
20737 A lot of MCU specific code does creep into this phase because of the limited
20738 number of index registers available in the 8051.
20741 The Code generation phase is (unhappily), entirely MCU dependent and very
20742 little (if any at all) of this code can be reused for other MCU.
20743 However the scheme for allocating a homogenized assembler operand for each
20744 iCode operand may be reused.
20747 As mentioned in the optimization section the peep-hole optimizer is rule
20748 based system, which can reprogrammed for other MCUs.
20752 \begin_inset LatexCommand \index{Compiler internals}
20759 The anatomy of the compiler
20760 \begin_inset LatexCommand \label{sub:The-anatomy-of}
20769 This is an excerpt from an article published in Circuit Cellar Magazine
20771 It's a little outdated (the compiler is much more efficient now and user/develo
20772 per friendly), but pretty well exposes the guts of it all.
20778 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
20779 It is fairly easy to retarget for other 8-bit MCU.
20780 Here we take a look at some of the internals of the compiler.
20785 \begin_inset LatexCommand \index{Parsing}
20792 Parsing the input source file and creating an AST (Annotated Syntax Tree
20793 \begin_inset LatexCommand \index{Annotated syntax tree}
20798 This phase also involves propagating types (annotating each node of the
20799 parse tree with type information) and semantic analysis.
20800 There are some MCU specific parsing rules.
20801 For example the storage classes, the extended storage classes are MCU specific
20802 while there may be a xdata storage class for 8051 there is no such storage
20803 class for z80 or Atmel AVR.
20804 SDCC allows MCU specific storage class extensions, i.e.
20805 xdata will be treated as a storage class specifier when parsing 8051 C
20806 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
20811 \begin_inset LatexCommand \index{iCode}
20818 Intermediate code generation.
20819 In this phase the AST is broken down into three-operand form (iCode).
20820 These three operand forms are represented as doubly linked lists.
20821 ICode is the term given to the intermediate form generated by the compiler.
20822 ICode example section shows some examples of iCode generated for some simple
20823 C source functions.
20827 \begin_inset LatexCommand \index{Optimizations}
20834 Bulk of the target independent optimizations is performed in this phase.
20835 The optimizations include constant propagation, common sub-expression eliminati
20836 on, loop invariant code movement, strength reduction of loop induction variables
20837 and dead-code elimination.
20840 Live range analysis
20841 \begin_inset LatexCommand \index{Live range analysis}
20848 During intermediate code generation phase, the compiler assumes the target
20849 machine has infinite number of registers and generates a lot of temporary
20851 The live range computation determines the lifetime of each of these compiler-ge
20852 nerated temporaries.
20853 A picture speaks a thousand words.
20854 ICode example sections show the live range annotations for each of the
20856 It is important to note here, each iCode is assigned a number in the order
20857 of its execution in the function.
20858 The live ranges are computed in terms of these numbers.
20859 The from number is the number of the iCode which first defines the operand
20860 and the to number signifies the iCode which uses this operand last.
20863 Register Allocation
20864 \begin_inset LatexCommand \index{Register allocation}
20871 The register allocation determines the type and number of registers needed
20873 In most MCUs only a few registers can be used for indirect addressing.
20874 In case of 8051 for example the registers R0 & R1 can be used to indirectly
20875 address the internal ram and DPTR to indirectly address the external ram.
20876 The compiler will try to allocate the appropriate register to pointer variables
20878 ICode example section shows the operands annotated with the registers assigned
20880 The compiler will try to keep operands in registers as much as possible;
20881 there are several schemes the compiler uses to do achieve this.
20882 When the compiler runs out of registers the compiler will check to see
20883 if there are any live operands which is not used or defined in the current
20884 basic block being processed, if there are any found then it will push that
20885 operand and use the registers in this block, the operand will then be popped
20886 at the end of the basic block.
20890 There are other MCU specific considerations in this phase.
20891 Some MCUs have an accumulator; very short-lived operands could be assigned
20892 to the accumulator instead of a general-purpose register.
20898 Figure II gives a table of iCode operations supported by the compiler.
20899 The code generation involves translating these operations into corresponding
20900 assembly code for the processor.
20901 This sounds overly simple but that is the essence of code generation.
20902 Some of the iCode operations are generated on a MCU specific manner for
20903 example, the z80 port does not use registers to pass parameters so the
20904 SEND and RECV iCode operations will not be generated, and it also does
20905 not support JUMPTABLES.
20912 <Where is Figure II ?>
20916 \begin_inset LatexCommand \index{iCode}
20923 This section shows some details of iCode.
20924 The example C code does not do anything useful; it is used as an example
20925 to illustrate the intermediate code generated by the compiler.
20937 /* This function does nothing useful.
20944 for the purpose of explaining iCode */
20947 short function (data int *x)
20955 short i=10; \SpecialChar ~
20957 /* dead initialization eliminated */
20962 short sum=10; /* dead initialization eliminated */
20975 while (*x) *x++ = *p++;
20989 /* compiler detects i,j to be induction variables */
20993 for (i = 0, j = 10 ; i < 10 ; i++, j
21019 mul += i * 3; \SpecialChar ~
21021 /* this multiplication remains */
21027 gint += j * 3;\SpecialChar ~
21029 /* this multiplication changed to addition */
21043 In addition to the operands each iCode contains information about the filename
21044 and line it corresponds to in the source file.
21045 The first field in the listing should be interpreted as follows:
21050 Filename(linenumber: iCode Execution sequence number : ICode hash table
21051 key : loop depth of the iCode).
21056 Then follows the human readable form of the ICode operation.
21057 Each operand of this triplet form can be of three basic types a) compiler
21058 generated temporary b) user defined variable c) a constant value.
21059 Note that local variables and parameters are replaced by compiler generated
21062 \begin_inset LatexCommand \index{Live range analysis}
21066 are computed only for temporaries (i.e.
21067 live ranges are not computed for global variables).
21069 \begin_inset LatexCommand \index{Register allocation}
21073 are allocated for temporaries only.
21074 Operands are formatted in the following manner:
21079 Operand Name [lr live-from : live-to ] { type information } [ registers
21085 As mentioned earlier the live ranges are computed in terms of the execution
21086 sequence number of the iCodes, for example
21088 the iTemp0 is live from (i.e.
21089 first defined in iCode with execution sequence number 3, and is last used
21090 in the iCode with sequence number 5).
21091 For induction variables such as iTemp21 the live range computation extends
21092 the lifetime from the start to the end of the loop.
21094 The register allocator used the live range information to allocate registers,
21095 the same registers may be used for different temporaries if their live
21096 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
21097 iTemp17 since their live ranges do not overlap.
21098 In addition the allocator also takes into consideration the type and usage
21099 of a temporary, for example itemp6 is a pointer to near space and is used
21100 as to fetch data from (i.e.
21101 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
21102 Some short lived temporaries are allocated to special registers which have
21103 meaning to the code generator e.g.
21104 iTemp13 is allocated to a pseudo register CC which tells the back end that
21105 the temporary is used only for a conditional jump the code generation makes
21106 use of this information to optimize a compare and jump ICode.
21108 There are several loop optimizations
21109 \begin_inset LatexCommand \index{Loop optimization}
21113 performed by the compiler.
21114 It can detect induction variables iTemp21(i) and iTemp23(j).
21115 Also note the compiler does selective strength reduction
21116 \begin_inset LatexCommand \index{Strength reduction}
21121 the multiplication of an induction variable in line 18 (gint = j * 3) is
21122 changed to addition, a new temporary iTemp17 is allocated and assigned
21123 a initial value, a constant 3 is then added for each iteration of the loop.
21124 The compiler does not change the multiplication
21125 \begin_inset LatexCommand \index{Multiplication}
21129 in line 17 however since the processor does support an 8 * 8 bit multiplication.
21131 Note the dead code elimination
21132 \begin_inset LatexCommand \index{Dead-code elimination}
21136 optimization eliminated the dead assignments in line 7 & 8 to I and sum
21144 Sample.c (5:1:0:0) _entry($9) :
21149 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
21154 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
21159 Sample.c(11:4:53:0) preHeaderLbl0($11) :
21164 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
21170 Sample.c(11:6:5:1) _whilecontinue_0($1) :
21175 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
21181 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
21186 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
21192 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
21198 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
21204 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
21210 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
21211 * int}[r0] + 0x2 {short}
21216 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
21221 Sample.c(11:17:21:0)_whilebreak_0($3) :
21226 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
21231 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
21236 Sample.c(15:20:54:0)preHeaderLbl1($13) :
21241 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
21246 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
21251 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
21256 Sample.c(15:24:26:1)_forcond_0($4) :
21261 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
21267 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
21272 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
21273 + ITemp21 [lr21:38]{short}[r4]
21278 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
21284 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
21285 + iTemp15 [lr29:30]{short}[r1]
21290 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
21296 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
21302 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
21308 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
21314 Sample.c(19:38:47:1) goto _forcond_0($4)
21319 Sample.c(19:39:48:0)_forbreak_0($7) :
21324 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
21325 + ITemp11 [lr19:40]{short}[r3]
21330 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
21335 Sample.c(20:42:51:0)_return($8) :
21340 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
21346 Finally the code generated for this function:
21387 ; ----------------------------------------------
21392 ; function function
21397 ; ----------------------------------------------
21407 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
21419 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
21431 ;_whilecontinue_0($1) :
21441 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
21446 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
21505 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
21524 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
21571 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
21611 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
21637 ; iTemp6 [lr5:16]{_near * int}[r0] =
21642 ; iTemp6 [lr5:16]{_near * int}[r0] +
21659 ; goto _whilecontinue_0($1)
21671 ; _whilebreak_0($3) :
21681 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
21693 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
21705 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
21717 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
21736 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
21765 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
21770 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
21815 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
21820 ; iTemp21 [lr21:38]{short}[r4]
21846 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
21879 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
21884 ; iTemp15 [lr29:30]{short}[r1]
21903 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
21950 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
21997 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
22009 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
22023 cjne r5,#0xff,00104$
22035 ; goto _forcond_0($4)
22047 ; _forbreak_0($7) :
22057 ; ret iTemp24 [lr40:41]{short}
22100 A few words about basic block successors, predecessors and dominators
22103 Successors are basic blocks
22104 \begin_inset LatexCommand \index{Basic blocks}
22108 that might execute after this basic block.
22110 Predecessors are basic blocks that might execute before reaching this basic
22113 Dominators are basic blocks that WILL execute before reaching this basic
22147 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
22150 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
22153 c) domVect of [BB4] = BB1 ...
22154 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
22162 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
22172 Thanks to all the other volunteer developers who have helped with coding,
22173 testing, web-page creation, distribution sets, etc.
22174 You know who you are :-)
22181 This document was initially written by Sandeep Dutta
22184 All product names mentioned herein may be trademarks
22185 \begin_inset LatexCommand \index{Trademarks}
22189 of their respective companies.
22196 To avoid confusion, the installation and building options for SDCC itself
22197 (chapter 2) are not part of the index.
22201 \begin_inset LatexCommand \printindex{}