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.
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 an
11263 RETI instruction to return from the interrupt.
11264 To write an interrupt handler for the non-maskable interrupt, which needs
11265 an RETN instruction instead, add the
11274 void nmi_isr (void) critical interrupt
11288 Enabling and Disabling Interrupts
11291 Critical Functions and Critical Statements
11294 A special keyword may be associated with a block or a function declaring
11300 SDCC will generate code to disable all interrupts
11301 \begin_inset LatexCommand \index{interrupt}
11305 upon entry to a critical function and restore the interrupt enable to the
11306 previous state before returning.
11307 Nesting critical functions will need one additional byte on the stack
11308 \begin_inset LatexCommand \index{stack}
11317 int foo () critical
11318 \begin_inset LatexCommand \index{critical}
11323 \begin_inset LatexCommand \index{\_\_critical}
11348 The critical attribute maybe used with other attributes like
11358 may also be used to disable interrupts more locally:
11366 More than one statement could have been included in the block.
11369 Enabling and Disabling Interrupts directly
11373 \begin_inset LatexCommand \index{interrupt}
11377 can also be disabled and enabled directly (8051):
11382 EA = 0;\SpecialChar ~
11445 EA = 1;\SpecialChar ~
11512 On other architectures which have seperate opcodes for enabling and disabling
11513 interrupts you might want to make use of defines with inline assembly
11514 \begin_inset LatexCommand \index{Assembler routines}
11524 \begin_inset LatexCommand \index{\_asm}
11533 \begin_inset LatexCommand \index{\_endasm}
11542 #define SEI _asm\SpecialChar ~
11554 Note: it is sometimes sufficient to disable only a specific interrupt source
11556 a timer or serial interrupt by manipulating an
11559 \begin_inset LatexCommand \index{interrupt mask}
11569 Usually the time during which interrupts are disabled should be kept as
11571 This minimizes both
11576 \begin_inset LatexCommand \index{interrupt latency}
11580 (the time between the occurrence of the interrupt and the execution of
11581 the first code in the interrupt routine) and
11586 \begin_inset LatexCommand \index{interrupt jitter}
11590 (the difference between the shortest and the longest interrupt latency).
11591 These really are something different, f.e.
11592 a serial interrupt has to be served before its buffer overruns so it cares
11593 for the maximum interrupt latency, whereas it does not care about jitter.
11594 On a loudspeaker driven via a digital to analog converter which is fed
11595 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11596 a much smaller jitter will be very audible.
11599 You can reenable interrupts within an interrupt routine and on some architecture
11600 s you can make use of two (or more) levels of
11602 interrupt priorities
11605 \begin_inset LatexCommand \index{interrupt priority}
11610 On some architectures which don't support interrupt priorities these can
11611 be implemented by manipulating the interrupt mask and reenabling interrupts
11612 within the interrupt routine.
11613 Check there is sufficient space on the stack
11614 \begin_inset LatexCommand \index{stack}
11618 and don't add complexity unless you have to.
11623 \begin_inset LatexCommand \index{semaphore}
11627 locking (mcs51/ds390)
11630 Some architectures (mcs51/ds390) have an atomic
11631 \begin_inset LatexCommand \index{atomic}
11644 These type of instructions are typically used in preemptive multitasking
11645 systems, where a routine f.e.
11646 claims the use of a data structure ('acquires a lock
11647 \begin_inset LatexCommand \index{lock}
11651 on it'), makes some modifications and then releases the lock when the data
11652 structure is consistent again.
11653 The instruction may also be used if interrupt and non-interrupt code have
11654 to compete for a resource.
11655 With the atomic bit test and clear instruction interrupts
11656 \begin_inset LatexCommand \index{interrupt}
11660 don't have to be disabled for the locking operation.
11664 SDCC generates this instruction if the source follows this pattern:
11670 \begin_inset LatexCommand \index{volatile}
11674 bit resource_is_free;
11678 if (resource_is_free)
11688 resource_is_free=0;
11701 resource_is_free=1;
11708 Note, mcs51 and ds390 support only an atomic
11709 \begin_inset LatexCommand \index{atomic}
11717 instruction (as opposed to atomic bit test and
11722 Functions using private register banks
11723 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11730 Some architectures have support for quickly changing register sets.
11731 SDCC supports this feature with the
11734 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11739 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11745 attribute (which tells the compiler to use a register bank
11746 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11750 other than the default bank zero).
11751 It should only be applied to
11754 \begin_inset LatexCommand \index{interrupt}
11760 functions (see footnote below).
11761 This will in most circumstances make the generated ISR code more efficient
11762 since it will not have to save registers on the stack.
11769 attribute will have no effect on the generated code for a
11773 function (but may occasionally be useful anyway
11779 possible exception: if a function is called ONLY from 'interrupt' functions
11780 using a particular bank, it can be declared with the same 'using' attribute
11781 as the calling 'interrupt' functions.
11782 For instance, if you have several ISRs using bank one, and all of them
11783 call memcpy(), it might make sense to create a specialized version of memcpy()
11784 'using 1', since this would prevent the ISR from having to save bank zero
11785 to the stack on entry and switch to bank zero before calling the function
11792 (pending: I don't think this has been done yet)
11799 function using a non-zero bank will assume that it can trash that register
11800 bank, and will not save it.
11801 Since high-priority interrupts
11802 \begin_inset LatexCommand \index{interrupts}
11807 \begin_inset LatexCommand \index{interrupt priority}
11811 can interrupt low-priority ones on the 8051 and friends, this means that
11812 if a high-priority ISR
11816 a particular bank occurs while processing a low-priority ISR
11820 the same bank, terrible and bad things can happen.
11821 To prevent this, no single register bank should be
11825 by both a high priority and a low priority ISR.
11826 This is probably most easily done by having all high priority ISRs use
11827 one bank and all low priority ISRs use another.
11828 If you have an ISR which can change priority at runtime, you're on your
11829 own: I suggest using the default bank zero and taking the small performance
11833 It is most efficient if your ISR calls no other functions.
11834 If your ISR must call other functions, it is most efficient if those functions
11835 use the same bank as the ISR (see note 1 below); the next best is if the
11836 called functions use bank zero.
11837 It is very inefficient to call a function using a different, non-zero bank
11843 \begin_inset LatexCommand \label{sub:Startup-Code}
11848 \begin_inset LatexCommand \index{Startup code}
11855 MCS51/DS390 Startup Code
11858 The compiler inserts a call to the C routine
11860 _sdcc_external_startup()
11861 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
11870 at the start of the CODE area.
11871 This routine is in the runtime library
11872 \begin_inset LatexCommand \index{Runtime library}
11877 By default this routine returns 0, if this routine returns a non-zero value,
11878 the static & global variable initialization will be skipped and the function
11879 main will be invoked.
11880 Otherwise static & global variables will be initialized before the function
11884 _sdcc_external_startup()
11886 routine to your program to override the default if you need to setup hardware
11887 or perform some other critical operation prior to static & global variable
11889 \begin_inset LatexCommand \index{Variable initialization}
11894 On some mcs51 variants xdata
11895 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
11899 memory has to be explicitly enabled before it can be accessed or if the
11900 watchdog needs to be disabled, this is the place to do it.
11901 The startup code clears all internal data memory, 256 bytes by default,
11902 but from 0 to n-1 if
11915 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
11922 (recommended for Chipcon CC1010).
11925 See also the compiler option
11944 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
11949 \begin_inset LatexCommand \ref{sub:MCS51-variants}
11954 about MCS51-variants.
11960 The HC08 startup code follows the same scheme as the MCS51 startup code.
11966 On the Z80 the startup code is inserted by linking with crt0.o which is generated
11967 from sdcc/device/lib/z80/crt0.s.
11968 If you need a different startup code you can use the compiler option
11989 \begin_inset LatexCommand \index{-\/-no-std-crt0}
11993 and provide your own crt0.o.
11997 Inline Assembler Code
11998 \begin_inset LatexCommand \index{Assembler routines}
12005 A Step by Step Introduction
12006 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12013 Starting from a small snippet of c-code this example shows for the MCS51
12014 how to use inline assembly, access variables, a function parameter and
12015 an array in xdata memory.
12016 The example uses an MCS51 here but is easily adapted for other architectures.
12017 This is a buffer routine which should be optimized:
12024 \begin_inset LatexCommand \index{far (storage class)}
12029 \begin_inset LatexCommand \index{\_\_far (storage class)}
12034 \begin_inset LatexCommand \index{at}
12039 \begin_inset LatexCommand \index{\_\_at}
12044 \begin_inset LatexCommand \index{Aligned array}
12050 unsigned char head,tail;
12054 void to_buffer( unsigned char c )
12062 if( head != tail-1 )
12072 buf[ head++ ] = c;\SpecialChar ~
12076 /* access to a 256 byte aligned array */
12081 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12082 then a corresponding buffer.asm file is generated.
12083 We define a new function
12087 in file buffer.c in which we cut and paste the generated code, removing
12088 unwanted comments and some ':'.
12090 \begin_inset Quotes sld
12094 \begin_inset Quotes srd
12098 \begin_inset Quotes sld
12102 \begin_inset Quotes srd
12105 to the beginning and the end of the function body:
12111 /* With a cut and paste from the .asm file, we have something to start with.
12116 The function is not yet OK! (registers aren't saved) */
12118 void to_buffer_asm( unsigned char c )
12127 \begin_inset LatexCommand \index{\_asm}
12132 \begin_inset LatexCommand \index{\_\_asm}
12146 ;buffer.c if( head != tail-1 )
12194 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12195 \begin_inset LatexCommand \index{Aligned array}
12260 \begin_inset LatexCommand \index{\_endasm}
12265 \begin_inset LatexCommand \index{\_\_endasm}
12274 The new file buffer.c should compile with only one warning about the unreferenced
12275 function argument 'c'.
12276 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12277 (1) and finally have:
12283 unsigned char far at 0x7f00 buf[0x100];
12285 unsigned char head,tail;
12287 #define USE_ASSEMBLY (1)
12295 void to_buffer( unsigned char c )
12303 if( head != tail-1 )
12323 void to_buffer( unsigned char c )
12331 c; // to avoid warning: unreferenced function argument
12338 \begin_inset LatexCommand \index{\_asm}
12343 \begin_inset LatexCommand \index{\_\_asm}
12357 ; save used registers here.
12368 ; If we were still using r2,r3 we would have to push them here.
12371 ; if( head != tail-1 )
12414 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12438 ; buf[ head++ ] = c;
12449 a,dpl \SpecialChar ~
12456 ; dpl holds lower byte of function argument
12467 dpl,_head \SpecialChar ~
12470 ; buf is 0x100 byte aligned so head can be used directly
12512 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12524 ; restore used registers here
12531 \begin_inset LatexCommand \index{\_endasm}
12536 \begin_inset LatexCommand \index{\_\_endasm}
12547 The inline assembler code can contain any valid code understood by the assembler
12548 , this includes any assembler directives and comment lines
12554 The assembler does not like some characters like ':' or ''' in comments.
12555 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12556 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12561 \begin_inset LatexCommand \index{Assembler documentation}
12569 The compiler does not do any validation of the code within the
12572 \begin_inset LatexCommand \index{\_asm}
12577 \begin_inset LatexCommand \index{\_\_asm}
12585 \begin_inset LatexCommand \index{\_endasm}
12590 \begin_inset LatexCommand \index{\_\_endasm}
12599 Specifically it will not know which registers are used and thus register
12601 \begin_inset LatexCommand \index{push/pop}
12605 has to be done manually.
12609 It is recommended that each assembly instruction (including labels) be placed
12610 in a separate line (as the example shows).
12624 \begin_inset LatexCommand \index{-\/-peep-asm}
12630 command line option is used, the inline assembler code will be passed through
12631 the peephole optimizer
12632 \begin_inset LatexCommand \index{Peephole optimizer}
12637 There are only a few (if any) cases where this option makes sense, it might
12638 cause some unexpected changes in the inline assembler code.
12639 Please go through the peephole optimizer rules defined in file
12643 before using this option.
12647 \begin_inset LatexCommand \label{sub:Naked-Functions}
12652 \begin_inset LatexCommand \index{Naked functions}
12659 A special keyword may be associated with a function declaring it as
12662 \begin_inset LatexCommand \index{\_naked}
12667 \begin_inset LatexCommand \index{\_\_naked}
12678 function modifier attribute prevents the compiler from generating prologue
12679 \begin_inset LatexCommand \index{function prologue}
12684 \begin_inset LatexCommand \index{function epilogue}
12688 code for that function.
12689 This means that the user is entirely responsible for such things as saving
12690 any registers that may need to be preserved, selecting the proper register
12691 bank, generating the
12695 instruction at the end, etc.
12696 Practically, this means that the contents of the function must be written
12697 in inline assembler.
12698 This is particularly useful for interrupt functions, which can have a large
12699 (and often unnecessary) prologue/epilogue.
12700 For example, compare the code generated by these two functions:
12706 \begin_inset LatexCommand \index{volatile}
12710 data unsigned char counter;
12714 void simpleInterrupt(void) interrupt
12715 \begin_inset LatexCommand \index{interrupt}
12720 \begin_inset LatexCommand \index{\_\_interrupt}
12738 void nakedInterrupt(void) interrupt 2 _naked
12747 \begin_inset LatexCommand \index{\_asm}
12752 \begin_inset LatexCommand \index{\_\_asm}
12769 _counter ; does not change flags, no need to save psw
12781 ; MUST explicitly include ret or reti in _naked function.
12788 \begin_inset LatexCommand \index{\_endasm}
12793 \begin_inset LatexCommand \index{\_\_endasm}
12802 For an 8051 target, the generated simpleInterrupt looks like:
12943 whereas nakedInterrupt looks like:
12958 _counter ; does not change flags, no need to save psw
12976 ; MUST explicitly include ret or reti in _naked function
12979 The related directive #pragma exclude
12980 \begin_inset LatexCommand \index{\#pragma exclude}
12984 allows a more fine grained control over pushing & popping
12985 \begin_inset LatexCommand \index{push/pop}
12992 While there is nothing preventing you from writing C code inside a
12996 function, there are many ways to shoot yourself in the foot doing this,
12997 and it is recommended that you stick to inline assembler.
13000 Use of Labels within Inline Assembler
13003 SDCC allows the use of in-line assembler with a few restrictions regarding
13005 In older versions of the compiler all labels defined within inline assembler
13014 where nnnn is a number less than 100 (which implies a limit of utmost 100
13015 inline assembler labels
13029 \begin_inset LatexCommand \index{\_asm}
13034 \begin_inset LatexCommand \index{\_\_asm}
13064 \begin_inset LatexCommand \index{\_endasm}
13069 \begin_inset LatexCommand \index{\_\_endasm}
13076 Inline assembler code cannot reference any C-Labels, however it can reference
13078 \begin_inset LatexCommand \index{Labels}
13082 defined by the inline assembler, e.g.:
13107 ; some assembler code
13127 /* some more c code */
13129 clabel:\SpecialChar ~
13131 /* inline assembler cannot reference this label */
13143 $0003: ;label (can be referenced by inline assembler only)
13150 \begin_inset LatexCommand \index{\_endasm}
13155 \begin_inset LatexCommand \index{\_\_endasm}
13165 /* some more c code */
13170 In other words inline assembly code can access labels defined in inline
13171 assembly within the scope of the function.
13172 The same goes the other way, i.e.
13173 labels defines in inline assembly can not be accessed by C statements.
13176 Interfacing with Assembler Code
13177 \begin_inset LatexCommand \index{Assembler routines}
13184 Global Registers used for Parameter Passing
13185 \begin_inset LatexCommand \index{Parameter passing}
13192 The compiler always uses the global registers
13195 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13200 \begin_inset LatexCommand \index{DPTR}
13205 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13214 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13220 to pass the first parameter to a routine.
13221 The second parameter onwards is either allocated on the stack (for reentrant
13232 -stack-auto is used) or in data / xdata memory (depending on the memory
13237 Assembler Routine (non-reentrant)
13240 In the following example
13241 \begin_inset LatexCommand \index{reentrant}
13246 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13250 the function c_func calls an assembler routine asm_func, which takes two
13252 \begin_inset LatexCommand \index{function parameter}
13261 extern int asm_func(unsigned char, unsigned char);
13265 int c_func (unsigned char i, unsigned char j)
13273 return asm_func(i,j);
13287 return c_func(10,9);
13292 The corresponding assembler function is:
13297 .globl _asm_func_PARM_2
13398 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13415 Note here that the return values
13416 \begin_inset LatexCommand \index{return value}
13420 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13422 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13423 b' & 'acc' for four byte values.
13426 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13427 the parameter number starting from 1, and counting from the left.
13428 The first parameter is passed in
13429 \begin_inset Quotes eld
13433 \begin_inset Quotes erd
13436 for a one byte parameter,
13437 \begin_inset Quotes eld
13441 \begin_inset Quotes erd
13445 \begin_inset Quotes eld
13449 \begin_inset Quotes erd
13452 for three bytes and
13453 \begin_inset Quotes eld
13457 \begin_inset Quotes erd
13460 for a four bytes parameter.
13461 The variable name for the second parameter will be _<function_name>_PARM_2.
13465 Assemble the assembler routine with the following command:
13472 asx8051 -losg asmfunc.asm
13479 Then compile and link the assembler routine to the C source file with the
13487 sdcc cfunc.c asmfunc.rel
13490 Assembler Routine (reentrant)
13494 \begin_inset LatexCommand \index{reentrant}
13499 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13503 the second parameter
13504 \begin_inset LatexCommand \index{function parameter}
13508 onwards will be passed on the stack, the parameters are pushed from right
13510 after the call the leftmost parameter will be on the top of the stack.
13511 Here is an example:
13516 extern int asm_func(unsigned char, unsigned char);
13520 int c_func (unsigned char i, unsigned char j) reentrant
13528 return asm_func(i,j);
13542 return c_func(10,9);
13547 The corresponding assembler routine is:
13647 The compiling and linking procedure remains the same, however note the extra
13648 entry & exit linkage required for the assembler code, _bp is the stack
13649 frame pointer and is used to compute the offset into the stack for parameters
13650 and local variables.
13654 \begin_inset LatexCommand \index{int (16 bit)}
13659 \begin_inset LatexCommand \index{long (32 bit)}
13666 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13667 multiplication and modulus operations are implemented by support routines.
13668 These support routines are all developed in ANSI-C to facilitate porting
13669 to other MCUs, although some model specific assembler optimizations are
13671 The following files contain the described routines, all of them can be
13672 found in <installdir>/share/sdcc/lib.
13678 \begin_inset Tabular
13679 <lyxtabular version="3" rows="11" columns="2">
13681 <column alignment="center" valignment="top" leftline="true" width="0">
13682 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13683 <row topline="true" bottomline="true">
13684 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13694 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13705 <row topline="true">
13706 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13719 16 bit multiplication
13723 <row topline="true">
13724 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13732 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13737 signed 16 bit division (calls _divuint)
13741 <row topline="true">
13742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13750 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13755 unsigned 16 bit division
13759 <row topline="true">
13760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13768 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13773 signed 16 bit modulus (calls _moduint)
13777 <row topline="true">
13778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13791 unsigned 16 bit modulus
13795 <row topline="true">
13796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13809 32 bit multiplication
13813 <row topline="true">
13814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13827 signed 32 division (calls _divulong)
13831 <row topline="true">
13832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13840 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13845 unsigned 32 division
13849 <row topline="true">
13850 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13858 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13863 signed 32 bit modulus (calls _modulong)
13867 <row topline="true" bottomline="true">
13868 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13876 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13881 unsigned 32 bit modulus
13894 Since they are compiled as
13899 \begin_inset LatexCommand \index{reentrant}
13904 \begin_inset LatexCommand \index{interrupt}
13908 service routines should not do any of the above operations.
13909 If this is unavoidable then the above routines will need to be compiled
13923 \begin_inset LatexCommand \index{-\/-stack-auto}
13929 option, after which the source program will have to be compiled with
13942 \begin_inset LatexCommand \index{-\/-int-long-reent}
13949 Notice that you don't have to call these routines directly.
13950 The compiler will use them automatically every time an integer operation
13954 Floating Point Support
13955 \begin_inset LatexCommand \index{Floating point support}
13962 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
13963 floating point support routines are derived from gcc's floatlib.c and consist
13964 of the following routines:
13972 \begin_inset Tabular
13973 <lyxtabular version="3" rows="17" columns="2">
13975 <column alignment="center" valignment="top" leftline="true" width="0">
13976 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13977 <row topline="true" bottomline="true">
13978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13995 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14004 <row topline="true">
14005 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14022 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14036 add floating point numbers
14040 <row topline="true">
14041 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14058 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14072 subtract floating point numbers
14076 <row topline="true">
14077 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14094 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14108 divide floating point numbers
14112 <row topline="true">
14113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14130 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14144 multiply floating point numbers
14148 <row topline="true">
14149 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14166 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14180 convert floating point to unsigned char
14184 <row topline="true">
14185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14216 convert floating point to signed char
14220 <row topline="true">
14221 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14238 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14252 convert floating point to unsigned int
14256 <row topline="true">
14257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14274 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14288 convert floating point to signed int
14292 <row topline="true">
14293 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14333 convert floating point to unsigned long
14337 <row topline="true">
14338 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14355 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14369 convert floating point to signed long
14373 <row topline="true">
14374 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14391 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14405 convert unsigned char to floating point
14409 <row topline="true">
14410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14427 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14441 convert char to floating point number
14445 <row topline="true">
14446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14463 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14477 convert unsigned int to floating point
14481 <row topline="true">
14482 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14513 convert int to floating point numbers
14517 <row topline="true">
14518 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14535 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14549 convert unsigned long to floating point number
14553 <row topline="true" bottomline="true">
14554 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14571 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14585 convert long to floating point number
14598 These support routines are developed in ANSI-C so there is room for space
14599 and speed improvement
14605 The floating point routines for the mcs51 are implemented in assembler
14609 Note if all these routines are used simultaneously the data space might
14611 For serious floating point usage the large model might be needed.
14612 Also notice that you don't have to call this routines directly.
14613 The compiler will use them automatically every time a floating point operation
14618 \begin_inset LatexCommand \index{Libraries}
14627 <pending: this is messy and incomplete - a little more information is in
14628 sdcc/doc/libdoc.txt
14633 Compiler support routines (_gptrget, _mulint etc.)
14636 Stdclib functions (puts, printf, strcat etc.)
14637 \layout Subsubsection
14643 \begin_inset LatexCommand \index{<stdio.h>}
14647 As usual on embedded systems you have to provide your own
14650 \begin_inset LatexCommand \index{getchar()}
14659 \begin_inset LatexCommand \index{putchar()}
14666 SDCC does not know whether the system connects to a serial line with or
14667 without handshake, LCD, keyboard or other device.
14668 You'll find examples for serial routines f.e.
14669 in sdcc/device/lib.
14675 \begin_inset LatexCommand \index{printf()}
14685 does not support float (except on ds390).
14686 To enable this recompile it with the option
14699 \begin_inset LatexCommand \index{USE\_FLOATS}
14705 on the command line.
14719 \begin_inset LatexCommand \index{-\/-model-large}
14725 for the mcs51 port, since this uses a lot of memory.
14728 If you're short on memory you might want to use
14731 \begin_inset LatexCommand \index{printf\_small()}
14746 For the mcs51 there additionally are assembly versions
14749 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14758 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14767 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14773 which should fit the requirements of many embedded systems (printf_fast()
14774 can be customized by unsetting #defines to
14778 support long variables and field widths).
14781 Math functions (sin, pow, sqrt etc.)
14788 \begin_inset LatexCommand \index{Libraries}
14792 included in SDCC should have a license at least as liberal as the GNU Lesser
14793 General Public License
14794 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14805 license statements for the libraries are missing.
14806 sdcc/device/lib/ser_ir.c
14810 come with a GPL (as opposed to LGPL) License - this will not be liberal
14811 enough for many embedded programmers.
14814 If you have ported some library or want to share experience about some code
14816 falls into any of these categories Busses (I
14817 \begin_inset Formula $^{\textrm{2}}$
14820 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14821 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14822 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14823 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14828 would certainly like to hear about it.
14829 Programmers coding for embedded systems are not especially famous for being
14830 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14831 e these references are very valuable.
14832 Let's help to create a climate where information is shared.
14838 MCS51 Memory Models
14839 \begin_inset LatexCommand \index{Memory model}
14844 \begin_inset LatexCommand \index{MCS51 memory model}
14849 \layout Subsubsection
14851 Small, Medium and Large
14854 SDCC allows three memory models for MCS51 code,
14863 Modules compiled with different memory models should
14867 be combined together or the results would be unpredictable.
14868 The library routines supplied with the compiler are compiled as small,
14870 The compiled library modules are contained in separate directories as small,
14871 medium and large so that you can link to the appropriate set.
14874 When the medium or large model is used all variables declared without a
14875 storage class will be allocated into the external ram, this includes all
14876 parameters and local variables (for non-reentrant
14877 \begin_inset LatexCommand \index{reentrant}
14882 When the small model is used variables without storage class are allocated
14883 in the internal ram.
14886 Judicious usage of the processor specific storage classes
14887 \begin_inset LatexCommand \index{Storage class}
14891 and the 'reentrant' function type will yield much more efficient code,
14892 than using the large model.
14893 Several optimizations are disabled when the program is compiled using the
14894 large model, it is therefore recommended that the small model be used unless
14895 absolutely required.
14896 \layout Subsubsection
14899 \begin_inset LatexCommand \label{sub:External-Stack}
14904 \begin_inset LatexCommand \index{stack}
14909 \begin_inset LatexCommand \index{External stack (mcs51)}
14916 The external stack (-
14927 \begin_inset LatexCommand \index{-\/-xstack}
14931 ) is located in pdata
14932 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
14936 memory (usually at the start of the external ram segment) and uses all
14937 unused space in pdata (max.
14949 -xstack option is used to compile the program, the parameters and local
14951 \begin_inset LatexCommand \index{local variables}
14955 of all reentrant functions are allocated in this area.
14956 This option is provided for programs with large stack space requirements.
14957 When used with the -
14968 \begin_inset LatexCommand \index{-\/-stack-auto}
14972 option, all parameters and local variables are allocated on the external
14973 stack (note: support libraries will need to be recompiled with the same
14975 There is a predefined target in the library makefile).
14978 The compiler outputs the higher order address byte of the external ram segment
14980 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
14985 \begin_inset LatexCommand \ref{sub:MCS51-variants}
14989 ), therefore when using the External Stack option, this port
14993 be used by the application program.
14997 \begin_inset LatexCommand \index{Memory model}
15002 \begin_inset LatexCommand \index{DS390 memory model}
15009 The only model supported is Flat 24
15010 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15015 This generates code for the 24 bit contiguous addressing mode of the Dallas
15017 In this mode, up to four meg of external RAM or code space can be directly
15019 See the data sheets at www.dalsemi.com for further information on this part.
15023 Note that the compiler does not generate any code to place the processor
15024 into 24 bitmode (although
15028 in the ds390 libraries will do that for you).
15034 \begin_inset LatexCommand \index{Tinibios (DS390)}
15038 , the boot loader or similar code must ensure that the processor is in 24
15039 bit contiguous addressing mode before calling the SDCC startup code.
15057 option, variables will by default be placed into the XDATA segment.
15062 Segments may be placed anywhere in the 4 meg address space using the usual
15074 Note that if any segments are located above 64K, the -r flag must be passed
15075 to the linker to generate the proper segment relocations, and the Intel
15076 HEX output format must be used.
15077 The -r flag can be passed to the linker by using the option
15081 on the SDCC command line.
15082 However, currently the linker can not handle code segments > 64k.
15086 \begin_inset LatexCommand \index{Pragmas}
15093 SDCC supports the following #pragma directives:
15097 \begin_inset LatexCommand \index{\#pragma save}
15101 - this will save all current options to the save/restore stack.
15102 See #pragma\SpecialChar ~
15107 \begin_inset LatexCommand \index{\#pragma restore}
15111 - will restore saved options from the last save.
15112 saves & restores can be nested.
15113 SDCC uses a save/restore stack: save pushes current options to the stack,
15114 restore pulls current options from the stack.
15115 See #pragma\SpecialChar ~
15122 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15127 \begin_inset LatexCommand \index{function prologue}
15131 function1[,function2[,function3...]] - The compiler by default uses a caller
15132 saves convention for register saving across function calls, however this
15133 can cause unnecessary register pushing & popping
15134 \begin_inset LatexCommand \index{push/pop}
15138 when calling small functions from larger functions.
15139 This option can be used to switch off the register saving convention for
15140 the function names specified.
15141 The compiler will not save registers when calling these functions, extra
15142 code need to be manually inserted at the entry & exit for these functions
15143 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15144 reduce code & improve run time performance of the generated code.
15145 In the future the compiler (with inter procedural analysis) may be able
15146 to determine the appropriate scheme to use for each function call.
15157 -callee-saves command line option is used, the function names specified
15158 in #pragma\SpecialChar ~
15160 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15164 is appended to the list of functions specified in the command line.
15168 \begin_inset LatexCommand \index{\#pragma exclude}
15172 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15173 of pairs of push/pop
15174 \begin_inset LatexCommand \index{push/pop}
15183 \begin_inset LatexCommand \index{interrupt}
15196 The directive should be placed immediately before the ISR function definition
15197 and it affects ALL ISR functions following it.
15198 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15199 exclude\SpecialChar ~
15201 \begin_inset LatexCommand \index{\#pragma exclude}
15206 See also the related keyword _naked
15207 \begin_inset LatexCommand \index{\_naked}
15212 \begin_inset LatexCommand \index{\_\_naked}
15220 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15224 - the compiler will not warn you anymore for obvious mistakes, you'r on
15228 disable_warning <nnnn>
15229 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15233 - the compiler will not warn you anymore about warning number <nnnn>.
15237 \begin_inset LatexCommand \index{\#pragma nogcse}
15241 - will stop global common subexpression elimination.
15245 \begin_inset LatexCommand \index{\#pragma noinduction}
15249 - will stop loop induction optimizations.
15253 \begin_inset LatexCommand \index{\#pragma noinvariant}
15257 - will not do loop invariant optimizations.
15258 For more details see Loop Invariants in section
15259 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15267 \begin_inset LatexCommand \index{\#pragma noiv}
15271 - Do not generate interrupt
15272 \begin_inset LatexCommand \index{interrupt}
15276 vector table entries for all ISR functions defined after the pragma.
15277 This is useful in cases where the interrupt vector table must be defined
15278 manually, or when there is a secondary, manually defined interrupt vector
15280 for the autovector feature of the Cypress EZ-USB FX2).
15281 More elegantly this can be achieved by obmitting the optional interrupt
15282 number after the interrupt keyword, see section
15283 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15292 \begin_inset LatexCommand \index{\#pragma nojtbound}
15296 - will not generate code for boundary value checking, when switch statements
15297 are turned into jump-tables (dangerous).
15298 For more details see section
15299 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15307 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15311 - Will not do loop reversal optimization
15315 \begin_inset LatexCommand \index{\#pragma nooverlay}
15319 - the compiler will not overlay the parameters and local variables of a
15324 \begin_inset LatexCommand \index{\#pragma stackauto}
15339 \begin_inset LatexCommand \index{-\/-stack-auto}
15344 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15348 Parameters and Local Variables.
15352 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15356 - The compiler will optimize code generation towards fast code, possibly
15357 at the expense of code size.
15361 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15365 - The compiler will optimize code generation towards compact code, possibly
15366 at the expense of code speed.
15370 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15374 - The compiler will attempt to generate code that is both compact and fast,
15375 as long as meeting one goal is not a detriment to the other (this is the
15381 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15385 - Generally follow the C89 standard, but allow SDCC features that conflict
15386 with the standard (default).
15390 \begin_inset LatexCommand \index{\#pragma std\_c89}
15394 - Follow the C89 standard and disable SDCC features that conflict with the
15399 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15403 - Generally follow the C99 standard, but allow SDCC features that conflict
15404 with the standard (incomplete support).
15408 \begin_inset LatexCommand \index{\#pragma std\_c99}
15412 - Follow the C99 standard and disable SDCC features that conflict with the
15413 standard (incomplete support).
15417 \begin_inset LatexCommand \index{\#pragma codeseg}
15421 - Use this name (max.
15422 8 characters) for the code segment.
15426 \begin_inset LatexCommand \index{\#pragma constseg}
15430 - Use this name (max.
15431 8 characters) for the const segment.
15434 SDCPP supports the following #pragma directives:
15438 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15442 (+ | -) - switch _asm _endasm block preprocessing on / off.
15446 The pragma's are intended to be used to turn-on or off certain optimizations
15447 which might cause the compiler to generate extra stack / data space to
15448 store compiler generated temporary variables.
15449 This usually happens in large functions.
15450 Pragma directives should be used as shown in the following example, they
15451 are used to control options & optimizations for a given function; pragmas
15452 should be placed before and/or after a function, placing pragma's inside
15453 a function body could have unpredictable results.
15459 \begin_inset LatexCommand \index{\#pragma save}
15470 /* save the current settings */
15473 \begin_inset LatexCommand \index{\#pragma nogcse}
15482 /* turnoff global subexpression elimination */
15484 #pragma noinduction
15485 \begin_inset LatexCommand \index{\#pragma noinduction}
15489 /* turn off induction optimizations */
15512 \begin_inset LatexCommand \index{\#pragma restore}
15516 /* turn the optimizations back on */
15519 The compiler will generate a warning message when extra space is allocated.
15520 It is strongly recommended that the save and restore pragma's be used when
15521 changing options for a function.
15524 Defines Created by the Compiler
15527 The compiler creates the following #defines
15528 \begin_inset LatexCommand \index{\#defines}
15533 \begin_inset LatexCommand \index{Defines created by the compiler}
15543 \begin_inset Tabular
15544 <lyxtabular version="3" rows="11" columns="2">
15546 <column alignment="center" valignment="top" leftline="true" width="0">
15547 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15548 <row topline="true" bottomline="true">
15549 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15559 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15570 <row topline="true">
15571 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15577 \begin_inset LatexCommand \index{SDCC}
15584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15589 this Symbol is always defined
15593 <row topline="true">
15594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15600 \begin_inset LatexCommand \index{SDCC\_mcs51}
15605 \begin_inset LatexCommand \index{SDCC\_ds390}
15610 \begin_inset LatexCommand \index{SDCC\_z80}
15617 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15622 depending on the model used (e.g.: -mds390
15626 <row topline="true">
15627 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15633 \begin_inset LatexCommand \index{\_\_mcs51}
15638 \begin_inset LatexCommand \index{\_\_ds390}
15643 \begin_inset LatexCommand \index{\_\_hc08}
15648 \begin_inset LatexCommand \index{\_\_z80}
15655 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15660 depending on the model used (e.g.
15665 <row topline="true">
15666 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15672 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15702 <row topline="true">
15703 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15709 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15716 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15739 <row topline="true">
15740 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15746 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15753 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15776 <row topline="true">
15777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15783 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15790 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15813 <row topline="true">
15814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15820 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15827 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15850 <row topline="true">
15851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15857 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
15864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15877 <row topline="true" bottomline="true">
15878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15884 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
15891 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15911 Notes on supported Processors
15915 \begin_inset LatexCommand \label{sub:MCS51-variants}
15920 \begin_inset LatexCommand \index{MCS51 variants}
15927 MCS51 processors are available from many vendors and come in many different
15929 While they might differ considerably in respect to Special Function Registers
15930 the core MCS51 is usually not modified or is kept compatible.
15934 pdata access by SFR
15937 With the upcome of devices with internal xdata and flash memory devices
15939 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15943 as dedicated I/O port is becoming more popular.
15944 Switching the high byte for pdata
15945 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15949 access which was formerly done by port P2 is then achieved by a Special
15951 \begin_inset LatexCommand \index{sfr}
15956 In well-established MCS51 tradition the address of this
15960 is where the chip designers decided to put it.
15961 Needless to say that they didn't agree on a common name either.
15962 So that the startup code can correctly initialize xdata variables, you
15963 should define an sfr with the name _XPAGE
15966 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
15972 at the appropriate location if the default, port P2, is not used for this.
15978 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
15983 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
15988 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
15991 For more exotic implementations further customizations may be needed.
15993 \begin_inset LatexCommand \ref{sub:Startup-Code}
15997 for other possibilities.
16000 Other Features available by SFR
16003 Some MCS51 variants offer features like Double DPTR
16004 \begin_inset LatexCommand \index{DPTR}
16008 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16009 These are currently not used for the MCS51 port.
16010 If you absolutely need them you can fall back to inline assembly or submit
16017 The DS80C400 microcontroller has a rich set of peripherals.
16018 In its built-in ROM library it includes functions to access some of the
16019 features, among them is a TCP stack with IP4 and IP6 support.
16020 Library headers (currently in beta status) and other files are provided
16024 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16032 The Z80 and gbz80 port
16035 SDCC can target both the Zilog
16036 \begin_inset LatexCommand \index{Z80}
16040 and the Nintendo Gameboy's Z80-like gbz80
16041 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16046 The Z80 port is passed through the same
16049 \begin_inset LatexCommand \index{Regression test}
16055 as the MCS51 and DS390 ports, so floating point support, support for long
16056 variables and bitfield support is fine.
16057 See mailing lists and forums about interrupt routines.
16060 As always, the code is the authoritative reference - see z80/ralloc.c and
16063 \begin_inset LatexCommand \index{stack}
16067 frame is similar to that generated by the IAR Z80 compiler.
16068 IX is used as the base pointer, HL and IY are used as a temporary registers,
16069 and BC and DE are available for holding variables.
16071 \begin_inset LatexCommand \index{return value}
16075 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16077 The gbz80 port use the same set of registers for the return values, but
16078 in a different order of significance: E (one byte), DE (two bytes), or
16085 The port to the Motorola HC08
16086 \begin_inset LatexCommand \index{HC08}
16090 family has been added in October 2003, and is still undergoing some basic
16092 The code generator is complete, but the register allocation is still quite
16094 Some of the SDCC's standard C library functions have embedded non-HC08
16095 inline assembly and so are not yet usable.
16106 \begin_inset LatexCommand \index{PIC14}
16110 port still requires a major effort from the development community.
16111 However it can work for very simple code.
16114 C code and 14bit PIC code page
16115 \begin_inset LatexCommand \index{code page (pic14)}
16120 \begin_inset LatexCommand \index{RAM bank (pic14)}
16127 The linker organizes allocation for the code page and RAM banks.
16128 It does not have intimate knowledge of the code flow.
16129 It will put all the code section of a single asm file into a single code
16131 In order to make use of multiple code pages, separate asm files must be
16133 The compiler treats all functions of a single C file as being in the same
16134 code page unless it is non static.
16135 The compiler treats all local variables of a single C file as being in
16136 the same RAM bank unless it is an extern.
16140 To get the best follow these guide lines:
16143 make local functions static, as non static functions require code page selection
16147 Make local variables static as extern variables require RAM bank selection
16151 For devices that have multiple code pages it is more efficient to use the
16152 same number of files as pages, i.e.
16153 for the 16F877 use 4 separate files and i.e.
16154 for the 16F874 use 2 separate files.
16155 This way the linker can put the code for each file into different code
16156 pages and the compiler can allocate reusable variables more efficiently
16157 and there's less page selection overhead.
16158 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16159 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16162 Creating a device include file
16165 For generating a device include file use the support perl script inc2h.pl
16166 kept in directory support/script.
16172 For the interrupt function, use the keyword 'interrupt'
16173 \begin_inset LatexCommand \index{interrupt}
16177 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16178 there to avoid a syntax error - it ought to be fixed).
16184 void Intr(void) interrupt 0
16190 T0IF = 0; /* Clear timer interrupt */
16195 Linking and assembling
16198 For assembling you can use either GPUTILS'
16199 \begin_inset LatexCommand \index{gputils (pic tools)}
16203 gpasm.exe or MPLAB's mpasmwin.exe.
16204 GPUTILS is available from
16205 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16210 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16211 If you use MPLAB and an interrupt function then the linker script file
16212 vectors section will need to be enlarged to link with mplink.
16235 sdcc -S -V -mpic14 -p16F877 $<
16249 $(PRJ).hex: $(OBJS)
16259 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16281 sdcc -S -V -mpic14 -p16F877 $<
16291 mpasmwin /q /o $*.asm
16295 $(PRJ).hex: $(OBJS)
16305 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16308 Please note that indentations within a
16312 have to be done with a tabulator character.
16315 Command-line options
16318 Besides the switches common to all SDCC backends, the PIC14 port accepts
16319 the following options (for an updated list see sdcc -
16331 \labelwidthstring 00.00.0000
16343 -debug-extra emit debug info in assembly output
16345 \labelwidthstring 00.00.0000
16357 -no-pcode-opt disable (slightly faulty) optimization on pCode
16361 \layout Subsubsection
16363 error: missing definition for symbol
16364 \begin_inset Quotes sld
16368 \begin_inset Quotes srd
16374 The PIC14 port uses library routines to provide more complex operations
16375 like multiplication, division/modulus and (generic) pointer dereferencing.
16376 In order to add these routines to your project, you must link with PIC14's
16382 For single source file projects this is done automatically, more complex
16387 to the linker's arguments.
16388 Make sure you also add an include path for the library (using the -I switch
16390 \layout Subsubsection
16392 Processor mismatch in file
16393 \begin_inset Quotes sld
16397 \begin_inset Quotes srd
16403 This warning can usually be ignored due to the very good compatibility amongst
16404 14 bit PIC devices.
16407 You might also consider recompiling the library for your specific device
16408 by changing the ARCH=p16f877 (default target) entry in
16410 device/lib/pic/Makefile.in
16414 device/lib/pic/Makefile
16416 to reflect your device.
16417 This might even improve performance for smaller devices as unneccesary
16418 BANKSELs migth be removed.
16422 \layout Subsubsection
16427 Currently, data can only be initialized if it resides in the source file
16433 Data in other source files will silently
16441 \begin_inset LatexCommand \index{PIC16}
16449 \begin_inset LatexCommand \index{PIC16}
16453 port is the portion of SDCC that is responsible to produce code for the
16455 \begin_inset LatexCommand \index{Microchip}
16459 (TM) microcontrollers with 16 bit core.
16460 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16461 Currently supported devices are:
16465 \begin_inset Tabular
16466 <lyxtabular version="3" rows="4" columns="6">
16468 <column alignment="center" valignment="top" leftline="true" width="0">
16469 <column alignment="center" valignment="top" leftline="true" width="0">
16470 <column alignment="center" valignment="top" leftline="true" width="0">
16471 <column alignment="center" valignment="top" leftline="true" width="0">
16472 <column alignment="center" valignment="top" leftline="true" width="0">
16473 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16474 <row topline="true">
16475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16483 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16491 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16499 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16507 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16515 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16524 <row topline="true">
16525 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16533 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16541 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16549 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16557 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16565 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16574 <row topline="true">
16575 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16591 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16599 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16607 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16624 <row topline="true" bottomline="true">
16625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16633 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16641 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16649 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16663 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16681 PIC16 port supports the standard command line arguments as supposed, with
16682 the exception of certain cases that will be mentioned in the following
16685 \labelwidthstring 00.00.0000
16697 -callee-saves See -
16709 \labelwidthstring 00.00.0000
16721 -all-callee-saves All function arguments are passed on stack by default.
16724 There is no need to specify this in the command line.
16726 \labelwidthstring 00.00.0000
16738 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16739 no local variables.
16742 Port Specific Options
16743 \begin_inset LatexCommand \index{Options PIC16}
16750 The port specific options appear after the global options in the sdcc --help
16752 \layout Subsubsection
16757 General options enable certain port features and optimizations.
16759 \labelwidthstring 00.00.0000
16771 -stack-model=[model] Used in conjuction with the command above.
16772 Defines the stack model to be used, valid stack models are :
16775 \labelwidthstring 00.00.0000
16781 Selects small stack model.
16782 8 bit stack and frame pointers.
16783 Supports 256 bytes stack size.
16785 \labelwidthstring 00.00.0000
16791 Selects large stack model.
16792 16 bit stack and frame pointers.
16793 Supports 65536 bytes stack size.
16796 \labelwidthstring 00.00.0000
16808 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16809 unitialized data variables with [kword].
16810 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16812 \labelwidthstring 00.00.0000
16824 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16825 Useful for bootloaders.
16827 \labelwidthstring 00.00.0000
16839 -asm= sets the full path and name of an external assembler to call.
16841 \labelwidthstring 00.00.0000
16853 -link= sets the full path and name of an external linker to call.
16855 \labelwidthstring 00.00.0000
16867 -mplab-comp MPLAB compatibility option.
16868 Currently only suppresses special gpasm directives.
16869 \layout Subsubsection
16871 Optimization Options
16873 \labelwidthstring 00.00.0000
16885 -optimize-goto Try to use (conditional) BRA instead of GOTO
16887 \labelwidthstring 00.00.0000
16899 -optimize-cmp Try to optimize some compares.
16901 \labelwidthstring 00.00.0000
16913 -optimize-df Analyze the dataflow of the generated code and improve it.
16915 \labelwidthstring 00.00.0000
16927 -obanksel=nn Set optimization level for inserting BANKSELs.
16932 \labelwidthstring 00.00.0000
16936 \labelwidthstring 00.00.0000
16938 1 checks previous used register and if it is the same then does not emit
16939 BANKSEL, accounts only for labels.
16941 \labelwidthstring 00.00.0000
16943 2 tries to check the location of (even different) symbols and removes BANKSELs
16944 if they are in the same bank.
16949 Important: There might be problems if the linker script has data sections
16950 across bank borders!
16952 \layout Subsubsection
16956 \labelwidthstring 00.00.0000
16968 -nodefaultlibs do not link default libraries when linking
16970 \labelwidthstring 00.00.0000
16982 -no-crt Don't link the default run-time modules
16984 \labelwidthstring 00.00.0000
16996 -use-crt= Use a custom run-time module instead of the defaults.
16997 \layout Subsubsection
17002 Debugging options enable extra debugging information in the output files.
17004 \labelwidthstring 00.00.0000
17016 -debug-xtra Similar to -
17027 \begin_inset LatexCommand \index{-\/-debug}
17031 , but dumps more information.
17033 \labelwidthstring 00.00.0000
17045 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17047 <source> is the name of the file compiled.
17049 \labelwidthstring 00.00.0000
17061 -pcode-verbose Enable pcode debugging information in translation.
17063 \labelwidthstring 00.00.0000
17075 -denable-peeps Force the usage of peepholes.
17078 \labelwidthstring 00.00.0000
17090 -gstack Trace push/pops for stack pointer overflow
17092 \labelwidthstring 00.00.0000
17104 -call-tree dump call tree in .calltree file
17107 Enviromental Variables
17110 There is a number of enviromental variables that can be used when running
17111 SDCC to enable certain optimizations or force a specific program behaviour.
17112 these variables are primarily for debugging purposes so they can be enabled/dis
17116 Currently there is only two such variables available:
17118 \labelwidthstring 00.00.0000
17120 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17121 bitfields is optimized by directly loading FSR0 with the address of the
17122 bitfield structure.
17123 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17125 This step saves data ram and code space for functions that perform heavy
17128 80 bytes of code space are saved when compiling malloc.c with this option).
17131 \labelwidthstring 00.00.0000
17133 NO_REG_OPT do not perform pCode registers optimization.
17134 This should be used for debugging purposes.
17135 In some where bugs in the pcode optimizer are found, users can benefit
17136 from temporarily disabling the optimizer until the bug is fixed.
17139 Preprocessor Macros
17142 PIC16 port defines the following preprocessor macros while translating a
17147 \begin_inset Tabular
17148 <lyxtabular version="3" rows="6" columns="2">
17150 <column alignment="center" valignment="top" leftline="true" width="0">
17151 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17152 <row topline="true" bottomline="true">
17153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17161 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17170 <row topline="true">
17171 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17179 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17184 Port identification
17188 <row topline="true">
17189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17207 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17212 Port identification (same as above)
17216 <row topline="true">
17217 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17225 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17230 MCU Identification.
17235 is the microcontrol identification number, i.e.
17240 <row topline="true">
17241 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17259 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17264 MCU Identification (same as above)
17268 <row topline="true" bottomline="true">
17269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17277 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17282 nnn = SMALL or LARGE respectively according to the stack model used
17293 In addition the following macros are defined when calling assembler:
17297 \begin_inset Tabular
17298 <lyxtabular version="3" rows="4" columns="2">
17300 <column alignment="center" valignment="top" leftline="true" width="0">
17301 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17302 <row topline="true" bottomline="true">
17303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17311 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17320 <row topline="true">
17321 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17329 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17334 MCU Identification.
17339 is the microcontrol identification number, i.e.
17344 <row topline="true">
17345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17358 nnn = SMALL or LARGE respectively according to the memory model used for
17363 <row topline="true" bottomline="true">
17364 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17372 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17377 nnn = SMALL or LARGE respectively according to the stack model used
17392 \begin_inset LatexCommand \index{PIC16}
17396 port uses the following directories for searching header files and libraries.
17400 \begin_inset Tabular
17401 <lyxtabular version="3" rows="3" columns="4">
17403 <column alignment="center" valignment="top" leftline="true" width="0">
17404 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17405 <column alignment="center" valignment="top" width="0">
17406 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17407 <row topline="true" bottomline="true">
17408 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17416 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17424 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17432 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17441 <row topline="true">
17442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17447 PREFIX/sdcc/include/pic16
17450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17455 PIC16 specific headers
17458 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17466 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17475 <row topline="true" bottomline="true">
17476 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17481 PREFIX/sdcc/lib/pic16
17484 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17489 PIC16 specific libraries
17492 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17500 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17517 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17524 PIC16 port currently supports the following pragmas:
17526 \labelwidthstring 00.00.0000
17528 stack pragma stack forces the code generator to initialize the stack & frame
17529 pointers at a specific address.
17530 This is an adhoc solution for cases where no STACK directive is available
17531 in the linker script or gplink is not instructed to create a stack section.
17533 The stack pragma should be used only once in a project.
17534 Multiple pragmas may result in indeterminate behaviour of the program.
17540 The old format (ie.
17541 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17542 cross page boundaries (or even exceed the available data RAM) and crash
17544 Make sure that stack does not cross page boundaries when using the SMALL
17550 The format is as follows:
17553 #pragma stack bottom_address [stack_size]
17560 is the lower bound of the stack section.
17561 The stack pointer initially will point at address (bottom_address+stack_size-1).
17569 /* initializes stack of 100 bytes at RAM address 0x200 */
17572 #pragma stack 0x200 100
17575 If the stack_size field is omitted then a stack is created with the default
17577 This size might be enough for most programs, but its not enough for operations
17578 with deep function nesting or excessive stack usage.
17580 \labelwidthstring 00.00.0000
17584 This pragma is deprecated.
17585 Its use will cause a warning message to be issued.
17591 \labelwidthstring 00.00.0000
17593 code place a function symbol at static FLASH address
17601 /* place function test_func at 0x4000 */
17604 #pragma code test_func 0x4000
17608 \labelwidthstring 00.00.0000
17610 library instructs the linker to use a library module.
17615 #pragma library module_name
17622 can be any library or object file (including its path).
17623 Note that there are four reserved keywords which have special meaning.
17628 \begin_inset Tabular
17629 <lyxtabular version="3" rows="6" columns="3">
17631 <column alignment="center" valignment="top" leftline="true" width="0">
17632 <column alignment="block" valignment="top" leftline="true" width="20page%">
17633 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17634 <row topline="true" bottomline="true">
17635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17660 <row topline="true">
17661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17671 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17676 ignore all library pragmas
17679 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17690 <row topline="true">
17691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17701 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17709 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17722 <row topline="true">
17723 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17738 link the Math libarary
17741 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17754 <row topline="true">
17755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17765 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17770 link the I/O library
17773 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17786 <row topline="true" bottomline="true">
17787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17797 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17802 link the debug library
17805 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17824 * is the device number, i.e.
17825 452 for PIC18F452 MCU.
17828 This feature allows for linking with specific libraries withoug having to
17829 explicit name them in the command line.
17834 keyword will reject all modules specified by the library pragma.
17836 \labelwidthstring 00.00.0000
17838 udata pragma udata instructs the compiler to emit code so that linker will
17839 place a variable at a specific memory bank
17847 /* places variable foo at bank2 */
17850 #pragma udata bank2 foo
17856 In order for this pragma to work extra SECTION directives should be added
17857 in the .lkr script.
17858 In the following example a sample .lkr file is shown:
17863 // Sample linker script for the PIC18F452 processor
17869 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
17872 CODEPAGE NAME=page START=0x2A END=0x7FFF
17875 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
17878 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
17881 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
17884 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
17887 ACCESSBANK NAME=accessram START=0x0 END=0x7F
17892 DATABANK NAME=gpr0 START=0x80 END=0xFF
17895 DATABANK NAME=gpr1 START=0x100 END=0x1FF
17898 DATABANK NAME=gpr2 START=0x200 END=0x2FF
17901 DATABANK NAME=gpr3 START=0x300 END=0x3FF
17904 DATABANK NAME=gpr4 START=0x400 END=0x4FF
17907 DATABANK NAME=gpr5 START=0x500 END=0x5FF
17910 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
17915 SECTION NAME=CONFIG ROM=config
17920 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
17923 SECTION NAME=bank1 RAM=gpr1 # should be added to link
17926 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
17929 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
17932 SECTION NAME=bank4 RAM=gpr4
17935 SECTION NAME=bank5 RAM=gpr5
17938 The linker will recognise the section name set in the pragma statement and
17939 will position the variable at the memory bank set with the RAM field at
17940 the SECTION line in the linker script file.
17944 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
17951 There is one main header file that can be included to the source files using
17958 This header file contains the definitions for the processor special registers,
17959 so it is necessary if the source accesses them.
17960 It can be included by adding the following line in the beginning of the
17964 #include <pic18fregs.h>
17967 The specific microcontroller is selected within the pic18fregs.h automatically,
17968 so the same source can be used with a variety of devices.
17974 The libraries that PIC16
17975 \begin_inset LatexCommand \index{PIC16}
17979 port depends on are the microcontroller device libraries which contain
17980 the symbol definitions for the microcontroller special function registers.
17981 These libraries have the format pic18fxxxx.lib, where
17985 is the microcontroller identification number.
17986 The specific library is selected automatically by the compiler at link
17987 stage according to the selected device.
17990 Libraries are created with gplib which is part of the gputils package
17991 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
17996 \layout Subsubsection*
17998 Building the libraries
18001 Before using SDCC/pic16 there are some libraries that need to be compiled.
18002 This process is not done automatically by SDCC since not all users use
18003 SDCC for pic16 projects.
18004 So each user should compile the libraries separately.
18007 The steps to compile the pic16 libraries under Linux are:
18010 cd device/lib/pic16
18025 su -c 'make install' # install the libraries, you need the root password
18028 If you need to install the headers too, do:
18034 su -c 'make install' # install the headers, you need the root password
18037 There exist a special target to build the I/O libraries.
18038 This target is not automatically build because it will build the I/O library
18044 This way building will take quite a lot of time.
18045 Users are advised to edit the
18047 device/lib/pic16/pics.build
18049 file and then execute:
18058 The following memory models are supported by the PIC16 port:
18067 Memory model affects the default size of pointers within the source.
18068 The sizes are shown in the next table:
18072 \begin_inset Tabular
18073 <lyxtabular version="3" rows="3" columns="3">
18075 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18076 <column alignment="center" valignment="top" leftline="true" width="0">
18077 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18078 <row topline="true" bottomline="true">
18079 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18084 Pointer sizes according to memory model
18087 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18095 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18104 <row topline="true" bottomline="true">
18105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18113 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18121 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18130 <row topline="true" bottomline="true">
18131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18139 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18147 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18163 It is advisable that all sources within a project are compiled with the
18165 If one wants to override the default memory model, this can be done by
18166 declaring a pointer as
18175 Far selects large memory model's pointers, while near selects small memory
18179 The standard device libraries (see
18180 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18184 ) contain no reference to pointers, so they can be used with both memory
18191 The stack implementation for the PIC16 port uses two indirect registers,
18194 \labelwidthstring 00.00.0000
18196 FSR1 is assigned as stack pointer
18198 \labelwidthstring 00.00.0000
18200 FSR2 is assigned as frame pointer
18203 The following stack models are supported by the PIC16 port
18224 model means that only the FSRxL byte is used to access stack and frame,
18231 uses both FSRxL and FSRxH registers.
18232 The following table shows the stack/frame pointers sizes according to stack
18233 model and the maximum space they can address:
18237 \begin_inset Tabular
18238 <lyxtabular version="3" rows="3" columns="3">
18240 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18241 <column alignment="center" valignment="top" leftline="true" width="0">
18242 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18243 <row topline="true" bottomline="true">
18244 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18249 Stack & Frame pointer sizes according to stack model
18252 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18260 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18269 <row topline="true">
18270 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18278 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18286 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18295 <row topline="true" bottomline="true">
18296 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18304 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18312 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18332 stack model is currently not working properly throughout the code generator.
18333 So its use is not advised.
18334 Also there are some other points that need special care:
18339 Do not create stack sections with size more than one physical bank (that
18343 Stack sections should no cross physical bank limits (i.e.
18344 #pragma stack 0x50 0x100)
18347 These limitations are caused by the fact that only FSRxL is modified when
18348 using SMALL stack model, so no more than 256 bytes of stack can be used.
18349 This problem will disappear after LARGE model is fully implemented.
18355 In addition to the standard SDCC function keywords, PIC16 port makes available
18358 \labelwidthstring 00.00.0000
18360 wparam Use the WREG to pass one byte of the first function argument.
18361 This improves speed but you may not use this for functions with arguments
18362 that are called via function pointers, otherwise the first byte of the
18363 first parameter will get lost.
18367 void func_wparam(int a) wparam
18373 /* WREG hold the lower part of a */
18376 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18386 This keyword replaces the deprecated wparam pragma.
18388 \labelwidthstring 00.00.0000
18390 shadowregs When entering/exiting an ISR, it is possible to take advantage
18391 of the PIC18F hardware shadow registers which hold the values of WREG,
18392 STATUS and BSR registers.
18393 This can be done by adding the keyword
18401 keyword in the function's header.
18404 void isr_shadow(void) shadowregs interrupt 1
18420 instructs the code generator not to store/restore WREG, STATUS, BSR when
18421 entering/exiting the ISR.
18424 Function return values
18427 Return values from functions are placed to the appropriate registers following
18428 a modified Microchip policy optimized for SDCC.
18429 The following table shows these registers:
18433 \begin_inset Tabular
18434 <lyxtabular version="3" rows="6" columns="2">
18436 <column alignment="center" valignment="top" leftline="true" width="0">
18437 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18438 <row topline="true" bottomline="true">
18439 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18447 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18452 destination register
18456 <row topline="true">
18457 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18465 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18474 <row topline="true">
18475 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18483 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18492 <row topline="true">
18493 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18501 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18510 <row topline="true">
18511 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18519 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18524 FSR0L:PRODH:PRODL:WREG
18528 <row topline="true" bottomline="true">
18529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18542 on stack, FSR0 points to the beginning
18556 An interrupt servive routine (ISR) is declared using the
18563 void isr(void) interrupt
18581 is the interrupt number, which for PIC18F devices can be:
18585 \begin_inset Tabular
18586 <lyxtabular version="3" rows="4" columns="3">
18588 <column alignment="center" valignment="top" leftline="true" width="0">
18589 <column alignment="center" valignment="top" leftline="true" width="0">
18590 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18591 <row topline="true" bottomline="true">
18592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18602 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18610 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18615 Interrupt Vector Address
18619 <row topline="true">
18620 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18628 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18636 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18645 <row topline="true">
18646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18663 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18677 HIGH priority interrupts
18680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18689 <row topline="true" bottomline="true">
18690 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18698 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18703 LOW priority interrupts
18706 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18722 When generating assembly code for ISR the code generator places a
18728 Interrupt Vector Address
18730 which points at the genetated ISR.
18731 This single GOTO instruction is part of an automatically generated
18733 interrupt entry point
18736 The actuall ISR code is placed as normally would in the code space.
18737 Upon interrupt request, the GOTO instruction is executed which jumps to
18739 When declaring interrupt functions as _naked this GOTO instruction is
18744 The whole interrupt functions is therefore placed at the Interrupt Vector
18745 Address of the specific interrupt.
18746 This is not a problem for the LOW priority interrupts, but it is a problem
18747 for the RESET and the HIGH priority interrupts because code may be written
18748 at the next interrupt´s vector address and cause undeterminate program
18749 behaviour if that interrupt is raised.
18755 This is not a problem when
18758 this is a HIGH interrupt ISR and LOW interrupts are
18765 when the ISR is small enough not to reach the next interrupt´s vector address.
18775 is possible to be omitted.
18776 This way a function is generated similar to an ISR, but it is not assigned
18780 When entering an interrupt, currently the PIC16
18781 \begin_inset LatexCommand \index{PIC16}
18785 port automatically saves the following registers:
18797 PROD (PRODL and PRODH)
18800 FSR0 (FSR0L and FSR0H)
18803 These registers are restored upon return from the interrupt routine.
18809 NOTE that when the _naked attribute is specified for an interrupt routine,
18810 then NO registers are stored or restored.
18819 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18820 There are 3 types of generic pointers currently implemented data, code
18821 and eeprom pointers.
18822 They are differentiated by the value of the 7th and 6th bits of the upper
18827 \begin_inset Tabular
18828 <lyxtabular version="3" rows="5" columns="5">
18830 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18831 <column alignment="center" valignment="top" width="0">
18832 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18833 <column alignment="center" valignment="top" width="0">
18834 <column alignment="left" valignment="top" rightline="true" width="0">
18835 <row topline="true" bottomline="true">
18836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18844 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18852 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18860 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18865 rest of the pointer
18868 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18877 <row topline="true" bottomline="true">
18878 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18886 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18894 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18902 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18911 uuuuuu uuuuxxxx xxxxxxxx
18914 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18919 a 12-bit data pointer in data RAM memory
18923 <row bottomline="true">
18924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18932 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18948 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18957 uxxxxx xxxxxxxx xxxxxxxx
18960 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18965 a 21-bit code pointer in FLASH memory
18969 <row bottomline="true">
18970 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18978 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18986 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18994 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19003 uuuuuu uuuuuuxx xxxxxxxx
19006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19011 a 10-bit eeprom pointer in EEPROM memory
19015 <row bottomline="true">
19016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19024 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19032 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19040 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19049 xxxxxx xxxxxxxx xxxxxxxx
19052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19057 unimplemented pointer type
19068 Generic pointer are read and written with a set of library functions which
19069 read/write 1, 2, 3, 4 bytes.
19073 \layout Subsubsection
19075 Standard I/O Streams
19082 the type FILE is defined as:
19085 typedef char * FILE;
19088 This type is the stream type implemented I/O in the PIC18F devices.
19089 Also the standard input and output streams are declared in stdio.h:
19092 extern FILE * stdin;
19095 extern FILE * stdout;
19098 The FILE type is actually a generic pointer which defines one more type
19099 of generic pointers, the
19104 This new type has the format:
19108 \begin_inset Tabular
19109 <lyxtabular version="3" rows="2" columns="7">
19111 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19112 <column alignment="center" valignment="top" width="0">
19113 <column alignment="center" valignment="top" leftline="true" width="0">
19114 <column alignment="center" valignment="top" leftline="true" width="0">
19115 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19116 <column alignment="center" valignment="top" width="0">
19117 <column alignment="left" valignment="top" rightline="true" width="0">
19118 <row topline="true" bottomline="true">
19119 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19127 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19135 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19151 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19159 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19164 rest of the pointer
19167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19176 <row topline="true" bottomline="true">
19177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19185 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19193 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19201 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19209 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19217 <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">
19234 upper byte high nubble is 0x2n, the rest are zeroes
19245 Currently implemented there are 3 types of streams defined:
19249 \begin_inset Tabular
19250 <lyxtabular version="3" rows="4" columns="4">
19252 <column alignment="center" valignment="top" leftline="true" width="0">
19253 <column alignment="center" valignment="top" leftline="true" width="0">
19254 <column alignment="center" valignment="top" leftline="true" width="0">
19255 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19256 <row topline="true" bottomline="true">
19257 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19265 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19273 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19281 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19290 <row topline="true">
19291 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19299 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19309 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19322 Writes/Reads characters via the USART peripheral
19326 <row topline="true">
19327 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19345 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19353 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19358 Writes/Reads characters via the MSSP peripheral
19362 <row topline="true" bottomline="true">
19363 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19371 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19381 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19389 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19394 Writes/Reads characters via used defined functions
19405 The stream identifiers are declared as macros in the stdio.h header.
19408 In the libc library there exist the functions that are used to write to
19409 each of the above streams.
19412 \labelwidthstring 00.00.0000
19424 _stream_usart_putchar writes a character at the USART stream
19426 \labelwidthstring 00.00.0000
19438 _stream_mssp_putchar writes a character at the MSSP stream
19440 \labelwidthstring 00.00.0000
19442 putchar dummy function.
19443 This writes a character to a user specified manner.
19446 In order to increase performance
19450 is declared in stdio.h as having its parameter in WREG (it has the wparam
19452 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19453 in a user-friendly way.
19458 is the name of the variable that holds the character to print.
19459 An example follows:
19462 #include <pic18fregs.h>
19474 PORTA = c; /* dump character c to PORTA */
19487 stdout = STREAM_USER; /* this is not necessery, since stdout points
19490 * by default to STREAM_USER */
19493 printf (¨This is a printf test
19501 \layout Subsubsection
19506 PIC16 contains an implementation of the printf-family of functions.
19507 There exist the following functions:
19510 extern unsigned int sprintf(char *buf, char *fmt, ...);
19513 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19518 extern unsigned int printf(char *fmt, ...);
19521 extern unsigned int vprintf(char *fmt, va_lista ap);
19526 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19529 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19532 For sprintf and vsprintf
19536 should normally be a data pointer where the resulting string will be placed.
19537 No range checking is done so the user should allocate the necessery buffer.
19538 For fprintf and vfprintf
19542 should be a stream pointer (i.e.
19543 stdout, STREAM_MSSP, etc...).
19544 \layout Subsubsection
19549 The PIC18F family of microcontrollers supports a number of interrupt sources.
19550 A list of these interrupts is shown in the following table:
19554 \begin_inset Tabular
19555 <lyxtabular version="3" rows="11" columns="4">
19557 <column alignment="left" valignment="top" leftline="true" width="0">
19558 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19559 <column alignment="left" valignment="top" leftline="true" width="0">
19560 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19561 <row topline="true" bottomline="true">
19562 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19570 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19578 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19586 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19595 <row topline="true">
19596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19604 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19609 PORTB change interrupt
19612 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19620 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19625 EEPROM/FLASH write complete interrupt
19629 <row topline="true">
19630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19638 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19643 INT0 external interrupt
19646 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19654 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19659 Bus collision interrupt
19663 <row topline="true">
19664 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19672 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19677 INT1 external interrupt
19680 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19688 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19693 Low voltage detect interrupt
19697 <row topline="true">
19698 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19706 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19711 INT2 external interrupt
19714 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19727 Parallel slave port interrupt
19731 <row topline="true">
19732 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19740 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19745 CCP1 module interrupt
19748 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19756 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19761 AD convertion complete interrupt
19765 <row topline="true">
19766 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19774 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19779 CCP2 module interrupt
19782 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19790 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19795 USART receive interrupt
19799 <row topline="true">
19800 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19808 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19813 TMR0 overflow interrupt
19816 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19824 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19829 USART transmit interrupt
19833 <row topline="true">
19834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19847 TMR1 overflow interrupt
19850 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19858 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19863 SSP receive/transmit interrupt
19867 <row topline="true">
19868 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19876 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19881 TMR2 matches PR2 interrupt
19884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19891 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19899 <row topline="true" bottomline="true">
19900 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19908 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19913 TMR3 overflow interrupt
19916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19938 The prototypes for these names are defined in the header file
19945 In order to simplify signal handling, a number of macros is provided:
19947 \labelwidthstring 00.00.0000
19949 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
19950 high priority interrupts.
19955 is the function name to use.
19957 \labelwidthstring 00.00.0000
19959 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
19960 low priority interrupt.
19965 is the function name to use.
19967 \labelwidthstring 00.00.0000
19969 DEF_HANDLER(sig,handler) define a handler for signal
19973 \labelwidthstring 00.00.0000
19975 END_DEF end the declaration of the dispatch table.
19978 Additionally there are two more macros to simplify the declaration of the
19981 \labelwidthstring 00.00.0000
19985 SIGHANDLER(handler)
19987 this declares the function prototype for the
19993 \labelwidthstring 00.00.0000
19995 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
19998 An example of using the macros above is shown below:
20001 #include <pic18fregs.h>
20004 #include <signal.h>
20008 DEF_INTHIGH(high_int)
20011 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20014 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20021 SIGHANDLER(_tmr0_handler)
20027 /* action to be taken when timer 0 overflows */
20034 SIGHANDLERNAKED(_bcol_handler)
20043 /* action to be taken when bus collision occurs */
20059 Special care should be taken when using the above scheme:
20062 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20065 when declaring SIGHANDLERNAKED handler never forget to use
20069 for proper returning.
20075 Here you can find some general tips for compiling programs with SDCC/pic16.
20076 \layout Subsubsection
20081 The default stack size (that is 64 bytes) probably is enough for many programs.
20082 One must take care that when there are many levels of function nesting,
20083 or there is excessive usage of stack, its size should be extended.
20084 An example of such a case is the printf/sprintf family of functions.
20085 If you encounter problems like not being able to print integers, then you
20086 need to set the stack size around the maximum (256 for small stack model).
20087 The following diagram shows what happens when calling printf to print an
20091 printf () --> ltoa () --> ultoa () --> divschar ()
20094 It is should be understood that stack is easily consumed when calling complicate
20096 Using command line arguments like -
20106 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20108 Other ways to reduce stack usage may exist.
20111 Debugging with SDCDB
20112 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20117 \begin_inset LatexCommand \index{sdcdb (debugger)}
20124 SDCC is distributed with a source level debugger
20125 \begin_inset LatexCommand \index{Debugger}
20130 The debugger uses a command line interface, the command repertoire of the
20131 debugger has been kept as close to gdb
20132 \begin_inset LatexCommand \index{gdb}
20136 (the GNU debugger) as possible.
20137 The configuration and build process is part of the standard compiler installati
20138 on, which also builds and installs the debugger in the target directory
20139 specified during configuration.
20140 The debugger allows you debug BOTH at the C source and at the ASM source
20142 Sdcdb is currently not available on Win32 platforms.
20145 Compiling for Debugging
20159 \begin_inset LatexCommand \index{-\/-debug}
20163 option must be specified for all files for which debug information is to
20165 The complier generates a .adb file for each of these files.
20166 The linker creates the .cdb
20167 \begin_inset LatexCommand \index{<file>.cdb}
20172 \begin_inset LatexCommand \index{<file>.adb}
20176 files and the address information.
20177 This .cdb is used by the debugger.
20180 How the Debugger Works
20193 -debug option is specified the compiler generates extra symbol information
20194 some of which are put into the assembler source and some are put into the
20196 Then the linker creates the .cdb file from the individual .adb files with
20197 the address information for the symbols.
20198 The debugger reads the symbolic information generated by the compiler &
20199 the address information generated by the linker.
20200 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20201 execution is controlled by the debugger.
20202 When a command is issued for the debugger, it translates it into appropriate
20203 commands for the simulator.
20206 Starting the Debugger
20209 The debugger can be started using the following command line.
20210 (Assume the file you are debugging has the file name foo).
20224 The debugger will look for the following files.
20227 foo.c - the source file.
20230 foo.cdb - the debugger symbol information file.
20233 foo.ihx - the Intel hex format
20234 \begin_inset LatexCommand \index{Intel hex format}
20241 Command Line Options
20254 -directory=<source file directory> this option can used to specify the directory
20256 The debugger will look into the directory list specified for source, cdb
20258 The items in the directory list must be separated by ':', e.g.
20259 if the source files can be in the directories /home/src1 and /home/src2,
20270 -directory option should be -
20280 -directory=/home/src1:/home/src2.
20281 Note there can be no spaces in the option.
20285 -cd <directory> - change to the <directory>.
20288 -fullname - used by GUI front ends.
20291 -cpu <cpu-type> - this argument is passed to the simulator please see the
20292 simulator docs for details.
20295 -X <Clock frequency > this options is passed to the simulator please see
20296 the simulator docs for details.
20299 -s <serial port file> passed to simulator see the simulator docs for details.
20302 -S <serial in,out> passed to simulator see the simulator docs for details.
20305 -k <port number> passed to simulator see the simulator docs for details.
20311 As mentioned earlier the command interface for the debugger has been deliberatel
20312 y kept as close the GNU debugger gdb, as possible.
20313 This will help the integration with existing graphical user interfaces
20314 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20315 If you use a graphical user interface for the debugger you can skip this
20317 \layout Subsubsection*
20319 break [line | file:line | function | file:function]
20322 Set breakpoint at specified line or function:
20331 sdcdb>break foo.c:100
20333 sdcdb>break funcfoo
20335 sdcdb>break foo.c:funcfoo
20336 \layout Subsubsection*
20338 clear [line | file:line | function | file:function ]
20341 Clear breakpoint at specified line or function:
20350 sdcdb>clear foo.c:100
20352 sdcdb>clear funcfoo
20354 sdcdb>clear foo.c:funcfoo
20355 \layout Subsubsection*
20360 Continue program being debugged, after breakpoint.
20361 \layout Subsubsection*
20366 Execute till the end of the current function.
20367 \layout Subsubsection*
20372 Delete breakpoint number 'n'.
20373 If used without any option clear ALL user defined break points.
20374 \layout Subsubsection*
20376 info [break | stack | frame | registers ]
20379 info break - list all breakpoints
20382 info stack - show the function call stack.
20385 info frame - show information about the current execution frame.
20388 info registers - show content of all registers.
20389 \layout Subsubsection*
20394 Step program until it reaches a different source line.
20395 Note: pressing <return> repeats the last command.
20396 \layout Subsubsection*
20401 Step program, proceeding through subroutine calls.
20402 \layout Subsubsection*
20407 Start debugged program.
20408 \layout Subsubsection*
20413 Print type information of the variable.
20414 \layout Subsubsection*
20419 print value of variable.
20420 \layout Subsubsection*
20425 load the given file name.
20426 Note this is an alternate method of loading file for debugging.
20427 \layout Subsubsection*
20432 print information about current frame.
20433 \layout Subsubsection*
20438 Toggle between C source & assembly source.
20439 \layout Subsubsection*
20441 ! simulator command
20444 Send the string following '!' to the simulator, the simulator response is
20446 Note the debugger does not interpret the command being sent to the simulator,
20447 so if a command like 'go' is sent the debugger can loose its execution
20448 context and may display incorrect values.
20449 \layout Subsubsection*
20456 My name is Bobby Brown"
20459 Interfacing with DDD
20462 The screenshot was converted from png to eps with:
20463 \begin_inset Quotes sld
20466 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20467 \begin_inset Quotes srd
20470 which produces a pretty compact eps file which is free from compression
20474 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20475 as this broke the build system on Sourceforge (pdf-file was broken).
20481 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
20487 shows a screenshot of a debugging session with DDD
20488 \begin_inset LatexCommand \index{DDD (debugger)}
20492 (Unix only) on a simulated 8032.
20493 The debugging session might not run as smoothly as the screenshot suggests.
20494 The debugger allows setting of breakpoints, displaying and changing variables,
20495 single stepping through C and assembler code.
20498 The source was compiled with
20521 -debug ddd_example.c
20534 and DDD was invoked with
20541 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20544 Interfacing with XEmacs
20545 \begin_inset LatexCommand \index{XEmacs}
20550 \begin_inset LatexCommand \index{Emacs}
20557 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20558 sdcdb.el and sdcdbsrc.el.
20559 These two files can be found in the $(prefix)/bin directory after the installat
20561 These files need to be loaded into XEmacs for the interface to work.
20562 This can be done at XEmacs startup time by inserting the following into
20563 your '.xemacs' file (which can be found in your HOME directory):
20569 (load-file sdcdbsrc.el)
20575 .xemacs is a lisp file so the () around the command is REQUIRED.
20576 The files can also be loaded dynamically while XEmacs is running, set the
20577 environment variable 'EMACSLOADPATH' to the installation bin directory
20578 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20579 To start the interface enter the following command:
20593 You will prompted to enter the file name to be debugged.
20598 The command line options that are passed to the simulator directly are bound
20599 to default values in the file sdcdbsrc.el.
20600 The variables are listed below, these values maybe changed as required.
20603 sdcdbsrc-cpu-type '51
20606 sdcdbsrc-frequency '11059200
20609 sdcdbsrc-serial nil
20612 The following is a list of key mapping for the debugger interface.
20623 ;;key\SpecialChar ~
20637 binding\SpecialChar ~
20661 ;;---\SpecialChar ~
20675 -------\SpecialChar ~
20717 sdcdb-next-from-src\SpecialChar ~
20745 sdcdb-back-from-src\SpecialChar ~
20773 sdcdb-cont-from-src\SpecialChar ~
20783 SDCDB continue command
20801 sdcdb-step-from-src\SpecialChar ~
20829 sdcdb-whatis-c-sexp\SpecialChar ~
20839 SDCDB ptypecommand for data at
20906 sdcdbsrc-delete\SpecialChar ~
20920 SDCDB Delete all breakpoints if no arg
20969 given or delete arg (C-u arg x)
20987 sdcdbsrc-frame\SpecialChar ~
21002 SDCDB Display current frame if no arg,
21051 given or display frame arg
21118 sdcdbsrc-goto-sdcdb\SpecialChar ~
21128 Goto the SDCDB output buffer
21146 sdcdb-print-c-sexp\SpecialChar ~
21157 SDCDB print command for data at
21224 sdcdbsrc-goto-sdcdb\SpecialChar ~
21234 Goto the SDCDB output buffer
21252 sdcdbsrc-mode\SpecialChar ~
21268 Toggles Sdcdbsrc mode (turns it off)
21283 sdcdb-finish-from-src\SpecialChar ~
21291 SDCDB finish command
21306 sdcdb-break\SpecialChar ~
21324 Set break for line with point
21339 sdcdbsrc-mode\SpecialChar ~
21355 Toggle Sdcdbsrc mode
21370 sdcdbsrc-srcmode\SpecialChar ~
21393 Here are a few guidelines that will help the compiler generate more efficient
21394 code, some of the tips are specific to this compiler others are generally
21395 good programming practice.
21398 Use the smallest data type to represent your data-value.
21399 If it is known in advance that the value is going to be less than 256 then
21400 use an 'unsigned char' instead of a 'short' or 'int'.
21401 Please note, that ANSI C requires both signed and unsigned chars to be
21402 promoted to 'signed int' before doing any operation.
21404 \begin_inset LatexCommand \index{type promotion}
21408 can be omitted, if the result is the same.
21409 The effect of the promotion rules together with the sign-extension is often
21416 unsigned char uc = 0xfe;
21418 if (uc * uc < 0) /* this is true! */
21437 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21447 (unsigned char) -12 / (signed char) -3 = ...
21450 No, the result is not 4:
21455 (int) (unsigned char) -12 / (int) (signed char) -3 =
21457 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21459 (int) 0x00f4 / (int) 0xfffd =
21461 (int) 0x00f4 / (int) 0xfffd =
21463 (int) 244 / (int) -3 =
21465 (int) -81 = (int) 0xffaf;
21468 Don't complain, that gcc gives you a different result.
21469 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21470 Therefore the results are different.
21473 \begin_inset Quotes sld
21477 \begin_inset Quotes srd
21483 If well-defined overflow characteristics are important and negative values
21484 are not, or if you want to steer clear of sign-extension problems when
21485 manipulating bits or bytes, use one of the corresponding unsigned types.
21486 (Beware when mixing signed and unsigned values in expressions, though.)
21488 Although character types (especially unsigned char) can be used as "tiny"
21489 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21490 ble sign extension and increased code size.
21494 Use unsigned when it is known in advance that the value is not going to
21496 This helps especially if you are doing division or multiplication, bit-shifting
21497 or are using an array index.
21500 NEVER jump into a LOOP.
21503 Declare the variables to be local
21504 \begin_inset LatexCommand \index{local variables}
21508 whenever possible, especially loop control variables (induction).
21511 Since the compiler does not always do implicit integral promotion, the programme
21512 r should do an explicit cast when integral promotion is required.
21515 Reducing the size of division, multiplication & modulus operations can reduce
21516 code size substantially.
21517 Take the following code for example.
21523 foobar(unsigned int p1, unsigned char ch)
21531 unsigned char ch1 = p1 % ch ;
21542 For the modulus operation the variable ch will be promoted to unsigned int
21543 first then the modulus operation will be performed (this will lead to a
21544 call to support routine _moduint()), and the result will be casted to a
21546 If the code is changed to
21551 foobar(unsigned int p1, unsigned char ch)
21559 unsigned char ch1 = (unsigned char)p1 % ch ;
21570 It would substantially reduce the code generated (future versions of the
21571 compiler will be smart enough to detect such optimization opportunities).
21575 Have a look at the assembly listing to get a
21576 \begin_inset Quotes sld
21580 \begin_inset Quotes srd
21583 for the code generation.
21587 \begin_inset LatexCommand \index{Tools}
21591 included in the distribution
21595 \begin_inset Tabular
21596 <lyxtabular version="3" rows="12" columns="3">
21598 <column alignment="center" valignment="top" leftline="true" width="0pt">
21599 <column alignment="center" valignment="top" leftline="true" width="0pt">
21600 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21601 <row topline="true" bottomline="true">
21602 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21610 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21618 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21627 <row topline="true">
21628 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21636 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21641 Simulator for various architectures
21644 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21653 <row topline="true">
21654 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21662 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21667 header file conversion
21670 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21675 sdcc/support/scripts
21679 <row topline="true">
21680 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21688 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21693 header file conversion
21696 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21701 sdcc/support/scripts
21705 <row topline="true">
21706 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21714 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21722 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21740 <row topline="true">
21741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21749 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21775 <row topline="true">
21776 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21784 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21792 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21810 <row topline="true">
21811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21819 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21827 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21845 <row topline="true">
21846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21880 <row topline="true">
21881 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21889 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21897 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21915 <row topline="true">
21916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21924 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21950 <row topline="true" bottomline="true">
21951 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21959 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21967 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21995 \begin_inset LatexCommand \index{Documentation}
21999 included in the distribution
22003 \begin_inset Tabular
22004 <lyxtabular version="3" rows="10" columns="2">
22006 <column alignment="left" valignment="top" leftline="true" width="0">
22007 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22008 <row topline="true" bottomline="true">
22009 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22017 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22022 Where to get / filename
22026 <row topline="true">
22027 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22032 SDCC Compiler User Guide
22035 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22040 You're reading it right now
22044 <row topline="true">
22045 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22053 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22062 <row topline="true">
22063 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22069 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22074 \begin_inset LatexCommand \index{Assembler documentation}
22078 Assemblers and ASLINK
22079 \begin_inset LatexCommand \index{aslink}
22084 \begin_inset LatexCommand \index{Linker documentation}
22091 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22096 sdcc/as/doc/asxhtm.html
22100 <row topline="true">
22101 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22106 SDCC regression test
22107 \begin_inset LatexCommand \index{Regression test}
22114 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22119 sdcc/doc/test_suite_spec.pdf
22123 <row topline="true">
22124 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22132 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22141 <row topline="true">
22142 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22147 Notes on debugging with sdcdb
22148 \begin_inset LatexCommand \index{sdcdb (debugger)}
22155 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22160 sdcc/debugger/README
22164 <row topline="true">
22165 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22170 Software simulator for microcontrollers
22173 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22200 <row topline="true">
22201 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22206 Temporary notes on the pic16
22207 \begin_inset LatexCommand \index{PIC16}
22214 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22219 sdcc/src/pic16/NOTES
22223 <row topline="true" bottomline="true">
22224 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22229 SDCC internal documentation (debugging file format)
22232 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22268 Related open source tools
22269 \begin_inset LatexCommand \index{Related tools}
22277 \begin_inset Tabular
22278 <lyxtabular version="3" rows="11" columns="3">
22280 <column alignment="center" valignment="top" leftline="true" width="0pt">
22281 <column alignment="block" valignment="top" leftline="true" width="30line%">
22282 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22283 <row topline="true" bottomline="true">
22284 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22300 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22309 <row topline="true">
22310 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22316 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22323 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22331 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22337 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22345 <row topline="true">
22346 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22352 \begin_inset LatexCommand \index{gputils (pic tools)}
22359 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22367 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22373 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22381 <row topline="true">
22382 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22390 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22398 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22404 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22412 <row topline="true">
22413 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22419 \begin_inset LatexCommand \index{indent (source formatting tool)}
22426 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22431 Formats C source - Master of the white spaces
22434 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22440 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22448 <row topline="true">
22449 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22455 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22467 Object file conversion, checksumming, ...
22470 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22476 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22484 <row topline="true">
22485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22491 \begin_inset LatexCommand \index{objdump (tool)}
22498 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22503 Object file conversion, ...
22506 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22511 Part of binutils (should be there anyway)
22515 <row topline="true">
22516 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22522 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22529 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22534 Source code documentation system
22537 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22543 \begin_inset LatexCommand \url{http://www.doxygen.org}
22551 <row topline="true">
22552 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22560 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22565 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22568 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22574 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22582 <row topline="true">
22583 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22589 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22596 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22601 Statically checks c sources (see
22602 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22609 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22615 \begin_inset LatexCommand \url{http://www.splint.org}
22623 <row topline="true" bottomline="true">
22624 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22630 \begin_inset LatexCommand \index{ddd (debugger)}
22637 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22642 Debugger, serves nicely as GUI to sdcdb
22643 \begin_inset LatexCommand \index{sdcdb (debugger)}
22650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22656 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22673 Related documentation / recommended reading
22677 \begin_inset Tabular
22678 <lyxtabular version="3" rows="6" columns="3">
22680 <column alignment="center" valignment="top" leftline="true" width="0pt">
22681 <column alignment="block" valignment="top" leftline="true" width="30line%">
22682 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22683 <row topline="true" bottomline="true">
22684 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22692 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22700 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22709 <row topline="true">
22710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22727 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22733 \begin_inset LatexCommand \index{C Reference card}
22740 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22746 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22754 <row topline="true">
22755 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22763 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22771 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22777 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22785 <row topline="true">
22786 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22798 Latest datasheet of the target CPU
22801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22810 <row topline="true">
22811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22818 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22823 Revision history of datasheet
22826 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22835 <row topline="true" bottomline="true">
22836 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22846 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22851 Advanced Compiler Design and Implementation
22854 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22859 bookstore (very dedicated, probably read other books first)
22875 Some questions answered, some pointers given - it might be time to in turn
22883 can you solve your project with the selected microcontroller? Would you
22884 find out early or rather late that your target is too small/slow/whatever?
22885 Can you switch to a slightly better device if it doesn't fit?
22888 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
22889 and/or another programming language be more adequate? Would an operating
22890 system on the target device help?
22893 if you solved the problem, will the marketing department be happy?
22896 if the marketing department is happy, will customers be happy?
22899 if you're the project manager, marketing department and maybe even the customer
22900 in one person, have you tried to see the project from the outside?
22903 is the project done if you think it is done? Or is just that other interface/pro
22904 tocol/feature/configuration/option missing? How about website, manual(s),
22905 internationali(z|s)ation, packaging, labels, 2nd source for components,
22906 electromagnetic compatability/interference, documentation for production,
22907 production test software, update mechanism, patent issues?
22910 is your project adequately positioned in that magic triangle: fame, fortune,
22914 Maybe not all answers to these questions are known and some answers may
22919 , nevertheless knowing these questions may help you to avoid burnout
22925 burnout is bad for electronic devices, programmers and motorcycle tyres
22929 Chances are you didn't want to hear some of them...
22933 \begin_inset LatexCommand \index{Support}
22940 SDCC has grown to be a large project.
22941 The compiler alone (without the preprocessor, assembler and linker) is
22942 well over 100,000 lines of code (blank stripped).
22943 The open source nature of this project is a key to its continued growth
22945 You gain the benefit and support of many active software developers and
22947 Is SDCC perfect? No, that's why we need your help.
22948 The developers take pride in fixing reported bugs.
22949 You can help by reporting the bugs and helping other SDCC users.
22950 There are lots of ways to contribute, and we encourage you to take part
22951 in making SDCC a great software package.
22955 The SDCC project is hosted on the SDCC sourceforge site at
22956 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
22961 You'll find the complete set of mailing lists
22962 \begin_inset LatexCommand \index{Mailing list(s)}
22966 , forums, bug reporting system, patch submission
22967 \begin_inset LatexCommand \index{Patch submission}
22972 \begin_inset LatexCommand \index{download}
22976 area and Subversion code repository
22977 \begin_inset LatexCommand \index{Subversion code repository}
22985 \begin_inset LatexCommand \index{Bug reporting}
22990 \begin_inset LatexCommand \index{Reporting bugs}
22997 The recommended way of reporting bugs is using the infrastructure of the
22999 You can follow the status of bug reports there and have an overview about
23003 Bug reports are automatically forwarded to the developer mailing list and
23004 will be fixed ASAP.
23005 When reporting a bug, it is very useful to include a small test program
23006 (the smaller the better) which reproduces the problem.
23007 If you can isolate the problem by looking at the generated assembly code,
23008 this can be very helpful.
23009 Compiling your program with the -
23020 \begin_inset LatexCommand \index{-\/-dumpall}
23024 option can sometimes be useful in locating optimization problems.
23025 When reporting a bug please maker sure you:
23028 Attach the code you are compiling with SDCC.
23032 Specify the exact command you use to run SDCC, or attach your Makefile.
23036 Specify the SDCC version (type "
23042 "), your platform, and operating system.
23046 Provide an exact copy of any error message or incorrect output.
23050 Put something meaningful in the subject of your message.
23053 Please attempt to include these 5 important parts, as applicable, in all
23054 requests for support or when reporting any problems or bugs with SDCC.
23055 Though this will make your message lengthy, it will greatly improve your
23056 chance that SDCC users and developers will be able to help you.
23057 Some SDCC developers are frustrated by bug reports without code provided
23058 that they can use to reproduce and ultimately fix the problem, so please
23059 be sure to provide sample code if you are reporting a bug!
23062 Please have a short check that you are using a recent version of SDCC and
23063 the bug is not yet known.
23064 This is the link for reporting bugs:
23065 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23072 Requesting Features
23073 \begin_inset LatexCommand \label{sub:Requesting-Features}
23078 \begin_inset LatexCommand \index{Feature request}
23083 \begin_inset LatexCommand \index{Requesting features}
23090 Like bug reports feature requests are forwarded to the developer mailing
23092 This is the link for requesting features:
23093 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23103 Like bug reports contributed patches are forwarded to the developer mailing
23105 This is the link for submitting patches
23106 \begin_inset LatexCommand \index{Patch submission}
23111 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23118 You need to specify some parameters to the
23122 command for the patches to be useful.
23123 If you modified more than one file a patch created f.e.
23128 \begin_inset Quotes sld
23131 diff -Naur unmodified_directory modified_directory >my_changes.patch
23132 \begin_inset Quotes srd
23138 will be fine, otherwise
23142 \begin_inset Quotes sld
23145 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23146 \begin_inset Quotes srd
23159 These links should take you directly to the
23160 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23170 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23171 automated messages (mid 2003)
23175 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23180 \begin_inset LatexCommand \index{Mailing list(s)}
23184 and forums are archived and searchable so if you are lucky someone already
23185 had a similar problem.
23186 While mails to the lists themselves are delivered promptly their web front
23187 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23188 if you're seriously using SDCC please consider subscribing to the lists.
23194 You can follow the status of the Subversion version
23195 \begin_inset LatexCommand \index{version}
23199 of SDCC by watching the Changelog
23200 \begin_inset LatexCommand \index{Changelog}
23204 in the Subversion repository
23209 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
23217 \begin_inset LatexCommand \index{Release policy}
23224 Historically there often were long delays between official releases and
23225 the sourceforge download area tends to get not updated at all.
23226 Excuses in the past might have referred to problems with live range analysis,
23227 but as this was fixed a while ago, the current problem is that another
23228 excuse has to be found.
23229 Kidding aside, we have to get better there! On the other hand there are
23230 daily snapshots available at
23231 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23235 , and you can always build the very last version (hopefully with many bugs
23236 fixed, and features added) from the source code available at
23237 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23245 \begin_inset LatexCommand \index{Examples}
23252 You'll find some small examples in the directory
23254 sdcc/device/examples/.
23257 More examples and libraries are available at
23259 The SDCC Open Knowledge Resource
23260 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23267 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23274 I did insert a reference to Paul's web site here although it seems rather
23275 dedicated to a specific 8032 board (I think it's okay because it f.e.
23276 shows LCD/Harddisc interface and has a free 8051 monitor.
23277 Independent 8032 board vendors face hard competition of heavily subsidized
23278 development boards anyway).
23281 Maybe we should include some links to real world applications.
23282 Preferably pointer to pointers (one for each architecture) so this stays
23287 \begin_inset LatexCommand \index{Quality control}
23294 The compiler is passed through nightly compile and build checks.
23300 \begin_inset LatexCommand \index{Regression test}
23304 check that SDCC itself compiles flawlessly on several platforms and checks
23305 the quality of the code generated by SDCC by running the code through simulator
23307 There is a separate document
23310 \begin_inset LatexCommand \index{Test suite}
23319 You'll find the test code in the directory
23321 sdcc/support/regression
23324 You can run these tests manually by running
23328 in this directory (or f.e.
23333 \begin_inset Quotes sld
23337 \begin_inset Quotes srd
23343 if you don't want to run the complete tests).
23344 The test code might also be interesting if you want to look for examples
23345 \begin_inset LatexCommand \index{Examples}
23349 checking corner cases of SDCC or if you plan to submit patches
23350 \begin_inset LatexCommand \index{Patch submission}
23357 The pic port uses a different set of regression tests, you'll find them
23360 sdcc/src/regression
23365 Use of SDCC in Education
23378 the phrase "use in education" might evoke the association "
23382 fit for use in education".
23383 This connotation is not intended but nevertheless risked as the licensing
23384 of SDCC makes it difficult to offer educational discounts
23388 If your rationales are to:
23391 give students a chance to understand the
23395 steps of code generation
23398 have a curriculum that can be extended for years.
23399 Then you could use an fpga board as target and your curriculum will seamlessly
23400 extend from logic synthesis (
23401 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
23406 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
23410 ), over assembly programming, to C to FPGA compilers (
23411 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
23418 be able to insert excursions about skills like using a revision control
23419 system, submitting/applying patches, using a type-setting (as opposed to
23420 word-processing) engine LyX/LaTeX, using
23421 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
23426 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
23430 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
23431 Source Software, CPU simulation, compiler regression tests
23432 \begin_inset LatexCommand \index{Regression test}
23439 And if there should be a shortage of ideas then you can always point students
23440 to the ever-growing feature request list
23441 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23448 not tie students to a specific host platform and instead allow them to use
23453 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
23455 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
23462 not encourage students to use illegal copies of educational software
23465 be immune to licensing/availability/price changes of the chosen tool chain
23468 be able to change to a new target platform without having to adopt a new
23472 have complete control over and insight into the tool chain
23475 make your students aware about the pros and cons of open source software
23479 give back to the public as you are probably at least partially publically
23483 give students a chance to publically prove their skills and to possibly
23484 see a world wide impact
23487 then SDCC is probably among the first choices.
23488 Well, probably SDCC might be the only choice.
23491 SDCC Technical Data
23495 \begin_inset LatexCommand \index{Optimizations}
23502 SDCC performs a host of standard optimizations in addition to some MCU specific
23507 Sub-expression Elimination
23508 \begin_inset LatexCommand \index{Subexpression elimination}
23515 The compiler does local and
23541 will be translated to
23553 Some subexpressions are not as obvious as the above example, e.g.:
23563 In this case the address arithmetic a->b[i] will be computed only once;
23564 the equivalent code in C would be.
23576 The compiler will try to keep these temporary variables in registers.
23579 Dead-Code Elimination
23580 \begin_inset LatexCommand \index{Dead-code elimination}
23601 i = 1; \SpecialChar ~
23610 global = 1;\SpecialChar ~
23623 global = 3;\SpecialChar ~
23648 \begin_inset LatexCommand \index{Copy propagation}
23704 Note: the dead stores created by this copy propagation will be eliminated
23705 by dead-code elimination.
23709 \begin_inset LatexCommand \index{Loop optimization}
23714 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23721 Two types of loop optimizations are done by SDCC
23729 of loop induction variables.
23730 In addition to the strength reduction the optimizer marks the induction
23731 variables and the register allocator tries to keep the induction variables
23732 in registers for the duration of the loop.
23733 Because of this preference of the register allocator
23734 \begin_inset LatexCommand \index{Register allocation}
23738 , loop induction optimization causes an increase in register pressure, which
23739 may cause unwanted spilling of other temporary variables into the stack
23740 \begin_inset LatexCommand \index{stack}
23745 The compiler will generate a warning message when it is forced to allocate
23746 extra space either on the stack or data space.
23747 If this extra space allocation is undesirable then induction optimization
23748 can be eliminated either for the entire source file (with -
23758 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23760 \begin_inset LatexCommand \index{\#pragma noinduction}
23773 for (i = 0 ; i < 100 ; i ++)
23789 for (i = 0; i < 100; i++)
23798 As mentioned previously some loop invariants are not as apparent, all static
23799 address computations are also moved out of the loop.
23804 \begin_inset LatexCommand \index{Strength reduction}
23808 , this optimization substitutes an expression by a cheaper expression:
23813 for (i=0;i < 100; i++)
23831 for (i=0;i< 100;i++) {
23837 ar[itemp1] = itemp2;
23854 The more expensive multiplication
23855 \begin_inset LatexCommand \index{Multiplication}
23859 is changed to a less expensive addition.
23863 \begin_inset LatexCommand \index{Loop reversing}
23870 This optimization is done to reduce the overhead of checking loop boundaries
23871 for every iteration.
23872 Some simple loops can be reversed and implemented using a
23873 \begin_inset Quotes eld
23876 decrement and jump if not zero
23877 \begin_inset Quotes erd
23881 SDCC checks for the following criterion to determine if a loop is reversible
23882 (note: more sophisticated compilers use data-dependency analysis to make
23883 this determination, SDCC uses a more simple minded analysis).
23886 The 'for' loop is of the form
23892 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
23902 The <for body> does not contain
23903 \begin_inset Quotes eld
23907 \begin_inset Quotes erd
23911 \begin_inset Quotes erd
23917 All goto's are contained within the loop.
23920 No function calls within the loop.
23923 The loop control variable <sym> is not assigned any value within the loop
23926 The loop control variable does NOT participate in any arithmetic operation
23930 There are NO switch statements in the loop.
23933 Algebraic Simplifications
23936 SDCC does numerous algebraic simplifications, the following is a small sub-set
23937 of these optimizations.
23942 i = j + 0;\SpecialChar ~
23946 /* changed to: */\SpecialChar ~
23952 i /= 2;\SpecialChar ~
23959 /* changed to: */\SpecialChar ~
23965 i = j - j;\SpecialChar ~
23969 /* changed to: */\SpecialChar ~
23975 i = j / 1;\SpecialChar ~
23979 /* changed to: */\SpecialChar ~
23986 Note the subexpressions
23987 \begin_inset LatexCommand \index{Subexpression}
23991 given above are generally introduced by macro expansions or as a result
23992 of copy/constant propagation.
23995 'switch' Statements
23996 \begin_inset LatexCommand \label{sub:'switch'-Statements}
24001 \begin_inset LatexCommand \index{switch statement}
24008 SDCC can optimize switch statements to jump tables
24009 \begin_inset LatexCommand \index{jump tables}
24014 It makes the decision based on an estimate of the generated code size.
24015 SDCC is quite liberal in the requirements for jump table generation:
24018 The labels need not be in order, and the starting number need not be one
24019 or zero, the case labels are in numerical sequence or not too many case
24020 labels are missing.
24026 switch(i) {\SpecialChar ~
24057 case 4: ...\SpecialChar ~
24089 case 5: ...\SpecialChar ~
24121 case 3: ...\SpecialChar ~
24152 case 6: ...\SpecialChar ~
24184 case 7: ...\SpecialChar ~
24216 case 8: ...\SpecialChar ~
24248 case 9: ...\SpecialChar ~
24280 case 10: ...\SpecialChar ~
24311 case 11: ...\SpecialChar ~
24378 Both the above switch statements will be implemented using a jump-table.
24379 The example to the right side is slightly more efficient as the check for
24380 the lower boundary of the jump-table is not needed.
24384 The number of case labels is not larger than supported by the target architectur
24388 If the case labels are not in numerical sequence ('gaps' between cases)
24389 SDCC checks whether a jump table with additionally inserted dummy cases
24390 is still attractive.
24394 If the starting number is not zero and a check for the lower boundary of
24395 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24400 Switch statements which have large gaps in the numeric sequence or those
24401 that have too many case labels can be split into more than one switch statement
24402 for efficient code generation, e.g.:
24482 If the above switch statement is broken down into two switch statements
24572 then both the switch statements will be implemented using jump-tables whereas
24573 the unmodified switch statement will not be.
24576 There might be reasons which SDCC cannot know about to either favour or
24577 not favour jump tables.
24578 If the target system has to be as quick for the last switch case as for
24579 the first (pro jump table), or if the switch argument is known to be zero
24580 in the majority of the cases (contra jump table).
24583 The pragma nojtbound
24584 \begin_inset LatexCommand \index{\#pragma nojtbound}
24588 can be used to turn off checking the
24601 It has no effect if a default label is supplied.
24602 Use of this pragma is dangerous: if the switch
24603 \begin_inset LatexCommand \index{switch statement}
24607 argument is not matched by a case statement the processor will happily
24611 Bit-shifting Operations
24612 \begin_inset LatexCommand \index{Bit shifting}
24619 Bit shifting is one of the most frequently used operation in embedded programmin
24621 SDCC tries to implement bit-shift operations in the most efficient way
24637 generates the following code:
24654 In general SDCC will never setup a loop if the shift count is known.
24697 \begin_inset LatexCommand \index{Bit rotation}
24704 A special case of the bit-shift operation is bit rotation
24705 \begin_inset LatexCommand \index{rotating bits}
24709 , SDCC recognizes the following expression to be a left bit-rotation:
24719 char i;\SpecialChar ~
24730 /* unsigned is needed for rotation */
24735 i = ((i << 1) | (i >> 7));
24744 will generate the following code:
24763 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24764 ns of this case will also be recognized as bit-rotation, i.e.:
24769 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24772 Nibble and Byte Swapping
24775 Other special cases of the bit-shift operations are nibble or byte swapping
24776 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24780 , SDCC recognizes the following expressions:
24803 i = ((i << 4) | (i >> 4));
24809 j = ((j << 8) | (j >> 8));
24812 and generates a swap instruction for the nibble swapping
24813 \begin_inset LatexCommand \index{Nibble swapping}
24817 or move instructions for the byte swapping
24818 \begin_inset LatexCommand \index{Byte swapping}
24824 \begin_inset Quotes sld
24828 \begin_inset Quotes srd
24831 example can be used to convert from little to big-endian or vice versa.
24832 If you want to change the endianness of a
24836 integer you have to cast to
24843 Note that SDCC stores numbers in little-endian
24849 Usually 8-bit processors don't care much about endianness.
24850 This is not the case for the standard 8051 which only has an instruction
24856 \begin_inset LatexCommand \index{DPTR}
24864 so little-endian is the more efficient byte order.
24868 \begin_inset LatexCommand \index{little-endian}
24873 \begin_inset LatexCommand \index{Endianness}
24878 lowest order first).
24882 \begin_inset LatexCommand \index{Highest Order Bit}
24887 \begin_inset LatexCommand \index{Any Order Bit}
24894 It is frequently required to obtain the highest order bit of an integral
24895 type (long, int, short or char types).
24896 Also obtaining any other order bit is not uncommon.
24897 SDCC recognizes the following expressions to yield the highest order bit
24898 and generates optimized code for it, e.g.:
24911 unsigned char hob1, aob1;
24915 bit hob2, hob3, aob2, aob3;
24924 hob1 = (gint >> 15) & 1;
24928 hob2 = (gint >> 15) & 1;
24932 hob3 = gint & 0x8000;
24936 aob1 = (gint >> 9) & 1;
24940 aob2 = (gint >> 8) & 1;
24944 aob3 = gint & 0x0800;
24954 will generate the following code:
24987 000A E5*01\SpecialChar ~
25014 000C 23\SpecialChar ~
25045 000D 54 01\SpecialChar ~
25072 000F F5*02\SpecialChar ~
25127 0011 E5*01\SpecialChar ~
25154 0013 33\SpecialChar ~
25184 0014 92*00\SpecialChar ~
25239 0016 E5*01\SpecialChar ~
25266 0018 33\SpecialChar ~
25296 0019 92*01\SpecialChar ~
25351 001B E5*01\SpecialChar ~
25378 001D 03\SpecialChar ~
25409 001E 54 01\SpecialChar ~
25436 0020 F5*03\SpecialChar ~
25491 0022 E5*01\SpecialChar ~
25518 0024 13\SpecialChar ~
25548 0025 92*02\SpecialChar ~
25603 0027 E5*01\SpecialChar ~
25630 0029 A2 E3\SpecialChar ~
25657 002B 92*03\SpecialChar ~
25685 Other variations of these cases however will
25690 They are standard C expressions, so I heartily recommend these be the only
25691 way to get the highest order bit, (it is portable).
25692 Of course it will be recognized even if it is embedded in other expressions,
25698 xyz = gint + ((gint >> 15) & 1);
25701 will still be recognized.
25705 \begin_inset LatexCommand \index{Higher Order Byte}
25709 / Higher Order Word
25710 \begin_inset LatexCommand \index{Higher Order Word}
25717 It is also frequently required to obtain a higher order byte or word of
25718 a larger integral type (long, int or short types).
25719 SDCC recognizes the following expressions to yield the higher order byte
25720 or word and generates optimized code for it, e.g.:
25727 unsigned long int glong;
25735 unsigned char hob1, hob2;
25739 unsigned int how1, how2;
25748 hob1 = (gint >> 8) & 0xFF;
25752 hob2 = glong >> 24;
25756 how1 = (glong >> 16) & 0xFFFF;
25770 will generate the following code:
25803 0037 85*01*06\SpecialChar ~
25825 _foo_hob1_1_1,(_gint + 1)
25855 003A 85*05*07\SpecialChar ~
25877 _foo_hob2_1_1,(_glong + 3)
25907 003D 85*04*08\SpecialChar ~
25929 _foo_how1_1_1,(_glong + 2)
25931 0040 85*05*09\SpecialChar ~
25953 (_foo_how1_1_1 + 1),(_glong + 3)
25955 0043 85*03*0A\SpecialChar ~
25977 _foo_how2_1_1,(_glong + 1)
25979 0046 85*04*0B\SpecialChar ~
26001 (_foo_how2_1_1 + 1),(_glong + 2)
26004 Again, variations of these cases may
26009 They are standard C expressions, so I heartily recommend these be the only
26010 way to get the higher order byte/word, (it is portable).
26011 Of course it will be recognized even if it is embedded in other expressions,
26017 xyz = gint + ((gint >> 8) & 0xFF);
26020 will still be recognized.
26024 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
26029 \begin_inset LatexCommand \index{Peephole optimizer}
26036 The compiler uses a rule based, pattern matching and re-writing mechanism
26037 for peep-hole optimization.
26042 a peep-hole optimizer by Christopher W.
26043 Fraser (cwfraser\SpecialChar ~
26046 A default set of rules are compiled into the compiler, additional rules
26047 may be added with the
26060 \begin_inset LatexCommand \index{-\/-peep-file}
26067 The rule language is best illustrated with examples.
26091 The above rule will change the following assembly
26092 \begin_inset LatexCommand \index{Assembler routines}
26114 Note: All occurrences of a
26118 (pattern variable) must denote the same string.
26119 With the above rule, the assembly sequence:
26129 will remain unmodified.
26133 Other special case optimizations may be added by the user (via
26149 some variants of the 8051 MCU
26150 \begin_inset LatexCommand \index{MCS51 variants}
26163 The following two rules will change all
26182 replace { lcall %1 } by { acall %1 }
26184 replace { ljmp %1 } by { ajmp %1 }
26189 inline-assembler code
26191 is also passed through the peep hole optimizer, thus the peephole optimizer
26192 can also be used as an assembly level macro expander.
26193 The rules themselves are MCU dependent whereas the rule language infra-structur
26194 e is MCU independent.
26195 Peephole optimization rules for other MCU can be easily programmed using
26200 The syntax for a rule is as follows:
26205 rule := replace [ restart ] '{' <assembly sequence> '
26243 <assembly sequence> '
26261 '}' [if <functionName> ] '
26266 <assembly sequence> := assembly instruction (each instruction including
26267 labels must be on a separate line).
26271 The optimizer will apply to the rules one by one from the top in the sequence
26272 of their appearance, it will terminate when all rules are exhausted.
26273 If the 'restart' option is specified, then the optimizer will start matching
26274 the rules again from the top, this option for a rule is expensive (performance)
26275 , it is intended to be used in situations where a transformation will trigger
26276 the same rule again.
26277 An example of this (not a good one, it has side effects) is the following
26300 Note that the replace pattern cannot be a blank, but can be a comment line.
26301 Without the 'restart' option only the innermost 'pop' 'push' pair would
26302 be eliminated, i.e.:
26332 the restart option the rule will be applied again to the resulting code
26333 and then all the pop-push pairs will be eliminated to yield:
26343 A conditional function can be attached to a rule.
26344 Attaching rules are somewhat more involved, let me illustrate this with
26371 The optimizer does a look-up of a function name table defined in function
26376 in the source file SDCCpeeph.c, with the name
26381 If it finds a corresponding entry the function is called.
26382 Note there can be no parameters specified for these functions, in this
26387 is crucial, since the function
26391 expects to find the label in that particular variable (the hash table containin
26392 g the variable bindings is passed as a parameter).
26393 If you want to code more such functions, take a close look at the function
26394 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26395 Currently implemented are
26397 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26398 24bitMode, portIsDS390, 24bitModeAndPortDS390
26407 I know this whole thing is a little kludgey, but maybe some day we will
26408 have some better means.
26409 If you are looking at this file, you will see the default rules that are
26410 compiled into the compiler, you can add your own rules in the default set
26411 there if you get tired of specifying the -
26425 \begin_inset LatexCommand \index{ANSI-compliance}
26430 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26437 Deviations from the compliance:
26440 functions are not reentrant
26441 \begin_inset LatexCommand \index{reentrant}
26445 unless explicitly declared as such or the
26458 \begin_inset LatexCommand \index{-\/-stack-auto}
26464 command line option is specified.
26467 structures and unions cannot be assigned values directly, cannot be passed
26468 as function parameters or assigned to each other and cannot be a return
26469 value from a function, e.g.:
26495 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26506 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26528 return rets;/* is invalid in SDCC although allowed in ANSI */
26534 initialization of structure arrays must be fully braced.
26540 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26542 struct s { char x } a[] = {{1}, {2}}; /* OK */
26547 \begin_inset LatexCommand \index{long long (not supported)}
26552 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26560 \begin_inset LatexCommand \index{double (not supported)}
26564 ' precision floating point
26565 \begin_inset LatexCommand \index{Floating point support}
26573 \begin_inset LatexCommand \index{K\&R style}
26577 function declarations are NOT allowed.
26583 foo(i,j) /* this old style of function declarations */
26585 int i,j; /* are valid in ANSI but not valid in SDCC */
26600 Most enhancements in C99 are not supported, f.e.:
26609 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
26616 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
26620 Certain words that are valid identifiers in the standard may be reserved
26621 words in SDCC unless the
26634 \begin_inset LatexCommand \index{-\/-std-c89}
26649 \begin_inset LatexCommand \index{-\/-std-c99}
26655 command line options are used.
26656 These may include (depending on the selected processor): 'at', 'banked',
26657 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26658 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26659 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26661 Compliant equivalents of these keywords are always available in a form
26662 that begin with two underscores
26663 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26668 '__data' instead of 'data'.
26671 Cyclomatic Complexity
26672 \begin_inset LatexCommand \index{Cyclomatic complexity}
26679 Cyclomatic complexity of a function is defined as the number of independent
26680 paths the program can take during execution of the function.
26681 This is an important number since it defines the number test cases you
26682 have to generate to validate the function.
26683 The accepted industry standard for complexity number is 10, if the cyclomatic
26684 complexity reported by SDCC exceeds 10 you should think about simplification
26685 of the function logic.
26686 Note that the complexity level is not related to the number of lines of
26687 code in a function.
26688 Large functions can have low complexity, and small functions can have large
26694 SDCC uses the following formula to compute the complexity:
26699 complexity = (number of edges in control flow graph) - (number of nodes
26700 in control flow graph) + 2;
26704 Having said that the industry standard is 10, you should be aware that in
26705 some cases it be may unavoidable to have a complexity level of less than
26707 For example if you have switch statement with more than 10 case labels,
26708 each case label adds one to the complexity level.
26709 The complexity level is by no means an absolute measure of the algorithmic
26710 complexity of the function, it does however provide a good starting point
26711 for which functions you might look at for further optimization.
26714 Retargetting for other Processors
26717 The issues for retargetting the compiler are far too numerous to be covered
26719 What follows is a brief description of each of the seven phases of the
26720 compiler and its MCU dependency.
26723 Parsing the source and building the annotated parse tree.
26724 This phase is largely MCU independent (except for the language extensions).
26725 Syntax & semantic checks are also done in this phase, along with some initial
26726 optimizations like back patching labels and the pattern matching optimizations
26727 like bit-rotation etc.
26730 The second phase involves generating an intermediate code which can be easy
26731 manipulated during the later phases.
26732 This phase is entirely MCU independent.
26733 The intermediate code generation assumes the target machine has unlimited
26734 number of registers, and designates them with the name iTemp.
26735 The compiler can be made to dump a human readable form of the code generated
26749 This phase does the bulk of the standard optimizations and is also MCU independe
26751 This phase can be broken down into several sub-phases:
26755 Break down intermediate code (iCode) into basic blocks.
26757 Do control flow & data flow analysis on the basic blocks.
26759 Do local common subexpression elimination, then global subexpression elimination
26761 Dead code elimination
26765 If loop optimizations caused any changes then do 'global subexpression eliminati
26766 on' and 'dead code elimination' again.
26769 This phase determines the live-ranges; by live range I mean those iTemp
26770 variables defined by the compiler that still survive after all the optimization
26772 Live range analysis
26773 \begin_inset LatexCommand \index{Live range analysis}
26777 is essential for register allocation, since these computation determines
26778 which of these iTemps will be assigned to registers, and for how long.
26781 Phase five is register allocation.
26782 There are two parts to this process.
26786 The first part I call 'register packing' (for lack of a better term).
26787 In this case several MCU specific expression folding is done to reduce
26792 The second part is more MCU independent and deals with allocating registers
26793 to the remaining live ranges.
26794 A lot of MCU specific code does creep into this phase because of the limited
26795 number of index registers available in the 8051.
26798 The Code generation phase is (unhappily), entirely MCU dependent and very
26799 little (if any at all) of this code can be reused for other MCU.
26800 However the scheme for allocating a homogenized assembler operand for each
26801 iCode operand may be reused.
26804 As mentioned in the optimization section the peep-hole optimizer is rule
26805 based system, which can reprogrammed for other MCUs.
26809 \begin_inset LatexCommand \index{Compiler internals}
26816 The anatomy of the compiler
26817 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26826 This is an excerpt from an article published in Circuit Cellar Magazine
26832 It's a little outdated (the compiler is much more efficient now and user/develo
26833 per friendly), but pretty well exposes the guts of it all.
26839 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26840 It is fairly easy to retarget for other 8-bit MCU.
26841 Here we take a look at some of the internals of the compiler.
26846 \begin_inset LatexCommand \index{Parsing}
26853 Parsing the input source file and creating an AST (Annotated Syntax Tree
26854 \begin_inset LatexCommand \index{Annotated syntax tree}
26859 This phase also involves propagating types (annotating each node of the
26860 parse tree with type information) and semantic analysis.
26861 There are some MCU specific parsing rules.
26862 For example the storage classes, the extended storage classes are MCU specific
26863 while there may be a xdata storage class for 8051 there is no such storage
26864 class for z80 or Atmel AVR.
26865 SDCC allows MCU specific storage class extensions, i.e.
26866 xdata will be treated as a storage class specifier when parsing 8051 C
26867 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
26872 \begin_inset LatexCommand \index{iCode}
26879 Intermediate code generation.
26880 In this phase the AST is broken down into three-operand form (iCode).
26881 These three operand forms are represented as doubly linked lists.
26882 ICode is the term given to the intermediate form generated by the compiler.
26883 ICode example section shows some examples of iCode generated for some simple
26884 C source functions.
26888 \begin_inset LatexCommand \index{Optimizations}
26895 Bulk of the target independent optimizations is performed in this phase.
26896 The optimizations include constant propagation, common sub-expression eliminati
26897 on, loop invariant code movement, strength reduction of loop induction variables
26898 and dead-code elimination.
26901 Live range analysis
26902 \begin_inset LatexCommand \index{Live range analysis}
26909 During intermediate code generation phase, the compiler assumes the target
26910 machine has infinite number of registers and generates a lot of temporary
26912 The live range computation determines the lifetime of each of these compiler-ge
26913 nerated temporaries.
26914 A picture speaks a thousand words.
26915 ICode example sections show the live range annotations for each of the
26917 It is important to note here, each iCode is assigned a number in the order
26918 of its execution in the function.
26919 The live ranges are computed in terms of these numbers.
26920 The from number is the number of the iCode which first defines the operand
26921 and the to number signifies the iCode which uses this operand last.
26924 Register Allocation
26925 \begin_inset LatexCommand \index{Register allocation}
26932 The register allocation determines the type and number of registers needed
26934 In most MCUs only a few registers can be used for indirect addressing.
26935 In case of 8051 for example the registers R0 & R1 can be used to indirectly
26936 address the internal ram and DPTR to indirectly address the external ram.
26937 The compiler will try to allocate the appropriate register to pointer variables
26939 ICode example section shows the operands annotated with the registers assigned
26941 The compiler will try to keep operands in registers as much as possible;
26942 there are several schemes the compiler uses to do achieve this.
26943 When the compiler runs out of registers the compiler will check to see
26944 if there are any live operands which is not used or defined in the current
26945 basic block being processed, if there are any found then it will push that
26946 operand and use the registers in this block, the operand will then be popped
26947 at the end of the basic block.
26951 There are other MCU specific considerations in this phase.
26952 Some MCUs have an accumulator; very short-lived operands could be assigned
26953 to the accumulator instead of a general-purpose register.
26959 Figure II gives a table of iCode operations supported by the compiler.
26960 The code generation involves translating these operations into corresponding
26961 assembly code for the processor.
26962 This sounds overly simple but that is the essence of code generation.
26963 Some of the iCode operations are generated on a MCU specific manner for
26964 example, the z80 port does not use registers to pass parameters so the
26965 SEND and RECV iCode operations will not be generated, and it also does
26966 not support JUMPTABLES.
26973 <Where is Figure II?>
26976 In the original article Figure II was announced to be downloadable on
26981 Unfortunately it never seemed to have shown up there, so: where is Figure
26986 \begin_inset LatexCommand \index{iCode}
26993 This section shows some details of iCode.
26994 The example C code does not do anything useful; it is used as an example
26995 to illustrate the intermediate code generated by the compiler.
27007 /* This function does nothing useful.
27014 for the purpose of explaining iCode */
27017 short function (data int *x)
27025 short i=10; \SpecialChar ~
27027 /* dead initialization eliminated */
27032 short sum=10; /* dead initialization eliminated */
27045 while (*x) *x++ = *p++;
27059 /* compiler detects i,j to be induction variables */
27063 for (i = 0, j = 10 ; i < 10 ; i++, j
27089 mul += i * 3; \SpecialChar ~
27091 /* this multiplication remains */
27097 gint += j * 3;\SpecialChar ~
27099 /* this multiplication changed to addition */
27113 In addition to the operands each iCode contains information about the filename
27114 and line it corresponds to in the source file.
27115 The first field in the listing should be interpreted as follows:
27120 Filename(linenumber: iCode Execution sequence number : ICode hash table
27121 key : loop depth of the iCode).
27126 Then follows the human readable form of the ICode operation.
27127 Each operand of this triplet form can be of three basic types a) compiler
27128 generated temporary b) user defined variable c) a constant value.
27129 Note that local variables and parameters are replaced by compiler generated
27132 \begin_inset LatexCommand \index{Live range analysis}
27136 are computed only for temporaries (i.e.
27137 live ranges are not computed for global variables).
27139 \begin_inset LatexCommand \index{Register allocation}
27143 are allocated for temporaries only.
27144 Operands are formatted in the following manner:
27149 Operand Name [lr live-from : live-to ] { type information } [ registers
27155 As mentioned earlier the live ranges are computed in terms of the execution
27156 sequence number of the iCodes, for example
27158 the iTemp0 is live from (i.e.
27159 first defined in iCode with execution sequence number 3, and is last used
27160 in the iCode with sequence number 5).
27161 For induction variables such as iTemp21 the live range computation extends
27162 the lifetime from the start to the end of the loop.
27164 The register allocator used the live range information to allocate registers,
27165 the same registers may be used for different temporaries if their live
27166 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27167 iTemp17 since their live ranges do not overlap.
27168 In addition the allocator also takes into consideration the type and usage
27169 of a temporary, for example itemp6 is a pointer to near space and is used
27170 as to fetch data from (i.e.
27171 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27172 Some short lived temporaries are allocated to special registers which have
27173 meaning to the code generator e.g.
27174 iTemp13 is allocated to a pseudo register CC which tells the back end that
27175 the temporary is used only for a conditional jump the code generation makes
27176 use of this information to optimize a compare and jump ICode.
27178 There are several loop optimizations
27179 \begin_inset LatexCommand \index{Loop optimization}
27183 performed by the compiler.
27184 It can detect induction variables iTemp21(i) and iTemp23(j).
27185 Also note the compiler does selective strength reduction
27186 \begin_inset LatexCommand \index{Strength reduction}
27191 the multiplication of an induction variable in line 18 (gint = j * 3) is
27192 changed to addition, a new temporary iTemp17 is allocated and assigned
27193 a initial value, a constant 3 is then added for each iteration of the loop.
27194 The compiler does not change the multiplication
27195 \begin_inset LatexCommand \index{Multiplication}
27199 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27201 Note the dead code elimination
27202 \begin_inset LatexCommand \index{Dead-code elimination}
27206 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27214 Sample.c (5:1:0:0) _entry($9) :
27219 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27224 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27229 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27234 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27240 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27245 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27251 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27256 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27262 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27268 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27274 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27280 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27281 * int}[r0] + 0x2 {short}
27286 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27291 Sample.c(11:17:21:0)_whilebreak_0($3) :
27296 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27301 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27306 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27311 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27316 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27321 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27326 Sample.c(15:24:26:1)_forcond_0($4) :
27331 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27337 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27342 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27343 + ITemp21 [lr21:38]{short}[r4]
27348 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27354 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27355 + iTemp15 [lr29:30]{short}[r1]
27360 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27366 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27372 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27378 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27384 Sample.c(19:38:47:1) goto _forcond_0($4)
27389 Sample.c(19:39:48:0)_forbreak_0($7) :
27394 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27395 + ITemp11 [lr19:40]{short}[r3]
27400 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27405 Sample.c(20:42:51:0)_return($8) :
27410 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27416 Finally the code generated for this function:
27457 ; ----------------------------------------------
27462 ; function function
27467 ; ----------------------------------------------
27477 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27489 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27501 ;_whilecontinue_0($1) :
27511 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27516 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27575 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27594 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27641 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27681 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27707 ; iTemp6 [lr5:16]{_near * int}[r0] =
27712 ; iTemp6 [lr5:16]{_near * int}[r0] +
27729 ; goto _whilecontinue_0($1)
27741 ; _whilebreak_0($3) :
27751 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27763 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27775 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27787 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27806 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27835 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27840 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27885 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
27890 ; iTemp21 [lr21:38]{short}[r4]
27916 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
27949 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
27954 ; iTemp15 [lr29:30]{short}[r1]
27973 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
28020 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
28067 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
28079 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
28093 cjne r5,#0xff,00104$
28105 ; goto _forcond_0($4)
28117 ; _forbreak_0($7) :
28127 ; ret iTemp24 [lr40:41]{short}
28170 A few words about basic block successors, predecessors and dominators
28173 Successors are basic blocks
28174 \begin_inset LatexCommand \index{Basic blocks}
28178 that might execute after this basic block.
28180 Predecessors are basic blocks that might execute before reaching this basic
28183 Dominators are basic blocks that WILL execute before reaching this basic
28217 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28220 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28223 c) domVect of [BB4] = BB1 ...
28224 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28232 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28242 Thanks to all the other volunteer developers who have helped with coding,
28243 testing, web-page creation, distribution sets, etc.
28244 You know who you are :-)
28251 This document was initially written by Sandeep Dutta
28254 All product names mentioned herein may be trademarks
28255 \begin_inset LatexCommand \index{Trademarks}
28259 of their respective companies.
28266 To avoid confusion, the installation and building options for SDCC itself
28267 (chapter 2) are not part of the index.
28271 \begin_inset LatexCommand \printindex{}