1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={8032, 8051, ansi, c, compiler, CPU, DS390,
11 embedded, GPL, HC08, manual, mcs51, PIC, Z80},
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by cvs
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is a Freeware, retargettable, optimizing ANSI-C compiler by
135 designed for 8 bit Microprocessors.
136 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
138 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
142 , etc.), Dallas DS80C390 variants, Motorola HC08 and Zilog Z80 based MCUs.
143 It can be retargetted for other microprocessors, support for Microchip
144 PIC, Atmel AVR is under development.
145 The entire source code for the compiler is distributed under GPL.
146 SDCC uses ASXXXX & ASLINK, a Freeware, retargettable assembler & linker.
147 SDCC has extensive language extensions suitable for utilizing various microcont
148 rollers and underlying hardware effectively.
153 In addition to the MCU specific optimizations SDCC also does a host of standard
157 global sub expression elimination,
160 loop optimizations (loop invariant, strength reduction of induction variables
164 constant folding & propagation,
170 dead code elimination
180 For the back-end SDCC uses a global register allocation scheme which should
181 be well suited for other 8 bit MCUs.
186 The peep hole optimizer uses a rule based substitution mechanism which is
192 Supported data-types are:
195 char (8 bits, 1 byte),
198 short and int (16 bits, 2 bytes),
201 long (32 bit, 4 bytes)
208 The compiler also allows
210 inline assembler code
212 to be embedded anywhere in a function.
213 In addition, routines developed in assembly can also be called.
217 SDCC also provides an option (-
227 -cyclomatic) to report the relative complexity of a function.
228 These functions can then be further optimized, or hand coded in assembly
234 SDCC also comes with a companion source level debugger SDCDB, the debugger
235 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
240 The latest version can be downloaded from
241 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
251 Please note: the compiler will probably always be some steps ahead of this
256 \begin_inset LatexCommand \index{Status of documentation}
266 Obviously this has pros and cons
275 All packages used in this compiler system are
283 ; source code for all the sub-packages (pre-processor, assemblers, linkers
284 etc) is distributed with the package.
285 This documentation is maintained using a freeware word processor (LyX).
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of the GNU General Public License
289 \begin_inset LatexCommand \index{GNU General Public License, GPL}
293 as published by the Free Software Foundation; either version 2, or (at
294 your option) any later version.
295 This program is distributed in the hope that it will be useful, but WITHOUT
296 ANY WARRANTY; without even the implied warranty
297 \begin_inset LatexCommand \index{warranty}
301 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
302 See the GNU General Public License for more details.
303 You should have received a copy of the GNU General Public License along
304 with this program; if not, write to the Free Software Foundation, 59 Temple
305 Place - Suite 330, Boston, MA 02111-1307, USA.
306 In other words, you are welcome to use, share and improve this program.
307 You are forbidden to forbid anyone else to use, share and improve what
309 Help stamp out software-hoarding!
312 Typographic conventions
313 \begin_inset LatexCommand \index{Typographic conventions}
320 Throughout this manual, we will use the following convention.
321 Commands you have to type in are printed in
329 Code samples are printed in
334 Interesting items and new terms are printed in
339 Compatibility with previous versions
342 This version has numerous bug fixes compared with the previous version.
343 But we also introduced some incompatibilities with older versions.
344 Not just for the fun of it, but to make the compiler more stable, efficient
346 \begin_inset LatexCommand \index{ANSI-compliance}
351 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
355 for ANSI-Compliance).
361 short is now equivalent to int (16 bits), it used to be equivalent to char
362 (8 bits) which is not ANSI compliant
365 the default directory for gcc-builds where include, library and documentation
366 files are stored is now in /usr/local/share
369 char type parameters to vararg functions are casted to int unless explicitly
386 will push a as an int and as a char resp.
399 -regextend has been removed
412 -noregparms has been removed
425 -stack-after-data has been removed
430 <pending: more incompatibilities?>
436 What do you need before you start installation of SDCC? A computer, and
438 The preferred method of installation is to compile SDCC from source using
440 For Windows some pre-compiled binary distributions are available for your
442 You should have some experience with command line tools and compiler use.
448 The SDCC home page at
449 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
453 is a great place to find distribution sets.
454 You can also find links to the user mailing lists that offer help or discuss
455 SDCC with other SDCC users.
456 Web links to other SDCC related sites can also be found here.
457 This document can be found in the DOC directory of the source package as
459 A pdf version of this document is available at
460 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
465 Some of the other tools (simulator and assembler) included with SDCC contain
466 their own documentation and can be found in the source distribution.
467 If you want the latest unreleased software, the complete source package
468 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
471 Wishes for the future
474 There are (and always will be) some things that could be done.
475 Here are some I can think of:
482 char KernelFunction3(char p) at 0x340;
490 \begin_inset LatexCommand \index{code banking (not supported)}
500 If you can think of some more, please see the section
501 \begin_inset LatexCommand \ref{sub:Requesting-Features}
505 about filing feature requests
506 \begin_inset LatexCommand \index{Requesting features}
511 \begin_inset LatexCommand \index{Feature request}
521 \begin_inset LatexCommand \index{Installation}
528 For most users it is sufficient to skip to either section
529 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
534 \begin_inset LatexCommand \ref{sub:Windows-Install}
539 More detailled instructions follow below.
543 \begin_inset LatexCommand \index{Options SDCC configuration}
550 The install paths, search paths and other options are defined when running
552 The defaults can be overridden by:
554 \labelwidthstring 00.00.0000
566 -prefix see table below
568 \labelwidthstring 00.00.0000
580 -exec_prefix see table below
582 \labelwidthstring 00.00.0000
594 -bindir see table below
596 \labelwidthstring 00.00.0000
608 -datadir see table below
610 \labelwidthstring 00.00.0000
612 docdir environment variable, see table below
614 \labelwidthstring 00.00.0000
616 include_dir_suffix environment variable, see table below
618 \labelwidthstring 00.00.0000
620 lib_dir_suffix environment variable, see table below
622 \labelwidthstring 00.00.0000
624 sdccconf_h_dir_separator environment variable, either / or
629 This character will only be used in sdccconf.h; don't forget it's a C-header,
630 therefore a double-backslash is needed there.
632 \labelwidthstring 00.00.0000
644 -disable-mcs51-port Excludes the Intel mcs51 port
646 \labelwidthstring 00.00.0000
658 -disable-gbz80-port Excludes the Gameboy gbz80 port
660 \labelwidthstring 00.00.0000
672 -disable-z80-port Excludes the z80 port
674 \labelwidthstring 00.00.0000
686 -disable-avr-port Excludes the AVR port
688 \labelwidthstring 00.00.0000
700 -disable-ds390-port Excludes the DS390 port
702 \labelwidthstring 00.00.0000
714 -disable-hc08-port Excludes the HC08 port
716 \labelwidthstring 00.00.0000
728 -disable-pic-port Excludes the PIC port
730 \labelwidthstring 00.00.0000
742 -disable-xa51-port Excludes the XA51 port
744 \labelwidthstring 00.00.0000
756 -disable-ucsim Disables configuring and building of ucsim
758 \labelwidthstring 00.00.0000
770 -disable-device-lib-build Disables automatically building device libraries
772 \labelwidthstring 00.00.0000
784 -disable-packihx Disables building packihx
786 \labelwidthstring 00.00.0000
798 -enable-libgc Use the Bohem memory allocator.
799 Lower runtime footprint.
802 Furthermore the environment variables CC, CFLAGS, ...
803 the tools and their arguments can be influenced.
804 Please see `configure -
814 -help` and the man/info pages of `configure` for details.
818 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
819 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
820 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
821 At the moment it's not possible to change the default settings (it was
822 simply never required).
826 These configure options are compiled into the binaries, and can only be
827 changed by rerunning 'configure' and recompiling SDCC.
828 The configure options are written in
832 to distinguish them from run time environment variables (see section search
838 \begin_inset Quotes sld
842 \begin_inset Quotes srd
845 are used by the SDCC team to build the official Win32 binaries.
846 The SDCC team uses Mingw32 to build the official Windows binaries, because
853 a gcc compiler and last but not least
856 the binaries can be built by cross compiling on Sourceforge's compile farm.
859 See the examples, how to pass the Win32 settings to 'configure'.
860 The other Win32 builds using Borland, VC or whatever don't use 'configure',
861 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
873 <lyxtabular version="3" rows="8" columns="3">
875 <column alignment="block" valignment="top" leftline="true" width="0in">
876 <column alignment="block" valignment="top" leftline="true" width="0in">
877 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
878 <row topline="true" bottomline="true">
879 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
895 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
935 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
945 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
955 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
967 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
977 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1004 <row topline="true">
1005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1027 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1038 <row topline="true">
1039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1076 <row topline="true">
1077 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1104 <row topline="true" bottomline="true">
1105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1123 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1141 'configure' also computes relative paths.
1142 This is needed for full relocatability of a binary package and to complete
1143 search paths (see section search paths below):
1149 \begin_inset Tabular
1150 <lyxtabular version="3" rows="4" columns="3">
1152 <column alignment="block" valignment="top" leftline="true" width="0in">
1153 <column alignment="block" valignment="top" leftline="true" width="0in">
1154 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1155 <row topline="true" bottomline="true">
1156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1181 <row topline="true" bottomline="true">
1182 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1209 <row bottomline="true">
1210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1237 <row bottomline="true">
1238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1289 \begin_inset Quotes srd
1293 \begin_inset Quotes srd
1307 \begin_inset Quotes srd
1311 \begin_inset Quotes srd
1339 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1348 \begin_inset Quotes srd
1351 i586-mingw32msvc-gcc
1352 \begin_inset Quotes srd
1356 \begin_inset Quotes srd
1359 i586-mingw32msvc-g++
1360 \begin_inset Quotes srd
1368 \begin_inset Quotes srd
1371 i586-mingw32msvc-ranlib
1372 \begin_inset Quotes srd
1380 \begin_inset Quotes srd
1383 i586-mingw32msvc-strip
1384 \begin_inset Quotes srd
1402 \begin_inset Quotes srd
1406 \begin_inset Quotes srd
1424 \begin_inset Quotes srd
1428 \begin_inset Quotes srd
1436 \begin_inset Quotes srd
1440 \begin_inset Quotes srd
1448 \begin_inset Quotes srd
1452 \begin_inset Quotes srd
1460 \begin_inset Quotes srd
1464 \begin_inset Quotes srd
1471 sdccconf_h_dir_separator=
1472 \begin_inset Quotes srd
1484 \begin_inset Quotes srd
1501 -disable-device-lib-build
1529 -host=i586-mingw32msvc -
1539 -build=unknown-unknown-linux-gnu
1543 \begin_inset Quotes sld
1547 \begin_inset Quotes srd
1550 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1559 \begin_inset Quotes srd
1563 \begin_inset Quotes srd
1571 \begin_inset Quotes srd
1575 \begin_inset Quotes srd
1593 \begin_inset Quotes srd
1597 \begin_inset Quotes srd
1615 \begin_inset Quotes srd
1619 \begin_inset Quotes srd
1627 \begin_inset Quotes srd
1631 \begin_inset Quotes srd
1639 \begin_inset Quotes srd
1643 \begin_inset Quotes srd
1651 \begin_inset Quotes srd
1655 \begin_inset Quotes srd
1662 sdccconf_h_dir_separator=
1663 \begin_inset Quotes srd
1675 \begin_inset Quotes srd
1695 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1706 -C' turns on caching, which gives a little bit extra speed.
1707 However if options are changed, it can be necessary to delete the config.cache
1712 \begin_inset LatexCommand \label{sub:Install-paths}
1717 \begin_inset LatexCommand \index{Install paths}
1723 \added_space_top medskip \align center
1725 \begin_inset Tabular
1726 <lyxtabular version="3" rows="5" columns="4">
1728 <column alignment="center" valignment="top" leftline="true" width="0">
1729 <column alignment="center" valignment="top" leftline="true" width="0">
1730 <column alignment="center" valignment="top" leftline="true" width="0">
1731 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1732 <row topline="true" bottomline="true">
1733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1763 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1774 <row topline="true">
1775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1814 <row topline="true">
1815 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1830 $DATADIR/ $INCLUDE_DIR_SUFFIX
1833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1838 /usr/local/share/sdcc/include
1841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1854 <row topline="true">
1855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1870 $DATADIR/$LIB_DIR_SUFFIX
1873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1878 /usr/local/share/sdcc/lib
1881 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1894 <row topline="true" bottomline="true">
1895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1918 /usr/local/share/sdcc/doc
1921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1943 *compiler, preprocessor, assembler, and linker
1949 is auto-appended by the compiler, e.g.
1950 small, large, z80, ds390 etc
1953 The install paths can still be changed during `make install` with e.g.:
1956 make install prefix=$(HOME)/local/sdcc
1959 Of course this doesn't change the search paths compiled into the binaries.
1963 \begin_inset LatexCommand \label{sub:Search-Paths}
1968 \begin_inset LatexCommand \index{Search path}
1975 Some search paths or parts of them are determined by configure variables
1980 , see section above).
1981 Further search paths are determined by environment variables during runtime.
1984 The paths searched when running the compiler are as follows (the first catch
1990 Binary files (preprocessor, assembler and linker)
1996 \begin_inset Tabular
1997 <lyxtabular version="3" rows="4" columns="3">
1999 <column alignment="block" valignment="top" leftline="true" width="0in">
2000 <column alignment="block" valignment="top" leftline="true" width="0in">
2001 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2002 <row topline="true" bottomline="true">
2003 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2011 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2019 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2028 <row topline="true">
2029 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2058 <row topline="true">
2059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2064 Path of argv[0] (if available)
2067 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2075 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2084 <row topline="true" bottomline="true">
2085 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2093 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2101 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2126 \begin_inset Tabular
2127 <lyxtabular version="3" rows="6" columns="3">
2129 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2130 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2131 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2132 <row topline="true" bottomline="true">
2133 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2141 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2149 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2158 <row topline="true">
2159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2195 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2214 <row topline="true">
2215 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2240 <row topline="true">
2241 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2255 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2267 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2278 <row topline="true">
2279 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2360 <row topline="true" bottomline="true">
2361 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2377 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2382 /usr/local/share/sdcc/
2387 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2415 -nostdinc disables the last two search paths.
2425 With the exception of
2426 \begin_inset Quotes sld
2440 \begin_inset Quotes srd
2447 is auto-appended by the compiler (e.g.
2448 small, large, z80, ds390 etc.).
2455 \begin_inset Tabular
2456 <lyxtabular version="3" rows="6" columns="3">
2458 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2459 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2460 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2461 <row topline="true" bottomline="true">
2462 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2470 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2487 <row topline="true">
2488 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2524 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2543 <row topline="true">
2544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2556 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2568 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2583 <row topline="true">
2584 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2595 $LIB_DIR_SUFFIX/<model>
2598 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2612 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2629 <row topline="true">
2630 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2645 $LIB_DIR_SUFFIX/<model>
2648 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2757 <row topline="true" bottomline="true">
2758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2767 $LIB_DIR_SUFFIX/<model>
2770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2775 /usr/local/share/sdcc/
2782 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2800 Don't delete any of the stray spaces in the table above without checking
2801 the HTML output (last line)!
2817 -nostdlib disables the last two search paths.
2821 \begin_inset LatexCommand \index{Building SDCC}
2828 Building SDCC on Linux
2829 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2838 Download the source package
2840 either from the SDCC CVS repository or from the nightly snapshots
2842 , it will be named something like sdcc
2853 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2862 Bring up a command line terminal, such as xterm.
2867 Unpack the file using a command like:
2870 "tar -xvzf sdcc.src.tar.gz
2875 , this will create a sub-directory called sdcc with all of the sources.
2878 Change directory into the main SDCC directory, for example type:
2895 This configures the package for compilation on your system.
2911 All of the source packages will compile, this can take a while.
2927 This copies the binary executables, the include files, the libraries and
2928 the documentation to the install directories.
2929 Proceed with section
2930 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
2937 Building SDCC on OSX 2.x
2940 Follow the instruction for Linux.
2944 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2945 )) fails to compile SDCC.
2946 Fortunately there's also gcc 2.9.x installed, which works fine.
2947 This compiler can be selected by running 'configure' with:
2950 ./configure CC=gcc2 CXX=g++2
2953 Cross compiling SDCC on Linux for Windows
2956 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2957 See section 'Configure Options'.
2960 Building SDCC on Windows
2963 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2965 They use Unix-sockets, which are not available on Win32.
2968 Building SDCC using Cygwin and Mingw32
2971 For building and installing a Cygwin executable follow the instructions
2977 \begin_inset Quotes sld
2981 \begin_inset Quotes srd
2984 Win32-binary can be built, which will not need the Cygwin-DLL.
2985 For the necessary 'configure' options see section 'configure options' or
2986 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
2990 In order to install Cygwin on Windows download setup.exe from
2991 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
2997 \begin_inset Quotes sld
3000 default text file type
3001 \begin_inset Quotes srd
3005 \begin_inset Quotes sld
3009 \begin_inset Quotes srd
3012 and download/install at least the following packages.
3013 Some packages are selected by default, others will be automatically selected
3014 because of dependencies with the manually selected packages.
3015 Never deselect these packages!
3024 gcc ; version 3.x is fine, no need to use the old 2.9x
3027 binutils ; selected with gcc
3033 rxvt ; a nice console, which makes life much easier under windoze (see below)
3036 man ; not really needed for building SDCC, but you'll miss it sooner or
3040 less ; not really needed for building SDCC, but you'll miss it sooner or
3044 cvs ; only if you use CVS access
3047 If you want to develop something you'll need:
3050 python ; for the regression tests
3053 gdb ; the gnu debugger, together with the nice GUI
3054 \begin_inset Quotes sld
3058 \begin_inset Quotes srd
3064 openssh ; to access the CF or commit changes
3067 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3068 use autoconf-stable!
3071 rxvt is a nice console with history.
3072 Replace in your cygwin.bat the line
3091 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3094 -bg black -fg white -geometry 100x65 -e bash -
3107 Text selected with the mouse is automatically copied to the clipboard, pasting
3108 works with shift-insert.
3112 The other good tip is to make sure you have no //c/-style paths anywhere,
3113 use /cygdrive/c/ instead.
3114 Using // invokes a network lookup which is very slow.
3116 \begin_inset Quotes sld
3120 \begin_inset Quotes srd
3123 is too long, you can change it with e.g.
3129 SDCC sources use the unix line ending LF.
3130 Life is much easier, if you store the source tree on a drive which is mounted
3132 And use an editor which can handle LF-only line endings.
3133 Make sure not to commit files with windows line endings.
3134 The tabulator spacing
3135 \begin_inset LatexCommand \index{tabulator spacing (8)}
3139 used in the project is 8.
3142 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3147 Download the source package
3149 either from the SDCC CVS repository or from the
3150 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3156 , it will be named something like sdcc
3163 SDCC is distributed with all the projects, workspaces, and files you need
3164 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3165 doesn't build under MSVC).
3166 The workspace name is 'sdcc.dsw'.
3167 Please note that as it is now, all the executables are created in a folder
3171 Once built you need to copy the executables from sdcc
3175 bin before running SDCC.
3180 WARNING: Visual studio is very picky with line terminations; it expects
3181 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3182 If you are getting a message such as "This makefile was not generated by
3183 Developer Studio etc.
3185 \begin_inset Quotes srd
3188 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3189 need to convert the Unix style line endings to DOS style line endings.
3190 To do so you can use the
3191 \begin_inset Quotes sld
3195 \begin_inset Quotes srd
3198 utility freely available on the internet.
3199 Doug Hawkins reported in the sdcc-user list that this works:
3207 SDCC> unix2dos sdcc.dsw
3213 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3217 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3218 flex.exe, and gawk.exe.
3219 One good place to get them is
3220 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3228 Download the file UnxUtils
3229 \begin_inset LatexCommand \index{UnxUtils}
3234 Now you have to install the utilities and setup MSVC so it can locate the
3236 Here there are two alternatives (choose one!):
3243 a) Extract UnxUtils.zip to your C:
3245 hard disk PRESERVING the original paths, otherwise bison won't work.
3246 (If you are using WinZip make certain that 'Use folder names' is selected)
3250 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3251 in 'Show directories for:' select 'Executable files', and in the directories
3252 window add a new path: 'C:
3262 (As a side effect, you get a bunch of Unix utilities that could be useful,
3263 such as diff and patch.)
3270 This one avoids extracting a bunch of files you may not use, but requires
3275 a) Create a directory were to put the tools needed, or use a directory already
3283 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3284 to such directory WITHOUT preserving the original paths.
3285 (If you are using WinZip make certain that 'Use folder names' is not selected)
3289 c) Rename bison.exe to '_bison.exe'.
3293 d) Create a batch file 'bison.bat' in 'C:
3297 ' and add these lines:
3317 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3321 Steps 'c' and 'd' are needed because bison requires by default that the
3322 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3323 '/usr/local/share/' I think.
3324 So it is necessary to tell bison where those files are located if they
3325 are not in such directory.
3326 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3330 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3331 in 'Show directories for:' select 'Executable files', and in the directories
3332 window add a new path: 'c:
3335 Note that you can use any other path instead of 'c:
3337 util', even the path where the Visual C++ tools are, probably: 'C:
3341 Microsoft Visual Studio
3346 So you don't have to execute step 'e' :)
3350 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3351 the executables from sdcc
3355 bin, and you can compile using SDCC.
3358 Building SDCC Using Borland
3361 From the sdcc directory, run the command "make -f Makefile.bcc".
3362 This should regenerate all the .exe files in the bin directory except for
3363 sdcdb.exe (which currently doesn't build under Borland C++).
3366 If you modify any source files and need to rebuild, be aware that the dependenci
3367 es may not be correctly calculated.
3368 The safest option is to delete all .obj files and run the build again.
3369 From a Cygwin BASH prompt, this can easily be done with the command (be
3370 sure you are in the sdcc directory):
3380 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3382 ) -print -exec rm {}
3391 or on Windows NT/2000/XP from the command prompt with the command:
3398 del /s *.obj *.lib *.rul
3401 from the sdcc directory.
3404 Windows Install Using a Binary Package
3405 \begin_inset LatexCommand \label{sub:Windows-Install}
3412 Download the binary package from
3413 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3417 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3418 This should unpack to a group of sub-directories.
3419 An example directory structure after unpacking the mingw32 package is:
3424 bin for the executables, c:
3432 lib for the include and libraries.
3435 Adjust your environment variable PATH to include the location of the bin
3436 directory or start sdcc using the full path.
3439 Building the Documentation
3442 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3443 as changing into the doc directory and typing
3447 \begin_inset Quotes srd
3451 \begin_inset Quotes srd
3458 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3461 \begin_inset LatexCommand \url{www.lyx.org}
3465 as editor this is straightforward.
3466 Prebuilt documentation in html and pdf format is available from
3467 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3474 Reading the Documentation
3477 Currently reading the document in pdf format is recommended, as for unknown
3478 reason the hyperlinks are working there whereas in the html version they
3485 If you should know why please drop us a note
3489 You'll find the pdf version at
3490 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
3496 This documentation is in some aspects different from a commercial documentation:
3500 It tries to document SDCC for several processor architectures in one document
3501 (commercially these probably would be separate documents/products).
3503 \begin_inset LatexCommand \index{Status of documentation}
3507 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3509 Z80, PIC14, PIC16 and HC08.
3512 There are many references pointing away from this documentation.
3513 Don't let this distract you.
3515 was a reference like
3516 \begin_inset LatexCommand \url{www.opencores.org}
3520 together with a statement
3521 \begin_inset Quotes sld
3524 some processors which are targetted by SDCC can be implemented in a
3541 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3546 \begin_inset Quotes srd
3549 we expect you to have a quick look there and come back.
3550 If you read this you are on the right track.
3553 Some sections attribute more space to problems, restrictions and warnings
3554 than to the solution.
3557 The installation section and the section about the debugger is intimidating.
3560 There are still lots of typos and there are more different writing styles
3564 Testing the SDCC Compiler
3565 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3572 The first thing you should do after installing your SDCC compiler is to
3588 \begin_inset LatexCommand \index{version}
3595 at the prompt, and the program should run and tell you the version.
3596 If it doesn't run, or gives a message about not finding sdcc program, then
3597 you need to check over your installation.
3598 Make sure that the sdcc bin directory is in your executable search path
3599 defined by the PATH environment setting (
3604 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3611 Install trouble-shooting for suggestions
3614 Make sure that the sdcc program is in the bin folder, if not perhaps something
3615 did not install correctly.
3623 is commonly installed as described in section
3624 \begin_inset Quotes sld
3627 Install and search paths
3628 \begin_inset Quotes srd
3637 Make sure the compiler works on a very simple example.
3638 Type in the following test.c program using your favorite
3664 Compile this using the following command:
3673 If all goes well, the compiler will generate a test.asm and test.rel file.
3674 Congratulations, you've just compiled your first program with SDCC.
3675 We used the -c option to tell SDCC not to link the generated code, just
3676 to keep things simple for this step.
3684 The next step is to try it with the linker.
3694 If all goes well the compiler will link with the libraries and produce
3695 a test.ihx output file.
3700 (no test.ihx, and the linker generates warnings), then the problem is most
3709 usr/local/share/sdcc/lib directory
3716 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3723 Install trouble-shooting for suggestions).
3731 The final test is to ensure
3739 header files and libraries.
3740 Edit test.c and change it to the following:
3757 strcpy(str1, "testing");
3764 Compile this by typing
3771 This should generate a test.ihx output file, and it should give no warnings
3772 such as not finding the string.h file.
3773 If it cannot find the string.h file, then the problem is that
3777 cannot find the /usr/local/share/sdcc/include directory
3784 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3791 Install trouble-shooting section for suggestions).
3809 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3813 to find exactly where SDCC is looking for the include and lib files.
3816 Install Trouble-shooting
3817 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3822 \begin_inset LatexCommand \index{Install trouble-shooting}
3829 If SDCC does not build correctly
3832 A thing to try is starting from scratch by unpacking the .tgz source package
3833 again in an empty directory.
3841 ./configure 2>&1 | tee configure.log
3855 make 2>&1 | tee make.log
3862 If anything goes wrong, you can review the log files to locate the problem.
3863 Or a relevant part of this can be attached to an email that could be helpful
3864 when requesting help from the mailing list.
3868 \begin_inset Quotes sld
3872 \begin_inset Quotes srd
3879 \begin_inset Quotes sld
3883 \begin_inset Quotes srd
3886 command is a script that analyzes your system and performs some configuration
3887 to ensure the source package compiles on your system.
3888 It will take a few minutes to run, and will compile a few tests to determine
3889 what compiler features are installed.
3893 \begin_inset Quotes sld
3897 \begin_inset Quotes srd
3903 This runs the GNU make tool, which automatically compiles all the source
3904 packages into the final installed binary executables.
3908 \begin_inset Quotes sld
3912 \begin_inset Quotes erd
3918 This will install the compiler, other executables libraries and include
3919 files into the appropriate directories.
3921 \begin_inset LatexCommand \ref{sub:Install-paths}
3927 \begin_inset LatexCommand \ref{sub:Search-Paths}
3932 about install and search paths.
3934 On most systems you will need super-user privileges to do this.
3940 SDCC is not just a compiler, but a collection of tools by various developers.
3941 These include linkers, assemblers, simulators and other components.
3942 Here is a summary of some of the components.
3943 Note that the included simulator and assembler have separate documentation
3944 which you can find in the source package in their respective directories.
3945 As SDCC grows to include support for other processors, other packages from
3946 various developers are included and may have their own sets of documentation.
3950 You might want to look at the files which are installed in <installdir>.
3951 At the time of this writing, we find the following programs for gcc-builds:
3955 In <installdir>/bin:
3958 sdcc - The compiler.
3961 sdcpp - The C preprocessor.
3964 asx8051 - The assembler for 8051 type processors.
3971 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
3974 aslink -The linker for 8051 type processors.
3981 link-gbz80 - The Z80 and GameBoy Z80 linkers.
3984 s51 - The ucSim 8051 simulator.
3987 sdcdb - The source debugger.
3990 packihx - A tool to pack (compress) Intel hex files.
3993 In <installdir>/share/sdcc/include
3999 In <installdir>/share/sdcc/lib
4002 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4006 In <installdir>/share/sdcc/doc
4012 As development for other processors proceeds, this list will expand to include
4013 executables to support processors like AVR, PIC, etc.
4019 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4020 the assembler and linkage editor.
4023 sdcpp - The C-Preprocessor
4027 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4031 is a modified version of the GNU preprocessor.
4032 The C preprocessor is used to pull in #include sources, process #ifdef
4033 statements, #defines and so on.
4044 - The Assemblers and Linkage Editors
4047 This is retargettable assembler & linkage editor, it was developed by Alan
4049 John Hartman created the version for 8051, and I (Sandeep) have made some
4050 enhancements and bug fixes for it to work properly with SDCC.
4057 \begin_inset LatexCommand \index{s51}
4061 is a freeware, opensource simulator developed by Daniel Drotos (
4062 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
4067 The simulator is built as part of the build process.
4068 For more information visit Daniel's web site at:
4069 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4074 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4078 sdcdb - Source Level Debugger
4082 \begin_inset LatexCommand \index{sdcdb (debugger)}
4086 is the companion source level debugger.
4087 More about sdcdb in section
4088 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4093 The current version of the debugger uses Daniel's Simulator S51
4094 \begin_inset LatexCommand \index{s51}
4098 , but can be easily changed to use other simulators.
4108 Single Source File Projects
4111 For single source file 8051 projects the process is very simple.
4112 Compile your programs with the following command
4115 "sdcc sourcefile.c".
4119 This will compile, assemble and link your source file.
4120 Output files are as follows:
4124 \begin_inset LatexCommand \index{<file>.asm}
4129 \begin_inset LatexCommand \index{Assembler source}
4133 file created by the compiler
4137 \begin_inset LatexCommand \index{<file>.lst}
4142 \begin_inset LatexCommand \index{Assembler listing}
4146 file created by the Assembler
4150 \begin_inset LatexCommand \index{<file>.rst}
4155 \begin_inset LatexCommand \index{Assembler listing}
4159 file updated with linkedit information, created by linkage editor
4163 \begin_inset LatexCommand \index{<file>.sym}
4168 \begin_inset LatexCommand \index{Symbol listing}
4172 for the sourcefile, created by the assembler
4176 \begin_inset LatexCommand \index{<file>.rel}
4181 \begin_inset LatexCommand \index{<file>.o}
4186 \begin_inset LatexCommand \index{Object file}
4190 created by the assembler, input to Linkage editor
4194 \begin_inset LatexCommand \index{<file>.map}
4199 \begin_inset LatexCommand \index{Memory map}
4203 for the load module, created by the Linker
4207 \begin_inset LatexCommand \index{<file>.mem}
4211 - A file with a summary of the memory usage
4215 \begin_inset LatexCommand \index{<file>.ihx}
4219 - The load module in Intel hex format
4220 \begin_inset LatexCommand \index{Intel hex format}
4224 (you can select the Motorola S19 format
4225 \begin_inset LatexCommand \index{Motorola S19 format}
4240 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4245 If you need another format you might want to use
4252 \begin_inset LatexCommand \index{objdump (tool)}
4263 \begin_inset LatexCommand \index{srecord (tool)}
4268 Both formats are documented in the documentation of srecord
4269 \begin_inset LatexCommand \index{srecord (tool)}
4277 \begin_inset LatexCommand \index{<file>.adb}
4281 - An intermediate file containing debug information needed to create the
4293 \begin_inset LatexCommand \index{-\/-debug}
4301 \begin_inset LatexCommand \index{<file>.cdb}
4305 - An optional file (with -
4315 -debug) containing debug information.
4316 The format is documented in cdbfileformat.pdf.
4321 \begin_inset LatexCommand \index{<file> (no extension)}
4325 An optional AOMF or AOMF51
4326 \begin_inset LatexCommand \index{AOMF, AOMF51}
4330 file containing debug information (generated with option -
4357 ormat is commonly used by third party tools (debuggers
4358 \begin_inset LatexCommand \index{Debugger}
4362 , simulators, emulators)
4366 \begin_inset LatexCommand \index{<file>.dump*}
4370 - Dump file to debug the compiler it self (generated with option -
4380 -dumpall) (see section
4381 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4387 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4393 \begin_inset Quotes sld
4396 Anatomy of the compiler
4397 \begin_inset Quotes srd
4403 Projects with Multiple Source Files
4406 SDCC can compile only ONE file at a time.
4407 Let us for example assume that you have a project containing the following
4412 foo1.c (contains some functions)
4414 foo2.c (contains some more functions)
4416 foomain.c (contains more functions and the function main)
4424 The first two files will need to be compiled separately with the commands:
4456 Then compile the source file containing the
4461 \begin_inset LatexCommand \index{Linker}
4465 the files together with the following command:
4473 foomain.c\SpecialChar ~
4474 foo1.rel\SpecialChar ~
4479 \begin_inset LatexCommand \index{<file>.rel}
4491 can be separately compiled as well:
4502 sdcc foomain.rel foo1.rel foo2.rel
4509 The file containing the
4524 file specified in the command line, since the linkage editor processes
4525 file in the order they are presented to it.
4526 The linker is invoked from SDCC using a script file with extension .lnk
4527 \begin_inset LatexCommand \index{<file>.lnk}
4532 You can view this file to troubleshoot linking problems such as those arising
4533 from missing libraries.
4536 Projects with Additional Libraries
4537 \begin_inset LatexCommand \index{Libraries}
4544 Some reusable routines may be compiled into a library, see the documentation
4545 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4549 \begin_inset LatexCommand \index{<file>.lib}
4556 Libraries created in this manner can be included in the command line.
4557 Make sure you include the -L <library-path> option to tell the linker where
4558 to look for these files if they are not in the current directory.
4559 Here is an example, assuming you have the source file
4571 (if that is not the same as your current project):
4578 sdcc foomain.c foolib.lib -L mylib
4589 must be an absolute path name.
4593 The most efficient way to use libraries is to keep separate modules in separate
4595 The lib file now should name all the modules.rel
4596 \begin_inset LatexCommand \index{<file>.rel}
4601 For an example see the standard library file
4605 in the directory <installdir>/share/lib/small.
4608 Using sdcclib to Create and Manage Libraries
4609 \begin_inset LatexCommand \index{sdcclib}
4616 Alternatively, instead of having a .rel file for each entry on the library
4617 file as described in the preceding section, sdcclib can be used to embed
4618 all the modules belonging to such library in the library file itself.
4619 This results in a larger library file, but it greatly reduces the number
4620 of disk files accessed by the linker.
4621 Additionally, the packed library file contains an index of all include
4622 modules and symbols that significantly speeds up the linking process.
4623 To display a list of options supported by sdcclib type:
4632 \begin_inset LatexCommand \index{sdcclib}
4643 To create a new library file, start by compiling all the required modules.
4681 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4683 The next step is to add the .rel files to the library file:
4691 sdcclib libint.lib _divsint.rel
4694 \begin_inset LatexCommand \index{sdcclib}
4704 sdcclib libint.lib _divuint.rel
4710 sdcclib libint.lib _modsint.rel
4716 sdcclib libint.lib _moduint.rel
4722 sdcclib libint.lib _mulint.rel
4729 If the file already exists in the library, it will be replaced.
4730 To see what modules and symbols are included in the library, options -s
4731 and -m are available.
4739 sdcclib -s libint.lib
4742 \begin_inset LatexCommand \index{sdcclib}
4852 If the source files are compiled using -
4863 \begin_inset LatexCommand \index{-\/-debug}
4867 , the corresponding debug information file .adb will be include in the library
4869 The library files created with sdcclib are plain text files, so they can
4870 be viewed with a text editor.
4871 It is not recomended to modify a library file created with sdcclib using
4872 a text editor, as there are file indexes numbers located accross the file
4873 used by the linker to quickly locate the required module to link.
4874 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
4875 it can be safely deleted, since all the information required for linking
4876 is embedded in the library file itself.
4877 Library files created using sdcclib are used as described in the preceding
4881 Command Line Options
4882 \begin_inset LatexCommand \index{Command Line Options}
4889 Processor Selection Options
4890 \begin_inset LatexCommand \index{Options processor selection}
4895 \begin_inset LatexCommand \index{Processor selection options}
4901 \labelwidthstring 00.00.0000
4906 \begin_inset LatexCommand \index{-mmcs51}
4912 Generate code for the Intel MCS51
4913 \begin_inset LatexCommand \index{MCS51}
4917 family of processors.
4918 This is the default processor target.
4920 \labelwidthstring 00.00.0000
4925 \begin_inset LatexCommand \index{-mds390}
4931 Generate code for the Dallas DS80C390
4932 \begin_inset LatexCommand \index{DS80C390}
4938 \labelwidthstring 00.00.0000
4943 \begin_inset LatexCommand \index{-mds400}
4949 Generate code for the Dallas DS80C400
4950 \begin_inset LatexCommand \index{DS80C400}
4956 \labelwidthstring 00.00.0000
4961 \begin_inset LatexCommand \index{-mhc08}
4967 Generate code for the Motorola HC08
4968 \begin_inset LatexCommand \index{HC08}
4972 family of processors (added Oct 2003).
4974 \labelwidthstring 00.00.0000
4979 \begin_inset LatexCommand \index{-mz80}
4985 Generate code for the Zilog Z80
4986 \begin_inset LatexCommand \index{Z80}
4990 family of processors.
4992 \labelwidthstring 00.00.0000
4997 \begin_inset LatexCommand \index{-mgbz80}
5003 Generate code for the GameBoy Z80
5004 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5008 processor (Not actively maintained).
5010 \labelwidthstring 00.00.0000
5015 \begin_inset LatexCommand \index{-mavr}
5021 Generate code for the Atmel AVR
5022 \begin_inset LatexCommand \index{AVR}
5026 processor (In development, not complete).
5027 AVR users should probably have a look at avr-gcc
5028 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
5033 \begin_inset LatexCommand \url{http://winavr.sourceforge.net}
5040 I think it is fair to direct users there for now.
5041 Open source is also about avoiding unnecessary work .
5042 But I didn't find the 'official' link.
5044 \labelwidthstring 00.00.0000
5049 \begin_inset LatexCommand \index{-mpic14}
5055 Generate code for the Microchip PIC 14
5056 \begin_inset LatexCommand \index{PIC14}
5060 -bit processors (p16f84 and variants.
5061 In development, not complete).
5064 p16f627 p16f628 p16f84 p16f873 p16f877?
5066 \labelwidthstring 00.00.0000
5071 \begin_inset LatexCommand \index{-mpic16}
5077 Generate code for the Microchip PIC 16
5078 \begin_inset LatexCommand \index{PIC16}
5082 -bit processors (p18f452 and variants.
5083 In development, not complete).
5085 \labelwidthstring 00.00.0000
5091 Generate code for the Toshiba TLCS-900H
5092 \begin_inset LatexCommand \index{TLCS-900H}
5096 processor (Not maintained, not complete).
5098 \labelwidthstring 00.00.0000
5103 \begin_inset LatexCommand \index{-mxa51}
5109 Generate code for the Phillips XA51
5110 \begin_inset LatexCommand \index{XA51}
5114 processor (Not maintained, not complete).
5117 Preprocessor Options
5118 \begin_inset LatexCommand \index{Options preprocessor}
5123 \begin_inset LatexCommand \index{Preprocessor options}
5128 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5134 \labelwidthstring 00.00.0000
5139 \begin_inset LatexCommand \index{-I<path>}
5145 The additional location where the pre processor will look for <..h> or
5146 \begin_inset Quotes eld
5150 \begin_inset Quotes erd
5155 \labelwidthstring 00.00.0000
5160 \begin_inset LatexCommand \index{-D<macro[=value]>}
5166 Command line definition of macros.
5167 Passed to the preprocessor.
5169 \labelwidthstring 00.00.0000
5174 \begin_inset LatexCommand \index{-M}
5180 Tell the preprocessor to output a rule suitable for make describing the
5181 dependencies of each object file.
5182 For each source file, the preprocessor outputs one make-rule whose target
5183 is the object file name for that source file and whose dependencies are
5184 all the files `#include'd in it.
5185 This rule may be a single line or may be continued with `
5187 '-newline if it is long.
5188 The list of rules is printed on standard output instead of the preprocessed
5191 \begin_inset LatexCommand \index{-E}
5197 \labelwidthstring 00.00.0000
5202 \begin_inset LatexCommand \index{-C}
5208 Tell the preprocessor not to discard comments.
5209 Used with the `-E' option.
5211 \labelwidthstring 00.00.0000
5216 \begin_inset LatexCommand \index{-MM}
5227 Like `-M' but the output mentions only the user header files included with
5229 \begin_inset Quotes eld
5233 System header files included with `#include <file>' are omitted.
5235 \labelwidthstring 00.00.0000
5240 \begin_inset LatexCommand \index{-Aquestion(answer)}
5246 Assert the answer answer for question, in case it is tested with a preprocessor
5247 conditional such as `#if #question(answer)'.
5248 `-A-' disables the standard assertions that normally describe the target
5251 \labelwidthstring 00.00.0000
5256 \begin_inset LatexCommand \index{-Umacro}
5262 Undefine macro macro.
5263 `-U' options are evaluated after all `-D' options, but before any `-include'
5264 and `-imacros' options.
5266 \labelwidthstring 00.00.0000
5271 \begin_inset LatexCommand \index{-dM}
5277 Tell the preprocessor to output only a list of the macro definitions that
5278 are in effect at the end of preprocessing.
5279 Used with the `-E' option.
5281 \labelwidthstring 00.00.0000
5286 \begin_inset LatexCommand \index{-dD}
5292 Tell the preprocessor to pass all macro definitions into the output, in
5293 their proper sequence in the rest of the output.
5295 \labelwidthstring 00.00.0000
5300 \begin_inset LatexCommand \index{-dN}
5311 Like `-dD' except that the macro arguments and contents are omitted.
5312 Only `#define name' is included in the output.
5314 \labelwidthstring 00.00.0000
5319 preprocessorOption[,preprocessorOption]
5322 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5327 Pass the preprocessorOption to the preprocessor
5332 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5337 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5338 Collection (gcc), if you need more dedicated options please refer to the
5340 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5348 \begin_inset LatexCommand \index{Options linker}
5353 \begin_inset LatexCommand \index{Linker options}
5359 \labelwidthstring 00.00.0000
5379 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5384 \begin_inset LatexCommand \index{-L -\/-lib-path}
5391 <absolute path to additional libraries> This option is passed to the linkage
5392 editor's additional libraries
5393 \begin_inset LatexCommand \index{Libraries}
5398 The path name must be absolute.
5399 Additional library files may be specified in the command line.
5400 See section Compiling programs for more details.
5402 \labelwidthstring 00.00.0000
5419 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5424 <Value> The start location of the external ram
5425 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5429 , default value is 0.
5430 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5440 -xram-loc 0x8000 or -
5452 \labelwidthstring 00.00.0000
5469 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5474 <Value> The start location of the code
5475 \begin_inset LatexCommand \index{code}
5479 segment, default value 0.
5480 Note when this option is used the interrupt vector table is also relocated
5481 to the given address.
5482 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5492 -code-loc 0x8000 or -
5504 \labelwidthstring 00.00.0000
5521 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5526 <Value> By default the stack
5527 \begin_inset LatexCommand \index{stack}
5531 is placed after the data segment.
5532 Using this option the stack can be placed anywhere in the internal memory
5534 The value entered can be in Hexadecimal or Decimal format, e.g.
5545 -stack-loc 0x20 or -
5556 Since the sp register is incremented before a push or call, the initial
5557 sp will be set to one byte prior the provided value.
5558 The provided value should not overlap any other memory areas such as used
5559 register banks or the data segment and with enough space for the current
5562 \labelwidthstring 00.00.0000
5579 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5584 <Value> The start location of the internal ram data
5585 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5590 The value entered can be in Hexadecimal or Decimal format, eg.
5612 (By default, the start location of the internal ram data segment is set
5613 as low as possible in memory, taking into account the used register banks
5614 and the bit segment at address 0x20.
5615 For example if register banks 0 and 1 are used without bit variables, the
5616 data segment will be set, if -
5626 -data-loc is not used, to location 0x10.)
5628 \labelwidthstring 00.00.0000
5645 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5650 <Value> The start location of the indirectly addressable internal ram
5651 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5655 of the 8051, default value is 0x80.
5656 The value entered can be in Hexadecimal or Decimal format, eg.
5667 -idata-loc 0x88 or -
5679 \labelwidthstring 00.00.0000
5696 <Value> The start location of the bit
5697 \begin_inset LatexCommand \index{bit}
5701 addressable internal ram of the 8051.
5707 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5710 \labelwidthstring 00.00.0000
5725 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5734 The linker output (final object code) is in Intel Hex format.
5735 \begin_inset LatexCommand \index{Intel hex format}
5739 This is the default option.
5740 The format itself is documented in the documentation of srecord
5741 \begin_inset LatexCommand \index{srecord (tool)}
5747 \labelwidthstring 00.00.0000
5762 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5771 The linker output (final object code) is in Motorola S19 format
5772 \begin_inset LatexCommand \index{Motorola S19 format}
5777 The format itself is documented in the documentation of srecord.
5779 \labelwidthstring 00.00.0000
5784 linkOption[,linkOption]
5787 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5792 Pass the linkOption to the linker.
5793 See file sdcc/as/doc/asxhtm.html for more on linker options.
5797 \begin_inset LatexCommand \index{Options MCS51}
5802 \begin_inset LatexCommand \index{MCS51 options}
5808 \labelwidthstring 00.00.0000
5823 \begin_inset LatexCommand \index{-\/-model-small}
5834 Generate code for Small Model programs, see section Memory Models for more
5836 This is the default model.
5838 \labelwidthstring 00.00.0000
5853 \begin_inset LatexCommand \index{-\/-model-large}
5859 Generate code for Large model programs, see section Memory Models for more
5861 If this option is used all source files in the project have to be compiled
5864 \labelwidthstring 00.00.0000
5879 \begin_inset LatexCommand \index{-\/-xstack}
5885 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5886 variables and passing parameters.
5888 \begin_inset LatexCommand \ref{sub:External-Stack}
5893 External Stack for more details.
5895 \labelwidthstring 00.00.0000
5913 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
5917 Causes the linker to check if the internal ram usage is within limits of
5920 \labelwidthstring 00.00.0000
5938 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
5942 Causes the linker to check if the external ram usage is within limits of
5945 \labelwidthstring 00.00.0000
5963 \begin_inset LatexCommand \index{-\/-code-size <Value>}
5967 Causes the linker to check if the code memory usage is within limits of
5970 \labelwidthstring 00.00.0000
5988 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
5992 Causes the linker to check if there is at minimum <Value> bytes for stack.
5994 \labelwidthstring 00.00.0000
6012 \begin_inset LatexCommand \index{-\/-pack-iram}
6016 Causes the linker use unused register banks for data variables or stack.
6019 DS390 / DS400 Options
6020 \begin_inset LatexCommand \index{Options DS390}
6025 \begin_inset LatexCommand \index{DS390 options}
6031 \labelwidthstring 00.00.0000
6048 \begin_inset LatexCommand \index{-\/-model-flat24}
6058 Generate 24-bit flat mode code.
6059 This is the one and only that the ds390 code generator supports right now
6060 and is default when using
6065 See section Memory Models for more details.
6067 \labelwidthstring 00.00.0000
6082 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6088 disable interrupts during ESP:SP updates.
6090 \labelwidthstring 00.00.0000
6107 \begin_inset LatexCommand \index{-\/-stack-10bit}
6111 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6112 This is the one and only that the ds390 code generator supports right now
6113 and is default when using
6118 In this mode, the stack is located in the lower 1K of the internal RAM,
6119 which is mapped to 0x400000.
6120 Note that the support is incomplete, since it still uses a single byte
6121 as the stack pointer.
6122 This means that only the lower 256 bytes of the potential 1K stack space
6123 will actually be used.
6124 However, this does allow you to reclaim the precious 256 bytes of low RAM
6125 for use for the DATA and IDATA segments.
6126 The compiler will not generate any code to put the processor into 10 bit
6128 It is important to ensure that the processor is in this mode before calling
6129 any re-entrant functions compiled with this option.
6130 In principle, this should work with the
6143 \begin_inset LatexCommand \index{-\/-stack-auto}
6149 option, but that has not been tested.
6150 It is incompatible with the
6163 \begin_inset LatexCommand \index{-\/-xstack}
6170 It also only makes sense if the processor is in 24 bit contiguous addressing
6183 -model-flat24 option
6187 \labelwidthstring 00.00.0000
6202 \begin_inset LatexCommand \index{-\/-stack-probe}
6208 insert call to function __stack_probe at each function prologue.
6210 \labelwidthstring 00.00.0000
6225 \begin_inset LatexCommand \index{-\/-tini-libid}
6231 <nnnn> LibraryID used in -mTININative.
6234 \labelwidthstring 00.00.0000
6249 \begin_inset LatexCommand \index{-\/-use-accelerator}
6255 generate code for DS390 Arithmetic Accelerator.
6260 \begin_inset LatexCommand \index{Options Z80}
6265 \begin_inset LatexCommand \index{Z80 options}
6271 \labelwidthstring 00.00.0000
6288 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6298 Force a called function to always save BC.
6300 \labelwidthstring 00.00.0000
6317 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6321 When linking, skip the standard crt0.o object file.
6322 You must provide your own crt0.o for your system when linking.
6326 Optimization Options
6327 \begin_inset LatexCommand \index{Options optimization}
6332 \begin_inset LatexCommand \index{Optimization options}
6338 \labelwidthstring 00.00.0000
6353 \begin_inset LatexCommand \index{-\/-nogcse}
6359 Will not do global subexpression elimination, this option may be used when
6360 the compiler creates undesirably large stack/data spaces to store compiler
6362 A warning message will be generated when this happens and the compiler
6363 will indicate the number of extra bytes it allocated.
6364 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6366 \begin_inset LatexCommand \index{\#pragma nogcse}
6370 can be used to turn off global subexpression elimination
6371 \begin_inset LatexCommand \index{Subexpression elimination}
6375 for a given function only.
6377 \labelwidthstring 00.00.0000
6392 \begin_inset LatexCommand \index{-\/-noinvariant}
6398 Will not do loop invariant optimizations, this may be turned off for reasons
6399 explained for the previous option.
6400 For more details of loop optimizations performed see Loop Invariants in
6402 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6407 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6409 \begin_inset LatexCommand \index{\#pragma noinvariant}
6413 can be used to turn off invariant optimizations for a given function only.
6415 \labelwidthstring 00.00.0000
6430 \begin_inset LatexCommand \index{-\/-noinduction}
6436 Will not do loop induction optimizations, see section strength reduction
6438 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6440 \begin_inset LatexCommand \index{\#pragma noinduction}
6444 can be used to turn off induction optimizations for a given function only.
6446 \labelwidthstring 00.00.0000
6461 \begin_inset LatexCommand \index{-\/-nojtbound}
6472 Will not generate boundary condition check when switch statements
6473 \begin_inset LatexCommand \index{switch statement}
6477 are implemented using jump-tables.
6479 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6484 Switch Statements for more details.
6485 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6487 \begin_inset LatexCommand \index{\#pragma nojtbound}
6491 can be used to turn off boundary checking for jump tables for a given function
6494 \labelwidthstring 00.00.0000
6509 \begin_inset LatexCommand \index{-\/-noloopreverse}
6518 Will not do loop reversal
6519 \begin_inset LatexCommand \index{Loop reversing}
6525 \labelwidthstring 00.00.0000
6542 \begin_inset LatexCommand \index{-\/-nolabelopt }
6546 Will not optimize labels (makes the dumpfiles more readable).
6548 \labelwidthstring 00.00.0000
6563 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6569 Will not memcpy initialized data from code space into xdata space.
6570 This saves a few bytes in code space if you don't have initialized data.
6572 \labelwidthstring 00.00.0000
6587 \begin_inset LatexCommand \index{-\/-nooverlay}
6593 The compiler will not overlay parameters and local variables of any function,
6594 see section Parameters and local variables for more details.
6596 \labelwidthstring 00.00.0000
6611 \begin_inset LatexCommand \index{-\/-no-peep}
6617 Disable peep-hole optimization.
6619 \labelwidthstring 00.00.0000
6636 \begin_inset LatexCommand \index{-\/-peep-file}
6641 <filename> This option can be used to use additional rules to be used by
6642 the peep hole optimizer.
6644 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6649 Peep Hole optimizations for details on how to write these rules.
6651 \labelwidthstring 00.00.0000
6666 \begin_inset LatexCommand \index{-\/-peep-asm}
6672 Pass the inline assembler code through the peep hole optimizer.
6673 This can cause unexpected changes to inline assembler code, please go through
6674 the peephole optimizer
6675 \begin_inset LatexCommand \index{Peephole optimizer}
6679 rules defined in the source file tree '<target>/peeph.def' before using
6684 \begin_inset LatexCommand \index{Options other}
6690 \labelwidthstring 00.00.0000
6706 \begin_inset LatexCommand \index{-\/-compile-only}
6711 \begin_inset LatexCommand \index{-c -\/-compile-only}
6717 will compile and assemble the source, but will not call the linkage editor.
6719 \labelwidthstring 00.00.0000
6738 \begin_inset LatexCommand \index{-\/-c1mode}
6744 reads the preprocessed source from standard input and compiles it.
6745 The file name for the assembler output must be specified using the -o option.
6747 \labelwidthstring 00.00.0000
6752 \begin_inset LatexCommand \index{-E}
6758 Run only the C preprocessor.
6759 Preprocess all the C source files specified and output the results to standard
6762 \labelwidthstring 00.00.0000
6768 \begin_inset LatexCommand \index{-o <path/file>}
6774 The output path resp.
6775 file where everything will be placed.
6776 If the parameter is a path, it must have a trailing slash (or backslash
6777 for the Windows binaries) to be recognized as a path.
6780 \labelwidthstring 00.00.0000
6795 \begin_inset LatexCommand \index{-\/-stack-auto}
6806 All functions in the source file will be compiled as
6811 \begin_inset LatexCommand \index{reentrant}
6816 the parameters and local variables will be allocated on the stack
6817 \begin_inset LatexCommand \index{stack}
6823 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
6827 Parameters and Local Variables for more details.
6828 If this option is used all source files in the project should be compiled
6832 \labelwidthstring 00.00.0000
6847 \begin_inset LatexCommand \index{-\/-callee-saves}
6851 function1[,function2][,function3]....
6854 The compiler by default uses a caller saves convention for register saving
6855 across function calls, however this can cause unnecessary register pushing
6856 & popping when calling small functions from larger functions.
6857 This option can be used to switch the register saving convention for the
6858 function names specified.
6859 The compiler will not save registers when calling these functions, no extra
6860 code will be generated at the entry & exit (function prologue
6863 \begin_inset LatexCommand \index{function prologue}
6872 \begin_inset LatexCommand \index{function epilogue}
6878 ) for these functions to save & restore the registers used by these functions,
6879 this can SUBSTANTIALLY reduce code & improve run time performance of the
6881 In the future the compiler (with inter procedural analysis) will be able
6882 to determine the appropriate scheme to use for each function call.
6883 DO NOT use this option for built-in functions such as _mulint..., if this
6884 option is used for a library function the appropriate library function
6885 needs to be recompiled with the same option.
6886 If the project consists of multiple source files then all the source file
6887 should be compiled with the same -
6897 -callee-saves option string.
6898 Also see #pragma\SpecialChar ~
6900 \begin_inset LatexCommand \index{\#pragma callee\_saves}
6906 \labelwidthstring 00.00.0000
6921 \begin_inset LatexCommand \index{-\/-debug}
6930 When this option is used the compiler will generate debug information.
6931 The debug information collected in a file with .cdb extension can be used
6933 For more information see documentation for SDCDB.
6934 Another file with no extension contains debug information in AOMF or AOMF51
6935 \begin_inset LatexCommand \index{AOMF, AOMF51}
6939 format which is commonly used by third party tools.
6941 \labelwidthstring 00.00.0000
6946 \begin_inset LatexCommand \index{-S}
6957 Stop after the stage of compilation proper; do not assemble.
6958 The output is an assembler code file for the input file specified.
6960 \labelwidthstring 00.00.0000
6975 \begin_inset LatexCommand \index{-\/-int-long-reent}
6981 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
6982 Note by default these libraries are compiled as non-reentrant.
6983 See section Installation for more details.
6985 \labelwidthstring 00.00.0000
7000 \begin_inset LatexCommand \index{-\/-cyclomatic}
7009 This option will cause the compiler to generate an information message for
7010 each function in the source file.
7011 The message contains some
7015 information about the function.
7016 The number of edges and nodes the compiler detected in the control flow
7017 graph of the function, and most importantly the
7019 cyclomatic complexity
7020 \begin_inset LatexCommand \index{Cyclomatic complexity}
7026 see section on Cyclomatic Complexity for more details.
7028 \labelwidthstring 00.00.0000
7043 \begin_inset LatexCommand \index{-\/-float-reent}
7049 Floating point library is compiled as reentrant
7050 \begin_inset LatexCommand \index{reentrant}
7055 See section Installation for more details.
7057 \labelwidthstring 00.00.0000
7072 \begin_inset LatexCommand \index{-\/-main-return}
7078 This option can be used if the code generated is called by a monitor program
7079 or if the main routine includes an endless loop.
7080 This option might result in slightly smaller code and save two bytes of
7082 The return from the 'main'
7083 \begin_inset LatexCommand \index{main return}
7087 function will return to the function calling main.
7088 The default setting is to lock up i.e.
7095 \labelwidthstring 00.00.0000
7110 \begin_inset LatexCommand \index{-\/-nostdincl}
7116 This will prevent the compiler from passing on the default include path
7117 to the preprocessor.
7119 \labelwidthstring 00.00.0000
7134 \begin_inset LatexCommand \index{-\/-nostdlib}
7140 This will prevent the compiler from passing on the default library
7141 \begin_inset LatexCommand \index{Libraries}
7147 \labelwidthstring 00.00.0000
7162 \begin_inset LatexCommand \index{-\/-verbose}
7168 Shows the various actions the compiler is performing.
7170 \labelwidthstring 00.00.0000
7175 \begin_inset LatexCommand \index{-V}
7181 Shows the actual commands the compiler is executing.
7183 \labelwidthstring 00.00.0000
7198 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7204 Hides your ugly and inefficient c-code from the asm file, so you can always
7205 blame the compiler :)
7207 \labelwidthstring 00.00.0000
7222 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7228 Will not include peep-hole comments in the generated files.
7230 \labelwidthstring 00.00.0000
7245 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7251 Include i-codes in the asm file.
7252 Sounds like noise but is most helpful for debugging the compiler itself.
7254 \labelwidthstring 00.00.0000
7269 \begin_inset LatexCommand \index{-\/-less-pedantic}
7275 Disable some of the more pedantic warnings
7276 \begin_inset LatexCommand \index{Warnings}
7280 (jwk burps: please be more specific here, please!).
7281 If you want rather more than less warnings you should consider using a
7282 separate tool dedicated to syntax checking like splint
7283 \begin_inset LatexCommand \url{www.splint.org}
7289 \labelwidthstring 00.00.0000
7304 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7310 Display the directories in the compiler's search path
7312 \labelwidthstring 00.00.0000
7327 \begin_inset LatexCommand \index{-\/-vc}
7333 Display errors and warnings using MSVC style, so you can use SDCC with
7336 \labelwidthstring 00.00.0000
7351 \begin_inset LatexCommand \index{-\/-use-stdout}
7357 Send errors and warnings to stdout instead of stderr.
7359 \labelwidthstring 00.00.0000
7364 asmOption[,asmOption]
7367 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7372 Pass the asmOption to the assembler
7373 \begin_inset LatexCommand \index{Options assembler}
7378 \begin_inset LatexCommand \index{Assembler options}
7383 See file sdcc/as/doc/asxhtm.html for assembler options.
7386 Intermediate Dump Options
7387 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7392 \begin_inset LatexCommand \index{Options intermediate dump}
7397 \begin_inset LatexCommand \index{Intermediate dump options}
7404 The following options are provided for the purpose of retargetting and debugging
7406 These provided a means to dump the intermediate code (iCode
7407 \begin_inset LatexCommand \index{iCode}
7411 ) generated by the compiler in human readable form at various stages of
7412 the compilation process.
7413 More on iCodes see chapter
7414 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7419 \begin_inset Quotes srd
7422 The anatomy of the compiler
7423 \begin_inset Quotes srd
7428 \labelwidthstring 00.00.0000
7443 \begin_inset LatexCommand \index{-\/-dumpraw}
7449 This option will cause the compiler to dump the intermediate code into
7452 <source filename>.dumpraw
7454 just after the intermediate code has been generated for a function, i.e.
7455 before any optimizations are done.
7457 \begin_inset LatexCommand \index{Basic blocks}
7461 at this stage ordered in the depth first number, so they may not be in
7462 sequence of execution.
7464 \labelwidthstring 00.00.0000
7479 \begin_inset LatexCommand \index{-\/-dumpgcse}
7485 Will create a dump of iCode's, after global subexpression elimination
7486 \begin_inset LatexCommand \index{Global subexpression elimination}
7492 <source filename>.dumpgcse.
7494 \labelwidthstring 00.00.0000
7509 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7515 Will create a dump of iCode's, after deadcode elimination
7516 \begin_inset LatexCommand \index{Dead-code elimination}
7522 <source filename>.dumpdeadcode.
7524 \labelwidthstring 00.00.0000
7539 \begin_inset LatexCommand \index{-\/-dumploop}
7548 Will create a dump of iCode's, after loop optimizations
7549 \begin_inset LatexCommand \index{Loop optimization}
7555 <source filename>.dumploop.
7557 \labelwidthstring 00.00.0000
7572 \begin_inset LatexCommand \index{-\/-dumprange}
7581 Will create a dump of iCode's, after live range analysis
7582 \begin_inset LatexCommand \index{Live range analysis}
7588 <source filename>.dumprange.
7590 \labelwidthstring 00.00.0000
7605 \begin_inset LatexCommand \index{-\/-dumlrange}
7611 Will dump the life ranges
7612 \begin_inset LatexCommand \index{Live range analysis}
7618 \labelwidthstring 00.00.0000
7633 \begin_inset LatexCommand \index{-\/-dumpregassign}
7642 Will create a dump of iCode's, after register assignment
7643 \begin_inset LatexCommand \index{Register assignment}
7649 <source filename>.dumprassgn.
7651 \labelwidthstring 00.00.0000
7666 \begin_inset LatexCommand \index{-\/-dumplrange}
7672 Will create a dump of the live ranges of iTemp's
7674 \labelwidthstring 00.00.0000
7689 \begin_inset LatexCommand \index{-\/-dumpall}
7700 Will cause all the above mentioned dumps to be created.
7703 Redirecting output on Windows Shells
7706 By default SDCC writes it's error messages to
7707 \begin_inset Quotes sld
7711 \begin_inset Quotes srd
7715 To force all messages to
7716 \begin_inset Quotes sld
7720 \begin_inset Quotes srd
7744 \begin_inset LatexCommand \index{-\/-use-stdout}
7749 Additionally, if you happen to have visual studio installed in your windows
7750 machine, you can use it to compile your sources using a custom build and
7766 \begin_inset LatexCommand \index{-\/-vc}
7771 Something like this should work:
7815 -model-large -c $(InputPath)
7818 Environment variables
7819 \begin_inset LatexCommand \index{Environment variables}
7826 SDCC recognizes the following environment variables:
7828 \labelwidthstring 00.00.0000
7833 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7839 SDCC installs a signal handler
7840 \begin_inset LatexCommand \index{signal handler}
7844 to be able to delete temporary files after an user break (^C) or an exception.
7845 If this environment variable is set, SDCC won't install the signal handler
7846 in order to be able to debug SDCC.
7848 \labelwidthstring 00.00.0000
7855 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7861 Path, where temporary files will be created.
7862 The order of the variables is the search order.
7863 In a standard *nix environment these variables are not set, and there's
7864 no need to set them.
7865 On Windows it's recommended to set one of them.
7867 \labelwidthstring 00.00.0000
7872 \begin_inset LatexCommand \index{SDCC\_HOME}
7879 \begin_inset LatexCommand \ref{sub:Install-paths}
7885 \begin_inset Quotes sld
7889 \begin_inset Quotes srd
7894 \labelwidthstring 00.00.0000
7899 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
7906 \begin_inset LatexCommand \ref{sub:Search-Paths}
7912 \begin_inset Quotes sld
7916 \begin_inset Quotes srd
7921 \labelwidthstring 00.00.0000
7926 \begin_inset LatexCommand \index{SDCC\_LIB}
7933 \begin_inset LatexCommand \ref{sub:Search-Paths}
7939 \begin_inset Quotes sld
7943 \begin_inset Quotes srd
7949 There are some more environment variables recognized by SDCC, but these
7950 are solely used for debugging purposes.
7951 They can change or disappear very quickly, and will never be documented.
7954 Storage Class Language Extensions
7957 MCS51/DS390 Storage Class
7958 \begin_inset LatexCommand \index{Storage class}
7965 In addition to the ANSI storage classes SDCC allows the following MCS51
7966 specific storage classes:
7967 \layout Subsubsection
7970 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
7975 \begin_inset LatexCommand \index{near (storage class)}
7986 storage class for the Small Memory model (
7994 can be used synonymously).
7995 Variables declared with this storage class will be allocated in the directly
7996 addressable portion of the internal RAM of a 8051, e.g.:
8001 data unsigned char test_data;
8004 Writing 0x01 to this variable generates the assembly code:
8009 75*00 01\SpecialChar ~
8015 \layout Subsubsection
8018 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8023 \begin_inset LatexCommand \index{far (storage class)}
8030 Variables declared with this storage class will be placed in the external
8036 storage class for the Large Memory model, e.g.:
8041 xdata unsigned char test_xdata;
8044 Writing 0x01 to this variable generates the assembly code:
8049 90s00r00\SpecialChar ~
8078 \layout Subsubsection
8081 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8088 Variables declared with this storage class will be allocated into the indirectly
8089 addressable portion of the internal ram of a 8051, e.g.:
8094 idata unsigned char test_idata;
8097 Writing 0x01 to this variable generates the assembly code:
8126 Please note, the first 128 byte of idata physically access the same RAM
8128 The original 8051 had 128 byte idata memory, nowadays most devices have
8129 256 byte idata memory.
8131 \begin_inset LatexCommand \index{stack}
8135 is located in idata memory.
8136 \layout Subsubsection
8139 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8146 Paged xdata access is currently not as straightforward as using the other
8147 addressing modes of a 8051.
8148 The following example writes 0x01 to the address pointed to.
8149 Please note, pdata access physically accesses xdata memory.
8150 The high byte of the address is determined by port P2
8151 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8155 (or in case of some 8051 variants by a separate Special Function Register,
8157 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8166 pdata unsigned char *test_pdata_ptr;
8178 test_pdata_ptr = (pdata *)0xfe;
8184 *test_pdata_ptr = 1;
8189 Generates the assembly code:
8194 75*01 FE\SpecialChar ~
8198 _test_pdata_ptr,#0xFE
8230 Be extremely carefull if you use pdata together with the -
8241 \begin_inset LatexCommand \index{-\/-xstack}
8246 \layout Subsubsection
8249 \begin_inset LatexCommand \index{code}
8256 'Variables' declared with this storage class will be placed in the code
8262 code unsigned char test_code;
8265 Read access to this variable generates the assembly code:
8270 90s00r6F\SpecialChar ~
8273 mov dptr,#_test_code
8302 indexed arrays of characters in code memory can be accessed efficiently:
8307 code char test_array[] = {'c','h','e','a','p'};
8310 Read access to this array using an 8-bit unsigned index generates the assembly
8327 90s00r41\SpecialChar ~
8330 mov dptr,#_test_array
8345 \layout Subsubsection
8348 \begin_inset LatexCommand \index{bit}
8355 This is a data-type and a storage class specifier.
8356 When a variable is declared as a bit, it is allocated into the bit addressable
8357 memory of 8051, e.g.:
8365 Writing 1 to this variable generates the assembly code:
8381 The bit addressable memory consists of 128 bits which are located from 0x20
8382 to 0x2f in data memory.
8385 Apart from this 8051 specific storage class most architectures support ANSI-C
8387 \begin_inset LatexCommand \index{bitfields}
8397 Not really meant as examples, but nevertheless showing what bitfields are
8398 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8402 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8403 signed modifier are implemented as unsigned.
8404 \layout Subsubsection
8407 \begin_inset LatexCommand \index{sfr}
8412 \begin_inset LatexCommand \index{sbit}
8419 Like the bit keyword,
8423 signifies both a data-type and storage class, they are used to describe
8444 variables of a 8051, eg:
8450 \begin_inset LatexCommand \index{at}
8454 0x80 P0;\SpecialChar ~
8455 /* special function register P0 at location 0x80 */
8457 sbit at 0xd7 CY; /* CY (Carry Flag
8458 \begin_inset LatexCommand \index{Flags}
8463 \begin_inset LatexCommand \index{Carry flag}
8470 Special function registers which are located on an address dividable by
8471 8 are bit-addressable, an
8475 addresses a specific bit within these sfr.
8476 \layout Subsubsection
8479 \begin_inset LatexCommand \index{Pointer}
8483 to MCS51/DS390 specific memory spaces
8486 SDCC allows (via language extensions) pointers to explicitly point to any
8487 of the memory spaces
8488 \begin_inset LatexCommand \index{Memory model}
8493 In addition to the explicit pointers, the compiler uses (by default) generic
8494 pointers which can be used to point to any of the memory spaces.
8498 Pointer declaration examples:
8503 /* pointer physically in internal ram pointing to object in external ram
8506 xdata unsigned char * data p;
8510 /* pointer physically in external ram pointing to object in internal ram
8513 data unsigned char * xdata p;
8517 /* pointer physically in code rom pointing to data in xdata space */
8519 xdata unsigned char * code p;
8523 /* pointer physically in code space pointing to data in code space */
8525 code unsigned char * code p;
8529 /* the following is a generic pointer physically located in xdata space
8535 Well you get the idea.
8540 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8553 The highest order byte of the
8557 pointers contains the data space information.
8558 Assembler support routines are called whenever data is stored or retrieved
8564 These are useful for developing reusable library
8565 \begin_inset LatexCommand \index{Libraries}
8570 Explicitly specifying the pointer type will generate the most efficient
8572 \layout Subsubsection
8574 Notes on MCS51 memory
8575 \begin_inset LatexCommand \index{MCS51 memory}
8582 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8583 RAM memory which is structured as follows:
8587 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8590 - Bytes 20-2F - 16 bytes to hold 128 bit
8591 \begin_inset LatexCommand \index{bit}
8597 - Bytes 30-7F - 80 bytes for general purpose use.
8602 Additionally some members of the MCS51 family may have up to 128 bytes of
8603 additional, indirectly addressable, internal RAM memory (
8608 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8613 Furthermore, some chips may have some built in external memory (
8618 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8622 ) which should not be confused with the internal, directly addressable RAM
8628 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8633 Sometimes this built in
8637 memory has to be activated before using it (you can probably find this
8638 information on the datasheet of the microcontroller your are using, see
8640 \begin_inset LatexCommand \ref{sub:Startup-Code}
8648 Normally SDCC will only use the first bank
8649 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8653 of registers (register bank 0), but it is possible to specify that other
8654 banks of registers should be used in interrupt
8655 \begin_inset LatexCommand \index{interrupt}
8660 By default, the compiler will place the stack after the last byte of allocated
8661 memory for variables.
8662 For example, if the first 2 banks of registers are used, and only four
8667 variables, it will position the base of the internal stack at address 20
8669 This implies that as the stack
8670 \begin_inset LatexCommand \index{stack}
8674 grows, it will use up the remaining register banks, and the 16 bytes used
8675 by the 128 bit variables, and 80 bytes for general purpose use.
8676 If any bit variables are used, the data variables will be placed after
8677 the byte holding the last bit variable.
8678 For example, if register banks 0 and 1 are used, and there are 9 bit variables
8683 variables will be placed starting at address 0x22.
8695 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
8699 to specify the start address of the
8713 -iram-size to specify the size of the total internal RAM (
8725 By default the 8051 linker will place the stack after the last byte of data
8738 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
8742 allows you to specify the start of the stack, i.e.
8743 you could start it after any data in the general purpose area.
8744 If your microcontroller has additional indirectly addressable internal
8749 ) you can place the stack on it.
8750 You may also need to use -
8761 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
8765 to set the start address of the external RAM (
8780 \begin_inset LatexCommand \index{-\/-data-loc}
8784 to specify its size.
8785 Same goes for the code memory, using -
8796 \begin_inset LatexCommand \index{-\/-data-loc}
8811 \begin_inset LatexCommand \index{-\/-data-loc}
8816 If in doubt, don't specify any options and see if the resulting memory
8817 layout is appropriate, then you can adjust it.
8820 The linker generates two files with memory allocation information.
8821 The first, with extension .map
8822 \begin_inset LatexCommand \index{<file>.map}
8826 shows all the variables and segments.
8827 The second with extension .mem
8828 \begin_inset LatexCommand \index{<file>.mem}
8832 shows the final memory layout.
8833 The linker will complain either if memory segments overlap, there is not
8834 enough memory, or there is not enough space for stack.
8835 If you get any linking warnings and/or errors related to stack or segments
8836 allocation, take a look at either the .map or .mem files to find out what
8838 The .mem file may even suggest a solution to the problem.
8841 Z80/Z180 Storage Class
8842 \begin_inset LatexCommand \index{Storage class}
8847 \layout Subsubsection
8850 \begin_inset LatexCommand \index{sfr}
8854 (in/out to 8-bit addresses)
8858 \begin_inset LatexCommand \index{Z80}
8862 family has separate address spaces for memory and
8872 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
8876 is accessed with special instructions, e.g.:
8881 sfr at 0x78 IoPort;\SpecialChar ~
8883 /* define a var in I/O space at 78h called IoPort */
8887 Writing 0x01 to this variable generates the assembly code:
8907 \layout Subsubsection
8910 \begin_inset LatexCommand \index{sfr}
8914 (in/out to 16-bit addresses)
8921 is used to support 16 bit addresses in I/O memory e.g.:
8927 \begin_inset LatexCommand \index{at}
8934 Writing 0x01 to this variable generates the assembly code:
8939 01 23 01\SpecialChar ~
8959 \layout Subsubsection
8962 \begin_inset LatexCommand \index{sfr}
8966 (in0/out0 to 8 bit addresses on Z180
8967 \begin_inset LatexCommand \index{Z180}
8972 \begin_inset LatexCommand \index{HD64180}
8979 The compiler option -
8989 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
8991 \begin_inset LatexCommand \index{\#pragma portmode}
8995 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9005 If you include the file z180.h this will be set automatically.
9009 \begin_inset LatexCommand \index{Storage class}
9014 \layout Subsubsection
9017 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9024 The data storage class declares a variable that resides in the first 256
9025 bytes of memory (the direct page).
9026 The HC08 is most efficient at accessing variables (especially pointers)
9028 \layout Subsubsection
9031 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9038 The xdata storage class declares a variable that can reside anywhere in
9040 This is the default if no storage class is specified.
9045 \begin_inset LatexCommand \index{Absolute addressing}
9052 Data items can be assigned an absolute address with the
9055 \begin_inset LatexCommand \index{at}
9061 keyword, in addition to a storage class, e.g.:
9067 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9072 \begin_inset LatexCommand \index{at}
9076 0x7ffe unsigned int chksum;
9079 In the above example the variable chksum will located at 0x7ffe and 0x7fff
9080 of the external ram.
9085 reserve any space for variables declared in this way (they are implemented
9086 with an equate in the assembler).
9087 Thus it is left to the programmer to make sure there are no overlaps with
9088 other variables that are declared without the absolute address.
9089 The assembler listing file (.lst
9090 \begin_inset LatexCommand \index{<file>.lst}
9094 ) and the linker output files (.rst
9095 \begin_inset LatexCommand \index{<file>.rst}
9100 \begin_inset LatexCommand \index{<file>.map}
9104 ) are good places to look for such overlaps.
9105 Variables with an absolute address are
9110 \begin_inset LatexCommand \index{Variable initialization}
9117 In case of memory mapped I/O devices the keyword
9121 have to be used to tell the compiler that accesses might not be optimized
9128 \begin_inset LatexCommand \index{volatile}
9133 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9138 \begin_inset LatexCommand \index{at}
9142 0x8000 unsigned char PORTA_8255;
9145 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9150 \begin_inset LatexCommand \index{Aligned array}
9157 starts at a block (256 byte) boundary
9158 \begin_inset LatexCommand \index{block boundary}
9163 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9169 Absolute addresses can be specified for variables in all storage classes,
9176 \begin_inset LatexCommand \index{bit}
9181 \begin_inset LatexCommand \index{at}
9188 The above example will allocate the variable at offset 0x02 in the bit-addressab
9190 There is no real advantage to assigning absolute addresses to variables
9191 in this manner, unless you want strict control over all the variables allocated.
9192 One possible use would be to write hardware portable code.
9193 For example, if you have a routine that uses one or more of the microcontroller
9194 I/O pins, and such pins are different for two different hardwares, you
9195 can declare the I/O pins in your routine using:
9200 extern volatile bit MOSI;\SpecialChar ~
9204 /* master out, slave in */
9206 extern volatile bit MISO;\SpecialChar ~
9210 /* master in, slave out */
9212 extern volatile bit MCLK;\SpecialChar ~
9220 /* Input and Output of a byte on a 3-wire serial bus.
9225 If needed adapt polarity of clock, polarity of data and bit order
9230 unsigned char spi_io(unsigned char out_byte)
9254 MOSI = out_byte & 0x80;
9284 /* _asm nop _endasm; */\SpecialChar ~
9292 /* for slow peripherals */
9343 Then, someplace in the code for the first hardware you would use
9348 bit at 0x80 MOSI;\SpecialChar ~
9352 /* I/O port 0, bit 0 */
9354 bit at 0x81 MISO;\SpecialChar ~
9358 /* I/O port 0, bit 1 */
9360 bit at 0x82 MCLK;\SpecialChar ~
9364 /* I/O port 0, bit 2 */
9367 Similarly, for the second hardware you would use
9372 bit at 0x83 MOSI;\SpecialChar ~
9376 /* I/O port 0, bit 3 */
9378 bit at 0x91 MISO;\SpecialChar ~
9382 /* I/O port 1, bit 1 */
9385 \begin_inset LatexCommand \index{bit}
9389 at 0x92 MCLK;\SpecialChar ~
9393 /* I/O port 1, bit 2 */
9396 and you can use the same hardware dependent routine without changes, as
9397 for example in a library.
9398 This is somehow similar to sbit, but only one absolute address has to be
9399 specified in the whole project.
9403 \begin_inset LatexCommand \index{Parameters}
9408 \begin_inset LatexCommand \index{function parameter}
9413 \begin_inset LatexCommand \index{local variables}
9418 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9425 Automatic (local) variables and parameters to functions can either be placed
9426 on the stack or in data-space.
9427 The default action of the compiler is to place these variables in the internal
9428 RAM (for small model) or external RAM (for large model).
9429 This in fact makes them similar to
9432 \begin_inset LatexCommand \index{static}
9438 so by default functions are non-reentrant
9439 \begin_inset LatexCommand \index{reentrant}
9448 They can be placed on the stack
9449 \begin_inset LatexCommand \index{stack}
9466 \begin_inset LatexCommand \index{-\/-stack-auto}
9474 #pragma\SpecialChar ~
9478 \begin_inset LatexCommand \index{\#pragma stackauto}
9485 \begin_inset LatexCommand \index{reentrant}
9491 keyword in the function declaration, e.g.:
9496 unsigned char foo(char i) reentrant
9510 Since stack space on 8051 is limited, the
9528 option should be used sparingly.
9529 Note that the reentrant keyword just means that the parameters & local
9530 variables will be allocated to the stack, it
9534 mean that the function is register bank
9535 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9544 \begin_inset LatexCommand \index{local variables}
9548 can be assigned storage classes and absolute
9549 \begin_inset LatexCommand \index{Absolute addressing}
9566 xdata unsigned char i;
9579 \begin_inset LatexCommand \index{at}
9583 0x31 unsigned char j;
9595 In the above example the variable
9599 will be allocated in the external ram,
9603 in bit addressable space and
9622 or when a function is declared as
9626 this should only be done for static variables.
9630 \begin_inset LatexCommand \index{function parameter}
9634 however are not allowed any storage class
9635 \begin_inset LatexCommand \index{Storage class}
9639 , (storage classes for parameters will be ignored), their allocation is
9640 governed by the memory model in use, and the reentrancy options.
9644 \begin_inset LatexCommand \label{sub:Overlaying}
9649 \begin_inset LatexCommand \index{Overlaying}
9657 \begin_inset LatexCommand \index{reentrant}
9661 functions SDCC will try to reduce internal ram space usage by overlaying
9662 parameters and local variables of a function (if possible).
9663 Parameters and local variables
9664 \begin_inset LatexCommand \index{local variables}
9668 of a function will be allocated to an overlayable segment if the function
9671 no other function calls and the function is non-reentrant and the memory
9673 \begin_inset LatexCommand \index{Memory model}
9680 If an explicit storage class
9681 \begin_inset LatexCommand \index{Storage class}
9685 is specified for a local variable, it will NOT be overlayed.
9688 Note that the compiler (not the linkage editor) makes the decision for overlayin
9690 Functions that are called from an interrupt service routine should be preceded
9691 by a #pragma\SpecialChar ~
9693 \begin_inset LatexCommand \index{\#pragma nooverlay}
9697 if they are not reentrant.
9700 Also note that the compiler does not do any processing of inline assembler
9701 code, so the compiler might incorrectly assign local variables and parameters
9702 of a function into the overlay segment if the inline assembler code calls
9703 other c-functions that might use the overlay.
9704 In that case the #pragma\SpecialChar ~
9705 nooverlay should be used.
9708 Parameters and local variables of functions that contain 16 or 32 bit multiplica
9710 \begin_inset LatexCommand \index{Multiplication}
9715 \begin_inset LatexCommand \index{Division}
9719 will NOT be overlayed since these are implemented using external functions,
9728 \begin_inset LatexCommand \index{\#pragma nooverlay}
9734 void set_error(unsigned char errcd)
9750 void some_isr () interrupt
9751 \begin_inset LatexCommand \index{interrupt}
9781 In the above example the parameter
9789 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
9791 not present, this could cause unpredictable runtime behavior when called
9792 from an interrupt service routine.
9793 The #pragma\SpecialChar ~
9794 nooverlay ensures that the parameters and local variables for
9795 the function are NOT overlayed.
9798 Interrupt Service Routines
9799 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
9821 outines to be coded in C, with some extended keywords.
9826 void timer_isr (void) interrupt 1 using 1
9840 The optional number following the
9843 \begin_inset LatexCommand \index{interrupt}
9849 keyword is the interrupt number this routine will service.
9850 When present, the compiler will insert a call to this routine in the interrupt
9851 vector table for the interrupt number specified.
9852 If you have multiple source files in your project, interrupt service routines
9853 can be present in any of them, but a prototype of the isr MUST be present
9854 or included in the file that contains the function
9863 keyword can be used to tell the compiler to use the specified register
9864 bank (8051 specific) when generating code for this function.
9870 Interrupt service routines open the door for some very interesting bugs:
9872 If an interrupt service routine changes variables which are accessed by
9873 other functions these variables have to be declared
9878 \begin_inset LatexCommand \index{volatile}
9886 If the access to these variables is not
9889 \begin_inset LatexCommand \index{atomic access}
9896 the processor needs more than one instruction for the access and could
9897 be interrupted while accessing the variable) the interrupt must be disabled
9898 during the access to avoid inconsistent data.
9899 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
9900 and should be protected by disabling interrupts.
9901 You're not automatically on the safe side if you use 8 bit variables though.
9902 We need an example here: f.e.
9903 on the 8051 the harmless looking
9904 \begin_inset Quotes srd
9914 \begin_inset Quotes sld
9923 \begin_inset Quotes srd
9933 \begin_inset Quotes sld
9936 from within an interrupt routine might get lost if the interrupt occurs
9939 \begin_inset Quotes sld
9944 counter\SpecialChar ~
9949 \begin_inset Quotes srd
9952 is not atomic on the 8051 even if
9956 is located in data memory.
9957 Bugs like these are hard to reproduce and can cause a lot of trouble.
9961 The return address and the registers used in the interrupt service routine
9962 are saved on the stack
9963 \begin_inset LatexCommand \index{stack}
9967 so there must be sufficient stack space.
9968 If there isn't variables or registers (or even the return address itself)
9975 \begin_inset LatexCommand \index{stack overflow}
9979 is most likely to happen if the interrupt occurs during the
9980 \begin_inset Quotes sld
9984 \begin_inset Quotes srd
9987 subroutine when the stack is already in use for f.e.
9988 many return addresses.
9991 A special note here, int (16 bit) and long (32 bit) integer division
9992 \begin_inset LatexCommand \index{Division}
9997 \begin_inset LatexCommand \index{Multiplication}
10002 \begin_inset LatexCommand \index{Modulus}
10007 \begin_inset LatexCommand \index{Floating point support}
10011 operations are implemented using external support routines developed in
10013 If an interrupt service routine needs to do any of these operations then
10014 the support routines (as mentioned in a following section) will have to
10015 be recompiled using the
10028 \begin_inset LatexCommand \index{-\/-stack-auto}
10034 option and the source file will need to be compiled using the
10049 \begin_inset LatexCommand \index{-\/-int-long-reent}
10056 Calling other functions from an interrupt service routine is not recommended,
10057 avoid it if possible.
10058 Note that when some function is called from an interrupt service routine
10059 it should be preceded by a #pragma\SpecialChar ~
10061 \begin_inset LatexCommand \index{\#pragma nooverlay}
10065 if it is not reentrant.
10066 Furthermore nonreentrant functions should not be called from the main program
10067 while the interrupt service routine might be active.
10073 \begin_inset LatexCommand \ref{sub:Overlaying}
10078 about Overlaying and section
10079 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10084 about Functions using private register banks.
10087 MCS51/DS390 Interrupt Service Routines
10090 Interrupt numbers and the corresponding address & descriptions for the Standard
10091 8051/8052 are listed below.
10092 SDCC will automatically adjust the interrupt vector table to the maximum
10093 interrupt number specified.
10099 \begin_inset Tabular
10100 <lyxtabular version="3" rows="7" columns="3">
10102 <column alignment="center" valignment="top" leftline="true" width="0in">
10103 <column alignment="center" valignment="top" leftline="true" width="0in">
10104 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10105 <row topline="true" bottomline="true">
10106 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10131 <row topline="true">
10132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10157 <row topline="true">
10158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10183 <row topline="true">
10184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10209 <row topline="true">
10210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10226 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10235 <row topline="true">
10236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10252 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10261 <row topline="true" bottomline="true">
10262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10278 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10296 If the interrupt service routine is defined without
10299 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10305 a register bank or with register bank 0 (
10309 0), the compiler will save the registers used by itself on the stack upon
10310 entry and restore them at exit, however if such an interrupt service routine
10311 calls another function then the entire register bank will be saved on the
10313 This scheme may be advantageous for small interrupt service routines which
10314 have low register usage.
10317 If the interrupt service routine is defined to be using a specific register
10322 & psw are saved and restored, if such an interrupt service routine calls
10323 another function (using another register bank) then the entire register
10324 bank of the called function will be saved on the stack.
10325 This scheme is recommended for larger interrupt service routines.
10328 HC08 Interrupt Service Routines
10331 Since the number of interrupts available is chip specific and the interrupt
10332 vector table always ends at the last byte of memory, the interrupt numbers
10333 corresponds to the interrupt vectors in reverse order of address.
10334 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10335 2 will use the interrupt vector at 0xfffa, and so on.
10336 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10337 this way; instead see section
10338 \begin_inset LatexCommand \ref{sub:Startup-Code}
10342 for details on customizing startup.
10345 Z80 Interrupt Service Routines
10348 The Z80 uses several different methods for determining the correct interrupt
10349 vector depending on the hardware implementation.
10350 Therefore, SDCC ignores the optional interrupt number and does not attempt
10351 to generate an interrupt vector table.
10354 By default, SDCC generates code for a maskable interrupt, which uses an
10355 RETI instruction to return from the interrupt.
10356 To write an interrupt handler for the non-maskable interrupt, which needs
10357 an RETN instruction instead, add the
10366 void nmi_isr (void) critical interrupt
10380 Enabling and Disabling Interrupts
10383 Critical Functions and Critical Statements
10386 A special keyword may be associated with a block or a function declaring
10392 SDCC will generate code to disable all interrupts
10393 \begin_inset LatexCommand \index{interrupt}
10397 upon entry to a critical function and restore the interrupt enable to the
10398 previous state before returning.
10399 Nesting critical functions will need one additional byte on the stack
10400 \begin_inset LatexCommand \index{stack}
10409 int foo () critical
10410 \begin_inset LatexCommand \index{critical}
10435 The critical attribute maybe used with other attributes like
10445 may also be used to disable interrupts more locally:
10453 More than one statement could have been included in the block.
10456 Enabling and Disabling Interrupts directly
10460 \begin_inset LatexCommand \index{interrupt}
10464 can also be disabled and enabled directly (8051):
10469 EA = 0;\SpecialChar ~
10532 EA = 1;\SpecialChar ~
10599 On other architectures which have seperate opcodes for enabling and disabling
10600 interrupts you might want to make use of defines with inline assembly
10601 \begin_inset LatexCommand \index{Assembler routines}
10611 \begin_inset LatexCommand \index{\_asm}
10620 \begin_inset LatexCommand \index{\_endasm}
10629 #define SEI _asm\SpecialChar ~
10641 Note: it is sometimes sufficient to disable only a specific interrupt source
10643 a timer or serial interrupt by manipulating an
10646 \begin_inset LatexCommand \index{interrupt mask}
10656 Usually the time during which interrupts are disabled should be kept as
10658 This minimizes both
10663 \begin_inset LatexCommand \index{interrupt latency}
10667 (the time between the occurrence of the interrupt and the execution of
10668 the first code in the interrupt routine) and
10673 \begin_inset LatexCommand \index{interrupt jitter}
10677 (the difference between the shortest and the longest interrupt latency).
10678 These really are something different, f.e.
10679 a serial interrupt has to be served before its buffer overruns so it cares
10680 for the maximum interrupt latency, whereas it does not care about jitter.
10681 On a loudspeaker driven via a digital to analog converter which is fed
10682 by an interrupt a latency of a few milliseconds might be tolerable, whereas
10683 a much smaller jitter will be very audible.
10686 You can reenable interrupts within an interrupt routine and on some architecture
10687 s you can make use of two (or more) levels of
10689 interrupt priorities
10692 \begin_inset LatexCommand \index{interrupt priority}
10697 On some architectures which don't support interrupt priorities these can
10698 be implemented by manipulating the interrupt mask and reenabling interrupts
10699 within the interrupt routine.
10700 Check there is sufficient space on the stack
10701 \begin_inset LatexCommand \index{stack}
10705 and don't add complexity unless you have to.
10709 Functions using private register banks
10710 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
10717 Some architectures have support for quickly changing register sets.
10718 SDCC supports this feature with the
10721 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10727 attribute (which tells the compiler to use a register bank
10728 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10732 other than the default bank zero).
10733 It should only be applied to
10736 \begin_inset LatexCommand \index{interrupt}
10742 functions (see footnote below).
10743 This will in most circumstances make the generated ISR code more efficient
10744 since it will not have to save registers on the stack.
10751 attribute will have no effect on the generated code for a
10755 function (but may occasionally be useful anyway
10761 possible exception: if a function is called ONLY from 'interrupt' functions
10762 using a particular bank, it can be declared with the same 'using' attribute
10763 as the calling 'interrupt' functions.
10764 For instance, if you have several ISRs using bank one, and all of them
10765 call memcpy(), it might make sense to create a specialized version of memcpy()
10766 'using 1', since this would prevent the ISR from having to save bank zero
10767 to the stack on entry and switch to bank zero before calling the function
10774 (pending: I don't think this has been done yet)
10781 function using a non-zero bank will assume that it can trash that register
10782 bank, and will not save it.
10783 Since high-priority interrupts
10784 \begin_inset LatexCommand \index{interrupt priority}
10788 can interrupt low-priority ones on the 8051 and friends, this means that
10789 if a high-priority ISR
10793 a particular bank occurs while processing a low-priority ISR
10797 the same bank, terrible and bad things can happen.
10798 To prevent this, no single register bank should be
10802 by both a high priority and a low priority ISR.
10803 This is probably most easily done by having all high priority ISRs use
10804 one bank and all low priority ISRs use another.
10805 If you have an ISR which can change priority at runtime, you're on your
10806 own: I suggest using the default bank zero and taking the small performance
10810 It is most efficient if your ISR calls no other functions.
10811 If your ISR must call other functions, it is most efficient if those functions
10812 use the same bank as the ISR (see note 1 below); the next best is if the
10813 called functions use bank zero.
10814 It is very inefficient to call a function using a different, non-zero bank
10820 \begin_inset LatexCommand \label{sub:Startup-Code}
10825 \begin_inset LatexCommand \index{Startup code}
10832 MCS51/DS390 Startup Code
10835 The compiler inserts a call to the C routine
10837 _sdcc_external_startup()
10838 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
10847 at the start of the CODE area.
10848 This routine is in the runtime library
10849 \begin_inset LatexCommand \index{Runtime library}
10854 By default this routine returns 0, if this routine returns a non-zero value,
10855 the static & global variable initialization will be skipped and the function
10856 main will be invoked.
10857 Otherwise static & global variables will be initialized before the function
10861 _sdcc_external_startup()
10863 routine to your program to override the default if you need to setup hardware
10864 or perform some other critical operation prior to static & global variable
10866 On some mcs51 variants xdata has to be explicitly enabled before it can
10867 be accessed, this is the place to do it.
10868 The startup code clears the complete 256 byte of idata memory, this might
10869 cause problems for 128 byte devices (endless loop reported for Chipcon
10873 See also the compiler option
10892 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
10897 \begin_inset LatexCommand \ref{sub:MCS51-variants}
10902 about MCS51-variants.
10908 The HC08 startup code follows the same scheme as the MCS51 startup code.
10914 On the Z80 the startup code is inserted by linking with crt0.o which is generated
10915 from sdcc/device/lib/z80/crt0.s.
10916 If you need a different startup code you can use the compiler option
10937 \begin_inset LatexCommand \index{-\/-no-std-crt0}
10941 and provide your own crt0.o.
10945 Inline Assembler Code
10946 \begin_inset LatexCommand \index{Assembler routines}
10953 A Step by Step Introduction
10954 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
10961 Starting from a small snippet of c-code this example shows for the MCS51
10962 how to use inline assembly, access variables, a function parameter and
10963 an array in xdata memory.
10964 The example uses an MCS51 here but is easily adapted for other architectures.
10965 This is a buffer routine which should be optimized:
10972 \begin_inset LatexCommand \index{far (storage class)}
10977 \begin_inset LatexCommand \index{at}
10982 \begin_inset LatexCommand \index{Aligned array}
10988 unsigned char head,tail;
10992 void to_buffer( unsigned char c )
11000 if( head != tail-1 )
11010 buf[ head++ ] = c;\SpecialChar ~
11014 /* access to a 256 byte aligned array */
11019 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11020 then a corresponding buffer.asm file is generated.
11021 We define a new function
11025 in file buffer.c in which we cut and paste the generated code, removing
11026 unwanted comments and some ':'.
11028 \begin_inset Quotes sld
11032 \begin_inset Quotes srd
11036 \begin_inset Quotes sld
11040 \begin_inset Quotes srd
11043 to the beginning and the end of the function body:
11049 /* With a cut and paste from the .asm file, we have something to start with.
11054 The function is not yet OK! (registers aren't saved) */
11056 void to_buffer_asm( unsigned char c )
11065 \begin_inset LatexCommand \index{\_asm}
11079 ;buffer.c if( head != tail-1 )
11127 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11128 \begin_inset LatexCommand \index{Aligned array}
11197 The new file buffer.c should compile with only one warning about the unreferenced
11198 function argument 'c'.
11199 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11200 (1) and finally have:
11206 unsigned char far at 0x7f00 buf[0x100];
11208 unsigned char head,tail;
11210 #define USE_ASSEMBLY (1)
11218 void to_buffer( unsigned char c )
11226 if( head != tail-1 )
11246 void to_buffer( unsigned char c )
11254 c; // to avoid warning: unreferenced function argument
11261 \begin_inset LatexCommand \index{\_asm}
11275 ; save used registers here.
11286 ; If we were still using r2,r3 we would have to push them here.
11289 ; if( head != tail-1 )
11332 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11356 ; buf[ head++ ] = c;
11367 a,dpl \SpecialChar ~
11374 ; dpl holds lower byte of function argument
11385 dpl,_head \SpecialChar ~
11388 ; buf is 0x100 byte aligned so head can be used directly
11430 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11442 ; restore used registers here
11455 The inline assembler code can contain any valid code understood by the assembler
11456 , this includes any assembler directives and comment lines
11462 The assembler does not like some characters like ':' or ''' in comments.
11463 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11467 The compiler does not do any validation of the code within the
11470 \begin_inset LatexCommand \index{\_asm}
11478 Specifically it will not know which registers are used and thus register
11480 \begin_inset LatexCommand \index{push/pop}
11484 has to be done manually.
11488 It is recommended that each assembly instruction (including labels) be placed
11489 in a separate line (as the example shows).
11503 \begin_inset LatexCommand \index{-\/-peep-asm}
11509 command line option is used, the inline assembler code will be passed through
11510 the peephole optimizer
11511 \begin_inset LatexCommand \index{Peephole optimizer}
11516 There are only a few (if any) cases where this option makes sense, it might
11517 cause some unexpected changes in the inline assembler code.
11518 Please go through the peephole optimizer rules defined in file
11522 before using this option.
11526 \begin_inset LatexCommand \label{sub:Naked-Functions}
11531 \begin_inset LatexCommand \index{Naked functions}
11538 A special keyword may be associated with a function declaring it as
11541 \begin_inset LatexCommand \index{\_naked}
11552 function modifier attribute prevents the compiler from generating prologue
11553 \begin_inset LatexCommand \index{function prologue}
11558 \begin_inset LatexCommand \index{function epilogue}
11562 code for that function.
11563 This means that the user is entirely responsible for such things as saving
11564 any registers that may need to be preserved, selecting the proper register
11565 bank, generating the
11569 instruction at the end, etc.
11570 Practically, this means that the contents of the function must be written
11571 in inline assembler.
11572 This is particularly useful for interrupt functions, which can have a large
11573 (and often unnecessary) prologue/epilogue.
11574 For example, compare the code generated by these two functions:
11580 \begin_inset LatexCommand \index{volatile}
11584 data unsigned char counter;
11588 void simpleInterrupt(void) interrupt
11589 \begin_inset LatexCommand \index{interrupt}
11607 void nakedInterrupt(void) interrupt 2 _naked
11616 \begin_inset LatexCommand \index{\_asm}
11633 _counter ; does not change flags, no need to save psw
11645 ; MUST explicitly include ret or reti in _naked function.
11652 \begin_inset LatexCommand \index{\_endasm}
11661 For an 8051 target, the generated simpleInterrupt looks like:
11802 whereas nakedInterrupt looks like:
11817 _counter ; does not change flags, no need to save psw
11835 ; MUST explicitly include ret or reti in _naked function
11838 The related directive #pragma exclude
11839 \begin_inset LatexCommand \index{\#pragma exclude}
11843 allows a more fine grained control over pushing & popping
11844 \begin_inset LatexCommand \index{push/pop}
11851 While there is nothing preventing you from writing C code inside a
11855 function, there are many ways to shoot yourself in the foot doing this,
11856 and it is recommended that you stick to inline assembler.
11859 Use of Labels within Inline Assembler
11862 SDCC allows the use of in-line assembler with a few restrictions regarding
11864 In older versions of the compiler all labels defined within inline assembler
11873 where nnnn is a number less than 100 (which implies a limit of utmost 100
11874 inline assembler labels
11888 \begin_inset LatexCommand \index{\_asm}
11918 \begin_inset LatexCommand \index{\_endasm}
11925 Inline assembler code cannot reference any C-Labels, however it can reference
11927 \begin_inset LatexCommand \index{Labels}
11931 defined by the inline assembler, e.g.:
11956 ; some assembler code
11976 /* some more c code */
11978 clabel:\SpecialChar ~
11980 /* inline assembler cannot reference this label */
11992 $0003: ;label (can be referenced by inline assembler only)
12004 /* some more c code */
12009 In other words inline assembly code can access labels defined in inline
12010 assembly within the scope of the function.
12011 The same goes the other way, i.e.
12012 labels defines in inline assembly can not be accessed by C statements.
12015 Interfacing with Assembler Code
12016 \begin_inset LatexCommand \index{Assembler routines}
12023 Global Registers used for Parameter Passing
12024 \begin_inset LatexCommand \index{Parameter passing}
12031 The compiler always uses the global registers
12034 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12039 \begin_inset LatexCommand \index{DPTR}
12044 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12053 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12059 to pass the first parameter to a routine.
12060 The second parameter onwards is either allocated on the stack (for reentrant
12071 -stack-auto is used) or in data / xdata memory (depending on the memory
12076 Assembler Routine (non-reentrant)
12079 In the following example
12080 \begin_inset LatexCommand \index{reentrant}
12085 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12089 the function c_func calls an assembler routine asm_func, which takes two
12091 \begin_inset LatexCommand \index{function parameter}
12100 extern int asm_func(unsigned char, unsigned char);
12104 int c_func (unsigned char i, unsigned char j)
12112 return asm_func(i,j);
12126 return c_func(10,9);
12131 The corresponding assembler function is:
12136 .globl _asm_func_PARM_2
12237 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12254 Note here that the return values
12255 \begin_inset LatexCommand \index{return value}
12259 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12261 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12262 b' & 'acc' for four byte values.
12265 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12266 the parameter number starting from 1, and counting from the left.
12267 The first parameter is passed in
12268 \begin_inset Quotes eld
12272 \begin_inset Quotes erd
12275 for a one byte parameter,
12276 \begin_inset Quotes eld
12280 \begin_inset Quotes erd
12284 \begin_inset Quotes eld
12288 \begin_inset Quotes erd
12291 for three bytes and
12292 \begin_inset Quotes eld
12296 \begin_inset Quotes erd
12299 for a four bytes parameter.
12300 The variable name for the second parameter will be _<function_name>_PARM_2.
12304 Assemble the assembler routine with the following command:
12311 asx8051 -losg asmfunc.asm
12318 Then compile and link the assembler routine to the C source file with the
12326 sdcc cfunc.c asmfunc.rel
12329 Assembler Routine (reentrant)
12333 \begin_inset LatexCommand \index{reentrant}
12338 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12342 the second parameter
12343 \begin_inset LatexCommand \index{function parameter}
12347 onwards will be passed on the stack, the parameters are pushed from right
12349 after the call the leftmost parameter will be on the top of the stack.
12350 Here is an example:
12355 extern int asm_func(unsigned char, unsigned char);
12359 int c_func (unsigned char i, unsigned char j) reentrant
12367 return asm_func(i,j);
12381 return c_func(10,9);
12386 The corresponding assembler routine is:
12486 The compiling and linking procedure remains the same, however note the extra
12487 entry & exit linkage required for the assembler code, _bp is the stack
12488 frame pointer and is used to compute the offset into the stack for parameters
12489 and local variables.
12493 \begin_inset LatexCommand \index{int (16 bit)}
12498 \begin_inset LatexCommand \index{long (32 bit)}
12505 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12506 multiplication and modulus operations are implemented by support routines.
12507 These support routines are all developed in ANSI-C to facilitate porting
12508 to other MCUs, although some model specific assembler optimizations are
12510 The following files contain the described routines, all of them can be
12511 found in <installdir>/share/sdcc/lib.
12517 \begin_inset Tabular
12518 <lyxtabular version="3" rows="11" columns="2">
12520 <column alignment="center" valignment="top" leftline="true" width="0">
12521 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12522 <row topline="true" bottomline="true">
12523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12544 <row topline="true">
12545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12558 16 bit multiplication
12562 <row topline="true">
12563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12571 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12576 signed 16 bit division (calls _divuint)
12580 <row topline="true">
12581 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12589 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12594 unsigned 16 bit division
12598 <row topline="true">
12599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12607 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12612 signed 16 bit modulus (calls _moduint)
12616 <row topline="true">
12617 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12625 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12630 unsigned 16 bit modulus
12634 <row topline="true">
12635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12648 32 bit multiplication
12652 <row topline="true">
12653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12661 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12666 signed 32 division (calls _divulong)
12670 <row topline="true">
12671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12684 unsigned 32 division
12688 <row topline="true">
12689 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12697 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12702 signed 32 bit modulus (calls _modulong)
12706 <row topline="true" bottomline="true">
12707 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12720 unsigned 32 bit modulus
12733 Since they are compiled as
12738 \begin_inset LatexCommand \index{reentrant}
12743 \begin_inset LatexCommand \index{interrupt}
12747 service routines should not do any of the above operations.
12748 If this is unavoidable then the above routines will need to be compiled
12762 \begin_inset LatexCommand \index{-\/-stack-auto}
12768 option, after which the source program will have to be compiled with
12781 \begin_inset LatexCommand \index{-\/-int-long-reent}
12788 Notice that you don't have to call these routines directly.
12789 The compiler will use them automatically every time an integer operation
12793 Floating Point Support
12794 \begin_inset LatexCommand \index{Floating point support}
12801 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
12802 floating point support routines are derived from gcc's floatlib.c and consist
12803 of the following routines:
12811 \begin_inset Tabular
12812 <lyxtabular version="3" rows="17" columns="2">
12814 <column alignment="center" valignment="top" leftline="true" width="0">
12815 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12816 <row topline="true" bottomline="true">
12817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12843 <row topline="true">
12844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12861 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12875 add floating point numbers
12879 <row topline="true">
12880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12897 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12911 subtract floating point numbers
12915 <row topline="true">
12916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12933 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12947 divide floating point numbers
12951 <row topline="true">
12952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12969 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12983 multiply floating point numbers
12987 <row topline="true">
12988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13019 convert floating point to unsigned char
13023 <row topline="true">
13024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13055 convert floating point to signed char
13059 <row topline="true">
13060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13077 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13091 convert floating point to unsigned int
13095 <row topline="true">
13096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13127 convert floating point to signed int
13131 <row topline="true">
13132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13172 convert floating point to unsigned long
13176 <row topline="true">
13177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13208 convert floating point to signed long
13212 <row topline="true">
13213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13230 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13244 convert unsigned char to floating point
13248 <row topline="true">
13249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13280 convert char to floating point number
13284 <row topline="true">
13285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13302 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13316 convert unsigned int to floating point
13320 <row topline="true">
13321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13338 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13352 convert int to floating point numbers
13356 <row topline="true">
13357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13388 convert unsigned long to floating point number
13392 <row topline="true" bottomline="true">
13393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13424 convert long to floating point number
13437 These support routines are developed in ANSI-C so there is room for space
13438 and speed improvement.
13439 Note if all these routines are used simultaneously the data space might
13441 For serious floating point usage it is recommended that the large model
13443 Also notice that you don't have to call this routines directly.
13444 The compiler will use them automatically every time a floating point operation
13449 \begin_inset LatexCommand \index{Libraries}
13458 <pending: this is messy and incomplete - a little more information is in
13459 sdcc/doc/libdoc.txt
13464 Compiler support routines (_gptrget, _mulint etc.)
13467 Stdclib functions (puts, printf, strcat etc.)
13468 \layout Subsubsection
13474 \begin_inset LatexCommand \index{<stdio.h>}
13478 As usual on embedded systems you have to provide your own
13481 \begin_inset LatexCommand \index{getchar()}
13490 \begin_inset LatexCommand \index{putchar()}
13497 SDCC does not know whether the system connects to a serial line with or
13498 without handshake, LCD, keyboard or other device.
13499 You'll find examples for serial routines f.e.
13500 in sdcc/device/lib.
13503 If you're short on memory you might want to use
13514 \begin_inset LatexCommand \index{printf()}
13521 For the mcs51 there is an assembly version
13525 which should fit the requirements of many embedded systems (by unsetting
13526 #defines it can be customized to
13530 support long variables and field widths).
13533 Math functions (sin, pow, sqrt etc.)
13540 \begin_inset LatexCommand \index{Libraries}
13544 included in SDCC should have a license at least as liberal as the GNU Lesser
13545 General Public License
13546 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
13557 license statements for the libraries are missing.
13558 sdcc/device/lib/ser_ir.c
13562 come with a GPL (as opposed to LGPL) License - this will not be liberal
13563 enough for many embedded programmers.
13566 If you have ported some library or want to share experience about some code
13568 falls into any of these categories Busses (I
13569 \begin_inset Formula $^{\textrm{2}}$
13572 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
13573 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
13574 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
13575 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
13580 would certainly like to hear about it.
13581 Programmers coding for embedded systems are not especially famous for being
13582 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
13583 e these references are very valuable.
13584 Let's help to create a climate where information is shared.
13590 MCS51 Memory Models
13591 \begin_inset LatexCommand \index{Memory model}
13596 \begin_inset LatexCommand \index{MCS51 memory model}
13601 \layout Subsubsection
13606 SDCC allows two memory models for MCS51 code,
13615 Modules compiled with different memory models should
13619 be combined together or the results would be unpredictable.
13620 The library routines supplied with the compiler are compiled as both small
13622 The compiled library modules are contained in separate directories as small
13623 and large so that you can link to either set.
13627 When the large model is used all variables declared without a storage class
13628 will be allocated into the external ram, this includes all parameters and
13629 local variables (for non-reentrant
13630 \begin_inset LatexCommand \index{reentrant}
13635 When the small model is used variables without storage class are allocated
13636 in the internal ram.
13639 Judicious usage of the processor specific storage classes
13640 \begin_inset LatexCommand \index{Storage class}
13644 and the 'reentrant' function type will yield much more efficient code,
13645 than using the large model.
13646 Several optimizations are disabled when the program is compiled using the
13647 large model, it is therefore recommended that the small model be used unless
13648 absolutely required.
13649 \layout Subsubsection
13652 \begin_inset LatexCommand \label{sub:External-Stack}
13657 \begin_inset LatexCommand \index{stack}
13662 \begin_inset LatexCommand \index{External stack (mcs51)}
13673 : this option wasn't maintained for a long time and is quite buggy.
13674 Small programs might work.
13675 You've been warned!
13678 The external stack (-
13689 \begin_inset LatexCommand \index{-\/-xstack}
13693 ) is located in pdata
13694 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
13698 memory (usually at the start of the external ram segment) and is 256 bytes
13710 -xstack option is used to compile the program, the parameters and local
13712 \begin_inset LatexCommand \index{local variables}
13716 of all reentrant functions are allocated in this area.
13717 This option is provided for programs with large stack space requirements.
13718 When used with the -
13729 \begin_inset LatexCommand \index{-\/-stack-auto}
13733 option, all parameters and local variables are allocated on the external
13734 stack (note: support libraries will need to be recompiled with the same
13738 The compiler outputs the higher order address byte of the external ram segment
13740 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
13745 \begin_inset LatexCommand \ref{sub:MCS51-variants}
13749 ), therefore when using the External Stack option, this port
13753 be used by the application program.
13757 \begin_inset LatexCommand \index{Memory model}
13762 \begin_inset LatexCommand \index{DS390 memory model}
13769 The only model supported is Flat 24
13770 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
13775 This generates code for the 24 bit contiguous addressing mode of the Dallas
13777 In this mode, up to four meg of external RAM or code space can be directly
13779 See the data sheets at www.dalsemi.com for further information on this part.
13783 Note that the compiler does not generate any code to place the processor
13784 into 24 bitmode (although
13788 in the ds390 libraries will do that for you).
13794 \begin_inset LatexCommand \index{Tinibios (DS390)}
13798 , the boot loader or similar code must ensure that the processor is in 24
13799 bit contiguous addressing mode before calling the SDCC startup code.
13817 option, variables will by default be placed into the XDATA segment.
13822 Segments may be placed anywhere in the 4 meg address space using the usual
13834 Note that if any segments are located above 64K, the -r flag must be passed
13835 to the linker to generate the proper segment relocations, and the Intel
13836 HEX output format must be used.
13837 The -r flag can be passed to the linker by using the option
13841 on the SDCC command line.
13842 However, currently the linker can not handle code segments > 64k.
13846 \begin_inset LatexCommand \index{Pragmas}
13853 SDCC supports the following #pragma directives:
13857 \begin_inset LatexCommand \index{\#pragma save}
13861 - this will save all current options to the save/restore stack.
13862 See #pragma\SpecialChar ~
13867 \begin_inset LatexCommand \index{\#pragma restore}
13871 - will restore saved options from the last save.
13872 saves & restores can be nested.
13873 SDCC uses a save/restore stack: save pushes current options to the stack,
13874 restore pulls current options from the stack.
13875 See #pragma\SpecialChar ~
13882 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13887 \begin_inset LatexCommand \index{function prologue}
13891 function1[,function2[,function3...]] - The compiler by default uses a caller
13892 saves convention for register saving across function calls, however this
13893 can cause unnecessary register pushing & popping
13894 \begin_inset LatexCommand \index{push/pop}
13898 when calling small functions from larger functions.
13899 This option can be used to switch off the register saving convention for
13900 the function names specified.
13901 The compiler will not save registers when calling these functions, extra
13902 code need to be manually inserted at the entry & exit for these functions
13903 to save & restore the registers used by these functions, this can SUBSTANTIALLY
13904 reduce code & improve run time performance of the generated code.
13905 In the future the compiler (with inter procedural analysis) may be able
13906 to determine the appropriate scheme to use for each function call.
13917 -callee-saves command line option is used, the function names specified
13918 in #pragma\SpecialChar ~
13920 \begin_inset LatexCommand \index{\#pragma callee\_saves}
13924 is appended to the list of functions specified in the command line.
13928 \begin_inset LatexCommand \index{\#pragma exclude}
13932 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
13933 of pairs of push/pop
13934 \begin_inset LatexCommand \index{push/pop}
13943 \begin_inset LatexCommand \index{interrupt}
13956 The directive should be placed immediately before the ISR function definition
13957 and it affects ALL ISR functions following it.
13958 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
13959 exclude\SpecialChar ~
13961 \begin_inset LatexCommand \index{\#pragma exclude}
13966 See also the related keyword _naked
13967 \begin_inset LatexCommand \index{\_naked}
13975 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
13979 - the compiler will not warn you anymore for obvious mistakes, you'r on
13984 \begin_inset LatexCommand \index{\#pragma nogcse}
13988 - will stop global common subexpression elimination.
13992 \begin_inset LatexCommand \index{\#pragma noinduction}
13996 - will stop loop induction optimizations.
14000 \begin_inset LatexCommand \index{\#pragma noinvariant}
14004 - will not do loop invariant optimizations.
14005 For more details see Loop Invariants in section
14006 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14014 \begin_inset LatexCommand \index{\#pragma noiv}
14018 - Do not generate interrupt
14019 \begin_inset LatexCommand \index{interrupt}
14023 vector table entries for all ISR functions defined after the pragma.
14024 This is useful in cases where the interrupt vector table must be defined
14025 manually, or when there is a secondary, manually defined interrupt vector
14027 for the autovector feature of the Cypress EZ-USB FX2).
14028 More elegantly this can be achieved by obmitting the optional interrupt
14029 number after the interrupt keyword, see section
14030 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14039 \begin_inset LatexCommand \index{\#pragma nojtbound}
14043 - will not generate code for boundary value checking, when switch statements
14044 are turned into jump-tables (dangerous).
14045 For more details see section
14046 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14054 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14058 - Will not do loop reversal optimization
14062 \begin_inset LatexCommand \index{\#pragma nooverlay}
14066 - the compiler will not overlay the parameters and local variables of a
14071 \begin_inset LatexCommand \index{\#pragma stackauto}
14086 \begin_inset LatexCommand \index{-\/-stack-auto}
14091 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
14095 Parameters and Local Variables.
14098 SDCPP supports the following #pragma directives:
14102 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
14106 (+ | -) - switch _asm _endasm block preprocessing on / off.
14110 The pragma's are intended to be used to turn-on or off certain optimizations
14111 which might cause the compiler to generate extra stack / data space to
14112 store compiler generated temporary variables.
14113 This usually happens in large functions.
14114 Pragma directives should be used as shown in the following example, they
14115 are used to control options & optimizations for a given function; pragmas
14116 should be placed before and/or after a function, placing pragma's inside
14117 a function body could have unpredictable results.
14123 \begin_inset LatexCommand \index{\#pragma save}
14134 /* save the current settings */
14137 \begin_inset LatexCommand \index{\#pragma nogcse}
14146 /* turnoff global subexpression elimination */
14148 #pragma noinduction
14149 \begin_inset LatexCommand \index{\#pragma noinduction}
14153 /* turn off induction optimizations */
14176 \begin_inset LatexCommand \index{\#pragma restore}
14180 /* turn the optimizations back on */
14183 The compiler will generate a warning message when extra space is allocated.
14184 It is strongly recommended that the save and restore pragma's be used when
14185 changing options for a function.
14188 Defines Created by the Compiler
14191 The compiler creates the following #defines
14192 \begin_inset LatexCommand \index{\#defines}
14197 \begin_inset LatexCommand \index{Defines created by the compiler}
14207 \begin_inset Tabular
14208 <lyxtabular version="3" rows="10" columns="2">
14210 <column alignment="center" valignment="top" leftline="true" width="0">
14211 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14212 <row topline="true" bottomline="true">
14213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14223 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14234 <row topline="true">
14235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14241 \begin_inset LatexCommand \index{SDCC}
14248 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14253 this Symbol is always defined
14257 <row topline="true">
14258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14264 \begin_inset LatexCommand \index{SDCC\_mcs51}
14269 \begin_inset LatexCommand \index{SDCC\_ds390}
14274 \begin_inset LatexCommand \index{SDCC\_z80}
14281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14286 depending on the model used (e.g.: -mds390
14290 <row topline="true">
14291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14297 \begin_inset LatexCommand \index{\_\_mcs51}
14302 \begin_inset LatexCommand \index{\_\_ds390}
14307 \begin_inset LatexCommand \index{\_\_hc08}
14312 \begin_inset LatexCommand \index{\_\_z80}
14319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14324 depending on the model used (e.g.
14329 <row topline="true">
14330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14336 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14366 <row topline="true">
14367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14373 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14403 <row topline="true">
14404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14410 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14440 <row topline="true">
14441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14447 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
14454 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14477 <row topline="true">
14478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14484 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
14491 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14504 <row topline="true" bottomline="true">
14505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14511 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
14518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14538 Notes on supported Processors
14542 \begin_inset LatexCommand \label{sub:MCS51-variants}
14547 \begin_inset LatexCommand \index{MCS51 variants}
14554 MCS51 processors are available from many vendors and come in many different
14556 While they might differ considerably in respect to Special Function Registers
14557 the core MCS51 is usually not modified or is kept compatible.
14561 pdata access by SFR
14564 With the upcome of devices with internal xdata and flash memory devices
14566 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14570 as dedicated I/O port is becoming more popular.
14571 Switching the high byte for pdata
14572 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14576 access which was formerly done by port P2 is then achieved by a Special
14578 \begin_inset LatexCommand \index{sfr}
14583 In well-established MCS51 tradition the address of this
14587 is where the chip designers decided to put it.
14588 Needless to say that they didn't agree on a common name either.
14589 So that the startup code can correctly initialize xdata variables, you
14590 should define an sfr with the name _XPAGE
14593 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
14599 at the appropriate location if the default, port P2, is not used for this.
14605 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
14610 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
14615 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
14618 For more exotic implementations further customizations may be needed.
14620 \begin_inset LatexCommand \ref{sub:Startup-Code}
14624 for other possibilities.
14627 Other Features available by SFR
14630 Some MCS51 variants offer features like Double DPTR
14631 \begin_inset LatexCommand \index{DPTR}
14635 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
14636 These are currently not used for the MCS51 port.
14637 If you absolutely need them you can fall back to inline assembly or submit
14644 The DS80C400 microcontroller has a rich set of peripherals.
14645 In its built-in ROM library it includes functions to access some of the
14646 features, among them is a TCP stack with IP4 and IP6 support.
14647 Library headers (currently in beta status) and other files are provided
14651 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
14659 The Z80 and gbz80 port
14662 SDCC can target both the Zilog
14663 \begin_inset LatexCommand \index{Z80}
14667 and the Nintendo Gameboy's Z80-like gbz80
14668 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
14673 The Z80 port is passed through the same
14676 \begin_inset LatexCommand \index{Regression test}
14682 as the MCS51 and DS390 ports, so floating point support, support for long
14683 variables and bitfield support is fine.
14684 See mailing lists and forums about interrupt routines.
14687 As always, the code is the authoritative reference - see z80/ralloc.c and
14690 \begin_inset LatexCommand \index{stack}
14694 frame is similar to that generated by the IAR Z80 compiler.
14695 IX is used as the base pointer, HL and IY are used as a temporary registers,
14696 and BC and DE are available for holding variables.
14698 \begin_inset LatexCommand \index{return value}
14702 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
14704 The gbz80 port use the same set of registers for the return values, but
14705 in a different order of significance: E (one byte), DE (two bytes), or
14712 The port to the Motorola HC08
14713 \begin_inset LatexCommand \index{HC08}
14717 family has been added in October 2003, and is still undergoing some basic
14719 The code generator is complete, but the register allocation is still quite
14721 Some of the SDCC's standard C library functions have embedded non-HC08
14722 inline assembly and so are not yet usable.
14733 \begin_inset LatexCommand \index{PIC14}
14737 port still requires a major effort from the development community.
14738 However it can work for very simple code.
14741 C code and 14bit PIC code page
14742 \begin_inset LatexCommand \index{code page (pic14)}
14747 \begin_inset LatexCommand \index{RAM bank (pic14)}
14754 The linker organizes allocation for the code page and RAM banks.
14755 It does not have intimate knowledge of the code flow.
14756 It will put all the code section of a single asm file into a single code
14758 In order to make use of multiple code pages, separate asm files must be
14760 The compiler treats all functions of a single C file as being in the same
14761 code page unless it is non static.
14762 The compiler treats all local variables of a single C file as being in
14763 the same RAM bank unless it is an extern.
14767 To get the best follow these guide lines:
14770 make local functions static, as non static functions require code page selection
14774 Make local variables static as extern variables require RAM bank selection
14778 For devices that have multiple code pages it is more efficient to use the
14779 same number of files as pages, i.e.
14780 for the 16F877 use 4 separate files and i.e.
14781 for the 16F874 use 2 separate files.
14782 This way the linker can put the code for each file into different code
14783 pages and the compiler can allocate reusable variables more efficiently
14784 and there's less page selection overhead.
14785 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
14786 instruction set) use 'unsigned char' whereever possible instead of 'int'.
14789 Creating a device include file
14792 For generating a device include file use the support perl script inc2h.pl
14793 kept in directory support/script.
14799 For the interrupt function, use the keyword 'interrupt'
14800 \begin_inset LatexCommand \index{interrupt}
14804 with level number of 0 (PIC14 only has 1 interrupt so this number is only
14805 there to avoid a syntax error - it ought to be fixed).
14811 void Intr(void) interrupt 0
14817 T0IF = 0; /* Clear timer interrupt */
14822 Linking and assembling
14825 For assembling you can use either GPUTILS'
14826 \begin_inset LatexCommand \index{gputils (pic tools)}
14830 gpasm.exe or MPLAB's mpasmwin.exe.
14831 GPUTILS is available from
14832 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
14837 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
14838 If you use MPLAB and an interrupt function then the linker script file
14839 vectors section will need to be enlarged to link with mplink.
14862 sdcc -S -V -mpic14 -p16F877 $<
14876 $(PRJ).hex: $(OBJS)
14886 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
14908 sdcc -S -V -mpic14 -p16F877 $<
14918 mpasmwin /q /o $*.asm
14922 $(PRJ).hex: $(OBJS)
14932 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
14935 Please note that indentations within a
14939 have to be done with a tabulator character.
14943 \begin_inset LatexCommand \index{PIC16}
14951 \begin_inset LatexCommand \index{PIC16}
14955 port is the portion of SDCC that is responsible to produce code for the
14957 \begin_inset LatexCommand \index{Microchip}
14961 (TM) microcontrollers with 16 bit core.
14962 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
14968 PIC16 port supports the standard command line arguments as supposed, with
14969 the exception of certain cases that will be mentioned in the following
14972 \labelwidthstring 00.00.0000
14984 -stack-auto Auto variables that are function parameters, will be saved on
14988 There is no need to specify this in the command line.
14990 \labelwidthstring 00.00.0000
15002 -float-reent All floating point functions are reentrant by default.
15005 There is no need to specifiy this in the command line.
15007 \labelwidthstring 00.00.0000
15019 -callee-saves See -
15031 \labelwidthstring 00.00.0000
15043 -all-callee-saves All function arguments are passed on stack by default.
15046 There is no need to specify this in the command line.
15048 \labelwidthstring 00.00.0000
15060 -fommit-frame-pointer Frame pointer will be omitted when the function uses
15061 no local variables.
15064 Port Specific Options
15065 \begin_inset LatexCommand \index{Options PIC16}
15072 The port specific options appear after the global options in the sdcc --help
15074 \layout Subsubsection
15079 General options enable certain port features and optimizations.
15081 \labelwidthstring 00.00.0000
15093 -pgen-bank Instructs the port to insert BANKSEL directives before instructions
15094 that use the Bank Select Register (BSR).
15096 \labelwidthstring 00.00.0000
15108 -pomit-config-words Instructs the port to omit the generation of the configurati
15111 \labelwidthstring 00.00.0000
15123 -pomit-ivt Instructs the port to omit the generation of the interrupt vectors
15125 \labelwidthstring 00.00.0000
15137 -pleave-reset-vector Used in conjuction with the previous command, instructs
15138 the port NOT to omit the reset vector.
15140 \labelwidthstring 00.00.0000
15152 -stack-model=[model] Used in conjuction with the command above.
15153 Defines the stack model to be used, valid stack models are :
15156 \labelwidthstring 00.00.0000
15162 Selects small stack model.
15163 8 bit stack and frame pointers.
15164 Supports 256 bytes stack size.
15166 \labelwidthstring 00.00.0000
15172 Selects large stack model.
15173 16 bit stack and frame pointers.
15174 Supports 65536 bytes stack size.
15177 \labelwidthstring 00.00.0000
15189 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
15190 unitialized data variables with [kword].
15191 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
15193 \labelwidthstring 00.00.0000
15205 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
15206 Useful for bootloaders.
15208 \labelwidthstring 00.00.0000
15220 -asm= sets the full path and name of an external assembler to call.
15222 \labelwidthstring 00.00.0000
15234 -link= sets the full path and name of an external linker to call.
15235 \layout Subsubsection
15240 Debugging options enable extra debugging information in the output files.
15242 \labelwidthstring 00.00.0000
15254 -debug-xtra Similar to -
15265 \begin_inset LatexCommand \index{-\/-debug}
15269 , but dumps more information.
15271 \labelwidthstring 00.00.0000
15283 -debug-ralloc Force register allocator to dump <source>.d file with debugging
15285 <source> is the name of the file compiled.
15287 \labelwidthstring 00.00.0000
15299 -pcode-verbose Enable pcode debugging information in translation.
15302 Preprocessor Macros
15305 PIC16 port defines the following preprocessor macros while translating a
15310 \begin_inset Tabular
15311 <lyxtabular version="3" rows="2" columns="2">
15313 <column alignment="center" valignment="top" leftline="true" width="0">
15314 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15315 <row topline="true" bottomline="true">
15316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15333 <row topline="true" bottomline="true">
15334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15347 MCU Identification.
15352 is the microcontrol identification number, i.e.
15368 \begin_inset LatexCommand \index{PIC16}
15372 port uses the following directories for searching header files and libraries.
15376 \begin_inset Tabular
15377 <lyxtabular version="3" rows="3" columns="4">
15379 <column alignment="center" valignment="top" leftline="true" width="0">
15380 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15381 <column alignment="center" valignment="top" width="0">
15382 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15383 <row topline="true" bottomline="true">
15384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15408 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15417 <row topline="true">
15418 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15423 PREFIX/sdcc/include/pic16
15426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15431 PIC16 specific headers
15434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15451 <row topline="true" bottomline="true">
15452 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15457 PREFIX/sdcc/lib/pic16
15460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15465 PIC16 specific libraries
15468 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15476 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15493 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
15500 PIC16 port currently supports the following pragmas:
15502 \labelwidthstring 00.00.0000
15504 stack pragma stack forces the code generator to initialize the stack & frame
15505 pointers at a specific address.
15506 This is an adhoc solution since gplink does not support yet stack.
15507 When the gplink issue is resolved the pragma will be deprecated
15515 It is important to initialize the stack, otherwise strange things can happen.
15516 Stack is not initialized by default because there are some sources that
15518 (like library sources)
15523 The stack pragma should be used only once in a project.
15524 Multiple pragmas may result in indeterminate behaviour of the program.
15532 /* initializes stack at RAM address 0x5ff */
15535 #pragma stack 0x5ff
15537 \labelwidthstring 00.00.0000
15539 udata pragma udata instructs the compiler to emit code so that linker will
15540 place a variable at a specific memory bank
15548 /* places variable foo at bank2 */
15551 #pragma udata bank2 foo
15557 In order for this pragma to work there are some changes that must be made
15558 in the .lkr script used in link stage.
15559 In the following example a sample .lkr file is shown:
15564 // Sample linker script for the PIC18F452 processor
15570 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
15573 CODEPAGE NAME=page START=0x2A END=0x7FFF
15576 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
15579 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
15582 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
15585 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
15588 ACCESSBANK NAME=accessram START=0x0 END=0x7F
15593 DATABANK NAME=gpr0 START=0x80 END=0xFF
15596 DATABANK NAME=gpr1 START=0x100 END=0x1FF
15599 DATABANK NAME=gpr2 START=0x200 END=0x2FF
15602 DATABANK NAME=gpr3 START=0x300 END=0x3FF
15605 DATABANK NAME=gpr4 START=0x400 END=0x4FF
15608 DATABANK NAME=gpr5 START=0x500 END=0x5FF
15611 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
15616 SECTION NAME=CONFIG ROM=config
15621 SECTION NAME=bank0 RAM=gpr0
15624 SECTION NAME=bank1 RAM=gpr1
15627 SECTION NAME=bank2 RAM=gpr2
15630 SECTION NAME=bank3 RAM=gpr3
15633 SECTION NAME=bank4 RAM=gpr4
15636 SECTION NAME=bank5 RAM=gpr5
15639 The linker will recognise the section name set in the pragma statement and
15640 will position the variable at the memory bank set with the RAM field at
15641 the SECTION line in the linker script file.
15645 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
15652 There is one main header file that can be included to the source files using
15659 This header file contains the definitions for the processor special registers,
15660 so it is necessary if the source accesses them.
15661 It can be included by adding the following line in the beginning of the
15665 #include <pic18fregs.h>
15668 The specific microcontroller is selected within the pic18fregs.h automatically,
15669 so the same source can be used with a variety of devices.
15675 The libraries that PIC16
15676 \begin_inset LatexCommand \index{PIC16}
15680 port depends on are the microcontroller device libraries which contain
15681 the symbol definitions for the microcontroller special function registers.
15682 These libraries have the format pic18fxxxx.lib, where
15686 is the microcontroller identification number.
15687 The specific library is selected automatically by the compiler at link
15688 stage according to the selected device.
15691 Libraries are created with gplib which is part of the gputils package
15692 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
15702 The following memory models are supported by the PIC16 port:
15711 Memory model affects the default size of pointers within the source.
15712 The sizes are shown in the next table:
15716 \begin_inset Tabular
15717 <lyxtabular version="3" rows="3" columns="3">
15719 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15720 <column alignment="center" valignment="top" leftline="true" width="0">
15721 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15722 <row topline="true" bottomline="true">
15723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15728 Pointer sizes according to memory model
15731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15748 <row topline="true" bottomline="true">
15749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15757 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15774 <row topline="true" bottomline="true">
15775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15783 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
15791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15807 It is advisable that all sources within a project are compiled with the
15809 If one wants to override the default memory model, this can be done by
15810 declaring a pointer as
15819 Far selects large memory model's pointers, while near selects small memory
15823 The standard device libraries (see
15824 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
15828 ) contain no reference to pointers, so they can be used with both memory
15835 The stack implementation for the PIC16 port uses two indirect registers,
15838 \labelwidthstring 00.00.0000
15840 FSR1 is assigned as stack pointer
15842 \labelwidthstring 00.00.0000
15844 FSR2 is assigned as frame pointer
15847 The following stack models are supported by the PIC16 port
15860 model means that only the FSRxL byte is used to access stack and frame,
15867 uses both FSRxL and FSRxH registers.
15868 The following table shows the stack/frame pointers sizes according to stack
15869 model and the maximum space they can address:
15873 \begin_inset Tabular
15874 <lyxtabular version="3" rows="3" columns="3">
15876 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15877 <column alignment="center" valignment="top" leftline="true" width="0">
15878 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15879 <row topline="true" bottomline="true">
15880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15885 Stack & Frame pointer sizes according to stack model
15888 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15905 <row topline="true">
15906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15931 <row topline="true" bottomline="true">
15932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15966 Currently stack and frame pointers should be initialized explicit by the
15967 user at the desired Data RAM position (see
15968 \begin_inset LatexCommand \ref{sub:PIC16_Pragmas}
15973 Uninitialized stack and frame pointers can result in unexpected behavior
15974 of the resulting binary.
15977 Function return values
15980 Return values from functions are placed to the appropriate registers following
15981 a modified Microchip policy optimized for SDCC.
15982 The following table shows these registers:
15986 \begin_inset Tabular
15987 <lyxtabular version="3" rows="6" columns="2">
15989 <column alignment="center" valignment="top" leftline="true" width="0">
15990 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15991 <row topline="true" bottomline="true">
15992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16005 destination register
16009 <row topline="true">
16010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16027 <row topline="true">
16028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16045 <row topline="true">
16046 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16054 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16063 <row topline="true">
16064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16077 FSR0L:PRODH:PRODL:WREG
16081 <row topline="true" bottomline="true">
16082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16095 on stack, FSR0 points to the beginning
16109 When entering an interrupt, currently the PIC16
16110 \begin_inset LatexCommand \index{PIC16}
16114 port automatically saves the following registers:
16126 PROD (PRODL and PRODH)
16129 FSR0 (FSR0L and FSR0H)
16132 These registers are restored upon return from the interrupt routine.
16136 When entering a high priority interrupt WREG, STATUS and BSR are not explicit
16138 The hardware shadow registers for WREG, STATUS and BSR are used in these
16148 NOTE that when the _naked attribute is specified for an interrupt routine,
16149 then NO registers are stored or restored.
16155 Debugging with SDCDB
16156 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
16161 \begin_inset LatexCommand \index{sdcdb (debugger)}
16168 SDCC is distributed with a source level debugger
16169 \begin_inset LatexCommand \index{Debugger}
16174 The debugger uses a command line interface, the command repertoire of the
16175 debugger has been kept as close to gdb
16176 \begin_inset LatexCommand \index{gdb}
16180 (the GNU debugger) as possible.
16181 The configuration and build process is part of the standard compiler installati
16182 on, which also builds and installs the debugger in the target directory
16183 specified during configuration.
16184 The debugger allows you debug BOTH at the C source and at the ASM source
16186 Sdcdb is available on Unix platforms only.
16189 Compiling for Debugging
16203 \begin_inset LatexCommand \index{-\/-debug}
16207 option must be specified for all files for which debug information is to
16209 The complier generates a .adb file for each of these files.
16210 The linker creates the .cdb
16211 \begin_inset LatexCommand \index{<file>.cdb}
16216 \begin_inset LatexCommand \index{<file>.adb}
16220 files and the address information.
16221 This .cdb is used by the debugger.
16224 How the Debugger Works
16237 -debug option is specified the compiler generates extra symbol information
16238 some of which are put into the assembler source and some are put into the
16240 Then the linker creates the .cdb file from the individual .adb files with
16241 the address information for the symbols.
16242 The debugger reads the symbolic information generated by the compiler &
16243 the address information generated by the linker.
16244 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
16245 execution is controlled by the debugger.
16246 When a command is issued for the debugger, it translates it into appropriate
16247 commands for the simulator.
16250 Starting the Debugger
16253 The debugger can be started using the following command line.
16254 (Assume the file you are debugging has the file name foo).
16268 The debugger will look for the following files.
16271 foo.c - the source file.
16274 foo.cdb - the debugger symbol information file.
16277 foo.ihx - the Intel hex format
16278 \begin_inset LatexCommand \index{Intel hex format}
16285 Command Line Options.
16298 -directory=<source file directory> this option can used to specify the directory
16300 The debugger will look into the directory list specified for source, cdb
16302 The items in the directory list must be separated by ':', e.g.
16303 if the source files can be in the directories /home/src1 and /home/src2,
16314 -directory option should be -
16324 -directory=/home/src1:/home/src2.
16325 Note there can be no spaces in the option.
16329 -cd <directory> - change to the <directory>.
16332 -fullname - used by GUI front ends.
16335 -cpu <cpu-type> - this argument is passed to the simulator please see the
16336 simulator docs for details.
16339 -X <Clock frequency > this options is passed to the simulator please see
16340 the simulator docs for details.
16343 -s <serial port file> passed to simulator see the simulator docs for details.
16346 -S <serial in,out> passed to simulator see the simulator docs for details.
16349 -k <port number> passed to simulator see the simulator docs for details.
16355 As mentioned earlier the command interface for the debugger has been deliberatel
16356 y kept as close the GNU debugger gdb, as possible.
16357 This will help the integration with existing graphical user interfaces
16358 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
16359 If you use a graphical user interface for the debugger you can skip the
16361 \layout Subsubsection*
16363 break [line | file:line | function | file:function]
16366 Set breakpoint at specified line or function:
16375 sdcdb>break foo.c:100
16377 sdcdb>break funcfoo
16379 sdcdb>break foo.c:funcfoo
16380 \layout Subsubsection*
16382 clear [line | file:line | function | file:function ]
16385 Clear breakpoint at specified line or function:
16394 sdcdb>clear foo.c:100
16396 sdcdb>clear funcfoo
16398 sdcdb>clear foo.c:funcfoo
16399 \layout Subsubsection*
16404 Continue program being debugged, after breakpoint.
16405 \layout Subsubsection*
16410 Execute till the end of the current function.
16411 \layout Subsubsection*
16416 Delete breakpoint number 'n'.
16417 If used without any option clear ALL user defined break points.
16418 \layout Subsubsection*
16420 info [break | stack | frame | registers ]
16423 info break - list all breakpoints
16426 info stack - show the function call stack.
16429 info frame - show information about the current execution frame.
16432 info registers - show content of all registers.
16433 \layout Subsubsection*
16438 Step program until it reaches a different source line.
16439 Note: pressing <return> repeats the last command.
16440 \layout Subsubsection*
16445 Step program, proceeding through subroutine calls.
16446 \layout Subsubsection*
16451 Start debugged program.
16452 \layout Subsubsection*
16457 Print type information of the variable.
16458 \layout Subsubsection*
16463 print value of variable.
16464 \layout Subsubsection*
16469 load the given file name.
16470 Note this is an alternate method of loading file for debugging.
16471 \layout Subsubsection*
16476 print information about current frame.
16477 \layout Subsubsection*
16482 Toggle between C source & assembly source.
16483 \layout Subsubsection*
16485 ! simulator command
16488 Send the string following '!' to the simulator, the simulator response is
16490 Note the debugger does not interpret the command being sent to the simulator,
16491 so if a command like 'go' is sent the debugger can loose its execution
16492 context and may display incorrect values.
16493 \layout Subsubsection*
16500 My name is Bobby Brown"
16503 Interfacing with XEmacs
16504 \begin_inset LatexCommand \index{XEmacs}
16509 \begin_inset LatexCommand \index{Emacs}
16516 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
16517 sdcdb.el and sdcdbsrc.el.
16518 These two files can be found in the $(prefix)/bin directory after the installat
16520 These files need to be loaded into XEmacs for the interface to work.
16521 This can be done at XEmacs startup time by inserting the following into
16522 your '.xemacs' file (which can be found in your HOME directory):
16528 (load-file sdcdbsrc.el)
16534 .xemacs is a lisp file so the () around the command is REQUIRED.
16535 The files can also be loaded dynamically while XEmacs is running, set the
16536 environment variable 'EMACSLOADPATH' to the installation bin directory
16537 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
16538 To start the interface enter the following command:
16552 You will prompted to enter the file name to be debugged.
16557 The command line options that are passed to the simulator directly are bound
16558 to default values in the file sdcdbsrc.el.
16559 The variables are listed below, these values maybe changed as required.
16562 sdcdbsrc-cpu-type '51
16565 sdcdbsrc-frequency '11059200
16568 sdcdbsrc-serial nil
16571 The following is a list of key mapping for the debugger interface.
16582 ;;key\SpecialChar ~
16596 binding\SpecialChar ~
16620 ;;---\SpecialChar ~
16634 -------\SpecialChar ~
16676 sdcdb-next-from-src\SpecialChar ~
16704 sdcdb-back-from-src\SpecialChar ~
16732 sdcdb-cont-from-src\SpecialChar ~
16742 SDCDB continue command
16760 sdcdb-step-from-src\SpecialChar ~
16788 sdcdb-whatis-c-sexp\SpecialChar ~
16798 SDCDB ptypecommand for data at
16865 sdcdbsrc-delete\SpecialChar ~
16879 SDCDB Delete all breakpoints if no arg
16928 given or delete arg (C-u arg x)
16946 sdcdbsrc-frame\SpecialChar ~
16961 SDCDB Display current frame if no arg,
17010 given or display frame arg
17077 sdcdbsrc-goto-sdcdb\SpecialChar ~
17087 Goto the SDCDB output buffer
17105 sdcdb-print-c-sexp\SpecialChar ~
17116 SDCDB print command for data at
17183 sdcdbsrc-goto-sdcdb\SpecialChar ~
17193 Goto the SDCDB output buffer
17211 sdcdbsrc-mode\SpecialChar ~
17227 Toggles Sdcdbsrc mode (turns it off)
17242 sdcdb-finish-from-src\SpecialChar ~
17250 SDCDB finish command
17265 sdcdb-break\SpecialChar ~
17283 Set break for line with point
17298 sdcdbsrc-mode\SpecialChar ~
17314 Toggle Sdcdbsrc mode
17329 sdcdbsrc-srcmode\SpecialChar ~
17352 Here are a few guidelines that will help the compiler generate more efficient
17353 code, some of the tips are specific to this compiler others are generally
17354 good programming practice.
17357 Use the smallest data type to represent your data-value.
17358 If it is known in advance that the value is going to be less than 256 then
17359 use an 'unsigned char' instead of a 'short' or 'int'.
17360 Please note, that ANSI C requires both signed and unsigned chars to be
17361 promoted to 'signed int' before doing any operation.
17362 This promotion can be omitted, if the result is the same.
17363 The effect of the promotion rules together with the sign-extension is often
17370 unsigned char uc = 0xfe;
17372 if (uc * uc < 0) /* this is true! */
17391 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
17401 (unsigned char) -12 / (signed char) -3 = ...
17404 No, the result is not 4:
17409 (int) (unsigned char) -12 / (int) (signed char) -3 =
17411 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
17413 (int) 0x00f4 / (int) 0xfffd =
17415 (int) 0x00f4 / (int) 0xfffd =
17417 (int) 244 / (int) -3 =
17419 (int) -81 = (int) 0xffaf;
17422 Don't complain, that gcc gives you a different result.
17423 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
17424 Therefore the results are different.
17427 \begin_inset Quotes sld
17431 \begin_inset Quotes srd
17437 If well-defined overflow characteristics are important and negative values
17438 are not, or if you want to steer clear of sign-extension problems when
17439 manipulating bits or bytes, use one of the corresponding unsigned types.
17440 (Beware when mixing signed and unsigned values in expressions, though.)
17442 Although character types (especially unsigned char) can be used as "tiny"
17443 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
17444 ble sign extension and increased code size.
17448 Use unsigned when it is known in advance that the value is not going to
17450 This helps especially if you are doing division or multiplication, bit-shifting
17451 or are using an array index.
17454 NEVER jump into a LOOP.
17457 Declare the variables to be local
17458 \begin_inset LatexCommand \index{local variables}
17462 whenever possible, especially loop control variables (induction).
17465 Since the compiler does not always do implicit integral promotion, the programme
17466 r should do an explicit cast when integral promotion is required.
17469 Reducing the size of division, multiplication & modulus operations can reduce
17470 code size substantially.
17471 Take the following code for example.
17477 foobar(unsigned int p1, unsigned char ch)
17485 unsigned char ch1 = p1 % ch ;
17496 For the modulus operation the variable ch will be promoted to unsigned int
17497 first then the modulus operation will be performed (this will lead to a
17498 call to support routine _moduint()), and the result will be casted to a
17500 If the code is changed to
17505 foobar(unsigned int p1, unsigned char ch)
17513 unsigned char ch1 = (unsigned char)p1 % ch ;
17524 It would substantially reduce the code generated (future versions of the
17525 compiler will be smart enough to detect such optimization opportunities).
17529 Have a look at the assembly listing to get a
17530 \begin_inset Quotes sld
17534 \begin_inset Quotes srd
17537 for the code generation.
17541 \begin_inset LatexCommand \index{Tools}
17545 included in the distribution
17549 \begin_inset Tabular
17550 <lyxtabular version="3" rows="12" columns="3">
17552 <column alignment="center" valignment="top" leftline="true" width="0pt">
17553 <column alignment="center" valignment="top" leftline="true" width="0pt">
17554 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
17555 <row topline="true" bottomline="true">
17556 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17572 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17581 <row topline="true">
17582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17595 Simulator for various architectures
17598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17607 <row topline="true">
17608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17616 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17621 header file conversion
17624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17629 sdcc/support/scripts
17633 <row topline="true">
17634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17647 header file conversion
17650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17655 sdcc/support/scripts
17659 <row topline="true">
17660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17694 <row topline="true">
17695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17729 <row topline="true">
17730 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17746 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17764 <row topline="true">
17765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17799 <row topline="true">
17800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17808 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17816 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17834 <row topline="true">
17835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17869 <row topline="true">
17870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17886 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17904 <row topline="true" bottomline="true">
17905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17949 \begin_inset LatexCommand \index{Documentation}
17953 included in the distribution
17957 \begin_inset Tabular
17958 <lyxtabular version="3" rows="10" columns="2">
17960 <column alignment="left" valignment="top" leftline="true" width="0">
17961 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
17962 <row topline="true" bottomline="true">
17963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17971 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17976 Where to get / filename
17980 <row topline="true">
17981 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17986 SDCC Compiler User Guide
17989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17994 You're reading it right now
17998 <row topline="true">
17999 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18007 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18016 <row topline="true">
18017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18022 ASXXXX Assemblers and ASLINK Relocating Linker
18025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18030 sdcc/as/doc/asxhtm.html
18034 <row topline="true">
18035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18040 SDCC regression test
18041 \begin_inset LatexCommand \index{Regression test}
18048 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18053 sdcc/doc/test_suite_spec.pdf
18057 <row topline="true">
18058 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18075 <row topline="true">
18076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18081 Notes on debugging with sdcdb
18082 \begin_inset LatexCommand \index{sdcdb (debugger)}
18089 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18094 sdcc/debugger/README
18098 <row topline="true">
18099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18104 Software simulator for microcontrollers
18107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18134 <row topline="true">
18135 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18140 Temporary notes on the pic16
18141 \begin_inset LatexCommand \index{PIC16}
18148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18153 sdcc/src/pic16/NOTES
18157 <row topline="true" bottomline="true">
18158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18163 SDCC internal documentation (debugging file format)
18166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18202 Related open source tools
18203 \begin_inset LatexCommand \index{Related tools}
18211 \begin_inset Tabular
18212 <lyxtabular version="3" rows="11" columns="3">
18214 <column alignment="center" valignment="top" leftline="true" width="0pt">
18215 <column alignment="block" valignment="top" leftline="true" width="30line%">
18216 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18217 <row topline="true" bottomline="true">
18218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18243 <row topline="true">
18244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18250 \begin_inset LatexCommand \index{gpsim (pic simulator)}
18257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18265 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18271 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
18279 <row topline="true">
18280 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18286 \begin_inset LatexCommand \index{gputils (pic tools)}
18293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18307 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
18315 <row topline="true">
18316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18338 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
18346 <row topline="true">
18347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18353 \begin_inset LatexCommand \index{indent (source formatting tool)}
18360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18365 Formats C source - Master of the white spaces
18368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18374 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
18382 <row topline="true">
18383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18389 \begin_inset LatexCommand \index{srecord (tool)}
18396 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18401 Object file conversion, checksumming, ...
18404 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18410 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
18418 <row topline="true">
18419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18425 \begin_inset LatexCommand \index{objdump (tool)}
18432 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18437 Object file conversion, ...
18440 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18445 Part of binutils (should be there anyway)
18449 <row topline="true">
18450 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18456 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
18463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18468 Source code documentation system
18471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18477 \begin_inset LatexCommand \url{http://www.doxygen.org}
18485 <row topline="true">
18486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18494 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18499 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
18502 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18508 \begin_inset LatexCommand \url{http://www.kdevelop.org}
18516 <row topline="true">
18517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18523 \begin_inset LatexCommand \index{splint (syntax checking tool)}
18530 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18535 Statically checks c sources (has anyone adapted splint for SDCC?)
18538 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18544 \begin_inset LatexCommand \url{http://www.splint.org}
18552 <row topline="true" bottomline="true">
18553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18559 \begin_inset LatexCommand \index{ddd (debugger)}
18566 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18571 Debugger, serves nicely as GUI to sdcdb
18572 \begin_inset LatexCommand \index{sdcdb (debugger)}
18579 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18585 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
18602 Related documentation / recommended reading
18606 \begin_inset Tabular
18607 <lyxtabular version="3" rows="6" columns="3">
18609 <column alignment="center" valignment="top" leftline="true" width="0pt">
18610 <column alignment="block" valignment="top" leftline="true" width="30line%">
18611 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18612 <row topline="true" bottomline="true">
18613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18638 <row topline="true">
18639 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18656 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18662 \begin_inset LatexCommand \index{C Reference card}
18669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18675 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
18683 <row topline="true">
18684 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18700 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18706 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
18714 <row topline="true">
18715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18727 Latest datasheet of the target CPU
18730 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18739 <row topline="true">
18740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18752 Revision history of datasheet
18755 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18764 <row topline="true" bottomline="true">
18765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18780 Advanced Compiler Design and Implementation
18783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18788 bookstore (very dedicated, probably read other books first)
18804 Some questions answered, some pointers given - it might be time to in turn
18812 can you solve your project with the selected microcontroller? Would you
18813 find out early or rather late that your target is too small/slow/whatever?
18814 Can you switch to a slightly better device if it doesn't fit?
18817 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
18818 and/or another programming language be more adequate? Would an operating
18819 system on the target device help?
18822 if you solved the problem, will the marketing department be happy?
18825 if the marketing department is happy, will customers be happy?
18828 if you're the project manager, marketing department and maybe even the customer
18829 in one person, have you tried to see the project from the outside?
18832 is the project done if you think it is done? Or is just that other interface/pro
18833 tocol/feature/configuration/option missing? How about website, manual(s),
18834 internationali(z|s)ation, packaging, labels, 2nd source for components,
18835 electromagnetic compatability/interference, documentation for production,
18836 production test software, update mechanism, patent issues?
18839 is your project adequately positioned in that magic triangle: fame, fortune,
18843 Maybe not all answers to these questions are known and some answers may
18848 , nevertheless knowing these questions may help you to avoid burnout
18854 burnout is bad for electronic devices, programmers and motorcycle tyres
18858 Chances are you didn't want to hear some of them...
18862 \begin_inset LatexCommand \index{Support}
18869 SDCC has grown to be a large project.
18870 The compiler alone (without the preprocessor, assembler and linker) is
18871 well over 100,000 lines of code (blank stripped).
18872 The open source nature of this project is a key to its continued growth
18874 You gain the benefit and support of many active software developers and
18876 Is SDCC perfect? No, that's why we need your help.
18877 The developers take pride in fixing reported bugs.
18878 You can help by reporting the bugs and helping other SDCC users.
18879 There are lots of ways to contribute, and we encourage you to take part
18880 in making SDCC a great software package.
18884 The SDCC project is hosted on the SDCC sourceforge site at
18885 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
18890 You'll find the complete set of mailing lists
18891 \begin_inset LatexCommand \index{Mailing list(s)}
18895 , forums, bug reporting system, patch submission
18896 \begin_inset LatexCommand \index{Patch submission}
18901 \begin_inset LatexCommand \index{download}
18905 area and cvs code repository
18906 \begin_inset LatexCommand \index{cvs code repository}
18914 \begin_inset LatexCommand \index{Bug reporting}
18919 \begin_inset LatexCommand \index{Reporting bugs}
18926 The recommended way of reporting bugs is using the infrastructure of the
18928 You can follow the status of bug reports there and have an overview about
18932 Bug reports are automatically forwarded to the developer mailing list and
18933 will be fixed ASAP.
18934 When reporting a bug, it is very useful to include a small test program
18935 (the smaller the better) which reproduces the problem.
18936 If you can isolate the problem by looking at the generated assembly code,
18937 this can be very helpful.
18938 Compiling your program with the -
18949 \begin_inset LatexCommand \index{-\/-dumpall}
18953 option can sometimes be useful in locating optimization problems.
18954 When reporting a bug please maker sure you:
18957 Attach the code you are compiling with SDCC.
18961 Specify the exact command you use to run SDCC, or attach your Makefile.
18965 Specify the SDCC version (type "
18971 "), your platform, and operating system.
18975 Provide an exact copy of any error message or incorrect output.
18979 Put something meaningful in the subject of your message.
18982 Please attempt to include these 5 important parts, as applicable, in all
18983 requests for support or when reporting any problems or bugs with SDCC.
18984 Though this will make your message lengthy, it will greatly improve your
18985 chance that SDCC users and developers will be able to help you.
18986 Some SDCC developers are frustrated by bug reports without code provided
18987 that they can use to reproduce and ultimately fix the problem, so please
18988 be sure to provide sample code if you are reporting a bug!
18991 Please have a short check that you are using a recent version of SDCC and
18992 the bug is not yet known.
18993 This is the link for reporting bugs:
18994 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
19001 Requesting Features
19002 \begin_inset LatexCommand \label{sub:Requesting-Features}
19007 \begin_inset LatexCommand \index{Feature request}
19012 \begin_inset LatexCommand \index{Requesting features}
19019 Like bug reports feature requests are forwarded to the developer mailing
19021 This is the link for requesting features:
19022 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
19032 Like bug reports contributed patches are forwarded to the developer mailing
19034 This is the link for submitting patches
19035 \begin_inset LatexCommand \index{Patch submission}
19040 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
19047 You need to specify some parameters to the
19051 command for the patches to be useful.
19052 If you modified more than one file a patch created f.e.
19057 \begin_inset Quotes sld
19060 diff -Naur unmodified_directory modified_directory >my_changes.patch
19061 \begin_inset Quotes srd
19067 will be fine, otherwise
19071 \begin_inset Quotes sld
19074 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
19075 \begin_inset Quotes srd
19088 These links should take you directly to the
19089 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
19099 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
19100 automated messages (mid 2003)
19104 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
19109 \begin_inset LatexCommand \index{Mailing list(s)}
19113 and forums are archived and searchable so if you are lucky someone already
19114 had a similar problem.
19115 While mails to the lists themselves are delivered promptly their web front
19116 end on sourceforge sometimes shows a severe time lag (up to several weeks),
19117 if you're seriously using SDCC please consider subscribing to the lists.
19123 You can follow the status of the cvs version
19124 \begin_inset LatexCommand \index{version}
19128 of SDCC by watching the Changelog
19129 \begin_inset LatexCommand \index{Changelog}
19133 in the cvs-repository
19138 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
19146 \begin_inset LatexCommand \index{Release policy}
19153 Historically there often were long delays between official releases and
19154 the sourceforge download area tends to get not updated at all.
19155 Excuses in the past might have referred to problems with live range analysis,
19156 but as this was fixed a while ago, the current problem is that another
19157 excuse has to be found.
19158 Kidding aside, we have to get better there! On the other hand there are
19159 daily snapshots available at
19160 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
19164 , and you can always build the very last version (hopefully with many bugs
19165 fixed, and features added) from the source code available at
19166 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
19174 \begin_inset LatexCommand \index{Examples}
19181 You'll find some small examples in the directory
19183 sdcc/device/examples/.
19186 More examples and libraries are available at
19188 The SDCC Open Knowledge Resource
19189 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
19196 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
19203 I did insert a reference to Paul's web site here although it seems rather
19204 dedicated to a specific 8032 board (I think it's okay because it f.e.
19205 shows LCD/Harddisc interface and has a free 8051 monitor.
19206 Independent 8032 board vendors face hard competition of heavily subsidized
19207 development boards anyway).
19210 Maybe we should include some links to real world applications.
19211 Preferably pointer to pointers (one for each architecture) so this stays
19216 \begin_inset LatexCommand \index{Quality control}
19223 The compiler is passed through nightly compile and build checks.
19229 \begin_inset LatexCommand \index{Regression test}
19233 check that SDCC itself compiles flawlessly on several platforms and checks
19234 the quality of the code generated by SDCC by running the code through simulator
19236 There is a separate document
19239 \begin_inset LatexCommand \index{Test suite}
19248 You'll find the test code in the directory
19250 sdcc/support/regression
19253 You can run these tests manually by running
19257 in this directory (or f.e.
19262 \begin_inset Quotes sld
19266 \begin_inset Quotes srd
19272 if you don't want to run the complete tests).
19273 The test code might also be interesting if you want to look for examples
19274 \begin_inset LatexCommand \index{Examples}
19278 checking corner cases of SDCC or if you plan to submit patches
19279 \begin_inset LatexCommand \index{Patch submission}
19286 The pic port uses a different set of regression tests, you'll find them
19289 sdcc/src/regression
19294 SDCC Technical Data
19298 \begin_inset LatexCommand \index{Optimizations}
19305 SDCC performs a host of standard optimizations in addition to some MCU specific
19310 Sub-expression Elimination
19311 \begin_inset LatexCommand \index{Subexpression elimination}
19318 The compiler does local and
19344 will be translated to
19356 Some subexpressions are not as obvious as the above example, e.g.:
19366 In this case the address arithmetic a->b[i] will be computed only once;
19367 the equivalent code in C would be.
19379 The compiler will try to keep these temporary variables in registers.
19382 Dead-Code Elimination
19383 \begin_inset LatexCommand \index{Dead-code elimination}
19404 i = 1; \SpecialChar ~
19413 global = 1;\SpecialChar ~
19426 global = 3;\SpecialChar ~
19455 \begin_inset LatexCommand \index{Copy propagation}
19511 Note: the dead stores created by this copy propagation will be eliminated
19512 by dead-code elimination.
19516 \begin_inset LatexCommand \index{Loop optimization}
19521 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
19528 Two types of loop optimizations are done by SDCC
19536 of loop induction variables.
19537 In addition to the strength reduction the optimizer marks the induction
19538 variables and the register allocator tries to keep the induction variables
19539 in registers for the duration of the loop.
19540 Because of this preference of the register allocator
19541 \begin_inset LatexCommand \index{Register allocation}
19545 , loop induction optimization causes an increase in register pressure, which
19546 may cause unwanted spilling of other temporary variables into the stack
19547 \begin_inset LatexCommand \index{stack}
19552 The compiler will generate a warning message when it is forced to allocate
19553 extra space either on the stack or data space.
19554 If this extra space allocation is undesirable then induction optimization
19555 can be eliminated either for the entire source file (with -
19565 -noinduction option) or for a given function only using #pragma\SpecialChar ~
19567 \begin_inset LatexCommand \index{\#pragma noinduction}
19580 for (i = 0 ; i < 100 ; i ++)
19596 for (i = 0; i < 100; i++)
19605 As mentioned previously some loop invariants are not as apparent, all static
19606 address computations are also moved out of the loop.
19611 \begin_inset LatexCommand \index{Strength reduction}
19615 , this optimization substitutes an expression by a cheaper expression:
19620 for (i=0;i < 100; i++)
19638 for (i=0;i< 100;i++) {
19644 ar[itemp1] = itemp2;
19661 The more expensive multiplication
19662 \begin_inset LatexCommand \index{Multiplication}
19666 is changed to a less expensive addition.
19670 \begin_inset LatexCommand \index{Loop reversing}
19677 This optimization is done to reduce the overhead of checking loop boundaries
19678 for every iteration.
19679 Some simple loops can be reversed and implemented using a
19680 \begin_inset Quotes eld
19683 decrement and jump if not zero
19684 \begin_inset Quotes erd
19688 SDCC checks for the following criterion to determine if a loop is reversible
19689 (note: more sophisticated compilers use data-dependency analysis to make
19690 this determination, SDCC uses a more simple minded analysis).
19693 The 'for' loop is of the form
19699 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
19709 The <for body> does not contain
19710 \begin_inset Quotes eld
19714 \begin_inset Quotes erd
19718 \begin_inset Quotes erd
19724 All goto's are contained within the loop.
19727 No function calls within the loop.
19730 The loop control variable <sym> is not assigned any value within the loop
19733 The loop control variable does NOT participate in any arithmetic operation
19737 There are NO switch statements in the loop.
19740 Algebraic Simplifications
19743 SDCC does numerous algebraic simplifications, the following is a small sub-set
19744 of these optimizations.
19749 i = j + 0;\SpecialChar ~
19753 /* changed to: */\SpecialChar ~
19759 i /= 2;\SpecialChar ~
19766 /* changed to: */\SpecialChar ~
19772 i = j - j;\SpecialChar ~
19776 /* changed to: */\SpecialChar ~
19782 i = j / 1;\SpecialChar ~
19786 /* changed to: */\SpecialChar ~
19793 Note the subexpressions
19794 \begin_inset LatexCommand \index{Subexpression}
19798 given above are generally introduced by macro expansions or as a result
19799 of copy/constant propagation.
19802 'switch' Statements
19803 \begin_inset LatexCommand \label{sub:'switch'-Statements}
19808 \begin_inset LatexCommand \index{switch statement}
19815 SDCC changes switch statements to jump tables
19816 \begin_inset LatexCommand \index{jump tables}
19820 when the following conditions are true.
19824 The case labels are in numerical sequence, the labels need not be in order,
19825 and the starting number need not be one or zero.
19831 switch(i) {\SpecialChar ~
19862 case 4: ...\SpecialChar ~
19894 case 5: ...\SpecialChar ~
19926 case 3: ...\SpecialChar ~
19958 case 6: ...\SpecialChar ~
20026 Both the above switch statements will be implemented using a jump-table.
20027 The example to the right side is slightly more efficient as the check for
20028 the lower boundary of the jump-table is not needed.
20032 The number of case labels is at least three, since it takes two conditional
20033 statements to handle the boundary conditions.
20036 The number of case labels is less than 84, since each label takes 3 bytes
20037 and a jump-table can be utmost 256 bytes long.
20040 Switch statements which have gaps in the numeric sequence or those that
20041 have more that 84 case labels can be split into more than one switch statement
20042 for efficient code generation, e.g.:
20092 If the above switch statement is broken down into two switch statements
20131 case 9:\SpecialChar ~
20138 case 10:\SpecialChar ~
20144 case 11:\SpecialChar ~
20150 case 12:\SpecialChar ~
20157 then both the switch statements will be implemented using jump-tables whereas
20158 the unmodified switch statement will not be.
20159 You might also consider inserting dummy cases 0 and 5 to 8 in this example.
20162 The pragma nojtbound
20163 \begin_inset LatexCommand \index{\#pragma nojtbound}
20167 can be used to turn off checking the
20180 It has no effect if a default label is supplied.
20181 Use of this pragma is dangerous: if the switch argument is not matched
20182 by a case statement the processor will happily jump into Nirvana.
20185 Bit-shifting Operations
20186 \begin_inset LatexCommand \index{Bit shifting}
20193 Bit shifting is one of the most frequently used operation in embedded programmin
20195 SDCC tries to implement bit-shift operations in the most efficient way
20211 generates the following code:
20228 In general SDCC will never setup a loop if the shift count is known.
20271 \begin_inset LatexCommand \index{Bit rotation}
20278 A special case of the bit-shift operation is bit rotation
20279 \begin_inset LatexCommand \index{rotating bits}
20283 , SDCC recognizes the following expression to be a left bit-rotation:
20293 char i;\SpecialChar ~
20304 /* unsigned is needed for rotation */
20309 i = ((i << 1) | (i >> 7));
20318 will generate the following code:
20337 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
20338 ns of this case will also be recognized as bit-rotation, i.e.:
20343 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
20346 Nibble and Byte Swapping
20349 Other special cases of the bit-shift operations are nibble or byte swapping
20350 \begin_inset LatexCommand \index{swapping nibbles/bytes}
20354 , SDCC recognizes the following expressions:
20377 i = ((i << 4) | (i >> 4));
20383 j = ((j << 8) | (j >> 8));
20386 and generates a swap instruction for the nibble swapping
20387 \begin_inset LatexCommand \index{Nibble swapping}
20391 or move instructions for the byte swapping
20392 \begin_inset LatexCommand \index{Byte swapping}
20398 \begin_inset Quotes sld
20402 \begin_inset Quotes srd
20405 example can be used to convert from little to big-endian or vice versa.
20406 If you want to change the endianness of a
20410 integer you have to cast to
20417 Note that SDCC stores numbers in little-endian
20423 Usually 8-bit processors don't care much about endianness.
20424 This is not the case for the standard 8051 which only has an instruction
20430 \begin_inset LatexCommand \index{DPTR}
20438 so little-endian is the more efficient byte order.
20442 \begin_inset LatexCommand \index{little-endian}
20447 \begin_inset LatexCommand \index{Endianness}
20452 lowest order first).
20456 \begin_inset LatexCommand \index{Highest Order Bit}
20463 It is frequently required to obtain the highest order bit of an integral
20464 type (long, int, short or char types).
20465 SDCC recognizes the following expression to yield the highest order bit
20466 and generates optimized code for it, e.g.:
20488 hob = (gint >> 15) & 1;
20498 will generate the following code:
20531 000A E5*01\SpecialChar ~
20558 000C 23\SpecialChar ~
20589 000D 54 01\SpecialChar ~
20616 000F F5*02\SpecialChar ~
20644 Variations of this case however will
20649 It is a standard C expression, so I heartily recommend this be the only
20650 way to get the highest order bit, (it is portable).
20651 Of course it will be recognized even if it is embedded in other expressions,
20657 xyz = gint + ((gint >> 15) & 1);
20660 will still be recognized.
20664 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
20669 \begin_inset LatexCommand \index{Peephole optimizer}
20676 The compiler uses a rule based, pattern matching and re-writing mechanism
20677 for peep-hole optimization.
20682 a peep-hole optimizer by Christopher W.
20683 Fraser (cwfraser@microsoft.com).
20684 A default set of rules are compiled into the compiler, additional rules
20685 may be added with the
20698 \begin_inset LatexCommand \index{-\/-peep-file}
20705 The rule language is best illustrated with examples.
20729 The above rule will change the following assembly
20730 \begin_inset LatexCommand \index{Assembler routines}
20752 Note: All occurrences of a
20756 (pattern variable) must denote the same string.
20757 With the above rule, the assembly sequence:
20767 will remain unmodified.
20771 Other special case optimizations may be added by the user (via
20787 some variants of the 8051 MCU
20788 \begin_inset LatexCommand \index{MCS51 variants}
20801 The following two rules will change all
20820 replace { lcall %1 } by { acall %1 }
20822 replace { ljmp %1 } by { ajmp %1 }
20827 inline-assembler code
20829 is also passed through the peep hole optimizer, thus the peephole optimizer
20830 can also be used as an assembly level macro expander.
20831 The rules themselves are MCU dependent whereas the rule language infra-structur
20832 e is MCU independent.
20833 Peephole optimization rules for other MCU can be easily programmed using
20838 The syntax for a rule is as follows:
20843 rule := replace [ restart ] '{' <assembly sequence> '
20881 <assembly sequence> '
20899 '}' [if <functionName> ] '
20904 <assembly sequence> := assembly instruction (each instruction including
20905 labels must be on a separate line).
20909 The optimizer will apply to the rules one by one from the top in the sequence
20910 of their appearance, it will terminate when all rules are exhausted.
20911 If the 'restart' option is specified, then the optimizer will start matching
20912 the rules again from the top, this option for a rule is expensive (performance)
20913 , it is intended to be used in situations where a transformation will trigger
20914 the same rule again.
20915 An example of this (not a good one, it has side effects) is the following
20938 Note that the replace pattern cannot be a blank, but can be a comment line.
20939 Without the 'restart' option only the innermost 'pop' 'push' pair would
20940 be eliminated, i.e.:
20970 the restart option the rule will be applied again to the resulting code
20971 and then all the pop-push pairs will be eliminated to yield:
20981 A conditional function can be attached to a rule.
20982 Attaching rules are somewhat more involved, let me illustrate this with
21009 The optimizer does a look-up of a function name table defined in function
21014 in the source file SDCCpeeph.c, with the name
21019 If it finds a corresponding entry the function is called.
21020 Note there can be no parameters specified for these functions, in this
21025 is crucial, since the function
21029 expects to find the label in that particular variable (the hash table containin
21030 g the variable bindings is passed as a parameter).
21031 If you want to code more such functions, take a close look at the function
21032 labelInRange and the calling mechanism in source file SDCCpeeph.c.
21033 Currently implemented are
21035 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
21036 24bitMode, portIsDS390, 24bitModeAndPortDS390
21045 I know this whole thing is a little kludgey, but maybe some day we will
21046 have some better means.
21047 If you are looking at this file, you will see the default rules that are
21048 compiled into the compiler, you can add your own rules in the default set
21049 there if you get tired of specifying the -
21063 \begin_inset LatexCommand \index{ANSI-compliance}
21068 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
21075 Deviations from the compliance:
21078 functions are not always reentrant
21079 \begin_inset LatexCommand \index{reentrant}
21086 structures cannot be assigned values directly, cannot be passed as function
21087 parameters or assigned to each other and cannot be a return value from
21114 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
21125 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
21147 return rets;/* is invalid in SDCC although allowed in ANSI */
21154 \begin_inset LatexCommand \index{long long (not supported)}
21159 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
21167 \begin_inset LatexCommand \index{double (not supported)}
21171 ' precision floating point
21172 \begin_inset LatexCommand \index{Floating point support}
21179 No support for setjmp
21180 \begin_inset LatexCommand \index{setjmp (not supported)}
21185 \begin_inset LatexCommand \index{longjmp (not supported)}
21193 \begin_inset LatexCommand \index{K\&R style}
21197 function declarations are NOT allowed.
21203 foo(i,j) /* this old style of function declarations */
21205 int i,j; /* are valid in ANSI but not valid in SDCC */
21220 Cyclomatic Complexity
21221 \begin_inset LatexCommand \index{Cyclomatic complexity}
21228 Cyclomatic complexity of a function is defined as the number of independent
21229 paths the program can take during execution of the function.
21230 This is an important number since it defines the number test cases you
21231 have to generate to validate the function.
21232 The accepted industry standard for complexity number is 10, if the cyclomatic
21233 complexity reported by SDCC exceeds 10 you should think about simplification
21234 of the function logic.
21235 Note that the complexity level is not related to the number of lines of
21236 code in a function.
21237 Large functions can have low complexity, and small functions can have large
21243 SDCC uses the following formula to compute the complexity:
21248 complexity = (number of edges in control flow graph) - (number of nodes
21249 in control flow graph) + 2;
21253 Having said that the industry standard is 10, you should be aware that in
21254 some cases it be may unavoidable to have a complexity level of less than
21256 For example if you have switch statement with more than 10 case labels,
21257 each case label adds one to the complexity level.
21258 The complexity level is by no means an absolute measure of the algorithmic
21259 complexity of the function, it does however provide a good starting point
21260 for which functions you might look at for further optimization.
21263 Retargetting for other Processors
21266 The issues for retargetting the compiler are far too numerous to be covered
21268 What follows is a brief description of each of the seven phases of the
21269 compiler and its MCU dependency.
21272 Parsing the source and building the annotated parse tree.
21273 This phase is largely MCU independent (except for the language extensions).
21274 Syntax & semantic checks are also done in this phase, along with some initial
21275 optimizations like back patching labels and the pattern matching optimizations
21276 like bit-rotation etc.
21279 The second phase involves generating an intermediate code which can be easy
21280 manipulated during the later phases.
21281 This phase is entirely MCU independent.
21282 The intermediate code generation assumes the target machine has unlimited
21283 number of registers, and designates them with the name iTemp.
21284 The compiler can be made to dump a human readable form of the code generated
21298 This phase does the bulk of the standard optimizations and is also MCU independe
21300 This phase can be broken down into several sub-phases:
21304 Break down intermediate code (iCode) into basic blocks.
21306 Do control flow & data flow analysis on the basic blocks.
21308 Do local common subexpression elimination, then global subexpression elimination
21310 Dead code elimination
21314 If loop optimizations caused any changes then do 'global subexpression eliminati
21315 on' and 'dead code elimination' again.
21318 This phase determines the live-ranges; by live range I mean those iTemp
21319 variables defined by the compiler that still survive after all the optimization
21321 Live range analysis
21322 \begin_inset LatexCommand \index{Live range analysis}
21326 is essential for register allocation, since these computation determines
21327 which of these iTemps will be assigned to registers, and for how long.
21330 Phase five is register allocation.
21331 There are two parts to this process.
21335 The first part I call 'register packing' (for lack of a better term).
21336 In this case several MCU specific expression folding is done to reduce
21341 The second part is more MCU independent and deals with allocating registers
21342 to the remaining live ranges.
21343 A lot of MCU specific code does creep into this phase because of the limited
21344 number of index registers available in the 8051.
21347 The Code generation phase is (unhappily), entirely MCU dependent and very
21348 little (if any at all) of this code can be reused for other MCU.
21349 However the scheme for allocating a homogenized assembler operand for each
21350 iCode operand may be reused.
21353 As mentioned in the optimization section the peep-hole optimizer is rule
21354 based system, which can reprogrammed for other MCUs.
21358 \begin_inset LatexCommand \index{Compiler internals}
21365 The anatomy of the compiler
21366 \begin_inset LatexCommand \label{sub:The-anatomy-of}
21375 This is an excerpt from an article published in Circuit Cellar Magazine
21377 It's a little outdated (the compiler is much more efficient now and user/develo
21378 per friendly), but pretty well exposes the guts of it all.
21384 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
21385 It is fairly easy to retarget for other 8-bit MCU.
21386 Here we take a look at some of the internals of the compiler.
21391 \begin_inset LatexCommand \index{Parsing}
21398 Parsing the input source file and creating an AST (Annotated Syntax Tree
21399 \begin_inset LatexCommand \index{Annotated syntax tree}
21404 This phase also involves propagating types (annotating each node of the
21405 parse tree with type information) and semantic analysis.
21406 There are some MCU specific parsing rules.
21407 For example the storage classes, the extended storage classes are MCU specific
21408 while there may be a xdata storage class for 8051 there is no such storage
21409 class for z80 or Atmel AVR.
21410 SDCC allows MCU specific storage class extensions, i.e.
21411 xdata will be treated as a storage class specifier when parsing 8051 C
21412 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
21417 \begin_inset LatexCommand \index{iCode}
21424 Intermediate code generation.
21425 In this phase the AST is broken down into three-operand form (iCode).
21426 These three operand forms are represented as doubly linked lists.
21427 ICode is the term given to the intermediate form generated by the compiler.
21428 ICode example section shows some examples of iCode generated for some simple
21429 C source functions.
21433 \begin_inset LatexCommand \index{Optimizations}
21440 Bulk of the target independent optimizations is performed in this phase.
21441 The optimizations include constant propagation, common sub-expression eliminati
21442 on, loop invariant code movement, strength reduction of loop induction variables
21443 and dead-code elimination.
21446 Live range analysis
21447 \begin_inset LatexCommand \index{Live range analysis}
21454 During intermediate code generation phase, the compiler assumes the target
21455 machine has infinite number of registers and generates a lot of temporary
21457 The live range computation determines the lifetime of each of these compiler-ge
21458 nerated temporaries.
21459 A picture speaks a thousand words.
21460 ICode example sections show the live range annotations for each of the
21462 It is important to note here, each iCode is assigned a number in the order
21463 of its execution in the function.
21464 The live ranges are computed in terms of these numbers.
21465 The from number is the number of the iCode which first defines the operand
21466 and the to number signifies the iCode which uses this operand last.
21469 Register Allocation
21470 \begin_inset LatexCommand \index{Register allocation}
21477 The register allocation determines the type and number of registers needed
21479 In most MCUs only a few registers can be used for indirect addressing.
21480 In case of 8051 for example the registers R0 & R1 can be used to indirectly
21481 address the internal ram and DPTR to indirectly address the external ram.
21482 The compiler will try to allocate the appropriate register to pointer variables
21484 ICode example section shows the operands annotated with the registers assigned
21486 The compiler will try to keep operands in registers as much as possible;
21487 there are several schemes the compiler uses to do achieve this.
21488 When the compiler runs out of registers the compiler will check to see
21489 if there are any live operands which is not used or defined in the current
21490 basic block being processed, if there are any found then it will push that
21491 operand and use the registers in this block, the operand will then be popped
21492 at the end of the basic block.
21496 There are other MCU specific considerations in this phase.
21497 Some MCUs have an accumulator; very short-lived operands could be assigned
21498 to the accumulator instead of a general-purpose register.
21504 Figure II gives a table of iCode operations supported by the compiler.
21505 The code generation involves translating these operations into corresponding
21506 assembly code for the processor.
21507 This sounds overly simple but that is the essence of code generation.
21508 Some of the iCode operations are generated on a MCU specific manner for
21509 example, the z80 port does not use registers to pass parameters so the
21510 SEND and RECV iCode operations will not be generated, and it also does
21511 not support JUMPTABLES.
21518 <Where is Figure II ?>
21522 \begin_inset LatexCommand \index{iCode}
21529 This section shows some details of iCode.
21530 The example C code does not do anything useful; it is used as an example
21531 to illustrate the intermediate code generated by the compiler.
21543 /* This function does nothing useful.
21550 for the purpose of explaining iCode */
21553 short function (data int *x)
21561 short i=10; \SpecialChar ~
21563 /* dead initialization eliminated */
21568 short sum=10; /* dead initialization eliminated */
21581 while (*x) *x++ = *p++;
21595 /* compiler detects i,j to be induction variables */
21599 for (i = 0, j = 10 ; i < 10 ; i++, j
21625 mul += i * 3; \SpecialChar ~
21627 /* this multiplication remains */
21633 gint += j * 3;\SpecialChar ~
21635 /* this multiplication changed to addition */
21649 In addition to the operands each iCode contains information about the filename
21650 and line it corresponds to in the source file.
21651 The first field in the listing should be interpreted as follows:
21656 Filename(linenumber: iCode Execution sequence number : ICode hash table
21657 key : loop depth of the iCode).
21662 Then follows the human readable form of the ICode operation.
21663 Each operand of this triplet form can be of three basic types a) compiler
21664 generated temporary b) user defined variable c) a constant value.
21665 Note that local variables and parameters are replaced by compiler generated
21668 \begin_inset LatexCommand \index{Live range analysis}
21672 are computed only for temporaries (i.e.
21673 live ranges are not computed for global variables).
21675 \begin_inset LatexCommand \index{Register allocation}
21679 are allocated for temporaries only.
21680 Operands are formatted in the following manner:
21685 Operand Name [lr live-from : live-to ] { type information } [ registers
21691 As mentioned earlier the live ranges are computed in terms of the execution
21692 sequence number of the iCodes, for example
21694 the iTemp0 is live from (i.e.
21695 first defined in iCode with execution sequence number 3, and is last used
21696 in the iCode with sequence number 5).
21697 For induction variables such as iTemp21 the live range computation extends
21698 the lifetime from the start to the end of the loop.
21700 The register allocator used the live range information to allocate registers,
21701 the same registers may be used for different temporaries if their live
21702 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
21703 iTemp17 since their live ranges do not overlap.
21704 In addition the allocator also takes into consideration the type and usage
21705 of a temporary, for example itemp6 is a pointer to near space and is used
21706 as to fetch data from (i.e.
21707 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
21708 Some short lived temporaries are allocated to special registers which have
21709 meaning to the code generator e.g.
21710 iTemp13 is allocated to a pseudo register CC which tells the back end that
21711 the temporary is used only for a conditional jump the code generation makes
21712 use of this information to optimize a compare and jump ICode.
21714 There are several loop optimizations
21715 \begin_inset LatexCommand \index{Loop optimization}
21719 performed by the compiler.
21720 It can detect induction variables iTemp21(i) and iTemp23(j).
21721 Also note the compiler does selective strength reduction
21722 \begin_inset LatexCommand \index{Strength reduction}
21727 the multiplication of an induction variable in line 18 (gint = j * 3) is
21728 changed to addition, a new temporary iTemp17 is allocated and assigned
21729 a initial value, a constant 3 is then added for each iteration of the loop.
21730 The compiler does not change the multiplication
21731 \begin_inset LatexCommand \index{Multiplication}
21735 in line 17 however since the processor does support an 8 * 8 bit multiplication.
21737 Note the dead code elimination
21738 \begin_inset LatexCommand \index{Dead-code elimination}
21742 optimization eliminated the dead assignments in line 7 & 8 to I and sum
21750 Sample.c (5:1:0:0) _entry($9) :
21755 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
21760 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
21765 Sample.c(11:4:53:0) preHeaderLbl0($11) :
21770 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
21776 Sample.c(11:6:5:1) _whilecontinue_0($1) :
21781 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
21787 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
21792 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
21798 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
21804 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
21810 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
21816 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
21817 * int}[r0] + 0x2 {short}
21822 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
21827 Sample.c(11:17:21:0)_whilebreak_0($3) :
21832 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
21837 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
21842 Sample.c(15:20:54:0)preHeaderLbl1($13) :
21847 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
21852 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
21857 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
21862 Sample.c(15:24:26:1)_forcond_0($4) :
21867 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
21873 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
21878 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
21879 + ITemp21 [lr21:38]{short}[r4]
21884 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
21890 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
21891 + iTemp15 [lr29:30]{short}[r1]
21896 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
21902 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
21908 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
21914 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
21920 Sample.c(19:38:47:1) goto _forcond_0($4)
21925 Sample.c(19:39:48:0)_forbreak_0($7) :
21930 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
21931 + ITemp11 [lr19:40]{short}[r3]
21936 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
21941 Sample.c(20:42:51:0)_return($8) :
21946 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
21952 Finally the code generated for this function:
21993 ; ----------------------------------------------
21998 ; function function
22003 ; ----------------------------------------------
22013 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
22025 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
22037 ;_whilecontinue_0($1) :
22047 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
22052 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22111 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
22130 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
22177 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
22217 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
22243 ; iTemp6 [lr5:16]{_near * int}[r0] =
22248 ; iTemp6 [lr5:16]{_near * int}[r0] +
22265 ; goto _whilecontinue_0($1)
22277 ; _whilebreak_0($3) :
22287 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
22299 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
22311 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
22323 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
22342 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
22371 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
22376 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
22421 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
22426 ; iTemp21 [lr21:38]{short}[r4]
22452 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
22485 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
22490 ; iTemp15 [lr29:30]{short}[r1]
22509 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
22556 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
22603 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
22615 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
22629 cjne r5,#0xff,00104$
22641 ; goto _forcond_0($4)
22653 ; _forbreak_0($7) :
22663 ; ret iTemp24 [lr40:41]{short}
22706 A few words about basic block successors, predecessors and dominators
22709 Successors are basic blocks
22710 \begin_inset LatexCommand \index{Basic blocks}
22714 that might execute after this basic block.
22716 Predecessors are basic blocks that might execute before reaching this basic
22719 Dominators are basic blocks that WILL execute before reaching this basic
22753 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
22756 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
22759 c) domVect of [BB4] = BB1 ...
22760 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
22768 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
22778 Thanks to all the other volunteer developers who have helped with coding,
22779 testing, web-page creation, distribution sets, etc.
22780 You know who you are :-)
22787 This document was initially written by Sandeep Dutta
22790 All product names mentioned herein may be trademarks
22791 \begin_inset LatexCommand \index{Trademarks}
22795 of their respective companies.
22802 To avoid confusion, the installation and building options for SDCC itself
22803 (chapter 2) are not part of the index.
22807 \begin_inset LatexCommand \printindex{}