1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={68hc08 8032 8051 ansi c compiler CPU DS390 embedded free Freescale GPL HC08 Intel manual Maxim mcs51 Microchip microcontroller open source PIC Z80 Zilog},
11 pdfpagemode=UseOutlines,
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by Subversion
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 Zilog Z80 based MCUs.
145 It can be retargeted for other microprocessors, support for Microchip PIC,
146 Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargetable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
209 <lyxtabular version="3" rows="8" columns="5">
211 <column alignment="center" valignment="top" leftline="true" width="0">
212 <column alignment="center" valignment="top" leftline="true" width="0">
213 <column alignment="center" valignment="top" leftline="true" width="0">
214 <column alignment="center" valignment="top" leftline="true" width="0">
215 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
216 <row topline="true" bottomline="true">
217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
249 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
301 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
317 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
401 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
409 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
417 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
426 <row topline="true" bottomline="true">
427 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
443 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
456 -2.147.483.648, +2.147.483.647
459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
468 <row topline="true" bottomline="true">
469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
508 <row topline="true" bottomline="true">
509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
517 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
540 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
554 The compiler also allows
556 inline assembler code
558 to be embedded anywhere in a function.
559 In addition, routines developed in assembly can also be called.
563 SDCC also provides an option (-
573 -cyclomatic) to report the relative complexity of a function.
574 These functions can then be further optimized, or hand coded in assembly
580 SDCC also comes with a companion source level debugger SDCDB, the debugger
581 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
586 The latest version can be downloaded from
587 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
597 Please note: the compiler will probably always be some steps ahead of this
602 \begin_inset LatexCommand \index{Status of documentation}
612 Obviously this has pros and cons
621 All packages used in this compiler system are
629 ; source code for all the sub-packages (pre-processor, assemblers, linkers
630 etc) is distributed with the package.
631 This documentation is maintained using a freeware word processor (LyX).
633 This program is free software; you can redistribute it and/or modify it
634 under the terms of the GNU General Public License
635 \begin_inset LatexCommand \index{GNU General Public License, GPL}
639 as published by the Free Software Foundation; either version 2, or (at
640 your option) any later version.
641 This program is distributed in the hope that it will be useful, but WITHOUT
642 ANY WARRANTY; without even the implied warranty
643 \begin_inset LatexCommand \index{warranty}
647 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
648 See the GNU General Public License for more details.
649 You should have received a copy of the GNU General Public License along
650 with this program; if not, write to the Free Software Foundation, 59 Temple
651 Place - Suite 330, Boston, MA 02111-1307, USA.
652 In other words, you are welcome to use, share and improve this program.
653 You are forbidden to forbid anyone else to use, share and improve what
655 Help stamp out software-hoarding!
658 Typographic conventions
659 \begin_inset LatexCommand \index{Typographic conventions}
666 Throughout this manual, we will use the following convention.
667 Commands you have to type in are printed in
675 Code samples are printed in
680 Interesting items and new terms are printed in
686 \begin_inset LatexCommand \label{sec:Compatibility-with-previous}
690 with previous versions
691 \begin_inset LatexCommand \index{Compatibility with previous versions}
698 This version has numerous bug fixes compared with the previous version.
699 But we also introduced some incompatibilities with older versions.
700 Not just for the fun of it, but to make the compiler more stable, efficient
702 \begin_inset LatexCommand \index{ANSI-compliance}
707 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
711 for ANSI-Compliance).
717 short is now equivalent to int (16 bits), it used to be equivalent to char
718 (8 bits) which is not ANSI compliant.
721 the default directory for gcc-builds where include, library and documentation
722 files are stored is now in /usr/local/share.
725 char type parameters to vararg functions are casted to int unless explicitly
742 will push a as an int and as a char resp.
755 -regextend has been removed.
768 -noregparms has been removed.
781 -stack-after-data has been removed.
785 \begin_inset LatexCommand \index{bit}
790 \begin_inset LatexCommand \index{sbit}
795 \begin_inset LatexCommand \index{\_\_sbit}
799 types now consistently behave like the C99 _Bool type with respect to type
801 \begin_inset LatexCommand \index{type conversion}
806 \begin_inset LatexCommand \index{type promotion}
811 The most common incompatibility resulting from this change is related to
813 \begin_inset LatexCommand \index{Bit toggling}
827 b = ~b; /* equivalent to b=1 instead of toggling b */
828 \begin_inset Marginal
843 b = !b; /* toggles b */
847 In previous versions, both forms would have toggled the bit.
852 <pending: more incompatibilities?>
858 What do you need before you start installation of SDCC? A computer, and
860 The preferred method of installation is to compile SDCC from source using
862 For Windows some pre-compiled binary distributions are available for your
864 You should have some experience with command line tools and compiler use.
870 The SDCC home page at
871 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
875 is a great place to find distribution sets.
876 You can also find links to the user mailing lists that offer help or discuss
877 SDCC with other SDCC users.
878 Web links to other SDCC related sites can also be found here.
879 This document can be found in the DOC directory of the source package as
881 A pdf version of this document is available at
882 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
887 Some of the other tools (simulator and assembler) included with SDCC contain
888 their own documentation and can be found in the source distribution.
889 If you want the latest unreleased software, the complete source package
890 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
894 Wishes for the future
897 There are (and always will be) some things that could be done.
898 Here are some I can think of:
905 char KernelFunction3(char p) at 0x340;
913 \begin_inset LatexCommand \index{code banking (limited support)}
923 If you can think of some more, please see the section
924 \begin_inset LatexCommand \ref{sub:Requesting-Features}
928 about filing feature requests
929 \begin_inset LatexCommand \index{Requesting features}
934 \begin_inset LatexCommand \index{Feature request}
944 \begin_inset LatexCommand \index{Installation}
951 For most users it is sufficient to skip to either section
952 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
957 \begin_inset LatexCommand \ref{sub:Windows-Install}
962 More detailed instructions follow below.
966 \begin_inset LatexCommand \index{Options SDCC configuration}
973 The install paths, search paths and other options are defined when running
975 The defaults can be overridden by:
977 \labelwidthstring 00.00.0000
989 -prefix see table below
991 \labelwidthstring 00.00.0000
1003 -exec_prefix see table below
1005 \labelwidthstring 00.00.0000
1017 -bindir see table below
1019 \labelwidthstring 00.00.0000
1031 -datadir see table below
1033 \labelwidthstring 00.00.0000
1035 docdir environment variable, see table below
1037 \labelwidthstring 00.00.0000
1039 include_dir_suffix environment variable, see table below
1041 \labelwidthstring 00.00.0000
1043 lib_dir_suffix environment variable, see table below
1045 \labelwidthstring 00.00.0000
1047 sdccconf_h_dir_separator environment variable, either / or
1052 This character will only be used in sdccconf.h; don't forget it's a C-header,
1053 therefore a double-backslash is needed there.
1055 \labelwidthstring 00.00.0000
1067 -disable-mcs51-port Excludes the Intel mcs51 port
1069 \labelwidthstring 00.00.0000
1081 -disable-gbz80-port Excludes the Gameboy gbz80 port
1083 \labelwidthstring 00.00.0000
1095 -disable-z80-port Excludes the z80 port
1097 \labelwidthstring 00.00.0000
1109 -disable-avr-port Excludes the AVR port
1111 \labelwidthstring 00.00.0000
1123 -disable-ds390-port Excludes the DS390 port
1125 \labelwidthstring 00.00.0000
1137 -disable-hc08-port Excludes the HC08 port
1139 \labelwidthstring 00.00.0000
1151 -disable-pic-port Excludes the PIC port
1153 \labelwidthstring 00.00.0000
1165 -disable-xa51-port Excludes the XA51 port
1167 \labelwidthstring 00.00.0000
1179 -disable-ucsim Disables configuring and building of ucsim
1181 \labelwidthstring 00.00.0000
1193 -disable-device-lib Disables automatically building device libraries
1195 \labelwidthstring 00.00.0000
1207 -disable-packihx Disables building packihx
1209 \labelwidthstring 00.00.0000
1221 -enable-doc Build pdf, html and txt files from the lyx sources
1223 \labelwidthstring 00.00.0000
1235 -enable-libgc Use the Bohem memory allocator.
1236 Lower runtime footprint.
1239 Furthermore the environment variables CC, CFLAGS, ...
1240 the tools and their arguments can be influenced.
1241 Please see `configure -
1251 -help` and the man/info pages of `configure` for details.
1255 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
1256 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
1257 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
1258 At the moment it's not possible to change the default settings (it was
1259 simply never required).
1263 These configure options are compiled into the binaries, and can only be
1264 changed by rerunning 'configure' and recompiling SDCC.
1265 The configure options are written in
1269 to distinguish them from run time environment variables (see section search
1275 \begin_inset Quotes sld
1279 \begin_inset Quotes srd
1282 are used by the SDCC team to build the official Win32 binaries.
1283 The SDCC team uses Mingw32 to build the official Windows binaries, because
1290 a gcc compiler and last but not least
1293 the binaries can be built by cross compiling on Sourceforge's compile farm.
1296 See the examples, how to pass the Win32 settings to 'configure'.
1297 The other Win32 builds using Borland, VC or whatever don't use 'configure',
1298 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
1309 \begin_inset Tabular
1310 <lyxtabular version="3" rows="8" columns="3">
1312 <column alignment="block" valignment="top" leftline="true" width="0in">
1313 <column alignment="block" valignment="top" leftline="true" width="0in">
1314 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1315 <row topline="true" bottomline="true">
1316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1341 <row topline="true">
1342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1360 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1371 <row topline="true">
1372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1403 <row topline="true">
1404 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1414 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1426 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1441 <row topline="true">
1442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1452 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1464 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1475 <row topline="true">
1476 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1513 <row topline="true">
1514 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1524 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1541 <row topline="true" bottomline="true">
1542 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1552 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1560 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1578 'configure' also computes relative paths.
1579 This is needed for full relocatability of a binary package and to complete
1580 search paths (see section search paths below):
1586 \begin_inset Tabular
1587 <lyxtabular version="3" rows="4" columns="3">
1589 <column alignment="block" valignment="top" leftline="true" width="0in">
1590 <column alignment="block" valignment="top" leftline="true" width="0in">
1591 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1592 <row topline="true" bottomline="true">
1593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1601 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1618 <row topline="true" bottomline="true">
1619 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1646 <row bottomline="true">
1647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1674 <row bottomline="true">
1675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1685 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1726 \begin_inset Quotes srd
1730 \begin_inset Quotes srd
1744 \begin_inset Quotes srd
1748 \begin_inset Quotes srd
1776 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1785 \begin_inset Quotes srd
1788 i586-mingw32msvc-gcc
1789 \begin_inset Quotes srd
1793 \begin_inset Quotes srd
1796 i586-mingw32msvc-g++
1797 \begin_inset Quotes srd
1805 \begin_inset Quotes srd
1808 i586-mingw32msvc-ranlib
1809 \begin_inset Quotes srd
1817 \begin_inset Quotes srd
1820 i586-mingw32msvc-strip
1821 \begin_inset Quotes srd
1839 \begin_inset Quotes srd
1843 \begin_inset Quotes srd
1861 \begin_inset Quotes srd
1865 \begin_inset Quotes srd
1873 \begin_inset Quotes srd
1877 \begin_inset Quotes srd
1885 \begin_inset Quotes srd
1889 \begin_inset Quotes srd
1897 \begin_inset Quotes srd
1901 \begin_inset Quotes srd
1908 sdccconf_h_dir_separator=
1909 \begin_inset Quotes srd
1921 \begin_inset Quotes srd
1952 -host=i586-mingw32msvc
1966 -build=unknown-unknown-linux-gnu
1970 \begin_inset Quotes sld
1974 \begin_inset Quotes srd
1977 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1996 \begin_inset Quotes srd
2000 \begin_inset Quotes srd
2018 \begin_inset Quotes srd
2022 \begin_inset Quotes srd
2030 \begin_inset Quotes srd
2034 \begin_inset Quotes srd
2042 \begin_inset Quotes srd
2046 \begin_inset Quotes srd
2054 \begin_inset Quotes srd
2058 \begin_inset Quotes srd
2065 sdccconf_h_dir_separator=
2066 \begin_inset Quotes srd
2078 \begin_inset Quotes srd
2086 \begin_inset Quotes srd
2090 \begin_inset Quotes srd
2098 \begin_inset Quotes srd
2102 \begin_inset Quotes srd
2108 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2119 -C' turns on caching, which gives a little bit extra speed.
2120 However if options are changed, it can be necessary to delete the config.cache
2125 \begin_inset LatexCommand \label{sub:Install-paths}
2130 \begin_inset LatexCommand \index{Install paths}
2136 \added_space_top medskip \align center
2138 \begin_inset Tabular
2139 <lyxtabular version="3" rows="5" columns="4">
2141 <column alignment="left" valignment="top" leftline="true" width="0">
2142 <column alignment="left" valignment="top" leftline="true" width="0">
2143 <column alignment="left" valignment="top" leftline="true" width="0">
2144 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2145 <row topline="true" bottomline="true">
2146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2156 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2187 <row topline="true">
2188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2196 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2227 <row topline="true">
2228 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2243 $DATADIR/ $INCLUDE_DIR_SUFFIX
2246 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2251 /usr/local/share/sdcc/include
2254 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2267 <row topline="true">
2268 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2276 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2283 $DATADIR/$LIB_DIR_SUFFIX
2286 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2291 /usr/local/share/sdcc/lib
2294 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2307 <row topline="true" bottomline="true">
2308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2331 /usr/local/share/sdcc/doc
2334 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2356 *compiler, preprocessor, assembler, and linker
2362 is auto-appended by the compiler, e.g.
2363 small, large, z80, ds390 etc
2366 The install paths can still be changed during `make install` with e.g.:
2369 make install prefix=$(HOME)/local/sdcc
2372 Of course this doesn't change the search paths compiled into the binaries.
2376 Moreover the install path can be changed by defining DESTDIR
2377 \begin_inset LatexCommand \index{DESTDIR}
2384 make install DESTDIR=$(HOME)/sdcc.rpm/
2387 Please note that DESTDIR must have a trailing slash!
2391 \begin_inset LatexCommand \label{sub:Search-Paths}
2396 \begin_inset LatexCommand \index{Search path}
2403 Some search paths or parts of them are determined by configure variables
2408 , see section above).
2409 Further search paths are determined by environment variables during runtime.
2412 The paths searched when running the compiler are as follows (the first catch
2418 Binary files (preprocessor, assembler and linker)
2424 \begin_inset Tabular
2425 <lyxtabular version="3" rows="4" columns="3">
2427 <column alignment="block" valignment="top" leftline="true" width="0in">
2428 <column alignment="block" valignment="top" leftline="true" width="0in">
2429 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2430 <row topline="true" bottomline="true">
2431 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2447 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2456 <row topline="true">
2457 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2467 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2486 <row topline="true">
2487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2492 Path of argv[0] (if available)
2495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2512 <row topline="true" bottomline="true">
2513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2529 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2554 \begin_inset Tabular
2555 <lyxtabular version="3" rows="6" columns="3">
2557 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2558 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2559 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2560 <row topline="true" bottomline="true">
2561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2569 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2577 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2586 <row topline="true">
2587 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2623 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2642 <row topline="true">
2643 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2651 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2668 <row topline="true">
2669 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2683 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2695 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2706 <row topline="true">
2707 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2788 <row topline="true" bottomline="true">
2789 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2805 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2810 /usr/local/share/sdcc/
2815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2843 -nostdinc disables the last two search paths.
2853 With the exception of
2854 \begin_inset Quotes sld
2868 \begin_inset Quotes srd
2875 is auto-appended by the compiler (e.g.
2876 small, large, z80, ds390 etc.).
2883 \begin_inset Tabular
2884 <lyxtabular version="3" rows="6" columns="3">
2886 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2887 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2888 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2889 <row topline="true" bottomline="true">
2890 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2898 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2915 <row topline="true">
2916 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2971 <row topline="true">
2972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2984 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3011 <row topline="true">
3012 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3023 $LIB_DIR_SUFFIX/<model>
3026 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3040 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3057 <row topline="true">
3058 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3073 $LIB_DIR_SUFFIX/<model>
3076 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3185 <row topline="true" bottomline="true">
3186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3195 $LIB_DIR_SUFFIX/<model>
3198 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3203 /usr/local/share/sdcc/
3210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3228 Don't delete any of the stray spaces in the table above without checking
3229 the HTML output (last line)!
3245 -nostdlib disables the last two search paths.
3249 \begin_inset LatexCommand \index{Building SDCC}
3256 Building SDCC on Linux
3257 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3266 Download the source package
3268 either from the SDCC Subversion repository or from the nightly snapshots
3270 , it will be named something like sdcc
3281 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3290 Bring up a command line terminal, such as xterm.
3295 Unpack the file using a command like:
3298 "tar -xvzf sdcc.src.tar.gz
3303 , this will create a sub-directory called sdcc with all of the sources.
3306 Change directory into the main SDCC directory, for example type:
3323 This configures the package for compilation on your system.
3339 All of the source packages will compile, this can take a while.
3355 This copies the binary executables, the include files, the libraries and
3356 the documentation to the install directories.
3357 Proceed with section
3358 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3365 Building SDCC on OSX 2.x
3368 Follow the instruction for Linux.
3372 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3373 )) fails to compile SDCC.
3374 Fortunately there's also gcc 2.9.x installed, which works fine.
3375 This compiler can be selected by running 'configure' with:
3378 ./configure CC=gcc2 CXX=g++2
3381 Cross compiling SDCC on Linux for Windows
3384 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3385 See section 'Configure Options'.
3388 Building SDCC using Cygwin and Mingw32
3391 For building and installing a Cygwin executable follow the instructions
3397 \begin_inset Quotes sld
3401 \begin_inset Quotes srd
3404 Win32-binary can be built, which will not need the Cygwin-DLL.
3405 For the necessary 'configure' options see section 'configure options' or
3406 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3410 In order to install Cygwin on Windows download setup.exe from
3411 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3417 \begin_inset Quotes sld
3420 default text file type
3421 \begin_inset Quotes srd
3425 \begin_inset Quotes sld
3429 \begin_inset Quotes srd
3432 and download/install at least the following packages.
3433 Some packages are selected by default, others will be automatically selected
3434 because of dependencies with the manually selected packages.
3435 Never deselect these packages!
3444 gcc ; version 3.x is fine, no need to use the old 2.9x
3447 binutils ; selected with gcc
3453 rxvt ; a nice console, which makes life much easier under windoze (see below)
3456 man ; not really needed for building SDCC, but you'll miss it sooner or
3460 less ; not really needed for building SDCC, but you'll miss it sooner or
3464 svn ; only if you use Subversion access
3467 If you want to develop something you'll need:
3470 python ; for the regression tests
3473 gdb ; the gnu debugger, together with the nice GUI
3474 \begin_inset Quotes sld
3478 \begin_inset Quotes srd
3484 openssh ; to access the CF or commit changes
3487 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3488 use autoconf-stable!
3491 rxvt is a nice console with history.
3492 Replace in your cygwin.bat the line
3511 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3514 -bg black -fg white -geometry 100x65 -e bash -
3527 Text selected with the mouse is automatically copied to the clipboard, pasting
3528 works with shift-insert.
3532 The other good tip is to make sure you have no //c/-style paths anywhere,
3533 use /cygdrive/c/ instead.
3534 Using // invokes a network lookup which is very slow.
3536 \begin_inset Quotes sld
3540 \begin_inset Quotes srd
3543 is too long, you can change it with e.g.
3549 SDCC sources use the unix line ending LF.
3550 Life is much easier, if you store the source tree on a drive which is mounted
3552 And use an editor which can handle LF-only line endings.
3553 Make sure not to commit files with windows line endings.
3554 The tabulator spacing
3555 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3559 used in the project is 8.
3560 Although a tabulator spacing of 8 is a sensible choice for programmers
3561 (it's a power of 2 and allows to display 8/16 bit signed variables without
3562 loosing columns) the plan is to move towards using only spaces in the source.
3565 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3570 Download the source package
3572 either from the SDCC Subversion repository or from the
3573 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3579 , it will be named something like sdcc
3586 SDCC is distributed with all the projects, workspaces, and files you need
3587 to build it using Visual C++ 6.0/NET (except for SDCDB and ucSim).
3588 The workspace name is 'sdcc.dsw'.
3589 Please note that as it is now, all the executables are created in a folder
3593 Once built you need to copy the executables from sdcc
3597 bin before running SDCC.
3602 WARNING: Visual studio is very picky with line terminations; it expects
3603 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3604 When using the Subversion repository it's easiest to configure the svn
3605 client to convert automatically for you.
3606 If however you are getting a message such as "This makefile was not generated
3607 by Developer Studio etc.
3609 \begin_inset Quotes srd
3612 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3613 need to convert the Unix style line endings to DOS style line endings.
3614 To do so you can use the
3615 \begin_inset Quotes sld
3619 \begin_inset Quotes srd
3622 utility freely available on the internet.
3623 Doug Hawkins reported in the sdcc-user list that this works:
3631 SDCC> unix2dos sdcc.dsw
3637 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3641 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3642 flex.exe, and gawk.exe.
3643 One good place to get them is
3644 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3652 Download the file UnxUtils
3653 \begin_inset LatexCommand \index{UnxUtils}
3658 Now you have to install the utilities and setup MSVC so it can locate the
3660 Here there are two alternatives (choose one!):
3667 a) Extract UnxUtils.zip to your C:
3669 hard disk PRESERVING the original paths, otherwise bison won't work.
3670 (If you are using WinZip make certain that 'Use folder names' is selected)
3674 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3675 in 'Show directories for:' select 'Executable files', and in the directories
3676 window add a new path: 'C:
3686 (As a side effect, you get a bunch of Unix utilities that could be useful,
3687 such as diff and patch.)
3694 This one avoids extracting a bunch of files you may not use, but requires
3699 a) Create a directory were to put the tools needed, or use a directory already
3707 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3708 to such directory WITHOUT preserving the original paths.
3709 (If you are using WinZip make certain that 'Use folder names' is not selected)
3713 c) Rename bison.exe to '_bison.exe'.
3717 d) Create a batch file 'bison.bat' in 'C:
3721 ' and add these lines:
3741 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3745 Steps 'c' and 'd' are needed because bison requires by default that the
3746 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3747 '/usr/local/share/' I think.
3748 So it is necessary to tell bison where those files are located if they
3749 are not in such directory.
3750 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3754 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3755 in 'Show directories for:' select 'Executable files', and in the directories
3756 window add a new path: 'c:
3759 Note that you can use any other path instead of 'c:
3761 util', even the path where the Visual C++ tools are, probably: 'C:
3765 Microsoft Visual Studio
3770 So you don't have to execute step 'e' :)
3774 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3775 the executables from sdcc
3779 bin, and you can compile using SDCC.
3782 Building SDCC Using Borland
3785 From the sdcc directory, run the command "make -f Makefile.bcc".
3786 This should regenerate all the .exe files in the bin directory except for
3790 If you modify any source files and need to rebuild, be aware that the dependenci
3791 es may not be correctly calculated.
3792 The safest option is to delete all .obj files and run the build again.
3793 From a Cygwin BASH prompt, this can easily be done with the command (be
3794 sure you are in the sdcc directory):
3804 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3806 ) -print -exec rm {}
3815 or on Windows NT/2000/XP from the command prompt with the command:
3822 del /s *.obj *.lib *.rul
3825 from the sdcc directory.
3828 Windows Install Using a ZIP Package
3831 Download the binary zip package from
3832 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3836 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3837 This should unpack to a group of sub-directories.
3838 An example directory structure after unpacking the mingw32 package is:
3843 bin for the executables, c:
3851 lib for the include and libraries.
3854 Adjust your environment variable PATH to include the location of the bin
3855 directory or start sdcc using the full path.
3858 Windows Install Using the Setup Program
3859 \begin_inset LatexCommand \label{sub:Windows-Install}
3866 Download the setup program
3868 sdcc-x.y.z-setup.exe
3870 for an official release from
3873 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3877 or a setup program for one of the snapshots
3879 sdcc-yyyymmdd-xxxx-setup.exe
3882 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3887 A windows typical installer will guide you through the installation process.
3891 \begin_inset LatexCommand \index{VPATH}
3898 SDCC supports the VPATH feature provided by configure and make.
3899 It allows to separate the source and build trees.
3931 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3932 # extract source to directory sdcc
3937 mkdir sdcc.build\SpecialChar ~
3946 # put output in sdcc.build
3956 ../sdcc/configure\SpecialChar ~
3964 # configure is doing all the magic!
3976 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
3977 It automagically computes the variables srcdir, top_srcdir and top_buildir
3983 the generated files will be in ~/sdcc.build, while the source files stay
3986 This is not only usefull for building different binaries, e.g.
3987 when cross compiling.
3988 It also gives you a much better overview in the source tree when all the
3989 generated files are not scattered between the source files.
3990 And the best thing is: if you want to change a file you can leave the original
3991 file untouched in the source directory.
3992 Simply copy it to the build directory, edit it, enter `make clean`, `rm
3993 Makefile.dep` and `make`.
3998 will do the rest for you!
4001 Building the Documentation
4014 -enable-doc to the configure arguments to build the documentation together
4015 with all the other stuff.
4016 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4017 dvips and makeindex) to get the job done.
4018 Another possibility is to change to the doc directory and to type
4022 \begin_inset Quotes srd
4026 \begin_inset Quotes srd
4033 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4036 \begin_inset LatexCommand \url{http://www.lyx.org}
4040 as editor is straightforward.
4041 Prebuilt documentation in html and pdf format is available from
4042 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4049 Reading the Documentation
4052 Currently reading the document in pdf format is recommended, as for unknown
4053 reason the hyperlinks are working there whereas in the html version they
4060 If you should know why please drop us a note
4066 You'll find the pdf version
4067 \begin_inset LatexCommand \index{PDF version of this document}
4072 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4080 \begin_inset LatexCommand \index{HTML version of this document}
4085 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4091 This documentation is in some aspects different from a commercial documentation:
4095 It tries to document SDCC for several processor architectures in one document
4096 (commercially these probably would be separate documents/products).
4098 \begin_inset LatexCommand \index{Status of documentation}
4102 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4104 Z80, PIC14, PIC16 and HC08.
4107 There are many references pointing away from this documentation.
4108 Don't let this distract you.
4110 was a reference like
4111 \begin_inset LatexCommand \url{http://www.opencores.org}
4115 together with a statement
4116 \begin_inset Quotes sld
4119 some processors which are targetted by SDCC can be implemented in a
4136 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4141 \begin_inset Quotes srd
4145 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4150 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4155 \begin_inset Quotes sld
4158 have you ever heard of an open source compiler that compiles a subset of
4160 \begin_inset Quotes srd
4163 we expect you to have a quick look there and come back.
4164 If you read this you are on the right track.
4167 Some sections attribute more space to problems, restrictions and warnings
4168 than to the solution.
4171 The installation section and the section about the debugger is intimidating.
4174 There are still lots of typos and there are more different writing styles
4178 Testing the SDCC Compiler
4179 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4186 The first thing you should do after installing your SDCC compiler is to
4202 \begin_inset LatexCommand \index{version}
4209 at the prompt, and the program should run and output its version like:
4214 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4218 If it doesn't run, or gives a message about not finding sdcc program, then
4219 you need to check over your installation.
4220 Make sure that the sdcc bin directory is in your executable search path
4221 defined by the PATH environment setting (
4226 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4233 Install trouble-shooting for suggestions
4236 Make sure that the sdcc program is in the bin folder, if not perhaps something
4237 did not install correctly.
4245 is commonly installed as described in section
4246 \begin_inset Quotes sld
4249 Install and search paths
4250 \begin_inset Quotes srd
4259 Make sure the compiler works on a very simple example.
4260 Type in the following test.c program using your favorite
4286 Compile this using the following command:
4295 If all goes well, the compiler will generate a test.asm and test.rel file.
4296 Congratulations, you've just compiled your first program with SDCC.
4297 We used the -c option to tell SDCC not to link the generated code, just
4298 to keep things simple for this step.
4306 The next step is to try it with the linker.
4316 If all goes well the compiler will link with the libraries and produce
4317 a test.ihx output file.
4322 (no test.ihx, and the linker generates warnings), then the problem is most
4331 usr/local/share/sdcc/lib directory
4338 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4345 Install trouble-shooting for suggestions).
4353 The final test is to ensure
4361 header files and libraries.
4362 Edit test.c and change it to the following:
4379 strcpy(str1, "testing");
4386 Compile this by typing
4393 This should generate a test.ihx output file, and it should give no warnings
4394 such as not finding the string.h file.
4395 If it cannot find the string.h file, then the problem is that
4399 cannot find the /usr/local/share/sdcc/include directory
4406 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4413 Install trouble-shooting section for suggestions).
4431 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4435 to find exactly where SDCC is looking for the include and lib files.
4438 Install Trouble-shooting
4439 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4444 \begin_inset LatexCommand \index{Install trouble-shooting}
4451 If SDCC does not build correctly
4454 A thing to try is starting from scratch by unpacking the .tgz source package
4455 again in an empty directory.
4463 ./configure 2>&1 | tee configure.log
4477 make 2>&1 | tee make.log
4484 If anything goes wrong, you can review the log files to locate the problem.
4485 Or a relevant part of this can be attached to an email that could be helpful
4486 when requesting help from the mailing list.
4490 \begin_inset Quotes sld
4494 \begin_inset Quotes srd
4501 \begin_inset Quotes sld
4505 \begin_inset Quotes srd
4508 command is a script that analyzes your system and performs some configuration
4509 to ensure the source package compiles on your system.
4510 It will take a few minutes to run, and will compile a few tests to determine
4511 what compiler features are installed.
4515 \begin_inset Quotes sld
4519 \begin_inset Quotes srd
4525 This runs the GNU make tool, which automatically compiles all the source
4526 packages into the final installed binary executables.
4530 \begin_inset Quotes sld
4534 \begin_inset Quotes erd
4540 This will install the compiler, other executables libraries and include
4541 files into the appropriate directories.
4543 \begin_inset LatexCommand \ref{sub:Install-paths}
4549 \begin_inset LatexCommand \ref{sub:Search-Paths}
4554 about install and search paths.
4556 On most systems you will need super-user privileges to do this.
4562 SDCC is not just a compiler, but a collection of tools by various developers.
4563 These include linkers, assemblers, simulators and other components.
4564 Here is a summary of some of the components.
4565 Note that the included simulator and assembler have separate documentation
4566 which you can find in the source package in their respective directories.
4567 As SDCC grows to include support for other processors, other packages from
4568 various developers are included and may have their own sets of documentation.
4572 You might want to look at the files which are installed in <installdir>.
4573 At the time of this writing, we find the following programs for gcc-builds:
4577 In <installdir>/bin:
4580 sdcc - The compiler.
4583 sdcpp - The C preprocessor.
4586 asx8051 - The assembler for 8051 type processors.
4593 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4596 aslink -The linker for 8051 type processors.
4603 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4606 s51 - The ucSim 8051 simulator.
4609 sdcdb - The source debugger.
4612 packihx - A tool to pack (compress) Intel hex files.
4615 In <installdir>/share/sdcc/include
4621 In <installdir>/share/sdcc/lib
4624 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4628 In <installdir>/share/sdcc/doc
4634 As development for other processors proceeds, this list will expand to include
4635 executables to support processors like AVR, PIC, etc.
4641 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4642 the assembler and linkage editor.
4645 sdcpp - The C-Preprocessor
4649 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4653 is a modified version of the GNU preprocessor.
4654 The C preprocessor is used to pull in #include sources, process #ifdef
4655 statements, #defines and so on.
4666 - The Assemblers and Linkage Editors
4669 This is retargettable assembler & linkage editor, it was developed by Alan
4671 John Hartman created the version for 8051, and I (Sandeep) have made some
4672 enhancements and bug fixes for it to work properly with SDCC.
4679 \begin_inset LatexCommand \index{s51}
4683 is a freeware, opensource simulator developed by Daniel Drotos.
4684 The simulator is built as part of the build process.
4685 For more information visit Daniel's web site at:
4686 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4691 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4695 sdcdb - Source Level Debugger
4699 \begin_inset LatexCommand \index{SDCDB (debugger)}
4703 is the companion source level debugger.
4704 More about SDCDB in section
4705 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4710 The current version of the debugger uses Daniel's Simulator S51
4711 \begin_inset LatexCommand \index{s51}
4715 , but can be easily changed to use other simulators.
4724 Single Source File Projects
4727 For single source file 8051 projects the process is very simple.
4728 Compile your programs with the following command
4731 "sdcc sourcefile.c".
4735 This will compile, assemble and link your source file.
4736 Output files are as follows:
4740 \begin_inset LatexCommand \index{<file>.asm}
4745 \begin_inset LatexCommand \index{Assembler source}
4749 file created by the compiler
4753 \begin_inset LatexCommand \index{<file>.lst}
4758 \begin_inset LatexCommand \index{Assembler listing}
4762 file created by the Assembler
4766 \begin_inset LatexCommand \index{<file>.rst}
4771 \begin_inset LatexCommand \index{Assembler listing}
4775 file updated with linkedit information, created by linkage editor
4779 \begin_inset LatexCommand \index{<file>.sym}
4784 \begin_inset LatexCommand \index{Symbol listing}
4788 for the sourcefile, created by the assembler
4792 \begin_inset LatexCommand \index{<file>.rel}
4797 \begin_inset LatexCommand \index{<file>.o}
4802 \begin_inset LatexCommand \index{Object file}
4806 created by the assembler, input to Linkage editor
4810 \begin_inset LatexCommand \index{<file>.map}
4815 \begin_inset LatexCommand \index{Memory map}
4819 for the load module, created by the Linker
4823 \begin_inset LatexCommand \index{<file>.mem}
4827 - A file with a summary of the memory usage
4831 \begin_inset LatexCommand \index{<file>.ihx}
4835 - The load module in Intel hex format
4836 \begin_inset LatexCommand \index{Intel hex format}
4840 (you can select the Motorola S19 format
4841 \begin_inset LatexCommand \index{Motorola S19 format}
4856 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4861 If you need another format you might want to use
4868 \begin_inset LatexCommand \index{objdump (tool)}
4879 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4884 Both formats are documented in the documentation of srecord
4885 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4893 \begin_inset LatexCommand \index{<file>.adb}
4897 - An intermediate file containing debug information needed to create the
4909 \begin_inset LatexCommand \index{-\/-debug}
4917 \begin_inset LatexCommand \index{<file>.cdb}
4921 - An optional file (with -
4931 -debug) containing debug information.
4932 The format is documented in cdbfileformat.pdf
4937 \begin_inset LatexCommand \index{<file> (no extension)}
4941 An optional AOMF or AOMF51
4942 \begin_inset LatexCommand \index{AOMF, AOMF51}
4947 \begin_inset LatexCommand \label{OMF file}
4951 file containing debug information (generated with option -
4978 ormat is commonly used by third party tools (debuggers
4979 \begin_inset LatexCommand \index{Debugger}
4983 , simulators, emulators)
4987 \begin_inset LatexCommand \index{<file>.dump*}
4991 - Dump file to debug the compiler it self (generated with option -
5001 -dumpall) (see section
5002 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5008 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5014 \begin_inset Quotes sld
5017 Anatomy of the compiler
5018 \begin_inset Quotes srd
5024 Projects with Multiple Source Files
5027 SDCC can compile only ONE file at a time.
5028 Let us for example assume that you have a project containing the following
5033 foo1.c (contains some functions)
5035 foo2.c (contains some more functions)
5037 foomain.c (contains more functions and the function main)
5045 The first two files will need to be compiled separately with the commands:
5077 Then compile the source file containing the
5082 \begin_inset LatexCommand \index{Linker}
5086 the files together with the following command:
5094 foomain.c\SpecialChar ~
5095 foo1.rel\SpecialChar ~
5100 \begin_inset LatexCommand \index{<file>.rel}
5112 can be separately compiled as well:
5123 sdcc foomain.rel foo1.rel foo2.rel
5130 The file containing the
5145 file specified in the command line, since the linkage editor processes
5146 file in the order they are presented to it.
5147 The linker is invoked from SDCC using a script file with extension .lnk
5148 \begin_inset LatexCommand \index{<file>.lnk}
5153 You can view this file to troubleshoot linking problems such as those arising
5154 from missing libraries.
5157 Projects with Additional Libraries
5158 \begin_inset LatexCommand \index{Libraries}
5165 Some reusable routines may be compiled into a library, see the documentation
5166 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5170 \begin_inset LatexCommand \index{<file>.lib}
5177 Libraries created in this manner can be included in the command line.
5178 Make sure you include the -L <library-path> option to tell the linker where
5179 to look for these files if they are not in the current directory.
5180 Here is an example, assuming you have the source file
5192 (if that is not the same as your current project):
5199 sdcc foomain.c foolib.lib -L mylib
5210 must be an absolute path name.
5214 The most efficient way to use libraries is to keep separate modules in separate
5216 The lib file now should name all the modules.rel
5217 \begin_inset LatexCommand \index{<file>.rel}
5222 For an example see the standard library file
5226 in the directory <installdir>/share/lib/small.
5229 Using sdcclib to Create and Manage Libraries
5230 \begin_inset LatexCommand \index{sdcclib}
5237 Alternatively, instead of having a .rel file for each entry on the library
5238 file as described in the preceding section, sdcclib can be used to embed
5239 all the modules belonging to such library in the library file itself.
5240 This results in a larger library file, but it greatly reduces the number
5241 of disk files accessed by the linker.
5242 Additionally, the packed library file contains an index of all include
5243 modules and symbols that significantly speeds up the linking process.
5244 To display a list of options supported by sdcclib type:
5253 \begin_inset LatexCommand \index{sdcclib}
5264 To create a new library file, start by compiling all the required modules.
5302 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5304 The next step is to add the .rel files to the library file:
5312 sdcclib libint.lib _divsint.rel
5315 \begin_inset LatexCommand \index{sdcclib}
5325 sdcclib libint.lib _divuint.rel
5331 sdcclib libint.lib _modsint.rel
5337 sdcclib libint.lib _moduint.rel
5343 sdcclib libint.lib _mulint.rel
5350 If the file already exists in the library, it will be replaced.
5351 To see what modules and symbols are included in the library, options -s
5352 and -m are available.
5360 sdcclib -s libint.lib
5363 \begin_inset LatexCommand \index{sdcclib}
5473 If the source files are compiled using -
5484 \begin_inset LatexCommand \index{-\/-debug}
5488 , the corresponding debug information file .adb will be include in the library
5490 The library files created with sdcclib are plain text files, so they can
5491 be viewed with a text editor.
5492 It is not recomended to modify a library file created with sdcclib using
5493 a text editor, as there are file indexes numbers located accross the file
5494 used by the linker to quickly locate the required module to link.
5495 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5496 it can be safely deleted, since all the information required for linking
5497 is embedded in the library file itself.
5498 Library files created using sdcclib are used as described in the preceding
5502 Command Line Options
5503 \begin_inset LatexCommand \index{Command Line Options}
5510 Processor Selection Options
5511 \begin_inset LatexCommand \index{Options processor selection}
5516 \begin_inset LatexCommand \index{Processor selection options}
5522 \labelwidthstring 00.00.0000
5527 \begin_inset LatexCommand \index{-mmcs51}
5533 Generate code for the Intel MCS51
5534 \begin_inset LatexCommand \index{MCS51}
5538 family of processors.
5539 This is the default processor target.
5541 \labelwidthstring 00.00.0000
5546 \begin_inset LatexCommand \index{-mds390}
5552 Generate code for the Dallas DS80C390
5553 \begin_inset LatexCommand \index{DS80C390}
5559 \labelwidthstring 00.00.0000
5564 \begin_inset LatexCommand \index{-mds400}
5570 Generate code for the Dallas DS80C400
5571 \begin_inset LatexCommand \index{DS80C400}
5577 \labelwidthstring 00.00.0000
5582 \begin_inset LatexCommand \index{-mhc08}
5588 Generate code for the Freescale/Motorola HC08
5589 \begin_inset LatexCommand \index{HC08}
5593 family of processors.
5595 \labelwidthstring 00.00.0000
5600 \begin_inset LatexCommand \index{-mz80}
5606 Generate code for the Zilog Z80
5607 \begin_inset LatexCommand \index{Z80}
5611 family of processors.
5613 \labelwidthstring 00.00.0000
5618 \begin_inset LatexCommand \index{-mgbz80}
5624 Generate code for the GameBoy Z80
5625 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5629 processor (Not actively maintained).
5631 \labelwidthstring 00.00.0000
5636 \begin_inset LatexCommand \index{-mavr}
5642 Generate code for the Atmel AVR
5643 \begin_inset LatexCommand \index{AVR}
5647 processor (In development, not complete).
5648 AVR users should probably have a look at winavr
5649 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5654 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5661 I think it is fair to direct users there for now.
5662 Open source is also about avoiding unnecessary work .
5663 But I didn't find the 'official' link.
5665 \labelwidthstring 00.00.0000
5670 \begin_inset LatexCommand \index{-mpic14}
5676 Generate code for the Microchip PIC 14
5677 \begin_inset LatexCommand \index{PIC14}
5681 -bit processors (p16f84 and variants.
5682 In development, not complete).
5685 p16f627 p16f628 p16f84 p16f873 p16f877?
5687 \labelwidthstring 00.00.0000
5692 \begin_inset LatexCommand \index{-mpic16}
5698 Generate code for the Microchip PIC 16
5699 \begin_inset LatexCommand \index{PIC16}
5703 -bit processors (p18f452 and variants.
5704 In development, not complete).
5706 \labelwidthstring 00.00.0000
5712 Generate code for the Toshiba TLCS-900H
5713 \begin_inset LatexCommand \index{TLCS-900H}
5717 processor (Not maintained, not complete).
5719 \labelwidthstring 00.00.0000
5724 \begin_inset LatexCommand \index{-mxa51}
5730 Generate code for the Phillips XA51
5731 \begin_inset LatexCommand \index{XA51}
5735 processor (Not maintained, not complete).
5738 Preprocessor Options
5739 \begin_inset LatexCommand \index{Options preprocessor}
5744 \begin_inset LatexCommand \index{Preprocessor options}
5749 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5755 \labelwidthstring 00.00.0000
5760 \begin_inset LatexCommand \index{-I<path>}
5766 The additional location where the pre processor will look for <..h> or
5767 \begin_inset Quotes eld
5771 \begin_inset Quotes erd
5776 \labelwidthstring 00.00.0000
5781 \begin_inset LatexCommand \index{-D<macro[=value]>}
5787 Command line definition of macros.
5788 Passed to the preprocessor.
5790 \labelwidthstring 00.00.0000
5795 \begin_inset LatexCommand \index{-M}
5801 Tell the preprocessor to output a rule suitable for make describing the
5802 dependencies of each object file.
5803 For each source file, the preprocessor outputs one make-rule whose target
5804 is the object file name for that source file and whose dependencies are
5805 all the files `#include'd in it.
5806 This rule may be a single line or may be continued with `
5808 '-newline if it is long.
5809 The list of rules is printed on standard output instead of the preprocessed
5812 \begin_inset LatexCommand \index{-E}
5818 \labelwidthstring 00.00.0000
5823 \begin_inset LatexCommand \index{-C}
5829 Tell the preprocessor not to discard comments.
5830 Used with the `-E' option.
5832 \labelwidthstring 00.00.0000
5837 \begin_inset LatexCommand \index{-MM}
5848 Like `-M' but the output mentions only the user header files included with
5850 \begin_inset Quotes eld
5854 System header files included with `#include <file>' are omitted.
5856 \labelwidthstring 00.00.0000
5861 \begin_inset LatexCommand \index{-Aquestion(answer)}
5867 Assert the answer answer for question, in case it is tested with a preprocessor
5868 conditional such as `#if #question(answer)'.
5869 `-A-' disables the standard assertions that normally describe the target
5872 \labelwidthstring 00.00.0000
5877 \begin_inset LatexCommand \index{-Umacro}
5883 Undefine macro macro.
5884 `-U' options are evaluated after all `-D' options, but before any `-include'
5885 and `-imacros' options.
5887 \labelwidthstring 00.00.0000
5892 \begin_inset LatexCommand \index{-dM}
5898 Tell the preprocessor to output only a list of the macro definitions that
5899 are in effect at the end of preprocessing.
5900 Used with the `-E' option.
5902 \labelwidthstring 00.00.0000
5907 \begin_inset LatexCommand \index{-dD}
5913 Tell the preprocessor to pass all macro definitions into the output, in
5914 their proper sequence in the rest of the output.
5916 \labelwidthstring 00.00.0000
5921 \begin_inset LatexCommand \index{-dN}
5932 Like `-dD' except that the macro arguments and contents are omitted.
5933 Only `#define name' is included in the output.
5935 \labelwidthstring 00.00.0000
5940 preprocessorOption[,preprocessorOption]
5943 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5948 Pass the preprocessorOption to the preprocessor
5953 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5958 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5959 Collection (gcc), if you need more dedicated options please refer to the
5961 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5969 \begin_inset LatexCommand \index{Options linker}
5974 \begin_inset LatexCommand \index{Linker options}
5980 \labelwidthstring 00.00.0000
6000 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6005 \begin_inset LatexCommand \index{-L -\/-lib-path}
6012 <absolute path to additional libraries> This option is passed to the linkage
6013 editor's additional libraries
6014 \begin_inset LatexCommand \index{Libraries}
6019 The path name must be absolute.
6020 Additional library files may be specified in the command line.
6021 See section Compiling programs for more details.
6023 \labelwidthstring 00.00.0000
6040 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6045 <Value> The start location of the external ram
6046 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6050 , default value is 0.
6051 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6061 -xram-loc 0x8000 or -
6073 \labelwidthstring 00.00.0000
6090 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6095 <Value> The start location of the code
6096 \begin_inset LatexCommand \index{code}
6100 segment, default value 0.
6101 Note when this option is used the interrupt vector table is also relocated
6102 to the given address.
6103 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6113 -code-loc 0x8000 or -
6125 \labelwidthstring 00.00.0000
6142 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6147 <Value> By default the stack
6148 \begin_inset LatexCommand \index{stack}
6152 is placed after the data segment.
6153 Using this option the stack can be placed anywhere in the internal memory
6155 The value entered can be in Hexadecimal or Decimal format, e.g.
6166 -stack-loc 0x20 or -
6177 Since the sp register is incremented before a push or call, the initial
6178 sp will be set to one byte prior the provided value.
6179 The provided value should not overlap any other memory areas such as used
6180 register banks or the data segment and with enough space for the current
6198 \begin_inset LatexCommand \index{-\/-pack-iram}
6202 option (which is now a default setting) will override this setting, so
6203 you should also specify the
6219 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6223 option if you need to manually place the stack.
6225 \labelwidthstring 00.00.0000
6242 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6247 <Value> By default the external stack
6248 \begin_inset LatexCommand \index{xstack}
6252 is placed after the pdata segment.
6253 Using this option the xstack can be placed anywhere in the external memory
6255 The value entered can be in Hexadecimal or Decimal format, e.g.
6266 -xstack-loc 0x8000 or -
6277 The provided value should not overlap any other memory areas such as the
6278 pdata or xdata segment and with enough space for the current application.
6280 \labelwidthstring 00.00.0000
6297 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6302 <Value> The start location of the internal ram data
6303 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6308 The value entered can be in Hexadecimal or Decimal format, eg.
6330 (By default, the start location of the internal ram data segment is set
6331 as low as possible in memory, taking into account the used register banks
6332 and the bit segment at address 0x20.
6333 For example if register banks 0 and 1 are used without bit variables, the
6334 data segment will be set, if -
6344 -data-loc is not used, to location 0x10.)
6346 \labelwidthstring 00.00.0000
6363 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6368 <Value> The start location of the indirectly addressable internal ram
6369 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6373 of the 8051, default value is 0x80.
6374 The value entered can be in Hexadecimal or Decimal format, eg.
6385 -idata-loc 0x88 or -
6397 \labelwidthstring 00.00.0000
6414 <Value> The start location of the bit
6415 \begin_inset LatexCommand \index{bit}
6419 addressable internal ram of the 8051.
6425 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6428 \labelwidthstring 00.00.0000
6443 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6452 The linker output (final object code) is in Intel Hex format.
6453 \begin_inset LatexCommand \index{Intel hex format}
6457 This is the default option.
6458 The format itself is documented in the documentation of srecord
6459 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6465 \labelwidthstring 00.00.0000
6480 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6489 The linker output (final object code) is in Motorola S19 format
6490 \begin_inset LatexCommand \index{Motorola S19 format}
6495 The format itself is documented in the documentation of srecord.
6497 \labelwidthstring 00.00.0000
6512 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6521 The linker output (final object code) is in ELF format
6522 \begin_inset LatexCommand \index{ELF format}
6527 (Currently only supported for the HC08 processors)
6529 \labelwidthstring 00.00.0000
6534 linkOption[,linkOption]
6537 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6542 Pass the linkOption to the linker.
6543 See file sdcc/as/doc/asxhtm.html for more on linker options.
6547 \begin_inset LatexCommand \index{Options MCS51}
6552 \begin_inset LatexCommand \index{MCS51 options}
6558 \labelwidthstring 00.00.0000
6573 \begin_inset LatexCommand \index{-\/-model-small}
6584 Generate code for Small Model programs, see section Memory Models for more
6586 This is the default model.
6588 \labelwidthstring 00.00.0000
6603 \begin_inset LatexCommand \index{-\/-model-medium}
6609 Generate code for Medium model programs, see section Memory Models for
6611 If this option is used all source files in the project have to be compiled
6613 It must also be used when invoking the linker.
6615 \labelwidthstring 00.00.0000
6630 \begin_inset LatexCommand \index{-\/-model-large}
6636 Generate code for Large model programs, see section Memory Models for more
6638 If this option is used all source files in the project have to be compiled
6640 It must also be used when invoking the linker.
6642 \labelwidthstring 00.00.0000
6657 \begin_inset LatexCommand \index{-\/-xstack}
6663 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6664 variables and passing parameters.
6666 \begin_inset LatexCommand \ref{sub:External-Stack}
6671 External Stack for more details.
6673 \labelwidthstring 00.00.0000
6691 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6695 Causes the linker to check if the internal ram usage is within limits of
6698 \labelwidthstring 00.00.0000
6716 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6720 Causes the linker to check if the external ram usage is within limits of
6723 \labelwidthstring 00.00.0000
6741 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6745 Causes the linker to check if the code memory usage is within limits of
6748 \labelwidthstring 00.00.0000
6766 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6770 Causes the linker to check if there is at minimum <Value> bytes for stack.
6772 \labelwidthstring 00.00.0000
6790 \begin_inset LatexCommand \index{-\/-pack-iram}
6794 Causes the linker to use unused register banks for data variables and pack
6795 data, idata and stack together.
6796 This is the default now.
6798 \labelwidthstring 00.00.0000
6816 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6820 Causes the linker to use old style for allocating memory areas.
6823 DS390 / DS400 Options
6824 \begin_inset LatexCommand \index{Options DS390}
6829 \begin_inset LatexCommand \index{DS390 options}
6835 \labelwidthstring 00.00.0000
6852 \begin_inset LatexCommand \index{-\/-model-flat24}
6862 Generate 24-bit flat mode code.
6863 This is the one and only that the ds390 code generator supports right now
6864 and is default when using
6869 See section Memory Models for more details.
6871 \labelwidthstring 00.00.0000
6886 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6892 disable interrupts during ESP:SP updates.
6894 \labelwidthstring 00.00.0000
6911 \begin_inset LatexCommand \index{-\/-stack-10bit}
6915 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6916 This is the one and only that the ds390 code generator supports right now
6917 and is default when using
6922 In this mode, the stack is located in the lower 1K of the internal RAM,
6923 which is mapped to 0x400000.
6924 Note that the support is incomplete, since it still uses a single byte
6925 as the stack pointer.
6926 This means that only the lower 256 bytes of the potential 1K stack space
6927 will actually be used.
6928 However, this does allow you to reclaim the precious 256 bytes of low RAM
6929 for use for the DATA and IDATA segments.
6930 The compiler will not generate any code to put the processor into 10 bit
6932 It is important to ensure that the processor is in this mode before calling
6933 any re-entrant functions compiled with this option.
6934 In principle, this should work with the
6947 \begin_inset LatexCommand \index{-\/-stack-auto}
6953 option, but that has not been tested.
6954 It is incompatible with the
6967 \begin_inset LatexCommand \index{-\/-xstack}
6974 It also only makes sense if the processor is in 24 bit contiguous addressing
6987 -model-flat24 option
6991 \labelwidthstring 00.00.0000
7006 \begin_inset LatexCommand \index{-\/-stack-probe}
7012 insert call to function __stack_probe at each function prologue.
7014 \labelwidthstring 00.00.0000
7029 \begin_inset LatexCommand \index{-\/-tini-libid}
7035 <nnnn> LibraryID used in -mTININative.
7038 \labelwidthstring 00.00.0000
7053 \begin_inset LatexCommand \index{-\/-use-accelerator}
7059 generate code for DS390 Arithmetic Accelerator.
7064 \begin_inset LatexCommand \index{Options Z80}
7069 \begin_inset LatexCommand \index{Z80 options}
7075 \labelwidthstring 00.00.0000
7092 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
7102 Force a called function to always save BC.
7104 \labelwidthstring 00.00.0000
7121 \begin_inset LatexCommand \index{-\/-no-std-crt0}
7125 When linking, skip the standard crt0.o object file.
7126 You must provide your own crt0.o for your system when linking.
7130 Optimization Options
7131 \begin_inset LatexCommand \index{Options optimization}
7136 \begin_inset LatexCommand \index{Optimization options}
7142 \labelwidthstring 00.00.0000
7157 \begin_inset LatexCommand \index{-\/-nogcse}
7163 Will not do global subexpression elimination, this option may be used when
7164 the compiler creates undesirably large stack/data spaces to store compiler
7174 \begin_inset LatexCommand \index{sloc (spill location)}
7179 A warning message will be generated when this happens and the compiler
7180 will indicate the number of extra bytes it allocated.
7181 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7183 \begin_inset LatexCommand \index{\#pragma nogcse}
7187 can be used to turn off global subexpression elimination
7188 \begin_inset LatexCommand \index{Subexpression elimination}
7192 for a given function only.
7194 \labelwidthstring 00.00.0000
7209 \begin_inset LatexCommand \index{-\/-noinvariant}
7215 Will not do loop invariant optimizations, this may be turned off for reasons
7216 explained for the previous option.
7217 For more details of loop optimizations performed see Loop Invariants in
7219 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7224 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7226 \begin_inset LatexCommand \index{\#pragma noinvariant}
7230 can be used to turn off invariant optimizations for a given function only.
7232 \labelwidthstring 00.00.0000
7247 \begin_inset LatexCommand \index{-\/-noinduction}
7253 Will not do loop induction optimizations, see section strength reduction
7255 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7257 \begin_inset LatexCommand \index{\#pragma noinduction}
7261 can be used to turn off induction optimizations for a given function only.
7263 \labelwidthstring 00.00.0000
7278 \begin_inset LatexCommand \index{-\/-nojtbound}
7289 Will not generate boundary condition check when switch statements
7290 \begin_inset LatexCommand \index{switch statement}
7294 are implemented using jump-tables.
7296 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7301 Switch Statements for more details.
7302 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7304 \begin_inset LatexCommand \index{\#pragma nojtbound}
7308 can be used to turn off boundary checking for jump tables for a given function
7311 \labelwidthstring 00.00.0000
7326 \begin_inset LatexCommand \index{-\/-noloopreverse}
7335 Will not do loop reversal
7336 \begin_inset LatexCommand \index{Loop reversing}
7342 \labelwidthstring 00.00.0000
7359 \begin_inset LatexCommand \index{-\/-nolabelopt }
7363 Will not optimize labels (makes the dumpfiles more readable).
7365 \labelwidthstring 00.00.0000
7380 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7386 Will not memcpy initialized data from code space into xdata space.
7387 This saves a few bytes in code space if you don't have initialized data
7388 \begin_inset LatexCommand \index{Variable initialization}
7394 \labelwidthstring 00.00.0000
7409 \begin_inset LatexCommand \index{-\/-nooverlay}
7415 The compiler will not overlay parameters and local variables of any function,
7416 see section Parameters and local variables for more details.
7418 \labelwidthstring 00.00.0000
7433 \begin_inset LatexCommand \index{-\/-no-peep}
7439 Disable peep-hole optimization with built-in rules.
7441 \labelwidthstring 00.00.0000
7458 \begin_inset LatexCommand \index{-\/-peep-file}
7463 <filename> This option can be used to use additional rules to be used by
7464 the peep hole optimizer.
7466 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7471 Peep Hole optimizations for details on how to write these rules.
7473 \labelwidthstring 00.00.0000
7488 \begin_inset LatexCommand \index{-\/-peep-asm}
7494 Pass the inline assembler code through the peep hole optimizer.
7495 This can cause unexpected changes to inline assembler code, please go through
7496 the peephole optimizer
7497 \begin_inset LatexCommand \index{Peephole optimizer}
7501 rules defined in the source file tree '<target>/peeph.def' before using
7504 \labelwidthstring 00.00.0000
7519 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7525 The compiler will optimize code generation towards fast code, possibly
7526 at the expense of code size.
7528 \labelwidthstring 00.00.0000
7543 \begin_inset LatexCommand \index{-\/-opt-code-size}
7549 The compiler will optimize code generation towards compact code, possibly
7550 at the expense of code speed.
7554 \begin_inset LatexCommand \index{Options other}
7560 \labelwidthstring 00.00.0000
7576 \begin_inset LatexCommand \index{-\/-compile-only}
7581 \begin_inset LatexCommand \index{-c -\/-compile-only}
7587 will compile and assemble the source, but will not call the linkage editor.
7589 \labelwidthstring 00.00.0000
7608 \begin_inset LatexCommand \index{-\/-c1mode}
7614 reads the preprocessed source from standard input and compiles it.
7615 The file name for the assembler output must be specified using the -o option.
7617 \labelwidthstring 00.00.0000
7622 \begin_inset LatexCommand \index{-E}
7628 Run only the C preprocessor.
7629 Preprocess all the C source files specified and output the results to standard
7632 \labelwidthstring 00.00.0000
7638 \begin_inset LatexCommand \index{-o <path/file>}
7644 The output path resp.
7645 file where everything will be placed.
7646 If the parameter is a path, it must have a trailing slash (or backslash
7647 for the Windows binaries) to be recognized as a path.
7650 \labelwidthstring 00.00.0000
7665 \begin_inset LatexCommand \index{-\/-stack-auto}
7676 All functions in the source file will be compiled as
7681 \begin_inset LatexCommand \index{reentrant}
7686 the parameters and local variables will be allocated on the stack
7687 \begin_inset LatexCommand \index{stack}
7693 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7697 Parameters and Local Variables for more details.
7698 If this option is used all source files in the project should be compiled
7700 It automatically implies --int-long-reent and --float-reent.
7703 \labelwidthstring 00.00.0000
7718 \begin_inset LatexCommand \index{-\/-callee-saves}
7722 function1[,function2][,function3]....
7725 The compiler by default uses a caller saves convention for register saving
7726 across function calls, however this can cause unnecessary register pushing
7727 & popping when calling small functions from larger functions.
7728 This option can be used to switch the register saving convention for the
7729 function names specified.
7730 The compiler will not save registers when calling these functions, no extra
7731 code will be generated at the entry & exit (function prologue
7734 \begin_inset LatexCommand \index{function prologue}
7743 \begin_inset LatexCommand \index{function epilogue}
7749 ) for these functions to save & restore the registers used by these functions,
7750 this can SUBSTANTIALLY reduce code & improve run time performance of the
7752 In the future the compiler (with inter procedural analysis) will be able
7753 to determine the appropriate scheme to use for each function call.
7754 DO NOT use this option for built-in functions such as _mulint..., if this
7755 option is used for a library function the appropriate library function
7756 needs to be recompiled with the same option.
7757 If the project consists of multiple source files then all the source file
7758 should be compiled with the same -
7768 -callee-saves option string.
7769 Also see #pragma\SpecialChar ~
7771 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7777 \labelwidthstring 00.00.0000
7792 \begin_inset LatexCommand \index{-\/-debug}
7801 When this option is used the compiler will generate debug information.
7802 The debug information collected in a file with .cdb extension can be used
7804 For more information see documentation for SDCDB.
7805 Another file with no extension contains debug information in AOMF or AOMF51
7806 \begin_inset LatexCommand \index{AOMF, AOMF51}
7810 format which is commonly used by third party tools.
7812 \labelwidthstring 00.00.0000
7817 \begin_inset LatexCommand \index{-S}
7828 Stop after the stage of compilation proper; do not assemble.
7829 The output is an assembler code file for the input file specified.
7831 \labelwidthstring 00.00.0000
7846 \begin_inset LatexCommand \index{-\/-int-long-reent}
7852 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7853 Note by default these libraries are compiled as non-reentrant.
7854 See section Installation for more details.
7856 \labelwidthstring 00.00.0000
7871 \begin_inset LatexCommand \index{-\/-cyclomatic}
7880 This option will cause the compiler to generate an information message for
7881 each function in the source file.
7882 The message contains some
7886 information about the function.
7887 The number of edges and nodes the compiler detected in the control flow
7888 graph of the function, and most importantly the
7890 cyclomatic complexity
7891 \begin_inset LatexCommand \index{Cyclomatic complexity}
7897 see section on Cyclomatic Complexity for more details.
7899 \labelwidthstring 00.00.0000
7914 \begin_inset LatexCommand \index{-\/-float-reent}
7920 Floating point library is compiled as reentrant
7921 \begin_inset LatexCommand \index{reentrant}
7926 See section Installation for more details.
7928 \labelwidthstring 00.00.0000
7943 \begin_inset LatexCommand \index{-\/-main-return}
7949 This option can be used if the code generated is called by a monitor program
7950 or if the main routine includes an endless loop.
7951 This option might result in slightly smaller code and save two bytes of
7953 The return from the 'main'
7954 \begin_inset LatexCommand \index{main return}
7958 function will return to the function calling main.
7959 The default setting is to lock up i.e.
7966 \labelwidthstring 00.00.0000
7981 \begin_inset LatexCommand \index{-\/-nostdinc}
7987 This will prevent the compiler from passing on the default include path
7988 to the preprocessor.
7990 \labelwidthstring 00.00.0000
8005 \begin_inset LatexCommand \index{-\/-nostdlib}
8011 This will prevent the compiler from passing on the default library
8012 \begin_inset LatexCommand \index{Libraries}
8018 \labelwidthstring 00.00.0000
8033 \begin_inset LatexCommand \index{-\/-verbose}
8039 Shows the various actions the compiler is performing.
8041 \labelwidthstring 00.00.0000
8046 \begin_inset LatexCommand \index{-V}
8052 Shows the actual commands the compiler is executing.
8054 \labelwidthstring 00.00.0000
8069 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8075 Hides your ugly and inefficient c-code from the asm file, so you can always
8076 blame the compiler :)
8078 \labelwidthstring 00.00.0000
8093 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8099 Will not include peep-hole comments in the generated files.
8101 \labelwidthstring 00.00.0000
8116 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8122 Include i-codes in the asm file.
8123 Sounds like noise but is most helpful for debugging the compiler itself.
8125 \labelwidthstring 00.00.0000
8140 \begin_inset LatexCommand \index{-\/-less-pedantic}
8146 Disable some of the more pedantic warnings
8147 \begin_inset LatexCommand \index{Warnings}
8151 (jwk burps: please be more specific here, please!).
8153 \labelwidthstring 00.00.0000
8167 -disable-warning\SpecialChar ~
8169 \begin_inset LatexCommand \index{-\/-disable-warning}
8175 Disable specific warning with number <nnnn>.
8177 \labelwidthstring 00.00.0000
8192 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8198 Display the directories in the compiler's search path
8200 \labelwidthstring 00.00.0000
8215 \begin_inset LatexCommand \index{-\/-vc}
8221 Display errors and warnings using MSVC style, so you can use SDCC with
8224 \labelwidthstring 00.00.0000
8239 \begin_inset LatexCommand \index{-\/-use-stdout}
8245 Send errors and warnings to stdout instead of stderr.
8247 \labelwidthstring 00.00.0000
8252 asmOption[,asmOption]
8255 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8260 Pass the asmOption to the assembler
8261 \begin_inset LatexCommand \index{Options assembler}
8266 \begin_inset LatexCommand \index{Assembler options}
8271 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8273 \labelwidthstring 00.00.0000
8288 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8294 Generally follow the C89 standard, but allow SDCC features that conflict
8295 with the standard (default).
8297 \labelwidthstring 00.00.0000
8312 \begin_inset LatexCommand \index{-\/-std-c89}
8318 Follow the C89 standard and disable SDCC features that conflict with the
8321 \labelwidthstring 00.00.0000
8336 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8342 Generally follow the C99 standard, but allow SDCC features that conflict
8343 with the standard (incomplete support).
8345 \labelwidthstring 00.00.0000
8360 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8366 Follow the C99 standard and disable SDCC features that conflict with the
8367 standard (incomplete support).
8369 \labelwidthstring 00.00.0000
8386 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8391 <Name> The name to be used for the code
8392 \begin_inset LatexCommand \index{code}
8396 segment, default CSEG.
8397 This is useful if you need to tell the compiler to put the code in a special
8398 segment so you can later on tell the linker to put this segment in a special
8400 Can be used for instance when using bank switching to put the code in a
8403 \labelwidthstring 00.00.0000
8420 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8425 <Name> The name to be used for the const
8426 \begin_inset LatexCommand \index{code}
8430 segment, default CONST.
8431 This is useful if you need to tell the compiler to put the const data in
8432 a special segment so you can later on tell the linker to put this segment
8433 in a special place in memory.
8434 Can be used for instance when using bank switching to put the const data
8437 \labelwidthstring 00.00.0000
8449 a SDCC compiler option but if you want
8453 warnings you can use a separate tool dedicated to syntax checking like
8455 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8460 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8465 \begin_inset LatexCommand \url{http://www.splint.org}
8470 To make your source files parseable by splint you will have to include
8476 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8480 in your source file and add brackets around extended keywords (like
8483 \begin_inset Quotes sld
8496 \begin_inset Quotes srd
8504 \begin_inset Quotes sld
8507 __interrupt\SpecialChar ~
8509 \begin_inset Quotes srd
8517 Splint has an excellent on line manual at
8518 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8522 and it's capabilities go beyond pure syntax checking.
8523 You'll need to tell splint the location of SDCC's include files so a typical
8524 command line could look like this:
8528 splint\SpecialChar ~
8530 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8535 Intermediate Dump Options
8536 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8541 \begin_inset LatexCommand \index{Options intermediate dump}
8546 \begin_inset LatexCommand \index{Intermediate dump options}
8553 The following options are provided for the purpose of retargetting and debugging
8555 They provide a means to dump the intermediate code (iCode
8556 \begin_inset LatexCommand \index{iCode}
8560 ) generated by the compiler in human readable form at various stages of
8561 the compilation process.
8562 More on iCodes see chapter
8563 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8568 \begin_inset Quotes srd
8571 The anatomy of the compiler
8572 \begin_inset Quotes srd
8577 \labelwidthstring 00.00.0000
8592 \begin_inset LatexCommand \index{-\/-dumpraw}
8598 This option will cause the compiler to dump the intermediate code into
8601 <source filename>.dumpraw
8603 just after the intermediate code has been generated for a function, i.e.
8604 before any optimizations are done.
8606 \begin_inset LatexCommand \index{Basic blocks}
8610 at this stage ordered in the depth first number, so they may not be in
8611 sequence of execution.
8613 \labelwidthstring 00.00.0000
8628 \begin_inset LatexCommand \index{-\/-dumpgcse}
8634 Will create a dump of iCode's, after global subexpression elimination
8635 \begin_inset LatexCommand \index{Global subexpression elimination}
8641 <source filename>.dumpgcse.
8643 \labelwidthstring 00.00.0000
8658 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8664 Will create a dump of iCode's, after deadcode elimination
8665 \begin_inset LatexCommand \index{Dead-code elimination}
8671 <source filename>.dumpdeadcode.
8673 \labelwidthstring 00.00.0000
8688 \begin_inset LatexCommand \index{-\/-dumploop}
8697 Will create a dump of iCode's, after loop optimizations
8698 \begin_inset LatexCommand \index{Loop optimization}
8704 <source filename>.dumploop.
8706 \labelwidthstring 00.00.0000
8721 \begin_inset LatexCommand \index{-\/-dumprange}
8730 Will create a dump of iCode's, after live range analysis
8731 \begin_inset LatexCommand \index{Live range analysis}
8737 <source filename>.dumprange.
8739 \labelwidthstring 00.00.0000
8754 \begin_inset LatexCommand \index{-\/-dumlrange}
8760 Will dump the life ranges
8761 \begin_inset LatexCommand \index{Live range analysis}
8767 \labelwidthstring 00.00.0000
8782 \begin_inset LatexCommand \index{-\/-dumpregassign}
8791 Will create a dump of iCode's, after register assignment
8792 \begin_inset LatexCommand \index{Register assignment}
8798 <source filename>.dumprassgn.
8800 \labelwidthstring 00.00.0000
8815 \begin_inset LatexCommand \index{-\/-dumplrange}
8821 Will create a dump of the live ranges of iTemp's
8823 \labelwidthstring 00.00.0000
8838 \begin_inset LatexCommand \index{-\/-dumpall}
8849 Will cause all the above mentioned dumps to be created.
8852 Redirecting output on Windows Shells
8855 By default SDCC writes it's error messages to
8856 \begin_inset Quotes sld
8860 \begin_inset Quotes srd
8864 To force all messages to
8865 \begin_inset Quotes sld
8869 \begin_inset Quotes srd
8893 \begin_inset LatexCommand \index{-\/-use-stdout}
8898 Additionally, if you happen to have visual studio installed in your windows
8899 machine, you can use it to compile your sources using a custom build and
8915 \begin_inset LatexCommand \index{-\/-vc}
8920 Something like this should work:
8964 -model-large -c $(InputPath)
8967 Environment variables
8968 \begin_inset LatexCommand \index{Environment variables}
8975 SDCC recognizes the following environment variables:
8977 \labelwidthstring 00.00.0000
8982 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8988 SDCC installs a signal handler
8989 \begin_inset LatexCommand \index{signal handler}
8993 to be able to delete temporary files after an user break (^C) or an exception.
8994 If this environment variable is set, SDCC won't install the signal handler
8995 in order to be able to debug SDCC.
8997 \labelwidthstring 00.00.0000
9004 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9010 Path, where temporary files will be created.
9011 The order of the variables is the search order.
9012 In a standard *nix environment these variables are not set, and there's
9013 no need to set them.
9014 On Windows it's recommended to set one of them.
9016 \labelwidthstring 00.00.0000
9021 \begin_inset LatexCommand \index{SDCC\_HOME}
9028 \begin_inset LatexCommand \ref{sub:Install-paths}
9034 \begin_inset Quotes sld
9038 \begin_inset Quotes srd
9043 \labelwidthstring 00.00.0000
9048 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9055 \begin_inset LatexCommand \ref{sub:Search-Paths}
9061 \begin_inset Quotes sld
9065 \begin_inset Quotes srd
9070 \labelwidthstring 00.00.0000
9075 \begin_inset LatexCommand \index{SDCC\_LIB}
9082 \begin_inset LatexCommand \ref{sub:Search-Paths}
9088 \begin_inset Quotes sld
9092 \begin_inset Quotes srd
9098 There are some more environment variables recognized by SDCC, but these
9099 are solely used for debugging purposes.
9100 They can change or disappear very quickly, and will never be documented.
9103 Storage Class Language Extensions
9106 MCS51/DS390 Storage Class
9107 \begin_inset LatexCommand \index{Storage class}
9114 In addition to the ANSI storage classes SDCC allows the following MCS51
9115 specific storage classes:
9116 \layout Subsubsection
9119 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9124 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9129 \begin_inset LatexCommand \index{near (storage class)}
9134 \begin_inset LatexCommand \index{\_\_near (storage class)}
9145 storage class for the Small Memory model (
9153 or the more ANSI-C compliant forms
9161 can be used synonymously).
9162 Variables declared with this storage class will be allocated in the directly
9163 addressable portion of the internal RAM of a 8051, e.g.:
9168 __data unsigned char test_data;
9171 Writing 0x01 to this variable generates the assembly code:
9176 75*00 01\SpecialChar ~
9182 \layout Subsubsection
9185 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9190 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9195 \begin_inset LatexCommand \index{far (storage class)}
9200 \begin_inset LatexCommand \index{\_\_far (storage class)}
9207 Variables declared with this storage class will be placed in the external
9213 storage class for the Large Memory model, e.g.:
9218 __xdata unsigned char test_xdata;
9221 Writing 0x01 to this variable generates the assembly code:
9226 90s00r00\SpecialChar ~
9255 \layout Subsubsection
9258 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9263 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9270 Variables declared with this storage class will be allocated into the indirectly
9271 addressable portion of the internal ram of a 8051, e.g.:
9276 __idata unsigned char test_idata;
9279 Writing 0x01 to this variable generates the assembly code:
9308 Please note, the first 128 byte of idata physically access the same RAM
9310 The original 8051 had 128 byte idata memory, nowadays most devices have
9311 256 byte idata memory.
9313 \begin_inset LatexCommand \index{stack}
9317 is located in idata memory.
9318 \layout Subsubsection
9321 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9326 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9333 Paged xdata access is just as straightforward as using the other addressing
9335 It is typically located at the start of xdata and has a maximum size of
9337 The following example writes 0x01 to the pdata variable.
9338 Please note, pdata access physically accesses xdata memory.
9339 The high byte of the address is determined by port P2
9340 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9344 (or in case of some 8051 variants by a separate Special Function Register,
9346 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9355 storage class for the Medium Memory model, e.g.:
9360 __pdata unsigned char test_pdata;
9363 Writing 0x01 to this variable generates the assembly code:
9407 \begin_inset LatexCommand \index{-\/-xstack}
9411 option is used the pdata memory area is followed by the xstack memory area
9412 and the sum of their sizes is limited to 256 bytes.
9413 \layout Subsubsection
9416 \begin_inset LatexCommand \index{code}
9421 \begin_inset LatexCommand \index{\_\_code}
9428 'Variables' declared with this storage class will be placed in the code
9434 __code unsigned char test_code;
9437 Read access to this variable generates the assembly code:
9442 90s00r6F\SpecialChar ~
9445 mov dptr,#_test_code
9474 indexed arrays of characters in code memory can be accessed efficiently:
9479 __code char test_array[] = {'c','h','e','a','p'};
9482 Read access to this array using an 8-bit unsigned index generates the assembly
9499 90s00r41\SpecialChar ~
9502 mov dptr,#_test_array
9517 \layout Subsubsection
9520 \begin_inset LatexCommand \index{bit}
9525 \begin_inset LatexCommand \index{\_\_bit}
9532 This is a data-type and a storage class specifier.
9533 When a variable is declared as a bit, it is allocated into the bit addressable
9534 memory of 8051, e.g.:
9542 Writing 1 to this variable generates the assembly code:
9558 The bit addressable memory consists of 128 bits which are located from 0x20
9559 to 0x2f in data memory.
9562 Apart from this 8051 specific storage class most architectures support ANSI-C
9564 \begin_inset LatexCommand \index{bitfields}
9574 Not really meant as examples, but nevertheless showing what bitfields are
9575 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9579 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9580 signed modifier are implemented as unsigned.
9581 \layout Subsubsection
9584 \begin_inset LatexCommand \index{sfr}
9589 \begin_inset LatexCommand \index{\_\_sfr}
9594 \begin_inset LatexCommand \index{sfr16}
9599 \begin_inset LatexCommand \index{\_\_sfr16}
9604 \begin_inset LatexCommand \index{sfr32}
9609 \begin_inset LatexCommand \index{\_\_sfr32}
9614 \begin_inset LatexCommand \index{\_\_sbit}
9621 Like the bit keyword,
9623 sfr / sfr16 / sfr32 / sbit
9625 signify both a data-type and storage class, they are used to describe the
9646 variables of a 8051, eg:
9652 \begin_inset LatexCommand \index{at}
9657 \begin_inset LatexCommand \index{\_\_at}
9661 (0x80) P0;\SpecialChar ~
9662 /* special function register P0 at location 0x80 */
9664 /* 16 bit special function register combination for timer 0 */
9666 /* with the high byte at location 0x8C and the low byte at location 0x8A
9670 \begin_inset LatexCommand \index{at}
9675 \begin_inset LatexCommand \index{\_\_at}
9681 __sbit __at (0xd7) CY; /* CY (Carry Flag
9682 \begin_inset LatexCommand \index{Flags}
9687 \begin_inset LatexCommand \index{Carry flag}
9694 Special function registers which are located on an address dividable by
9695 8 are bit-addressable, an
9699 addresses a specific bit within these sfr.
9701 16 Bit and 32 bit special function register combinations which require a
9702 certain access order are better not declared using
9711 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9712 this is not guaranteed.
9713 \layout Subsubsection
9716 \begin_inset LatexCommand \index{Pointer}
9720 to MCS51/DS390 specific memory spaces
9723 SDCC allows (via language extensions) pointers to explicitly point to any
9724 of the memory spaces
9725 \begin_inset LatexCommand \index{Memory model}
9730 In addition to the explicit pointers, the compiler uses (by default) generic
9731 pointers which can be used to point to any of the memory spaces.
9735 Pointer declaration examples:
9740 /* pointer physically in internal ram pointing to object in external ram
9743 __xdata unsigned char * __data p;
9747 /* pointer physically in external ram pointing to object in internal ram
9750 __data unsigned char * __xdata p;
9754 /* pointer physically in code rom pointing to data in xdata space */
9756 __xdata unsigned char * __code p;
9760 /* pointer physically in code space pointing to data in code space */
9762 __code unsigned char * __code p;
9766 /* the following is a generic pointer physically located in xdata space
9773 /* the following is a function pointer physically located in data space
9776 char (* __data fp)(void);
9779 Well you get the idea.
9784 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9797 The highest order byte of the
9801 pointers contains the data space information.
9802 Assembler support routines are called whenever data is stored or retrieved
9808 These are useful for developing reusable library
9809 \begin_inset LatexCommand \index{Libraries}
9814 Explicitly specifying the pointer type will generate the most efficient
9816 \layout Subsubsection
9818 Notes on MCS51 memory
9819 \begin_inset LatexCommand \index{MCS51 memory}
9826 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9827 RAM memory which is structured as follows:
9831 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9834 - Bytes 20-2F - 16 bytes to hold 128 bit
9835 \begin_inset LatexCommand \index{bit}
9841 - Bytes 30-7F - 80 bytes for general purpose use.
9846 Additionally some members of the MCS51 family may have up to 128 bytes of
9847 additional, indirectly addressable, internal RAM memory (
9852 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9857 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9862 Furthermore, some chips may have some built in external memory (
9867 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9872 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9876 ) which should not be confused with the internal, directly addressable RAM
9882 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9887 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9892 Sometimes this built in
9896 memory has to be activated before using it (you can probably find this
9897 information on the datasheet of the microcontroller your are using, see
9899 \begin_inset LatexCommand \ref{sub:Startup-Code}
9907 Normally SDCC will only use the first bank
9908 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9912 of registers (register bank 0), but it is possible to specify that other
9913 banks of registers (keyword
9920 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9925 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9931 ) should be used in interrupt
9932 \begin_inset LatexCommand \index{interrupt}
9937 \begin_inset LatexCommand \index{\_\_interrupt}
9942 By default, the compiler will place the stack after the last byte of allocated
9943 memory for variables.
9944 For example, if the first 2 banks of registers are used, and only four
9949 variables, it will position the base of the internal stack at address 20
9951 This implies that as the stack
9952 \begin_inset LatexCommand \index{stack}
9956 grows, it will use up the remaining register banks, and the 16 bytes used
9957 by the 128 bit variables, and 80 bytes for general purpose use.
9958 If any bit variables are used, the data variables will be placed in unused
9959 register banks and after the byte holding the last bit variable.
9960 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9965 variables will be placed starting from address 0x10 to 0x20 and continue
9978 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9982 to specify the start address of the
9997 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10001 to specify the size of the total internal RAM (
10015 By default the 8051 linker will place the stack after the last byte of (i)data
10028 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10032 allows you to specify the start of the stack, i.e.
10033 you could start it after any data in the general purpose area.
10034 If your microcontroller has additional indirectly addressable internal
10039 ) you can place the stack on it.
10040 You may also need to use -
10051 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10055 to set the start address of the external RAM (
10070 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10074 to specify its size.
10075 Same goes for the code memory, using -
10086 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10101 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10106 If in doubt, don't specify any options and see if the resulting memory
10107 layout is appropriate, then you can adjust it.
10110 The linker generates two files with memory allocation information.
10111 The first, with extension .map
10112 \begin_inset LatexCommand \index{<file>.map}
10116 shows all the variables and segments.
10117 The second with extension .mem
10118 \begin_inset LatexCommand \index{<file>.mem}
10122 shows the final memory layout.
10123 The linker will complain either if memory segments overlap, there is not
10124 enough memory, or there is not enough space for stack.
10125 If you get any linking warnings and/or errors related to stack or segments
10126 allocation, take a look at either the .map or .mem files to find out what
10128 The .mem file may even suggest a solution to the problem.
10131 Z80/Z180 Storage Class
10132 \begin_inset LatexCommand \index{Storage class}
10136 Language Extensions
10137 \layout Subsubsection
10140 \begin_inset LatexCommand \index{sfr}
10145 \begin_inset LatexCommand \index{\_\_sfr}
10149 (in/out to 8-bit addresses)
10153 \begin_inset LatexCommand \index{Z80}
10157 family has separate address spaces for memory and
10167 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10171 is accessed with special instructions, e.g.:
10176 sfr at 0x78 IoPort;\SpecialChar ~
10178 /* define a var in I/O space at 78h called IoPort */
10182 Writing 0x01 to this variable generates the assembly code:
10187 3E 01\SpecialChar ~
10195 D3 78\SpecialChar ~
10202 \layout Subsubsection
10205 \begin_inset LatexCommand \index{sfr}
10210 \begin_inset LatexCommand \index{\_\_sfr}
10214 (in/out to 16-bit addresses)
10221 is used to support 16 bit addresses in I/O memory e.g.:
10227 \begin_inset LatexCommand \index{at}
10232 \begin_inset LatexCommand \index{\_\_at}
10239 Writing 0x01 to this variable generates the assembly code:
10244 01 23 01\SpecialChar ~
10249 3E 01\SpecialChar ~
10257 ED 79\SpecialChar ~
10264 \layout Subsubsection
10267 \begin_inset LatexCommand \index{sfr}
10272 \begin_inset LatexCommand \index{\_\_sfr}
10276 (in0/out0 to 8 bit addresses on Z180
10277 \begin_inset LatexCommand \index{Z180}
10282 \begin_inset LatexCommand \index{HD64180}
10289 The compiler option -
10299 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
10301 \begin_inset LatexCommand \index{\#pragma portmode}
10305 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10315 If you include the file z180.h this will be set automatically.
10319 \begin_inset LatexCommand \index{Storage class}
10323 Language Extensions
10324 \layout Subsubsection
10327 \begin_inset LatexCommand \index{data (hc08 storage class)}
10332 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10339 The data storage class declares a variable that resides in the first 256
10340 bytes of memory (the direct page).
10341 The HC08 is most efficient at accessing variables (especially pointers)
10343 \layout Subsubsection
10346 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10351 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10358 The xdata storage class declares a variable that can reside anywhere in
10360 This is the default if no storage class is specified.
10364 Absolute Addressing
10365 \begin_inset LatexCommand \index{Absolute addressing}
10372 Data items can be assigned an absolute address with the
10375 \begin_inset LatexCommand \index{at}
10380 \begin_inset LatexCommand \index{\_\_at}
10386 keyword, in addition to a storage class, e.g.:
10392 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10397 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10402 \begin_inset LatexCommand \index{at}
10407 \begin_inset LatexCommand \index{\_\_at}
10411 (0x7ffe) unsigned int chksum;
10419 __xdata __at (0x7ffe) unsigned int chksum;
10422 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10423 of the external ram.
10428 reserve any space for variables declared in this way
10429 \begin_inset Marginal
10440 (they are implemented with an equate in the assembler).
10441 Thus it is left to the programmer to make sure there are no overlaps with
10442 other variables that are declared without the absolute address.
10443 The assembler listing file (.lst
10444 \begin_inset LatexCommand \index{<file>.lst}
10448 ) and the linker output files (.rst
10449 \begin_inset LatexCommand \index{<file>.rst}
10454 \begin_inset LatexCommand \index{<file>.map}
10458 ) are good places to look for such overlaps.
10459 Variables with an absolute address are
10462 \begin_inset Marginal
10476 \begin_inset LatexCommand \index{Variable initialization}
10483 In case of memory mapped I/O devices the keyword
10487 has to be used to tell the compiler that accesses might not be removed:
10493 \begin_inset LatexCommand \index{volatile}
10498 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10503 \begin_inset LatexCommand \index{at}
10507 (0x8000) unsigned char PORTA_8255;
10510 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10515 \begin_inset LatexCommand \index{Aligned array}
10522 starts at a block (256 byte) boundary
10523 \begin_inset LatexCommand \index{block boundary}
10528 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10534 Absolute addresses can be specified for variables in all storage classes,
10541 \begin_inset LatexCommand \index{bit}
10546 \begin_inset LatexCommand \index{at}
10553 The above example will allocate the variable at offset 0x02 in the bit-addressab
10555 There is no real advantage to assigning absolute addresses to variables
10556 in this manner, unless you want strict control over all the variables allocated.
10557 One possible use would be to write hardware portable code.
10558 For example, if you have a routine that uses one or more of the microcontroller
10559 I/O pins, and such pins are different for two different hardwares, you
10560 can declare the I/O pins in your routine using:
10566 \begin_inset LatexCommand \index{volatile}
10570 __bit MOSI;\SpecialChar ~
10574 /* master out, slave in */
10576 extern volatile __bit MISO;\SpecialChar ~
10580 /* master in, slave out */
10582 extern volatile __bit MCLK;\SpecialChar ~
10590 /* Input and Output of a byte on a 3-wire serial bus.
10595 If needed adapt polarity of clock, polarity of data and bit order
10600 unsigned char spi_io(unsigned char out_byte)
10624 MOSI = out_byte & 0x80;
10654 /* _asm nop _endasm; */\SpecialChar ~
10662 /* for slow peripherals */
10713 Then, someplace in the code for the first hardware you would use
10719 \begin_inset LatexCommand \index{at}
10724 \begin_inset LatexCommand \index{\_\_at}
10728 (0x80) MOSI;\SpecialChar ~
10732 /* I/O port 0, bit 0 */
10734 __bit __at (0x81) MISO;\SpecialChar ~
10738 /* I/O port 0, bit 1 */
10740 __bit __at (0x82) MCLK;\SpecialChar ~
10744 /* I/O port 0, bit 2 */
10747 Similarly, for the second hardware you would use
10752 __bit __at (0x83) MOSI;\SpecialChar ~
10756 /* I/O port 0, bit 3 */
10758 __bit __at (0x91) MISO;\SpecialChar ~
10762 /* I/O port 1, bit 1 */
10765 \begin_inset LatexCommand \index{bit}
10769 __at (0x92) MCLK;\SpecialChar ~
10773 /* I/O port 1, bit 2 */
10776 and you can use the same hardware dependent routine without changes, as
10777 for example in a library.
10778 This is somehow similar to sbit, but only one absolute address has to be
10779 specified in the whole project.
10783 \begin_inset LatexCommand \index{Parameters}
10788 \begin_inset LatexCommand \index{function parameter}
10793 \begin_inset LatexCommand \index{local variables}
10798 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10805 Automatic (local) variables and parameters to functions can either be placed
10806 on the stack or in data-space.
10807 The default action of the compiler is to place these variables in the internal
10808 RAM (for small model) or external RAM (for large model).
10809 This in fact makes them similar to
10812 \begin_inset LatexCommand \index{static}
10818 so by default functions are non-reentrant
10819 \begin_inset LatexCommand \index{reentrant}
10828 They can be placed on the stack
10829 \begin_inset LatexCommand \index{stack}
10846 \begin_inset LatexCommand \index{-\/-stack-auto}
10854 #pragma\SpecialChar ~
10858 \begin_inset LatexCommand \index{\#pragma stackauto}
10865 \begin_inset LatexCommand \index{reentrant}
10871 keyword in the function declaration, e.g.:
10876 unsigned char foo(char i) __reentrant
10890 Since stack space on 8051 is limited, the
10908 option should be used sparingly.
10909 Note that the reentrant keyword just means that the parameters & local
10910 variables will be allocated to the stack, it
10914 mean that the function is register bank
10915 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10924 \begin_inset LatexCommand \index{local variables}
10928 can be assigned storage classes and absolute
10929 \begin_inset LatexCommand \index{Absolute addressing}
10938 unsigned char foo()
10946 __xdata unsigned char i;
10959 \begin_inset LatexCommand \index{at}
10963 (0x31) unsigned char j;
10975 In the above example the variable
10979 will be allocated in the external ram,
10983 in bit addressable space and
11002 or when a function is declared as
11006 this should only be done for static variables.
11010 \begin_inset LatexCommand \index{function parameter}
11014 however are not allowed any storage class
11015 \begin_inset LatexCommand \index{Storage class}
11019 , (storage classes for parameters will be ignored), their allocation is
11020 governed by the memory model in use, and the reentrancy options.
11023 It is however allowed to use bit parameters in reentrant functions and also
11024 non-static local bit variables are supported.
11025 Efficient use is limited to 8 semi-bitregisters in bit space.
11026 They are pushed and popped to stack as a single byte just like the normal
11031 \begin_inset LatexCommand \label{sub:Overlaying}
11036 \begin_inset LatexCommand \index{Overlaying}
11044 \begin_inset LatexCommand \index{reentrant}
11048 functions SDCC will try to reduce internal ram space usage by overlaying
11049 parameters and local variables of a function (if possible).
11050 Parameters and local variables
11051 \begin_inset LatexCommand \index{local variables}
11055 of a function will be allocated to an overlayable segment if the function
11058 no other function calls and the function is non-reentrant and the memory
11060 \begin_inset LatexCommand \index{Memory model}
11067 If an explicit storage class
11068 \begin_inset LatexCommand \index{Storage class}
11072 is specified for a local variable, it will NOT be overlayed.
11075 Note that the compiler (not the linkage editor) makes the decision for overlayin
11077 Functions that are called from an interrupt service routine
11078 \begin_inset Marginal
11088 should be preceded by a #pragma\SpecialChar ~
11090 \begin_inset LatexCommand \index{\#pragma nooverlay}
11094 if they are not reentrant.
11097 Also note that the compiler does not do any processing of inline assembler
11098 code, so the compiler might incorrectly assign local variables and parameters
11099 of a function into the overlay segment if the inline assembler code calls
11100 other c-functions that might use the overlay.
11101 In that case the #pragma\SpecialChar ~
11102 nooverlay should be used.
11105 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11107 \begin_inset LatexCommand \index{Multiplication}
11112 \begin_inset LatexCommand \index{Division}
11116 will NOT be overlayed since these are implemented using external functions,
11125 \begin_inset LatexCommand \index{\#pragma nooverlay}
11131 void set_error(unsigned char errcd)
11147 void some_isr () __interrupt
11148 \begin_inset LatexCommand \index{interrupt}
11178 In the above example the parameter
11186 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11188 not present, this could cause unpredictable runtime behavior when called
11189 from an interrupt service routine.
11190 The #pragma\SpecialChar ~
11191 nooverlay ensures that the parameters and local variables for
11192 the function are NOT overlayed.
11195 Interrupt Service Routines
11196 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11203 General Information
11218 outines to be coded in C, with some extended keywords.
11223 void timer_isr (void) __interrupt (1) __using (1)
11237 The optional number following the
11240 \begin_inset LatexCommand \index{interrupt}
11245 \begin_inset LatexCommand \index{\_\_interrupt}
11251 keyword is the interrupt number this routine will service.
11252 When present, the compiler will insert a call to this routine in the interrupt
11253 vector table for the interrupt number specified.
11254 If you have multiple source files in your project, interrupt service routines
11255 can be present in any of them, but a prototype of the isr MUST be present
11256 or included in the file that contains the function
11264 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11269 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11275 keyword can be used to tell the compiler to use the specified register
11276 bank (8051 specific) when generating code for this function.
11282 Interrupt service routines open the door for some very interesting bugs:
11284 If an interrupt service routine changes variables which are accessed by
11285 other functions these variables have to be declared
11290 \begin_inset LatexCommand \index{volatile}
11298 If the access to these variables is not
11301 \begin_inset LatexCommand \index{atomic}
11308 the processor needs more than one instruction for the access and could
11309 be interrupted while accessing the variable) the interrupt must be disabled
11310 during the access to avoid inconsistent data.
11311 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11312 and should be protected by disabling interrupts.
11313 You're not automatically on the safe side if you use 8 bit variables though.
11314 We need an example here: f.e.
11315 on the 8051 the harmless looking
11316 \begin_inset Quotes srd
11321 flags\SpecialChar ~
11326 \begin_inset Quotes sld
11335 \begin_inset Quotes srd
11340 flags\SpecialChar ~
11345 \begin_inset Quotes sld
11348 from within an interrupt routine might get lost if the interrupt occurs
11351 \begin_inset Quotes sld
11356 counter\SpecialChar ~
11361 \begin_inset Quotes srd
11364 is not atomic on the 8051 even if
11368 is located in data memory.
11369 Bugs like these are hard to reproduce and can cause a lot of trouble.
11373 The return address and the registers used in the interrupt service routine
11374 are saved on the stack
11375 \begin_inset LatexCommand \index{stack}
11379 so there must be sufficient stack space.
11380 If there isn't variables or registers (or even the return address itself)
11387 \begin_inset LatexCommand \index{stack overflow}
11391 is most likely to happen if the interrupt occurs during the
11392 \begin_inset Quotes sld
11396 \begin_inset Quotes srd
11399 subroutine when the stack is already in use for f.e.
11400 many return addresses.
11403 A special note here, int (16 bit) and long (32 bit) integer division
11404 \begin_inset LatexCommand \index{Division}
11409 \begin_inset LatexCommand \index{Multiplication}
11414 \begin_inset LatexCommand \index{Modulus}
11419 \begin_inset LatexCommand \index{Floating point support}
11423 operations are implemented using external support routines developed in
11425 If an interrupt service routine needs to do any of these operations then
11426 the support routines (as mentioned in a following section) will have to
11427 be recompiled using the
11440 \begin_inset LatexCommand \index{-\/-stack-auto}
11446 option and the source file will need to be compiled using the
11461 \begin_inset LatexCommand \index{-\/-int-long-reent}
11466 Note, the type promotion
11467 \begin_inset LatexCommand \index{type promotion}
11471 required by ANSI C can cause 16 bit routines to be used without the programmer
11476 Calling other functions from an interrupt service routine is not recommended,
11477 avoid it if possible.
11478 Note that when some function is called from an interrupt service routine
11479 it should be preceded by a #pragma\SpecialChar ~
11481 \begin_inset LatexCommand \index{\#pragma nooverlay}
11485 if it is not reentrant.
11486 Furthermore nonreentrant functions should not be called from the main program
11487 while the interrupt service routine might be active.
11488 They also must not be called from low priority interrupt service routines
11489 while a high priority interrupt service routine might be active.
11490 You could use semaphores or make the function
11494 if all parameters are passed in registers.
11499 \begin_inset LatexCommand \ref{sub:Overlaying}
11504 about Overlaying and section
11505 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11510 about Functions using private register banks.
11513 MCS51/DS390 Interrupt Service Routines
11516 Interrupt numbers and the corresponding address & descriptions for the Standard
11517 8051/8052 are listed below.
11518 SDCC will automatically adjust the interrupt vector table to the maximum
11519 interrupt number specified.
11525 \begin_inset Tabular
11526 <lyxtabular version="3" rows="9" columns="3">
11528 <column alignment="center" valignment="top" leftline="true" width="0in">
11529 <column alignment="left" valignment="top" leftline="true" width="0in">
11530 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11531 <row topline="true" bottomline="true">
11532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11557 <row topline="true">
11558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11566 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11574 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11583 <row topline="true">
11584 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11600 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11609 <row topline="true">
11610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11618 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11635 <row topline="true">
11636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11652 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11661 <row topline="true">
11662 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11670 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11678 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11687 <row topline="true">
11688 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11696 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11704 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11713 <row topline="true">
11714 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11729 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11738 <row topline="true" bottomline="true">
11739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11747 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11754 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11772 If the interrupt service routine is defined without
11775 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11780 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11786 a register bank or with register bank 0 (
11790 0), the compiler will save the registers used by itself on the stack upon
11791 entry and restore them at exit, however if such an interrupt service routine
11792 calls another function then the entire register bank will be saved on the
11794 This scheme may be advantageous for small interrupt service routines which
11795 have low register usage.
11798 If the interrupt service routine is defined to be using a specific register
11803 & psw are saved and restored, if such an interrupt service routine calls
11804 another function (using another register bank) then the entire register
11805 bank of the called function will be saved on the stack.
11806 This scheme is recommended for larger interrupt service routines.
11809 HC08 Interrupt Service Routines
11812 Since the number of interrupts available is chip specific and the interrupt
11813 vector table always ends at the last byte of memory, the interrupt numbers
11814 corresponds to the interrupt vectors in reverse order of address.
11815 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11816 2 will use the interrupt vector at 0xfffa, and so on.
11817 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11818 this way; instead see section
11819 \begin_inset LatexCommand \ref{sub:Startup-Code}
11823 for details on customizing startup.
11826 Z80 Interrupt Service Routines
11829 The Z80 uses several different methods for determining the correct interrupt
11830 vector depending on the hardware implementation.
11831 Therefore, SDCC ignores the optional interrupt number and does not attempt
11832 to generate an interrupt vector table.
11835 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11836 instruction to return from the interrupt.
11837 To write an interrupt handler for the non-maskable interrupt, which needs
11838 a RETN instruction instead, add the
11847 void nmi_isr (void) critical interrupt
11861 However if you need to create a non-interruptable interrupt service routine
11862 you would also require the
11867 To distinguish between this and an nmi_isr you must provide an interrupt
11871 Enabling and Disabling Interrupts
11874 Critical Functions and Critical Statements
11877 A special keyword may be associated with a block or a function declaring
11883 SDCC will generate code to disable all interrupts
11884 \begin_inset LatexCommand \index{interrupt}
11888 upon entry to a critical function and restore the interrupt enable to the
11889 previous state before returning.
11890 Nesting critical functions will need one additional byte on the stack
11891 \begin_inset LatexCommand \index{stack}
11900 int foo () __critical
11901 \begin_inset LatexCommand \index{critical}
11906 \begin_inset LatexCommand \index{\_\_critical}
11931 The critical attribute maybe used with other attributes like
11941 may also be used to disable interrupts more locally:
11949 More than one statement could have been included in the block.
11952 Enabling and Disabling Interrupts directly
11956 \begin_inset LatexCommand \index{interrupt}
11960 can also be disabled and enabled directly (8051):
11965 EA = 0;\SpecialChar ~
12028 EA = 1;\SpecialChar ~
12095 On other architectures which have seperate opcodes for enabling and disabling
12096 interrupts you might want to make use of defines with inline assembly
12097 \begin_inset LatexCommand \index{Assembler routines}
12107 \begin_inset LatexCommand \index{\_asm}
12116 \begin_inset LatexCommand \index{\_endasm}
12125 #define SEI _asm\SpecialChar ~
12137 Note: it is sometimes sufficient to disable only a specific interrupt source
12139 a timer or serial interrupt by manipulating an
12142 \begin_inset LatexCommand \index{interrupt mask}
12152 Usually the time during which interrupts are disabled should be kept as
12154 This minimizes both
12159 \begin_inset LatexCommand \index{interrupt latency}
12163 (the time between the occurrence of the interrupt and the execution of
12164 the first code in the interrupt routine) and
12169 \begin_inset LatexCommand \index{interrupt jitter}
12173 (the difference between the shortest and the longest interrupt latency).
12174 These really are something different, f.e.
12175 a serial interrupt has to be served before its buffer overruns so it cares
12176 for the maximum interrupt latency, whereas it does not care about jitter.
12177 On a loudspeaker driven via a digital to analog converter which is fed
12178 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12179 a much smaller jitter will be very audible.
12182 You can reenable interrupts within an interrupt routine and on some architecture
12183 s you can make use of two (or more) levels of
12185 interrupt priorities
12188 \begin_inset LatexCommand \index{interrupt priority}
12193 On some architectures which don't support interrupt priorities these can
12194 be implemented by manipulating the interrupt mask and reenabling interrupts
12195 within the interrupt routine.
12196 Check there is sufficient space on the stack
12197 \begin_inset LatexCommand \index{stack}
12201 and don't add complexity unless you have to.
12206 \begin_inset LatexCommand \index{semaphore}
12210 locking (mcs51/ds390)
12213 Some architectures (mcs51/ds390) have an atomic
12214 \begin_inset LatexCommand \index{atomic}
12227 These type of instructions are typically used in preemptive multitasking
12228 systems, where a routine f.e.
12229 claims the use of a data structure ('acquires a lock
12230 \begin_inset LatexCommand \index{lock}
12234 on it'), makes some modifications and then releases the lock when the data
12235 structure is consistent again.
12236 The instruction may also be used if interrupt and non-interrupt code have
12237 to compete for a resource.
12238 With the atomic bit test and clear instruction interrupts
12239 \begin_inset LatexCommand \index{interrupt}
12243 don't have to be disabled for the locking operation.
12247 SDCC generates this instruction if the source follows this pattern:
12253 \begin_inset LatexCommand \index{volatile}
12257 bit resource_is_free;
12261 if (resource_is_free)
12271 resource_is_free=0;
12284 resource_is_free=1;
12291 Note, mcs51 and ds390 support only an atomic
12292 \begin_inset LatexCommand \index{atomic}
12300 instruction (as opposed to atomic bit test and
12305 Functions using private register banks
12306 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12313 Some architectures have support for quickly changing register sets.
12314 SDCC supports this feature with the
12317 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12322 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12328 attribute (which tells the compiler to use a register bank
12329 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12333 other than the default bank zero).
12334 It should only be applied to
12337 \begin_inset LatexCommand \index{interrupt}
12343 functions (see footnote below).
12344 This will in most circumstances make the generated ISR code more efficient
12345 since it will not have to save registers on the stack.
12352 attribute will have no effect on the generated code for a
12356 function (but may occasionally be useful anyway
12362 possible exception: if a function is called ONLY from 'interrupt' functions
12363 using a particular bank, it can be declared with the same 'using' attribute
12364 as the calling 'interrupt' functions.
12365 For instance, if you have several ISRs using bank one, and all of them
12366 call memcpy(), it might make sense to create a specialized version of memcpy()
12367 'using 1', since this would prevent the ISR from having to save bank zero
12368 to the stack on entry and switch to bank zero before calling the function
12375 (pending: I don't think this has been done yet)
12382 function using a non-zero bank will assume that it can trash that register
12383 bank, and will not save it.
12384 Since high-priority interrupts
12385 \begin_inset LatexCommand \index{interrupts}
12390 \begin_inset LatexCommand \index{interrupt priority}
12394 can interrupt low-priority ones on the 8051 and friends, this means that
12395 if a high-priority ISR
12399 a particular bank occurs while processing a low-priority ISR
12403 the same bank, terrible and bad things can happen.
12404 To prevent this, no single register bank should be
12408 by both a high priority and a low priority ISR.
12409 This is probably most easily done by having all high priority ISRs use
12410 one bank and all low priority ISRs use another.
12411 If you have an ISR which can change priority at runtime, you're on your
12412 own: I suggest using the default bank zero and taking the small performance
12416 It is most efficient if your ISR calls no other functions.
12417 If your ISR must call other functions, it is most efficient if those functions
12418 use the same bank as the ISR (see note 1 below); the next best is if the
12419 called functions use bank zero.
12420 It is very inefficient to call a function using a different, non-zero bank
12426 \begin_inset LatexCommand \label{sub:Startup-Code}
12431 \begin_inset LatexCommand \index{Startup code}
12438 MCS51/DS390 Startup Code
12441 The compiler inserts a call to the C routine
12443 _sdcc_external_startup()
12444 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12453 at the start of the CODE area.
12454 This routine is in the runtime library
12455 \begin_inset LatexCommand \index{Runtime library}
12460 By default this routine returns 0, if this routine returns a non-zero value,
12461 the static & global variable initialization will be skipped and the function
12462 main will be invoked.
12463 Otherwise static & global variables will be initialized before the function
12467 _sdcc_external_startup()
12469 routine to your program to override the default if you need to setup hardware
12470 or perform some other critical operation prior to static & global variable
12472 \begin_inset LatexCommand \index{Variable initialization}
12477 On some mcs51 variants xdata
12478 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12482 memory has to be explicitly enabled before it can be accessed or if the
12483 watchdog needs to be disabled, this is the place to do it.
12484 The startup code clears all internal data memory, 256 bytes by default,
12485 but from 0 to n-1 if
12498 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12505 (recommended for Chipcon CC1010).
12508 See also the compiler option
12527 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12532 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12537 about MCS51-variants.
12543 The HC08 startup code follows the same scheme as the MCS51 startup code.
12549 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12550 from sdcc/device/lib/z80/crt0.s.
12551 If you need a different startup code you can use the compiler option
12572 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12576 and provide your own crt0.o.
12580 Inline Assembler Code
12581 \begin_inset LatexCommand \index{Assembler routines}
12588 A Step by Step Introduction
12589 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12596 Starting from a small snippet of c-code this example shows for the MCS51
12597 how to use inline assembly, access variables, a function parameter and
12598 an array in xdata memory.
12599 The example uses an MCS51 here but is easily adapted for other architectures.
12600 This is a buffer routine which should be optimized:
12606 unsigned char __far
12607 \begin_inset LatexCommand \index{far (storage class)}
12612 \begin_inset LatexCommand \index{\_\_far (storage class)}
12617 \begin_inset LatexCommand \index{at}
12622 \begin_inset LatexCommand \index{\_\_at}
12626 (0x7f00) buf[0x100];
12627 \begin_inset LatexCommand \index{Aligned array}
12633 unsigned char head, tail;
12637 void to_buffer( unsigned char c )
12645 if( head != (unsigned char)(tail-1) )\SpecialChar ~
12651 \begin_inset LatexCommand \index{promotion to signed int}
12656 \begin_inset LatexCommand \index{type promotion}
12661 \begin_inset Marginal
12682 buf[ head++ ] = c;\SpecialChar ~
12698 /* access to a 256 byte aligned array */
12703 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12704 then a corresponding buffer.asm file is generated.
12705 We define a new function
12709 in file buffer.c in which we cut and paste the generated code, removing
12710 unwanted comments and some ':'.
12712 \begin_inset Quotes sld
12716 \begin_inset Quotes srd
12720 \begin_inset Quotes sld
12724 \begin_inset Quotes srd
12727 to the beginning and the end of the function body:
12733 /* With a cut and paste from the .asm file, we have something to start with.
12738 The function is not yet OK! (registers aren't saved) */
12740 void to_buffer_asm( unsigned char c )
12749 \begin_inset LatexCommand \index{\_asm}
12754 \begin_inset LatexCommand \index{\_\_asm}
12768 ;buffer.c if( head != (unsigned char)(tail-1) )
12816 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12817 \begin_inset LatexCommand \index{Aligned array}
12882 \begin_inset LatexCommand \index{\_endasm}
12887 \begin_inset LatexCommand \index{\_\_endasm}
12896 The new file buffer.c should compile with only one warning about the unreferenced
12897 function argument 'c'.
12898 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12899 (1) and finally have:
12905 unsigned char __far __at(0x7f00) buf[0x100];
12907 unsigned char head, tail;
12909 #define USE_ASSEMBLY (1)
12917 void to_buffer( unsigned char c )
12925 if( head != (unsigned char)(tail-1) )
12945 void to_buffer( unsigned char c )
12953 c; // to avoid warning: unreferenced function argument
12960 \begin_inset LatexCommand \index{\_asm}
12965 \begin_inset LatexCommand \index{\_\_asm}
12979 ; save used registers here.
12990 ; If we were still using r2,r3 we would have to push them here.
12993 ; if( head != (unsigned char)(tail-1) )
13036 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13060 ; buf[ head++ ] = c;
13071 a,dpl \SpecialChar ~
13078 ; dpl holds lower byte of function argument
13089 dpl,_head \SpecialChar ~
13092 ; buf is 0x100 byte aligned so head can be used directly
13134 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13146 ; restore used registers here
13153 \begin_inset LatexCommand \index{\_endasm}
13158 \begin_inset LatexCommand \index{\_\_endasm}
13169 The inline assembler code can contain any valid code understood by the assembler
13170 , this includes any assembler directives and comment lines
13176 The assembler does not like some characters like ':' or ''' in comments.
13177 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13178 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13183 \begin_inset LatexCommand \index{Assembler documentation}
13191 The compiler does not do any validation of the code within the
13194 \begin_inset LatexCommand \index{\_asm}
13199 \begin_inset LatexCommand \index{\_\_asm}
13207 \begin_inset LatexCommand \index{\_endasm}
13212 \begin_inset LatexCommand \index{\_\_endasm}
13221 Specifically it will not know which registers are used and thus register
13223 \begin_inset LatexCommand \index{push/pop}
13227 has to be done manually.
13231 It is recommended that each assembly instruction (including labels) be placed
13232 in a separate line (as the example shows).
13246 \begin_inset LatexCommand \index{-\/-peep-asm}
13252 command line option is used, the inline assembler code will be passed through
13253 the peephole optimizer
13254 \begin_inset LatexCommand \index{Peephole optimizer}
13259 There are only a few (if any) cases where this option makes sense, it might
13260 cause some unexpected changes in the inline assembler code.
13261 Please go through the peephole optimizer rules defined in file
13265 before using this option.
13269 \begin_inset LatexCommand \label{sub:Naked-Functions}
13274 \begin_inset LatexCommand \index{Naked functions}
13281 A special keyword may be associated with a function declaring it as
13284 \begin_inset LatexCommand \index{\_naked}
13289 \begin_inset LatexCommand \index{\_\_naked}
13300 function modifier attribute prevents the compiler from generating prologue
13301 \begin_inset LatexCommand \index{function prologue}
13306 \begin_inset LatexCommand \index{function epilogue}
13310 code for that function.
13311 This means that the user is entirely responsible for such things as saving
13312 any registers that may need to be preserved, selecting the proper register
13313 bank, generating the
13317 instruction at the end, etc.
13318 Practically, this means that the contents of the function must be written
13319 in inline assembler.
13320 This is particularly useful for interrupt functions, which can have a large
13321 (and often unnecessary) prologue/epilogue.
13322 For example, compare the code generated by these two functions:
13328 \begin_inset LatexCommand \index{volatile}
13332 data unsigned char counter;
13336 void simpleInterrupt(void) __interrupt
13337 \begin_inset LatexCommand \index{interrupt}
13342 \begin_inset LatexCommand \index{\_\_interrupt}
13360 void nakedInterrupt(void) __interrupt (2) __naked
13369 \begin_inset LatexCommand \index{\_asm}
13374 \begin_inset LatexCommand \index{\_\_asm}
13391 _counter ; does not change flags, no need to save psw
13403 ; MUST explicitly include ret or reti in _naked function.
13410 \begin_inset LatexCommand \index{\_endasm}
13415 \begin_inset LatexCommand \index{\_\_endasm}
13424 For an 8051 target, the generated simpleInterrupt looks like:
13433 example, recent versions of SDCC generate
13435 the same code for simpleInterrupt() and nakedInterrupt()!
13575 whereas nakedInterrupt looks like:
13590 _counter ; does not change flags, no need to save psw
13608 ; MUST explicitly include ret or reti in _naked function
13611 The related directive #pragma exclude
13612 \begin_inset LatexCommand \index{\#pragma exclude}
13616 allows a more fine grained control over pushing & popping
13617 \begin_inset LatexCommand \index{push/pop}
13624 While there is nothing preventing you from writing C code inside a
13628 function, there are many ways to shoot yourself in the foot doing this,
13629 and it is recommended that you stick to inline assembler.
13632 Use of Labels within Inline Assembler
13635 SDCC allows the use of in-line assembler with a few restrictions regarding
13637 In older versions of the compiler all labels defined within inline assembler
13646 where nnnn is a number less than 100 (which implies a limit of utmost 100
13647 inline assembler labels
13661 \begin_inset LatexCommand \index{\_asm}
13666 \begin_inset LatexCommand \index{\_\_asm}
13696 \begin_inset LatexCommand \index{\_endasm}
13701 \begin_inset LatexCommand \index{\_\_endasm}
13708 Inline assembler code cannot reference any C-Labels, however it can reference
13710 \begin_inset LatexCommand \index{Labels}
13714 defined by the inline assembler, e.g.:
13739 ; some assembler code
13759 /* some more c code */
13761 clabel:\SpecialChar ~
13763 /* inline assembler cannot reference this label */
13775 $0003: ;label (can be referenced by inline assembler only)
13782 \begin_inset LatexCommand \index{\_endasm}
13787 \begin_inset LatexCommand \index{\_\_endasm}
13797 /* some more c code */
13802 In other words inline assembly code can access labels defined in inline
13803 assembly within the scope of the function.
13804 The same goes the other way, i.e.
13805 labels defines in inline assembly can not be accessed by C statements.
13808 Interfacing with Assembler Code
13809 \begin_inset LatexCommand \index{Assembler routines}
13816 Global Registers used for Parameter Passing
13817 \begin_inset LatexCommand \index{Parameter passing}
13824 The compiler always uses the global registers
13827 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13832 \begin_inset LatexCommand \index{DPTR}
13837 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13846 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13852 to pass the first parameter to a routine.
13853 The second parameter onwards is either allocated on the stack (for reentrant
13864 -stack-auto is used) or in data / xdata memory (depending on the memory
13869 Assembler Routine (non-reentrant)
13872 In the following example
13873 \begin_inset LatexCommand \index{reentrant}
13878 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13882 the function c_func calls an assembler routine asm_func, which takes two
13884 \begin_inset LatexCommand \index{function parameter}
13893 extern int asm_func(unsigned char, unsigned char);
13897 int c_func (unsigned char i, unsigned char j)
13905 return asm_func(i,j);
13919 return c_func(10,9);
13924 The corresponding assembler function is:
13929 .globl _asm_func_PARM_2
14030 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14047 Note here that the return values
14048 \begin_inset LatexCommand \index{return value}
14052 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14054 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14055 b' & 'acc' for four byte values.
14058 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14059 the parameter number starting from 1, and counting from the left.
14060 The first parameter is passed in
14061 \begin_inset Quotes eld
14065 \begin_inset Quotes erd
14068 for a one byte parameter,
14069 \begin_inset Quotes eld
14073 \begin_inset Quotes erd
14077 \begin_inset Quotes eld
14081 \begin_inset Quotes erd
14084 for three bytes and
14085 \begin_inset Quotes eld
14089 \begin_inset Quotes erd
14092 for a four bytes parameter.
14093 The variable name for the second parameter will be _<function_name>_PARM_2.
14097 Assemble the assembler routine with the following command:
14104 asx8051 -losg asmfunc.asm
14111 Then compile and link the assembler routine to the C source file with the
14119 sdcc cfunc.c asmfunc.rel
14122 Assembler Routine (reentrant)
14126 \begin_inset LatexCommand \index{reentrant}
14131 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14135 the second parameter
14136 \begin_inset LatexCommand \index{function parameter}
14140 onwards will be passed on the stack, the parameters are pushed from right
14142 after the call the leftmost parameter will be on the top of the stack.
14143 Here is an example:
14148 extern int asm_func(unsigned char, unsigned char);
14152 int c_func (unsigned char i, unsigned char j) reentrant
14160 return asm_func(i,j);
14174 return c_func(10,9);
14179 The corresponding assembler routine is:
14279 The compiling and linking procedure remains the same, however note the extra
14280 entry & exit linkage required for the assembler code, _bp is the stack
14281 frame pointer and is used to compute the offset into the stack for parameters
14282 and local variables.
14286 \begin_inset LatexCommand \index{int (16 bit)}
14291 \begin_inset LatexCommand \index{long (32 bit)}
14298 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14299 multiplication and modulus operations are implemented by support routines.
14300 These support routines are all developed in ANSI-C to facilitate porting
14301 to other MCUs, although some model specific assembler optimizations are
14303 The following files contain the described routines, all of them can be
14304 found in <installdir>/share/sdcc/lib.
14310 \begin_inset Tabular
14311 <lyxtabular version="3" rows="11" columns="2">
14313 <column alignment="left" valignment="top" leftline="true" width="0">
14314 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14315 <row topline="true" bottomline="true">
14316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14337 <row topline="true">
14338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14346 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14351 16 bit multiplication
14355 <row topline="true">
14356 <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">
14369 signed 16 bit division (calls _divuint)
14373 <row topline="true">
14374 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14387 unsigned 16 bit division
14391 <row topline="true">
14392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14405 signed 16 bit modulus (calls _moduint)
14409 <row topline="true">
14410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14418 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14423 unsigned 16 bit modulus
14427 <row topline="true">
14428 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14436 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14441 32 bit multiplication
14445 <row topline="true">
14446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14454 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14459 signed 32 division (calls _divulong)
14463 <row topline="true">
14464 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14472 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14477 unsigned 32 division
14481 <row topline="true">
14482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14490 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14495 signed 32 bit modulus (calls _modulong)
14499 <row topline="true" bottomline="true">
14500 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14508 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14513 unsigned 32 bit modulus
14526 Since they are compiled as
14531 \begin_inset LatexCommand \index{reentrant}
14536 \begin_inset LatexCommand \index{interrupt}
14540 service routines should not do any of the above operations.
14541 If this is unavoidable then the above routines will need to be compiled
14555 \begin_inset LatexCommand \index{-\/-stack-auto}
14561 option, after which the source program will have to be compiled with
14574 \begin_inset LatexCommand \index{-\/-int-long-reent}
14581 Notice that you don't have to call these routines directly.
14582 The compiler will use them automatically every time an integer operation
14586 Floating Point Support
14587 \begin_inset LatexCommand \index{Floating point support}
14594 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14595 floating point support routines are derived from gcc's floatlib.c and consist
14596 of the following routines:
14604 \begin_inset Tabular
14605 <lyxtabular version="3" rows="17" columns="2">
14607 <column alignment="left" valignment="top" leftline="true" width="0">
14608 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14609 <row topline="true" bottomline="true">
14610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14636 <row topline="true">
14637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14668 add floating point numbers
14672 <row topline="true">
14673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14690 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14704 subtract floating point numbers
14708 <row topline="true">
14709 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14726 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14740 divide floating point numbers
14744 <row topline="true">
14745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14776 multiply floating point numbers
14780 <row topline="true">
14781 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14798 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14812 convert floating point to unsigned char
14816 <row topline="true">
14817 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14834 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14848 convert floating point to signed char
14852 <row topline="true">
14853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14870 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14884 convert floating point to unsigned int
14888 <row topline="true">
14889 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14920 convert floating point to signed int
14924 <row topline="true">
14925 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14951 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14965 convert floating point to unsigned long
14969 <row topline="true">
14970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14987 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15001 convert floating point to signed long
15005 <row topline="true">
15006 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15023 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15037 convert unsigned char to floating point
15041 <row topline="true">
15042 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15059 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15073 convert char to floating point number
15077 <row topline="true">
15078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15109 convert unsigned int to floating point
15113 <row topline="true">
15114 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15145 convert int to floating point numbers
15149 <row topline="true">
15150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15181 convert unsigned long to floating point number
15185 <row topline="true" bottomline="true">
15186 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15217 convert long to floating point number
15230 These support routines are developed in ANSI-C so there is room for space
15231 and speed improvement
15237 These floating point routines (
15241 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15246 Note if all these routines are used simultaneously the data space might
15248 For serious floating point usage the large model might be needed.
15249 Also notice that you don't have to call this routines directly.
15250 The compiler will use them automatically every time a floating point operation
15255 \begin_inset LatexCommand \index{Libraries}
15264 <pending: this is messy and incomplete - a little more information is in
15265 sdcc/doc/libdoc.txt
15270 Compiler support routines (_gptrget, _mulint etc.)
15273 Stdclib functions (puts, printf, strcat etc.)
15274 \layout Subsubsection
15280 \begin_inset LatexCommand \index{<stdio.h>}
15284 As usual on embedded systems you have to provide your own
15287 \begin_inset LatexCommand \index{getchar()}
15296 \begin_inset LatexCommand \index{putchar()}
15303 SDCC does not know whether the system connects to a serial line with or
15304 without handshake, LCD, keyboard or other device.
15318 You'll find examples for serial routines f.e.
15319 in sdcc/device/lib.
15325 \begin_inset LatexCommand \index{printf()}
15335 does not support float (except on ds390).
15336 To enable this recompile it with the option
15349 \begin_inset LatexCommand \index{USE\_FLOATS}
15355 on the command line.
15369 \begin_inset LatexCommand \index{-\/-model-large}
15375 for the mcs51 port, since this uses a lot of memory.
15378 If you're short on memory you might want to use
15381 \begin_inset LatexCommand \index{printf\_small()}
15396 For the mcs51 there additionally are assembly versions
15399 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15405 (subset of printf using less than 270 bytes) and
15408 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15417 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15423 (floating-point aware version of printf_fast) which should fit the requirements
15424 of many embedded systems (printf_fast() can be customized by unsetting
15429 support long variables and field widths).
15430 \layout Subsubsection
15433 \begin_inset LatexCommand \index{malloc.h}
15440 Before using dynamic
15441 \begin_inset LatexCommand \index{dynamic}
15445 memory allocation with SDCC, you have to provide heap
15446 \begin_inset LatexCommand \index{heap}
15450 space for malloc to allocate memory from
15455 You can acomplish this by including the following code into your source:
15460 #include <malloc.h>
15461 \begin_inset LatexCommand \index{malloc.h}
15466 \begin_inset LatexCommand \index{calloc}
15471 \begin_inset LatexCommand \index{malloc}
15476 \begin_inset LatexCommand \index{realloc}
15481 \begin_inset LatexCommand \index{free}
15489 #define HEAPSIZE 0x1000 /* Adjust depending on available memory */
15491 unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory
15503 /* Your variable declarations come here*/
15513 init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE);
15519 /* Rest of your code*/
15526 Math functions (sinf, powf, sqrtf etc.)
15527 \layout Subsubsection
15532 See definitions in file <math.h>.
15539 \begin_inset LatexCommand \index{Libraries}
15543 included in SDCC should have a license at least as liberal as the GNU Lesser
15544 General Public License
15545 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15556 license statements for the libraries are missing.
15557 sdcc/device/lib/ser_ir.c
15561 come with a GPL (as opposed to LGPL) License - this will not be liberal
15562 enough for many embedded programmers.
15565 If you have ported some library or want to share experience about some code
15567 falls into any of these categories Busses (I
15568 \begin_inset Formula $^{\textrm{2}}$
15571 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15572 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15573 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15574 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15579 would certainly like to hear about it.
15580 Programmers coding for embedded systems are not especially famous for being
15581 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15582 e these references are very valuable.
15583 Let's help to create a climate where information is shared.
15589 MCS51 Memory Models
15590 \begin_inset LatexCommand \index{Memory model}
15595 \begin_inset LatexCommand \index{MCS51 memory model}
15600 \layout Subsubsection
15602 Small, Medium and Large
15605 SDCC allows three memory models for MCS51 code,
15614 Modules compiled with different memory models should
15618 be combined together or the results would be unpredictable.
15619 The library routines supplied with the compiler are compiled as small,
15621 The compiled library modules are contained in separate directories as small,
15622 medium and large so that you can link to the appropriate set.
15625 When the medium or large model is used all variables declared without a
15626 storage class will be allocated into the external ram, this includes all
15627 parameters and local variables (for non-reentrant
15628 \begin_inset LatexCommand \index{reentrant}
15633 When the small model is used variables without storage class are allocated
15634 in the internal ram.
15637 Judicious usage of the processor specific storage classes
15638 \begin_inset LatexCommand \index{Storage class}
15642 and the 'reentrant' function type will yield much more efficient code,
15643 than using the large model.
15644 Several optimizations are disabled when the program is compiled using the
15645 large model, it is therefore recommended that the small model be used unless
15646 absolutely required.
15647 \layout Subsubsection
15650 \begin_inset LatexCommand \label{sub:External-Stack}
15655 \begin_inset LatexCommand \index{stack}
15660 \begin_inset LatexCommand \index{External stack (mcs51)}
15667 The external stack (-
15678 \begin_inset LatexCommand \index{-\/-xstack}
15682 ) is located in pdata
15683 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15687 memory (usually at the start of the external ram segment) and uses all
15688 unused space in pdata (max.
15700 -xstack option is used to compile the program, the parameters and local
15702 \begin_inset LatexCommand \index{local variables}
15706 of all reentrant functions are allocated in this area.
15707 This option is provided for programs with large stack space requirements.
15708 When used with the -
15719 \begin_inset LatexCommand \index{-\/-stack-auto}
15723 option, all parameters and local variables are allocated on the external
15724 stack (note: support libraries will need to be recompiled with the same
15726 There is a predefined target in the library makefile).
15729 The compiler outputs the higher order address byte of the external ram segment
15731 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15736 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15740 ), therefore when using the External Stack option, this port
15744 be used by the application program.
15748 \begin_inset LatexCommand \index{Memory model}
15753 \begin_inset LatexCommand \index{DS390 memory model}
15760 The only model supported is Flat 24
15761 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15766 This generates code for the 24 bit contiguous addressing mode of the Dallas
15768 In this mode, up to four meg of external RAM or code space can be directly
15770 See the data sheets at www.dalsemi.com for further information on this part.
15774 Note that the compiler does not generate any code to place the processor
15775 into 24 bitmode (although
15779 in the ds390 libraries will do that for you).
15785 \begin_inset LatexCommand \index{Tinibios (DS390)}
15789 , the boot loader or similar code must ensure that the processor is in 24
15790 bit contiguous addressing mode before calling the SDCC startup code.
15808 option, variables will by default be placed into the XDATA segment.
15813 Segments may be placed anywhere in the 4 meg address space using the usual
15825 Note that if any segments are located above 64K, the -r flag must be passed
15826 to the linker to generate the proper segment relocations, and the Intel
15827 HEX output format must be used.
15828 The -r flag can be passed to the linker by using the option
15832 on the SDCC command line.
15833 However, currently the linker can not handle code segments > 64k.
15837 \begin_inset LatexCommand \index{Pragmas}
15844 SDCC supports the following #pragma directives:
15848 \begin_inset LatexCommand \index{\#pragma save}
15852 - this will save all current options to the save/restore stack.
15853 See #pragma\SpecialChar ~
15858 \begin_inset LatexCommand \index{\#pragma restore}
15862 - will restore saved options from the last save.
15863 saves & restores can be nested.
15864 SDCC uses a save/restore stack: save pushes current options to the stack,
15865 restore pulls current options from the stack.
15866 See #pragma\SpecialChar ~
15873 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15878 \begin_inset LatexCommand \index{function prologue}
15882 function1[,function2[,function3...]] - The compiler by default uses a caller
15883 saves convention for register saving across function calls, however this
15884 can cause unnecessary register pushing & popping
15885 \begin_inset LatexCommand \index{push/pop}
15889 when calling small functions from larger functions.
15890 This option can be used to switch off the register saving convention for
15891 the function names specified.
15892 The compiler will not save registers when calling these functions, extra
15893 code need to be manually inserted at the entry & exit for these functions
15894 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15895 reduce code & improve run time performance of the generated code.
15896 In the future the compiler (with inter procedural analysis) may be able
15897 to determine the appropriate scheme to use for each function call.
15908 -callee-saves command line option is used, the function names specified
15909 in #pragma\SpecialChar ~
15911 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15915 is appended to the list of functions specified in the command line.
15919 \begin_inset LatexCommand \index{\#pragma exclude}
15923 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15924 of pairs of push/pop
15925 \begin_inset LatexCommand \index{push/pop}
15934 \begin_inset LatexCommand \index{interrupt}
15947 The directive should be placed immediately before the ISR function definition
15948 and it affects ALL ISR functions following it.
15949 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15950 exclude\SpecialChar ~
15952 \begin_inset LatexCommand \index{\#pragma exclude}
15957 See also the related keyword _naked
15958 \begin_inset LatexCommand \index{\_naked}
15963 \begin_inset LatexCommand \index{\_\_naked}
15971 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15975 - the compiler will not warn you anymore for obvious mistakes, you'r on
15979 disable_warning <nnnn>
15980 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15984 - the compiler will not warn you anymore about warning number <nnnn>.
15988 \begin_inset LatexCommand \index{\#pragma nogcse}
15992 - will stop global common subexpression elimination.
15996 \begin_inset LatexCommand \index{\#pragma noinduction}
16000 - will stop loop induction optimizations.
16004 \begin_inset LatexCommand \index{\#pragma noinvariant}
16008 - will not do loop invariant optimizations.
16009 For more details see Loop Invariants in section
16010 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
16018 \begin_inset LatexCommand \index{\#pragma noiv}
16022 - Do not generate interrupt
16023 \begin_inset LatexCommand \index{interrupt}
16027 vector table entries for all ISR functions defined after the pragma.
16028 This is useful in cases where the interrupt vector table must be defined
16029 manually, or when there is a secondary, manually defined interrupt vector
16031 for the autovector feature of the Cypress EZ-USB FX2).
16032 More elegantly this can be achieved by obmitting the optional interrupt
16033 number after the interrupt keyword, see section
16034 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
16043 \begin_inset LatexCommand \index{\#pragma nojtbound}
16047 - will not generate code for boundary value checking, when switch statements
16048 are turned into jump-tables (dangerous).
16049 For more details see section
16050 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16058 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16062 - Will not do loop reversal optimization
16066 \begin_inset LatexCommand \index{\#pragma nooverlay}
16070 - the compiler will not overlay the parameters and local variables of a
16075 \begin_inset LatexCommand \index{\#pragma stackauto}
16090 \begin_inset LatexCommand \index{-\/-stack-auto}
16095 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16099 Parameters and Local Variables.
16103 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16107 - The compiler will optimize code generation towards fast code, possibly
16108 at the expense of code size.
16112 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16116 - The compiler will optimize code generation towards compact code, possibly
16117 at the expense of code speed.
16121 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16125 - The compiler will attempt to generate code that is both compact and fast,
16126 as long as meeting one goal is not a detriment to the other (this is the
16132 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16136 - Generally follow the C89 standard, but allow SDCC features that conflict
16137 with the standard (default).
16141 \begin_inset LatexCommand \index{\#pragma std\_c89}
16145 - Follow the C89 standard and disable SDCC features that conflict with the
16150 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16154 - Generally follow the C99 standard, but allow SDCC features that conflict
16155 with the standard (incomplete support).
16159 \begin_inset LatexCommand \index{\#pragma std\_c99}
16163 - Follow the C99 standard and disable SDCC features that conflict with the
16164 standard (incomplete support).
16168 \begin_inset LatexCommand \index{\#pragma codeseg}
16172 - Use this name (max.
16173 8 characters) for the code segment.
16188 \begin_inset LatexCommand \index{\#pragma constseg}
16192 - Use this name (max.
16193 8 characters) for the const segment.
16207 SDCPP supports the following #pragma directives:
16211 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16215 (+ | -) - switch _asm _endasm block preprocessing on / off.
16217 You use this prama to define multilines of assembly code.
16218 This will prevent the preprocessor from changing the formating required
16220 Below is an example on how to use this pragma.
16225 #pragma preproc_asm -
16226 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16232 #define MYDELAY _asm
16237 nop ;my assembly comment...
16251 #pragma preproc_asm +
16281 The pragma's are intended to be used to turn-on or off certain optimizations
16282 which might cause the compiler to generate extra stack / data space to
16283 store compiler generated temporary variables.
16284 This usually happens in large functions.
16285 Pragma directives should be used as shown in the following example, they
16286 are used to control options & optimizations for a given function; pragmas
16287 should be placed before and/or after a function, placing pragma's inside
16288 a function body could have unpredictable results.
16294 \begin_inset LatexCommand \index{\#pragma save}
16305 /* save the current settings */
16308 \begin_inset LatexCommand \index{\#pragma nogcse}
16317 /* turnoff global subexpression elimination */
16319 #pragma noinduction
16320 \begin_inset LatexCommand \index{\#pragma noinduction}
16324 /* turn off induction optimizations */
16347 \begin_inset LatexCommand \index{\#pragma restore}
16351 /* turn the optimizations back on */
16354 The compiler will generate a warning message when extra space is allocated.
16355 It is strongly recommended that the save and restore pragma's be used when
16356 changing options for a function.
16365 Defines Created by the Compiler
16368 The compiler creates the following #defines
16369 \begin_inset LatexCommand \index{\#defines}
16374 \begin_inset LatexCommand \index{Defines created by the compiler}
16384 \begin_inset Tabular
16385 <lyxtabular version="3" rows="11" columns="2">
16387 <column alignment="left" valignment="top" leftline="true" width="3in">
16388 <column alignment="left" valignment="top" leftline="true" rightline="true" width="3in">
16389 <row topline="true" bottomline="true">
16390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16411 <row topline="true">
16412 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16418 \begin_inset LatexCommand \index{SDCC}
16425 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16431 Since version 2.5.6 the version number as an int (ex.
16436 <row topline="true">
16437 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16443 \begin_inset LatexCommand \index{SDCC\_mcs51}
16448 \begin_inset LatexCommand \index{SDCC\_ds390}
16453 \begin_inset LatexCommand \index{SDCC\_z80}
16460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16465 depending on the model used (e.g.: -mds390)
16469 <row topline="true">
16470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16476 \begin_inset LatexCommand \index{\_\_mcs51}
16481 \begin_inset LatexCommand \index{\_\_ds390}
16486 \begin_inset LatexCommand \index{\_\_hc08}
16491 \begin_inset LatexCommand \index{\_\_z80}
16498 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16503 depending on the model used (e.g.
16508 <row topline="true">
16509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16515 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16545 <row topline="true">
16546 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16552 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16559 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16582 <row topline="true">
16583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16589 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16596 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16619 <row topline="true">
16620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16626 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16633 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16656 <row topline="true">
16657 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16663 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16670 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16693 <row topline="true">
16694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16700 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16720 <row topline="true" bottomline="true">
16721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16727 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16734 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16754 Notes on supported Processors
16758 \begin_inset LatexCommand \label{sub:MCS51-variants}
16763 \begin_inset LatexCommand \index{MCS51 variants}
16770 MCS51 processors are available from many vendors and come in many different
16772 While they might differ considerably in respect to Special Function Registers
16773 the core MCS51 is usually not modified or is kept compatible.
16777 pdata access by SFR
16780 With the upcome of devices with internal xdata and flash memory devices
16782 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16786 as dedicated I/O port is becoming more popular.
16787 Switching the high byte for pdata
16788 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16792 access which was formerly done by port P2 is then achieved by a Special
16794 \begin_inset LatexCommand \index{sfr}
16799 In well-established MCS51 tradition the address of this
16803 is where the chip designers decided to put it.
16804 Needless to say that they didn't agree on a common name either.
16805 So that the startup code can correctly initialize xdata variables, you
16806 should define an sfr with the name _XPAGE
16809 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16815 at the appropriate location if the default, port P2, is not used for this.
16821 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16826 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16831 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16834 For more exotic implementations further customizations may be needed.
16836 \begin_inset LatexCommand \ref{sub:Startup-Code}
16840 for other possibilities.
16843 Other Features available by SFR
16846 Some MCS51 variants offer features like Double DPTR
16847 \begin_inset LatexCommand \index{DPTR}
16851 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16852 These are currently not used for the MCS51 port.
16853 If you absolutely need them you can fall back to inline assembly or submit
16860 The DS80C400 microcontroller has a rich set of peripherals.
16861 In its built-in ROM library it includes functions to access some of the
16862 features, among them is a TCP stack with IP4 and IP6 support.
16863 Library headers (currently in beta status) and other files are provided
16867 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16875 The Z80 and gbz80 port
16878 SDCC can target both the Zilog
16879 \begin_inset LatexCommand \index{Z80}
16883 and the Nintendo Gameboy's Z80-like gbz80
16884 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16889 The Z80 port is passed through the same
16892 \begin_inset LatexCommand \index{Regression test}
16898 as the MCS51 and DS390 ports, so floating point support, support for long
16899 variables and bitfield support is fine.
16900 See mailing lists and forums about interrupt routines.
16903 As always, the code is the authoritative reference - see z80/ralloc.c and
16906 \begin_inset LatexCommand \index{stack}
16910 frame is similar to that generated by the IAR Z80 compiler.
16911 IX is used as the base pointer, HL and IY are used as a temporary registers,
16912 and BC and DE are available for holding variables.
16914 \begin_inset LatexCommand \index{return value}
16918 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16920 The gbz80 port use the same set of registers for the return values, but
16921 in a different order of significance: E (one byte), DE (two bytes), or
16928 The port to the Motorola HC08
16929 \begin_inset LatexCommand \index{HC08}
16933 family has been added in October 2003, and is still undergoing some basic
16935 The code generator is complete, but the register allocation is still quite
16937 Some of the SDCC's standard C library functions have embedded non-HC08
16938 inline assembly and so are not yet usable.
16949 \begin_inset LatexCommand \index{PIC14}
16953 port still requires a major effort from the development community.
16954 However it can work for very simple code.
16957 C code and 14bit PIC code page
16958 \begin_inset LatexCommand \index{code page (pic14)}
16963 \begin_inset LatexCommand \index{RAM bank (pic14)}
16970 The linker organizes allocation for the code page and RAM banks.
16971 It does not have intimate knowledge of the code flow.
16972 It will put all the code section of a single asm file into a single code
16974 In order to make use of multiple code pages, separate asm files must be
16976 The compiler treats all functions of a single C file as being in the same
16977 code page unless it is non static.
16978 The compiler treats all local variables of a single C file as being in
16979 the same RAM bank unless it is an extern.
16983 To get the best follow these guide lines:
16986 make local functions static, as non static functions require code page selection
16990 Make local variables static as extern variables require RAM bank selection
16994 For devices that have multiple code pages it is more efficient to use the
16995 same number of files as pages, i.e.
16996 for the 16F877 use 4 separate files and i.e.
16997 for the 16F874 use 2 separate files.
16998 This way the linker can put the code for each file into different code
16999 pages and the compiler can allocate reusable variables more efficiently
17000 and there's less page selection overhead.
17001 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
17002 instruction set) use 'unsigned char' whereever possible instead of 'int'.
17005 Creating a device include file
17008 For generating a device include file use the support perl script inc2h.pl
17009 kept in directory support/script.
17015 For the interrupt function, use the keyword 'interrupt'
17016 \begin_inset LatexCommand \index{interrupt}
17020 with level number of 0 (PIC14 only has 1 interrupt so this number is only
17021 there to avoid a syntax error - it ought to be fixed).
17027 void Intr(void) interrupt 0
17033 T0IF = 0; /* Clear timer interrupt */
17038 Linking and assembling
17041 For assembling you can use either GPUTILS'
17042 \begin_inset LatexCommand \index{gputils (pic tools)}
17046 gpasm.exe or MPLAB's mpasmwin.exe.
17047 GPUTILS is available from
17048 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17053 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17054 If you use MPLAB and an interrupt function then the linker script file
17055 vectors section will need to be enlarged to link with mplink.
17078 sdcc -S -V -mpic14 -p16F877 $<
17092 $(PRJ).hex: $(OBJS)
17102 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17124 sdcc -S -V -mpic14 -p16F877 $<
17134 mpasmwin /q /o $*.asm
17138 $(PRJ).hex: $(OBJS)
17148 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17151 Please note that indentations within a
17155 have to be done with a tabulator character.
17158 Command-line options
17161 Besides the switches common to all SDCC backends, the PIC14 port accepts
17162 the following options (for an updated list see sdcc -
17174 \labelwidthstring 00.00.0000
17186 -debug-extra emit debug info in assembly output
17188 \labelwidthstring 00.00.0000
17200 -no-pcode-opt disable (slightly faulty) optimization on pCode
17204 \layout Subsubsection
17206 error: missing definition for symbol
17207 \begin_inset Quotes sld
17211 \begin_inset Quotes srd
17217 The PIC14 port uses library routines to provide more complex operations
17218 like multiplication, division/modulus and (generic) pointer dereferencing.
17219 In order to add these routines to your project, you must link with PIC14's
17225 For single source file projects this is done automatically, more complex
17230 to the linker's arguments.
17231 Make sure you also add an include path for the library (using the -I switch
17233 \layout Subsubsection
17235 Processor mismatch in file
17236 \begin_inset Quotes sld
17240 \begin_inset Quotes srd
17246 This warning can usually be ignored due to the very good compatibility amongst
17247 14 bit PIC devices.
17250 You might also consider recompiling the library for your specific device
17251 by changing the ARCH=p16f877 (default target) entry in
17253 device/lib/pic/Makefile.in
17257 device/lib/pic/Makefile
17259 to reflect your device.
17260 This might even improve performance for smaller devices as unneccesary
17261 BANKSELs migth be removed.
17265 \layout Subsubsection
17270 Currently, data can only be initialized if it resides in the source file
17276 Data in other source files will silently
17284 \begin_inset LatexCommand \index{PIC16}
17292 \begin_inset LatexCommand \index{PIC16}
17296 port is the portion of SDCC that is responsible to produce code for the
17298 \begin_inset LatexCommand \index{Microchip}
17302 (TM) microcontrollers with 16 bit core.
17303 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17304 Currently supported devices are:
17308 \begin_inset Tabular
17309 <lyxtabular version="3" rows="4" columns="6">
17311 <column alignment="center" valignment="top" leftline="true" width="0">
17312 <column alignment="center" valignment="top" leftline="true" width="0">
17313 <column alignment="center" valignment="top" leftline="true" width="0">
17314 <column alignment="center" valignment="top" leftline="true" width="0">
17315 <column alignment="center" valignment="top" leftline="true" width="0">
17316 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17317 <row topline="true">
17318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17326 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17358 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17367 <row topline="true">
17368 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17392 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17400 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17408 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17417 <row topline="true">
17418 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17458 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17467 <row topline="true" bottomline="true">
17468 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17476 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17484 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17524 PIC16 port supports the standard command line arguments as supposed, with
17525 the exception of certain cases that will be mentioned in the following
17528 \labelwidthstring 00.00.0000
17540 -callee-saves See -
17552 \labelwidthstring 00.00.0000
17564 -all-callee-saves All function arguments are passed on stack by default.
17567 There is no need to specify this in the command line.
17569 \labelwidthstring 00.00.0000
17581 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17582 no local variables.
17585 Port Specific Options
17586 \begin_inset LatexCommand \index{Options PIC16}
17593 The port specific options appear after the global options in the sdcc --help
17595 \layout Subsubsection
17600 General options enable certain port features and optimizations.
17602 \labelwidthstring 00.00.0000
17614 -stack-model=[model] Used in conjuction with the command above.
17615 Defines the stack model to be used, valid stack models are :
17618 \labelwidthstring 00.00.0000
17624 Selects small stack model.
17625 8 bit stack and frame pointers.
17626 Supports 256 bytes stack size.
17628 \labelwidthstring 00.00.0000
17634 Selects large stack model.
17635 16 bit stack and frame pointers.
17636 Supports 65536 bytes stack size.
17639 \labelwidthstring 00.00.0000
17651 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17652 unitialized data variables with [kword].
17653 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17655 \labelwidthstring 00.00.0000
17667 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17668 Useful for bootloaders.
17670 \labelwidthstring 00.00.0000
17682 -asm= sets the full path and name of an external assembler to call.
17684 \labelwidthstring 00.00.0000
17696 -link= sets the full path and name of an external linker to call.
17698 \labelwidthstring 00.00.0000
17710 -mplab-comp MPLAB compatibility option.
17711 Currently only suppresses special gpasm directives.
17712 \layout Subsubsection
17714 Optimization Options
17716 \labelwidthstring 00.00.0000
17728 -optimize-goto Try to use (conditional) BRA instead of GOTO
17730 \labelwidthstring 00.00.0000
17742 -optimize-cmp Try to optimize some compares.
17744 \labelwidthstring 00.00.0000
17756 -optimize-df Analyze the dataflow of the generated code and improve it.
17758 \labelwidthstring 00.00.0000
17770 -obanksel=nn Set optimization level for inserting BANKSELs.
17775 \labelwidthstring 00.00.0000
17779 \labelwidthstring 00.00.0000
17781 1 checks previous used register and if it is the same then does not emit
17782 BANKSEL, accounts only for labels.
17784 \labelwidthstring 00.00.0000
17786 2 tries to check the location of (even different) symbols and removes BANKSELs
17787 if they are in the same bank.
17792 Important: There might be problems if the linker script has data sections
17793 across bank borders!
17795 \layout Subsubsection
17799 \labelwidthstring 00.00.0000
17811 -nodefaultlibs do not link default libraries when linking
17813 \labelwidthstring 00.00.0000
17825 -no-crt Don't link the default run-time modules
17827 \labelwidthstring 00.00.0000
17839 -use-crt= Use a custom run-time module instead of the defaults.
17840 \layout Subsubsection
17845 Debugging options enable extra debugging information in the output files.
17847 \labelwidthstring 00.00.0000
17859 -debug-xtra Similar to -
17870 \begin_inset LatexCommand \index{-\/-debug}
17874 , but dumps more information.
17876 \labelwidthstring 00.00.0000
17888 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17890 <source> is the name of the file compiled.
17892 \labelwidthstring 00.00.0000
17904 -pcode-verbose Enable pcode debugging information in translation.
17906 \labelwidthstring 00.00.0000
17918 -denable-peeps Force the usage of peepholes.
17921 \labelwidthstring 00.00.0000
17933 -gstack Trace push/pops for stack pointer overflow
17935 \labelwidthstring 00.00.0000
17947 -call-tree dump call tree in .calltree file
17950 Enviromental Variables
17953 There is a number of enviromental variables that can be used when running
17954 SDCC to enable certain optimizations or force a specific program behaviour.
17955 these variables are primarily for debugging purposes so they can be enabled/dis
17959 Currently there is only two such variables available:
17961 \labelwidthstring 00.00.0000
17963 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17964 bitfields is optimized by directly loading FSR0 with the address of the
17965 bitfield structure.
17966 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17968 This step saves data ram and code space for functions that perform heavy
17971 80 bytes of code space are saved when compiling malloc.c with this option).
17974 \labelwidthstring 00.00.0000
17976 NO_REG_OPT do not perform pCode registers optimization.
17977 This should be used for debugging purposes.
17978 In some where bugs in the pcode optimizer are found, users can benefit
17979 from temporarily disabling the optimizer until the bug is fixed.
17982 Preprocessor Macros
17985 PIC16 port defines the following preprocessor macros while translating a
17990 \begin_inset Tabular
17991 <lyxtabular version="3" rows="6" columns="2">
17993 <column alignment="center" valignment="top" leftline="true" width="0">
17994 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17995 <row topline="true" bottomline="true">
17996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18013 <row topline="true">
18014 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18027 Port identification
18031 <row topline="true">
18032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18050 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18055 Port identification (same as above)
18059 <row topline="true">
18060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18073 MCU Identification.
18078 is the microcontrol identification number, i.e.
18083 <row topline="true">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18102 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18107 MCU Identification (same as above)
18111 <row topline="true" bottomline="true">
18112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18120 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18125 nnn = SMALL or LARGE respectively according to the stack model used
18136 In addition the following macros are defined when calling assembler:
18140 \begin_inset Tabular
18141 <lyxtabular version="3" rows="4" columns="2">
18143 <column alignment="center" valignment="top" leftline="true" width="0">
18144 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18145 <row topline="true" bottomline="true">
18146 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18154 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18163 <row topline="true">
18164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18172 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18177 MCU Identification.
18182 is the microcontrol identification number, i.e.
18187 <row topline="true">
18188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18201 nnn = SMALL or LARGE respectively according to the memory model used for
18206 <row topline="true" bottomline="true">
18207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18220 nnn = SMALL or LARGE respectively according to the stack model used
18235 \begin_inset LatexCommand \index{PIC16}
18239 port uses the following directories for searching header files and libraries.
18243 \begin_inset Tabular
18244 <lyxtabular version="3" rows="3" columns="4">
18246 <column alignment="center" valignment="top" leftline="true" width="0">
18247 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18248 <column alignment="center" valignment="top" width="0">
18249 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18250 <row topline="true" bottomline="true">
18251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18259 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18267 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18275 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18284 <row topline="true">
18285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18290 PREFIX/sdcc/include/pic16
18293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18298 PIC16 specific headers
18301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18318 <row topline="true" bottomline="true">
18319 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18324 PREFIX/sdcc/lib/pic16
18327 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18332 PIC16 specific libraries
18335 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18360 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18367 PIC16 port currently supports the following pragmas:
18369 \labelwidthstring 00.00.0000
18371 stack pragma stack forces the code generator to initialize the stack & frame
18372 pointers at a specific address.
18373 This is an adhoc solution for cases where no STACK directive is available
18374 in the linker script or gplink is not instructed to create a stack section.
18376 The stack pragma should be used only once in a project.
18377 Multiple pragmas may result in indeterminate behaviour of the program.
18383 The old format (ie.
18384 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18385 cross page boundaries (or even exceed the available data RAM) and crash
18387 Make sure that stack does not cross page boundaries when using the SMALL
18393 The format is as follows:
18396 #pragma stack bottom_address [stack_size]
18403 is the lower bound of the stack section.
18404 The stack pointer initially will point at address (bottom_address+stack_size-1).
18412 /* initializes stack of 100 bytes at RAM address 0x200 */
18415 #pragma stack 0x200 100
18418 If the stack_size field is omitted then a stack is created with the default
18420 This size might be enough for most programs, but its not enough for operations
18421 with deep function nesting or excessive stack usage.
18423 \labelwidthstring 00.00.0000
18427 This pragma is deprecated.
18428 Its use will cause a warning message to be issued.
18434 \labelwidthstring 00.00.0000
18436 code place a function symbol at static FLASH address
18444 /* place function test_func at 0x4000 */
18447 #pragma code test_func 0x4000
18451 \labelwidthstring 00.00.0000
18453 library instructs the linker to use a library module.
18458 #pragma library module_name
18465 can be any library or object file (including its path).
18466 Note that there are four reserved keywords which have special meaning.
18471 \begin_inset Tabular
18472 <lyxtabular version="3" rows="6" columns="3">
18474 <column alignment="center" valignment="top" leftline="true" width="0">
18475 <column alignment="block" valignment="top" leftline="true" width="20page%">
18476 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18477 <row topline="true" bottomline="true">
18478 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18494 <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">
18519 ignore all library pragmas
18522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18533 <row topline="true">
18534 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18544 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18552 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18565 <row topline="true">
18566 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18581 link the Math libarary
18584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18597 <row topline="true">
18598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18613 link the I/O library
18616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18629 <row topline="true" bottomline="true">
18630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18645 link the debug library
18648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18667 * is the device number, i.e.
18668 452 for PIC18F452 MCU.
18671 This feature allows for linking with specific libraries withoug having to
18672 explicit name them in the command line.
18677 keyword will reject all modules specified by the library pragma.
18679 \labelwidthstring 00.00.0000
18681 udata pragma udata instructs the compiler to emit code so that linker will
18682 place a variable at a specific memory bank
18690 /* places variable foo at bank2 */
18693 #pragma udata bank2 foo
18699 In order for this pragma to work extra SECTION directives should be added
18700 in the .lkr script.
18701 In the following example a sample .lkr file is shown:
18706 // Sample linker script for the PIC18F452 processor
18712 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18715 CODEPAGE NAME=page START=0x2A END=0x7FFF
18718 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18721 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18724 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18727 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18730 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18735 DATABANK NAME=gpr0 START=0x80 END=0xFF
18738 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18741 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18744 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18747 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18750 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18753 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18758 SECTION NAME=CONFIG ROM=config
18763 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18766 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18769 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18772 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18775 SECTION NAME=bank4 RAM=gpr4
18778 SECTION NAME=bank5 RAM=gpr5
18781 The linker will recognise the section name set in the pragma statement and
18782 will position the variable at the memory bank set with the RAM field at
18783 the SECTION line in the linker script file.
18787 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18794 There is one main header file that can be included to the source files using
18801 This header file contains the definitions for the processor special registers,
18802 so it is necessary if the source accesses them.
18803 It can be included by adding the following line in the beginning of the
18807 #include <pic18fregs.h>
18810 The specific microcontroller is selected within the pic18fregs.h automatically,
18811 so the same source can be used with a variety of devices.
18817 The libraries that PIC16
18818 \begin_inset LatexCommand \index{PIC16}
18822 port depends on are the microcontroller device libraries which contain
18823 the symbol definitions for the microcontroller special function registers.
18824 These libraries have the format pic18fxxxx.lib, where
18828 is the microcontroller identification number.
18829 The specific library is selected automatically by the compiler at link
18830 stage according to the selected device.
18833 Libraries are created with gplib which is part of the gputils package
18834 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18839 \layout Subsubsection*
18841 Building the libraries
18844 Before using SDCC/pic16 there are some libraries that need to be compiled.
18845 This process is not done automatically by SDCC since not all users use
18846 SDCC for pic16 projects.
18847 So each user should compile the libraries separately.
18850 The steps to compile the pic16 libraries under Linux are:
18853 cd device/lib/pic16
18868 su -c 'make install' # install the libraries, you need the root password
18871 If you need to install the headers too, do:
18877 su -c 'make install' # install the headers, you need the root password
18880 There exist a special target to build the I/O libraries.
18881 This target is not automatically build because it will build the I/O library
18887 This way building will take quite a lot of time.
18888 Users are advised to edit the
18890 device/lib/pic16/pics.build
18892 file and then execute:
18901 The following memory models are supported by the PIC16 port:
18910 Memory model affects the default size of pointers within the source.
18911 The sizes are shown in the next table:
18915 \begin_inset Tabular
18916 <lyxtabular version="3" rows="3" columns="3">
18918 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18919 <column alignment="center" valignment="top" leftline="true" width="0">
18920 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18921 <row topline="true" bottomline="true">
18922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18927 Pointer sizes according to memory model
18930 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18938 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18947 <row topline="true" bottomline="true">
18948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18956 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18964 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18973 <row topline="true" bottomline="true">
18974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18982 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18990 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19006 It is advisable that all sources within a project are compiled with the
19008 If one wants to override the default memory model, this can be done by
19009 declaring a pointer as
19018 Far selects large memory model's pointers, while near selects small memory
19022 The standard device libraries (see
19023 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
19027 ) contain no reference to pointers, so they can be used with both memory
19034 The stack implementation for the PIC16 port uses two indirect registers,
19037 \labelwidthstring 00.00.0000
19039 FSR1 is assigned as stack pointer
19041 \labelwidthstring 00.00.0000
19043 FSR2 is assigned as frame pointer
19046 The following stack models are supported by the PIC16 port
19067 model means that only the FSRxL byte is used to access stack and frame,
19074 uses both FSRxL and FSRxH registers.
19075 The following table shows the stack/frame pointers sizes according to stack
19076 model and the maximum space they can address:
19080 \begin_inset Tabular
19081 <lyxtabular version="3" rows="3" columns="3">
19083 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19084 <column alignment="center" valignment="top" leftline="true" width="0">
19085 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19086 <row topline="true" bottomline="true">
19087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19092 Stack & Frame pointer sizes according to stack model
19095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19112 <row topline="true">
19113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19129 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19138 <row topline="true" bottomline="true">
19139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19175 stack model is currently not working properly throughout the code generator.
19176 So its use is not advised.
19177 Also there are some other points that need special care:
19182 Do not create stack sections with size more than one physical bank (that
19186 Stack sections should no cross physical bank limits (i.e.
19187 #pragma stack 0x50 0x100)
19190 These limitations are caused by the fact that only FSRxL is modified when
19191 using SMALL stack model, so no more than 256 bytes of stack can be used.
19192 This problem will disappear after LARGE model is fully implemented.
19198 In addition to the standard SDCC function keywords, PIC16 port makes available
19201 \labelwidthstring 00.00.0000
19203 wparam Use the WREG to pass one byte of the first function argument.
19204 This improves speed but you may not use this for functions with arguments
19205 that are called via function pointers, otherwise the first byte of the
19206 first parameter will get lost.
19210 void func_wparam(int a) wparam
19216 /* WREG hold the lower part of a */
19219 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19229 This keyword replaces the deprecated wparam pragma.
19231 \labelwidthstring 00.00.0000
19233 shadowregs When entering/exiting an ISR, it is possible to take advantage
19234 of the PIC18F hardware shadow registers which hold the values of WREG,
19235 STATUS and BSR registers.
19236 This can be done by adding the keyword
19244 keyword in the function's header.
19247 void isr_shadow(void) shadowregs interrupt 1
19263 instructs the code generator not to store/restore WREG, STATUS, BSR when
19264 entering/exiting the ISR.
19267 Function return values
19270 Return values from functions are placed to the appropriate registers following
19271 a modified Microchip policy optimized for SDCC.
19272 The following table shows these registers:
19276 \begin_inset Tabular
19277 <lyxtabular version="3" rows="6" columns="2">
19279 <column alignment="center" valignment="top" leftline="true" width="0">
19280 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19281 <row topline="true" bottomline="true">
19282 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19290 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19295 destination register
19299 <row topline="true">
19300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19308 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19317 <row topline="true">
19318 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19326 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19335 <row topline="true">
19336 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19344 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19353 <row topline="true">
19354 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19362 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19367 FSR0L:PRODH:PRODL:WREG
19371 <row topline="true" bottomline="true">
19372 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19380 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19385 on stack, FSR0 points to the beginning
19399 An interrupt servive routine (ISR) is declared using the
19406 void isr(void) interrupt
19424 is the interrupt number, which for PIC18F devices can be:
19428 \begin_inset Tabular
19429 <lyxtabular version="3" rows="4" columns="3">
19431 <column alignment="center" valignment="top" leftline="true" width="0">
19432 <column alignment="center" valignment="top" leftline="true" width="0">
19433 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19434 <row topline="true" bottomline="true">
19435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19445 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19453 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19458 Interrupt Vector Address
19462 <row topline="true">
19463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19488 <row topline="true">
19489 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19520 HIGH priority interrupts
19523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19532 <row topline="true" bottomline="true">
19533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19546 LOW priority interrupts
19549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19565 When generating assembly code for ISR the code generator places a
19571 Interrupt Vector Address
19573 which points at the genetated ISR.
19574 This single GOTO instruction is part of an automatically generated
19576 interrupt entry point
19579 The actuall ISR code is placed as normally would in the code space.
19580 Upon interrupt request, the GOTO instruction is executed which jumps to
19582 When declaring interrupt functions as _naked this GOTO instruction is
19587 The whole interrupt functions is therefore placed at the Interrupt Vector
19588 Address of the specific interrupt.
19589 This is not a problem for the LOW priority interrupts, but it is a problem
19590 for the RESET and the HIGH priority interrupts because code may be written
19591 at the next interrupt´s vector address and cause undeterminate program
19592 behaviour if that interrupt is raised.
19598 This is not a problem when
19601 this is a HIGH interrupt ISR and LOW interrupts are
19608 when the ISR is small enough not to reach the next interrupt´s vector address.
19618 is possible to be omitted.
19619 This way a function is generated similar to an ISR, but it is not assigned
19623 When entering an interrupt, currently the PIC16
19624 \begin_inset LatexCommand \index{PIC16}
19628 port automatically saves the following registers:
19640 PROD (PRODL and PRODH)
19643 FSR0 (FSR0L and FSR0H)
19646 These registers are restored upon return from the interrupt routine.
19652 NOTE that when the _naked attribute is specified for an interrupt routine,
19653 then NO registers are stored or restored.
19662 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19663 There are 3 types of generic pointers currently implemented data, code
19664 and eeprom pointers.
19665 They are differentiated by the value of the 7th and 6th bits of the upper
19670 \begin_inset Tabular
19671 <lyxtabular version="3" rows="5" columns="5">
19673 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19674 <column alignment="center" valignment="top" width="0">
19675 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19676 <column alignment="center" valignment="top" width="0">
19677 <column alignment="left" valignment="top" rightline="true" width="0">
19678 <row topline="true" bottomline="true">
19679 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19687 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19695 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19703 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19708 rest of the pointer
19711 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19720 <row topline="true" bottomline="true">
19721 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19729 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19737 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19745 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19754 uuuuuu uuuuxxxx xxxxxxxx
19757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19762 a 12-bit data pointer in data RAM memory
19766 <row bottomline="true">
19767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19791 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19800 uxxxxx xxxxxxxx xxxxxxxx
19803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19808 a 21-bit code pointer in FLASH memory
19812 <row bottomline="true">
19813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19837 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19846 uuuuuu uuuuuuxx xxxxxxxx
19849 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19854 a 10-bit eeprom pointer in EEPROM memory
19858 <row bottomline="true">
19859 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19867 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19875 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19883 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19892 xxxxxx xxxxxxxx xxxxxxxx
19895 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19900 unimplemented pointer type
19911 Generic pointer are read and written with a set of library functions which
19912 read/write 1, 2, 3, 4 bytes.
19916 \layout Subsubsection
19918 Standard I/O Streams
19925 the type FILE is defined as:
19928 typedef char * FILE;
19931 This type is the stream type implemented I/O in the PIC18F devices.
19932 Also the standard input and output streams are declared in stdio.h:
19935 extern FILE * stdin;
19938 extern FILE * stdout;
19941 The FILE type is actually a generic pointer which defines one more type
19942 of generic pointers, the
19947 This new type has the format:
19951 \begin_inset Tabular
19952 <lyxtabular version="3" rows="2" columns="7">
19954 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19955 <column alignment="center" valignment="top" width="0">
19956 <column alignment="center" valignment="top" leftline="true" width="0">
19957 <column alignment="center" valignment="top" leftline="true" width="0">
19958 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19959 <column alignment="center" valignment="top" width="0">
19960 <column alignment="left" valignment="top" rightline="true" width="0">
19961 <row topline="true" bottomline="true">
19962 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19978 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20007 rest of the pointer
20010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20019 <row topline="true" bottomline="true">
20020 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20036 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20044 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20077 upper byte high nubble is 0x2n, the rest are zeroes
20088 Currently implemented there are 3 types of streams defined:
20092 \begin_inset Tabular
20093 <lyxtabular version="3" rows="4" columns="4">
20095 <column alignment="center" valignment="top" leftline="true" width="0">
20096 <column alignment="center" valignment="top" leftline="true" width="0">
20097 <column alignment="center" valignment="top" leftline="true" width="0">
20098 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20099 <row topline="true" bottomline="true">
20100 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20116 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20124 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20133 <row topline="true">
20134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20142 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20152 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20165 Writes/Reads characters via the USART peripheral
20169 <row topline="true">
20170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20178 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20201 Writes/Reads characters via the MSSP peripheral
20205 <row topline="true" bottomline="true">
20206 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20214 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20237 Writes/Reads characters via used defined functions
20248 The stream identifiers are declared as macros in the stdio.h header.
20251 In the libc library there exist the functions that are used to write to
20252 each of the above streams.
20255 \labelwidthstring 00.00.0000
20267 _stream_usart_putchar writes a character at the USART stream
20269 \labelwidthstring 00.00.0000
20281 _stream_mssp_putchar writes a character at the MSSP stream
20283 \labelwidthstring 00.00.0000
20285 putchar dummy function.
20286 This writes a character to a user specified manner.
20289 In order to increase performance
20293 is declared in stdio.h as having its parameter in WREG (it has the wparam
20295 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20296 in a user-friendly way.
20301 is the name of the variable that holds the character to print.
20302 An example follows:
20305 #include <pic18fregs.h>
20317 PORTA = c; /* dump character c to PORTA */
20330 stdout = STREAM_USER; /* this is not necessary, since stdout points
20333 * by default to STREAM_USER */
20336 printf (¨This is a printf test
20344 \layout Subsubsection
20349 PIC16 contains an implementation of the printf-family of functions.
20350 There exist the following functions:
20353 extern unsigned int sprintf(char *buf, char *fmt, ...);
20356 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20361 extern unsigned int printf(char *fmt, ...);
20364 extern unsigned int vprintf(char *fmt, va_lista ap);
20369 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20372 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20375 For sprintf and vsprintf
20379 should normally be a data pointer where the resulting string will be placed.
20380 No range checking is done so the user should allocate the necessery buffer.
20381 For fprintf and vfprintf
20385 should be a stream pointer (i.e.
20386 stdout, STREAM_MSSP, etc...).
20387 \layout Subsubsection
20392 The PIC18F family of microcontrollers supports a number of interrupt sources.
20393 A list of these interrupts is shown in the following table:
20397 \begin_inset Tabular
20398 <lyxtabular version="3" rows="11" columns="4">
20400 <column alignment="left" valignment="top" leftline="true" width="0">
20401 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20402 <column alignment="left" valignment="top" leftline="true" width="0">
20403 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20404 <row topline="true" bottomline="true">
20405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20413 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20421 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20429 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20438 <row topline="true">
20439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20447 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20452 PORTB change interrupt
20455 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20468 EEPROM/FLASH write complete interrupt
20472 <row topline="true">
20473 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20486 INT0 external interrupt
20489 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20497 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20502 Bus collision interrupt
20506 <row topline="true">
20507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20520 INT1 external interrupt
20523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20531 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20536 Low voltage detect interrupt
20540 <row topline="true">
20541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20554 INT2 external interrupt
20557 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20565 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20570 Parallel slave port interrupt
20574 <row topline="true">
20575 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20588 CCP1 module interrupt
20591 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20599 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20604 AD convertion complete interrupt
20608 <row topline="true">
20609 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20617 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20622 CCP2 module interrupt
20625 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20633 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20638 USART receive interrupt
20642 <row topline="true">
20643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20656 TMR0 overflow interrupt
20659 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20672 USART transmit interrupt
20676 <row topline="true">
20677 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20685 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20690 TMR1 overflow interrupt
20693 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20701 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20706 SSP receive/transmit interrupt
20710 <row topline="true">
20711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20724 TMR2 matches PR2 interrupt
20727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20734 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20742 <row topline="true" bottomline="true">
20743 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20756 TMR3 overflow interrupt
20759 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20766 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20781 The prototypes for these names are defined in the header file
20788 In order to simplify signal handling, a number of macros is provided:
20790 \labelwidthstring 00.00.0000
20792 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20793 high priority interrupts.
20798 is the function name to use.
20800 \labelwidthstring 00.00.0000
20802 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20803 low priority interrupt.
20808 is the function name to use.
20810 \labelwidthstring 00.00.0000
20812 DEF_HANDLER(sig,handler) define a handler for signal
20816 \labelwidthstring 00.00.0000
20818 END_DEF end the declaration of the dispatch table.
20821 Additionally there are two more macros to simplify the declaration of the
20824 \labelwidthstring 00.00.0000
20828 SIGHANDLER(handler)
20830 this declares the function prototype for the
20836 \labelwidthstring 00.00.0000
20838 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20841 An example of using the macros above is shown below:
20844 #include <pic18fregs.h>
20847 #include <signal.h>
20851 DEF_INTHIGH(high_int)
20854 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20857 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20864 SIGHANDLER(_tmr0_handler)
20870 /* action to be taken when timer 0 overflows */
20877 SIGHANDLERNAKED(_bcol_handler)
20886 /* action to be taken when bus collision occurs */
20902 Special care should be taken when using the above scheme:
20905 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20908 when declaring SIGHANDLERNAKED handler never forget to use
20912 for proper returning.
20918 Here you can find some general tips for compiling programs with SDCC/pic16.
20919 \layout Subsubsection
20924 The default stack size (that is 64 bytes) probably is enough for many programs.
20925 One must take care that when there are many levels of function nesting,
20926 or there is excessive usage of stack, its size should be extended.
20927 An example of such a case is the printf/sprintf family of functions.
20928 If you encounter problems like not being able to print integers, then you
20929 need to set the stack size around the maximum (256 for small stack model).
20930 The following diagram shows what happens when calling printf to print an
20934 printf () --> ltoa () --> ultoa () --> divschar ()
20937 It is should be understood that stack is easily consumed when calling complicate
20939 Using command line arguments like -
20949 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20951 Other ways to reduce stack usage may exist.
20957 There are several approaches to debugging your code.
20958 This chapter is meant to show your options and to give detail on some of
20963 When writing your code:
20966 write your code with debugging in mind (avoid duplicating code, put conceptually
20967 similar variables into structs, use structured code, have strategic points
20968 within your code where all variables are consistent, ...)
20971 run a syntax-checking tool like splint
20972 \begin_inset LatexCommand \index{splint (syntax checking tool)}
20977 \begin_inset LatexCommand \index{lint (syntax checking tool)}
20992 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
20999 for the high level code use a C-compiler (like f.e.
21000 GCC) to compile run and debug the code on your host.
21012 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21016 ) on howto handle syntax extensions like __xdata, __at(), ...
21020 use another C-compiler to compile code for your target.
21021 Always an option but not recommended:) And not very likely to help you.
21022 If you seriously consider walking this path you should at least occasionally
21023 check portability of your code.
21024 Most commercial compiler vendors will offer an evaluation version so you
21025 can test compile your code or snippets of your code.
21028 Debugging on a simulator:
21031 there is a separate section about SDCDB (section
21032 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21039 or (8051 specific) use a freeware/commercial simulator which interfaces
21041 \begin_inset LatexCommand \index{AOMF, AOMF51}
21046 \begin_inset LatexCommand \ref{OMF file}
21050 ) optionally generated by SDCC.
21053 Debugging On-target:
21056 use a MCU port pin to serially output debug data to the RS232 port of your
21058 You'll probably want some level shifting device typically involving a MAX232
21060 If the hardware serial port of the MCU is not available search for 'Software
21061 UART' in your favourite search machine.
21064 use an on-target monitor.
21065 In this context a monitor is a small program which usually accepts commands
21066 via a serial line and allows to set program counter, to single step through
21067 a program and read/write memory locations.
21068 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21070 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21077 toggle MCU port pins at strategic points within your code and use an oscilloscop
21081 digital oscilloscope
21084 \begin_inset LatexCommand \index{oscilloscope}
21088 with deep trace memory is really helpful especially if you have to debug
21089 a realtime application.
21090 If you need to monitor more pins than your oscilloscope provides you can
21091 sometimes get away with a small R-2R network.
21092 On a single channel oscilloscope you could f.e.
21093 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21095 \begin_inset Formula $\Omega$
21098 resistor and the other one by a 5\SpecialChar ~
21100 \begin_inset Formula $\Omega$
21103 resistor to the oscilloscope probe (check output drive capability of the
21104 pins you want to monitor).
21105 If you need to monitor many more pins a
21125 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21130 Usually very expensive.
21131 And very nice to have too.
21132 And usually locks you (for years...) to the devices the ICE can emulate.
21136 use a remote debugger.
21137 In most 8-bit systems the symbol information is not available on the target,
21138 and a complete debugger is too bulky for the target system.
21139 Therefore usually a debugger on the host system connects to an on-target
21140 debugging stub which accepts only primitive commands.
21143 Terms to enter into your favourite search engine could be 'remote debugging',
21144 'gdb stub' or 'inferior debugger'.
21148 use an on target hardware debugger.
21149 Some of the more modern MCUs include hardware support for setting break
21150 points and monitoring/changing variables by using dedicated hardware pins.
21151 This facility doesn't require additional code to run on the target and
21156 doesn't affect runtime behaviour until a breakpoint is hit.
21157 For the mcs51 most hardware debuggers use the AOMF
21158 \begin_inset LatexCommand \index{AOMF, AOMF51}
21163 \begin_inset LatexCommand \ref{OMF file}
21174 if you are not familiar with any of the following terms you're likely to
21175 run into problems rather sooner than later:
21192 As an embedded programmer you
21196 to know them so why not look them up
21200 you have problems?)
21203 tell someone else about your problem (actually this is a surprisingly effective
21204 means to hunt down the bug even if the listener is not familiar with your
21206 As 'failure to communicate' is probably one of the job-induced deformations
21207 of an embedded programmer this is highly encouraged.
21210 Debugging with SDCDB
21211 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21216 \begin_inset LatexCommand \index{SDCDB (debugger)}
21223 SDCC is distributed with a source level debugger
21224 \begin_inset LatexCommand \index{Debugger}
21229 The debugger uses a command line interface, the command repertoire of the
21230 debugger has been kept as close to gdb
21231 \begin_inset LatexCommand \index{gdb}
21235 (the GNU debugger) as possible.
21236 The configuration and build process is part of the standard compiler installati
21237 on, which also builds and installs the debugger in the target directory
21238 specified during configuration.
21239 The debugger allows you debug BOTH at the C source and at the ASM source
21243 Compiling for Debugging
21257 \begin_inset LatexCommand \index{-\/-debug}
21261 option must be specified for all files for which debug information is to
21263 The compiler generates a .adb file for each of these files.
21264 The linker creates the .cdb
21265 \begin_inset LatexCommand \index{<file>.cdb}
21270 \begin_inset LatexCommand \index{<file>.adb}
21274 files and the address information.
21275 This .cdb is used by the debugger.
21278 How the Debugger Works
21291 -debug option is specified the compiler generates extra symbol information
21292 some of which are put into the assembler source and some are put into the
21294 Then the linker creates the .cdb file from the individual .adb files with
21295 the address information for the symbols.
21296 The debugger reads the symbolic information generated by the compiler &
21297 the address information generated by the linker.
21298 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21299 execution is controlled by the debugger.
21300 When a command is issued for the debugger, it translates it into appropriate
21301 commands for the simulator.
21302 (Currently SDCDM only connects to the simulator but
21307 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21311 is an effort to connect directly to the hardware.)
21314 Starting the Debugger SDCDB
21317 The debugger can be started using the following command line.
21318 (Assume the file you are debugging has the file name foo).
21332 The debugger will look for the following files.
21335 foo.c - the source file.
21338 foo.cdb - the debugger symbol information file.
21341 foo.ihx - the Intel hex format
21342 \begin_inset LatexCommand \index{Intel hex format}
21349 SDCDB Command Line Options
21362 -directory=<source file directory> this option can used to specify the directory
21364 The debugger will look into the directory list specified for source, cdb
21366 The items in the directory list must be separated by ':', e.g.
21367 if the source files can be in the directories /home/src1 and /home/src2,
21378 -directory option should be -
21388 -directory=/home/src1:/home/src2.
21389 Note there can be no spaces in the option.
21393 -cd <directory> - change to the <directory>.
21396 -fullname - used by GUI front ends.
21399 -cpu <cpu-type> - this argument is passed to the simulator please see the
21400 simulator docs for details.
21403 -X <Clock frequency > this options is passed to the simulator please see
21404 the simulator docs for details.
21407 -s <serial port file> passed to simulator see the simulator docs for details.
21410 -S <serial in,out> passed to simulator see the simulator docs for details.
21413 -k <port number> passed to simulator see the simulator docs for details.
21416 SDCDB Debugger Commands
21419 As mentioned earlier the command interface for the debugger has been deliberatel
21420 y kept as close the GNU debugger gdb, as possible.
21421 This will help the integration with existing graphical user interfaces
21422 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21423 If you use a graphical user interface for the debugger you can skip this
21425 \layout Subsubsection*
21427 break [line | file:line | function | file:function]
21430 Set breakpoint at specified line or function:
21439 sdcdb>break foo.c:100
21441 sdcdb>break funcfoo
21443 sdcdb>break foo.c:funcfoo
21444 \layout Subsubsection*
21446 clear [line | file:line | function | file:function ]
21449 Clear breakpoint at specified line or function:
21458 sdcdb>clear foo.c:100
21460 sdcdb>clear funcfoo
21462 sdcdb>clear foo.c:funcfoo
21463 \layout Subsubsection*
21468 Continue program being debugged, after breakpoint.
21469 \layout Subsubsection*
21474 Execute till the end of the current function.
21475 \layout Subsubsection*
21480 Delete breakpoint number 'n'.
21481 If used without any option clear ALL user defined break points.
21482 \layout Subsubsection*
21484 info [break | stack | frame | registers ]
21487 info break - list all breakpoints
21490 info stack - show the function call stack.
21493 info frame - show information about the current execution frame.
21496 info registers - show content of all registers.
21497 \layout Subsubsection*
21502 Step program until it reaches a different source line.
21503 Note: pressing <return> repeats the last command.
21504 \layout Subsubsection*
21509 Step program, proceeding through subroutine calls.
21510 \layout Subsubsection*
21515 Start debugged program.
21516 \layout Subsubsection*
21521 Print type information of the variable.
21522 \layout Subsubsection*
21527 print value of variable.
21528 \layout Subsubsection*
21533 load the given file name.
21534 Note this is an alternate method of loading file for debugging.
21535 \layout Subsubsection*
21540 print information about current frame.
21541 \layout Subsubsection*
21546 Toggle between C source & assembly source.
21547 \layout Subsubsection*
21549 ! simulator command
21552 Send the string following '!' to the simulator, the simulator response is
21554 Note the debugger does not interpret the command being sent to the simulator,
21555 so if a command like 'go' is sent the debugger can loose its execution
21556 context and may display incorrect values.
21557 \layout Subsubsection*
21564 My name is Bobby Brown"
21567 Interfacing SDCDB with DDD
21570 The screenshot was converted from png to eps with:
21571 \begin_inset Quotes sld
21574 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
21575 \begin_inset Quotes srd
21578 which produces a pretty compact eps file which is free from compression
21582 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
21583 as this broke the build system on Sourceforge (pdf-file was broken).
21589 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
21595 shows a screenshot of a debugging session with DDD
21596 \begin_inset LatexCommand \index{DDD (debugger)}
21600 (Unix only) on a simulated 8032.
21601 The debugging session might not run as smoothly as the screenshot suggests.
21602 The debugger allows setting of breakpoints, displaying and changing variables,
21603 single stepping through C and assembler code.
21606 The source was compiled with
21629 -debug ddd_example.c
21642 and DDD was invoked with
21649 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21652 Interfacing SDCDB with XEmacs
21653 \begin_inset LatexCommand \index{XEmacs}
21658 \begin_inset LatexCommand \index{Emacs}
21665 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21666 sdcdb.el and sdcdbsrc.el.
21667 These two files can be found in the $(prefix)/bin directory after the installat
21669 These files need to be loaded into XEmacs for the interface to work.
21670 This can be done at XEmacs startup time by inserting the following into
21671 your '.xemacs' file (which can be found in your HOME directory):
21677 (load-file sdcdbsrc.el)
21683 .xemacs is a lisp file so the () around the command is REQUIRED.
21684 The files can also be loaded dynamically while XEmacs is running, set the
21685 environment variable 'EMACSLOADPATH' to the installation bin directory
21686 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21687 To start the interface enter the following command:
21701 You will prompted to enter the file name to be debugged.
21706 The command line options that are passed to the simulator directly are bound
21707 to default values in the file sdcdbsrc.el.
21708 The variables are listed below, these values maybe changed as required.
21711 sdcdbsrc-cpu-type '51
21714 sdcdbsrc-frequency '11059200
21717 sdcdbsrc-serial nil
21720 The following is a list of key mapping for the debugger interface.
21731 ;;key\SpecialChar ~
21745 binding\SpecialChar ~
21769 ;;---\SpecialChar ~
21783 -------\SpecialChar ~
21825 sdcdb-next-from-src\SpecialChar ~
21853 sdcdb-back-from-src\SpecialChar ~
21881 sdcdb-cont-from-src\SpecialChar ~
21891 SDCDB continue command
21909 sdcdb-step-from-src\SpecialChar ~
21937 sdcdb-whatis-c-sexp\SpecialChar ~
21947 SDCDB ptypecommand for data at
22014 sdcdbsrc-delete\SpecialChar ~
22028 SDCDB Delete all breakpoints if no arg
22077 given or delete arg (C-u arg x)
22095 sdcdbsrc-frame\SpecialChar ~
22110 SDCDB Display current frame if no arg,
22159 given or display frame arg
22226 sdcdbsrc-goto-sdcdb\SpecialChar ~
22236 Goto the SDCDB output buffer
22254 sdcdb-print-c-sexp\SpecialChar ~
22265 SDCDB print command for data at
22332 sdcdbsrc-goto-sdcdb\SpecialChar ~
22342 Goto the SDCDB output buffer
22360 sdcdbsrc-mode\SpecialChar ~
22376 Toggles Sdcdbsrc mode (turns it off)
22391 sdcdb-finish-from-src\SpecialChar ~
22399 SDCDB finish command
22414 sdcdb-break\SpecialChar ~
22432 Set break for line with point
22447 sdcdbsrc-mode\SpecialChar ~
22463 Toggle Sdcdbsrc mode
22478 sdcdbsrc-srcmode\SpecialChar ~
22501 Here are a few guidelines that will help the compiler generate more efficient
22502 code, some of the tips are specific to this compiler others are generally
22503 good programming practice.
22506 Use the smallest data type to represent your data-value.
22507 If it is known in advance that the value is going to be less than 256 then
22508 use an 'unsigned char' instead of a 'short' or 'int'.
22509 Please note, that ANSI C requires both signed and unsigned chars to be
22510 promoted to 'signed int'
22511 \begin_inset LatexCommand \index{promotion to signed int}
22515 before doing any operation.
22517 \begin_inset LatexCommand \index{type promotion}
22522 \begin_inset LatexCommand \label{type promotion}
22526 can be omitted, if the result is the same.
22527 The effect of the promotion rules together with the sign-extension is often
22534 unsigned char uc = 0xfe;
22536 if (uc * uc < 0) /* this is true! */
22555 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
22565 (unsigned char) -12 / (signed char) -3 = ...
22568 No, the result is not 4:
22573 (int) (unsigned char) -12 / (int) (signed char) -3 =
22575 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
22577 (int) 0x00f4 / (int) 0xfffd =
22579 (int) 0x00f4 / (int) 0xfffd =
22581 (int) 244 / (int) -3 =
22583 (int) -81 = (int) 0xffaf;
22586 Don't complain, that gcc gives you a different result.
22587 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
22588 Therefore the results are different.
22591 \begin_inset Quotes sld
22595 \begin_inset Quotes srd
22601 If well-defined overflow characteristics are important and negative values
22602 are not, or if you want to steer clear of sign-extension problems when
22603 manipulating bits or bytes, use one of the corresponding unsigned types.
22604 (Beware when mixing signed and unsigned values in expressions, though.)
22606 Although character types (especially unsigned char) can be used as "tiny"
22607 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
22608 ble sign extension and increased code size.
22612 Use unsigned when it is known in advance that the value is not going to
22614 This helps especially if you are doing division or multiplication, bit-shifting
22615 or are using an array index.
22618 NEVER jump into a LOOP.
22621 Declare the variables to be local
22622 \begin_inset LatexCommand \index{local variables}
22626 whenever possible, especially loop control variables (induction).
22629 Since the compiler does not always do implicit integral promotion, the programme
22630 r should do an explicit cast when integral promotion is required.
22633 Reducing the size of division, multiplication & modulus operations can reduce
22634 code size substantially.
22635 Take the following code for example.
22641 foobar(unsigned int p1, unsigned char ch)
22649 unsigned char ch1 = p1 % ch ;
22660 For the modulus operation the variable ch will be promoted to unsigned int
22661 first then the modulus operation will be performed (this will lead to a
22662 call to support routine _moduint()), and the result will be casted to a
22664 If the code is changed to
22669 foobar(unsigned int p1, unsigned char ch)
22677 unsigned char ch1 = (unsigned char)p1 % ch ;
22688 It would substantially reduce the code generated (future versions of the
22689 compiler will be smart enough to detect such optimization opportunities).
22693 Have a look at the assembly listing to get a
22694 \begin_inset Quotes sld
22698 \begin_inset Quotes srd
22701 for the code generation.
22704 Porting code from or to other compilers
22707 check whether endianness of the compilers differs and adapt where needed.
22710 check the device specific header files for compiler specific syntax.
22711 Eventually include the file <compiler.h
22712 \begin_inset LatexCommand \index{compiler.h (include file)}
22716 > to allow using common header files.
22719 check whether the startup code contains the correct initialization (watchdog,
22723 check whether the sizes of short, int, long match.
22726 check if some 16 or 32 bit hardware registers require a specific addressing
22727 order (least significant or most significant byte first) and adapt if needed
22736 relate to time and not to lower/upper memory location here, so this is
22741 the same as endianness).
22744 check whether the keyword
22748 is used where needed.
22749 The compilers might differ in their optimization characteristics (as different
22750 versions of the same compiler might also use more clever optimizations
22751 this is good idea anyway).
22754 check that the compilers are not told to supress warnings.
22757 check and convert compiler specific extensions (interrupts, memory areas,
22761 check for differences in type promotion (especially check for math operations
22762 on char variables and for the use of the ~\SpecialChar ~
22763 operator on bit variables.
22765 \begin_inset LatexCommand \ref{type promotion}
22770 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
22777 check the assembly code generated for interrupt routines (f.e.
22778 for calls to possibly non-reentrant library functions).
22781 check whether timing loops result in proper timing (or preferably consider
22782 a rewrite of the code with timer based delays instead).
22785 check for differences in printf parameters (some compilers push (va_arg
22786 \begin_inset LatexCommand \index{va\_arg}
22790 ) char variables as integers others as char).
22793 check the resulting memory layout.
22797 \begin_inset LatexCommand \index{Tools}
22801 included in the distribution
22805 \begin_inset Tabular
22806 <lyxtabular version="3" rows="12" columns="3">
22808 <column alignment="left" valignment="top" leftline="true" width="0pt">
22809 <column alignment="left" valignment="top" leftline="true" width="0pt">
22810 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22811 <row topline="true" bottomline="true">
22812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22828 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22837 <row topline="true">
22838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22851 Simulator for various architectures
22854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22863 <row topline="true">
22864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22877 header file conversion
22880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22885 sdcc/support/scripts
22889 <row topline="true">
22890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22903 header file conversion
22906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22911 sdcc/support/scripts
22915 <row topline="true">
22916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22950 <row topline="true">
22951 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22985 <row topline="true">
22986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23020 <row topline="true">
23021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23029 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23037 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23055 <row topline="true">
23056 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23072 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23090 <row topline="true">
23091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23125 <row topline="true">
23126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23134 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23142 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23160 <row topline="true" bottomline="true">
23161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23169 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23177 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23205 \begin_inset LatexCommand \index{Documentation}
23209 included in the distribution
23213 \begin_inset Tabular
23214 <lyxtabular version="3" rows="10" columns="2">
23216 <column alignment="left" valignment="top" leftline="true" width="0">
23217 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23218 <row topline="true" bottomline="true">
23219 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23232 Where to get / filename
23236 <row topline="true">
23237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23242 SDCC Compiler User Guide
23245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23250 You're reading it right now
23254 <row topline="true">
23255 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23263 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23272 <row topline="true">
23273 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23279 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23284 \begin_inset LatexCommand \index{Assembler documentation}
23288 Assemblers and ASLINK
23289 \begin_inset LatexCommand \index{aslink}
23294 \begin_inset LatexCommand \index{Linker documentation}
23301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23306 sdcc/as/doc/asxhtm.html
23310 <row topline="true">
23311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23316 SDCC regression test
23317 \begin_inset LatexCommand \index{Regression test}
23324 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23329 sdcc/doc/test_suite_spec.pdf
23333 <row topline="true">
23334 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23342 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23351 <row topline="true">
23352 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23357 Notes on debugging with SDCDB
23358 \begin_inset LatexCommand \index{SDCDB (debugger)}
23365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23370 sdcc/debugger/README
23374 <row topline="true">
23375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23380 Software simulator for microcontrollers
23383 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23410 <row topline="true">
23411 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23416 Temporary notes on the pic16
23417 \begin_inset LatexCommand \index{PIC16}
23424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23429 sdcc/src/pic16/NOTES
23433 <row topline="true" bottomline="true">
23434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23439 SDCC internal documentation (debugging file format)
23442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23478 Related open source tools
23479 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
23484 \begin_inset LatexCommand \index{Related tools}
23492 \begin_inset Tabular
23493 <lyxtabular version="3" rows="14" columns="3">
23495 <column alignment="left" valignment="top" leftline="true" width="0pt">
23496 <column alignment="block" valignment="top" leftline="true" width="30line%">
23497 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23498 <row topline="true" bottomline="true">
23499 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23507 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23524 <row topline="true">
23525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23531 \begin_inset LatexCommand \index{gpsim (pic simulator)}
23538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23552 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
23560 <row topline="true">
23561 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23567 \begin_inset LatexCommand \index{gputils (pic tools)}
23574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23582 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23588 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
23596 <row topline="true">
23597 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23605 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23613 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23619 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
23627 <row topline="true">
23628 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23641 Tools for Silicon Laboratories JTAG debug adapter, partly based on SDCDB
23645 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23651 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
23659 <row topline="true">
23660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23666 \begin_inset LatexCommand \index{indent (source formatting tool)}
23673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23678 Formats C source - Master of the white spaces
23681 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23687 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
23695 <row topline="true">
23696 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23702 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
23709 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23714 Object file conversion, checksumming, ...
23717 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23723 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
23731 <row topline="true">
23732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23738 \begin_inset LatexCommand \index{objdump (tool)}
23745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23750 Object file conversion, ...
23753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23758 Part of binutils (should be there anyway)
23762 <row topline="true">
23763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23776 8051 monitor (hex up-/download, single step, disassemble)
23779 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23785 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
23793 <row topline="true">
23794 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23800 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23807 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23812 Source code documentation system
23815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23821 \begin_inset LatexCommand \url{http://www.doxygen.org}
23829 <row topline="true">
23830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23843 IDE (has anyone tried integrating SDCC & SDCDB? Unix only)
23846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23852 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23860 <row topline="true">
23861 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23869 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23874 8051 monitor (hex up-/download, single step, disassemble)
23877 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23883 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
23891 <row topline="true">
23892 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23898 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23910 Statically checks c sources (see
23911 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23918 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23924 \begin_inset LatexCommand \url{http://www.splint.org}
23932 <row topline="true" bottomline="true">
23933 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23939 \begin_inset LatexCommand \index{ddd (debugger)}
23946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23951 Debugger, serves nicely as GUI to SDCDB
23952 \begin_inset LatexCommand \index{SDCDB (debugger)}
23959 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23965 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
23982 Related documentation / recommended reading
23986 \begin_inset Tabular
23987 <lyxtabular version="3" rows="8" columns="3">
23989 <column alignment="left" valignment="top" leftline="true" width="0pt">
23990 <column alignment="block" valignment="top" leftline="true" width="30line%">
23991 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23992 <row topline="true" bottomline="true">
23993 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24001 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24009 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24018 <row topline="true">
24019 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24042 \begin_inset LatexCommand \index{C Reference card}
24049 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24055 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24063 <row topline="true">
24064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24072 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24080 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24086 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24094 <row topline="true">
24095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24103 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24109 \begin_inset Quotes sld
24113 \begin_inset Quotes srd
24119 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24127 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24135 <row topline="true">
24136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24144 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24150 \begin_inset Quotes sld
24153 Extensions for Embedded C
24154 \begin_inset Quotes srd
24160 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24168 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24176 <row topline="true">
24177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24184 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24189 Latest datasheet of the target CPU
24192 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24201 <row topline="true">
24202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24209 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24214 Revision history of datasheet
24217 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24226 <row topline="true" bottomline="true">
24227 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24242 Advanced Compiler Design and Implementation
24245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24250 bookstore (very dedicated, probably read other books first)
24266 Some questions answered, some pointers given - it might be time to in turn
24274 can you solve your project with the selected microcontroller? Would you
24275 find out early or rather late that your target is too small/slow/whatever?
24276 Can you switch to a slightly better device if it doesn't fit?
24279 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
24280 and/or another programming language be more adequate? Would an operating
24281 system on the target device help?
24284 if you solved the problem, will the marketing department be happy?
24287 if the marketing department is happy, will customers be happy?
24290 if you're the project manager, marketing department and maybe even the customer
24291 in one person, have you tried to see the project from the outside?
24294 is the project done if you think it is done? Or is just that other interface/pro
24295 tocol/feature/configuration/option missing? How about website, manual(s),
24296 internationali(z|s)ation, packaging, labels, 2nd source for components,
24297 electromagnetic compatability/interference, documentation for production,
24298 production test software, update mechanism, patent issues?
24301 is your project adequately positioned in that magic triangle: fame, fortune,
24305 Maybe not all answers to these questions are known and some answers may
24310 , nevertheless knowing these questions may help you to avoid burnout
24316 burnout is bad for electronic devices, programmers and motorcycle tyres
24320 Chances are you didn't want to hear some of them...
24324 \begin_inset LatexCommand \index{Support}
24331 SDCC has grown to be a large project.
24332 The compiler alone (without the preprocessor, assembler and linker) is
24333 well over 100,000 lines of code (blank stripped).
24334 The open source nature of this project is a key to its continued growth
24336 You gain the benefit and support of many active software developers and
24338 Is SDCC perfect? No, that's why we need your help.
24339 The developers take pride in fixing reported bugs.
24340 You can help by reporting the bugs and helping other SDCC users.
24341 There are lots of ways to contribute, and we encourage you to take part
24342 in making SDCC a great software package.
24346 The SDCC project is hosted on the SDCC sourceforge site at
24347 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
24352 You'll find the complete set of mailing lists
24353 \begin_inset LatexCommand \index{Mailing list(s)}
24357 , forums, bug reporting system, patch submission
24358 \begin_inset LatexCommand \index{Patch submission}
24363 \begin_inset LatexCommand \index{download}
24367 area and Subversion code repository
24368 \begin_inset LatexCommand \index{Subversion code repository}
24376 \begin_inset LatexCommand \index{Bug reporting}
24381 \begin_inset LatexCommand \index{Reporting bugs}
24388 The recommended way of reporting bugs is using the infrastructure of the
24390 You can follow the status of bug reports there and have an overview about
24394 Bug reports are automatically forwarded to the developer mailing list and
24395 will be fixed ASAP.
24396 When reporting a bug, it is very useful to include a small test program
24397 (the smaller the better) which reproduces the problem.
24398 If you can isolate the problem by looking at the generated assembly code,
24399 this can be very helpful.
24400 Compiling your program with the -
24411 \begin_inset LatexCommand \index{-\/-dumpall}
24415 option can sometimes be useful in locating optimization problems.
24416 When reporting a bug please maker sure you:
24419 Attach the code you are compiling with SDCC.
24423 Specify the exact command you use to run SDCC, or attach your Makefile.
24427 Specify the SDCC version (type "
24433 "), your platform, and operating system.
24437 Provide an exact copy of any error message or incorrect output.
24441 Put something meaningful in the subject of your message.
24444 Please attempt to include these 5 important parts, as applicable, in all
24445 requests for support or when reporting any problems or bugs with SDCC.
24446 Though this will make your message lengthy, it will greatly improve your
24447 chance that SDCC users and developers will be able to help you.
24448 Some SDCC developers are frustrated by bug reports without code provided
24449 that they can use to reproduce and ultimately fix the problem, so please
24450 be sure to provide sample code if you are reporting a bug!
24453 Please have a short check that you are using a recent version of SDCC and
24454 the bug is not yet known.
24455 This is the link for reporting bugs:
24456 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
24463 Requesting Features
24464 \begin_inset LatexCommand \label{sub:Requesting-Features}
24469 \begin_inset LatexCommand \index{Feature request}
24474 \begin_inset LatexCommand \index{Requesting features}
24481 Like bug reports feature requests are forwarded to the developer mailing
24483 This is the link for requesting features:
24484 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24494 Like bug reports contributed patches are forwarded to the developer mailing
24496 This is the link for submitting patches
24497 \begin_inset LatexCommand \index{Patch submission}
24502 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
24509 You need to specify some parameters to the
24513 command for the patches to be useful.
24514 If you modified more than one file a patch created f.e.
24519 \begin_inset Quotes sld
24522 diff -Naur unmodified_directory modified_directory >my_changes.patch
24523 \begin_inset Quotes srd
24529 will be fine, otherwise
24533 \begin_inset Quotes sld
24536 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
24537 \begin_inset Quotes srd
24550 These links should take you directly to the
24551 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
24561 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
24562 automated messages (mid 2003)
24566 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
24571 \begin_inset LatexCommand \index{Mailing list(s)}
24575 and forums are archived and searchable so if you are lucky someone already
24576 had a similar problem.
24577 While mails to the lists themselves are delivered promptly their web front
24578 end on sourceforge sometimes shows a severe time lag (up to several weeks),
24579 if you're seriously using SDCC please consider subscribing to the lists.
24585 You can follow the status of the Subversion version
24586 \begin_inset LatexCommand \index{version}
24590 of SDCC by watching the Changelog
24591 \begin_inset LatexCommand \index{Changelog}
24595 in the Subversion repository
24598 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
24605 Subversion Source Code Repository
24614 or the filenames of the snapshot versions of SDCC include date and its
24616 \begin_inset LatexCommand \index{Subversion}
24621 Subversion allows to download the source of recent or previous versions
24623 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
24627 (by number or by date).
24628 An on-line source code browser and detailled instructions are also available
24630 SDCC versions starting from 1999 up to now are available (currently the
24631 versions prior to the conversion from cvs to Subversion (April 2006) are
24632 either by accessible by Subversion or by cvs).
24636 \begin_inset LatexCommand \index{Release policy}
24643 Historically there often were long delays between official releases and
24644 the sourceforge download area tends to get not updated at all.
24645 Excuses in the past might have referred to problems with live range analysis,
24646 but as this was fixed a while ago, the current problem is that another
24647 excuse has to be found.
24648 Kidding aside, we have to get better there! On the other hand there are
24649 daily snapshots available at
24650 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
24654 , and you can always build the very last version (hopefully with many bugs
24655 fixed, and features added) from the source code available at
24656 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
24664 \begin_inset LatexCommand \index{Examples}
24671 You'll find some small examples in the directory
24673 sdcc/device/examples/.
24676 More examples and libraries are available at
24678 The SDCC Open Knowledge Resource
24679 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
24686 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
24693 I did insert a reference to Paul's web site here although it seems rather
24694 dedicated to a specific 8032 board (I think it's okay because it f.e.
24695 shows LCD/Harddisc interface and has a free 8051 monitor.
24696 Independent 8032 board vendors face hard competition of heavily subsidized
24697 development boards anyway).
24700 Maybe we should include some links to real world applications.
24701 Preferably pointer to pointers (one for each architecture) so this stays
24706 \begin_inset LatexCommand \index{Quality control}
24713 The compiler is passed through nightly compile and build checks.
24719 \begin_inset LatexCommand \index{Regression test}
24723 check that SDCC itself compiles flawlessly on several platforms and checks
24724 the quality of the code generated by SDCC by running the code through simulator
24726 There is a separate document
24729 \begin_inset LatexCommand \index{Test suite}
24738 You'll find the test code in the directory
24740 sdcc/support/regression
24743 You can run these tests manually by running
24747 in this directory (or f.e.
24752 \begin_inset Quotes sld
24756 \begin_inset Quotes srd
24762 if you don't want to run the complete tests).
24763 The test code might also be interesting if you want to look for examples
24764 \begin_inset LatexCommand \index{Examples}
24768 checking corner cases of SDCC or if you plan to submit patches
24769 \begin_inset LatexCommand \index{Patch submission}
24776 The pic port uses a different set of regression tests, you'll find them
24779 sdcc/src/regression
24784 Use of SDCC in Education
24797 the phrase "use in education" might evoke the association "
24801 fit for use in education".
24802 This connotation is not intended but nevertheless risked as the licensing
24803 of SDCC makes it difficult to offer educational discounts
24807 If your rationales are to:
24810 give students a chance to understand the
24814 steps of code generation
24817 have a curriculum that can be extended for years.
24818 Then you could use an fpga board as target and your curriculum will seamlessly
24819 extend from logic synthesis (
24820 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
24825 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
24829 ), over assembly programming, to C to FPGA compilers (
24830 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
24837 be able to insert excursions about skills like using a revision control
24838 system, submitting/applying patches, using a type-setting (as opposed to
24839 word-processing) engine LyX/LaTeX, using
24840 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
24845 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
24849 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
24850 Source Software, CPU simulation, compiler regression tests
24851 \begin_inset LatexCommand \index{Regression test}
24858 And if there should be a shortage of ideas then you can always point students
24859 to the ever-growing feature request list
24860 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24867 not tie students to a specific host platform and instead allow them to use
24872 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
24874 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
24881 not encourage students to use illegal copies of educational software
24884 be immune to licensing/availability/price changes of the chosen tool chain
24887 be able to change to a new target platform without having to adopt a new
24891 have complete control over and insight into the tool chain
24894 make your students aware about the pros and cons of open source software
24898 give back to the public as you are probably at least partially publically
24902 give students a chance to publically prove their skills and to possibly
24903 see a world wide impact
24906 then SDCC is probably among the first choices.
24907 Well, probably SDCC might be the only choice.
24910 SDCC Technical Data
24914 \begin_inset LatexCommand \index{Optimizations}
24921 SDCC performs a host of standard optimizations in addition to some MCU specific
24926 Sub-expression Elimination
24927 \begin_inset LatexCommand \index{Subexpression elimination}
24934 The compiler does local and
24960 will be translated to
24972 Some subexpressions are not as obvious as the above example, e.g.:
24982 In this case the address arithmetic a->b[i] will be computed only once;
24983 the equivalent code in C would be.
24995 The compiler will try to keep these temporary variables in registers.
24998 Dead-Code Elimination
24999 \begin_inset LatexCommand \index{Dead-code elimination}
25020 i = 1; \SpecialChar ~
25029 global = 1;\SpecialChar ~
25042 global = 3;\SpecialChar ~
25067 \begin_inset LatexCommand \index{Copy propagation}
25123 Note: the dead stores created by this copy propagation will be eliminated
25124 by dead-code elimination.
25128 \begin_inset LatexCommand \index{Loop optimization}
25133 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
25140 Two types of loop optimizations are done by SDCC
25148 of loop induction variables.
25149 In addition to the strength reduction the optimizer marks the induction
25150 variables and the register allocator tries to keep the induction variables
25151 in registers for the duration of the loop.
25152 Because of this preference of the register allocator
25153 \begin_inset LatexCommand \index{Register allocation}
25157 , loop induction optimization causes an increase in register pressure, which
25158 may cause unwanted spilling of other temporary variables into the stack
25159 \begin_inset LatexCommand \index{stack}
25164 The compiler will generate a warning message when it is forced to allocate
25165 extra space either on the stack or data space.
25166 If this extra space allocation is undesirable then induction optimization
25167 can be eliminated either for the entire source file (with -
25177 -noinduction option) or for a given function only using #pragma\SpecialChar ~
25179 \begin_inset LatexCommand \index{\#pragma noinduction}
25192 for (i = 0 ; i < 100 ; i ++)
25208 for (i = 0; i < 100; i++)
25217 As mentioned previously some loop invariants are not as apparent, all static
25218 address computations are also moved out of the loop.
25223 \begin_inset LatexCommand \index{Strength reduction}
25227 , this optimization substitutes an expression by a cheaper expression:
25232 for (i=0;i < 100; i++)
25250 for (i=0;i< 100;i++) {
25256 ar[itemp1] = itemp2;
25273 The more expensive multiplication
25274 \begin_inset LatexCommand \index{Multiplication}
25278 is changed to a less expensive addition.
25282 \begin_inset LatexCommand \index{Loop reversing}
25289 This optimization is done to reduce the overhead of checking loop boundaries
25290 for every iteration.
25291 Some simple loops can be reversed and implemented using a
25292 \begin_inset Quotes eld
25295 decrement and jump if not zero
25296 \begin_inset Quotes erd
25300 SDCC checks for the following criterion to determine if a loop is reversible
25301 (note: more sophisticated compilers use data-dependency analysis to make
25302 this determination, SDCC uses a more simple minded analysis).
25305 The 'for' loop is of the form
25311 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
25321 The <for body> does not contain
25322 \begin_inset Quotes eld
25326 \begin_inset Quotes erd
25330 \begin_inset Quotes erd
25336 All goto's are contained within the loop.
25339 No function calls within the loop.
25342 The loop control variable <sym> is not assigned any value within the loop
25345 The loop control variable does NOT participate in any arithmetic operation
25349 There are NO switch statements in the loop.
25352 Algebraic Simplifications
25355 SDCC does numerous algebraic simplifications, the following is a small sub-set
25356 of these optimizations.
25361 i = j + 0;\SpecialChar ~
25365 /* changed to: */\SpecialChar ~
25371 i /= 2;\SpecialChar ~
25378 /* changed to: */\SpecialChar ~
25384 i = j - j;\SpecialChar ~
25388 /* changed to: */\SpecialChar ~
25394 i = j / 1;\SpecialChar ~
25398 /* changed to: */\SpecialChar ~
25405 Note the subexpressions
25406 \begin_inset LatexCommand \index{Subexpression}
25410 given above are generally introduced by macro expansions or as a result
25411 of copy/constant propagation.
25414 'switch' Statements
25415 \begin_inset LatexCommand \label{sub:'switch'-Statements}
25420 \begin_inset LatexCommand \index{switch statement}
25427 SDCC can optimize switch statements to jump tables
25428 \begin_inset LatexCommand \index{jump tables}
25433 It makes the decision based on an estimate of the generated code size.
25434 SDCC is quite liberal in the requirements for jump table generation:
25437 The labels need not be in order, and the starting number need not be one
25438 or zero, the case labels are in numerical sequence or not too many case
25439 labels are missing.
25445 switch(i) {\SpecialChar ~
25476 case 4: ...\SpecialChar ~
25508 case 5: ...\SpecialChar ~
25540 case 3: ...\SpecialChar ~
25571 case 6: ...\SpecialChar ~
25603 case 7: ...\SpecialChar ~
25635 case 8: ...\SpecialChar ~
25667 case 9: ...\SpecialChar ~
25699 case 10: ...\SpecialChar ~
25730 case 11: ...\SpecialChar ~
25797 Both the above switch statements will be implemented using a jump-table.
25798 The example to the right side is slightly more efficient as the check for
25799 the lower boundary of the jump-table is not needed.
25803 The number of case labels is not larger than supported by the target architectur
25807 If the case labels are not in numerical sequence ('gaps' between cases)
25808 SDCC checks whether a jump table with additionally inserted dummy cases
25809 is still attractive.
25813 If the starting number is not zero and a check for the lower boundary of
25814 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
25819 Switch statements which have large gaps in the numeric sequence or those
25820 that have too many case labels can be split into more than one switch statement
25821 for efficient code generation, e.g.:
25901 If the above switch statement is broken down into two switch statements
25991 then both the switch statements will be implemented using jump-tables whereas
25992 the unmodified switch statement will not be.
25995 There might be reasons which SDCC cannot know about to either favour or
25996 not favour jump tables.
25997 If the target system has to be as quick for the last switch case as for
25998 the first (pro jump table), or if the switch argument is known to be zero
25999 in the majority of the cases (contra jump table).
26002 The pragma nojtbound
26003 \begin_inset LatexCommand \index{\#pragma nojtbound}
26007 can be used to turn off checking the
26020 It has no effect if a default label is supplied.
26021 Use of this pragma is dangerous: if the switch
26022 \begin_inset LatexCommand \index{switch statement}
26026 argument is not matched by a case statement the processor will happily
26030 Bit-shifting Operations
26031 \begin_inset LatexCommand \index{Bit shifting}
26038 Bit shifting is one of the most frequently used operation in embedded programmin
26040 SDCC tries to implement bit-shift operations in the most efficient way
26056 generates the following code:
26073 In general SDCC will never setup a loop if the shift count is known.
26116 \begin_inset LatexCommand \index{Bit rotation}
26123 A special case of the bit-shift operation is bit rotation
26124 \begin_inset LatexCommand \index{rotating bits}
26128 , SDCC recognizes the following expression to be a left bit-rotation:
26138 char i;\SpecialChar ~
26149 /* unsigned is needed for rotation */
26154 i = ((i << 1) | (i >> 7));
26163 will generate the following code:
26182 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
26183 ns of this case will also be recognized as bit-rotation, i.e.:
26188 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
26191 Nibble and Byte Swapping
26194 Other special cases of the bit-shift operations are nibble or byte swapping
26195 \begin_inset LatexCommand \index{swapping nibbles/bytes}
26199 , SDCC recognizes the following expressions:
26222 i = ((i << 4) | (i >> 4));
26228 j = ((j << 8) | (j >> 8));
26231 and generates a swap instruction for the nibble swapping
26232 \begin_inset LatexCommand \index{Nibble swapping}
26236 or move instructions for the byte swapping
26237 \begin_inset LatexCommand \index{Byte swapping}
26243 \begin_inset Quotes sld
26247 \begin_inset Quotes srd
26250 example can be used to convert from little to big-endian or vice versa.
26251 If you want to change the endianness of a
26255 integer you have to cast to
26262 Note that SDCC stores numbers in little-endian
26268 Usually 8-bit processors don't care much about endianness.
26269 This is not the case for the standard 8051 which only has an instruction
26275 \begin_inset LatexCommand \index{DPTR}
26283 so little-endian is the more efficient byte order.
26287 \begin_inset LatexCommand \index{little-endian}
26292 \begin_inset LatexCommand \index{Endianness}
26297 lowest order first).
26301 \begin_inset LatexCommand \index{Highest Order Bit}
26306 \begin_inset LatexCommand \index{Any Order Bit}
26313 It is frequently required to obtain the highest order bit of an integral
26314 type (long, int, short or char types).
26315 Also obtaining any other order bit is not uncommon.
26316 SDCC recognizes the following expressions to yield the highest order bit
26317 and generates optimized code for it, e.g.:
26330 unsigned char hob1, aob1;
26334 bit hob2, hob3, aob2, aob3;
26343 hob1 = (gint >> 15) & 1;
26347 hob2 = (gint >> 15) & 1;
26351 hob3 = gint & 0x8000;
26355 aob1 = (gint >> 9) & 1;
26359 aob2 = (gint >> 8) & 1;
26363 aob3 = gint & 0x0800;
26373 will generate the following code:
26406 000A E5*01\SpecialChar ~
26433 000C 23\SpecialChar ~
26464 000D 54 01\SpecialChar ~
26491 000F F5*02\SpecialChar ~
26546 0011 E5*01\SpecialChar ~
26573 0013 33\SpecialChar ~
26603 0014 92*00\SpecialChar ~
26658 0016 E5*01\SpecialChar ~
26685 0018 33\SpecialChar ~
26715 0019 92*01\SpecialChar ~
26770 001B E5*01\SpecialChar ~
26797 001D 03\SpecialChar ~
26828 001E 54 01\SpecialChar ~
26855 0020 F5*03\SpecialChar ~
26910 0022 E5*01\SpecialChar ~
26937 0024 13\SpecialChar ~
26967 0025 92*02\SpecialChar ~
27022 0027 E5*01\SpecialChar ~
27049 0029 A2 E3\SpecialChar ~
27076 002B 92*03\SpecialChar ~
27104 Other variations of these cases however will
27109 They are standard C expressions, so I heartily recommend these be the only
27110 way to get the highest order bit, (it is portable).
27111 Of course it will be recognized even if it is embedded in other expressions,
27117 xyz = gint + ((gint >> 15) & 1);
27120 will still be recognized.
27124 \begin_inset LatexCommand \index{Higher Order Byte}
27128 / Higher Order Word
27129 \begin_inset LatexCommand \index{Higher Order Word}
27136 It is also frequently required to obtain a higher order byte or word of
27137 a larger integral type (long, int or short types).
27138 SDCC recognizes the following expressions to yield the higher order byte
27139 or word and generates optimized code for it, e.g.:
27146 unsigned long int glong;
27154 unsigned char hob1, hob2;
27158 unsigned int how1, how2;
27167 hob1 = (gint >> 8) & 0xFF;
27171 hob2 = glong >> 24;
27175 how1 = (glong >> 16) & 0xFFFF;
27189 will generate the following code:
27222 0037 85*01*06\SpecialChar ~
27244 _foo_hob1_1_1,(_gint + 1)
27274 003A 85*05*07\SpecialChar ~
27296 _foo_hob2_1_1,(_glong + 3)
27326 003D 85*04*08\SpecialChar ~
27348 _foo_how1_1_1,(_glong + 2)
27350 0040 85*05*09\SpecialChar ~
27372 (_foo_how1_1_1 + 1),(_glong + 3)
27374 0043 85*03*0A\SpecialChar ~
27396 _foo_how2_1_1,(_glong + 1)
27398 0046 85*04*0B\SpecialChar ~
27420 (_foo_how2_1_1 + 1),(_glong + 2)
27423 Again, variations of these cases may
27428 They are standard C expressions, so I heartily recommend these be the only
27429 way to get the higher order byte/word, (it is portable).
27430 Of course it will be recognized even if it is embedded in other expressions,
27436 xyz = gint + ((gint >> 8) & 0xFF);
27439 will still be recognized.
27443 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
27448 \begin_inset LatexCommand \index{Peephole optimizer}
27455 The compiler uses a rule based, pattern matching and re-writing mechanism
27456 for peep-hole optimization.
27461 a peep-hole optimizer by Christopher W.
27462 Fraser (cwfraser\SpecialChar ~
27465 A default set of rules are compiled into the compiler, additional rules
27466 may be added with the
27479 \begin_inset LatexCommand \index{-\/-peep-file}
27486 The rule language is best illustrated with examples.
27510 The above rule will change the following assembly
27511 \begin_inset LatexCommand \index{Assembler routines}
27533 Note: All occurrences of a
27537 (pattern variable) must denote the same string.
27538 With the above rule, the assembly sequence:
27548 will remain unmodified.
27552 Other special case optimizations may be added by the user (via
27568 some variants of the 8051 MCU
27569 \begin_inset LatexCommand \index{MCS51 variants}
27582 The following two rules will change all
27601 replace { lcall %1 } by { acall %1 }
27603 replace { ljmp %1 } by { ajmp %1 }
27608 inline-assembler code
27610 is also passed through the peep hole optimizer, thus the peephole optimizer
27611 can also be used as an assembly level macro expander.
27612 The rules themselves are MCU dependent whereas the rule language infra-structur
27613 e is MCU independent.
27614 Peephole optimization rules for other MCU can be easily programmed using
27619 The syntax for a rule is as follows:
27624 rule := replace [ restart ] '{' <assembly sequence> '
27662 <assembly sequence> '
27680 '}' [if <functionName> ] '
27685 <assembly sequence> := assembly instruction (each instruction including
27686 labels must be on a separate line).
27690 The optimizer will apply to the rules one by one from the top in the sequence
27691 of their appearance, it will terminate when all rules are exhausted.
27692 If the 'restart' option is specified, then the optimizer will start matching
27693 the rules again from the top, this option for a rule is expensive (performance)
27694 , it is intended to be used in situations where a transformation will trigger
27695 the same rule again.
27696 An example of this (not a good one, it has side effects) is the following
27719 Note that the replace pattern cannot be a blank, but can be a comment line.
27720 Without the 'restart' option only the innermost 'pop' 'push' pair would
27721 be eliminated, i.e.:
27751 the restart option the rule will be applied again to the resulting code
27752 and then all the pop-push pairs will be eliminated to yield:
27762 A conditional function can be attached to a rule.
27763 Attaching rules are somewhat more involved, let me illustrate this with
27790 The optimizer does a look-up of a function name table defined in function
27795 in the source file SDCCpeeph.c, with the name
27800 If it finds a corresponding entry the function is called.
27801 Note there can be no parameters specified for these functions, in this
27806 is crucial, since the function
27810 expects to find the label in that particular variable (the hash table containin
27811 g the variable bindings is passed as a parameter).
27812 If you want to code more such functions, take a close look at the function
27813 labelInRange and the calling mechanism in source file SDCCpeeph.c.
27814 Currently implemented are
27816 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
27817 24bitMode, portIsDS390, 24bitModeAndPortDS390
27826 I know this whole thing is a little kludgey, but maybe some day we will
27827 have some better means.
27828 If you are looking at this file, you will see the default rules that are
27829 compiled into the compiler, you can add your own rules in the default set
27830 there if you get tired of specifying the -
27844 \begin_inset LatexCommand \index{ANSI-compliance}
27849 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
27856 The latest publically available version of the standard
27858 ISO/IEC 9899 - Programming languages - C
27860 should be available at:
27861 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
27870 Deviations from the compliance:
27873 functions are not reentrant
27874 \begin_inset LatexCommand \index{reentrant}
27878 unless explicitly declared as such or the
27891 \begin_inset LatexCommand \index{-\/-stack-auto}
27897 command line option is specified.
27900 structures and unions cannot be assigned values directly, cannot be passed
27901 as function parameters or assigned to each other and cannot be a return
27902 value from a function, e.g.:
27928 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
27939 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
27961 return rets;/* is invalid in SDCC although allowed in ANSI */
27967 initialization of structure arrays must be fully braced.
27973 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
27975 struct s { char x } a[] = {{1}, {2}}; /* OK */
27980 \begin_inset LatexCommand \index{long long (not supported)}
27985 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
27993 \begin_inset LatexCommand \index{double (not supported)}
27997 ' precision floating point
27998 \begin_inset LatexCommand \index{Floating point support}
28006 \begin_inset LatexCommand \index{K\&R style}
28010 function declarations are NOT allowed.
28016 foo(i,j) /* this old style of function declarations */
28018 int i,j; /* are valid in ANSI but not valid in SDCC */
28033 Most enhancements in C99 are not supported, f.e.:
28042 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
28049 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
28053 Certain words that are valid identifiers in the standard may be reserved
28054 words in SDCC unless the
28067 \begin_inset LatexCommand \index{-\/-std-c89}
28082 \begin_inset LatexCommand \index{-\/-std-c99}
28088 command line options are used.
28089 These may include (depending on the selected processor): 'at', 'banked',
28090 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
28091 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
28092 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
28094 Compliant equivalents of these keywords are always available in a form
28095 that begin with two underscores
28096 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
28101 '__data' instead of 'data'.
28104 Cyclomatic Complexity
28105 \begin_inset LatexCommand \index{Cyclomatic complexity}
28112 Cyclomatic complexity of a function is defined as the number of independent
28113 paths the program can take during execution of the function.
28114 This is an important number since it defines the number test cases you
28115 have to generate to validate the function.
28116 The accepted industry standard for complexity number is 10, if the cyclomatic
28117 complexity reported by SDCC exceeds 10 you should think about simplification
28118 of the function logic.
28119 Note that the complexity level is not related to the number of lines of
28120 code in a function.
28121 Large functions can have low complexity, and small functions can have large
28127 SDCC uses the following formula to compute the complexity:
28132 complexity = (number of edges in control flow graph) - (number of nodes
28133 in control flow graph) + 2;
28137 Having said that the industry standard is 10, you should be aware that in
28138 some cases it be may unavoidable to have a complexity level of less than
28140 For example if you have switch statement with more than 10 case labels,
28141 each case label adds one to the complexity level.
28142 The complexity level is by no means an absolute measure of the algorithmic
28143 complexity of the function, it does however provide a good starting point
28144 for which functions you might look at for further optimization.
28147 Retargetting for other Processors
28150 The issues for retargetting the compiler are far too numerous to be covered
28152 What follows is a brief description of each of the seven phases of the
28153 compiler and its MCU dependency.
28156 Parsing the source and building the annotated parse tree.
28157 This phase is largely MCU independent (except for the language extensions).
28158 Syntax & semantic checks are also done in this phase, along with some initial
28159 optimizations like back patching labels and the pattern matching optimizations
28160 like bit-rotation etc.
28163 The second phase involves generating an intermediate code which can be easy
28164 manipulated during the later phases.
28165 This phase is entirely MCU independent.
28166 The intermediate code generation assumes the target machine has unlimited
28167 number of registers, and designates them with the name iTemp.
28168 The compiler can be made to dump a human readable form of the code generated
28182 This phase does the bulk of the standard optimizations and is also MCU independe
28184 This phase can be broken down into several sub-phases:
28188 Break down intermediate code (iCode) into basic blocks.
28190 Do control flow & data flow analysis on the basic blocks.
28192 Do local common subexpression elimination, then global subexpression elimination
28194 Dead code elimination
28198 If loop optimizations caused any changes then do 'global subexpression eliminati
28199 on' and 'dead code elimination' again.
28202 This phase determines the live-ranges; by live range I mean those iTemp
28203 variables defined by the compiler that still survive after all the optimization
28205 Live range analysis
28206 \begin_inset LatexCommand \index{Live range analysis}
28210 is essential for register allocation, since these computation determines
28211 which of these iTemps will be assigned to registers, and for how long.
28214 Phase five is register allocation.
28215 There are two parts to this process.
28219 The first part I call 'register packing' (for lack of a better term).
28220 In this case several MCU specific expression folding is done to reduce
28225 The second part is more MCU independent and deals with allocating registers
28226 to the remaining live ranges.
28227 A lot of MCU specific code does creep into this phase because of the limited
28228 number of index registers available in the 8051.
28231 The Code generation phase is (unhappily), entirely MCU dependent and very
28232 little (if any at all) of this code can be reused for other MCU.
28233 However the scheme for allocating a homogenized assembler operand for each
28234 iCode operand may be reused.
28237 As mentioned in the optimization section the peep-hole optimizer is rule
28238 based system, which can reprogrammed for other MCUs.
28242 \begin_inset LatexCommand \index{Compiler internals}
28249 The anatomy of the compiler
28250 \begin_inset LatexCommand \label{sub:The-anatomy-of}
28259 This is an excerpt from an article published in Circuit Cellar Magazine
28265 It's a little outdated (the compiler is much more efficient now and user/develo
28266 per friendly), but pretty well exposes the guts of it all.
28272 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
28273 It is fairly easy to retarget for other 8-bit MCU.
28274 Here we take a look at some of the internals of the compiler.
28279 \begin_inset LatexCommand \index{Parsing}
28286 Parsing the input source file and creating an AST (Annotated Syntax Tree
28287 \begin_inset LatexCommand \index{Annotated syntax tree}
28292 This phase also involves propagating types (annotating each node of the
28293 parse tree with type information) and semantic analysis.
28294 There are some MCU specific parsing rules.
28295 For example the storage classes, the extended storage classes are MCU specific
28296 while there may be a xdata storage class for 8051 there is no such storage
28297 class for z80 or Atmel AVR.
28298 SDCC allows MCU specific storage class extensions, i.e.
28299 xdata will be treated as a storage class specifier when parsing 8051 C
28300 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
28305 \begin_inset LatexCommand \index{iCode}
28312 Intermediate code generation.
28313 In this phase the AST is broken down into three-operand form (iCode).
28314 These three operand forms are represented as doubly linked lists.
28315 ICode is the term given to the intermediate form generated by the compiler.
28316 ICode example section shows some examples of iCode generated for some simple
28317 C source functions.
28321 \begin_inset LatexCommand \index{Optimizations}
28328 Bulk of the target independent optimizations is performed in this phase.
28329 The optimizations include constant propagation, common sub-expression eliminati
28330 on, loop invariant code movement, strength reduction of loop induction variables
28331 and dead-code elimination.
28334 Live range analysis
28335 \begin_inset LatexCommand \index{Live range analysis}
28342 During intermediate code generation phase, the compiler assumes the target
28343 machine has infinite number of registers and generates a lot of temporary
28345 The live range computation determines the lifetime of each of these compiler-ge
28346 nerated temporaries.
28347 A picture speaks a thousand words.
28348 ICode example sections show the live range annotations for each of the
28350 It is important to note here, each iCode is assigned a number in the order
28351 of its execution in the function.
28352 The live ranges are computed in terms of these numbers.
28353 The from number is the number of the iCode which first defines the operand
28354 and the to number signifies the iCode which uses this operand last.
28357 Register Allocation
28358 \begin_inset LatexCommand \index{Register allocation}
28365 The register allocation determines the type and number of registers needed
28367 In most MCUs only a few registers can be used for indirect addressing.
28368 In case of 8051 for example the registers R0 & R1 can be used to indirectly
28369 address the internal ram and DPTR to indirectly address the external ram.
28370 The compiler will try to allocate the appropriate register to pointer variables
28372 ICode example section shows the operands annotated with the registers assigned
28374 The compiler will try to keep operands in registers as much as possible;
28375 there are several schemes the compiler uses to do achieve this.
28376 When the compiler runs out of registers the compiler will check to see
28377 if there are any live operands which is not used or defined in the current
28378 basic block being processed, if there are any found then it will push that
28379 operand and use the registers in this block, the operand will then be popped
28380 at the end of the basic block.
28384 There are other MCU specific considerations in this phase.
28385 Some MCUs have an accumulator; very short-lived operands could be assigned
28386 to the accumulator instead of a general-purpose register.
28392 Figure II gives a table of iCode operations supported by the compiler.
28393 The code generation involves translating these operations into corresponding
28394 assembly code for the processor.
28395 This sounds overly simple but that is the essence of code generation.
28396 Some of the iCode operations are generated on a MCU specific manner for
28397 example, the z80 port does not use registers to pass parameters so the
28398 SEND and RECV iCode operations will not be generated, and it also does
28399 not support JUMPTABLES.
28406 <Where is Figure II?>
28409 In the original article Figure II was announced to be downloadable on
28414 Unfortunately it never seemed to have shown up there, so: where is Figure
28419 \begin_inset LatexCommand \index{iCode}
28426 This section shows some details of iCode.
28427 The example C code does not do anything useful; it is used as an example
28428 to illustrate the intermediate code generated by the compiler.
28440 /* This function does nothing useful.
28447 for the purpose of explaining iCode */
28450 short function (data int *x)
28458 short i=10; \SpecialChar ~
28460 /* dead initialization eliminated */
28465 short sum=10; /* dead initialization eliminated */
28478 while (*x) *x++ = *p++;
28492 /* compiler detects i,j to be induction variables */
28496 for (i = 0, j = 10 ; i < 10 ; i++, j
28522 mul += i * 3; \SpecialChar ~
28524 /* this multiplication remains */
28530 gint += j * 3;\SpecialChar ~
28532 /* this multiplication changed to addition */
28546 In addition to the operands each iCode contains information about the filename
28547 and line it corresponds to in the source file.
28548 The first field in the listing should be interpreted as follows:
28553 Filename(linenumber: iCode Execution sequence number : ICode hash table
28554 key : loop depth of the iCode).
28559 Then follows the human readable form of the ICode operation.
28560 Each operand of this triplet form can be of three basic types a) compiler
28561 generated temporary b) user defined variable c) a constant value.
28562 Note that local variables and parameters are replaced by compiler generated
28565 \begin_inset LatexCommand \index{Live range analysis}
28569 are computed only for temporaries (i.e.
28570 live ranges are not computed for global variables).
28572 \begin_inset LatexCommand \index{Register allocation}
28576 are allocated for temporaries only.
28577 Operands are formatted in the following manner:
28582 Operand Name [lr live-from : live-to ] { type information } [ registers
28588 As mentioned earlier the live ranges are computed in terms of the execution
28589 sequence number of the iCodes, for example
28591 the iTemp0 is live from (i.e.
28592 first defined in iCode with execution sequence number 3, and is last used
28593 in the iCode with sequence number 5).
28594 For induction variables such as iTemp21 the live range computation extends
28595 the lifetime from the start to the end of the loop.
28597 The register allocator used the live range information to allocate registers,
28598 the same registers may be used for different temporaries if their live
28599 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
28600 iTemp17 since their live ranges do not overlap.
28601 In addition the allocator also takes into consideration the type and usage
28602 of a temporary, for example itemp6 is a pointer to near space and is used
28603 as to fetch data from (i.e.
28604 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
28605 Some short lived temporaries are allocated to special registers which have
28606 meaning to the code generator e.g.
28607 iTemp13 is allocated to a pseudo register CC which tells the back end that
28608 the temporary is used only for a conditional jump the code generation makes
28609 use of this information to optimize a compare and jump ICode.
28611 There are several loop optimizations
28612 \begin_inset LatexCommand \index{Loop optimization}
28616 performed by the compiler.
28617 It can detect induction variables iTemp21(i) and iTemp23(j).
28618 Also note the compiler does selective strength reduction
28619 \begin_inset LatexCommand \index{Strength reduction}
28624 the multiplication of an induction variable in line 18 (gint = j * 3) is
28625 changed to addition, a new temporary iTemp17 is allocated and assigned
28626 a initial value, a constant 3 is then added for each iteration of the loop.
28627 The compiler does not change the multiplication
28628 \begin_inset LatexCommand \index{Multiplication}
28632 in line 17 however since the processor does support an 8 * 8 bit multiplication.
28634 Note the dead code elimination
28635 \begin_inset LatexCommand \index{Dead-code elimination}
28639 optimization eliminated the dead assignments in line 7 & 8 to I and sum
28647 Sample.c (5:1:0:0) _entry($9) :
28652 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
28657 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
28662 Sample.c(11:4:53:0) preHeaderLbl0($11) :
28667 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
28673 Sample.c(11:6:5:1) _whilecontinue_0($1) :
28678 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
28684 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28689 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
28695 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
28701 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
28707 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
28713 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
28714 * int}[r0] + 0x2 {short}
28719 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
28724 Sample.c(11:17:21:0)_whilebreak_0($3) :
28729 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28734 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28739 Sample.c(15:20:54:0)preHeaderLbl1($13) :
28744 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28749 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28754 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28759 Sample.c(15:24:26:1)_forcond_0($4) :
28764 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
28770 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28775 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
28776 + ITemp21 [lr21:38]{short}[r4]
28781 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
28787 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
28788 + iTemp15 [lr29:30]{short}[r1]
28793 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
28799 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
28805 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
28811 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
28817 Sample.c(19:38:47:1) goto _forcond_0($4)
28822 Sample.c(19:39:48:0)_forbreak_0($7) :
28827 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
28828 + ITemp11 [lr19:40]{short}[r3]
28833 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
28838 Sample.c(20:42:51:0)_return($8) :
28843 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
28849 Finally the code generated for this function:
28890 ; ----------------------------------------------
28895 ; function function
28900 ; ----------------------------------------------
28910 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
28922 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
28934 ;_whilecontinue_0($1) :
28944 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28949 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
29008 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
29027 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
29074 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
29114 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
29140 ; iTemp6 [lr5:16]{_near * int}[r0] =
29145 ; iTemp6 [lr5:16]{_near * int}[r0] +
29162 ; goto _whilecontinue_0($1)
29174 ; _whilebreak_0($3) :
29184 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29196 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29208 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29220 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29239 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29268 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
29273 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29318 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
29323 ; iTemp21 [lr21:38]{short}[r4]
29349 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
29382 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
29387 ; iTemp15 [lr29:30]{short}[r1]
29406 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
29453 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
29500 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
29512 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
29526 cjne r5,#0xff,00104$
29538 ; goto _forcond_0($4)
29550 ; _forbreak_0($7) :
29560 ; ret iTemp24 [lr40:41]{short}
29603 A few words about basic block successors, predecessors and dominators
29606 Successors are basic blocks
29607 \begin_inset LatexCommand \index{Basic blocks}
29611 that might execute after this basic block.
29613 Predecessors are basic blocks that might execute before reaching this basic
29616 Dominators are basic blocks that WILL execute before reaching this basic
29650 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
29653 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
29656 c) domVect of [BB4] = BB1 ...
29657 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
29665 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
29675 Thanks to all the other volunteer developers who have helped with coding,
29676 testing, web-page creation, distribution sets, etc.
29677 You know who you are :-)
29684 This document was initially written by Sandeep Dutta
29687 All product names mentioned herein may be trademarks
29688 \begin_inset LatexCommand \index{Trademarks}
29692 of their respective companies.
29699 To avoid confusion, the installation and building options for SDCC itself
29700 (chapter 2) are not part of the index.
29704 \begin_inset LatexCommand \printindex{}