1 #LyX 1.3 created this file. For more info see http://www.lyx.org/
5 \pdfoptionpdfminorversion=3
7 pdftitle={SDCC Compiler User Guide},
8 pdfauthor={SDCC development team},
9 pdfsubject={installation, user manual},
10 pdfkeywords={8032 8051 ansi c compiler CPU DS390
11 embedded GPL HC08 manual mcs51 microcontroller PIC Z80},
13 linkcolor=blue] {hyperref}
17 \emergencystretch=30pt
22 \inputencoding default
25 \paperfontsize default
27 \papersize letterpaper
32 \use_numerical_citations 0
33 \paperorientation portrait
40 \paragraph_separation indent
42 \quotes_language swedish
50 Please note: double dashed longoptions (e.g.
51 --version) are written this way: -
65 three consecutive dashes simply result in a long resp.
69 Architecture specific stuff (like memory models, code examples) should maybe
73 into seperate sections/chapters/appendices (it is hard to document PIC or
77 a 8051 centered document) - for now simply add.
80 SDCC Compiler User Guide
94 The above strings enclosed in $ are automatically updated by Subversion
98 \begin_inset LatexCommand \tableofcontents{}
131 ompiler) is an open source, retargettable, optimizing ANSI-C compiler by
136 designed for 8 bit Microprocessors.
137 The current version targets Intel MCS51 based Microprocessors (8031, 8032,
139 \begin_inset LatexCommand \index{8031, 8032, 8051, 8052, mcs51 CPU}
143 , etc.), Dallas DS80C390 variants, Freescale (formerly Motorola) HC08 and
144 Zilog Z80 based MCUs.
145 It can be retargetted for other microprocessors, support for Microchip
146 PIC, Atmel AVR is under development.
147 The entire source code for the compiler is distributed under GPL.
149 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
154 \begin_inset LatexCommand \index{aslink}
158 , an open source retargettable assembler & linker.
159 SDCC has extensive language extensions suitable for utilizing various microcont
160 rollers and underlying hardware effectively.
165 In addition to the MCU specific optimizations SDCC also does a host of standard
169 global sub expression elimination,
172 loop optimizations (loop invariant, strength reduction of induction variables
176 constant folding & propagation,
182 dead code elimination
192 For the back-end SDCC uses a global register allocation scheme which should
193 be well suited for other 8 bit MCUs.
198 The peep hole optimizer uses a rule based substitution mechanism which is
204 Supported data-types are:
207 char (8 bits, 1 byte),
210 short and int (16 bits, 2 bytes),
213 long (32 bit, 4 bytes)
220 The compiler also allows
222 inline assembler code
224 to be embedded anywhere in a function.
225 In addition, routines developed in assembly can also be called.
229 SDCC also provides an option (-
239 -cyclomatic) to report the relative complexity of a function.
240 These functions can then be further optimized, or hand coded in assembly
246 SDCC also comes with a companion source level debugger SDCDB, the debugger
247 currently uses ucSim a freeware simulator for 8051 and other micro-controllers.
248 SDCDB and ucSim are currently not available on Win32 platforms.
253 The latest version can be downloaded from
254 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
264 Please note: the compiler will probably always be some steps ahead of this
269 \begin_inset LatexCommand \index{Status of documentation}
279 Obviously this has pros and cons
288 All packages used in this compiler system are
296 ; source code for all the sub-packages (pre-processor, assemblers, linkers
297 etc) is distributed with the package.
298 This documentation is maintained using a freeware word processor (LyX).
300 This program is free software; you can redistribute it and/or modify it
301 under the terms of the GNU General Public License
302 \begin_inset LatexCommand \index{GNU General Public License, GPL}
306 as published by the Free Software Foundation; either version 2, or (at
307 your option) any later version.
308 This program is distributed in the hope that it will be useful, but WITHOUT
309 ANY WARRANTY; without even the implied warranty
310 \begin_inset LatexCommand \index{warranty}
314 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
315 See the GNU General Public License for more details.
316 You should have received a copy of the GNU General Public License along
317 with this program; if not, write to the Free Software Foundation, 59 Temple
318 Place - Suite 330, Boston, MA 02111-1307, USA.
319 In other words, you are welcome to use, share and improve this program.
320 You are forbidden to forbid anyone else to use, share and improve what
322 Help stamp out software-hoarding!
325 Typographic conventions
326 \begin_inset LatexCommand \index{Typographic conventions}
333 Throughout this manual, we will use the following convention.
334 Commands you have to type in are printed in
342 Code samples are printed in
347 Interesting items and new terms are printed in
352 Compatibility with previous versions
353 \begin_inset LatexCommand \index{Compatibility with previous versions}
360 This version has numerous bug fixes compared with the previous version.
361 But we also introduced some incompatibilities with older versions.
362 Not just for the fun of it, but to make the compiler more stable, efficient
364 \begin_inset LatexCommand \index{ANSI-compliance}
369 \begin_inset LatexCommand \ref{sub:ANSI-Compliance}
373 for ANSI-Compliance).
379 short is now equivalent to int (16 bits), it used to be equivalent to char
380 (8 bits) which is not ANSI compliant.
383 the default directory for gcc-builds where include, library and documentation
384 files are stored is now in /usr/local/share.
387 char type parameters to vararg functions are casted to int unless explicitly
404 will push a as an int and as a char resp.
417 -regextend has been removed.
430 -noregparms has been removed.
443 -stack-after-data has been removed.
447 \begin_inset LatexCommand \index{bit}
452 \begin_inset LatexCommand \index{sbit}
457 \begin_inset LatexCommand \index{\_\_sbit}
461 types now consistently behave like the C99 _Bool type with respect to type
463 \begin_inset LatexCommand \index{type conversion}
468 \begin_inset LatexCommand \index{type promotion}
473 The most common incompatibility resulting from this change is related to
475 \begin_inset LatexCommand \index{Bit toggling}
489 b = ~b; /* equivalent to b=1 instead of toggling b */
493 b = !b; /* toggles b */
497 In previous versions, both forms would have toggled the bit.
502 <pending: more incompatibilities?>
508 What do you need before you start installation of SDCC? A computer, and
510 The preferred method of installation is to compile SDCC from source using
512 For Windows some pre-compiled binary distributions are available for your
514 You should have some experience with command line tools and compiler use.
520 The SDCC home page at
521 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/}
525 is a great place to find distribution sets.
526 You can also find links to the user mailing lists that offer help or discuss
527 SDCC with other SDCC users.
528 Web links to other SDCC related sites can also be found here.
529 This document can be found in the DOC directory of the source package as
531 A pdf version of this document is available at
532 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/doc/sdccman.pdf}
537 Some of the other tools (simulator and assembler) included with SDCC contain
538 their own documentation and can be found in the source distribution.
539 If you want the latest unreleased software, the complete source package
540 is available directly from Subversion on https://svn.sourceforge.net/svnroot/sdcc
544 Wishes for the future
547 There are (and always will be) some things that could be done.
548 Here are some I can think of:
555 char KernelFunction3(char p) at 0x340;
563 \begin_inset LatexCommand \index{code banking (limited support)}
573 If you can think of some more, please see the section
574 \begin_inset LatexCommand \ref{sub:Requesting-Features}
578 about filing feature requests
579 \begin_inset LatexCommand \index{Requesting features}
584 \begin_inset LatexCommand \index{Feature request}
594 \begin_inset LatexCommand \index{Installation}
601 For most users it is sufficient to skip to either section
602 \begin_inset LatexCommand \ref{sub:Building-SDCC-on-Linux}
607 \begin_inset LatexCommand \ref{sub:Windows-Install}
612 More detailled instructions follow below.
616 \begin_inset LatexCommand \index{Options SDCC configuration}
623 The install paths, search paths and other options are defined when running
625 The defaults can be overridden by:
627 \labelwidthstring 00.00.0000
639 -prefix see table below
641 \labelwidthstring 00.00.0000
653 -exec_prefix see table below
655 \labelwidthstring 00.00.0000
667 -bindir see table below
669 \labelwidthstring 00.00.0000
681 -datadir see table below
683 \labelwidthstring 00.00.0000
685 docdir environment variable, see table below
687 \labelwidthstring 00.00.0000
689 include_dir_suffix environment variable, see table below
691 \labelwidthstring 00.00.0000
693 lib_dir_suffix environment variable, see table below
695 \labelwidthstring 00.00.0000
697 sdccconf_h_dir_separator environment variable, either / or
702 This character will only be used in sdccconf.h; don't forget it's a C-header,
703 therefore a double-backslash is needed there.
705 \labelwidthstring 00.00.0000
717 -disable-mcs51-port Excludes the Intel mcs51 port
719 \labelwidthstring 00.00.0000
731 -disable-gbz80-port Excludes the Gameboy gbz80 port
733 \labelwidthstring 00.00.0000
745 -disable-z80-port Excludes the z80 port
747 \labelwidthstring 00.00.0000
759 -disable-avr-port Excludes the AVR port
761 \labelwidthstring 00.00.0000
773 -disable-ds390-port Excludes the DS390 port
775 \labelwidthstring 00.00.0000
787 -disable-hc08-port Excludes the HC08 port
789 \labelwidthstring 00.00.0000
801 -disable-pic-port Excludes the PIC port
803 \labelwidthstring 00.00.0000
815 -disable-xa51-port Excludes the XA51 port
817 \labelwidthstring 00.00.0000
829 -disable-ucsim Disables configuring and building of ucsim
831 \labelwidthstring 00.00.0000
843 -disable-device-lib-build Disables automatically building device libraries
845 \labelwidthstring 00.00.0000
857 -disable-packihx Disables building packihx
859 \labelwidthstring 00.00.0000
871 -enable-libgc Use the Bohem memory allocator.
872 Lower runtime footprint.
875 Furthermore the environment variables CC, CFLAGS, ...
876 the tools and their arguments can be influenced.
877 Please see `configure -
887 -help` and the man/info pages of `configure` for details.
891 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
892 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
893 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
894 At the moment it's not possible to change the default settings (it was
895 simply never required).
899 These configure options are compiled into the binaries, and can only be
900 changed by rerunning 'configure' and recompiling SDCC.
901 The configure options are written in
905 to distinguish them from run time environment variables (see section search
911 \begin_inset Quotes sld
915 \begin_inset Quotes srd
918 are used by the SDCC team to build the official Win32 binaries.
919 The SDCC team uses Mingw32 to build the official Windows binaries, because
926 a gcc compiler and last but not least
929 the binaries can be built by cross compiling on Sourceforge's compile farm.
932 See the examples, how to pass the Win32 settings to 'configure'.
933 The other Win32 builds using Borland, VC or whatever don't use 'configure',
934 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
946 <lyxtabular version="3" rows="8" columns="3">
948 <column alignment="block" valignment="top" leftline="true" width="0in">
949 <column alignment="block" valignment="top" leftline="true" width="0in">
950 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
951 <row topline="true" bottomline="true">
952 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
968 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
996 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1007 <row topline="true">
1008 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1028 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1039 <row topline="true">
1040 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1050 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1062 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1077 <row topline="true">
1078 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1088 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1100 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1111 <row topline="true">
1112 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1122 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1134 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1149 <row topline="true">
1150 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1160 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1168 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1177 <row topline="true" bottomline="true">
1178 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1188 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1196 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1214 'configure' also computes relative paths.
1215 This is needed for full relocatability of a binary package and to complete
1216 search paths (see section search paths below):
1222 \begin_inset Tabular
1223 <lyxtabular version="3" rows="4" columns="3">
1225 <column alignment="block" valignment="top" leftline="true" width="0in">
1226 <column alignment="block" valignment="top" leftline="true" width="0in">
1227 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1228 <row topline="true" bottomline="true">
1229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1254 <row topline="true" bottomline="true">
1255 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1273 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1282 <row bottomline="true">
1283 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1310 <row bottomline="true">
1311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1362 \begin_inset Quotes srd
1366 \begin_inset Quotes srd
1380 \begin_inset Quotes srd
1384 \begin_inset Quotes srd
1412 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1421 \begin_inset Quotes srd
1424 i586-mingw32msvc-gcc
1425 \begin_inset Quotes srd
1429 \begin_inset Quotes srd
1432 i586-mingw32msvc-g++
1433 \begin_inset Quotes srd
1441 \begin_inset Quotes srd
1444 i586-mingw32msvc-ranlib
1445 \begin_inset Quotes srd
1453 \begin_inset Quotes srd
1456 i586-mingw32msvc-strip
1457 \begin_inset Quotes srd
1475 \begin_inset Quotes srd
1479 \begin_inset Quotes srd
1497 \begin_inset Quotes srd
1501 \begin_inset Quotes srd
1509 \begin_inset Quotes srd
1513 \begin_inset Quotes srd
1521 \begin_inset Quotes srd
1525 \begin_inset Quotes srd
1533 \begin_inset Quotes srd
1537 \begin_inset Quotes srd
1544 sdccconf_h_dir_separator=
1545 \begin_inset Quotes srd
1557 \begin_inset Quotes srd
1574 -disable-device-lib-build
1602 -host=i586-mingw32msvc -
1612 -build=unknown-unknown-linux-gnu
1616 \begin_inset Quotes sld
1620 \begin_inset Quotes srd
1623 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1632 \begin_inset Quotes srd
1636 \begin_inset Quotes srd
1644 \begin_inset Quotes srd
1648 \begin_inset Quotes srd
1666 \begin_inset Quotes srd
1670 \begin_inset Quotes srd
1688 \begin_inset Quotes srd
1692 \begin_inset Quotes srd
1700 \begin_inset Quotes srd
1704 \begin_inset Quotes srd
1712 \begin_inset Quotes srd
1716 \begin_inset Quotes srd
1724 \begin_inset Quotes srd
1728 \begin_inset Quotes srd
1735 sdccconf_h_dir_separator=
1736 \begin_inset Quotes srd
1748 \begin_inset Quotes srd
1768 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1779 -C' turns on caching, which gives a little bit extra speed.
1780 However if options are changed, it can be necessary to delete the config.cache
1785 \begin_inset LatexCommand \label{sub:Install-paths}
1790 \begin_inset LatexCommand \index{Install paths}
1796 \added_space_top medskip \align center
1798 \begin_inset Tabular
1799 <lyxtabular version="3" rows="5" columns="4">
1801 <column alignment="center" valignment="top" leftline="true" width="0">
1802 <column alignment="center" valignment="top" leftline="true" width="0">
1803 <column alignment="center" valignment="top" leftline="true" width="0">
1804 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1805 <row topline="true" bottomline="true">
1806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1847 <row topline="true">
1848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1874 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1887 <row topline="true">
1888 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1896 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1903 $DATADIR/ $INCLUDE_DIR_SUFFIX
1906 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1911 /usr/local/share/sdcc/include
1914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1927 <row topline="true">
1928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1943 $DATADIR/$LIB_DIR_SUFFIX
1946 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1951 /usr/local/share/sdcc/lib
1954 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1967 <row topline="true" bottomline="true">
1968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1976 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1991 /usr/local/share/sdcc/doc
1994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2016 *compiler, preprocessor, assembler, and linker
2022 is auto-appended by the compiler, e.g.
2023 small, large, z80, ds390 etc
2026 The install paths can still be changed during `make install` with e.g.:
2029 make install prefix=$(HOME)/local/sdcc
2032 Of course this doesn't change the search paths compiled into the binaries.
2036 Moreover the install path can be changed by defining DESTDIR
2037 \begin_inset LatexCommand \index{DESTDIR}
2044 make install DESTDIR=$(HOME)/sdcc.rpm/
2047 Please note that DESTDIR must have a trailing slash!
2051 \begin_inset LatexCommand \label{sub:Search-Paths}
2056 \begin_inset LatexCommand \index{Search path}
2063 Some search paths or parts of them are determined by configure variables
2068 , see section above).
2069 Further search paths are determined by environment variables during runtime.
2072 The paths searched when running the compiler are as follows (the first catch
2078 Binary files (preprocessor, assembler and linker)
2084 \begin_inset Tabular
2085 <lyxtabular version="3" rows="4" columns="3">
2087 <column alignment="block" valignment="top" leftline="true" width="0in">
2088 <column alignment="block" valignment="top" leftline="true" width="0in">
2089 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2090 <row topline="true" bottomline="true">
2091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2116 <row topline="true">
2117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2146 <row topline="true">
2147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2152 Path of argv[0] (if available)
2155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2172 <row topline="true" bottomline="true">
2173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2214 \begin_inset Tabular
2215 <lyxtabular version="3" rows="6" columns="3">
2217 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2218 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2219 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2220 <row topline="true" bottomline="true">
2221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2246 <row topline="true">
2247 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2283 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2302 <row topline="true">
2303 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2328 <row topline="true">
2329 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2343 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2355 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2366 <row topline="true">
2367 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2385 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2435 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2448 <row topline="true" bottomline="true">
2449 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2465 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2470 /usr/local/share/sdcc/
2475 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2503 -nostdinc disables the last two search paths.
2513 With the exception of
2514 \begin_inset Quotes sld
2528 \begin_inset Quotes srd
2535 is auto-appended by the compiler (e.g.
2536 small, large, z80, ds390 etc.).
2543 \begin_inset Tabular
2544 <lyxtabular version="3" rows="6" columns="3">
2546 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2547 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2548 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2549 <row topline="true" bottomline="true">
2550 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2558 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2575 <row topline="true">
2576 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2612 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2631 <row topline="true">
2632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2644 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2671 <row topline="true">
2672 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2683 $LIB_DIR_SUFFIX/<model>
2686 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2700 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2717 <row topline="true">
2718 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2733 $LIB_DIR_SUFFIX/<model>
2736 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2789 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2845 <row topline="true" bottomline="true">
2846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2855 $LIB_DIR_SUFFIX/<model>
2858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2863 /usr/local/share/sdcc/
2870 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2888 Don't delete any of the stray spaces in the table above without checking
2889 the HTML output (last line)!
2905 -nostdlib disables the last two search paths.
2909 \begin_inset LatexCommand \index{Building SDCC}
2916 Building SDCC on Linux
2917 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2926 Download the source package
2928 either from the SDCC Subversion repository or from the nightly snapshots
2930 , it will be named something like sdcc
2941 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2950 Bring up a command line terminal, such as xterm.
2955 Unpack the file using a command like:
2958 "tar -xvzf sdcc.src.tar.gz
2963 , this will create a sub-directory called sdcc with all of the sources.
2966 Change directory into the main SDCC directory, for example type:
2983 This configures the package for compilation on your system.
2999 All of the source packages will compile, this can take a while.
3015 This copies the binary executables, the include files, the libraries and
3016 the documentation to the install directories.
3017 Proceed with section
3018 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3025 Building SDCC on OSX 2.x
3028 Follow the instruction for Linux.
3032 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3033 )) fails to compile SDCC.
3034 Fortunately there's also gcc 2.9.x installed, which works fine.
3035 This compiler can be selected by running 'configure' with:
3038 ./configure CC=gcc2 CXX=g++2
3041 Cross compiling SDCC on Linux for Windows
3044 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3045 See section 'Configure Options'.
3048 Building SDCC on Windows
3051 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3053 They use Unix-sockets, which are not available on Win32.
3056 Building SDCC using Cygwin and Mingw32
3059 For building and installing a Cygwin executable follow the instructions
3065 \begin_inset Quotes sld
3069 \begin_inset Quotes srd
3072 Win32-binary can be built, which will not need the Cygwin-DLL.
3073 For the necessary 'configure' options see section 'configure options' or
3074 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3078 In order to install Cygwin on Windows download setup.exe from
3079 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3085 \begin_inset Quotes sld
3088 default text file type
3089 \begin_inset Quotes srd
3093 \begin_inset Quotes sld
3097 \begin_inset Quotes srd
3100 and download/install at least the following packages.
3101 Some packages are selected by default, others will be automatically selected
3102 because of dependencies with the manually selected packages.
3103 Never deselect these packages!
3112 gcc ; version 3.x is fine, no need to use the old 2.9x
3115 binutils ; selected with gcc
3121 rxvt ; a nice console, which makes life much easier under windoze (see below)
3124 man ; not really needed for building SDCC, but you'll miss it sooner or
3128 less ; not really needed for building SDCC, but you'll miss it sooner or
3132 svn ; only if you use Subversion access
3135 If you want to develop something you'll need:
3138 python ; for the regression tests
3141 gdb ; the gnu debugger, together with the nice GUI
3142 \begin_inset Quotes sld
3146 \begin_inset Quotes srd
3152 openssh ; to access the CF or commit changes
3155 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3156 use autoconf-stable!
3159 rxvt is a nice console with history.
3160 Replace in your cygwin.bat the line
3179 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3182 -bg black -fg white -geometry 100x65 -e bash -
3195 Text selected with the mouse is automatically copied to the clipboard, pasting
3196 works with shift-insert.
3200 The other good tip is to make sure you have no //c/-style paths anywhere,
3201 use /cygdrive/c/ instead.
3202 Using // invokes a network lookup which is very slow.
3204 \begin_inset Quotes sld
3208 \begin_inset Quotes srd
3211 is too long, you can change it with e.g.
3217 SDCC sources use the unix line ending LF.
3218 Life is much easier, if you store the source tree on a drive which is mounted
3220 And use an editor which can handle LF-only line endings.
3221 Make sure not to commit files with windows line endings.
3222 The tabulator spacing
3223 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3227 used in the project is 8.
3228 Although a tabulator spacing of 8 is a sensible choice for programmers
3229 (it's a power of 2 and allows to display 8/16 bit signed variables without
3230 loosing columns) the plan is to move towards using only spaces in the source.
3233 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3238 Download the source package
3240 either from the SDCC Subversion repository or from the
3241 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3247 , it will be named something like sdcc
3254 SDCC is distributed with all the projects, workspaces, and files you need
3255 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3256 doesn't build under MSVC).
3257 The workspace name is 'sdcc.dsw'.
3258 Please note that as it is now, all the executables are created in a folder
3262 Once built you need to copy the executables from sdcc
3266 bin before running SDCC.
3271 WARNING: Visual studio is very picky with line terminations; it expects
3272 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3273 When using the Subversion repository it's easiest to configure the svn
3274 client to convert automatically for you.
3275 If however you are getting a message such as "This makefile was not generated
3276 by Developer Studio etc.
3278 \begin_inset Quotes srd
3281 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3282 need to convert the Unix style line endings to DOS style line endings.
3283 To do so you can use the
3284 \begin_inset Quotes sld
3288 \begin_inset Quotes srd
3291 utility freely available on the internet.
3292 Doug Hawkins reported in the sdcc-user list that this works:
3300 SDCC> unix2dos sdcc.dsw
3306 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3310 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3311 flex.exe, and gawk.exe.
3312 One good place to get them is
3313 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3321 Download the file UnxUtils
3322 \begin_inset LatexCommand \index{UnxUtils}
3327 Now you have to install the utilities and setup MSVC so it can locate the
3329 Here there are two alternatives (choose one!):
3336 a) Extract UnxUtils.zip to your C:
3338 hard disk PRESERVING the original paths, otherwise bison won't work.
3339 (If you are using WinZip make certain that 'Use folder names' is selected)
3343 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3344 in 'Show directories for:' select 'Executable files', and in the directories
3345 window add a new path: 'C:
3355 (As a side effect, you get a bunch of Unix utilities that could be useful,
3356 such as diff and patch.)
3363 This one avoids extracting a bunch of files you may not use, but requires
3368 a) Create a directory were to put the tools needed, or use a directory already
3376 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3377 to such directory WITHOUT preserving the original paths.
3378 (If you are using WinZip make certain that 'Use folder names' is not selected)
3382 c) Rename bison.exe to '_bison.exe'.
3386 d) Create a batch file 'bison.bat' in 'C:
3390 ' and add these lines:
3410 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3414 Steps 'c' and 'd' are needed because bison requires by default that the
3415 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3416 '/usr/local/share/' I think.
3417 So it is necessary to tell bison where those files are located if they
3418 are not in such directory.
3419 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3423 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3424 in 'Show directories for:' select 'Executable files', and in the directories
3425 window add a new path: 'c:
3428 Note that you can use any other path instead of 'c:
3430 util', even the path where the Visual C++ tools are, probably: 'C:
3434 Microsoft Visual Studio
3439 So you don't have to execute step 'e' :)
3443 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3444 the executables from sdcc
3448 bin, and you can compile using SDCC.
3451 Building SDCC Using Borland
3454 From the sdcc directory, run the command "make -f Makefile.bcc".
3455 This should regenerate all the .exe files in the bin directory except for
3456 sdcdb.exe (which currently doesn't build under Borland C++).
3459 If you modify any source files and need to rebuild, be aware that the dependenci
3460 es may not be correctly calculated.
3461 The safest option is to delete all .obj files and run the build again.
3462 From a Cygwin BASH prompt, this can easily be done with the command (be
3463 sure you are in the sdcc directory):
3473 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3475 ) -print -exec rm {}
3484 or on Windows NT/2000/XP from the command prompt with the command:
3491 del /s *.obj *.lib *.rul
3494 from the sdcc directory.
3497 Windows Install Using a ZIP Package
3500 Download the binary zip package from
3501 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3505 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3506 This should unpack to a group of sub-directories.
3507 An example directory structure after unpacking the mingw32 package is:
3512 bin for the executables, c:
3520 lib for the include and libraries.
3523 Adjust your environment variable PATH to include the location of the bin
3524 directory or start sdcc using the full path.
3527 Windows Install Using the Setup Program
3528 \begin_inset LatexCommand \label{sub:Windows-Install}
3535 Download the setup program
3537 sdcc-x.y.z-setup.exe
3539 for an official release from
3542 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3546 or a setup program for one of the snapshots
3548 sdcc_yyyymmdd_setup.exe
3551 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3556 A windows typical installer will guide you through the installation process.
3559 Building the Documentation
3562 If the necessary tools (LyX, LaTeX, LaTeX2HTML) are installed it is as easy
3563 as changing into the doc directory and typing
3567 \begin_inset Quotes srd
3571 \begin_inset Quotes srd
3578 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3581 \begin_inset LatexCommand \url{http://www.lyx.org}
3585 as editor this is straightforward.
3586 Prebuilt documentation in html and pdf format is available from
3587 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3594 Reading the Documentation
3597 Currently reading the document in pdf format is recommended, as for unknown
3598 reason the hyperlinks are working there whereas in the html version they
3605 If you should know why please drop us a note
3611 You'll find the pdf version
3612 \begin_inset LatexCommand \index{PDF version of this document}
3617 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3625 \begin_inset LatexCommand \index{HTML version of this document}
3630 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3636 This documentation is in some aspects different from a commercial documentation:
3640 It tries to document SDCC for several processor architectures in one document
3641 (commercially these probably would be separate documents/products).
3643 \begin_inset LatexCommand \index{Status of documentation}
3647 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3649 Z80, PIC14, PIC16 and HC08.
3652 There are many references pointing away from this documentation.
3653 Don't let this distract you.
3655 was a reference like
3656 \begin_inset LatexCommand \url{http://www.opencores.org}
3660 together with a statement
3661 \begin_inset Quotes sld
3664 some processors which are targetted by SDCC can be implemented in a
3681 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
3686 \begin_inset Quotes srd
3690 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
3695 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
3700 \begin_inset Quotes sld
3703 have you ever heard of an open source compiler that compiles a subset of
3705 \begin_inset Quotes srd
3708 we expect you to have a quick look there and come back.
3709 If you read this you are on the right track.
3712 Some sections attribute more space to problems, restrictions and warnings
3713 than to the solution.
3716 The installation section and the section about the debugger is intimidating.
3719 There are still lots of typos and there are more different writing styles
3723 Testing the SDCC Compiler
3724 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3731 The first thing you should do after installing your SDCC compiler is to
3747 \begin_inset LatexCommand \index{version}
3754 at the prompt, and the program should run and tell you the version.
3755 If it doesn't run, or gives a message about not finding sdcc program, then
3756 you need to check over your installation.
3757 Make sure that the sdcc bin directory is in your executable search path
3758 defined by the PATH environment setting (
3763 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3770 Install trouble-shooting for suggestions
3773 Make sure that the sdcc program is in the bin folder, if not perhaps something
3774 did not install correctly.
3782 is commonly installed as described in section
3783 \begin_inset Quotes sld
3786 Install and search paths
3787 \begin_inset Quotes srd
3796 Make sure the compiler works on a very simple example.
3797 Type in the following test.c program using your favorite
3823 Compile this using the following command:
3832 If all goes well, the compiler will generate a test.asm and test.rel file.
3833 Congratulations, you've just compiled your first program with SDCC.
3834 We used the -c option to tell SDCC not to link the generated code, just
3835 to keep things simple for this step.
3843 The next step is to try it with the linker.
3853 If all goes well the compiler will link with the libraries and produce
3854 a test.ihx output file.
3859 (no test.ihx, and the linker generates warnings), then the problem is most
3868 usr/local/share/sdcc/lib directory
3875 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3882 Install trouble-shooting for suggestions).
3890 The final test is to ensure
3898 header files and libraries.
3899 Edit test.c and change it to the following:
3916 strcpy(str1, "testing");
3923 Compile this by typing
3930 This should generate a test.ihx output file, and it should give no warnings
3931 such as not finding the string.h file.
3932 If it cannot find the string.h file, then the problem is that
3936 cannot find the /usr/local/share/sdcc/include directory
3943 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3950 Install trouble-shooting section for suggestions).
3968 \begin_inset LatexCommand \index{-\/-print-search-dirs}
3972 to find exactly where SDCC is looking for the include and lib files.
3975 Install Trouble-shooting
3976 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
3981 \begin_inset LatexCommand \index{Install trouble-shooting}
3988 If SDCC does not build correctly
3991 A thing to try is starting from scratch by unpacking the .tgz source package
3992 again in an empty directory.
4000 ./configure 2>&1 | tee configure.log
4014 make 2>&1 | tee make.log
4021 If anything goes wrong, you can review the log files to locate the problem.
4022 Or a relevant part of this can be attached to an email that could be helpful
4023 when requesting help from the mailing list.
4027 \begin_inset Quotes sld
4031 \begin_inset Quotes srd
4038 \begin_inset Quotes sld
4042 \begin_inset Quotes srd
4045 command is a script that analyzes your system and performs some configuration
4046 to ensure the source package compiles on your system.
4047 It will take a few minutes to run, and will compile a few tests to determine
4048 what compiler features are installed.
4052 \begin_inset Quotes sld
4056 \begin_inset Quotes srd
4062 This runs the GNU make tool, which automatically compiles all the source
4063 packages into the final installed binary executables.
4067 \begin_inset Quotes sld
4071 \begin_inset Quotes erd
4077 This will install the compiler, other executables libraries and include
4078 files into the appropriate directories.
4080 \begin_inset LatexCommand \ref{sub:Install-paths}
4086 \begin_inset LatexCommand \ref{sub:Search-Paths}
4091 about install and search paths.
4093 On most systems you will need super-user privileges to do this.
4099 SDCC is not just a compiler, but a collection of tools by various developers.
4100 These include linkers, assemblers, simulators and other components.
4101 Here is a summary of some of the components.
4102 Note that the included simulator and assembler have separate documentation
4103 which you can find in the source package in their respective directories.
4104 As SDCC grows to include support for other processors, other packages from
4105 various developers are included and may have their own sets of documentation.
4109 You might want to look at the files which are installed in <installdir>.
4110 At the time of this writing, we find the following programs for gcc-builds:
4114 In <installdir>/bin:
4117 sdcc - The compiler.
4120 sdcpp - The C preprocessor.
4123 asx8051 - The assembler for 8051 type processors.
4130 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4133 aslink -The linker for 8051 type processors.
4140 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4143 s51 - The ucSim 8051 simulator.
4144 Not available on Win32 platforms.
4147 sdcdb - The source debugger.
4148 Not available on Win32 platforms.
4151 packihx - A tool to pack (compress) Intel hex files.
4154 In <installdir>/share/sdcc/include
4160 In <installdir>/share/sdcc/lib
4163 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4167 In <installdir>/share/sdcc/doc
4173 As development for other processors proceeds, this list will expand to include
4174 executables to support processors like AVR, PIC, etc.
4180 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4181 the assembler and linkage editor.
4184 sdcpp - The C-Preprocessor
4188 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4192 is a modified version of the GNU preprocessor.
4193 The C preprocessor is used to pull in #include sources, process #ifdef
4194 statements, #defines and so on.
4205 - The Assemblers and Linkage Editors
4208 This is retargettable assembler & linkage editor, it was developed by Alan
4210 John Hartman created the version for 8051, and I (Sandeep) have made some
4211 enhancements and bug fixes for it to work properly with SDCC.
4218 \begin_inset LatexCommand \index{s51}
4222 is a freeware, opensource simulator developed by Daniel Drotos.
4223 The simulator is built as part of the build process.
4224 For more information visit Daniel's web site at:
4225 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4230 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4232 S51 is currently not available on Win32 platfors.
4235 sdcdb - Source Level Debugger
4239 \begin_inset LatexCommand \index{sdcdb (debugger)}
4243 is the companion source level debugger.
4244 More about sdcdb in section
4245 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4250 The current version of the debugger uses Daniel's Simulator S51
4251 \begin_inset LatexCommand \index{s51}
4255 , but can be easily changed to use other simulators.
4256 Sdcdb is currently not available on Win32 platfors.
4265 Single Source File Projects
4268 For single source file 8051 projects the process is very simple.
4269 Compile your programs with the following command
4272 "sdcc sourcefile.c".
4276 This will compile, assemble and link your source file.
4277 Output files are as follows:
4281 \begin_inset LatexCommand \index{<file>.asm}
4286 \begin_inset LatexCommand \index{Assembler source}
4290 file created by the compiler
4294 \begin_inset LatexCommand \index{<file>.lst}
4299 \begin_inset LatexCommand \index{Assembler listing}
4303 file created by the Assembler
4307 \begin_inset LatexCommand \index{<file>.rst}
4312 \begin_inset LatexCommand \index{Assembler listing}
4316 file updated with linkedit information, created by linkage editor
4320 \begin_inset LatexCommand \index{<file>.sym}
4325 \begin_inset LatexCommand \index{Symbol listing}
4329 for the sourcefile, created by the assembler
4333 \begin_inset LatexCommand \index{<file>.rel}
4338 \begin_inset LatexCommand \index{<file>.o}
4343 \begin_inset LatexCommand \index{Object file}
4347 created by the assembler, input to Linkage editor
4351 \begin_inset LatexCommand \index{<file>.map}
4356 \begin_inset LatexCommand \index{Memory map}
4360 for the load module, created by the Linker
4364 \begin_inset LatexCommand \index{<file>.mem}
4368 - A file with a summary of the memory usage
4372 \begin_inset LatexCommand \index{<file>.ihx}
4376 - The load module in Intel hex format
4377 \begin_inset LatexCommand \index{Intel hex format}
4381 (you can select the Motorola S19 format
4382 \begin_inset LatexCommand \index{Motorola S19 format}
4397 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4402 If you need another format you might want to use
4409 \begin_inset LatexCommand \index{objdump (tool)}
4420 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4425 Both formats are documented in the documentation of srecord
4426 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4434 \begin_inset LatexCommand \index{<file>.adb}
4438 - An intermediate file containing debug information needed to create the
4450 \begin_inset LatexCommand \index{-\/-debug}
4458 \begin_inset LatexCommand \index{<file>.cdb}
4462 - An optional file (with -
4472 -debug) containing debug information.
4473 The format is documented in cdbfileformat.pdf
4478 \begin_inset LatexCommand \index{<file> (no extension)}
4482 An optional AOMF or AOMF51
4483 \begin_inset LatexCommand \index{AOMF, AOMF51}
4487 file containing debug information (generated with option -
4514 ormat is commonly used by third party tools (debuggers
4515 \begin_inset LatexCommand \index{Debugger}
4519 , simulators, emulators)
4523 \begin_inset LatexCommand \index{<file>.dump*}
4527 - Dump file to debug the compiler it self (generated with option -
4537 -dumpall) (see section
4538 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4544 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4550 \begin_inset Quotes sld
4553 Anatomy of the compiler
4554 \begin_inset Quotes srd
4560 Projects with Multiple Source Files
4563 SDCC can compile only ONE file at a time.
4564 Let us for example assume that you have a project containing the following
4569 foo1.c (contains some functions)
4571 foo2.c (contains some more functions)
4573 foomain.c (contains more functions and the function main)
4581 The first two files will need to be compiled separately with the commands:
4613 Then compile the source file containing the
4618 \begin_inset LatexCommand \index{Linker}
4622 the files together with the following command:
4630 foomain.c\SpecialChar ~
4631 foo1.rel\SpecialChar ~
4636 \begin_inset LatexCommand \index{<file>.rel}
4648 can be separately compiled as well:
4659 sdcc foomain.rel foo1.rel foo2.rel
4666 The file containing the
4681 file specified in the command line, since the linkage editor processes
4682 file in the order they are presented to it.
4683 The linker is invoked from SDCC using a script file with extension .lnk
4684 \begin_inset LatexCommand \index{<file>.lnk}
4689 You can view this file to troubleshoot linking problems such as those arising
4690 from missing libraries.
4693 Projects with Additional Libraries
4694 \begin_inset LatexCommand \index{Libraries}
4701 Some reusable routines may be compiled into a library, see the documentation
4702 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4706 \begin_inset LatexCommand \index{<file>.lib}
4713 Libraries created in this manner can be included in the command line.
4714 Make sure you include the -L <library-path> option to tell the linker where
4715 to look for these files if they are not in the current directory.
4716 Here is an example, assuming you have the source file
4728 (if that is not the same as your current project):
4735 sdcc foomain.c foolib.lib -L mylib
4746 must be an absolute path name.
4750 The most efficient way to use libraries is to keep separate modules in separate
4752 The lib file now should name all the modules.rel
4753 \begin_inset LatexCommand \index{<file>.rel}
4758 For an example see the standard library file
4762 in the directory <installdir>/share/lib/small.
4765 Using sdcclib to Create and Manage Libraries
4766 \begin_inset LatexCommand \index{sdcclib}
4773 Alternatively, instead of having a .rel file for each entry on the library
4774 file as described in the preceding section, sdcclib can be used to embed
4775 all the modules belonging to such library in the library file itself.
4776 This results in a larger library file, but it greatly reduces the number
4777 of disk files accessed by the linker.
4778 Additionally, the packed library file contains an index of all include
4779 modules and symbols that significantly speeds up the linking process.
4780 To display a list of options supported by sdcclib type:
4789 \begin_inset LatexCommand \index{sdcclib}
4800 To create a new library file, start by compiling all the required modules.
4838 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4840 The next step is to add the .rel files to the library file:
4848 sdcclib libint.lib _divsint.rel
4851 \begin_inset LatexCommand \index{sdcclib}
4861 sdcclib libint.lib _divuint.rel
4867 sdcclib libint.lib _modsint.rel
4873 sdcclib libint.lib _moduint.rel
4879 sdcclib libint.lib _mulint.rel
4886 If the file already exists in the library, it will be replaced.
4887 To see what modules and symbols are included in the library, options -s
4888 and -m are available.
4896 sdcclib -s libint.lib
4899 \begin_inset LatexCommand \index{sdcclib}
5009 If the source files are compiled using -
5020 \begin_inset LatexCommand \index{-\/-debug}
5024 , the corresponding debug information file .adb will be include in the library
5026 The library files created with sdcclib are plain text files, so they can
5027 be viewed with a text editor.
5028 It is not recomended to modify a library file created with sdcclib using
5029 a text editor, as there are file indexes numbers located accross the file
5030 used by the linker to quickly locate the required module to link.
5031 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5032 it can be safely deleted, since all the information required for linking
5033 is embedded in the library file itself.
5034 Library files created using sdcclib are used as described in the preceding
5038 Command Line Options
5039 \begin_inset LatexCommand \index{Command Line Options}
5046 Processor Selection Options
5047 \begin_inset LatexCommand \index{Options processor selection}
5052 \begin_inset LatexCommand \index{Processor selection options}
5058 \labelwidthstring 00.00.0000
5063 \begin_inset LatexCommand \index{-mmcs51}
5069 Generate code for the Intel MCS51
5070 \begin_inset LatexCommand \index{MCS51}
5074 family of processors.
5075 This is the default processor target.
5077 \labelwidthstring 00.00.0000
5082 \begin_inset LatexCommand \index{-mds390}
5088 Generate code for the Dallas DS80C390
5089 \begin_inset LatexCommand \index{DS80C390}
5095 \labelwidthstring 00.00.0000
5100 \begin_inset LatexCommand \index{-mds400}
5106 Generate code for the Dallas DS80C400
5107 \begin_inset LatexCommand \index{DS80C400}
5113 \labelwidthstring 00.00.0000
5118 \begin_inset LatexCommand \index{-mhc08}
5124 Generate code for the Freescale/Motorola HC08
5125 \begin_inset LatexCommand \index{HC08}
5129 family of processors.
5131 \labelwidthstring 00.00.0000
5136 \begin_inset LatexCommand \index{-mz80}
5142 Generate code for the Zilog Z80
5143 \begin_inset LatexCommand \index{Z80}
5147 family of processors.
5149 \labelwidthstring 00.00.0000
5154 \begin_inset LatexCommand \index{-mgbz80}
5160 Generate code for the GameBoy Z80
5161 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5165 processor (Not actively maintained).
5167 \labelwidthstring 00.00.0000
5172 \begin_inset LatexCommand \index{-mavr}
5178 Generate code for the Atmel AVR
5179 \begin_inset LatexCommand \index{AVR}
5183 processor (In development, not complete).
5184 AVR users should probably have a look at winavr
5185 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5190 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5197 I think it is fair to direct users there for now.
5198 Open source is also about avoiding unnecessary work .
5199 But I didn't find the 'official' link.
5201 \labelwidthstring 00.00.0000
5206 \begin_inset LatexCommand \index{-mpic14}
5212 Generate code for the Microchip PIC 14
5213 \begin_inset LatexCommand \index{PIC14}
5217 -bit processors (p16f84 and variants.
5218 In development, not complete).
5221 p16f627 p16f628 p16f84 p16f873 p16f877?
5223 \labelwidthstring 00.00.0000
5228 \begin_inset LatexCommand \index{-mpic16}
5234 Generate code for the Microchip PIC 16
5235 \begin_inset LatexCommand \index{PIC16}
5239 -bit processors (p18f452 and variants.
5240 In development, not complete).
5242 \labelwidthstring 00.00.0000
5248 Generate code for the Toshiba TLCS-900H
5249 \begin_inset LatexCommand \index{TLCS-900H}
5253 processor (Not maintained, not complete).
5255 \labelwidthstring 00.00.0000
5260 \begin_inset LatexCommand \index{-mxa51}
5266 Generate code for the Phillips XA51
5267 \begin_inset LatexCommand \index{XA51}
5271 processor (Not maintained, not complete).
5274 Preprocessor Options
5275 \begin_inset LatexCommand \index{Options preprocessor}
5280 \begin_inset LatexCommand \index{Preprocessor options}
5285 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5291 \labelwidthstring 00.00.0000
5296 \begin_inset LatexCommand \index{-I<path>}
5302 The additional location where the pre processor will look for <..h> or
5303 \begin_inset Quotes eld
5307 \begin_inset Quotes erd
5312 \labelwidthstring 00.00.0000
5317 \begin_inset LatexCommand \index{-D<macro[=value]>}
5323 Command line definition of macros.
5324 Passed to the preprocessor.
5326 \labelwidthstring 00.00.0000
5331 \begin_inset LatexCommand \index{-M}
5337 Tell the preprocessor to output a rule suitable for make describing the
5338 dependencies of each object file.
5339 For each source file, the preprocessor outputs one make-rule whose target
5340 is the object file name for that source file and whose dependencies are
5341 all the files `#include'd in it.
5342 This rule may be a single line or may be continued with `
5344 '-newline if it is long.
5345 The list of rules is printed on standard output instead of the preprocessed
5348 \begin_inset LatexCommand \index{-E}
5354 \labelwidthstring 00.00.0000
5359 \begin_inset LatexCommand \index{-C}
5365 Tell the preprocessor not to discard comments.
5366 Used with the `-E' option.
5368 \labelwidthstring 00.00.0000
5373 \begin_inset LatexCommand \index{-MM}
5384 Like `-M' but the output mentions only the user header files included with
5386 \begin_inset Quotes eld
5390 System header files included with `#include <file>' are omitted.
5392 \labelwidthstring 00.00.0000
5397 \begin_inset LatexCommand \index{-Aquestion(answer)}
5403 Assert the answer answer for question, in case it is tested with a preprocessor
5404 conditional such as `#if #question(answer)'.
5405 `-A-' disables the standard assertions that normally describe the target
5408 \labelwidthstring 00.00.0000
5413 \begin_inset LatexCommand \index{-Umacro}
5419 Undefine macro macro.
5420 `-U' options are evaluated after all `-D' options, but before any `-include'
5421 and `-imacros' options.
5423 \labelwidthstring 00.00.0000
5428 \begin_inset LatexCommand \index{-dM}
5434 Tell the preprocessor to output only a list of the macro definitions that
5435 are in effect at the end of preprocessing.
5436 Used with the `-E' option.
5438 \labelwidthstring 00.00.0000
5443 \begin_inset LatexCommand \index{-dD}
5449 Tell the preprocessor to pass all macro definitions into the output, in
5450 their proper sequence in the rest of the output.
5452 \labelwidthstring 00.00.0000
5457 \begin_inset LatexCommand \index{-dN}
5468 Like `-dD' except that the macro arguments and contents are omitted.
5469 Only `#define name' is included in the output.
5471 \labelwidthstring 00.00.0000
5476 preprocessorOption[,preprocessorOption]
5479 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5484 Pass the preprocessorOption to the preprocessor
5489 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5494 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5495 Collection (gcc), if you need more dedicated options please refer to the
5497 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5505 \begin_inset LatexCommand \index{Options linker}
5510 \begin_inset LatexCommand \index{Linker options}
5516 \labelwidthstring 00.00.0000
5536 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5541 \begin_inset LatexCommand \index{-L -\/-lib-path}
5548 <absolute path to additional libraries> This option is passed to the linkage
5549 editor's additional libraries
5550 \begin_inset LatexCommand \index{Libraries}
5555 The path name must be absolute.
5556 Additional library files may be specified in the command line.
5557 See section Compiling programs for more details.
5559 \labelwidthstring 00.00.0000
5576 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5581 <Value> The start location of the external ram
5582 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5586 , default value is 0.
5587 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5597 -xram-loc 0x8000 or -
5609 \labelwidthstring 00.00.0000
5626 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5631 <Value> The start location of the code
5632 \begin_inset LatexCommand \index{code}
5636 segment, default value 0.
5637 Note when this option is used the interrupt vector table is also relocated
5638 to the given address.
5639 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5649 -code-loc 0x8000 or -
5661 \labelwidthstring 00.00.0000
5678 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5683 <Value> By default the stack
5684 \begin_inset LatexCommand \index{stack}
5688 is placed after the data segment.
5689 Using this option the stack can be placed anywhere in the internal memory
5691 The value entered can be in Hexadecimal or Decimal format, e.g.
5702 -stack-loc 0x20 or -
5713 Since the sp register is incremented before a push or call, the initial
5714 sp will be set to one byte prior the provided value.
5715 The provided value should not overlap any other memory areas such as used
5716 register banks or the data segment and with enough space for the current
5734 \begin_inset LatexCommand \index{-\/-pack-iram}
5738 option (which is now a default setting) will override this setting, so
5739 you should also specify the
5755 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5759 option if you need to manually place the stack.
5761 \labelwidthstring 00.00.0000
5778 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
5783 <Value> By default the external stack
5784 \begin_inset LatexCommand \index{xstack}
5788 is placed after the pdata segment.
5789 Using this option the xstack can be placed anywhere in the external memory
5791 The value entered can be in Hexadecimal or Decimal format, e.g.
5802 -xstack-loc 0x8000 or -
5813 The provided value should not overlap any other memory areas such as the
5814 pdata or xdata segment and with enough space for the current application.
5816 \labelwidthstring 00.00.0000
5833 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5838 <Value> The start location of the internal ram data
5839 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5844 The value entered can be in Hexadecimal or Decimal format, eg.
5866 (By default, the start location of the internal ram data segment is set
5867 as low as possible in memory, taking into account the used register banks
5868 and the bit segment at address 0x20.
5869 For example if register banks 0 and 1 are used without bit variables, the
5870 data segment will be set, if -
5880 -data-loc is not used, to location 0x10.)
5882 \labelwidthstring 00.00.0000
5899 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
5904 <Value> The start location of the indirectly addressable internal ram
5905 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
5909 of the 8051, default value is 0x80.
5910 The value entered can be in Hexadecimal or Decimal format, eg.
5921 -idata-loc 0x88 or -
5933 \labelwidthstring 00.00.0000
5950 <Value> The start location of the bit
5951 \begin_inset LatexCommand \index{bit}
5955 addressable internal ram of the 8051.
5961 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
5964 \labelwidthstring 00.00.0000
5979 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
5988 The linker output (final object code) is in Intel Hex format.
5989 \begin_inset LatexCommand \index{Intel hex format}
5993 This is the default option.
5994 The format itself is documented in the documentation of srecord
5995 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6001 \labelwidthstring 00.00.0000
6016 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6025 The linker output (final object code) is in Motorola S19 format
6026 \begin_inset LatexCommand \index{Motorola S19 format}
6031 The format itself is documented in the documentation of srecord.
6033 \labelwidthstring 00.00.0000
6048 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6057 The linker output (final object code) is in ELF format
6058 \begin_inset LatexCommand \index{ELF format}
6063 (Currently only supported for the HC08 processors)
6065 \labelwidthstring 00.00.0000
6070 linkOption[,linkOption]
6073 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6078 Pass the linkOption to the linker.
6079 See file sdcc/as/doc/asxhtm.html for more on linker options.
6083 \begin_inset LatexCommand \index{Options MCS51}
6088 \begin_inset LatexCommand \index{MCS51 options}
6094 \labelwidthstring 00.00.0000
6109 \begin_inset LatexCommand \index{-\/-model-small}
6120 Generate code for Small Model programs, see section Memory Models for more
6122 This is the default model.
6124 \labelwidthstring 00.00.0000
6139 \begin_inset LatexCommand \index{-\/-model-medium}
6145 Generate code for Medium model programs, see section Memory Models for
6147 If this option is used all source files in the project have to be compiled
6149 It must also be used when invoking the linker.
6151 \labelwidthstring 00.00.0000
6166 \begin_inset LatexCommand \index{-\/-model-large}
6172 Generate code for Large model programs, see section Memory Models for more
6174 If this option is used all source files in the project have to be compiled
6176 It must also be used when invoking the linker.
6178 \labelwidthstring 00.00.0000
6193 \begin_inset LatexCommand \index{-\/-xstack}
6199 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6200 variables and passing parameters.
6202 \begin_inset LatexCommand \ref{sub:External-Stack}
6207 External Stack for more details.
6209 \labelwidthstring 00.00.0000
6227 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6231 Causes the linker to check if the internal ram usage is within limits of
6234 \labelwidthstring 00.00.0000
6252 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6256 Causes the linker to check if the external ram usage is within limits of
6259 \labelwidthstring 00.00.0000
6277 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6281 Causes the linker to check if the code memory usage is within limits of
6284 \labelwidthstring 00.00.0000
6302 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6306 Causes the linker to check if there is at minimum <Value> bytes for stack.
6308 \labelwidthstring 00.00.0000
6326 \begin_inset LatexCommand \index{-\/-pack-iram}
6330 Causes the linker to use unused register banks for data variables and pack
6331 data, idata and stack together.
6332 This is the default now.
6334 \labelwidthstring 00.00.0000
6352 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6356 Causes the linker to use old style for allocating memory areas.
6359 DS390 / DS400 Options
6360 \begin_inset LatexCommand \index{Options DS390}
6365 \begin_inset LatexCommand \index{DS390 options}
6371 \labelwidthstring 00.00.0000
6388 \begin_inset LatexCommand \index{-\/-model-flat24}
6398 Generate 24-bit flat mode code.
6399 This is the one and only that the ds390 code generator supports right now
6400 and is default when using
6405 See section Memory Models for more details.
6407 \labelwidthstring 00.00.0000
6422 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6428 disable interrupts during ESP:SP updates.
6430 \labelwidthstring 00.00.0000
6447 \begin_inset LatexCommand \index{-\/-stack-10bit}
6451 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6452 This is the one and only that the ds390 code generator supports right now
6453 and is default when using
6458 In this mode, the stack is located in the lower 1K of the internal RAM,
6459 which is mapped to 0x400000.
6460 Note that the support is incomplete, since it still uses a single byte
6461 as the stack pointer.
6462 This means that only the lower 256 bytes of the potential 1K stack space
6463 will actually be used.
6464 However, this does allow you to reclaim the precious 256 bytes of low RAM
6465 for use for the DATA and IDATA segments.
6466 The compiler will not generate any code to put the processor into 10 bit
6468 It is important to ensure that the processor is in this mode before calling
6469 any re-entrant functions compiled with this option.
6470 In principle, this should work with the
6483 \begin_inset LatexCommand \index{-\/-stack-auto}
6489 option, but that has not been tested.
6490 It is incompatible with the
6503 \begin_inset LatexCommand \index{-\/-xstack}
6510 It also only makes sense if the processor is in 24 bit contiguous addressing
6523 -model-flat24 option
6527 \labelwidthstring 00.00.0000
6542 \begin_inset LatexCommand \index{-\/-stack-probe}
6548 insert call to function __stack_probe at each function prologue.
6550 \labelwidthstring 00.00.0000
6565 \begin_inset LatexCommand \index{-\/-tini-libid}
6571 <nnnn> LibraryID used in -mTININative.
6574 \labelwidthstring 00.00.0000
6589 \begin_inset LatexCommand \index{-\/-use-accelerator}
6595 generate code for DS390 Arithmetic Accelerator.
6600 \begin_inset LatexCommand \index{Options Z80}
6605 \begin_inset LatexCommand \index{Z80 options}
6611 \labelwidthstring 00.00.0000
6628 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6638 Force a called function to always save BC.
6640 \labelwidthstring 00.00.0000
6657 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6661 When linking, skip the standard crt0.o object file.
6662 You must provide your own crt0.o for your system when linking.
6666 Optimization Options
6667 \begin_inset LatexCommand \index{Options optimization}
6672 \begin_inset LatexCommand \index{Optimization options}
6678 \labelwidthstring 00.00.0000
6693 \begin_inset LatexCommand \index{-\/-nogcse}
6699 Will not do global subexpression elimination, this option may be used when
6700 the compiler creates undesirably large stack/data spaces to store compiler
6710 \begin_inset LatexCommand \index{sloc (spill location)}
6715 A warning message will be generated when this happens and the compiler
6716 will indicate the number of extra bytes it allocated.
6717 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6719 \begin_inset LatexCommand \index{\#pragma nogcse}
6723 can be used to turn off global subexpression elimination
6724 \begin_inset LatexCommand \index{Subexpression elimination}
6728 for a given function only.
6730 \labelwidthstring 00.00.0000
6745 \begin_inset LatexCommand \index{-\/-noinvariant}
6751 Will not do loop invariant optimizations, this may be turned off for reasons
6752 explained for the previous option.
6753 For more details of loop optimizations performed see Loop Invariants in
6755 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6760 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6762 \begin_inset LatexCommand \index{\#pragma noinvariant}
6766 can be used to turn off invariant optimizations for a given function only.
6768 \labelwidthstring 00.00.0000
6783 \begin_inset LatexCommand \index{-\/-noinduction}
6789 Will not do loop induction optimizations, see section strength reduction
6791 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6793 \begin_inset LatexCommand \index{\#pragma noinduction}
6797 can be used to turn off induction optimizations for a given function only.
6799 \labelwidthstring 00.00.0000
6814 \begin_inset LatexCommand \index{-\/-nojtbound}
6825 Will not generate boundary condition check when switch statements
6826 \begin_inset LatexCommand \index{switch statement}
6830 are implemented using jump-tables.
6832 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6837 Switch Statements for more details.
6838 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6840 \begin_inset LatexCommand \index{\#pragma nojtbound}
6844 can be used to turn off boundary checking for jump tables for a given function
6847 \labelwidthstring 00.00.0000
6862 \begin_inset LatexCommand \index{-\/-noloopreverse}
6871 Will not do loop reversal
6872 \begin_inset LatexCommand \index{Loop reversing}
6878 \labelwidthstring 00.00.0000
6895 \begin_inset LatexCommand \index{-\/-nolabelopt }
6899 Will not optimize labels (makes the dumpfiles more readable).
6901 \labelwidthstring 00.00.0000
6916 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
6922 Will not memcpy initialized data from code space into xdata space.
6923 This saves a few bytes in code space if you don't have initialized data
6924 \begin_inset LatexCommand \index{Variable initialization}
6930 \labelwidthstring 00.00.0000
6945 \begin_inset LatexCommand \index{-\/-nooverlay}
6951 The compiler will not overlay parameters and local variables of any function,
6952 see section Parameters and local variables for more details.
6954 \labelwidthstring 00.00.0000
6969 \begin_inset LatexCommand \index{-\/-no-peep}
6975 Disable peep-hole optimization with built-in rules.
6977 \labelwidthstring 00.00.0000
6994 \begin_inset LatexCommand \index{-\/-peep-file}
6999 <filename> This option can be used to use additional rules to be used by
7000 the peep hole optimizer.
7002 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7007 Peep Hole optimizations for details on how to write these rules.
7009 \labelwidthstring 00.00.0000
7024 \begin_inset LatexCommand \index{-\/-peep-asm}
7030 Pass the inline assembler code through the peep hole optimizer.
7031 This can cause unexpected changes to inline assembler code, please go through
7032 the peephole optimizer
7033 \begin_inset LatexCommand \index{Peephole optimizer}
7037 rules defined in the source file tree '<target>/peeph.def' before using
7040 \labelwidthstring 00.00.0000
7055 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7061 The compiler will optimize code generation towards fast code, possibly
7062 at the expense of code size.
7064 \labelwidthstring 00.00.0000
7079 \begin_inset LatexCommand \index{-\/-opt-code-size}
7085 The compiler will optimize code generation towards compact code, possibly
7086 at the expense of code speed.
7090 \begin_inset LatexCommand \index{Options other}
7096 \labelwidthstring 00.00.0000
7112 \begin_inset LatexCommand \index{-\/-compile-only}
7117 \begin_inset LatexCommand \index{-c -\/-compile-only}
7123 will compile and assemble the source, but will not call the linkage editor.
7125 \labelwidthstring 00.00.0000
7144 \begin_inset LatexCommand \index{-\/-c1mode}
7150 reads the preprocessed source from standard input and compiles it.
7151 The file name for the assembler output must be specified using the -o option.
7153 \labelwidthstring 00.00.0000
7158 \begin_inset LatexCommand \index{-E}
7164 Run only the C preprocessor.
7165 Preprocess all the C source files specified and output the results to standard
7168 \labelwidthstring 00.00.0000
7174 \begin_inset LatexCommand \index{-o <path/file>}
7180 The output path resp.
7181 file where everything will be placed.
7182 If the parameter is a path, it must have a trailing slash (or backslash
7183 for the Windows binaries) to be recognized as a path.
7186 \labelwidthstring 00.00.0000
7201 \begin_inset LatexCommand \index{-\/-stack-auto}
7212 All functions in the source file will be compiled as
7217 \begin_inset LatexCommand \index{reentrant}
7222 the parameters and local variables will be allocated on the stack
7223 \begin_inset LatexCommand \index{stack}
7229 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7233 Parameters and Local Variables for more details.
7234 If this option is used all source files in the project should be compiled
7236 It automatically implies --int-long-reent and --float-reent.
7239 \labelwidthstring 00.00.0000
7254 \begin_inset LatexCommand \index{-\/-callee-saves}
7258 function1[,function2][,function3]....
7261 The compiler by default uses a caller saves convention for register saving
7262 across function calls, however this can cause unnecessary register pushing
7263 & popping when calling small functions from larger functions.
7264 This option can be used to switch the register saving convention for the
7265 function names specified.
7266 The compiler will not save registers when calling these functions, no extra
7267 code will be generated at the entry & exit (function prologue
7270 \begin_inset LatexCommand \index{function prologue}
7279 \begin_inset LatexCommand \index{function epilogue}
7285 ) for these functions to save & restore the registers used by these functions,
7286 this can SUBSTANTIALLY reduce code & improve run time performance of the
7288 In the future the compiler (with inter procedural analysis) will be able
7289 to determine the appropriate scheme to use for each function call.
7290 DO NOT use this option for built-in functions such as _mulint..., if this
7291 option is used for a library function the appropriate library function
7292 needs to be recompiled with the same option.
7293 If the project consists of multiple source files then all the source file
7294 should be compiled with the same -
7304 -callee-saves option string.
7305 Also see #pragma\SpecialChar ~
7307 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7313 \labelwidthstring 00.00.0000
7328 \begin_inset LatexCommand \index{-\/-debug}
7337 When this option is used the compiler will generate debug information.
7338 The debug information collected in a file with .cdb extension can be used
7340 For more information see documentation for SDCDB.
7341 Another file with no extension contains debug information in AOMF or AOMF51
7342 \begin_inset LatexCommand \index{AOMF, AOMF51}
7346 format which is commonly used by third party tools.
7348 \labelwidthstring 00.00.0000
7353 \begin_inset LatexCommand \index{-S}
7364 Stop after the stage of compilation proper; do not assemble.
7365 The output is an assembler code file for the input file specified.
7367 \labelwidthstring 00.00.0000
7382 \begin_inset LatexCommand \index{-\/-int-long-reent}
7388 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7389 Note by default these libraries are compiled as non-reentrant.
7390 See section Installation for more details.
7392 \labelwidthstring 00.00.0000
7407 \begin_inset LatexCommand \index{-\/-cyclomatic}
7416 This option will cause the compiler to generate an information message for
7417 each function in the source file.
7418 The message contains some
7422 information about the function.
7423 The number of edges and nodes the compiler detected in the control flow
7424 graph of the function, and most importantly the
7426 cyclomatic complexity
7427 \begin_inset LatexCommand \index{Cyclomatic complexity}
7433 see section on Cyclomatic Complexity for more details.
7435 \labelwidthstring 00.00.0000
7450 \begin_inset LatexCommand \index{-\/-float-reent}
7456 Floating point library is compiled as reentrant
7457 \begin_inset LatexCommand \index{reentrant}
7462 See section Installation for more details.
7464 \labelwidthstring 00.00.0000
7479 \begin_inset LatexCommand \index{-\/-main-return}
7485 This option can be used if the code generated is called by a monitor program
7486 or if the main routine includes an endless loop.
7487 This option might result in slightly smaller code and save two bytes of
7489 The return from the 'main'
7490 \begin_inset LatexCommand \index{main return}
7494 function will return to the function calling main.
7495 The default setting is to lock up i.e.
7502 \labelwidthstring 00.00.0000
7517 \begin_inset LatexCommand \index{-\/-nostdinc}
7523 This will prevent the compiler from passing on the default include path
7524 to the preprocessor.
7526 \labelwidthstring 00.00.0000
7541 \begin_inset LatexCommand \index{-\/-nostdlib}
7547 This will prevent the compiler from passing on the default library
7548 \begin_inset LatexCommand \index{Libraries}
7554 \labelwidthstring 00.00.0000
7569 \begin_inset LatexCommand \index{-\/-verbose}
7575 Shows the various actions the compiler is performing.
7577 \labelwidthstring 00.00.0000
7582 \begin_inset LatexCommand \index{-V}
7588 Shows the actual commands the compiler is executing.
7590 \labelwidthstring 00.00.0000
7605 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7611 Hides your ugly and inefficient c-code from the asm file, so you can always
7612 blame the compiler :)
7614 \labelwidthstring 00.00.0000
7629 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7635 Will not include peep-hole comments in the generated files.
7637 \labelwidthstring 00.00.0000
7652 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7658 Include i-codes in the asm file.
7659 Sounds like noise but is most helpful for debugging the compiler itself.
7661 \labelwidthstring 00.00.0000
7676 \begin_inset LatexCommand \index{-\/-less-pedantic}
7682 Disable some of the more pedantic warnings
7683 \begin_inset LatexCommand \index{Warnings}
7687 (jwk burps: please be more specific here, please!).
7689 \labelwidthstring 00.00.0000
7703 -disable-warning\SpecialChar ~
7705 \begin_inset LatexCommand \index{-\/-disable-warning}
7711 Disable specific warning with number <nnnn>.
7713 \labelwidthstring 00.00.0000
7728 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7734 Display the directories in the compiler's search path
7736 \labelwidthstring 00.00.0000
7751 \begin_inset LatexCommand \index{-\/-vc}
7757 Display errors and warnings using MSVC style, so you can use SDCC with
7760 \labelwidthstring 00.00.0000
7775 \begin_inset LatexCommand \index{-\/-use-stdout}
7781 Send errors and warnings to stdout instead of stderr.
7783 \labelwidthstring 00.00.0000
7788 asmOption[,asmOption]
7791 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7796 Pass the asmOption to the assembler
7797 \begin_inset LatexCommand \index{Options assembler}
7802 \begin_inset LatexCommand \index{Assembler options}
7807 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7809 \labelwidthstring 00.00.0000
7824 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7830 Generally follow the C89 standard, but allow SDCC features that conflict
7831 with the standard (default).
7833 \labelwidthstring 00.00.0000
7848 \begin_inset LatexCommand \index{-\/-std-c89}
7854 Follow the C89 standard and disable SDCC features that conflict with the
7857 \labelwidthstring 00.00.0000
7872 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7878 Generally follow the C99 standard, but allow SDCC features that conflict
7879 with the standard (incomplete support).
7881 \labelwidthstring 00.00.0000
7896 \begin_inset LatexCommand \index{-\/-std-sdcc99}
7902 Follow the C99 standard and disable SDCC features that conflict with the
7903 standard (incomplete support).
7905 \labelwidthstring 00.00.0000
7922 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
7927 <Name> The name to be used for the code
7928 \begin_inset LatexCommand \index{code}
7932 segment, default CSEG.
7933 This is useful if you need to tell the compiler to put the code in a special
7934 segment so you can later on tell the linker to put this segment in a special
7936 Can be used for instance when using bank switching to put the code in a
7939 \labelwidthstring 00.00.0000
7956 \begin_inset LatexCommand \index{-\/-constseg <Value>}
7961 <Name> The name to be used for the const
7962 \begin_inset LatexCommand \index{code}
7966 segment, default CONST.
7967 This is useful if you need to tell the compiler to put the const data in
7968 a special segment so you can later on tell the linker to put this segment
7969 in a special place in memory.
7970 Can be used for instance when using bank switching to put the const data
7973 \labelwidthstring 00.00.0000
7985 a SDCC compiler option but if you want
7989 warnings you can use a separate tool dedicated to syntax checking like
7991 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
7996 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8001 \begin_inset LatexCommand \url{http://www.splint.org}
8006 To make your source files parseable by splint you will have to include
8012 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8016 in your source file and add brackets around extended keywords (like
8019 \begin_inset Quotes sld
8032 \begin_inset Quotes srd
8040 \begin_inset Quotes sld
8043 __interrupt\SpecialChar ~
8045 \begin_inset Quotes srd
8053 Splint has an excellent on line manual at
8054 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8058 and it's capabilities go beyond pure syntax checking.
8059 You'll need to tell splint the location of SDCC's include files so a typical
8060 command line could look like this:
8064 splint\SpecialChar ~
8066 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8071 Intermediate Dump Options
8072 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8077 \begin_inset LatexCommand \index{Options intermediate dump}
8082 \begin_inset LatexCommand \index{Intermediate dump options}
8089 The following options are provided for the purpose of retargetting and debugging
8091 They provide a means to dump the intermediate code (iCode
8092 \begin_inset LatexCommand \index{iCode}
8096 ) generated by the compiler in human readable form at various stages of
8097 the compilation process.
8098 More on iCodes see chapter
8099 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8104 \begin_inset Quotes srd
8107 The anatomy of the compiler
8108 \begin_inset Quotes srd
8113 \labelwidthstring 00.00.0000
8128 \begin_inset LatexCommand \index{-\/-dumpraw}
8134 This option will cause the compiler to dump the intermediate code into
8137 <source filename>.dumpraw
8139 just after the intermediate code has been generated for a function, i.e.
8140 before any optimizations are done.
8142 \begin_inset LatexCommand \index{Basic blocks}
8146 at this stage ordered in the depth first number, so they may not be in
8147 sequence of execution.
8149 \labelwidthstring 00.00.0000
8164 \begin_inset LatexCommand \index{-\/-dumpgcse}
8170 Will create a dump of iCode's, after global subexpression elimination
8171 \begin_inset LatexCommand \index{Global subexpression elimination}
8177 <source filename>.dumpgcse.
8179 \labelwidthstring 00.00.0000
8194 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8200 Will create a dump of iCode's, after deadcode elimination
8201 \begin_inset LatexCommand \index{Dead-code elimination}
8207 <source filename>.dumpdeadcode.
8209 \labelwidthstring 00.00.0000
8224 \begin_inset LatexCommand \index{-\/-dumploop}
8233 Will create a dump of iCode's, after loop optimizations
8234 \begin_inset LatexCommand \index{Loop optimization}
8240 <source filename>.dumploop.
8242 \labelwidthstring 00.00.0000
8257 \begin_inset LatexCommand \index{-\/-dumprange}
8266 Will create a dump of iCode's, after live range analysis
8267 \begin_inset LatexCommand \index{Live range analysis}
8273 <source filename>.dumprange.
8275 \labelwidthstring 00.00.0000
8290 \begin_inset LatexCommand \index{-\/-dumlrange}
8296 Will dump the life ranges
8297 \begin_inset LatexCommand \index{Live range analysis}
8303 \labelwidthstring 00.00.0000
8318 \begin_inset LatexCommand \index{-\/-dumpregassign}
8327 Will create a dump of iCode's, after register assignment
8328 \begin_inset LatexCommand \index{Register assignment}
8334 <source filename>.dumprassgn.
8336 \labelwidthstring 00.00.0000
8351 \begin_inset LatexCommand \index{-\/-dumplrange}
8357 Will create a dump of the live ranges of iTemp's
8359 \labelwidthstring 00.00.0000
8374 \begin_inset LatexCommand \index{-\/-dumpall}
8385 Will cause all the above mentioned dumps to be created.
8388 Redirecting output on Windows Shells
8391 By default SDCC writes it's error messages to
8392 \begin_inset Quotes sld
8396 \begin_inset Quotes srd
8400 To force all messages to
8401 \begin_inset Quotes sld
8405 \begin_inset Quotes srd
8429 \begin_inset LatexCommand \index{-\/-use-stdout}
8434 Additionally, if you happen to have visual studio installed in your windows
8435 machine, you can use it to compile your sources using a custom build and
8451 \begin_inset LatexCommand \index{-\/-vc}
8456 Something like this should work:
8500 -model-large -c $(InputPath)
8503 Environment variables
8504 \begin_inset LatexCommand \index{Environment variables}
8511 SDCC recognizes the following environment variables:
8513 \labelwidthstring 00.00.0000
8518 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8524 SDCC installs a signal handler
8525 \begin_inset LatexCommand \index{signal handler}
8529 to be able to delete temporary files after an user break (^C) or an exception.
8530 If this environment variable is set, SDCC won't install the signal handler
8531 in order to be able to debug SDCC.
8533 \labelwidthstring 00.00.0000
8540 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8546 Path, where temporary files will be created.
8547 The order of the variables is the search order.
8548 In a standard *nix environment these variables are not set, and there's
8549 no need to set them.
8550 On Windows it's recommended to set one of them.
8552 \labelwidthstring 00.00.0000
8557 \begin_inset LatexCommand \index{SDCC\_HOME}
8564 \begin_inset LatexCommand \ref{sub:Install-paths}
8570 \begin_inset Quotes sld
8574 \begin_inset Quotes srd
8579 \labelwidthstring 00.00.0000
8584 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8591 \begin_inset LatexCommand \ref{sub:Search-Paths}
8597 \begin_inset Quotes sld
8601 \begin_inset Quotes srd
8606 \labelwidthstring 00.00.0000
8611 \begin_inset LatexCommand \index{SDCC\_LIB}
8618 \begin_inset LatexCommand \ref{sub:Search-Paths}
8624 \begin_inset Quotes sld
8628 \begin_inset Quotes srd
8634 There are some more environment variables recognized by SDCC, but these
8635 are solely used for debugging purposes.
8636 They can change or disappear very quickly, and will never be documented.
8639 Storage Class Language Extensions
8642 MCS51/DS390 Storage Class
8643 \begin_inset LatexCommand \index{Storage class}
8650 In addition to the ANSI storage classes SDCC allows the following MCS51
8651 specific storage classes:
8652 \layout Subsubsection
8655 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8660 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8665 \begin_inset LatexCommand \index{near (storage class)}
8670 \begin_inset LatexCommand \index{\_\_near (storage class)}
8681 storage class for the Small Memory model (
8689 can be used synonymously).
8690 Variables declared with this storage class will be allocated in the directly
8691 addressable portion of the internal RAM of a 8051, e.g.:
8696 data unsigned char test_data;
8699 Writing 0x01 to this variable generates the assembly code:
8704 75*00 01\SpecialChar ~
8710 \layout Subsubsection
8713 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8718 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8723 \begin_inset LatexCommand \index{far (storage class)}
8728 \begin_inset LatexCommand \index{\_\_far (storage class)}
8735 Variables declared with this storage class will be placed in the external
8741 storage class for the Large Memory model, e.g.:
8746 xdata unsigned char test_xdata;
8749 Writing 0x01 to this variable generates the assembly code:
8754 90s00r00\SpecialChar ~
8783 \layout Subsubsection
8786 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8791 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8798 Variables declared with this storage class will be allocated into the indirectly
8799 addressable portion of the internal ram of a 8051, e.g.:
8804 idata unsigned char test_idata;
8807 Writing 0x01 to this variable generates the assembly code:
8836 Please note, the first 128 byte of idata physically access the same RAM
8838 The original 8051 had 128 byte idata memory, nowadays most devices have
8839 256 byte idata memory.
8841 \begin_inset LatexCommand \index{stack}
8845 is located in idata memory.
8846 \layout Subsubsection
8849 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8854 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8861 Paged xdata access is just as straightforward as using the other addressing
8863 It is typically located at the start of xdata and has a maximum size of
8865 The following example writes 0x01 to the pdata variable.
8866 Please note, pdata access physically accesses xdata memory.
8867 The high byte of the address is determined by port P2
8868 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
8872 (or in case of some 8051 variants by a separate Special Function Register,
8874 \begin_inset LatexCommand \ref{sub:MCS51-variants}
8883 storage class for the Medium Memory model, e.g.:
8888 pdata unsigned char test_pdata;
8891 Writing 0x01 to this variable generates the assembly code:
8935 \begin_inset LatexCommand \index{-\/-xstack}
8939 option is used the pdata memory area is followed by the xstack memory area
8940 and the sum of their sizes is limited to 256 bytes.
8941 \layout Subsubsection
8944 \begin_inset LatexCommand \index{code}
8949 \begin_inset LatexCommand \index{\_\_code}
8956 'Variables' declared with this storage class will be placed in the code
8962 code unsigned char test_code;
8965 Read access to this variable generates the assembly code:
8970 90s00r6F\SpecialChar ~
8973 mov dptr,#_test_code
9002 indexed arrays of characters in code memory can be accessed efficiently:
9007 code char test_array[] = {'c','h','e','a','p'};
9010 Read access to this array using an 8-bit unsigned index generates the assembly
9027 90s00r41\SpecialChar ~
9030 mov dptr,#_test_array
9045 \layout Subsubsection
9048 \begin_inset LatexCommand \index{bit}
9053 \begin_inset LatexCommand \index{\_\_bit}
9060 This is a data-type and a storage class specifier.
9061 When a variable is declared as a bit, it is allocated into the bit addressable
9062 memory of 8051, e.g.:
9070 Writing 1 to this variable generates the assembly code:
9086 The bit addressable memory consists of 128 bits which are located from 0x20
9087 to 0x2f in data memory.
9090 Apart from this 8051 specific storage class most architectures support ANSI-C
9092 \begin_inset LatexCommand \index{bitfields}
9102 Not really meant as examples, but nevertheless showing what bitfields are
9103 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9107 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9108 signed modifier are implemented as unsigned.
9109 \layout Subsubsection
9112 \begin_inset LatexCommand \index{sfr}
9117 \begin_inset LatexCommand \index{\_\_sfr}
9122 \begin_inset LatexCommand \index{sfr16}
9127 \begin_inset LatexCommand \index{\_\_sfr16}
9132 \begin_inset LatexCommand \index{sfr32}
9137 \begin_inset LatexCommand \index{\_\_sfr32}
9142 \begin_inset LatexCommand \index{\_\_sbit}
9149 Like the bit keyword,
9151 sfr / sfr16 / sfr32 / sbit
9153 signify both a data-type and storage class, they are used to describe the
9174 variables of a 8051, eg:
9180 \begin_inset LatexCommand \index{at}
9185 \begin_inset LatexCommand \index{\_\_at}
9189 0x80 P0;\SpecialChar ~
9190 /* special function register P0 at location 0x80 */
9192 /* 16 bit special function register combination for timer 0 */
9194 /* with the high byte at location 0x8C and the low byte at location 0x8A
9198 \begin_inset LatexCommand \index{at}
9203 \begin_inset LatexCommand \index{\_\_at}
9209 sbit at 0xd7 CY; /* CY (Carry Flag
9210 \begin_inset LatexCommand \index{Flags}
9215 \begin_inset LatexCommand \index{Carry flag}
9222 Special function registers which are located on an address dividable by
9223 8 are bit-addressable, an
9227 addresses a specific bit within these sfr.
9229 16 Bit and 32 bit special function register combinations which require a
9230 certain access order are better not declared using
9239 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9240 this is not guaranteed.
9241 \layout Subsubsection
9244 \begin_inset LatexCommand \index{Pointer}
9248 to MCS51/DS390 specific memory spaces
9251 SDCC allows (via language extensions) pointers to explicitly point to any
9252 of the memory spaces
9253 \begin_inset LatexCommand \index{Memory model}
9258 In addition to the explicit pointers, the compiler uses (by default) generic
9259 pointers which can be used to point to any of the memory spaces.
9263 Pointer declaration examples:
9268 /* pointer physically in internal ram pointing to object in external ram
9271 xdata unsigned char * data p;
9275 /* pointer physically in external ram pointing to object in internal ram
9278 data unsigned char * xdata p;
9282 /* pointer physically in code rom pointing to data in xdata space */
9284 xdata unsigned char * code p;
9288 /* pointer physically in code space pointing to data in code space */
9290 code unsigned char * code p;
9294 /* the following is a generic pointer physically located in xdata space
9301 /* the following is a function pointer physically located in data space
9304 char (* data fp)(void);
9307 Well you get the idea.
9312 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9325 The highest order byte of the
9329 pointers contains the data space information.
9330 Assembler support routines are called whenever data is stored or retrieved
9336 These are useful for developing reusable library
9337 \begin_inset LatexCommand \index{Libraries}
9342 Explicitly specifying the pointer type will generate the most efficient
9344 \layout Subsubsection
9346 Notes on MCS51 memory
9347 \begin_inset LatexCommand \index{MCS51 memory}
9354 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9355 RAM memory which is structured as follows:
9359 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9362 - Bytes 20-2F - 16 bytes to hold 128 bit
9363 \begin_inset LatexCommand \index{bit}
9369 - Bytes 30-7F - 80 bytes for general purpose use.
9374 Additionally some members of the MCS51 family may have up to 128 bytes of
9375 additional, indirectly addressable, internal RAM memory (
9380 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9385 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9390 Furthermore, some chips may have some built in external memory (
9395 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9400 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9404 ) which should not be confused with the internal, directly addressable RAM
9410 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9415 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9420 Sometimes this built in
9424 memory has to be activated before using it (you can probably find this
9425 information on the datasheet of the microcontroller your are using, see
9427 \begin_inset LatexCommand \ref{sub:Startup-Code}
9435 Normally SDCC will only use the first bank
9436 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9440 of registers (register bank 0), but it is possible to specify that other
9441 banks of registers (keyword
9448 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9453 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9459 ) should be used in interrupt
9460 \begin_inset LatexCommand \index{interrupt}
9465 \begin_inset LatexCommand \index{\_\_interrupt}
9470 By default, the compiler will place the stack after the last byte of allocated
9471 memory for variables.
9472 For example, if the first 2 banks of registers are used, and only four
9477 variables, it will position the base of the internal stack at address 20
9479 This implies that as the stack
9480 \begin_inset LatexCommand \index{stack}
9484 grows, it will use up the remaining register banks, and the 16 bytes used
9485 by the 128 bit variables, and 80 bytes for general purpose use.
9486 If any bit variables are used, the data variables will be placed in unused
9487 register banks and after the byte holding the last bit variable.
9488 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9493 variables will be placed starting from address 0x10 to 0x20 and continue
9506 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9510 to specify the start address of the
9525 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9529 to specify the size of the total internal RAM (
9541 By default the 8051 linker will place the stack after the last byte of (i)data
9554 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9558 allows you to specify the start of the stack, i.e.
9559 you could start it after any data in the general purpose area.
9560 If your microcontroller has additional indirectly addressable internal
9565 ) you can place the stack on it.
9566 You may also need to use -
9577 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9581 to set the start address of the external RAM (
9596 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9600 to specify its size.
9601 Same goes for the code memory, using -
9612 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9627 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9632 If in doubt, don't specify any options and see if the resulting memory
9633 layout is appropriate, then you can adjust it.
9636 The linker generates two files with memory allocation information.
9637 The first, with extension .map
9638 \begin_inset LatexCommand \index{<file>.map}
9642 shows all the variables and segments.
9643 The second with extension .mem
9644 \begin_inset LatexCommand \index{<file>.mem}
9648 shows the final memory layout.
9649 The linker will complain either if memory segments overlap, there is not
9650 enough memory, or there is not enough space for stack.
9651 If you get any linking warnings and/or errors related to stack or segments
9652 allocation, take a look at either the .map or .mem files to find out what
9654 The .mem file may even suggest a solution to the problem.
9657 Z80/Z180 Storage Class
9658 \begin_inset LatexCommand \index{Storage class}
9663 \layout Subsubsection
9666 \begin_inset LatexCommand \index{sfr}
9671 \begin_inset LatexCommand \index{\_\_sfr}
9675 (in/out to 8-bit addresses)
9679 \begin_inset LatexCommand \index{Z80}
9683 family has separate address spaces for memory and
9693 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9697 is accessed with special instructions, e.g.:
9702 sfr at 0x78 IoPort;\SpecialChar ~
9704 /* define a var in I/O space at 78h called IoPort */
9708 Writing 0x01 to this variable generates the assembly code:
9728 \layout Subsubsection
9731 \begin_inset LatexCommand \index{sfr}
9736 \begin_inset LatexCommand \index{\_\_sfr}
9740 (in/out to 16-bit addresses)
9747 is used to support 16 bit addresses in I/O memory e.g.:
9753 \begin_inset LatexCommand \index{at}
9758 \begin_inset LatexCommand \index{\_\_at}
9765 Writing 0x01 to this variable generates the assembly code:
9770 01 23 01\SpecialChar ~
9790 \layout Subsubsection
9793 \begin_inset LatexCommand \index{sfr}
9798 \begin_inset LatexCommand \index{\_\_sfr}
9802 (in0/out0 to 8 bit addresses on Z180
9803 \begin_inset LatexCommand \index{Z180}
9808 \begin_inset LatexCommand \index{HD64180}
9815 The compiler option -
9825 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9827 \begin_inset LatexCommand \index{\#pragma portmode}
9831 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9841 If you include the file z180.h this will be set automatically.
9845 \begin_inset LatexCommand \index{Storage class}
9850 \layout Subsubsection
9853 \begin_inset LatexCommand \index{data (hc08 storage class)}
9858 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
9865 The data storage class declares a variable that resides in the first 256
9866 bytes of memory (the direct page).
9867 The HC08 is most efficient at accessing variables (especially pointers)
9869 \layout Subsubsection
9872 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
9877 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
9884 The xdata storage class declares a variable that can reside anywhere in
9886 This is the default if no storage class is specified.
9891 \begin_inset LatexCommand \index{Absolute addressing}
9898 Data items can be assigned an absolute address with the
9901 \begin_inset LatexCommand \index{at}
9906 \begin_inset LatexCommand \index{\_\_at}
9912 keyword, in addition to a storage class, e.g.:
9918 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9923 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9928 \begin_inset LatexCommand \index{at}
9933 \begin_inset LatexCommand \index{\_\_at}
9937 0x7ffe unsigned int chksum;
9940 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
9941 of the external ram.
9946 reserve any space for variables declared in this way (they are implemented
9947 with an equate in the assembler).
9948 Thus it is left to the programmer to make sure there are no overlaps with
9949 other variables that are declared without the absolute address.
9950 The assembler listing file (.lst
9951 \begin_inset LatexCommand \index{<file>.lst}
9955 ) and the linker output files (.rst
9956 \begin_inset LatexCommand \index{<file>.rst}
9961 \begin_inset LatexCommand \index{<file>.map}
9965 ) are good places to look for such overlaps.
9966 Variables with an absolute address are
9971 \begin_inset LatexCommand \index{Variable initialization}
9978 In case of memory mapped I/O devices the keyword
9982 has to be used to tell the compiler that accesses might not be removed:
9988 \begin_inset LatexCommand \index{volatile}
9993 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9998 \begin_inset LatexCommand \index{at}
10002 0x8000 unsigned char PORTA_8255;
10005 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10010 \begin_inset LatexCommand \index{Aligned array}
10017 starts at a block (256 byte) boundary
10018 \begin_inset LatexCommand \index{block boundary}
10023 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10029 Absolute addresses can be specified for variables in all storage classes,
10036 \begin_inset LatexCommand \index{bit}
10041 \begin_inset LatexCommand \index{at}
10048 The above example will allocate the variable at offset 0x02 in the bit-addressab
10050 There is no real advantage to assigning absolute addresses to variables
10051 in this manner, unless you want strict control over all the variables allocated.
10052 One possible use would be to write hardware portable code.
10053 For example, if you have a routine that uses one or more of the microcontroller
10054 I/O pins, and such pins are different for two different hardwares, you
10055 can declare the I/O pins in your routine using:
10061 \begin_inset LatexCommand \index{volatile}
10065 bit MOSI;\SpecialChar ~
10069 /* master out, slave in */
10071 extern volatile bit MISO;\SpecialChar ~
10075 /* master in, slave out */
10077 extern volatile bit MCLK;\SpecialChar ~
10085 /* Input and Output of a byte on a 3-wire serial bus.
10090 If needed adapt polarity of clock, polarity of data and bit order
10095 unsigned char spi_io(unsigned char out_byte)
10119 MOSI = out_byte & 0x80;
10149 /* _asm nop _endasm; */\SpecialChar ~
10157 /* for slow peripherals */
10208 Then, someplace in the code for the first hardware you would use
10214 \begin_inset LatexCommand \index{at}
10219 \begin_inset LatexCommand \index{\_\_at}
10223 0x80 MOSI;\SpecialChar ~
10227 /* I/O port 0, bit 0 */
10229 bit at 0x81 MISO;\SpecialChar ~
10233 /* I/O port 0, bit 1 */
10235 bit at 0x82 MCLK;\SpecialChar ~
10239 /* I/O port 0, bit 2 */
10242 Similarly, for the second hardware you would use
10247 bit at 0x83 MOSI;\SpecialChar ~
10251 /* I/O port 0, bit 3 */
10253 bit at 0x91 MISO;\SpecialChar ~
10257 /* I/O port 1, bit 1 */
10260 \begin_inset LatexCommand \index{bit}
10264 at 0x92 MCLK;\SpecialChar ~
10268 /* I/O port 1, bit 2 */
10271 and you can use the same hardware dependent routine without changes, as
10272 for example in a library.
10273 This is somehow similar to sbit, but only one absolute address has to be
10274 specified in the whole project.
10278 \begin_inset LatexCommand \index{Parameters}
10283 \begin_inset LatexCommand \index{function parameter}
10288 \begin_inset LatexCommand \index{local variables}
10293 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10300 Automatic (local) variables and parameters to functions can either be placed
10301 on the stack or in data-space.
10302 The default action of the compiler is to place these variables in the internal
10303 RAM (for small model) or external RAM (for large model).
10304 This in fact makes them similar to
10307 \begin_inset LatexCommand \index{static}
10313 so by default functions are non-reentrant
10314 \begin_inset LatexCommand \index{reentrant}
10323 They can be placed on the stack
10324 \begin_inset LatexCommand \index{stack}
10341 \begin_inset LatexCommand \index{-\/-stack-auto}
10349 #pragma\SpecialChar ~
10353 \begin_inset LatexCommand \index{\#pragma stackauto}
10360 \begin_inset LatexCommand \index{reentrant}
10366 keyword in the function declaration, e.g.:
10371 unsigned char foo(char i) reentrant
10385 Since stack space on 8051 is limited, the
10403 option should be used sparingly.
10404 Note that the reentrant keyword just means that the parameters & local
10405 variables will be allocated to the stack, it
10409 mean that the function is register bank
10410 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10419 \begin_inset LatexCommand \index{local variables}
10423 can be assigned storage classes and absolute
10424 \begin_inset LatexCommand \index{Absolute addressing}
10433 unsigned char foo()
10441 xdata unsigned char i;
10454 \begin_inset LatexCommand \index{at}
10458 0x31 unsigned char j;
10470 In the above example the variable
10474 will be allocated in the external ram,
10478 in bit addressable space and
10497 or when a function is declared as
10501 this should only be done for static variables.
10505 \begin_inset LatexCommand \index{function parameter}
10509 however are not allowed any storage class
10510 \begin_inset LatexCommand \index{Storage class}
10514 , (storage classes for parameters will be ignored), their allocation is
10515 governed by the memory model in use, and the reentrancy options.
10518 It is however allowed to use bit parameters in reentrant functions and also
10519 non-static local bit variables are supported.
10520 Efficient use is limited to 8 semi-bitregisters in bit space.
10521 They are pushed and popped to stack as a single byte just like the normal
10526 \begin_inset LatexCommand \label{sub:Overlaying}
10531 \begin_inset LatexCommand \index{Overlaying}
10539 \begin_inset LatexCommand \index{reentrant}
10543 functions SDCC will try to reduce internal ram space usage by overlaying
10544 parameters and local variables of a function (if possible).
10545 Parameters and local variables
10546 \begin_inset LatexCommand \index{local variables}
10550 of a function will be allocated to an overlayable segment if the function
10553 no other function calls and the function is non-reentrant and the memory
10555 \begin_inset LatexCommand \index{Memory model}
10562 If an explicit storage class
10563 \begin_inset LatexCommand \index{Storage class}
10567 is specified for a local variable, it will NOT be overlayed.
10570 Note that the compiler (not the linkage editor) makes the decision for overlayin
10572 Functions that are called from an interrupt service routine should be preceded
10573 by a #pragma\SpecialChar ~
10575 \begin_inset LatexCommand \index{\#pragma nooverlay}
10579 if they are not reentrant.
10582 Also note that the compiler does not do any processing of inline assembler
10583 code, so the compiler might incorrectly assign local variables and parameters
10584 of a function into the overlay segment if the inline assembler code calls
10585 other c-functions that might use the overlay.
10586 In that case the #pragma\SpecialChar ~
10587 nooverlay should be used.
10590 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10592 \begin_inset LatexCommand \index{Multiplication}
10597 \begin_inset LatexCommand \index{Division}
10601 will NOT be overlayed since these are implemented using external functions,
10610 \begin_inset LatexCommand \index{\#pragma nooverlay}
10616 void set_error(unsigned char errcd)
10632 void some_isr () interrupt
10633 \begin_inset LatexCommand \index{interrupt}
10663 In the above example the parameter
10671 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10673 not present, this could cause unpredictable runtime behavior when called
10674 from an interrupt service routine.
10675 The #pragma\SpecialChar ~
10676 nooverlay ensures that the parameters and local variables for
10677 the function are NOT overlayed.
10680 Interrupt Service Routines
10681 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10688 General Information
10703 outines to be coded in C, with some extended keywords.
10708 void timer_isr (void) interrupt 1 using 1
10722 The optional number following the
10725 \begin_inset LatexCommand \index{interrupt}
10730 \begin_inset LatexCommand \index{\_\_interrupt}
10736 keyword is the interrupt number this routine will service.
10737 When present, the compiler will insert a call to this routine in the interrupt
10738 vector table for the interrupt number specified.
10739 If you have multiple source files in your project, interrupt service routines
10740 can be present in any of them, but a prototype of the isr MUST be present
10741 or included in the file that contains the function
10749 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10754 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10760 keyword can be used to tell the compiler to use the specified register
10761 bank (8051 specific) when generating code for this function.
10767 Interrupt service routines open the door for some very interesting bugs:
10769 If an interrupt service routine changes variables which are accessed by
10770 other functions these variables have to be declared
10775 \begin_inset LatexCommand \index{volatile}
10783 If the access to these variables is not
10786 \begin_inset LatexCommand \index{atomic}
10793 the processor needs more than one instruction for the access and could
10794 be interrupted while accessing the variable) the interrupt must be disabled
10795 during the access to avoid inconsistent data.
10796 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10797 and should be protected by disabling interrupts.
10798 You're not automatically on the safe side if you use 8 bit variables though.
10799 We need an example here: f.e.
10800 on the 8051 the harmless looking
10801 \begin_inset Quotes srd
10806 flags\SpecialChar ~
10811 \begin_inset Quotes sld
10820 \begin_inset Quotes srd
10825 flags\SpecialChar ~
10830 \begin_inset Quotes sld
10833 from within an interrupt routine might get lost if the interrupt occurs
10836 \begin_inset Quotes sld
10841 counter\SpecialChar ~
10846 \begin_inset Quotes srd
10849 is not atomic on the 8051 even if
10853 is located in data memory.
10854 Bugs like these are hard to reproduce and can cause a lot of trouble.
10858 The return address and the registers used in the interrupt service routine
10859 are saved on the stack
10860 \begin_inset LatexCommand \index{stack}
10864 so there must be sufficient stack space.
10865 If there isn't variables or registers (or even the return address itself)
10872 \begin_inset LatexCommand \index{stack overflow}
10876 is most likely to happen if the interrupt occurs during the
10877 \begin_inset Quotes sld
10881 \begin_inset Quotes srd
10884 subroutine when the stack is already in use for f.e.
10885 many return addresses.
10888 A special note here, int (16 bit) and long (32 bit) integer division
10889 \begin_inset LatexCommand \index{Division}
10894 \begin_inset LatexCommand \index{Multiplication}
10899 \begin_inset LatexCommand \index{Modulus}
10904 \begin_inset LatexCommand \index{Floating point support}
10908 operations are implemented using external support routines developed in
10910 If an interrupt service routine needs to do any of these operations then
10911 the support routines (as mentioned in a following section) will have to
10912 be recompiled using the
10925 \begin_inset LatexCommand \index{-\/-stack-auto}
10931 option and the source file will need to be compiled using the
10946 \begin_inset LatexCommand \index{-\/-int-long-reent}
10953 Calling other functions from an interrupt service routine is not recommended,
10954 avoid it if possible.
10955 Note that when some function is called from an interrupt service routine
10956 it should be preceded by a #pragma\SpecialChar ~
10958 \begin_inset LatexCommand \index{\#pragma nooverlay}
10962 if it is not reentrant.
10963 Furthermore nonreentrant functions should not be called from the main program
10964 while the interrupt service routine might be active.
10965 They also must not be called from low priority interrupt service routines
10966 while a high priority interrupt service routine might be active.
10967 You could use semaphores or make the function
10971 if all parameters are passed in registers.
10976 \begin_inset LatexCommand \ref{sub:Overlaying}
10981 about Overlaying and section
10982 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
10987 about Functions using private register banks.
10990 MCS51/DS390 Interrupt Service Routines
10993 Interrupt numbers and the corresponding address & descriptions for the Standard
10994 8051/8052 are listed below.
10995 SDCC will automatically adjust the interrupt vector table to the maximum
10996 interrupt number specified.
11002 \begin_inset Tabular
11003 <lyxtabular version="3" rows="7" columns="3">
11005 <column alignment="center" valignment="top" leftline="true" width="0in">
11006 <column alignment="center" valignment="top" leftline="true" width="0in">
11007 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
11008 <row topline="true" bottomline="true">
11009 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11017 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11025 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11034 <row topline="true">
11035 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11043 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11051 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11060 <row topline="true">
11061 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11069 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11077 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11086 <row topline="true">
11087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11095 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11112 <row topline="true">
11113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11121 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11129 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11138 <row topline="true">
11139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11164 <row topline="true" bottomline="true">
11165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11181 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11199 If the interrupt service routine is defined without
11202 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11207 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11213 a register bank or with register bank 0 (
11217 0), the compiler will save the registers used by itself on the stack upon
11218 entry and restore them at exit, however if such an interrupt service routine
11219 calls another function then the entire register bank will be saved on the
11221 This scheme may be advantageous for small interrupt service routines which
11222 have low register usage.
11225 If the interrupt service routine is defined to be using a specific register
11230 & psw are saved and restored, if such an interrupt service routine calls
11231 another function (using another register bank) then the entire register
11232 bank of the called function will be saved on the stack.
11233 This scheme is recommended for larger interrupt service routines.
11236 HC08 Interrupt Service Routines
11239 Since the number of interrupts available is chip specific and the interrupt
11240 vector table always ends at the last byte of memory, the interrupt numbers
11241 corresponds to the interrupt vectors in reverse order of address.
11242 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11243 2 will use the interrupt vector at 0xfffa, and so on.
11244 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11245 this way; instead see section
11246 \begin_inset LatexCommand \ref{sub:Startup-Code}
11250 for details on customizing startup.
11253 Z80 Interrupt Service Routines
11256 The Z80 uses several different methods for determining the correct interrupt
11257 vector depending on the hardware implementation.
11258 Therefore, SDCC ignores the optional interrupt number and does not attempt
11259 to generate an interrupt vector table.
11262 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11263 instruction to return from the interrupt.
11264 To write an interrupt handler for the non-maskable interrupt, which needs
11265 a RETN instruction instead, add the
11274 void nmi_isr (void) critical interrupt
11288 However if you need to create a non-interruptable interrupt service routine
11289 you would also require the
11294 To distinguish between this and an nmi_isr you must provide an interrupt
11298 Enabling and Disabling Interrupts
11301 Critical Functions and Critical Statements
11304 A special keyword may be associated with a block or a function declaring
11310 SDCC will generate code to disable all interrupts
11311 \begin_inset LatexCommand \index{interrupt}
11315 upon entry to a critical function and restore the interrupt enable to the
11316 previous state before returning.
11317 Nesting critical functions will need one additional byte on the stack
11318 \begin_inset LatexCommand \index{stack}
11327 int foo () critical
11328 \begin_inset LatexCommand \index{critical}
11333 \begin_inset LatexCommand \index{\_\_critical}
11358 The critical attribute maybe used with other attributes like
11368 may also be used to disable interrupts more locally:
11376 More than one statement could have been included in the block.
11379 Enabling and Disabling Interrupts directly
11383 \begin_inset LatexCommand \index{interrupt}
11387 can also be disabled and enabled directly (8051):
11392 EA = 0;\SpecialChar ~
11455 EA = 1;\SpecialChar ~
11522 On other architectures which have seperate opcodes for enabling and disabling
11523 interrupts you might want to make use of defines with inline assembly
11524 \begin_inset LatexCommand \index{Assembler routines}
11534 \begin_inset LatexCommand \index{\_asm}
11543 \begin_inset LatexCommand \index{\_endasm}
11552 #define SEI _asm\SpecialChar ~
11564 Note: it is sometimes sufficient to disable only a specific interrupt source
11566 a timer or serial interrupt by manipulating an
11569 \begin_inset LatexCommand \index{interrupt mask}
11579 Usually the time during which interrupts are disabled should be kept as
11581 This minimizes both
11586 \begin_inset LatexCommand \index{interrupt latency}
11590 (the time between the occurrence of the interrupt and the execution of
11591 the first code in the interrupt routine) and
11596 \begin_inset LatexCommand \index{interrupt jitter}
11600 (the difference between the shortest and the longest interrupt latency).
11601 These really are something different, f.e.
11602 a serial interrupt has to be served before its buffer overruns so it cares
11603 for the maximum interrupt latency, whereas it does not care about jitter.
11604 On a loudspeaker driven via a digital to analog converter which is fed
11605 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11606 a much smaller jitter will be very audible.
11609 You can reenable interrupts within an interrupt routine and on some architecture
11610 s you can make use of two (or more) levels of
11612 interrupt priorities
11615 \begin_inset LatexCommand \index{interrupt priority}
11620 On some architectures which don't support interrupt priorities these can
11621 be implemented by manipulating the interrupt mask and reenabling interrupts
11622 within the interrupt routine.
11623 Check there is sufficient space on the stack
11624 \begin_inset LatexCommand \index{stack}
11628 and don't add complexity unless you have to.
11633 \begin_inset LatexCommand \index{semaphore}
11637 locking (mcs51/ds390)
11640 Some architectures (mcs51/ds390) have an atomic
11641 \begin_inset LatexCommand \index{atomic}
11654 These type of instructions are typically used in preemptive multitasking
11655 systems, where a routine f.e.
11656 claims the use of a data structure ('acquires a lock
11657 \begin_inset LatexCommand \index{lock}
11661 on it'), makes some modifications and then releases the lock when the data
11662 structure is consistent again.
11663 The instruction may also be used if interrupt and non-interrupt code have
11664 to compete for a resource.
11665 With the atomic bit test and clear instruction interrupts
11666 \begin_inset LatexCommand \index{interrupt}
11670 don't have to be disabled for the locking operation.
11674 SDCC generates this instruction if the source follows this pattern:
11680 \begin_inset LatexCommand \index{volatile}
11684 bit resource_is_free;
11688 if (resource_is_free)
11698 resource_is_free=0;
11711 resource_is_free=1;
11718 Note, mcs51 and ds390 support only an atomic
11719 \begin_inset LatexCommand \index{atomic}
11727 instruction (as opposed to atomic bit test and
11732 Functions using private register banks
11733 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11740 Some architectures have support for quickly changing register sets.
11741 SDCC supports this feature with the
11744 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11749 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11755 attribute (which tells the compiler to use a register bank
11756 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11760 other than the default bank zero).
11761 It should only be applied to
11764 \begin_inset LatexCommand \index{interrupt}
11770 functions (see footnote below).
11771 This will in most circumstances make the generated ISR code more efficient
11772 since it will not have to save registers on the stack.
11779 attribute will have no effect on the generated code for a
11783 function (but may occasionally be useful anyway
11789 possible exception: if a function is called ONLY from 'interrupt' functions
11790 using a particular bank, it can be declared with the same 'using' attribute
11791 as the calling 'interrupt' functions.
11792 For instance, if you have several ISRs using bank one, and all of them
11793 call memcpy(), it might make sense to create a specialized version of memcpy()
11794 'using 1', since this would prevent the ISR from having to save bank zero
11795 to the stack on entry and switch to bank zero before calling the function
11802 (pending: I don't think this has been done yet)
11809 function using a non-zero bank will assume that it can trash that register
11810 bank, and will not save it.
11811 Since high-priority interrupts
11812 \begin_inset LatexCommand \index{interrupts}
11817 \begin_inset LatexCommand \index{interrupt priority}
11821 can interrupt low-priority ones on the 8051 and friends, this means that
11822 if a high-priority ISR
11826 a particular bank occurs while processing a low-priority ISR
11830 the same bank, terrible and bad things can happen.
11831 To prevent this, no single register bank should be
11835 by both a high priority and a low priority ISR.
11836 This is probably most easily done by having all high priority ISRs use
11837 one bank and all low priority ISRs use another.
11838 If you have an ISR which can change priority at runtime, you're on your
11839 own: I suggest using the default bank zero and taking the small performance
11843 It is most efficient if your ISR calls no other functions.
11844 If your ISR must call other functions, it is most efficient if those functions
11845 use the same bank as the ISR (see note 1 below); the next best is if the
11846 called functions use bank zero.
11847 It is very inefficient to call a function using a different, non-zero bank
11853 \begin_inset LatexCommand \label{sub:Startup-Code}
11858 \begin_inset LatexCommand \index{Startup code}
11865 MCS51/DS390 Startup Code
11868 The compiler inserts a call to the C routine
11870 _sdcc_external_startup()
11871 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11880 at the start of the CODE area.
11881 This routine is in the runtime library
11882 \begin_inset LatexCommand \index{Runtime library}
11887 By default this routine returns 0, if this routine returns a non-zero value,
11888 the static & global variable initialization will be skipped and the function
11889 main will be invoked.
11890 Otherwise static & global variables will be initialized before the function
11894 _sdcc_external_startup()
11896 routine to your program to override the default if you need to setup hardware
11897 or perform some other critical operation prior to static & global variable
11899 \begin_inset LatexCommand \index{Variable initialization}
11904 On some mcs51 variants xdata
11905 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11909 memory has to be explicitly enabled before it can be accessed or if the
11910 watchdog needs to be disabled, this is the place to do it.
11911 The startup code clears all internal data memory, 256 bytes by default,
11912 but from 0 to n-1 if
11925 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11932 (recommended for Chipcon CC1010).
11935 See also the compiler option
11954 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11959 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11964 about MCS51-variants.
11970 The HC08 startup code follows the same scheme as the MCS51 startup code.
11976 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11977 from sdcc/device/lib/z80/crt0.s.
11978 If you need a different startup code you can use the compiler option
11999 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12003 and provide your own crt0.o.
12007 Inline Assembler Code
12008 \begin_inset LatexCommand \index{Assembler routines}
12015 A Step by Step Introduction
12016 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12023 Starting from a small snippet of c-code this example shows for the MCS51
12024 how to use inline assembly, access variables, a function parameter and
12025 an array in xdata memory.
12026 The example uses an MCS51 here but is easily adapted for other architectures.
12027 This is a buffer routine which should be optimized:
12034 \begin_inset LatexCommand \index{far (storage class)}
12039 \begin_inset LatexCommand \index{\_\_far (storage class)}
12044 \begin_inset LatexCommand \index{at}
12049 \begin_inset LatexCommand \index{\_\_at}
12054 \begin_inset LatexCommand \index{Aligned array}
12060 unsigned char head,tail;
12064 void to_buffer( unsigned char c )
12072 if( head != tail-1 )
12082 buf[ head++ ] = c;\SpecialChar ~
12086 /* access to a 256 byte aligned array */
12091 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12092 then a corresponding buffer.asm file is generated.
12093 We define a new function
12097 in file buffer.c in which we cut and paste the generated code, removing
12098 unwanted comments and some ':'.
12100 \begin_inset Quotes sld
12104 \begin_inset Quotes srd
12108 \begin_inset Quotes sld
12112 \begin_inset Quotes srd
12115 to the beginning and the end of the function body:
12121 /* With a cut and paste from the .asm file, we have something to start with.
12126 The function is not yet OK! (registers aren't saved) */
12128 void to_buffer_asm( unsigned char c )
12137 \begin_inset LatexCommand \index{\_asm}
12142 \begin_inset LatexCommand \index{\_\_asm}
12156 ;buffer.c if( head != tail-1 )
12204 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12205 \begin_inset LatexCommand \index{Aligned array}
12270 \begin_inset LatexCommand \index{\_endasm}
12275 \begin_inset LatexCommand \index{\_\_endasm}
12284 The new file buffer.c should compile with only one warning about the unreferenced
12285 function argument 'c'.
12286 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12287 (1) and finally have:
12293 unsigned char far at 0x7f00 buf[0x100];
12295 unsigned char head,tail;
12297 #define USE_ASSEMBLY (1)
12305 void to_buffer( unsigned char c )
12313 if( head != tail-1 )
12333 void to_buffer( unsigned char c )
12341 c; // to avoid warning: unreferenced function argument
12348 \begin_inset LatexCommand \index{\_asm}
12353 \begin_inset LatexCommand \index{\_\_asm}
12367 ; save used registers here.
12378 ; If we were still using r2,r3 we would have to push them here.
12381 ; if( head != tail-1 )
12424 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12448 ; buf[ head++ ] = c;
12459 a,dpl \SpecialChar ~
12466 ; dpl holds lower byte of function argument
12477 dpl,_head \SpecialChar ~
12480 ; buf is 0x100 byte aligned so head can be used directly
12522 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12534 ; restore used registers here
12541 \begin_inset LatexCommand \index{\_endasm}
12546 \begin_inset LatexCommand \index{\_\_endasm}
12557 The inline assembler code can contain any valid code understood by the assembler
12558 , this includes any assembler directives and comment lines
12564 The assembler does not like some characters like ':' or ''' in comments.
12565 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12566 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12571 \begin_inset LatexCommand \index{Assembler documentation}
12579 The compiler does not do any validation of the code within the
12582 \begin_inset LatexCommand \index{\_asm}
12587 \begin_inset LatexCommand \index{\_\_asm}
12595 \begin_inset LatexCommand \index{\_endasm}
12600 \begin_inset LatexCommand \index{\_\_endasm}
12609 Specifically it will not know which registers are used and thus register
12611 \begin_inset LatexCommand \index{push/pop}
12615 has to be done manually.
12619 It is recommended that each assembly instruction (including labels) be placed
12620 in a separate line (as the example shows).
12634 \begin_inset LatexCommand \index{-\/-peep-asm}
12640 command line option is used, the inline assembler code will be passed through
12641 the peephole optimizer
12642 \begin_inset LatexCommand \index{Peephole optimizer}
12647 There are only a few (if any) cases where this option makes sense, it might
12648 cause some unexpected changes in the inline assembler code.
12649 Please go through the peephole optimizer rules defined in file
12653 before using this option.
12657 \begin_inset LatexCommand \label{sub:Naked-Functions}
12662 \begin_inset LatexCommand \index{Naked functions}
12669 A special keyword may be associated with a function declaring it as
12672 \begin_inset LatexCommand \index{\_naked}
12677 \begin_inset LatexCommand \index{\_\_naked}
12688 function modifier attribute prevents the compiler from generating prologue
12689 \begin_inset LatexCommand \index{function prologue}
12694 \begin_inset LatexCommand \index{function epilogue}
12698 code for that function.
12699 This means that the user is entirely responsible for such things as saving
12700 any registers that may need to be preserved, selecting the proper register
12701 bank, generating the
12705 instruction at the end, etc.
12706 Practically, this means that the contents of the function must be written
12707 in inline assembler.
12708 This is particularly useful for interrupt functions, which can have a large
12709 (and often unnecessary) prologue/epilogue.
12710 For example, compare the code generated by these two functions:
12716 \begin_inset LatexCommand \index{volatile}
12720 data unsigned char counter;
12724 void simpleInterrupt(void) interrupt
12725 \begin_inset LatexCommand \index{interrupt}
12730 \begin_inset LatexCommand \index{\_\_interrupt}
12748 void nakedInterrupt(void) interrupt 2 _naked
12757 \begin_inset LatexCommand \index{\_asm}
12762 \begin_inset LatexCommand \index{\_\_asm}
12779 _counter ; does not change flags, no need to save psw
12791 ; MUST explicitly include ret or reti in _naked function.
12798 \begin_inset LatexCommand \index{\_endasm}
12803 \begin_inset LatexCommand \index{\_\_endasm}
12812 For an 8051 target, the generated simpleInterrupt looks like:
12953 whereas nakedInterrupt looks like:
12968 _counter ; does not change flags, no need to save psw
12986 ; MUST explicitly include ret or reti in _naked function
12989 The related directive #pragma exclude
12990 \begin_inset LatexCommand \index{\#pragma exclude}
12994 allows a more fine grained control over pushing & popping
12995 \begin_inset LatexCommand \index{push/pop}
13002 While there is nothing preventing you from writing C code inside a
13006 function, there are many ways to shoot yourself in the foot doing this,
13007 and it is recommended that you stick to inline assembler.
13010 Use of Labels within Inline Assembler
13013 SDCC allows the use of in-line assembler with a few restrictions regarding
13015 In older versions of the compiler all labels defined within inline assembler
13024 where nnnn is a number less than 100 (which implies a limit of utmost 100
13025 inline assembler labels
13039 \begin_inset LatexCommand \index{\_asm}
13044 \begin_inset LatexCommand \index{\_\_asm}
13074 \begin_inset LatexCommand \index{\_endasm}
13079 \begin_inset LatexCommand \index{\_\_endasm}
13086 Inline assembler code cannot reference any C-Labels, however it can reference
13088 \begin_inset LatexCommand \index{Labels}
13092 defined by the inline assembler, e.g.:
13117 ; some assembler code
13137 /* some more c code */
13139 clabel:\SpecialChar ~
13141 /* inline assembler cannot reference this label */
13153 $0003: ;label (can be referenced by inline assembler only)
13160 \begin_inset LatexCommand \index{\_endasm}
13165 \begin_inset LatexCommand \index{\_\_endasm}
13175 /* some more c code */
13180 In other words inline assembly code can access labels defined in inline
13181 assembly within the scope of the function.
13182 The same goes the other way, i.e.
13183 labels defines in inline assembly can not be accessed by C statements.
13186 Interfacing with Assembler Code
13187 \begin_inset LatexCommand \index{Assembler routines}
13194 Global Registers used for Parameter Passing
13195 \begin_inset LatexCommand \index{Parameter passing}
13202 The compiler always uses the global registers
13205 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13210 \begin_inset LatexCommand \index{DPTR}
13215 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13224 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13230 to pass the first parameter to a routine.
13231 The second parameter onwards is either allocated on the stack (for reentrant
13242 -stack-auto is used) or in data / xdata memory (depending on the memory
13247 Assembler Routine (non-reentrant)
13250 In the following example
13251 \begin_inset LatexCommand \index{reentrant}
13256 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13260 the function c_func calls an assembler routine asm_func, which takes two
13262 \begin_inset LatexCommand \index{function parameter}
13271 extern int asm_func(unsigned char, unsigned char);
13275 int c_func (unsigned char i, unsigned char j)
13283 return asm_func(i,j);
13297 return c_func(10,9);
13302 The corresponding assembler function is:
13307 .globl _asm_func_PARM_2
13408 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13425 Note here that the return values
13426 \begin_inset LatexCommand \index{return value}
13430 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13432 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13433 b' & 'acc' for four byte values.
13436 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13437 the parameter number starting from 1, and counting from the left.
13438 The first parameter is passed in
13439 \begin_inset Quotes eld
13443 \begin_inset Quotes erd
13446 for a one byte parameter,
13447 \begin_inset Quotes eld
13451 \begin_inset Quotes erd
13455 \begin_inset Quotes eld
13459 \begin_inset Quotes erd
13462 for three bytes and
13463 \begin_inset Quotes eld
13467 \begin_inset Quotes erd
13470 for a four bytes parameter.
13471 The variable name for the second parameter will be _<function_name>_PARM_2.
13475 Assemble the assembler routine with the following command:
13482 asx8051 -losg asmfunc.asm
13489 Then compile and link the assembler routine to the C source file with the
13497 sdcc cfunc.c asmfunc.rel
13500 Assembler Routine (reentrant)
13504 \begin_inset LatexCommand \index{reentrant}
13509 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13513 the second parameter
13514 \begin_inset LatexCommand \index{function parameter}
13518 onwards will be passed on the stack, the parameters are pushed from right
13520 after the call the leftmost parameter will be on the top of the stack.
13521 Here is an example:
13526 extern int asm_func(unsigned char, unsigned char);
13530 int c_func (unsigned char i, unsigned char j) reentrant
13538 return asm_func(i,j);
13552 return c_func(10,9);
13557 The corresponding assembler routine is:
13657 The compiling and linking procedure remains the same, however note the extra
13658 entry & exit linkage required for the assembler code, _bp is the stack
13659 frame pointer and is used to compute the offset into the stack for parameters
13660 and local variables.
13664 \begin_inset LatexCommand \index{int (16 bit)}
13669 \begin_inset LatexCommand \index{long (32 bit)}
13676 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13677 multiplication and modulus operations are implemented by support routines.
13678 These support routines are all developed in ANSI-C to facilitate porting
13679 to other MCUs, although some model specific assembler optimizations are
13681 The following files contain the described routines, all of them can be
13682 found in <installdir>/share/sdcc/lib.
13688 \begin_inset Tabular
13689 <lyxtabular version="3" rows="11" columns="2">
13691 <column alignment="center" valignment="top" leftline="true" width="0">
13692 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13693 <row topline="true" bottomline="true">
13694 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13704 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13715 <row topline="true">
13716 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13724 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13729 16 bit multiplication
13733 <row topline="true">
13734 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13742 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13747 signed 16 bit division (calls _divuint)
13751 <row topline="true">
13752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13760 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13765 unsigned 16 bit division
13769 <row topline="true">
13770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13778 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13783 signed 16 bit modulus (calls _moduint)
13787 <row topline="true">
13788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13801 unsigned 16 bit modulus
13805 <row topline="true">
13806 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13814 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13819 32 bit multiplication
13823 <row topline="true">
13824 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13832 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13837 signed 32 division (calls _divulong)
13841 <row topline="true">
13842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13850 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13855 unsigned 32 division
13859 <row topline="true">
13860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13873 signed 32 bit modulus (calls _modulong)
13877 <row topline="true" bottomline="true">
13878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13886 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13891 unsigned 32 bit modulus
13904 Since they are compiled as
13909 \begin_inset LatexCommand \index{reentrant}
13914 \begin_inset LatexCommand \index{interrupt}
13918 service routines should not do any of the above operations.
13919 If this is unavoidable then the above routines will need to be compiled
13933 \begin_inset LatexCommand \index{-\/-stack-auto}
13939 option, after which the source program will have to be compiled with
13952 \begin_inset LatexCommand \index{-\/-int-long-reent}
13959 Notice that you don't have to call these routines directly.
13960 The compiler will use them automatically every time an integer operation
13964 Floating Point Support
13965 \begin_inset LatexCommand \index{Floating point support}
13972 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13973 floating point support routines are derived from gcc's floatlib.c and consist
13974 of the following routines:
13982 \begin_inset Tabular
13983 <lyxtabular version="3" rows="17" columns="2">
13985 <column alignment="center" valignment="top" leftline="true" width="0">
13986 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13987 <row topline="true" bottomline="true">
13988 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14005 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14014 <row topline="true">
14015 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14046 add floating point numbers
14050 <row topline="true">
14051 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14068 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14082 subtract floating point numbers
14086 <row topline="true">
14087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14104 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14118 divide floating point numbers
14122 <row topline="true">
14123 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14140 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14154 multiply floating point numbers
14158 <row topline="true">
14159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14176 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14190 convert floating point to unsigned char
14194 <row topline="true">
14195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14212 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14226 convert floating point to signed char
14230 <row topline="true">
14231 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14248 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14262 convert floating point to unsigned int
14266 <row topline="true">
14267 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14284 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14298 convert floating point to signed int
14302 <row topline="true">
14303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14343 convert floating point to unsigned long
14347 <row topline="true">
14348 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14379 convert floating point to signed long
14383 <row topline="true">
14384 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14401 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14415 convert unsigned char to floating point
14419 <row topline="true">
14420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14437 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14451 convert char to floating point number
14455 <row topline="true">
14456 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14473 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14487 convert unsigned int to floating point
14491 <row topline="true">
14492 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14509 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14523 convert int to floating point numbers
14527 <row topline="true">
14528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14545 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14559 convert unsigned long to floating point number
14563 <row topline="true" bottomline="true">
14564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14581 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14595 convert long to floating point number
14608 These support routines are developed in ANSI-C so there is room for space
14609 and speed improvement
14615 The floating point routines for the mcs51 are implemented in assembler
14619 Note if all these routines are used simultaneously the data space might
14621 For serious floating point usage the large model might be needed.
14622 Also notice that you don't have to call this routines directly.
14623 The compiler will use them automatically every time a floating point operation
14628 \begin_inset LatexCommand \index{Libraries}
14637 <pending: this is messy and incomplete - a little more information is in
14638 sdcc/doc/libdoc.txt
14643 Compiler support routines (_gptrget, _mulint etc.)
14646 Stdclib functions (puts, printf, strcat etc.)
14647 \layout Subsubsection
14653 \begin_inset LatexCommand \index{<stdio.h>}
14657 As usual on embedded systems you have to provide your own
14660 \begin_inset LatexCommand \index{getchar()}
14669 \begin_inset LatexCommand \index{putchar()}
14676 SDCC does not know whether the system connects to a serial line with or
14677 without handshake, LCD, keyboard or other device.
14678 You'll find examples for serial routines f.e.
14679 in sdcc/device/lib.
14685 \begin_inset LatexCommand \index{printf()}
14695 does not support float (except on ds390).
14696 To enable this recompile it with the option
14709 \begin_inset LatexCommand \index{USE\_FLOATS}
14715 on the command line.
14729 \begin_inset LatexCommand \index{-\/-model-large}
14735 for the mcs51 port, since this uses a lot of memory.
14738 If you're short on memory you might want to use
14741 \begin_inset LatexCommand \index{printf\_small()}
14756 For the mcs51 there additionally are assembly versions
14759 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14768 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14777 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14783 which should fit the requirements of many embedded systems (printf_fast()
14784 can be customized by unsetting #defines to
14788 support long variables and field widths).
14791 Math functions (sin, pow, sqrt etc.)
14798 \begin_inset LatexCommand \index{Libraries}
14802 included in SDCC should have a license at least as liberal as the GNU Lesser
14803 General Public License
14804 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14815 license statements for the libraries are missing.
14816 sdcc/device/lib/ser_ir.c
14820 come with a GPL (as opposed to LGPL) License - this will not be liberal
14821 enough for many embedded programmers.
14824 If you have ported some library or want to share experience about some code
14826 falls into any of these categories Busses (I
14827 \begin_inset Formula $^{\textrm{2}}$
14830 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14831 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14832 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14833 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14838 would certainly like to hear about it.
14839 Programmers coding for embedded systems are not especially famous for being
14840 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14841 e these references are very valuable.
14842 Let's help to create a climate where information is shared.
14848 MCS51 Memory Models
14849 \begin_inset LatexCommand \index{Memory model}
14854 \begin_inset LatexCommand \index{MCS51 memory model}
14859 \layout Subsubsection
14861 Small, Medium and Large
14864 SDCC allows three memory models for MCS51 code,
14873 Modules compiled with different memory models should
14877 be combined together or the results would be unpredictable.
14878 The library routines supplied with the compiler are compiled as small,
14880 The compiled library modules are contained in separate directories as small,
14881 medium and large so that you can link to the appropriate set.
14884 When the medium or large model is used all variables declared without a
14885 storage class will be allocated into the external ram, this includes all
14886 parameters and local variables (for non-reentrant
14887 \begin_inset LatexCommand \index{reentrant}
14892 When the small model is used variables without storage class are allocated
14893 in the internal ram.
14896 Judicious usage of the processor specific storage classes
14897 \begin_inset LatexCommand \index{Storage class}
14901 and the 'reentrant' function type will yield much more efficient code,
14902 than using the large model.
14903 Several optimizations are disabled when the program is compiled using the
14904 large model, it is therefore recommended that the small model be used unless
14905 absolutely required.
14906 \layout Subsubsection
14909 \begin_inset LatexCommand \label{sub:External-Stack}
14914 \begin_inset LatexCommand \index{stack}
14919 \begin_inset LatexCommand \index{External stack (mcs51)}
14926 The external stack (-
14937 \begin_inset LatexCommand \index{-\/-xstack}
14941 ) is located in pdata
14942 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14946 memory (usually at the start of the external ram segment) and uses all
14947 unused space in pdata (max.
14959 -xstack option is used to compile the program, the parameters and local
14961 \begin_inset LatexCommand \index{local variables}
14965 of all reentrant functions are allocated in this area.
14966 This option is provided for programs with large stack space requirements.
14967 When used with the -
14978 \begin_inset LatexCommand \index{-\/-stack-auto}
14982 option, all parameters and local variables are allocated on the external
14983 stack (note: support libraries will need to be recompiled with the same
14985 There is a predefined target in the library makefile).
14988 The compiler outputs the higher order address byte of the external ram segment
14990 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14995 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14999 ), therefore when using the External Stack option, this port
15003 be used by the application program.
15007 \begin_inset LatexCommand \index{Memory model}
15012 \begin_inset LatexCommand \index{DS390 memory model}
15019 The only model supported is Flat 24
15020 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15025 This generates code for the 24 bit contiguous addressing mode of the Dallas
15027 In this mode, up to four meg of external RAM or code space can be directly
15029 See the data sheets at www.dalsemi.com for further information on this part.
15033 Note that the compiler does not generate any code to place the processor
15034 into 24 bitmode (although
15038 in the ds390 libraries will do that for you).
15044 \begin_inset LatexCommand \index{Tinibios (DS390)}
15048 , the boot loader or similar code must ensure that the processor is in 24
15049 bit contiguous addressing mode before calling the SDCC startup code.
15067 option, variables will by default be placed into the XDATA segment.
15072 Segments may be placed anywhere in the 4 meg address space using the usual
15084 Note that if any segments are located above 64K, the -r flag must be passed
15085 to the linker to generate the proper segment relocations, and the Intel
15086 HEX output format must be used.
15087 The -r flag can be passed to the linker by using the option
15091 on the SDCC command line.
15092 However, currently the linker can not handle code segments > 64k.
15096 \begin_inset LatexCommand \index{Pragmas}
15103 SDCC supports the following #pragma directives:
15107 \begin_inset LatexCommand \index{\#pragma save}
15111 - this will save all current options to the save/restore stack.
15112 See #pragma\SpecialChar ~
15117 \begin_inset LatexCommand \index{\#pragma restore}
15121 - will restore saved options from the last save.
15122 saves & restores can be nested.
15123 SDCC uses a save/restore stack: save pushes current options to the stack,
15124 restore pulls current options from the stack.
15125 See #pragma\SpecialChar ~
15132 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15137 \begin_inset LatexCommand \index{function prologue}
15141 function1[,function2[,function3...]] - The compiler by default uses a caller
15142 saves convention for register saving across function calls, however this
15143 can cause unnecessary register pushing & popping
15144 \begin_inset LatexCommand \index{push/pop}
15148 when calling small functions from larger functions.
15149 This option can be used to switch off the register saving convention for
15150 the function names specified.
15151 The compiler will not save registers when calling these functions, extra
15152 code need to be manually inserted at the entry & exit for these functions
15153 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15154 reduce code & improve run time performance of the generated code.
15155 In the future the compiler (with inter procedural analysis) may be able
15156 to determine the appropriate scheme to use for each function call.
15167 -callee-saves command line option is used, the function names specified
15168 in #pragma\SpecialChar ~
15170 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15174 is appended to the list of functions specified in the command line.
15178 \begin_inset LatexCommand \index{\#pragma exclude}
15182 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15183 of pairs of push/pop
15184 \begin_inset LatexCommand \index{push/pop}
15193 \begin_inset LatexCommand \index{interrupt}
15206 The directive should be placed immediately before the ISR function definition
15207 and it affects ALL ISR functions following it.
15208 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15209 exclude\SpecialChar ~
15211 \begin_inset LatexCommand \index{\#pragma exclude}
15216 See also the related keyword _naked
15217 \begin_inset LatexCommand \index{\_naked}
15222 \begin_inset LatexCommand \index{\_\_naked}
15230 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15234 - the compiler will not warn you anymore for obvious mistakes, you'r on
15238 disable_warning <nnnn>
15239 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15243 - the compiler will not warn you anymore about warning number <nnnn>.
15247 \begin_inset LatexCommand \index{\#pragma nogcse}
15251 - will stop global common subexpression elimination.
15255 \begin_inset LatexCommand \index{\#pragma noinduction}
15259 - will stop loop induction optimizations.
15263 \begin_inset LatexCommand \index{\#pragma noinvariant}
15267 - will not do loop invariant optimizations.
15268 For more details see Loop Invariants in section
15269 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15277 \begin_inset LatexCommand \index{\#pragma noiv}
15281 - Do not generate interrupt
15282 \begin_inset LatexCommand \index{interrupt}
15286 vector table entries for all ISR functions defined after the pragma.
15287 This is useful in cases where the interrupt vector table must be defined
15288 manually, or when there is a secondary, manually defined interrupt vector
15290 for the autovector feature of the Cypress EZ-USB FX2).
15291 More elegantly this can be achieved by obmitting the optional interrupt
15292 number after the interrupt keyword, see section
15293 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15302 \begin_inset LatexCommand \index{\#pragma nojtbound}
15306 - will not generate code for boundary value checking, when switch statements
15307 are turned into jump-tables (dangerous).
15308 For more details see section
15309 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15317 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15321 - Will not do loop reversal optimization
15325 \begin_inset LatexCommand \index{\#pragma nooverlay}
15329 - the compiler will not overlay the parameters and local variables of a
15334 \begin_inset LatexCommand \index{\#pragma stackauto}
15349 \begin_inset LatexCommand \index{-\/-stack-auto}
15354 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15358 Parameters and Local Variables.
15362 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15366 - The compiler will optimize code generation towards fast code, possibly
15367 at the expense of code size.
15371 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15375 - The compiler will optimize code generation towards compact code, possibly
15376 at the expense of code speed.
15380 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15384 - The compiler will attempt to generate code that is both compact and fast,
15385 as long as meeting one goal is not a detriment to the other (this is the
15391 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15395 - Generally follow the C89 standard, but allow SDCC features that conflict
15396 with the standard (default).
15400 \begin_inset LatexCommand \index{\#pragma std\_c89}
15404 - Follow the C89 standard and disable SDCC features that conflict with the
15409 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15413 - Generally follow the C99 standard, but allow SDCC features that conflict
15414 with the standard (incomplete support).
15418 \begin_inset LatexCommand \index{\#pragma std\_c99}
15422 - Follow the C99 standard and disable SDCC features that conflict with the
15423 standard (incomplete support).
15427 \begin_inset LatexCommand \index{\#pragma codeseg}
15431 - Use this name (max.
15432 8 characters) for the code segment.
15436 \begin_inset LatexCommand \index{\#pragma constseg}
15440 - Use this name (max.
15441 8 characters) for the const segment.
15444 SDCPP supports the following #pragma directives:
15448 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15452 (+ | -) - switch _asm _endasm block preprocessing on / off.
15456 The pragma's are intended to be used to turn-on or off certain optimizations
15457 which might cause the compiler to generate extra stack / data space to
15458 store compiler generated temporary variables.
15459 This usually happens in large functions.
15460 Pragma directives should be used as shown in the following example, they
15461 are used to control options & optimizations for a given function; pragmas
15462 should be placed before and/or after a function, placing pragma's inside
15463 a function body could have unpredictable results.
15469 \begin_inset LatexCommand \index{\#pragma save}
15480 /* save the current settings */
15483 \begin_inset LatexCommand \index{\#pragma nogcse}
15492 /* turnoff global subexpression elimination */
15494 #pragma noinduction
15495 \begin_inset LatexCommand \index{\#pragma noinduction}
15499 /* turn off induction optimizations */
15522 \begin_inset LatexCommand \index{\#pragma restore}
15526 /* turn the optimizations back on */
15529 The compiler will generate a warning message when extra space is allocated.
15530 It is strongly recommended that the save and restore pragma's be used when
15531 changing options for a function.
15534 Defines Created by the Compiler
15537 The compiler creates the following #defines
15538 \begin_inset LatexCommand \index{\#defines}
15543 \begin_inset LatexCommand \index{Defines created by the compiler}
15553 \begin_inset Tabular
15554 <lyxtabular version="3" rows="11" columns="2">
15556 <column alignment="center" valignment="top" leftline="true" width="0">
15557 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15558 <row topline="true" bottomline="true">
15559 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15580 <row topline="true">
15581 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15587 \begin_inset LatexCommand \index{SDCC}
15594 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15599 this Symbol is always defined.
15600 Since version 2.5.6 it's this version number as an int (ex.
15605 <row topline="true">
15606 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15612 \begin_inset LatexCommand \index{SDCC\_mcs51}
15617 \begin_inset LatexCommand \index{SDCC\_ds390}
15622 \begin_inset LatexCommand \index{SDCC\_z80}
15629 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15634 depending on the model used (e.g.: -mds390
15638 <row topline="true">
15639 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15645 \begin_inset LatexCommand \index{\_\_mcs51}
15650 \begin_inset LatexCommand \index{\_\_ds390}
15655 \begin_inset LatexCommand \index{\_\_hc08}
15660 \begin_inset LatexCommand \index{\_\_z80}
15667 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15672 depending on the model used (e.g.
15677 <row topline="true">
15678 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15684 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15691 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15714 <row topline="true">
15715 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15721 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15751 <row topline="true">
15752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15758 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15788 <row topline="true">
15789 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15795 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15825 <row topline="true">
15826 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15832 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15862 <row topline="true">
15863 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15869 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15876 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15889 <row topline="true" bottomline="true">
15890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15896 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15923 Notes on supported Processors
15927 \begin_inset LatexCommand \label{sub:MCS51-variants}
15932 \begin_inset LatexCommand \index{MCS51 variants}
15939 MCS51 processors are available from many vendors and come in many different
15941 While they might differ considerably in respect to Special Function Registers
15942 the core MCS51 is usually not modified or is kept compatible.
15946 pdata access by SFR
15949 With the upcome of devices with internal xdata and flash memory devices
15951 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15955 as dedicated I/O port is becoming more popular.
15956 Switching the high byte for pdata
15957 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15961 access which was formerly done by port P2 is then achieved by a Special
15963 \begin_inset LatexCommand \index{sfr}
15968 In well-established MCS51 tradition the address of this
15972 is where the chip designers decided to put it.
15973 Needless to say that they didn't agree on a common name either.
15974 So that the startup code can correctly initialize xdata variables, you
15975 should define an sfr with the name _XPAGE
15978 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15984 at the appropriate location if the default, port P2, is not used for this.
15990 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15995 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16000 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16003 For more exotic implementations further customizations may be needed.
16005 \begin_inset LatexCommand \ref{sub:Startup-Code}
16009 for other possibilities.
16012 Other Features available by SFR
16015 Some MCS51 variants offer features like Double DPTR
16016 \begin_inset LatexCommand \index{DPTR}
16020 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16021 These are currently not used for the MCS51 port.
16022 If you absolutely need them you can fall back to inline assembly or submit
16029 The DS80C400 microcontroller has a rich set of peripherals.
16030 In its built-in ROM library it includes functions to access some of the
16031 features, among them is a TCP stack with IP4 and IP6 support.
16032 Library headers (currently in beta status) and other files are provided
16036 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16044 The Z80 and gbz80 port
16047 SDCC can target both the Zilog
16048 \begin_inset LatexCommand \index{Z80}
16052 and the Nintendo Gameboy's Z80-like gbz80
16053 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16058 The Z80 port is passed through the same
16061 \begin_inset LatexCommand \index{Regression test}
16067 as the MCS51 and DS390 ports, so floating point support, support for long
16068 variables and bitfield support is fine.
16069 See mailing lists and forums about interrupt routines.
16072 As always, the code is the authoritative reference - see z80/ralloc.c and
16075 \begin_inset LatexCommand \index{stack}
16079 frame is similar to that generated by the IAR Z80 compiler.
16080 IX is used as the base pointer, HL and IY are used as a temporary registers,
16081 and BC and DE are available for holding variables.
16083 \begin_inset LatexCommand \index{return value}
16087 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16089 The gbz80 port use the same set of registers for the return values, but
16090 in a different order of significance: E (one byte), DE (two bytes), or
16097 The port to the Motorola HC08
16098 \begin_inset LatexCommand \index{HC08}
16102 family has been added in October 2003, and is still undergoing some basic
16104 The code generator is complete, but the register allocation is still quite
16106 Some of the SDCC's standard C library functions have embedded non-HC08
16107 inline assembly and so are not yet usable.
16118 \begin_inset LatexCommand \index{PIC14}
16122 port still requires a major effort from the development community.
16123 However it can work for very simple code.
16126 C code and 14bit PIC code page
16127 \begin_inset LatexCommand \index{code page (pic14)}
16132 \begin_inset LatexCommand \index{RAM bank (pic14)}
16139 The linker organizes allocation for the code page and RAM banks.
16140 It does not have intimate knowledge of the code flow.
16141 It will put all the code section of a single asm file into a single code
16143 In order to make use of multiple code pages, separate asm files must be
16145 The compiler treats all functions of a single C file as being in the same
16146 code page unless it is non static.
16147 The compiler treats all local variables of a single C file as being in
16148 the same RAM bank unless it is an extern.
16152 To get the best follow these guide lines:
16155 make local functions static, as non static functions require code page selection
16159 Make local variables static as extern variables require RAM bank selection
16163 For devices that have multiple code pages it is more efficient to use the
16164 same number of files as pages, i.e.
16165 for the 16F877 use 4 separate files and i.e.
16166 for the 16F874 use 2 separate files.
16167 This way the linker can put the code for each file into different code
16168 pages and the compiler can allocate reusable variables more efficiently
16169 and there's less page selection overhead.
16170 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16171 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16174 Creating a device include file
16177 For generating a device include file use the support perl script inc2h.pl
16178 kept in directory support/script.
16184 For the interrupt function, use the keyword 'interrupt'
16185 \begin_inset LatexCommand \index{interrupt}
16189 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16190 there to avoid a syntax error - it ought to be fixed).
16196 void Intr(void) interrupt 0
16202 T0IF = 0; /* Clear timer interrupt */
16207 Linking and assembling
16210 For assembling you can use either GPUTILS'
16211 \begin_inset LatexCommand \index{gputils (pic tools)}
16215 gpasm.exe or MPLAB's mpasmwin.exe.
16216 GPUTILS is available from
16217 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16222 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16223 If you use MPLAB and an interrupt function then the linker script file
16224 vectors section will need to be enlarged to link with mplink.
16247 sdcc -S -V -mpic14 -p16F877 $<
16261 $(PRJ).hex: $(OBJS)
16271 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16293 sdcc -S -V -mpic14 -p16F877 $<
16303 mpasmwin /q /o $*.asm
16307 $(PRJ).hex: $(OBJS)
16317 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16320 Please note that indentations within a
16324 have to be done with a tabulator character.
16327 Command-line options
16330 Besides the switches common to all SDCC backends, the PIC14 port accepts
16331 the following options (for an updated list see sdcc -
16343 \labelwidthstring 00.00.0000
16355 -debug-extra emit debug info in assembly output
16357 \labelwidthstring 00.00.0000
16369 -no-pcode-opt disable (slightly faulty) optimization on pCode
16373 \layout Subsubsection
16375 error: missing definition for symbol
16376 \begin_inset Quotes sld
16380 \begin_inset Quotes srd
16386 The PIC14 port uses library routines to provide more complex operations
16387 like multiplication, division/modulus and (generic) pointer dereferencing.
16388 In order to add these routines to your project, you must link with PIC14's
16394 For single source file projects this is done automatically, more complex
16399 to the linker's arguments.
16400 Make sure you also add an include path for the library (using the -I switch
16402 \layout Subsubsection
16404 Processor mismatch in file
16405 \begin_inset Quotes sld
16409 \begin_inset Quotes srd
16415 This warning can usually be ignored due to the very good compatibility amongst
16416 14 bit PIC devices.
16419 You might also consider recompiling the library for your specific device
16420 by changing the ARCH=p16f877 (default target) entry in
16422 device/lib/pic/Makefile.in
16426 device/lib/pic/Makefile
16428 to reflect your device.
16429 This might even improve performance for smaller devices as unneccesary
16430 BANKSELs migth be removed.
16434 \layout Subsubsection
16439 Currently, data can only be initialized if it resides in the source file
16445 Data in other source files will silently
16453 \begin_inset LatexCommand \index{PIC16}
16461 \begin_inset LatexCommand \index{PIC16}
16465 port is the portion of SDCC that is responsible to produce code for the
16467 \begin_inset LatexCommand \index{Microchip}
16471 (TM) microcontrollers with 16 bit core.
16472 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16473 Currently supported devices are:
16477 \begin_inset Tabular
16478 <lyxtabular version="3" rows="4" columns="6">
16480 <column alignment="center" valignment="top" leftline="true" width="0">
16481 <column alignment="center" valignment="top" leftline="true" width="0">
16482 <column alignment="center" valignment="top" leftline="true" width="0">
16483 <column alignment="center" valignment="top" leftline="true" width="0">
16484 <column alignment="center" valignment="top" leftline="true" width="0">
16485 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16486 <row topline="true">
16487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16503 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16511 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16527 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16536 <row topline="true">
16537 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16545 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16553 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16561 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16569 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16577 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16586 <row topline="true">
16587 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16603 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16619 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16627 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16636 <row topline="true" bottomline="true">
16637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16645 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16653 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16661 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16668 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16675 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16693 PIC16 port supports the standard command line arguments as supposed, with
16694 the exception of certain cases that will be mentioned in the following
16697 \labelwidthstring 00.00.0000
16709 -callee-saves See -
16721 \labelwidthstring 00.00.0000
16733 -all-callee-saves All function arguments are passed on stack by default.
16736 There is no need to specify this in the command line.
16738 \labelwidthstring 00.00.0000
16750 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16751 no local variables.
16754 Port Specific Options
16755 \begin_inset LatexCommand \index{Options PIC16}
16762 The port specific options appear after the global options in the sdcc --help
16764 \layout Subsubsection
16769 General options enable certain port features and optimizations.
16771 \labelwidthstring 00.00.0000
16783 -stack-model=[model] Used in conjuction with the command above.
16784 Defines the stack model to be used, valid stack models are :
16787 \labelwidthstring 00.00.0000
16793 Selects small stack model.
16794 8 bit stack and frame pointers.
16795 Supports 256 bytes stack size.
16797 \labelwidthstring 00.00.0000
16803 Selects large stack model.
16804 16 bit stack and frame pointers.
16805 Supports 65536 bytes stack size.
16808 \labelwidthstring 00.00.0000
16820 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16821 unitialized data variables with [kword].
16822 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16824 \labelwidthstring 00.00.0000
16836 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16837 Useful for bootloaders.
16839 \labelwidthstring 00.00.0000
16851 -asm= sets the full path and name of an external assembler to call.
16853 \labelwidthstring 00.00.0000
16865 -link= sets the full path and name of an external linker to call.
16867 \labelwidthstring 00.00.0000
16879 -mplab-comp MPLAB compatibility option.
16880 Currently only suppresses special gpasm directives.
16881 \layout Subsubsection
16883 Optimization Options
16885 \labelwidthstring 00.00.0000
16897 -optimize-goto Try to use (conditional) BRA instead of GOTO
16899 \labelwidthstring 00.00.0000
16911 -optimize-cmp Try to optimize some compares.
16913 \labelwidthstring 00.00.0000
16925 -optimize-df Analyze the dataflow of the generated code and improve it.
16927 \labelwidthstring 00.00.0000
16939 -obanksel=nn Set optimization level for inserting BANKSELs.
16944 \labelwidthstring 00.00.0000
16948 \labelwidthstring 00.00.0000
16950 1 checks previous used register and if it is the same then does not emit
16951 BANKSEL, accounts only for labels.
16953 \labelwidthstring 00.00.0000
16955 2 tries to check the location of (even different) symbols and removes BANKSELs
16956 if they are in the same bank.
16961 Important: There might be problems if the linker script has data sections
16962 across bank borders!
16964 \layout Subsubsection
16968 \labelwidthstring 00.00.0000
16980 -nodefaultlibs do not link default libraries when linking
16982 \labelwidthstring 00.00.0000
16994 -no-crt Don't link the default run-time modules
16996 \labelwidthstring 00.00.0000
17008 -use-crt= Use a custom run-time module instead of the defaults.
17009 \layout Subsubsection
17014 Debugging options enable extra debugging information in the output files.
17016 \labelwidthstring 00.00.0000
17028 -debug-xtra Similar to -
17039 \begin_inset LatexCommand \index{-\/-debug}
17043 , but dumps more information.
17045 \labelwidthstring 00.00.0000
17057 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17059 <source> is the name of the file compiled.
17061 \labelwidthstring 00.00.0000
17073 -pcode-verbose Enable pcode debugging information in translation.
17075 \labelwidthstring 00.00.0000
17087 -denable-peeps Force the usage of peepholes.
17090 \labelwidthstring 00.00.0000
17102 -gstack Trace push/pops for stack pointer overflow
17104 \labelwidthstring 00.00.0000
17116 -call-tree dump call tree in .calltree file
17119 Enviromental Variables
17122 There is a number of enviromental variables that can be used when running
17123 SDCC to enable certain optimizations or force a specific program behaviour.
17124 these variables are primarily for debugging purposes so they can be enabled/dis
17128 Currently there is only two such variables available:
17130 \labelwidthstring 00.00.0000
17132 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17133 bitfields is optimized by directly loading FSR0 with the address of the
17134 bitfield structure.
17135 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17137 This step saves data ram and code space for functions that perform heavy
17140 80 bytes of code space are saved when compiling malloc.c with this option).
17143 \labelwidthstring 00.00.0000
17145 NO_REG_OPT do not perform pCode registers optimization.
17146 This should be used for debugging purposes.
17147 In some where bugs in the pcode optimizer are found, users can benefit
17148 from temporarily disabling the optimizer until the bug is fixed.
17151 Preprocessor Macros
17154 PIC16 port defines the following preprocessor macros while translating a
17159 \begin_inset Tabular
17160 <lyxtabular version="3" rows="6" columns="2">
17162 <column alignment="center" valignment="top" leftline="true" width="0">
17163 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17164 <row topline="true" bottomline="true">
17165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17182 <row topline="true">
17183 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17191 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17196 Port identification
17200 <row topline="true">
17201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17219 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17224 Port identification (same as above)
17228 <row topline="true">
17229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17237 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17242 MCU Identification.
17247 is the microcontrol identification number, i.e.
17252 <row topline="true">
17253 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17276 MCU Identification (same as above)
17280 <row topline="true" bottomline="true">
17281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17289 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17294 nnn = SMALL or LARGE respectively according to the stack model used
17305 In addition the following macros are defined when calling assembler:
17309 \begin_inset Tabular
17310 <lyxtabular version="3" rows="4" columns="2">
17312 <column alignment="center" valignment="top" leftline="true" width="0">
17313 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17314 <row topline="true" bottomline="true">
17315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17323 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17332 <row topline="true">
17333 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17341 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17346 MCU Identification.
17351 is the microcontrol identification number, i.e.
17356 <row topline="true">
17357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17370 nnn = SMALL or LARGE respectively according to the memory model used for
17375 <row topline="true" bottomline="true">
17376 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17384 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17389 nnn = SMALL or LARGE respectively according to the stack model used
17404 \begin_inset LatexCommand \index{PIC16}
17408 port uses the following directories for searching header files and libraries.
17412 \begin_inset Tabular
17413 <lyxtabular version="3" rows="3" columns="4">
17415 <column alignment="center" valignment="top" leftline="true" width="0">
17416 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17417 <column alignment="center" valignment="top" width="0">
17418 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17419 <row topline="true" bottomline="true">
17420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17428 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17436 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17444 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17453 <row topline="true">
17454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17459 PREFIX/sdcc/include/pic16
17462 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17467 PIC16 specific headers
17470 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17478 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17487 <row topline="true" bottomline="true">
17488 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17493 PREFIX/sdcc/lib/pic16
17496 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17501 PIC16 specific libraries
17504 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17512 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17529 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17536 PIC16 port currently supports the following pragmas:
17538 \labelwidthstring 00.00.0000
17540 stack pragma stack forces the code generator to initialize the stack & frame
17541 pointers at a specific address.
17542 This is an adhoc solution for cases where no STACK directive is available
17543 in the linker script or gplink is not instructed to create a stack section.
17545 The stack pragma should be used only once in a project.
17546 Multiple pragmas may result in indeterminate behaviour of the program.
17552 The old format (ie.
17553 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17554 cross page boundaries (or even exceed the available data RAM) and crash
17556 Make sure that stack does not cross page boundaries when using the SMALL
17562 The format is as follows:
17565 #pragma stack bottom_address [stack_size]
17572 is the lower bound of the stack section.
17573 The stack pointer initially will point at address (bottom_address+stack_size-1).
17581 /* initializes stack of 100 bytes at RAM address 0x200 */
17584 #pragma stack 0x200 100
17587 If the stack_size field is omitted then a stack is created with the default
17589 This size might be enough for most programs, but its not enough for operations
17590 with deep function nesting or excessive stack usage.
17592 \labelwidthstring 00.00.0000
17596 This pragma is deprecated.
17597 Its use will cause a warning message to be issued.
17603 \labelwidthstring 00.00.0000
17605 code place a function symbol at static FLASH address
17613 /* place function test_func at 0x4000 */
17616 #pragma code test_func 0x4000
17620 \labelwidthstring 00.00.0000
17622 library instructs the linker to use a library module.
17627 #pragma library module_name
17634 can be any library or object file (including its path).
17635 Note that there are four reserved keywords which have special meaning.
17640 \begin_inset Tabular
17641 <lyxtabular version="3" rows="6" columns="3">
17643 <column alignment="center" valignment="top" leftline="true" width="0">
17644 <column alignment="block" valignment="top" leftline="true" width="20page%">
17645 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17646 <row topline="true" bottomline="true">
17647 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17655 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17672 <row topline="true">
17673 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17688 ignore all library pragmas
17691 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17702 <row topline="true">
17703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17713 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17721 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17734 <row topline="true">
17735 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17745 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17750 link the Math libarary
17753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17766 <row topline="true">
17767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17782 link the I/O library
17785 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17798 <row topline="true" bottomline="true">
17799 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17809 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17814 link the debug library
17817 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17836 * is the device number, i.e.
17837 452 for PIC18F452 MCU.
17840 This feature allows for linking with specific libraries withoug having to
17841 explicit name them in the command line.
17846 keyword will reject all modules specified by the library pragma.
17848 \labelwidthstring 00.00.0000
17850 udata pragma udata instructs the compiler to emit code so that linker will
17851 place a variable at a specific memory bank
17859 /* places variable foo at bank2 */
17862 #pragma udata bank2 foo
17868 In order for this pragma to work extra SECTION directives should be added
17869 in the .lkr script.
17870 In the following example a sample .lkr file is shown:
17875 // Sample linker script for the PIC18F452 processor
17881 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17884 CODEPAGE NAME=page START=0x2A END=0x7FFF
17887 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17890 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17893 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17896 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17899 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17904 DATABANK NAME=gpr0 START=0x80 END=0xFF
17907 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17910 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17913 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17916 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17919 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17922 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17927 SECTION NAME=CONFIG ROM=config
17932 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17935 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17938 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17941 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17944 SECTION NAME=bank4 RAM=gpr4
17947 SECTION NAME=bank5 RAM=gpr5
17950 The linker will recognise the section name set in the pragma statement and
17951 will position the variable at the memory bank set with the RAM field at
17952 the SECTION line in the linker script file.
17956 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17963 There is one main header file that can be included to the source files using
17970 This header file contains the definitions for the processor special registers,
17971 so it is necessary if the source accesses them.
17972 It can be included by adding the following line in the beginning of the
17976 #include <pic18fregs.h>
17979 The specific microcontroller is selected within the pic18fregs.h automatically,
17980 so the same source can be used with a variety of devices.
17986 The libraries that PIC16
17987 \begin_inset LatexCommand \index{PIC16}
17991 port depends on are the microcontroller device libraries which contain
17992 the symbol definitions for the microcontroller special function registers.
17993 These libraries have the format pic18fxxxx.lib, where
17997 is the microcontroller identification number.
17998 The specific library is selected automatically by the compiler at link
17999 stage according to the selected device.
18002 Libraries are created with gplib which is part of the gputils package
18003 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18008 \layout Subsubsection*
18010 Building the libraries
18013 Before using SDCC/pic16 there are some libraries that need to be compiled.
18014 This process is not done automatically by SDCC since not all users use
18015 SDCC for pic16 projects.
18016 So each user should compile the libraries separately.
18019 The steps to compile the pic16 libraries under Linux are:
18022 cd device/lib/pic16
18037 su -c 'make install' # install the libraries, you need the root password
18040 If you need to install the headers too, do:
18046 su -c 'make install' # install the headers, you need the root password
18049 There exist a special target to build the I/O libraries.
18050 This target is not automatically build because it will build the I/O library
18056 This way building will take quite a lot of time.
18057 Users are advised to edit the
18059 device/lib/pic16/pics.build
18061 file and then execute:
18070 The following memory models are supported by the PIC16 port:
18079 Memory model affects the default size of pointers within the source.
18080 The sizes are shown in the next table:
18084 \begin_inset Tabular
18085 <lyxtabular version="3" rows="3" columns="3">
18087 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18088 <column alignment="center" valignment="top" leftline="true" width="0">
18089 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18090 <row topline="true" bottomline="true">
18091 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18096 Pointer sizes according to memory model
18099 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18107 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18116 <row topline="true" bottomline="true">
18117 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18125 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18133 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18142 <row topline="true" bottomline="true">
18143 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18151 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18159 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18175 It is advisable that all sources within a project are compiled with the
18177 If one wants to override the default memory model, this can be done by
18178 declaring a pointer as
18187 Far selects large memory model's pointers, while near selects small memory
18191 The standard device libraries (see
18192 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18196 ) contain no reference to pointers, so they can be used with both memory
18203 The stack implementation for the PIC16 port uses two indirect registers,
18206 \labelwidthstring 00.00.0000
18208 FSR1 is assigned as stack pointer
18210 \labelwidthstring 00.00.0000
18212 FSR2 is assigned as frame pointer
18215 The following stack models are supported by the PIC16 port
18236 model means that only the FSRxL byte is used to access stack and frame,
18243 uses both FSRxL and FSRxH registers.
18244 The following table shows the stack/frame pointers sizes according to stack
18245 model and the maximum space they can address:
18249 \begin_inset Tabular
18250 <lyxtabular version="3" rows="3" columns="3">
18252 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18253 <column alignment="center" valignment="top" leftline="true" width="0">
18254 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18255 <row topline="true" bottomline="true">
18256 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18261 Stack & Frame pointer sizes according to stack model
18264 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18272 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18281 <row topline="true">
18282 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18290 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18298 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18307 <row topline="true" bottomline="true">
18308 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18316 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18324 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18344 stack model is currently not working properly throughout the code generator.
18345 So its use is not advised.
18346 Also there are some other points that need special care:
18351 Do not create stack sections with size more than one physical bank (that
18355 Stack sections should no cross physical bank limits (i.e.
18356 #pragma stack 0x50 0x100)
18359 These limitations are caused by the fact that only FSRxL is modified when
18360 using SMALL stack model, so no more than 256 bytes of stack can be used.
18361 This problem will disappear after LARGE model is fully implemented.
18367 In addition to the standard SDCC function keywords, PIC16 port makes available
18370 \labelwidthstring 00.00.0000
18372 wparam Use the WREG to pass one byte of the first function argument.
18373 This improves speed but you may not use this for functions with arguments
18374 that are called via function pointers, otherwise the first byte of the
18375 first parameter will get lost.
18379 void func_wparam(int a) wparam
18385 /* WREG hold the lower part of a */
18388 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18398 This keyword replaces the deprecated wparam pragma.
18400 \labelwidthstring 00.00.0000
18402 shadowregs When entering/exiting an ISR, it is possible to take advantage
18403 of the PIC18F hardware shadow registers which hold the values of WREG,
18404 STATUS and BSR registers.
18405 This can be done by adding the keyword
18413 keyword in the function's header.
18416 void isr_shadow(void) shadowregs interrupt 1
18432 instructs the code generator not to store/restore WREG, STATUS, BSR when
18433 entering/exiting the ISR.
18436 Function return values
18439 Return values from functions are placed to the appropriate registers following
18440 a modified Microchip policy optimized for SDCC.
18441 The following table shows these registers:
18445 \begin_inset Tabular
18446 <lyxtabular version="3" rows="6" columns="2">
18448 <column alignment="center" valignment="top" leftline="true" width="0">
18449 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18450 <row topline="true" bottomline="true">
18451 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18459 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18464 destination register
18468 <row topline="true">
18469 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18477 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18486 <row topline="true">
18487 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18495 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18504 <row topline="true">
18505 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18513 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18522 <row topline="true">
18523 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18531 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18536 FSR0L:PRODH:PRODL:WREG
18540 <row topline="true" bottomline="true">
18541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18554 on stack, FSR0 points to the beginning
18568 An interrupt servive routine (ISR) is declared using the
18575 void isr(void) interrupt
18593 is the interrupt number, which for PIC18F devices can be:
18597 \begin_inset Tabular
18598 <lyxtabular version="3" rows="4" columns="3">
18600 <column alignment="center" valignment="top" leftline="true" width="0">
18601 <column alignment="center" valignment="top" leftline="true" width="0">
18602 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18603 <row topline="true" bottomline="true">
18604 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18622 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18627 Interrupt Vector Address
18631 <row topline="true">
18632 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18648 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18657 <row topline="true">
18658 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18689 HIGH priority interrupts
18692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18701 <row topline="true" bottomline="true">
18702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18715 LOW priority interrupts
18718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18734 When generating assembly code for ISR the code generator places a
18740 Interrupt Vector Address
18742 which points at the genetated ISR.
18743 This single GOTO instruction is part of an automatically generated
18745 interrupt entry point
18748 The actuall ISR code is placed as normally would in the code space.
18749 Upon interrupt request, the GOTO instruction is executed which jumps to
18751 When declaring interrupt functions as _naked this GOTO instruction is
18756 The whole interrupt functions is therefore placed at the Interrupt Vector
18757 Address of the specific interrupt.
18758 This is not a problem for the LOW priority interrupts, but it is a problem
18759 for the RESET and the HIGH priority interrupts because code may be written
18760 at the next interrupt´s vector address and cause undeterminate program
18761 behaviour if that interrupt is raised.
18767 This is not a problem when
18770 this is a HIGH interrupt ISR and LOW interrupts are
18777 when the ISR is small enough not to reach the next interrupt´s vector address.
18787 is possible to be omitted.
18788 This way a function is generated similar to an ISR, but it is not assigned
18792 When entering an interrupt, currently the PIC16
18793 \begin_inset LatexCommand \index{PIC16}
18797 port automatically saves the following registers:
18809 PROD (PRODL and PRODH)
18812 FSR0 (FSR0L and FSR0H)
18815 These registers are restored upon return from the interrupt routine.
18821 NOTE that when the _naked attribute is specified for an interrupt routine,
18822 then NO registers are stored or restored.
18831 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18832 There are 3 types of generic pointers currently implemented data, code
18833 and eeprom pointers.
18834 They are differentiated by the value of the 7th and 6th bits of the upper
18839 \begin_inset Tabular
18840 <lyxtabular version="3" rows="5" columns="5">
18842 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18843 <column alignment="center" valignment="top" width="0">
18844 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18845 <column alignment="center" valignment="top" width="0">
18846 <column alignment="left" valignment="top" rightline="true" width="0">
18847 <row topline="true" bottomline="true">
18848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18877 rest of the pointer
18880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18889 <row topline="true" bottomline="true">
18890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18898 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18923 uuuuuu uuuuxxxx xxxxxxxx
18926 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18931 a 12-bit data pointer in data RAM memory
18935 <row bottomline="true">
18936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18969 uxxxxx xxxxxxxx xxxxxxxx
18972 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18977 a 21-bit code pointer in FLASH memory
18981 <row bottomline="true">
18982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18998 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19015 uuuuuu uuuuuuxx xxxxxxxx
19018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19023 a 10-bit eeprom pointer in EEPROM memory
19027 <row bottomline="true">
19028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19044 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19061 xxxxxx xxxxxxxx xxxxxxxx
19064 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19069 unimplemented pointer type
19080 Generic pointer are read and written with a set of library functions which
19081 read/write 1, 2, 3, 4 bytes.
19085 \layout Subsubsection
19087 Standard I/O Streams
19094 the type FILE is defined as:
19097 typedef char * FILE;
19100 This type is the stream type implemented I/O in the PIC18F devices.
19101 Also the standard input and output streams are declared in stdio.h:
19104 extern FILE * stdin;
19107 extern FILE * stdout;
19110 The FILE type is actually a generic pointer which defines one more type
19111 of generic pointers, the
19116 This new type has the format:
19120 \begin_inset Tabular
19121 <lyxtabular version="3" rows="2" columns="7">
19123 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19124 <column alignment="center" valignment="top" width="0">
19125 <column alignment="center" valignment="top" leftline="true" width="0">
19126 <column alignment="center" valignment="top" leftline="true" width="0">
19127 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19128 <column alignment="center" valignment="top" width="0">
19129 <column alignment="left" valignment="top" rightline="true" width="0">
19130 <row topline="true" bottomline="true">
19131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19139 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19171 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19176 rest of the pointer
19179 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19188 <row topline="true" bottomline="true">
19189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19197 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19205 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19213 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19221 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19229 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19246 upper byte high nubble is 0x2n, the rest are zeroes
19257 Currently implemented there are 3 types of streams defined:
19261 \begin_inset Tabular
19262 <lyxtabular version="3" rows="4" columns="4">
19264 <column alignment="center" valignment="top" leftline="true" width="0">
19265 <column alignment="center" valignment="top" leftline="true" width="0">
19266 <column alignment="center" valignment="top" leftline="true" width="0">
19267 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19268 <row topline="true" bottomline="true">
19269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19302 <row topline="true">
19303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19334 Writes/Reads characters via the USART peripheral
19338 <row topline="true">
19339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19347 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19357 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19365 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19370 Writes/Reads characters via the MSSP peripheral
19374 <row topline="true" bottomline="true">
19375 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19383 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19393 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19401 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19406 Writes/Reads characters via used defined functions
19417 The stream identifiers are declared as macros in the stdio.h header.
19420 In the libc library there exist the functions that are used to write to
19421 each of the above streams.
19424 \labelwidthstring 00.00.0000
19436 _stream_usart_putchar writes a character at the USART stream
19438 \labelwidthstring 00.00.0000
19450 _stream_mssp_putchar writes a character at the MSSP stream
19452 \labelwidthstring 00.00.0000
19454 putchar dummy function.
19455 This writes a character to a user specified manner.
19458 In order to increase performance
19462 is declared in stdio.h as having its parameter in WREG (it has the wparam
19464 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19465 in a user-friendly way.
19470 is the name of the variable that holds the character to print.
19471 An example follows:
19474 #include <pic18fregs.h>
19486 PORTA = c; /* dump character c to PORTA */
19499 stdout = STREAM_USER; /* this is not necessery, since stdout points
19502 * by default to STREAM_USER */
19505 printf (¨This is a printf test
19513 \layout Subsubsection
19518 PIC16 contains an implementation of the printf-family of functions.
19519 There exist the following functions:
19522 extern unsigned int sprintf(char *buf, char *fmt, ...);
19525 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19530 extern unsigned int printf(char *fmt, ...);
19533 extern unsigned int vprintf(char *fmt, va_lista ap);
19538 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19541 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19544 For sprintf and vsprintf
19548 should normally be a data pointer where the resulting string will be placed.
19549 No range checking is done so the user should allocate the necessery buffer.
19550 For fprintf and vfprintf
19554 should be a stream pointer (i.e.
19555 stdout, STREAM_MSSP, etc...).
19556 \layout Subsubsection
19561 The PIC18F family of microcontrollers supports a number of interrupt sources.
19562 A list of these interrupts is shown in the following table:
19566 \begin_inset Tabular
19567 <lyxtabular version="3" rows="11" columns="4">
19569 <column alignment="left" valignment="top" leftline="true" width="0">
19570 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19571 <column alignment="left" valignment="top" leftline="true" width="0">
19572 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19573 <row topline="true" bottomline="true">
19574 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19582 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19590 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19598 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19607 <row topline="true">
19608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19616 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19621 PORTB change interrupt
19624 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19632 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19637 EEPROM/FLASH write complete interrupt
19641 <row topline="true">
19642 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19655 INT0 external interrupt
19658 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19666 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19671 Bus collision interrupt
19675 <row topline="true">
19676 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19684 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19689 INT1 external interrupt
19692 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19700 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19705 Low voltage detect interrupt
19709 <row topline="true">
19710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19723 INT2 external interrupt
19726 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19734 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19739 Parallel slave port interrupt
19743 <row topline="true">
19744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19757 CCP1 module interrupt
19760 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19768 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19773 AD convertion complete interrupt
19777 <row topline="true">
19778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19791 CCP2 module interrupt
19794 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19802 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19807 USART receive interrupt
19811 <row topline="true">
19812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19820 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19825 TMR0 overflow interrupt
19828 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19836 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19841 USART transmit interrupt
19845 <row topline="true">
19846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19859 TMR1 overflow interrupt
19862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19870 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19875 SSP receive/transmit interrupt
19879 <row topline="true">
19880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19893 TMR2 matches PR2 interrupt
19896 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19911 <row topline="true" bottomline="true">
19912 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19920 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19925 TMR3 overflow interrupt
19928 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19935 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19950 The prototypes for these names are defined in the header file
19957 In order to simplify signal handling, a number of macros is provided:
19959 \labelwidthstring 00.00.0000
19961 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19962 high priority interrupts.
19967 is the function name to use.
19969 \labelwidthstring 00.00.0000
19971 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19972 low priority interrupt.
19977 is the function name to use.
19979 \labelwidthstring 00.00.0000
19981 DEF_HANDLER(sig,handler) define a handler for signal
19985 \labelwidthstring 00.00.0000
19987 END_DEF end the declaration of the dispatch table.
19990 Additionally there are two more macros to simplify the declaration of the
19993 \labelwidthstring 00.00.0000
19997 SIGHANDLER(handler)
19999 this declares the function prototype for the
20005 \labelwidthstring 00.00.0000
20007 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20010 An example of using the macros above is shown below:
20013 #include <pic18fregs.h>
20016 #include <signal.h>
20020 DEF_INTHIGH(high_int)
20023 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20026 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20033 SIGHANDLER(_tmr0_handler)
20039 /* action to be taken when timer 0 overflows */
20046 SIGHANDLERNAKED(_bcol_handler)
20055 /* action to be taken when bus collision occurs */
20071 Special care should be taken when using the above scheme:
20074 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20077 when declaring SIGHANDLERNAKED handler never forget to use
20081 for proper returning.
20087 Here you can find some general tips for compiling programs with SDCC/pic16.
20088 \layout Subsubsection
20093 The default stack size (that is 64 bytes) probably is enough for many programs.
20094 One must take care that when there are many levels of function nesting,
20095 or there is excessive usage of stack, its size should be extended.
20096 An example of such a case is the printf/sprintf family of functions.
20097 If you encounter problems like not being able to print integers, then you
20098 need to set the stack size around the maximum (256 for small stack model).
20099 The following diagram shows what happens when calling printf to print an
20103 printf () --> ltoa () --> ultoa () --> divschar ()
20106 It is should be understood that stack is easily consumed when calling complicate
20108 Using command line arguments like -
20118 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20120 Other ways to reduce stack usage may exist.
20123 Debugging with SDCDB
20124 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20129 \begin_inset LatexCommand \index{sdcdb (debugger)}
20136 SDCC is distributed with a source level debugger
20137 \begin_inset LatexCommand \index{Debugger}
20142 The debugger uses a command line interface, the command repertoire of the
20143 debugger has been kept as close to gdb
20144 \begin_inset LatexCommand \index{gdb}
20148 (the GNU debugger) as possible.
20149 The configuration and build process is part of the standard compiler installati
20150 on, which also builds and installs the debugger in the target directory
20151 specified during configuration.
20152 The debugger allows you debug BOTH at the C source and at the ASM source
20154 Sdcdb is currently not available on Win32 platforms.
20157 Compiling for Debugging
20171 \begin_inset LatexCommand \index{-\/-debug}
20175 option must be specified for all files for which debug information is to
20177 The complier generates a .adb file for each of these files.
20178 The linker creates the .cdb
20179 \begin_inset LatexCommand \index{<file>.cdb}
20184 \begin_inset LatexCommand \index{<file>.adb}
20188 files and the address information.
20189 This .cdb is used by the debugger.
20192 How the Debugger Works
20205 -debug option is specified the compiler generates extra symbol information
20206 some of which are put into the assembler source and some are put into the
20208 Then the linker creates the .cdb file from the individual .adb files with
20209 the address information for the symbols.
20210 The debugger reads the symbolic information generated by the compiler &
20211 the address information generated by the linker.
20212 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20213 execution is controlled by the debugger.
20214 When a command is issued for the debugger, it translates it into appropriate
20215 commands for the simulator.
20218 Starting the Debugger
20221 The debugger can be started using the following command line.
20222 (Assume the file you are debugging has the file name foo).
20236 The debugger will look for the following files.
20239 foo.c - the source file.
20242 foo.cdb - the debugger symbol information file.
20245 foo.ihx - the Intel hex format
20246 \begin_inset LatexCommand \index{Intel hex format}
20253 Command Line Options
20266 -directory=<source file directory> this option can used to specify the directory
20268 The debugger will look into the directory list specified for source, cdb
20270 The items in the directory list must be separated by ':', e.g.
20271 if the source files can be in the directories /home/src1 and /home/src2,
20282 -directory option should be -
20292 -directory=/home/src1:/home/src2.
20293 Note there can be no spaces in the option.
20297 -cd <directory> - change to the <directory>.
20300 -fullname - used by GUI front ends.
20303 -cpu <cpu-type> - this argument is passed to the simulator please see the
20304 simulator docs for details.
20307 -X <Clock frequency > this options is passed to the simulator please see
20308 the simulator docs for details.
20311 -s <serial port file> passed to simulator see the simulator docs for details.
20314 -S <serial in,out> passed to simulator see the simulator docs for details.
20317 -k <port number> passed to simulator see the simulator docs for details.
20323 As mentioned earlier the command interface for the debugger has been deliberatel
20324 y kept as close the GNU debugger gdb, as possible.
20325 This will help the integration with existing graphical user interfaces
20326 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20327 If you use a graphical user interface for the debugger you can skip this
20329 \layout Subsubsection*
20331 break [line | file:line | function | file:function]
20334 Set breakpoint at specified line or function:
20343 sdcdb>break foo.c:100
20345 sdcdb>break funcfoo
20347 sdcdb>break foo.c:funcfoo
20348 \layout Subsubsection*
20350 clear [line | file:line | function | file:function ]
20353 Clear breakpoint at specified line or function:
20362 sdcdb>clear foo.c:100
20364 sdcdb>clear funcfoo
20366 sdcdb>clear foo.c:funcfoo
20367 \layout Subsubsection*
20372 Continue program being debugged, after breakpoint.
20373 \layout Subsubsection*
20378 Execute till the end of the current function.
20379 \layout Subsubsection*
20384 Delete breakpoint number 'n'.
20385 If used without any option clear ALL user defined break points.
20386 \layout Subsubsection*
20388 info [break | stack | frame | registers ]
20391 info break - list all breakpoints
20394 info stack - show the function call stack.
20397 info frame - show information about the current execution frame.
20400 info registers - show content of all registers.
20401 \layout Subsubsection*
20406 Step program until it reaches a different source line.
20407 Note: pressing <return> repeats the last command.
20408 \layout Subsubsection*
20413 Step program, proceeding through subroutine calls.
20414 \layout Subsubsection*
20419 Start debugged program.
20420 \layout Subsubsection*
20425 Print type information of the variable.
20426 \layout Subsubsection*
20431 print value of variable.
20432 \layout Subsubsection*
20437 load the given file name.
20438 Note this is an alternate method of loading file for debugging.
20439 \layout Subsubsection*
20444 print information about current frame.
20445 \layout Subsubsection*
20450 Toggle between C source & assembly source.
20451 \layout Subsubsection*
20453 ! simulator command
20456 Send the string following '!' to the simulator, the simulator response is
20458 Note the debugger does not interpret the command being sent to the simulator,
20459 so if a command like 'go' is sent the debugger can loose its execution
20460 context and may display incorrect values.
20461 \layout Subsubsection*
20468 My name is Bobby Brown"
20471 Interfacing with DDD
20474 The screenshot was converted from png to eps with:
20475 \begin_inset Quotes sld
20478 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20479 \begin_inset Quotes srd
20482 which produces a pretty compact eps file which is free from compression
20486 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20487 as this broke the build system on Sourceforge (pdf-file was broken).
20493 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
20499 shows a screenshot of a debugging session with DDD
20500 \begin_inset LatexCommand \index{DDD (debugger)}
20504 (Unix only) on a simulated 8032.
20505 The debugging session might not run as smoothly as the screenshot suggests.
20506 The debugger allows setting of breakpoints, displaying and changing variables,
20507 single stepping through C and assembler code.
20510 The source was compiled with
20533 -debug ddd_example.c
20546 and DDD was invoked with
20553 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20556 Interfacing with XEmacs
20557 \begin_inset LatexCommand \index{XEmacs}
20562 \begin_inset LatexCommand \index{Emacs}
20569 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20570 sdcdb.el and sdcdbsrc.el.
20571 These two files can be found in the $(prefix)/bin directory after the installat
20573 These files need to be loaded into XEmacs for the interface to work.
20574 This can be done at XEmacs startup time by inserting the following into
20575 your '.xemacs' file (which can be found in your HOME directory):
20581 (load-file sdcdbsrc.el)
20587 .xemacs is a lisp file so the () around the command is REQUIRED.
20588 The files can also be loaded dynamically while XEmacs is running, set the
20589 environment variable 'EMACSLOADPATH' to the installation bin directory
20590 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20591 To start the interface enter the following command:
20605 You will prompted to enter the file name to be debugged.
20610 The command line options that are passed to the simulator directly are bound
20611 to default values in the file sdcdbsrc.el.
20612 The variables are listed below, these values maybe changed as required.
20615 sdcdbsrc-cpu-type '51
20618 sdcdbsrc-frequency '11059200
20621 sdcdbsrc-serial nil
20624 The following is a list of key mapping for the debugger interface.
20635 ;;key\SpecialChar ~
20649 binding\SpecialChar ~
20673 ;;---\SpecialChar ~
20687 -------\SpecialChar ~
20729 sdcdb-next-from-src\SpecialChar ~
20757 sdcdb-back-from-src\SpecialChar ~
20785 sdcdb-cont-from-src\SpecialChar ~
20795 SDCDB continue command
20813 sdcdb-step-from-src\SpecialChar ~
20841 sdcdb-whatis-c-sexp\SpecialChar ~
20851 SDCDB ptypecommand for data at
20918 sdcdbsrc-delete\SpecialChar ~
20932 SDCDB Delete all breakpoints if no arg
20981 given or delete arg (C-u arg x)
20999 sdcdbsrc-frame\SpecialChar ~
21014 SDCDB Display current frame if no arg,
21063 given or display frame arg
21130 sdcdbsrc-goto-sdcdb\SpecialChar ~
21140 Goto the SDCDB output buffer
21158 sdcdb-print-c-sexp\SpecialChar ~
21169 SDCDB print command for data at
21236 sdcdbsrc-goto-sdcdb\SpecialChar ~
21246 Goto the SDCDB output buffer
21264 sdcdbsrc-mode\SpecialChar ~
21280 Toggles Sdcdbsrc mode (turns it off)
21295 sdcdb-finish-from-src\SpecialChar ~
21303 SDCDB finish command
21318 sdcdb-break\SpecialChar ~
21336 Set break for line with point
21351 sdcdbsrc-mode\SpecialChar ~
21367 Toggle Sdcdbsrc mode
21382 sdcdbsrc-srcmode\SpecialChar ~
21405 Here are a few guidelines that will help the compiler generate more efficient
21406 code, some of the tips are specific to this compiler others are generally
21407 good programming practice.
21410 Use the smallest data type to represent your data-value.
21411 If it is known in advance that the value is going to be less than 256 then
21412 use an 'unsigned char' instead of a 'short' or 'int'.
21413 Please note, that ANSI C requires both signed and unsigned chars to be
21414 promoted to 'signed int' before doing any operation.
21416 \begin_inset LatexCommand \index{type promotion}
21420 can be omitted, if the result is the same.
21421 The effect of the promotion rules together with the sign-extension is often
21428 unsigned char uc = 0xfe;
21430 if (uc * uc < 0) /* this is true! */
21449 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21459 (unsigned char) -12 / (signed char) -3 = ...
21462 No, the result is not 4:
21467 (int) (unsigned char) -12 / (int) (signed char) -3 =
21469 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21471 (int) 0x00f4 / (int) 0xfffd =
21473 (int) 0x00f4 / (int) 0xfffd =
21475 (int) 244 / (int) -3 =
21477 (int) -81 = (int) 0xffaf;
21480 Don't complain, that gcc gives you a different result.
21481 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21482 Therefore the results are different.
21485 \begin_inset Quotes sld
21489 \begin_inset Quotes srd
21495 If well-defined overflow characteristics are important and negative values
21496 are not, or if you want to steer clear of sign-extension problems when
21497 manipulating bits or bytes, use one of the corresponding unsigned types.
21498 (Beware when mixing signed and unsigned values in expressions, though.)
21500 Although character types (especially unsigned char) can be used as "tiny"
21501 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21502 ble sign extension and increased code size.
21506 Use unsigned when it is known in advance that the value is not going to
21508 This helps especially if you are doing division or multiplication, bit-shifting
21509 or are using an array index.
21512 NEVER jump into a LOOP.
21515 Declare the variables to be local
21516 \begin_inset LatexCommand \index{local variables}
21520 whenever possible, especially loop control variables (induction).
21523 Since the compiler does not always do implicit integral promotion, the programme
21524 r should do an explicit cast when integral promotion is required.
21527 Reducing the size of division, multiplication & modulus operations can reduce
21528 code size substantially.
21529 Take the following code for example.
21535 foobar(unsigned int p1, unsigned char ch)
21543 unsigned char ch1 = p1 % ch ;
21554 For the modulus operation the variable ch will be promoted to unsigned int
21555 first then the modulus operation will be performed (this will lead to a
21556 call to support routine _moduint()), and the result will be casted to a
21558 If the code is changed to
21563 foobar(unsigned int p1, unsigned char ch)
21571 unsigned char ch1 = (unsigned char)p1 % ch ;
21582 It would substantially reduce the code generated (future versions of the
21583 compiler will be smart enough to detect such optimization opportunities).
21587 Have a look at the assembly listing to get a
21588 \begin_inset Quotes sld
21592 \begin_inset Quotes srd
21595 for the code generation.
21599 \begin_inset LatexCommand \index{Tools}
21603 included in the distribution
21607 \begin_inset Tabular
21608 <lyxtabular version="3" rows="12" columns="3">
21610 <column alignment="center" valignment="top" leftline="true" width="0pt">
21611 <column alignment="center" valignment="top" leftline="true" width="0pt">
21612 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21613 <row topline="true" bottomline="true">
21614 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21622 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21630 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21639 <row topline="true">
21640 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21653 Simulator for various architectures
21656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21665 <row topline="true">
21666 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21674 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21679 header file conversion
21682 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21687 sdcc/support/scripts
21691 <row topline="true">
21692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21700 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21705 header file conversion
21708 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21713 sdcc/support/scripts
21717 <row topline="true">
21718 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21726 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21734 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21752 <row topline="true">
21753 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21761 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21769 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21787 <row topline="true">
21788 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21822 <row topline="true">
21823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21831 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21839 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21857 <row topline="true">
21858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21866 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21874 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21892 <row topline="true">
21893 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21901 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21909 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21927 <row topline="true">
21928 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21944 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21962 <row topline="true" bottomline="true">
21963 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21971 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21979 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22007 \begin_inset LatexCommand \index{Documentation}
22011 included in the distribution
22015 \begin_inset Tabular
22016 <lyxtabular version="3" rows="10" columns="2">
22018 <column alignment="left" valignment="top" leftline="true" width="0">
22019 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22020 <row topline="true" bottomline="true">
22021 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22029 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22034 Where to get / filename
22038 <row topline="true">
22039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22044 SDCC Compiler User Guide
22047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22052 You're reading it right now
22056 <row topline="true">
22057 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22065 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22074 <row topline="true">
22075 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22081 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22086 \begin_inset LatexCommand \index{Assembler documentation}
22090 Assemblers and ASLINK
22091 \begin_inset LatexCommand \index{aslink}
22096 \begin_inset LatexCommand \index{Linker documentation}
22103 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22108 sdcc/as/doc/asxhtm.html
22112 <row topline="true">
22113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22118 SDCC regression test
22119 \begin_inset LatexCommand \index{Regression test}
22126 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22131 sdcc/doc/test_suite_spec.pdf
22135 <row topline="true">
22136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22153 <row topline="true">
22154 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22159 Notes on debugging with sdcdb
22160 \begin_inset LatexCommand \index{sdcdb (debugger)}
22167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22172 sdcc/debugger/README
22176 <row topline="true">
22177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22182 Software simulator for microcontrollers
22185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22212 <row topline="true">
22213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22218 Temporary notes on the pic16
22219 \begin_inset LatexCommand \index{PIC16}
22226 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22231 sdcc/src/pic16/NOTES
22235 <row topline="true" bottomline="true">
22236 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22241 SDCC internal documentation (debugging file format)
22244 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22280 Related open source tools
22281 \begin_inset LatexCommand \index{Related tools}
22289 \begin_inset Tabular
22290 <lyxtabular version="3" rows="11" columns="3">
22292 <column alignment="center" valignment="top" leftline="true" width="0pt">
22293 <column alignment="block" valignment="top" leftline="true" width="30line%">
22294 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22295 <row topline="true" bottomline="true">
22296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22312 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22321 <row topline="true">
22322 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22328 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22349 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22357 <row topline="true">
22358 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22364 \begin_inset LatexCommand \index{gputils (pic tools)}
22371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22379 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22385 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22393 <row topline="true">
22394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22410 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22416 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22424 <row topline="true">
22425 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22431 \begin_inset LatexCommand \index{indent (source formatting tool)}
22438 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22443 Formats C source - Master of the white spaces
22446 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22452 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22460 <row topline="true">
22461 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22467 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22474 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22479 Object file conversion, checksumming, ...
22482 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22488 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22496 <row topline="true">
22497 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22503 \begin_inset LatexCommand \index{objdump (tool)}
22510 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22515 Object file conversion, ...
22518 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22523 Part of binutils (should be there anyway)
22527 <row topline="true">
22528 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22534 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22546 Source code documentation system
22549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22555 \begin_inset LatexCommand \url{http://www.doxygen.org}
22563 <row topline="true">
22564 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22572 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22577 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22580 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22586 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22594 <row topline="true">
22595 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22601 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22613 Statically checks c sources (see
22614 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22621 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22627 \begin_inset LatexCommand \url{http://www.splint.org}
22635 <row topline="true" bottomline="true">
22636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22642 \begin_inset LatexCommand \index{ddd (debugger)}
22649 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22654 Debugger, serves nicely as GUI to sdcdb
22655 \begin_inset LatexCommand \index{sdcdb (debugger)}
22662 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22668 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22685 Related documentation / recommended reading
22689 \begin_inset Tabular
22690 <lyxtabular version="3" rows="6" columns="3">
22692 <column alignment="center" valignment="top" leftline="true" width="0pt">
22693 <column alignment="block" valignment="top" leftline="true" width="30line%">
22694 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22695 <row topline="true" bottomline="true">
22696 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22704 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22712 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22721 <row topline="true">
22722 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22739 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22745 \begin_inset LatexCommand \index{C Reference card}
22752 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22758 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22766 <row topline="true">
22767 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22783 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22789 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22797 <row topline="true">
22798 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22805 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22810 Latest datasheet of the target CPU
22813 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22822 <row topline="true">
22823 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22835 Revision history of datasheet
22838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22847 <row topline="true" bottomline="true">
22848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22858 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22863 Advanced Compiler Design and Implementation
22866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22871 bookstore (very dedicated, probably read other books first)
22887 Some questions answered, some pointers given - it might be time to in turn
22895 can you solve your project with the selected microcontroller? Would you
22896 find out early or rather late that your target is too small/slow/whatever?
22897 Can you switch to a slightly better device if it doesn't fit?
22900 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22901 and/or another programming language be more adequate? Would an operating
22902 system on the target device help?
22905 if you solved the problem, will the marketing department be happy?
22908 if the marketing department is happy, will customers be happy?
22911 if you're the project manager, marketing department and maybe even the customer
22912 in one person, have you tried to see the project from the outside?
22915 is the project done if you think it is done? Or is just that other interface/pro
22916 tocol/feature/configuration/option missing? How about website, manual(s),
22917 internationali(z|s)ation, packaging, labels, 2nd source for components,
22918 electromagnetic compatability/interference, documentation for production,
22919 production test software, update mechanism, patent issues?
22922 is your project adequately positioned in that magic triangle: fame, fortune,
22926 Maybe not all answers to these questions are known and some answers may
22931 , nevertheless knowing these questions may help you to avoid burnout
22937 burnout is bad for electronic devices, programmers and motorcycle tyres
22941 Chances are you didn't want to hear some of them...
22945 \begin_inset LatexCommand \index{Support}
22952 SDCC has grown to be a large project.
22953 The compiler alone (without the preprocessor, assembler and linker) is
22954 well over 100,000 lines of code (blank stripped).
22955 The open source nature of this project is a key to its continued growth
22957 You gain the benefit and support of many active software developers and
22959 Is SDCC perfect? No, that's why we need your help.
22960 The developers take pride in fixing reported bugs.
22961 You can help by reporting the bugs and helping other SDCC users.
22962 There are lots of ways to contribute, and we encourage you to take part
22963 in making SDCC a great software package.
22967 The SDCC project is hosted on the SDCC sourceforge site at
22968 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22973 You'll find the complete set of mailing lists
22974 \begin_inset LatexCommand \index{Mailing list(s)}
22978 , forums, bug reporting system, patch submission
22979 \begin_inset LatexCommand \index{Patch submission}
22984 \begin_inset LatexCommand \index{download}
22988 area and Subversion code repository
22989 \begin_inset LatexCommand \index{Subversion code repository}
22997 \begin_inset LatexCommand \index{Bug reporting}
23002 \begin_inset LatexCommand \index{Reporting bugs}
23009 The recommended way of reporting bugs is using the infrastructure of the
23011 You can follow the status of bug reports there and have an overview about
23015 Bug reports are automatically forwarded to the developer mailing list and
23016 will be fixed ASAP.
23017 When reporting a bug, it is very useful to include a small test program
23018 (the smaller the better) which reproduces the problem.
23019 If you can isolate the problem by looking at the generated assembly code,
23020 this can be very helpful.
23021 Compiling your program with the -
23032 \begin_inset LatexCommand \index{-\/-dumpall}
23036 option can sometimes be useful in locating optimization problems.
23037 When reporting a bug please maker sure you:
23040 Attach the code you are compiling with SDCC.
23044 Specify the exact command you use to run SDCC, or attach your Makefile.
23048 Specify the SDCC version (type "
23054 "), your platform, and operating system.
23058 Provide an exact copy of any error message or incorrect output.
23062 Put something meaningful in the subject of your message.
23065 Please attempt to include these 5 important parts, as applicable, in all
23066 requests for support or when reporting any problems or bugs with SDCC.
23067 Though this will make your message lengthy, it will greatly improve your
23068 chance that SDCC users and developers will be able to help you.
23069 Some SDCC developers are frustrated by bug reports without code provided
23070 that they can use to reproduce and ultimately fix the problem, so please
23071 be sure to provide sample code if you are reporting a bug!
23074 Please have a short check that you are using a recent version of SDCC and
23075 the bug is not yet known.
23076 This is the link for reporting bugs:
23077 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23084 Requesting Features
23085 \begin_inset LatexCommand \label{sub:Requesting-Features}
23090 \begin_inset LatexCommand \index{Feature request}
23095 \begin_inset LatexCommand \index{Requesting features}
23102 Like bug reports feature requests are forwarded to the developer mailing
23104 This is the link for requesting features:
23105 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23115 Like bug reports contributed patches are forwarded to the developer mailing
23117 This is the link for submitting patches
23118 \begin_inset LatexCommand \index{Patch submission}
23123 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23130 You need to specify some parameters to the
23134 command for the patches to be useful.
23135 If you modified more than one file a patch created f.e.
23140 \begin_inset Quotes sld
23143 diff -Naur unmodified_directory modified_directory >my_changes.patch
23144 \begin_inset Quotes srd
23150 will be fine, otherwise
23154 \begin_inset Quotes sld
23157 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23158 \begin_inset Quotes srd
23171 These links should take you directly to the
23172 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23182 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23183 automated messages (mid 2003)
23187 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23192 \begin_inset LatexCommand \index{Mailing list(s)}
23196 and forums are archived and searchable so if you are lucky someone already
23197 had a similar problem.
23198 While mails to the lists themselves are delivered promptly their web front
23199 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23200 if you're seriously using SDCC please consider subscribing to the lists.
23206 You can follow the status of the Subversion version
23207 \begin_inset LatexCommand \index{version}
23211 of SDCC by watching the Changelog
23212 \begin_inset LatexCommand \index{Changelog}
23216 in the Subversion repository
23221 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
23229 \begin_inset LatexCommand \index{Release policy}
23236 Historically there often were long delays between official releases and
23237 the sourceforge download area tends to get not updated at all.
23238 Excuses in the past might have referred to problems with live range analysis,
23239 but as this was fixed a while ago, the current problem is that another
23240 excuse has to be found.
23241 Kidding aside, we have to get better there! On the other hand there are
23242 daily snapshots available at
23243 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23247 , and you can always build the very last version (hopefully with many bugs
23248 fixed, and features added) from the source code available at
23249 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23257 \begin_inset LatexCommand \index{Examples}
23264 You'll find some small examples in the directory
23266 sdcc/device/examples/.
23269 More examples and libraries are available at
23271 The SDCC Open Knowledge Resource
23272 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23279 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23286 I did insert a reference to Paul's web site here although it seems rather
23287 dedicated to a specific 8032 board (I think it's okay because it f.e.
23288 shows LCD/Harddisc interface and has a free 8051 monitor.
23289 Independent 8032 board vendors face hard competition of heavily subsidized
23290 development boards anyway).
23293 Maybe we should include some links to real world applications.
23294 Preferably pointer to pointers (one for each architecture) so this stays
23299 \begin_inset LatexCommand \index{Quality control}
23306 The compiler is passed through nightly compile and build checks.
23312 \begin_inset LatexCommand \index{Regression test}
23316 check that SDCC itself compiles flawlessly on several platforms and checks
23317 the quality of the code generated by SDCC by running the code through simulator
23319 There is a separate document
23322 \begin_inset LatexCommand \index{Test suite}
23331 You'll find the test code in the directory
23333 sdcc/support/regression
23336 You can run these tests manually by running
23340 in this directory (or f.e.
23345 \begin_inset Quotes sld
23349 \begin_inset Quotes srd
23355 if you don't want to run the complete tests).
23356 The test code might also be interesting if you want to look for examples
23357 \begin_inset LatexCommand \index{Examples}
23361 checking corner cases of SDCC or if you plan to submit patches
23362 \begin_inset LatexCommand \index{Patch submission}
23369 The pic port uses a different set of regression tests, you'll find them
23372 sdcc/src/regression
23377 Use of SDCC in Education
23390 the phrase "use in education" might evoke the association "
23394 fit for use in education".
23395 This connotation is not intended but nevertheless risked as the licensing
23396 of SDCC makes it difficult to offer educational discounts
23400 If your rationales are to:
23403 give students a chance to understand the
23407 steps of code generation
23410 have a curriculum that can be extended for years.
23411 Then you could use an fpga board as target and your curriculum will seamlessly
23412 extend from logic synthesis (
23413 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
23418 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
23422 ), over assembly programming, to C to FPGA compilers (
23423 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
23430 be able to insert excursions about skills like using a revision control
23431 system, submitting/applying patches, using a type-setting (as opposed to
23432 word-processing) engine LyX/LaTeX, using
23433 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
23438 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
23442 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
23443 Source Software, CPU simulation, compiler regression tests
23444 \begin_inset LatexCommand \index{Regression test}
23451 And if there should be a shortage of ideas then you can always point students
23452 to the ever-growing feature request list
23453 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23460 not tie students to a specific host platform and instead allow them to use
23465 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
23467 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
23474 not encourage students to use illegal copies of educational software
23477 be immune to licensing/availability/price changes of the chosen tool chain
23480 be able to change to a new target platform without having to adopt a new
23484 have complete control over and insight into the tool chain
23487 make your students aware about the pros and cons of open source software
23491 give back to the public as you are probably at least partially publically
23495 give students a chance to publically prove their skills and to possibly
23496 see a world wide impact
23499 then SDCC is probably among the first choices.
23500 Well, probably SDCC might be the only choice.
23503 SDCC Technical Data
23507 \begin_inset LatexCommand \index{Optimizations}
23514 SDCC performs a host of standard optimizations in addition to some MCU specific
23519 Sub-expression Elimination
23520 \begin_inset LatexCommand \index{Subexpression elimination}
23527 The compiler does local and
23553 will be translated to
23565 Some subexpressions are not as obvious as the above example, e.g.:
23575 In this case the address arithmetic a->b[i] will be computed only once;
23576 the equivalent code in C would be.
23588 The compiler will try to keep these temporary variables in registers.
23591 Dead-Code Elimination
23592 \begin_inset LatexCommand \index{Dead-code elimination}
23613 i = 1; \SpecialChar ~
23622 global = 1;\SpecialChar ~
23635 global = 3;\SpecialChar ~
23660 \begin_inset LatexCommand \index{Copy propagation}
23716 Note: the dead stores created by this copy propagation will be eliminated
23717 by dead-code elimination.
23721 \begin_inset LatexCommand \index{Loop optimization}
23726 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23733 Two types of loop optimizations are done by SDCC
23741 of loop induction variables.
23742 In addition to the strength reduction the optimizer marks the induction
23743 variables and the register allocator tries to keep the induction variables
23744 in registers for the duration of the loop.
23745 Because of this preference of the register allocator
23746 \begin_inset LatexCommand \index{Register allocation}
23750 , loop induction optimization causes an increase in register pressure, which
23751 may cause unwanted spilling of other temporary variables into the stack
23752 \begin_inset LatexCommand \index{stack}
23757 The compiler will generate a warning message when it is forced to allocate
23758 extra space either on the stack or data space.
23759 If this extra space allocation is undesirable then induction optimization
23760 can be eliminated either for the entire source file (with -
23770 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23772 \begin_inset LatexCommand \index{\#pragma noinduction}
23785 for (i = 0 ; i < 100 ; i ++)
23801 for (i = 0; i < 100; i++)
23810 As mentioned previously some loop invariants are not as apparent, all static
23811 address computations are also moved out of the loop.
23816 \begin_inset LatexCommand \index{Strength reduction}
23820 , this optimization substitutes an expression by a cheaper expression:
23825 for (i=0;i < 100; i++)
23843 for (i=0;i< 100;i++) {
23849 ar[itemp1] = itemp2;
23866 The more expensive multiplication
23867 \begin_inset LatexCommand \index{Multiplication}
23871 is changed to a less expensive addition.
23875 \begin_inset LatexCommand \index{Loop reversing}
23882 This optimization is done to reduce the overhead of checking loop boundaries
23883 for every iteration.
23884 Some simple loops can be reversed and implemented using a
23885 \begin_inset Quotes eld
23888 decrement and jump if not zero
23889 \begin_inset Quotes erd
23893 SDCC checks for the following criterion to determine if a loop is reversible
23894 (note: more sophisticated compilers use data-dependency analysis to make
23895 this determination, SDCC uses a more simple minded analysis).
23898 The 'for' loop is of the form
23904 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23914 The <for body> does not contain
23915 \begin_inset Quotes eld
23919 \begin_inset Quotes erd
23923 \begin_inset Quotes erd
23929 All goto's are contained within the loop.
23932 No function calls within the loop.
23935 The loop control variable <sym> is not assigned any value within the loop
23938 The loop control variable does NOT participate in any arithmetic operation
23942 There are NO switch statements in the loop.
23945 Algebraic Simplifications
23948 SDCC does numerous algebraic simplifications, the following is a small sub-set
23949 of these optimizations.
23954 i = j + 0;\SpecialChar ~
23958 /* changed to: */\SpecialChar ~
23964 i /= 2;\SpecialChar ~
23971 /* changed to: */\SpecialChar ~
23977 i = j - j;\SpecialChar ~
23981 /* changed to: */\SpecialChar ~
23987 i = j / 1;\SpecialChar ~
23991 /* changed to: */\SpecialChar ~
23998 Note the subexpressions
23999 \begin_inset LatexCommand \index{Subexpression}
24003 given above are generally introduced by macro expansions or as a result
24004 of copy/constant propagation.
24007 'switch' Statements
24008 \begin_inset LatexCommand \label{sub:'switch'-Statements}
24013 \begin_inset LatexCommand \index{switch statement}
24020 SDCC can optimize switch statements to jump tables
24021 \begin_inset LatexCommand \index{jump tables}
24026 It makes the decision based on an estimate of the generated code size.
24027 SDCC is quite liberal in the requirements for jump table generation:
24030 The labels need not be in order, and the starting number need not be one
24031 or zero, the case labels are in numerical sequence or not too many case
24032 labels are missing.
24038 switch(i) {\SpecialChar ~
24069 case 4: ...\SpecialChar ~
24101 case 5: ...\SpecialChar ~
24133 case 3: ...\SpecialChar ~
24164 case 6: ...\SpecialChar ~
24196 case 7: ...\SpecialChar ~
24228 case 8: ...\SpecialChar ~
24260 case 9: ...\SpecialChar ~
24292 case 10: ...\SpecialChar ~
24323 case 11: ...\SpecialChar ~
24390 Both the above switch statements will be implemented using a jump-table.
24391 The example to the right side is slightly more efficient as the check for
24392 the lower boundary of the jump-table is not needed.
24396 The number of case labels is not larger than supported by the target architectur
24400 If the case labels are not in numerical sequence ('gaps' between cases)
24401 SDCC checks whether a jump table with additionally inserted dummy cases
24402 is still attractive.
24406 If the starting number is not zero and a check for the lower boundary of
24407 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24412 Switch statements which have large gaps in the numeric sequence or those
24413 that have too many case labels can be split into more than one switch statement
24414 for efficient code generation, e.g.:
24494 If the above switch statement is broken down into two switch statements
24584 then both the switch statements will be implemented using jump-tables whereas
24585 the unmodified switch statement will not be.
24588 There might be reasons which SDCC cannot know about to either favour or
24589 not favour jump tables.
24590 If the target system has to be as quick for the last switch case as for
24591 the first (pro jump table), or if the switch argument is known to be zero
24592 in the majority of the cases (contra jump table).
24595 The pragma nojtbound
24596 \begin_inset LatexCommand \index{\#pragma nojtbound}
24600 can be used to turn off checking the
24613 It has no effect if a default label is supplied.
24614 Use of this pragma is dangerous: if the switch
24615 \begin_inset LatexCommand \index{switch statement}
24619 argument is not matched by a case statement the processor will happily
24623 Bit-shifting Operations
24624 \begin_inset LatexCommand \index{Bit shifting}
24631 Bit shifting is one of the most frequently used operation in embedded programmin
24633 SDCC tries to implement bit-shift operations in the most efficient way
24649 generates the following code:
24666 In general SDCC will never setup a loop if the shift count is known.
24709 \begin_inset LatexCommand \index{Bit rotation}
24716 A special case of the bit-shift operation is bit rotation
24717 \begin_inset LatexCommand \index{rotating bits}
24721 , SDCC recognizes the following expression to be a left bit-rotation:
24731 char i;\SpecialChar ~
24742 /* unsigned is needed for rotation */
24747 i = ((i << 1) | (i >> 7));
24756 will generate the following code:
24775 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24776 ns of this case will also be recognized as bit-rotation, i.e.:
24781 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24784 Nibble and Byte Swapping
24787 Other special cases of the bit-shift operations are nibble or byte swapping
24788 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24792 , SDCC recognizes the following expressions:
24815 i = ((i << 4) | (i >> 4));
24821 j = ((j << 8) | (j >> 8));
24824 and generates a swap instruction for the nibble swapping
24825 \begin_inset LatexCommand \index{Nibble swapping}
24829 or move instructions for the byte swapping
24830 \begin_inset LatexCommand \index{Byte swapping}
24836 \begin_inset Quotes sld
24840 \begin_inset Quotes srd
24843 example can be used to convert from little to big-endian or vice versa.
24844 If you want to change the endianness of a
24848 integer you have to cast to
24855 Note that SDCC stores numbers in little-endian
24861 Usually 8-bit processors don't care much about endianness.
24862 This is not the case for the standard 8051 which only has an instruction
24868 \begin_inset LatexCommand \index{DPTR}
24876 so little-endian is the more efficient byte order.
24880 \begin_inset LatexCommand \index{little-endian}
24885 \begin_inset LatexCommand \index{Endianness}
24890 lowest order first).
24894 \begin_inset LatexCommand \index{Highest Order Bit}
24899 \begin_inset LatexCommand \index{Any Order Bit}
24906 It is frequently required to obtain the highest order bit of an integral
24907 type (long, int, short or char types).
24908 Also obtaining any other order bit is not uncommon.
24909 SDCC recognizes the following expressions to yield the highest order bit
24910 and generates optimized code for it, e.g.:
24923 unsigned char hob1, aob1;
24927 bit hob2, hob3, aob2, aob3;
24936 hob1 = (gint >> 15) & 1;
24940 hob2 = (gint >> 15) & 1;
24944 hob3 = gint & 0x8000;
24948 aob1 = (gint >> 9) & 1;
24952 aob2 = (gint >> 8) & 1;
24956 aob3 = gint & 0x0800;
24966 will generate the following code:
24999 000A E5*01\SpecialChar ~
25026 000C 23\SpecialChar ~
25057 000D 54 01\SpecialChar ~
25084 000F F5*02\SpecialChar ~
25139 0011 E5*01\SpecialChar ~
25166 0013 33\SpecialChar ~
25196 0014 92*00\SpecialChar ~
25251 0016 E5*01\SpecialChar ~
25278 0018 33\SpecialChar ~
25308 0019 92*01\SpecialChar ~
25363 001B E5*01\SpecialChar ~
25390 001D 03\SpecialChar ~
25421 001E 54 01\SpecialChar ~
25448 0020 F5*03\SpecialChar ~
25503 0022 E5*01\SpecialChar ~
25530 0024 13\SpecialChar ~
25560 0025 92*02\SpecialChar ~
25615 0027 E5*01\SpecialChar ~
25642 0029 A2 E3\SpecialChar ~
25669 002B 92*03\SpecialChar ~
25697 Other variations of these cases however will
25702 They are standard C expressions, so I heartily recommend these be the only
25703 way to get the highest order bit, (it is portable).
25704 Of course it will be recognized even if it is embedded in other expressions,
25710 xyz = gint + ((gint >> 15) & 1);
25713 will still be recognized.
25717 \begin_inset LatexCommand \index{Higher Order Byte}
25721 / Higher Order Word
25722 \begin_inset LatexCommand \index{Higher Order Word}
25729 It is also frequently required to obtain a higher order byte or word of
25730 a larger integral type (long, int or short types).
25731 SDCC recognizes the following expressions to yield the higher order byte
25732 or word and generates optimized code for it, e.g.:
25739 unsigned long int glong;
25747 unsigned char hob1, hob2;
25751 unsigned int how1, how2;
25760 hob1 = (gint >> 8) & 0xFF;
25764 hob2 = glong >> 24;
25768 how1 = (glong >> 16) & 0xFFFF;
25782 will generate the following code:
25815 0037 85*01*06\SpecialChar ~
25837 _foo_hob1_1_1,(_gint + 1)
25867 003A 85*05*07\SpecialChar ~
25889 _foo_hob2_1_1,(_glong + 3)
25919 003D 85*04*08\SpecialChar ~
25941 _foo_how1_1_1,(_glong + 2)
25943 0040 85*05*09\SpecialChar ~
25965 (_foo_how1_1_1 + 1),(_glong + 3)
25967 0043 85*03*0A\SpecialChar ~
25989 _foo_how2_1_1,(_glong + 1)
25991 0046 85*04*0B\SpecialChar ~
26013 (_foo_how2_1_1 + 1),(_glong + 2)
26016 Again, variations of these cases may
26021 They are standard C expressions, so I heartily recommend these be the only
26022 way to get the higher order byte/word, (it is portable).
26023 Of course it will be recognized even if it is embedded in other expressions,
26029 xyz = gint + ((gint >> 8) & 0xFF);
26032 will still be recognized.
26036 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
26041 \begin_inset LatexCommand \index{Peephole optimizer}
26048 The compiler uses a rule based, pattern matching and re-writing mechanism
26049 for peep-hole optimization.
26054 a peep-hole optimizer by Christopher W.
26055 Fraser (cwfraser\SpecialChar ~
26058 A default set of rules are compiled into the compiler, additional rules
26059 may be added with the
26072 \begin_inset LatexCommand \index{-\/-peep-file}
26079 The rule language is best illustrated with examples.
26103 The above rule will change the following assembly
26104 \begin_inset LatexCommand \index{Assembler routines}
26126 Note: All occurrences of a
26130 (pattern variable) must denote the same string.
26131 With the above rule, the assembly sequence:
26141 will remain unmodified.
26145 Other special case optimizations may be added by the user (via
26161 some variants of the 8051 MCU
26162 \begin_inset LatexCommand \index{MCS51 variants}
26175 The following two rules will change all
26194 replace { lcall %1 } by { acall %1 }
26196 replace { ljmp %1 } by { ajmp %1 }
26201 inline-assembler code
26203 is also passed through the peep hole optimizer, thus the peephole optimizer
26204 can also be used as an assembly level macro expander.
26205 The rules themselves are MCU dependent whereas the rule language infra-structur
26206 e is MCU independent.
26207 Peephole optimization rules for other MCU can be easily programmed using
26212 The syntax for a rule is as follows:
26217 rule := replace [ restart ] '{' <assembly sequence> '
26255 <assembly sequence> '
26273 '}' [if <functionName> ] '
26278 <assembly sequence> := assembly instruction (each instruction including
26279 labels must be on a separate line).
26283 The optimizer will apply to the rules one by one from the top in the sequence
26284 of their appearance, it will terminate when all rules are exhausted.
26285 If the 'restart' option is specified, then the optimizer will start matching
26286 the rules again from the top, this option for a rule is expensive (performance)
26287 , it is intended to be used in situations where a transformation will trigger
26288 the same rule again.
26289 An example of this (not a good one, it has side effects) is the following
26312 Note that the replace pattern cannot be a blank, but can be a comment line.
26313 Without the 'restart' option only the innermost 'pop' 'push' pair would
26314 be eliminated, i.e.:
26344 the restart option the rule will be applied again to the resulting code
26345 and then all the pop-push pairs will be eliminated to yield:
26355 A conditional function can be attached to a rule.
26356 Attaching rules are somewhat more involved, let me illustrate this with
26383 The optimizer does a look-up of a function name table defined in function
26388 in the source file SDCCpeeph.c, with the name
26393 If it finds a corresponding entry the function is called.
26394 Note there can be no parameters specified for these functions, in this
26399 is crucial, since the function
26403 expects to find the label in that particular variable (the hash table containin
26404 g the variable bindings is passed as a parameter).
26405 If you want to code more such functions, take a close look at the function
26406 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26407 Currently implemented are
26409 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26410 24bitMode, portIsDS390, 24bitModeAndPortDS390
26419 I know this whole thing is a little kludgey, but maybe some day we will
26420 have some better means.
26421 If you are looking at this file, you will see the default rules that are
26422 compiled into the compiler, you can add your own rules in the default set
26423 there if you get tired of specifying the -
26437 \begin_inset LatexCommand \index{ANSI-compliance}
26442 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26449 Deviations from the compliance:
26452 functions are not reentrant
26453 \begin_inset LatexCommand \index{reentrant}
26457 unless explicitly declared as such or the
26470 \begin_inset LatexCommand \index{-\/-stack-auto}
26476 command line option is specified.
26479 structures and unions cannot be assigned values directly, cannot be passed
26480 as function parameters or assigned to each other and cannot be a return
26481 value from a function, e.g.:
26507 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26518 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26540 return rets;/* is invalid in SDCC although allowed in ANSI */
26546 initialization of structure arrays must be fully braced.
26552 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26554 struct s { char x } a[] = {{1}, {2}}; /* OK */
26559 \begin_inset LatexCommand \index{long long (not supported)}
26564 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26572 \begin_inset LatexCommand \index{double (not supported)}
26576 ' precision floating point
26577 \begin_inset LatexCommand \index{Floating point support}
26585 \begin_inset LatexCommand \index{K\&R style}
26589 function declarations are NOT allowed.
26595 foo(i,j) /* this old style of function declarations */
26597 int i,j; /* are valid in ANSI but not valid in SDCC */
26612 Most enhancements in C99 are not supported, f.e.:
26621 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
26628 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
26632 Certain words that are valid identifiers in the standard may be reserved
26633 words in SDCC unless the
26646 \begin_inset LatexCommand \index{-\/-std-c89}
26661 \begin_inset LatexCommand \index{-\/-std-c99}
26667 command line options are used.
26668 These may include (depending on the selected processor): 'at', 'banked',
26669 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26670 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26671 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26673 Compliant equivalents of these keywords are always available in a form
26674 that begin with two underscores
26675 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26680 '__data' instead of 'data'.
26683 Cyclomatic Complexity
26684 \begin_inset LatexCommand \index{Cyclomatic complexity}
26691 Cyclomatic complexity of a function is defined as the number of independent
26692 paths the program can take during execution of the function.
26693 This is an important number since it defines the number test cases you
26694 have to generate to validate the function.
26695 The accepted industry standard for complexity number is 10, if the cyclomatic
26696 complexity reported by SDCC exceeds 10 you should think about simplification
26697 of the function logic.
26698 Note that the complexity level is not related to the number of lines of
26699 code in a function.
26700 Large functions can have low complexity, and small functions can have large
26706 SDCC uses the following formula to compute the complexity:
26711 complexity = (number of edges in control flow graph) - (number of nodes
26712 in control flow graph) + 2;
26716 Having said that the industry standard is 10, you should be aware that in
26717 some cases it be may unavoidable to have a complexity level of less than
26719 For example if you have switch statement with more than 10 case labels,
26720 each case label adds one to the complexity level.
26721 The complexity level is by no means an absolute measure of the algorithmic
26722 complexity of the function, it does however provide a good starting point
26723 for which functions you might look at for further optimization.
26726 Retargetting for other Processors
26729 The issues for retargetting the compiler are far too numerous to be covered
26731 What follows is a brief description of each of the seven phases of the
26732 compiler and its MCU dependency.
26735 Parsing the source and building the annotated parse tree.
26736 This phase is largely MCU independent (except for the language extensions).
26737 Syntax & semantic checks are also done in this phase, along with some initial
26738 optimizations like back patching labels and the pattern matching optimizations
26739 like bit-rotation etc.
26742 The second phase involves generating an intermediate code which can be easy
26743 manipulated during the later phases.
26744 This phase is entirely MCU independent.
26745 The intermediate code generation assumes the target machine has unlimited
26746 number of registers, and designates them with the name iTemp.
26747 The compiler can be made to dump a human readable form of the code generated
26761 This phase does the bulk of the standard optimizations and is also MCU independe
26763 This phase can be broken down into several sub-phases:
26767 Break down intermediate code (iCode) into basic blocks.
26769 Do control flow & data flow analysis on the basic blocks.
26771 Do local common subexpression elimination, then global subexpression elimination
26773 Dead code elimination
26777 If loop optimizations caused any changes then do 'global subexpression eliminati
26778 on' and 'dead code elimination' again.
26781 This phase determines the live-ranges; by live range I mean those iTemp
26782 variables defined by the compiler that still survive after all the optimization
26784 Live range analysis
26785 \begin_inset LatexCommand \index{Live range analysis}
26789 is essential for register allocation, since these computation determines
26790 which of these iTemps will be assigned to registers, and for how long.
26793 Phase five is register allocation.
26794 There are two parts to this process.
26798 The first part I call 'register packing' (for lack of a better term).
26799 In this case several MCU specific expression folding is done to reduce
26804 The second part is more MCU independent and deals with allocating registers
26805 to the remaining live ranges.
26806 A lot of MCU specific code does creep into this phase because of the limited
26807 number of index registers available in the 8051.
26810 The Code generation phase is (unhappily), entirely MCU dependent and very
26811 little (if any at all) of this code can be reused for other MCU.
26812 However the scheme for allocating a homogenized assembler operand for each
26813 iCode operand may be reused.
26816 As mentioned in the optimization section the peep-hole optimizer is rule
26817 based system, which can reprogrammed for other MCUs.
26821 \begin_inset LatexCommand \index{Compiler internals}
26828 The anatomy of the compiler
26829 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26838 This is an excerpt from an article published in Circuit Cellar Magazine
26844 It's a little outdated (the compiler is much more efficient now and user/develo
26845 per friendly), but pretty well exposes the guts of it all.
26851 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26852 It is fairly easy to retarget for other 8-bit MCU.
26853 Here we take a look at some of the internals of the compiler.
26858 \begin_inset LatexCommand \index{Parsing}
26865 Parsing the input source file and creating an AST (Annotated Syntax Tree
26866 \begin_inset LatexCommand \index{Annotated syntax tree}
26871 This phase also involves propagating types (annotating each node of the
26872 parse tree with type information) and semantic analysis.
26873 There are some MCU specific parsing rules.
26874 For example the storage classes, the extended storage classes are MCU specific
26875 while there may be a xdata storage class for 8051 there is no such storage
26876 class for z80 or Atmel AVR.
26877 SDCC allows MCU specific storage class extensions, i.e.
26878 xdata will be treated as a storage class specifier when parsing 8051 C
26879 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
26884 \begin_inset LatexCommand \index{iCode}
26891 Intermediate code generation.
26892 In this phase the AST is broken down into three-operand form (iCode).
26893 These three operand forms are represented as doubly linked lists.
26894 ICode is the term given to the intermediate form generated by the compiler.
26895 ICode example section shows some examples of iCode generated for some simple
26896 C source functions.
26900 \begin_inset LatexCommand \index{Optimizations}
26907 Bulk of the target independent optimizations is performed in this phase.
26908 The optimizations include constant propagation, common sub-expression eliminati
26909 on, loop invariant code movement, strength reduction of loop induction variables
26910 and dead-code elimination.
26913 Live range analysis
26914 \begin_inset LatexCommand \index{Live range analysis}
26921 During intermediate code generation phase, the compiler assumes the target
26922 machine has infinite number of registers and generates a lot of temporary
26924 The live range computation determines the lifetime of each of these compiler-ge
26925 nerated temporaries.
26926 A picture speaks a thousand words.
26927 ICode example sections show the live range annotations for each of the
26929 It is important to note here, each iCode is assigned a number in the order
26930 of its execution in the function.
26931 The live ranges are computed in terms of these numbers.
26932 The from number is the number of the iCode which first defines the operand
26933 and the to number signifies the iCode which uses this operand last.
26936 Register Allocation
26937 \begin_inset LatexCommand \index{Register allocation}
26944 The register allocation determines the type and number of registers needed
26946 In most MCUs only a few registers can be used for indirect addressing.
26947 In case of 8051 for example the registers R0 & R1 can be used to indirectly
26948 address the internal ram and DPTR to indirectly address the external ram.
26949 The compiler will try to allocate the appropriate register to pointer variables
26951 ICode example section shows the operands annotated with the registers assigned
26953 The compiler will try to keep operands in registers as much as possible;
26954 there are several schemes the compiler uses to do achieve this.
26955 When the compiler runs out of registers the compiler will check to see
26956 if there are any live operands which is not used or defined in the current
26957 basic block being processed, if there are any found then it will push that
26958 operand and use the registers in this block, the operand will then be popped
26959 at the end of the basic block.
26963 There are other MCU specific considerations in this phase.
26964 Some MCUs have an accumulator; very short-lived operands could be assigned
26965 to the accumulator instead of a general-purpose register.
26971 Figure II gives a table of iCode operations supported by the compiler.
26972 The code generation involves translating these operations into corresponding
26973 assembly code for the processor.
26974 This sounds overly simple but that is the essence of code generation.
26975 Some of the iCode operations are generated on a MCU specific manner for
26976 example, the z80 port does not use registers to pass parameters so the
26977 SEND and RECV iCode operations will not be generated, and it also does
26978 not support JUMPTABLES.
26985 <Where is Figure II?>
26988 In the original article Figure II was announced to be downloadable on
26993 Unfortunately it never seemed to have shown up there, so: where is Figure
26998 \begin_inset LatexCommand \index{iCode}
27005 This section shows some details of iCode.
27006 The example C code does not do anything useful; it is used as an example
27007 to illustrate the intermediate code generated by the compiler.
27019 /* This function does nothing useful.
27026 for the purpose of explaining iCode */
27029 short function (data int *x)
27037 short i=10; \SpecialChar ~
27039 /* dead initialization eliminated */
27044 short sum=10; /* dead initialization eliminated */
27057 while (*x) *x++ = *p++;
27071 /* compiler detects i,j to be induction variables */
27075 for (i = 0, j = 10 ; i < 10 ; i++, j
27101 mul += i * 3; \SpecialChar ~
27103 /* this multiplication remains */
27109 gint += j * 3;\SpecialChar ~
27111 /* this multiplication changed to addition */
27125 In addition to the operands each iCode contains information about the filename
27126 and line it corresponds to in the source file.
27127 The first field in the listing should be interpreted as follows:
27132 Filename(linenumber: iCode Execution sequence number : ICode hash table
27133 key : loop depth of the iCode).
27138 Then follows the human readable form of the ICode operation.
27139 Each operand of this triplet form can be of three basic types a) compiler
27140 generated temporary b) user defined variable c) a constant value.
27141 Note that local variables and parameters are replaced by compiler generated
27144 \begin_inset LatexCommand \index{Live range analysis}
27148 are computed only for temporaries (i.e.
27149 live ranges are not computed for global variables).
27151 \begin_inset LatexCommand \index{Register allocation}
27155 are allocated for temporaries only.
27156 Operands are formatted in the following manner:
27161 Operand Name [lr live-from : live-to ] { type information } [ registers
27167 As mentioned earlier the live ranges are computed in terms of the execution
27168 sequence number of the iCodes, for example
27170 the iTemp0 is live from (i.e.
27171 first defined in iCode with execution sequence number 3, and is last used
27172 in the iCode with sequence number 5).
27173 For induction variables such as iTemp21 the live range computation extends
27174 the lifetime from the start to the end of the loop.
27176 The register allocator used the live range information to allocate registers,
27177 the same registers may be used for different temporaries if their live
27178 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27179 iTemp17 since their live ranges do not overlap.
27180 In addition the allocator also takes into consideration the type and usage
27181 of a temporary, for example itemp6 is a pointer to near space and is used
27182 as to fetch data from (i.e.
27183 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27184 Some short lived temporaries are allocated to special registers which have
27185 meaning to the code generator e.g.
27186 iTemp13 is allocated to a pseudo register CC which tells the back end that
27187 the temporary is used only for a conditional jump the code generation makes
27188 use of this information to optimize a compare and jump ICode.
27190 There are several loop optimizations
27191 \begin_inset LatexCommand \index{Loop optimization}
27195 performed by the compiler.
27196 It can detect induction variables iTemp21(i) and iTemp23(j).
27197 Also note the compiler does selective strength reduction
27198 \begin_inset LatexCommand \index{Strength reduction}
27203 the multiplication of an induction variable in line 18 (gint = j * 3) is
27204 changed to addition, a new temporary iTemp17 is allocated and assigned
27205 a initial value, a constant 3 is then added for each iteration of the loop.
27206 The compiler does not change the multiplication
27207 \begin_inset LatexCommand \index{Multiplication}
27211 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27213 Note the dead code elimination
27214 \begin_inset LatexCommand \index{Dead-code elimination}
27218 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27226 Sample.c (5:1:0:0) _entry($9) :
27231 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27236 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27241 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27246 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27252 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27257 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27263 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27268 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27274 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27280 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27286 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27292 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27293 * int}[r0] + 0x2 {short}
27298 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27303 Sample.c(11:17:21:0)_whilebreak_0($3) :
27308 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27313 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27318 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27323 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27328 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27333 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27338 Sample.c(15:24:26:1)_forcond_0($4) :
27343 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27349 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27354 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27355 + ITemp21 [lr21:38]{short}[r4]
27360 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27366 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27367 + iTemp15 [lr29:30]{short}[r1]
27372 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27378 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27384 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27390 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27396 Sample.c(19:38:47:1) goto _forcond_0($4)
27401 Sample.c(19:39:48:0)_forbreak_0($7) :
27406 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27407 + ITemp11 [lr19:40]{short}[r3]
27412 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27417 Sample.c(20:42:51:0)_return($8) :
27422 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27428 Finally the code generated for this function:
27469 ; ----------------------------------------------
27474 ; function function
27479 ; ----------------------------------------------
27489 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27501 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27513 ;_whilecontinue_0($1) :
27523 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27528 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27587 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27606 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27653 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27693 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27719 ; iTemp6 [lr5:16]{_near * int}[r0] =
27724 ; iTemp6 [lr5:16]{_near * int}[r0] +
27741 ; goto _whilecontinue_0($1)
27753 ; _whilebreak_0($3) :
27763 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27775 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27787 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27799 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27818 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27847 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27852 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27897 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
27902 ; iTemp21 [lr21:38]{short}[r4]
27928 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
27961 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
27966 ; iTemp15 [lr29:30]{short}[r1]
27985 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
28032 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
28079 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
28091 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
28105 cjne r5,#0xff,00104$
28117 ; goto _forcond_0($4)
28129 ; _forbreak_0($7) :
28139 ; ret iTemp24 [lr40:41]{short}
28182 A few words about basic block successors, predecessors and dominators
28185 Successors are basic blocks
28186 \begin_inset LatexCommand \index{Basic blocks}
28190 that might execute after this basic block.
28192 Predecessors are basic blocks that might execute before reaching this basic
28195 Dominators are basic blocks that WILL execute before reaching this basic
28229 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28232 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28235 c) domVect of [BB4] = BB1 ...
28236 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28244 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28254 Thanks to all the other volunteer developers who have helped with coding,
28255 testing, web-page creation, distribution sets, etc.
28256 You know who you are :-)
28263 This document was initially written by Sandeep Dutta
28266 All product names mentioned herein may be trademarks
28267 \begin_inset LatexCommand \index{Trademarks}
28271 of their respective companies.
28278 To avoid confusion, the installation and building options for SDCC itself
28279 (chapter 2) are not part of the index.
28283 \begin_inset LatexCommand \printindex{}