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 to use unused register banks for data variables and pack
6047 data, idata and stack together.
6048 This is the default now.
6050 \labelwidthstring 00.00.0000
6068 \begin_inset LatexCommand \index{-\/-pack-iram}
6072 Causes the linker to use old style for allocating memory areas.
6075 DS390 / DS400 Options
6076 \begin_inset LatexCommand \index{Options DS390}
6081 \begin_inset LatexCommand \index{DS390 options}
6087 \labelwidthstring 00.00.0000
6104 \begin_inset LatexCommand \index{-\/-model-flat24}
6114 Generate 24-bit flat mode code.
6115 This is the one and only that the ds390 code generator supports right now
6116 and is default when using
6121 See section Memory Models for more details.
6123 \labelwidthstring 00.00.0000
6138 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6144 disable interrupts during ESP:SP updates.
6146 \labelwidthstring 00.00.0000
6163 \begin_inset LatexCommand \index{-\/-stack-10bit}
6167 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6168 This is the one and only that the ds390 code generator supports right now
6169 and is default when using
6174 In this mode, the stack is located in the lower 1K of the internal RAM,
6175 which is mapped to 0x400000.
6176 Note that the support is incomplete, since it still uses a single byte
6177 as the stack pointer.
6178 This means that only the lower 256 bytes of the potential 1K stack space
6179 will actually be used.
6180 However, this does allow you to reclaim the precious 256 bytes of low RAM
6181 for use for the DATA and IDATA segments.
6182 The compiler will not generate any code to put the processor into 10 bit
6184 It is important to ensure that the processor is in this mode before calling
6185 any re-entrant functions compiled with this option.
6186 In principle, this should work with the
6199 \begin_inset LatexCommand \index{-\/-stack-auto}
6205 option, but that has not been tested.
6206 It is incompatible with the
6219 \begin_inset LatexCommand \index{-\/-xstack}
6226 It also only makes sense if the processor is in 24 bit contiguous addressing
6239 -model-flat24 option
6243 \labelwidthstring 00.00.0000
6258 \begin_inset LatexCommand \index{-\/-stack-probe}
6264 insert call to function __stack_probe at each function prologue.
6266 \labelwidthstring 00.00.0000
6281 \begin_inset LatexCommand \index{-\/-tini-libid}
6287 <nnnn> LibraryID used in -mTININative.
6290 \labelwidthstring 00.00.0000
6305 \begin_inset LatexCommand \index{-\/-use-accelerator}
6311 generate code for DS390 Arithmetic Accelerator.
6316 \begin_inset LatexCommand \index{Options Z80}
6321 \begin_inset LatexCommand \index{Z80 options}
6327 \labelwidthstring 00.00.0000
6344 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6354 Force a called function to always save BC.
6356 \labelwidthstring 00.00.0000
6373 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6377 When linking, skip the standard crt0.o object file.
6378 You must provide your own crt0.o for your system when linking.
6382 Optimization Options
6383 \begin_inset LatexCommand \index{Options optimization}
6388 \begin_inset LatexCommand \index{Optimization options}
6394 \labelwidthstring 00.00.0000
6409 \begin_inset LatexCommand \index{-\/-nogcse}
6415 Will not do global subexpression elimination, this option may be used when
6416 the compiler creates undesirably large stack/data spaces to store compiler
6418 A warning message will be generated when this happens and the compiler
6419 will indicate the number of extra bytes it allocated.
6420 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6422 \begin_inset LatexCommand \index{\#pragma nogcse}
6426 can be used to turn off global subexpression elimination
6427 \begin_inset LatexCommand \index{Subexpression elimination}
6431 for a given function only.
6433 \labelwidthstring 00.00.0000
6448 \begin_inset LatexCommand \index{-\/-noinvariant}
6454 Will not do loop invariant optimizations, this may be turned off for reasons
6455 explained for the previous option.
6456 For more details of loop optimizations performed see Loop Invariants in
6458 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6463 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6465 \begin_inset LatexCommand \index{\#pragma noinvariant}
6469 can be used to turn off invariant optimizations for a given function only.
6471 \labelwidthstring 00.00.0000
6486 \begin_inset LatexCommand \index{-\/-noinduction}
6492 Will not do loop induction optimizations, see section strength reduction
6494 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6496 \begin_inset LatexCommand \index{\#pragma noinduction}
6500 can be used to turn off induction optimizations for a given function only.
6502 \labelwidthstring 00.00.0000
6517 \begin_inset LatexCommand \index{-\/-nojtbound}
6528 Will not generate boundary condition check when switch statements
6529 \begin_inset LatexCommand \index{switch statement}
6533 are implemented using jump-tables.
6535 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6540 Switch Statements for more details.
6541 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6543 \begin_inset LatexCommand \index{\#pragma nojtbound}
6547 can be used to turn off boundary checking for jump tables for a given function
6550 \labelwidthstring 00.00.0000
6565 \begin_inset LatexCommand \index{-\/-noloopreverse}
6574 Will not do loop reversal
6575 \begin_inset LatexCommand \index{Loop reversing}
6581 \labelwidthstring 00.00.0000
6598 \begin_inset LatexCommand \index{-\/-nolabelopt }
6602 Will not optimize labels (makes the dumpfiles more readable).
6604 \labelwidthstring 00.00.0000
6619 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6625 Will not memcpy initialized data from code space into xdata space.
6626 This saves a few bytes in code space if you don't have initialized data.
6628 \labelwidthstring 00.00.0000
6643 \begin_inset LatexCommand \index{-\/-nooverlay}
6649 The compiler will not overlay parameters and local variables of any function,
6650 see section Parameters and local variables for more details.
6652 \labelwidthstring 00.00.0000
6667 \begin_inset LatexCommand \index{-\/-no-peep}
6673 Disable peep-hole optimization.
6675 \labelwidthstring 00.00.0000
6692 \begin_inset LatexCommand \index{-\/-peep-file}
6697 <filename> This option can be used to use additional rules to be used by
6698 the peep hole optimizer.
6700 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6705 Peep Hole optimizations for details on how to write these rules.
6707 \labelwidthstring 00.00.0000
6722 \begin_inset LatexCommand \index{-\/-peep-asm}
6728 Pass the inline assembler code through the peep hole optimizer.
6729 This can cause unexpected changes to inline assembler code, please go through
6730 the peephole optimizer
6731 \begin_inset LatexCommand \index{Peephole optimizer}
6735 rules defined in the source file tree '<target>/peeph.def' before using
6738 \labelwidthstring 00.00.0000
6753 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6759 The compiler will optimize code generation towards fast code, possibly
6760 at the expense of code size.
6762 \labelwidthstring 00.00.0000
6777 \begin_inset LatexCommand \index{-\/-opt-code-size}
6783 The compiler will optimize code generation towards compact code, possibly
6784 at the expense of code speed.
6788 \begin_inset LatexCommand \index{Options other}
6794 \labelwidthstring 00.00.0000
6810 \begin_inset LatexCommand \index{-\/-compile-only}
6815 \begin_inset LatexCommand \index{-c -\/-compile-only}
6821 will compile and assemble the source, but will not call the linkage editor.
6823 \labelwidthstring 00.00.0000
6842 \begin_inset LatexCommand \index{-\/-c1mode}
6848 reads the preprocessed source from standard input and compiles it.
6849 The file name for the assembler output must be specified using the -o option.
6851 \labelwidthstring 00.00.0000
6856 \begin_inset LatexCommand \index{-E}
6862 Run only the C preprocessor.
6863 Preprocess all the C source files specified and output the results to standard
6866 \labelwidthstring 00.00.0000
6872 \begin_inset LatexCommand \index{-o <path/file>}
6878 The output path resp.
6879 file where everything will be placed.
6880 If the parameter is a path, it must have a trailing slash (or backslash
6881 for the Windows binaries) to be recognized as a path.
6884 \labelwidthstring 00.00.0000
6899 \begin_inset LatexCommand \index{-\/-stack-auto}
6910 All functions in the source file will be compiled as
6915 \begin_inset LatexCommand \index{reentrant}
6920 the parameters and local variables will be allocated on the stack
6921 \begin_inset LatexCommand \index{stack}
6927 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
6931 Parameters and Local Variables for more details.
6932 If this option is used all source files in the project should be compiled
6934 It automatically implies --int-long-reent and --float-reent.
6937 \labelwidthstring 00.00.0000
6952 \begin_inset LatexCommand \index{-\/-callee-saves}
6956 function1[,function2][,function3]....
6959 The compiler by default uses a caller saves convention for register saving
6960 across function calls, however this can cause unnecessary register pushing
6961 & popping when calling small functions from larger functions.
6962 This option can be used to switch the register saving convention for the
6963 function names specified.
6964 The compiler will not save registers when calling these functions, no extra
6965 code will be generated at the entry & exit (function prologue
6968 \begin_inset LatexCommand \index{function prologue}
6977 \begin_inset LatexCommand \index{function epilogue}
6983 ) for these functions to save & restore the registers used by these functions,
6984 this can SUBSTANTIALLY reduce code & improve run time performance of the
6986 In the future the compiler (with inter procedural analysis) will be able
6987 to determine the appropriate scheme to use for each function call.
6988 DO NOT use this option for built-in functions such as _mulint..., if this
6989 option is used for a library function the appropriate library function
6990 needs to be recompiled with the same option.
6991 If the project consists of multiple source files then all the source file
6992 should be compiled with the same -
7002 -callee-saves option string.
7003 Also see #pragma\SpecialChar ~
7005 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7011 \labelwidthstring 00.00.0000
7026 \begin_inset LatexCommand \index{-\/-debug}
7035 When this option is used the compiler will generate debug information.
7036 The debug information collected in a file with .cdb extension can be used
7038 For more information see documentation for SDCDB.
7039 Another file with no extension contains debug information in AOMF or AOMF51
7040 \begin_inset LatexCommand \index{AOMF, AOMF51}
7044 format which is commonly used by third party tools.
7046 \labelwidthstring 00.00.0000
7051 \begin_inset LatexCommand \index{-S}
7062 Stop after the stage of compilation proper; do not assemble.
7063 The output is an assembler code file for the input file specified.
7065 \labelwidthstring 00.00.0000
7080 \begin_inset LatexCommand \index{-\/-int-long-reent}
7086 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7087 Note by default these libraries are compiled as non-reentrant.
7088 See section Installation for more details.
7090 \labelwidthstring 00.00.0000
7105 \begin_inset LatexCommand \index{-\/-cyclomatic}
7114 This option will cause the compiler to generate an information message for
7115 each function in the source file.
7116 The message contains some
7120 information about the function.
7121 The number of edges and nodes the compiler detected in the control flow
7122 graph of the function, and most importantly the
7124 cyclomatic complexity
7125 \begin_inset LatexCommand \index{Cyclomatic complexity}
7131 see section on Cyclomatic Complexity for more details.
7133 \labelwidthstring 00.00.0000
7148 \begin_inset LatexCommand \index{-\/-float-reent}
7154 Floating point library is compiled as reentrant
7155 \begin_inset LatexCommand \index{reentrant}
7160 See section Installation for more details.
7162 \labelwidthstring 00.00.0000
7177 \begin_inset LatexCommand \index{-\/-main-return}
7183 This option can be used if the code generated is called by a monitor program
7184 or if the main routine includes an endless loop.
7185 This option might result in slightly smaller code and save two bytes of
7187 The return from the 'main'
7188 \begin_inset LatexCommand \index{main return}
7192 function will return to the function calling main.
7193 The default setting is to lock up i.e.
7200 \labelwidthstring 00.00.0000
7215 \begin_inset LatexCommand \index{-\/-nostdinc}
7221 This will prevent the compiler from passing on the default include path
7222 to the preprocessor.
7224 \labelwidthstring 00.00.0000
7239 \begin_inset LatexCommand \index{-\/-nostdlib}
7245 This will prevent the compiler from passing on the default library
7246 \begin_inset LatexCommand \index{Libraries}
7252 \labelwidthstring 00.00.0000
7267 \begin_inset LatexCommand \index{-\/-verbose}
7273 Shows the various actions the compiler is performing.
7275 \labelwidthstring 00.00.0000
7280 \begin_inset LatexCommand \index{-V}
7286 Shows the actual commands the compiler is executing.
7288 \labelwidthstring 00.00.0000
7303 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7309 Hides your ugly and inefficient c-code from the asm file, so you can always
7310 blame the compiler :)
7312 \labelwidthstring 00.00.0000
7327 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7333 Will not include peep-hole comments in the generated files.
7335 \labelwidthstring 00.00.0000
7350 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7356 Include i-codes in the asm file.
7357 Sounds like noise but is most helpful for debugging the compiler itself.
7359 \labelwidthstring 00.00.0000
7374 \begin_inset LatexCommand \index{-\/-less-pedantic}
7380 Disable some of the more pedantic warnings
7381 \begin_inset LatexCommand \index{Warnings}
7385 (jwk burps: please be more specific here, please!).
7386 If you want rather more than less warnings you should consider using a
7387 separate tool dedicated to syntax checking like splint
7388 \begin_inset LatexCommand \url{www.splint.org}
7394 \labelwidthstring 00.00.0000
7408 -disable-warning\SpecialChar ~
7410 \begin_inset LatexCommand \index{-\/-disable-warning}
7416 Disable specific warning with number <nnnn>.
7418 \labelwidthstring 00.00.0000
7433 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7439 Display the directories in the compiler's search path
7441 \labelwidthstring 00.00.0000
7456 \begin_inset LatexCommand \index{-\/-vc}
7462 Display errors and warnings using MSVC style, so you can use SDCC with
7465 \labelwidthstring 00.00.0000
7480 \begin_inset LatexCommand \index{-\/-use-stdout}
7486 Send errors and warnings to stdout instead of stderr.
7488 \labelwidthstring 00.00.0000
7493 asmOption[,asmOption]
7496 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7501 Pass the asmOption to the assembler
7502 \begin_inset LatexCommand \index{Options assembler}
7507 \begin_inset LatexCommand \index{Assembler options}
7512 See file sdcc/as/doc/asxhtm.html for assembler options.
7515 Intermediate Dump Options
7516 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7521 \begin_inset LatexCommand \index{Options intermediate dump}
7526 \begin_inset LatexCommand \index{Intermediate dump options}
7533 The following options are provided for the purpose of retargetting and debugging
7535 These provided a means to dump the intermediate code (iCode
7536 \begin_inset LatexCommand \index{iCode}
7540 ) generated by the compiler in human readable form at various stages of
7541 the compilation process.
7542 More on iCodes see chapter
7543 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7548 \begin_inset Quotes srd
7551 The anatomy of the compiler
7552 \begin_inset Quotes srd
7557 \labelwidthstring 00.00.0000
7572 \begin_inset LatexCommand \index{-\/-dumpraw}
7578 This option will cause the compiler to dump the intermediate code into
7581 <source filename>.dumpraw
7583 just after the intermediate code has been generated for a function, i.e.
7584 before any optimizations are done.
7586 \begin_inset LatexCommand \index{Basic blocks}
7590 at this stage ordered in the depth first number, so they may not be in
7591 sequence of execution.
7593 \labelwidthstring 00.00.0000
7608 \begin_inset LatexCommand \index{-\/-dumpgcse}
7614 Will create a dump of iCode's, after global subexpression elimination
7615 \begin_inset LatexCommand \index{Global subexpression elimination}
7621 <source filename>.dumpgcse.
7623 \labelwidthstring 00.00.0000
7638 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7644 Will create a dump of iCode's, after deadcode elimination
7645 \begin_inset LatexCommand \index{Dead-code elimination}
7651 <source filename>.dumpdeadcode.
7653 \labelwidthstring 00.00.0000
7668 \begin_inset LatexCommand \index{-\/-dumploop}
7677 Will create a dump of iCode's, after loop optimizations
7678 \begin_inset LatexCommand \index{Loop optimization}
7684 <source filename>.dumploop.
7686 \labelwidthstring 00.00.0000
7701 \begin_inset LatexCommand \index{-\/-dumprange}
7710 Will create a dump of iCode's, after live range analysis
7711 \begin_inset LatexCommand \index{Live range analysis}
7717 <source filename>.dumprange.
7719 \labelwidthstring 00.00.0000
7734 \begin_inset LatexCommand \index{-\/-dumlrange}
7740 Will dump the life ranges
7741 \begin_inset LatexCommand \index{Live range analysis}
7747 \labelwidthstring 00.00.0000
7762 \begin_inset LatexCommand \index{-\/-dumpregassign}
7771 Will create a dump of iCode's, after register assignment
7772 \begin_inset LatexCommand \index{Register assignment}
7778 <source filename>.dumprassgn.
7780 \labelwidthstring 00.00.0000
7795 \begin_inset LatexCommand \index{-\/-dumplrange}
7801 Will create a dump of the live ranges of iTemp's
7803 \labelwidthstring 00.00.0000
7818 \begin_inset LatexCommand \index{-\/-dumpall}
7829 Will cause all the above mentioned dumps to be created.
7832 Redirecting output on Windows Shells
7835 By default SDCC writes it's error messages to
7836 \begin_inset Quotes sld
7840 \begin_inset Quotes srd
7844 To force all messages to
7845 \begin_inset Quotes sld
7849 \begin_inset Quotes srd
7873 \begin_inset LatexCommand \index{-\/-use-stdout}
7878 Additionally, if you happen to have visual studio installed in your windows
7879 machine, you can use it to compile your sources using a custom build and
7895 \begin_inset LatexCommand \index{-\/-vc}
7900 Something like this should work:
7944 -model-large -c $(InputPath)
7947 Environment variables
7948 \begin_inset LatexCommand \index{Environment variables}
7955 SDCC recognizes the following environment variables:
7957 \labelwidthstring 00.00.0000
7962 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
7968 SDCC installs a signal handler
7969 \begin_inset LatexCommand \index{signal handler}
7973 to be able to delete temporary files after an user break (^C) or an exception.
7974 If this environment variable is set, SDCC won't install the signal handler
7975 in order to be able to debug SDCC.
7977 \labelwidthstring 00.00.0000
7984 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
7990 Path, where temporary files will be created.
7991 The order of the variables is the search order.
7992 In a standard *nix environment these variables are not set, and there's
7993 no need to set them.
7994 On Windows it's recommended to set one of them.
7996 \labelwidthstring 00.00.0000
8001 \begin_inset LatexCommand \index{SDCC\_HOME}
8008 \begin_inset LatexCommand \ref{sub:Install-paths}
8014 \begin_inset Quotes sld
8018 \begin_inset Quotes srd
8023 \labelwidthstring 00.00.0000
8028 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8035 \begin_inset LatexCommand \ref{sub:Search-Paths}
8041 \begin_inset Quotes sld
8045 \begin_inset Quotes srd
8050 \labelwidthstring 00.00.0000
8055 \begin_inset LatexCommand \index{SDCC\_LIB}
8062 \begin_inset LatexCommand \ref{sub:Search-Paths}
8068 \begin_inset Quotes sld
8072 \begin_inset Quotes srd
8078 There are some more environment variables recognized by SDCC, but these
8079 are solely used for debugging purposes.
8080 They can change or disappear very quickly, and will never be documented.
8083 Storage Class Language Extensions
8086 MCS51/DS390 Storage Class
8087 \begin_inset LatexCommand \index{Storage class}
8094 In addition to the ANSI storage classes SDCC allows the following MCS51
8095 specific storage classes:
8096 \layout Subsubsection
8099 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8104 \begin_inset LatexCommand \index{near (storage class)}
8115 storage class for the Small Memory model (
8123 can be used synonymously).
8124 Variables declared with this storage class will be allocated in the directly
8125 addressable portion of the internal RAM of a 8051, e.g.:
8130 data unsigned char test_data;
8133 Writing 0x01 to this variable generates the assembly code:
8138 75*00 01\SpecialChar ~
8144 \layout Subsubsection
8147 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8152 \begin_inset LatexCommand \index{far (storage class)}
8159 Variables declared with this storage class will be placed in the external
8165 storage class for the Large Memory model, e.g.:
8170 xdata unsigned char test_xdata;
8173 Writing 0x01 to this variable generates the assembly code:
8178 90s00r00\SpecialChar ~
8207 \layout Subsubsection
8210 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8217 Variables declared with this storage class will be allocated into the indirectly
8218 addressable portion of the internal ram of a 8051, e.g.:
8223 idata unsigned char test_idata;
8226 Writing 0x01 to this variable generates the assembly code:
8255 Please note, the first 128 byte of idata physically access the same RAM
8257 The original 8051 had 128 byte idata memory, nowadays most devices have
8258 256 byte idata memory.
8260 \begin_inset LatexCommand \index{stack}
8264 is located in idata memory.
8265 \layout Subsubsection
8268 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8275 Paged xdata access is just as straightforward as using the other addressing
8277 It is typically located at the start of xdata and has a maximum size of
8279 The following example writes 0x01 to the address pointed to.
8280 Please note, pdata access physically accesses xdata memory.
8281 The high byte of the address is determined by port P2
8282 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8286 (or in case of some 8051 variants by a separate Special Function Register,
8288 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8297 pdata unsigned char *test_pdata_ptr;
8309 test_pdata_ptr = (pdata *)0xfe;
8315 *test_pdata_ptr = 1;
8320 Generates the assembly code:
8325 75*01 FE\SpecialChar ~
8329 _test_pdata_ptr,#0xFE
8372 \begin_inset LatexCommand \index{-\/-xstack}
8376 option is used the pdata memory area is followed by the xstack memory area
8377 and the sum of their sizes is limited to 256 bytes.
8378 \layout Subsubsection
8381 \begin_inset LatexCommand \index{code}
8388 'Variables' declared with this storage class will be placed in the code
8394 code unsigned char test_code;
8397 Read access to this variable generates the assembly code:
8402 90s00r6F\SpecialChar ~
8405 mov dptr,#_test_code
8434 indexed arrays of characters in code memory can be accessed efficiently:
8439 code char test_array[] = {'c','h','e','a','p'};
8442 Read access to this array using an 8-bit unsigned index generates the assembly
8459 90s00r41\SpecialChar ~
8462 mov dptr,#_test_array
8477 \layout Subsubsection
8480 \begin_inset LatexCommand \index{bit}
8487 This is a data-type and a storage class specifier.
8488 When a variable is declared as a bit, it is allocated into the bit addressable
8489 memory of 8051, e.g.:
8497 Writing 1 to this variable generates the assembly code:
8513 The bit addressable memory consists of 128 bits which are located from 0x20
8514 to 0x2f in data memory.
8517 Apart from this 8051 specific storage class most architectures support ANSI-C
8519 \begin_inset LatexCommand \index{bitfields}
8529 Not really meant as examples, but nevertheless showing what bitfields are
8530 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8534 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8535 signed modifier are implemented as unsigned.
8536 \layout Subsubsection
8539 \begin_inset LatexCommand \index{sfr}
8544 \begin_inset LatexCommand \index{sbit}
8551 Like the bit keyword,
8555 signifies both a data-type and storage class, they are used to describe
8576 variables of a 8051, eg:
8582 \begin_inset LatexCommand \index{at}
8586 0x80 P0;\SpecialChar ~
8587 /* special function register P0 at location 0x80 */
8589 sbit at 0xd7 CY; /* CY (Carry Flag
8590 \begin_inset LatexCommand \index{Flags}
8595 \begin_inset LatexCommand \index{Carry flag}
8602 Special function registers which are located on an address dividable by
8603 8 are bit-addressable, an
8607 addresses a specific bit within these sfr.
8608 \layout Subsubsection
8611 \begin_inset LatexCommand \index{Pointer}
8615 to MCS51/DS390 specific memory spaces
8618 SDCC allows (via language extensions) pointers to explicitly point to any
8619 of the memory spaces
8620 \begin_inset LatexCommand \index{Memory model}
8625 In addition to the explicit pointers, the compiler uses (by default) generic
8626 pointers which can be used to point to any of the memory spaces.
8630 Pointer declaration examples:
8635 /* pointer physically in internal ram pointing to object in external ram
8638 xdata unsigned char * data p;
8642 /* pointer physically in external ram pointing to object in internal ram
8645 data unsigned char * xdata p;
8649 /* pointer physically in code rom pointing to data in xdata space */
8651 xdata unsigned char * code p;
8655 /* pointer physically in code space pointing to data in code space */
8657 code unsigned char * code p;
8661 /* the following is a generic pointer physically located in xdata space
8667 Well you get the idea.
8672 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
8685 The highest order byte of the
8689 pointers contains the data space information.
8690 Assembler support routines are called whenever data is stored or retrieved
8696 These are useful for developing reusable library
8697 \begin_inset LatexCommand \index{Libraries}
8702 Explicitly specifying the pointer type will generate the most efficient
8704 \layout Subsubsection
8706 Notes on MCS51 memory
8707 \begin_inset LatexCommand \index{MCS51 memory}
8714 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
8715 RAM memory which is structured as follows:
8719 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
8722 - Bytes 20-2F - 16 bytes to hold 128 bit
8723 \begin_inset LatexCommand \index{bit}
8729 - Bytes 30-7F - 80 bytes for general purpose use.
8734 Additionally some members of the MCS51 family may have up to 128 bytes of
8735 additional, indirectly addressable, internal RAM memory (
8740 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8745 Furthermore, some chips may have some built in external memory (
8750 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8754 ) which should not be confused with the internal, directly addressable RAM
8760 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8765 Sometimes this built in
8769 memory has to be activated before using it (you can probably find this
8770 information on the datasheet of the microcontroller your are using, see
8772 \begin_inset LatexCommand \ref{sub:Startup-Code}
8780 Normally SDCC will only use the first bank
8781 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
8785 of registers (register bank 0), but it is possible to specify that other
8786 banks of registers should be used in interrupt
8787 \begin_inset LatexCommand \index{interrupt}
8792 By default, the compiler will place the stack after the last byte of allocated
8793 memory for variables.
8794 For example, if the first 2 banks of registers are used, and only four
8799 variables, it will position the base of the internal stack at address 20
8801 This implies that as the stack
8802 \begin_inset LatexCommand \index{stack}
8806 grows, it will use up the remaining register banks, and the 16 bytes used
8807 by the 128 bit variables, and 80 bytes for general purpose use.
8808 If any bit variables are used, the data variables will be placed in unused
8809 register banks and after the byte holding the last bit variable.
8810 For example, if register banks 0 and 1 are used, and there are 9 bit variables
8815 variables will be placed starting from address 0x10 to 0x20 and continue
8828 \begin_inset LatexCommand \index{-\/-data-loc<Value>}
8832 to specify the start address of the
8846 -iram-size to specify the size of the total internal RAM (
8858 By default the 8051 linker will place the stack after the last byte of (i)data
8871 \begin_inset LatexCommand \index{-\/-stack-loc<Value>}
8875 allows you to specify the start of the stack, i.e.
8876 you could start it after any data in the general purpose area.
8877 If your microcontroller has additional indirectly addressable internal
8882 ) you can place the stack on it.
8883 You may also need to use -
8894 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
8898 to set the start address of the external RAM (
8913 \begin_inset LatexCommand \index{-\/-data-loc}
8917 to specify its size.
8918 Same goes for the code memory, using -
8929 \begin_inset LatexCommand \index{-\/-data-loc}
8944 \begin_inset LatexCommand \index{-\/-data-loc}
8949 If in doubt, don't specify any options and see if the resulting memory
8950 layout is appropriate, then you can adjust it.
8953 The linker generates two files with memory allocation information.
8954 The first, with extension .map
8955 \begin_inset LatexCommand \index{<file>.map}
8959 shows all the variables and segments.
8960 The second with extension .mem
8961 \begin_inset LatexCommand \index{<file>.mem}
8965 shows the final memory layout.
8966 The linker will complain either if memory segments overlap, there is not
8967 enough memory, or there is not enough space for stack.
8968 If you get any linking warnings and/or errors related to stack or segments
8969 allocation, take a look at either the .map or .mem files to find out what
8971 The .mem file may even suggest a solution to the problem.
8974 Z80/Z180 Storage Class
8975 \begin_inset LatexCommand \index{Storage class}
8980 \layout Subsubsection
8983 \begin_inset LatexCommand \index{sfr}
8987 (in/out to 8-bit addresses)
8991 \begin_inset LatexCommand \index{Z80}
8995 family has separate address spaces for memory and
9005 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9009 is accessed with special instructions, e.g.:
9014 sfr at 0x78 IoPort;\SpecialChar ~
9016 /* define a var in I/O space at 78h called IoPort */
9020 Writing 0x01 to this variable generates the assembly code:
9040 \layout Subsubsection
9043 \begin_inset LatexCommand \index{sfr}
9047 (in/out to 16-bit addresses)
9054 is used to support 16 bit addresses in I/O memory e.g.:
9060 \begin_inset LatexCommand \index{at}
9067 Writing 0x01 to this variable generates the assembly code:
9072 01 23 01\SpecialChar ~
9092 \layout Subsubsection
9095 \begin_inset LatexCommand \index{sfr}
9099 (in0/out0 to 8 bit addresses on Z180
9100 \begin_inset LatexCommand \index{Z180}
9105 \begin_inset LatexCommand \index{HD64180}
9112 The compiler option -
9122 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9124 \begin_inset LatexCommand \index{\#pragma portmode}
9128 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9138 If you include the file z180.h this will be set automatically.
9142 \begin_inset LatexCommand \index{Storage class}
9147 \layout Subsubsection
9150 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9157 The data storage class declares a variable that resides in the first 256
9158 bytes of memory (the direct page).
9159 The HC08 is most efficient at accessing variables (especially pointers)
9161 \layout Subsubsection
9164 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9171 The xdata storage class declares a variable that can reside anywhere in
9173 This is the default if no storage class is specified.
9178 \begin_inset LatexCommand \index{Absolute addressing}
9185 Data items can be assigned an absolute address with the
9188 \begin_inset LatexCommand \index{at}
9194 keyword, in addition to a storage class, e.g.:
9200 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9205 \begin_inset LatexCommand \index{at}
9209 0x7ffe unsigned int chksum;
9212 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9213 of the external ram.
9218 reserve any space for variables declared in this way (they are implemented
9219 with an equate in the assembler).
9220 Thus it is left to the programmer to make sure there are no overlaps with
9221 other variables that are declared without the absolute address.
9222 The assembler listing file (.lst
9223 \begin_inset LatexCommand \index{<file>.lst}
9227 ) and the linker output files (.rst
9228 \begin_inset LatexCommand \index{<file>.rst}
9233 \begin_inset LatexCommand \index{<file>.map}
9237 ) are good places to look for such overlaps.
9238 Variables with an absolute address are
9243 \begin_inset LatexCommand \index{Variable initialization}
9250 In case of memory mapped I/O devices the keyword
9254 has to be used to tell the compiler that accesses might not be removed:
9260 \begin_inset LatexCommand \index{volatile}
9265 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9270 \begin_inset LatexCommand \index{at}
9274 0x8000 unsigned char PORTA_8255;
9277 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9282 \begin_inset LatexCommand \index{Aligned array}
9289 starts at a block (256 byte) boundary
9290 \begin_inset LatexCommand \index{block boundary}
9295 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9301 Absolute addresses can be specified for variables in all storage classes,
9308 \begin_inset LatexCommand \index{bit}
9313 \begin_inset LatexCommand \index{at}
9320 The above example will allocate the variable at offset 0x02 in the bit-addressab
9322 There is no real advantage to assigning absolute addresses to variables
9323 in this manner, unless you want strict control over all the variables allocated.
9324 One possible use would be to write hardware portable code.
9325 For example, if you have a routine that uses one or more of the microcontroller
9326 I/O pins, and such pins are different for two different hardwares, you
9327 can declare the I/O pins in your routine using:
9332 extern volatile bit MOSI;\SpecialChar ~
9336 /* master out, slave in */
9338 extern volatile bit MISO;\SpecialChar ~
9342 /* master in, slave out */
9344 extern volatile bit MCLK;\SpecialChar ~
9352 /* Input and Output of a byte on a 3-wire serial bus.
9357 If needed adapt polarity of clock, polarity of data and bit order
9362 unsigned char spi_io(unsigned char out_byte)
9386 MOSI = out_byte & 0x80;
9416 /* _asm nop _endasm; */\SpecialChar ~
9424 /* for slow peripherals */
9475 Then, someplace in the code for the first hardware you would use
9480 bit at 0x80 MOSI;\SpecialChar ~
9484 /* I/O port 0, bit 0 */
9486 bit at 0x81 MISO;\SpecialChar ~
9490 /* I/O port 0, bit 1 */
9492 bit at 0x82 MCLK;\SpecialChar ~
9496 /* I/O port 0, bit 2 */
9499 Similarly, for the second hardware you would use
9504 bit at 0x83 MOSI;\SpecialChar ~
9508 /* I/O port 0, bit 3 */
9510 bit at 0x91 MISO;\SpecialChar ~
9514 /* I/O port 1, bit 1 */
9517 \begin_inset LatexCommand \index{bit}
9521 at 0x92 MCLK;\SpecialChar ~
9525 /* I/O port 1, bit 2 */
9528 and you can use the same hardware dependent routine without changes, as
9529 for example in a library.
9530 This is somehow similar to sbit, but only one absolute address has to be
9531 specified in the whole project.
9535 \begin_inset LatexCommand \index{Parameters}
9540 \begin_inset LatexCommand \index{function parameter}
9545 \begin_inset LatexCommand \index{local variables}
9550 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9557 Automatic (local) variables and parameters to functions can either be placed
9558 on the stack or in data-space.
9559 The default action of the compiler is to place these variables in the internal
9560 RAM (for small model) or external RAM (for large model).
9561 This in fact makes them similar to
9564 \begin_inset LatexCommand \index{static}
9570 so by default functions are non-reentrant
9571 \begin_inset LatexCommand \index{reentrant}
9580 They can be placed on the stack
9581 \begin_inset LatexCommand \index{stack}
9598 \begin_inset LatexCommand \index{-\/-stack-auto}
9606 #pragma\SpecialChar ~
9610 \begin_inset LatexCommand \index{\#pragma stackauto}
9617 \begin_inset LatexCommand \index{reentrant}
9623 keyword in the function declaration, e.g.:
9628 unsigned char foo(char i) reentrant
9642 Since stack space on 8051 is limited, the
9660 option should be used sparingly.
9661 Note that the reentrant keyword just means that the parameters & local
9662 variables will be allocated to the stack, it
9666 mean that the function is register bank
9667 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9676 \begin_inset LatexCommand \index{local variables}
9680 can be assigned storage classes and absolute
9681 \begin_inset LatexCommand \index{Absolute addressing}
9698 xdata unsigned char i;
9711 \begin_inset LatexCommand \index{at}
9715 0x31 unsigned char j;
9727 In the above example the variable
9731 will be allocated in the external ram,
9735 in bit addressable space and
9754 or when a function is declared as
9758 this should only be done for static variables.
9762 \begin_inset LatexCommand \index{function parameter}
9766 however are not allowed any storage class
9767 \begin_inset LatexCommand \index{Storage class}
9771 , (storage classes for parameters will be ignored), their allocation is
9772 governed by the memory model in use, and the reentrancy options.
9776 \begin_inset LatexCommand \label{sub:Overlaying}
9781 \begin_inset LatexCommand \index{Overlaying}
9789 \begin_inset LatexCommand \index{reentrant}
9793 functions SDCC will try to reduce internal ram space usage by overlaying
9794 parameters and local variables of a function (if possible).
9795 Parameters and local variables
9796 \begin_inset LatexCommand \index{local variables}
9800 of a function will be allocated to an overlayable segment if the function
9803 no other function calls and the function is non-reentrant and the memory
9805 \begin_inset LatexCommand \index{Memory model}
9812 If an explicit storage class
9813 \begin_inset LatexCommand \index{Storage class}
9817 is specified for a local variable, it will NOT be overlayed.
9820 Note that the compiler (not the linkage editor) makes the decision for overlayin
9822 Functions that are called from an interrupt service routine should be preceded
9823 by a #pragma\SpecialChar ~
9825 \begin_inset LatexCommand \index{\#pragma nooverlay}
9829 if they are not reentrant.
9832 Also note that the compiler does not do any processing of inline assembler
9833 code, so the compiler might incorrectly assign local variables and parameters
9834 of a function into the overlay segment if the inline assembler code calls
9835 other c-functions that might use the overlay.
9836 In that case the #pragma\SpecialChar ~
9837 nooverlay should be used.
9840 Parameters and local variables of functions that contain 16 or 32 bit multiplica
9842 \begin_inset LatexCommand \index{Multiplication}
9847 \begin_inset LatexCommand \index{Division}
9851 will NOT be overlayed since these are implemented using external functions,
9860 \begin_inset LatexCommand \index{\#pragma nooverlay}
9866 void set_error(unsigned char errcd)
9882 void some_isr () interrupt
9883 \begin_inset LatexCommand \index{interrupt}
9913 In the above example the parameter
9921 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
9923 not present, this could cause unpredictable runtime behavior when called
9924 from an interrupt service routine.
9925 The #pragma\SpecialChar ~
9926 nooverlay ensures that the parameters and local variables for
9927 the function are NOT overlayed.
9930 Interrupt Service Routines
9931 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
9953 outines to be coded in C, with some extended keywords.
9958 void timer_isr (void) interrupt 1 using 1
9972 The optional number following the
9975 \begin_inset LatexCommand \index{interrupt}
9981 keyword is the interrupt number this routine will service.
9982 When present, the compiler will insert a call to this routine in the interrupt
9983 vector table for the interrupt number specified.
9984 If you have multiple source files in your project, interrupt service routines
9985 can be present in any of them, but a prototype of the isr MUST be present
9986 or included in the file that contains the function
9995 keyword can be used to tell the compiler to use the specified register
9996 bank (8051 specific) when generating code for this function.
10002 Interrupt service routines open the door for some very interesting bugs:
10004 If an interrupt service routine changes variables which are accessed by
10005 other functions these variables have to be declared
10010 \begin_inset LatexCommand \index{volatile}
10018 If the access to these variables is not
10021 \begin_inset LatexCommand \index{atomic}
10028 the processor needs more than one instruction for the access and could
10029 be interrupted while accessing the variable) the interrupt must be disabled
10030 during the access to avoid inconsistent data.
10031 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10032 and should be protected by disabling interrupts.
10033 You're not automatically on the safe side if you use 8 bit variables though.
10034 We need an example here: f.e.
10035 on the 8051 the harmless looking
10036 \begin_inset Quotes srd
10041 flags\SpecialChar ~
10046 \begin_inset Quotes sld
10055 \begin_inset Quotes srd
10060 flags\SpecialChar ~
10065 \begin_inset Quotes sld
10068 from within an interrupt routine might get lost if the interrupt occurs
10071 \begin_inset Quotes sld
10076 counter\SpecialChar ~
10081 \begin_inset Quotes srd
10084 is not atomic on the 8051 even if
10088 is located in data memory.
10089 Bugs like these are hard to reproduce and can cause a lot of trouble.
10093 The return address and the registers used in the interrupt service routine
10094 are saved on the stack
10095 \begin_inset LatexCommand \index{stack}
10099 so there must be sufficient stack space.
10100 If there isn't variables or registers (or even the return address itself)
10107 \begin_inset LatexCommand \index{stack overflow}
10111 is most likely to happen if the interrupt occurs during the
10112 \begin_inset Quotes sld
10116 \begin_inset Quotes srd
10119 subroutine when the stack is already in use for f.e.
10120 many return addresses.
10123 A special note here, int (16 bit) and long (32 bit) integer division
10124 \begin_inset LatexCommand \index{Division}
10129 \begin_inset LatexCommand \index{Multiplication}
10134 \begin_inset LatexCommand \index{Modulus}
10139 \begin_inset LatexCommand \index{Floating point support}
10143 operations are implemented using external support routines developed in
10145 If an interrupt service routine needs to do any of these operations then
10146 the support routines (as mentioned in a following section) will have to
10147 be recompiled using the
10160 \begin_inset LatexCommand \index{-\/-stack-auto}
10166 option and the source file will need to be compiled using the
10181 \begin_inset LatexCommand \index{-\/-int-long-reent}
10188 Calling other functions from an interrupt service routine is not recommended,
10189 avoid it if possible.
10190 Note that when some function is called from an interrupt service routine
10191 it should be preceded by a #pragma\SpecialChar ~
10193 \begin_inset LatexCommand \index{\#pragma nooverlay}
10197 if it is not reentrant.
10198 Furthermore nonreentrant functions should not be called from the main program
10199 while the interrupt service routine might be active.
10205 \begin_inset LatexCommand \ref{sub:Overlaying}
10210 about Overlaying and section
10211 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10216 about Functions using private register banks.
10219 MCS51/DS390 Interrupt Service Routines
10222 Interrupt numbers and the corresponding address & descriptions for the Standard
10223 8051/8052 are listed below.
10224 SDCC will automatically adjust the interrupt vector table to the maximum
10225 interrupt number specified.
10231 \begin_inset Tabular
10232 <lyxtabular version="3" rows="7" columns="3">
10234 <column alignment="center" valignment="top" leftline="true" width="0in">
10235 <column alignment="center" valignment="top" leftline="true" width="0in">
10236 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10237 <row topline="true" bottomline="true">
10238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10254 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10263 <row topline="true">
10264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10280 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10289 <row topline="true">
10290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10306 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10315 <row topline="true">
10316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10341 <row topline="true">
10342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10350 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10358 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10367 <row topline="true">
10368 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10393 <row topline="true" bottomline="true">
10394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10428 If the interrupt service routine is defined without
10431 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10437 a register bank or with register bank 0 (
10441 0), the compiler will save the registers used by itself on the stack upon
10442 entry and restore them at exit, however if such an interrupt service routine
10443 calls another function then the entire register bank will be saved on the
10445 This scheme may be advantageous for small interrupt service routines which
10446 have low register usage.
10449 If the interrupt service routine is defined to be using a specific register
10454 & psw are saved and restored, if such an interrupt service routine calls
10455 another function (using another register bank) then the entire register
10456 bank of the called function will be saved on the stack.
10457 This scheme is recommended for larger interrupt service routines.
10460 HC08 Interrupt Service Routines
10463 Since the number of interrupts available is chip specific and the interrupt
10464 vector table always ends at the last byte of memory, the interrupt numbers
10465 corresponds to the interrupt vectors in reverse order of address.
10466 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10467 2 will use the interrupt vector at 0xfffa, and so on.
10468 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10469 this way; instead see section
10470 \begin_inset LatexCommand \ref{sub:Startup-Code}
10474 for details on customizing startup.
10477 Z80 Interrupt Service Routines
10480 The Z80 uses several different methods for determining the correct interrupt
10481 vector depending on the hardware implementation.
10482 Therefore, SDCC ignores the optional interrupt number and does not attempt
10483 to generate an interrupt vector table.
10486 By default, SDCC generates code for a maskable interrupt, which uses an
10487 RETI instruction to return from the interrupt.
10488 To write an interrupt handler for the non-maskable interrupt, which needs
10489 an RETN instruction instead, add the
10498 void nmi_isr (void) critical interrupt
10512 Enabling and Disabling Interrupts
10515 Critical Functions and Critical Statements
10518 A special keyword may be associated with a block or a function declaring
10524 SDCC will generate code to disable all interrupts
10525 \begin_inset LatexCommand \index{interrupt}
10529 upon entry to a critical function and restore the interrupt enable to the
10530 previous state before returning.
10531 Nesting critical functions will need one additional byte on the stack
10532 \begin_inset LatexCommand \index{stack}
10541 int foo () critical
10542 \begin_inset LatexCommand \index{critical}
10567 The critical attribute maybe used with other attributes like
10577 may also be used to disable interrupts more locally:
10585 More than one statement could have been included in the block.
10588 Enabling and Disabling Interrupts directly
10592 \begin_inset LatexCommand \index{interrupt}
10596 can also be disabled and enabled directly (8051):
10601 EA = 0;\SpecialChar ~
10664 EA = 1;\SpecialChar ~
10731 On other architectures which have seperate opcodes for enabling and disabling
10732 interrupts you might want to make use of defines with inline assembly
10733 \begin_inset LatexCommand \index{Assembler routines}
10743 \begin_inset LatexCommand \index{\_asm}
10752 \begin_inset LatexCommand \index{\_endasm}
10761 #define SEI _asm\SpecialChar ~
10773 Note: it is sometimes sufficient to disable only a specific interrupt source
10775 a timer or serial interrupt by manipulating an
10778 \begin_inset LatexCommand \index{interrupt mask}
10788 Usually the time during which interrupts are disabled should be kept as
10790 This minimizes both
10795 \begin_inset LatexCommand \index{interrupt latency}
10799 (the time between the occurrence of the interrupt and the execution of
10800 the first code in the interrupt routine) and
10805 \begin_inset LatexCommand \index{interrupt jitter}
10809 (the difference between the shortest and the longest interrupt latency).
10810 These really are something different, f.e.
10811 a serial interrupt has to be served before its buffer overruns so it cares
10812 for the maximum interrupt latency, whereas it does not care about jitter.
10813 On a loudspeaker driven via a digital to analog converter which is fed
10814 by an interrupt a latency of a few milliseconds might be tolerable, whereas
10815 a much smaller jitter will be very audible.
10818 You can reenable interrupts within an interrupt routine and on some architecture
10819 s you can make use of two (or more) levels of
10821 interrupt priorities
10824 \begin_inset LatexCommand \index{interrupt priority}
10829 On some architectures which don't support interrupt priorities these can
10830 be implemented by manipulating the interrupt mask and reenabling interrupts
10831 within the interrupt routine.
10832 Check there is sufficient space on the stack
10833 \begin_inset LatexCommand \index{stack}
10837 and don't add complexity unless you have to.
10842 \begin_inset LatexCommand \index{semaphore}
10846 locking (mcs51/ds390)
10849 Some architectures (mcs51/ds390) have an atomic
10850 \begin_inset LatexCommand \index{atomic}
10863 These type of instructions are typically used in preemptive multitasking
10864 systems, where a routine f.e.
10865 claims the use of a data structure ('acquires a lock
10866 \begin_inset LatexCommand \index{lock}
10870 on it'), makes some modifications and then releases the lock when the data
10871 structure is consistent again.
10872 The instruction may also be used if interrupt and non-interrupt code have
10873 to compete for a resource.
10874 With the atomic bit test and clear instruction interrupts
10875 \begin_inset LatexCommand \index{interrupt}
10879 don't have to be disabled for the locking operation.
10883 SDCC generates this instruction if the source follows this pattern:
10888 volatile bit resource_is_free;
10892 if (resource_is_free)
10902 resource_is_free=0;
10915 resource_is_free=1;
10922 Note, mcs51 and ds390 support only an atomic
10923 \begin_inset LatexCommand \index{atomic}
10931 instruction (as opposed to atomic bit test and
10936 Functions using private register banks
10937 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
10944 Some architectures have support for quickly changing register sets.
10945 SDCC supports this feature with the
10948 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10954 attribute (which tells the compiler to use a register bank
10955 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10959 other than the default bank zero).
10960 It should only be applied to
10963 \begin_inset LatexCommand \index{interrupt}
10969 functions (see footnote below).
10970 This will in most circumstances make the generated ISR code more efficient
10971 since it will not have to save registers on the stack.
10978 attribute will have no effect on the generated code for a
10982 function (but may occasionally be useful anyway
10988 possible exception: if a function is called ONLY from 'interrupt' functions
10989 using a particular bank, it can be declared with the same 'using' attribute
10990 as the calling 'interrupt' functions.
10991 For instance, if you have several ISRs using bank one, and all of them
10992 call memcpy(), it might make sense to create a specialized version of memcpy()
10993 'using 1', since this would prevent the ISR from having to save bank zero
10994 to the stack on entry and switch to bank zero before calling the function
11001 (pending: I don't think this has been done yet)
11008 function using a non-zero bank will assume that it can trash that register
11009 bank, and will not save it.
11010 Since high-priority interrupts
11011 \begin_inset LatexCommand \index{interrupt priority}
11015 can interrupt low-priority ones on the 8051 and friends, this means that
11016 if a high-priority ISR
11020 a particular bank occurs while processing a low-priority ISR
11024 the same bank, terrible and bad things can happen.
11025 To prevent this, no single register bank should be
11029 by both a high priority and a low priority ISR.
11030 This is probably most easily done by having all high priority ISRs use
11031 one bank and all low priority ISRs use another.
11032 If you have an ISR which can change priority at runtime, you're on your
11033 own: I suggest using the default bank zero and taking the small performance
11037 It is most efficient if your ISR calls no other functions.
11038 If your ISR must call other functions, it is most efficient if those functions
11039 use the same bank as the ISR (see note 1 below); the next best is if the
11040 called functions use bank zero.
11041 It is very inefficient to call a function using a different, non-zero bank
11047 \begin_inset LatexCommand \label{sub:Startup-Code}
11052 \begin_inset LatexCommand \index{Startup code}
11059 MCS51/DS390 Startup Code
11062 The compiler inserts a call to the C routine
11064 _sdcc_external_startup()
11065 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11074 at the start of the CODE area.
11075 This routine is in the runtime library
11076 \begin_inset LatexCommand \index{Runtime library}
11081 By default this routine returns 0, if this routine returns a non-zero value,
11082 the static & global variable initialization will be skipped and the function
11083 main will be invoked.
11084 Otherwise static & global variables will be initialized before the function
11088 _sdcc_external_startup()
11090 routine to your program to override the default if you need to setup hardware
11091 or perform some other critical operation prior to static & global variable
11093 On some mcs51 variants xdata has to be explicitly enabled before it can
11094 be accessed or if the watchdog needs to be disabled, this is the place
11096 The startup code clears all internal data memory, 256 bytes by default,
11097 but from 0 to n-1 if
11110 \begin_inset LatexCommand \index{-\/-iram-size}
11117 (recommended for Chipcon CC1010).
11120 See also the compiler option
11139 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11144 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11149 about MCS51-variants.
11155 The HC08 startup code follows the same scheme as the MCS51 startup code.
11161 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11162 from sdcc/device/lib/z80/crt0.s.
11163 If you need a different startup code you can use the compiler option
11184 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11188 and provide your own crt0.o.
11192 Inline Assembler Code
11193 \begin_inset LatexCommand \index{Assembler routines}
11200 A Step by Step Introduction
11201 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11208 Starting from a small snippet of c-code this example shows for the MCS51
11209 how to use inline assembly, access variables, a function parameter and
11210 an array in xdata memory.
11211 The example uses an MCS51 here but is easily adapted for other architectures.
11212 This is a buffer routine which should be optimized:
11219 \begin_inset LatexCommand \index{far (storage class)}
11224 \begin_inset LatexCommand \index{at}
11229 \begin_inset LatexCommand \index{Aligned array}
11235 unsigned char head,tail;
11239 void to_buffer( unsigned char c )
11247 if( head != tail-1 )
11257 buf[ head++ ] = c;\SpecialChar ~
11261 /* access to a 256 byte aligned array */
11266 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11267 then a corresponding buffer.asm file is generated.
11268 We define a new function
11272 in file buffer.c in which we cut and paste the generated code, removing
11273 unwanted comments and some ':'.
11275 \begin_inset Quotes sld
11279 \begin_inset Quotes srd
11283 \begin_inset Quotes sld
11287 \begin_inset Quotes srd
11290 to the beginning and the end of the function body:
11296 /* With a cut and paste from the .asm file, we have something to start with.
11301 The function is not yet OK! (registers aren't saved) */
11303 void to_buffer_asm( unsigned char c )
11312 \begin_inset LatexCommand \index{\_asm}
11326 ;buffer.c if( head != tail-1 )
11374 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11375 \begin_inset LatexCommand \index{Aligned array}
11444 The new file buffer.c should compile with only one warning about the unreferenced
11445 function argument 'c'.
11446 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11447 (1) and finally have:
11453 unsigned char far at 0x7f00 buf[0x100];
11455 unsigned char head,tail;
11457 #define USE_ASSEMBLY (1)
11465 void to_buffer( unsigned char c )
11473 if( head != tail-1 )
11493 void to_buffer( unsigned char c )
11501 c; // to avoid warning: unreferenced function argument
11508 \begin_inset LatexCommand \index{\_asm}
11522 ; save used registers here.
11533 ; If we were still using r2,r3 we would have to push them here.
11536 ; if( head != tail-1 )
11579 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
11603 ; buf[ head++ ] = c;
11614 a,dpl \SpecialChar ~
11621 ; dpl holds lower byte of function argument
11632 dpl,_head \SpecialChar ~
11635 ; buf is 0x100 byte aligned so head can be used directly
11677 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
11689 ; restore used registers here
11702 The inline assembler code can contain any valid code understood by the assembler
11703 , this includes any assembler directives and comment lines
11709 The assembler does not like some characters like ':' or ''' in comments.
11710 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
11714 The compiler does not do any validation of the code within the
11717 \begin_inset LatexCommand \index{\_asm}
11725 Specifically it will not know which registers are used and thus register
11727 \begin_inset LatexCommand \index{push/pop}
11731 has to be done manually.
11735 It is recommended that each assembly instruction (including labels) be placed
11736 in a separate line (as the example shows).
11750 \begin_inset LatexCommand \index{-\/-peep-asm}
11756 command line option is used, the inline assembler code will be passed through
11757 the peephole optimizer
11758 \begin_inset LatexCommand \index{Peephole optimizer}
11763 There are only a few (if any) cases where this option makes sense, it might
11764 cause some unexpected changes in the inline assembler code.
11765 Please go through the peephole optimizer rules defined in file
11769 before using this option.
11773 \begin_inset LatexCommand \label{sub:Naked-Functions}
11778 \begin_inset LatexCommand \index{Naked functions}
11785 A special keyword may be associated with a function declaring it as
11788 \begin_inset LatexCommand \index{\_naked}
11799 function modifier attribute prevents the compiler from generating prologue
11800 \begin_inset LatexCommand \index{function prologue}
11805 \begin_inset LatexCommand \index{function epilogue}
11809 code for that function.
11810 This means that the user is entirely responsible for such things as saving
11811 any registers that may need to be preserved, selecting the proper register
11812 bank, generating the
11816 instruction at the end, etc.
11817 Practically, this means that the contents of the function must be written
11818 in inline assembler.
11819 This is particularly useful for interrupt functions, which can have a large
11820 (and often unnecessary) prologue/epilogue.
11821 For example, compare the code generated by these two functions:
11827 \begin_inset LatexCommand \index{volatile}
11831 data unsigned char counter;
11835 void simpleInterrupt(void) interrupt
11836 \begin_inset LatexCommand \index{interrupt}
11854 void nakedInterrupt(void) interrupt 2 _naked
11863 \begin_inset LatexCommand \index{\_asm}
11880 _counter ; does not change flags, no need to save psw
11892 ; MUST explicitly include ret or reti in _naked function.
11899 \begin_inset LatexCommand \index{\_endasm}
11908 For an 8051 target, the generated simpleInterrupt looks like:
12049 whereas nakedInterrupt looks like:
12064 _counter ; does not change flags, no need to save psw
12082 ; MUST explicitly include ret or reti in _naked function
12085 The related directive #pragma exclude
12086 \begin_inset LatexCommand \index{\#pragma exclude}
12090 allows a more fine grained control over pushing & popping
12091 \begin_inset LatexCommand \index{push/pop}
12098 While there is nothing preventing you from writing C code inside a
12102 function, there are many ways to shoot yourself in the foot doing this,
12103 and it is recommended that you stick to inline assembler.
12106 Use of Labels within Inline Assembler
12109 SDCC allows the use of in-line assembler with a few restrictions regarding
12111 In older versions of the compiler all labels defined within inline assembler
12120 where nnnn is a number less than 100 (which implies a limit of utmost 100
12121 inline assembler labels
12135 \begin_inset LatexCommand \index{\_asm}
12165 \begin_inset LatexCommand \index{\_endasm}
12172 Inline assembler code cannot reference any C-Labels, however it can reference
12174 \begin_inset LatexCommand \index{Labels}
12178 defined by the inline assembler, e.g.:
12203 ; some assembler code
12223 /* some more c code */
12225 clabel:\SpecialChar ~
12227 /* inline assembler cannot reference this label */
12239 $0003: ;label (can be referenced by inline assembler only)
12251 /* some more c code */
12256 In other words inline assembly code can access labels defined in inline
12257 assembly within the scope of the function.
12258 The same goes the other way, i.e.
12259 labels defines in inline assembly can not be accessed by C statements.
12262 Interfacing with Assembler Code
12263 \begin_inset LatexCommand \index{Assembler routines}
12270 Global Registers used for Parameter Passing
12271 \begin_inset LatexCommand \index{Parameter passing}
12278 The compiler always uses the global registers
12281 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12286 \begin_inset LatexCommand \index{DPTR}
12291 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12300 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12306 to pass the first parameter to a routine.
12307 The second parameter onwards is either allocated on the stack (for reentrant
12318 -stack-auto is used) or in data / xdata memory (depending on the memory
12323 Assembler Routine (non-reentrant)
12326 In the following example
12327 \begin_inset LatexCommand \index{reentrant}
12332 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12336 the function c_func calls an assembler routine asm_func, which takes two
12338 \begin_inset LatexCommand \index{function parameter}
12347 extern int asm_func(unsigned char, unsigned char);
12351 int c_func (unsigned char i, unsigned char j)
12359 return asm_func(i,j);
12373 return c_func(10,9);
12378 The corresponding assembler function is:
12383 .globl _asm_func_PARM_2
12484 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12501 Note here that the return values
12502 \begin_inset LatexCommand \index{return value}
12506 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
12508 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
12509 b' & 'acc' for four byte values.
12512 The parameter naming convention is _<function_name>_PARM_<n>, where n is
12513 the parameter number starting from 1, and counting from the left.
12514 The first parameter is passed in
12515 \begin_inset Quotes eld
12519 \begin_inset Quotes erd
12522 for a one byte parameter,
12523 \begin_inset Quotes eld
12527 \begin_inset Quotes erd
12531 \begin_inset Quotes eld
12535 \begin_inset Quotes erd
12538 for three bytes and
12539 \begin_inset Quotes eld
12543 \begin_inset Quotes erd
12546 for a four bytes parameter.
12547 The variable name for the second parameter will be _<function_name>_PARM_2.
12551 Assemble the assembler routine with the following command:
12558 asx8051 -losg asmfunc.asm
12565 Then compile and link the assembler routine to the C source file with the
12573 sdcc cfunc.c asmfunc.rel
12576 Assembler Routine (reentrant)
12580 \begin_inset LatexCommand \index{reentrant}
12585 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
12589 the second parameter
12590 \begin_inset LatexCommand \index{function parameter}
12594 onwards will be passed on the stack, the parameters are pushed from right
12596 after the call the leftmost parameter will be on the top of the stack.
12597 Here is an example:
12602 extern int asm_func(unsigned char, unsigned char);
12606 int c_func (unsigned char i, unsigned char j) reentrant
12614 return asm_func(i,j);
12628 return c_func(10,9);
12633 The corresponding assembler routine is:
12733 The compiling and linking procedure remains the same, however note the extra
12734 entry & exit linkage required for the assembler code, _bp is the stack
12735 frame pointer and is used to compute the offset into the stack for parameters
12736 and local variables.
12740 \begin_inset LatexCommand \index{int (16 bit)}
12745 \begin_inset LatexCommand \index{long (32 bit)}
12752 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
12753 multiplication and modulus operations are implemented by support routines.
12754 These support routines are all developed in ANSI-C to facilitate porting
12755 to other MCUs, although some model specific assembler optimizations are
12757 The following files contain the described routines, all of them can be
12758 found in <installdir>/share/sdcc/lib.
12764 \begin_inset Tabular
12765 <lyxtabular version="3" rows="11" columns="2">
12767 <column alignment="center" valignment="top" leftline="true" width="0">
12768 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
12769 <row topline="true" bottomline="true">
12770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12791 <row topline="true">
12792 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12805 16 bit multiplication
12809 <row topline="true">
12810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12818 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12823 signed 16 bit division (calls _divuint)
12827 <row topline="true">
12828 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12841 unsigned 16 bit division
12845 <row topline="true">
12846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12859 signed 16 bit modulus (calls _moduint)
12863 <row topline="true">
12864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12877 unsigned 16 bit modulus
12881 <row topline="true">
12882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12895 32 bit multiplication
12899 <row topline="true">
12900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12913 signed 32 division (calls _divulong)
12917 <row topline="true">
12918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12926 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12931 unsigned 32 division
12935 <row topline="true">
12936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12949 signed 32 bit modulus (calls _modulong)
12953 <row topline="true" bottomline="true">
12954 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
12962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
12967 unsigned 32 bit modulus
12980 Since they are compiled as
12985 \begin_inset LatexCommand \index{reentrant}
12990 \begin_inset LatexCommand \index{interrupt}
12994 service routines should not do any of the above operations.
12995 If this is unavoidable then the above routines will need to be compiled
13009 \begin_inset LatexCommand \index{-\/-stack-auto}
13015 option, after which the source program will have to be compiled with
13028 \begin_inset LatexCommand \index{-\/-int-long-reent}
13035 Notice that you don't have to call these routines directly.
13036 The compiler will use them automatically every time an integer operation
13040 Floating Point Support
13041 \begin_inset LatexCommand \index{Floating point support}
13048 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13049 floating point support routines are derived from gcc's floatlib.c and consist
13050 of the following routines:
13058 \begin_inset Tabular
13059 <lyxtabular version="3" rows="17" columns="2">
13061 <column alignment="center" valignment="top" leftline="true" width="0">
13062 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13063 <row topline="true" bottomline="true">
13064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13081 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13090 <row topline="true">
13091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13108 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13122 add floating point numbers
13126 <row topline="true">
13127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13158 subtract floating point numbers
13162 <row topline="true">
13163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13180 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13194 divide floating point numbers
13198 <row topline="true">
13199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13230 multiply floating point numbers
13234 <row topline="true">
13235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13252 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13266 convert floating point to unsigned char
13270 <row topline="true">
13271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13288 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13302 convert floating point to signed char
13306 <row topline="true">
13307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13338 convert floating point to unsigned int
13342 <row topline="true">
13343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13360 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13374 convert floating point to signed int
13378 <row topline="true">
13379 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13405 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13419 convert floating point to unsigned long
13423 <row topline="true">
13424 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13441 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13455 convert floating point to signed long
13459 <row topline="true">
13460 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13491 convert unsigned char to floating point
13495 <row topline="true">
13496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13513 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13527 convert char to floating point number
13531 <row topline="true">
13532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13563 convert unsigned int to floating point
13567 <row topline="true">
13568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13599 convert int to floating point numbers
13603 <row topline="true">
13604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13635 convert unsigned long to floating point number
13639 <row topline="true" bottomline="true">
13640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13671 convert long to floating point number
13684 These support routines are developed in ANSI-C so there is room for space
13685 and speed improvement.
13686 Note if all these routines are used simultaneously the data space might
13688 For serious floating point usage it is recommended that the large model
13690 Also notice that you don't have to call this routines directly.
13691 The compiler will use them automatically every time a floating point operation
13696 \begin_inset LatexCommand \index{Libraries}
13705 <pending: this is messy and incomplete - a little more information is in
13706 sdcc/doc/libdoc.txt
13711 Compiler support routines (_gptrget, _mulint etc.)
13714 Stdclib functions (puts, printf, strcat etc.)
13715 \layout Subsubsection
13721 \begin_inset LatexCommand \index{<stdio.h>}
13725 As usual on embedded systems you have to provide your own
13728 \begin_inset LatexCommand \index{getchar()}
13737 \begin_inset LatexCommand \index{putchar()}
13744 SDCC does not know whether the system connects to a serial line with or
13745 without handshake, LCD, keyboard or other device.
13746 You'll find examples for serial routines f.e.
13747 in sdcc/device/lib.
13753 \begin_inset LatexCommand \index{printf()}
13763 does not support float (except on ds390).
13764 To enable this recompile it with the option
13777 \begin_inset LatexCommand \index{USE\_FLOATS}
13783 on the command line.
13797 \begin_inset LatexCommand \index{-\/-model-large}
13803 for the mcs51 port, since this uses a lot of memory.
13806 If you're short on memory you might want to use
13819 For the mcs51 there is an assembly version
13823 which should fit the requirements of many embedded systems (by unsetting
13824 #defines it can be customized to
13828 support long variables and field widths).
13831 Math functions (sin, pow, sqrt etc.)
13838 \begin_inset LatexCommand \index{Libraries}
13842 included in SDCC should have a license at least as liberal as the GNU Lesser
13843 General Public License
13844 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
13855 license statements for the libraries are missing.
13856 sdcc/device/lib/ser_ir.c
13860 come with a GPL (as opposed to LGPL) License - this will not be liberal
13861 enough for many embedded programmers.
13864 If you have ported some library or want to share experience about some code
13866 falls into any of these categories Busses (I
13867 \begin_inset Formula $^{\textrm{2}}$
13870 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
13871 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
13872 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
13873 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
13878 would certainly like to hear about it.
13879 Programmers coding for embedded systems are not especially famous for being
13880 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
13881 e these references are very valuable.
13882 Let's help to create a climate where information is shared.
13888 MCS51 Memory Models
13889 \begin_inset LatexCommand \index{Memory model}
13894 \begin_inset LatexCommand \index{MCS51 memory model}
13899 \layout Subsubsection
13904 SDCC allows two memory models for MCS51 code,
13913 Modules compiled with different memory models should
13917 be combined together or the results would be unpredictable.
13918 The library routines supplied with the compiler are compiled as both small
13920 The compiled library modules are contained in separate directories as small
13921 and large so that you can link to either set.
13925 When the large model is used all variables declared without a storage class
13926 will be allocated into the external ram, this includes all parameters and
13927 local variables (for non-reentrant
13928 \begin_inset LatexCommand \index{reentrant}
13933 When the small model is used variables without storage class are allocated
13934 in the internal ram.
13937 Judicious usage of the processor specific storage classes
13938 \begin_inset LatexCommand \index{Storage class}
13942 and the 'reentrant' function type will yield much more efficient code,
13943 than using the large model.
13944 Several optimizations are disabled when the program is compiled using the
13945 large model, it is therefore recommended that the small model be used unless
13946 absolutely required.
13947 \layout Subsubsection
13950 \begin_inset LatexCommand \label{sub:External-Stack}
13955 \begin_inset LatexCommand \index{stack}
13960 \begin_inset LatexCommand \index{External stack (mcs51)}
13967 The external stack (-
13978 \begin_inset LatexCommand \index{-\/-xstack}
13982 ) is located in pdata
13983 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
13987 memory (usually at the start of the external ram segment) and uses all
13988 unused space in pdata (max.
14000 -xstack option is used to compile the program, the parameters and local
14002 \begin_inset LatexCommand \index{local variables}
14006 of all reentrant functions are allocated in this area.
14007 This option is provided for programs with large stack space requirements.
14008 When used with the -
14019 \begin_inset LatexCommand \index{-\/-stack-auto}
14023 option, all parameters and local variables are allocated on the external
14024 stack (note: support libraries will need to be recompiled with the same
14026 There is a predefined target in the library makefile).
14029 The compiler outputs the higher order address byte of the external ram segment
14031 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14036 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14040 ), therefore when using the External Stack option, this port
14044 be used by the application program.
14048 \begin_inset LatexCommand \index{Memory model}
14053 \begin_inset LatexCommand \index{DS390 memory model}
14060 The only model supported is Flat 24
14061 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14066 This generates code for the 24 bit contiguous addressing mode of the Dallas
14068 In this mode, up to four meg of external RAM or code space can be directly
14070 See the data sheets at www.dalsemi.com for further information on this part.
14074 Note that the compiler does not generate any code to place the processor
14075 into 24 bitmode (although
14079 in the ds390 libraries will do that for you).
14085 \begin_inset LatexCommand \index{Tinibios (DS390)}
14089 , the boot loader or similar code must ensure that the processor is in 24
14090 bit contiguous addressing mode before calling the SDCC startup code.
14108 option, variables will by default be placed into the XDATA segment.
14113 Segments may be placed anywhere in the 4 meg address space using the usual
14125 Note that if any segments are located above 64K, the -r flag must be passed
14126 to the linker to generate the proper segment relocations, and the Intel
14127 HEX output format must be used.
14128 The -r flag can be passed to the linker by using the option
14132 on the SDCC command line.
14133 However, currently the linker can not handle code segments > 64k.
14137 \begin_inset LatexCommand \index{Pragmas}
14144 SDCC supports the following #pragma directives:
14148 \begin_inset LatexCommand \index{\#pragma save}
14152 - this will save all current options to the save/restore stack.
14153 See #pragma\SpecialChar ~
14158 \begin_inset LatexCommand \index{\#pragma restore}
14162 - will restore saved options from the last save.
14163 saves & restores can be nested.
14164 SDCC uses a save/restore stack: save pushes current options to the stack,
14165 restore pulls current options from the stack.
14166 See #pragma\SpecialChar ~
14173 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14178 \begin_inset LatexCommand \index{function prologue}
14182 function1[,function2[,function3...]] - The compiler by default uses a caller
14183 saves convention for register saving across function calls, however this
14184 can cause unnecessary register pushing & popping
14185 \begin_inset LatexCommand \index{push/pop}
14189 when calling small functions from larger functions.
14190 This option can be used to switch off the register saving convention for
14191 the function names specified.
14192 The compiler will not save registers when calling these functions, extra
14193 code need to be manually inserted at the entry & exit for these functions
14194 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14195 reduce code & improve run time performance of the generated code.
14196 In the future the compiler (with inter procedural analysis) may be able
14197 to determine the appropriate scheme to use for each function call.
14208 -callee-saves command line option is used, the function names specified
14209 in #pragma\SpecialChar ~
14211 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14215 is appended to the list of functions specified in the command line.
14219 \begin_inset LatexCommand \index{\#pragma exclude}
14223 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14224 of pairs of push/pop
14225 \begin_inset LatexCommand \index{push/pop}
14234 \begin_inset LatexCommand \index{interrupt}
14247 The directive should be placed immediately before the ISR function definition
14248 and it affects ALL ISR functions following it.
14249 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14250 exclude\SpecialChar ~
14252 \begin_inset LatexCommand \index{\#pragma exclude}
14257 See also the related keyword _naked
14258 \begin_inset LatexCommand \index{\_naked}
14266 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14270 - the compiler will not warn you anymore for obvious mistakes, you'r on
14274 disable_warning <nnnn>
14275 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14279 - the compiler will not warn you anymore about warning number <nnnn>.
14283 \begin_inset LatexCommand \index{\#pragma nogcse}
14287 - will stop global common subexpression elimination.
14291 \begin_inset LatexCommand \index{\#pragma noinduction}
14295 - will stop loop induction optimizations.
14299 \begin_inset LatexCommand \index{\#pragma noinvariant}
14303 - will not do loop invariant optimizations.
14304 For more details see Loop Invariants in section
14305 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14313 \begin_inset LatexCommand \index{\#pragma noiv}
14317 - Do not generate interrupt
14318 \begin_inset LatexCommand \index{interrupt}
14322 vector table entries for all ISR functions defined after the pragma.
14323 This is useful in cases where the interrupt vector table must be defined
14324 manually, or when there is a secondary, manually defined interrupt vector
14326 for the autovector feature of the Cypress EZ-USB FX2).
14327 More elegantly this can be achieved by obmitting the optional interrupt
14328 number after the interrupt keyword, see section
14329 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14338 \begin_inset LatexCommand \index{\#pragma nojtbound}
14342 - will not generate code for boundary value checking, when switch statements
14343 are turned into jump-tables (dangerous).
14344 For more details see section
14345 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14353 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14357 - Will not do loop reversal optimization
14361 \begin_inset LatexCommand \index{\#pragma nooverlay}
14365 - the compiler will not overlay the parameters and local variables of a
14370 \begin_inset LatexCommand \index{\#pragma stackauto}
14385 \begin_inset LatexCommand \index{-\/-stack-auto}
14390 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
14394 Parameters and Local Variables.
14398 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
14402 - The compiler will optimize code generation towards fast code, possibly
14403 at the expense of code size.
14407 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
14411 - The compiler will optimize code generation towards compact code, possibly
14412 at the expense of code speed.
14416 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
14420 - The compiler will attempt to generate code that is both compact and fast,
14421 as long as meeting one goal is not a detriment to the other (this is the
14426 SDCPP supports the following #pragma directives:
14430 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
14434 (+ | -) - switch _asm _endasm block preprocessing on / off.
14438 The pragma's are intended to be used to turn-on or off certain optimizations
14439 which might cause the compiler to generate extra stack / data space to
14440 store compiler generated temporary variables.
14441 This usually happens in large functions.
14442 Pragma directives should be used as shown in the following example, they
14443 are used to control options & optimizations for a given function; pragmas
14444 should be placed before and/or after a function, placing pragma's inside
14445 a function body could have unpredictable results.
14451 \begin_inset LatexCommand \index{\#pragma save}
14462 /* save the current settings */
14465 \begin_inset LatexCommand \index{\#pragma nogcse}
14474 /* turnoff global subexpression elimination */
14476 #pragma noinduction
14477 \begin_inset LatexCommand \index{\#pragma noinduction}
14481 /* turn off induction optimizations */
14504 \begin_inset LatexCommand \index{\#pragma restore}
14508 /* turn the optimizations back on */
14511 The compiler will generate a warning message when extra space is allocated.
14512 It is strongly recommended that the save and restore pragma's be used when
14513 changing options for a function.
14516 Defines Created by the Compiler
14519 The compiler creates the following #defines
14520 \begin_inset LatexCommand \index{\#defines}
14525 \begin_inset LatexCommand \index{Defines created by the compiler}
14535 \begin_inset Tabular
14536 <lyxtabular version="3" rows="10" columns="2">
14538 <column alignment="center" valignment="top" leftline="true" width="0">
14539 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14540 <row topline="true" bottomline="true">
14541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14551 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14562 <row topline="true">
14563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14569 \begin_inset LatexCommand \index{SDCC}
14576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14581 this Symbol is always defined
14585 <row topline="true">
14586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14592 \begin_inset LatexCommand \index{SDCC\_mcs51}
14597 \begin_inset LatexCommand \index{SDCC\_ds390}
14602 \begin_inset LatexCommand \index{SDCC\_z80}
14609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14614 depending on the model used (e.g.: -mds390
14618 <row topline="true">
14619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14625 \begin_inset LatexCommand \index{\_\_mcs51}
14630 \begin_inset LatexCommand \index{\_\_ds390}
14635 \begin_inset LatexCommand \index{\_\_hc08}
14640 \begin_inset LatexCommand \index{\_\_z80}
14647 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14652 depending on the model used (e.g.
14657 <row topline="true">
14658 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14664 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
14671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14694 <row topline="true">
14695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14701 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
14708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14731 <row topline="true">
14732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14738 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
14745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14768 <row topline="true">
14769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14775 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
14782 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14805 <row topline="true">
14806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14812 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
14819 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14832 <row topline="true" bottomline="true">
14833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14839 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
14846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14866 Notes on supported Processors
14870 \begin_inset LatexCommand \label{sub:MCS51-variants}
14875 \begin_inset LatexCommand \index{MCS51 variants}
14882 MCS51 processors are available from many vendors and come in many different
14884 While they might differ considerably in respect to Special Function Registers
14885 the core MCS51 is usually not modified or is kept compatible.
14889 pdata access by SFR
14892 With the upcome of devices with internal xdata and flash memory devices
14894 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14898 as dedicated I/O port is becoming more popular.
14899 Switching the high byte for pdata
14900 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14904 access which was formerly done by port P2 is then achieved by a Special
14906 \begin_inset LatexCommand \index{sfr}
14911 In well-established MCS51 tradition the address of this
14915 is where the chip designers decided to put it.
14916 Needless to say that they didn't agree on a common name either.
14917 So that the startup code can correctly initialize xdata variables, you
14918 should define an sfr with the name _XPAGE
14921 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
14927 at the appropriate location if the default, port P2, is not used for this.
14933 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
14938 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
14943 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
14946 For more exotic implementations further customizations may be needed.
14948 \begin_inset LatexCommand \ref{sub:Startup-Code}
14952 for other possibilities.
14955 Other Features available by SFR
14958 Some MCS51 variants offer features like Double DPTR
14959 \begin_inset LatexCommand \index{DPTR}
14963 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
14964 These are currently not used for the MCS51 port.
14965 If you absolutely need them you can fall back to inline assembly or submit
14972 The DS80C400 microcontroller has a rich set of peripherals.
14973 In its built-in ROM library it includes functions to access some of the
14974 features, among them is a TCP stack with IP4 and IP6 support.
14975 Library headers (currently in beta status) and other files are provided
14979 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
14987 The Z80 and gbz80 port
14990 SDCC can target both the Zilog
14991 \begin_inset LatexCommand \index{Z80}
14995 and the Nintendo Gameboy's Z80-like gbz80
14996 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15001 The Z80 port is passed through the same
15004 \begin_inset LatexCommand \index{Regression test}
15010 as the MCS51 and DS390 ports, so floating point support, support for long
15011 variables and bitfield support is fine.
15012 See mailing lists and forums about interrupt routines.
15015 As always, the code is the authoritative reference - see z80/ralloc.c and
15018 \begin_inset LatexCommand \index{stack}
15022 frame is similar to that generated by the IAR Z80 compiler.
15023 IX is used as the base pointer, HL and IY are used as a temporary registers,
15024 and BC and DE are available for holding variables.
15026 \begin_inset LatexCommand \index{return value}
15030 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15032 The gbz80 port use the same set of registers for the return values, but
15033 in a different order of significance: E (one byte), DE (two bytes), or
15040 The port to the Motorola HC08
15041 \begin_inset LatexCommand \index{HC08}
15045 family has been added in October 2003, and is still undergoing some basic
15047 The code generator is complete, but the register allocation is still quite
15049 Some of the SDCC's standard C library functions have embedded non-HC08
15050 inline assembly and so are not yet usable.
15061 \begin_inset LatexCommand \index{PIC14}
15065 port still requires a major effort from the development community.
15066 However it can work for very simple code.
15069 C code and 14bit PIC code page
15070 \begin_inset LatexCommand \index{code page (pic14)}
15075 \begin_inset LatexCommand \index{RAM bank (pic14)}
15082 The linker organizes allocation for the code page and RAM banks.
15083 It does not have intimate knowledge of the code flow.
15084 It will put all the code section of a single asm file into a single code
15086 In order to make use of multiple code pages, separate asm files must be
15088 The compiler treats all functions of a single C file as being in the same
15089 code page unless it is non static.
15090 The compiler treats all local variables of a single C file as being in
15091 the same RAM bank unless it is an extern.
15095 To get the best follow these guide lines:
15098 make local functions static, as non static functions require code page selection
15102 Make local variables static as extern variables require RAM bank selection
15106 For devices that have multiple code pages it is more efficient to use the
15107 same number of files as pages, i.e.
15108 for the 16F877 use 4 separate files and i.e.
15109 for the 16F874 use 2 separate files.
15110 This way the linker can put the code for each file into different code
15111 pages and the compiler can allocate reusable variables more efficiently
15112 and there's less page selection overhead.
15113 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15114 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15117 Creating a device include file
15120 For generating a device include file use the support perl script inc2h.pl
15121 kept in directory support/script.
15127 For the interrupt function, use the keyword 'interrupt'
15128 \begin_inset LatexCommand \index{interrupt}
15132 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15133 there to avoid a syntax error - it ought to be fixed).
15139 void Intr(void) interrupt 0
15145 T0IF = 0; /* Clear timer interrupt */
15150 Linking and assembling
15153 For assembling you can use either GPUTILS'
15154 \begin_inset LatexCommand \index{gputils (pic tools)}
15158 gpasm.exe or MPLAB's mpasmwin.exe.
15159 GPUTILS is available from
15160 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
15165 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15166 If you use MPLAB and an interrupt function then the linker script file
15167 vectors section will need to be enlarged to link with mplink.
15190 sdcc -S -V -mpic14 -p16F877 $<
15204 $(PRJ).hex: $(OBJS)
15214 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15236 sdcc -S -V -mpic14 -p16F877 $<
15246 mpasmwin /q /o $*.asm
15250 $(PRJ).hex: $(OBJS)
15260 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
15263 Please note that indentations within a
15267 have to be done with a tabulator character.
15271 \begin_inset LatexCommand \index{PIC16}
15279 \begin_inset LatexCommand \index{PIC16}
15283 port is the portion of SDCC that is responsible to produce code for the
15285 \begin_inset LatexCommand \index{Microchip}
15289 (TM) microcontrollers with 16 bit core.
15290 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
15296 PIC16 port supports the standard command line arguments as supposed, with
15297 the exception of certain cases that will be mentioned in the following
15300 \labelwidthstring 00.00.0000
15312 -callee-saves See -
15324 \labelwidthstring 00.00.0000
15336 -all-callee-saves All function arguments are passed on stack by default.
15339 There is no need to specify this in the command line.
15341 \labelwidthstring 00.00.0000
15353 -fommit-frame-pointer Frame pointer will be omitted when the function uses
15354 no local variables.
15357 Port Specific Options
15358 \begin_inset LatexCommand \index{Options PIC16}
15365 The port specific options appear after the global options in the sdcc --help
15367 \layout Subsubsection
15372 General options enable certain port features and optimizations.
15374 \labelwidthstring 00.00.0000
15386 -nodefaultlibs do not link default libraries when linking.
15388 \labelwidthstring 00.00.0000
15400 -obanksel=# Set optimization level for inserting BANKSELs.
15403 \labelwidthstring 00.00.0000
15405 0 is no optimization
15407 \labelwidthstring 00.00.0000
15409 1 checkes previous used register and if it is the same then doesn't emit
15410 BANKSEL, accounts only for labels.
15413 \labelwidthstring 00.00.0000
15425 -pomit-config-words Omit the generation of the configuration words.
15427 \labelwidthstring 00.00.0000
15439 -pomit-ivt Omit the generation of the interrupt vectors.
15441 \labelwidthstring 00.00.0000
15453 -pleave-reset-vector Used in conjuction with -
15463 -pomit-ivt, instructs the port NOT to omit the reset vector.
15465 \labelwidthstring 00.00.0000
15477 -stack-model=[model] Used in conjuction with the command above.
15478 Defines the stack model to be used, valid stack models are :
15481 \labelwidthstring 00.00.0000
15487 Selects small stack model.
15488 8 bit stack and frame pointers.
15489 Supports 256 bytes stack size.
15491 \labelwidthstring 00.00.0000
15497 Selects large stack model.
15498 16 bit stack and frame pointers.
15499 Supports 65536 bytes stack size.
15502 \labelwidthstring 00.00.0000
15514 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
15515 unitialized data variables with [kword].
15516 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
15518 \labelwidthstring 00.00.0000
15530 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
15531 Useful for bootloaders.
15533 \labelwidthstring 00.00.0000
15545 -asm= sets the full path and name of an external assembler to call.
15547 \labelwidthstring 00.00.0000
15559 -link= sets the full path and name of an external linker to call.
15561 \labelwidthstring 00.00.0000
15573 -call-tree dump call tree in .calltree file
15575 \labelwidthstring 00.00.0000
15587 -mplab-comp MPLAB compatibility option.
15588 Currently only suppresses special gpasm directives.
15590 \labelwidthstring 00.00.0000
15602 -use-crt= Use a custom run-time module instead of the defaults.
15604 \labelwidthstring 00.00.0000
15616 -no-crt Don't link the default run-time modules
15618 \labelwidthstring 00.00.0000
15630 -flr-support When entering/leaving a function, call support functions to
15631 store/restore used registers
15632 \layout Subsubsection
15637 Debugging options enable extra debugging information in the output files.
15639 \labelwidthstring 00.00.0000
15651 -debug-xtra Similar to -
15662 \begin_inset LatexCommand \index{-\/-debug}
15666 , but dumps more information.
15668 \labelwidthstring 00.00.0000
15680 -debug-ralloc Force register allocator to dump <source>.d file with debugging
15682 <source> is the name of the file compiled.
15684 \labelwidthstring 00.00.0000
15696 -pcode-verbose Enable pcode debugging information in translation.
15698 \labelwidthstring 00.00.0000
15710 -denable-peeps Force the usage of peepholes.
15713 \labelwidthstring 00.00.0000
15725 -gstack Trace push/pops for stack pointer overflow
15728 Preprocessor Macros
15731 PIC16 port defines the following preprocessor macros while translating a
15736 \begin_inset Tabular
15737 <lyxtabular version="3" rows="2" columns="2">
15739 <column alignment="center" valignment="top" leftline="true" width="0">
15740 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15741 <row topline="true" bottomline="true">
15742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15750 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15759 <row topline="true" bottomline="true">
15760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15768 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15773 MCU Identification.
15778 is the microcontrol identification number, i.e.
15794 \begin_inset LatexCommand \index{PIC16}
15798 port uses the following directories for searching header files and libraries.
15802 \begin_inset Tabular
15803 <lyxtabular version="3" rows="3" columns="4">
15805 <column alignment="center" valignment="top" leftline="true" width="0">
15806 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15807 <column alignment="center" valignment="top" width="0">
15808 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15809 <row topline="true" bottomline="true">
15810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15818 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15843 <row topline="true">
15844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15849 PREFIX/sdcc/include/pic16
15852 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15857 PIC16 specific headers
15860 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15877 <row topline="true" bottomline="true">
15878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15883 PREFIX/sdcc/lib/pic16
15886 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15891 PIC16 specific libraries
15894 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15902 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15919 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
15926 PIC16 port currently supports the following pragmas:
15928 \labelwidthstring 00.00.0000
15930 stack pragma stack forces the code generator to initialize the stack & frame
15931 pointers at a specific address.
15932 This is an adhoc solution for cases where no STACK directive is available
15933 in the linker script or gplink is not instructed to create a stack section.
15935 The stack pragma should be used only once in a project.
15936 Multiple pragmas may result in indeterminate behaviour of the program.
15938 The format is as follows:
15941 #pragma stack bottom_address [stack_size]
15948 is the lower bound of the stack section.
15949 The stack pointer initially will point at address (bottom_address+stack_size-1).
15957 /* initializes stack of 100 bytes at RAM address 0x200 */
15960 #pragma stack 0x200 100
15962 \labelwidthstring 00.00.0000
15964 code place a function symbol at static FLASH address
15972 /* place function test_func at 0x4000 */
15975 #pragma code test_func 0x4000
15977 \labelwidthstring 00.00.0000
15979 udata pragma udata instructs the compiler to emit code so that linker will
15980 place a variable at a specific memory bank
15988 /* places variable foo at bank2 */
15991 #pragma udata bank2 foo
15997 In order for this pragma to work there are some changes that must be made
15998 in the .lkr script used in link stage.
15999 In the following example a sample .lkr file is shown:
16004 // Sample linker script for the PIC18F452 processor
16010 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
16013 CODEPAGE NAME=page START=0x2A END=0x7FFF
16016 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
16019 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
16022 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
16025 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
16028 ACCESSBANK NAME=accessram START=0x0 END=0x7F
16033 DATABANK NAME=gpr0 START=0x80 END=0xFF
16036 DATABANK NAME=gpr1 START=0x100 END=0x1FF
16039 DATABANK NAME=gpr2 START=0x200 END=0x2FF
16042 DATABANK NAME=gpr3 START=0x300 END=0x3FF
16045 DATABANK NAME=gpr4 START=0x400 END=0x4FF
16048 DATABANK NAME=gpr5 START=0x500 END=0x5FF
16051 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
16056 SECTION NAME=CONFIG ROM=config
16061 SECTION NAME=bank0 RAM=gpr0
16064 SECTION NAME=bank1 RAM=gpr1
16067 SECTION NAME=bank2 RAM=gpr2
16070 SECTION NAME=bank3 RAM=gpr3
16073 SECTION NAME=bank4 RAM=gpr4
16076 SECTION NAME=bank5 RAM=gpr5
16079 The linker will recognise the section name set in the pragma statement and
16080 will position the variable at the memory bank set with the RAM field at
16081 the SECTION line in the linker script file.
16085 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
16092 There is one main header file that can be included to the source files using
16099 This header file contains the definitions for the processor special registers,
16100 so it is necessary if the source accesses them.
16101 It can be included by adding the following line in the beginning of the
16105 #include <pic18fregs.h>
16108 The specific microcontroller is selected within the pic18fregs.h automatically,
16109 so the same source can be used with a variety of devices.
16115 The libraries that PIC16
16116 \begin_inset LatexCommand \index{PIC16}
16120 port depends on are the microcontroller device libraries which contain
16121 the symbol definitions for the microcontroller special function registers.
16122 These libraries have the format pic18fxxxx.lib, where
16126 is the microcontroller identification number.
16127 The specific library is selected automatically by the compiler at link
16128 stage according to the selected device.
16131 Libraries are created with gplib which is part of the gputils package
16132 \begin_inset LatexCommand \url{http://gputils.sourceforge.net}
16142 The following memory models are supported by the PIC16 port:
16151 Memory model affects the default size of pointers within the source.
16152 The sizes are shown in the next table:
16156 \begin_inset Tabular
16157 <lyxtabular version="3" rows="3" columns="3">
16159 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16160 <column alignment="center" valignment="top" leftline="true" width="0">
16161 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16162 <row topline="true" bottomline="true">
16163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16168 Pointer sizes according to memory model
16171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16179 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16188 <row topline="true" bottomline="true">
16189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16197 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16214 <row topline="true" bottomline="true">
16215 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16223 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
16231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16247 It is advisable that all sources within a project are compiled with the
16249 If one wants to override the default memory model, this can be done by
16250 declaring a pointer as
16259 Far selects large memory model's pointers, while near selects small memory
16263 The standard device libraries (see
16264 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
16268 ) contain no reference to pointers, so they can be used with both memory
16275 The stack implementation for the PIC16 port uses two indirect registers,
16278 \labelwidthstring 00.00.0000
16280 FSR1 is assigned as stack pointer
16282 \labelwidthstring 00.00.0000
16284 FSR2 is assigned as frame pointer
16287 The following stack models are supported by the PIC16 port
16308 model means that only the FSRxL byte is used to access stack and frame,
16315 uses both FSRxL and FSRxH registers.
16316 The following table shows the stack/frame pointers sizes according to stack
16317 model and the maximum space they can address:
16321 \begin_inset Tabular
16322 <lyxtabular version="3" rows="3" columns="3">
16324 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16325 <column alignment="center" valignment="top" leftline="true" width="0">
16326 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16327 <row topline="true" bottomline="true">
16328 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16333 Stack & Frame pointer sizes according to stack model
16336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16344 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16353 <row topline="true">
16354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16379 <row topline="true" bottomline="true">
16380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16396 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16416 stack model is currently not working properly throughout the code generator.
16417 So its use is not advised.
16418 Also there are some other points that need special care:
16421 Do not create stack sections with size more than one physical bank (that
16425 Stack sections should no cross physical bank limits (i.e.
16426 #pragma stack 0x50 0x100)
16429 These limitations are caused by the fact that only FSRxL is modified when
16430 using SMALL stack model, so no more than 256 bytes of stack can be used.
16431 This problem will disappear after LARGE model is fully implemented.
16434 Function return values
16437 Return values from functions are placed to the appropriate registers following
16438 a modified Microchip policy optimized for SDCC.
16439 The following table shows these registers:
16443 \begin_inset Tabular
16444 <lyxtabular version="3" rows="6" columns="2">
16446 <column alignment="center" valignment="top" leftline="true" width="0">
16447 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16448 <row topline="true" bottomline="true">
16449 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16457 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16462 destination register
16466 <row topline="true">
16467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16484 <row topline="true">
16485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16502 <row topline="true">
16503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16511 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16520 <row topline="true">
16521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16534 FSR0L:PRODH:PRODL:WREG
16538 <row topline="true" bottomline="true">
16539 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16547 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16552 on stack, FSR0 points to the beginning
16566 When entering an interrupt, currently the PIC16
16567 \begin_inset LatexCommand \index{PIC16}
16571 port automatically saves the following registers:
16583 PROD (PRODL and PRODH)
16586 FSR0 (FSR0L and FSR0H)
16589 These registers are restored upon return from the interrupt routine.
16593 When entering a high priority interrupt WREG, STATUS and BSR are not explicit
16595 The hardware shadow registers for WREG, STATUS and BSR are used in these
16605 NOTE that when the _naked attribute is specified for an interrupt routine,
16606 then NO registers are stored or restored.
16612 Debugging with SDCDB
16613 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
16618 \begin_inset LatexCommand \index{sdcdb (debugger)}
16625 SDCC is distributed with a source level debugger
16626 \begin_inset LatexCommand \index{Debugger}
16631 The debugger uses a command line interface, the command repertoire of the
16632 debugger has been kept as close to gdb
16633 \begin_inset LatexCommand \index{gdb}
16637 (the GNU debugger) as possible.
16638 The configuration and build process is part of the standard compiler installati
16639 on, which also builds and installs the debugger in the target directory
16640 specified during configuration.
16641 The debugger allows you debug BOTH at the C source and at the ASM source
16643 Sdcdb is available on Unix platforms only.
16646 Compiling for Debugging
16660 \begin_inset LatexCommand \index{-\/-debug}
16664 option must be specified for all files for which debug information is to
16666 The complier generates a .adb file for each of these files.
16667 The linker creates the .cdb
16668 \begin_inset LatexCommand \index{<file>.cdb}
16673 \begin_inset LatexCommand \index{<file>.adb}
16677 files and the address information.
16678 This .cdb is used by the debugger.
16681 How the Debugger Works
16694 -debug option is specified the compiler generates extra symbol information
16695 some of which are put into the assembler source and some are put into the
16697 Then the linker creates the .cdb file from the individual .adb files with
16698 the address information for the symbols.
16699 The debugger reads the symbolic information generated by the compiler &
16700 the address information generated by the linker.
16701 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
16702 execution is controlled by the debugger.
16703 When a command is issued for the debugger, it translates it into appropriate
16704 commands for the simulator.
16707 Starting the Debugger
16710 The debugger can be started using the following command line.
16711 (Assume the file you are debugging has the file name foo).
16725 The debugger will look for the following files.
16728 foo.c - the source file.
16731 foo.cdb - the debugger symbol information file.
16734 foo.ihx - the Intel hex format
16735 \begin_inset LatexCommand \index{Intel hex format}
16742 Command Line Options.
16755 -directory=<source file directory> this option can used to specify the directory
16757 The debugger will look into the directory list specified for source, cdb
16759 The items in the directory list must be separated by ':', e.g.
16760 if the source files can be in the directories /home/src1 and /home/src2,
16771 -directory option should be -
16781 -directory=/home/src1:/home/src2.
16782 Note there can be no spaces in the option.
16786 -cd <directory> - change to the <directory>.
16789 -fullname - used by GUI front ends.
16792 -cpu <cpu-type> - this argument is passed to the simulator please see the
16793 simulator docs for details.
16796 -X <Clock frequency > this options is passed to the simulator please see
16797 the simulator docs for details.
16800 -s <serial port file> passed to simulator see the simulator docs for details.
16803 -S <serial in,out> passed to simulator see the simulator docs for details.
16806 -k <port number> passed to simulator see the simulator docs for details.
16812 As mentioned earlier the command interface for the debugger has been deliberatel
16813 y kept as close the GNU debugger gdb, as possible.
16814 This will help the integration with existing graphical user interfaces
16815 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
16816 If you use a graphical user interface for the debugger you can skip the
16818 \layout Subsubsection*
16820 break [line | file:line | function | file:function]
16823 Set breakpoint at specified line or function:
16832 sdcdb>break foo.c:100
16834 sdcdb>break funcfoo
16836 sdcdb>break foo.c:funcfoo
16837 \layout Subsubsection*
16839 clear [line | file:line | function | file:function ]
16842 Clear breakpoint at specified line or function:
16851 sdcdb>clear foo.c:100
16853 sdcdb>clear funcfoo
16855 sdcdb>clear foo.c:funcfoo
16856 \layout Subsubsection*
16861 Continue program being debugged, after breakpoint.
16862 \layout Subsubsection*
16867 Execute till the end of the current function.
16868 \layout Subsubsection*
16873 Delete breakpoint number 'n'.
16874 If used without any option clear ALL user defined break points.
16875 \layout Subsubsection*
16877 info [break | stack | frame | registers ]
16880 info break - list all breakpoints
16883 info stack - show the function call stack.
16886 info frame - show information about the current execution frame.
16889 info registers - show content of all registers.
16890 \layout Subsubsection*
16895 Step program until it reaches a different source line.
16896 Note: pressing <return> repeats the last command.
16897 \layout Subsubsection*
16902 Step program, proceeding through subroutine calls.
16903 \layout Subsubsection*
16908 Start debugged program.
16909 \layout Subsubsection*
16914 Print type information of the variable.
16915 \layout Subsubsection*
16920 print value of variable.
16921 \layout Subsubsection*
16926 load the given file name.
16927 Note this is an alternate method of loading file for debugging.
16928 \layout Subsubsection*
16933 print information about current frame.
16934 \layout Subsubsection*
16939 Toggle between C source & assembly source.
16940 \layout Subsubsection*
16942 ! simulator command
16945 Send the string following '!' to the simulator, the simulator response is
16947 Note the debugger does not interpret the command being sent to the simulator,
16948 so if a command like 'go' is sent the debugger can loose its execution
16949 context and may display incorrect values.
16950 \layout Subsubsection*
16957 My name is Bobby Brown"
16960 Interfacing with XEmacs
16961 \begin_inset LatexCommand \index{XEmacs}
16966 \begin_inset LatexCommand \index{Emacs}
16973 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
16974 sdcdb.el and sdcdbsrc.el.
16975 These two files can be found in the $(prefix)/bin directory after the installat
16977 These files need to be loaded into XEmacs for the interface to work.
16978 This can be done at XEmacs startup time by inserting the following into
16979 your '.xemacs' file (which can be found in your HOME directory):
16985 (load-file sdcdbsrc.el)
16991 .xemacs is a lisp file so the () around the command is REQUIRED.
16992 The files can also be loaded dynamically while XEmacs is running, set the
16993 environment variable 'EMACSLOADPATH' to the installation bin directory
16994 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
16995 To start the interface enter the following command:
17009 You will prompted to enter the file name to be debugged.
17014 The command line options that are passed to the simulator directly are bound
17015 to default values in the file sdcdbsrc.el.
17016 The variables are listed below, these values maybe changed as required.
17019 sdcdbsrc-cpu-type '51
17022 sdcdbsrc-frequency '11059200
17025 sdcdbsrc-serial nil
17028 The following is a list of key mapping for the debugger interface.
17039 ;;key\SpecialChar ~
17053 binding\SpecialChar ~
17077 ;;---\SpecialChar ~
17091 -------\SpecialChar ~
17133 sdcdb-next-from-src\SpecialChar ~
17161 sdcdb-back-from-src\SpecialChar ~
17189 sdcdb-cont-from-src\SpecialChar ~
17199 SDCDB continue command
17217 sdcdb-step-from-src\SpecialChar ~
17245 sdcdb-whatis-c-sexp\SpecialChar ~
17255 SDCDB ptypecommand for data at
17322 sdcdbsrc-delete\SpecialChar ~
17336 SDCDB Delete all breakpoints if no arg
17385 given or delete arg (C-u arg x)
17403 sdcdbsrc-frame\SpecialChar ~
17418 SDCDB Display current frame if no arg,
17467 given or display frame arg
17534 sdcdbsrc-goto-sdcdb\SpecialChar ~
17544 Goto the SDCDB output buffer
17562 sdcdb-print-c-sexp\SpecialChar ~
17573 SDCDB print command for data at
17640 sdcdbsrc-goto-sdcdb\SpecialChar ~
17650 Goto the SDCDB output buffer
17668 sdcdbsrc-mode\SpecialChar ~
17684 Toggles Sdcdbsrc mode (turns it off)
17699 sdcdb-finish-from-src\SpecialChar ~
17707 SDCDB finish command
17722 sdcdb-break\SpecialChar ~
17740 Set break for line with point
17755 sdcdbsrc-mode\SpecialChar ~
17771 Toggle Sdcdbsrc mode
17786 sdcdbsrc-srcmode\SpecialChar ~
17809 Here are a few guidelines that will help the compiler generate more efficient
17810 code, some of the tips are specific to this compiler others are generally
17811 good programming practice.
17814 Use the smallest data type to represent your data-value.
17815 If it is known in advance that the value is going to be less than 256 then
17816 use an 'unsigned char' instead of a 'short' or 'int'.
17817 Please note, that ANSI C requires both signed and unsigned chars to be
17818 promoted to 'signed int' before doing any operation.
17819 This promotion can be omitted, if the result is the same.
17820 The effect of the promotion rules together with the sign-extension is often
17827 unsigned char uc = 0xfe;
17829 if (uc * uc < 0) /* this is true! */
17848 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
17858 (unsigned char) -12 / (signed char) -3 = ...
17861 No, the result is not 4:
17866 (int) (unsigned char) -12 / (int) (signed char) -3 =
17868 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
17870 (int) 0x00f4 / (int) 0xfffd =
17872 (int) 0x00f4 / (int) 0xfffd =
17874 (int) 244 / (int) -3 =
17876 (int) -81 = (int) 0xffaf;
17879 Don't complain, that gcc gives you a different result.
17880 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
17881 Therefore the results are different.
17884 \begin_inset Quotes sld
17888 \begin_inset Quotes srd
17894 If well-defined overflow characteristics are important and negative values
17895 are not, or if you want to steer clear of sign-extension problems when
17896 manipulating bits or bytes, use one of the corresponding unsigned types.
17897 (Beware when mixing signed and unsigned values in expressions, though.)
17899 Although character types (especially unsigned char) can be used as "tiny"
17900 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
17901 ble sign extension and increased code size.
17905 Use unsigned when it is known in advance that the value is not going to
17907 This helps especially if you are doing division or multiplication, bit-shifting
17908 or are using an array index.
17911 NEVER jump into a LOOP.
17914 Declare the variables to be local
17915 \begin_inset LatexCommand \index{local variables}
17919 whenever possible, especially loop control variables (induction).
17922 Since the compiler does not always do implicit integral promotion, the programme
17923 r should do an explicit cast when integral promotion is required.
17926 Reducing the size of division, multiplication & modulus operations can reduce
17927 code size substantially.
17928 Take the following code for example.
17934 foobar(unsigned int p1, unsigned char ch)
17942 unsigned char ch1 = p1 % ch ;
17953 For the modulus operation the variable ch will be promoted to unsigned int
17954 first then the modulus operation will be performed (this will lead to a
17955 call to support routine _moduint()), and the result will be casted to a
17957 If the code is changed to
17962 foobar(unsigned int p1, unsigned char ch)
17970 unsigned char ch1 = (unsigned char)p1 % ch ;
17981 It would substantially reduce the code generated (future versions of the
17982 compiler will be smart enough to detect such optimization opportunities).
17986 Have a look at the assembly listing to get a
17987 \begin_inset Quotes sld
17991 \begin_inset Quotes srd
17994 for the code generation.
17998 \begin_inset LatexCommand \index{Tools}
18002 included in the distribution
18006 \begin_inset Tabular
18007 <lyxtabular version="3" rows="12" columns="3">
18009 <column alignment="center" valignment="top" leftline="true" width="0pt">
18010 <column alignment="center" valignment="top" leftline="true" width="0pt">
18011 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18012 <row topline="true" bottomline="true">
18013 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18029 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18038 <row topline="true">
18039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18047 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18052 Simulator for various architectures
18055 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18064 <row topline="true">
18065 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18078 header file conversion
18081 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18086 sdcc/support/scripts
18090 <row topline="true">
18091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18104 header file conversion
18107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18112 sdcc/support/scripts
18116 <row topline="true">
18117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18151 <row topline="true">
18152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18186 <row topline="true">
18187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18221 <row topline="true">
18222 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18256 <row topline="true">
18257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18291 <row topline="true">
18292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18326 <row topline="true">
18327 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18361 <row topline="true" bottomline="true">
18362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18406 \begin_inset LatexCommand \index{Documentation}
18410 included in the distribution
18414 \begin_inset Tabular
18415 <lyxtabular version="3" rows="10" columns="2">
18417 <column alignment="left" valignment="top" leftline="true" width="0">
18418 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
18419 <row topline="true" bottomline="true">
18420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18433 Where to get / filename
18437 <row topline="true">
18438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18443 SDCC Compiler User Guide
18446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18451 You're reading it right now
18455 <row topline="true">
18456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18473 <row topline="true">
18474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18480 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
18485 \begin_inset LatexCommand \index{Assembler documentation}
18489 Assemblers and ASLINK
18490 \begin_inset LatexCommand \index{aslink}
18495 \begin_inset LatexCommand \index{Linker documentation}
18502 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18507 sdcc/as/doc/asxhtm.html
18511 <row topline="true">
18512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18517 SDCC regression test
18518 \begin_inset LatexCommand \index{Regression test}
18525 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18530 sdcc/doc/test_suite_spec.pdf
18534 <row topline="true">
18535 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18543 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18552 <row topline="true">
18553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18558 Notes on debugging with sdcdb
18559 \begin_inset LatexCommand \index{sdcdb (debugger)}
18566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18571 sdcc/debugger/README
18575 <row topline="true">
18576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18581 Software simulator for microcontrollers
18584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18611 <row topline="true">
18612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18617 Temporary notes on the pic16
18618 \begin_inset LatexCommand \index{PIC16}
18625 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18630 sdcc/src/pic16/NOTES
18634 <row topline="true" bottomline="true">
18635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18640 SDCC internal documentation (debugging file format)
18643 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18679 Related open source tools
18680 \begin_inset LatexCommand \index{Related tools}
18688 \begin_inset Tabular
18689 <lyxtabular version="3" rows="11" columns="3">
18691 <column alignment="center" valignment="top" leftline="true" width="0pt">
18692 <column alignment="block" valignment="top" leftline="true" width="30line%">
18693 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
18694 <row topline="true" bottomline="true">
18695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18720 <row topline="true">
18721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18727 \begin_inset LatexCommand \index{gpsim (pic simulator)}
18734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18748 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
18756 <row topline="true">
18757 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18763 \begin_inset LatexCommand \index{gputils (pic tools)}
18770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18778 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18784 \begin_inset LatexCommand \url{http://gputils.sourceforge.net/}
18792 <row topline="true">
18793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18809 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18815 \begin_inset LatexCommand \url{http://digilander.libero.it/fbradasc/FLP5.html}
18823 <row topline="true">
18824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18830 \begin_inset LatexCommand \index{indent (source formatting tool)}
18837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18842 Formats C source - Master of the white spaces
18845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18851 \begin_inset LatexCommand \url{http://home.hccnet.nl/d.ingamells/beautify.html}
18859 <row topline="true">
18860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18866 \begin_inset LatexCommand \index{srecord (tool)}
18873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18878 Object file conversion, checksumming, ...
18881 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18887 \begin_inset LatexCommand \url{http://srecord.sourceforge.net/}
18895 <row topline="true">
18896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18902 \begin_inset LatexCommand \index{objdump (tool)}
18909 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18914 Object file conversion, ...
18917 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18922 Part of binutils (should be there anyway)
18926 <row topline="true">
18927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18933 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
18940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18945 Source code documentation system
18948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18954 \begin_inset LatexCommand \url{http://www.doxygen.org}
18962 <row topline="true">
18963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18976 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
18979 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18985 \begin_inset LatexCommand \url{http://www.kdevelop.org}
18993 <row topline="true">
18994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19000 \begin_inset LatexCommand \index{splint (syntax checking tool)}
19007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19012 Statically checks c sources (has anyone adapted splint for SDCC?)
19015 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19021 \begin_inset LatexCommand \url{http://www.splint.org}
19029 <row topline="true" bottomline="true">
19030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19036 \begin_inset LatexCommand \index{ddd (debugger)}
19043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19048 Debugger, serves nicely as GUI to sdcdb
19049 \begin_inset LatexCommand \index{sdcdb (debugger)}
19056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19062 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
19079 Related documentation / recommended reading
19083 \begin_inset Tabular
19084 <lyxtabular version="3" rows="6" columns="3">
19086 <column alignment="center" valignment="top" leftline="true" width="0pt">
19087 <column alignment="block" valignment="top" leftline="true" width="30line%">
19088 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
19089 <row topline="true" bottomline="true">
19090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19115 <row topline="true">
19116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19133 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19139 \begin_inset LatexCommand \index{C Reference card}
19146 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19152 \begin_inset LatexCommand \url{http://www.refcards.com/about/c.html}
19160 <row topline="true">
19161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19169 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19177 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19183 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
19191 <row topline="true">
19192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19204 Latest datasheet of the target CPU
19207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19216 <row topline="true">
19217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19229 Revision history of datasheet
19232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19241 <row topline="true" bottomline="true">
19242 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19257 Advanced Compiler Design and Implementation
19260 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19265 bookstore (very dedicated, probably read other books first)
19281 Some questions answered, some pointers given - it might be time to in turn
19289 can you solve your project with the selected microcontroller? Would you
19290 find out early or rather late that your target is too small/slow/whatever?
19291 Can you switch to a slightly better device if it doesn't fit?
19294 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
19295 and/or another programming language be more adequate? Would an operating
19296 system on the target device help?
19299 if you solved the problem, will the marketing department be happy?
19302 if the marketing department is happy, will customers be happy?
19305 if you're the project manager, marketing department and maybe even the customer
19306 in one person, have you tried to see the project from the outside?
19309 is the project done if you think it is done? Or is just that other interface/pro
19310 tocol/feature/configuration/option missing? How about website, manual(s),
19311 internationali(z|s)ation, packaging, labels, 2nd source for components,
19312 electromagnetic compatability/interference, documentation for production,
19313 production test software, update mechanism, patent issues?
19316 is your project adequately positioned in that magic triangle: fame, fortune,
19320 Maybe not all answers to these questions are known and some answers may
19325 , nevertheless knowing these questions may help you to avoid burnout
19331 burnout is bad for electronic devices, programmers and motorcycle tyres
19335 Chances are you didn't want to hear some of them...
19339 \begin_inset LatexCommand \index{Support}
19346 SDCC has grown to be a large project.
19347 The compiler alone (without the preprocessor, assembler and linker) is
19348 well over 100,000 lines of code (blank stripped).
19349 The open source nature of this project is a key to its continued growth
19351 You gain the benefit and support of many active software developers and
19353 Is SDCC perfect? No, that's why we need your help.
19354 The developers take pride in fixing reported bugs.
19355 You can help by reporting the bugs and helping other SDCC users.
19356 There are lots of ways to contribute, and we encourage you to take part
19357 in making SDCC a great software package.
19361 The SDCC project is hosted on the SDCC sourceforge site at
19362 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
19367 You'll find the complete set of mailing lists
19368 \begin_inset LatexCommand \index{Mailing list(s)}
19372 , forums, bug reporting system, patch submission
19373 \begin_inset LatexCommand \index{Patch submission}
19378 \begin_inset LatexCommand \index{download}
19382 area and cvs code repository
19383 \begin_inset LatexCommand \index{cvs code repository}
19391 \begin_inset LatexCommand \index{Bug reporting}
19396 \begin_inset LatexCommand \index{Reporting bugs}
19403 The recommended way of reporting bugs is using the infrastructure of the
19405 You can follow the status of bug reports there and have an overview about
19409 Bug reports are automatically forwarded to the developer mailing list and
19410 will be fixed ASAP.
19411 When reporting a bug, it is very useful to include a small test program
19412 (the smaller the better) which reproduces the problem.
19413 If you can isolate the problem by looking at the generated assembly code,
19414 this can be very helpful.
19415 Compiling your program with the -
19426 \begin_inset LatexCommand \index{-\/-dumpall}
19430 option can sometimes be useful in locating optimization problems.
19431 When reporting a bug please maker sure you:
19434 Attach the code you are compiling with SDCC.
19438 Specify the exact command you use to run SDCC, or attach your Makefile.
19442 Specify the SDCC version (type "
19448 "), your platform, and operating system.
19452 Provide an exact copy of any error message or incorrect output.
19456 Put something meaningful in the subject of your message.
19459 Please attempt to include these 5 important parts, as applicable, in all
19460 requests for support or when reporting any problems or bugs with SDCC.
19461 Though this will make your message lengthy, it will greatly improve your
19462 chance that SDCC users and developers will be able to help you.
19463 Some SDCC developers are frustrated by bug reports without code provided
19464 that they can use to reproduce and ultimately fix the problem, so please
19465 be sure to provide sample code if you are reporting a bug!
19468 Please have a short check that you are using a recent version of SDCC and
19469 the bug is not yet known.
19470 This is the link for reporting bugs:
19471 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
19478 Requesting Features
19479 \begin_inset LatexCommand \label{sub:Requesting-Features}
19484 \begin_inset LatexCommand \index{Feature request}
19489 \begin_inset LatexCommand \index{Requesting features}
19496 Like bug reports feature requests are forwarded to the developer mailing
19498 This is the link for requesting features:
19499 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
19509 Like bug reports contributed patches are forwarded to the developer mailing
19511 This is the link for submitting patches
19512 \begin_inset LatexCommand \index{Patch submission}
19517 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
19524 You need to specify some parameters to the
19528 command for the patches to be useful.
19529 If you modified more than one file a patch created f.e.
19534 \begin_inset Quotes sld
19537 diff -Naur unmodified_directory modified_directory >my_changes.patch
19538 \begin_inset Quotes srd
19544 will be fine, otherwise
19548 \begin_inset Quotes sld
19551 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
19552 \begin_inset Quotes srd
19565 These links should take you directly to the
19566 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
19576 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
19577 automated messages (mid 2003)
19581 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
19586 \begin_inset LatexCommand \index{Mailing list(s)}
19590 and forums are archived and searchable so if you are lucky someone already
19591 had a similar problem.
19592 While mails to the lists themselves are delivered promptly their web front
19593 end on sourceforge sometimes shows a severe time lag (up to several weeks),
19594 if you're seriously using SDCC please consider subscribing to the lists.
19600 You can follow the status of the cvs version
19601 \begin_inset LatexCommand \index{version}
19605 of SDCC by watching the Changelog
19606 \begin_inset LatexCommand \index{Changelog}
19610 in the cvs-repository
19615 \begin_inset LatexCommand \htmlurl{http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
19623 \begin_inset LatexCommand \index{Release policy}
19630 Historically there often were long delays between official releases and
19631 the sourceforge download area tends to get not updated at all.
19632 Excuses in the past might have referred to problems with live range analysis,
19633 but as this was fixed a while ago, the current problem is that another
19634 excuse has to be found.
19635 Kidding aside, we have to get better there! On the other hand there are
19636 daily snapshots available at
19637 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
19641 , and you can always build the very last version (hopefully with many bugs
19642 fixed, and features added) from the source code available at
19643 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
19651 \begin_inset LatexCommand \index{Examples}
19658 You'll find some small examples in the directory
19660 sdcc/device/examples/.
19663 More examples and libraries are available at
19665 The SDCC Open Knowledge Resource
19666 \begin_inset LatexCommand \url{http://www.qsl.net/dl9sec/SDCC_OKR.html}
19673 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
19680 I did insert a reference to Paul's web site here although it seems rather
19681 dedicated to a specific 8032 board (I think it's okay because it f.e.
19682 shows LCD/Harddisc interface and has a free 8051 monitor.
19683 Independent 8032 board vendors face hard competition of heavily subsidized
19684 development boards anyway).
19687 Maybe we should include some links to real world applications.
19688 Preferably pointer to pointers (one for each architecture) so this stays
19693 \begin_inset LatexCommand \index{Quality control}
19700 The compiler is passed through nightly compile and build checks.
19706 \begin_inset LatexCommand \index{Regression test}
19710 check that SDCC itself compiles flawlessly on several platforms and checks
19711 the quality of the code generated by SDCC by running the code through simulator
19713 There is a separate document
19716 \begin_inset LatexCommand \index{Test suite}
19725 You'll find the test code in the directory
19727 sdcc/support/regression
19730 You can run these tests manually by running
19734 in this directory (or f.e.
19739 \begin_inset Quotes sld
19743 \begin_inset Quotes srd
19749 if you don't want to run the complete tests).
19750 The test code might also be interesting if you want to look for examples
19751 \begin_inset LatexCommand \index{Examples}
19755 checking corner cases of SDCC or if you plan to submit patches
19756 \begin_inset LatexCommand \index{Patch submission}
19763 The pic port uses a different set of regression tests, you'll find them
19766 sdcc/src/regression
19771 SDCC Technical Data
19775 \begin_inset LatexCommand \index{Optimizations}
19782 SDCC performs a host of standard optimizations in addition to some MCU specific
19787 Sub-expression Elimination
19788 \begin_inset LatexCommand \index{Subexpression elimination}
19795 The compiler does local and
19821 will be translated to
19833 Some subexpressions are not as obvious as the above example, e.g.:
19843 In this case the address arithmetic a->b[i] will be computed only once;
19844 the equivalent code in C would be.
19856 The compiler will try to keep these temporary variables in registers.
19859 Dead-Code Elimination
19860 \begin_inset LatexCommand \index{Dead-code elimination}
19881 i = 1; \SpecialChar ~
19890 global = 1;\SpecialChar ~
19903 global = 3;\SpecialChar ~
19928 \begin_inset LatexCommand \index{Copy propagation}
19984 Note: the dead stores created by this copy propagation will be eliminated
19985 by dead-code elimination.
19989 \begin_inset LatexCommand \index{Loop optimization}
19994 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
20001 Two types of loop optimizations are done by SDCC
20009 of loop induction variables.
20010 In addition to the strength reduction the optimizer marks the induction
20011 variables and the register allocator tries to keep the induction variables
20012 in registers for the duration of the loop.
20013 Because of this preference of the register allocator
20014 \begin_inset LatexCommand \index{Register allocation}
20018 , loop induction optimization causes an increase in register pressure, which
20019 may cause unwanted spilling of other temporary variables into the stack
20020 \begin_inset LatexCommand \index{stack}
20025 The compiler will generate a warning message when it is forced to allocate
20026 extra space either on the stack or data space.
20027 If this extra space allocation is undesirable then induction optimization
20028 can be eliminated either for the entire source file (with -
20038 -noinduction option) or for a given function only using #pragma\SpecialChar ~
20040 \begin_inset LatexCommand \index{\#pragma noinduction}
20053 for (i = 0 ; i < 100 ; i ++)
20069 for (i = 0; i < 100; i++)
20078 As mentioned previously some loop invariants are not as apparent, all static
20079 address computations are also moved out of the loop.
20084 \begin_inset LatexCommand \index{Strength reduction}
20088 , this optimization substitutes an expression by a cheaper expression:
20093 for (i=0;i < 100; i++)
20111 for (i=0;i< 100;i++) {
20117 ar[itemp1] = itemp2;
20134 The more expensive multiplication
20135 \begin_inset LatexCommand \index{Multiplication}
20139 is changed to a less expensive addition.
20143 \begin_inset LatexCommand \index{Loop reversing}
20150 This optimization is done to reduce the overhead of checking loop boundaries
20151 for every iteration.
20152 Some simple loops can be reversed and implemented using a
20153 \begin_inset Quotes eld
20156 decrement and jump if not zero
20157 \begin_inset Quotes erd
20161 SDCC checks for the following criterion to determine if a loop is reversible
20162 (note: more sophisticated compilers use data-dependency analysis to make
20163 this determination, SDCC uses a more simple minded analysis).
20166 The 'for' loop is of the form
20172 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
20182 The <for body> does not contain
20183 \begin_inset Quotes eld
20187 \begin_inset Quotes erd
20191 \begin_inset Quotes erd
20197 All goto's are contained within the loop.
20200 No function calls within the loop.
20203 The loop control variable <sym> is not assigned any value within the loop
20206 The loop control variable does NOT participate in any arithmetic operation
20210 There are NO switch statements in the loop.
20213 Algebraic Simplifications
20216 SDCC does numerous algebraic simplifications, the following is a small sub-set
20217 of these optimizations.
20222 i = j + 0;\SpecialChar ~
20226 /* changed to: */\SpecialChar ~
20232 i /= 2;\SpecialChar ~
20239 /* changed to: */\SpecialChar ~
20245 i = j - j;\SpecialChar ~
20249 /* changed to: */\SpecialChar ~
20255 i = j / 1;\SpecialChar ~
20259 /* changed to: */\SpecialChar ~
20266 Note the subexpressions
20267 \begin_inset LatexCommand \index{Subexpression}
20271 given above are generally introduced by macro expansions or as a result
20272 of copy/constant propagation.
20275 'switch' Statements
20276 \begin_inset LatexCommand \label{sub:'switch'-Statements}
20281 \begin_inset LatexCommand \index{switch statement}
20288 SDCC can optimize switch statements to jump tables
20289 \begin_inset LatexCommand \index{jump tables}
20294 It makes the decision based on an estimate of the generated code size.
20295 SDCC is quite liberal in the requirements for jump table generation:
20298 The labels need not be in order, and the starting number need not be one
20299 or zero, the case labels are in numerical sequence or not too many case
20300 labels are missing.
20306 switch(i) {\SpecialChar ~
20337 case 4: ...\SpecialChar ~
20369 case 5: ...\SpecialChar ~
20401 case 3: ...\SpecialChar ~
20432 case 6: ...\SpecialChar ~
20464 case 7: ...\SpecialChar ~
20496 case 8: ...\SpecialChar ~
20528 case 9: ...\SpecialChar ~
20560 case 10: ...\SpecialChar ~
20591 case 11: ...\SpecialChar ~
20658 Both the above switch statements will be implemented using a jump-table.
20659 The example to the right side is slightly more efficient as the check for
20660 the lower boundary of the jump-table is not needed.
20664 The number of case labels is not larger than supported by the target architectur
20668 If the case labels are not in numerical sequence ('gaps' between cases)
20669 SDCC checks whether a jump table with additionally inserted dummy cases
20670 is still attractive.
20674 If the starting number is not zero and a check for the lower boundary of
20675 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
20680 Switch statements which have large gaps in the numeric sequence or those
20681 that have too many case labels can be split into more than one switch statement
20682 for efficient code generation, e.g.:
20762 If the above switch statement is broken down into two switch statements
20852 then both the switch statements will be implemented using jump-tables whereas
20853 the unmodified switch statement will not be.
20856 There might be reasons which SDCC cannot know about to either favour or
20857 not favour jump tables.
20858 If the target system has to be as quick for the last switch case as for
20859 the first (pro jump table), or if the switch argument is known to be zero
20860 in the majority of the cases (contra jump table).
20863 The pragma nojtbound
20864 \begin_inset LatexCommand \index{\#pragma nojtbound}
20868 can be used to turn off checking the
20881 It has no effect if a default label is supplied.
20882 Use of this pragma is dangerous: if the switch argument is not matched
20883 by a case statement the processor will happily jump into Nirvana.
20886 Bit-shifting Operations
20887 \begin_inset LatexCommand \index{Bit shifting}
20894 Bit shifting is one of the most frequently used operation in embedded programmin
20896 SDCC tries to implement bit-shift operations in the most efficient way
20912 generates the following code:
20929 In general SDCC will never setup a loop if the shift count is known.
20972 \begin_inset LatexCommand \index{Bit rotation}
20979 A special case of the bit-shift operation is bit rotation
20980 \begin_inset LatexCommand \index{rotating bits}
20984 , SDCC recognizes the following expression to be a left bit-rotation:
20994 char i;\SpecialChar ~
21005 /* unsigned is needed for rotation */
21010 i = ((i << 1) | (i >> 7));
21019 will generate the following code:
21038 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
21039 ns of this case will also be recognized as bit-rotation, i.e.:
21044 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
21047 Nibble and Byte Swapping
21050 Other special cases of the bit-shift operations are nibble or byte swapping
21051 \begin_inset LatexCommand \index{swapping nibbles/bytes}
21055 , SDCC recognizes the following expressions:
21078 i = ((i << 4) | (i >> 4));
21084 j = ((j << 8) | (j >> 8));
21087 and generates a swap instruction for the nibble swapping
21088 \begin_inset LatexCommand \index{Nibble swapping}
21092 or move instructions for the byte swapping
21093 \begin_inset LatexCommand \index{Byte swapping}
21099 \begin_inset Quotes sld
21103 \begin_inset Quotes srd
21106 example can be used to convert from little to big-endian or vice versa.
21107 If you want to change the endianness of a
21111 integer you have to cast to
21118 Note that SDCC stores numbers in little-endian
21124 Usually 8-bit processors don't care much about endianness.
21125 This is not the case for the standard 8051 which only has an instruction
21131 \begin_inset LatexCommand \index{DPTR}
21139 so little-endian is the more efficient byte order.
21143 \begin_inset LatexCommand \index{little-endian}
21148 \begin_inset LatexCommand \index{Endianness}
21153 lowest order first).
21157 \begin_inset LatexCommand \index{Highest Order Bit}
21164 It is frequently required to obtain the highest order bit of an integral
21165 type (long, int, short or char types).
21166 SDCC recognizes the following expression to yield the highest order bit
21167 and generates optimized code for it, e.g.:
21189 hob = (gint >> 15) & 1;
21199 will generate the following code:
21232 000A E5*01\SpecialChar ~
21259 000C 23\SpecialChar ~
21290 000D 54 01\SpecialChar ~
21317 000F F5*02\SpecialChar ~
21345 Variations of this case however will
21350 It is a standard C expression, so I heartily recommend this be the only
21351 way to get the highest order bit, (it is portable).
21352 Of course it will be recognized even if it is embedded in other expressions,
21358 xyz = gint + ((gint >> 15) & 1);
21361 will still be recognized.
21365 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
21370 \begin_inset LatexCommand \index{Peephole optimizer}
21377 The compiler uses a rule based, pattern matching and re-writing mechanism
21378 for peep-hole optimization.
21383 a peep-hole optimizer by Christopher W.
21384 Fraser (cwfraser@microsoft.com).
21385 A default set of rules are compiled into the compiler, additional rules
21386 may be added with the
21399 \begin_inset LatexCommand \index{-\/-peep-file}
21406 The rule language is best illustrated with examples.
21430 The above rule will change the following assembly
21431 \begin_inset LatexCommand \index{Assembler routines}
21453 Note: All occurrences of a
21457 (pattern variable) must denote the same string.
21458 With the above rule, the assembly sequence:
21468 will remain unmodified.
21472 Other special case optimizations may be added by the user (via
21488 some variants of the 8051 MCU
21489 \begin_inset LatexCommand \index{MCS51 variants}
21502 The following two rules will change all
21521 replace { lcall %1 } by { acall %1 }
21523 replace { ljmp %1 } by { ajmp %1 }
21528 inline-assembler code
21530 is also passed through the peep hole optimizer, thus the peephole optimizer
21531 can also be used as an assembly level macro expander.
21532 The rules themselves are MCU dependent whereas the rule language infra-structur
21533 e is MCU independent.
21534 Peephole optimization rules for other MCU can be easily programmed using
21539 The syntax for a rule is as follows:
21544 rule := replace [ restart ] '{' <assembly sequence> '
21582 <assembly sequence> '
21600 '}' [if <functionName> ] '
21605 <assembly sequence> := assembly instruction (each instruction including
21606 labels must be on a separate line).
21610 The optimizer will apply to the rules one by one from the top in the sequence
21611 of their appearance, it will terminate when all rules are exhausted.
21612 If the 'restart' option is specified, then the optimizer will start matching
21613 the rules again from the top, this option for a rule is expensive (performance)
21614 , it is intended to be used in situations where a transformation will trigger
21615 the same rule again.
21616 An example of this (not a good one, it has side effects) is the following
21639 Note that the replace pattern cannot be a blank, but can be a comment line.
21640 Without the 'restart' option only the innermost 'pop' 'push' pair would
21641 be eliminated, i.e.:
21671 the restart option the rule will be applied again to the resulting code
21672 and then all the pop-push pairs will be eliminated to yield:
21682 A conditional function can be attached to a rule.
21683 Attaching rules are somewhat more involved, let me illustrate this with
21710 The optimizer does a look-up of a function name table defined in function
21715 in the source file SDCCpeeph.c, with the name
21720 If it finds a corresponding entry the function is called.
21721 Note there can be no parameters specified for these functions, in this
21726 is crucial, since the function
21730 expects to find the label in that particular variable (the hash table containin
21731 g the variable bindings is passed as a parameter).
21732 If you want to code more such functions, take a close look at the function
21733 labelInRange and the calling mechanism in source file SDCCpeeph.c.
21734 Currently implemented are
21736 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
21737 24bitMode, portIsDS390, 24bitModeAndPortDS390
21746 I know this whole thing is a little kludgey, but maybe some day we will
21747 have some better means.
21748 If you are looking at this file, you will see the default rules that are
21749 compiled into the compiler, you can add your own rules in the default set
21750 there if you get tired of specifying the -
21764 \begin_inset LatexCommand \index{ANSI-compliance}
21769 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
21776 Deviations from the compliance:
21779 functions are not always reentrant
21780 \begin_inset LatexCommand \index{reentrant}
21787 structures cannot be assigned values directly, cannot be passed as function
21788 parameters or assigned to each other and cannot be a return value from
21815 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
21826 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
21848 return rets;/* is invalid in SDCC although allowed in ANSI */
21855 \begin_inset LatexCommand \index{long long (not supported)}
21860 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
21868 \begin_inset LatexCommand \index{double (not supported)}
21872 ' precision floating point
21873 \begin_inset LatexCommand \index{Floating point support}
21880 No support for setjmp
21881 \begin_inset LatexCommand \index{setjmp (not supported)}
21886 \begin_inset LatexCommand \index{longjmp (not supported)}
21894 \begin_inset LatexCommand \index{K\&R style}
21898 function declarations are NOT allowed.
21904 foo(i,j) /* this old style of function declarations */
21906 int i,j; /* are valid in ANSI but not valid in SDCC */
21921 Cyclomatic Complexity
21922 \begin_inset LatexCommand \index{Cyclomatic complexity}
21929 Cyclomatic complexity of a function is defined as the number of independent
21930 paths the program can take during execution of the function.
21931 This is an important number since it defines the number test cases you
21932 have to generate to validate the function.
21933 The accepted industry standard for complexity number is 10, if the cyclomatic
21934 complexity reported by SDCC exceeds 10 you should think about simplification
21935 of the function logic.
21936 Note that the complexity level is not related to the number of lines of
21937 code in a function.
21938 Large functions can have low complexity, and small functions can have large
21944 SDCC uses the following formula to compute the complexity:
21949 complexity = (number of edges in control flow graph) - (number of nodes
21950 in control flow graph) + 2;
21954 Having said that the industry standard is 10, you should be aware that in
21955 some cases it be may unavoidable to have a complexity level of less than
21957 For example if you have switch statement with more than 10 case labels,
21958 each case label adds one to the complexity level.
21959 The complexity level is by no means an absolute measure of the algorithmic
21960 complexity of the function, it does however provide a good starting point
21961 for which functions you might look at for further optimization.
21964 Retargetting for other Processors
21967 The issues for retargetting the compiler are far too numerous to be covered
21969 What follows is a brief description of each of the seven phases of the
21970 compiler and its MCU dependency.
21973 Parsing the source and building the annotated parse tree.
21974 This phase is largely MCU independent (except for the language extensions).
21975 Syntax & semantic checks are also done in this phase, along with some initial
21976 optimizations like back patching labels and the pattern matching optimizations
21977 like bit-rotation etc.
21980 The second phase involves generating an intermediate code which can be easy
21981 manipulated during the later phases.
21982 This phase is entirely MCU independent.
21983 The intermediate code generation assumes the target machine has unlimited
21984 number of registers, and designates them with the name iTemp.
21985 The compiler can be made to dump a human readable form of the code generated
21999 This phase does the bulk of the standard optimizations and is also MCU independe
22001 This phase can be broken down into several sub-phases:
22005 Break down intermediate code (iCode) into basic blocks.
22007 Do control flow & data flow analysis on the basic blocks.
22009 Do local common subexpression elimination, then global subexpression elimination
22011 Dead code elimination
22015 If loop optimizations caused any changes then do 'global subexpression eliminati
22016 on' and 'dead code elimination' again.
22019 This phase determines the live-ranges; by live range I mean those iTemp
22020 variables defined by the compiler that still survive after all the optimization
22022 Live range analysis
22023 \begin_inset LatexCommand \index{Live range analysis}
22027 is essential for register allocation, since these computation determines
22028 which of these iTemps will be assigned to registers, and for how long.
22031 Phase five is register allocation.
22032 There are two parts to this process.
22036 The first part I call 'register packing' (for lack of a better term).
22037 In this case several MCU specific expression folding is done to reduce
22042 The second part is more MCU independent and deals with allocating registers
22043 to the remaining live ranges.
22044 A lot of MCU specific code does creep into this phase because of the limited
22045 number of index registers available in the 8051.
22048 The Code generation phase is (unhappily), entirely MCU dependent and very
22049 little (if any at all) of this code can be reused for other MCU.
22050 However the scheme for allocating a homogenized assembler operand for each
22051 iCode operand may be reused.
22054 As mentioned in the optimization section the peep-hole optimizer is rule
22055 based system, which can reprogrammed for other MCUs.
22059 \begin_inset LatexCommand \index{Compiler internals}
22066 The anatomy of the compiler
22067 \begin_inset LatexCommand \label{sub:The-anatomy-of}
22076 This is an excerpt from an article published in Circuit Cellar Magazine
22078 It's a little outdated (the compiler is much more efficient now and user/develo
22079 per friendly), but pretty well exposes the guts of it all.
22085 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
22086 It is fairly easy to retarget for other 8-bit MCU.
22087 Here we take a look at some of the internals of the compiler.
22092 \begin_inset LatexCommand \index{Parsing}
22099 Parsing the input source file and creating an AST (Annotated Syntax Tree
22100 \begin_inset LatexCommand \index{Annotated syntax tree}
22105 This phase also involves propagating types (annotating each node of the
22106 parse tree with type information) and semantic analysis.
22107 There are some MCU specific parsing rules.
22108 For example the storage classes, the extended storage classes are MCU specific
22109 while there may be a xdata storage class for 8051 there is no such storage
22110 class for z80 or Atmel AVR.
22111 SDCC allows MCU specific storage class extensions, i.e.
22112 xdata will be treated as a storage class specifier when parsing 8051 C
22113 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
22118 \begin_inset LatexCommand \index{iCode}
22125 Intermediate code generation.
22126 In this phase the AST is broken down into three-operand form (iCode).
22127 These three operand forms are represented as doubly linked lists.
22128 ICode is the term given to the intermediate form generated by the compiler.
22129 ICode example section shows some examples of iCode generated for some simple
22130 C source functions.
22134 \begin_inset LatexCommand \index{Optimizations}
22141 Bulk of the target independent optimizations is performed in this phase.
22142 The optimizations include constant propagation, common sub-expression eliminati
22143 on, loop invariant code movement, strength reduction of loop induction variables
22144 and dead-code elimination.
22147 Live range analysis
22148 \begin_inset LatexCommand \index{Live range analysis}
22155 During intermediate code generation phase, the compiler assumes the target
22156 machine has infinite number of registers and generates a lot of temporary
22158 The live range computation determines the lifetime of each of these compiler-ge
22159 nerated temporaries.
22160 A picture speaks a thousand words.
22161 ICode example sections show the live range annotations for each of the
22163 It is important to note here, each iCode is assigned a number in the order
22164 of its execution in the function.
22165 The live ranges are computed in terms of these numbers.
22166 The from number is the number of the iCode which first defines the operand
22167 and the to number signifies the iCode which uses this operand last.
22170 Register Allocation
22171 \begin_inset LatexCommand \index{Register allocation}
22178 The register allocation determines the type and number of registers needed
22180 In most MCUs only a few registers can be used for indirect addressing.
22181 In case of 8051 for example the registers R0 & R1 can be used to indirectly
22182 address the internal ram and DPTR to indirectly address the external ram.
22183 The compiler will try to allocate the appropriate register to pointer variables
22185 ICode example section shows the operands annotated with the registers assigned
22187 The compiler will try to keep operands in registers as much as possible;
22188 there are several schemes the compiler uses to do achieve this.
22189 When the compiler runs out of registers the compiler will check to see
22190 if there are any live operands which is not used or defined in the current
22191 basic block being processed, if there are any found then it will push that
22192 operand and use the registers in this block, the operand will then be popped
22193 at the end of the basic block.
22197 There are other MCU specific considerations in this phase.
22198 Some MCUs have an accumulator; very short-lived operands could be assigned
22199 to the accumulator instead of a general-purpose register.
22205 Figure II gives a table of iCode operations supported by the compiler.
22206 The code generation involves translating these operations into corresponding
22207 assembly code for the processor.
22208 This sounds overly simple but that is the essence of code generation.
22209 Some of the iCode operations are generated on a MCU specific manner for
22210 example, the z80 port does not use registers to pass parameters so the
22211 SEND and RECV iCode operations will not be generated, and it also does
22212 not support JUMPTABLES.
22219 <Where is Figure II?>
22222 In the original article Figure II was announced to be downloadable on
22227 Unfortunately it never seemed to have shown up there, so: where is Figure
22232 \begin_inset LatexCommand \index{iCode}
22239 This section shows some details of iCode.
22240 The example C code does not do anything useful; it is used as an example
22241 to illustrate the intermediate code generated by the compiler.
22253 /* This function does nothing useful.
22260 for the purpose of explaining iCode */
22263 short function (data int *x)
22271 short i=10; \SpecialChar ~
22273 /* dead initialization eliminated */
22278 short sum=10; /* dead initialization eliminated */
22291 while (*x) *x++ = *p++;
22305 /* compiler detects i,j to be induction variables */
22309 for (i = 0, j = 10 ; i < 10 ; i++, j
22335 mul += i * 3; \SpecialChar ~
22337 /* this multiplication remains */
22343 gint += j * 3;\SpecialChar ~
22345 /* this multiplication changed to addition */
22359 In addition to the operands each iCode contains information about the filename
22360 and line it corresponds to in the source file.
22361 The first field in the listing should be interpreted as follows:
22366 Filename(linenumber: iCode Execution sequence number : ICode hash table
22367 key : loop depth of the iCode).
22372 Then follows the human readable form of the ICode operation.
22373 Each operand of this triplet form can be of three basic types a) compiler
22374 generated temporary b) user defined variable c) a constant value.
22375 Note that local variables and parameters are replaced by compiler generated
22378 \begin_inset LatexCommand \index{Live range analysis}
22382 are computed only for temporaries (i.e.
22383 live ranges are not computed for global variables).
22385 \begin_inset LatexCommand \index{Register allocation}
22389 are allocated for temporaries only.
22390 Operands are formatted in the following manner:
22395 Operand Name [lr live-from : live-to ] { type information } [ registers
22401 As mentioned earlier the live ranges are computed in terms of the execution
22402 sequence number of the iCodes, for example
22404 the iTemp0 is live from (i.e.
22405 first defined in iCode with execution sequence number 3, and is last used
22406 in the iCode with sequence number 5).
22407 For induction variables such as iTemp21 the live range computation extends
22408 the lifetime from the start to the end of the loop.
22410 The register allocator used the live range information to allocate registers,
22411 the same registers may be used for different temporaries if their live
22412 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
22413 iTemp17 since their live ranges do not overlap.
22414 In addition the allocator also takes into consideration the type and usage
22415 of a temporary, for example itemp6 is a pointer to near space and is used
22416 as to fetch data from (i.e.
22417 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
22418 Some short lived temporaries are allocated to special registers which have
22419 meaning to the code generator e.g.
22420 iTemp13 is allocated to a pseudo register CC which tells the back end that
22421 the temporary is used only for a conditional jump the code generation makes
22422 use of this information to optimize a compare and jump ICode.
22424 There are several loop optimizations
22425 \begin_inset LatexCommand \index{Loop optimization}
22429 performed by the compiler.
22430 It can detect induction variables iTemp21(i) and iTemp23(j).
22431 Also note the compiler does selective strength reduction
22432 \begin_inset LatexCommand \index{Strength reduction}
22437 the multiplication of an induction variable in line 18 (gint = j * 3) is
22438 changed to addition, a new temporary iTemp17 is allocated and assigned
22439 a initial value, a constant 3 is then added for each iteration of the loop.
22440 The compiler does not change the multiplication
22441 \begin_inset LatexCommand \index{Multiplication}
22445 in line 17 however since the processor does support an 8 * 8 bit multiplication.
22447 Note the dead code elimination
22448 \begin_inset LatexCommand \index{Dead-code elimination}
22452 optimization eliminated the dead assignments in line 7 & 8 to I and sum
22460 Sample.c (5:1:0:0) _entry($9) :
22465 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
22470 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
22475 Sample.c(11:4:53:0) preHeaderLbl0($11) :
22480 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
22486 Sample.c(11:6:5:1) _whilecontinue_0($1) :
22491 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
22497 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22502 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
22508 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
22514 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
22520 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
22526 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
22527 * int}[r0] + 0x2 {short}
22532 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
22537 Sample.c(11:17:21:0)_whilebreak_0($3) :
22542 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
22547 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
22552 Sample.c(15:20:54:0)preHeaderLbl1($13) :
22557 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
22562 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
22567 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
22572 Sample.c(15:24:26:1)_forcond_0($4) :
22577 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
22583 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
22588 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
22589 + ITemp21 [lr21:38]{short}[r4]
22594 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
22600 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
22601 + iTemp15 [lr29:30]{short}[r1]
22606 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
22612 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
22618 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
22624 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
22630 Sample.c(19:38:47:1) goto _forcond_0($4)
22635 Sample.c(19:39:48:0)_forbreak_0($7) :
22640 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
22641 + ITemp11 [lr19:40]{short}[r3]
22646 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
22651 Sample.c(20:42:51:0)_return($8) :
22656 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
22662 Finally the code generated for this function:
22703 ; ----------------------------------------------
22708 ; function function
22713 ; ----------------------------------------------
22723 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
22735 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
22747 ;_whilecontinue_0($1) :
22757 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
22762 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
22821 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
22840 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
22887 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
22927 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
22953 ; iTemp6 [lr5:16]{_near * int}[r0] =
22958 ; iTemp6 [lr5:16]{_near * int}[r0] +
22975 ; goto _whilecontinue_0($1)
22987 ; _whilebreak_0($3) :
22997 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
23009 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
23021 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
23033 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
23052 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
23081 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
23086 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
23131 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
23136 ; iTemp21 [lr21:38]{short}[r4]
23162 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
23195 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
23200 ; iTemp15 [lr29:30]{short}[r1]
23219 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
23266 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
23313 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
23325 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
23339 cjne r5,#0xff,00104$
23351 ; goto _forcond_0($4)
23363 ; _forbreak_0($7) :
23373 ; ret iTemp24 [lr40:41]{short}
23416 A few words about basic block successors, predecessors and dominators
23419 Successors are basic blocks
23420 \begin_inset LatexCommand \index{Basic blocks}
23424 that might execute after this basic block.
23426 Predecessors are basic blocks that might execute before reaching this basic
23429 Dominators are basic blocks that WILL execute before reaching this basic
23463 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
23466 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
23469 c) domVect of [BB4] = BB1 ...
23470 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
23478 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
23488 Thanks to all the other volunteer developers who have helped with coding,
23489 testing, web-page creation, distribution sets, etc.
23490 You know who you are :-)
23497 This document was initially written by Sandeep Dutta
23500 All product names mentioned herein may be trademarks
23501 \begin_inset LatexCommand \index{Trademarks}
23505 of their respective companies.
23512 To avoid confusion, the installation and building options for SDCC itself
23513 (chapter 2) are not part of the index.
23517 \begin_inset LatexCommand \printindex{}