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 Disables automatically building device libraries
845 \labelwidthstring 00.00.0000
857 -disable-packihx Disables building packihx
859 \labelwidthstring 00.00.0000
871 -enable-doc Build pdf, html and txt files from the lyx sources
873 \labelwidthstring 00.00.0000
885 -enable-libgc Use the Bohem memory allocator.
886 Lower runtime footprint.
889 Furthermore the environment variables CC, CFLAGS, ...
890 the tools and their arguments can be influenced.
891 Please see `configure -
901 -help` and the man/info pages of `configure` for details.
905 The names of the standard libraries STD_LIB, STD_INT_LIB, STD_LONG_LIB,
906 STD_FP_LIB, STD_DS390_LIB, STD_XA51_LIB and the environment variables SDCC_DIR_
907 NAME, SDCC_INCLUDE_NAME, SDCC_LIB_NAME are defined by `configure` too.
908 At the moment it's not possible to change the default settings (it was
909 simply never required).
913 These configure options are compiled into the binaries, and can only be
914 changed by rerunning 'configure' and recompiling SDCC.
915 The configure options are written in
919 to distinguish them from run time environment variables (see section search
925 \begin_inset Quotes sld
929 \begin_inset Quotes srd
932 are used by the SDCC team to build the official Win32 binaries.
933 The SDCC team uses Mingw32 to build the official Windows binaries, because
940 a gcc compiler and last but not least
943 the binaries can be built by cross compiling on Sourceforge's compile farm.
946 See the examples, how to pass the Win32 settings to 'configure'.
947 The other Win32 builds using Borland, VC or whatever don't use 'configure',
948 but a header file sdcc_vc_in.h is the same as sdccconf.h built by 'configure'
960 <lyxtabular version="3" rows="8" columns="3">
962 <column alignment="block" valignment="top" leftline="true" width="0in">
963 <column alignment="block" valignment="top" leftline="true" width="0in">
964 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
965 <row topline="true" bottomline="true">
966 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
974 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
982 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
992 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1002 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1021 <row topline="true">
1022 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1032 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1042 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1053 <row topline="true">
1054 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1064 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1076 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1091 <row topline="true">
1092 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1102 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1114 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1125 <row topline="true">
1126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1136 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1148 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1163 <row topline="true">
1164 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1182 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1191 <row topline="true" bottomline="true">
1192 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1202 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1210 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1228 'configure' also computes relative paths.
1229 This is needed for full relocatability of a binary package and to complete
1230 search paths (see section search paths below):
1236 \begin_inset Tabular
1237 <lyxtabular version="3" rows="4" columns="3">
1239 <column alignment="block" valignment="top" leftline="true" width="0in">
1240 <column alignment="block" valignment="top" leftline="true" width="0in">
1241 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
1242 <row topline="true" bottomline="true">
1243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1259 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1268 <row topline="true" bottomline="true">
1269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1287 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1296 <row bottomline="true">
1297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1307 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1315 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1324 <row bottomline="true">
1325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1335 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1376 \begin_inset Quotes srd
1380 \begin_inset Quotes srd
1394 \begin_inset Quotes srd
1398 \begin_inset Quotes srd
1426 To cross compile on linux for Mingw32 (see also 'sdcc/support/scripts/sdcc_mingw
1435 \begin_inset Quotes srd
1438 i586-mingw32msvc-gcc
1439 \begin_inset Quotes srd
1443 \begin_inset Quotes srd
1446 i586-mingw32msvc-g++
1447 \begin_inset Quotes srd
1455 \begin_inset Quotes srd
1458 i586-mingw32msvc-ranlib
1459 \begin_inset Quotes srd
1467 \begin_inset Quotes srd
1470 i586-mingw32msvc-strip
1471 \begin_inset Quotes srd
1489 \begin_inset Quotes srd
1493 \begin_inset Quotes srd
1511 \begin_inset Quotes srd
1515 \begin_inset Quotes srd
1523 \begin_inset Quotes srd
1527 \begin_inset Quotes srd
1535 \begin_inset Quotes srd
1539 \begin_inset Quotes srd
1547 \begin_inset Quotes srd
1551 \begin_inset Quotes srd
1558 sdccconf_h_dir_separator=
1559 \begin_inset Quotes srd
1571 \begin_inset Quotes srd
1616 -host=i586-mingw32msvc -
1626 -build=unknown-unknown-linux-gnu
1630 \begin_inset Quotes sld
1634 \begin_inset Quotes srd
1637 compile on Cygwin for Mingw32 (see also sdcc/support/scripts/sdcc_cygwin_mingw32
1646 \begin_inset Quotes srd
1650 \begin_inset Quotes srd
1658 \begin_inset Quotes srd
1662 \begin_inset Quotes srd
1680 \begin_inset Quotes srd
1684 \begin_inset Quotes srd
1702 \begin_inset Quotes srd
1706 \begin_inset Quotes srd
1714 \begin_inset Quotes srd
1718 \begin_inset Quotes srd
1726 \begin_inset Quotes srd
1730 \begin_inset Quotes srd
1738 \begin_inset Quotes srd
1742 \begin_inset Quotes srd
1749 sdccconf_h_dir_separator=
1750 \begin_inset Quotes srd
1762 \begin_inset Quotes srd
1782 'configure' is quite slow on Cygwin (at least on windows before Win2000/XP).
1793 -C' turns on caching, which gives a little bit extra speed.
1794 However if options are changed, it can be necessary to delete the config.cache
1799 \begin_inset LatexCommand \label{sub:Install-paths}
1804 \begin_inset LatexCommand \index{Install paths}
1810 \added_space_top medskip \align center
1812 \begin_inset Tabular
1813 <lyxtabular version="3" rows="5" columns="4">
1815 <column alignment="center" valignment="top" leftline="true" width="0">
1816 <column alignment="center" valignment="top" leftline="true" width="0">
1817 <column alignment="center" valignment="top" leftline="true" width="0">
1818 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
1819 <row topline="true" bottomline="true">
1820 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1850 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1861 <row topline="true">
1862 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1870 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1880 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1888 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1901 <row topline="true">
1902 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1910 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1917 $DATADIR/ $INCLUDE_DIR_SUFFIX
1920 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1925 /usr/local/share/sdcc/include
1928 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1941 <row topline="true">
1942 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1957 $DATADIR/$LIB_DIR_SUFFIX
1960 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1965 /usr/local/share/sdcc/lib
1968 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
1981 <row topline="true" bottomline="true">
1982 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
1990 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2000 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2005 /usr/local/share/sdcc/doc
2008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2030 *compiler, preprocessor, assembler, and linker
2036 is auto-appended by the compiler, e.g.
2037 small, large, z80, ds390 etc
2040 The install paths can still be changed during `make install` with e.g.:
2043 make install prefix=$(HOME)/local/sdcc
2046 Of course this doesn't change the search paths compiled into the binaries.
2050 Moreover the install path can be changed by defining DESTDIR
2051 \begin_inset LatexCommand \index{DESTDIR}
2058 make install DESTDIR=$(HOME)/sdcc.rpm/
2061 Please note that DESTDIR must have a trailing slash!
2065 \begin_inset LatexCommand \label{sub:Search-Paths}
2070 \begin_inset LatexCommand \index{Search path}
2077 Some search paths or parts of them are determined by configure variables
2082 , see section above).
2083 Further search paths are determined by environment variables during runtime.
2086 The paths searched when running the compiler are as follows (the first catch
2092 Binary files (preprocessor, assembler and linker)
2098 \begin_inset Tabular
2099 <lyxtabular version="3" rows="4" columns="3">
2101 <column alignment="block" valignment="top" leftline="true" width="0in">
2102 <column alignment="block" valignment="top" leftline="true" width="0in">
2103 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2104 <row topline="true" bottomline="true">
2105 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2113 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2121 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2130 <row topline="true">
2131 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2141 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2149 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2160 <row topline="true">
2161 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2166 Path of argv[0] (if available)
2169 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2177 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2186 <row topline="true" bottomline="true">
2187 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2228 \begin_inset Tabular
2229 <lyxtabular version="3" rows="6" columns="3">
2231 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2232 <column alignment="block" valignment="top" leftline="true" width="1.5in">
2233 <column alignment="block" valignment="top" leftline="true" rightline="true" width="0in">
2234 <row topline="true" bottomline="true">
2235 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2243 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2251 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2260 <row topline="true">
2261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2279 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2316 <row topline="true">
2317 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2325 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2333 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2342 <row topline="true">
2343 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2357 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2369 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2380 <row topline="true">
2381 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2399 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2449 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2462 <row topline="true" bottomline="true">
2463 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2479 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2484 /usr/local/share/sdcc/
2489 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2517 -nostdinc disables the last two search paths.
2527 With the exception of
2528 \begin_inset Quotes sld
2542 \begin_inset Quotes srd
2549 is auto-appended by the compiler (e.g.
2550 small, large, z80, ds390 etc.).
2557 \begin_inset Tabular
2558 <lyxtabular version="3" rows="6" columns="3">
2560 <column alignment="block" valignment="top" leftline="true" width="1.7in">
2561 <column alignment="block" valignment="top" leftline="true" width="1.2in">
2562 <column alignment="block" valignment="top" leftline="true" rightline="true" width="1.2in">
2563 <row topline="true" bottomline="true">
2564 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2572 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2580 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2589 <row topline="true">
2590 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2608 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2626 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2645 <row topline="true">
2646 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2658 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2670 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2685 <row topline="true">
2686 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2697 $LIB_DIR_SUFFIX/<model>
2700 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2714 <cell alignment="left" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2731 <row topline="true">
2732 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2747 $LIB_DIR_SUFFIX/<model>
2750 <cell alignment="left" valignment="top" topline="true" leftline="true" usebox="none">
2803 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2859 <row topline="true" bottomline="true">
2860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2869 $LIB_DIR_SUFFIX/<model>
2872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
2877 /usr/local/share/sdcc/
2884 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
2902 Don't delete any of the stray spaces in the table above without checking
2903 the HTML output (last line)!
2919 -nostdlib disables the last two search paths.
2923 \begin_inset LatexCommand \index{Building SDCC}
2930 Building SDCC on Linux
2931 \begin_inset LatexCommand \label{sub:Building-SDCC-on-Linux}
2940 Download the source package
2942 either from the SDCC Subversion repository or from the nightly snapshots
2944 , it will be named something like sdcc
2955 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net/snap.php}
2964 Bring up a command line terminal, such as xterm.
2969 Unpack the file using a command like:
2972 "tar -xvzf sdcc.src.tar.gz
2977 , this will create a sub-directory called sdcc with all of the sources.
2980 Change directory into the main SDCC directory, for example type:
2997 This configures the package for compilation on your system.
3013 All of the source packages will compile, this can take a while.
3029 This copies the binary executables, the include files, the libraries and
3030 the documentation to the install directories.
3031 Proceed with section
3032 \begin_inset LatexCommand \ref{sec:Testing-the-SDCC}
3039 Building SDCC on OSX 2.x
3042 Follow the instruction for Linux.
3046 On OSX 2.x it was reported, that the default gcc (version 3.1 20020420 (prerelease
3047 )) fails to compile SDCC.
3048 Fortunately there's also gcc 2.9.x installed, which works fine.
3049 This compiler can be selected by running 'configure' with:
3052 ./configure CC=gcc2 CXX=g++2
3055 Cross compiling SDCC on Linux for Windows
3058 With the Mingw32 gcc cross compiler it's easy to compile SDCC for Win32.
3059 See section 'Configure Options'.
3062 Building SDCC on Windows
3065 With the exception of Cygwin the SDCC binaries uCsim and sdcdb can't be
3067 They use Unix-sockets, which are not available on Win32.
3070 Building SDCC using Cygwin and Mingw32
3073 For building and installing a Cygwin executable follow the instructions
3079 \begin_inset Quotes sld
3083 \begin_inset Quotes srd
3086 Win32-binary can be built, which will not need the Cygwin-DLL.
3087 For the necessary 'configure' options see section 'configure options' or
3088 the script 'sdcc/support/scripts/sdcc_cygwin_mingw32'.
3092 In order to install Cygwin on Windows download setup.exe from
3093 \begin_inset LatexCommand \url[www.cygwin.com]{http://www.cygwin.com/}
3099 \begin_inset Quotes sld
3102 default text file type
3103 \begin_inset Quotes srd
3107 \begin_inset Quotes sld
3111 \begin_inset Quotes srd
3114 and download/install at least the following packages.
3115 Some packages are selected by default, others will be automatically selected
3116 because of dependencies with the manually selected packages.
3117 Never deselect these packages!
3126 gcc ; version 3.x is fine, no need to use the old 2.9x
3129 binutils ; selected with gcc
3135 rxvt ; a nice console, which makes life much easier under windoze (see below)
3138 man ; not really needed for building SDCC, but you'll miss it sooner or
3142 less ; not really needed for building SDCC, but you'll miss it sooner or
3146 svn ; only if you use Subversion access
3149 If you want to develop something you'll need:
3152 python ; for the regression tests
3155 gdb ; the gnu debugger, together with the nice GUI
3156 \begin_inset Quotes sld
3160 \begin_inset Quotes srd
3166 openssh ; to access the CF or commit changes
3169 autoconf and autoconf-devel ; if you want to fight with 'configure', don't
3170 use autoconf-stable!
3173 rxvt is a nice console with history.
3174 Replace in your cygwin.bat the line
3193 rxvt -sl 1000 -fn "Lucida Console-12" -sr -cr red
3196 -bg black -fg white -geometry 100x65 -e bash -
3209 Text selected with the mouse is automatically copied to the clipboard, pasting
3210 works with shift-insert.
3214 The other good tip is to make sure you have no //c/-style paths anywhere,
3215 use /cygdrive/c/ instead.
3216 Using // invokes a network lookup which is very slow.
3218 \begin_inset Quotes sld
3222 \begin_inset Quotes srd
3225 is too long, you can change it with e.g.
3231 SDCC sources use the unix line ending LF.
3232 Life is much easier, if you store the source tree on a drive which is mounted
3234 And use an editor which can handle LF-only line endings.
3235 Make sure not to commit files with windows line endings.
3236 The tabulator spacing
3237 \begin_inset LatexCommand \index{tabulator spacing (8 columns)}
3241 used in the project is 8.
3242 Although a tabulator spacing of 8 is a sensible choice for programmers
3243 (it's a power of 2 and allows to display 8/16 bit signed variables without
3244 loosing columns) the plan is to move towards using only spaces in the source.
3247 Building SDCC Using Microsoft Visual C++ 6.0/NET (MSVC)
3252 Download the source package
3254 either from the SDCC Subversion repository or from the
3255 \begin_inset LatexCommand \url[nightly snapshots]{http://sdcc.sourceforge.net/snap.php}
3261 , it will be named something like sdcc
3268 SDCC is distributed with all the projects, workspaces, and files you need
3269 to build it using Visual C++ 6.0/NET (except for sdcdb.exe which currently
3270 doesn't build under MSVC).
3271 The workspace name is 'sdcc.dsw'.
3272 Please note that as it is now, all the executables are created in a folder
3276 Once built you need to copy the executables from sdcc
3280 bin before running SDCC.
3285 WARNING: Visual studio is very picky with line terminations; it expects
3286 the 0x0d, 0x0a DOS style line endings, not the 0x0a Unix style line endings.
3287 When using the Subversion repository it's easiest to configure the svn
3288 client to convert automatically for you.
3289 If however you are getting a message such as "This makefile was not generated
3290 by Developer Studio etc.
3292 \begin_inset Quotes srd
3295 when opening the sdcc.dsw workspace or any of the *.dsp projects, then you
3296 need to convert the Unix style line endings to DOS style line endings.
3297 To do so you can use the
3298 \begin_inset Quotes sld
3302 \begin_inset Quotes srd
3305 utility freely available on the internet.
3306 Doug Hawkins reported in the sdcc-user list that this works:
3314 SDCC> unix2dos sdcc.dsw
3320 SDCC> for /R %I in (*.dsp) do @unix2dos "%I"
3324 In order to build SDCC with MSVC you need win32 executables of bison.exe,
3325 flex.exe, and gawk.exe.
3326 One good place to get them is
3327 \begin_inset LatexCommand \url[here]{http://unxutils.sourceforge.net}
3335 Download the file UnxUtils
3336 \begin_inset LatexCommand \index{UnxUtils}
3341 Now you have to install the utilities and setup MSVC so it can locate the
3343 Here there are two alternatives (choose one!):
3350 a) Extract UnxUtils.zip to your C:
3352 hard disk PRESERVING the original paths, otherwise bison won't work.
3353 (If you are using WinZip make certain that 'Use folder names' is selected)
3357 b) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3358 in 'Show directories for:' select 'Executable files', and in the directories
3359 window add a new path: 'C:
3369 (As a side effect, you get a bunch of Unix utilities that could be useful,
3370 such as diff and patch.)
3377 This one avoids extracting a bunch of files you may not use, but requires
3382 a) Create a directory were to put the tools needed, or use a directory already
3390 b) Extract 'bison.exe', 'bison.hairy', 'bison.simple', 'flex.exe', and gawk.exe
3391 to such directory WITHOUT preserving the original paths.
3392 (If you are using WinZip make certain that 'Use folder names' is not selected)
3396 c) Rename bison.exe to '_bison.exe'.
3400 d) Create a batch file 'bison.bat' in 'C:
3404 ' and add these lines:
3424 _bison %1 %2 %3 %4 %5 %6 %7 %8 %9
3428 Steps 'c' and 'd' are needed because bison requires by default that the
3429 files 'bison.simple' and 'bison.hairy' reside in some weird Unix directory,
3430 '/usr/local/share/' I think.
3431 So it is necessary to tell bison where those files are located if they
3432 are not in such directory.
3433 That is the function of the environment variables BISON_SIMPLE and BISON_HAIRY.
3437 e) In the Visual C++ IDE click Tools, Options, select the Directory tab,
3438 in 'Show directories for:' select 'Executable files', and in the directories
3439 window add a new path: 'c:
3442 Note that you can use any other path instead of 'c:
3444 util', even the path where the Visual C++ tools are, probably: 'C:
3448 Microsoft Visual Studio
3453 So you don't have to execute step 'e' :)
3457 Open 'sdcc.dsw' in Visual Studio, click 'build all', when it finishes copy
3458 the executables from sdcc
3462 bin, and you can compile using SDCC.
3465 Building SDCC Using Borland
3468 From the sdcc directory, run the command "make -f Makefile.bcc".
3469 This should regenerate all the .exe files in the bin directory except for
3470 sdcdb.exe (which currently doesn't build under Borland C++).
3473 If you modify any source files and need to rebuild, be aware that the dependenci
3474 es may not be correctly calculated.
3475 The safest option is to delete all .obj files and run the build again.
3476 From a Cygwin BASH prompt, this can easily be done with the command (be
3477 sure you are in the sdcc directory):
3487 ( -name '*.obj' -o -name '*.lib' -o -name '*.rul'
3489 ) -print -exec rm {}
3498 or on Windows NT/2000/XP from the command prompt with the command:
3505 del /s *.obj *.lib *.rul
3508 from the sdcc directory.
3511 Windows Install Using a ZIP Package
3514 Download the binary zip package from
3515 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3519 and unpack it using your favorite unpacking tool (gunzip, WinZip, etc).
3520 This should unpack to a group of sub-directories.
3521 An example directory structure after unpacking the mingw32 package is:
3526 bin for the executables, c:
3534 lib for the include and libraries.
3537 Adjust your environment variable PATH to include the location of the bin
3538 directory or start sdcc using the full path.
3541 Windows Install Using the Setup Program
3542 \begin_inset LatexCommand \label{sub:Windows-Install}
3549 Download the setup program
3551 sdcc-x.y.z-setup.exe
3553 for an official release from
3556 \begin_inset LatexCommand \url{http://sf.net/project/showfiles.php?group_id=599}
3560 or a setup program for one of the snapshots
3562 sdcc_yyyymmdd_setup.exe
3565 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3570 A windows typical installer will guide you through the installation process.
3574 \begin_inset LatexCommand \index{VPATH}
3581 SDCC supports the VPATH feature provided by configure and make.
3582 It allows to separate the source and build trees.
3614 tar -xzf sdcc.src.tar.gz\SpecialChar ~
3615 # extract source to directory sdcc
3620 mkdir sdcc.build\SpecialChar ~
3629 # put output in sdcc.build
3639 ../sdcc/configure\SpecialChar ~
3647 # configure is doing all the magic!
3659 will create the directory tree will all the necessary Makefiles in ~/sdcc.build.
3660 It automagically computes the variables srcdir, top_srcdir and top_buildir
3666 the generated files will be in ~/sdcc.build, while the source files stay
3669 This is not only usefull for building different binaries, e.g.
3670 when cross compiling.
3671 It also gives you a much better overview in the source tree when all the
3672 generated files are not scattered between the source files.
3673 And the best thing is: if you want to change a file you can leave the original
3674 file untouched in the source directory.
3675 Simply copy it to the build directory, edit it, enter `make clean`, `rm
3676 Makefile.dep` and `make`.
3681 will do the rest for you!
3684 Building the Documentation
3697 -enable-doc to the configure arguments to build the documentation together
3698 with all the other stuff.
3699 You will need several tools (LyX, LaTeX, LaTeX2HTML, pdflatex, dvipdf,
3700 dvips and makeindex) to get the job done.
3701 Another possibility is to change to the doc directory and to type
3705 \begin_inset Quotes srd
3709 \begin_inset Quotes srd
3716 You're invited to make changes and additions to this manual (sdcc/doc/sdccman.ly
3719 \begin_inset LatexCommand \url{http://www.lyx.org}
3723 as editor is straightforward.
3724 Prebuilt documentation in html and pdf format is available from
3725 \begin_inset LatexCommand \url{http://sdcc.sf.net/snap.php}
3732 Reading the Documentation
3735 Currently reading the document in pdf format is recommended, as for unknown
3736 reason the hyperlinks are working there whereas in the html version they
3743 If you should know why please drop us a note
3749 You'll find the pdf version
3750 \begin_inset LatexCommand \index{PDF version of this document}
3755 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.pdf}
3763 \begin_inset LatexCommand \index{HTML version of this document}
3768 \begin_inset LatexCommand \url{http://sdcc.sf.net/doc/sdccman.html/index.html}
3774 This documentation is in some aspects different from a commercial documentation:
3778 It tries to document SDCC for several processor architectures in one document
3779 (commercially these probably would be separate documents/products).
3781 \begin_inset LatexCommand \index{Status of documentation}
3785 currently matches SDCC for mcs51 and DS390 best and does give too few informati
3787 Z80, PIC14, PIC16 and HC08.
3790 There are many references pointing away from this documentation.
3791 Don't let this distract you.
3793 was a reference like
3794 \begin_inset LatexCommand \url{http://www.opencores.org}
3798 together with a statement
3799 \begin_inset Quotes sld
3802 some processors which are targetted by SDCC can be implemented in a
3819 \begin_inset LatexCommand \index{FPGA (field programmable gate array)}
3824 \begin_inset Quotes srd
3828 \begin_inset LatexCommand \url{http://sf.net/projects/fpgac}
3833 \begin_inset LatexCommand \index{FpgaC ((subset of) C to FPGA compiler)}
3838 \begin_inset Quotes sld
3841 have you ever heard of an open source compiler that compiles a subset of
3843 \begin_inset Quotes srd
3846 we expect you to have a quick look there and come back.
3847 If you read this you are on the right track.
3850 Some sections attribute more space to problems, restrictions and warnings
3851 than to the solution.
3854 The installation section and the section about the debugger is intimidating.
3857 There are still lots of typos and there are more different writing styles
3861 Testing the SDCC Compiler
3862 \begin_inset LatexCommand \label{sec:Testing-the-SDCC}
3869 The first thing you should do after installing your SDCC compiler is to
3885 \begin_inset LatexCommand \index{version}
3892 at the prompt, and the program should run and tell you the version.
3893 If it doesn't run, or gives a message about not finding sdcc program, then
3894 you need to check over your installation.
3895 Make sure that the sdcc bin directory is in your executable search path
3896 defined by the PATH environment setting (
3901 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
3908 Install trouble-shooting for suggestions
3911 Make sure that the sdcc program is in the bin folder, if not perhaps something
3912 did not install correctly.
3920 is commonly installed as described in section
3921 \begin_inset Quotes sld
3924 Install and search paths
3925 \begin_inset Quotes srd
3934 Make sure the compiler works on a very simple example.
3935 Type in the following test.c program using your favorite
3961 Compile this using the following command:
3970 If all goes well, the compiler will generate a test.asm and test.rel file.
3971 Congratulations, you've just compiled your first program with SDCC.
3972 We used the -c option to tell SDCC not to link the generated code, just
3973 to keep things simple for this step.
3981 The next step is to try it with the linker.
3991 If all goes well the compiler will link with the libraries and produce
3992 a test.ihx output file.
3997 (no test.ihx, and the linker generates warnings), then the problem is most
4006 usr/local/share/sdcc/lib directory
4013 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4020 Install trouble-shooting for suggestions).
4028 The final test is to ensure
4036 header files and libraries.
4037 Edit test.c and change it to the following:
4054 strcpy(str1, "testing");
4061 Compile this by typing
4068 This should generate a test.ihx output file, and it should give no warnings
4069 such as not finding the string.h file.
4070 If it cannot find the string.h file, then the problem is that
4074 cannot find the /usr/local/share/sdcc/include directory
4081 \begin_inset LatexCommand \ref{sub:Install-Trouble-shooting}
4088 Install trouble-shooting section for suggestions).
4106 \begin_inset LatexCommand \index{-\/-print-search-dirs}
4110 to find exactly where SDCC is looking for the include and lib files.
4113 Install Trouble-shooting
4114 \begin_inset LatexCommand \label{sub:Install-Trouble-shooting}
4119 \begin_inset LatexCommand \index{Install trouble-shooting}
4126 If SDCC does not build correctly
4129 A thing to try is starting from scratch by unpacking the .tgz source package
4130 again in an empty directory.
4138 ./configure 2>&1 | tee configure.log
4152 make 2>&1 | tee make.log
4159 If anything goes wrong, you can review the log files to locate the problem.
4160 Or a relevant part of this can be attached to an email that could be helpful
4161 when requesting help from the mailing list.
4165 \begin_inset Quotes sld
4169 \begin_inset Quotes srd
4176 \begin_inset Quotes sld
4180 \begin_inset Quotes srd
4183 command is a script that analyzes your system and performs some configuration
4184 to ensure the source package compiles on your system.
4185 It will take a few minutes to run, and will compile a few tests to determine
4186 what compiler features are installed.
4190 \begin_inset Quotes sld
4194 \begin_inset Quotes srd
4200 This runs the GNU make tool, which automatically compiles all the source
4201 packages into the final installed binary executables.
4205 \begin_inset Quotes sld
4209 \begin_inset Quotes erd
4215 This will install the compiler, other executables libraries and include
4216 files into the appropriate directories.
4218 \begin_inset LatexCommand \ref{sub:Install-paths}
4224 \begin_inset LatexCommand \ref{sub:Search-Paths}
4229 about install and search paths.
4231 On most systems you will need super-user privileges to do this.
4237 SDCC is not just a compiler, but a collection of tools by various developers.
4238 These include linkers, assemblers, simulators and other components.
4239 Here is a summary of some of the components.
4240 Note that the included simulator and assembler have separate documentation
4241 which you can find in the source package in their respective directories.
4242 As SDCC grows to include support for other processors, other packages from
4243 various developers are included and may have their own sets of documentation.
4247 You might want to look at the files which are installed in <installdir>.
4248 At the time of this writing, we find the following programs for gcc-builds:
4252 In <installdir>/bin:
4255 sdcc - The compiler.
4258 sdcpp - The C preprocessor.
4261 asx8051 - The assembler for 8051 type processors.
4268 as-gbz80 - The Z80 and GameBoy Z80 assemblers.
4271 aslink -The linker for 8051 type processors.
4278 link-gbz80 - The Z80 and GameBoy Z80 linkers.
4281 s51 - The ucSim 8051 simulator.
4282 Not available on Win32 platforms.
4285 sdcdb - The source debugger.
4286 Not available on Win32 platforms.
4289 packihx - A tool to pack (compress) Intel hex files.
4292 In <installdir>/share/sdcc/include
4298 In <installdir>/share/sdcc/lib
4301 the subdirs src and small, large, z80, gbz80 and ds390 with the precompiled
4305 In <installdir>/share/sdcc/doc
4311 As development for other processors proceeds, this list will expand to include
4312 executables to support processors like AVR, PIC, etc.
4318 This is the actual compiler, it in turn uses the c-preprocessor and invokes
4319 the assembler and linkage editor.
4322 sdcpp - The C-Preprocessor
4326 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
4330 is a modified version of the GNU preprocessor.
4331 The C preprocessor is used to pull in #include sources, process #ifdef
4332 statements, #defines and so on.
4343 - The Assemblers and Linkage Editors
4346 This is retargettable assembler & linkage editor, it was developed by Alan
4348 John Hartman created the version for 8051, and I (Sandeep) have made some
4349 enhancements and bug fixes for it to work properly with SDCC.
4356 \begin_inset LatexCommand \index{s51}
4360 is a freeware, opensource simulator developed by Daniel Drotos.
4361 The simulator is built as part of the build process.
4362 For more information visit Daniel's web site at:
4363 \begin_inset LatexCommand \url{http://mazsola.iit.uni-miskolc.hu/~drdani/embedded/s51}
4368 It currently supports the core mcs51, the Dallas DS80C390 and the Phillips
4370 S51 is currently not available on Win32 platfors.
4373 sdcdb - Source Level Debugger
4377 \begin_inset LatexCommand \index{sdcdb (debugger)}
4381 is the companion source level debugger.
4382 More about sdcdb in section
4383 \begin_inset LatexCommand \ref{cha:Debugging-with-SDCDB}
4388 The current version of the debugger uses Daniel's Simulator S51
4389 \begin_inset LatexCommand \index{s51}
4393 , but can be easily changed to use other simulators.
4394 Sdcdb is currently not available on Win32 platfors.
4403 Single Source File Projects
4406 For single source file 8051 projects the process is very simple.
4407 Compile your programs with the following command
4410 "sdcc sourcefile.c".
4414 This will compile, assemble and link your source file.
4415 Output files are as follows:
4419 \begin_inset LatexCommand \index{<file>.asm}
4424 \begin_inset LatexCommand \index{Assembler source}
4428 file created by the compiler
4432 \begin_inset LatexCommand \index{<file>.lst}
4437 \begin_inset LatexCommand \index{Assembler listing}
4441 file created by the Assembler
4445 \begin_inset LatexCommand \index{<file>.rst}
4450 \begin_inset LatexCommand \index{Assembler listing}
4454 file updated with linkedit information, created by linkage editor
4458 \begin_inset LatexCommand \index{<file>.sym}
4463 \begin_inset LatexCommand \index{Symbol listing}
4467 for the sourcefile, created by the assembler
4471 \begin_inset LatexCommand \index{<file>.rel}
4476 \begin_inset LatexCommand \index{<file>.o}
4481 \begin_inset LatexCommand \index{Object file}
4485 created by the assembler, input to Linkage editor
4489 \begin_inset LatexCommand \index{<file>.map}
4494 \begin_inset LatexCommand \index{Memory map}
4498 for the load module, created by the Linker
4502 \begin_inset LatexCommand \index{<file>.mem}
4506 - A file with a summary of the memory usage
4510 \begin_inset LatexCommand \index{<file>.ihx}
4514 - The load module in Intel hex format
4515 \begin_inset LatexCommand \index{Intel hex format}
4519 (you can select the Motorola S19 format
4520 \begin_inset LatexCommand \index{Motorola S19 format}
4535 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
4540 If you need another format you might want to use
4547 \begin_inset LatexCommand \index{objdump (tool)}
4558 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4563 Both formats are documented in the documentation of srecord
4564 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
4572 \begin_inset LatexCommand \index{<file>.adb}
4576 - An intermediate file containing debug information needed to create the
4588 \begin_inset LatexCommand \index{-\/-debug}
4596 \begin_inset LatexCommand \index{<file>.cdb}
4600 - An optional file (with -
4610 -debug) containing debug information.
4611 The format is documented in cdbfileformat.pdf
4616 \begin_inset LatexCommand \index{<file> (no extension)}
4620 An optional AOMF or AOMF51
4621 \begin_inset LatexCommand \index{AOMF, AOMF51}
4625 file containing debug information (generated with option -
4652 ormat is commonly used by third party tools (debuggers
4653 \begin_inset LatexCommand \index{Debugger}
4657 , simulators, emulators)
4661 \begin_inset LatexCommand \index{<file>.dump*}
4665 - Dump file to debug the compiler it self (generated with option -
4675 -dumpall) (see section
4676 \begin_inset LatexCommand \ref{sub:Intermediate-Dump-Options}
4682 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
4688 \begin_inset Quotes sld
4691 Anatomy of the compiler
4692 \begin_inset Quotes srd
4698 Projects with Multiple Source Files
4701 SDCC can compile only ONE file at a time.
4702 Let us for example assume that you have a project containing the following
4707 foo1.c (contains some functions)
4709 foo2.c (contains some more functions)
4711 foomain.c (contains more functions and the function main)
4719 The first two files will need to be compiled separately with the commands:
4751 Then compile the source file containing the
4756 \begin_inset LatexCommand \index{Linker}
4760 the files together with the following command:
4768 foomain.c\SpecialChar ~
4769 foo1.rel\SpecialChar ~
4774 \begin_inset LatexCommand \index{<file>.rel}
4786 can be separately compiled as well:
4797 sdcc foomain.rel foo1.rel foo2.rel
4804 The file containing the
4819 file specified in the command line, since the linkage editor processes
4820 file in the order they are presented to it.
4821 The linker is invoked from SDCC using a script file with extension .lnk
4822 \begin_inset LatexCommand \index{<file>.lnk}
4827 You can view this file to troubleshoot linking problems such as those arising
4828 from missing libraries.
4831 Projects with Additional Libraries
4832 \begin_inset LatexCommand \index{Libraries}
4839 Some reusable routines may be compiled into a library, see the documentation
4840 for the assembler and linkage editor (which are in <installdir>/share/sdcc/doc)
4844 \begin_inset LatexCommand \index{<file>.lib}
4851 Libraries created in this manner can be included in the command line.
4852 Make sure you include the -L <library-path> option to tell the linker where
4853 to look for these files if they are not in the current directory.
4854 Here is an example, assuming you have the source file
4866 (if that is not the same as your current project):
4873 sdcc foomain.c foolib.lib -L mylib
4884 must be an absolute path name.
4888 The most efficient way to use libraries is to keep separate modules in separate
4890 The lib file now should name all the modules.rel
4891 \begin_inset LatexCommand \index{<file>.rel}
4896 For an example see the standard library file
4900 in the directory <installdir>/share/lib/small.
4903 Using sdcclib to Create and Manage Libraries
4904 \begin_inset LatexCommand \index{sdcclib}
4911 Alternatively, instead of having a .rel file for each entry on the library
4912 file as described in the preceding section, sdcclib can be used to embed
4913 all the modules belonging to such library in the library file itself.
4914 This results in a larger library file, but it greatly reduces the number
4915 of disk files accessed by the linker.
4916 Additionally, the packed library file contains an index of all include
4917 modules and symbols that significantly speeds up the linking process.
4918 To display a list of options supported by sdcclib type:
4927 \begin_inset LatexCommand \index{sdcclib}
4938 To create a new library file, start by compiling all the required modules.
4976 This will create files _divsint.rel, _divuint.rel, _modsint.rel, _moduint.rel,
4978 The next step is to add the .rel files to the library file:
4986 sdcclib libint.lib _divsint.rel
4989 \begin_inset LatexCommand \index{sdcclib}
4999 sdcclib libint.lib _divuint.rel
5005 sdcclib libint.lib _modsint.rel
5011 sdcclib libint.lib _moduint.rel
5017 sdcclib libint.lib _mulint.rel
5024 If the file already exists in the library, it will be replaced.
5025 To see what modules and symbols are included in the library, options -s
5026 and -m are available.
5034 sdcclib -s libint.lib
5037 \begin_inset LatexCommand \index{sdcclib}
5147 If the source files are compiled using -
5158 \begin_inset LatexCommand \index{-\/-debug}
5162 , the corresponding debug information file .adb will be include in the library
5164 The library files created with sdcclib are plain text files, so they can
5165 be viewed with a text editor.
5166 It is not recomended to modify a library file created with sdcclib using
5167 a text editor, as there are file indexes numbers located accross the file
5168 used by the linker to quickly locate the required module to link.
5169 Once a .rel file (as well as a .adb file) is added to a library using sdcclib,
5170 it can be safely deleted, since all the information required for linking
5171 is embedded in the library file itself.
5172 Library files created using sdcclib are used as described in the preceding
5176 Command Line Options
5177 \begin_inset LatexCommand \index{Command Line Options}
5184 Processor Selection Options
5185 \begin_inset LatexCommand \index{Options processor selection}
5190 \begin_inset LatexCommand \index{Processor selection options}
5196 \labelwidthstring 00.00.0000
5201 \begin_inset LatexCommand \index{-mmcs51}
5207 Generate code for the Intel MCS51
5208 \begin_inset LatexCommand \index{MCS51}
5212 family of processors.
5213 This is the default processor target.
5215 \labelwidthstring 00.00.0000
5220 \begin_inset LatexCommand \index{-mds390}
5226 Generate code for the Dallas DS80C390
5227 \begin_inset LatexCommand \index{DS80C390}
5233 \labelwidthstring 00.00.0000
5238 \begin_inset LatexCommand \index{-mds400}
5244 Generate code for the Dallas DS80C400
5245 \begin_inset LatexCommand \index{DS80C400}
5251 \labelwidthstring 00.00.0000
5256 \begin_inset LatexCommand \index{-mhc08}
5262 Generate code for the Freescale/Motorola HC08
5263 \begin_inset LatexCommand \index{HC08}
5267 family of processors.
5269 \labelwidthstring 00.00.0000
5274 \begin_inset LatexCommand \index{-mz80}
5280 Generate code for the Zilog Z80
5281 \begin_inset LatexCommand \index{Z80}
5285 family of processors.
5287 \labelwidthstring 00.00.0000
5292 \begin_inset LatexCommand \index{-mgbz80}
5298 Generate code for the GameBoy Z80
5299 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
5303 processor (Not actively maintained).
5305 \labelwidthstring 00.00.0000
5310 \begin_inset LatexCommand \index{-mavr}
5316 Generate code for the Atmel AVR
5317 \begin_inset LatexCommand \index{AVR}
5321 processor (In development, not complete).
5322 AVR users should probably have a look at winavr
5323 \begin_inset LatexCommand \url{http://sourceforge.net/projects/winavr}
5328 \begin_inset LatexCommand \url{http://www.avrfreaks.net/index.php?name=PNphpBB2&file=index}
5335 I think it is fair to direct users there for now.
5336 Open source is also about avoiding unnecessary work .
5337 But I didn't find the 'official' link.
5339 \labelwidthstring 00.00.0000
5344 \begin_inset LatexCommand \index{-mpic14}
5350 Generate code for the Microchip PIC 14
5351 \begin_inset LatexCommand \index{PIC14}
5355 -bit processors (p16f84 and variants.
5356 In development, not complete).
5359 p16f627 p16f628 p16f84 p16f873 p16f877?
5361 \labelwidthstring 00.00.0000
5366 \begin_inset LatexCommand \index{-mpic16}
5372 Generate code for the Microchip PIC 16
5373 \begin_inset LatexCommand \index{PIC16}
5377 -bit processors (p18f452 and variants.
5378 In development, not complete).
5380 \labelwidthstring 00.00.0000
5386 Generate code for the Toshiba TLCS-900H
5387 \begin_inset LatexCommand \index{TLCS-900H}
5391 processor (Not maintained, not complete).
5393 \labelwidthstring 00.00.0000
5398 \begin_inset LatexCommand \index{-mxa51}
5404 Generate code for the Phillips XA51
5405 \begin_inset LatexCommand \index{XA51}
5409 processor (Not maintained, not complete).
5412 Preprocessor Options
5413 \begin_inset LatexCommand \index{Options preprocessor}
5418 \begin_inset LatexCommand \index{Preprocessor options}
5423 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5429 \labelwidthstring 00.00.0000
5434 \begin_inset LatexCommand \index{-I<path>}
5440 The additional location where the pre processor will look for <..h> or
5441 \begin_inset Quotes eld
5445 \begin_inset Quotes erd
5450 \labelwidthstring 00.00.0000
5455 \begin_inset LatexCommand \index{-D<macro[=value]>}
5461 Command line definition of macros.
5462 Passed to the preprocessor.
5464 \labelwidthstring 00.00.0000
5469 \begin_inset LatexCommand \index{-M}
5475 Tell the preprocessor to output a rule suitable for make describing the
5476 dependencies of each object file.
5477 For each source file, the preprocessor outputs one make-rule whose target
5478 is the object file name for that source file and whose dependencies are
5479 all the files `#include'd in it.
5480 This rule may be a single line or may be continued with `
5482 '-newline if it is long.
5483 The list of rules is printed on standard output instead of the preprocessed
5486 \begin_inset LatexCommand \index{-E}
5492 \labelwidthstring 00.00.0000
5497 \begin_inset LatexCommand \index{-C}
5503 Tell the preprocessor not to discard comments.
5504 Used with the `-E' option.
5506 \labelwidthstring 00.00.0000
5511 \begin_inset LatexCommand \index{-MM}
5522 Like `-M' but the output mentions only the user header files included with
5524 \begin_inset Quotes eld
5528 System header files included with `#include <file>' are omitted.
5530 \labelwidthstring 00.00.0000
5535 \begin_inset LatexCommand \index{-Aquestion(answer)}
5541 Assert the answer answer for question, in case it is tested with a preprocessor
5542 conditional such as `#if #question(answer)'.
5543 `-A-' disables the standard assertions that normally describe the target
5546 \labelwidthstring 00.00.0000
5551 \begin_inset LatexCommand \index{-Umacro}
5557 Undefine macro macro.
5558 `-U' options are evaluated after all `-D' options, but before any `-include'
5559 and `-imacros' options.
5561 \labelwidthstring 00.00.0000
5566 \begin_inset LatexCommand \index{-dM}
5572 Tell the preprocessor to output only a list of the macro definitions that
5573 are in effect at the end of preprocessing.
5574 Used with the `-E' option.
5576 \labelwidthstring 00.00.0000
5581 \begin_inset LatexCommand \index{-dD}
5587 Tell the preprocessor to pass all macro definitions into the output, in
5588 their proper sequence in the rest of the output.
5590 \labelwidthstring 00.00.0000
5595 \begin_inset LatexCommand \index{-dN}
5606 Like `-dD' except that the macro arguments and contents are omitted.
5607 Only `#define name' is included in the output.
5609 \labelwidthstring 00.00.0000
5614 preprocessorOption[,preprocessorOption]
5617 \begin_inset LatexCommand \index{-Wp preprocessorOption[,preprocessorOption]}
5622 Pass the preprocessorOption to the preprocessor
5627 \begin_inset LatexCommand \index{sdcpp (preprocessor)}
5632 SDCC uses an adapted version of the preprocessor cpp of the GNU Compiler
5633 Collection (gcc), if you need more dedicated options please refer to the
5635 \begin_inset LatexCommand \htmlurl{http://www.gnu.org/software/gcc/onlinedocs/}
5643 \begin_inset LatexCommand \index{Options linker}
5648 \begin_inset LatexCommand \index{Linker options}
5654 \labelwidthstring 00.00.0000
5674 \begin_inset LatexCommand \index{-\/-lib-path <path>}
5679 \begin_inset LatexCommand \index{-L -\/-lib-path}
5686 <absolute path to additional libraries> This option is passed to the linkage
5687 editor's additional libraries
5688 \begin_inset LatexCommand \index{Libraries}
5693 The path name must be absolute.
5694 Additional library files may be specified in the command line.
5695 See section Compiling programs for more details.
5697 \labelwidthstring 00.00.0000
5714 \begin_inset LatexCommand \index{-\/-xram-loc <Value>}
5719 <Value> The start location of the external ram
5720 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
5724 , default value is 0.
5725 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5735 -xram-loc 0x8000 or -
5747 \labelwidthstring 00.00.0000
5764 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
5769 <Value> The start location of the code
5770 \begin_inset LatexCommand \index{code}
5774 segment, default value 0.
5775 Note when this option is used the interrupt vector table is also relocated
5776 to the given address.
5777 The value entered can be in Hexadecimal or Decimal format, e.g.: -
5787 -code-loc 0x8000 or -
5799 \labelwidthstring 00.00.0000
5816 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
5821 <Value> By default the stack
5822 \begin_inset LatexCommand \index{stack}
5826 is placed after the data segment.
5827 Using this option the stack can be placed anywhere in the internal memory
5829 The value entered can be in Hexadecimal or Decimal format, e.g.
5840 -stack-loc 0x20 or -
5851 Since the sp register is incremented before a push or call, the initial
5852 sp will be set to one byte prior the provided value.
5853 The provided value should not overlap any other memory areas such as used
5854 register banks or the data segment and with enough space for the current
5872 \begin_inset LatexCommand \index{-\/-pack-iram}
5876 option (which is now a default setting) will override this setting, so
5877 you should also specify the
5893 \begin_inset LatexCommand \index{-\/-no-pack-iram}
5897 option if you need to manually place the stack.
5899 \labelwidthstring 00.00.0000
5916 \begin_inset LatexCommand \index{-\/-xstack-loc <Value>}
5921 <Value> By default the external stack
5922 \begin_inset LatexCommand \index{xstack}
5926 is placed after the pdata segment.
5927 Using this option the xstack can be placed anywhere in the external memory
5929 The value entered can be in Hexadecimal or Decimal format, e.g.
5940 -xstack-loc 0x8000 or -
5951 The provided value should not overlap any other memory areas such as the
5952 pdata or xdata segment and with enough space for the current application.
5954 \labelwidthstring 00.00.0000
5971 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
5976 <Value> The start location of the internal ram data
5977 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
5982 The value entered can be in Hexadecimal or Decimal format, eg.
6004 (By default, the start location of the internal ram data segment is set
6005 as low as possible in memory, taking into account the used register banks
6006 and the bit segment at address 0x20.
6007 For example if register banks 0 and 1 are used without bit variables, the
6008 data segment will be set, if -
6018 -data-loc is not used, to location 0x10.)
6020 \labelwidthstring 00.00.0000
6037 \begin_inset LatexCommand \index{-\/-idata-loc <Value>}
6042 <Value> The start location of the indirectly addressable internal ram
6043 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
6047 of the 8051, default value is 0x80.
6048 The value entered can be in Hexadecimal or Decimal format, eg.
6059 -idata-loc 0x88 or -
6071 \labelwidthstring 00.00.0000
6088 <Value> The start location of the bit
6089 \begin_inset LatexCommand \index{bit}
6093 addressable internal ram of the 8051.
6099 Instead an option can be passed directly to the linker: -Wl\SpecialChar ~
6102 \labelwidthstring 00.00.0000
6117 \begin_inset LatexCommand \index{-\/-out-fmt-ihx}
6126 The linker output (final object code) is in Intel Hex format.
6127 \begin_inset LatexCommand \index{Intel hex format}
6131 This is the default option.
6132 The format itself is documented in the documentation of srecord
6133 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
6139 \labelwidthstring 00.00.0000
6154 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6163 The linker output (final object code) is in Motorola S19 format
6164 \begin_inset LatexCommand \index{Motorola S19 format}
6169 The format itself is documented in the documentation of srecord.
6171 \labelwidthstring 00.00.0000
6186 \begin_inset LatexCommand \index{-\/-out-fmt-s19}
6195 The linker output (final object code) is in ELF format
6196 \begin_inset LatexCommand \index{ELF format}
6201 (Currently only supported for the HC08 processors)
6203 \labelwidthstring 00.00.0000
6208 linkOption[,linkOption]
6211 \begin_inset LatexCommand \index{-Wl linkOption[,linkOption]}
6216 Pass the linkOption to the linker.
6217 See file sdcc/as/doc/asxhtm.html for more on linker options.
6221 \begin_inset LatexCommand \index{Options MCS51}
6226 \begin_inset LatexCommand \index{MCS51 options}
6232 \labelwidthstring 00.00.0000
6247 \begin_inset LatexCommand \index{-\/-model-small}
6258 Generate code for Small Model programs, see section Memory Models for more
6260 This is the default model.
6262 \labelwidthstring 00.00.0000
6277 \begin_inset LatexCommand \index{-\/-model-medium}
6283 Generate code for Medium model programs, see section Memory Models for
6285 If this option is used all source files in the project have to be compiled
6287 It must also be used when invoking the linker.
6289 \labelwidthstring 00.00.0000
6304 \begin_inset LatexCommand \index{-\/-model-large}
6310 Generate code for Large model programs, see section Memory Models for more
6312 If this option is used all source files in the project have to be compiled
6314 It must also be used when invoking the linker.
6316 \labelwidthstring 00.00.0000
6331 \begin_inset LatexCommand \index{-\/-xstack}
6337 Uses a pseudo stack in the first 256 bytes in the external ram for allocating
6338 variables and passing parameters.
6340 \begin_inset LatexCommand \ref{sub:External-Stack}
6345 External Stack for more details.
6347 \labelwidthstring 00.00.0000
6365 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
6369 Causes the linker to check if the internal ram usage is within limits of
6372 \labelwidthstring 00.00.0000
6390 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
6394 Causes the linker to check if the external ram usage is within limits of
6397 \labelwidthstring 00.00.0000
6415 \begin_inset LatexCommand \index{-\/-code-size <Value>}
6419 Causes the linker to check if the code memory usage is within limits of
6422 \labelwidthstring 00.00.0000
6440 \begin_inset LatexCommand \index{-\/-stack-size <Value>}
6444 Causes the linker to check if there is at minimum <Value> bytes for stack.
6446 \labelwidthstring 00.00.0000
6464 \begin_inset LatexCommand \index{-\/-pack-iram}
6468 Causes the linker to use unused register banks for data variables and pack
6469 data, idata and stack together.
6470 This is the default now.
6472 \labelwidthstring 00.00.0000
6490 \begin_inset LatexCommand \index{-\/-no-pack-iram}
6494 Causes the linker to use old style for allocating memory areas.
6497 DS390 / DS400 Options
6498 \begin_inset LatexCommand \index{Options DS390}
6503 \begin_inset LatexCommand \index{DS390 options}
6509 \labelwidthstring 00.00.0000
6526 \begin_inset LatexCommand \index{-\/-model-flat24}
6536 Generate 24-bit flat mode code.
6537 This is the one and only that the ds390 code generator supports right now
6538 and is default when using
6543 See section Memory Models for more details.
6545 \labelwidthstring 00.00.0000
6560 \begin_inset LatexCommand \index{-\/-protect-sp-update}
6566 disable interrupts during ESP:SP updates.
6568 \labelwidthstring 00.00.0000
6585 \begin_inset LatexCommand \index{-\/-stack-10bit}
6589 Generate code for the 10 bit stack mode of the Dallas DS80C390 part.
6590 This is the one and only that the ds390 code generator supports right now
6591 and is default when using
6596 In this mode, the stack is located in the lower 1K of the internal RAM,
6597 which is mapped to 0x400000.
6598 Note that the support is incomplete, since it still uses a single byte
6599 as the stack pointer.
6600 This means that only the lower 256 bytes of the potential 1K stack space
6601 will actually be used.
6602 However, this does allow you to reclaim the precious 256 bytes of low RAM
6603 for use for the DATA and IDATA segments.
6604 The compiler will not generate any code to put the processor into 10 bit
6606 It is important to ensure that the processor is in this mode before calling
6607 any re-entrant functions compiled with this option.
6608 In principle, this should work with the
6621 \begin_inset LatexCommand \index{-\/-stack-auto}
6627 option, but that has not been tested.
6628 It is incompatible with the
6641 \begin_inset LatexCommand \index{-\/-xstack}
6648 It also only makes sense if the processor is in 24 bit contiguous addressing
6661 -model-flat24 option
6665 \labelwidthstring 00.00.0000
6680 \begin_inset LatexCommand \index{-\/-stack-probe}
6686 insert call to function __stack_probe at each function prologue.
6688 \labelwidthstring 00.00.0000
6703 \begin_inset LatexCommand \index{-\/-tini-libid}
6709 <nnnn> LibraryID used in -mTININative.
6712 \labelwidthstring 00.00.0000
6727 \begin_inset LatexCommand \index{-\/-use-accelerator}
6733 generate code for DS390 Arithmetic Accelerator.
6738 \begin_inset LatexCommand \index{Options Z80}
6743 \begin_inset LatexCommand \index{Z80 options}
6749 \labelwidthstring 00.00.0000
6766 \begin_inset LatexCommand \index{-\/-callee-saves-bc}
6776 Force a called function to always save BC.
6778 \labelwidthstring 00.00.0000
6795 \begin_inset LatexCommand \index{-\/-no-std-crt0}
6799 When linking, skip the standard crt0.o object file.
6800 You must provide your own crt0.o for your system when linking.
6804 Optimization Options
6805 \begin_inset LatexCommand \index{Options optimization}
6810 \begin_inset LatexCommand \index{Optimization options}
6816 \labelwidthstring 00.00.0000
6831 \begin_inset LatexCommand \index{-\/-nogcse}
6837 Will not do global subexpression elimination, this option may be used when
6838 the compiler creates undesirably large stack/data spaces to store compiler
6848 \begin_inset LatexCommand \index{sloc (spill location)}
6853 A warning message will be generated when this happens and the compiler
6854 will indicate the number of extra bytes it allocated.
6855 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6857 \begin_inset LatexCommand \index{\#pragma nogcse}
6861 can be used to turn off global subexpression elimination
6862 \begin_inset LatexCommand \index{Subexpression elimination}
6866 for a given function only.
6868 \labelwidthstring 00.00.0000
6883 \begin_inset LatexCommand \index{-\/-noinvariant}
6889 Will not do loop invariant optimizations, this may be turned off for reasons
6890 explained for the previous option.
6891 For more details of loop optimizations performed see Loop Invariants in
6893 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
6898 It is recommended that this option NOT be used, #pragma\SpecialChar ~
6900 \begin_inset LatexCommand \index{\#pragma noinvariant}
6904 can be used to turn off invariant optimizations for a given function only.
6906 \labelwidthstring 00.00.0000
6921 \begin_inset LatexCommand \index{-\/-noinduction}
6927 Will not do loop induction optimizations, see section strength reduction
6929 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6931 \begin_inset LatexCommand \index{\#pragma noinduction}
6935 can be used to turn off induction optimizations for a given function only.
6937 \labelwidthstring 00.00.0000
6952 \begin_inset LatexCommand \index{-\/-nojtbound}
6963 Will not generate boundary condition check when switch statements
6964 \begin_inset LatexCommand \index{switch statement}
6968 are implemented using jump-tables.
6970 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
6975 Switch Statements for more details.
6976 It is recommended that this option is NOT used, #pragma\SpecialChar ~
6978 \begin_inset LatexCommand \index{\#pragma nojtbound}
6982 can be used to turn off boundary checking for jump tables for a given function
6985 \labelwidthstring 00.00.0000
7000 \begin_inset LatexCommand \index{-\/-noloopreverse}
7009 Will not do loop reversal
7010 \begin_inset LatexCommand \index{Loop reversing}
7016 \labelwidthstring 00.00.0000
7033 \begin_inset LatexCommand \index{-\/-nolabelopt }
7037 Will not optimize labels (makes the dumpfiles more readable).
7039 \labelwidthstring 00.00.0000
7054 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
7060 Will not memcpy initialized data from code space into xdata space.
7061 This saves a few bytes in code space if you don't have initialized data
7062 \begin_inset LatexCommand \index{Variable initialization}
7068 \labelwidthstring 00.00.0000
7083 \begin_inset LatexCommand \index{-\/-nooverlay}
7089 The compiler will not overlay parameters and local variables of any function,
7090 see section Parameters and local variables for more details.
7092 \labelwidthstring 00.00.0000
7107 \begin_inset LatexCommand \index{-\/-no-peep}
7113 Disable peep-hole optimization with built-in rules.
7115 \labelwidthstring 00.00.0000
7132 \begin_inset LatexCommand \index{-\/-peep-file}
7137 <filename> This option can be used to use additional rules to be used by
7138 the peep hole optimizer.
7140 \begin_inset LatexCommand \ref{sub:Peephole-Optimizer}
7145 Peep Hole optimizations for details on how to write these rules.
7147 \labelwidthstring 00.00.0000
7162 \begin_inset LatexCommand \index{-\/-peep-asm}
7168 Pass the inline assembler code through the peep hole optimizer.
7169 This can cause unexpected changes to inline assembler code, please go through
7170 the peephole optimizer
7171 \begin_inset LatexCommand \index{Peephole optimizer}
7175 rules defined in the source file tree '<target>/peeph.def' before using
7178 \labelwidthstring 00.00.0000
7193 \begin_inset LatexCommand \index{-\/-opt-code-speed}
7199 The compiler will optimize code generation towards fast code, possibly
7200 at the expense of code size.
7202 \labelwidthstring 00.00.0000
7217 \begin_inset LatexCommand \index{-\/-opt-code-size}
7223 The compiler will optimize code generation towards compact code, possibly
7224 at the expense of code speed.
7228 \begin_inset LatexCommand \index{Options other}
7234 \labelwidthstring 00.00.0000
7250 \begin_inset LatexCommand \index{-\/-compile-only}
7255 \begin_inset LatexCommand \index{-c -\/-compile-only}
7261 will compile and assemble the source, but will not call the linkage editor.
7263 \labelwidthstring 00.00.0000
7282 \begin_inset LatexCommand \index{-\/-c1mode}
7288 reads the preprocessed source from standard input and compiles it.
7289 The file name for the assembler output must be specified using the -o option.
7291 \labelwidthstring 00.00.0000
7296 \begin_inset LatexCommand \index{-E}
7302 Run only the C preprocessor.
7303 Preprocess all the C source files specified and output the results to standard
7306 \labelwidthstring 00.00.0000
7312 \begin_inset LatexCommand \index{-o <path/file>}
7318 The output path resp.
7319 file where everything will be placed.
7320 If the parameter is a path, it must have a trailing slash (or backslash
7321 for the Windows binaries) to be recognized as a path.
7324 \labelwidthstring 00.00.0000
7339 \begin_inset LatexCommand \index{-\/-stack-auto}
7350 All functions in the source file will be compiled as
7355 \begin_inset LatexCommand \index{reentrant}
7360 the parameters and local variables will be allocated on the stack
7361 \begin_inset LatexCommand \index{stack}
7367 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
7371 Parameters and Local Variables for more details.
7372 If this option is used all source files in the project should be compiled
7374 It automatically implies --int-long-reent and --float-reent.
7377 \labelwidthstring 00.00.0000
7392 \begin_inset LatexCommand \index{-\/-callee-saves}
7396 function1[,function2][,function3]....
7399 The compiler by default uses a caller saves convention for register saving
7400 across function calls, however this can cause unnecessary register pushing
7401 & popping when calling small functions from larger functions.
7402 This option can be used to switch the register saving convention for the
7403 function names specified.
7404 The compiler will not save registers when calling these functions, no extra
7405 code will be generated at the entry & exit (function prologue
7408 \begin_inset LatexCommand \index{function prologue}
7417 \begin_inset LatexCommand \index{function epilogue}
7423 ) for these functions to save & restore the registers used by these functions,
7424 this can SUBSTANTIALLY reduce code & improve run time performance of the
7426 In the future the compiler (with inter procedural analysis) will be able
7427 to determine the appropriate scheme to use for each function call.
7428 DO NOT use this option for built-in functions such as _mulint..., if this
7429 option is used for a library function the appropriate library function
7430 needs to be recompiled with the same option.
7431 If the project consists of multiple source files then all the source file
7432 should be compiled with the same -
7442 -callee-saves option string.
7443 Also see #pragma\SpecialChar ~
7445 \begin_inset LatexCommand \index{\#pragma callee\_saves}
7451 \labelwidthstring 00.00.0000
7466 \begin_inset LatexCommand \index{-\/-debug}
7475 When this option is used the compiler will generate debug information.
7476 The debug information collected in a file with .cdb extension can be used
7478 For more information see documentation for SDCDB.
7479 Another file with no extension contains debug information in AOMF or AOMF51
7480 \begin_inset LatexCommand \index{AOMF, AOMF51}
7484 format which is commonly used by third party tools.
7486 \labelwidthstring 00.00.0000
7491 \begin_inset LatexCommand \index{-S}
7502 Stop after the stage of compilation proper; do not assemble.
7503 The output is an assembler code file for the input file specified.
7505 \labelwidthstring 00.00.0000
7520 \begin_inset LatexCommand \index{-\/-int-long-reent}
7526 Integer (16 bit) and long (32 bit) libraries have been compiled as reentrant.
7527 Note by default these libraries are compiled as non-reentrant.
7528 See section Installation for more details.
7530 \labelwidthstring 00.00.0000
7545 \begin_inset LatexCommand \index{-\/-cyclomatic}
7554 This option will cause the compiler to generate an information message for
7555 each function in the source file.
7556 The message contains some
7560 information about the function.
7561 The number of edges and nodes the compiler detected in the control flow
7562 graph of the function, and most importantly the
7564 cyclomatic complexity
7565 \begin_inset LatexCommand \index{Cyclomatic complexity}
7571 see section on Cyclomatic Complexity for more details.
7573 \labelwidthstring 00.00.0000
7588 \begin_inset LatexCommand \index{-\/-float-reent}
7594 Floating point library is compiled as reentrant
7595 \begin_inset LatexCommand \index{reentrant}
7600 See section Installation for more details.
7602 \labelwidthstring 00.00.0000
7617 \begin_inset LatexCommand \index{-\/-main-return}
7623 This option can be used if the code generated is called by a monitor program
7624 or if the main routine includes an endless loop.
7625 This option might result in slightly smaller code and save two bytes of
7627 The return from the 'main'
7628 \begin_inset LatexCommand \index{main return}
7632 function will return to the function calling main.
7633 The default setting is to lock up i.e.
7640 \labelwidthstring 00.00.0000
7655 \begin_inset LatexCommand \index{-\/-nostdinc}
7661 This will prevent the compiler from passing on the default include path
7662 to the preprocessor.
7664 \labelwidthstring 00.00.0000
7679 \begin_inset LatexCommand \index{-\/-nostdlib}
7685 This will prevent the compiler from passing on the default library
7686 \begin_inset LatexCommand \index{Libraries}
7692 \labelwidthstring 00.00.0000
7707 \begin_inset LatexCommand \index{-\/-verbose}
7713 Shows the various actions the compiler is performing.
7715 \labelwidthstring 00.00.0000
7720 \begin_inset LatexCommand \index{-V}
7726 Shows the actual commands the compiler is executing.
7728 \labelwidthstring 00.00.0000
7743 \begin_inset LatexCommand \index{-\/-no-c-code-in-asm}
7749 Hides your ugly and inefficient c-code from the asm file, so you can always
7750 blame the compiler :)
7752 \labelwidthstring 00.00.0000
7767 \begin_inset LatexCommand \index{-\/-no-peep-comments}
7773 Will not include peep-hole comments in the generated files.
7775 \labelwidthstring 00.00.0000
7790 \begin_inset LatexCommand \index{-\/-i-code-in-asm}
7796 Include i-codes in the asm file.
7797 Sounds like noise but is most helpful for debugging the compiler itself.
7799 \labelwidthstring 00.00.0000
7814 \begin_inset LatexCommand \index{-\/-less-pedantic}
7820 Disable some of the more pedantic warnings
7821 \begin_inset LatexCommand \index{Warnings}
7825 (jwk burps: please be more specific here, please!).
7827 \labelwidthstring 00.00.0000
7841 -disable-warning\SpecialChar ~
7843 \begin_inset LatexCommand \index{-\/-disable-warning}
7849 Disable specific warning with number <nnnn>.
7851 \labelwidthstring 00.00.0000
7866 \begin_inset LatexCommand \index{-\/-print-search-dirs}
7872 Display the directories in the compiler's search path
7874 \labelwidthstring 00.00.0000
7889 \begin_inset LatexCommand \index{-\/-vc}
7895 Display errors and warnings using MSVC style, so you can use SDCC with
7898 \labelwidthstring 00.00.0000
7913 \begin_inset LatexCommand \index{-\/-use-stdout}
7919 Send errors and warnings to stdout instead of stderr.
7921 \labelwidthstring 00.00.0000
7926 asmOption[,asmOption]
7929 \begin_inset LatexCommand \index{-Wa asmOption[,asmOption]}
7934 Pass the asmOption to the assembler
7935 \begin_inset LatexCommand \index{Options assembler}
7940 \begin_inset LatexCommand \index{Assembler options}
7945 See file sdcc/as/doc/asxhtm.html for assembler options.cd
7947 \labelwidthstring 00.00.0000
7962 \begin_inset LatexCommand \index{-\/-std-sdcc89}
7968 Generally follow the C89 standard, but allow SDCC features that conflict
7969 with the standard (default).
7971 \labelwidthstring 00.00.0000
7986 \begin_inset LatexCommand \index{-\/-std-c89}
7992 Follow the C89 standard and disable SDCC features that conflict with the
7995 \labelwidthstring 00.00.0000
8010 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8016 Generally follow the C99 standard, but allow SDCC features that conflict
8017 with the standard (incomplete support).
8019 \labelwidthstring 00.00.0000
8034 \begin_inset LatexCommand \index{-\/-std-sdcc99}
8040 Follow the C99 standard and disable SDCC features that conflict with the
8041 standard (incomplete support).
8043 \labelwidthstring 00.00.0000
8060 \begin_inset LatexCommand \index{-\/-codeseg <Value>}
8065 <Name> The name to be used for the code
8066 \begin_inset LatexCommand \index{code}
8070 segment, default CSEG.
8071 This is useful if you need to tell the compiler to put the code in a special
8072 segment so you can later on tell the linker to put this segment in a special
8074 Can be used for instance when using bank switching to put the code in a
8077 \labelwidthstring 00.00.0000
8094 \begin_inset LatexCommand \index{-\/-constseg <Value>}
8099 <Name> The name to be used for the const
8100 \begin_inset LatexCommand \index{code}
8104 segment, default CONST.
8105 This is useful if you need to tell the compiler to put the const data in
8106 a special segment so you can later on tell the linker to put this segment
8107 in a special place in memory.
8108 Can be used for instance when using bank switching to put the const data
8111 \labelwidthstring 00.00.0000
8123 a SDCC compiler option but if you want
8127 warnings you can use a separate tool dedicated to syntax checking like
8129 \begin_inset LatexCommand \label{lyx:more-pedantic-SPLINT}
8134 \begin_inset LatexCommand \index{lint (syntax checking tool)}
8139 \begin_inset LatexCommand \url{http://www.splint.org}
8144 To make your source files parseable by splint you will have to include
8150 \begin_inset LatexCommand \index{splint (syntax checking tool)}
8154 in your source file and add brackets around extended keywords (like
8157 \begin_inset Quotes sld
8170 \begin_inset Quotes srd
8178 \begin_inset Quotes sld
8181 __interrupt\SpecialChar ~
8183 \begin_inset Quotes srd
8191 Splint has an excellent on line manual at
8192 \begin_inset LatexCommand \url{http://www.splint.org/manual/}
8196 and it's capabilities go beyond pure syntax checking.
8197 You'll need to tell splint the location of SDCC's include files so a typical
8198 command line could look like this:
8202 splint\SpecialChar ~
8204 /usr/local/share/sdcc/include/mcs51/\SpecialChar ~
8209 Intermediate Dump Options
8210 \begin_inset LatexCommand \label{sub:Intermediate-Dump-Options}
8215 \begin_inset LatexCommand \index{Options intermediate dump}
8220 \begin_inset LatexCommand \index{Intermediate dump options}
8227 The following options are provided for the purpose of retargetting and debugging
8229 They provide a means to dump the intermediate code (iCode
8230 \begin_inset LatexCommand \index{iCode}
8234 ) generated by the compiler in human readable form at various stages of
8235 the compilation process.
8236 More on iCodes see chapter
8237 \begin_inset LatexCommand \ref{sub:The-anatomy-of}
8242 \begin_inset Quotes srd
8245 The anatomy of the compiler
8246 \begin_inset Quotes srd
8251 \labelwidthstring 00.00.0000
8266 \begin_inset LatexCommand \index{-\/-dumpraw}
8272 This option will cause the compiler to dump the intermediate code into
8275 <source filename>.dumpraw
8277 just after the intermediate code has been generated for a function, i.e.
8278 before any optimizations are done.
8280 \begin_inset LatexCommand \index{Basic blocks}
8284 at this stage ordered in the depth first number, so they may not be in
8285 sequence of execution.
8287 \labelwidthstring 00.00.0000
8302 \begin_inset LatexCommand \index{-\/-dumpgcse}
8308 Will create a dump of iCode's, after global subexpression elimination
8309 \begin_inset LatexCommand \index{Global subexpression elimination}
8315 <source filename>.dumpgcse.
8317 \labelwidthstring 00.00.0000
8332 \begin_inset LatexCommand \index{-\/-dumpdeadcode}
8338 Will create a dump of iCode's, after deadcode elimination
8339 \begin_inset LatexCommand \index{Dead-code elimination}
8345 <source filename>.dumpdeadcode.
8347 \labelwidthstring 00.00.0000
8362 \begin_inset LatexCommand \index{-\/-dumploop}
8371 Will create a dump of iCode's, after loop optimizations
8372 \begin_inset LatexCommand \index{Loop optimization}
8378 <source filename>.dumploop.
8380 \labelwidthstring 00.00.0000
8395 \begin_inset LatexCommand \index{-\/-dumprange}
8404 Will create a dump of iCode's, after live range analysis
8405 \begin_inset LatexCommand \index{Live range analysis}
8411 <source filename>.dumprange.
8413 \labelwidthstring 00.00.0000
8428 \begin_inset LatexCommand \index{-\/-dumlrange}
8434 Will dump the life ranges
8435 \begin_inset LatexCommand \index{Live range analysis}
8441 \labelwidthstring 00.00.0000
8456 \begin_inset LatexCommand \index{-\/-dumpregassign}
8465 Will create a dump of iCode's, after register assignment
8466 \begin_inset LatexCommand \index{Register assignment}
8472 <source filename>.dumprassgn.
8474 \labelwidthstring 00.00.0000
8489 \begin_inset LatexCommand \index{-\/-dumplrange}
8495 Will create a dump of the live ranges of iTemp's
8497 \labelwidthstring 00.00.0000
8512 \begin_inset LatexCommand \index{-\/-dumpall}
8523 Will cause all the above mentioned dumps to be created.
8526 Redirecting output on Windows Shells
8529 By default SDCC writes it's error messages to
8530 \begin_inset Quotes sld
8534 \begin_inset Quotes srd
8538 To force all messages to
8539 \begin_inset Quotes sld
8543 \begin_inset Quotes srd
8567 \begin_inset LatexCommand \index{-\/-use-stdout}
8572 Additionally, if you happen to have visual studio installed in your windows
8573 machine, you can use it to compile your sources using a custom build and
8589 \begin_inset LatexCommand \index{-\/-vc}
8594 Something like this should work:
8638 -model-large -c $(InputPath)
8641 Environment variables
8642 \begin_inset LatexCommand \index{Environment variables}
8649 SDCC recognizes the following environment variables:
8651 \labelwidthstring 00.00.0000
8656 \begin_inset LatexCommand \index{SDCC\_LEAVE\_SIGNALS}
8662 SDCC installs a signal handler
8663 \begin_inset LatexCommand \index{signal handler}
8667 to be able to delete temporary files after an user break (^C) or an exception.
8668 If this environment variable is set, SDCC won't install the signal handler
8669 in order to be able to debug SDCC.
8671 \labelwidthstring 00.00.0000
8678 \begin_inset LatexCommand \index{TMP, TEMP, TMPDIR}
8684 Path, where temporary files will be created.
8685 The order of the variables is the search order.
8686 In a standard *nix environment these variables are not set, and there's
8687 no need to set them.
8688 On Windows it's recommended to set one of them.
8690 \labelwidthstring 00.00.0000
8695 \begin_inset LatexCommand \index{SDCC\_HOME}
8702 \begin_inset LatexCommand \ref{sub:Install-paths}
8708 \begin_inset Quotes sld
8712 \begin_inset Quotes srd
8717 \labelwidthstring 00.00.0000
8722 \begin_inset LatexCommand \index{SDCC\_INCLUDE}
8729 \begin_inset LatexCommand \ref{sub:Search-Paths}
8735 \begin_inset Quotes sld
8739 \begin_inset Quotes srd
8744 \labelwidthstring 00.00.0000
8749 \begin_inset LatexCommand \index{SDCC\_LIB}
8756 \begin_inset LatexCommand \ref{sub:Search-Paths}
8762 \begin_inset Quotes sld
8766 \begin_inset Quotes srd
8772 There are some more environment variables recognized by SDCC, but these
8773 are solely used for debugging purposes.
8774 They can change or disappear very quickly, and will never be documented.
8777 Storage Class Language Extensions
8780 MCS51/DS390 Storage Class
8781 \begin_inset LatexCommand \index{Storage class}
8788 In addition to the ANSI storage classes SDCC allows the following MCS51
8789 specific storage classes:
8790 \layout Subsubsection
8793 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
8798 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
8803 \begin_inset LatexCommand \index{near (storage class)}
8808 \begin_inset LatexCommand \index{\_\_near (storage class)}
8819 storage class for the Small Memory model (
8827 can be used synonymously).
8828 Variables declared with this storage class will be allocated in the directly
8829 addressable portion of the internal RAM of a 8051, e.g.:
8834 data unsigned char test_data;
8837 Writing 0x01 to this variable generates the assembly code:
8842 75*00 01\SpecialChar ~
8848 \layout Subsubsection
8851 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
8856 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
8861 \begin_inset LatexCommand \index{far (storage class)}
8866 \begin_inset LatexCommand \index{\_\_far (storage class)}
8873 Variables declared with this storage class will be placed in the external
8879 storage class for the Large Memory model, e.g.:
8884 xdata unsigned char test_xdata;
8887 Writing 0x01 to this variable generates the assembly code:
8892 90s00r00\SpecialChar ~
8921 \layout Subsubsection
8924 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
8929 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
8936 Variables declared with this storage class will be allocated into the indirectly
8937 addressable portion of the internal ram of a 8051, e.g.:
8942 idata unsigned char test_idata;
8945 Writing 0x01 to this variable generates the assembly code:
8974 Please note, the first 128 byte of idata physically access the same RAM
8976 The original 8051 had 128 byte idata memory, nowadays most devices have
8977 256 byte idata memory.
8979 \begin_inset LatexCommand \index{stack}
8983 is located in idata memory.
8984 \layout Subsubsection
8987 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
8992 \begin_inset LatexCommand \index{\_\_pdata (mcs51, ds390 storage class)}
8999 Paged xdata access is just as straightforward as using the other addressing
9001 It is typically located at the start of xdata and has a maximum size of
9003 The following example writes 0x01 to the pdata variable.
9004 Please note, pdata access physically accesses xdata memory.
9005 The high byte of the address is determined by port P2
9006 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
9010 (or in case of some 8051 variants by a separate Special Function Register,
9012 \begin_inset LatexCommand \ref{sub:MCS51-variants}
9021 storage class for the Medium Memory model, e.g.:
9026 pdata unsigned char test_pdata;
9029 Writing 0x01 to this variable generates the assembly code:
9073 \begin_inset LatexCommand \index{-\/-xstack}
9077 option is used the pdata memory area is followed by the xstack memory area
9078 and the sum of their sizes is limited to 256 bytes.
9079 \layout Subsubsection
9082 \begin_inset LatexCommand \index{code}
9087 \begin_inset LatexCommand \index{\_\_code}
9094 'Variables' declared with this storage class will be placed in the code
9100 code unsigned char test_code;
9103 Read access to this variable generates the assembly code:
9108 90s00r6F\SpecialChar ~
9111 mov dptr,#_test_code
9140 indexed arrays of characters in code memory can be accessed efficiently:
9145 code char test_array[] = {'c','h','e','a','p'};
9148 Read access to this array using an 8-bit unsigned index generates the assembly
9165 90s00r41\SpecialChar ~
9168 mov dptr,#_test_array
9183 \layout Subsubsection
9186 \begin_inset LatexCommand \index{bit}
9191 \begin_inset LatexCommand \index{\_\_bit}
9198 This is a data-type and a storage class specifier.
9199 When a variable is declared as a bit, it is allocated into the bit addressable
9200 memory of 8051, e.g.:
9208 Writing 1 to this variable generates the assembly code:
9224 The bit addressable memory consists of 128 bits which are located from 0x20
9225 to 0x2f in data memory.
9228 Apart from this 8051 specific storage class most architectures support ANSI-C
9230 \begin_inset LatexCommand \index{bitfields}
9240 Not really meant as examples, but nevertheless showing what bitfields are
9241 about: device/include/mc68hc908qy.h and support/regression/tests/bitfields.c
9245 In accordance with ISO/IEC 9899 bits and bitfields without an explicit
9246 signed modifier are implemented as unsigned.
9247 \layout Subsubsection
9250 \begin_inset LatexCommand \index{sfr}
9255 \begin_inset LatexCommand \index{\_\_sfr}
9260 \begin_inset LatexCommand \index{sfr16}
9265 \begin_inset LatexCommand \index{\_\_sfr16}
9270 \begin_inset LatexCommand \index{sfr32}
9275 \begin_inset LatexCommand \index{\_\_sfr32}
9280 \begin_inset LatexCommand \index{\_\_sbit}
9287 Like the bit keyword,
9289 sfr / sfr16 / sfr32 / sbit
9291 signify both a data-type and storage class, they are used to describe the
9312 variables of a 8051, eg:
9318 \begin_inset LatexCommand \index{at}
9323 \begin_inset LatexCommand \index{\_\_at}
9327 0x80 P0;\SpecialChar ~
9328 /* special function register P0 at location 0x80 */
9330 /* 16 bit special function register combination for timer 0 */
9332 /* with the high byte at location 0x8C and the low byte at location 0x8A
9336 \begin_inset LatexCommand \index{at}
9341 \begin_inset LatexCommand \index{\_\_at}
9347 sbit at 0xd7 CY; /* CY (Carry Flag
9348 \begin_inset LatexCommand \index{Flags}
9353 \begin_inset LatexCommand \index{Carry flag}
9360 Special function registers which are located on an address dividable by
9361 8 are bit-addressable, an
9365 addresses a specific bit within these sfr.
9367 16 Bit and 32 bit special function register combinations which require a
9368 certain access order are better not declared using
9377 Allthough SDCC usually accesses them Least Significant Byte (LSB) first,
9378 this is not guaranteed.
9379 \layout Subsubsection
9382 \begin_inset LatexCommand \index{Pointer}
9386 to MCS51/DS390 specific memory spaces
9389 SDCC allows (via language extensions) pointers to explicitly point to any
9390 of the memory spaces
9391 \begin_inset LatexCommand \index{Memory model}
9396 In addition to the explicit pointers, the compiler uses (by default) generic
9397 pointers which can be used to point to any of the memory spaces.
9401 Pointer declaration examples:
9406 /* pointer physically in internal ram pointing to object in external ram
9409 xdata unsigned char * data p;
9413 /* pointer physically in external ram pointing to object in internal ram
9416 data unsigned char * xdata p;
9420 /* pointer physically in code rom pointing to data in xdata space */
9422 xdata unsigned char * code p;
9426 /* pointer physically in code space pointing to data in code space */
9428 code unsigned char * code p;
9432 /* the following is a generic pointer physically located in xdata space
9439 /* the following is a function pointer physically located in data space
9442 char (* data fp)(void);
9445 Well you get the idea.
9450 All unqualified pointers are treated as 3-byte (4-byte for the ds390)
9463 The highest order byte of the
9467 pointers contains the data space information.
9468 Assembler support routines are called whenever data is stored or retrieved
9474 These are useful for developing reusable library
9475 \begin_inset LatexCommand \index{Libraries}
9480 Explicitly specifying the pointer type will generate the most efficient
9482 \layout Subsubsection
9484 Notes on MCS51 memory
9485 \begin_inset LatexCommand \index{MCS51 memory}
9492 The 8051 family of microcontrollers have a minimum of 128 bytes of internal
9493 RAM memory which is structured as follows:
9497 - Bytes 00-1F - 32 bytes to hold up to 4 banks of the registers R0 to R7,
9500 - Bytes 20-2F - 16 bytes to hold 128 bit
9501 \begin_inset LatexCommand \index{bit}
9507 - Bytes 30-7F - 80 bytes for general purpose use.
9512 Additionally some members of the MCS51 family may have up to 128 bytes of
9513 additional, indirectly addressable, internal RAM memory (
9518 \begin_inset LatexCommand \index{idata (mcs51, ds390 storage class)}
9523 \begin_inset LatexCommand \index{\_\_idata (mcs51, ds390 storage class)}
9528 Furthermore, some chips may have some built in external memory (
9533 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
9538 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
9542 ) which should not be confused with the internal, directly addressable RAM
9548 \begin_inset LatexCommand \index{data (mcs51, ds390 storage class)}
9553 \begin_inset LatexCommand \index{\_\_data (mcs51, ds390 storage class)}
9558 Sometimes this built in
9562 memory has to be activated before using it (you can probably find this
9563 information on the datasheet of the microcontroller your are using, see
9565 \begin_inset LatexCommand \ref{sub:Startup-Code}
9573 Normally SDCC will only use the first bank
9574 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
9578 of registers (register bank 0), but it is possible to specify that other
9579 banks of registers (keyword
9586 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
9591 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
9597 ) should be used in interrupt
9598 \begin_inset LatexCommand \index{interrupt}
9603 \begin_inset LatexCommand \index{\_\_interrupt}
9608 By default, the compiler will place the stack after the last byte of allocated
9609 memory for variables.
9610 For example, if the first 2 banks of registers are used, and only four
9615 variables, it will position the base of the internal stack at address 20
9617 This implies that as the stack
9618 \begin_inset LatexCommand \index{stack}
9622 grows, it will use up the remaining register banks, and the 16 bytes used
9623 by the 128 bit variables, and 80 bytes for general purpose use.
9624 If any bit variables are used, the data variables will be placed in unused
9625 register banks and after the byte holding the last bit variable.
9626 For example, if register banks 0 and 1 are used, and there are 9 bit variables
9631 variables will be placed starting from address 0x10 to 0x20 and continue
9644 \begin_inset LatexCommand \index{-\/-data-loc <Value>}
9648 to specify the start address of the
9663 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
9667 to specify the size of the total internal RAM (
9679 By default the 8051 linker will place the stack after the last byte of (i)data
9692 \begin_inset LatexCommand \index{-\/-stack-loc <Value>}
9696 allows you to specify the start of the stack, i.e.
9697 you could start it after any data in the general purpose area.
9698 If your microcontroller has additional indirectly addressable internal
9703 ) you can place the stack on it.
9704 You may also need to use -
9715 \begin_inset LatexCommand \index{-\/-xdata-loc<Value>}
9719 to set the start address of the external RAM (
9734 \begin_inset LatexCommand \index{-\/-xram-size <Value>}
9738 to specify its size.
9739 Same goes for the code memory, using -
9750 \begin_inset LatexCommand \index{-\/-code-loc <Value>}
9765 \begin_inset LatexCommand \index{-\/-code-size <Value>}
9770 If in doubt, don't specify any options and see if the resulting memory
9771 layout is appropriate, then you can adjust it.
9774 The linker generates two files with memory allocation information.
9775 The first, with extension .map
9776 \begin_inset LatexCommand \index{<file>.map}
9780 shows all the variables and segments.
9781 The second with extension .mem
9782 \begin_inset LatexCommand \index{<file>.mem}
9786 shows the final memory layout.
9787 The linker will complain either if memory segments overlap, there is not
9788 enough memory, or there is not enough space for stack.
9789 If you get any linking warnings and/or errors related to stack or segments
9790 allocation, take a look at either the .map or .mem files to find out what
9792 The .mem file may even suggest a solution to the problem.
9795 Z80/Z180 Storage Class
9796 \begin_inset LatexCommand \index{Storage class}
9801 \layout Subsubsection
9804 \begin_inset LatexCommand \index{sfr}
9809 \begin_inset LatexCommand \index{\_\_sfr}
9813 (in/out to 8-bit addresses)
9817 \begin_inset LatexCommand \index{Z80}
9821 family has separate address spaces for memory and
9831 \begin_inset LatexCommand \index{I/O memory (Z80, Z180)}
9835 is accessed with special instructions, e.g.:
9840 sfr at 0x78 IoPort;\SpecialChar ~
9842 /* define a var in I/O space at 78h called IoPort */
9846 Writing 0x01 to this variable generates the assembly code:
9866 \layout Subsubsection
9869 \begin_inset LatexCommand \index{sfr}
9874 \begin_inset LatexCommand \index{\_\_sfr}
9878 (in/out to 16-bit addresses)
9885 is used to support 16 bit addresses in I/O memory e.g.:
9891 \begin_inset LatexCommand \index{at}
9896 \begin_inset LatexCommand \index{\_\_at}
9903 Writing 0x01 to this variable generates the assembly code:
9908 01 23 01\SpecialChar ~
9928 \layout Subsubsection
9931 \begin_inset LatexCommand \index{sfr}
9936 \begin_inset LatexCommand \index{\_\_sfr}
9940 (in0/out0 to 8 bit addresses on Z180
9941 \begin_inset LatexCommand \index{Z180}
9946 \begin_inset LatexCommand \index{HD64180}
9953 The compiler option -
9963 -portmode=180 (80) and a compiler #pragma\SpecialChar ~
9965 \begin_inset LatexCommand \index{\#pragma portmode}
9969 =z180 (z80) is used to turn on (off) the Z180/HD64180 port addressing instructio
9979 If you include the file z180.h this will be set automatically.
9983 \begin_inset LatexCommand \index{Storage class}
9988 \layout Subsubsection
9991 \begin_inset LatexCommand \index{data (hc08 storage class)}
9996 \begin_inset LatexCommand \index{\_\_data (hc08 storage class)}
10003 The data storage class declares a variable that resides in the first 256
10004 bytes of memory (the direct page).
10005 The HC08 is most efficient at accessing variables (especially pointers)
10007 \layout Subsubsection
10010 \begin_inset LatexCommand \index{xdata (hc08 storage class)}
10015 \begin_inset LatexCommand \index{\_\_xdata (hc08 storage class)}
10022 The xdata storage class declares a variable that can reside anywhere in
10024 This is the default if no storage class is specified.
10028 Absolute Addressing
10029 \begin_inset LatexCommand \index{Absolute addressing}
10036 Data items can be assigned an absolute address with the
10039 \begin_inset LatexCommand \index{at}
10044 \begin_inset LatexCommand \index{\_\_at}
10050 keyword, in addition to a storage class, e.g.:
10056 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10061 \begin_inset LatexCommand \index{\_\_xdata (mcs51, ds390 storage class)}
10066 \begin_inset LatexCommand \index{at}
10071 \begin_inset LatexCommand \index{\_\_at}
10075 0x7ffe unsigned int chksum;
10078 In the above example the variable chksum will be located at 0x7ffe and 0x7fff
10079 of the external ram.
10084 reserve any space for variables declared in this way (they are implemented
10085 with an equate in the assembler).
10086 Thus it is left to the programmer to make sure there are no overlaps with
10087 other variables that are declared without the absolute address.
10088 The assembler listing file (.lst
10089 \begin_inset LatexCommand \index{<file>.lst}
10093 ) and the linker output files (.rst
10094 \begin_inset LatexCommand \index{<file>.rst}
10099 \begin_inset LatexCommand \index{<file>.map}
10103 ) are good places to look for such overlaps.
10104 Variables with an absolute address are
10109 \begin_inset LatexCommand \index{Variable initialization}
10116 In case of memory mapped I/O devices the keyword
10120 has to be used to tell the compiler that accesses might not be removed:
10126 \begin_inset LatexCommand \index{volatile}
10131 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
10136 \begin_inset LatexCommand \index{at}
10140 0x8000 unsigned char PORTA_8255;
10143 For some architectures (mcs51) array accesses are more efficient if an (xdata/fa
10148 \begin_inset LatexCommand \index{Aligned array}
10155 starts at a block (256 byte) boundary
10156 \begin_inset LatexCommand \index{block boundary}
10161 \begin_inset LatexCommand \ref{sub:A-Step-by Assembler Introduction}
10167 Absolute addresses can be specified for variables in all storage classes,
10174 \begin_inset LatexCommand \index{bit}
10179 \begin_inset LatexCommand \index{at}
10186 The above example will allocate the variable at offset 0x02 in the bit-addressab
10188 There is no real advantage to assigning absolute addresses to variables
10189 in this manner, unless you want strict control over all the variables allocated.
10190 One possible use would be to write hardware portable code.
10191 For example, if you have a routine that uses one or more of the microcontroller
10192 I/O pins, and such pins are different for two different hardwares, you
10193 can declare the I/O pins in your routine using:
10199 \begin_inset LatexCommand \index{volatile}
10203 bit MOSI;\SpecialChar ~
10207 /* master out, slave in */
10209 extern volatile bit MISO;\SpecialChar ~
10213 /* master in, slave out */
10215 extern volatile bit MCLK;\SpecialChar ~
10223 /* Input and Output of a byte on a 3-wire serial bus.
10228 If needed adapt polarity of clock, polarity of data and bit order
10233 unsigned char spi_io(unsigned char out_byte)
10257 MOSI = out_byte & 0x80;
10287 /* _asm nop _endasm; */\SpecialChar ~
10295 /* for slow peripherals */
10346 Then, someplace in the code for the first hardware you would use
10352 \begin_inset LatexCommand \index{at}
10357 \begin_inset LatexCommand \index{\_\_at}
10361 0x80 MOSI;\SpecialChar ~
10365 /* I/O port 0, bit 0 */
10367 bit at 0x81 MISO;\SpecialChar ~
10371 /* I/O port 0, bit 1 */
10373 bit at 0x82 MCLK;\SpecialChar ~
10377 /* I/O port 0, bit 2 */
10380 Similarly, for the second hardware you would use
10385 bit at 0x83 MOSI;\SpecialChar ~
10389 /* I/O port 0, bit 3 */
10391 bit at 0x91 MISO;\SpecialChar ~
10395 /* I/O port 1, bit 1 */
10398 \begin_inset LatexCommand \index{bit}
10402 at 0x92 MCLK;\SpecialChar ~
10406 /* I/O port 1, bit 2 */
10409 and you can use the same hardware dependent routine without changes, as
10410 for example in a library.
10411 This is somehow similar to sbit, but only one absolute address has to be
10412 specified in the whole project.
10416 \begin_inset LatexCommand \index{Parameters}
10421 \begin_inset LatexCommand \index{function parameter}
10426 \begin_inset LatexCommand \index{local variables}
10431 \begin_inset LatexCommand \label{sec:Parameters-and-Local-Variables}
10438 Automatic (local) variables and parameters to functions can either be placed
10439 on the stack or in data-space.
10440 The default action of the compiler is to place these variables in the internal
10441 RAM (for small model) or external RAM (for large model).
10442 This in fact makes them similar to
10445 \begin_inset LatexCommand \index{static}
10451 so by default functions are non-reentrant
10452 \begin_inset LatexCommand \index{reentrant}
10461 They can be placed on the stack
10462 \begin_inset LatexCommand \index{stack}
10479 \begin_inset LatexCommand \index{-\/-stack-auto}
10487 #pragma\SpecialChar ~
10491 \begin_inset LatexCommand \index{\#pragma stackauto}
10498 \begin_inset LatexCommand \index{reentrant}
10504 keyword in the function declaration, e.g.:
10509 unsigned char foo(char i) reentrant
10523 Since stack space on 8051 is limited, the
10541 option should be used sparingly.
10542 Note that the reentrant keyword just means that the parameters & local
10543 variables will be allocated to the stack, it
10547 mean that the function is register bank
10548 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
10557 \begin_inset LatexCommand \index{local variables}
10561 can be assigned storage classes and absolute
10562 \begin_inset LatexCommand \index{Absolute addressing}
10571 unsigned char foo()
10579 xdata unsigned char i;
10592 \begin_inset LatexCommand \index{at}
10596 0x31 unsigned char j;
10608 In the above example the variable
10612 will be allocated in the external ram,
10616 in bit addressable space and
10635 or when a function is declared as
10639 this should only be done for static variables.
10643 \begin_inset LatexCommand \index{function parameter}
10647 however are not allowed any storage class
10648 \begin_inset LatexCommand \index{Storage class}
10652 , (storage classes for parameters will be ignored), their allocation is
10653 governed by the memory model in use, and the reentrancy options.
10656 It is however allowed to use bit parameters in reentrant functions and also
10657 non-static local bit variables are supported.
10658 Efficient use is limited to 8 semi-bitregisters in bit space.
10659 They are pushed and popped to stack as a single byte just like the normal
10664 \begin_inset LatexCommand \label{sub:Overlaying}
10669 \begin_inset LatexCommand \index{Overlaying}
10677 \begin_inset LatexCommand \index{reentrant}
10681 functions SDCC will try to reduce internal ram space usage by overlaying
10682 parameters and local variables of a function (if possible).
10683 Parameters and local variables
10684 \begin_inset LatexCommand \index{local variables}
10688 of a function will be allocated to an overlayable segment if the function
10691 no other function calls and the function is non-reentrant and the memory
10693 \begin_inset LatexCommand \index{Memory model}
10700 If an explicit storage class
10701 \begin_inset LatexCommand \index{Storage class}
10705 is specified for a local variable, it will NOT be overlayed.
10708 Note that the compiler (not the linkage editor) makes the decision for overlayin
10710 Functions that are called from an interrupt service routine should be preceded
10711 by a #pragma\SpecialChar ~
10713 \begin_inset LatexCommand \index{\#pragma nooverlay}
10717 if they are not reentrant.
10720 Also note that the compiler does not do any processing of inline assembler
10721 code, so the compiler might incorrectly assign local variables and parameters
10722 of a function into the overlay segment if the inline assembler code calls
10723 other c-functions that might use the overlay.
10724 In that case the #pragma\SpecialChar ~
10725 nooverlay should be used.
10728 Parameters and local variables of functions that contain 16 or 32 bit multiplica
10730 \begin_inset LatexCommand \index{Multiplication}
10735 \begin_inset LatexCommand \index{Division}
10739 will NOT be overlayed since these are implemented using external functions,
10748 \begin_inset LatexCommand \index{\#pragma nooverlay}
10754 void set_error(unsigned char errcd)
10770 void some_isr () interrupt
10771 \begin_inset LatexCommand \index{interrupt}
10801 In the above example the parameter
10809 would be assigned to the overlayable segment if the #pragma\SpecialChar ~
10811 not present, this could cause unpredictable runtime behavior when called
10812 from an interrupt service routine.
10813 The #pragma\SpecialChar ~
10814 nooverlay ensures that the parameters and local variables for
10815 the function are NOT overlayed.
10818 Interrupt Service Routines
10819 \begin_inset LatexCommand \label{sub:Interrupt-Service-Routines}
10826 General Information
10841 outines to be coded in C, with some extended keywords.
10846 void timer_isr (void) interrupt 1 using 1
10860 The optional number following the
10863 \begin_inset LatexCommand \index{interrupt}
10868 \begin_inset LatexCommand \index{\_\_interrupt}
10874 keyword is the interrupt number this routine will service.
10875 When present, the compiler will insert a call to this routine in the interrupt
10876 vector table for the interrupt number specified.
10877 If you have multiple source files in your project, interrupt service routines
10878 can be present in any of them, but a prototype of the isr MUST be present
10879 or included in the file that contains the function
10887 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
10892 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
10898 keyword can be used to tell the compiler to use the specified register
10899 bank (8051 specific) when generating code for this function.
10905 Interrupt service routines open the door for some very interesting bugs:
10907 If an interrupt service routine changes variables which are accessed by
10908 other functions these variables have to be declared
10913 \begin_inset LatexCommand \index{volatile}
10921 If the access to these variables is not
10924 \begin_inset LatexCommand \index{atomic}
10931 the processor needs more than one instruction for the access and could
10932 be interrupted while accessing the variable) the interrupt must be disabled
10933 during the access to avoid inconsistent data.
10934 Access to 16 or 32 bit variables is obviously not atomic on 8 bit CPUs
10935 and should be protected by disabling interrupts.
10936 You're not automatically on the safe side if you use 8 bit variables though.
10937 We need an example here: f.e.
10938 on the 8051 the harmless looking
10939 \begin_inset Quotes srd
10944 flags\SpecialChar ~
10949 \begin_inset Quotes sld
10958 \begin_inset Quotes srd
10963 flags\SpecialChar ~
10968 \begin_inset Quotes sld
10971 from within an interrupt routine might get lost if the interrupt occurs
10974 \begin_inset Quotes sld
10979 counter\SpecialChar ~
10984 \begin_inset Quotes srd
10987 is not atomic on the 8051 even if
10991 is located in data memory.
10992 Bugs like these are hard to reproduce and can cause a lot of trouble.
10996 The return address and the registers used in the interrupt service routine
10997 are saved on the stack
10998 \begin_inset LatexCommand \index{stack}
11002 so there must be sufficient stack space.
11003 If there isn't variables or registers (or even the return address itself)
11010 \begin_inset LatexCommand \index{stack overflow}
11014 is most likely to happen if the interrupt occurs during the
11015 \begin_inset Quotes sld
11019 \begin_inset Quotes srd
11022 subroutine when the stack is already in use for f.e.
11023 many return addresses.
11026 A special note here, int (16 bit) and long (32 bit) integer division
11027 \begin_inset LatexCommand \index{Division}
11032 \begin_inset LatexCommand \index{Multiplication}
11037 \begin_inset LatexCommand \index{Modulus}
11042 \begin_inset LatexCommand \index{Floating point support}
11046 operations are implemented using external support routines developed in
11048 If an interrupt service routine needs to do any of these operations then
11049 the support routines (as mentioned in a following section) will have to
11050 be recompiled using the
11063 \begin_inset LatexCommand \index{-\/-stack-auto}
11069 option and the source file will need to be compiled using the
11084 \begin_inset LatexCommand \index{-\/-int-long-reent}
11091 Calling other functions from an interrupt service routine is not recommended,
11092 avoid it if possible.
11093 Note that when some function is called from an interrupt service routine
11094 it should be preceded by a #pragma\SpecialChar ~
11096 \begin_inset LatexCommand \index{\#pragma nooverlay}
11100 if it is not reentrant.
11101 Furthermore nonreentrant functions should not be called from the main program
11102 while the interrupt service routine might be active.
11103 They also must not be called from low priority interrupt service routines
11104 while a high priority interrupt service routine might be active.
11105 You could use semaphores or make the function
11109 if all parameters are passed in registers.
11114 \begin_inset LatexCommand \ref{sub:Overlaying}
11119 about Overlaying and section
11120 \begin_inset LatexCommand \ref{sub:Functions-using-private-banks}
11125 about Functions using private register banks.
11128 MCS51/DS390 Interrupt Service Routines
11131 Interrupt numbers and the corresponding address & descriptions for the Standard
11132 8051/8052 are listed below.
11133 SDCC will automatically adjust the interrupt vector table to the maximum
11134 interrupt number specified.
11140 \begin_inset Tabular
11141 <lyxtabular version="3" rows="7" columns="3">
11143 <column alignment="center" valignment="top" leftline="true" width="0in">
11144 <column alignment="center" valignment="top" leftline="true" width="0in">
11145 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0in">
11146 <row topline="true" bottomline="true">
11147 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11155 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11163 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11172 <row topline="true">
11173 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11181 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11189 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11198 <row topline="true">
11199 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11207 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11215 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11224 <row topline="true">
11225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11233 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11250 <row topline="true">
11251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11259 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11267 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11276 <row topline="true">
11277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11285 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11302 <row topline="true" bottomline="true">
11303 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11311 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
11319 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
11337 If the interrupt service routine is defined without
11340 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11345 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11351 a register bank or with register bank 0 (
11355 0), the compiler will save the registers used by itself on the stack upon
11356 entry and restore them at exit, however if such an interrupt service routine
11357 calls another function then the entire register bank will be saved on the
11359 This scheme may be advantageous for small interrupt service routines which
11360 have low register usage.
11363 If the interrupt service routine is defined to be using a specific register
11368 & psw are saved and restored, if such an interrupt service routine calls
11369 another function (using another register bank) then the entire register
11370 bank of the called function will be saved on the stack.
11371 This scheme is recommended for larger interrupt service routines.
11374 HC08 Interrupt Service Routines
11377 Since the number of interrupts available is chip specific and the interrupt
11378 vector table always ends at the last byte of memory, the interrupt numbers
11379 corresponds to the interrupt vectors in reverse order of address.
11380 For example, interrupt 1 will use the interrupt vector at 0xfffc, interrupt
11381 2 will use the interrupt vector at 0xfffa, and so on.
11382 However, interrupt 0 (the reset vector at 0xfffe) is not redefinable in
11383 this way; instead see section
11384 \begin_inset LatexCommand \ref{sub:Startup-Code}
11388 for details on customizing startup.
11391 Z80 Interrupt Service Routines
11394 The Z80 uses several different methods for determining the correct interrupt
11395 vector depending on the hardware implementation.
11396 Therefore, SDCC ignores the optional interrupt number and does not attempt
11397 to generate an interrupt vector table.
11400 By default, SDCC generates code for a maskable interrupt, which uses a RETI
11401 instruction to return from the interrupt.
11402 To write an interrupt handler for the non-maskable interrupt, which needs
11403 a RETN instruction instead, add the
11412 void nmi_isr (void) critical interrupt
11426 However if you need to create a non-interruptable interrupt service routine
11427 you would also require the
11432 To distinguish between this and an nmi_isr you must provide an interrupt
11436 Enabling and Disabling Interrupts
11439 Critical Functions and Critical Statements
11442 A special keyword may be associated with a block or a function declaring
11448 SDCC will generate code to disable all interrupts
11449 \begin_inset LatexCommand \index{interrupt}
11453 upon entry to a critical function and restore the interrupt enable to the
11454 previous state before returning.
11455 Nesting critical functions will need one additional byte on the stack
11456 \begin_inset LatexCommand \index{stack}
11465 int foo () critical
11466 \begin_inset LatexCommand \index{critical}
11471 \begin_inset LatexCommand \index{\_\_critical}
11496 The critical attribute maybe used with other attributes like
11506 may also be used to disable interrupts more locally:
11514 More than one statement could have been included in the block.
11517 Enabling and Disabling Interrupts directly
11521 \begin_inset LatexCommand \index{interrupt}
11525 can also be disabled and enabled directly (8051):
11530 EA = 0;\SpecialChar ~
11593 EA = 1;\SpecialChar ~
11660 On other architectures which have seperate opcodes for enabling and disabling
11661 interrupts you might want to make use of defines with inline assembly
11662 \begin_inset LatexCommand \index{Assembler routines}
11672 \begin_inset LatexCommand \index{\_asm}
11681 \begin_inset LatexCommand \index{\_endasm}
11690 #define SEI _asm\SpecialChar ~
11702 Note: it is sometimes sufficient to disable only a specific interrupt source
11704 a timer or serial interrupt by manipulating an
11707 \begin_inset LatexCommand \index{interrupt mask}
11717 Usually the time during which interrupts are disabled should be kept as
11719 This minimizes both
11724 \begin_inset LatexCommand \index{interrupt latency}
11728 (the time between the occurrence of the interrupt and the execution of
11729 the first code in the interrupt routine) and
11734 \begin_inset LatexCommand \index{interrupt jitter}
11738 (the difference between the shortest and the longest interrupt latency).
11739 These really are something different, f.e.
11740 a serial interrupt has to be served before its buffer overruns so it cares
11741 for the maximum interrupt latency, whereas it does not care about jitter.
11742 On a loudspeaker driven via a digital to analog converter which is fed
11743 by an interrupt a latency of a few milliseconds might be tolerable, whereas
11744 a much smaller jitter will be very audible.
11747 You can reenable interrupts within an interrupt routine and on some architecture
11748 s you can make use of two (or more) levels of
11750 interrupt priorities
11753 \begin_inset LatexCommand \index{interrupt priority}
11758 On some architectures which don't support interrupt priorities these can
11759 be implemented by manipulating the interrupt mask and reenabling interrupts
11760 within the interrupt routine.
11761 Check there is sufficient space on the stack
11762 \begin_inset LatexCommand \index{stack}
11766 and don't add complexity unless you have to.
11771 \begin_inset LatexCommand \index{semaphore}
11775 locking (mcs51/ds390)
11778 Some architectures (mcs51/ds390) have an atomic
11779 \begin_inset LatexCommand \index{atomic}
11792 These type of instructions are typically used in preemptive multitasking
11793 systems, where a routine f.e.
11794 claims the use of a data structure ('acquires a lock
11795 \begin_inset LatexCommand \index{lock}
11799 on it'), makes some modifications and then releases the lock when the data
11800 structure is consistent again.
11801 The instruction may also be used if interrupt and non-interrupt code have
11802 to compete for a resource.
11803 With the atomic bit test and clear instruction interrupts
11804 \begin_inset LatexCommand \index{interrupt}
11808 don't have to be disabled for the locking operation.
11812 SDCC generates this instruction if the source follows this pattern:
11818 \begin_inset LatexCommand \index{volatile}
11822 bit resource_is_free;
11826 if (resource_is_free)
11836 resource_is_free=0;
11849 resource_is_free=1;
11856 Note, mcs51 and ds390 support only an atomic
11857 \begin_inset LatexCommand \index{atomic}
11865 instruction (as opposed to atomic bit test and
11870 Functions using private register banks
11871 \begin_inset LatexCommand \label{sub:Functions-using-private-banks}
11878 Some architectures have support for quickly changing register sets.
11879 SDCC supports this feature with the
11882 \begin_inset LatexCommand \index{using (mcs51, ds390 register bank)}
11887 \begin_inset LatexCommand \index{\_\_using (mcs51, ds390 register bank)}
11893 attribute (which tells the compiler to use a register bank
11894 \begin_inset LatexCommand \index{register bank (mcs51, ds390)}
11898 other than the default bank zero).
11899 It should only be applied to
11902 \begin_inset LatexCommand \index{interrupt}
11908 functions (see footnote below).
11909 This will in most circumstances make the generated ISR code more efficient
11910 since it will not have to save registers on the stack.
11917 attribute will have no effect on the generated code for a
11921 function (but may occasionally be useful anyway
11927 possible exception: if a function is called ONLY from 'interrupt' functions
11928 using a particular bank, it can be declared with the same 'using' attribute
11929 as the calling 'interrupt' functions.
11930 For instance, if you have several ISRs using bank one, and all of them
11931 call memcpy(), it might make sense to create a specialized version of memcpy()
11932 'using 1', since this would prevent the ISR from having to save bank zero
11933 to the stack on entry and switch to bank zero before calling the function
11940 (pending: I don't think this has been done yet)
11947 function using a non-zero bank will assume that it can trash that register
11948 bank, and will not save it.
11949 Since high-priority interrupts
11950 \begin_inset LatexCommand \index{interrupts}
11955 \begin_inset LatexCommand \index{interrupt priority}
11959 can interrupt low-priority ones on the 8051 and friends, this means that
11960 if a high-priority ISR
11964 a particular bank occurs while processing a low-priority ISR
11968 the same bank, terrible and bad things can happen.
11969 To prevent this, no single register bank should be
11973 by both a high priority and a low priority ISR.
11974 This is probably most easily done by having all high priority ISRs use
11975 one bank and all low priority ISRs use another.
11976 If you have an ISR which can change priority at runtime, you're on your
11977 own: I suggest using the default bank zero and taking the small performance
11981 It is most efficient if your ISR calls no other functions.
11982 If your ISR must call other functions, it is most efficient if those functions
11983 use the same bank as the ISR (see note 1 below); the next best is if the
11984 called functions use bank zero.
11985 It is very inefficient to call a function using a different, non-zero bank
11991 \begin_inset LatexCommand \label{sub:Startup-Code}
11996 \begin_inset LatexCommand \index{Startup code}
12003 MCS51/DS390 Startup Code
12006 The compiler inserts a call to the C routine
12008 _sdcc_external_startup()
12009 \begin_inset LatexCommand \index{\_sdcc\_external\_startup()}
12018 at the start of the CODE area.
12019 This routine is in the runtime library
12020 \begin_inset LatexCommand \index{Runtime library}
12025 By default this routine returns 0, if this routine returns a non-zero value,
12026 the static & global variable initialization will be skipped and the function
12027 main will be invoked.
12028 Otherwise static & global variables will be initialized before the function
12032 _sdcc_external_startup()
12034 routine to your program to override the default if you need to setup hardware
12035 or perform some other critical operation prior to static & global variable
12037 \begin_inset LatexCommand \index{Variable initialization}
12042 On some mcs51 variants xdata
12043 \begin_inset LatexCommand \index{xdata (mcs51, ds390 storage class)}
12047 memory has to be explicitly enabled before it can be accessed or if the
12048 watchdog needs to be disabled, this is the place to do it.
12049 The startup code clears all internal data memory, 256 bytes by default,
12050 but from 0 to n-1 if
12063 \begin_inset LatexCommand \index{-\/-iram-size <Value>}
12070 (recommended for Chipcon CC1010).
12073 See also the compiler option
12092 \begin_inset LatexCommand \index{-\/-no-xinit-opt}
12097 \begin_inset LatexCommand \ref{sub:MCS51-variants}
12102 about MCS51-variants.
12108 The HC08 startup code follows the same scheme as the MCS51 startup code.
12114 On the Z80 the startup code is inserted by linking with crt0.o which is generated
12115 from sdcc/device/lib/z80/crt0.s.
12116 If you need a different startup code you can use the compiler option
12137 \begin_inset LatexCommand \index{-\/-no-std-crt0}
12141 and provide your own crt0.o.
12145 Inline Assembler Code
12146 \begin_inset LatexCommand \index{Assembler routines}
12153 A Step by Step Introduction
12154 \begin_inset LatexCommand \label{sub:A-Step-by Assembler Introduction}
12161 Starting from a small snippet of c-code this example shows for the MCS51
12162 how to use inline assembly, access variables, a function parameter and
12163 an array in xdata memory.
12164 The example uses an MCS51 here but is easily adapted for other architectures.
12165 This is a buffer routine which should be optimized:
12172 \begin_inset LatexCommand \index{far (storage class)}
12177 \begin_inset LatexCommand \index{\_\_far (storage class)}
12182 \begin_inset LatexCommand \index{at}
12187 \begin_inset LatexCommand \index{\_\_at}
12192 \begin_inset LatexCommand \index{Aligned array}
12198 unsigned char head,tail;
12202 void to_buffer( unsigned char c )
12210 if( head != tail-1 )
12220 buf[ head++ ] = c;\SpecialChar ~
12224 /* access to a 256 byte aligned array */
12229 If the code snippet (assume it is saved in buffer.c) is compiled with SDCC
12230 then a corresponding buffer.asm file is generated.
12231 We define a new function
12235 in file buffer.c in which we cut and paste the generated code, removing
12236 unwanted comments and some ':'.
12238 \begin_inset Quotes sld
12242 \begin_inset Quotes srd
12246 \begin_inset Quotes sld
12250 \begin_inset Quotes srd
12253 to the beginning and the end of the function body:
12259 /* With a cut and paste from the .asm file, we have something to start with.
12264 The function is not yet OK! (registers aren't saved) */
12266 void to_buffer_asm( unsigned char c )
12275 \begin_inset LatexCommand \index{\_asm}
12280 \begin_inset LatexCommand \index{\_\_asm}
12294 ;buffer.c if( head != tail-1 )
12342 ;buffer.c buf[ head++ ] = c; /* access to a 256 byte aligned array */
12343 \begin_inset LatexCommand \index{Aligned array}
12408 \begin_inset LatexCommand \index{\_endasm}
12413 \begin_inset LatexCommand \index{\_\_endasm}
12422 The new file buffer.c should compile with only one warning about the unreferenced
12423 function argument 'c'.
12424 Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY
12425 (1) and finally have:
12431 unsigned char far at 0x7f00 buf[0x100];
12433 unsigned char head,tail;
12435 #define USE_ASSEMBLY (1)
12443 void to_buffer( unsigned char c )
12451 if( head != tail-1 )
12471 void to_buffer( unsigned char c )
12479 c; // to avoid warning: unreferenced function argument
12486 \begin_inset LatexCommand \index{\_asm}
12491 \begin_inset LatexCommand \index{\_\_asm}
12505 ; save used registers here.
12516 ; If we were still using r2,r3 we would have to push them here.
12519 ; if( head != tail-1 )
12562 ; we could do an ANL a,#0x0f here to use a smaller buffer (see below)
12586 ; buf[ head++ ] = c;
12597 a,dpl \SpecialChar ~
12604 ; dpl holds lower byte of function argument
12615 dpl,_head \SpecialChar ~
12618 ; buf is 0x100 byte aligned so head can be used directly
12660 ; we could do an ANL _head,#0x0f here to use a smaller buffer (see above)
12672 ; restore used registers here
12679 \begin_inset LatexCommand \index{\_endasm}
12684 \begin_inset LatexCommand \index{\_\_endasm}
12695 The inline assembler code can contain any valid code understood by the assembler
12696 , this includes any assembler directives and comment lines
12702 The assembler does not like some characters like ':' or ''' in comments.
12703 You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html
12704 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
12709 \begin_inset LatexCommand \index{Assembler documentation}
12717 The compiler does not do any validation of the code within the
12720 \begin_inset LatexCommand \index{\_asm}
12725 \begin_inset LatexCommand \index{\_\_asm}
12733 \begin_inset LatexCommand \index{\_endasm}
12738 \begin_inset LatexCommand \index{\_\_endasm}
12747 Specifically it will not know which registers are used and thus register
12749 \begin_inset LatexCommand \index{push/pop}
12753 has to be done manually.
12757 It is recommended that each assembly instruction (including labels) be placed
12758 in a separate line (as the example shows).
12772 \begin_inset LatexCommand \index{-\/-peep-asm}
12778 command line option is used, the inline assembler code will be passed through
12779 the peephole optimizer
12780 \begin_inset LatexCommand \index{Peephole optimizer}
12785 There are only a few (if any) cases where this option makes sense, it might
12786 cause some unexpected changes in the inline assembler code.
12787 Please go through the peephole optimizer rules defined in file
12791 before using this option.
12795 \begin_inset LatexCommand \label{sub:Naked-Functions}
12800 \begin_inset LatexCommand \index{Naked functions}
12807 A special keyword may be associated with a function declaring it as
12810 \begin_inset LatexCommand \index{\_naked}
12815 \begin_inset LatexCommand \index{\_\_naked}
12826 function modifier attribute prevents the compiler from generating prologue
12827 \begin_inset LatexCommand \index{function prologue}
12832 \begin_inset LatexCommand \index{function epilogue}
12836 code for that function.
12837 This means that the user is entirely responsible for such things as saving
12838 any registers that may need to be preserved, selecting the proper register
12839 bank, generating the
12843 instruction at the end, etc.
12844 Practically, this means that the contents of the function must be written
12845 in inline assembler.
12846 This is particularly useful for interrupt functions, which can have a large
12847 (and often unnecessary) prologue/epilogue.
12848 For example, compare the code generated by these two functions:
12854 \begin_inset LatexCommand \index{volatile}
12858 data unsigned char counter;
12862 void simpleInterrupt(void) interrupt
12863 \begin_inset LatexCommand \index{interrupt}
12868 \begin_inset LatexCommand \index{\_\_interrupt}
12886 void nakedInterrupt(void) interrupt 2 _naked
12895 \begin_inset LatexCommand \index{\_asm}
12900 \begin_inset LatexCommand \index{\_\_asm}
12917 _counter ; does not change flags, no need to save psw
12929 ; MUST explicitly include ret or reti in _naked function.
12936 \begin_inset LatexCommand \index{\_endasm}
12941 \begin_inset LatexCommand \index{\_\_endasm}
12950 For an 8051 target, the generated simpleInterrupt looks like:
13091 whereas nakedInterrupt looks like:
13106 _counter ; does not change flags, no need to save psw
13124 ; MUST explicitly include ret or reti in _naked function
13127 The related directive #pragma exclude
13128 \begin_inset LatexCommand \index{\#pragma exclude}
13132 allows a more fine grained control over pushing & popping
13133 \begin_inset LatexCommand \index{push/pop}
13140 While there is nothing preventing you from writing C code inside a
13144 function, there are many ways to shoot yourself in the foot doing this,
13145 and it is recommended that you stick to inline assembler.
13148 Use of Labels within Inline Assembler
13151 SDCC allows the use of in-line assembler with a few restrictions regarding
13153 In older versions of the compiler all labels defined within inline assembler
13162 where nnnn is a number less than 100 (which implies a limit of utmost 100
13163 inline assembler labels
13177 \begin_inset LatexCommand \index{\_asm}
13182 \begin_inset LatexCommand \index{\_\_asm}
13212 \begin_inset LatexCommand \index{\_endasm}
13217 \begin_inset LatexCommand \index{\_\_endasm}
13224 Inline assembler code cannot reference any C-Labels, however it can reference
13226 \begin_inset LatexCommand \index{Labels}
13230 defined by the inline assembler, e.g.:
13255 ; some assembler code
13275 /* some more c code */
13277 clabel:\SpecialChar ~
13279 /* inline assembler cannot reference this label */
13291 $0003: ;label (can be referenced by inline assembler only)
13298 \begin_inset LatexCommand \index{\_endasm}
13303 \begin_inset LatexCommand \index{\_\_endasm}
13313 /* some more c code */
13318 In other words inline assembly code can access labels defined in inline
13319 assembly within the scope of the function.
13320 The same goes the other way, i.e.
13321 labels defines in inline assembly can not be accessed by C statements.
13324 Interfacing with Assembler Code
13325 \begin_inset LatexCommand \index{Assembler routines}
13332 Global Registers used for Parameter Passing
13333 \begin_inset LatexCommand \index{Parameter passing}
13340 The compiler always uses the global registers
13343 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13348 \begin_inset LatexCommand \index{DPTR}
13353 \begin_inset LatexCommand \index{B (mcs51, ds390 register)}
13362 \begin_inset LatexCommand \index{ACC (mcs51, ds390 register)}
13368 to pass the first parameter to a routine.
13369 The second parameter onwards is either allocated on the stack (for reentrant
13380 -stack-auto is used) or in data / xdata memory (depending on the memory
13385 Assembler Routine (non-reentrant)
13388 In the following example
13389 \begin_inset LatexCommand \index{reentrant}
13394 \begin_inset LatexCommand \index{Assembler routines (non-reentrant)}
13398 the function c_func calls an assembler routine asm_func, which takes two
13400 \begin_inset LatexCommand \index{function parameter}
13409 extern int asm_func(unsigned char, unsigned char);
13413 int c_func (unsigned char i, unsigned char j)
13421 return asm_func(i,j);
13435 return c_func(10,9);
13440 The corresponding assembler function is:
13445 .globl _asm_func_PARM_2
13546 \begin_inset LatexCommand \index{DPTR, DPH, DPL}
13563 Note here that the return values
13564 \begin_inset LatexCommand \index{return value}
13568 are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for
13570 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','
13571 b' & 'acc' for four byte values.
13574 The parameter naming convention is _<function_name>_PARM_<n>, where n is
13575 the parameter number starting from 1, and counting from the left.
13576 The first parameter is passed in
13577 \begin_inset Quotes eld
13581 \begin_inset Quotes erd
13584 for a one byte parameter,
13585 \begin_inset Quotes eld
13589 \begin_inset Quotes erd
13593 \begin_inset Quotes eld
13597 \begin_inset Quotes erd
13600 for three bytes and
13601 \begin_inset Quotes eld
13605 \begin_inset Quotes erd
13608 for a four bytes parameter.
13609 The variable name for the second parameter will be _<function_name>_PARM_2.
13613 Assemble the assembler routine with the following command:
13620 asx8051 -losg asmfunc.asm
13627 Then compile and link the assembler routine to the C source file with the
13635 sdcc cfunc.c asmfunc.rel
13638 Assembler Routine (reentrant)
13642 \begin_inset LatexCommand \index{reentrant}
13647 \begin_inset LatexCommand \index{Assembler routines (reentrant)}
13651 the second parameter
13652 \begin_inset LatexCommand \index{function parameter}
13656 onwards will be passed on the stack, the parameters are pushed from right
13658 after the call the leftmost parameter will be on the top of the stack.
13659 Here is an example:
13664 extern int asm_func(unsigned char, unsigned char);
13668 int c_func (unsigned char i, unsigned char j) reentrant
13676 return asm_func(i,j);
13690 return c_func(10,9);
13695 The corresponding assembler routine is:
13795 The compiling and linking procedure remains the same, however note the extra
13796 entry & exit linkage required for the assembler code, _bp is the stack
13797 frame pointer and is used to compute the offset into the stack for parameters
13798 and local variables.
13802 \begin_inset LatexCommand \index{int (16 bit)}
13807 \begin_inset LatexCommand \index{long (32 bit)}
13814 For signed & unsigned int (16 bit) and long (32 bit) variables, division,
13815 multiplication and modulus operations are implemented by support routines.
13816 These support routines are all developed in ANSI-C to facilitate porting
13817 to other MCUs, although some model specific assembler optimizations are
13819 The following files contain the described routines, all of them can be
13820 found in <installdir>/share/sdcc/lib.
13826 \begin_inset Tabular
13827 <lyxtabular version="3" rows="11" columns="2">
13829 <column alignment="center" valignment="top" leftline="true" width="0">
13830 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
13831 <row topline="true" bottomline="true">
13832 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13842 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13853 <row topline="true">
13854 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13862 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13867 16 bit multiplication
13871 <row topline="true">
13872 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13880 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13885 signed 16 bit division (calls _divuint)
13889 <row topline="true">
13890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13898 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13903 unsigned 16 bit division
13907 <row topline="true">
13908 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13916 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13921 signed 16 bit modulus (calls _moduint)
13925 <row topline="true">
13926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13934 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13939 unsigned 16 bit modulus
13943 <row topline="true">
13944 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13952 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13957 32 bit multiplication
13961 <row topline="true">
13962 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13970 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13975 signed 32 division (calls _divulong)
13979 <row topline="true">
13980 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
13988 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
13993 unsigned 32 division
13997 <row topline="true">
13998 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14006 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14011 signed 32 bit modulus (calls _modulong)
14015 <row topline="true" bottomline="true">
14016 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14024 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14029 unsigned 32 bit modulus
14042 Since they are compiled as
14047 \begin_inset LatexCommand \index{reentrant}
14052 \begin_inset LatexCommand \index{interrupt}
14056 service routines should not do any of the above operations.
14057 If this is unavoidable then the above routines will need to be compiled
14071 \begin_inset LatexCommand \index{-\/-stack-auto}
14077 option, after which the source program will have to be compiled with
14090 \begin_inset LatexCommand \index{-\/-int-long-reent}
14097 Notice that you don't have to call these routines directly.
14098 The compiler will use them automatically every time an integer operation
14102 Floating Point Support
14103 \begin_inset LatexCommand \index{Floating point support}
14110 SDCC supports IEEE (single precision 4 bytes) floating point numbers.The
14111 floating point support routines are derived from gcc's floatlib.c and consist
14112 of the following routines:
14120 \begin_inset Tabular
14121 <lyxtabular version="3" rows="17" columns="2">
14123 <column alignment="center" valignment="top" leftline="true" width="0">
14124 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
14125 <row topline="true" bottomline="true">
14126 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14143 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14152 <row topline="true">
14153 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14170 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14184 add floating point numbers
14188 <row topline="true">
14189 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14206 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14220 subtract floating point numbers
14224 <row topline="true">
14225 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14242 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14256 divide floating point numbers
14260 <row topline="true">
14261 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14278 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14292 multiply floating point numbers
14296 <row topline="true">
14297 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14314 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14328 convert floating point to unsigned char
14332 <row topline="true">
14333 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14350 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14364 convert floating point to signed char
14368 <row topline="true">
14369 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14386 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14400 convert floating point to unsigned int
14404 <row topline="true">
14405 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14422 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14436 convert floating point to signed int
14440 <row topline="true">
14441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14481 convert floating point to unsigned long
14485 <row topline="true">
14486 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14517 convert floating point to signed long
14521 <row topline="true">
14522 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14553 convert unsigned char to floating point
14557 <row topline="true">
14558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14575 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14589 convert char to floating point number
14593 <row topline="true">
14594 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14611 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14625 convert unsigned int to floating point
14629 <row topline="true">
14630 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14647 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14661 convert int to floating point numbers
14665 <row topline="true">
14666 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14683 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14697 convert unsigned long to floating point number
14701 <row topline="true" bottomline="true">
14702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
14719 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
14733 convert long to floating point number
14746 These support routines are developed in ANSI-C so there is room for space
14747 and speed improvement
14753 The floating point routines for the mcs51 are implemented in assembler
14757 Note if all these routines are used simultaneously the data space might
14759 For serious floating point usage the large model might be needed.
14760 Also notice that you don't have to call this routines directly.
14761 The compiler will use them automatically every time a floating point operation
14766 \begin_inset LatexCommand \index{Libraries}
14775 <pending: this is messy and incomplete - a little more information is in
14776 sdcc/doc/libdoc.txt
14781 Compiler support routines (_gptrget, _mulint etc.)
14784 Stdclib functions (puts, printf, strcat etc.)
14785 \layout Subsubsection
14791 \begin_inset LatexCommand \index{<stdio.h>}
14795 As usual on embedded systems you have to provide your own
14798 \begin_inset LatexCommand \index{getchar()}
14807 \begin_inset LatexCommand \index{putchar()}
14814 SDCC does not know whether the system connects to a serial line with or
14815 without handshake, LCD, keyboard or other device.
14816 You'll find examples for serial routines f.e.
14817 in sdcc/device/lib.
14823 \begin_inset LatexCommand \index{printf()}
14833 does not support float (except on ds390).
14834 To enable this recompile it with the option
14847 \begin_inset LatexCommand \index{USE\_FLOATS}
14853 on the command line.
14867 \begin_inset LatexCommand \index{-\/-model-large}
14873 for the mcs51 port, since this uses a lot of memory.
14876 If you're short on memory you might want to use
14879 \begin_inset LatexCommand \index{printf\_small()}
14894 For the mcs51 there additionally are assembly versions
14897 \begin_inset LatexCommand \index{printf\_tiny() (mcs51)}
14906 \begin_inset LatexCommand \index{printf\_fast() (mcs51)}
14915 \begin_inset LatexCommand \index{printf\_fast\_f() (mcs51)}
14921 which should fit the requirements of many embedded systems (printf_fast()
14922 can be customized by unsetting #defines to
14926 support long variables and field widths).
14929 Math functions (sin, pow, sqrt etc.)
14936 \begin_inset LatexCommand \index{Libraries}
14940 included in SDCC should have a license at least as liberal as the GNU Lesser
14941 General Public License
14942 \begin_inset LatexCommand \index{GNU Lesser General Public License, LGPL}
14953 license statements for the libraries are missing.
14954 sdcc/device/lib/ser_ir.c
14958 come with a GPL (as opposed to LGPL) License - this will not be liberal
14959 enough for many embedded programmers.
14962 If you have ported some library or want to share experience about some code
14964 falls into any of these categories Busses (I
14965 \begin_inset Formula $^{\textrm{2}}$
14968 C, CAN, Ethernet, Profibus, Modbus, USB, SPI, JTAG ...), Media (IDE, Memory
14969 cards, eeprom, flash...), En-/Decryption, Remote debugging, Realtime kernel,
14970 Keyboard, LCD, RTC, FPGA, PID then the sdcc-user mailing list
14971 \begin_inset LatexCommand \url{http://sourceforge.net/mail/?group_id=599}
14976 would certainly like to hear about it.
14977 Programmers coding for embedded systems are not especially famous for being
14978 enthusiastic, so don't expect a big hurray but as the mailing list is searchabl
14979 e these references are very valuable.
14980 Let's help to create a climate where information is shared.
14986 MCS51 Memory Models
14987 \begin_inset LatexCommand \index{Memory model}
14992 \begin_inset LatexCommand \index{MCS51 memory model}
14997 \layout Subsubsection
14999 Small, Medium and Large
15002 SDCC allows three memory models for MCS51 code,
15011 Modules compiled with different memory models should
15015 be combined together or the results would be unpredictable.
15016 The library routines supplied with the compiler are compiled as small,
15018 The compiled library modules are contained in separate directories as small,
15019 medium and large so that you can link to the appropriate set.
15022 When the medium or large model is used all variables declared without a
15023 storage class will be allocated into the external ram, this includes all
15024 parameters and local variables (for non-reentrant
15025 \begin_inset LatexCommand \index{reentrant}
15030 When the small model is used variables without storage class are allocated
15031 in the internal ram.
15034 Judicious usage of the processor specific storage classes
15035 \begin_inset LatexCommand \index{Storage class}
15039 and the 'reentrant' function type will yield much more efficient code,
15040 than using the large model.
15041 Several optimizations are disabled when the program is compiled using the
15042 large model, it is therefore recommended that the small model be used unless
15043 absolutely required.
15044 \layout Subsubsection
15047 \begin_inset LatexCommand \label{sub:External-Stack}
15052 \begin_inset LatexCommand \index{stack}
15057 \begin_inset LatexCommand \index{External stack (mcs51)}
15064 The external stack (-
15075 \begin_inset LatexCommand \index{-\/-xstack}
15079 ) is located in pdata
15080 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
15084 memory (usually at the start of the external ram segment) and uses all
15085 unused space in pdata (max.
15097 -xstack option is used to compile the program, the parameters and local
15099 \begin_inset LatexCommand \index{local variables}
15103 of all reentrant functions are allocated in this area.
15104 This option is provided for programs with large stack space requirements.
15105 When used with the -
15116 \begin_inset LatexCommand \index{-\/-stack-auto}
15120 option, all parameters and local variables are allocated on the external
15121 stack (note: support libraries will need to be recompiled with the same
15123 There is a predefined target in the library makefile).
15126 The compiler outputs the higher order address byte of the external ram segment
15128 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
15133 \begin_inset LatexCommand \ref{sub:MCS51-variants}
15137 ), therefore when using the External Stack option, this port
15141 be used by the application program.
15145 \begin_inset LatexCommand \index{Memory model}
15150 \begin_inset LatexCommand \index{DS390 memory model}
15157 The only model supported is Flat 24
15158 \begin_inset LatexCommand \index{Flat 24 (DS390 memory model)}
15163 This generates code for the 24 bit contiguous addressing mode of the Dallas
15165 In this mode, up to four meg of external RAM or code space can be directly
15167 See the data sheets at www.dalsemi.com for further information on this part.
15171 Note that the compiler does not generate any code to place the processor
15172 into 24 bitmode (although
15176 in the ds390 libraries will do that for you).
15182 \begin_inset LatexCommand \index{Tinibios (DS390)}
15186 , the boot loader or similar code must ensure that the processor is in 24
15187 bit contiguous addressing mode before calling the SDCC startup code.
15205 option, variables will by default be placed into the XDATA segment.
15210 Segments may be placed anywhere in the 4 meg address space using the usual
15222 Note that if any segments are located above 64K, the -r flag must be passed
15223 to the linker to generate the proper segment relocations, and the Intel
15224 HEX output format must be used.
15225 The -r flag can be passed to the linker by using the option
15229 on the SDCC command line.
15230 However, currently the linker can not handle code segments > 64k.
15234 \begin_inset LatexCommand \index{Pragmas}
15241 SDCC supports the following #pragma directives:
15245 \begin_inset LatexCommand \index{\#pragma save}
15249 - this will save all current options to the save/restore stack.
15250 See #pragma\SpecialChar ~
15255 \begin_inset LatexCommand \index{\#pragma restore}
15259 - will restore saved options from the last save.
15260 saves & restores can be nested.
15261 SDCC uses a save/restore stack: save pushes current options to the stack,
15262 restore pulls current options from the stack.
15263 See #pragma\SpecialChar ~
15270 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15275 \begin_inset LatexCommand \index{function prologue}
15279 function1[,function2[,function3...]] - The compiler by default uses a caller
15280 saves convention for register saving across function calls, however this
15281 can cause unnecessary register pushing & popping
15282 \begin_inset LatexCommand \index{push/pop}
15286 when calling small functions from larger functions.
15287 This option can be used to switch off the register saving convention for
15288 the function names specified.
15289 The compiler will not save registers when calling these functions, extra
15290 code need to be manually inserted at the entry & exit for these functions
15291 to save & restore the registers used by these functions, this can SUBSTANTIALLY
15292 reduce code & improve run time performance of the generated code.
15293 In the future the compiler (with inter procedural analysis) may be able
15294 to determine the appropriate scheme to use for each function call.
15305 -callee-saves command line option is used, the function names specified
15306 in #pragma\SpecialChar ~
15308 \begin_inset LatexCommand \index{\#pragma callee\_saves}
15312 is appended to the list of functions specified in the command line.
15316 \begin_inset LatexCommand \index{\#pragma exclude}
15320 none | {acc[,b[,dpl[,dph]]] - The exclude pragma disables the generation
15321 of pairs of push/pop
15322 \begin_inset LatexCommand \index{push/pop}
15331 \begin_inset LatexCommand \index{interrupt}
15344 The directive should be placed immediately before the ISR function definition
15345 and it affects ALL ISR functions following it.
15346 To enable the normal register saving for ISR functions use #pragma\SpecialChar ~
15347 exclude\SpecialChar ~
15349 \begin_inset LatexCommand \index{\#pragma exclude}
15354 See also the related keyword _naked
15355 \begin_inset LatexCommand \index{\_naked}
15360 \begin_inset LatexCommand \index{\_\_naked}
15368 \begin_inset LatexCommand \index{\#pragma less\_pedantic}
15372 - the compiler will not warn you anymore for obvious mistakes, you'r on
15376 disable_warning <nnnn>
15377 \begin_inset LatexCommand \index{\#pragma disable\_warning}
15381 - the compiler will not warn you anymore about warning number <nnnn>.
15385 \begin_inset LatexCommand \index{\#pragma nogcse}
15389 - will stop global common subexpression elimination.
15393 \begin_inset LatexCommand \index{\#pragma noinduction}
15397 - will stop loop induction optimizations.
15401 \begin_inset LatexCommand \index{\#pragma noinvariant}
15405 - will not do loop invariant optimizations.
15406 For more details see Loop Invariants in section
15407 \begin_inset LatexCommand \ref{sub:Loop-Optimizations}
15415 \begin_inset LatexCommand \index{\#pragma noiv}
15419 - Do not generate interrupt
15420 \begin_inset LatexCommand \index{interrupt}
15424 vector table entries for all ISR functions defined after the pragma.
15425 This is useful in cases where the interrupt vector table must be defined
15426 manually, or when there is a secondary, manually defined interrupt vector
15428 for the autovector feature of the Cypress EZ-USB FX2).
15429 More elegantly this can be achieved by obmitting the optional interrupt
15430 number after the interrupt keyword, see section
15431 \begin_inset LatexCommand \ref{sub:Interrupt-Service-Routines}
15440 \begin_inset LatexCommand \index{\#pragma nojtbound}
15444 - will not generate code for boundary value checking, when switch statements
15445 are turned into jump-tables (dangerous).
15446 For more details see section
15447 \begin_inset LatexCommand \ref{sub:'switch'-Statements}
15455 \begin_inset LatexCommand \index{\#pragma noloopreverse}
15459 - Will not do loop reversal optimization
15463 \begin_inset LatexCommand \index{\#pragma nooverlay}
15467 - the compiler will not overlay the parameters and local variables of a
15472 \begin_inset LatexCommand \index{\#pragma stackauto}
15487 \begin_inset LatexCommand \index{-\/-stack-auto}
15492 \begin_inset LatexCommand \ref{sec:Parameters-and-Local-Variables}
15496 Parameters and Local Variables.
15500 \begin_inset LatexCommand \index{\#pragma opt\_code\_speed}
15504 - The compiler will optimize code generation towards fast code, possibly
15505 at the expense of code size.
15509 \begin_inset LatexCommand \index{\#pragma opt\_code\_size}
15513 - The compiler will optimize code generation towards compact code, possibly
15514 at the expense of code speed.
15518 \begin_inset LatexCommand \index{\#pragma opt\_code\_balanced}
15522 - The compiler will attempt to generate code that is both compact and fast,
15523 as long as meeting one goal is not a detriment to the other (this is the
15529 \begin_inset LatexCommand \index{\#pragma std\_sdcc89}
15533 - Generally follow the C89 standard, but allow SDCC features that conflict
15534 with the standard (default).
15538 \begin_inset LatexCommand \index{\#pragma std\_c89}
15542 - Follow the C89 standard and disable SDCC features that conflict with the
15547 \begin_inset LatexCommand \index{\#pragma std\_sdcc99}
15551 - Generally follow the C99 standard, but allow SDCC features that conflict
15552 with the standard (incomplete support).
15556 \begin_inset LatexCommand \index{\#pragma std\_c99}
15560 - Follow the C99 standard and disable SDCC features that conflict with the
15561 standard (incomplete support).
15565 \begin_inset LatexCommand \index{\#pragma codeseg}
15569 - Use this name (max.
15570 8 characters) for the code segment.
15574 \begin_inset LatexCommand \index{\#pragma constseg}
15578 - Use this name (max.
15579 8 characters) for the const segment.
15582 SDCPP supports the following #pragma directives:
15586 \begin_inset LatexCommand \index{\#pragma preproc\_asm}
15590 (+ | -) - switch _asm _endasm block preprocessing on / off.
15594 The pragma's are intended to be used to turn-on or off certain optimizations
15595 which might cause the compiler to generate extra stack / data space to
15596 store compiler generated temporary variables.
15597 This usually happens in large functions.
15598 Pragma directives should be used as shown in the following example, they
15599 are used to control options & optimizations for a given function; pragmas
15600 should be placed before and/or after a function, placing pragma's inside
15601 a function body could have unpredictable results.
15607 \begin_inset LatexCommand \index{\#pragma save}
15618 /* save the current settings */
15621 \begin_inset LatexCommand \index{\#pragma nogcse}
15630 /* turnoff global subexpression elimination */
15632 #pragma noinduction
15633 \begin_inset LatexCommand \index{\#pragma noinduction}
15637 /* turn off induction optimizations */
15660 \begin_inset LatexCommand \index{\#pragma restore}
15664 /* turn the optimizations back on */
15667 The compiler will generate a warning message when extra space is allocated.
15668 It is strongly recommended that the save and restore pragma's be used when
15669 changing options for a function.
15672 Defines Created by the Compiler
15675 The compiler creates the following #defines
15676 \begin_inset LatexCommand \index{\#defines}
15681 \begin_inset LatexCommand \index{Defines created by the compiler}
15691 \begin_inset Tabular
15692 <lyxtabular version="3" rows="11" columns="2">
15694 <column alignment="center" valignment="top" leftline="true" width="0">
15695 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
15696 <row topline="true" bottomline="true">
15697 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15718 <row topline="true">
15719 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15725 \begin_inset LatexCommand \index{SDCC}
15732 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15737 this Symbol is always defined.
15738 Since version 2.5.6 it's this version number as an int (ex.
15743 <row topline="true">
15744 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15750 \begin_inset LatexCommand \index{SDCC\_mcs51}
15755 \begin_inset LatexCommand \index{SDCC\_ds390}
15760 \begin_inset LatexCommand \index{SDCC\_z80}
15767 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15772 depending on the model used (e.g.: -mds390
15776 <row topline="true">
15777 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15783 \begin_inset LatexCommand \index{\_\_mcs51}
15788 \begin_inset LatexCommand \index{\_\_ds390}
15793 \begin_inset LatexCommand \index{\_\_hc08}
15798 \begin_inset LatexCommand \index{\_\_z80}
15805 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15810 depending on the model used (e.g.
15815 <row topline="true">
15816 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15822 \begin_inset LatexCommand \index{SDCC\_STACK\_AUTO}
15829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15852 <row topline="true">
15853 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15859 \begin_inset LatexCommand \index{SDCC\_MODEL\_SMALL}
15866 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15889 <row topline="true">
15890 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15896 \begin_inset LatexCommand \index{SDCC\_MODEL\_MEDIUM}
15903 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15926 <row topline="true">
15927 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15933 \begin_inset LatexCommand \index{SDCC\_MODEL\_LARGE}
15940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
15963 <row topline="true">
15964 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
15970 \begin_inset LatexCommand \index{SDCC\_USE\_XSTACK}
15977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16000 <row topline="true">
16001 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16007 \begin_inset LatexCommand \index{SDCC\_STACK\_TENBIT}
16014 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16027 <row topline="true" bottomline="true">
16028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16034 \begin_inset LatexCommand \index{SDCC\_MODEL\_FLAT24}
16041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16061 Notes on supported Processors
16065 \begin_inset LatexCommand \label{sub:MCS51-variants}
16070 \begin_inset LatexCommand \index{MCS51 variants}
16077 MCS51 processors are available from many vendors and come in many different
16079 While they might differ considerably in respect to Special Function Registers
16080 the core MCS51 is usually not modified or is kept compatible.
16084 pdata access by SFR
16087 With the upcome of devices with internal xdata and flash memory devices
16089 \begin_inset LatexCommand \index{P2 (mcs51 sfr)}
16093 as dedicated I/O port is becoming more popular.
16094 Switching the high byte for pdata
16095 \begin_inset LatexCommand \index{pdata (mcs51, ds390 storage class)}
16099 access which was formerly done by port P2 is then achieved by a Special
16101 \begin_inset LatexCommand \index{sfr}
16106 In well-established MCS51 tradition the address of this
16110 is where the chip designers decided to put it.
16111 Needless to say that they didn't agree on a common name either.
16112 So that the startup code can correctly initialize xdata variables, you
16113 should define an sfr with the name _XPAGE
16116 \begin_inset LatexCommand \index{\_XPAGE (mcs51)}
16122 at the appropriate location if the default, port P2, is not used for this.
16128 sfr at 0x92 _XPAGE; /* Cypress EZ-USB family */
16133 sfr at 0xaf _XPAGE; /* some Silicon Labs (Cygnal) chips */
16138 sfr at 0xaa _XPAGE; /* some Silicon Labs (Cygnal) chips */
16141 For more exotic implementations further customizations may be needed.
16143 \begin_inset LatexCommand \ref{sub:Startup-Code}
16147 for other possibilities.
16150 Other Features available by SFR
16153 Some MCS51 variants offer features like Double DPTR
16154 \begin_inset LatexCommand \index{DPTR}
16158 , multiple DPTR, decrementing DPTR, 16x16 Multiply.
16159 These are currently not used for the MCS51 port.
16160 If you absolutely need them you can fall back to inline assembly or submit
16167 The DS80C400 microcontroller has a rich set of peripherals.
16168 In its built-in ROM library it includes functions to access some of the
16169 features, among them is a TCP stack with IP4 and IP6 support.
16170 Library headers (currently in beta status) and other files are provided
16174 \begin_inset LatexCommand \url{ftp://ftp.dalsemi.com/pub/tini/ds80c400/c_libraries/sdcc/index.html}
16182 The Z80 and gbz80 port
16185 SDCC can target both the Zilog
16186 \begin_inset LatexCommand \index{Z80}
16190 and the Nintendo Gameboy's Z80-like gbz80
16191 \begin_inset LatexCommand \index{gbz80 (GameBoy Z80)}
16196 The Z80 port is passed through the same
16199 \begin_inset LatexCommand \index{Regression test}
16205 as the MCS51 and DS390 ports, so floating point support, support for long
16206 variables and bitfield support is fine.
16207 See mailing lists and forums about interrupt routines.
16210 As always, the code is the authoritative reference - see z80/ralloc.c and
16213 \begin_inset LatexCommand \index{stack}
16217 frame is similar to that generated by the IAR Z80 compiler.
16218 IX is used as the base pointer, HL and IY are used as a temporary registers,
16219 and BC and DE are available for holding variables.
16221 \begin_inset LatexCommand \index{return value}
16225 for the Z80 port are stored in L (one byte), HL (two bytes), or DEHL (four
16227 The gbz80 port use the same set of registers for the return values, but
16228 in a different order of significance: E (one byte), DE (two bytes), or
16235 The port to the Motorola HC08
16236 \begin_inset LatexCommand \index{HC08}
16240 family has been added in October 2003, and is still undergoing some basic
16242 The code generator is complete, but the register allocation is still quite
16244 Some of the SDCC's standard C library functions have embedded non-HC08
16245 inline assembly and so are not yet usable.
16256 \begin_inset LatexCommand \index{PIC14}
16260 port still requires a major effort from the development community.
16261 However it can work for very simple code.
16264 C code and 14bit PIC code page
16265 \begin_inset LatexCommand \index{code page (pic14)}
16270 \begin_inset LatexCommand \index{RAM bank (pic14)}
16277 The linker organizes allocation for the code page and RAM banks.
16278 It does not have intimate knowledge of the code flow.
16279 It will put all the code section of a single asm file into a single code
16281 In order to make use of multiple code pages, separate asm files must be
16283 The compiler treats all functions of a single C file as being in the same
16284 code page unless it is non static.
16285 The compiler treats all local variables of a single C file as being in
16286 the same RAM bank unless it is an extern.
16290 To get the best follow these guide lines:
16293 make local functions static, as non static functions require code page selection
16297 Make local variables static as extern variables require RAM bank selection
16301 For devices that have multiple code pages it is more efficient to use the
16302 same number of files as pages, i.e.
16303 for the 16F877 use 4 separate files and i.e.
16304 for the 16F874 use 2 separate files.
16305 This way the linker can put the code for each file into different code
16306 pages and the compiler can allocate reusable variables more efficiently
16307 and there's less page selection overhead.
16308 And as for any 8 bit micro (especially for PIC 14 as they have a very simple
16309 instruction set) use 'unsigned char' whereever possible instead of 'int'.
16312 Creating a device include file
16315 For generating a device include file use the support perl script inc2h.pl
16316 kept in directory support/script.
16322 For the interrupt function, use the keyword 'interrupt'
16323 \begin_inset LatexCommand \index{interrupt}
16327 with level number of 0 (PIC14 only has 1 interrupt so this number is only
16328 there to avoid a syntax error - it ought to be fixed).
16334 void Intr(void) interrupt 0
16340 T0IF = 0; /* Clear timer interrupt */
16345 Linking and assembling
16348 For assembling you can use either GPUTILS'
16349 \begin_inset LatexCommand \index{gputils (pic tools)}
16353 gpasm.exe or MPLAB's mpasmwin.exe.
16354 GPUTILS is available from
16355 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
16360 For linking you can use either GPUTIL's gplink or MPLAB's mplink.exe.
16361 If you use MPLAB and an interrupt function then the linker script file
16362 vectors section will need to be enlarged to link with mplink.
16385 sdcc -S -V -mpic14 -p16F877 $<
16399 $(PRJ).hex: $(OBJS)
16409 gplink -m -s $(PRJ).lkr -o $(PRJ).hex $(OBJS) libsdcc.lib
16431 sdcc -S -V -mpic14 -p16F877 $<
16441 mpasmwin /q /o $*.asm
16445 $(PRJ).hex: $(OBJS)
16455 mplink /v $(PRJ).lkr /m $(PRJ).map /o $(PRJ).hex $(OBJS) libsdcc.lib
16458 Please note that indentations within a
16462 have to be done with a tabulator character.
16465 Command-line options
16468 Besides the switches common to all SDCC backends, the PIC14 port accepts
16469 the following options (for an updated list see sdcc -
16481 \labelwidthstring 00.00.0000
16493 -debug-extra emit debug info in assembly output
16495 \labelwidthstring 00.00.0000
16507 -no-pcode-opt disable (slightly faulty) optimization on pCode
16511 \layout Subsubsection
16513 error: missing definition for symbol
16514 \begin_inset Quotes sld
16518 \begin_inset Quotes srd
16524 The PIC14 port uses library routines to provide more complex operations
16525 like multiplication, division/modulus and (generic) pointer dereferencing.
16526 In order to add these routines to your project, you must link with PIC14's
16532 For single source file projects this is done automatically, more complex
16537 to the linker's arguments.
16538 Make sure you also add an include path for the library (using the -I switch
16540 \layout Subsubsection
16542 Processor mismatch in file
16543 \begin_inset Quotes sld
16547 \begin_inset Quotes srd
16553 This warning can usually be ignored due to the very good compatibility amongst
16554 14 bit PIC devices.
16557 You might also consider recompiling the library for your specific device
16558 by changing the ARCH=p16f877 (default target) entry in
16560 device/lib/pic/Makefile.in
16564 device/lib/pic/Makefile
16566 to reflect your device.
16567 This might even improve performance for smaller devices as unneccesary
16568 BANKSELs migth be removed.
16572 \layout Subsubsection
16577 Currently, data can only be initialized if it resides in the source file
16583 Data in other source files will silently
16591 \begin_inset LatexCommand \index{PIC16}
16599 \begin_inset LatexCommand \index{PIC16}
16603 port is the portion of SDCC that is responsible to produce code for the
16605 \begin_inset LatexCommand \index{Microchip}
16609 (TM) microcontrollers with 16 bit core.
16610 Currently this family of microcontrollers contains the PIC18Fxxx and PIC18Fxxxx.
16611 Currently supported devices are:
16615 \begin_inset Tabular
16616 <lyxtabular version="3" rows="4" columns="6">
16618 <column alignment="center" valignment="top" leftline="true" width="0">
16619 <column alignment="center" valignment="top" leftline="true" width="0">
16620 <column alignment="center" valignment="top" leftline="true" width="0">
16621 <column alignment="center" valignment="top" leftline="true" width="0">
16622 <column alignment="center" valignment="top" leftline="true" width="0">
16623 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
16624 <row topline="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">
16657 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16665 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16674 <row topline="true">
16675 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16683 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16691 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16699 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16707 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16715 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16724 <row topline="true">
16725 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16741 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16749 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16757 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16765 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16774 <row topline="true" bottomline="true">
16775 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16783 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16791 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
16799 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16806 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16813 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
16831 PIC16 port supports the standard command line arguments as supposed, with
16832 the exception of certain cases that will be mentioned in the following
16835 \labelwidthstring 00.00.0000
16847 -callee-saves See -
16859 \labelwidthstring 00.00.0000
16871 -all-callee-saves All function arguments are passed on stack by default.
16874 There is no need to specify this in the command line.
16876 \labelwidthstring 00.00.0000
16888 -fommit-frame-pointer Frame pointer will be omitted when the function uses
16889 no local variables.
16892 Port Specific Options
16893 \begin_inset LatexCommand \index{Options PIC16}
16900 The port specific options appear after the global options in the sdcc --help
16902 \layout Subsubsection
16907 General options enable certain port features and optimizations.
16909 \labelwidthstring 00.00.0000
16921 -stack-model=[model] Used in conjuction with the command above.
16922 Defines the stack model to be used, valid stack models are :
16925 \labelwidthstring 00.00.0000
16931 Selects small stack model.
16932 8 bit stack and frame pointers.
16933 Supports 256 bytes stack size.
16935 \labelwidthstring 00.00.0000
16941 Selects large stack model.
16942 16 bit stack and frame pointers.
16943 Supports 65536 bytes stack size.
16946 \labelwidthstring 00.00.0000
16958 -preplace-udata-with=[kword] Replaces the default udata keyword for allocating
16959 unitialized data variables with [kword].
16960 Valid keywords are: "udata_acs", "udata_shr", "udata_ovr".
16962 \labelwidthstring 00.00.0000
16974 -ivt-loc <nnnn> positions the Interrupt Vector Table at location <nnnn>.
16975 Useful for bootloaders.
16977 \labelwidthstring 00.00.0000
16989 -asm= sets the full path and name of an external assembler to call.
16991 \labelwidthstring 00.00.0000
17003 -link= sets the full path and name of an external linker to call.
17005 \labelwidthstring 00.00.0000
17017 -mplab-comp MPLAB compatibility option.
17018 Currently only suppresses special gpasm directives.
17019 \layout Subsubsection
17021 Optimization Options
17023 \labelwidthstring 00.00.0000
17035 -optimize-goto Try to use (conditional) BRA instead of GOTO
17037 \labelwidthstring 00.00.0000
17049 -optimize-cmp Try to optimize some compares.
17051 \labelwidthstring 00.00.0000
17063 -optimize-df Analyze the dataflow of the generated code and improve it.
17065 \labelwidthstring 00.00.0000
17077 -obanksel=nn Set optimization level for inserting BANKSELs.
17082 \labelwidthstring 00.00.0000
17086 \labelwidthstring 00.00.0000
17088 1 checks previous used register and if it is the same then does not emit
17089 BANKSEL, accounts only for labels.
17091 \labelwidthstring 00.00.0000
17093 2 tries to check the location of (even different) symbols and removes BANKSELs
17094 if they are in the same bank.
17099 Important: There might be problems if the linker script has data sections
17100 across bank borders!
17102 \layout Subsubsection
17106 \labelwidthstring 00.00.0000
17118 -nodefaultlibs do not link default libraries when linking
17120 \labelwidthstring 00.00.0000
17132 -no-crt Don't link the default run-time modules
17134 \labelwidthstring 00.00.0000
17146 -use-crt= Use a custom run-time module instead of the defaults.
17147 \layout Subsubsection
17152 Debugging options enable extra debugging information in the output files.
17154 \labelwidthstring 00.00.0000
17166 -debug-xtra Similar to -
17177 \begin_inset LatexCommand \index{-\/-debug}
17181 , but dumps more information.
17183 \labelwidthstring 00.00.0000
17195 -debug-ralloc Force register allocator to dump <source>.d file with debugging
17197 <source> is the name of the file compiled.
17199 \labelwidthstring 00.00.0000
17211 -pcode-verbose Enable pcode debugging information in translation.
17213 \labelwidthstring 00.00.0000
17225 -denable-peeps Force the usage of peepholes.
17228 \labelwidthstring 00.00.0000
17240 -gstack Trace push/pops for stack pointer overflow
17242 \labelwidthstring 00.00.0000
17254 -call-tree dump call tree in .calltree file
17257 Enviromental Variables
17260 There is a number of enviromental variables that can be used when running
17261 SDCC to enable certain optimizations or force a specific program behaviour.
17262 these variables are primarily for debugging purposes so they can be enabled/dis
17266 Currently there is only two such variables available:
17268 \labelwidthstring 00.00.0000
17270 OPTIMIZE_BITFIELD_POINTER_GET when this variable exists reading of structure
17271 bitfields is optimized by directly loading FSR0 with the address of the
17272 bitfield structure.
17273 Normally SDCC will cast the bitfield structure to a bitfield pointer and
17275 This step saves data ram and code space for functions that perform heavy
17278 80 bytes of code space are saved when compiling malloc.c with this option).
17281 \labelwidthstring 00.00.0000
17283 NO_REG_OPT do not perform pCode registers optimization.
17284 This should be used for debugging purposes.
17285 In some where bugs in the pcode optimizer are found, users can benefit
17286 from temporarily disabling the optimizer until the bug is fixed.
17289 Preprocessor Macros
17292 PIC16 port defines the following preprocessor macros while translating a
17297 \begin_inset Tabular
17298 <lyxtabular version="3" rows="6" 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 Port identification
17338 <row topline="true">
17339 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17357 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17362 Port identification (same as above)
17366 <row topline="true">
17367 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17375 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17380 MCU Identification.
17385 is the microcontrol identification number, i.e.
17390 <row topline="true">
17391 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17409 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17414 MCU Identification (same as above)
17418 <row topline="true" bottomline="true">
17419 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17427 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17432 nnn = SMALL or LARGE respectively according to the stack model used
17443 In addition the following macros are defined when calling assembler:
17447 \begin_inset Tabular
17448 <lyxtabular version="3" rows="4" columns="2">
17450 <column alignment="center" valignment="top" leftline="true" width="0">
17451 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17452 <row topline="true" bottomline="true">
17453 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17461 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17470 <row topline="true">
17471 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17479 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17484 MCU Identification.
17489 is the microcontrol identification number, i.e.
17494 <row topline="true">
17495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17508 nnn = SMALL or LARGE respectively according to the memory model used for
17513 <row topline="true" bottomline="true">
17514 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17522 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17527 nnn = SMALL or LARGE respectively according to the stack model used
17542 \begin_inset LatexCommand \index{PIC16}
17546 port uses the following directories for searching header files and libraries.
17550 \begin_inset Tabular
17551 <lyxtabular version="3" rows="3" columns="4">
17553 <column alignment="center" valignment="top" leftline="true" width="0">
17554 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17555 <column alignment="center" valignment="top" width="0">
17556 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
17557 <row topline="true" bottomline="true">
17558 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17566 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17574 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17582 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17591 <row topline="true">
17592 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17597 PREFIX/sdcc/include/pic16
17600 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17605 PIC16 specific headers
17608 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17616 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17625 <row topline="true" bottomline="true">
17626 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17631 PREFIX/sdcc/lib/pic16
17634 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17639 PIC16 specific libraries
17642 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17650 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17667 \begin_inset LatexCommand \label{sub:PIC16_Pragmas}
17674 PIC16 port currently supports the following pragmas:
17676 \labelwidthstring 00.00.0000
17678 stack pragma stack forces the code generator to initialize the stack & frame
17679 pointers at a specific address.
17680 This is an adhoc solution for cases where no STACK directive is available
17681 in the linker script or gplink is not instructed to create a stack section.
17683 The stack pragma should be used only once in a project.
17684 Multiple pragmas may result in indeterminate behaviour of the program.
17690 The old format (ie.
17691 #pragma stack 0x5ff) is deprecated and will cause the stack pointer to
17692 cross page boundaries (or even exceed the available data RAM) and crash
17694 Make sure that stack does not cross page boundaries when using the SMALL
17700 The format is as follows:
17703 #pragma stack bottom_address [stack_size]
17710 is the lower bound of the stack section.
17711 The stack pointer initially will point at address (bottom_address+stack_size-1).
17719 /* initializes stack of 100 bytes at RAM address 0x200 */
17722 #pragma stack 0x200 100
17725 If the stack_size field is omitted then a stack is created with the default
17727 This size might be enough for most programs, but its not enough for operations
17728 with deep function nesting or excessive stack usage.
17730 \labelwidthstring 00.00.0000
17734 This pragma is deprecated.
17735 Its use will cause a warning message to be issued.
17741 \labelwidthstring 00.00.0000
17743 code place a function symbol at static FLASH address
17751 /* place function test_func at 0x4000 */
17754 #pragma code test_func 0x4000
17758 \labelwidthstring 00.00.0000
17760 library instructs the linker to use a library module.
17765 #pragma library module_name
17772 can be any library or object file (including its path).
17773 Note that there are four reserved keywords which have special meaning.
17778 \begin_inset Tabular
17779 <lyxtabular version="3" rows="6" columns="3">
17781 <column alignment="center" valignment="top" leftline="true" width="0">
17782 <column alignment="block" valignment="top" leftline="true" width="20page%">
17783 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0">
17784 <row topline="true" bottomline="true">
17785 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17793 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17801 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17810 <row topline="true">
17811 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17821 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17826 ignore all library pragmas
17829 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17840 <row topline="true">
17841 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17851 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17859 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17872 <row topline="true">
17873 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17883 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17888 link the Math libarary
17891 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17904 <row topline="true">
17905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17915 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17920 link the I/O library
17923 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17936 <row topline="true" bottomline="true">
17937 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17947 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
17952 link the debug library
17955 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
17974 * is the device number, i.e.
17975 452 for PIC18F452 MCU.
17978 This feature allows for linking with specific libraries withoug having to
17979 explicit name them in the command line.
17984 keyword will reject all modules specified by the library pragma.
17986 \labelwidthstring 00.00.0000
17988 udata pragma udata instructs the compiler to emit code so that linker will
17989 place a variable at a specific memory bank
17997 /* places variable foo at bank2 */
18000 #pragma udata bank2 foo
18006 In order for this pragma to work extra SECTION directives should be added
18007 in the .lkr script.
18008 In the following example a sample .lkr file is shown:
18013 // Sample linker script for the PIC18F452 processor
18019 CODEPAGE NAME=vectors START=0x0 END=0x29 PROTECTED
18022 CODEPAGE NAME=page START=0x2A END=0x7FFF
18025 CODEPAGE NAME=idlocs START=0x200000 END=0x200007 PROTECTED
18028 CODEPAGE NAME=config START=0x300000 END=0x30000D PROTECTED
18031 CODEPAGE NAME=devid START=0x3FFFFE END=0x3FFFFF PROTECTED
18034 CODEPAGE NAME=eedata START=0xF00000 END=0xF000FF PROTECTED
18037 ACCESSBANK NAME=accessram START=0x0 END=0x7F
18042 DATABANK NAME=gpr0 START=0x80 END=0xFF
18045 DATABANK NAME=gpr1 START=0x100 END=0x1FF
18048 DATABANK NAME=gpr2 START=0x200 END=0x2FF
18051 DATABANK NAME=gpr3 START=0x300 END=0x3FF
18054 DATABANK NAME=gpr4 START=0x400 END=0x4FF
18057 DATABANK NAME=gpr5 START=0x500 END=0x5FF
18060 ACCESSBANK NAME=accesssfr START=0xF80 END=0xFFF PROTECTED
18065 SECTION NAME=CONFIG ROM=config
18070 SECTION NAME=bank0 RAM=gpr0 # these SECTION directives
18073 SECTION NAME=bank1 RAM=gpr1 # should be added to link
18076 SECTION NAME=bank2 RAM=gpr2 # section name 'bank?' with
18079 SECTION NAME=bank3 RAM=gpr3 # a specific DATABANK name
18082 SECTION NAME=bank4 RAM=gpr4
18085 SECTION NAME=bank5 RAM=gpr5
18088 The linker will recognise the section name set in the pragma statement and
18089 will position the variable at the memory bank set with the RAM field at
18090 the SECTION line in the linker script file.
18094 \begin_inset LatexCommand \label{sub:PIC16_Header-Files}
18101 There is one main header file that can be included to the source files using
18108 This header file contains the definitions for the processor special registers,
18109 so it is necessary if the source accesses them.
18110 It can be included by adding the following line in the beginning of the
18114 #include <pic18fregs.h>
18117 The specific microcontroller is selected within the pic18fregs.h automatically,
18118 so the same source can be used with a variety of devices.
18124 The libraries that PIC16
18125 \begin_inset LatexCommand \index{PIC16}
18129 port depends on are the microcontroller device libraries which contain
18130 the symbol definitions for the microcontroller special function registers.
18131 These libraries have the format pic18fxxxx.lib, where
18135 is the microcontroller identification number.
18136 The specific library is selected automatically by the compiler at link
18137 stage according to the selected device.
18140 Libraries are created with gplib which is part of the gputils package
18141 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
18146 \layout Subsubsection*
18148 Building the libraries
18151 Before using SDCC/pic16 there are some libraries that need to be compiled.
18152 This process is not done automatically by SDCC since not all users use
18153 SDCC for pic16 projects.
18154 So each user should compile the libraries separately.
18157 The steps to compile the pic16 libraries under Linux are:
18160 cd device/lib/pic16
18175 su -c 'make install' # install the libraries, you need the root password
18178 If you need to install the headers too, do:
18184 su -c 'make install' # install the headers, you need the root password
18187 There exist a special target to build the I/O libraries.
18188 This target is not automatically build because it will build the I/O library
18194 This way building will take quite a lot of time.
18195 Users are advised to edit the
18197 device/lib/pic16/pics.build
18199 file and then execute:
18208 The following memory models are supported by the PIC16 port:
18217 Memory model affects the default size of pointers within the source.
18218 The sizes are shown in the next table:
18222 \begin_inset Tabular
18223 <lyxtabular version="3" rows="3" columns="3">
18225 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18226 <column alignment="center" valignment="top" leftline="true" width="0">
18227 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18228 <row topline="true" bottomline="true">
18229 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18234 Pointer sizes according to memory model
18237 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18245 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18254 <row topline="true" bottomline="true">
18255 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18263 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18271 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18280 <row topline="true" bottomline="true">
18281 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18289 <cell multicolumn="1" alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
18297 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18313 It is advisable that all sources within a project are compiled with the
18315 If one wants to override the default memory model, this can be done by
18316 declaring a pointer as
18325 Far selects large memory model's pointers, while near selects small memory
18329 The standard device libraries (see
18330 \begin_inset LatexCommand \ref{sub:PIC16_Header-Files}
18334 ) contain no reference to pointers, so they can be used with both memory
18341 The stack implementation for the PIC16 port uses two indirect registers,
18344 \labelwidthstring 00.00.0000
18346 FSR1 is assigned as stack pointer
18348 \labelwidthstring 00.00.0000
18350 FSR2 is assigned as frame pointer
18353 The following stack models are supported by the PIC16 port
18374 model means that only the FSRxL byte is used to access stack and frame,
18381 uses both FSRxL and FSRxH registers.
18382 The following table shows the stack/frame pointers sizes according to stack
18383 model and the maximum space they can address:
18387 \begin_inset Tabular
18388 <lyxtabular version="3" rows="3" columns="3">
18390 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18391 <column alignment="center" valignment="top" leftline="true" width="0">
18392 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18393 <row topline="true" bottomline="true">
18394 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18399 Stack & Frame pointer sizes according to stack model
18402 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18410 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18419 <row topline="true">
18420 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18428 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18436 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18445 <row topline="true" bottomline="true">
18446 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18454 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18462 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18482 stack model is currently not working properly throughout the code generator.
18483 So its use is not advised.
18484 Also there are some other points that need special care:
18489 Do not create stack sections with size more than one physical bank (that
18493 Stack sections should no cross physical bank limits (i.e.
18494 #pragma stack 0x50 0x100)
18497 These limitations are caused by the fact that only FSRxL is modified when
18498 using SMALL stack model, so no more than 256 bytes of stack can be used.
18499 This problem will disappear after LARGE model is fully implemented.
18505 In addition to the standard SDCC function keywords, PIC16 port makes available
18508 \labelwidthstring 00.00.0000
18510 wparam Use the WREG to pass one byte of the first function argument.
18511 This improves speed but you may not use this for functions with arguments
18512 that are called via function pointers, otherwise the first byte of the
18513 first parameter will get lost.
18517 void func_wparam(int a) wparam
18523 /* WREG hold the lower part of a */
18526 /* the high part of a is stored in FSR2+2 (or +3 for large stack model)
18536 This keyword replaces the deprecated wparam pragma.
18538 \labelwidthstring 00.00.0000
18540 shadowregs When entering/exiting an ISR, it is possible to take advantage
18541 of the PIC18F hardware shadow registers which hold the values of WREG,
18542 STATUS and BSR registers.
18543 This can be done by adding the keyword
18551 keyword in the function's header.
18554 void isr_shadow(void) shadowregs interrupt 1
18570 instructs the code generator not to store/restore WREG, STATUS, BSR when
18571 entering/exiting the ISR.
18574 Function return values
18577 Return values from functions are placed to the appropriate registers following
18578 a modified Microchip policy optimized for SDCC.
18579 The following table shows these registers:
18583 \begin_inset Tabular
18584 <lyxtabular version="3" rows="6" columns="2">
18586 <column alignment="center" valignment="top" leftline="true" width="0">
18587 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18588 <row topline="true" bottomline="true">
18589 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18597 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18602 destination register
18606 <row topline="true">
18607 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18615 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18624 <row topline="true">
18625 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18633 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18642 <row topline="true">
18643 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18651 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18660 <row topline="true">
18661 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18669 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18674 FSR0L:PRODH:PRODL:WREG
18678 <row topline="true" bottomline="true">
18679 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18687 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18692 on stack, FSR0 points to the beginning
18706 An interrupt servive routine (ISR) is declared using the
18713 void isr(void) interrupt
18731 is the interrupt number, which for PIC18F devices can be:
18735 \begin_inset Tabular
18736 <lyxtabular version="3" rows="4" columns="3">
18738 <column alignment="center" valignment="top" leftline="true" width="0">
18739 <column alignment="center" valignment="top" leftline="true" width="0">
18740 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18741 <row topline="true" bottomline="true">
18742 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18760 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18765 Interrupt Vector Address
18769 <row topline="true">
18770 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18786 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18795 <row topline="true">
18796 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18813 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18827 HIGH priority interrupts
18830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18839 <row topline="true" bottomline="true">
18840 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18853 LOW priority interrupts
18856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
18872 When generating assembly code for ISR the code generator places a
18878 Interrupt Vector Address
18880 which points at the genetated ISR.
18881 This single GOTO instruction is part of an automatically generated
18883 interrupt entry point
18886 The actuall ISR code is placed as normally would in the code space.
18887 Upon interrupt request, the GOTO instruction is executed which jumps to
18889 When declaring interrupt functions as _naked this GOTO instruction is
18894 The whole interrupt functions is therefore placed at the Interrupt Vector
18895 Address of the specific interrupt.
18896 This is not a problem for the LOW priority interrupts, but it is a problem
18897 for the RESET and the HIGH priority interrupts because code may be written
18898 at the next interrupt´s vector address and cause undeterminate program
18899 behaviour if that interrupt is raised.
18905 This is not a problem when
18908 this is a HIGH interrupt ISR and LOW interrupts are
18915 when the ISR is small enough not to reach the next interrupt´s vector address.
18925 is possible to be omitted.
18926 This way a function is generated similar to an ISR, but it is not assigned
18930 When entering an interrupt, currently the PIC16
18931 \begin_inset LatexCommand \index{PIC16}
18935 port automatically saves the following registers:
18947 PROD (PRODL and PRODH)
18950 FSR0 (FSR0L and FSR0H)
18953 These registers are restored upon return from the interrupt routine.
18959 NOTE that when the _naked attribute is specified for an interrupt routine,
18960 then NO registers are stored or restored.
18969 Generic pointers are implemented in PIC16 port as 3-byte (24-bit) types.
18970 There are 3 types of generic pointers currently implemented data, code
18971 and eeprom pointers.
18972 They are differentiated by the value of the 7th and 6th bits of the upper
18977 \begin_inset Tabular
18978 <lyxtabular version="3" rows="5" columns="5">
18980 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18981 <column alignment="center" valignment="top" width="0">
18982 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
18983 <column alignment="center" valignment="top" width="0">
18984 <column alignment="left" valignment="top" rightline="true" width="0">
18985 <row topline="true" bottomline="true">
18986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
18994 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19002 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19010 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19015 rest of the pointer
19018 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19027 <row topline="true" bottomline="true">
19028 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19036 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19044 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19052 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19061 uuuuuu uuuuxxxx xxxxxxxx
19064 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19069 a 12-bit data pointer in data RAM memory
19073 <row bottomline="true">
19074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19082 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19090 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19098 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19107 uxxxxx xxxxxxxx xxxxxxxx
19110 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19115 a 21-bit code pointer in FLASH memory
19119 <row bottomline="true">
19120 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19128 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19136 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19144 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19153 uuuuuu uuuuuuxx xxxxxxxx
19156 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19161 a 10-bit eeprom pointer in EEPROM memory
19165 <row bottomline="true">
19166 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19174 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19182 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19190 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19199 xxxxxx xxxxxxxx xxxxxxxx
19202 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19207 unimplemented pointer type
19218 Generic pointer are read and written with a set of library functions which
19219 read/write 1, 2, 3, 4 bytes.
19223 \layout Subsubsection
19225 Standard I/O Streams
19232 the type FILE is defined as:
19235 typedef char * FILE;
19238 This type is the stream type implemented I/O in the PIC18F devices.
19239 Also the standard input and output streams are declared in stdio.h:
19242 extern FILE * stdin;
19245 extern FILE * stdout;
19248 The FILE type is actually a generic pointer which defines one more type
19249 of generic pointers, the
19254 This new type has the format:
19258 \begin_inset Tabular
19259 <lyxtabular version="3" rows="2" columns="7">
19261 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19262 <column alignment="center" valignment="top" width="0">
19263 <column alignment="center" valignment="top" leftline="true" width="0">
19264 <column alignment="center" valignment="top" leftline="true" width="0">
19265 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19266 <column alignment="center" valignment="top" width="0">
19267 <column alignment="left" valignment="top" rightline="true" width="0">
19268 <row topline="true" bottomline="true">
19269 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19277 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19285 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19293 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19301 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19309 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19314 rest of the pointer
19317 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19326 <row topline="true" bottomline="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">
19343 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19351 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19359 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19367 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19379 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19384 upper byte high nubble is 0x2n, the rest are zeroes
19395 Currently implemented there are 3 types of streams defined:
19399 \begin_inset Tabular
19400 <lyxtabular version="3" rows="4" columns="4">
19402 <column alignment="center" valignment="top" leftline="true" width="0">
19403 <column alignment="center" valignment="top" leftline="true" width="0">
19404 <column alignment="center" valignment="top" leftline="true" width="0">
19405 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19406 <row topline="true" bottomline="true">
19407 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19415 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19423 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19431 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19440 <row topline="true">
19441 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19449 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19459 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19467 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19472 Writes/Reads characters via the USART peripheral
19476 <row topline="true">
19477 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19485 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19495 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19503 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19508 Writes/Reads characters via the MSSP peripheral
19512 <row topline="true" bottomline="true">
19513 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19521 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19531 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19539 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19544 Writes/Reads characters via used defined functions
19555 The stream identifiers are declared as macros in the stdio.h header.
19558 In the libc library there exist the functions that are used to write to
19559 each of the above streams.
19562 \labelwidthstring 00.00.0000
19574 _stream_usart_putchar writes a character at the USART stream
19576 \labelwidthstring 00.00.0000
19588 _stream_mssp_putchar writes a character at the MSSP stream
19590 \labelwidthstring 00.00.0000
19592 putchar dummy function.
19593 This writes a character to a user specified manner.
19596 In order to increase performance
19600 is declared in stdio.h as having its parameter in WREG (it has the wparam
19602 In stdio.h exists the macro PUTCHAR(arg) that defines the putchar function
19603 in a user-friendly way.
19608 is the name of the variable that holds the character to print.
19609 An example follows:
19612 #include <pic18fregs.h>
19624 PORTA = c; /* dump character c to PORTA */
19637 stdout = STREAM_USER; /* this is not necessery, since stdout points
19640 * by default to STREAM_USER */
19643 printf (¨This is a printf test
19651 \layout Subsubsection
19656 PIC16 contains an implementation of the printf-family of functions.
19657 There exist the following functions:
19660 extern unsigned int sprintf(char *buf, char *fmt, ...);
19663 extern unsigned int vsprintf(char *buf, char *fmt, va_list ap);
19668 extern unsigned int printf(char *fmt, ...);
19671 extern unsigned int vprintf(char *fmt, va_lista ap);
19676 extern unsigned int fprintf(FILE *fp, char *fmt, ...);
19679 extern unsigned int vfprintf(FILE *fp, char *fmt, va_list ap);
19682 For sprintf and vsprintf
19686 should normally be a data pointer where the resulting string will be placed.
19687 No range checking is done so the user should allocate the necessery buffer.
19688 For fprintf and vfprintf
19692 should be a stream pointer (i.e.
19693 stdout, STREAM_MSSP, etc...).
19694 \layout Subsubsection
19699 The PIC18F family of microcontrollers supports a number of interrupt sources.
19700 A list of these interrupts is shown in the following table:
19704 \begin_inset Tabular
19705 <lyxtabular version="3" rows="11" columns="4">
19707 <column alignment="left" valignment="top" leftline="true" width="0">
19708 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19709 <column alignment="left" valignment="top" leftline="true" width="0">
19710 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0">
19711 <row topline="true" bottomline="true">
19712 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19720 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19728 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19736 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19745 <row topline="true">
19746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19754 <cell multicolumn="1" alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19759 PORTB change interrupt
19762 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19770 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19775 EEPROM/FLASH write complete interrupt
19779 <row topline="true">
19780 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19788 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19793 INT0 external interrupt
19796 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19804 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19809 Bus collision interrupt
19813 <row topline="true">
19814 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19822 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19827 INT1 external interrupt
19830 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19838 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19843 Low voltage detect interrupt
19847 <row topline="true">
19848 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19856 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19861 INT2 external interrupt
19864 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19877 Parallel slave port interrupt
19881 <row topline="true">
19882 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19895 CCP1 module interrupt
19898 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19906 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19911 AD convertion complete interrupt
19915 <row topline="true">
19916 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19924 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19929 CCP2 module interrupt
19932 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19940 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19945 USART receive interrupt
19949 <row topline="true">
19950 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19958 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19963 TMR0 overflow interrupt
19966 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19974 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19979 USART transmit interrupt
19983 <row topline="true">
19984 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
19992 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
19997 TMR1 overflow interrupt
20000 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20008 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20013 SSP receive/transmit interrupt
20017 <row topline="true">
20018 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20026 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20031 TMR2 matches PR2 interrupt
20034 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20041 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20049 <row topline="true" bottomline="true">
20050 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
20058 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20063 TMR3 overflow interrupt
20066 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20073 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
20088 The prototypes for these names are defined in the header file
20095 In order to simplify signal handling, a number of macros is provided:
20097 \labelwidthstring 00.00.0000
20099 DEF_INTHIGH(name) begin the definition of the interrupt dispatch table for
20100 high priority interrupts.
20105 is the function name to use.
20107 \labelwidthstring 00.00.0000
20109 DEF_INTLOW(name) begin the definition of the interrupt dispatch table fo
20110 low priority interrupt.
20115 is the function name to use.
20117 \labelwidthstring 00.00.0000
20119 DEF_HANDLER(sig,handler) define a handler for signal
20123 \labelwidthstring 00.00.0000
20125 END_DEF end the declaration of the dispatch table.
20128 Additionally there are two more macros to simplify the declaration of the
20131 \labelwidthstring 00.00.0000
20135 SIGHANDLER(handler)
20137 this declares the function prototype for the
20143 \labelwidthstring 00.00.0000
20145 SIGHANDLERNAKED(handler) same as SIGHANDLER() but declares a naked function.
20148 An example of using the macros above is shown below:
20151 #include <pic18fregs.h>
20154 #include <signal.h>
20158 DEF_INTHIGH(high_int)
20161 DEF_HANDLER(SIG_TMR0, _tmr0_handler)
20164 DEF_HANDLER(SIG_BCOL, _bcol_handler)
20171 SIGHANDLER(_tmr0_handler)
20177 /* action to be taken when timer 0 overflows */
20184 SIGHANDLERNAKED(_bcol_handler)
20193 /* action to be taken when bus collision occurs */
20209 Special care should be taken when using the above scheme:
20212 do not place a colon (;) at the end of the DEF_* and END_DEF macros.
20215 when declaring SIGHANDLERNAKED handler never forget to use
20219 for proper returning.
20225 Here you can find some general tips for compiling programs with SDCC/pic16.
20226 \layout Subsubsection
20231 The default stack size (that is 64 bytes) probably is enough for many programs.
20232 One must take care that when there are many levels of function nesting,
20233 or there is excessive usage of stack, its size should be extended.
20234 An example of such a case is the printf/sprintf family of functions.
20235 If you encounter problems like not being able to print integers, then you
20236 need to set the stack size around the maximum (256 for small stack model).
20237 The following diagram shows what happens when calling printf to print an
20241 printf () --> ltoa () --> ultoa () --> divschar ()
20244 It is should be understood that stack is easily consumed when calling complicate
20246 Using command line arguments like -
20256 -fommit-frame-pointer might reduce stack usage by not creating unnecessery
20258 Other ways to reduce stack usage may exist.
20261 Debugging with SDCDB
20262 \begin_inset LatexCommand \label{cha:Debugging-with-SDCDB}
20267 \begin_inset LatexCommand \index{sdcdb (debugger)}
20274 SDCC is distributed with a source level debugger
20275 \begin_inset LatexCommand \index{Debugger}
20280 The debugger uses a command line interface, the command repertoire of the
20281 debugger has been kept as close to gdb
20282 \begin_inset LatexCommand \index{gdb}
20286 (the GNU debugger) as possible.
20287 The configuration and build process is part of the standard compiler installati
20288 on, which also builds and installs the debugger in the target directory
20289 specified during configuration.
20290 The debugger allows you debug BOTH at the C source and at the ASM source
20292 Sdcdb is currently not available on Win32 platforms.
20295 Compiling for Debugging
20309 \begin_inset LatexCommand \index{-\/-debug}
20313 option must be specified for all files for which debug information is to
20315 The complier generates a .adb file for each of these files.
20316 The linker creates the .cdb
20317 \begin_inset LatexCommand \index{<file>.cdb}
20322 \begin_inset LatexCommand \index{<file>.adb}
20326 files and the address information.
20327 This .cdb is used by the debugger.
20330 How the Debugger Works
20343 -debug option is specified the compiler generates extra symbol information
20344 some of which are put into the assembler source and some are put into the
20346 Then the linker creates the .cdb file from the individual .adb files with
20347 the address information for the symbols.
20348 The debugger reads the symbolic information generated by the compiler &
20349 the address information generated by the linker.
20350 It uses the SIMULATOR (Daniel's S51) to execute the program, the program
20351 execution is controlled by the debugger.
20352 When a command is issued for the debugger, it translates it into appropriate
20353 commands for the simulator.
20356 Starting the Debugger
20359 The debugger can be started using the following command line.
20360 (Assume the file you are debugging has the file name foo).
20374 The debugger will look for the following files.
20377 foo.c - the source file.
20380 foo.cdb - the debugger symbol information file.
20383 foo.ihx - the Intel hex format
20384 \begin_inset LatexCommand \index{Intel hex format}
20391 Command Line Options
20404 -directory=<source file directory> this option can used to specify the directory
20406 The debugger will look into the directory list specified for source, cdb
20408 The items in the directory list must be separated by ':', e.g.
20409 if the source files can be in the directories /home/src1 and /home/src2,
20420 -directory option should be -
20430 -directory=/home/src1:/home/src2.
20431 Note there can be no spaces in the option.
20435 -cd <directory> - change to the <directory>.
20438 -fullname - used by GUI front ends.
20441 -cpu <cpu-type> - this argument is passed to the simulator please see the
20442 simulator docs for details.
20445 -X <Clock frequency > this options is passed to the simulator please see
20446 the simulator docs for details.
20449 -s <serial port file> passed to simulator see the simulator docs for details.
20452 -S <serial in,out> passed to simulator see the simulator docs for details.
20455 -k <port number> passed to simulator see the simulator docs for details.
20461 As mentioned earlier the command interface for the debugger has been deliberatel
20462 y kept as close the GNU debugger gdb, as possible.
20463 This will help the integration with existing graphical user interfaces
20464 (like ddd, xxgdb or xemacs) existing for the GNU debugger.
20465 If you use a graphical user interface for the debugger you can skip this
20467 \layout Subsubsection*
20469 break [line | file:line | function | file:function]
20472 Set breakpoint at specified line or function:
20481 sdcdb>break foo.c:100
20483 sdcdb>break funcfoo
20485 sdcdb>break foo.c:funcfoo
20486 \layout Subsubsection*
20488 clear [line | file:line | function | file:function ]
20491 Clear breakpoint at specified line or function:
20500 sdcdb>clear foo.c:100
20502 sdcdb>clear funcfoo
20504 sdcdb>clear foo.c:funcfoo
20505 \layout Subsubsection*
20510 Continue program being debugged, after breakpoint.
20511 \layout Subsubsection*
20516 Execute till the end of the current function.
20517 \layout Subsubsection*
20522 Delete breakpoint number 'n'.
20523 If used without any option clear ALL user defined break points.
20524 \layout Subsubsection*
20526 info [break | stack | frame | registers ]
20529 info break - list all breakpoints
20532 info stack - show the function call stack.
20535 info frame - show information about the current execution frame.
20538 info registers - show content of all registers.
20539 \layout Subsubsection*
20544 Step program until it reaches a different source line.
20545 Note: pressing <return> repeats the last command.
20546 \layout Subsubsection*
20551 Step program, proceeding through subroutine calls.
20552 \layout Subsubsection*
20557 Start debugged program.
20558 \layout Subsubsection*
20563 Print type information of the variable.
20564 \layout Subsubsection*
20569 print value of variable.
20570 \layout Subsubsection*
20575 load the given file name.
20576 Note this is an alternate method of loading file for debugging.
20577 \layout Subsubsection*
20582 print information about current frame.
20583 \layout Subsubsection*
20588 Toggle between C source & assembly source.
20589 \layout Subsubsection*
20591 ! simulator command
20594 Send the string following '!' to the simulator, the simulator response is
20596 Note the debugger does not interpret the command being sent to the simulator,
20597 so if a command like 'go' is sent the debugger can loose its execution
20598 context and may display incorrect values.
20599 \layout Subsubsection*
20606 My name is Bobby Brown"
20609 Interfacing with DDD
20612 The screenshot was converted from png to eps with:
20613 \begin_inset Quotes sld
20616 bmeps -c -e8f -p3 ddd_example.png >ddd_example.eps
20617 \begin_inset Quotes srd
20620 which produces a pretty compact eps file which is free from compression
20624 The screenshot was included in sdccman.lyx cvs version 1.120 but later removed
20625 as this broke the build system on Sourceforge (pdf-file was broken).
20631 \begin_inset LatexCommand \url{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/doc/figures/ddd_example.eps}
20637 shows a screenshot of a debugging session with DDD
20638 \begin_inset LatexCommand \index{DDD (debugger)}
20642 (Unix only) on a simulated 8032.
20643 The debugging session might not run as smoothly as the screenshot suggests.
20644 The debugger allows setting of breakpoints, displaying and changing variables,
20645 single stepping through C and assembler code.
20648 The source was compiled with
20671 -debug ddd_example.c
20684 and DDD was invoked with
20691 ddd -debugger 'sdcdb -cpu 8032 ddd_example'
20694 Interfacing with XEmacs
20695 \begin_inset LatexCommand \index{XEmacs}
20700 \begin_inset LatexCommand \index{Emacs}
20707 Two files (in emacs lisp) are provided for the interfacing with XEmacs,
20708 sdcdb.el and sdcdbsrc.el.
20709 These two files can be found in the $(prefix)/bin directory after the installat
20711 These files need to be loaded into XEmacs for the interface to work.
20712 This can be done at XEmacs startup time by inserting the following into
20713 your '.xemacs' file (which can be found in your HOME directory):
20719 (load-file sdcdbsrc.el)
20725 .xemacs is a lisp file so the () around the command is REQUIRED.
20726 The files can also be loaded dynamically while XEmacs is running, set the
20727 environment variable 'EMACSLOADPATH' to the installation bin directory
20728 (<installdir>/bin), then enter the following command ESC-x load-file sdcdbsrc.
20729 To start the interface enter the following command:
20743 You will prompted to enter the file name to be debugged.
20748 The command line options that are passed to the simulator directly are bound
20749 to default values in the file sdcdbsrc.el.
20750 The variables are listed below, these values maybe changed as required.
20753 sdcdbsrc-cpu-type '51
20756 sdcdbsrc-frequency '11059200
20759 sdcdbsrc-serial nil
20762 The following is a list of key mapping for the debugger interface.
20773 ;;key\SpecialChar ~
20787 binding\SpecialChar ~
20811 ;;---\SpecialChar ~
20825 -------\SpecialChar ~
20867 sdcdb-next-from-src\SpecialChar ~
20895 sdcdb-back-from-src\SpecialChar ~
20923 sdcdb-cont-from-src\SpecialChar ~
20933 SDCDB continue command
20951 sdcdb-step-from-src\SpecialChar ~
20979 sdcdb-whatis-c-sexp\SpecialChar ~
20989 SDCDB ptypecommand for data at
21056 sdcdbsrc-delete\SpecialChar ~
21070 SDCDB Delete all breakpoints if no arg
21119 given or delete arg (C-u arg x)
21137 sdcdbsrc-frame\SpecialChar ~
21152 SDCDB Display current frame if no arg,
21201 given or display frame arg
21268 sdcdbsrc-goto-sdcdb\SpecialChar ~
21278 Goto the SDCDB output buffer
21296 sdcdb-print-c-sexp\SpecialChar ~
21307 SDCDB print command for data at
21374 sdcdbsrc-goto-sdcdb\SpecialChar ~
21384 Goto the SDCDB output buffer
21402 sdcdbsrc-mode\SpecialChar ~
21418 Toggles Sdcdbsrc mode (turns it off)
21433 sdcdb-finish-from-src\SpecialChar ~
21441 SDCDB finish command
21456 sdcdb-break\SpecialChar ~
21474 Set break for line with point
21489 sdcdbsrc-mode\SpecialChar ~
21505 Toggle Sdcdbsrc mode
21520 sdcdbsrc-srcmode\SpecialChar ~
21543 Here are a few guidelines that will help the compiler generate more efficient
21544 code, some of the tips are specific to this compiler others are generally
21545 good programming practice.
21548 Use the smallest data type to represent your data-value.
21549 If it is known in advance that the value is going to be less than 256 then
21550 use an 'unsigned char' instead of a 'short' or 'int'.
21551 Please note, that ANSI C requires both signed and unsigned chars to be
21552 promoted to 'signed int' before doing any operation.
21554 \begin_inset LatexCommand \index{type promotion}
21558 can be omitted, if the result is the same.
21559 The effect of the promotion rules together with the sign-extension is often
21566 unsigned char uc = 0xfe;
21568 if (uc * uc < 0) /* this is true! */
21587 (int) uc * (int) uc = (int) 0xfe * (int) 0xfe = (int) 0xfc04 = -1024
21597 (unsigned char) -12 / (signed char) -3 = ...
21600 No, the result is not 4:
21605 (int) (unsigned char) -12 / (int) (signed char) -3 =
21607 (int) (unsigned char) 0xf4 / (int) (signed char) 0xfd =
21609 (int) 0x00f4 / (int) 0xfffd =
21611 (int) 0x00f4 / (int) 0xfffd =
21613 (int) 244 / (int) -3 =
21615 (int) -81 = (int) 0xffaf;
21618 Don't complain, that gcc gives you a different result.
21619 gcc uses 32 bit ints, while SDCC uses 16 bit ints.
21620 Therefore the results are different.
21623 \begin_inset Quotes sld
21627 \begin_inset Quotes srd
21633 If well-defined overflow characteristics are important and negative values
21634 are not, or if you want to steer clear of sign-extension problems when
21635 manipulating bits or bytes, use one of the corresponding unsigned types.
21636 (Beware when mixing signed and unsigned values in expressions, though.)
21638 Although character types (especially unsigned char) can be used as "tiny"
21639 integers, doing so is sometimes more trouble than it's worth, due to unpredicta
21640 ble sign extension and increased code size.
21644 Use unsigned when it is known in advance that the value is not going to
21646 This helps especially if you are doing division or multiplication, bit-shifting
21647 or are using an array index.
21650 NEVER jump into a LOOP.
21653 Declare the variables to be local
21654 \begin_inset LatexCommand \index{local variables}
21658 whenever possible, especially loop control variables (induction).
21661 Since the compiler does not always do implicit integral promotion, the programme
21662 r should do an explicit cast when integral promotion is required.
21665 Reducing the size of division, multiplication & modulus operations can reduce
21666 code size substantially.
21667 Take the following code for example.
21673 foobar(unsigned int p1, unsigned char ch)
21681 unsigned char ch1 = p1 % ch ;
21692 For the modulus operation the variable ch will be promoted to unsigned int
21693 first then the modulus operation will be performed (this will lead to a
21694 call to support routine _moduint()), and the result will be casted to a
21696 If the code is changed to
21701 foobar(unsigned int p1, unsigned char ch)
21709 unsigned char ch1 = (unsigned char)p1 % ch ;
21720 It would substantially reduce the code generated (future versions of the
21721 compiler will be smart enough to detect such optimization opportunities).
21725 Have a look at the assembly listing to get a
21726 \begin_inset Quotes sld
21730 \begin_inset Quotes srd
21733 for the code generation.
21737 \begin_inset LatexCommand \index{Tools}
21741 included in the distribution
21745 \begin_inset Tabular
21746 <lyxtabular version="3" rows="12" columns="3">
21748 <column alignment="center" valignment="top" leftline="true" width="0pt">
21749 <column alignment="center" valignment="top" leftline="true" width="0pt">
21750 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
21751 <row topline="true" bottomline="true">
21752 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21760 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21768 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21777 <row topline="true">
21778 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21786 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21791 Simulator for various architectures
21794 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21803 <row topline="true">
21804 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21812 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21817 header file conversion
21820 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21825 sdcc/support/scripts
21829 <row topline="true">
21830 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21838 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21843 header file conversion
21846 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21851 sdcc/support/scripts
21855 <row topline="true">
21856 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21864 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21872 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21890 <row topline="true">
21891 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21899 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21907 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21925 <row topline="true">
21926 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21934 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21942 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21960 <row topline="true">
21961 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21969 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
21977 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
21995 <row topline="true">
21996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22004 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22012 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22030 <row topline="true">
22031 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22039 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22047 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22065 <row topline="true">
22066 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22074 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22082 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22100 <row topline="true" bottomline="true">
22101 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22109 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22117 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22145 \begin_inset LatexCommand \index{Documentation}
22149 included in the distribution
22153 \begin_inset Tabular
22154 <lyxtabular version="3" rows="10" columns="2">
22156 <column alignment="left" valignment="top" leftline="true" width="0">
22157 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22158 <row topline="true" bottomline="true">
22159 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22167 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22172 Where to get / filename
22176 <row topline="true">
22177 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22182 SDCC Compiler User Guide
22185 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22190 You're reading it right now
22194 <row topline="true">
22195 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22203 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22212 <row topline="true">
22213 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22219 \begin_inset LatexCommand \index{asXXXX (as-gbz80, as-hc08, asx8051, as-z80)}
22224 \begin_inset LatexCommand \index{Assembler documentation}
22228 Assemblers and ASLINK
22229 \begin_inset LatexCommand \index{aslink}
22234 \begin_inset LatexCommand \index{Linker documentation}
22241 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22246 sdcc/as/doc/asxhtm.html
22250 <row topline="true">
22251 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22256 SDCC regression test
22257 \begin_inset LatexCommand \index{Regression test}
22264 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22269 sdcc/doc/test_suite_spec.pdf
22273 <row topline="true">
22274 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22282 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22291 <row topline="true">
22292 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22297 Notes on debugging with sdcdb
22298 \begin_inset LatexCommand \index{sdcdb (debugger)}
22305 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22310 sdcc/debugger/README
22314 <row topline="true">
22315 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22320 Software simulator for microcontrollers
22323 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22350 <row topline="true">
22351 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22356 Temporary notes on the pic16
22357 \begin_inset LatexCommand \index{PIC16}
22364 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22369 sdcc/src/pic16/NOTES
22373 <row topline="true" bottomline="true">
22374 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22379 SDCC internal documentation (debugging file format)
22382 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22418 Related open source tools
22419 \begin_inset LatexCommand \index{Related tools}
22427 \begin_inset Tabular
22428 <lyxtabular version="3" rows="11" columns="3">
22430 <column alignment="center" valignment="top" leftline="true" width="0pt">
22431 <column alignment="block" valignment="top" leftline="true" width="30line%">
22432 <column alignment="left" valignment="top" leftline="true" rightline="true" width="0pt">
22433 <row topline="true" bottomline="true">
22434 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22442 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22450 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22459 <row topline="true">
22460 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22466 \begin_inset LatexCommand \index{gpsim (pic simulator)}
22473 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22481 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22487 \begin_inset LatexCommand \url{http://www.dattalo.com/gnupic/gpsim.html}
22495 <row topline="true">
22496 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22502 \begin_inset LatexCommand \index{gputils (pic tools)}
22509 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22517 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22523 \begin_inset LatexCommand \url{http://sourceforge.net/projects/gputils}
22531 <row topline="true">
22532 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22540 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22548 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22554 \begin_inset LatexCommand \url{http://freshmeat.net/projects/flp5/}
22562 <row topline="true">
22563 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22569 \begin_inset LatexCommand \index{indent (source formatting tool)}
22576 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22581 Formats C source - Master of the white spaces
22584 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22590 \begin_inset LatexCommand \url{http://directory.fsf.org/GNU/indent.html}
22598 <row topline="true">
22599 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22605 \begin_inset LatexCommand \index{srecord (bin, hex, ... tool)}
22612 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22617 Object file conversion, checksumming, ...
22620 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22626 \begin_inset LatexCommand \url{http://sourceforge.net/projects/srecord}
22634 <row topline="true">
22635 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22641 \begin_inset LatexCommand \index{objdump (tool)}
22648 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22653 Object file conversion, ...
22656 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22661 Part of binutils (should be there anyway)
22665 <row topline="true">
22666 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22672 \begin_inset LatexCommand \index{doxygen (source documentation tool)}
22679 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22684 Source code documentation system
22687 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22693 \begin_inset LatexCommand \url{http://www.doxygen.org}
22701 <row topline="true">
22702 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22710 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22715 IDE (has anyone tried integrating SDCC & sdcdb? Unix only)
22718 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22724 \begin_inset LatexCommand \url{http://www.kdevelop.org}
22732 <row topline="true">
22733 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22739 \begin_inset LatexCommand \index{splint (syntax checking tool)}
22746 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22751 Statically checks c sources (see
22752 \begin_inset LatexCommand \ref{lyx:more-pedantic-SPLINT}
22759 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22765 \begin_inset LatexCommand \url{http://www.splint.org}
22773 <row topline="true" bottomline="true">
22774 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22780 \begin_inset LatexCommand \index{ddd (debugger)}
22787 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22792 Debugger, serves nicely as GUI to sdcdb
22793 \begin_inset LatexCommand \index{sdcdb (debugger)}
22800 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22806 \begin_inset LatexCommand \url{http://www.gnu.org/software/ddd/}
22823 Related documentation / recommended reading
22827 \begin_inset Tabular
22828 <lyxtabular version="3" rows="6" columns="3">
22830 <column alignment="center" valignment="top" leftline="true" width="0pt">
22831 <column alignment="block" valignment="top" leftline="true" width="30line%">
22832 <column alignment="center" valignment="top" leftline="true" rightline="true" width="0pt">
22833 <row topline="true" bottomline="true">
22834 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22842 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22850 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22859 <row topline="true">
22860 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22877 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22883 \begin_inset LatexCommand \index{C Reference card}
22890 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22896 \begin_inset LatexCommand \url{http://refcards.com/refcards/c/index.html}
22904 <row topline="true">
22905 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22913 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22921 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22927 \begin_inset LatexCommand \url{http://www.eskimo.com/~scs/C-faq/top.html}
22935 <row topline="true">
22936 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22943 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22948 Latest datasheet of the target CPU
22951 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22960 <row topline="true">
22961 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22968 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22973 Revision history of datasheet
22976 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
22985 <row topline="true" bottomline="true">
22986 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
22996 <cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
23001 Advanced Compiler Design and Implementation
23004 <cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
23009 bookstore (very dedicated, probably read other books first)
23025 Some questions answered, some pointers given - it might be time to in turn
23033 can you solve your project with the selected microcontroller? Would you
23034 find out early or rather late that your target is too small/slow/whatever?
23035 Can you switch to a slightly better device if it doesn't fit?
23038 should you solve the problem with an 8 bit CPU? Or would a 16/32 bit CPU
23039 and/or another programming language be more adequate? Would an operating
23040 system on the target device help?
23043 if you solved the problem, will the marketing department be happy?
23046 if the marketing department is happy, will customers be happy?
23049 if you're the project manager, marketing department and maybe even the customer
23050 in one person, have you tried to see the project from the outside?
23053 is the project done if you think it is done? Or is just that other interface/pro
23054 tocol/feature/configuration/option missing? How about website, manual(s),
23055 internationali(z|s)ation, packaging, labels, 2nd source for components,
23056 electromagnetic compatability/interference, documentation for production,
23057 production test software, update mechanism, patent issues?
23060 is your project adequately positioned in that magic triangle: fame, fortune,
23064 Maybe not all answers to these questions are known and some answers may
23069 , nevertheless knowing these questions may help you to avoid burnout
23075 burnout is bad for electronic devices, programmers and motorcycle tyres
23079 Chances are you didn't want to hear some of them...
23083 \begin_inset LatexCommand \index{Support}
23090 SDCC has grown to be a large project.
23091 The compiler alone (without the preprocessor, assembler and linker) is
23092 well over 100,000 lines of code (blank stripped).
23093 The open source nature of this project is a key to its continued growth
23095 You gain the benefit and support of many active software developers and
23097 Is SDCC perfect? No, that's why we need your help.
23098 The developers take pride in fixing reported bugs.
23099 You can help by reporting the bugs and helping other SDCC users.
23100 There are lots of ways to contribute, and we encourage you to take part
23101 in making SDCC a great software package.
23105 The SDCC project is hosted on the SDCC sourceforge site at
23106 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/projects/sdcc}
23111 You'll find the complete set of mailing lists
23112 \begin_inset LatexCommand \index{Mailing list(s)}
23116 , forums, bug reporting system, patch submission
23117 \begin_inset LatexCommand \index{Patch submission}
23122 \begin_inset LatexCommand \index{download}
23126 area and Subversion code repository
23127 \begin_inset LatexCommand \index{Subversion code repository}
23135 \begin_inset LatexCommand \index{Bug reporting}
23140 \begin_inset LatexCommand \index{Reporting bugs}
23147 The recommended way of reporting bugs is using the infrastructure of the
23149 You can follow the status of bug reports there and have an overview about
23153 Bug reports are automatically forwarded to the developer mailing list and
23154 will be fixed ASAP.
23155 When reporting a bug, it is very useful to include a small test program
23156 (the smaller the better) which reproduces the problem.
23157 If you can isolate the problem by looking at the generated assembly code,
23158 this can be very helpful.
23159 Compiling your program with the -
23170 \begin_inset LatexCommand \index{-\/-dumpall}
23174 option can sometimes be useful in locating optimization problems.
23175 When reporting a bug please maker sure you:
23178 Attach the code you are compiling with SDCC.
23182 Specify the exact command you use to run SDCC, or attach your Makefile.
23186 Specify the SDCC version (type "
23192 "), your platform, and operating system.
23196 Provide an exact copy of any error message or incorrect output.
23200 Put something meaningful in the subject of your message.
23203 Please attempt to include these 5 important parts, as applicable, in all
23204 requests for support or when reporting any problems or bugs with SDCC.
23205 Though this will make your message lengthy, it will greatly improve your
23206 chance that SDCC users and developers will be able to help you.
23207 Some SDCC developers are frustrated by bug reports without code provided
23208 that they can use to reproduce and ultimately fix the problem, so please
23209 be sure to provide sample code if you are reporting a bug!
23212 Please have a short check that you are using a recent version of SDCC and
23213 the bug is not yet known.
23214 This is the link for reporting bugs:
23215 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=100599}
23222 Requesting Features
23223 \begin_inset LatexCommand \label{sub:Requesting-Features}
23228 \begin_inset LatexCommand \index{Feature request}
23233 \begin_inset LatexCommand \index{Requesting features}
23240 Like bug reports feature requests are forwarded to the developer mailing
23242 This is the link for requesting features:
23243 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23253 Like bug reports contributed patches are forwarded to the developer mailing
23255 This is the link for submitting patches
23256 \begin_inset LatexCommand \index{Patch submission}
23261 \begin_inset LatexCommand \url{http://sourceforge.net/tracker/?group_id=599&atid=300599}
23268 You need to specify some parameters to the
23272 command for the patches to be useful.
23273 If you modified more than one file a patch created f.e.
23278 \begin_inset Quotes sld
23281 diff -Naur unmodified_directory modified_directory >my_changes.patch
23282 \begin_inset Quotes srd
23288 will be fine, otherwise
23292 \begin_inset Quotes sld
23295 diff -u sourcefile.c.orig sourcefile.c >my_changes.patch
23296 \begin_inset Quotes srd
23309 These links should take you directly to the
23310 \begin_inset LatexCommand \url[Mailing lists]{http://sourceforge.net/mail/?group_id=599}
23320 Traffic on sdcc-devel and sdcc-user is about 100 mails/month each not counting
23321 automated messages (mid 2003)
23325 \begin_inset LatexCommand \url[Forums]{http://sourceforge.net/forum/?group_id=599}
23330 \begin_inset LatexCommand \index{Mailing list(s)}
23334 and forums are archived and searchable so if you are lucky someone already
23335 had a similar problem.
23336 While mails to the lists themselves are delivered promptly their web front
23337 end on sourceforge sometimes shows a severe time lag (up to several weeks),
23338 if you're seriously using SDCC please consider subscribing to the lists.
23344 You can follow the status of the Subversion version
23345 \begin_inset LatexCommand \index{version}
23349 of SDCC by watching the Changelog
23350 \begin_inset LatexCommand \index{Changelog}
23354 in the Subversion repository
23359 \begin_inset LatexCommand \htmlurl{http://svn.sourceforge.net/viewcvs.cgi/*checkout*/sdcc/trunk/sdcc/ChangeLog}
23367 \begin_inset LatexCommand \index{Release policy}
23374 Historically there often were long delays between official releases and
23375 the sourceforge download area tends to get not updated at all.
23376 Excuses in the past might have referred to problems with live range analysis,
23377 but as this was fixed a while ago, the current problem is that another
23378 excuse has to be found.
23379 Kidding aside, we have to get better there! On the other hand there are
23380 daily snapshots available at
23381 \begin_inset LatexCommand \htmlurl[snap]{http://sdcc.sourceforge.net/snap.php}
23385 , and you can always build the very last version (hopefully with many bugs
23386 fixed, and features added) from the source code available at
23387 \begin_inset LatexCommand \htmlurl[Source]{http://sdcc.sourceforge.net/snap.php#Source}
23395 \begin_inset LatexCommand \index{Examples}
23402 You'll find some small examples in the directory
23404 sdcc/device/examples/.
23407 More examples and libraries are available at
23409 The SDCC Open Knowledge Resource
23410 \begin_inset LatexCommand \url{http://sdccokr.dl9sec.de/}
23417 \begin_inset LatexCommand \url{http://www.pjrc.com/tech/8051/}
23424 I did insert a reference to Paul's web site here although it seems rather
23425 dedicated to a specific 8032 board (I think it's okay because it f.e.
23426 shows LCD/Harddisc interface and has a free 8051 monitor.
23427 Independent 8032 board vendors face hard competition of heavily subsidized
23428 development boards anyway).
23431 Maybe we should include some links to real world applications.
23432 Preferably pointer to pointers (one for each architecture) so this stays
23437 \begin_inset LatexCommand \index{Quality control}
23444 The compiler is passed through nightly compile and build checks.
23450 \begin_inset LatexCommand \index{Regression test}
23454 check that SDCC itself compiles flawlessly on several platforms and checks
23455 the quality of the code generated by SDCC by running the code through simulator
23457 There is a separate document
23460 \begin_inset LatexCommand \index{Test suite}
23469 You'll find the test code in the directory
23471 sdcc/support/regression
23474 You can run these tests manually by running
23478 in this directory (or f.e.
23483 \begin_inset Quotes sld
23487 \begin_inset Quotes srd
23493 if you don't want to run the complete tests).
23494 The test code might also be interesting if you want to look for examples
23495 \begin_inset LatexCommand \index{Examples}
23499 checking corner cases of SDCC or if you plan to submit patches
23500 \begin_inset LatexCommand \index{Patch submission}
23507 The pic port uses a different set of regression tests, you'll find them
23510 sdcc/src/regression
23515 Use of SDCC in Education
23528 the phrase "use in education" might evoke the association "
23532 fit for use in education".
23533 This connotation is not intended but nevertheless risked as the licensing
23534 of SDCC makes it difficult to offer educational discounts
23538 If your rationales are to:
23541 give students a chance to understand the
23545 steps of code generation
23548 have a curriculum that can be extended for years.
23549 Then you could use an fpga board as target and your curriculum will seamlessly
23550 extend from logic synthesis (
23551 \begin_inset LatexCommand \url[http://www.opencores.org]{opencores.org}
23556 \begin_inset LatexCommand \url[Oregano]{http://www.oregano.at/ip/ip01.htm}
23560 ), over assembly programming, to C to FPGA compilers (
23561 \begin_inset LatexCommand \url[FPGAC]{http://sf.net/projects/fpgac}
23568 be able to insert excursions about skills like using a revision control
23569 system, submitting/applying patches, using a type-setting (as opposed to
23570 word-processing) engine LyX/LaTeX, using
23571 \begin_inset LatexCommand \url[SourceForge]{http://www.sf.net}
23576 \begin_inset LatexCommand \url[netiquette]{http://en.wikipedia.org/wiki/Netiquette}
23580 , understanding BSD/LGPL/GPL/Proprietary licensing, growth models of Open
23581 Source Software, CPU simulation, compiler regression tests
23582 \begin_inset LatexCommand \index{Regression test}
23589 And if there should be a shortage of ideas then you can always point students
23590 to the ever-growing feature request list
23591 \begin_inset LatexCommand \htmlurl{http://sourceforge.net/tracker/?group_id=599&atid=350599}
23598 not tie students to a specific host platform and instead allow them to use
23603 choice (among them Alpha, i386, i386_64, MacOs, Mips, Sparc, Windows and
23605 \begin_inset LatexCommand \url[OLPC]{http://wiki.laptop.org/wiki/One_Laptop_per_Child}
23612 not encourage students to use illegal copies of educational software
23615 be immune to licensing/availability/price changes of the chosen tool chain
23618 be able to change to a new target platform without having to adopt a new
23622 have complete control over and insight into the tool chain
23625 make your students aware about the pros and cons of open source software
23629 give back to the public as you are probably at least partially publically
23633 give students a chance to publically prove their skills and to possibly
23634 see a world wide impact
23637 then SDCC is probably among the first choices.
23638 Well, probably SDCC might be the only choice.
23641 SDCC Technical Data
23645 \begin_inset LatexCommand \index{Optimizations}
23652 SDCC performs a host of standard optimizations in addition to some MCU specific
23657 Sub-expression Elimination
23658 \begin_inset LatexCommand \index{Subexpression elimination}
23665 The compiler does local and
23691 will be translated to
23703 Some subexpressions are not as obvious as the above example, e.g.:
23713 In this case the address arithmetic a->b[i] will be computed only once;
23714 the equivalent code in C would be.
23726 The compiler will try to keep these temporary variables in registers.
23729 Dead-Code Elimination
23730 \begin_inset LatexCommand \index{Dead-code elimination}
23751 i = 1; \SpecialChar ~
23760 global = 1;\SpecialChar ~
23773 global = 3;\SpecialChar ~
23798 \begin_inset LatexCommand \index{Copy propagation}
23854 Note: the dead stores created by this copy propagation will be eliminated
23855 by dead-code elimination.
23859 \begin_inset LatexCommand \index{Loop optimization}
23864 \begin_inset LatexCommand \label{sub:Loop-Optimizations}
23871 Two types of loop optimizations are done by SDCC
23879 of loop induction variables.
23880 In addition to the strength reduction the optimizer marks the induction
23881 variables and the register allocator tries to keep the induction variables
23882 in registers for the duration of the loop.
23883 Because of this preference of the register allocator
23884 \begin_inset LatexCommand \index{Register allocation}
23888 , loop induction optimization causes an increase in register pressure, which
23889 may cause unwanted spilling of other temporary variables into the stack
23890 \begin_inset LatexCommand \index{stack}
23895 The compiler will generate a warning message when it is forced to allocate
23896 extra space either on the stack or data space.
23897 If this extra space allocation is undesirable then induction optimization
23898 can be eliminated either for the entire source file (with -
23908 -noinduction option) or for a given function only using #pragma\SpecialChar ~
23910 \begin_inset LatexCommand \index{\#pragma noinduction}
23923 for (i = 0 ; i < 100 ; i ++)
23939 for (i = 0; i < 100; i++)
23948 As mentioned previously some loop invariants are not as apparent, all static
23949 address computations are also moved out of the loop.
23954 \begin_inset LatexCommand \index{Strength reduction}
23958 , this optimization substitutes an expression by a cheaper expression:
23963 for (i=0;i < 100; i++)
23981 for (i=0;i< 100;i++) {
23987 ar[itemp1] = itemp2;
24004 The more expensive multiplication
24005 \begin_inset LatexCommand \index{Multiplication}
24009 is changed to a less expensive addition.
24013 \begin_inset LatexCommand \index{Loop reversing}
24020 This optimization is done to reduce the overhead of checking loop boundaries
24021 for every iteration.
24022 Some simple loops can be reversed and implemented using a
24023 \begin_inset Quotes eld
24026 decrement and jump if not zero
24027 \begin_inset Quotes erd
24031 SDCC checks for the following criterion to determine if a loop is reversible
24032 (note: more sophisticated compilers use data-dependency analysis to make
24033 this determination, SDCC uses a more simple minded analysis).
24036 The 'for' loop is of the form
24042 for(<symbol> = <expression>; <sym> [< | <=] <expression>; [<sym>++ | <sym>
24052 The <for body> does not contain
24053 \begin_inset Quotes eld
24057 \begin_inset Quotes erd
24061 \begin_inset Quotes erd
24067 All goto's are contained within the loop.
24070 No function calls within the loop.
24073 The loop control variable <sym> is not assigned any value within the loop
24076 The loop control variable does NOT participate in any arithmetic operation
24080 There are NO switch statements in the loop.
24083 Algebraic Simplifications
24086 SDCC does numerous algebraic simplifications, the following is a small sub-set
24087 of these optimizations.
24092 i = j + 0;\SpecialChar ~
24096 /* changed to: */\SpecialChar ~
24102 i /= 2;\SpecialChar ~
24109 /* changed to: */\SpecialChar ~
24115 i = j - j;\SpecialChar ~
24119 /* changed to: */\SpecialChar ~
24125 i = j / 1;\SpecialChar ~
24129 /* changed to: */\SpecialChar ~
24136 Note the subexpressions
24137 \begin_inset LatexCommand \index{Subexpression}
24141 given above are generally introduced by macro expansions or as a result
24142 of copy/constant propagation.
24145 'switch' Statements
24146 \begin_inset LatexCommand \label{sub:'switch'-Statements}
24151 \begin_inset LatexCommand \index{switch statement}
24158 SDCC can optimize switch statements to jump tables
24159 \begin_inset LatexCommand \index{jump tables}
24164 It makes the decision based on an estimate of the generated code size.
24165 SDCC is quite liberal in the requirements for jump table generation:
24168 The labels need not be in order, and the starting number need not be one
24169 or zero, the case labels are in numerical sequence or not too many case
24170 labels are missing.
24176 switch(i) {\SpecialChar ~
24207 case 4: ...\SpecialChar ~
24239 case 5: ...\SpecialChar ~
24271 case 3: ...\SpecialChar ~
24302 case 6: ...\SpecialChar ~
24334 case 7: ...\SpecialChar ~
24366 case 8: ...\SpecialChar ~
24398 case 9: ...\SpecialChar ~
24430 case 10: ...\SpecialChar ~
24461 case 11: ...\SpecialChar ~
24528 Both the above switch statements will be implemented using a jump-table.
24529 The example to the right side is slightly more efficient as the check for
24530 the lower boundary of the jump-table is not needed.
24534 The number of case labels is not larger than supported by the target architectur
24538 If the case labels are not in numerical sequence ('gaps' between cases)
24539 SDCC checks whether a jump table with additionally inserted dummy cases
24540 is still attractive.
24544 If the starting number is not zero and a check for the lower boundary of
24545 the jump-table can thus be eliminated SDCC might insert dummy cases 0,
24550 Switch statements which have large gaps in the numeric sequence or those
24551 that have too many case labels can be split into more than one switch statement
24552 for efficient code generation, e.g.:
24632 If the above switch statement is broken down into two switch statements
24722 then both the switch statements will be implemented using jump-tables whereas
24723 the unmodified switch statement will not be.
24726 There might be reasons which SDCC cannot know about to either favour or
24727 not favour jump tables.
24728 If the target system has to be as quick for the last switch case as for
24729 the first (pro jump table), or if the switch argument is known to be zero
24730 in the majority of the cases (contra jump table).
24733 The pragma nojtbound
24734 \begin_inset LatexCommand \index{\#pragma nojtbound}
24738 can be used to turn off checking the
24751 It has no effect if a default label is supplied.
24752 Use of this pragma is dangerous: if the switch
24753 \begin_inset LatexCommand \index{switch statement}
24757 argument is not matched by a case statement the processor will happily
24761 Bit-shifting Operations
24762 \begin_inset LatexCommand \index{Bit shifting}
24769 Bit shifting is one of the most frequently used operation in embedded programmin
24771 SDCC tries to implement bit-shift operations in the most efficient way
24787 generates the following code:
24804 In general SDCC will never setup a loop if the shift count is known.
24847 \begin_inset LatexCommand \index{Bit rotation}
24854 A special case of the bit-shift operation is bit rotation
24855 \begin_inset LatexCommand \index{rotating bits}
24859 , SDCC recognizes the following expression to be a left bit-rotation:
24869 char i;\SpecialChar ~
24880 /* unsigned is needed for rotation */
24885 i = ((i << 1) | (i >> 7));
24894 will generate the following code:
24913 SDCC uses pattern matching on the parse tree to determine this operation.Variatio
24914 ns of this case will also be recognized as bit-rotation, i.e.:
24919 i = ((i >> 7) | (i << 1)); /* left-bit rotation */
24922 Nibble and Byte Swapping
24925 Other special cases of the bit-shift operations are nibble or byte swapping
24926 \begin_inset LatexCommand \index{swapping nibbles/bytes}
24930 , SDCC recognizes the following expressions:
24953 i = ((i << 4) | (i >> 4));
24959 j = ((j << 8) | (j >> 8));
24962 and generates a swap instruction for the nibble swapping
24963 \begin_inset LatexCommand \index{Nibble swapping}
24967 or move instructions for the byte swapping
24968 \begin_inset LatexCommand \index{Byte swapping}
24974 \begin_inset Quotes sld
24978 \begin_inset Quotes srd
24981 example can be used to convert from little to big-endian or vice versa.
24982 If you want to change the endianness of a
24986 integer you have to cast to
24993 Note that SDCC stores numbers in little-endian
24999 Usually 8-bit processors don't care much about endianness.
25000 This is not the case for the standard 8051 which only has an instruction
25006 \begin_inset LatexCommand \index{DPTR}
25014 so little-endian is the more efficient byte order.
25018 \begin_inset LatexCommand \index{little-endian}
25023 \begin_inset LatexCommand \index{Endianness}
25028 lowest order first).
25032 \begin_inset LatexCommand \index{Highest Order Bit}
25037 \begin_inset LatexCommand \index{Any Order Bit}
25044 It is frequently required to obtain the highest order bit of an integral
25045 type (long, int, short or char types).
25046 Also obtaining any other order bit is not uncommon.
25047 SDCC recognizes the following expressions to yield the highest order bit
25048 and generates optimized code for it, e.g.:
25061 unsigned char hob1, aob1;
25065 bit hob2, hob3, aob2, aob3;
25074 hob1 = (gint >> 15) & 1;
25078 hob2 = (gint >> 15) & 1;
25082 hob3 = gint & 0x8000;
25086 aob1 = (gint >> 9) & 1;
25090 aob2 = (gint >> 8) & 1;
25094 aob3 = gint & 0x0800;
25104 will generate the following code:
25137 000A E5*01\SpecialChar ~
25164 000C 23\SpecialChar ~
25195 000D 54 01\SpecialChar ~
25222 000F F5*02\SpecialChar ~
25277 0011 E5*01\SpecialChar ~
25304 0013 33\SpecialChar ~
25334 0014 92*00\SpecialChar ~
25389 0016 E5*01\SpecialChar ~
25416 0018 33\SpecialChar ~
25446 0019 92*01\SpecialChar ~
25501 001B E5*01\SpecialChar ~
25528 001D 03\SpecialChar ~
25559 001E 54 01\SpecialChar ~
25586 0020 F5*03\SpecialChar ~
25641 0022 E5*01\SpecialChar ~
25668 0024 13\SpecialChar ~
25698 0025 92*02\SpecialChar ~
25753 0027 E5*01\SpecialChar ~
25780 0029 A2 E3\SpecialChar ~
25807 002B 92*03\SpecialChar ~
25835 Other variations of these cases however will
25840 They are standard C expressions, so I heartily recommend these be the only
25841 way to get the highest order bit, (it is portable).
25842 Of course it will be recognized even if it is embedded in other expressions,
25848 xyz = gint + ((gint >> 15) & 1);
25851 will still be recognized.
25855 \begin_inset LatexCommand \index{Higher Order Byte}
25859 / Higher Order Word
25860 \begin_inset LatexCommand \index{Higher Order Word}
25867 It is also frequently required to obtain a higher order byte or word of
25868 a larger integral type (long, int or short types).
25869 SDCC recognizes the following expressions to yield the higher order byte
25870 or word and generates optimized code for it, e.g.:
25877 unsigned long int glong;
25885 unsigned char hob1, hob2;
25889 unsigned int how1, how2;
25898 hob1 = (gint >> 8) & 0xFF;
25902 hob2 = glong >> 24;
25906 how1 = (glong >> 16) & 0xFFFF;
25920 will generate the following code:
25953 0037 85*01*06\SpecialChar ~
25975 _foo_hob1_1_1,(_gint + 1)
26005 003A 85*05*07\SpecialChar ~
26027 _foo_hob2_1_1,(_glong + 3)
26057 003D 85*04*08\SpecialChar ~
26079 _foo_how1_1_1,(_glong + 2)
26081 0040 85*05*09\SpecialChar ~
26103 (_foo_how1_1_1 + 1),(_glong + 3)
26105 0043 85*03*0A\SpecialChar ~
26127 _foo_how2_1_1,(_glong + 1)
26129 0046 85*04*0B\SpecialChar ~
26151 (_foo_how2_1_1 + 1),(_glong + 2)
26154 Again, variations of these cases may
26159 They are standard C expressions, so I heartily recommend these be the only
26160 way to get the higher order byte/word, (it is portable).
26161 Of course it will be recognized even if it is embedded in other expressions,
26167 xyz = gint + ((gint >> 8) & 0xFF);
26170 will still be recognized.
26174 \begin_inset LatexCommand \label{sub:Peephole-Optimizer}
26179 \begin_inset LatexCommand \index{Peephole optimizer}
26186 The compiler uses a rule based, pattern matching and re-writing mechanism
26187 for peep-hole optimization.
26192 a peep-hole optimizer by Christopher W.
26193 Fraser (cwfraser\SpecialChar ~
26196 A default set of rules are compiled into the compiler, additional rules
26197 may be added with the
26210 \begin_inset LatexCommand \index{-\/-peep-file}
26217 The rule language is best illustrated with examples.
26241 The above rule will change the following assembly
26242 \begin_inset LatexCommand \index{Assembler routines}
26264 Note: All occurrences of a
26268 (pattern variable) must denote the same string.
26269 With the above rule, the assembly sequence:
26279 will remain unmodified.
26283 Other special case optimizations may be added by the user (via
26299 some variants of the 8051 MCU
26300 \begin_inset LatexCommand \index{MCS51 variants}
26313 The following two rules will change all
26332 replace { lcall %1 } by { acall %1 }
26334 replace { ljmp %1 } by { ajmp %1 }
26339 inline-assembler code
26341 is also passed through the peep hole optimizer, thus the peephole optimizer
26342 can also be used as an assembly level macro expander.
26343 The rules themselves are MCU dependent whereas the rule language infra-structur
26344 e is MCU independent.
26345 Peephole optimization rules for other MCU can be easily programmed using
26350 The syntax for a rule is as follows:
26355 rule := replace [ restart ] '{' <assembly sequence> '
26393 <assembly sequence> '
26411 '}' [if <functionName> ] '
26416 <assembly sequence> := assembly instruction (each instruction including
26417 labels must be on a separate line).
26421 The optimizer will apply to the rules one by one from the top in the sequence
26422 of their appearance, it will terminate when all rules are exhausted.
26423 If the 'restart' option is specified, then the optimizer will start matching
26424 the rules again from the top, this option for a rule is expensive (performance)
26425 , it is intended to be used in situations where a transformation will trigger
26426 the same rule again.
26427 An example of this (not a good one, it has side effects) is the following
26450 Note that the replace pattern cannot be a blank, but can be a comment line.
26451 Without the 'restart' option only the innermost 'pop' 'push' pair would
26452 be eliminated, i.e.:
26482 the restart option the rule will be applied again to the resulting code
26483 and then all the pop-push pairs will be eliminated to yield:
26493 A conditional function can be attached to a rule.
26494 Attaching rules are somewhat more involved, let me illustrate this with
26521 The optimizer does a look-up of a function name table defined in function
26526 in the source file SDCCpeeph.c, with the name
26531 If it finds a corresponding entry the function is called.
26532 Note there can be no parameters specified for these functions, in this
26537 is crucial, since the function
26541 expects to find the label in that particular variable (the hash table containin
26542 g the variable bindings is passed as a parameter).
26543 If you want to code more such functions, take a close look at the function
26544 labelInRange and the calling mechanism in source file SDCCpeeph.c.
26545 Currently implemented are
26547 labelInRange, labelRefCount, labelIsReturnOnly, operandsNotSame, xramMovcOption,
26548 24bitMode, portIsDS390, 24bitModeAndPortDS390
26557 I know this whole thing is a little kludgey, but maybe some day we will
26558 have some better means.
26559 If you are looking at this file, you will see the default rules that are
26560 compiled into the compiler, you can add your own rules in the default set
26561 there if you get tired of specifying the -
26575 \begin_inset LatexCommand \index{ANSI-compliance}
26580 \begin_inset LatexCommand \label{sub:ANSI-Compliance}
26587 Deviations from the compliance:
26590 functions are not reentrant
26591 \begin_inset LatexCommand \index{reentrant}
26595 unless explicitly declared as such or the
26608 \begin_inset LatexCommand \index{-\/-stack-auto}
26614 command line option is specified.
26617 structures and unions cannot be assigned values directly, cannot be passed
26618 as function parameters or assigned to each other and cannot be a return
26619 value from a function, e.g.:
26645 s1 = s2 ; /* is invalid in SDCC although allowed in ANSI */
26656 struct s foo1 (struct s parms) /* invalid in SDCC although allowed in ANSI
26678 return rets;/* is invalid in SDCC although allowed in ANSI */
26684 initialization of structure arrays must be fully braced.
26690 struct s { char x } a[] = {1, 2}; /* invalid in SDCC */
26692 struct s { char x } a[] = {{1}, {2}}; /* OK */
26697 \begin_inset LatexCommand \index{long long (not supported)}
26702 \begin_inset LatexCommand \index{int (64 bit) (not supported)}
26710 \begin_inset LatexCommand \index{double (not supported)}
26714 ' precision floating point
26715 \begin_inset LatexCommand \index{Floating point support}
26723 \begin_inset LatexCommand \index{K\&R style}
26727 function declarations are NOT allowed.
26733 foo(i,j) /* this old style of function declarations */
26735 int i,j; /* are valid in ANSI but not valid in SDCC */
26750 Most enhancements in C99 are not supported, f.e.:
26759 int increment (int a) { return a+1; } /* is invalid in SDCC although allowed
26766 i=0; i<10; i++) /* is invalid in SDCC although allowed in C99 */
26770 Certain words that are valid identifiers in the standard may be reserved
26771 words in SDCC unless the
26784 \begin_inset LatexCommand \index{-\/-std-c89}
26799 \begin_inset LatexCommand \index{-\/-std-c99}
26805 command line options are used.
26806 These may include (depending on the selected processor): 'at', 'banked',
26807 'bit', 'code', 'critical', 'data', 'eeprom', 'far', 'flash', 'idata', 'interrup
26808 t', 'near', 'nonbanked', 'pdata', 'reentrant', 'sbit', 'sfr', 'shadowregs',
26809 'sram', 'using', 'wparam', 'xdata', '_overlay', '_asm', '_endasm', and
26811 Compliant equivalents of these keywords are always available in a form
26812 that begin with two underscores
26813 \begin_inset LatexCommand \index{\_\_ (prefix for extended keywords)}
26818 '__data' instead of 'data'.
26821 Cyclomatic Complexity
26822 \begin_inset LatexCommand \index{Cyclomatic complexity}
26829 Cyclomatic complexity of a function is defined as the number of independent
26830 paths the program can take during execution of the function.
26831 This is an important number since it defines the number test cases you
26832 have to generate to validate the function.
26833 The accepted industry standard for complexity number is 10, if the cyclomatic
26834 complexity reported by SDCC exceeds 10 you should think about simplification
26835 of the function logic.
26836 Note that the complexity level is not related to the number of lines of
26837 code in a function.
26838 Large functions can have low complexity, and small functions can have large
26844 SDCC uses the following formula to compute the complexity:
26849 complexity = (number of edges in control flow graph) - (number of nodes
26850 in control flow graph) + 2;
26854 Having said that the industry standard is 10, you should be aware that in
26855 some cases it be may unavoidable to have a complexity level of less than
26857 For example if you have switch statement with more than 10 case labels,
26858 each case label adds one to the complexity level.
26859 The complexity level is by no means an absolute measure of the algorithmic
26860 complexity of the function, it does however provide a good starting point
26861 for which functions you might look at for further optimization.
26864 Retargetting for other Processors
26867 The issues for retargetting the compiler are far too numerous to be covered
26869 What follows is a brief description of each of the seven phases of the
26870 compiler and its MCU dependency.
26873 Parsing the source and building the annotated parse tree.
26874 This phase is largely MCU independent (except for the language extensions).
26875 Syntax & semantic checks are also done in this phase, along with some initial
26876 optimizations like back patching labels and the pattern matching optimizations
26877 like bit-rotation etc.
26880 The second phase involves generating an intermediate code which can be easy
26881 manipulated during the later phases.
26882 This phase is entirely MCU independent.
26883 The intermediate code generation assumes the target machine has unlimited
26884 number of registers, and designates them with the name iTemp.
26885 The compiler can be made to dump a human readable form of the code generated
26899 This phase does the bulk of the standard optimizations and is also MCU independe
26901 This phase can be broken down into several sub-phases:
26905 Break down intermediate code (iCode) into basic blocks.
26907 Do control flow & data flow analysis on the basic blocks.
26909 Do local common subexpression elimination, then global subexpression elimination
26911 Dead code elimination
26915 If loop optimizations caused any changes then do 'global subexpression eliminati
26916 on' and 'dead code elimination' again.
26919 This phase determines the live-ranges; by live range I mean those iTemp
26920 variables defined by the compiler that still survive after all the optimization
26922 Live range analysis
26923 \begin_inset LatexCommand \index{Live range analysis}
26927 is essential for register allocation, since these computation determines
26928 which of these iTemps will be assigned to registers, and for how long.
26931 Phase five is register allocation.
26932 There are two parts to this process.
26936 The first part I call 'register packing' (for lack of a better term).
26937 In this case several MCU specific expression folding is done to reduce
26942 The second part is more MCU independent and deals with allocating registers
26943 to the remaining live ranges.
26944 A lot of MCU specific code does creep into this phase because of the limited
26945 number of index registers available in the 8051.
26948 The Code generation phase is (unhappily), entirely MCU dependent and very
26949 little (if any at all) of this code can be reused for other MCU.
26950 However the scheme for allocating a homogenized assembler operand for each
26951 iCode operand may be reused.
26954 As mentioned in the optimization section the peep-hole optimizer is rule
26955 based system, which can reprogrammed for other MCUs.
26959 \begin_inset LatexCommand \index{Compiler internals}
26966 The anatomy of the compiler
26967 \begin_inset LatexCommand \label{sub:The-anatomy-of}
26976 This is an excerpt from an article published in Circuit Cellar Magazine
26982 It's a little outdated (the compiler is much more efficient now and user/develo
26983 per friendly), but pretty well exposes the guts of it all.
26989 The current version of SDCC can generate code for Intel 8051 and Z80 MCU.
26990 It is fairly easy to retarget for other 8-bit MCU.
26991 Here we take a look at some of the internals of the compiler.
26996 \begin_inset LatexCommand \index{Parsing}
27003 Parsing the input source file and creating an AST (Annotated Syntax Tree
27004 \begin_inset LatexCommand \index{Annotated syntax tree}
27009 This phase also involves propagating types (annotating each node of the
27010 parse tree with type information) and semantic analysis.
27011 There are some MCU specific parsing rules.
27012 For example the storage classes, the extended storage classes are MCU specific
27013 while there may be a xdata storage class for 8051 there is no such storage
27014 class for z80 or Atmel AVR.
27015 SDCC allows MCU specific storage class extensions, i.e.
27016 xdata will be treated as a storage class specifier when parsing 8051 C
27017 code but will be treated as a C identifier when parsing z80 or ATMEL AVR
27022 \begin_inset LatexCommand \index{iCode}
27029 Intermediate code generation.
27030 In this phase the AST is broken down into three-operand form (iCode).
27031 These three operand forms are represented as doubly linked lists.
27032 ICode is the term given to the intermediate form generated by the compiler.
27033 ICode example section shows some examples of iCode generated for some simple
27034 C source functions.
27038 \begin_inset LatexCommand \index{Optimizations}
27045 Bulk of the target independent optimizations is performed in this phase.
27046 The optimizations include constant propagation, common sub-expression eliminati
27047 on, loop invariant code movement, strength reduction of loop induction variables
27048 and dead-code elimination.
27051 Live range analysis
27052 \begin_inset LatexCommand \index{Live range analysis}
27059 During intermediate code generation phase, the compiler assumes the target
27060 machine has infinite number of registers and generates a lot of temporary
27062 The live range computation determines the lifetime of each of these compiler-ge
27063 nerated temporaries.
27064 A picture speaks a thousand words.
27065 ICode example sections show the live range annotations for each of the
27067 It is important to note here, each iCode is assigned a number in the order
27068 of its execution in the function.
27069 The live ranges are computed in terms of these numbers.
27070 The from number is the number of the iCode which first defines the operand
27071 and the to number signifies the iCode which uses this operand last.
27074 Register Allocation
27075 \begin_inset LatexCommand \index{Register allocation}
27082 The register allocation determines the type and number of registers needed
27084 In most MCUs only a few registers can be used for indirect addressing.
27085 In case of 8051 for example the registers R0 & R1 can be used to indirectly
27086 address the internal ram and DPTR to indirectly address the external ram.
27087 The compiler will try to allocate the appropriate register to pointer variables
27089 ICode example section shows the operands annotated with the registers assigned
27091 The compiler will try to keep operands in registers as much as possible;
27092 there are several schemes the compiler uses to do achieve this.
27093 When the compiler runs out of registers the compiler will check to see
27094 if there are any live operands which is not used or defined in the current
27095 basic block being processed, if there are any found then it will push that
27096 operand and use the registers in this block, the operand will then be popped
27097 at the end of the basic block.
27101 There are other MCU specific considerations in this phase.
27102 Some MCUs have an accumulator; very short-lived operands could be assigned
27103 to the accumulator instead of a general-purpose register.
27109 Figure II gives a table of iCode operations supported by the compiler.
27110 The code generation involves translating these operations into corresponding
27111 assembly code for the processor.
27112 This sounds overly simple but that is the essence of code generation.
27113 Some of the iCode operations are generated on a MCU specific manner for
27114 example, the z80 port does not use registers to pass parameters so the
27115 SEND and RECV iCode operations will not be generated, and it also does
27116 not support JUMPTABLES.
27123 <Where is Figure II?>
27126 In the original article Figure II was announced to be downloadable on
27131 Unfortunately it never seemed to have shown up there, so: where is Figure
27136 \begin_inset LatexCommand \index{iCode}
27143 This section shows some details of iCode.
27144 The example C code does not do anything useful; it is used as an example
27145 to illustrate the intermediate code generated by the compiler.
27157 /* This function does nothing useful.
27164 for the purpose of explaining iCode */
27167 short function (data int *x)
27175 short i=10; \SpecialChar ~
27177 /* dead initialization eliminated */
27182 short sum=10; /* dead initialization eliminated */
27195 while (*x) *x++ = *p++;
27209 /* compiler detects i,j to be induction variables */
27213 for (i = 0, j = 10 ; i < 10 ; i++, j
27239 mul += i * 3; \SpecialChar ~
27241 /* this multiplication remains */
27247 gint += j * 3;\SpecialChar ~
27249 /* this multiplication changed to addition */
27263 In addition to the operands each iCode contains information about the filename
27264 and line it corresponds to in the source file.
27265 The first field in the listing should be interpreted as follows:
27270 Filename(linenumber: iCode Execution sequence number : ICode hash table
27271 key : loop depth of the iCode).
27276 Then follows the human readable form of the ICode operation.
27277 Each operand of this triplet form can be of three basic types a) compiler
27278 generated temporary b) user defined variable c) a constant value.
27279 Note that local variables and parameters are replaced by compiler generated
27282 \begin_inset LatexCommand \index{Live range analysis}
27286 are computed only for temporaries (i.e.
27287 live ranges are not computed for global variables).
27289 \begin_inset LatexCommand \index{Register allocation}
27293 are allocated for temporaries only.
27294 Operands are formatted in the following manner:
27299 Operand Name [lr live-from : live-to ] { type information } [ registers
27305 As mentioned earlier the live ranges are computed in terms of the execution
27306 sequence number of the iCodes, for example
27308 the iTemp0 is live from (i.e.
27309 first defined in iCode with execution sequence number 3, and is last used
27310 in the iCode with sequence number 5).
27311 For induction variables such as iTemp21 the live range computation extends
27312 the lifetime from the start to the end of the loop.
27314 The register allocator used the live range information to allocate registers,
27315 the same registers may be used for different temporaries if their live
27316 ranges do not overlap, for example r0 is allocated to both iTemp6 and to
27317 iTemp17 since their live ranges do not overlap.
27318 In addition the allocator also takes into consideration the type and usage
27319 of a temporary, for example itemp6 is a pointer to near space and is used
27320 as to fetch data from (i.e.
27321 used in GET_VALUE_AT_ADDRESS) so it is allocated a pointer register (r0).
27322 Some short lived temporaries are allocated to special registers which have
27323 meaning to the code generator e.g.
27324 iTemp13 is allocated to a pseudo register CC which tells the back end that
27325 the temporary is used only for a conditional jump the code generation makes
27326 use of this information to optimize a compare and jump ICode.
27328 There are several loop optimizations
27329 \begin_inset LatexCommand \index{Loop optimization}
27333 performed by the compiler.
27334 It can detect induction variables iTemp21(i) and iTemp23(j).
27335 Also note the compiler does selective strength reduction
27336 \begin_inset LatexCommand \index{Strength reduction}
27341 the multiplication of an induction variable in line 18 (gint = j * 3) is
27342 changed to addition, a new temporary iTemp17 is allocated and assigned
27343 a initial value, a constant 3 is then added for each iteration of the loop.
27344 The compiler does not change the multiplication
27345 \begin_inset LatexCommand \index{Multiplication}
27349 in line 17 however since the processor does support an 8 * 8 bit multiplication.
27351 Note the dead code elimination
27352 \begin_inset LatexCommand \index{Dead-code elimination}
27356 optimization eliminated the dead assignments in line 7 & 8 to I and sum
27364 Sample.c (5:1:0:0) _entry($9) :
27369 Sample.c(5:2:1:0) proc _function [lr0:0]{function short}
27374 Sample.c(11:3:2:0) iTemp0 [lr3:5]{_near * int}[r2] = recv
27379 Sample.c(11:4:53:0) preHeaderLbl0($11) :
27384 Sample.c(11:5:55:0) iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near
27390 Sample.c(11:6:5:1) _whilecontinue_0($1) :
27395 Sample.c(11:7:7:1) iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near *
27401 Sample.c(11:8:8:1) if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27406 Sample.c(11:9:14:1) iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far
27412 Sample.c(11:10:15:1) _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2
27418 Sample.c(11:13:18:1) iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far
27424 Sample.c(11:14:19:1) *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int
27430 Sample.c(11:15:12:1) iTemp6 [lr5:16]{_near * int}[r0] = iTemp6 [lr5:16]{_near
27431 * int}[r0] + 0x2 {short}
27436 Sample.c(11:16:20:1) goto _whilecontinue_0($1)
27441 Sample.c(11:17:21:0)_whilebreak_0($3) :
27446 Sample.c(12:18:22:0) iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27451 Sample.c(13:19:23:0) iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27456 Sample.c(15:20:54:0)preHeaderLbl1($13) :
27461 Sample.c(15:21:56:0) iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27466 Sample.c(15:22:57:0) iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27471 Sample.c(15:23:58:0) iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27476 Sample.c(15:24:26:1)_forcond_0($4) :
27481 Sample.c(15:25:27:1) iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4]
27487 Sample.c(15:26:28:1) if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
27492 Sample.c(16:27:31:1) iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2]
27493 + ITemp21 [lr21:38]{short}[r4]
27498 Sample.c(17:29:33:1) iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4]
27504 Sample.c(17:30:34:1) iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3]
27505 + iTemp15 [lr29:30]{short}[r1]
27510 Sample.c(18:32:36:1:1) iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7
27516 Sample.c(18:33:37:1) _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{
27522 Sample.c(15:36:42:1) iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4]
27528 Sample.c(15:37:45:1) iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5
27534 Sample.c(19:38:47:1) goto _forcond_0($4)
27539 Sample.c(19:39:48:0)_forbreak_0($7) :
27544 Sample.c(20:40:49:0) iTemp24 [lr40:41]{short}[DPTR] = iTemp2 [lr18:40]{short}[r2]
27545 + ITemp11 [lr19:40]{short}[r3]
27550 Sample.c(20:41:50:0) ret iTemp24 [lr40:41]{short}
27555 Sample.c(20:42:51:0)_return($8) :
27560 Sample.c(20:43:52:0) eproc _function [lr0:0]{ ia0 re0 rm0}{function short}
27566 Finally the code generated for this function:
27607 ; ----------------------------------------------
27612 ; function function
27617 ; ----------------------------------------------
27627 ; iTemp0 [lr3:5]{_near * int}[r2] = recv
27639 ; iTemp6 [lr5:16]{_near * int}[r0] := iTemp0 [lr3:5]{_near * int}[r2]
27651 ;_whilecontinue_0($1) :
27661 ; iTemp4 [lr7:8]{int}[r2 r3] = @[iTemp6 [lr5:16]{_near * int}[r0]]
27666 ; if iTemp4 [lr7:8]{int}[r2 r3] == 0 goto _whilebreak_0($3)
27725 ; iTemp7 [lr9:13]{_far * int}[DPTR] := _p [lr0:0]{_far * int}
27744 ; _p [lr0:0]{_far * int} = _p [lr0:0]{_far * int} + 0x2 {short}
27791 ; iTemp10 [lr13:14]{int}[r2 r3] = @[iTemp7 [lr9:13]{_far * int}[DPTR]]
27831 ; *(iTemp6 [lr5:16]{_near * int}[r0]) := iTemp10 [lr13:14]{int}[r2 r3]
27857 ; iTemp6 [lr5:16]{_near * int}[r0] =
27862 ; iTemp6 [lr5:16]{_near * int}[r0] +
27879 ; goto _whilecontinue_0($1)
27891 ; _whilebreak_0($3) :
27901 ; iTemp2 [lr18:40]{short}[r2] := 0x0 {short}
27913 ; iTemp11 [lr19:40]{short}[r3] := 0x0 {short}
27925 ; iTemp21 [lr21:38]{short}[r4] := 0x0 {short}
27937 ; iTemp23 [lr22:38]{int}[r5 r6] := 0xa {int}
27956 ; iTemp17 [lr23:38]{int}[r7 r0] := 0x1e {int}
27985 ; iTemp13 [lr25:26]{char}[CC] = iTemp21 [lr21:38]{short}[r4] < 0xa {short}
27990 ; if iTemp13 [lr25:26]{char}[CC] == 0 goto _forbreak_0($7)
28035 ; iTemp2 [lr18:40]{short}[r2] = iTemp2 [lr18:40]{short}[r2] +
28040 ; iTemp21 [lr21:38]{short}[r4]
28066 ; iTemp15 [lr29:30]{short}[r1] = iTemp21 [lr21:38]{short}[r4] * 0x3 {short}
28099 ; iTemp11 [lr19:40]{short}[r3] = iTemp11 [lr19:40]{short}[r3] +
28104 ; iTemp15 [lr29:30]{short}[r1]
28123 ; iTemp17 [lr23:38]{int}[r7 r0]= iTemp17 [lr23:38]{int}[r7 r0]- 0x3 {short}
28170 ; _gint [lr0:0]{int} = _gint [lr0:0]{int} + iTemp17 [lr23:38]{int}[r7 r0]
28217 ; iTemp21 [lr21:38]{short}[r4] = iTemp21 [lr21:38]{short}[r4] + 0x1 {short}
28229 ; iTemp23 [lr22:38]{int}[r5 r6]= iTemp23 [lr22:38]{int}[r5 r6]- 0x1 {short}
28243 cjne r5,#0xff,00104$
28255 ; goto _forcond_0($4)
28267 ; _forbreak_0($7) :
28277 ; ret iTemp24 [lr40:41]{short}
28320 A few words about basic block successors, predecessors and dominators
28323 Successors are basic blocks
28324 \begin_inset LatexCommand \index{Basic blocks}
28328 that might execute after this basic block.
28330 Predecessors are basic blocks that might execute before reaching this basic
28333 Dominators are basic blocks that WILL execute before reaching this basic
28367 a) succList of [BB2] = [BB4], of [BB3] = [BB4], of [BB1] = [BB2,BB3]
28370 b) predList of [BB2] = [BB1], of [BB3] = [BB1], of [BB4] = [BB2,BB3]
28373 c) domVect of [BB4] = BB1 ...
28374 here we are not sure if BB2 or BB3 was executed but we are SURE that BB1
28382 \begin_inset LatexCommand \url{http://sdcc.sourceforge.net#Who}
28392 Thanks to all the other volunteer developers who have helped with coding,
28393 testing, web-page creation, distribution sets, etc.
28394 You know who you are :-)
28401 This document was initially written by Sandeep Dutta
28404 All product names mentioned herein may be trademarks
28405 \begin_inset LatexCommand \index{Trademarks}
28409 of their respective companies.
28416 To avoid confusion, the installation and building options for SDCC itself
28417 (chapter 2) are not part of the index.
28421 \begin_inset LatexCommand \printindex{}