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={68hc08 8032 8051 ansi c compiler CPU DS390 embedded free Freescale GPL HC08 Intel manual Maxim mcs51 Microchip microcontroller open source PIC Z80 Zilog},
11 pdfpagemode=UseOutlines,
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 Subversion
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 retargeted for other microprocessors, support for Microchip PIC,
146 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 retargetable 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:
209 <lyxtabular version="3" rows="8" columns="5">
211 <column alignment="center" valignment="top" leftline="true" width="0">
212 <column alignment="center" valignment="top" leftline="true" width="0">
213 <column alignment="center" valignment="top" leftline="true" width="0">
214 <column alignment="center" valignment="top" leftline="true" width="0">
215 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
216 <row topline="true" bottomline="true">
217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
426 <row topline="true" bottomline="true">
427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
456 -2.147.483.648, +2.147.483.647
459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
468 <row topline="true" bottomline="true">
469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
508 <row topline="true" bottomline="true">
509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
554 The compiler also allows
556 inline assembler code
558 to be embedded anywhere in a function.
559 In addition, routines developed in assembly can also be called.
563 SDCC also provides an option (-
573 -cyclomatic) to report the relative complexity of a function.
574 These functions can then be further optimized, or hand coded in assembly
580 SDCC also comes with a companion source level debugger SDCDB, the debugger
581 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
582 SDCDB and ucSim are currently not available on Win32 platforms.
587 The latest version can be downloaded from
588 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
598 Please note: the compiler will probably always be some steps ahead of this
603 \begin_inset LatexCommand \index{Status of documentation}
613 Obviously this has pros and cons
622 All packages used in this compiler system are
630 ; source code for all the sub-packages (pre-processor, assemblers, linkers
631 etc) is distributed with the package.
632 This documentation is maintained using a freeware word processor (LyX).
634 This program is free software; you can redistribute it and/or modify it
635 under the terms of the GNU General Public License
636 \begin_inset LatexCommand \index{GNU General Public License, GPL}
640 as published by the Free Software Foundation; either version 2, or (at
641 your option) any later version.
642 This program is distributed in the hope that it will be useful, but WITHOUT
643 ANY WARRANTY; without even the implied warranty
644 \begin_inset LatexCommand \index{warranty}
648 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
649 See the GNU General Public License for more details.
650 You should have received a copy of the GNU General Public License along
651 with this program; if not, write to the Free Software Foundation, 59 Temple
652 Place - Suite 330, Boston, MA 02111-1307, USA.
653 In other words, you are welcome to use, share and improve this program.
654 You are forbidden to forbid anyone else to use, share and improve what
656 Help stamp out software-hoarding!
659 Typographic conventions
660 \begin_inset LatexCommand \index{Typographic conventions}
667 Throughout this manual, we will use the following convention.
668 Commands you have to type in are printed in
676 Code samples are printed in
681 Interesting items and new terms are printed in
687 \begin_inset LatexCommand \label{sec:Compatibility-with-previous}
691 with previous versions
692 \begin_inset LatexCommand \index{Compatibility with previous versions}
699 This version has numerous bug fixes compared with the previous version.
700 But we also introduced some incompatibilities with older versions.
701 Not just for the fun of it, but to make the compiler more stable, efficient
703 \begin_inset LatexCommand \index{ANSI-compliance}
708 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
712 for ANSI-Compliance).
718 short is now equivalent to int (16 bits), it used to be equivalent to char
719 (8 bits) which is not ANSI compliant.
722 the default directory for gcc-builds where include, library and documentation
723 files are stored is now in /usr/local/share.
726 char type parameters to vararg functions are casted to int unless explicitly
743 will push a as an int and as a char resp.
756 -regextend has been removed.
769 -noregparms has been removed.
782 -stack-after-data has been removed.
786 \begin_inset LatexCommand \index{bit}
791 \begin_inset LatexCommand \index{sbit}
796 \begin_inset LatexCommand \index{\_\_sbit}
800 types now consistently behave like the C99 _Bool type with respect to type
802 \begin_inset LatexCommand \index{type conversion}
807 \begin_inset LatexCommand \index{type promotion}
812 The most common incompatibility resulting from this change is related to
814 \begin_inset LatexCommand \index{Bit toggling}
828 b = ~b; /* equivalent to b=1 instead of toggling b */
829 \begin_inset Marginal
844 b = !b; /* toggles b */
848 In previous versions, both forms would have toggled the bit.
853 <pending: more incompatibilities?>
859 What do you need before you start installation of SDCC? A computer, and
861 The preferred method of installation is to compile SDCC from source using
863 For Windows some pre-compiled binary distributions are available for your
865 You should have some experience with command line tools and compiler use.
871 The SDCC home page at
872 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
876 is a great place to find distribution sets.
877 You can also find links to the user mailing lists that offer help or discuss
878 SDCC with other SDCC users.
879 Web links to other SDCC related sites can also be found here.
880 This document can be found in the DOC directory of the source package as
882 A pdf version of this document is available at
883 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
888 Some of the other tools (simulator and assembler) included with SDCC contain
889 their own documentation and can be found in the source distribution.
890 If you want the latest unreleased software, the complete source package
891 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
895 Wishes for the future
898 There are (and always will be) some things that could be done.
899 Here are some I can think of:
906 char KernelFunction3(char p) at 0x340;
914 \begin_inset LatexCommand \index{code banking (limited support)}
924 If you can think of some more, please see the section
925 \begin_inset LatexCommand \ref{sub:Requesting-Features}
929 about filing feature requests
930 \begin_inset LatexCommand \index{Requesting features}
935 \begin_inset LatexCommand \index{Feature request}
945 \begin_inset LatexCommand \index{Installation}
952 For most users it is sufficient to skip to either section
953 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
958 \begin_inset LatexCommand \ref{sub:Windows-Install}
963 More detailed instructions follow below.
967 \begin_inset LatexCommand \index{Options SDCC configuration}
974 The install paths, search paths and other options are defined when running
976 The defaults can be overridden by:
978 \labelwidthstring 00.00.0000
990 -prefix see table below
992 \labelwidthstring 00.00.0000
1004 -exec_prefix see table below
1006 \labelwidthstring 00.00.0000
1018 -bindir see table below
1020 \labelwidthstring 00.00.0000
1032 -datadir see table below
1034 \labelwidthstring 00.00.0000
1036 docdir environment variable, see table below
1038 \labelwidthstring 00.00.0000
1040 include_dir_suffix environment variable, see table below
1042 \labelwidthstring 00.00.0000
1044 lib_dir_suffix environment variable, see table below
1046 \labelwidthstring 00.00.0000
1048 sdccconf_h_dir_separator environment variable, either / or
1053 This character will only be used in sdccconf.h; don't forget it's a C-header,
1054 therefore a double-backslash is needed there.
1056 \labelwidthstring 00.00.0000
1068 -disable-mcs51-port Excludes the Intel mcs51 port
1070 \labelwidthstring 00.00.0000
1082 -disable-gbz80-port Excludes the Gameboy gbz80 port
1084 \labelwidthstring 00.00.0000
1096 -disable-z80-port Excludes the z80 port
1098 \labelwidthstring 00.00.0000
1110 -disable-avr-port Excludes the AVR port
1112 \labelwidthstring 00.00.0000
1124 -disable-ds390-port Excludes the DS390 port
1126 \labelwidthstring 00.00.0000
1138 -disable-hc08-port Excludes the HC08 port
1140 \labelwidthstring 00.00.0000
1152 -disable-pic-port Excludes the PIC port
1154 \labelwidthstring 00.00.0000
1166 -disable-xa51-port Excludes the XA51 port
1168 \labelwidthstring 00.00.0000
1180 -disable-ucsim Disables configuring and building of ucsim
1182 \labelwidthstring 00.00.0000
1194 -disable-device-lib Disables automatically building device libraries
1196 \labelwidthstring 00.00.0000
1208 -disable-packihx Disables building packihx
1210 \labelwidthstring 00.00.0000
1222 -enable-doc Build pdf, html and txt files from the lyx sources
1224 \labelwidthstring 00.00.0000
1236 -enable-libgc Use the Bohem memory allocator.
1237 Lower runtime footprint.
1240 Furthermore the environment variables CC, CFLAGS, ...
1241 the tools and their arguments can be influenced.
1242 Please see `configure -
1252 -help` and the man/info pages of `configure` for details.
1256 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1257 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1258 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1259 At the moment it's not possible to change the default settings (it was
1260 simply never required).
1264 These configure options are compiled into the binaries, and can only be
1265 changed by rerunning 'configure' and recompiling SDCC.
1266 The configure options are written in
1270 to distinguish them from run time environment variables (see section search
1276 \begin_inset Quotes sld
1280 \begin_inset Quotes srd
1283 are used by the SDCC team to build the official Win32 binaries.
1284 The SDCC team uses Mingw32 to build the official Windows binaries, because
1291 a gcc compiler and last but not least
1294 the binaries can be built by cross compiling on Sourceforge's compile farm.
1297 See the examples, how to pass the Win32 settings to 'configure'.
1298 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1299 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1310 \begin_inset Tabular
1311 <lyxtabular version="3" rows="8" columns="3">
1313 <column alignment="block" valignment="top" leftline="true" width="0in">
1314 <column alignment="block" valignment="top" leftline="true" width="0in">
1315 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1316 <row topline="true" bottomline="true">
1317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1342 <row topline="true">
1343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1353 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1361 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1372 <row topline="true">
1373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1393 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1404 <row topline="true">
1405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1427 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1442 <row topline="true">
1443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1465 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1476 <row topline="true">
1477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1514 <row topline="true">
1515 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1533 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1542 <row topline="true" bottomline="true">
1543 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1579 'configure' also computes relative paths.
1580 This is needed for full relocatability of a binary package and to complete
1581 search paths (see section search paths below):
1587 \begin_inset Tabular
1588 <lyxtabular version="3" rows="4" columns="3">
1590 <column alignment="block" valignment="top" leftline="true" width="0in">
1591 <column alignment="block" valignment="top" leftline="true" width="0in">
1592 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1593 <row topline="true" bottomline="true">
1594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1602 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1619 <row topline="true" bottomline="true">
1620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1647 <row bottomline="true">
1648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1658 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1666 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1675 <row bottomline="true">
1676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1727 \begin_inset Quotes srd
1731 \begin_inset Quotes srd
1745 \begin_inset Quotes srd
1749 \begin_inset Quotes srd
1777 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1786 \begin_inset Quotes srd
1789 i586-mingw32msvc-gcc
1790 \begin_inset Quotes srd
1794 \begin_inset Quotes srd
1797 i586-mingw32msvc-g++
1798 \begin_inset Quotes srd
1806 \begin_inset Quotes srd
1809 i586-mingw32msvc-ranlib
1810 \begin_inset Quotes srd
1818 \begin_inset Quotes srd
1821 i586-mingw32msvc-strip
1822 \begin_inset Quotes srd
1840 \begin_inset Quotes srd
1844 \begin_inset Quotes srd
1862 \begin_inset Quotes srd
1866 \begin_inset Quotes srd
1874 \begin_inset Quotes srd
1878 \begin_inset Quotes srd
1886 \begin_inset Quotes srd
1890 \begin_inset Quotes srd
1898 \begin_inset Quotes srd
1902 \begin_inset Quotes srd
1909 sdccconf_h_dir_separator=
1910 \begin_inset Quotes srd
1922 \begin_inset Quotes srd
1967 -host=i586-mingw32msvc -
1977 -build=unknown-unknown-linux-gnu
1981 \begin_inset Quotes sld
1985 \begin_inset Quotes srd
1988 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1997 \begin_inset Quotes srd
2001 \begin_inset Quotes srd
2009 \begin_inset Quotes srd
2013 \begin_inset Quotes srd
2031 \begin_inset Quotes srd
2035 \begin_inset Quotes srd
2053 \begin_inset Quotes srd
2057 \begin_inset Quotes srd
2065 \begin_inset Quotes srd
2069 \begin_inset Quotes srd
2077 \begin_inset Quotes srd
2081 \begin_inset Quotes srd
2089 \begin_inset Quotes srd
2093 \begin_inset Quotes srd
2100 sdccconf_h_dir_separator=
2101 \begin_inset Quotes srd
2113 \begin_inset Quotes srd
2133 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2144 -C' turns on caching, which gives a little bit extra speed.
2145 However if options are changed, it can be necessary to delete the config.cache
2150 \begin_inset LatexCommand \label{sub:Install-paths}
2155 \begin_inset LatexCommand \index{Install paths}
2161 \added_space_top medskip \align center
2163 \begin_inset Tabular
2164 <lyxtabular version="3" rows="5" columns="4">
2166 <column alignment="left" valignment="top" leftline="true" width="0">
2167 <column alignment="left" valignment="top" leftline="true" width="0">
2168 <column alignment="left" valignment="top" leftline="true" width="0">
2169 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2170 <row topline="true" bottomline="true">
2171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2191 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2212 <row topline="true">
2213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2231 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2239 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2252 <row topline="true">
2253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2268 $DATADIR/ $INCLUDE_DIR_SUFFIX
2271 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2276 /usr/local/share/sdcc/include
2279 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2292 <row topline="true">
2293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2308 $DATADIR/$LIB_DIR_SUFFIX
2311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2316 /usr/local/share/sdcc/lib
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2332 <row topline="true" bottomline="true">
2333 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2341 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2356 /usr/local/share/sdcc/doc
2359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2381 *compiler, preprocessor, assembler, and linker
2387 is auto-appended by the compiler, e.g.
2388 small, large, z80, ds390 etc
2391 The install paths can still be changed during `make install` with e.g.:
2394 make install prefix=$(HOME)/local/sdcc
2397 Of course this doesn't change the search paths compiled into the binaries.
2401 Moreover the install path can be changed by defining DESTDIR
2402 \begin_inset LatexCommand \index{DESTDIR}
2409 make install DESTDIR=$(HOME)/sdcc.rpm/
2412 Please note that DESTDIR must have a trailing slash!
2416 \begin_inset LatexCommand \label{sub:Search-Paths}
2421 \begin_inset LatexCommand \index{Search path}
2428 Some search paths or parts of them are determined by configure variables
2433 , see section above).
2434 Further search paths are determined by environment variables during runtime.
2437 The paths searched when running the compiler are as follows (the first catch
2443 Binary files (preprocessor, assembler and linker)
2449 \begin_inset Tabular
2450 <lyxtabular version="3" rows="4" columns="3">
2452 <column alignment="block" valignment="top" leftline="true" width="0in">
2453 <column alignment="block" valignment="top" leftline="true" width="0in">
2454 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2455 <row topline="true" bottomline="true">
2456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2481 <row topline="true">
2482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2511 <row topline="true">
2512 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2517 Path of argv[0] (if available)
2520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2537 <row topline="true" bottomline="true">
2538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2579 \begin_inset Tabular
2580 <lyxtabular version="3" rows="6" columns="3">
2582 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2583 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2584 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2585 <row topline="true" bottomline="true">
2586 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2602 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2611 <row topline="true">
2612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2667 <row topline="true">
2668 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2693 <row topline="true">
2694 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2708 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2731 <row topline="true">
2732 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2750 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2813 <row topline="true" bottomline="true">
2814 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2830 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2835 /usr/local/share/sdcc/
2840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2868 -nostdinc disables the last two search paths.
2878 With the exception of
2879 \begin_inset Quotes sld
2893 \begin_inset Quotes srd
2900 is auto-appended by the compiler (e.g.
2901 small, large, z80, ds390 etc.).
2908 \begin_inset Tabular
2909 <lyxtabular version="3" rows="6" columns="3">
2911 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2912 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2913 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2914 <row topline="true" bottomline="true">
2915 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2923 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2931 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2940 <row topline="true">
2941 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2996 <row topline="true">
2997 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3009 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3021 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3036 <row topline="true">
3037 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3048 $LIB_DIR_SUFFIX/<model>
3051 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3065 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3082 <row topline="true">
3083 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3098 $LIB_DIR_SUFFIX/<model>
3101 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3210 <row topline="true" bottomline="true">
3211 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3220 $LIB_DIR_SUFFIX/<model>
3223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3228 /usr/local/share/sdcc/
3235 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3253 Don't delete any of the stray spaces in the table above without checking
3254 the HTML output (last line)!
3270 -nostdlib disables the last two search paths.
3274 \begin_inset LatexCommand \index{Building SDCC}
3281 Building SDCC on Linux
3282 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3291 Download the source package
3293 either from the SDCC Subversion repository or from the nightly snapshots
3295 , it will be named something like sdcc
3306 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3315 Bring up a command line terminal, such as xterm.
3320 Unpack the file using a command like:
3323 "tar -xvzf sdcc.src.tar.gz
3328 , this will create a sub-directory called sdcc with all of the sources.
3331 Change directory into the main SDCC directory, for example type:
3348 This configures the package for compilation on your system.
3364 All of the source packages will compile, this can take a while.
3380 This copies the binary executables, the include files, the libraries and
3381 the documentation to the install directories.
3382 Proceed with section
3383 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3390 Building SDCC on OSX 2.x
3393 Follow the instruction for Linux.
3397 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3398 )) fails to compile SDCC.
3399 Fortunately there's also gcc 2.9.x installed, which works fine.
3400 This compiler can be selected by running 'configure' with:
3403 ./configure CC=gcc2 CXX=g++2
3406 Cross compiling SDCC on Linux for Windows
3409 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3410 See section 'Configure Options'.
3413 Building SDCC on Windows
3416 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3418 They use Unix-sockets, which are not available on Win32.
3421 Building SDCC using Cygwin and Mingw32
3424 For building and installing a Cygwin executable follow the instructions
3430 \begin_inset Quotes sld
3434 \begin_inset Quotes srd
3437 Win32-binary can be built, which will not need the Cygwin-DLL.
3438 For the necessary 'configure' options see section 'configure options' or
3439 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3443 In order to install Cygwin on Windows download setup.exe from
3444 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3450 \begin_inset Quotes sld
3453 default text file type
3454 \begin_inset Quotes srd
3458 \begin_inset Quotes sld
3462 \begin_inset Quotes srd
3465 and download/install at least the following packages.
3466 Some packages are selected by default, others will be automatically selected
3467 because of dependencies with the manually selected packages.
3468 Never deselect these packages!
3477 gcc ; version 3.x is fine, no need to use the old 2.9x
3480 binutils ; selected with gcc
3486 rxvt ; a nice console, which makes life much easier under windoze (see below)
3489 man ; not really needed for building SDCC, but you'll miss it sooner or
3493 less ; not really needed for building SDCC, but you'll miss it sooner or
3497 svn ; only if you use Subversion access
3500 If you want to develop something you'll need:
3503 python ; for the regression tests
3506 gdb ; the gnu debugger, together with the nice GUI
3507 \begin_inset Quotes sld
3511 \begin_inset Quotes srd
3517 openssh ; to access the CF or commit changes
3520 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3521 use autoconf-stable!
3524 rxvt is a nice console with history.
3525 Replace in your cygwin.bat the line
3544 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3547 -bg black -fg white -geometry 100x65 -e bash -
3560 Text selected with the mouse is automatically copied to the clipboard, pasting
3561 works with shift-insert.
3565 The other good tip is to make sure you have no //c/-style paths anywhere,
3566 use /cygdrive/c/ instead.
3567 Using // invokes a network lookup which is very slow.
3569 \begin_inset Quotes sld
3573 \begin_inset Quotes srd
3576 is too long, you can change it with e.g.
3582 SDCC sources use the unix line ending LF.
3583 Life is much easier, if you store the source tree on a drive which is mounted
3585 And use an editor which can handle LF-only line endings.
3586 Make sure not to commit files with windows line endings.
3587 The tabulator spacing
3588 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3592 used in the project is 8.
3593 Although a tabulator spacing of 8 is a sensible choice for programmers
3594 (it's a power of 2 and allows to display 8/16 bit signed variables without
3595 loosing columns) the plan is to move towards using only spaces in the source.
3598 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3603 Download the source package
3605 either from the SDCC Subversion repository or from the
3606 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3612 , it will be named something like sdcc
3619 SDCC is distributed with all the projects, workspaces, and files you need
3620 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3621 doesn't build under MSVC).
3622 The workspace name is 'sdcc.dsw'.
3623 Please note that as it is now, all the executables are created in a folder
3627 Once built you need to copy the executables from sdcc
3631 bin before running SDCC.
3636 WARNING: Visual studio is very picky with line terminations; it expects
3637 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3638 When using the Subversion repository it's easiest to configure the svn
3639 client to convert automatically for you.
3640 If however you are getting a message such as "This makefile was not generated
3641 by Developer Studio etc.
3643 \begin_inset Quotes srd
3646 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3647 need to convert the Unix style line endings to DOS style line endings.
3648 To do so you can use the
3649 \begin_inset Quotes sld
3653 \begin_inset Quotes srd
3656 utility freely available on the internet.
3657 Doug Hawkins reported in the sdcc-user list that this works:
3665 SDCC> unix2dos sdcc.dsw
3671 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3675 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3676 flex.exe, and gawk.exe.
3677 One good place to get them is
3678 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3686 Download the file UnxUtils
3687 \begin_inset LatexCommand \index{UnxUtils}
3692 Now you have to install the utilities and setup MSVC so it can locate the
3694 Here there are two alternatives (choose one!):
3701 a) Extract UnxUtils.zip to your C:
3703 hard disk PRESERVING the original paths, otherwise bison won't work.
3704 (If you are using WinZip make certain that 'Use folder names' is selected)
3708 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3709 in 'Show directories for:' select 'Executable files', and in the directories
3710 window add a new path: 'C:
3720 (As a side effect, you get a bunch of Unix utilities that could be useful,
3721 such as diff and patch.)
3728 This one avoids extracting a bunch of files you may not use, but requires
3733 a) Create a directory were to put the tools needed, or use a directory already
3741 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3742 to such directory WITHOUT preserving the original paths.
3743 (If you are using WinZip make certain that 'Use folder names' is not selected)
3747 c) Rename bison.exe to '_bison.exe'.
3751 d) Create a batch file 'bison.bat' in 'C:
3755 ' and add these lines:
3775 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3779 Steps 'c' and 'd' are needed because bison requires by default that the
3780 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3781 '/usr/local/share/' I think.
3782 So it is necessary to tell bison where those files are located if they
3783 are not in such directory.
3784 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3788 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3789 in 'Show directories for:' select 'Executable files', and in the directories
3790 window add a new path: 'c:
3793 Note that you can use any other path instead of 'c:
3795 util', even the path where the Visual C++ tools are, probably: 'C:
3799 Microsoft Visual Studio
3804 So you don't have to execute step 'e' :)
3808 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3809 the executables from sdcc
3813 bin, and you can compile using SDCC.
3816 Building SDCC Using Borland
3819 From the sdcc directory, run the command "make -f Makefile.bcc".
3820 This should regenerate all the .exe files in the bin directory except for
3821 sdcdb.exe (which currently doesn't build under Borland C++).
3824 If you modify any source files and need to rebuild, be aware that the dependenci
3825 es may not be correctly calculated.
3826 The safest option is to delete all .obj files and run the build again.
3827 From a Cygwin BASH prompt, this can easily be done with the command (be
3828 sure you are in the sdcc directory):
3838 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3840 ) -print -exec rm {}
3849 or on Windows NT/2000/XP from the command prompt with the command:
3856 del /s *.obj *.lib *.rul
3859 from the sdcc directory.
3862 Windows Install Using a ZIP Package
3865 Download the binary zip package from
3866 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3870 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3871 This should unpack to a group of sub-directories.
3872 An example directory structure after unpacking the mingw32 package is:
3877 bin for the executables, c:
3885 lib for the include and libraries.
3888 Adjust your environment variable PATH to include the location of the bin
3889 directory or start sdcc using the full path.
3892 Windows Install Using the Setup Program
3893 \begin_inset LatexCommand \label{sub:Windows-Install}
3900 Download the setup program
3902 sdcc-x.y.z-setup.exe
3904 for an official release from
3907 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3911 or a setup program for one of the snapshots
3913 sdcc-yyyymmdd-xxxx-setup.exe
3916 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3921 A windows typical installer will guide you through the installation process.
3925 \begin_inset LatexCommand \index{VPATH}
3932 SDCC supports the VPATH feature provided by configure and make.
3933 It allows to separate the source and build trees.
3965 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3966 # extract source to directory sdcc
3971 mkdir sdcc.build\SpecialChar ~
3980 # put output in sdcc.build
3990 ../sdcc/configure\SpecialChar ~
3998 # configure is doing all the magic!
4010 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
4011 It automagically computes the variables srcdir, top_srcdir and top_buildir
4017 the generated files will be in ~/sdcc.build, while the source files stay
4020 This is not only usefull for building different binaries, e.g.
4021 when cross compiling.
4022 It also gives you a much better overview in the source tree when all the
4023 generated files are not scattered between the source files.
4024 And the best thing is: if you want to change a file you can leave the original
4025 file untouched in the source directory.
4026 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4027 Makefile.dep` and `make`.
4032 will do the rest for you!
4035 Building the Documentation
4048 -enable-doc to the configure arguments to build the documentation together
4049 with all the other stuff.
4050 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4051 dvips and makeindex) to get the job done.
4052 Another possibility is to change to the doc directory and to type
4056 \begin_inset Quotes srd
4060 \begin_inset Quotes srd
4067 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4070 \begin_inset LatexCommand \url{http://www.lyx.org}
4074 as editor is straightforward.
4075 Prebuilt documentation in html and pdf format is available from
4076 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4083 Reading the Documentation
4086 Currently reading the document in pdf format is recommended, as for unknown
4087 reason the hyperlinks are working there whereas in the html version they
4094 If you should know why please drop us a note
4100 You'll find the pdf version
4101 \begin_inset LatexCommand \index{PDF version of this document}
4106 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4114 \begin_inset LatexCommand \index{HTML version of this document}
4119 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4125 This documentation is in some aspects different from a commercial documentation:
4129 It tries to document SDCC for several processor architectures in one document
4130 (commercially these probably would be separate documents/products).
4132 \begin_inset LatexCommand \index{Status of documentation}
4136 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4138 Z80, PIC14, PIC16 and HC08.
4141 There are many references pointing away from this documentation.
4142 Don't let this distract you.
4144 was a reference like
4145 \begin_inset LatexCommand \url{http://www.opencores.org}
4149 together with a statement
4150 \begin_inset Quotes sld
4153 some processors which are targetted by SDCC can be implemented in a
4170 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4175 \begin_inset Quotes srd
4179 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4184 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4189 \begin_inset Quotes sld
4192 have you ever heard of an open source compiler that compiles a subset of
4194 \begin_inset Quotes srd
4197 we expect you to have a quick look there and come back.
4198 If you read this you are on the right track.
4201 Some sections attribute more space to problems, restrictions and warnings
4202 than to the solution.
4205 The installation section and the section about the debugger is intimidating.
4208 There are still lots of typos and there are more different writing styles
4212 Testing the SDCC Compiler
4213 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4220 The first thing you should do after installing your SDCC compiler is to
4236 \begin_inset LatexCommand \index{version}
4243 at the prompt, and the program should run and output its version like:
4248 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4252 If it doesn't run, or gives a message about not finding sdcc program, then
4253 you need to check over your installation.
4254 Make sure that the sdcc bin directory is in your executable search path
4255 defined by the PATH environment setting (
4260 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4267 Install trouble-shooting for suggestions
4270 Make sure that the sdcc program is in the bin folder, if not perhaps something
4271 did not install correctly.
4279 is commonly installed as described in section
4280 \begin_inset Quotes sld
4283 Install and search paths
4284 \begin_inset Quotes srd
4293 Make sure the compiler works on a very simple example.
4294 Type in the following test.c program using your favorite
4320 Compile this using the following command:
4329 If all goes well, the compiler will generate a test.asm and test.rel file.
4330 Congratulations, you've just compiled your first program with SDCC.
4331 We used the -c option to tell SDCC not to link the generated code, just
4332 to keep things simple for this step.
4340 The next step is to try it with the linker.
4350 If all goes well the compiler will link with the libraries and produce
4351 a test.ihx output file.
4356 (no test.ihx, and the linker generates warnings), then the problem is most
4365 usr/local/share/sdcc/lib directory
4372 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4379 Install trouble-shooting for suggestions).
4387 The final test is to ensure
4395 header files and libraries.
4396 Edit test.c and change it to the following:
4413 strcpy(str1, "testing");
4420 Compile this by typing
4427 This should generate a test.ihx output file, and it should give no warnings
4428 such as not finding the string.h file.
4429 If it cannot find the string.h file, then the problem is that
4433 cannot find the /usr/local/share/sdcc/include directory
4440 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4447 Install trouble-shooting section for suggestions).
4465 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4469 to find exactly where SDCC is looking for the include and lib files.
4472 Install Trouble-shooting
4473 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4478 \begin_inset LatexCommand \index{Install trouble-shooting}
4485 If SDCC does not build correctly
4488 A thing to try is starting from scratch by unpacking the .tgz source package
4489 again in an empty directory.
4497 ./configure 2>&1 | tee configure.log
4511 make 2>&1 | tee make.log
4518 If anything goes wrong, you can review the log files to locate the problem.
4519 Or a relevant part of this can be attached to an email that could be helpful
4520 when requesting help from the mailing list.
4524 \begin_inset Quotes sld
4528 \begin_inset Quotes srd
4535 \begin_inset Quotes sld
4539 \begin_inset Quotes srd
4542 command is a script that analyzes your system and performs some configuration
4543 to ensure the source package compiles on your system.
4544 It will take a few minutes to run, and will compile a few tests to determine
4545 what compiler features are installed.
4549 \begin_inset Quotes sld
4553 \begin_inset Quotes srd
4559 This runs the GNU make tool, which automatically compiles all the source
4560 packages into the final installed binary executables.
4564 \begin_inset Quotes sld
4568 \begin_inset Quotes erd
4574 This will install the compiler, other executables libraries and include
4575 files into the appropriate directories.
4577 \begin_inset LatexCommand \ref{sub:Install-paths}
4583 \begin_inset LatexCommand \ref{sub:Search-Paths}
4588 about install and search paths.
4590 On most systems you will need super-user privileges to do this.
4596 SDCC is not just a compiler, but a collection of tools by various developers.
4597 These include linkers, assemblers, simulators and other components.
4598 Here is a summary of some of the components.
4599 Note that the included simulator and assembler have separate documentation
4600 which you can find in the source package in their respective directories.
4601 As SDCC grows to include support for other processors, other packages from
4602 various developers are included and may have their own sets of documentation.
4606 You might want to look at the files which are installed in <installdir>.
4607 At the time of this writing, we find the following programs for gcc-builds:
4611 In <installdir>/bin:
4614 sdcc - The compiler.
4617 sdcpp - The C preprocessor.
4620 asx8051 - The assembler for 8051 type processors.
4627 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4630 aslink -The linker for 8051 type processors.
4637 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4640 s51 - The ucSim 8051 simulator.
4641 Not available on Win32 platforms.
4644 sdcdb - The source debugger.
4645 Not available on Win32 platforms.
4648 packihx - A tool to pack (compress) Intel hex files.
4651 In <installdir>/share/sdcc/include
4657 In <installdir>/share/sdcc/lib
4660 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4664 In <installdir>/share/sdcc/doc
4670 As development for other processors proceeds, this list will expand to include
4671 executables to support processors like AVR, PIC, etc.
4677 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4678 the assembler and linkage editor.
4681 sdcpp - The C-Preprocessor
4685 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4689 is a modified version of the GNU preprocessor.
4690 The C preprocessor is used to pull in #include sources, process #ifdef
4691 statements, #defines and so on.
4702 - The Assemblers and Linkage Editors
4705 This is retargettable assembler & linkage editor, it was developed by Alan
4707 John Hartman created the version for 8051, and I (Sandeep) have made some
4708 enhancements and bug fixes for it to work properly with SDCC.
4715 \begin_inset LatexCommand \index{s51}
4719 is a freeware, opensource simulator developed by Daniel Drotos.
4720 The simulator is built as part of the build process.
4721 For more information visit Daniel's web site at:
4722 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4727 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4729 S51 is currently not available on Win32 platfors.
4732 sdcdb - Source Level Debugger
4736 \begin_inset LatexCommand \index{sdcdb (debugger)}
4740 is the companion source level debugger.
4741 More about sdcdb in section
4742 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4747 The current version of the debugger uses Daniel's Simulator S51
4748 \begin_inset LatexCommand \index{s51}
4752 , but can be easily changed to use other simulators.
4753 Sdcdb is currently not available on Win32 platfors.
4762 Single Source File Projects
4765 For single source file 8051 projects the process is very simple.
4766 Compile your programs with the following command
4769 "sdcc sourcefile.c".
4773 This will compile, assemble and link your source file.
4774 Output files are as follows:
4778 \begin_inset LatexCommand \index{<file>.asm}
4783 \begin_inset LatexCommand \index{Assembler source}
4787 file created by the compiler
4791 \begin_inset LatexCommand \index{<file>.lst}
4796 \begin_inset LatexCommand \index{Assembler listing}
4800 file created by the Assembler
4804 \begin_inset LatexCommand \index{<file>.rst}
4809 \begin_inset LatexCommand \index{Assembler listing}
4813 file updated with linkedit information, created by linkage editor
4817 \begin_inset LatexCommand \index{<file>.sym}
4822 \begin_inset LatexCommand \index{Symbol listing}
4826 for the sourcefile, created by the assembler
4830 \begin_inset LatexCommand \index{<file>.rel}
4835 \begin_inset LatexCommand \index{<file>.o}
4840 \begin_inset LatexCommand \index{Object file}
4844 created by the assembler, input to Linkage editor
4848 \begin_inset LatexCommand \index{<file>.map}
4853 \begin_inset LatexCommand \index{Memory map}
4857 for the load module, created by the Linker
4861 \begin_inset LatexCommand \index{<file>.mem}
4865 - A file with a summary of the memory usage
4869 \begin_inset LatexCommand \index{<file>.ihx}
4873 - The load module in Intel hex format
4874 \begin_inset LatexCommand \index{Intel hex format}
4878 (you can select the Motorola S19 format
4879 \begin_inset LatexCommand \index{Motorola S19 format}
4894 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4899 If you need another format you might want to use
4906 \begin_inset LatexCommand \index{objdump (tool)}
4917 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4922 Both formats are documented in the documentation of srecord
4923 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4931 \begin_inset LatexCommand \index{<file>.adb}
4935 - An intermediate file containing debug information needed to create the
4947 \begin_inset LatexCommand \index{-\/-debug}
4955 \begin_inset LatexCommand \index{<file>.cdb}
4959 - An optional file (with -
4969 -debug) containing debug information.
4970 The format is documented in cdbfileformat.pdf
4975 \begin_inset LatexCommand \index{<file> (no extension)}
4979 An optional AOMF or AOMF51
4980 \begin_inset LatexCommand \index{AOMF, AOMF51}
4985 \begin_inset LatexCommand \label{OMF file}
4989 file containing debug information (generated with option -
5016 ormat is commonly used by third party tools (debuggers
5017 \begin_inset LatexCommand \index{Debugger}
5021 , simulators, emulators)
5025 \begin_inset LatexCommand \index{<file>.dump*}
5029 - Dump file to debug the compiler it self (generated with option -
5039 -dumpall) (see section
5040 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5046 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5052 \begin_inset Quotes sld
5055 Anatomy of the compiler
5056 \begin_inset Quotes srd
5062 Projects with Multiple Source Files
5065 SDCC can compile only ONE file at a time.
5066 Let us for example assume that you have a project containing the following
5071 foo1.c (contains some functions)
5073 foo2.c (contains some more functions)
5075 foomain.c (contains more functions and the function main)
5083 The first two files will need to be compiled separately with the commands:
5115 Then compile the source file containing the
5120 \begin_inset LatexCommand \index{Linker}
5124 the files together with the following command:
5132 foomain.c\SpecialChar ~
5133 foo1.rel\SpecialChar ~
5138 \begin_inset LatexCommand \index{<file>.rel}
5150 can be separately compiled as well:
5161 sdcc foomain.rel foo1.rel foo2.rel
5168 The file containing the
5183 file specified in the command line, since the linkage editor processes
5184 file in the order they are presented to it.
5185 The linker is invoked from SDCC using a script file with extension .lnk
5186 \begin_inset LatexCommand \index{<file>.lnk}
5191 You can view this file to troubleshoot linking problems such as those arising
5192 from missing libraries.
5195 Projects with Additional Libraries
5196 \begin_inset LatexCommand \index{Libraries}
5203 Some reusable routines may be compiled into a library, see the documentation
5204 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5208 \begin_inset LatexCommand \index{<file>.lib}
5215 Libraries created in this manner can be included in the command line.
5216 Make sure you include the -L <library-path> option to tell the linker where
5217 to look for these files if they are not in the current directory.
5218 Here is an example, assuming you have the source file
5230 (if that is not the same as your current project):
5237 sdcc foomain.c foolib.lib -L mylib
5248 must be an absolute path name.
5252 The most efficient way to use libraries is to keep separate modules in separate
5254 The lib file now should name all the modules.rel
5255 \begin_inset LatexCommand \index{<file>.rel}
5260 For an example see the standard library file
5264 in the directory <installdir>/share/lib/small.
5267 Using sdcclib to Create and Manage Libraries
5268 \begin_inset LatexCommand \index{sdcclib}
5275 Alternatively, instead of having a .rel file for each entry on the library
5276 file as described in the preceding section, sdcclib can be used to embed
5277 all the modules belonging to such library in the library file itself.
5278 This results in a larger library file, but it greatly reduces the number
5279 of disk files accessed by the linker.
5280 Additionally, the packed library file contains an index of all include
5281 modules and symbols that significantly speeds up the linking process.
5282 To display a list of options supported by sdcclib type:
5291 \begin_inset LatexCommand \index{sdcclib}
5302 To create a new library file, start by compiling all the required modules.
5340 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5342 The next step is to add the .rel files to the library file:
5350 sdcclib libint.lib _divsint.rel
5353 \begin_inset LatexCommand \index{sdcclib}
5363 sdcclib libint.lib _divuint.rel
5369 sdcclib libint.lib _modsint.rel
5375 sdcclib libint.lib _moduint.rel
5381 sdcclib libint.lib _mulint.rel
5388 If the file already exists in the library, it will be replaced.
5389 To see what modules and symbols are included in the library, options -s
5390 and -m are available.
5398 sdcclib -s libint.lib
5401 \begin_inset LatexCommand \index{sdcclib}
5511 If the source files are compiled using -
5522 \begin_inset LatexCommand \index{-\/-debug}
5526 , the corresponding debug information file .adb will be include in the library
5528 The library files created with sdcclib are plain text files, so they can
5529 be viewed with a text editor.
5530 It is not recomended to modify a library file created with sdcclib using
5531 a text editor, as there are file indexes numbers located accross the file
5532 used by the linker to quickly locate the required module to link.
5533 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5534 it can be safely deleted, since all the information required for linking
5535 is embedded in the library file itself.
5536 Library files created using sdcclib are used as described in the preceding
5540 Command Line Options
5541 \begin_inset LatexCommand \index{Command Line Options}
5548 Processor Selection Options
5549 \begin_inset LatexCommand \index{Options processor selection}
5554 \begin_inset LatexCommand \index{Processor selection options}
5560 \labelwidthstring 00.00.0000
5565 \begin_inset LatexCommand \index{-mmcs51}
5571 Generate code for the Intel MCS51
5572 \begin_inset LatexCommand \index{MCS51}
5576 family of processors.
5577 This is the default processor target.
5579 \labelwidthstring 00.00.0000
5584 \begin_inset LatexCommand \index{-mds390}
5590 Generate code for the Dallas DS80C390
5591 \begin_inset LatexCommand \index{DS80C390}
5597 \labelwidthstring 00.00.0000
5602 \begin_inset LatexCommand \index{-mds400}
5608 Generate code for the Dallas DS80C400
5609 \begin_inset LatexCommand \index{DS80C400}
5615 \labelwidthstring 00.00.0000
5620 \begin_inset LatexCommand \index{-mhc08}
5626 Generate code for the Freescale/Motorola HC08
5627 \begin_inset LatexCommand \index{HC08}
5631 family of processors.
5633 \labelwidthstring 00.00.0000
5638 \begin_inset LatexCommand \index{-mz80}
5644 Generate code for the Zilog Z80
5645 \begin_inset LatexCommand \index{Z80}
5649 family of processors.
5651 \labelwidthstring 00.00.0000
5656 \begin_inset LatexCommand \index{-mgbz80}
5662 Generate code for the GameBoy Z80
5663 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5667 processor (Not actively maintained).
5669 \labelwidthstring 00.00.0000
5674 \begin_inset LatexCommand \index{-mavr}
5680 Generate code for the Atmel AVR
5681 \begin_inset LatexCommand \index{AVR}
5685 processor (In development, not complete).
5686 AVR users should probably have a look at winavr
5687 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5692 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5699 I think it is fair to direct users there for now.
5700 Open source is also about avoiding unnecessary work .
5701 But I didn't find the 'official' link.
5703 \labelwidthstring 00.00.0000
5708 \begin_inset LatexCommand \index{-mpic14}
5714 Generate code for the Microchip PIC 14
5715 \begin_inset LatexCommand \index{PIC14}
5719 -bit processors (p16f84 and variants.
5720 In development, not complete).
5723 p16f627 p16f628 p16f84 p16f873 p16f877?
5725 \labelwidthstring 00.00.0000
5730 \begin_inset LatexCommand \index{-mpic16}
5736 Generate code for the Microchip PIC 16
5737 \begin_inset LatexCommand \index{PIC16}
5741 -bit processors (p18f452 and variants.
5742 In development, not complete).
5744 \labelwidthstring 00.00.0000
5750 Generate code for the Toshiba TLCS-900H
5751 \begin_inset LatexCommand \index{TLCS-900H}
5755 processor (Not maintained, not complete).
5757 \labelwidthstring 00.00.0000
5762 \begin_inset LatexCommand \index{-mxa51}
5768 Generate code for the Phillips XA51
5769 \begin_inset LatexCommand \index{XA51}
5773 processor (Not maintained, not complete).
5776 Preprocessor Options
5777 \begin_inset LatexCommand \index{Options preprocessor}
5782 \begin_inset LatexCommand \index{Preprocessor options}
5787 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5793 \labelwidthstring 00.00.0000
5798 \begin_inset LatexCommand \index{-I<path>}
5804 The additional location where the pre processor will look for <..h> or
5805 \begin_inset Quotes eld
5809 \begin_inset Quotes erd
5814 \labelwidthstring 00.00.0000
5819 \begin_inset LatexCommand \index{-D<macro[=value]>}
5825 Command line definition of macros.
5826 Passed to the preprocessor.
5828 \labelwidthstring 00.00.0000
5833 \begin_inset LatexCommand \index{-M}
5839 Tell the preprocessor to output a rule suitable for make describing the
5840 dependencies of each object file.
5841 For each source file, the preprocessor outputs one make-rule whose target
5842 is the object file name for that source file and whose dependencies are
5843 all the files `#include'd in it.
5844 This rule may be a single line or may be continued with `
5846 '-newline if it is long.
5847 The list of rules is printed on standard output instead of the preprocessed
5850 \begin_inset LatexCommand \index{-E}
5856 \labelwidthstring 00.00.0000
5861 \begin_inset LatexCommand \index{-C}
5867 Tell the preprocessor not to discard comments.
5868 Used with the `-E' option.
5870 \labelwidthstring 00.00.0000
5875 \begin_inset LatexCommand \index{-MM}
5886 Like `-M' but the output mentions only the user header files included with
5888 \begin_inset Quotes eld
5892 System header files included with `#include <file>' are omitted.
5894 \labelwidthstring 00.00.0000
5899 \begin_inset LatexCommand \index{-Aquestion(answer)}
5905 Assert the answer answer for question, in case it is tested with a preprocessor
5906 conditional such as `#if #question(answer)'.
5907 `-A-' disables the standard assertions that normally describe the target
5910 \labelwidthstring 00.00.0000
5915 \begin_inset LatexCommand \index{-Umacro}
5921 Undefine macro macro.
5922 `-U' options are evaluated after all `-D' options, but before any `-include'
5923 and `-imacros' options.
5925 \labelwidthstring 00.00.0000
5930 \begin_inset LatexCommand \index{-dM}
5936 Tell the preprocessor to output only a list of the macro definitions that
5937 are in effect at the end of preprocessing.
5938 Used with the `-E' option.
5940 \labelwidthstring 00.00.0000
5945 \begin_inset LatexCommand \index{-dD}
5951 Tell the preprocessor to pass all macro definitions into the output, in
5952 their proper sequence in the rest of the output.
5954 \labelwidthstring 00.00.0000
5959 \begin_inset LatexCommand \index{-dN}
5970 Like `-dD' except that the macro arguments and contents are omitted.
5971 Only `#define name' is included in the output.
5973 \labelwidthstring 00.00.0000
5978 preprocessorOption[,preprocessorOption]
5981 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5986 Pass the preprocessorOption to the preprocessor
5991 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5996 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5997 Collection (gcc), if you need more dedicated options please refer to the
5999 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
6007 \begin_inset LatexCommand \index{Options linker}
6012 \begin_inset LatexCommand \index{Linker options}
6018 \labelwidthstring 00.00.0000
6038 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6043 \begin_inset LatexCommand \index{-L -\/-lib-path}
6050 <absolute path to additional libraries> This option is passed to the linkage
6051 editor's additional libraries
6052 \begin_inset LatexCommand \index{Libraries}
6057 The path name must be absolute.
6058 Additional library files may be specified in the command line.
6059 See section Compiling programs for more details.
6061 \labelwidthstring 00.00.0000
6078 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6083 <Value> The start location of the external ram
6084 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6088 , default value is 0.
6089 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6099 -xram-loc 0x8000 or -
6111 \labelwidthstring 00.00.0000
6128 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6133 <Value> The start location of the code
6134 \begin_inset LatexCommand \index{code}
6138 segment, default value 0.
6139 Note when this option is used the interrupt vector table is also relocated
6140 to the given address.
6141 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6151 -code-loc 0x8000 or -
6163 \labelwidthstring 00.00.0000
6180 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6185 <Value> By default the stack
6186 \begin_inset LatexCommand \index{stack}
6190 is placed after the data segment.
6191 Using this option the stack can be placed anywhere in the internal memory
6193 The value entered can be in Hexadecimal or Decimal format, e.g.
6204 -stack-loc 0x20 or -
6215 Since the sp register is incremented before a push or call, the initial
6216 sp will be set to one byte prior the provided value.
6217 The provided value should not overlap any other memory areas such as used
6218 register banks or the data segment and with enough space for the current
6236 \begin_inset LatexCommand \index{-\/-pack-iram}
6240 option (which is now a default setting) will override this setting, so
6241 you should also specify the
6257 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6261 option if you need to manually place the stack.
6263 \labelwidthstring 00.00.0000
6280 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6285 <Value> By default the external stack
6286 \begin_inset LatexCommand \index{xstack}
6290 is placed after the pdata segment.
6291 Using this option the xstack can be placed anywhere in the external memory
6293 The value entered can be in Hexadecimal or Decimal format, e.g.
6304 -xstack-loc 0x8000 or -
6315 The provided value should not overlap any other memory areas such as the
6316 pdata or xdata segment and with enough space for the current application.
6318 \labelwidthstring 00.00.0000
6335 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6340 <Value> The start location of the internal ram data
6341 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6346 The value entered can be in Hexadecimal or Decimal format, eg.
6368 (By default, the start location of the internal ram data segment is set
6369 as low as possible in memory, taking into account the used register banks
6370 and the bit segment at address 0x20.
6371 For example if register banks 0 and 1 are used without bit variables, the
6372 data segment will be set, if -
6382 -data-loc is not used, to location 0x10.)
6384 \labelwidthstring 00.00.0000
6401 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6406 <Value> The start location of the indirectly addressable internal ram
6407 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6411 of the 8051, default value is 0x80.
6412 The value entered can be in Hexadecimal or Decimal format, eg.
6423 -idata-loc 0x88 or -
6435 \labelwidthstring 00.00.0000
6452 <Value> The start location of the bit
6453 \begin_inset LatexCommand \index{bit}
6457 addressable internal ram of the 8051.
6463 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6466 \labelwidthstring 00.00.0000
6481 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6490 The linker output (final object code) is in Intel Hex format.
6491 \begin_inset LatexCommand \index{Intel hex format}
6495 This is the default option.
6496 The format itself is documented in the documentation of srecord
6497 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6503 \labelwidthstring 00.00.0000
6518 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6527 The linker output (final object code) is in Motorola S19 format
6528 \begin_inset LatexCommand \index{Motorola S19 format}
6533 The format itself is documented in the documentation of srecord.
6535 \labelwidthstring 00.00.0000
6550 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6559 The linker output (final object code) is in ELF format
6560 \begin_inset LatexCommand \index{ELF format}
6565 (Currently only supported for the HC08 processors)
6567 \labelwidthstring 00.00.0000
6572 linkOption[,linkOption]
6575 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6580 Pass the linkOption to the linker.
6581 See file sdcc/as/doc/asxhtm.html for more on linker options.
6585 \begin_inset LatexCommand \index{Options MCS51}
6590 \begin_inset LatexCommand \index{MCS51 options}
6596 \labelwidthstring 00.00.0000
6611 \begin_inset LatexCommand \index{-\/-model-small}
6622 Generate code for Small Model programs, see section Memory Models for more
6624 This is the default model.
6626 \labelwidthstring 00.00.0000
6641 \begin_inset LatexCommand \index{-\/-model-medium}
6647 Generate code for Medium model programs, see section Memory Models for
6649 If this option is used all source files in the project have to be compiled
6651 It must also be used when invoking the linker.
6653 \labelwidthstring 00.00.0000
6668 \begin_inset LatexCommand \index{-\/-model-large}
6674 Generate code for Large model programs, see section Memory Models for more
6676 If this option is used all source files in the project have to be compiled
6678 It must also be used when invoking the linker.
6680 \labelwidthstring 00.00.0000
6695 \begin_inset LatexCommand \index{-\/-xstack}
6701 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6702 variables and passing parameters.
6704 \begin_inset LatexCommand \ref{sub:External-Stack}
6709 External Stack for more details.
6711 \labelwidthstring 00.00.0000
6729 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6733 Causes the linker to check if the internal ram usage is within limits of
6736 \labelwidthstring 00.00.0000
6754 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6758 Causes the linker to check if the external ram usage is within limits of
6761 \labelwidthstring 00.00.0000
6779 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6783 Causes the linker to check if the code memory usage is within limits of
6786 \labelwidthstring 00.00.0000
6804 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6808 Causes the linker to check if there is at minimum <Value> bytes for stack.
6810 \labelwidthstring 00.00.0000
6828 \begin_inset LatexCommand \index{-\/-pack-iram}
6832 Causes the linker to use unused register banks for data variables and pack
6833 data, idata and stack together.
6834 This is the default now.
6836 \labelwidthstring 00.00.0000
6854 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6858 Causes the linker to use old style for allocating memory areas.
6861 DS390 / DS400 Options
6862 \begin_inset LatexCommand \index{Options DS390}
6867 \begin_inset LatexCommand \index{DS390 options}
6873 \labelwidthstring 00.00.0000
6890 \begin_inset LatexCommand \index{-\/-model-flat24}
6900 Generate 24-bit flat mode code.
6901 This is the one and only that the ds390 code generator supports right now
6902 and is default when using
6907 See section Memory Models for more details.
6909 \labelwidthstring 00.00.0000
6924 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6930 disable interrupts during ESP:SP updates.
6932 \labelwidthstring 00.00.0000
6949 \begin_inset LatexCommand \index{-\/-stack-10bit}
6953 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6954 This is the one and only that the ds390 code generator supports right now
6955 and is default when using
6960 In this mode, the stack is located in the lower 1K of the internal RAM,
6961 which is mapped to 0x400000.
6962 Note that the support is incomplete, since it still uses a single byte
6963 as the stack pointer.
6964 This means that only the lower 256 bytes of the potential 1K stack space
6965 will actually be used.
6966 However, this does allow you to reclaim the precious 256 bytes of low RAM
6967 for use for the DATA and IDATA segments.
6968 The compiler will not generate any code to put the processor into 10 bit
6970 It is important to ensure that the processor is in this mode before calling
6971 any re-entrant functions compiled with this option.
6972 In principle, this should work with the
6985 \begin_inset LatexCommand \index{-\/-stack-auto}
6991 option, but that has not been tested.
6992 It is incompatible with the
7005 \begin_inset LatexCommand \index{-\/-xstack}
7012 It also only makes sense if the processor is in 24 bit contiguous addressing
7025 -model-flat24 option
7029 \labelwidthstring 00.00.0000
7044 \begin_inset LatexCommand \index{-\/-stack-probe}
7050 insert call to function __stack_probe at each function prologue.
7052 \labelwidthstring 00.00.0000
7067 \begin_inset LatexCommand \index{-\/-tini-libid}
7073 <nnnn> LibraryID used in -mTININative.
7076 \labelwidthstring 00.00.0000
7091 \begin_inset LatexCommand \index{-\/-use-accelerator}
7097 generate code for DS390 Arithmetic Accelerator.
7102 \begin_inset LatexCommand \index{Options Z80}
7107 \begin_inset LatexCommand \index{Z80 options}
7113 \labelwidthstring 00.00.0000
7130 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
7140 Force a called function to always save BC.
7142 \labelwidthstring 00.00.0000
7159 \begin_inset LatexCommand \index{-\/-no-std-crt0}
7163 When linking, skip the standard crt0.o object file.
7164 You must provide your own crt0.o for your system when linking.
7168 Optimization Options
7169 \begin_inset LatexCommand \index{Options optimization}
7174 \begin_inset LatexCommand \index{Optimization options}
7180 \labelwidthstring 00.00.0000
7195 \begin_inset LatexCommand \index{-\/-nogcse}
7201 Will not do global subexpression elimination, this option may be used when
7202 the compiler creates undesirably large stack/data spaces to store compiler
7212 \begin_inset LatexCommand \index{sloc (spill location)}
7217 A warning message will be generated when this happens and the compiler
7218 will indicate the number of extra bytes it allocated.
7219 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7221 \begin_inset LatexCommand \index{\#pragma nogcse}
7225 can be used to turn off global subexpression elimination
7226 \begin_inset LatexCommand \index{Subexpression elimination}
7230 for a given function only.
7232 \labelwidthstring 00.00.0000
7247 \begin_inset LatexCommand \index{-\/-noinvariant}
7253 Will not do loop invariant optimizations, this may be turned off for reasons
7254 explained for the previous option.
7255 For more details of loop optimizations performed see Loop Invariants in
7257 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7262 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7264 \begin_inset LatexCommand \index{\#pragma noinvariant}
7268 can be used to turn off invariant optimizations for a given function only.
7270 \labelwidthstring 00.00.0000
7285 \begin_inset LatexCommand \index{-\/-noinduction}
7291 Will not do loop induction optimizations, see section strength reduction
7293 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7295 \begin_inset LatexCommand \index{\#pragma noinduction}
7299 can be used to turn off induction optimizations for a given function only.
7301 \labelwidthstring 00.00.0000
7316 \begin_inset LatexCommand \index{-\/-nojtbound}
7327 Will not generate boundary condition check when switch statements
7328 \begin_inset LatexCommand \index{switch statement}
7332 are implemented using jump-tables.
7334 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7339 Switch Statements for more details.
7340 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7342 \begin_inset LatexCommand \index{\#pragma nojtbound}
7346 can be used to turn off boundary checking for jump tables for a given function
7349 \labelwidthstring 00.00.0000
7364 \begin_inset LatexCommand \index{-\/-noloopreverse}
7373 Will not do loop reversal
7374 \begin_inset LatexCommand \index{Loop reversing}
7380 \labelwidthstring 00.00.0000
7397 \begin_inset LatexCommand \index{-\/-nolabelopt }
7401 Will not optimize labels (makes the dumpfiles more readable).
7403 \labelwidthstring 00.00.0000
7418 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7424 Will not memcpy initialized data from code space into xdata space.
7425 This saves a few bytes in code space if you don't have initialized data
7426 \begin_inset LatexCommand \index{Variable initialization}
7432 \labelwidthstring 00.00.0000
7447 \begin_inset LatexCommand \index{-\/-nooverlay}
7453 The compiler will not overlay parameters and local variables of any function,
7454 see section Parameters and local variables for more details.
7456 \labelwidthstring 00.00.0000
7471 \begin_inset LatexCommand \index{-\/-no-peep}
7477 Disable peep-hole optimization with built-in rules.
7479 \labelwidthstring 00.00.0000
7496 \begin_inset LatexCommand \index{-\/-peep-file}
7501 <filename> This option can be used to use additional rules to be used by
7502 the peep hole optimizer.
7504 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7509 Peep Hole optimizations for details on how to write these rules.
7511 \labelwidthstring 00.00.0000
7526 \begin_inset LatexCommand \index{-\/-peep-asm}
7532 Pass the inline assembler code through the peep hole optimizer.
7533 This can cause unexpected changes to inline assembler code, please go through
7534 the peephole optimizer
7535 \begin_inset LatexCommand \index{Peephole optimizer}
7539 rules defined in the source file tree '<target>/peeph.def' before using
7542 \labelwidthstring 00.00.0000
7557 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7563 The compiler will optimize code generation towards fast code, possibly
7564 at the expense of code size.
7566 \labelwidthstring 00.00.0000
7581 \begin_inset LatexCommand \index{-\/-opt-code-size}
7587 The compiler will optimize code generation towards compact code, possibly
7588 at the expense of code speed.
7592 \begin_inset LatexCommand \index{Options other}
7598 \labelwidthstring 00.00.0000
7614 \begin_inset LatexCommand \index{-\/-compile-only}
7619 \begin_inset LatexCommand \index{-c -\/-compile-only}
7625 will compile and assemble the source, but will not call the linkage editor.
7627 \labelwidthstring 00.00.0000
7646 \begin_inset LatexCommand \index{-\/-c1mode}
7652 reads the preprocessed source from standard input and compiles it.
7653 The file name for the assembler output must be specified using the -o option.
7655 \labelwidthstring 00.00.0000
7660 \begin_inset LatexCommand \index{-E}
7666 Run only the C preprocessor.
7667 Preprocess all the C source files specified and output the results to standard
7670 \labelwidthstring 00.00.0000
7676 \begin_inset LatexCommand \index{-o <path/file>}
7682 The output path resp.
7683 file where everything will be placed.
7684 If the parameter is a path, it must have a trailing slash (or backslash
7685 for the Windows binaries) to be recognized as a path.
7688 \labelwidthstring 00.00.0000
7703 \begin_inset LatexCommand \index{-\/-stack-auto}
7714 All functions in the source file will be compiled as
7719 \begin_inset LatexCommand \index{reentrant}
7724 the parameters and local variables will be allocated on the stack
7725 \begin_inset LatexCommand \index{stack}
7731 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7735 Parameters and Local Variables for more details.
7736 If this option is used all source files in the project should be compiled
7738 It automatically implies --int-long-reent and --float-reent.
7741 \labelwidthstring 00.00.0000
7756 \begin_inset LatexCommand \index{-\/-callee-saves}
7760 function1[,function2][,function3]....
7763 The compiler by default uses a caller saves convention for register saving
7764 across function calls, however this can cause unnecessary register pushing
7765 & popping when calling small functions from larger functions.
7766 This option can be used to switch the register saving convention for the
7767 function names specified.
7768 The compiler will not save registers when calling these functions, no extra
7769 code will be generated at the entry & exit (function prologue
7772 \begin_inset LatexCommand \index{function prologue}
7781 \begin_inset LatexCommand \index{function epilogue}
7787 ) for these functions to save & restore the registers used by these functions,
7788 this can SUBSTANTIALLY reduce code & improve run time performance of the
7790 In the future the compiler (with inter procedural analysis) will be able
7791 to determine the appropriate scheme to use for each function call.
7792 DO NOT use this option for built-in functions such as _mulint..., if this
7793 option is used for a library function the appropriate library function
7794 needs to be recompiled with the same option.
7795 If the project consists of multiple source files then all the source file
7796 should be compiled with the same -
7806 -callee-saves option string.
7807 Also see #pragma\SpecialChar ~
7809 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7815 \labelwidthstring 00.00.0000
7830 \begin_inset LatexCommand \index{-\/-debug}
7839 When this option is used the compiler will generate debug information.
7840 The debug information collected in a file with .cdb extension can be used
7842 For more information see documentation for SDCDB.
7843 Another file with no extension contains debug information in AOMF or AOMF51
7844 \begin_inset LatexCommand \index{AOMF, AOMF51}
7848 format which is commonly used by third party tools.
7850 \labelwidthstring 00.00.0000
7855 \begin_inset LatexCommand \index{-S}
7866 Stop after the stage of compilation proper; do not assemble.
7867 The output is an assembler code file for the input file specified.
7869 \labelwidthstring 00.00.0000
7884 \begin_inset LatexCommand \index{-\/-int-long-reent}
7890 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7891 Note by default these libraries are compiled as non-reentrant.
7892 See section Installation for more details.
7894 \labelwidthstring 00.00.0000
7909 \begin_inset LatexCommand \index{-\/-cyclomatic}
7918 This option will cause the compiler to generate an information message for
7919 each function in the source file.
7920 The message contains some
7924 information about the function.
7925 The number of edges and nodes the compiler detected in the control flow
7926 graph of the function, and most importantly the
7928 cyclomatic complexity
7929 \begin_inset LatexCommand \index{Cyclomatic complexity}
7935 see section on Cyclomatic Complexity for more details.
7937 \labelwidthstring 00.00.0000
7952 \begin_inset LatexCommand \index{-\/-float-reent}
7958 Floating point library is compiled as reentrant
7959 \begin_inset LatexCommand \index{reentrant}
7964 See section Installation for more details.
7966 \labelwidthstring 00.00.0000
7981 \begin_inset LatexCommand \index{-\/-main-return}
7987 This option can be used if the code generated is called by a monitor program
7988 or if the main routine includes an endless loop.
7989 This option might result in slightly smaller code and save two bytes of
7991 The return from the 'main'
7992 \begin_inset LatexCommand \index{main return}
7996 function will return to the function calling main.
7997 The default setting is to lock up i.e.
8004 \labelwidthstring 00.00.0000
8019 \begin_inset LatexCommand \index{-\/-nostdinc}
8025 This will prevent the compiler from passing on the default include path
8026 to the preprocessor.
8028 \labelwidthstring 00.00.0000
8043 \begin_inset LatexCommand \index{-\/-nostdlib}
8049 This will prevent the compiler from passing on the default library
8050 \begin_inset LatexCommand \index{Libraries}
8056 \labelwidthstring 00.00.0000
8071 \begin_inset LatexCommand \index{-\/-verbose}
8077 Shows the various actions the compiler is performing.
8079 \labelwidthstring 00.00.0000
8084 \begin_inset LatexCommand \index{-V}
8090 Shows the actual commands the compiler is executing.
8092 \labelwidthstring 00.00.0000
8107 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8113 Hides your ugly and inefficient c-code from the asm file, so you can always
8114 blame the compiler :)
8116 \labelwidthstring 00.00.0000
8131 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8137 Will not include peep-hole comments in the generated files.
8139 \labelwidthstring 00.00.0000
8154 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8160 Include i-codes in the asm file.
8161 Sounds like noise but is most helpful for debugging the compiler itself.
8163 \labelwidthstring 00.00.0000
8178 \begin_inset LatexCommand \index{-\/-less-pedantic}
8184 Disable some of the more pedantic warnings
8185 \begin_inset LatexCommand \index{Warnings}
8189 (jwk burps: please be more specific here, please!).
8191 \labelwidthstring 00.00.0000
8205 -disable-warning\SpecialChar ~
8207 \begin_inset LatexCommand \index{-\/-disable-warning}
8213 Disable specific warning with number <nnnn>.
8215 \labelwidthstring 00.00.0000
8230 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8236 Display the directories in the compiler's search path
8238 \labelwidthstring 00.00.0000
8253 \begin_inset LatexCommand \index{-\/-vc}
8259 Display errors and warnings using MSVC style, so you can use SDCC with
8262 \labelwidthstring 00.00.0000
8277 \begin_inset LatexCommand \index{-\/-use-stdout}
8283 Send errors and warnings to stdout instead of stderr.
8285 \labelwidthstring 00.00.0000
8290 asmOption[,asmOption]
8293 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8298 Pass the asmOption to the assembler
8299 \begin_inset LatexCommand \index{Options assembler}
8304 \begin_inset LatexCommand \index{Assembler options}
8309 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8311 \labelwidthstring 00.00.0000
8326 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8332 Generally follow the C89 standard, but allow SDCC features that conflict
8333 with the standard (default).
8335 \labelwidthstring 00.00.0000
8350 \begin_inset LatexCommand \index{-\/-std-c89}
8356 Follow the C89 standard and disable SDCC features that conflict with the
8359 \labelwidthstring 00.00.0000
8374 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8380 Generally follow the C99 standard, but allow SDCC features that conflict
8381 with the standard (incomplete support).
8383 \labelwidthstring 00.00.0000
8398 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8404 Follow the C99 standard and disable SDCC features that conflict with the
8405 standard (incomplete support).
8407 \labelwidthstring 00.00.0000
8424 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8429 <Name> The name to be used for the code
8430 \begin_inset LatexCommand \index{code}
8434 segment, default CSEG.
8435 This is useful if you need to tell the compiler to put the code in a special
8436 segment so you can later on tell the linker to put this segment in a special
8438 Can be used for instance when using bank switching to put the code in a
8441 \labelwidthstring 00.00.0000
8458 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8463 <Name> The name to be used for the const
8464 \begin_inset LatexCommand \index{code}
8468 segment, default CONST.
8469 This is useful if you need to tell the compiler to put the const data in
8470 a special segment so you can later on tell the linker to put this segment
8471 in a special place in memory.
8472 Can be used for instance when using bank switching to put the const data
8475 \labelwidthstring 00.00.0000
8487 a SDCC compiler option but if you want
8491 warnings you can use a separate tool dedicated to syntax checking like
8493 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8498 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8503 \begin_inset LatexCommand \url{http://www.splint.org}
8508 To make your source files parseable by splint you will have to include
8514 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8518 in your source file and add brackets around extended keywords (like
8521 \begin_inset Quotes sld
8534 \begin_inset Quotes srd
8542 \begin_inset Quotes sld
8545 __interrupt\SpecialChar ~
8547 \begin_inset Quotes srd
8555 Splint has an excellent on line manual at
8556 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8560 and it's capabilities go beyond pure syntax checking.
8561 You'll need to tell splint the location of SDCC's include files so a typical
8562 command line could look like this:
8566 splint\SpecialChar ~
8568 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8573 Intermediate Dump Options
8574 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8579 \begin_inset LatexCommand \index{Options intermediate dump}
8584 \begin_inset LatexCommand \index{Intermediate dump options}
8591 The following options are provided for the purpose of retargetting and debugging
8593 They provide a means to dump the intermediate code (iCode
8594 \begin_inset LatexCommand \index{iCode}
8598 ) generated by the compiler in human readable form at various stages of
8599 the compilation process.
8600 More on iCodes see chapter
8601 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8606 \begin_inset Quotes srd
8609 The anatomy of the compiler
8610 \begin_inset Quotes srd
8615 \labelwidthstring 00.00.0000
8630 \begin_inset LatexCommand \index{-\/-dumpraw}
8636 This option will cause the compiler to dump the intermediate code into
8639 <source filename>.dumpraw
8641 just after the intermediate code has been generated for a function, i.e.
8642 before any optimizations are done.
8644 \begin_inset LatexCommand \index{Basic blocks}
8648 at this stage ordered in the depth first number, so they may not be in
8649 sequence of execution.
8651 \labelwidthstring 00.00.0000
8666 \begin_inset LatexCommand \index{-\/-dumpgcse}
8672 Will create a dump of iCode's, after global subexpression elimination
8673 \begin_inset LatexCommand \index{Global subexpression elimination}
8679 <source filename>.dumpgcse.
8681 \labelwidthstring 00.00.0000
8696 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8702 Will create a dump of iCode's, after deadcode elimination
8703 \begin_inset LatexCommand \index{Dead-code elimination}
8709 <source filename>.dumpdeadcode.
8711 \labelwidthstring 00.00.0000
8726 \begin_inset LatexCommand \index{-\/-dumploop}
8735 Will create a dump of iCode's, after loop optimizations
8736 \begin_inset LatexCommand \index{Loop optimization}
8742 <source filename>.dumploop.
8744 \labelwidthstring 00.00.0000
8759 \begin_inset LatexCommand \index{-\/-dumprange}
8768 Will create a dump of iCode's, after live range analysis
8769 \begin_inset LatexCommand \index{Live range analysis}
8775 <source filename>.dumprange.
8777 \labelwidthstring 00.00.0000
8792 \begin_inset LatexCommand \index{-\/-dumlrange}
8798 Will dump the life ranges
8799 \begin_inset LatexCommand \index{Live range analysis}
8805 \labelwidthstring 00.00.0000
8820 \begin_inset LatexCommand \index{-\/-dumpregassign}
8829 Will create a dump of iCode's, after register assignment
8830 \begin_inset LatexCommand \index{Register assignment}
8836 <source filename>.dumprassgn.
8838 \labelwidthstring 00.00.0000
8853 \begin_inset LatexCommand \index{-\/-dumplrange}
8859 Will create a dump of the live ranges of iTemp's
8861 \labelwidthstring 00.00.0000
8876 \begin_inset LatexCommand \index{-\/-dumpall}
8887 Will cause all the above mentioned dumps to be created.
8890 Redirecting output on Windows Shells
8893 By default SDCC writes it's error messages to
8894 \begin_inset Quotes sld
8898 \begin_inset Quotes srd
8902 To force all messages to
8903 \begin_inset Quotes sld
8907 \begin_inset Quotes srd
8931 \begin_inset LatexCommand \index{-\/-use-stdout}
8936 Additionally, if you happen to have visual studio installed in your windows
8937 machine, you can use it to compile your sources using a custom build and
8953 \begin_inset LatexCommand \index{-\/-vc}
8958 Something like this should work:
9002 -model-large -c $(InputPath)
9005 Environment variables
9006 \begin_inset LatexCommand \index{Environment variables}
9013 SDCC recognizes the following environment variables:
9015 \labelwidthstring 00.00.0000
9020 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
9026 SDCC installs a signal handler
9027 \begin_inset LatexCommand \index{signal handler}
9031 to be able to delete temporary files after an user break (^C) or an exception.
9032 If this environment variable is set, SDCC won't install the signal handler
9033 in order to be able to debug SDCC.
9035 \labelwidthstring 00.00.0000
9042 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9048 Path, where temporary files will be created.
9049 The order of the variables is the search order.
9050 In a standard *nix environment these variables are not set, and there's
9051 no need to set them.
9052 On Windows it's recommended to set one of them.
9054 \labelwidthstring 00.00.0000
9059 \begin_inset LatexCommand \index{SDCC\_HOME}
9066 \begin_inset LatexCommand \ref{sub:Install-paths}
9072 \begin_inset Quotes sld
9076 \begin_inset Quotes srd
9081 \labelwidthstring 00.00.0000
9086 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9093 \begin_inset LatexCommand \ref{sub:Search-Paths}
9099 \begin_inset Quotes sld
9103 \begin_inset Quotes srd
9108 \labelwidthstring 00.00.0000
9113 \begin_inset LatexCommand \index{SDCC\_LIB}
9120 \begin_inset LatexCommand \ref{sub:Search-Paths}
9126 \begin_inset Quotes sld
9130 \begin_inset Quotes srd
9136 There are some more environment variables recognized by SDCC, but these
9137 are solely used for debugging purposes.
9138 They can change or disappear very quickly, and will never be documented.
9141 Storage Class Language Extensions
9144 MCS51/DS390 Storage Class
9145 \begin_inset LatexCommand \index{Storage class}
9152 In addition to the ANSI storage classes SDCC allows the following MCS51
9153 specific storage classes:
9154 \layout Subsubsection
9157 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9162 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9167 \begin_inset LatexCommand \index{near (storage class)}
9172 \begin_inset LatexCommand \index{\_\_near (storage class)}
9183 storage class for the Small Memory model (
9191 or the more ANSI-C compliant forms
9199 can be used synonymously).
9200 Variables declared with this storage class will be allocated in the directly
9201 addressable portion of the internal RAM of a 8051, e.g.:
9206 __data unsigned char test_data;
9209 Writing 0x01 to this variable generates the assembly code:
9214 75*00 01\SpecialChar ~
9220 \layout Subsubsection
9223 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9228 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9233 \begin_inset LatexCommand \index{far (storage class)}
9238 \begin_inset LatexCommand \index{\_\_far (storage class)}
9245 Variables declared with this storage class will be placed in the external
9251 storage class for the Large Memory model, e.g.:
9256 __xdata unsigned char test_xdata;
9259 Writing 0x01 to this variable generates the assembly code:
9264 90s00r00\SpecialChar ~
9293 \layout Subsubsection
9296 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9301 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9308 Variables declared with this storage class will be allocated into the indirectly
9309 addressable portion of the internal ram of a 8051, e.g.:
9314 __idata unsigned char test_idata;
9317 Writing 0x01 to this variable generates the assembly code:
9346 Please note, the first 128 byte of idata physically access the same RAM
9348 The original 8051 had 128 byte idata memory, nowadays most devices have
9349 256 byte idata memory.
9351 \begin_inset LatexCommand \index{stack}
9355 is located in idata memory.
9356 \layout Subsubsection
9359 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9364 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9371 Paged xdata access is just as straightforward as using the other addressing
9373 It is typically located at the start of xdata and has a maximum size of
9375 The following example writes 0x01 to the pdata variable.
9376 Please note, pdata access physically accesses xdata memory.
9377 The high byte of the address is determined by port P2
9378 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9382 (or in case of some 8051 variants by a separate Special Function Register,
9384 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9393 storage class for the Medium Memory model, e.g.:
9398 __pdata unsigned char test_pdata;
9401 Writing 0x01 to this variable generates the assembly code:
9445 \begin_inset LatexCommand \index{-\/-xstack}
9449 option is used the pdata memory area is followed by the xstack memory area
9450 and the sum of their sizes is limited to 256 bytes.
9451 \layout Subsubsection
9454 \begin_inset LatexCommand \index{code}
9459 \begin_inset LatexCommand \index{\_\_code}
9466 'Variables' declared with this storage class will be placed in the code
9472 __code unsigned char test_code;
9475 Read access to this variable generates the assembly code:
9480 90s00r6F\SpecialChar ~
9483 mov dptr,#_test_code
9512 indexed arrays of characters in code memory can be accessed efficiently:
9517 __code char test_array[] = {'c','h','e','a','p'};
9520 Read access to this array using an 8-bit unsigned index generates the assembly
9537 90s00r41\SpecialChar ~
9540 mov dptr,#_test_array
9555 \layout Subsubsection
9558 \begin_inset LatexCommand \index{bit}
9563 \begin_inset LatexCommand \index{\_\_bit}
9570 This is a data-type and a storage class specifier.
9571 When a variable is declared as a bit, it is allocated into the bit addressable
9572 memory of 8051, e.g.:
9580 Writing 1 to this variable generates the assembly code:
9596 The bit addressable memory consists of 128 bits which are located from 0x20
9597 to 0x2f in data memory.
9600 Apart from this 8051 specific storage class most architectures support ANSI-C
9602 \begin_inset LatexCommand \index{bitfields}
9612 Not really meant as examples, but nevertheless showing what bitfields are
9613 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9617 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9618 signed modifier are implemented as unsigned.
9619 \layout Subsubsection
9622 \begin_inset LatexCommand \index{sfr}
9627 \begin_inset LatexCommand \index{\_\_sfr}
9632 \begin_inset LatexCommand \index{sfr16}
9637 \begin_inset LatexCommand \index{\_\_sfr16}
9642 \begin_inset LatexCommand \index{sfr32}
9647 \begin_inset LatexCommand \index{\_\_sfr32}
9652 \begin_inset LatexCommand \index{\_\_sbit}
9659 Like the bit keyword,
9661 sfr / sfr16 / sfr32 / sbit
9663 signify both a data-type and storage class, they are used to describe the
9684 variables of a 8051, eg:
9690 \begin_inset LatexCommand \index{at}
9695 \begin_inset LatexCommand \index{\_\_at}
9699 (0x80) P0;\SpecialChar ~
9700 /* special function register P0 at location 0x80 */
9702 /* 16 bit special function register combination for timer 0 */
9704 /* with the high byte at location 0x8C and the low byte at location 0x8A
9708 \begin_inset LatexCommand \index{at}
9713 \begin_inset LatexCommand \index{\_\_at}
9719 __sbit __at (0xd7) CY; /* CY (Carry Flag
9720 \begin_inset LatexCommand \index{Flags}
9725 \begin_inset LatexCommand \index{Carry flag}
9732 Special function registers which are located on an address dividable by
9733 8 are bit-addressable, an
9737 addresses a specific bit within these sfr.
9739 16 Bit and 32 bit special function register combinations which require a
9740 certain access order are better not declared using
9749 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9750 this is not guaranteed.
9751 \layout Subsubsection
9754 \begin_inset LatexCommand \index{Pointer}
9758 to MCS51/DS390 specific memory spaces
9761 SDCC allows (via language extensions) pointers to explicitly point to any
9762 of the memory spaces
9763 \begin_inset LatexCommand \index{Memory model}
9768 In addition to the explicit pointers, the compiler uses (by default) generic
9769 pointers which can be used to point to any of the memory spaces.
9773 Pointer declaration examples:
9778 /* pointer physically in internal ram pointing to object in external ram
9781 __xdata unsigned char * __data p;
9785 /* pointer physically in external ram pointing to object in internal ram
9788 __data unsigned char * __xdata p;
9792 /* pointer physically in code rom pointing to data in xdata space */
9794 __xdata unsigned char * __code p;
9798 /* pointer physically in code space pointing to data in code space */
9800 __code unsigned char * __code p;
9804 /* the following is a generic pointer physically located in xdata space
9811 /* the following is a function pointer physically located in data space
9814 char (* __data fp)(void);
9817 Well you get the idea.
9822 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9835 The highest order byte of the
9839 pointers contains the data space information.
9840 Assembler support routines are called whenever data is stored or retrieved
9846 These are useful for developing reusable library
9847 \begin_inset LatexCommand \index{Libraries}
9852 Explicitly specifying the pointer type will generate the most efficient
9854 \layout Subsubsection
9856 Notes on MCS51 memory
9857 \begin_inset LatexCommand \index{MCS51 memory}
9864 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9865 RAM memory which is structured as follows:
9869 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9872 - Bytes 20-2F - 16 bytes to hold 128 bit
9873 \begin_inset LatexCommand \index{bit}
9879 - Bytes 30-7F - 80 bytes for general purpose use.
9884 Additionally some members of the MCS51 family may have up to 128 bytes of
9885 additional, indirectly addressable, internal RAM memory (
9890 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9895 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9900 Furthermore, some chips may have some built in external memory (
9905 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9910 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9914 ) which should not be confused with the internal, directly addressable RAM
9920 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9925 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9930 Sometimes this built in
9934 memory has to be activated before using it (you can probably find this
9935 information on the datasheet of the microcontroller your are using, see
9937 \begin_inset LatexCommand \ref{sub:Startup-Code}
9945 Normally SDCC will only use the first bank
9946 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9950 of registers (register bank 0), but it is possible to specify that other
9951 banks of registers (keyword
9958 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9963 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9969 ) should be used in interrupt
9970 \begin_inset LatexCommand \index{interrupt}
9975 \begin_inset LatexCommand \index{\_\_interrupt}
9980 By default, the compiler will place the stack after the last byte of allocated
9981 memory for variables.
9982 For example, if the first 2 banks of registers are used, and only four
9987 variables, it will position the base of the internal stack at address 20
9989 This implies that as the stack
9990 \begin_inset LatexCommand \index{stack}
9994 grows, it will use up the remaining register banks, and the 16 bytes used
9995 by the 128 bit variables, and 80 bytes for general purpose use.
9996 If any bit variables are used, the data variables will be placed in unused
9997 register banks and after the byte holding the last bit variable.
9998 For example, if register banks 0 and 1 are used, and there are 9 bit variables
10003 variables will be placed starting from address 0x10 to 0x20 and continue
10016 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
10020 to specify the start address of the
10035 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10039 to specify the size of the total internal RAM (
10053 By default the 8051 linker will place the stack after the last byte of (i)data
10066 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10070 allows you to specify the start of the stack, i.e.
10071 you could start it after any data in the general purpose area.
10072 If your microcontroller has additional indirectly addressable internal
10077 ) you can place the stack on it.
10078 You may also need to use -
10089 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10093 to set the start address of the external RAM (
10108 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10112 to specify its size.
10113 Same goes for the code memory, using -
10124 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10139 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10144 If in doubt, don't specify any options and see if the resulting memory
10145 layout is appropriate, then you can adjust it.
10148 The linker generates two files with memory allocation information.
10149 The first, with extension .map
10150 \begin_inset LatexCommand \index{<file>.map}
10154 shows all the variables and segments.
10155 The second with extension .mem
10156 \begin_inset LatexCommand \index{<file>.mem}
10160 shows the final memory layout.
10161 The linker will complain either if memory segments overlap, there is not
10162 enough memory, or there is not enough space for stack.
10163 If you get any linking warnings and/or errors related to stack or segments
10164 allocation, take a look at either the .map or .mem files to find out what
10166 The .mem file may even suggest a solution to the problem.
10169 Z80/Z180 Storage Class
10170 \begin_inset LatexCommand \index{Storage class}
10174 Language Extensions
10175 \layout Subsubsection
10178 \begin_inset LatexCommand \index{sfr}
10183 \begin_inset LatexCommand \index{\_\_sfr}
10187 (in/out to 8-bit addresses)
10191 \begin_inset LatexCommand \index{Z80}
10195 family has separate address spaces for memory and
10205 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10209 is accessed with special instructions, e.g.:
10214 sfr at 0x78 IoPort;\SpecialChar ~
10216 /* define a var in I/O space at 78h called IoPort */
10220 Writing 0x01 to this variable generates the assembly code:
10225 3E 01\SpecialChar ~
10233 D3 78\SpecialChar ~
10240 \layout Subsubsection
10243 \begin_inset LatexCommand \index{sfr}
10248 \begin_inset LatexCommand \index{\_\_sfr}
10252 (in/out to 16-bit addresses)
10259 is used to support 16 bit addresses in I/O memory e.g.:
10265 \begin_inset LatexCommand \index{at}
10270 \begin_inset LatexCommand \index{\_\_at}
10277 Writing 0x01 to this variable generates the assembly code:
10282 01 23 01\SpecialChar ~
10287 3E 01\SpecialChar ~
10295 ED 79\SpecialChar ~
10302 \layout Subsubsection
10305 \begin_inset LatexCommand \index{sfr}
10310 \begin_inset LatexCommand \index{\_\_sfr}
10314 (in0/out0 to 8 bit addresses on Z180
10315 \begin_inset LatexCommand \index{Z180}
10320 \begin_inset LatexCommand \index{HD64180}
10327 The compiler option -
10337 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
10339 \begin_inset LatexCommand \index{\#pragma portmode}
10343 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10353 If you include the file z180.h this will be set automatically.
10357 \begin_inset LatexCommand \index{Storage class}
10361 Language Extensions
10362 \layout Subsubsection
10365 \begin_inset LatexCommand \index{data (hc08 storage class)}
10370 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10377 The data storage class declares a variable that resides in the first 256
10378 bytes of memory (the direct page).
10379 The HC08 is most efficient at accessing variables (especially pointers)
10381 \layout Subsubsection
10384 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10389 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10396 The xdata storage class declares a variable that can reside anywhere in
10398 This is the default if no storage class is specified.
10402 Absolute Addressing
10403 \begin_inset LatexCommand \index{Absolute addressing}
10410 Data items can be assigned an absolute address with the
10413 \begin_inset LatexCommand \index{at}
10418 \begin_inset LatexCommand \index{\_\_at}
10424 keyword, in addition to a storage class, e.g.:
10430 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10435 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10440 \begin_inset LatexCommand \index{at}
10445 \begin_inset LatexCommand \index{\_\_at}
10449 (0x7ffe) unsigned int chksum;
10457 __xdata __at (0x7ffe) unsigned int chksum;
10460 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10461 of the external ram.
10466 reserve any space for variables declared in this way
10467 \begin_inset Marginal
10478 (they are implemented with an equate in the assembler).
10479 Thus it is left to the programmer to make sure there are no overlaps with
10480 other variables that are declared without the absolute address.
10481 The assembler listing file (.lst
10482 \begin_inset LatexCommand \index{<file>.lst}
10486 ) and the linker output files (.rst
10487 \begin_inset LatexCommand \index{<file>.rst}
10492 \begin_inset LatexCommand \index{<file>.map}
10496 ) are good places to look for such overlaps.
10497 Variables with an absolute address are
10500 \begin_inset Marginal
10514 \begin_inset LatexCommand \index{Variable initialization}
10521 In case of memory mapped I/O devices the keyword
10525 has to be used to tell the compiler that accesses might not be removed:
10531 \begin_inset LatexCommand \index{volatile}
10536 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10541 \begin_inset LatexCommand \index{at}
10545 (0x8000) unsigned char PORTA_8255;
10548 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10553 \begin_inset LatexCommand \index{Aligned array}
10560 starts at a block (256 byte) boundary
10561 \begin_inset LatexCommand \index{block boundary}
10566 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10572 Absolute addresses can be specified for variables in all storage classes,
10579 \begin_inset LatexCommand \index{bit}
10584 \begin_inset LatexCommand \index{at}
10591 The above example will allocate the variable at offset 0x02 in the bit-addressab
10593 There is no real advantage to assigning absolute addresses to variables
10594 in this manner, unless you want strict control over all the variables allocated.
10595 One possible use would be to write hardware portable code.
10596 For example, if you have a routine that uses one or more of the microcontroller
10597 I/O pins, and such pins are different for two different hardwares, you
10598 can declare the I/O pins in your routine using:
10604 \begin_inset LatexCommand \index{volatile}
10608 __bit MOSI;\SpecialChar ~
10612 /* master out, slave in */
10614 extern volatile __bit MISO;\SpecialChar ~
10618 /* master in, slave out */
10620 extern volatile __bit MCLK;\SpecialChar ~
10628 /* Input and Output of a byte on a 3-wire serial bus.
10633 If needed adapt polarity of clock, polarity of data and bit order
10638 unsigned char spi_io(unsigned char out_byte)
10662 MOSI = out_byte & 0x80;
10692 /* _asm nop _endasm; */\SpecialChar ~
10700 /* for slow peripherals */
10751 Then, someplace in the code for the first hardware you would use
10757 \begin_inset LatexCommand \index{at}
10762 \begin_inset LatexCommand \index{\_\_at}
10766 (0x80) MOSI;\SpecialChar ~
10770 /* I/O port 0, bit 0 */
10772 __bit __at (0x81) MISO;\SpecialChar ~
10776 /* I/O port 0, bit 1 */
10778 __bit __at (0x82) MCLK;\SpecialChar ~
10782 /* I/O port 0, bit 2 */
10785 Similarly, for the second hardware you would use
10790 __bit __at (0x83) MOSI;\SpecialChar ~
10794 /* I/O port 0, bit 3 */
10796 __bit __at (0x91) MISO;\SpecialChar ~
10800 /* I/O port 1, bit 1 */
10803 \begin_inset LatexCommand \index{bit}
10807 __at (0x92) MCLK;\SpecialChar ~
10811 /* I/O port 1, bit 2 */
10814 and you can use the same hardware dependent routine without changes, as
10815 for example in a library.
10816 This is somehow similar to sbit, but only one absolute address has to be
10817 specified in the whole project.
10821 \begin_inset LatexCommand \index{Parameters}
10826 \begin_inset LatexCommand \index{function parameter}
10831 \begin_inset LatexCommand \index{local variables}
10836 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10843 Automatic (local) variables and parameters to functions can either be placed
10844 on the stack or in data-space.
10845 The default action of the compiler is to place these variables in the internal
10846 RAM (for small model) or external RAM (for large model).
10847 This in fact makes them similar to
10850 \begin_inset LatexCommand \index{static}
10856 so by default functions are non-reentrant
10857 \begin_inset LatexCommand \index{reentrant}
10866 They can be placed on the stack
10867 \begin_inset LatexCommand \index{stack}
10884 \begin_inset LatexCommand \index{-\/-stack-auto}
10892 #pragma\SpecialChar ~
10896 \begin_inset LatexCommand \index{\#pragma stackauto}
10903 \begin_inset LatexCommand \index{reentrant}
10909 keyword in the function declaration, e.g.:
10914 unsigned char foo(char i) __reentrant
10928 Since stack space on 8051 is limited, the
10946 option should be used sparingly.
10947 Note that the reentrant keyword just means that the parameters & local
10948 variables will be allocated to the stack, it
10952 mean that the function is register bank
10953 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10962 \begin_inset LatexCommand \index{local variables}
10966 can be assigned storage classes and absolute
10967 \begin_inset LatexCommand \index{Absolute addressing}
10976 unsigned char foo()
10984 __xdata unsigned char i;
10997 \begin_inset LatexCommand \index{at}
11001 (0x31) unsigned char j;
11013 In the above example the variable
11017 will be allocated in the external ram,
11021 in bit addressable space and
11040 or when a function is declared as
11044 this should only be done for static variables.
11048 \begin_inset LatexCommand \index{function parameter}
11052 however are not allowed any storage class
11053 \begin_inset LatexCommand \index{Storage class}
11057 , (storage classes for parameters will be ignored), their allocation is
11058 governed by the memory model in use, and the reentrancy options.
11061 It is however allowed to use bit parameters in reentrant functions and also
11062 non-static local bit variables are supported.
11063 Efficient use is limited to 8 semi-bitregisters in bit space.
11064 They are pushed and popped to stack as a single byte just like the normal
11069 \begin_inset LatexCommand \label{sub:Overlaying}
11074 \begin_inset LatexCommand \index{Overlaying}
11082 \begin_inset LatexCommand \index{reentrant}
11086 functions SDCC will try to reduce internal ram space usage by overlaying
11087 parameters and local variables of a function (if possible).
11088 Parameters and local variables
11089 \begin_inset LatexCommand \index{local variables}
11093 of a function will be allocated to an overlayable segment if the function
11096 no other function calls and the function is non-reentrant and the memory
11098 \begin_inset LatexCommand \index{Memory model}
11105 If an explicit storage class
11106 \begin_inset LatexCommand \index{Storage class}
11110 is specified for a local variable, it will NOT be overlayed.
11113 Note that the compiler (not the linkage editor) makes the decision for overlayin
11115 Functions that are called from an interrupt service routine
11116 \begin_inset Marginal
11126 should be preceded by a #pragma\SpecialChar ~
11128 \begin_inset LatexCommand \index{\#pragma nooverlay}
11132 if they are not reentrant.
11135 Also note that the compiler does not do any processing of inline assembler
11136 code, so the compiler might incorrectly assign local variables and parameters
11137 of a function into the overlay segment if the inline assembler code calls
11138 other c-functions that might use the overlay.
11139 In that case the #pragma\SpecialChar ~
11140 nooverlay should be used.
11143 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11145 \begin_inset LatexCommand \index{Multiplication}
11150 \begin_inset LatexCommand \index{Division}
11154 will NOT be overlayed since these are implemented using external functions,
11163 \begin_inset LatexCommand \index{\#pragma nooverlay}
11169 void set_error(unsigned char errcd)
11185 void some_isr () __interrupt
11186 \begin_inset LatexCommand \index{interrupt}
11216 In the above example the parameter
11224 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11226 not present, this could cause unpredictable runtime behavior when called
11227 from an interrupt service routine.
11228 The #pragma\SpecialChar ~
11229 nooverlay ensures that the parameters and local variables for
11230 the function are NOT overlayed.
11233 Interrupt Service Routines
11234 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11241 General Information
11256 outines to be coded in C, with some extended keywords.
11261 void timer_isr (void) __interrupt (1) __using (1)
11275 The optional number following the
11278 \begin_inset LatexCommand \index{interrupt}
11283 \begin_inset LatexCommand \index{\_\_interrupt}
11289 keyword is the interrupt number this routine will service.
11290 When present, the compiler will insert a call to this routine in the interrupt
11291 vector table for the interrupt number specified.
11292 If you have multiple source files in your project, interrupt service routines
11293 can be present in any of them, but a prototype of the isr MUST be present
11294 or included in the file that contains the function
11302 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11307 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11313 keyword can be used to tell the compiler to use the specified register
11314 bank (8051 specific) when generating code for this function.
11320 Interrupt service routines open the door for some very interesting bugs:
11322 If an interrupt service routine changes variables which are accessed by
11323 other functions these variables have to be declared
11328 \begin_inset LatexCommand \index{volatile}
11336 If the access to these variables is not
11339 \begin_inset LatexCommand \index{atomic}
11346 the processor needs more than one instruction for the access and could
11347 be interrupted while accessing the variable) the interrupt must be disabled
11348 during the access to avoid inconsistent data.
11349 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11350 and should be protected by disabling interrupts.
11351 You're not automatically on the safe side if you use 8 bit variables though.
11352 We need an example here: f.e.
11353 on the 8051 the harmless looking
11354 \begin_inset Quotes srd
11359 flags\SpecialChar ~
11364 \begin_inset Quotes sld
11373 \begin_inset Quotes srd
11378 flags\SpecialChar ~
11383 \begin_inset Quotes sld
11386 from within an interrupt routine might get lost if the interrupt occurs
11389 \begin_inset Quotes sld
11394 counter\SpecialChar ~
11399 \begin_inset Quotes srd
11402 is not atomic on the 8051 even if
11406 is located in data memory.
11407 Bugs like these are hard to reproduce and can cause a lot of trouble.
11411 The return address and the registers used in the interrupt service routine
11412 are saved on the stack
11413 \begin_inset LatexCommand \index{stack}
11417 so there must be sufficient stack space.
11418 If there isn't variables or registers (or even the return address itself)
11425 \begin_inset LatexCommand \index{stack overflow}
11429 is most likely to happen if the interrupt occurs during the
11430 \begin_inset Quotes sld
11434 \begin_inset Quotes srd
11437 subroutine when the stack is already in use for f.e.
11438 many return addresses.
11441 A special note here, int (16 bit) and long (32 bit) integer division
11442 \begin_inset LatexCommand \index{Division}
11447 \begin_inset LatexCommand \index{Multiplication}
11452 \begin_inset LatexCommand \index{Modulus}
11457 \begin_inset LatexCommand \index{Floating point support}
11461 operations are implemented using external support routines developed in
11463 If an interrupt service routine needs to do any of these operations then
11464 the support routines (as mentioned in a following section) will have to
11465 be recompiled using the
11478 \begin_inset LatexCommand \index{-\/-stack-auto}
11484 option and the source file will need to be compiled using the
11499 \begin_inset LatexCommand \index{-\/-int-long-reent}
11504 Note, the type promotion
11505 \begin_inset LatexCommand \index{type promotion}
11509 required by ANSI C can cause 16 bit routines to be used without the programmer
11514 Calling other functions from an interrupt service routine is not recommended,
11515 avoid it if possible.
11516 Note that when some function is called from an interrupt service routine
11517 it should be preceded by a #pragma\SpecialChar ~
11519 \begin_inset LatexCommand \index{\#pragma nooverlay}
11523 if it is not reentrant.
11524 Furthermore nonreentrant functions should not be called from the main program
11525 while the interrupt service routine might be active.
11526 They also must not be called from low priority interrupt service routines
11527 while a high priority interrupt service routine might be active.
11528 You could use semaphores or make the function
11532 if all parameters are passed in registers.
11537 \begin_inset LatexCommand \ref{sub:Overlaying}
11542 about Overlaying and section
11543 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11548 about Functions using private register banks.
11551 MCS51/DS390 Interrupt Service Routines
11554 Interrupt numbers and the corresponding address & descriptions for the Standard
11555 8051/8052 are listed below.
11556 SDCC will automatically adjust the interrupt vector table to the maximum
11557 interrupt number specified.
11563 \begin_inset Tabular
11564 <lyxtabular version="3" rows="9" columns="3">
11566 <column alignment="center" valignment="top" leftline="true" width="0in">
11567 <column alignment="left" valignment="top" leftline="true" width="0in">
11568 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11569 <row topline="true" bottomline="true">
11570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11578 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11595 <row topline="true">
11596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11612 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11621 <row topline="true">
11622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11647 <row topline="true">
11648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11656 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11664 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11673 <row topline="true">
11674 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11682 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11690 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11699 <row topline="true">
11700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11708 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11725 <row topline="true">
11726 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11751 <row topline="true">
11752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11776 <row topline="true" bottomline="true">
11777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11810 If the interrupt service routine is defined without
11813 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11818 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11824 a register bank or with register bank 0 (
11828 0), the compiler will save the registers used by itself on the stack upon
11829 entry and restore them at exit, however if such an interrupt service routine
11830 calls another function then the entire register bank will be saved on the
11832 This scheme may be advantageous for small interrupt service routines which
11833 have low register usage.
11836 If the interrupt service routine is defined to be using a specific register
11841 & psw are saved and restored, if such an interrupt service routine calls
11842 another function (using another register bank) then the entire register
11843 bank of the called function will be saved on the stack.
11844 This scheme is recommended for larger interrupt service routines.
11847 HC08 Interrupt Service Routines
11850 Since the number of interrupts available is chip specific and the interrupt
11851 vector table always ends at the last byte of memory, the interrupt numbers
11852 corresponds to the interrupt vectors in reverse order of address.
11853 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11854 2 will use the interrupt vector at 0xfffa, and so on.
11855 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11856 this way; instead see section
11857 \begin_inset LatexCommand \ref{sub:Startup-Code}
11861 for details on customizing startup.
11864 Z80 Interrupt Service Routines
11867 The Z80 uses several different methods for determining the correct interrupt
11868 vector depending on the hardware implementation.
11869 Therefore, SDCC ignores the optional interrupt number and does not attempt
11870 to generate an interrupt vector table.
11873 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11874 instruction to return from the interrupt.
11875 To write an interrupt handler for the non-maskable interrupt, which needs
11876 a RETN instruction instead, add the
11885 void nmi_isr (void) critical interrupt
11899 However if you need to create a non-interruptable interrupt service routine
11900 you would also require the
11905 To distinguish between this and an nmi_isr you must provide an interrupt
11909 Enabling and Disabling Interrupts
11912 Critical Functions and Critical Statements
11915 A special keyword may be associated with a block or a function declaring
11921 SDCC will generate code to disable all interrupts
11922 \begin_inset LatexCommand \index{interrupt}
11926 upon entry to a critical function and restore the interrupt enable to the
11927 previous state before returning.
11928 Nesting critical functions will need one additional byte on the stack
11929 \begin_inset LatexCommand \index{stack}
11938 int foo () __critical
11939 \begin_inset LatexCommand \index{critical}
11944 \begin_inset LatexCommand \index{\_\_critical}
11969 The critical attribute maybe used with other attributes like
11979 may also be used to disable interrupts more locally:
11987 More than one statement could have been included in the block.
11990 Enabling and Disabling Interrupts directly
11994 \begin_inset LatexCommand \index{interrupt}
11998 can also be disabled and enabled directly (8051):
12003 EA = 0;\SpecialChar ~
12066 EA = 1;\SpecialChar ~
12133 On other architectures which have seperate opcodes for enabling and disabling
12134 interrupts you might want to make use of defines with inline assembly
12135 \begin_inset LatexCommand \index{Assembler routines}
12145 \begin_inset LatexCommand \index{\_asm}
12154 \begin_inset LatexCommand \index{\_endasm}
12163 #define SEI _asm\SpecialChar ~
12175 Note: it is sometimes sufficient to disable only a specific interrupt source
12177 a timer or serial interrupt by manipulating an
12180 \begin_inset LatexCommand \index{interrupt mask}
12190 Usually the time during which interrupts are disabled should be kept as
12192 This minimizes both
12197 \begin_inset LatexCommand \index{interrupt latency}
12201 (the time between the occurrence of the interrupt and the execution of
12202 the first code in the interrupt routine) and
12207 \begin_inset LatexCommand \index{interrupt jitter}
12211 (the difference between the shortest and the longest interrupt latency).
12212 These really are something different, f.e.
12213 a serial interrupt has to be served before its buffer overruns so it cares
12214 for the maximum interrupt latency, whereas it does not care about jitter.
12215 On a loudspeaker driven via a digital to analog converter which is fed
12216 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12217 a much smaller jitter will be very audible.
12220 You can reenable interrupts within an interrupt routine and on some architecture
12221 s you can make use of two (or more) levels of
12223 interrupt priorities
12226 \begin_inset LatexCommand \index{interrupt priority}
12231 On some architectures which don't support interrupt priorities these can
12232 be implemented by manipulating the interrupt mask and reenabling interrupts
12233 within the interrupt routine.
12234 Check there is sufficient space on the stack
12235 \begin_inset LatexCommand \index{stack}
12239 and don't add complexity unless you have to.
12244 \begin_inset LatexCommand \index{semaphore}
12248 locking (mcs51/ds390)
12251 Some architectures (mcs51/ds390) have an atomic
12252 \begin_inset LatexCommand \index{atomic}
12265 These type of instructions are typically used in preemptive multitasking
12266 systems, where a routine f.e.
12267 claims the use of a data structure ('acquires a lock
12268 \begin_inset LatexCommand \index{lock}
12272 on it'), makes some modifications and then releases the lock when the data
12273 structure is consistent again.
12274 The instruction may also be used if interrupt and non-interrupt code have
12275 to compete for a resource.
12276 With the atomic bit test and clear instruction interrupts
12277 \begin_inset LatexCommand \index{interrupt}
12281 don't have to be disabled for the locking operation.
12285 SDCC generates this instruction if the source follows this pattern:
12291 \begin_inset LatexCommand \index{volatile}
12295 bit resource_is_free;
12299 if (resource_is_free)
12309 resource_is_free=0;
12322 resource_is_free=1;
12329 Note, mcs51 and ds390 support only an atomic
12330 \begin_inset LatexCommand \index{atomic}
12338 instruction (as opposed to atomic bit test and
12343 Functions using private register banks
12344 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12351 Some architectures have support for quickly changing register sets.
12352 SDCC supports this feature with the
12355 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12360 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12366 attribute (which tells the compiler to use a register bank
12367 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12371 other than the default bank zero).
12372 It should only be applied to
12375 \begin_inset LatexCommand \index{interrupt}
12381 functions (see footnote below).
12382 This will in most circumstances make the generated ISR code more efficient
12383 since it will not have to save registers on the stack.
12390 attribute will have no effect on the generated code for a
12394 function (but may occasionally be useful anyway
12400 possible exception: if a function is called ONLY from 'interrupt' functions
12401 using a particular bank, it can be declared with the same 'using' attribute
12402 as the calling 'interrupt' functions.
12403 For instance, if you have several ISRs using bank one, and all of them
12404 call memcpy(), it might make sense to create a specialized version of memcpy()
12405 'using 1', since this would prevent the ISR from having to save bank zero
12406 to the stack on entry and switch to bank zero before calling the function
12413 (pending: I don't think this has been done yet)
12420 function using a non-zero bank will assume that it can trash that register
12421 bank, and will not save it.
12422 Since high-priority interrupts
12423 \begin_inset LatexCommand \index{interrupts}
12428 \begin_inset LatexCommand \index{interrupt priority}
12432 can interrupt low-priority ones on the 8051 and friends, this means that
12433 if a high-priority ISR
12437 a particular bank occurs while processing a low-priority ISR
12441 the same bank, terrible and bad things can happen.
12442 To prevent this, no single register bank should be
12446 by both a high priority and a low priority ISR.
12447 This is probably most easily done by having all high priority ISRs use
12448 one bank and all low priority ISRs use another.
12449 If you have an ISR which can change priority at runtime, you're on your
12450 own: I suggest using the default bank zero and taking the small performance
12454 It is most efficient if your ISR calls no other functions.
12455 If your ISR must call other functions, it is most efficient if those functions
12456 use the same bank as the ISR (see note 1 below); the next best is if the
12457 called functions use bank zero.
12458 It is very inefficient to call a function using a different, non-zero bank
12464 \begin_inset LatexCommand \label{sub:Startup-Code}
12469 \begin_inset LatexCommand \index{Startup code}
12476 MCS51/DS390 Startup Code
12479 The compiler inserts a call to the C routine
12481 _sdcc_external_startup()
12482 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12491 at the start of the CODE area.
12492 This routine is in the runtime library
12493 \begin_inset LatexCommand \index{Runtime library}
12498 By default this routine returns 0, if this routine returns a non-zero value,
12499 the static & global variable initialization will be skipped and the function
12500 main will be invoked.
12501 Otherwise static & global variables will be initialized before the function
12505 _sdcc_external_startup()
12507 routine to your program to override the default if you need to setup hardware
12508 or perform some other critical operation prior to static & global variable
12510 \begin_inset LatexCommand \index{Variable initialization}
12515 On some mcs51 variants xdata
12516 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12520 memory has to be explicitly enabled before it can be accessed or if the
12521 watchdog needs to be disabled, this is the place to do it.
12522 The startup code clears all internal data memory, 256 bytes by default,
12523 but from 0 to n-1 if
12536 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12543 (recommended for Chipcon CC1010).
12546 See also the compiler option
12565 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12570 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12575 about MCS51-variants.
12581 The HC08 startup code follows the same scheme as the MCS51 startup code.
12587 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12588 from sdcc/device/lib/z80/crt0.s.
12589 If you need a different startup code you can use the compiler option
12610 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12614 and provide your own crt0.o.
12618 Inline Assembler Code
12619 \begin_inset LatexCommand \index{Assembler routines}
12626 A Step by Step Introduction
12627 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12634 Starting from a small snippet of c-code this example shows for the MCS51
12635 how to use inline assembly, access variables, a function parameter and
12636 an array in xdata memory.
12637 The example uses an MCS51 here but is easily adapted for other architectures.
12638 This is a buffer routine which should be optimized:
12644 unsigned char __far
12645 \begin_inset LatexCommand \index{far (storage class)}
12650 \begin_inset LatexCommand \index{\_\_far (storage class)}
12655 \begin_inset LatexCommand \index{at}
12660 \begin_inset LatexCommand \index{\_\_at}
12664 (0x7f00) buf[0x100];
12665 \begin_inset LatexCommand \index{Aligned array}
12671 unsigned char head, tail;
12675 void to_buffer( unsigned char c )
12683 if( head != (unsigned char)(tail-1) )\SpecialChar ~
12689 \begin_inset LatexCommand \index{promotion to signed int}
12694 \begin_inset LatexCommand \index{type promotion}
12699 \begin_inset Marginal
12720 buf[ head++ ] = c;\SpecialChar ~
12736 /* access to a 256 byte aligned array */
12741 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12742 then a corresponding buffer.asm file is generated.
12743 We define a new function
12747 in file buffer.c in which we cut and paste the generated code, removing
12748 unwanted comments and some ':'.
12750 \begin_inset Quotes sld
12754 \begin_inset Quotes srd
12758 \begin_inset Quotes sld
12762 \begin_inset Quotes srd
12765 to the beginning and the end of the function body:
12771 /* With a cut and paste from the .asm file, we have something to start with.
12776 The function is not yet OK! (registers aren't saved) */
12778 void to_buffer_asm( unsigned char c )
12787 \begin_inset LatexCommand \index{\_asm}
12792 \begin_inset LatexCommand \index{\_\_asm}
12806 ;buffer.c if( head != (unsigned char)(tail-1) )
12854 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12855 \begin_inset LatexCommand \index{Aligned array}
12920 \begin_inset LatexCommand \index{\_endasm}
12925 \begin_inset LatexCommand \index{\_\_endasm}
12934 The new file buffer.c should compile with only one warning about the unreferenced
12935 function argument 'c'.
12936 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12937 (1) and finally have:
12943 unsigned char __far __at(0x7f00) buf[0x100];
12945 unsigned char head, tail;
12947 #define USE_ASSEMBLY (1)
12955 void to_buffer( unsigned char c )
12963 if( head != (unsigned char)(tail-1) )
12983 void to_buffer( unsigned char c )
12991 c; // to avoid warning: unreferenced function argument
12998 \begin_inset LatexCommand \index{\_asm}
13003 \begin_inset LatexCommand \index{\_\_asm}
13017 ; save used registers here.
13028 ; If we were still using r2,r3 we would have to push them here.
13031 ; if( head != (unsigned char)(tail-1) )
13074 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13098 ; buf[ head++ ] = c;
13109 a,dpl \SpecialChar ~
13116 ; dpl holds lower byte of function argument
13127 dpl,_head \SpecialChar ~
13130 ; buf is 0x100 byte aligned so head can be used directly
13172 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13184 ; restore used registers here
13191 \begin_inset LatexCommand \index{\_endasm}
13196 \begin_inset LatexCommand \index{\_\_endasm}
13207 The inline assembler code can contain any valid code understood by the assembler
13208 , this includes any assembler directives and comment lines
13214 The assembler does not like some characters like ':' or ''' in comments.
13215 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13216 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13221 \begin_inset LatexCommand \index{Assembler documentation}
13229 The compiler does not do any validation of the code within the
13232 \begin_inset LatexCommand \index{\_asm}
13237 \begin_inset LatexCommand \index{\_\_asm}
13245 \begin_inset LatexCommand \index{\_endasm}
13250 \begin_inset LatexCommand \index{\_\_endasm}
13259 Specifically it will not know which registers are used and thus register
13261 \begin_inset LatexCommand \index{push/pop}
13265 has to be done manually.
13269 It is recommended that each assembly instruction (including labels) be placed
13270 in a separate line (as the example shows).
13284 \begin_inset LatexCommand \index{-\/-peep-asm}
13290 command line option is used, the inline assembler code will be passed through
13291 the peephole optimizer
13292 \begin_inset LatexCommand \index{Peephole optimizer}
13297 There are only a few (if any) cases where this option makes sense, it might
13298 cause some unexpected changes in the inline assembler code.
13299 Please go through the peephole optimizer rules defined in file
13303 before using this option.
13307 \begin_inset LatexCommand \label{sub:Naked-Functions}
13312 \begin_inset LatexCommand \index{Naked functions}
13319 A special keyword may be associated with a function declaring it as
13322 \begin_inset LatexCommand \index{\_naked}
13327 \begin_inset LatexCommand \index{\_\_naked}
13338 function modifier attribute prevents the compiler from generating prologue
13339 \begin_inset LatexCommand \index{function prologue}
13344 \begin_inset LatexCommand \index{function epilogue}
13348 code for that function.
13349 This means that the user is entirely responsible for such things as saving
13350 any registers that may need to be preserved, selecting the proper register
13351 bank, generating the
13355 instruction at the end, etc.
13356 Practically, this means that the contents of the function must be written
13357 in inline assembler.
13358 This is particularly useful for interrupt functions, which can have a large
13359 (and often unnecessary) prologue/epilogue.
13360 For example, compare the code generated by these two functions:
13366 \begin_inset LatexCommand \index{volatile}
13370 data unsigned char counter;
13374 void simpleInterrupt(void) __interrupt
13375 \begin_inset LatexCommand \index{interrupt}
13380 \begin_inset LatexCommand \index{\_\_interrupt}
13398 void nakedInterrupt(void) __interrupt (2) __naked
13407 \begin_inset LatexCommand \index{\_asm}
13412 \begin_inset LatexCommand \index{\_\_asm}
13429 _counter ; does not change flags, no need to save psw
13441 ; MUST explicitly include ret or reti in _naked function.
13448 \begin_inset LatexCommand \index{\_endasm}
13453 \begin_inset LatexCommand \index{\_\_endasm}
13462 For an 8051 target, the generated simpleInterrupt looks like:
13471 example, recent versions of SDCC generate
13473 the same code for simpleInterrupt() and nakedInterrupt()!
13613 whereas nakedInterrupt looks like:
13628 _counter ; does not change flags, no need to save psw
13646 ; MUST explicitly include ret or reti in _naked function
13649 The related directive #pragma exclude
13650 \begin_inset LatexCommand \index{\#pragma exclude}
13654 allows a more fine grained control over pushing & popping
13655 \begin_inset LatexCommand \index{push/pop}
13662 While there is nothing preventing you from writing C code inside a
13666 function, there are many ways to shoot yourself in the foot doing this,
13667 and it is recommended that you stick to inline assembler.
13670 Use of Labels within Inline Assembler
13673 SDCC allows the use of in-line assembler with a few restrictions regarding
13675 In older versions of the compiler all labels defined within inline assembler
13684 where nnnn is a number less than 100 (which implies a limit of utmost 100
13685 inline assembler labels
13699 \begin_inset LatexCommand \index{\_asm}
13704 \begin_inset LatexCommand \index{\_\_asm}
13734 \begin_inset LatexCommand \index{\_endasm}
13739 \begin_inset LatexCommand \index{\_\_endasm}
13746 Inline assembler code cannot reference any C-Labels, however it can reference
13748 \begin_inset LatexCommand \index{Labels}
13752 defined by the inline assembler, e.g.:
13777 ; some assembler code
13797 /* some more c code */
13799 clabel:\SpecialChar ~
13801 /* inline assembler cannot reference this label */
13813 $0003: ;label (can be referenced by inline assembler only)
13820 \begin_inset LatexCommand \index{\_endasm}
13825 \begin_inset LatexCommand \index{\_\_endasm}
13835 /* some more c code */
13840 In other words inline assembly code can access labels defined in inline
13841 assembly within the scope of the function.
13842 The same goes the other way, i.e.
13843 labels defines in inline assembly can not be accessed by C statements.
13846 Interfacing with Assembler Code
13847 \begin_inset LatexCommand \index{Assembler routines}
13854 Global Registers used for Parameter Passing
13855 \begin_inset LatexCommand \index{Parameter passing}
13862 The compiler always uses the global registers
13865 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13870 \begin_inset LatexCommand \index{DPTR}
13875 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13884 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13890 to pass the first parameter to a routine.
13891 The second parameter onwards is either allocated on the stack (for reentrant
13902 -stack-auto is used) or in data / xdata memory (depending on the memory
13907 Assembler Routine (non-reentrant)
13910 In the following example
13911 \begin_inset LatexCommand \index{reentrant}
13916 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13920 the function c_func calls an assembler routine asm_func, which takes two
13922 \begin_inset LatexCommand \index{function parameter}
13931 extern int asm_func(unsigned char, unsigned char);
13935 int c_func (unsigned char i, unsigned char j)
13943 return asm_func(i,j);
13957 return c_func(10,9);
13962 The corresponding assembler function is:
13967 .globl _asm_func_PARM_2
14068 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14085 Note here that the return values
14086 \begin_inset LatexCommand \index{return value}
14090 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14092 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14093 b' & 'acc' for four byte values.
14096 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14097 the parameter number starting from 1, and counting from the left.
14098 The first parameter is passed in
14099 \begin_inset Quotes eld
14103 \begin_inset Quotes erd
14106 for a one byte parameter,
14107 \begin_inset Quotes eld
14111 \begin_inset Quotes erd
14115 \begin_inset Quotes eld
14119 \begin_inset Quotes erd
14122 for three bytes and
14123 \begin_inset Quotes eld
14127 \begin_inset Quotes erd
14130 for a four bytes parameter.
14131 The variable name for the second parameter will be _<function_name>_PARM_2.
14135 Assemble the assembler routine with the following command:
14142 asx8051 -losg asmfunc.asm
14149 Then compile and link the assembler routine to the C source file with the
14157 sdcc cfunc.c asmfunc.rel
14160 Assembler Routine (reentrant)
14164 \begin_inset LatexCommand \index{reentrant}
14169 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14173 the second parameter
14174 \begin_inset LatexCommand \index{function parameter}
14178 onwards will be passed on the stack, the parameters are pushed from right
14180 after the call the leftmost parameter will be on the top of the stack.
14181 Here is an example:
14186 extern int asm_func(unsigned char, unsigned char);
14190 int c_func (unsigned char i, unsigned char j) reentrant
14198 return asm_func(i,j);
14212 return c_func(10,9);
14217 The corresponding assembler routine is:
14317 The compiling and linking procedure remains the same, however note the extra
14318 entry & exit linkage required for the assembler code, _bp is the stack
14319 frame pointer and is used to compute the offset into the stack for parameters
14320 and local variables.
14324 \begin_inset LatexCommand \index{int (16 bit)}
14329 \begin_inset LatexCommand \index{long (32 bit)}
14336 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14337 multiplication and modulus operations are implemented by support routines.
14338 These support routines are all developed in ANSI-C to facilitate porting
14339 to other MCUs, although some model specific assembler optimizations are
14341 The following files contain the described routines, all of them can be
14342 found in <installdir>/share/sdcc/lib.
14348 \begin_inset Tabular
14349 <lyxtabular version="3" rows="11" columns="2">
14351 <column alignment="left" valignment="top" leftline="true" width="0">
14352 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14353 <row topline="true" bottomline="true">
14354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14375 <row topline="true">
14376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14389 16 bit multiplication
14393 <row topline="true">
14394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14402 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14407 signed 16 bit division (calls _divuint)
14411 <row topline="true">
14412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14420 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14425 unsigned 16 bit division
14429 <row topline="true">
14430 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14443 signed 16 bit modulus (calls _moduint)
14447 <row topline="true">
14448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14456 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14461 unsigned 16 bit modulus
14465 <row topline="true">
14466 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14479 32 bit multiplication
14483 <row topline="true">
14484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14497 signed 32 division (calls _divulong)
14501 <row topline="true">
14502 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14510 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14515 unsigned 32 division
14519 <row topline="true">
14520 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14528 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14533 signed 32 bit modulus (calls _modulong)
14537 <row topline="true" bottomline="true">
14538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14551 unsigned 32 bit modulus
14564 Since they are compiled as
14569 \begin_inset LatexCommand \index{reentrant}
14574 \begin_inset LatexCommand \index{interrupt}
14578 service routines should not do any of the above operations.
14579 If this is unavoidable then the above routines will need to be compiled
14593 \begin_inset LatexCommand \index{-\/-stack-auto}
14599 option, after which the source program will have to be compiled with
14612 \begin_inset LatexCommand \index{-\/-int-long-reent}
14619 Notice that you don't have to call these routines directly.
14620 The compiler will use them automatically every time an integer operation
14624 Floating Point Support
14625 \begin_inset LatexCommand \index{Floating point support}
14632 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14633 floating point support routines are derived from gcc's floatlib.c and consist
14634 of the following routines:
14642 \begin_inset Tabular
14643 <lyxtabular version="3" rows="17" columns="2">
14645 <column alignment="left" valignment="top" leftline="true" width="0">
14646 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14647 <row topline="true" bottomline="true">
14648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14674 <row topline="true">
14675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14706 add floating point numbers
14710 <row topline="true">
14711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14742 subtract floating point numbers
14746 <row topline="true">
14747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14764 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14778 divide floating point numbers
14782 <row topline="true">
14783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14814 multiply floating point numbers
14818 <row topline="true">
14819 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14850 convert floating point to unsigned char
14854 <row topline="true">
14855 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14886 convert floating point to signed char
14890 <row topline="true">
14891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14922 convert floating point to unsigned int
14926 <row topline="true">
14927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14958 convert floating point to signed int
14962 <row topline="true">
14963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14989 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15003 convert floating point to unsigned long
15007 <row topline="true">
15008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15039 convert floating point to signed long
15043 <row topline="true">
15044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15075 convert unsigned char to floating point
15079 <row topline="true">
15080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15097 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15111 convert char to floating point number
15115 <row topline="true">
15116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15147 convert unsigned int to floating point
15151 <row topline="true">
15152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15169 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15183 convert int to floating point numbers
15187 <row topline="true">
15188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15219 convert unsigned long to floating point number
15223 <row topline="true" bottomline="true">
15224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15255 convert long to floating point number
15268 These support routines are developed in ANSI-C so there is room for space
15269 and speed improvement
15275 These floating point routines (
15279 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15284 Note if all these routines are used simultaneously the data space might
15286 For serious floating point usage the large model might be needed.
15287 Also notice that you don't have to call this routines directly.
15288 The compiler will use them automatically every time a floating point operation
15293 \begin_inset LatexCommand \index{Libraries}
15302 <pending: this is messy and incomplete - a little more information is in
15303 sdcc/doc/libdoc.txt
15308 Compiler support routines (_gptrget, _mulint etc.)
15311 Stdclib functions (puts, printf, strcat etc.)
15312 \layout Subsubsection
15318 \begin_inset LatexCommand \index{<stdio.h>}
15322 As usual on embedded systems you have to provide your own
15325 \begin_inset LatexCommand \index{getchar()}
15334 \begin_inset LatexCommand \index{putchar()}
15341 SDCC does not know whether the system connects to a serial line with or
15342 without handshake, LCD, keyboard or other device.
15356 You'll find examples for serial routines f.e.
15357 in sdcc/device/lib.
15363 \begin_inset LatexCommand \index{printf()}
15373 does not support float (except on ds390).
15374 To enable this recompile it with the option
15387 \begin_inset LatexCommand \index{USE\_FLOATS}
15393 on the command line.
15407 \begin_inset LatexCommand \index{-\/-model-large}
15413 for the mcs51 port, since this uses a lot of memory.
15416 If you're short on memory you might want to use
15419 \begin_inset LatexCommand \index{printf\_small()}
15434 For the mcs51 there additionally are assembly versions
15437 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15443 (subset of printf using less than 270 bytes) and
15446 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15455 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15461 (floating-point aware version of printf_fast) which should fit the requirements
15462 of many embedded systems (printf_fast() can be customized by unsetting
15467 support long variables and field widths).
15468 \layout Subsubsection
15471 \begin_inset LatexCommand \index{malloc.h}
15478 Before using dynamic
15479 \begin_inset LatexCommand \index{dynamic}
15483 memory allocation with SDCC, you have to provide heap
15484 \begin_inset LatexCommand \index{heap}
15488 space for malloc to allocate memory from
15493 You can acomplish this by including the following code into your source:
15498 #include <malloc.h>
15499 \begin_inset LatexCommand \index{malloc.h}
15504 \begin_inset LatexCommand \index{calloc}
15509 \begin_inset LatexCommand \index{malloc}
15514 \begin_inset LatexCommand \index{realloc}
15519 \begin_inset LatexCommand \index{free}
15527 #define HEAPSIZE 0x1000 /* Adjust depending on available memory */
15529 unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory
15541 /* Your variable declarations come here*/
15551 init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE);
15557 /* Rest of your code*/
15564 Math functions (sinf, powf, sqrtf etc.)
15565 \layout Subsubsection
15570 See definitions in file <math.h>.
15577 \begin_inset LatexCommand \index{Libraries}
15581 included in SDCC should have a license at least as liberal as the GNU Lesser
15582 General Public License
15583 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15594 license statements for the libraries are missing.
15595 sdcc/device/lib/ser_ir.c
15599 come with a GPL (as opposed to LGPL) License - this will not be liberal
15600 enough for many embedded programmers.
15603 If you have ported some library or want to share experience about some code
15605 falls into any of these categories Busses (I
15606 \begin_inset Formula $^{\textrm{2}}$
15609 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15610 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15611 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15612 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15617 would certainly like to hear about it.
15618 Programmers coding for embedded systems are not especially famous for being
15619 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15620 e these references are very valuable.
15621 Let's help to create a climate where information is shared.
15627 MCS51 Memory Models
15628 \begin_inset LatexCommand \index{Memory model}
15633 \begin_inset LatexCommand \index{MCS51 memory model}
15638 \layout Subsubsection
15640 Small, Medium and Large
15643 SDCC allows three memory models for MCS51 code,
15652 Modules compiled with different memory models should
15656 be combined together or the results would be unpredictable.
15657 The library routines supplied with the compiler are compiled as small,
15659 The compiled library modules are contained in separate directories as small,
15660 medium and large so that you can link to the appropriate set.
15663 When the medium or large model is used all variables declared without a
15664 storage class will be allocated into the external ram, this includes all
15665 parameters and local variables (for non-reentrant
15666 \begin_inset LatexCommand \index{reentrant}
15671 When the small model is used variables without storage class are allocated
15672 in the internal ram.
15675 Judicious usage of the processor specific storage classes
15676 \begin_inset LatexCommand \index{Storage class}
15680 and the 'reentrant' function type will yield much more efficient code,
15681 than using the large model.
15682 Several optimizations are disabled when the program is compiled using the
15683 large model, it is therefore recommended that the small model be used unless
15684 absolutely required.
15685 \layout Subsubsection
15688 \begin_inset LatexCommand \label{sub:External-Stack}
15693 \begin_inset LatexCommand \index{stack}
15698 \begin_inset LatexCommand \index{External stack (mcs51)}
15705 The external stack (-
15716 \begin_inset LatexCommand \index{-\/-xstack}
15720 ) is located in pdata
15721 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15725 memory (usually at the start of the external ram segment) and uses all
15726 unused space in pdata (max.
15738 -xstack option is used to compile the program, the parameters and local
15740 \begin_inset LatexCommand \index{local variables}
15744 of all reentrant functions are allocated in this area.
15745 This option is provided for programs with large stack space requirements.
15746 When used with the -
15757 \begin_inset LatexCommand \index{-\/-stack-auto}
15761 option, all parameters and local variables are allocated on the external
15762 stack (note: support libraries will need to be recompiled with the same
15764 There is a predefined target in the library makefile).
15767 The compiler outputs the higher order address byte of the external ram segment
15769 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15774 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15778 ), therefore when using the External Stack option, this port
15782 be used by the application program.
15786 \begin_inset LatexCommand \index{Memory model}
15791 \begin_inset LatexCommand \index{DS390 memory model}
15798 The only model supported is Flat 24
15799 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15804 This generates code for the 24 bit contiguous addressing mode of the Dallas
15806 In this mode, up to four meg of external RAM or code space can be directly
15808 See the data sheets at www.dalsemi.com for further information on this part.
15812 Note that the compiler does not generate any code to place the processor
15813 into 24 bitmode (although
15817 in the ds390 libraries will do that for you).
15823 \begin_inset LatexCommand \index{Tinibios (DS390)}
15827 , the boot loader or similar code must ensure that the processor is in 24
15828 bit contiguous addressing mode before calling the SDCC startup code.
15846 option, variables will by default be placed into the XDATA segment.
15851 Segments may be placed anywhere in the 4 meg address space using the usual
15863 Note that if any segments are located above 64K, the -r flag must be passed
15864 to the linker to generate the proper segment relocations, and the Intel
15865 HEX output format must be used.
15866 The -r flag can be passed to the linker by using the option
15870 on the SDCC command line.
15871 However, currently the linker can not handle code segments > 64k.
15875 \begin_inset LatexCommand \index{Pragmas}
15882 SDCC supports the following #pragma directives:
15886 \begin_inset LatexCommand \index{\#pragma save}
15890 - this will save all current options to the save/restore stack.
15891 See #pragma\SpecialChar ~
15896 \begin_inset LatexCommand \index{\#pragma restore}
15900 - will restore saved options from the last save.
15901 saves & restores can be nested.
15902 SDCC uses a save/restore stack: save pushes current options to the stack,
15903 restore pulls current options from the stack.
15904 See #pragma\SpecialChar ~
15911 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15916 \begin_inset LatexCommand \index{function prologue}
15920 function1[,function2[,function3...]] - The compiler by default uses a caller
15921 saves convention for register saving across function calls, however this
15922 can cause unnecessary register pushing & popping
15923 \begin_inset LatexCommand \index{push/pop}
15927 when calling small functions from larger functions.
15928 This option can be used to switch off the register saving convention for
15929 the function names specified.
15930 The compiler will not save registers when calling these functions, extra
15931 code need to be manually inserted at the entry & exit for these functions
15932 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15933 reduce code & improve run time performance of the generated code.
15934 In the future the compiler (with inter procedural analysis) may be able
15935 to determine the appropriate scheme to use for each function call.
15946 -callee-saves command line option is used, the function names specified
15947 in #pragma\SpecialChar ~
15949 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15953 is appended to the list of functions specified in the command line.
15957 \begin_inset LatexCommand \index{\#pragma exclude}
15961 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15962 of pairs of push/pop
15963 \begin_inset LatexCommand \index{push/pop}
15972 \begin_inset LatexCommand \index{interrupt}
15985 The directive should be placed immediately before the ISR function definition
15986 and it affects ALL ISR functions following it.
15987 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15988 exclude\SpecialChar ~
15990 \begin_inset LatexCommand \index{\#pragma exclude}
15995 See also the related keyword _naked
15996 \begin_inset LatexCommand \index{\_naked}
16001 \begin_inset LatexCommand \index{\_\_naked}
16009 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
16013 - the compiler will not warn you anymore for obvious mistakes, you'r on
16017 disable_warning <nnnn>
16018 \begin_inset LatexCommand \index{\#pragma disable\_warning}
16022 - the compiler will not warn you anymore about warning number <nnnn>.
16026 \begin_inset LatexCommand \index{\#pragma nogcse}
16030 - will stop global common subexpression elimination.
16034 \begin_inset LatexCommand \index{\#pragma noinduction}
16038 - will stop loop induction optimizations.
16042 \begin_inset LatexCommand \index{\#pragma noinvariant}
16046 - will not do loop invariant optimizations.
16047 For more details see Loop Invariants in section
16048 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
16056 \begin_inset LatexCommand \index{\#pragma noiv}
16060 - Do not generate interrupt
16061 \begin_inset LatexCommand \index{interrupt}
16065 vector table entries for all ISR functions defined after the pragma.
16066 This is useful in cases where the interrupt vector table must be defined
16067 manually, or when there is a secondary, manually defined interrupt vector
16069 for the autovector feature of the Cypress EZ-USB FX2).
16070 More elegantly this can be achieved by obmitting the optional interrupt
16071 number after the interrupt keyword, see section
16072 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
16081 \begin_inset LatexCommand \index{\#pragma nojtbound}
16085 - will not generate code for boundary value checking, when switch statements
16086 are turned into jump-tables (dangerous).
16087 For more details see section
16088 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16096 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16100 - Will not do loop reversal optimization
16104 \begin_inset LatexCommand \index{\#pragma nooverlay}
16108 - the compiler will not overlay the parameters and local variables of a
16113 \begin_inset LatexCommand \index{\#pragma stackauto}
16128 \begin_inset LatexCommand \index{-\/-stack-auto}
16133 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16137 Parameters and Local Variables.
16141 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16145 - The compiler will optimize code generation towards fast code, possibly
16146 at the expense of code size.
16150 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16154 - The compiler will optimize code generation towards compact code, possibly
16155 at the expense of code speed.
16159 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16163 - The compiler will attempt to generate code that is both compact and fast,
16164 as long as meeting one goal is not a detriment to the other (this is the
16170 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16174 - Generally follow the C89 standard, but allow SDCC features that conflict
16175 with the standard (default).
16179 \begin_inset LatexCommand \index{\#pragma std\_c89}
16183 - Follow the C89 standard and disable SDCC features that conflict with the
16188 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16192 - Generally follow the C99 standard, but allow SDCC features that conflict
16193 with the standard (incomplete support).
16197 \begin_inset LatexCommand \index{\#pragma std\_c99}
16201 - Follow the C99 standard and disable SDCC features that conflict with the
16202 standard (incomplete support).
16206 \begin_inset LatexCommand \index{\#pragma codeseg}
16210 - Use this name (max.
16211 8 characters) for the code segment.
16226 \begin_inset LatexCommand \index{\#pragma constseg}
16230 - Use this name (max.
16231 8 characters) for the const segment.
16245 SDCPP supports the following #pragma directives:
16249 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16253 (+ | -) - switch _asm _endasm block preprocessing on / off.
16255 You use this prama to define multilines of assembly code.
16256 This will prevent the preprocessor from changing the formating required
16258 Below is an example on how to use this pragma.
16263 #pragma preproc_asm -
16264 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16270 #define MYDELAY _asm
16275 nop ;my assembly comment...
16289 #pragma preproc_asm +
16319 The pragma's are intended to be used to turn-on or off certain optimizations
16320 which might cause the compiler to generate extra stack / data space to
16321 store compiler generated temporary variables.
16322 This usually happens in large functions.
16323 Pragma directives should be used as shown in the following example, they
16324 are used to control options & optimizations for a given function; pragmas
16325 should be placed before and/or after a function, placing pragma's inside
16326 a function body could have unpredictable results.
16332 \begin_inset LatexCommand \index{\#pragma save}
16343 /* save the current settings */
16346 \begin_inset LatexCommand \index{\#pragma nogcse}
16355 /* turnoff global subexpression elimination */
16357 #pragma noinduction
16358 \begin_inset LatexCommand \index{\#pragma noinduction}
16362 /* turn off induction optimizations */
16385 \begin_inset LatexCommand \index{\#pragma restore}
16389 /* turn the optimizations back on */
16392 The compiler will generate a warning message when extra space is allocated.
16393 It is strongly recommended that the save and restore pragma's be used when
16394 changing options for a function.
16403 Defines Created by the Compiler
16406 The compiler creates the following #defines
16407 \begin_inset LatexCommand \index{\#defines}
16412 \begin_inset LatexCommand \index{Defines created by the compiler}
16422 \begin_inset Tabular
16423 <lyxtabular version="3" rows="11" columns="2">
16425 <column alignment="left" valignment="top" leftline="true" width="3in">
16426 <column alignment="left" valignment="top" leftline="true" rightline="true" width="3in">
16427 <row topline="true" bottomline="true">
16428 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16449 <row topline="true">
16450 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16456 \begin_inset LatexCommand \index{SDCC}
16463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16469 Since version 2.5.6 the version number as an int (ex.
16474 <row topline="true">
16475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16481 \begin_inset LatexCommand \index{SDCC\_mcs51}
16486 \begin_inset LatexCommand \index{SDCC\_ds390}
16491 \begin_inset LatexCommand \index{SDCC\_z80}
16498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16503 depending on the model used (e.g.: -mds390)
16507 <row topline="true">
16508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16514 \begin_inset LatexCommand \index{\_\_mcs51}
16519 \begin_inset LatexCommand \index{\_\_ds390}
16524 \begin_inset LatexCommand \index{\_\_hc08}
16529 \begin_inset LatexCommand \index{\_\_z80}
16536 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16541 depending on the model used (e.g.
16546 <row topline="true">
16547 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16553 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16560 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16583 <row topline="true">
16584 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16590 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16597 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16620 <row topline="true">
16621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16627 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16634 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16657 <row topline="true">
16658 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16664 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16694 <row topline="true">
16695 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16701 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16731 <row topline="true">
16732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16738 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16758 <row topline="true" bottomline="true">
16759 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16765 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16772 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16792 Notes on supported Processors
16796 \begin_inset LatexCommand \label{sub:MCS51-variants}
16801 \begin_inset LatexCommand \index{MCS51 variants}
16808 MCS51 processors are available from many vendors and come in many different
16810 While they might differ considerably in respect to Special Function Registers
16811 the core MCS51 is usually not modified or is kept compatible.
16815 pdata access by SFR
16818 With the upcome of devices with internal xdata and flash memory devices
16820 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16824 as dedicated I/O port is becoming more popular.
16825 Switching the high byte for pdata
16826 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16830 access which was formerly done by port P2 is then achieved by a Special
16832 \begin_inset LatexCommand \index{sfr}
16837 In well-established MCS51 tradition the address of this
16841 is where the chip designers decided to put it.
16842 Needless to say that they didn't agree on a common name either.
16843 So that the startup code can correctly initialize xdata variables, you
16844 should define an sfr with the name _XPAGE
16847 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16853 at the appropriate location if the default, port P2, is not used for this.
16859 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16864 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16869 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16872 For more exotic implementations further customizations may be needed.
16874 \begin_inset LatexCommand \ref{sub:Startup-Code}
16878 for other possibilities.
16881 Other Features available by SFR
16884 Some MCS51 variants offer features like Double DPTR
16885 \begin_inset LatexCommand \index{DPTR}
16889 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16890 These are currently not used for the MCS51 port.
16891 If you absolutely need them you can fall back to inline assembly or submit
16898 The DS80C400 microcontroller has a rich set of peripherals.
16899 In its built-in ROM library it includes functions to access some of the
16900 features, among them is a TCP stack with IP4 and IP6 support.
16901 Library headers (currently in beta status) and other files are provided
16905 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16913 The Z80 and gbz80 port
16916 SDCC can target both the Zilog
16917 \begin_inset LatexCommand \index{Z80}
16921 and the Nintendo Gameboy's Z80-like gbz80
16922 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16927 The Z80 port is passed through the same
16930 \begin_inset LatexCommand \index{Regression test}
16936 as the MCS51 and DS390 ports, so floating point support, support for long
16937 variables and bitfield support is fine.
16938 See mailing lists and forums about interrupt routines.
16941 As always, the code is the authoritative reference - see z80/ralloc.c and
16944 \begin_inset LatexCommand \index{stack}
16948 frame is similar to that generated by the IAR Z80 compiler.
16949 IX is used as the base pointer, HL and IY are used as a temporary registers,
16950 and BC and DE are available for holding variables.
16952 \begin_inset LatexCommand \index{return value}
16956 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16958 The gbz80 port use the same set of registers for the return values, but
16959 in a different order of significance: E (one byte), DE (two bytes), or
16966 The port to the Motorola HC08
16967 \begin_inset LatexCommand \index{HC08}
16971 family has been added in October 2003, and is still undergoing some basic
16973 The code generator is complete, but the register allocation is still quite
16975 Some of the SDCC's standard C library functions have embedded non-HC08
16976 inline assembly and so are not yet usable.
16987 \begin_inset LatexCommand \index{PIC14}
16991 port still requires a major effort from the development community.
16992 However it can work for very simple code.
16995 C code and 14bit PIC code page
16996 \begin_inset LatexCommand \index{code page (pic14)}
17001 \begin_inset LatexCommand \index{RAM bank (pic14)}
17008 The linker organizes allocation for the code page and RAM banks.
17009 It does not have intimate knowledge of the code flow.
17010 It will put all the code section of a single asm file into a single code
17012 In order to make use of multiple code pages, separate asm files must be
17014 The compiler treats all functions of a single C file as being in the same
17015 code page unless it is non static.
17016 The compiler treats all local variables of a single C file as being in
17017 the same RAM bank unless it is an extern.
17021 To get the best follow these guide lines:
17024 make local functions static, as non static functions require code page selection
17028 Make local variables static as extern variables require RAM bank selection
17032 For devices that have multiple code pages it is more efficient to use the
17033 same number of files as pages, i.e.
17034 for the 16F877 use 4 separate files and i.e.
17035 for the 16F874 use 2 separate files.
17036 This way the linker can put the code for each file into different code
17037 pages and the compiler can allocate reusable variables more efficiently
17038 and there's less page selection overhead.
17039 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
17040 instruction set) use 'unsigned char' whereever possible instead of 'int'.
17043 Creating a device include file
17046 For generating a device include file use the support perl script inc2h.pl
17047 kept in directory support/script.
17053 For the interrupt function, use the keyword 'interrupt'
17054 \begin_inset LatexCommand \index{interrupt}
17058 with level number of 0 (PIC14 only has 1 interrupt so this number is only
17059 there to avoid a syntax error - it ought to be fixed).
17065 void Intr(void) interrupt 0
17071 T0IF = 0; /* Clear timer interrupt */
17076 Linking and assembling
17079 For assembling you can use either GPUTILS'
17080 \begin_inset LatexCommand \index{gputils (pic tools)}
17084 gpasm.exe or MPLAB's mpasmwin.exe.
17085 GPUTILS is available from
17086 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17091 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17092 If you use MPLAB and an interrupt function then the linker script file
17093 vectors section will need to be enlarged to link with mplink.
17116 sdcc -S -V -mpic14 -p16F877 $<
17130 $(PRJ).hex: $(OBJS)
17140 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17162 sdcc -S -V -mpic14 -p16F877 $<
17172 mpasmwin /q /o $*.asm
17176 $(PRJ).hex: $(OBJS)
17186 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17189 Please note that indentations within a
17193 have to be done with a tabulator character.
17196 Command-line options
17199 Besides the switches common to all SDCC backends, the PIC14 port accepts
17200 the following options (for an updated list see sdcc -
17212 \labelwidthstring 00.00.0000
17224 -debug-extra emit debug info in assembly output
17226 \labelwidthstring 00.00.0000
17238 -no-pcode-opt disable (slightly faulty) optimization on pCode
17242 \layout Subsubsection
17244 error: missing definition for symbol
17245 \begin_inset Quotes sld
17249 \begin_inset Quotes srd
17255 The PIC14 port uses library routines to provide more complex operations
17256 like multiplication, division/modulus and (generic) pointer dereferencing.
17257 In order to add these routines to your project, you must link with PIC14's
17263 For single source file projects this is done automatically, more complex
17268 to the linker's arguments.
17269 Make sure you also add an include path for the library (using the -I switch
17271 \layout Subsubsection
17273 Processor mismatch in file
17274 \begin_inset Quotes sld
17278 \begin_inset Quotes srd
17284 This warning can usually be ignored due to the very good compatibility amongst
17285 14 bit PIC devices.
17288 You might also consider recompiling the library for your specific device
17289 by changing the ARCH=p16f877 (default target) entry in
17291 device/lib/pic/Makefile.in
17295 device/lib/pic/Makefile
17297 to reflect your device.
17298 This might even improve performance for smaller devices as unneccesary
17299 BANKSELs migth be removed.
17303 \layout Subsubsection
17308 Currently, data can only be initialized if it resides in the source file
17314 Data in other source files will silently
17322 \begin_inset LatexCommand \index{PIC16}
17330 \begin_inset LatexCommand \index{PIC16}
17334 port is the portion of SDCC that is responsible to produce code for the
17336 \begin_inset LatexCommand \index{Microchip}
17340 (TM) microcontrollers with 16 bit core.
17341 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17342 Currently supported devices are:
17346 \begin_inset Tabular
17347 <lyxtabular version="3" rows="4" columns="6">
17349 <column alignment="center" valignment="top" leftline="true" width="0">
17350 <column alignment="center" valignment="top" leftline="true" width="0">
17351 <column alignment="center" valignment="top" leftline="true" width="0">
17352 <column alignment="center" valignment="top" leftline="true" width="0">
17353 <column alignment="center" valignment="top" leftline="true" width="0">
17354 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17355 <row topline="true">
17356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17388 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17396 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17405 <row topline="true">
17406 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17414 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17422 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17430 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17438 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17455 <row topline="true">
17456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17472 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17480 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17488 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17496 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17505 <row topline="true" bottomline="true">
17506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17514 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17522 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17530 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17544 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17562 PIC16 port supports the standard command line arguments as supposed, with
17563 the exception of certain cases that will be mentioned in the following
17566 \labelwidthstring 00.00.0000
17578 -callee-saves See -
17590 \labelwidthstring 00.00.0000
17602 -all-callee-saves All function arguments are passed on stack by default.
17605 There is no need to specify this in the command line.
17607 \labelwidthstring 00.00.0000
17619 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17620 no local variables.
17623 Port Specific Options
17624 \begin_inset LatexCommand \index{Options PIC16}
17631 The port specific options appear after the global options in the sdcc --help
17633 \layout Subsubsection
17638 General options enable certain port features and optimizations.
17640 \labelwidthstring 00.00.0000
17652 -stack-model=[model] Used in conjuction with the command above.
17653 Defines the stack model to be used, valid stack models are :
17656 \labelwidthstring 00.00.0000
17662 Selects small stack model.
17663 8 bit stack and frame pointers.
17664 Supports 256 bytes stack size.
17666 \labelwidthstring 00.00.0000
17672 Selects large stack model.
17673 16 bit stack and frame pointers.
17674 Supports 65536 bytes stack size.
17677 \labelwidthstring 00.00.0000
17689 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17690 unitialized data variables with [kword].
17691 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17693 \labelwidthstring 00.00.0000
17705 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17706 Useful for bootloaders.
17708 \labelwidthstring 00.00.0000
17720 -asm= sets the full path and name of an external assembler to call.
17722 \labelwidthstring 00.00.0000
17734 -link= sets the full path and name of an external linker to call.
17736 \labelwidthstring 00.00.0000
17748 -mplab-comp MPLAB compatibility option.
17749 Currently only suppresses special gpasm directives.
17750 \layout Subsubsection
17752 Optimization Options
17754 \labelwidthstring 00.00.0000
17766 -optimize-goto Try to use (conditional) BRA instead of GOTO
17768 \labelwidthstring 00.00.0000
17780 -optimize-cmp Try to optimize some compares.
17782 \labelwidthstring 00.00.0000
17794 -optimize-df Analyze the dataflow of the generated code and improve it.
17796 \labelwidthstring 00.00.0000
17808 -obanksel=nn Set optimization level for inserting BANKSELs.
17813 \labelwidthstring 00.00.0000
17817 \labelwidthstring 00.00.0000
17819 1 checks previous used register and if it is the same then does not emit
17820 BANKSEL, accounts only for labels.
17822 \labelwidthstring 00.00.0000
17824 2 tries to check the location of (even different) symbols and removes BANKSELs
17825 if they are in the same bank.
17830 Important: There might be problems if the linker script has data sections
17831 across bank borders!
17833 \layout Subsubsection
17837 \labelwidthstring 00.00.0000
17849 -nodefaultlibs do not link default libraries when linking
17851 \labelwidthstring 00.00.0000
17863 -no-crt Don't link the default run-time modules
17865 \labelwidthstring 00.00.0000
17877 -use-crt= Use a custom run-time module instead of the defaults.
17878 \layout Subsubsection
17883 Debugging options enable extra debugging information in the output files.
17885 \labelwidthstring 00.00.0000
17897 -debug-xtra Similar to -
17908 \begin_inset LatexCommand \index{-\/-debug}
17912 , but dumps more information.
17914 \labelwidthstring 00.00.0000
17926 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17928 <source> is the name of the file compiled.
17930 \labelwidthstring 00.00.0000
17942 -pcode-verbose Enable pcode debugging information in translation.
17944 \labelwidthstring 00.00.0000
17956 -denable-peeps Force the usage of peepholes.
17959 \labelwidthstring 00.00.0000
17971 -gstack Trace push/pops for stack pointer overflow
17973 \labelwidthstring 00.00.0000
17985 -call-tree dump call tree in .calltree file
17988 Enviromental Variables
17991 There is a number of enviromental variables that can be used when running
17992 SDCC to enable certain optimizations or force a specific program behaviour.
17993 these variables are primarily for debugging purposes so they can be enabled/dis
17997 Currently there is only two such variables available:
17999 \labelwidthstring 00.00.0000
18001 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
18002 bitfields is optimized by directly loading FSR0 with the address of the
18003 bitfield structure.
18004 Normally SDCC will cast the bitfield structure to a bitfield pointer and
18006 This step saves data ram and code space for functions that perform heavy
18009 80 bytes of code space are saved when compiling malloc.c with this option).
18012 \labelwidthstring 00.00.0000
18014 NO_REG_OPT do not perform pCode registers optimization.
18015 This should be used for debugging purposes.
18016 In some where bugs in the pcode optimizer are found, users can benefit
18017 from temporarily disabling the optimizer until the bug is fixed.
18020 Preprocessor Macros
18023 PIC16 port defines the following preprocessor macros while translating a
18028 \begin_inset Tabular
18029 <lyxtabular version="3" rows="6" columns="2">
18031 <column alignment="center" valignment="top" leftline="true" width="0">
18032 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18033 <row topline="true" bottomline="true">
18034 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18051 <row topline="true">
18052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18065 Port identification
18069 <row topline="true">
18070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18093 Port identification (same as above)
18097 <row topline="true">
18098 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18106 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18111 MCU Identification.
18116 is the microcontrol identification number, i.e.
18121 <row topline="true">
18122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18145 MCU Identification (same as above)
18149 <row topline="true" bottomline="true">
18150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18163 nnn = SMALL or LARGE respectively according to the stack model used
18174 In addition the following macros are defined when calling assembler:
18178 \begin_inset Tabular
18179 <lyxtabular version="3" rows="4" columns="2">
18181 <column alignment="center" valignment="top" leftline="true" width="0">
18182 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18183 <row topline="true" bottomline="true">
18184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18201 <row topline="true">
18202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18215 MCU Identification.
18220 is the microcontrol identification number, i.e.
18225 <row topline="true">
18226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18239 nnn = SMALL or LARGE respectively according to the memory model used for
18244 <row topline="true" bottomline="true">
18245 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18253 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18258 nnn = SMALL or LARGE respectively according to the stack model used
18273 \begin_inset LatexCommand \index{PIC16}
18277 port uses the following directories for searching header files and libraries.
18281 \begin_inset Tabular
18282 <lyxtabular version="3" rows="3" columns="4">
18284 <column alignment="center" valignment="top" leftline="true" width="0">
18285 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18286 <column alignment="center" valignment="top" width="0">
18287 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18288 <row topline="true" bottomline="true">
18289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18313 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18322 <row topline="true">
18323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18328 PREFIX/sdcc/include/pic16
18331 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18336 PIC16 specific headers
18339 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18347 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18356 <row topline="true" bottomline="true">
18357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18362 PREFIX/sdcc/lib/pic16
18365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18370 PIC16 specific libraries
18373 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18398 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18405 PIC16 port currently supports the following pragmas:
18407 \labelwidthstring 00.00.0000
18409 stack pragma stack forces the code generator to initialize the stack & frame
18410 pointers at a specific address.
18411 This is an adhoc solution for cases where no STACK directive is available
18412 in the linker script or gplink is not instructed to create a stack section.
18414 The stack pragma should be used only once in a project.
18415 Multiple pragmas may result in indeterminate behaviour of the program.
18421 The old format (ie.
18422 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18423 cross page boundaries (or even exceed the available data RAM) and crash
18425 Make sure that stack does not cross page boundaries when using the SMALL
18431 The format is as follows:
18434 #pragma stack bottom_address [stack_size]
18441 is the lower bound of the stack section.
18442 The stack pointer initially will point at address (bottom_address+stack_size-1).
18450 /* initializes stack of 100 bytes at RAM address 0x200 */
18453 #pragma stack 0x200 100
18456 If the stack_size field is omitted then a stack is created with the default
18458 This size might be enough for most programs, but its not enough for operations
18459 with deep function nesting or excessive stack usage.
18461 \labelwidthstring 00.00.0000
18465 This pragma is deprecated.
18466 Its use will cause a warning message to be issued.
18472 \labelwidthstring 00.00.0000
18474 code place a function symbol at static FLASH address
18482 /* place function test_func at 0x4000 */
18485 #pragma code test_func 0x4000
18489 \labelwidthstring 00.00.0000
18491 library instructs the linker to use a library module.
18496 #pragma library module_name
18503 can be any library or object file (including its path).
18504 Note that there are four reserved keywords which have special meaning.
18509 \begin_inset Tabular
18510 <lyxtabular version="3" rows="6" columns="3">
18512 <column alignment="center" valignment="top" leftline="true" width="0">
18513 <column alignment="block" valignment="top" leftline="true" width="20page%">
18514 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18515 <row topline="true" bottomline="true">
18516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18524 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18541 <row topline="true">
18542 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18552 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18557 ignore all library pragmas
18560 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18571 <row topline="true">
18572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18603 <row topline="true">
18604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18619 link the Math libarary
18622 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18635 <row topline="true">
18636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18651 link the I/O library
18654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18667 <row topline="true" bottomline="true">
18668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18683 link the debug library
18686 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18705 * is the device number, i.e.
18706 452 for PIC18F452 MCU.
18709 This feature allows for linking with specific libraries withoug having to
18710 explicit name them in the command line.
18715 keyword will reject all modules specified by the library pragma.
18717 \labelwidthstring 00.00.0000
18719 udata pragma udata instructs the compiler to emit code so that linker will
18720 place a variable at a specific memory bank
18728 /* places variable foo at bank2 */
18731 #pragma udata bank2 foo
18737 In order for this pragma to work extra SECTION directives should be added
18738 in the .lkr script.
18739 In the following example a sample .lkr file is shown:
18744 // Sample linker script for the PIC18F452 processor
18750 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18753 CODEPAGE NAME=page START=0x2A END=0x7FFF
18756 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18759 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18762 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18765 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18768 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18773 DATABANK NAME=gpr0 START=0x80 END=0xFF
18776 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18779 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18782 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18785 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18788 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18791 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18796 SECTION NAME=CONFIG ROM=config
18801 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18804 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18807 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18810 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18813 SECTION NAME=bank4 RAM=gpr4
18816 SECTION NAME=bank5 RAM=gpr5
18819 The linker will recognise the section name set in the pragma statement and
18820 will position the variable at the memory bank set with the RAM field at
18821 the SECTION line in the linker script file.
18825 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18832 There is one main header file that can be included to the source files using
18839 This header file contains the definitions for the processor special registers,
18840 so it is necessary if the source accesses them.
18841 It can be included by adding the following line in the beginning of the
18845 #include <pic18fregs.h>
18848 The specific microcontroller is selected within the pic18fregs.h automatically,
18849 so the same source can be used with a variety of devices.
18855 The libraries that PIC16
18856 \begin_inset LatexCommand \index{PIC16}
18860 port depends on are the microcontroller device libraries which contain
18861 the symbol definitions for the microcontroller special function registers.
18862 These libraries have the format pic18fxxxx.lib, where
18866 is the microcontroller identification number.
18867 The specific library is selected automatically by the compiler at link
18868 stage according to the selected device.
18871 Libraries are created with gplib which is part of the gputils package
18872 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18877 \layout Subsubsection*
18879 Building the libraries
18882 Before using SDCC/pic16 there are some libraries that need to be compiled.
18883 This process is not done automatically by SDCC since not all users use
18884 SDCC for pic16 projects.
18885 So each user should compile the libraries separately.
18888 The steps to compile the pic16 libraries under Linux are:
18891 cd device/lib/pic16
18906 su -c 'make install' # install the libraries, you need the root password
18909 If you need to install the headers too, do:
18915 su -c 'make install' # install the headers, you need the root password
18918 There exist a special target to build the I/O libraries.
18919 This target is not automatically build because it will build the I/O library
18925 This way building will take quite a lot of time.
18926 Users are advised to edit the
18928 device/lib/pic16/pics.build
18930 file and then execute:
18939 The following memory models are supported by the PIC16 port:
18948 Memory model affects the default size of pointers within the source.
18949 The sizes are shown in the next table:
18953 \begin_inset Tabular
18954 <lyxtabular version="3" rows="3" columns="3">
18956 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18957 <column alignment="center" valignment="top" leftline="true" width="0">
18958 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18959 <row topline="true" bottomline="true">
18960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18965 Pointer sizes according to memory model
18968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18985 <row topline="true" bottomline="true">
18986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18994 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19011 <row topline="true" bottomline="true">
19012 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19020 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
19028 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19044 It is advisable that all sources within a project are compiled with the
19046 If one wants to override the default memory model, this can be done by
19047 declaring a pointer as
19056 Far selects large memory model's pointers, while near selects small memory
19060 The standard device libraries (see
19061 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
19065 ) contain no reference to pointers, so they can be used with both memory
19072 The stack implementation for the PIC16 port uses two indirect registers,
19075 \labelwidthstring 00.00.0000
19077 FSR1 is assigned as stack pointer
19079 \labelwidthstring 00.00.0000
19081 FSR2 is assigned as frame pointer
19084 The following stack models are supported by the PIC16 port
19105 model means that only the FSRxL byte is used to access stack and frame,
19112 uses both FSRxL and FSRxH registers.
19113 The following table shows the stack/frame pointers sizes according to stack
19114 model and the maximum space they can address:
19118 \begin_inset Tabular
19119 <lyxtabular version="3" rows="3" columns="3">
19121 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19122 <column alignment="center" valignment="top" leftline="true" width="0">
19123 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19124 <row topline="true" bottomline="true">
19125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19130 Stack & Frame pointer sizes according to stack model
19133 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19141 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19150 <row topline="true">
19151 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19167 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19176 <row topline="true" bottomline="true">
19177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19213 stack model is currently not working properly throughout the code generator.
19214 So its use is not advised.
19215 Also there are some other points that need special care:
19220 Do not create stack sections with size more than one physical bank (that
19224 Stack sections should no cross physical bank limits (i.e.
19225 #pragma stack 0x50 0x100)
19228 These limitations are caused by the fact that only FSRxL is modified when
19229 using SMALL stack model, so no more than 256 bytes of stack can be used.
19230 This problem will disappear after LARGE model is fully implemented.
19236 In addition to the standard SDCC function keywords, PIC16 port makes available
19239 \labelwidthstring 00.00.0000
19241 wparam Use the WREG to pass one byte of the first function argument.
19242 This improves speed but you may not use this for functions with arguments
19243 that are called via function pointers, otherwise the first byte of the
19244 first parameter will get lost.
19248 void func_wparam(int a) wparam
19254 /* WREG hold the lower part of a */
19257 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19267 This keyword replaces the deprecated wparam pragma.
19269 \labelwidthstring 00.00.0000
19271 shadowregs When entering/exiting an ISR, it is possible to take advantage
19272 of the PIC18F hardware shadow registers which hold the values of WREG,
19273 STATUS and BSR registers.
19274 This can be done by adding the keyword
19282 keyword in the function's header.
19285 void isr_shadow(void) shadowregs interrupt 1
19301 instructs the code generator not to store/restore WREG, STATUS, BSR when
19302 entering/exiting the ISR.
19305 Function return values
19308 Return values from functions are placed to the appropriate registers following
19309 a modified Microchip policy optimized for SDCC.
19310 The following table shows these registers:
19314 \begin_inset Tabular
19315 <lyxtabular version="3" rows="6" columns="2">
19317 <column alignment="center" valignment="top" leftline="true" width="0">
19318 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19319 <row topline="true" bottomline="true">
19320 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19333 destination register
19337 <row topline="true">
19338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19346 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19355 <row topline="true">
19356 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19373 <row topline="true">
19374 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19391 <row topline="true">
19392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19405 FSR0L:PRODH:PRODL:WREG
19409 <row topline="true" bottomline="true">
19410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19423 on stack, FSR0 points to the beginning
19437 An interrupt servive routine (ISR) is declared using the
19444 void isr(void) interrupt
19462 is the interrupt number, which for PIC18F devices can be:
19466 \begin_inset Tabular
19467 <lyxtabular version="3" rows="4" columns="3">
19469 <column alignment="center" valignment="top" leftline="true" width="0">
19470 <column alignment="center" valignment="top" leftline="true" width="0">
19471 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19472 <row topline="true" bottomline="true">
19473 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19483 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19491 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19496 Interrupt Vector Address
19500 <row topline="true">
19501 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19526 <row topline="true">
19527 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19558 HIGH priority interrupts
19561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19570 <row topline="true" bottomline="true">
19571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19584 LOW priority interrupts
19587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19603 When generating assembly code for ISR the code generator places a
19609 Interrupt Vector Address
19611 which points at the genetated ISR.
19612 This single GOTO instruction is part of an automatically generated
19614 interrupt entry point
19617 The actuall ISR code is placed as normally would in the code space.
19618 Upon interrupt request, the GOTO instruction is executed which jumps to
19620 When declaring interrupt functions as _naked this GOTO instruction is
19625 The whole interrupt functions is therefore placed at the Interrupt Vector
19626 Address of the specific interrupt.
19627 This is not a problem for the LOW priority interrupts, but it is a problem
19628 for the RESET and the HIGH priority interrupts because code may be written
19629 at the next interrupt´s vector address and cause undeterminate program
19630 behaviour if that interrupt is raised.
19636 This is not a problem when
19639 this is a HIGH interrupt ISR and LOW interrupts are
19646 when the ISR is small enough not to reach the next interrupt´s vector address.
19656 is possible to be omitted.
19657 This way a function is generated similar to an ISR, but it is not assigned
19661 When entering an interrupt, currently the PIC16
19662 \begin_inset LatexCommand \index{PIC16}
19666 port automatically saves the following registers:
19678 PROD (PRODL and PRODH)
19681 FSR0 (FSR0L and FSR0H)
19684 These registers are restored upon return from the interrupt routine.
19690 NOTE that when the _naked attribute is specified for an interrupt routine,
19691 then NO registers are stored or restored.
19700 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19701 There are 3 types of generic pointers currently implemented data, code
19702 and eeprom pointers.
19703 They are differentiated by the value of the 7th and 6th bits of the upper
19708 \begin_inset Tabular
19709 <lyxtabular version="3" rows="5" columns="5">
19711 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19712 <column alignment="center" valignment="top" width="0">
19713 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19714 <column alignment="center" valignment="top" width="0">
19715 <column alignment="left" valignment="top" rightline="true" width="0">
19716 <row topline="true" bottomline="true">
19717 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19733 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19741 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19746 rest of the pointer
19749 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19758 <row topline="true" bottomline="true">
19759 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19792 uuuuuu uuuuxxxx xxxxxxxx
19795 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19800 a 12-bit data pointer in data RAM memory
19804 <row bottomline="true">
19805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19821 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19838 uxxxxx xxxxxxxx xxxxxxxx
19841 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19846 a 21-bit code pointer in FLASH memory
19850 <row bottomline="true">
19851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19884 uuuuuu uuuuuuxx xxxxxxxx
19887 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19892 a 10-bit eeprom pointer in EEPROM memory
19896 <row bottomline="true">
19897 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19913 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19930 xxxxxx xxxxxxxx xxxxxxxx
19933 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19938 unimplemented pointer type
19949 Generic pointer are read and written with a set of library functions which
19950 read/write 1, 2, 3, 4 bytes.
19954 \layout Subsubsection
19956 Standard I/O Streams
19963 the type FILE is defined as:
19966 typedef char * FILE;
19969 This type is the stream type implemented I/O in the PIC18F devices.
19970 Also the standard input and output streams are declared in stdio.h:
19973 extern FILE * stdin;
19976 extern FILE * stdout;
19979 The FILE type is actually a generic pointer which defines one more type
19980 of generic pointers, the
19985 This new type has the format:
19989 \begin_inset Tabular
19990 <lyxtabular version="3" rows="2" columns="7">
19992 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19993 <column alignment="center" valignment="top" width="0">
19994 <column alignment="center" valignment="top" leftline="true" width="0">
19995 <column alignment="center" valignment="top" leftline="true" width="0">
19996 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19997 <column alignment="center" valignment="top" width="0">
19998 <column alignment="left" valignment="top" rightline="true" width="0">
19999 <row topline="true" bottomline="true">
20000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20016 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20024 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20045 rest of the pointer
20048 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20057 <row topline="true" bottomline="true">
20058 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20082 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20110 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20115 upper byte high nubble is 0x2n, the rest are zeroes
20126 Currently implemented there are 3 types of streams defined:
20130 \begin_inset Tabular
20131 <lyxtabular version="3" rows="4" columns="4">
20133 <column alignment="center" valignment="top" leftline="true" width="0">
20134 <column alignment="center" valignment="top" leftline="true" width="0">
20135 <column alignment="center" valignment="top" leftline="true" width="0">
20136 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20137 <row topline="true" bottomline="true">
20138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20162 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20171 <row topline="true">
20172 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20203 Writes/Reads characters via the USART peripheral
20207 <row topline="true">
20208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20239 Writes/Reads characters via the MSSP peripheral
20243 <row topline="true" bottomline="true">
20244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20262 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20270 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20275 Writes/Reads characters via used defined functions
20286 The stream identifiers are declared as macros in the stdio.h header.
20289 In the libc library there exist the functions that are used to write to
20290 each of the above streams.
20293 \labelwidthstring 00.00.0000
20305 _stream_usart_putchar writes a character at the USART stream
20307 \labelwidthstring 00.00.0000
20319 _stream_mssp_putchar writes a character at the MSSP stream
20321 \labelwidthstring 00.00.0000
20323 putchar dummy function.
20324 This writes a character to a user specified manner.
20327 In order to increase performance
20331 is declared in stdio.h as having its parameter in WREG (it has the wparam
20333 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20334 in a user-friendly way.
20339 is the name of the variable that holds the character to print.
20340 An example follows:
20343 #include <pic18fregs.h>
20355 PORTA = c; /* dump character c to PORTA */
20368 stdout = STREAM_USER; /* this is not necessary, since stdout points
20371 * by default to STREAM_USER */
20374 printf (¨This is a printf test
20382 \layout Subsubsection
20387 PIC16 contains an implementation of the printf-family of functions.
20388 There exist the following functions:
20391 extern unsigned int sprintf(char *buf, char *fmt, ...);
20394 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20399 extern unsigned int printf(char *fmt, ...);
20402 extern unsigned int vprintf(char *fmt, va_lista ap);
20407 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20410 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20413 For sprintf and vsprintf
20417 should normally be a data pointer where the resulting string will be placed.
20418 No range checking is done so the user should allocate the necessery buffer.
20419 For fprintf and vfprintf
20423 should be a stream pointer (i.e.
20424 stdout, STREAM_MSSP, etc...).
20425 \layout Subsubsection
20430 The PIC18F family of microcontrollers supports a number of interrupt sources.
20431 A list of these interrupts is shown in the following table:
20435 \begin_inset Tabular
20436 <lyxtabular version="3" rows="11" columns="4">
20438 <column alignment="left" valignment="top" leftline="true" width="0">
20439 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20440 <column alignment="left" valignment="top" leftline="true" width="0">
20441 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20442 <row topline="true" bottomline="true">
20443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20451 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20476 <row topline="true">
20477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20485 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20490 PORTB change interrupt
20493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20501 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20506 EEPROM/FLASH write complete interrupt
20510 <row topline="true">
20511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20524 INT0 external interrupt
20527 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20540 Bus collision interrupt
20544 <row topline="true">
20545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20558 INT1 external interrupt
20561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20574 Low voltage detect interrupt
20578 <row topline="true">
20579 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20587 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20592 INT2 external interrupt
20595 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20608 Parallel slave port interrupt
20612 <row topline="true">
20613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20626 CCP1 module interrupt
20629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20642 AD convertion complete interrupt
20646 <row topline="true">
20647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20660 CCP2 module interrupt
20663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20671 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20676 USART receive interrupt
20680 <row topline="true">
20681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20689 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20694 TMR0 overflow interrupt
20697 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20710 USART transmit interrupt
20714 <row topline="true">
20715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20723 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20728 TMR1 overflow interrupt
20731 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20739 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20744 SSP receive/transmit interrupt
20748 <row topline="true">
20749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20762 TMR2 matches PR2 interrupt
20765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20772 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20780 <row topline="true" bottomline="true">
20781 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20794 TMR3 overflow interrupt
20797 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20819 The prototypes for these names are defined in the header file
20826 In order to simplify signal handling, a number of macros is provided:
20828 \labelwidthstring 00.00.0000
20830 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20831 high priority interrupts.
20836 is the function name to use.
20838 \labelwidthstring 00.00.0000
20840 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20841 low priority interrupt.
20846 is the function name to use.
20848 \labelwidthstring 00.00.0000
20850 DEF_HANDLER(sig,handler) define a handler for signal
20854 \labelwidthstring 00.00.0000
20856 END_DEF end the declaration of the dispatch table.
20859 Additionally there are two more macros to simplify the declaration of the
20862 \labelwidthstring 00.00.0000
20866 SIGHANDLER(handler)
20868 this declares the function prototype for the
20874 \labelwidthstring 00.00.0000
20876 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20879 An example of using the macros above is shown below:
20882 #include <pic18fregs.h>
20885 #include <signal.h>
20889 DEF_INTHIGH(high_int)
20892 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20895 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20902 SIGHANDLER(_tmr0_handler)
20908 /* action to be taken when timer 0 overflows */
20915 SIGHANDLERNAKED(_bcol_handler)
20924 /* action to be taken when bus collision occurs */
20940 Special care should be taken when using the above scheme:
20943 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20946 when declaring SIGHANDLERNAKED handler never forget to use
20950 for proper returning.
20956 Here you can find some general tips for compiling programs with SDCC/pic16.
20957 \layout Subsubsection
20962 The default stack size (that is 64 bytes) probably is enough for many programs.
20963 One must take care that when there are many levels of function nesting,
20964 or there is excessive usage of stack, its size should be extended.
20965 An example of such a case is the printf/sprintf family of functions.
20966 If you encounter problems like not being able to print integers, then you
20967 need to set the stack size around the maximum (256 for small stack model).
20968 The following diagram shows what happens when calling printf to print an
20972 printf () --> ltoa () --> ultoa () --> divschar ()
20975 It is should be understood that stack is easily consumed when calling complicate
20977 Using command line arguments like -
20987 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20989 Other ways to reduce stack usage may exist.
20995 There are several approaches to debugging your code.
20996 This chapter is meant to show your options and to give detail on some of
21001 When writing your code:
21004 write your code with debugging in mind (avoid duplicating code, put conceptually
21005 similar variables into structs, use structured code, have strategic points
21006 within your code where all variables are consistent, ...)
21009 run a syntax-checking tool like splint
21010 \begin_inset LatexCommand \index{splint (syntax checking tool)}
21015 \begin_inset LatexCommand \index{lint (syntax checking tool)}
21030 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21037 for the high level code use a C-compiler (like f.e.
21038 GCC) to compile run and debug the code on your host.
21050 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21054 ) on howto handle syntax extensions like __xdata, __at(), ...
21058 use another C-compiler to compile code for your target.
21059 Always an option but not recommended:) And not very likely to help you.
21060 If you seriously consider walking this path you should at least occasionally
21061 check portability of your code.
21062 Most commercial compiler vendors will offer an evaluation version so you
21063 can test compile your code or snippets of your code.
21066 Debugging on a simulator:
21069 there is a separate section about SDCDB (section
21070 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21077 or (8051 specific) use a freeware/commercial simulator which interfaces
21079 \begin_inset LatexCommand \index{AOMF, AOMF51}
21084 \begin_inset LatexCommand \ref{OMF file}
21088 ) optionally generated by SDCC.
21091 Debugging On-target:
21094 use a MCU port pin to serially output debug data to the RS232 port of your
21096 You'll probably want some level shifting device typically involving a MAX232
21098 If the hardware serial port of the MCU is not available search for 'Software
21099 UART' in your favourite search machine.
21102 use an on-target monitor.
21103 In this context a monitor is a small program which usually accepts commands
21104 via a serial line and allows to set program counter, to single step through
21105 a program and read/write memory locations.
21106 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21108 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21115 toggle MCU port pins at strategic points within your code and use an oscilloscop
21119 digital oscilloscope
21122 \begin_inset LatexCommand \index{oscilloscope}
21126 with deep trace memory is really helpful especially if you have to debug
21127 a realtime application.
21128 If you need to monitor more pins than your oscilloscope provides you can
21129 sometimes get away with a small R-2R network.
21130 On a single channel oscilloscope you could f.e.
21131 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21133 \begin_inset Formula $\Omega$
21136 resistor and the other one by a 5\SpecialChar ~
21138 \begin_inset Formula $\Omega$
21141 resistor to the oscilloscope probe (check output drive capability of the
21142 pins you want to monitor).
21143 If you need to monitor many more pins a
21163 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21168 Usually very expensive.
21169 And very nice to have too.
21170 And usually locks you (for years...) to the devices the ICE can emulate.
21174 use a remote debugger.
21175 In most 8-bit systems the symbol information is not available on the target,
21176 and a complete debugger is too bulky for the target system.
21177 Therefore usually a debugger on the host system connects to an on-target
21178 debugging stub which accepts only primitive commands.
21181 Terms to enter into your favourite search engine could be 'remote debugging',
21182 'gdb stub' or 'inferior debugger'.
21186 use an on target hardware debugger.
21187 Some of the more modern MCUs include hardware support for setting break
21188 points and monitoring/changing variables by using dedicated hardware pins.
21189 This facility doesn't require additional code to run on the target and
21194 doesn't affect runtime behaviour until a breakpoint is hit.
21195 For the mcs51 most hardware debuggers use the AOMF
21196 \begin_inset LatexCommand \index{AOMF, AOMF51}
21201 \begin_inset LatexCommand \ref{OMF file}
21212 if you are not familiar with any of the following terms you're likely to
21213 run into problems rather sooner than later:
21230 As an embedded programmer you
21234 to know them so why not look them up
21238 you have problems?)
21241 tell someone else about your problem (actually this is a surprisingly effective
21242 means to hunt down the bug even if the listener is not familiar with your
21244 As 'failure to communicate' is probably one of the job-induced deformations
21245 of an embedded programmer this is highly encouraged.
21248 Debugging with SDCDB
21249 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21254 \begin_inset LatexCommand \index{sdcdb (debugger)}
21261 SDCC is distributed with a source level debugger
21262 \begin_inset LatexCommand \index{Debugger}
21267 The debugger uses a command line interface, the command repertoire of the
21268 debugger has been kept as close to gdb
21269 \begin_inset LatexCommand \index{gdb}
21273 (the GNU debugger) as possible.
21274 The configuration and build process is part of the standard compiler installati
21275 on, which also builds and installs the debugger in the target directory
21276 specified during configuration.
21277 The debugger allows you debug BOTH at the C source and at the ASM source
21279 Sdcdb is currently not available on Win32 platforms.
21282 Compiling for Debugging
21296 \begin_inset LatexCommand \index{-\/-debug}
21300 option must be specified for all files for which debug information is to
21302 The compiler generates a .adb file for each of these files.
21303 The linker creates the .cdb
21304 \begin_inset LatexCommand \index{<file>.cdb}
21309 \begin_inset LatexCommand \index{<file>.adb}
21313 files and the address information.
21314 This .cdb is used by the debugger.
21317 How the Debugger Works
21330 -debug option is specified the compiler generates extra symbol information
21331 some of which are put into the assembler source and some are put into the
21333 Then the linker creates the .cdb file from the individual .adb files with
21334 the address information for the symbols.
21335 The debugger reads the symbolic information generated by the compiler &
21336 the address information generated by the linker.
21337 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21338 execution is controlled by the debugger.
21339 When a command is issued for the debugger, it translates it into appropriate
21340 commands for the simulator.
21341 (Currently sdcdb only connects to the simulator but
21346 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21350 is an effort to connect directly to the hardware.)
21353 Starting the Debugger SDCDB
21356 The debugger can be started using the following command line.
21357 (Assume the file you are debugging has the file name foo).
21371 The debugger will look for the following files.
21374 foo.c - the source file.
21377 foo.cdb - the debugger symbol information file.
21380 foo.ihx - the Intel hex format
21381 \begin_inset LatexCommand \index{Intel hex format}
21388 SDCDB Command Line Options
21401 -directory=<source file directory> this option can used to specify the directory
21403 The debugger will look into the directory list specified for source, cdb
21405 The items in the directory list must be separated by ':', e.g.
21406 if the source files can be in the directories /home/src1 and /home/src2,
21417 -directory option should be -
21427 -directory=/home/src1:/home/src2.
21428 Note there can be no spaces in the option.
21432 -cd <directory> - change to the <directory>.
21435 -fullname - used by GUI front ends.
21438 -cpu <cpu-type> - this argument is passed to the simulator please see the
21439 simulator docs for details.
21442 -X <Clock frequency > this options is passed to the simulator please see
21443 the simulator docs for details.
21446 -s <serial port file> passed to simulator see the simulator docs for details.
21449 -S <serial in,out> passed to simulator see the simulator docs for details.
21452 -k <port number> passed to simulator see the simulator docs for details.
21455 SDCDB Debugger Commands
21458 As mentioned earlier the command interface for the debugger has been deliberatel
21459 y kept as close the GNU debugger gdb, as possible.
21460 This will help the integration with existing graphical user interfaces
21461 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21462 If you use a graphical user interface for the debugger you can skip this
21464 \layout Subsubsection*
21466 break [line | file:line | function | file:function]
21469 Set breakpoint at specified line or function:
21478 sdcdb>break foo.c:100
21480 sdcdb>break funcfoo
21482 sdcdb>break foo.c:funcfoo
21483 \layout Subsubsection*
21485 clear [line | file:line | function | file:function ]
21488 Clear breakpoint at specified line or function:
21497 sdcdb>clear foo.c:100
21499 sdcdb>clear funcfoo
21501 sdcdb>clear foo.c:funcfoo
21502 \layout Subsubsection*
21507 Continue program being debugged, after breakpoint.
21508 \layout Subsubsection*
21513 Execute till the end of the current function.
21514 \layout Subsubsection*
21519 Delete breakpoint number 'n'.
21520 If used without any option clear ALL user defined break points.
21521 \layout Subsubsection*
21523 info [break | stack | frame | registers ]
21526 info break - list all breakpoints
21529 info stack - show the function call stack.
21532 info frame - show information about the current execution frame.
21535 info registers - show content of all registers.
21536 \layout Subsubsection*
21541 Step program until it reaches a different source line.
21542 Note: pressing <return> repeats the last command.
21543 \layout Subsubsection*
21548 Step program, proceeding through subroutine calls.
21549 \layout Subsubsection*
21554 Start debugged program.
21555 \layout Subsubsection*
21560 Print type information of the variable.
21561 \layout Subsubsection*
21566 print value of variable.
21567 \layout Subsubsection*
21572 load the given file name.
21573 Note this is an alternate method of loading file for debugging.
21574 \layout Subsubsection*
21579 print information about current frame.
21580 \layout Subsubsection*
21585 Toggle between C source & assembly source.
21586 \layout Subsubsection*
21588 ! simulator command
21591 Send the string following '!' to the simulator, the simulator response is
21593 Note the debugger does not interpret the command being sent to the simulator,
21594 so if a command like 'go' is sent the debugger can loose its execution
21595 context and may display incorrect values.
21596 \layout Subsubsection*
21603 My name is Bobby Brown"
21606 Interfacing SDCDB with DDD
21609 The screenshot was converted from png to eps with:
21610 \begin_inset Quotes sld
21613 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
21614 \begin_inset Quotes srd
21617 which produces a pretty compact eps file which is free from compression
21621 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
21622 as this broke the build system on Sourceforge (pdf-file was broken).
21628 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
21634 shows a screenshot of a debugging session with DDD
21635 \begin_inset LatexCommand \index{DDD (debugger)}
21639 (Unix only) on a simulated 8032.
21640 The debugging session might not run as smoothly as the screenshot suggests.
21641 The debugger allows setting of breakpoints, displaying and changing variables,
21642 single stepping through C and assembler code.
21645 The source was compiled with
21668 -debug ddd_example.c
21681 and DDD was invoked with
21688 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21691 Interfacing SDCDB with XEmacs
21692 \begin_inset LatexCommand \index{XEmacs}
21697 \begin_inset LatexCommand \index{Emacs}
21704 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21705 sdcdb.el and sdcdbsrc.el.
21706 These two files can be found in the $(prefix)/bin directory after the installat
21708 These files need to be loaded into XEmacs for the interface to work.
21709 This can be done at XEmacs startup time by inserting the following into
21710 your '.xemacs' file (which can be found in your HOME directory):
21716 (load-file sdcdbsrc.el)
21722 .xemacs is a lisp file so the () around the command is REQUIRED.
21723 The files can also be loaded dynamically while XEmacs is running, set the
21724 environment variable 'EMACSLOADPATH' to the installation bin directory
21725 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21726 To start the interface enter the following command:
21740 You will prompted to enter the file name to be debugged.
21745 The command line options that are passed to the simulator directly are bound
21746 to default values in the file sdcdbsrc.el.
21747 The variables are listed below, these values maybe changed as required.
21750 sdcdbsrc-cpu-type '51
21753 sdcdbsrc-frequency '11059200
21756 sdcdbsrc-serial nil
21759 The following is a list of key mapping for the debugger interface.
21770 ;;key\SpecialChar ~
21784 binding\SpecialChar ~
21808 ;;---\SpecialChar ~
21822 -------\SpecialChar ~
21864 sdcdb-next-from-src\SpecialChar ~
21892 sdcdb-back-from-src\SpecialChar ~
21920 sdcdb-cont-from-src\SpecialChar ~
21930 SDCDB continue command
21948 sdcdb-step-from-src\SpecialChar ~
21976 sdcdb-whatis-c-sexp\SpecialChar ~
21986 SDCDB ptypecommand for data at
22053 sdcdbsrc-delete\SpecialChar ~
22067 SDCDB Delete all breakpoints if no arg
22116 given or delete arg (C-u arg x)
22134 sdcdbsrc-frame\SpecialChar ~
22149 SDCDB Display current frame if no arg,
22198 given or display frame arg
22265 sdcdbsrc-goto-sdcdb\SpecialChar ~
22275 Goto the SDCDB output buffer
22293 sdcdb-print-c-sexp\SpecialChar ~
22304 SDCDB print command for data at
22371 sdcdbsrc-goto-sdcdb\SpecialChar ~
22381 Goto the SDCDB output buffer
22399 sdcdbsrc-mode\SpecialChar ~
22415 Toggles Sdcdbsrc mode (turns it off)
22430 sdcdb-finish-from-src\SpecialChar ~
22438 SDCDB finish command
22453 sdcdb-break\SpecialChar ~
22471 Set break for line with point
22486 sdcdbsrc-mode\SpecialChar ~
22502 Toggle Sdcdbsrc mode
22517 sdcdbsrc-srcmode\SpecialChar ~
22540 Here are a few guidelines that will help the compiler generate more efficient
22541 code, some of the tips are specific to this compiler others are generally
22542 good programming practice.
22545 Use the smallest data type to represent your data-value.
22546 If it is known in advance that the value is going to be less than 256 then
22547 use an 'unsigned char' instead of a 'short' or 'int'.
22548 Please note, that ANSI C requires both signed and unsigned chars to be
22549 promoted to 'signed int'
22550 \begin_inset LatexCommand \index{promotion to signed int}
22554 before doing any operation.
22556 \begin_inset LatexCommand \index{type promotion}
22561 \begin_inset LatexCommand \label{type promotion}
22565 can be omitted, if the result is the same.
22566 The effect of the promotion rules together with the sign-extension is often
22573 unsigned char uc = 0xfe;
22575 if (uc * uc < 0) /* this is true! */
22594 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
22604 (unsigned char) -12 / (signed char) -3 = ...
22607 No, the result is not 4:
22612 (int) (unsigned char) -12 / (int) (signed char) -3 =
22614 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
22616 (int) 0x00f4 / (int) 0xfffd =
22618 (int) 0x00f4 / (int) 0xfffd =
22620 (int) 244 / (int) -3 =
22622 (int) -81 = (int) 0xffaf;
22625 Don't complain, that gcc gives you a different result.
22626 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
22627 Therefore the results are different.
22630 \begin_inset Quotes sld
22634 \begin_inset Quotes srd
22640 If well-defined overflow characteristics are important and negative values
22641 are not, or if you want to steer clear of sign-extension problems when
22642 manipulating bits or bytes, use one of the corresponding unsigned types.
22643 (Beware when mixing signed and unsigned values in expressions, though.)
22645 Although character types (especially unsigned char) can be used as "tiny"
22646 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
22647 ble sign extension and increased code size.
22651 Use unsigned when it is known in advance that the value is not going to
22653 This helps especially if you are doing division or multiplication, bit-shifting
22654 or are using an array index.
22657 NEVER jump into a LOOP.
22660 Declare the variables to be local
22661 \begin_inset LatexCommand \index{local variables}
22665 whenever possible, especially loop control variables (induction).
22668 Since the compiler does not always do implicit integral promotion, the programme
22669 r should do an explicit cast when integral promotion is required.
22672 Reducing the size of division, multiplication & modulus operations can reduce
22673 code size substantially.
22674 Take the following code for example.
22680 foobar(unsigned int p1, unsigned char ch)
22688 unsigned char ch1 = p1 % ch ;
22699 For the modulus operation the variable ch will be promoted to unsigned int
22700 first then the modulus operation will be performed (this will lead to a
22701 call to support routine _moduint()), and the result will be casted to a
22703 If the code is changed to
22708 foobar(unsigned int p1, unsigned char ch)
22716 unsigned char ch1 = (unsigned char)p1 % ch ;
22727 It would substantially reduce the code generated (future versions of the
22728 compiler will be smart enough to detect such optimization opportunities).
22732 Have a look at the assembly listing to get a
22733 \begin_inset Quotes sld
22737 \begin_inset Quotes srd
22740 for the code generation.
22743 Porting code from or to other compilers
22746 check whether endianness of the compilers differs and adapt where needed.
22749 check the device specific header files for compiler specific syntax.
22750 Eventually include the file <compiler.h
22751 \begin_inset LatexCommand \index{compiler.h (include file)}
22755 > to allow using common header files.
22758 check whether the startup code contains the correct initialization (watchdog,
22762 check whether the sizes of short, int, long match.
22765 check if some 16 or 32 bit hardware registers require a specific addressing
22766 order (least significant or most significant byte first) and adapt if needed
22775 relate to time and not to lower/upper memory location here, so this is
22780 the same as endianness).
22783 check whether the keyword
22787 is used where needed.
22788 The compilers might differ in their optimization characteristics (as different
22789 versions of the same compiler might also use more clever optimizations
22790 this is good idea anyway).
22793 check that the compilers are not told to supress warnings.
22796 check and convert compiler specific extensions (interrupts, memory areas,
22800 check for differences in type promotion (especially check for math operations
22801 on char variables and for the use of the ~\SpecialChar ~
22802 operator on bit variables.
22804 \begin_inset LatexCommand \ref{type promotion}
22809 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
22816 check the assembly code generated for interrupt routines (f.e.
22817 for calls to possibly non-reentrant library functions).
22820 check whether timing loops result in proper timing (or preferably consider
22821 a rewrite of the code with timer based delays instead).
22824 check for differences in printf parameters (some compilers push (va_arg
22825 \begin_inset LatexCommand \index{va\_arg}
22829 ) char variables as integers others as char).
22832 check the resulting memory layout.
22836 \begin_inset LatexCommand \index{Tools}
22840 included in the distribution
22844 \begin_inset Tabular
22845 <lyxtabular version="3" rows="12" columns="3">
22847 <column alignment="left" valignment="top" leftline="true" width="0pt">
22848 <column alignment="left" valignment="top" leftline="true" width="0pt">
22849 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22850 <row topline="true" bottomline="true">
22851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22867 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22876 <row topline="true">
22877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22890 Simulator for various architectures
22893 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22902 <row topline="true">
22903 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22911 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22916 header file conversion
22919 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22924 sdcc/support/scripts
22928 <row topline="true">
22929 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22937 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22942 header file conversion
22945 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22950 sdcc/support/scripts
22954 <row topline="true">
22955 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22971 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22989 <row topline="true">
22990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23024 <row topline="true">
23025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23033 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23059 <row topline="true">
23060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23068 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23094 <row topline="true">
23095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23111 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23129 <row topline="true">
23130 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23146 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23164 <row topline="true">
23165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23181 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23199 <row topline="true" bottomline="true">
23200 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23244 \begin_inset LatexCommand \index{Documentation}
23248 included in the distribution
23252 \begin_inset Tabular
23253 <lyxtabular version="3" rows="10" columns="2">
23255 <column alignment="left" valignment="top" leftline="true" width="0">
23256 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23257 <row topline="true" bottomline="true">
23258 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23266 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23271 Where to get / filename
23275 <row topline="true">
23276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23281 SDCC Compiler User Guide
23284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23289 You're reading it right now
23293 <row topline="true">
23294 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23302 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23311 <row topline="true">
23312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23318 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23323 \begin_inset LatexCommand \index{Assembler documentation}
23327 Assemblers and ASLINK
23328 \begin_inset LatexCommand \index{aslink}
23333 \begin_inset LatexCommand \index{Linker documentation}
23340 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23345 sdcc/as/doc/asxhtm.html
23349 <row topline="true">
23350 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23355 SDCC regression test
23356 \begin_inset LatexCommand \index{Regression test}
23363 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23368 sdcc/doc/test_suite_spec.pdf
23372 <row topline="true">
23373 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23381 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23390 <row topline="true">
23391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23396 Notes on debugging with sdcdb
23397 \begin_inset LatexCommand \index{sdcdb (debugger)}
23404 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23409 sdcc/debugger/README
23413 <row topline="true">
23414 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23419 Software simulator for microcontrollers
23422 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23449 <row topline="true">
23450 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23455 Temporary notes on the pic16
23456 \begin_inset LatexCommand \index{PIC16}
23463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23468 sdcc/src/pic16/NOTES
23472 <row topline="true" bottomline="true">
23473 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23478 SDCC internal documentation (debugging file format)
23481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23517 Related open source tools
23518 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
23523 \begin_inset LatexCommand \index{Related tools}
23531 \begin_inset Tabular
23532 <lyxtabular version="3" rows="14" columns="3">
23534 <column alignment="left" valignment="top" leftline="true" width="0pt">
23535 <column alignment="block" valignment="top" leftline="true" width="30line%">
23536 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23537 <row topline="true" bottomline="true">
23538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23563 <row topline="true">
23564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23570 \begin_inset LatexCommand \index{gpsim (pic simulator)}
23577 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23585 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23591 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
23599 <row topline="true">
23600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23606 \begin_inset LatexCommand \index{gputils (pic tools)}
23613 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23627 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
23635 <row topline="true">
23636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23652 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23658 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
23666 <row topline="true">
23667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23680 Tools for Silicon Laboratories JTAG debug adapter, partly based on sdcdb
23684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23690 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
23698 <row topline="true">
23699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23705 \begin_inset LatexCommand \index{indent (source formatting tool)}
23712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23717 Formats C source - Master of the white spaces
23720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23726 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
23734 <row topline="true">
23735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23741 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
23748 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23753 Object file conversion, checksumming, ...
23756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23762 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
23770 <row topline="true">
23771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23777 \begin_inset LatexCommand \index{objdump (tool)}
23784 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23789 Object file conversion, ...
23792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23797 Part of binutils (should be there anyway)
23801 <row topline="true">
23802 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23810 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23815 8051 monitor (hex up-/download, single step, disassemble)
23818 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23824 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
23832 <row topline="true">
23833 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23839 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23851 Source code documentation system
23854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23860 \begin_inset LatexCommand \url{http://www.doxygen.org}
23868 <row topline="true">
23869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23882 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
23885 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23891 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23899 <row topline="true">
23900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23913 8051 monitor (hex up-/download, single step, disassemble)
23916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23922 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
23930 <row topline="true">
23931 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23937 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23949 Statically checks c sources (see
23950 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23957 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23963 \begin_inset LatexCommand \url{http://www.splint.org}
23971 <row topline="true" bottomline="true">
23972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23978 \begin_inset LatexCommand \index{ddd (debugger)}
23985 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23990 Debugger, serves nicely as GUI to sdcdb
23991 \begin_inset LatexCommand \index{sdcdb (debugger)}
23998 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24004 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
24021 Related documentation / recommended reading
24025 \begin_inset Tabular
24026 <lyxtabular version="3" rows="8" columns="3">
24028 <column alignment="left" valignment="top" leftline="true" width="0pt">
24029 <column alignment="block" valignment="top" leftline="true" width="30line%">
24030 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24031 <row topline="true" bottomline="true">
24032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24048 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24057 <row topline="true">
24058 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24081 \begin_inset LatexCommand \index{C Reference card}
24088 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24094 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24102 <row topline="true">
24103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24119 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24125 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24133 <row topline="true">
24134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24142 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24148 \begin_inset Quotes sld
24152 \begin_inset Quotes srd
24158 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24166 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24174 <row topline="true">
24175 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24189 \begin_inset Quotes sld
24192 Extensions for Embedded C
24193 \begin_inset Quotes srd
24199 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24207 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24215 <row topline="true">
24216 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24223 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24228 Latest datasheet of the target CPU
24231 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24240 <row topline="true">
24241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24253 Revision history of datasheet
24256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24265 <row topline="true" bottomline="true">
24266 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24281 Advanced Compiler Design and Implementation
24284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24289 bookstore (very dedicated, probably read other books first)
24305 Some questions answered, some pointers given - it might be time to in turn
24313 can you solve your project with the selected microcontroller? Would you
24314 find out early or rather late that your target is too small/slow/whatever?
24315 Can you switch to a slightly better device if it doesn't fit?
24318 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
24319 and/or another programming language be more adequate? Would an operating
24320 system on the target device help?
24323 if you solved the problem, will the marketing department be happy?
24326 if the marketing department is happy, will customers be happy?
24329 if you're the project manager, marketing department and maybe even the customer
24330 in one person, have you tried to see the project from the outside?
24333 is the project done if you think it is done? Or is just that other interface/pro
24334 tocol/feature/configuration/option missing? How about website, manual(s),
24335 internationali(z|s)ation, packaging, labels, 2nd source for components,
24336 electromagnetic compatability/interference, documentation for production,
24337 production test software, update mechanism, patent issues?
24340 is your project adequately positioned in that magic triangle: fame, fortune,
24344 Maybe not all answers to these questions are known and some answers may
24349 , nevertheless knowing these questions may help you to avoid burnout
24355 burnout is bad for electronic devices, programmers and motorcycle tyres
24359 Chances are you didn't want to hear some of them...
24363 \begin_inset LatexCommand \index{Support}
24370 SDCC has grown to be a large project.
24371 The compiler alone (without the preprocessor, assembler and linker) is
24372 well over 100,000 lines of code (blank stripped).
24373 The open source nature of this project is a key to its continued growth
24375 You gain the benefit and support of many active software developers and
24377 Is SDCC perfect? No, that's why we need your help.
24378 The developers take pride in fixing reported bugs.
24379 You can help by reporting the bugs and helping other SDCC users.
24380 There are lots of ways to contribute, and we encourage you to take part
24381 in making SDCC a great software package.
24385 The SDCC project is hosted on the SDCC sourceforge site at
24386 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
24391 You'll find the complete set of mailing lists
24392 \begin_inset LatexCommand \index{Mailing list(s)}
24396 , forums, bug reporting system, patch submission
24397 \begin_inset LatexCommand \index{Patch submission}
24402 \begin_inset LatexCommand \index{download}
24406 area and Subversion code repository
24407 \begin_inset LatexCommand \index{Subversion code repository}
24415 \begin_inset LatexCommand \index{Bug reporting}
24420 \begin_inset LatexCommand \index{Reporting bugs}
24427 The recommended way of reporting bugs is using the infrastructure of the
24429 You can follow the status of bug reports there and have an overview about
24433 Bug reports are automatically forwarded to the developer mailing list and
24434 will be fixed ASAP.
24435 When reporting a bug, it is very useful to include a small test program
24436 (the smaller the better) which reproduces the problem.
24437 If you can isolate the problem by looking at the generated assembly code,
24438 this can be very helpful.
24439 Compiling your program with the -
24450 \begin_inset LatexCommand \index{-\/-dumpall}
24454 option can sometimes be useful in locating optimization problems.
24455 When reporting a bug please maker sure you:
24458 Attach the code you are compiling with SDCC.
24462 Specify the exact command you use to run SDCC, or attach your Makefile.
24466 Specify the SDCC version (type "
24472 "), your platform, and operating system.
24476 Provide an exact copy of any error message or incorrect output.
24480 Put something meaningful in the subject of your message.
24483 Please attempt to include these 5 important parts, as applicable, in all
24484 requests for support or when reporting any problems or bugs with SDCC.
24485 Though this will make your message lengthy, it will greatly improve your
24486 chance that SDCC users and developers will be able to help you.
24487 Some SDCC developers are frustrated by bug reports without code provided
24488 that they can use to reproduce and ultimately fix the problem, so please
24489 be sure to provide sample code if you are reporting a bug!
24492 Please have a short check that you are using a recent version of SDCC and
24493 the bug is not yet known.
24494 This is the link for reporting bugs:
24495 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
24502 Requesting Features
24503 \begin_inset LatexCommand \label{sub:Requesting-Features}
24508 \begin_inset LatexCommand \index{Feature request}
24513 \begin_inset LatexCommand \index{Requesting features}
24520 Like bug reports feature requests are forwarded to the developer mailing
24522 This is the link for requesting features:
24523 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24533 Like bug reports contributed patches are forwarded to the developer mailing
24535 This is the link for submitting patches
24536 \begin_inset LatexCommand \index{Patch submission}
24541 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
24548 You need to specify some parameters to the
24552 command for the patches to be useful.
24553 If you modified more than one file a patch created f.e.
24558 \begin_inset Quotes sld
24561 diff -Naur unmodified_directory modified_directory >my_changes.patch
24562 \begin_inset Quotes srd
24568 will be fine, otherwise
24572 \begin_inset Quotes sld
24575 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
24576 \begin_inset Quotes srd
24589 These links should take you directly to the
24590 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
24600 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
24601 automated messages (mid 2003)
24605 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
24610 \begin_inset LatexCommand \index{Mailing list(s)}
24614 and forums are archived and searchable so if you are lucky someone already
24615 had a similar problem.
24616 While mails to the lists themselves are delivered promptly their web front
24617 end on sourceforge sometimes shows a severe time lag (up to several weeks),
24618 if you're seriously using SDCC please consider subscribing to the lists.
24624 You can follow the status of the Subversion version
24625 \begin_inset LatexCommand \index{version}
24629 of SDCC by watching the Changelog
24630 \begin_inset LatexCommand \index{Changelog}
24634 in the Subversion repository
24637 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
24644 Subversion Source Code Repository
24653 or the filenames of the snapshot versions of SDCC include date and its
24655 \begin_inset LatexCommand \index{Subversion}
24660 Subversion allows to download the source of recent or previous versions
24662 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
24666 (by number or by date).
24667 An on-line source code browser and detailled instructions are also available
24669 SDCC versions starting from 1999 up to now are available (currently the
24670 versions prior to the conversion from cvs to Subversion (April 2006) are
24671 either by accessible by Subversion or by cvs).
24675 \begin_inset LatexCommand \index{Release policy}
24682 Historically there often were long delays between official releases and
24683 the sourceforge download area tends to get not updated at all.
24684 Excuses in the past might have referred to problems with live range analysis,
24685 but as this was fixed a while ago, the current problem is that another
24686 excuse has to be found.
24687 Kidding aside, we have to get better there! On the other hand there are
24688 daily snapshots available at
24689 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
24693 , and you can always build the very last version (hopefully with many bugs
24694 fixed, and features added) from the source code available at
24695 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
24703 \begin_inset LatexCommand \index{Examples}
24710 You'll find some small examples in the directory
24712 sdcc/device/examples/.
24715 More examples and libraries are available at
24717 The SDCC Open Knowledge Resource
24718 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
24725 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
24732 I did insert a reference to Paul's web site here although it seems rather
24733 dedicated to a specific 8032 board (I think it's okay because it f.e.
24734 shows LCD/Harddisc interface and has a free 8051 monitor.
24735 Independent 8032 board vendors face hard competition of heavily subsidized
24736 development boards anyway).
24739 Maybe we should include some links to real world applications.
24740 Preferably pointer to pointers (one for each architecture) so this stays
24745 \begin_inset LatexCommand \index{Quality control}
24752 The compiler is passed through nightly compile and build checks.
24758 \begin_inset LatexCommand \index{Regression test}
24762 check that SDCC itself compiles flawlessly on several platforms and checks
24763 the quality of the code generated by SDCC by running the code through simulator
24765 There is a separate document
24768 \begin_inset LatexCommand \index{Test suite}
24777 You'll find the test code in the directory
24779 sdcc/support/regression
24782 You can run these tests manually by running
24786 in this directory (or f.e.
24791 \begin_inset Quotes sld
24795 \begin_inset Quotes srd
24801 if you don't want to run the complete tests).
24802 The test code might also be interesting if you want to look for examples
24803 \begin_inset LatexCommand \index{Examples}
24807 checking corner cases of SDCC or if you plan to submit patches
24808 \begin_inset LatexCommand \index{Patch submission}
24815 The pic port uses a different set of regression tests, you'll find them
24818 sdcc/src/regression
24823 Use of SDCC in Education
24836 the phrase "use in education" might evoke the association "
24840 fit for use in education".
24841 This connotation is not intended but nevertheless risked as the licensing
24842 of SDCC makes it difficult to offer educational discounts
24846 If your rationales are to:
24849 give students a chance to understand the
24853 steps of code generation
24856 have a curriculum that can be extended for years.
24857 Then you could use an fpga board as target and your curriculum will seamlessly
24858 extend from logic synthesis (
24859 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
24864 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
24868 ), over assembly programming, to C to FPGA compilers (
24869 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
24876 be able to insert excursions about skills like using a revision control
24877 system, submitting/applying patches, using a type-setting (as opposed to
24878 word-processing) engine LyX/LaTeX, using
24879 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
24884 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
24888 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
24889 Source Software, CPU simulation, compiler regression tests
24890 \begin_inset LatexCommand \index{Regression test}
24897 And if there should be a shortage of ideas then you can always point students
24898 to the ever-growing feature request list
24899 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24906 not tie students to a specific host platform and instead allow them to use
24911 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
24913 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
24920 not encourage students to use illegal copies of educational software
24923 be immune to licensing/availability/price changes of the chosen tool chain
24926 be able to change to a new target platform without having to adopt a new
24930 have complete control over and insight into the tool chain
24933 make your students aware about the pros and cons of open source software
24937 give back to the public as you are probably at least partially publically
24941 give students a chance to publically prove their skills and to possibly
24942 see a world wide impact
24945 then SDCC is probably among the first choices.
24946 Well, probably SDCC might be the only choice.
24949 SDCC Technical Data
24953 \begin_inset LatexCommand \index{Optimizations}
24960 SDCC performs a host of standard optimizations in addition to some MCU specific
24965 Sub-expression Elimination
24966 \begin_inset LatexCommand \index{Subexpression elimination}
24973 The compiler does local and
24999 will be translated to
25011 Some subexpressions are not as obvious as the above example, e.g.:
25021 In this case the address arithmetic a->b[i] will be computed only once;
25022 the equivalent code in C would be.
25034 The compiler will try to keep these temporary variables in registers.
25037 Dead-Code Elimination
25038 \begin_inset LatexCommand \index{Dead-code elimination}
25059 i = 1; \SpecialChar ~
25068 global = 1;\SpecialChar ~
25081 global = 3;\SpecialChar ~
25106 \begin_inset LatexCommand \index{Copy propagation}
25162 Note: the dead stores created by this copy propagation will be eliminated
25163 by dead-code elimination.
25167 \begin_inset LatexCommand \index{Loop optimization}
25172 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
25179 Two types of loop optimizations are done by SDCC
25187 of loop induction variables.
25188 In addition to the strength reduction the optimizer marks the induction
25189 variables and the register allocator tries to keep the induction variables
25190 in registers for the duration of the loop.
25191 Because of this preference of the register allocator
25192 \begin_inset LatexCommand \index{Register allocation}
25196 , loop induction optimization causes an increase in register pressure, which
25197 may cause unwanted spilling of other temporary variables into the stack
25198 \begin_inset LatexCommand \index{stack}
25203 The compiler will generate a warning message when it is forced to allocate
25204 extra space either on the stack or data space.
25205 If this extra space allocation is undesirable then induction optimization
25206 can be eliminated either for the entire source file (with -
25216 -noinduction option) or for a given function only using #pragma\SpecialChar ~
25218 \begin_inset LatexCommand \index{\#pragma noinduction}
25231 for (i = 0 ; i < 100 ; i ++)
25247 for (i = 0; i < 100; i++)
25256 As mentioned previously some loop invariants are not as apparent, all static
25257 address computations are also moved out of the loop.
25262 \begin_inset LatexCommand \index{Strength reduction}
25266 , this optimization substitutes an expression by a cheaper expression:
25271 for (i=0;i < 100; i++)
25289 for (i=0;i< 100;i++) {
25295 ar[itemp1] = itemp2;
25312 The more expensive multiplication
25313 \begin_inset LatexCommand \index{Multiplication}
25317 is changed to a less expensive addition.
25321 \begin_inset LatexCommand \index{Loop reversing}
25328 This optimization is done to reduce the overhead of checking loop boundaries
25329 for every iteration.
25330 Some simple loops can be reversed and implemented using a
25331 \begin_inset Quotes eld
25334 decrement and jump if not zero
25335 \begin_inset Quotes erd
25339 SDCC checks for the following criterion to determine if a loop is reversible
25340 (note: more sophisticated compilers use data-dependency analysis to make
25341 this determination, SDCC uses a more simple minded analysis).
25344 The 'for' loop is of the form
25350 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
25360 The <for body> does not contain
25361 \begin_inset Quotes eld
25365 \begin_inset Quotes erd
25369 \begin_inset Quotes erd
25375 All goto's are contained within the loop.
25378 No function calls within the loop.
25381 The loop control variable <sym> is not assigned any value within the loop
25384 The loop control variable does NOT participate in any arithmetic operation
25388 There are NO switch statements in the loop.
25391 Algebraic Simplifications
25394 SDCC does numerous algebraic simplifications, the following is a small sub-set
25395 of these optimizations.
25400 i = j + 0;\SpecialChar ~
25404 /* changed to: */\SpecialChar ~
25410 i /= 2;\SpecialChar ~
25417 /* changed to: */\SpecialChar ~
25423 i = j - j;\SpecialChar ~
25427 /* changed to: */\SpecialChar ~
25433 i = j / 1;\SpecialChar ~
25437 /* changed to: */\SpecialChar ~
25444 Note the subexpressions
25445 \begin_inset LatexCommand \index{Subexpression}
25449 given above are generally introduced by macro expansions or as a result
25450 of copy/constant propagation.
25453 'switch' Statements
25454 \begin_inset LatexCommand \label{sub:'switch'-Statements}
25459 \begin_inset LatexCommand \index{switch statement}
25466 SDCC can optimize switch statements to jump tables
25467 \begin_inset LatexCommand \index{jump tables}
25472 It makes the decision based on an estimate of the generated code size.
25473 SDCC is quite liberal in the requirements for jump table generation:
25476 The labels need not be in order, and the starting number need not be one
25477 or zero, the case labels are in numerical sequence or not too many case
25478 labels are missing.
25484 switch(i) {\SpecialChar ~
25515 case 4: ...\SpecialChar ~
25547 case 5: ...\SpecialChar ~
25579 case 3: ...\SpecialChar ~
25610 case 6: ...\SpecialChar ~
25642 case 7: ...\SpecialChar ~
25674 case 8: ...\SpecialChar ~
25706 case 9: ...\SpecialChar ~
25738 case 10: ...\SpecialChar ~
25769 case 11: ...\SpecialChar ~
25836 Both the above switch statements will be implemented using a jump-table.
25837 The example to the right side is slightly more efficient as the check for
25838 the lower boundary of the jump-table is not needed.
25842 The number of case labels is not larger than supported by the target architectur
25846 If the case labels are not in numerical sequence ('gaps' between cases)
25847 SDCC checks whether a jump table with additionally inserted dummy cases
25848 is still attractive.
25852 If the starting number is not zero and a check for the lower boundary of
25853 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
25858 Switch statements which have large gaps in the numeric sequence or those
25859 that have too many case labels can be split into more than one switch statement
25860 for efficient code generation, e.g.:
25940 If the above switch statement is broken down into two switch statements
26030 then both the switch statements will be implemented using jump-tables whereas
26031 the unmodified switch statement will not be.
26034 There might be reasons which SDCC cannot know about to either favour or
26035 not favour jump tables.
26036 If the target system has to be as quick for the last switch case as for
26037 the first (pro jump table), or if the switch argument is known to be zero
26038 in the majority of the cases (contra jump table).
26041 The pragma nojtbound
26042 \begin_inset LatexCommand \index{\#pragma nojtbound}
26046 can be used to turn off checking the
26059 It has no effect if a default label is supplied.
26060 Use of this pragma is dangerous: if the switch
26061 \begin_inset LatexCommand \index{switch statement}
26065 argument is not matched by a case statement the processor will happily
26069 Bit-shifting Operations
26070 \begin_inset LatexCommand \index{Bit shifting}
26077 Bit shifting is one of the most frequently used operation in embedded programmin
26079 SDCC tries to implement bit-shift operations in the most efficient way
26095 generates the following code:
26112 In general SDCC will never setup a loop if the shift count is known.
26155 \begin_inset LatexCommand \index{Bit rotation}
26162 A special case of the bit-shift operation is bit rotation
26163 \begin_inset LatexCommand \index{rotating bits}
26167 , SDCC recognizes the following expression to be a left bit-rotation:
26177 char i;\SpecialChar ~
26188 /* unsigned is needed for rotation */
26193 i = ((i << 1) | (i >> 7));
26202 will generate the following code:
26221 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
26222 ns of this case will also be recognized as bit-rotation, i.e.:
26227 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
26230 Nibble and Byte Swapping
26233 Other special cases of the bit-shift operations are nibble or byte swapping
26234 \begin_inset LatexCommand \index{swapping nibbles/bytes}
26238 , SDCC recognizes the following expressions:
26261 i = ((i << 4) | (i >> 4));
26267 j = ((j << 8) | (j >> 8));
26270 and generates a swap instruction for the nibble swapping
26271 \begin_inset LatexCommand \index{Nibble swapping}
26275 or move instructions for the byte swapping
26276 \begin_inset LatexCommand \index{Byte swapping}
26282 \begin_inset Quotes sld
26286 \begin_inset Quotes srd
26289 example can be used to convert from little to big-endian or vice versa.
26290 If you want to change the endianness of a
26294 integer you have to cast to
26301 Note that SDCC stores numbers in little-endian
26307 Usually 8-bit processors don't care much about endianness.
26308 This is not the case for the standard 8051 which only has an instruction
26314 \begin_inset LatexCommand \index{DPTR}
26322 so little-endian is the more efficient byte order.
26326 \begin_inset LatexCommand \index{little-endian}
26331 \begin_inset LatexCommand \index{Endianness}
26336 lowest order first).
26340 \begin_inset LatexCommand \index{Highest Order Bit}
26345 \begin_inset LatexCommand \index{Any Order Bit}
26352 It is frequently required to obtain the highest order bit of an integral
26353 type (long, int, short or char types).
26354 Also obtaining any other order bit is not uncommon.
26355 SDCC recognizes the following expressions to yield the highest order bit
26356 and generates optimized code for it, e.g.:
26369 unsigned char hob1, aob1;
26373 bit hob2, hob3, aob2, aob3;
26382 hob1 = (gint >> 15) & 1;
26386 hob2 = (gint >> 15) & 1;
26390 hob3 = gint & 0x8000;
26394 aob1 = (gint >> 9) & 1;
26398 aob2 = (gint >> 8) & 1;
26402 aob3 = gint & 0x0800;
26412 will generate the following code:
26445 000A E5*01\SpecialChar ~
26472 000C 23\SpecialChar ~
26503 000D 54 01\SpecialChar ~
26530 000F F5*02\SpecialChar ~
26585 0011 E5*01\SpecialChar ~
26612 0013 33\SpecialChar ~
26642 0014 92*00\SpecialChar ~
26697 0016 E5*01\SpecialChar ~
26724 0018 33\SpecialChar ~
26754 0019 92*01\SpecialChar ~
26809 001B E5*01\SpecialChar ~
26836 001D 03\SpecialChar ~
26867 001E 54 01\SpecialChar ~
26894 0020 F5*03\SpecialChar ~
26949 0022 E5*01\SpecialChar ~
26976 0024 13\SpecialChar ~
27006 0025 92*02\SpecialChar ~
27061 0027 E5*01\SpecialChar ~
27088 0029 A2 E3\SpecialChar ~
27115 002B 92*03\SpecialChar ~
27143 Other variations of these cases however will
27148 They are standard C expressions, so I heartily recommend these be the only
27149 way to get the highest order bit, (it is portable).
27150 Of course it will be recognized even if it is embedded in other expressions,
27156 xyz = gint + ((gint >> 15) & 1);
27159 will still be recognized.
27163 \begin_inset LatexCommand \index{Higher Order Byte}
27167 / Higher Order Word
27168 \begin_inset LatexCommand \index{Higher Order Word}
27175 It is also frequently required to obtain a higher order byte or word of
27176 a larger integral type (long, int or short types).
27177 SDCC recognizes the following expressions to yield the higher order byte
27178 or word and generates optimized code for it, e.g.:
27185 unsigned long int glong;
27193 unsigned char hob1, hob2;
27197 unsigned int how1, how2;
27206 hob1 = (gint >> 8) & 0xFF;
27210 hob2 = glong >> 24;
27214 how1 = (glong >> 16) & 0xFFFF;
27228 will generate the following code:
27261 0037 85*01*06\SpecialChar ~
27283 _foo_hob1_1_1,(_gint + 1)
27313 003A 85*05*07\SpecialChar ~
27335 _foo_hob2_1_1,(_glong + 3)
27365 003D 85*04*08\SpecialChar ~
27387 _foo_how1_1_1,(_glong + 2)
27389 0040 85*05*09\SpecialChar ~
27411 (_foo_how1_1_1 + 1),(_glong + 3)
27413 0043 85*03*0A\SpecialChar ~
27435 _foo_how2_1_1,(_glong + 1)
27437 0046 85*04*0B\SpecialChar ~
27459 (_foo_how2_1_1 + 1),(_glong + 2)
27462 Again, variations of these cases may
27467 They are standard C expressions, so I heartily recommend these be the only
27468 way to get the higher order byte/word, (it is portable).
27469 Of course it will be recognized even if it is embedded in other expressions,
27475 xyz = gint + ((gint >> 8) & 0xFF);
27478 will still be recognized.
27482 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
27487 \begin_inset LatexCommand \index{Peephole optimizer}
27494 The compiler uses a rule based, pattern matching and re-writing mechanism
27495 for peep-hole optimization.
27500 a peep-hole optimizer by Christopher W.
27501 Fraser (cwfraser\SpecialChar ~
27504 A default set of rules are compiled into the compiler, additional rules
27505 may be added with the
27518 \begin_inset LatexCommand \index{-\/-peep-file}
27525 The rule language is best illustrated with examples.
27549 The above rule will change the following assembly
27550 \begin_inset LatexCommand \index{Assembler routines}
27572 Note: All occurrences of a
27576 (pattern variable) must denote the same string.
27577 With the above rule, the assembly sequence:
27587 will remain unmodified.
27591 Other special case optimizations may be added by the user (via
27607 some variants of the 8051 MCU
27608 \begin_inset LatexCommand \index{MCS51 variants}
27621 The following two rules will change all
27640 replace { lcall %1 } by { acall %1 }
27642 replace { ljmp %1 } by { ajmp %1 }
27647 inline-assembler code
27649 is also passed through the peep hole optimizer, thus the peephole optimizer
27650 can also be used as an assembly level macro expander.
27651 The rules themselves are MCU dependent whereas the rule language infra-structur
27652 e is MCU independent.
27653 Peephole optimization rules for other MCU can be easily programmed using
27658 The syntax for a rule is as follows:
27663 rule := replace [ restart ] '{' <assembly sequence> '
27701 <assembly sequence> '
27719 '}' [if <functionName> ] '
27724 <assembly sequence> := assembly instruction (each instruction including
27725 labels must be on a separate line).
27729 The optimizer will apply to the rules one by one from the top in the sequence
27730 of their appearance, it will terminate when all rules are exhausted.
27731 If the 'restart' option is specified, then the optimizer will start matching
27732 the rules again from the top, this option for a rule is expensive (performance)
27733 , it is intended to be used in situations where a transformation will trigger
27734 the same rule again.
27735 An example of this (not a good one, it has side effects) is the following
27758 Note that the replace pattern cannot be a blank, but can be a comment line.
27759 Without the 'restart' option only the innermost 'pop' 'push' pair would
27760 be eliminated, i.e.:
27790 the restart option the rule will be applied again to the resulting code
27791 and then all the pop-push pairs will be eliminated to yield:
27801 A conditional function can be attached to a rule.
27802 Attaching rules are somewhat more involved, let me illustrate this with
27829 The optimizer does a look-up of a function name table defined in function
27834 in the source file SDCCpeeph.c, with the name
27839 If it finds a corresponding entry the function is called.
27840 Note there can be no parameters specified for these functions, in this
27845 is crucial, since the function
27849 expects to find the label in that particular variable (the hash table containin
27850 g the variable bindings is passed as a parameter).
27851 If you want to code more such functions, take a close look at the function
27852 labelInRange and the calling mechanism in source file SDCCpeeph.c.
27853 Currently implemented are
27855 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
27856 24bitMode, portIsDS390, 24bitModeAndPortDS390
27865 I know this whole thing is a little kludgey, but maybe some day we will
27866 have some better means.
27867 If you are looking at this file, you will see the default rules that are
27868 compiled into the compiler, you can add your own rules in the default set
27869 there if you get tired of specifying the -
27883 \begin_inset LatexCommand \index{ANSI-compliance}
27888 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
27895 The latest publically available version of the standard
27897 ISO/IEC 9899 - Programming languages - C
27899 should be available at:
27900 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
27909 Deviations from the compliance:
27912 functions are not reentrant
27913 \begin_inset LatexCommand \index{reentrant}
27917 unless explicitly declared as such or the
27930 \begin_inset LatexCommand \index{-\/-stack-auto}
27936 command line option is specified.
27939 structures and unions cannot be assigned values directly, cannot be passed
27940 as function parameters or assigned to each other and cannot be a return
27941 value from a function, e.g.:
27967 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
27978 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
28000 return rets;/* is invalid in SDCC although allowed in ANSI */
28006 initialization of structure arrays must be fully braced.
28012 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
28014 struct s { char x } a[] = {{1}, {2}}; /* OK */
28019 \begin_inset LatexCommand \index{long long (not supported)}
28024 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
28032 \begin_inset LatexCommand \index{double (not supported)}
28036 ' precision floating point
28037 \begin_inset LatexCommand \index{Floating point support}
28045 \begin_inset LatexCommand \index{K\&R style}
28049 function declarations are NOT allowed.
28055 foo(i,j) /* this old style of function declarations */
28057 int i,j; /* are valid in ANSI but not valid in SDCC */
28072 Most enhancements in C99 are not supported, f.e.:
28081 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
28088 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
28092 Certain words that are valid identifiers in the standard may be reserved
28093 words in SDCC unless the
28106 \begin_inset LatexCommand \index{-\/-std-c89}
28121 \begin_inset LatexCommand \index{-\/-std-c99}
28127 command line options are used.
28128 These may include (depending on the selected processor): 'at', 'banked',
28129 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
28130 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
28131 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
28133 Compliant equivalents of these keywords are always available in a form
28134 that begin with two underscores
28135 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
28140 '__data' instead of 'data'.
28143 Cyclomatic Complexity
28144 \begin_inset LatexCommand \index{Cyclomatic complexity}
28151 Cyclomatic complexity of a function is defined as the number of independent
28152 paths the program can take during execution of the function.
28153 This is an important number since it defines the number test cases you
28154 have to generate to validate the function.
28155 The accepted industry standard for complexity number is 10, if the cyclomatic
28156 complexity reported by SDCC exceeds 10 you should think about simplification
28157 of the function logic.
28158 Note that the complexity level is not related to the number of lines of
28159 code in a function.
28160 Large functions can have low complexity, and small functions can have large
28166 SDCC uses the following formula to compute the complexity:
28171 complexity = (number of edges in control flow graph) - (number of nodes
28172 in control flow graph) + 2;
28176 Having said that the industry standard is 10, you should be aware that in
28177 some cases it be may unavoidable to have a complexity level of less than
28179 For example if you have switch statement with more than 10 case labels,
28180 each case label adds one to the complexity level.
28181 The complexity level is by no means an absolute measure of the algorithmic
28182 complexity of the function, it does however provide a good starting point
28183 for which functions you might look at for further optimization.
28186 Retargetting for other Processors
28189 The issues for retargetting the compiler are far too numerous to be covered
28191 What follows is a brief description of each of the seven phases of the
28192 compiler and its MCU dependency.
28195 Parsing the source and building the annotated parse tree.
28196 This phase is largely MCU independent (except for the language extensions).
28197 Syntax & semantic checks are also done in this phase, along with some initial
28198 optimizations like back patching labels and the pattern matching optimizations
28199 like bit-rotation etc.
28202 The second phase involves generating an intermediate code which can be easy
28203 manipulated during the later phases.
28204 This phase is entirely MCU independent.
28205 The intermediate code generation assumes the target machine has unlimited
28206 number of registers, and designates them with the name iTemp.
28207 The compiler can be made to dump a human readable form of the code generated
28221 This phase does the bulk of the standard optimizations and is also MCU independe
28223 This phase can be broken down into several sub-phases:
28227 Break down intermediate code (iCode) into basic blocks.
28229 Do control flow & data flow analysis on the basic blocks.
28231 Do local common subexpression elimination, then global subexpression elimination
28233 Dead code elimination
28237 If loop optimizations caused any changes then do 'global subexpression eliminati
28238 on' and 'dead code elimination' again.
28241 This phase determines the live-ranges; by live range I mean those iTemp
28242 variables defined by the compiler that still survive after all the optimization
28244 Live range analysis
28245 \begin_inset LatexCommand \index{Live range analysis}
28249 is essential for register allocation, since these computation determines
28250 which of these iTemps will be assigned to registers, and for how long.
28253 Phase five is register allocation.
28254 There are two parts to this process.
28258 The first part I call 'register packing' (for lack of a better term).
28259 In this case several MCU specific expression folding is done to reduce
28264 The second part is more MCU independent and deals with allocating registers
28265 to the remaining live ranges.
28266 A lot of MCU specific code does creep into this phase because of the limited
28267 number of index registers available in the 8051.
28270 The Code generation phase is (unhappily), entirely MCU dependent and very
28271 little (if any at all) of this code can be reused for other MCU.
28272 However the scheme for allocating a homogenized assembler operand for each
28273 iCode operand may be reused.
28276 As mentioned in the optimization section the peep-hole optimizer is rule
28277 based system, which can reprogrammed for other MCUs.
28281 \begin_inset LatexCommand \index{Compiler internals}
28288 The anatomy of the compiler
28289 \begin_inset LatexCommand \label{sub:The-anatomy-of}
28298 This is an excerpt from an article published in Circuit Cellar Magazine
28304 It's a little outdated (the compiler is much more efficient now and user/develo
28305 per friendly), but pretty well exposes the guts of it all.
28311 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
28312 It is fairly easy to retarget for other 8-bit MCU.
28313 Here we take a look at some of the internals of the compiler.
28318 \begin_inset LatexCommand \index{Parsing}
28325 Parsing the input source file and creating an AST (Annotated Syntax Tree
28326 \begin_inset LatexCommand \index{Annotated syntax tree}
28331 This phase also involves propagating types (annotating each node of the
28332 parse tree with type information) and semantic analysis.
28333 There are some MCU specific parsing rules.
28334 For example the storage classes, the extended storage classes are MCU specific
28335 while there may be a xdata storage class for 8051 there is no such storage
28336 class for z80 or Atmel AVR.
28337 SDCC allows MCU specific storage class extensions, i.e.
28338 xdata will be treated as a storage class specifier when parsing 8051 C
28339 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
28344 \begin_inset LatexCommand \index{iCode}
28351 Intermediate code generation.
28352 In this phase the AST is broken down into three-operand form (iCode).
28353 These three operand forms are represented as doubly linked lists.
28354 ICode is the term given to the intermediate form generated by the compiler.
28355 ICode example section shows some examples of iCode generated for some simple
28356 C source functions.
28360 \begin_inset LatexCommand \index{Optimizations}
28367 Bulk of the target independent optimizations is performed in this phase.
28368 The optimizations include constant propagation, common sub-expression eliminati
28369 on, loop invariant code movement, strength reduction of loop induction variables
28370 and dead-code elimination.
28373 Live range analysis
28374 \begin_inset LatexCommand \index{Live range analysis}
28381 During intermediate code generation phase, the compiler assumes the target
28382 machine has infinite number of registers and generates a lot of temporary
28384 The live range computation determines the lifetime of each of these compiler-ge
28385 nerated temporaries.
28386 A picture speaks a thousand words.
28387 ICode example sections show the live range annotations for each of the
28389 It is important to note here, each iCode is assigned a number in the order
28390 of its execution in the function.
28391 The live ranges are computed in terms of these numbers.
28392 The from number is the number of the iCode which first defines the operand
28393 and the to number signifies the iCode which uses this operand last.
28396 Register Allocation
28397 \begin_inset LatexCommand \index{Register allocation}
28404 The register allocation determines the type and number of registers needed
28406 In most MCUs only a few registers can be used for indirect addressing.
28407 In case of 8051 for example the registers R0 & R1 can be used to indirectly
28408 address the internal ram and DPTR to indirectly address the external ram.
28409 The compiler will try to allocate the appropriate register to pointer variables
28411 ICode example section shows the operands annotated with the registers assigned
28413 The compiler will try to keep operands in registers as much as possible;
28414 there are several schemes the compiler uses to do achieve this.
28415 When the compiler runs out of registers the compiler will check to see
28416 if there are any live operands which is not used or defined in the current
28417 basic block being processed, if there are any found then it will push that
28418 operand and use the registers in this block, the operand will then be popped
28419 at the end of the basic block.
28423 There are other MCU specific considerations in this phase.
28424 Some MCUs have an accumulator; very short-lived operands could be assigned
28425 to the accumulator instead of a general-purpose register.
28431 Figure II gives a table of iCode operations supported by the compiler.
28432 The code generation involves translating these operations into corresponding
28433 assembly code for the processor.
28434 This sounds overly simple but that is the essence of code generation.
28435 Some of the iCode operations are generated on a MCU specific manner for
28436 example, the z80 port does not use registers to pass parameters so the
28437 SEND and RECV iCode operations will not be generated, and it also does
28438 not support JUMPTABLES.
28445 <Where is Figure II?>
28448 In the original article Figure II was announced to be downloadable on
28453 Unfortunately it never seemed to have shown up there, so: where is Figure
28458 \begin_inset LatexCommand \index{iCode}
28465 This section shows some details of iCode.
28466 The example C code does not do anything useful; it is used as an example
28467 to illustrate the intermediate code generated by the compiler.
28479 /* This function does nothing useful.
28486 for the purpose of explaining iCode */
28489 short function (data int *x)
28497 short i=10; \SpecialChar ~
28499 /* dead initialization eliminated */
28504 short sum=10; /* dead initialization eliminated */
28517 while (*x) *x++ = *p++;
28531 /* compiler detects i,j to be induction variables */
28535 for (i = 0, j = 10 ; i < 10 ; i++, j
28561 mul += i * 3; \SpecialChar ~
28563 /* this multiplication remains */
28569 gint += j * 3;\SpecialChar ~
28571 /* this multiplication changed to addition */
28585 In addition to the operands each iCode contains information about the filename
28586 and line it corresponds to in the source file.
28587 The first field in the listing should be interpreted as follows:
28592 Filename(linenumber: iCode Execution sequence number : ICode hash table
28593 key : loop depth of the iCode).
28598 Then follows the human readable form of the ICode operation.
28599 Each operand of this triplet form can be of three basic types a) compiler
28600 generated temporary b) user defined variable c) a constant value.
28601 Note that local variables and parameters are replaced by compiler generated
28604 \begin_inset LatexCommand \index{Live range analysis}
28608 are computed only for temporaries (i.e.
28609 live ranges are not computed for global variables).
28611 \begin_inset LatexCommand \index{Register allocation}
28615 are allocated for temporaries only.
28616 Operands are formatted in the following manner:
28621 Operand Name [lr live-from : live-to ] { type information } [ registers
28627 As mentioned earlier the live ranges are computed in terms of the execution
28628 sequence number of the iCodes, for example
28630 the iTemp0 is live from (i.e.
28631 first defined in iCode with execution sequence number 3, and is last used
28632 in the iCode with sequence number 5).
28633 For induction variables such as iTemp21 the live range computation extends
28634 the lifetime from the start to the end of the loop.
28636 The register allocator used the live range information to allocate registers,
28637 the same registers may be used for different temporaries if their live
28638 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
28639 iTemp17 since their live ranges do not overlap.
28640 In addition the allocator also takes into consideration the type and usage
28641 of a temporary, for example itemp6 is a pointer to near space and is used
28642 as to fetch data from (i.e.
28643 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
28644 Some short lived temporaries are allocated to special registers which have
28645 meaning to the code generator e.g.
28646 iTemp13 is allocated to a pseudo register CC which tells the back end that
28647 the temporary is used only for a conditional jump the code generation makes
28648 use of this information to optimize a compare and jump ICode.
28650 There are several loop optimizations
28651 \begin_inset LatexCommand \index{Loop optimization}
28655 performed by the compiler.
28656 It can detect induction variables iTemp21(i) and iTemp23(j).
28657 Also note the compiler does selective strength reduction
28658 \begin_inset LatexCommand \index{Strength reduction}
28663 the multiplication of an induction variable in line 18 (gint = j * 3) is
28664 changed to addition, a new temporary iTemp17 is allocated and assigned
28665 a initial value, a constant 3 is then added for each iteration of the loop.
28666 The compiler does not change the multiplication
28667 \begin_inset LatexCommand \index{Multiplication}
28671 in line 17 however since the processor does support an 8 * 8 bit multiplication.
28673 Note the dead code elimination
28674 \begin_inset LatexCommand \index{Dead-code elimination}
28678 optimization eliminated the dead assignments in line 7 & 8 to I and sum
28686 Sample.c (5:1:0:0) _entry($9) :
28691 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
28696 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
28701 Sample.c(11:4:53:0) preHeaderLbl0($11) :
28706 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
28712 Sample.c(11:6:5:1) _whilecontinue_0($1) :
28717 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
28723 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28728 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
28734 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
28740 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
28746 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
28752 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
28753 * int}[r0] + 0x2 {short}
28758 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
28763 Sample.c(11:17:21:0)_whilebreak_0($3) :
28768 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28773 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28778 Sample.c(15:20:54:0)preHeaderLbl1($13) :
28783 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28788 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28793 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28798 Sample.c(15:24:26:1)_forcond_0($4) :
28803 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
28809 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28814 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
28815 + ITemp21 [lr21:38]{short}[r4]
28820 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
28826 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
28827 + iTemp15 [lr29:30]{short}[r1]
28832 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
28838 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
28844 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
28850 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
28856 Sample.c(19:38:47:1) goto _forcond_0($4)
28861 Sample.c(19:39:48:0)_forbreak_0($7) :
28866 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
28867 + ITemp11 [lr19:40]{short}[r3]
28872 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
28877 Sample.c(20:42:51:0)_return($8) :
28882 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
28888 Finally the code generated for this function:
28929 ; ----------------------------------------------
28934 ; function function
28939 ; ----------------------------------------------
28949 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
28961 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
28973 ;_whilecontinue_0($1) :
28983 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28988 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
29047 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
29066 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
29113 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
29153 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
29179 ; iTemp6 [lr5:16]{_near * int}[r0] =
29184 ; iTemp6 [lr5:16]{_near * int}[r0] +
29201 ; goto _whilecontinue_0($1)
29213 ; _whilebreak_0($3) :
29223 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29235 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29247 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29259 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29278 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29307 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
29312 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29357 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
29362 ; iTemp21 [lr21:38]{short}[r4]
29388 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
29421 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
29426 ; iTemp15 [lr29:30]{short}[r1]
29445 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
29492 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
29539 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
29551 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
29565 cjne r5,#0xff,00104$
29577 ; goto _forcond_0($4)
29589 ; _forbreak_0($7) :
29599 ; ret iTemp24 [lr40:41]{short}
29642 A few words about basic block successors, predecessors and dominators
29645 Successors are basic blocks
29646 \begin_inset LatexCommand \index{Basic blocks}
29650 that might execute after this basic block.
29652 Predecessors are basic blocks that might execute before reaching this basic
29655 Dominators are basic blocks that WILL execute before reaching this basic
29689 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
29692 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
29695 c) domVect of [BB4] = BB1 ...
29696 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
29704 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
29714 Thanks to all the other volunteer developers who have helped with coding,
29715 testing, web-page creation, distribution sets, etc.
29716 You know who you are :-)
29723 This document was initially written by Sandeep Dutta
29726 All product names mentioned herein may be trademarks
29727 \begin_inset LatexCommand \index{Trademarks}
29731 of their respective companies.
29738 To avoid confusion, the installation and building options for SDCC itself
29739 (chapter 2) are not part of the index.
29743 \begin_inset LatexCommand \printindex{}