1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={8032 8051 ansi c compiler CPU DS390
11 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by cvs
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 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
352 \begin_inset LatexCommand \index{Compatibility with previous versions}
359 This version has numerous bug fixes compared with the previous version.
360 But we also introduced some incompatibilities with older versions.
361 Not just for the fun of it, but to make the compiler more stable, efficient
363 \begin_inset LatexCommand \index{ANSI-compliance}
368 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
372 for ANSI-Compliance).
378 short is now equivalent to int (16 bits), it used to be equivalent to char
379 (8 bits) which is not ANSI compliant.
382 the default directory for gcc-builds where include, library and documentation
383 files are stored is now in /usr/local/share.
386 char type parameters to vararg functions are casted to int unless explicitly
403 will push a as an int and as a char resp.
416 -regextend has been removed.
429 -noregparms has been removed.
442 -stack-after-data has been removed.
446 \begin_inset LatexCommand \index{bit}
451 \begin_inset LatexCommand \index{sbit}
456 \begin_inset LatexCommand \index{\_\_sbit}
460 types now consistently behave like the C99 _Bool type with respect to type
462 \begin_inset LatexCommand \index{type conversion}
467 \begin_inset LatexCommand \index{type promotion}
472 The most common incompatibility resulting from this change is related to
474 \begin_inset LatexCommand \index{Bit toggling}
488 b = ~b; /* equivalent to b=1 instead of toggling b */
492 b = !b; /* toggles b */
496 In previous versions, both forms would have toggled the bit.
501 <pending: more incompatibilities?>
507 What do you need before you start installation of SDCC? A computer, and
509 The preferred method of installation is to compile SDCC from source using
511 For Windows some pre-compiled binary distributions are available for your
513 You should have some experience with command line tools and compiler use.
519 The SDCC home page at
520 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
524 is a great place to find distribution sets.
525 You can also find links to the user mailing lists that offer help or discuss
526 SDCC with other SDCC users.
527 Web links to other SDCC related sites can also be found here.
528 This document can be found in the DOC directory of the source package as
530 A pdf version of this document is available at
531 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
536 Some of the other tools (simulator and assembler) included with SDCC contain
537 their own documentation and can be found in the source distribution.
538 If you want the latest unreleased software, the complete source package
539 is available directly by anonymous CVS on cvs.sdcc.sourceforge.net.
542 Wishes for the future
545 There are (and always will be) some things that could be done.
546 Here are some I can think of:
553 char KernelFunction3(char p) at 0x340;
561 \begin_inset LatexCommand \index{code banking (not supported)}
571 If you can think of some more, please see the section
572 \begin_inset LatexCommand \ref{sub:Requesting-Features}
576 about filing feature requests
577 \begin_inset LatexCommand \index{Requesting features}
582 \begin_inset LatexCommand \index{Feature request}
592 \begin_inset LatexCommand \index{Installation}
599 For most users it is sufficient to skip to either section
600 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
605 \begin_inset LatexCommand \ref{sub:Windows-Install}
610 More detailled instructions follow below.
614 \begin_inset LatexCommand \index{Options SDCC configuration}
621 The install paths, search paths and other options are defined when running
623 The defaults can be overridden by:
625 \labelwidthstring 00.00.0000
637 -prefix see table below
639 \labelwidthstring 00.00.0000
651 -exec_prefix see table below
653 \labelwidthstring 00.00.0000
665 -bindir see table below
667 \labelwidthstring 00.00.0000
679 -datadir see table below
681 \labelwidthstring 00.00.0000
683 docdir environment variable, see table below
685 \labelwidthstring 00.00.0000
687 include_dir_suffix environment variable, see table below
689 \labelwidthstring 00.00.0000
691 lib_dir_suffix environment variable, see table below
693 \labelwidthstring 00.00.0000
695 sdccconf_h_dir_separator environment variable, either / or
700 This character will only be used in sdccconf.h; don't forget it's a C-header,
701 therefore a double-backslash is needed there.
703 \labelwidthstring 00.00.0000
715 -disable-mcs51-port Excludes the Intel mcs51 port
717 \labelwidthstring 00.00.0000
729 -disable-gbz80-port Excludes the Gameboy gbz80 port
731 \labelwidthstring 00.00.0000
743 -disable-z80-port Excludes the z80 port
745 \labelwidthstring 00.00.0000
757 -disable-avr-port Excludes the AVR port
759 \labelwidthstring 00.00.0000
771 -disable-ds390-port Excludes the DS390 port
773 \labelwidthstring 00.00.0000
785 -disable-hc08-port Excludes the HC08 port
787 \labelwidthstring 00.00.0000
799 -disable-pic-port Excludes the PIC port
801 \labelwidthstring 00.00.0000
813 -disable-xa51-port Excludes the XA51 port
815 \labelwidthstring 00.00.0000
827 -disable-ucsim Disables configuring and building of ucsim
829 \labelwidthstring 00.00.0000
841 -disable-device-lib-build Disables automatically building device libraries
843 \labelwidthstring 00.00.0000
855 -disable-packihx Disables building packihx
857 \labelwidthstring 00.00.0000
869 -enable-libgc Use the Bohem memory allocator.
870 Lower runtime footprint.
873 Furthermore the environment variables CC, CFLAGS, ...
874 the tools and their arguments can be influenced.
875 Please see `configure -
885 -help` and the man/info pages of `configure` for details.
889 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
890 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
891 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
892 At the moment it's not possible to change the default settings (it was
893 simply never required).
897 These configure options are compiled into the binaries, and can only be
898 changed by rerunning 'configure' and recompiling SDCC.
899 The configure options are written in
903 to distinguish them from run time environment variables (see section search
909 \begin_inset Quotes sld
913 \begin_inset Quotes srd
916 are used by the SDCC team to build the official Win32 binaries.
917 The SDCC team uses Mingw32 to build the official Windows binaries, because
924 a gcc compiler and last but not least
927 the binaries can be built by cross compiling on Sourceforge's compile farm.
930 See the examples, how to pass the Win32 settings to 'configure'.
931 The other Win32 builds using Borland, VC or whatever don't use 'configure',
932 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
944 <lyxtabular version="3" rows="8" columns="3">
946 <column alignment="block" valignment="top" leftline="true" width="0in">
947 <column alignment="block" valignment="top" leftline="true" width="0in">
948 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
949 <row topline="true" bottomline="true">
950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1005 <row topline="true">
1006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1037 <row topline="true">
1038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1075 <row topline="true">
1076 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1109 <row topline="true">
1110 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1147 <row topline="true">
1148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1175 <row topline="true" bottomline="true">
1176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1194 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1212 'configure' also computes relative paths.
1213 This is needed for full relocatability of a binary package and to complete
1214 search paths (see section search paths below):
1220 \begin_inset Tabular
1221 <lyxtabular version="3" rows="4" columns="3">
1223 <column alignment="block" valignment="top" leftline="true" width="0in">
1224 <column alignment="block" valignment="top" leftline="true" width="0in">
1225 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1226 <row topline="true" bottomline="true">
1227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1243 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1252 <row topline="true" bottomline="true">
1253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1280 <row bottomline="true">
1281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1308 <row bottomline="true">
1309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1360 \begin_inset Quotes srd
1364 \begin_inset Quotes srd
1378 \begin_inset Quotes srd
1382 \begin_inset Quotes srd
1410 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1419 \begin_inset Quotes srd
1422 i586-mingw32msvc-gcc
1423 \begin_inset Quotes srd
1427 \begin_inset Quotes srd
1430 i586-mingw32msvc-g++
1431 \begin_inset Quotes srd
1439 \begin_inset Quotes srd
1442 i586-mingw32msvc-ranlib
1443 \begin_inset Quotes srd
1451 \begin_inset Quotes srd
1454 i586-mingw32msvc-strip
1455 \begin_inset Quotes srd
1473 \begin_inset Quotes srd
1477 \begin_inset Quotes srd
1495 \begin_inset Quotes srd
1499 \begin_inset Quotes srd
1507 \begin_inset Quotes srd
1511 \begin_inset Quotes srd
1519 \begin_inset Quotes srd
1523 \begin_inset Quotes srd
1531 \begin_inset Quotes srd
1535 \begin_inset Quotes srd
1542 sdccconf_h_dir_separator=
1543 \begin_inset Quotes srd
1555 \begin_inset Quotes srd
1572 -disable-device-lib-build
1600 -host=i586-mingw32msvc -
1610 -build=unknown-unknown-linux-gnu
1614 \begin_inset Quotes sld
1618 \begin_inset Quotes srd
1621 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1630 \begin_inset Quotes srd
1634 \begin_inset Quotes srd
1642 \begin_inset Quotes srd
1646 \begin_inset Quotes srd
1664 \begin_inset Quotes srd
1668 \begin_inset Quotes srd
1686 \begin_inset Quotes srd
1690 \begin_inset Quotes srd
1698 \begin_inset Quotes srd
1702 \begin_inset Quotes srd
1710 \begin_inset Quotes srd
1714 \begin_inset Quotes srd
1722 \begin_inset Quotes srd
1726 \begin_inset Quotes srd
1733 sdccconf_h_dir_separator=
1734 \begin_inset Quotes srd
1746 \begin_inset Quotes srd
1766 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1777 -C' turns on caching, which gives a little bit extra speed.
1778 However if options are changed, it can be necessary to delete the config.cache
1783 \begin_inset LatexCommand \label{sub:Install-paths}
1788 \begin_inset LatexCommand \index{Install paths}
1794 \added_space_top medskip \align center
1796 \begin_inset Tabular
1797 <lyxtabular version="3" rows="5" columns="4">
1799 <column alignment="center" valignment="top" leftline="true" width="0">
1800 <column alignment="center" valignment="top" leftline="true" width="0">
1801 <column alignment="center" valignment="top" leftline="true" width="0">
1802 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1803 <row topline="true" bottomline="true">
1804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1845 <row topline="true">
1846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1885 <row topline="true">
1886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1894 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1901 $DATADIR/ $INCLUDE_DIR_SUFFIX
1904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1909 /usr/local/share/sdcc/include
1912 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1925 <row topline="true">
1926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1941 $DATADIR/$LIB_DIR_SUFFIX
1944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1949 /usr/local/share/sdcc/lib
1952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1965 <row topline="true" bottomline="true">
1966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1989 /usr/local/share/sdcc/doc
1992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2014 *compiler, preprocessor, assembler, and linker
2020 is auto-appended by the compiler, e.g.
2021 small, large, z80, ds390 etc
2024 The install paths can still be changed during `make install` with e.g.:
2027 make install prefix=$(HOME)/local/sdcc
2030 Of course this doesn't change the search paths compiled into the binaries.
2034 Moreover the install path can be changed by defining DESTDIR
2035 \begin_inset LatexCommand \index{DESTDIR}
2042 make install DESTDIR=$(HOME)/sdcc.rpm/
2045 Please note that DESTDIR must have a trailing slash!
2049 \begin_inset LatexCommand \label{sub:Search-Paths}
2054 \begin_inset LatexCommand \index{Search path}
2061 Some search paths or parts of them are determined by configure variables
2066 , see section above).
2067 Further search paths are determined by environment variables during runtime.
2070 The paths searched when running the compiler are as follows (the first catch
2076 Binary files (preprocessor, assembler and linker)
2082 \begin_inset Tabular
2083 <lyxtabular version="3" rows="4" columns="3">
2085 <column alignment="block" valignment="top" leftline="true" width="0in">
2086 <column alignment="block" valignment="top" leftline="true" width="0in">
2087 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2088 <row topline="true" bottomline="true">
2089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2114 <row topline="true">
2115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2144 <row topline="true">
2145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2150 Path of argv[0] (if available)
2153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2170 <row topline="true" bottomline="true">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2187 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 \begin_inset Tabular
2213 <lyxtabular version="3" rows="6" columns="3">
2215 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2216 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2217 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2218 <row topline="true" bottomline="true">
2219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2244 <row topline="true">
2245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2300 <row topline="true">
2301 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2326 <row topline="true">
2327 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2364 <row topline="true">
2365 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2446 <row topline="true" bottomline="true">
2447 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2468 /usr/local/share/sdcc/
2473 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2501 -nostdinc disables the last two search paths.
2511 With the exception of
2512 \begin_inset Quotes sld
2526 \begin_inset Quotes srd
2533 is auto-appended by the compiler (e.g.
2534 small, large, z80, ds390 etc.).
2541 \begin_inset Tabular
2542 <lyxtabular version="3" rows="6" columns="3">
2544 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2545 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2546 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2547 <row topline="true" bottomline="true">
2548 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2556 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2564 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2573 <row topline="true">
2574 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2629 <row topline="true">
2630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2642 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2669 <row topline="true">
2670 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2681 $LIB_DIR_SUFFIX/<model>
2684 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2698 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2715 <row topline="true">
2716 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2731 $LIB_DIR_SUFFIX/<model>
2734 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2787 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2843 <row topline="true" bottomline="true">
2844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2853 $LIB_DIR_SUFFIX/<model>
2856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2861 /usr/local/share/sdcc/
2868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2886 Don't delete any of the stray spaces in the table above without checking
2887 the HTML output (last line)!
2903 -nostdlib disables the last two search paths.
2907 \begin_inset LatexCommand \index{Building SDCC}
2914 Building SDCC on Linux
2915 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2924 Download the source package
2926 either from the SDCC CVS repository or from the nightly snapshots
2928 , it will be named something like sdcc
2939 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2948 Bring up a command line terminal, such as xterm.
2953 Unpack the file using a command like:
2956 "tar -xvzf sdcc.src.tar.gz
2961 , this will create a sub-directory called sdcc with all of the sources.
2964 Change directory into the main SDCC directory, for example type:
2981 This configures the package for compilation on your system.
2997 All of the source packages will compile, this can take a while.
3013 This copies the binary executables, the include files, the libraries and
3014 the documentation to the install directories.
3015 Proceed with section
3016 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3023 Building SDCC on OSX 2.x
3026 Follow the instruction for Linux.
3030 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3031 )) fails to compile SDCC.
3032 Fortunately there's also gcc 2.9.x installed, which works fine.
3033 This compiler can be selected by running 'configure' with:
3036 ./configure CC=gcc2 CXX=g++2
3039 Cross compiling SDCC on Linux for Windows
3042 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3043 See section 'Configure Options'.
3046 Building SDCC on Windows
3049 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3051 They use Unix-sockets, which are not available on Win32.
3054 Building SDCC using Cygwin and Mingw32
3057 For building and installing a Cygwin executable follow the instructions
3063 \begin_inset Quotes sld
3067 \begin_inset Quotes srd
3070 Win32-binary can be built, which will not need the Cygwin-DLL.
3071 For the necessary 'configure' options see section 'configure options' or
3072 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3076 In order to install Cygwin on Windows download setup.exe from
3077 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3083 \begin_inset Quotes sld
3086 default text file type
3087 \begin_inset Quotes srd
3091 \begin_inset Quotes sld
3095 \begin_inset Quotes srd
3098 and download/install at least the following packages.
3099 Some packages are selected by default, others will be automatically selected
3100 because of dependencies with the manually selected packages.
3101 Never deselect these packages!
3110 gcc ; version 3.x is fine, no need to use the old 2.9x
3113 binutils ; selected with gcc
3119 rxvt ; a nice console, which makes life much easier under windoze (see below)
3122 man ; not really needed for building SDCC, but you'll miss it sooner or
3126 less ; not really needed for building SDCC, but you'll miss it sooner or
3130 cvs ; only if you use CVS access
3133 If you want to develop something you'll need:
3136 python ; for the regression tests
3139 gdb ; the gnu debugger, together with the nice GUI
3140 \begin_inset Quotes sld
3144 \begin_inset Quotes srd
3150 openssh ; to access the CF or commit changes
3153 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3154 use autoconf-stable!
3157 rxvt is a nice console with history.
3158 Replace in your cygwin.bat the line
3177 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3180 -bg black -fg white -geometry 100x65 -e bash -
3193 Text selected with the mouse is automatically copied to the clipboard, pasting
3194 works with shift-insert.
3198 The other good tip is to make sure you have no //c/-style paths anywhere,
3199 use /cygdrive/c/ instead.
3200 Using // invokes a network lookup which is very slow.
3202 \begin_inset Quotes sld
3206 \begin_inset Quotes srd
3209 is too long, you can change it with e.g.
3215 SDCC sources use the unix line ending LF.
3216 Life is much easier, if you store the source tree on a drive which is mounted
3218 And use an editor which can handle LF-only line endings.
3219 Make sure not to commit files with windows line endings.
3220 The tabulator spacing
3221 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3225 used in the project is 8.
3226 Although a tabulator spacing of 8 is a sensible choice for programmers
3227 (it's a power of 2 and allows to display 8/16 bit signed variables without
3228 loosing columns) the plan is to move towards using only spaces in the source.
3231 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3236 Download the source package
3238 either from the SDCC CVS repository or from the
3239 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3245 , it will be named something like sdcc
3252 SDCC is distributed with all the projects, workspaces, and files you need
3253 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3254 doesn't build under MSVC).
3255 The workspace name is 'sdcc.dsw'.
3256 Please note that as it is now, all the executables are created in a folder
3260 Once built you need to copy the executables from sdcc
3264 bin before running SDCC.
3269 WARNING: Visual studio is very picky with line terminations; it expects
3270 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3271 If you are getting a message such as "This makefile was not generated by
3272 Developer Studio etc.
3274 \begin_inset Quotes srd
3277 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3278 need to convert the Unix style line endings to DOS style line endings.
3279 To do so you can use the
3280 \begin_inset Quotes sld
3284 \begin_inset Quotes srd
3287 utility freely available on the internet.
3288 Doug Hawkins reported in the sdcc-user list that this works:
3296 SDCC> unix2dos sdcc.dsw
3302 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3306 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3307 flex.exe, and gawk.exe.
3308 One good place to get them is
3309 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3317 Download the file UnxUtils
3318 \begin_inset LatexCommand \index{UnxUtils}
3323 Now you have to install the utilities and setup MSVC so it can locate the
3325 Here there are two alternatives (choose one!):
3332 a) Extract UnxUtils.zip to your C:
3334 hard disk PRESERVING the original paths, otherwise bison won't work.
3335 (If you are using WinZip make certain that 'Use folder names' is selected)
3339 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3340 in 'Show directories for:' select 'Executable files', and in the directories
3341 window add a new path: 'C:
3351 (As a side effect, you get a bunch of Unix utilities that could be useful,
3352 such as diff and patch.)
3359 This one avoids extracting a bunch of files you may not use, but requires
3364 a) Create a directory were to put the tools needed, or use a directory already
3372 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3373 to such directory WITHOUT preserving the original paths.
3374 (If you are using WinZip make certain that 'Use folder names' is not selected)
3378 c) Rename bison.exe to '_bison.exe'.
3382 d) Create a batch file 'bison.bat' in 'C:
3386 ' and add these lines:
3406 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3410 Steps 'c' and 'd' are needed because bison requires by default that the
3411 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3412 '/usr/local/share/' I think.
3413 So it is necessary to tell bison where those files are located if they
3414 are not in such directory.
3415 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3419 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3420 in 'Show directories for:' select 'Executable files', and in the directories
3421 window add a new path: 'c:
3424 Note that you can use any other path instead of 'c:
3426 util', even the path where the Visual C++ tools are, probably: 'C:
3430 Microsoft Visual Studio
3435 So you don't have to execute step 'e' :)
3439 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3440 the executables from sdcc
3444 bin, and you can compile using SDCC.
3447 Building SDCC Using Borland
3450 From the sdcc directory, run the command "make -f Makefile.bcc".
3451 This should regenerate all the .exe files in the bin directory except for
3452 sdcdb.exe (which currently doesn't build under Borland C++).
3455 If you modify any source files and need to rebuild, be aware that the dependenci
3456 es may not be correctly calculated.
3457 The safest option is to delete all .obj files and run the build again.
3458 From a Cygwin BASH prompt, this can easily be done with the command (be
3459 sure you are in the sdcc directory):
3469 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3471 ) -print -exec rm {}
3480 or on Windows NT/2000/XP from the command prompt with the command:
3487 del /s *.obj *.lib *.rul
3490 from the sdcc directory.
3493 Windows Install Using a ZIP Package
3496 Download the binary zip package from
3497 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3501 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3502 This should unpack to a group of sub-directories.
3503 An example directory structure after unpacking the mingw32 package is:
3508 bin for the executables, c:
3516 lib for the include and libraries.
3519 Adjust your environment variable PATH to include the location of the bin
3520 directory or start sdcc using the full path.
3523 Windows Install Using the Setup Program
3524 \begin_inset LatexCommand \label{sub:Windows-Install}
3531 Download the setup program
3533 sdcc-x.y.z-setup.exe
3535 for an official release from
3538 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3542 or a setup program for one of the snapshots
3544 sdcc_yyyymmdd_setup.exe
3547 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3552 A windows typical installer will guide you through the installation process.
3555 Building the Documentation
3558 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3559 as changing into the doc directory and typing
3563 \begin_inset Quotes srd
3567 \begin_inset Quotes srd
3574 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3577 \begin_inset LatexCommand \url{http://www.lyx.org}
3581 as editor this is straightforward.
3582 Prebuilt documentation in html and pdf format is available from
3583 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3590 Reading the Documentation
3593 Currently reading the document in pdf format is recommended, as for unknown
3594 reason the hyperlinks are working there whereas in the html version they
3601 If you should know why please drop us a note
3607 You'll find the pdf version
3608 \begin_inset LatexCommand \index{PDF version of this document}
3613 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3621 \begin_inset LatexCommand \index{HTML version of this document}
3626 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3632 This documentation is in some aspects different from a commercial documentation:
3636 It tries to document SDCC for several processor architectures in one document
3637 (commercially these probably would be separate documents/products).
3639 \begin_inset LatexCommand \index{Status of documentation}
3643 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3645 Z80, PIC14, PIC16 and HC08.
3648 There are many references pointing away from this documentation.
3649 Don't let this distract you.
3651 was a reference like
3652 \begin_inset LatexCommand \url{http://www.opencores.org}
3656 together with a statement
3657 \begin_inset Quotes sld
3660 some processors which are targetted by SDCC can be implemented in a
3677 \begin_inset LatexCommand \index{fpga (field programmable gate array)}
3682 \begin_inset Quotes srd
3685 we expect you to have a quick look there and come back.
3686 If you read this you are on the right track.
3689 Some sections attribute more space to problems, restrictions and warnings
3690 than to the solution.
3693 The installation section and the section about the debugger is intimidating.
3696 There are still lots of typos and there are more different writing styles
3700 Testing the SDCC Compiler
3701 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3708 The first thing you should do after installing your SDCC compiler is to
3724 \begin_inset LatexCommand \index{version}
3731 at the prompt, and the program should run and tell you the version.
3732 If it doesn't run, or gives a message about not finding sdcc program, then
3733 you need to check over your installation.
3734 Make sure that the sdcc bin directory is in your executable search path
3735 defined by the PATH environment setting (
3740 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3747 Install trouble-shooting for suggestions
3750 Make sure that the sdcc program is in the bin folder, if not perhaps something
3751 did not install correctly.
3759 is commonly installed as described in section
3760 \begin_inset Quotes sld
3763 Install and search paths
3764 \begin_inset Quotes srd
3773 Make sure the compiler works on a very simple example.
3774 Type in the following test.c program using your favorite
3800 Compile this using the following command:
3809 If all goes well, the compiler will generate a test.asm and test.rel file.
3810 Congratulations, you've just compiled your first program with SDCC.
3811 We used the -c option to tell SDCC not to link the generated code, just
3812 to keep things simple for this step.
3820 The next step is to try it with the linker.
3830 If all goes well the compiler will link with the libraries and produce
3831 a test.ihx output file.
3836 (no test.ihx, and the linker generates warnings), then the problem is most
3845 usr/local/share/sdcc/lib directory
3852 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3859 Install trouble-shooting for suggestions).
3867 The final test is to ensure
3875 header files and libraries.
3876 Edit test.c and change it to the following:
3893 strcpy(str1, "testing");
3900 Compile this by typing
3907 This should generate a test.ihx output file, and it should give no warnings
3908 such as not finding the string.h file.
3909 If it cannot find the string.h file, then the problem is that
3913 cannot find the /usr/local/share/sdcc/include directory
3920 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3927 Install trouble-shooting section for suggestions).
3945 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3949 to find exactly where SDCC is looking for the include and lib files.
3952 Install Trouble-shooting
3953 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3958 \begin_inset LatexCommand \index{Install trouble-shooting}
3965 If SDCC does not build correctly
3968 A thing to try is starting from scratch by unpacking the .tgz source package
3969 again in an empty directory.
3977 ./configure 2>&1 | tee configure.log
3991 make 2>&1 | tee make.log
3998 If anything goes wrong, you can review the log files to locate the problem.
3999 Or a relevant part of this can be attached to an email that could be helpful
4000 when requesting help from the mailing list.
4004 \begin_inset Quotes sld
4008 \begin_inset Quotes srd
4015 \begin_inset Quotes sld
4019 \begin_inset Quotes srd
4022 command is a script that analyzes your system and performs some configuration
4023 to ensure the source package compiles on your system.
4024 It will take a few minutes to run, and will compile a few tests to determine
4025 what compiler features are installed.
4029 \begin_inset Quotes sld
4033 \begin_inset Quotes srd
4039 This runs the GNU make tool, which automatically compiles all the source
4040 packages into the final installed binary executables.
4044 \begin_inset Quotes sld
4048 \begin_inset Quotes erd
4054 This will install the compiler, other executables libraries and include
4055 files into the appropriate directories.
4057 \begin_inset LatexCommand \ref{sub:Install-paths}
4063 \begin_inset LatexCommand \ref{sub:Search-Paths}
4068 about install and search paths.
4070 On most systems you will need super-user privileges to do this.
4076 SDCC is not just a compiler, but a collection of tools by various developers.
4077 These include linkers, assemblers, simulators and other components.
4078 Here is a summary of some of the components.
4079 Note that the included simulator and assembler have separate documentation
4080 which you can find in the source package in their respective directories.
4081 As SDCC grows to include support for other processors, other packages from
4082 various developers are included and may have their own sets of documentation.
4086 You might want to look at the files which are installed in <installdir>.
4087 At the time of this writing, we find the following programs for gcc-builds:
4091 In <installdir>/bin:
4094 sdcc - The compiler.
4097 sdcpp - The C preprocessor.
4100 asx8051 - The assembler for 8051 type processors.
4107 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4110 aslink -The linker for 8051 type processors.
4117 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4120 s51 - The ucSim 8051 simulator.
4123 sdcdb - The source debugger.
4126 packihx - A tool to pack (compress) Intel hex files.
4129 In <installdir>/share/sdcc/include
4135 In <installdir>/share/sdcc/lib
4138 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4142 In <installdir>/share/sdcc/doc
4148 As development for other processors proceeds, this list will expand to include
4149 executables to support processors like AVR, PIC, etc.
4155 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4156 the assembler and linkage editor.
4159 sdcpp - The C-Preprocessor
4163 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4167 is a modified version of the GNU preprocessor.
4168 The C preprocessor is used to pull in #include sources, process #ifdef
4169 statements, #defines and so on.
4180 - The Assemblers and Linkage Editors
4183 This is retargettable assembler & linkage editor, it was developed by Alan
4185 John Hartman created the version for 8051, and I (Sandeep) have made some
4186 enhancements and bug fixes for it to work properly with SDCC.
4193 \begin_inset LatexCommand \index{s51}
4197 is a freeware, opensource simulator developed by Daniel Drotos.
4198 The simulator is built as part of the build process.
4199 For more information visit Daniel's web site at:
4200 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4205 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4209 sdcdb - Source Level Debugger
4213 \begin_inset LatexCommand \index{sdcdb (debugger)}
4217 is the companion source level debugger.
4218 More about sdcdb in section
4219 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4224 The current version of the debugger uses Daniel's Simulator S51
4225 \begin_inset LatexCommand \index{s51}
4229 , but can be easily changed to use other simulators.
4239 Single Source File Projects
4242 For single source file 8051 projects the process is very simple.
4243 Compile your programs with the following command
4246 "sdcc sourcefile.c".
4250 This will compile, assemble and link your source file.
4251 Output files are as follows:
4255 \begin_inset LatexCommand \index{<file>.asm}
4260 \begin_inset LatexCommand \index{Assembler source}
4264 file created by the compiler
4268 \begin_inset LatexCommand \index{<file>.lst}
4273 \begin_inset LatexCommand \index{Assembler listing}
4277 file created by the Assembler
4281 \begin_inset LatexCommand \index{<file>.rst}
4286 \begin_inset LatexCommand \index{Assembler listing}
4290 file updated with linkedit information, created by linkage editor
4294 \begin_inset LatexCommand \index{<file>.sym}
4299 \begin_inset LatexCommand \index{Symbol listing}
4303 for the sourcefile, created by the assembler
4307 \begin_inset LatexCommand \index{<file>.rel}
4312 \begin_inset LatexCommand \index{<file>.o}
4317 \begin_inset LatexCommand \index{Object file}
4321 created by the assembler, input to Linkage editor
4325 \begin_inset LatexCommand \index{<file>.map}
4330 \begin_inset LatexCommand \index{Memory map}
4334 for the load module, created by the Linker
4338 \begin_inset LatexCommand \index{<file>.mem}
4342 - A file with a summary of the memory usage
4346 \begin_inset LatexCommand \index{<file>.ihx}
4350 - The load module in Intel hex format
4351 \begin_inset LatexCommand \index{Intel hex format}
4355 (you can select the Motorola S19 format
4356 \begin_inset LatexCommand \index{Motorola S19 format}
4371 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4376 If you need another format you might want to use
4383 \begin_inset LatexCommand \index{objdump (tool)}
4394 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4399 Both formats are documented in the documentation of srecord
4400 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4408 \begin_inset LatexCommand \index{<file>.adb}
4412 - An intermediate file containing debug information needed to create the
4424 \begin_inset LatexCommand \index{-\/-debug}
4432 \begin_inset LatexCommand \index{<file>.cdb}
4436 - An optional file (with -
4446 -debug) containing debug information.
4447 The format is documented in cdbfileformat.pdf
4452 \begin_inset LatexCommand \index{<file> (no extension)}
4456 An optional AOMF or AOMF51
4457 \begin_inset LatexCommand \index{AOMF, AOMF51}
4461 file containing debug information (generated with option -
4488 ormat is commonly used by third party tools (debuggers
4489 \begin_inset LatexCommand \index{Debugger}
4493 , simulators, emulators)
4497 \begin_inset LatexCommand \index{<file>.dump*}
4501 - Dump file to debug the compiler it self (generated with option -
4511 -dumpall) (see section
4512 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4518 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4524 \begin_inset Quotes sld
4527 Anatomy of the compiler
4528 \begin_inset Quotes srd
4534 Projects with Multiple Source Files
4537 SDCC can compile only ONE file at a time.
4538 Let us for example assume that you have a project containing the following
4543 foo1.c (contains some functions)
4545 foo2.c (contains some more functions)
4547 foomain.c (contains more functions and the function main)
4555 The first two files will need to be compiled separately with the commands:
4587 Then compile the source file containing the
4592 \begin_inset LatexCommand \index{Linker}
4596 the files together with the following command:
4604 foomain.c\SpecialChar ~
4605 foo1.rel\SpecialChar ~
4610 \begin_inset LatexCommand \index{<file>.rel}
4622 can be separately compiled as well:
4633 sdcc foomain.rel foo1.rel foo2.rel
4640 The file containing the
4655 file specified in the command line, since the linkage editor processes
4656 file in the order they are presented to it.
4657 The linker is invoked from SDCC using a script file with extension .lnk
4658 \begin_inset LatexCommand \index{<file>.lnk}
4663 You can view this file to troubleshoot linking problems such as those arising
4664 from missing libraries.
4667 Projects with Additional Libraries
4668 \begin_inset LatexCommand \index{Libraries}
4675 Some reusable routines may be compiled into a library, see the documentation
4676 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4680 \begin_inset LatexCommand \index{<file>.lib}
4687 Libraries created in this manner can be included in the command line.
4688 Make sure you include the -L <library-path> option to tell the linker where
4689 to look for these files if they are not in the current directory.
4690 Here is an example, assuming you have the source file
4702 (if that is not the same as your current project):
4709 sdcc foomain.c foolib.lib -L mylib
4720 must be an absolute path name.
4724 The most efficient way to use libraries is to keep separate modules in separate
4726 The lib file now should name all the modules.rel
4727 \begin_inset LatexCommand \index{<file>.rel}
4732 For an example see the standard library file
4736 in the directory <installdir>/share/lib/small.
4739 Using sdcclib to Create and Manage Libraries
4740 \begin_inset LatexCommand \index{sdcclib}
4747 Alternatively, instead of having a .rel file for each entry on the library
4748 file as described in the preceding section, sdcclib can be used to embed
4749 all the modules belonging to such library in the library file itself.
4750 This results in a larger library file, but it greatly reduces the number
4751 of disk files accessed by the linker.
4752 Additionally, the packed library file contains an index of all include
4753 modules and symbols that significantly speeds up the linking process.
4754 To display a list of options supported by sdcclib type:
4763 \begin_inset LatexCommand \index{sdcclib}
4774 To create a new library file, start by compiling all the required modules.
4812 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4814 The next step is to add the .rel files to the library file:
4822 sdcclib libint.lib _divsint.rel
4825 \begin_inset LatexCommand \index{sdcclib}
4835 sdcclib libint.lib _divuint.rel
4841 sdcclib libint.lib _modsint.rel
4847 sdcclib libint.lib _moduint.rel
4853 sdcclib libint.lib _mulint.rel
4860 If the file already exists in the library, it will be replaced.
4861 To see what modules and symbols are included in the library, options -s
4862 and -m are available.
4870 sdcclib -s libint.lib
4873 \begin_inset LatexCommand \index{sdcclib}
4983 If the source files are compiled using -
4994 \begin_inset LatexCommand \index{-\/-debug}
4998 , the corresponding debug information file .adb will be include in the library
5000 The library files created with sdcclib are plain text files, so they can
5001 be viewed with a text editor.
5002 It is not recomended to modify a library file created with sdcclib using
5003 a text editor, as there are file indexes numbers located accross the file
5004 used by the linker to quickly locate the required module to link.
5005 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5006 it can be safely deleted, since all the information required for linking
5007 is embedded in the library file itself.
5008 Library files created using sdcclib are used as described in the preceding
5012 Command Line Options
5013 \begin_inset LatexCommand \index{Command Line Options}
5020 Processor Selection Options
5021 \begin_inset LatexCommand \index{Options processor selection}
5026 \begin_inset LatexCommand \index{Processor selection options}
5032 \labelwidthstring 00.00.0000
5037 \begin_inset LatexCommand \index{-mmcs51}
5043 Generate code for the Intel MCS51
5044 \begin_inset LatexCommand \index{MCS51}
5048 family of processors.
5049 This is the default processor target.
5051 \labelwidthstring 00.00.0000
5056 \begin_inset LatexCommand \index{-mds390}
5062 Generate code for the Dallas DS80C390
5063 \begin_inset LatexCommand \index{DS80C390}
5069 \labelwidthstring 00.00.0000
5074 \begin_inset LatexCommand \index{-mds400}
5080 Generate code for the Dallas DS80C400
5081 \begin_inset LatexCommand \index{DS80C400}
5087 \labelwidthstring 00.00.0000
5092 \begin_inset LatexCommand \index{-mhc08}
5098 Generate code for the Freescale/Motorola HC08
5099 \begin_inset LatexCommand \index{HC08}
5103 family of processors.
5105 \labelwidthstring 00.00.0000
5110 \begin_inset LatexCommand \index{-mz80}
5116 Generate code for the Zilog Z80
5117 \begin_inset LatexCommand \index{Z80}
5121 family of processors.
5123 \labelwidthstring 00.00.0000
5128 \begin_inset LatexCommand \index{-mgbz80}
5134 Generate code for the GameBoy Z80
5135 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5139 processor (Not actively maintained).
5141 \labelwidthstring 00.00.0000
5146 \begin_inset LatexCommand \index{-mavr}
5152 Generate code for the Atmel AVR
5153 \begin_inset LatexCommand \index{AVR}
5157 processor (In development, not complete).
5158 AVR users should probably have a look at winavr
5159 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5164 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5171 I think it is fair to direct users there for now.
5172 Open source is also about avoiding unnecessary work .
5173 But I didn't find the 'official' link.
5175 \labelwidthstring 00.00.0000
5180 \begin_inset LatexCommand \index{-mpic14}
5186 Generate code for the Microchip PIC 14
5187 \begin_inset LatexCommand \index{PIC14}
5191 -bit processors (p16f84 and variants.
5192 In development, not complete).
5195 p16f627 p16f628 p16f84 p16f873 p16f877?
5197 \labelwidthstring 00.00.0000
5202 \begin_inset LatexCommand \index{-mpic16}
5208 Generate code for the Microchip PIC 16
5209 \begin_inset LatexCommand \index{PIC16}
5213 -bit processors (p18f452 and variants.
5214 In development, not complete).
5216 \labelwidthstring 00.00.0000
5222 Generate code for the Toshiba TLCS-900H
5223 \begin_inset LatexCommand \index{TLCS-900H}
5227 processor (Not maintained, not complete).
5229 \labelwidthstring 00.00.0000
5234 \begin_inset LatexCommand \index{-mxa51}
5240 Generate code for the Phillips XA51
5241 \begin_inset LatexCommand \index{XA51}
5245 processor (Not maintained, not complete).
5248 Preprocessor Options
5249 \begin_inset LatexCommand \index{Options preprocessor}
5254 \begin_inset LatexCommand \index{Preprocessor options}
5259 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5265 \labelwidthstring 00.00.0000
5270 \begin_inset LatexCommand \index{-I<path>}
5276 The additional location where the pre processor will look for <..h> or
5277 \begin_inset Quotes eld
5281 \begin_inset Quotes erd
5286 \labelwidthstring 00.00.0000
5291 \begin_inset LatexCommand \index{-D<macro[=value]>}
5297 Command line definition of macros.
5298 Passed to the preprocessor.
5300 \labelwidthstring 00.00.0000
5305 \begin_inset LatexCommand \index{-M}
5311 Tell the preprocessor to output a rule suitable for make describing the
5312 dependencies of each object file.
5313 For each source file, the preprocessor outputs one make-rule whose target
5314 is the object file name for that source file and whose dependencies are
5315 all the files `#include'd in it.
5316 This rule may be a single line or may be continued with `
5318 '-newline if it is long.
5319 The list of rules is printed on standard output instead of the preprocessed
5322 \begin_inset LatexCommand \index{-E}
5328 \labelwidthstring 00.00.0000
5333 \begin_inset LatexCommand \index{-C}
5339 Tell the preprocessor not to discard comments.
5340 Used with the `-E' option.
5342 \labelwidthstring 00.00.0000
5347 \begin_inset LatexCommand \index{-MM}
5358 Like `-M' but the output mentions only the user header files included with
5360 \begin_inset Quotes eld
5364 System header files included with `#include <file>' are omitted.
5366 \labelwidthstring 00.00.0000
5371 \begin_inset LatexCommand \index{-Aquestion(answer)}
5377 Assert the answer answer for question, in case it is tested with a preprocessor
5378 conditional such as `#if #question(answer)'.
5379 `-A-' disables the standard assertions that normally describe the target
5382 \labelwidthstring 00.00.0000
5387 \begin_inset LatexCommand \index{-Umacro}
5393 Undefine macro macro.
5394 `-U' options are evaluated after all `-D' options, but before any `-include'
5395 and `-imacros' options.
5397 \labelwidthstring 00.00.0000
5402 \begin_inset LatexCommand \index{-dM}
5408 Tell the preprocessor to output only a list of the macro definitions that
5409 are in effect at the end of preprocessing.
5410 Used with the `-E' option.
5412 \labelwidthstring 00.00.0000
5417 \begin_inset LatexCommand \index{-dD}
5423 Tell the preprocessor to pass all macro definitions into the output, in
5424 their proper sequence in the rest of the output.
5426 \labelwidthstring 00.00.0000
5431 \begin_inset LatexCommand \index{-dN}
5442 Like `-dD' except that the macro arguments and contents are omitted.
5443 Only `#define name' is included in the output.
5445 \labelwidthstring 00.00.0000
5450 preprocessorOption[,preprocessorOption]
5453 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5458 Pass the preprocessorOption to the preprocessor
5463 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5468 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5469 Collection (gcc), if you need more dedicated options please refer to the
5471 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5479 \begin_inset LatexCommand \index{Options linker}
5484 \begin_inset LatexCommand \index{Linker options}
5490 \labelwidthstring 00.00.0000
5510 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5515 \begin_inset LatexCommand \index{-L -\/-lib-path}
5522 <absolute path to additional libraries> This option is passed to the linkage
5523 editor's additional libraries
5524 \begin_inset LatexCommand \index{Libraries}
5529 The path name must be absolute.
5530 Additional library files may be specified in the command line.
5531 See section Compiling programs for more details.
5533 \labelwidthstring 00.00.0000
5550 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5555 <Value> The start location of the external ram
5556 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5560 , default value is 0.
5561 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5571 -xram-loc 0x8000 or -
5583 \labelwidthstring 00.00.0000
5600 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5605 <Value> The start location of the code
5606 \begin_inset LatexCommand \index{code}
5610 segment, default value 0.
5611 Note when this option is used the interrupt vector table is also relocated
5612 to the given address.
5613 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5623 -code-loc 0x8000 or -
5635 \labelwidthstring 00.00.0000
5652 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5657 <Value> By default the stack
5658 \begin_inset LatexCommand \index{stack}
5662 is placed after the data segment.
5663 Using this option the stack can be placed anywhere in the internal memory
5665 The value entered can be in Hexadecimal or Decimal format, e.g.
5676 -stack-loc 0x20 or -
5687 Since the sp register is incremented before a push or call, the initial
5688 sp will be set to one byte prior the provided value.
5689 The provided value should not overlap any other memory areas such as used
5690 register banks or the data segment and with enough space for the current
5708 \begin_inset LatexCommand \index{-\/-pack-iram}
5712 option (which is now a default setting) will override this setting, so
5713 you should also specify the
5729 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5733 option if you need to manually place the stack.
5735 \labelwidthstring 00.00.0000
5752 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5757 <Value> The start location of the internal ram data
5758 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5763 The value entered can be in Hexadecimal or Decimal format, eg.
5785 (By default, the start location of the internal ram data segment is set
5786 as low as possible in memory, taking into account the used register banks
5787 and the bit segment at address 0x20.
5788 For example if register banks 0 and 1 are used without bit variables, the
5789 data segment will be set, if -
5799 -data-loc is not used, to location 0x10.)
5801 \labelwidthstring 00.00.0000
5818 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5823 <Value> The start location of the indirectly addressable internal ram
5824 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5828 of the 8051, default value is 0x80.
5829 The value entered can be in Hexadecimal or Decimal format, eg.
5840 -idata-loc 0x88 or -
5852 \labelwidthstring 00.00.0000
5869 <Value> The start location of the bit
5870 \begin_inset LatexCommand \index{bit}
5874 addressable internal ram of the 8051.
5880 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5883 \labelwidthstring 00.00.0000
5898 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5907 The linker output (final object code) is in Intel Hex format.
5908 \begin_inset LatexCommand \index{Intel hex format}
5912 This is the default option.
5913 The format itself is documented in the documentation of srecord
5914 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
5920 \labelwidthstring 00.00.0000
5935 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5944 The linker output (final object code) is in Motorola S19 format
5945 \begin_inset LatexCommand \index{Motorola S19 format}
5950 The format itself is documented in the documentation of srecord.
5952 \labelwidthstring 00.00.0000
5967 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
5976 The linker output (final object code) is in ELF format
5977 \begin_inset LatexCommand \index{ELF format}
5982 (Currently only supported for the HC08 processors)
5984 \labelwidthstring 00.00.0000
5989 linkOption[,linkOption]
5992 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
5997 Pass the linkOption to the linker.
5998 See file sdcc/as/doc/asxhtm.html for more on linker options.
6002 \begin_inset LatexCommand \index{Options MCS51}
6007 \begin_inset LatexCommand \index{MCS51 options}
6013 \labelwidthstring 00.00.0000
6028 \begin_inset LatexCommand \index{-\/-model-small}
6039 Generate code for Small Model programs, see section Memory Models for more
6041 This is the default model.
6043 \labelwidthstring 00.00.0000
6058 \begin_inset LatexCommand \index{-\/-model-large}
6064 Generate code for Large model programs, see section Memory Models for more
6066 If this option is used all source files in the project have to be compiled
6069 \labelwidthstring 00.00.0000
6084 \begin_inset LatexCommand \index{-\/-xstack}
6090 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6091 variables and passing parameters.
6093 \begin_inset LatexCommand \ref{sub:External-Stack}
6098 External Stack for more details.
6100 \labelwidthstring 00.00.0000
6118 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6122 Causes the linker to check if the internal ram usage is within limits of
6125 \labelwidthstring 00.00.0000
6143 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6147 Causes the linker to check if the external ram usage is within limits of
6150 \labelwidthstring 00.00.0000
6168 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6172 Causes the linker to check if the code memory usage is within limits of
6175 \labelwidthstring 00.00.0000
6193 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6197 Causes the linker to check if there is at minimum <Value> bytes for stack.
6199 \labelwidthstring 00.00.0000
6217 \begin_inset LatexCommand \index{-\/-pack-iram}
6221 Causes the linker to use unused register banks for data variables and pack
6222 data, idata and stack together.
6223 This is the default now.
6225 \labelwidthstring 00.00.0000
6243 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6247 Causes the linker to use old style for allocating memory areas.
6250 DS390 / DS400 Options
6251 \begin_inset LatexCommand \index{Options DS390}
6256 \begin_inset LatexCommand \index{DS390 options}
6262 \labelwidthstring 00.00.0000
6279 \begin_inset LatexCommand \index{-\/-model-flat24}
6289 Generate 24-bit flat mode code.
6290 This is the one and only that the ds390 code generator supports right now
6291 and is default when using
6296 See section Memory Models for more details.
6298 \labelwidthstring 00.00.0000
6313 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6319 disable interrupts during ESP:SP updates.
6321 \labelwidthstring 00.00.0000
6338 \begin_inset LatexCommand \index{-\/-stack-10bit}
6342 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6343 This is the one and only that the ds390 code generator supports right now
6344 and is default when using
6349 In this mode, the stack is located in the lower 1K of the internal RAM,
6350 which is mapped to 0x400000.
6351 Note that the support is incomplete, since it still uses a single byte
6352 as the stack pointer.
6353 This means that only the lower 256 bytes of the potential 1K stack space
6354 will actually be used.
6355 However, this does allow you to reclaim the precious 256 bytes of low RAM
6356 for use for the DATA and IDATA segments.
6357 The compiler will not generate any code to put the processor into 10 bit
6359 It is important to ensure that the processor is in this mode before calling
6360 any re-entrant functions compiled with this option.
6361 In principle, this should work with the
6374 \begin_inset LatexCommand \index{-\/-stack-auto}
6380 option, but that has not been tested.
6381 It is incompatible with the
6394 \begin_inset LatexCommand \index{-\/-xstack}
6401 It also only makes sense if the processor is in 24 bit contiguous addressing
6414 -model-flat24 option
6418 \labelwidthstring 00.00.0000
6433 \begin_inset LatexCommand \index{-\/-stack-probe}
6439 insert call to function __stack_probe at each function prologue.
6441 \labelwidthstring 00.00.0000
6456 \begin_inset LatexCommand \index{-\/-tini-libid}
6462 <nnnn> LibraryID used in -mTININative.
6465 \labelwidthstring 00.00.0000
6480 \begin_inset LatexCommand \index{-\/-use-accelerator}
6486 generate code for DS390 Arithmetic Accelerator.
6491 \begin_inset LatexCommand \index{Options Z80}
6496 \begin_inset LatexCommand \index{Z80 options}
6502 \labelwidthstring 00.00.0000
6519 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6529 Force a called function to always save BC.
6531 \labelwidthstring 00.00.0000
6548 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6552 When linking, skip the standard crt0.o object file.
6553 You must provide your own crt0.o for your system when linking.
6557 Optimization Options
6558 \begin_inset LatexCommand \index{Options optimization}
6563 \begin_inset LatexCommand \index{Optimization options}
6569 \labelwidthstring 00.00.0000
6584 \begin_inset LatexCommand \index{-\/-nogcse}
6590 Will not do global subexpression elimination, this option may be used when
6591 the compiler creates undesirably large stack/data spaces to store compiler
6601 \begin_inset LatexCommand \index{sloc (spill location)}
6606 A warning message will be generated when this happens and the compiler
6607 will indicate the number of extra bytes it allocated.
6608 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6610 \begin_inset LatexCommand \index{\#pragma nogcse}
6614 can be used to turn off global subexpression elimination
6615 \begin_inset LatexCommand \index{Subexpression elimination}
6619 for a given function only.
6621 \labelwidthstring 00.00.0000
6636 \begin_inset LatexCommand \index{-\/-noinvariant}
6642 Will not do loop invariant optimizations, this may be turned off for reasons
6643 explained for the previous option.
6644 For more details of loop optimizations performed see Loop Invariants in
6646 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6651 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6653 \begin_inset LatexCommand \index{\#pragma noinvariant}
6657 can be used to turn off invariant optimizations for a given function only.
6659 \labelwidthstring 00.00.0000
6674 \begin_inset LatexCommand \index{-\/-noinduction}
6680 Will not do loop induction optimizations, see section strength reduction
6682 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6684 \begin_inset LatexCommand \index{\#pragma noinduction}
6688 can be used to turn off induction optimizations for a given function only.
6690 \labelwidthstring 00.00.0000
6705 \begin_inset LatexCommand \index{-\/-nojtbound}
6716 Will not generate boundary condition check when switch statements
6717 \begin_inset LatexCommand \index{switch statement}
6721 are implemented using jump-tables.
6723 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6728 Switch Statements for more details.
6729 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6731 \begin_inset LatexCommand \index{\#pragma nojtbound}
6735 can be used to turn off boundary checking for jump tables for a given function
6738 \labelwidthstring 00.00.0000
6753 \begin_inset LatexCommand \index{-\/-noloopreverse}
6762 Will not do loop reversal
6763 \begin_inset LatexCommand \index{Loop reversing}
6769 \labelwidthstring 00.00.0000
6786 \begin_inset LatexCommand \index{-\/-nolabelopt }
6790 Will not optimize labels (makes the dumpfiles more readable).
6792 \labelwidthstring 00.00.0000
6807 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6813 Will not memcpy initialized data from code space into xdata space.
6814 This saves a few bytes in code space if you don't have initialized data
6815 \begin_inset LatexCommand \index{Variable initialization}
6821 \labelwidthstring 00.00.0000
6836 \begin_inset LatexCommand \index{-\/-nooverlay}
6842 The compiler will not overlay parameters and local variables of any function,
6843 see section Parameters and local variables for more details.
6845 \labelwidthstring 00.00.0000
6860 \begin_inset LatexCommand \index{-\/-no-peep}
6866 Disable peep-hole optimization.
6868 \labelwidthstring 00.00.0000
6885 \begin_inset LatexCommand \index{-\/-peep-file}
6890 <filename> This option can be used to use additional rules to be used by
6891 the peep hole optimizer.
6893 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
6898 Peep Hole optimizations for details on how to write these rules.
6900 \labelwidthstring 00.00.0000
6915 \begin_inset LatexCommand \index{-\/-peep-asm}
6921 Pass the inline assembler code through the peep hole optimizer.
6922 This can cause unexpected changes to inline assembler code, please go through
6923 the peephole optimizer
6924 \begin_inset LatexCommand \index{Peephole optimizer}
6928 rules defined in the source file tree '<target>/peeph.def' before using
6931 \labelwidthstring 00.00.0000
6946 \begin_inset LatexCommand \index{-\/-opt-code-speed}
6952 The compiler will optimize code generation towards fast code, possibly
6953 at the expense of code size.
6955 \labelwidthstring 00.00.0000
6970 \begin_inset LatexCommand \index{-\/-opt-code-size}
6976 The compiler will optimize code generation towards compact code, possibly
6977 at the expense of code speed.
6981 \begin_inset LatexCommand \index{Options other}
6987 \labelwidthstring 00.00.0000
7003 \begin_inset LatexCommand \index{-\/-compile-only}
7008 \begin_inset LatexCommand \index{-c -\/-compile-only}
7014 will compile and assemble the source, but will not call the linkage editor.
7016 \labelwidthstring 00.00.0000
7035 \begin_inset LatexCommand \index{-\/-c1mode}
7041 reads the preprocessed source from standard input and compiles it.
7042 The file name for the assembler output must be specified using the -o option.
7044 \labelwidthstring 00.00.0000
7049 \begin_inset LatexCommand \index{-E}
7055 Run only the C preprocessor.
7056 Preprocess all the C source files specified and output the results to standard
7059 \labelwidthstring 00.00.0000
7065 \begin_inset LatexCommand \index{-o <path/file>}
7071 The output path resp.
7072 file where everything will be placed.
7073 If the parameter is a path, it must have a trailing slash (or backslash
7074 for the Windows binaries) to be recognized as a path.
7077 \labelwidthstring 00.00.0000
7092 \begin_inset LatexCommand \index{-\/-stack-auto}
7103 All functions in the source file will be compiled as
7108 \begin_inset LatexCommand \index{reentrant}
7113 the parameters and local variables will be allocated on the stack
7114 \begin_inset LatexCommand \index{stack}
7120 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7124 Parameters and Local Variables for more details.
7125 If this option is used all source files in the project should be compiled
7127 It automatically implies --int-long-reent and --float-reent.
7130 \labelwidthstring 00.00.0000
7145 \begin_inset LatexCommand \index{-\/-callee-saves}
7149 function1[,function2][,function3]....
7152 The compiler by default uses a caller saves convention for register saving
7153 across function calls, however this can cause unnecessary register pushing
7154 & popping when calling small functions from larger functions.
7155 This option can be used to switch the register saving convention for the
7156 function names specified.
7157 The compiler will not save registers when calling these functions, no extra
7158 code will be generated at the entry & exit (function prologue
7161 \begin_inset LatexCommand \index{function prologue}
7170 \begin_inset LatexCommand \index{function epilogue}
7176 ) for these functions to save & restore the registers used by these functions,
7177 this can SUBSTANTIALLY reduce code & improve run time performance of the
7179 In the future the compiler (with inter procedural analysis) will be able
7180 to determine the appropriate scheme to use for each function call.
7181 DO NOT use this option for built-in functions such as _mulint..., if this
7182 option is used for a library function the appropriate library function
7183 needs to be recompiled with the same option.
7184 If the project consists of multiple source files then all the source file
7185 should be compiled with the same -
7195 -callee-saves option string.
7196 Also see #pragma\SpecialChar ~
7198 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7204 \labelwidthstring 00.00.0000
7219 \begin_inset LatexCommand \index{-\/-debug}
7228 When this option is used the compiler will generate debug information.
7229 The debug information collected in a file with .cdb extension can be used
7231 For more information see documentation for SDCDB.
7232 Another file with no extension contains debug information in AOMF or AOMF51
7233 \begin_inset LatexCommand \index{AOMF, AOMF51}
7237 format which is commonly used by third party tools.
7239 \labelwidthstring 00.00.0000
7244 \begin_inset LatexCommand \index{-S}
7255 Stop after the stage of compilation proper; do not assemble.
7256 The output is an assembler code file for the input file specified.
7258 \labelwidthstring 00.00.0000
7273 \begin_inset LatexCommand \index{-\/-int-long-reent}
7279 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7280 Note by default these libraries are compiled as non-reentrant.
7281 See section Installation for more details.
7283 \labelwidthstring 00.00.0000
7298 \begin_inset LatexCommand \index{-\/-cyclomatic}
7307 This option will cause the compiler to generate an information message for
7308 each function in the source file.
7309 The message contains some
7313 information about the function.
7314 The number of edges and nodes the compiler detected in the control flow
7315 graph of the function, and most importantly the
7317 cyclomatic complexity
7318 \begin_inset LatexCommand \index{Cyclomatic complexity}
7324 see section on Cyclomatic Complexity for more details.
7326 \labelwidthstring 00.00.0000
7341 \begin_inset LatexCommand \index{-\/-float-reent}
7347 Floating point library is compiled as reentrant
7348 \begin_inset LatexCommand \index{reentrant}
7353 See section Installation for more details.
7355 \labelwidthstring 00.00.0000
7370 \begin_inset LatexCommand \index{-\/-main-return}
7376 This option can be used if the code generated is called by a monitor program
7377 or if the main routine includes an endless loop.
7378 This option might result in slightly smaller code and save two bytes of
7380 The return from the 'main'
7381 \begin_inset LatexCommand \index{main return}
7385 function will return to the function calling main.
7386 The default setting is to lock up i.e.
7393 \labelwidthstring 00.00.0000
7408 \begin_inset LatexCommand \index{-\/-nostdinc}
7414 This will prevent the compiler from passing on the default include path
7415 to the preprocessor.
7417 \labelwidthstring 00.00.0000
7432 \begin_inset LatexCommand \index{-\/-nostdlib}
7438 This will prevent the compiler from passing on the default library
7439 \begin_inset LatexCommand \index{Libraries}
7445 \labelwidthstring 00.00.0000
7460 \begin_inset LatexCommand \index{-\/-verbose}
7466 Shows the various actions the compiler is performing.
7468 \labelwidthstring 00.00.0000
7473 \begin_inset LatexCommand \index{-V}
7479 Shows the actual commands the compiler is executing.
7481 \labelwidthstring 00.00.0000
7496 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7502 Hides your ugly and inefficient c-code from the asm file, so you can always
7503 blame the compiler :)
7505 \labelwidthstring 00.00.0000
7520 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7526 Will not include peep-hole comments in the generated files.
7528 \labelwidthstring 00.00.0000
7543 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7549 Include i-codes in the asm file.
7550 Sounds like noise but is most helpful for debugging the compiler itself.
7552 \labelwidthstring 00.00.0000
7567 \begin_inset LatexCommand \index{-\/-less-pedantic}
7573 Disable some of the more pedantic warnings
7574 \begin_inset LatexCommand \index{Warnings}
7578 (jwk burps: please be more specific here, please!).
7579 If you want rather more than less warnings you should consider using a
7580 separate tool dedicated to syntax checking like splint
7581 \begin_inset LatexCommand \url{http://www.splint.org}
7587 \labelwidthstring 00.00.0000
7601 -disable-warning\SpecialChar ~
7603 \begin_inset LatexCommand \index{-\/-disable-warning}
7609 Disable specific warning with number <nnnn>.
7611 \labelwidthstring 00.00.0000
7626 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7632 Display the directories in the compiler's search path
7634 \labelwidthstring 00.00.0000
7649 \begin_inset LatexCommand \index{-\/-vc}
7655 Display errors and warnings using MSVC style, so you can use SDCC with
7658 \labelwidthstring 00.00.0000
7673 \begin_inset LatexCommand \index{-\/-use-stdout}
7679 Send errors and warnings to stdout instead of stderr.
7681 \labelwidthstring 00.00.0000
7686 asmOption[,asmOption]
7689 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7694 Pass the asmOption to the assembler
7695 \begin_inset LatexCommand \index{Options assembler}
7700 \begin_inset LatexCommand \index{Assembler options}
7705 See file sdcc/as/doc/asxhtm.html for assembler options.
7707 \labelwidthstring 00.00.0000
7722 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7728 Generally follow the C89 standard, but allow SDCC features that conflict
7729 with the standard (default).
7731 \labelwidthstring 00.00.0000
7746 \begin_inset LatexCommand \index{-\/-std-c89}
7752 Follow the C89 standard and disable SDCC features that conflict with the
7755 \labelwidthstring 00.00.0000
7770 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7776 Generally follow the C99 standard, but allow SDCC features that conflict
7777 with the standard (incomplete support).
7779 \labelwidthstring 00.00.0000
7794 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7800 Follow the C99 standard and disable SDCC features that conflict with the
7801 standard (incomplete support).
7804 Intermediate Dump Options
7805 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
7810 \begin_inset LatexCommand \index{Options intermediate dump}
7815 \begin_inset LatexCommand \index{Intermediate dump options}
7822 The following options are provided for the purpose of retargetting and debugging
7824 They provide a means to dump the intermediate code (iCode
7825 \begin_inset LatexCommand \index{iCode}
7829 ) generated by the compiler in human readable form at various stages of
7830 the compilation process.
7831 More on iCodes see chapter
7832 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
7837 \begin_inset Quotes srd
7840 The anatomy of the compiler
7841 \begin_inset Quotes srd
7846 \labelwidthstring 00.00.0000
7861 \begin_inset LatexCommand \index{-\/-dumpraw}
7867 This option will cause the compiler to dump the intermediate code into
7870 <source filename>.dumpraw
7872 just after the intermediate code has been generated for a function, i.e.
7873 before any optimizations are done.
7875 \begin_inset LatexCommand \index{Basic blocks}
7879 at this stage ordered in the depth first number, so they may not be in
7880 sequence of execution.
7882 \labelwidthstring 00.00.0000
7897 \begin_inset LatexCommand \index{-\/-dumpgcse}
7903 Will create a dump of iCode's, after global subexpression elimination
7904 \begin_inset LatexCommand \index{Global subexpression elimination}
7910 <source filename>.dumpgcse.
7912 \labelwidthstring 00.00.0000
7927 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
7933 Will create a dump of iCode's, after deadcode elimination
7934 \begin_inset LatexCommand \index{Dead-code elimination}
7940 <source filename>.dumpdeadcode.
7942 \labelwidthstring 00.00.0000
7957 \begin_inset LatexCommand \index{-\/-dumploop}
7966 Will create a dump of iCode's, after loop optimizations
7967 \begin_inset LatexCommand \index{Loop optimization}
7973 <source filename>.dumploop.
7975 \labelwidthstring 00.00.0000
7990 \begin_inset LatexCommand \index{-\/-dumprange}
7999 Will create a dump of iCode's, after live range analysis
8000 \begin_inset LatexCommand \index{Live range analysis}
8006 <source filename>.dumprange.
8008 \labelwidthstring 00.00.0000
8023 \begin_inset LatexCommand \index{-\/-dumlrange}
8029 Will dump the life ranges
8030 \begin_inset LatexCommand \index{Live range analysis}
8036 \labelwidthstring 00.00.0000
8051 \begin_inset LatexCommand \index{-\/-dumpregassign}
8060 Will create a dump of iCode's, after register assignment
8061 \begin_inset LatexCommand \index{Register assignment}
8067 <source filename>.dumprassgn.
8069 \labelwidthstring 00.00.0000
8084 \begin_inset LatexCommand \index{-\/-dumplrange}
8090 Will create a dump of the live ranges of iTemp's
8092 \labelwidthstring 00.00.0000
8107 \begin_inset LatexCommand \index{-\/-dumpall}
8118 Will cause all the above mentioned dumps to be created.
8121 Redirecting output on Windows Shells
8124 By default SDCC writes it's error messages to
8125 \begin_inset Quotes sld
8129 \begin_inset Quotes srd
8133 To force all messages to
8134 \begin_inset Quotes sld
8138 \begin_inset Quotes srd
8162 \begin_inset LatexCommand \index{-\/-use-stdout}
8167 Additionally, if you happen to have visual studio installed in your windows
8168 machine, you can use it to compile your sources using a custom build and
8184 \begin_inset LatexCommand \index{-\/-vc}
8189 Something like this should work:
8233 -model-large -c $(InputPath)
8236 Environment variables
8237 \begin_inset LatexCommand \index{Environment variables}
8244 SDCC recognizes the following environment variables:
8246 \labelwidthstring 00.00.0000
8251 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8257 SDCC installs a signal handler
8258 \begin_inset LatexCommand \index{signal handler}
8262 to be able to delete temporary files after an user break (^C) or an exception.
8263 If this environment variable is set, SDCC won't install the signal handler
8264 in order to be able to debug SDCC.
8266 \labelwidthstring 00.00.0000
8273 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8279 Path, where temporary files will be created.
8280 The order of the variables is the search order.
8281 In a standard *nix environment these variables are not set, and there's
8282 no need to set them.
8283 On Windows it's recommended to set one of them.
8285 \labelwidthstring 00.00.0000
8290 \begin_inset LatexCommand \index{SDCC\_HOME}
8297 \begin_inset LatexCommand \ref{sub:Install-paths}
8303 \begin_inset Quotes sld
8307 \begin_inset Quotes srd
8312 \labelwidthstring 00.00.0000
8317 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8324 \begin_inset LatexCommand \ref{sub:Search-Paths}
8330 \begin_inset Quotes sld
8334 \begin_inset Quotes srd
8339 \labelwidthstring 00.00.0000
8344 \begin_inset LatexCommand \index{SDCC\_LIB}
8351 \begin_inset LatexCommand \ref{sub:Search-Paths}
8357 \begin_inset Quotes sld
8361 \begin_inset Quotes srd
8367 There are some more environment variables recognized by SDCC, but these
8368 are solely used for debugging purposes.
8369 They can change or disappear very quickly, and will never be documented.
8372 Storage Class Language Extensions
8375 MCS51/DS390 Storage Class
8376 \begin_inset LatexCommand \index{Storage class}
8383 In addition to the ANSI storage classes SDCC allows the following MCS51
8384 specific storage classes:
8385 \layout Subsubsection
8388 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8393 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8398 \begin_inset LatexCommand \index{near (storage class)}
8403 \begin_inset LatexCommand \index{\_\_near (storage class)}
8414 storage class for the Small Memory model (
8422 can be used synonymously).
8423 Variables declared with this storage class will be allocated in the directly
8424 addressable portion of the internal RAM of a 8051, e.g.:
8429 data unsigned char test_data;
8432 Writing 0x01 to this variable generates the assembly code:
8437 75*00 01\SpecialChar ~
8443 \layout Subsubsection
8446 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8451 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8456 \begin_inset LatexCommand \index{far (storage class)}
8461 \begin_inset LatexCommand \index{\_\_far (storage class)}
8468 Variables declared with this storage class will be placed in the external
8474 storage class for the Large Memory model, e.g.:
8479 xdata unsigned char test_xdata;
8482 Writing 0x01 to this variable generates the assembly code:
8487 90s00r00\SpecialChar ~
8516 \layout Subsubsection
8519 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8524 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8531 Variables declared with this storage class will be allocated into the indirectly
8532 addressable portion of the internal ram of a 8051, e.g.:
8537 idata unsigned char test_idata;
8540 Writing 0x01 to this variable generates the assembly code:
8569 Please note, the first 128 byte of idata physically access the same RAM
8571 The original 8051 had 128 byte idata memory, nowadays most devices have
8572 256 byte idata memory.
8574 \begin_inset LatexCommand \index{stack}
8578 is located in idata memory.
8579 \layout Subsubsection
8582 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8587 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8594 Paged xdata access is just as straightforward as using the other addressing
8596 It is typically located at the start of xdata and has a maximum size of
8598 The following example writes 0x01 to the address pointed to.
8599 Please note, pdata access physically accesses xdata memory.
8600 The high byte of the address is determined by port P2
8601 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8605 (or in case of some 8051 variants by a separate Special Function Register,
8607 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8616 pdata unsigned char *test_pdata_ptr;
8628 test_pdata_ptr = (pdata *)0xfe;
8634 *test_pdata_ptr = 1;
8639 Generates the assembly code:
8644 75*01 FE\SpecialChar ~
8648 _test_pdata_ptr,#0xFE
8691 \begin_inset LatexCommand \index{-\/-xstack}
8695 option is used the pdata memory area is followed by the xstack memory area
8696 and the sum of their sizes is limited to 256 bytes.
8697 \layout Subsubsection
8700 \begin_inset LatexCommand \index{code}
8705 \begin_inset LatexCommand \index{\_\_code}
8712 'Variables' declared with this storage class will be placed in the code
8718 code unsigned char test_code;
8721 Read access to this variable generates the assembly code:
8726 90s00r6F\SpecialChar ~
8729 mov dptr,#_test_code
8758 indexed arrays of characters in code memory can be accessed efficiently:
8763 code char test_array[] = {'c','h','e','a','p'};
8766 Read access to this array using an 8-bit unsigned index generates the assembly
8783 90s00r41\SpecialChar ~
8786 mov dptr,#_test_array
8801 \layout Subsubsection
8804 \begin_inset LatexCommand \index{bit}
8809 \begin_inset LatexCommand \index{\_\_bit}
8816 This is a data-type and a storage class specifier.
8817 When a variable is declared as a bit, it is allocated into the bit addressable
8818 memory of 8051, e.g.:
8826 Writing 1 to this variable generates the assembly code:
8842 The bit addressable memory consists of 128 bits which are located from 0x20
8843 to 0x2f in data memory.
8846 Apart from this 8051 specific storage class most architectures support ANSI-C
8848 \begin_inset LatexCommand \index{bitfields}
8858 Not really meant as examples, but nevertheless showing what bitfields are
8859 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
8863 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
8864 signed modifier are implemented as unsigned.
8865 \layout Subsubsection
8868 \begin_inset LatexCommand \index{sfr}
8873 \begin_inset LatexCommand \index{\_\_sfr}
8878 \begin_inset LatexCommand \index{\_\_sbit}
8885 Like the bit keyword,
8889 signifies both a data-type and storage class, they are used to describe
8910 variables of a 8051, eg:
8916 \begin_inset LatexCommand \index{at}
8921 \begin_inset LatexCommand \index{\_\_at}
8925 0x80 P0;\SpecialChar ~
8926 /* special function register P0 at location 0x80 */
8928 sbit at 0xd7 CY; /* CY (Carry Flag
8929 \begin_inset LatexCommand \index{Flags}
8934 \begin_inset LatexCommand \index{Carry flag}
8941 Special function registers which are located on an address dividable by
8942 8 are bit-addressable, an
8946 addresses a specific bit within these sfr.
8947 \layout Subsubsection
8950 \begin_inset LatexCommand \index{Pointer}
8954 to MCS51/DS390 specific memory spaces
8957 SDCC allows (via language extensions) pointers to explicitly point to any
8958 of the memory spaces
8959 \begin_inset LatexCommand \index{Memory model}
8964 In addition to the explicit pointers, the compiler uses (by default) generic
8965 pointers which can be used to point to any of the memory spaces.
8969 Pointer declaration examples:
8974 /* pointer physically in internal ram pointing to object in external ram
8977 xdata unsigned char * data p;
8981 /* pointer physically in external ram pointing to object in internal ram
8984 data unsigned char * xdata p;
8988 /* pointer physically in code rom pointing to data in xdata space */
8990 xdata unsigned char * code p;
8994 /* pointer physically in code space pointing to data in code space */
8996 code unsigned char * code p;
9000 /* the following is a generic pointer physically located in xdata space
9006 Well you get the idea.
9011 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9024 The highest order byte of the
9028 pointers contains the data space information.
9029 Assembler support routines are called whenever data is stored or retrieved
9035 These are useful for developing reusable library
9036 \begin_inset LatexCommand \index{Libraries}
9041 Explicitly specifying the pointer type will generate the most efficient
9043 \layout Subsubsection
9045 Notes on MCS51 memory
9046 \begin_inset LatexCommand \index{MCS51 memory}
9053 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9054 RAM memory which is structured as follows:
9058 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9061 - Bytes 20-2F - 16 bytes to hold 128 bit
9062 \begin_inset LatexCommand \index{bit}
9068 - Bytes 30-7F - 80 bytes for general purpose use.
9073 Additionally some members of the MCS51 family may have up to 128 bytes of
9074 additional, indirectly addressable, internal RAM memory (
9079 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9084 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9089 Furthermore, some chips may have some built in external memory (
9094 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9099 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9103 ) which should not be confused with the internal, directly addressable RAM
9109 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9114 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9119 Sometimes this built in
9123 memory has to be activated before using it (you can probably find this
9124 information on the datasheet of the microcontroller your are using, see
9126 \begin_inset LatexCommand \ref{sub:Startup-Code}
9134 Normally SDCC will only use the first bank
9135 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9139 of registers (register bank 0), but it is possible to specify that other
9140 banks of registers (keyword
9147 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9152 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9158 ) should be used in interrupt
9159 \begin_inset LatexCommand \index{interrupt}
9164 \begin_inset LatexCommand \index{\_\_interrupt}
9169 By default, the compiler will place the stack after the last byte of allocated
9170 memory for variables.
9171 For example, if the first 2 banks of registers are used, and only four
9176 variables, it will position the base of the internal stack at address 20
9178 This implies that as the stack
9179 \begin_inset LatexCommand \index{stack}
9183 grows, it will use up the remaining register banks, and the 16 bytes used
9184 by the 128 bit variables, and 80 bytes for general purpose use.
9185 If any bit variables are used, the data variables will be placed in unused
9186 register banks and after the byte holding the last bit variable.
9187 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9192 variables will be placed starting from address 0x10 to 0x20 and continue
9205 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9209 to specify the start address of the
9224 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9228 to specify the size of the total internal RAM (
9240 By default the 8051 linker will place the stack after the last byte of (i)data
9253 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9257 allows you to specify the start of the stack, i.e.
9258 you could start it after any data in the general purpose area.
9259 If your microcontroller has additional indirectly addressable internal
9264 ) you can place the stack on it.
9265 You may also need to use -
9276 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9280 to set the start address of the external RAM (
9295 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9299 to specify its size.
9300 Same goes for the code memory, using -
9311 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9326 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9331 If in doubt, don't specify any options and see if the resulting memory
9332 layout is appropriate, then you can adjust it.
9335 The linker generates two files with memory allocation information.
9336 The first, with extension .map
9337 \begin_inset LatexCommand \index{<file>.map}
9341 shows all the variables and segments.
9342 The second with extension .mem
9343 \begin_inset LatexCommand \index{<file>.mem}
9347 shows the final memory layout.
9348 The linker will complain either if memory segments overlap, there is not
9349 enough memory, or there is not enough space for stack.
9350 If you get any linking warnings and/or errors related to stack or segments
9351 allocation, take a look at either the .map or .mem files to find out what
9353 The .mem file may even suggest a solution to the problem.
9356 Z80/Z180 Storage Class
9357 \begin_inset LatexCommand \index{Storage class}
9362 \layout Subsubsection
9365 \begin_inset LatexCommand \index{sfr}
9370 \begin_inset LatexCommand \index{\_\_sfr}
9374 (in/out to 8-bit addresses)
9378 \begin_inset LatexCommand \index{Z80}
9382 family has separate address spaces for memory and
9392 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9396 is accessed with special instructions, e.g.:
9401 sfr at 0x78 IoPort;\SpecialChar ~
9403 /* define a var in I/O space at 78h called IoPort */
9407 Writing 0x01 to this variable generates the assembly code:
9427 \layout Subsubsection
9430 \begin_inset LatexCommand \index{sfr}
9435 \begin_inset LatexCommand \index{\_\_sfr}
9439 (in/out to 16-bit addresses)
9446 is used to support 16 bit addresses in I/O memory e.g.:
9452 \begin_inset LatexCommand \index{at}
9457 \begin_inset LatexCommand \index{\_\_at}
9464 Writing 0x01 to this variable generates the assembly code:
9469 01 23 01\SpecialChar ~
9489 \layout Subsubsection
9492 \begin_inset LatexCommand \index{sfr}
9497 \begin_inset LatexCommand \index{\_\_sfr}
9501 (in0/out0 to 8 bit addresses on Z180
9502 \begin_inset LatexCommand \index{Z180}
9507 \begin_inset LatexCommand \index{HD64180}
9514 The compiler option -
9524 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9526 \begin_inset LatexCommand \index{\#pragma portmode}
9530 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9540 If you include the file z180.h this will be set automatically.
9544 \begin_inset LatexCommand \index{Storage class}
9549 \layout Subsubsection
9552 \begin_inset LatexCommand \index{data (hc08 storage class)}
9557 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9564 The data storage class declares a variable that resides in the first 256
9565 bytes of memory (the direct page).
9566 The HC08 is most efficient at accessing variables (especially pointers)
9568 \layout Subsubsection
9571 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9576 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9583 The xdata storage class declares a variable that can reside anywhere in
9585 This is the default if no storage class is specified.
9590 \begin_inset LatexCommand \index{Absolute addressing}
9597 Data items can be assigned an absolute address with the
9600 \begin_inset LatexCommand \index{at}
9605 \begin_inset LatexCommand \index{\_\_at}
9611 keyword, in addition to a storage class, e.g.:
9617 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9622 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9627 \begin_inset LatexCommand \index{at}
9632 \begin_inset LatexCommand \index{\_\_at}
9636 0x7ffe unsigned int chksum;
9639 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9640 of the external ram.
9645 reserve any space for variables declared in this way (they are implemented
9646 with an equate in the assembler).
9647 Thus it is left to the programmer to make sure there are no overlaps with
9648 other variables that are declared without the absolute address.
9649 The assembler listing file (.lst
9650 \begin_inset LatexCommand \index{<file>.lst}
9654 ) and the linker output files (.rst
9655 \begin_inset LatexCommand \index{<file>.rst}
9660 \begin_inset LatexCommand \index{<file>.map}
9664 ) are good places to look for such overlaps.
9665 Variables with an absolute address are
9670 \begin_inset LatexCommand \index{Variable initialization}
9677 In case of memory mapped I/O devices the keyword
9681 has to be used to tell the compiler that accesses might not be removed:
9687 \begin_inset LatexCommand \index{volatile}
9692 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9697 \begin_inset LatexCommand \index{at}
9701 0x8000 unsigned char PORTA_8255;
9704 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
9709 \begin_inset LatexCommand \index{Aligned array}
9716 starts at a block (256 byte) boundary
9717 \begin_inset LatexCommand \index{block boundary}
9722 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
9728 Absolute addresses can be specified for variables in all storage classes,
9735 \begin_inset LatexCommand \index{bit}
9740 \begin_inset LatexCommand \index{at}
9747 The above example will allocate the variable at offset 0x02 in the bit-addressab
9749 There is no real advantage to assigning absolute addresses to variables
9750 in this manner, unless you want strict control over all the variables allocated.
9751 One possible use would be to write hardware portable code.
9752 For example, if you have a routine that uses one or more of the microcontroller
9753 I/O pins, and such pins are different for two different hardwares, you
9754 can declare the I/O pins in your routine using:
9760 \begin_inset LatexCommand \index{volatile}
9764 bit MOSI;\SpecialChar ~
9768 /* master out, slave in */
9770 extern volatile bit MISO;\SpecialChar ~
9774 /* master in, slave out */
9776 extern volatile bit MCLK;\SpecialChar ~
9784 /* Input and Output of a byte on a 3-wire serial bus.
9789 If needed adapt polarity of clock, polarity of data and bit order
9794 unsigned char spi_io(unsigned char out_byte)
9818 MOSI = out_byte & 0x80;
9848 /* _asm nop _endasm; */\SpecialChar ~
9856 /* for slow peripherals */
9907 Then, someplace in the code for the first hardware you would use
9913 \begin_inset LatexCommand \index{at}
9918 \begin_inset LatexCommand \index{\_\_at}
9922 0x80 MOSI;\SpecialChar ~
9926 /* I/O port 0, bit 0 */
9928 bit at 0x81 MISO;\SpecialChar ~
9932 /* I/O port 0, bit 1 */
9934 bit at 0x82 MCLK;\SpecialChar ~
9938 /* I/O port 0, bit 2 */
9941 Similarly, for the second hardware you would use
9946 bit at 0x83 MOSI;\SpecialChar ~
9950 /* I/O port 0, bit 3 */
9952 bit at 0x91 MISO;\SpecialChar ~
9956 /* I/O port 1, bit 1 */
9959 \begin_inset LatexCommand \index{bit}
9963 at 0x92 MCLK;\SpecialChar ~
9967 /* I/O port 1, bit 2 */
9970 and you can use the same hardware dependent routine without changes, as
9971 for example in a library.
9972 This is somehow similar to sbit, but only one absolute address has to be
9973 specified in the whole project.
9977 \begin_inset LatexCommand \index{Parameters}
9982 \begin_inset LatexCommand \index{function parameter}
9987 \begin_inset LatexCommand \index{local variables}
9992 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
9999 Automatic (local) variables and parameters to functions can either be placed
10000 on the stack or in data-space.
10001 The default action of the compiler is to place these variables in the internal
10002 RAM (for small model) or external RAM (for large model).
10003 This in fact makes them similar to
10006 \begin_inset LatexCommand \index{static}
10012 so by default functions are non-reentrant
10013 \begin_inset LatexCommand \index{reentrant}
10022 They can be placed on the stack
10023 \begin_inset LatexCommand \index{stack}
10040 \begin_inset LatexCommand \index{-\/-stack-auto}
10048 #pragma\SpecialChar ~
10052 \begin_inset LatexCommand \index{\#pragma stackauto}
10059 \begin_inset LatexCommand \index{reentrant}
10065 keyword in the function declaration, e.g.:
10070 unsigned char foo(char i) reentrant
10084 Since stack space on 8051 is limited, the
10102 option should be used sparingly.
10103 Note that the reentrant keyword just means that the parameters & local
10104 variables will be allocated to the stack, it
10108 mean that the function is register bank
10109 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10118 \begin_inset LatexCommand \index{local variables}
10122 can be assigned storage classes and absolute
10123 \begin_inset LatexCommand \index{Absolute addressing}
10132 unsigned char foo()
10140 xdata unsigned char i;
10153 \begin_inset LatexCommand \index{at}
10157 0x31 unsigned char j;
10169 In the above example the variable
10173 will be allocated in the external ram,
10177 in bit addressable space and
10196 or when a function is declared as
10200 this should only be done for static variables.
10204 \begin_inset LatexCommand \index{function parameter}
10208 however are not allowed any storage class
10209 \begin_inset LatexCommand \index{Storage class}
10213 , (storage classes for parameters will be ignored), their allocation is
10214 governed by the memory model in use, and the reentrancy options.
10218 \begin_inset LatexCommand \label{sub:Overlaying}
10223 \begin_inset LatexCommand \index{Overlaying}
10231 \begin_inset LatexCommand \index{reentrant}
10235 functions SDCC will try to reduce internal ram space usage by overlaying
10236 parameters and local variables of a function (if possible).
10237 Parameters and local variables
10238 \begin_inset LatexCommand \index{local variables}
10242 of a function will be allocated to an overlayable segment if the function
10245 no other function calls and the function is non-reentrant and the memory
10247 \begin_inset LatexCommand \index{Memory model}
10254 If an explicit storage class
10255 \begin_inset LatexCommand \index{Storage class}
10259 is specified for a local variable, it will NOT be overlayed.
10262 Note that the compiler (not the linkage editor) makes the decision for overlayin
10264 Functions that are called from an interrupt service routine should be preceded
10265 by a #pragma\SpecialChar ~
10267 \begin_inset LatexCommand \index{\#pragma nooverlay}
10271 if they are not reentrant.
10274 Also note that the compiler does not do any processing of inline assembler
10275 code, so the compiler might incorrectly assign local variables and parameters
10276 of a function into the overlay segment if the inline assembler code calls
10277 other c-functions that might use the overlay.
10278 In that case the #pragma\SpecialChar ~
10279 nooverlay should be used.
10282 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10284 \begin_inset LatexCommand \index{Multiplication}
10289 \begin_inset LatexCommand \index{Division}
10293 will NOT be overlayed since these are implemented using external functions,
10302 \begin_inset LatexCommand \index{\#pragma nooverlay}
10308 void set_error(unsigned char errcd)
10324 void some_isr () interrupt
10325 \begin_inset LatexCommand \index{interrupt}
10355 In the above example the parameter
10363 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10365 not present, this could cause unpredictable runtime behavior when called
10366 from an interrupt service routine.
10367 The #pragma\SpecialChar ~
10368 nooverlay ensures that the parameters and local variables for
10369 the function are NOT overlayed.
10372 Interrupt Service Routines
10373 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10380 General Information
10395 outines to be coded in C, with some extended keywords.
10400 void timer_isr (void) interrupt 1 using 1
10414 The optional number following the
10417 \begin_inset LatexCommand \index{interrupt}
10422 \begin_inset LatexCommand \index{\_\_interrupt}
10428 keyword is the interrupt number this routine will service.
10429 When present, the compiler will insert a call to this routine in the interrupt
10430 vector table for the interrupt number specified.
10431 If you have multiple source files in your project, interrupt service routines
10432 can be present in any of them, but a prototype of the isr MUST be present
10433 or included in the file that contains the function
10441 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10446 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10452 keyword can be used to tell the compiler to use the specified register
10453 bank (8051 specific) when generating code for this function.
10459 Interrupt service routines open the door for some very interesting bugs:
10461 If an interrupt service routine changes variables which are accessed by
10462 other functions these variables have to be declared
10467 \begin_inset LatexCommand \index{volatile}
10475 If the access to these variables is not
10478 \begin_inset LatexCommand \index{atomic}
10485 the processor needs more than one instruction for the access and could
10486 be interrupted while accessing the variable) the interrupt must be disabled
10487 during the access to avoid inconsistent data.
10488 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10489 and should be protected by disabling interrupts.
10490 You're not automatically on the safe side if you use 8 bit variables though.
10491 We need an example here: f.e.
10492 on the 8051 the harmless looking
10493 \begin_inset Quotes srd
10498 flags\SpecialChar ~
10503 \begin_inset Quotes sld
10512 \begin_inset Quotes srd
10517 flags\SpecialChar ~
10522 \begin_inset Quotes sld
10525 from within an interrupt routine might get lost if the interrupt occurs
10528 \begin_inset Quotes sld
10533 counter\SpecialChar ~
10538 \begin_inset Quotes srd
10541 is not atomic on the 8051 even if
10545 is located in data memory.
10546 Bugs like these are hard to reproduce and can cause a lot of trouble.
10550 The return address and the registers used in the interrupt service routine
10551 are saved on the stack
10552 \begin_inset LatexCommand \index{stack}
10556 so there must be sufficient stack space.
10557 If there isn't variables or registers (or even the return address itself)
10564 \begin_inset LatexCommand \index{stack overflow}
10568 is most likely to happen if the interrupt occurs during the
10569 \begin_inset Quotes sld
10573 \begin_inset Quotes srd
10576 subroutine when the stack is already in use for f.e.
10577 many return addresses.
10580 A special note here, int (16 bit) and long (32 bit) integer division
10581 \begin_inset LatexCommand \index{Division}
10586 \begin_inset LatexCommand \index{Multiplication}
10591 \begin_inset LatexCommand \index{Modulus}
10596 \begin_inset LatexCommand \index{Floating point support}
10600 operations are implemented using external support routines developed in
10602 If an interrupt service routine needs to do any of these operations then
10603 the support routines (as mentioned in a following section) will have to
10604 be recompiled using the
10617 \begin_inset LatexCommand \index{-\/-stack-auto}
10623 option and the source file will need to be compiled using the
10638 \begin_inset LatexCommand \index{-\/-int-long-reent}
10645 Calling other functions from an interrupt service routine is not recommended,
10646 avoid it if possible.
10647 Note that when some function is called from an interrupt service routine
10648 it should be preceded by a #pragma\SpecialChar ~
10650 \begin_inset LatexCommand \index{\#pragma nooverlay}
10654 if it is not reentrant.
10655 Furthermore nonreentrant functions should not be called from the main program
10656 while the interrupt service routine might be active.
10662 \begin_inset LatexCommand \ref{sub:Overlaying}
10667 about Overlaying and section
10668 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10673 about Functions using private register banks.
10676 MCS51/DS390 Interrupt Service Routines
10679 Interrupt numbers and the corresponding address & descriptions for the Standard
10680 8051/8052 are listed below.
10681 SDCC will automatically adjust the interrupt vector table to the maximum
10682 interrupt number specified.
10688 \begin_inset Tabular
10689 <lyxtabular version="3" rows="7" columns="3">
10691 <column alignment="center" valignment="top" leftline="true" width="0in">
10692 <column alignment="center" valignment="top" leftline="true" width="0in">
10693 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
10694 <row topline="true" bottomline="true">
10695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10720 <row topline="true">
10721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10746 <row topline="true">
10747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10763 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10772 <row topline="true">
10773 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10781 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10798 <row topline="true">
10799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10824 <row topline="true">
10825 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10850 <row topline="true" bottomline="true">
10851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
10867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
10885 If the interrupt service routine is defined without
10888 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10893 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10899 a register bank or with register bank 0 (
10903 0), the compiler will save the registers used by itself on the stack upon
10904 entry and restore them at exit, however if such an interrupt service routine
10905 calls another function then the entire register bank will be saved on the
10907 This scheme may be advantageous for small interrupt service routines which
10908 have low register usage.
10911 If the interrupt service routine is defined to be using a specific register
10916 & psw are saved and restored, if such an interrupt service routine calls
10917 another function (using another register bank) then the entire register
10918 bank of the called function will be saved on the stack.
10919 This scheme is recommended for larger interrupt service routines.
10922 HC08 Interrupt Service Routines
10925 Since the number of interrupts available is chip specific and the interrupt
10926 vector table always ends at the last byte of memory, the interrupt numbers
10927 corresponds to the interrupt vectors in reverse order of address.
10928 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
10929 2 will use the interrupt vector at 0xfffa, and so on.
10930 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
10931 this way; instead see section
10932 \begin_inset LatexCommand \ref{sub:Startup-Code}
10936 for details on customizing startup.
10939 Z80 Interrupt Service Routines
10942 The Z80 uses several different methods for determining the correct interrupt
10943 vector depending on the hardware implementation.
10944 Therefore, SDCC ignores the optional interrupt number and does not attempt
10945 to generate an interrupt vector table.
10948 By default, SDCC generates code for a maskable interrupt, which uses an
10949 RETI instruction to return from the interrupt.
10950 To write an interrupt handler for the non-maskable interrupt, which needs
10951 an RETN instruction instead, add the
10960 void nmi_isr (void) critical interrupt
10974 Enabling and Disabling Interrupts
10977 Critical Functions and Critical Statements
10980 A special keyword may be associated with a block or a function declaring
10986 SDCC will generate code to disable all interrupts
10987 \begin_inset LatexCommand \index{interrupt}
10991 upon entry to a critical function and restore the interrupt enable to the
10992 previous state before returning.
10993 Nesting critical functions will need one additional byte on the stack
10994 \begin_inset LatexCommand \index{stack}
11003 int foo () critical
11004 \begin_inset LatexCommand \index{critical}
11009 \begin_inset LatexCommand \index{\_\_critical}
11034 The critical attribute maybe used with other attributes like
11044 may also be used to disable interrupts more locally:
11052 More than one statement could have been included in the block.
11055 Enabling and Disabling Interrupts directly
11059 \begin_inset LatexCommand \index{interrupt}
11063 can also be disabled and enabled directly (8051):
11068 EA = 0;\SpecialChar ~
11131 EA = 1;\SpecialChar ~
11198 On other architectures which have seperate opcodes for enabling and disabling
11199 interrupts you might want to make use of defines with inline assembly
11200 \begin_inset LatexCommand \index{Assembler routines}
11210 \begin_inset LatexCommand \index{\_asm}
11219 \begin_inset LatexCommand \index{\_endasm}
11228 #define SEI _asm\SpecialChar ~
11240 Note: it is sometimes sufficient to disable only a specific interrupt source
11242 a timer or serial interrupt by manipulating an
11245 \begin_inset LatexCommand \index{interrupt mask}
11255 Usually the time during which interrupts are disabled should be kept as
11257 This minimizes both
11262 \begin_inset LatexCommand \index{interrupt latency}
11266 (the time between the occurrence of the interrupt and the execution of
11267 the first code in the interrupt routine) and
11272 \begin_inset LatexCommand \index{interrupt jitter}
11276 (the difference between the shortest and the longest interrupt latency).
11277 These really are something different, f.e.
11278 a serial interrupt has to be served before its buffer overruns so it cares
11279 for the maximum interrupt latency, whereas it does not care about jitter.
11280 On a loudspeaker driven via a digital to analog converter which is fed
11281 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11282 a much smaller jitter will be very audible.
11285 You can reenable interrupts within an interrupt routine and on some architecture
11286 s you can make use of two (or more) levels of
11288 interrupt priorities
11291 \begin_inset LatexCommand \index{interrupt priority}
11296 On some architectures which don't support interrupt priorities these can
11297 be implemented by manipulating the interrupt mask and reenabling interrupts
11298 within the interrupt routine.
11299 Check there is sufficient space on the stack
11300 \begin_inset LatexCommand \index{stack}
11304 and don't add complexity unless you have to.
11309 \begin_inset LatexCommand \index{semaphore}
11313 locking (mcs51/ds390)
11316 Some architectures (mcs51/ds390) have an atomic
11317 \begin_inset LatexCommand \index{atomic}
11330 These type of instructions are typically used in preemptive multitasking
11331 systems, where a routine f.e.
11332 claims the use of a data structure ('acquires a lock
11333 \begin_inset LatexCommand \index{lock}
11337 on it'), makes some modifications and then releases the lock when the data
11338 structure is consistent again.
11339 The instruction may also be used if interrupt and non-interrupt code have
11340 to compete for a resource.
11341 With the atomic bit test and clear instruction interrupts
11342 \begin_inset LatexCommand \index{interrupt}
11346 don't have to be disabled for the locking operation.
11350 SDCC generates this instruction if the source follows this pattern:
11356 \begin_inset LatexCommand \index{volatile}
11360 bit resource_is_free;
11364 if (resource_is_free)
11374 resource_is_free=0;
11387 resource_is_free=1;
11394 Note, mcs51 and ds390 support only an atomic
11395 \begin_inset LatexCommand \index{atomic}
11403 instruction (as opposed to atomic bit test and
11408 Functions using private register banks
11409 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11416 Some architectures have support for quickly changing register sets.
11417 SDCC supports this feature with the
11420 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11425 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11431 attribute (which tells the compiler to use a register bank
11432 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11436 other than the default bank zero).
11437 It should only be applied to
11440 \begin_inset LatexCommand \index{interrupt}
11446 functions (see footnote below).
11447 This will in most circumstances make the generated ISR code more efficient
11448 since it will not have to save registers on the stack.
11455 attribute will have no effect on the generated code for a
11459 function (but may occasionally be useful anyway
11465 possible exception: if a function is called ONLY from 'interrupt' functions
11466 using a particular bank, it can be declared with the same 'using' attribute
11467 as the calling 'interrupt' functions.
11468 For instance, if you have several ISRs using bank one, and all of them
11469 call memcpy(), it might make sense to create a specialized version of memcpy()
11470 'using 1', since this would prevent the ISR from having to save bank zero
11471 to the stack on entry and switch to bank zero before calling the function
11478 (pending: I don't think this has been done yet)
11485 function using a non-zero bank will assume that it can trash that register
11486 bank, and will not save it.
11487 Since high-priority interrupts
11488 \begin_inset LatexCommand \index{interrupts}
11493 \begin_inset LatexCommand \index{interrupt priority}
11497 can interrupt low-priority ones on the 8051 and friends, this means that
11498 if a high-priority ISR
11502 a particular bank occurs while processing a low-priority ISR
11506 the same bank, terrible and bad things can happen.
11507 To prevent this, no single register bank should be
11511 by both a high priority and a low priority ISR.
11512 This is probably most easily done by having all high priority ISRs use
11513 one bank and all low priority ISRs use another.
11514 If you have an ISR which can change priority at runtime, you're on your
11515 own: I suggest using the default bank zero and taking the small performance
11519 It is most efficient if your ISR calls no other functions.
11520 If your ISR must call other functions, it is most efficient if those functions
11521 use the same bank as the ISR (see note 1 below); the next best is if the
11522 called functions use bank zero.
11523 It is very inefficient to call a function using a different, non-zero bank
11529 \begin_inset LatexCommand \label{sub:Startup-Code}
11534 \begin_inset LatexCommand \index{Startup code}
11541 MCS51/DS390 Startup Code
11544 The compiler inserts a call to the C routine
11546 _sdcc_external_startup()
11547 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11556 at the start of the CODE area.
11557 This routine is in the runtime library
11558 \begin_inset LatexCommand \index{Runtime library}
11563 By default this routine returns 0, if this routine returns a non-zero value,
11564 the static & global variable initialization will be skipped and the function
11565 main will be invoked.
11566 Otherwise static & global variables will be initialized before the function
11570 _sdcc_external_startup()
11572 routine to your program to override the default if you need to setup hardware
11573 or perform some other critical operation prior to static & global variable
11575 \begin_inset LatexCommand \index{Variable initialization}
11580 On some mcs51 variants xdata
11581 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11585 memory has to be explicitly enabled before it can be accessed or if the
11586 watchdog needs to be disabled, this is the place to do it.
11587 The startup code clears all internal data memory, 256 bytes by default,
11588 but from 0 to n-1 if
11601 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11608 (recommended for Chipcon CC1010).
11611 See also the compiler option
11630 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11635 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11640 about MCS51-variants.
11646 The HC08 startup code follows the same scheme as the MCS51 startup code.
11652 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11653 from sdcc/device/lib/z80/crt0.s.
11654 If you need a different startup code you can use the compiler option
11675 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11679 and provide your own crt0.o.
11683 Inline Assembler Code
11684 \begin_inset LatexCommand \index{Assembler routines}
11691 A Step by Step Introduction
11692 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
11699 Starting from a small snippet of c-code this example shows for the MCS51
11700 how to use inline assembly, access variables, a function parameter and
11701 an array in xdata memory.
11702 The example uses an MCS51 here but is easily adapted for other architectures.
11703 This is a buffer routine which should be optimized:
11710 \begin_inset LatexCommand \index{far (storage class)}
11715 \begin_inset LatexCommand \index{\_\_far (storage class)}
11720 \begin_inset LatexCommand \index{at}
11725 \begin_inset LatexCommand \index{\_\_at}
11730 \begin_inset LatexCommand \index{Aligned array}
11736 unsigned char head,tail;
11740 void to_buffer( unsigned char c )
11748 if( head != tail-1 )
11758 buf[ head++ ] = c;\SpecialChar ~
11762 /* access to a 256 byte aligned array */
11767 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
11768 then a corresponding buffer.asm file is generated.
11769 We define a new function
11773 in file buffer.c in which we cut and paste the generated code, removing
11774 unwanted comments and some ':'.
11776 \begin_inset Quotes sld
11780 \begin_inset Quotes srd
11784 \begin_inset Quotes sld
11788 \begin_inset Quotes srd
11791 to the beginning and the end of the function body:
11797 /* With a cut and paste from the .asm file, we have something to start with.
11802 The function is not yet OK! (registers aren't saved) */
11804 void to_buffer_asm( unsigned char c )
11813 \begin_inset LatexCommand \index{\_asm}
11818 \begin_inset LatexCommand \index{\_\_asm}
11832 ;buffer.c if( head != tail-1 )
11880 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
11881 \begin_inset LatexCommand \index{Aligned array}
11946 \begin_inset LatexCommand \index{\_endasm}
11951 \begin_inset LatexCommand \index{\_\_endasm}
11960 The new file buffer.c should compile with only one warning about the unreferenced
11961 function argument 'c'.
11962 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
11963 (1) and finally have:
11969 unsigned char far at 0x7f00 buf[0x100];
11971 unsigned char head,tail;
11973 #define USE_ASSEMBLY (1)
11981 void to_buffer( unsigned char c )
11989 if( head != tail-1 )
12009 void to_buffer( unsigned char c )
12017 c; // to avoid warning: unreferenced function argument
12024 \begin_inset LatexCommand \index{\_asm}
12029 \begin_inset LatexCommand \index{\_\_asm}
12043 ; save used registers here.
12054 ; If we were still using r2,r3 we would have to push them here.
12057 ; if( head != tail-1 )
12100 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12124 ; buf[ head++ ] = c;
12135 a,dpl \SpecialChar ~
12142 ; dpl holds lower byte of function argument
12153 dpl,_head \SpecialChar ~
12156 ; buf is 0x100 byte aligned so head can be used directly
12198 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12210 ; restore used registers here
12217 \begin_inset LatexCommand \index{\_endasm}
12222 \begin_inset LatexCommand \index{\_\_endasm}
12233 The inline assembler code can contain any valid code understood by the assembler
12234 , this includes any assembler directives and comment lines
12240 The assembler does not like some characters like ':' or ''' in comments.
12241 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12242 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12247 \begin_inset LatexCommand \index{Assembler documentation}
12255 The compiler does not do any validation of the code within the
12258 \begin_inset LatexCommand \index{\_asm}
12263 \begin_inset LatexCommand \index{\_\_asm}
12271 \begin_inset LatexCommand \index{\_endasm}
12276 \begin_inset LatexCommand \index{\_\_endasm}
12285 Specifically it will not know which registers are used and thus register
12287 \begin_inset LatexCommand \index{push/pop}
12291 has to be done manually.
12295 It is recommended that each assembly instruction (including labels) be placed
12296 in a separate line (as the example shows).
12310 \begin_inset LatexCommand \index{-\/-peep-asm}
12316 command line option is used, the inline assembler code will be passed through
12317 the peephole optimizer
12318 \begin_inset LatexCommand \index{Peephole optimizer}
12323 There are only a few (if any) cases where this option makes sense, it might
12324 cause some unexpected changes in the inline assembler code.
12325 Please go through the peephole optimizer rules defined in file
12329 before using this option.
12333 \begin_inset LatexCommand \label{sub:Naked-Functions}
12338 \begin_inset LatexCommand \index{Naked functions}
12345 A special keyword may be associated with a function declaring it as
12348 \begin_inset LatexCommand \index{\_naked}
12353 \begin_inset LatexCommand \index{\_\_naked}
12364 function modifier attribute prevents the compiler from generating prologue
12365 \begin_inset LatexCommand \index{function prologue}
12370 \begin_inset LatexCommand \index{function epilogue}
12374 code for that function.
12375 This means that the user is entirely responsible for such things as saving
12376 any registers that may need to be preserved, selecting the proper register
12377 bank, generating the
12381 instruction at the end, etc.
12382 Practically, this means that the contents of the function must be written
12383 in inline assembler.
12384 This is particularly useful for interrupt functions, which can have a large
12385 (and often unnecessary) prologue/epilogue.
12386 For example, compare the code generated by these two functions:
12392 \begin_inset LatexCommand \index{volatile}
12396 data unsigned char counter;
12400 void simpleInterrupt(void) interrupt
12401 \begin_inset LatexCommand \index{interrupt}
12406 \begin_inset LatexCommand \index{\_\_interrupt}
12424 void nakedInterrupt(void) interrupt 2 _naked
12433 \begin_inset LatexCommand \index{\_asm}
12438 \begin_inset LatexCommand \index{\_\_asm}
12455 _counter ; does not change flags, no need to save psw
12467 ; MUST explicitly include ret or reti in _naked function.
12474 \begin_inset LatexCommand \index{\_endasm}
12479 \begin_inset LatexCommand \index{\_\_endasm}
12488 For an 8051 target, the generated simpleInterrupt looks like:
12629 whereas nakedInterrupt looks like:
12644 _counter ; does not change flags, no need to save psw
12662 ; MUST explicitly include ret or reti in _naked function
12665 The related directive #pragma exclude
12666 \begin_inset LatexCommand \index{\#pragma exclude}
12670 allows a more fine grained control over pushing & popping
12671 \begin_inset LatexCommand \index{push/pop}
12678 While there is nothing preventing you from writing C code inside a
12682 function, there are many ways to shoot yourself in the foot doing this,
12683 and it is recommended that you stick to inline assembler.
12686 Use of Labels within Inline Assembler
12689 SDCC allows the use of in-line assembler with a few restrictions regarding
12691 In older versions of the compiler all labels defined within inline assembler
12700 where nnnn is a number less than 100 (which implies a limit of utmost 100
12701 inline assembler labels
12715 \begin_inset LatexCommand \index{\_asm}
12720 \begin_inset LatexCommand \index{\_\_asm}
12750 \begin_inset LatexCommand \index{\_endasm}
12755 \begin_inset LatexCommand \index{\_\_endasm}
12762 Inline assembler code cannot reference any C-Labels, however it can reference
12764 \begin_inset LatexCommand \index{Labels}
12768 defined by the inline assembler, e.g.:
12793 ; some assembler code
12813 /* some more c code */
12815 clabel:\SpecialChar ~
12817 /* inline assembler cannot reference this label */
12829 $0003: ;label (can be referenced by inline assembler only)
12836 \begin_inset LatexCommand \index{\_endasm}
12841 \begin_inset LatexCommand \index{\_\_endasm}
12851 /* some more c code */
12856 In other words inline assembly code can access labels defined in inline
12857 assembly within the scope of the function.
12858 The same goes the other way, i.e.
12859 labels defines in inline assembly can not be accessed by C statements.
12862 Interfacing with Assembler Code
12863 \begin_inset LatexCommand \index{Assembler routines}
12870 Global Registers used for Parameter Passing
12871 \begin_inset LatexCommand \index{Parameter passing}
12878 The compiler always uses the global registers
12881 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
12886 \begin_inset LatexCommand \index{DPTR}
12891 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
12900 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
12906 to pass the first parameter to a routine.
12907 The second parameter onwards is either allocated on the stack (for reentrant
12918 -stack-auto is used) or in data / xdata memory (depending on the memory
12923 Assembler Routine (non-reentrant)
12926 In the following example
12927 \begin_inset LatexCommand \index{reentrant}
12932 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
12936 the function c_func calls an assembler routine asm_func, which takes two
12938 \begin_inset LatexCommand \index{function parameter}
12947 extern int asm_func(unsigned char, unsigned char);
12951 int c_func (unsigned char i, unsigned char j)
12959 return asm_func(i,j);
12973 return c_func(10,9);
12978 The corresponding assembler function is:
12983 .globl _asm_func_PARM_2
13084 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13101 Note here that the return values
13102 \begin_inset LatexCommand \index{return value}
13106 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13108 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13109 b' & 'acc' for four byte values.
13112 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13113 the parameter number starting from 1, and counting from the left.
13114 The first parameter is passed in
13115 \begin_inset Quotes eld
13119 \begin_inset Quotes erd
13122 for a one byte parameter,
13123 \begin_inset Quotes eld
13127 \begin_inset Quotes erd
13131 \begin_inset Quotes eld
13135 \begin_inset Quotes erd
13138 for three bytes and
13139 \begin_inset Quotes eld
13143 \begin_inset Quotes erd
13146 for a four bytes parameter.
13147 The variable name for the second parameter will be _<function_name>_PARM_2.
13151 Assemble the assembler routine with the following command:
13158 asx8051 -losg asmfunc.asm
13165 Then compile and link the assembler routine to the C source file with the
13173 sdcc cfunc.c asmfunc.rel
13176 Assembler Routine (reentrant)
13180 \begin_inset LatexCommand \index{reentrant}
13185 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13189 the second parameter
13190 \begin_inset LatexCommand \index{function parameter}
13194 onwards will be passed on the stack, the parameters are pushed from right
13196 after the call the leftmost parameter will be on the top of the stack.
13197 Here is an example:
13202 extern int asm_func(unsigned char, unsigned char);
13206 int c_func (unsigned char i, unsigned char j) reentrant
13214 return asm_func(i,j);
13228 return c_func(10,9);
13233 The corresponding assembler routine is:
13333 The compiling and linking procedure remains the same, however note the extra
13334 entry & exit linkage required for the assembler code, _bp is the stack
13335 frame pointer and is used to compute the offset into the stack for parameters
13336 and local variables.
13340 \begin_inset LatexCommand \index{int (16 bit)}
13345 \begin_inset LatexCommand \index{long (32 bit)}
13352 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13353 multiplication and modulus operations are implemented by support routines.
13354 These support routines are all developed in ANSI-C to facilitate porting
13355 to other MCUs, although some model specific assembler optimizations are
13357 The following files contain the described routines, all of them can be
13358 found in <installdir>/share/sdcc/lib.
13364 \begin_inset Tabular
13365 <lyxtabular version="3" rows="11" columns="2">
13367 <column alignment="center" valignment="top" leftline="true" width="0">
13368 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13369 <row topline="true" bottomline="true">
13370 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13391 <row topline="true">
13392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13405 16 bit multiplication
13409 <row topline="true">
13410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13423 signed 16 bit division (calls _divuint)
13427 <row topline="true">
13428 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13436 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13441 unsigned 16 bit division
13445 <row topline="true">
13446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13454 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13459 signed 16 bit modulus (calls _moduint)
13463 <row topline="true">
13464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13477 unsigned 16 bit modulus
13481 <row topline="true">
13482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13490 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13495 32 bit multiplication
13499 <row topline="true">
13500 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13508 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13513 signed 32 division (calls _divulong)
13517 <row topline="true">
13518 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13526 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13531 unsigned 32 division
13535 <row topline="true">
13536 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13544 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13549 signed 32 bit modulus (calls _modulong)
13553 <row topline="true" bottomline="true">
13554 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13562 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13567 unsigned 32 bit modulus
13580 Since they are compiled as
13585 \begin_inset LatexCommand \index{reentrant}
13590 \begin_inset LatexCommand \index{interrupt}
13594 service routines should not do any of the above operations.
13595 If this is unavoidable then the above routines will need to be compiled
13609 \begin_inset LatexCommand \index{-\/-stack-auto}
13615 option, after which the source program will have to be compiled with
13628 \begin_inset LatexCommand \index{-\/-int-long-reent}
13635 Notice that you don't have to call these routines directly.
13636 The compiler will use them automatically every time an integer operation
13640 Floating Point Support
13641 \begin_inset LatexCommand \index{Floating point support}
13648 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13649 floating point support routines are derived from gcc's floatlib.c and consist
13650 of the following routines:
13658 \begin_inset Tabular
13659 <lyxtabular version="3" rows="17" columns="2">
13661 <column alignment="center" valignment="top" leftline="true" width="0">
13662 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13663 <row topline="true" bottomline="true">
13664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13681 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13690 <row topline="true">
13691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13722 add floating point numbers
13726 <row topline="true">
13727 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13744 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13758 subtract floating point numbers
13762 <row topline="true">
13763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13780 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13794 divide floating point numbers
13798 <row topline="true">
13799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13816 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13830 multiply floating point numbers
13834 <row topline="true">
13835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13852 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13866 convert floating point to unsigned char
13870 <row topline="true">
13871 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13902 convert floating point to signed char
13906 <row topline="true">
13907 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13924 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13938 convert floating point to unsigned int
13942 <row topline="true">
13943 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13974 convert floating point to signed int
13978 <row topline="true">
13979 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14019 convert floating point to unsigned long
14023 <row topline="true">
14024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14055 convert floating point to signed long
14059 <row topline="true">
14060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14077 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14091 convert unsigned char to floating point
14095 <row topline="true">
14096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14127 convert char to floating point number
14131 <row topline="true">
14132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14149 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14163 convert unsigned int to floating point
14167 <row topline="true">
14168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14199 convert int to floating point numbers
14203 <row topline="true">
14204 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14221 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14235 convert unsigned long to floating point number
14239 <row topline="true" bottomline="true">
14240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14271 convert long to floating point number
14284 These support routines are developed in ANSI-C so there is room for space
14285 and speed improvement
14291 The floating point routines for the mcs51 are implemented in assembler
14295 Note if all these routines are used simultaneously the data space might
14297 For serious floating point usage the large model might be needed.
14298 Also notice that you don't have to call this routines directly.
14299 The compiler will use them automatically every time a floating point operation
14304 \begin_inset LatexCommand \index{Libraries}
14313 <pending: this is messy and incomplete - a little more information is in
14314 sdcc/doc/libdoc.txt
14319 Compiler support routines (_gptrget, _mulint etc.)
14322 Stdclib functions (puts, printf, strcat etc.)
14323 \layout Subsubsection
14329 \begin_inset LatexCommand \index{<stdio.h>}
14333 As usual on embedded systems you have to provide your own
14336 \begin_inset LatexCommand \index{getchar()}
14345 \begin_inset LatexCommand \index{putchar()}
14352 SDCC does not know whether the system connects to a serial line with or
14353 without handshake, LCD, keyboard or other device.
14354 You'll find examples for serial routines f.e.
14355 in sdcc/device/lib.
14361 \begin_inset LatexCommand \index{printf()}
14371 does not support float (except on ds390).
14372 To enable this recompile it with the option
14385 \begin_inset LatexCommand \index{USE\_FLOATS}
14391 on the command line.
14405 \begin_inset LatexCommand \index{-\/-model-large}
14411 for the mcs51 port, since this uses a lot of memory.
14414 If you're short on memory you might want to use
14417 \begin_inset LatexCommand \index{printf\_small()}
14432 For the mcs51 there additionally are assembly versions
14435 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14444 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14453 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14459 which should fit the requirements of many embedded systems (printf_fast()
14460 can be customized by unsetting #defines to
14464 support long variables and field widths).
14467 Math functions (sin, pow, sqrt etc.)
14474 \begin_inset LatexCommand \index{Libraries}
14478 included in SDCC should have a license at least as liberal as the GNU Lesser
14479 General Public License
14480 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14491 license statements for the libraries are missing.
14492 sdcc/device/lib/ser_ir.c
14496 come with a GPL (as opposed to LGPL) License - this will not be liberal
14497 enough for many embedded programmers.
14500 If you have ported some library or want to share experience about some code
14502 falls into any of these categories Busses (I
14503 \begin_inset Formula $^{\textrm{2}}$
14506 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14507 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14508 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14509 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14514 would certainly like to hear about it.
14515 Programmers coding for embedded systems are not especially famous for being
14516 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14517 e these references are very valuable.
14518 Let's help to create a climate where information is shared.
14524 MCS51 Memory Models
14525 \begin_inset LatexCommand \index{Memory model}
14530 \begin_inset LatexCommand \index{MCS51 memory model}
14535 \layout Subsubsection
14540 SDCC allows two memory models for MCS51 code,
14549 Modules compiled with different memory models should
14553 be combined together or the results would be unpredictable.
14554 The library routines supplied with the compiler are compiled as both small
14556 The compiled library modules are contained in separate directories as small
14557 and large so that you can link to either set.
14561 When the large model is used all variables declared without a storage class
14562 will be allocated into the external ram, this includes all parameters and
14563 local variables (for non-reentrant
14564 \begin_inset LatexCommand \index{reentrant}
14569 When the small model is used variables without storage class are allocated
14570 in the internal ram.
14573 Judicious usage of the processor specific storage classes
14574 \begin_inset LatexCommand \index{Storage class}
14578 and the 'reentrant' function type will yield much more efficient code,
14579 than using the large model.
14580 Several optimizations are disabled when the program is compiled using the
14581 large model, it is therefore recommended that the small model be used unless
14582 absolutely required.
14583 \layout Subsubsection
14586 \begin_inset LatexCommand \label{sub:External-Stack}
14591 \begin_inset LatexCommand \index{stack}
14596 \begin_inset LatexCommand \index{External stack (mcs51)}
14603 The external stack (-
14614 \begin_inset LatexCommand \index{-\/-xstack}
14618 ) is located in pdata
14619 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14623 memory (usually at the start of the external ram segment) and uses all
14624 unused space in pdata (max.
14636 -xstack option is used to compile the program, the parameters and local
14638 \begin_inset LatexCommand \index{local variables}
14642 of all reentrant functions are allocated in this area.
14643 This option is provided for programs with large stack space requirements.
14644 When used with the -
14655 \begin_inset LatexCommand \index{-\/-stack-auto}
14659 option, all parameters and local variables are allocated on the external
14660 stack (note: support libraries will need to be recompiled with the same
14662 There is a predefined target in the library makefile).
14665 The compiler outputs the higher order address byte of the external ram segment
14667 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14672 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14676 ), therefore when using the External Stack option, this port
14680 be used by the application program.
14684 \begin_inset LatexCommand \index{Memory model}
14689 \begin_inset LatexCommand \index{DS390 memory model}
14696 The only model supported is Flat 24
14697 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
14702 This generates code for the 24 bit contiguous addressing mode of the Dallas
14704 In this mode, up to four meg of external RAM or code space can be directly
14706 See the data sheets at www.dalsemi.com for further information on this part.
14710 Note that the compiler does not generate any code to place the processor
14711 into 24 bitmode (although
14715 in the ds390 libraries will do that for you).
14721 \begin_inset LatexCommand \index{Tinibios (DS390)}
14725 , the boot loader or similar code must ensure that the processor is in 24
14726 bit contiguous addressing mode before calling the SDCC startup code.
14744 option, variables will by default be placed into the XDATA segment.
14749 Segments may be placed anywhere in the 4 meg address space using the usual
14761 Note that if any segments are located above 64K, the -r flag must be passed
14762 to the linker to generate the proper segment relocations, and the Intel
14763 HEX output format must be used.
14764 The -r flag can be passed to the linker by using the option
14768 on the SDCC command line.
14769 However, currently the linker can not handle code segments > 64k.
14773 \begin_inset LatexCommand \index{Pragmas}
14780 SDCC supports the following #pragma directives:
14784 \begin_inset LatexCommand \index{\#pragma save}
14788 - this will save all current options to the save/restore stack.
14789 See #pragma\SpecialChar ~
14794 \begin_inset LatexCommand \index{\#pragma restore}
14798 - will restore saved options from the last save.
14799 saves & restores can be nested.
14800 SDCC uses a save/restore stack: save pushes current options to the stack,
14801 restore pulls current options from the stack.
14802 See #pragma\SpecialChar ~
14809 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14814 \begin_inset LatexCommand \index{function prologue}
14818 function1[,function2[,function3...]] - The compiler by default uses a caller
14819 saves convention for register saving across function calls, however this
14820 can cause unnecessary register pushing & popping
14821 \begin_inset LatexCommand \index{push/pop}
14825 when calling small functions from larger functions.
14826 This option can be used to switch off the register saving convention for
14827 the function names specified.
14828 The compiler will not save registers when calling these functions, extra
14829 code need to be manually inserted at the entry & exit for these functions
14830 to save & restore the registers used by these functions, this can SUBSTANTIALLY
14831 reduce code & improve run time performance of the generated code.
14832 In the future the compiler (with inter procedural analysis) may be able
14833 to determine the appropriate scheme to use for each function call.
14844 -callee-saves command line option is used, the function names specified
14845 in #pragma\SpecialChar ~
14847 \begin_inset LatexCommand \index{\#pragma callee\_saves}
14851 is appended to the list of functions specified in the command line.
14855 \begin_inset LatexCommand \index{\#pragma exclude}
14859 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
14860 of pairs of push/pop
14861 \begin_inset LatexCommand \index{push/pop}
14870 \begin_inset LatexCommand \index{interrupt}
14883 The directive should be placed immediately before the ISR function definition
14884 and it affects ALL ISR functions following it.
14885 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
14886 exclude\SpecialChar ~
14888 \begin_inset LatexCommand \index{\#pragma exclude}
14893 See also the related keyword _naked
14894 \begin_inset LatexCommand \index{\_naked}
14899 \begin_inset LatexCommand \index{\_\_naked}
14907 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
14911 - the compiler will not warn you anymore for obvious mistakes, you'r on
14915 disable_warning <nnnn>
14916 \begin_inset LatexCommand \index{\#pragma disable\_warning}
14920 - the compiler will not warn you anymore about warning number <nnnn>.
14924 \begin_inset LatexCommand \index{\#pragma nogcse}
14928 - will stop global common subexpression elimination.
14932 \begin_inset LatexCommand \index{\#pragma noinduction}
14936 - will stop loop induction optimizations.
14940 \begin_inset LatexCommand \index{\#pragma noinvariant}
14944 - will not do loop invariant optimizations.
14945 For more details see Loop Invariants in section
14946 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
14954 \begin_inset LatexCommand \index{\#pragma noiv}
14958 - Do not generate interrupt
14959 \begin_inset LatexCommand \index{interrupt}
14963 vector table entries for all ISR functions defined after the pragma.
14964 This is useful in cases where the interrupt vector table must be defined
14965 manually, or when there is a secondary, manually defined interrupt vector
14967 for the autovector feature of the Cypress EZ-USB FX2).
14968 More elegantly this can be achieved by obmitting the optional interrupt
14969 number after the interrupt keyword, see section
14970 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
14979 \begin_inset LatexCommand \index{\#pragma nojtbound}
14983 - will not generate code for boundary value checking, when switch statements
14984 are turned into jump-tables (dangerous).
14985 For more details see section
14986 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
14994 \begin_inset LatexCommand \index{\#pragma noloopreverse}
14998 - Will not do loop reversal optimization
15002 \begin_inset LatexCommand \index{\#pragma nooverlay}
15006 - the compiler will not overlay the parameters and local variables of a
15011 \begin_inset LatexCommand \index{\#pragma stackauto}
15026 \begin_inset LatexCommand \index{-\/-stack-auto}
15031 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15035 Parameters and Local Variables.
15039 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15043 - The compiler will optimize code generation towards fast code, possibly
15044 at the expense of code size.
15048 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15052 - The compiler will optimize code generation towards compact code, possibly
15053 at the expense of code speed.
15057 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15061 - The compiler will attempt to generate code that is both compact and fast,
15062 as long as meeting one goal is not a detriment to the other (this is the
15068 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15072 - Generally follow the C89 standard, but allow SDCC features that conflict
15073 with the standard (default).
15077 \begin_inset LatexCommand \index{\#pragma std\_c89}
15081 - Follow the C89 standard and disable SDCC features that conflict with the
15086 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15090 - Generally follow the C99 standard, but allow SDCC features that conflict
15091 with the standard (incomplete support).
15095 \begin_inset LatexCommand \index{\#pragma std\_c99}
15099 - Follow the C99 standard and disable SDCC features that conflict with the
15100 standard (incomplete support).
15103 SDCPP supports the following #pragma directives:
15107 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15111 (+ | -) - switch _asm _endasm block preprocessing on / off.
15115 The pragma's are intended to be used to turn-on or off certain optimizations
15116 which might cause the compiler to generate extra stack / data space to
15117 store compiler generated temporary variables.
15118 This usually happens in large functions.
15119 Pragma directives should be used as shown in the following example, they
15120 are used to control options & optimizations for a given function; pragmas
15121 should be placed before and/or after a function, placing pragma's inside
15122 a function body could have unpredictable results.
15128 \begin_inset LatexCommand \index{\#pragma save}
15139 /* save the current settings */
15142 \begin_inset LatexCommand \index{\#pragma nogcse}
15151 /* turnoff global subexpression elimination */
15153 #pragma noinduction
15154 \begin_inset LatexCommand \index{\#pragma noinduction}
15158 /* turn off induction optimizations */
15181 \begin_inset LatexCommand \index{\#pragma restore}
15185 /* turn the optimizations back on */
15188 The compiler will generate a warning message when extra space is allocated.
15189 It is strongly recommended that the save and restore pragma's be used when
15190 changing options for a function.
15193 Defines Created by the Compiler
15196 The compiler creates the following #defines
15197 \begin_inset LatexCommand \index{\#defines}
15202 \begin_inset LatexCommand \index{Defines created by the compiler}
15212 \begin_inset Tabular
15213 <lyxtabular version="3" rows="10" columns="2">
15215 <column alignment="center" valignment="top" leftline="true" width="0">
15216 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15217 <row topline="true" bottomline="true">
15218 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15228 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15239 <row topline="true">
15240 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15246 \begin_inset LatexCommand \index{SDCC}
15253 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15258 this Symbol is always defined
15262 <row topline="true">
15263 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15269 \begin_inset LatexCommand \index{SDCC\_mcs51}
15274 \begin_inset LatexCommand \index{SDCC\_ds390}
15279 \begin_inset LatexCommand \index{SDCC\_z80}
15286 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15291 depending on the model used (e.g.: -mds390
15295 <row topline="true">
15296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15302 \begin_inset LatexCommand \index{\_\_mcs51}
15307 \begin_inset LatexCommand \index{\_\_ds390}
15312 \begin_inset LatexCommand \index{\_\_hc08}
15317 \begin_inset LatexCommand \index{\_\_z80}
15324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15329 depending on the model used (e.g.
15334 <row topline="true">
15335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15341 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15371 <row topline="true">
15372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15378 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15385 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15408 <row topline="true">
15409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15415 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15422 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15445 <row topline="true">
15446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15452 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15482 <row topline="true">
15483 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15489 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15496 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15509 <row topline="true" bottomline="true">
15510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15516 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15543 Notes on supported Processors
15547 \begin_inset LatexCommand \label{sub:MCS51-variants}
15552 \begin_inset LatexCommand \index{MCS51 variants}
15559 MCS51 processors are available from many vendors and come in many different
15561 While they might differ considerably in respect to Special Function Registers
15562 the core MCS51 is usually not modified or is kept compatible.
15566 pdata access by SFR
15569 With the upcome of devices with internal xdata and flash memory devices
15571 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15575 as dedicated I/O port is becoming more popular.
15576 Switching the high byte for pdata
15577 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15581 access which was formerly done by port P2 is then achieved by a Special
15583 \begin_inset LatexCommand \index{sfr}
15588 In well-established MCS51 tradition the address of this
15592 is where the chip designers decided to put it.
15593 Needless to say that they didn't agree on a common name either.
15594 So that the startup code can correctly initialize xdata variables, you
15595 should define an sfr with the name _XPAGE
15598 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15604 at the appropriate location if the default, port P2, is not used for this.
15610 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15615 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15620 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15623 For more exotic implementations further customizations may be needed.
15625 \begin_inset LatexCommand \ref{sub:Startup-Code}
15629 for other possibilities.
15632 Other Features available by SFR
15635 Some MCS51 variants offer features like Double DPTR
15636 \begin_inset LatexCommand \index{DPTR}
15640 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
15641 These are currently not used for the MCS51 port.
15642 If you absolutely need them you can fall back to inline assembly or submit
15649 The DS80C400 microcontroller has a rich set of peripherals.
15650 In its built-in ROM library it includes functions to access some of the
15651 features, among them is a TCP stack with IP4 and IP6 support.
15652 Library headers (currently in beta status) and other files are provided
15656 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
15664 The Z80 and gbz80 port
15667 SDCC can target both the Zilog
15668 \begin_inset LatexCommand \index{Z80}
15672 and the Nintendo Gameboy's Z80-like gbz80
15673 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
15678 The Z80 port is passed through the same
15681 \begin_inset LatexCommand \index{Regression test}
15687 as the MCS51 and DS390 ports, so floating point support, support for long
15688 variables and bitfield support is fine.
15689 See mailing lists and forums about interrupt routines.
15692 As always, the code is the authoritative reference - see z80/ralloc.c and
15695 \begin_inset LatexCommand \index{stack}
15699 frame is similar to that generated by the IAR Z80 compiler.
15700 IX is used as the base pointer, HL and IY are used as a temporary registers,
15701 and BC and DE are available for holding variables.
15703 \begin_inset LatexCommand \index{return value}
15707 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
15709 The gbz80 port use the same set of registers for the return values, but
15710 in a different order of significance: E (one byte), DE (two bytes), or
15717 The port to the Motorola HC08
15718 \begin_inset LatexCommand \index{HC08}
15722 family has been added in October 2003, and is still undergoing some basic
15724 The code generator is complete, but the register allocation is still quite
15726 Some of the SDCC's standard C library functions have embedded non-HC08
15727 inline assembly and so are not yet usable.
15738 \begin_inset LatexCommand \index{PIC14}
15742 port still requires a major effort from the development community.
15743 However it can work for very simple code.
15746 C code and 14bit PIC code page
15747 \begin_inset LatexCommand \index{code page (pic14)}
15752 \begin_inset LatexCommand \index{RAM bank (pic14)}
15759 The linker organizes allocation for the code page and RAM banks.
15760 It does not have intimate knowledge of the code flow.
15761 It will put all the code section of a single asm file into a single code
15763 In order to make use of multiple code pages, separate asm files must be
15765 The compiler treats all functions of a single C file as being in the same
15766 code page unless it is non static.
15767 The compiler treats all local variables of a single C file as being in
15768 the same RAM bank unless it is an extern.
15772 To get the best follow these guide lines:
15775 make local functions static, as non static functions require code page selection
15779 Make local variables static as extern variables require RAM bank selection
15783 For devices that have multiple code pages it is more efficient to use the
15784 same number of files as pages, i.e.
15785 for the 16F877 use 4 separate files and i.e.
15786 for the 16F874 use 2 separate files.
15787 This way the linker can put the code for each file into different code
15788 pages and the compiler can allocate reusable variables more efficiently
15789 and there's less page selection overhead.
15790 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
15791 instruction set) use 'unsigned char' whereever possible instead of 'int'.
15794 Creating a device include file
15797 For generating a device include file use the support perl script inc2h.pl
15798 kept in directory support/script.
15804 For the interrupt function, use the keyword 'interrupt'
15805 \begin_inset LatexCommand \index{interrupt}
15809 with level number of 0 (PIC14 only has 1 interrupt so this number is only
15810 there to avoid a syntax error - it ought to be fixed).
15816 void Intr(void) interrupt 0
15822 T0IF = 0; /* Clear timer interrupt */
15827 Linking and assembling
15830 For assembling you can use either GPUTILS'
15831 \begin_inset LatexCommand \index{gputils (pic tools)}
15835 gpasm.exe or MPLAB's mpasmwin.exe.
15836 GPUTILS is available from
15837 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
15842 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
15843 If you use MPLAB and an interrupt function then the linker script file
15844 vectors section will need to be enlarged to link with mplink.
15867 sdcc -S -V -mpic14 -p16F877 $<
15881 $(PRJ).hex: $(OBJS)
15891 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS)
15913 sdcc -S -V -mpic14 -p16F877 $<
15923 mpasmwin /q /o $*.asm
15927 $(PRJ).hex: $(OBJS)
15937 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS)
15940 Please note that indentations within a
15944 have to be done with a tabulator character.
15948 \begin_inset LatexCommand \index{PIC16}
15956 \begin_inset LatexCommand \index{PIC16}
15960 port is the portion of SDCC that is responsible to produce code for the
15962 \begin_inset LatexCommand \index{Microchip}
15966 (TM) microcontrollers with 16 bit core.
15967 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
15968 Currently supported devices are:
15972 \begin_inset Tabular
15973 <lyxtabular version="3" rows="4" columns="6">
15975 <column alignment="center" valignment="top" leftline="true" width="0">
15976 <column alignment="center" valignment="top" leftline="true" width="0">
15977 <column alignment="center" valignment="top" leftline="true" width="0">
15978 <column alignment="center" valignment="top" leftline="true" width="0">
15979 <column alignment="center" valignment="top" leftline="true" width="0">
15980 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15981 <row topline="true">
15982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16031 <row topline="true">
16032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16048 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16056 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16064 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16081 <row topline="true">
16082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16090 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16114 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16122 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16131 <row topline="true" bottomline="true">
16132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16156 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16188 PIC16 port supports the standard command line arguments as supposed, with
16189 the exception of certain cases that will be mentioned in the following
16192 \labelwidthstring 00.00.0000
16204 -callee-saves See -
16216 \labelwidthstring 00.00.0000
16228 -all-callee-saves All function arguments are passed on stack by default.
16231 There is no need to specify this in the command line.
16233 \labelwidthstring 00.00.0000
16245 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16246 no local variables.
16249 Port Specific Options
16250 \begin_inset LatexCommand \index{Options PIC16}
16257 The port specific options appear after the global options in the sdcc --help
16259 \layout Subsubsection
16264 General options enable certain port features and optimizations.
16266 \labelwidthstring 00.00.0000
16278 -stack-model=[model] Used in conjuction with the command above.
16279 Defines the stack model to be used, valid stack models are :
16282 \labelwidthstring 00.00.0000
16288 Selects small stack model.
16289 8 bit stack and frame pointers.
16290 Supports 256 bytes stack size.
16292 \labelwidthstring 00.00.0000
16298 Selects large stack model.
16299 16 bit stack and frame pointers.
16300 Supports 65536 bytes stack size.
16303 \labelwidthstring 00.00.0000
16315 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16316 unitialized data variables with [kword].
16317 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16319 \labelwidthstring 00.00.0000
16331 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16332 Useful for bootloaders.
16334 \labelwidthstring 00.00.0000
16346 -asm= sets the full path and name of an external assembler to call.
16348 \labelwidthstring 00.00.0000
16360 -link= sets the full path and name of an external linker to call.
16362 \labelwidthstring 00.00.0000
16374 -mplab-comp MPLAB compatibility option.
16375 Currently only suppresses special gpasm directives.
16376 \layout Subsubsection
16378 Optimization Options
16380 \labelwidthstring 00.00.0000
16392 -optimize-goto Try to use (conditional) BRA instead of GOTO
16394 \labelwidthstring 00.00.0000
16406 -optimize-cmp Try to optimize some compares.
16408 \labelwidthstring 00.00.0000
16420 -obanksel=nn Set optimization level for inserting BANKSELs.
16425 \labelwidthstring 00.00.0000
16429 \labelwidthstring 00.00.0000
16431 1 checks previous used register and if it is the same then does not emit
16432 BANKSEL, accounts only for labels.
16434 \labelwidthstring 00.00.0000
16436 2 tries to check the location of (even different) symbols and removes BANKSELs
16437 if they are in the same bank.
16442 Important: There might be problems if the linker script has data sections
16443 across bank borders!
16445 \layout Subsubsection
16449 \labelwidthstring 00.00.0000
16461 -nodefaultlibs do not link default libraries when linking
16463 \labelwidthstring 00.00.0000
16475 -no-crt Don't link the default run-time modules
16477 \labelwidthstring 00.00.0000
16489 -use-crt= Use a custom run-time module instead of the defaults.
16490 \layout Subsubsection
16495 Debugging options enable extra debugging information in the output files.
16497 \labelwidthstring 00.00.0000
16509 -debug-xtra Similar to -
16520 \begin_inset LatexCommand \index{-\/-debug}
16524 , but dumps more information.
16526 \labelwidthstring 00.00.0000
16538 -debug-ralloc Force register allocator to dump <source>.d file with debugging
16540 <source> is the name of the file compiled.
16542 \labelwidthstring 00.00.0000
16554 -pcode-verbose Enable pcode debugging information in translation.
16556 \labelwidthstring 00.00.0000
16568 -denable-peeps Force the usage of peepholes.
16571 \labelwidthstring 00.00.0000
16583 -gstack Trace push/pops for stack pointer overflow
16585 \labelwidthstring 00.00.0000
16597 -call-tree dump call tree in .calltree file
16600 Enviromental Variables
16603 There is a number of enviromental variables that can be used when running
16604 SDCC to enable certain optimizations or force a specific program behaviour.
16605 these variables are primarily for debugging purposes so they can be enabled/dis
16609 Currently there is only two such variables available:
16611 \labelwidthstring 00.00.0000
16613 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
16614 bitfields is optimized by directly loading FSR0 with the address of the
16615 bitfield structure.
16616 Normally SDCC will cast the bitfield structure to a bitfield pointer and
16618 This step saves data ram and code space for functions that perform heavy
16621 80 bytes of code space are saved when compiling malloc.c with this option).
16624 \labelwidthstring 00.00.0000
16626 NO_REG_OPT do not perform pCode registers optimization.
16627 This should be used for debugging purposes.
16628 In some where bugs in the pcode optimizer are found, users can benefit
16629 from temporarily disabling the optimizer until the bug is fixed.
16632 Preprocessor Macros
16635 PIC16 port defines the following preprocessor macros while translating a
16640 \begin_inset Tabular
16641 <lyxtabular version="3" rows="6" columns="2">
16643 <column alignment="center" valignment="top" leftline="true" width="0">
16644 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16645 <row topline="true" bottomline="true">
16646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16663 <row topline="true">
16664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16672 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16677 Port identification
16681 <row topline="true">
16682 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16700 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16705 Port identification (same as above)
16709 <row topline="true">
16710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16723 MCU Identification.
16728 is the microcontrol identification number, i.e.
16733 <row topline="true">
16734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16757 MCU Identification (same as above)
16761 <row topline="true" bottomline="true">
16762 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16775 nnn = SMALL or LARGE respectively according to the stack model used
16786 In addition the following macros are defined when calling assembler:
16790 \begin_inset Tabular
16791 <lyxtabular version="3" rows="4" columns="2">
16793 <column alignment="center" valignment="top" leftline="true" width="0">
16794 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16795 <row topline="true" bottomline="true">
16796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16813 <row topline="true">
16814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16827 MCU Identification.
16832 is the microcontrol identification number, i.e.
16837 <row topline="true">
16838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16851 nnn = SMALL or LARGE respectively according to the memory model used for
16856 <row topline="true" bottomline="true">
16857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16865 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16870 nnn = SMALL or LARGE respectively according to the stack model used
16885 \begin_inset LatexCommand \index{PIC16}
16889 port uses the following directories for searching header files and libraries.
16893 \begin_inset Tabular
16894 <lyxtabular version="3" rows="3" columns="4">
16896 <column alignment="center" valignment="top" leftline="true" width="0">
16897 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16898 <column alignment="center" valignment="top" width="0">
16899 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16900 <row topline="true" bottomline="true">
16901 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16909 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16917 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16925 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16934 <row topline="true">
16935 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16940 PREFIX/sdcc/include/pic16
16943 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16948 PIC16 specific headers
16951 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16959 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16968 <row topline="true" bottomline="true">
16969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16974 PREFIX/sdcc/lib/pic16
16977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16982 PIC16 specific libraries
16985 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16993 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17010 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17017 PIC16 port currently supports the following pragmas:
17019 \labelwidthstring 00.00.0000
17021 stack pragma stack forces the code generator to initialize the stack & frame
17022 pointers at a specific address.
17023 This is an adhoc solution for cases where no STACK directive is available
17024 in the linker script or gplink is not instructed to create a stack section.
17026 The stack pragma should be used only once in a project.
17027 Multiple pragmas may result in indeterminate behaviour of the program.
17033 The old format (ie.
17034 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17035 cross page boundaries (or even exceed the available data RAM) and crash
17037 Make sure that stack does not cross page boundaries when using the SMALL
17043 The format is as follows:
17046 #pragma stack bottom_address [stack_size]
17053 is the lower bound of the stack section.
17054 The stack pointer initially will point at address (bottom_address+stack_size-1).
17062 /* initializes stack of 100 bytes at RAM address 0x200 */
17065 #pragma stack 0x200 100
17068 If the stack_size field is omitted then a stack is created with the default
17070 This size might be enough for most programs, but its not enough for operations
17071 with deep function nesting or excessive stack usage.
17073 \labelwidthstring 00.00.0000
17077 This pragma is deprecated.
17078 Its use will cause a warning message to be issued.
17084 \labelwidthstring 00.00.0000
17086 code place a function symbol at static FLASH address
17094 /* place function test_func at 0x4000 */
17097 #pragma code test_func 0x4000
17101 \labelwidthstring 00.00.0000
17103 library instructs the linker to use a library module.
17108 #pragma library module_name
17115 can be any library or object file (including its path).
17116 Note that there are four reserved keywords which have special meaning.
17121 \begin_inset Tabular
17122 <lyxtabular version="3" rows="6" columns="3">
17124 <column alignment="center" valignment="top" leftline="true" width="0">
17125 <column alignment="block" valignment="top" leftline="true" width="20page%">
17126 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17127 <row topline="true" bottomline="true">
17128 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17153 <row topline="true">
17154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17169 ignore all library pragmas
17172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17183 <row topline="true">
17184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17215 <row topline="true">
17216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17231 link the Math libarary
17234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17247 <row topline="true">
17248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17263 link the I/O library
17266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17279 <row topline="true" bottomline="true">
17280 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17295 link the debug library
17298 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17317 * is the device number, i.e.
17318 452 for PIC18F452 MCU.
17321 This feature allows for linking with specific libraries withoug having to
17322 explicit name them in the command line.
17327 keyword will reject all modules specified by the library pragma.
17329 \labelwidthstring 00.00.0000
17331 udata pragma udata instructs the compiler to emit code so that linker will
17332 place a variable at a specific memory bank
17340 /* places variable foo at bank2 */
17343 #pragma udata bank2 foo
17349 In order for this pragma to work extra SECTION directives should be added
17350 in the .lkr script.
17351 In the following example a sample .lkr file is shown:
17356 // Sample linker script for the PIC18F452 processor
17362 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17365 CODEPAGE NAME=page START=0x2A END=0x7FFF
17368 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17371 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17374 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17377 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17380 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17385 DATABANK NAME=gpr0 START=0x80 END=0xFF
17388 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17391 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17394 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17397 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17400 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17403 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17408 SECTION NAME=CONFIG ROM=config
17413 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17416 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17419 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17422 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17425 SECTION NAME=bank4 RAM=gpr4
17428 SECTION NAME=bank5 RAM=gpr5
17431 The linker will recognise the section name set in the pragma statement and
17432 will position the variable at the memory bank set with the RAM field at
17433 the SECTION line in the linker script file.
17437 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17444 There is one main header file that can be included to the source files using
17451 This header file contains the definitions for the processor special registers,
17452 so it is necessary if the source accesses them.
17453 It can be included by adding the following line in the beginning of the
17457 #include <pic18fregs.h>
17460 The specific microcontroller is selected within the pic18fregs.h automatically,
17461 so the same source can be used with a variety of devices.
17467 The libraries that PIC16
17468 \begin_inset LatexCommand \index{PIC16}
17472 port depends on are the microcontroller device libraries which contain
17473 the symbol definitions for the microcontroller special function registers.
17474 These libraries have the format pic18fxxxx.lib, where
17478 is the microcontroller identification number.
17479 The specific library is selected automatically by the compiler at link
17480 stage according to the selected device.
17483 Libraries are created with gplib which is part of the gputils package
17484 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17489 \layout Subsubsection*
17491 Building the libraries
17494 Before using SDCC/pic16 there are some libraries that need to be compiled.
17495 This process is not done automatically by SDCC since not all users use
17496 SDCC for pic16 projects.
17497 So each user should compile the libraries separately.
17500 The steps to compile the pic16 libraries under Linux are:
17503 cd device/lib/pic16
17518 su -c 'make install' # install the libraries, you need the root password
17521 If you need to install the headers too, do:
17527 su -c 'make install' # install the headers, you need the root password
17530 There exist a special target to build the I/O libraries.
17531 This target is not automatically build because it will build the I/O library
17537 This way building will take quite a lot of time.
17538 Users are advised to edit the
17540 device/lib/pic16/pics.build
17542 file and then execute:
17551 The following memory models are supported by the PIC16 port:
17560 Memory model affects the default size of pointers within the source.
17561 The sizes are shown in the next table:
17565 \begin_inset Tabular
17566 <lyxtabular version="3" rows="3" columns="3">
17568 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17569 <column alignment="center" valignment="top" leftline="true" width="0">
17570 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17571 <row topline="true" bottomline="true">
17572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17577 Pointer sizes according to memory model
17580 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17588 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17597 <row topline="true" bottomline="true">
17598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17606 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17614 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17623 <row topline="true" bottomline="true">
17624 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17632 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
17640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17656 It is advisable that all sources within a project are compiled with the
17658 If one wants to override the default memory model, this can be done by
17659 declaring a pointer as
17668 Far selects large memory model's pointers, while near selects small memory
17672 The standard device libraries (see
17673 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
17677 ) contain no reference to pointers, so they can be used with both memory
17684 The stack implementation for the PIC16 port uses two indirect registers,
17687 \labelwidthstring 00.00.0000
17689 FSR1 is assigned as stack pointer
17691 \labelwidthstring 00.00.0000
17693 FSR2 is assigned as frame pointer
17696 The following stack models are supported by the PIC16 port
17717 model means that only the FSRxL byte is used to access stack and frame,
17724 uses both FSRxL and FSRxH registers.
17725 The following table shows the stack/frame pointers sizes according to stack
17726 model and the maximum space they can address:
17730 \begin_inset Tabular
17731 <lyxtabular version="3" rows="3" columns="3">
17733 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17734 <column alignment="center" valignment="top" leftline="true" width="0">
17735 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17736 <row topline="true" bottomline="true">
17737 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17742 Stack & Frame pointer sizes according to stack model
17745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17762 <row topline="true">
17763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17779 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17788 <row topline="true" bottomline="true">
17789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17797 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17825 stack model is currently not working properly throughout the code generator.
17826 So its use is not advised.
17827 Also there are some other points that need special care:
17832 Do not create stack sections with size more than one physical bank (that
17836 Stack sections should no cross physical bank limits (i.e.
17837 #pragma stack 0x50 0x100)
17840 These limitations are caused by the fact that only FSRxL is modified when
17841 using SMALL stack model, so no more than 256 bytes of stack can be used.
17842 This problem will disappear after LARGE model is fully implemented.
17848 In addition to the standard SDCC function keywords, PIC16 port makes available
17851 \labelwidthstring 00.00.0000
17853 wparam Use the WREG to pass one byte of the first function argument.
17854 This improves speed but you may not use this for functions with arguments
17855 that are called via function pointers, otherwise the first byte of the
17856 first parameter will get lost.
17860 void func_wparam(int a) wparam
17866 /* WREG hold the lower part of a */
17869 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
17879 This keyword replaces the deprecated wparam pragma.
17881 \labelwidthstring 00.00.0000
17883 shadowregs When entering/exiting an ISR, it is possible to take advantage
17884 of the PIC18F hardware shadow registers which hold the values of WREG,
17885 STATUS and BSR registers.
17886 This can be done by adding the keyword
17894 keyword in the function's header.
17897 void isr_shadow(void) shadowregs interrupt 1
17913 instructs the code generator not to store/restore WREG, STATUS, BSR when
17914 entering/exiting the ISR.
17917 Function return values
17920 Return values from functions are placed to the appropriate registers following
17921 a modified Microchip policy optimized for SDCC.
17922 The following table shows these registers:
17926 \begin_inset Tabular
17927 <lyxtabular version="3" rows="6" columns="2">
17929 <column alignment="center" valignment="top" leftline="true" width="0">
17930 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17931 <row topline="true" bottomline="true">
17932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17945 destination register
17949 <row topline="true">
17950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17967 <row topline="true">
17968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17985 <row topline="true">
17986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18003 <row topline="true">
18004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18017 FSR0L:PRODH:PRODL:WREG
18021 <row topline="true" bottomline="true">
18022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18035 on stack, FSR0 points to the beginning
18049 An interrupt servive routine (ISR) is declared using the
18056 void isr(void) interrupt
18074 is the interrupt number, which for PIC18F devices can be:
18078 \begin_inset Tabular
18079 <lyxtabular version="3" rows="4" columns="3">
18081 <column alignment="center" valignment="top" leftline="true" width="0">
18082 <column alignment="center" valignment="top" leftline="true" width="0">
18083 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18084 <row topline="true" bottomline="true">
18085 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18108 Interrupt Vector Address
18112 <row topline="true">
18113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18138 <row topline="true">
18139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18170 HIGH priority interrupts
18173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18182 <row topline="true" bottomline="true">
18183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18196 LOW priority interrupts
18199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18215 When generating assembly code for ISR the code generator places a
18221 Interrupt Vector Address
18223 which points at the genetated ISR.
18224 This single GOTO instruction is part of an automatically generated
18226 interrupt entry point
18229 The actuall ISR code is placed as normally would in the code space.
18230 Upon interrupt request, the GOTO instruction is executed which jumps to
18232 When declaring interrupt functions as _naked this GOTO instruction is
18237 The whole interrupt functions is therefore placed at the Interrupt Vector
18238 Address of the specific interrupt.
18239 This is not a problem for the LOW priority interrupts, but it is a problem
18240 for the RESET and the HIGH priority interrupts because code may be written
18241 at the next interrupt´s vector address and cause undeterminate program
18242 behaviour if that interrupt is raised.
18248 This is not a problem when
18251 this is a HIGH interrupt ISR and LOW interrupts are
18258 when the ISR is small enough not to reach the next interrupt´s vector address.
18268 is possible to be omitted.
18269 This way a function is generated similar to an ISR, but it is not assigned
18273 When entering an interrupt, currently the PIC16
18274 \begin_inset LatexCommand \index{PIC16}
18278 port automatically saves the following registers:
18290 PROD (PRODL and PRODH)
18293 FSR0 (FSR0L and FSR0H)
18296 These registers are restored upon return from the interrupt routine.
18302 NOTE that when the _naked attribute is specified for an interrupt routine,
18303 then NO registers are stored or restored.
18312 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18313 There are 3 types of generic pointers currently implemented data, code
18314 and eeprom pointers.
18315 They are differentiated by the value of the 7th and 6th bits of the upper
18320 \begin_inset Tabular
18321 <lyxtabular version="3" rows="5" columns="5">
18323 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18324 <column alignment="center" valignment="top" width="0">
18325 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18326 <column alignment="center" valignment="top" width="0">
18327 <column alignment="left" valignment="top" rightline="true" width="0">
18328 <row topline="true" bottomline="true">
18329 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18337 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18345 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18358 rest of the pointer
18361 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18370 <row topline="true" bottomline="true">
18371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18379 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18387 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18395 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18404 uuuuuu uuuuxxxx xxxxxxxx
18407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18412 a 12-bit data pointer in data RAM memory
18416 <row bottomline="true">
18417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18441 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18450 uxxxxx xxxxxxxx xxxxxxxx
18453 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18458 a 21-bit code pointer in FLASH memory
18462 <row bottomline="true">
18463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18496 uuuuuu uuuuuuxx xxxxxxxx
18499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18504 a 10-bit eeprom pointer in EEPROM memory
18508 <row bottomline="true">
18509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18525 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18542 xxxxxx xxxxxxxx xxxxxxxx
18545 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18550 unimplemented pointer type
18561 Generic pointer are read and written with a set of library functions which
18562 read/write 1, 2, 3, 4 bytes.
18566 \layout Subsubsection
18568 Standard I/O Streams
18575 the type FILE is defined as:
18578 typedef char * FILE;
18581 This type is the stream type implemented I/O in the PIC18F devices.
18582 Also the standard input and output streams are declared in stdio.h:
18585 extern FILE * stdin;
18588 extern FILE * stdout;
18591 The FILE type is actually a generic pointer which defines one more type
18592 of generic pointers, the
18597 This new type has the format:
18601 \begin_inset Tabular
18602 <lyxtabular version="3" rows="2" columns="7">
18604 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18605 <column alignment="center" valignment="top" width="0">
18606 <column alignment="center" valignment="top" leftline="true" width="0">
18607 <column alignment="center" valignment="top" leftline="true" width="0">
18608 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18609 <column alignment="center" valignment="top" width="0">
18610 <column alignment="left" valignment="top" rightline="true" width="0">
18611 <row topline="true" bottomline="true">
18612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18628 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18636 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18644 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18652 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18657 rest of the pointer
18660 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18669 <row topline="true" bottomline="true">
18670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18710 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18727 upper byte high nubble is 0x2n, the rest are zeroes
18738 Currently implemented there are 3 types of streams defined:
18742 \begin_inset Tabular
18743 <lyxtabular version="3" rows="4" columns="4">
18745 <column alignment="center" valignment="top" leftline="true" width="0">
18746 <column alignment="center" valignment="top" leftline="true" width="0">
18747 <column alignment="center" valignment="top" leftline="true" width="0">
18748 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18749 <row topline="true" bottomline="true">
18750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18758 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18774 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18783 <row topline="true">
18784 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18792 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18802 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18810 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18815 Writes/Reads characters via the USART peripheral
18819 <row topline="true">
18820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18828 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18851 Writes/Reads characters via the MSSP peripheral
18855 <row topline="true" bottomline="true">
18856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18874 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18882 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18887 Writes/Reads characters via used defined functions
18898 The stream identifiers are declared as macros in the stdio.h header.
18901 In the libc library there exist the functions that are used to write to
18902 each of the above streams.
18905 \labelwidthstring 00.00.0000
18917 _stream_usart_putchar writes a character at the USART stream
18919 \labelwidthstring 00.00.0000
18931 _stream_mssp_putchar writes a character at the MSSP stream
18933 \labelwidthstring 00.00.0000
18935 putchar dummy function.
18936 This writes a character to a user specified manner.
18939 In order to increase performance
18943 is declared in stdio.h as having its parameter in WREG (it has the wparam
18945 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
18946 in a user-friendly way.
18951 is the name of the variable that holds the character to print.
18952 An example follows:
18955 #include <pic18fregs.h>
18967 PORTA = c; /* dump character c to PORTA */
18980 stdout = STREAM_USER; /* this is not necessery, since stdout points
18983 * by default to STREAM_USER */
18986 printf (¨This is a printf test
18994 \layout Subsubsection
18999 PIC16 contains an implementation of the printf-family of functions.
19000 There exist the following functions:
19003 extern unsigned int sprintf(char *buf, char *fmt, ...);
19006 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19011 extern unsigned int printf(char *fmt, ...);
19014 extern unsigned int vprintf(char *fmt, va_lista ap);
19019 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19022 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19025 For sprintf and vsprintf
19029 should normally be a data pointer where the resulting string will be placed.
19030 No range checking is done so the user should allocate the necessery buffer.
19031 For fprintf and vfprintf
19035 should be a stream pointer (i.e.
19036 stdout, STREAM_MSSP, etc...).
19037 \layout Subsubsection
19042 The PIC18F family of microcontrollers supports a number of interrupt sources.
19043 A list of these interrupts is shown in the following table:
19047 \begin_inset Tabular
19048 <lyxtabular version="3" rows="11" columns="4">
19050 <column alignment="left" valignment="top" leftline="true" width="0">
19051 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19052 <column alignment="left" valignment="top" leftline="true" width="0">
19053 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19054 <row topline="true" bottomline="true">
19055 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19063 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19071 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19079 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19088 <row topline="true">
19089 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19097 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19102 PORTB change interrupt
19105 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19118 EEPROM/FLASH write complete interrupt
19122 <row topline="true">
19123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19136 INT0 external interrupt
19139 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19152 Bus collision interrupt
19156 <row topline="true">
19157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19170 INT1 external interrupt
19173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19181 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19186 Low voltage detect interrupt
19190 <row topline="true">
19191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19204 INT2 external interrupt
19207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19220 Parallel slave port interrupt
19224 <row topline="true">
19225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19233 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19238 CCP1 module interrupt
19241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19254 AD convertion complete interrupt
19258 <row topline="true">
19259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19267 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19272 CCP2 module interrupt
19275 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19283 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19288 USART receive interrupt
19292 <row topline="true">
19293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19306 TMR0 overflow interrupt
19309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19322 USART transmit interrupt
19326 <row topline="true">
19327 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19335 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19340 TMR1 overflow interrupt
19343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19351 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19356 SSP receive/transmit interrupt
19360 <row topline="true">
19361 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19369 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19374 TMR2 matches PR2 interrupt
19377 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19392 <row topline="true" bottomline="true">
19393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19401 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19406 TMR3 overflow interrupt
19409 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19431 The prototypes for these names are defined in the header file
19438 In order to simplify signal handling, a number of macros is provided:
19440 \labelwidthstring 00.00.0000
19442 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19443 high priority interrupts.
19448 is the function name to use.
19450 \labelwidthstring 00.00.0000
19452 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19453 low priority interrupt.
19458 is the function name to use.
19460 \labelwidthstring 00.00.0000
19462 DEF_HANDLER(sig,handler) define a handler for signal
19466 \labelwidthstring 00.00.0000
19468 END_DEF end the declaration of the dispatch table.
19471 Additionally there are two more macros to simplify the declaration of the
19474 \labelwidthstring 00.00.0000
19478 SIGHANDLER(handler)
19480 this declares the function prototype for the
19486 \labelwidthstring 00.00.0000
19488 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19491 An example of using the macros above is shown below:
19494 #include <pic18fregs.h>
19497 #include <signal.h>
19501 DEF_INTHIGH(high_int)
19504 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
19507 DEF_HANDLER(SIG_BCOL, _bcol_handler)
19514 SIGHANDLER(_tmr0_handler)
19520 /* action to be taken when timer 0 overflows */
19527 SIGHANDLERNAKED(_bcol_handler)
19536 /* action to be taken when bus collision occurs */
19552 Special care should be taken when using the above scheme:
19555 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
19558 when declaring SIGHANDLERNAKED handler never forget to use
19562 for proper returning.
19568 Here you can find some general tips for compiling programs with SDCC/pic16.
19569 \layout Subsubsection
19574 The default stack size (that is 64 bytes) probably is enough for many programs.
19575 One must take care that when there are many levels of function nesting,
19576 or there is excessive usage of stack, its size should be extended.
19577 An example of such a case is the printf/sprintf family of functions.
19578 If you encounter problems like not being able to print integers, then you
19579 need to set the stack size around the maximum (256 for small stack model).
19580 The following diagram shows what happens when calling printf to print an
19584 printf () --> ltoa () --> ultoa () --> divschar ()
19587 It is should be understood that stack is easily consumed when calling complicate
19589 Using command line arguments like -
19599 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
19601 Other ways to reduce stack usage may exist.
19604 Debugging with SDCDB
19605 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
19610 \begin_inset LatexCommand \index{sdcdb (debugger)}
19617 SDCC is distributed with a source level debugger
19618 \begin_inset LatexCommand \index{Debugger}
19623 The debugger uses a command line interface, the command repertoire of the
19624 debugger has been kept as close to gdb
19625 \begin_inset LatexCommand \index{gdb}
19629 (the GNU debugger) as possible.
19630 The configuration and build process is part of the standard compiler installati
19631 on, which also builds and installs the debugger in the target directory
19632 specified during configuration.
19633 The debugger allows you debug BOTH at the C source and at the ASM source
19635 Sdcdb is available on Unix platforms only.
19638 Compiling for Debugging
19652 \begin_inset LatexCommand \index{-\/-debug}
19656 option must be specified for all files for which debug information is to
19658 The complier generates a .adb file for each of these files.
19659 The linker creates the .cdb
19660 \begin_inset LatexCommand \index{<file>.cdb}
19665 \begin_inset LatexCommand \index{<file>.adb}
19669 files and the address information.
19670 This .cdb is used by the debugger.
19673 How the Debugger Works
19686 -debug option is specified the compiler generates extra symbol information
19687 some of which are put into the assembler source and some are put into the
19689 Then the linker creates the .cdb file from the individual .adb files with
19690 the address information for the symbols.
19691 The debugger reads the symbolic information generated by the compiler &
19692 the address information generated by the linker.
19693 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
19694 execution is controlled by the debugger.
19695 When a command is issued for the debugger, it translates it into appropriate
19696 commands for the simulator.
19699 Starting the Debugger
19702 The debugger can be started using the following command line.
19703 (Assume the file you are debugging has the file name foo).
19717 The debugger will look for the following files.
19720 foo.c - the source file.
19723 foo.cdb - the debugger symbol information file.
19726 foo.ihx - the Intel hex format
19727 \begin_inset LatexCommand \index{Intel hex format}
19734 Command Line Options.
19747 -directory=<source file directory> this option can used to specify the directory
19749 The debugger will look into the directory list specified for source, cdb
19751 The items in the directory list must be separated by ':', e.g.
19752 if the source files can be in the directories /home/src1 and /home/src2,
19763 -directory option should be -
19773 -directory=/home/src1:/home/src2.
19774 Note there can be no spaces in the option.
19778 -cd <directory> - change to the <directory>.
19781 -fullname - used by GUI front ends.
19784 -cpu <cpu-type> - this argument is passed to the simulator please see the
19785 simulator docs for details.
19788 -X <Clock frequency > this options is passed to the simulator please see
19789 the simulator docs for details.
19792 -s <serial port file> passed to simulator see the simulator docs for details.
19795 -S <serial in,out> passed to simulator see the simulator docs for details.
19798 -k <port number> passed to simulator see the simulator docs for details.
19804 As mentioned earlier the command interface for the debugger has been deliberatel
19805 y kept as close the GNU debugger gdb, as possible.
19806 This will help the integration with existing graphical user interfaces
19807 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
19808 If you use a graphical user interface for the debugger you can skip the
19810 \layout Subsubsection*
19812 break [line | file:line | function | file:function]
19815 Set breakpoint at specified line or function:
19824 sdcdb>break foo.c:100
19826 sdcdb>break funcfoo
19828 sdcdb>break foo.c:funcfoo
19829 \layout Subsubsection*
19831 clear [line | file:line | function | file:function ]
19834 Clear breakpoint at specified line or function:
19843 sdcdb>clear foo.c:100
19845 sdcdb>clear funcfoo
19847 sdcdb>clear foo.c:funcfoo
19848 \layout Subsubsection*
19853 Continue program being debugged, after breakpoint.
19854 \layout Subsubsection*
19859 Execute till the end of the current function.
19860 \layout Subsubsection*
19865 Delete breakpoint number 'n'.
19866 If used without any option clear ALL user defined break points.
19867 \layout Subsubsection*
19869 info [break | stack | frame | registers ]
19872 info break - list all breakpoints
19875 info stack - show the function call stack.
19878 info frame - show information about the current execution frame.
19881 info registers - show content of all registers.
19882 \layout Subsubsection*
19887 Step program until it reaches a different source line.
19888 Note: pressing <return> repeats the last command.
19889 \layout Subsubsection*
19894 Step program, proceeding through subroutine calls.
19895 \layout Subsubsection*
19900 Start debugged program.
19901 \layout Subsubsection*
19906 Print type information of the variable.
19907 \layout Subsubsection*
19912 print value of variable.
19913 \layout Subsubsection*
19918 load the given file name.
19919 Note this is an alternate method of loading file for debugging.
19920 \layout Subsubsection*
19925 print information about current frame.
19926 \layout Subsubsection*
19931 Toggle between C source & assembly source.
19932 \layout Subsubsection*
19934 ! simulator command
19937 Send the string following '!' to the simulator, the simulator response is
19939 Note the debugger does not interpret the command being sent to the simulator,
19940 so if a command like 'go' is sent the debugger can loose its execution
19941 context and may display incorrect values.
19942 \layout Subsubsection*
19949 My name is Bobby Brown"
19952 Interfacing with XEmacs
19953 \begin_inset LatexCommand \index{XEmacs}
19958 \begin_inset LatexCommand \index{Emacs}
19965 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
19966 sdcdb.el and sdcdbsrc.el.
19967 These two files can be found in the $(prefix)/bin directory after the installat
19969 These files need to be loaded into XEmacs for the interface to work.
19970 This can be done at XEmacs startup time by inserting the following into
19971 your '.xemacs' file (which can be found in your HOME directory):
19977 (load-file sdcdbsrc.el)
19983 .xemacs is a lisp file so the () around the command is REQUIRED.
19984 The files can also be loaded dynamically while XEmacs is running, set the
19985 environment variable 'EMACSLOADPATH' to the installation bin directory
19986 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
19987 To start the interface enter the following command:
20001 You will prompted to enter the file name to be debugged.
20006 The command line options that are passed to the simulator directly are bound
20007 to default values in the file sdcdbsrc.el.
20008 The variables are listed below, these values maybe changed as required.
20011 sdcdbsrc-cpu-type '51
20014 sdcdbsrc-frequency '11059200
20017 sdcdbsrc-serial nil
20020 The following is a list of key mapping for the debugger interface.
20031 ;;key\SpecialChar ~
20045 binding\SpecialChar ~
20069 ;;---\SpecialChar ~
20083 -------\SpecialChar ~
20125 sdcdb-next-from-src\SpecialChar ~
20153 sdcdb-back-from-src\SpecialChar ~
20181 sdcdb-cont-from-src\SpecialChar ~
20191 SDCDB continue command
20209 sdcdb-step-from-src\SpecialChar ~
20237 sdcdb-whatis-c-sexp\SpecialChar ~
20247 SDCDB ptypecommand for data at
20314 sdcdbsrc-delete\SpecialChar ~
20328 SDCDB Delete all breakpoints if no arg
20377 given or delete arg (C-u arg x)
20395 sdcdbsrc-frame\SpecialChar ~
20410 SDCDB Display current frame if no arg,
20459 given or display frame arg
20526 sdcdbsrc-goto-sdcdb\SpecialChar ~
20536 Goto the SDCDB output buffer
20554 sdcdb-print-c-sexp\SpecialChar ~
20565 SDCDB print command for data at
20632 sdcdbsrc-goto-sdcdb\SpecialChar ~
20642 Goto the SDCDB output buffer
20660 sdcdbsrc-mode\SpecialChar ~
20676 Toggles Sdcdbsrc mode (turns it off)
20691 sdcdb-finish-from-src\SpecialChar ~
20699 SDCDB finish command
20714 sdcdb-break\SpecialChar ~
20732 Set break for line with point
20747 sdcdbsrc-mode\SpecialChar ~
20763 Toggle Sdcdbsrc mode
20778 sdcdbsrc-srcmode\SpecialChar ~
20801 Here are a few guidelines that will help the compiler generate more efficient
20802 code, some of the tips are specific to this compiler others are generally
20803 good programming practice.
20806 Use the smallest data type to represent your data-value.
20807 If it is known in advance that the value is going to be less than 256 then
20808 use an 'unsigned char' instead of a 'short' or 'int'.
20809 Please note, that ANSI C requires both signed and unsigned chars to be
20810 promoted to 'signed int' before doing any operation.
20812 \begin_inset LatexCommand \index{type promotion}
20816 can be omitted, if the result is the same.
20817 The effect of the promotion rules together with the sign-extension is often
20824 unsigned char uc = 0xfe;
20826 if (uc * uc < 0) /* this is true! */
20845 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
20855 (unsigned char) -12 / (signed char) -3 = ...
20858 No, the result is not 4:
20863 (int) (unsigned char) -12 / (int) (signed char) -3 =
20865 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
20867 (int) 0x00f4 / (int) 0xfffd =
20869 (int) 0x00f4 / (int) 0xfffd =
20871 (int) 244 / (int) -3 =
20873 (int) -81 = (int) 0xffaf;
20876 Don't complain, that gcc gives you a different result.
20877 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
20878 Therefore the results are different.
20881 \begin_inset Quotes sld
20885 \begin_inset Quotes srd
20891 If well-defined overflow characteristics are important and negative values
20892 are not, or if you want to steer clear of sign-extension problems when
20893 manipulating bits or bytes, use one of the corresponding unsigned types.
20894 (Beware when mixing signed and unsigned values in expressions, though.)
20896 Although character types (especially unsigned char) can be used as "tiny"
20897 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
20898 ble sign extension and increased code size.
20902 Use unsigned when it is known in advance that the value is not going to
20904 This helps especially if you are doing division or multiplication, bit-shifting
20905 or are using an array index.
20908 NEVER jump into a LOOP.
20911 Declare the variables to be local
20912 \begin_inset LatexCommand \index{local variables}
20916 whenever possible, especially loop control variables (induction).
20919 Since the compiler does not always do implicit integral promotion, the programme
20920 r should do an explicit cast when integral promotion is required.
20923 Reducing the size of division, multiplication & modulus operations can reduce
20924 code size substantially.
20925 Take the following code for example.
20931 foobar(unsigned int p1, unsigned char ch)
20939 unsigned char ch1 = p1 % ch ;
20950 For the modulus operation the variable ch will be promoted to unsigned int
20951 first then the modulus operation will be performed (this will lead to a
20952 call to support routine _moduint()), and the result will be casted to a
20954 If the code is changed to
20959 foobar(unsigned int p1, unsigned char ch)
20967 unsigned char ch1 = (unsigned char)p1 % ch ;
20978 It would substantially reduce the code generated (future versions of the
20979 compiler will be smart enough to detect such optimization opportunities).
20983 Have a look at the assembly listing to get a
20984 \begin_inset Quotes sld
20988 \begin_inset Quotes srd
20991 for the code generation.
20995 \begin_inset LatexCommand \index{Tools}
20999 included in the distribution
21003 \begin_inset Tabular
21004 <lyxtabular version="3" rows="12" columns="3">
21006 <column alignment="center" valignment="top" leftline="true" width="0pt">
21007 <column alignment="center" valignment="top" leftline="true" width="0pt">
21008 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21009 <row topline="true" bottomline="true">
21010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21035 <row topline="true">
21036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21049 Simulator for various architectures
21052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21061 <row topline="true">
21062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21075 header file conversion
21078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21083 sdcc/support/scripts
21087 <row topline="true">
21088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21101 header file conversion
21104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21109 sdcc/support/scripts
21113 <row topline="true">
21114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21148 <row topline="true">
21149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21157 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21165 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21183 <row topline="true">
21184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21218 <row topline="true">
21219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21253 <row topline="true">
21254 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21270 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21288 <row topline="true">
21289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21323 <row topline="true">
21324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21332 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21358 <row topline="true" bottomline="true">
21359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21403 \begin_inset LatexCommand \index{Documentation}
21407 included in the distribution
21411 \begin_inset Tabular
21412 <lyxtabular version="3" rows="10" columns="2">
21414 <column alignment="left" valignment="top" leftline="true" width="0">
21415 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21416 <row topline="true" bottomline="true">
21417 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21430 Where to get / filename
21434 <row topline="true">
21435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21440 SDCC Compiler User Guide
21443 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21448 You're reading it right now
21452 <row topline="true">
21453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21461 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21470 <row topline="true">
21471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21477 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
21482 \begin_inset LatexCommand \index{Assembler documentation}
21486 Assemblers and ASLINK
21487 \begin_inset LatexCommand \index{aslink}
21492 \begin_inset LatexCommand \index{Linker documentation}
21499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21504 sdcc/as/doc/asxhtm.html
21508 <row topline="true">
21509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21514 SDCC regression test
21515 \begin_inset LatexCommand \index{Regression test}
21522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21527 sdcc/doc/test_suite_spec.pdf
21531 <row topline="true">
21532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21549 <row topline="true">
21550 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21555 Notes on debugging with sdcdb
21556 \begin_inset LatexCommand \index{sdcdb (debugger)}
21563 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21568 sdcc/debugger/README
21572 <row topline="true">
21573 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21578 Software simulator for microcontrollers
21581 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21608 <row topline="true">
21609 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21614 Temporary notes on the pic16
21615 \begin_inset LatexCommand \index{PIC16}
21622 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21627 sdcc/src/pic16/NOTES
21631 <row topline="true" bottomline="true">
21632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21637 SDCC internal documentation (debugging file format)
21640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21676 Related open source tools
21677 \begin_inset LatexCommand \index{Related tools}
21685 \begin_inset Tabular
21686 <lyxtabular version="3" rows="11" columns="3">
21688 <column alignment="center" valignment="top" leftline="true" width="0pt">
21689 <column alignment="block" valignment="top" leftline="true" width="30line%">
21690 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
21691 <row topline="true" bottomline="true">
21692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21717 <row topline="true">
21718 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21724 \begin_inset LatexCommand \index{gpsim (pic simulator)}
21731 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21745 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
21753 <row topline="true">
21754 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21760 \begin_inset LatexCommand \index{gputils (pic tools)}
21767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21781 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
21789 <row topline="true">
21790 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21812 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
21820 <row topline="true">
21821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21827 \begin_inset LatexCommand \index{indent (source formatting tool)}
21834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21839 Formats C source - Master of the white spaces
21842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21848 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
21856 <row topline="true">
21857 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21863 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
21870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21875 Object file conversion, checksumming, ...
21878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21884 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
21892 <row topline="true">
21893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21899 \begin_inset LatexCommand \index{objdump (tool)}
21906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21911 Object file conversion, ...
21914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21919 Part of binutils (should be there anyway)
21923 <row topline="true">
21924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21930 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
21937 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21942 Source code documentation system
21945 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21951 \begin_inset LatexCommand \url{http://www.doxygen.org}
21959 <row topline="true">
21960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21973 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
21976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21982 \begin_inset LatexCommand \url{http://www.kdevelop.org}
21990 <row topline="true">
21991 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21997 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22009 Statically checks c sources (has anyone adapted splint for SDCC?)
22012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22018 \begin_inset LatexCommand \url{http://www.splint.org}
22026 <row topline="true" bottomline="true">
22027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22033 \begin_inset LatexCommand \index{ddd (debugger)}
22040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22045 Debugger, serves nicely as GUI to sdcdb
22046 \begin_inset LatexCommand \index{sdcdb (debugger)}
22053 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22059 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22076 Related documentation / recommended reading
22080 \begin_inset Tabular
22081 <lyxtabular version="3" rows="6" columns="3">
22083 <column alignment="center" valignment="top" leftline="true" width="0pt">
22084 <column alignment="block" valignment="top" leftline="true" width="30line%">
22085 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22086 <row topline="true" bottomline="true">
22087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22112 <row topline="true">
22113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22136 \begin_inset LatexCommand \index{C Reference card}
22143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22149 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22157 <row topline="true">
22158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22174 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22180 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22188 <row topline="true">
22189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22196 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22201 Latest datasheet of the target CPU
22204 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22213 <row topline="true">
22214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22226 Revision history of datasheet
22229 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22238 <row topline="true" bottomline="true">
22239 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22249 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22254 Advanced Compiler Design and Implementation
22257 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22262 bookstore (very dedicated, probably read other books first)
22278 Some questions answered, some pointers given - it might be time to in turn
22286 can you solve your project with the selected microcontroller? Would you
22287 find out early or rather late that your target is too small/slow/whatever?
22288 Can you switch to a slightly better device if it doesn't fit?
22291 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22292 and/or another programming language be more adequate? Would an operating
22293 system on the target device help?
22296 if you solved the problem, will the marketing department be happy?
22299 if the marketing department is happy, will customers be happy?
22302 if you're the project manager, marketing department and maybe even the customer
22303 in one person, have you tried to see the project from the outside?
22306 is the project done if you think it is done? Or is just that other interface/pro
22307 tocol/feature/configuration/option missing? How about website, manual(s),
22308 internationali(z|s)ation, packaging, labels, 2nd source for components,
22309 electromagnetic compatability/interference, documentation for production,
22310 production test software, update mechanism, patent issues?
22313 is your project adequately positioned in that magic triangle: fame, fortune,
22317 Maybe not all answers to these questions are known and some answers may
22322 , nevertheless knowing these questions may help you to avoid burnout
22328 burnout is bad for electronic devices, programmers and motorcycle tyres
22332 Chances are you didn't want to hear some of them...
22336 \begin_inset LatexCommand \index{Support}
22343 SDCC has grown to be a large project.
22344 The compiler alone (without the preprocessor, assembler and linker) is
22345 well over 100,000 lines of code (blank stripped).
22346 The open source nature of this project is a key to its continued growth
22348 You gain the benefit and support of many active software developers and
22350 Is SDCC perfect? No, that's why we need your help.
22351 The developers take pride in fixing reported bugs.
22352 You can help by reporting the bugs and helping other SDCC users.
22353 There are lots of ways to contribute, and we encourage you to take part
22354 in making SDCC a great software package.
22358 The SDCC project is hosted on the SDCC sourceforge site at
22359 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22364 You'll find the complete set of mailing lists
22365 \begin_inset LatexCommand \index{Mailing list(s)}
22369 , forums, bug reporting system, patch submission
22370 \begin_inset LatexCommand \index{Patch submission}
22375 \begin_inset LatexCommand \index{download}
22379 area and cvs code repository
22380 \begin_inset LatexCommand \index{cvs code repository}
22388 \begin_inset LatexCommand \index{Bug reporting}
22393 \begin_inset LatexCommand \index{Reporting bugs}
22400 The recommended way of reporting bugs is using the infrastructure of the
22402 You can follow the status of bug reports there and have an overview about
22406 Bug reports are automatically forwarded to the developer mailing list and
22407 will be fixed ASAP.
22408 When reporting a bug, it is very useful to include a small test program
22409 (the smaller the better) which reproduces the problem.
22410 If you can isolate the problem by looking at the generated assembly code,
22411 this can be very helpful.
22412 Compiling your program with the -
22423 \begin_inset LatexCommand \index{-\/-dumpall}
22427 option can sometimes be useful in locating optimization problems.
22428 When reporting a bug please maker sure you:
22431 Attach the code you are compiling with SDCC.
22435 Specify the exact command you use to run SDCC, or attach your Makefile.
22439 Specify the SDCC version (type "
22445 "), your platform, and operating system.
22449 Provide an exact copy of any error message or incorrect output.
22453 Put something meaningful in the subject of your message.
22456 Please attempt to include these 5 important parts, as applicable, in all
22457 requests for support or when reporting any problems or bugs with SDCC.
22458 Though this will make your message lengthy, it will greatly improve your
22459 chance that SDCC users and developers will be able to help you.
22460 Some SDCC developers are frustrated by bug reports without code provided
22461 that they can use to reproduce and ultimately fix the problem, so please
22462 be sure to provide sample code if you are reporting a bug!
22465 Please have a short check that you are using a recent version of SDCC and
22466 the bug is not yet known.
22467 This is the link for reporting bugs:
22468 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
22475 Requesting Features
22476 \begin_inset LatexCommand \label{sub:Requesting-Features}
22481 \begin_inset LatexCommand \index{Feature request}
22486 \begin_inset LatexCommand \index{Requesting features}
22493 Like bug reports feature requests are forwarded to the developer mailing
22495 This is the link for requesting features:
22496 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
22506 Like bug reports contributed patches are forwarded to the developer mailing
22508 This is the link for submitting patches
22509 \begin_inset LatexCommand \index{Patch submission}
22514 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
22521 You need to specify some parameters to the
22525 command for the patches to be useful.
22526 If you modified more than one file a patch created f.e.
22531 \begin_inset Quotes sld
22534 diff -Naur unmodified_directory modified_directory >my_changes.patch
22535 \begin_inset Quotes srd
22541 will be fine, otherwise
22545 \begin_inset Quotes sld
22548 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
22549 \begin_inset Quotes srd
22562 These links should take you directly to the
22563 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
22573 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
22574 automated messages (mid 2003)
22578 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
22583 \begin_inset LatexCommand \index{Mailing list(s)}
22587 and forums are archived and searchable so if you are lucky someone already
22588 had a similar problem.
22589 While mails to the lists themselves are delivered promptly their web front
22590 end on sourceforge sometimes shows a severe time lag (up to several weeks),
22591 if you're seriously using SDCC please consider subscribing to the lists.
22597 You can follow the status of the cvs version
22598 \begin_inset LatexCommand \index{version}
22602 of SDCC by watching the Changelog
22603 \begin_inset LatexCommand \index{Changelog}
22607 in the cvs-repository
22612 \begin_inset LatexCommand \htmlurl{http://cvs.sf.net/cgi-bin/viewcvs.cgi/*checkout*/sdcc/sdcc/ChangeLog?rev=HEAD&content-type=text/plain}
22620 \begin_inset LatexCommand \index{Release policy}
22627 Historically there often were long delays between official releases and
22628 the sourceforge download area tends to get not updated at all.
22629 Excuses in the past might have referred to problems with live range analysis,
22630 but as this was fixed a while ago, the current problem is that another
22631 excuse has to be found.
22632 Kidding aside, we have to get better there! On the other hand there are
22633 daily snapshots available at
22634 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
22638 , and you can always build the very last version (hopefully with many bugs
22639 fixed, and features added) from the source code available at
22640 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
22648 \begin_inset LatexCommand \index{Examples}
22655 You'll find some small examples in the directory
22657 sdcc/device/examples/.
22660 More examples and libraries are available at
22662 The SDCC Open Knowledge Resource
22663 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
22670 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
22677 I did insert a reference to Paul's web site here although it seems rather
22678 dedicated to a specific 8032 board (I think it's okay because it f.e.
22679 shows LCD/Harddisc interface and has a free 8051 monitor.
22680 Independent 8032 board vendors face hard competition of heavily subsidized
22681 development boards anyway).
22684 Maybe we should include some links to real world applications.
22685 Preferably pointer to pointers (one for each architecture) so this stays
22690 \begin_inset LatexCommand \index{Quality control}
22697 The compiler is passed through nightly compile and build checks.
22703 \begin_inset LatexCommand \index{Regression test}
22707 check that SDCC itself compiles flawlessly on several platforms and checks
22708 the quality of the code generated by SDCC by running the code through simulator
22710 There is a separate document
22713 \begin_inset LatexCommand \index{Test suite}
22722 You'll find the test code in the directory
22724 sdcc/support/regression
22727 You can run these tests manually by running
22731 in this directory (or f.e.
22736 \begin_inset Quotes sld
22740 \begin_inset Quotes srd
22746 if you don't want to run the complete tests).
22747 The test code might also be interesting if you want to look for examples
22748 \begin_inset LatexCommand \index{Examples}
22752 checking corner cases of SDCC or if you plan to submit patches
22753 \begin_inset LatexCommand \index{Patch submission}
22760 The pic port uses a different set of regression tests, you'll find them
22763 sdcc/src/regression
22768 SDCC Technical Data
22772 \begin_inset LatexCommand \index{Optimizations}
22779 SDCC performs a host of standard optimizations in addition to some MCU specific
22784 Sub-expression Elimination
22785 \begin_inset LatexCommand \index{Subexpression elimination}
22792 The compiler does local and
22818 will be translated to
22830 Some subexpressions are not as obvious as the above example, e.g.:
22840 In this case the address arithmetic a->b[i] will be computed only once;
22841 the equivalent code in C would be.
22853 The compiler will try to keep these temporary variables in registers.
22856 Dead-Code Elimination
22857 \begin_inset LatexCommand \index{Dead-code elimination}
22878 i = 1; \SpecialChar ~
22887 global = 1;\SpecialChar ~
22900 global = 3;\SpecialChar ~
22925 \begin_inset LatexCommand \index{Copy propagation}
22981 Note: the dead stores created by this copy propagation will be eliminated
22982 by dead-code elimination.
22986 \begin_inset LatexCommand \index{Loop optimization}
22991 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
22998 Two types of loop optimizations are done by SDCC
23006 of loop induction variables.
23007 In addition to the strength reduction the optimizer marks the induction
23008 variables and the register allocator tries to keep the induction variables
23009 in registers for the duration of the loop.
23010 Because of this preference of the register allocator
23011 \begin_inset LatexCommand \index{Register allocation}
23015 , loop induction optimization causes an increase in register pressure, which
23016 may cause unwanted spilling of other temporary variables into the stack
23017 \begin_inset LatexCommand \index{stack}
23022 The compiler will generate a warning message when it is forced to allocate
23023 extra space either on the stack or data space.
23024 If this extra space allocation is undesirable then induction optimization
23025 can be eliminated either for the entire source file (with -
23035 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23037 \begin_inset LatexCommand \index{\#pragma noinduction}
23050 for (i = 0 ; i < 100 ; i ++)
23066 for (i = 0; i < 100; i++)
23075 As mentioned previously some loop invariants are not as apparent, all static
23076 address computations are also moved out of the loop.
23081 \begin_inset LatexCommand \index{Strength reduction}
23085 , this optimization substitutes an expression by a cheaper expression:
23090 for (i=0;i < 100; i++)
23108 for (i=0;i< 100;i++) {
23114 ar[itemp1] = itemp2;
23131 The more expensive multiplication
23132 \begin_inset LatexCommand \index{Multiplication}
23136 is changed to a less expensive addition.
23140 \begin_inset LatexCommand \index{Loop reversing}
23147 This optimization is done to reduce the overhead of checking loop boundaries
23148 for every iteration.
23149 Some simple loops can be reversed and implemented using a
23150 \begin_inset Quotes eld
23153 decrement and jump if not zero
23154 \begin_inset Quotes erd
23158 SDCC checks for the following criterion to determine if a loop is reversible
23159 (note: more sophisticated compilers use data-dependency analysis to make
23160 this determination, SDCC uses a more simple minded analysis).
23163 The 'for' loop is of the form
23169 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23179 The <for body> does not contain
23180 \begin_inset Quotes eld
23184 \begin_inset Quotes erd
23188 \begin_inset Quotes erd
23194 All goto's are contained within the loop.
23197 No function calls within the loop.
23200 The loop control variable <sym> is not assigned any value within the loop
23203 The loop control variable does NOT participate in any arithmetic operation
23207 There are NO switch statements in the loop.
23210 Algebraic Simplifications
23213 SDCC does numerous algebraic simplifications, the following is a small sub-set
23214 of these optimizations.
23219 i = j + 0;\SpecialChar ~
23223 /* changed to: */\SpecialChar ~
23229 i /= 2;\SpecialChar ~
23236 /* changed to: */\SpecialChar ~
23242 i = j - j;\SpecialChar ~
23246 /* changed to: */\SpecialChar ~
23252 i = j / 1;\SpecialChar ~
23256 /* changed to: */\SpecialChar ~
23263 Note the subexpressions
23264 \begin_inset LatexCommand \index{Subexpression}
23268 given above are generally introduced by macro expansions or as a result
23269 of copy/constant propagation.
23272 'switch' Statements
23273 \begin_inset LatexCommand \label{sub:'switch'-Statements}
23278 \begin_inset LatexCommand \index{switch statement}
23285 SDCC can optimize switch statements to jump tables
23286 \begin_inset LatexCommand \index{jump tables}
23291 It makes the decision based on an estimate of the generated code size.
23292 SDCC is quite liberal in the requirements for jump table generation:
23295 The labels need not be in order, and the starting number need not be one
23296 or zero, the case labels are in numerical sequence or not too many case
23297 labels are missing.
23303 switch(i) {\SpecialChar ~
23334 case 4: ...\SpecialChar ~
23366 case 5: ...\SpecialChar ~
23398 case 3: ...\SpecialChar ~
23429 case 6: ...\SpecialChar ~
23461 case 7: ...\SpecialChar ~
23493 case 8: ...\SpecialChar ~
23525 case 9: ...\SpecialChar ~
23557 case 10: ...\SpecialChar ~
23588 case 11: ...\SpecialChar ~
23655 Both the above switch statements will be implemented using a jump-table.
23656 The example to the right side is slightly more efficient as the check for
23657 the lower boundary of the jump-table is not needed.
23661 The number of case labels is not larger than supported by the target architectur
23665 If the case labels are not in numerical sequence ('gaps' between cases)
23666 SDCC checks whether a jump table with additionally inserted dummy cases
23667 is still attractive.
23671 If the starting number is not zero and a check for the lower boundary of
23672 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
23677 Switch statements which have large gaps in the numeric sequence or those
23678 that have too many case labels can be split into more than one switch statement
23679 for efficient code generation, e.g.:
23759 If the above switch statement is broken down into two switch statements
23849 then both the switch statements will be implemented using jump-tables whereas
23850 the unmodified switch statement will not be.
23853 There might be reasons which SDCC cannot know about to either favour or
23854 not favour jump tables.
23855 If the target system has to be as quick for the last switch case as for
23856 the first (pro jump table), or if the switch argument is known to be zero
23857 in the majority of the cases (contra jump table).
23860 The pragma nojtbound
23861 \begin_inset LatexCommand \index{\#pragma nojtbound}
23865 can be used to turn off checking the
23878 It has no effect if a default label is supplied.
23879 Use of this pragma is dangerous: if the switch
23880 \begin_inset LatexCommand \index{switch statement}
23884 argument is not matched by a case statement the processor will happily
23888 Bit-shifting Operations
23889 \begin_inset LatexCommand \index{Bit shifting}
23896 Bit shifting is one of the most frequently used operation in embedded programmin
23898 SDCC tries to implement bit-shift operations in the most efficient way
23914 generates the following code:
23931 In general SDCC will never setup a loop if the shift count is known.
23974 \begin_inset LatexCommand \index{Bit rotation}
23981 A special case of the bit-shift operation is bit rotation
23982 \begin_inset LatexCommand \index{rotating bits}
23986 , SDCC recognizes the following expression to be a left bit-rotation:
23996 char i;\SpecialChar ~
24007 /* unsigned is needed for rotation */
24012 i = ((i << 1) | (i >> 7));
24021 will generate the following code:
24040 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24041 ns of this case will also be recognized as bit-rotation, i.e.:
24046 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24049 Nibble and Byte Swapping
24052 Other special cases of the bit-shift operations are nibble or byte swapping
24053 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24057 , SDCC recognizes the following expressions:
24080 i = ((i << 4) | (i >> 4));
24086 j = ((j << 8) | (j >> 8));
24089 and generates a swap instruction for the nibble swapping
24090 \begin_inset LatexCommand \index{Nibble swapping}
24094 or move instructions for the byte swapping
24095 \begin_inset LatexCommand \index{Byte swapping}
24101 \begin_inset Quotes sld
24105 \begin_inset Quotes srd
24108 example can be used to convert from little to big-endian or vice versa.
24109 If you want to change the endianness of a
24113 integer you have to cast to
24120 Note that SDCC stores numbers in little-endian
24126 Usually 8-bit processors don't care much about endianness.
24127 This is not the case for the standard 8051 which only has an instruction
24133 \begin_inset LatexCommand \index{DPTR}
24141 so little-endian is the more efficient byte order.
24145 \begin_inset LatexCommand \index{little-endian}
24150 \begin_inset LatexCommand \index{Endianness}
24155 lowest order first).
24159 \begin_inset LatexCommand \index{Highest Order Bit}
24166 It is frequently required to obtain the highest order bit of an integral
24167 type (long, int, short or char types).
24168 SDCC recognizes the following expression to yield the highest order bit
24169 and generates optimized code for it, e.g.:
24191 hob = (gint >> 15) & 1;
24201 will generate the following code:
24234 000A E5*01\SpecialChar ~
24261 000C 23\SpecialChar ~
24292 000D 54 01\SpecialChar ~
24319 000F F5*02\SpecialChar ~
24347 Variations of this case however will
24352 It is a standard C expression, so I heartily recommend this be the only
24353 way to get the highest order bit, (it is portable).
24354 Of course it will be recognized even if it is embedded in other expressions,
24360 xyz = gint + ((gint >> 15) & 1);
24363 will still be recognized.
24367 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
24372 \begin_inset LatexCommand \index{Peephole optimizer}
24379 The compiler uses a rule based, pattern matching and re-writing mechanism
24380 for peep-hole optimization.
24385 a peep-hole optimizer by Christopher W.
24386 Fraser (cwfraser\SpecialChar ~
24389 A default set of rules are compiled into the compiler, additional rules
24390 may be added with the
24403 \begin_inset LatexCommand \index{-\/-peep-file}
24410 The rule language is best illustrated with examples.
24434 The above rule will change the following assembly
24435 \begin_inset LatexCommand \index{Assembler routines}
24457 Note: All occurrences of a
24461 (pattern variable) must denote the same string.
24462 With the above rule, the assembly sequence:
24472 will remain unmodified.
24476 Other special case optimizations may be added by the user (via
24492 some variants of the 8051 MCU
24493 \begin_inset LatexCommand \index{MCS51 variants}
24506 The following two rules will change all
24525 replace { lcall %1 } by { acall %1 }
24527 replace { ljmp %1 } by { ajmp %1 }
24532 inline-assembler code
24534 is also passed through the peep hole optimizer, thus the peephole optimizer
24535 can also be used as an assembly level macro expander.
24536 The rules themselves are MCU dependent whereas the rule language infra-structur
24537 e is MCU independent.
24538 Peephole optimization rules for other MCU can be easily programmed using
24543 The syntax for a rule is as follows:
24548 rule := replace [ restart ] '{' <assembly sequence> '
24586 <assembly sequence> '
24604 '}' [if <functionName> ] '
24609 <assembly sequence> := assembly instruction (each instruction including
24610 labels must be on a separate line).
24614 The optimizer will apply to the rules one by one from the top in the sequence
24615 of their appearance, it will terminate when all rules are exhausted.
24616 If the 'restart' option is specified, then the optimizer will start matching
24617 the rules again from the top, this option for a rule is expensive (performance)
24618 , it is intended to be used in situations where a transformation will trigger
24619 the same rule again.
24620 An example of this (not a good one, it has side effects) is the following
24643 Note that the replace pattern cannot be a blank, but can be a comment line.
24644 Without the 'restart' option only the innermost 'pop' 'push' pair would
24645 be eliminated, i.e.:
24675 the restart option the rule will be applied again to the resulting code
24676 and then all the pop-push pairs will be eliminated to yield:
24686 A conditional function can be attached to a rule.
24687 Attaching rules are somewhat more involved, let me illustrate this with
24714 The optimizer does a look-up of a function name table defined in function
24719 in the source file SDCCpeeph.c, with the name
24724 If it finds a corresponding entry the function is called.
24725 Note there can be no parameters specified for these functions, in this
24730 is crucial, since the function
24734 expects to find the label in that particular variable (the hash table containin
24735 g the variable bindings is passed as a parameter).
24736 If you want to code more such functions, take a close look at the function
24737 labelInRange and the calling mechanism in source file SDCCpeeph.c.
24738 Currently implemented are
24740 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
24741 24bitMode, portIsDS390, 24bitModeAndPortDS390
24750 I know this whole thing is a little kludgey, but maybe some day we will
24751 have some better means.
24752 If you are looking at this file, you will see the default rules that are
24753 compiled into the compiler, you can add your own rules in the default set
24754 there if you get tired of specifying the -
24768 \begin_inset LatexCommand \index{ANSI-compliance}
24773 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
24780 Deviations from the compliance:
24783 functions are not reentrant
24784 \begin_inset LatexCommand \index{reentrant}
24788 unless explicitly declared as such or the
24801 \begin_inset LatexCommand \index{-\/-stack-auto}
24807 command line option is specified.
24810 structures and unions cannot be assigned values directly, cannot be passed
24811 as function parameters or assigned to each other and cannot be a return
24812 value from a function, e.g.:
24838 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
24849 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
24871 return rets;/* is invalid in SDCC although allowed in ANSI */
24878 \begin_inset LatexCommand \index{long long (not supported)}
24883 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
24891 \begin_inset LatexCommand \index{double (not supported)}
24895 ' precision floating point
24896 \begin_inset LatexCommand \index{Floating point support}
24903 No support for setjmp
24904 \begin_inset LatexCommand \index{setjmp (not supported)}
24909 \begin_inset LatexCommand \index{longjmp (not supported)}
24917 \begin_inset LatexCommand \index{K\&R style}
24921 function declarations are NOT allowed.
24927 foo(i,j) /* this old style of function declarations */
24929 int i,j; /* are valid in ANSI but not valid in SDCC */
24944 Certain words that are valid identifiers in the standard may be reserved
24945 words in SDCC unless the
24958 \begin_inset LatexCommand \index{-\/-std-c89}
24973 \begin_inset LatexCommand \index{-\/-std-c99}
24979 command line options are used.
24980 These may include (depending on the selected processor): 'at', 'banked',
24981 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
24982 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
24983 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
24985 Compliant equivalents of these keywords are always available in a form
24986 that begin with two underscores
24987 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
24992 '__data' instead of 'data'.
24995 Cyclomatic Complexity
24996 \begin_inset LatexCommand \index{Cyclomatic complexity}
25003 Cyclomatic complexity of a function is defined as the number of independent
25004 paths the program can take during execution of the function.
25005 This is an important number since it defines the number test cases you
25006 have to generate to validate the function.
25007 The accepted industry standard for complexity number is 10, if the cyclomatic
25008 complexity reported by SDCC exceeds 10 you should think about simplification
25009 of the function logic.
25010 Note that the complexity level is not related to the number of lines of
25011 code in a function.
25012 Large functions can have low complexity, and small functions can have large
25018 SDCC uses the following formula to compute the complexity:
25023 complexity = (number of edges in control flow graph) - (number of nodes
25024 in control flow graph) + 2;
25028 Having said that the industry standard is 10, you should be aware that in
25029 some cases it be may unavoidable to have a complexity level of less than
25031 For example if you have switch statement with more than 10 case labels,
25032 each case label adds one to the complexity level.
25033 The complexity level is by no means an absolute measure of the algorithmic
25034 complexity of the function, it does however provide a good starting point
25035 for which functions you might look at for further optimization.
25038 Retargetting for other Processors
25041 The issues for retargetting the compiler are far too numerous to be covered
25043 What follows is a brief description of each of the seven phases of the
25044 compiler and its MCU dependency.
25047 Parsing the source and building the annotated parse tree.
25048 This phase is largely MCU independent (except for the language extensions).
25049 Syntax & semantic checks are also done in this phase, along with some initial
25050 optimizations like back patching labels and the pattern matching optimizations
25051 like bit-rotation etc.
25054 The second phase involves generating an intermediate code which can be easy
25055 manipulated during the later phases.
25056 This phase is entirely MCU independent.
25057 The intermediate code generation assumes the target machine has unlimited
25058 number of registers, and designates them with the name iTemp.
25059 The compiler can be made to dump a human readable form of the code generated
25073 This phase does the bulk of the standard optimizations and is also MCU independe
25075 This phase can be broken down into several sub-phases:
25079 Break down intermediate code (iCode) into basic blocks.
25081 Do control flow & data flow analysis on the basic blocks.
25083 Do local common subexpression elimination, then global subexpression elimination
25085 Dead code elimination
25089 If loop optimizations caused any changes then do 'global subexpression eliminati
25090 on' and 'dead code elimination' again.
25093 This phase determines the live-ranges; by live range I mean those iTemp
25094 variables defined by the compiler that still survive after all the optimization
25096 Live range analysis
25097 \begin_inset LatexCommand \index{Live range analysis}
25101 is essential for register allocation, since these computation determines
25102 which of these iTemps will be assigned to registers, and for how long.
25105 Phase five is register allocation.
25106 There are two parts to this process.
25110 The first part I call 'register packing' (for lack of a better term).
25111 In this case several MCU specific expression folding is done to reduce
25116 The second part is more MCU independent and deals with allocating registers
25117 to the remaining live ranges.
25118 A lot of MCU specific code does creep into this phase because of the limited
25119 number of index registers available in the 8051.
25122 The Code generation phase is (unhappily), entirely MCU dependent and very
25123 little (if any at all) of this code can be reused for other MCU.
25124 However the scheme for allocating a homogenized assembler operand for each
25125 iCode operand may be reused.
25128 As mentioned in the optimization section the peep-hole optimizer is rule
25129 based system, which can reprogrammed for other MCUs.
25133 \begin_inset LatexCommand \index{Compiler internals}
25140 The anatomy of the compiler
25141 \begin_inset LatexCommand \label{sub:The-anatomy-of}
25150 This is an excerpt from an article published in Circuit Cellar Magazine
25156 It's a little outdated (the compiler is much more efficient now and user/develo
25157 per friendly), but pretty well exposes the guts of it all.
25163 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
25164 It is fairly easy to retarget for other 8-bit MCU.
25165 Here we take a look at some of the internals of the compiler.
25170 \begin_inset LatexCommand \index{Parsing}
25177 Parsing the input source file and creating an AST (Annotated Syntax Tree
25178 \begin_inset LatexCommand \index{Annotated syntax tree}
25183 This phase also involves propagating types (annotating each node of the
25184 parse tree with type information) and semantic analysis.
25185 There are some MCU specific parsing rules.
25186 For example the storage classes, the extended storage classes are MCU specific
25187 while there may be a xdata storage class for 8051 there is no such storage
25188 class for z80 or Atmel AVR.
25189 SDCC allows MCU specific storage class extensions, i.e.
25190 xdata will be treated as a storage class specifier when parsing 8051 C
25191 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
25196 \begin_inset LatexCommand \index{iCode}
25203 Intermediate code generation.
25204 In this phase the AST is broken down into three-operand form (iCode).
25205 These three operand forms are represented as doubly linked lists.
25206 ICode is the term given to the intermediate form generated by the compiler.
25207 ICode example section shows some examples of iCode generated for some simple
25208 C source functions.
25212 \begin_inset LatexCommand \index{Optimizations}
25219 Bulk of the target independent optimizations is performed in this phase.
25220 The optimizations include constant propagation, common sub-expression eliminati
25221 on, loop invariant code movement, strength reduction of loop induction variables
25222 and dead-code elimination.
25225 Live range analysis
25226 \begin_inset LatexCommand \index{Live range analysis}
25233 During intermediate code generation phase, the compiler assumes the target
25234 machine has infinite number of registers and generates a lot of temporary
25236 The live range computation determines the lifetime of each of these compiler-ge
25237 nerated temporaries.
25238 A picture speaks a thousand words.
25239 ICode example sections show the live range annotations for each of the
25241 It is important to note here, each iCode is assigned a number in the order
25242 of its execution in the function.
25243 The live ranges are computed in terms of these numbers.
25244 The from number is the number of the iCode which first defines the operand
25245 and the to number signifies the iCode which uses this operand last.
25248 Register Allocation
25249 \begin_inset LatexCommand \index{Register allocation}
25256 The register allocation determines the type and number of registers needed
25258 In most MCUs only a few registers can be used for indirect addressing.
25259 In case of 8051 for example the registers R0 & R1 can be used to indirectly
25260 address the internal ram and DPTR to indirectly address the external ram.
25261 The compiler will try to allocate the appropriate register to pointer variables
25263 ICode example section shows the operands annotated with the registers assigned
25265 The compiler will try to keep operands in registers as much as possible;
25266 there are several schemes the compiler uses to do achieve this.
25267 When the compiler runs out of registers the compiler will check to see
25268 if there are any live operands which is not used or defined in the current
25269 basic block being processed, if there are any found then it will push that
25270 operand and use the registers in this block, the operand will then be popped
25271 at the end of the basic block.
25275 There are other MCU specific considerations in this phase.
25276 Some MCUs have an accumulator; very short-lived operands could be assigned
25277 to the accumulator instead of a general-purpose register.
25283 Figure II gives a table of iCode operations supported by the compiler.
25284 The code generation involves translating these operations into corresponding
25285 assembly code for the processor.
25286 This sounds overly simple but that is the essence of code generation.
25287 Some of the iCode operations are generated on a MCU specific manner for
25288 example, the z80 port does not use registers to pass parameters so the
25289 SEND and RECV iCode operations will not be generated, and it also does
25290 not support JUMPTABLES.
25297 <Where is Figure II?>
25300 In the original article Figure II was announced to be downloadable on
25305 Unfortunately it never seemed to have shown up there, so: where is Figure
25310 \begin_inset LatexCommand \index{iCode}
25317 This section shows some details of iCode.
25318 The example C code does not do anything useful; it is used as an example
25319 to illustrate the intermediate code generated by the compiler.
25331 /* This function does nothing useful.
25338 for the purpose of explaining iCode */
25341 short function (data int *x)
25349 short i=10; \SpecialChar ~
25351 /* dead initialization eliminated */
25356 short sum=10; /* dead initialization eliminated */
25369 while (*x) *x++ = *p++;
25383 /* compiler detects i,j to be induction variables */
25387 for (i = 0, j = 10 ; i < 10 ; i++, j
25413 mul += i * 3; \SpecialChar ~
25415 /* this multiplication remains */
25421 gint += j * 3;\SpecialChar ~
25423 /* this multiplication changed to addition */
25437 In addition to the operands each iCode contains information about the filename
25438 and line it corresponds to in the source file.
25439 The first field in the listing should be interpreted as follows:
25444 Filename(linenumber: iCode Execution sequence number : ICode hash table
25445 key : loop depth of the iCode).
25450 Then follows the human readable form of the ICode operation.
25451 Each operand of this triplet form can be of three basic types a) compiler
25452 generated temporary b) user defined variable c) a constant value.
25453 Note that local variables and parameters are replaced by compiler generated
25456 \begin_inset LatexCommand \index{Live range analysis}
25460 are computed only for temporaries (i.e.
25461 live ranges are not computed for global variables).
25463 \begin_inset LatexCommand \index{Register allocation}
25467 are allocated for temporaries only.
25468 Operands are formatted in the following manner:
25473 Operand Name [lr live-from : live-to ] { type information } [ registers
25479 As mentioned earlier the live ranges are computed in terms of the execution
25480 sequence number of the iCodes, for example
25482 the iTemp0 is live from (i.e.
25483 first defined in iCode with execution sequence number 3, and is last used
25484 in the iCode with sequence number 5).
25485 For induction variables such as iTemp21 the live range computation extends
25486 the lifetime from the start to the end of the loop.
25488 The register allocator used the live range information to allocate registers,
25489 the same registers may be used for different temporaries if their live
25490 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
25491 iTemp17 since their live ranges do not overlap.
25492 In addition the allocator also takes into consideration the type and usage
25493 of a temporary, for example itemp6 is a pointer to near space and is used
25494 as to fetch data from (i.e.
25495 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
25496 Some short lived temporaries are allocated to special registers which have
25497 meaning to the code generator e.g.
25498 iTemp13 is allocated to a pseudo register CC which tells the back end that
25499 the temporary is used only for a conditional jump the code generation makes
25500 use of this information to optimize a compare and jump ICode.
25502 There are several loop optimizations
25503 \begin_inset LatexCommand \index{Loop optimization}
25507 performed by the compiler.
25508 It can detect induction variables iTemp21(i) and iTemp23(j).
25509 Also note the compiler does selective strength reduction
25510 \begin_inset LatexCommand \index{Strength reduction}
25515 the multiplication of an induction variable in line 18 (gint = j * 3) is
25516 changed to addition, a new temporary iTemp17 is allocated and assigned
25517 a initial value, a constant 3 is then added for each iteration of the loop.
25518 The compiler does not change the multiplication
25519 \begin_inset LatexCommand \index{Multiplication}
25523 in line 17 however since the processor does support an 8 * 8 bit multiplication.
25525 Note the dead code elimination
25526 \begin_inset LatexCommand \index{Dead-code elimination}
25530 optimization eliminated the dead assignments in line 7 & 8 to I and sum
25538 Sample.c (5:1:0:0) _entry($9) :
25543 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
25548 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
25553 Sample.c(11:4:53:0) preHeaderLbl0($11) :
25558 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
25564 Sample.c(11:6:5:1) _whilecontinue_0($1) :
25569 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
25575 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25580 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
25586 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
25592 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
25598 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
25604 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
25605 * int}[r0] + 0x2 {short}
25610 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
25615 Sample.c(11:17:21:0)_whilebreak_0($3) :
25620 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
25625 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
25630 Sample.c(15:20:54:0)preHeaderLbl1($13) :
25635 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
25640 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
25645 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
25650 Sample.c(15:24:26:1)_forcond_0($4) :
25655 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
25661 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
25666 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
25667 + ITemp21 [lr21:38]{short}[r4]
25672 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
25678 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
25679 + iTemp15 [lr29:30]{short}[r1]
25684 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
25690 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
25696 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
25702 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
25708 Sample.c(19:38:47:1) goto _forcond_0($4)
25713 Sample.c(19:39:48:0)_forbreak_0($7) :
25718 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
25719 + ITemp11 [lr19:40]{short}[r3]
25724 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
25729 Sample.c(20:42:51:0)_return($8) :
25734 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
25740 Finally the code generated for this function:
25781 ; ----------------------------------------------
25786 ; function function
25791 ; ----------------------------------------------
25801 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
25813 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
25825 ;_whilecontinue_0($1) :
25835 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
25840 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
25899 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
25918 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
25965 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
26005 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
26031 ; iTemp6 [lr5:16]{_near * int}[r0] =
26036 ; iTemp6 [lr5:16]{_near * int}[r0] +
26053 ; goto _whilecontinue_0($1)
26065 ; _whilebreak_0($3) :
26075 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
26087 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
26099 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
26111 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
26130 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
26159 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
26164 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
26209 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
26214 ; iTemp21 [lr21:38]{short}[r4]
26240 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
26273 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
26278 ; iTemp15 [lr29:30]{short}[r1]
26297 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
26344 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
26391 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
26403 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
26417 cjne r5,#0xff,00104$
26429 ; goto _forcond_0($4)
26441 ; _forbreak_0($7) :
26451 ; ret iTemp24 [lr40:41]{short}
26494 A few words about basic block successors, predecessors and dominators
26497 Successors are basic blocks
26498 \begin_inset LatexCommand \index{Basic blocks}
26502 that might execute after this basic block.
26504 Predecessors are basic blocks that might execute before reaching this basic
26507 Dominators are basic blocks that WILL execute before reaching this basic
26541 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
26544 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
26547 c) domVect of [BB4] = BB1 ...
26548 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
26556 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
26566 Thanks to all the other volunteer developers who have helped with coding,
26567 testing, web-page creation, distribution sets, etc.
26568 You know who you are :-)
26575 This document was initially written by Sandeep Dutta
26578 All product names mentioned herein may be trademarks
26579 \begin_inset LatexCommand \index{Trademarks}
26583 of their respective companies.
26590 To avoid confusion, the installation and building options for SDCC itself
26591 (chapter 2) are not part of the index.
26595 \begin_inset LatexCommand \printindex{}