1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
8 pdftitle={SDCC Compiler User Guide},
9 pdfauthor={SDCC development team},
10 pdfsubject={installation, user manual},
11 pdfkeywords={8032, 8051, ansi, c, compiler, CPU, DS390,
12 embedded, GPL, HC08, manual, mcs51, PIC, Z80},
14 linkcolor=blue] {hyperref}
18 \emergencystretch=30pt
23 \inputencoding default
26 \paperfontsize default
28 \papersize letterpaper
33 \use_numerical_citations 0
34 \paperorientation portrait
41 \paragraph_separation indent
43 \quotes_language swedish
51 Please note: double dashed longoptions (e.g.
52 --version) are written this way: -
66 three consecutive dashes simply result in a long resp.
70 Architecture specific stuff (like memory models, code examples) should maybe
74 into seperate sections/chapters/appendices (it is hard to document PIC or
78 a 8051 centered document) - for now simply add.
81 SDCC Compiler User Guide
95 The above strings enclosed in $ are automatically updated by cvs
99 \begin_inset LatexCommand \tableofcontents{}
132 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
137 designed for 8 bit Microprocessors.
138 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
140 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
144 , etc.), Dallas DS80C390 variants, Motorola HC08 and Zilog Z80 based MCUs.
145 It can be retargetted for other microprocessors, support for Microchip
146 PIC, Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargettable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
207 char (8 bits, 1 byte),
210 short and int (16 bits, 2 bytes),
213 long (32 bit, 4 bytes)
220 The compiler also allows
222 inline assembler code
224 to be embedded anywhere in a function.
225 In addition, routines developed in assembly can also be called.
229 SDCC also provides an option (-
239 -cyclomatic) to report the relative complexity of a function.
240 These functions can then be further optimized, or hand coded in assembly
246 SDCC also comes with a companion source level debugger SDCDB, the debugger
247 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
252 The latest version can be downloaded from
253 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
263 Please note: the compiler will probably always be some steps ahead of this
268 \begin_inset LatexCommand \index{Status of documentation}
278 Obviously this has pros and cons
287 All packages used in this compiler system are
295 ; source code for all the sub-packages (pre-processor, assemblers, linkers
296 etc) is distributed with the package.
297 This documentation is maintained using a freeware word processor (LyX).
299 This program is free software; you can redistribute it and/or modify it
300 under the terms of the GNU General Public License
301 \begin_inset LatexCommand \index{GNU General Public License, GPL}
305 as published by the Free Software Foundation; either version 2, or (at
306 your option) any later version.
307 This program is distributed in the hope that it will be useful, but WITHOUT
308 ANY WARRANTY; without even the implied warranty
309 \begin_inset LatexCommand \index{warranty}
313 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
314 See the GNU General Public License for more details.
315 You should have received a copy of the GNU General Public License along
316 with this program; if not, write to the Free Software Foundation, 59 Temple
317 Place - Suite 330, Boston, MA 02111-1307, USA.
318 In other words, you are welcome to use, share and improve this program.
319 You are forbidden to forbid anyone else to use, share and improve what
321 Help stamp out software-hoarding!
324 Typographic conventions
325 \begin_inset LatexCommand \index{Typographic conventions}
332 Throughout this manual, we will use the following convention.
333 Commands you have to type in are printed in
341 Code samples are printed in
346 Interesting items and new terms are printed in
351 Compatibility with previous versions
354 This version has numerous bug fixes compared with the previous version.
355 But we also introduced some incompatibilities with older versions.
356 Not just for the fun of it, but to make the compiler more stable, efficient
358 \begin_inset LatexCommand \index{ANSI-compliance}
363 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
367 for ANSI-Compliance).
373 short is now equivalent to int (16 bits), it used to be equivalent to char
374 (8 bits) which is not ANSI compliant
377 the default directory for gcc-builds where include, library and documentation
378 files are stored is now in /usr/local/share
381 char type parameters to vararg functions are casted to int unless explicitly
398 will push a as an int and as a char resp.
411 -regextend has been removed
424 -noregparms has been removed
437 -stack-after-data has been removed
442 <pending: more incompatibilities?>
448 What do you need before you start installation of SDCC? A computer, and
450 The preferred method of installation is to compile SDCC from source using
452 For Windows some pre-compiled binary distributions are available for your
454 You should have some experience with command line tools and compiler use.
460 The SDCC home page at
461 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
465 is a great place to find distribution sets.
466 You can also find links to the user mailing lists that offer help or discuss
467 SDCC with other SDCC users.
468 Web links to other SDCC related sites can also be found here.
469 This document can be found in the DOC directory of the source package as
471 A pdf version of this document is available at
472 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
477 Some of the other tools (simulator and assembler) included with SDCC contain
478 their own documentation and can be found in the source distribution.
479 If you want the latest unreleased software, the complete source package
480 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
483 Wishes for the future
486 There are (and always will be) some things that could be done.
487 Here are some I can think of:
494 char KernelFunction3(char p) at 0x340;
502 \begin_inset LatexCommand \index{code banking (not supported)}
512 If you can think of some more, please see the section
513 \begin_inset LatexCommand \ref{sub:Requesting-Features}
517 about filing feature requests
518 \begin_inset LatexCommand \index{Requesting features}
523 \begin_inset LatexCommand \index{Feature request}
533 \begin_inset LatexCommand \index{Installation}
540 For most users it is sufficient to skip to either section
541 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
546 \begin_inset LatexCommand \ref{sub:Windows-Install}
551 More detailled instructions follow below.
555 \begin_inset LatexCommand \index{Options SDCC configuration}
562 The install paths, search paths and other options are defined when running
564 The defaults can be overridden by:
566 \labelwidthstring 00.00.0000
578 -prefix see table below
580 \labelwidthstring 00.00.0000
592 -exec_prefix see table below
594 \labelwidthstring 00.00.0000
606 -bindir see table below
608 \labelwidthstring 00.00.0000
620 -datadir see table below
622 \labelwidthstring 00.00.0000
624 docdir environment variable, see table below
626 \labelwidthstring 00.00.0000
628 include_dir_suffix environment variable, see table below
630 \labelwidthstring 00.00.0000
632 lib_dir_suffix environment variable, see table below
634 \labelwidthstring 00.00.0000
636 sdccconf_h_dir_separator environment variable, either / or
641 This character will only be used in sdccconf.h; don't forget it's a C-header,
642 therefore a double-backslash is needed there.
644 \labelwidthstring 00.00.0000
656 -disable-mcs51-port Excludes the Intel mcs51 port
658 \labelwidthstring 00.00.0000
670 -disable-gbz80-port Excludes the Gameboy gbz80 port
672 \labelwidthstring 00.00.0000
684 -disable-z80-port Excludes the z80 port
686 \labelwidthstring 00.00.0000
698 -disable-avr-port Excludes the AVR port
700 \labelwidthstring 00.00.0000
712 -disable-ds390-port Excludes the DS390 port
714 \labelwidthstring 00.00.0000
726 -disable-hc08-port Excludes the HC08 port
728 \labelwidthstring 00.00.0000
740 -disable-pic-port Excludes the PIC port
742 \labelwidthstring 00.00.0000
754 -disable-xa51-port Excludes the XA51 port
756 \labelwidthstring 00.00.0000
768 -disable-ucsim Disables configuring and building of ucsim
770 \labelwidthstring 00.00.0000
782 -disable-device-lib-build Disables automatically building device libraries
784 \labelwidthstring 00.00.0000
796 -disable-packihx Disables building packihx
798 \labelwidthstring 00.00.0000
810 -enable-libgc Use the Bohem memory allocator.
811 Lower runtime footprint.
814 Furthermore the environment variables CC, CFLAGS, ...
815 the tools and their arguments can be influenced.
816 Please see `configure -
826 -help` and the man/info pages of `configure` for details.
830 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
831 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
832 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
833 At the moment it's not possible to change the default settings (it was
834 simply never required).
838 These configure options are compiled into the binaries, and can only be
839 changed by rerunning 'configure' and recompiling SDCC.
840 The configure options are written in
844 to distinguish them from run time environment variables (see section search
850 \begin_inset Quotes sld
854 \begin_inset Quotes srd
857 are used by the SDCC team to build the official Win32 binaries.
858 The SDCC team uses Mingw32 to build the official Windows binaries, because
865 a gcc compiler and last but not least
868 the binaries can be built by cross compiling on Sourceforge's compile farm.
871 See the examples, how to pass the Win32 settings to 'configure'.
872 The other Win32 builds using Borland, VC or whatever don't use 'configure',
873 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
885 <lyxtabular version="3" rows="8" columns="3">
887 <column alignment="block" valignment="top" leftline="true" width="0in">
888 <column alignment="block" valignment="top" leftline="true" width="0in">
889 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
890 <row topline="true" bottomline="true">
891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
899 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
907 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
935 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
947 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
989 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1001 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1016 <row topline="true">
1017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1039 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1050 <row topline="true">
1051 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1061 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1073 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1088 <row topline="true">
1089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1116 <row topline="true" bottomline="true">
1117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1153 'configure' also computes relative paths.
1154 This is needed for full relocatability of a binary package and to complete
1155 search paths (see section search paths below):
1161 \begin_inset Tabular
1162 <lyxtabular version="3" rows="4" columns="3">
1164 <column alignment="block" valignment="top" leftline="true" width="0in">
1165 <column alignment="block" valignment="top" leftline="true" width="0in">
1166 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1167 <row topline="true" bottomline="true">
1168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1193 <row topline="true" bottomline="true">
1194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1212 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1221 <row bottomline="true">
1222 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1232 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1240 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1249 <row bottomline="true">
1250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1268 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1301 \begin_inset Quotes srd
1305 \begin_inset Quotes srd
1319 \begin_inset Quotes srd
1323 \begin_inset Quotes srd
1351 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1360 \begin_inset Quotes srd
1363 i586-mingw32msvc-gcc
1364 \begin_inset Quotes srd
1368 \begin_inset Quotes srd
1371 i586-mingw32msvc-g++
1372 \begin_inset Quotes srd
1380 \begin_inset Quotes srd
1383 i586-mingw32msvc-ranlib
1384 \begin_inset Quotes srd
1392 \begin_inset Quotes srd
1395 i586-mingw32msvc-strip
1396 \begin_inset Quotes srd
1414 \begin_inset Quotes srd
1418 \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
1472 \begin_inset Quotes srd
1476 \begin_inset Quotes srd
1483 sdccconf_h_dir_separator=
1484 \begin_inset Quotes srd
1496 \begin_inset Quotes srd
1513 -disable-device-lib-build
1541 -host=i586-mingw32msvc -
1551 -build=unknown-unknown-linux-gnu
1555 \begin_inset Quotes sld
1559 \begin_inset Quotes srd
1562 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1571 \begin_inset Quotes srd
1575 \begin_inset Quotes srd
1583 \begin_inset Quotes srd
1587 \begin_inset Quotes srd
1605 \begin_inset Quotes srd
1609 \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
1663 \begin_inset Quotes srd
1667 \begin_inset Quotes srd
1674 sdccconf_h_dir_separator=
1675 \begin_inset Quotes srd
1687 \begin_inset Quotes srd
1707 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1718 -C' turns on caching, which gives a little bit extra speed.
1719 However if options are changed, it can be necessary to delete the config.cache
1724 \begin_inset LatexCommand \label{sub:Install-paths}
1729 \begin_inset LatexCommand \index{Install paths}
1735 \added_space_top medskip \align center
1737 \begin_inset Tabular
1738 <lyxtabular version="3" rows="5" columns="4">
1740 <column alignment="center" valignment="top" leftline="true" width="0">
1741 <column alignment="center" valignment="top" leftline="true" width="0">
1742 <column alignment="center" valignment="top" leftline="true" width="0">
1743 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1744 <row topline="true" bottomline="true">
1745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1786 <row topline="true">
1787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1813 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1826 <row topline="true">
1827 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1842 $DATADIR/ $INCLUDE_DIR_SUFFIX
1845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1850 /usr/local/share/sdcc/include
1853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1866 <row topline="true">
1867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1875 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1882 $DATADIR/$LIB_DIR_SUFFIX
1885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1890 /usr/local/share/sdcc/lib
1893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1906 <row topline="true" bottomline="true">
1907 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1925 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1930 /usr/local/share/sdcc/doc
1933 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1955 *compiler, preprocessor, assembler, and linker
1961 is auto-appended by the compiler, e.g.
1962 small, large, z80, ds390 etc
1965 The install paths can still be changed during `make install` with e.g.:
1968 make install prefix=$(HOME)/local/sdcc
1971 Of course this doesn't change the search paths compiled into the binaries.
1975 Moreover the install path can be changed by defining DESTDIR
1976 \begin_inset LatexCommand \index{DESTDIR}
1983 make install DESTDIR=$(HOME)/sdcc.rpm/
1986 Please note that DESTDIR must have a trailing slash!
1990 \begin_inset LatexCommand \label{sub:Search-Paths}
1995 \begin_inset LatexCommand \index{Search path}
2002 Some search paths or parts of them are determined by configure variables
2007 , see section above).
2008 Further search paths are determined by environment variables during runtime.
2011 The paths searched when running the compiler are as follows (the first catch
2017 Binary files (preprocessor, assembler and linker)
2023 \begin_inset Tabular
2024 <lyxtabular version="3" rows="4" columns="3">
2026 <column alignment="block" valignment="top" leftline="true" width="0in">
2027 <column alignment="block" valignment="top" leftline="true" width="0in">
2028 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2029 <row topline="true" bottomline="true">
2030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2046 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2055 <row topline="true">
2056 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2085 <row topline="true">
2086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2091 Path of argv[0] (if available)
2094 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2102 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2111 <row topline="true" bottomline="true">
2112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2128 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2153 \begin_inset Tabular
2154 <lyxtabular version="3" rows="6" columns="3">
2156 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2157 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2158 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2159 <row topline="true" bottomline="true">
2160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2185 <row topline="true">
2186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2222 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2241 <row topline="true">
2242 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2250 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2258 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2267 <row topline="true">
2268 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2282 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2294 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2305 <row topline="true">
2306 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2387 <row topline="true" bottomline="true">
2388 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2404 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2409 /usr/local/share/sdcc/
2414 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2442 -nostdinc disables the last two search paths.
2452 With the exception of
2453 \begin_inset Quotes sld
2467 \begin_inset Quotes srd
2474 is auto-appended by the compiler (e.g.
2475 small, large, z80, ds390 etc.).
2482 \begin_inset Tabular
2483 <lyxtabular version="3" rows="6" columns="3">
2485 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2486 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2487 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2488 <row topline="true" bottomline="true">
2489 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2497 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2505 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2514 <row topline="true">
2515 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2551 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2570 <row topline="true">
2571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2583 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2610 <row topline="true">
2611 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2622 $LIB_DIR_SUFFIX/<model>
2625 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2639 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2656 <row topline="true">
2657 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2672 $LIB_DIR_SUFFIX/<model>
2675 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2784 <row topline="true" bottomline="true">
2785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2794 $LIB_DIR_SUFFIX/<model>
2797 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2802 /usr/local/share/sdcc/
2809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2827 Don't delete any of the stray spaces in the table above without checking
2828 the HTML output (last line)!
2844 -nostdlib disables the last two search paths.
2848 \begin_inset LatexCommand \index{Building SDCC}
2855 Building SDCC on Linux
2856 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2865 Download the source package
2867 either from the SDCC CVS repository or from the nightly snapshots
2869 , it will be named something like sdcc
2880 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2889 Bring up a command line terminal, such as xterm.
2894 Unpack the file using a command like:
2897 "tar -xvzf sdcc.src.tar.gz
2902 , this will create a sub-directory called sdcc with all of the sources.
2905 Change directory into the main SDCC directory, for example type:
2922 This configures the package for compilation on your system.
2938 All of the source packages will compile, this can take a while.
2954 This copies the binary executables, the include files, the libraries and
2955 the documentation to the install directories.
2956 Proceed with section
2957 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
2964 Building SDCC on OSX 2.x
2967 Follow the instruction for Linux.
2971 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
2972 )) fails to compile SDCC.
2973 Fortunately there's also gcc 2.9.x installed, which works fine.
2974 This compiler can be selected by running 'configure' with:
2977 ./configure CC=gcc2 CXX=g++2
2980 Cross compiling SDCC on Linux for Windows
2983 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
2984 See section 'Configure Options'.
2987 Building SDCC on Windows
2990 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
2992 They use Unix-sockets, which are not available on Win32.
2995 Building SDCC using Cygwin and Mingw32
2998 For building and installing a Cygwin executable follow the instructions
3004 \begin_inset Quotes sld
3008 \begin_inset Quotes srd
3011 Win32-binary can be built, which will not need the Cygwin-DLL.
3012 For the necessary 'configure' options see section 'configure options' or
3013 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3017 In order to install Cygwin on Windows download setup.exe from
3018 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3024 \begin_inset Quotes sld
3027 default text file type
3028 \begin_inset Quotes srd
3032 \begin_inset Quotes sld
3036 \begin_inset Quotes srd
3039 and download/install at least the following packages.
3040 Some packages are selected by default, others will be automatically selected
3041 because of dependencies with the manually selected packages.
3042 Never deselect these packages!
3051 gcc ; version 3.x is fine, no need to use the old 2.9x
3054 binutils ; selected with gcc
3060 rxvt ; a nice console, which makes life much easier under windoze (see below)
3063 man ; not really needed for building SDCC, but you'll miss it sooner or
3067 less ; not really needed for building SDCC, but you'll miss it sooner or
3071 cvs ; only if you use CVS access
3074 If you want to develop something you'll need:
3077 python ; for the regression tests
3080 gdb ; the gnu debugger, together with the nice GUI
3081 \begin_inset Quotes sld
3085 \begin_inset Quotes srd
3091 openssh ; to access the CF or commit changes
3094 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3095 use autoconf-stable!
3098 rxvt is a nice console with history.
3099 Replace in your cygwin.bat the line
3118 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3121 -bg black -fg white -geometry 100x65 -e bash -
3134 Text selected with the mouse is automatically copied to the clipboard, pasting
3135 works with shift-insert.
3139 The other good tip is to make sure you have no //c/-style paths anywhere,
3140 use /cygdrive/c/ instead.
3141 Using // invokes a network lookup which is very slow.
3143 \begin_inset Quotes sld
3147 \begin_inset Quotes srd
3150 is too long, you can change it with e.g.
3156 SDCC sources use the unix line ending LF.
3157 Life is much easier, if you store the source tree on a drive which is mounted
3159 And use an editor which can handle LF-only line endings.
3160 Make sure not to commit files with windows line endings.
3161 The tabulator spacing
3162 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3166 used in the project is 8.
3167 Although a tabulator spacing of 8 is a sensible choice for programmers
3168 (it's a power of 2 and allows to display 8/16 bit signed variables without
3169 loosing columns) the plan is to move towards using only spaces in the source.
3172 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3177 Download the source package
3179 either from the SDCC CVS repository or from the
3180 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3186 , it will be named something like sdcc
3193 SDCC is distributed with all the projects, workspaces, and files you need
3194 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3195 doesn't build under MSVC).
3196 The workspace name is 'sdcc.dsw'.
3197 Please note that as it is now, all the executables are created in a folder
3201 Once built you need to copy the executables from sdcc
3205 bin before running SDCC.
3210 WARNING: Visual studio is very picky with line terminations; it expects
3211 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3212 If you are getting a message such as "This makefile was not generated by
3213 Developer Studio etc.
3215 \begin_inset Quotes srd
3218 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3219 need to convert the Unix style line endings to DOS style line endings.
3220 To do so you can use the
3221 \begin_inset Quotes sld
3225 \begin_inset Quotes srd
3228 utility freely available on the internet.
3229 Doug Hawkins reported in the sdcc-user list that this works:
3237 SDCC> unix2dos sdcc.dsw
3243 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3247 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3248 flex.exe, and gawk.exe.
3249 One good place to get them is
3250 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3258 Download the file UnxUtils
3259 \begin_inset LatexCommand \index{UnxUtils}
3264 Now you have to install the utilities and setup MSVC so it can locate the
3266 Here there are two alternatives (choose one!):
3273 a) Extract UnxUtils.zip to your C:
3275 hard disk PRESERVING the original paths, otherwise bison won't work.
3276 (If you are using WinZip make certain that 'Use folder names' is selected)
3280 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3281 in 'Show directories for:' select 'Executable files', and in the directories
3282 window add a new path: 'C:
3292 (As a side effect, you get a bunch of Unix utilities that could be useful,
3293 such as diff and patch.)
3300 This one avoids extracting a bunch of files you may not use, but requires
3305 a) Create a directory were to put the tools needed, or use a directory already
3313 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3314 to such directory WITHOUT preserving the original paths.
3315 (If you are using WinZip make certain that 'Use folder names' is not selected)
3319 c) Rename bison.exe to '_bison.exe'.
3323 d) Create a batch file 'bison.bat' in 'C:
3327 ' and add these lines:
3347 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3351 Steps 'c' and 'd' are needed because bison requires by default that the
3352 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3353 '/usr/local/share/' I think.
3354 So it is necessary to tell bison where those files are located if they
3355 are not in such directory.
3356 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3360 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3361 in 'Show directories for:' select 'Executable files', and in the directories
3362 window add a new path: 'c:
3365 Note that you can use any other path instead of 'c:
3367 util', even the path where the Visual C++ tools are, probably: 'C:
3371 Microsoft Visual Studio
3376 So you don't have to execute step 'e' :)
3380 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3381 the executables from sdcc
3385 bin, and you can compile using SDCC.
3388 Building SDCC Using Borland
3391 From the sdcc directory, run the command "make -f Makefile.bcc".
3392 This should regenerate all the .exe files in the bin directory except for
3393 sdcdb.exe (which currently doesn't build under Borland C++).
3396 If you modify any source files and need to rebuild, be aware that the dependenci
3397 es may not be correctly calculated.
3398 The safest option is to delete all .obj files and run the build again.
3399 From a Cygwin BASH prompt, this can easily be done with the command (be
3400 sure you are in the sdcc directory):
3410 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3412 ) -print -exec rm {}
3421 or on Windows NT/2000/XP from the command prompt with the command:
3428 del /s *.obj *.lib *.rul
3431 from the sdcc directory.
3434 Windows Install Using a Binary Package
3435 \begin_inset LatexCommand \label{sub:Windows-Install}
3442 Download the binary package from
3443 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3447 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3448 This should unpack to a group of sub-directories.
3449 An example directory structure after unpacking the mingw32 package is:
3454 bin for the executables, c:
3462 lib for the include and libraries.
3465 Adjust your environment variable PATH to include the location of the bin
3466 directory or start sdcc using the full path.
3469 Building the Documentation
3472 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3473 as changing into the doc directory and typing
3477 \begin_inset Quotes srd
3481 \begin_inset Quotes srd
3488 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3491 \begin_inset LatexCommand \url{www.lyx.org}
3495 as editor this is straightforward.
3496 Prebuilt documentation in html and pdf format is available from
3497 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3504 Reading the Documentation
3507 Currently reading the document in pdf format is recommended, as for unknown
3508 reason the hyperlinks are working there whereas in the html version they
3515 If you should know why please drop us a note
3519 You'll find the pdf version at
3520 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
3526 This documentation is in some aspects different from a commercial documentation:
3530 It tries to document SDCC for several processor architectures in one document
3531 (commercially these probably would be separate documents/products).
3533 \begin_inset LatexCommand \index{Status of documentation}
3537 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3539 Z80, PIC14, PIC16 and HC08.
3542 There are many references pointing away from this documentation.
3543 Don't let this distract you.
3545 was a reference like
3546 \begin_inset LatexCommand \url{www.opencores.org}
3550 together with a statement
3551 \begin_inset Quotes sld
3554 some processors which are targetted by SDCC can be implemented in a
3571 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3576 \begin_inset Quotes srd
3579 we expect you to have a quick look there and come back.
3580 If you read this you are on the right track.
3583 Some sections attribute more space to problems, restrictions and warnings
3584 than to the solution.
3587 The installation section and the section about the debugger is intimidating.
3590 There are still lots of typos and there are more different writing styles
3594 Testing the SDCC Compiler
3595 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3602 The first thing you should do after installing your SDCC compiler is to
3618 \begin_inset LatexCommand \index{version}
3625 at the prompt, and the program should run and tell you the version.
3626 If it doesn't run, or gives a message about not finding sdcc program, then
3627 you need to check over your installation.
3628 Make sure that the sdcc bin directory is in your executable search path
3629 defined by the PATH environment setting (
3634 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3641 Install trouble-shooting for suggestions
3644 Make sure that the sdcc program is in the bin folder, if not perhaps something
3645 did not install correctly.
3653 is commonly installed as described in section
3654 \begin_inset Quotes sld
3657 Install and search paths
3658 \begin_inset Quotes srd
3667 Make sure the compiler works on a very simple example.
3668 Type in the following test.c program using your favorite
3694 Compile this using the following command:
3703 If all goes well, the compiler will generate a test.asm and test.rel file.
3704 Congratulations, you've just compiled your first program with SDCC.
3705 We used the -c option to tell SDCC not to link the generated code, just
3706 to keep things simple for this step.
3714 The next step is to try it with the linker.
3724 If all goes well the compiler will link with the libraries and produce
3725 a test.ihx output file.
3730 (no test.ihx, and the linker generates warnings), then the problem is most
3739 usr/local/share/sdcc/lib directory
3746 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3753 Install trouble-shooting for suggestions).
3761 The final test is to ensure
3769 header files and libraries.
3770 Edit test.c and change it to the following:
3787 strcpy(str1, "testing");
3794 Compile this by typing
3801 This should generate a test.ihx output file, and it should give no warnings
3802 such as not finding the string.h file.
3803 If it cannot find the string.h file, then the problem is that
3807 cannot find the /usr/local/share/sdcc/include directory
3814 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3821 Install trouble-shooting section for suggestions).
3839 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3843 to find exactly where SDCC is looking for the include and lib files.
3846 Install Trouble-shooting
3847 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3852 \begin_inset LatexCommand \index{Install trouble-shooting}
3859 If SDCC does not build correctly
3862 A thing to try is starting from scratch by unpacking the .tgz source package
3863 again in an empty directory.
3871 ./configure 2>&1 | tee configure.log
3885 make 2>&1 | tee make.log
3892 If anything goes wrong, you can review the log files to locate the problem.
3893 Or a relevant part of this can be attached to an email that could be helpful
3894 when requesting help from the mailing list.
3898 \begin_inset Quotes sld
3902 \begin_inset Quotes srd
3909 \begin_inset Quotes sld
3913 \begin_inset Quotes srd
3916 command is a script that analyzes your system and performs some configuration
3917 to ensure the source package compiles on your system.
3918 It will take a few minutes to run, and will compile a few tests to determine
3919 what compiler features are installed.
3923 \begin_inset Quotes sld
3927 \begin_inset Quotes srd
3933 This runs the GNU make tool, which automatically compiles all the source
3934 packages into the final installed binary executables.
3938 \begin_inset Quotes sld
3942 \begin_inset Quotes erd
3948 This will install the compiler, other executables libraries and include
3949 files into the appropriate directories.
3951 \begin_inset LatexCommand \ref{sub:Install-paths}
3957 \begin_inset LatexCommand \ref{sub:Search-Paths}
3962 about install and search paths.
3964 On most systems you will need super-user privileges to do this.
3970 SDCC is not just a compiler, but a collection of tools by various developers.
3971 These include linkers, assemblers, simulators and other components.
3972 Here is a summary of some of the components.
3973 Note that the included simulator and assembler have separate documentation
3974 which you can find in the source package in their respective directories.
3975 As SDCC grows to include support for other processors, other packages from
3976 various developers are included and may have their own sets of documentation.
3980 You might want to look at the files which are installed in <installdir>.
3981 At the time of this writing, we find the following programs for gcc-builds:
3985 In <installdir>/bin:
3988 sdcc - The compiler.
3991 sdcpp - The C preprocessor.
3994 asx8051 - The assembler for 8051 type processors.
4001 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4004 aslink -The linker for 8051 type processors.
4011 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4014 s51 - The ucSim 8051 simulator.
4017 sdcdb - The source debugger.
4020 packihx - A tool to pack (compress) Intel hex files.
4023 In <installdir>/share/sdcc/include
4029 In <installdir>/share/sdcc/lib
4032 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4036 In <installdir>/share/sdcc/doc
4042 As development for other processors proceeds, this list will expand to include
4043 executables to support processors like AVR, PIC, etc.
4049 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4050 the assembler and linkage editor.
4053 sdcpp - The C-Preprocessor
4057 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4061 is a modified version of the GNU preprocessor.
4062 The C preprocessor is used to pull in #include sources, process #ifdef
4063 statements, #defines and so on.
4074 - The Assemblers and Linkage Editors
4077 This is retargettable assembler & linkage editor, it was developed by Alan
4079 John Hartman created the version for 8051, and I (Sandeep) have made some
4080 enhancements and bug fixes for it to work properly with SDCC.
4087 \begin_inset LatexCommand \index{s51}
4091 is a freeware, opensource simulator developed by Daniel Drotos (
4092 \begin_inset LatexCommand \url{mailto:drdani@mazsola.iit.uni-miskolc.hu}
4097 The simulator is built as part of the build process.
4098 For more information visit Daniel's web site at:
4099 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4104 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4108 sdcdb - Source Level Debugger
4112 \begin_inset LatexCommand \index{sdcdb (debugger)}
4116 is the companion source level debugger.
4117 More about sdcdb in section
4118 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4123 The current version of the debugger uses Daniel's Simulator S51
4124 \begin_inset LatexCommand \index{s51}
4128 , but can be easily changed to use other simulators.
4138 Single Source File Projects
4141 For single source file 8051 projects the process is very simple.
4142 Compile your programs with the following command
4145 "sdcc sourcefile.c".
4149 This will compile, assemble and link your source file.
4150 Output files are as follows:
4154 \begin_inset LatexCommand \index{<file>.asm}
4159 \begin_inset LatexCommand \index{Assembler source}
4163 file created by the compiler
4167 \begin_inset LatexCommand \index{<file>.lst}
4172 \begin_inset LatexCommand \index{Assembler listing}
4176 file created by the Assembler
4180 \begin_inset LatexCommand \index{<file>.rst}
4185 \begin_inset LatexCommand \index{Assembler listing}
4189 file updated with linkedit information, created by linkage editor
4193 \begin_inset LatexCommand \index{<file>.sym}
4198 \begin_inset LatexCommand \index{Symbol listing}
4202 for the sourcefile, created by the assembler
4206 \begin_inset LatexCommand \index{<file>.rel}
4211 \begin_inset LatexCommand \index{<file>.o}
4216 \begin_inset LatexCommand \index{Object file}
4220 created by the assembler, input to Linkage editor
4224 \begin_inset LatexCommand \index{<file>.map}
4229 \begin_inset LatexCommand \index{Memory map}
4233 for the load module, created by the Linker
4237 \begin_inset LatexCommand \index{<file>.mem}
4241 - A file with a summary of the memory usage
4245 \begin_inset LatexCommand \index{<file>.ihx}
4249 - The load module in Intel hex format
4250 \begin_inset LatexCommand \index{Intel hex format}
4254 (you can select the Motorola S19 format
4255 \begin_inset LatexCommand \index{Motorola S19 format}
4270 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4275 If you need another format you might want to use
4282 \begin_inset LatexCommand \index{objdump (tool)}
4293 \begin_inset LatexCommand \index{srecord (tool)}
4298 Both formats are documented in the documentation of srecord
4299 \begin_inset LatexCommand \index{srecord (tool)}
4307 \begin_inset LatexCommand \index{<file>.adb}
4311 - An intermediate file containing debug information needed to create the
4323 \begin_inset LatexCommand \index{-\/-debug}
4331 \begin_inset LatexCommand \index{<file>.cdb}
4335 - An optional file (with -
4345 -debug) containing debug information.
4346 The format is documented in cdbfileformat.pdf.
4351 \begin_inset LatexCommand \index{<file> (no extension)}
4355 An optional AOMF or AOMF51
4356 \begin_inset LatexCommand \index{AOMF, AOMF51}
4360 file containing debug information (generated with option -
4387 ormat is commonly used by third party tools (debuggers
4388 \begin_inset LatexCommand \index{Debugger}
4392 , simulators, emulators)
4396 \begin_inset LatexCommand \index{<file>.dump*}
4400 - Dump file to debug the compiler it self (generated with option -
4410 -dumpall) (see section
4411 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4417 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4423 \begin_inset Quotes sld
4426 Anatomy of the compiler
4427 \begin_inset Quotes srd
4433 Projects with Multiple Source Files
4436 SDCC can compile only ONE file at a time.
4437 Let us for example assume that you have a project containing the following
4442 foo1.c (contains some functions)
4444 foo2.c (contains some more functions)
4446 foomain.c (contains more functions and the function main)
4454 The first two files will need to be compiled separately with the commands:
4486 Then compile the source file containing the
4491 \begin_inset LatexCommand \index{Linker}
4495 the files together with the following command:
4503 foomain.c\SpecialChar ~
4504 foo1.rel\SpecialChar ~
4509 \begin_inset LatexCommand \index{<file>.rel}
4521 can be separately compiled as well:
4532 sdcc foomain.rel foo1.rel foo2.rel
4539 The file containing the
4554 file specified in the command line, since the linkage editor processes
4555 file in the order they are presented to it.
4556 The linker is invoked from SDCC using a script file with extension .lnk
4557 \begin_inset LatexCommand \index{<file>.lnk}
4562 You can view this file to troubleshoot linking problems such as those arising
4563 from missing libraries.
4566 Projects with Additional Libraries
4567 \begin_inset LatexCommand \index{Libraries}
4574 Some reusable routines may be compiled into a library, see the documentation
4575 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4579 \begin_inset LatexCommand \index{<file>.lib}
4586 Libraries created in this manner can be included in the command line.
4587 Make sure you include the -L <library-path> option to tell the linker where
4588 to look for these files if they are not in the current directory.
4589 Here is an example, assuming you have the source file
4601 (if that is not the same as your current project):
4608 sdcc foomain.c foolib.lib -L mylib
4619 must be an absolute path name.
4623 The most efficient way to use libraries is to keep separate modules in separate
4625 The lib file now should name all the modules.rel
4626 \begin_inset LatexCommand \index{<file>.rel}
4631 For an example see the standard library file
4635 in the directory <installdir>/share/lib/small.
4638 Using sdcclib to Create and Manage Libraries
4639 \begin_inset LatexCommand \index{sdcclib}
4646 Alternatively, instead of having a .rel file for each entry on the library
4647 file as described in the preceding section, sdcclib can be used to embed
4648 all the modules belonging to such library in the library file itself.
4649 This results in a larger library file, but it greatly reduces the number
4650 of disk files accessed by the linker.
4651 Additionally, the packed library file contains an index of all include
4652 modules and symbols that significantly speeds up the linking process.
4653 To display a list of options supported by sdcclib type:
4662 \begin_inset LatexCommand \index{sdcclib}
4673 To create a new library file, start by compiling all the required modules.
4711 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4713 The next step is to add the .rel files to the library file:
4721 sdcclib libint.lib _divsint.rel
4724 \begin_inset LatexCommand \index{sdcclib}
4734 sdcclib libint.lib _divuint.rel
4740 sdcclib libint.lib _modsint.rel
4746 sdcclib libint.lib _moduint.rel
4752 sdcclib libint.lib _mulint.rel
4759 If the file already exists in the library, it will be replaced.
4760 To see what modules and symbols are included in the library, options -s
4761 and -m are available.
4769 sdcclib -s libint.lib
4772 \begin_inset LatexCommand \index{sdcclib}
4882 If the source files are compiled using -
4893 \begin_inset LatexCommand \index{-\/-debug}
4897 , the corresponding debug information file .adb will be include in the library
4899 The library files created with sdcclib are plain text files, so they can
4900 be viewed with a text editor.
4901 It is not recomended to modify a library file created with sdcclib using
4902 a text editor, as there are file indexes numbers located accross the file
4903 used by the linker to quickly locate the required module to link.
4904 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
4905 it can be safely deleted, since all the information required for linking
4906 is embedded in the library file itself.
4907 Library files created using sdcclib are used as described in the preceding
4911 Command Line Options
4912 \begin_inset LatexCommand \index{Command Line Options}
4919 Processor Selection Options
4920 \begin_inset LatexCommand \index{Options processor selection}
4925 \begin_inset LatexCommand \index{Processor selection options}
4931 \labelwidthstring 00.00.0000
4936 \begin_inset LatexCommand \index{-mmcs51}
4942 Generate code for the Intel MCS51
4943 \begin_inset LatexCommand \index{MCS51}
4947 family of processors.
4948 This is the default processor target.
4950 \labelwidthstring 00.00.0000
4955 \begin_inset LatexCommand \index{-mds390}
4961 Generate code for the Dallas DS80C390
4962 \begin_inset LatexCommand \index{DS80C390}
4968 \labelwidthstring 00.00.0000
4973 \begin_inset LatexCommand \index{-mds400}
4979 Generate code for the Dallas DS80C400
4980 \begin_inset LatexCommand \index{DS80C400}
4986 \labelwidthstring 00.00.0000
4991 \begin_inset LatexCommand \index{-mhc08}
4997 Generate code for the Motorola HC08
4998 \begin_inset LatexCommand \index{HC08}
5002 family of processors (added Oct 2003).
5004 \labelwidthstring 00.00.0000
5009 \begin_inset LatexCommand \index{-mz80}
5015 Generate code for the Zilog Z80
5016 \begin_inset LatexCommand \index{Z80}
5020 family of processors.
5022 \labelwidthstring 00.00.0000
5027 \begin_inset LatexCommand \index{-mgbz80}
5033 Generate code for the GameBoy Z80
5034 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5038 processor (Not actively maintained).
5040 \labelwidthstring 00.00.0000
5045 \begin_inset LatexCommand \index{-mavr}
5051 Generate code for the Atmel AVR
5052 \begin_inset LatexCommand \index{AVR}
5056 processor (In development, not complete).
5057 AVR users should probably have a look at avr-gcc
5058 \begin_inset LatexCommand \url{ http://savannah.nongnu.org/download/avr-libc/snapshots/}
5063 \begin_inset LatexCommand \url{http://winavr.sourceforge.net}
5070 I think it is fair to direct users there for now.
5071 Open source is also about avoiding unnecessary work .
5072 But I didn't find the 'official' link.
5074 \labelwidthstring 00.00.0000
5079 \begin_inset LatexCommand \index{-mpic14}
5085 Generate code for the Microchip PIC 14
5086 \begin_inset LatexCommand \index{PIC14}
5090 -bit processors (p16f84 and variants.
5091 In development, not complete).
5094 p16f627 p16f628 p16f84 p16f873 p16f877?
5096 \labelwidthstring 00.00.0000
5101 \begin_inset LatexCommand \index{-mpic16}
5107 Generate code for the Microchip PIC 16
5108 \begin_inset LatexCommand \index{PIC16}
5112 -bit processors (p18f452 and variants.
5113 In development, not complete).
5115 \labelwidthstring 00.00.0000
5121 Generate code for the Toshiba TLCS-900H
5122 \begin_inset LatexCommand \index{TLCS-900H}
5126 processor (Not maintained, not complete).
5128 \labelwidthstring 00.00.0000
5133 \begin_inset LatexCommand \index{-mxa51}
5139 Generate code for the Phillips XA51
5140 \begin_inset LatexCommand \index{XA51}
5144 processor (Not maintained, not complete).
5147 Preprocessor Options
5148 \begin_inset LatexCommand \index{Options preprocessor}
5153 \begin_inset LatexCommand \index{Preprocessor options}
5158 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5164 \labelwidthstring 00.00.0000
5169 \begin_inset LatexCommand \index{-I<path>}
5175 The additional location where the pre processor will look for <..h> or
5176 \begin_inset Quotes eld
5180 \begin_inset Quotes erd
5185 \labelwidthstring 00.00.0000
5190 \begin_inset LatexCommand \index{-D<macro[=value]>}
5196 Command line definition of macros.
5197 Passed to the preprocessor.
5199 \labelwidthstring 00.00.0000
5204 \begin_inset LatexCommand \index{-M}
5210 Tell the preprocessor to output a rule suitable for make describing the
5211 dependencies of each object file.
5212 For each source file, the preprocessor outputs one make-rule whose target
5213 is the object file name for that source file and whose dependencies are
5214 all the files `#include'd in it.
5215 This rule may be a single line or may be continued with `
5217 '-newline if it is long.
5218 The list of rules is printed on standard output instead of the preprocessed
5221 \begin_inset LatexCommand \index{-E}
5227 \labelwidthstring 00.00.0000
5232 \begin_inset LatexCommand \index{-C}
5238 Tell the preprocessor not to discard comments.
5239 Used with the `-E' option.
5241 \labelwidthstring 00.00.0000
5246 \begin_inset LatexCommand \index{-MM}
5257 Like `-M' but the output mentions only the user header files included with
5259 \begin_inset Quotes eld
5263 System header files included with `#include <file>' are omitted.
5265 \labelwidthstring 00.00.0000
5270 \begin_inset LatexCommand \index{-Aquestion(answer)}
5276 Assert the answer answer for question, in case it is tested with a preprocessor
5277 conditional such as `#if #question(answer)'.
5278 `-A-' disables the standard assertions that normally describe the target
5281 \labelwidthstring 00.00.0000
5286 \begin_inset LatexCommand \index{-Umacro}
5292 Undefine macro macro.
5293 `-U' options are evaluated after all `-D' options, but before any `-include'
5294 and `-imacros' options.
5296 \labelwidthstring 00.00.0000
5301 \begin_inset LatexCommand \index{-dM}
5307 Tell the preprocessor to output only a list of the macro definitions that
5308 are in effect at the end of preprocessing.
5309 Used with the `-E' option.
5311 \labelwidthstring 00.00.0000
5316 \begin_inset LatexCommand \index{-dD}
5322 Tell the preprocessor to pass all macro definitions into the output, in
5323 their proper sequence in the rest of the output.
5325 \labelwidthstring 00.00.0000
5330 \begin_inset LatexCommand \index{-dN}
5341 Like `-dD' except that the macro arguments and contents are omitted.
5342 Only `#define name' is included in the output.
5344 \labelwidthstring 00.00.0000
5349 preprocessorOption[,preprocessorOption]
5352 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5357 Pass the preprocessorOption to the preprocessor
5362 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5367 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5368 Collection (gcc), if you need more dedicated options please refer to the
5370 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5378 \begin_inset LatexCommand \index{Options linker}
5383 \begin_inset LatexCommand \index{Linker options}
5389 \labelwidthstring 00.00.0000
5409 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5414 \begin_inset LatexCommand \index{-L -\/-lib-path}
5421 <absolute path to additional libraries> This option is passed to the linkage
5422 editor's additional libraries
5423 \begin_inset LatexCommand \index{Libraries}
5428 The path name must be absolute.
5429 Additional library files may be specified in the command line.
5430 See section Compiling programs for more details.
5432 \labelwidthstring 00.00.0000
5449 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5454 <Value> The start location of the external ram
5455 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5459 , default value is 0.
5460 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5470 -xram-loc 0x8000 or -
5482 \labelwidthstring 00.00.0000
5499 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5504 <Value> The start location of the code
5505 \begin_inset LatexCommand \index{code}
5509 segment, default value 0.
5510 Note when this option is used the interrupt vector table is also relocated
5511 to the given address.
5512 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5522 -code-loc 0x8000 or -
5534 \labelwidthstring 00.00.0000
5551 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5556 <Value> By default the stack
5557 \begin_inset LatexCommand \index{stack}
5561 is placed after the data segment.
5562 Using this option the stack can be placed anywhere in the internal memory
5564 The value entered can be in Hexadecimal or Decimal format, e.g.
5575 -stack-loc 0x20 or -
5586 Since the sp register is incremented before a push or call, the initial
5587 sp will be set to one byte prior the provided value.
5588 The provided value should not overlap any other memory areas such as used
5589 register banks or the data segment and with enough space for the current
5592 \labelwidthstring 00.00.0000
5609 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5614 <Value> The start location of the internal ram data
5615 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5620 The value entered can be in Hexadecimal or Decimal format, eg.
5642 (By default, the start location of the internal ram data segment is set
5643 as low as possible in memory, taking into account the used register banks
5644 and the bit segment at address 0x20.
5645 For example if register banks 0 and 1 are used without bit variables, the
5646 data segment will be set, if -
5656 -data-loc is not used, to location 0x10.)
5658 \labelwidthstring 00.00.0000
5675 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5680 <Value> The start location of the indirectly addressable internal ram
5681 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5685 of the 8051, default value is 0x80.
5686 The value entered can be in Hexadecimal or Decimal format, eg.
5697 -idata-loc 0x88 or -
5709 \labelwidthstring 00.00.0000
5726 <Value> The start location of the bit
5727 \begin_inset LatexCommand \index{bit}
5731 addressable internal ram of the 8051.
5737 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5740 \labelwidthstring 00.00.0000
5755 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5764 The linker output (final object code) is in Intel Hex format.
5765 \begin_inset LatexCommand \index{Intel hex format}
5769 This is the default option.
5770 The format itself is documented in the documentation of srecord
5771 \begin_inset LatexCommand \index{srecord (tool)}
5777 \labelwidthstring 00.00.0000
5792 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5801 The linker output (final object code) is in Motorola S19 format
5802 \begin_inset LatexCommand \index{Motorola S19 format}
5807 The format itself is documented in the documentation of srecord.
5809 \labelwidthstring 00.00.0000
5814 linkOption[,linkOption]
5817 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5822 Pass the linkOption to the linker.
5823 See file sdcc/as/doc/asxhtm.html for more on linker options.
5827 \begin_inset LatexCommand \index{Options MCS51}
5832 \begin_inset LatexCommand \index{MCS51 options}
5838 \labelwidthstring 00.00.0000
5853 \begin_inset LatexCommand \index{-\/-model-small}
5864 Generate code for Small Model programs, see section Memory Models for more
5866 This is the default model.
5868 \labelwidthstring 00.00.0000
5883 \begin_inset LatexCommand \index{-\/-model-large}
5889 Generate code for Large model programs, see section Memory Models for more
5891 If this option is used all source files in the project have to be compiled
5894 \labelwidthstring 00.00.0000
5909 \begin_inset LatexCommand \index{-\/-xstack}
5915 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
5916 variables and passing parameters.
5918 \begin_inset LatexCommand \ref{sub:External-Stack}
5923 External Stack for more details.
5925 \labelwidthstring 00.00.0000
5943 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
5947 Causes the linker to check if the internal ram usage is within limits of
5950 \labelwidthstring 00.00.0000
5968 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
5972 Causes the linker to check if the external ram usage is within limits of
5975 \labelwidthstring 00.00.0000
5993 \begin_inset LatexCommand \index{-\/-code-size <Value>}
5997 Causes the linker to check if the code memory usage is within limits of
6000 \labelwidthstring 00.00.0000
6018 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6022 Causes the linker to check if there is at minimum <Value> bytes for stack.
6024 \labelwidthstring 00.00.0000
6042 \begin_inset LatexCommand \index{-\/-pack-iram}
6046 Causes the linker use unused register banks for data variables or stack.
6049 DS390 / DS400 Options
6050 \begin_inset LatexCommand \index{Options DS390}
6055 \begin_inset LatexCommand \index{DS390 options}
6061 \labelwidthstring 00.00.0000
6078 \begin_inset LatexCommand \index{-\/-model-flat24}
6088 Generate 24-bit flat mode code.
6089 This is the one and only that the ds390 code generator supports right now
6090 and is default when using
6095 See section Memory Models for more details.
6097 \labelwidthstring 00.00.0000
6112 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6118 disable interrupts during ESP:SP updates.
6120 \labelwidthstring 00.00.0000
6137 \begin_inset LatexCommand \index{-\/-stack-10bit}
6141 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6142 This is the one and only that the ds390 code generator supports right now
6143 and is default when using
6148 In this mode, the stack is located in the lower 1K of the internal RAM,
6149 which is mapped to 0x400000.
6150 Note that the support is incomplete, since it still uses a single byte
6151 as the stack pointer.
6152 This means that only the lower 256 bytes of the potential 1K stack space
6153 will actually be used.
6154 However, this does allow you to reclaim the precious 256 bytes of low RAM
6155 for use for the DATA and IDATA segments.
6156 The compiler will not generate any code to put the processor into 10 bit
6158 It is important to ensure that the processor is in this mode before calling
6159 any re-entrant functions compiled with this option.
6160 In principle, this should work with the
6173 \begin_inset LatexCommand \index{-\/-stack-auto}
6179 option, but that has not been tested.
6180 It is incompatible with the
6193 \begin_inset LatexCommand \index{-\/-xstack}
6200 It also only makes sense if the processor is in 24 bit contiguous addressing
6213 -model-flat24 option
6217 \labelwidthstring 00.00.0000
6232 \begin_inset LatexCommand \index{-\/-stack-probe}
6238 insert call to function __stack_probe at each function prologue.
6240 \labelwidthstring 00.00.0000
6255 \begin_inset LatexCommand \index{-\/-tini-libid}
6261 <nnnn> LibraryID used in -mTININative.
6264 \labelwidthstring 00.00.0000
6279 \begin_inset LatexCommand \index{-\/-use-accelerator}
6285 generate code for DS390 Arithmetic Accelerator.
6290 \begin_inset LatexCommand \index{Options Z80}
6295 \begin_inset LatexCommand \index{Z80 options}
6301 \labelwidthstring 00.00.0000
6318 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6328 Force a called function to always save BC.
6330 \labelwidthstring 00.00.0000
6347 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6351 When linking, skip the standard crt0.o object file.
6352 You must provide your own crt0.o for your system when linking.
6356 Optimization Options
6357 \begin_inset LatexCommand \index{Options optimization}
6362 \begin_inset LatexCommand \index{Optimization options}
6368 \labelwidthstring 00.00.0000
6383 \begin_inset LatexCommand \index{-\/-nogcse}
6389 Will not do global subexpression elimination, this option may be used when
6390 the compiler creates undesirably large stack/data spaces to store compiler
6392 A warning message will be generated when this happens and the compiler
6393 will indicate the number of extra bytes it allocated.
6394 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6396 \begin_inset LatexCommand \index{\#pragma nogcse}
6400 can be used to turn off global subexpression elimination
6401 \begin_inset LatexCommand \index{Subexpression elimination}
6405 for a given function only.
6407 \labelwidthstring 00.00.0000
6422 \begin_inset LatexCommand \index{-\/-noinvariant}
6428 Will not do loop invariant optimizations, this may be turned off for reasons
6429 explained for the previous option.
6430 For more details of loop optimizations performed see Loop Invariants in
6432 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6437 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6439 \begin_inset LatexCommand \index{\#pragma noinvariant}
6443 can be used to turn off invariant optimizations for a given function only.
6445 \labelwidthstring 00.00.0000
6460 \begin_inset LatexCommand \index{-\/-noinduction}
6466 Will not do loop induction optimizations, see section strength reduction
6468 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6470 \begin_inset LatexCommand \index{\#pragma noinduction}
6474 can be used to turn off induction optimizations for a given function only.
6476 \labelwidthstring 00.00.0000
6491 \begin_inset LatexCommand \index{-\/-nojtbound}
6502 Will not generate boundary condition check when switch statements
6503 \begin_inset LatexCommand \index{switch statement}
6507 are implemented using jump-tables.
6509 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6514 Switch Statements for more details.
6515 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6517 \begin_inset LatexCommand \index{\#pragma nojtbound}
6521 can be used to turn off boundary checking for jump tables for a given function
6524 \labelwidthstring 00.00.0000
6539 \begin_inset LatexCommand \index{-\/-noloopreverse}
6548 Will not do loop reversal
6549 \begin_inset LatexCommand \index{Loop reversing}
6555 \labelwidthstring 00.00.0000
6572 \begin_inset LatexCommand \index{-\/-nolabelopt }
6576 Will not optimize labels (makes the dumpfiles more readable).
6578 \labelwidthstring 00.00.0000
6593 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6599 Will not memcpy initialized data from code space into xdata space.
6600 This saves a few bytes in code space if you don't have initialized data.
6602 \labelwidthstring 00.00.0000
6617 \begin_inset LatexCommand \index{-\/-nooverlay}
6623 The compiler will not overlay parameters and local variables of any function,
6624 see section Parameters and local variables for more details.
6626 \labelwidthstring 00.00.0000
6641 \begin_inset LatexCommand \index{-\/-no-peep}
6647 Disable peep-hole optimization.
6649 \labelwidthstring 00.00.0000
6666 \begin_inset LatexCommand \index{-\/-peep-file}
6671 <filename> This option can be used to use additional rules to be used by
6672 the peep hole optimizer.
6674 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6679 Peep Hole optimizations for details on how to write these rules.
6681 \labelwidthstring 00.00.0000
6696 \begin_inset LatexCommand \index{-\/-peep-asm}
6702 Pass the inline assembler code through the peep hole optimizer.
6703 This can cause unexpected changes to inline assembler code, please go through
6704 the peephole optimizer
6705 \begin_inset LatexCommand \index{Peephole optimizer}
6709 rules defined in the source file tree '<target>/peeph.def' before using
6712 \labelwidthstring 00.00.0000
6727 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6733 The compiler will optimize code generation towards fast code, possibly
6734 at the expense of code size.
6736 \labelwidthstring 00.00.0000
6751 \begin_inset LatexCommand \index{-\/-opt-code-size}
6757 The compiler will optimize code generation towards compact code, possibly
6758 at the expense of code speed.
6762 \begin_inset LatexCommand \index{Options other}
6768 \labelwidthstring 00.00.0000
6784 \begin_inset LatexCommand \index{-\/-compile-only}
6789 \begin_inset LatexCommand \index{-c -\/-compile-only}
6795 will compile and assemble the source, but will not call the linkage editor.
6797 \labelwidthstring 00.00.0000
6816 \begin_inset LatexCommand \index{-\/-c1mode}
6822 reads the preprocessed source from standard input and compiles it.
6823 The file name for the assembler output must be specified using the -o option.
6825 \labelwidthstring 00.00.0000
6830 \begin_inset LatexCommand \index{-E}
6836 Run only the C preprocessor.
6837 Preprocess all the C source files specified and output the results to standard
6840 \labelwidthstring 00.00.0000
6846 \begin_inset LatexCommand \index{-o <path/file>}
6852 The output path resp.
6853 file where everything will be placed.
6854 If the parameter is a path, it must have a trailing slash (or backslash
6855 for the Windows binaries) to be recognized as a path.
6858 \labelwidthstring 00.00.0000
6873 \begin_inset LatexCommand \index{-\/-stack-auto}
6884 All functions in the source file will be compiled as
6889 \begin_inset LatexCommand \index{reentrant}
6894 the parameters and local variables will be allocated on the stack
6895 \begin_inset LatexCommand \index{stack}
6901 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
6905 Parameters and Local Variables for more details.
6906 If this option is used all source files in the project should be compiled
6910 \labelwidthstring 00.00.0000
6925 \begin_inset LatexCommand \index{-\/-callee-saves}
6929 function1[,function2][,function3]....
6932 The compiler by default uses a caller saves convention for register saving
6933 across function calls, however this can cause unnecessary register pushing
6934 & popping when calling small functions from larger functions.
6935 This option can be used to switch the register saving convention for the
6936 function names specified.
6937 The compiler will not save registers when calling these functions, no extra
6938 code will be generated at the entry & exit (function prologue
6941 \begin_inset LatexCommand \index{function prologue}
6950 \begin_inset LatexCommand \index{function epilogue}
6956 ) for these functions to save & restore the registers used by these functions,
6957 this can SUBSTANTIALLY reduce code & improve run time performance of the
6959 In the future the compiler (with inter procedural analysis) will be able
6960 to determine the appropriate scheme to use for each function call.
6961 DO NOT use this option for built-in functions such as _mulint..., if this
6962 option is used for a library function the appropriate library function
6963 needs to be recompiled with the same option.
6964 If the project consists of multiple source files then all the source file
6965 should be compiled with the same -
6975 -callee-saves option string.
6976 Also see #pragma\SpecialChar ~
6978 \begin_inset LatexCommand \index{\#pragma callee\_saves}
6984 \labelwidthstring 00.00.0000
6999 \begin_inset LatexCommand \index{-\/-debug}
7008 When this option is used the compiler will generate debug information.
7009 The debug information collected in a file with .cdb extension can be used
7011 For more information see documentation for SDCDB.
7012 Another file with no extension contains debug information in AOMF or AOMF51
7013 \begin_inset LatexCommand \index{AOMF, AOMF51}
7017 format which is commonly used by third party tools.
7019 \labelwidthstring 00.00.0000
7024 \begin_inset LatexCommand \index{-S}
7035 Stop after the stage of compilation proper; do not assemble.
7036 The output is an assembler code file for the input file specified.
7038 \labelwidthstring 00.00.0000
7053 \begin_inset LatexCommand \index{-\/-int-long-reent}
7059 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7060 Note by default these libraries are compiled as non-reentrant.
7061 See section Installation for more details.
7063 \labelwidthstring 00.00.0000
7078 \begin_inset LatexCommand \index{-\/-cyclomatic}
7087 This option will cause the compiler to generate an information message for
7088 each function in the source file.
7089 The message contains some
7093 information about the function.
7094 The number of edges and nodes the compiler detected in the control flow
7095 graph of the function, and most importantly the
7097 cyclomatic complexity
7098 \begin_inset LatexCommand \index{Cyclomatic complexity}
7104 see section on Cyclomatic Complexity for more details.
7106 \labelwidthstring 00.00.0000
7121 \begin_inset LatexCommand \index{-\/-float-reent}
7127 Floating point library is compiled as reentrant
7128 \begin_inset LatexCommand \index{reentrant}
7133 See section Installation for more details.
7135 \labelwidthstring 00.00.0000
7150 \begin_inset LatexCommand \index{-\/-main-return}
7156 This option can be used if the code generated is called by a monitor program
7157 or if the main routine includes an endless loop.
7158 This option might result in slightly smaller code and save two bytes of
7160 The return from the 'main'
7161 \begin_inset LatexCommand \index{main return}
7165 function will return to the function calling main.
7166 The default setting is to lock up i.e.
7173 \labelwidthstring 00.00.0000
7188 \begin_inset LatexCommand \index{-\/-nostdincl}
7194 This will prevent the compiler from passing on the default include path
7195 to the preprocessor.
7197 \labelwidthstring 00.00.0000
7212 \begin_inset LatexCommand \index{-\/-nostdlib}
7218 This will prevent the compiler from passing on the default library
7219 \begin_inset LatexCommand \index{Libraries}
7225 \labelwidthstring 00.00.0000
7240 \begin_inset LatexCommand \index{-\/-verbose}
7246 Shows the various actions the compiler is performing.
7248 \labelwidthstring 00.00.0000
7253 \begin_inset LatexCommand \index{-V}
7259 Shows the actual commands the compiler is executing.
7261 \labelwidthstring 00.00.0000
7276 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7282 Hides your ugly and inefficient c-code from the asm file, so you can always
7283 blame the compiler :)
7285 \labelwidthstring 00.00.0000
7300 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7306 Will not include peep-hole comments in the generated files.
7308 \labelwidthstring 00.00.0000
7323 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7329 Include i-codes in the asm file.
7330 Sounds like noise but is most helpful for debugging the compiler itself.
7332 \labelwidthstring 00.00.0000
7347 \begin_inset LatexCommand \index{-\/-less-pedantic}
7353 Disable some of the more pedantic warnings
7354 \begin_inset LatexCommand \index{Warnings}
7358 (jwk burps: please be more specific here, please!).
7359 If you want rather more than less warnings you should consider using a
7360 separate tool dedicated to syntax checking like splint
7361 \begin_inset LatexCommand \url{www.splint.org}
7367 \labelwidthstring 00.00.0000
7381 -disable-warning\SpecialChar ~
7383 \begin_inset LatexCommand \index{-\/-disable-warning}
7389 Disable specific warning with number <nnnn>.
7391 \labelwidthstring 00.00.0000
7406 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7412 Display the directories in the compiler's search path
7414 \labelwidthstring 00.00.0000
7429 \begin_inset LatexCommand \index{-\/-vc}
7435 Display errors and warnings using MSVC style, so you can use SDCC with
7438 \labelwidthstring 00.00.0000
7453 \begin_inset LatexCommand \index{-\/-use-stdout}
7459 Send errors and warnings to stdout instead of stderr.
7461 \labelwidthstring 00.00.0000
7466 asmOption[,asmOption]
7469 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7474 Pass the asmOption to the assembler
7475 \begin_inset LatexCommand \index{Options assembler}
7480 \begin_inset LatexCommand \index{Assembler options}
7485 See file sdcc/as/doc/asxhtm.html for assembler options.
7488 Intermediate Dump Options
7489 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7494 \begin_inset LatexCommand \index{Options intermediate dump}
7499 \begin_inset LatexCommand \index{Intermediate dump options}
7506 The following options are provided for the purpose of retargetting and debugging
7508 These provided a means to dump the intermediate code (iCode
7509 \begin_inset LatexCommand \index{iCode}
7513 ) generated by the compiler in human readable form at various stages of
7514 the compilation process.
7515 More on iCodes see chapter
7516 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7521 \begin_inset Quotes srd
7524 The anatomy of the compiler
7525 \begin_inset Quotes srd
7530 \labelwidthstring 00.00.0000
7545 \begin_inset LatexCommand \index{-\/-dumpraw}
7551 This option will cause the compiler to dump the intermediate code into
7554 <source filename>.dumpraw
7556 just after the intermediate code has been generated for a function, i.e.
7557 before any optimizations are done.
7559 \begin_inset LatexCommand \index{Basic blocks}
7563 at this stage ordered in the depth first number, so they may not be in
7564 sequence of execution.
7566 \labelwidthstring 00.00.0000
7581 \begin_inset LatexCommand \index{-\/-dumpgcse}
7587 Will create a dump of iCode's, after global subexpression elimination
7588 \begin_inset LatexCommand \index{Global subexpression elimination}
7594 <source filename>.dumpgcse.
7596 \labelwidthstring 00.00.0000
7611 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7617 Will create a dump of iCode's, after deadcode elimination
7618 \begin_inset LatexCommand \index{Dead-code elimination}
7624 <source filename>.dumpdeadcode.
7626 \labelwidthstring 00.00.0000
7641 \begin_inset LatexCommand \index{-\/-dumploop}
7650 Will create a dump of iCode's, after loop optimizations
7651 \begin_inset LatexCommand \index{Loop optimization}
7657 <source filename>.dumploop.
7659 \labelwidthstring 00.00.0000
7674 \begin_inset LatexCommand \index{-\/-dumprange}
7683 Will create a dump of iCode's, after live range analysis
7684 \begin_inset LatexCommand \index{Live range analysis}
7690 <source filename>.dumprange.
7692 \labelwidthstring 00.00.0000
7707 \begin_inset LatexCommand \index{-\/-dumlrange}
7713 Will dump the life ranges
7714 \begin_inset LatexCommand \index{Live range analysis}
7720 \labelwidthstring 00.00.0000
7735 \begin_inset LatexCommand \index{-\/-dumpregassign}
7744 Will create a dump of iCode's, after register assignment
7745 \begin_inset LatexCommand \index{Register assignment}
7751 <source filename>.dumprassgn.
7753 \labelwidthstring 00.00.0000
7768 \begin_inset LatexCommand \index{-\/-dumplrange}
7774 Will create a dump of the live ranges of iTemp's
7776 \labelwidthstring 00.00.0000
7791 \begin_inset LatexCommand \index{-\/-dumpall}
7802 Will cause all the above mentioned dumps to be created.
7805 Redirecting output on Windows Shells
7808 By default SDCC writes it's error messages to
7809 \begin_inset Quotes sld
7813 \begin_inset Quotes srd
7817 To force all messages to
7818 \begin_inset Quotes sld
7822 \begin_inset Quotes srd
7846 \begin_inset LatexCommand \index{-\/-use-stdout}
7851 Additionally, if you happen to have visual studio installed in your windows
7852 machine, you can use it to compile your sources using a custom build and
7868 \begin_inset LatexCommand \index{-\/-vc}
7873 Something like this should work:
7917 -model-large -c $(InputPath)
7920 Environment variables
7921 \begin_inset LatexCommand \index{Environment variables}
7928 SDCC recognizes the following environment variables:
7930 \labelwidthstring 00.00.0000
7935 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7941 SDCC installs a signal handler
7942 \begin_inset LatexCommand \index{signal handler}
7946 to be able to delete temporary files after an user break (^C) or an exception.
7947 If this environment variable is set, SDCC won't install the signal handler
7948 in order to be able to debug SDCC.
7950 \labelwidthstring 00.00.0000
7957 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7963 Path, where temporary files will be created.
7964 The order of the variables is the search order.
7965 In a standard *nix environment these variables are not set, and there's
7966 no need to set them.
7967 On Windows it's recommended to set one of them.
7969 \labelwidthstring 00.00.0000
7974 \begin_inset LatexCommand \index{SDCC\_HOME}
7981 \begin_inset LatexCommand \ref{sub:Install-paths}
7987 \begin_inset Quotes sld
7991 \begin_inset Quotes srd
7996 \labelwidthstring 00.00.0000
8001 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8008 \begin_inset LatexCommand \ref{sub:Search-Paths}
8014 \begin_inset Quotes sld
8018 \begin_inset Quotes srd
8023 \labelwidthstring 00.00.0000
8028 \begin_inset LatexCommand \index{SDCC\_LIB}
8035 \begin_inset LatexCommand \ref{sub:Search-Paths}
8041 \begin_inset Quotes sld
8045 \begin_inset Quotes srd
8051 There are some more environment variables recognized by SDCC, but these
8052 are solely used for debugging purposes.
8053 They can change or disappear very quickly, and will never be documented.
8056 Storage Class Language Extensions
8059 MCS51/DS390 Storage Class
8060 \begin_inset LatexCommand \index{Storage class}
8067 In addition to the ANSI storage classes SDCC allows the following MCS51
8068 specific storage classes:
8069 \layout Subsubsection
8072 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8077 \begin_inset LatexCommand \index{near (storage class)}
8088 storage class for the Small Memory model (
8096 can be used synonymously).
8097 Variables declared with this storage class will be allocated in the directly
8098 addressable portion of the internal RAM of a 8051, e.g.:
8103 data unsigned char test_data;
8106 Writing 0x01 to this variable generates the assembly code:
8111 75*00 01\SpecialChar ~
8117 \layout Subsubsection
8120 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8125 \begin_inset LatexCommand \index{far (storage class)}
8132 Variables declared with this storage class will be placed in the external
8138 storage class for the Large Memory model, e.g.:
8143 xdata unsigned char test_xdata;
8146 Writing 0x01 to this variable generates the assembly code:
8151 90s00r00\SpecialChar ~
8180 \layout Subsubsection
8183 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8190 Variables declared with this storage class will be allocated into the indirectly
8191 addressable portion of the internal ram of a 8051, e.g.:
8196 idata unsigned char test_idata;
8199 Writing 0x01 to this variable generates the assembly code:
8228 Please note, the first 128 byte of idata physically access the same RAM
8230 The original 8051 had 128 byte idata memory, nowadays most devices have
8231 256 byte idata memory.
8233 \begin_inset LatexCommand \index{stack}
8237 is located in idata memory.
8238 \layout Subsubsection
8241 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8248 Paged xdata access is currently not as straightforward as using the other
8249 addressing modes of a 8051.
8250 The following example writes 0x01 to the address pointed to.
8251 Please note, pdata access physically accesses xdata memory.
8252 The high byte of the address is determined by port P2
8253 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8257 (or in case of some 8051 variants by a separate Special Function Register,
8259 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8268 pdata unsigned char *test_pdata_ptr;
8280 test_pdata_ptr = (pdata *)0xfe;
8286 *test_pdata_ptr = 1;
8291 Generates the assembly code:
8296 75*01 FE\SpecialChar ~
8300 _test_pdata_ptr,#0xFE
8332 Be extremely carefull if you use pdata together with the -
8343 \begin_inset LatexCommand \index{-\/-xstack}
8348 \layout Subsubsection
8351 \begin_inset LatexCommand \index{code}
8358 'Variables' declared with this storage class will be placed in the code
8364 code unsigned char test_code;
8367 Read access to this variable generates the assembly code:
8372 90s00r6F\SpecialChar ~
8375 mov dptr,#_test_code
8404 indexed arrays of characters in code memory can be accessed efficiently:
8409 code char test_array[] = {'c','h','e','a','p'};
8412 Read access to this array using an 8-bit unsigned index generates the assembly
8429 90s00r41\SpecialChar ~
8432 mov dptr,#_test_array
8447 \layout Subsubsection
8450 \begin_inset LatexCommand \index{bit}
8457 This is a data-type and a storage class specifier.
8458 When a variable is declared as a bit, it is allocated into the bit addressable
8459 memory of 8051, e.g.:
8467 Writing 1 to this variable generates the assembly code:
8483 The bit addressable memory consists of 128 bits which are located from 0x20
8484 to 0x2f in data memory.
8487 Apart from this 8051 specific storage class most architectures support ANSI-C
8489 \begin_inset LatexCommand \index{bitfields}
8499 Not really meant as examples, but nevertheless showing what bitfields are
8500 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8504 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8505 signed modifier are implemented as unsigned.
8506 \layout Subsubsection
8509 \begin_inset LatexCommand \index{sfr}
8514 \begin_inset LatexCommand \index{sbit}
8521 Like the bit keyword,
8525 signifies both a data-type and storage class, they are used to describe
8546 variables of a 8051, eg:
8552 \begin_inset LatexCommand \index{at}
8556 0x80 P0;\SpecialChar ~
8557 /* special function register P0 at location 0x80 */
8559 sbit at 0xd7 CY; /* CY (Carry Flag
8560 \begin_inset LatexCommand \index{Flags}
8565 \begin_inset LatexCommand \index{Carry flag}
8572 Special function registers which are located on an address dividable by
8573 8 are bit-addressable, an
8577 addresses a specific bit within these sfr.
8578 \layout Subsubsection
8581 \begin_inset LatexCommand \index{Pointer}
8585 to MCS51/DS390 specific memory spaces
8588 SDCC allows (via language extensions) pointers to explicitly point to any
8589 of the memory spaces
8590 \begin_inset LatexCommand \index{Memory model}
8595 In addition to the explicit pointers, the compiler uses (by default) generic
8596 pointers which can be used to point to any of the memory spaces.
8600 Pointer declaration examples:
8605 /* pointer physically in internal ram pointing to object in external ram
8608 xdata unsigned char * data p;
8612 /* pointer physically in external ram pointing to object in internal ram
8615 data unsigned char * xdata p;
8619 /* pointer physically in code rom pointing to data in xdata space */
8621 xdata unsigned char * code p;
8625 /* pointer physically in code space pointing to data in code space */
8627 code unsigned char * code p;
8631 /* the following is a generic pointer physically located in xdata space
8637 Well you get the idea.
8642 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8655 The highest order byte of the
8659 pointers contains the data space information.
8660 Assembler support routines are called whenever data is stored or retrieved
8666 These are useful for developing reusable library
8667 \begin_inset LatexCommand \index{Libraries}
8672 Explicitly specifying the pointer type will generate the most efficient
8674 \layout Subsubsection
8676 Notes on MCS51 memory
8677 \begin_inset LatexCommand \index{MCS51 memory}
8684 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8685 RAM memory which is structured as follows:
8689 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8692 - Bytes 20-2F - 16 bytes to hold 128 bit
8693 \begin_inset LatexCommand \index{bit}
8699 - Bytes 30-7F - 80 bytes for general purpose use.
8704 Additionally some members of the MCS51 family may have up to 128 bytes of
8705 additional, indirectly addressable, internal RAM memory (
8710 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8715 Furthermore, some chips may have some built in external memory (
8720 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8724 ) which should not be confused with the internal, directly addressable RAM
8730 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8735 Sometimes this built in
8739 memory has to be activated before using it (you can probably find this
8740 information on the datasheet of the microcontroller your are using, see
8742 \begin_inset LatexCommand \ref{sub:Startup-Code}
8750 Normally SDCC will only use the first bank
8751 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8755 of registers (register bank 0), but it is possible to specify that other
8756 banks of registers should be used in interrupt
8757 \begin_inset LatexCommand \index{interrupt}
8762 By default, the compiler will place the stack after the last byte of allocated
8763 memory for variables.
8764 For example, if the first 2 banks of registers are used, and only four
8769 variables, it will position the base of the internal stack at address 20
8771 This implies that as the stack
8772 \begin_inset LatexCommand \index{stack}
8776 grows, it will use up the remaining register banks, and the 16 bytes used
8777 by the 128 bit variables, and 80 bytes for general purpose use.
8778 If any bit variables are used, the data variables will be placed after
8779 the byte holding the last bit variable.
8780 For example, if register banks 0 and 1 are used, and there are 9 bit variables
8785 variables will be placed starting at address 0x22.
8797 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
8801 to specify the start address of the
8815 -iram-size to specify the size of the total internal RAM (
8827 By default the 8051 linker will place the stack after the last byte of data
8840 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
8844 allows you to specify the start of the stack, i.e.
8845 you could start it after any data in the general purpose area.
8846 If your microcontroller has additional indirectly addressable internal
8851 ) you can place the stack on it.
8852 You may also need to use -
8863 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
8867 to set the start address of the external RAM (
8882 \begin_inset LatexCommand \index{-\/-data-loc}
8886 to specify its size.
8887 Same goes for the code memory, using -
8898 \begin_inset LatexCommand \index{-\/-data-loc}
8913 \begin_inset LatexCommand \index{-\/-data-loc}
8918 If in doubt, don't specify any options and see if the resulting memory
8919 layout is appropriate, then you can adjust it.
8922 The linker generates two files with memory allocation information.
8923 The first, with extension .map
8924 \begin_inset LatexCommand \index{<file>.map}
8928 shows all the variables and segments.
8929 The second with extension .mem
8930 \begin_inset LatexCommand \index{<file>.mem}
8934 shows the final memory layout.
8935 The linker will complain either if memory segments overlap, there is not
8936 enough memory, or there is not enough space for stack.
8937 If you get any linking warnings and/or errors related to stack or segments
8938 allocation, take a look at either the .map or .mem files to find out what
8940 The .mem file may even suggest a solution to the problem.
8943 Z80/Z180 Storage Class
8944 \begin_inset LatexCommand \index{Storage class}
8949 \layout Subsubsection
8952 \begin_inset LatexCommand \index{sfr}
8956 (in/out to 8-bit addresses)
8960 \begin_inset LatexCommand \index{Z80}
8964 family has separate address spaces for memory and
8974 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
8978 is accessed with special instructions, e.g.:
8983 sfr at 0x78 IoPort;\SpecialChar ~
8985 /* define a var in I/O space at 78h called IoPort */
8989 Writing 0x01 to this variable generates the assembly code:
9009 \layout Subsubsection
9012 \begin_inset LatexCommand \index{sfr}
9016 (in/out to 16-bit addresses)
9023 is used to support 16 bit addresses in I/O memory e.g.:
9029 \begin_inset LatexCommand \index{at}
9036 Writing 0x01 to this variable generates the assembly code:
9041 01 23 01\SpecialChar ~
9061 \layout Subsubsection
9064 \begin_inset LatexCommand \index{sfr}
9068 (in0/out0 to 8 bit addresses on Z180
9069 \begin_inset LatexCommand \index{Z180}
9074 \begin_inset LatexCommand \index{HD64180}
9081 The compiler option -
9091 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9093 \begin_inset LatexCommand \index{\#pragma portmode}
9097 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9107 If you include the file z180.h this will be set automatically.
9111 \begin_inset LatexCommand \index{Storage class}
9116 \layout Subsubsection
9119 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9126 The data storage class declares a variable that resides in the first 256
9127 bytes of memory (the direct page).
9128 The HC08 is most efficient at accessing variables (especially pointers)
9130 \layout Subsubsection
9133 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9140 The xdata storage class declares a variable that can reside anywhere in
9142 This is the default if no storage class is specified.
9147 \begin_inset LatexCommand \index{Absolute addressing}
9154 Data items can be assigned an absolute address with the
9157 \begin_inset LatexCommand \index{at}
9163 keyword, in addition to a storage class, e.g.:
9169 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9174 \begin_inset LatexCommand \index{at}
9178 0x7ffe unsigned int chksum;
9181 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9182 of the external ram.
9187 reserve any space for variables declared in this way (they are implemented
9188 with an equate in the assembler).
9189 Thus it is left to the programmer to make sure there are no overlaps with
9190 other variables that are declared without the absolute address.
9191 The assembler listing file (.lst
9192 \begin_inset LatexCommand \index{<file>.lst}
9196 ) and the linker output files (.rst
9197 \begin_inset LatexCommand \index{<file>.rst}
9202 \begin_inset LatexCommand \index{<file>.map}
9206 ) are good places to look for such overlaps.
9207 Variables with an absolute address are
9212 \begin_inset LatexCommand \index{Variable initialization}
9219 In case of memory mapped I/O devices the keyword
9223 has to be used to tell the compiler that accesses might not be removed:
9229 \begin_inset LatexCommand \index{volatile}
9234 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9239 \begin_inset LatexCommand \index{at}
9243 0x8000 unsigned char PORTA_8255;
9246 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9251 \begin_inset LatexCommand \index{Aligned array}
9258 starts at a block (256 byte) boundary
9259 \begin_inset LatexCommand \index{block boundary}
9264 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9270 Absolute addresses can be specified for variables in all storage classes,
9277 \begin_inset LatexCommand \index{bit}
9282 \begin_inset LatexCommand \index{at}
9289 The above example will allocate the variable at offset 0x02 in the bit-addressab
9291 There is no real advantage to assigning absolute addresses to variables
9292 in this manner, unless you want strict control over all the variables allocated.
9293 One possible use would be to write hardware portable code.
9294 For example, if you have a routine that uses one or more of the microcontroller
9295 I/O pins, and such pins are different for two different hardwares, you
9296 can declare the I/O pins in your routine using:
9301 extern volatile bit MOSI;\SpecialChar ~
9305 /* master out, slave in */
9307 extern volatile bit MISO;\SpecialChar ~
9311 /* master in, slave out */
9313 extern volatile bit MCLK;\SpecialChar ~
9321 /* Input and Output of a byte on a 3-wire serial bus.
9326 If needed adapt polarity of clock, polarity of data and bit order
9331 unsigned char spi_io(unsigned char out_byte)
9355 MOSI = out_byte & 0x80;
9385 /* _asm nop _endasm; */\SpecialChar ~
9393 /* for slow peripherals */
9444 Then, someplace in the code for the first hardware you would use
9449 bit at 0x80 MOSI;\SpecialChar ~
9453 /* I/O port 0, bit 0 */
9455 bit at 0x81 MISO;\SpecialChar ~
9459 /* I/O port 0, bit 1 */
9461 bit at 0x82 MCLK;\SpecialChar ~
9465 /* I/O port 0, bit 2 */
9468 Similarly, for the second hardware you would use
9473 bit at 0x83 MOSI;\SpecialChar ~
9477 /* I/O port 0, bit 3 */
9479 bit at 0x91 MISO;\SpecialChar ~
9483 /* I/O port 1, bit 1 */
9486 \begin_inset LatexCommand \index{bit}
9490 at 0x92 MCLK;\SpecialChar ~
9494 /* I/O port 1, bit 2 */
9497 and you can use the same hardware dependent routine without changes, as
9498 for example in a library.
9499 This is somehow similar to sbit, but only one absolute address has to be
9500 specified in the whole project.
9504 \begin_inset LatexCommand \index{Parameters}
9509 \begin_inset LatexCommand \index{function parameter}
9514 \begin_inset LatexCommand \index{local variables}
9519 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9526 Automatic (local) variables and parameters to functions can either be placed
9527 on the stack or in data-space.
9528 The default action of the compiler is to place these variables in the internal
9529 RAM (for small model) or external RAM (for large model).
9530 This in fact makes them similar to
9533 \begin_inset LatexCommand \index{static}
9539 so by default functions are non-reentrant
9540 \begin_inset LatexCommand \index{reentrant}
9549 They can be placed on the stack
9550 \begin_inset LatexCommand \index{stack}
9567 \begin_inset LatexCommand \index{-\/-stack-auto}
9575 #pragma\SpecialChar ~
9579 \begin_inset LatexCommand \index{\#pragma stackauto}
9586 \begin_inset LatexCommand \index{reentrant}
9592 keyword in the function declaration, e.g.:
9597 unsigned char foo(char i) reentrant
9611 Since stack space on 8051 is limited, the
9629 option should be used sparingly.
9630 Note that the reentrant keyword just means that the parameters & local
9631 variables will be allocated to the stack, it
9635 mean that the function is register bank
9636 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9645 \begin_inset LatexCommand \index{local variables}
9649 can be assigned storage classes and absolute
9650 \begin_inset LatexCommand \index{Absolute addressing}
9667 xdata unsigned char i;
9680 \begin_inset LatexCommand \index{at}
9684 0x31 unsigned char j;
9696 In the above example the variable
9700 will be allocated in the external ram,
9704 in bit addressable space and
9723 or when a function is declared as
9727 this should only be done for static variables.
9731 \begin_inset LatexCommand \index{function parameter}
9735 however are not allowed any storage class
9736 \begin_inset LatexCommand \index{Storage class}
9740 , (storage classes for parameters will be ignored), their allocation is
9741 governed by the memory model in use, and the reentrancy options.
9745 \begin_inset LatexCommand \label{sub:Overlaying}
9750 \begin_inset LatexCommand \index{Overlaying}
9758 \begin_inset LatexCommand \index{reentrant}
9762 functions SDCC will try to reduce internal ram space usage by overlaying
9763 parameters and local variables of a function (if possible).
9764 Parameters and local variables
9765 \begin_inset LatexCommand \index{local variables}
9769 of a function will be allocated to an overlayable segment if the function
9772 no other function calls and the function is non-reentrant and the memory
9774 \begin_inset LatexCommand \index{Memory model}
9781 If an explicit storage class
9782 \begin_inset LatexCommand \index{Storage class}
9786 is specified for a local variable, it will NOT be overlayed.
9789 Note that the compiler (not the linkage editor) makes the decision for overlayin
9791 Functions that are called from an interrupt service routine should be preceded
9792 by a #pragma\SpecialChar ~
9794 \begin_inset LatexCommand \index{\#pragma nooverlay}
9798 if they are not reentrant.
9801 Also note that the compiler does not do any processing of inline assembler
9802 code, so the compiler might incorrectly assign local variables and parameters
9803 of a function into the overlay segment if the inline assembler code calls
9804 other c-functions that might use the overlay.
9805 In that case the #pragma\SpecialChar ~
9806 nooverlay should be used.
9809 Parameters and local variables of functions that contain 16 or 32 bit multiplica
9811 \begin_inset LatexCommand \index{Multiplication}
9816 \begin_inset LatexCommand \index{Division}
9820 will NOT be overlayed since these are implemented using external functions,
9829 \begin_inset LatexCommand \index{\#pragma nooverlay}
9835 void set_error(unsigned char errcd)
9851 void some_isr () interrupt
9852 \begin_inset LatexCommand \index{interrupt}
9882 In the above example the parameter
9890 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
9892 not present, this could cause unpredictable runtime behavior when called
9893 from an interrupt service routine.
9894 The #pragma\SpecialChar ~
9895 nooverlay ensures that the parameters and local variables for
9896 the function are NOT overlayed.
9899 Interrupt Service Routines
9900 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
9922 outines to be coded in C, with some extended keywords.
9927 void timer_isr (void) interrupt 1 using 1
9941 The optional number following the
9944 \begin_inset LatexCommand \index{interrupt}
9950 keyword is the interrupt number this routine will service.
9951 When present, the compiler will insert a call to this routine in the interrupt
9952 vector table for the interrupt number specified.
9953 If you have multiple source files in your project, interrupt service routines
9954 can be present in any of them, but a prototype of the isr MUST be present
9955 or included in the file that contains the function
9964 keyword can be used to tell the compiler to use the specified register
9965 bank (8051 specific) when generating code for this function.
9971 Interrupt service routines open the door for some very interesting bugs:
9973 If an interrupt service routine changes variables which are accessed by
9974 other functions these variables have to be declared
9979 \begin_inset LatexCommand \index{volatile}
9987 If the access to these variables is not
9990 \begin_inset LatexCommand \index{atomic}
9997 the processor needs more than one instruction for the access and could
9998 be interrupted while accessing the variable) the interrupt must be disabled
9999 during the access to avoid inconsistent data.
10000 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10001 and should be protected by disabling interrupts.
10002 You're not automatically on the safe side if you use 8 bit variables though.
10003 We need an example here: f.e.
10004 on the 8051 the harmless looking
10005 \begin_inset Quotes srd
10010 flags\SpecialChar ~
10015 \begin_inset Quotes sld
10024 \begin_inset Quotes srd
10029 flags\SpecialChar ~
10034 \begin_inset Quotes sld
10037 from within an interrupt routine might get lost if the interrupt occurs
10040 \begin_inset Quotes sld
10045 counter\SpecialChar ~
10050 \begin_inset Quotes srd
10053 is not atomic on the 8051 even if
10057 is located in data memory.
10058 Bugs like these are hard to reproduce and can cause a lot of trouble.
10062 The return address and the registers used in the interrupt service routine
10063 are saved on the stack
10064 \begin_inset LatexCommand \index{stack}
10068 so there must be sufficient stack space.
10069 If there isn't variables or registers (or even the return address itself)
10076 \begin_inset LatexCommand \index{stack overflow}
10080 is most likely to happen if the interrupt occurs during the
10081 \begin_inset Quotes sld
10085 \begin_inset Quotes srd
10088 subroutine when the stack is already in use for f.e.
10089 many return addresses.
10092 A special note here, int (16 bit) and long (32 bit) integer division
10093 \begin_inset LatexCommand \index{Division}
10098 \begin_inset LatexCommand \index{Multiplication}
10103 \begin_inset LatexCommand \index{Modulus}
10108 \begin_inset LatexCommand \index{Floating point support}
10112 operations are implemented using external support routines developed in
10114 If an interrupt service routine needs to do any of these operations then
10115 the support routines (as mentioned in a following section) will have to
10116 be recompiled using the
10129 \begin_inset LatexCommand \index{-\/-stack-auto}
10135 option and the source file will need to be compiled using the
10150 \begin_inset LatexCommand \index{-\/-int-long-reent}
10157 Calling other functions from an interrupt service routine is not recommended,
10158 avoid it if possible.
10159 Note that when some function is called from an interrupt service routine
10160 it should be preceded by a #pragma\SpecialChar ~
10162 \begin_inset LatexCommand \index{\#pragma nooverlay}
10166 if it is not reentrant.
10167 Furthermore nonreentrant functions should not be called from the main program
10168 while the interrupt service routine might be active.
10174 \begin_inset LatexCommand \ref{sub:Overlaying}
10179 about Overlaying and section
10180 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10185 about Functions using private register banks.
10188 MCS51/DS390 Interrupt Service Routines
10191 Interrupt numbers and the corresponding address & descriptions for the Standard
10192 8051/8052 are listed below.
10193 SDCC will automatically adjust the interrupt vector table to the maximum
10194 interrupt number specified.
10200 \begin_inset Tabular
10201 <lyxtabular version="3" rows="7" columns="3">
10203 <column alignment="center" valignment="top" leftline="true" width="0in">
10204 <column alignment="center" valignment="top" leftline="true" width="0in">
10205 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10206 <row topline="true" bottomline="true">
10207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10223 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10232 <row topline="true">
10233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10258 <row topline="true">
10259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10275 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10284 <row topline="true">
10285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10310 <row topline="true">
10311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10336 <row topline="true">
10337 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10362 <row topline="true" bottomline="true">
10363 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10379 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10397 If the interrupt service routine is defined without
10400 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10406 a register bank or with register bank 0 (
10410 0), the compiler will save the registers used by itself on the stack upon
10411 entry and restore them at exit, however if such an interrupt service routine
10412 calls another function then the entire register bank will be saved on the
10414 This scheme may be advantageous for small interrupt service routines which
10415 have low register usage.
10418 If the interrupt service routine is defined to be using a specific register
10423 & psw are saved and restored, if such an interrupt service routine calls
10424 another function (using another register bank) then the entire register
10425 bank of the called function will be saved on the stack.
10426 This scheme is recommended for larger interrupt service routines.
10429 HC08 Interrupt Service Routines
10432 Since the number of interrupts available is chip specific and the interrupt
10433 vector table always ends at the last byte of memory, the interrupt numbers
10434 corresponds to the interrupt vectors in reverse order of address.
10435 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10436 2 will use the interrupt vector at 0xfffa, and so on.
10437 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10438 this way; instead see section
10439 \begin_inset LatexCommand \ref{sub:Startup-Code}
10443 for details on customizing startup.
10446 Z80 Interrupt Service Routines
10449 The Z80 uses several different methods for determining the correct interrupt
10450 vector depending on the hardware implementation.
10451 Therefore, SDCC ignores the optional interrupt number and does not attempt
10452 to generate an interrupt vector table.
10455 By default, SDCC generates code for a maskable interrupt, which uses an
10456 RETI instruction to return from the interrupt.
10457 To write an interrupt handler for the non-maskable interrupt, which needs
10458 an RETN instruction instead, add the
10467 void nmi_isr (void) critical interrupt
10481 Enabling and Disabling Interrupts
10484 Critical Functions and Critical Statements
10487 A special keyword may be associated with a block or a function declaring
10493 SDCC will generate code to disable all interrupts
10494 \begin_inset LatexCommand \index{interrupt}
10498 upon entry to a critical function and restore the interrupt enable to the
10499 previous state before returning.
10500 Nesting critical functions will need one additional byte on the stack
10501 \begin_inset LatexCommand \index{stack}
10510 int foo () critical
10511 \begin_inset LatexCommand \index{critical}
10536 The critical attribute maybe used with other attributes like
10546 may also be used to disable interrupts more locally:
10554 More than one statement could have been included in the block.
10557 Enabling and Disabling Interrupts directly
10561 \begin_inset LatexCommand \index{interrupt}
10565 can also be disabled and enabled directly (8051):
10570 EA = 0;\SpecialChar ~
10633 EA = 1;\SpecialChar ~
10700 On other architectures which have seperate opcodes for enabling and disabling
10701 interrupts you might want to make use of defines with inline assembly
10702 \begin_inset LatexCommand \index{Assembler routines}
10712 \begin_inset LatexCommand \index{\_asm}
10721 \begin_inset LatexCommand \index{\_endasm}
10730 #define SEI _asm\SpecialChar ~
10742 Note: it is sometimes sufficient to disable only a specific interrupt source
10744 a timer or serial interrupt by manipulating an
10747 \begin_inset LatexCommand \index{interrupt mask}
10757 Usually the time during which interrupts are disabled should be kept as
10759 This minimizes both
10764 \begin_inset LatexCommand \index{interrupt latency}
10768 (the time between the occurrence of the interrupt and the execution of
10769 the first code in the interrupt routine) and
10774 \begin_inset LatexCommand \index{interrupt jitter}
10778 (the difference between the shortest and the longest interrupt latency).
10779 These really are something different, f.e.
10780 a serial interrupt has to be served before its buffer overruns so it cares
10781 for the maximum interrupt latency, whereas it does not care about jitter.
10782 On a loudspeaker driven via a digital to analog converter which is fed
10783 by an interrupt a latency of a few milliseconds might be tolerable, whereas
10784 a much smaller jitter will be very audible.
10787 You can reenable interrupts within an interrupt routine and on some architecture
10788 s you can make use of two (or more) levels of
10790 interrupt priorities
10793 \begin_inset LatexCommand \index{interrupt priority}
10798 On some architectures which don't support interrupt priorities these can
10799 be implemented by manipulating the interrupt mask and reenabling interrupts
10800 within the interrupt routine.
10801 Check there is sufficient space on the stack
10802 \begin_inset LatexCommand \index{stack}
10806 and don't add complexity unless you have to.
10811 \begin_inset LatexCommand \index{semaphore}
10815 locking (mcs51/ds390)
10818 Some architectures (mcs51/ds390) have an atomic
10819 \begin_inset LatexCommand \index{atomic}
10832 These type of instructions are typically used in preemptive multitasking
10833 systems, where a routine f.e.
10834 claims the use of a data structure ('acquires a lock
10835 \begin_inset LatexCommand \index{lock}
10839 on it'), makes some modifications and then releases the lock when the data
10840 structure is consistent again.
10841 The instruction may also be used if interrupt and non-interrupt code have
10842 to compete for a resource.
10843 With the atomic bit test and clear instruction interrupts
10844 \begin_inset LatexCommand \index{interrupt}
10848 don't have to be disabled for the locking operation.
10852 SDCC generates this instruction if the source follows this pattern:
10857 volatile bit resource_is_free;
10861 if (resource_is_free)
10871 resource_is_free=0;
10884 resource_is_free=1;
10891 Note, mcs51 and ds390 support only an atomic
10892 \begin_inset LatexCommand \index{atomic}
10900 instruction (as opposed to atomic bit test and
10905 Functions using private register banks
10906 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
10913 Some architectures have support for quickly changing register sets.
10914 SDCC supports this feature with the
10917 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10923 attribute (which tells the compiler to use a register bank
10924 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10928 other than the default bank zero).
10929 It should only be applied to
10932 \begin_inset LatexCommand \index{interrupt}
10938 functions (see footnote below).
10939 This will in most circumstances make the generated ISR code more efficient
10940 since it will not have to save registers on the stack.
10947 attribute will have no effect on the generated code for a
10951 function (but may occasionally be useful anyway
10957 possible exception: if a function is called ONLY from 'interrupt' functions
10958 using a particular bank, it can be declared with the same 'using' attribute
10959 as the calling 'interrupt' functions.
10960 For instance, if you have several ISRs using bank one, and all of them
10961 call memcpy(), it might make sense to create a specialized version of memcpy()
10962 'using 1', since this would prevent the ISR from having to save bank zero
10963 to the stack on entry and switch to bank zero before calling the function
10970 (pending: I don't think this has been done yet)
10977 function using a non-zero bank will assume that it can trash that register
10978 bank, and will not save it.
10979 Since high-priority interrupts
10980 \begin_inset LatexCommand \index{interrupt priority}
10984 can interrupt low-priority ones on the 8051 and friends, this means that
10985 if a high-priority ISR
10989 a particular bank occurs while processing a low-priority ISR
10993 the same bank, terrible and bad things can happen.
10994 To prevent this, no single register bank should be
10998 by both a high priority and a low priority ISR.
10999 This is probably most easily done by having all high priority ISRs use
11000 one bank and all low priority ISRs use another.
11001 If you have an ISR which can change priority at runtime, you're on your
11002 own: I suggest using the default bank zero and taking the small performance
11006 It is most efficient if your ISR calls no other functions.
11007 If your ISR must call other functions, it is most efficient if those functions
11008 use the same bank as the ISR (see note 1 below); the next best is if the
11009 called functions use bank zero.
11010 It is very inefficient to call a function using a different, non-zero bank
11016 \begin_inset LatexCommand \label{sub:Startup-Code}
11021 \begin_inset LatexCommand \index{Startup code}
11028 MCS51/DS390 Startup Code
11031 The compiler inserts a call to the C routine
11033 _sdcc_external_startup()
11034 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11043 at the start of the CODE area.
11044 This routine is in the runtime library
11045 \begin_inset LatexCommand \index{Runtime library}
11050 By default this routine returns 0, if this routine returns a non-zero value,
11051 the static & global variable initialization will be skipped and the function
11052 main will be invoked.
11053 Otherwise static & global variables will be initialized before the function
11057 _sdcc_external_startup()
11059 routine to your program to override the default if you need to setup hardware
11060 or perform some other critical operation prior to static & global variable
11062 On some mcs51 variants xdata has to be explicitly enabled before it can
11063 be accessed, this is the place to do it.
11064 The startup code clears the complete 256 byte of idata memory, this might
11065 cause problems for 128 byte devices (endless loop reported for Chipcon
11069 See also the compiler option
11088 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11093 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11098 about MCS51-variants.
11104 The HC08 startup code follows the same scheme as the MCS51 startup code.
11110 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11111 from sdcc/device/lib/z80/crt0.s.
11112 If you need a different startup code you can use the compiler option
11133 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11137 and provide your own crt0.o.
11141 Inline Assembler Code
11142 \begin_inset LatexCommand \index{Assembler routines}
11149 A Step by Step Introduction
11150 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11157 Starting from a small snippet of c-code this example shows for the MCS51
11158 how to use inline assembly, access variables, a function parameter and
11159 an array in xdata memory.
11160 The example uses an MCS51 here but is easily adapted for other architectures.
11161 This is a buffer routine which should be optimized:
11168 \begin_inset LatexCommand \index{far (storage class)}
11173 \begin_inset LatexCommand \index{at}
11178 \begin_inset LatexCommand \index{Aligned array}
11184 unsigned char head,tail;
11188 void to_buffer( unsigned char c )
11196 if( head != tail-1 )
11206 buf[ head++ ] = c;\SpecialChar ~
11210 /* access to a 256 byte aligned array */
11215 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11216 then a corresponding buffer.asm file is generated.
11217 We define a new function
11221 in file buffer.c in which we cut and paste the generated code, removing
11222 unwanted comments and some ':'.
11224 \begin_inset Quotes sld
11228 \begin_inset Quotes srd
11232 \begin_inset Quotes sld
11236 \begin_inset Quotes srd
11239 to the beginning and the end of the function body:
11245 /* With a cut and paste from the .asm file, we have something to start with.
11250 The function is not yet OK! (registers aren't saved) */
11252 void to_buffer_asm( unsigned char c )
11261 \begin_inset LatexCommand \index{\_asm}
11275 ;buffer.c if( head != tail-1 )
11323 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11324 \begin_inset LatexCommand \index{Aligned array}
11393 The new file buffer.c should compile with only one warning about the unreferenced
11394 function argument 'c'.
11395 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11396 (1) and finally have:
11402 unsigned char far at 0x7f00 buf[0x100];
11404 unsigned char head,tail;
11406 #define USE_ASSEMBLY (1)
11414 void to_buffer( unsigned char c )
11422 if( head != tail-1 )
11442 void to_buffer( unsigned char c )
11450 c; // to avoid warning: unreferenced function argument
11457 \begin_inset LatexCommand \index{\_asm}
11471 ; save used registers here.
11482 ; If we were still using r2,r3 we would have to push them here.
11485 ; if( head != tail-1 )
11528 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11552 ; buf[ head++ ] = c;
11563 a,dpl \SpecialChar ~
11570 ; dpl holds lower byte of function argument
11581 dpl,_head \SpecialChar ~
11584 ; buf is 0x100 byte aligned so head can be used directly
11626 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11638 ; restore used registers here
11651 The inline assembler code can contain any valid code understood by the assembler
11652 , this includes any assembler directives and comment lines
11658 The assembler does not like some characters like ':' or ''' in comments.
11659 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11663 The compiler does not do any validation of the code within the
11666 \begin_inset LatexCommand \index{\_asm}
11674 Specifically it will not know which registers are used and thus register
11676 \begin_inset LatexCommand \index{push/pop}
11680 has to be done manually.
11684 It is recommended that each assembly instruction (including labels) be placed
11685 in a separate line (as the example shows).
11699 \begin_inset LatexCommand \index{-\/-peep-asm}
11705 command line option is used, the inline assembler code will be passed through
11706 the peephole optimizer
11707 \begin_inset LatexCommand \index{Peephole optimizer}
11712 There are only a few (if any) cases where this option makes sense, it might
11713 cause some unexpected changes in the inline assembler code.
11714 Please go through the peephole optimizer rules defined in file
11718 before using this option.
11722 \begin_inset LatexCommand \label{sub:Naked-Functions}
11727 \begin_inset LatexCommand \index{Naked functions}
11734 A special keyword may be associated with a function declaring it as
11737 \begin_inset LatexCommand \index{\_naked}
11748 function modifier attribute prevents the compiler from generating prologue
11749 \begin_inset LatexCommand \index{function prologue}
11754 \begin_inset LatexCommand \index{function epilogue}
11758 code for that function.
11759 This means that the user is entirely responsible for such things as saving
11760 any registers that may need to be preserved, selecting the proper register
11761 bank, generating the
11765 instruction at the end, etc.
11766 Practically, this means that the contents of the function must be written
11767 in inline assembler.
11768 This is particularly useful for interrupt functions, which can have a large
11769 (and often unnecessary) prologue/epilogue.
11770 For example, compare the code generated by these two functions:
11776 \begin_inset LatexCommand \index{volatile}
11780 data unsigned char counter;
11784 void simpleInterrupt(void) interrupt
11785 \begin_inset LatexCommand \index{interrupt}
11803 void nakedInterrupt(void) interrupt 2 _naked
11812 \begin_inset LatexCommand \index{\_asm}
11829 _counter ; does not change flags, no need to save psw
11841 ; MUST explicitly include ret or reti in _naked function.
11848 \begin_inset LatexCommand \index{\_endasm}
11857 For an 8051 target, the generated simpleInterrupt looks like:
11998 whereas nakedInterrupt looks like:
12013 _counter ; does not change flags, no need to save psw
12031 ; MUST explicitly include ret or reti in _naked function
12034 The related directive #pragma exclude
12035 \begin_inset LatexCommand \index{\#pragma exclude}
12039 allows a more fine grained control over pushing & popping
12040 \begin_inset LatexCommand \index{push/pop}
12047 While there is nothing preventing you from writing C code inside a
12051 function, there are many ways to shoot yourself in the foot doing this,
12052 and it is recommended that you stick to inline assembler.
12055 Use of Labels within Inline Assembler
12058 SDCC allows the use of in-line assembler with a few restrictions regarding
12060 In older versions of the compiler all labels defined within inline assembler
12069 where nnnn is a number less than 100 (which implies a limit of utmost 100
12070 inline assembler labels
12084 \begin_inset LatexCommand \index{\_asm}
12114 \begin_inset LatexCommand \index{\_endasm}
12121 Inline assembler code cannot reference any C-Labels, however it can reference
12123 \begin_inset LatexCommand \index{Labels}
12127 defined by the inline assembler, e.g.:
12152 ; some assembler code
12172 /* some more c code */
12174 clabel:\SpecialChar ~
12176 /* inline assembler cannot reference this label */
12188 $0003: ;label (can be referenced by inline assembler only)
12200 /* some more c code */
12205 In other words inline assembly code can access labels defined in inline
12206 assembly within the scope of the function.
12207 The same goes the other way, i.e.
12208 labels defines in inline assembly can not be accessed by C statements.
12211 Interfacing with Assembler Code
12212 \begin_inset LatexCommand \index{Assembler routines}
12219 Global Registers used for Parameter Passing
12220 \begin_inset LatexCommand \index{Parameter passing}
12227 The compiler always uses the global registers
12230 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12235 \begin_inset LatexCommand \index{DPTR}
12240 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12249 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12255 to pass the first parameter to a routine.
12256 The second parameter onwards is either allocated on the stack (for reentrant
12267 -stack-auto is used) or in data / xdata memory (depending on the memory
12272 Assembler Routine (non-reentrant)
12275 In the following example
12276 \begin_inset LatexCommand \index{reentrant}
12281 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12285 the function c_func calls an assembler routine asm_func, which takes two
12287 \begin_inset LatexCommand \index{function parameter}
12296 extern int asm_func(unsigned char, unsigned char);
12300 int c_func (unsigned char i, unsigned char j)
12308 return asm_func(i,j);
12322 return c_func(10,9);
12327 The corresponding assembler function is:
12332 .globl _asm_func_PARM_2
12433 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12450 Note here that the return values
12451 \begin_inset LatexCommand \index{return value}
12455 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12457 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12458 b' & 'acc' for four byte values.
12461 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12462 the parameter number starting from 1, and counting from the left.
12463 The first parameter is passed in
12464 \begin_inset Quotes eld
12468 \begin_inset Quotes erd
12471 for a one byte parameter,
12472 \begin_inset Quotes eld
12476 \begin_inset Quotes erd
12480 \begin_inset Quotes eld
12484 \begin_inset Quotes erd
12487 for three bytes and
12488 \begin_inset Quotes eld
12492 \begin_inset Quotes erd
12495 for a four bytes parameter.
12496 The variable name for the second parameter will be _<function_name>_PARM_2.
12500 Assemble the assembler routine with the following command:
12507 asx8051 -losg asmfunc.asm
12514 Then compile and link the assembler routine to the C source file with the
12522 sdcc cfunc.c asmfunc.rel
12525 Assembler Routine (reentrant)
12529 \begin_inset LatexCommand \index{reentrant}
12534 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12538 the second parameter
12539 \begin_inset LatexCommand \index{function parameter}
12543 onwards will be passed on the stack, the parameters are pushed from right
12545 after the call the leftmost parameter will be on the top of the stack.
12546 Here is an example:
12551 extern int asm_func(unsigned char, unsigned char);
12555 int c_func (unsigned char i, unsigned char j) reentrant
12563 return asm_func(i,j);
12577 return c_func(10,9);
12582 The corresponding assembler routine is:
12682 The compiling and linking procedure remains the same, however note the extra
12683 entry & exit linkage required for the assembler code, _bp is the stack
12684 frame pointer and is used to compute the offset into the stack for parameters
12685 and local variables.
12689 \begin_inset LatexCommand \index{int (16 bit)}
12694 \begin_inset LatexCommand \index{long (32 bit)}
12701 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12702 multiplication and modulus operations are implemented by support routines.
12703 These support routines are all developed in ANSI-C to facilitate porting
12704 to other MCUs, although some model specific assembler optimizations are
12706 The following files contain the described routines, all of them can be
12707 found in <installdir>/share/sdcc/lib.
12713 \begin_inset Tabular
12714 <lyxtabular version="3" rows="11" columns="2">
12716 <column alignment="center" valignment="top" leftline="true" width="0">
12717 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12718 <row topline="true" bottomline="true">
12719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12740 <row topline="true">
12741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12749 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12754 16 bit multiplication
12758 <row topline="true">
12759 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12772 signed 16 bit division (calls _divuint)
12776 <row topline="true">
12777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12785 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12790 unsigned 16 bit division
12794 <row topline="true">
12795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12808 signed 16 bit modulus (calls _moduint)
12812 <row topline="true">
12813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12826 unsigned 16 bit modulus
12830 <row topline="true">
12831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12844 32 bit multiplication
12848 <row topline="true">
12849 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12857 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12862 signed 32 division (calls _divulong)
12866 <row topline="true">
12867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12880 unsigned 32 division
12884 <row topline="true">
12885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12898 signed 32 bit modulus (calls _modulong)
12902 <row topline="true" bottomline="true">
12903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12911 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12916 unsigned 32 bit modulus
12929 Since they are compiled as
12934 \begin_inset LatexCommand \index{reentrant}
12939 \begin_inset LatexCommand \index{interrupt}
12943 service routines should not do any of the above operations.
12944 If this is unavoidable then the above routines will need to be compiled
12958 \begin_inset LatexCommand \index{-\/-stack-auto}
12964 option, after which the source program will have to be compiled with
12977 \begin_inset LatexCommand \index{-\/-int-long-reent}
12984 Notice that you don't have to call these routines directly.
12985 The compiler will use them automatically every time an integer operation
12989 Floating Point Support
12990 \begin_inset LatexCommand \index{Floating point support}
12997 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
12998 floating point support routines are derived from gcc's floatlib.c and consist
12999 of the following routines:
13007 \begin_inset Tabular
13008 <lyxtabular version="3" rows="17" columns="2">
13010 <column alignment="center" valignment="top" leftline="true" width="0">
13011 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13012 <row topline="true" bottomline="true">
13013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13039 <row topline="true">
13040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13057 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13071 add floating point numbers
13075 <row topline="true">
13076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13093 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13107 subtract floating point numbers
13111 <row topline="true">
13112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13143 divide floating point numbers
13147 <row topline="true">
13148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13179 multiply floating point numbers
13183 <row topline="true">
13184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13215 convert floating point to unsigned char
13219 <row topline="true">
13220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13251 convert floating point to signed char
13255 <row topline="true">
13256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13287 convert floating point to unsigned int
13291 <row topline="true">
13292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13323 convert floating point to signed int
13327 <row topline="true">
13328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13368 convert floating point to unsigned long
13372 <row topline="true">
13373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13390 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13404 convert floating point to signed long
13408 <row topline="true">
13409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13440 convert unsigned char to floating point
13444 <row topline="true">
13445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13462 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13476 convert char to floating point number
13480 <row topline="true">
13481 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13512 convert unsigned int to floating point
13516 <row topline="true">
13517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13534 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13548 convert int to floating point numbers
13552 <row topline="true">
13553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13584 convert unsigned long to floating point number
13588 <row topline="true" bottomline="true">
13589 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13606 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13620 convert long to floating point number
13633 These support routines are developed in ANSI-C so there is room for space
13634 and speed improvement.
13635 Note if all these routines are used simultaneously the data space might
13637 For serious floating point usage it is recommended that the large model
13639 Also notice that you don't have to call this routines directly.
13640 The compiler will use them automatically every time a floating point operation
13645 \begin_inset LatexCommand \index{Libraries}
13654 <pending: this is messy and incomplete - a little more information is in
13655 sdcc/doc/libdoc.txt
13660 Compiler support routines (_gptrget, _mulint etc.)
13663 Stdclib functions (puts, printf, strcat etc.)
13664 \layout Subsubsection
13670 \begin_inset LatexCommand \index{<stdio.h>}
13674 As usual on embedded systems you have to provide your own
13677 \begin_inset LatexCommand \index{getchar()}
13686 \begin_inset LatexCommand \index{putchar()}
13693 SDCC does not know whether the system connects to a serial line with or
13694 without handshake, LCD, keyboard or other device.
13695 You'll find examples for serial routines f.e.
13696 in sdcc/device/lib.
13702 \begin_inset LatexCommand \index{printf()}
13712 does not support float (except on ds390).
13713 To enable this recompile it with the option
13726 \begin_inset LatexCommand \index{USE\_FLOATS}
13732 on the command line.
13746 \begin_inset LatexCommand \index{-\/-model-large}
13752 for the mcs51 port, since this uses a lot of memory.
13755 If you're short on memory you might want to use
13768 For the mcs51 there is an assembly version
13772 which should fit the requirements of many embedded systems (by unsetting
13773 #defines it can be customized to
13777 support long variables and field widths).
13780 Math functions (sin, pow, sqrt etc.)
13787 \begin_inset LatexCommand \index{Libraries}
13791 included in SDCC should have a license at least as liberal as the GNU Lesser
13792 General Public License
13793 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
13804 license statements for the libraries are missing.
13805 sdcc/device/lib/ser_ir.c
13809 come with a GPL (as opposed to LGPL) License - this will not be liberal
13810 enough for many embedded programmers.
13813 If you have ported some library or want to share experience about some code
13815 falls into any of these categories Busses (I
13816 \begin_inset Formula $^{\textrm{2}}$
13819 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
13820 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
13821 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
13822 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
13827 would certainly like to hear about it.
13828 Programmers coding for embedded systems are not especially famous for being
13829 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
13830 e these references are very valuable.
13831 Let's help to create a climate where information is shared.
13837 MCS51 Memory Models
13838 \begin_inset LatexCommand \index{Memory model}
13843 \begin_inset LatexCommand \index{MCS51 memory model}
13848 \layout Subsubsection
13853 SDCC allows two memory models for MCS51 code,
13862 Modules compiled with different memory models should
13866 be combined together or the results would be unpredictable.
13867 The library routines supplied with the compiler are compiled as both small
13869 The compiled library modules are contained in separate directories as small
13870 and large so that you can link to either set.
13874 When the large model is used all variables declared without a storage class
13875 will be allocated into the external ram, this includes all parameters and
13876 local variables (for non-reentrant
13877 \begin_inset LatexCommand \index{reentrant}
13882 When the small model is used variables without storage class are allocated
13883 in the internal ram.
13886 Judicious usage of the processor specific storage classes
13887 \begin_inset LatexCommand \index{Storage class}
13891 and the 'reentrant' function type will yield much more efficient code,
13892 than using the large model.
13893 Several optimizations are disabled when the program is compiled using the
13894 large model, it is therefore recommended that the small model be used unless
13895 absolutely required.
13896 \layout Subsubsection
13899 \begin_inset LatexCommand \label{sub:External-Stack}
13904 \begin_inset LatexCommand \index{stack}
13909 \begin_inset LatexCommand \index{External stack (mcs51)}
13920 : this option wasn't maintained for a long time and is quite buggy.
13921 Small programs might work.
13922 You've been warned!
13925 The external stack (-
13936 \begin_inset LatexCommand \index{-\/-xstack}
13940 ) is located in pdata
13941 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
13945 memory (usually at the start of the external ram segment) and is 256 bytes
13957 -xstack option is used to compile the program, the parameters and local
13959 \begin_inset LatexCommand \index{local variables}
13963 of all reentrant functions are allocated in this area.
13964 This option is provided for programs with large stack space requirements.
13965 When used with the -
13976 \begin_inset LatexCommand \index{-\/-stack-auto}
13980 option, all parameters and local variables are allocated on the external
13981 stack (note: support libraries will need to be recompiled with the same
13985 The compiler outputs the higher order address byte of the external ram segment
13987 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
13992 \begin_inset LatexCommand \ref{sub:MCS51-variants}
13996 ), therefore when using the External Stack option, this port
14000 be used by the application program.
14004 \begin_inset LatexCommand \index{Memory model}
14009 \begin_inset LatexCommand \index{DS390 memory model}
14016 The only model supported is Flat 24
14017 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14022 This generates code for the 24 bit contiguous addressing mode of the Dallas
14024 In this mode, up to four meg of external RAM or code space can be directly
14026 See the data sheets at www.dalsemi.com for further information on this part.
14030 Note that the compiler does not generate any code to place the processor
14031 into 24 bitmode (although
14035 in the ds390 libraries will do that for you).
14041 \begin_inset LatexCommand \index{Tinibios (DS390)}
14045 , the boot loader or similar code must ensure that the processor is in 24
14046 bit contiguous addressing mode before calling the SDCC startup code.
14064 option, variables will by default be placed into the XDATA segment.
14069 Segments may be placed anywhere in the 4 meg address space using the usual
14081 Note that if any segments are located above 64K, the -r flag must be passed
14082 to the linker to generate the proper segment relocations, and the Intel
14083 HEX output format must be used.
14084 The -r flag can be passed to the linker by using the option
14088 on the SDCC command line.
14089 However, currently the linker can not handle code segments > 64k.
14093 \begin_inset LatexCommand \index{Pragmas}
14100 SDCC supports the following #pragma directives:
14104 \begin_inset LatexCommand \index{\#pragma save}
14108 - this will save all current options to the save/restore stack.
14109 See #pragma\SpecialChar ~
14114 \begin_inset LatexCommand \index{\#pragma restore}
14118 - will restore saved options from the last save.
14119 saves & restores can be nested.
14120 SDCC uses a save/restore stack: save pushes current options to the stack,
14121 restore pulls current options from the stack.
14122 See #pragma\SpecialChar ~
14129 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14134 \begin_inset LatexCommand \index{function prologue}
14138 function1[,function2[,function3...]] - The compiler by default uses a caller
14139 saves convention for register saving across function calls, however this
14140 can cause unnecessary register pushing & popping
14141 \begin_inset LatexCommand \index{push/pop}
14145 when calling small functions from larger functions.
14146 This option can be used to switch off the register saving convention for
14147 the function names specified.
14148 The compiler will not save registers when calling these functions, extra
14149 code need to be manually inserted at the entry & exit for these functions
14150 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14151 reduce code & improve run time performance of the generated code.
14152 In the future the compiler (with inter procedural analysis) may be able
14153 to determine the appropriate scheme to use for each function call.
14164 -callee-saves command line option is used, the function names specified
14165 in #pragma\SpecialChar ~
14167 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14171 is appended to the list of functions specified in the command line.
14175 \begin_inset LatexCommand \index{\#pragma exclude}
14179 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14180 of pairs of push/pop
14181 \begin_inset LatexCommand \index{push/pop}
14190 \begin_inset LatexCommand \index{interrupt}
14203 The directive should be placed immediately before the ISR function definition
14204 and it affects ALL ISR functions following it.
14205 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14206 exclude\SpecialChar ~
14208 \begin_inset LatexCommand \index{\#pragma exclude}
14213 See also the related keyword _naked
14214 \begin_inset LatexCommand \index{\_naked}
14222 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14226 - the compiler will not warn you anymore for obvious mistakes, you'r on
14230 disable_warning <nnnn>
14231 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14235 - the compiler will not warn you anymore about warning number <nnnn>.
14239 \begin_inset LatexCommand \index{\#pragma nogcse}
14243 - will stop global common subexpression elimination.
14247 \begin_inset LatexCommand \index{\#pragma noinduction}
14251 - will stop loop induction optimizations.
14255 \begin_inset LatexCommand \index{\#pragma noinvariant}
14259 - will not do loop invariant optimizations.
14260 For more details see Loop Invariants in section
14261 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14269 \begin_inset LatexCommand \index{\#pragma noiv}
14273 - Do not generate interrupt
14274 \begin_inset LatexCommand \index{interrupt}
14278 vector table entries for all ISR functions defined after the pragma.
14279 This is useful in cases where the interrupt vector table must be defined
14280 manually, or when there is a secondary, manually defined interrupt vector
14282 for the autovector feature of the Cypress EZ-USB FX2).
14283 More elegantly this can be achieved by obmitting the optional interrupt
14284 number after the interrupt keyword, see section
14285 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14294 \begin_inset LatexCommand \index{\#pragma nojtbound}
14298 - will not generate code for boundary value checking, when switch statements
14299 are turned into jump-tables (dangerous).
14300 For more details see section
14301 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14309 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14313 - Will not do loop reversal optimization
14317 \begin_inset LatexCommand \index{\#pragma nooverlay}
14321 - the compiler will not overlay the parameters and local variables of a
14326 \begin_inset LatexCommand \index{\#pragma stackauto}
14341 \begin_inset LatexCommand \index{-\/-stack-auto}
14346 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
14350 Parameters and Local Variables.
14354 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
14358 - The compiler will optimize code generation towards fast code, possibly
14359 at the expense of code size.
14363 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
14367 - The compiler will optimize code generation towards compact code, possibly
14368 at the expense of code speed.
14372 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
14376 - The compiler will attempt to generate code that is both compact and fast,
14377 as long as meeting one goal is not a detriment to the other (this is the
14382 SDCPP supports the following #pragma directives:
14386 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
14390 (+ | -) - switch _asm _endasm block preprocessing on / off.
14394 The pragma's are intended to be used to turn-on or off certain optimizations
14395 which might cause the compiler to generate extra stack / data space to
14396 store compiler generated temporary variables.
14397 This usually happens in large functions.
14398 Pragma directives should be used as shown in the following example, they
14399 are used to control options & optimizations for a given function; pragmas
14400 should be placed before and/or after a function, placing pragma's inside
14401 a function body could have unpredictable results.
14407 \begin_inset LatexCommand \index{\#pragma save}
14418 /* save the current settings */
14421 \begin_inset LatexCommand \index{\#pragma nogcse}
14430 /* turnoff global subexpression elimination */
14432 #pragma noinduction
14433 \begin_inset LatexCommand \index{\#pragma noinduction}
14437 /* turn off induction optimizations */
14460 \begin_inset LatexCommand \index{\#pragma restore}
14464 /* turn the optimizations back on */
14467 The compiler will generate a warning message when extra space is allocated.
14468 It is strongly recommended that the save and restore pragma's be used when
14469 changing options for a function.
14472 Defines Created by the Compiler
14475 The compiler creates the following #defines
14476 \begin_inset LatexCommand \index{\#defines}
14481 \begin_inset LatexCommand \index{Defines created by the compiler}
14491 \begin_inset Tabular
14492 <lyxtabular version="3" rows="10" columns="2">
14494 <column alignment="center" valignment="top" leftline="true" width="0">
14495 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14496 <row topline="true" bottomline="true">
14497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14507 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14518 <row topline="true">
14519 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14525 \begin_inset LatexCommand \index{SDCC}
14532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14537 this Symbol is always defined
14541 <row topline="true">
14542 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14548 \begin_inset LatexCommand \index{SDCC\_mcs51}
14553 \begin_inset LatexCommand \index{SDCC\_ds390}
14558 \begin_inset LatexCommand \index{SDCC\_z80}
14565 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14570 depending on the model used (e.g.: -mds390
14574 <row topline="true">
14575 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14581 \begin_inset LatexCommand \index{\_\_mcs51}
14586 \begin_inset LatexCommand \index{\_\_ds390}
14591 \begin_inset LatexCommand \index{\_\_hc08}
14596 \begin_inset LatexCommand \index{\_\_z80}
14603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14608 depending on the model used (e.g.
14613 <row topline="true">
14614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14620 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14650 <row topline="true">
14651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14657 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14664 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14687 <row topline="true">
14688 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14694 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14724 <row topline="true">
14725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14731 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
14738 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14761 <row topline="true">
14762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14768 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
14775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14788 <row topline="true" bottomline="true">
14789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14795 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
14802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14822 Notes on supported Processors
14826 \begin_inset LatexCommand \label{sub:MCS51-variants}
14831 \begin_inset LatexCommand \index{MCS51 variants}
14838 MCS51 processors are available from many vendors and come in many different
14840 While they might differ considerably in respect to Special Function Registers
14841 the core MCS51 is usually not modified or is kept compatible.
14845 pdata access by SFR
14848 With the upcome of devices with internal xdata and flash memory devices
14850 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14854 as dedicated I/O port is becoming more popular.
14855 Switching the high byte for pdata
14856 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14860 access which was formerly done by port P2 is then achieved by a Special
14862 \begin_inset LatexCommand \index{sfr}
14867 In well-established MCS51 tradition the address of this
14871 is where the chip designers decided to put it.
14872 Needless to say that they didn't agree on a common name either.
14873 So that the startup code can correctly initialize xdata variables, you
14874 should define an sfr with the name _XPAGE
14877 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
14883 at the appropriate location if the default, port P2, is not used for this.
14889 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
14894 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
14899 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
14902 For more exotic implementations further customizations may be needed.
14904 \begin_inset LatexCommand \ref{sub:Startup-Code}
14908 for other possibilities.
14911 Other Features available by SFR
14914 Some MCS51 variants offer features like Double DPTR
14915 \begin_inset LatexCommand \index{DPTR}
14919 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
14920 These are currently not used for the MCS51 port.
14921 If you absolutely need them you can fall back to inline assembly or submit
14928 The DS80C400 microcontroller has a rich set of peripherals.
14929 In its built-in ROM library it includes functions to access some of the
14930 features, among them is a TCP stack with IP4 and IP6 support.
14931 Library headers (currently in beta status) and other files are provided
14935 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
14943 The Z80 and gbz80 port
14946 SDCC can target both the Zilog
14947 \begin_inset LatexCommand \index{Z80}
14951 and the Nintendo Gameboy's Z80-like gbz80
14952 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
14957 The Z80 port is passed through the same
14960 \begin_inset LatexCommand \index{Regression test}
14966 as the MCS51 and DS390 ports, so floating point support, support for long
14967 variables and bitfield support is fine.
14968 See mailing lists and forums about interrupt routines.
14971 As always, the code is the authoritative reference - see z80/ralloc.c and
14974 \begin_inset LatexCommand \index{stack}
14978 frame is similar to that generated by the IAR Z80 compiler.
14979 IX is used as the base pointer, HL and IY are used as a temporary registers,
14980 and BC and DE are available for holding variables.
14982 \begin_inset LatexCommand \index{return value}
14986 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
14988 The gbz80 port use the same set of registers for the return values, but
14989 in a different order of significance: E (one byte), DE (two bytes), or
14996 The port to the Motorola HC08
14997 \begin_inset LatexCommand \index{HC08}
15001 family has been added in October 2003, and is still undergoing some basic
15003 The code generator is complete, but the register allocation is still quite
15005 Some of the SDCC's standard C library functions have embedded non-HC08
15006 inline assembly and so are not yet usable.
15017 \begin_inset LatexCommand \index{PIC14}
15021 port still requires a major effort from the development community.
15022 However it can work for very simple code.
15025 C code and 14bit PIC code page
15026 \begin_inset LatexCommand \index{code page (pic14)}
15031 \begin_inset LatexCommand \index{RAM bank (pic14)}
15038 The linker organizes allocation for the code page and RAM banks.
15039 It does not have intimate knowledge of the code flow.
15040 It will put all the code section of a single asm file into a single code
15042 In order to make use of multiple code pages, separate asm files must be
15044 The compiler treats all functions of a single C file as being in the same
15045 code page unless it is non static.
15046 The compiler treats all local variables of a single C file as being in
15047 the same RAM bank unless it is an extern.
15051 To get the best follow these guide lines:
15054 make local functions static, as non static functions require code page selection
15058 Make local variables static as extern variables require RAM bank selection
15062 For devices that have multiple code pages it is more efficient to use the
15063 same number of files as pages, i.e.
15064 for the 16F877 use 4 separate files and i.e.
15065 for the 16F874 use 2 separate files.
15066 This way the linker can put the code for each file into different code
15067 pages and the compiler can allocate reusable variables more efficiently
15068 and there's less page selection overhead.
15069 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15070 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15073 Creating a device include file
15076 For generating a device include file use the support perl script inc2h.pl
15077 kept in directory support/script.
15083 For the interrupt function, use the keyword 'interrupt'
15084 \begin_inset LatexCommand \index{interrupt}
15088 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15089 there to avoid a syntax error - it ought to be fixed).
15095 void Intr(void) interrupt 0
15101 T0IF = 0; /* Clear timer interrupt */
15106 Linking and assembling
15109 For assembling you can use either GPUTILS'
15110 \begin_inset LatexCommand \index{gputils (pic tools)}
15114 gpasm.exe or MPLAB's mpasmwin.exe.
15115 GPUTILS is available from
15116 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
15121 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15122 If you use MPLAB and an interrupt function then the linker script file
15123 vectors section will need to be enlarged to link with mplink.
15146 sdcc -S -V -mpic14 -p16F877 $<
15160 $(PRJ).hex: $(OBJS)
15170 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15192 sdcc -S -V -mpic14 -p16F877 $<
15202 mpasmwin /q /o $*.asm
15206 $(PRJ).hex: $(OBJS)
15216 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
15219 Please note that indentations within a
15223 have to be done with a tabulator character.
15227 \begin_inset LatexCommand \index{PIC16}
15235 \begin_inset LatexCommand \index{PIC16}
15239 port is the portion of SDCC that is responsible to produce code for the
15241 \begin_inset LatexCommand \index{Microchip}
15245 (TM) microcontrollers with 16 bit core.
15246 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
15252 PIC16 port supports the standard command line arguments as supposed, with
15253 the exception of certain cases that will be mentioned in the following
15256 \labelwidthstring 00.00.0000
15268 -stack-auto Auto variables that are function parameters, will be saved on
15272 There is no need to specify this in the command line.
15274 \labelwidthstring 00.00.0000
15286 -float-reent All floating point functions are reentrant by default.
15289 There is no need to specifiy this in the command line.
15291 \labelwidthstring 00.00.0000
15303 -callee-saves See -
15315 \labelwidthstring 00.00.0000
15327 -all-callee-saves All function arguments are passed on stack by default.
15330 There is no need to specify this in the command line.
15332 \labelwidthstring 00.00.0000
15344 -fommit-frame-pointer Frame pointer will be omitted when the function uses
15345 no local variables.
15348 Port Specific Options
15349 \begin_inset LatexCommand \index{Options PIC16}
15356 The port specific options appear after the global options in the sdcc --help
15358 \layout Subsubsection
15363 General options enable certain port features and optimizations.
15365 \labelwidthstring 00.00.0000
15377 -pgen-bank Instructs the port to insert BANKSEL directives before instructions
15378 that use the Bank Select Register (BSR).
15380 \labelwidthstring 00.00.0000
15392 -pomit-config-words Instructs the port to omit the generation of the configurati
15395 \labelwidthstring 00.00.0000
15407 -pomit-ivt Instructs the port to omit the generation of the interrupt vectors
15409 \labelwidthstring 00.00.0000
15421 -pleave-reset-vector Used in conjuction with the previous command, instructs
15422 the port NOT to omit the reset vector.
15424 \labelwidthstring 00.00.0000
15436 -stack-model=[model] Used in conjuction with the command above.
15437 Defines the stack model to be used, valid stack models are :
15440 \labelwidthstring 00.00.0000
15446 Selects small stack model.
15447 8 bit stack and frame pointers.
15448 Supports 256 bytes stack size.
15450 \labelwidthstring 00.00.0000
15456 Selects large stack model.
15457 16 bit stack and frame pointers.
15458 Supports 65536 bytes stack size.
15461 \labelwidthstring 00.00.0000
15473 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
15474 unitialized data variables with [kword].
15475 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
15477 \labelwidthstring 00.00.0000
15489 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
15490 Useful for bootloaders.
15492 \labelwidthstring 00.00.0000
15504 -asm= sets the full path and name of an external assembler to call.
15506 \labelwidthstring 00.00.0000
15518 -link= sets the full path and name of an external linker to call.
15519 \layout Subsubsection
15524 Debugging options enable extra debugging information in the output files.
15526 \labelwidthstring 00.00.0000
15538 -debug-xtra Similar to -
15549 \begin_inset LatexCommand \index{-\/-debug}
15553 , but dumps more information.
15555 \labelwidthstring 00.00.0000
15567 -debug-ralloc Force register allocator to dump <source>.d file with debugging
15569 <source> is the name of the file compiled.
15571 \labelwidthstring 00.00.0000
15583 -pcode-verbose Enable pcode debugging information in translation.
15586 Preprocessor Macros
15589 PIC16 port defines the following preprocessor macros while translating a
15594 \begin_inset Tabular
15595 <lyxtabular version="3" rows="2" columns="2">
15597 <column alignment="center" valignment="top" leftline="true" width="0">
15598 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15599 <row topline="true" bottomline="true">
15600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15608 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15617 <row topline="true" bottomline="true">
15618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15631 MCU Identification.
15636 is the microcontrol identification number, i.e.
15652 \begin_inset LatexCommand \index{PIC16}
15656 port uses the following directories for searching header files and libraries.
15660 \begin_inset Tabular
15661 <lyxtabular version="3" rows="3" columns="4">
15663 <column alignment="center" valignment="top" leftline="true" width="0">
15664 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15665 <column alignment="center" valignment="top" width="0">
15666 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15667 <row topline="true" bottomline="true">
15668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15701 <row topline="true">
15702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15707 PREFIX/sdcc/include/pic16
15710 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15715 PIC16 specific headers
15718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15726 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15735 <row topline="true" bottomline="true">
15736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15741 PREFIX/sdcc/lib/pic16
15744 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15749 PIC16 specific libraries
15752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15760 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15777 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
15784 PIC16 port currently supports the following pragmas:
15786 \labelwidthstring 00.00.0000
15788 stack pragma stack forces the code generator to initialize the stack & frame
15789 pointers at a specific address.
15790 This is an adhoc solution since gplink does not support yet stack.
15791 When the gplink issue is resolved the pragma will be deprecated
15799 It is important to initialize the stack, otherwise strange things can happen.
15800 Stack is not initialized by default because there are some sources that
15802 (like library sources)
15807 The stack pragma should be used only once in a project.
15808 Multiple pragmas may result in indeterminate behaviour of the program.
15816 /* initializes stack at RAM address 0x5ff */
15819 #pragma stack 0x5ff
15821 \labelwidthstring 00.00.0000
15823 udata pragma udata instructs the compiler to emit code so that linker will
15824 place a variable at a specific memory bank
15832 /* places variable foo at bank2 */
15835 #pragma udata bank2 foo
15841 In order for this pragma to work there are some changes that must be made
15842 in the .lkr script used in link stage.
15843 In the following example a sample .lkr file is shown:
15848 // Sample linker script for the PIC18F452 processor
15854 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
15857 CODEPAGE NAME=page START=0x2A END=0x7FFF
15860 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
15863 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
15866 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
15869 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
15872 ACCESSBANK NAME=accessram START=0x0 END=0x7F
15877 DATABANK NAME=gpr0 START=0x80 END=0xFF
15880 DATABANK NAME=gpr1 START=0x100 END=0x1FF
15883 DATABANK NAME=gpr2 START=0x200 END=0x2FF
15886 DATABANK NAME=gpr3 START=0x300 END=0x3FF
15889 DATABANK NAME=gpr4 START=0x400 END=0x4FF
15892 DATABANK NAME=gpr5 START=0x500 END=0x5FF
15895 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
15900 SECTION NAME=CONFIG ROM=config
15905 SECTION NAME=bank0 RAM=gpr0
15908 SECTION NAME=bank1 RAM=gpr1
15911 SECTION NAME=bank2 RAM=gpr2
15914 SECTION NAME=bank3 RAM=gpr3
15917 SECTION NAME=bank4 RAM=gpr4
15920 SECTION NAME=bank5 RAM=gpr5
15923 The linker will recognise the section name set in the pragma statement and
15924 will position the variable at the memory bank set with the RAM field at
15925 the SECTION line in the linker script file.
15929 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
15936 There is one main header file that can be included to the source files using
15943 This header file contains the definitions for the processor special registers,
15944 so it is necessary if the source accesses them.
15945 It can be included by adding the following line in the beginning of the
15949 #include <pic18fregs.h>
15952 The specific microcontroller is selected within the pic18fregs.h automatically,
15953 so the same source can be used with a variety of devices.
15959 The libraries that PIC16
15960 \begin_inset LatexCommand \index{PIC16}
15964 port depends on are the microcontroller device libraries which contain
15965 the symbol definitions for the microcontroller special function registers.
15966 These libraries have the format pic18fxxxx.lib, where
15970 is the microcontroller identification number.
15971 The specific library is selected automatically by the compiler at link
15972 stage according to the selected device.
15975 Libraries are created with gplib which is part of the gputils package
15976 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
15986 The following memory models are supported by the PIC16 port:
15995 Memory model affects the default size of pointers within the source.
15996 The sizes are shown in the next table:
16000 \begin_inset Tabular
16001 <lyxtabular version="3" rows="3" columns="3">
16003 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16004 <column alignment="center" valignment="top" leftline="true" width="0">
16005 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16006 <row topline="true" bottomline="true">
16007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16012 Pointer sizes according to memory model
16015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16023 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16032 <row topline="true" bottomline="true">
16033 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16041 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16058 <row topline="true" bottomline="true">
16059 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16067 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
16075 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16091 It is advisable that all sources within a project are compiled with the
16093 If one wants to override the default memory model, this can be done by
16094 declaring a pointer as
16103 Far selects large memory model's pointers, while near selects small memory
16107 The standard device libraries (see
16108 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
16112 ) contain no reference to pointers, so they can be used with both memory
16119 The stack implementation for the PIC16 port uses two indirect registers,
16122 \labelwidthstring 00.00.0000
16124 FSR1 is assigned as stack pointer
16126 \labelwidthstring 00.00.0000
16128 FSR2 is assigned as frame pointer
16131 The following stack models are supported by the PIC16 port
16144 model means that only the FSRxL byte is used to access stack and frame,
16151 uses both FSRxL and FSRxH registers.
16152 The following table shows the stack/frame pointers sizes according to stack
16153 model and the maximum space they can address:
16157 \begin_inset Tabular
16158 <lyxtabular version="3" rows="3" columns="3">
16160 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16161 <column alignment="center" valignment="top" leftline="true" width="0">
16162 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16163 <row topline="true" bottomline="true">
16164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16169 Stack & Frame pointer sizes according to stack model
16172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16189 <row topline="true">
16190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16215 <row topline="true" bottomline="true">
16216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16232 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16250 Currently stack and frame pointers should be initialized explicit by the
16251 user at the desired Data RAM position (see
16252 \begin_inset LatexCommand \ref{sub:PIC16_Pragmas}
16257 Uninitialized stack and frame pointers can result in unexpected behavior
16258 of the resulting binary.
16261 Function return values
16264 Return values from functions are placed to the appropriate registers following
16265 a modified Microchip policy optimized for SDCC.
16266 The following table shows these registers:
16270 \begin_inset Tabular
16271 <lyxtabular version="3" rows="6" columns="2">
16273 <column alignment="center" valignment="top" leftline="true" width="0">
16274 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16275 <row topline="true" bottomline="true">
16276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16289 destination register
16293 <row topline="true">
16294 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16302 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16311 <row topline="true">
16312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16320 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16329 <row topline="true">
16330 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16338 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16347 <row topline="true">
16348 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16356 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16361 FSR0L:PRODH:PRODL:WREG
16365 <row topline="true" bottomline="true">
16366 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16379 on stack, FSR0 points to the beginning
16393 When entering an interrupt, currently the PIC16
16394 \begin_inset LatexCommand \index{PIC16}
16398 port automatically saves the following registers:
16410 PROD (PRODL and PRODH)
16413 FSR0 (FSR0L and FSR0H)
16416 These registers are restored upon return from the interrupt routine.
16420 When entering a high priority interrupt WREG, STATUS and BSR are not explicit
16422 The hardware shadow registers for WREG, STATUS and BSR are used in these
16432 NOTE that when the _naked attribute is specified for an interrupt routine,
16433 then NO registers are stored or restored.
16439 Debugging with SDCDB
16440 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
16445 \begin_inset LatexCommand \index{sdcdb (debugger)}
16452 SDCC is distributed with a source level debugger
16453 \begin_inset LatexCommand \index{Debugger}
16458 The debugger uses a command line interface, the command repertoire of the
16459 debugger has been kept as close to gdb
16460 \begin_inset LatexCommand \index{gdb}
16464 (the GNU debugger) as possible.
16465 The configuration and build process is part of the standard compiler installati
16466 on, which also builds and installs the debugger in the target directory
16467 specified during configuration.
16468 The debugger allows you debug BOTH at the C source and at the ASM source
16470 Sdcdb is available on Unix platforms only.
16473 Compiling for Debugging
16487 \begin_inset LatexCommand \index{-\/-debug}
16491 option must be specified for all files for which debug information is to
16493 The complier generates a .adb file for each of these files.
16494 The linker creates the .cdb
16495 \begin_inset LatexCommand \index{<file>.cdb}
16500 \begin_inset LatexCommand \index{<file>.adb}
16504 files and the address information.
16505 This .cdb is used by the debugger.
16508 How the Debugger Works
16521 -debug option is specified the compiler generates extra symbol information
16522 some of which are put into the assembler source and some are put into the
16524 Then the linker creates the .cdb file from the individual .adb files with
16525 the address information for the symbols.
16526 The debugger reads the symbolic information generated by the compiler &
16527 the address information generated by the linker.
16528 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
16529 execution is controlled by the debugger.
16530 When a command is issued for the debugger, it translates it into appropriate
16531 commands for the simulator.
16534 Starting the Debugger
16537 The debugger can be started using the following command line.
16538 (Assume the file you are debugging has the file name foo).
16552 The debugger will look for the following files.
16555 foo.c - the source file.
16558 foo.cdb - the debugger symbol information file.
16561 foo.ihx - the Intel hex format
16562 \begin_inset LatexCommand \index{Intel hex format}
16569 Command Line Options.
16582 -directory=<source file directory> this option can used to specify the directory
16584 The debugger will look into the directory list specified for source, cdb
16586 The items in the directory list must be separated by ':', e.g.
16587 if the source files can be in the directories /home/src1 and /home/src2,
16598 -directory option should be -
16608 -directory=/home/src1:/home/src2.
16609 Note there can be no spaces in the option.
16613 -cd <directory> - change to the <directory>.
16616 -fullname - used by GUI front ends.
16619 -cpu <cpu-type> - this argument is passed to the simulator please see the
16620 simulator docs for details.
16623 -X <Clock frequency > this options is passed to the simulator please see
16624 the simulator docs for details.
16627 -s <serial port file> passed to simulator see the simulator docs for details.
16630 -S <serial in,out> passed to simulator see the simulator docs for details.
16633 -k <port number> passed to simulator see the simulator docs for details.
16639 As mentioned earlier the command interface for the debugger has been deliberatel
16640 y kept as close the GNU debugger gdb, as possible.
16641 This will help the integration with existing graphical user interfaces
16642 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
16643 If you use a graphical user interface for the debugger you can skip the
16645 \layout Subsubsection*
16647 break [line | file:line | function | file:function]
16650 Set breakpoint at specified line or function:
16659 sdcdb>break foo.c:100
16661 sdcdb>break funcfoo
16663 sdcdb>break foo.c:funcfoo
16664 \layout Subsubsection*
16666 clear [line | file:line | function | file:function ]
16669 Clear breakpoint at specified line or function:
16678 sdcdb>clear foo.c:100
16680 sdcdb>clear funcfoo
16682 sdcdb>clear foo.c:funcfoo
16683 \layout Subsubsection*
16688 Continue program being debugged, after breakpoint.
16689 \layout Subsubsection*
16694 Execute till the end of the current function.
16695 \layout Subsubsection*
16700 Delete breakpoint number 'n'.
16701 If used without any option clear ALL user defined break points.
16702 \layout Subsubsection*
16704 info [break | stack | frame | registers ]
16707 info break - list all breakpoints
16710 info stack - show the function call stack.
16713 info frame - show information about the current execution frame.
16716 info registers - show content of all registers.
16717 \layout Subsubsection*
16722 Step program until it reaches a different source line.
16723 Note: pressing <return> repeats the last command.
16724 \layout Subsubsection*
16729 Step program, proceeding through subroutine calls.
16730 \layout Subsubsection*
16735 Start debugged program.
16736 \layout Subsubsection*
16741 Print type information of the variable.
16742 \layout Subsubsection*
16747 print value of variable.
16748 \layout Subsubsection*
16753 load the given file name.
16754 Note this is an alternate method of loading file for debugging.
16755 \layout Subsubsection*
16760 print information about current frame.
16761 \layout Subsubsection*
16766 Toggle between C source & assembly source.
16767 \layout Subsubsection*
16769 ! simulator command
16772 Send the string following '!' to the simulator, the simulator response is
16774 Note the debugger does not interpret the command being sent to the simulator,
16775 so if a command like 'go' is sent the debugger can loose its execution
16776 context and may display incorrect values.
16777 \layout Subsubsection*
16784 My name is Bobby Brown"
16787 Interfacing with XEmacs
16788 \begin_inset LatexCommand \index{XEmacs}
16793 \begin_inset LatexCommand \index{Emacs}
16800 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
16801 sdcdb.el and sdcdbsrc.el.
16802 These two files can be found in the $(prefix)/bin directory after the installat
16804 These files need to be loaded into XEmacs for the interface to work.
16805 This can be done at XEmacs startup time by inserting the following into
16806 your '.xemacs' file (which can be found in your HOME directory):
16812 (load-file sdcdbsrc.el)
16818 .xemacs is a lisp file so the () around the command is REQUIRED.
16819 The files can also be loaded dynamically while XEmacs is running, set the
16820 environment variable 'EMACSLOADPATH' to the installation bin directory
16821 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
16822 To start the interface enter the following command:
16836 You will prompted to enter the file name to be debugged.
16841 The command line options that are passed to the simulator directly are bound
16842 to default values in the file sdcdbsrc.el.
16843 The variables are listed below, these values maybe changed as required.
16846 sdcdbsrc-cpu-type '51
16849 sdcdbsrc-frequency '11059200
16852 sdcdbsrc-serial nil
16855 The following is a list of key mapping for the debugger interface.
16866 ;;key\SpecialChar ~
16880 binding\SpecialChar ~
16904 ;;---\SpecialChar ~
16918 -------\SpecialChar ~
16960 sdcdb-next-from-src\SpecialChar ~
16988 sdcdb-back-from-src\SpecialChar ~
17016 sdcdb-cont-from-src\SpecialChar ~
17026 SDCDB continue command
17044 sdcdb-step-from-src\SpecialChar ~
17072 sdcdb-whatis-c-sexp\SpecialChar ~
17082 SDCDB ptypecommand for data at
17149 sdcdbsrc-delete\SpecialChar ~
17163 SDCDB Delete all breakpoints if no arg
17212 given or delete arg (C-u arg x)
17230 sdcdbsrc-frame\SpecialChar ~
17245 SDCDB Display current frame if no arg,
17294 given or display frame arg
17361 sdcdbsrc-goto-sdcdb\SpecialChar ~
17371 Goto the SDCDB output buffer
17389 sdcdb-print-c-sexp\SpecialChar ~
17400 SDCDB print command for data at
17467 sdcdbsrc-goto-sdcdb\SpecialChar ~
17477 Goto the SDCDB output buffer
17495 sdcdbsrc-mode\SpecialChar ~
17511 Toggles Sdcdbsrc mode (turns it off)
17526 sdcdb-finish-from-src\SpecialChar ~
17534 SDCDB finish command
17549 sdcdb-break\SpecialChar ~
17567 Set break for line with point
17582 sdcdbsrc-mode\SpecialChar ~
17598 Toggle Sdcdbsrc mode
17613 sdcdbsrc-srcmode\SpecialChar ~
17636 Here are a few guidelines that will help the compiler generate more efficient
17637 code, some of the tips are specific to this compiler others are generally
17638 good programming practice.
17641 Use the smallest data type to represent your data-value.
17642 If it is known in advance that the value is going to be less than 256 then
17643 use an 'unsigned char' instead of a 'short' or 'int'.
17644 Please note, that ANSI C requires both signed and unsigned chars to be
17645 promoted to 'signed int' before doing any operation.
17646 This promotion can be omitted, if the result is the same.
17647 The effect of the promotion rules together with the sign-extension is often
17654 unsigned char uc = 0xfe;
17656 if (uc * uc < 0) /* this is true! */
17675 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
17685 (unsigned char) -12 / (signed char) -3 = ...
17688 No, the result is not 4:
17693 (int) (unsigned char) -12 / (int) (signed char) -3 =
17695 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
17697 (int) 0x00f4 / (int) 0xfffd =
17699 (int) 0x00f4 / (int) 0xfffd =
17701 (int) 244 / (int) -3 =
17703 (int) -81 = (int) 0xffaf;
17706 Don't complain, that gcc gives you a different result.
17707 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
17708 Therefore the results are different.
17711 \begin_inset Quotes sld
17715 \begin_inset Quotes srd
17721 If well-defined overflow characteristics are important and negative values
17722 are not, or if you want to steer clear of sign-extension problems when
17723 manipulating bits or bytes, use one of the corresponding unsigned types.
17724 (Beware when mixing signed and unsigned values in expressions, though.)
17726 Although character types (especially unsigned char) can be used as "tiny"
17727 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
17728 ble sign extension and increased code size.
17732 Use unsigned when it is known in advance that the value is not going to
17734 This helps especially if you are doing division or multiplication, bit-shifting
17735 or are using an array index.
17738 NEVER jump into a LOOP.
17741 Declare the variables to be local
17742 \begin_inset LatexCommand \index{local variables}
17746 whenever possible, especially loop control variables (induction).
17749 Since the compiler does not always do implicit integral promotion, the programme
17750 r should do an explicit cast when integral promotion is required.
17753 Reducing the size of division, multiplication & modulus operations can reduce
17754 code size substantially.
17755 Take the following code for example.
17761 foobar(unsigned int p1, unsigned char ch)
17769 unsigned char ch1 = p1 % ch ;
17780 For the modulus operation the variable ch will be promoted to unsigned int
17781 first then the modulus operation will be performed (this will lead to a
17782 call to support routine _moduint()), and the result will be casted to a
17784 If the code is changed to
17789 foobar(unsigned int p1, unsigned char ch)
17797 unsigned char ch1 = (unsigned char)p1 % ch ;
17808 It would substantially reduce the code generated (future versions of the
17809 compiler will be smart enough to detect such optimization opportunities).
17813 Have a look at the assembly listing to get a
17814 \begin_inset Quotes sld
17818 \begin_inset Quotes srd
17821 for the code generation.
17825 \begin_inset LatexCommand \index{Tools}
17829 included in the distribution
17833 \begin_inset Tabular
17834 <lyxtabular version="3" rows="12" columns="3">
17836 <column alignment="center" valignment="top" leftline="true" width="0pt">
17837 <column alignment="center" valignment="top" leftline="true" width="0pt">
17838 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
17839 <row topline="true" bottomline="true">
17840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17865 <row topline="true">
17866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17879 Simulator for various architectures
17882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17891 <row topline="true">
17892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17905 header file conversion
17908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17913 sdcc/support/scripts
17917 <row topline="true">
17918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17931 header file conversion
17934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17939 sdcc/support/scripts
17943 <row topline="true">
17944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17978 <row topline="true">
17979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17987 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17995 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18013 <row topline="true">
18014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18048 <row topline="true">
18049 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18065 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18083 <row topline="true">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18092 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18100 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18118 <row topline="true">
18119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18153 <row topline="true">
18154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18162 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18188 <row topline="true" bottomline="true">
18189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18233 \begin_inset LatexCommand \index{Documentation}
18237 included in the distribution
18241 \begin_inset Tabular
18242 <lyxtabular version="3" rows="10" columns="2">
18244 <column alignment="left" valignment="top" leftline="true" width="0">
18245 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
18246 <row topline="true" bottomline="true">
18247 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18255 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18260 Where to get / filename
18264 <row topline="true">
18265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18270 SDCC Compiler User Guide
18273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18278 You're reading it right now
18282 <row topline="true">
18283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18300 <row topline="true">
18301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18307 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
18312 \begin_inset LatexCommand \index{Assembler documentation}
18316 Assemblers and ASLINK
18317 \begin_inset LatexCommand \index{aslink}
18322 \begin_inset LatexCommand \index{Linker documentation}
18329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18334 sdcc/as/doc/asxhtm.html
18338 <row topline="true">
18339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18344 SDCC regression test
18345 \begin_inset LatexCommand \index{Regression test}
18352 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18357 sdcc/doc/test_suite_spec.pdf
18361 <row topline="true">
18362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18379 <row topline="true">
18380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18385 Notes on debugging with sdcdb
18386 \begin_inset LatexCommand \index{sdcdb (debugger)}
18393 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18398 sdcc/debugger/README
18402 <row topline="true">
18403 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18408 Software simulator for microcontrollers
18411 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18438 <row topline="true">
18439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18444 Temporary notes on the pic16
18445 \begin_inset LatexCommand \index{PIC16}
18452 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18457 sdcc/src/pic16/NOTES
18461 <row topline="true" bottomline="true">
18462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18467 SDCC internal documentation (debugging file format)
18470 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18506 Related open source tools
18507 \begin_inset LatexCommand \index{Related tools}
18515 \begin_inset Tabular
18516 <lyxtabular version="3" rows="11" columns="3">
18518 <column alignment="center" valignment="top" leftline="true" width="0pt">
18519 <column alignment="block" valignment="top" leftline="true" width="30line%">
18520 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18521 <row topline="true" bottomline="true">
18522 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18530 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18538 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18547 <row topline="true">
18548 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18554 \begin_inset LatexCommand \index{gpsim (pic simulator)}
18561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18575 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
18583 <row topline="true">
18584 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18590 \begin_inset LatexCommand \index{gputils (pic tools)}
18597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18605 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18611 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
18619 <row topline="true">
18620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18628 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18636 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18642 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
18650 <row topline="true">
18651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18657 \begin_inset LatexCommand \index{indent (source formatting tool)}
18664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18669 Formats C source - Master of the white spaces
18672 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18678 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
18686 <row topline="true">
18687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18693 \begin_inset LatexCommand \index{srecord (tool)}
18700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18705 Object file conversion, checksumming, ...
18708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18714 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
18722 <row topline="true">
18723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18729 \begin_inset LatexCommand \index{objdump (tool)}
18736 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18741 Object file conversion, ...
18744 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18749 Part of binutils (should be there anyway)
18753 <row topline="true">
18754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18760 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
18767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18772 Source code documentation system
18775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18781 \begin_inset LatexCommand \url{http://www.doxygen.org}
18789 <row topline="true">
18790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18803 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
18806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18812 \begin_inset LatexCommand \url{http://www.kdevelop.org}
18820 <row topline="true">
18821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18827 \begin_inset LatexCommand \index{splint (syntax checking tool)}
18834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18839 Statically checks c sources (has anyone adapted splint for SDCC?)
18842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18848 \begin_inset LatexCommand \url{http://www.splint.org}
18856 <row topline="true" bottomline="true">
18857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18863 \begin_inset LatexCommand \index{ddd (debugger)}
18870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18875 Debugger, serves nicely as GUI to sdcdb
18876 \begin_inset LatexCommand \index{sdcdb (debugger)}
18883 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18889 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
18906 Related documentation / recommended reading
18910 \begin_inset Tabular
18911 <lyxtabular version="3" rows="6" columns="3">
18913 <column alignment="center" valignment="top" leftline="true" width="0pt">
18914 <column alignment="block" valignment="top" leftline="true" width="30line%">
18915 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18916 <row topline="true" bottomline="true">
18917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18925 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18933 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18942 <row topline="true">
18943 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18966 \begin_inset LatexCommand \index{C Reference card}
18973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18979 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
18987 <row topline="true">
18988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19010 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
19018 <row topline="true">
19019 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19026 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19031 Latest datasheet of the target CPU
19034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19043 <row topline="true">
19044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19051 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19056 Revision history of datasheet
19059 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19068 <row topline="true" bottomline="true">
19069 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19079 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19084 Advanced Compiler Design and Implementation
19087 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19092 bookstore (very dedicated, probably read other books first)
19108 Some questions answered, some pointers given - it might be time to in turn
19116 can you solve your project with the selected microcontroller? Would you
19117 find out early or rather late that your target is too small/slow/whatever?
19118 Can you switch to a slightly better device if it doesn't fit?
19121 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
19122 and/or another programming language be more adequate? Would an operating
19123 system on the target device help?
19126 if you solved the problem, will the marketing department be happy?
19129 if the marketing department is happy, will customers be happy?
19132 if you're the project manager, marketing department and maybe even the customer
19133 in one person, have you tried to see the project from the outside?
19136 is the project done if you think it is done? Or is just that other interface/pro
19137 tocol/feature/configuration/option missing? How about website, manual(s),
19138 internationali(z|s)ation, packaging, labels, 2nd source for components,
19139 electromagnetic compatability/interference, documentation for production,
19140 production test software, update mechanism, patent issues?
19143 is your project adequately positioned in that magic triangle: fame, fortune,
19147 Maybe not all answers to these questions are known and some answers may
19152 , nevertheless knowing these questions may help you to avoid burnout
19158 burnout is bad for electronic devices, programmers and motorcycle tyres
19162 Chances are you didn't want to hear some of them...
19166 \begin_inset LatexCommand \index{Support}
19173 SDCC has grown to be a large project.
19174 The compiler alone (without the preprocessor, assembler and linker) is
19175 well over 100,000 lines of code (blank stripped).
19176 The open source nature of this project is a key to its continued growth
19178 You gain the benefit and support of many active software developers and
19180 Is SDCC perfect? No, that's why we need your help.
19181 The developers take pride in fixing reported bugs.
19182 You can help by reporting the bugs and helping other SDCC users.
19183 There are lots of ways to contribute, and we encourage you to take part
19184 in making SDCC a great software package.
19188 The SDCC project is hosted on the SDCC sourceforge site at
19189 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
19194 You'll find the complete set of mailing lists
19195 \begin_inset LatexCommand \index{Mailing list(s)}
19199 , forums, bug reporting system, patch submission
19200 \begin_inset LatexCommand \index{Patch submission}
19205 \begin_inset LatexCommand \index{download}
19209 area and cvs code repository
19210 \begin_inset LatexCommand \index{cvs code repository}
19218 \begin_inset LatexCommand \index{Bug reporting}
19223 \begin_inset LatexCommand \index{Reporting bugs}
19230 The recommended way of reporting bugs is using the infrastructure of the
19232 You can follow the status of bug reports there and have an overview about
19236 Bug reports are automatically forwarded to the developer mailing list and
19237 will be fixed ASAP.
19238 When reporting a bug, it is very useful to include a small test program
19239 (the smaller the better) which reproduces the problem.
19240 If you can isolate the problem by looking at the generated assembly code,
19241 this can be very helpful.
19242 Compiling your program with the -
19253 \begin_inset LatexCommand \index{-\/-dumpall}
19257 option can sometimes be useful in locating optimization problems.
19258 When reporting a bug please maker sure you:
19261 Attach the code you are compiling with SDCC.
19265 Specify the exact command you use to run SDCC, or attach your Makefile.
19269 Specify the SDCC version (type "
19275 "), your platform, and operating system.
19279 Provide an exact copy of any error message or incorrect output.
19283 Put something meaningful in the subject of your message.
19286 Please attempt to include these 5 important parts, as applicable, in all
19287 requests for support or when reporting any problems or bugs with SDCC.
19288 Though this will make your message lengthy, it will greatly improve your
19289 chance that SDCC users and developers will be able to help you.
19290 Some SDCC developers are frustrated by bug reports without code provided
19291 that they can use to reproduce and ultimately fix the problem, so please
19292 be sure to provide sample code if you are reporting a bug!
19295 Please have a short check that you are using a recent version of SDCC and
19296 the bug is not yet known.
19297 This is the link for reporting bugs:
19298 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
19305 Requesting Features
19306 \begin_inset LatexCommand \label{sub:Requesting-Features}
19311 \begin_inset LatexCommand \index{Feature request}
19316 \begin_inset LatexCommand \index{Requesting features}
19323 Like bug reports feature requests are forwarded to the developer mailing
19325 This is the link for requesting features:
19326 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
19336 Like bug reports contributed patches are forwarded to the developer mailing
19338 This is the link for submitting patches
19339 \begin_inset LatexCommand \index{Patch submission}
19344 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
19351 You need to specify some parameters to the
19355 command for the patches to be useful.
19356 If you modified more than one file a patch created f.e.
19361 \begin_inset Quotes sld
19364 diff -Naur unmodified_directory modified_directory >my_changes.patch
19365 \begin_inset Quotes srd
19371 will be fine, otherwise
19375 \begin_inset Quotes sld
19378 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
19379 \begin_inset Quotes srd
19392 These links should take you directly to the
19393 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
19403 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
19404 automated messages (mid 2003)
19408 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
19413 \begin_inset LatexCommand \index{Mailing list(s)}
19417 and forums are archived and searchable so if you are lucky someone already
19418 had a similar problem.
19419 While mails to the lists themselves are delivered promptly their web front
19420 end on sourceforge sometimes shows a severe time lag (up to several weeks),
19421 if you're seriously using SDCC please consider subscribing to the lists.
19427 You can follow the status of the cvs version
19428 \begin_inset LatexCommand \index{version}
19432 of SDCC by watching the Changelog
19433 \begin_inset LatexCommand \index{Changelog}
19437 in the cvs-repository
19442 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
19450 \begin_inset LatexCommand \index{Release policy}
19457 Historically there often were long delays between official releases and
19458 the sourceforge download area tends to get not updated at all.
19459 Excuses in the past might have referred to problems with live range analysis,
19460 but as this was fixed a while ago, the current problem is that another
19461 excuse has to be found.
19462 Kidding aside, we have to get better there! On the other hand there are
19463 daily snapshots available at
19464 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
19468 , and you can always build the very last version (hopefully with many bugs
19469 fixed, and features added) from the source code available at
19470 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
19478 \begin_inset LatexCommand \index{Examples}
19485 You'll find some small examples in the directory
19487 sdcc/device/examples/.
19490 More examples and libraries are available at
19492 The SDCC Open Knowledge Resource
19493 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
19500 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
19507 I did insert a reference to Paul's web site here although it seems rather
19508 dedicated to a specific 8032 board (I think it's okay because it f.e.
19509 shows LCD/Harddisc interface and has a free 8051 monitor.
19510 Independent 8032 board vendors face hard competition of heavily subsidized
19511 development boards anyway).
19514 Maybe we should include some links to real world applications.
19515 Preferably pointer to pointers (one for each architecture) so this stays
19520 \begin_inset LatexCommand \index{Quality control}
19527 The compiler is passed through nightly compile and build checks.
19533 \begin_inset LatexCommand \index{Regression test}
19537 check that SDCC itself compiles flawlessly on several platforms and checks
19538 the quality of the code generated by SDCC by running the code through simulator
19540 There is a separate document
19543 \begin_inset LatexCommand \index{Test suite}
19552 You'll find the test code in the directory
19554 sdcc/support/regression
19557 You can run these tests manually by running
19561 in this directory (or f.e.
19566 \begin_inset Quotes sld
19570 \begin_inset Quotes srd
19576 if you don't want to run the complete tests).
19577 The test code might also be interesting if you want to look for examples
19578 \begin_inset LatexCommand \index{Examples}
19582 checking corner cases of SDCC or if you plan to submit patches
19583 \begin_inset LatexCommand \index{Patch submission}
19590 The pic port uses a different set of regression tests, you'll find them
19593 sdcc/src/regression
19598 SDCC Technical Data
19602 \begin_inset LatexCommand \index{Optimizations}
19609 SDCC performs a host of standard optimizations in addition to some MCU specific
19614 Sub-expression Elimination
19615 \begin_inset LatexCommand \index{Subexpression elimination}
19622 The compiler does local and
19648 will be translated to
19660 Some subexpressions are not as obvious as the above example, e.g.:
19670 In this case the address arithmetic a->b[i] will be computed only once;
19671 the equivalent code in C would be.
19683 The compiler will try to keep these temporary variables in registers.
19686 Dead-Code Elimination
19687 \begin_inset LatexCommand \index{Dead-code elimination}
19708 i = 1; \SpecialChar ~
19717 global = 1;\SpecialChar ~
19730 global = 3;\SpecialChar ~
19755 \begin_inset LatexCommand \index{Copy propagation}
19811 Note: the dead stores created by this copy propagation will be eliminated
19812 by dead-code elimination.
19816 \begin_inset LatexCommand \index{Loop optimization}
19821 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
19828 Two types of loop optimizations are done by SDCC
19836 of loop induction variables.
19837 In addition to the strength reduction the optimizer marks the induction
19838 variables and the register allocator tries to keep the induction variables
19839 in registers for the duration of the loop.
19840 Because of this preference of the register allocator
19841 \begin_inset LatexCommand \index{Register allocation}
19845 , loop induction optimization causes an increase in register pressure, which
19846 may cause unwanted spilling of other temporary variables into the stack
19847 \begin_inset LatexCommand \index{stack}
19852 The compiler will generate a warning message when it is forced to allocate
19853 extra space either on the stack or data space.
19854 If this extra space allocation is undesirable then induction optimization
19855 can be eliminated either for the entire source file (with -
19865 -noinduction option) or for a given function only using #pragma\SpecialChar ~
19867 \begin_inset LatexCommand \index{\#pragma noinduction}
19880 for (i = 0 ; i < 100 ; i ++)
19896 for (i = 0; i < 100; i++)
19905 As mentioned previously some loop invariants are not as apparent, all static
19906 address computations are also moved out of the loop.
19911 \begin_inset LatexCommand \index{Strength reduction}
19915 , this optimization substitutes an expression by a cheaper expression:
19920 for (i=0;i < 100; i++)
19938 for (i=0;i< 100;i++) {
19944 ar[itemp1] = itemp2;
19961 The more expensive multiplication
19962 \begin_inset LatexCommand \index{Multiplication}
19966 is changed to a less expensive addition.
19970 \begin_inset LatexCommand \index{Loop reversing}
19977 This optimization is done to reduce the overhead of checking loop boundaries
19978 for every iteration.
19979 Some simple loops can be reversed and implemented using a
19980 \begin_inset Quotes eld
19983 decrement and jump if not zero
19984 \begin_inset Quotes erd
19988 SDCC checks for the following criterion to determine if a loop is reversible
19989 (note: more sophisticated compilers use data-dependency analysis to make
19990 this determination, SDCC uses a more simple minded analysis).
19993 The 'for' loop is of the form
19999 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
20009 The <for body> does not contain
20010 \begin_inset Quotes eld
20014 \begin_inset Quotes erd
20018 \begin_inset Quotes erd
20024 All goto's are contained within the loop.
20027 No function calls within the loop.
20030 The loop control variable <sym> is not assigned any value within the loop
20033 The loop control variable does NOT participate in any arithmetic operation
20037 There are NO switch statements in the loop.
20040 Algebraic Simplifications
20043 SDCC does numerous algebraic simplifications, the following is a small sub-set
20044 of these optimizations.
20049 i = j + 0;\SpecialChar ~
20053 /* changed to: */\SpecialChar ~
20059 i /= 2;\SpecialChar ~
20066 /* changed to: */\SpecialChar ~
20072 i = j - j;\SpecialChar ~
20076 /* changed to: */\SpecialChar ~
20082 i = j / 1;\SpecialChar ~
20086 /* changed to: */\SpecialChar ~
20093 Note the subexpressions
20094 \begin_inset LatexCommand \index{Subexpression}
20098 given above are generally introduced by macro expansions or as a result
20099 of copy/constant propagation.
20102 'switch' Statements
20103 \begin_inset LatexCommand \label{sub:'switch'-Statements}
20108 \begin_inset LatexCommand \index{switch statement}
20115 SDCC can optimize switch statements to jump tables
20116 \begin_inset LatexCommand \index{jump tables}
20121 It makes the decision based on an estimate of the generated code size.
20122 SDCC is quite liberal in the requirements for jump table generation:
20125 The labels need not be in order, and the starting number need not be one
20126 or zero, the case labels are in numerical sequence or not too many case
20127 labels are missing.
20133 switch(i) {\SpecialChar ~
20164 case 4: ...\SpecialChar ~
20196 case 5: ...\SpecialChar ~
20228 case 3: ...\SpecialChar ~
20259 case 6: ...\SpecialChar ~
20291 case 7: ...\SpecialChar ~
20323 case 8: ...\SpecialChar ~
20355 case 9: ...\SpecialChar ~
20387 case 10: ...\SpecialChar ~
20418 case 11: ...\SpecialChar ~
20485 Both the above switch statements will be implemented using a jump-table.
20486 The example to the right side is slightly more efficient as the check for
20487 the lower boundary of the jump-table is not needed.
20491 The number of case labels is not larger than supported by the target architectur
20495 If the case labels are not in numerical sequence ('gaps' between cases)
20496 SDCC checks whether a jump table with additionally inserted dummy cases
20497 is still attractive.
20501 If the starting number is not zero and a check for the lower boundary of
20502 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
20507 Switch statements which have large gaps in the numeric sequence or those
20508 that have too many case labels can be split into more than one switch statement
20509 for efficient code generation, e.g.:
20589 If the above switch statement is broken down into two switch statements
20679 then both the switch statements will be implemented using jump-tables whereas
20680 the unmodified switch statement will not be.
20683 There might be reasons which SDCC cannot know about to either favour or
20684 not favour jump tables.
20685 If the target system has to be as quick for the last switch case as for
20686 the first (pro jump table), or if the switch argument is known to be zero
20687 in the majority of the cases (contra jump table).
20690 The pragma nojtbound
20691 \begin_inset LatexCommand \index{\#pragma nojtbound}
20695 can be used to turn off checking the
20708 It has no effect if a default label is supplied.
20709 Use of this pragma is dangerous: if the switch argument is not matched
20710 by a case statement the processor will happily jump into Nirvana.
20713 Bit-shifting Operations
20714 \begin_inset LatexCommand \index{Bit shifting}
20721 Bit shifting is one of the most frequently used operation in embedded programmin
20723 SDCC tries to implement bit-shift operations in the most efficient way
20739 generates the following code:
20756 In general SDCC will never setup a loop if the shift count is known.
20799 \begin_inset LatexCommand \index{Bit rotation}
20806 A special case of the bit-shift operation is bit rotation
20807 \begin_inset LatexCommand \index{rotating bits}
20811 , SDCC recognizes the following expression to be a left bit-rotation:
20821 char i;\SpecialChar ~
20832 /* unsigned is needed for rotation */
20837 i = ((i << 1) | (i >> 7));
20846 will generate the following code:
20865 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
20866 ns of this case will also be recognized as bit-rotation, i.e.:
20871 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
20874 Nibble and Byte Swapping
20877 Other special cases of the bit-shift operations are nibble or byte swapping
20878 \begin_inset LatexCommand \index{swapping nibbles/bytes}
20882 , SDCC recognizes the following expressions:
20905 i = ((i << 4) | (i >> 4));
20911 j = ((j << 8) | (j >> 8));
20914 and generates a swap instruction for the nibble swapping
20915 \begin_inset LatexCommand \index{Nibble swapping}
20919 or move instructions for the byte swapping
20920 \begin_inset LatexCommand \index{Byte swapping}
20926 \begin_inset Quotes sld
20930 \begin_inset Quotes srd
20933 example can be used to convert from little to big-endian or vice versa.
20934 If you want to change the endianness of a
20938 integer you have to cast to
20945 Note that SDCC stores numbers in little-endian
20951 Usually 8-bit processors don't care much about endianness.
20952 This is not the case for the standard 8051 which only has an instruction
20958 \begin_inset LatexCommand \index{DPTR}
20966 so little-endian is the more efficient byte order.
20970 \begin_inset LatexCommand \index{little-endian}
20975 \begin_inset LatexCommand \index{Endianness}
20980 lowest order first).
20984 \begin_inset LatexCommand \index{Highest Order Bit}
20991 It is frequently required to obtain the highest order bit of an integral
20992 type (long, int, short or char types).
20993 SDCC recognizes the following expression to yield the highest order bit
20994 and generates optimized code for it, e.g.:
21016 hob = (gint >> 15) & 1;
21026 will generate the following code:
21059 000A E5*01\SpecialChar ~
21086 000C 23\SpecialChar ~
21117 000D 54 01\SpecialChar ~
21144 000F F5*02\SpecialChar ~
21172 Variations of this case however will
21177 It is a standard C expression, so I heartily recommend this be the only
21178 way to get the highest order bit, (it is portable).
21179 Of course it will be recognized even if it is embedded in other expressions,
21185 xyz = gint + ((gint >> 15) & 1);
21188 will still be recognized.
21192 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
21197 \begin_inset LatexCommand \index{Peephole optimizer}
21204 The compiler uses a rule based, pattern matching and re-writing mechanism
21205 for peep-hole optimization.
21210 a peep-hole optimizer by Christopher W.
21211 Fraser (cwfraser@microsoft.com).
21212 A default set of rules are compiled into the compiler, additional rules
21213 may be added with the
21226 \begin_inset LatexCommand \index{-\/-peep-file}
21233 The rule language is best illustrated with examples.
21257 The above rule will change the following assembly
21258 \begin_inset LatexCommand \index{Assembler routines}
21280 Note: All occurrences of a
21284 (pattern variable) must denote the same string.
21285 With the above rule, the assembly sequence:
21295 will remain unmodified.
21299 Other special case optimizations may be added by the user (via
21315 some variants of the 8051 MCU
21316 \begin_inset LatexCommand \index{MCS51 variants}
21329 The following two rules will change all
21348 replace { lcall %1 } by { acall %1 }
21350 replace { ljmp %1 } by { ajmp %1 }
21355 inline-assembler code
21357 is also passed through the peep hole optimizer, thus the peephole optimizer
21358 can also be used as an assembly level macro expander.
21359 The rules themselves are MCU dependent whereas the rule language infra-structur
21360 e is MCU independent.
21361 Peephole optimization rules for other MCU can be easily programmed using
21366 The syntax for a rule is as follows:
21371 rule := replace [ restart ] '{' <assembly sequence> '
21409 <assembly sequence> '
21427 '}' [if <functionName> ] '
21432 <assembly sequence> := assembly instruction (each instruction including
21433 labels must be on a separate line).
21437 The optimizer will apply to the rules one by one from the top in the sequence
21438 of their appearance, it will terminate when all rules are exhausted.
21439 If the 'restart' option is specified, then the optimizer will start matching
21440 the rules again from the top, this option for a rule is expensive (performance)
21441 , it is intended to be used in situations where a transformation will trigger
21442 the same rule again.
21443 An example of this (not a good one, it has side effects) is the following
21466 Note that the replace pattern cannot be a blank, but can be a comment line.
21467 Without the 'restart' option only the innermost 'pop' 'push' pair would
21468 be eliminated, i.e.:
21498 the restart option the rule will be applied again to the resulting code
21499 and then all the pop-push pairs will be eliminated to yield:
21509 A conditional function can be attached to a rule.
21510 Attaching rules are somewhat more involved, let me illustrate this with
21537 The optimizer does a look-up of a function name table defined in function
21542 in the source file SDCCpeeph.c, with the name
21547 If it finds a corresponding entry the function is called.
21548 Note there can be no parameters specified for these functions, in this
21553 is crucial, since the function
21557 expects to find the label in that particular variable (the hash table containin
21558 g the variable bindings is passed as a parameter).
21559 If you want to code more such functions, take a close look at the function
21560 labelInRange and the calling mechanism in source file SDCCpeeph.c.
21561 Currently implemented are
21563 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
21564 24bitMode, portIsDS390, 24bitModeAndPortDS390
21573 I know this whole thing is a little kludgey, but maybe some day we will
21574 have some better means.
21575 If you are looking at this file, you will see the default rules that are
21576 compiled into the compiler, you can add your own rules in the default set
21577 there if you get tired of specifying the -
21591 \begin_inset LatexCommand \index{ANSI-compliance}
21596 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
21603 Deviations from the compliance:
21606 functions are not always reentrant
21607 \begin_inset LatexCommand \index{reentrant}
21614 structures cannot be assigned values directly, cannot be passed as function
21615 parameters or assigned to each other and cannot be a return value from
21642 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
21653 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
21675 return rets;/* is invalid in SDCC although allowed in ANSI */
21682 \begin_inset LatexCommand \index{long long (not supported)}
21687 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
21695 \begin_inset LatexCommand \index{double (not supported)}
21699 ' precision floating point
21700 \begin_inset LatexCommand \index{Floating point support}
21707 No support for setjmp
21708 \begin_inset LatexCommand \index{setjmp (not supported)}
21713 \begin_inset LatexCommand \index{longjmp (not supported)}
21721 \begin_inset LatexCommand \index{K\&R style}
21725 function declarations are NOT allowed.
21731 foo(i,j) /* this old style of function declarations */
21733 int i,j; /* are valid in ANSI but not valid in SDCC */
21748 Cyclomatic Complexity
21749 \begin_inset LatexCommand \index{Cyclomatic complexity}
21756 Cyclomatic complexity of a function is defined as the number of independent
21757 paths the program can take during execution of the function.
21758 This is an important number since it defines the number test cases you
21759 have to generate to validate the function.
21760 The accepted industry standard for complexity number is 10, if the cyclomatic
21761 complexity reported by SDCC exceeds 10 you should think about simplification
21762 of the function logic.
21763 Note that the complexity level is not related to the number of lines of
21764 code in a function.
21765 Large functions can have low complexity, and small functions can have large
21771 SDCC uses the following formula to compute the complexity:
21776 complexity = (number of edges in control flow graph) - (number of nodes
21777 in control flow graph) + 2;
21781 Having said that the industry standard is 10, you should be aware that in
21782 some cases it be may unavoidable to have a complexity level of less than
21784 For example if you have switch statement with more than 10 case labels,
21785 each case label adds one to the complexity level.
21786 The complexity level is by no means an absolute measure of the algorithmic
21787 complexity of the function, it does however provide a good starting point
21788 for which functions you might look at for further optimization.
21791 Retargetting for other Processors
21794 The issues for retargetting the compiler are far too numerous to be covered
21796 What follows is a brief description of each of the seven phases of the
21797 compiler and its MCU dependency.
21800 Parsing the source and building the annotated parse tree.
21801 This phase is largely MCU independent (except for the language extensions).
21802 Syntax & semantic checks are also done in this phase, along with some initial
21803 optimizations like back patching labels and the pattern matching optimizations
21804 like bit-rotation etc.
21807 The second phase involves generating an intermediate code which can be easy
21808 manipulated during the later phases.
21809 This phase is entirely MCU independent.
21810 The intermediate code generation assumes the target machine has unlimited
21811 number of registers, and designates them with the name iTemp.
21812 The compiler can be made to dump a human readable form of the code generated
21826 This phase does the bulk of the standard optimizations and is also MCU independe
21828 This phase can be broken down into several sub-phases:
21832 Break down intermediate code (iCode) into basic blocks.
21834 Do control flow & data flow analysis on the basic blocks.
21836 Do local common subexpression elimination, then global subexpression elimination
21838 Dead code elimination
21842 If loop optimizations caused any changes then do 'global subexpression eliminati
21843 on' and 'dead code elimination' again.
21846 This phase determines the live-ranges; by live range I mean those iTemp
21847 variables defined by the compiler that still survive after all the optimization
21849 Live range analysis
21850 \begin_inset LatexCommand \index{Live range analysis}
21854 is essential for register allocation, since these computation determines
21855 which of these iTemps will be assigned to registers, and for how long.
21858 Phase five is register allocation.
21859 There are two parts to this process.
21863 The first part I call 'register packing' (for lack of a better term).
21864 In this case several MCU specific expression folding is done to reduce
21869 The second part is more MCU independent and deals with allocating registers
21870 to the remaining live ranges.
21871 A lot of MCU specific code does creep into this phase because of the limited
21872 number of index registers available in the 8051.
21875 The Code generation phase is (unhappily), entirely MCU dependent and very
21876 little (if any at all) of this code can be reused for other MCU.
21877 However the scheme for allocating a homogenized assembler operand for each
21878 iCode operand may be reused.
21881 As mentioned in the optimization section the peep-hole optimizer is rule
21882 based system, which can reprogrammed for other MCUs.
21886 \begin_inset LatexCommand \index{Compiler internals}
21893 The anatomy of the compiler
21894 \begin_inset LatexCommand \label{sub:The-anatomy-of}
21903 This is an excerpt from an article published in Circuit Cellar Magazine
21905 It's a little outdated (the compiler is much more efficient now and user/develo
21906 per friendly), but pretty well exposes the guts of it all.
21912 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
21913 It is fairly easy to retarget for other 8-bit MCU.
21914 Here we take a look at some of the internals of the compiler.
21919 \begin_inset LatexCommand \index{Parsing}
21926 Parsing the input source file and creating an AST (Annotated Syntax Tree
21927 \begin_inset LatexCommand \index{Annotated syntax tree}
21932 This phase also involves propagating types (annotating each node of the
21933 parse tree with type information) and semantic analysis.
21934 There are some MCU specific parsing rules.
21935 For example the storage classes, the extended storage classes are MCU specific
21936 while there may be a xdata storage class for 8051 there is no such storage
21937 class for z80 or Atmel AVR.
21938 SDCC allows MCU specific storage class extensions, i.e.
21939 xdata will be treated as a storage class specifier when parsing 8051 C
21940 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
21945 \begin_inset LatexCommand \index{iCode}
21952 Intermediate code generation.
21953 In this phase the AST is broken down into three-operand form (iCode).
21954 These three operand forms are represented as doubly linked lists.
21955 ICode is the term given to the intermediate form generated by the compiler.
21956 ICode example section shows some examples of iCode generated for some simple
21957 C source functions.
21961 \begin_inset LatexCommand \index{Optimizations}
21968 Bulk of the target independent optimizations is performed in this phase.
21969 The optimizations include constant propagation, common sub-expression eliminati
21970 on, loop invariant code movement, strength reduction of loop induction variables
21971 and dead-code elimination.
21974 Live range analysis
21975 \begin_inset LatexCommand \index{Live range analysis}
21982 During intermediate code generation phase, the compiler assumes the target
21983 machine has infinite number of registers and generates a lot of temporary
21985 The live range computation determines the lifetime of each of these compiler-ge
21986 nerated temporaries.
21987 A picture speaks a thousand words.
21988 ICode example sections show the live range annotations for each of the
21990 It is important to note here, each iCode is assigned a number in the order
21991 of its execution in the function.
21992 The live ranges are computed in terms of these numbers.
21993 The from number is the number of the iCode which first defines the operand
21994 and the to number signifies the iCode which uses this operand last.
21997 Register Allocation
21998 \begin_inset LatexCommand \index{Register allocation}
22005 The register allocation determines the type and number of registers needed
22007 In most MCUs only a few registers can be used for indirect addressing.
22008 In case of 8051 for example the registers R0 & R1 can be used to indirectly
22009 address the internal ram and DPTR to indirectly address the external ram.
22010 The compiler will try to allocate the appropriate register to pointer variables
22012 ICode example section shows the operands annotated with the registers assigned
22014 The compiler will try to keep operands in registers as much as possible;
22015 there are several schemes the compiler uses to do achieve this.
22016 When the compiler runs out of registers the compiler will check to see
22017 if there are any live operands which is not used or defined in the current
22018 basic block being processed, if there are any found then it will push that
22019 operand and use the registers in this block, the operand will then be popped
22020 at the end of the basic block.
22024 There are other MCU specific considerations in this phase.
22025 Some MCUs have an accumulator; very short-lived operands could be assigned
22026 to the accumulator instead of a general-purpose register.
22032 Figure II gives a table of iCode operations supported by the compiler.
22033 The code generation involves translating these operations into corresponding
22034 assembly code for the processor.
22035 This sounds overly simple but that is the essence of code generation.
22036 Some of the iCode operations are generated on a MCU specific manner for
22037 example, the z80 port does not use registers to pass parameters so the
22038 SEND and RECV iCode operations will not be generated, and it also does
22039 not support JUMPTABLES.
22046 <Where is Figure II?>
22049 In the original article Figure II was announced to be downloadable on
22054 Unfortunately it never seemed to have shown up there, so: where is Figure
22059 \begin_inset LatexCommand \index{iCode}
22066 This section shows some details of iCode.
22067 The example C code does not do anything useful; it is used as an example
22068 to illustrate the intermediate code generated by the compiler.
22080 /* This function does nothing useful.
22087 for the purpose of explaining iCode */
22090 short function (data int *x)
22098 short i=10; \SpecialChar ~
22100 /* dead initialization eliminated */
22105 short sum=10; /* dead initialization eliminated */
22118 while (*x) *x++ = *p++;
22132 /* compiler detects i,j to be induction variables */
22136 for (i = 0, j = 10 ; i < 10 ; i++, j
22162 mul += i * 3; \SpecialChar ~
22164 /* this multiplication remains */
22170 gint += j * 3;\SpecialChar ~
22172 /* this multiplication changed to addition */
22186 In addition to the operands each iCode contains information about the filename
22187 and line it corresponds to in the source file.
22188 The first field in the listing should be interpreted as follows:
22193 Filename(linenumber: iCode Execution sequence number : ICode hash table
22194 key : loop depth of the iCode).
22199 Then follows the human readable form of the ICode operation.
22200 Each operand of this triplet form can be of three basic types a) compiler
22201 generated temporary b) user defined variable c) a constant value.
22202 Note that local variables and parameters are replaced by compiler generated
22205 \begin_inset LatexCommand \index{Live range analysis}
22209 are computed only for temporaries (i.e.
22210 live ranges are not computed for global variables).
22212 \begin_inset LatexCommand \index{Register allocation}
22216 are allocated for temporaries only.
22217 Operands are formatted in the following manner:
22222 Operand Name [lr live-from : live-to ] { type information } [ registers
22228 As mentioned earlier the live ranges are computed in terms of the execution
22229 sequence number of the iCodes, for example
22231 the iTemp0 is live from (i.e.
22232 first defined in iCode with execution sequence number 3, and is last used
22233 in the iCode with sequence number 5).
22234 For induction variables such as iTemp21 the live range computation extends
22235 the lifetime from the start to the end of the loop.
22237 The register allocator used the live range information to allocate registers,
22238 the same registers may be used for different temporaries if their live
22239 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
22240 iTemp17 since their live ranges do not overlap.
22241 In addition the allocator also takes into consideration the type and usage
22242 of a temporary, for example itemp6 is a pointer to near space and is used
22243 as to fetch data from (i.e.
22244 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
22245 Some short lived temporaries are allocated to special registers which have
22246 meaning to the code generator e.g.
22247 iTemp13 is allocated to a pseudo register CC which tells the back end that
22248 the temporary is used only for a conditional jump the code generation makes
22249 use of this information to optimize a compare and jump ICode.
22251 There are several loop optimizations
22252 \begin_inset LatexCommand \index{Loop optimization}
22256 performed by the compiler.
22257 It can detect induction variables iTemp21(i) and iTemp23(j).
22258 Also note the compiler does selective strength reduction
22259 \begin_inset LatexCommand \index{Strength reduction}
22264 the multiplication of an induction variable in line 18 (gint = j * 3) is
22265 changed to addition, a new temporary iTemp17 is allocated and assigned
22266 a initial value, a constant 3 is then added for each iteration of the loop.
22267 The compiler does not change the multiplication
22268 \begin_inset LatexCommand \index{Multiplication}
22272 in line 17 however since the processor does support an 8 * 8 bit multiplication.
22274 Note the dead code elimination
22275 \begin_inset LatexCommand \index{Dead-code elimination}
22279 optimization eliminated the dead assignments in line 7 & 8 to I and sum
22287 Sample.c (5:1:0:0) _entry($9) :
22292 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
22297 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
22302 Sample.c(11:4:53:0) preHeaderLbl0($11) :
22307 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
22313 Sample.c(11:6:5:1) _whilecontinue_0($1) :
22318 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
22324 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22329 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
22335 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
22341 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
22347 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
22353 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
22354 * int}[r0] + 0x2 {short}
22359 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
22364 Sample.c(11:17:21:0)_whilebreak_0($3) :
22369 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
22374 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
22379 Sample.c(15:20:54:0)preHeaderLbl1($13) :
22384 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
22389 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
22394 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
22399 Sample.c(15:24:26:1)_forcond_0($4) :
22404 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
22410 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
22415 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
22416 + ITemp21 [lr21:38]{short}[r4]
22421 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
22427 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
22428 + iTemp15 [lr29:30]{short}[r1]
22433 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
22439 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
22445 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
22451 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
22457 Sample.c(19:38:47:1) goto _forcond_0($4)
22462 Sample.c(19:39:48:0)_forbreak_0($7) :
22467 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
22468 + ITemp11 [lr19:40]{short}[r3]
22473 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
22478 Sample.c(20:42:51:0)_return($8) :
22483 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
22489 Finally the code generated for this function:
22530 ; ----------------------------------------------
22535 ; function function
22540 ; ----------------------------------------------
22550 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
22562 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
22574 ;_whilecontinue_0($1) :
22584 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
22589 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22648 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
22667 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
22714 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
22754 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
22780 ; iTemp6 [lr5:16]{_near * int}[r0] =
22785 ; iTemp6 [lr5:16]{_near * int}[r0] +
22802 ; goto _whilecontinue_0($1)
22814 ; _whilebreak_0($3) :
22824 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
22836 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
22848 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
22860 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
22879 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
22908 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
22913 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
22958 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
22963 ; iTemp21 [lr21:38]{short}[r4]
22989 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
23022 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
23027 ; iTemp15 [lr29:30]{short}[r1]
23046 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
23093 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
23140 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
23152 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
23166 cjne r5,#0xff,00104$
23178 ; goto _forcond_0($4)
23190 ; _forbreak_0($7) :
23200 ; ret iTemp24 [lr40:41]{short}
23243 A few words about basic block successors, predecessors and dominators
23246 Successors are basic blocks
23247 \begin_inset LatexCommand \index{Basic blocks}
23251 that might execute after this basic block.
23253 Predecessors are basic blocks that might execute before reaching this basic
23256 Dominators are basic blocks that WILL execute before reaching this basic
23290 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
23293 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
23296 c) domVect of [BB4] = BB1 ...
23297 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
23305 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
23315 Thanks to all the other volunteer developers who have helped with coding,
23316 testing, web-page creation, distribution sets, etc.
23317 You know who you are :-)
23324 This document was initially written by Sandeep Dutta
23327 All product names mentioned herein may be trademarks
23328 \begin_inset LatexCommand \index{Trademarks}
23332 of their respective companies.
23339 To avoid confusion, the installation and building options for SDCC itself
23340 (chapter 2) are not part of the index.
23344 \begin_inset LatexCommand \printindex{}