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 retargetted for other microprocessors, support for Microchip
146 PIC, Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargettable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
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 detailled 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.
16257 The pragma's are intended to be used to turn-on or off certain optimizations
16258 which might cause the compiler to generate extra stack / data space to
16259 store compiler generated temporary variables.
16260 This usually happens in large functions.
16261 Pragma directives should be used as shown in the following example, they
16262 are used to control options & optimizations for a given function; pragmas
16263 should be placed before and/or after a function, placing pragma's inside
16264 a function body could have unpredictable results.
16270 \begin_inset LatexCommand \index{\#pragma save}
16281 /* save the current settings */
16284 \begin_inset LatexCommand \index{\#pragma nogcse}
16293 /* turnoff global subexpression elimination */
16295 #pragma noinduction
16296 \begin_inset LatexCommand \index{\#pragma noinduction}
16300 /* turn off induction optimizations */
16323 \begin_inset LatexCommand \index{\#pragma restore}
16327 /* turn the optimizations back on */
16330 The compiler will generate a warning message when extra space is allocated.
16331 It is strongly recommended that the save and restore pragma's be used when
16332 changing options for a function.
16335 Defines Created by the Compiler
16338 The compiler creates the following #defines
16339 \begin_inset LatexCommand \index{\#defines}
16344 \begin_inset LatexCommand \index{Defines created by the compiler}
16354 \begin_inset Tabular
16355 <lyxtabular version="3" rows="11" columns="2">
16357 <column alignment="left" valignment="top" leftline="true" width="0">
16358 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
16359 <row topline="true" bottomline="true">
16360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16381 <row topline="true">
16382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16388 \begin_inset LatexCommand \index{SDCC}
16395 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16401 Since version 2.5.6 the version number as an int (ex.
16406 <row topline="true">
16407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16413 \begin_inset LatexCommand \index{SDCC\_mcs51}
16418 \begin_inset LatexCommand \index{SDCC\_ds390}
16423 \begin_inset LatexCommand \index{SDCC\_z80}
16430 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16435 depending on the model used (e.g.: -mds390
16439 <row topline="true">
16440 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16446 \begin_inset LatexCommand \index{\_\_mcs51}
16451 \begin_inset LatexCommand \index{\_\_ds390}
16456 \begin_inset LatexCommand \index{\_\_hc08}
16461 \begin_inset LatexCommand \index{\_\_z80}
16468 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16473 depending on the model used (e.g.
16478 <row topline="true">
16479 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16485 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16515 <row topline="true">
16516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16522 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16552 <row topline="true">
16553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16559 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16589 <row topline="true">
16590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16596 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16603 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16626 <row topline="true">
16627 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16633 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16663 <row topline="true">
16664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16670 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16677 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16690 <row topline="true" bottomline="true">
16691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16697 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16704 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16724 Notes on supported Processors
16728 \begin_inset LatexCommand \label{sub:MCS51-variants}
16733 \begin_inset LatexCommand \index{MCS51 variants}
16740 MCS51 processors are available from many vendors and come in many different
16742 While they might differ considerably in respect to Special Function Registers
16743 the core MCS51 is usually not modified or is kept compatible.
16747 pdata access by SFR
16750 With the upcome of devices with internal xdata and flash memory devices
16752 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16756 as dedicated I/O port is becoming more popular.
16757 Switching the high byte for pdata
16758 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16762 access which was formerly done by port P2 is then achieved by a Special
16764 \begin_inset LatexCommand \index{sfr}
16769 In well-established MCS51 tradition the address of this
16773 is where the chip designers decided to put it.
16774 Needless to say that they didn't agree on a common name either.
16775 So that the startup code can correctly initialize xdata variables, you
16776 should define an sfr with the name _XPAGE
16779 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16785 at the appropriate location if the default, port P2, is not used for this.
16791 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16796 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16801 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16804 For more exotic implementations further customizations may be needed.
16806 \begin_inset LatexCommand \ref{sub:Startup-Code}
16810 for other possibilities.
16813 Other Features available by SFR
16816 Some MCS51 variants offer features like Double DPTR
16817 \begin_inset LatexCommand \index{DPTR}
16821 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16822 These are currently not used for the MCS51 port.
16823 If you absolutely need them you can fall back to inline assembly or submit
16830 The DS80C400 microcontroller has a rich set of peripherals.
16831 In its built-in ROM library it includes functions to access some of the
16832 features, among them is a TCP stack with IP4 and IP6 support.
16833 Library headers (currently in beta status) and other files are provided
16837 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16845 The Z80 and gbz80 port
16848 SDCC can target both the Zilog
16849 \begin_inset LatexCommand \index{Z80}
16853 and the Nintendo Gameboy's Z80-like gbz80
16854 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16859 The Z80 port is passed through the same
16862 \begin_inset LatexCommand \index{Regression test}
16868 as the MCS51 and DS390 ports, so floating point support, support for long
16869 variables and bitfield support is fine.
16870 See mailing lists and forums about interrupt routines.
16873 As always, the code is the authoritative reference - see z80/ralloc.c and
16876 \begin_inset LatexCommand \index{stack}
16880 frame is similar to that generated by the IAR Z80 compiler.
16881 IX is used as the base pointer, HL and IY are used as a temporary registers,
16882 and BC and DE are available for holding variables.
16884 \begin_inset LatexCommand \index{return value}
16888 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16890 The gbz80 port use the same set of registers for the return values, but
16891 in a different order of significance: E (one byte), DE (two bytes), or
16898 The port to the Motorola HC08
16899 \begin_inset LatexCommand \index{HC08}
16903 family has been added in October 2003, and is still undergoing some basic
16905 The code generator is complete, but the register allocation is still quite
16907 Some of the SDCC's standard C library functions have embedded non-HC08
16908 inline assembly and so are not yet usable.
16919 \begin_inset LatexCommand \index{PIC14}
16923 port still requires a major effort from the development community.
16924 However it can work for very simple code.
16927 C code and 14bit PIC code page
16928 \begin_inset LatexCommand \index{code page (pic14)}
16933 \begin_inset LatexCommand \index{RAM bank (pic14)}
16940 The linker organizes allocation for the code page and RAM banks.
16941 It does not have intimate knowledge of the code flow.
16942 It will put all the code section of a single asm file into a single code
16944 In order to make use of multiple code pages, separate asm files must be
16946 The compiler treats all functions of a single C file as being in the same
16947 code page unless it is non static.
16948 The compiler treats all local variables of a single C file as being in
16949 the same RAM bank unless it is an extern.
16953 To get the best follow these guide lines:
16956 make local functions static, as non static functions require code page selection
16960 Make local variables static as extern variables require RAM bank selection
16964 For devices that have multiple code pages it is more efficient to use the
16965 same number of files as pages, i.e.
16966 for the 16F877 use 4 separate files and i.e.
16967 for the 16F874 use 2 separate files.
16968 This way the linker can put the code for each file into different code
16969 pages and the compiler can allocate reusable variables more efficiently
16970 and there's less page selection overhead.
16971 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16972 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16975 Creating a device include file
16978 For generating a device include file use the support perl script inc2h.pl
16979 kept in directory support/script.
16985 For the interrupt function, use the keyword 'interrupt'
16986 \begin_inset LatexCommand \index{interrupt}
16990 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16991 there to avoid a syntax error - it ought to be fixed).
16997 void Intr(void) interrupt 0
17003 T0IF = 0; /* Clear timer interrupt */
17008 Linking and assembling
17011 For assembling you can use either GPUTILS'
17012 \begin_inset LatexCommand \index{gputils (pic tools)}
17016 gpasm.exe or MPLAB's mpasmwin.exe.
17017 GPUTILS is available from
17018 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17023 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17024 If you use MPLAB and an interrupt function then the linker script file
17025 vectors section will need to be enlarged to link with mplink.
17048 sdcc -S -V -mpic14 -p16F877 $<
17062 $(PRJ).hex: $(OBJS)
17072 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17094 sdcc -S -V -mpic14 -p16F877 $<
17104 mpasmwin /q /o $*.asm
17108 $(PRJ).hex: $(OBJS)
17118 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17121 Please note that indentations within a
17125 have to be done with a tabulator character.
17128 Command-line options
17131 Besides the switches common to all SDCC backends, the PIC14 port accepts
17132 the following options (for an updated list see sdcc -
17144 \labelwidthstring 00.00.0000
17156 -debug-extra emit debug info in assembly output
17158 \labelwidthstring 00.00.0000
17170 -no-pcode-opt disable (slightly faulty) optimization on pCode
17174 \layout Subsubsection
17176 error: missing definition for symbol
17177 \begin_inset Quotes sld
17181 \begin_inset Quotes srd
17187 The PIC14 port uses library routines to provide more complex operations
17188 like multiplication, division/modulus and (generic) pointer dereferencing.
17189 In order to add these routines to your project, you must link with PIC14's
17195 For single source file projects this is done automatically, more complex
17200 to the linker's arguments.
17201 Make sure you also add an include path for the library (using the -I switch
17203 \layout Subsubsection
17205 Processor mismatch in file
17206 \begin_inset Quotes sld
17210 \begin_inset Quotes srd
17216 This warning can usually be ignored due to the very good compatibility amongst
17217 14 bit PIC devices.
17220 You might also consider recompiling the library for your specific device
17221 by changing the ARCH=p16f877 (default target) entry in
17223 device/lib/pic/Makefile.in
17227 device/lib/pic/Makefile
17229 to reflect your device.
17230 This might even improve performance for smaller devices as unneccesary
17231 BANKSELs migth be removed.
17235 \layout Subsubsection
17240 Currently, data can only be initialized if it resides in the source file
17246 Data in other source files will silently
17254 \begin_inset LatexCommand \index{PIC16}
17262 \begin_inset LatexCommand \index{PIC16}
17266 port is the portion of SDCC that is responsible to produce code for the
17268 \begin_inset LatexCommand \index{Microchip}
17272 (TM) microcontrollers with 16 bit core.
17273 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17274 Currently supported devices are:
17278 \begin_inset Tabular
17279 <lyxtabular version="3" rows="4" columns="6">
17281 <column alignment="center" valignment="top" leftline="true" width="0">
17282 <column alignment="center" valignment="top" leftline="true" width="0">
17283 <column alignment="center" valignment="top" leftline="true" width="0">
17284 <column alignment="center" valignment="top" leftline="true" width="0">
17285 <column alignment="center" valignment="top" leftline="true" width="0">
17286 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17287 <row topline="true">
17288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17312 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17320 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17328 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17337 <row topline="true">
17338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17378 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17387 <row topline="true">
17388 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17396 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17412 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17420 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17437 <row topline="true" bottomline="true">
17438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17462 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17469 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17476 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17494 PIC16 port supports the standard command line arguments as supposed, with
17495 the exception of certain cases that will be mentioned in the following
17498 \labelwidthstring 00.00.0000
17510 -callee-saves See -
17522 \labelwidthstring 00.00.0000
17534 -all-callee-saves All function arguments are passed on stack by default.
17537 There is no need to specify this in the command line.
17539 \labelwidthstring 00.00.0000
17551 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17552 no local variables.
17555 Port Specific Options
17556 \begin_inset LatexCommand \index{Options PIC16}
17563 The port specific options appear after the global options in the sdcc --help
17565 \layout Subsubsection
17570 General options enable certain port features and optimizations.
17572 \labelwidthstring 00.00.0000
17584 -stack-model=[model] Used in conjuction with the command above.
17585 Defines the stack model to be used, valid stack models are :
17588 \labelwidthstring 00.00.0000
17594 Selects small stack model.
17595 8 bit stack and frame pointers.
17596 Supports 256 bytes stack size.
17598 \labelwidthstring 00.00.0000
17604 Selects large stack model.
17605 16 bit stack and frame pointers.
17606 Supports 65536 bytes stack size.
17609 \labelwidthstring 00.00.0000
17621 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17622 unitialized data variables with [kword].
17623 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17625 \labelwidthstring 00.00.0000
17637 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17638 Useful for bootloaders.
17640 \labelwidthstring 00.00.0000
17652 -asm= sets the full path and name of an external assembler to call.
17654 \labelwidthstring 00.00.0000
17666 -link= sets the full path and name of an external linker to call.
17668 \labelwidthstring 00.00.0000
17680 -mplab-comp MPLAB compatibility option.
17681 Currently only suppresses special gpasm directives.
17682 \layout Subsubsection
17684 Optimization Options
17686 \labelwidthstring 00.00.0000
17698 -optimize-goto Try to use (conditional) BRA instead of GOTO
17700 \labelwidthstring 00.00.0000
17712 -optimize-cmp Try to optimize some compares.
17714 \labelwidthstring 00.00.0000
17726 -optimize-df Analyze the dataflow of the generated code and improve it.
17728 \labelwidthstring 00.00.0000
17740 -obanksel=nn Set optimization level for inserting BANKSELs.
17745 \labelwidthstring 00.00.0000
17749 \labelwidthstring 00.00.0000
17751 1 checks previous used register and if it is the same then does not emit
17752 BANKSEL, accounts only for labels.
17754 \labelwidthstring 00.00.0000
17756 2 tries to check the location of (even different) symbols and removes BANKSELs
17757 if they are in the same bank.
17762 Important: There might be problems if the linker script has data sections
17763 across bank borders!
17765 \layout Subsubsection
17769 \labelwidthstring 00.00.0000
17781 -nodefaultlibs do not link default libraries when linking
17783 \labelwidthstring 00.00.0000
17795 -no-crt Don't link the default run-time modules
17797 \labelwidthstring 00.00.0000
17809 -use-crt= Use a custom run-time module instead of the defaults.
17810 \layout Subsubsection
17815 Debugging options enable extra debugging information in the output files.
17817 \labelwidthstring 00.00.0000
17829 -debug-xtra Similar to -
17840 \begin_inset LatexCommand \index{-\/-debug}
17844 , but dumps more information.
17846 \labelwidthstring 00.00.0000
17858 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17860 <source> is the name of the file compiled.
17862 \labelwidthstring 00.00.0000
17874 -pcode-verbose Enable pcode debugging information in translation.
17876 \labelwidthstring 00.00.0000
17888 -denable-peeps Force the usage of peepholes.
17891 \labelwidthstring 00.00.0000
17903 -gstack Trace push/pops for stack pointer overflow
17905 \labelwidthstring 00.00.0000
17917 -call-tree dump call tree in .calltree file
17920 Enviromental Variables
17923 There is a number of enviromental variables that can be used when running
17924 SDCC to enable certain optimizations or force a specific program behaviour.
17925 these variables are primarily for debugging purposes so they can be enabled/dis
17929 Currently there is only two such variables available:
17931 \labelwidthstring 00.00.0000
17933 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17934 bitfields is optimized by directly loading FSR0 with the address of the
17935 bitfield structure.
17936 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17938 This step saves data ram and code space for functions that perform heavy
17941 80 bytes of code space are saved when compiling malloc.c with this option).
17944 \labelwidthstring 00.00.0000
17946 NO_REG_OPT do not perform pCode registers optimization.
17947 This should be used for debugging purposes.
17948 In some where bugs in the pcode optimizer are found, users can benefit
17949 from temporarily disabling the optimizer until the bug is fixed.
17952 Preprocessor Macros
17955 PIC16 port defines the following preprocessor macros while translating a
17960 \begin_inset Tabular
17961 <lyxtabular version="3" rows="6" columns="2">
17963 <column alignment="center" valignment="top" leftline="true" width="0">
17964 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17965 <row topline="true" bottomline="true">
17966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17974 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17983 <row topline="true">
17984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17997 Port identification
18001 <row topline="true">
18002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18025 Port identification (same as above)
18029 <row topline="true">
18030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18038 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18043 MCU Identification.
18048 is the microcontrol identification number, i.e.
18053 <row topline="true">
18054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18077 MCU Identification (same as above)
18081 <row topline="true" bottomline="true">
18082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18095 nnn = SMALL or LARGE respectively according to the stack model used
18106 In addition the following macros are defined when calling assembler:
18110 \begin_inset Tabular
18111 <lyxtabular version="3" rows="4" columns="2">
18113 <column alignment="center" valignment="top" leftline="true" width="0">
18114 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18115 <row topline="true" bottomline="true">
18116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18124 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18133 <row topline="true">
18134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18147 MCU Identification.
18152 is the microcontrol identification number, i.e.
18157 <row topline="true">
18158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18171 nnn = SMALL or LARGE respectively according to the memory model used for
18176 <row topline="true" bottomline="true">
18177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18190 nnn = SMALL or LARGE respectively according to the stack model used
18205 \begin_inset LatexCommand \index{PIC16}
18209 port uses the following directories for searching header files and libraries.
18213 \begin_inset Tabular
18214 <lyxtabular version="3" rows="3" columns="4">
18216 <column alignment="center" valignment="top" leftline="true" width="0">
18217 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18218 <column alignment="center" valignment="top" width="0">
18219 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18220 <row topline="true" bottomline="true">
18221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18229 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18254 <row topline="true">
18255 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18260 PREFIX/sdcc/include/pic16
18263 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18268 PIC16 specific headers
18271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18279 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18288 <row topline="true" bottomline="true">
18289 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18294 PREFIX/sdcc/lib/pic16
18297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18302 PIC16 specific libraries
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">
18330 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18337 PIC16 port currently supports the following pragmas:
18339 \labelwidthstring 00.00.0000
18341 stack pragma stack forces the code generator to initialize the stack & frame
18342 pointers at a specific address.
18343 This is an adhoc solution for cases where no STACK directive is available
18344 in the linker script or gplink is not instructed to create a stack section.
18346 The stack pragma should be used only once in a project.
18347 Multiple pragmas may result in indeterminate behaviour of the program.
18353 The old format (ie.
18354 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18355 cross page boundaries (or even exceed the available data RAM) and crash
18357 Make sure that stack does not cross page boundaries when using the SMALL
18363 The format is as follows:
18366 #pragma stack bottom_address [stack_size]
18373 is the lower bound of the stack section.
18374 The stack pointer initially will point at address (bottom_address+stack_size-1).
18382 /* initializes stack of 100 bytes at RAM address 0x200 */
18385 #pragma stack 0x200 100
18388 If the stack_size field is omitted then a stack is created with the default
18390 This size might be enough for most programs, but its not enough for operations
18391 with deep function nesting or excessive stack usage.
18393 \labelwidthstring 00.00.0000
18397 This pragma is deprecated.
18398 Its use will cause a warning message to be issued.
18404 \labelwidthstring 00.00.0000
18406 code place a function symbol at static FLASH address
18414 /* place function test_func at 0x4000 */
18417 #pragma code test_func 0x4000
18421 \labelwidthstring 00.00.0000
18423 library instructs the linker to use a library module.
18428 #pragma library module_name
18435 can be any library or object file (including its path).
18436 Note that there are four reserved keywords which have special meaning.
18441 \begin_inset Tabular
18442 <lyxtabular version="3" rows="6" columns="3">
18444 <column alignment="center" valignment="top" leftline="true" width="0">
18445 <column alignment="block" valignment="top" leftline="true" width="20page%">
18446 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18447 <row topline="true" bottomline="true">
18448 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18473 <row topline="true">
18474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18489 ignore all library pragmas
18492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18503 <row topline="true">
18504 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18514 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18535 <row topline="true">
18536 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18551 link the Math libarary
18554 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18567 <row topline="true">
18568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18578 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18583 link the I/O library
18586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18599 <row topline="true" bottomline="true">
18600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18615 link the debug library
18618 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18637 * is the device number, i.e.
18638 452 for PIC18F452 MCU.
18641 This feature allows for linking with specific libraries withoug having to
18642 explicit name them in the command line.
18647 keyword will reject all modules specified by the library pragma.
18649 \labelwidthstring 00.00.0000
18651 udata pragma udata instructs the compiler to emit code so that linker will
18652 place a variable at a specific memory bank
18660 /* places variable foo at bank2 */
18663 #pragma udata bank2 foo
18669 In order for this pragma to work extra SECTION directives should be added
18670 in the .lkr script.
18671 In the following example a sample .lkr file is shown:
18676 // Sample linker script for the PIC18F452 processor
18682 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18685 CODEPAGE NAME=page START=0x2A END=0x7FFF
18688 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18691 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18694 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18697 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18700 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18705 DATABANK NAME=gpr0 START=0x80 END=0xFF
18708 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18711 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18714 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18717 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18720 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18723 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18728 SECTION NAME=CONFIG ROM=config
18733 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18736 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18739 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18742 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18745 SECTION NAME=bank4 RAM=gpr4
18748 SECTION NAME=bank5 RAM=gpr5
18751 The linker will recognise the section name set in the pragma statement and
18752 will position the variable at the memory bank set with the RAM field at
18753 the SECTION line in the linker script file.
18757 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18764 There is one main header file that can be included to the source files using
18771 This header file contains the definitions for the processor special registers,
18772 so it is necessary if the source accesses them.
18773 It can be included by adding the following line in the beginning of the
18777 #include <pic18fregs.h>
18780 The specific microcontroller is selected within the pic18fregs.h automatically,
18781 so the same source can be used with a variety of devices.
18787 The libraries that PIC16
18788 \begin_inset LatexCommand \index{PIC16}
18792 port depends on are the microcontroller device libraries which contain
18793 the symbol definitions for the microcontroller special function registers.
18794 These libraries have the format pic18fxxxx.lib, where
18798 is the microcontroller identification number.
18799 The specific library is selected automatically by the compiler at link
18800 stage according to the selected device.
18803 Libraries are created with gplib which is part of the gputils package
18804 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18809 \layout Subsubsection*
18811 Building the libraries
18814 Before using SDCC/pic16 there are some libraries that need to be compiled.
18815 This process is not done automatically by SDCC since not all users use
18816 SDCC for pic16 projects.
18817 So each user should compile the libraries separately.
18820 The steps to compile the pic16 libraries under Linux are:
18823 cd device/lib/pic16
18838 su -c 'make install' # install the libraries, you need the root password
18841 If you need to install the headers too, do:
18847 su -c 'make install' # install the headers, you need the root password
18850 There exist a special target to build the I/O libraries.
18851 This target is not automatically build because it will build the I/O library
18857 This way building will take quite a lot of time.
18858 Users are advised to edit the
18860 device/lib/pic16/pics.build
18862 file and then execute:
18871 The following memory models are supported by the PIC16 port:
18880 Memory model affects the default size of pointers within the source.
18881 The sizes are shown in the next table:
18885 \begin_inset Tabular
18886 <lyxtabular version="3" rows="3" columns="3">
18888 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18889 <column alignment="center" valignment="top" leftline="true" width="0">
18890 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18891 <row topline="true" bottomline="true">
18892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18897 Pointer sizes according to memory model
18900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18917 <row topline="true" bottomline="true">
18918 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18926 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18943 <row topline="true" bottomline="true">
18944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18952 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18976 It is advisable that all sources within a project are compiled with the
18978 If one wants to override the default memory model, this can be done by
18979 declaring a pointer as
18988 Far selects large memory model's pointers, while near selects small memory
18992 The standard device libraries (see
18993 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18997 ) contain no reference to pointers, so they can be used with both memory
19004 The stack implementation for the PIC16 port uses two indirect registers,
19007 \labelwidthstring 00.00.0000
19009 FSR1 is assigned as stack pointer
19011 \labelwidthstring 00.00.0000
19013 FSR2 is assigned as frame pointer
19016 The following stack models are supported by the PIC16 port
19037 model means that only the FSRxL byte is used to access stack and frame,
19044 uses both FSRxL and FSRxH registers.
19045 The following table shows the stack/frame pointers sizes according to stack
19046 model and the maximum space they can address:
19050 \begin_inset Tabular
19051 <lyxtabular version="3" rows="3" columns="3">
19053 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19054 <column alignment="center" valignment="top" leftline="true" width="0">
19055 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19056 <row topline="true" bottomline="true">
19057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19062 Stack & Frame pointer sizes according to stack model
19065 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19073 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19082 <row topline="true">
19083 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19108 <row topline="true" bottomline="true">
19109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19125 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19145 stack model is currently not working properly throughout the code generator.
19146 So its use is not advised.
19147 Also there are some other points that need special care:
19152 Do not create stack sections with size more than one physical bank (that
19156 Stack sections should no cross physical bank limits (i.e.
19157 #pragma stack 0x50 0x100)
19160 These limitations are caused by the fact that only FSRxL is modified when
19161 using SMALL stack model, so no more than 256 bytes of stack can be used.
19162 This problem will disappear after LARGE model is fully implemented.
19168 In addition to the standard SDCC function keywords, PIC16 port makes available
19171 \labelwidthstring 00.00.0000
19173 wparam Use the WREG to pass one byte of the first function argument.
19174 This improves speed but you may not use this for functions with arguments
19175 that are called via function pointers, otherwise the first byte of the
19176 first parameter will get lost.
19180 void func_wparam(int a) wparam
19186 /* WREG hold the lower part of a */
19189 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19199 This keyword replaces the deprecated wparam pragma.
19201 \labelwidthstring 00.00.0000
19203 shadowregs When entering/exiting an ISR, it is possible to take advantage
19204 of the PIC18F hardware shadow registers which hold the values of WREG,
19205 STATUS and BSR registers.
19206 This can be done by adding the keyword
19214 keyword in the function's header.
19217 void isr_shadow(void) shadowregs interrupt 1
19233 instructs the code generator not to store/restore WREG, STATUS, BSR when
19234 entering/exiting the ISR.
19237 Function return values
19240 Return values from functions are placed to the appropriate registers following
19241 a modified Microchip policy optimized for SDCC.
19242 The following table shows these registers:
19246 \begin_inset Tabular
19247 <lyxtabular version="3" rows="6" columns="2">
19249 <column alignment="center" valignment="top" leftline="true" width="0">
19250 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19251 <row topline="true" bottomline="true">
19252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19260 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19265 destination register
19269 <row topline="true">
19270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19278 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19287 <row topline="true">
19288 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19296 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19305 <row topline="true">
19306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19314 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19323 <row topline="true">
19324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19337 FSR0L:PRODH:PRODL:WREG
19341 <row topline="true" bottomline="true">
19342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19355 on stack, FSR0 points to the beginning
19369 An interrupt servive routine (ISR) is declared using the
19376 void isr(void) interrupt
19394 is the interrupt number, which for PIC18F devices can be:
19398 \begin_inset Tabular
19399 <lyxtabular version="3" rows="4" columns="3">
19401 <column alignment="center" valignment="top" leftline="true" width="0">
19402 <column alignment="center" valignment="top" leftline="true" width="0">
19403 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19404 <row topline="true" bottomline="true">
19405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19423 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19428 Interrupt Vector Address
19432 <row topline="true">
19433 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19458 <row topline="true">
19459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19476 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19490 HIGH priority interrupts
19493 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19502 <row topline="true" bottomline="true">
19503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19516 LOW priority interrupts
19519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19535 When generating assembly code for ISR the code generator places a
19541 Interrupt Vector Address
19543 which points at the genetated ISR.
19544 This single GOTO instruction is part of an automatically generated
19546 interrupt entry point
19549 The actuall ISR code is placed as normally would in the code space.
19550 Upon interrupt request, the GOTO instruction is executed which jumps to
19552 When declaring interrupt functions as _naked this GOTO instruction is
19557 The whole interrupt functions is therefore placed at the Interrupt Vector
19558 Address of the specific interrupt.
19559 This is not a problem for the LOW priority interrupts, but it is a problem
19560 for the RESET and the HIGH priority interrupts because code may be written
19561 at the next interrupt´s vector address and cause undeterminate program
19562 behaviour if that interrupt is raised.
19568 This is not a problem when
19571 this is a HIGH interrupt ISR and LOW interrupts are
19578 when the ISR is small enough not to reach the next interrupt´s vector address.
19588 is possible to be omitted.
19589 This way a function is generated similar to an ISR, but it is not assigned
19593 When entering an interrupt, currently the PIC16
19594 \begin_inset LatexCommand \index{PIC16}
19598 port automatically saves the following registers:
19610 PROD (PRODL and PRODH)
19613 FSR0 (FSR0L and FSR0H)
19616 These registers are restored upon return from the interrupt routine.
19622 NOTE that when the _naked attribute is specified for an interrupt routine,
19623 then NO registers are stored or restored.
19632 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19633 There are 3 types of generic pointers currently implemented data, code
19634 and eeprom pointers.
19635 They are differentiated by the value of the 7th and 6th bits of the upper
19640 \begin_inset Tabular
19641 <lyxtabular version="3" rows="5" columns="5">
19643 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19644 <column alignment="center" valignment="top" width="0">
19645 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19646 <column alignment="center" valignment="top" width="0">
19647 <column alignment="left" valignment="top" rightline="true" width="0">
19648 <row topline="true" bottomline="true">
19649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19673 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19678 rest of the pointer
19681 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19690 <row topline="true" bottomline="true">
19691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19699 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19724 uuuuuu uuuuxxxx xxxxxxxx
19727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19732 a 12-bit data pointer in data RAM memory
19736 <row bottomline="true">
19737 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19761 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19770 uxxxxx xxxxxxxx xxxxxxxx
19773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19778 a 21-bit code pointer in FLASH memory
19782 <row bottomline="true">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19816 uuuuuu uuuuuuxx xxxxxxxx
19819 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19824 a 10-bit eeprom pointer in EEPROM memory
19828 <row bottomline="true">
19829 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19845 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19862 xxxxxx xxxxxxxx xxxxxxxx
19865 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19870 unimplemented pointer type
19881 Generic pointer are read and written with a set of library functions which
19882 read/write 1, 2, 3, 4 bytes.
19886 \layout Subsubsection
19888 Standard I/O Streams
19895 the type FILE is defined as:
19898 typedef char * FILE;
19901 This type is the stream type implemented I/O in the PIC18F devices.
19902 Also the standard input and output streams are declared in stdio.h:
19905 extern FILE * stdin;
19908 extern FILE * stdout;
19911 The FILE type is actually a generic pointer which defines one more type
19912 of generic pointers, the
19917 This new type has the format:
19921 \begin_inset Tabular
19922 <lyxtabular version="3" rows="2" columns="7">
19924 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19925 <column alignment="center" valignment="top" width="0">
19926 <column alignment="center" valignment="top" leftline="true" width="0">
19927 <column alignment="center" valignment="top" leftline="true" width="0">
19928 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19929 <column alignment="center" valignment="top" width="0">
19930 <column alignment="left" valignment="top" rightline="true" width="0">
19931 <row topline="true" bottomline="true">
19932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19956 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19977 rest of the pointer
19980 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19989 <row topline="true" bottomline="true">
19990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20030 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20047 upper byte high nubble is 0x2n, the rest are zeroes
20058 Currently implemented there are 3 types of streams defined:
20062 \begin_inset Tabular
20063 <lyxtabular version="3" rows="4" columns="4">
20065 <column alignment="center" valignment="top" leftline="true" width="0">
20066 <column alignment="center" valignment="top" leftline="true" width="0">
20067 <column alignment="center" valignment="top" leftline="true" width="0">
20068 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20069 <row topline="true" bottomline="true">
20070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20086 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20103 <row topline="true">
20104 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20135 Writes/Reads characters via the USART peripheral
20139 <row topline="true">
20140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20171 Writes/Reads characters via the MSSP peripheral
20175 <row topline="true" bottomline="true">
20176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20207 Writes/Reads characters via used defined functions
20218 The stream identifiers are declared as macros in the stdio.h header.
20221 In the libc library there exist the functions that are used to write to
20222 each of the above streams.
20225 \labelwidthstring 00.00.0000
20237 _stream_usart_putchar writes a character at the USART stream
20239 \labelwidthstring 00.00.0000
20251 _stream_mssp_putchar writes a character at the MSSP stream
20253 \labelwidthstring 00.00.0000
20255 putchar dummy function.
20256 This writes a character to a user specified manner.
20259 In order to increase performance
20263 is declared in stdio.h as having its parameter in WREG (it has the wparam
20265 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20266 in a user-friendly way.
20271 is the name of the variable that holds the character to print.
20272 An example follows:
20275 #include <pic18fregs.h>
20287 PORTA = c; /* dump character c to PORTA */
20300 stdout = STREAM_USER; /* this is not necessery, since stdout points
20303 * by default to STREAM_USER */
20306 printf (¨This is a printf test
20314 \layout Subsubsection
20319 PIC16 contains an implementation of the printf-family of functions.
20320 There exist the following functions:
20323 extern unsigned int sprintf(char *buf, char *fmt, ...);
20326 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20331 extern unsigned int printf(char *fmt, ...);
20334 extern unsigned int vprintf(char *fmt, va_lista ap);
20339 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20342 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20345 For sprintf and vsprintf
20349 should normally be a data pointer where the resulting string will be placed.
20350 No range checking is done so the user should allocate the necessery buffer.
20351 For fprintf and vfprintf
20355 should be a stream pointer (i.e.
20356 stdout, STREAM_MSSP, etc...).
20357 \layout Subsubsection
20362 The PIC18F family of microcontrollers supports a number of interrupt sources.
20363 A list of these interrupts is shown in the following table:
20367 \begin_inset Tabular
20368 <lyxtabular version="3" rows="11" columns="4">
20370 <column alignment="left" valignment="top" leftline="true" width="0">
20371 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20372 <column alignment="left" valignment="top" leftline="true" width="0">
20373 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20374 <row topline="true" bottomline="true">
20375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20383 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20391 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20399 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20408 <row topline="true">
20409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20417 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20422 PORTB change interrupt
20425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20433 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20438 EEPROM/FLASH write complete interrupt
20442 <row topline="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">
20456 INT0 external interrupt
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">
20472 Bus collision interrupt
20476 <row topline="true">
20477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20485 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20490 INT1 external 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 Low voltage detect 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 INT2 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 Parallel slave port 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 CCP1 module 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 AD convertion complete 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 CCP2 module 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 USART receive 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 TMR0 overflow 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 USART transmit 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 TMR1 overflow 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 SSP receive/transmit 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 TMR2 matches PR2 interrupt
20697 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20704 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20712 <row topline="true" bottomline="true">
20713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20726 TMR3 overflow interrupt
20729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20736 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20751 The prototypes for these names are defined in the header file
20758 In order to simplify signal handling, a number of macros is provided:
20760 \labelwidthstring 00.00.0000
20762 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20763 high priority interrupts.
20768 is the function name to use.
20770 \labelwidthstring 00.00.0000
20772 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20773 low priority interrupt.
20778 is the function name to use.
20780 \labelwidthstring 00.00.0000
20782 DEF_HANDLER(sig,handler) define a handler for signal
20786 \labelwidthstring 00.00.0000
20788 END_DEF end the declaration of the dispatch table.
20791 Additionally there are two more macros to simplify the declaration of the
20794 \labelwidthstring 00.00.0000
20798 SIGHANDLER(handler)
20800 this declares the function prototype for the
20806 \labelwidthstring 00.00.0000
20808 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20811 An example of using the macros above is shown below:
20814 #include <pic18fregs.h>
20817 #include <signal.h>
20821 DEF_INTHIGH(high_int)
20824 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20827 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20834 SIGHANDLER(_tmr0_handler)
20840 /* action to be taken when timer 0 overflows */
20847 SIGHANDLERNAKED(_bcol_handler)
20856 /* action to be taken when bus collision occurs */
20872 Special care should be taken when using the above scheme:
20875 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20878 when declaring SIGHANDLERNAKED handler never forget to use
20882 for proper returning.
20888 Here you can find some general tips for compiling programs with SDCC/pic16.
20889 \layout Subsubsection
20894 The default stack size (that is 64 bytes) probably is enough for many programs.
20895 One must take care that when there are many levels of function nesting,
20896 or there is excessive usage of stack, its size should be extended.
20897 An example of such a case is the printf/sprintf family of functions.
20898 If you encounter problems like not being able to print integers, then you
20899 need to set the stack size around the maximum (256 for small stack model).
20900 The following diagram shows what happens when calling printf to print an
20904 printf () --> ltoa () --> ultoa () --> divschar ()
20907 It is should be understood that stack is easily consumed when calling complicate
20909 Using command line arguments like -
20919 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20921 Other ways to reduce stack usage may exist.
20927 There are several approaches to debugging your code.
20928 This chapter is meant to show your options and to give detail on some of
20933 When writing your code:
20936 write your code with debugging in mind (avoid duplicating code, put conceptually
20937 similar variables into structs, use structured code, have strategic points
20938 within your code where all variables are consistent, ...)
20941 run a syntax-checking tool like splint
20942 \begin_inset LatexCommand \index{splint (syntax checking tool)}
20947 \begin_inset LatexCommand \index{lint (syntax checking tool)}
20962 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
20969 for the high level code use a C-compiler (like f.e.
20970 GCC) to compile run and debug the code on your host.
20982 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
20986 ) on howto handle syntax extensions like __xdata, __at(), ...
20990 use another C-compiler to compile code for your target.
20991 Always an option but not recommended:) And not very likely to help you.
20992 If you seriously consider walking this path you should at least occasionally
20993 check portability of your code.
20994 Most commercial compiler vendors will offer an evaluation version so you
20995 can test compile your code or snippets of your code.
20998 Debugging on a simulator:
21001 there is a separate section about SDCDB (section
21002 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21009 or (8051 specific) use a freeware/commercial simulator which interfaces
21011 \begin_inset LatexCommand \index{AOMF, AOMF51}
21016 \begin_inset LatexCommand \ref{OMF file}
21020 ) optionally generated by SDCC.
21023 Debugging On-target:
21026 use a MCU port pin to serially output debug data to the RS232 port of your
21028 You'll probably want some level shifting device typically involving a MAX232
21030 If the hardware serial port of the MCU is not available search for 'Software
21031 UART' in your favourite search machine.
21034 use an on-target monitor.
21035 In this context a monitor is a small program which usually accepts commands
21036 via a serial line and allows to set program counter, to single step through
21037 a program and read/write memory locations.
21038 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21040 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21047 toggle MCU port pins at strategic points within your code and use an oscilloscop
21051 digital oscilloscope
21054 \begin_inset LatexCommand \index{oscilloscope}
21058 with deep trace memory is really helpful especially if you have to debug
21059 a realtime application.
21060 If you need to monitor more pins than your oscilloscope provides you can
21061 sometimes get away with a small R-2R network.
21062 On a single channel oscilloscope you could f.e.
21063 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21065 \begin_inset Formula $\Omega$
21068 resistor and the other one by a 5\SpecialChar ~
21070 \begin_inset Formula $\Omega$
21073 resistor to the oscilloscope probe (check output drive capability of the
21074 pins you want to monitor).
21075 If you need to monitor many more pins a
21095 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21100 Usually very expensive.
21101 And very nice to have too.
21102 And usually locks you (for years...) to the devices the ICE can emulate.
21106 use a remote debugger.
21107 In most 8-bit systems the symbol information is not available on the target,
21108 and a complete debugger is too bulky for the target system.
21109 Therefore usually a debugger on the host system connects to an on-target
21110 debugging stub which accepts only primitive commands.
21113 Terms to enter into your favourite search engine could be 'remote debugging',
21114 'gdb stub' or 'inferior debugger'.
21118 use an on target hardware debugger.
21119 Some of the more modern MCUs include hardware support for setting break
21120 points and monitoring/changing variables by using dedicated hardware pins.
21121 This facility doesn't require additional code to run on the target and
21126 doesn't affect runtime behaviour until a breakpoint is hit.
21127 For the mcs51 most hardware debuggers use the AOMF
21128 \begin_inset LatexCommand \index{AOMF, AOMF51}
21133 \begin_inset LatexCommand \ref{OMF file}
21144 if you are not familiar with any of the following terms you're likely to
21145 run into problems rather sooner than later:
21162 As an embedded programmer you
21166 to know them so why not look them up
21170 you have problems?)
21173 tell someone else about your problem (actually this is a surprisingly effective
21174 means to hunt down the bug even if the listener is not familiar with your
21176 As 'failure to communicate' is probably one of the job-induced deformations
21177 of an embedded programmer this is highly encouraged.
21180 Debugging with SDCDB
21181 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21186 \begin_inset LatexCommand \index{sdcdb (debugger)}
21193 SDCC is distributed with a source level debugger
21194 \begin_inset LatexCommand \index{Debugger}
21199 The debugger uses a command line interface, the command repertoire of the
21200 debugger has been kept as close to gdb
21201 \begin_inset LatexCommand \index{gdb}
21205 (the GNU debugger) as possible.
21206 The configuration and build process is part of the standard compiler installati
21207 on, which also builds and installs the debugger in the target directory
21208 specified during configuration.
21209 The debugger allows you debug BOTH at the C source and at the ASM source
21211 Sdcdb is currently not available on Win32 platforms.
21214 Compiling for Debugging
21228 \begin_inset LatexCommand \index{-\/-debug}
21232 option must be specified for all files for which debug information is to
21234 The compiler generates a .adb file for each of these files.
21235 The linker creates the .cdb
21236 \begin_inset LatexCommand \index{<file>.cdb}
21241 \begin_inset LatexCommand \index{<file>.adb}
21245 files and the address information.
21246 This .cdb is used by the debugger.
21249 How the Debugger Works
21262 -debug option is specified the compiler generates extra symbol information
21263 some of which are put into the assembler source and some are put into the
21265 Then the linker creates the .cdb file from the individual .adb files with
21266 the address information for the symbols.
21267 The debugger reads the symbolic information generated by the compiler &
21268 the address information generated by the linker.
21269 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21270 execution is controlled by the debugger.
21271 When a command is issued for the debugger, it translates it into appropriate
21272 commands for the simulator.
21273 (Currently sdcdb only connects to the simulator but
21278 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21282 is an effort to connect directly to the hardware.)
21285 Starting the Debugger SDCDB
21288 The debugger can be started using the following command line.
21289 (Assume the file you are debugging has the file name foo).
21303 The debugger will look for the following files.
21306 foo.c - the source file.
21309 foo.cdb - the debugger symbol information file.
21312 foo.ihx - the Intel hex format
21313 \begin_inset LatexCommand \index{Intel hex format}
21320 SDCDB Command Line Options
21333 -directory=<source file directory> this option can used to specify the directory
21335 The debugger will look into the directory list specified for source, cdb
21337 The items in the directory list must be separated by ':', e.g.
21338 if the source files can be in the directories /home/src1 and /home/src2,
21349 -directory option should be -
21359 -directory=/home/src1:/home/src2.
21360 Note there can be no spaces in the option.
21364 -cd <directory> - change to the <directory>.
21367 -fullname - used by GUI front ends.
21370 -cpu <cpu-type> - this argument is passed to the simulator please see the
21371 simulator docs for details.
21374 -X <Clock frequency > this options is passed to the simulator please see
21375 the simulator docs for details.
21378 -s <serial port file> passed to simulator see the simulator docs for details.
21381 -S <serial in,out> passed to simulator see the simulator docs for details.
21384 -k <port number> passed to simulator see the simulator docs for details.
21387 SDCDB Debugger Commands
21390 As mentioned earlier the command interface for the debugger has been deliberatel
21391 y kept as close the GNU debugger gdb, as possible.
21392 This will help the integration with existing graphical user interfaces
21393 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21394 If you use a graphical user interface for the debugger you can skip this
21396 \layout Subsubsection*
21398 break [line | file:line | function | file:function]
21401 Set breakpoint at specified line or function:
21410 sdcdb>break foo.c:100
21412 sdcdb>break funcfoo
21414 sdcdb>break foo.c:funcfoo
21415 \layout Subsubsection*
21417 clear [line | file:line | function | file:function ]
21420 Clear breakpoint at specified line or function:
21429 sdcdb>clear foo.c:100
21431 sdcdb>clear funcfoo
21433 sdcdb>clear foo.c:funcfoo
21434 \layout Subsubsection*
21439 Continue program being debugged, after breakpoint.
21440 \layout Subsubsection*
21445 Execute till the end of the current function.
21446 \layout Subsubsection*
21451 Delete breakpoint number 'n'.
21452 If used without any option clear ALL user defined break points.
21453 \layout Subsubsection*
21455 info [break | stack | frame | registers ]
21458 info break - list all breakpoints
21461 info stack - show the function call stack.
21464 info frame - show information about the current execution frame.
21467 info registers - show content of all registers.
21468 \layout Subsubsection*
21473 Step program until it reaches a different source line.
21474 Note: pressing <return> repeats the last command.
21475 \layout Subsubsection*
21480 Step program, proceeding through subroutine calls.
21481 \layout Subsubsection*
21486 Start debugged program.
21487 \layout Subsubsection*
21492 Print type information of the variable.
21493 \layout Subsubsection*
21498 print value of variable.
21499 \layout Subsubsection*
21504 load the given file name.
21505 Note this is an alternate method of loading file for debugging.
21506 \layout Subsubsection*
21511 print information about current frame.
21512 \layout Subsubsection*
21517 Toggle between C source & assembly source.
21518 \layout Subsubsection*
21520 ! simulator command
21523 Send the string following '!' to the simulator, the simulator response is
21525 Note the debugger does not interpret the command being sent to the simulator,
21526 so if a command like 'go' is sent the debugger can loose its execution
21527 context and may display incorrect values.
21528 \layout Subsubsection*
21535 My name is Bobby Brown"
21538 Interfacing SDCDB with DDD
21541 The screenshot was converted from png to eps with:
21542 \begin_inset Quotes sld
21545 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
21546 \begin_inset Quotes srd
21549 which produces a pretty compact eps file which is free from compression
21553 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
21554 as this broke the build system on Sourceforge (pdf-file was broken).
21560 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
21566 shows a screenshot of a debugging session with DDD
21567 \begin_inset LatexCommand \index{DDD (debugger)}
21571 (Unix only) on a simulated 8032.
21572 The debugging session might not run as smoothly as the screenshot suggests.
21573 The debugger allows setting of breakpoints, displaying and changing variables,
21574 single stepping through C and assembler code.
21577 The source was compiled with
21600 -debug ddd_example.c
21613 and DDD was invoked with
21620 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21623 Interfacing SDCDB with XEmacs
21624 \begin_inset LatexCommand \index{XEmacs}
21629 \begin_inset LatexCommand \index{Emacs}
21636 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21637 sdcdb.el and sdcdbsrc.el.
21638 These two files can be found in the $(prefix)/bin directory after the installat
21640 These files need to be loaded into XEmacs for the interface to work.
21641 This can be done at XEmacs startup time by inserting the following into
21642 your '.xemacs' file (which can be found in your HOME directory):
21648 (load-file sdcdbsrc.el)
21654 .xemacs is a lisp file so the () around the command is REQUIRED.
21655 The files can also be loaded dynamically while XEmacs is running, set the
21656 environment variable 'EMACSLOADPATH' to the installation bin directory
21657 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21658 To start the interface enter the following command:
21672 You will prompted to enter the file name to be debugged.
21677 The command line options that are passed to the simulator directly are bound
21678 to default values in the file sdcdbsrc.el.
21679 The variables are listed below, these values maybe changed as required.
21682 sdcdbsrc-cpu-type '51
21685 sdcdbsrc-frequency '11059200
21688 sdcdbsrc-serial nil
21691 The following is a list of key mapping for the debugger interface.
21702 ;;key\SpecialChar ~
21716 binding\SpecialChar ~
21740 ;;---\SpecialChar ~
21754 -------\SpecialChar ~
21796 sdcdb-next-from-src\SpecialChar ~
21824 sdcdb-back-from-src\SpecialChar ~
21852 sdcdb-cont-from-src\SpecialChar ~
21862 SDCDB continue command
21880 sdcdb-step-from-src\SpecialChar ~
21908 sdcdb-whatis-c-sexp\SpecialChar ~
21918 SDCDB ptypecommand for data at
21985 sdcdbsrc-delete\SpecialChar ~
21999 SDCDB Delete all breakpoints if no arg
22048 given or delete arg (C-u arg x)
22066 sdcdbsrc-frame\SpecialChar ~
22081 SDCDB Display current frame if no arg,
22130 given or display frame arg
22197 sdcdbsrc-goto-sdcdb\SpecialChar ~
22207 Goto the SDCDB output buffer
22225 sdcdb-print-c-sexp\SpecialChar ~
22236 SDCDB print command for data at
22303 sdcdbsrc-goto-sdcdb\SpecialChar ~
22313 Goto the SDCDB output buffer
22331 sdcdbsrc-mode\SpecialChar ~
22347 Toggles Sdcdbsrc mode (turns it off)
22362 sdcdb-finish-from-src\SpecialChar ~
22370 SDCDB finish command
22385 sdcdb-break\SpecialChar ~
22403 Set break for line with point
22418 sdcdbsrc-mode\SpecialChar ~
22434 Toggle Sdcdbsrc mode
22449 sdcdbsrc-srcmode\SpecialChar ~
22472 Here are a few guidelines that will help the compiler generate more efficient
22473 code, some of the tips are specific to this compiler others are generally
22474 good programming practice.
22477 Use the smallest data type to represent your data-value.
22478 If it is known in advance that the value is going to be less than 256 then
22479 use an 'unsigned char' instead of a 'short' or 'int'.
22480 Please note, that ANSI C requires both signed and unsigned chars to be
22481 promoted to 'signed int'
22482 \begin_inset LatexCommand \index{promotion to signed int}
22486 before doing any operation.
22488 \begin_inset LatexCommand \index{type promotion}
22493 \begin_inset LatexCommand \label{type promotion}
22497 can be omitted, if the result is the same.
22498 The effect of the promotion rules together with the sign-extension is often
22505 unsigned char uc = 0xfe;
22507 if (uc * uc < 0) /* this is true! */
22526 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
22536 (unsigned char) -12 / (signed char) -3 = ...
22539 No, the result is not 4:
22544 (int) (unsigned char) -12 / (int) (signed char) -3 =
22546 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
22548 (int) 0x00f4 / (int) 0xfffd =
22550 (int) 0x00f4 / (int) 0xfffd =
22552 (int) 244 / (int) -3 =
22554 (int) -81 = (int) 0xffaf;
22557 Don't complain, that gcc gives you a different result.
22558 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
22559 Therefore the results are different.
22562 \begin_inset Quotes sld
22566 \begin_inset Quotes srd
22572 If well-defined overflow characteristics are important and negative values
22573 are not, or if you want to steer clear of sign-extension problems when
22574 manipulating bits or bytes, use one of the corresponding unsigned types.
22575 (Beware when mixing signed and unsigned values in expressions, though.)
22577 Although character types (especially unsigned char) can be used as "tiny"
22578 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
22579 ble sign extension and increased code size.
22583 Use unsigned when it is known in advance that the value is not going to
22585 This helps especially if you are doing division or multiplication, bit-shifting
22586 or are using an array index.
22589 NEVER jump into a LOOP.
22592 Declare the variables to be local
22593 \begin_inset LatexCommand \index{local variables}
22597 whenever possible, especially loop control variables (induction).
22600 Since the compiler does not always do implicit integral promotion, the programme
22601 r should do an explicit cast when integral promotion is required.
22604 Reducing the size of division, multiplication & modulus operations can reduce
22605 code size substantially.
22606 Take the following code for example.
22612 foobar(unsigned int p1, unsigned char ch)
22620 unsigned char ch1 = p1 % ch ;
22631 For the modulus operation the variable ch will be promoted to unsigned int
22632 first then the modulus operation will be performed (this will lead to a
22633 call to support routine _moduint()), and the result will be casted to a
22635 If the code is changed to
22640 foobar(unsigned int p1, unsigned char ch)
22648 unsigned char ch1 = (unsigned char)p1 % ch ;
22659 It would substantially reduce the code generated (future versions of the
22660 compiler will be smart enough to detect such optimization opportunities).
22664 Have a look at the assembly listing to get a
22665 \begin_inset Quotes sld
22669 \begin_inset Quotes srd
22672 for the code generation.
22675 Porting code from or to other compilers
22678 check whether endianness of the compilers differs and adapt where needed.
22681 check the device specific header files for compiler specific syntax.
22682 Eventually include the file <compiler.h
22683 \begin_inset LatexCommand \index{compiler.h (include file)}
22687 > to allow using common header files.
22690 check whether the startup code contains the correct initialization (watchdog,
22694 check whether the sizes of short, int, long match.
22697 check if some 16 or 32 bit hardware registers require a specific addressing
22698 order (least significant or most significant byte first) and adapt if needed
22707 relate to time and not to lower/upper memory location here, so this is
22712 the same as endianness).
22715 check whether the keyword
22719 is used where needed.
22720 The compilers might differ in their optimization characteristics (as different
22721 versions of the same compiler might also use more clever optimizations
22722 this is good idea anyway).
22725 check that the compilers are not told to supress warnings.
22728 check and convert compiler specific extensions (interrupts, memory areas,
22732 check for differences in type promotion (especially check for math operations
22733 on char variables and for the use of the ~\SpecialChar ~
22734 operator on bit variables.
22736 \begin_inset LatexCommand \ref{type promotion}
22741 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
22748 check the assembly code generated for interrupt routines (f.e.
22749 for calls to possibly non-reentrant library functions).
22752 check whether timing loops result in proper timing (or preferably consider
22753 a rewrite of the code with timer based delays instead).
22756 check for differences in printf parameters (some compilers push (va_arg
22757 \begin_inset LatexCommand \index{va\_arg}
22761 ) char variables as integers others as char).
22764 check the resulting memory layout.
22768 \begin_inset LatexCommand \index{Tools}
22772 included in the distribution
22776 \begin_inset Tabular
22777 <lyxtabular version="3" rows="12" columns="3">
22779 <column alignment="left" valignment="top" leftline="true" width="0pt">
22780 <column alignment="left" valignment="top" leftline="true" width="0pt">
22781 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22782 <row topline="true" bottomline="true">
22783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22808 <row topline="true">
22809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22822 Simulator for various architectures
22825 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22834 <row topline="true">
22835 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22843 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22848 header file conversion
22851 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22856 sdcc/support/scripts
22860 <row topline="true">
22861 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22874 header file conversion
22877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22882 sdcc/support/scripts
22886 <row topline="true">
22887 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22895 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22921 <row topline="true">
22922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22930 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22956 <row topline="true">
22957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22965 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22973 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22991 <row topline="true">
22992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23026 <row topline="true">
23027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23043 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23061 <row topline="true">
23062 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23070 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23078 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23096 <row topline="true">
23097 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23113 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23131 <row topline="true" bottomline="true">
23132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23176 \begin_inset LatexCommand \index{Documentation}
23180 included in the distribution
23184 \begin_inset Tabular
23185 <lyxtabular version="3" rows="10" columns="2">
23187 <column alignment="left" valignment="top" leftline="true" width="0">
23188 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23189 <row topline="true" bottomline="true">
23190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23198 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23203 Where to get / filename
23207 <row topline="true">
23208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23213 SDCC Compiler User Guide
23216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23221 You're reading it right now
23225 <row topline="true">
23226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23243 <row topline="true">
23244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23250 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23255 \begin_inset LatexCommand \index{Assembler documentation}
23259 Assemblers and ASLINK
23260 \begin_inset LatexCommand \index{aslink}
23265 \begin_inset LatexCommand \index{Linker documentation}
23272 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23277 sdcc/as/doc/asxhtm.html
23281 <row topline="true">
23282 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23287 SDCC regression test
23288 \begin_inset LatexCommand \index{Regression test}
23295 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23300 sdcc/doc/test_suite_spec.pdf
23304 <row topline="true">
23305 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23313 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23322 <row topline="true">
23323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23328 Notes on debugging with sdcdb
23329 \begin_inset LatexCommand \index{sdcdb (debugger)}
23336 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23341 sdcc/debugger/README
23345 <row topline="true">
23346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23351 Software simulator for microcontrollers
23354 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23381 <row topline="true">
23382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23387 Temporary notes on the pic16
23388 \begin_inset LatexCommand \index{PIC16}
23395 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23400 sdcc/src/pic16/NOTES
23404 <row topline="true" bottomline="true">
23405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23410 SDCC internal documentation (debugging file format)
23413 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23449 Related open source tools
23450 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
23455 \begin_inset LatexCommand \index{Related tools}
23463 \begin_inset Tabular
23464 <lyxtabular version="3" rows="14" columns="3">
23466 <column alignment="left" valignment="top" leftline="true" width="0pt">
23467 <column alignment="block" valignment="top" leftline="true" width="30line%">
23468 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23469 <row topline="true" bottomline="true">
23470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23486 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23495 <row topline="true">
23496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23502 \begin_inset LatexCommand \index{gpsim (pic simulator)}
23509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23523 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
23531 <row topline="true">
23532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23538 \begin_inset LatexCommand \index{gputils (pic tools)}
23545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23559 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
23567 <row topline="true">
23568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23590 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
23598 <row topline="true">
23599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23612 Tools for Silicon Laboratories JTAG debug adapter, partly based on sdcdb
23616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23622 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
23630 <row topline="true">
23631 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23637 \begin_inset LatexCommand \index{indent (source formatting tool)}
23644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23649 Formats C source - Master of the white spaces
23652 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23658 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
23666 <row topline="true">
23667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23673 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
23680 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23685 Object file conversion, checksumming, ...
23688 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23694 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
23702 <row topline="true">
23703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23709 \begin_inset LatexCommand \index{objdump (tool)}
23716 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23721 Object file conversion, ...
23724 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23729 Part of binutils (should be there anyway)
23733 <row topline="true">
23734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23747 8051 monitor (hex up-/download, single step, disassemble)
23750 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23756 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
23764 <row topline="true">
23765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23771 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23783 Source code documentation system
23786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23792 \begin_inset LatexCommand \url{http://www.doxygen.org}
23800 <row topline="true">
23801 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23814 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
23817 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23823 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23831 <row topline="true">
23832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23845 8051 monitor (hex up-/download, single step, disassemble)
23848 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23854 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
23862 <row topline="true">
23863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23869 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23876 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23881 Statically checks c sources (see
23882 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23889 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23895 \begin_inset LatexCommand \url{http://www.splint.org}
23903 <row topline="true" bottomline="true">
23904 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23910 \begin_inset LatexCommand \index{ddd (debugger)}
23917 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23922 Debugger, serves nicely as GUI to sdcdb
23923 \begin_inset LatexCommand \index{sdcdb (debugger)}
23930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23936 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
23953 Related documentation / recommended reading
23957 \begin_inset Tabular
23958 <lyxtabular version="3" rows="8" columns="3">
23960 <column alignment="left" valignment="top" leftline="true" width="0pt">
23961 <column alignment="block" valignment="top" leftline="true" width="30line%">
23962 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23963 <row topline="true" bottomline="true">
23964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23980 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23989 <row topline="true">
23990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24007 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24013 \begin_inset LatexCommand \index{C Reference card}
24020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24026 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24034 <row topline="true">
24035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24057 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24065 <row topline="true">
24066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24080 \begin_inset Quotes sld
24084 \begin_inset Quotes srd
24090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24098 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24106 <row topline="true">
24107 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24121 \begin_inset Quotes sld
24124 Extensions for Embedded C
24125 \begin_inset Quotes srd
24131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24139 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24147 <row topline="true">
24148 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24160 Latest datasheet of the target CPU
24163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24172 <row topline="true">
24173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24185 Revision history of datasheet
24188 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24197 <row topline="true" bottomline="true">
24198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24213 Advanced Compiler Design and Implementation
24216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24221 bookstore (very dedicated, probably read other books first)
24237 Some questions answered, some pointers given - it might be time to in turn
24245 can you solve your project with the selected microcontroller? Would you
24246 find out early or rather late that your target is too small/slow/whatever?
24247 Can you switch to a slightly better device if it doesn't fit?
24250 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
24251 and/or another programming language be more adequate? Would an operating
24252 system on the target device help?
24255 if you solved the problem, will the marketing department be happy?
24258 if the marketing department is happy, will customers be happy?
24261 if you're the project manager, marketing department and maybe even the customer
24262 in one person, have you tried to see the project from the outside?
24265 is the project done if you think it is done? Or is just that other interface/pro
24266 tocol/feature/configuration/option missing? How about website, manual(s),
24267 internationali(z|s)ation, packaging, labels, 2nd source for components,
24268 electromagnetic compatability/interference, documentation for production,
24269 production test software, update mechanism, patent issues?
24272 is your project adequately positioned in that magic triangle: fame, fortune,
24276 Maybe not all answers to these questions are known and some answers may
24281 , nevertheless knowing these questions may help you to avoid burnout
24287 burnout is bad for electronic devices, programmers and motorcycle tyres
24291 Chances are you didn't want to hear some of them...
24295 \begin_inset LatexCommand \index{Support}
24302 SDCC has grown to be a large project.
24303 The compiler alone (without the preprocessor, assembler and linker) is
24304 well over 100,000 lines of code (blank stripped).
24305 The open source nature of this project is a key to its continued growth
24307 You gain the benefit and support of many active software developers and
24309 Is SDCC perfect? No, that's why we need your help.
24310 The developers take pride in fixing reported bugs.
24311 You can help by reporting the bugs and helping other SDCC users.
24312 There are lots of ways to contribute, and we encourage you to take part
24313 in making SDCC a great software package.
24317 The SDCC project is hosted on the SDCC sourceforge site at
24318 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
24323 You'll find the complete set of mailing lists
24324 \begin_inset LatexCommand \index{Mailing list(s)}
24328 , forums, bug reporting system, patch submission
24329 \begin_inset LatexCommand \index{Patch submission}
24334 \begin_inset LatexCommand \index{download}
24338 area and Subversion code repository
24339 \begin_inset LatexCommand \index{Subversion code repository}
24347 \begin_inset LatexCommand \index{Bug reporting}
24352 \begin_inset LatexCommand \index{Reporting bugs}
24359 The recommended way of reporting bugs is using the infrastructure of the
24361 You can follow the status of bug reports there and have an overview about
24365 Bug reports are automatically forwarded to the developer mailing list and
24366 will be fixed ASAP.
24367 When reporting a bug, it is very useful to include a small test program
24368 (the smaller the better) which reproduces the problem.
24369 If you can isolate the problem by looking at the generated assembly code,
24370 this can be very helpful.
24371 Compiling your program with the -
24382 \begin_inset LatexCommand \index{-\/-dumpall}
24386 option can sometimes be useful in locating optimization problems.
24387 When reporting a bug please maker sure you:
24390 Attach the code you are compiling with SDCC.
24394 Specify the exact command you use to run SDCC, or attach your Makefile.
24398 Specify the SDCC version (type "
24404 "), your platform, and operating system.
24408 Provide an exact copy of any error message or incorrect output.
24412 Put something meaningful in the subject of your message.
24415 Please attempt to include these 5 important parts, as applicable, in all
24416 requests for support or when reporting any problems or bugs with SDCC.
24417 Though this will make your message lengthy, it will greatly improve your
24418 chance that SDCC users and developers will be able to help you.
24419 Some SDCC developers are frustrated by bug reports without code provided
24420 that they can use to reproduce and ultimately fix the problem, so please
24421 be sure to provide sample code if you are reporting a bug!
24424 Please have a short check that you are using a recent version of SDCC and
24425 the bug is not yet known.
24426 This is the link for reporting bugs:
24427 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
24434 Requesting Features
24435 \begin_inset LatexCommand \label{sub:Requesting-Features}
24440 \begin_inset LatexCommand \index{Feature request}
24445 \begin_inset LatexCommand \index{Requesting features}
24452 Like bug reports feature requests are forwarded to the developer mailing
24454 This is the link for requesting features:
24455 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24465 Like bug reports contributed patches are forwarded to the developer mailing
24467 This is the link for submitting patches
24468 \begin_inset LatexCommand \index{Patch submission}
24473 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
24480 You need to specify some parameters to the
24484 command for the patches to be useful.
24485 If you modified more than one file a patch created f.e.
24490 \begin_inset Quotes sld
24493 diff -Naur unmodified_directory modified_directory >my_changes.patch
24494 \begin_inset Quotes srd
24500 will be fine, otherwise
24504 \begin_inset Quotes sld
24507 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
24508 \begin_inset Quotes srd
24521 These links should take you directly to the
24522 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
24532 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
24533 automated messages (mid 2003)
24537 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
24542 \begin_inset LatexCommand \index{Mailing list(s)}
24546 and forums are archived and searchable so if you are lucky someone already
24547 had a similar problem.
24548 While mails to the lists themselves are delivered promptly their web front
24549 end on sourceforge sometimes shows a severe time lag (up to several weeks),
24550 if you're seriously using SDCC please consider subscribing to the lists.
24556 You can follow the status of the Subversion version
24557 \begin_inset LatexCommand \index{version}
24561 of SDCC by watching the Changelog
24562 \begin_inset LatexCommand \index{Changelog}
24566 in the Subversion repository
24569 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
24576 Subversion Source Code Repository
24585 or the filenames of the snapshot versions of SDCC include date and its
24587 \begin_inset LatexCommand \index{Subversion}
24592 Subversion allows to download the source of recent or previous versions
24594 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
24598 (by number or by date).
24599 An on-line source code browser and detailled instructions are also available
24601 SDCC versions starting from 1999 up to now are available (currently the
24602 versions prior to the conversion from cvs to Subversion (April 2006) are
24603 either by accessible by Subversion or by cvs).
24607 \begin_inset LatexCommand \index{Release policy}
24614 Historically there often were long delays between official releases and
24615 the sourceforge download area tends to get not updated at all.
24616 Excuses in the past might have referred to problems with live range analysis,
24617 but as this was fixed a while ago, the current problem is that another
24618 excuse has to be found.
24619 Kidding aside, we have to get better there! On the other hand there are
24620 daily snapshots available at
24621 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
24625 , and you can always build the very last version (hopefully with many bugs
24626 fixed, and features added) from the source code available at
24627 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
24635 \begin_inset LatexCommand \index{Examples}
24642 You'll find some small examples in the directory
24644 sdcc/device/examples/.
24647 More examples and libraries are available at
24649 The SDCC Open Knowledge Resource
24650 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
24657 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
24664 I did insert a reference to Paul's web site here although it seems rather
24665 dedicated to a specific 8032 board (I think it's okay because it f.e.
24666 shows LCD/Harddisc interface and has a free 8051 monitor.
24667 Independent 8032 board vendors face hard competition of heavily subsidized
24668 development boards anyway).
24671 Maybe we should include some links to real world applications.
24672 Preferably pointer to pointers (one for each architecture) so this stays
24677 \begin_inset LatexCommand \index{Quality control}
24684 The compiler is passed through nightly compile and build checks.
24690 \begin_inset LatexCommand \index{Regression test}
24694 check that SDCC itself compiles flawlessly on several platforms and checks
24695 the quality of the code generated by SDCC by running the code through simulator
24697 There is a separate document
24700 \begin_inset LatexCommand \index{Test suite}
24709 You'll find the test code in the directory
24711 sdcc/support/regression
24714 You can run these tests manually by running
24718 in this directory (or f.e.
24723 \begin_inset Quotes sld
24727 \begin_inset Quotes srd
24733 if you don't want to run the complete tests).
24734 The test code might also be interesting if you want to look for examples
24735 \begin_inset LatexCommand \index{Examples}
24739 checking corner cases of SDCC or if you plan to submit patches
24740 \begin_inset LatexCommand \index{Patch submission}
24747 The pic port uses a different set of regression tests, you'll find them
24750 sdcc/src/regression
24755 Use of SDCC in Education
24768 the phrase "use in education" might evoke the association "
24772 fit for use in education".
24773 This connotation is not intended but nevertheless risked as the licensing
24774 of SDCC makes it difficult to offer educational discounts
24778 If your rationales are to:
24781 give students a chance to understand the
24785 steps of code generation
24788 have a curriculum that can be extended for years.
24789 Then you could use an fpga board as target and your curriculum will seamlessly
24790 extend from logic synthesis (
24791 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
24796 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
24800 ), over assembly programming, to C to FPGA compilers (
24801 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
24808 be able to insert excursions about skills like using a revision control
24809 system, submitting/applying patches, using a type-setting (as opposed to
24810 word-processing) engine LyX/LaTeX, using
24811 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
24816 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
24820 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
24821 Source Software, CPU simulation, compiler regression tests
24822 \begin_inset LatexCommand \index{Regression test}
24829 And if there should be a shortage of ideas then you can always point students
24830 to the ever-growing feature request list
24831 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24838 not tie students to a specific host platform and instead allow them to use
24843 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
24845 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
24852 not encourage students to use illegal copies of educational software
24855 be immune to licensing/availability/price changes of the chosen tool chain
24858 be able to change to a new target platform without having to adopt a new
24862 have complete control over and insight into the tool chain
24865 make your students aware about the pros and cons of open source software
24869 give back to the public as you are probably at least partially publically
24873 give students a chance to publically prove their skills and to possibly
24874 see a world wide impact
24877 then SDCC is probably among the first choices.
24878 Well, probably SDCC might be the only choice.
24881 SDCC Technical Data
24885 \begin_inset LatexCommand \index{Optimizations}
24892 SDCC performs a host of standard optimizations in addition to some MCU specific
24897 Sub-expression Elimination
24898 \begin_inset LatexCommand \index{Subexpression elimination}
24905 The compiler does local and
24931 will be translated to
24943 Some subexpressions are not as obvious as the above example, e.g.:
24953 In this case the address arithmetic a->b[i] will be computed only once;
24954 the equivalent code in C would be.
24966 The compiler will try to keep these temporary variables in registers.
24969 Dead-Code Elimination
24970 \begin_inset LatexCommand \index{Dead-code elimination}
24991 i = 1; \SpecialChar ~
25000 global = 1;\SpecialChar ~
25013 global = 3;\SpecialChar ~
25038 \begin_inset LatexCommand \index{Copy propagation}
25094 Note: the dead stores created by this copy propagation will be eliminated
25095 by dead-code elimination.
25099 \begin_inset LatexCommand \index{Loop optimization}
25104 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
25111 Two types of loop optimizations are done by SDCC
25119 of loop induction variables.
25120 In addition to the strength reduction the optimizer marks the induction
25121 variables and the register allocator tries to keep the induction variables
25122 in registers for the duration of the loop.
25123 Because of this preference of the register allocator
25124 \begin_inset LatexCommand \index{Register allocation}
25128 , loop induction optimization causes an increase in register pressure, which
25129 may cause unwanted spilling of other temporary variables into the stack
25130 \begin_inset LatexCommand \index{stack}
25135 The compiler will generate a warning message when it is forced to allocate
25136 extra space either on the stack or data space.
25137 If this extra space allocation is undesirable then induction optimization
25138 can be eliminated either for the entire source file (with -
25148 -noinduction option) or for a given function only using #pragma\SpecialChar ~
25150 \begin_inset LatexCommand \index{\#pragma noinduction}
25163 for (i = 0 ; i < 100 ; i ++)
25179 for (i = 0; i < 100; i++)
25188 As mentioned previously some loop invariants are not as apparent, all static
25189 address computations are also moved out of the loop.
25194 \begin_inset LatexCommand \index{Strength reduction}
25198 , this optimization substitutes an expression by a cheaper expression:
25203 for (i=0;i < 100; i++)
25221 for (i=0;i< 100;i++) {
25227 ar[itemp1] = itemp2;
25244 The more expensive multiplication
25245 \begin_inset LatexCommand \index{Multiplication}
25249 is changed to a less expensive addition.
25253 \begin_inset LatexCommand \index{Loop reversing}
25260 This optimization is done to reduce the overhead of checking loop boundaries
25261 for every iteration.
25262 Some simple loops can be reversed and implemented using a
25263 \begin_inset Quotes eld
25266 decrement and jump if not zero
25267 \begin_inset Quotes erd
25271 SDCC checks for the following criterion to determine if a loop is reversible
25272 (note: more sophisticated compilers use data-dependency analysis to make
25273 this determination, SDCC uses a more simple minded analysis).
25276 The 'for' loop is of the form
25282 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
25292 The <for body> does not contain
25293 \begin_inset Quotes eld
25297 \begin_inset Quotes erd
25301 \begin_inset Quotes erd
25307 All goto's are contained within the loop.
25310 No function calls within the loop.
25313 The loop control variable <sym> is not assigned any value within the loop
25316 The loop control variable does NOT participate in any arithmetic operation
25320 There are NO switch statements in the loop.
25323 Algebraic Simplifications
25326 SDCC does numerous algebraic simplifications, the following is a small sub-set
25327 of these optimizations.
25332 i = j + 0;\SpecialChar ~
25336 /* changed to: */\SpecialChar ~
25342 i /= 2;\SpecialChar ~
25349 /* changed to: */\SpecialChar ~
25355 i = j - j;\SpecialChar ~
25359 /* changed to: */\SpecialChar ~
25365 i = j / 1;\SpecialChar ~
25369 /* changed to: */\SpecialChar ~
25376 Note the subexpressions
25377 \begin_inset LatexCommand \index{Subexpression}
25381 given above are generally introduced by macro expansions or as a result
25382 of copy/constant propagation.
25385 'switch' Statements
25386 \begin_inset LatexCommand \label{sub:'switch'-Statements}
25391 \begin_inset LatexCommand \index{switch statement}
25398 SDCC can optimize switch statements to jump tables
25399 \begin_inset LatexCommand \index{jump tables}
25404 It makes the decision based on an estimate of the generated code size.
25405 SDCC is quite liberal in the requirements for jump table generation:
25408 The labels need not be in order, and the starting number need not be one
25409 or zero, the case labels are in numerical sequence or not too many case
25410 labels are missing.
25416 switch(i) {\SpecialChar ~
25447 case 4: ...\SpecialChar ~
25479 case 5: ...\SpecialChar ~
25511 case 3: ...\SpecialChar ~
25542 case 6: ...\SpecialChar ~
25574 case 7: ...\SpecialChar ~
25606 case 8: ...\SpecialChar ~
25638 case 9: ...\SpecialChar ~
25670 case 10: ...\SpecialChar ~
25701 case 11: ...\SpecialChar ~
25768 Both the above switch statements will be implemented using a jump-table.
25769 The example to the right side is slightly more efficient as the check for
25770 the lower boundary of the jump-table is not needed.
25774 The number of case labels is not larger than supported by the target architectur
25778 If the case labels are not in numerical sequence ('gaps' between cases)
25779 SDCC checks whether a jump table with additionally inserted dummy cases
25780 is still attractive.
25784 If the starting number is not zero and a check for the lower boundary of
25785 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
25790 Switch statements which have large gaps in the numeric sequence or those
25791 that have too many case labels can be split into more than one switch statement
25792 for efficient code generation, e.g.:
25872 If the above switch statement is broken down into two switch statements
25962 then both the switch statements will be implemented using jump-tables whereas
25963 the unmodified switch statement will not be.
25966 There might be reasons which SDCC cannot know about to either favour or
25967 not favour jump tables.
25968 If the target system has to be as quick for the last switch case as for
25969 the first (pro jump table), or if the switch argument is known to be zero
25970 in the majority of the cases (contra jump table).
25973 The pragma nojtbound
25974 \begin_inset LatexCommand \index{\#pragma nojtbound}
25978 can be used to turn off checking the
25991 It has no effect if a default label is supplied.
25992 Use of this pragma is dangerous: if the switch
25993 \begin_inset LatexCommand \index{switch statement}
25997 argument is not matched by a case statement the processor will happily
26001 Bit-shifting Operations
26002 \begin_inset LatexCommand \index{Bit shifting}
26009 Bit shifting is one of the most frequently used operation in embedded programmin
26011 SDCC tries to implement bit-shift operations in the most efficient way
26027 generates the following code:
26044 In general SDCC will never setup a loop if the shift count is known.
26087 \begin_inset LatexCommand \index{Bit rotation}
26094 A special case of the bit-shift operation is bit rotation
26095 \begin_inset LatexCommand \index{rotating bits}
26099 , SDCC recognizes the following expression to be a left bit-rotation:
26109 char i;\SpecialChar ~
26120 /* unsigned is needed for rotation */
26125 i = ((i << 1) | (i >> 7));
26134 will generate the following code:
26153 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
26154 ns of this case will also be recognized as bit-rotation, i.e.:
26159 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
26162 Nibble and Byte Swapping
26165 Other special cases of the bit-shift operations are nibble or byte swapping
26166 \begin_inset LatexCommand \index{swapping nibbles/bytes}
26170 , SDCC recognizes the following expressions:
26193 i = ((i << 4) | (i >> 4));
26199 j = ((j << 8) | (j >> 8));
26202 and generates a swap instruction for the nibble swapping
26203 \begin_inset LatexCommand \index{Nibble swapping}
26207 or move instructions for the byte swapping
26208 \begin_inset LatexCommand \index{Byte swapping}
26214 \begin_inset Quotes sld
26218 \begin_inset Quotes srd
26221 example can be used to convert from little to big-endian or vice versa.
26222 If you want to change the endianness of a
26226 integer you have to cast to
26233 Note that SDCC stores numbers in little-endian
26239 Usually 8-bit processors don't care much about endianness.
26240 This is not the case for the standard 8051 which only has an instruction
26246 \begin_inset LatexCommand \index{DPTR}
26254 so little-endian is the more efficient byte order.
26258 \begin_inset LatexCommand \index{little-endian}
26263 \begin_inset LatexCommand \index{Endianness}
26268 lowest order first).
26272 \begin_inset LatexCommand \index{Highest Order Bit}
26277 \begin_inset LatexCommand \index{Any Order Bit}
26284 It is frequently required to obtain the highest order bit of an integral
26285 type (long, int, short or char types).
26286 Also obtaining any other order bit is not uncommon.
26287 SDCC recognizes the following expressions to yield the highest order bit
26288 and generates optimized code for it, e.g.:
26301 unsigned char hob1, aob1;
26305 bit hob2, hob3, aob2, aob3;
26314 hob1 = (gint >> 15) & 1;
26318 hob2 = (gint >> 15) & 1;
26322 hob3 = gint & 0x8000;
26326 aob1 = (gint >> 9) & 1;
26330 aob2 = (gint >> 8) & 1;
26334 aob3 = gint & 0x0800;
26344 will generate the following code:
26377 000A E5*01\SpecialChar ~
26404 000C 23\SpecialChar ~
26435 000D 54 01\SpecialChar ~
26462 000F F5*02\SpecialChar ~
26517 0011 E5*01\SpecialChar ~
26544 0013 33\SpecialChar ~
26574 0014 92*00\SpecialChar ~
26629 0016 E5*01\SpecialChar ~
26656 0018 33\SpecialChar ~
26686 0019 92*01\SpecialChar ~
26741 001B E5*01\SpecialChar ~
26768 001D 03\SpecialChar ~
26799 001E 54 01\SpecialChar ~
26826 0020 F5*03\SpecialChar ~
26881 0022 E5*01\SpecialChar ~
26908 0024 13\SpecialChar ~
26938 0025 92*02\SpecialChar ~
26993 0027 E5*01\SpecialChar ~
27020 0029 A2 E3\SpecialChar ~
27047 002B 92*03\SpecialChar ~
27075 Other variations of these cases however will
27080 They are standard C expressions, so I heartily recommend these be the only
27081 way to get the highest order bit, (it is portable).
27082 Of course it will be recognized even if it is embedded in other expressions,
27088 xyz = gint + ((gint >> 15) & 1);
27091 will still be recognized.
27095 \begin_inset LatexCommand \index{Higher Order Byte}
27099 / Higher Order Word
27100 \begin_inset LatexCommand \index{Higher Order Word}
27107 It is also frequently required to obtain a higher order byte or word of
27108 a larger integral type (long, int or short types).
27109 SDCC recognizes the following expressions to yield the higher order byte
27110 or word and generates optimized code for it, e.g.:
27117 unsigned long int glong;
27125 unsigned char hob1, hob2;
27129 unsigned int how1, how2;
27138 hob1 = (gint >> 8) & 0xFF;
27142 hob2 = glong >> 24;
27146 how1 = (glong >> 16) & 0xFFFF;
27160 will generate the following code:
27193 0037 85*01*06\SpecialChar ~
27215 _foo_hob1_1_1,(_gint + 1)
27245 003A 85*05*07\SpecialChar ~
27267 _foo_hob2_1_1,(_glong + 3)
27297 003D 85*04*08\SpecialChar ~
27319 _foo_how1_1_1,(_glong + 2)
27321 0040 85*05*09\SpecialChar ~
27343 (_foo_how1_1_1 + 1),(_glong + 3)
27345 0043 85*03*0A\SpecialChar ~
27367 _foo_how2_1_1,(_glong + 1)
27369 0046 85*04*0B\SpecialChar ~
27391 (_foo_how2_1_1 + 1),(_glong + 2)
27394 Again, variations of these cases may
27399 They are standard C expressions, so I heartily recommend these be the only
27400 way to get the higher order byte/word, (it is portable).
27401 Of course it will be recognized even if it is embedded in other expressions,
27407 xyz = gint + ((gint >> 8) & 0xFF);
27410 will still be recognized.
27414 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
27419 \begin_inset LatexCommand \index{Peephole optimizer}
27426 The compiler uses a rule based, pattern matching and re-writing mechanism
27427 for peep-hole optimization.
27432 a peep-hole optimizer by Christopher W.
27433 Fraser (cwfraser\SpecialChar ~
27436 A default set of rules are compiled into the compiler, additional rules
27437 may be added with the
27450 \begin_inset LatexCommand \index{-\/-peep-file}
27457 The rule language is best illustrated with examples.
27481 The above rule will change the following assembly
27482 \begin_inset LatexCommand \index{Assembler routines}
27504 Note: All occurrences of a
27508 (pattern variable) must denote the same string.
27509 With the above rule, the assembly sequence:
27519 will remain unmodified.
27523 Other special case optimizations may be added by the user (via
27539 some variants of the 8051 MCU
27540 \begin_inset LatexCommand \index{MCS51 variants}
27553 The following two rules will change all
27572 replace { lcall %1 } by { acall %1 }
27574 replace { ljmp %1 } by { ajmp %1 }
27579 inline-assembler code
27581 is also passed through the peep hole optimizer, thus the peephole optimizer
27582 can also be used as an assembly level macro expander.
27583 The rules themselves are MCU dependent whereas the rule language infra-structur
27584 e is MCU independent.
27585 Peephole optimization rules for other MCU can be easily programmed using
27590 The syntax for a rule is as follows:
27595 rule := replace [ restart ] '{' <assembly sequence> '
27633 <assembly sequence> '
27651 '}' [if <functionName> ] '
27656 <assembly sequence> := assembly instruction (each instruction including
27657 labels must be on a separate line).
27661 The optimizer will apply to the rules one by one from the top in the sequence
27662 of their appearance, it will terminate when all rules are exhausted.
27663 If the 'restart' option is specified, then the optimizer will start matching
27664 the rules again from the top, this option for a rule is expensive (performance)
27665 , it is intended to be used in situations where a transformation will trigger
27666 the same rule again.
27667 An example of this (not a good one, it has side effects) is the following
27690 Note that the replace pattern cannot be a blank, but can be a comment line.
27691 Without the 'restart' option only the innermost 'pop' 'push' pair would
27692 be eliminated, i.e.:
27722 the restart option the rule will be applied again to the resulting code
27723 and then all the pop-push pairs will be eliminated to yield:
27733 A conditional function can be attached to a rule.
27734 Attaching rules are somewhat more involved, let me illustrate this with
27761 The optimizer does a look-up of a function name table defined in function
27766 in the source file SDCCpeeph.c, with the name
27771 If it finds a corresponding entry the function is called.
27772 Note there can be no parameters specified for these functions, in this
27777 is crucial, since the function
27781 expects to find the label in that particular variable (the hash table containin
27782 g the variable bindings is passed as a parameter).
27783 If you want to code more such functions, take a close look at the function
27784 labelInRange and the calling mechanism in source file SDCCpeeph.c.
27785 Currently implemented are
27787 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
27788 24bitMode, portIsDS390, 24bitModeAndPortDS390
27797 I know this whole thing is a little kludgey, but maybe some day we will
27798 have some better means.
27799 If you are looking at this file, you will see the default rules that are
27800 compiled into the compiler, you can add your own rules in the default set
27801 there if you get tired of specifying the -
27815 \begin_inset LatexCommand \index{ANSI-compliance}
27820 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
27827 The latest publically available version of the standard
27829 ISO/IEC 9899 - Programming languages - C
27831 should be available at:
27832 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
27841 Deviations from the compliance:
27844 functions are not reentrant
27845 \begin_inset LatexCommand \index{reentrant}
27849 unless explicitly declared as such or the
27862 \begin_inset LatexCommand \index{-\/-stack-auto}
27868 command line option is specified.
27871 structures and unions cannot be assigned values directly, cannot be passed
27872 as function parameters or assigned to each other and cannot be a return
27873 value from a function, e.g.:
27899 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
27910 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
27932 return rets;/* is invalid in SDCC although allowed in ANSI */
27938 initialization of structure arrays must be fully braced.
27944 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
27946 struct s { char x } a[] = {{1}, {2}}; /* OK */
27951 \begin_inset LatexCommand \index{long long (not supported)}
27956 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
27964 \begin_inset LatexCommand \index{double (not supported)}
27968 ' precision floating point
27969 \begin_inset LatexCommand \index{Floating point support}
27977 \begin_inset LatexCommand \index{K\&R style}
27981 function declarations are NOT allowed.
27987 foo(i,j) /* this old style of function declarations */
27989 int i,j; /* are valid in ANSI but not valid in SDCC */
28004 Most enhancements in C99 are not supported, f.e.:
28013 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
28020 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
28024 Certain words that are valid identifiers in the standard may be reserved
28025 words in SDCC unless the
28038 \begin_inset LatexCommand \index{-\/-std-c89}
28053 \begin_inset LatexCommand \index{-\/-std-c99}
28059 command line options are used.
28060 These may include (depending on the selected processor): 'at', 'banked',
28061 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
28062 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
28063 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
28065 Compliant equivalents of these keywords are always available in a form
28066 that begin with two underscores
28067 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
28072 '__data' instead of 'data'.
28075 Cyclomatic Complexity
28076 \begin_inset LatexCommand \index{Cyclomatic complexity}
28083 Cyclomatic complexity of a function is defined as the number of independent
28084 paths the program can take during execution of the function.
28085 This is an important number since it defines the number test cases you
28086 have to generate to validate the function.
28087 The accepted industry standard for complexity number is 10, if the cyclomatic
28088 complexity reported by SDCC exceeds 10 you should think about simplification
28089 of the function logic.
28090 Note that the complexity level is not related to the number of lines of
28091 code in a function.
28092 Large functions can have low complexity, and small functions can have large
28098 SDCC uses the following formula to compute the complexity:
28103 complexity = (number of edges in control flow graph) - (number of nodes
28104 in control flow graph) + 2;
28108 Having said that the industry standard is 10, you should be aware that in
28109 some cases it be may unavoidable to have a complexity level of less than
28111 For example if you have switch statement with more than 10 case labels,
28112 each case label adds one to the complexity level.
28113 The complexity level is by no means an absolute measure of the algorithmic
28114 complexity of the function, it does however provide a good starting point
28115 for which functions you might look at for further optimization.
28118 Retargetting for other Processors
28121 The issues for retargetting the compiler are far too numerous to be covered
28123 What follows is a brief description of each of the seven phases of the
28124 compiler and its MCU dependency.
28127 Parsing the source and building the annotated parse tree.
28128 This phase is largely MCU independent (except for the language extensions).
28129 Syntax & semantic checks are also done in this phase, along with some initial
28130 optimizations like back patching labels and the pattern matching optimizations
28131 like bit-rotation etc.
28134 The second phase involves generating an intermediate code which can be easy
28135 manipulated during the later phases.
28136 This phase is entirely MCU independent.
28137 The intermediate code generation assumes the target machine has unlimited
28138 number of registers, and designates them with the name iTemp.
28139 The compiler can be made to dump a human readable form of the code generated
28153 This phase does the bulk of the standard optimizations and is also MCU independe
28155 This phase can be broken down into several sub-phases:
28159 Break down intermediate code (iCode) into basic blocks.
28161 Do control flow & data flow analysis on the basic blocks.
28163 Do local common subexpression elimination, then global subexpression elimination
28165 Dead code elimination
28169 If loop optimizations caused any changes then do 'global subexpression eliminati
28170 on' and 'dead code elimination' again.
28173 This phase determines the live-ranges; by live range I mean those iTemp
28174 variables defined by the compiler that still survive after all the optimization
28176 Live range analysis
28177 \begin_inset LatexCommand \index{Live range analysis}
28181 is essential for register allocation, since these computation determines
28182 which of these iTemps will be assigned to registers, and for how long.
28185 Phase five is register allocation.
28186 There are two parts to this process.
28190 The first part I call 'register packing' (for lack of a better term).
28191 In this case several MCU specific expression folding is done to reduce
28196 The second part is more MCU independent and deals with allocating registers
28197 to the remaining live ranges.
28198 A lot of MCU specific code does creep into this phase because of the limited
28199 number of index registers available in the 8051.
28202 The Code generation phase is (unhappily), entirely MCU dependent and very
28203 little (if any at all) of this code can be reused for other MCU.
28204 However the scheme for allocating a homogenized assembler operand for each
28205 iCode operand may be reused.
28208 As mentioned in the optimization section the peep-hole optimizer is rule
28209 based system, which can reprogrammed for other MCUs.
28213 \begin_inset LatexCommand \index{Compiler internals}
28220 The anatomy of the compiler
28221 \begin_inset LatexCommand \label{sub:The-anatomy-of}
28230 This is an excerpt from an article published in Circuit Cellar Magazine
28236 It's a little outdated (the compiler is much more efficient now and user/develo
28237 per friendly), but pretty well exposes the guts of it all.
28243 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
28244 It is fairly easy to retarget for other 8-bit MCU.
28245 Here we take a look at some of the internals of the compiler.
28250 \begin_inset LatexCommand \index{Parsing}
28257 Parsing the input source file and creating an AST (Annotated Syntax Tree
28258 \begin_inset LatexCommand \index{Annotated syntax tree}
28263 This phase also involves propagating types (annotating each node of the
28264 parse tree with type information) and semantic analysis.
28265 There are some MCU specific parsing rules.
28266 For example the storage classes, the extended storage classes are MCU specific
28267 while there may be a xdata storage class for 8051 there is no such storage
28268 class for z80 or Atmel AVR.
28269 SDCC allows MCU specific storage class extensions, i.e.
28270 xdata will be treated as a storage class specifier when parsing 8051 C
28271 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
28276 \begin_inset LatexCommand \index{iCode}
28283 Intermediate code generation.
28284 In this phase the AST is broken down into three-operand form (iCode).
28285 These three operand forms are represented as doubly linked lists.
28286 ICode is the term given to the intermediate form generated by the compiler.
28287 ICode example section shows some examples of iCode generated for some simple
28288 C source functions.
28292 \begin_inset LatexCommand \index{Optimizations}
28299 Bulk of the target independent optimizations is performed in this phase.
28300 The optimizations include constant propagation, common sub-expression eliminati
28301 on, loop invariant code movement, strength reduction of loop induction variables
28302 and dead-code elimination.
28305 Live range analysis
28306 \begin_inset LatexCommand \index{Live range analysis}
28313 During intermediate code generation phase, the compiler assumes the target
28314 machine has infinite number of registers and generates a lot of temporary
28316 The live range computation determines the lifetime of each of these compiler-ge
28317 nerated temporaries.
28318 A picture speaks a thousand words.
28319 ICode example sections show the live range annotations for each of the
28321 It is important to note here, each iCode is assigned a number in the order
28322 of its execution in the function.
28323 The live ranges are computed in terms of these numbers.
28324 The from number is the number of the iCode which first defines the operand
28325 and the to number signifies the iCode which uses this operand last.
28328 Register Allocation
28329 \begin_inset LatexCommand \index{Register allocation}
28336 The register allocation determines the type and number of registers needed
28338 In most MCUs only a few registers can be used for indirect addressing.
28339 In case of 8051 for example the registers R0 & R1 can be used to indirectly
28340 address the internal ram and DPTR to indirectly address the external ram.
28341 The compiler will try to allocate the appropriate register to pointer variables
28343 ICode example section shows the operands annotated with the registers assigned
28345 The compiler will try to keep operands in registers as much as possible;
28346 there are several schemes the compiler uses to do achieve this.
28347 When the compiler runs out of registers the compiler will check to see
28348 if there are any live operands which is not used or defined in the current
28349 basic block being processed, if there are any found then it will push that
28350 operand and use the registers in this block, the operand will then be popped
28351 at the end of the basic block.
28355 There are other MCU specific considerations in this phase.
28356 Some MCUs have an accumulator; very short-lived operands could be assigned
28357 to the accumulator instead of a general-purpose register.
28363 Figure II gives a table of iCode operations supported by the compiler.
28364 The code generation involves translating these operations into corresponding
28365 assembly code for the processor.
28366 This sounds overly simple but that is the essence of code generation.
28367 Some of the iCode operations are generated on a MCU specific manner for
28368 example, the z80 port does not use registers to pass parameters so the
28369 SEND and RECV iCode operations will not be generated, and it also does
28370 not support JUMPTABLES.
28377 <Where is Figure II?>
28380 In the original article Figure II was announced to be downloadable on
28385 Unfortunately it never seemed to have shown up there, so: where is Figure
28390 \begin_inset LatexCommand \index{iCode}
28397 This section shows some details of iCode.
28398 The example C code does not do anything useful; it is used as an example
28399 to illustrate the intermediate code generated by the compiler.
28411 /* This function does nothing useful.
28418 for the purpose of explaining iCode */
28421 short function (data int *x)
28429 short i=10; \SpecialChar ~
28431 /* dead initialization eliminated */
28436 short sum=10; /* dead initialization eliminated */
28449 while (*x) *x++ = *p++;
28463 /* compiler detects i,j to be induction variables */
28467 for (i = 0, j = 10 ; i < 10 ; i++, j
28493 mul += i * 3; \SpecialChar ~
28495 /* this multiplication remains */
28501 gint += j * 3;\SpecialChar ~
28503 /* this multiplication changed to addition */
28517 In addition to the operands each iCode contains information about the filename
28518 and line it corresponds to in the source file.
28519 The first field in the listing should be interpreted as follows:
28524 Filename(linenumber: iCode Execution sequence number : ICode hash table
28525 key : loop depth of the iCode).
28530 Then follows the human readable form of the ICode operation.
28531 Each operand of this triplet form can be of three basic types a) compiler
28532 generated temporary b) user defined variable c) a constant value.
28533 Note that local variables and parameters are replaced by compiler generated
28536 \begin_inset LatexCommand \index{Live range analysis}
28540 are computed only for temporaries (i.e.
28541 live ranges are not computed for global variables).
28543 \begin_inset LatexCommand \index{Register allocation}
28547 are allocated for temporaries only.
28548 Operands are formatted in the following manner:
28553 Operand Name [lr live-from : live-to ] { type information } [ registers
28559 As mentioned earlier the live ranges are computed in terms of the execution
28560 sequence number of the iCodes, for example
28562 the iTemp0 is live from (i.e.
28563 first defined in iCode with execution sequence number 3, and is last used
28564 in the iCode with sequence number 5).
28565 For induction variables such as iTemp21 the live range computation extends
28566 the lifetime from the start to the end of the loop.
28568 The register allocator used the live range information to allocate registers,
28569 the same registers may be used for different temporaries if their live
28570 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
28571 iTemp17 since their live ranges do not overlap.
28572 In addition the allocator also takes into consideration the type and usage
28573 of a temporary, for example itemp6 is a pointer to near space and is used
28574 as to fetch data from (i.e.
28575 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
28576 Some short lived temporaries are allocated to special registers which have
28577 meaning to the code generator e.g.
28578 iTemp13 is allocated to a pseudo register CC which tells the back end that
28579 the temporary is used only for a conditional jump the code generation makes
28580 use of this information to optimize a compare and jump ICode.
28582 There are several loop optimizations
28583 \begin_inset LatexCommand \index{Loop optimization}
28587 performed by the compiler.
28588 It can detect induction variables iTemp21(i) and iTemp23(j).
28589 Also note the compiler does selective strength reduction
28590 \begin_inset LatexCommand \index{Strength reduction}
28595 the multiplication of an induction variable in line 18 (gint = j * 3) is
28596 changed to addition, a new temporary iTemp17 is allocated and assigned
28597 a initial value, a constant 3 is then added for each iteration of the loop.
28598 The compiler does not change the multiplication
28599 \begin_inset LatexCommand \index{Multiplication}
28603 in line 17 however since the processor does support an 8 * 8 bit multiplication.
28605 Note the dead code elimination
28606 \begin_inset LatexCommand \index{Dead-code elimination}
28610 optimization eliminated the dead assignments in line 7 & 8 to I and sum
28618 Sample.c (5:1:0:0) _entry($9) :
28623 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
28628 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
28633 Sample.c(11:4:53:0) preHeaderLbl0($11) :
28638 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
28644 Sample.c(11:6:5:1) _whilecontinue_0($1) :
28649 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
28655 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28660 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
28666 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
28672 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
28678 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
28684 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
28685 * int}[r0] + 0x2 {short}
28690 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
28695 Sample.c(11:17:21:0)_whilebreak_0($3) :
28700 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28705 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28710 Sample.c(15:20:54:0)preHeaderLbl1($13) :
28715 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28720 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28725 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28730 Sample.c(15:24:26:1)_forcond_0($4) :
28735 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
28741 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28746 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
28747 + ITemp21 [lr21:38]{short}[r4]
28752 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
28758 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
28759 + iTemp15 [lr29:30]{short}[r1]
28764 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
28770 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
28776 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
28782 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
28788 Sample.c(19:38:47:1) goto _forcond_0($4)
28793 Sample.c(19:39:48:0)_forbreak_0($7) :
28798 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
28799 + ITemp11 [lr19:40]{short}[r3]
28804 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
28809 Sample.c(20:42:51:0)_return($8) :
28814 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
28820 Finally the code generated for this function:
28861 ; ----------------------------------------------
28866 ; function function
28871 ; ----------------------------------------------
28881 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
28893 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
28905 ;_whilecontinue_0($1) :
28915 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28920 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28979 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
28998 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
29045 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
29085 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
29111 ; iTemp6 [lr5:16]{_near * int}[r0] =
29116 ; iTemp6 [lr5:16]{_near * int}[r0] +
29133 ; goto _whilecontinue_0($1)
29145 ; _whilebreak_0($3) :
29155 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29167 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29179 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29191 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29210 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29239 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
29244 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29289 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
29294 ; iTemp21 [lr21:38]{short}[r4]
29320 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
29353 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
29358 ; iTemp15 [lr29:30]{short}[r1]
29377 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
29424 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
29471 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
29483 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
29497 cjne r5,#0xff,00104$
29509 ; goto _forcond_0($4)
29521 ; _forbreak_0($7) :
29531 ; ret iTemp24 [lr40:41]{short}
29574 A few words about basic block successors, predecessors and dominators
29577 Successors are basic blocks
29578 \begin_inset LatexCommand \index{Basic blocks}
29582 that might execute after this basic block.
29584 Predecessors are basic blocks that might execute before reaching this basic
29587 Dominators are basic blocks that WILL execute before reaching this basic
29621 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
29624 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
29627 c) domVect of [BB4] = BB1 ...
29628 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
29636 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
29646 Thanks to all the other volunteer developers who have helped with coding,
29647 testing, web-page creation, distribution sets, etc.
29648 You know who you are :-)
29655 This document was initially written by Sandeep Dutta
29658 All product names mentioned herein may be trademarks
29659 \begin_inset LatexCommand \index{Trademarks}
29663 of their respective companies.
29670 To avoid confusion, the installation and building options for SDCC itself
29671 (chapter 2) are not part of the index.
29675 \begin_inset LatexCommand \printindex{}