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
1966 -host=i586-mingw32msvc -
1976 -build=unknown-unknown-linux-gnu
1980 \begin_inset Quotes sld
1984 \begin_inset Quotes srd
1987 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1996 \begin_inset Quotes srd
2000 \begin_inset Quotes srd
2008 \begin_inset Quotes srd
2012 \begin_inset Quotes srd
2030 \begin_inset Quotes srd
2034 \begin_inset Quotes srd
2052 \begin_inset Quotes srd
2056 \begin_inset Quotes srd
2064 \begin_inset Quotes srd
2068 \begin_inset Quotes srd
2076 \begin_inset Quotes srd
2080 \begin_inset Quotes srd
2088 \begin_inset Quotes srd
2092 \begin_inset Quotes srd
2099 sdccconf_h_dir_separator=
2100 \begin_inset Quotes srd
2112 \begin_inset Quotes srd
2132 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
2143 -C' turns on caching, which gives a little bit extra speed.
2144 However if options are changed, it can be necessary to delete the config.cache
2149 \begin_inset LatexCommand \label{sub:Install-paths}
2154 \begin_inset LatexCommand \index{Install paths}
2160 \added_space_top medskip \align center
2162 \begin_inset Tabular
2163 <lyxtabular version="3" rows="5" columns="4">
2165 <column alignment="left" valignment="top" leftline="true" width="0">
2166 <column alignment="left" valignment="top" leftline="true" width="0">
2167 <column alignment="left" valignment="top" leftline="true" width="0">
2168 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
2169 <row topline="true" bottomline="true">
2170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2180 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2190 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2200 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2211 <row topline="true">
2212 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2220 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2251 <row topline="true">
2252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2267 $DATADIR/ $INCLUDE_DIR_SUFFIX
2270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2275 /usr/local/share/sdcc/include
2278 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2291 <row topline="true">
2292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2300 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2307 $DATADIR/$LIB_DIR_SUFFIX
2310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2315 /usr/local/share/sdcc/lib
2318 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2331 <row topline="true" bottomline="true">
2332 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2340 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2350 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2355 /usr/local/share/sdcc/doc
2358 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2380 *compiler, preprocessor, assembler, and linker
2386 is auto-appended by the compiler, e.g.
2387 small, large, z80, ds390 etc
2390 The install paths can still be changed during `make install` with e.g.:
2393 make install prefix=$(HOME)/local/sdcc
2396 Of course this doesn't change the search paths compiled into the binaries.
2400 Moreover the install path can be changed by defining DESTDIR
2401 \begin_inset LatexCommand \index{DESTDIR}
2408 make install DESTDIR=$(HOME)/sdcc.rpm/
2411 Please note that DESTDIR must have a trailing slash!
2415 \begin_inset LatexCommand \label{sub:Search-Paths}
2420 \begin_inset LatexCommand \index{Search path}
2427 Some search paths or parts of them are determined by configure variables
2432 , see section above).
2433 Further search paths are determined by environment variables during runtime.
2436 The paths searched when running the compiler are as follows (the first catch
2442 Binary files (preprocessor, assembler and linker)
2448 \begin_inset Tabular
2449 <lyxtabular version="3" rows="4" columns="3">
2451 <column alignment="block" valignment="top" leftline="true" width="0in">
2452 <column alignment="block" valignment="top" leftline="true" width="0in">
2453 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2454 <row topline="true" bottomline="true">
2455 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2471 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2480 <row topline="true">
2481 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2491 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2510 <row topline="true">
2511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2516 Path of argv[0] (if available)
2519 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2527 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2536 <row topline="true" bottomline="true">
2537 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2553 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2578 \begin_inset Tabular
2579 <lyxtabular version="3" rows="6" columns="3">
2581 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2582 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2583 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2584 <row topline="true" bottomline="true">
2585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2593 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2601 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2610 <row topline="true">
2611 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2647 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2666 <row topline="true">
2667 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2683 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2692 <row topline="true">
2693 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2707 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2730 <row topline="true">
2731 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2812 <row topline="true" bottomline="true">
2813 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2829 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2834 /usr/local/share/sdcc/
2839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2867 -nostdinc disables the last two search paths.
2877 With the exception of
2878 \begin_inset Quotes sld
2892 \begin_inset Quotes srd
2899 is auto-appended by the compiler (e.g.
2900 small, large, z80, ds390 etc.).
2907 \begin_inset Tabular
2908 <lyxtabular version="3" rows="6" columns="3">
2910 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2911 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2912 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2913 <row topline="true" bottomline="true">
2914 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2922 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2939 <row topline="true">
2940 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2958 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2995 <row topline="true">
2996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3008 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3020 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3035 <row topline="true">
3036 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3047 $LIB_DIR_SUFFIX/<model>
3050 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3064 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3081 <row topline="true">
3082 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3097 $LIB_DIR_SUFFIX/<model>
3100 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
3153 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3209 <row topline="true" bottomline="true">
3210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3219 $LIB_DIR_SUFFIX/<model>
3222 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
3227 /usr/local/share/sdcc/
3234 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
3252 Don't delete any of the stray spaces in the table above without checking
3253 the HTML output (last line)!
3269 -nostdlib disables the last two search paths.
3273 \begin_inset LatexCommand \index{Building SDCC}
3280 Building SDCC on Linux
3281 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
3290 Download the source package
3292 either from the SDCC Subversion repository or from the nightly snapshots
3294 , it will be named something like sdcc
3305 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
3314 Bring up a command line terminal, such as xterm.
3319 Unpack the file using a command like:
3322 "tar -xvzf sdcc.src.tar.gz
3327 , this will create a sub-directory called sdcc with all of the sources.
3330 Change directory into the main SDCC directory, for example type:
3347 This configures the package for compilation on your system.
3363 All of the source packages will compile, this can take a while.
3379 This copies the binary executables, the include files, the libraries and
3380 the documentation to the install directories.
3381 Proceed with section
3382 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3389 Building SDCC on OSX 2.x
3392 Follow the instruction for Linux.
3396 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3397 )) fails to compile SDCC.
3398 Fortunately there's also gcc 2.9.x installed, which works fine.
3399 This compiler can be selected by running 'configure' with:
3402 ./configure CC=gcc2 CXX=g++2
3405 Cross compiling SDCC on Linux for Windows
3408 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3409 See section 'Configure Options'.
3412 Building SDCC using Cygwin and Mingw32
3415 For building and installing a Cygwin executable follow the instructions
3421 \begin_inset Quotes sld
3425 \begin_inset Quotes srd
3428 Win32-binary can be built, which will not need the Cygwin-DLL.
3429 For the necessary 'configure' options see section 'configure options' or
3430 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3434 In order to install Cygwin on Windows download setup.exe from
3435 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3441 \begin_inset Quotes sld
3444 default text file type
3445 \begin_inset Quotes srd
3449 \begin_inset Quotes sld
3453 \begin_inset Quotes srd
3456 and download/install at least the following packages.
3457 Some packages are selected by default, others will be automatically selected
3458 because of dependencies with the manually selected packages.
3459 Never deselect these packages!
3468 gcc ; version 3.x is fine, no need to use the old 2.9x
3471 binutils ; selected with gcc
3477 rxvt ; a nice console, which makes life much easier under windoze (see below)
3480 man ; not really needed for building SDCC, but you'll miss it sooner or
3484 less ; not really needed for building SDCC, but you'll miss it sooner or
3488 svn ; only if you use Subversion access
3491 If you want to develop something you'll need:
3494 python ; for the regression tests
3497 gdb ; the gnu debugger, together with the nice GUI
3498 \begin_inset Quotes sld
3502 \begin_inset Quotes srd
3508 openssh ; to access the CF or commit changes
3511 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3512 use autoconf-stable!
3515 rxvt is a nice console with history.
3516 Replace in your cygwin.bat the line
3535 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3538 -bg black -fg white -geometry 100x65 -e bash -
3551 Text selected with the mouse is automatically copied to the clipboard, pasting
3552 works with shift-insert.
3556 The other good tip is to make sure you have no //c/-style paths anywhere,
3557 use /cygdrive/c/ instead.
3558 Using // invokes a network lookup which is very slow.
3560 \begin_inset Quotes sld
3564 \begin_inset Quotes srd
3567 is too long, you can change it with e.g.
3573 SDCC sources use the unix line ending LF.
3574 Life is much easier, if you store the source tree on a drive which is mounted
3576 And use an editor which can handle LF-only line endings.
3577 Make sure not to commit files with windows line endings.
3578 The tabulator spacing
3579 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3583 used in the project is 8.
3584 Although a tabulator spacing of 8 is a sensible choice for programmers
3585 (it's a power of 2 and allows to display 8/16 bit signed variables without
3586 loosing columns) the plan is to move towards using only spaces in the source.
3589 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3594 Download the source package
3596 either from the SDCC Subversion repository or from the
3597 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3603 , it will be named something like sdcc
3610 SDCC is distributed with all the projects, workspaces, and files you need
3611 to build it using Visual C++ 6.0/NET (except for SDCDB and ucSim).
3612 The workspace name is 'sdcc.dsw'.
3613 Please note that as it is now, all the executables are created in a folder
3617 Once built you need to copy the executables from sdcc
3621 bin before running SDCC.
3626 WARNING: Visual studio is very picky with line terminations; it expects
3627 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3628 When using the Subversion repository it's easiest to configure the svn
3629 client to convert automatically for you.
3630 If however you are getting a message such as "This makefile was not generated
3631 by Developer Studio etc.
3633 \begin_inset Quotes srd
3636 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3637 need to convert the Unix style line endings to DOS style line endings.
3638 To do so you can use the
3639 \begin_inset Quotes sld
3643 \begin_inset Quotes srd
3646 utility freely available on the internet.
3647 Doug Hawkins reported in the sdcc-user list that this works:
3655 SDCC> unix2dos sdcc.dsw
3661 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3665 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3666 flex.exe, and gawk.exe.
3667 One good place to get them is
3668 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3676 Download the file UnxUtils
3677 \begin_inset LatexCommand \index{UnxUtils}
3682 Now you have to install the utilities and setup MSVC so it can locate the
3684 Here there are two alternatives (choose one!):
3691 a) Extract UnxUtils.zip to your C:
3693 hard disk PRESERVING the original paths, otherwise bison won't work.
3694 (If you are using WinZip make certain that 'Use folder names' is selected)
3698 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3699 in 'Show directories for:' select 'Executable files', and in the directories
3700 window add a new path: 'C:
3710 (As a side effect, you get a bunch of Unix utilities that could be useful,
3711 such as diff and patch.)
3718 This one avoids extracting a bunch of files you may not use, but requires
3723 a) Create a directory were to put the tools needed, or use a directory already
3731 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3732 to such directory WITHOUT preserving the original paths.
3733 (If you are using WinZip make certain that 'Use folder names' is not selected)
3737 c) Rename bison.exe to '_bison.exe'.
3741 d) Create a batch file 'bison.bat' in 'C:
3745 ' and add these lines:
3765 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3769 Steps 'c' and 'd' are needed because bison requires by default that the
3770 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3771 '/usr/local/share/' I think.
3772 So it is necessary to tell bison where those files are located if they
3773 are not in such directory.
3774 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3778 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3779 in 'Show directories for:' select 'Executable files', and in the directories
3780 window add a new path: 'c:
3783 Note that you can use any other path instead of 'c:
3785 util', even the path where the Visual C++ tools are, probably: 'C:
3789 Microsoft Visual Studio
3794 So you don't have to execute step 'e' :)
3798 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3799 the executables from sdcc
3803 bin, and you can compile using SDCC.
3806 Building SDCC Using Borland
3809 From the sdcc directory, run the command "make -f Makefile.bcc".
3810 This should regenerate all the .exe files in the bin directory except for
3814 If you modify any source files and need to rebuild, be aware that the dependenci
3815 es may not be correctly calculated.
3816 The safest option is to delete all .obj files and run the build again.
3817 From a Cygwin BASH prompt, this can easily be done with the command (be
3818 sure you are in the sdcc directory):
3828 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3830 ) -print -exec rm {}
3839 or on Windows NT/2000/XP from the command prompt with the command:
3846 del /s *.obj *.lib *.rul
3849 from the sdcc directory.
3852 Windows Install Using a ZIP Package
3855 Download the binary zip package from
3856 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3860 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3861 This should unpack to a group of sub-directories.
3862 An example directory structure after unpacking the mingw32 package is:
3867 bin for the executables, c:
3875 lib for the include and libraries.
3878 Adjust your environment variable PATH to include the location of the bin
3879 directory or start sdcc using the full path.
3882 Windows Install Using the Setup Program
3883 \begin_inset LatexCommand \label{sub:Windows-Install}
3890 Download the setup program
3892 sdcc-x.y.z-setup.exe
3894 for an official release from
3897 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3901 or a setup program for one of the snapshots
3903 sdcc-yyyymmdd-xxxx-setup.exe
3906 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3911 A windows typical installer will guide you through the installation process.
3915 \begin_inset LatexCommand \index{VPATH}
3922 SDCC supports the VPATH feature provided by configure and make.
3923 It allows to separate the source and build trees.
3955 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3956 # extract source to directory sdcc
3961 mkdir sdcc.build\SpecialChar ~
3970 # put output in sdcc.build
3980 ../sdcc/configure\SpecialChar ~
3988 # configure is doing all the magic!
4000 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
4001 It automagically computes the variables srcdir, top_srcdir and top_buildir
4007 the generated files will be in ~/sdcc.build, while the source files stay
4010 This is not only usefull for building different binaries, e.g.
4011 when cross compiling.
4012 It also gives you a much better overview in the source tree when all the
4013 generated files are not scattered between the source files.
4014 And the best thing is: if you want to change a file you can leave the original
4015 file untouched in the source directory.
4016 Simply copy it to the build directory, edit it, enter `make clean`, `rm
4017 Makefile.dep` and `make`.
4022 will do the rest for you!
4025 Building the Documentation
4038 -enable-doc to the configure arguments to build the documentation together
4039 with all the other stuff.
4040 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
4041 dvips and makeindex) to get the job done.
4042 Another possibility is to change to the doc directory and to type
4046 \begin_inset Quotes srd
4050 \begin_inset Quotes srd
4057 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
4060 \begin_inset LatexCommand \url{http://www.lyx.org}
4064 as editor is straightforward.
4065 Prebuilt documentation in html and pdf format is available from
4066 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
4073 Reading the Documentation
4076 Currently reading the document in pdf format is recommended, as for unknown
4077 reason the hyperlinks are working there whereas in the html version they
4084 If you should know why please drop us a note
4090 You'll find the pdf version
4091 \begin_inset LatexCommand \index{PDF version of this document}
4096 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
4104 \begin_inset LatexCommand \index{HTML version of this document}
4109 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
4115 This documentation is in some aspects different from a commercial documentation:
4119 It tries to document SDCC for several processor architectures in one document
4120 (commercially these probably would be separate documents/products).
4122 \begin_inset LatexCommand \index{Status of documentation}
4126 currently matches SDCC for mcs51 and DS390 best and does give too few informati
4128 Z80, PIC14, PIC16 and HC08.
4131 There are many references pointing away from this documentation.
4132 Don't let this distract you.
4134 was a reference like
4135 \begin_inset LatexCommand \url{http://www.opencores.org}
4139 together with a statement
4140 \begin_inset Quotes sld
4143 some processors which are targetted by SDCC can be implemented in a
4160 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
4165 \begin_inset Quotes srd
4169 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
4174 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
4179 \begin_inset Quotes sld
4182 have you ever heard of an open source compiler that compiles a subset of
4184 \begin_inset Quotes srd
4187 we expect you to have a quick look there and come back.
4188 If you read this you are on the right track.
4191 Some sections attribute more space to problems, restrictions and warnings
4192 than to the solution.
4195 The installation section and the section about the debugger is intimidating.
4198 There are still lots of typos and there are more different writing styles
4202 Testing the SDCC Compiler
4203 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
4210 The first thing you should do after installing your SDCC compiler is to
4226 \begin_inset LatexCommand \index{version}
4233 at the prompt, and the program should run and output its version like:
4238 SDCC : mcs51/z80/avr/ds390/pic16/pic14/ds400/hc08 2.5.6 #4169 (May 8 2006)
4242 If it doesn't run, or gives a message about not finding sdcc program, then
4243 you need to check over your installation.
4244 Make sure that the sdcc bin directory is in your executable search path
4245 defined by the PATH environment setting (
4250 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4257 Install trouble-shooting for suggestions
4260 Make sure that the sdcc program is in the bin folder, if not perhaps something
4261 did not install correctly.
4269 is commonly installed as described in section
4270 \begin_inset Quotes sld
4273 Install and search paths
4274 \begin_inset Quotes srd
4283 Make sure the compiler works on a very simple example.
4284 Type in the following test.c program using your favorite
4310 Compile this using the following command:
4319 If all goes well, the compiler will generate a test.asm and test.rel file.
4320 Congratulations, you've just compiled your first program with SDCC.
4321 We used the -c option to tell SDCC not to link the generated code, just
4322 to keep things simple for this step.
4330 The next step is to try it with the linker.
4340 If all goes well the compiler will link with the libraries and produce
4341 a test.ihx output file.
4346 (no test.ihx, and the linker generates warnings), then the problem is most
4355 usr/local/share/sdcc/lib directory
4362 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4369 Install trouble-shooting for suggestions).
4377 The final test is to ensure
4385 header files and libraries.
4386 Edit test.c and change it to the following:
4403 strcpy(str1, "testing");
4410 Compile this by typing
4417 This should generate a test.ihx output file, and it should give no warnings
4418 such as not finding the string.h file.
4419 If it cannot find the string.h file, then the problem is that
4423 cannot find the /usr/local/share/sdcc/include directory
4430 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4437 Install trouble-shooting section for suggestions).
4455 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4459 to find exactly where SDCC is looking for the include and lib files.
4462 Install Trouble-shooting
4463 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4468 \begin_inset LatexCommand \index{Install trouble-shooting}
4475 If SDCC does not build correctly
4478 A thing to try is starting from scratch by unpacking the .tgz source package
4479 again in an empty directory.
4487 ./configure 2>&1 | tee configure.log
4501 make 2>&1 | tee make.log
4508 If anything goes wrong, you can review the log files to locate the problem.
4509 Or a relevant part of this can be attached to an email that could be helpful
4510 when requesting help from the mailing list.
4514 \begin_inset Quotes sld
4518 \begin_inset Quotes srd
4525 \begin_inset Quotes sld
4529 \begin_inset Quotes srd
4532 command is a script that analyzes your system and performs some configuration
4533 to ensure the source package compiles on your system.
4534 It will take a few minutes to run, and will compile a few tests to determine
4535 what compiler features are installed.
4539 \begin_inset Quotes sld
4543 \begin_inset Quotes srd
4549 This runs the GNU make tool, which automatically compiles all the source
4550 packages into the final installed binary executables.
4554 \begin_inset Quotes sld
4558 \begin_inset Quotes erd
4564 This will install the compiler, other executables libraries and include
4565 files into the appropriate directories.
4567 \begin_inset LatexCommand \ref{sub:Install-paths}
4573 \begin_inset LatexCommand \ref{sub:Search-Paths}
4578 about install and search paths.
4580 On most systems you will need super-user privileges to do this.
4586 SDCC is not just a compiler, but a collection of tools by various developers.
4587 These include linkers, assemblers, simulators and other components.
4588 Here is a summary of some of the components.
4589 Note that the included simulator and assembler have separate documentation
4590 which you can find in the source package in their respective directories.
4591 As SDCC grows to include support for other processors, other packages from
4592 various developers are included and may have their own sets of documentation.
4596 You might want to look at the files which are installed in <installdir>.
4597 At the time of this writing, we find the following programs for gcc-builds:
4601 In <installdir>/bin:
4604 sdcc - The compiler.
4607 sdcpp - The C preprocessor.
4610 asx8051 - The assembler for 8051 type processors.
4617 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4620 aslink -The linker for 8051 type processors.
4627 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4630 s51 - The ucSim 8051 simulator.
4633 sdcdb - The source debugger.
4636 packihx - A tool to pack (compress) Intel hex files.
4639 In <installdir>/share/sdcc/include
4645 In <installdir>/share/sdcc/lib
4648 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4652 In <installdir>/share/sdcc/doc
4658 As development for other processors proceeds, this list will expand to include
4659 executables to support processors like AVR, PIC, etc.
4665 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4666 the assembler and linkage editor.
4669 sdcpp - The C-Preprocessor
4673 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4677 is a modified version of the GNU preprocessor.
4678 The C preprocessor is used to pull in #include sources, process #ifdef
4679 statements, #defines and so on.
4690 - The Assemblers and Linkage Editors
4693 This is retargettable assembler & linkage editor, it was developed by Alan
4695 John Hartman created the version for 8051, and I (Sandeep) have made some
4696 enhancements and bug fixes for it to work properly with SDCC.
4703 \begin_inset LatexCommand \index{s51}
4707 is a freeware, opensource simulator developed by Daniel Drotos.
4708 The simulator is built as part of the build process.
4709 For more information visit Daniel's web site at:
4710 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4715 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4719 sdcdb - Source Level Debugger
4723 \begin_inset LatexCommand \index{SDCDB (debugger)}
4727 is the companion source level debugger.
4728 More about SDCDB in section
4729 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4734 The current version of the debugger uses Daniel's Simulator S51
4735 \begin_inset LatexCommand \index{s51}
4739 , but can be easily changed to use other simulators.
4748 Single Source File Projects
4751 For single source file 8051 projects the process is very simple.
4752 Compile your programs with the following command
4755 "sdcc sourcefile.c".
4759 This will compile, assemble and link your source file.
4760 Output files are as follows:
4764 \begin_inset LatexCommand \index{<file>.asm}
4769 \begin_inset LatexCommand \index{Assembler source}
4773 file created by the compiler
4777 \begin_inset LatexCommand \index{<file>.lst}
4782 \begin_inset LatexCommand \index{Assembler listing}
4786 file created by the Assembler
4790 \begin_inset LatexCommand \index{<file>.rst}
4795 \begin_inset LatexCommand \index{Assembler listing}
4799 file updated with linkedit information, created by linkage editor
4803 \begin_inset LatexCommand \index{<file>.sym}
4808 \begin_inset LatexCommand \index{Symbol listing}
4812 for the sourcefile, created by the assembler
4816 \begin_inset LatexCommand \index{<file>.rel}
4821 \begin_inset LatexCommand \index{<file>.o}
4826 \begin_inset LatexCommand \index{Object file}
4830 created by the assembler, input to Linkage editor
4834 \begin_inset LatexCommand \index{<file>.map}
4839 \begin_inset LatexCommand \index{Memory map}
4843 for the load module, created by the Linker
4847 \begin_inset LatexCommand \index{<file>.mem}
4851 - A file with a summary of the memory usage
4855 \begin_inset LatexCommand \index{<file>.ihx}
4859 - The load module in Intel hex format
4860 \begin_inset LatexCommand \index{Intel hex format}
4864 (you can select the Motorola S19 format
4865 \begin_inset LatexCommand \index{Motorola S19 format}
4880 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4885 If you need another format you might want to use
4892 \begin_inset LatexCommand \index{objdump (tool)}
4903 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4908 Both formats are documented in the documentation of srecord
4909 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4917 \begin_inset LatexCommand \index{<file>.adb}
4921 - An intermediate file containing debug information needed to create the
4933 \begin_inset LatexCommand \index{-\/-debug}
4941 \begin_inset LatexCommand \index{<file>.cdb}
4945 - An optional file (with -
4955 -debug) containing debug information.
4956 The format is documented in cdbfileformat.pdf
4961 \begin_inset LatexCommand \index{<file> (no extension)}
4965 An optional AOMF or AOMF51
4966 \begin_inset LatexCommand \index{AOMF, AOMF51}
4971 \begin_inset LatexCommand \label{OMF file}
4975 file containing debug information (generated with option -
5002 ormat is commonly used by third party tools (debuggers
5003 \begin_inset LatexCommand \index{Debugger}
5007 , simulators, emulators)
5011 \begin_inset LatexCommand \index{<file>.dump*}
5015 - Dump file to debug the compiler it self (generated with option -
5025 -dumpall) (see section
5026 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
5032 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
5038 \begin_inset Quotes sld
5041 Anatomy of the compiler
5042 \begin_inset Quotes srd
5048 Projects with Multiple Source Files
5051 SDCC can compile only ONE file at a time.
5052 Let us for example assume that you have a project containing the following
5057 foo1.c (contains some functions)
5059 foo2.c (contains some more functions)
5061 foomain.c (contains more functions and the function main)
5069 The first two files will need to be compiled separately with the commands:
5101 Then compile the source file containing the
5106 \begin_inset LatexCommand \index{Linker}
5110 the files together with the following command:
5118 foomain.c\SpecialChar ~
5119 foo1.rel\SpecialChar ~
5124 \begin_inset LatexCommand \index{<file>.rel}
5136 can be separately compiled as well:
5147 sdcc foomain.rel foo1.rel foo2.rel
5154 The file containing the
5169 file specified in the command line, since the linkage editor processes
5170 file in the order they are presented to it.
5171 The linker is invoked from SDCC using a script file with extension .lnk
5172 \begin_inset LatexCommand \index{<file>.lnk}
5177 You can view this file to troubleshoot linking problems such as those arising
5178 from missing libraries.
5181 Projects with Additional Libraries
5182 \begin_inset LatexCommand \index{Libraries}
5189 Some reusable routines may be compiled into a library, see the documentation
5190 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
5194 \begin_inset LatexCommand \index{<file>.lib}
5201 Libraries created in this manner can be included in the command line.
5202 Make sure you include the -L <library-path> option to tell the linker where
5203 to look for these files if they are not in the current directory.
5204 Here is an example, assuming you have the source file
5216 (if that is not the same as your current project):
5223 sdcc foomain.c foolib.lib -L mylib
5234 must be an absolute path name.
5238 The most efficient way to use libraries is to keep separate modules in separate
5240 The lib file now should name all the modules.rel
5241 \begin_inset LatexCommand \index{<file>.rel}
5246 For an example see the standard library file
5250 in the directory <installdir>/share/lib/small.
5253 Using sdcclib to Create and Manage Libraries
5254 \begin_inset LatexCommand \index{sdcclib}
5261 Alternatively, instead of having a .rel file for each entry on the library
5262 file as described in the preceding section, sdcclib can be used to embed
5263 all the modules belonging to such library in the library file itself.
5264 This results in a larger library file, but it greatly reduces the number
5265 of disk files accessed by the linker.
5266 Additionally, the packed library file contains an index of all include
5267 modules and symbols that significantly speeds up the linking process.
5268 To display a list of options supported by sdcclib type:
5277 \begin_inset LatexCommand \index{sdcclib}
5288 To create a new library file, start by compiling all the required modules.
5326 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
5328 The next step is to add the .rel files to the library file:
5336 sdcclib libint.lib _divsint.rel
5339 \begin_inset LatexCommand \index{sdcclib}
5349 sdcclib libint.lib _divuint.rel
5355 sdcclib libint.lib _modsint.rel
5361 sdcclib libint.lib _moduint.rel
5367 sdcclib libint.lib _mulint.rel
5374 If the file already exists in the library, it will be replaced.
5375 To see what modules and symbols are included in the library, options -s
5376 and -m are available.
5384 sdcclib -s libint.lib
5387 \begin_inset LatexCommand \index{sdcclib}
5497 If the source files are compiled using -
5508 \begin_inset LatexCommand \index{-\/-debug}
5512 , the corresponding debug information file .adb will be include in the library
5514 The library files created with sdcclib are plain text files, so they can
5515 be viewed with a text editor.
5516 It is not recomended to modify a library file created with sdcclib using
5517 a text editor, as there are file indexes numbers located accross the file
5518 used by the linker to quickly locate the required module to link.
5519 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5520 it can be safely deleted, since all the information required for linking
5521 is embedded in the library file itself.
5522 Library files created using sdcclib are used as described in the preceding
5526 Command Line Options
5527 \begin_inset LatexCommand \index{Command Line Options}
5534 Processor Selection Options
5535 \begin_inset LatexCommand \index{Options processor selection}
5540 \begin_inset LatexCommand \index{Processor selection options}
5546 \labelwidthstring 00.00.0000
5551 \begin_inset LatexCommand \index{-mmcs51}
5557 Generate code for the Intel MCS51
5558 \begin_inset LatexCommand \index{MCS51}
5562 family of processors.
5563 This is the default processor target.
5565 \labelwidthstring 00.00.0000
5570 \begin_inset LatexCommand \index{-mds390}
5576 Generate code for the Dallas DS80C390
5577 \begin_inset LatexCommand \index{DS80C390}
5583 \labelwidthstring 00.00.0000
5588 \begin_inset LatexCommand \index{-mds400}
5594 Generate code for the Dallas DS80C400
5595 \begin_inset LatexCommand \index{DS80C400}
5601 \labelwidthstring 00.00.0000
5606 \begin_inset LatexCommand \index{-mhc08}
5612 Generate code for the Freescale/Motorola HC08
5613 \begin_inset LatexCommand \index{HC08}
5617 family of processors.
5619 \labelwidthstring 00.00.0000
5624 \begin_inset LatexCommand \index{-mz80}
5630 Generate code for the Zilog Z80
5631 \begin_inset LatexCommand \index{Z80}
5635 family of processors.
5637 \labelwidthstring 00.00.0000
5642 \begin_inset LatexCommand \index{-mgbz80}
5648 Generate code for the GameBoy Z80
5649 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5653 processor (Not actively maintained).
5655 \labelwidthstring 00.00.0000
5660 \begin_inset LatexCommand \index{-mavr}
5666 Generate code for the Atmel AVR
5667 \begin_inset LatexCommand \index{AVR}
5671 processor (In development, not complete).
5672 AVR users should probably have a look at winavr
5673 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5678 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5685 I think it is fair to direct users there for now.
5686 Open source is also about avoiding unnecessary work .
5687 But I didn't find the 'official' link.
5689 \labelwidthstring 00.00.0000
5694 \begin_inset LatexCommand \index{-mpic14}
5700 Generate code for the Microchip PIC 14
5701 \begin_inset LatexCommand \index{PIC14}
5705 -bit processors (p16f84 and variants.
5706 In development, not complete).
5709 p16f627 p16f628 p16f84 p16f873 p16f877?
5711 \labelwidthstring 00.00.0000
5716 \begin_inset LatexCommand \index{-mpic16}
5722 Generate code for the Microchip PIC 16
5723 \begin_inset LatexCommand \index{PIC16}
5727 -bit processors (p18f452 and variants.
5728 In development, not complete).
5730 \labelwidthstring 00.00.0000
5736 Generate code for the Toshiba TLCS-900H
5737 \begin_inset LatexCommand \index{TLCS-900H}
5741 processor (Not maintained, not complete).
5743 \labelwidthstring 00.00.0000
5748 \begin_inset LatexCommand \index{-mxa51}
5754 Generate code for the Phillips XA51
5755 \begin_inset LatexCommand \index{XA51}
5759 processor (Not maintained, not complete).
5762 Preprocessor Options
5763 \begin_inset LatexCommand \index{Options preprocessor}
5768 \begin_inset LatexCommand \index{Preprocessor options}
5773 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5779 \labelwidthstring 00.00.0000
5784 \begin_inset LatexCommand \index{-I<path>}
5790 The additional location where the pre processor will look for <..h> or
5791 \begin_inset Quotes eld
5795 \begin_inset Quotes erd
5800 \labelwidthstring 00.00.0000
5805 \begin_inset LatexCommand \index{-D<macro[=value]>}
5811 Command line definition of macros.
5812 Passed to the preprocessor.
5814 \labelwidthstring 00.00.0000
5819 \begin_inset LatexCommand \index{-M}
5825 Tell the preprocessor to output a rule suitable for make describing the
5826 dependencies of each object file.
5827 For each source file, the preprocessor outputs one make-rule whose target
5828 is the object file name for that source file and whose dependencies are
5829 all the files `#include'd in it.
5830 This rule may be a single line or may be continued with `
5832 '-newline if it is long.
5833 The list of rules is printed on standard output instead of the preprocessed
5836 \begin_inset LatexCommand \index{-E}
5842 \labelwidthstring 00.00.0000
5847 \begin_inset LatexCommand \index{-C}
5853 Tell the preprocessor not to discard comments.
5854 Used with the `-E' option.
5856 \labelwidthstring 00.00.0000
5861 \begin_inset LatexCommand \index{-MM}
5872 Like `-M' but the output mentions only the user header files included with
5874 \begin_inset Quotes eld
5878 System header files included with `#include <file>' are omitted.
5880 \labelwidthstring 00.00.0000
5885 \begin_inset LatexCommand \index{-Aquestion(answer)}
5891 Assert the answer answer for question, in case it is tested with a preprocessor
5892 conditional such as `#if #question(answer)'.
5893 `-A-' disables the standard assertions that normally describe the target
5896 \labelwidthstring 00.00.0000
5901 \begin_inset LatexCommand \index{-Umacro}
5907 Undefine macro macro.
5908 `-U' options are evaluated after all `-D' options, but before any `-include'
5909 and `-imacros' options.
5911 \labelwidthstring 00.00.0000
5916 \begin_inset LatexCommand \index{-dM}
5922 Tell the preprocessor to output only a list of the macro definitions that
5923 are in effect at the end of preprocessing.
5924 Used with the `-E' option.
5926 \labelwidthstring 00.00.0000
5931 \begin_inset LatexCommand \index{-dD}
5937 Tell the preprocessor to pass all macro definitions into the output, in
5938 their proper sequence in the rest of the output.
5940 \labelwidthstring 00.00.0000
5945 \begin_inset LatexCommand \index{-dN}
5956 Like `-dD' except that the macro arguments and contents are omitted.
5957 Only `#define name' is included in the output.
5959 \labelwidthstring 00.00.0000
5964 preprocessorOption[,preprocessorOption]
5967 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5972 Pass the preprocessorOption to the preprocessor
5977 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5982 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5983 Collection (gcc), if you need more dedicated options please refer to the
5985 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5993 \begin_inset LatexCommand \index{Options linker}
5998 \begin_inset LatexCommand \index{Linker options}
6004 \labelwidthstring 00.00.0000
6024 \begin_inset LatexCommand \index{-\/-lib-path <path>}
6029 \begin_inset LatexCommand \index{-L -\/-lib-path}
6036 <absolute path to additional libraries> This option is passed to the linkage
6037 editor's additional libraries
6038 \begin_inset LatexCommand \index{Libraries}
6043 The path name must be absolute.
6044 Additional library files may be specified in the command line.
6045 See section Compiling programs for more details.
6047 \labelwidthstring 00.00.0000
6064 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
6069 <Value> The start location of the external ram
6070 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
6074 , default value is 0.
6075 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6085 -xram-loc 0x8000 or -
6097 \labelwidthstring 00.00.0000
6114 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
6119 <Value> The start location of the code
6120 \begin_inset LatexCommand \index{code}
6124 segment, default value 0.
6125 Note when this option is used the interrupt vector table is also relocated
6126 to the given address.
6127 The value entered can be in Hexadecimal or Decimal format, e.g.: -
6137 -code-loc 0x8000 or -
6149 \labelwidthstring 00.00.0000
6166 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
6171 <Value> By default the stack
6172 \begin_inset LatexCommand \index{stack}
6176 is placed after the data segment.
6177 Using this option the stack can be placed anywhere in the internal memory
6179 The value entered can be in Hexadecimal or Decimal format, e.g.
6190 -stack-loc 0x20 or -
6201 Since the sp register is incremented before a push or call, the initial
6202 sp will be set to one byte prior the provided value.
6203 The provided value should not overlap any other memory areas such as used
6204 register banks or the data segment and with enough space for the current
6222 \begin_inset LatexCommand \index{-\/-pack-iram}
6226 option (which is now a default setting) will override this setting, so
6227 you should also specify the
6243 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6247 option if you need to manually place the stack.
6249 \labelwidthstring 00.00.0000
6266 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
6271 <Value> By default the external stack
6272 \begin_inset LatexCommand \index{xstack}
6276 is placed after the pdata segment.
6277 Using this option the xstack can be placed anywhere in the external memory
6279 The value entered can be in Hexadecimal or Decimal format, e.g.
6290 -xstack-loc 0x8000 or -
6301 The provided value should not overlap any other memory areas such as the
6302 pdata or xdata segment and with enough space for the current application.
6304 \labelwidthstring 00.00.0000
6321 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
6326 <Value> The start location of the internal ram data
6327 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
6332 The value entered can be in Hexadecimal or Decimal format, eg.
6354 (By default, the start location of the internal ram data segment is set
6355 as low as possible in memory, taking into account the used register banks
6356 and the bit segment at address 0x20.
6357 For example if register banks 0 and 1 are used without bit variables, the
6358 data segment will be set, if -
6368 -data-loc is not used, to location 0x10.)
6370 \labelwidthstring 00.00.0000
6387 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6392 <Value> The start location of the indirectly addressable internal ram
6393 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6397 of the 8051, default value is 0x80.
6398 The value entered can be in Hexadecimal or Decimal format, eg.
6409 -idata-loc 0x88 or -
6421 \labelwidthstring 00.00.0000
6438 <Value> The start location of the bit
6439 \begin_inset LatexCommand \index{bit}
6443 addressable internal ram of the 8051.
6449 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6452 \labelwidthstring 00.00.0000
6467 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6476 The linker output (final object code) is in Intel Hex format.
6477 \begin_inset LatexCommand \index{Intel hex format}
6481 This is the default option.
6482 The format itself is documented in the documentation of srecord
6483 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6489 \labelwidthstring 00.00.0000
6504 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6513 The linker output (final object code) is in Motorola S19 format
6514 \begin_inset LatexCommand \index{Motorola S19 format}
6519 The format itself is documented in the documentation of srecord.
6521 \labelwidthstring 00.00.0000
6536 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6545 The linker output (final object code) is in ELF format
6546 \begin_inset LatexCommand \index{ELF format}
6551 (Currently only supported for the HC08 processors)
6553 \labelwidthstring 00.00.0000
6558 linkOption[,linkOption]
6561 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6566 Pass the linkOption to the linker.
6567 See file sdcc/as/doc/asxhtm.html for more on linker options.
6571 \begin_inset LatexCommand \index{Options MCS51}
6576 \begin_inset LatexCommand \index{MCS51 options}
6582 \labelwidthstring 00.00.0000
6597 \begin_inset LatexCommand \index{-\/-model-small}
6608 Generate code for Small Model programs, see section Memory Models for more
6610 This is the default model.
6612 \labelwidthstring 00.00.0000
6627 \begin_inset LatexCommand \index{-\/-model-medium}
6633 Generate code for Medium model programs, see section Memory Models for
6635 If this option is used all source files in the project have to be compiled
6637 It must also be used when invoking the linker.
6639 \labelwidthstring 00.00.0000
6654 \begin_inset LatexCommand \index{-\/-model-large}
6660 Generate code for Large model programs, see section Memory Models for more
6662 If this option is used all source files in the project have to be compiled
6664 It must also be used when invoking the linker.
6666 \labelwidthstring 00.00.0000
6681 \begin_inset LatexCommand \index{-\/-xstack}
6687 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6688 variables and passing parameters.
6690 \begin_inset LatexCommand \ref{sub:External-Stack}
6695 External Stack for more details.
6697 \labelwidthstring 00.00.0000
6715 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6719 Causes the linker to check if the internal ram usage is within limits of
6722 \labelwidthstring 00.00.0000
6740 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6744 Causes the linker to check if the external ram usage is within limits of
6747 \labelwidthstring 00.00.0000
6765 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6769 Causes the linker to check if the code memory usage is within limits of
6772 \labelwidthstring 00.00.0000
6790 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6794 Causes the linker to check if there is at minimum <Value> bytes for stack.
6796 \labelwidthstring 00.00.0000
6814 \begin_inset LatexCommand \index{-\/-pack-iram}
6818 Causes the linker to use unused register banks for data variables and pack
6819 data, idata and stack together.
6820 This is the default now.
6822 \labelwidthstring 00.00.0000
6840 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6844 Causes the linker to use old style for allocating memory areas.
6847 DS390 / DS400 Options
6848 \begin_inset LatexCommand \index{Options DS390}
6853 \begin_inset LatexCommand \index{DS390 options}
6859 \labelwidthstring 00.00.0000
6876 \begin_inset LatexCommand \index{-\/-model-flat24}
6886 Generate 24-bit flat mode code.
6887 This is the one and only that the ds390 code generator supports right now
6888 and is default when using
6893 See section Memory Models for more details.
6895 \labelwidthstring 00.00.0000
6910 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6916 disable interrupts during ESP:SP updates.
6918 \labelwidthstring 00.00.0000
6935 \begin_inset LatexCommand \index{-\/-stack-10bit}
6939 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6940 This is the one and only that the ds390 code generator supports right now
6941 and is default when using
6946 In this mode, the stack is located in the lower 1K of the internal RAM,
6947 which is mapped to 0x400000.
6948 Note that the support is incomplete, since it still uses a single byte
6949 as the stack pointer.
6950 This means that only the lower 256 bytes of the potential 1K stack space
6951 will actually be used.
6952 However, this does allow you to reclaim the precious 256 bytes of low RAM
6953 for use for the DATA and IDATA segments.
6954 The compiler will not generate any code to put the processor into 10 bit
6956 It is important to ensure that the processor is in this mode before calling
6957 any re-entrant functions compiled with this option.
6958 In principle, this should work with the
6971 \begin_inset LatexCommand \index{-\/-stack-auto}
6977 option, but that has not been tested.
6978 It is incompatible with the
6991 \begin_inset LatexCommand \index{-\/-xstack}
6998 It also only makes sense if the processor is in 24 bit contiguous addressing
7011 -model-flat24 option
7015 \labelwidthstring 00.00.0000
7030 \begin_inset LatexCommand \index{-\/-stack-probe}
7036 insert call to function __stack_probe at each function prologue.
7038 \labelwidthstring 00.00.0000
7053 \begin_inset LatexCommand \index{-\/-tini-libid}
7059 <nnnn> LibraryID used in -mTININative.
7062 \labelwidthstring 00.00.0000
7077 \begin_inset LatexCommand \index{-\/-use-accelerator}
7083 generate code for DS390 Arithmetic Accelerator.
7088 \begin_inset LatexCommand \index{Options Z80}
7093 \begin_inset LatexCommand \index{Z80 options}
7099 \labelwidthstring 00.00.0000
7116 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
7126 Force a called function to always save BC.
7128 \labelwidthstring 00.00.0000
7145 \begin_inset LatexCommand \index{-\/-no-std-crt0}
7149 When linking, skip the standard crt0.o object file.
7150 You must provide your own crt0.o for your system when linking.
7154 Optimization Options
7155 \begin_inset LatexCommand \index{Options optimization}
7160 \begin_inset LatexCommand \index{Optimization options}
7166 \labelwidthstring 00.00.0000
7181 \begin_inset LatexCommand \index{-\/-nogcse}
7187 Will not do global subexpression elimination, this option may be used when
7188 the compiler creates undesirably large stack/data spaces to store compiler
7198 \begin_inset LatexCommand \index{sloc (spill location)}
7203 A warning message will be generated when this happens and the compiler
7204 will indicate the number of extra bytes it allocated.
7205 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7207 \begin_inset LatexCommand \index{\#pragma nogcse}
7211 can be used to turn off global subexpression elimination
7212 \begin_inset LatexCommand \index{Subexpression elimination}
7216 for a given function only.
7218 \labelwidthstring 00.00.0000
7233 \begin_inset LatexCommand \index{-\/-noinvariant}
7239 Will not do loop invariant optimizations, this may be turned off for reasons
7240 explained for the previous option.
7241 For more details of loop optimizations performed see Loop Invariants in
7243 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
7248 It is recommended that this option NOT be used, #pragma\SpecialChar ~
7250 \begin_inset LatexCommand \index{\#pragma noinvariant}
7254 can be used to turn off invariant optimizations for a given function only.
7256 \labelwidthstring 00.00.0000
7271 \begin_inset LatexCommand \index{-\/-noinduction}
7277 Will not do loop induction optimizations, see section strength reduction
7279 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7281 \begin_inset LatexCommand \index{\#pragma noinduction}
7285 can be used to turn off induction optimizations for a given function only.
7287 \labelwidthstring 00.00.0000
7302 \begin_inset LatexCommand \index{-\/-nojtbound}
7313 Will not generate boundary condition check when switch statements
7314 \begin_inset LatexCommand \index{switch statement}
7318 are implemented using jump-tables.
7320 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
7325 Switch Statements for more details.
7326 It is recommended that this option is NOT used, #pragma\SpecialChar ~
7328 \begin_inset LatexCommand \index{\#pragma nojtbound}
7332 can be used to turn off boundary checking for jump tables for a given function
7335 \labelwidthstring 00.00.0000
7350 \begin_inset LatexCommand \index{-\/-noloopreverse}
7359 Will not do loop reversal
7360 \begin_inset LatexCommand \index{Loop reversing}
7366 \labelwidthstring 00.00.0000
7383 \begin_inset LatexCommand \index{-\/-nolabelopt }
7387 Will not optimize labels (makes the dumpfiles more readable).
7389 \labelwidthstring 00.00.0000
7404 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7410 Will not memcpy initialized data from code space into xdata space.
7411 This saves a few bytes in code space if you don't have initialized data
7412 \begin_inset LatexCommand \index{Variable initialization}
7418 \labelwidthstring 00.00.0000
7433 \begin_inset LatexCommand \index{-\/-nooverlay}
7439 The compiler will not overlay parameters and local variables of any function,
7440 see section Parameters and local variables for more details.
7442 \labelwidthstring 00.00.0000
7457 \begin_inset LatexCommand \index{-\/-no-peep}
7463 Disable peep-hole optimization with built-in rules.
7465 \labelwidthstring 00.00.0000
7482 \begin_inset LatexCommand \index{-\/-peep-file}
7487 <filename> This option can be used to use additional rules to be used by
7488 the peep hole optimizer.
7490 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7495 Peep Hole optimizations for details on how to write these rules.
7497 \labelwidthstring 00.00.0000
7512 \begin_inset LatexCommand \index{-\/-peep-asm}
7518 Pass the inline assembler code through the peep hole optimizer.
7519 This can cause unexpected changes to inline assembler code, please go through
7520 the peephole optimizer
7521 \begin_inset LatexCommand \index{Peephole optimizer}
7525 rules defined in the source file tree '<target>/peeph.def' before using
7528 \labelwidthstring 00.00.0000
7543 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7549 The compiler will optimize code generation towards fast code, possibly
7550 at the expense of code size.
7552 \labelwidthstring 00.00.0000
7567 \begin_inset LatexCommand \index{-\/-opt-code-size}
7573 The compiler will optimize code generation towards compact code, possibly
7574 at the expense of code speed.
7578 \begin_inset LatexCommand \index{Options other}
7584 \labelwidthstring 00.00.0000
7600 \begin_inset LatexCommand \index{-\/-compile-only}
7605 \begin_inset LatexCommand \index{-c -\/-compile-only}
7611 will compile and assemble the source, but will not call the linkage editor.
7613 \labelwidthstring 00.00.0000
7632 \begin_inset LatexCommand \index{-\/-c1mode}
7638 reads the preprocessed source from standard input and compiles it.
7639 The file name for the assembler output must be specified using the -o option.
7641 \labelwidthstring 00.00.0000
7646 \begin_inset LatexCommand \index{-E}
7652 Run only the C preprocessor.
7653 Preprocess all the C source files specified and output the results to standard
7656 \labelwidthstring 00.00.0000
7662 \begin_inset LatexCommand \index{-o <path/file>}
7668 The output path resp.
7669 file where everything will be placed.
7670 If the parameter is a path, it must have a trailing slash (or backslash
7671 for the Windows binaries) to be recognized as a path.
7674 \labelwidthstring 00.00.0000
7689 \begin_inset LatexCommand \index{-\/-stack-auto}
7700 All functions in the source file will be compiled as
7705 \begin_inset LatexCommand \index{reentrant}
7710 the parameters and local variables will be allocated on the stack
7711 \begin_inset LatexCommand \index{stack}
7717 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7721 Parameters and Local Variables for more details.
7722 If this option is used all source files in the project should be compiled
7724 It automatically implies --int-long-reent and --float-reent.
7727 \labelwidthstring 00.00.0000
7742 \begin_inset LatexCommand \index{-\/-callee-saves}
7746 function1[,function2][,function3]....
7749 The compiler by default uses a caller saves convention for register saving
7750 across function calls, however this can cause unnecessary register pushing
7751 & popping when calling small functions from larger functions.
7752 This option can be used to switch the register saving convention for the
7753 function names specified.
7754 The compiler will not save registers when calling these functions, no extra
7755 code will be generated at the entry & exit (function prologue
7758 \begin_inset LatexCommand \index{function prologue}
7767 \begin_inset LatexCommand \index{function epilogue}
7773 ) for these functions to save & restore the registers used by these functions,
7774 this can SUBSTANTIALLY reduce code & improve run time performance of the
7776 In the future the compiler (with inter procedural analysis) will be able
7777 to determine the appropriate scheme to use for each function call.
7778 DO NOT use this option for built-in functions such as _mulint..., if this
7779 option is used for a library function the appropriate library function
7780 needs to be recompiled with the same option.
7781 If the project consists of multiple source files then all the source file
7782 should be compiled with the same -
7792 -callee-saves option string.
7793 Also see #pragma\SpecialChar ~
7795 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7801 \labelwidthstring 00.00.0000
7816 \begin_inset LatexCommand \index{-\/-debug}
7825 When this option is used the compiler will generate debug information.
7826 The debug information collected in a file with .cdb extension can be used
7828 For more information see documentation for SDCDB.
7829 Another file with no extension contains debug information in AOMF or AOMF51
7830 \begin_inset LatexCommand \index{AOMF, AOMF51}
7834 format which is commonly used by third party tools.
7836 \labelwidthstring 00.00.0000
7841 \begin_inset LatexCommand \index{-S}
7852 Stop after the stage of compilation proper; do not assemble.
7853 The output is an assembler code file for the input file specified.
7855 \labelwidthstring 00.00.0000
7870 \begin_inset LatexCommand \index{-\/-int-long-reent}
7876 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7877 Note by default these libraries are compiled as non-reentrant.
7878 See section Installation for more details.
7880 \labelwidthstring 00.00.0000
7895 \begin_inset LatexCommand \index{-\/-cyclomatic}
7904 This option will cause the compiler to generate an information message for
7905 each function in the source file.
7906 The message contains some
7910 information about the function.
7911 The number of edges and nodes the compiler detected in the control flow
7912 graph of the function, and most importantly the
7914 cyclomatic complexity
7915 \begin_inset LatexCommand \index{Cyclomatic complexity}
7921 see section on Cyclomatic Complexity for more details.
7923 \labelwidthstring 00.00.0000
7938 \begin_inset LatexCommand \index{-\/-float-reent}
7944 Floating point library is compiled as reentrant
7945 \begin_inset LatexCommand \index{reentrant}
7950 See section Installation for more details.
7952 \labelwidthstring 00.00.0000
7967 \begin_inset LatexCommand \index{-\/-main-return}
7973 This option can be used if the code generated is called by a monitor program
7974 or if the main routine includes an endless loop.
7975 This option might result in slightly smaller code and save two bytes of
7977 The return from the 'main'
7978 \begin_inset LatexCommand \index{main return}
7982 function will return to the function calling main.
7983 The default setting is to lock up i.e.
7990 \labelwidthstring 00.00.0000
8005 \begin_inset LatexCommand \index{-\/-nostdinc}
8011 This will prevent the compiler from passing on the default include path
8012 to the preprocessor.
8014 \labelwidthstring 00.00.0000
8029 \begin_inset LatexCommand \index{-\/-nostdlib}
8035 This will prevent the compiler from passing on the default library
8036 \begin_inset LatexCommand \index{Libraries}
8042 \labelwidthstring 00.00.0000
8057 \begin_inset LatexCommand \index{-\/-verbose}
8063 Shows the various actions the compiler is performing.
8065 \labelwidthstring 00.00.0000
8070 \begin_inset LatexCommand \index{-V}
8076 Shows the actual commands the compiler is executing.
8078 \labelwidthstring 00.00.0000
8093 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
8099 Hides your ugly and inefficient c-code from the asm file, so you can always
8100 blame the compiler :)
8102 \labelwidthstring 00.00.0000
8117 \begin_inset LatexCommand \index{-\/-no-peep-comments}
8123 Will not include peep-hole comments in the generated files.
8125 \labelwidthstring 00.00.0000
8140 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
8146 Include i-codes in the asm file.
8147 Sounds like noise but is most helpful for debugging the compiler itself.
8149 \labelwidthstring 00.00.0000
8164 \begin_inset LatexCommand \index{-\/-less-pedantic}
8170 Disable some of the more pedantic warnings
8171 \begin_inset LatexCommand \index{Warnings}
8175 (jwk burps: please be more specific here, please!).
8177 \labelwidthstring 00.00.0000
8191 -disable-warning\SpecialChar ~
8193 \begin_inset LatexCommand \index{-\/-disable-warning}
8199 Disable specific warning with number <nnnn>.
8201 \labelwidthstring 00.00.0000
8216 \begin_inset LatexCommand \index{-\/-print-search-dirs}
8222 Display the directories in the compiler's search path
8224 \labelwidthstring 00.00.0000
8239 \begin_inset LatexCommand \index{-\/-vc}
8245 Display errors and warnings using MSVC style, so you can use SDCC with
8248 \labelwidthstring 00.00.0000
8263 \begin_inset LatexCommand \index{-\/-use-stdout}
8269 Send errors and warnings to stdout instead of stderr.
8271 \labelwidthstring 00.00.0000
8276 asmOption[,asmOption]
8279 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
8284 Pass the asmOption to the assembler
8285 \begin_inset LatexCommand \index{Options assembler}
8290 \begin_inset LatexCommand \index{Assembler options}
8295 See file sdcc/as/doc/asxhtm.html for assembler options.cd
8297 \labelwidthstring 00.00.0000
8312 \begin_inset LatexCommand \index{-\/-std-sdcc89}
8318 Generally follow the C89 standard, but allow SDCC features that conflict
8319 with the standard (default).
8321 \labelwidthstring 00.00.0000
8336 \begin_inset LatexCommand \index{-\/-std-c89}
8342 Follow the C89 standard and disable SDCC features that conflict with the
8345 \labelwidthstring 00.00.0000
8360 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8366 Generally follow the C99 standard, but allow SDCC features that conflict
8367 with the standard (incomplete support).
8369 \labelwidthstring 00.00.0000
8384 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8390 Follow the C99 standard and disable SDCC features that conflict with the
8391 standard (incomplete support).
8393 \labelwidthstring 00.00.0000
8410 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8415 <Name> The name to be used for the code
8416 \begin_inset LatexCommand \index{code}
8420 segment, default CSEG.
8421 This is useful if you need to tell the compiler to put the code in a special
8422 segment so you can later on tell the linker to put this segment in a special
8424 Can be used for instance when using bank switching to put the code in a
8427 \labelwidthstring 00.00.0000
8444 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8449 <Name> The name to be used for the const
8450 \begin_inset LatexCommand \index{code}
8454 segment, default CONST.
8455 This is useful if you need to tell the compiler to put the const data in
8456 a special segment so you can later on tell the linker to put this segment
8457 in a special place in memory.
8458 Can be used for instance when using bank switching to put the const data
8461 \labelwidthstring 00.00.0000
8473 a SDCC compiler option but if you want
8477 warnings you can use a separate tool dedicated to syntax checking like
8479 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8484 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8489 \begin_inset LatexCommand \url{http://www.splint.org}
8494 To make your source files parseable by splint you will have to include
8500 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8504 in your source file and add brackets around extended keywords (like
8507 \begin_inset Quotes sld
8520 \begin_inset Quotes srd
8528 \begin_inset Quotes sld
8531 __interrupt\SpecialChar ~
8533 \begin_inset Quotes srd
8541 Splint has an excellent on line manual at
8542 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8546 and it's capabilities go beyond pure syntax checking.
8547 You'll need to tell splint the location of SDCC's include files so a typical
8548 command line could look like this:
8552 splint\SpecialChar ~
8554 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8559 Intermediate Dump Options
8560 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8565 \begin_inset LatexCommand \index{Options intermediate dump}
8570 \begin_inset LatexCommand \index{Intermediate dump options}
8577 The following options are provided for the purpose of retargetting and debugging
8579 They provide a means to dump the intermediate code (iCode
8580 \begin_inset LatexCommand \index{iCode}
8584 ) generated by the compiler in human readable form at various stages of
8585 the compilation process.
8586 More on iCodes see chapter
8587 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8592 \begin_inset Quotes srd
8595 The anatomy of the compiler
8596 \begin_inset Quotes srd
8601 \labelwidthstring 00.00.0000
8616 \begin_inset LatexCommand \index{-\/-dumpraw}
8622 This option will cause the compiler to dump the intermediate code into
8625 <source filename>.dumpraw
8627 just after the intermediate code has been generated for a function, i.e.
8628 before any optimizations are done.
8630 \begin_inset LatexCommand \index{Basic blocks}
8634 at this stage ordered in the depth first number, so they may not be in
8635 sequence of execution.
8637 \labelwidthstring 00.00.0000
8652 \begin_inset LatexCommand \index{-\/-dumpgcse}
8658 Will create a dump of iCode's, after global subexpression elimination
8659 \begin_inset LatexCommand \index{Global subexpression elimination}
8665 <source filename>.dumpgcse.
8667 \labelwidthstring 00.00.0000
8682 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8688 Will create a dump of iCode's, after deadcode elimination
8689 \begin_inset LatexCommand \index{Dead-code elimination}
8695 <source filename>.dumpdeadcode.
8697 \labelwidthstring 00.00.0000
8712 \begin_inset LatexCommand \index{-\/-dumploop}
8721 Will create a dump of iCode's, after loop optimizations
8722 \begin_inset LatexCommand \index{Loop optimization}
8728 <source filename>.dumploop.
8730 \labelwidthstring 00.00.0000
8745 \begin_inset LatexCommand \index{-\/-dumprange}
8754 Will create a dump of iCode's, after live range analysis
8755 \begin_inset LatexCommand \index{Live range analysis}
8761 <source filename>.dumprange.
8763 \labelwidthstring 00.00.0000
8778 \begin_inset LatexCommand \index{-\/-dumlrange}
8784 Will dump the life ranges
8785 \begin_inset LatexCommand \index{Live range analysis}
8791 \labelwidthstring 00.00.0000
8806 \begin_inset LatexCommand \index{-\/-dumpregassign}
8815 Will create a dump of iCode's, after register assignment
8816 \begin_inset LatexCommand \index{Register assignment}
8822 <source filename>.dumprassgn.
8824 \labelwidthstring 00.00.0000
8839 \begin_inset LatexCommand \index{-\/-dumplrange}
8845 Will create a dump of the live ranges of iTemp's
8847 \labelwidthstring 00.00.0000
8862 \begin_inset LatexCommand \index{-\/-dumpall}
8873 Will cause all the above mentioned dumps to be created.
8876 Redirecting output on Windows Shells
8879 By default SDCC writes it's error messages to
8880 \begin_inset Quotes sld
8884 \begin_inset Quotes srd
8888 To force all messages to
8889 \begin_inset Quotes sld
8893 \begin_inset Quotes srd
8917 \begin_inset LatexCommand \index{-\/-use-stdout}
8922 Additionally, if you happen to have visual studio installed in your windows
8923 machine, you can use it to compile your sources using a custom build and
8939 \begin_inset LatexCommand \index{-\/-vc}
8944 Something like this should work:
8988 -model-large -c $(InputPath)
8991 Environment variables
8992 \begin_inset LatexCommand \index{Environment variables}
8999 SDCC recognizes the following environment variables:
9001 \labelwidthstring 00.00.0000
9006 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
9012 SDCC installs a signal handler
9013 \begin_inset LatexCommand \index{signal handler}
9017 to be able to delete temporary files after an user break (^C) or an exception.
9018 If this environment variable is set, SDCC won't install the signal handler
9019 in order to be able to debug SDCC.
9021 \labelwidthstring 00.00.0000
9028 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
9034 Path, where temporary files will be created.
9035 The order of the variables is the search order.
9036 In a standard *nix environment these variables are not set, and there's
9037 no need to set them.
9038 On Windows it's recommended to set one of them.
9040 \labelwidthstring 00.00.0000
9045 \begin_inset LatexCommand \index{SDCC\_HOME}
9052 \begin_inset LatexCommand \ref{sub:Install-paths}
9058 \begin_inset Quotes sld
9062 \begin_inset Quotes srd
9067 \labelwidthstring 00.00.0000
9072 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
9079 \begin_inset LatexCommand \ref{sub:Search-Paths}
9085 \begin_inset Quotes sld
9089 \begin_inset Quotes srd
9094 \labelwidthstring 00.00.0000
9099 \begin_inset LatexCommand \index{SDCC\_LIB}
9106 \begin_inset LatexCommand \ref{sub:Search-Paths}
9112 \begin_inset Quotes sld
9116 \begin_inset Quotes srd
9122 There are some more environment variables recognized by SDCC, but these
9123 are solely used for debugging purposes.
9124 They can change or disappear very quickly, and will never be documented.
9127 Storage Class Language Extensions
9130 MCS51/DS390 Storage Class
9131 \begin_inset LatexCommand \index{Storage class}
9138 In addition to the ANSI storage classes SDCC allows the following MCS51
9139 specific storage classes:
9140 \layout Subsubsection
9143 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9148 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9153 \begin_inset LatexCommand \index{near (storage class)}
9158 \begin_inset LatexCommand \index{\_\_near (storage class)}
9169 storage class for the Small Memory model (
9177 or the more ANSI-C compliant forms
9185 can be used synonymously).
9186 Variables declared with this storage class will be allocated in the directly
9187 addressable portion of the internal RAM of a 8051, e.g.:
9192 __data unsigned char test_data;
9195 Writing 0x01 to this variable generates the assembly code:
9200 75*00 01\SpecialChar ~
9206 \layout Subsubsection
9209 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9214 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9219 \begin_inset LatexCommand \index{far (storage class)}
9224 \begin_inset LatexCommand \index{\_\_far (storage class)}
9231 Variables declared with this storage class will be placed in the external
9237 storage class for the Large Memory model, e.g.:
9242 __xdata unsigned char test_xdata;
9245 Writing 0x01 to this variable generates the assembly code:
9250 90s00r00\SpecialChar ~
9279 \layout Subsubsection
9282 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9287 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9294 Variables declared with this storage class will be allocated into the indirectly
9295 addressable portion of the internal ram of a 8051, e.g.:
9300 __idata unsigned char test_idata;
9303 Writing 0x01 to this variable generates the assembly code:
9332 Please note, the first 128 byte of idata physically access the same RAM
9334 The original 8051 had 128 byte idata memory, nowadays most devices have
9335 256 byte idata memory.
9337 \begin_inset LatexCommand \index{stack}
9341 is located in idata memory.
9342 \layout Subsubsection
9345 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
9350 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
9357 Paged xdata access is just as straightforward as using the other addressing
9359 It is typically located at the start of xdata and has a maximum size of
9361 The following example writes 0x01 to the pdata variable.
9362 Please note, pdata access physically accesses xdata memory.
9363 The high byte of the address is determined by port P2
9364 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9368 (or in case of some 8051 variants by a separate Special Function Register,
9370 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9379 storage class for the Medium Memory model, e.g.:
9384 __pdata unsigned char test_pdata;
9387 Writing 0x01 to this variable generates the assembly code:
9431 \begin_inset LatexCommand \index{-\/-xstack}
9435 option is used the pdata memory area is followed by the xstack memory area
9436 and the sum of their sizes is limited to 256 bytes.
9437 \layout Subsubsection
9440 \begin_inset LatexCommand \index{code}
9445 \begin_inset LatexCommand \index{\_\_code}
9452 'Variables' declared with this storage class will be placed in the code
9458 __code unsigned char test_code;
9461 Read access to this variable generates the assembly code:
9466 90s00r6F\SpecialChar ~
9469 mov dptr,#_test_code
9498 indexed arrays of characters in code memory can be accessed efficiently:
9503 __code char test_array[] = {'c','h','e','a','p'};
9506 Read access to this array using an 8-bit unsigned index generates the assembly
9523 90s00r41\SpecialChar ~
9526 mov dptr,#_test_array
9541 \layout Subsubsection
9544 \begin_inset LatexCommand \index{bit}
9549 \begin_inset LatexCommand \index{\_\_bit}
9556 This is a data-type and a storage class specifier.
9557 When a variable is declared as a bit, it is allocated into the bit addressable
9558 memory of 8051, e.g.:
9566 Writing 1 to this variable generates the assembly code:
9582 The bit addressable memory consists of 128 bits which are located from 0x20
9583 to 0x2f in data memory.
9586 Apart from this 8051 specific storage class most architectures support ANSI-C
9588 \begin_inset LatexCommand \index{bitfields}
9598 Not really meant as examples, but nevertheless showing what bitfields are
9599 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9603 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9604 signed modifier are implemented as unsigned.
9605 \layout Subsubsection
9608 \begin_inset LatexCommand \index{sfr}
9613 \begin_inset LatexCommand \index{\_\_sfr}
9618 \begin_inset LatexCommand \index{sfr16}
9623 \begin_inset LatexCommand \index{\_\_sfr16}
9628 \begin_inset LatexCommand \index{sfr32}
9633 \begin_inset LatexCommand \index{\_\_sfr32}
9638 \begin_inset LatexCommand \index{\_\_sbit}
9645 Like the bit keyword,
9647 sfr / sfr16 / sfr32 / sbit
9649 signify both a data-type and storage class, they are used to describe the
9670 variables of a 8051, eg:
9676 \begin_inset LatexCommand \index{at}
9681 \begin_inset LatexCommand \index{\_\_at}
9685 (0x80) P0;\SpecialChar ~
9686 /* special function register P0 at location 0x80 */
9688 /* 16 bit special function register combination for timer 0 */
9690 /* with the high byte at location 0x8C and the low byte at location 0x8A
9694 \begin_inset LatexCommand \index{at}
9699 \begin_inset LatexCommand \index{\_\_at}
9705 __sbit __at (0xd7) CY; /* CY (Carry Flag
9706 \begin_inset LatexCommand \index{Flags}
9711 \begin_inset LatexCommand \index{Carry flag}
9718 Special function registers which are located on an address dividable by
9719 8 are bit-addressable, an
9723 addresses a specific bit within these sfr.
9725 16 Bit and 32 bit special function register combinations which require a
9726 certain access order are better not declared using
9735 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9736 this is not guaranteed.
9737 \layout Subsubsection
9740 \begin_inset LatexCommand \index{Pointer}
9744 to MCS51/DS390 specific memory spaces
9747 SDCC allows (via language extensions) pointers to explicitly point to any
9748 of the memory spaces
9749 \begin_inset LatexCommand \index{Memory model}
9754 In addition to the explicit pointers, the compiler uses (by default) generic
9755 pointers which can be used to point to any of the memory spaces.
9759 Pointer declaration examples:
9764 /* pointer physically in internal ram pointing to object in external ram
9767 __xdata unsigned char * __data p;
9771 /* pointer physically in external ram pointing to object in internal ram
9774 __data unsigned char * __xdata p;
9778 /* pointer physically in code rom pointing to data in xdata space */
9780 __xdata unsigned char * __code p;
9784 /* pointer physically in code space pointing to data in code space */
9786 __code unsigned char * __code p;
9790 /* the following is a generic pointer physically located in xdata space
9797 /* the following is a function pointer physically located in data space
9800 char (* __data fp)(void);
9803 Well you get the idea.
9808 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9821 The highest order byte of the
9825 pointers contains the data space information.
9826 Assembler support routines are called whenever data is stored or retrieved
9832 These are useful for developing reusable library
9833 \begin_inset LatexCommand \index{Libraries}
9838 Explicitly specifying the pointer type will generate the most efficient
9840 \layout Subsubsection
9842 Notes on MCS51 memory
9843 \begin_inset LatexCommand \index{MCS51 memory}
9850 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9851 RAM memory which is structured as follows:
9855 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9858 - Bytes 20-2F - 16 bytes to hold 128 bit
9859 \begin_inset LatexCommand \index{bit}
9865 - Bytes 30-7F - 80 bytes for general purpose use.
9870 Additionally some members of the MCS51 family may have up to 128 bytes of
9871 additional, indirectly addressable, internal RAM memory (
9876 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9881 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9886 Furthermore, some chips may have some built in external memory (
9891 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9896 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9900 ) which should not be confused with the internal, directly addressable RAM
9906 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9911 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9916 Sometimes this built in
9920 memory has to be activated before using it (you can probably find this
9921 information on the datasheet of the microcontroller your are using, see
9923 \begin_inset LatexCommand \ref{sub:Startup-Code}
9931 Normally SDCC will only use the first bank
9932 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9936 of registers (register bank 0), but it is possible to specify that other
9937 banks of registers (keyword
9944 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9949 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9955 ) should be used in interrupt
9956 \begin_inset LatexCommand \index{interrupt}
9961 \begin_inset LatexCommand \index{\_\_interrupt}
9966 By default, the compiler will place the stack after the last byte of allocated
9967 memory for variables.
9968 For example, if the first 2 banks of registers are used, and only four
9973 variables, it will position the base of the internal stack at address 20
9975 This implies that as the stack
9976 \begin_inset LatexCommand \index{stack}
9980 grows, it will use up the remaining register banks, and the 16 bytes used
9981 by the 128 bit variables, and 80 bytes for general purpose use.
9982 If any bit variables are used, the data variables will be placed in unused
9983 register banks and after the byte holding the last bit variable.
9984 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9989 variables will be placed starting from address 0x10 to 0x20 and continue
10002 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
10006 to specify the start address of the
10021 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
10025 to specify the size of the total internal RAM (
10039 By default the 8051 linker will place the stack after the last byte of (i)data
10052 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
10056 allows you to specify the start of the stack, i.e.
10057 you could start it after any data in the general purpose area.
10058 If your microcontroller has additional indirectly addressable internal
10063 ) you can place the stack on it.
10064 You may also need to use -
10075 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
10079 to set the start address of the external RAM (
10094 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
10098 to specify its size.
10099 Same goes for the code memory, using -
10110 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
10125 \begin_inset LatexCommand \index{-\/-code-size <Value>}
10130 If in doubt, don't specify any options and see if the resulting memory
10131 layout is appropriate, then you can adjust it.
10134 The linker generates two files with memory allocation information.
10135 The first, with extension .map
10136 \begin_inset LatexCommand \index{<file>.map}
10140 shows all the variables and segments.
10141 The second with extension .mem
10142 \begin_inset LatexCommand \index{<file>.mem}
10146 shows the final memory layout.
10147 The linker will complain either if memory segments overlap, there is not
10148 enough memory, or there is not enough space for stack.
10149 If you get any linking warnings and/or errors related to stack or segments
10150 allocation, take a look at either the .map or .mem files to find out what
10152 The .mem file may even suggest a solution to the problem.
10155 Z80/Z180 Storage Class
10156 \begin_inset LatexCommand \index{Storage class}
10160 Language Extensions
10161 \layout Subsubsection
10164 \begin_inset LatexCommand \index{sfr}
10169 \begin_inset LatexCommand \index{\_\_sfr}
10173 (in/out to 8-bit addresses)
10177 \begin_inset LatexCommand \index{Z80}
10181 family has separate address spaces for memory and
10191 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
10195 is accessed with special instructions, e.g.:
10200 sfr at 0x78 IoPort;\SpecialChar ~
10202 /* define a var in I/O space at 78h called IoPort */
10206 Writing 0x01 to this variable generates the assembly code:
10211 3E 01\SpecialChar ~
10219 D3 78\SpecialChar ~
10226 \layout Subsubsection
10229 \begin_inset LatexCommand \index{sfr}
10234 \begin_inset LatexCommand \index{\_\_sfr}
10238 (in/out to 16-bit addresses)
10245 is used to support 16 bit addresses in I/O memory e.g.:
10251 \begin_inset LatexCommand \index{at}
10256 \begin_inset LatexCommand \index{\_\_at}
10263 Writing 0x01 to this variable generates the assembly code:
10268 01 23 01\SpecialChar ~
10273 3E 01\SpecialChar ~
10281 ED 79\SpecialChar ~
10288 \layout Subsubsection
10291 \begin_inset LatexCommand \index{sfr}
10296 \begin_inset LatexCommand \index{\_\_sfr}
10300 (in0/out0 to 8 bit addresses on Z180
10301 \begin_inset LatexCommand \index{Z180}
10306 \begin_inset LatexCommand \index{HD64180}
10313 The compiler option -
10323 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
10325 \begin_inset LatexCommand \index{\#pragma portmode}
10329 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
10339 If you include the file z180.h this will be set automatically.
10343 \begin_inset LatexCommand \index{Storage class}
10347 Language Extensions
10348 \layout Subsubsection
10351 \begin_inset LatexCommand \index{data (hc08 storage class)}
10356 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10363 The data storage class declares a variable that resides in the first 256
10364 bytes of memory (the direct page).
10365 The HC08 is most efficient at accessing variables (especially pointers)
10367 \layout Subsubsection
10370 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10375 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10382 The xdata storage class declares a variable that can reside anywhere in
10384 This is the default if no storage class is specified.
10388 Absolute Addressing
10389 \begin_inset LatexCommand \index{Absolute addressing}
10396 Data items can be assigned an absolute address with the
10399 \begin_inset LatexCommand \index{at}
10404 \begin_inset LatexCommand \index{\_\_at}
10410 keyword, in addition to a storage class, e.g.:
10416 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10421 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10426 \begin_inset LatexCommand \index{at}
10431 \begin_inset LatexCommand \index{\_\_at}
10435 (0x7ffe) unsigned int chksum;
10443 __xdata __at (0x7ffe) unsigned int chksum;
10446 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10447 of the external ram.
10452 reserve any space for variables declared in this way
10453 \begin_inset Marginal
10464 (they are implemented with an equate in the assembler).
10465 Thus it is left to the programmer to make sure there are no overlaps with
10466 other variables that are declared without the absolute address.
10467 The assembler listing file (.lst
10468 \begin_inset LatexCommand \index{<file>.lst}
10472 ) and the linker output files (.rst
10473 \begin_inset LatexCommand \index{<file>.rst}
10478 \begin_inset LatexCommand \index{<file>.map}
10482 ) are good places to look for such overlaps.
10483 Variables with an absolute address are
10486 \begin_inset Marginal
10500 \begin_inset LatexCommand \index{Variable initialization}
10507 In case of memory mapped I/O devices the keyword
10511 has to be used to tell the compiler that accesses might not be removed:
10517 \begin_inset LatexCommand \index{volatile}
10522 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10527 \begin_inset LatexCommand \index{at}
10531 (0x8000) unsigned char PORTA_8255;
10534 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10539 \begin_inset LatexCommand \index{Aligned array}
10546 starts at a block (256 byte) boundary
10547 \begin_inset LatexCommand \index{block boundary}
10552 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10558 Absolute addresses can be specified for variables in all storage classes,
10565 \begin_inset LatexCommand \index{bit}
10570 \begin_inset LatexCommand \index{at}
10577 The above example will allocate the variable at offset 0x02 in the bit-addressab
10579 There is no real advantage to assigning absolute addresses to variables
10580 in this manner, unless you want strict control over all the variables allocated.
10581 One possible use would be to write hardware portable code.
10582 For example, if you have a routine that uses one or more of the microcontroller
10583 I/O pins, and such pins are different for two different hardwares, you
10584 can declare the I/O pins in your routine using:
10590 \begin_inset LatexCommand \index{volatile}
10594 __bit MOSI;\SpecialChar ~
10598 /* master out, slave in */
10600 extern volatile __bit MISO;\SpecialChar ~
10604 /* master in, slave out */
10606 extern volatile __bit MCLK;\SpecialChar ~
10614 /* Input and Output of a byte on a 3-wire serial bus.
10619 If needed adapt polarity of clock, polarity of data and bit order
10624 unsigned char spi_io(unsigned char out_byte)
10648 MOSI = out_byte & 0x80;
10678 /* _asm nop _endasm; */\SpecialChar ~
10686 /* for slow peripherals */
10737 Then, someplace in the code for the first hardware you would use
10743 \begin_inset LatexCommand \index{at}
10748 \begin_inset LatexCommand \index{\_\_at}
10752 (0x80) MOSI;\SpecialChar ~
10756 /* I/O port 0, bit 0 */
10758 __bit __at (0x81) MISO;\SpecialChar ~
10762 /* I/O port 0, bit 1 */
10764 __bit __at (0x82) MCLK;\SpecialChar ~
10768 /* I/O port 0, bit 2 */
10771 Similarly, for the second hardware you would use
10776 __bit __at (0x83) MOSI;\SpecialChar ~
10780 /* I/O port 0, bit 3 */
10782 __bit __at (0x91) MISO;\SpecialChar ~
10786 /* I/O port 1, bit 1 */
10789 \begin_inset LatexCommand \index{bit}
10793 __at (0x92) MCLK;\SpecialChar ~
10797 /* I/O port 1, bit 2 */
10800 and you can use the same hardware dependent routine without changes, as
10801 for example in a library.
10802 This is somehow similar to sbit, but only one absolute address has to be
10803 specified in the whole project.
10807 \begin_inset LatexCommand \index{Parameters}
10812 \begin_inset LatexCommand \index{function parameter}
10817 \begin_inset LatexCommand \index{local variables}
10822 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10829 Automatic (local) variables and parameters to functions can either be placed
10830 on the stack or in data-space.
10831 The default action of the compiler is to place these variables in the internal
10832 RAM (for small model) or external RAM (for large model).
10833 This in fact makes them similar to
10836 \begin_inset LatexCommand \index{static}
10842 so by default functions are non-reentrant
10843 \begin_inset LatexCommand \index{reentrant}
10852 They can be placed on the stack
10853 \begin_inset LatexCommand \index{stack}
10870 \begin_inset LatexCommand \index{-\/-stack-auto}
10878 #pragma\SpecialChar ~
10882 \begin_inset LatexCommand \index{\#pragma stackauto}
10889 \begin_inset LatexCommand \index{reentrant}
10895 keyword in the function declaration, e.g.:
10900 unsigned char foo(char i) __reentrant
10914 Since stack space on 8051 is limited, the
10932 option should be used sparingly.
10933 Note that the reentrant keyword just means that the parameters & local
10934 variables will be allocated to the stack, it
10938 mean that the function is register bank
10939 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10948 \begin_inset LatexCommand \index{local variables}
10952 can be assigned storage classes and absolute
10953 \begin_inset LatexCommand \index{Absolute addressing}
10962 unsigned char foo()
10970 __xdata unsigned char i;
10983 \begin_inset LatexCommand \index{at}
10987 (0x31) unsigned char j;
10999 In the above example the variable
11003 will be allocated in the external ram,
11007 in bit addressable space and
11026 or when a function is declared as
11030 this should only be done for static variables.
11034 \begin_inset LatexCommand \index{function parameter}
11038 however are not allowed any storage class
11039 \begin_inset LatexCommand \index{Storage class}
11043 , (storage classes for parameters will be ignored), their allocation is
11044 governed by the memory model in use, and the reentrancy options.
11047 It is however allowed to use bit parameters in reentrant functions and also
11048 non-static local bit variables are supported.
11049 Efficient use is limited to 8 semi-bitregisters in bit space.
11050 They are pushed and popped to stack as a single byte just like the normal
11055 \begin_inset LatexCommand \label{sub:Overlaying}
11060 \begin_inset LatexCommand \index{Overlaying}
11068 \begin_inset LatexCommand \index{reentrant}
11072 functions SDCC will try to reduce internal ram space usage by overlaying
11073 parameters and local variables of a function (if possible).
11074 Parameters and local variables
11075 \begin_inset LatexCommand \index{local variables}
11079 of a function will be allocated to an overlayable segment if the function
11082 no other function calls and the function is non-reentrant and the memory
11084 \begin_inset LatexCommand \index{Memory model}
11091 If an explicit storage class
11092 \begin_inset LatexCommand \index{Storage class}
11096 is specified for a local variable, it will NOT be overlayed.
11099 Note that the compiler (not the linkage editor) makes the decision for overlayin
11101 Functions that are called from an interrupt service routine
11102 \begin_inset Marginal
11112 should be preceded by a #pragma\SpecialChar ~
11114 \begin_inset LatexCommand \index{\#pragma nooverlay}
11118 if they are not reentrant.
11121 Also note that the compiler does not do any processing of inline assembler
11122 code, so the compiler might incorrectly assign local variables and parameters
11123 of a function into the overlay segment if the inline assembler code calls
11124 other c-functions that might use the overlay.
11125 In that case the #pragma\SpecialChar ~
11126 nooverlay should be used.
11129 Parameters and local variables of functions that contain 16 or 32 bit multiplica
11131 \begin_inset LatexCommand \index{Multiplication}
11136 \begin_inset LatexCommand \index{Division}
11140 will NOT be overlayed since these are implemented using external functions,
11149 \begin_inset LatexCommand \index{\#pragma nooverlay}
11155 void set_error(unsigned char errcd)
11171 void some_isr () __interrupt
11172 \begin_inset LatexCommand \index{interrupt}
11202 In the above example the parameter
11210 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
11212 not present, this could cause unpredictable runtime behavior when called
11213 from an interrupt service routine.
11214 The #pragma\SpecialChar ~
11215 nooverlay ensures that the parameters and local variables for
11216 the function are NOT overlayed.
11219 Interrupt Service Routines
11220 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
11227 General Information
11242 outines to be coded in C, with some extended keywords.
11247 void timer_isr (void) __interrupt (1) __using (1)
11261 The optional number following the
11264 \begin_inset LatexCommand \index{interrupt}
11269 \begin_inset LatexCommand \index{\_\_interrupt}
11275 keyword is the interrupt number this routine will service.
11276 When present, the compiler will insert a call to this routine in the interrupt
11277 vector table for the interrupt number specified.
11278 If you have multiple source files in your project, interrupt service routines
11279 can be present in any of them, but a prototype of the isr MUST be present
11280 or included in the file that contains the function
11288 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11293 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11299 keyword can be used to tell the compiler to use the specified register
11300 bank (8051 specific) when generating code for this function.
11306 Interrupt service routines open the door for some very interesting bugs:
11308 If an interrupt service routine changes variables which are accessed by
11309 other functions these variables have to be declared
11314 \begin_inset LatexCommand \index{volatile}
11322 If the access to these variables is not
11325 \begin_inset LatexCommand \index{atomic}
11332 the processor needs more than one instruction for the access and could
11333 be interrupted while accessing the variable) the interrupt must be disabled
11334 during the access to avoid inconsistent data.
11335 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
11336 and should be protected by disabling interrupts.
11337 You're not automatically on the safe side if you use 8 bit variables though.
11338 We need an example here: f.e.
11339 on the 8051 the harmless looking
11340 \begin_inset Quotes srd
11345 flags\SpecialChar ~
11350 \begin_inset Quotes sld
11359 \begin_inset Quotes srd
11364 flags\SpecialChar ~
11369 \begin_inset Quotes sld
11372 from within an interrupt routine might get lost if the interrupt occurs
11375 \begin_inset Quotes sld
11380 counter\SpecialChar ~
11385 \begin_inset Quotes srd
11388 is not atomic on the 8051 even if
11392 is located in data memory.
11393 Bugs like these are hard to reproduce and can cause a lot of trouble.
11397 The return address and the registers used in the interrupt service routine
11398 are saved on the stack
11399 \begin_inset LatexCommand \index{stack}
11403 so there must be sufficient stack space.
11404 If there isn't variables or registers (or even the return address itself)
11411 \begin_inset LatexCommand \index{stack overflow}
11415 is most likely to happen if the interrupt occurs during the
11416 \begin_inset Quotes sld
11420 \begin_inset Quotes srd
11423 subroutine when the stack is already in use for f.e.
11424 many return addresses.
11427 A special note here, int (16 bit) and long (32 bit) integer division
11428 \begin_inset LatexCommand \index{Division}
11433 \begin_inset LatexCommand \index{Multiplication}
11438 \begin_inset LatexCommand \index{Modulus}
11443 \begin_inset LatexCommand \index{Floating point support}
11447 operations are implemented using external support routines developed in
11449 If an interrupt service routine needs to do any of these operations then
11450 the support routines (as mentioned in a following section) will have to
11451 be recompiled using the
11464 \begin_inset LatexCommand \index{-\/-stack-auto}
11470 option and the source file will need to be compiled using the
11485 \begin_inset LatexCommand \index{-\/-int-long-reent}
11490 Note, the type promotion
11491 \begin_inset LatexCommand \index{type promotion}
11495 required by ANSI C can cause 16 bit routines to be used without the programmer
11500 Calling other functions from an interrupt service routine is not recommended,
11501 avoid it if possible.
11502 Note that when some function is called from an interrupt service routine
11503 it should be preceded by a #pragma\SpecialChar ~
11505 \begin_inset LatexCommand \index{\#pragma nooverlay}
11509 if it is not reentrant.
11510 Furthermore nonreentrant functions should not be called from the main program
11511 while the interrupt service routine might be active.
11512 They also must not be called from low priority interrupt service routines
11513 while a high priority interrupt service routine might be active.
11514 You could use semaphores or make the function
11518 if all parameters are passed in registers.
11523 \begin_inset LatexCommand \ref{sub:Overlaying}
11528 about Overlaying and section
11529 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11534 about Functions using private register banks.
11537 MCS51/DS390 Interrupt Service Routines
11540 Interrupt numbers and the corresponding address & descriptions for the Standard
11541 8051/8052 are listed below.
11542 SDCC will automatically adjust the interrupt vector table to the maximum
11543 interrupt number specified.
11549 \begin_inset Tabular
11550 <lyxtabular version="3" rows="9" columns="3">
11552 <column alignment="center" valignment="top" leftline="true" width="0in">
11553 <column alignment="left" valignment="top" leftline="true" width="0in">
11554 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0in">
11555 <row topline="true" bottomline="true">
11556 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11572 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11581 <row topline="true">
11582 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11607 <row topline="true">
11608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11616 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11633 <row topline="true">
11634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11659 <row topline="true">
11660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11668 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11676 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11685 <row topline="true">
11686 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11702 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11711 <row topline="true">
11712 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11737 <row topline="true">
11738 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11762 <row topline="true" bottomline="true">
11763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11771 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11778 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11796 If the interrupt service routine is defined without
11799 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11804 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11810 a register bank or with register bank 0 (
11814 0), the compiler will save the registers used by itself on the stack upon
11815 entry and restore them at exit, however if such an interrupt service routine
11816 calls another function then the entire register bank will be saved on the
11818 This scheme may be advantageous for small interrupt service routines which
11819 have low register usage.
11822 If the interrupt service routine is defined to be using a specific register
11827 & psw are saved and restored, if such an interrupt service routine calls
11828 another function (using another register bank) then the entire register
11829 bank of the called function will be saved on the stack.
11830 This scheme is recommended for larger interrupt service routines.
11833 HC08 Interrupt Service Routines
11836 Since the number of interrupts available is chip specific and the interrupt
11837 vector table always ends at the last byte of memory, the interrupt numbers
11838 corresponds to the interrupt vectors in reverse order of address.
11839 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11840 2 will use the interrupt vector at 0xfffa, and so on.
11841 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11842 this way; instead see section
11843 \begin_inset LatexCommand \ref{sub:Startup-Code}
11847 for details on customizing startup.
11850 Z80 Interrupt Service Routines
11853 The Z80 uses several different methods for determining the correct interrupt
11854 vector depending on the hardware implementation.
11855 Therefore, SDCC ignores the optional interrupt number and does not attempt
11856 to generate an interrupt vector table.
11859 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11860 instruction to return from the interrupt.
11861 To write an interrupt handler for the non-maskable interrupt, which needs
11862 a RETN instruction instead, add the
11871 void nmi_isr (void) critical interrupt
11885 However if you need to create a non-interruptable interrupt service routine
11886 you would also require the
11891 To distinguish between this and an nmi_isr you must provide an interrupt
11895 Enabling and Disabling Interrupts
11898 Critical Functions and Critical Statements
11901 A special keyword may be associated with a block or a function declaring
11907 SDCC will generate code to disable all interrupts
11908 \begin_inset LatexCommand \index{interrupt}
11912 upon entry to a critical function and restore the interrupt enable to the
11913 previous state before returning.
11914 Nesting critical functions will need one additional byte on the stack
11915 \begin_inset LatexCommand \index{stack}
11924 int foo () __critical
11925 \begin_inset LatexCommand \index{critical}
11930 \begin_inset LatexCommand \index{\_\_critical}
11955 The critical attribute maybe used with other attributes like
11965 may also be used to disable interrupts more locally:
11973 More than one statement could have been included in the block.
11976 Enabling and Disabling Interrupts directly
11980 \begin_inset LatexCommand \index{interrupt}
11984 can also be disabled and enabled directly (8051):
11989 EA = 0;\SpecialChar ~
12052 EA = 1;\SpecialChar ~
12119 On other architectures which have seperate opcodes for enabling and disabling
12120 interrupts you might want to make use of defines with inline assembly
12121 \begin_inset LatexCommand \index{Assembler routines}
12131 \begin_inset LatexCommand \index{\_asm}
12140 \begin_inset LatexCommand \index{\_endasm}
12149 #define SEI _asm\SpecialChar ~
12161 Note: it is sometimes sufficient to disable only a specific interrupt source
12163 a timer or serial interrupt by manipulating an
12166 \begin_inset LatexCommand \index{interrupt mask}
12176 Usually the time during which interrupts are disabled should be kept as
12178 This minimizes both
12183 \begin_inset LatexCommand \index{interrupt latency}
12187 (the time between the occurrence of the interrupt and the execution of
12188 the first code in the interrupt routine) and
12193 \begin_inset LatexCommand \index{interrupt jitter}
12197 (the difference between the shortest and the longest interrupt latency).
12198 These really are something different, f.e.
12199 a serial interrupt has to be served before its buffer overruns so it cares
12200 for the maximum interrupt latency, whereas it does not care about jitter.
12201 On a loudspeaker driven via a digital to analog converter which is fed
12202 by an interrupt a latency of a few milliseconds might be tolerable, whereas
12203 a much smaller jitter will be very audible.
12206 You can reenable interrupts within an interrupt routine and on some architecture
12207 s you can make use of two (or more) levels of
12209 interrupt priorities
12212 \begin_inset LatexCommand \index{interrupt priority}
12217 On some architectures which don't support interrupt priorities these can
12218 be implemented by manipulating the interrupt mask and reenabling interrupts
12219 within the interrupt routine.
12220 Check there is sufficient space on the stack
12221 \begin_inset LatexCommand \index{stack}
12225 and don't add complexity unless you have to.
12230 \begin_inset LatexCommand \index{semaphore}
12234 locking (mcs51/ds390)
12237 Some architectures (mcs51/ds390) have an atomic
12238 \begin_inset LatexCommand \index{atomic}
12251 These type of instructions are typically used in preemptive multitasking
12252 systems, where a routine f.e.
12253 claims the use of a data structure ('acquires a lock
12254 \begin_inset LatexCommand \index{lock}
12258 on it'), makes some modifications and then releases the lock when the data
12259 structure is consistent again.
12260 The instruction may also be used if interrupt and non-interrupt code have
12261 to compete for a resource.
12262 With the atomic bit test and clear instruction interrupts
12263 \begin_inset LatexCommand \index{interrupt}
12267 don't have to be disabled for the locking operation.
12271 SDCC generates this instruction if the source follows this pattern:
12277 \begin_inset LatexCommand \index{volatile}
12281 bit resource_is_free;
12285 if (resource_is_free)
12295 resource_is_free=0;
12308 resource_is_free=1;
12315 Note, mcs51 and ds390 support only an atomic
12316 \begin_inset LatexCommand \index{atomic}
12324 instruction (as opposed to atomic bit test and
12329 Functions using private register banks
12330 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
12337 Some architectures have support for quickly changing register sets.
12338 SDCC supports this feature with the
12341 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
12346 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
12352 attribute (which tells the compiler to use a register bank
12353 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
12357 other than the default bank zero).
12358 It should only be applied to
12361 \begin_inset LatexCommand \index{interrupt}
12367 functions (see footnote below).
12368 This will in most circumstances make the generated ISR code more efficient
12369 since it will not have to save registers on the stack.
12376 attribute will have no effect on the generated code for a
12380 function (but may occasionally be useful anyway
12386 possible exception: if a function is called ONLY from 'interrupt' functions
12387 using a particular bank, it can be declared with the same 'using' attribute
12388 as the calling 'interrupt' functions.
12389 For instance, if you have several ISRs using bank one, and all of them
12390 call memcpy(), it might make sense to create a specialized version of memcpy()
12391 'using 1', since this would prevent the ISR from having to save bank zero
12392 to the stack on entry and switch to bank zero before calling the function
12399 (pending: I don't think this has been done yet)
12406 function using a non-zero bank will assume that it can trash that register
12407 bank, and will not save it.
12408 Since high-priority interrupts
12409 \begin_inset LatexCommand \index{interrupts}
12414 \begin_inset LatexCommand \index{interrupt priority}
12418 can interrupt low-priority ones on the 8051 and friends, this means that
12419 if a high-priority ISR
12423 a particular bank occurs while processing a low-priority ISR
12427 the same bank, terrible and bad things can happen.
12428 To prevent this, no single register bank should be
12432 by both a high priority and a low priority ISR.
12433 This is probably most easily done by having all high priority ISRs use
12434 one bank and all low priority ISRs use another.
12435 If you have an ISR which can change priority at runtime, you're on your
12436 own: I suggest using the default bank zero and taking the small performance
12440 It is most efficient if your ISR calls no other functions.
12441 If your ISR must call other functions, it is most efficient if those functions
12442 use the same bank as the ISR (see note 1 below); the next best is if the
12443 called functions use bank zero.
12444 It is very inefficient to call a function using a different, non-zero bank
12450 \begin_inset LatexCommand \label{sub:Startup-Code}
12455 \begin_inset LatexCommand \index{Startup code}
12462 MCS51/DS390 Startup Code
12465 The compiler inserts a call to the C routine
12467 _sdcc_external_startup()
12468 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12477 at the start of the CODE area.
12478 This routine is in the runtime library
12479 \begin_inset LatexCommand \index{Runtime library}
12484 By default this routine returns 0, if this routine returns a non-zero value,
12485 the static & global variable initialization will be skipped and the function
12486 main will be invoked.
12487 Otherwise static & global variables will be initialized before the function
12491 _sdcc_external_startup()
12493 routine to your program to override the default if you need to setup hardware
12494 or perform some other critical operation prior to static & global variable
12496 \begin_inset LatexCommand \index{Variable initialization}
12501 On some mcs51 variants xdata
12502 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12506 memory has to be explicitly enabled before it can be accessed or if the
12507 watchdog needs to be disabled, this is the place to do it.
12508 The startup code clears all internal data memory, 256 bytes by default,
12509 but from 0 to n-1 if
12522 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12529 (recommended for Chipcon CC1010).
12532 See also the compiler option
12551 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12556 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12561 about MCS51-variants.
12567 The HC08 startup code follows the same scheme as the MCS51 startup code.
12573 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12574 from sdcc/device/lib/z80/crt0.s.
12575 If you need a different startup code you can use the compiler option
12596 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12600 and provide your own crt0.o.
12604 Inline Assembler Code
12605 \begin_inset LatexCommand \index{Assembler routines}
12612 A Step by Step Introduction
12613 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12620 Starting from a small snippet of c-code this example shows for the MCS51
12621 how to use inline assembly, access variables, a function parameter and
12622 an array in xdata memory.
12623 The example uses an MCS51 here but is easily adapted for other architectures.
12624 This is a buffer routine which should be optimized:
12630 unsigned char __far
12631 \begin_inset LatexCommand \index{far (storage class)}
12636 \begin_inset LatexCommand \index{\_\_far (storage class)}
12641 \begin_inset LatexCommand \index{at}
12646 \begin_inset LatexCommand \index{\_\_at}
12650 (0x7f00) buf[0x100];
12651 \begin_inset LatexCommand \index{Aligned array}
12657 unsigned char head, tail;
12661 void to_buffer( unsigned char c )
12669 if( head != (unsigned char)(tail-1) )\SpecialChar ~
12675 \begin_inset LatexCommand \index{promotion to signed int}
12680 \begin_inset LatexCommand \index{type promotion}
12685 \begin_inset Marginal
12706 buf[ head++ ] = c;\SpecialChar ~
12722 /* access to a 256 byte aligned array */
12727 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12728 then a corresponding buffer.asm file is generated.
12729 We define a new function
12733 in file buffer.c in which we cut and paste the generated code, removing
12734 unwanted comments and some ':'.
12736 \begin_inset Quotes sld
12740 \begin_inset Quotes srd
12744 \begin_inset Quotes sld
12748 \begin_inset Quotes srd
12751 to the beginning and the end of the function body:
12757 /* With a cut and paste from the .asm file, we have something to start with.
12762 The function is not yet OK! (registers aren't saved) */
12764 void to_buffer_asm( unsigned char c )
12773 \begin_inset LatexCommand \index{\_asm}
12778 \begin_inset LatexCommand \index{\_\_asm}
12792 ;buffer.c if( head != (unsigned char)(tail-1) )
12840 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12841 \begin_inset LatexCommand \index{Aligned array}
12906 \begin_inset LatexCommand \index{\_endasm}
12911 \begin_inset LatexCommand \index{\_\_endasm}
12920 The new file buffer.c should compile with only one warning about the unreferenced
12921 function argument 'c'.
12922 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12923 (1) and finally have:
12929 unsigned char __far __at(0x7f00) buf[0x100];
12931 unsigned char head, tail;
12933 #define USE_ASSEMBLY (1)
12941 void to_buffer( unsigned char c )
12949 if( head != (unsigned char)(tail-1) )
12969 void to_buffer( unsigned char c )
12977 c; // to avoid warning: unreferenced function argument
12984 \begin_inset LatexCommand \index{\_asm}
12989 \begin_inset LatexCommand \index{\_\_asm}
13003 ; save used registers here.
13014 ; If we were still using r2,r3 we would have to push them here.
13017 ; if( head != (unsigned char)(tail-1) )
13060 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
13084 ; buf[ head++ ] = c;
13095 a,dpl \SpecialChar ~
13102 ; dpl holds lower byte of function argument
13113 dpl,_head \SpecialChar ~
13116 ; buf is 0x100 byte aligned so head can be used directly
13158 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
13170 ; restore used registers here
13177 \begin_inset LatexCommand \index{\_endasm}
13182 \begin_inset LatexCommand \index{\_\_endasm}
13193 The inline assembler code can contain any valid code understood by the assembler
13194 , this includes any assembler directives and comment lines
13200 The assembler does not like some characters like ':' or ''' in comments.
13201 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
13202 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
13207 \begin_inset LatexCommand \index{Assembler documentation}
13215 The compiler does not do any validation of the code within the
13218 \begin_inset LatexCommand \index{\_asm}
13223 \begin_inset LatexCommand \index{\_\_asm}
13231 \begin_inset LatexCommand \index{\_endasm}
13236 \begin_inset LatexCommand \index{\_\_endasm}
13245 Specifically it will not know which registers are used and thus register
13247 \begin_inset LatexCommand \index{push/pop}
13251 has to be done manually.
13255 It is recommended that each assembly instruction (including labels) be placed
13256 in a separate line (as the example shows).
13270 \begin_inset LatexCommand \index{-\/-peep-asm}
13276 command line option is used, the inline assembler code will be passed through
13277 the peephole optimizer
13278 \begin_inset LatexCommand \index{Peephole optimizer}
13283 There are only a few (if any) cases where this option makes sense, it might
13284 cause some unexpected changes in the inline assembler code.
13285 Please go through the peephole optimizer rules defined in file
13289 before using this option.
13293 \begin_inset LatexCommand \label{sub:Naked-Functions}
13298 \begin_inset LatexCommand \index{Naked functions}
13305 A special keyword may be associated with a function declaring it as
13308 \begin_inset LatexCommand \index{\_naked}
13313 \begin_inset LatexCommand \index{\_\_naked}
13324 function modifier attribute prevents the compiler from generating prologue
13325 \begin_inset LatexCommand \index{function prologue}
13330 \begin_inset LatexCommand \index{function epilogue}
13334 code for that function.
13335 This means that the user is entirely responsible for such things as saving
13336 any registers that may need to be preserved, selecting the proper register
13337 bank, generating the
13341 instruction at the end, etc.
13342 Practically, this means that the contents of the function must be written
13343 in inline assembler.
13344 This is particularly useful for interrupt functions, which can have a large
13345 (and often unnecessary) prologue/epilogue.
13346 For example, compare the code generated by these two functions:
13352 \begin_inset LatexCommand \index{volatile}
13356 data unsigned char counter;
13360 void simpleInterrupt(void) __interrupt
13361 \begin_inset LatexCommand \index{interrupt}
13366 \begin_inset LatexCommand \index{\_\_interrupt}
13384 void nakedInterrupt(void) __interrupt (2) __naked
13393 \begin_inset LatexCommand \index{\_asm}
13398 \begin_inset LatexCommand \index{\_\_asm}
13415 _counter ; does not change flags, no need to save psw
13427 ; MUST explicitly include ret or reti in _naked function.
13434 \begin_inset LatexCommand \index{\_endasm}
13439 \begin_inset LatexCommand \index{\_\_endasm}
13448 For an 8051 target, the generated simpleInterrupt looks like:
13457 example, recent versions of SDCC generate
13459 the same code for simpleInterrupt() and nakedInterrupt()!
13599 whereas nakedInterrupt looks like:
13614 _counter ; does not change flags, no need to save psw
13632 ; MUST explicitly include ret or reti in _naked function
13635 The related directive #pragma exclude
13636 \begin_inset LatexCommand \index{\#pragma exclude}
13640 allows a more fine grained control over pushing & popping
13641 \begin_inset LatexCommand \index{push/pop}
13648 While there is nothing preventing you from writing C code inside a
13652 function, there are many ways to shoot yourself in the foot doing this,
13653 and it is recommended that you stick to inline assembler.
13656 Use of Labels within Inline Assembler
13659 SDCC allows the use of in-line assembler with a few restrictions regarding
13661 In older versions of the compiler all labels defined within inline assembler
13670 where nnnn is a number less than 100 (which implies a limit of utmost 100
13671 inline assembler labels
13685 \begin_inset LatexCommand \index{\_asm}
13690 \begin_inset LatexCommand \index{\_\_asm}
13720 \begin_inset LatexCommand \index{\_endasm}
13725 \begin_inset LatexCommand \index{\_\_endasm}
13732 Inline assembler code cannot reference any C-Labels, however it can reference
13734 \begin_inset LatexCommand \index{Labels}
13738 defined by the inline assembler, e.g.:
13763 ; some assembler code
13783 /* some more c code */
13785 clabel:\SpecialChar ~
13787 /* inline assembler cannot reference this label */
13799 $0003: ;label (can be referenced by inline assembler only)
13806 \begin_inset LatexCommand \index{\_endasm}
13811 \begin_inset LatexCommand \index{\_\_endasm}
13821 /* some more c code */
13826 In other words inline assembly code can access labels defined in inline
13827 assembly within the scope of the function.
13828 The same goes the other way, i.e.
13829 labels defines in inline assembly can not be accessed by C statements.
13832 Interfacing with Assembler Code
13833 \begin_inset LatexCommand \index{Assembler routines}
13840 Global Registers used for Parameter Passing
13841 \begin_inset LatexCommand \index{Parameter passing}
13848 The compiler always uses the global registers
13851 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13856 \begin_inset LatexCommand \index{DPTR}
13861 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13870 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13876 to pass the first parameter to a routine.
13877 The second parameter onwards is either allocated on the stack (for reentrant
13888 -stack-auto is used) or in data / xdata memory (depending on the memory
13893 Assembler Routine (non-reentrant)
13896 In the following example
13897 \begin_inset LatexCommand \index{reentrant}
13902 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13906 the function c_func calls an assembler routine asm_func, which takes two
13908 \begin_inset LatexCommand \index{function parameter}
13917 extern int asm_func(unsigned char, unsigned char);
13921 int c_func (unsigned char i, unsigned char j)
13929 return asm_func(i,j);
13943 return c_func(10,9);
13948 The corresponding assembler function is:
13953 .globl _asm_func_PARM_2
14054 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
14071 Note here that the return values
14072 \begin_inset LatexCommand \index{return value}
14076 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
14078 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
14079 b' & 'acc' for four byte values.
14082 The parameter naming convention is _<function_name>_PARM_<n>, where n is
14083 the parameter number starting from 1, and counting from the left.
14084 The first parameter is passed in
14085 \begin_inset Quotes eld
14089 \begin_inset Quotes erd
14092 for a one byte parameter,
14093 \begin_inset Quotes eld
14097 \begin_inset Quotes erd
14101 \begin_inset Quotes eld
14105 \begin_inset Quotes erd
14108 for three bytes and
14109 \begin_inset Quotes eld
14113 \begin_inset Quotes erd
14116 for a four bytes parameter.
14117 The variable name for the second parameter will be _<function_name>_PARM_2.
14121 Assemble the assembler routine with the following command:
14128 asx8051 -losg asmfunc.asm
14135 Then compile and link the assembler routine to the C source file with the
14143 sdcc cfunc.c asmfunc.rel
14146 Assembler Routine (reentrant)
14150 \begin_inset LatexCommand \index{reentrant}
14155 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
14159 the second parameter
14160 \begin_inset LatexCommand \index{function parameter}
14164 onwards will be passed on the stack, the parameters are pushed from right
14166 after the call the leftmost parameter will be on the top of the stack.
14167 Here is an example:
14172 extern int asm_func(unsigned char, unsigned char);
14176 int c_func (unsigned char i, unsigned char j) reentrant
14184 return asm_func(i,j);
14198 return c_func(10,9);
14203 The corresponding assembler routine is:
14303 The compiling and linking procedure remains the same, however note the extra
14304 entry & exit linkage required for the assembler code, _bp is the stack
14305 frame pointer and is used to compute the offset into the stack for parameters
14306 and local variables.
14310 \begin_inset LatexCommand \index{int (16 bit)}
14315 \begin_inset LatexCommand \index{long (32 bit)}
14322 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
14323 multiplication and modulus operations are implemented by support routines.
14324 These support routines are all developed in ANSI-C to facilitate porting
14325 to other MCUs, although some model specific assembler optimizations are
14327 The following files contain the described routines, all of them can be
14328 found in <installdir>/share/sdcc/lib.
14334 \begin_inset Tabular
14335 <lyxtabular version="3" rows="11" columns="2">
14337 <column alignment="left" valignment="top" leftline="true" width="0">
14338 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14339 <row topline="true" bottomline="true">
14340 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14361 <row topline="true">
14362 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14370 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14375 16 bit multiplication
14379 <row topline="true">
14380 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14388 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14393 signed 16 bit division (calls _divuint)
14397 <row topline="true">
14398 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14406 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14411 unsigned 16 bit division
14415 <row topline="true">
14416 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14429 signed 16 bit modulus (calls _moduint)
14433 <row topline="true">
14434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14442 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14447 unsigned 16 bit modulus
14451 <row topline="true">
14452 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14460 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14465 32 bit multiplication
14469 <row topline="true">
14470 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14483 signed 32 division (calls _divulong)
14487 <row topline="true">
14488 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14496 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14501 unsigned 32 division
14505 <row topline="true">
14506 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14514 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14519 signed 32 bit modulus (calls _modulong)
14523 <row topline="true" bottomline="true">
14524 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14532 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14537 unsigned 32 bit modulus
14550 Since they are compiled as
14555 \begin_inset LatexCommand \index{reentrant}
14560 \begin_inset LatexCommand \index{interrupt}
14564 service routines should not do any of the above operations.
14565 If this is unavoidable then the above routines will need to be compiled
14579 \begin_inset LatexCommand \index{-\/-stack-auto}
14585 option, after which the source program will have to be compiled with
14598 \begin_inset LatexCommand \index{-\/-int-long-reent}
14605 Notice that you don't have to call these routines directly.
14606 The compiler will use them automatically every time an integer operation
14610 Floating Point Support
14611 \begin_inset LatexCommand \index{Floating point support}
14618 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14619 floating point support routines are derived from gcc's floatlib.c and consist
14620 of the following routines:
14628 \begin_inset Tabular
14629 <lyxtabular version="3" rows="17" columns="2">
14631 <column alignment="left" valignment="top" leftline="true" width="0">
14632 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
14633 <row topline="true" bottomline="true">
14634 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14660 <row topline="true">
14661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14678 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14692 add floating point numbers
14696 <row topline="true">
14697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14728 subtract floating point numbers
14732 <row topline="true">
14733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14750 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14764 divide floating point numbers
14768 <row topline="true">
14769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14800 multiply floating point numbers
14804 <row topline="true">
14805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14836 convert floating point to unsigned char
14840 <row topline="true">
14841 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14858 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14872 convert floating point to signed char
14876 <row topline="true">
14877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14894 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14908 convert floating point to unsigned int
14912 <row topline="true">
14913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14944 convert floating point to signed int
14948 <row topline="true">
14949 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14975 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14989 convert floating point to unsigned long
14993 <row topline="true">
14994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15011 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15025 convert floating point to signed long
15029 <row topline="true">
15030 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15061 convert unsigned char to floating point
15065 <row topline="true">
15066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15083 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15097 convert char to floating point number
15101 <row topline="true">
15102 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15119 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15133 convert unsigned int to floating point
15137 <row topline="true">
15138 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15169 convert int to floating point numbers
15173 <row topline="true">
15174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15191 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15205 convert unsigned long to floating point number
15209 <row topline="true" bottomline="true">
15210 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15227 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15241 convert long to floating point number
15254 These support routines are developed in ANSI-C so there is room for space
15255 and speed improvement
15261 These floating point routines (
15265 sinf(), cosf(), ...) for the mcs51 are implemented in assembler.
15270 Note if all these routines are used simultaneously the data space might
15272 For serious floating point usage the large model might be needed.
15273 Also notice that you don't have to call this routines directly.
15274 The compiler will use them automatically every time a floating point operation
15279 \begin_inset LatexCommand \index{Libraries}
15288 <pending: this is messy and incomplete - a little more information is in
15289 sdcc/doc/libdoc.txt
15294 Compiler support routines (_gptrget, _mulint etc.)
15297 Stdclib functions (puts, printf, strcat etc.)
15298 \layout Subsubsection
15304 \begin_inset LatexCommand \index{<stdio.h>}
15308 As usual on embedded systems you have to provide your own
15311 \begin_inset LatexCommand \index{getchar()}
15320 \begin_inset LatexCommand \index{putchar()}
15327 SDCC does not know whether the system connects to a serial line with or
15328 without handshake, LCD, keyboard or other device.
15342 You'll find examples for serial routines f.e.
15343 in sdcc/device/lib.
15349 \begin_inset LatexCommand \index{printf()}
15359 does not support float (except on ds390).
15360 To enable this recompile it with the option
15373 \begin_inset LatexCommand \index{USE\_FLOATS}
15379 on the command line.
15393 \begin_inset LatexCommand \index{-\/-model-large}
15399 for the mcs51 port, since this uses a lot of memory.
15402 If you're short on memory you might want to use
15405 \begin_inset LatexCommand \index{printf\_small()}
15420 For the mcs51 there additionally are assembly versions
15423 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
15429 (subset of printf using less than 270 bytes) and
15432 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
15441 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
15447 (floating-point aware version of printf_fast) which should fit the requirements
15448 of many embedded systems (printf_fast() can be customized by unsetting
15453 support long variables and field widths).
15454 \layout Subsubsection
15457 \begin_inset LatexCommand \index{malloc.h}
15464 Before using dynamic
15465 \begin_inset LatexCommand \index{dynamic}
15469 memory allocation with SDCC, you have to provide heap
15470 \begin_inset LatexCommand \index{heap}
15474 space for malloc to allocate memory from
15479 You can acomplish this by including the following code into your source:
15484 #include <malloc.h>
15485 \begin_inset LatexCommand \index{malloc.h}
15490 \begin_inset LatexCommand \index{calloc}
15495 \begin_inset LatexCommand \index{malloc}
15500 \begin_inset LatexCommand \index{realloc}
15505 \begin_inset LatexCommand \index{free}
15513 #define HEAPSIZE 0x1000 /* Adjust depending on available memory */
15515 unsigned char xdata myheap[HEAPSIZE]; /* The actual heap for dynamic memory
15527 /* Your variable declarations come here*/
15537 init_dynamic_memory((MEMHEADER xdata *)myheap, HEAPSIZE);
15543 /* Rest of your code*/
15550 Math functions (sinf, powf, sqrtf etc.)
15551 \layout Subsubsection
15556 See definitions in file <math.h>.
15563 \begin_inset LatexCommand \index{Libraries}
15567 included in SDCC should have a license at least as liberal as the GNU Lesser
15568 General Public License
15569 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
15580 license statements for the libraries are missing.
15581 sdcc/device/lib/ser_ir.c
15585 come with a GPL (as opposed to LGPL) License - this will not be liberal
15586 enough for many embedded programmers.
15589 If you have ported some library or want to share experience about some code
15591 falls into any of these categories Busses (I
15592 \begin_inset Formula $^{\textrm{2}}$
15595 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
15596 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
15597 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
15598 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
15603 would certainly like to hear about it.
15604 Programmers coding for embedded systems are not especially famous for being
15605 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
15606 e these references are very valuable.
15607 Let's help to create a climate where information is shared.
15613 MCS51 Memory Models
15614 \begin_inset LatexCommand \index{Memory model}
15619 \begin_inset LatexCommand \index{MCS51 memory model}
15624 \layout Subsubsection
15626 Small, Medium and Large
15629 SDCC allows three memory models for MCS51 code,
15638 Modules compiled with different memory models should
15642 be combined together or the results would be unpredictable.
15643 The library routines supplied with the compiler are compiled as small,
15645 The compiled library modules are contained in separate directories as small,
15646 medium and large so that you can link to the appropriate set.
15649 When the medium or large model is used all variables declared without a
15650 storage class will be allocated into the external ram, this includes all
15651 parameters and local variables (for non-reentrant
15652 \begin_inset LatexCommand \index{reentrant}
15657 When the small model is used variables without storage class are allocated
15658 in the internal ram.
15661 Judicious usage of the processor specific storage classes
15662 \begin_inset LatexCommand \index{Storage class}
15666 and the 'reentrant' function type will yield much more efficient code,
15667 than using the large model.
15668 Several optimizations are disabled when the program is compiled using the
15669 large model, it is therefore recommended that the small model be used unless
15670 absolutely required.
15671 \layout Subsubsection
15674 \begin_inset LatexCommand \label{sub:External-Stack}
15679 \begin_inset LatexCommand \index{stack}
15684 \begin_inset LatexCommand \index{External stack (mcs51)}
15691 The external stack (-
15702 \begin_inset LatexCommand \index{-\/-xstack}
15706 ) is located in pdata
15707 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15711 memory (usually at the start of the external ram segment) and uses all
15712 unused space in pdata (max.
15724 -xstack option is used to compile the program, the parameters and local
15726 \begin_inset LatexCommand \index{local variables}
15730 of all reentrant functions are allocated in this area.
15731 This option is provided for programs with large stack space requirements.
15732 When used with the -
15743 \begin_inset LatexCommand \index{-\/-stack-auto}
15747 option, all parameters and local variables are allocated on the external
15748 stack (note: support libraries will need to be recompiled with the same
15750 There is a predefined target in the library makefile).
15753 The compiler outputs the higher order address byte of the external ram segment
15755 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15760 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15764 ), therefore when using the External Stack option, this port
15768 be used by the application program.
15772 \begin_inset LatexCommand \index{Memory model}
15777 \begin_inset LatexCommand \index{DS390 memory model}
15784 The only model supported is Flat 24
15785 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15790 This generates code for the 24 bit contiguous addressing mode of the Dallas
15792 In this mode, up to four meg of external RAM or code space can be directly
15794 See the data sheets at www.dalsemi.com for further information on this part.
15798 Note that the compiler does not generate any code to place the processor
15799 into 24 bitmode (although
15803 in the ds390 libraries will do that for you).
15809 \begin_inset LatexCommand \index{Tinibios (DS390)}
15813 , the boot loader or similar code must ensure that the processor is in 24
15814 bit contiguous addressing mode before calling the SDCC startup code.
15832 option, variables will by default be placed into the XDATA segment.
15837 Segments may be placed anywhere in the 4 meg address space using the usual
15849 Note that if any segments are located above 64K, the -r flag must be passed
15850 to the linker to generate the proper segment relocations, and the Intel
15851 HEX output format must be used.
15852 The -r flag can be passed to the linker by using the option
15856 on the SDCC command line.
15857 However, currently the linker can not handle code segments > 64k.
15861 \begin_inset LatexCommand \index{Pragmas}
15868 SDCC supports the following #pragma directives:
15872 \begin_inset LatexCommand \index{\#pragma save}
15876 - this will save all current options to the save/restore stack.
15877 See #pragma\SpecialChar ~
15882 \begin_inset LatexCommand \index{\#pragma restore}
15886 - will restore saved options from the last save.
15887 saves & restores can be nested.
15888 SDCC uses a save/restore stack: save pushes current options to the stack,
15889 restore pulls current options from the stack.
15890 See #pragma\SpecialChar ~
15897 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15902 \begin_inset LatexCommand \index{function prologue}
15906 function1[,function2[,function3...]] - The compiler by default uses a caller
15907 saves convention for register saving across function calls, however this
15908 can cause unnecessary register pushing & popping
15909 \begin_inset LatexCommand \index{push/pop}
15913 when calling small functions from larger functions.
15914 This option can be used to switch off the register saving convention for
15915 the function names specified.
15916 The compiler will not save registers when calling these functions, extra
15917 code need to be manually inserted at the entry & exit for these functions
15918 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15919 reduce code & improve run time performance of the generated code.
15920 In the future the compiler (with inter procedural analysis) may be able
15921 to determine the appropriate scheme to use for each function call.
15932 -callee-saves command line option is used, the function names specified
15933 in #pragma\SpecialChar ~
15935 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15939 is appended to the list of functions specified in the command line.
15943 \begin_inset LatexCommand \index{\#pragma exclude}
15947 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15948 of pairs of push/pop
15949 \begin_inset LatexCommand \index{push/pop}
15958 \begin_inset LatexCommand \index{interrupt}
15971 The directive should be placed immediately before the ISR function definition
15972 and it affects ALL ISR functions following it.
15973 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15974 exclude\SpecialChar ~
15976 \begin_inset LatexCommand \index{\#pragma exclude}
15981 See also the related keyword _naked
15982 \begin_inset LatexCommand \index{\_naked}
15987 \begin_inset LatexCommand \index{\_\_naked}
15995 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15999 - the compiler will not warn you anymore for obvious mistakes, you'r on
16003 disable_warning <nnnn>
16004 \begin_inset LatexCommand \index{\#pragma disable\_warning}
16008 - the compiler will not warn you anymore about warning number <nnnn>.
16012 \begin_inset LatexCommand \index{\#pragma nogcse}
16016 - will stop global common subexpression elimination.
16020 \begin_inset LatexCommand \index{\#pragma noinduction}
16024 - will stop loop induction optimizations.
16028 \begin_inset LatexCommand \index{\#pragma noinvariant}
16032 - will not do loop invariant optimizations.
16033 For more details see Loop Invariants in section
16034 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
16042 \begin_inset LatexCommand \index{\#pragma noiv}
16046 - Do not generate interrupt
16047 \begin_inset LatexCommand \index{interrupt}
16051 vector table entries for all ISR functions defined after the pragma.
16052 This is useful in cases where the interrupt vector table must be defined
16053 manually, or when there is a secondary, manually defined interrupt vector
16055 for the autovector feature of the Cypress EZ-USB FX2).
16056 More elegantly this can be achieved by obmitting the optional interrupt
16057 number after the interrupt keyword, see section
16058 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
16067 \begin_inset LatexCommand \index{\#pragma nojtbound}
16071 - will not generate code for boundary value checking, when switch statements
16072 are turned into jump-tables (dangerous).
16073 For more details see section
16074 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
16082 \begin_inset LatexCommand \index{\#pragma noloopreverse}
16086 - Will not do loop reversal optimization
16090 \begin_inset LatexCommand \index{\#pragma nooverlay}
16094 - the compiler will not overlay the parameters and local variables of a
16099 \begin_inset LatexCommand \index{\#pragma stackauto}
16114 \begin_inset LatexCommand \index{-\/-stack-auto}
16119 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
16123 Parameters and Local Variables.
16127 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
16131 - The compiler will optimize code generation towards fast code, possibly
16132 at the expense of code size.
16136 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
16140 - The compiler will optimize code generation towards compact code, possibly
16141 at the expense of code speed.
16145 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
16149 - The compiler will attempt to generate code that is both compact and fast,
16150 as long as meeting one goal is not a detriment to the other (this is the
16156 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
16160 - Generally follow the C89 standard, but allow SDCC features that conflict
16161 with the standard (default).
16165 \begin_inset LatexCommand \index{\#pragma std\_c89}
16169 - Follow the C89 standard and disable SDCC features that conflict with the
16174 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
16178 - Generally follow the C99 standard, but allow SDCC features that conflict
16179 with the standard (incomplete support).
16183 \begin_inset LatexCommand \index{\#pragma std\_c99}
16187 - Follow the C99 standard and disable SDCC features that conflict with the
16188 standard (incomplete support).
16192 \begin_inset LatexCommand \index{\#pragma codeseg}
16196 - Use this name (max.
16197 8 characters) for the code segment.
16212 \begin_inset LatexCommand \index{\#pragma constseg}
16216 - Use this name (max.
16217 8 characters) for the const segment.
16231 SDCPP supports the following #pragma directives:
16235 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16239 (+ | -) - switch _asm _endasm block preprocessing on / off.
16241 You use this prama to define multilines of assembly code.
16242 This will prevent the preprocessor from changing the formating required
16244 Below is an example on how to use this pragma.
16249 #pragma preproc_asm -
16250 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
16256 #define MYDELAY _asm
16261 nop ;my assembly comment...
16275 #pragma preproc_asm +
16305 The pragma's are intended to be used to turn-on or off certain optimizations
16306 which might cause the compiler to generate extra stack / data space to
16307 store compiler generated temporary variables.
16308 This usually happens in large functions.
16309 Pragma directives should be used as shown in the following example, they
16310 are used to control options & optimizations for a given function; pragmas
16311 should be placed before and/or after a function, placing pragma's inside
16312 a function body could have unpredictable results.
16318 \begin_inset LatexCommand \index{\#pragma save}
16329 /* save the current settings */
16332 \begin_inset LatexCommand \index{\#pragma nogcse}
16341 /* turnoff global subexpression elimination */
16343 #pragma noinduction
16344 \begin_inset LatexCommand \index{\#pragma noinduction}
16348 /* turn off induction optimizations */
16371 \begin_inset LatexCommand \index{\#pragma restore}
16375 /* turn the optimizations back on */
16378 The compiler will generate a warning message when extra space is allocated.
16379 It is strongly recommended that the save and restore pragma's be used when
16380 changing options for a function.
16389 Defines Created by the Compiler
16392 The compiler creates the following #defines
16393 \begin_inset LatexCommand \index{\#defines}
16398 \begin_inset LatexCommand \index{Defines created by the compiler}
16408 \begin_inset Tabular
16409 <lyxtabular version="3" rows="11" columns="2">
16411 <column alignment="left" valignment="top" leftline="true" width="3in">
16412 <column alignment="left" valignment="top" leftline="true" rightline="true" width="3in">
16413 <row topline="true" bottomline="true">
16414 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16435 <row topline="true">
16436 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16442 \begin_inset LatexCommand \index{SDCC}
16449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16455 Since version 2.5.6 the version number as an int (ex.
16460 <row topline="true">
16461 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16467 \begin_inset LatexCommand \index{SDCC\_mcs51}
16472 \begin_inset LatexCommand \index{SDCC\_ds390}
16477 \begin_inset LatexCommand \index{SDCC\_z80}
16484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16489 depending on the model used (e.g.: -mds390)
16493 <row topline="true">
16494 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16500 \begin_inset LatexCommand \index{\_\_mcs51}
16505 \begin_inset LatexCommand \index{\_\_ds390}
16510 \begin_inset LatexCommand \index{\_\_hc08}
16515 \begin_inset LatexCommand \index{\_\_z80}
16522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16527 depending on the model used (e.g.
16532 <row topline="true">
16533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16539 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
16546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16569 <row topline="true">
16570 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16576 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
16583 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16606 <row topline="true">
16607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16613 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
16620 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16643 <row topline="true">
16644 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16650 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
16657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16680 <row topline="true">
16681 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16687 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
16694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16717 <row topline="true">
16718 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16724 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16731 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16744 <row topline="true" bottomline="true">
16745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16751 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16778 Notes on supported Processors
16782 \begin_inset LatexCommand \label{sub:MCS51-variants}
16787 \begin_inset LatexCommand \index{MCS51 variants}
16794 MCS51 processors are available from many vendors and come in many different
16796 While they might differ considerably in respect to Special Function Registers
16797 the core MCS51 is usually not modified or is kept compatible.
16801 pdata access by SFR
16804 With the upcome of devices with internal xdata and flash memory devices
16806 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16810 as dedicated I/O port is becoming more popular.
16811 Switching the high byte for pdata
16812 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16816 access which was formerly done by port P2 is then achieved by a Special
16818 \begin_inset LatexCommand \index{sfr}
16823 In well-established MCS51 tradition the address of this
16827 is where the chip designers decided to put it.
16828 Needless to say that they didn't agree on a common name either.
16829 So that the startup code can correctly initialize xdata variables, you
16830 should define an sfr with the name _XPAGE
16833 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16839 at the appropriate location if the default, port P2, is not used for this.
16845 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16850 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16855 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16858 For more exotic implementations further customizations may be needed.
16860 \begin_inset LatexCommand \ref{sub:Startup-Code}
16864 for other possibilities.
16867 Other Features available by SFR
16870 Some MCS51 variants offer features like Double DPTR
16871 \begin_inset LatexCommand \index{DPTR}
16875 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16876 These are currently not used for the MCS51 port.
16877 If you absolutely need them you can fall back to inline assembly or submit
16884 The DS80C400 microcontroller has a rich set of peripherals.
16885 In its built-in ROM library it includes functions to access some of the
16886 features, among them is a TCP stack with IP4 and IP6 support.
16887 Library headers (currently in beta status) and other files are provided
16891 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16899 The Z80 and gbz80 port
16902 SDCC can target both the Zilog
16903 \begin_inset LatexCommand \index{Z80}
16907 and the Nintendo Gameboy's Z80-like gbz80
16908 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16913 The Z80 port is passed through the same
16916 \begin_inset LatexCommand \index{Regression test}
16922 as the MCS51 and DS390 ports, so floating point support, support for long
16923 variables and bitfield support is fine.
16924 See mailing lists and forums about interrupt routines.
16927 As always, the code is the authoritative reference - see z80/ralloc.c and
16930 \begin_inset LatexCommand \index{stack}
16934 frame is similar to that generated by the IAR Z80 compiler.
16935 IX is used as the base pointer, HL and IY are used as a temporary registers,
16936 and BC and DE are available for holding variables.
16938 \begin_inset LatexCommand \index{return value}
16942 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16944 The gbz80 port use the same set of registers for the return values, but
16945 in a different order of significance: E (one byte), DE (two bytes), or
16952 The port to the Motorola HC08
16953 \begin_inset LatexCommand \index{HC08}
16957 family has been added in October 2003, and is still undergoing some basic
16959 The code generator is complete, but the register allocation is still quite
16961 Some of the SDCC's standard C library functions have embedded non-HC08
16962 inline assembly and so are not yet usable.
16973 \begin_inset LatexCommand \index{PIC14}
16977 port still requires a major effort from the development community.
16978 However it can work for very simple code.
16981 C code and 14bit PIC code page
16982 \begin_inset LatexCommand \index{code page (pic14)}
16987 \begin_inset LatexCommand \index{RAM bank (pic14)}
16994 The linker organizes allocation for the code page and RAM banks.
16995 It does not have intimate knowledge of the code flow.
16996 It will put all the code section of a single asm file into a single code
16998 In order to make use of multiple code pages, separate asm files must be
17000 The compiler treats all functions of a single C file as being in the same
17001 code page unless it is non static.
17002 The compiler treats all local variables of a single C file as being in
17003 the same RAM bank unless it is an extern.
17007 To get the best follow these guide lines:
17010 make local functions static, as non static functions require code page selection
17014 Make local variables static as extern variables require RAM bank selection
17018 For devices that have multiple code pages it is more efficient to use the
17019 same number of files as pages, i.e.
17020 for the 16F877 use 4 separate files and i.e.
17021 for the 16F874 use 2 separate files.
17022 This way the linker can put the code for each file into different code
17023 pages and the compiler can allocate reusable variables more efficiently
17024 and there's less page selection overhead.
17025 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
17026 instruction set) use 'unsigned char' whereever possible instead of 'int'.
17029 Creating a device include file
17032 For generating a device include file use the support perl script inc2h.pl
17033 kept in directory support/script.
17039 For the interrupt function, use the keyword 'interrupt'
17040 \begin_inset LatexCommand \index{interrupt}
17044 with level number of 0 (PIC14 only has 1 interrupt so this number is only
17045 there to avoid a syntax error - it ought to be fixed).
17051 void Intr(void) interrupt 0
17057 T0IF = 0; /* Clear timer interrupt */
17062 Linking and assembling
17065 For assembling you can use either GPUTILS'
17066 \begin_inset LatexCommand \index{gputils (pic tools)}
17070 gpasm.exe or MPLAB's mpasmwin.exe.
17071 GPUTILS is available from
17072 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17077 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
17078 If you use MPLAB and an interrupt function then the linker script file
17079 vectors section will need to be enlarged to link with mplink.
17102 sdcc -S -V -mpic14 -p16F877 $<
17116 $(PRJ).hex: $(OBJS)
17126 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
17148 sdcc -S -V -mpic14 -p16F877 $<
17158 mpasmwin /q /o $*.asm
17162 $(PRJ).hex: $(OBJS)
17172 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
17175 Please note that indentations within a
17179 have to be done with a tabulator character.
17182 Command-line options
17185 Besides the switches common to all SDCC backends, the PIC14 port accepts
17186 the following options (for an updated list see sdcc -
17198 \labelwidthstring 00.00.0000
17210 -debug-extra emit debug info in assembly output
17212 \labelwidthstring 00.00.0000
17224 -no-pcode-opt disable (slightly faulty) optimization on pCode
17228 \layout Subsubsection
17230 error: missing definition for symbol
17231 \begin_inset Quotes sld
17235 \begin_inset Quotes srd
17241 The PIC14 port uses library routines to provide more complex operations
17242 like multiplication, division/modulus and (generic) pointer dereferencing.
17243 In order to add these routines to your project, you must link with PIC14's
17249 For single source file projects this is done automatically, more complex
17254 to the linker's arguments.
17255 Make sure you also add an include path for the library (using the -I switch
17257 \layout Subsubsection
17259 Processor mismatch in file
17260 \begin_inset Quotes sld
17264 \begin_inset Quotes srd
17270 This warning can usually be ignored due to the very good compatibility amongst
17271 14 bit PIC devices.
17274 You might also consider recompiling the library for your specific device
17275 by changing the ARCH=p16f877 (default target) entry in
17277 device/lib/pic/Makefile.in
17281 device/lib/pic/Makefile
17283 to reflect your device.
17284 This might even improve performance for smaller devices as unneccesary
17285 BANKSELs migth be removed.
17289 \layout Subsubsection
17294 Currently, data can only be initialized if it resides in the source file
17300 Data in other source files will silently
17308 \begin_inset LatexCommand \index{PIC16}
17316 \begin_inset LatexCommand \index{PIC16}
17320 port is the portion of SDCC that is responsible to produce code for the
17322 \begin_inset LatexCommand \index{Microchip}
17326 (TM) microcontrollers with 16 bit core.
17327 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
17328 Currently supported devices are:
17332 \begin_inset Tabular
17333 <lyxtabular version="3" rows="4" columns="6">
17335 <column alignment="center" valignment="top" leftline="true" width="0">
17336 <column alignment="center" valignment="top" leftline="true" width="0">
17337 <column alignment="center" valignment="top" leftline="true" width="0">
17338 <column alignment="center" valignment="top" leftline="true" width="0">
17339 <column alignment="center" valignment="top" leftline="true" width="0">
17340 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17341 <row topline="true">
17342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17350 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17366 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17374 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17391 <row topline="true">
17392 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17400 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17441 <row topline="true">
17442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17450 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17458 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17466 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17474 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17491 <row topline="true" bottomline="true">
17492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17500 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17508 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17516 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17523 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17530 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17548 PIC16 port supports the standard command line arguments as supposed, with
17549 the exception of certain cases that will be mentioned in the following
17552 \labelwidthstring 00.00.0000
17564 -callee-saves See -
17576 \labelwidthstring 00.00.0000
17588 -all-callee-saves All function arguments are passed on stack by default.
17591 There is no need to specify this in the command line.
17593 \labelwidthstring 00.00.0000
17605 -fommit-frame-pointer Frame pointer will be omitted when the function uses
17606 no local variables.
17609 Port Specific Options
17610 \begin_inset LatexCommand \index{Options PIC16}
17617 The port specific options appear after the global options in the sdcc --help
17619 \layout Subsubsection
17624 General options enable certain port features and optimizations.
17626 \labelwidthstring 00.00.0000
17638 -stack-model=[model] Used in conjuction with the command above.
17639 Defines the stack model to be used, valid stack models are :
17642 \labelwidthstring 00.00.0000
17648 Selects small stack model.
17649 8 bit stack and frame pointers.
17650 Supports 256 bytes stack size.
17652 \labelwidthstring 00.00.0000
17658 Selects large stack model.
17659 16 bit stack and frame pointers.
17660 Supports 65536 bytes stack size.
17663 \labelwidthstring 00.00.0000
17675 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
17676 unitialized data variables with [kword].
17677 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
17679 \labelwidthstring 00.00.0000
17691 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
17692 Useful for bootloaders.
17694 \labelwidthstring 00.00.0000
17706 -asm= sets the full path and name of an external assembler to call.
17708 \labelwidthstring 00.00.0000
17720 -link= sets the full path and name of an external linker to call.
17722 \labelwidthstring 00.00.0000
17734 -mplab-comp MPLAB compatibility option.
17735 Currently only suppresses special gpasm directives.
17736 \layout Subsubsection
17738 Optimization Options
17740 \labelwidthstring 00.00.0000
17752 -optimize-goto Try to use (conditional) BRA instead of GOTO
17754 \labelwidthstring 00.00.0000
17766 -optimize-cmp Try to optimize some compares.
17768 \labelwidthstring 00.00.0000
17780 -optimize-df Analyze the dataflow of the generated code and improve it.
17782 \labelwidthstring 00.00.0000
17794 -obanksel=nn Set optimization level for inserting BANKSELs.
17799 \labelwidthstring 00.00.0000
17803 \labelwidthstring 00.00.0000
17805 1 checks previous used register and if it is the same then does not emit
17806 BANKSEL, accounts only for labels.
17808 \labelwidthstring 00.00.0000
17810 2 tries to check the location of (even different) symbols and removes BANKSELs
17811 if they are in the same bank.
17816 Important: There might be problems if the linker script has data sections
17817 across bank borders!
17819 \layout Subsubsection
17823 \labelwidthstring 00.00.0000
17835 -nodefaultlibs do not link default libraries when linking
17837 \labelwidthstring 00.00.0000
17849 -no-crt Don't link the default run-time modules
17851 \labelwidthstring 00.00.0000
17863 -use-crt= Use a custom run-time module instead of the defaults.
17864 \layout Subsubsection
17869 Debugging options enable extra debugging information in the output files.
17871 \labelwidthstring 00.00.0000
17883 -debug-xtra Similar to -
17894 \begin_inset LatexCommand \index{-\/-debug}
17898 , but dumps more information.
17900 \labelwidthstring 00.00.0000
17912 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17914 <source> is the name of the file compiled.
17916 \labelwidthstring 00.00.0000
17928 -pcode-verbose Enable pcode debugging information in translation.
17930 \labelwidthstring 00.00.0000
17942 -denable-peeps Force the usage of peepholes.
17945 \labelwidthstring 00.00.0000
17957 -gstack Trace push/pops for stack pointer overflow
17959 \labelwidthstring 00.00.0000
17971 -call-tree dump call tree in .calltree file
17974 Enviromental Variables
17977 There is a number of enviromental variables that can be used when running
17978 SDCC to enable certain optimizations or force a specific program behaviour.
17979 these variables are primarily for debugging purposes so they can be enabled/dis
17983 Currently there is only two such variables available:
17985 \labelwidthstring 00.00.0000
17987 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17988 bitfields is optimized by directly loading FSR0 with the address of the
17989 bitfield structure.
17990 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17992 This step saves data ram and code space for functions that perform heavy
17995 80 bytes of code space are saved when compiling malloc.c with this option).
17998 \labelwidthstring 00.00.0000
18000 NO_REG_OPT do not perform pCode registers optimization.
18001 This should be used for debugging purposes.
18002 In some where bugs in the pcode optimizer are found, users can benefit
18003 from temporarily disabling the optimizer until the bug is fixed.
18006 Preprocessor Macros
18009 PIC16 port defines the following preprocessor macros while translating a
18014 \begin_inset Tabular
18015 <lyxtabular version="3" rows="6" columns="2">
18017 <column alignment="center" valignment="top" leftline="true" width="0">
18018 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18019 <row topline="true" bottomline="true">
18020 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18028 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18037 <row topline="true">
18038 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18046 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18051 Port identification
18055 <row topline="true">
18056 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18074 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18079 Port identification (same as above)
18083 <row topline="true">
18084 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18092 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18097 MCU Identification.
18102 is the microcontrol identification number, i.e.
18107 <row topline="true">
18108 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18131 MCU Identification (same as above)
18135 <row topline="true" bottomline="true">
18136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18149 nnn = SMALL or LARGE respectively according to the stack model used
18160 In addition the following macros are defined when calling assembler:
18164 \begin_inset Tabular
18165 <lyxtabular version="3" rows="4" columns="2">
18167 <column alignment="center" valignment="top" leftline="true" width="0">
18168 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18169 <row topline="true" bottomline="true">
18170 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18178 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
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 MCU Identification.
18206 is the microcontrol identification number, i.e.
18211 <row topline="true">
18212 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18220 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18225 nnn = SMALL or LARGE respectively according to the memory model used for
18230 <row topline="true" bottomline="true">
18231 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18239 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18244 nnn = SMALL or LARGE respectively according to the stack model used
18259 \begin_inset LatexCommand \index{PIC16}
18263 port uses the following directories for searching header files and libraries.
18267 \begin_inset Tabular
18268 <lyxtabular version="3" rows="3" columns="4">
18270 <column alignment="center" valignment="top" leftline="true" width="0">
18271 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18272 <column alignment="center" valignment="top" width="0">
18273 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18274 <row topline="true" bottomline="true">
18275 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18283 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18291 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18299 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18308 <row topline="true">
18309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18314 PREFIX/sdcc/include/pic16
18317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18322 PIC16 specific headers
18325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18342 <row topline="true" bottomline="true">
18343 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18348 PREFIX/sdcc/lib/pic16
18351 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18356 PIC16 specific libraries
18359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18367 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18384 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
18391 PIC16 port currently supports the following pragmas:
18393 \labelwidthstring 00.00.0000
18395 stack pragma stack forces the code generator to initialize the stack & frame
18396 pointers at a specific address.
18397 This is an adhoc solution for cases where no STACK directive is available
18398 in the linker script or gplink is not instructed to create a stack section.
18400 The stack pragma should be used only once in a project.
18401 Multiple pragmas may result in indeterminate behaviour of the program.
18407 The old format (ie.
18408 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
18409 cross page boundaries (or even exceed the available data RAM) and crash
18411 Make sure that stack does not cross page boundaries when using the SMALL
18417 The format is as follows:
18420 #pragma stack bottom_address [stack_size]
18427 is the lower bound of the stack section.
18428 The stack pointer initially will point at address (bottom_address+stack_size-1).
18436 /* initializes stack of 100 bytes at RAM address 0x200 */
18439 #pragma stack 0x200 100
18442 If the stack_size field is omitted then a stack is created with the default
18444 This size might be enough for most programs, but its not enough for operations
18445 with deep function nesting or excessive stack usage.
18447 \labelwidthstring 00.00.0000
18451 This pragma is deprecated.
18452 Its use will cause a warning message to be issued.
18458 \labelwidthstring 00.00.0000
18460 code place a function symbol at static FLASH address
18468 /* place function test_func at 0x4000 */
18471 #pragma code test_func 0x4000
18475 \labelwidthstring 00.00.0000
18477 library instructs the linker to use a library module.
18482 #pragma library module_name
18489 can be any library or object file (including its path).
18490 Note that there are four reserved keywords which have special meaning.
18495 \begin_inset Tabular
18496 <lyxtabular version="3" rows="6" columns="3">
18498 <column alignment="center" valignment="top" leftline="true" width="0">
18499 <column alignment="block" valignment="top" leftline="true" width="20page%">
18500 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
18501 <row topline="true" bottomline="true">
18502 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18527 <row topline="true">
18528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18538 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18543 ignore all library pragmas
18546 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18557 <row topline="true">
18558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18568 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18576 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18589 <row topline="true">
18590 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18600 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18605 link the Math libarary
18608 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18621 <row topline="true">
18622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18637 link the I/O library
18640 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18653 <row topline="true" bottomline="true">
18654 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18669 link the debug library
18672 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18691 * is the device number, i.e.
18692 452 for PIC18F452 MCU.
18695 This feature allows for linking with specific libraries withoug having to
18696 explicit name them in the command line.
18701 keyword will reject all modules specified by the library pragma.
18703 \labelwidthstring 00.00.0000
18705 udata pragma udata instructs the compiler to emit code so that linker will
18706 place a variable at a specific memory bank
18714 /* places variable foo at bank2 */
18717 #pragma udata bank2 foo
18723 In order for this pragma to work extra SECTION directives should be added
18724 in the .lkr script.
18725 In the following example a sample .lkr file is shown:
18730 // Sample linker script for the PIC18F452 processor
18736 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18739 CODEPAGE NAME=page START=0x2A END=0x7FFF
18742 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18745 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18748 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18751 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18754 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18759 DATABANK NAME=gpr0 START=0x80 END=0xFF
18762 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18765 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18768 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18771 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18774 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18777 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18782 SECTION NAME=CONFIG ROM=config
18787 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18790 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18793 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18796 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18799 SECTION NAME=bank4 RAM=gpr4
18802 SECTION NAME=bank5 RAM=gpr5
18805 The linker will recognise the section name set in the pragma statement and
18806 will position the variable at the memory bank set with the RAM field at
18807 the SECTION line in the linker script file.
18811 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18818 There is one main header file that can be included to the source files using
18825 This header file contains the definitions for the processor special registers,
18826 so it is necessary if the source accesses them.
18827 It can be included by adding the following line in the beginning of the
18831 #include <pic18fregs.h>
18834 The specific microcontroller is selected within the pic18fregs.h automatically,
18835 so the same source can be used with a variety of devices.
18841 The libraries that PIC16
18842 \begin_inset LatexCommand \index{PIC16}
18846 port depends on are the microcontroller device libraries which contain
18847 the symbol definitions for the microcontroller special function registers.
18848 These libraries have the format pic18fxxxx.lib, where
18852 is the microcontroller identification number.
18853 The specific library is selected automatically by the compiler at link
18854 stage according to the selected device.
18857 Libraries are created with gplib which is part of the gputils package
18858 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18863 \layout Subsubsection*
18865 Building the libraries
18868 Before using SDCC/pic16 there are some libraries that need to be compiled.
18869 This process is not done automatically by SDCC since not all users use
18870 SDCC for pic16 projects.
18871 So each user should compile the libraries separately.
18874 The steps to compile the pic16 libraries under Linux are:
18877 cd device/lib/pic16
18892 su -c 'make install' # install the libraries, you need the root password
18895 If you need to install the headers too, do:
18901 su -c 'make install' # install the headers, you need the root password
18904 There exist a special target to build the I/O libraries.
18905 This target is not automatically build because it will build the I/O library
18911 This way building will take quite a lot of time.
18912 Users are advised to edit the
18914 device/lib/pic16/pics.build
18916 file and then execute:
18925 The following memory models are supported by the PIC16 port:
18934 Memory model affects the default size of pointers within the source.
18935 The sizes are shown in the next table:
18939 \begin_inset Tabular
18940 <lyxtabular version="3" rows="3" columns="3">
18942 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18943 <column alignment="center" valignment="top" leftline="true" width="0">
18944 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18945 <row topline="true" bottomline="true">
18946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18951 Pointer sizes according to memory model
18954 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18962 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18971 <row topline="true" bottomline="true">
18972 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18980 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18988 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18997 <row topline="true" bottomline="true">
18998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19006 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
19014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19030 It is advisable that all sources within a project are compiled with the
19032 If one wants to override the default memory model, this can be done by
19033 declaring a pointer as
19042 Far selects large memory model's pointers, while near selects small memory
19046 The standard device libraries (see
19047 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
19051 ) contain no reference to pointers, so they can be used with both memory
19058 The stack implementation for the PIC16 port uses two indirect registers,
19061 \labelwidthstring 00.00.0000
19063 FSR1 is assigned as stack pointer
19065 \labelwidthstring 00.00.0000
19067 FSR2 is assigned as frame pointer
19070 The following stack models are supported by the PIC16 port
19091 model means that only the FSRxL byte is used to access stack and frame,
19098 uses both FSRxL and FSRxH registers.
19099 The following table shows the stack/frame pointers sizes according to stack
19100 model and the maximum space they can address:
19104 \begin_inset Tabular
19105 <lyxtabular version="3" rows="3" columns="3">
19107 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19108 <column alignment="center" valignment="top" leftline="true" width="0">
19109 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19110 <row topline="true" bottomline="true">
19111 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19116 Stack & Frame pointer sizes according to stack model
19119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19136 <row topline="true">
19137 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19145 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19162 <row topline="true" bottomline="true">
19163 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19179 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19199 stack model is currently not working properly throughout the code generator.
19200 So its use is not advised.
19201 Also there are some other points that need special care:
19206 Do not create stack sections with size more than one physical bank (that
19210 Stack sections should no cross physical bank limits (i.e.
19211 #pragma stack 0x50 0x100)
19214 These limitations are caused by the fact that only FSRxL is modified when
19215 using SMALL stack model, so no more than 256 bytes of stack can be used.
19216 This problem will disappear after LARGE model is fully implemented.
19222 In addition to the standard SDCC function keywords, PIC16 port makes available
19225 \labelwidthstring 00.00.0000
19227 wparam Use the WREG to pass one byte of the first function argument.
19228 This improves speed but you may not use this for functions with arguments
19229 that are called via function pointers, otherwise the first byte of the
19230 first parameter will get lost.
19234 void func_wparam(int a) wparam
19240 /* WREG hold the lower part of a */
19243 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
19253 This keyword replaces the deprecated wparam pragma.
19255 \labelwidthstring 00.00.0000
19257 shadowregs When entering/exiting an ISR, it is possible to take advantage
19258 of the PIC18F hardware shadow registers which hold the values of WREG,
19259 STATUS and BSR registers.
19260 This can be done by adding the keyword
19268 keyword in the function's header.
19271 void isr_shadow(void) shadowregs interrupt 1
19287 instructs the code generator not to store/restore WREG, STATUS, BSR when
19288 entering/exiting the ISR.
19291 Function return values
19294 Return values from functions are placed to the appropriate registers following
19295 a modified Microchip policy optimized for SDCC.
19296 The following table shows these registers:
19300 \begin_inset Tabular
19301 <lyxtabular version="3" rows="6" columns="2">
19303 <column alignment="center" valignment="top" leftline="true" width="0">
19304 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19305 <row topline="true" bottomline="true">
19306 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19314 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19319 destination register
19323 <row topline="true">
19324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19332 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19341 <row topline="true">
19342 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19359 <row topline="true">
19360 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19368 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19377 <row topline="true">
19378 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19386 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19391 FSR0L:PRODH:PRODL:WREG
19395 <row topline="true" bottomline="true">
19396 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19404 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19409 on stack, FSR0 points to the beginning
19423 An interrupt servive routine (ISR) is declared using the
19430 void isr(void) interrupt
19448 is the interrupt number, which for PIC18F devices can be:
19452 \begin_inset Tabular
19453 <lyxtabular version="3" rows="4" columns="3">
19455 <column alignment="center" valignment="top" leftline="true" width="0">
19456 <column alignment="center" valignment="top" leftline="true" width="0">
19457 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19458 <row topline="true" bottomline="true">
19459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19482 Interrupt Vector Address
19486 <row topline="true">
19487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19512 <row topline="true">
19513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19530 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19544 HIGH priority interrupts
19547 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19556 <row topline="true" bottomline="true">
19557 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19565 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19570 LOW priority interrupts
19573 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19589 When generating assembly code for ISR the code generator places a
19595 Interrupt Vector Address
19597 which points at the genetated ISR.
19598 This single GOTO instruction is part of an automatically generated
19600 interrupt entry point
19603 The actuall ISR code is placed as normally would in the code space.
19604 Upon interrupt request, the GOTO instruction is executed which jumps to
19606 When declaring interrupt functions as _naked this GOTO instruction is
19611 The whole interrupt functions is therefore placed at the Interrupt Vector
19612 Address of the specific interrupt.
19613 This is not a problem for the LOW priority interrupts, but it is a problem
19614 for the RESET and the HIGH priority interrupts because code may be written
19615 at the next interrupt´s vector address and cause undeterminate program
19616 behaviour if that interrupt is raised.
19622 This is not a problem when
19625 this is a HIGH interrupt ISR and LOW interrupts are
19632 when the ISR is small enough not to reach the next interrupt´s vector address.
19642 is possible to be omitted.
19643 This way a function is generated similar to an ISR, but it is not assigned
19647 When entering an interrupt, currently the PIC16
19648 \begin_inset LatexCommand \index{PIC16}
19652 port automatically saves the following registers:
19664 PROD (PRODL and PRODH)
19667 FSR0 (FSR0L and FSR0H)
19670 These registers are restored upon return from the interrupt routine.
19676 NOTE that when the _naked attribute is specified for an interrupt routine,
19677 then NO registers are stored or restored.
19686 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
19687 There are 3 types of generic pointers currently implemented data, code
19688 and eeprom pointers.
19689 They are differentiated by the value of the 7th and 6th bits of the upper
19694 \begin_inset Tabular
19695 <lyxtabular version="3" rows="5" columns="5">
19697 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19698 <column alignment="center" valignment="top" width="0">
19699 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19700 <column alignment="center" valignment="top" width="0">
19701 <column alignment="left" valignment="top" rightline="true" width="0">
19702 <row topline="true" bottomline="true">
19703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19711 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19727 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19732 rest of the pointer
19735 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19744 <row topline="true" bottomline="true">
19745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19761 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19769 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19778 uuuuuu uuuuxxxx xxxxxxxx
19781 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19786 a 12-bit data pointer in data RAM memory
19790 <row bottomline="true">
19791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19807 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19815 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19824 uxxxxx xxxxxxxx xxxxxxxx
19827 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19832 a 21-bit code pointer in FLASH memory
19836 <row bottomline="true">
19837 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19845 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19853 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19861 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19870 uuuuuu uuuuuuxx xxxxxxxx
19873 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19878 a 10-bit eeprom pointer in EEPROM memory
19882 <row bottomline="true">
19883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19899 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19907 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19916 xxxxxx xxxxxxxx xxxxxxxx
19919 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19924 unimplemented pointer type
19935 Generic pointer are read and written with a set of library functions which
19936 read/write 1, 2, 3, 4 bytes.
19940 \layout Subsubsection
19942 Standard I/O Streams
19949 the type FILE is defined as:
19952 typedef char * FILE;
19955 This type is the stream type implemented I/O in the PIC18F devices.
19956 Also the standard input and output streams are declared in stdio.h:
19959 extern FILE * stdin;
19962 extern FILE * stdout;
19965 The FILE type is actually a generic pointer which defines one more type
19966 of generic pointers, the
19971 This new type has the format:
19975 \begin_inset Tabular
19976 <lyxtabular version="3" rows="2" columns="7">
19978 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19979 <column alignment="center" valignment="top" width="0">
19980 <column alignment="center" valignment="top" leftline="true" width="0">
19981 <column alignment="center" valignment="top" leftline="true" width="0">
19982 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19983 <column alignment="center" valignment="top" width="0">
19984 <column alignment="left" valignment="top" rightline="true" width="0">
19985 <row topline="true" bottomline="true">
19986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20031 rest of the pointer
20034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20043 <row topline="true" bottomline="true">
20044 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20052 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20060 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20084 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20096 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20101 upper byte high nubble is 0x2n, the rest are zeroes
20112 Currently implemented there are 3 types of streams defined:
20116 \begin_inset Tabular
20117 <lyxtabular version="3" rows="4" columns="4">
20119 <column alignment="center" valignment="top" leftline="true" width="0">
20120 <column alignment="center" valignment="top" leftline="true" width="0">
20121 <column alignment="center" valignment="top" leftline="true" width="0">
20122 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20123 <row topline="true" bottomline="true">
20124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20132 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20140 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20157 <row topline="true">
20158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20176 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20189 Writes/Reads characters via the USART peripheral
20193 <row topline="true">
20194 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20212 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20220 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20225 Writes/Reads characters via the MSSP peripheral
20229 <row topline="true" bottomline="true">
20230 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20238 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20248 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20256 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20261 Writes/Reads characters via used defined functions
20272 The stream identifiers are declared as macros in the stdio.h header.
20275 In the libc library there exist the functions that are used to write to
20276 each of the above streams.
20279 \labelwidthstring 00.00.0000
20291 _stream_usart_putchar writes a character at the USART stream
20293 \labelwidthstring 00.00.0000
20305 _stream_mssp_putchar writes a character at the MSSP stream
20307 \labelwidthstring 00.00.0000
20309 putchar dummy function.
20310 This writes a character to a user specified manner.
20313 In order to increase performance
20317 is declared in stdio.h as having its parameter in WREG (it has the wparam
20319 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
20320 in a user-friendly way.
20325 is the name of the variable that holds the character to print.
20326 An example follows:
20329 #include <pic18fregs.h>
20341 PORTA = c; /* dump character c to PORTA */
20354 stdout = STREAM_USER; /* this is not necessary, since stdout points
20357 * by default to STREAM_USER */
20360 printf (¨This is a printf test
20368 \layout Subsubsection
20373 PIC16 contains an implementation of the printf-family of functions.
20374 There exist the following functions:
20377 extern unsigned int sprintf(char *buf, char *fmt, ...);
20380 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
20385 extern unsigned int printf(char *fmt, ...);
20388 extern unsigned int vprintf(char *fmt, va_lista ap);
20393 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
20396 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
20399 For sprintf and vsprintf
20403 should normally be a data pointer where the resulting string will be placed.
20404 No range checking is done so the user should allocate the necessery buffer.
20405 For fprintf and vfprintf
20409 should be a stream pointer (i.e.
20410 stdout, STREAM_MSSP, etc...).
20411 \layout Subsubsection
20416 The PIC18F family of microcontrollers supports a number of interrupt sources.
20417 A list of these interrupts is shown in the following table:
20421 \begin_inset Tabular
20422 <lyxtabular version="3" rows="11" columns="4">
20424 <column alignment="left" valignment="top" leftline="true" width="0">
20425 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20426 <column alignment="left" valignment="top" leftline="true" width="0">
20427 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
20428 <row topline="true" bottomline="true">
20429 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20437 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20445 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20453 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20462 <row topline="true">
20463 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20471 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20476 PORTB change interrupt
20479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20487 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20492 EEPROM/FLASH write complete interrupt
20496 <row topline="true">
20497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20505 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20510 INT0 external interrupt
20513 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20521 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20526 Bus collision interrupt
20530 <row topline="true">
20531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20544 INT1 external interrupt
20547 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20555 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20560 Low voltage detect interrupt
20564 <row topline="true">
20565 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20573 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20578 INT2 external interrupt
20581 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20589 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20594 Parallel slave port interrupt
20598 <row topline="true">
20599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20607 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20612 CCP1 module interrupt
20615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20623 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20628 AD convertion complete interrupt
20632 <row topline="true">
20633 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20641 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20646 CCP2 module interrupt
20649 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20662 USART receive interrupt
20666 <row topline="true">
20667 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20675 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20680 TMR0 overflow interrupt
20683 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20691 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20696 USART transmit interrupt
20700 <row topline="true">
20701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20714 TMR1 overflow interrupt
20717 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20725 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20730 SSP receive/transmit interrupt
20734 <row topline="true">
20735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20743 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20748 TMR2 matches PR2 interrupt
20751 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20758 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20766 <row topline="true" bottomline="true">
20767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20775 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20780 TMR3 overflow interrupt
20783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20790 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20805 The prototypes for these names are defined in the header file
20812 In order to simplify signal handling, a number of macros is provided:
20814 \labelwidthstring 00.00.0000
20816 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20817 high priority interrupts.
20822 is the function name to use.
20824 \labelwidthstring 00.00.0000
20826 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20827 low priority interrupt.
20832 is the function name to use.
20834 \labelwidthstring 00.00.0000
20836 DEF_HANDLER(sig,handler) define a handler for signal
20840 \labelwidthstring 00.00.0000
20842 END_DEF end the declaration of the dispatch table.
20845 Additionally there are two more macros to simplify the declaration of the
20848 \labelwidthstring 00.00.0000
20852 SIGHANDLER(handler)
20854 this declares the function prototype for the
20860 \labelwidthstring 00.00.0000
20862 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20865 An example of using the macros above is shown below:
20868 #include <pic18fregs.h>
20871 #include <signal.h>
20875 DEF_INTHIGH(high_int)
20878 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20881 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20888 SIGHANDLER(_tmr0_handler)
20894 /* action to be taken when timer 0 overflows */
20901 SIGHANDLERNAKED(_bcol_handler)
20910 /* action to be taken when bus collision occurs */
20926 Special care should be taken when using the above scheme:
20929 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20932 when declaring SIGHANDLERNAKED handler never forget to use
20936 for proper returning.
20942 Here you can find some general tips for compiling programs with SDCC/pic16.
20943 \layout Subsubsection
20948 The default stack size (that is 64 bytes) probably is enough for many programs.
20949 One must take care that when there are many levels of function nesting,
20950 or there is excessive usage of stack, its size should be extended.
20951 An example of such a case is the printf/sprintf family of functions.
20952 If you encounter problems like not being able to print integers, then you
20953 need to set the stack size around the maximum (256 for small stack model).
20954 The following diagram shows what happens when calling printf to print an
20958 printf () --> ltoa () --> ultoa () --> divschar ()
20961 It is should be understood that stack is easily consumed when calling complicate
20963 Using command line arguments like -
20973 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20975 Other ways to reduce stack usage may exist.
20981 There are several approaches to debugging your code.
20982 This chapter is meant to show your options and to give detail on some of
20987 When writing your code:
20990 write your code with debugging in mind (avoid duplicating code, put conceptually
20991 similar variables into structs, use structured code, have strategic points
20992 within your code where all variables are consistent, ...)
20995 run a syntax-checking tool like splint
20996 \begin_inset LatexCommand \index{splint (syntax checking tool)}
21001 \begin_inset LatexCommand \index{lint (syntax checking tool)}
21016 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21023 for the high level code use a C-compiler (like f.e.
21024 GCC) to compile run and debug the code on your host.
21036 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
21040 ) on howto handle syntax extensions like __xdata, __at(), ...
21044 use another C-compiler to compile code for your target.
21045 Always an option but not recommended:) And not very likely to help you.
21046 If you seriously consider walking this path you should at least occasionally
21047 check portability of your code.
21048 Most commercial compiler vendors will offer an evaluation version so you
21049 can test compile your code or snippets of your code.
21052 Debugging on a simulator:
21055 there is a separate section about SDCDB (section
21056 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
21063 or (8051 specific) use a freeware/commercial simulator which interfaces
21065 \begin_inset LatexCommand \index{AOMF, AOMF51}
21070 \begin_inset LatexCommand \ref{OMF file}
21074 ) optionally generated by SDCC.
21077 Debugging On-target:
21080 use a MCU port pin to serially output debug data to the RS232 port of your
21082 You'll probably want some level shifting device typically involving a MAX232
21084 If the hardware serial port of the MCU is not available search for 'Software
21085 UART' in your favourite search machine.
21088 use an on-target monitor.
21089 In this context a monitor is a small program which usually accepts commands
21090 via a serial line and allows to set program counter, to single step through
21091 a program and read/write memory locations.
21092 For the 8051 good examples of monitors are paulmon and cmon51 (see section
21094 \begin_inset LatexCommand \ref{sec:Related-open-source-tools}
21101 toggle MCU port pins at strategic points within your code and use an oscilloscop
21105 digital oscilloscope
21108 \begin_inset LatexCommand \index{oscilloscope}
21112 with deep trace memory is really helpful especially if you have to debug
21113 a realtime application.
21114 If you need to monitor more pins than your oscilloscope provides you can
21115 sometimes get away with a small R-2R network.
21116 On a single channel oscilloscope you could f.e.
21117 monitor 2 push-pull driven pins by connecting one via a 10\SpecialChar ~
21119 \begin_inset Formula $\Omega$
21122 resistor and the other one by a 5\SpecialChar ~
21124 \begin_inset Formula $\Omega$
21127 resistor to the oscilloscope probe (check output drive capability of the
21128 pins you want to monitor).
21129 If you need to monitor many more pins a
21149 \begin_inset LatexCommand \index{ICE (in circuit emulator)}
21154 Usually very expensive.
21155 And very nice to have too.
21156 And usually locks you (for years...) to the devices the ICE can emulate.
21160 use a remote debugger.
21161 In most 8-bit systems the symbol information is not available on the target,
21162 and a complete debugger is too bulky for the target system.
21163 Therefore usually a debugger on the host system connects to an on-target
21164 debugging stub which accepts only primitive commands.
21167 Terms to enter into your favourite search engine could be 'remote debugging',
21168 'gdb stub' or 'inferior debugger'.
21172 use an on target hardware debugger.
21173 Some of the more modern MCUs include hardware support for setting break
21174 points and monitoring/changing variables by using dedicated hardware pins.
21175 This facility doesn't require additional code to run on the target and
21180 doesn't affect runtime behaviour until a breakpoint is hit.
21181 For the mcs51 most hardware debuggers use the AOMF
21182 \begin_inset LatexCommand \index{AOMF, AOMF51}
21187 \begin_inset LatexCommand \ref{OMF file}
21198 if you are not familiar with any of the following terms you're likely to
21199 run into problems rather sooner than later:
21216 As an embedded programmer you
21220 to know them so why not look them up
21224 you have problems?)
21227 tell someone else about your problem (actually this is a surprisingly effective
21228 means to hunt down the bug even if the listener is not familiar with your
21230 As 'failure to communicate' is probably one of the job-induced deformations
21231 of an embedded programmer this is highly encouraged.
21234 Debugging with SDCDB
21235 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
21240 \begin_inset LatexCommand \index{SDCDB (debugger)}
21247 SDCC is distributed with a source level debugger
21248 \begin_inset LatexCommand \index{Debugger}
21253 The debugger uses a command line interface, the command repertoire of the
21254 debugger has been kept as close to gdb
21255 \begin_inset LatexCommand \index{gdb}
21259 (the GNU debugger) as possible.
21260 The configuration and build process is part of the standard compiler installati
21261 on, which also builds and installs the debugger in the target directory
21262 specified during configuration.
21263 The debugger allows you debug BOTH at the C source and at the ASM source
21267 Compiling for Debugging
21281 \begin_inset LatexCommand \index{-\/-debug}
21285 option must be specified for all files for which debug information is to
21287 The compiler generates a .adb file for each of these files.
21288 The linker creates the .cdb
21289 \begin_inset LatexCommand \index{<file>.cdb}
21294 \begin_inset LatexCommand \index{<file>.adb}
21298 files and the address information.
21299 This .cdb is used by the debugger.
21302 How the Debugger Works
21315 -debug option is specified the compiler generates extra symbol information
21316 some of which are put into the assembler source and some are put into the
21318 Then the linker creates the .cdb file from the individual .adb files with
21319 the address information for the symbols.
21320 The debugger reads the symbolic information generated by the compiler &
21321 the address information generated by the linker.
21322 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
21323 execution is controlled by the debugger.
21324 When a command is issued for the debugger, it translates it into appropriate
21325 commands for the simulator.
21326 (Currently SDCDM only connects to the simulator but
21331 \begin_inset LatexCommand \url{http://ec2drv.sf.net/}
21335 is an effort to connect directly to the hardware.)
21338 Starting the Debugger SDCDB
21341 The debugger can be started using the following command line.
21342 (Assume the file you are debugging has the file name foo).
21356 The debugger will look for the following files.
21359 foo.c - the source file.
21362 foo.cdb - the debugger symbol information file.
21365 foo.ihx - the Intel hex format
21366 \begin_inset LatexCommand \index{Intel hex format}
21373 SDCDB Command Line Options
21386 -directory=<source file directory> this option can used to specify the directory
21388 The debugger will look into the directory list specified for source, cdb
21390 The items in the directory list must be separated by ':', e.g.
21391 if the source files can be in the directories /home/src1 and /home/src2,
21402 -directory option should be -
21412 -directory=/home/src1:/home/src2.
21413 Note there can be no spaces in the option.
21417 -cd <directory> - change to the <directory>.
21420 -fullname - used by GUI front ends.
21423 -cpu <cpu-type> - this argument is passed to the simulator please see the
21424 simulator docs for details.
21427 -X <Clock frequency > this options is passed to the simulator please see
21428 the simulator docs for details.
21431 -s <serial port file> passed to simulator see the simulator docs for details.
21434 -S <serial in,out> passed to simulator see the simulator docs for details.
21437 -k <port number> passed to simulator see the simulator docs for details.
21440 SDCDB Debugger Commands
21443 As mentioned earlier the command interface for the debugger has been deliberatel
21444 y kept as close the GNU debugger gdb, as possible.
21445 This will help the integration with existing graphical user interfaces
21446 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
21447 If you use a graphical user interface for the debugger you can skip this
21449 \layout Subsubsection*
21451 break [line | file:line | function | file:function]
21454 Set breakpoint at specified line or function:
21463 sdcdb>break foo.c:100
21465 sdcdb>break funcfoo
21467 sdcdb>break foo.c:funcfoo
21468 \layout Subsubsection*
21470 clear [line | file:line | function | file:function ]
21473 Clear breakpoint at specified line or function:
21482 sdcdb>clear foo.c:100
21484 sdcdb>clear funcfoo
21486 sdcdb>clear foo.c:funcfoo
21487 \layout Subsubsection*
21492 Continue program being debugged, after breakpoint.
21493 \layout Subsubsection*
21498 Execute till the end of the current function.
21499 \layout Subsubsection*
21504 Delete breakpoint number 'n'.
21505 If used without any option clear ALL user defined break points.
21506 \layout Subsubsection*
21508 info [break | stack | frame | registers ]
21511 info break - list all breakpoints
21514 info stack - show the function call stack.
21517 info frame - show information about the current execution frame.
21520 info registers - show content of all registers.
21521 \layout Subsubsection*
21526 Step program until it reaches a different source line.
21527 Note: pressing <return> repeats the last command.
21528 \layout Subsubsection*
21533 Step program, proceeding through subroutine calls.
21534 \layout Subsubsection*
21539 Start debugged program.
21540 \layout Subsubsection*
21545 Print type information of the variable.
21546 \layout Subsubsection*
21551 print value of variable.
21552 \layout Subsubsection*
21557 load the given file name.
21558 Note this is an alternate method of loading file for debugging.
21559 \layout Subsubsection*
21564 print information about current frame.
21565 \layout Subsubsection*
21570 Toggle between C source & assembly source.
21571 \layout Subsubsection*
21573 ! simulator command
21576 Send the string following '!' to the simulator, the simulator response is
21578 Note the debugger does not interpret the command being sent to the simulator,
21579 so if a command like 'go' is sent the debugger can loose its execution
21580 context and may display incorrect values.
21581 \layout Subsubsection*
21588 My name is Bobby Brown"
21591 Interfacing SDCDB with DDD
21594 The screenshot was converted from png to eps with:
21595 \begin_inset Quotes sld
21598 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
21599 \begin_inset Quotes srd
21602 which produces a pretty compact eps file which is free from compression
21606 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
21607 as this broke the build system on Sourceforge (pdf-file was broken).
21613 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
21619 shows a screenshot of a debugging session with DDD
21620 \begin_inset LatexCommand \index{DDD (debugger)}
21624 (Unix only) on a simulated 8032.
21625 The debugging session might not run as smoothly as the screenshot suggests.
21626 The debugger allows setting of breakpoints, displaying and changing variables,
21627 single stepping through C and assembler code.
21630 The source was compiled with
21653 -debug ddd_example.c
21666 and DDD was invoked with
21673 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
21676 Interfacing SDCDB with XEmacs
21677 \begin_inset LatexCommand \index{XEmacs}
21682 \begin_inset LatexCommand \index{Emacs}
21689 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
21690 sdcdb.el and sdcdbsrc.el.
21691 These two files can be found in the $(prefix)/bin directory after the installat
21693 These files need to be loaded into XEmacs for the interface to work.
21694 This can be done at XEmacs startup time by inserting the following into
21695 your '.xemacs' file (which can be found in your HOME directory):
21701 (load-file sdcdbsrc.el)
21707 .xemacs is a lisp file so the () around the command is REQUIRED.
21708 The files can also be loaded dynamically while XEmacs is running, set the
21709 environment variable 'EMACSLOADPATH' to the installation bin directory
21710 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
21711 To start the interface enter the following command:
21725 You will prompted to enter the file name to be debugged.
21730 The command line options that are passed to the simulator directly are bound
21731 to default values in the file sdcdbsrc.el.
21732 The variables are listed below, these values maybe changed as required.
21735 sdcdbsrc-cpu-type '51
21738 sdcdbsrc-frequency '11059200
21741 sdcdbsrc-serial nil
21744 The following is a list of key mapping for the debugger interface.
21755 ;;key\SpecialChar ~
21769 binding\SpecialChar ~
21793 ;;---\SpecialChar ~
21807 -------\SpecialChar ~
21849 sdcdb-next-from-src\SpecialChar ~
21877 sdcdb-back-from-src\SpecialChar ~
21905 sdcdb-cont-from-src\SpecialChar ~
21915 SDCDB continue command
21933 sdcdb-step-from-src\SpecialChar ~
21961 sdcdb-whatis-c-sexp\SpecialChar ~
21971 SDCDB ptypecommand for data at
22038 sdcdbsrc-delete\SpecialChar ~
22052 SDCDB Delete all breakpoints if no arg
22101 given or delete arg (C-u arg x)
22119 sdcdbsrc-frame\SpecialChar ~
22134 SDCDB Display current frame if no arg,
22183 given or display frame arg
22250 sdcdbsrc-goto-sdcdb\SpecialChar ~
22260 Goto the SDCDB output buffer
22278 sdcdb-print-c-sexp\SpecialChar ~
22289 SDCDB print command for data at
22356 sdcdbsrc-goto-sdcdb\SpecialChar ~
22366 Goto the SDCDB output buffer
22384 sdcdbsrc-mode\SpecialChar ~
22400 Toggles Sdcdbsrc mode (turns it off)
22415 sdcdb-finish-from-src\SpecialChar ~
22423 SDCDB finish command
22438 sdcdb-break\SpecialChar ~
22456 Set break for line with point
22471 sdcdbsrc-mode\SpecialChar ~
22487 Toggle Sdcdbsrc mode
22502 sdcdbsrc-srcmode\SpecialChar ~
22525 Here are a few guidelines that will help the compiler generate more efficient
22526 code, some of the tips are specific to this compiler others are generally
22527 good programming practice.
22530 Use the smallest data type to represent your data-value.
22531 If it is known in advance that the value is going to be less than 256 then
22532 use an 'unsigned char' instead of a 'short' or 'int'.
22533 Please note, that ANSI C requires both signed and unsigned chars to be
22534 promoted to 'signed int'
22535 \begin_inset LatexCommand \index{promotion to signed int}
22539 before doing any operation.
22541 \begin_inset LatexCommand \index{type promotion}
22546 \begin_inset LatexCommand \label{type promotion}
22550 can be omitted, if the result is the same.
22551 The effect of the promotion rules together with the sign-extension is often
22558 unsigned char uc = 0xfe;
22560 if (uc * uc < 0) /* this is true! */
22579 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
22589 (unsigned char) -12 / (signed char) -3 = ...
22592 No, the result is not 4:
22597 (int) (unsigned char) -12 / (int) (signed char) -3 =
22599 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
22601 (int) 0x00f4 / (int) 0xfffd =
22603 (int) 0x00f4 / (int) 0xfffd =
22605 (int) 244 / (int) -3 =
22607 (int) -81 = (int) 0xffaf;
22610 Don't complain, that gcc gives you a different result.
22611 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
22612 Therefore the results are different.
22615 \begin_inset Quotes sld
22619 \begin_inset Quotes srd
22625 If well-defined overflow characteristics are important and negative values
22626 are not, or if you want to steer clear of sign-extension problems when
22627 manipulating bits or bytes, use one of the corresponding unsigned types.
22628 (Beware when mixing signed and unsigned values in expressions, though.)
22630 Although character types (especially unsigned char) can be used as "tiny"
22631 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
22632 ble sign extension and increased code size.
22636 Use unsigned when it is known in advance that the value is not going to
22638 This helps especially if you are doing division or multiplication, bit-shifting
22639 or are using an array index.
22642 NEVER jump into a LOOP.
22645 Declare the variables to be local
22646 \begin_inset LatexCommand \index{local variables}
22650 whenever possible, especially loop control variables (induction).
22653 Since the compiler does not always do implicit integral promotion, the programme
22654 r should do an explicit cast when integral promotion is required.
22657 Reducing the size of division, multiplication & modulus operations can reduce
22658 code size substantially.
22659 Take the following code for example.
22665 foobar(unsigned int p1, unsigned char ch)
22673 unsigned char ch1 = p1 % ch ;
22684 For the modulus operation the variable ch will be promoted to unsigned int
22685 first then the modulus operation will be performed (this will lead to a
22686 call to support routine _moduint()), and the result will be casted to a
22688 If the code is changed to
22693 foobar(unsigned int p1, unsigned char ch)
22701 unsigned char ch1 = (unsigned char)p1 % ch ;
22712 It would substantially reduce the code generated (future versions of the
22713 compiler will be smart enough to detect such optimization opportunities).
22717 Have a look at the assembly listing to get a
22718 \begin_inset Quotes sld
22722 \begin_inset Quotes srd
22725 for the code generation.
22728 Porting code from or to other compilers
22731 check whether endianness of the compilers differs and adapt where needed.
22734 check the device specific header files for compiler specific syntax.
22735 Eventually include the file <compiler.h
22736 \begin_inset LatexCommand \index{compiler.h (include file)}
22740 > to allow using common header files.
22743 check whether the startup code contains the correct initialization (watchdog,
22747 check whether the sizes of short, int, long match.
22750 check if some 16 or 32 bit hardware registers require a specific addressing
22751 order (least significant or most significant byte first) and adapt if needed
22760 relate to time and not to lower/upper memory location here, so this is
22765 the same as endianness).
22768 check whether the keyword
22772 is used where needed.
22773 The compilers might differ in their optimization characteristics (as different
22774 versions of the same compiler might also use more clever optimizations
22775 this is good idea anyway).
22778 check that the compilers are not told to supress warnings.
22781 check and convert compiler specific extensions (interrupts, memory areas,
22785 check for differences in type promotion (especially check for math operations
22786 on char variables and for the use of the ~\SpecialChar ~
22787 operator on bit variables.
22789 \begin_inset LatexCommand \ref{type promotion}
22794 \begin_inset LatexCommand \ref{sec:Compatibility-with-previous}
22801 check the assembly code generated for interrupt routines (f.e.
22802 for calls to possibly non-reentrant library functions).
22805 check whether timing loops result in proper timing (or preferably consider
22806 a rewrite of the code with timer based delays instead).
22809 check for differences in printf parameters (some compilers push (va_arg
22810 \begin_inset LatexCommand \index{va\_arg}
22814 ) char variables as integers others as char).
22817 check the resulting memory layout.
22821 \begin_inset LatexCommand \index{Tools}
22825 included in the distribution
22829 \begin_inset Tabular
22830 <lyxtabular version="3" rows="12" columns="3">
22832 <column alignment="left" valignment="top" leftline="true" width="0pt">
22833 <column alignment="left" valignment="top" leftline="true" width="0pt">
22834 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22835 <row topline="true" bottomline="true">
22836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22852 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22861 <row topline="true">
22862 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22875 Simulator for various architectures
22878 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22887 <row topline="true">
22888 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22901 header file conversion
22904 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22909 sdcc/support/scripts
22913 <row topline="true">
22914 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22922 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22927 header file conversion
22930 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22935 sdcc/support/scripts
22939 <row topline="true">
22940 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22948 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22956 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22974 <row topline="true">
22975 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22983 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22991 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23009 <row topline="true">
23010 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23044 <row topline="true">
23045 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23053 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23061 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23079 <row topline="true">
23080 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23096 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23114 <row topline="true">
23115 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23131 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23149 <row topline="true">
23150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23158 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23184 <row topline="true" bottomline="true">
23185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23193 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23229 \begin_inset LatexCommand \index{Documentation}
23233 included in the distribution
23237 \begin_inset Tabular
23238 <lyxtabular version="3" rows="10" columns="2">
23240 <column alignment="left" valignment="top" leftline="true" width="0">
23241 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23242 <row topline="true" bottomline="true">
23243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23251 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23256 Where to get / filename
23260 <row topline="true">
23261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23266 SDCC Compiler User Guide
23269 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23274 You're reading it right now
23278 <row topline="true">
23279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23287 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23296 <row topline="true">
23297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23303 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
23308 \begin_inset LatexCommand \index{Assembler documentation}
23312 Assemblers and ASLINK
23313 \begin_inset LatexCommand \index{aslink}
23318 \begin_inset LatexCommand \index{Linker documentation}
23325 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23330 sdcc/as/doc/asxhtm.html
23334 <row topline="true">
23335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23340 SDCC regression test
23341 \begin_inset LatexCommand \index{Regression test}
23348 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23353 sdcc/doc/test_suite_spec.pdf
23357 <row topline="true">
23358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23366 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23375 <row topline="true">
23376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23381 Notes on debugging with SDCDB
23382 \begin_inset LatexCommand \index{SDCDB (debugger)}
23389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23394 sdcc/debugger/README
23398 <row topline="true">
23399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23404 Software simulator for microcontrollers
23407 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23434 <row topline="true">
23435 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23440 Temporary notes on the pic16
23441 \begin_inset LatexCommand \index{PIC16}
23448 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23453 sdcc/src/pic16/NOTES
23457 <row topline="true" bottomline="true">
23458 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23463 SDCC internal documentation (debugging file format)
23466 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23502 Related open source tools
23503 \begin_inset LatexCommand \label{sec:Related-open-source-tools}
23508 \begin_inset LatexCommand \index{Related tools}
23516 \begin_inset Tabular
23517 <lyxtabular version="3" rows="14" columns="3">
23519 <column alignment="left" valignment="top" leftline="true" width="0pt">
23520 <column alignment="block" valignment="top" leftline="true" width="30line%">
23521 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
23522 <row topline="true" bottomline="true">
23523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23548 <row topline="true">
23549 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23555 \begin_inset LatexCommand \index{gpsim (pic simulator)}
23562 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23576 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
23584 <row topline="true">
23585 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23591 \begin_inset LatexCommand \index{gputils (pic tools)}
23598 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23606 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23612 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
23620 <row topline="true">
23621 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23629 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23637 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23643 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
23651 <row topline="true">
23652 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23660 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23665 Tools for Silicon Laboratories JTAG debug adapter, partly based on SDCDB
23669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23675 \begin_inset LatexCommand \url{http://sourceforge.net/projects/ec2drv}
23683 <row topline="true">
23684 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23690 \begin_inset LatexCommand \index{indent (source formatting tool)}
23697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23702 Formats C source - Master of the white spaces
23705 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23711 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
23719 <row topline="true">
23720 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23726 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
23733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23738 Object file conversion, checksumming, ...
23741 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23747 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
23755 <row topline="true">
23756 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23762 \begin_inset LatexCommand \index{objdump (tool)}
23769 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23774 Object file conversion, ...
23777 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23782 Part of binutils (should be there anyway)
23786 <row topline="true">
23787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23795 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23800 8051 monitor (hex up-/download, single step, disassemble)
23803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23809 \begin_inset LatexCommand \url{http://sourceforge.net/projects/cmon51}
23817 <row topline="true">
23818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23824 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
23831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23836 Source code documentation system
23839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23845 \begin_inset LatexCommand \url{http://www.doxygen.org}
23853 <row topline="true">
23854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23862 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23867 IDE (has anyone tried integrating SDCC & SDCDB? Unix only)
23870 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23876 \begin_inset LatexCommand \url{http://www.kdevelop.org}
23884 <row topline="true">
23885 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23898 8051 monitor (hex up-/download, single step, disassemble)
23901 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23907 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/paulmon2.html}
23915 <row topline="true">
23916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23922 \begin_inset LatexCommand \index{splint (syntax checking tool)}
23929 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23934 Statically checks c sources (see
23935 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
23942 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23948 \begin_inset LatexCommand \url{http://www.splint.org}
23956 <row topline="true" bottomline="true">
23957 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23963 \begin_inset LatexCommand \index{ddd (debugger)}
23970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23975 Debugger, serves nicely as GUI to SDCDB
23976 \begin_inset LatexCommand \index{SDCDB (debugger)}
23983 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23989 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
24006 Related documentation / recommended reading
24010 \begin_inset Tabular
24011 <lyxtabular version="3" rows="8" columns="3">
24013 <column alignment="left" valignment="top" leftline="true" width="0pt">
24014 <column alignment="block" valignment="top" leftline="true" width="30line%">
24015 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
24016 <row topline="true" bottomline="true">
24017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24025 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24033 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24042 <row topline="true">
24043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24060 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24066 \begin_inset LatexCommand \index{C Reference card}
24073 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24079 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
24087 <row topline="true">
24088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24096 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24110 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
24118 <row topline="true">
24119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24133 \begin_inset Quotes sld
24137 \begin_inset Quotes srd
24143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24151 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
24159 <row topline="true">
24160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24168 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24174 \begin_inset Quotes sld
24177 Extensions for Embedded C
24178 \begin_inset Quotes srd
24184 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24192 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1021.pdf}
24200 <row topline="true">
24201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24208 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24213 Latest datasheet of the target CPU
24216 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24225 <row topline="true">
24226 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24238 Revision history of datasheet
24241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24250 <row topline="true" bottomline="true">
24251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
24266 Advanced Compiler Design and Implementation
24269 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
24274 bookstore (very dedicated, probably read other books first)
24290 Some questions answered, some pointers given - it might be time to in turn
24298 can you solve your project with the selected microcontroller? Would you
24299 find out early or rather late that your target is too small/slow/whatever?
24300 Can you switch to a slightly better device if it doesn't fit?
24303 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
24304 and/or another programming language be more adequate? Would an operating
24305 system on the target device help?
24308 if you solved the problem, will the marketing department be happy?
24311 if the marketing department is happy, will customers be happy?
24314 if you're the project manager, marketing department and maybe even the customer
24315 in one person, have you tried to see the project from the outside?
24318 is the project done if you think it is done? Or is just that other interface/pro
24319 tocol/feature/configuration/option missing? How about website, manual(s),
24320 internationali(z|s)ation, packaging, labels, 2nd source for components,
24321 electromagnetic compatability/interference, documentation for production,
24322 production test software, update mechanism, patent issues?
24325 is your project adequately positioned in that magic triangle: fame, fortune,
24329 Maybe not all answers to these questions are known and some answers may
24334 , nevertheless knowing these questions may help you to avoid burnout
24340 burnout is bad for electronic devices, programmers and motorcycle tyres
24344 Chances are you didn't want to hear some of them...
24348 \begin_inset LatexCommand \index{Support}
24355 SDCC has grown to be a large project.
24356 The compiler alone (without the preprocessor, assembler and linker) is
24357 well over 100,000 lines of code (blank stripped).
24358 The open source nature of this project is a key to its continued growth
24360 You gain the benefit and support of many active software developers and
24362 Is SDCC perfect? No, that's why we need your help.
24363 The developers take pride in fixing reported bugs.
24364 You can help by reporting the bugs and helping other SDCC users.
24365 There are lots of ways to contribute, and we encourage you to take part
24366 in making SDCC a great software package.
24370 The SDCC project is hosted on the SDCC sourceforge site at
24371 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
24376 You'll find the complete set of mailing lists
24377 \begin_inset LatexCommand \index{Mailing list(s)}
24381 , forums, bug reporting system, patch submission
24382 \begin_inset LatexCommand \index{Patch submission}
24387 \begin_inset LatexCommand \index{download}
24391 area and Subversion code repository
24392 \begin_inset LatexCommand \index{Subversion code repository}
24400 \begin_inset LatexCommand \index{Bug reporting}
24405 \begin_inset LatexCommand \index{Reporting bugs}
24412 The recommended way of reporting bugs is using the infrastructure of the
24414 You can follow the status of bug reports there and have an overview about
24418 Bug reports are automatically forwarded to the developer mailing list and
24419 will be fixed ASAP.
24420 When reporting a bug, it is very useful to include a small test program
24421 (the smaller the better) which reproduces the problem.
24422 If you can isolate the problem by looking at the generated assembly code,
24423 this can be very helpful.
24424 Compiling your program with the -
24435 \begin_inset LatexCommand \index{-\/-dumpall}
24439 option can sometimes be useful in locating optimization problems.
24440 When reporting a bug please maker sure you:
24443 Attach the code you are compiling with SDCC.
24447 Specify the exact command you use to run SDCC, or attach your Makefile.
24451 Specify the SDCC version (type "
24457 "), your platform, and operating system.
24461 Provide an exact copy of any error message or incorrect output.
24465 Put something meaningful in the subject of your message.
24468 Please attempt to include these 5 important parts, as applicable, in all
24469 requests for support or when reporting any problems or bugs with SDCC.
24470 Though this will make your message lengthy, it will greatly improve your
24471 chance that SDCC users and developers will be able to help you.
24472 Some SDCC developers are frustrated by bug reports without code provided
24473 that they can use to reproduce and ultimately fix the problem, so please
24474 be sure to provide sample code if you are reporting a bug!
24477 Please have a short check that you are using a recent version of SDCC and
24478 the bug is not yet known.
24479 This is the link for reporting bugs:
24480 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
24487 Requesting Features
24488 \begin_inset LatexCommand \label{sub:Requesting-Features}
24493 \begin_inset LatexCommand \index{Feature request}
24498 \begin_inset LatexCommand \index{Requesting features}
24505 Like bug reports feature requests are forwarded to the developer mailing
24507 This is the link for requesting features:
24508 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24518 Like bug reports contributed patches are forwarded to the developer mailing
24520 This is the link for submitting patches
24521 \begin_inset LatexCommand \index{Patch submission}
24526 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
24533 You need to specify some parameters to the
24537 command for the patches to be useful.
24538 If you modified more than one file a patch created f.e.
24543 \begin_inset Quotes sld
24546 diff -Naur unmodified_directory modified_directory >my_changes.patch
24547 \begin_inset Quotes srd
24553 will be fine, otherwise
24557 \begin_inset Quotes sld
24560 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
24561 \begin_inset Quotes srd
24574 These links should take you directly to the
24575 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
24585 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
24586 automated messages (mid 2003)
24590 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
24595 \begin_inset LatexCommand \index{Mailing list(s)}
24599 and forums are archived and searchable so if you are lucky someone already
24600 had a similar problem.
24601 While mails to the lists themselves are delivered promptly their web front
24602 end on sourceforge sometimes shows a severe time lag (up to several weeks),
24603 if you're seriously using SDCC please consider subscribing to the lists.
24609 You can follow the status of the Subversion version
24610 \begin_inset LatexCommand \index{version}
24614 of SDCC by watching the Changelog
24615 \begin_inset LatexCommand \index{Changelog}
24619 in the Subversion repository
24622 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
24629 Subversion Source Code Repository
24638 or the filenames of the snapshot versions of SDCC include date and its
24640 \begin_inset LatexCommand \index{Subversion}
24645 Subversion allows to download the source of recent or previous versions
24647 \begin_inset LatexCommand \url{http://sourceforge.net/svn/?group_id=599}
24651 (by number or by date).
24652 An on-line source code browser and detailled instructions are also available
24654 SDCC versions starting from 1999 up to now are available (currently the
24655 versions prior to the conversion from cvs to Subversion (April 2006) are
24656 either by accessible by Subversion or by cvs).
24660 \begin_inset LatexCommand \index{Release policy}
24667 Historically there often were long delays between official releases and
24668 the sourceforge download area tends to get not updated at all.
24669 Excuses in the past might have referred to problems with live range analysis,
24670 but as this was fixed a while ago, the current problem is that another
24671 excuse has to be found.
24672 Kidding aside, we have to get better there! On the other hand there are
24673 daily snapshots available at
24674 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
24678 , and you can always build the very last version (hopefully with many bugs
24679 fixed, and features added) from the source code available at
24680 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
24688 \begin_inset LatexCommand \index{Examples}
24695 You'll find some small examples in the directory
24697 sdcc/device/examples/.
24700 More examples and libraries are available at
24702 The SDCC Open Knowledge Resource
24703 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
24710 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
24717 I did insert a reference to Paul's web site here although it seems rather
24718 dedicated to a specific 8032 board (I think it's okay because it f.e.
24719 shows LCD/Harddisc interface and has a free 8051 monitor.
24720 Independent 8032 board vendors face hard competition of heavily subsidized
24721 development boards anyway).
24724 Maybe we should include some links to real world applications.
24725 Preferably pointer to pointers (one for each architecture) so this stays
24730 \begin_inset LatexCommand \index{Quality control}
24737 The compiler is passed through nightly compile and build checks.
24743 \begin_inset LatexCommand \index{Regression test}
24747 check that SDCC itself compiles flawlessly on several platforms and checks
24748 the quality of the code generated by SDCC by running the code through simulator
24750 There is a separate document
24753 \begin_inset LatexCommand \index{Test suite}
24762 You'll find the test code in the directory
24764 sdcc/support/regression
24767 You can run these tests manually by running
24771 in this directory (or f.e.
24776 \begin_inset Quotes sld
24780 \begin_inset Quotes srd
24786 if you don't want to run the complete tests).
24787 The test code might also be interesting if you want to look for examples
24788 \begin_inset LatexCommand \index{Examples}
24792 checking corner cases of SDCC or if you plan to submit patches
24793 \begin_inset LatexCommand \index{Patch submission}
24800 The pic port uses a different set of regression tests, you'll find them
24803 sdcc/src/regression
24808 Use of SDCC in Education
24821 the phrase "use in education" might evoke the association "
24825 fit for use in education".
24826 This connotation is not intended but nevertheless risked as the licensing
24827 of SDCC makes it difficult to offer educational discounts
24831 If your rationales are to:
24834 give students a chance to understand the
24838 steps of code generation
24841 have a curriculum that can be extended for years.
24842 Then you could use an fpga board as target and your curriculum will seamlessly
24843 extend from logic synthesis (
24844 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
24849 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
24853 ), over assembly programming, to C to FPGA compilers (
24854 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
24861 be able to insert excursions about skills like using a revision control
24862 system, submitting/applying patches, using a type-setting (as opposed to
24863 word-processing) engine LyX/LaTeX, using
24864 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
24869 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
24873 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
24874 Source Software, CPU simulation, compiler regression tests
24875 \begin_inset LatexCommand \index{Regression test}
24882 And if there should be a shortage of ideas then you can always point students
24883 to the ever-growing feature request list
24884 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
24891 not tie students to a specific host platform and instead allow them to use
24896 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
24898 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
24905 not encourage students to use illegal copies of educational software
24908 be immune to licensing/availability/price changes of the chosen tool chain
24911 be able to change to a new target platform without having to adopt a new
24915 have complete control over and insight into the tool chain
24918 make your students aware about the pros and cons of open source software
24922 give back to the public as you are probably at least partially publically
24926 give students a chance to publically prove their skills and to possibly
24927 see a world wide impact
24930 then SDCC is probably among the first choices.
24931 Well, probably SDCC might be the only choice.
24934 SDCC Technical Data
24938 \begin_inset LatexCommand \index{Optimizations}
24945 SDCC performs a host of standard optimizations in addition to some MCU specific
24950 Sub-expression Elimination
24951 \begin_inset LatexCommand \index{Subexpression elimination}
24958 The compiler does local and
24984 will be translated to
24996 Some subexpressions are not as obvious as the above example, e.g.:
25006 In this case the address arithmetic a->b[i] will be computed only once;
25007 the equivalent code in C would be.
25019 The compiler will try to keep these temporary variables in registers.
25022 Dead-Code Elimination
25023 \begin_inset LatexCommand \index{Dead-code elimination}
25044 i = 1; \SpecialChar ~
25053 global = 1;\SpecialChar ~
25066 global = 3;\SpecialChar ~
25091 \begin_inset LatexCommand \index{Copy propagation}
25147 Note: the dead stores created by this copy propagation will be eliminated
25148 by dead-code elimination.
25152 \begin_inset LatexCommand \index{Loop optimization}
25157 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
25164 Two types of loop optimizations are done by SDCC
25172 of loop induction variables.
25173 In addition to the strength reduction the optimizer marks the induction
25174 variables and the register allocator tries to keep the induction variables
25175 in registers for the duration of the loop.
25176 Because of this preference of the register allocator
25177 \begin_inset LatexCommand \index{Register allocation}
25181 , loop induction optimization causes an increase in register pressure, which
25182 may cause unwanted spilling of other temporary variables into the stack
25183 \begin_inset LatexCommand \index{stack}
25188 The compiler will generate a warning message when it is forced to allocate
25189 extra space either on the stack or data space.
25190 If this extra space allocation is undesirable then induction optimization
25191 can be eliminated either for the entire source file (with -
25201 -noinduction option) or for a given function only using #pragma\SpecialChar ~
25203 \begin_inset LatexCommand \index{\#pragma noinduction}
25216 for (i = 0 ; i < 100 ; i ++)
25232 for (i = 0; i < 100; i++)
25241 As mentioned previously some loop invariants are not as apparent, all static
25242 address computations are also moved out of the loop.
25247 \begin_inset LatexCommand \index{Strength reduction}
25251 , this optimization substitutes an expression by a cheaper expression:
25256 for (i=0;i < 100; i++)
25274 for (i=0;i< 100;i++) {
25280 ar[itemp1] = itemp2;
25297 The more expensive multiplication
25298 \begin_inset LatexCommand \index{Multiplication}
25302 is changed to a less expensive addition.
25306 \begin_inset LatexCommand \index{Loop reversing}
25313 This optimization is done to reduce the overhead of checking loop boundaries
25314 for every iteration.
25315 Some simple loops can be reversed and implemented using a
25316 \begin_inset Quotes eld
25319 decrement and jump if not zero
25320 \begin_inset Quotes erd
25324 SDCC checks for the following criterion to determine if a loop is reversible
25325 (note: more sophisticated compilers use data-dependency analysis to make
25326 this determination, SDCC uses a more simple minded analysis).
25329 The 'for' loop is of the form
25335 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
25345 The <for body> does not contain
25346 \begin_inset Quotes eld
25350 \begin_inset Quotes erd
25354 \begin_inset Quotes erd
25360 All goto's are contained within the loop.
25363 No function calls within the loop.
25366 The loop control variable <sym> is not assigned any value within the loop
25369 The loop control variable does NOT participate in any arithmetic operation
25373 There are NO switch statements in the loop.
25376 Algebraic Simplifications
25379 SDCC does numerous algebraic simplifications, the following is a small sub-set
25380 of these optimizations.
25385 i = j + 0;\SpecialChar ~
25389 /* changed to: */\SpecialChar ~
25395 i /= 2;\SpecialChar ~
25402 /* changed to: */\SpecialChar ~
25408 i = j - j;\SpecialChar ~
25412 /* changed to: */\SpecialChar ~
25418 i = j / 1;\SpecialChar ~
25422 /* changed to: */\SpecialChar ~
25429 Note the subexpressions
25430 \begin_inset LatexCommand \index{Subexpression}
25434 given above are generally introduced by macro expansions or as a result
25435 of copy/constant propagation.
25438 'switch' Statements
25439 \begin_inset LatexCommand \label{sub:'switch'-Statements}
25444 \begin_inset LatexCommand \index{switch statement}
25451 SDCC can optimize switch statements to jump tables
25452 \begin_inset LatexCommand \index{jump tables}
25457 It makes the decision based on an estimate of the generated code size.
25458 SDCC is quite liberal in the requirements for jump table generation:
25461 The labels need not be in order, and the starting number need not be one
25462 or zero, the case labels are in numerical sequence or not too many case
25463 labels are missing.
25469 switch(i) {\SpecialChar ~
25500 case 4: ...\SpecialChar ~
25532 case 5: ...\SpecialChar ~
25564 case 3: ...\SpecialChar ~
25595 case 6: ...\SpecialChar ~
25627 case 7: ...\SpecialChar ~
25659 case 8: ...\SpecialChar ~
25691 case 9: ...\SpecialChar ~
25723 case 10: ...\SpecialChar ~
25754 case 11: ...\SpecialChar ~
25821 Both the above switch statements will be implemented using a jump-table.
25822 The example to the right side is slightly more efficient as the check for
25823 the lower boundary of the jump-table is not needed.
25827 The number of case labels is not larger than supported by the target architectur
25831 If the case labels are not in numerical sequence ('gaps' between cases)
25832 SDCC checks whether a jump table with additionally inserted dummy cases
25833 is still attractive.
25837 If the starting number is not zero and a check for the lower boundary of
25838 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
25843 Switch statements which have large gaps in the numeric sequence or those
25844 that have too many case labels can be split into more than one switch statement
25845 for efficient code generation, e.g.:
25925 If the above switch statement is broken down into two switch statements
26015 then both the switch statements will be implemented using jump-tables whereas
26016 the unmodified switch statement will not be.
26019 There might be reasons which SDCC cannot know about to either favour or
26020 not favour jump tables.
26021 If the target system has to be as quick for the last switch case as for
26022 the first (pro jump table), or if the switch argument is known to be zero
26023 in the majority of the cases (contra jump table).
26026 The pragma nojtbound
26027 \begin_inset LatexCommand \index{\#pragma nojtbound}
26031 can be used to turn off checking the
26044 It has no effect if a default label is supplied.
26045 Use of this pragma is dangerous: if the switch
26046 \begin_inset LatexCommand \index{switch statement}
26050 argument is not matched by a case statement the processor will happily
26054 Bit-shifting Operations
26055 \begin_inset LatexCommand \index{Bit shifting}
26062 Bit shifting is one of the most frequently used operation in embedded programmin
26064 SDCC tries to implement bit-shift operations in the most efficient way
26080 generates the following code:
26097 In general SDCC will never setup a loop if the shift count is known.
26140 \begin_inset LatexCommand \index{Bit rotation}
26147 A special case of the bit-shift operation is bit rotation
26148 \begin_inset LatexCommand \index{rotating bits}
26152 , SDCC recognizes the following expression to be a left bit-rotation:
26162 char i;\SpecialChar ~
26173 /* unsigned is needed for rotation */
26178 i = ((i << 1) | (i >> 7));
26187 will generate the following code:
26206 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
26207 ns of this case will also be recognized as bit-rotation, i.e.:
26212 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
26215 Nibble and Byte Swapping
26218 Other special cases of the bit-shift operations are nibble or byte swapping
26219 \begin_inset LatexCommand \index{swapping nibbles/bytes}
26223 , SDCC recognizes the following expressions:
26246 i = ((i << 4) | (i >> 4));
26252 j = ((j << 8) | (j >> 8));
26255 and generates a swap instruction for the nibble swapping
26256 \begin_inset LatexCommand \index{Nibble swapping}
26260 or move instructions for the byte swapping
26261 \begin_inset LatexCommand \index{Byte swapping}
26267 \begin_inset Quotes sld
26271 \begin_inset Quotes srd
26274 example can be used to convert from little to big-endian or vice versa.
26275 If you want to change the endianness of a
26279 integer you have to cast to
26286 Note that SDCC stores numbers in little-endian
26292 Usually 8-bit processors don't care much about endianness.
26293 This is not the case for the standard 8051 which only has an instruction
26299 \begin_inset LatexCommand \index{DPTR}
26307 so little-endian is the more efficient byte order.
26311 \begin_inset LatexCommand \index{little-endian}
26316 \begin_inset LatexCommand \index{Endianness}
26321 lowest order first).
26325 \begin_inset LatexCommand \index{Highest Order Bit}
26330 \begin_inset LatexCommand \index{Any Order Bit}
26337 It is frequently required to obtain the highest order bit of an integral
26338 type (long, int, short or char types).
26339 Also obtaining any other order bit is not uncommon.
26340 SDCC recognizes the following expressions to yield the highest order bit
26341 and generates optimized code for it, e.g.:
26354 unsigned char hob1, aob1;
26358 bit hob2, hob3, aob2, aob3;
26367 hob1 = (gint >> 15) & 1;
26371 hob2 = (gint >> 15) & 1;
26375 hob3 = gint & 0x8000;
26379 aob1 = (gint >> 9) & 1;
26383 aob2 = (gint >> 8) & 1;
26387 aob3 = gint & 0x0800;
26397 will generate the following code:
26430 000A E5*01\SpecialChar ~
26457 000C 23\SpecialChar ~
26488 000D 54 01\SpecialChar ~
26515 000F F5*02\SpecialChar ~
26570 0011 E5*01\SpecialChar ~
26597 0013 33\SpecialChar ~
26627 0014 92*00\SpecialChar ~
26682 0016 E5*01\SpecialChar ~
26709 0018 33\SpecialChar ~
26739 0019 92*01\SpecialChar ~
26794 001B E5*01\SpecialChar ~
26821 001D 03\SpecialChar ~
26852 001E 54 01\SpecialChar ~
26879 0020 F5*03\SpecialChar ~
26934 0022 E5*01\SpecialChar ~
26961 0024 13\SpecialChar ~
26991 0025 92*02\SpecialChar ~
27046 0027 E5*01\SpecialChar ~
27073 0029 A2 E3\SpecialChar ~
27100 002B 92*03\SpecialChar ~
27128 Other variations of these cases however will
27133 They are standard C expressions, so I heartily recommend these be the only
27134 way to get the highest order bit, (it is portable).
27135 Of course it will be recognized even if it is embedded in other expressions,
27141 xyz = gint + ((gint >> 15) & 1);
27144 will still be recognized.
27148 \begin_inset LatexCommand \index{Higher Order Byte}
27152 / Higher Order Word
27153 \begin_inset LatexCommand \index{Higher Order Word}
27160 It is also frequently required to obtain a higher order byte or word of
27161 a larger integral type (long, int or short types).
27162 SDCC recognizes the following expressions to yield the higher order byte
27163 or word and generates optimized code for it, e.g.:
27170 unsigned long int glong;
27178 unsigned char hob1, hob2;
27182 unsigned int how1, how2;
27191 hob1 = (gint >> 8) & 0xFF;
27195 hob2 = glong >> 24;
27199 how1 = (glong >> 16) & 0xFFFF;
27213 will generate the following code:
27246 0037 85*01*06\SpecialChar ~
27268 _foo_hob1_1_1,(_gint + 1)
27298 003A 85*05*07\SpecialChar ~
27320 _foo_hob2_1_1,(_glong + 3)
27350 003D 85*04*08\SpecialChar ~
27372 _foo_how1_1_1,(_glong + 2)
27374 0040 85*05*09\SpecialChar ~
27396 (_foo_how1_1_1 + 1),(_glong + 3)
27398 0043 85*03*0A\SpecialChar ~
27420 _foo_how2_1_1,(_glong + 1)
27422 0046 85*04*0B\SpecialChar ~
27444 (_foo_how2_1_1 + 1),(_glong + 2)
27447 Again, variations of these cases may
27452 They are standard C expressions, so I heartily recommend these be the only
27453 way to get the higher order byte/word, (it is portable).
27454 Of course it will be recognized even if it is embedded in other expressions,
27460 xyz = gint + ((gint >> 8) & 0xFF);
27463 will still be recognized.
27467 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
27472 \begin_inset LatexCommand \index{Peephole optimizer}
27479 The compiler uses a rule based, pattern matching and re-writing mechanism
27480 for peep-hole optimization.
27485 a peep-hole optimizer by Christopher W.
27486 Fraser (cwfraser\SpecialChar ~
27489 A default set of rules are compiled into the compiler, additional rules
27490 may be added with the
27503 \begin_inset LatexCommand \index{-\/-peep-file}
27510 The rule language is best illustrated with examples.
27534 The above rule will change the following assembly
27535 \begin_inset LatexCommand \index{Assembler routines}
27557 Note: All occurrences of a
27561 (pattern variable) must denote the same string.
27562 With the above rule, the assembly sequence:
27572 will remain unmodified.
27576 Other special case optimizations may be added by the user (via
27592 some variants of the 8051 MCU
27593 \begin_inset LatexCommand \index{MCS51 variants}
27606 The following two rules will change all
27625 replace { lcall %1 } by { acall %1 }
27627 replace { ljmp %1 } by { ajmp %1 }
27632 inline-assembler code
27634 is also passed through the peep hole optimizer, thus the peephole optimizer
27635 can also be used as an assembly level macro expander.
27636 The rules themselves are MCU dependent whereas the rule language infra-structur
27637 e is MCU independent.
27638 Peephole optimization rules for other MCU can be easily programmed using
27643 The syntax for a rule is as follows:
27648 rule := replace [ restart ] '{' <assembly sequence> '
27686 <assembly sequence> '
27704 '}' [if <functionName> ] '
27709 <assembly sequence> := assembly instruction (each instruction including
27710 labels must be on a separate line).
27714 The optimizer will apply to the rules one by one from the top in the sequence
27715 of their appearance, it will terminate when all rules are exhausted.
27716 If the 'restart' option is specified, then the optimizer will start matching
27717 the rules again from the top, this option for a rule is expensive (performance)
27718 , it is intended to be used in situations where a transformation will trigger
27719 the same rule again.
27720 An example of this (not a good one, it has side effects) is the following
27743 Note that the replace pattern cannot be a blank, but can be a comment line.
27744 Without the 'restart' option only the innermost 'pop' 'push' pair would
27745 be eliminated, i.e.:
27775 the restart option the rule will be applied again to the resulting code
27776 and then all the pop-push pairs will be eliminated to yield:
27786 A conditional function can be attached to a rule.
27787 Attaching rules are somewhat more involved, let me illustrate this with
27814 The optimizer does a look-up of a function name table defined in function
27819 in the source file SDCCpeeph.c, with the name
27824 If it finds a corresponding entry the function is called.
27825 Note there can be no parameters specified for these functions, in this
27830 is crucial, since the function
27834 expects to find the label in that particular variable (the hash table containin
27835 g the variable bindings is passed as a parameter).
27836 If you want to code more such functions, take a close look at the function
27837 labelInRange and the calling mechanism in source file SDCCpeeph.c.
27838 Currently implemented are
27840 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
27841 24bitMode, portIsDS390, 24bitModeAndPortDS390
27850 I know this whole thing is a little kludgey, but maybe some day we will
27851 have some better means.
27852 If you are looking at this file, you will see the default rules that are
27853 compiled into the compiler, you can add your own rules in the default set
27854 there if you get tired of specifying the -
27868 \begin_inset LatexCommand \index{ANSI-compliance}
27873 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
27880 The latest publically available version of the standard
27882 ISO/IEC 9899 - Programming languages - C
27884 should be available at:
27885 \begin_inset LatexCommand \url{http://www.open-std.org/jtc1/sc22/wg14/www/standards.html#9899}
27894 Deviations from the compliance:
27897 functions are not reentrant
27898 \begin_inset LatexCommand \index{reentrant}
27902 unless explicitly declared as such or the
27915 \begin_inset LatexCommand \index{-\/-stack-auto}
27921 command line option is specified.
27924 structures and unions cannot be assigned values directly, cannot be passed
27925 as function parameters or assigned to each other and cannot be a return
27926 value from a function, e.g.:
27952 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
27963 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
27985 return rets;/* is invalid in SDCC although allowed in ANSI */
27991 initialization of structure arrays must be fully braced.
27997 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
27999 struct s { char x } a[] = {{1}, {2}}; /* OK */
28004 \begin_inset LatexCommand \index{long long (not supported)}
28009 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
28017 \begin_inset LatexCommand \index{double (not supported)}
28021 ' precision floating point
28022 \begin_inset LatexCommand \index{Floating point support}
28030 \begin_inset LatexCommand \index{K\&R style}
28034 function declarations are NOT allowed.
28040 foo(i,j) /* this old style of function declarations */
28042 int i,j; /* are valid in ANSI but not valid in SDCC */
28057 Most enhancements in C99 are not supported, f.e.:
28066 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
28073 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
28077 Certain words that are valid identifiers in the standard may be reserved
28078 words in SDCC unless the
28091 \begin_inset LatexCommand \index{-\/-std-c89}
28106 \begin_inset LatexCommand \index{-\/-std-c99}
28112 command line options are used.
28113 These may include (depending on the selected processor): 'at', 'banked',
28114 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
28115 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
28116 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
28118 Compliant equivalents of these keywords are always available in a form
28119 that begin with two underscores
28120 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
28125 '__data' instead of 'data'.
28128 Cyclomatic Complexity
28129 \begin_inset LatexCommand \index{Cyclomatic complexity}
28136 Cyclomatic complexity of a function is defined as the number of independent
28137 paths the program can take during execution of the function.
28138 This is an important number since it defines the number test cases you
28139 have to generate to validate the function.
28140 The accepted industry standard for complexity number is 10, if the cyclomatic
28141 complexity reported by SDCC exceeds 10 you should think about simplification
28142 of the function logic.
28143 Note that the complexity level is not related to the number of lines of
28144 code in a function.
28145 Large functions can have low complexity, and small functions can have large
28151 SDCC uses the following formula to compute the complexity:
28156 complexity = (number of edges in control flow graph) - (number of nodes
28157 in control flow graph) + 2;
28161 Having said that the industry standard is 10, you should be aware that in
28162 some cases it be may unavoidable to have a complexity level of less than
28164 For example if you have switch statement with more than 10 case labels,
28165 each case label adds one to the complexity level.
28166 The complexity level is by no means an absolute measure of the algorithmic
28167 complexity of the function, it does however provide a good starting point
28168 for which functions you might look at for further optimization.
28171 Retargetting for other Processors
28174 The issues for retargetting the compiler are far too numerous to be covered
28176 What follows is a brief description of each of the seven phases of the
28177 compiler and its MCU dependency.
28180 Parsing the source and building the annotated parse tree.
28181 This phase is largely MCU independent (except for the language extensions).
28182 Syntax & semantic checks are also done in this phase, along with some initial
28183 optimizations like back patching labels and the pattern matching optimizations
28184 like bit-rotation etc.
28187 The second phase involves generating an intermediate code which can be easy
28188 manipulated during the later phases.
28189 This phase is entirely MCU independent.
28190 The intermediate code generation assumes the target machine has unlimited
28191 number of registers, and designates them with the name iTemp.
28192 The compiler can be made to dump a human readable form of the code generated
28206 This phase does the bulk of the standard optimizations and is also MCU independe
28208 This phase can be broken down into several sub-phases:
28212 Break down intermediate code (iCode) into basic blocks.
28214 Do control flow & data flow analysis on the basic blocks.
28216 Do local common subexpression elimination, then global subexpression elimination
28218 Dead code elimination
28222 If loop optimizations caused any changes then do 'global subexpression eliminati
28223 on' and 'dead code elimination' again.
28226 This phase determines the live-ranges; by live range I mean those iTemp
28227 variables defined by the compiler that still survive after all the optimization
28229 Live range analysis
28230 \begin_inset LatexCommand \index{Live range analysis}
28234 is essential for register allocation, since these computation determines
28235 which of these iTemps will be assigned to registers, and for how long.
28238 Phase five is register allocation.
28239 There are two parts to this process.
28243 The first part I call 'register packing' (for lack of a better term).
28244 In this case several MCU specific expression folding is done to reduce
28249 The second part is more MCU independent and deals with allocating registers
28250 to the remaining live ranges.
28251 A lot of MCU specific code does creep into this phase because of the limited
28252 number of index registers available in the 8051.
28255 The Code generation phase is (unhappily), entirely MCU dependent and very
28256 little (if any at all) of this code can be reused for other MCU.
28257 However the scheme for allocating a homogenized assembler operand for each
28258 iCode operand may be reused.
28261 As mentioned in the optimization section the peep-hole optimizer is rule
28262 based system, which can reprogrammed for other MCUs.
28266 \begin_inset LatexCommand \index{Compiler internals}
28273 The anatomy of the compiler
28274 \begin_inset LatexCommand \label{sub:The-anatomy-of}
28283 This is an excerpt from an article published in Circuit Cellar Magazine
28289 It's a little outdated (the compiler is much more efficient now and user/develo
28290 per friendly), but pretty well exposes the guts of it all.
28296 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
28297 It is fairly easy to retarget for other 8-bit MCU.
28298 Here we take a look at some of the internals of the compiler.
28303 \begin_inset LatexCommand \index{Parsing}
28310 Parsing the input source file and creating an AST (Annotated Syntax Tree
28311 \begin_inset LatexCommand \index{Annotated syntax tree}
28316 This phase also involves propagating types (annotating each node of the
28317 parse tree with type information) and semantic analysis.
28318 There are some MCU specific parsing rules.
28319 For example the storage classes, the extended storage classes are MCU specific
28320 while there may be a xdata storage class for 8051 there is no such storage
28321 class for z80 or Atmel AVR.
28322 SDCC allows MCU specific storage class extensions, i.e.
28323 xdata will be treated as a storage class specifier when parsing 8051 C
28324 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
28329 \begin_inset LatexCommand \index{iCode}
28336 Intermediate code generation.
28337 In this phase the AST is broken down into three-operand form (iCode).
28338 These three operand forms are represented as doubly linked lists.
28339 ICode is the term given to the intermediate form generated by the compiler.
28340 ICode example section shows some examples of iCode generated for some simple
28341 C source functions.
28345 \begin_inset LatexCommand \index{Optimizations}
28352 Bulk of the target independent optimizations is performed in this phase.
28353 The optimizations include constant propagation, common sub-expression eliminati
28354 on, loop invariant code movement, strength reduction of loop induction variables
28355 and dead-code elimination.
28358 Live range analysis
28359 \begin_inset LatexCommand \index{Live range analysis}
28366 During intermediate code generation phase, the compiler assumes the target
28367 machine has infinite number of registers and generates a lot of temporary
28369 The live range computation determines the lifetime of each of these compiler-ge
28370 nerated temporaries.
28371 A picture speaks a thousand words.
28372 ICode example sections show the live range annotations for each of the
28374 It is important to note here, each iCode is assigned a number in the order
28375 of its execution in the function.
28376 The live ranges are computed in terms of these numbers.
28377 The from number is the number of the iCode which first defines the operand
28378 and the to number signifies the iCode which uses this operand last.
28381 Register Allocation
28382 \begin_inset LatexCommand \index{Register allocation}
28389 The register allocation determines the type and number of registers needed
28391 In most MCUs only a few registers can be used for indirect addressing.
28392 In case of 8051 for example the registers R0 & R1 can be used to indirectly
28393 address the internal ram and DPTR to indirectly address the external ram.
28394 The compiler will try to allocate the appropriate register to pointer variables
28396 ICode example section shows the operands annotated with the registers assigned
28398 The compiler will try to keep operands in registers as much as possible;
28399 there are several schemes the compiler uses to do achieve this.
28400 When the compiler runs out of registers the compiler will check to see
28401 if there are any live operands which is not used or defined in the current
28402 basic block being processed, if there are any found then it will push that
28403 operand and use the registers in this block, the operand will then be popped
28404 at the end of the basic block.
28408 There are other MCU specific considerations in this phase.
28409 Some MCUs have an accumulator; very short-lived operands could be assigned
28410 to the accumulator instead of a general-purpose register.
28416 Figure II gives a table of iCode operations supported by the compiler.
28417 The code generation involves translating these operations into corresponding
28418 assembly code for the processor.
28419 This sounds overly simple but that is the essence of code generation.
28420 Some of the iCode operations are generated on a MCU specific manner for
28421 example, the z80 port does not use registers to pass parameters so the
28422 SEND and RECV iCode operations will not be generated, and it also does
28423 not support JUMPTABLES.
28430 <Where is Figure II?>
28433 In the original article Figure II was announced to be downloadable on
28438 Unfortunately it never seemed to have shown up there, so: where is Figure
28443 \begin_inset LatexCommand \index{iCode}
28450 This section shows some details of iCode.
28451 The example C code does not do anything useful; it is used as an example
28452 to illustrate the intermediate code generated by the compiler.
28464 /* This function does nothing useful.
28471 for the purpose of explaining iCode */
28474 short function (data int *x)
28482 short i=10; \SpecialChar ~
28484 /* dead initialization eliminated */
28489 short sum=10; /* dead initialization eliminated */
28502 while (*x) *x++ = *p++;
28516 /* compiler detects i,j to be induction variables */
28520 for (i = 0, j = 10 ; i < 10 ; i++, j
28546 mul += i * 3; \SpecialChar ~
28548 /* this multiplication remains */
28554 gint += j * 3;\SpecialChar ~
28556 /* this multiplication changed to addition */
28570 In addition to the operands each iCode contains information about the filename
28571 and line it corresponds to in the source file.
28572 The first field in the listing should be interpreted as follows:
28577 Filename(linenumber: iCode Execution sequence number : ICode hash table
28578 key : loop depth of the iCode).
28583 Then follows the human readable form of the ICode operation.
28584 Each operand of this triplet form can be of three basic types a) compiler
28585 generated temporary b) user defined variable c) a constant value.
28586 Note that local variables and parameters are replaced by compiler generated
28589 \begin_inset LatexCommand \index{Live range analysis}
28593 are computed only for temporaries (i.e.
28594 live ranges are not computed for global variables).
28596 \begin_inset LatexCommand \index{Register allocation}
28600 are allocated for temporaries only.
28601 Operands are formatted in the following manner:
28606 Operand Name [lr live-from : live-to ] { type information } [ registers
28612 As mentioned earlier the live ranges are computed in terms of the execution
28613 sequence number of the iCodes, for example
28615 the iTemp0 is live from (i.e.
28616 first defined in iCode with execution sequence number 3, and is last used
28617 in the iCode with sequence number 5).
28618 For induction variables such as iTemp21 the live range computation extends
28619 the lifetime from the start to the end of the loop.
28621 The register allocator used the live range information to allocate registers,
28622 the same registers may be used for different temporaries if their live
28623 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
28624 iTemp17 since their live ranges do not overlap.
28625 In addition the allocator also takes into consideration the type and usage
28626 of a temporary, for example itemp6 is a pointer to near space and is used
28627 as to fetch data from (i.e.
28628 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
28629 Some short lived temporaries are allocated to special registers which have
28630 meaning to the code generator e.g.
28631 iTemp13 is allocated to a pseudo register CC which tells the back end that
28632 the temporary is used only for a conditional jump the code generation makes
28633 use of this information to optimize a compare and jump ICode.
28635 There are several loop optimizations
28636 \begin_inset LatexCommand \index{Loop optimization}
28640 performed by the compiler.
28641 It can detect induction variables iTemp21(i) and iTemp23(j).
28642 Also note the compiler does selective strength reduction
28643 \begin_inset LatexCommand \index{Strength reduction}
28648 the multiplication of an induction variable in line 18 (gint = j * 3) is
28649 changed to addition, a new temporary iTemp17 is allocated and assigned
28650 a initial value, a constant 3 is then added for each iteration of the loop.
28651 The compiler does not change the multiplication
28652 \begin_inset LatexCommand \index{Multiplication}
28656 in line 17 however since the processor does support an 8 * 8 bit multiplication.
28658 Note the dead code elimination
28659 \begin_inset LatexCommand \index{Dead-code elimination}
28663 optimization eliminated the dead assignments in line 7 & 8 to I and sum
28671 Sample.c (5:1:0:0) _entry($9) :
28676 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
28681 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
28686 Sample.c(11:4:53:0) preHeaderLbl0($11) :
28691 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
28697 Sample.c(11:6:5:1) _whilecontinue_0($1) :
28702 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
28708 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
28713 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
28719 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
28725 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
28731 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
28737 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
28738 * int}[r0] + 0x2 {short}
28743 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
28748 Sample.c(11:17:21:0)_whilebreak_0($3) :
28753 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
28758 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
28763 Sample.c(15:20:54:0)preHeaderLbl1($13) :
28768 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
28773 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
28778 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
28783 Sample.c(15:24:26:1)_forcond_0($4) :
28788 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
28794 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28799 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
28800 + ITemp21 [lr21:38]{short}[r4]
28805 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
28811 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
28812 + iTemp15 [lr29:30]{short}[r1]
28817 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
28823 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
28829 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
28835 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
28841 Sample.c(19:38:47:1) goto _forcond_0($4)
28846 Sample.c(19:39:48:0)_forbreak_0($7) :
28851 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
28852 + ITemp11 [lr19:40]{short}[r3]
28857 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
28862 Sample.c(20:42:51:0)_return($8) :
28867 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
28873 Finally the code generated for this function:
28914 ; ----------------------------------------------
28919 ; function function
28924 ; ----------------------------------------------
28934 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
28946 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
28958 ;_whilecontinue_0($1) :
28968 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
28973 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
29032 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
29051 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
29098 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
29138 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
29164 ; iTemp6 [lr5:16]{_near * int}[r0] =
29169 ; iTemp6 [lr5:16]{_near * int}[r0] +
29186 ; goto _whilecontinue_0($1)
29198 ; _whilebreak_0($3) :
29208 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
29220 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
29232 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
29244 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
29263 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
29292 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
29297 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
29342 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
29347 ; iTemp21 [lr21:38]{short}[r4]
29373 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
29406 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
29411 ; iTemp15 [lr29:30]{short}[r1]
29430 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
29477 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
29524 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
29536 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
29550 cjne r5,#0xff,00104$
29562 ; goto _forcond_0($4)
29574 ; _forbreak_0($7) :
29584 ; ret iTemp24 [lr40:41]{short}
29627 A few words about basic block successors, predecessors and dominators
29630 Successors are basic blocks
29631 \begin_inset LatexCommand \index{Basic blocks}
29635 that might execute after this basic block.
29637 Predecessors are basic blocks that might execute before reaching this basic
29640 Dominators are basic blocks that WILL execute before reaching this basic
29674 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
29677 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
29680 c) domVect of [BB4] = BB1 ...
29681 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
29689 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
29699 Thanks to all the other volunteer developers who have helped with coding,
29700 testing, web-page creation, distribution sets, etc.
29701 You know who you are :-)
29708 This document was initially written by Sandeep Dutta
29711 All product names mentioned herein may be trademarks
29712 \begin_inset LatexCommand \index{Trademarks}
29716 of their respective companies.
29723 To avoid confusion, the installation and building options for SDCC itself
29724 (chapter 2) are not part of the index.
29728 \begin_inset LatexCommand \printindex{}